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