xref: /illumos-gate/usr/src/uts/common/io/ib/clients/ibd/ibd.c (revision 753a6d45)
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 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * An implementation of the IPoIB standard based on PSARC 2001/289.
29  */
30 
31 #include <sys/types.h>
32 #include <sys/conf.h>
33 #include <sys/ddi.h>
34 #include <sys/sunddi.h>
35 #include <sys/modctl.h>
36 #include <sys/stropts.h>
37 #include <sys/stream.h>
38 #include <sys/strsun.h>
39 #include <sys/strsubr.h>
40 #include <sys/dlpi.h>
41 #include <sys/mac_provider.h>
42 
43 #include <sys/pattr.h>		/* for HCK_FULLCKSUM */
44 #include <sys/sysmacros.h>	/* for offsetof */
45 #include <sys/disp.h>		/* for async thread pri */
46 #include <sys/atomic.h>		/* for atomic_add*() */
47 #include <sys/ethernet.h>	/* for ETHERTYPE_IP */
48 #include <netinet/in.h>		/* for netinet/ip.h below */
49 #include <netinet/ip.h>		/* for struct ip */
50 #include <netinet/udp.h>	/* for struct udphdr */
51 #include <inet/common.h>	/* for inet/ip.h below */
52 #include <inet/ip.h>		/* for ipha_t */
53 #include <inet/ip_if.h>		/* for IP6_DL_SAP */
54 #include <inet/ip6.h>		/* for ip6_t */
55 #include <inet/tcp.h>		/* for tcph_t */
56 #include <netinet/icmp6.h>	/* for icmp6_t */
57 #include <sys/callb.h>
58 #include <sys/modhash.h>
59 
60 #include <sys/ib/clients/ibd/ibd.h>
61 #include <sys/ib/mgt/sm_attr.h>	/* for SM_INIT_TYPE_* */
62 #include <sys/note.h>
63 #include <sys/multidata.h>
64 
65 #include <sys/ib/mgt/ibmf/ibmf.h>	/* for ibd_get_portspeed */
66 
67 /*
68  * Per-interface tunables
69  *
70  * ibd_tx_copy_thresh
71  *     This sets the threshold at which ibd will attempt to do a bcopy of the
72  *     outgoing data into a pre-mapped buffer. The IPoIB driver's send behavior
73  *     is restricted by various parameters, so setting of this value must be
74  *     made after careful considerations only.  For instance, IB HCAs currently
75  *     impose a relatively small limit (when compared to ethernet NICs) on the
76  *     length of the SGL for transmit. On the other hand, the ip stack could
77  *     send down mp chains that are quite long when LSO is enabled.
78  *
79  * ibd_num_swqe
80  *     Number of "send WQE" elements that will be allocated and used by ibd.
81  *     When tuning this parameter, the size of pre-allocated, pre-mapped copy
82  *     buffer in each of these send wqes must be taken into account. This
83  *     copy buffer size is determined by the value of IBD_TX_BUF_SZ (this is
84  *     currently set to the same value of ibd_tx_copy_thresh, but may be
85  *     changed independently if needed).
86  *
87  * ibd_num_rwqe
88  *     Number of "receive WQE" elements that will be allocated and used by
89  *     ibd. This parameter is limited by the maximum channel size of the HCA.
90  *     Each buffer in the receive wqe will be of MTU size.
91  *
92  * ibd_num_lso_bufs
93  *     Number of "larger-than-MTU" copy buffers to use for cases when the
94  *     outgoing mblk chain is too fragmented to be used with ibt_map_mem_iov()
95  *     and too large to be used with regular MTU-sized copy buffers. It is
96  *     not recommended to tune this variable without understanding the
97  *     application environment and/or memory resources. The size of each of
98  *     these lso buffers is determined by the value of IBD_LSO_BUFSZ.
99  *
100  * ibd_num_ah
101  *     Number of AH cache entries to allocate
102  *
103  * ibd_hash_size
104  *     Hash table size for the active AH list
105  *
106  * ibd_separate_cqs
107  * ibd_txcomp_poll
108  *     These boolean variables (1 or 0) may be used to tune the behavior of
109  *     ibd in managing the send and receive completion queues and in deciding
110  *     whether or not transmit completions should be polled or interrupt
111  *     driven (when the completion queues are separate). If both the completion
112  *     queues are interrupt driven, it may not be possible for the handlers to
113  *     be invoked concurrently, depending on how the interrupts are tied on
114  *     the PCI intr line.  Note that some combination of these two parameters
115  *     may not be meaningful (and therefore not allowed).
116  *
117  * ibd_tx_softintr
118  * ibd_rx_softintr
119  *     The softintr mechanism allows ibd to avoid event queue overflows if
120  *     the receive/completion handlers are to be expensive. These are enabled
121  *     by default.
122  *
123  * ibd_log_sz
124  *     This specifies the size of the ibd log buffer in bytes. The buffer is
125  *     allocated and logging is enabled only when IBD_LOGGING is defined.
126  *
127  */
128 uint_t ibd_tx_copy_thresh = 0x1000;
129 uint_t ibd_num_swqe = 4000;
130 uint_t ibd_num_rwqe = 4000;
131 uint_t ibd_num_lso_bufs = 0x400;
132 uint_t ibd_num_ah = 64;
133 uint_t ibd_hash_size = 32;
134 uint_t ibd_separate_cqs = 1;
135 uint_t ibd_txcomp_poll = 0;
136 uint_t ibd_rx_softintr = 1;
137 uint_t ibd_tx_softintr = 1;
138 #ifdef IBD_LOGGING
139 uint_t ibd_log_sz = 0x20000;
140 #endif
141 
142 #define	IBD_TX_COPY_THRESH		ibd_tx_copy_thresh
143 #define	IBD_TX_BUF_SZ			ibd_tx_copy_thresh
144 #define	IBD_NUM_SWQE			ibd_num_swqe
145 #define	IBD_NUM_RWQE			ibd_num_rwqe
146 #define	IBD_NUM_LSO_BUFS		ibd_num_lso_bufs
147 #define	IBD_NUM_AH			ibd_num_ah
148 #define	IBD_HASH_SIZE			ibd_hash_size
149 #ifdef IBD_LOGGING
150 #define	IBD_LOG_SZ			ibd_log_sz
151 #endif
152 
153 /*
154  * Receive CQ moderation parameters: NOT tunables
155  */
156 static uint_t ibd_rxcomp_count = 4;
157 static uint_t ibd_rxcomp_usec = 10;
158 
159 /*
160  * Thresholds
161  *
162  * When waiting for resources (swqes or lso buffers) to become available,
163  * the first two thresholds below determine how long to wait before informing
164  * the network layer to start sending packets again. The IBD_TX_POLL_THRESH
165  * determines how low the available swqes should go before we start polling
166  * the completion queue.
167  */
168 #define	IBD_FREE_LSOS_THRESH		8
169 #define	IBD_FREE_SWQES_THRESH		20
170 #define	IBD_TX_POLL_THRESH		80
171 
172 /*
173  * When doing multiple-send-wr or multiple-recv-wr posts, this value
174  * determines how many to do at a time (in a single ibt_post_send/recv).
175  */
176 #define	IBD_MAX_POST_MULTIPLE		4
177 
178 /*
179  * Maximum length for returning chained mps back to crossbow
180  */
181 #define	IBD_MAX_RX_MP_LEN		16
182 
183 /*
184  * LSO parameters
185  */
186 #define	IBD_LSO_MAXLEN			65536
187 #define	IBD_LSO_BUFSZ			8192
188 #define	IBD_PROP_LSO_POLICY		"lso-policy"
189 
190 /*
191  * Completion queue polling control
192  */
193 #define	IBD_RX_CQ_POLLING		0x1
194 #define	IBD_TX_CQ_POLLING		0x2
195 #define	IBD_REDO_RX_CQ_POLLING		0x4
196 #define	IBD_REDO_TX_CQ_POLLING		0x8
197 
198 /*
199  * Flag bits for resources to reap
200  */
201 #define	IBD_RSRC_SWQE			0x1
202 #define	IBD_RSRC_LSOBUF			0x2
203 
204 /*
205  * Async operation types
206  */
207 #define	IBD_ASYNC_GETAH			1
208 #define	IBD_ASYNC_JOIN			2
209 #define	IBD_ASYNC_LEAVE			3
210 #define	IBD_ASYNC_PROMON		4
211 #define	IBD_ASYNC_PROMOFF		5
212 #define	IBD_ASYNC_REAP			6
213 #define	IBD_ASYNC_TRAP			7
214 #define	IBD_ASYNC_SCHED			8
215 #define	IBD_ASYNC_LINK			9
216 #define	IBD_ASYNC_EXIT			10
217 
218 /*
219  * Async operation states
220  */
221 #define	IBD_OP_NOTSTARTED		0
222 #define	IBD_OP_ONGOING			1
223 #define	IBD_OP_COMPLETED		2
224 #define	IBD_OP_ERRORED			3
225 #define	IBD_OP_ROUTERED			4
226 
227 /*
228  * Miscellaneous constants
229  */
230 #define	IBD_SEND			0
231 #define	IBD_RECV			1
232 #define	IB_MGID_IPV4_LOWGRP_MASK	0xFFFFFFFF
233 #ifdef IBD_LOGGING
234 #define	IBD_DMAX_LINE			100
235 #endif
236 
237 /*
238  * Enumerations for link states
239  */
240 typedef enum {
241 	IBD_LINK_DOWN,
242 	IBD_LINK_UP,
243 	IBD_LINK_UP_ABSENT
244 } ibd_link_op_t;
245 
246 /*
247  * Driver State Pointer
248  */
249 void *ibd_list;
250 
251 /*
252  * Logging
253  */
254 #ifdef IBD_LOGGING
255 kmutex_t ibd_lbuf_lock;
256 uint8_t *ibd_lbuf;
257 uint32_t ibd_lbuf_ndx;
258 #endif
259 
260 /*
261  * Required system entry points
262  */
263 static int ibd_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
264 static int ibd_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
265 
266 /*
267  * Required driver entry points for GLDv3
268  */
269 static int ibd_m_stat(void *, uint_t, uint64_t *);
270 static int ibd_m_start(void *);
271 static void ibd_m_stop(void *);
272 static int ibd_m_promisc(void *, boolean_t);
273 static int ibd_m_multicst(void *, boolean_t, const uint8_t *);
274 static int ibd_m_unicst(void *, const uint8_t *);
275 static mblk_t *ibd_m_tx(void *, mblk_t *);
276 static boolean_t ibd_m_getcapab(void *, mac_capab_t, void *);
277 
278 /*
279  * Private driver entry points for GLDv3
280  */
281 
282 /*
283  * Initialization
284  */
285 static int ibd_state_init(ibd_state_t *, dev_info_t *);
286 static int ibd_drv_init(ibd_state_t *);
287 static int ibd_init_txlist(ibd_state_t *);
288 static int ibd_init_rxlist(ibd_state_t *);
289 static int ibd_acache_init(ibd_state_t *);
290 #ifdef IBD_LOGGING
291 static void ibd_log_init(void);
292 #endif
293 
294 /*
295  * Termination/cleanup
296  */
297 static void ibd_state_fini(ibd_state_t *);
298 static void ibd_drv_fini(ibd_state_t *);
299 static void ibd_fini_txlist(ibd_state_t *);
300 static void ibd_fini_rxlist(ibd_state_t *);
301 static void ibd_tx_cleanup(ibd_state_t *, ibd_swqe_t *);
302 static void ibd_acache_fini(ibd_state_t *);
303 #ifdef IBD_LOGGING
304 static void ibd_log_fini(void);
305 #endif
306 
307 /*
308  * Allocation/acquire/map routines
309  */
310 static int ibd_alloc_swqe(ibd_state_t *, ibd_swqe_t **, int, ibt_lkey_t);
311 static int ibd_alloc_rwqe(ibd_state_t *, ibd_rwqe_t **);
312 static int ibd_alloc_tx_copybufs(ibd_state_t *);
313 static int ibd_alloc_tx_lsobufs(ibd_state_t *);
314 static int ibd_acquire_swqe(ibd_state_t *, ibd_swqe_t **);
315 static int ibd_acquire_lsobufs(ibd_state_t *, uint_t, ibt_wr_ds_t *,
316     uint32_t *);
317 
318 /*
319  * Free/release/unmap routines
320  */
321 static void ibd_free_swqe(ibd_state_t *, ibd_swqe_t *);
322 static void ibd_free_rwqe(ibd_state_t *, ibd_rwqe_t *);
323 static void ibd_delete_rwqe(ibd_state_t *, ibd_rwqe_t *);
324 static void ibd_free_tx_copybufs(ibd_state_t *);
325 static void ibd_free_tx_lsobufs(ibd_state_t *);
326 static void ibd_release_swqe(ibd_state_t *, ibd_swqe_t *);
327 static void ibd_release_lsobufs(ibd_state_t *, ibt_wr_ds_t *, uint32_t);
328 static void ibd_free_lsohdr(ibd_swqe_t *, mblk_t *);
329 static void ibd_unmap_mem(ibd_state_t *, ibd_swqe_t *);
330 
331 /*
332  * Handlers/callback routines
333  */
334 static uint_t ibd_intr(char *);
335 static uint_t ibd_tx_recycle(char *);
336 static void ibd_rcq_handler(ibt_cq_hdl_t, void *);
337 static void ibd_scq_handler(ibt_cq_hdl_t, void *);
338 static void ibd_poll_compq(ibd_state_t *, ibt_cq_hdl_t);
339 static uint_t ibd_drain_cq(ibd_state_t *, ibt_cq_hdl_t, ibt_wc_t *, uint_t);
340 static void ibd_freemsg_cb(char *);
341 static void ibd_async_handler(void *, ibt_hca_hdl_t, ibt_async_code_t,
342     ibt_async_event_t *);
343 static void ibd_snet_notices_handler(void *, ib_gid_t,
344     ibt_subnet_event_code_t, ibt_subnet_event_t *);
345 
346 /*
347  * Send/receive routines
348  */
349 static boolean_t ibd_send(ibd_state_t *, mblk_t *);
350 static void ibd_post_send(ibd_state_t *, ibd_swqe_t *);
351 static int ibd_post_rwqe(ibd_state_t *, ibd_rwqe_t *, boolean_t);
352 static void ibd_process_rx(ibd_state_t *, ibd_rwqe_t *, ibt_wc_t *);
353 static void ibd_flush_rx(ibd_state_t *, mblk_t *);
354 
355 /*
356  * Threads
357  */
358 static void ibd_async_work(ibd_state_t *);
359 
360 /*
361  * Async tasks
362  */
363 static void ibd_async_acache(ibd_state_t *, ipoib_mac_t *);
364 static void ibd_async_multicast(ibd_state_t *, ib_gid_t, int);
365 static void ibd_async_setprom(ibd_state_t *);
366 static void ibd_async_unsetprom(ibd_state_t *);
367 static void ibd_async_reap_group(ibd_state_t *, ibd_mce_t *, ib_gid_t, uint8_t);
368 static void ibd_async_trap(ibd_state_t *, ibd_req_t *);
369 static void ibd_async_txsched(ibd_state_t *);
370 static void ibd_async_link(ibd_state_t *, ibd_req_t *);
371 
372 /*
373  * Async task helpers
374  */
375 static ibd_mce_t *ibd_async_mcache(ibd_state_t *, ipoib_mac_t *, boolean_t *);
376 static ibd_mce_t *ibd_join_group(ibd_state_t *, ib_gid_t, uint8_t);
377 static ibd_mce_t *ibd_mcache_find(ib_gid_t, struct list *);
378 static boolean_t ibd_get_allroutergroup(ibd_state_t *,
379     ipoib_mac_t *, ipoib_mac_t *);
380 static void ibd_leave_group(ibd_state_t *, ib_gid_t, uint8_t);
381 static void ibd_reacquire_group(ibd_state_t *, ibd_mce_t *);
382 static ibt_status_t ibd_iba_join(ibd_state_t *, ib_gid_t, ibd_mce_t *);
383 static ibt_status_t ibd_find_bgroup(ibd_state_t *);
384 static void ibd_n2h_gid(ipoib_mac_t *, ib_gid_t *);
385 static void ibd_h2n_mac(ipoib_mac_t *, ib_qpn_t, ib_sn_prefix_t, ib_guid_t);
386 static uint64_t ibd_get_portspeed(ibd_state_t *);
387 static int ibd_get_portpkey(ibd_state_t *, ib_guid_t *);
388 static boolean_t ibd_async_safe(ibd_state_t *);
389 static void ibd_async_done(ibd_state_t *);
390 static ibd_ace_t *ibd_acache_find(ibd_state_t *, ipoib_mac_t *, boolean_t, int);
391 static ibd_ace_t *ibd_acache_lookup(ibd_state_t *, ipoib_mac_t *, int *, int);
392 static ibd_ace_t *ibd_acache_get_unref(ibd_state_t *);
393 static boolean_t ibd_acache_recycle(ibd_state_t *, ipoib_mac_t *, boolean_t);
394 static void ibd_link_mod(ibd_state_t *, ibt_async_code_t);
395 
396 /*
397  * Miscellaneous helpers
398  */
399 static int ibd_sched_poll(ibd_state_t *, int, int);
400 static void ibd_queue_work_slot(ibd_state_t *, ibd_req_t *, int);
401 static int ibd_resume_transmission(ibd_state_t *);
402 static int ibd_setup_lso(ibd_swqe_t *, mblk_t *, uint32_t, ibt_ud_dest_hdl_t);
403 static int ibd_prepare_sgl(ibd_state_t *, mblk_t *, ibd_swqe_t *, uint_t);
404 static void *list_get_head(list_t *);
405 static int ibd_hash_key_cmp(mod_hash_key_t, mod_hash_key_t);
406 static uint_t ibd_hash_by_id(void *, mod_hash_key_t);
407 static void ibd_print_warn(ibd_state_t *, char *, ...);
408 #ifdef IBD_LOGGING
409 static void ibd_log(const char *, ...);
410 #endif
411 
412 DDI_DEFINE_STREAM_OPS(ibd_dev_ops, nulldev, nulldev, ibd_attach, ibd_detach,
413     nodev, NULL, D_MP, NULL, ddi_quiesce_not_needed);
414 
415 /* Module Driver Info */
416 static struct modldrv ibd_modldrv = {
417 	&mod_driverops,			/* This one is a driver */
418 	"InfiniBand GLDv3 Driver",	/* short description */
419 	&ibd_dev_ops			/* driver specific ops */
420 };
421 
422 /* Module Linkage */
423 static struct modlinkage ibd_modlinkage = {
424 	MODREV_1, (void *)&ibd_modldrv, NULL
425 };
426 
427 /*
428  * Module (static) info passed to IBTL during ibt_attach
429  */
430 static struct ibt_clnt_modinfo_s ibd_clnt_modinfo = {
431 	IBTI_V_CURR,
432 	IBT_NETWORK,
433 	ibd_async_handler,
434 	NULL,
435 	"IPIB"
436 };
437 
438 /*
439  * GLDv3 entry points
440  */
441 #define	IBD_M_CALLBACK_FLAGS	(MC_GETCAPAB)
442 static mac_callbacks_t ib_m_callbacks = {
443 	IBD_M_CALLBACK_FLAGS,
444 	ibd_m_stat,
445 	ibd_m_start,
446 	ibd_m_stop,
447 	ibd_m_promisc,
448 	ibd_m_multicst,
449 	ibd_m_unicst,
450 	ibd_m_tx,
451 	NULL,
452 	ibd_m_getcapab
453 };
454 
455 /*
456  * Fill/clear <scope> and <p_key> in multicast/broadcast address
457  */
458 #define	IBD_FILL_SCOPE_PKEY(maddr, scope, pkey)		\
459 {							\
460 	*(uint32_t *)((char *)(maddr) + 4) |=		\
461 	    htonl((uint32_t)(scope) << 16);		\
462 	*(uint32_t *)((char *)(maddr) + 8) |=		\
463 	    htonl((uint32_t)(pkey) << 16);		\
464 }
465 
466 #define	IBD_CLEAR_SCOPE_PKEY(maddr)			\
467 {							\
468 	*(uint32_t *)((char *)(maddr) + 4) &=		\
469 	    htonl(~((uint32_t)0xF << 16));		\
470 	*(uint32_t *)((char *)(maddr) + 8) &=		\
471 	    htonl(~((uint32_t)0xFFFF << 16));		\
472 }
473 
474 /*
475  * Rudimentary debugging support
476  */
477 #ifdef DEBUG
478 int ibd_debuglevel = 100;
479 static void
480 debug_print(int l, char *fmt, ...)
481 {
482 	va_list ap;
483 
484 	if (l < ibd_debuglevel)
485 		return;
486 	va_start(ap, fmt);
487 	vcmn_err(CE_CONT, fmt, ap);
488 	va_end(ap);
489 }
490 #define	DPRINT		debug_print
491 #else
492 #define	DPRINT
493 #endif
494 
495 /*
496  * Common routine to print warning messages; adds in hca guid, port number
497  * and pkey to be able to identify the IBA interface.
498  */
499 static void
500 ibd_print_warn(ibd_state_t *state, char *fmt, ...)
501 {
502 	ib_guid_t hca_guid;
503 	char ibd_print_buf[256];
504 	int len;
505 	va_list ap;
506 
507 	hca_guid = ddi_prop_get_int64(DDI_DEV_T_ANY, state->id_dip,
508 	    0, "hca-guid", 0);
509 	len = snprintf(ibd_print_buf, sizeof (ibd_print_buf),
510 	    "%s%d: HCA GUID %016llx port %d PKEY %02x ",
511 	    ddi_driver_name(state->id_dip), ddi_get_instance(state->id_dip),
512 	    (u_longlong_t)hca_guid, state->id_port, state->id_pkey);
513 	va_start(ap, fmt);
514 	(void) vsnprintf(ibd_print_buf + len, sizeof (ibd_print_buf) - len,
515 	    fmt, ap);
516 	cmn_err(CE_NOTE, "!%s", ibd_print_buf);
517 	va_end(ap);
518 }
519 
520 /*
521  * Warlock directives
522  */
523 
524 /*
525  * id_lso_lock
526  *
527  * state->id_lso->bkt_nfree may be accessed without a lock to
528  * determine the threshold at which we have to ask the nw layer
529  * to resume transmission (see ibd_resume_transmission()).
530  */
531 _NOTE(MUTEX_PROTECTS_DATA(ibd_state_t::id_lso_lock,
532     ibd_state_t::id_lso))
533 _NOTE(DATA_READABLE_WITHOUT_LOCK(ibd_state_t::id_lso))
534 _NOTE(DATA_READABLE_WITHOUT_LOCK(ibd_lsobkt_t::bkt_nfree))
535 
536 /*
537  * id_cq_poll_lock
538  */
539 _NOTE(MUTEX_PROTECTS_DATA(ibd_state_t::id_cq_poll_lock,
540     ibd_state_t::id_cq_poll_busy))
541 
542 /*
543  * id_txpost_lock
544  */
545 _NOTE(MUTEX_PROTECTS_DATA(ibd_state_t::id_txpost_lock,
546     ibd_state_t::id_tx_head))
547 _NOTE(MUTEX_PROTECTS_DATA(ibd_state_t::id_txpost_lock,
548     ibd_state_t::id_tx_busy))
549 _NOTE(MUTEX_PROTECTS_DATA(ibd_state_t::id_txpost_lock,
550     ibd_state_t::id_tx_tailp))
551 
552 /*
553  * id_rxpost_lock
554  */
555 _NOTE(MUTEX_PROTECTS_DATA(ibd_state_t::id_rxpost_lock,
556     ibd_state_t::id_rx_head))
557 _NOTE(MUTEX_PROTECTS_DATA(ibd_state_t::id_rxpost_lock,
558     ibd_state_t::id_rx_busy))
559 _NOTE(MUTEX_PROTECTS_DATA(ibd_state_t::id_rxpost_lock,
560     ibd_state_t::id_rx_tailp))
561 
562 /*
563  * id_acache_req_lock
564  */
565 _NOTE(MUTEX_PROTECTS_DATA(ibd_state_t::id_acache_req_lock,
566     ibd_state_t::id_acache_req_cv))
567 _NOTE(MUTEX_PROTECTS_DATA(ibd_state_t::id_acache_req_lock,
568     ibd_state_t::id_req_list))
569 
570 /*
571  * id_ac_mutex
572  *
573  * This mutex is actually supposed to protect id_ah_op as well,
574  * but this path of the code isn't clean (see update of id_ah_op
575  * in ibd_async_acache(), immediately after the call to
576  * ibd_async_mcache()). For now, we'll skip this check by
577  * declaring that id_ah_op is protected by some internal scheme
578  * that warlock isn't aware of.
579  */
580 _NOTE(MUTEX_PROTECTS_DATA(ibd_state_t::id_ac_mutex,
581     ibd_state_t::id_ah_active))
582 _NOTE(MUTEX_PROTECTS_DATA(ibd_state_t::id_ac_mutex,
583     ibd_state_t::id_ah_free))
584 _NOTE(MUTEX_PROTECTS_DATA(ibd_state_t::id_ac_mutex,
585     ibd_state_t::id_ah_addr))
586 _NOTE(SCHEME_PROTECTS_DATA("ac mutex should protect this",
587     ibd_state_t::id_ah_op))
588 _NOTE(MUTEX_PROTECTS_DATA(ibd_state_t::id_ac_mutex,
589     ibd_state_t::id_ah_error))
590 _NOTE(DATA_READABLE_WITHOUT_LOCK(ibd_state_t::id_ah_error))
591 
592 /*
593  * id_mc_mutex
594  */
595 _NOTE(MUTEX_PROTECTS_DATA(ibd_state_t::id_mc_mutex,
596     ibd_state_t::id_mc_full))
597 _NOTE(MUTEX_PROTECTS_DATA(ibd_state_t::id_mc_mutex,
598     ibd_state_t::id_mc_non))
599 
600 /*
601  * id_trap_lock
602  */
603 _NOTE(MUTEX_PROTECTS_DATA(ibd_state_t::id_trap_lock,
604     ibd_state_t::id_trap_cv))
605 _NOTE(MUTEX_PROTECTS_DATA(ibd_state_t::id_trap_lock,
606     ibd_state_t::id_trap_stop))
607 _NOTE(MUTEX_PROTECTS_DATA(ibd_state_t::id_trap_lock,
608     ibd_state_t::id_trap_inprog))
609 
610 /*
611  * id_prom_op
612  */
613 _NOTE(SCHEME_PROTECTS_DATA("only by async thread",
614     ibd_state_t::id_prom_op))
615 
616 /*
617  * id_sched_lock
618  */
619 _NOTE(MUTEX_PROTECTS_DATA(ibd_state_t::id_sched_lock,
620     ibd_state_t::id_sched_needed))
621 
622 /*
623  * id_link_mutex
624  */
625 _NOTE(MUTEX_PROTECTS_DATA(ibd_state_t::id_link_mutex,
626     ibd_state_t::id_link_state))
627 _NOTE(DATA_READABLE_WITHOUT_LOCK(ibd_state_t::id_link_state))
628 _NOTE(SCHEME_PROTECTS_DATA("only async thr and drv init",
629     ibd_state_t::id_link_speed))
630 
631 /*
632  * id_tx_list.dl_mutex
633  */
634 _NOTE(MUTEX_PROTECTS_DATA(ibd_state_t::id_tx_list.dl_mutex,
635     ibd_state_t::id_tx_list.dl_head))
636 _NOTE(MUTEX_PROTECTS_DATA(ibd_state_t::id_tx_list.dl_mutex,
637     ibd_state_t::id_tx_list.dl_tail))
638 _NOTE(SCHEME_PROTECTS_DATA("atomic or dl mutex or single thr",
639     ibd_state_t::id_tx_list.dl_pending_sends))
640 _NOTE(SCHEME_PROTECTS_DATA("atomic or dl mutex or single thr",
641     ibd_state_t::id_tx_list.dl_cnt))
642 
643 /*
644  * id_rx_list.dl_mutex
645  */
646 _NOTE(MUTEX_PROTECTS_DATA(ibd_state_t::id_rx_list.dl_mutex,
647     ibd_state_t::id_rx_list.dl_head))
648 _NOTE(MUTEX_PROTECTS_DATA(ibd_state_t::id_rx_list.dl_mutex,
649     ibd_state_t::id_rx_list.dl_tail))
650 _NOTE(SCHEME_PROTECTS_DATA("atomic or dl mutex or single thr",
651     ibd_state_t::id_rx_list.dl_bufs_outstanding))
652 _NOTE(SCHEME_PROTECTS_DATA("atomic or dl mutex or single thr",
653     ibd_state_t::id_rx_list.dl_cnt))
654 
655 
656 /*
657  * Items protected by atomic updates
658  */
659 _NOTE(SCHEME_PROTECTS_DATA("atomic update only",
660     ibd_state_s::id_brd_rcv
661     ibd_state_s::id_brd_xmt
662     ibd_state_s::id_multi_rcv
663     ibd_state_s::id_multi_xmt
664     ibd_state_s::id_num_intrs
665     ibd_state_s::id_rcv_bytes
666     ibd_state_s::id_rcv_pkt
667     ibd_state_s::id_tx_short
668     ibd_state_s::id_xmt_bytes
669     ibd_state_s::id_xmt_pkt))
670 
671 /*
672  * Non-mutex protection schemes for data elements. Almost all of
673  * these are non-shared items.
674  */
675 _NOTE(SCHEME_PROTECTS_DATA("unshared or single-threaded",
676     callb_cpr
677     ib_gid_s
678     ib_header_info
679     ibd_acache_rq
680     ibd_acache_s::ac_mce
681     ibd_mcache::mc_fullreap
682     ibd_mcache::mc_jstate
683     ibd_mcache::mc_req
684     ibd_rwqe_s
685     ibd_swqe_s
686     ibd_wqe_s
687     ibt_wr_ds_s::ds_va
688     ibt_wr_lso_s
689     ipoib_mac::ipoib_qpn
690     mac_capab_lso_s
691     msgb::b_next
692     msgb::b_rptr
693     msgb::b_wptr))
694 
695 int
696 _init()
697 {
698 	int status;
699 
700 	/*
701 	 * Sanity check some parameter settings. Tx completion polling
702 	 * only makes sense with separate CQs for Tx and Rx.
703 	 */
704 	if ((ibd_txcomp_poll == 1) && (ibd_separate_cqs == 0)) {
705 		cmn_err(CE_NOTE, "!ibd: %s",
706 		    "Setting ibd_txcomp_poll = 0 for combined CQ");
707 		ibd_txcomp_poll = 0;
708 	}
709 
710 	status = ddi_soft_state_init(&ibd_list, sizeof (ibd_state_t), 0);
711 	if (status != 0) {
712 		DPRINT(10, "_init:failed in ddi_soft_state_init()");
713 		return (status);
714 	}
715 
716 	mac_init_ops(&ibd_dev_ops, "ibd");
717 	status = mod_install(&ibd_modlinkage);
718 	if (status != 0) {
719 		DPRINT(10, "_init:failed in mod_install()");
720 		ddi_soft_state_fini(&ibd_list);
721 		mac_fini_ops(&ibd_dev_ops);
722 		return (status);
723 	}
724 
725 #ifdef IBD_LOGGING
726 	ibd_log_init();
727 #endif
728 	return (0);
729 }
730 
731 int
732 _info(struct modinfo *modinfop)
733 {
734 	return (mod_info(&ibd_modlinkage, modinfop));
735 }
736 
737 int
738 _fini()
739 {
740 	int status;
741 
742 	status = mod_remove(&ibd_modlinkage);
743 	if (status != 0)
744 		return (status);
745 
746 	mac_fini_ops(&ibd_dev_ops);
747 	ddi_soft_state_fini(&ibd_list);
748 #ifdef IBD_LOGGING
749 	ibd_log_fini();
750 #endif
751 	return (0);
752 }
753 
754 /*
755  * Convert the GID part of the mac address from network byte order
756  * to host order.
757  */
758 static void
759 ibd_n2h_gid(ipoib_mac_t *mac, ib_gid_t *dgid)
760 {
761 	ib_sn_prefix_t nbopref;
762 	ib_guid_t nboguid;
763 
764 	bcopy(mac->ipoib_gidpref, &nbopref, sizeof (ib_sn_prefix_t));
765 	bcopy(mac->ipoib_gidsuff, &nboguid, sizeof (ib_guid_t));
766 	dgid->gid_prefix = b2h64(nbopref);
767 	dgid->gid_guid = b2h64(nboguid);
768 }
769 
770 /*
771  * Create the IPoIB address in network byte order from host order inputs.
772  */
773 static void
774 ibd_h2n_mac(ipoib_mac_t *mac, ib_qpn_t qpn, ib_sn_prefix_t prefix,
775     ib_guid_t guid)
776 {
777 	ib_sn_prefix_t nbopref;
778 	ib_guid_t nboguid;
779 
780 	mac->ipoib_qpn = htonl(qpn);
781 	nbopref = h2b64(prefix);
782 	nboguid = h2b64(guid);
783 	bcopy(&nbopref, mac->ipoib_gidpref, sizeof (ib_sn_prefix_t));
784 	bcopy(&nboguid, mac->ipoib_gidsuff, sizeof (ib_guid_t));
785 }
786 
787 /*
788  * Send to the appropriate all-routers group when the IBA multicast group
789  * does not exist, based on whether the target group is v4 or v6.
790  */
791 static boolean_t
792 ibd_get_allroutergroup(ibd_state_t *state, ipoib_mac_t *mcmac,
793     ipoib_mac_t *rmac)
794 {
795 	boolean_t retval = B_TRUE;
796 	uint32_t adjscope = state->id_scope << 16;
797 	uint32_t topword;
798 
799 	/*
800 	 * Copy the first 4 bytes in without assuming any alignment of
801 	 * input mac address; this will have IPoIB signature, flags and
802 	 * scope bits.
803 	 */
804 	bcopy(mcmac->ipoib_gidpref, &topword, sizeof (uint32_t));
805 	topword = ntohl(topword);
806 
807 	/*
808 	 * Generate proper address for IPv4/v6, adding in the Pkey properly.
809 	 */
810 	if ((topword == (IB_MCGID_IPV4_PREFIX | adjscope)) ||
811 	    (topword == (IB_MCGID_IPV6_PREFIX | adjscope)))
812 		ibd_h2n_mac(rmac, IB_MC_QPN, (((uint64_t)topword << 32) |
813 		    ((uint32_t)(state->id_pkey << 16))),
814 		    (INADDR_ALLRTRS_GROUP - INADDR_UNSPEC_GROUP));
815 	else
816 		/*
817 		 * Does not have proper bits in the mgid address.
818 		 */
819 		retval = B_FALSE;
820 
821 	return (retval);
822 }
823 
824 /*
825  * Padding for nd6 Neighbor Solicitation and Advertisement needs to be at
826  * front of optional src/tgt link layer address. Right now Solaris inserts
827  * padding by default at the end. The routine which is doing is nce_xmit()
828  * in ip_ndp.c. It copies the nd_lla_addr after the nd_opt_hdr_t. So when
829  * the packet comes down from IP layer to the IBD driver, it is in the
830  * following format: [IPoIB_PTXHDR_T][INET6 packet][ICMP6][OPT_ND_HDR_T]
831  * This size is 2 bytes followed by [22 bytes of ipoib_machdr]. As a result
832  * machdr is not 4 byte aligned and had 2 bytes of padding at the end.
833  *
834  * The send routine at IBD driver changes this packet as follows:
835  * [IPoIB_HDR_T][INET6 packet][ICMP6][OPT_ND_HDR_T + 2 bytes of padding]
836  * followed by [22 bytes of ipoib_machdr] resulting in machdr 4 byte
837  * aligned.
838  *
839  * At the receiving side again ibd_process_rx takes the above packet and
840  * removes the two bytes of front padding and inserts it at the end. This
841  * is since the IP layer does not understand padding at the front.
842  */
843 #define	IBD_PAD_NSNA(ip6h, len, type) {					\
844 	uchar_t 	*nd_lla_ptr;					\
845 	icmp6_t 	*icmp6;						\
846 	nd_opt_hdr_t	*opt;						\
847 	int 		i;						\
848 									\
849 	icmp6 = (icmp6_t *)&ip6h[1];					\
850 	len -= sizeof (nd_neighbor_advert_t);				\
851 	if (((icmp6->icmp6_type == ND_NEIGHBOR_SOLICIT) ||		\
852 	    (icmp6->icmp6_type == ND_NEIGHBOR_ADVERT)) &&		\
853 	    (len != 0)) {						\
854 		opt = (nd_opt_hdr_t *)((uint8_t *)ip6h			\
855 		    + IPV6_HDR_LEN + sizeof (nd_neighbor_advert_t));	\
856 		ASSERT(opt != NULL);					\
857 		nd_lla_ptr = (uchar_t *)&opt[1];			\
858 		if (type == IBD_SEND) {					\
859 			for (i = IPOIB_ADDRL; i > 0; i--)		\
860 				*(nd_lla_ptr + i + 1) =			\
861 				    *(nd_lla_ptr + i - 1);		\
862 		} else {						\
863 			for (i = 0; i < IPOIB_ADDRL; i++)		\
864 				*(nd_lla_ptr + i) =			\
865 				    *(nd_lla_ptr + i + 2);		\
866 		}							\
867 		*(nd_lla_ptr + i) = 0;					\
868 		*(nd_lla_ptr + i + 1) = 0;				\
869 	}								\
870 }
871 
872 /*
873  * Address handle entries maintained by the driver are kept in the
874  * free and active lists. Each entry starts out in the free list;
875  * it migrates to the active list when primed using ibt_get_paths()
876  * and ibt_modify_ud_dest() for transmission to a specific destination.
877  * In the active list, the entry has a reference count indicating the
878  * number of ongoing/uncompleted transmits that reference it. The
879  * entry is left in the active list even after the reference count
880  * goes to 0, since successive transmits can find it there and do
881  * not need to set up another entry (ie the path information is
882  * cached using the active list). Entries on the active list are
883  * also hashed using the destination link address as a key for faster
884  * lookups during transmits.
885  *
886  * For any destination address (unicast or multicast, whatever the
887  * join states), there will be at most one entry in the active list.
888  * Entries with a 0 reference count on the active list can be reused
889  * for a transmit to a new destination, if the free list is empty.
890  *
891  * The AH free list insertion/deletion is protected with the id_ac_mutex,
892  * since the async thread and Tx callback handlers insert/delete. The
893  * active list does not need a lock (all operations are done by the
894  * async thread) but updates to the reference count are atomically
895  * done (increments done by Tx path, decrements by the Tx callback handler).
896  */
897 #define	IBD_ACACHE_INSERT_FREE(state, ce) \
898 	list_insert_head(&state->id_ah_free, ce)
899 #define	IBD_ACACHE_GET_FREE(state) \
900 	list_get_head(&state->id_ah_free)
901 #define	IBD_ACACHE_INSERT_ACTIVE(state, ce) {			\
902 	int _ret_;						\
903 	list_insert_head(&state->id_ah_active, ce);		\
904 	_ret_ = mod_hash_insert(state->id_ah_active_hash,	\
905 	    (mod_hash_key_t)&ce->ac_mac, (mod_hash_val_t)ce);	\
906 	ASSERT(_ret_ == 0);					\
907 }
908 #define	IBD_ACACHE_PULLOUT_ACTIVE(state, ce) {			\
909 	list_remove(&state->id_ah_active, ce);			\
910 	(void) mod_hash_remove(state->id_ah_active_hash,	\
911 	    (mod_hash_key_t)&ce->ac_mac, (mod_hash_val_t)ce);	\
912 }
913 #define	IBD_ACACHE_GET_ACTIVE(state) \
914 	list_get_head(&state->id_ah_active)
915 
916 /*
917  * Membership states for different mcg's are tracked by two lists:
918  * the "non" list is used for promiscuous mode, when all mcg traffic
919  * needs to be inspected. This type of membership is never used for
920  * transmission, so there can not be an AH in the active list
921  * corresponding to a member in this list. This list does not need
922  * any protection, since all operations are performed by the async
923  * thread.
924  *
925  * "Full" and "SendOnly" membership is tracked using a single list,
926  * the "full" list. This is because this single list can then be
927  * searched during transmit to a multicast group (if an AH for the
928  * mcg is not found in the active list), since at least one type
929  * of membership must be present before initiating the transmit.
930  * This list is also emptied during driver detach, since sendonly
931  * membership acquired during transmit is dropped at detach time
932  * alongwith ipv4 broadcast full membership. Insert/deletes to
933  * this list are done only by the async thread, but it is also
934  * searched in program context (see multicast disable case), thus
935  * the id_mc_mutex protects the list. The driver detach path also
936  * deconstructs the "full" list, but it ensures that the async
937  * thread will not be accessing the list (by blocking out mcg
938  * trap handling and making sure no more Tx reaping will happen).
939  *
940  * Currently, an IBA attach is done in the SendOnly case too,
941  * although this is not required.
942  */
943 #define	IBD_MCACHE_INSERT_FULL(state, mce) \
944 	list_insert_head(&state->id_mc_full, mce)
945 #define	IBD_MCACHE_INSERT_NON(state, mce) \
946 	list_insert_head(&state->id_mc_non, mce)
947 #define	IBD_MCACHE_FIND_FULL(state, mgid) \
948 	ibd_mcache_find(mgid, &state->id_mc_full)
949 #define	IBD_MCACHE_FIND_NON(state, mgid) \
950 	ibd_mcache_find(mgid, &state->id_mc_non)
951 #define	IBD_MCACHE_PULLOUT_FULL(state, mce) \
952 	list_remove(&state->id_mc_full, mce)
953 #define	IBD_MCACHE_PULLOUT_NON(state, mce) \
954 	list_remove(&state->id_mc_non, mce)
955 
956 /*
957  * AH and MCE active list manipulation:
958  *
959  * Multicast disable requests and MCG delete traps are two cases
960  * where the active AH entry for the mcg (if any unreferenced one exists)
961  * will be moved to the free list (to force the next Tx to the mcg to
962  * join the MCG in SendOnly mode). Port up handling will also move AHs
963  * from active to free list.
964  *
965  * In the case when some transmits are still pending on an entry
966  * for an mcg, but a multicast disable has already been issued on the
967  * mcg, there are some options to consider to preserve the join state
968  * to ensure the emitted packet is properly routed on the IBA fabric.
969  * For the AH, we can
970  * 1. take out of active list at multicast disable time.
971  * 2. take out of active list only when last pending Tx completes.
972  * For the MCE, we can
973  * 3. take out of active list at multicast disable time.
974  * 4. take out of active list only when last pending Tx completes.
975  * 5. move from active list to stale list at multicast disable time.
976  * We choose to use 2,4. We use option 4 so that if a multicast enable
977  * is tried before the pending Tx completes, the enable code finds the
978  * mce in the active list and just has to make sure it will not be reaped
979  * (ie the mcg leave done) when the pending Tx does complete. Alternatively,
980  * a stale list (#5) that would be checked in the enable code would need
981  * to be implemented. Option 2 is used, because otherwise, a Tx attempt
982  * after the multicast disable would try to put an AH in the active list,
983  * and associate the mce it finds in the active list to this new AH,
984  * whereas the mce is already associated with the previous AH (taken off
985  * the active list), and will be removed once the pending Tx's complete
986  * (unless a reference count on mce's is implemented). One implication of
987  * using 2,4 is that new Tx's posted before the pending Tx's complete will
988  * grab new references on the AH, further delaying the leave.
989  *
990  * In the case of mcg delete (or create) trap when the port is sendonly
991  * joined, the AH and MCE handling is different: the AH and MCE has to be
992  * immediately taken off the active lists (forcing a join and path lookup
993  * at the next Tx is the only guaranteed means of ensuring a proper Tx
994  * to an mcg as it is repeatedly created and deleted and goes thru
995  * reincarnations).
996  *
997  * When a port is already sendonly joined, and a multicast enable is
998  * attempted, the same mce structure is promoted; this ensures only a
999  * single mce on the active list tracks the most powerful join state.
1000  *
1001  * In the case of port up event handling, the MCE for sendonly membership
1002  * is freed up, and the ACE is put into the free list as soon as possible
1003  * (depending on whether posted Tx's have completed). For fullmembership
1004  * MCE's though, the ACE is similarly handled; but the MCE is kept around
1005  * (a re-JOIN is attempted) only if the DLPI leave has not already been
1006  * done; else the mce is deconstructed (mc_fullreap case).
1007  *
1008  * MCG creation and deletion trap handling:
1009  *
1010  * These traps are unreliable (meaning sometimes the trap might never
1011  * be delivered to the subscribed nodes) and may arrive out-of-order
1012  * since they use UD transport. An alternative to relying on these
1013  * unreliable traps is to poll for mcg presence every so often, but
1014  * instead of doing that, we try to be as conservative as possible
1015  * while handling the traps, and hope that the traps do arrive at
1016  * the subscribed nodes soon. Note that if a node is fullmember
1017  * joined to an mcg, it can not possibly receive a mcg create/delete
1018  * trap for that mcg (by fullmember definition); if it does, it is
1019  * an old trap from a previous incarnation of the mcg.
1020  *
1021  * Whenever a trap is received, the driver cleans up its sendonly
1022  * membership to the group; we choose to do a sendonly leave even
1023  * on a creation trap to handle the case of a prior deletion of the mcg
1024  * having gone unnoticed. Consider an example scenario:
1025  * T1: MCG M is deleted, and fires off deletion trap D1.
1026  * T2: MCG M is recreated, fires off creation trap C1, which is lost.
1027  * T3: Node N tries to transmit to M, joining in sendonly mode.
1028  * T4: MCG M is deleted, and fires off deletion trap D2.
1029  * T5: N receives a deletion trap, but can not distinguish D1 from D2.
1030  *     If the trap is D2, then a LEAVE is not required, since the mcg
1031  *     is already deleted; but if it is D1, a LEAVE is required. A safe
1032  *     approach is to always LEAVE, but the SM may be confused if it
1033  *     receives a LEAVE without a prior JOIN.
1034  *
1035  * Management of the non-membership to an mcg is similar to the above,
1036  * except that if the interface is in promiscuous mode, it is required
1037  * to attempt to re-join the mcg after receiving a trap. Unfortunately,
1038  * if the re-join attempt fails (in which case a warning message needs
1039  * to be printed), it is not clear whether it failed due to the mcg not
1040  * existing, or some fabric/hca issues, due to the delayed nature of
1041  * trap delivery. Querying the SA to establish presence/absence of the
1042  * mcg is also racy at best. Thus, the driver just prints a warning
1043  * message when it can not rejoin after receiving a create trap, although
1044  * this might be (on rare occassions) a mis-warning if the create trap is
1045  * received after the mcg was deleted.
1046  */
1047 
1048 /*
1049  * Implementation of atomic "recycle" bits and reference count
1050  * on address handles. This utilizes the fact that max reference
1051  * count on any handle is limited by number of send wqes, thus
1052  * high bits in the ac_ref field can be used as the recycle bits,
1053  * and only the low bits hold the number of pending Tx requests.
1054  * This atomic AH reference counting allows the Tx completion
1055  * handler not to acquire the id_ac_mutex to process every completion,
1056  * thus reducing lock contention problems between completion and
1057  * the Tx path.
1058  */
1059 #define	CYCLEVAL		0x80000
1060 #define	CLEAR_REFCYCLE(ace)	(ace)->ac_ref = 0
1061 #define	CYCLE_SET(ace)		(((ace)->ac_ref & CYCLEVAL) == CYCLEVAL)
1062 #define	GET_REF(ace)		((ace)->ac_ref)
1063 #define	GET_REF_CYCLE(ace) (				\
1064 	/*						\
1065 	 * Make sure "cycle" bit is set.		\
1066 	 */						\
1067 	ASSERT(CYCLE_SET(ace)),				\
1068 	((ace)->ac_ref & ~(CYCLEVAL))			\
1069 )
1070 #define	INC_REF(ace, num) {				\
1071 	atomic_add_32(&(ace)->ac_ref, num);		\
1072 }
1073 #define	SET_CYCLE_IF_REF(ace) (				\
1074 	CYCLE_SET(ace) ? B_TRUE :			\
1075 	    atomic_add_32_nv(&ace->ac_ref, CYCLEVAL) ==	\
1076 		CYCLEVAL ?				\
1077 		/*					\
1078 		 * Clear the "cycle" bit we just set;	\
1079 		 * ref count known to be 0 from above.	\
1080 		 */					\
1081 		CLEAR_REFCYCLE(ace), B_FALSE :		\
1082 		/*					\
1083 		 * We set "cycle" bit; let caller know.	\
1084 		 */					\
1085 		B_TRUE					\
1086 )
1087 #define	DEC_REF_DO_CYCLE(ace) (				\
1088 	atomic_add_32_nv(&ace->ac_ref, -1) ==		\
1089 	    CYCLEVAL ?					\
1090 		/*					\
1091 		 * Ref count known to be 0 from above.	\
1092 		 */					\
1093 		B_TRUE :				\
1094 		B_FALSE					\
1095 )
1096 
1097 static void *
1098 list_get_head(list_t *list)
1099 {
1100 	list_node_t *lhead = list_head(list);
1101 
1102 	if (lhead != NULL)
1103 		list_remove(list, lhead);
1104 	return (lhead);
1105 }
1106 
1107 /*
1108  * This is always guaranteed to be able to queue the work.
1109  */
1110 static void
1111 ibd_queue_work_slot(ibd_state_t *state, ibd_req_t *ptr, int op)
1112 {
1113 	/* Initialize request */
1114 	DPRINT(1, "ibd_queue_work_slot : op: %d \n", op);
1115 	ptr->rq_op = op;
1116 
1117 	/*
1118 	 * Queue provided slot onto request pool.
1119 	 */
1120 	mutex_enter(&state->id_acache_req_lock);
1121 	list_insert_tail(&state->id_req_list, ptr);
1122 
1123 	/* Go, fetch, async thread */
1124 	cv_signal(&state->id_acache_req_cv);
1125 	mutex_exit(&state->id_acache_req_lock);
1126 }
1127 
1128 /*
1129  * Main body of the per interface async thread.
1130  */
1131 static void
1132 ibd_async_work(ibd_state_t *state)
1133 {
1134 	ibd_req_t *ptr;
1135 	callb_cpr_t cprinfo;
1136 
1137 	mutex_enter(&state->id_acache_req_lock);
1138 	CALLB_CPR_INIT(&cprinfo, &state->id_acache_req_lock,
1139 	    callb_generic_cpr, "ibd_async_work");
1140 
1141 	for (;;) {
1142 		ptr = list_get_head(&state->id_req_list);
1143 		if (ptr != NULL) {
1144 			mutex_exit(&state->id_acache_req_lock);
1145 
1146 			/*
1147 			 * Once we have done the operation, there is no
1148 			 * guarantee the request slot is going to be valid,
1149 			 * it might be freed up (as in IBD_ASYNC_LEAVE, REAP,
1150 			 * TRAP).
1151 			 *
1152 			 * Perform the request.
1153 			 */
1154 			switch (ptr->rq_op) {
1155 				case IBD_ASYNC_GETAH:
1156 					ibd_async_acache(state, &ptr->rq_mac);
1157 					break;
1158 				case IBD_ASYNC_JOIN:
1159 				case IBD_ASYNC_LEAVE:
1160 					ibd_async_multicast(state,
1161 					    ptr->rq_gid, ptr->rq_op);
1162 					break;
1163 				case IBD_ASYNC_PROMON:
1164 					ibd_async_setprom(state);
1165 					break;
1166 				case IBD_ASYNC_PROMOFF:
1167 					ibd_async_unsetprom(state);
1168 					break;
1169 				case IBD_ASYNC_REAP:
1170 					ibd_async_reap_group(state,
1171 					    ptr->rq_ptr, ptr->rq_gid,
1172 					    IB_MC_JSTATE_FULL);
1173 					/*
1174 					 * the req buf contains in mce
1175 					 * structure, so we do not need
1176 					 * to free it here.
1177 					 */
1178 					ptr = NULL;
1179 					break;
1180 				case IBD_ASYNC_TRAP:
1181 					ibd_async_trap(state, ptr);
1182 					break;
1183 				case IBD_ASYNC_SCHED:
1184 					ibd_async_txsched(state);
1185 					break;
1186 				case IBD_ASYNC_LINK:
1187 					ibd_async_link(state, ptr);
1188 					break;
1189 				case IBD_ASYNC_EXIT:
1190 					mutex_enter(&state->id_acache_req_lock);
1191 #ifndef __lock_lint
1192 					CALLB_CPR_EXIT(&cprinfo);
1193 #else
1194 					mutex_exit(&state->id_acache_req_lock);
1195 #endif
1196 					return;
1197 			}
1198 			if (ptr != NULL)
1199 				kmem_cache_free(state->id_req_kmc, ptr);
1200 
1201 			mutex_enter(&state->id_acache_req_lock);
1202 		} else {
1203 #ifndef __lock_lint
1204 			/*
1205 			 * Nothing to do: wait till new request arrives.
1206 			 */
1207 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
1208 			cv_wait(&state->id_acache_req_cv,
1209 			    &state->id_acache_req_lock);
1210 			CALLB_CPR_SAFE_END(&cprinfo,
1211 			    &state->id_acache_req_lock);
1212 #endif
1213 		}
1214 	}
1215 
1216 	/*NOTREACHED*/
1217 	_NOTE(NOT_REACHED)
1218 }
1219 
1220 /*
1221  * Return when it is safe to queue requests to the async daemon; primarily
1222  * for subnet trap and async event handling. Disallow requests before the
1223  * daemon is created, and when interface deinitilization starts.
1224  */
1225 static boolean_t
1226 ibd_async_safe(ibd_state_t *state)
1227 {
1228 	mutex_enter(&state->id_trap_lock);
1229 	if (state->id_trap_stop) {
1230 		mutex_exit(&state->id_trap_lock);
1231 		return (B_FALSE);
1232 	}
1233 	state->id_trap_inprog++;
1234 	mutex_exit(&state->id_trap_lock);
1235 	return (B_TRUE);
1236 }
1237 
1238 /*
1239  * Wake up ibd_drv_fini() if the detach code is waiting for pending subnet
1240  * trap or event handling to complete to kill the async thread and deconstruct
1241  * the mcg/ace list.
1242  */
1243 static void
1244 ibd_async_done(ibd_state_t *state)
1245 {
1246 	mutex_enter(&state->id_trap_lock);
1247 	if (--state->id_trap_inprog == 0)
1248 		cv_signal(&state->id_trap_cv);
1249 	mutex_exit(&state->id_trap_lock);
1250 }
1251 
1252 /*
1253  * Hash functions:
1254  * ibd_hash_by_id: Returns the qpn as the hash entry into bucket.
1255  * ibd_hash_key_cmp: Compares two keys, return 0 on success or else 1.
1256  * These operate on mac addresses input into ibd_send, but there is no
1257  * guarantee on the alignment of the ipoib_mac_t structure.
1258  */
1259 /*ARGSUSED*/
1260 static uint_t
1261 ibd_hash_by_id(void *hash_data, mod_hash_key_t key)
1262 {
1263 	ulong_t ptraddr = (ulong_t)key;
1264 	uint_t hval;
1265 
1266 	/*
1267 	 * If the input address is 4 byte aligned, we can just dereference
1268 	 * it. This is most common, since IP will send in a 4 byte aligned
1269 	 * IP header, which implies the 24 byte IPoIB psuedo header will be
1270 	 * 4 byte aligned too.
1271 	 */
1272 	if ((ptraddr & 3) == 0)
1273 		return ((uint_t)((ipoib_mac_t *)key)->ipoib_qpn);
1274 
1275 	bcopy(&(((ipoib_mac_t *)key)->ipoib_qpn), &hval, sizeof (uint_t));
1276 	return (hval);
1277 }
1278 
1279 static int
1280 ibd_hash_key_cmp(mod_hash_key_t key1, mod_hash_key_t key2)
1281 {
1282 	if (bcmp((char *)key1, (char *)key2, IPOIB_ADDRL) == 0)
1283 		return (0);
1284 	else
1285 		return (1);
1286 }
1287 
1288 /*
1289  * Initialize all the per interface caches and lists; AH cache,
1290  * MCG list etc.
1291  */
1292 static int
1293 ibd_acache_init(ibd_state_t *state)
1294 {
1295 	ibd_ace_t *ce;
1296 	int i;
1297 
1298 	mutex_init(&state->id_acache_req_lock, NULL, MUTEX_DRIVER, NULL);
1299 	cv_init(&state->id_acache_req_cv, NULL, CV_DEFAULT, NULL);
1300 
1301 	mutex_init(&state->id_ac_mutex, NULL, MUTEX_DRIVER, NULL);
1302 	mutex_init(&state->id_mc_mutex, NULL, MUTEX_DRIVER, NULL);
1303 	list_create(&state->id_ah_free, sizeof (ibd_ace_t),
1304 	    offsetof(ibd_ace_t, ac_list));
1305 	list_create(&state->id_ah_active, sizeof (ibd_ace_t),
1306 	    offsetof(ibd_ace_t, ac_list));
1307 	state->id_ah_active_hash = mod_hash_create_extended("IBD AH hash",
1308 	    IBD_HASH_SIZE, mod_hash_null_keydtor, mod_hash_null_valdtor,
1309 	    ibd_hash_by_id, NULL, ibd_hash_key_cmp, KM_SLEEP);
1310 	list_create(&state->id_mc_full, sizeof (ibd_mce_t),
1311 	    offsetof(ibd_mce_t, mc_list));
1312 	list_create(&state->id_mc_non, sizeof (ibd_mce_t),
1313 	    offsetof(ibd_mce_t, mc_list));
1314 	list_create(&state->id_req_list, sizeof (ibd_req_t),
1315 	    offsetof(ibd_req_t, rq_list));
1316 
1317 	state->id_ac_list = ce = (ibd_ace_t *)kmem_zalloc(sizeof (ibd_ace_t) *
1318 	    IBD_NUM_AH, KM_SLEEP);
1319 	for (i = 0; i < IBD_NUM_AH; i++, ce++) {
1320 		if (ibt_alloc_ud_dest(state->id_hca_hdl, IBT_UD_DEST_NO_FLAGS,
1321 		    state->id_pd_hdl, &ce->ac_dest) != IBT_SUCCESS) {
1322 			ibd_acache_fini(state);
1323 			return (DDI_FAILURE);
1324 		} else {
1325 			CLEAR_REFCYCLE(ce);
1326 			ce->ac_mce = NULL;
1327 			IBD_ACACHE_INSERT_FREE(state, ce);
1328 		}
1329 	}
1330 	return (DDI_SUCCESS);
1331 }
1332 
1333 static void
1334 ibd_acache_fini(ibd_state_t *state)
1335 {
1336 	ibd_ace_t *ptr;
1337 
1338 	mutex_enter(&state->id_ac_mutex);
1339 
1340 	while ((ptr = IBD_ACACHE_GET_ACTIVE(state)) != NULL) {
1341 		ASSERT(GET_REF(ptr) == 0);
1342 		(void) ibt_free_ud_dest(ptr->ac_dest);
1343 	}
1344 
1345 	while ((ptr = IBD_ACACHE_GET_FREE(state)) != NULL) {
1346 		ASSERT(GET_REF(ptr) == 0);
1347 		(void) ibt_free_ud_dest(ptr->ac_dest);
1348 	}
1349 
1350 	list_destroy(&state->id_ah_free);
1351 	list_destroy(&state->id_ah_active);
1352 	list_destroy(&state->id_mc_full);
1353 	list_destroy(&state->id_mc_non);
1354 	list_destroy(&state->id_req_list);
1355 	kmem_free(state->id_ac_list, sizeof (ibd_ace_t) * IBD_NUM_AH);
1356 	mutex_exit(&state->id_ac_mutex);
1357 	mutex_destroy(&state->id_ac_mutex);
1358 	mutex_destroy(&state->id_mc_mutex);
1359 	mutex_destroy(&state->id_acache_req_lock);
1360 	cv_destroy(&state->id_acache_req_cv);
1361 }
1362 
1363 /*
1364  * Search AH active hash list for a cached path to input destination.
1365  * If we are "just looking", hold == F. When we are in the Tx path,
1366  * we set hold == T to grab a reference on the AH so that it can not
1367  * be recycled to a new destination while the Tx request is posted.
1368  */
1369 static ibd_ace_t *
1370 ibd_acache_find(ibd_state_t *state, ipoib_mac_t *mac, boolean_t hold, int num)
1371 {
1372 	ibd_ace_t *ptr;
1373 
1374 	ASSERT(mutex_owned(&state->id_ac_mutex));
1375 
1376 	/*
1377 	 * Do hash search.
1378 	 */
1379 	if (mod_hash_find(state->id_ah_active_hash,
1380 	    (mod_hash_key_t)mac, (mod_hash_val_t)&ptr) == 0) {
1381 		if (hold)
1382 			INC_REF(ptr, num);
1383 		return (ptr);
1384 	}
1385 	return (NULL);
1386 }
1387 
1388 /*
1389  * This is called by the tx side; if an initialized AH is found in
1390  * the active list, it is locked down and can be used; if no entry
1391  * is found, an async request is queued to do path resolution.
1392  */
1393 static ibd_ace_t *
1394 ibd_acache_lookup(ibd_state_t *state, ipoib_mac_t *mac, int *err, int numwqe)
1395 {
1396 	ibd_ace_t *ptr;
1397 	ibd_req_t *req;
1398 
1399 	/*
1400 	 * Only attempt to print when we can; in the mdt pattr case, the
1401 	 * address is not aligned properly.
1402 	 */
1403 	if (((ulong_t)mac & 3) == 0) {
1404 		DPRINT(4,
1405 		    "ibd_acache_lookup : lookup for %08X:%08X:%08X:%08X:%08X",
1406 		    htonl(mac->ipoib_qpn), htonl(mac->ipoib_gidpref[0]),
1407 		    htonl(mac->ipoib_gidpref[1]), htonl(mac->ipoib_gidsuff[0]),
1408 		    htonl(mac->ipoib_gidsuff[1]));
1409 	}
1410 
1411 	mutex_enter(&state->id_ac_mutex);
1412 
1413 	if ((ptr = ibd_acache_find(state, mac, B_TRUE, numwqe)) != NULL) {
1414 		mutex_exit(&state->id_ac_mutex);
1415 		return (ptr);
1416 	}
1417 
1418 	/*
1419 	 * Implementation of a single outstanding async request; if
1420 	 * the operation is not started yet, queue a request and move
1421 	 * to ongoing state. Remember in id_ah_addr for which address
1422 	 * we are queueing the request, in case we need to flag an error;
1423 	 * Any further requests, for the same or different address, until
1424 	 * the operation completes, is sent back to GLDv3 to be retried.
1425 	 * The async thread will update id_ah_op with an error indication
1426 	 * or will set it to indicate the next look up can start; either
1427 	 * way, it will mac_tx_update() so that all blocked requests come
1428 	 * back here.
1429 	 */
1430 	*err = EAGAIN;
1431 	if (state->id_ah_op == IBD_OP_NOTSTARTED) {
1432 		req = kmem_cache_alloc(state->id_req_kmc, KM_NOSLEEP);
1433 		if (req != NULL) {
1434 			/*
1435 			 * We did not even find the entry; queue a request
1436 			 * for it.
1437 			 */
1438 			bcopy(mac, &(req->rq_mac), IPOIB_ADDRL);
1439 			ibd_queue_work_slot(state, req, IBD_ASYNC_GETAH);
1440 			state->id_ah_op = IBD_OP_ONGOING;
1441 			bcopy(mac, &state->id_ah_addr, IPOIB_ADDRL);
1442 		}
1443 	} else if ((state->id_ah_op != IBD_OP_ONGOING) &&
1444 	    (bcmp(&state->id_ah_addr, mac, IPOIB_ADDRL) == 0)) {
1445 		/*
1446 		 * Check the status of the pathrecord lookup request
1447 		 * we had queued before.
1448 		 */
1449 		if (state->id_ah_op == IBD_OP_ERRORED) {
1450 			*err = EFAULT;
1451 			state->id_ah_error++;
1452 		} else {
1453 			/*
1454 			 * IBD_OP_ROUTERED case: We need to send to the
1455 			 * all-router MCG. If we can find the AH for
1456 			 * the mcg, the Tx will be attempted. If we
1457 			 * do not find the AH, we return NORESOURCES
1458 			 * to retry.
1459 			 */
1460 			ipoib_mac_t routermac;
1461 
1462 			(void) ibd_get_allroutergroup(state, mac, &routermac);
1463 			ptr = ibd_acache_find(state, &routermac, B_TRUE,
1464 			    numwqe);
1465 		}
1466 		state->id_ah_op = IBD_OP_NOTSTARTED;
1467 	} else if ((state->id_ah_op != IBD_OP_ONGOING) &&
1468 	    (bcmp(&state->id_ah_addr, mac, IPOIB_ADDRL) != 0)) {
1469 		/*
1470 		 * This case can happen when we get a higher band
1471 		 * packet. The easiest way is to reset the state machine
1472 		 * to accommodate the higher priority packet.
1473 		 */
1474 		state->id_ah_op = IBD_OP_NOTSTARTED;
1475 	}
1476 	mutex_exit(&state->id_ac_mutex);
1477 
1478 	return (ptr);
1479 }
1480 
1481 /*
1482  * Grab a not-currently-in-use AH/PathRecord from the active
1483  * list to recycle to a new destination. Only the async thread
1484  * executes this code.
1485  */
1486 static ibd_ace_t *
1487 ibd_acache_get_unref(ibd_state_t *state)
1488 {
1489 	ibd_ace_t *ptr = list_head(&state->id_ah_active);
1490 
1491 	ASSERT(mutex_owned(&state->id_ac_mutex));
1492 
1493 	/*
1494 	 * Do plain linear search.
1495 	 */
1496 	while (ptr != NULL) {
1497 		/*
1498 		 * Note that it is possible that the "cycle" bit
1499 		 * is set on the AH w/o any reference count. The
1500 		 * mcg must have been deleted, and the tx cleanup
1501 		 * just decremented the reference count to 0, but
1502 		 * hasn't gotten around to grabbing the id_ac_mutex
1503 		 * to move the AH into the free list.
1504 		 */
1505 		if (GET_REF(ptr) == 0) {
1506 			IBD_ACACHE_PULLOUT_ACTIVE(state, ptr);
1507 			break;
1508 		}
1509 		ptr = list_next(&state->id_ah_active, ptr);
1510 	}
1511 	return (ptr);
1512 }
1513 
1514 /*
1515  * Invoked to clean up AH from active list in case of multicast
1516  * disable and to handle sendonly memberships during mcg traps.
1517  * And for port up processing for multicast and unicast AHs.
1518  * Normally, the AH is taken off the active list, and put into
1519  * the free list to be recycled for a new destination. In case
1520  * Tx requests on the AH have not completed yet, the AH is marked
1521  * for reaping (which will put the AH on the free list) once the Tx's
1522  * complete; in this case, depending on the "force" input, we take
1523  * out the AH from the active list right now, or leave it also for
1524  * the reap operation. Returns TRUE if the AH is taken off the active
1525  * list (and either put into the free list right now, or arranged for
1526  * later), FALSE otherwise.
1527  */
1528 static boolean_t
1529 ibd_acache_recycle(ibd_state_t *state, ipoib_mac_t *acmac, boolean_t force)
1530 {
1531 	ibd_ace_t *acactive;
1532 	boolean_t ret = B_TRUE;
1533 
1534 	ASSERT(mutex_owned(&state->id_ac_mutex));
1535 
1536 	if ((acactive = ibd_acache_find(state, acmac, B_FALSE, 0)) != NULL) {
1537 
1538 		/*
1539 		 * Note that the AH might already have the cycle bit set
1540 		 * on it; this might happen if sequences of multicast
1541 		 * enables and disables are coming so fast, that posted
1542 		 * Tx's to the mcg have not completed yet, and the cycle
1543 		 * bit is set successively by each multicast disable.
1544 		 */
1545 		if (SET_CYCLE_IF_REF(acactive)) {
1546 			if (!force) {
1547 				/*
1548 				 * The ace is kept on the active list, further
1549 				 * Tx's can still grab a reference on it; the
1550 				 * ace is reaped when all pending Tx's
1551 				 * referencing the AH complete.
1552 				 */
1553 				ret = B_FALSE;
1554 			} else {
1555 				/*
1556 				 * In the mcg trap case, we always pull the
1557 				 * AH from the active list. And also the port
1558 				 * up multi/unicast case.
1559 				 */
1560 				IBD_ACACHE_PULLOUT_ACTIVE(state, acactive);
1561 				acactive->ac_mce = NULL;
1562 			}
1563 		} else {
1564 			/*
1565 			 * Determined the ref count is 0, thus reclaim
1566 			 * immediately after pulling out the ace from
1567 			 * the active list.
1568 			 */
1569 			IBD_ACACHE_PULLOUT_ACTIVE(state, acactive);
1570 			acactive->ac_mce = NULL;
1571 			IBD_ACACHE_INSERT_FREE(state, acactive);
1572 		}
1573 
1574 	}
1575 	return (ret);
1576 }
1577 
1578 /*
1579  * Helper function for async path record lookup. If we are trying to
1580  * Tx to a MCG, check our membership, possibly trying to join the
1581  * group if required. If that fails, try to send the packet to the
1582  * all router group (indicated by the redirect output), pointing
1583  * the input mac address to the router mcg address.
1584  */
1585 static ibd_mce_t *
1586 ibd_async_mcache(ibd_state_t *state, ipoib_mac_t *mac, boolean_t *redirect)
1587 {
1588 	ib_gid_t mgid;
1589 	ibd_mce_t *mce;
1590 	ipoib_mac_t routermac;
1591 
1592 	*redirect = B_FALSE;
1593 	ibd_n2h_gid(mac, &mgid);
1594 
1595 	/*
1596 	 * Check the FullMember+SendOnlyNonMember list.
1597 	 * Since we are the only one who manipulates the
1598 	 * id_mc_full list, no locks are needed.
1599 	 */
1600 	mce = IBD_MCACHE_FIND_FULL(state, mgid);
1601 	if (mce != NULL) {
1602 		DPRINT(4, "ibd_async_mcache : already joined to group");
1603 		return (mce);
1604 	}
1605 
1606 	/*
1607 	 * Not found; try to join(SendOnlyNonMember) and attach.
1608 	 */
1609 	DPRINT(4, "ibd_async_mcache : not joined to group");
1610 	if ((mce = ibd_join_group(state, mgid, IB_MC_JSTATE_SEND_ONLY_NON)) !=
1611 	    NULL) {
1612 		DPRINT(4, "ibd_async_mcache : nonmem joined to group");
1613 		return (mce);
1614 	}
1615 
1616 	/*
1617 	 * MCGroup not present; try to join the all-router group. If
1618 	 * any of the following steps succeed, we will be redirecting
1619 	 * to the all router group.
1620 	 */
1621 	DPRINT(4, "ibd_async_mcache : nonmem join failed");
1622 	if (!ibd_get_allroutergroup(state, mac, &routermac))
1623 		return (NULL);
1624 	*redirect = B_TRUE;
1625 	ibd_n2h_gid(&routermac, &mgid);
1626 	bcopy(&routermac, mac, IPOIB_ADDRL);
1627 	DPRINT(4, "ibd_async_mcache : router mgid : %016llx:%016llx\n",
1628 	    mgid.gid_prefix, mgid.gid_guid);
1629 
1630 	/*
1631 	 * Are we already joined to the router group?
1632 	 */
1633 	if ((mce = IBD_MCACHE_FIND_FULL(state, mgid)) != NULL) {
1634 		DPRINT(4, "ibd_async_mcache : using already joined router"
1635 		    "group\n");
1636 		return (mce);
1637 	}
1638 
1639 	/*
1640 	 * Can we join(SendOnlyNonMember) the router group?
1641 	 */
1642 	DPRINT(4, "ibd_async_mcache : attempting join to router grp");
1643 	if ((mce = ibd_join_group(state, mgid, IB_MC_JSTATE_SEND_ONLY_NON)) !=
1644 	    NULL) {
1645 		DPRINT(4, "ibd_async_mcache : joined to router grp");
1646 		return (mce);
1647 	}
1648 
1649 	return (NULL);
1650 }
1651 
1652 /*
1653  * Async path record lookup code.
1654  */
1655 static void
1656 ibd_async_acache(ibd_state_t *state, ipoib_mac_t *mac)
1657 {
1658 	ibd_ace_t *ce;
1659 	ibd_mce_t *mce = NULL;
1660 	ibt_path_attr_t path_attr;
1661 	ibt_path_info_t path_info;
1662 	ib_gid_t destgid;
1663 	int ret = IBD_OP_NOTSTARTED;
1664 
1665 	DPRINT(4, "ibd_async_acache :  %08X:%08X:%08X:%08X:%08X",
1666 	    htonl(mac->ipoib_qpn), htonl(mac->ipoib_gidpref[0]),
1667 	    htonl(mac->ipoib_gidpref[1]), htonl(mac->ipoib_gidsuff[0]),
1668 	    htonl(mac->ipoib_gidsuff[1]));
1669 
1670 	/*
1671 	 * Check whether we are trying to transmit to a MCG.
1672 	 * In that case, we need to make sure we are a member of
1673 	 * the MCG.
1674 	 */
1675 	if (mac->ipoib_qpn == htonl(IB_MC_QPN)) {
1676 		boolean_t redirected;
1677 
1678 		/*
1679 		 * If we can not find or join the group or even
1680 		 * redirect, error out.
1681 		 */
1682 		if ((mce = ibd_async_mcache(state, mac, &redirected)) ==
1683 		    NULL) {
1684 			state->id_ah_op = IBD_OP_ERRORED;
1685 			return;
1686 		}
1687 
1688 		/*
1689 		 * If we got redirected, we need to determine whether
1690 		 * the AH for the new mcg is in the cache already, and
1691 		 * not pull it in then; otherwise proceed to get the
1692 		 * path for the new mcg. There is no guarantee that
1693 		 * if the AH is currently in the cache, it will still be
1694 		 * there when we look in ibd_acache_lookup(), but that's
1695 		 * okay, we will come back here.
1696 		 */
1697 		if (redirected) {
1698 			ret = IBD_OP_ROUTERED;
1699 			DPRINT(4, "ibd_async_acache :  redirected to "
1700 			    "%08X:%08X:%08X:%08X:%08X",
1701 			    htonl(mac->ipoib_qpn), htonl(mac->ipoib_gidpref[0]),
1702 			    htonl(mac->ipoib_gidpref[1]),
1703 			    htonl(mac->ipoib_gidsuff[0]),
1704 			    htonl(mac->ipoib_gidsuff[1]));
1705 
1706 			mutex_enter(&state->id_ac_mutex);
1707 			if (ibd_acache_find(state, mac, B_FALSE, 0) != NULL) {
1708 				state->id_ah_op = IBD_OP_ROUTERED;
1709 				mutex_exit(&state->id_ac_mutex);
1710 				DPRINT(4, "ibd_async_acache : router AH found");
1711 				return;
1712 			}
1713 			mutex_exit(&state->id_ac_mutex);
1714 		}
1715 	}
1716 
1717 	/*
1718 	 * Get an AH from the free list.
1719 	 */
1720 	mutex_enter(&state->id_ac_mutex);
1721 	if ((ce = IBD_ACACHE_GET_FREE(state)) == NULL) {
1722 		/*
1723 		 * No free ones; try to grab an unreferenced active
1724 		 * one. Maybe we need to make the active list LRU,
1725 		 * but that will create more work for Tx callbacks.
1726 		 * Is there a way of not having to pull out the
1727 		 * entry from the active list, but just indicate it
1728 		 * is being recycled? Yes, but that creates one more
1729 		 * check in the fast lookup path.
1730 		 */
1731 		if ((ce = ibd_acache_get_unref(state)) == NULL) {
1732 			/*
1733 			 * Pretty serious shortage now.
1734 			 */
1735 			state->id_ah_op = IBD_OP_NOTSTARTED;
1736 			mutex_exit(&state->id_ac_mutex);
1737 			DPRINT(10, "ibd_async_acache : failed to find AH "
1738 			    "slot\n");
1739 			return;
1740 		}
1741 		/*
1742 		 * We could check whether ac_mce points to a SendOnly
1743 		 * member and drop that membership now. Or do it lazily
1744 		 * at detach time.
1745 		 */
1746 		ce->ac_mce = NULL;
1747 	}
1748 	mutex_exit(&state->id_ac_mutex);
1749 	ASSERT(ce->ac_mce == NULL);
1750 
1751 	/*
1752 	 * Update the entry.
1753 	 */
1754 	bcopy((char *)mac, &ce->ac_mac, IPOIB_ADDRL);
1755 
1756 	bzero(&path_info, sizeof (path_info));
1757 	bzero(&path_attr, sizeof (ibt_path_attr_t));
1758 	path_attr.pa_sgid = state->id_sgid;
1759 	path_attr.pa_num_dgids = 1;
1760 	ibd_n2h_gid(&ce->ac_mac, &destgid);
1761 	path_attr.pa_dgids = &destgid;
1762 	path_attr.pa_sl = state->id_mcinfo->mc_adds_vect.av_srvl;
1763 	if (ibt_get_paths(state->id_ibt_hdl, IBT_PATH_NO_FLAGS,
1764 	    &path_attr, 1, &path_info, NULL) != IBT_SUCCESS) {
1765 		DPRINT(10, "ibd_async_acache : failed in ibt_get_paths");
1766 		goto error;
1767 	}
1768 	if (ibt_modify_ud_dest(ce->ac_dest, state->id_mcinfo->mc_qkey,
1769 	    ntohl(ce->ac_mac.ipoib_qpn),
1770 	    &path_info.pi_prim_cep_path.cep_adds_vect) != IBT_SUCCESS) {
1771 		DPRINT(10, "ibd_async_acache : failed in ibt_modify_ud_dest");
1772 		goto error;
1773 	}
1774 
1775 	/*
1776 	 * mce is set whenever an AH is being associated with a
1777 	 * MCG; this will come in handy when we leave the MCG. The
1778 	 * lock protects Tx fastpath from scanning the active list.
1779 	 */
1780 	if (mce != NULL)
1781 		ce->ac_mce = mce;
1782 	mutex_enter(&state->id_ac_mutex);
1783 	IBD_ACACHE_INSERT_ACTIVE(state, ce);
1784 	state->id_ah_op = ret;
1785 	mutex_exit(&state->id_ac_mutex);
1786 	return;
1787 error:
1788 	/*
1789 	 * We might want to drop SendOnly membership here if we
1790 	 * joined above. The lock protects Tx callbacks inserting
1791 	 * into the free list.
1792 	 */
1793 	mutex_enter(&state->id_ac_mutex);
1794 	state->id_ah_op = IBD_OP_ERRORED;
1795 	IBD_ACACHE_INSERT_FREE(state, ce);
1796 	mutex_exit(&state->id_ac_mutex);
1797 }
1798 
1799 /*
1800  * While restoring port's presence on the subnet on a port up, it is possible
1801  * that the port goes down again.
1802  */
1803 static void
1804 ibd_async_link(ibd_state_t *state, ibd_req_t *req)
1805 {
1806 	ibd_link_op_t opcode = (ibd_link_op_t)req->rq_ptr;
1807 	link_state_t lstate = (opcode == IBD_LINK_DOWN) ? LINK_STATE_DOWN :
1808 	    LINK_STATE_UP;
1809 	ibd_mce_t *mce, *pmce;
1810 	ibd_ace_t *ace, *pace;
1811 
1812 	DPRINT(10, "ibd_async_link(): %d", opcode);
1813 
1814 	/*
1815 	 * On a link up, revalidate the link speed/width. No point doing
1816 	 * this on a link down, since we will be unable to do SA operations,
1817 	 * defaulting to the lowest speed. Also notice that we update our
1818 	 * notion of speed before calling mac_link_update(), which will do
1819 	 * neccesary higher level notifications for speed changes.
1820 	 */
1821 	if ((opcode == IBD_LINK_UP_ABSENT) || (opcode == IBD_LINK_UP)) {
1822 		_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*state))
1823 		state->id_link_speed = ibd_get_portspeed(state);
1824 		_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*state))
1825 	}
1826 
1827 	/*
1828 	 * Do all the work required to establish our presence on
1829 	 * the subnet.
1830 	 */
1831 	if (opcode == IBD_LINK_UP_ABSENT) {
1832 		/*
1833 		 * If in promiscuous mode ...
1834 		 */
1835 		if (state->id_prom_op == IBD_OP_COMPLETED) {
1836 			/*
1837 			 * Drop all nonmembership.
1838 			 */
1839 			ibd_async_unsetprom(state);
1840 
1841 			/*
1842 			 * Then, try to regain nonmembership to all mcg's.
1843 			 */
1844 			ibd_async_setprom(state);
1845 
1846 		}
1847 
1848 		/*
1849 		 * Drop all sendonly membership (which also gets rid of the
1850 		 * AHs); try to reacquire all full membership.
1851 		 */
1852 		mce = list_head(&state->id_mc_full);
1853 		while ((pmce = mce) != NULL) {
1854 			mce = list_next(&state->id_mc_full, mce);
1855 			if (pmce->mc_jstate == IB_MC_JSTATE_SEND_ONLY_NON)
1856 				ibd_leave_group(state,
1857 				    pmce->mc_info.mc_adds_vect.av_dgid,
1858 				    IB_MC_JSTATE_SEND_ONLY_NON);
1859 			else
1860 				ibd_reacquire_group(state, pmce);
1861 		}
1862 
1863 		/*
1864 		 * Recycle all active AHs to free list (and if there are
1865 		 * pending posts, make sure they will go into the free list
1866 		 * once the Tx's complete). Grab the lock to prevent
1867 		 * concurrent Tx's as well as Tx cleanups.
1868 		 */
1869 		mutex_enter(&state->id_ac_mutex);
1870 		ace = list_head(&state->id_ah_active);
1871 		while ((pace = ace) != NULL) {
1872 			boolean_t cycled;
1873 
1874 			ace = list_next(&state->id_ah_active, ace);
1875 			mce = pace->ac_mce;
1876 			cycled = ibd_acache_recycle(state, &pace->ac_mac,
1877 			    B_TRUE);
1878 			/*
1879 			 * If this is for an mcg, it must be for a fullmember,
1880 			 * since we got rid of send-only members above when
1881 			 * processing the mce list.
1882 			 */
1883 			ASSERT(cycled && ((mce == NULL) || (mce->mc_jstate ==
1884 			    IB_MC_JSTATE_FULL)));
1885 
1886 			/*
1887 			 * Check if the fullmember mce needs to be torn down,
1888 			 * ie whether the DLPI disable has already been done.
1889 			 * If so, do some of the work of tx_cleanup, namely
1890 			 * causing leave (which will fail), detach and
1891 			 * mce-freeing. tx_cleanup will put the AH into free
1892 			 * list. The reason to duplicate some of this
1893 			 * tx_cleanup work is because we want to delete the
1894 			 * AH right now instead of waiting for tx_cleanup, to
1895 			 * force subsequent Tx's to reacquire an AH.
1896 			 */
1897 			if ((mce != NULL) && (mce->mc_fullreap))
1898 				ibd_async_reap_group(state, mce,
1899 				    mce->mc_info.mc_adds_vect.av_dgid,
1900 				    mce->mc_jstate);
1901 		}
1902 		mutex_exit(&state->id_ac_mutex);
1903 	}
1904 
1905 	/*
1906 	 * mac handle is guaranteed to exist since driver does ibt_close_hca()
1907 	 * (which stops further events from being delivered) before
1908 	 * mac_unregister(). At this point, it is guaranteed that mac_register
1909 	 * has already been done.
1910 	 */
1911 	mutex_enter(&state->id_link_mutex);
1912 	state->id_link_state = lstate;
1913 	mac_link_update(state->id_mh, lstate);
1914 	mutex_exit(&state->id_link_mutex);
1915 
1916 	ibd_async_done(state);
1917 }
1918 
1919 /*
1920  * When the link is notified up, we need to do a few things, based
1921  * on the port's current p_init_type_reply claiming a reinit has been
1922  * done or not. The reinit steps are:
1923  * 1. If in InitTypeReply, NoLoadReply == PreserveContentReply == 0, verify
1924  *    the old Pkey and GID0 are correct.
1925  * 2. Register for mcg traps (already done by ibmf).
1926  * 3. If PreservePresenceReply indicates the SM has restored port's presence
1927  *    in subnet, nothing more to do. Else go to next steps (on async daemon).
1928  * 4. Give up all sendonly memberships.
1929  * 5. Acquire all full memberships.
1930  * 6. In promiscuous mode, acquire all non memberships.
1931  * 7. Recycle all AHs to free list.
1932  */
1933 static void
1934 ibd_link_mod(ibd_state_t *state, ibt_async_code_t code)
1935 {
1936 	ibt_hca_portinfo_t *port_infop;
1937 	ibt_status_t ibt_status;
1938 	uint_t psize, port_infosz;
1939 	ibd_link_op_t opcode;
1940 	ibd_req_t *req;
1941 
1942 	/*
1943 	 * Do not send a request to the async daemon if it has not
1944 	 * yet been created or is being destroyed. If the async
1945 	 * daemon has not yet been created, we still need to track
1946 	 * last known state of the link. If this code races with the
1947 	 * detach path, then we are assured that the detach path has
1948 	 * not yet done the ibt_close_hca (which waits for all async
1949 	 * events to complete). If the code races with the attach path,
1950 	 * we need to validate the pkey/gid (in the link_up case) if
1951 	 * the initialization path has already set these up and created
1952 	 * IBTF resources based on the values.
1953 	 */
1954 	mutex_enter(&state->id_link_mutex);
1955 
1956 	/*
1957 	 * If the init code in ibd_drv_init hasn't yet set up the
1958 	 * pkey/gid, nothing to do; that code will set the link state.
1959 	 */
1960 	if (state->id_link_state == LINK_STATE_UNKNOWN) {
1961 		mutex_exit(&state->id_link_mutex);
1962 		return;
1963 	}
1964 
1965 	if (code == IBT_EVENT_PORT_UP) {
1966 		uint8_t itreply;
1967 		boolean_t badup = B_FALSE;
1968 
1969 		ibt_status = ibt_query_hca_ports(state->id_hca_hdl,
1970 		    state->id_port, &port_infop, &psize, &port_infosz);
1971 		if ((ibt_status != IBT_SUCCESS) || (psize != 1)) {
1972 			mutex_exit(&state->id_link_mutex);
1973 			DPRINT(10, "ibd_link_up : failed in"
1974 			    " ibt_query_port()\n");
1975 			return;
1976 		}
1977 
1978 		/*
1979 		 * If the link already went down by the time the handler gets
1980 		 * here, give up; we can not even validate pkey/gid since those
1981 		 * are not valid.
1982 		 */
1983 		if (port_infop->p_linkstate != IBT_PORT_ACTIVE)
1984 			badup = B_TRUE;
1985 
1986 		itreply = port_infop->p_init_type_reply;
1987 
1988 		/*
1989 		 * In InitTypeReply, check if NoLoadReply ==
1990 		 * PreserveContentReply == 0, in which case, verify Pkey/GID0.
1991 		 */
1992 		if (((itreply & SM_INIT_TYPE_REPLY_NO_LOAD_REPLY) == 0) &&
1993 		    ((itreply & SM_INIT_TYPE_PRESERVE_CONTENT_REPLY) == 0) &&
1994 		    (!badup)) {
1995 			/*
1996 			 * Check that the subnet part of GID0 has not changed.
1997 			 */
1998 			if (bcmp(port_infop->p_sgid_tbl, &state->id_sgid,
1999 			    sizeof (ib_gid_t)) != 0)
2000 				badup = B_TRUE;
2001 
2002 			/*
2003 			 * Check that Pkey/index mapping is still valid.
2004 			 */
2005 			if ((port_infop->p_pkey_tbl_sz <= state->id_pkix) ||
2006 			    (port_infop->p_pkey_tbl[state->id_pkix] !=
2007 			    state->id_pkey))
2008 				badup = B_TRUE;
2009 		}
2010 
2011 		/*
2012 		 * In InitTypeReply, if PreservePresenceReply indicates the SM
2013 		 * has ensured that the port's presence in mcg, traps etc is
2014 		 * intact, nothing more to do.
2015 		 */
2016 		opcode = IBD_LINK_UP_ABSENT;
2017 		if ((itreply & SM_INIT_TYPE_PRESERVE_PRESENCE_REPLY) ==
2018 		    SM_INIT_TYPE_PRESERVE_PRESENCE_REPLY)
2019 			opcode = IBD_LINK_UP;
2020 
2021 		if (badup)
2022 			code = IBT_ERROR_PORT_DOWN;
2023 		ibt_free_portinfo(port_infop, port_infosz);
2024 	}
2025 
2026 	if (!ibd_async_safe(state)) {
2027 		state->id_link_state = ((code == IBT_EVENT_PORT_UP) ?
2028 		    LINK_STATE_UP : LINK_STATE_DOWN);
2029 		mutex_exit(&state->id_link_mutex);
2030 		return;
2031 	}
2032 	mutex_exit(&state->id_link_mutex);
2033 
2034 	if (code == IBT_ERROR_PORT_DOWN)
2035 		opcode = IBD_LINK_DOWN;
2036 
2037 	req = kmem_cache_alloc(state->id_req_kmc, KM_SLEEP);
2038 	req->rq_ptr = (void *)opcode;
2039 	ibd_queue_work_slot(state, req, IBD_ASYNC_LINK);
2040 }
2041 
2042 /*
2043  * For the port up/down events, IBTL guarantees there will not be concurrent
2044  * invocations of the handler. IBTL might coalesce link transition events,
2045  * and not invoke the handler for _each_ up/down transition, but it will
2046  * invoke the handler with last known state
2047  */
2048 static void
2049 ibd_async_handler(void *clnt_private, ibt_hca_hdl_t hca_hdl,
2050     ibt_async_code_t code, ibt_async_event_t *event)
2051 {
2052 	ibd_state_t *state = (ibd_state_t *)clnt_private;
2053 
2054 	switch (code) {
2055 	case IBT_ERROR_CATASTROPHIC_CHAN:
2056 		ibd_print_warn(state, "catastrophic channel error");
2057 		break;
2058 	case IBT_ERROR_CQ:
2059 		ibd_print_warn(state, "completion queue error");
2060 		break;
2061 	case IBT_ERROR_PORT_DOWN:
2062 	case IBT_EVENT_PORT_UP:
2063 		/*
2064 		 * Events will be delivered to all instances that have
2065 		 * done ibt_open_hca() but not yet done ibt_close_hca().
2066 		 * Only need to do work for our port; IBTF will deliver
2067 		 * events for other ports on the hca we have ibt_open_hca'ed
2068 		 * too. Note that ibd_drv_init() initializes id_port before
2069 		 * doing ibt_open_hca().
2070 		 */
2071 		ASSERT(state->id_hca_hdl == hca_hdl);
2072 		if (state->id_port != event->ev_port)
2073 			break;
2074 
2075 		ibd_link_mod(state, code);
2076 		break;
2077 
2078 	case IBT_HCA_ATTACH_EVENT:
2079 	case IBT_HCA_DETACH_EVENT:
2080 		/*
2081 		 * When a new card is plugged to the system, attach_event is
2082 		 * invoked. Additionally, a cfgadm needs to be run to make the
2083 		 * card known to the system, and an ifconfig needs to be run to
2084 		 * plumb up any ibd interfaces on the card. In the case of card
2085 		 * unplug, a cfgadm is run that will trigger any RCM scripts to
2086 		 * unplumb the ibd interfaces on the card; when the card is
2087 		 * actually unplugged, the detach_event is invoked;
2088 		 * additionally, if any ibd instances are still active on the
2089 		 * card (eg there were no associated RCM scripts), driver's
2090 		 * detach routine is invoked.
2091 		 */
2092 		break;
2093 	default:
2094 		break;
2095 	}
2096 }
2097 
2098 /*
2099  * Attach device to the IO framework.
2100  */
2101 static int
2102 ibd_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2103 {
2104 	mac_register_t *macp;
2105 	ibd_state_t *state;
2106 	int instance;
2107 	int err;
2108 
2109 	switch (cmd) {
2110 		case DDI_ATTACH:
2111 			break;
2112 		case DDI_RESUME:
2113 			/* This driver does not support resume */
2114 		default:
2115 			return (DDI_FAILURE);
2116 	}
2117 
2118 	/*
2119 	 * Allocate soft device data structure
2120 	 */
2121 	instance = ddi_get_instance(dip);
2122 	if (ddi_soft_state_zalloc(ibd_list, instance) == DDI_FAILURE)
2123 		return (DDI_FAILURE);
2124 	state = ddi_get_soft_state(ibd_list, instance);
2125 
2126 	/* pre ibt_attach() soft state initialization */
2127 	if (ibd_state_init(state, dip) != DDI_SUCCESS) {
2128 		DPRINT(10, "ibd_attach : failed in ibd_state_init()");
2129 		goto attach_fail_state_init;
2130 	}
2131 
2132 	/* alloc rx soft intr */
2133 	if ((ibd_rx_softintr == 1) &&
2134 	    ddi_add_softintr(dip, DDI_SOFTINT_LOW, &state->id_rx,
2135 	    NULL, NULL, ibd_intr, (caddr_t)state) != DDI_SUCCESS) {
2136 		DPRINT(10, "ibd_attach : failed in ddi_add_softintr()");
2137 		goto attach_fail_ddi_add_rx_softintr;
2138 	}
2139 
2140 	/* alloc tx soft intr */
2141 	if ((ibd_tx_softintr == 1) &&
2142 	    ddi_add_softintr(dip, DDI_SOFTINT_LOW, &state->id_tx,
2143 	    NULL, NULL, ibd_tx_recycle, (caddr_t)state) != DDI_SUCCESS) {
2144 		DPRINT(10, "ibd_attach : failed in ddi_add_softintr()");
2145 		goto attach_fail_ddi_add_tx_softintr;
2146 	}
2147 
2148 	/* "attach" to IBTL */
2149 	if (ibt_attach(&ibd_clnt_modinfo, dip, state,
2150 	    &state->id_ibt_hdl) != IBT_SUCCESS) {
2151 		DPRINT(10, "ibd_attach : failed in ibt_attach()");
2152 		goto attach_fail_ibt_attach;
2153 	}
2154 
2155 	/* Finish initializing this driver */
2156 	if (ibd_drv_init(state) != DDI_SUCCESS) {
2157 		DPRINT(10, "ibd_attach : failed in ibd_drv_init()\n");
2158 		goto attach_fail_drv_init;
2159 	}
2160 
2161 	/*
2162 	 * Initialize pointers to device specific functions which will be
2163 	 * used by the generic layer.
2164 	 */
2165 	if ((macp = mac_alloc(MAC_VERSION)) == NULL) {
2166 		DPRINT(10, "ibd_attach : failed in mac_alloc()");
2167 		goto attach_fail_drv_init;
2168 	}
2169 
2170 	macp->m_type_ident = MAC_PLUGIN_IDENT_IB;
2171 	macp->m_driver = state;
2172 	macp->m_dip = state->id_dip;
2173 	macp->m_src_addr = (uint8_t *)&state->id_macaddr;
2174 	macp->m_callbacks = &ib_m_callbacks;
2175 	macp->m_min_sdu = 0;
2176 	macp->m_max_sdu = state->id_mtu - IPOIB_HDRSIZE;
2177 
2178 	/*
2179 	 *  Register ourselves with the GLDv3 interface
2180 	 */
2181 	err = mac_register(macp, &state->id_mh);
2182 	mac_free(macp);
2183 	if (err != 0) {
2184 		DPRINT(10, "ibd_attach : failed in mac_register()");
2185 		goto attach_fail_mac_register;
2186 	}
2187 
2188 	/*
2189 	 * Setup the handler we will use for regular DLPI stuff. Its important
2190 	 * to setup the recv handler after registering with gldv3.
2191 	 */
2192 	ibt_set_cq_handler(state->id_rcq_hdl, ibd_rcq_handler, state);
2193 	if (ibt_enable_cq_notify(state->id_rcq_hdl, IBT_NEXT_COMPLETION) !=
2194 	    IBT_SUCCESS) {
2195 		DPRINT(10, "ibd_attach : failed in ibt_enable_cq_notify()\n");
2196 		goto attach_fail_setup_handler;
2197 	}
2198 
2199 	/*
2200 	 * Setup the subnet notices handler after we initialize the a/mcaches
2201 	 * and start the async thread, both of which are required for the
2202 	 * trap handler to function properly. Enable the trap handler to
2203 	 * queue requests to the async thread after the mac_register, because
2204 	 * the async daemon invokes mac_tx_update(), which must be done after
2205 	 * mac_register().
2206 	 */
2207 	ibt_register_subnet_notices(state->id_ibt_hdl,
2208 	    ibd_snet_notices_handler, state);
2209 	mutex_enter(&state->id_trap_lock);
2210 	state->id_trap_stop = B_FALSE;
2211 	mutex_exit(&state->id_trap_lock);
2212 
2213 	/*
2214 	 * Indicate link status to GLDv3 and higher layers. By default,
2215 	 * we assume we are in up state (which must have been true at
2216 	 * least at the time the broadcast mcg's were probed); if there
2217 	 * were any up/down transitions till the time we come here, the
2218 	 * async handler will have updated last known state, which we
2219 	 * use to tell GLDv3. The async handler will not send any
2220 	 * notifications to GLDv3 till we reach here in the initialization
2221 	 * sequence.
2222 	 */
2223 	mac_link_update(state->id_mh, state->id_link_state);
2224 
2225 	return (DDI_SUCCESS);
2226 
2227 	/* Attach failure points, cleanup */
2228 attach_fail_setup_handler:
2229 	(void) mac_unregister(state->id_mh);
2230 
2231 attach_fail_mac_register:
2232 	ibd_drv_fini(state);
2233 
2234 attach_fail_drv_init:
2235 	if (ibt_detach(state->id_ibt_hdl) != IBT_SUCCESS)
2236 		ibd_print_warn(state, "failed to free IB resources");
2237 
2238 attach_fail_ibt_attach:
2239 	if (ibd_tx_softintr == 1)
2240 		ddi_remove_softintr(state->id_tx);
2241 
2242 attach_fail_ddi_add_tx_softintr:
2243 	if (ibd_rx_softintr == 1)
2244 		ddi_remove_softintr(state->id_rx);
2245 
2246 attach_fail_ddi_add_rx_softintr:
2247 	ibd_state_fini(state);
2248 
2249 attach_fail_state_init:
2250 	ddi_soft_state_free(ibd_list, instance);
2251 
2252 	return (DDI_FAILURE);
2253 }
2254 
2255 /*
2256  * Detach device from the IO framework.
2257  */
2258 static int
2259 ibd_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2260 {
2261 	ibd_state_t *state;
2262 	int status;
2263 	int instance;
2264 
2265 	switch (cmd) {
2266 		case DDI_DETACH:
2267 			break;
2268 		case DDI_SUSPEND:
2269 		default:
2270 			return (DDI_FAILURE);
2271 	}
2272 
2273 	instance = ddi_get_instance(dip);
2274 	state = ddi_get_soft_state(ibd_list, instance);
2275 
2276 	/*
2277 	 * First, stop receive interrupts; this stops the
2278 	 * driver from handing up buffers to higher layers.
2279 	 * Wait for receive buffers to be returned; give up
2280 	 * after 5 seconds.
2281 	 */
2282 	ibt_set_cq_handler(state->id_rcq_hdl, 0, 0);
2283 	status = 50;
2284 	while (state->id_rx_list.dl_bufs_outstanding > 0) {
2285 		delay(drv_usectohz(100000));
2286 		if (--status == 0) {
2287 			DPRINT(2, "ibd_detach : reclaiming failed");
2288 			goto failed;
2289 		}
2290 	}
2291 
2292 	if (mac_unregister(state->id_mh) != DDI_SUCCESS) {
2293 		DPRINT(10, "ibd_detach : failed in mac_unregister()");
2294 		goto failed;
2295 	}
2296 
2297 	if (ibd_rx_softintr == 1)
2298 		ddi_remove_softintr(state->id_rx);
2299 
2300 	if (ibd_tx_softintr == 1)
2301 		ddi_remove_softintr(state->id_tx);
2302 
2303 	ibd_drv_fini(state);
2304 
2305 	if (ibt_detach(state->id_ibt_hdl) != IBT_SUCCESS)
2306 		ibd_print_warn(state, "failed to free all IB resources at "
2307 		    "driver detach time");
2308 
2309 	ibd_state_fini(state);
2310 	ddi_soft_state_free(ibd_list, instance);
2311 	return (DDI_SUCCESS);
2312 
2313 failed:
2314 	/*
2315 	 * Reap all the Tx/Rx completions that were posted since we
2316 	 * turned off the notification. Turn on notifications. There
2317 	 * is a race in that we do not reap completions that come in
2318 	 * after the poll and before notifications get turned on. That
2319 	 * is okay, the next rx/tx packet will trigger a completion
2320 	 * that will reap any missed completions.
2321 	 */
2322 	ibd_poll_compq(state, state->id_rcq_hdl);
2323 	ibt_set_cq_handler(state->id_rcq_hdl, ibd_rcq_handler, state);
2324 	return (DDI_FAILURE);
2325 }
2326 
2327 /*
2328  * Pre ibt_attach() driver initialization
2329  */
2330 static int
2331 ibd_state_init(ibd_state_t *state, dev_info_t *dip)
2332 {
2333 	char buf[64];
2334 
2335 	mutex_init(&state->id_link_mutex, NULL, MUTEX_DRIVER, NULL);
2336 	state->id_link_state = LINK_STATE_UNKNOWN;
2337 
2338 	mutex_init(&state->id_trap_lock, NULL, MUTEX_DRIVER, NULL);
2339 	cv_init(&state->id_trap_cv, NULL, CV_DEFAULT, NULL);
2340 	state->id_trap_stop = B_TRUE;
2341 	state->id_trap_inprog = 0;
2342 
2343 	mutex_init(&state->id_cq_poll_lock, NULL, MUTEX_DRIVER, NULL);
2344 	state->id_dip = dip;
2345 
2346 	mutex_init(&state->id_sched_lock, NULL, MUTEX_DRIVER, NULL);
2347 
2348 	state->id_tx_list.dl_head = NULL;
2349 	state->id_tx_list.dl_tail = NULL;
2350 	state->id_tx_list.dl_pending_sends = B_FALSE;
2351 	state->id_tx_list.dl_cnt = 0;
2352 	mutex_init(&state->id_tx_list.dl_mutex, NULL, MUTEX_DRIVER, NULL);
2353 	mutex_init(&state->id_txpost_lock, NULL, MUTEX_DRIVER, NULL);
2354 	state->id_tx_busy = 0;
2355 
2356 	state->id_rx_list.dl_head = NULL;
2357 	state->id_rx_list.dl_tail = NULL;
2358 	state->id_rx_list.dl_bufs_outstanding = 0;
2359 	state->id_rx_list.dl_cnt = 0;
2360 	mutex_init(&state->id_rx_list.dl_mutex, NULL, MUTEX_DRIVER, NULL);
2361 	mutex_init(&state->id_rxpost_lock, NULL, MUTEX_DRIVER, NULL);
2362 
2363 	(void) sprintf(buf, "ibd_req%d", ddi_get_instance(dip));
2364 	state->id_req_kmc = kmem_cache_create(buf, sizeof (ibd_req_t),
2365 	    0, NULL, NULL, NULL, NULL, NULL, 0);
2366 
2367 #ifdef IBD_LOGGING
2368 	mutex_init(&ibd_lbuf_lock, NULL, MUTEX_DRIVER, NULL);
2369 #endif
2370 
2371 	return (DDI_SUCCESS);
2372 }
2373 
2374 /*
2375  * Post ibt_detach() driver deconstruction
2376  */
2377 static void
2378 ibd_state_fini(ibd_state_t *state)
2379 {
2380 	kmem_cache_destroy(state->id_req_kmc);
2381 
2382 	mutex_destroy(&state->id_rxpost_lock);
2383 	mutex_destroy(&state->id_rx_list.dl_mutex);
2384 
2385 	mutex_destroy(&state->id_txpost_lock);
2386 	mutex_destroy(&state->id_tx_list.dl_mutex);
2387 
2388 	mutex_destroy(&state->id_sched_lock);
2389 	mutex_destroy(&state->id_cq_poll_lock);
2390 
2391 	cv_destroy(&state->id_trap_cv);
2392 	mutex_destroy(&state->id_trap_lock);
2393 	mutex_destroy(&state->id_link_mutex);
2394 
2395 #ifdef IBD_LOGGING
2396 	mutex_destroy(&ibd_lbuf_lock);
2397 #endif
2398 }
2399 
2400 /*
2401  * Fetch IBA parameters for the network device from IB nexus.
2402  */
2403 static int
2404 ibd_get_portpkey(ibd_state_t *state, ib_guid_t *hca_guid)
2405 {
2406 	/*
2407 	 * Get the IBA Pkey ... allow only fullmembers, per IPoIB spec.
2408 	 * Note that the default partition is also allowed.
2409 	 */
2410 	state->id_pkey = ddi_prop_get_int(DDI_DEV_T_ANY, state->id_dip,
2411 	    0, "port-pkey", IB_PKEY_INVALID_LIMITED);
2412 	if (state->id_pkey <= IB_PKEY_INVALID_FULL) {
2413 		DPRINT(10, "ibd_get_portpkey : ERROR: IBport device has wrong"
2414 		    "partition\n");
2415 		return (DDI_FAILURE);
2416 	}
2417 
2418 	/*
2419 	 * ... the IBA port ...
2420 	 */
2421 	state->id_port = ddi_prop_get_int(DDI_DEV_T_ANY, state->id_dip,
2422 	    0, "port-number", 0);
2423 	if (state->id_port == 0) {
2424 		DPRINT(10, "ibd_get_portpkey : ERROR: invalid port number\n");
2425 		return (DDI_FAILURE);
2426 	}
2427 
2428 	/*
2429 	 * ... and HCA GUID.
2430 	 */
2431 	*hca_guid = ddi_prop_get_int64(DDI_DEV_T_ANY, state->id_dip,
2432 	    0, "hca-guid", 0);
2433 	if (*hca_guid == 0) {
2434 		DPRINT(10, "ibd_get_portpkey : ERROR: IBport hca has wrong "
2435 		    "guid\n");
2436 		return (DDI_FAILURE);
2437 	}
2438 
2439 	return (DDI_SUCCESS);
2440 }
2441 
2442 /*
2443  * Fetch link speed from SA for snmp ifspeed reporting.
2444  */
2445 static uint64_t
2446 ibd_get_portspeed(ibd_state_t *state)
2447 {
2448 	int			ret;
2449 	ibt_path_info_t		path;
2450 	ibt_path_attr_t		path_attr;
2451 	uint8_t			num_paths;
2452 	uint64_t		ifspeed;
2453 
2454 	/*
2455 	 * Due to serdes 8b10b encoding on the wire, 2.5 Gbps on wire
2456 	 * translates to 2 Gbps data rate. Thus, 1X single data rate is
2457 	 * 2000000000. Start with that as default.
2458 	 */
2459 	ifspeed = 2000000000;
2460 
2461 	bzero(&path_attr, sizeof (path_attr));
2462 
2463 	/*
2464 	 * Get the port speed from Loopback path information.
2465 	 */
2466 	path_attr.pa_dgids = &state->id_sgid;
2467 	path_attr.pa_num_dgids = 1;
2468 	path_attr.pa_sgid = state->id_sgid;
2469 
2470 	if (ibt_get_paths(state->id_ibt_hdl, IBT_PATH_NO_FLAGS,
2471 	    &path_attr, 1, &path, &num_paths) != IBT_SUCCESS)
2472 		goto earlydone;
2473 
2474 	if (num_paths < 1)
2475 		goto earlydone;
2476 
2477 	/*
2478 	 * In case SA does not return an expected value, report the default
2479 	 * speed as 1X.
2480 	 */
2481 	ret = 1;
2482 	switch (path.pi_prim_cep_path.cep_adds_vect.av_srate) {
2483 		case IBT_SRATE_2:	/*  1X SDR i.e 2.5 Gbps */
2484 			ret = 1;
2485 			break;
2486 		case IBT_SRATE_10:	/*  4X SDR or 1X QDR i.e 10 Gbps */
2487 			ret = 4;
2488 			break;
2489 		case IBT_SRATE_30:	/* 12X SDR i.e 30 Gbps */
2490 			ret = 12;
2491 			break;
2492 		case IBT_SRATE_5:	/*  1X DDR i.e  5 Gbps */
2493 			ret = 2;
2494 			break;
2495 		case IBT_SRATE_20:	/*  4X DDR or 8X SDR i.e 20 Gbps */
2496 			ret = 8;
2497 			break;
2498 		case IBT_SRATE_40:	/*  8X DDR or 4X QDR i.e 40 Gbps */
2499 			ret = 16;
2500 			break;
2501 		case IBT_SRATE_60:	/* 12X DDR i.e 60 Gbps */
2502 			ret = 24;
2503 			break;
2504 		case IBT_SRATE_80:	/*  8X QDR i.e 80 Gbps */
2505 			ret = 32;
2506 			break;
2507 		case IBT_SRATE_120:	/* 12X QDR i.e 120 Gbps */
2508 			ret = 48;
2509 			break;
2510 	}
2511 
2512 	ifspeed *= ret;
2513 
2514 earlydone:
2515 	return (ifspeed);
2516 }
2517 
2518 /*
2519  * Search input mcg list (id_mc_full or id_mc_non) for an entry
2520  * representing the input mcg mgid.
2521  */
2522 static ibd_mce_t *
2523 ibd_mcache_find(ib_gid_t mgid, struct list *mlist)
2524 {
2525 	ibd_mce_t *ptr = list_head(mlist);
2526 
2527 	/*
2528 	 * Do plain linear search.
2529 	 */
2530 	while (ptr != NULL) {
2531 		if (bcmp(&mgid, &ptr->mc_info.mc_adds_vect.av_dgid,
2532 		    sizeof (ib_gid_t)) == 0)
2533 			return (ptr);
2534 		ptr = list_next(mlist, ptr);
2535 	}
2536 	return (NULL);
2537 }
2538 
2539 /*
2540  * Execute IBA JOIN.
2541  */
2542 static ibt_status_t
2543 ibd_iba_join(ibd_state_t *state, ib_gid_t mgid, ibd_mce_t *mce)
2544 {
2545 	ibt_mcg_attr_t mcg_attr;
2546 
2547 	bzero(&mcg_attr, sizeof (ibt_mcg_attr_t));
2548 	mcg_attr.mc_qkey = state->id_mcinfo->mc_qkey;
2549 	mcg_attr.mc_mgid = mgid;
2550 	mcg_attr.mc_join_state = mce->mc_jstate;
2551 	mcg_attr.mc_scope = state->id_scope;
2552 	mcg_attr.mc_pkey = state->id_pkey;
2553 	mcg_attr.mc_flow = state->id_mcinfo->mc_adds_vect.av_flow;
2554 	mcg_attr.mc_sl = state->id_mcinfo->mc_adds_vect.av_srvl;
2555 	mcg_attr.mc_tclass = state->id_mcinfo->mc_adds_vect.av_tclass;
2556 	return (ibt_join_mcg(state->id_sgid, &mcg_attr, &mce->mc_info,
2557 	    NULL, NULL));
2558 }
2559 
2560 /*
2561  * This code JOINs the port in the proper way (depending on the join
2562  * state) so that IBA fabric will forward mcg packets to/from the port.
2563  * It also attaches the QPN to the mcg so it can receive those mcg
2564  * packets. This code makes sure not to attach the mcg to the QP if
2565  * that has been previously done due to the mcg being joined with a
2566  * different join state, even though this is not required by SWG_0216,
2567  * refid 3610.
2568  */
2569 static ibd_mce_t *
2570 ibd_join_group(ibd_state_t *state, ib_gid_t mgid, uint8_t jstate)
2571 {
2572 	ibt_status_t ibt_status;
2573 	ibd_mce_t *mce, *tmce, *omce = NULL;
2574 	boolean_t do_attach = B_TRUE;
2575 
2576 	DPRINT(2, "ibd_join_group : join_group state %d : %016llx:%016llx\n",
2577 	    jstate, mgid.gid_prefix, mgid.gid_guid);
2578 
2579 	/*
2580 	 * For enable_multicast Full member joins, we need to do some
2581 	 * extra work. If there is already an mce on the list that
2582 	 * indicates full membership, that means the membership has
2583 	 * not yet been dropped (since the disable_multicast was issued)
2584 	 * because there are pending Tx's to the mcg; in that case, just
2585 	 * mark the mce not to be reaped when the Tx completion queues
2586 	 * an async reap operation.
2587 	 *
2588 	 * If there is already an mce on the list indicating sendonly
2589 	 * membership, try to promote to full membership. Be careful
2590 	 * not to deallocate the old mce, since there might be an AH
2591 	 * pointing to it; instead, update the old mce with new data
2592 	 * that tracks the full membership.
2593 	 */
2594 	if ((jstate == IB_MC_JSTATE_FULL) && ((omce =
2595 	    IBD_MCACHE_FIND_FULL(state, mgid)) != NULL)) {
2596 		if (omce->mc_jstate == IB_MC_JSTATE_FULL) {
2597 			ASSERT(omce->mc_fullreap);
2598 			omce->mc_fullreap = B_FALSE;
2599 			return (omce);
2600 		} else {
2601 			ASSERT(omce->mc_jstate == IB_MC_JSTATE_SEND_ONLY_NON);
2602 		}
2603 	}
2604 
2605 	/*
2606 	 * Allocate the ibd_mce_t to track this JOIN.
2607 	 */
2608 	mce = kmem_zalloc(sizeof (ibd_mce_t), KM_SLEEP);
2609 	mce->mc_fullreap = B_FALSE;
2610 	mce->mc_jstate = jstate;
2611 
2612 	if ((ibt_status = ibd_iba_join(state, mgid, mce)) != IBT_SUCCESS) {
2613 		DPRINT(10, "ibd_join_group : failed ibt_join_mcg() %d",
2614 		    ibt_status);
2615 		kmem_free(mce, sizeof (ibd_mce_t));
2616 		return (NULL);
2617 	}
2618 
2619 	/*
2620 	 * Is an IBA attach required? Not if the interface is already joined
2621 	 * to the mcg in a different appropriate join state.
2622 	 */
2623 	if (jstate == IB_MC_JSTATE_NON) {
2624 		tmce = IBD_MCACHE_FIND_FULL(state, mgid);
2625 		if ((tmce != NULL) && (tmce->mc_jstate == IB_MC_JSTATE_FULL))
2626 			do_attach = B_FALSE;
2627 	} else if (jstate == IB_MC_JSTATE_FULL) {
2628 		if (IBD_MCACHE_FIND_NON(state, mgid) != NULL)
2629 			do_attach = B_FALSE;
2630 	} else {	/* jstate == IB_MC_JSTATE_SEND_ONLY_NON */
2631 		do_attach = B_FALSE;
2632 	}
2633 
2634 	if (do_attach) {
2635 		/*
2636 		 * Do the IBA attach.
2637 		 */
2638 		DPRINT(10, "ibd_join_group: ibt_attach_mcg \n");
2639 		if ((ibt_status = ibt_attach_mcg(state->id_chnl_hdl,
2640 		    &mce->mc_info)) != IBT_SUCCESS) {
2641 			DPRINT(10, "ibd_join_group : failed qp attachment "
2642 			    "%d\n", ibt_status);
2643 			/*
2644 			 * NOTE that we should probably preserve the join info
2645 			 * in the list and later try to leave again at detach
2646 			 * time.
2647 			 */
2648 			(void) ibt_leave_mcg(state->id_sgid, mgid,
2649 			    state->id_sgid, jstate);
2650 			kmem_free(mce, sizeof (ibd_mce_t));
2651 			return (NULL);
2652 		}
2653 	}
2654 
2655 	/*
2656 	 * Insert the ibd_mce_t in the proper list.
2657 	 */
2658 	if (jstate == IB_MC_JSTATE_NON) {
2659 		IBD_MCACHE_INSERT_NON(state, mce);
2660 	} else {
2661 		/*
2662 		 * Set up the mc_req fields used for reaping the
2663 		 * mcg in case of delayed tx completion (see
2664 		 * ibd_tx_cleanup()). Also done for sendonly join in
2665 		 * case we are promoted to fullmembership later and
2666 		 * keep using the same mce.
2667 		 */
2668 		mce->mc_req.rq_gid = mgid;
2669 		mce->mc_req.rq_ptr = mce;
2670 		/*
2671 		 * Check whether this is the case of trying to join
2672 		 * full member, and we were already joined send only.
2673 		 * We try to drop our SendOnly membership, but it is
2674 		 * possible that the mcg does not exist anymore (and
2675 		 * the subnet trap never reached us), so the leave
2676 		 * operation might fail.
2677 		 */
2678 		if (omce != NULL) {
2679 			(void) ibt_leave_mcg(state->id_sgid, mgid,
2680 			    state->id_sgid, IB_MC_JSTATE_SEND_ONLY_NON);
2681 			omce->mc_jstate = IB_MC_JSTATE_FULL;
2682 			bcopy(&mce->mc_info, &omce->mc_info,
2683 			    sizeof (ibt_mcg_info_t));
2684 			kmem_free(mce, sizeof (ibd_mce_t));
2685 			return (omce);
2686 		}
2687 		mutex_enter(&state->id_mc_mutex);
2688 		IBD_MCACHE_INSERT_FULL(state, mce);
2689 		mutex_exit(&state->id_mc_mutex);
2690 	}
2691 
2692 	return (mce);
2693 }
2694 
2695 /*
2696  * Called during port up event handling to attempt to reacquire full
2697  * membership to an mcg. Stripped down version of ibd_join_group().
2698  * Note that it is possible that the mcg might have gone away, and
2699  * gets recreated at this point.
2700  */
2701 static void
2702 ibd_reacquire_group(ibd_state_t *state, ibd_mce_t *mce)
2703 {
2704 	ib_gid_t mgid;
2705 
2706 	/*
2707 	 * If the mc_fullreap flag is set, or this join fails, a subsequent
2708 	 * reap/leave is going to try to leave the group. We could prevent
2709 	 * that by adding a boolean flag into ibd_mce_t, if required.
2710 	 */
2711 	if (mce->mc_fullreap)
2712 		return;
2713 
2714 	mgid = mce->mc_info.mc_adds_vect.av_dgid;
2715 
2716 	DPRINT(2, "ibd_reacquire_group : %016llx:%016llx\n", mgid.gid_prefix,
2717 	    mgid.gid_guid);
2718 
2719 	if (ibd_iba_join(state, mgid, mce) != IBT_SUCCESS)
2720 		ibd_print_warn(state, "Failure on port up to rejoin "
2721 		    "multicast gid %016llx:%016llx",
2722 		    (u_longlong_t)mgid.gid_prefix,
2723 		    (u_longlong_t)mgid.gid_guid);
2724 }
2725 
2726 /*
2727  * This code handles delayed Tx completion cleanups for mcg's to which
2728  * disable_multicast has been issued, regular mcg related cleanups during
2729  * disable_multicast, disable_promiscous and mcg traps, as well as
2730  * cleanups during driver detach time. Depending on the join state,
2731  * it deletes the mce from the appropriate list and issues the IBA
2732  * leave/detach; except in the disable_multicast case when the mce
2733  * is left on the active list for a subsequent Tx completion cleanup.
2734  */
2735 static void
2736 ibd_async_reap_group(ibd_state_t *state, ibd_mce_t *mce, ib_gid_t mgid,
2737     uint8_t jstate)
2738 {
2739 	ibd_mce_t *tmce;
2740 	boolean_t do_detach = B_TRUE;
2741 
2742 	/*
2743 	 * Before detaching, we must check whether the other list
2744 	 * contains the mcg; if we detach blindly, the consumer
2745 	 * who set up the other list will also stop receiving
2746 	 * traffic.
2747 	 */
2748 	if (jstate == IB_MC_JSTATE_FULL) {
2749 		/*
2750 		 * The following check is only relevant while coming
2751 		 * from the Tx completion path in the reap case.
2752 		 */
2753 		if (!mce->mc_fullreap)
2754 			return;
2755 		mutex_enter(&state->id_mc_mutex);
2756 		IBD_MCACHE_PULLOUT_FULL(state, mce);
2757 		mutex_exit(&state->id_mc_mutex);
2758 		if (IBD_MCACHE_FIND_NON(state, mgid) != NULL)
2759 			do_detach = B_FALSE;
2760 	} else if (jstate == IB_MC_JSTATE_NON) {
2761 		IBD_MCACHE_PULLOUT_NON(state, mce);
2762 		tmce = IBD_MCACHE_FIND_FULL(state, mgid);
2763 		if ((tmce != NULL) && (tmce->mc_jstate == IB_MC_JSTATE_FULL))
2764 			do_detach = B_FALSE;
2765 	} else {	/* jstate == IB_MC_JSTATE_SEND_ONLY_NON */
2766 		mutex_enter(&state->id_mc_mutex);
2767 		IBD_MCACHE_PULLOUT_FULL(state, mce);
2768 		mutex_exit(&state->id_mc_mutex);
2769 		do_detach = B_FALSE;
2770 	}
2771 
2772 	/*
2773 	 * If we are reacting to a mcg trap and leaving our sendonly or
2774 	 * non membership, the mcg is possibly already gone, so attempting
2775 	 * to leave might fail. On the other hand, we must try to leave
2776 	 * anyway, since this might be a trap from long ago, and we could
2777 	 * have potentially sendonly joined to a recent incarnation of
2778 	 * the mcg and are about to loose track of this information.
2779 	 */
2780 	if (do_detach) {
2781 		DPRINT(2, "ibd_async_reap_group : ibt_detach_mcg : "
2782 		    "%016llx:%016llx\n", mgid.gid_prefix, mgid.gid_guid);
2783 		(void) ibt_detach_mcg(state->id_chnl_hdl, &mce->mc_info);
2784 	}
2785 
2786 	(void) ibt_leave_mcg(state->id_sgid, mgid, state->id_sgid, jstate);
2787 	kmem_free(mce, sizeof (ibd_mce_t));
2788 }
2789 
2790 /*
2791  * Async code executed due to multicast and promiscuous disable requests
2792  * and mcg trap handling; also executed during driver detach. Mostly, a
2793  * leave and detach is done; except for the fullmember case when Tx
2794  * requests are pending, whence arrangements are made for subsequent
2795  * cleanup on Tx completion.
2796  */
2797 static void
2798 ibd_leave_group(ibd_state_t *state, ib_gid_t mgid, uint8_t jstate)
2799 {
2800 	ipoib_mac_t mcmac;
2801 	boolean_t recycled;
2802 	ibd_mce_t *mce;
2803 
2804 	DPRINT(2, "ibd_leave_group : leave_group state %d : %016llx:%016llx\n",
2805 	    jstate, mgid.gid_prefix, mgid.gid_guid);
2806 
2807 	if (jstate == IB_MC_JSTATE_NON) {
2808 		recycled = B_TRUE;
2809 		mce = IBD_MCACHE_FIND_NON(state, mgid);
2810 		/*
2811 		 * In case we are handling a mcg trap, we might not find
2812 		 * the mcg in the non list.
2813 		 */
2814 		if (mce == NULL)
2815 			return;
2816 	} else {
2817 		mce = IBD_MCACHE_FIND_FULL(state, mgid);
2818 
2819 		/*
2820 		 * In case we are handling a mcg trap, make sure the trap
2821 		 * is not arriving late; if we have an mce that indicates
2822 		 * that we are already a fullmember, that would be a clear
2823 		 * indication that the trap arrived late (ie, is for a
2824 		 * previous incarnation of the mcg).
2825 		 */
2826 		if (jstate == IB_MC_JSTATE_SEND_ONLY_NON) {
2827 			if ((mce == NULL) || (mce->mc_jstate ==
2828 			    IB_MC_JSTATE_FULL))
2829 				return;
2830 			ASSERT(mce->mc_jstate == IB_MC_JSTATE_SEND_ONLY_NON);
2831 		} else {
2832 			ASSERT(jstate == IB_MC_JSTATE_FULL);
2833 
2834 			/*
2835 			 * If join group failed, mce will be NULL here.
2836 			 * This is because in GLDv3 driver, set multicast
2837 			 *  will always return success.
2838 			 */
2839 			if (mce == NULL)
2840 				return;
2841 
2842 			ASSERT(mce->mc_jstate == IB_MC_JSTATE_FULL);
2843 
2844 			mce->mc_fullreap = B_TRUE;
2845 		}
2846 
2847 		/*
2848 		 * If no pending Tx's remain that reference the AH
2849 		 * for the mcg, recycle it from active to free list.
2850 		 * Else in the IB_MC_JSTATE_FULL case, just mark the AH,
2851 		 * so the last completing Tx will cause an async reap
2852 		 * operation to be invoked, at which time we will drop our
2853 		 * membership to the mcg so that the pending Tx's complete
2854 		 * successfully. Refer to comments on "AH and MCE active
2855 		 * list manipulation" at top of this file. The lock protects
2856 		 * against Tx fast path and Tx cleanup code.
2857 		 */
2858 		mutex_enter(&state->id_ac_mutex);
2859 		ibd_h2n_mac(&mcmac, IB_MC_QPN, mgid.gid_prefix, mgid.gid_guid);
2860 		recycled = ibd_acache_recycle(state, &mcmac, (jstate ==
2861 		    IB_MC_JSTATE_SEND_ONLY_NON));
2862 		mutex_exit(&state->id_ac_mutex);
2863 	}
2864 
2865 	if (recycled) {
2866 		DPRINT(2, "ibd_leave_group : leave_group reaping : "
2867 		    "%016llx:%016llx\n", mgid.gid_prefix, mgid.gid_guid);
2868 		ibd_async_reap_group(state, mce, mgid, jstate);
2869 	}
2870 }
2871 
2872 /*
2873  * Find the broadcast address as defined by IPoIB; implicitly
2874  * determines the IBA scope, mtu, tclass etc of the link the
2875  * interface is going to be a member of.
2876  */
2877 static ibt_status_t
2878 ibd_find_bgroup(ibd_state_t *state)
2879 {
2880 	ibt_mcg_attr_t mcg_attr;
2881 	uint_t numg;
2882 	uchar_t scopes[] = { IB_MC_SCOPE_SUBNET_LOCAL,
2883 	    IB_MC_SCOPE_SITE_LOCAL, IB_MC_SCOPE_ORG_LOCAL,
2884 	    IB_MC_SCOPE_GLOBAL };
2885 	int i, mcgmtu;
2886 	boolean_t found = B_FALSE;
2887 
2888 	bzero(&mcg_attr, sizeof (ibt_mcg_attr_t));
2889 	mcg_attr.mc_pkey = state->id_pkey;
2890 	state->id_mgid.gid_guid = IB_MGID_IPV4_LOWGRP_MASK;
2891 
2892 	for (i = 0; i < sizeof (scopes)/sizeof (scopes[0]); i++) {
2893 		state->id_scope = mcg_attr.mc_scope = scopes[i];
2894 
2895 		/*
2896 		 * Look for the IPoIB broadcast group.
2897 		 */
2898 		state->id_mgid.gid_prefix =
2899 		    (((uint64_t)IB_MCGID_IPV4_PREFIX << 32) |
2900 		    ((uint64_t)state->id_scope << 48) |
2901 		    ((uint32_t)(state->id_pkey << 16)));
2902 		mcg_attr.mc_mgid = state->id_mgid;
2903 		if (ibt_query_mcg(state->id_sgid, &mcg_attr, 1,
2904 		    &state->id_mcinfo, &numg) == IBT_SUCCESS) {
2905 			found = B_TRUE;
2906 			break;
2907 		}
2908 
2909 	}
2910 
2911 	if (!found) {
2912 		ibd_print_warn(state, "IPoIB broadcast group absent");
2913 		return (IBT_FAILURE);
2914 	}
2915 
2916 	/*
2917 	 * Assert that the mcg mtu <= id_mtu. Fill in updated id_mtu.
2918 	 */
2919 	mcgmtu = (128 << state->id_mcinfo->mc_mtu);
2920 	if (state->id_mtu < mcgmtu) {
2921 		ibd_print_warn(state, "IPoIB broadcast group MTU %d "
2922 		    "greater than port's maximum MTU %d", mcgmtu,
2923 		    state->id_mtu);
2924 		return (IBT_FAILURE);
2925 	}
2926 	state->id_mtu = mcgmtu;
2927 
2928 	return (IBT_SUCCESS);
2929 }
2930 
2931 /*
2932  * Post ibt_attach() initialization.
2933  */
2934 static int
2935 ibd_drv_init(ibd_state_t *state)
2936 {
2937 	kthread_t *kht;
2938 	ibt_ud_chan_alloc_args_t ud_alloc_attr;
2939 	ibt_ud_chan_query_attr_t ud_chan_attr;
2940 	ibt_hca_portinfo_t *port_infop;
2941 	ibt_hca_attr_t hca_attrs;
2942 	ibt_status_t ibt_status;
2943 	ibt_cq_attr_t cq_attr;
2944 	ib_guid_t hca_guid;
2945 	uint32_t real_size;
2946 	uint32_t *ptr;
2947 	char pathname[OBP_MAXPATHLEN];
2948 	uint_t psize, port_infosz;
2949 
2950 	/*
2951 	 * Initialize id_port before ibt_open_hca because of
2952 	 * ordering requirements in port up/down handling.
2953 	 */
2954 	if (ibd_get_portpkey(state, &hca_guid) != DDI_SUCCESS)
2955 		return (DDI_FAILURE);
2956 
2957 	if (ibt_open_hca(state->id_ibt_hdl, hca_guid,
2958 	    &state->id_hca_hdl) != IBT_SUCCESS) {
2959 		DPRINT(10, "ibd_drv_init : failed in ibt_open_hca()\n");
2960 		return (DDI_FAILURE);
2961 	}
2962 
2963 	mutex_enter(&state->id_link_mutex);
2964 	ibt_status = ibt_query_hca_ports(state->id_hca_hdl,
2965 	    state->id_port, &port_infop, &psize,
2966 	    &port_infosz);
2967 	if ((ibt_status != IBT_SUCCESS) || (psize != 1)) {
2968 		mutex_exit(&state->id_link_mutex);
2969 		DPRINT(10, "ibd_drv_init : failed in ibt_query_port()\n");
2970 		(void) ibt_close_hca(state->id_hca_hdl);
2971 		return (DDI_FAILURE);
2972 	}
2973 
2974 	/*
2975 	 * If the link already went down by the time we get here, give up;
2976 	 * we can not even get the gid since that is not valid. We would
2977 	 * fail in ibd_find_bgroup() anyway.
2978 	 */
2979 	if (port_infop->p_linkstate != IBT_PORT_ACTIVE) {
2980 		mutex_exit(&state->id_link_mutex);
2981 		ibt_free_portinfo(port_infop, port_infosz);
2982 		(void) ibt_close_hca(state->id_hca_hdl);
2983 		ibd_print_warn(state, "Port is not active");
2984 		return (DDI_FAILURE);
2985 	}
2986 
2987 	/*
2988 	 * This verifies the Pkey ibnexus handed us is still valid.
2989 	 * This is also the point from which the pkey table for the
2990 	 * port must hold the exact pkey value at the exact index
2991 	 * across port up/downs.
2992 	 */
2993 	if (ibt_pkey2index(state->id_hca_hdl, state->id_port,
2994 	    state->id_pkey, &state->id_pkix) != IBT_SUCCESS) {
2995 		mutex_exit(&state->id_link_mutex);
2996 		ibt_free_portinfo(port_infop, port_infosz);
2997 		DPRINT(10, "ibd_drv_init : failed in ibt_pkey2index()\n");
2998 		(void) ibt_close_hca(state->id_hca_hdl);
2999 		return (DDI_FAILURE);
3000 	}
3001 
3002 	state->id_mtu = (128 << port_infop->p_mtu);
3003 	state->id_sgid = *port_infop->p_sgid_tbl;
3004 	state->id_link_state = LINK_STATE_UP;
3005 	mutex_exit(&state->id_link_mutex);
3006 
3007 	ibt_free_portinfo(port_infop, port_infosz);
3008 
3009 	state->id_link_speed = ibd_get_portspeed(state);
3010 
3011 	/*
3012 	 * Read drv conf and record what the policy is on enabling LSO
3013 	 */
3014 	if (ddi_prop_get_int(DDI_DEV_T_ANY, state->id_dip,
3015 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, IBD_PROP_LSO_POLICY, 1)) {
3016 		state->id_lso_policy = B_TRUE;
3017 	} else {
3018 		state->id_lso_policy = B_FALSE;
3019 	}
3020 
3021 	ibt_status = ibt_query_hca(state->id_hca_hdl, &hca_attrs);
3022 	ASSERT(ibt_status == IBT_SUCCESS);
3023 
3024 	if (ibd_find_bgroup(state) != IBT_SUCCESS) {
3025 		DPRINT(10, "ibd_drv_init : failed in ibd_find_bgroup\n");
3026 		goto drv_init_fail_find_bgroup;
3027 	}
3028 
3029 	if (ibt_alloc_pd(state->id_hca_hdl, IBT_PD_NO_FLAGS,
3030 	    &state->id_pd_hdl) != IBT_SUCCESS) {
3031 		DPRINT(10, "ibd_drv_init : failed in ibt_alloc_pd()\n");
3032 		goto drv_init_fail_alloc_pd;
3033 	}
3034 
3035 	/* Initialize the parallel ARP cache and AHs */
3036 	if (ibd_acache_init(state) != DDI_SUCCESS) {
3037 		DPRINT(10, "ibd_drv_init : failed in ibd_acache_init()\n");
3038 		goto drv_init_fail_acache;
3039 	}
3040 
3041 	if ((hca_attrs.hca_flags2 & IBT_HCA2_RES_LKEY) == IBT_HCA2_RES_LKEY) {
3042 		state->id_hca_res_lkey_capab = 1;
3043 		state->id_res_lkey = hca_attrs.hca_reserved_lkey;
3044 	}
3045 
3046 	/*
3047 	 * Check various tunable limits.
3048 	 */
3049 
3050 	/*
3051 	 * See if extended sgl size information is provided by the hca; if yes,
3052 	 * use the correct one and set the maximum sqseg value.
3053 	 */
3054 	if (hca_attrs.hca_flags & IBT_HCA_WQE_SIZE_INFO)
3055 		state->id_max_sqseg = hca_attrs.hca_ud_send_sgl_sz;
3056 	else
3057 		state->id_max_sqseg = hca_attrs.hca_max_sgl;
3058 
3059 	/*
3060 	 * Set LSO capability and maximum length
3061 	 */
3062 	if (hca_attrs.hca_max_lso_size > 0) {
3063 		state->id_lso_capable = B_TRUE;
3064 		if (hca_attrs.hca_max_lso_size > IBD_LSO_MAXLEN)
3065 			state->id_lso_maxlen = IBD_LSO_MAXLEN;
3066 		else
3067 			state->id_lso_maxlen = hca_attrs.hca_max_lso_size;
3068 	} else {
3069 		state->id_lso_capable = B_FALSE;
3070 		state->id_lso_maxlen = 0;
3071 	}
3072 
3073 
3074 	/*
3075 	 * Check #r/s wqes against max channel size.
3076 	 */
3077 	if (hca_attrs.hca_max_chan_sz < IBD_NUM_RWQE)
3078 		state->id_num_rwqe = hca_attrs.hca_max_chan_sz;
3079 	else
3080 		state->id_num_rwqe = IBD_NUM_RWQE;
3081 
3082 	if (hca_attrs.hca_max_chan_sz < IBD_NUM_SWQE)
3083 		state->id_num_swqe = hca_attrs.hca_max_chan_sz;
3084 	else
3085 		state->id_num_swqe = IBD_NUM_SWQE;
3086 
3087 	/*
3088 	 * Check the hardware checksum capability. Currently we only consider
3089 	 * full checksum offload.
3090 	 */
3091 	if ((hca_attrs.hca_flags & IBT_HCA_CKSUM_FULL) == IBT_HCA_CKSUM_FULL) {
3092 		state->id_hwcksum_capab = IBT_HCA_CKSUM_FULL;
3093 	}
3094 
3095 	/*
3096 	 * Allocate Rx/combined CQ:
3097 	 * Theoretically, there is no point in having more than #rwqe
3098 	 * plus #swqe cqe's, except that the CQ will be signalled for
3099 	 * overflow when the last wqe completes, if none of the previous
3100 	 * cqe's have been polled. Thus, we allocate just a few less wqe's
3101 	 * to make sure such overflow does not occur.
3102 	 */
3103 	cq_attr.cq_sched = NULL;
3104 	cq_attr.cq_flags = IBT_CQ_NO_FLAGS;
3105 
3106 	if (ibd_separate_cqs == 1) {
3107 		/*
3108 		 * Allocate Receive CQ.
3109 		 */
3110 		if (hca_attrs.hca_max_cq_sz >= (state->id_num_rwqe + 1)) {
3111 			cq_attr.cq_size = state->id_num_rwqe + 1;
3112 		} else {
3113 			cq_attr.cq_size = hca_attrs.hca_max_cq_sz;
3114 			state->id_num_rwqe = cq_attr.cq_size - 1;
3115 		}
3116 
3117 		if (ibt_alloc_cq(state->id_hca_hdl, &cq_attr,
3118 		    &state->id_rcq_hdl, &real_size) != IBT_SUCCESS) {
3119 			DPRINT(10, "ibd_drv_init : failed in ibt_alloc_cq()\n");
3120 			goto drv_init_fail_alloc_rcq;
3121 		}
3122 
3123 		if (ibt_modify_cq(state->id_rcq_hdl,
3124 		    ibd_rxcomp_count, ibd_rxcomp_usec, 0) != IBT_SUCCESS) {
3125 			DPRINT(10, "ibd_drv_init: Receive CQ interrupt "
3126 			    "moderation failed\n");
3127 		}
3128 
3129 		state->id_rxwcs_size = state->id_num_rwqe + 1;
3130 		state->id_rxwcs = kmem_alloc(sizeof (ibt_wc_t) *
3131 		    state->id_rxwcs_size, KM_SLEEP);
3132 
3133 		/*
3134 		 * Allocate Send CQ.
3135 		 */
3136 		if (hca_attrs.hca_max_cq_sz >= (state->id_num_swqe + 1)) {
3137 			cq_attr.cq_size = state->id_num_swqe + 1;
3138 		} else {
3139 			cq_attr.cq_size = hca_attrs.hca_max_cq_sz;
3140 			state->id_num_swqe = cq_attr.cq_size - 1;
3141 		}
3142 
3143 		if (ibt_alloc_cq(state->id_hca_hdl, &cq_attr,
3144 		    &state->id_scq_hdl, &real_size) != IBT_SUCCESS) {
3145 			DPRINT(10, "ibd_drv_init : failed in ibt_alloc_cq()\n");
3146 			goto drv_init_fail_alloc_scq;
3147 		}
3148 		if (ibt_modify_cq(state->id_scq_hdl,
3149 		    10, 300, 0) != IBT_SUCCESS) {
3150 			DPRINT(10, "ibd_drv_init: Send CQ interrupt "
3151 			    "moderation failed\n");
3152 		}
3153 
3154 		state->id_txwcs_size = state->id_num_swqe + 1;
3155 		state->id_txwcs = kmem_alloc(sizeof (ibt_wc_t) *
3156 		    state->id_txwcs_size, KM_SLEEP);
3157 	} else {
3158 		/*
3159 		 * Allocate combined Send/Receive CQ.
3160 		 */
3161 		if (hca_attrs.hca_max_cq_sz >= (state->id_num_rwqe +
3162 		    state->id_num_swqe + 1)) {
3163 			cq_attr.cq_size = state->id_num_rwqe +
3164 			    state->id_num_swqe + 1;
3165 		} else {
3166 			cq_attr.cq_size = hca_attrs.hca_max_cq_sz;
3167 			state->id_num_rwqe = ((cq_attr.cq_size - 1) *
3168 			    state->id_num_rwqe) / (state->id_num_rwqe +
3169 			    state->id_num_swqe);
3170 			state->id_num_swqe = cq_attr.cq_size - 1 -
3171 			    state->id_num_rwqe;
3172 		}
3173 
3174 		state->id_rxwcs_size = cq_attr.cq_size;
3175 		state->id_txwcs_size = state->id_rxwcs_size;
3176 
3177 		if (ibt_alloc_cq(state->id_hca_hdl, &cq_attr,
3178 		    &state->id_rcq_hdl, &real_size) != IBT_SUCCESS) {
3179 			DPRINT(10, "ibd_drv_init : failed in ibt_alloc_cq()\n");
3180 			goto drv_init_fail_alloc_rcq;
3181 		}
3182 		state->id_scq_hdl = state->id_rcq_hdl;
3183 		state->id_rxwcs = kmem_alloc(sizeof (ibt_wc_t) *
3184 		    state->id_rxwcs_size, KM_SLEEP);
3185 		state->id_txwcs = state->id_rxwcs;
3186 	}
3187 
3188 	/*
3189 	 * Print message in case we could not allocate as many wqe's
3190 	 * as was requested. Note that in the combined CQ case, we will
3191 	 * get the following message.
3192 	 */
3193 	if (state->id_num_rwqe != IBD_NUM_RWQE)
3194 		ibd_print_warn(state, "Setting #rwqe = %d instead of default "
3195 		    "%d", state->id_num_rwqe, IBD_NUM_RWQE);
3196 	if (state->id_num_swqe != IBD_NUM_SWQE)
3197 		ibd_print_warn(state, "Setting #swqe = %d instead of default "
3198 		    "%d", state->id_num_swqe, IBD_NUM_SWQE);
3199 
3200 	ud_alloc_attr.ud_flags  = IBT_WR_SIGNALED;
3201 	if (state->id_hca_res_lkey_capab)
3202 		ud_alloc_attr.ud_flags |= IBT_FAST_REG_RES_LKEY;
3203 	if (state->id_lso_policy && state->id_lso_capable)
3204 		ud_alloc_attr.ud_flags |= IBT_USES_LSO;
3205 
3206 	ud_alloc_attr.ud_hca_port_num	= state->id_port;
3207 	ud_alloc_attr.ud_sizes.cs_sq_sgl = state->id_max_sqseg;
3208 	ud_alloc_attr.ud_sizes.cs_rq_sgl = IBD_MAX_RQSEG;
3209 	ud_alloc_attr.ud_sizes.cs_sq	= state->id_num_swqe;
3210 	ud_alloc_attr.ud_sizes.cs_rq	= state->id_num_rwqe;
3211 	ud_alloc_attr.ud_qkey		= state->id_mcinfo->mc_qkey;
3212 	ud_alloc_attr.ud_scq		= state->id_scq_hdl;
3213 	ud_alloc_attr.ud_rcq		= state->id_rcq_hdl;
3214 	ud_alloc_attr.ud_pd		= state->id_pd_hdl;
3215 	ud_alloc_attr.ud_pkey_ix	= state->id_pkix;
3216 	ud_alloc_attr.ud_clone_chan	= NULL;
3217 
3218 	if (ibt_alloc_ud_channel(state->id_hca_hdl, IBT_ACHAN_NO_FLAGS,
3219 	    &ud_alloc_attr, &state->id_chnl_hdl, NULL) != IBT_SUCCESS) {
3220 		DPRINT(10, "ibd_drv_init : failed in ibt_alloc_ud_channel()"
3221 		    "\n");
3222 		goto drv_init_fail_alloc_chan;
3223 	}
3224 
3225 	if (ibt_query_ud_channel(state->id_chnl_hdl, &ud_chan_attr) !=
3226 	    DDI_SUCCESS) {
3227 		DPRINT(10, "ibd_drv_init : failed in ibt_query_ud_channel()");
3228 		goto drv_init_fail_query_chan;
3229 	}
3230 
3231 	state->id_qpnum = ud_chan_attr.ud_qpn;
3232 	/* state->id_max_sqseg = ud_chan_attr.ud_chan_sizes.cs_sq_sgl; */
3233 
3234 	if (state->id_max_sqseg > IBD_MAX_SQSEG) {
3235 		state->id_max_sqseg = IBD_MAX_SQSEG;
3236 	} else if (state->id_max_sqseg < IBD_MAX_SQSEG) {
3237 		ibd_print_warn(state, "Set #sgl = %d instead of default %d",
3238 		    state->id_max_sqseg, IBD_MAX_SQSEG);
3239 	}
3240 
3241 	/* Initialize the Transmit buffer list */
3242 	if (ibd_init_txlist(state) != DDI_SUCCESS) {
3243 		DPRINT(10, "ibd_drv_init : failed in ibd_init_txlist()\n");
3244 		goto drv_init_fail_txlist_init;
3245 	}
3246 
3247 	if ((ibd_separate_cqs == 1) && (ibd_txcomp_poll == 0)) {
3248 		/*
3249 		 * Setup the handler we will use for regular DLPI stuff
3250 		 */
3251 		ibt_set_cq_handler(state->id_scq_hdl, ibd_scq_handler, state);
3252 		if (ibt_enable_cq_notify(state->id_scq_hdl,
3253 		    IBT_NEXT_COMPLETION) != IBT_SUCCESS) {
3254 			DPRINT(10, "ibd_drv_init : failed in"
3255 			    " ibt_enable_cq_notify()\n");
3256 			goto drv_init_fail_cq_notify;
3257 		}
3258 	}
3259 
3260 	/* Initialize the Receive buffer list */
3261 	if (ibd_init_rxlist(state) != DDI_SUCCESS) {
3262 		DPRINT(10, "ibd_drv_init : failed in ibd_init_rxlist()\n");
3263 		goto drv_init_fail_rxlist_init;
3264 	}
3265 
3266 	/* Join to IPoIB broadcast group as required by IPoIB */
3267 	if (ibd_join_group(state, state->id_mgid, IB_MC_JSTATE_FULL) == NULL) {
3268 		DPRINT(10, "ibd_drv_init : failed in ibd_join_group\n");
3269 		goto drv_init_fail_join_group;
3270 	}
3271 
3272 	/*
3273 	 * Create the async thread; thread_create never fails.
3274 	 */
3275 	kht = thread_create(NULL, 0, ibd_async_work, state, 0, &p0,
3276 	    TS_RUN, minclsyspri);
3277 
3278 	state->id_async_thrid = kht->t_did;
3279 
3280 	/*
3281 	 * The local mac address is now known. Create the IPoIB
3282 	 * address.
3283 	 */
3284 	ibd_h2n_mac(&state->id_macaddr, state->id_qpnum,
3285 	    state->id_sgid.gid_prefix, state->id_sgid.gid_guid);
3286 	/*
3287 	 * Similarly, program in the broadcast mac address.
3288 	 */
3289 	ibd_h2n_mac(&state->id_bcaddr, IB_QPN_MASK, state->id_mgid.gid_prefix,
3290 	    state->id_mgid.gid_guid);
3291 
3292 	ptr = (uint32_t *)&state->id_macaddr;
3293 	DPRINT(10, "ibd_drv_init : INFO: MAC %08X:%08X:%08X:%08X:%08X\n",
3294 	    *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4));
3295 	ptr = (uint32_t *)&state->id_bcaddr;
3296 	DPRINT(10, "ibd_drv_init : INFO: BCMAC %08X:%08X:%08X:%08X:%08X\n",
3297 	    *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4));
3298 	DPRINT(10, "ibd_drv_init : INFO: Pkey 0x%x, Mgid %016llx%016llx\n",
3299 	    state->id_pkey, state->id_mgid.gid_prefix,
3300 	    state->id_mgid.gid_guid);
3301 	DPRINT(10, "ibd_drv_init : INFO: GID %016llx%016llx\n",
3302 	    state->id_sgid.gid_prefix, state->id_sgid.gid_guid);
3303 	DPRINT(10, "ibd_drv_init : INFO: PKEY %04x\n", state->id_pkey);
3304 	DPRINT(10, "ibd_drv_init : INFO: MTU %d\n", state->id_mtu);
3305 	(void) ddi_pathname(state->id_dip, pathname);
3306 	DPRINT(10, "ibd_drv_init : INFO: Pathname %s\n", pathname);
3307 
3308 	return (DDI_SUCCESS);
3309 
3310 drv_init_fail_join_group:
3311 	ibd_fini_rxlist(state);
3312 
3313 drv_init_fail_rxlist_init:
3314 drv_init_fail_cq_notify:
3315 	ibd_fini_txlist(state);
3316 
3317 drv_init_fail_txlist_init:
3318 drv_init_fail_query_chan:
3319 	if (ibt_free_channel(state->id_chnl_hdl) != IBT_SUCCESS)
3320 		DPRINT(10, "ibd_drv_init : failed in ibt_free_channel()");
3321 
3322 drv_init_fail_alloc_chan:
3323 	if ((ibd_separate_cqs == 1) && (ibt_free_cq(state->id_scq_hdl) !=
3324 	    IBT_SUCCESS))
3325 		DPRINT(10, "ibd_drv_init : Tx ibt_free_cq()");
3326 
3327 	if (ibd_separate_cqs == 1)
3328 		kmem_free(state->id_txwcs, sizeof (ibt_wc_t) *
3329 		    state->id_txwcs_size);
3330 
3331 drv_init_fail_alloc_scq:
3332 	if (ibt_free_cq(state->id_rcq_hdl) != IBT_SUCCESS)
3333 		DPRINT(10, "ibd_drv_init : Rx ibt_free_cq()");
3334 	kmem_free(state->id_rxwcs, sizeof (ibt_wc_t) * state->id_rxwcs_size);
3335 
3336 drv_init_fail_alloc_rcq:
3337 	ibd_acache_fini(state);
3338 drv_init_fail_acache:
3339 	if (ibt_free_pd(state->id_hca_hdl, state->id_pd_hdl) != IBT_SUCCESS)
3340 		DPRINT(10, "ibd_drv_init : failed in ibt_free_pd()");
3341 
3342 drv_init_fail_alloc_pd:
3343 	ibt_free_mcg_info(state->id_mcinfo, 1);
3344 drv_init_fail_find_bgroup:
3345 	if (ibt_close_hca(state->id_hca_hdl) != IBT_SUCCESS)
3346 		DPRINT(10, "ibd_drv_init : failed in ibt_close_hca()");
3347 
3348 	return (DDI_FAILURE);
3349 }
3350 
3351 
3352 static int
3353 ibd_alloc_tx_copybufs(ibd_state_t *state)
3354 {
3355 	ibt_mr_attr_t mem_attr;
3356 
3357 	/*
3358 	 * Allocate one big chunk for all regular tx copy bufs
3359 	 */
3360 	state->id_tx_buf_sz = state->id_mtu;
3361 	if (state->id_lso_policy && state->id_lso_capable &&
3362 	    (IBD_TX_BUF_SZ > state->id_mtu)) {
3363 		state->id_tx_buf_sz = IBD_TX_BUF_SZ;
3364 	}
3365 
3366 	state->id_tx_bufs = kmem_zalloc(state->id_num_swqe *
3367 	    state->id_tx_buf_sz, KM_SLEEP);
3368 
3369 	/*
3370 	 * Do one memory registration on the entire txbuf area
3371 	 */
3372 	mem_attr.mr_vaddr = (uint64_t)(uintptr_t)state->id_tx_bufs;
3373 	mem_attr.mr_len = state->id_num_swqe * state->id_tx_buf_sz;
3374 	mem_attr.mr_as = NULL;
3375 	mem_attr.mr_flags = IBT_MR_SLEEP;
3376 	if (ibt_register_mr(state->id_hca_hdl, state->id_pd_hdl, &mem_attr,
3377 	    &state->id_tx_mr_hdl, &state->id_tx_mr_desc) != IBT_SUCCESS) {
3378 		DPRINT(10, "ibd_alloc_tx_copybufs: ibt_register_mr failed");
3379 		kmem_free(state->id_tx_bufs,
3380 		    state->id_num_swqe * state->id_tx_buf_sz);
3381 		state->id_tx_bufs = NULL;
3382 		return (DDI_FAILURE);
3383 	}
3384 
3385 	return (DDI_SUCCESS);
3386 }
3387 
3388 static int
3389 ibd_alloc_tx_lsobufs(ibd_state_t *state)
3390 {
3391 	ibt_mr_attr_t mem_attr;
3392 	ibd_lsobuf_t *buflist;
3393 	ibd_lsobuf_t *lbufp;
3394 	ibd_lsobuf_t *tail;
3395 	ibd_lsobkt_t *bktp;
3396 	uint8_t *membase;
3397 	uint8_t *memp;
3398 	uint_t memsz;
3399 	int i;
3400 
3401 	/*
3402 	 * Allocate the lso bucket
3403 	 */
3404 	bktp = kmem_zalloc(sizeof (ibd_lsobkt_t), KM_SLEEP);
3405 
3406 	/*
3407 	 * Allocate the entire lso memory and register it
3408 	 */
3409 	memsz = IBD_NUM_LSO_BUFS * IBD_LSO_BUFSZ;
3410 	membase = kmem_zalloc(memsz, KM_SLEEP);
3411 
3412 	mem_attr.mr_vaddr = (uint64_t)(uintptr_t)membase;
3413 	mem_attr.mr_len = memsz;
3414 	mem_attr.mr_as = NULL;
3415 	mem_attr.mr_flags = IBT_MR_SLEEP;
3416 	if (ibt_register_mr(state->id_hca_hdl, state->id_pd_hdl,
3417 	    &mem_attr, &bktp->bkt_mr_hdl, &bktp->bkt_mr_desc) != IBT_SUCCESS) {
3418 		DPRINT(10, "ibd_alloc_tx_lsobufs: ibt_register_mr failed");
3419 		kmem_free(membase, memsz);
3420 		kmem_free(bktp, sizeof (ibd_lsobkt_t));
3421 		return (DDI_FAILURE);
3422 	}
3423 
3424 	/*
3425 	 * Now allocate the buflist.  Note that the elements in the buflist and
3426 	 * the buffers in the lso memory have a permanent 1-1 relation, so we
3427 	 * can always derive the address of a buflist entry from the address of
3428 	 * an lso buffer.
3429 	 */
3430 	buflist = kmem_zalloc(IBD_NUM_LSO_BUFS * sizeof (ibd_lsobuf_t),
3431 	    KM_SLEEP);
3432 
3433 	/*
3434 	 * Set up the lso buf chain
3435 	 */
3436 	memp = membase;
3437 	lbufp = buflist;
3438 	for (i = 0; i < IBD_NUM_LSO_BUFS; i++) {
3439 		lbufp->lb_isfree = 1;
3440 		lbufp->lb_buf = memp;
3441 		lbufp->lb_next = lbufp + 1;
3442 
3443 		tail = lbufp;
3444 
3445 		memp += IBD_LSO_BUFSZ;
3446 		lbufp++;
3447 	}
3448 	tail->lb_next = NULL;
3449 
3450 	/*
3451 	 * Set up the LSO buffer information in ibd state
3452 	 */
3453 	bktp->bkt_bufl = buflist;
3454 	bktp->bkt_free_head = buflist;
3455 	bktp->bkt_mem = membase;
3456 	bktp->bkt_nelem = IBD_NUM_LSO_BUFS;
3457 	bktp->bkt_nfree = bktp->bkt_nelem;
3458 
3459 	state->id_lso = bktp;
3460 
3461 	return (DDI_SUCCESS);
3462 }
3463 
3464 /*
3465  * Statically allocate Tx buffer list(s).
3466  */
3467 static int
3468 ibd_init_txlist(ibd_state_t *state)
3469 {
3470 	ibd_swqe_t *swqe;
3471 	ibt_lkey_t lkey;
3472 	int i;
3473 
3474 	if (ibd_alloc_tx_copybufs(state) != DDI_SUCCESS)
3475 		return (DDI_FAILURE);
3476 
3477 	if (state->id_lso_policy && state->id_lso_capable) {
3478 		if (ibd_alloc_tx_lsobufs(state) != DDI_SUCCESS)
3479 			state->id_lso_policy = B_FALSE;
3480 	}
3481 
3482 	/*
3483 	 * Allocate and setup the swqe list
3484 	 */
3485 	lkey = state->id_tx_mr_desc.md_lkey;
3486 	for (i = 0; i < state->id_num_swqe; i++) {
3487 		if (ibd_alloc_swqe(state, &swqe, i, lkey) != DDI_SUCCESS) {
3488 			DPRINT(10, "ibd_init_txlist: ibd_alloc_swqe failed");
3489 			ibd_fini_txlist(state);
3490 			return (DDI_FAILURE);
3491 		}
3492 
3493 		/* add to list */
3494 		state->id_tx_list.dl_cnt++;
3495 		if (state->id_tx_list.dl_head == NULL) {
3496 			swqe->swqe_prev = NULL;
3497 			swqe->swqe_next = NULL;
3498 			state->id_tx_list.dl_head = SWQE_TO_WQE(swqe);
3499 			state->id_tx_list.dl_tail = SWQE_TO_WQE(swqe);
3500 		} else {
3501 			swqe->swqe_prev = state->id_tx_list.dl_tail;
3502 			swqe->swqe_next = NULL;
3503 			state->id_tx_list.dl_tail->w_next = SWQE_TO_WQE(swqe);
3504 			state->id_tx_list.dl_tail = SWQE_TO_WQE(swqe);
3505 		}
3506 	}
3507 
3508 	return (DDI_SUCCESS);
3509 }
3510 
3511 static int
3512 ibd_acquire_lsobufs(ibd_state_t *state, uint_t req_sz, ibt_wr_ds_t *sgl_p,
3513     uint32_t *nds_p)
3514 {
3515 	ibd_lsobkt_t *bktp;
3516 	ibd_lsobuf_t *lbufp;
3517 	ibd_lsobuf_t *nextp;
3518 	ibt_lkey_t lso_lkey;
3519 	uint_t frag_sz;
3520 	uint_t num_needed;
3521 	int i;
3522 
3523 	ASSERT(sgl_p != NULL);
3524 	ASSERT(nds_p != NULL);
3525 	ASSERT(req_sz != 0);
3526 
3527 	/*
3528 	 * Determine how many bufs we'd need for the size requested
3529 	 */
3530 	num_needed = req_sz / IBD_LSO_BUFSZ;
3531 	if ((frag_sz = req_sz % IBD_LSO_BUFSZ) != 0)
3532 		num_needed++;
3533 
3534 	mutex_enter(&state->id_lso_lock);
3535 
3536 	/*
3537 	 * If we don't have enough lso bufs, return failure
3538 	 */
3539 	ASSERT(state->id_lso != NULL);
3540 	bktp = state->id_lso;
3541 	if (bktp->bkt_nfree < num_needed) {
3542 		mutex_exit(&state->id_lso_lock);
3543 		return (-1);
3544 	}
3545 
3546 	/*
3547 	 * Pick the first 'num_needed' bufs from the free list
3548 	 */
3549 	lso_lkey = bktp->bkt_mr_desc.md_lkey;
3550 	lbufp = bktp->bkt_free_head;
3551 	for (i = 0; i < num_needed; i++) {
3552 		ASSERT(lbufp->lb_isfree != 0);
3553 		ASSERT(lbufp->lb_buf != NULL);
3554 
3555 		nextp = lbufp->lb_next;
3556 
3557 		sgl_p[i].ds_va = (ib_vaddr_t)(uintptr_t)lbufp->lb_buf;
3558 		sgl_p[i].ds_key = lso_lkey;
3559 		sgl_p[i].ds_len = IBD_LSO_BUFSZ;
3560 
3561 		lbufp->lb_isfree = 0;
3562 		lbufp->lb_next = NULL;
3563 
3564 		lbufp = nextp;
3565 	}
3566 	bktp->bkt_free_head = lbufp;
3567 
3568 	/*
3569 	 * If the requested size is not a multiple of IBD_LSO_BUFSZ, we need
3570 	 * to adjust the last sgl entry's length. Since we know we need atleast
3571 	 * one, the i-1 use below is ok.
3572 	 */
3573 	if (frag_sz) {
3574 		sgl_p[i-1].ds_len = frag_sz;
3575 	}
3576 
3577 	/*
3578 	 * Update nfree count and return
3579 	 */
3580 	bktp->bkt_nfree -= num_needed;
3581 
3582 	mutex_exit(&state->id_lso_lock);
3583 
3584 	*nds_p = num_needed;
3585 
3586 	return (0);
3587 }
3588 
3589 static void
3590 ibd_release_lsobufs(ibd_state_t *state, ibt_wr_ds_t *sgl_p, uint32_t nds)
3591 {
3592 	ibd_lsobkt_t *bktp;
3593 	ibd_lsobuf_t *lbufp;
3594 	uint8_t *lso_mem_end;
3595 	uint_t ndx;
3596 	int i;
3597 
3598 	mutex_enter(&state->id_lso_lock);
3599 
3600 	bktp = state->id_lso;
3601 	ASSERT(bktp != NULL);
3602 
3603 	lso_mem_end = bktp->bkt_mem + bktp->bkt_nelem * IBD_LSO_BUFSZ;
3604 	for (i = 0; i < nds; i++) {
3605 		uint8_t *va;
3606 
3607 		va = (uint8_t *)(uintptr_t)sgl_p[i].ds_va;
3608 		ASSERT(va >= bktp->bkt_mem && va < lso_mem_end);
3609 
3610 		/*
3611 		 * Figure out the buflist element this sgl buffer corresponds
3612 		 * to and put it back at the head
3613 		 */
3614 		ndx = (va - bktp->bkt_mem) / IBD_LSO_BUFSZ;
3615 		lbufp = bktp->bkt_bufl + ndx;
3616 
3617 		ASSERT(lbufp->lb_isfree == 0);
3618 		ASSERT(lbufp->lb_buf == va);
3619 
3620 		lbufp->lb_isfree = 1;
3621 		lbufp->lb_next = bktp->bkt_free_head;
3622 		bktp->bkt_free_head = lbufp;
3623 	}
3624 	bktp->bkt_nfree += nds;
3625 
3626 	mutex_exit(&state->id_lso_lock);
3627 }
3628 
3629 static void
3630 ibd_free_tx_copybufs(ibd_state_t *state)
3631 {
3632 	/*
3633 	 * Unregister txbuf mr
3634 	 */
3635 	if (ibt_deregister_mr(state->id_hca_hdl,
3636 	    state->id_tx_mr_hdl) != IBT_SUCCESS) {
3637 		DPRINT(10, "ibd_free_tx_copybufs: ibt_deregister_mr failed");
3638 	}
3639 	state->id_tx_mr_hdl = NULL;
3640 
3641 	/*
3642 	 * Free txbuf memory
3643 	 */
3644 	kmem_free(state->id_tx_bufs, state->id_num_swqe * state->id_tx_buf_sz);
3645 	state->id_tx_bufs = NULL;
3646 }
3647 
3648 static void
3649 ibd_free_tx_lsobufs(ibd_state_t *state)
3650 {
3651 	ibd_lsobkt_t *bktp;
3652 
3653 	mutex_enter(&state->id_lso_lock);
3654 
3655 	if ((bktp = state->id_lso) == NULL) {
3656 		mutex_exit(&state->id_lso_lock);
3657 		return;
3658 	}
3659 
3660 	/*
3661 	 * First, free the buflist
3662 	 */
3663 	ASSERT(bktp->bkt_bufl != NULL);
3664 	kmem_free(bktp->bkt_bufl, bktp->bkt_nelem * sizeof (ibd_lsobuf_t));
3665 
3666 	/*
3667 	 * Unregister the LSO memory and free it
3668 	 */
3669 	ASSERT(bktp->bkt_mr_hdl != NULL);
3670 	if (ibt_deregister_mr(state->id_hca_hdl,
3671 	    bktp->bkt_mr_hdl) != IBT_SUCCESS) {
3672 		DPRINT(10,
3673 		    "ibd_free_lsobufs: ibt_deregister_mr failed");
3674 	}
3675 	ASSERT(bktp->bkt_mem);
3676 	kmem_free(bktp->bkt_mem, bktp->bkt_nelem * IBD_LSO_BUFSZ);
3677 
3678 	/*
3679 	 * Finally free the bucket
3680 	 */
3681 	kmem_free(bktp, sizeof (ibd_lsobkt_t));
3682 	state->id_lso = NULL;
3683 
3684 	mutex_exit(&state->id_lso_lock);
3685 }
3686 
3687 /*
3688  * Free the statically allocated Tx buffer list.
3689  */
3690 static void
3691 ibd_fini_txlist(ibd_state_t *state)
3692 {
3693 	ibd_swqe_t *node;
3694 
3695 	/*
3696 	 * Free the allocated swqes
3697 	 */
3698 	mutex_enter(&state->id_tx_list.dl_mutex);
3699 	while (state->id_tx_list.dl_head != NULL) {
3700 		node = WQE_TO_SWQE(state->id_tx_list.dl_head);
3701 		state->id_tx_list.dl_head = node->swqe_next;
3702 		state->id_tx_list.dl_cnt--;
3703 		ASSERT(state->id_tx_list.dl_cnt >= 0);
3704 		ibd_free_swqe(state, node);
3705 	}
3706 	mutex_exit(&state->id_tx_list.dl_mutex);
3707 
3708 	ibd_free_tx_lsobufs(state);
3709 	ibd_free_tx_copybufs(state);
3710 }
3711 
3712 /*
3713  * Allocate a single send wqe and register it so it is almost
3714  * ready to be posted to the hardware.
3715  */
3716 static int
3717 ibd_alloc_swqe(ibd_state_t *state, ibd_swqe_t **wqe, int ndx, ibt_lkey_t lkey)
3718 {
3719 	ibd_swqe_t *swqe;
3720 
3721 	swqe = kmem_zalloc(sizeof (ibd_swqe_t), KM_SLEEP);
3722 	*wqe = swqe;
3723 
3724 	swqe->swqe_type = IBD_WQE_SEND;
3725 	swqe->swqe_next = NULL;
3726 	swqe->swqe_prev = NULL;
3727 	swqe->swqe_im_mblk = NULL;
3728 
3729 	swqe->swqe_copybuf.ic_sgl.ds_va = (ib_vaddr_t)(uintptr_t)
3730 	    (state->id_tx_bufs + ndx * state->id_tx_buf_sz);
3731 	swqe->swqe_copybuf.ic_sgl.ds_key = lkey;
3732 	swqe->swqe_copybuf.ic_sgl.ds_len = 0; /* set in send */
3733 
3734 	swqe->w_swr.wr_id = (ibt_wrid_t)(uintptr_t)swqe;
3735 	swqe->w_swr.wr_flags = IBT_WR_SEND_SIGNAL;
3736 	swqe->w_swr.wr_trans = IBT_UD_SRV;
3737 
3738 	/* These are set in send */
3739 	swqe->w_swr.wr_nds = 0;
3740 	swqe->w_swr.wr_sgl = NULL;
3741 	swqe->w_swr.wr_opcode = IBT_WRC_SEND;
3742 
3743 	return (DDI_SUCCESS);
3744 }
3745 
3746 /*
3747  * Free an allocated send wqe.
3748  */
3749 /*ARGSUSED*/
3750 static void
3751 ibd_free_swqe(ibd_state_t *state, ibd_swqe_t *swqe)
3752 {
3753 	kmem_free(swqe, sizeof (ibd_swqe_t));
3754 }
3755 
3756 /*
3757  * Post a rwqe to the hardware and add it to the Rx list. The
3758  * "recycle" parameter indicates whether an old rwqe is being
3759  * recycled, or this is a new one.
3760  */
3761 static int
3762 ibd_post_rwqe(ibd_state_t *state, ibd_rwqe_t *rwqe, boolean_t recycle)
3763 {
3764 	ibt_status_t ibt_status;
3765 
3766 	if (recycle == B_FALSE) {
3767 		mutex_enter(&state->id_rx_list.dl_mutex);
3768 		if (state->id_rx_list.dl_head == NULL) {
3769 			rwqe->rwqe_prev = NULL;
3770 			rwqe->rwqe_next = NULL;
3771 			state->id_rx_list.dl_head = RWQE_TO_WQE(rwqe);
3772 			state->id_rx_list.dl_tail = RWQE_TO_WQE(rwqe);
3773 		} else {
3774 			rwqe->rwqe_prev = state->id_rx_list.dl_tail;
3775 			rwqe->rwqe_next = NULL;
3776 			state->id_rx_list.dl_tail->w_next = RWQE_TO_WQE(rwqe);
3777 			state->id_rx_list.dl_tail = RWQE_TO_WQE(rwqe);
3778 		}
3779 		mutex_exit(&state->id_rx_list.dl_mutex);
3780 	}
3781 
3782 	mutex_enter(&state->id_rxpost_lock);
3783 	if (state->id_rx_busy) {
3784 		rwqe->w_post_link = NULL;
3785 		if (state->id_rx_head)
3786 			*(state->id_rx_tailp) = (ibd_wqe_t *)rwqe;
3787 		else
3788 			state->id_rx_head = rwqe;
3789 		state->id_rx_tailp = &(rwqe->w_post_link);
3790 	} else {
3791 		state->id_rx_busy = 1;
3792 		do {
3793 			mutex_exit(&state->id_rxpost_lock);
3794 
3795 			/*
3796 			 * Here we should add dl_cnt before post recv, because
3797 			 * we would have to make sure dl_cnt is updated before
3798 			 * the corresponding ibd_process_rx() is called.
3799 			 */
3800 			atomic_add_32(&state->id_rx_list.dl_cnt, 1);
3801 
3802 			ibt_status = ibt_post_recv(state->id_chnl_hdl,
3803 			    &rwqe->w_rwr, 1, NULL);
3804 			if (ibt_status != IBT_SUCCESS) {
3805 				(void) atomic_add_32_nv(
3806 				    &state->id_rx_list.dl_cnt, -1);
3807 				ibd_print_warn(state, "ibd_post_rwqe: "
3808 				    "posting failed, ret=%d", ibt_status);
3809 				return (DDI_FAILURE);
3810 			}
3811 
3812 			mutex_enter(&state->id_rxpost_lock);
3813 			rwqe = state->id_rx_head;
3814 			if (rwqe) {
3815 				state->id_rx_head =
3816 				    (ibd_rwqe_t *)(rwqe->w_post_link);
3817 			}
3818 		} while (rwqe);
3819 		state->id_rx_busy = 0;
3820 	}
3821 	mutex_exit(&state->id_rxpost_lock);
3822 
3823 	return (DDI_SUCCESS);
3824 }
3825 
3826 /*
3827  * Allocate the statically allocated Rx buffer list.
3828  */
3829 static int
3830 ibd_init_rxlist(ibd_state_t *state)
3831 {
3832 	ibd_rwqe_t *rwqe;
3833 	int i;
3834 
3835 	for (i = 0; i < state->id_num_rwqe; i++) {
3836 		if (ibd_alloc_rwqe(state, &rwqe) != DDI_SUCCESS) {
3837 			ibd_fini_rxlist(state);
3838 			return (DDI_FAILURE);
3839 		}
3840 
3841 		if (ibd_post_rwqe(state, rwqe, B_FALSE) == DDI_FAILURE) {
3842 			ibd_free_rwqe(state, rwqe);
3843 			ibd_fini_rxlist(state);
3844 			return (DDI_FAILURE);
3845 		}
3846 	}
3847 
3848 	return (DDI_SUCCESS);
3849 }
3850 
3851 /*
3852  * Free the statically allocated Rx buffer list.
3853  *
3854  */
3855 static void
3856 ibd_fini_rxlist(ibd_state_t *state)
3857 {
3858 	ibd_rwqe_t *node;
3859 
3860 	mutex_enter(&state->id_rx_list.dl_mutex);
3861 	while (state->id_rx_list.dl_head != NULL) {
3862 		node = WQE_TO_RWQE(state->id_rx_list.dl_head);
3863 		state->id_rx_list.dl_head = state->id_rx_list.dl_head->w_next;
3864 		state->id_rx_list.dl_cnt--;
3865 		ASSERT(state->id_rx_list.dl_cnt >= 0);
3866 
3867 		ibd_free_rwqe(state, node);
3868 	}
3869 	mutex_exit(&state->id_rx_list.dl_mutex);
3870 }
3871 
3872 /*
3873  * Allocate a single recv wqe and register it so it is almost
3874  * ready to be posted to the hardware.
3875  */
3876 static int
3877 ibd_alloc_rwqe(ibd_state_t *state, ibd_rwqe_t **wqe)
3878 {
3879 	ibt_mr_attr_t mem_attr;
3880 	ibd_rwqe_t *rwqe;
3881 
3882 	if ((rwqe = kmem_zalloc(sizeof (ibd_rwqe_t), KM_NOSLEEP)) == NULL) {
3883 		DPRINT(10, "ibd_alloc_rwqe: failed in kmem_alloc");
3884 		return (DDI_FAILURE);
3885 	}
3886 	*wqe = rwqe;
3887 	rwqe->rwqe_type = IBD_WQE_RECV;
3888 	rwqe->w_state = state;
3889 	rwqe->rwqe_next = NULL;
3890 	rwqe->rwqe_prev = NULL;
3891 	rwqe->w_freeing_wqe = B_FALSE;
3892 	rwqe->w_freemsg_cb.free_func = ibd_freemsg_cb;
3893 	rwqe->w_freemsg_cb.free_arg = (char *)rwqe;
3894 
3895 	rwqe->rwqe_copybuf.ic_bufaddr = kmem_alloc(state->id_mtu +
3896 	    IPOIB_GRH_SIZE, KM_NOSLEEP);
3897 	if (rwqe->rwqe_copybuf.ic_bufaddr == NULL) {
3898 		DPRINT(10, "ibd_alloc_rwqe: failed in kmem_alloc");
3899 		kmem_free(rwqe, sizeof (ibd_rwqe_t));
3900 		return (DDI_FAILURE);
3901 	}
3902 
3903 	if ((rwqe->rwqe_im_mblk = desballoc(rwqe->rwqe_copybuf.ic_bufaddr,
3904 	    state->id_mtu + IPOIB_GRH_SIZE, 0, &rwqe->w_freemsg_cb)) ==
3905 	    NULL) {
3906 		DPRINT(10, "ibd_alloc_rwqe : failed in desballoc()");
3907 		kmem_free(rwqe->rwqe_copybuf.ic_bufaddr,
3908 		    state->id_mtu + IPOIB_GRH_SIZE);
3909 		rwqe->rwqe_copybuf.ic_bufaddr = NULL;
3910 		kmem_free(rwqe, sizeof (ibd_rwqe_t));
3911 		return (DDI_FAILURE);
3912 	}
3913 
3914 	mem_attr.mr_vaddr = (uint64_t)(uintptr_t)rwqe->rwqe_copybuf.ic_bufaddr;
3915 	mem_attr.mr_len = state->id_mtu + IPOIB_GRH_SIZE;
3916 	mem_attr.mr_as = NULL;
3917 	mem_attr.mr_flags = IBT_MR_NOSLEEP | IBT_MR_ENABLE_LOCAL_WRITE;
3918 	if (ibt_register_mr(state->id_hca_hdl, state->id_pd_hdl, &mem_attr,
3919 	    &rwqe->rwqe_copybuf.ic_mr_hdl, &rwqe->rwqe_copybuf.ic_mr_desc) !=
3920 	    IBT_SUCCESS) {
3921 		DPRINT(10, "ibd_alloc_rwqe : failed in ibt_register_mem()");
3922 		rwqe->w_freeing_wqe = B_TRUE;
3923 		freemsg(rwqe->rwqe_im_mblk);
3924 		kmem_free(rwqe->rwqe_copybuf.ic_bufaddr,
3925 		    state->id_mtu + IPOIB_GRH_SIZE);
3926 		rwqe->rwqe_copybuf.ic_bufaddr = NULL;
3927 		kmem_free(rwqe, sizeof (ibd_rwqe_t));
3928 		return (DDI_FAILURE);
3929 	}
3930 
3931 	rwqe->rwqe_copybuf.ic_sgl.ds_va =
3932 	    (ib_vaddr_t)(uintptr_t)rwqe->rwqe_copybuf.ic_bufaddr;
3933 	rwqe->rwqe_copybuf.ic_sgl.ds_key =
3934 	    rwqe->rwqe_copybuf.ic_mr_desc.md_lkey;
3935 	rwqe->rwqe_copybuf.ic_sgl.ds_len = state->id_mtu + IPOIB_GRH_SIZE;
3936 	rwqe->w_rwr.wr_id = (ibt_wrid_t)(uintptr_t)rwqe;
3937 	rwqe->w_rwr.wr_nds = 1;
3938 	rwqe->w_rwr.wr_sgl = &rwqe->rwqe_copybuf.ic_sgl;
3939 
3940 	return (DDI_SUCCESS);
3941 }
3942 
3943 /*
3944  * Free an allocated recv wqe.
3945  */
3946 static void
3947 ibd_free_rwqe(ibd_state_t *state, ibd_rwqe_t *rwqe)
3948 {
3949 	if (ibt_deregister_mr(state->id_hca_hdl,
3950 	    rwqe->rwqe_copybuf.ic_mr_hdl) != IBT_SUCCESS) {
3951 		DPRINT(10, "ibd_free_rwqe: failed in ibt_deregister_mr()");
3952 		return;
3953 	}
3954 
3955 	/*
3956 	 * Indicate to the callback function that this rwqe/mblk
3957 	 * should not be recycled. The freemsg() will invoke
3958 	 * ibd_freemsg_cb().
3959 	 */
3960 	if (rwqe->rwqe_im_mblk != NULL) {
3961 		rwqe->w_freeing_wqe = B_TRUE;
3962 		freemsg(rwqe->rwqe_im_mblk);
3963 	}
3964 	kmem_free(rwqe->rwqe_copybuf.ic_bufaddr,
3965 	    state->id_mtu + IPOIB_GRH_SIZE);
3966 	rwqe->rwqe_copybuf.ic_bufaddr = NULL;
3967 	kmem_free(rwqe, sizeof (ibd_rwqe_t));
3968 }
3969 
3970 /*
3971  * Delete the rwqe being freed from the rx list.
3972  */
3973 static void
3974 ibd_delete_rwqe(ibd_state_t *state, ibd_rwqe_t *rwqe)
3975 {
3976 	mutex_enter(&state->id_rx_list.dl_mutex);
3977 	if (state->id_rx_list.dl_head == RWQE_TO_WQE(rwqe))
3978 		state->id_rx_list.dl_head = rwqe->rwqe_next;
3979 	else
3980 		rwqe->rwqe_prev->w_next = rwqe->rwqe_next;
3981 	if (state->id_rx_list.dl_tail == RWQE_TO_WQE(rwqe))
3982 		state->id_rx_list.dl_tail = rwqe->rwqe_prev;
3983 	else
3984 		rwqe->rwqe_next->w_prev = rwqe->rwqe_prev;
3985 	mutex_exit(&state->id_rx_list.dl_mutex);
3986 }
3987 
3988 /*
3989  * Pre ibt_detach() deconstruction.
3990  */
3991 static void
3992 ibd_drv_fini(ibd_state_t *state)
3993 {
3994 	ib_gid_t mgid;
3995 	ibd_mce_t *mce;
3996 	ibt_status_t status;
3997 	uint8_t jstate;
3998 
3999 	/*
4000 	 * Desubscribe from trap notices; we will be tearing down
4001 	 * the mcg lists soon. Make sure the trap handler does nothing
4002 	 * even if it is invoked (ie till we invoke ibt_detach()).
4003 	 */
4004 	ibt_register_subnet_notices(state->id_ibt_hdl, NULL, NULL);
4005 	mutex_enter(&state->id_trap_lock);
4006 	state->id_trap_stop = B_TRUE;
4007 	while (state->id_trap_inprog > 0)
4008 		cv_wait(&state->id_trap_cv, &state->id_trap_lock);
4009 	mutex_exit(&state->id_trap_lock);
4010 
4011 	/*
4012 	 * Flushing the channel ensures that all pending WQE's
4013 	 * are marked with flush_error and handed to the CQ. It
4014 	 * does not guarantee the invocation of the CQ handler.
4015 	 * This call is guaranteed to return successfully for UD QPNs.
4016 	 */
4017 	status = ibt_flush_channel(state->id_chnl_hdl);
4018 	ASSERT(status == IBT_SUCCESS);
4019 
4020 	/*
4021 	 * We possibly need a loop here to wait for all the Tx
4022 	 * callbacks to happen. The Tx handlers will retrieve
4023 	 * held resources like AH ac_ref count, registered memory
4024 	 * and possibly IBD_ASYNC_REAP requests. Rx interrupts were already
4025 	 * turned off (in ibd_detach()); turn off Tx interrupts and
4026 	 * poll. By the time the polling returns an empty indicator,
4027 	 * we are sure we have seen all pending Tx callbacks. Note
4028 	 * that after the ibt_set_cq_handler() returns, the old handler
4029 	 * is guaranteed not to be invoked anymore.
4030 	 */
4031 	if (ibd_separate_cqs == 1)
4032 		ibt_set_cq_handler(state->id_scq_hdl, 0, 0);
4033 	ibd_poll_compq(state, state->id_scq_hdl);
4034 
4035 	/*
4036 	 * No more async requests will be posted since the device has been
4037 	 * unregistered; completion handlers have been turned off, so Tx
4038 	 * handler will not cause any more IBD_ASYNC_REAP requests. Queue a
4039 	 * request for the async thread to exit, which will be serviced
4040 	 * after any pending ones. This can take a while, specially if the
4041 	 * SM is unreachable, since IBMF will slowly timeout each SM request
4042 	 * issued by the async thread. Reap the thread before continuing on,
4043 	 * we do not want it to be lingering in modunloaded code.
4044 	 */
4045 	ibd_queue_work_slot(state, &state->id_ah_req, IBD_ASYNC_EXIT);
4046 	thread_join(state->id_async_thrid);
4047 
4048 	/*
4049 	 * We can not be in promiscuous mode anymore, upper layers
4050 	 * would have made a request to disable it (if ever set previously)
4051 	 * before the detach is allowed to progress to this point; and the
4052 	 * aysnc thread would have processed that request by now. Thus the
4053 	 * nonmember list is guaranteed empty at this point.
4054 	 */
4055 	ASSERT(state->id_prom_op != IBD_OP_COMPLETED);
4056 
4057 	/*
4058 	 * Drop all residual full/non membership. This includes full
4059 	 * membership to the broadcast group, and any nonmembership
4060 	 * acquired during transmits. We do this after the Tx completion
4061 	 * handlers are done, since those might result in some late
4062 	 * leaves; this also eliminates a potential race with that
4063 	 * path wrt the mc full list insert/delete. Trap handling
4064 	 * has also been suppressed at this point. Thus, no locks
4065 	 * are required while traversing the mc full list.
4066 	 */
4067 	DPRINT(2, "ibd_drv_fini : clear full cache entries");
4068 	mce = list_head(&state->id_mc_full);
4069 	while (mce != NULL) {
4070 		mgid = mce->mc_info.mc_adds_vect.av_dgid;
4071 		jstate = mce->mc_jstate;
4072 		mce = list_next(&state->id_mc_full, mce);
4073 		ibd_leave_group(state, mgid, jstate);
4074 	}
4075 
4076 	ibt_free_mcg_info(state->id_mcinfo, 1);
4077 
4078 	/*
4079 	 * Kill the channel now; guaranteed to return successfully
4080 	 * for UD QPNs.
4081 	 */
4082 	status = ibt_free_channel(state->id_chnl_hdl);
4083 	ASSERT(status == IBT_SUCCESS);
4084 
4085 	/*
4086 	 * Kill the CQ; all completion handlers are guaranteed to
4087 	 * have terminated by the time this returns. Since we killed
4088 	 * the QPN above, we can not receive the IBT_CQ_BUSY error.
4089 	 */
4090 	status = ibt_free_cq(state->id_rcq_hdl);
4091 	ASSERT(status == IBT_SUCCESS);
4092 	kmem_free(state->id_rxwcs, sizeof (ibt_wc_t) * state->id_rxwcs_size);
4093 
4094 	if (ibd_separate_cqs == 1) {
4095 		status = ibt_free_cq(state->id_scq_hdl);
4096 		ASSERT(status == IBT_SUCCESS);
4097 		kmem_free(state->id_txwcs, sizeof (ibt_wc_t) *
4098 		    state->id_txwcs_size);
4099 	}
4100 
4101 	/*
4102 	 * Since these following will act on the Rx/Tx list, which
4103 	 * is also looked at by the Rx/Tx handlers, keep them around
4104 	 * till all handlers are guaranteed to have completed.
4105 	 */
4106 	ibd_fini_rxlist(state);
4107 	ibd_fini_txlist(state);
4108 
4109 	/*
4110 	 * Clean up the active AH hash list.
4111 	 */
4112 	mod_hash_destroy_hash(state->id_ah_active_hash);
4113 
4114 	/*
4115 	 * Free parallel ARP cache and AHs; we are sure all of these
4116 	 * resources have been released by the Tx completion handler.
4117 	 */
4118 	ibd_acache_fini(state);
4119 
4120 	/*
4121 	 * We freed the QPN, all the MRs and AHs. This step should not
4122 	 * fail; print a warning message if it does fail, due to a bug
4123 	 * in the driver.
4124 	 */
4125 	if (ibt_free_pd(state->id_hca_hdl, state->id_pd_hdl) != IBT_SUCCESS)
4126 		ibd_print_warn(state, "failed to free protection domain");
4127 
4128 	if (ibt_close_hca(state->id_hca_hdl) != IBT_SUCCESS)
4129 		ibd_print_warn(state, "failed to close HCA device");
4130 }
4131 
4132 /*
4133  * IBA Rx/Tx completion queue handler. Guaranteed to be single
4134  * threaded and nonreentrant for this CQ. When using combined CQ,
4135  * this handles Tx and Rx completions. With separate CQs, this handles
4136  * only Rx completions.
4137  */
4138 /* ARGSUSED */
4139 static void
4140 ibd_rcq_handler(ibt_cq_hdl_t cq_hdl, void *arg)
4141 {
4142 	ibd_state_t *state = (ibd_state_t *)arg;
4143 
4144 	atomic_add_64(&state->id_num_intrs, 1);
4145 
4146 	if (ibd_rx_softintr == 1)
4147 		ddi_trigger_softintr(state->id_rx);
4148 	else
4149 		(void) ibd_intr((char *)state);
4150 }
4151 
4152 /*
4153  * Separate CQ handler for Tx completions, when the Tx CQ is in
4154  * interrupt driven mode.
4155  */
4156 /* ARGSUSED */
4157 static void
4158 ibd_scq_handler(ibt_cq_hdl_t cq_hdl, void *arg)
4159 {
4160 	ibd_state_t *state = (ibd_state_t *)arg;
4161 
4162 	atomic_add_64(&state->id_num_intrs, 1);
4163 
4164 	if (ibd_tx_softintr == 1)
4165 		ddi_trigger_softintr(state->id_tx);
4166 	else
4167 		(void) ibd_tx_recycle((char *)state);
4168 }
4169 
4170 /*
4171  * Multicast group create/delete trap handler. These will be delivered
4172  * on a kernel thread (handling can thus block) and can be invoked
4173  * concurrently. The handler can be invoked anytime after it is
4174  * registered and before ibt_detach().
4175  */
4176 /* ARGSUSED */
4177 static void
4178 ibd_snet_notices_handler(void *arg, ib_gid_t gid, ibt_subnet_event_code_t code,
4179     ibt_subnet_event_t *event)
4180 {
4181 	ibd_state_t *state = (ibd_state_t *)arg;
4182 	ibd_req_t *req;
4183 
4184 	/*
4185 	 * The trap handler will get invoked once for every event for
4186 	 * evert port. The input "gid" is the GID0 of the port the
4187 	 * trap came in on; we just need to act on traps that came
4188 	 * to our port, meaning the port on which the ipoib interface
4189 	 * resides. Since ipoib uses GID0 of the port, we just match
4190 	 * the gids to check whether we need to handle the trap.
4191 	 */
4192 	if (bcmp(&gid, &state->id_sgid, sizeof (ib_gid_t)) != 0)
4193 		return;
4194 
4195 	DPRINT(10, "ibd_notices_handler : %d\n", code);
4196 
4197 	switch (code) {
4198 		case IBT_SM_EVENT_UNAVAILABLE:
4199 			/*
4200 			 * If we are in promiscuous mode or have
4201 			 * sendnonmembers, we need to print a warning
4202 			 * message right now. Else, just store the
4203 			 * information, print when we enter promiscuous
4204 			 * mode or attempt nonmember send. We might
4205 			 * also want to stop caching sendnonmember.
4206 			 */
4207 			ibd_print_warn(state, "IBA multicast support "
4208 			    "degraded due to unavailability of multicast "
4209 			    "traps");
4210 			break;
4211 		case IBT_SM_EVENT_AVAILABLE:
4212 			/*
4213 			 * If we printed a warning message above or
4214 			 * while trying to nonmember send or get into
4215 			 * promiscuous mode, print an okay message.
4216 			 */
4217 			ibd_print_warn(state, "IBA multicast support "
4218 			    "restored due to availability of multicast "
4219 			    "traps");
4220 			break;
4221 		case IBT_SM_EVENT_MCG_CREATED:
4222 		case IBT_SM_EVENT_MCG_DELETED:
4223 			/*
4224 			 * Common processing of creation/deletion traps.
4225 			 * First check if the instance is being
4226 			 * [de]initialized; back off then, without doing
4227 			 * anything more, since we are not sure if the
4228 			 * async thread is around, or whether we might
4229 			 * be racing with the detach code in ibd_drv_fini()
4230 			 * that scans the mcg list.
4231 			 */
4232 			if (!ibd_async_safe(state))
4233 				return;
4234 
4235 			req = kmem_cache_alloc(state->id_req_kmc, KM_SLEEP);
4236 			req->rq_gid = event->sm_notice_gid;
4237 			req->rq_ptr = (void *)code;
4238 			ibd_queue_work_slot(state, req, IBD_ASYNC_TRAP);
4239 			break;
4240 	}
4241 }
4242 
4243 static void
4244 ibd_async_trap(ibd_state_t *state, ibd_req_t *req)
4245 {
4246 	ib_gid_t mgid = req->rq_gid;
4247 	ibt_subnet_event_code_t code = (ibt_subnet_event_code_t)req->rq_ptr;
4248 
4249 	DPRINT(10, "ibd_async_trap : %d\n", code);
4250 
4251 	/*
4252 	 * Atomically search the nonmember and sendonlymember lists and
4253 	 * delete.
4254 	 */
4255 	ibd_leave_group(state, mgid, IB_MC_JSTATE_SEND_ONLY_NON);
4256 
4257 	if (state->id_prom_op == IBD_OP_COMPLETED) {
4258 		ibd_leave_group(state, mgid, IB_MC_JSTATE_NON);
4259 
4260 		/*
4261 		 * If in promiscuous mode, try to join/attach to the new
4262 		 * mcg. Given the unreliable out-of-order mode of trap
4263 		 * delivery, we can never be sure whether it is a problem
4264 		 * if the join fails. Thus, we warn the admin of a failure
4265 		 * if this was a creation trap. Note that the trap might
4266 		 * actually be reporting a long past event, and the mcg
4267 		 * might already have been deleted, thus we might be warning
4268 		 * in vain.
4269 		 */
4270 		if ((ibd_join_group(state, mgid, IB_MC_JSTATE_NON) ==
4271 		    NULL) && (code == IBT_SM_EVENT_MCG_CREATED))
4272 			ibd_print_warn(state, "IBA promiscuous mode missed "
4273 			    "new multicast gid %016llx:%016llx",
4274 			    (u_longlong_t)mgid.gid_prefix,
4275 			    (u_longlong_t)mgid.gid_guid);
4276 	}
4277 
4278 	/*
4279 	 * Free the request slot allocated by the subnet event thread.
4280 	 */
4281 	ibd_async_done(state);
4282 }
4283 
4284 /*
4285  * GLDv3 entry point to get capabilities.
4286  */
4287 static boolean_t
4288 ibd_m_getcapab(void *arg, mac_capab_t cap, void *cap_data)
4289 {
4290 	ibd_state_t *state = arg;
4291 
4292 	switch (cap) {
4293 	case MAC_CAPAB_HCKSUM: {
4294 		uint32_t *txflags = cap_data;
4295 
4296 		/*
4297 		 * We either do full checksum or not do it at all
4298 		 */
4299 		if (state->id_hwcksum_capab & IBT_HCA_CKSUM_FULL)
4300 			*txflags = HCK_FULLCKSUM | HCKSUM_INET_FULL_V4;
4301 		else
4302 			return (B_FALSE);
4303 		break;
4304 	}
4305 
4306 	case MAC_CAPAB_LSO: {
4307 		mac_capab_lso_t *cap_lso = cap_data;
4308 
4309 		/*
4310 		 * In addition to the capability and policy, since LSO
4311 		 * relies on hw checksum, we'll not enable LSO if we
4312 		 * don't have hw checksum.  Of course, if the HCA doesn't
4313 		 * provide the reserved lkey capability, enabling LSO will
4314 		 * actually affect performance adversely, so we'll disable
4315 		 * LSO even for that case.
4316 		 */
4317 		if (!state->id_lso_policy || !state->id_lso_capable)
4318 			return (B_FALSE);
4319 
4320 		if ((state->id_hwcksum_capab & IBT_HCA_CKSUM_FULL) == 0)
4321 			return (B_FALSE);
4322 
4323 		if (state->id_hca_res_lkey_capab == 0) {
4324 			ibd_print_warn(state, "no reserved-lkey capability, "
4325 			    "disabling LSO");
4326 			return (B_FALSE);
4327 		}
4328 
4329 		cap_lso->lso_flags = LSO_TX_BASIC_TCP_IPV4;
4330 		cap_lso->lso_basic_tcp_ipv4.lso_max = state->id_lso_maxlen - 1;
4331 		break;
4332 	}
4333 
4334 	default:
4335 		return (B_FALSE);
4336 	}
4337 
4338 	return (B_TRUE);
4339 }
4340 
4341 /*
4342  * GLDv3 entry point to start hardware.
4343  */
4344 /*ARGSUSED*/
4345 static int
4346 ibd_m_start(void *arg)
4347 {
4348 	return (0);
4349 }
4350 
4351 /*
4352  * GLDv3 entry point to stop hardware from receiving packets.
4353  */
4354 /*ARGSUSED*/
4355 static void
4356 ibd_m_stop(void *arg)
4357 {
4358 }
4359 
4360 /*
4361  * GLDv3 entry point to modify device's mac address. We do not
4362  * allow address modifications.
4363  */
4364 static int
4365 ibd_m_unicst(void *arg, const uint8_t *macaddr)
4366 {
4367 	ibd_state_t *state;
4368 
4369 	state = (ibd_state_t *)arg;
4370 	if (bcmp(macaddr, &state->id_macaddr, IPOIB_ADDRL) == 0)
4371 		return (0);
4372 	else
4373 		return (EINVAL);
4374 }
4375 
4376 /*
4377  * The blocking part of the IBA join/leave operations are done out
4378  * of here on the async thread.
4379  */
4380 static void
4381 ibd_async_multicast(ibd_state_t *state, ib_gid_t mgid, int op)
4382 {
4383 	DPRINT(3, "ibd_async_multicast : async_setmc op %d :"
4384 	    "%016llx:%016llx\n", op, mgid.gid_prefix, mgid.gid_guid);
4385 
4386 	if (op == IBD_ASYNC_JOIN) {
4387 
4388 		if (ibd_join_group(state, mgid, IB_MC_JSTATE_FULL) == NULL) {
4389 			ibd_print_warn(state, "Joint multicast group failed :"
4390 			"%016llx:%016llx", mgid.gid_prefix, mgid.gid_guid);
4391 		}
4392 	} else {
4393 		/*
4394 		 * Here, we must search for the proper mcg_info and
4395 		 * use that to leave the group.
4396 		 */
4397 		ibd_leave_group(state, mgid, IB_MC_JSTATE_FULL);
4398 	}
4399 }
4400 
4401 /*
4402  * GLDv3 entry point for multicast enable/disable requests.
4403  * This function queues the operation to the async thread and
4404  * return success for a valid multicast address.
4405  */
4406 static int
4407 ibd_m_multicst(void *arg, boolean_t add, const uint8_t *mcmac)
4408 {
4409 	ibd_state_t *state = (ibd_state_t *)arg;
4410 	ipoib_mac_t maddr, *mcast;
4411 	ib_gid_t mgid;
4412 	ibd_req_t *req;
4413 
4414 	/*
4415 	 * The incoming multicast address might not be aligned properly
4416 	 * on a 4 byte boundary to be considered an ipoib_mac_t. We force
4417 	 * it to look like one though, to get the offsets of the mc gid,
4418 	 * since we know we are not going to dereference any values with
4419 	 * the ipoib_mac_t pointer.
4420 	 */
4421 	bcopy(mcmac, &maddr, sizeof (ipoib_mac_t));
4422 	mcast = &maddr;
4423 
4424 	/*
4425 	 * Check validity of MCG address. We could additionally check
4426 	 * that a enable/disable is not being issued on the "broadcast"
4427 	 * mcg, but since this operation is only invokable by priviledged
4428 	 * programs anyway, we allow the flexibility to those dlpi apps.
4429 	 * Note that we do not validate the "scope" of the IBA mcg.
4430 	 */
4431 	if ((ntohl(mcast->ipoib_qpn) & IB_QPN_MASK) != IB_MC_QPN)
4432 		return (EINVAL);
4433 
4434 	/*
4435 	 * fill in multicast pkey and scope
4436 	 */
4437 	IBD_FILL_SCOPE_PKEY(mcast, state->id_scope, state->id_pkey);
4438 
4439 	/*
4440 	 * If someone is trying to JOIN/LEAVE the broadcast group, we do
4441 	 * nothing (ie we stay JOINed to the broadcast group done in
4442 	 * ibd_drv_init()), to mimic ethernet behavior. IPv4 specifically
4443 	 * requires to be joined to broadcast groups at all times.
4444 	 * ibd_join_group() has an ASSERT(omce->mc_fullreap) that also
4445 	 * depends on this.
4446 	 */
4447 	if (bcmp(mcast, &state->id_bcaddr, IPOIB_ADDRL) == 0)
4448 		return (0);
4449 
4450 	ibd_n2h_gid(mcast, &mgid);
4451 	req = kmem_cache_alloc(state->id_req_kmc, KM_NOSLEEP);
4452 	if (req == NULL)
4453 		return (ENOMEM);
4454 
4455 	req->rq_gid = mgid;
4456 
4457 	if (add) {
4458 		DPRINT(1, "ibd_m_multicst : %016llx:%016llx\n",
4459 		    mgid.gid_prefix, mgid.gid_guid);
4460 		ibd_queue_work_slot(state, req, IBD_ASYNC_JOIN);
4461 	} else {
4462 		DPRINT(1, "ibd_m_multicst : unset_multicast : "
4463 		    "%016llx:%016llx", mgid.gid_prefix, mgid.gid_guid);
4464 		ibd_queue_work_slot(state, req, IBD_ASYNC_LEAVE);
4465 	}
4466 	return (0);
4467 }
4468 
4469 /*
4470  * The blocking part of the IBA promiscuous operations are done
4471  * out of here on the async thread. The dlpireq parameter indicates
4472  * whether this invocation is due to a dlpi request or due to
4473  * a port up/down event.
4474  */
4475 static void
4476 ibd_async_unsetprom(ibd_state_t *state)
4477 {
4478 	ibd_mce_t *mce = list_head(&state->id_mc_non);
4479 	ib_gid_t mgid;
4480 
4481 	DPRINT(2, "ibd_async_unsetprom : async_unset_promisc");
4482 
4483 	while (mce != NULL) {
4484 		mgid = mce->mc_info.mc_adds_vect.av_dgid;
4485 		mce = list_next(&state->id_mc_non, mce);
4486 		ibd_leave_group(state, mgid, IB_MC_JSTATE_NON);
4487 	}
4488 	state->id_prom_op = IBD_OP_NOTSTARTED;
4489 }
4490 
4491 /*
4492  * The blocking part of the IBA promiscuous operations are done
4493  * out of here on the async thread. The dlpireq parameter indicates
4494  * whether this invocation is due to a dlpi request or due to
4495  * a port up/down event.
4496  */
4497 static void
4498 ibd_async_setprom(ibd_state_t *state)
4499 {
4500 	ibt_mcg_attr_t mcg_attr;
4501 	ibt_mcg_info_t *mcg_info;
4502 	ib_gid_t mgid;
4503 	uint_t numg;
4504 	int i, ret = IBD_OP_COMPLETED;
4505 
4506 	DPRINT(2, "ibd_async_setprom : async_set_promisc");
4507 
4508 	/*
4509 	 * Obtain all active MC groups on the IB fabric with
4510 	 * specified criteria (scope + Pkey + Qkey + mtu).
4511 	 */
4512 	bzero(&mcg_attr, sizeof (mcg_attr));
4513 	mcg_attr.mc_pkey = state->id_pkey;
4514 	mcg_attr.mc_scope = state->id_scope;
4515 	mcg_attr.mc_qkey = state->id_mcinfo->mc_qkey;
4516 	mcg_attr.mc_mtu_req.r_mtu = state->id_mcinfo->mc_mtu;
4517 	mcg_attr.mc_mtu_req.r_selector = IBT_EQU;
4518 	if (ibt_query_mcg(state->id_sgid, &mcg_attr, 0, &mcg_info, &numg) !=
4519 	    IBT_SUCCESS) {
4520 		ibd_print_warn(state, "Could not get list of IBA multicast "
4521 		    "groups");
4522 		ret = IBD_OP_ERRORED;
4523 		goto done;
4524 	}
4525 
4526 	/*
4527 	 * Iterate over the returned mcg's and join as NonMember
4528 	 * to the IP mcg's.
4529 	 */
4530 	for (i = 0; i < numg; i++) {
4531 		/*
4532 		 * Do a NonMember JOIN on the MC group.
4533 		 */
4534 		mgid = mcg_info[i].mc_adds_vect.av_dgid;
4535 		if (ibd_join_group(state, mgid, IB_MC_JSTATE_NON) == NULL)
4536 			ibd_print_warn(state, "IBA promiscuous mode missed "
4537 			    "multicast gid %016llx:%016llx",
4538 			    (u_longlong_t)mgid.gid_prefix,
4539 			    (u_longlong_t)mgid.gid_guid);
4540 	}
4541 
4542 	ibt_free_mcg_info(mcg_info, numg);
4543 	DPRINT(4, "ibd_async_setprom : async_set_promisc completes");
4544 done:
4545 	state->id_prom_op = ret;
4546 }
4547 
4548 /*
4549  * GLDv3 entry point for multicast promiscuous enable/disable requests.
4550  * GLDv3 assumes phys state receives more packets than multi state,
4551  * which is not true for IPoIB. Thus, treat the multi and phys
4552  * promiscuous states the same way to work with GLDv3's assumption.
4553  */
4554 static int
4555 ibd_m_promisc(void *arg, boolean_t on)
4556 {
4557 	ibd_state_t *state = (ibd_state_t *)arg;
4558 	ibd_req_t *req;
4559 
4560 	req = kmem_cache_alloc(state->id_req_kmc, KM_NOSLEEP);
4561 	if (req == NULL)
4562 		return (ENOMEM);
4563 	if (on) {
4564 		DPRINT(1, "ibd_m_promisc : set_promisc : %d", on);
4565 		ibd_queue_work_slot(state, req, IBD_ASYNC_PROMON);
4566 	} else {
4567 		DPRINT(1, "ibd_m_promisc : unset_promisc");
4568 		ibd_queue_work_slot(state, req, IBD_ASYNC_PROMOFF);
4569 	}
4570 
4571 	return (0);
4572 }
4573 
4574 /*
4575  * GLDv3 entry point for gathering statistics.
4576  */
4577 static int
4578 ibd_m_stat(void *arg, uint_t stat, uint64_t *val)
4579 {
4580 	ibd_state_t *state = (ibd_state_t *)arg;
4581 
4582 	switch (stat) {
4583 	case MAC_STAT_IFSPEED:
4584 		*val = state->id_link_speed;
4585 		break;
4586 	case MAC_STAT_MULTIRCV:
4587 		*val = state->id_multi_rcv;
4588 		break;
4589 	case MAC_STAT_BRDCSTRCV:
4590 		*val = state->id_brd_rcv;
4591 		break;
4592 	case MAC_STAT_MULTIXMT:
4593 		*val = state->id_multi_xmt;
4594 		break;
4595 	case MAC_STAT_BRDCSTXMT:
4596 		*val = state->id_brd_xmt;
4597 		break;
4598 	case MAC_STAT_RBYTES:
4599 		*val = state->id_rcv_bytes;
4600 		break;
4601 	case MAC_STAT_IPACKETS:
4602 		*val = state->id_rcv_pkt;
4603 		break;
4604 	case MAC_STAT_OBYTES:
4605 		*val = state->id_xmt_bytes;
4606 		break;
4607 	case MAC_STAT_OPACKETS:
4608 		*val = state->id_xmt_pkt;
4609 		break;
4610 	case MAC_STAT_OERRORS:
4611 		*val = state->id_ah_error;	/* failed AH translation */
4612 		break;
4613 	case MAC_STAT_IERRORS:
4614 		*val = 0;
4615 		break;
4616 	case MAC_STAT_NOXMTBUF:
4617 		*val = state->id_tx_short;
4618 		break;
4619 	case MAC_STAT_NORCVBUF:
4620 	default:
4621 		return (ENOTSUP);
4622 	}
4623 
4624 	return (0);
4625 }
4626 
4627 static void
4628 ibd_async_txsched(ibd_state_t *state)
4629 {
4630 	ibd_req_t *req;
4631 	int ret;
4632 
4633 	if (ibd_txcomp_poll)
4634 		ibd_poll_compq(state, state->id_scq_hdl);
4635 
4636 	ret = ibd_resume_transmission(state);
4637 	if (ret && ibd_txcomp_poll) {
4638 		if (req = kmem_cache_alloc(state->id_req_kmc, KM_NOSLEEP))
4639 			ibd_queue_work_slot(state, req, IBD_ASYNC_SCHED);
4640 		else {
4641 			ibd_print_warn(state, "ibd_async_txsched: "
4642 			    "no memory, can't schedule work slot");
4643 		}
4644 	}
4645 }
4646 
4647 static int
4648 ibd_resume_transmission(ibd_state_t *state)
4649 {
4650 	int flag;
4651 	int met_thresh = 0;
4652 	int ret = -1;
4653 
4654 	mutex_enter(&state->id_sched_lock);
4655 	if (state->id_sched_needed & IBD_RSRC_SWQE) {
4656 		met_thresh = (state->id_tx_list.dl_cnt >
4657 		    IBD_FREE_SWQES_THRESH);
4658 		flag = IBD_RSRC_SWQE;
4659 	} else if (state->id_sched_needed & IBD_RSRC_LSOBUF) {
4660 		ASSERT(state->id_lso != NULL);
4661 		met_thresh = (state->id_lso->bkt_nfree >
4662 		    IBD_FREE_LSOS_THRESH);
4663 		flag = IBD_RSRC_LSOBUF;
4664 	}
4665 	if (met_thresh) {
4666 		state->id_sched_needed &= ~flag;
4667 		ret = 0;
4668 	}
4669 	mutex_exit(&state->id_sched_lock);
4670 
4671 	if (ret == 0)
4672 		mac_tx_update(state->id_mh);
4673 
4674 	return (ret);
4675 }
4676 
4677 /*
4678  * Release the send wqe back into free list.
4679  */
4680 static void
4681 ibd_release_swqe(ibd_state_t *state, ibd_swqe_t *swqe)
4682 {
4683 	/*
4684 	 * Add back on Tx list for reuse.
4685 	 */
4686 	swqe->swqe_next = NULL;
4687 	mutex_enter(&state->id_tx_list.dl_mutex);
4688 	if (state->id_tx_list.dl_pending_sends) {
4689 		state->id_tx_list.dl_pending_sends = B_FALSE;
4690 	}
4691 	if (state->id_tx_list.dl_head == NULL) {
4692 		state->id_tx_list.dl_head = SWQE_TO_WQE(swqe);
4693 	} else {
4694 		state->id_tx_list.dl_tail->w_next = SWQE_TO_WQE(swqe);
4695 	}
4696 	state->id_tx_list.dl_tail = SWQE_TO_WQE(swqe);
4697 	state->id_tx_list.dl_cnt++;
4698 	mutex_exit(&state->id_tx_list.dl_mutex);
4699 }
4700 
4701 /*
4702  * Acquire a send wqe from free list.
4703  * Returns error number and send wqe pointer.
4704  */
4705 static int
4706 ibd_acquire_swqe(ibd_state_t *state, ibd_swqe_t **swqe)
4707 {
4708 	int rc = 0;
4709 	ibd_swqe_t *wqe;
4710 
4711 	/*
4712 	 * Check and reclaim some of the completed Tx requests.
4713 	 * If someone else is already in this code and pulling Tx
4714 	 * completions, no need to poll, since the current lock holder
4715 	 * will do the work anyway. Normally, we poll for completions
4716 	 * every few Tx attempts, but if we are short on Tx descriptors,
4717 	 * we always try to poll.
4718 	 */
4719 	if ((ibd_txcomp_poll == 1) &&
4720 	    (state->id_tx_list.dl_cnt < IBD_TX_POLL_THRESH)) {
4721 		ibd_poll_compq(state, state->id_scq_hdl);
4722 	}
4723 
4724 	/*
4725 	 * Grab required transmit wqes.
4726 	 */
4727 	mutex_enter(&state->id_tx_list.dl_mutex);
4728 	wqe = WQE_TO_SWQE(state->id_tx_list.dl_head);
4729 	if (wqe != NULL) {
4730 		state->id_tx_list.dl_cnt -= 1;
4731 		state->id_tx_list.dl_head = wqe->swqe_next;
4732 		if (state->id_tx_list.dl_tail == SWQE_TO_WQE(wqe))
4733 			state->id_tx_list.dl_tail = NULL;
4734 	} else {
4735 		/*
4736 		 * If we did not find the number we were looking for, flag
4737 		 * no resource. Adjust list appropriately in either case.
4738 		 */
4739 		rc = ENOENT;
4740 		state->id_tx_list.dl_pending_sends = B_TRUE;
4741 		DPRINT(5, "ibd_acquire_swqe: out of Tx wqe");
4742 		atomic_add_64(&state->id_tx_short, 1);
4743 	}
4744 	mutex_exit(&state->id_tx_list.dl_mutex);
4745 	*swqe = wqe;
4746 
4747 	return (rc);
4748 }
4749 
4750 static int
4751 ibd_setup_lso(ibd_swqe_t *node, mblk_t *mp, uint32_t mss,
4752     ibt_ud_dest_hdl_t ud_dest)
4753 {
4754 	mblk_t	*nmp;
4755 	int iph_len, tcph_len;
4756 	ibt_wr_lso_t *lso;
4757 	uintptr_t ip_start, tcp_start;
4758 	uint8_t *dst;
4759 	uint_t pending, mblen;
4760 
4761 	/*
4762 	 * The code in ibd_send would've set 'wr.ud.udwr_dest' by default;
4763 	 * we need to adjust it here for lso.
4764 	 */
4765 	lso = &(node->w_swr.wr.ud_lso);
4766 	lso->lso_ud_dest = ud_dest;
4767 	lso->lso_mss = mss;
4768 
4769 	/*
4770 	 * Calculate the LSO header size and set it in the UD LSO structure.
4771 	 * Note that the only assumption we make is that each of the IPoIB,
4772 	 * IP and TCP headers will be contained in a single mblk fragment;
4773 	 * together, the headers may span multiple mblk fragments.
4774 	 */
4775 	nmp = mp;
4776 	ip_start = (uintptr_t)(nmp->b_rptr) + IPOIB_HDRSIZE;
4777 	if (ip_start >= (uintptr_t)(nmp->b_wptr)) {
4778 		ip_start = (uintptr_t)nmp->b_cont->b_rptr
4779 		    + (ip_start - (uintptr_t)(nmp->b_wptr));
4780 		nmp = nmp->b_cont;
4781 
4782 	}
4783 	iph_len = IPH_HDR_LENGTH((ipha_t *)ip_start);
4784 
4785 	tcp_start = ip_start + iph_len;
4786 	if (tcp_start >= (uintptr_t)(nmp->b_wptr)) {
4787 		tcp_start = (uintptr_t)nmp->b_cont->b_rptr
4788 		    + (tcp_start - (uintptr_t)(nmp->b_wptr));
4789 		nmp = nmp->b_cont;
4790 	}
4791 	tcph_len = TCP_HDR_LENGTH((tcph_t *)tcp_start);
4792 	lso->lso_hdr_sz = IPOIB_HDRSIZE + iph_len + tcph_len;
4793 
4794 	/*
4795 	 * If the lso header fits entirely within a single mblk fragment,
4796 	 * we'll avoid an additional copy of the lso header here and just
4797 	 * pass the b_rptr of the mblk directly.
4798 	 *
4799 	 * If this isn't true, we'd have to allocate for it explicitly.
4800 	 */
4801 	if (lso->lso_hdr_sz <= MBLKL(mp)) {
4802 		lso->lso_hdr = mp->b_rptr;
4803 	} else {
4804 		/* On work completion, remember to free this allocated hdr */
4805 		lso->lso_hdr = kmem_zalloc(lso->lso_hdr_sz, KM_NOSLEEP);
4806 		if (lso->lso_hdr == NULL) {
4807 			DPRINT(10, "ibd_setup_lso: couldn't allocate lso hdr, "
4808 			    "sz = %d", lso->lso_hdr_sz);
4809 			lso->lso_hdr_sz = 0;
4810 			lso->lso_mss = 0;
4811 			return (-1);
4812 		}
4813 	}
4814 
4815 	/*
4816 	 * Copy in the lso header only if we need to
4817 	 */
4818 	if (lso->lso_hdr != mp->b_rptr) {
4819 		dst = lso->lso_hdr;
4820 		pending = lso->lso_hdr_sz;
4821 
4822 		for (nmp = mp; nmp && pending; nmp = nmp->b_cont) {
4823 			mblen = MBLKL(nmp);
4824 			if (pending > mblen) {
4825 				bcopy(nmp->b_rptr, dst, mblen);
4826 				dst += mblen;
4827 				pending -= mblen;
4828 			} else {
4829 				bcopy(nmp->b_rptr, dst, pending);
4830 				break;
4831 			}
4832 		}
4833 	}
4834 
4835 	return (0);
4836 }
4837 
4838 static void
4839 ibd_free_lsohdr(ibd_swqe_t *node, mblk_t *mp)
4840 {
4841 	ibt_wr_lso_t *lso;
4842 
4843 	if ((!node) || (!mp))
4844 		return;
4845 
4846 	/*
4847 	 * Free any header space that we might've allocated if we
4848 	 * did an LSO
4849 	 */
4850 	if (node->w_swr.wr_opcode == IBT_WRC_SEND_LSO) {
4851 		lso = &(node->w_swr.wr.ud_lso);
4852 		if ((lso->lso_hdr) && (lso->lso_hdr != mp->b_rptr)) {
4853 			kmem_free(lso->lso_hdr, lso->lso_hdr_sz);
4854 			lso->lso_hdr = NULL;
4855 			lso->lso_hdr_sz = 0;
4856 		}
4857 	}
4858 }
4859 
4860 static void
4861 ibd_post_send(ibd_state_t *state, ibd_swqe_t *node)
4862 {
4863 	uint_t		i;
4864 	uint_t		num_posted;
4865 	uint_t		n_wrs;
4866 	ibt_status_t	ibt_status;
4867 	ibt_send_wr_t	wrs[IBD_MAX_POST_MULTIPLE];
4868 	ibd_swqe_t	*elem;
4869 	ibd_swqe_t	*nodes[IBD_MAX_POST_MULTIPLE];
4870 
4871 	node->swqe_next = NULL;
4872 
4873 	mutex_enter(&state->id_txpost_lock);
4874 
4875 	/*
4876 	 * Enqueue the new node in chain of wqes to send
4877 	 */
4878 	if (state->id_tx_head) {
4879 		*(state->id_tx_tailp) = (ibd_wqe_t *)node;
4880 	} else {
4881 		state->id_tx_head = node;
4882 	}
4883 	state->id_tx_tailp = &(node->swqe_next);
4884 
4885 	/*
4886 	 * If someone else is helping out with the sends,
4887 	 * just go back
4888 	 */
4889 	if (state->id_tx_busy) {
4890 		mutex_exit(&state->id_txpost_lock);
4891 		return;
4892 	}
4893 
4894 	/*
4895 	 * Otherwise, mark the flag to indicate that we'll be
4896 	 * doing the dispatch of what's there in the wqe chain
4897 	 */
4898 	state->id_tx_busy = 1;
4899 
4900 	while (state->id_tx_head) {
4901 		/*
4902 		 * Collect pending requests, IBD_MAX_POST_MULTIPLE wrs
4903 		 * at a time if possible, and keep posting them.
4904 		 */
4905 		for (n_wrs = 0, elem = state->id_tx_head;
4906 		    (elem) && (n_wrs < IBD_MAX_POST_MULTIPLE);
4907 		    elem = WQE_TO_SWQE(elem->swqe_next), n_wrs++) {
4908 
4909 			nodes[n_wrs] = elem;
4910 			wrs[n_wrs] = elem->w_swr;
4911 		}
4912 		state->id_tx_head = elem;
4913 
4914 		/*
4915 		 * Release the txpost lock before posting the
4916 		 * send request to the hca; if the posting fails
4917 		 * for some reason, we'll never receive completion
4918 		 * intimation, so we'll need to cleanup.
4919 		 */
4920 		mutex_exit(&state->id_txpost_lock);
4921 
4922 		ASSERT(n_wrs != 0);
4923 
4924 		/*
4925 		 * If posting fails for some reason, we'll never receive
4926 		 * completion intimation, so we'll need to cleanup. But
4927 		 * we need to make sure we don't clean up nodes whose
4928 		 * wrs have been successfully posted. We assume that the
4929 		 * hca driver returns on the first failure to post and
4930 		 * therefore the first 'num_posted' entries don't need
4931 		 * cleanup here.
4932 		 */
4933 		num_posted = 0;
4934 		ibt_status = ibt_post_send(state->id_chnl_hdl,
4935 		    wrs, n_wrs, &num_posted);
4936 		if (ibt_status != IBT_SUCCESS) {
4937 
4938 			ibd_print_warn(state, "ibd_post_send: "
4939 			    "posting multiple wrs failed: "
4940 			    "requested=%d, done=%d, ret=%d",
4941 			    n_wrs, num_posted, ibt_status);
4942 
4943 			for (i = num_posted; i < n_wrs; i++)
4944 				ibd_tx_cleanup(state, nodes[i]);
4945 		}
4946 
4947 		/*
4948 		 * Grab the mutex before we go and check the tx Q again
4949 		 */
4950 		mutex_enter(&state->id_txpost_lock);
4951 	}
4952 
4953 	state->id_tx_busy = 0;
4954 	mutex_exit(&state->id_txpost_lock);
4955 }
4956 
4957 static int
4958 ibd_prepare_sgl(ibd_state_t *state, mblk_t *mp, ibd_swqe_t *node,
4959     uint_t lsohdr_sz)
4960 {
4961 	ibt_wr_ds_t *sgl;
4962 	ibt_status_t ibt_status;
4963 	mblk_t *nmp;
4964 	mblk_t *data_mp;
4965 	uchar_t *bufp;
4966 	size_t blksize;
4967 	size_t skip;
4968 	size_t avail;
4969 	uint_t pktsize;
4970 	uint_t frag_len;
4971 	uint_t pending_hdr;
4972 	uint_t hiwm;
4973 	int nmblks;
4974 	int i;
4975 
4976 	/*
4977 	 * Let's skip ahead to the data if this is LSO
4978 	 */
4979 	data_mp = mp;
4980 	pending_hdr = 0;
4981 	if (lsohdr_sz) {
4982 		pending_hdr = lsohdr_sz;
4983 		for (nmp = mp; nmp; nmp = nmp->b_cont) {
4984 			frag_len = nmp->b_wptr - nmp->b_rptr;
4985 			if (frag_len > pending_hdr)
4986 				break;
4987 			pending_hdr -= frag_len;
4988 		}
4989 		data_mp = nmp;	/* start of data past lso header */
4990 		ASSERT(data_mp != NULL);
4991 	}
4992 
4993 	/*
4994 	 * Calculate the size of message data and number of msg blocks
4995 	 */
4996 	pktsize = 0;
4997 	for (nmblks = 0, nmp = data_mp; nmp != NULL;
4998 	    nmp = nmp->b_cont, nmblks++) {
4999 		pktsize += MBLKL(nmp);
5000 	}
5001 	pktsize -= pending_hdr;
5002 
5003 	/*
5004 	 * Translating the virtual address regions into physical regions
5005 	 * for using the Reserved LKey feature results in a wr sgl that
5006 	 * is a little longer. Since failing ibt_map_mem_iov() is costly,
5007 	 * we'll fix a high-water mark (65%) for when we should stop.
5008 	 */
5009 	hiwm = (state->id_max_sqseg * 65) / 100;
5010 
5011 	/*
5012 	 * We only do ibt_map_mem_iov() if the pktsize is above the
5013 	 * "copy-threshold", and if the number of mp fragments is less than
5014 	 * the maximum acceptable.
5015 	 */
5016 	if ((state->id_hca_res_lkey_capab) &&
5017 	    (pktsize > IBD_TX_COPY_THRESH) &&
5018 	    (nmblks < hiwm)) {
5019 		ibt_iov_t iov_arr[IBD_MAX_SQSEG];
5020 		ibt_iov_attr_t iov_attr;
5021 
5022 		iov_attr.iov_as = NULL;
5023 		iov_attr.iov = iov_arr;
5024 		iov_attr.iov_buf = NULL;
5025 		iov_attr.iov_list_len = nmblks;
5026 		iov_attr.iov_wr_nds = state->id_max_sqseg;
5027 		iov_attr.iov_lso_hdr_sz = lsohdr_sz;
5028 		iov_attr.iov_flags = IBT_IOV_SLEEP;
5029 
5030 		for (nmp = data_mp, i = 0; i < nmblks; i++, nmp = nmp->b_cont) {
5031 			iov_arr[i].iov_addr = (caddr_t)(void *)nmp->b_rptr;
5032 			iov_arr[i].iov_len = MBLKL(nmp);
5033 			if (i == 0) {
5034 				iov_arr[i].iov_addr += pending_hdr;
5035 				iov_arr[i].iov_len -= pending_hdr;
5036 			}
5037 		}
5038 
5039 		node->w_buftype = IBD_WQE_MAPPED;
5040 		node->w_swr.wr_sgl = node->w_sgl;
5041 
5042 		ibt_status = ibt_map_mem_iov(state->id_hca_hdl, &iov_attr,
5043 		    (ibt_all_wr_t *)&node->w_swr, &node->w_mi_hdl);
5044 		if (ibt_status != IBT_SUCCESS) {
5045 			ibd_print_warn(state, "ibd_send: ibt_map_mem_iov "
5046 			    "failed, nmblks=%d, ret=%d\n", nmblks, ibt_status);
5047 			goto ibd_copy_path;
5048 		}
5049 
5050 		return (0);
5051 	}
5052 
5053 ibd_copy_path:
5054 	if (pktsize <= state->id_tx_buf_sz) {
5055 		node->swqe_copybuf.ic_sgl.ds_len = pktsize;
5056 		node->w_swr.wr_nds = 1;
5057 		node->w_swr.wr_sgl = &node->swqe_copybuf.ic_sgl;
5058 		node->w_buftype = IBD_WQE_TXBUF;
5059 
5060 		/*
5061 		 * Even though this is the copy path for transfers less than
5062 		 * id_tx_buf_sz, it could still be an LSO packet.  If so, it
5063 		 * is possible the first data mblk fragment (data_mp) still
5064 		 * contains part of the LSO header that we need to skip.
5065 		 */
5066 		bufp = (uchar_t *)(uintptr_t)node->w_swr.wr_sgl->ds_va;
5067 		for (nmp = data_mp; nmp != NULL; nmp = nmp->b_cont) {
5068 			blksize = MBLKL(nmp) - pending_hdr;
5069 			bcopy(nmp->b_rptr + pending_hdr, bufp, blksize);
5070 			bufp += blksize;
5071 			pending_hdr = 0;
5072 		}
5073 
5074 		return (0);
5075 	}
5076 
5077 	/*
5078 	 * Copy path for transfers greater than id_tx_buf_sz
5079 	 */
5080 	node->w_swr.wr_sgl = node->w_sgl;
5081 	if (ibd_acquire_lsobufs(state, pktsize,
5082 	    node->w_swr.wr_sgl, &(node->w_swr.wr_nds)) != 0) {
5083 		DPRINT(10, "ibd_prepare_sgl: lso bufs acquire failed");
5084 		return (-1);
5085 	}
5086 	node->w_buftype = IBD_WQE_LSOBUF;
5087 
5088 	/*
5089 	 * Copy the larger-than-id_tx_buf_sz packet into a set of
5090 	 * fixed-sized, pre-mapped LSO buffers. Note that we might
5091 	 * need to skip part of the LSO header in the first fragment
5092 	 * as before.
5093 	 */
5094 	nmp = data_mp;
5095 	skip = pending_hdr;
5096 	for (i = 0; i < node->w_swr.wr_nds; i++) {
5097 		sgl = node->w_swr.wr_sgl + i;
5098 		bufp = (uchar_t *)(uintptr_t)sgl->ds_va;
5099 		avail = IBD_LSO_BUFSZ;
5100 		while (nmp && avail) {
5101 			blksize = MBLKL(nmp) - skip;
5102 			if (blksize > avail) {
5103 				bcopy(nmp->b_rptr + skip, bufp, avail);
5104 				skip += avail;
5105 				avail = 0;
5106 			} else {
5107 				bcopy(nmp->b_rptr + skip, bufp, blksize);
5108 				skip = 0;
5109 				avail -= blksize;
5110 				bufp += blksize;
5111 				nmp = nmp->b_cont;
5112 			}
5113 		}
5114 	}
5115 
5116 	return (0);
5117 }
5118 
5119 /*
5120  * Schedule a completion queue polling to reap the resource we're
5121  * short on.  If we implement the change to reap tx completions
5122  * in a separate thread, we'll need to wake up that thread here.
5123  */
5124 static int
5125 ibd_sched_poll(ibd_state_t *state, int resource_type, int q_flag)
5126 {
5127 	ibd_req_t *req;
5128 
5129 	mutex_enter(&state->id_sched_lock);
5130 	state->id_sched_needed |= resource_type;
5131 	mutex_exit(&state->id_sched_lock);
5132 
5133 	/*
5134 	 * If we are asked to queue a work entry, we need to do it
5135 	 */
5136 	if (q_flag) {
5137 		req = kmem_cache_alloc(state->id_req_kmc, KM_NOSLEEP);
5138 		if (req == NULL)
5139 			return (-1);
5140 
5141 		ibd_queue_work_slot(state, req, IBD_ASYNC_SCHED);
5142 	}
5143 
5144 	return (0);
5145 }
5146 
5147 /*
5148  * The passed in packet has this format:
5149  * IPOIB_ADDRL b dest addr :: 2b sap :: 2b 0's :: data
5150  */
5151 static boolean_t
5152 ibd_send(ibd_state_t *state, mblk_t *mp)
5153 {
5154 	ibd_ace_t *ace;
5155 	ibd_swqe_t *node;
5156 	ipoib_mac_t *dest;
5157 	ib_header_info_t *ipibp;
5158 	ip6_t *ip6h;
5159 	uint_t pktsize;
5160 	uint32_t mss;
5161 	uint32_t hckflags;
5162 	uint32_t lsoflags = 0;
5163 	uint_t lsohdr_sz = 0;
5164 	int ret, len;
5165 	boolean_t dofree = B_FALSE;
5166 	boolean_t rc;
5167 
5168 	node = NULL;
5169 	if (ibd_acquire_swqe(state, &node) != 0) {
5170 		/*
5171 		 * If we don't have an swqe available, schedule a transmit
5172 		 * completion queue cleanup and hold off on sending more
5173 		 * more packets until we have some free swqes
5174 		 */
5175 		if (ibd_sched_poll(state, IBD_RSRC_SWQE, ibd_txcomp_poll) == 0)
5176 			return (B_FALSE);
5177 
5178 		/*
5179 		 * If a poll cannot be scheduled, we have no choice but
5180 		 * to drop this packet
5181 		 */
5182 		ibd_print_warn(state, "ibd_send: no swqe, pkt drop");
5183 		return (B_TRUE);
5184 	}
5185 
5186 	/*
5187 	 * Initialize the commonly used fields in swqe to NULL to protect
5188 	 * against ibd_tx_cleanup accidentally misinterpreting these on a
5189 	 * failure.
5190 	 */
5191 	node->swqe_im_mblk = NULL;
5192 	node->w_swr.wr_nds = 0;
5193 	node->w_swr.wr_sgl = NULL;
5194 	node->w_swr.wr_opcode = IBT_WRC_SEND;
5195 
5196 	/*
5197 	 * Obtain an address handle for the destination.
5198 	 */
5199 	ipibp = (ib_header_info_t *)mp->b_rptr;
5200 	dest = (ipoib_mac_t *)&ipibp->ib_dst;
5201 	if ((ntohl(dest->ipoib_qpn) & IB_QPN_MASK) == IB_MC_QPN)
5202 		IBD_FILL_SCOPE_PKEY(dest, state->id_scope, state->id_pkey);
5203 
5204 	pktsize = msgsize(mp);
5205 
5206 	atomic_add_64(&state->id_xmt_bytes, pktsize);
5207 	atomic_inc_64(&state->id_xmt_pkt);
5208 	if (bcmp(&ipibp->ib_dst, &state->id_bcaddr, IPOIB_ADDRL) == 0)
5209 		atomic_inc_64(&state->id_brd_xmt);
5210 	else if ((ntohl(ipibp->ib_dst.ipoib_qpn) & IB_QPN_MASK) == IB_MC_QPN)
5211 		atomic_inc_64(&state->id_multi_xmt);
5212 
5213 	if ((ace = ibd_acache_lookup(state, dest, &ret, 1)) != NULL) {
5214 		node->w_ahandle = ace;
5215 		node->w_swr.wr.ud.udwr_dest = ace->ac_dest;
5216 	} else {
5217 		DPRINT(5,
5218 		    "ibd_send: acache lookup %s for %08X:%08X:%08X:%08X:%08X",
5219 		    ((ret == EFAULT) ? "failed" : "queued"),
5220 		    htonl(dest->ipoib_qpn), htonl(dest->ipoib_gidpref[0]),
5221 		    htonl(dest->ipoib_gidpref[1]),
5222 		    htonl(dest->ipoib_gidsuff[0]),
5223 		    htonl(dest->ipoib_gidsuff[1]));
5224 		node->w_ahandle = NULL;
5225 
5226 		/*
5227 		 * for the poll mode, it is probably some cqe pending in the
5228 		 * cq. So ibd has to poll cq here, otherwise acache probably
5229 		 * may not be recycled.
5230 		 */
5231 		if (ibd_txcomp_poll == 1)
5232 			ibd_poll_compq(state, state->id_scq_hdl);
5233 
5234 		/*
5235 		 * Here if ibd_acache_lookup() returns EFAULT, it means ibd
5236 		 * can not find a path for the specific dest address. We
5237 		 * should get rid of this kind of packet.  We also should get
5238 		 * rid of the packet if we cannot schedule a poll via the
5239 		 * async thread.  For the normal case, ibd will return the
5240 		 * packet to upper layer and wait for AH creating.
5241 		 *
5242 		 * Note that we always queue a work slot entry for the async
5243 		 * thread when we fail AH lookup (even in intr mode); this is
5244 		 * due to the convoluted way the code currently looks for AH.
5245 		 */
5246 		if (ret == EFAULT) {
5247 			dofree = B_TRUE;
5248 			rc = B_TRUE;
5249 		} else if (ibd_sched_poll(state, IBD_RSRC_SWQE, 1) != 0) {
5250 			dofree = B_TRUE;
5251 			rc = B_TRUE;
5252 		} else {
5253 			dofree = B_FALSE;
5254 			rc = B_FALSE;
5255 		}
5256 		goto ibd_send_fail;
5257 	}
5258 
5259 	/*
5260 	 * For ND6 packets, padding is at the front of the source lladdr.
5261 	 * Insert the padding at front.
5262 	 */
5263 	if (ntohs(ipibp->ipib_rhdr.ipoib_type) == IP6_DL_SAP) {
5264 		if (MBLKL(mp) < sizeof (ib_header_info_t) + IPV6_HDR_LEN) {
5265 			if (!pullupmsg(mp, IPV6_HDR_LEN +
5266 			    sizeof (ib_header_info_t))) {
5267 				DPRINT(10, "ibd_send: pullupmsg failure ");
5268 				dofree = B_TRUE;
5269 				rc = B_TRUE;
5270 				goto ibd_send_fail;
5271 			}
5272 			ipibp = (ib_header_info_t *)mp->b_rptr;
5273 		}
5274 		ip6h = (ip6_t *)((uchar_t *)ipibp +
5275 		    sizeof (ib_header_info_t));
5276 		len = ntohs(ip6h->ip6_plen);
5277 		if (ip6h->ip6_nxt == IPPROTO_ICMPV6) {
5278 			mblk_t	*pad;
5279 
5280 			pad = allocb(4, 0);
5281 			pad->b_wptr = (uchar_t *)pad->b_rptr + 4;
5282 			linkb(mp, pad);
5283 			if (MBLKL(mp) < sizeof (ib_header_info_t) +
5284 			    IPV6_HDR_LEN + len + 4) {
5285 				if (!pullupmsg(mp, sizeof (ib_header_info_t) +
5286 				    IPV6_HDR_LEN + len + 4)) {
5287 					DPRINT(10, "ibd_send: pullupmsg "
5288 					    "failure ");
5289 					dofree = B_TRUE;
5290 					rc = B_TRUE;
5291 					goto ibd_send_fail;
5292 				}
5293 				ip6h = (ip6_t *)((uchar_t *)mp->b_rptr +
5294 				    sizeof (ib_header_info_t));
5295 			}
5296 
5297 			/* LINTED: E_CONSTANT_CONDITION */
5298 			IBD_PAD_NSNA(ip6h, len, IBD_SEND);
5299 		}
5300 	}
5301 
5302 	mp->b_rptr += sizeof (ib_addrs_t);
5303 
5304 	/*
5305 	 * Do LSO and checksum related work here.  For LSO send, adjust the
5306 	 * ud destination, the opcode and the LSO header information to the
5307 	 * work request.
5308 	 */
5309 	lso_info_get(mp, &mss, &lsoflags);
5310 	if ((lsoflags & HW_LSO) != HW_LSO) {
5311 		node->w_swr.wr_opcode = IBT_WRC_SEND;
5312 		lsohdr_sz = 0;
5313 	} else {
5314 		if (ibd_setup_lso(node, mp, mss, ace->ac_dest) != 0) {
5315 			/*
5316 			 * The routine can only fail if there's no memory; we
5317 			 * can only drop the packet if this happens
5318 			 */
5319 			ibd_print_warn(state,
5320 			    "ibd_send: no memory, lso posting failed");
5321 			dofree = B_TRUE;
5322 			rc = B_TRUE;
5323 			goto ibd_send_fail;
5324 		}
5325 
5326 		node->w_swr.wr_opcode = IBT_WRC_SEND_LSO;
5327 		lsohdr_sz = (node->w_swr.wr.ud_lso).lso_hdr_sz;
5328 	}
5329 
5330 	hcksum_retrieve(mp, NULL, NULL, NULL, NULL, NULL, NULL, &hckflags);
5331 	if ((hckflags & HCK_FULLCKSUM) == HCK_FULLCKSUM)
5332 		node->w_swr.wr_flags |= IBT_WR_SEND_CKSUM;
5333 	else
5334 		node->w_swr.wr_flags &= ~IBT_WR_SEND_CKSUM;
5335 
5336 	/*
5337 	 * Prepare the sgl for posting; the routine can only fail if there's
5338 	 * no lso buf available for posting. If this is the case, we should
5339 	 * probably resched for lso bufs to become available and then try again.
5340 	 */
5341 	if (ibd_prepare_sgl(state, mp, node, lsohdr_sz) != 0) {
5342 		if (ibd_sched_poll(state, IBD_RSRC_LSOBUF, 1) != 0) {
5343 			dofree = B_TRUE;
5344 			rc = B_TRUE;
5345 		} else {
5346 			dofree = B_FALSE;
5347 			rc = B_FALSE;
5348 		}
5349 		goto ibd_send_fail;
5350 	}
5351 	node->swqe_im_mblk = mp;
5352 
5353 	/*
5354 	 * Queue the wqe to hardware; since we can now simply queue a
5355 	 * post instead of doing it serially, we cannot assume anything
5356 	 * about the 'node' after ibd_post_send() returns.
5357 	 */
5358 	ibd_post_send(state, node);
5359 
5360 	return (B_TRUE);
5361 
5362 ibd_send_fail:
5363 	if (node && mp)
5364 		ibd_free_lsohdr(node, mp);
5365 
5366 	if (dofree)
5367 		freemsg(mp);
5368 
5369 	if (node != NULL)
5370 		ibd_tx_cleanup(state, node);
5371 
5372 	return (rc);
5373 }
5374 
5375 /*
5376  * GLDv3 entry point for transmitting datagram.
5377  */
5378 static mblk_t *
5379 ibd_m_tx(void *arg, mblk_t *mp)
5380 {
5381 	ibd_state_t *state = (ibd_state_t *)arg;
5382 	mblk_t *next;
5383 
5384 	while (mp != NULL) {
5385 		next = mp->b_next;
5386 		mp->b_next = NULL;
5387 		if (ibd_send(state, mp) == B_FALSE) {
5388 			/* Send fail */
5389 			mp->b_next = next;
5390 			break;
5391 		}
5392 		mp = next;
5393 	}
5394 
5395 	return (mp);
5396 }
5397 
5398 /*
5399  * this handles Tx and Rx completions. With separate CQs, this handles
5400  * only Rx completions.
5401  */
5402 static uint_t
5403 ibd_intr(char *arg)
5404 {
5405 	ibd_state_t *state = (ibd_state_t *)arg;
5406 
5407 	ibd_poll_compq(state, state->id_rcq_hdl);
5408 
5409 	return (DDI_INTR_CLAIMED);
5410 }
5411 
5412 /*
5413  * Poll and drain the cq
5414  */
5415 static uint_t
5416 ibd_drain_cq(ibd_state_t *state, ibt_cq_hdl_t cq_hdl, ibt_wc_t *wcs,
5417     uint_t numwcs)
5418 {
5419 	ibd_wqe_t *wqe;
5420 	ibt_wc_t *wc;
5421 	uint_t total_polled = 0;
5422 	uint_t num_polled;
5423 	int i;
5424 
5425 	while (ibt_poll_cq(cq_hdl, wcs, numwcs, &num_polled) == IBT_SUCCESS) {
5426 		total_polled += num_polled;
5427 		for (i = 0, wc = wcs; i < num_polled; i++, wc++) {
5428 			wqe = (ibd_wqe_t *)(uintptr_t)wc->wc_id;
5429 			ASSERT((wqe->w_type == IBD_WQE_SEND) ||
5430 			    (wqe->w_type == IBD_WQE_RECV));
5431 			if (wc->wc_status != IBT_WC_SUCCESS) {
5432 				/*
5433 				 * Channel being torn down.
5434 				 */
5435 				if (wc->wc_status == IBT_WC_WR_FLUSHED_ERR) {
5436 					DPRINT(5, "ibd_drain_cq: flush error");
5437 					/*
5438 					 * Only invoke the Tx handler to
5439 					 * release possibly held resources
5440 					 * like AH refcount etc. Can not
5441 					 * invoke Rx handler because it might
5442 					 * try adding buffers to the Rx pool
5443 					 * when we are trying to deinitialize.
5444 					 */
5445 					if (wqe->w_type == IBD_WQE_RECV) {
5446 						continue;
5447 					} else {
5448 						DPRINT(10, "ibd_drain_cq: Bad "
5449 						    "status %d", wc->wc_status);
5450 					}
5451 				}
5452 			}
5453 			if (wqe->w_type == IBD_WQE_SEND) {
5454 				ibd_tx_cleanup(state, WQE_TO_SWQE(wqe));
5455 			} else {
5456 				ibd_process_rx(state, WQE_TO_RWQE(wqe), wc);
5457 			}
5458 		}
5459 	}
5460 
5461 	return (total_polled);
5462 }
5463 
5464 /*
5465  * Common code for interrupt handling as well as for polling
5466  * for all completed wqe's while detaching.
5467  */
5468 static void
5469 ibd_poll_compq(ibd_state_t *state, ibt_cq_hdl_t cq_hdl)
5470 {
5471 	ibt_wc_t *wcs;
5472 	uint_t numwcs;
5473 	int flag, redo_flag;
5474 	int redo = 1;
5475 	uint_t num_polled = 0;
5476 
5477 	if (ibd_separate_cqs == 1) {
5478 		if (cq_hdl == state->id_rcq_hdl) {
5479 			flag = IBD_RX_CQ_POLLING;
5480 			redo_flag = IBD_REDO_RX_CQ_POLLING;
5481 		} else {
5482 			flag = IBD_TX_CQ_POLLING;
5483 			redo_flag = IBD_REDO_TX_CQ_POLLING;
5484 		}
5485 	} else {
5486 		flag = IBD_RX_CQ_POLLING | IBD_TX_CQ_POLLING;
5487 		redo_flag = IBD_REDO_RX_CQ_POLLING | IBD_REDO_TX_CQ_POLLING;
5488 	}
5489 
5490 	mutex_enter(&state->id_cq_poll_lock);
5491 	if (state->id_cq_poll_busy & flag) {
5492 		state->id_cq_poll_busy |= redo_flag;
5493 		mutex_exit(&state->id_cq_poll_lock);
5494 		return;
5495 	}
5496 	state->id_cq_poll_busy |= flag;
5497 	mutex_exit(&state->id_cq_poll_lock);
5498 
5499 	/*
5500 	 * In some cases (eg detaching), this code can be invoked on
5501 	 * any cpu after disabling cq notification (thus no concurrency
5502 	 * exists). Apart from that, the following applies normally:
5503 	 * The receive completion handling is always on the Rx interrupt
5504 	 * cpu. Transmit completion handling could be from any cpu if
5505 	 * Tx CQ is poll driven, but always on Tx interrupt cpu if Tx CQ
5506 	 * is interrupt driven. Combined completion handling is always
5507 	 * on the interrupt cpu. Thus, lock accordingly and use the
5508 	 * proper completion array.
5509 	 */
5510 	if (ibd_separate_cqs == 1) {
5511 		if (cq_hdl == state->id_rcq_hdl) {
5512 			wcs = state->id_rxwcs;
5513 			numwcs = state->id_rxwcs_size;
5514 		} else {
5515 			wcs = state->id_txwcs;
5516 			numwcs = state->id_txwcs_size;
5517 		}
5518 	} else {
5519 		wcs = state->id_rxwcs;
5520 		numwcs = state->id_rxwcs_size;
5521 	}
5522 
5523 	/*
5524 	 * Poll and drain the CQ
5525 	 */
5526 	num_polled = ibd_drain_cq(state, cq_hdl, wcs, numwcs);
5527 
5528 	/*
5529 	 * Enable CQ notifications and redrain the cq to catch any
5530 	 * completions we might have missed after the ibd_drain_cq()
5531 	 * above and before the ibt_enable_cq_notify() that follows.
5532 	 * Finally, service any new requests to poll the cq that
5533 	 * could've come in after the ibt_enable_cq_notify().
5534 	 */
5535 	do {
5536 		if (ibt_enable_cq_notify(cq_hdl, IBT_NEXT_COMPLETION) !=
5537 		    IBT_SUCCESS) {
5538 			DPRINT(10, "ibd_intr: ibt_enable_cq_notify() failed");
5539 		}
5540 
5541 		num_polled += ibd_drain_cq(state, cq_hdl, wcs, numwcs);
5542 
5543 		mutex_enter(&state->id_cq_poll_lock);
5544 		if (state->id_cq_poll_busy & redo_flag)
5545 			state->id_cq_poll_busy &= ~redo_flag;
5546 		else {
5547 			state->id_cq_poll_busy &= ~flag;
5548 			redo = 0;
5549 		}
5550 		mutex_exit(&state->id_cq_poll_lock);
5551 
5552 	} while (redo);
5553 
5554 	/*
5555 	 * If we polled the receive cq and found anything, we need to flush
5556 	 * it out to the nw layer here.
5557 	 */
5558 	if ((flag & IBD_RX_CQ_POLLING) && (num_polled > 0)) {
5559 		ibd_flush_rx(state, NULL);
5560 	}
5561 }
5562 
5563 /*
5564  * Unmap the memory area associated with a given swqe.
5565  */
5566 static void
5567 ibd_unmap_mem(ibd_state_t *state, ibd_swqe_t *swqe)
5568 {
5569 	ibt_status_t stat;
5570 
5571 	DPRINT(20, "ibd_unmap_mem: wqe=%p, seg=%d\n", swqe, swqe->w_swr.wr_nds);
5572 
5573 	if (swqe->w_mi_hdl) {
5574 		if ((stat = ibt_unmap_mem_iov(state->id_hca_hdl,
5575 		    swqe->w_mi_hdl)) != IBT_SUCCESS) {
5576 			DPRINT(10,
5577 			    "failed in ibt_unmap_mem_iov, ret=%d\n", stat);
5578 		}
5579 		swqe->w_mi_hdl = NULL;
5580 	}
5581 	swqe->w_swr.wr_nds = 0;
5582 }
5583 
5584 /*
5585  * Common code that deals with clean ups after a successful or
5586  * erroneous transmission attempt.
5587  */
5588 static void
5589 ibd_tx_cleanup(ibd_state_t *state, ibd_swqe_t *swqe)
5590 {
5591 	ibd_ace_t *ace = swqe->w_ahandle;
5592 
5593 	DPRINT(20, "ibd_tx_cleanup %p\n", swqe);
5594 
5595 	/*
5596 	 * If this was a dynamic mapping in ibd_send(), we need to
5597 	 * unmap here. If this was an lso buffer we'd used for sending,
5598 	 * we need to release the lso buf to the pool, since the resource
5599 	 * is scarce. However, if this was simply a normal send using
5600 	 * the copybuf (present in each swqe), we don't need to release it.
5601 	 */
5602 	if (swqe->swqe_im_mblk != NULL) {
5603 		if (swqe->w_buftype == IBD_WQE_MAPPED) {
5604 			ibd_unmap_mem(state, swqe);
5605 		} else if (swqe->w_buftype == IBD_WQE_LSOBUF) {
5606 			ibd_release_lsobufs(state,
5607 			    swqe->w_swr.wr_sgl, swqe->w_swr.wr_nds);
5608 		}
5609 		ibd_free_lsohdr(swqe, swqe->swqe_im_mblk);
5610 		freemsg(swqe->swqe_im_mblk);
5611 		swqe->swqe_im_mblk = NULL;
5612 	}
5613 
5614 	/*
5615 	 * Drop the reference count on the AH; it can be reused
5616 	 * now for a different destination if there are no more
5617 	 * posted sends that will use it. This can be eliminated
5618 	 * if we can always associate each Tx buffer with an AH.
5619 	 * The ace can be null if we are cleaning up from the
5620 	 * ibd_send() error path.
5621 	 */
5622 	if (ace != NULL) {
5623 		/*
5624 		 * The recycling logic can be eliminated from here
5625 		 * and put into the async thread if we create another
5626 		 * list to hold ACE's for unjoined mcg's.
5627 		 */
5628 		if (DEC_REF_DO_CYCLE(ace)) {
5629 			ibd_mce_t *mce;
5630 
5631 			/*
5632 			 * Check with the lock taken: we decremented
5633 			 * reference count without the lock, and some
5634 			 * transmitter might alreay have bumped the
5635 			 * reference count (possible in case of multicast
5636 			 * disable when we leave the AH on the active
5637 			 * list). If not still 0, get out, leaving the
5638 			 * recycle bit intact.
5639 			 *
5640 			 * Atomically transition the AH from active
5641 			 * to free list, and queue a work request to
5642 			 * leave the group and destroy the mce. No
5643 			 * transmitter can be looking at the AH or
5644 			 * the MCE in between, since we have the
5645 			 * ac_mutex lock. In the SendOnly reap case,
5646 			 * it is not neccesary to hold the ac_mutex
5647 			 * and recheck the ref count (since the AH was
5648 			 * taken off the active list), we just do it
5649 			 * to have uniform processing with the Full
5650 			 * reap case.
5651 			 */
5652 			mutex_enter(&state->id_ac_mutex);
5653 			mce = ace->ac_mce;
5654 			if (GET_REF_CYCLE(ace) == 0) {
5655 				CLEAR_REFCYCLE(ace);
5656 				/*
5657 				 * Identify the case of fullmember reap as
5658 				 * opposed to mcg trap reap. Also, port up
5659 				 * might set ac_mce to NULL to indicate Tx
5660 				 * cleanup should do no more than put the
5661 				 * AH in the free list (see ibd_async_link).
5662 				 */
5663 				if (mce != NULL) {
5664 					ace->ac_mce = NULL;
5665 					IBD_ACACHE_PULLOUT_ACTIVE(state, ace);
5666 					/*
5667 					 * mc_req was initialized at mce
5668 					 * creation time.
5669 					 */
5670 					ibd_queue_work_slot(state,
5671 					    &mce->mc_req, IBD_ASYNC_REAP);
5672 				}
5673 				IBD_ACACHE_INSERT_FREE(state, ace);
5674 			}
5675 			mutex_exit(&state->id_ac_mutex);
5676 		}
5677 	}
5678 
5679 	/*
5680 	 * Release the send wqe for reuse.
5681 	 */
5682 	ibd_release_swqe(state, swqe);
5683 }
5684 
5685 /*
5686  * Hand off the processed rx mp chain to mac_rx()
5687  */
5688 static void
5689 ibd_flush_rx(ibd_state_t *state, mblk_t *mpc)
5690 {
5691 	if (mpc == NULL) {
5692 		mutex_enter(&state->id_rx_lock);
5693 
5694 		mpc = state->id_rx_mp;
5695 
5696 		state->id_rx_mp = NULL;
5697 		state->id_rx_mp_tail = NULL;
5698 		state->id_rx_mp_len = 0;
5699 
5700 		mutex_exit(&state->id_rx_lock);
5701 	}
5702 
5703 	if (mpc) {
5704 		mac_rx(state->id_mh, state->id_rh, mpc);
5705 	}
5706 }
5707 
5708 /*
5709  * Processing to be done after receipt of a packet; hand off to GLD
5710  * in the format expected by GLD.  The received packet has this
5711  * format: 2b sap :: 00 :: data.
5712  */
5713 static void
5714 ibd_process_rx(ibd_state_t *state, ibd_rwqe_t *rwqe, ibt_wc_t *wc)
5715 {
5716 	ib_header_info_t *phdr;
5717 	mblk_t *mp;
5718 	mblk_t *mpc = NULL;
5719 	ipoib_hdr_t *ipibp;
5720 	ip6_t *ip6h;
5721 	int rxcnt, len;
5722 
5723 	/*
5724 	 * Track number handed to upper layer, and number still
5725 	 * available to receive packets.
5726 	 */
5727 	rxcnt = atomic_add_32_nv(&state->id_rx_list.dl_cnt, -1);
5728 	ASSERT(rxcnt >= 0);
5729 	atomic_add_32(&state->id_rx_list.dl_bufs_outstanding, 1);
5730 
5731 	/*
5732 	 * Adjust write pointer depending on how much data came in.
5733 	 */
5734 	mp = rwqe->rwqe_im_mblk;
5735 	mp->b_wptr = mp->b_rptr + wc->wc_bytes_xfer;
5736 
5737 	/*
5738 	 * Make sure this is NULL or we're in trouble.
5739 	 */
5740 	if (mp->b_next != NULL) {
5741 		ibd_print_warn(state,
5742 		    "ibd_process_rx: got duplicate mp from rcq?");
5743 		mp->b_next = NULL;
5744 	}
5745 
5746 	/*
5747 	 * the IB link will deliver one of the IB link layer
5748 	 * headers called, the Global Routing Header (GRH).
5749 	 * ibd driver uses the information in GRH to build the
5750 	 * Header_info structure and pass it with the datagram up
5751 	 * to GLDv3.
5752 	 * If the GRH is not valid, indicate to GLDv3 by setting
5753 	 * the VerTcFlow field to 0.
5754 	 */
5755 	phdr = (ib_header_info_t *)mp->b_rptr;
5756 	if (wc->wc_flags & IBT_WC_GRH_PRESENT) {
5757 		phdr->ib_grh.ipoib_sqpn = htonl(wc->wc_qpn);
5758 
5759 		/* if it is loop back packet, just drop it. */
5760 		if (bcmp(&phdr->ib_grh.ipoib_sqpn, &state->id_macaddr,
5761 		    IPOIB_ADDRL) == 0) {
5762 			freemsg(mp);
5763 			return;
5764 		}
5765 
5766 		ovbcopy(&phdr->ib_grh.ipoib_sqpn, &phdr->ib_src,
5767 		    sizeof (ipoib_mac_t));
5768 		if (*(uint8_t *)(phdr->ib_grh.ipoib_dgid_pref) == 0xFF) {
5769 			phdr->ib_dst.ipoib_qpn = htonl(IB_MC_QPN);
5770 			IBD_CLEAR_SCOPE_PKEY(&phdr->ib_dst);
5771 		} else {
5772 			phdr->ib_dst.ipoib_qpn = state->id_macaddr.ipoib_qpn;
5773 		}
5774 	} else {
5775 		/*
5776 		 * It can not be a IBA multicast packet. Must have been
5777 		 * unicast for us. Just copy the interface address to dst.
5778 		 */
5779 		phdr->ib_grh.ipoib_vertcflow = 0;
5780 		ovbcopy(&state->id_macaddr, &phdr->ib_dst,
5781 		    sizeof (ipoib_mac_t));
5782 	}
5783 
5784 	/*
5785 	 * For ND6 packets, padding is at the front of the source/target
5786 	 * lladdr. However the inet6 layer is not aware of it, hence remove
5787 	 * the padding from such packets.
5788 	 */
5789 	ipibp = (ipoib_hdr_t *)((uchar_t *)mp->b_rptr + sizeof (ipoib_pgrh_t));
5790 	if (ntohs(ipibp->ipoib_type) == IP6_DL_SAP) {
5791 		if (MBLKL(mp) < sizeof (ipoib_hdr_t) + IPV6_HDR_LEN) {
5792 			if (!pullupmsg(mp, IPV6_HDR_LEN +
5793 			    sizeof (ipoib_hdr_t))) {
5794 				DPRINT(10, "ibd_process_rx: pullupmsg failed");
5795 				freemsg(mp);
5796 				return;
5797 			}
5798 			ipibp = (ipoib_hdr_t *)((uchar_t *)mp->b_rptr +
5799 			    sizeof (ipoib_pgrh_t));
5800 		}
5801 		ip6h = (ip6_t *)((uchar_t *)ipibp + sizeof (ipoib_hdr_t));
5802 		len = ntohs(ip6h->ip6_plen);
5803 		if (ip6h->ip6_nxt == IPPROTO_ICMPV6) {
5804 			if (MBLKL(mp) < sizeof (ipoib_hdr_t) +
5805 			    IPV6_HDR_LEN + len) {
5806 				if (!pullupmsg(mp, sizeof (ipoib_hdr_t) +
5807 				    IPV6_HDR_LEN + len)) {
5808 					DPRINT(10, "ibd_process_rx: pullupmsg"
5809 					    " failed");
5810 					freemsg(mp);
5811 					return;
5812 				}
5813 				ip6h = (ip6_t *)((uchar_t *)mp->b_rptr +
5814 				    sizeof (ipoib_pgrh_t) +
5815 				    sizeof (ipoib_hdr_t));
5816 			}
5817 			/* LINTED: E_CONSTANT_CONDITION */
5818 			IBD_PAD_NSNA(ip6h, len, IBD_RECV);
5819 		}
5820 	}
5821 
5822 	/*
5823 	 * Update statistics
5824 	 */
5825 	atomic_add_64(&state->id_rcv_bytes, wc->wc_bytes_xfer);
5826 	atomic_inc_64(&state->id_rcv_pkt);
5827 	if (bcmp(&phdr->ib_dst, &state->id_bcaddr, IPOIB_ADDRL) == 0)
5828 		atomic_inc_64(&state->id_brd_rcv);
5829 	else if ((ntohl(phdr->ib_dst.ipoib_qpn) & IB_QPN_MASK) == IB_MC_QPN)
5830 		atomic_inc_64(&state->id_multi_rcv);
5831 
5832 	/*
5833 	 * Set receive checksum status in mp
5834 	 */
5835 	if ((wc->wc_flags & IBT_WC_CKSUM_OK) == IBT_WC_CKSUM_OK) {
5836 		(void) hcksum_assoc(mp, NULL, NULL, 0, 0, 0, 0,
5837 		    HCK_FULLCKSUM | HCK_FULLCKSUM_OK, 0);
5838 	}
5839 
5840 	/*
5841 	 * Add this mp to the list of processed mp's to send to
5842 	 * the nw layer
5843 	 */
5844 	mutex_enter(&state->id_rx_lock);
5845 	if (state->id_rx_mp) {
5846 		ASSERT(state->id_rx_mp_tail != NULL);
5847 		state->id_rx_mp_tail->b_next = mp;
5848 	} else {
5849 		ASSERT(state->id_rx_mp_tail == NULL);
5850 		state->id_rx_mp = mp;
5851 	}
5852 
5853 	state->id_rx_mp_tail = mp;
5854 	state->id_rx_mp_len++;
5855 
5856 	if (state->id_rx_mp_len  >= IBD_MAX_RX_MP_LEN) {
5857 		mpc = state->id_rx_mp;
5858 
5859 		state->id_rx_mp = NULL;
5860 		state->id_rx_mp_tail = NULL;
5861 		state->id_rx_mp_len = 0;
5862 	}
5863 
5864 	mutex_exit(&state->id_rx_lock);
5865 
5866 	if (mpc) {
5867 		ibd_flush_rx(state, mpc);
5868 	}
5869 }
5870 
5871 /*
5872  * Callback code invoked from STREAMs when the receive data buffer is
5873  * free for recycling.
5874  */
5875 static void
5876 ibd_freemsg_cb(char *arg)
5877 {
5878 	ibd_rwqe_t *rwqe = (ibd_rwqe_t *)arg;
5879 	ibd_state_t *state = rwqe->w_state;
5880 
5881 	/*
5882 	 * If the wqe is being destructed, do not attempt recycling.
5883 	 */
5884 	if (rwqe->w_freeing_wqe == B_TRUE) {
5885 		DPRINT(6, "ibd_freemsg: wqe being freed");
5886 		return;
5887 	} else {
5888 		/*
5889 		 * Upper layer has released held mblk, so we have
5890 		 * no more use for keeping the old pointer in
5891 		 * our rwqe.
5892 		 */
5893 		rwqe->rwqe_im_mblk = NULL;
5894 	}
5895 
5896 	rwqe->rwqe_im_mblk = desballoc(rwqe->rwqe_copybuf.ic_bufaddr,
5897 	    state->id_mtu + IPOIB_GRH_SIZE, 0, &rwqe->w_freemsg_cb);
5898 	if (rwqe->rwqe_im_mblk == NULL) {
5899 		ibd_delete_rwqe(state, rwqe);
5900 		ibd_free_rwqe(state, rwqe);
5901 		DPRINT(6, "ibd_freemsg: desballoc failed");
5902 		return;
5903 	}
5904 
5905 	if (ibd_post_rwqe(state, rwqe, B_TRUE) == DDI_FAILURE) {
5906 		ibd_delete_rwqe(state, rwqe);
5907 		ibd_free_rwqe(state, rwqe);
5908 		return;
5909 	}
5910 
5911 	atomic_add_32(&state->id_rx_list.dl_bufs_outstanding, -1);
5912 }
5913 
5914 static uint_t
5915 ibd_tx_recycle(char *arg)
5916 {
5917 	ibd_state_t *state = (ibd_state_t *)arg;
5918 
5919 	/*
5920 	 * Poll for completed entries
5921 	 */
5922 	ibd_poll_compq(state, state->id_scq_hdl);
5923 
5924 	/*
5925 	 * Resume any blocked transmissions if possible
5926 	 */
5927 	(void) ibd_resume_transmission(state);
5928 
5929 	return (DDI_INTR_CLAIMED);
5930 }
5931 
5932 #ifdef IBD_LOGGING
5933 static void
5934 ibd_log_init(void)
5935 {
5936 	ibd_lbuf = kmem_zalloc(IBD_LOG_SZ, KM_SLEEP);
5937 	ibd_lbuf_ndx = 0;
5938 }
5939 
5940 static void
5941 ibd_log_fini(void)
5942 {
5943 	if (ibd_lbuf)
5944 		kmem_free(ibd_lbuf, IBD_LOG_SZ);
5945 	ibd_lbuf_ndx = 0;
5946 	ibd_lbuf = NULL;
5947 }
5948 
5949 static void
5950 ibd_log(const char *fmt, ...)
5951 {
5952 	va_list	ap;
5953 	uint32_t off;
5954 	uint32_t msglen;
5955 	char tmpbuf[IBD_DMAX_LINE];
5956 
5957 	if (ibd_lbuf == NULL)
5958 		return;
5959 
5960 	va_start(ap, fmt);
5961 	msglen = vsnprintf(tmpbuf, IBD_DMAX_LINE, fmt, ap);
5962 	va_end(ap);
5963 
5964 	if (msglen >= IBD_DMAX_LINE)
5965 		msglen = IBD_DMAX_LINE - 1;
5966 
5967 	mutex_enter(&ibd_lbuf_lock);
5968 
5969 	off = ibd_lbuf_ndx;		/* current msg should go here */
5970 	if ((ibd_lbuf_ndx) && (ibd_lbuf[ibd_lbuf_ndx-1] != '\n'))
5971 		ibd_lbuf[ibd_lbuf_ndx-1] = '\n';
5972 
5973 	ibd_lbuf_ndx += msglen;		/* place where next msg should start */
5974 	ibd_lbuf[ibd_lbuf_ndx] = 0;	/* current msg should terminate */
5975 
5976 	if (ibd_lbuf_ndx >= (IBD_LOG_SZ - 2 * IBD_DMAX_LINE))
5977 		ibd_lbuf_ndx = 0;
5978 
5979 	mutex_exit(&ibd_lbuf_lock);
5980 
5981 	bcopy(tmpbuf, ibd_lbuf+off, msglen);	/* no lock needed for this */
5982 }
5983 #endif
5984