xref: /illumos-gate/usr/src/uts/common/io/ib/ibtl/ibtl_handlers.c (revision f67ca41a3fe371a8ac34045eb45b3c5449ee601c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/ib/ibtl/impl/ibtl.h>
29 #include <sys/ib/ibtl/impl/ibtl_cm.h>
30 #include <sys/taskq.h>
31 #include <sys/disp.h>
32 #include <sys/callb.h>
33 #include <sys/proc.h>
34 
35 /*
36  * ibtl_handlers.c
37  */
38 
39 /*
40  * What's in this file?
41  *
42  *   This file started as an implementation of Asynchronous Event/Error
43  *   handling and Completion Queue handling.  As the implementation
44  *   evolved, code has been added for other ibc_* interfaces (resume,
45  *   predetach, etc.) that use the same mechanisms as used for asyncs.
46  *
47  * Async and CQ handling at interrupt level.
48  *
49  *   CQ handling is normally done at interrupt level using the CQ callback
50  *   handler to call the appropriate IBT Client (owner of the CQ).  For
51  *   clients that would prefer a fully flexible non-interrupt context to
52  *   do their CQ handling, a CQ can be created so that its handler is
53  *   called from a non-interrupt thread.  CQ handling is done frequently
54  *   whereas Async handling is expected to occur very infrequently.
55  *
56  *   Async handling is done by marking (or'ing in of an async_code of) the
57  *   pertinent IBTL data structure, and then notifying the async_thread(s)
58  *   that the data structure has async work to be done.  The notification
59  *   occurs by linking the data structure through its async_link onto a
60  *   list of like data structures and waking up an async_thread.  This
61  *   list append is not done if there is already async work pending on
62  *   this data structure (IBTL_ASYNC_PENDING).
63  *
64  * Async Mutex and CQ Mutex
65  *
66  *   The global ibtl_async_mutex is "the" mutex used to control access
67  *   to all the data needed by ibc_async_handler.  All the threads that
68  *   use this mutex are written so that the mutex is held for very short
69  *   periods of time, and never held while making calls to functions
70  *   that may block.
71  *
72  *   The global ibtl_cq_mutex is used similarly by ibc_cq_handler and
73  *   the ibtl_cq_thread(s).
74  *
75  * Mutex hierarchy
76  *
77  *   The ibtl_clnt_list_mutex is above the ibtl_async_mutex.
78  *   ibtl_clnt_list_mutex protects all of the various lists.
79  *   The ibtl_async_mutex is below this in the hierarchy.
80  *
81  *   The ibtl_cq_mutex is independent of the above mutexes.
82  *
83  * Threads
84  *
85  *   There are "ibtl_cq_threads" number of threads created for handling
86  *   Completion Queues in threads.  If this feature really gets used,
87  *   then we will want to do some suitable tuning.  Similarly, we may
88  *   want to tune the number of "ibtl_async_thread_init".
89  *
90  *   The function ibtl_cq_thread is the main loop for handling a CQ in a
91  *   thread.  There can be multiple threads executing this same code.
92  *   The code sleeps when there is no work to be done (list is empty),
93  *   otherwise it pulls the first CQ structure off the list and performs
94  *   the CQ handler callback to the client.  After that returns, a check
95  *   is made, and if another ibc_cq_handler call was made for this CQ,
96  *   the client is called again.
97  *
98  *   The function ibtl_async_thread is the main loop for handling async
99  *   events/errors.  There can be multiple threads executing this same code.
100  *   The code sleeps when there is no work to be done (lists are empty),
101  *   otherwise it pulls the first structure off one of the lists and
102  *   performs the async callback(s) to the client(s).  Note that HCA
103  *   async handling is done by calling each of the clients using the HCA.
104  *   When the async handling completes, the data structure having the async
105  *   event/error is checked for more work before it's considered "done".
106  *
107  * Taskq
108  *
109  *   The async_taskq is used here for allowing async handler callbacks to
110  *   occur simultaneously to multiple clients of an HCA.  This taskq could
111  *   be used for other purposes, e.g., if all the async_threads are in
112  *   use, but this is deemed as overkill since asyncs should occur rarely.
113  */
114 
115 /* Globals */
116 static char ibtf_handlers[] = "ibtl_handlers";
117 
118 /* priority for IBTL threads (async, cq, and taskq) */
119 static pri_t ibtl_pri = MAXCLSYSPRI - 1; /* maybe override in /etc/system */
120 
121 /* taskq used for HCA asyncs */
122 #define	ibtl_async_taskq system_taskq
123 
124 /* data for async handling by threads */
125 static kmutex_t ibtl_async_mutex;	/* protects most *_async_* data */
126 static kcondvar_t ibtl_async_cv;	/* async_threads wait on this */
127 static kcondvar_t ibtl_clnt_cv;		/* ibt_detach might wait on this */
128 static void ibtl_dec_clnt_async_cnt(ibtl_clnt_t *clntp);
129 static void ibtl_inc_clnt_async_cnt(ibtl_clnt_t *clntp);
130 
131 static kt_did_t *ibtl_async_did;	/* for thread_join() */
132 static int ibtl_async_thread_init = 4;	/* total # of async_threads to create */
133 static int ibtl_async_thread_exit = 0;	/* set if/when thread(s) should exit */
134 
135 /* async lists for various structures */
136 static ibtl_hca_devinfo_t *ibtl_async_hca_list_start, *ibtl_async_hca_list_end;
137 static ibtl_eec_t *ibtl_async_eec_list_start, *ibtl_async_eec_list_end;
138 static ibtl_qp_t *ibtl_async_qp_list_start, *ibtl_async_qp_list_end;
139 static ibtl_cq_t *ibtl_async_cq_list_start, *ibtl_async_cq_list_end;
140 static ibtl_srq_t *ibtl_async_srq_list_start, *ibtl_async_srq_list_end;
141 
142 /* data for CQ completion handling by threads */
143 static kmutex_t ibtl_cq_mutex;	/* protects the cv and the list below */
144 static kcondvar_t ibtl_cq_cv;
145 static ibtl_cq_t *ibtl_cq_list_start, *ibtl_cq_list_end;
146 
147 static int ibtl_cq_threads = 0;		/* total # of cq threads */
148 static int ibtl_cqs_using_threads = 0;	/* total # of cqs using threads */
149 static int ibtl_cq_thread_exit = 0;	/* set if/when thread(s) should exit */
150 
151 /* value used to tell IBTL threads to exit */
152 #define	IBTL_THREAD_EXIT 0x1b7fdead	/* IBTF DEAD */
153 
154 int ibtl_eec_not_supported = 1;
155 
156 char *ibtl_last_client_name;	/* may help debugging */
157 
158 _NOTE(LOCK_ORDER(ibtl_clnt_list_mutex ibtl_async_mutex))
159 
160 /*
161  * ibc_async_handler()
162  *
163  * Asynchronous Event/Error Handler.
164  *
165  *	This is the function called HCA drivers to post various async
166  *	event and errors mention in the IB architecture spec.  See
167  *	ibtl_types.h for additional details of this.
168  *
169  *	This function marks the pertinent IBTF object with the async_code,
170  *	and queues the object for handling by an ibtl_async_thread.  If
171  *	the object is NOT already marked for async processing, it is added
172  *	to the associated list for that type of object, and an
173  *	ibtl_async_thread is signaled to finish the async work.
174  */
175 void
176 ibc_async_handler(ibc_clnt_hdl_t hca_devp, ibt_async_code_t code,
177     ibc_async_event_t *event_p)
178 {
179 	ibtl_qp_t	*ibtl_qp;
180 	ibtl_cq_t	*ibtl_cq;
181 	ibtl_srq_t	*ibtl_srq;
182 	ibtl_eec_t	*ibtl_eec;
183 	uint8_t		port_minus1;
184 
185 	IBTF_DPRINTF_L2(ibtf_handlers, "ibc_async_handler(%p, 0x%x, %p)",
186 	    hca_devp, code, event_p);
187 
188 	mutex_enter(&ibtl_async_mutex);
189 
190 	switch (code) {
191 	case IBT_EVENT_PATH_MIGRATED_QP:
192 	case IBT_EVENT_SQD:
193 	case IBT_ERROR_CATASTROPHIC_QP:
194 	case IBT_ERROR_PATH_MIGRATE_REQ_QP:
195 	case IBT_EVENT_COM_EST_QP:
196 	case IBT_ERROR_INVALID_REQUEST_QP:
197 	case IBT_ERROR_ACCESS_VIOLATION_QP:
198 	case IBT_EVENT_EMPTY_QP:
199 		ibtl_qp = event_p->ev_qp_hdl;
200 		if (ibtl_qp == NULL) {
201 			IBTF_DPRINTF_L2(ibtf_handlers, "ibc_async_handler: "
202 			    "bad qp handle");
203 			break;
204 		}
205 		switch (code) {
206 		case IBT_ERROR_CATASTROPHIC_QP:
207 			ibtl_qp->qp_cat_fma_ena = event_p->ev_fma_ena; break;
208 		case IBT_ERROR_PATH_MIGRATE_REQ_QP:
209 			ibtl_qp->qp_pth_fma_ena = event_p->ev_fma_ena; break;
210 		case IBT_ERROR_INVALID_REQUEST_QP:
211 			ibtl_qp->qp_inv_fma_ena = event_p->ev_fma_ena; break;
212 		case IBT_ERROR_ACCESS_VIOLATION_QP:
213 			ibtl_qp->qp_acc_fma_ena = event_p->ev_fma_ena; break;
214 		}
215 
216 		ibtl_qp->qp_async_codes |= code;
217 		if ((ibtl_qp->qp_async_flags & IBTL_ASYNC_PENDING) == 0) {
218 			ibtl_qp->qp_async_flags |= IBTL_ASYNC_PENDING;
219 			ibtl_qp->qp_async_link = NULL;
220 			if (ibtl_async_qp_list_end == NULL)
221 				ibtl_async_qp_list_start = ibtl_qp;
222 			else
223 				ibtl_async_qp_list_end->qp_async_link = ibtl_qp;
224 			ibtl_async_qp_list_end = ibtl_qp;
225 			cv_signal(&ibtl_async_cv);
226 		}
227 		break;
228 
229 	case IBT_ERROR_CQ:
230 		ibtl_cq = event_p->ev_cq_hdl;
231 		if (ibtl_cq == NULL) {
232 			IBTF_DPRINTF_L2(ibtf_handlers, "ibc_async_handler: "
233 			    "bad cq handle");
234 			break;
235 		}
236 		ibtl_cq->cq_async_codes |= code;
237 		ibtl_cq->cq_fma_ena = event_p->ev_fma_ena;
238 		if ((ibtl_cq->cq_async_flags & IBTL_ASYNC_PENDING) == 0) {
239 			ibtl_cq->cq_async_flags |= IBTL_ASYNC_PENDING;
240 			ibtl_cq->cq_async_link = NULL;
241 			if (ibtl_async_cq_list_end == NULL)
242 				ibtl_async_cq_list_start = ibtl_cq;
243 			else
244 				ibtl_async_cq_list_end->cq_async_link = ibtl_cq;
245 			ibtl_async_cq_list_end = ibtl_cq;
246 			cv_signal(&ibtl_async_cv);
247 		}
248 		break;
249 
250 	case IBT_ERROR_CATASTROPHIC_SRQ:
251 	case IBT_EVENT_LIMIT_REACHED_SRQ:
252 		ibtl_srq = event_p->ev_srq_hdl;
253 		if (ibtl_srq == NULL) {
254 			IBTF_DPRINTF_L2(ibtf_handlers, "ibc_async_handler: "
255 			    "bad srq handle");
256 			break;
257 		}
258 		ibtl_srq->srq_async_codes |= code;
259 		ibtl_srq->srq_fma_ena = event_p->ev_fma_ena;
260 		if ((ibtl_srq->srq_async_flags & IBTL_ASYNC_PENDING) == 0) {
261 			ibtl_srq->srq_async_flags |= IBTL_ASYNC_PENDING;
262 			ibtl_srq->srq_async_link = NULL;
263 			if (ibtl_async_srq_list_end == NULL)
264 				ibtl_async_srq_list_start = ibtl_srq;
265 			else
266 				ibtl_async_srq_list_end->srq_async_link =
267 				    ibtl_srq;
268 			ibtl_async_srq_list_end = ibtl_srq;
269 			cv_signal(&ibtl_async_cv);
270 		}
271 		break;
272 
273 	case IBT_EVENT_PATH_MIGRATED_EEC:
274 	case IBT_ERROR_PATH_MIGRATE_REQ_EEC:
275 	case IBT_ERROR_CATASTROPHIC_EEC:
276 	case IBT_EVENT_COM_EST_EEC:
277 		if (ibtl_eec_not_supported) {
278 			IBTF_DPRINTF_L2(ibtf_handlers, "ibc_async_handler: "
279 			    "EEC events are disabled.");
280 			break;
281 		}
282 		ibtl_eec = event_p->ev_eec_hdl;
283 		if (ibtl_eec == NULL) {
284 			IBTF_DPRINTF_L2(ibtf_handlers, "ibc_async_handler: "
285 			    "bad eec handle");
286 			break;
287 		}
288 		switch (code) {
289 		case IBT_ERROR_PATH_MIGRATE_REQ_EEC:
290 			ibtl_eec->eec_pth_fma_ena = event_p->ev_fma_ena; break;
291 		case IBT_ERROR_CATASTROPHIC_EEC:
292 			ibtl_eec->eec_cat_fma_ena = event_p->ev_fma_ena; break;
293 		}
294 		ibtl_eec->eec_async_codes |= code;
295 		if ((ibtl_eec->eec_async_flags & IBTL_ASYNC_PENDING) == 0) {
296 			ibtl_eec->eec_async_flags |= IBTL_ASYNC_PENDING;
297 			ibtl_eec->eec_async_link = NULL;
298 			if (ibtl_async_eec_list_end == NULL)
299 				ibtl_async_eec_list_start = ibtl_eec;
300 			else
301 				ibtl_async_eec_list_end->eec_async_link =
302 				    ibtl_eec;
303 			ibtl_async_eec_list_end = ibtl_eec;
304 			cv_signal(&ibtl_async_cv);
305 		}
306 		break;
307 
308 	case IBT_ERROR_LOCAL_CATASTROPHIC:
309 		hca_devp->hd_async_codes |= code;
310 		hca_devp->hd_fma_ena = event_p->ev_fma_ena;
311 		/* FALLTHROUGH */
312 
313 	case IBT_EVENT_PORT_UP:
314 	case IBT_ERROR_PORT_DOWN:
315 		if ((code == IBT_EVENT_PORT_UP) ||
316 		    (code == IBT_ERROR_PORT_DOWN)) {
317 			if ((port_minus1 = event_p->ev_port - 1) >=
318 			    hca_devp->hd_hca_attr->hca_nports) {
319 				IBTF_DPRINTF_L2(ibtf_handlers,
320 				    "ibc_async_handler: bad port #: %d",
321 				    event_p->ev_port);
322 			    break;
323 			}
324 			hca_devp->hd_async_port[port_minus1] =
325 			    ((code == IBT_EVENT_PORT_UP) ? IBTL_HCA_PORT_UP :
326 			    IBTL_HCA_PORT_DOWN) | IBTL_HCA_PORT_CHANGED;
327 			hca_devp->hd_async_codes |= code;
328 		}
329 
330 		if ((hca_devp->hd_async_flags & IBTL_ASYNC_PENDING) == 0) {
331 			hca_devp->hd_async_flags |= IBTL_ASYNC_PENDING;
332 			hca_devp->hd_async_link = NULL;
333 			if (ibtl_async_hca_list_end == NULL)
334 				ibtl_async_hca_list_start = hca_devp;
335 			else
336 				ibtl_async_hca_list_end->hd_async_link =
337 				    hca_devp;
338 			ibtl_async_hca_list_end = hca_devp;
339 			cv_signal(&ibtl_async_cv);
340 		}
341 
342 		break;
343 
344 	default:
345 		IBTF_DPRINTF_L1(ibtf_handlers, "ibc_async_handler: "
346 		    "invalid code (0x%x)", code);
347 	}
348 
349 	mutex_exit(&ibtl_async_mutex);
350 }
351 
352 
353 /* Finally, make the async call to the client. */
354 
355 static void
356 ibtl_async_client_call(ibtl_hca_t *ibt_hca, ibt_async_code_t code,
357     ibt_async_event_t *event_p)
358 {
359 	ibtl_clnt_t		*clntp;
360 	void			*client_private;
361 	ibt_async_handler_t	async_handler;
362 	char			*client_name;
363 
364 	IBTF_DPRINTF_L2(ibtf_handlers, "ibtl_async_client_call(%p, 0x%x, %p)",
365 	    ibt_hca, code, event_p);
366 
367 	clntp = ibt_hca->ha_clnt_devp;
368 
369 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(ibtl_last_client_name))
370 	/* Record who is being called (just a debugging aid) */
371 	ibtl_last_client_name = client_name = clntp->clnt_name;
372 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(ibtl_last_client_name))
373 
374 	client_private = clntp->clnt_private;
375 	async_handler = clntp->clnt_modinfop->mi_async_handler;
376 
377 	if (code & (IBT_EVENT_COM_EST_QP | IBT_EVENT_COM_EST_EEC)) {
378 		mutex_enter(&ibtl_clnt_list_mutex);
379 		async_handler = ibtl_cm_async_handler;
380 		client_private = ibtl_cm_clnt_private;
381 		mutex_exit(&ibtl_clnt_list_mutex);
382 		ibt_hca = NULL;
383 		IBTF_DPRINTF_L2(ibtf_handlers, "ibtl_async_client_call: "
384 		    "calling CM for COM_EST");
385 	} else {
386 		IBTF_DPRINTF_L2(ibtf_handlers, "ibtl_async_client_call: "
387 		    "calling client '%s'", client_name);
388 	}
389 	if (async_handler != NULL)
390 		async_handler(client_private, ibt_hca, code, event_p);
391 	else
392 		IBTF_DPRINTF_L2(ibtf_handlers, "ibtl_async_client_call: "
393 		    "client '%s' has no async handler", client_name);
394 }
395 
396 /*
397  * Inform CM or DM about HCA events.
398  *
399  *	We use taskqs to allow simultaneous notification, with sleeping.
400  *	Since taskqs only allow one argument, we define a structure
401  *	because we need to pass in more than one argument.
402  */
403 
404 struct ibtl_mgr_s {
405 	ibtl_hca_devinfo_t	*mgr_hca_devp;
406 	ibt_async_handler_t	mgr_async_handler;
407 	void			*mgr_clnt_private;
408 };
409 
410 /*
411  * Asyncs of HCA level events for CM and DM.  Call CM or DM and tell them
412  * about the HCA for the event recorded in the ibtl_hca_devinfo_t.
413  */
414 static void
415 ibtl_do_mgr_async_task(void *arg)
416 {
417 	struct ibtl_mgr_s	*mgrp = (struct ibtl_mgr_s *)arg;
418 	ibtl_hca_devinfo_t	*hca_devp = mgrp->mgr_hca_devp;
419 
420 	IBTF_DPRINTF_L3(ibtf_handlers, "ibtl_do_mgr_async_task(0x%x)",
421 	    hca_devp->hd_async_code);
422 
423 	mgrp->mgr_async_handler(mgrp->mgr_clnt_private, NULL,
424 	    hca_devp->hd_async_code, &hca_devp->hd_async_event);
425 	kmem_free(mgrp, sizeof (*mgrp));
426 
427 	mutex_enter(&ibtl_clnt_list_mutex);
428 	if (--hca_devp->hd_async_task_cnt == 0)
429 		cv_signal(&hca_devp->hd_async_task_cv);
430 	mutex_exit(&ibtl_clnt_list_mutex);
431 }
432 
433 static void
434 ibtl_tell_mgr(ibtl_hca_devinfo_t *hca_devp, ibt_async_handler_t async_handler,
435     void *clnt_private)
436 {
437 	struct ibtl_mgr_s *mgrp;
438 
439 	if (async_handler == NULL)
440 		return;
441 
442 	_NOTE(NO_COMPETING_THREADS_NOW)
443 	mgrp = kmem_alloc(sizeof (*mgrp), KM_SLEEP);
444 	mgrp->mgr_hca_devp = hca_devp;
445 	mgrp->mgr_async_handler = async_handler;
446 	mgrp->mgr_clnt_private = clnt_private;
447 	hca_devp->hd_async_task_cnt++;
448 
449 	(void) taskq_dispatch(ibtl_async_taskq, ibtl_do_mgr_async_task, mgrp,
450 	    TQ_SLEEP);
451 #ifndef lint
452 	_NOTE(COMPETING_THREADS_NOW)
453 #endif
454 }
455 
456 /*
457  * Per client-device asyncs for HCA level events.  Call each client that is
458  * using the HCA for the event recorded in the ibtl_hca_devinfo_t.
459  */
460 static void
461 ibtl_hca_client_async_task(void *arg)
462 {
463 	ibtl_hca_t		*ibt_hca = (ibtl_hca_t *)arg;
464 	ibtl_hca_devinfo_t	*hca_devp = ibt_hca->ha_hca_devp;
465 	ibtl_clnt_t		*clntp = ibt_hca->ha_clnt_devp;
466 	ibt_async_event_t	async_event;
467 
468 	IBTF_DPRINTF_L3(ibtf_handlers, "ibtl_hca_client_async_task(%p, 0x%x)",
469 	    ibt_hca, hca_devp->hd_async_code);
470 
471 	bcopy(&hca_devp->hd_async_event, &async_event, sizeof (async_event));
472 	ibtl_async_client_call(ibt_hca, hca_devp->hd_async_code, &async_event);
473 
474 	mutex_enter(&ibtl_async_mutex);
475 	if (--ibt_hca->ha_async_cnt == 0 &&
476 	    (ibt_hca->ha_async_flags & IBTL_ASYNC_FREE_OBJECT)) {
477 		mutex_exit(&ibtl_async_mutex);
478 		kmem_free(ibt_hca, sizeof (ibtl_hca_t));
479 	} else
480 		mutex_exit(&ibtl_async_mutex);
481 
482 	mutex_enter(&ibtl_clnt_list_mutex);
483 	if (--hca_devp->hd_async_task_cnt == 0)
484 		cv_signal(&hca_devp->hd_async_task_cv);
485 	if (--clntp->clnt_async_cnt == 0)
486 		cv_broadcast(&ibtl_clnt_cv);
487 
488 	mutex_exit(&ibtl_clnt_list_mutex);
489 }
490 
491 /*
492  * Asyncs for HCA level events.
493  *
494  * The function continues to run until there are no more async
495  * events/errors for this HCA.  An event is chosen for dispatch
496  * to all clients of this HCA.  This thread dispatches them via
497  * the ibtl_async_taskq, then sleeps until all tasks are done.
498  *
499  * This thread records the async_code and async_event in the
500  * ibtl_hca_devinfo_t for all client taskq threads to reference.
501  *
502  * This is called from an async or taskq thread with ibtl_async_mutex held.
503  */
504 static void
505 ibtl_do_hca_asyncs(ibtl_hca_devinfo_t *hca_devp)
506 {
507 	ibtl_hca_t			*ibt_hca;
508 	ibt_async_code_t		code;
509 	ibtl_async_port_status_t  	temp;
510 	uint8_t				nports;
511 	uint8_t				port_minus1;
512 	ibtl_async_port_status_t	*portp;
513 
514 	mutex_exit(&ibtl_async_mutex);
515 
516 	mutex_enter(&ibtl_clnt_list_mutex);
517 	while (hca_devp->hd_async_busy)
518 		cv_wait(&hca_devp->hd_async_busy_cv, &ibtl_clnt_list_mutex);
519 	hca_devp->hd_async_busy = 1;
520 	mutex_enter(&ibtl_async_mutex);
521 
522 	bzero(&hca_devp->hd_async_event, sizeof (hca_devp->hd_async_event));
523 	for (;;) {
524 
525 		hca_devp->hd_async_event.ev_fma_ena = 0;
526 
527 		code = hca_devp->hd_async_codes;
528 		if (code & IBT_ERROR_LOCAL_CATASTROPHIC) {
529 			code = IBT_ERROR_LOCAL_CATASTROPHIC;
530 			hca_devp->hd_async_event.ev_fma_ena =
531 			    hca_devp->hd_fma_ena;
532 		} else if (code & IBT_ERROR_PORT_DOWN)
533 			code = IBT_ERROR_PORT_DOWN;
534 		else if (code & IBT_EVENT_PORT_UP)
535 			code = IBT_EVENT_PORT_UP;
536 		else {
537 			hca_devp->hd_async_codes = 0;
538 			code = 0;
539 		}
540 
541 		if (code == 0) {
542 			hca_devp->hd_async_flags &= ~IBTL_ASYNC_PENDING;
543 			break;
544 		}
545 		hca_devp->hd_async_codes &= ~code;
546 
547 		if ((code == IBT_EVENT_PORT_UP) ||
548 		    (code == IBT_ERROR_PORT_DOWN)) {
549 			/* PORT_UP or PORT_DOWN */
550 			portp = hca_devp->hd_async_port;
551 			nports = hca_devp->hd_hca_attr->hca_nports;
552 			for (port_minus1 = 0; port_minus1 < nports;
553 			    port_minus1++) {
554 				temp = ((code == IBT_EVENT_PORT_UP) ?
555 				    IBTL_HCA_PORT_UP : IBTL_HCA_PORT_DOWN) |
556 				    IBTL_HCA_PORT_CHANGED;
557 				if (portp[port_minus1] == temp)
558 					break;
559 			}
560 			if (port_minus1 >= nports) {
561 				/* we checked again, but found nothing */
562 				continue;
563 			}
564 			IBTF_DPRINTF_L4(ibtf_handlers, "ibtl_do_hca_asyncs: "
565 			    "async: port# %x code %x", port_minus1 + 1, code);
566 			/* mark it to check for other ports after we're done */
567 			hca_devp->hd_async_codes |= code;
568 
569 			hca_devp->hd_async_event.ev_port = port_minus1 + 1;
570 			hca_devp->hd_async_port[port_minus1] &=
571 			    ~IBTL_HCA_PORT_CHANGED;
572 
573 			mutex_exit(&ibtl_async_mutex);
574 			ibtl_reinit_hca_portinfo(hca_devp, port_minus1 + 1);
575 			mutex_enter(&ibtl_async_mutex);
576 		}
577 
578 		hca_devp->hd_async_code = code;
579 		hca_devp->hd_async_event.ev_hca_guid =
580 		    hca_devp->hd_hca_attr->hca_node_guid;
581 		mutex_exit(&ibtl_async_mutex);
582 
583 		/*
584 		 * Make sure to inform CM, DM, and IBMA if we know of them.
585 		 * Also, make sure not to inform them a second time, which
586 		 * would occur if they have the HCA open.
587 		 */
588 
589 		if (ibtl_ibma_async_handler)
590 			ibtl_tell_mgr(hca_devp, ibtl_ibma_async_handler,
591 			    ibtl_ibma_clnt_private);
592 		/* wait for all tasks to complete */
593 		while (hca_devp->hd_async_task_cnt != 0)
594 			cv_wait(&hca_devp->hd_async_task_cv,
595 			    &ibtl_clnt_list_mutex);
596 
597 		if (ibtl_dm_async_handler)
598 			ibtl_tell_mgr(hca_devp, ibtl_dm_async_handler,
599 			    ibtl_dm_clnt_private);
600 		if (ibtl_cm_async_handler)
601 			ibtl_tell_mgr(hca_devp, ibtl_cm_async_handler,
602 			    ibtl_cm_clnt_private);
603 		/* wait for all tasks to complete */
604 		while (hca_devp->hd_async_task_cnt != 0)
605 			cv_wait(&hca_devp->hd_async_task_cv,
606 			    &ibtl_clnt_list_mutex);
607 
608 		for (ibt_hca = hca_devp->hd_clnt_list;
609 		    ibt_hca != NULL;
610 		    ibt_hca = ibt_hca->ha_clnt_link) {
611 
612 			/* Managers are handled above */
613 			if (IBTL_HCA2MODI_P(ibt_hca)->mi_async_handler ==
614 			    ibtl_cm_async_handler)
615 				continue;
616 			if (IBTL_HCA2MODI_P(ibt_hca)->mi_async_handler ==
617 			    ibtl_dm_async_handler)
618 				continue;
619 			if (IBTL_HCA2MODI_P(ibt_hca)->mi_async_handler ==
620 			    ibtl_ibma_async_handler)
621 				continue;
622 			++ibt_hca->ha_clnt_devp->clnt_async_cnt;
623 
624 			mutex_enter(&ibtl_async_mutex);
625 			ibt_hca->ha_async_cnt++;
626 			mutex_exit(&ibtl_async_mutex);
627 			hca_devp->hd_async_task_cnt++;
628 			(void) taskq_dispatch(ibtl_async_taskq,
629 			    ibtl_hca_client_async_task, ibt_hca, TQ_SLEEP);
630 		}
631 
632 		/* wait for all tasks to complete */
633 		while (hca_devp->hd_async_task_cnt != 0)
634 			cv_wait(&hca_devp->hd_async_task_cv,
635 			    &ibtl_clnt_list_mutex);
636 
637 		mutex_enter(&ibtl_async_mutex);
638 	}
639 	hca_devp->hd_async_code = 0;
640 	hca_devp->hd_async_busy = 0;
641 	cv_broadcast(&hca_devp->hd_async_busy_cv);
642 	mutex_exit(&ibtl_clnt_list_mutex);
643 }
644 
645 /*
646  * Asyncs for QP objects.
647  *
648  * The function continues to run until there are no more async
649  * events/errors for this object.
650  */
651 static void
652 ibtl_do_qp_asyncs(ibtl_qp_t *ibtl_qp)
653 {
654 	ibt_async_code_t	code;
655 	ibt_async_event_t	async_event;
656 
657 	ASSERT(MUTEX_HELD(&ibtl_async_mutex));
658 	bzero(&async_event, sizeof (async_event));
659 	async_event.ev_chan_hdl = IBTL_QP2CHAN(ibtl_qp);
660 
661 	while ((code = ibtl_qp->qp_async_codes) != 0) {
662 		async_event.ev_fma_ena = 0;
663 		if (ibtl_qp->qp_async_flags & IBTL_ASYNC_FREE_OBJECT)
664 			code = 0;	/* fallthrough to "kmem_free" */
665 		else if (code & IBT_ERROR_CATASTROPHIC_QP) {
666 			code = IBT_ERROR_CATASTROPHIC_QP;
667 			async_event.ev_fma_ena = ibtl_qp->qp_cat_fma_ena;
668 		} else if (code & IBT_ERROR_INVALID_REQUEST_QP) {
669 			code = IBT_ERROR_INVALID_REQUEST_QP;
670 			async_event.ev_fma_ena = ibtl_qp->qp_inv_fma_ena;
671 		} else if (code & IBT_ERROR_ACCESS_VIOLATION_QP) {
672 			code = IBT_ERROR_ACCESS_VIOLATION_QP;
673 			async_event.ev_fma_ena = ibtl_qp->qp_acc_fma_ena;
674 		} else if (code & IBT_ERROR_PATH_MIGRATE_REQ_QP) {
675 			code = IBT_ERROR_PATH_MIGRATE_REQ_QP;
676 			async_event.ev_fma_ena = ibtl_qp->qp_pth_fma_ena;
677 		} else if (code & IBT_EVENT_PATH_MIGRATED_QP)
678 			code = IBT_EVENT_PATH_MIGRATED_QP;
679 		else if (code & IBT_EVENT_SQD)
680 			code = IBT_EVENT_SQD;
681 		else if (code & IBT_EVENT_COM_EST_QP)
682 			code = IBT_EVENT_COM_EST_QP;
683 		else if (code & IBT_EVENT_EMPTY_QP)
684 			code = IBT_EVENT_EMPTY_QP;
685 		else {
686 			IBTF_DPRINTF_L2(ibtf_handlers, "ibtl_do_qp_asyncs: "
687 			    "async: unexpected QP async code 0x%x", code);
688 			ibtl_qp->qp_async_codes = 0;
689 			code = 0;
690 		}
691 		ibtl_qp->qp_async_codes &= ~code;
692 
693 		if (code) {
694 			mutex_exit(&ibtl_async_mutex);
695 			ibtl_async_client_call(ibtl_qp->qp_hca,
696 			    code, &async_event);
697 			mutex_enter(&ibtl_async_mutex);
698 		}
699 
700 		if (ibtl_qp->qp_async_flags & IBTL_ASYNC_FREE_OBJECT) {
701 			mutex_exit(&ibtl_async_mutex);
702 			cv_destroy(&(IBTL_QP2CHAN(ibtl_qp))->ch_cm_cv);
703 			mutex_destroy(&(IBTL_QP2CHAN(ibtl_qp))->ch_cm_mutex);
704 			kmem_free(IBTL_QP2CHAN(ibtl_qp),
705 			    sizeof (ibtl_channel_t));
706 			mutex_enter(&ibtl_async_mutex);
707 			return;
708 		}
709 	}
710 	ibtl_qp->qp_async_flags &= ~IBTL_ASYNC_PENDING;
711 }
712 
713 /*
714  * Asyncs for SRQ objects.
715  *
716  * The function continues to run until there are no more async
717  * events/errors for this object.
718  */
719 static void
720 ibtl_do_srq_asyncs(ibtl_srq_t *ibtl_srq)
721 {
722 	ibt_async_code_t	code;
723 	ibt_async_event_t	async_event;
724 
725 	ASSERT(MUTEX_HELD(&ibtl_async_mutex));
726 	bzero(&async_event, sizeof (async_event));
727 	async_event.ev_srq_hdl = ibtl_srq;
728 	async_event.ev_fma_ena = ibtl_srq->srq_fma_ena;
729 
730 	while ((code = ibtl_srq->srq_async_codes) != 0) {
731 		if (ibtl_srq->srq_async_flags & IBTL_ASYNC_FREE_OBJECT)
732 			code = 0;	/* fallthrough to "kmem_free" */
733 		else if (code & IBT_ERROR_CATASTROPHIC_SRQ)
734 			code = IBT_ERROR_CATASTROPHIC_SRQ;
735 		else if (code & IBT_EVENT_LIMIT_REACHED_SRQ)
736 			code = IBT_EVENT_LIMIT_REACHED_SRQ;
737 		else {
738 			IBTF_DPRINTF_L2(ibtf_handlers, "ibtl_do_srq_asyncs: "
739 			    "async: unexpected SRQ async code 0x%x", code);
740 			ibtl_srq->srq_async_codes = 0;
741 			code = 0;
742 		}
743 		ibtl_srq->srq_async_codes &= ~code;
744 
745 		if (code) {
746 			mutex_exit(&ibtl_async_mutex);
747 			ibtl_async_client_call(ibtl_srq->srq_hca,
748 			    code, &async_event);
749 			mutex_enter(&ibtl_async_mutex);
750 		}
751 
752 		if (ibtl_srq->srq_async_flags & IBTL_ASYNC_FREE_OBJECT) {
753 			mutex_exit(&ibtl_async_mutex);
754 			kmem_free(ibtl_srq, sizeof (struct ibtl_srq_s));
755 			mutex_enter(&ibtl_async_mutex);
756 			return;
757 		}
758 	}
759 	ibtl_srq->srq_async_flags &= ~IBTL_ASYNC_PENDING;
760 }
761 
762 /*
763  * Asyncs for CQ objects.
764  *
765  * The function continues to run until there are no more async
766  * events/errors for this object.
767  */
768 static void
769 ibtl_do_cq_asyncs(ibtl_cq_t *ibtl_cq)
770 {
771 	ibt_async_code_t	code;
772 	ibt_async_event_t	async_event;
773 
774 	ASSERT(MUTEX_HELD(&ibtl_async_mutex));
775 	bzero(&async_event, sizeof (async_event));
776 	async_event.ev_cq_hdl = ibtl_cq;
777 	async_event.ev_fma_ena = ibtl_cq->cq_fma_ena;
778 
779 	while ((code = ibtl_cq->cq_async_codes) != 0) {
780 		if (ibtl_cq->cq_async_flags & IBTL_ASYNC_FREE_OBJECT)
781 			code = 0;	/* fallthrough to "kmem_free" */
782 		else if (code & IBT_ERROR_CQ)
783 			code = IBT_ERROR_CQ;
784 		else {
785 			IBTF_DPRINTF_L2(ibtf_handlers, "ibtl_do_cq_asyncs: "
786 			    "async: unexpected CQ async code 0x%x", code);
787 			ibtl_cq->cq_async_codes = 0;
788 			code = 0;
789 		}
790 		ibtl_cq->cq_async_codes &= ~code;
791 
792 		if (code) {
793 			mutex_exit(&ibtl_async_mutex);
794 			ibtl_async_client_call(ibtl_cq->cq_hca,
795 			    code, &async_event);
796 			mutex_enter(&ibtl_async_mutex);
797 		}
798 
799 		if (ibtl_cq->cq_async_flags & IBTL_ASYNC_FREE_OBJECT) {
800 			mutex_exit(&ibtl_async_mutex);
801 			mutex_destroy(&ibtl_cq->cq_mutex);
802 			kmem_free(ibtl_cq, sizeof (struct ibtl_cq_s));
803 			mutex_enter(&ibtl_async_mutex);
804 			return;
805 		}
806 	}
807 	ibtl_cq->cq_async_flags &= ~IBTL_ASYNC_PENDING;
808 }
809 
810 /*
811  * Asyncs for EEC objects.
812  *
813  * The function continues to run until there are no more async
814  * events/errors for this object.
815  */
816 static void
817 ibtl_do_eec_asyncs(ibtl_eec_t *ibtl_eec)
818 {
819 	ibt_async_code_t	code;
820 	ibt_async_event_t	async_event;
821 
822 	ASSERT(MUTEX_HELD(&ibtl_async_mutex));
823 	bzero(&async_event, sizeof (async_event));
824 	async_event.ev_chan_hdl = ibtl_eec->eec_channel;
825 
826 	while ((code = ibtl_eec->eec_async_codes) != 0) {
827 		async_event.ev_fma_ena = 0;
828 		if (ibtl_eec->eec_async_flags & IBTL_ASYNC_FREE_OBJECT)
829 			code = 0;	/* fallthrough to "kmem_free" */
830 		else if (code & IBT_ERROR_CATASTROPHIC_EEC) {
831 			code = IBT_ERROR_CATASTROPHIC_CHAN;
832 			async_event.ev_fma_ena = ibtl_eec->eec_cat_fma_ena;
833 		} else if (code & IBT_ERROR_PATH_MIGRATE_REQ_EEC) {
834 			code = IBT_ERROR_PATH_MIGRATE_REQ;
835 			async_event.ev_fma_ena = ibtl_eec->eec_pth_fma_ena;
836 		} else if (code & IBT_EVENT_PATH_MIGRATED_EEC)
837 			code = IBT_EVENT_PATH_MIGRATED;
838 		else if (code & IBT_EVENT_COM_EST_EEC)
839 			code = IBT_EVENT_COM_EST;
840 		else {
841 			IBTF_DPRINTF_L2(ibtf_handlers, "ibtl_do_eec_asyncs: "
842 			    "async: unexpected code 0x%x", code);
843 			ibtl_eec->eec_async_codes = 0;
844 			code = 0;
845 		}
846 		ibtl_eec->eec_async_codes &= ~code;
847 
848 		if (code) {
849 			mutex_exit(&ibtl_async_mutex);
850 			ibtl_async_client_call(ibtl_eec->eec_hca,
851 			    code, &async_event);
852 			mutex_enter(&ibtl_async_mutex);
853 		}
854 
855 		if (ibtl_eec->eec_async_flags & IBTL_ASYNC_FREE_OBJECT) {
856 			mutex_exit(&ibtl_async_mutex);
857 			kmem_free(ibtl_eec, sizeof (struct ibtl_eec_s));
858 			mutex_enter(&ibtl_async_mutex);
859 			return;
860 		}
861 	}
862 	ibtl_eec->eec_async_flags &= ~IBTL_ASYNC_PENDING;
863 }
864 
865 #ifdef __lock_lint
866 kmutex_t cpr_mutex;
867 #endif
868 
869 /*
870  * Loop forever, calling async_handlers until all of the async lists
871  * are empty.
872  */
873 
874 static void
875 ibtl_async_thread(void)
876 {
877 #ifndef __lock_lint
878 	kmutex_t cpr_mutex;
879 #endif
880 	callb_cpr_t	cprinfo;
881 
882 	_NOTE(MUTEX_PROTECTS_DATA(cpr_mutex, cprinfo))
883 	_NOTE(NO_COMPETING_THREADS_NOW)
884 	mutex_init(&cpr_mutex, NULL, MUTEX_DRIVER, NULL);
885 	CALLB_CPR_INIT(&cprinfo, &cpr_mutex, callb_generic_cpr,
886 	    "ibtl_async_thread");
887 #ifndef lint
888 	_NOTE(COMPETING_THREADS_NOW)
889 #endif
890 
891 	mutex_enter(&ibtl_async_mutex);
892 
893 	for (;;) {
894 		if (ibtl_async_hca_list_start) {
895 			ibtl_hca_devinfo_t *hca_devp;
896 
897 			/* remove first entry from list */
898 			hca_devp = ibtl_async_hca_list_start;
899 			ibtl_async_hca_list_start = hca_devp->hd_async_link;
900 			hca_devp->hd_async_link = NULL;
901 			if (ibtl_async_hca_list_start == NULL)
902 				ibtl_async_hca_list_end = NULL;
903 
904 			ibtl_do_hca_asyncs(hca_devp);
905 
906 		} else if (ibtl_async_qp_list_start) {
907 			ibtl_qp_t *ibtl_qp;
908 
909 			/* remove from list */
910 			ibtl_qp = ibtl_async_qp_list_start;
911 			ibtl_async_qp_list_start = ibtl_qp->qp_async_link;
912 			ibtl_qp->qp_async_link = NULL;
913 			if (ibtl_async_qp_list_start == NULL)
914 				ibtl_async_qp_list_end = NULL;
915 
916 			ibtl_do_qp_asyncs(ibtl_qp);
917 
918 		} else if (ibtl_async_srq_list_start) {
919 			ibtl_srq_t *ibtl_srq;
920 
921 			/* remove from list */
922 			ibtl_srq = ibtl_async_srq_list_start;
923 			ibtl_async_srq_list_start = ibtl_srq->srq_async_link;
924 			ibtl_srq->srq_async_link = NULL;
925 			if (ibtl_async_srq_list_start == NULL)
926 				ibtl_async_srq_list_end = NULL;
927 
928 			ibtl_do_srq_asyncs(ibtl_srq);
929 
930 		} else if (ibtl_async_eec_list_start) {
931 			ibtl_eec_t *ibtl_eec;
932 
933 			/* remove from list */
934 			ibtl_eec = ibtl_async_eec_list_start;
935 			ibtl_async_eec_list_start = ibtl_eec->eec_async_link;
936 			ibtl_eec->eec_async_link = NULL;
937 			if (ibtl_async_eec_list_start == NULL)
938 				ibtl_async_eec_list_end = NULL;
939 
940 			ibtl_do_eec_asyncs(ibtl_eec);
941 
942 		} else if (ibtl_async_cq_list_start) {
943 			ibtl_cq_t *ibtl_cq;
944 
945 			/* remove from list */
946 			ibtl_cq = ibtl_async_cq_list_start;
947 			ibtl_async_cq_list_start = ibtl_cq->cq_async_link;
948 			ibtl_cq->cq_async_link = NULL;
949 			if (ibtl_async_cq_list_start == NULL)
950 				ibtl_async_cq_list_end = NULL;
951 
952 			ibtl_do_cq_asyncs(ibtl_cq);
953 
954 		} else {
955 			if (ibtl_async_thread_exit == IBTL_THREAD_EXIT)
956 				break;
957 			mutex_enter(&cpr_mutex);
958 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
959 			mutex_exit(&cpr_mutex);
960 
961 			cv_wait(&ibtl_async_cv, &ibtl_async_mutex);
962 
963 			mutex_exit(&ibtl_async_mutex);
964 			mutex_enter(&cpr_mutex);
965 			CALLB_CPR_SAFE_END(&cprinfo, &cpr_mutex);
966 			mutex_exit(&cpr_mutex);
967 			mutex_enter(&ibtl_async_mutex);
968 		}
969 	}
970 
971 	mutex_exit(&ibtl_async_mutex);
972 
973 #ifndef __lock_lint
974 	mutex_enter(&cpr_mutex);
975 	CALLB_CPR_EXIT(&cprinfo);
976 #endif
977 	mutex_destroy(&cpr_mutex);
978 }
979 
980 
981 void
982 ibtl_free_qp_async_check(ibtl_qp_t *ibtl_qp)
983 {
984 	IBTF_DPRINTF_L3(ibtf_handlers, "ibtl_free_qp_async_check(%p)", ibtl_qp);
985 
986 	mutex_enter(&ibtl_async_mutex);
987 
988 	/*
989 	 * If there is an active async, mark this object to be freed
990 	 * by the async_thread when it's done.
991 	 */
992 	if (ibtl_qp->qp_async_flags & IBTL_ASYNC_PENDING) {
993 		ibtl_qp->qp_async_flags |= IBTL_ASYNC_FREE_OBJECT;
994 		mutex_exit(&ibtl_async_mutex);
995 	} else {	/* free the object now */
996 		mutex_exit(&ibtl_async_mutex);
997 		cv_destroy(&(IBTL_QP2CHAN(ibtl_qp))->ch_cm_cv);
998 		mutex_destroy(&(IBTL_QP2CHAN(ibtl_qp))->ch_cm_mutex);
999 		kmem_free(IBTL_QP2CHAN(ibtl_qp), sizeof (ibtl_channel_t));
1000 	}
1001 }
1002 
1003 void
1004 ibtl_free_cq_async_check(ibtl_cq_t *ibtl_cq)
1005 {
1006 	IBTF_DPRINTF_L3(ibtf_handlers, "ibtl_free_cq_async_check(%p)", ibtl_cq);
1007 
1008 	mutex_enter(&ibtl_async_mutex);
1009 
1010 	/* if there is an active async, mark this object to be freed */
1011 	if (ibtl_cq->cq_async_flags & IBTL_ASYNC_PENDING) {
1012 		ibtl_cq->cq_async_flags |= IBTL_ASYNC_FREE_OBJECT;
1013 		mutex_exit(&ibtl_async_mutex);
1014 	} else {	/* free the object now */
1015 		mutex_exit(&ibtl_async_mutex);
1016 		mutex_destroy(&ibtl_cq->cq_mutex);
1017 		kmem_free(ibtl_cq, sizeof (struct ibtl_cq_s));
1018 	}
1019 }
1020 
1021 void
1022 ibtl_free_srq_async_check(ibtl_srq_t *ibtl_srq)
1023 {
1024 	IBTF_DPRINTF_L3(ibtf_handlers, "ibtl_free_srq_async_check(%p)",
1025 	    ibtl_srq);
1026 
1027 	mutex_enter(&ibtl_async_mutex);
1028 
1029 	/* if there is an active async, mark this object to be freed */
1030 	if (ibtl_srq->srq_async_flags & IBTL_ASYNC_PENDING) {
1031 		ibtl_srq->srq_async_flags |= IBTL_ASYNC_FREE_OBJECT;
1032 		mutex_exit(&ibtl_async_mutex);
1033 	} else {	/* free the object now */
1034 		mutex_exit(&ibtl_async_mutex);
1035 		kmem_free(ibtl_srq, sizeof (struct ibtl_srq_s));
1036 	}
1037 }
1038 
1039 void
1040 ibtl_free_eec_async_check(ibtl_eec_t *ibtl_eec)
1041 {
1042 	IBTF_DPRINTF_L3(ibtf_handlers, "ibtl_free_eec_async_check(%p)",
1043 	    ibtl_eec);
1044 
1045 	mutex_enter(&ibtl_async_mutex);
1046 
1047 	/* if there is an active async, mark this object to be freed */
1048 	if (ibtl_eec->eec_async_flags & IBTL_ASYNC_PENDING) {
1049 		ibtl_eec->eec_async_flags |= IBTL_ASYNC_FREE_OBJECT;
1050 		mutex_exit(&ibtl_async_mutex);
1051 	} else {	/* free the object now */
1052 		mutex_exit(&ibtl_async_mutex);
1053 		kmem_free(ibtl_eec, sizeof (struct ibtl_eec_s));
1054 	}
1055 }
1056 
1057 /*
1058  * This function differs from above in that we assume this is called
1059  * from non-interrupt context, and never called from the async_thread.
1060  */
1061 
1062 void
1063 ibtl_free_hca_async_check(ibtl_hca_t *ibt_hca)
1064 {
1065 	IBTF_DPRINTF_L3(ibtf_handlers, "ibtl_free_hca_async_check(%p)",
1066 	    ibt_hca);
1067 
1068 	mutex_enter(&ibtl_async_mutex);
1069 
1070 	/* if there is an active async, mark this object to be freed */
1071 	if (ibt_hca->ha_async_cnt > 0) {
1072 		ibt_hca->ha_async_flags |= IBTL_ASYNC_FREE_OBJECT;
1073 		mutex_exit(&ibtl_async_mutex);
1074 	} else {	/* free the object now */
1075 		mutex_exit(&ibtl_async_mutex);
1076 		kmem_free(ibt_hca, sizeof (ibtl_hca_t));
1077 	}
1078 }
1079 
1080 /*
1081  * Completion Queue Handling.
1082  *
1083  *	A completion queue can be handled through a simple callback
1084  *	at interrupt level, or it may be queued for an ibtl_cq_thread
1085  *	to handle.  The latter is chosen during ibt_alloc_cq when the
1086  *	IBTF_CQ_HANDLER_IN_THREAD is specified.
1087  */
1088 
1089 static void
1090 ibtl_cq_handler_call(ibtl_cq_t *ibtl_cq)
1091 {
1092 	ibt_cq_handler_t	cq_handler;
1093 	void			*arg;
1094 
1095 	IBTF_DPRINTF_L4(ibtf_handlers, "ibtl_cq_handler_call(%p)", ibtl_cq);
1096 
1097 	mutex_enter(&ibtl_cq->cq_mutex);
1098 	cq_handler = ibtl_cq->cq_comp_handler;
1099 	arg = ibtl_cq->cq_arg;
1100 	if (cq_handler != NULL)
1101 		cq_handler(ibtl_cq, arg);
1102 	else
1103 		IBTF_DPRINTF_L2(ibtf_handlers, "ibtl_cq_handler_call: "
1104 		    "no cq_handler for cq %p", ibtl_cq);
1105 	mutex_exit(&ibtl_cq->cq_mutex);
1106 }
1107 
1108 /*
1109  * Before ibt_free_cq can continue, we need to ensure no more cq_handler
1110  * callbacks can occur.  When we get the mutex, we know there are no
1111  * outstanding cq_handler callbacks.  We set the cq_handler to NULL to
1112  * prohibit future callbacks.
1113  */
1114 void
1115 ibtl_free_cq_check(ibtl_cq_t *ibtl_cq)
1116 {
1117 	mutex_enter(&ibtl_cq->cq_mutex);
1118 	ibtl_cq->cq_comp_handler = NULL;
1119 	mutex_exit(&ibtl_cq->cq_mutex);
1120 	if (ibtl_cq->cq_in_thread) {
1121 		mutex_enter(&ibtl_cq_mutex);
1122 		--ibtl_cqs_using_threads;
1123 		while (ibtl_cq->cq_impl_flags & IBTL_CQ_PENDING) {
1124 			ibtl_cq->cq_impl_flags &= ~IBTL_CQ_CALL_CLIENT;
1125 			ibtl_cq->cq_impl_flags |= IBTL_CQ_FREE;
1126 			cv_wait(&ibtl_cq_cv, &ibtl_cq_mutex);
1127 		}
1128 		mutex_exit(&ibtl_cq_mutex);
1129 	}
1130 }
1131 
1132 /*
1133  * Loop forever, calling cq_handlers until the cq list
1134  * is empty.
1135  */
1136 
1137 static void
1138 ibtl_cq_thread(void)
1139 {
1140 #ifndef __lock_lint
1141 	kmutex_t cpr_mutex;
1142 #endif
1143 	callb_cpr_t	cprinfo;
1144 
1145 	_NOTE(MUTEX_PROTECTS_DATA(cpr_mutex, cprinfo))
1146 	_NOTE(NO_COMPETING_THREADS_NOW)
1147 	mutex_init(&cpr_mutex, NULL, MUTEX_DRIVER, NULL);
1148 	CALLB_CPR_INIT(&cprinfo, &cpr_mutex, callb_generic_cpr,
1149 	    "ibtl_cq_thread");
1150 #ifndef lint
1151 	_NOTE(COMPETING_THREADS_NOW)
1152 #endif
1153 
1154 	mutex_enter(&ibtl_cq_mutex);
1155 
1156 	for (;;) {
1157 		if (ibtl_cq_list_start) {
1158 			ibtl_cq_t *ibtl_cq;
1159 
1160 			ibtl_cq = ibtl_cq_list_start;
1161 			ibtl_cq_list_start = ibtl_cq->cq_link;
1162 			ibtl_cq->cq_link = NULL;
1163 			if (ibtl_cq == ibtl_cq_list_end)
1164 				ibtl_cq_list_end = NULL;
1165 
1166 			while (ibtl_cq->cq_impl_flags & IBTL_CQ_CALL_CLIENT) {
1167 				ibtl_cq->cq_impl_flags &= ~IBTL_CQ_CALL_CLIENT;
1168 				mutex_exit(&ibtl_cq_mutex);
1169 				ibtl_cq_handler_call(ibtl_cq);
1170 				mutex_enter(&ibtl_cq_mutex);
1171 			}
1172 			ibtl_cq->cq_impl_flags &= ~IBTL_CQ_PENDING;
1173 			if (ibtl_cq->cq_impl_flags & IBTL_CQ_FREE)
1174 				cv_broadcast(&ibtl_cq_cv);
1175 		} else {
1176 			if (ibtl_cq_thread_exit == IBTL_THREAD_EXIT)
1177 				break;
1178 			mutex_enter(&cpr_mutex);
1179 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
1180 			mutex_exit(&cpr_mutex);
1181 
1182 			cv_wait(&ibtl_cq_cv, &ibtl_cq_mutex);
1183 
1184 			mutex_exit(&ibtl_cq_mutex);
1185 			mutex_enter(&cpr_mutex);
1186 			CALLB_CPR_SAFE_END(&cprinfo, &cpr_mutex);
1187 			mutex_exit(&cpr_mutex);
1188 			mutex_enter(&ibtl_cq_mutex);
1189 		}
1190 	}
1191 
1192 	mutex_exit(&ibtl_cq_mutex);
1193 #ifndef __lock_lint
1194 	mutex_enter(&cpr_mutex);
1195 	CALLB_CPR_EXIT(&cprinfo);
1196 #endif
1197 	mutex_destroy(&cpr_mutex);
1198 }
1199 
1200 
1201 /*
1202  * ibc_cq_handler()
1203  *
1204  *    Completion Queue Notification Handler.
1205  *
1206  */
1207 /*ARGSUSED*/
1208 void
1209 ibc_cq_handler(ibc_clnt_hdl_t ibc_hdl, ibt_cq_hdl_t ibtl_cq)
1210 {
1211 	IBTF_DPRINTF_L4(ibtf_handlers, "ibc_cq_handler(%p, %p)",
1212 	    ibc_hdl, ibtl_cq);
1213 
1214 	if (ibtl_cq->cq_in_thread) {
1215 		mutex_enter(&ibtl_cq_mutex);
1216 		ibtl_cq->cq_impl_flags |= IBTL_CQ_CALL_CLIENT;
1217 		if ((ibtl_cq->cq_impl_flags & IBTL_CQ_PENDING) == 0) {
1218 			ibtl_cq->cq_impl_flags |= IBTL_CQ_PENDING;
1219 			ibtl_cq->cq_link = NULL;
1220 			if (ibtl_cq_list_end == NULL)
1221 				ibtl_cq_list_start = ibtl_cq;
1222 			else
1223 				ibtl_cq_list_end->cq_link = ibtl_cq;
1224 			ibtl_cq_list_end = ibtl_cq;
1225 			cv_signal(&ibtl_cq_cv);
1226 		}
1227 		mutex_exit(&ibtl_cq_mutex);
1228 		return;
1229 	} else
1230 		ibtl_cq_handler_call(ibtl_cq);
1231 }
1232 
1233 
1234 /*
1235  * ibt_enable_cq_notify()
1236  *      Enable Notification requests on the specified CQ.
1237  *
1238  *      ibt_cq          The CQ handle.
1239  *
1240  *      notify_type     Enable notifications for all (IBT_NEXT_COMPLETION)
1241  *                      completions, or the next Solicited completion
1242  *                      (IBT_NEXT_SOLICITED) only.
1243  *
1244  *	Completion notifications are disabled by setting the completion
1245  *	handler to NULL by calling ibt_set_cq_handler().
1246  */
1247 ibt_status_t
1248 ibt_enable_cq_notify(ibt_cq_hdl_t ibtl_cq, ibt_cq_notify_flags_t notify_type)
1249 {
1250 	IBTF_DPRINTF_L3(ibtf_handlers, "ibt_enable_cq_notify(%p, %d)",
1251 	    ibtl_cq, notify_type);
1252 
1253 	return (IBTL_CQ2CIHCAOPS_P(ibtl_cq)->ibc_notify_cq(
1254 	    IBTL_CQ2CIHCA(ibtl_cq), ibtl_cq->cq_ibc_cq_hdl, notify_type));
1255 }
1256 
1257 
1258 /*
1259  * ibt_set_cq_handler()
1260  *      Register a work request completion handler with the IBTF.
1261  *
1262  *      ibt_cq                  The CQ handle.
1263  *
1264  *      completion_handler      The completion handler.
1265  *
1266  *      arg                     The IBTF client private argument to be passed
1267  *                              back to the client when calling the CQ
1268  *                              completion handler.
1269  *
1270  *	Completion notifications are disabled by setting the completion
1271  *	handler to NULL.  When setting the handler to NULL, no additional
1272  *	calls to the previous CQ handler will be initiated, but there may
1273  *	be one in progress.
1274  *
1275  *      This function does not otherwise change the state of previous
1276  *      calls to ibt_enable_cq_notify().
1277  */
1278 void
1279 ibt_set_cq_handler(ibt_cq_hdl_t ibtl_cq, ibt_cq_handler_t completion_handler,
1280     void *arg)
1281 {
1282 	IBTF_DPRINTF_L3(ibtf_handlers, "ibt_set_cq_handler(%p, %p, %p)",
1283 	    ibtl_cq, completion_handler, arg);
1284 
1285 	mutex_enter(&ibtl_cq->cq_mutex);
1286 	ibtl_cq->cq_comp_handler = completion_handler;
1287 	ibtl_cq->cq_arg = arg;
1288 	mutex_exit(&ibtl_cq->cq_mutex);
1289 }
1290 
1291 
1292 /*
1293  * Inform IBT clients about New HCAs.
1294  *
1295  *	We use taskqs to allow simultaneous notification, with sleeping.
1296  *	Since taskqs only allow one argument, we define a structure
1297  *	because we need to pass in two arguments.
1298  */
1299 
1300 struct ibtl_new_hca_s {
1301 	ibtl_clnt_t		*nh_clntp;
1302 	ibtl_hca_devinfo_t	*nh_hca_devp;
1303 	ibt_async_code_t	nh_code;
1304 };
1305 
1306 static void
1307 ibtl_tell_client_about_new_hca(void *arg)
1308 {
1309 	struct ibtl_new_hca_s	*new_hcap = (struct ibtl_new_hca_s *)arg;
1310 	ibtl_clnt_t		*clntp = new_hcap->nh_clntp;
1311 	ibt_async_event_t	async_event;
1312 	ibtl_hca_devinfo_t	*hca_devp = new_hcap->nh_hca_devp;
1313 
1314 	bzero(&async_event, sizeof (async_event));
1315 	async_event.ev_hca_guid = hca_devp->hd_hca_attr->hca_node_guid;
1316 	clntp->clnt_modinfop->mi_async_handler(
1317 	    clntp->clnt_private, NULL, new_hcap->nh_code, &async_event);
1318 	kmem_free(new_hcap, sizeof (*new_hcap));
1319 #ifdef __lock_lint
1320 	{
1321 		ibt_hca_hdl_t hca_hdl;
1322 		(void) ibt_open_hca(clntp, 0ULL, &hca_hdl);
1323 	}
1324 #endif
1325 	mutex_enter(&ibtl_clnt_list_mutex);
1326 	if (--hca_devp->hd_async_task_cnt == 0)
1327 		cv_signal(&hca_devp->hd_async_task_cv);
1328 	if (--clntp->clnt_async_cnt == 0)
1329 		cv_broadcast(&ibtl_clnt_cv);
1330 	mutex_exit(&ibtl_clnt_list_mutex);
1331 }
1332 
1333 /*
1334  * ibtl_announce_new_hca:
1335  *
1336  *	o First attach these clients in the given order
1337  *		IBMA
1338  *		IBCM
1339  *
1340  *	o Next attach all other clients in parallel.
1341  *
1342  * NOTE: Use the taskq to simultaneously notify all clients of the new HCA.
1343  * Retval from clients is ignored.
1344  */
1345 void
1346 ibtl_announce_new_hca(ibtl_hca_devinfo_t *hca_devp)
1347 {
1348 	ibtl_clnt_t		*clntp;
1349 	struct ibtl_new_hca_s	*new_hcap;
1350 
1351 	IBTF_DPRINTF_L3(ibtf_handlers, "ibtl_announce_new_hca(%p, %llX)",
1352 	    hca_devp, hca_devp->hd_hca_attr->hca_node_guid);
1353 
1354 	mutex_enter(&ibtl_clnt_list_mutex);
1355 
1356 	clntp = ibtl_clnt_list;
1357 	while (clntp != NULL) {
1358 		if (clntp->clnt_modinfop->mi_clnt_class == IBT_IBMA) {
1359 			IBTF_DPRINTF_L4(ibtf_handlers,
1360 			    "ibtl_announce_new_hca: calling IBMF");
1361 			if (clntp->clnt_modinfop->mi_async_handler) {
1362 				_NOTE(NO_COMPETING_THREADS_NOW)
1363 				new_hcap = kmem_alloc(sizeof (*new_hcap),
1364 				    KM_SLEEP);
1365 				new_hcap->nh_clntp = clntp;
1366 				new_hcap->nh_hca_devp = hca_devp;
1367 				new_hcap->nh_code = IBT_HCA_ATTACH_EVENT;
1368 #ifndef lint
1369 				_NOTE(COMPETING_THREADS_NOW)
1370 #endif
1371 				clntp->clnt_async_cnt++;
1372 				hca_devp->hd_async_task_cnt++;
1373 
1374 				(void) taskq_dispatch(ibtl_async_taskq,
1375 				    ibtl_tell_client_about_new_hca, new_hcap,
1376 				    TQ_SLEEP);
1377 			}
1378 			break;
1379 		}
1380 		clntp = clntp->clnt_list_link;
1381 	}
1382 	if (clntp != NULL)
1383 		while (clntp->clnt_async_cnt > 0)
1384 			cv_wait(&ibtl_clnt_cv, &ibtl_clnt_list_mutex);
1385 	clntp = ibtl_clnt_list;
1386 	while (clntp != NULL) {
1387 		if (clntp->clnt_modinfop->mi_clnt_class == IBT_DM) {
1388 			IBTF_DPRINTF_L4(ibtf_handlers,
1389 			    "ibtl_announce_new_hca: calling IBDM");
1390 			if (clntp->clnt_modinfop->mi_async_handler) {
1391 				_NOTE(NO_COMPETING_THREADS_NOW)
1392 				new_hcap = kmem_alloc(sizeof (*new_hcap),
1393 				    KM_SLEEP);
1394 				new_hcap->nh_clntp = clntp;
1395 				new_hcap->nh_hca_devp = hca_devp;
1396 				new_hcap->nh_code = IBT_HCA_ATTACH_EVENT;
1397 #ifndef lint
1398 				_NOTE(COMPETING_THREADS_NOW)
1399 #endif
1400 				clntp->clnt_async_cnt++;
1401 				hca_devp->hd_async_task_cnt++;
1402 
1403 				(void) taskq_dispatch(ibtl_async_taskq,
1404 				    ibtl_tell_client_about_new_hca, new_hcap,
1405 				    TQ_SLEEP);
1406 			}
1407 			break;
1408 		}
1409 		clntp = clntp->clnt_list_link;
1410 	}
1411 	if (clntp != NULL)
1412 		while (clntp->clnt_async_cnt > 0)
1413 			cv_wait(&ibtl_clnt_cv, &ibtl_clnt_list_mutex);
1414 	clntp = ibtl_clnt_list;
1415 	while (clntp != NULL) {
1416 		if ((clntp->clnt_modinfop->mi_clnt_class != IBT_DM) &&
1417 		    (clntp->clnt_modinfop->mi_clnt_class != IBT_IBMA)) {
1418 			IBTF_DPRINTF_L4(ibtf_handlers,
1419 			    "ibtl_announce_new_hca: Calling %s ",
1420 			    clntp->clnt_modinfop->mi_clnt_name);
1421 			if (clntp->clnt_modinfop->mi_async_handler) {
1422 				_NOTE(NO_COMPETING_THREADS_NOW)
1423 				new_hcap = kmem_alloc(sizeof (*new_hcap),
1424 				    KM_SLEEP);
1425 				new_hcap->nh_clntp = clntp;
1426 				new_hcap->nh_hca_devp = hca_devp;
1427 				new_hcap->nh_code = IBT_HCA_ATTACH_EVENT;
1428 #ifndef lint
1429 				_NOTE(COMPETING_THREADS_NOW)
1430 #endif
1431 				clntp->clnt_async_cnt++;
1432 				hca_devp->hd_async_task_cnt++;
1433 
1434 				(void) taskq_dispatch(ibtl_async_taskq,
1435 				    ibtl_tell_client_about_new_hca, new_hcap,
1436 				    TQ_SLEEP);
1437 			}
1438 		}
1439 		clntp = clntp->clnt_list_link;
1440 	}
1441 
1442 	/* wait for all tasks to complete */
1443 	while (hca_devp->hd_async_task_cnt != 0)
1444 		cv_wait(&hca_devp->hd_async_task_cv, &ibtl_clnt_list_mutex);
1445 
1446 	/* wakeup thread that may be waiting to send an HCA async */
1447 	ASSERT(hca_devp->hd_async_busy == 1);
1448 	hca_devp->hd_async_busy = 0;
1449 	cv_broadcast(&hca_devp->hd_async_busy_cv);
1450 	mutex_exit(&ibtl_clnt_list_mutex);
1451 }
1452 
1453 /*
1454  * ibtl_detach_all_clients:
1455  *
1456  *	Return value - 0 for Success, 1 for Failure
1457  *
1458  *	o First detach general clients.
1459  *
1460  *	o Next detach these clients
1461  *		IBCM
1462  *		IBDM
1463  *
1464  *	o Finally, detach this client
1465  *		IBMA
1466  */
1467 int
1468 ibtl_detach_all_clients(ibtl_hca_devinfo_t *hca_devp)
1469 {
1470 	ib_guid_t		hcaguid = hca_devp->hd_hca_attr->hca_node_guid;
1471 	ibtl_hca_t		*ibt_hca;
1472 	ibtl_clnt_t		*clntp;
1473 	int			retval;
1474 
1475 	IBTF_DPRINTF_L2(ibtf_handlers, "ibtl_detach_all_clients(%llX)",
1476 	    hcaguid);
1477 
1478 	ASSERT(MUTEX_HELD(&ibtl_clnt_list_mutex));
1479 
1480 	while (hca_devp->hd_async_busy)
1481 		cv_wait(&hca_devp->hd_async_busy_cv, &ibtl_clnt_list_mutex);
1482 	hca_devp->hd_async_busy = 1;
1483 
1484 	/* First inform general clients asynchronously */
1485 	hca_devp->hd_async_event.ev_hca_guid = hcaguid;
1486 	hca_devp->hd_async_event.ev_fma_ena = 0;
1487 	hca_devp->hd_async_event.ev_chan_hdl = NULL;
1488 	hca_devp->hd_async_event.ev_cq_hdl = NULL;
1489 	hca_devp->hd_async_code = IBT_HCA_DETACH_EVENT;
1490 
1491 	ibt_hca = hca_devp->hd_clnt_list;
1492 	while (ibt_hca != NULL) {
1493 		clntp = ibt_hca->ha_clnt_devp;
1494 		if (IBTL_GENERIC_CLIENT(clntp)) {
1495 			++ibt_hca->ha_clnt_devp->clnt_async_cnt;
1496 			mutex_enter(&ibtl_async_mutex);
1497 			ibt_hca->ha_async_cnt++;
1498 			mutex_exit(&ibtl_async_mutex);
1499 			hca_devp->hd_async_task_cnt++;
1500 
1501 			(void) taskq_dispatch(ibtl_async_taskq,
1502 			    ibtl_hca_client_async_task, ibt_hca, TQ_SLEEP);
1503 		}
1504 		ibt_hca = ibt_hca->ha_clnt_link;
1505 	}
1506 
1507 	/* wait for all clients to complete */
1508 	while (hca_devp->hd_async_task_cnt != 0) {
1509 		cv_wait(&hca_devp->hd_async_task_cv, &ibtl_clnt_list_mutex);
1510 	}
1511 	/* Go thru the clients and check if any have not closed this HCA. */
1512 	retval = 0;
1513 	ibt_hca = hca_devp->hd_clnt_list;
1514 	while (ibt_hca != NULL) {
1515 		clntp = ibt_hca->ha_clnt_devp;
1516 		if (IBTL_GENERIC_CLIENT(clntp)) {
1517 			IBTF_DPRINTF_L2(ibtf_handlers,
1518 			    "ibtl_detach_all_clients: "
1519 			    "client '%s' failed to close the HCA.",
1520 			    ibt_hca->ha_clnt_devp->clnt_modinfop->mi_clnt_name);
1521 			retval = 1;
1522 		}
1523 		ibt_hca = ibt_hca->ha_clnt_link;
1524 	}
1525 	if (retval == 1)
1526 		goto bailout;
1527 
1528 	/* Next inform IBDM asynchronously */
1529 	ibt_hca = hca_devp->hd_clnt_list;
1530 	while (ibt_hca != NULL) {
1531 		clntp = ibt_hca->ha_clnt_devp;
1532 		if (clntp->clnt_modinfop->mi_clnt_class == IBT_DM) {
1533 			++ibt_hca->ha_clnt_devp->clnt_async_cnt;
1534 			mutex_enter(&ibtl_async_mutex);
1535 			ibt_hca->ha_async_cnt++;
1536 			mutex_exit(&ibtl_async_mutex);
1537 			hca_devp->hd_async_task_cnt++;
1538 
1539 			(void) taskq_dispatch(ibtl_async_taskq,
1540 			    ibtl_hca_client_async_task, ibt_hca, TQ_SLEEP);
1541 		}
1542 		ibt_hca = ibt_hca->ha_clnt_link;
1543 	}
1544 	/* wait for IBDM to complete */
1545 	while (hca_devp->hd_async_task_cnt != 0) {
1546 		cv_wait(&hca_devp->hd_async_task_cv, &ibtl_clnt_list_mutex);
1547 	}
1548 
1549 	/*
1550 	 * Next inform IBCM.
1551 	 * As IBCM doesn't perform ibt_open_hca(), IBCM will not be
1552 	 * accessible via hca_devp->hd_clnt_list.
1553 	 * ibtl_cm_async_handler will NOT be NULL, if IBCM is registered.
1554 	 */
1555 	if (ibtl_cm_async_handler) {
1556 		ibtl_tell_mgr(hca_devp, ibtl_cm_async_handler,
1557 		    ibtl_cm_clnt_private);
1558 
1559 		/* wait for all tasks to complete */
1560 		while (hca_devp->hd_async_task_cnt != 0)
1561 			cv_wait(&hca_devp->hd_async_task_cv,
1562 			    &ibtl_clnt_list_mutex);
1563 	}
1564 
1565 	/* Go thru the clients and check if any have not closed this HCA. */
1566 	retval = 0;
1567 	ibt_hca = hca_devp->hd_clnt_list;
1568 	while (ibt_hca != NULL) {
1569 		clntp = ibt_hca->ha_clnt_devp;
1570 		if (clntp->clnt_modinfop->mi_clnt_class != IBT_IBMA) {
1571 			IBTF_DPRINTF_L2(ibtf_handlers,
1572 			    "ibtl_detach_all_clients: "
1573 			    "client '%s' failed to close the HCA.",
1574 			    ibt_hca->ha_clnt_devp->clnt_modinfop->mi_clnt_name);
1575 			retval = 1;
1576 		}
1577 		ibt_hca = ibt_hca->ha_clnt_link;
1578 	}
1579 	if (retval == 1)
1580 		goto bailout;
1581 
1582 	/* Finally, inform IBMA */
1583 	ibt_hca = hca_devp->hd_clnt_list;
1584 	while (ibt_hca != NULL) {
1585 		clntp = ibt_hca->ha_clnt_devp;
1586 		if (clntp->clnt_modinfop->mi_clnt_class == IBT_IBMA) {
1587 			++ibt_hca->ha_clnt_devp->clnt_async_cnt;
1588 			mutex_enter(&ibtl_async_mutex);
1589 			ibt_hca->ha_async_cnt++;
1590 			mutex_exit(&ibtl_async_mutex);
1591 			hca_devp->hd_async_task_cnt++;
1592 
1593 			(void) taskq_dispatch(ibtl_async_taskq,
1594 			    ibtl_hca_client_async_task, ibt_hca, TQ_SLEEP);
1595 		} else
1596 			IBTF_DPRINTF_L2(ibtf_handlers,
1597 			    "ibtl_detach_all_clients: "
1598 			    "client '%s' is unexpectedly on the client list",
1599 			    ibt_hca->ha_clnt_devp->clnt_modinfop->mi_clnt_name);
1600 		ibt_hca = ibt_hca->ha_clnt_link;
1601 	}
1602 
1603 	/* wait for IBMA to complete */
1604 	while (hca_devp->hd_async_task_cnt != 0) {
1605 		cv_wait(&hca_devp->hd_async_task_cv, &ibtl_clnt_list_mutex);
1606 	}
1607 
1608 	/* Check if this HCA's client list is empty. */
1609 	ibt_hca = hca_devp->hd_clnt_list;
1610 	if (ibt_hca != NULL) {
1611 		IBTF_DPRINTF_L2(ibtf_handlers,
1612 		    "ibtl_detach_all_clients: "
1613 		    "client '%s' failed to close the HCA.",
1614 		    ibt_hca->ha_clnt_devp->clnt_modinfop->mi_clnt_name);
1615 		retval = 1;
1616 	} else
1617 		retval = 0;
1618 
1619 bailout:
1620 	hca_devp->hd_async_busy = 0;
1621 	cv_broadcast(&hca_devp->hd_async_busy_cv);
1622 	return (retval);
1623 }
1624 
1625 void
1626 ibtl_free_clnt_async_check(ibtl_clnt_t *clntp)
1627 {
1628 	IBTF_DPRINTF_L3(ibtf_handlers, "ibtl_free_clnt_async_check(%p)", clntp);
1629 
1630 	ASSERT(MUTEX_HELD(&ibtl_clnt_list_mutex));
1631 
1632 	/* wait for all asyncs based on "ibtl_clnt_list" to complete */
1633 	while (clntp->clnt_async_cnt != 0) {
1634 		cv_wait(&ibtl_clnt_cv, &ibtl_clnt_list_mutex);
1635 	}
1636 }
1637 
1638 static void
1639 ibtl_dec_clnt_async_cnt(ibtl_clnt_t *clntp)
1640 {
1641 	mutex_enter(&ibtl_clnt_list_mutex);
1642 	if (--clntp->clnt_async_cnt == 0) {
1643 		cv_broadcast(&ibtl_clnt_cv);
1644 	}
1645 	mutex_exit(&ibtl_clnt_list_mutex);
1646 }
1647 
1648 static void
1649 ibtl_inc_clnt_async_cnt(ibtl_clnt_t *clntp)
1650 {
1651 	mutex_enter(&ibtl_clnt_list_mutex);
1652 	++clntp->clnt_async_cnt;
1653 	mutex_exit(&ibtl_clnt_list_mutex);
1654 }
1655 
1656 
1657 /*
1658  * Functions and data structures to inform clients that a notification
1659  * has occurred about Multicast Groups that might interest them.
1660  */
1661 struct ibtl_sm_notice {
1662 	ibt_clnt_hdl_t		np_ibt_hdl;
1663 	ib_gid_t		np_sgid;
1664 	ibt_subnet_event_code_t	np_code;
1665 	ibt_subnet_event_t	np_event;
1666 };
1667 
1668 static void
1669 ibtl_sm_notice_task(void *arg)
1670 {
1671 	struct ibtl_sm_notice *noticep = (struct ibtl_sm_notice *)arg;
1672 	ibt_clnt_hdl_t ibt_hdl = noticep->np_ibt_hdl;
1673 	ibt_sm_notice_handler_t sm_notice_handler;
1674 
1675 	sm_notice_handler = ibt_hdl->clnt_sm_trap_handler;
1676 	if (sm_notice_handler != NULL)
1677 		sm_notice_handler(ibt_hdl->clnt_sm_trap_handler_arg,
1678 		    noticep->np_sgid, noticep->np_code, &noticep->np_event);
1679 	kmem_free(noticep, sizeof (*noticep));
1680 	ibtl_dec_clnt_async_cnt(ibt_hdl);
1681 }
1682 
1683 /*
1684  * Inform the client that MCG notices are not working at this time.
1685  */
1686 void
1687 ibtl_cm_sm_notice_init_failure(ibtl_cm_sm_init_fail_t *ifail)
1688 {
1689 	ibt_clnt_hdl_t ibt_hdl = ifail->smf_ibt_hdl;
1690 	struct ibtl_sm_notice *noticep;
1691 	ib_gid_t *sgidp = &ifail->smf_sgid[0];
1692 	int i;
1693 
1694 	for (i = 0; i < ifail->smf_num_sgids; i++) {
1695 		_NOTE(NO_COMPETING_THREADS_NOW)
1696 		noticep = kmem_zalloc(sizeof (*noticep), KM_SLEEP);
1697 		noticep->np_ibt_hdl = ibt_hdl;
1698 		noticep->np_sgid = *sgidp++;
1699 		noticep->np_code = IBT_SM_EVENT_UNAVAILABLE;
1700 #ifndef lint
1701 		_NOTE(COMPETING_THREADS_NOW)
1702 #endif
1703 		ibtl_inc_clnt_async_cnt(ibt_hdl);
1704 		(void) taskq_dispatch(ibtl_async_taskq,
1705 		    ibtl_sm_notice_task, noticep, TQ_SLEEP);
1706 	}
1707 }
1708 
1709 /*
1710  * Inform all clients of the event.
1711  */
1712 void
1713 ibtl_cm_sm_notice_handler(ib_gid_t sgid, ibt_subnet_event_code_t code,
1714     ibt_subnet_event_t *event)
1715 {
1716 	_NOTE(NO_COMPETING_THREADS_NOW)
1717 	struct ibtl_sm_notice	*noticep;
1718 	ibtl_clnt_t		*clntp;
1719 
1720 	mutex_enter(&ibtl_clnt_list_mutex);
1721 	clntp = ibtl_clnt_list;
1722 	while (clntp != NULL) {
1723 		if (clntp->clnt_sm_trap_handler) {
1724 			noticep = kmem_zalloc(sizeof (*noticep), KM_SLEEP);
1725 			noticep->np_ibt_hdl = clntp;
1726 			noticep->np_sgid = sgid;
1727 			noticep->np_code = code;
1728 			noticep->np_event = *event;
1729 			++clntp->clnt_async_cnt;
1730 			(void) taskq_dispatch(ibtl_async_taskq,
1731 			    ibtl_sm_notice_task, noticep, TQ_SLEEP);
1732 		}
1733 		clntp = clntp->clnt_list_link;
1734 	}
1735 	mutex_exit(&ibtl_clnt_list_mutex);
1736 #ifndef lint
1737 	_NOTE(COMPETING_THREADS_NOW)
1738 #endif
1739 }
1740 
1741 /*
1742  * Record the handler for this client.
1743  */
1744 void
1745 ibtl_cm_set_sm_notice_handler(ibt_clnt_hdl_t ibt_hdl,
1746     ibt_sm_notice_handler_t sm_notice_handler, void *private)
1747 {
1748 	_NOTE(NO_COMPETING_THREADS_NOW)
1749 	ibt_hdl->clnt_sm_trap_handler = sm_notice_handler;
1750 	ibt_hdl->clnt_sm_trap_handler_arg = private;
1751 #ifndef lint
1752 	_NOTE(COMPETING_THREADS_NOW)
1753 #endif
1754 }
1755 
1756 
1757 /*
1758  * ibtl_another_cq_handler_in_thread()
1759  *
1760  * Conditionally increase the number of cq_threads.
1761  * The number of threads grows, based on the number of cqs using threads.
1762  *
1763  * The table below controls the number of threads as follows:
1764  *
1765  *	Number of CQs	Number of cq_threads
1766  *		0		0
1767  *		1		1
1768  *		2-3		2
1769  *		4-5		3
1770  *		6-9		4
1771  *		10-15		5
1772  *		16-23		6
1773  *		24-31		7
1774  *		32+		8
1775  */
1776 
1777 #define	IBTL_CQ_MAXTHREADS 8
1778 static uint8_t ibtl_cq_scaling[IBTL_CQ_MAXTHREADS] = {
1779 	1, 2, 4, 6, 10, 16, 24, 32
1780 };
1781 
1782 static kt_did_t ibtl_cq_did[IBTL_CQ_MAXTHREADS];
1783 
1784 void
1785 ibtl_another_cq_handler_in_thread(void)
1786 {
1787 	kthread_t *t;
1788 	int my_idx;
1789 
1790 	mutex_enter(&ibtl_cq_mutex);
1791 	if ((ibtl_cq_threads == IBTL_CQ_MAXTHREADS) ||
1792 	    (++ibtl_cqs_using_threads < ibtl_cq_scaling[ibtl_cq_threads])) {
1793 		mutex_exit(&ibtl_cq_mutex);
1794 		return;
1795 	}
1796 	my_idx = ibtl_cq_threads++;
1797 	mutex_exit(&ibtl_cq_mutex);
1798 	t = thread_create(NULL, 0, ibtl_cq_thread, NULL, 0, &p0, TS_RUN,
1799 	    ibtl_pri - 1);
1800 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(ibtl_cq_did))
1801 	ibtl_cq_did[my_idx] = t->t_did;	/* save for thread_join() */
1802 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(ibtl_cq_did))
1803 }
1804 
1805 void
1806 ibtl_thread_init(void)
1807 {
1808 	IBTF_DPRINTF_L3(ibtf_handlers, "ibtl_thread_init()");
1809 
1810 	mutex_init(&ibtl_async_mutex, NULL, MUTEX_DEFAULT, NULL);
1811 	cv_init(&ibtl_async_cv, NULL, CV_DEFAULT, NULL);
1812 	cv_init(&ibtl_clnt_cv, NULL, CV_DEFAULT, NULL);
1813 
1814 	mutex_init(&ibtl_cq_mutex, NULL, MUTEX_DEFAULT, NULL);
1815 	cv_init(&ibtl_cq_cv, NULL, CV_DEFAULT, NULL);
1816 }
1817 
1818 void
1819 ibtl_thread_init2(void)
1820 {
1821 	int i;
1822 	static int initted = 0;
1823 	kthread_t *t;
1824 
1825 	mutex_enter(&ibtl_async_mutex);
1826 	if (initted == 1) {
1827 		mutex_exit(&ibtl_async_mutex);
1828 		return;
1829 	}
1830 	initted = 1;
1831 	mutex_exit(&ibtl_async_mutex);
1832 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(ibtl_async_did))
1833 	ibtl_async_did = kmem_zalloc(ibtl_async_thread_init * sizeof (kt_did_t),
1834 	    KM_SLEEP);
1835 
1836 	IBTF_DPRINTF_L3(ibtf_handlers, "ibtl_thread_init2()");
1837 
1838 	for (i = 0; i < ibtl_async_thread_init; i++) {
1839 		t = thread_create(NULL, 0, ibtl_async_thread, NULL, 0, &p0,
1840 		    TS_RUN, ibtl_pri - 1);
1841 		ibtl_async_did[i] = t->t_did; /* thread_join() */
1842 	}
1843 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(ibtl_async_did))
1844 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(ibtl_cq_threads))
1845 	for (i = 0; i < ibtl_cq_threads; i++) {
1846 		t = thread_create(NULL, 0, ibtl_cq_thread, NULL, 0, &p0,
1847 		    TS_RUN, ibtl_pri - 1);
1848 		_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(ibtl_cq_did))
1849 		ibtl_cq_did[i] = t->t_did; /* save for thread_join() */
1850 		_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(ibtl_cq_did))
1851 	}
1852 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(ibtl_cq_threads))
1853 }
1854 
1855 void
1856 ibtl_thread_fini(void)
1857 {
1858 	int i;
1859 
1860 	IBTF_DPRINTF_L3(ibtf_handlers, "ibtl_thread_fini()");
1861 
1862 	/* undo the work done by ibtl_thread_init() */
1863 
1864 	mutex_enter(&ibtl_cq_mutex);
1865 	ibtl_cq_thread_exit = IBTL_THREAD_EXIT;
1866 	cv_broadcast(&ibtl_cq_cv);
1867 	mutex_exit(&ibtl_cq_mutex);
1868 
1869 	mutex_enter(&ibtl_async_mutex);
1870 	ibtl_async_thread_exit = IBTL_THREAD_EXIT;
1871 	cv_broadcast(&ibtl_async_cv);
1872 	mutex_exit(&ibtl_async_mutex);
1873 
1874 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(ibtl_cq_threads))
1875 	for (i = 0; i < ibtl_cq_threads; i++)
1876 		thread_join(ibtl_cq_did[i]);
1877 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(ibtl_cq_threads))
1878 
1879 	if (ibtl_async_did) {
1880 		for (i = 0; i < ibtl_async_thread_init; i++)
1881 			thread_join(ibtl_async_did[i]);
1882 
1883 		kmem_free(ibtl_async_did,
1884 		    ibtl_async_thread_init * sizeof (kt_did_t));
1885 	}
1886 	mutex_destroy(&ibtl_cq_mutex);
1887 	cv_destroy(&ibtl_cq_cv);
1888 
1889 	mutex_destroy(&ibtl_async_mutex);
1890 	cv_destroy(&ibtl_async_cv);
1891 	cv_destroy(&ibtl_clnt_cv);
1892 }
1893