xref: /illumos-gate/usr/src/uts/common/io/scsi/impl/scsi_hba.c (revision 628e3cbed6489fa1db545d8524a06cd6535af456)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/note.h>
27 
28 /*
29  * Generic SCSI Host Bus Adapter interface implementation
30  */
31 #include <sys/scsi/scsi.h>
32 #include <sys/file.h>
33 #include <sys/ddi_impldefs.h>
34 #include <sys/ndi_impldefs.h>
35 #include <sys/ddi.h>
36 #include <sys/epm.h>
37 
38 extern struct scsi_pkt *scsi_init_cache_pkt(struct scsi_address *,
39 		    struct scsi_pkt *, struct buf *, int, int, int, int,
40 		    int (*)(caddr_t), caddr_t);
41 extern void scsi_free_cache_pkt(struct scsi_address *,
42 		    struct scsi_pkt *);
43 extern void scsi_cache_dmafree(struct scsi_address *,
44 		    struct scsi_pkt *);
45 extern void scsi_sync_cache_pkt(struct scsi_address *,
46 		    struct scsi_pkt *);
47 
48 /*
49  * Round up all allocations so that we can guarantee
50  * long-long alignment.  This is the same alignment
51  * provided by kmem_alloc().
52  */
53 #define	ROUNDUP(x)	(((x) + 0x07) & ~0x07)
54 
55 /* Magic number to track correct allocations in wrappers */
56 #define	PKT_WRAPPER_MAGIC	0xa110ced	/* alloced correctly */
57 
58 static kmutex_t	scsi_hba_mutex;
59 
60 kmutex_t scsi_log_mutex;
61 
62 
63 struct scsi_hba_inst {
64 	dev_info_t		*inst_dip;
65 	scsi_hba_tran_t		*inst_hba_tran;
66 	struct scsi_hba_inst	*inst_next;
67 	struct scsi_hba_inst	*inst_prev;
68 };
69 
70 static struct scsi_hba_inst	*scsi_hba_list		= NULL;
71 static struct scsi_hba_inst	*scsi_hba_list_tail	= NULL;
72 
73 
74 kmutex_t	scsi_flag_nointr_mutex;
75 kcondvar_t	scsi_flag_nointr_cv;
76 
77 /*
78  * Prototypes for static functions
79  */
80 static int	scsi_hba_bus_ctl(
81 			dev_info_t		*dip,
82 			dev_info_t		*rdip,
83 			ddi_ctl_enum_t		op,
84 			void			*arg,
85 			void			*result);
86 
87 static int	scsi_hba_map_fault(
88 			dev_info_t		*dip,
89 			dev_info_t		*rdip,
90 			struct hat		*hat,
91 			struct seg		*seg,
92 			caddr_t			addr,
93 			struct devpage		*dp,
94 			pfn_t			pfn,
95 			uint_t			prot,
96 			uint_t			lock);
97 
98 static int	scsi_hba_get_eventcookie(
99 			dev_info_t		*dip,
100 			dev_info_t		*rdip,
101 			char			*name,
102 			ddi_eventcookie_t	*eventp);
103 
104 static int	scsi_hba_add_eventcall(
105 			dev_info_t		*dip,
106 			dev_info_t		*rdip,
107 			ddi_eventcookie_t	event,
108 			void			(*callback)(
109 					dev_info_t *dip,
110 					ddi_eventcookie_t event,
111 					void *arg,
112 					void *bus_impldata),
113 			void			*arg,
114 			ddi_callback_id_t	*cb_id);
115 
116 static int	scsi_hba_remove_eventcall(
117 			dev_info_t *devi,
118 			ddi_callback_id_t id);
119 
120 static int	scsi_hba_post_event(
121 			dev_info_t		*dip,
122 			dev_info_t		*rdip,
123 			ddi_eventcookie_t	event,
124 			void			*bus_impldata);
125 
126 static int	scsi_hba_info(
127 			dev_info_t		*dip,
128 			ddi_info_cmd_t		infocmd,
129 			void			*arg,
130 			void			**result);
131 
132 static int scsi_hba_bus_config(dev_info_t *parent, uint_t flag,
133     ddi_bus_config_op_t op, void *arg, dev_info_t **childp);
134 static int scsi_hba_bus_unconfig(dev_info_t *parent, uint_t flag,
135     ddi_bus_config_op_t op, void *arg);
136 static int scsi_hba_fm_init_child(dev_info_t *self, dev_info_t *child,
137     int cap, ddi_iblock_cookie_t *ibc);
138 static int scsi_hba_bus_power(dev_info_t *parent, void *impl_arg,
139     pm_bus_power_op_t op, void *arg, void *result);
140 
141 /*
142  * Busops vector for SCSI HBA's.
143  */
144 static struct bus_ops scsi_hba_busops = {
145 	BUSO_REV,
146 	nullbusmap,			/* bus_map */
147 	NULL,				/* bus_get_intrspec */
148 	NULL,				/* bus_add_intrspec */
149 	NULL,				/* bus_remove_intrspec */
150 	scsi_hba_map_fault,		/* bus_map_fault */
151 	ddi_dma_map,			/* bus_dma_map */
152 	ddi_dma_allochdl,		/* bus_dma_allochdl */
153 	ddi_dma_freehdl,		/* bus_dma_freehdl */
154 	ddi_dma_bindhdl,		/* bus_dma_bindhdl */
155 	ddi_dma_unbindhdl,		/* bus_unbindhdl */
156 	ddi_dma_flush,			/* bus_dma_flush */
157 	ddi_dma_win,			/* bus_dma_win */
158 	ddi_dma_mctl,			/* bus_dma_ctl */
159 	scsi_hba_bus_ctl,		/* bus_ctl */
160 	ddi_bus_prop_op,		/* bus_prop_op */
161 	scsi_hba_get_eventcookie,	/* bus_get_eventcookie */
162 	scsi_hba_add_eventcall,		/* bus_add_eventcall */
163 	scsi_hba_remove_eventcall,	/* bus_remove_eventcall */
164 	scsi_hba_post_event,		/* bus_post_event */
165 	NULL,				/* bus_intr_ctl */
166 	scsi_hba_bus_config,		/* bus_config */
167 	scsi_hba_bus_unconfig,		/* bus_unconfig */
168 	scsi_hba_fm_init_child,		/* bus_fm_init */
169 	NULL,				/* bus_fm_fini */
170 	NULL,				/* bus_fm_access_enter */
171 	NULL,				/* bus_fm_access_exit */
172 	scsi_hba_bus_power		/* bus_power */
173 };
174 
175 
176 static struct cb_ops scsi_hba_cbops = {
177 	scsi_hba_open,
178 	scsi_hba_close,
179 	nodev,			/* strategy */
180 	nodev,			/* print */
181 	nodev,			/* dump */
182 	nodev,			/* read */
183 	nodev,			/* write */
184 	scsi_hba_ioctl,		/* ioctl */
185 	nodev,			/* devmap */
186 	nodev,			/* mmap */
187 	nodev,			/* segmap */
188 	nochpoll,		/* poll */
189 	ddi_prop_op,		/* prop_op */
190 	NULL,			/* stream */
191 	D_NEW|D_MP|D_HOTPLUG,	/* cb_flag */
192 	CB_REV,			/* rev */
193 	nodev,			/* int (*cb_aread)() */
194 	nodev			/* int (*cb_awrite)() */
195 };
196 
197 
198 /*
199  * Called from _init() when loading scsi module
200  */
201 void
202 scsi_initialize_hba_interface()
203 {
204 	mutex_init(&scsi_hba_mutex, NULL, MUTEX_DRIVER, NULL);
205 	mutex_init(&scsi_flag_nointr_mutex, NULL, MUTEX_DRIVER, NULL);
206 	cv_init(&scsi_flag_nointr_cv, NULL, CV_DRIVER, NULL);
207 	mutex_init(&scsi_log_mutex, NULL, MUTEX_DRIVER, NULL);
208 }
209 
210 #ifdef	NO_SCSI_FINI_YET
211 /*
212  * Called from _fini() when unloading scsi module
213  */
214 void
215 scsi_uninitialize_hba_interface()
216 {
217 	mutex_destroy(&scsi_hba_mutex);
218 	cv_destroy(&scsi_flag_nointr_cv);
219 	mutex_destroy(&scsi_flag_nointr_mutex);
220 	mutex_destroy(&scsi_log_mutex);
221 }
222 #endif	/* NO_SCSI_FINI_YET */
223 
224 int
225 scsi_hba_pkt_constructor(void *buf, void *arg, int kmflag)
226 {
227 	struct scsi_pkt_cache_wrapper *pktw;
228 	struct scsi_pkt		*pkt;
229 	scsi_hba_tran_t		*tran = (scsi_hba_tran_t *)arg;
230 	int			pkt_len;
231 	char			*ptr;
232 
233 	/*
234 	 * allocate a chunk of memory for the following:
235 	 * scsi_pkt
236 	 * pcw_* fields
237 	 * pkt_ha_private
238 	 * pkt_cdbp, if needed
239 	 * (pkt_private always null)
240 	 * pkt_scbp, if needed
241 	 */
242 	pkt_len = tran->tran_hba_len + sizeof (struct scsi_pkt_cache_wrapper);
243 	if (tran->tran_hba_flags & SCSI_HBA_TRAN_CDB)
244 		pkt_len += DEFAULT_CDBLEN;
245 	if (tran->tran_hba_flags & SCSI_HBA_TRAN_SCB)
246 		pkt_len += DEFAULT_SCBLEN;
247 	bzero(buf, pkt_len);
248 
249 	ptr = buf;
250 	pktw = buf;
251 	ptr += sizeof (struct scsi_pkt_cache_wrapper);
252 	pkt = &(pktw->pcw_pkt);
253 	pkt->pkt_ha_private = (opaque_t)ptr;
254 
255 	pktw->pcw_magic = PKT_WRAPPER_MAGIC;	/* alloced correctly */
256 	/*
257 	 * keep track of the granularity at the time this handle was
258 	 * allocated
259 	 */
260 	pktw->pcw_granular = tran->tran_dma_attr.dma_attr_granular;
261 
262 	if (ddi_dma_alloc_handle(tran->tran_hba_dip,
263 	    &tran->tran_dma_attr,
264 	    kmflag == KM_SLEEP ? SLEEP_FUNC: NULL_FUNC, NULL,
265 	    &pkt->pkt_handle) != DDI_SUCCESS) {
266 
267 		return (-1);
268 	}
269 	ptr += tran->tran_hba_len;
270 	if (tran->tran_hba_flags & SCSI_HBA_TRAN_CDB) {
271 		pkt->pkt_cdbp = (opaque_t)ptr;
272 		ptr += DEFAULT_CDBLEN;
273 	}
274 	pkt->pkt_private = NULL;
275 	if (tran->tran_hba_flags & SCSI_HBA_TRAN_SCB)
276 		pkt->pkt_scbp = (opaque_t)ptr;
277 	if (tran->tran_pkt_constructor)
278 		return ((*tran->tran_pkt_constructor)(pkt, arg, kmflag));
279 	else
280 		return (0);
281 }
282 
283 #define	P_TO_TRAN(pkt)	((pkt)->pkt_address.a_hba_tran)
284 
285 void
286 scsi_hba_pkt_destructor(void *buf, void *arg)
287 {
288 	struct scsi_pkt_cache_wrapper *pktw = buf;
289 	struct scsi_pkt *pkt	= &(pktw->pcw_pkt);
290 	scsi_hba_tran_t		*tran = (scsi_hba_tran_t *)arg;
291 
292 	ASSERT(pktw->pcw_magic == PKT_WRAPPER_MAGIC);
293 	ASSERT((pktw->pcw_flags & PCW_BOUND) == 0);
294 	if (tran->tran_pkt_destructor)
295 		(*tran->tran_pkt_destructor)(pkt, arg);
296 
297 	/* make sure nobody messed with our pointers */
298 	ASSERT(pkt->pkt_ha_private == (opaque_t)((char *)pkt +
299 	    sizeof (struct scsi_pkt_cache_wrapper)));
300 	ASSERT(((tran->tran_hba_flags & SCSI_HBA_TRAN_SCB) == 0) ||
301 	    (pkt->pkt_scbp == (opaque_t)((char *)pkt +
302 	    tran->tran_hba_len +
303 	    (((tran->tran_hba_flags & SCSI_HBA_TRAN_CDB) == 0) ?
304 	    0 : DEFAULT_CDBLEN) +
305 	    DEFAULT_PRIVLEN + sizeof (struct scsi_pkt_cache_wrapper))));
306 	ASSERT(((tran->tran_hba_flags & SCSI_HBA_TRAN_CDB) == 0) ||
307 	    (pkt->pkt_cdbp == (opaque_t)((char *)pkt +
308 	    tran->tran_hba_len +
309 	    sizeof (struct scsi_pkt_cache_wrapper))));
310 	ASSERT(pkt->pkt_handle);
311 	ddi_dma_free_handle(&pkt->pkt_handle);
312 	pkt->pkt_handle = NULL;
313 	pkt->pkt_numcookies = 0;
314 	pktw->pcw_total_xfer = 0;
315 	pktw->pcw_totalwin = 0;
316 	pktw->pcw_curwin = 0;
317 }
318 
319 /*
320  * Called by an HBA from _init()
321  */
322 int
323 scsi_hba_init(struct modlinkage *modlp)
324 {
325 	struct dev_ops *hba_dev_ops;
326 
327 	/*
328 	 * Get the devops structure of the hba,
329 	 * and put our busops vector in its place.
330 	 */
331 	hba_dev_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
332 	ASSERT(hba_dev_ops->devo_bus_ops == NULL);
333 	hba_dev_ops->devo_bus_ops = &scsi_hba_busops;
334 
335 	/*
336 	 * Provide getinfo and hotplugging ioctl if driver
337 	 * does not provide them already
338 	 */
339 	if (hba_dev_ops->devo_cb_ops == NULL) {
340 		hba_dev_ops->devo_cb_ops = &scsi_hba_cbops;
341 	}
342 	if (hba_dev_ops->devo_cb_ops->cb_open == scsi_hba_open) {
343 		ASSERT(hba_dev_ops->devo_cb_ops->cb_close == scsi_hba_close);
344 		hba_dev_ops->devo_getinfo = scsi_hba_info;
345 	}
346 
347 	return (0);
348 }
349 
350 
351 /*
352  * Implement this older interface in terms of the new.
353  * This is hardly in the critical path, so avoiding
354  * unnecessary code duplication is more important.
355  */
356 /*ARGSUSED*/
357 int
358 scsi_hba_attach(
359 	dev_info_t		*dip,
360 	ddi_dma_lim_t		*hba_lim,
361 	scsi_hba_tran_t		*hba_tran,
362 	int			flags,
363 	void			*hba_options)
364 {
365 	ddi_dma_attr_t		hba_dma_attr;
366 
367 	bzero(&hba_dma_attr, sizeof (ddi_dma_attr_t));
368 
369 	hba_dma_attr.dma_attr_burstsizes = hba_lim->dlim_burstsizes;
370 	hba_dma_attr.dma_attr_minxfer = hba_lim->dlim_minxfer;
371 
372 	return (scsi_hba_attach_setup(dip, &hba_dma_attr, hba_tran, flags));
373 }
374 
375 
376 /*
377  * Called by an HBA to attach an instance of the driver
378  */
379 int
380 scsi_hba_attach_setup(
381 	dev_info_t		*dip,
382 	ddi_dma_attr_t		*hba_dma_attr,
383 	scsi_hba_tran_t		*hba_tran,
384 	int			flags)
385 {
386 	struct dev_ops		*hba_dev_ops;
387 	struct scsi_hba_inst	*elem;
388 	int			value;
389 	int			len;
390 	char			*prop_name;
391 	const char		*prop_value;
392 	int			capable;
393 	static char		*errmsg =
394 	    "scsi_hba_attach: cannot create property '%s' for %s%d\n";
395 	static const char	*interconnect[] = INTERCONNECT_TYPE_ASCII;
396 
397 	/*
398 	 * Link this instance into the scsi_hba_list
399 	 */
400 	elem = kmem_alloc(sizeof (struct scsi_hba_inst), KM_SLEEP);
401 
402 	mutex_enter(&scsi_hba_mutex);
403 
404 	elem->inst_dip = dip;
405 	elem->inst_hba_tran = hba_tran;
406 
407 	elem->inst_next = NULL;
408 	elem->inst_prev = scsi_hba_list_tail;
409 	if (scsi_hba_list == NULL) {
410 		scsi_hba_list = elem;
411 	}
412 	if (scsi_hba_list_tail) {
413 		scsi_hba_list_tail->inst_next = elem;
414 	}
415 	scsi_hba_list_tail = elem;
416 	mutex_exit(&scsi_hba_mutex);
417 
418 	/*
419 	 * Save all the important HBA information that must be accessed
420 	 * later by scsi_hba_bus_ctl(), and scsi_hba_map().
421 	 */
422 	hba_tran->tran_hba_dip = dip;
423 	hba_tran->tran_hba_flags &= SCSI_HBA_TRAN_ALLOC;
424 	hba_tran->tran_hba_flags |= (flags & ~SCSI_HBA_TRAN_ALLOC);
425 
426 	/*
427 	 * Note: we only need dma_attr_minxfer and dma_attr_burstsizes
428 	 * from the DMA attributes.  scsi_hba_attach(9f) only
429 	 * guarantees that these two fields are initialized properly.
430 	 * If this changes, be sure to revisit the implementation
431 	 * of scsi_hba_attach(9F).
432 	 */
433 	(void) memcpy(&hba_tran->tran_dma_attr, hba_dma_attr,
434 	    sizeof (ddi_dma_attr_t));
435 
436 	/* create kmem_cache, if needed */
437 	if (hba_tran->tran_setup_pkt) {
438 		char tmp[96];
439 		int hbalen;
440 		int cmdlen = 0;
441 		int statuslen = 0;
442 
443 		ASSERT(hba_tran->tran_init_pkt == NULL);
444 		ASSERT(hba_tran->tran_destroy_pkt == NULL);
445 
446 		hba_tran->tran_init_pkt = scsi_init_cache_pkt;
447 		hba_tran->tran_destroy_pkt = scsi_free_cache_pkt;
448 		hba_tran->tran_sync_pkt = scsi_sync_cache_pkt;
449 		hba_tran->tran_dmafree = scsi_cache_dmafree;
450 
451 		hbalen = ROUNDUP(hba_tran->tran_hba_len);
452 		if (flags & SCSI_HBA_TRAN_CDB)
453 			cmdlen = ROUNDUP(DEFAULT_CDBLEN);
454 		if (flags & SCSI_HBA_TRAN_SCB)
455 			statuslen = ROUNDUP(DEFAULT_SCBLEN);
456 
457 		(void) snprintf(tmp, sizeof (tmp), "pkt_cache_%s_%d",
458 		    ddi_driver_name(dip), ddi_get_instance(dip));
459 		hba_tran->tran_pkt_cache_ptr = kmem_cache_create(tmp,
460 		    sizeof (struct scsi_pkt_cache_wrapper) +
461 		    hbalen + cmdlen + statuslen, 8,
462 		    scsi_hba_pkt_constructor, scsi_hba_pkt_destructor,
463 		    NULL, hba_tran, NULL, 0);
464 	}
465 
466 	/*
467 	 * Attach scsi configuration property parameters
468 	 * to this instance of the hba.
469 	 */
470 	prop_name = "scsi-reset-delay";
471 	len = 0;
472 	if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN, 0, prop_name,
473 	    NULL, &len) == DDI_PROP_NOT_FOUND) {
474 		value = scsi_reset_delay;
475 		if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
476 		    prop_name, value) != DDI_PROP_SUCCESS) {
477 			cmn_err(CE_CONT, errmsg, prop_name,
478 			    ddi_driver_name(dip), ddi_get_instance(dip));
479 		}
480 	}
481 
482 	prop_name = "scsi-tag-age-limit";
483 	len = 0;
484 	if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN, 0, prop_name,
485 	    NULL, &len) == DDI_PROP_NOT_FOUND) {
486 		value = scsi_tag_age_limit;
487 		if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
488 		    prop_name, value) != DDI_PROP_SUCCESS) {
489 			cmn_err(CE_CONT, errmsg, prop_name,
490 			    ddi_driver_name(dip), ddi_get_instance(dip));
491 		}
492 	}
493 
494 	prop_name = "scsi-watchdog-tick";
495 	len = 0;
496 	if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN, 0, prop_name,
497 	    NULL, &len) == DDI_PROP_NOT_FOUND) {
498 		value = scsi_watchdog_tick;
499 		if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
500 		    prop_name, value) != DDI_PROP_SUCCESS) {
501 			cmn_err(CE_CONT, errmsg, prop_name,
502 			    ddi_driver_name(dip), ddi_get_instance(dip));
503 		}
504 	}
505 
506 	prop_name = "scsi-options";
507 	len = 0;
508 	if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN, 0, prop_name,
509 	    NULL, &len) == DDI_PROP_NOT_FOUND) {
510 		value = scsi_options;
511 		if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
512 		    prop_name, value) != DDI_PROP_SUCCESS) {
513 			cmn_err(CE_CONT, errmsg, prop_name,
514 			    ddi_driver_name(dip), ddi_get_instance(dip));
515 		}
516 	}
517 
518 	prop_name = "scsi-selection-timeout";
519 	len = 0;
520 	if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN, 0, prop_name,
521 	    NULL, &len) == DDI_PROP_NOT_FOUND) {
522 		value = scsi_selection_timeout;
523 		if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
524 		    prop_name, value) != DDI_PROP_SUCCESS) {
525 			cmn_err(CE_CONT, errmsg, prop_name,
526 			    ddi_driver_name(dip), ddi_get_instance(dip));
527 		}
528 	}
529 	if ((hba_tran->tran_hba_flags & SCSI_HBA_TRAN_ALLOC) &&
530 	    (hba_tran->tran_interconnect_type > 0) &&
531 	    (hba_tran->tran_interconnect_type < INTERCONNECT_MAX)) {
532 		prop_name = "initiator-interconnect-type";
533 		len = 0;
534 		if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN, 0, prop_name,
535 		    NULL, &len) == DDI_PROP_NOT_FOUND) {
536 			value = hba_tran->tran_interconnect_type;
537 			prop_value = interconnect[value];
538 			if (ddi_prop_update_string(DDI_DEV_T_NONE, dip,
539 			    prop_name, (char *)prop_value)
540 			    != DDI_PROP_SUCCESS) {
541 				cmn_err(CE_CONT, errmsg, prop_name,
542 				    ddi_driver_name(dip),
543 				    ddi_get_instance(dip));
544 			}
545 		}
546 	}
547 
548 	ddi_set_driver_private(dip, hba_tran);
549 
550 	/*
551 	 * Create devctl minor node unless driver supplied its own
552 	 * open/close entry points
553 	 */
554 	hba_dev_ops = ddi_get_driver(dip);
555 	ASSERT(hba_dev_ops != NULL);
556 	if (hba_dev_ops->devo_cb_ops->cb_open == scsi_hba_open) {
557 		/*
558 		 * Make sure that instance number doesn't overflow
559 		 * when forming minor numbers.
560 		 */
561 		ASSERT(ddi_get_instance(dip) <=
562 		    (L_MAXMIN >> INST_MINOR_SHIFT));
563 
564 		if ((ddi_create_minor_node(dip, "devctl", S_IFCHR,
565 		    INST2DEVCTL(ddi_get_instance(dip)),
566 		    DDI_NT_SCSI_NEXUS, 0) != DDI_SUCCESS) ||
567 		    (ddi_create_minor_node(dip, "scsi", S_IFCHR,
568 		    INST2SCSI(ddi_get_instance(dip)),
569 		    DDI_NT_SCSI_ATTACHMENT_POINT, 0) != DDI_SUCCESS)) {
570 			ddi_remove_minor_node(dip, "devctl");
571 			ddi_remove_minor_node(dip, "scsi");
572 			cmn_err(CE_WARN, "scsi_hba_attach: "
573 			    "cannot create devctl/scsi minor nodes");
574 		}
575 	}
576 
577 	/*
578 	 * NOTE: SCSA maintains an 'fm-capable' domain, in tran_fm_capable,
579 	 * that is not dependent (limited by) the capabilities of its parents.
580 	 * For example a dip in a branch that is not DDI_FM_EREPORT_CAPABLE
581 	 * may report as capable, via tran_fm_capable, to its scsi_device
582 	 * children.
583 	 *
584 	 * Get 'fm-capable' property from driver.conf, if present. If not
585 	 * present, default to the scsi_fm_capable global (which has
586 	 * DDI_FM_EREPORT_CAPABLE set by default).
587 	 */
588 	if (hba_tran->tran_fm_capable == DDI_FM_NOT_CAPABLE)
589 		hba_tran->tran_fm_capable = ddi_getprop(DDI_DEV_T_ANY, dip,
590 		    DDI_PROP_DONTPASS | DDI_PROP_CANSLEEP | DDI_PROP_NOTPROM,
591 		    "fm-capable", scsi_fm_capable);
592 
593 	/*
594 	 * If an HBA is *not* doing its own fma support by calling
595 	 * ddi_fm_init() prior to scsi_hba_attach_setup(), we provide a
596 	 * minimal common SCSA implementation so that scsi_device children
597 	 * can generate ereports via scsi_fm_ereport_post().  We use
598 	 * ddi_fm_capable() to detect an HBA calling ddi_fm_init() prior to
599 	 * scsi_hba_attach_setup().
600 	 */
601 	if (hba_tran->tran_fm_capable &&
602 	    (ddi_fm_capable(dip) == DDI_FM_NOT_CAPABLE)) {
603 		/*
604 		 * We are capable of something, pass our capabilities up
605 		 * the tree, but use a local variable so our parent can't
606 		 * limit our capabilities (we don't want our parent to
607 		 * clear DDI_FM_EREPORT_CAPABLE).
608 		 *
609 		 * NOTE: iblock cookies are not important because scsi
610 		 * HBAs always interrupt below LOCK_LEVEL.
611 		 */
612 		capable = hba_tran->tran_fm_capable;
613 		ddi_fm_init(dip, &capable, NULL);
614 
615 		/*
616 		 * Set SCSI_HBA_TRAN_FMSCSA bit to mark us as usiung the
617 		 * common minimal SCSA fm implementation -  we called
618 		 * ddi_fm_init(), so we are responsible for calling
619 		 * ddi_fm_fini() in scsi_hba_detach().
620 		 * NOTE: if ddi_fm_init fails in any reason, SKIP.
621 		 */
622 		if (DEVI(dip)->devi_fmhdl)
623 			hba_tran->tran_hba_flags |= SCSI_HBA_TRAN_FMSCSA;
624 	}
625 
626 	return (DDI_SUCCESS);
627 }
628 
629 /*
630  * Called by an HBA to detach an instance of the driver
631  */
632 int
633 scsi_hba_detach(dev_info_t *dip)
634 {
635 	struct dev_ops		*hba_dev_ops;
636 	scsi_hba_tran_t		*hba;
637 	struct scsi_hba_inst	*elem;
638 
639 
640 	hba = ddi_get_driver_private(dip);
641 	ddi_set_driver_private(dip, NULL);
642 	ASSERT(hba != NULL);
643 	ASSERT(hba->tran_open_flag == 0);
644 
645 	/*
646 	 * If we are taking care of mininal default fma implementation,
647 	 * call ddi_fm_fini(9F).
648 	 */
649 	if (hba->tran_hba_flags & SCSI_HBA_TRAN_FMSCSA) {
650 		ddi_fm_fini(dip);
651 	}
652 
653 	hba_dev_ops = ddi_get_driver(dip);
654 	ASSERT(hba_dev_ops != NULL);
655 	if (hba_dev_ops->devo_cb_ops->cb_open == scsi_hba_open) {
656 		ddi_remove_minor_node(dip, "devctl");
657 		ddi_remove_minor_node(dip, "scsi");
658 	}
659 
660 	/*
661 	 * XXX - scsi_transport.h states that these data fields should not be
662 	 *	 referenced by the HBA. However, to be consistent with
663 	 *	 scsi_hba_attach(), they are being reset.
664 	 */
665 	hba->tran_hba_dip = (dev_info_t *)NULL;
666 	hba->tran_hba_flags = 0;
667 	(void) memset(&hba->tran_dma_attr, 0, sizeof (ddi_dma_attr_t));
668 
669 	if (hba->tran_pkt_cache_ptr != NULL) {
670 		kmem_cache_destroy(hba->tran_pkt_cache_ptr);
671 		hba->tran_pkt_cache_ptr = NULL;
672 	}
673 	/*
674 	 * Remove HBA instance from scsi_hba_list
675 	 */
676 	mutex_enter(&scsi_hba_mutex);
677 	for (elem = scsi_hba_list; elem != (struct scsi_hba_inst *)NULL;
678 	    elem = elem->inst_next) {
679 		if (elem->inst_dip == dip)
680 			break;
681 	}
682 
683 	if (elem == (struct scsi_hba_inst *)NULL) {
684 		cmn_err(CE_CONT, "scsi_hba_attach: unknown HBA instance\n");
685 		mutex_exit(&scsi_hba_mutex);
686 		return (DDI_FAILURE);
687 	}
688 	if (elem == scsi_hba_list) {
689 		scsi_hba_list = elem->inst_next;
690 		if (scsi_hba_list) {
691 			scsi_hba_list->inst_prev = (struct scsi_hba_inst *)NULL;
692 		}
693 		if (elem == scsi_hba_list_tail) {
694 			scsi_hba_list_tail = NULL;
695 		}
696 	} else if (elem == scsi_hba_list_tail) {
697 		scsi_hba_list_tail = elem->inst_prev;
698 		if (scsi_hba_list_tail) {
699 			scsi_hba_list_tail->inst_next =
700 			    (struct scsi_hba_inst *)NULL;
701 		}
702 	} else {
703 		elem->inst_prev->inst_next = elem->inst_next;
704 		elem->inst_next->inst_prev = elem->inst_prev;
705 	}
706 	mutex_exit(&scsi_hba_mutex);
707 
708 	kmem_free(elem, sizeof (struct scsi_hba_inst));
709 
710 	return (DDI_SUCCESS);
711 }
712 
713 /*
714  * Called by an HBA from _fini()
715  */
716 void
717 scsi_hba_fini(struct modlinkage *modlp)
718 {
719 	struct dev_ops *hba_dev_ops;
720 
721 	/*
722 	 * Get the devops structure of this module
723 	 * and clear bus_ops vector.
724 	 */
725 	hba_dev_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
726 
727 	if (hba_dev_ops->devo_cb_ops == &scsi_hba_cbops) {
728 		hba_dev_ops->devo_cb_ops = NULL;
729 	}
730 
731 	if (hba_dev_ops->devo_getinfo == scsi_hba_info) {
732 		hba_dev_ops->devo_getinfo = NULL;
733 	}
734 
735 	hba_dev_ops->devo_bus_ops = (struct bus_ops *)NULL;
736 }
737 
738 static int
739 smp_ctlops_reportdev(dev_info_t	*dip, dev_info_t *rdip)
740 {
741 	scsi_hba_tran_t		*hba;
742 	char			*smp_wwn;
743 
744 	hba = ddi_get_driver_private(dip);
745 	ASSERT(hba != NULL);
746 
747 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, rdip,
748 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
749 	    SMP_WWN, &smp_wwn) != DDI_SUCCESS) {
750 		return (DDI_FAILURE);
751 	}
752 	cmn_err(CE_CONT,
753 	    "?%s%d at %s%d: wwn %s\n",
754 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
755 	    ddi_driver_name(dip), ddi_get_instance(dip),
756 	    smp_wwn);
757 
758 	ddi_prop_free(smp_wwn);
759 	return (DDI_SUCCESS);
760 }
761 
762 
763 static int
764 smp_ctlops_initchild(dev_info_t	*dip, dev_info_t *rdip)
765 {
766 	struct smp_device	*smp;
767 	char			name[SCSI_MAXNAMELEN];
768 	scsi_hba_tran_t		*hba;
769 	dev_info_t		*ndip;
770 	char			*smp_wwn;
771 	uint64_t		wwn;
772 
773 	hba = ddi_get_driver_private(dip);
774 
775 	if (hba == NULL)
776 		return (DDI_FAILURE);
777 
778 	smp = kmem_zalloc(sizeof (struct smp_device), KM_SLEEP);
779 
780 	/*
781 	 * Clone transport structure if requested, so
782 	 * the HBA can maintain target-specific info, if
783 	 * necessary.
784 	 */
785 	if (hba->tran_hba_flags & SCSI_HBA_TRAN_CLONE) {
786 		scsi_hba_tran_t	*clone =
787 		    kmem_alloc(sizeof (scsi_hba_tran_t), KM_SLEEP);
788 
789 		bcopy(hba, clone, sizeof (scsi_hba_tran_t));
790 		hba = clone;
791 	}
792 
793 	smp->dip = rdip;
794 	smp->smp_addr.a_hba_tran = hba;
795 
796 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, rdip,
797 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
798 	    SMP_WWN, &smp_wwn) != DDI_SUCCESS) {
799 		return (DDI_FAILURE);
800 	}
801 
802 	if (ddi_devid_str_to_wwn(smp_wwn, &wwn)) {
803 		goto failure;
804 	}
805 
806 	bcopy(&wwn, smp->smp_addr.a_wwn, SAS_WWN_BYTE_SIZE);
807 
808 	bzero(name, sizeof (SCSI_MAXNAMELEN));
809 
810 	(void) sprintf(name, "w%s", smp_wwn);
811 
812 	/*
813 	 * Prevent duplicate nodes.
814 	 */
815 	ndip = ndi_devi_find(dip, ddi_node_name(rdip), name);
816 
817 	if (ndip && (ndip != rdip)) {
818 		goto failure;
819 	}
820 
821 	ddi_set_name_addr(rdip, name);
822 
823 	ddi_set_driver_private(rdip, smp);
824 
825 	ddi_prop_free(smp_wwn);
826 
827 	return (DDI_SUCCESS);
828 
829 failure:
830 	kmem_free(smp, sizeof (struct smp_device));
831 	if (hba->tran_hba_flags & SCSI_HBA_TRAN_CLONE) {
832 		kmem_free(hba, sizeof (scsi_hba_tran_t));
833 	}
834 	ddi_prop_free(smp_wwn);
835 	return (DDI_FAILURE);
836 }
837 
838 static int
839 smp_ctlops_uninitchild(dev_info_t *dip, dev_info_t *rdip)
840 {
841 	struct smp_device	*smp;
842 	scsi_hba_tran_t		*hba;
843 
844 	hba = ddi_get_driver_private(dip);
845 	ASSERT(hba != NULL);
846 
847 	smp = ddi_get_driver_private(rdip);
848 	ASSERT(smp != NULL);
849 
850 	if (hba->tran_hba_flags & SCSI_HBA_TRAN_CLONE) {
851 		hba = smp->smp_addr.a_hba_tran;
852 		kmem_free(hba, sizeof (scsi_hba_tran_t));
853 	}
854 	kmem_free(smp, sizeof (*smp));
855 
856 	ddi_set_driver_private(rdip, NULL);
857 	ddi_set_name_addr(rdip, NULL);
858 
859 	return (DDI_SUCCESS);
860 }
861 
862 /*
863  * Generic bus_ctl operations for SCSI HBA's,
864  * hiding the busctl interface from the HBA.
865  */
866 /*ARGSUSED*/
867 static int
868 scsi_hba_bus_ctl(
869 	dev_info_t		*dip,
870 	dev_info_t		*rdip,
871 	ddi_ctl_enum_t		op,
872 	void			*arg,
873 	void			*result)
874 {
875 	switch (op) {
876 	case DDI_CTLOPS_REPORTDEV:
877 	{
878 		struct scsi_device	*devp;
879 		scsi_hba_tran_t		*hba;
880 
881 		hba = ddi_get_driver_private(dip);
882 		ASSERT(hba != NULL);
883 
884 		if (ddi_prop_exists(DDI_DEV_T_ANY, rdip,
885 		    DDI_PROP_DONTPASS | DDI_PROP_CANSLEEP | DDI_PROP_NOTPROM,
886 		    SMP_PROP)) {
887 			return (smp_ctlops_reportdev(dip, rdip));
888 		}
889 
890 		devp = ddi_get_driver_private(rdip);
891 
892 		if ((hba->tran_get_bus_addr == NULL) ||
893 		    (hba->tran_get_name == NULL)) {
894 			cmn_err(CE_CONT, "?%s%d at %s%d: target %x lun %x\n",
895 			    ddi_driver_name(rdip), ddi_get_instance(rdip),
896 			    ddi_driver_name(dip), ddi_get_instance(dip),
897 			    devp->sd_address.a_target, devp->sd_address.a_lun);
898 		} else {
899 			char name[SCSI_MAXNAMELEN];
900 			char bus_addr[SCSI_MAXNAMELEN];
901 
902 			if ((*hba->tran_get_name)(devp, name,
903 			    SCSI_MAXNAMELEN) != 1) {
904 				return (DDI_FAILURE);
905 			}
906 			if ((*hba->tran_get_bus_addr)(devp, bus_addr,
907 			    SCSI_MAXNAMELEN) != 1) {
908 				return (DDI_FAILURE);
909 			}
910 			cmn_err(CE_CONT,
911 			    "?%s%d at %s%d: name %s, bus address %s\n",
912 			    ddi_driver_name(rdip), ddi_get_instance(rdip),
913 			    ddi_driver_name(dip), ddi_get_instance(dip),
914 			    name, bus_addr);
915 		}
916 		return (DDI_SUCCESS);
917 	}
918 
919 	case DDI_CTLOPS_IOMIN:
920 	{
921 		int		val;
922 		scsi_hba_tran_t	*hba;
923 		ddi_dma_attr_t	*attr;
924 
925 		hba = ddi_get_driver_private(dip);
926 		ASSERT(hba != NULL);
927 		attr = &hba->tran_dma_attr;
928 
929 		val = *((int *)result);
930 		val = maxbit(val, attr->dma_attr_minxfer);
931 		/*
932 		 * The 'arg' value of nonzero indicates 'streaming'
933 		 * mode.  If in streaming mode, pick the largest
934 		 * of our burstsizes available and say that that
935 		 * is our minimum value (modulo what minxfer is).
936 		 */
937 		*((int *)result) = maxbit(val, ((intptr_t)arg ?
938 		    (1<<ddi_ffs(attr->dma_attr_burstsizes)-1) :
939 		    (1<<(ddi_fls(attr->dma_attr_burstsizes)-1))));
940 
941 		return (ddi_ctlops(dip, rdip, op, arg, result));
942 	}
943 
944 	case DDI_CTLOPS_INITCHILD:
945 	{
946 		dev_info_t		*child_dip = (dev_info_t *)arg;
947 		struct scsi_device	*sd;
948 		char			name[SCSI_MAXNAMELEN];
949 		scsi_hba_tran_t		*hba;
950 		dev_info_t		*ndip;
951 
952 		if (ddi_prop_exists(DDI_DEV_T_ANY, child_dip,
953 		    DDI_PROP_DONTPASS | DDI_PROP_CANSLEEP | DDI_PROP_NOTPROM,
954 		    SMP_PROP)) {
955 			return (smp_ctlops_initchild(dip, child_dip));
956 		}
957 
958 		hba = ddi_get_driver_private(dip);
959 
960 		/*
961 		 * For a driver like fp with multiple upper-layer-protocols
962 		 * it is possible for scsi_hba_init in _init to plumb SCSA
963 		 * and have the load of fcp (which does scsi_hba_attach_setup)
964 		 * to fail.  In this case we may get here with a NULL hba.
965 		 */
966 		if (hba == NULL)
967 			return (DDI_FAILURE);
968 
969 		sd = kmem_zalloc(sizeof (struct scsi_device), KM_SLEEP);
970 
971 		/*
972 		 * Clone transport structure if requested, so
973 		 * the HBA can maintain target-specific info, if
974 		 * necessary. At least all SCSI-3 HBAs will do this.
975 		 */
976 		if (hba->tran_hba_flags & SCSI_HBA_TRAN_CLONE) {
977 			scsi_hba_tran_t	*clone =
978 			    kmem_alloc(sizeof (scsi_hba_tran_t), KM_SLEEP);
979 
980 			bcopy(hba, clone, sizeof (scsi_hba_tran_t));
981 			hba = clone;
982 			hba->tran_sd = sd;
983 		} else {
984 			ASSERT(hba->tran_sd == NULL);
985 		}
986 
987 		sd->sd_dev = child_dip;
988 		sd->sd_address.a_hba_tran = hba;
989 
990 		/*
991 		 * Make sure that HBA either supports both or none
992 		 * of tran_get_name/tran_get_addr
993 		 */
994 		if ((hba->tran_get_name != NULL) ||
995 		    (hba->tran_get_bus_addr != NULL)) {
996 			if ((hba->tran_get_name == NULL) ||
997 			    (hba->tran_get_bus_addr == NULL)) {
998 				cmn_err(CE_CONT,
999 				    "%s%d: should support both or none of "
1000 				    "tran_get_name and tran_get_bus_addr\n",
1001 				    ddi_driver_name(dip),
1002 				    ddi_get_instance(dip));
1003 				goto failure;
1004 			}
1005 		}
1006 
1007 		/*
1008 		 * In case HBA doesn't support tran_get_name/tran_get_bus_addr
1009 		 * (e.g. most pre-SCSI-3 HBAs), we have to continue
1010 		 * to provide old semantics. In case a HBA driver does
1011 		 * support it, a_target and a_lun fields of scsi_address
1012 		 * are not defined and will be 0 except for parallel bus.
1013 		 */
1014 		{
1015 			int	t_len;
1016 			int	targ = 0;
1017 			int	lun = 0;
1018 
1019 			t_len = sizeof (targ);
1020 			if (ddi_prop_op(DDI_DEV_T_ANY, child_dip,
1021 			    PROP_LEN_AND_VAL_BUF, DDI_PROP_DONTPASS |
1022 			    DDI_PROP_CANSLEEP, "target", (caddr_t)&targ,
1023 			    &t_len) != DDI_SUCCESS) {
1024 				if (hba->tran_get_name == NULL) {
1025 					kmem_free(sd,
1026 					    sizeof (struct scsi_device));
1027 					if (hba->tran_hba_flags &
1028 					    SCSI_HBA_TRAN_CLONE) {
1029 						kmem_free(hba,
1030 						    sizeof (scsi_hba_tran_t));
1031 					}
1032 					return (DDI_NOT_WELL_FORMED);
1033 				}
1034 			}
1035 
1036 			t_len = sizeof (lun);
1037 			(void) ddi_prop_op(DDI_DEV_T_ANY, child_dip,
1038 			    PROP_LEN_AND_VAL_BUF, DDI_PROP_DONTPASS |
1039 			    DDI_PROP_CANSLEEP, "lun", (caddr_t)&lun,
1040 			    &t_len);
1041 
1042 			/*
1043 			 * If the HBA does not implement tran_get_name then it
1044 			 * doesn't have any hope of supporting a LUN >= 256.
1045 			 */
1046 			if (lun >= 256 && hba->tran_get_name == NULL) {
1047 				goto failure;
1048 			}
1049 
1050 			/*
1051 			 * This is also to make sure that if someone plugs in
1052 			 * a SCSI-2 disks to a SCSI-3 parallel bus HBA,
1053 			 * his SCSI-2 target driver still continue to work.
1054 			 */
1055 			sd->sd_address.a_target = (ushort_t)targ;
1056 			sd->sd_address.a_lun = (uchar_t)lun;
1057 		}
1058 
1059 		/*
1060 		 * In case HBA support tran_get_name (e.g. all SCSI-3 HBAs),
1061 		 * give it a chance to tell us the name.
1062 		 * If it doesn't support this entry point, a name will be
1063 		 * fabricated
1064 		 */
1065 		if (scsi_get_name(sd, name, SCSI_MAXNAMELEN) != 1) {
1066 			goto failure;
1067 		}
1068 
1069 		/*
1070 		 * Prevent duplicate nodes.
1071 		 */
1072 		ndip = ndi_devi_find(dip, ddi_node_name(child_dip), name);
1073 
1074 		if (ndip && (ndip != child_dip)) {
1075 			goto failure;
1076 		}
1077 
1078 		ddi_set_name_addr(child_dip, name);
1079 
1080 		/*
1081 		 * This is a grotty hack that allows direct-access
1082 		 * (non-scsi) drivers using this interface to
1083 		 * put its own vector in the 'a_hba_tran' field.
1084 		 * When the drivers are fixed, remove this hack.
1085 		 */
1086 		sd->sd_reserved = hba;
1087 
1088 		/*
1089 		 * call hba's target init entry point if it exists
1090 		 */
1091 		if (hba->tran_tgt_init != NULL) {
1092 			if ((*hba->tran_tgt_init)
1093 			    (dip, child_dip, hba, sd) != DDI_SUCCESS) {
1094 				ddi_set_name_addr(child_dip, NULL);
1095 				goto failure;
1096 			}
1097 
1098 			/*
1099 			 * Another grotty hack to undo initialization
1100 			 * some hba's think they have authority to
1101 			 * perform.
1102 			 *
1103 			 * XXX - Pending dadk_probe() semantics
1104 			 *	 change.  (Re: 1171432)
1105 			 */
1106 			if (hba->tran_tgt_probe != NULL)
1107 				sd->sd_inq = NULL;
1108 		}
1109 
1110 		mutex_init(&sd->sd_mutex, NULL, MUTEX_DRIVER, NULL);
1111 
1112 		ddi_set_driver_private(child_dip, sd);
1113 
1114 		return (DDI_SUCCESS);
1115 
1116 failure:
1117 		kmem_free(sd, sizeof (struct scsi_device));
1118 		if (hba->tran_hba_flags & SCSI_HBA_TRAN_CLONE) {
1119 			kmem_free(hba, sizeof (scsi_hba_tran_t));
1120 		}
1121 		return (DDI_FAILURE);
1122 	}
1123 
1124 	case DDI_CTLOPS_UNINITCHILD:
1125 	{
1126 		struct scsi_device	*sd;
1127 		dev_info_t		*child_dip = (dev_info_t *)arg;
1128 		scsi_hba_tran_t		*hba;
1129 
1130 		if (ddi_prop_exists(DDI_DEV_T_ANY, child_dip,
1131 		    DDI_PROP_DONTPASS | DDI_PROP_CANSLEEP | DDI_PROP_NOTPROM,
1132 		    SMP_PROP)) {
1133 			return (smp_ctlops_uninitchild(dip, child_dip));
1134 		}
1135 
1136 		hba = ddi_get_driver_private(dip);
1137 		ASSERT(hba != NULL);
1138 
1139 		sd = ddi_get_driver_private(child_dip);
1140 		ASSERT(sd != NULL);
1141 
1142 		if (hba->tran_hba_flags & SCSI_HBA_TRAN_CLONE) {
1143 			/*
1144 			 * This is a grotty hack, continued.  This
1145 			 * should be:
1146 			 *	hba = sd->sd_address.a_hba_tran;
1147 			 */
1148 			hba = sd->sd_reserved;
1149 			ASSERT(hba->tran_hba_flags & SCSI_HBA_TRAN_CLONE);
1150 			ASSERT(hba->tran_sd == sd);
1151 		} else {
1152 			ASSERT(hba->tran_sd == NULL);
1153 		}
1154 
1155 		scsi_unprobe(sd);
1156 		if (hba->tran_tgt_free != NULL) {
1157 			(*hba->tran_tgt_free) (dip, child_dip, hba, sd);
1158 		}
1159 		mutex_destroy(&sd->sd_mutex);
1160 		if (hba->tran_hba_flags & SCSI_HBA_TRAN_CLONE) {
1161 			kmem_free(hba, sizeof (scsi_hba_tran_t));
1162 		}
1163 		kmem_free(sd, sizeof (*sd));
1164 
1165 		ddi_set_driver_private(child_dip, NULL);
1166 		ddi_set_name_addr(child_dip, NULL);
1167 
1168 		return (DDI_SUCCESS);
1169 	}
1170 	case DDI_CTLOPS_SIDDEV:
1171 		return (ndi_dev_is_persistent_node(rdip) ?
1172 		    DDI_SUCCESS : DDI_FAILURE);
1173 
1174 	/* XXX these should be handled */
1175 	case DDI_CTLOPS_POWER:
1176 	case DDI_CTLOPS_ATTACH:
1177 	case DDI_CTLOPS_DETACH:
1178 
1179 		return (DDI_SUCCESS);
1180 
1181 	/*
1182 	 * These ops correspond to functions that "shouldn't" be called
1183 	 * by a SCSI target driver.  So we whine when we're called.
1184 	 */
1185 	case DDI_CTLOPS_DMAPMAPC:
1186 	case DDI_CTLOPS_REPORTINT:
1187 	case DDI_CTLOPS_REGSIZE:
1188 	case DDI_CTLOPS_NREGS:
1189 	case DDI_CTLOPS_SLAVEONLY:
1190 	case DDI_CTLOPS_AFFINITY:
1191 	case DDI_CTLOPS_POKE:
1192 	case DDI_CTLOPS_PEEK:
1193 		cmn_err(CE_CONT, "%s%d: invalid op (%d) from %s%d\n",
1194 		    ddi_driver_name(dip), ddi_get_instance(dip),
1195 		    op, ddi_driver_name(rdip), ddi_get_instance(rdip));
1196 		return (DDI_FAILURE);
1197 
1198 	/*
1199 	 * Everything else (e.g. PTOB/BTOP/BTOPR requests) we pass up
1200 	 */
1201 	default:
1202 		return (ddi_ctlops(dip, rdip, op, arg, result));
1203 	}
1204 }
1205 
1206 
1207 /*
1208  * Called by an HBA to allocate a scsi_hba_tran structure
1209  */
1210 /*ARGSUSED*/
1211 scsi_hba_tran_t *
1212 scsi_hba_tran_alloc(
1213 	dev_info_t		*dip,
1214 	int			flags)
1215 {
1216 	scsi_hba_tran_t		*hba_tran;
1217 
1218 	hba_tran = kmem_zalloc(sizeof (scsi_hba_tran_t),
1219 	    (flags & SCSI_HBA_CANSLEEP) ? KM_SLEEP : KM_NOSLEEP);
1220 
1221 	hba_tran->tran_interconnect_type = INTERCONNECT_PARALLEL;
1222 	hba_tran->tran_hba_flags |= SCSI_HBA_TRAN_ALLOC;
1223 
1224 	return (hba_tran);
1225 }
1226 
1227 int
1228 scsi_tran_ext_alloc(
1229 	scsi_hba_tran_t		*hba_tran,
1230 	size_t			length,
1231 	int			flags)
1232 {
1233 	void	*hba_tran_ext;
1234 	int	ret = DDI_FAILURE;
1235 
1236 	hba_tran_ext = kmem_zalloc(length, (flags & SCSI_HBA_CANSLEEP)
1237 	    ? KM_SLEEP : KM_NOSLEEP);
1238 	if (hba_tran_ext != NULL) {
1239 		hba_tran->tran_extension = hba_tran_ext;
1240 		ret = DDI_SUCCESS;
1241 	}
1242 	return (ret);
1243 }
1244 
1245 void
1246 scsi_tran_ext_free(
1247 	scsi_hba_tran_t		*hba_tran,
1248 	size_t			length)
1249 {
1250 	if (hba_tran->tran_extension != NULL) {
1251 		kmem_free(hba_tran->tran_extension, length);
1252 		hba_tran->tran_extension = NULL;
1253 	}
1254 }
1255 
1256 /*
1257  * Called by an HBA to free a scsi_hba_tran structure
1258  */
1259 void
1260 scsi_hba_tran_free(
1261 	scsi_hba_tran_t		*hba_tran)
1262 {
1263 	kmem_free(hba_tran, sizeof (scsi_hba_tran_t));
1264 }
1265 
1266 /*
1267  * Private wrapper for scsi_pkt's allocated via scsi_hba_pkt_alloc()
1268  */
1269 struct scsi_pkt_wrapper {
1270 	struct scsi_pkt		scsi_pkt;
1271 	int			pkt_wrapper_magic;
1272 	int			pkt_wrapper_len;
1273 };
1274 
1275 #if !defined(lint)
1276 _NOTE(SCHEME_PROTECTS_DATA("unique per thread", scsi_pkt_wrapper))
1277 _NOTE(SCHEME_PROTECTS_DATA("Unshared Data", dev_ops))
1278 #endif
1279 
1280 /*
1281  * Called by an HBA to allocate a scsi_pkt
1282  */
1283 /*ARGSUSED*/
1284 struct scsi_pkt *
1285 scsi_hba_pkt_alloc(
1286 	dev_info_t		*dip,
1287 	struct scsi_address	*ap,
1288 	int			cmdlen,
1289 	int			statuslen,
1290 	int			tgtlen,
1291 	int			hbalen,
1292 	int			(*callback)(caddr_t arg),
1293 	caddr_t			arg)
1294 {
1295 	struct scsi_pkt		*pkt;
1296 	struct scsi_pkt_wrapper	*hba_pkt;
1297 	caddr_t			p;
1298 	int			acmdlen, astatuslen, atgtlen, ahbalen;
1299 	int			pktlen;
1300 
1301 	/*
1302 	 * Sanity check
1303 	 */
1304 	if (callback != SLEEP_FUNC && callback != NULL_FUNC) {
1305 		cmn_err(CE_PANIC, "scsi_hba_pkt_alloc: callback must be"
1306 		    " either SLEEP or NULL\n");
1307 	}
1308 
1309 	/*
1310 	 * Round up so everything gets allocated on long-word boundaries
1311 	 */
1312 	acmdlen = ROUNDUP(cmdlen);
1313 	astatuslen = ROUNDUP(statuslen);
1314 	atgtlen = ROUNDUP(tgtlen);
1315 	ahbalen = ROUNDUP(hbalen);
1316 	pktlen = sizeof (struct scsi_pkt_wrapper) +
1317 	    acmdlen + astatuslen + atgtlen + ahbalen;
1318 
1319 	hba_pkt = kmem_zalloc(pktlen,
1320 	    (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP);
1321 	if (hba_pkt == NULL) {
1322 		ASSERT(callback == NULL_FUNC);
1323 		return (NULL);
1324 	}
1325 
1326 	/*
1327 	 * Set up our private info on this pkt
1328 	 */
1329 	hba_pkt->pkt_wrapper_len = pktlen;
1330 	hba_pkt->pkt_wrapper_magic = PKT_WRAPPER_MAGIC;	/* alloced correctly */
1331 	pkt = &hba_pkt->scsi_pkt;
1332 
1333 	/*
1334 	 * Set up pointers to private data areas, cdb, and status.
1335 	 */
1336 	p = (caddr_t)(hba_pkt + 1);
1337 	if (hbalen > 0) {
1338 		pkt->pkt_ha_private = (opaque_t)p;
1339 		p += ahbalen;
1340 	}
1341 	if (tgtlen > 0) {
1342 		pkt->pkt_private = (opaque_t)p;
1343 		p += atgtlen;
1344 	}
1345 	if (statuslen > 0) {
1346 		pkt->pkt_scbp = (uchar_t *)p;
1347 		p += astatuslen;
1348 	}
1349 	if (cmdlen > 0) {
1350 		pkt->pkt_cdbp = (uchar_t *)p;
1351 	}
1352 
1353 	/*
1354 	 * Initialize the pkt's scsi_address
1355 	 */
1356 	pkt->pkt_address = *ap;
1357 
1358 	/*
1359 	 * NB: It may not be safe for drivers, esp target drivers, to depend
1360 	 * on the following fields being set until all the scsi_pkt
1361 	 * allocation violations discussed in scsi_pkt.h are all resolved.
1362 	 */
1363 	pkt->pkt_cdblen = cmdlen;
1364 	pkt->pkt_tgtlen = tgtlen;
1365 	pkt->pkt_scblen = statuslen;
1366 
1367 	return (pkt);
1368 }
1369 
1370 /*
1371  * Called by an HBA to free a scsi_pkt
1372  */
1373 /*ARGSUSED*/
1374 void
1375 scsi_hba_pkt_free(
1376 	struct scsi_address	*ap,
1377 	struct scsi_pkt		*pkt)
1378 {
1379 	kmem_free(pkt, ((struct scsi_pkt_wrapper *)pkt)->pkt_wrapper_len);
1380 }
1381 
1382 /*
1383  * Return 1 if the scsi_pkt used a proper allocator.
1384  *
1385  * The DDI does not allow a driver to allocate it's own scsi_pkt(9S), a
1386  * driver should not have *any* compiled in dependencies on "sizeof (struct
1387  * scsi_pkt)". While this has been the case for many years, a number of
1388  * drivers have still not been fixed. This function can be used to detect
1389  * improperly allocated scsi_pkt structures, and produce messages identifying
1390  * drivers that need to be fixed.
1391  *
1392  * While drivers in violation are being fixed, this function can also
1393  * be used by the framework to detect packets that violated allocation
1394  * rules.
1395  *
1396  * NB: It is possible, but very unlikely, for this code to return a false
1397  * positive (finding correct magic, but for wrong reasons).  Careful
1398  * consideration is needed for callers using this interface to condition
1399  * access to newer scsi_pkt fields (those after pkt_reason).
1400  *
1401  * NB: As an aid to minimizing the amount of work involved in 'fixing' legacy
1402  * drivers that violate scsi_*(9S) allocation rules, private
1403  * scsi_pkt_size()/scsi_size_clean() functions are available (see their
1404  * implementation for details).
1405  *
1406  * *** Non-legacy use of scsi_pkt_size() is discouraged. ***
1407  *
1408  * NB: When supporting broken HBA drivers is not longer a concern, this
1409  * code should be removed.
1410  */
1411 int
1412 scsi_pkt_allocated_correctly(struct scsi_pkt *pkt)
1413 {
1414 	struct scsi_pkt_wrapper	*hba_pkt = (struct scsi_pkt_wrapper *)pkt;
1415 	int	magic;
1416 	major_t	major;
1417 #ifdef	DEBUG
1418 	int	*pspwm, *pspcwm;
1419 
1420 	/*
1421 	 * We are getting scsi packets from two 'correct' wrapper schemes,
1422 	 * make sure we are looking at the same place in both to detect
1423 	 * proper allocation.
1424 	 */
1425 	pspwm = &((struct scsi_pkt_wrapper *)0)->pkt_wrapper_magic;
1426 	pspcwm = &((struct scsi_pkt_cache_wrapper *)0)->pcw_magic;
1427 	ASSERT(pspwm == pspcwm);
1428 #endif	/* DEBUG */
1429 
1430 
1431 	/*
1432 	 * Check to see if driver is scsi_size_clean(), assume it
1433 	 * is using the scsi_pkt_size() interface everywhere it needs to
1434 	 * if the driver indicates it is scsi_size_clean().
1435 	 */
1436 	major = ddi_driver_major(P_TO_TRAN(pkt)->tran_hba_dip);
1437 	if (devnamesp[major].dn_flags & DN_SCSI_SIZE_CLEAN)
1438 		return (1);		/* ok */
1439 
1440 	/*
1441 	 * Special case crossing a page boundary. If the scsi_pkt was not
1442 	 * allocated correctly, then accross a page boundary we have a
1443 	 * fault hazzard.
1444 	 */
1445 	if ((((uintptr_t)(&hba_pkt->scsi_pkt)) & MMU_PAGEMASK) ==
1446 	    (((uintptr_t)(&hba_pkt->pkt_wrapper_magic)) & MMU_PAGEMASK)) {
1447 		/* fastpath, no cross-page hazzard */
1448 		magic = hba_pkt->pkt_wrapper_magic;
1449 	} else {
1450 		/* add protection for cross-page hazzard */
1451 		if (ddi_peek32((dev_info_t *)NULL,
1452 		    &hba_pkt->pkt_wrapper_magic, &magic) == DDI_FAILURE) {
1453 			return (0);	/* violation */
1454 		}
1455 	}
1456 
1457 	/* properly allocated packet always has correct magic */
1458 	return ((magic == PKT_WRAPPER_MAGIC) ? 1 : 0);
1459 }
1460 
1461 /*
1462  * Private interfaces to simplify conversion of legacy drivers so they don't
1463  * depend on scsi_*(9S) size. Instead of using these private interface, HBA
1464  * drivers should use DDI sanctioned allocation methods:
1465  *
1466  *	scsi_pkt	Use scsi_hba_pkt_alloc(9F), or implement
1467  *			tran_setup_pkt(9E).
1468  *
1469  *	scsi_device	You are doing something strange/special, a scsi_device
1470  *			structure should only be allocated by scsi_hba.c
1471  *			initchild code or scsi_vhci.c code.
1472  *
1473  *	scsi_hba_tran	Use scsi_hba_tran_alloc(9F).
1474  */
1475 size_t
1476 scsi_pkt_size()
1477 {
1478 	return (sizeof (struct scsi_pkt));
1479 }
1480 
1481 size_t
1482 scsi_hba_tran_size()
1483 {
1484 	return (sizeof (scsi_hba_tran_t));
1485 }
1486 
1487 size_t
1488 scsi_device_size()
1489 {
1490 	return (sizeof (struct scsi_device));
1491 }
1492 
1493 /*
1494  * Legacy compliance to scsi_pkt(9S) allocation rules through use of
1495  * scsi_pkt_size() is detected by the 'scsi-size-clean' driver.conf property
1496  * or an HBA driver calling to scsi_size_clean() from attach(9E).  A driver
1497  * developer should only indicate that a legacy driver is clean after using
1498  * SCSI_SIZE_CLEAN_VERIFY to ensure compliance (see scsi_pkt.h).
1499  */
1500 void
1501 scsi_size_clean(dev_info_t *dip)
1502 {
1503 	major_t		major;
1504 	struct devnames	*dnp;
1505 
1506 	ASSERT(dip);
1507 	major = ddi_driver_major(dip);
1508 	ASSERT(major < devcnt);
1509 	if (major >= devcnt) {
1510 		cmn_err(CE_WARN, "scsi_pkt_size: bogus major: %d", major);
1511 		return;
1512 	}
1513 
1514 	/* Set DN_SCSI_SIZE_CLEAN flag in dn_flags. */
1515 	dnp = &devnamesp[major];
1516 	if ((dnp->dn_flags & DN_SCSI_SIZE_CLEAN) == 0) {
1517 		LOCK_DEV_OPS(&dnp->dn_lock);
1518 		dnp->dn_flags |= DN_SCSI_SIZE_CLEAN;
1519 		UNLOCK_DEV_OPS(&dnp->dn_lock);
1520 	}
1521 }
1522 
1523 
1524 /*
1525  * Called by an HBA to map strings to capability indices
1526  */
1527 int
1528 scsi_hba_lookup_capstr(
1529 	char			*capstr)
1530 {
1531 	/*
1532 	 * Capability strings, masking the the '-' vs. '_' misery
1533 	 */
1534 	static struct cap_strings {
1535 		char	*cap_string;
1536 		int	cap_index;
1537 	} cap_strings[] = {
1538 		{ "dma_max",		SCSI_CAP_DMA_MAX		},
1539 		{ "dma-max",		SCSI_CAP_DMA_MAX		},
1540 		{ "msg_out",		SCSI_CAP_MSG_OUT		},
1541 		{ "msg-out",		SCSI_CAP_MSG_OUT		},
1542 		{ "disconnect",		SCSI_CAP_DISCONNECT		},
1543 		{ "synchronous",	SCSI_CAP_SYNCHRONOUS		},
1544 		{ "wide_xfer",		SCSI_CAP_WIDE_XFER		},
1545 		{ "wide-xfer",		SCSI_CAP_WIDE_XFER		},
1546 		{ "parity",		SCSI_CAP_PARITY			},
1547 		{ "initiator-id",	SCSI_CAP_INITIATOR_ID		},
1548 		{ "untagged-qing",	SCSI_CAP_UNTAGGED_QING		},
1549 		{ "tagged-qing",	SCSI_CAP_TAGGED_QING		},
1550 		{ "auto-rqsense",	SCSI_CAP_ARQ			},
1551 		{ "linked-cmds",	SCSI_CAP_LINKED_CMDS		},
1552 		{ "sector-size",	SCSI_CAP_SECTOR_SIZE		},
1553 		{ "total-sectors",	SCSI_CAP_TOTAL_SECTORS		},
1554 		{ "geometry",		SCSI_CAP_GEOMETRY		},
1555 		{ "reset-notification",	SCSI_CAP_RESET_NOTIFICATION	},
1556 		{ "qfull-retries",	SCSI_CAP_QFULL_RETRIES		},
1557 		{ "qfull-retry-interval", SCSI_CAP_QFULL_RETRY_INTERVAL	},
1558 		{ "scsi-version",	SCSI_CAP_SCSI_VERSION		},
1559 		{ "interconnect-type",	SCSI_CAP_INTERCONNECT_TYPE	},
1560 		{ "lun-reset",		SCSI_CAP_LUN_RESET		},
1561 		{ "max-cdb-length",	SCSI_CAP_CDB_LEN		},
1562 		{ "dma-max-arch",	SCSI_CAP_DMA_MAX_ARCH		},
1563 		{ NULL,			0				}
1564 	};
1565 	struct cap_strings	*cp;
1566 
1567 	for (cp = cap_strings; cp->cap_string != NULL; cp++) {
1568 		if (strcmp(cp->cap_string, capstr) == 0) {
1569 			return (cp->cap_index);
1570 		}
1571 	}
1572 
1573 	return (-1);
1574 }
1575 
1576 
1577 /*
1578  * Called by an HBA to determine if the system is in 'panic' state.
1579  */
1580 int
1581 scsi_hba_in_panic()
1582 {
1583 	return (panicstr != NULL);
1584 }
1585 
1586 
1587 
1588 /*
1589  * If a SCSI target driver attempts to mmap memory,
1590  * the buck stops here.
1591  */
1592 /*ARGSUSED*/
1593 static int
1594 scsi_hba_map_fault(
1595 	dev_info_t		*dip,
1596 	dev_info_t		*rdip,
1597 	struct hat		*hat,
1598 	struct seg		*seg,
1599 	caddr_t			addr,
1600 	struct devpage		*dp,
1601 	pfn_t			pfn,
1602 	uint_t			prot,
1603 	uint_t			lock)
1604 {
1605 	return (DDI_FAILURE);
1606 }
1607 
1608 
1609 static int
1610 scsi_hba_get_eventcookie(
1611 	dev_info_t		*dip,
1612 	dev_info_t		*rdip,
1613 	char			*name,
1614 	ddi_eventcookie_t	*eventp)
1615 {
1616 	scsi_hba_tran_t		*hba;
1617 
1618 	hba = ddi_get_driver_private(dip);
1619 	if (hba->tran_get_eventcookie && ((*hba->tran_get_eventcookie)(dip,
1620 	    rdip, name, eventp) == DDI_SUCCESS)) {
1621 		return (DDI_SUCCESS);
1622 	}
1623 
1624 	return (ndi_busop_get_eventcookie(dip, rdip, name, eventp));
1625 }
1626 
1627 
1628 static int
1629 scsi_hba_add_eventcall(
1630 	dev_info_t		*dip,
1631 	dev_info_t		*rdip,
1632 	ddi_eventcookie_t	event,
1633 	void			(*callback)(
1634 					dev_info_t *dip,
1635 					ddi_eventcookie_t event,
1636 					void *arg,
1637 					void *bus_impldata),
1638 	void			*arg,
1639 	ddi_callback_id_t	*cb_id)
1640 {
1641 	scsi_hba_tran_t		*hba;
1642 
1643 	hba = ddi_get_driver_private(dip);
1644 	if (hba->tran_add_eventcall && ((*hba->tran_add_eventcall)(dip, rdip,
1645 	    event, callback, arg, cb_id) == DDI_SUCCESS)) {
1646 		return (DDI_SUCCESS);
1647 	}
1648 
1649 	return (DDI_FAILURE);
1650 }
1651 
1652 
1653 static int
1654 scsi_hba_remove_eventcall(dev_info_t *devi, ddi_callback_id_t cb_id)
1655 {
1656 	scsi_hba_tran_t		*hba;
1657 	ASSERT(cb_id);
1658 
1659 	hba = ddi_get_driver_private(devi);
1660 	if (hba->tran_remove_eventcall && ((*hba->tran_remove_eventcall)(
1661 	    devi, cb_id) == DDI_SUCCESS)) {
1662 		return (DDI_SUCCESS);
1663 	}
1664 
1665 	return (DDI_FAILURE);
1666 }
1667 
1668 
1669 static int
1670 scsi_hba_post_event(
1671 	dev_info_t		*dip,
1672 	dev_info_t		*rdip,
1673 	ddi_eventcookie_t	event,
1674 	void			*bus_impldata)
1675 {
1676 	scsi_hba_tran_t		*hba;
1677 
1678 	hba = ddi_get_driver_private(dip);
1679 	if (hba->tran_post_event && ((*hba->tran_post_event)(dip,
1680 	    rdip, event, bus_impldata) == DDI_SUCCESS)) {
1681 		return (DDI_SUCCESS);
1682 	}
1683 
1684 	return (DDI_FAILURE);
1685 }
1686 
1687 /*
1688  * The attach/detach of individual instances is controlled by the DDI
1689  * framework, hence, DDI_DEVT2DEVINFO doesn't make much sense (because
1690  * it ask drivers to hold individual dips in memory.
1691  */
1692 static dev_info_t *
1693 devt_to_devinfo(dev_t dev)
1694 {
1695 	dev_info_t *dip;
1696 	struct devnames *dnp;
1697 	major_t major = getmajor(dev);
1698 	int instance = MINOR2INST(getminor(dev));
1699 
1700 	if (major >= devcnt) {
1701 		return (NULL);
1702 	}
1703 
1704 	dnp = &devnamesp[major];
1705 	LOCK_DEV_OPS(&(dnp->dn_lock));
1706 	dip = dnp->dn_head;
1707 	while (dip && (ddi_get_instance(dip) != instance)) {
1708 		dip = ddi_get_next(dip);
1709 	}
1710 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
1711 
1712 	return (dip);
1713 }
1714 
1715 /*
1716  * Default getinfo(9e) for scsi_hba
1717  */
1718 /* ARGSUSED */
1719 static int
1720 scsi_hba_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
1721     void **result)
1722 {
1723 	int error = DDI_SUCCESS;
1724 
1725 	switch (infocmd) {
1726 	case DDI_INFO_DEVT2DEVINFO:
1727 		*result = (void *)devt_to_devinfo((dev_t)arg);
1728 		if (*result == NULL) {
1729 			error = DDI_FAILURE;
1730 		}
1731 		break;
1732 	case DDI_INFO_DEVT2INSTANCE:
1733 		*result = (void *)(intptr_t)(MINOR2INST(getminor((dev_t)arg)));
1734 		break;
1735 	default:
1736 		error = DDI_FAILURE;
1737 	}
1738 	return (error);
1739 }
1740 
1741 /*
1742  * Default open and close routine for scsi_hba
1743  */
1744 
1745 /* ARGSUSED */
1746 int
1747 scsi_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1748 {
1749 	int rv = 0;
1750 	dev_info_t *dip;
1751 	scsi_hba_tran_t *hba;
1752 
1753 	if (otyp != OTYP_CHR)
1754 		return (EINVAL);
1755 
1756 	dip = devt_to_devinfo(*devp);
1757 	if (dip == NULL)
1758 		return (ENXIO);
1759 
1760 	if ((hba = ddi_get_driver_private(dip)) == NULL)
1761 		return (ENXIO);
1762 
1763 	/*
1764 	 * tran_open_flag bit field:
1765 	 *	0:	closed
1766 	 *	1:	shared open by minor at bit position
1767 	 *	1 at 31st bit:	exclusive open
1768 	 */
1769 	mutex_enter(&(hba->tran_open_lock));
1770 	if (flags & FEXCL) {
1771 		if (hba->tran_open_flag != 0) {
1772 			rv = EBUSY;		/* already open */
1773 		} else {
1774 			hba->tran_open_flag = TRAN_OPEN_EXCL;
1775 		}
1776 	} else {
1777 		if (hba->tran_open_flag == TRAN_OPEN_EXCL) {
1778 			rv = EBUSY;		/* already excl. open */
1779 		} else {
1780 			int minor = getminor(*devp) & TRAN_MINOR_MASK;
1781 			hba->tran_open_flag |= (1 << minor);
1782 			/*
1783 			 * Ensure that the last framework reserved minor
1784 			 * is unused. Otherwise, the exclusive open
1785 			 * mechanism may break.
1786 			 */
1787 			ASSERT(minor != 31);
1788 		}
1789 	}
1790 	mutex_exit(&(hba->tran_open_lock));
1791 
1792 	return (rv);
1793 }
1794 
1795 /* ARGSUSED */
1796 int
1797 scsi_hba_close(dev_t dev, int flag, int otyp, cred_t *credp)
1798 {
1799 	dev_info_t *dip;
1800 	scsi_hba_tran_t *hba;
1801 
1802 	if (otyp != OTYP_CHR)
1803 		return (EINVAL);
1804 
1805 	dip = devt_to_devinfo(dev);
1806 	if (dip == NULL)
1807 		return (ENXIO);
1808 
1809 	if ((hba = ddi_get_driver_private(dip)) == NULL)
1810 		return (ENXIO);
1811 
1812 	mutex_enter(&(hba->tran_open_lock));
1813 	if (hba->tran_open_flag == TRAN_OPEN_EXCL) {
1814 		hba->tran_open_flag = 0;
1815 	} else {
1816 		int minor = getminor(dev) & TRAN_MINOR_MASK;
1817 		hba->tran_open_flag &= ~(1 << minor);
1818 	}
1819 	mutex_exit(&(hba->tran_open_lock));
1820 	return (0);
1821 }
1822 
1823 /*
1824  * standard ioctl commands for SCSI hotplugging
1825  */
1826 
1827 /* ARGSUSED */
1828 int
1829 scsi_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1830 	int *rvalp)
1831 {
1832 	dev_info_t *self;
1833 	dev_info_t *child;
1834 	struct scsi_device *sd;
1835 	scsi_hba_tran_t *hba;
1836 	struct devctl_iocdata *dcp;
1837 	uint_t bus_state;
1838 	int rv = 0;
1839 	int circ;
1840 
1841 	self = devt_to_devinfo(dev);
1842 	if (self == NULL)
1843 		return (ENXIO);
1844 
1845 	if ((hba = ddi_get_driver_private(self)) == NULL)
1846 		return (ENXIO);
1847 
1848 	/*
1849 	 * For these ioctls, the general implementation suffices
1850 	 */
1851 	switch (cmd) {
1852 	case DEVCTL_DEVICE_GETSTATE:
1853 	case DEVCTL_DEVICE_ONLINE:
1854 	case DEVCTL_DEVICE_OFFLINE:
1855 	case DEVCTL_DEVICE_REMOVE:
1856 	case DEVCTL_BUS_GETSTATE:
1857 		return (ndi_devctl_ioctl(self, cmd, arg, mode, 0));
1858 	}
1859 
1860 	switch (cmd) {
1861 
1862 	case DEVCTL_DEVICE_RESET:
1863 		if (hba->tran_reset == NULL) {
1864 			rv = ENOTSUP;
1865 			break;
1866 		}
1867 		/*
1868 		 * read devctl ioctl data
1869 		 */
1870 		if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
1871 			return (EFAULT);
1872 		if (ndi_dc_getname(dcp) == NULL ||
1873 		    ndi_dc_getaddr(dcp) == NULL) {
1874 			ndi_dc_freehdl(dcp);
1875 			return (EINVAL);
1876 		}
1877 
1878 		ndi_devi_enter(self, &circ);
1879 
1880 		child = ndi_devi_find(self,
1881 		    ndi_dc_getname(dcp), ndi_dc_getaddr(dcp));
1882 		if (child == NULL) {
1883 			ndi_devi_exit(self, circ);
1884 			ndi_dc_freehdl(dcp);
1885 			return (ENXIO);
1886 		}
1887 
1888 		ndi_hold_devi(child);
1889 		ndi_devi_exit(self, circ);
1890 
1891 		/*
1892 		 * See DDI_CTLOPS_INITCHILD above
1893 		 */
1894 		sd = ddi_get_driver_private(child);
1895 		if ((sd == NULL) || hba->tran_reset(
1896 		    &sd->sd_address, RESET_TARGET) == 0) {
1897 			rv = EIO;
1898 		}
1899 
1900 		ndi_devi_enter(self, &circ);
1901 		ndi_rele_devi(child);
1902 		ndi_devi_exit(self, circ);
1903 
1904 		ndi_dc_freehdl(dcp);
1905 
1906 		break;
1907 
1908 
1909 	case DEVCTL_BUS_QUIESCE:
1910 		if ((ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS) &&
1911 		    (bus_state == BUS_QUIESCED)) {
1912 			rv = EALREADY;
1913 			break;
1914 		}
1915 
1916 		if (hba->tran_quiesce == NULL) {
1917 			rv = ENOTSUP;
1918 		} else if ((*hba->tran_quiesce)(self) != 0) {
1919 			rv = EIO;
1920 		} else {
1921 			(void) ndi_set_bus_state(self, BUS_QUIESCED);
1922 		}
1923 		break;
1924 
1925 	case DEVCTL_BUS_UNQUIESCE:
1926 		if ((ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS) &&
1927 		    (bus_state == BUS_ACTIVE)) {
1928 			rv = EALREADY;
1929 			break;
1930 		}
1931 
1932 		if (hba->tran_unquiesce == NULL) {
1933 			rv = ENOTSUP;
1934 		} else if ((*hba->tran_unquiesce)(self) != 0) {
1935 			rv = EIO;
1936 		} else {
1937 			(void) ndi_set_bus_state(self, BUS_ACTIVE);
1938 		}
1939 		break;
1940 
1941 	case DEVCTL_BUS_RESET:
1942 		/*
1943 		 * Use tran_bus_reset
1944 		 */
1945 		if (hba->tran_bus_reset == NULL) {
1946 			rv = ENOTSUP;
1947 		} else if ((*hba->tran_bus_reset)(self, RESET_BUS) == 0) {
1948 			rv = EIO;
1949 		}
1950 		break;
1951 
1952 	case DEVCTL_BUS_RESETALL:
1953 		if (hba->tran_reset == NULL) {
1954 			rv = ENOTSUP;
1955 			break;
1956 		}
1957 		/*
1958 		 * Find a child's scsi_address and invoke tran_reset
1959 		 *
1960 		 * XXX If no child exists, one may to able to fake a child.
1961 		 *	This will be a enhancement for the future.
1962 		 *	For now, we fall back to BUS_RESET.
1963 		 */
1964 		ndi_devi_enter(self, &circ);
1965 		child = ddi_get_child(self);
1966 		sd = NULL;
1967 		while (child) {
1968 			if ((sd = ddi_get_driver_private(child)) != NULL)
1969 				break;
1970 
1971 			child = ddi_get_next_sibling(child);
1972 		}
1973 
1974 		if (sd != NULL) {
1975 			ndi_hold_devi(child);
1976 			ndi_devi_exit(self, circ);
1977 			if ((*hba->tran_reset)
1978 			    (&sd->sd_address, RESET_ALL) == 0) {
1979 				rv = EIO;
1980 			}
1981 			ndi_devi_enter(self, &circ);
1982 			ndi_rele_devi(child);
1983 			ndi_devi_exit(self, circ);
1984 		} else {
1985 			ndi_devi_exit(self, circ);
1986 			if ((hba->tran_bus_reset == NULL) ||
1987 			    ((*hba->tran_bus_reset)(self, RESET_BUS) == 0)) {
1988 				rv = EIO;
1989 			}
1990 		}
1991 		break;
1992 
1993 	case DEVCTL_BUS_CONFIGURE:
1994 		if (ndi_devi_config(self, NDI_DEVFS_CLEAN|
1995 		    NDI_DEVI_PERSIST|NDI_CONFIG_REPROBE) != NDI_SUCCESS) {
1996 			rv = EIO;
1997 		}
1998 		break;
1999 
2000 	case DEVCTL_BUS_UNCONFIGURE:
2001 		if (ndi_devi_unconfig(self,
2002 		    NDI_DEVI_REMOVE|NDI_DEVFS_CLEAN) != NDI_SUCCESS) {
2003 			rv = EBUSY;
2004 		}
2005 		break;
2006 
2007 	default:
2008 		rv = ENOTTY;
2009 	} /* end of outer switch */
2010 
2011 	return (rv);
2012 }
2013 
2014 static int
2015 scsi_hba_bus_config(dev_info_t *parent, uint_t flag, ddi_bus_config_op_t op,
2016     void *arg, dev_info_t **childp)
2017 {
2018 	scsi_hba_tran_t *hba;
2019 
2020 	hba = ddi_get_driver_private(parent);
2021 	if (hba && hba->tran_bus_config) {
2022 		return (hba->tran_bus_config(parent, flag, op, arg, childp));
2023 	}
2024 
2025 	/*
2026 	 * Force reprobe for BUS_CONFIG_ONE or when manually reconfiguring
2027 	 * via devfsadm(1m) to emulate deferred attach.
2028 	 * Reprobe only discovers driver.conf enumerated nodes, more
2029 	 * dynamic implementations probably require their own bus_config.
2030 	 */
2031 	if ((op == BUS_CONFIG_ONE) || (flag & NDI_DRV_CONF_REPROBE))
2032 		flag |= NDI_CONFIG_REPROBE;
2033 
2034 	return (ndi_busop_bus_config(parent, flag, op, arg, childp, 0));
2035 }
2036 
2037 static int
2038 scsi_hba_bus_unconfig(dev_info_t *parent, uint_t flag, ddi_bus_config_op_t op,
2039     void *arg)
2040 {
2041 	scsi_hba_tran_t *hba;
2042 
2043 	hba = ddi_get_driver_private(parent);
2044 	if (hba && hba->tran_bus_unconfig) {
2045 		return (hba->tran_bus_unconfig(parent, flag, op, arg));
2046 	}
2047 	return (ndi_busop_bus_unconfig(parent, flag, op, arg));
2048 }
2049 
2050 /*
2051  * Convert scsi ascii string data to NULL terminated (semi) legal IEEE 1275
2052  * "compatible" (name) property form.
2053  *
2054  * For ASCII INQUIRY data, a one-way conversion algorithm is needed to take
2055  * SCSI_ASCII (20h - 7Eh) to a 1275-like compatible form. The 1275 spec allows
2056  * letters, digits, one ",", and ". _ + -", all limited by a maximum 31
2057  * character length. Since ", ." are used as separators in the compatible
2058  * string itself, they are converted to "_". All SCSI_ASCII characters that
2059  * are illegal in 1275, as well as any illegal SCSI_ASCII characters
2060  * encountered, are converted to "_". To reduce length, trailing blanks are
2061  * trimmed from SCSI_ASCII fields prior to conversion.
2062  *
2063  * Example: SCSI_ASCII "ST32550W SUN2.1G" -> "ST32550W_SUN2_1G"
2064  *
2065  * NOTE: the 1275 string form is always less than or equal to the scsi form.
2066  */
2067 static char *
2068 string_scsi_to_1275(char *s_1275, char *s_scsi, int len)
2069 {
2070 	(void) strncpy(s_1275, s_scsi, len);
2071 	s_1275[len--] = '\0';
2072 
2073 	while (len >= 0) {
2074 		if (s_1275[len] == ' ')
2075 			s_1275[len--] = '\0';	/* trim trailing " " */
2076 		else
2077 			break;
2078 	}
2079 
2080 	while (len >= 0) {
2081 		if (((s_1275[len] >= 'a') && (s_1275[len] <= 'z')) ||
2082 		    ((s_1275[len] >= 'A') && (s_1275[len] <= 'Z')) ||
2083 		    ((s_1275[len] >= '0') && (s_1275[len] <= '9')) ||
2084 		    (s_1275[len] == '_') ||
2085 		    (s_1275[len] == '+') ||
2086 		    (s_1275[len] == '-'))
2087 			len--;			/* legal 1275  */
2088 		else
2089 			s_1275[len--] = '_';	/* illegal SCSI_ASCII | 1275 */
2090 	}
2091 
2092 	return (s_1275);
2093 }
2094 
2095 /*
2096  * Given the inquiry data, binding_set, and dtype_node for a scsi device,
2097  * return the nodename and compatible property for the device. The "compatible"
2098  * concept comes from IEEE-1275.  The compatible information is returned is in
2099  * the correct form for direct use defining the "compatible" string array
2100  * property.  Internally, "compatible" is also used to determine the nodename
2101  * to return.
2102  *
2103  * This function is provided as a separate entry point for use by drivers that
2104  * currently issue their own non-SCSA inquiry command and perform their own
2105  * node creation based their own private compiled in tables.  Converting these
2106  * drivers to use this interface provides a quick easy way of obtaining
2107  * consistency as well as the flexibility associated with the 1275 techniques.
2108  *
2109  * The dtype_node is passed as a separate argument (instead of having the
2110  * implementation use inq_dtype).  It indicates that information about
2111  * a secondary function embedded service should be produced.
2112  *
2113  * Callers must always use scsi_hba_nodename_compatible_free, even if
2114  * *nodenamep is null, to free the nodename and compatible information
2115  * when done.
2116  *
2117  * If a nodename can't be determined then **compatiblep will point to a
2118  * diagnostic string containing all the compatible forms.
2119  *
2120  * NOTE: some compatible strings may violate the 31 character restriction
2121  * imposed by IEEE-1275.  This is not a problem because Solaris does not care
2122  * about this 31 character limit.
2123  *
2124  *  The following compatible forms, in high to low precedence
2125  *  order, are defined for SCSI target device nodes.
2126  *
2127  *  scsiclass,DDEEFFF.vVVVVVVVV.pPPPPPPPPPPPPPPPP.rRRRR	(1 *1&2)
2128  *  scsiclass,DDEE.vVVVVVVVV.pPPPPPPPPPPPPPPPP.rRRRR	(2 *1)
2129  *  scsiclass,DDFFF.vVVVVVVVV.pPPPPPPPPPPPPPPPP.rRRRR	(3 *2)
2130  *  scsiclass,DD.vVVVVVVVV.pPPPPPPPPPPPPPPPP.rRRRR	(4)
2131  *  scsiclass,DDEEFFF.vVVVVVVVV.pPPPPPPPPPPPPPPPP	(5 *1&2)
2132  *  scsiclass,DDEE.vVVVVVVVV.pPPPPPPPPPPPPPPPP		(6 *1)
2133  *  scsiclass,DDFFF.vVVVVVVVV.pPPPPPPPPPPPPPPPP		(7 *2)
2134  *  scsiclass,DD.vVVVVVVVV.pPPPPPPPPPPPPPPPP		(8)
2135  *  scsa,DD.bBBBBBBBB					(8.5 *3)
2136  *  scsiclass,DDEEFFF					(9 *1&2)
2137  *  scsiclass,DDEE					(10 *1)
2138  *  scsiclass,DDFFF					(11 *2)
2139  *  scsiclass,DD					(12)
2140  *  scsiclass						(13)
2141  *
2142  *	  *1 only produced on a secondary function node
2143  *	  *2 only produced on a node with flags
2144  *	  *3 only produces when binding-set legacy support is needed
2145  *
2146  *	where:
2147  *
2148  *	v                       is the letter 'v'. Denotest the
2149  *				beginning of VVVVVVVV.
2150  *
2151  *	VVVVVVVV                Translated scsi_vendor.
2152  *
2153  *	p                       is the letter 'p'. Denotes the
2154  *				beginning of PPPPPPPPPPPPPPPP.
2155  *
2156  *	PPPPPPPPPPPPPPPP	Translated scsi_product.
2157  *
2158  *	r                       is the letter 'r'. Denotes the
2159  *				beginning of RRRR.
2160  *
2161  *	RRRR                    Translated scsi_revision.
2162  *
2163  *	DD                      is a two digit ASCII hexadecimal
2164  *				number.  The value of the two digits is
2165  *				based one the SCSI "Peripheral device
2166  *				type" command set associated with the
2167  *				node.  On a primary node this is the
2168  *				scsi_dtype of the primary command set,
2169  *				on a secondary node this is the
2170  *				scsi_dtype associated with the embedded
2171  *				function command set.
2172  *
2173  *	EE                      Same encoding used for DD. This form is
2174  *				only generated on secondary function
2175  *				nodes. The DD function is embedded in
2176  *				an EE device.
2177  *
2178  *	FFF                     Concatenation, in alphabetical order,
2179  *				of the flag characters below. The
2180  *				following flag characters are defined:
2181  *
2182  *				R       Removable media: Used when
2183  *					scsi_rmb is set.
2184  *
2185  *				Forms using FFF are only be generated
2186  *				if there are applicable flag
2187  *				characters.
2188  *
2189  *	b                       is the letter 'b'. Denotes the
2190  *				beginning of BBBBBBBB.
2191  *
2192  *	BBBBBBBB                Binding-set. Operating System Specific:
2193  *				scsi-binding-set property of HBA.
2194  */
2195 #define	NCOMPAT		(1 + (8 + 1 + 5) + 1)
2196 #define	COMPAT_LONGEST	(strlen( \
2197 	"scsiclass,DDEEFFF.vVVVVVVVV.pPPPPPPPPPPPPPPPP.rRRRR" + 1))
2198 void
2199 scsi_hba_nodename_compatible_get(struct scsi_inquiry *inq, char *binding_set,
2200     int dtype_node, char *compat0,
2201     char **nodenamep, char ***compatiblep, int *ncompatiblep)
2202 {
2203 	char	vid[sizeof (inq->inq_vid) + 1 ];
2204 	char	pid[sizeof (inq->inq_pid) + 1];
2205 	char	rev[sizeof (inq->inq_revision) + 1];
2206 	char	f[sizeof ("ER")];
2207 	int	dtype_device;
2208 	int	ncompat;		/* number of compatible */
2209 	char	**compatp;		/* compatible ptrs */
2210 	int	i;
2211 	char	*nname;			/* nodename */
2212 	char	*dname;			/* driver name */
2213 	char	**csp;
2214 	char	*p;
2215 	int	tlen;
2216 	int	len;
2217 	major_t	major;
2218 
2219 	/*
2220 	 * Nodename_aliases: This table was originally designed to be
2221 	 * implemented via a new nodename_aliases file - a peer to the
2222 	 * driver_aliases that selects a nodename based on compatible
2223 	 * forms in much the same say driver_aliases is used to select
2224 	 * driver bindings from compatible forms.  Each compatible form
2225 	 * is an 'alias'.  Until a more general need for a
2226 	 * nodename_aliases file exists, which may never occur, the
2227 	 * scsi mappings are described here via a compiled in table.
2228 	 *
2229 	 * This table contains nodename mappings for self-identifying
2230 	 * scsi devices enumerated by the Solaris kernel.  For a given
2231 	 * device, the highest precedence "compatible" form with a
2232 	 * mapping is used to select the nodename for the device. This
2233 	 * will typically be a generic nodename, however in some legacy
2234 	 * compatibility cases a driver nodename mapping may be selected.
2235 	 *
2236 	 * Because of possible breakage associated with switching SCSI
2237 	 * target devices from driver nodenames to generic nodenames,
2238 	 * we are currently unable to support generic nodenames for all
2239 	 * SCSI devices (binding-sets).  Although /devices paths are
2240 	 * defined as unstable, avoiding possible breakage is
2241 	 * important.  Some of the newer SCSI transports (USB) already
2242 	 * use generic nodenames.  All new SCSI transports and target
2243 	 * devices should use generic nodenames. At times this decision
2244 	 * may be architecture dependent (sparc .vs. intel) based on when
2245 	 * a transport was supported on a particular architecture.
2246 	 *
2247 	 * We provide a base set of generic nodename mappings based on
2248 	 * scsiclass dtype and higher-precedence driver nodename
2249 	 * mappings based on scsa "binding-set" to cover legacy
2250 	 * issues.  The binding-set is typically associated with
2251 	 * "scsi-binding-set" property value of the HBA.  The legacy
2252 	 * mappings are provided independent of whether the driver they
2253 	 * refer to is installed.  This allows a correctly named node
2254 	 * be created at discovery time, and binding to occur when/if
2255 	 * an add_drv of the legacy driver occurs.
2256 	 *
2257 	 * We also have mappings for legacy SUN hardware that
2258 	 * misidentifies itself (enclosure services which identify
2259 	 * themselves as processors).  All future hardware should use
2260 	 * the correct dtype.
2261 	 *
2262 	 * As SCSI HBAs are modified to use the SCSA interfaces for
2263 	 * self-identifying SCSI target devices (PSARC/2004/116)  the
2264 	 * nodename_aliases table (PSARC/2004/420) should be augmented
2265 	 * with legacy mappings in order to maintain compatibility with
2266 	 * existing /devices paths, especially for devices that house
2267 	 * an OS.  Failure to do this may cause upgrade problems.
2268 	 * Additions for new target devices or transports should not
2269 	 * add scsa binding-set compatible mappings.
2270 	 */
2271 	static struct nodename_aliases {
2272 		char	*na_nodename;		/* nodename */
2273 		char	*na_alias;		/* compatible form match */
2274 	} na[] = {
2275 	/* # mapping to generic nodenames based on scsi dtype */
2276 		{"disk",		"scsiclass,00"},
2277 		{"tape",		"scsiclass,01"},
2278 		{"printer",		"scsiclass,02"},
2279 		{"processor",		"scsiclass,03"},
2280 		{"worm",		"scsiclass,04"},
2281 		{"cdrom",		"scsiclass,05"},
2282 		{"scanner",		"scsiclass,06"},
2283 		{"optical-disk",	"scsiclass,07"},
2284 		{"medium-changer",	"scsiclass,08"},
2285 		{"obsolete",		"scsiclass,09"},
2286 		{"prepress-a",		"scsiclass,0a"},
2287 		{"prepress-b",		"scsiclass,0b"},
2288 		{"array-controller",	"scsiclass,0c"},
2289 		{"enclosure",		"scsiclass,0d"},
2290 		{"disk",		"scsiclass,0e"},
2291 		{"card-reader",		"scsiclass,0f"},
2292 		{"bridge",		"scsiclass,10"},
2293 		{"object-store",	"scsiclass,11"},
2294 		{"reserved",		"scsiclass,12"},
2295 		{"reserved",		"scsiclass,13"},
2296 		{"reserved",		"scsiclass,14"},
2297 		{"reserved",		"scsiclass,15"},
2298 		{"reserved",		"scsiclass,16"},
2299 		{"reserved",		"scsiclass,17"},
2300 		{"reserved",		"scsiclass,18"},
2301 		{"reserved",		"scsiclass,19"},
2302 		{"reserved",		"scsiclass,1a"},
2303 		{"reserved",		"scsiclass,1b"},
2304 		{"reserved",		"scsiclass,1c"},
2305 		{"reserved",		"scsiclass,1d"},
2306 		{"well-known-lun",	"scsiclass,1e"},
2307 		{"unknown",		"scsiclass,1f"},
2308 
2309 #ifdef	sparc
2310 	/* # legacy mapping to driver nodenames for fcp binding-set */
2311 		{"ssd",			"scsa,00.bfcp"},
2312 		{"st",			"scsa,01.bfcp"},
2313 		{"sgen",		"scsa,08.bfcp"},
2314 		{"ses",			"scsa,0d.bfcp"},
2315 
2316 	/* # legacy mapping to driver nodenames for vhci binding-set */
2317 		{"ssd",			"scsa,00.bvhci"},
2318 		{"st",			"scsa,01.bvhci"},
2319 		{"sgen",		"scsa,08.bvhci"},
2320 		{"ses",			"scsa,0d.bvhci"},
2321 #else	/* sparc */
2322 	/* # for x86 fcp and vhci use generic nodenames */
2323 #endif	/* sparc */
2324 
2325 #ifdef	notdef
2326 	/*
2327 	 * The following binding-set specific mappings are not being
2328 	 * delivered at this time, but are listed here as an examples of
2329 	 * the type of mappings needed.
2330 	 */
2331 
2332 	/* # legacy mapping to driver nodenames for spi binding-set */
2333 		{"sd",			"scsa,00.bspi"},
2334 		{"sd",			"scsa,05.bspi"},
2335 		{"sd",			"scsa,07.bspi"},
2336 		{"st",			"scsa,01.bspi"},
2337 		{"ses",			"scsa,0d.bspi"},
2338 
2339 	/* #				SUN misidentified spi hardware */
2340 		{"ses",			"scsiclass,03.vSUN.pD2"},
2341 		{"ses",			"scsiclass,03.vSYMBIOS.pD1000"},
2342 
2343 	/* # legacy mapping to driver nodenames for atapi binding-set */
2344 		{"sd",			"scsa,00.batapi"},
2345 		{"sd",			"scsa,05.batapi"},
2346 		{"sd",			"scsa,07.batapi"},
2347 		{"st",			"scsa,01.batapi"},
2348 		{"unknown",		"scsa,0d.batapi"},
2349 
2350 	/* # legacy mapping to generic nodenames for usb binding-set */
2351 		{"disk",		"scsa,05.busb"},
2352 		{"disk",		"scsa,07.busb"},
2353 		{"changer",		"scsa,08.busb"},
2354 		{"comm",		"scsa,09.busb"},
2355 		{"array_ctlr",		"scsa,0c.busb"},
2356 		{"esi",			"scsa,0d.busb"},
2357 #endif	/* notdef */
2358 
2359 	/*
2360 	 * mapping nodenames for mpt based on scsi dtype
2361 	 * for being compatible with the original node names
2362 	 * under mpt controller
2363 	 */
2364 		{"sd",			"scsa,00.bmpt"},
2365 		{"sd",			"scsa,05.bmpt"},
2366 		{"sd",			"scsa,07.bmpt"},
2367 		{"st",			"scsa,01.bmpt"},
2368 		{"ses",			"scsa,0d.bmpt"},
2369 		{"sgen",		"scsa,08.bmpt"},
2370 		{NULL,		NULL}
2371 	};
2372 	struct nodename_aliases *nap;
2373 
2374 	ASSERT(nodenamep && compatiblep && ncompatiblep &&
2375 	    (binding_set == NULL || (strlen(binding_set) <= 8)));
2376 	if ((nodenamep == NULL) || (compatiblep == NULL) ||
2377 	    (ncompatiblep == NULL))
2378 		return;
2379 
2380 	/*
2381 	 * In order to reduce runtime we allocate one block of memory that
2382 	 * contains both the NULL terminated array of pointers to compatible
2383 	 * forms and the individual compatible strings.  This block is
2384 	 * somewhat larger than needed, but is short lived - it only exists
2385 	 * until the caller can transfer the information into the "compatible"
2386 	 * string array property and call scsi_hba_nodename_compatible_free.
2387 	 */
2388 	tlen = NCOMPAT * COMPAT_LONGEST;
2389 	compatp = kmem_alloc((NCOMPAT * sizeof (char *)) + tlen, KM_SLEEP);
2390 
2391 	/* convert inquiry data from SCSI ASCII to 1275 string */
2392 	(void) string_scsi_to_1275(vid, inq->inq_vid,
2393 	    sizeof (inq->inq_vid));
2394 	(void) string_scsi_to_1275(pid, inq->inq_pid,
2395 	    sizeof (inq->inq_pid));
2396 	(void) string_scsi_to_1275(rev, inq->inq_revision,
2397 	    sizeof (inq->inq_revision));
2398 	ASSERT((strlen(vid) <= sizeof (inq->inq_vid)) &&
2399 	    (strlen(pid) <= sizeof (inq->inq_pid)) &&
2400 	    (strlen(rev) <= sizeof (inq->inq_revision)));
2401 
2402 	/*
2403 	 * Form flags alphabetically:
2404 	 * R - removable:
2405 	 *	Set when inq_rmb is set and for well known scsi dtypes.  For a
2406 	 *	bus where the entire device is removable (like USB), we expect
2407 	 *	the HBA to intercept the inquiry data and set inq_rmb.
2408 	 *	Since OBP does not distinguish removable media in its generic
2409 	 *	name selection we avoid setting the 'R' flag if the root is not
2410 	 *	yet mounted.
2411 	 */
2412 	dtype_device = inq->inq_dtype & DTYPE_MASK;
2413 	i = 0;
2414 	if (rootvp && (inq->inq_rmb ||
2415 	    (dtype_device == DTYPE_WORM) ||
2416 	    (dtype_device == DTYPE_RODIRECT) ||
2417 	    (dtype_device == DTYPE_OPTICAL)))
2418 		f[i++] = 'R';
2419 	f[i] = '\0';
2420 
2421 	/*
2422 	 * Construct all applicable compatible forms. See comment at the
2423 	 * head of the function for a description of the compatible forms.
2424 	 */
2425 	csp = compatp;
2426 	p = (char *)(compatp + NCOMPAT);
2427 
2428 
2429 	/* ( 0) driver (optional, not documented in scsi(4)) */
2430 	if (compat0) {
2431 		*csp++ = p;
2432 		(void) snprintf(p, tlen, "%s", compat0);
2433 		len = strlen(p) + 1;
2434 		p += len;
2435 		tlen -= len;
2436 	}
2437 
2438 	/* ( 1) scsiclass,DDEEF.vV.pP.rR */
2439 	if ((dtype_device != dtype_node) && *f && *vid && *pid && *rev) {
2440 		*csp++ = p;
2441 		(void) snprintf(p, tlen, "scsiclass,%02x%02x%s.v%s.p%s.r%s",
2442 		    dtype_node, dtype_device, f, vid, pid, rev);
2443 		len = strlen(p) + 1;
2444 		p += len;
2445 		tlen -= len;
2446 	}
2447 
2448 	/* ( 2) scsiclass,DDEE.vV.pP.rR */
2449 	if ((dtype_device != dtype_node) && *vid && *pid && *rev) {
2450 		*csp++ = p;
2451 		(void) snprintf(p, tlen, "scsiclass,%02x%02x.v%s.p%s.r%s",
2452 		    dtype_node, dtype_device, vid, pid, rev);
2453 		len = strlen(p) + 1;
2454 		p += len;
2455 		tlen -= len;
2456 	}
2457 
2458 	/* ( 3) scsiclass,DDF.vV.pP.rR */
2459 	if (*f && *vid && *pid && *rev) {
2460 		*csp++ = p;
2461 		(void) snprintf(p, tlen, "scsiclass,%02x%s.v%s.p%s.r%s",
2462 		    dtype_node, f, vid, pid, rev);
2463 		len = strlen(p) + 1;
2464 		p += len;
2465 		tlen -= len;
2466 	}
2467 
2468 	/* ( 4) scsiclass,DD.vV.pP.rR */
2469 	if (*vid && *pid && rev) {
2470 		*csp++ = p;
2471 		(void) snprintf(p, tlen, "scsiclass,%02x.v%s.p%s.r%s",
2472 		    dtype_node, vid, pid, rev);
2473 		len = strlen(p) + 1;
2474 		p += len;
2475 		tlen -= len;
2476 	}
2477 
2478 	/* ( 5) scsiclass,DDEEF.vV.pP */
2479 	if ((dtype_device != dtype_node) && *f && *vid && *pid) {
2480 		*csp++ = p;
2481 		(void) snprintf(p, tlen, "scsiclass,%02x%02x%s.v%s.p%s",
2482 		    dtype_node, dtype_device, f, vid, pid);
2483 		len = strlen(p) + 1;
2484 		p += len;
2485 		tlen -= len;
2486 	}
2487 
2488 	/* ( 6) scsiclass,DDEE.vV.pP */
2489 	if ((dtype_device != dtype_node) && *vid && *pid) {
2490 		*csp++ = p;
2491 		(void) snprintf(p, tlen, "scsiclass,%02x%02x.v%s.p%s",
2492 		    dtype_node, dtype_device, vid, pid);
2493 		len = strlen(p) + 1;
2494 		p += len;
2495 		tlen -= len;
2496 	}
2497 
2498 	/* ( 7) scsiclass,DDF.vV.pP */
2499 	if (*f && *vid && *pid) {
2500 		*csp++ = p;
2501 		(void) snprintf(p, tlen, "scsiclass,%02x%s.v%s.p%s",
2502 		    dtype_node, f, vid, pid);
2503 		len = strlen(p) + 1;
2504 		p += len;
2505 		tlen -= len;
2506 	}
2507 
2508 	/* ( 8) scsiclass,DD.vV.pP */
2509 	if (*vid && *pid) {
2510 		*csp++ = p;
2511 		(void) snprintf(p, tlen, "scsiclass,%02x.v%s.p%s",
2512 		    dtype_node, vid, pid);
2513 		len = strlen(p) + 1;
2514 		p += len;
2515 		tlen -= len;
2516 	}
2517 
2518 	/* (8.5) scsa,DD.bB (not documented in scsi(4)) */
2519 	if (binding_set) {
2520 		*csp++ = p;
2521 		(void) snprintf(p, tlen, "scsa,%02x.b%s",
2522 		    dtype_node, binding_set);
2523 		len = strlen(p) + 1;
2524 		p += len;
2525 		tlen -= len;
2526 	}
2527 
2528 	/* ( 9) scsiclass,DDEEF */
2529 	if ((dtype_device != dtype_node) && *f) {
2530 		*csp++ = p;
2531 		(void) snprintf(p, tlen, "scsiclass,%02x%02x%s",
2532 		    dtype_node, dtype_device, f);
2533 		len = strlen(p) + 1;
2534 		p += len;
2535 		tlen -= len;
2536 	}
2537 
2538 	/* (10) scsiclass,DDEEF */
2539 	if (dtype_device != dtype_node) {
2540 		*csp++ = p;
2541 		(void) snprintf(p, tlen, "scsiclass,%02x%02x",
2542 		    dtype_node, dtype_device);
2543 		len = strlen(p) + 1;
2544 		p += len;
2545 		tlen -= len;
2546 	}
2547 
2548 	/* (11) scsiclass,DDF */
2549 	if (*f) {
2550 		*csp++ = p;
2551 		(void) snprintf(p, tlen, "scsiclass,%02x%s",
2552 		    dtype_node, f);
2553 		len = strlen(p) + 1;
2554 		p += len;
2555 		tlen -= len;
2556 	}
2557 
2558 	/* (12) scsiclass,DD */
2559 	*csp++ = p;
2560 	(void) snprintf(p, tlen, "scsiclass,%02x", dtype_node);
2561 	len = strlen(p) + 1;
2562 	p += len;
2563 	tlen -= len;
2564 
2565 	/* (13) scsiclass */
2566 	*csp++ = p;
2567 	(void) snprintf(p, tlen, "scsiclass");
2568 	len = strlen(p) + 1;
2569 	p += len;
2570 	tlen -= len;
2571 	ASSERT(tlen >= 0);
2572 
2573 	*csp = NULL;			/* NULL terminate array of pointers */
2574 	ncompat = csp - compatp;
2575 
2576 	/*
2577 	 * When determining a nodename, a nodename_aliases specified
2578 	 * mapping has precedence over using a driver_aliases specified
2579 	 * driver binding as a nodename.
2580 	 *
2581 	 * See if any of the compatible forms have a nodename_aliases
2582 	 * specified nodename.  These mappings are described by
2583 	 * nodename_aliases entries like:
2584 	 *
2585 	 *	disk		"scsiclass,00"
2586 	 *	enclosure	"scsiclass,03.vSYMBIOS.pD1000"
2587 	 *	ssd		"scsa,00.bfcp"
2588 	 *
2589 	 * All nodename_aliases mappings should idealy be to generic
2590 	 * names, however a higher precedence legacy mapping to a
2591 	 * driver name may exist.  The highest precedence mapping
2592 	 * provides the nodename, so legacy driver nodename mappings
2593 	 * (if they exist) take precedence over generic nodename
2594 	 * mappings.
2595 	 */
2596 	for (nname = NULL, csp = compatp; (nname == NULL) && *csp; csp++) {
2597 		for (nap = na; nap->na_nodename; nap++) {
2598 			if (strcmp(*csp, nap->na_alias) == 0) {
2599 				nname = nap->na_nodename;
2600 				break;
2601 			}
2602 		}
2603 	}
2604 
2605 	/*
2606 	 * If no nodename_aliases mapping exists then use the
2607 	 * driver_aliases specified driver binding as a nodename.
2608 	 * Determine the driver based on compatible (which may
2609 	 * have the passed in compat0 as the first item). The
2610 	 * driver_aliases file has entries like
2611 	 *
2612 	 *	sd	"scsiclass,00"
2613 	 *
2614 	 * that map compatible forms to specific drivers.  These
2615 	 * entries are established by add_drv. We use the most specific
2616 	 * driver binding as the nodename. This matches the eventual
2617 	 * ddi_driver_compatible_major() binding that will be
2618 	 * established by bind_node()
2619 	 */
2620 	if (nname == NULL) {
2621 		for (dname = NULL, csp = compatp; *csp; csp++) {
2622 			major = ddi_name_to_major(*csp);
2623 			if ((major == (major_t)-1) ||
2624 			    (devnamesp[major].dn_flags & DN_DRIVER_REMOVED))
2625 				continue;
2626 			if (dname = ddi_major_to_name(major))
2627 				break;
2628 		}
2629 		nname = dname;
2630 	}
2631 
2632 	/* return results */
2633 	if (nname) {
2634 		*nodenamep = kmem_alloc(strlen(nname) + 1, KM_SLEEP);
2635 		(void) strcpy(*nodenamep, nname);
2636 	} else {
2637 		*nodenamep = NULL;
2638 
2639 		/*
2640 		 * If no nodename could be determined return a special
2641 		 * 'compatible' to be used for a diagnostic message. This
2642 		 * compatible contains all compatible forms concatenated
2643 		 * into a single string pointed to by the first element.
2644 		 */
2645 		if (nname == NULL) {
2646 			for (csp = compatp; *(csp + 1); csp++)
2647 				*((*csp) + strlen(*csp)) = ' ';
2648 			*(compatp + 1) = NULL;
2649 			ncompat = 1;
2650 		}
2651 
2652 	}
2653 	*compatiblep = compatp;
2654 	*ncompatiblep = ncompat;
2655 }
2656 
2657 /* Free allocations associated with scsi_hba_nodename_compatible_get use. */
2658 void
2659 scsi_hba_nodename_compatible_free(char *nodename, char **compatible)
2660 {
2661 	if (nodename)
2662 		kmem_free(nodename, strlen(nodename) + 1);
2663 
2664 	if (compatible)
2665 		kmem_free(compatible, (NCOMPAT * sizeof (char *)) +
2666 		    (NCOMPAT * COMPAT_LONGEST));
2667 }
2668 
2669 /*ARGSUSED*/
2670 static int
2671 scsi_hba_fm_init_child(dev_info_t *self, dev_info_t *child, int cap,
2672     ddi_iblock_cookie_t *ibc)
2673 {
2674 	scsi_hba_tran_t	*hba = ddi_get_driver_private(self);
2675 
2676 	return (hba ? hba->tran_fm_capable : scsi_fm_capable);
2677 }
2678 
2679 static int
2680 scsi_hba_bus_power(dev_info_t *parent, void *impl_arg, pm_bus_power_op_t op,
2681     void *arg, void *result)
2682 {
2683 	scsi_hba_tran_t *hba;
2684 
2685 	hba = ddi_get_driver_private(parent);
2686 	if (hba && hba->tran_bus_power) {
2687 		return (hba->tran_bus_power(parent, impl_arg, op, arg, result));
2688 	}
2689 
2690 	return (pm_busop_bus_power(parent, impl_arg, op, arg, result));
2691 }
2692