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