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