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