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