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