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