xref: /illumos-gate/usr/src/uts/common/io/ib/clients/rds/rdsib_ib.c (revision b86efd96f8acd85ddaa930a2f0c1d664237e4aaf)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*
26  * Copyright (c) 2005 SilverStorm Technologies, Inc. All rights reserved.
27  *
28  * This software is available to you under a choice of one of two
29  * licenses.  You may choose to be licensed under the terms of the GNU
30  * General Public License (GPL) Version 2, available from the file
31  * COPYING in the main directory of this source tree, or the
32  * OpenIB.org BSD license below:
33  *
34  *     Redistribution and use in source and binary forms, with or
35  *     without modification, are permitted provided that the following
36  *     conditions are met:
37  *
38  *	- Redistributions of source code must retain the above
39  *	  copyright notice, this list of conditions and the following
40  *	  disclaimer.
41  *
42  *	- Redistributions in binary form must reproduce the above
43  *	  copyright notice, this list of conditions and the following
44  *	  disclaimer in the documentation and/or other materials
45  *	  provided with the distribution.
46  *
47  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
48  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
49  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
50  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
51  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
52  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
53  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
54  * SOFTWARE.
55  *
56  */
57 /*
58  * Sun elects to include this software in Sun product
59  * under the OpenIB BSD license.
60  *
61  *
62  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
63  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
66  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
67  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
68  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
69  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
70  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
71  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
72  * POSSIBILITY OF SUCH DAMAGE.
73  */
74 
75 #pragma ident	"%Z%%M%	%I%	%E% SMI"
76 
77 #include <sys/types.h>
78 #include <sys/ddi.h>
79 #include <sys/sunddi.h>
80 #include <sys/ib/ibtl/ibti.h>
81 #include <sys/ib/ibtl/ibtl_types.h>
82 #include <sys/ib/clients/rds/rdsib_cm.h>
83 #include <sys/ib/clients/rds/rdsib_ib.h>
84 #include <sys/ib/clients/rds/rdsib_buf.h>
85 #include <sys/ib/clients/rds/rdsib_ep.h>
86 #include <sys/ib/clients/rds/rds_kstat.h>
87 
88 static void rds_async_handler(void *clntp, ibt_hca_hdl_t hdl,
89     ibt_async_code_t code, ibt_async_event_t *event);
90 
91 static struct ibt_clnt_modinfo_s rds_ib_modinfo = {
92 	IBTI_V2,
93 	IBT_NETWORK,
94 	rds_async_handler,
95 	NULL,
96 	"RDS"
97 };
98 
99 /* performance tunables */
100 uint_t		rds_no_interrupts = 0;
101 uint_t		rds_poll_percent_full = 25;
102 uint_t		rds_wc_signal = IBT_NEXT_SOLICITED;
103 uint_t		rds_waittime_ms = 100; /* ms */
104 
105 extern dev_info_t *rdsib_dev_info;
106 extern void rds_close_sessions();
107 
108 static void
109 rdsib_validate_chan_sizes(ibt_hca_attr_t *hattrp)
110 {
111 	/* The SQ size should not be more than that supported by the HCA */
112 	if (((MaxDataSendBuffers + RDS_NUM_ACKS) > hattrp->hca_max_chan_sz) ||
113 	    ((MaxDataSendBuffers + RDS_NUM_ACKS) > hattrp->hca_max_cq_sz)) {
114 		RDS_DPRINTF0("RDSIB", "MaxDataSendBuffers + %d is greater "
115 		    "than that supported by the HCA driver "
116 		    "(%d + %d > %d or %d), lowering it to a supported value.",
117 		    RDS_NUM_ACKS, MaxDataSendBuffers, RDS_NUM_ACKS,
118 		    hattrp->hca_max_chan_sz, hattrp->hca_max_cq_sz);
119 
120 		MaxDataSendBuffers = (hattrp->hca_max_chan_sz >
121 		    hattrp->hca_max_cq_sz) ?
122 		    hattrp->hca_max_cq_sz - RDS_NUM_ACKS :
123 		    hattrp->hca_max_chan_sz - RDS_NUM_ACKS;
124 	}
125 
126 	/* The RQ size should not be more than that supported by the HCA */
127 	if ((MaxDataRecvBuffers > hattrp->hca_max_chan_sz) ||
128 	    (MaxDataRecvBuffers > hattrp->hca_max_cq_sz)) {
129 		RDS_DPRINTF0("RDSIB", "MaxDataRecvBuffers is greater than that "
130 		    "supported by the HCA driver (%d > %d or %d), lowering it "
131 		    "to a supported value.", MaxDataRecvBuffers,
132 		    hattrp->hca_max_chan_sz, hattrp->hca_max_cq_sz);
133 
134 		MaxDataRecvBuffers = (hattrp->hca_max_chan_sz >
135 		    hattrp->hca_max_cq_sz) ? hattrp->hca_max_cq_sz :
136 		    hattrp->hca_max_chan_sz;
137 	}
138 
139 	/* The SQ size should not be more than that supported by the HCA */
140 	if ((MaxCtrlSendBuffers > hattrp->hca_max_chan_sz) ||
141 	    (MaxCtrlSendBuffers > hattrp->hca_max_cq_sz)) {
142 		RDS_DPRINTF0("RDSIB", "MaxCtrlSendBuffers is greater than that "
143 		    "supported by the HCA driver (%d > %d or %d), lowering it "
144 		    "to a supported value.", MaxCtrlSendBuffers,
145 		    hattrp->hca_max_chan_sz, hattrp->hca_max_cq_sz);
146 
147 		MaxCtrlSendBuffers = (hattrp->hca_max_chan_sz >
148 		    hattrp->hca_max_cq_sz) ? hattrp->hca_max_cq_sz :
149 		    hattrp->hca_max_chan_sz;
150 	}
151 
152 	/* The RQ size should not be more than that supported by the HCA */
153 	if ((MaxCtrlRecvBuffers > hattrp->hca_max_chan_sz) ||
154 	    (MaxCtrlRecvBuffers > hattrp->hca_max_cq_sz)) {
155 		RDS_DPRINTF0("RDSIB", "MaxCtrlRecvBuffers is greater than that "
156 		    "supported by the HCA driver (%d > %d or %d), lowering it "
157 		    "to a supported value.", MaxCtrlRecvBuffers,
158 		    hattrp->hca_max_chan_sz, hattrp->hca_max_cq_sz);
159 
160 		MaxCtrlRecvBuffers = (hattrp->hca_max_chan_sz >
161 		    hattrp->hca_max_cq_sz) ? hattrp->hca_max_cq_sz :
162 		    hattrp->hca_max_chan_sz;
163 	}
164 
165 	/* The MaxRecvMemory should be less than that supported by the HCA */
166 	if ((MaxRecvMemory * 1024) > hattrp->hca_max_memr_len) {
167 		RDS_DPRINTF0("RDSIB", "MaxRecvMemory is greater than that "
168 		    "supported by the HCA driver (%d > %d), lowering it to %d",
169 		    MaxRecvMemory, hattrp->hca_max_memr_len,
170 		    hattrp->hca_max_memr_len);
171 
172 		MaxRecvMemory = hattrp->hca_max_memr_len;
173 	}
174 }
175 
176 /*
177  * Called on open of first RDS socket
178  */
179 int
180 rdsib_open_ib()
181 {
182 	ib_guid_t	*guidp;
183 	rds_hca_t	*hcap, *hcap1;
184 	uint_t		ix, hcaix, nhcas;
185 	int		ret;
186 
187 	RDS_DPRINTF4("rdsib_open_ib", "enter");
188 
189 	ASSERT(rdsib_statep != NULL);
190 	if (rdsib_statep == NULL) {
191 		RDS_DPRINTF1("rdsib_open_ib", "RDS Statep not initialized");
192 		return (-1);
193 	}
194 
195 	/* How many hcas are there? */
196 	nhcas = ibt_get_hca_list(&guidp);
197 	if (nhcas == 0) {
198 		RDS_DPRINTF2("rdsib_open_ib", "No IB HCAs Available");
199 		return (-1);
200 	}
201 
202 	RDS_DPRINTF3("rdsib_open_ib", "Number of HCAs: %d", nhcas);
203 
204 	/* Register with IBTF */
205 	ret = ibt_attach(&rds_ib_modinfo, rdsib_dev_info, rdsib_statep,
206 	    &rdsib_statep->rds_ibhdl);
207 	if (ret != IBT_SUCCESS) {
208 		RDS_DPRINTF2(LABEL, "ibt_attach failed: %d", ret);
209 		(void) ibt_free_hca_list(guidp, nhcas);
210 		return (-1);
211 	}
212 
213 	/*
214 	 * Open each HCA and gather its information. Don't care about HCAs
215 	 * that cannot be opened. It is OK as long as atleast one HCA can be
216 	 * opened.
217 	 * Initialize a HCA only if all the information is available.
218 	 */
219 	hcap1 = NULL;
220 	for (ix = 0, hcaix = 0; ix < nhcas; ix++) {
221 		RDS_DPRINTF3(LABEL, "Open HCA: 0x%llx", guidp[ix]);
222 
223 		hcap = (rds_hca_t *)kmem_zalloc(sizeof (rds_hca_t), KM_SLEEP);
224 
225 		ret = ibt_open_hca(rdsib_statep->rds_ibhdl, guidp[ix],
226 		    &hcap->hca_hdl);
227 		if (ret != IBT_SUCCESS) {
228 			RDS_DPRINTF2("rdsib_open_ib",
229 			    "ibt_open_hca: 0x%llx failed: %d", guidp[ix], ret);
230 			kmem_free(hcap, sizeof (rds_hca_t));
231 			continue;
232 		}
233 
234 		hcap->hca_guid = guidp[ix];
235 
236 		ret = ibt_query_hca(hcap->hca_hdl, &hcap->hca_attr);
237 		if (ret != IBT_SUCCESS) {
238 			RDS_DPRINTF2("rdsib_open_ib",
239 			    "Query HCA: 0x%llx failed:  %d", guidp[ix], ret);
240 			ret = ibt_close_hca(hcap->hca_hdl);
241 			ASSERT(ret == IBT_SUCCESS);
242 			kmem_free(hcap, sizeof (rds_hca_t));
243 			continue;
244 		}
245 
246 		ret = ibt_query_hca_ports(hcap->hca_hdl, 0,
247 		    &hcap->hca_pinfop, &hcap->hca_nports, &hcap->hca_pinfo_sz);
248 		if (ret != IBT_SUCCESS) {
249 			RDS_DPRINTF2("rdsib_open_ib",
250 			    "Query HCA 0x%llx ports failed: %d", guidp[ix],
251 			    ret);
252 			ret = ibt_close_hca(hcap->hca_hdl);
253 			ASSERT(ret == IBT_SUCCESS);
254 			kmem_free(hcap, sizeof (rds_hca_t));
255 			continue;
256 		}
257 
258 		/* Only one PD per HCA is allocated, so do it here */
259 		ret = ibt_alloc_pd(hcap->hca_hdl, IBT_PD_NO_FLAGS,
260 		    &hcap->hca_pdhdl);
261 		if (ret != IBT_SUCCESS) {
262 			RDS_DPRINTF2(LABEL, "ibt_alloc_pd 0x%llx failed: %d",
263 			    guidp[ix], ret);
264 			(void) ibt_free_portinfo(hcap->hca_pinfop,
265 			    hcap->hca_pinfo_sz);
266 			ret = ibt_close_hca(hcap->hca_hdl);
267 			ASSERT(ret == IBT_SUCCESS);
268 			kmem_free(hcap, sizeof (rds_hca_t));
269 			continue;
270 		}
271 
272 		rdsib_validate_chan_sizes(&hcap->hca_attr);
273 
274 		/* this HCA is fully initialized, go to the next one */
275 		hcaix++;
276 		hcap->hca_nextp = hcap1;
277 		hcap1 = hcap;
278 	}
279 
280 	/* free the HCA list, we are done with it */
281 	(void) ibt_free_hca_list(guidp, nhcas);
282 
283 	if (hcaix == 0) {
284 		/* Failed to Initialize even one HCA */
285 		RDS_DPRINTF2("rdsib_open_ib", "No HCAs are initialized");
286 		(void) ibt_detach(rdsib_statep->rds_ibhdl);
287 		rdsib_statep->rds_ibhdl = NULL;
288 		return (-1);
289 	}
290 
291 	if (hcaix < nhcas) {
292 		RDS_DPRINTF2("rdsib_open_ib", "HCAs %d/%d failed to initialize",
293 		    (nhcas - hcaix), nhcas);
294 	}
295 
296 	rdsib_statep->rds_hcalistp = hcap1;
297 	rdsib_statep->rds_nhcas = hcaix;
298 
299 	/* register the RDS service */
300 	rdsib_statep->rds_srvhdl =
301 	    rds_register_service(rdsib_statep->rds_ibhdl);
302 	if (rdsib_statep->rds_srvhdl == NULL) {
303 		RDS_DPRINTF2("rdsib_open_ib", "Service registration failed");
304 	} else {
305 		/* bind the service on all available ports */
306 		ret = rds_bind_service(rdsib_statep);
307 		if (ret != 0) {
308 			RDS_DPRINTF2("rdsib_open_ib", "Bind service failed");
309 		}
310 	}
311 
312 	RDS_DPRINTF4("rdsib_open_ib", "return");
313 
314 	return (0);
315 }
316 
317 /*
318  * Called when all ports are closed.
319  */
320 void
321 rdsib_close_ib()
322 {
323 	rds_hca_t	*hcap;
324 	int		ret;
325 
326 	RDS_DPRINTF4("rds_close_ib", "enter");
327 
328 	if (rdsib_statep->rds_srvhdl != NULL) {
329 		(void) ibt_unbind_all_services(rdsib_statep->rds_srvhdl);
330 		(void) ibt_deregister_service(rdsib_statep->rds_ibhdl,
331 		    rdsib_statep->rds_srvhdl);
332 	}
333 
334 	/* close and destroy all the sessions */
335 	rds_close_sessions(NULL);
336 
337 	/* Release all IB resources */
338 	hcap = rdsib_statep->rds_hcalistp;
339 	while (hcap != NULL) {
340 		rdsib_statep->rds_hcalistp = hcap->hca_nextp;
341 
342 		ret = ibt_free_pd(hcap->hca_hdl, hcap->hca_pdhdl);
343 		ASSERT(ret == IBT_SUCCESS);
344 
345 		(void) ibt_free_portinfo(hcap->hca_pinfop, hcap->hca_pinfo_sz);
346 
347 		ret = ibt_close_hca(hcap->hca_hdl);
348 		ASSERT(ret == IBT_SUCCESS);
349 
350 		kmem_free(hcap, sizeof (rds_hca_t));
351 		hcap = rdsib_statep->rds_hcalistp;
352 	}
353 
354 	/* Deregister with IBTF */
355 	if (rdsib_statep->rds_ibhdl != NULL) {
356 		(void) ibt_detach(rdsib_statep->rds_ibhdl);
357 		rdsib_statep->rds_ibhdl = NULL;
358 	}
359 
360 	RDS_DPRINTF4("rds_close_ib", "return");
361 }
362 
363 /* Return hcap, given the hca guid */
364 rds_hca_t *
365 rds_get_hcap(rds_state_t *statep, ib_guid_t hca_guid)
366 {
367 	rds_hca_t	*hcap;
368 
369 	RDS_DPRINTF4("rds_get_hcap", "rds_get_hcap: Enter: statep: 0x%p "
370 	    "guid: %llx", statep, hca_guid);
371 
372 	rw_enter(&statep->rds_hca_lock, RW_READER);
373 
374 	hcap = statep->rds_hcalistp;
375 	while ((hcap != NULL) && (hcap->hca_guid != hca_guid)) {
376 		hcap = hcap->hca_nextp;
377 	}
378 
379 	rw_exit(&statep->rds_hca_lock);
380 
381 	RDS_DPRINTF4("rds_get_hcap", "rds_get_hcap: return");
382 
383 	return (hcap);
384 }
385 
386 /* Return hcap, given a gid */
387 rds_hca_t *
388 rds_gid_to_hcap(rds_state_t *statep, ib_gid_t gid)
389 {
390 	ibt_node_info_t	nodeinfo;
391 	int		ret;
392 
393 	RDS_DPRINTF4("rds_gid_to_hcap", "Enter: statep: 0x%p gid: %llx:%llx",
394 	    statep, gid.gid_prefix, gid.gid_guid);
395 
396 	ret = ibt_gid_to_node_info(gid, &nodeinfo);
397 	if (ret != IBT_SUCCESS) {
398 		RDS_DPRINTF2(LABEL, "ibt_gid_node_info for gid: %llx:%llx "
399 		    "failed", gid.gid_prefix, gid.gid_guid);
400 		return (NULL);
401 	}
402 
403 	return (rds_get_hcap(statep, nodeinfo.n_node_guid));
404 }
405 
406 /* This is called from the send CQ handler */
407 void
408 rds_send_acknowledgement(rds_ep_t *ep)
409 {
410 	int	ret;
411 	uint_t	ix;
412 
413 	RDS_DPRINTF4("rds_send_acknowledgement", "Enter EP(%p)", ep);
414 
415 	mutex_enter(&ep->ep_lock);
416 
417 	ASSERT(ep->ep_rdmacnt != 0);
418 
419 	/*
420 	 * The previous ACK completed successfully, send the next one
421 	 * if more messages were received after sending the last ACK
422 	 */
423 	if (ep->ep_rbufid != *(uintptr_t *)(uintptr_t)ep->ep_ackds.ds_va) {
424 		*(uintptr_t *)(uintptr_t)ep->ep_ackds.ds_va = ep->ep_rbufid;
425 		mutex_exit(&ep->ep_lock);
426 
427 		/* send acknowledgement */
428 		RDS_INCR_TXACKS();
429 		ret = ibt_post_send(ep->ep_chanhdl, &ep->ep_ackwr, 1, &ix);
430 		if (ret != IBT_SUCCESS) {
431 			RDS_DPRINTF1("rds_send_acknowledgement",
432 			    "EP(%p): ibt_post_send for acknowledgement "
433 			    "failed: %d, SQ depth: %d",
434 			    ep, ret, ep->ep_sndpool.pool_nbusy);
435 			mutex_enter(&ep->ep_lock);
436 			ep->ep_rdmacnt--;
437 			mutex_exit(&ep->ep_lock);
438 		}
439 	} else {
440 		/* ACKed all messages, no more to ACK */
441 		ep->ep_rdmacnt--;
442 		mutex_exit(&ep->ep_lock);
443 		return;
444 	}
445 
446 	RDS_DPRINTF4("rds_send_acknowledgement", "Return EP(%p)", ep);
447 }
448 
449 static int
450 rds_poll_ctrl_completions(ibt_cq_hdl_t cq, rds_ep_t *ep)
451 {
452 	ibt_wc_t	wc;
453 	uint_t		npolled;
454 	rds_buf_t	*bp;
455 	rds_ctrl_pkt_t	*cpkt;
456 	rds_qp_t	*recvqp;
457 	int		ret = IBT_SUCCESS;
458 
459 	RDS_DPRINTF4("rds_poll_ctrl_completions", "Enter: EP(%p)", ep);
460 
461 	bzero(&wc, sizeof (ibt_wc_t));
462 	ret = ibt_poll_cq(cq, &wc, 1, &npolled);
463 	if (ret != IBT_SUCCESS) {
464 		if (ret != IBT_CQ_EMPTY) {
465 			RDS_DPRINTF2(LABEL, "EP(%p) CQ(%p): ibt_poll_cq "
466 			    "returned: %d", ep, cq, ret);
467 		} else {
468 			RDS_DPRINTF5(LABEL, "EP(%p) CQ(%p): ibt_poll_cq "
469 			    "returned: IBT_CQ_EMPTY", ep, cq);
470 		}
471 		return (ret);
472 	}
473 
474 	bp = (rds_buf_t *)(uintptr_t)wc.wc_id;
475 
476 	if (wc.wc_status != IBT_WC_SUCCESS) {
477 		mutex_enter(&ep->ep_recvqp.qp_lock);
478 		ep->ep_recvqp.qp_level--;
479 		mutex_exit(&ep->ep_recvqp.qp_lock);
480 
481 		/* Free the buffer */
482 		bp->buf_state = RDS_RCVBUF_FREE;
483 		rds_free_recv_buf(bp, 1);
484 
485 		/* Receive completion failure */
486 		if (wc.wc_status != IBT_WC_WR_FLUSHED_ERR) {
487 			RDS_DPRINTF2("rds_poll_ctrl_completions",
488 			    "EP(%p) CQ(%p) BP(%p): WC Error Status: %d",
489 			    ep, cq, wc.wc_id, wc.wc_status);
490 		}
491 		return (ret);
492 	}
493 
494 	/* there is one less in the RQ */
495 	recvqp = &ep->ep_recvqp;
496 	mutex_enter(&recvqp->qp_lock);
497 	recvqp->qp_level--;
498 	if ((recvqp->qp_taskqpending == B_FALSE) &&
499 	    (recvqp->qp_level <= recvqp->qp_lwm)) {
500 		/* Time to post more buffers into the RQ */
501 		recvqp->qp_taskqpending = B_TRUE;
502 		mutex_exit(&recvqp->qp_lock);
503 
504 		ret = ddi_taskq_dispatch(rds_taskq,
505 		    rds_post_recv_buf, (void *)ep->ep_chanhdl, DDI_NOSLEEP);
506 		if (ret != DDI_SUCCESS) {
507 			RDS_DPRINTF1(LABEL, "ddi_taskq_dispatch failed: %d",
508 			    ret);
509 			mutex_enter(&recvqp->qp_lock);
510 			recvqp->qp_taskqpending = B_FALSE;
511 			mutex_exit(&recvqp->qp_lock);
512 		}
513 	} else {
514 		mutex_exit(&recvqp->qp_lock);
515 	}
516 
517 	cpkt = (rds_ctrl_pkt_t *)(uintptr_t)bp->buf_ds.ds_va;
518 	rds_handle_control_message(ep->ep_sp, cpkt);
519 
520 	bp->buf_state = RDS_RCVBUF_FREE;
521 	rds_free_recv_buf(bp, 1);
522 
523 	RDS_DPRINTF4("rds_poll_ctrl_completions", "Return: EP(%p)", ep);
524 
525 	return (ret);
526 }
527 
528 #define	RDS_POST_FEW_ATATIME	100
529 /* Post recv WRs into the RQ. Assumes the ep->refcnt is already incremented */
530 void
531 rds_post_recv_buf(void *arg)
532 {
533 	ibt_channel_hdl_t	chanhdl;
534 	rds_ep_t		*ep;
535 	rds_session_t		*sp;
536 	rds_qp_t		*recvqp;
537 	rds_bufpool_t		*gp;
538 	rds_buf_t		*bp, *bp1;
539 	ibt_recv_wr_t		*wrp, wr[RDS_POST_FEW_ATATIME];
540 	rds_hca_t		*hcap;
541 	uint_t			npost, nspace, rcv_len;
542 	uint_t			ix, jx, kx;
543 	int			ret;
544 
545 	chanhdl = (ibt_channel_hdl_t)arg;
546 	RDS_DPRINTF4("rds_post_recv_buf", "Enter: CHAN(%p)", chanhdl);
547 	RDS_INCR_POST_RCV_BUF_CALLS();
548 
549 	ep = (rds_ep_t *)ibt_get_chan_private(chanhdl);
550 	ASSERT(ep != NULL);
551 	sp = ep->ep_sp;
552 	recvqp = &ep->ep_recvqp;
553 
554 	RDS_DPRINTF5("rds_post_recv_buf", "EP(%p)", ep);
555 
556 	/* get the hcap for the HCA hosting this channel */
557 	hcap = rds_get_hcap(rdsib_statep, ep->ep_hca_guid);
558 	if (hcap == NULL) {
559 		RDS_DPRINTF2("rds_post_recv_buf", "HCA (0x%llx) not found",
560 		    ep->ep_hca_guid);
561 		return;
562 	}
563 
564 	/* Make sure the session is still connected */
565 	rw_enter(&sp->session_lock, RW_READER);
566 	if ((sp->session_state != RDS_SESSION_STATE_INIT) &&
567 	    (sp->session_state != RDS_SESSION_STATE_CONNECTED)) {
568 		RDS_DPRINTF2("rds_post_recv_buf", "EP(%p): Session is not "
569 		    "in active state (%d)", ep, sp->session_state);
570 		rw_exit(&sp->session_lock);
571 		return;
572 	}
573 	rw_exit(&sp->session_lock);
574 
575 	/* how many can be posted */
576 	mutex_enter(&recvqp->qp_lock);
577 	nspace = recvqp->qp_depth - recvqp->qp_level;
578 	if (nspace == 0) {
579 		RDS_DPRINTF2("rds_post_recv_buf", "RQ is FULL");
580 		recvqp->qp_taskqpending = B_FALSE;
581 		mutex_exit(&recvqp->qp_lock);
582 		return;
583 	}
584 	mutex_exit(&recvqp->qp_lock);
585 
586 	if (ep->ep_type == RDS_EP_TYPE_DATA) {
587 		gp = &rds_dpool;
588 		rcv_len = RdsPktSize;
589 	} else {
590 		gp = &rds_cpool;
591 		rcv_len = RDS_CTRLPKT_SIZE;
592 	}
593 
594 	bp = rds_get_buf(gp, nspace, &jx);
595 	if (bp == NULL) {
596 		RDS_DPRINTF2(LABEL, "EP(%p): No Recv buffers available", ep);
597 		/* try again later */
598 		ret = ddi_taskq_dispatch(rds_taskq, rds_post_recv_buf,
599 		    (void *)ep->ep_chanhdl, DDI_NOSLEEP);
600 		if (ret != DDI_SUCCESS) {
601 			RDS_DPRINTF1(LABEL, "ddi_taskq_dispatch failed: %d",
602 			    ret);
603 			mutex_enter(&recvqp->qp_lock);
604 			recvqp->qp_taskqpending = B_FALSE;
605 			mutex_exit(&recvqp->qp_lock);
606 		}
607 		return;
608 	}
609 
610 	if (jx != nspace) {
611 		RDS_DPRINTF2(LABEL, "EP(%p): Recv buffers "
612 		    "needed: %d available: %d", ep, nspace, jx);
613 		nspace = jx;
614 	}
615 
616 	bp1 = bp;
617 	for (ix = 0; ix < nspace; ix++) {
618 		bp1->buf_ep = ep;
619 		ASSERT(bp1->buf_state == RDS_RCVBUF_FREE);
620 		bp1->buf_state = RDS_RCVBUF_POSTED;
621 		bp1->buf_ds.ds_key = hcap->hca_lkey;
622 		bp1->buf_ds.ds_len = rcv_len;
623 		bp1 = bp1->buf_nextp;
624 	}
625 
626 #if 0
627 	wrp = kmem_zalloc(RDS_POST_FEW_ATATIME * sizeof (ibt_recv_wr_t),
628 	    KM_SLEEP);
629 #else
630 	wrp = &wr[0];
631 #endif
632 
633 	npost = nspace;
634 	while (npost) {
635 		jx = (npost > RDS_POST_FEW_ATATIME) ?
636 		    RDS_POST_FEW_ATATIME : npost;
637 		for (ix = 0; ix < jx; ix++) {
638 			wrp[ix].wr_id = (uintptr_t)bp;
639 			wrp[ix].wr_nds = 1;
640 			wrp[ix].wr_sgl = &bp->buf_ds;
641 			bp = bp->buf_nextp;
642 		}
643 
644 		ret = ibt_post_recv(chanhdl, wrp, jx, &kx);
645 		if ((ret != IBT_SUCCESS) || (kx != jx)) {
646 			RDS_DPRINTF1(LABEL, "ibt_post_recv for %d WRs failed: "
647 			    "%d", npost, ret);
648 			npost -= kx;
649 			break;
650 		}
651 
652 		npost -= jx;
653 	}
654 
655 	mutex_enter(&recvqp->qp_lock);
656 	if (npost != 0) {
657 		RDS_DPRINTF2("rds_post_recv_buf",
658 		    "EP(%p) Failed to post %d WRs", ep, npost);
659 		recvqp->qp_level += (nspace - npost);
660 	} else {
661 		recvqp->qp_level += nspace;
662 	}
663 
664 	/*
665 	 * sometimes, the recv WRs can get consumed as soon as they are
666 	 * posted. In that case, taskq thread to post more WRs to the RQ will
667 	 * not be scheduled as the taskqpending flag is still set.
668 	 */
669 	if (recvqp->qp_level == 0) {
670 		mutex_exit(&recvqp->qp_lock);
671 		ret = ddi_taskq_dispatch(rds_taskq,
672 		    rds_post_recv_buf, (void *)ep->ep_chanhdl, DDI_NOSLEEP);
673 		if (ret != DDI_SUCCESS) {
674 			RDS_DPRINTF1("rds_post_recv_buf",
675 			    "ddi_taskq_dispatch failed: %d", ret);
676 			mutex_enter(&recvqp->qp_lock);
677 			recvqp->qp_taskqpending = B_FALSE;
678 			mutex_exit(&recvqp->qp_lock);
679 		}
680 	} else {
681 		recvqp->qp_taskqpending = B_FALSE;
682 		mutex_exit(&recvqp->qp_lock);
683 	}
684 
685 #if 0
686 	kmem_free(wrp, RDS_POST_FEW_ATATIME * sizeof (ibt_recv_wr_t));
687 #endif
688 
689 	RDS_DPRINTF4("rds_post_recv_buf", "Return: EP(%p)", ep);
690 }
691 
692 static int
693 rds_poll_data_completions(ibt_cq_hdl_t cq, rds_ep_t *ep)
694 {
695 	ibt_wc_t	wc;
696 	rds_buf_t	*bp;
697 	rds_data_hdr_t	*pktp;
698 	rds_qp_t	*recvqp;
699 	uint_t		npolled;
700 	int		ret = IBT_SUCCESS;
701 
702 
703 	RDS_DPRINTF4("rds_poll_data_completions", "Enter: EP(%p)", ep);
704 
705 	bzero(&wc, sizeof (ibt_wc_t));
706 	ret = ibt_poll_cq(cq, &wc, 1, &npolled);
707 	if (ret != IBT_SUCCESS) {
708 		if (ret != IBT_CQ_EMPTY) {
709 			RDS_DPRINTF2(LABEL, "EP(%p) CQ(%p): ibt_poll_cq "
710 			    "returned: %d", ep, cq, ret);
711 		} else {
712 			RDS_DPRINTF5(LABEL, "EP(%p) CQ(%p): ibt_poll_cq "
713 			    "returned: IBT_CQ_EMPTY", ep, cq);
714 		}
715 		return (ret);
716 	}
717 
718 	bp = (rds_buf_t *)(uintptr_t)wc.wc_id;
719 	ASSERT(bp->buf_state == RDS_RCVBUF_POSTED);
720 	bp->buf_state = RDS_RCVBUF_ONSOCKQ;
721 	bp->buf_nextp = NULL;
722 
723 	if (wc.wc_status != IBT_WC_SUCCESS) {
724 		mutex_enter(&ep->ep_recvqp.qp_lock);
725 		ep->ep_recvqp.qp_level--;
726 		mutex_exit(&ep->ep_recvqp.qp_lock);
727 
728 		/* free the buffer */
729 		bp->buf_state = RDS_RCVBUF_FREE;
730 		rds_free_recv_buf(bp, 1);
731 
732 		/* Receive completion failure */
733 		if (wc.wc_status != IBT_WC_WR_FLUSHED_ERR) {
734 			RDS_DPRINTF2("rds_poll_data_completions",
735 			    "EP(%p) CQ(%p) BP(%p): WC Error Status: %d",
736 			    ep, cq, wc.wc_id, wc.wc_status);
737 			RDS_INCR_RXERRS();
738 		}
739 		return (ret);
740 	}
741 
742 	/* there is one less in the RQ */
743 	recvqp = &ep->ep_recvqp;
744 	mutex_enter(&recvqp->qp_lock);
745 	recvqp->qp_level--;
746 	if ((recvqp->qp_taskqpending == B_FALSE) &&
747 	    (recvqp->qp_level <= recvqp->qp_lwm)) {
748 		/* Time to post more buffers into the RQ */
749 		recvqp->qp_taskqpending = B_TRUE;
750 		mutex_exit(&recvqp->qp_lock);
751 
752 		ret = ddi_taskq_dispatch(rds_taskq,
753 		    rds_post_recv_buf, (void *)ep->ep_chanhdl, DDI_NOSLEEP);
754 		if (ret != DDI_SUCCESS) {
755 			RDS_DPRINTF1(LABEL, "ddi_taskq_dispatch failed: %d",
756 			    ret);
757 			mutex_enter(&recvqp->qp_lock);
758 			recvqp->qp_taskqpending = B_FALSE;
759 			mutex_exit(&recvqp->qp_lock);
760 		}
761 	} else {
762 		mutex_exit(&recvqp->qp_lock);
763 	}
764 
765 	pktp = (rds_data_hdr_t *)(uintptr_t)bp->buf_ds.ds_va;
766 	ASSERT(pktp->dh_datalen != 0);
767 
768 	RDS_DPRINTF5(LABEL, "Message Received: sendIP: 0x%x recvIP: 0x%x "
769 	    "sendport: %d recvport: %d npkts: %d pktno: %d", ep->ep_remip,
770 	    ep->ep_myip, pktp->dh_sendport, pktp->dh_recvport,
771 	    pktp->dh_npkts, pktp->dh_psn);
772 
773 	RDS_DPRINTF3(LABEL, "BP(%p): npkts: %d psn: %d", bp,
774 	    pktp->dh_npkts, pktp->dh_psn);
775 
776 	if (pktp->dh_npkts == 1) {
777 		/* single pkt or last packet */
778 		if (pktp->dh_psn != 0) {
779 			/* last packet of a segmented message */
780 			ASSERT(ep->ep_seglbp != NULL);
781 			ep->ep_seglbp->buf_nextp = bp;
782 			ep->ep_seglbp = bp;
783 			rds_received_msg(ep, ep->ep_segfbp);
784 			ep->ep_segfbp = NULL;
785 			ep->ep_seglbp = NULL;
786 		} else {
787 			/* single packet */
788 			rds_received_msg(ep, bp);
789 		}
790 	} else {
791 		/* multi-pkt msg */
792 		if (pktp->dh_psn == 0) {
793 			/* first packet */
794 			ASSERT(ep->ep_segfbp == NULL);
795 			ep->ep_segfbp = bp;
796 			ep->ep_seglbp = bp;
797 		} else {
798 			/* intermediate packet */
799 			ASSERT(ep->ep_segfbp != NULL);
800 			ep->ep_seglbp->buf_nextp = bp;
801 			ep->ep_seglbp = bp;
802 		}
803 	}
804 
805 	RDS_DPRINTF4("rds_poll_data_completions", "Return: EP(%p)", ep);
806 
807 	return (ret);
808 }
809 
810 void
811 rds_recvcq_handler(ibt_cq_hdl_t cq, void *arg)
812 {
813 	rds_ep_t	*ep;
814 	int		ret = IBT_SUCCESS;
815 	int		(*func)(ibt_cq_hdl_t, rds_ep_t *);
816 
817 	ep = (rds_ep_t *)arg;
818 
819 	RDS_DPRINTF4("rds_recvcq_handler", "enter: EP(%p)", ep);
820 
821 	if (ep->ep_type == RDS_EP_TYPE_DATA) {
822 		func = rds_poll_data_completions;
823 	} else {
824 		func = rds_poll_ctrl_completions;
825 	}
826 
827 	do {
828 		ret = func(cq, ep);
829 	} while (ret != IBT_CQ_EMPTY);
830 
831 	/* enable the CQ */
832 	ret = ibt_enable_cq_notify(cq, rds_wc_signal);
833 	if (ret != IBT_SUCCESS) {
834 		RDS_DPRINTF2(LABEL, "EP(%p) CQ(%p): ibt_enable_cq_notify "
835 		    "failed: %d", ep, cq, ret);
836 		return;
837 	}
838 
839 	do {
840 		ret = func(cq, ep);
841 	} while (ret != IBT_CQ_EMPTY);
842 
843 	RDS_DPRINTF4("rds_recvcq_handler", "Return: EP(%p)", ep);
844 }
845 
846 void
847 rds_poll_send_completions(ibt_cq_hdl_t cq, rds_ep_t *ep, boolean_t lock)
848 {
849 	ibt_wc_t	wc[RDS_NUM_DATA_SEND_WCS];
850 	uint_t		npolled, nret, send_error = 0;
851 	rds_buf_t	*headp, *tailp, *bp;
852 	int		ret, ix;
853 
854 	RDS_DPRINTF4("rds_poll_send_completions", "Enter EP(%p)", ep);
855 
856 	headp = NULL;
857 	tailp = NULL;
858 	npolled = 0;
859 	do {
860 		ret = ibt_poll_cq(cq, wc, RDS_NUM_DATA_SEND_WCS, &nret);
861 		if (ret != IBT_SUCCESS) {
862 			if (ret != IBT_CQ_EMPTY) {
863 				RDS_DPRINTF2(LABEL, "EP(%p) CQ(%p): "
864 				    "ibt_poll_cq returned: %d", ep, cq, ret);
865 			} else {
866 				RDS_DPRINTF5(LABEL, "EP(%p) CQ(%p): "
867 				    "ibt_poll_cq returned: IBT_CQ_EMPTY",
868 				    ep, cq);
869 			}
870 
871 			break;
872 		}
873 
874 		for (ix = 0; ix < nret; ix++) {
875 			if (wc[ix].wc_status == IBT_WC_SUCCESS) {
876 				if (wc[ix].wc_type == IBT_WRC_RDMAW) {
877 					rds_send_acknowledgement(ep);
878 					continue;
879 				}
880 
881 				bp = (rds_buf_t *)(uintptr_t)wc[ix].wc_id;
882 				ASSERT(bp->buf_state == RDS_SNDBUF_PENDING);
883 				bp->buf_state = RDS_SNDBUF_FREE;
884 			} else if (wc[ix].wc_status == IBT_WC_WR_FLUSHED_ERR) {
885 				RDS_INCR_TXERRS();
886 				RDS_DPRINTF5("rds_poll_send_completions",
887 				    "EP(%p): WC ID: %p ERROR: %d", ep,
888 				    wc[ix].wc_id, wc[ix].wc_status);
889 
890 				if (wc[ix].wc_id == RDS_RDMAW_WRID) {
891 					mutex_enter(&ep->ep_lock);
892 					ep->ep_rdmacnt--;
893 					mutex_exit(&ep->ep_lock);
894 					continue;
895 				}
896 
897 				bp = (rds_buf_t *)(uintptr_t)wc[ix].wc_id;
898 				bp->buf_state = RDS_SNDBUF_ERROR;
899 			} else {
900 				RDS_INCR_TXERRS();
901 				RDS_DPRINTF2("rds_poll_send_completions",
902 				    "EP(%p): WC ID: %p ERROR: %d", ep,
903 				    wc[ix].wc_id, wc[ix].wc_status);
904 				if (send_error == 0) {
905 					rds_session_t	*sp = ep->ep_sp;
906 
907 					/* don't let anyone send anymore */
908 					rw_enter(&sp->session_lock, RW_WRITER);
909 					if (sp->session_state !=
910 					    RDS_SESSION_STATE_ERROR) {
911 						sp->session_state =
912 						    RDS_SESSION_STATE_ERROR;
913 						/* Make this the active end */
914 						sp->session_type =
915 						    RDS_SESSION_ACTIVE;
916 					}
917 					rw_exit(&sp->session_lock);
918 				}
919 
920 				send_error++;
921 
922 				if (wc[ix].wc_id == RDS_RDMAW_WRID) {
923 					mutex_enter(&ep->ep_lock);
924 					ep->ep_rdmacnt--;
925 					mutex_exit(&ep->ep_lock);
926 					continue;
927 				}
928 
929 				bp = (rds_buf_t *)(uintptr_t)wc[ix].wc_id;
930 				bp->buf_state = RDS_SNDBUF_ERROR;
931 			}
932 
933 			bp->buf_nextp = NULL;
934 			if (headp) {
935 				tailp->buf_nextp = bp;
936 				tailp = bp;
937 			} else {
938 				headp = bp;
939 				tailp = bp;
940 			}
941 
942 			npolled++;
943 		}
944 
945 		if (rds_no_interrupts && (npolled > 100)) {
946 			break;
947 		}
948 
949 		if (rds_no_interrupts == 1) {
950 			break;
951 		}
952 	} while (ret != IBT_CQ_EMPTY);
953 
954 	RDS_DPRINTF5("rds_poll_send_completions", "Npolled: %d send_error: %d",
955 	    npolled, send_error);
956 
957 	/* put the buffers to the pool */
958 	if (npolled != 0) {
959 		rds_free_send_buf(ep, headp, tailp, npolled, lock);
960 	}
961 
962 	if (send_error != 0) {
963 		rds_handle_send_error(ep);
964 	}
965 
966 	RDS_DPRINTF4("rds_poll_send_completions", "Return EP(%p)", ep);
967 }
968 
969 void
970 rds_sendcq_handler(ibt_cq_hdl_t cq, void *arg)
971 {
972 	rds_ep_t	*ep;
973 	int		ret;
974 
975 	ep = (rds_ep_t *)arg;
976 
977 	RDS_DPRINTF4("rds_sendcq_handler", "Enter: EP(%p)", ep);
978 
979 	/* enable the CQ */
980 	ret = ibt_enable_cq_notify(cq, IBT_NEXT_COMPLETION);
981 	if (ret != IBT_SUCCESS) {
982 		RDS_DPRINTF2(LABEL, "EP(%p) CQ(%p): ibt_enable_cq_notify "
983 		    "failed: %d", ep, cq, ret);
984 		return;
985 	}
986 
987 	rds_poll_send_completions(cq, ep, B_FALSE);
988 
989 	RDS_DPRINTF4("rds_sendcq_handler", "Return: EP(%p)", ep);
990 }
991 
992 void
993 rds_ep_free_rc_channel(rds_ep_t *ep)
994 {
995 	int ret;
996 
997 	RDS_DPRINTF2("rds_ep_free_rc_channel", "EP(%p) - Enter", ep);
998 
999 	ASSERT(mutex_owned(&ep->ep_lock));
1000 
1001 	/* free the QP */
1002 	if (ep->ep_chanhdl != NULL) {
1003 		/* wait until the RQ is empty */
1004 		(void) ibt_flush_channel(ep->ep_chanhdl);
1005 		(void) rds_is_recvq_empty(ep, B_TRUE);
1006 		ret = ibt_free_channel(ep->ep_chanhdl);
1007 		if (ret != IBT_SUCCESS) {
1008 			RDS_DPRINTF1("rds_ep_free_rc_channel", "EP(%p) "
1009 			    "ibt_free_channel returned: %d", ep, ret);
1010 		}
1011 		ep->ep_chanhdl = NULL;
1012 	} else {
1013 		RDS_DPRINTF2("rds_ep_free_rc_channel",
1014 		    "EP(%p) Channel is ALREADY FREE", ep);
1015 	}
1016 
1017 	/* free the Send CQ */
1018 	if (ep->ep_sendcq != NULL) {
1019 		ret = ibt_free_cq(ep->ep_sendcq);
1020 		if (ret != IBT_SUCCESS) {
1021 			RDS_DPRINTF1("rds_ep_free_rc_channel",
1022 			    "EP(%p) - for sendcq, ibt_free_cq returned %d",
1023 			    ep, ret);
1024 		}
1025 		ep->ep_sendcq = NULL;
1026 	} else {
1027 		RDS_DPRINTF2("rds_ep_free_rc_channel",
1028 		    "EP(%p) SendCQ is ALREADY FREE", ep);
1029 	}
1030 
1031 	/* free the Recv CQ */
1032 	if (ep->ep_recvcq != NULL) {
1033 		ret = ibt_free_cq(ep->ep_recvcq);
1034 		if (ret != IBT_SUCCESS) {
1035 			RDS_DPRINTF1("rds_ep_free_rc_channel",
1036 			    "EP(%p) - for recvcq, ibt_free_cq returned %d",
1037 			    ep, ret);
1038 		}
1039 		ep->ep_recvcq = NULL;
1040 	} else {
1041 		RDS_DPRINTF2("rds_ep_free_rc_channel",
1042 		    "EP(%p) RecvCQ is ALREADY FREE", ep);
1043 	}
1044 
1045 	RDS_DPRINTF2("rds_ep_free_rc_channel", "EP(%p) - Return", ep);
1046 }
1047 
1048 /* Allocate resources for RC channel */
1049 ibt_channel_hdl_t
1050 rds_ep_alloc_rc_channel(rds_ep_t *ep, uint8_t hca_port)
1051 {
1052 	int				ret = IBT_SUCCESS;
1053 	ibt_cq_attr_t			scqattr, rcqattr;
1054 	ibt_rc_chan_alloc_args_t	chanargs;
1055 	ibt_channel_hdl_t		chanhdl;
1056 	rds_hca_t			*hcap;
1057 
1058 	RDS_DPRINTF4("rds_ep_alloc_rc_channel", "Enter: 0x%p port: %d",
1059 	    ep, hca_port);
1060 
1061 	/* get the hcap for the HCA hosting this channel */
1062 	hcap = rds_get_hcap(rdsib_statep, ep->ep_hca_guid);
1063 	if (hcap == NULL) {
1064 		RDS_DPRINTF2("rds_ep_alloc_rc_channel",
1065 		    "HCA (0x%llx) not found", ep->ep_hca_guid);
1066 		return (NULL);
1067 	}
1068 
1069 	/* reset taskqpending flag here */
1070 	ep->ep_recvqp.qp_taskqpending = B_FALSE;
1071 
1072 	if (ep->ep_type == RDS_EP_TYPE_CTRL) {
1073 		scqattr.cq_size = MaxCtrlSendBuffers;
1074 		scqattr.cq_sched = NULL;
1075 		scqattr.cq_flags = IBT_CQ_NO_FLAGS;
1076 
1077 		rcqattr.cq_size = MaxCtrlRecvBuffers;
1078 		rcqattr.cq_sched = NULL;
1079 		rcqattr.cq_flags = IBT_CQ_NO_FLAGS;
1080 
1081 		chanargs.rc_sizes.cs_sq = MaxCtrlSendBuffers;
1082 		chanargs.rc_sizes.cs_rq = MaxCtrlRecvBuffers;
1083 		chanargs.rc_sizes.cs_sq_sgl = 1;
1084 		chanargs.rc_sizes.cs_rq_sgl = 1;
1085 	} else {
1086 		scqattr.cq_size = MaxDataSendBuffers + RDS_NUM_ACKS;
1087 		scqattr.cq_sched = NULL;
1088 		scqattr.cq_flags = IBT_CQ_NO_FLAGS;
1089 
1090 		rcqattr.cq_size = MaxDataRecvBuffers;
1091 		rcqattr.cq_sched = NULL;
1092 		rcqattr.cq_flags = IBT_CQ_NO_FLAGS;
1093 
1094 		chanargs.rc_sizes.cs_sq = MaxDataSendBuffers + RDS_NUM_ACKS;
1095 		chanargs.rc_sizes.cs_rq = MaxDataRecvBuffers;
1096 		chanargs.rc_sizes.cs_sq_sgl = 1;
1097 		chanargs.rc_sizes.cs_rq_sgl = 1;
1098 	}
1099 
1100 	if (ep->ep_sendcq == NULL) {
1101 		/* returned size is always greater than the requested size */
1102 		ret = ibt_alloc_cq(hcap->hca_hdl, &scqattr,
1103 		    &ep->ep_sendcq, NULL);
1104 		if (ret != IBT_SUCCESS) {
1105 			RDS_DPRINTF2(LABEL, "ibt_alloc_cq for sendCQ "
1106 			    "failed, size = %d: %d", scqattr.cq_size, ret);
1107 			return (NULL);
1108 		}
1109 
1110 		(void) ibt_set_cq_handler(ep->ep_sendcq, rds_sendcq_handler,
1111 		    ep);
1112 
1113 		if (rds_no_interrupts == 0) {
1114 			ret = ibt_enable_cq_notify(ep->ep_sendcq,
1115 			    IBT_NEXT_COMPLETION);
1116 			if (ret != IBT_SUCCESS) {
1117 				RDS_DPRINTF2(LABEL,
1118 				    "ibt_enable_cq_notify failed: %d", ret);
1119 				(void) ibt_free_cq(ep->ep_sendcq);
1120 				ep->ep_sendcq = NULL;
1121 				return (NULL);
1122 			}
1123 		}
1124 	}
1125 
1126 	if (ep->ep_recvcq == NULL) {
1127 		/* returned size is always greater than the requested size */
1128 		ret = ibt_alloc_cq(hcap->hca_hdl, &rcqattr,
1129 		    &ep->ep_recvcq, NULL);
1130 		if (ret != IBT_SUCCESS) {
1131 			RDS_DPRINTF2(LABEL, "ibt_alloc_cq for recvCQ "
1132 			    "failed, size = %d: %d", rcqattr.cq_size, ret);
1133 			(void) ibt_free_cq(ep->ep_sendcq);
1134 			ep->ep_sendcq = NULL;
1135 			return (NULL);
1136 		}
1137 
1138 		(void) ibt_set_cq_handler(ep->ep_recvcq, rds_recvcq_handler,
1139 		    ep);
1140 
1141 		ret = ibt_enable_cq_notify(ep->ep_recvcq, rds_wc_signal);
1142 		if (ret != IBT_SUCCESS) {
1143 			RDS_DPRINTF2(LABEL,
1144 			    "ibt_enable_cq_notify failed: %d", ret);
1145 			(void) ibt_free_cq(ep->ep_recvcq);
1146 			ep->ep_recvcq = NULL;
1147 			(void) ibt_free_cq(ep->ep_sendcq);
1148 			ep->ep_sendcq = NULL;
1149 			return (NULL);
1150 		}
1151 	}
1152 
1153 	chanargs.rc_flags = IBT_ALL_SIGNALED;
1154 	chanargs.rc_control = IBT_CEP_RDMA_RD | IBT_CEP_RDMA_WR |
1155 	    IBT_CEP_ATOMIC;
1156 	chanargs.rc_hca_port_num = hca_port;
1157 	chanargs.rc_scq = ep->ep_sendcq;
1158 	chanargs.rc_rcq = ep->ep_recvcq;
1159 	chanargs.rc_pd = hcap->hca_pdhdl;
1160 	chanargs.rc_srq = NULL;
1161 
1162 	ret = ibt_alloc_rc_channel(hcap->hca_hdl,
1163 	    IBT_ACHAN_NO_FLAGS, &chanargs, &chanhdl, NULL);
1164 	if (ret != IBT_SUCCESS) {
1165 		RDS_DPRINTF2(LABEL, "ibt_alloc_rc_channel fail: %d",
1166 		    ret);
1167 		(void) ibt_free_cq(ep->ep_recvcq);
1168 		ep->ep_recvcq = NULL;
1169 		(void) ibt_free_cq(ep->ep_sendcq);
1170 		ep->ep_sendcq = NULL;
1171 		return (NULL);
1172 	}
1173 
1174 	/* Chan private should contain the ep */
1175 	(void) ibt_set_chan_private(chanhdl, ep);
1176 
1177 	RDS_DPRINTF4("rds_ep_alloc_rc_channel", "Return: 0x%p", chanhdl);
1178 
1179 	return (chanhdl);
1180 }
1181 
1182 
1183 #if 0
1184 
1185 /* Return node guid given a port gid */
1186 ib_guid_t
1187 rds_gid_to_node_guid(ib_gid_t gid)
1188 {
1189 	ibt_node_info_t	nodeinfo;
1190 	int		ret;
1191 
1192 	RDS_DPRINTF4("rds_gid_to_node_guid", "Enter: gid: %llx:%llx",
1193 	    gid.gid_prefix, gid.gid_guid);
1194 
1195 	ret = ibt_gid_to_node_info(gid, &nodeinfo);
1196 	if (ret != IBT_SUCCESS) {
1197 		RDS_DPRINTF2(LABEL, "ibt_gid_node_info for gid: %llx:%llx "
1198 		    "failed", gid.gid_prefix, gid.gid_guid);
1199 		return (0LL);
1200 	}
1201 
1202 	RDS_DPRINTF4("rds_gid_to_node_guid", "Return: Node guid: %llx",
1203 	    nodeinfo.n_node_guid);
1204 
1205 	return (nodeinfo.n_node_guid);
1206 }
1207 
1208 #endif
1209 
1210 static void
1211 rds_handle_portup_event(rds_state_t *statep, ibt_hca_hdl_t hdl,
1212     ibt_async_event_t *event)
1213 {
1214 	rds_hca_t		*hcap;
1215 	ibt_hca_portinfo_t	*newpinfop, *oldpinfop;
1216 	uint_t			newsize, oldsize, nport;
1217 	ib_gid_t		gid;
1218 	int			ret;
1219 
1220 	RDS_DPRINTF2("rds_handle_portup_event", "Enter: GUID: 0x%llx",
1221 	    event->ev_hca_guid);
1222 
1223 	hcap = rds_get_hcap(statep, event->ev_hca_guid);
1224 	ASSERT(hcap != NULL);
1225 
1226 	ret = ibt_query_hca_ports(hdl, 0, &newpinfop, &nport, &newsize);
1227 	if (ret != IBT_SUCCESS) {
1228 		RDS_DPRINTF2(LABEL, "ibt_query_hca_ports failed: %d", ret);
1229 		return;
1230 	}
1231 
1232 	oldpinfop = hcap->hca_pinfop;
1233 	oldsize = hcap->hca_pinfo_sz;
1234 	hcap->hca_pinfop = newpinfop;
1235 	hcap->hca_pinfo_sz = newsize;
1236 
1237 	/* structure copy */
1238 	gid = newpinfop[event->ev_port - 1].p_sgid_tbl[0];
1239 
1240 	/* bind RDS service on the port, pass statep as cm_private */
1241 	ret = ibt_bind_service(statep->rds_srvhdl, gid, NULL, statep, NULL);
1242 	if (ret != IBT_SUCCESS) {
1243 		RDS_DPRINTF2(LABEL, "Bind service for HCA: 0x%llx Port: %d "
1244 		    "gid %llx:%llx returned: %d", event->ev_hca_guid,
1245 		    event->ev_port, gid.gid_prefix, gid.gid_guid, ret);
1246 	}
1247 
1248 	(void) ibt_free_portinfo(oldpinfop, oldsize);
1249 
1250 	RDS_DPRINTF2("rds_handle_portup_event", "Return: GUID: 0x%llx",
1251 	    event->ev_hca_guid);
1252 }
1253 
1254 static void
1255 rds_async_handler(void *clntp, ibt_hca_hdl_t hdl, ibt_async_code_t code,
1256     ibt_async_event_t *event)
1257 {
1258 	rds_state_t		*statep;
1259 
1260 	RDS_DPRINTF2("rds_async_handler", "Async code: %d", code);
1261 
1262 	switch (code) {
1263 	case IBT_EVENT_PORT_UP:
1264 		statep = (rds_state_t *)clntp;
1265 		rds_handle_portup_event(statep, hdl, event);
1266 		break;
1267 
1268 	default:
1269 		RDS_DPRINTF2(LABEL, "Async event: %d not handled", code);
1270 	}
1271 
1272 	RDS_DPRINTF2("rds_async_handler", "Return: code: %d", code);
1273 }
1274