xref: /illumos-gate/usr/src/uts/common/io/scsi/impl/scsi_hba.c (revision b8bf75cb74c7cf11bde9dc90fb021429968d7654)
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 /*
46  * Round up all allocations so that we can guarantee
47  * long-long alignment.  This is the same alignment
48  * provided by kmem_alloc().
49  */
50 #define	ROUNDUP(x)	(((x) + 0x07) & ~0x07)
51 
52 static kmutex_t	scsi_hba_mutex;
53 
54 kmutex_t scsi_log_mutex;
55 
56 
57 struct scsi_hba_inst {
58 	dev_info_t		*inst_dip;
59 	scsi_hba_tran_t		*inst_hba_tran;
60 	struct scsi_hba_inst	*inst_next;
61 	struct scsi_hba_inst	*inst_prev;
62 };
63 
64 static struct scsi_hba_inst	*scsi_hba_list		= NULL;
65 static struct scsi_hba_inst	*scsi_hba_list_tail	= NULL;
66 
67 
68 kmutex_t	scsi_flag_nointr_mutex;
69 kcondvar_t	scsi_flag_nointr_cv;
70 
71 /*
72  * Prototypes for static functions
73  */
74 static int	scsi_hba_bus_ctl(
75 			dev_info_t		*dip,
76 			dev_info_t		*rdip,
77 			ddi_ctl_enum_t		op,
78 			void			*arg,
79 			void			*result);
80 
81 static int	scsi_hba_map_fault(
82 			dev_info_t		*dip,
83 			dev_info_t		*rdip,
84 			struct hat		*hat,
85 			struct seg		*seg,
86 			caddr_t			addr,
87 			struct devpage		*dp,
88 			pfn_t			pfn,
89 			uint_t			prot,
90 			uint_t			lock);
91 
92 static int	scsi_hba_get_eventcookie(
93 			dev_info_t		*dip,
94 			dev_info_t		*rdip,
95 			char			*name,
96 			ddi_eventcookie_t	*eventp);
97 
98 static int	scsi_hba_add_eventcall(
99 			dev_info_t		*dip,
100 			dev_info_t		*rdip,
101 			ddi_eventcookie_t	event,
102 			void			(*callback)(
103 					dev_info_t *dip,
104 					ddi_eventcookie_t event,
105 					void *arg,
106 					void *bus_impldata),
107 			void			*arg,
108 			ddi_callback_id_t	*cb_id);
109 
110 static int	scsi_hba_remove_eventcall(
111 			dev_info_t *devi,
112 			ddi_callback_id_t id);
113 
114 static int	scsi_hba_post_event(
115 			dev_info_t		*dip,
116 			dev_info_t		*rdip,
117 			ddi_eventcookie_t	event,
118 			void			*bus_impldata);
119 
120 static int	scsi_hba_info(
121 			dev_info_t		*dip,
122 			ddi_info_cmd_t		infocmd,
123 			void			*arg,
124 			void			**result);
125 
126 static int scsi_hba_bus_config(dev_info_t *parent, uint_t flag,
127     ddi_bus_config_op_t op, void *arg, dev_info_t **childp);
128 static int scsi_hba_bus_unconfig(dev_info_t *parent, uint_t flag,
129     ddi_bus_config_op_t op, void *arg);
130 
131 static int scsi_hba_bus_power(dev_info_t *parent, void *impl_arg,
132     pm_bus_power_op_t op, void *arg, void *result);
133 
134 /*
135  * Busops vector for SCSI HBA's.
136  */
137 static struct bus_ops scsi_hba_busops = {
138 	BUSO_REV,
139 	nullbusmap,			/* bus_map */
140 	NULL,				/* bus_get_intrspec */
141 	NULL,				/* bus_add_intrspec */
142 	NULL,				/* bus_remove_intrspec */
143 	scsi_hba_map_fault,		/* bus_map_fault */
144 	ddi_dma_map,			/* bus_dma_map */
145 	ddi_dma_allochdl,		/* bus_dma_allochdl */
146 	ddi_dma_freehdl,		/* bus_dma_freehdl */
147 	ddi_dma_bindhdl,		/* bus_dma_bindhdl */
148 	ddi_dma_unbindhdl,		/* bus_unbindhdl */
149 	ddi_dma_flush,			/* bus_dma_flush */
150 	ddi_dma_win,			/* bus_dma_win */
151 	ddi_dma_mctl,			/* bus_dma_ctl */
152 	scsi_hba_bus_ctl,		/* bus_ctl */
153 	ddi_bus_prop_op,		/* bus_prop_op */
154 	scsi_hba_get_eventcookie,	/* bus_get_eventcookie */
155 	scsi_hba_add_eventcall,		/* bus_add_eventcall */
156 	scsi_hba_remove_eventcall,	/* bus_remove_eventcall */
157 	scsi_hba_post_event,		/* bus_post_event */
158 	NULL,				/* bus_intr_ctl */
159 	scsi_hba_bus_config,		/* bus_config */
160 	scsi_hba_bus_unconfig,		/* bus_unconfig */
161 	NULL,				/* bus_fm_init */
162 	NULL,				/* bus_fm_fini */
163 	NULL,				/* bus_fm_access_enter */
164 	NULL,				/* bus_fm_access_exit */
165 	scsi_hba_bus_power		/* bus_power */
166 };
167 
168 
169 static struct cb_ops scsi_hba_cbops = {
170 	scsi_hba_open,
171 	scsi_hba_close,
172 	nodev,			/* strategy */
173 	nodev,			/* print */
174 	nodev,			/* dump */
175 	nodev,			/* read */
176 	nodev,			/* write */
177 	scsi_hba_ioctl,		/* ioctl */
178 	nodev,			/* devmap */
179 	nodev,			/* mmap */
180 	nodev,			/* segmap */
181 	nochpoll,		/* poll */
182 	ddi_prop_op,		/* prop_op */
183 	NULL,			/* stream */
184 	D_NEW|D_MP|D_HOTPLUG,	/* cb_flag */
185 	CB_REV,			/* rev */
186 	nodev,			/* int (*cb_aread)() */
187 	nodev			/* int (*cb_awrite)() */
188 };
189 
190 
191 /*
192  * Called from _init() when loading scsi module
193  */
194 void
195 scsi_initialize_hba_interface()
196 {
197 	mutex_init(&scsi_hba_mutex, NULL, MUTEX_DRIVER, NULL);
198 	mutex_init(&scsi_flag_nointr_mutex, NULL, MUTEX_DRIVER, NULL);
199 	cv_init(&scsi_flag_nointr_cv, NULL, CV_DRIVER, NULL);
200 	mutex_init(&scsi_log_mutex, NULL, MUTEX_DRIVER, NULL);
201 }
202 
203 #ifdef	NO_SCSI_FINI_YET
204 /*
205  * Called from _fini() when unloading scsi module
206  */
207 void
208 scsi_uninitialize_hba_interface()
209 {
210 	mutex_destroy(&scsi_hba_mutex);
211 	cv_destroy(&scsi_flag_nointr_cv);
212 	mutex_destroy(&scsi_flag_nointr_mutex);
213 	mutex_destroy(&scsi_log_mutex);
214 }
215 #endif	/* NO_SCSI_FINI_YET */
216 
217 int
218 scsi_hba_pkt_constructor(void *buf, void *arg, int kmflag)
219 {
220 	struct scsi_pkt		*pkt;
221 	scsi_hba_tran_t		*tran = (scsi_hba_tran_t *)arg;
222 	int			pkt_len;
223 	char			*ptr;
224 
225 	pkt = &((struct scsi_pkt_cache_wrapper *)buf)->pcw_pkt;
226 
227 	/*
228 	 * allocate a chunk of memory for the following:
229 	 * scsi_pkt
230 	 * pkt_ha_private
231 	 * pkt_cdbp, if needed
232 	 * (pkt_private always null)
233 	 * pkt_scbp, if needed
234 	 */
235 	pkt_len = tran->tran_hba_len + sizeof (struct scsi_pkt_cache_wrapper);
236 	if (tran->tran_hba_flags & SCSI_HBA_TRAN_CDB)
237 		pkt_len += DEFAULT_CDBLEN;
238 	if (tran->tran_hba_flags & SCSI_HBA_TRAN_SCB)
239 		pkt_len += DEFAULT_SCBLEN;
240 	bzero(buf, pkt_len);
241 	ptr = buf;
242 	ptr += sizeof (struct scsi_pkt_cache_wrapper);
243 	pkt->pkt_ha_private = (opaque_t)ptr;
244 	ptr += tran->tran_hba_len;
245 	if (tran->tran_hba_flags & SCSI_HBA_TRAN_CDB) {
246 		pkt->pkt_cdbp = (opaque_t)ptr;
247 		ptr += DEFAULT_CDBLEN;
248 	}
249 	pkt->pkt_private = NULL;
250 	if (tran->tran_hba_flags & SCSI_HBA_TRAN_SCB)
251 		pkt->pkt_scbp = (opaque_t)ptr;
252 	if (tran->tran_pkt_constructor)
253 		return ((*tran->tran_pkt_constructor)(pkt, arg, kmflag));
254 	else
255 		return (0);
256 }
257 
258 #define	P_TO_TRAN(pkt)	((pkt)->pkt_address.a_hba_tran)
259 
260 void
261 scsi_hba_pkt_destructor(void *buf, void *arg)
262 {
263 	struct scsi_pkt		*pkt;
264 	scsi_hba_tran_t		*tran = (scsi_hba_tran_t *)arg;
265 
266 	pkt = &((struct scsi_pkt_cache_wrapper *)buf)->pcw_pkt;
267 	if (tran->tran_pkt_destructor)
268 		(*tran->tran_pkt_destructor)(pkt, arg);
269 
270 	/* make sure nobody messed with our pointers */
271 	ASSERT(pkt->pkt_ha_private == (opaque_t)((char *)pkt +
272 		sizeof (struct scsi_pkt_cache_wrapper)));
273 	ASSERT(((tran->tran_hba_flags & SCSI_HBA_TRAN_SCB) == 0) ||
274 	    (pkt->pkt_scbp == (opaque_t)((char *)pkt +
275 	    tran->tran_hba_len +
276 	    (((tran->tran_hba_flags & SCSI_HBA_TRAN_CDB) == 0)
277 		? 0 : DEFAULT_CDBLEN) +
278 	    DEFAULT_PRIVLEN + sizeof (struct scsi_pkt_cache_wrapper))));
279 	ASSERT(((tran->tran_hba_flags & SCSI_HBA_TRAN_CDB) == 0) ||
280 	    (pkt->pkt_cdbp == (opaque_t)((char *)pkt +
281 	    tran->tran_hba_len +
282 	    sizeof (struct scsi_pkt_cache_wrapper))));
283 }
284 
285 /*
286  * Called by an HBA from _init()
287  */
288 int
289 scsi_hba_init(struct modlinkage *modlp)
290 {
291 	struct dev_ops *hba_dev_ops;
292 
293 	/*
294 	 * Get the devops structure of the hba,
295 	 * and put our busops vector in its place.
296 	 */
297 	hba_dev_ops = ((struct modldrv *)
298 		(modlp->ml_linkage[0]))->drv_dev_ops;
299 	ASSERT(hba_dev_ops->devo_bus_ops == NULL);
300 	hba_dev_ops->devo_bus_ops = &scsi_hba_busops;
301 
302 	/*
303 	 * Provide getinfo and hotplugging ioctl if driver
304 	 * does not provide them already
305 	 */
306 	if (hba_dev_ops->devo_cb_ops == NULL) {
307 		hba_dev_ops->devo_cb_ops = &scsi_hba_cbops;
308 	}
309 	if (hba_dev_ops->devo_cb_ops->cb_open == scsi_hba_open) {
310 		ASSERT(hba_dev_ops->devo_cb_ops->cb_close == scsi_hba_close);
311 		hba_dev_ops->devo_getinfo = scsi_hba_info;
312 	}
313 
314 	return (0);
315 }
316 
317 
318 /*
319  * Implement this older interface in terms of the new.
320  * This is hardly in the critical path, so avoiding
321  * unnecessary code duplication is more important.
322  */
323 /*ARGSUSED*/
324 int
325 scsi_hba_attach(
326 	dev_info_t		*dip,
327 	ddi_dma_lim_t		*hba_lim,
328 	scsi_hba_tran_t		*hba_tran,
329 	int			flags,
330 	void			*hba_options)
331 {
332 	ddi_dma_attr_t		hba_dma_attr;
333 
334 	bzero(&hba_dma_attr, sizeof (ddi_dma_attr_t));
335 
336 	hba_dma_attr.dma_attr_burstsizes = hba_lim->dlim_burstsizes;
337 	hba_dma_attr.dma_attr_minxfer = hba_lim->dlim_minxfer;
338 
339 	return (scsi_hba_attach_setup(dip, &hba_dma_attr, hba_tran, flags));
340 }
341 
342 
343 /*
344  * Called by an HBA to attach an instance of the driver
345  */
346 int
347 scsi_hba_attach_setup(
348 	dev_info_t		*dip,
349 	ddi_dma_attr_t		*hba_dma_attr,
350 	scsi_hba_tran_t		*hba_tran,
351 	int			flags)
352 {
353 	struct dev_ops		*hba_dev_ops;
354 	struct scsi_hba_inst	*elem;
355 	int			value;
356 	int			len;
357 	char			*prop_name;
358 	const char		*prop_value;
359 	char			*errmsg =
360 		"scsi_hba_attach: cannot create property '%s' for %s%d\n";
361 	static const char	*interconnect[] = INTERCONNECT_TYPE_ASCII;
362 
363 	/*
364 	 * Link this instance into the scsi_hba_list
365 	 */
366 	elem = kmem_alloc(sizeof (struct scsi_hba_inst), KM_SLEEP);
367 
368 	mutex_enter(&scsi_hba_mutex);
369 
370 	elem->inst_dip = dip;
371 	elem->inst_hba_tran = hba_tran;
372 
373 	elem->inst_next = NULL;
374 	elem->inst_prev = scsi_hba_list_tail;
375 	if (scsi_hba_list == NULL) {
376 		scsi_hba_list = elem;
377 	}
378 	if (scsi_hba_list_tail) {
379 		scsi_hba_list_tail->inst_next = elem;
380 	}
381 	scsi_hba_list_tail = elem;
382 	mutex_exit(&scsi_hba_mutex);
383 
384 	/*
385 	 * Save all the important HBA information that must be accessed
386 	 * later by scsi_hba_bus_ctl(), and scsi_hba_map().
387 	 */
388 	hba_tran->tran_hba_dip = dip;
389 	hba_tran->tran_hba_flags &= SCSI_HBA_TRAN_ALLOC;
390 	hba_tran->tran_hba_flags |= (flags & ~SCSI_HBA_TRAN_ALLOC);
391 
392 	/*
393 	 * Note: we only need dma_attr_minxfer and dma_attr_burstsizes
394 	 * from the DMA attributes.  scsi_hba_attach(9f) only
395 	 * guarantees that these two fields are initialized properly.
396 	 * If this changes, be sure to revisit the implementation
397 	 * of scsi_hba_attach(9F).
398 	 */
399 	hba_tran->tran_min_xfer = hba_dma_attr->dma_attr_minxfer;
400 	hba_tran->tran_min_burst_size =
401 		(1<<(ddi_ffs(hba_dma_attr->dma_attr_burstsizes)-1));
402 	hba_tran->tran_max_burst_size =
403 		(1<<(ddi_fls(hba_dma_attr->dma_attr_burstsizes)-1));
404 
405 	/* create kmem_cache, if needed */
406 	if (hba_tran->tran_pkt_constructor) {
407 		char tmp[96];
408 		int hbalen;
409 		int cmdlen = 0;
410 		int statuslen = 0;
411 
412 		ASSERT(hba_tran->tran_init_pkt == NULL);
413 		ASSERT(hba_tran->tran_destroy_pkt == NULL);
414 
415 		hba_tran->tran_init_pkt = scsi_init_cache_pkt;
416 		hba_tran->tran_destroy_pkt = scsi_free_cache_pkt;
417 
418 		hbalen = ROUNDUP(hba_tran->tran_hba_len);
419 		if (flags & SCSI_HBA_TRAN_CDB)
420 			cmdlen = ROUNDUP(DEFAULT_CDBLEN);
421 		if (flags & SCSI_HBA_TRAN_SCB)
422 			statuslen = ROUNDUP(DEFAULT_SCBLEN);
423 
424 		(void) snprintf(tmp, sizeof (tmp), "pkt_cache_%s_%d",
425 		    ddi_driver_name(dip), ddi_get_instance(dip));
426 		hba_tran->tran_pkt_cache_ptr = kmem_cache_create(tmp,
427 		    sizeof (struct scsi_pkt_cache_wrapper) +
428 		    hbalen + cmdlen + statuslen, 8,
429 		    scsi_hba_pkt_constructor, scsi_hba_pkt_destructor,
430 		    NULL, hba_tran, NULL, 0);
431 	}
432 
433 	/*
434 	 * Attach scsi configuration property parameters
435 	 * to this instance of the hba.
436 	 */
437 	prop_name = "scsi-reset-delay";
438 	len = 0;
439 	if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN, 0, prop_name,
440 			NULL, &len) == DDI_PROP_NOT_FOUND) {
441 		value = scsi_reset_delay;
442 		if (ddi_prop_update_int(DDI_MAJOR_T_UNKNOWN, dip,
443 		    prop_name, value) != DDI_PROP_SUCCESS) {
444 			cmn_err(CE_CONT, errmsg, prop_name,
445 				ddi_get_name(dip), ddi_get_instance(dip));
446 		}
447 	}
448 
449 	prop_name = "scsi-tag-age-limit";
450 	len = 0;
451 	if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN, 0, prop_name,
452 			NULL, &len) == DDI_PROP_NOT_FOUND) {
453 		value = scsi_tag_age_limit;
454 		if (ddi_prop_update_int(DDI_MAJOR_T_UNKNOWN, dip,
455 		    prop_name, value) != DDI_PROP_SUCCESS) {
456 			cmn_err(CE_CONT, errmsg, prop_name,
457 				ddi_get_name(dip), ddi_get_instance(dip));
458 		}
459 	}
460 
461 	prop_name = "scsi-watchdog-tick";
462 	len = 0;
463 	if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN, 0, prop_name,
464 			NULL, &len) == DDI_PROP_NOT_FOUND) {
465 		value = scsi_watchdog_tick;
466 		if (ddi_prop_update_int(DDI_MAJOR_T_UNKNOWN, dip,
467 		    prop_name, value) != DDI_PROP_SUCCESS) {
468 			cmn_err(CE_CONT, errmsg, prop_name,
469 				ddi_get_name(dip), ddi_get_instance(dip));
470 		}
471 	}
472 
473 	prop_name = "scsi-options";
474 	len = 0;
475 	if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN, 0, prop_name,
476 			NULL, &len) == DDI_PROP_NOT_FOUND) {
477 		value = scsi_options;
478 		if (ddi_prop_update_int(DDI_MAJOR_T_UNKNOWN, dip,
479 		    prop_name, value) != DDI_PROP_SUCCESS) {
480 			cmn_err(CE_CONT, errmsg, prop_name,
481 				ddi_get_name(dip), ddi_get_instance(dip));
482 		}
483 	}
484 
485 	prop_name = "scsi-selection-timeout";
486 	len = 0;
487 	if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN, 0, prop_name,
488 			NULL, &len) == DDI_PROP_NOT_FOUND) {
489 		value = scsi_selection_timeout;
490 		if (ddi_prop_update_int(DDI_MAJOR_T_UNKNOWN, dip,
491 		    prop_name, value) != DDI_PROP_SUCCESS) {
492 			cmn_err(CE_CONT, errmsg, prop_name,
493 				ddi_get_name(dip), ddi_get_instance(dip));
494 		}
495 	}
496 	if ((hba_tran->tran_hba_flags & SCSI_HBA_TRAN_ALLOC) &&
497 	    (hba_tran->tran_interconnect_type > 0) &&
498 	    (hba_tran->tran_interconnect_type < INTERCONNECT_MAX)) {
499 		prop_name = "initiator-interconnect-type";
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 = hba_tran->tran_interconnect_type;
504 			prop_value = interconnect[value];
505 			if (ddi_prop_update_string(DDI_MAJOR_T_UNKNOWN, dip,
506 			    prop_name, (char *)prop_value)
507 			    != DDI_PROP_SUCCESS) {
508 				cmn_err(CE_CONT, errmsg, prop_name,
509 					ddi_get_name(dip),
510 					ddi_get_instance(dip));
511 			}
512 		}
513 	}
514 
515 	ddi_set_driver_private(dip, hba_tran);
516 
517 	/*
518 	 * Create devctl minor node unless driver supplied its own
519 	 * open/close entry points
520 	 */
521 	hba_dev_ops = ddi_get_driver(dip);
522 	ASSERT(hba_dev_ops != NULL);
523 	if (hba_dev_ops->devo_cb_ops->cb_open == scsi_hba_open) {
524 		/*
525 		 * Make sure that instance number doesn't overflow
526 		 * when forming minor numbers.
527 		 */
528 		ASSERT(ddi_get_instance(dip) <=
529 		    (L_MAXMIN >> INST_MINOR_SHIFT));
530 
531 		if ((ddi_create_minor_node(dip, "devctl", S_IFCHR,
532 		    INST2DEVCTL(ddi_get_instance(dip)),
533 		    DDI_NT_SCSI_NEXUS, 0) != DDI_SUCCESS) ||
534 		    (ddi_create_minor_node(dip, "scsi", S_IFCHR,
535 		    INST2SCSI(ddi_get_instance(dip)),
536 		    DDI_NT_SCSI_ATTACHMENT_POINT, 0) != DDI_SUCCESS)) {
537 			ddi_remove_minor_node(dip, "devctl");
538 			ddi_remove_minor_node(dip, "scsi");
539 			cmn_err(CE_WARN, "scsi_hba_attach: "
540 			    "cannot create devctl/scsi minor nodes");
541 		}
542 	}
543 
544 	return (DDI_SUCCESS);
545 }
546 
547 
548 /*
549  * Called by an HBA to detach an instance of the driver
550  */
551 int
552 scsi_hba_detach(dev_info_t *dip)
553 {
554 	struct dev_ops		*hba_dev_ops;
555 	scsi_hba_tran_t		*hba;
556 	struct scsi_hba_inst	*elem;
557 
558 
559 	hba = ddi_get_driver_private(dip);
560 	ddi_set_driver_private(dip, NULL);
561 	ASSERT(hba != NULL);
562 	ASSERT(hba->tran_open_flag == 0);
563 
564 	hba_dev_ops = ddi_get_driver(dip);
565 	ASSERT(hba_dev_ops != NULL);
566 	if (hba_dev_ops->devo_cb_ops->cb_open == scsi_hba_open) {
567 		ddi_remove_minor_node(dip, "devctl");
568 		ddi_remove_minor_node(dip, "scsi");
569 	}
570 
571 	/*
572 	 * XXX - scsi_transport.h states that these data fields should not be
573 	 *	 referenced by the HBA. However, to be consistent with
574 	 *	 scsi_hba_attach(), they are being reset.
575 	 */
576 	hba->tran_hba_dip = (dev_info_t *)NULL;
577 	hba->tran_hba_flags = 0;
578 	hba->tran_min_burst_size = (uchar_t)0;
579 	hba->tran_max_burst_size = (uchar_t)0;
580 
581 	if (hba->tran_pkt_cache_ptr != NULL) {
582 		kmem_cache_destroy(hba->tran_pkt_cache_ptr);
583 		hba->tran_pkt_cache_ptr = NULL;
584 	}
585 	/*
586 	 * Remove HBA instance from scsi_hba_list
587 	 */
588 	mutex_enter(&scsi_hba_mutex);
589 	for (elem = scsi_hba_list; elem != (struct scsi_hba_inst *)NULL;
590 		elem = elem->inst_next) {
591 		if (elem->inst_dip == dip)
592 			break;
593 	}
594 
595 	if (elem == (struct scsi_hba_inst *)NULL) {
596 		cmn_err(CE_CONT, "scsi_hba_attach: unknown HBA instance\n");
597 		mutex_exit(&scsi_hba_mutex);
598 		return (DDI_FAILURE);
599 	}
600 	if (elem == scsi_hba_list) {
601 		scsi_hba_list = elem->inst_next;
602 		if (scsi_hba_list) {
603 			scsi_hba_list->inst_prev = (struct scsi_hba_inst *)NULL;
604 		}
605 		if (elem == scsi_hba_list_tail) {
606 			scsi_hba_list_tail = NULL;
607 		}
608 	} else if (elem == scsi_hba_list_tail) {
609 		scsi_hba_list_tail = elem->inst_prev;
610 		if (scsi_hba_list_tail) {
611 			scsi_hba_list_tail->inst_next =
612 					(struct scsi_hba_inst *)NULL;
613 		}
614 	} else {
615 		elem->inst_prev->inst_next = elem->inst_next;
616 		elem->inst_next->inst_prev = elem->inst_prev;
617 	}
618 	mutex_exit(&scsi_hba_mutex);
619 
620 	kmem_free(elem, sizeof (struct scsi_hba_inst));
621 
622 	return (DDI_SUCCESS);
623 }
624 
625 
626 /*
627  * Called by an HBA from _fini()
628  */
629 void
630 scsi_hba_fini(struct modlinkage *modlp)
631 {
632 	struct dev_ops *hba_dev_ops;
633 
634 	/*
635 	 * Get the devops structure of this module
636 	 * and clear bus_ops vector.
637 	 */
638 	hba_dev_ops = ((struct modldrv *)
639 		(modlp->ml_linkage[0]))->drv_dev_ops;
640 
641 	if (hba_dev_ops->devo_cb_ops == &scsi_hba_cbops) {
642 		hba_dev_ops->devo_cb_ops = NULL;
643 	}
644 
645 	if (hba_dev_ops->devo_getinfo == scsi_hba_info) {
646 		hba_dev_ops->devo_getinfo = NULL;
647 	}
648 
649 	hba_dev_ops->devo_bus_ops = (struct bus_ops *)NULL;
650 }
651 
652 
653 /*
654  * Generic bus_ctl operations for SCSI HBA's,
655  * hiding the busctl interface from the HBA.
656  */
657 /*ARGSUSED*/
658 static int
659 scsi_hba_bus_ctl(
660 	dev_info_t		*dip,
661 	dev_info_t		*rdip,
662 	ddi_ctl_enum_t		op,
663 	void			*arg,
664 	void			*result)
665 {
666 
667 	switch (op) {
668 	case DDI_CTLOPS_REPORTDEV:
669 	{
670 		struct scsi_device	*devp;
671 		scsi_hba_tran_t		*hba;
672 
673 		hba = ddi_get_driver_private(dip);
674 		ASSERT(hba != NULL);
675 
676 		devp = ddi_get_driver_private(rdip);
677 
678 		if ((hba->tran_get_bus_addr == NULL) ||
679 		    (hba->tran_get_name == NULL)) {
680 			cmn_err(CE_CONT, "?%s%d at %s%d: target %x lun %x\n",
681 			    ddi_driver_name(rdip), ddi_get_instance(rdip),
682 			    ddi_driver_name(dip), ddi_get_instance(dip),
683 			    devp->sd_address.a_target, devp->sd_address.a_lun);
684 		} else {
685 			char name[SCSI_MAXNAMELEN];
686 			char bus_addr[SCSI_MAXNAMELEN];
687 
688 			if ((*hba->tran_get_name)(devp, name,
689 			    SCSI_MAXNAMELEN) != 1) {
690 				return (DDI_FAILURE);
691 			}
692 			if ((*hba->tran_get_bus_addr)(devp, bus_addr,
693 			    SCSI_MAXNAMELEN) != 1) {
694 				return (DDI_FAILURE);
695 			}
696 			cmn_err(CE_CONT,
697 			    "?%s%d at %s%d: name %s, bus address %s\n",
698 			    ddi_driver_name(rdip), ddi_get_instance(rdip),
699 			    ddi_driver_name(dip), ddi_get_instance(dip),
700 			    name, bus_addr);
701 		}
702 		return (DDI_SUCCESS);
703 	}
704 
705 	case DDI_CTLOPS_IOMIN:
706 	{
707 		int		val;
708 		scsi_hba_tran_t	*hba;
709 
710 		hba = ddi_get_driver_private(dip);
711 		ASSERT(hba != NULL);
712 
713 		val = *((int *)result);
714 		val = maxbit(val, hba->tran_min_xfer);
715 		/*
716 		 * The 'arg' value of nonzero indicates 'streaming'
717 		 * mode.  If in streaming mode, pick the largest
718 		 * of our burstsizes available and say that that
719 		 * is our minimum value (modulo what minxfer is).
720 		 */
721 		*((int *)result) = maxbit(val, ((intptr_t)arg ?
722 			hba->tran_max_burst_size :
723 			hba->tran_min_burst_size));
724 
725 		return (ddi_ctlops(dip, rdip, op, arg, result));
726 	}
727 
728 	case DDI_CTLOPS_INITCHILD:
729 	{
730 		dev_info_t		*child_dip = (dev_info_t *)arg;
731 		struct scsi_device	*sd;
732 		char			name[SCSI_MAXNAMELEN];
733 		scsi_hba_tran_t		*hba;
734 		dev_info_t		*ndip;
735 
736 		hba = ddi_get_driver_private(dip);
737 		ASSERT(hba != NULL);
738 
739 		sd = kmem_zalloc(sizeof (struct scsi_device), KM_SLEEP);
740 
741 		/*
742 		 * Clone transport structure if requested, so
743 		 * the HBA can maintain target-specific info, if
744 		 * necessary. At least all SCSI-3 HBAs will do this.
745 		 */
746 		if (hba->tran_hba_flags & SCSI_HBA_TRAN_CLONE) {
747 			scsi_hba_tran_t	*clone =
748 			    kmem_alloc(sizeof (scsi_hba_tran_t), KM_SLEEP);
749 
750 			bcopy(hba, clone, sizeof (scsi_hba_tran_t));
751 			hba = clone;
752 			hba->tran_sd = sd;
753 		} else {
754 			ASSERT(hba->tran_sd == NULL);
755 		}
756 
757 		sd->sd_dev = child_dip;
758 		sd->sd_address.a_hba_tran = hba;
759 
760 		/*
761 		 * Make sure that HBA either supports both or none
762 		 * of tran_get_name/tran_get_addr
763 		 */
764 		if ((hba->tran_get_name != NULL) ||
765 		    (hba->tran_get_bus_addr != NULL)) {
766 			if ((hba->tran_get_name == NULL) ||
767 			    (hba->tran_get_bus_addr == NULL)) {
768 				cmn_err(CE_CONT,
769 				    "%s%d: should support both or none of "
770 				    "tran_get_name and tran_get_bus_addr\n",
771 				    ddi_get_name(dip), ddi_get_instance(dip));
772 				goto failure;
773 			}
774 		}
775 
776 		/*
777 		 * In case HBA doesn't support tran_get_name/tran_get_bus_addr
778 		 * (e.g. most pre-SCSI-3 HBAs), we have to continue
779 		 * to provide old semantics. In case a HBA driver does
780 		 * support it, a_target and a_lun fields of scsi_address
781 		 * are not defined and will be 0 except for parallel bus.
782 		 */
783 		{
784 			int	t_len;
785 			int	targ = 0;
786 			int	lun = 0;
787 
788 			t_len = sizeof (targ);
789 			if (ddi_prop_op(DDI_DEV_T_ANY, child_dip,
790 			    PROP_LEN_AND_VAL_BUF, DDI_PROP_DONTPASS |
791 			    DDI_PROP_CANSLEEP, "target", (caddr_t)&targ,
792 			    &t_len) != DDI_SUCCESS) {
793 				if (hba->tran_get_name == NULL) {
794 					kmem_free(sd,
795 						sizeof (struct scsi_device));
796 					if (hba->tran_hba_flags &
797 					    SCSI_HBA_TRAN_CLONE) {
798 						kmem_free(hba,
799 						    sizeof (scsi_hba_tran_t));
800 					}
801 					return (DDI_NOT_WELL_FORMED);
802 				}
803 			}
804 
805 			t_len = sizeof (lun);
806 			(void) ddi_prop_op(DDI_DEV_T_ANY, child_dip,
807 			    PROP_LEN_AND_VAL_BUF, DDI_PROP_DONTPASS |
808 			    DDI_PROP_CANSLEEP, "lun", (caddr_t)&lun,
809 			    &t_len);
810 
811 			/*
812 			 * If the HBA does not implement tran_get_name then it
813 			 * doesn't have any hope of supporting a LUN >= 256.
814 			 */
815 			if (lun >= 256 && hba->tran_get_name == NULL) {
816 				goto failure;
817 			}
818 
819 			/*
820 			 * This is also to make sure that if someone plugs in
821 			 * a SCSI-2 disks to a SCSI-3 parallel bus HBA,
822 			 * his SCSI-2 target driver still continue to work.
823 			 */
824 			sd->sd_address.a_target = (ushort_t)targ;
825 			sd->sd_address.a_lun = (uchar_t)lun;
826 		}
827 
828 		/*
829 		 * In case HBA support tran_get_name (e.g. all SCSI-3 HBAs),
830 		 * give it a chance to tell us the name.
831 		 * If it doesn't support this entry point, a name will be
832 		 * fabricated
833 		 */
834 		if (scsi_get_name(sd, name, SCSI_MAXNAMELEN) != 1) {
835 			goto failure;
836 		}
837 
838 		/*
839 		 * Prevent duplicate nodes.
840 		 */
841 		ndip = ndi_devi_find(dip, ddi_node_name(child_dip), name);
842 
843 		if (ndip && (ndip != child_dip)) {
844 			goto failure;
845 		}
846 
847 		ddi_set_name_addr(child_dip, name);
848 
849 		/*
850 		 * This is a grotty hack that allows direct-access
851 		 * (non-scsi) drivers using this interface to
852 		 * put its own vector in the 'a_hba_tran' field.
853 		 * When the drivers are fixed, remove this hack.
854 		 */
855 		sd->sd_reserved = hba;
856 
857 		/*
858 		 * call hba's target init entry point if it exists
859 		 */
860 		if (hba->tran_tgt_init != NULL) {
861 			if ((*hba->tran_tgt_init)
862 			    (dip, child_dip, hba, sd) != DDI_SUCCESS) {
863 				ddi_set_name_addr(child_dip, NULL);
864 				goto failure;
865 			}
866 
867 			/*
868 			 * Another grotty hack to undo initialization
869 			 * some hba's think they have authority to
870 			 * perform.
871 			 *
872 			 * XXX - Pending dadk_probe() semantics
873 			 *	 change.  (Re: 1171432)
874 			 */
875 			if (hba->tran_tgt_probe != NULL)
876 				sd->sd_inq = NULL;
877 		}
878 
879 		mutex_init(&sd->sd_mutex, NULL, MUTEX_DRIVER, NULL);
880 
881 		ddi_set_driver_private(child_dip, sd);
882 
883 		return (DDI_SUCCESS);
884 
885 failure:
886 		kmem_free(sd, sizeof (struct scsi_device));
887 		if (hba->tran_hba_flags & SCSI_HBA_TRAN_CLONE) {
888 			kmem_free(hba, sizeof (scsi_hba_tran_t));
889 		}
890 		return (DDI_FAILURE);
891 	}
892 
893 	case DDI_CTLOPS_UNINITCHILD:
894 	{
895 		struct scsi_device	*sd;
896 		dev_info_t		*child_dip = (dev_info_t *)arg;
897 		scsi_hba_tran_t		*hba;
898 
899 		hba = ddi_get_driver_private(dip);
900 		ASSERT(hba != NULL);
901 
902 		sd = ddi_get_driver_private(child_dip);
903 		ASSERT(sd != NULL);
904 
905 		if (hba->tran_hba_flags & SCSI_HBA_TRAN_CLONE) {
906 			/*
907 			 * This is a grotty hack, continued.  This
908 			 * should be:
909 			 *	hba = sd->sd_address.a_hba_tran;
910 			 */
911 			hba = sd->sd_reserved;
912 			ASSERT(hba->tran_hba_flags & SCSI_HBA_TRAN_CLONE);
913 			ASSERT(hba->tran_sd == sd);
914 		} else {
915 			ASSERT(hba->tran_sd == NULL);
916 		}
917 
918 		scsi_unprobe(sd);
919 		if (hba->tran_tgt_free != NULL) {
920 			(*hba->tran_tgt_free) (dip, child_dip, hba, sd);
921 		}
922 		mutex_destroy(&sd->sd_mutex);
923 		if (hba->tran_hba_flags & SCSI_HBA_TRAN_CLONE) {
924 			kmem_free(hba, sizeof (scsi_hba_tran_t));
925 		}
926 		kmem_free(sd, sizeof (*sd));
927 
928 		ddi_set_driver_private(child_dip, NULL);
929 		ddi_set_name_addr(child_dip, NULL);
930 
931 		return (DDI_SUCCESS);
932 	}
933 	case DDI_CTLOPS_SIDDEV:
934 		return (ndi_dev_is_persistent_node(rdip) ?
935 		    DDI_SUCCESS : DDI_FAILURE);
936 
937 	/* XXX these should be handled */
938 	case DDI_CTLOPS_POWER:
939 	case DDI_CTLOPS_ATTACH:
940 	case DDI_CTLOPS_DETACH:
941 
942 		return (DDI_SUCCESS);
943 
944 	/*
945 	 * These ops correspond to functions that "shouldn't" be called
946 	 * by a SCSI target driver.  So we whine when we're called.
947 	 */
948 	case DDI_CTLOPS_DMAPMAPC:
949 	case DDI_CTLOPS_REPORTINT:
950 	case DDI_CTLOPS_REGSIZE:
951 	case DDI_CTLOPS_NREGS:
952 	case DDI_CTLOPS_SLAVEONLY:
953 	case DDI_CTLOPS_AFFINITY:
954 	case DDI_CTLOPS_POKE:
955 	case DDI_CTLOPS_PEEK:
956 		cmn_err(CE_CONT, "%s%d: invalid op (%d) from %s%d\n",
957 			ddi_get_name(dip), ddi_get_instance(dip),
958 			op, ddi_get_name(rdip), ddi_get_instance(rdip));
959 		return (DDI_FAILURE);
960 
961 	/*
962 	 * Everything else (e.g. PTOB/BTOP/BTOPR requests) we pass up
963 	 */
964 	default:
965 		return (ddi_ctlops(dip, rdip, op, arg, result));
966 	}
967 }
968 
969 
970 /*
971  * Called by an HBA to allocate a scsi_hba_tran structure
972  */
973 /*ARGSUSED*/
974 scsi_hba_tran_t *
975 scsi_hba_tran_alloc(
976 	dev_info_t		*dip,
977 	int			flags)
978 {
979 	scsi_hba_tran_t		*hba_tran;
980 
981 	hba_tran = kmem_zalloc(sizeof (scsi_hba_tran_t),
982 		(flags & SCSI_HBA_CANSLEEP) ? KM_SLEEP : KM_NOSLEEP);
983 
984 	hba_tran->tran_interconnect_type = INTERCONNECT_PARALLEL;
985 	hba_tran->tran_hba_flags |= SCSI_HBA_TRAN_ALLOC;
986 
987 	return (hba_tran);
988 }
989 
990 
991 
992 /*
993  * Called by an HBA to free a scsi_hba_tran structure
994  */
995 void
996 scsi_hba_tran_free(
997 	scsi_hba_tran_t		*hba_tran)
998 {
999 	kmem_free(hba_tran, sizeof (scsi_hba_tran_t));
1000 }
1001 
1002 
1003 
1004 /*
1005  * Private wrapper for scsi_pkt's allocated via scsi_hba_pkt_alloc()
1006  */
1007 struct scsi_pkt_wrapper {
1008 	struct scsi_pkt		scsi_pkt;
1009 	int			pkt_wrapper_len;
1010 };
1011 
1012 #if !defined(lint)
1013 _NOTE(SCHEME_PROTECTS_DATA("unique per thread", scsi_pkt_wrapper))
1014 _NOTE(SCHEME_PROTECTS_DATA("Unshared Data", dev_ops))
1015 #endif
1016 
1017 /*
1018  * Called by an HBA to allocate a scsi_pkt
1019  */
1020 /*ARGSUSED*/
1021 struct scsi_pkt *
1022 scsi_hba_pkt_alloc(
1023 	dev_info_t		*dip,
1024 	struct scsi_address	*ap,
1025 	int			cmdlen,
1026 	int			statuslen,
1027 	int			tgtlen,
1028 	int			hbalen,
1029 	int			(*callback)(caddr_t arg),
1030 	caddr_t			arg)
1031 {
1032 	struct scsi_pkt		*pkt;
1033 	struct scsi_pkt_wrapper	*hba_pkt;
1034 	caddr_t			p;
1035 	int			pktlen;
1036 
1037 	/*
1038 	 * Sanity check
1039 	 */
1040 	if (callback != SLEEP_FUNC && callback != NULL_FUNC) {
1041 		cmn_err(CE_PANIC, "scsi_hba_pkt_alloc: callback must be"
1042 			" either SLEEP or NULL\n");
1043 	}
1044 
1045 	/*
1046 	 * Round up so everything gets allocated on long-word boundaries
1047 	 */
1048 	cmdlen = ROUNDUP(cmdlen);
1049 	tgtlen = ROUNDUP(tgtlen);
1050 	hbalen = ROUNDUP(hbalen);
1051 	statuslen = ROUNDUP(statuslen);
1052 	pktlen = sizeof (struct scsi_pkt_wrapper)
1053 		+ cmdlen + tgtlen + hbalen + statuslen;
1054 
1055 	hba_pkt = kmem_zalloc(pktlen,
1056 		(callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP);
1057 	if (hba_pkt == NULL) {
1058 		ASSERT(callback == NULL_FUNC);
1059 		return (NULL);
1060 	}
1061 
1062 	/*
1063 	 * Set up our private info on this pkt
1064 	 */
1065 	hba_pkt->pkt_wrapper_len = pktlen;
1066 	pkt = &hba_pkt->scsi_pkt;
1067 	p = (caddr_t)(hba_pkt + 1);
1068 
1069 	/*
1070 	 * Set up pointers to private data areas, cdb, and status.
1071 	 */
1072 	if (hbalen > 0) {
1073 		pkt->pkt_ha_private = (opaque_t)p;
1074 		p += hbalen;
1075 	}
1076 	if (tgtlen > 0) {
1077 		pkt->pkt_private = (opaque_t)p;
1078 		p += tgtlen;
1079 	}
1080 	if (statuslen > 0) {
1081 		pkt->pkt_scbp = (uchar_t *)p;
1082 		p += statuslen;
1083 	}
1084 	if (cmdlen > 0) {
1085 		pkt->pkt_cdbp = (uchar_t *)p;
1086 	}
1087 
1088 	/*
1089 	 * Initialize the pkt's scsi_address
1090 	 */
1091 	pkt->pkt_address = *ap;
1092 
1093 	return (pkt);
1094 }
1095 
1096 
1097 /*
1098  * Called by an HBA to free a scsi_pkt
1099  */
1100 /*ARGSUSED*/
1101 void
1102 scsi_hba_pkt_free(
1103 	struct scsi_address	*ap,
1104 	struct scsi_pkt		*pkt)
1105 {
1106 	kmem_free(pkt, ((struct scsi_pkt_wrapper *)pkt)->pkt_wrapper_len);
1107 }
1108 
1109 
1110 
1111 /*
1112  * Called by an HBA to map strings to capability indices
1113  */
1114 int
1115 scsi_hba_lookup_capstr(
1116 	char			*capstr)
1117 {
1118 	/*
1119 	 * Capability strings, masking the the '-' vs. '_' misery
1120 	 */
1121 	static struct cap_strings {
1122 		char	*cap_string;
1123 		int	cap_index;
1124 	} cap_strings[] = {
1125 		{ "dma_max",		SCSI_CAP_DMA_MAX		},
1126 		{ "dma-max",		SCSI_CAP_DMA_MAX		},
1127 		{ "msg_out",		SCSI_CAP_MSG_OUT		},
1128 		{ "msg-out",		SCSI_CAP_MSG_OUT		},
1129 		{ "disconnect",		SCSI_CAP_DISCONNECT		},
1130 		{ "synchronous",	SCSI_CAP_SYNCHRONOUS		},
1131 		{ "wide_xfer",		SCSI_CAP_WIDE_XFER		},
1132 		{ "wide-xfer",		SCSI_CAP_WIDE_XFER		},
1133 		{ "parity",		SCSI_CAP_PARITY			},
1134 		{ "initiator-id",	SCSI_CAP_INITIATOR_ID		},
1135 		{ "untagged-qing",	SCSI_CAP_UNTAGGED_QING		},
1136 		{ "tagged-qing",	SCSI_CAP_TAGGED_QING		},
1137 		{ "auto-rqsense",	SCSI_CAP_ARQ			},
1138 		{ "linked-cmds",	SCSI_CAP_LINKED_CMDS		},
1139 		{ "sector-size",	SCSI_CAP_SECTOR_SIZE		},
1140 		{ "total-sectors",	SCSI_CAP_TOTAL_SECTORS		},
1141 		{ "geometry",		SCSI_CAP_GEOMETRY		},
1142 		{ "reset-notification",	SCSI_CAP_RESET_NOTIFICATION	},
1143 		{ "qfull-retries",	SCSI_CAP_QFULL_RETRIES		},
1144 		{ "qfull-retry-interval", SCSI_CAP_QFULL_RETRY_INTERVAL	},
1145 		{ "scsi-version",	SCSI_CAP_SCSI_VERSION		},
1146 		{ "interconnect-type",	SCSI_CAP_INTERCONNECT_TYPE	},
1147 		{ "lun-reset",		SCSI_CAP_LUN_RESET		},
1148 		{ "max-cdb-length",	SCSI_CAP_CDB_LEN		},
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