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