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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * ibtl_impl.c
31  *
32  * This file contains the IBTF module's initialization and
33  * IBTF Clients/Modules registration routines.
34  */
35 
36 #include <sys/modctl.h>
37 #include <sys/sunndi.h>
38 #include <sys/sunmdi.h>
39 #include <sys/ib/ibtl/impl/ibtl.h>
40 #include <sys/ib/ibtl/impl/ibtl_ibnex.h>
41 
42 /*
43  * Globals.
44  */
45 static char ibtf[] = "ibtl_impl";
46 
47 extern ibtl_ibnex_callback_t	ibtl_ibnex_callback_routine;
48 
49 /*
50  * ibtl_clnt_list:
51  *
52  *	Head of the list of IBT Client Instances. The IBT Client List
53  *	is modified by IBTF on an IBT client's ibt_attach/ibt_detach call.
54  *
55  * ibtl_hca_list:
56  *
57  *	Head of the list of HCA devices. The HCA List is modified by IBTF on
58  *	a CI's ibc_attach/ibc_detach call.
59  *	The datap of the list elements points to an ibtl_hca_devinfo_s
60  *	structure.
61  *
62  *				(ibc_attach)
63  *  ibtl_hca_list	-> ibtl_hca_devinfo_t--> ...	-->ibtl_hca_devinfo_t
64  *	[per-hca_dev]		|	^			{nth HCA Dev}
65  *				|	|
66  *				|  ibtl_hca_t (ibt_open_hca)
67  *				|	^  |
68  *				|	|  |
69  *				v	|  V
70  *  ibtl_clnt_list	->	ibtl_clnt_t--> ...--> {n'th Module}
71  *	[per-client_instance]	(ibt_attach)
72  *
73  */
74 
75 /* Global List of IBT Client Instances, and associated mutex. */
76 struct ibtl_clnt_s *ibtl_clnt_list = NULL;
77 kmutex_t ibtl_clnt_list_mutex;
78 
79 /* Lock for the race between the client and CM to free QPs. */
80 kmutex_t ibtl_free_qp_mutex;
81 
82 /* Lock for the race between the client closing the HCA and QPN being freed. */
83 kcondvar_t ibtl_close_hca_cv;
84 
85 /* Global List of HCA Devices, and associated mutex. */
86 struct ibtl_hca_devinfo_s *ibtl_hca_list = NULL;
87 
88 /* Well-known async handlers and associated client private. */
89 ibt_async_handler_t ibtl_cm_async_handler;
90 ibt_async_handler_t ibtl_dm_async_handler;
91 ibt_async_handler_t ibtl_ibma_async_handler;
92 void	*ibtl_cm_clnt_private;
93 void	*ibtl_dm_clnt_private;
94 void	*ibtl_ibma_clnt_private;
95 
96 extern int ib_hw_status;
97 _NOTE(SCHEME_PROTECTS_DATA("Scheme protects data", ib_hw_status))
98 
99 /*
100  * Misc Module Declarations.
101  */
102 extern struct mod_ops mod_miscops;
103 static struct modlmisc modlmisc = {
104 	&mod_miscops,			/* Type of module - misc. */
105 	"IB Transport Layer v%I%"	/* Name of the Module. */
106 };
107 
108 static struct modlinkage modlinkage = {
109 	MODREV_1, (void *)&modlmisc, NULL
110 };
111 
112 
113 /*
114  * IBTF Loadable Module Routines.
115  */
116 
117 int
118 _init(void)
119 {
120 	int rval;
121 
122 	if ((rval = mod_install(&modlinkage)) != 0)
123 		return (rval);
124 
125 	/*
126 	 * initialize IBTL ib2usec table
127 	 */
128 	ibtl_ib2usec_init();
129 
130 	/*
131 	 * Initialize Logging
132 	 */
133 	ibtl_logging_initialization();
134 
135 	/*
136 	 * Initialize the Alloc QP States.
137 	 */
138 	ibtl_init_cep_states();
139 
140 	/*
141 	 * Initialize all Global Link Lists.
142 	 */
143 	mutex_init(&ibtl_clnt_list_mutex, NULL, MUTEX_DEFAULT, NULL);
144 	mutex_init(&ibtl_free_qp_mutex, NULL, MUTEX_DEFAULT, NULL);
145 	cv_init(&ibtl_close_hca_cv, NULL, CV_DEFAULT, NULL);
146 
147 	mutex_init(&ibtl_qp_mutex, NULL, MUTEX_DEFAULT, NULL);
148 	cv_init(&ibtl_qp_cv, NULL, CV_DEFAULT, NULL);
149 
150 	ibtl_thread_init();
151 
152 	return (rval);
153 }
154 
155 
156 /*
157  * The IBTF Module is never unloaded. Actually there is no need of this
158  * routine, but provided just in case.
159  */
160 int
161 _fini(void)
162 {
163 	int rval;
164 
165 	if ((rval = mod_remove(&modlinkage)) != 0) {
166 		return (rval);
167 	}
168 
169 	ibtl_thread_fini();
170 
171 	mutex_destroy(&ibtl_clnt_list_mutex);
172 	mutex_destroy(&ibtl_free_qp_mutex);
173 	cv_destroy(&ibtl_close_hca_cv);
174 	mutex_destroy(&ibtl_qp_mutex);
175 	cv_destroy(&ibtl_qp_cv);
176 
177 	/*
178 	 * Stop Logging
179 	 */
180 	ibtl_logging_destroy();
181 
182 	return (rval);
183 }
184 
185 
186 int
187 _info(struct modinfo *modinfop)
188 {
189 	/* Return the Module Information. */
190 	return (mod_info(&modlinkage, modinfop));
191 }
192 
193 
194 /*
195  * IBTF Client Registration Routines.
196  */
197 
198 /*
199  * Function:
200  *	ibt_attach
201  * Input:
202  *	modinfop	- Client Module info structure.
203  *	arg		- usually client's dip
204  *	clnt_private	- client's private data pointer.
205  * Output:
206  *	ibt_hdl_p	- pointer to client's specific IBT handle,
207  *			 which is opaque to clients.
208  * Returns:
209  *	IBT_SUCCESS
210  *	IBT_INVALID_PARAM
211  * Called by:
212  *	IBTF Client module during its attach() to register its instance
213  *	to IBTF.
214  * Description:
215  *	Registers the IBTF client module instance and returns an opaque
216  *	handler to the client to be used for future calls to IBTF.
217  *	Adds this client module instance to ibtl_clnt_list list.
218  *	Records well-known async handlers.
219  */
220 ibt_status_t
221 ibt_attach(ibt_clnt_modinfo_t *mod_infop, dev_info_t *arg, void *clnt_private,
222     ibt_clnt_hdl_t *ibt_hdl_p)
223 {
224 	dev_info_t	*pdip;
225 	ibtl_clnt_t	*clntp;
226 
227 	IBTF_DPRINTF_L3(ibtf, "ibt_attach(%p, %p, %p)",
228 	    mod_infop, arg, clnt_private);
229 
230 	if (mod_infop->mi_clnt_name == NULL) {
231 		IBTF_DPRINTF_L1(ibtf, "ibt_attach: "
232 		    "IB client needs to specify its name");
233 		return (IBT_INVALID_PARAM);
234 	}
235 
236 	/*
237 	 * Validate the Transport API version.
238 	 */
239 	if (mod_infop->mi_ibt_version != IBTI_V2) {
240 		IBTF_DPRINTF_L1(ibtf, "ibt_attach: IB client '%s' has an "
241 		    "invalid IB TI Version '%d'", mod_infop->mi_clnt_name,
242 		    mod_infop->mi_ibt_version);
243 		return (IBT_NOT_SUPPORTED);
244 	}
245 
246 	if (mod_infop->mi_async_handler == NULL) {
247 		IBTF_DPRINTF_L2(ibtf, "ibt_attach: Client '%s' has not\n"
248 		    "        provided an Asynchronous Event Handler.\n"
249 		    "        This will be required soon.",
250 		    mod_infop->mi_clnt_name);
251 	}
252 
253 	/*
254 	 * Check out Client's Class information. If it is not of mgmt class,
255 	 * we expect 'arg' to be Not NULL and point to client driver's
256 	 * device info struct.
257 	 */
258 	if ((!IBT_CLNT_MGMT_CLASS(mod_infop->mi_clnt_class)) &&
259 	    (arg == NULL)) {
260 		IBTF_DPRINTF_L1(ibtf, "ibt_attach: "
261 		    "arg not set with driver's dip.");
262 		return (IBT_INVALID_PARAM);
263 	}
264 
265 	if (!IBT_CLNT_MGMT_CLASS(mod_infop->mi_clnt_class)) {
266 		pdip = ddi_get_parent(arg);
267 		if (pdip == NULL ||
268 		    ibtl_ibnex_valid_hca_parent(pdip) != IBT_SUCCESS) {
269 			IBTF_DPRINTF_L2(ibtf, "ibt_attach: "
270 			    "client %s is not a child of IB nexus driver.",
271 			    ddi_driver_name(arg));
272 			return (IBT_INVALID_PARAM);
273 		}
274 	}
275 
276 	mutex_enter(&ibtl_clnt_list_mutex);
277 	if (mod_infop->mi_clnt_class == IBT_CM) {
278 		if (ibtl_cm_async_handler != NULL) {
279 			IBTF_DPRINTF_L1(ibtf, "ibt_attach: "
280 			    "CM is already attached.");
281 			mutex_exit(&ibtl_clnt_list_mutex);
282 			return (IBT_INVALID_PARAM);
283 		}
284 		ibtl_cm_async_handler = mod_infop->mi_async_handler;
285 		ibtl_cm_clnt_private = clnt_private;
286 	} else if (mod_infop->mi_clnt_class == IBT_DM) {
287 		if (ibtl_dm_async_handler != NULL) {
288 			IBTF_DPRINTF_L1(ibtf, "ibt_attach: "
289 			    "DM is already attached.");
290 			mutex_exit(&ibtl_clnt_list_mutex);
291 			return (IBT_INVALID_PARAM);
292 		}
293 		ibtl_dm_async_handler = mod_infop->mi_async_handler;
294 		ibtl_dm_clnt_private = clnt_private;
295 	} else if (mod_infop->mi_clnt_class == IBT_IBMA) {
296 		if (ibtl_ibma_async_handler != NULL) {
297 			IBTF_DPRINTF_L1(ibtf, "ibt_attach: "
298 			    "IBMF is already attached.");
299 			mutex_exit(&ibtl_clnt_list_mutex);
300 			return (IBT_INVALID_PARAM);
301 		}
302 		ibtl_ibma_async_handler = mod_infop->mi_async_handler;
303 		ibtl_ibma_clnt_private = clnt_private;
304 	}
305 
306 	/* Allocate the memory for per-client-device info structure */
307 	clntp = kmem_zalloc(sizeof (ibtl_clnt_t), KM_SLEEP);
308 
309 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(clntp->clnt_modinfop,
310 	    clntp->clnt_dip, clntp->clnt_name, clntp->clnt_async_cnt,
311 	    clntp->clnt_private))
312 	/* Update the Client info structure */
313 	clntp->clnt_modinfop = mod_infop;	/* IBT Client's Mod Info */
314 	clntp->clnt_private = clnt_private;	/* IBT Client's private */
315 	clntp->clnt_dip = arg;			/* IBT Client's dip */
316 	clntp->clnt_async_cnt = 0;
317 	/* using a count of 7 below guarantees it is NULL terminated */
318 	(void) strncpy(clntp->clnt_name, mod_infop->mi_clnt_name, 7);
319 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(clntp->clnt_modinfop,
320 	    clntp->clnt_dip, clntp->clnt_name, clntp->clnt_async_cnt,
321 	    clntp->clnt_private))
322 
323 	/*
324 	 * Update Client Device Instance List.
325 	 */
326 	clntp->clnt_list_link = ibtl_clnt_list;
327 	ibtl_clnt_list = clntp;
328 	mutex_exit(&ibtl_clnt_list_mutex);
329 
330 	/*
331 	 * The ibt_hdl_p is a opaque handle which is the address of
332 	 * ibt_clnt_t structure passed back to the clients.
333 	 * The client will pass on this handle in its future calls to IBTF.
334 	 */
335 	*ibt_hdl_p = clntp;
336 
337 	return (IBT_SUCCESS);
338 }
339 
340 
341 /*
342  * Function:
343  *	ibt_detach
344  * Input:
345  *	ibt_hdl - IBT Handle as returned during ibt_attach call.
346  * Output:
347  *	none
348  * Returns:
349  *	IBT_SUCCESS
350  *	IBT_INVALID_PARAM.
351  * Called by:
352  *	IBTF Client module during its detach() to de-register its instance
353  *	from IBTF.
354  * Description:
355  *	Deregisters the IBTF client module instance from the IBTF.
356  *	All resources and any reference to this ibt_hdl will be removed.
357  */
358 ibt_status_t
359 ibt_detach(ibt_clnt_hdl_t ibt_hdl)
360 {
361 	ibtl_clnt_t **clntpp;
362 
363 	IBTF_DPRINTF_L3(ibtf, "ibt_detach(%p)", ibt_hdl);
364 
365 	mutex_enter(&ibtl_clnt_list_mutex);
366 	clntpp = &ibtl_clnt_list;
367 	for (; *clntpp != NULL; clntpp = &(*clntpp)->clnt_list_link)
368 		if (*clntpp == ibt_hdl)
369 			break;
370 	if (*clntpp == NULL) {
371 		IBTF_DPRINTF_L1(ibtf, "ibt_detach: Client @ %p Not Found",
372 		    ibt_hdl);
373 		mutex_exit(&ibtl_clnt_list_mutex);
374 		return (IBT_INVALID_PARAM);
375 	}
376 
377 	/*
378 	 * Check out whether the client has freed all its resources.
379 	 * If not done, then fail the detach.
380 	 *
381 	 * viz. A client has to close all the HCA they have opened,
382 	 * i.e. the HCA List maintained for clients has to be empty.
383 	 * If this list is not empty, then the client has not performed
384 	 * complete clean-up, so fail the detach.
385 	 */
386 	if (ibt_hdl->clnt_hca_list != NULL) {
387 		mutex_exit(&ibtl_clnt_list_mutex);
388 
389 		IBTF_DPRINTF_L2(ibtf, "ibt_detach: "
390 		    "ERROR: Client '%s' has not closed all of its HCAs",
391 		    ibt_hdl->clnt_modinfop->mi_clnt_name);
392 		return (IBT_HCA_RESOURCES_NOT_FREED);
393 	}
394 
395 	if (ibt_hdl->clnt_srv_cnt != 0) {
396 		mutex_exit(&ibtl_clnt_list_mutex);
397 		IBTF_DPRINTF_L2(ibtf, "ibt_detach: client '%s' still has "
398 		    "services or subnet_notices registered",
399 		    ibt_hdl->clnt_modinfop->mi_clnt_name);
400 		return (IBT_HCA_RESOURCES_NOT_FREED);
401 	}
402 
403 	/*
404 	 * Delete the entry of this module from the ibtl_clnt_list List.
405 	 */
406 	*clntpp = ibt_hdl->clnt_list_link;	/* remove us */
407 
408 	/* make sure asyncs complete before freeing */
409 	ibtl_free_clnt_async_check(ibt_hdl);
410 
411 	if (ibt_hdl->clnt_modinfop->mi_clnt_class == IBT_CM) {
412 		ibtl_cm_async_handler = NULL;
413 		ibtl_cm_clnt_private = NULL;
414 	} else if (ibt_hdl->clnt_modinfop->mi_clnt_class == IBT_DM) {
415 		ibtl_dm_async_handler = NULL;
416 		ibtl_dm_clnt_private = NULL;
417 	} else if (ibt_hdl->clnt_modinfop->mi_clnt_class == IBT_IBMA) {
418 		ibtl_ibma_async_handler = NULL;
419 		ibtl_ibma_clnt_private = NULL;
420 	}
421 	mutex_exit(&ibtl_clnt_list_mutex);
422 
423 	/* Free up the memory of per-client info struct. */
424 	kmem_free(ibt_hdl, sizeof (ibtl_clnt_t));
425 
426 	return (IBT_SUCCESS);
427 }
428 
429 static void
430 ibtl_set_ibhw_status()
431 {
432 	ib_hw_status++;
433 }
434 
435 static void
436 ibtl_clear_ibhw_status()
437 {
438 	ib_hw_status--;
439 }
440 
441 /*
442  * Function:
443  *	ibc_init
444  * Input:
445  *	modlp		- Pointer to IBC client module linkage structure
446  * Output:
447  *	None
448  * Returns:
449  *	0 always for now
450  * Called by:
451  *	CI client calls IBTF during its _init() to register HCA with
452  *	Solaris I/O framework.
453  * Description:
454  *	Initializes the CI clients module linkage structure with
455  *	default bus_ops structure
456  */
457 int
458 ibc_init(struct modlinkage *modlp)
459 {
460 	ibtl_ibnex_cb_args_t	cb_args;
461 
462 	mutex_enter(&ibtl_clnt_list_mutex);
463 	cb_args.cb_flag = IBTL_IBNEX_IBC_INIT;
464 	cb_args.cb_modlp = modlp;
465 	if (ibtl_ibnex_callback_routine) {
466 		(void) ((*ibtl_ibnex_callback_routine)(&cb_args));
467 	}
468 	mutex_exit(&ibtl_clnt_list_mutex);
469 	return (0);
470 }
471 
472 
473 /*
474  * Function:
475  *	ibc_fini
476  * Input:
477  *	modlp		- Pointer to IBC client module linkage structure
478  * Output:
479  *	None
480  * Returns:
481  *	None
482  * Called by:
483  *	CI client calls IBTF during its _fini() to remove HCA with
484  *	Solaris I/O framework.
485  * Description:
486  *	Undo what is done during ibc_init
487  */
488 void
489 ibc_fini(struct modlinkage *modlp)
490 {
491 	ibtl_ibnex_cb_args_t	cb_args;
492 
493 	mutex_enter(&ibtl_clnt_list_mutex);
494 	cb_args.cb_flag = IBTL_IBNEX_IBC_FINI;
495 	cb_args.cb_modlp = modlp;
496 	if (ibtl_ibnex_callback_routine) {
497 		(void) ((*ibtl_ibnex_callback_routine)(&cb_args));
498 	}
499 	mutex_exit(&ibtl_clnt_list_mutex);
500 }
501 
502 /*
503  * Function:
504  *	ibc_attach
505  * Input:
506  *	info_p		- IBC HCA Info.
507  * Output:
508  *	ibc_hdl_p	- IBC Client's HCA Handle.
509  * Returns:
510  *	IBC_SUCCESS
511  *	IBC_FAILURE
512  * Called by:
513  *	CI calls IBTF during its attach() to register HCA Device with IBTF.
514  * Description:
515  *	Registers the presence of HCA device by providing the HCA device info
516  *  	structure and provides an opaque HCA handler for future calls to this
517  *  	HCA device.
518  */
519 ibc_status_t
520 ibc_attach(ibc_clnt_hdl_t *ibc_hdl_p, ibc_hca_info_t *info_p)
521 {
522 	ibtl_hca_devinfo_t	*hca_devp;
523 	uint_t			nports;
524 	ibt_status_t		status;
525 
526 	IBTF_DPRINTF_L2(ibtf, "ibc_attach(%p, %p)", ibc_hdl_p, info_p);
527 
528 	/* Validate the Transport API version */
529 	if (info_p->hca_ci_vers != IBCI_V2) {
530 		IBTF_DPRINTF_L1(ibtf, "ibc_attach: Invalid IB CI Version '%d'",
531 		    info_p->hca_ci_vers);
532 		return (IBC_FAILURE);
533 	}
534 
535 	if (info_p->hca_attr == NULL) {
536 		IBTF_DPRINTF_L1(ibtf, "ibc_attach: "
537 		    "HCA Attributes must be specified.");
538 		return (IBC_FAILURE);
539 	}
540 
541 	nports = info_p->hca_attr->hca_nports;
542 	if (nports == 0) {
543 		IBTF_DPRINTF_L1(ibtf, "ibc_attach: "
544 		    "Number of ports must be valid");
545 		return (IBC_FAILURE);
546 	}
547 
548 	if (info_p->hca_attr->hca_max_port_pkey_tbl_sz == 0) {
549 		IBTF_DPRINTF_L1(ibtf, "ibc_attach: "
550 		    "Number of Partitions must be at least 1");
551 		return (IBC_FAILURE);
552 	}
553 
554 	if ((info_p->hca_attr->hca_flags & IBT_HCA_CURRENT_QP_STATE) == 0) {
555 		IBTF_DPRINTF_L1(ibtf, "ibc_attach: "
556 		    "HCA driver must support QP current state checking");
557 		return (IBC_FAILURE);
558 	}
559 
560 	if ((info_p->hca_attr->hca_flags & IBT_HCA_PORT_UP) == 0) {
561 		IBTF_DPRINTF_L1(ibtf, "ibc_attach: "
562 		    "HCA driver must support PORT_UP async events");
563 		return (IBC_FAILURE);
564 	}
565 
566 	/*
567 	 * Install IB nexus driver (if not installed already)
568 	 */
569 	if (ndi_devi_config_vhci("ib", 0) == NULL) {
570 		IBTF_DPRINTF_L2(ibtf, "ibc_attach: IB nexus attach failed");
571 		return (IBC_FAILURE);
572 	}
573 	ibtl_set_ibhw_status();
574 
575 	ibtl_thread_init2();
576 
577 	/* Allocate the memory for per-client info structure */
578 	hca_devp = kmem_zalloc(sizeof (ibtl_hca_devinfo_t) +
579 	    (nports - 1) * sizeof (ibtl_async_port_status_t), KM_SLEEP);
580 
581 	mutex_enter(&ibtl_clnt_list_mutex);
582 
583 	/* Update HCA dev info structure */
584 	hca_devp->hd_ibc_hca_hdl = info_p->hca_handle;
585 	hca_devp->hd_ibc_ops	= info_p->hca_ops;
586 	hca_devp->hd_hca_attr	= info_p->hca_attr;
587 	hca_devp->hd_hca_dip	= info_p->hca_dip;
588 
589 	status = ibtl_init_hca_portinfo(hca_devp);
590 	if (status != IBT_SUCCESS) {
591 		mutex_exit(&ibtl_clnt_list_mutex);
592 		IBTF_DPRINTF_L1(ibtf, "ibc_attach: call to ibc_query_hca_ports "
593 		    "failed: status = %d", status);
594 		kmem_free(hca_devp, sizeof (ibtl_hca_devinfo_t) +
595 		    (nports - 1) * sizeof (ibtl_async_port_status_t));
596 		return (IBC_FAILURE);
597 	}
598 
599 	/* Register the with MPxIO as PHCI */
600 	if (mdi_phci_register(MDI_HCI_CLASS_IB, info_p->hca_dip, 0) !=
601 		MDI_SUCCESS) {
602 		mutex_exit(&ibtl_clnt_list_mutex);
603 		IBTF_DPRINTF_L1(ibtf, "ibc_attach: MPxIO register failed");
604 		kmem_free(hca_devp, sizeof (ibtl_hca_devinfo_t) +
605 		    (nports - 1) * sizeof (ibtl_async_port_status_t));
606 		return (IBC_FAILURE);
607 	}
608 
609 	/* Initialize the Client List for this HCA. */
610 	hca_devp->hd_state	= IBTL_HCA_DEV_ATTACHED;
611 
612 	/* lock out asyncs until after we announce the new HCA */
613 	hca_devp->hd_async_busy = 1;
614 
615 	cv_init(&hca_devp->hd_async_task_cv, NULL, CV_DEFAULT, NULL);
616 	cv_init(&hca_devp->hd_async_busy_cv, NULL, CV_DEFAULT, NULL);
617 
618 	/* init portinfo locking variables */
619 	hca_devp->hd_portinfo_locked_port = 0;
620 	cv_init(&hca_devp->hd_portinfo_cv, NULL, CV_DEFAULT, NULL);
621 
622 	mutex_exit(&ibtl_clnt_list_mutex);
623 
624 	/*
625 	 * The ibc_hdl_p points to an opaque handle which is the address
626 	 * of ibt_hca_devinfo_t structure passed back to the CI.
627 	 * The CI will pass on this handle in its future upcalls to IBTF.
628 	 */
629 	*ibc_hdl_p = hca_devp;
630 
631 	return (IBC_SUCCESS);
632 }
633 
634 
635 /*
636  * Function:
637  *	ibc_post_attach
638  * Input:
639  *	ibc_hdl		- IBC Client's HCA Handle.
640  * Returns:
641  *	none
642  * Called by:
643  *	CI calls IBTF during its attach() after a successful ibc_attach().
644  * Description:
645  *	Announces to all known clients the existence of this HCA (by GUID).
646  */
647 void
648 ibc_post_attach(ibc_clnt_hdl_t ibc_hdl)
649 {
650 	IBTF_DPRINTF_L2(ibtf, "ibc_post_attach(%p)", ibc_hdl);
651 
652 	/*
653 	 * Update the HCA Device List.
654 	 */
655 	mutex_enter(&ibtl_clnt_list_mutex);
656 	ibc_hdl->hd_hca_dev_link = ibtl_hca_list;
657 	ibtl_hca_list = ibc_hdl;
658 	mutex_exit(&ibtl_clnt_list_mutex);
659 
660 	/* notify all IBT Client Device Instances of the new HCA Device */
661 	ibtl_announce_new_hca(ibc_hdl);
662 }
663 
664 
665 /*
666  * Function:
667  *	ibc_pre_detach
668  * Input:
669  *	ibc_clnt_hdl	- IBC HCA Handle as returned during ibc_attach call.
670  *  	cmd		- DDI_DETACH/DDI_SUSPEND command.
671  * Output:
672  *	none
673  * Returns:
674  *	IBC_SUCCESS
675  *	IBC_FAILURE.
676  * Called by:
677  *	CI to try to get all IBTF clients to close the HCA device.
678  * Description:
679  *	Attempts to deregister the HCA device entry from the IBTF.
680  *	If all resources are freed by the IBTF clients and this HCA
681  *	is closed, then IBC_SUCCESS is returned.
682  */
683 ibc_status_t
684 ibc_pre_detach(ibc_clnt_hdl_t hca_devp, ddi_detach_cmd_t cmd)
685 {
686 	ibtl_hca_devinfo_t **hcapp, *hcap;
687 
688 	IBTF_DPRINTF_L2(ibtf, "ibc_pre_detach(%p, 0x%x)", hca_devp, cmd);
689 
690 	/*
691 	 * Return failure, if command is not DDI_DETACH
692 	 */
693 	switch (cmd) {
694 	case DDI_DETACH:
695 		break;
696 	default:
697 		return (IBC_FAILURE); /* TBD: DDI_FAILURE */
698 	}
699 
700 	/* Make sure this HCA is on the HCA Device List.  */
701 	mutex_enter(&ibtl_clnt_list_mutex);
702 	hcap = ibtl_hca_list;
703 	while (hcap != NULL) {
704 		if (hcap == hca_devp)
705 			break;
706 		hcap = hcap->hd_hca_dev_link;
707 	}
708 	if (hcap == NULL) {
709 		mutex_exit(&ibtl_clnt_list_mutex);
710 		return (IBC_FAILURE);
711 	}
712 
713 	/*
714 	 * Initially set the state to "Detaching".
715 	 */
716 	hca_devp->hd_state = IBTL_HCA_DEV_DETACHING;
717 
718 	/*
719 	 * Try to detach all IBTI clients, and continue only if all
720 	 * of the detaches succeed.
721 	 */
722 	if (ibtl_detach_all_clients(hca_devp)) {
723 		hca_devp->hd_state = IBTL_HCA_DEV_ATTACHED; /* fix hd_state */
724 		mutex_exit(&ibtl_clnt_list_mutex);
725 
726 		return (IBC_FAILURE);
727 	}
728 
729 	/*
730 	 * Check to see if all clients closed this HCA, or not.
731 	 * We only succeed if all clients cooperated.
732 	 */
733 	if (hca_devp->hd_clnt_list != NULL) {
734 		hca_devp->hd_state = IBTL_HCA_DEV_ATTACHED;
735 		mutex_exit(&ibtl_clnt_list_mutex);
736 		IBTF_DPRINTF_L2(ibtf, "ibc_pre_detach: HCA still has attached "
737 		    "clients");
738 		return (IBC_FAILURE);
739 	}
740 
741 	/*
742 	 * mark this device as detached
743 	 */
744 	hca_devp->hd_state = IBTL_HCA_DEV_DETACHED;
745 
746 	/* Delete the entry for this hca_devp from hca_head_list */
747 	hcapp = &ibtl_hca_list;
748 	while (*hcapp != NULL) {
749 		if (*hcapp == hca_devp)
750 			break;
751 		hcapp = &(*hcapp)->hd_hca_dev_link;
752 	}
753 
754 	if (mdi_phci_unregister(hca_devp->hd_hca_dip, 0) != MDI_SUCCESS) {
755 		hca_devp->hd_state = IBTL_HCA_DEV_ATTACHED; /* fix hd_state */
756 		mutex_exit(&ibtl_clnt_list_mutex);
757 		IBTF_DPRINTF_L1(ibtf, "ibc_pre_detach: PHCI unregister failed");
758 		return (IBC_FAILURE);
759 	}
760 
761 	if (*hcapp == NULL) {
762 		hca_devp->hd_state = IBTL_HCA_DEV_ATTACHED; /* fix hd_state */
763 		mutex_exit(&ibtl_clnt_list_mutex);
764 		IBTF_DPRINTF_L1(ibtf, "ibc_pre_detach: HCA not attached");
765 		return (IBC_FAILURE);
766 	}
767 	*hcapp = hca_devp->hd_hca_dev_link;
768 	ibtl_fast_gid_cache_valid = B_FALSE;	/* invalidate fast_gid_cache */
769 	mutex_exit(&ibtl_clnt_list_mutex);
770 
771 	return (IBC_SUCCESS);
772 }
773 
774 /*
775  * Function:
776  *	ibc_detach
777  * Input:
778  *	ibc_clnt_hdl	- IBC HCA Handle as returned during ibc_attach call.
779  * Output:
780  *	none
781  * Returns:
782  *	None
783  * Called by:
784  *	CI to detach the HCA device from IBTF.
785  * Description:
786  *	Do the second step of detaching the HCA, which is required
787  *	after a successful ibc_pre_detach.
788  */
789 void
790 ibc_detach(ibc_clnt_hdl_t hca_devp)
791 {
792 	IBTF_DPRINTF_L2(ibtf, "ibc_detach(%p)", hca_devp);
793 
794 	mutex_enter(&ibtl_clnt_list_mutex);
795 	if (hca_devp->hd_state != IBTL_HCA_DEV_DETACHED) {
796 		mutex_exit(&ibtl_clnt_list_mutex);
797 		IBTF_DPRINTF_L0(ibtf, "ibc_detach: HCA has not successfully "
798 		    "pre-detached");
799 		return;
800 	}
801 
802 	cv_destroy(&hca_devp->hd_async_task_cv);
803 	cv_destroy(&hca_devp->hd_async_busy_cv);
804 	cv_destroy(&hca_devp->hd_portinfo_cv);
805 
806 	kmem_free(hca_devp->hd_portinfop, hca_devp->hd_portinfo_len);
807 	mutex_exit(&ibtl_clnt_list_mutex);
808 
809 	/* Free up the memory of per-client info struct */
810 	kmem_free(hca_devp, sizeof (ibtl_hca_devinfo_t) +
811 	    (hca_devp->hd_hca_attr->hca_nports - 1) *
812 	    sizeof (ibtl_async_port_status_t));
813 	ibtl_clear_ibhw_status();
814 }
815 
816 /*
817  * Function:
818  *	ibt_ci_data_in()
819  *
820  * Input:
821  *	hca_hdl			HCA Handle.
822  *	flags			IBT_COMPLETE_ALLOC - Finish a deferred alloc.
823  *      object                  Identifies the type object pointed to by
824  *                              ibt_object_handle.
825  *
826  *      ibt_object_handle       The handle of the object to be associated with
827  *				the data in/out
828  *
829  *	data_p			Pointer data passed in to the CI. The buffer
830  *				should be allocated by the caller.
831  *
832  *	data_sz			The size of the buffer pointed to by
833  *				data_p.
834  * Output:
835  *
836  * Returns:
837  *	IBT_SUCCESS
838  *	IBT_NOT_SUPPORTED	Feature not supported.
839  *	IBT_INVALID_PARAM	Invalid object type specified.
840  *	IBT_HCA_HDL_INVALID
841  *	IBT_AH_HDL_INVALID/IBT_UD_DEST_HDL_INVALID
842  *	IBT_CHAN_HDL_INVALID/IBT_QP_HDL_INVALID
843  *	IBT_CQ_HDL_INVALID
844  *	IBT_EEC_HDL_INVALID
845  *	IBT_RDD_HDL_INVALID
846  *	IBT_MW_HDL_INVALID
847  *	IBT_PD_HDL_INVALID
848  *	IBT_SRQ_HDL_INVALID
849  *
850  * Description:
851  *	Exchange CI private data for the specified CI object.
852  */
853 ibt_status_t
854 ibt_ci_data_in(ibt_hca_hdl_t hca, ibt_ci_data_flags_t flags,
855     ibt_object_type_t object, void *ibt_object_handle, void *data_p,
856     size_t data_sz)
857 {
858 	ibt_status_t		retval;
859 	void			*ci_obj_hdl;
860 
861 	IBTF_DPRINTF_L3(ibtf, "ibt_ci_data_in(%p, %x, %d, %p, %p, %d)",
862 	    hca, flags, object, ibt_object_handle, data_p, data_sz);
863 
864 	switch (object) {
865 	case IBT_HDL_HCA:
866 		ci_obj_hdl = (void *)
867 		    (IBTL_HCA2CIHCA(((ibt_hca_hdl_t)ibt_object_handle)));
868 		break;
869 
870 	case IBT_HDL_CHANNEL:
871 		ci_obj_hdl = (void *)
872 		    (IBTL_CHAN2CIQP(((ibt_channel_hdl_t)ibt_object_handle)));
873 		break;
874 
875 	case IBT_HDL_CQ:
876 		ci_obj_hdl = (void *)
877 		    (((ibt_cq_hdl_t)(ibt_object_handle))->cq_ibc_cq_hdl);
878 		break;
879 
880 	case IBT_HDL_EEC:
881 		ci_obj_hdl = (void *)
882 		    (((ibt_eec_hdl_t)(ibt_object_handle))->eec_ibc_eec_hdl);
883 		break;
884 
885 	case IBT_HDL_UD_DEST:
886 		ci_obj_hdl = (void *)
887 		    (((ibt_ud_dest_hdl_t)(ibt_object_handle))->ud_ah);
888 		break;
889 
890 	case IBT_HDL_SRQ:
891 		ci_obj_hdl = (void *)
892 		    (((ibt_srq_hdl_t)(ibt_object_handle))->srq_ibc_srq_hdl);
893 		break;
894 
895 	default:
896 		ci_obj_hdl = ibt_object_handle;
897 		break;
898 	}
899 
900 	retval = (IBTL_HCA2CIHCAOPS_P(hca)->ibc_ci_data_in)(IBTL_HCA2CIHCA(hca),
901 	    flags, object, ci_obj_hdl, data_p, data_sz);
902 
903 	if (retval != IBT_SUCCESS) {
904 		IBTF_DPRINTF_L2(ibtf, "ibt_ci_data_in: Failed : %d", retval);
905 	}
906 	return (retval);
907 }
908 
909 /*
910  * Function:
911  *	ibt_ci_data_out()
912  *
913  * Input:
914  *	hca_hdl			HCA Handle.
915  *	flags			IBT_COMPLETE_ALLOC - Finish a deferred alloc.
916  *      object                  Identifies the type object pointed to by
917  *                              ibt_object_handle.
918  *
919  *      ibt_object_handle       The handle of the object to be associated with
920  *				the data in/out
921  *
922  *	data_p			Pointer to a buffer in which to return the CI
923  *				private data. The buffer should be allocated
924  *				by the caller.
925  *
926  *	data_sz			The size of the buffer pointed to by
927  *				data_p.
928  * Output:
929  *
930  * Returns:
931  *	IBT_SUCCESS
932  *	IBT_NOT_SUPPORTED	Feature not supported.
933  *	IBT_INSUFF_RESOURCE	The buffer pointed to by data_p was too
934  *				small to hold the data.
935  *	IBT_INVALID_PARAM	Invalid object type specified.
936  *	IBT_HCA_HDL_INVALID
937  *	IBT_AH_HDL_INVALID/IBT_UD_DEST_HDL_INVALID
938  *	IBT_CHAN_HDL_INVALID/IBT_QP_HDL_INVALID
939  *	IBT_CQ_HDL_INVALID
940  *	IBT_EEC_HDL_INVALID
941  *	IBT_RDD_HDL_INVALID
942  *	IBT_MW_HDL_INVALID
943  *	IBT_PD_HDL_INVALID
944  *	IBT_SRQ_HDL_INVALID
945  *
946  * Description:
947  *	Exchange CI private data for the specified CI object.
948  */
949 ibt_status_t
950 ibt_ci_data_out(ibt_hca_hdl_t hca, ibt_ci_data_flags_t flags,
951     ibt_object_type_t object, void *ibt_object_handle, void *data_p,
952     size_t data_sz)
953 {
954 	ibt_status_t		retval;
955 	void			*ci_obj_hdl;
956 
957 	IBTF_DPRINTF_L3(ibtf, "ibt_ci_data_out(%p, %x, %d, %p, %p, %d)",
958 	    hca, flags, object, ibt_object_handle, data_p, data_sz);
959 
960 	switch (object) {
961 	case  IBT_HDL_HCA:
962 		ci_obj_hdl = (void *)
963 		    (IBTL_HCA2CIHCA(((ibt_hca_hdl_t)ibt_object_handle)));
964 		break;
965 
966 	case IBT_HDL_CHANNEL:
967 		ci_obj_hdl = (void *)
968 		    (IBTL_CHAN2CIQP(((ibt_channel_hdl_t)ibt_object_handle)));
969 		break;
970 
971 	case IBT_HDL_CQ:
972 		ci_obj_hdl = (void *)
973 		    (((ibt_cq_hdl_t)(ibt_object_handle))->cq_ibc_cq_hdl);
974 		break;
975 
976 	case IBT_HDL_EEC:
977 		ci_obj_hdl = (void *)
978 		    (((ibt_eec_hdl_t)(ibt_object_handle))->eec_ibc_eec_hdl);
979 		break;
980 
981 	case IBT_HDL_UD_DEST:
982 		ci_obj_hdl = (void *)
983 		    (((ibt_ud_dest_hdl_t)(ibt_object_handle))->ud_ah);
984 		break;
985 
986 	case IBT_HDL_SRQ:
987 		ci_obj_hdl = (void *)
988 		    (((ibt_srq_hdl_t)(ibt_object_handle))->srq_ibc_srq_hdl);
989 		break;
990 
991 	default:
992 		ci_obj_hdl = ibt_object_handle;
993 		break;
994 	}
995 
996 	retval = (IBTL_HCA2CIHCAOPS_P(hca)->ibc_ci_data_out)
997 	    (IBTL_HCA2CIHCA(hca), flags, object, ci_obj_hdl, data_p, data_sz);
998 
999 	if (retval != IBT_SUCCESS) {
1000 		IBTF_DPRINTF_L2(ibtf, "ibt_ci_data_out: Failed : %d", retval);
1001 	}
1002 	return (retval);
1003 }
1004 
1005 
1006 /*
1007  * FMA Support functions.
1008  */
1009 
1010 #define	IBTL_ENA_MASK		0xC0000000
1011 #define	IBTL_ENA_POSSIBLE	0x80000000
1012 #define	IBTL_TYPE_SHIFT		27
1013 
1014 /*
1015  * Function:
1016  *	ibt_get_module_failure()
1017  *
1018  * Input:
1019  *	type			Identifies the failing IB module.
1020  *	ena			'0' or the data for Fault Management
1021  *				Architecture (ENA).
1022  *
1023  * Returns:
1024  *	status			Special IB failure status.
1025  *
1026  * Description:
1027  *	XXX Just stubbed out to return failures with no data for Fault
1028  *	Management Architecture (ENAs) at the moment XXX
1029  */
1030 ibt_status_t
1031 ibt_get_module_failure(ibt_failure_type_t type, uint64_t ena)
1032 {
1033 	ibt_status_t	ret;
1034 
1035 	IBTF_DPRINTF_L3(ibtf, "ibt_get_module_failure(%d, 0x%llX)", type, ena);
1036 
1037 	switch (type) {
1038 	case IBT_FAILURE_CI:
1039 	case IBT_FAILURE_IBMF:
1040 	case IBT_FAILURE_IBCM:
1041 	case IBT_FAILURE_IBDM:
1042 	case IBT_FAILURE_IBTL:
1043 	case IBT_FAILURE_IBSM:
1044 		ret = IBTL_ENA_POSSIBLE | (type << IBTL_TYPE_SHIFT);
1045 		break;
1046 	default:
1047 		ret = IBT_FAILURE;
1048 	}
1049 	IBTF_DPRINTF_L3(ibtf, "ibt_get_module_failure: ret = 0x%lX", ret);
1050 	return (ret);
1051 }
1052 
1053 
1054 /*
1055  * Function:
1056  *	ibc_get_ci_failure()
1057  *
1058  * Input:
1059  *	ena			'0' or the data for Fault Management
1060  *				Architecture (ENA).
1061  *
1062  * Returns:
1063  *	status			Special CI failure status.
1064  *
1065  * Description:
1066  *	Just use the function above to do the job.
1067  */
1068 ibt_status_t
1069 ibc_get_ci_failure(uint64_t ena)
1070 {
1071 	return (ibt_get_module_failure(IBT_FAILURE_CI, ena));
1072 }
1073 
1074 
1075 /*
1076  * ibt_check_failure()
1077  *	Function to test for special case failures.
1078  *
1079  *	status		An ibt_status_t returned from an IBTF function call.
1080  *
1081  *	reserved_p	NULL, or a pointer to where we store the data for
1082  *			Fault Management Architecture (ENA).
1083  *
1084  * Description:
1085  *	XXX Still need to determine the data for Fault Management Architecture
1086  *	(ENA), using 0 for now XXX
1087  */
1088 ibt_failure_type_t
1089 ibt_check_failure(ibt_status_t status, uint64_t *reserved_p)
1090 {
1091 	ibt_failure_type_t type;
1092 
1093 	IBTF_DPRINTF_L3(ibtf, "ibt_check_failure(%X)", status);
1094 
1095 	if ((status & IBTL_ENA_MASK) == IBTL_ENA_POSSIBLE) {
1096 		type = status & ~IBTL_ENA_POSSIBLE >> IBTL_TYPE_SHIFT;
1097 
1098 		/* XXX Need more work here... */
1099 		if (reserved_p != NULL)
1100 			*reserved_p = 0;
1101 	} else {
1102 		type = IBT_FAILURE_STANDARD;
1103 		if (reserved_p != NULL)
1104 			*reserved_p = 0;	/* No FMA Data Available. */
1105 	}
1106 	IBTF_DPRINTF_L3(ibtf, "ibt_check_failure: type = 0x%X", type);
1107 	return (type);
1108 }
1109