1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/note.h>
27 
28 /*
29  * Generic SCSI Host Bus Adapter interface implementation
30  */
31 #include <sys/scsi/scsi.h>
32 #include <sys/file.h>
33 #include <sys/ddi_impldefs.h>
34 #include <sys/ndi_impldefs.h>
35 #include <sys/ddi.h>
36 #include <sys/epm.h>
37 
38 extern struct scsi_pkt *scsi_init_cache_pkt(struct scsi_address *,
39 		    struct scsi_pkt *, struct buf *, int, int, int, int,
40 		    int (*)(caddr_t), caddr_t);
41 extern void scsi_free_cache_pkt(struct scsi_address *,
42 		    struct scsi_pkt *);
43 extern void scsi_cache_dmafree(struct scsi_address *,
44 		    struct scsi_pkt *);
45 extern void scsi_sync_cache_pkt(struct scsi_address *,
46 		    struct scsi_pkt *);
47 
48 /*
49  * Round up all allocations so that we can guarantee
50  * long-long alignment.  This is the same alignment
51  * provided by kmem_alloc().
52  */
53 #define	ROUNDUP(x)	(((x) + 0x07) & ~0x07)
54 
55 /* Magic number to track correct allocations in wrappers */
56 #define	PKT_WRAPPER_MAGIC	0xa110ced	/* alloced correctly */
57 
58 kmutex_t	scsi_flag_nointr_mutex;
59 kcondvar_t	scsi_flag_nointr_cv;
60 kmutex_t	scsi_log_mutex;
61 
62 /*
63  * Prototypes for static functions
64  */
65 static int	scsi_hba_bus_ctl(
66 			dev_info_t		*self,
67 			dev_info_t		*child,
68 			ddi_ctl_enum_t		op,
69 			void			*arg,
70 			void			*result);
71 
72 static int	scsi_hba_map_fault(
73 			dev_info_t		*self,
74 			dev_info_t		*child,
75 			struct hat		*hat,
76 			struct seg		*seg,
77 			caddr_t			addr,
78 			struct devpage		*dp,
79 			pfn_t			pfn,
80 			uint_t			prot,
81 			uint_t			lock);
82 
83 static int	scsi_hba_get_eventcookie(
84 			dev_info_t		*self,
85 			dev_info_t		*child,
86 			char			*name,
87 			ddi_eventcookie_t	*eventp);
88 
89 static int	scsi_hba_add_eventcall(
90 			dev_info_t		*self,
91 			dev_info_t		*child,
92 			ddi_eventcookie_t	event,
93 			void			(*callback)(
94 				dev_info_t		*dip,
95 				ddi_eventcookie_t	event,
96 				void			*arg,
97 				void			*bus_impldata),
98 			void			*arg,
99 			ddi_callback_id_t	*cb_id);
100 
101 static int	scsi_hba_remove_eventcall(
102 			dev_info_t		*self,
103 			ddi_callback_id_t	id);
104 
105 static int	scsi_hba_post_event(
106 			dev_info_t		*self,
107 			dev_info_t		*child,
108 			ddi_eventcookie_t	event,
109 			void			*bus_impldata);
110 
111 static int	scsi_hba_info(
112 			dev_info_t		*self,
113 			ddi_info_cmd_t		infocmd,
114 			void			*arg,
115 			void			**result);
116 
117 static int	scsi_hba_bus_config(
118 			dev_info_t		*self,
119 			uint_t			flags,
120 			ddi_bus_config_op_t	op,
121 			void			*arg,
122 			dev_info_t		**childp);
123 
124 static int	scsi_hba_bus_unconfig(
125 			dev_info_t		*self,
126 			uint_t			flags,
127 			ddi_bus_config_op_t	op,
128 			void			*arg);
129 
130 static int	scsi_hba_fm_init_child(
131 			dev_info_t		*self,
132 			dev_info_t		*child,
133 			int			cap,
134 			ddi_iblock_cookie_t	*ibc);
135 
136 static int scsi_hba_bus_power(
137 			dev_info_t		*self,
138 			void			*impl_arg,
139 			pm_bus_power_op_t	op,
140 			void			*arg,
141 			void			*result);
142 
143 /* busops vector for SCSI HBA's. */
144 static struct bus_ops scsi_hba_busops = {
145 	BUSO_REV,
146 	nullbusmap,			/* bus_map */
147 	NULL,				/* bus_get_intrspec */
148 	NULL,				/* bus_add_intrspec */
149 	NULL,				/* bus_remove_intrspec */
150 	scsi_hba_map_fault,		/* bus_map_fault */
151 	ddi_dma_map,			/* bus_dma_map */
152 	ddi_dma_allochdl,		/* bus_dma_allochdl */
153 	ddi_dma_freehdl,		/* bus_dma_freehdl */
154 	ddi_dma_bindhdl,		/* bus_dma_bindhdl */
155 	ddi_dma_unbindhdl,		/* bus_unbindhdl */
156 	ddi_dma_flush,			/* bus_dma_flush */
157 	ddi_dma_win,			/* bus_dma_win */
158 	ddi_dma_mctl,			/* bus_dma_ctl */
159 	scsi_hba_bus_ctl,		/* bus_ctl */
160 	ddi_bus_prop_op,		/* bus_prop_op */
161 	scsi_hba_get_eventcookie,	/* bus_get_eventcookie */
162 	scsi_hba_add_eventcall,		/* bus_add_eventcall */
163 	scsi_hba_remove_eventcall,	/* bus_remove_eventcall */
164 	scsi_hba_post_event,		/* bus_post_event */
165 	NULL,				/* bus_intr_ctl */
166 	scsi_hba_bus_config,		/* bus_config */
167 	scsi_hba_bus_unconfig,		/* bus_unconfig */
168 	scsi_hba_fm_init_child,		/* bus_fm_init */
169 	NULL,				/* bus_fm_fini */
170 	NULL,				/* bus_fm_access_enter */
171 	NULL,				/* bus_fm_access_exit */
172 	scsi_hba_bus_power		/* bus_power */
173 };
174 
175 /* cb_ops for hotplug :devctl and :scsi support */
176 static struct cb_ops scsi_hba_cbops = {
177 	scsi_hba_open,
178 	scsi_hba_close,
179 	nodev,			/* strategy */
180 	nodev,			/* print */
181 	nodev,			/* dump */
182 	nodev,			/* read */
183 	nodev,			/* write */
184 	scsi_hba_ioctl,		/* ioctl */
185 	nodev,			/* devmap */
186 	nodev,			/* mmap */
187 	nodev,			/* segmap */
188 	nochpoll,		/* poll */
189 	ddi_prop_op,		/* prop_op */
190 	NULL,			/* stream */
191 	D_NEW|D_MP|D_HOTPLUG,	/* cb_flag */
192 	CB_REV,			/* rev */
193 	nodev,			/* int (*cb_aread)() */
194 	nodev			/* int (*cb_awrite)() */
195 };
196 
197 /*
198  * SCSI_HBA_LOG is used for all messages. Both a logging level and a component
199  * are specified when generating a message. Some levels correspond directly to
200  * cmn_err levels, the others are associated with increasing levels diagnostic.
201  * The component is used to identify groups of messages by utility, typically
202  * the entry point. Filtering is provided for both the level and component.
203  * Messages with cmn_err levels or not associated with a component
204  * (SCSI_HBA_LOG_NC) are never filtered.
205  *
206  * For debugging, more complete information can be displayed with each message
207  * (full device path and pointer values).
208  */
209 /* logging levels */
210 #define	SCSI_HBA_LOGCONT	CE_CONT
211 #define	SCSI_HBA_LOGNOTE	CE_NOTE
212 #define	SCSI_HBA_LOGWARN	CE_WARN
213 #define	SCSI_HBA_LOGPANIC	CE_PANIC
214 #define	SCSI_HBA_LOGIGNORE	CE_IGNORE
215 #define	SCSI_HBA_LOG_CE_MASK	0x0000000F	/* no filter */
216 #define	SCSI_HBA_LOGDIAG1	0x00000010
217 #define	SCSI_HBA_LOGDIAG2	0x00000020
218 #define	SCSI_HBA_LOGDIAG3	0x00000040
219 #define	SCSI_HBA_LOGDIAG4	0x00000080
220 #define	SCSI_HBA_LOGTRACE	0x00000100
221 #if (CE_CONT | CE_NOTE | CE_WARN | CE_PANIC | CE_IGNORE) > SCSI_HBA_LOG_CE_MASK
222 Error, problem with CE_ definitions
223 #endif
224 
225 /* logging components */
226 #define	SCSI_HBA_LOG_NC				0x00000000 /* no filter */
227 #define	SCSI_HBA_LOG_INITIALIZE_HBA_INTERFACE	0x00000001
228 #define	SCSI_HBA_LOG_ATTACH_SETUP		0x00000002
229 #define	SCSI_HBA_LOG_BUS_CTL			0x00000004
230 
231 #define	SCSI_HBA_LOG_BUS_CONFIG			0x00000010
232 #define	SCSI_HBA_LOG_BUS_CONFIGONE		0x00000020
233 #define	SCSI_HBA_LOG_BUS_CONFIGALL_SPI		0x00000040
234 #define	SCSI_HBA_LOG_ENUM_LUNS_ON_TGT		0x00000080
235 
236 #define	SCSI_HBA_LOG_DEVICE_REPORTLUNS		0x00000100
237 #define	SCSI_HBA_LOG_DEVICE_CONFIG		0x00000200
238 #define	SCSI_HBA_LOG_DEVICE_CONFIGCHILD		0x00000400
239 #define	SCSI_HBA_LOG_DEVICE_CREATECHILD		0x00000800
240 
241 #define	SCSI_HBA_LOG_DEVICE_INITCHILD		0x00001000
242 
243 #define	SCSI_HBA_LOG_BUS_UNCONFIG		0x00010000
244 #define	SCSI_HBA_LOG_BUS_UNCONFIGONE		0x00020000
245 #define	SCSI_HBA_LOG_BUS_UNCONFIGALL_SPI	0x00040000
246 #define	SCSI_HBA_LOG_DEVICE_UNINITCHILD		0x00080000
247 
248 #define	SCSI_HBA_LOG_REMOVE_NODE		0x00100000
249 
250 #define	SCSI_HBA_LOG_MSCSI_BUS_CONFIG_PORT	0x01000000
251 #define	SCSI_HBA_LOG_MSCSI_DEFINE_PORT		0x02000000
252 
253 #define	SCSI_HBA_LOG_BADLUN			0x10000000
254 #define	SCSI_HBA_LOG_PKT_ALLOC			0x20000000
255 #define	SCSI_HBA_LOG_DEVI_FIND			0x40000000
256 
257 
258 #define	SCSI_HBA_LOG_ASCII {			\
259 	"scsi_hba_initialize_hba_interface",	\
260 	"scsi_hba_attach_setup",		\
261 	"scsi_hba_bus_ctl",			\
262 	"-",					\
263 						\
264 	"scsi_hba_bus_config",			\
265 	"scsi_hba_bus_configone",		\
266 	"scsi_hba_bus_configall_spi",		\
267 	"scsi_hba_enum_luns_on_tgt",		\
268 						\
269 	"scsi_hba_device_reportluns",		\
270 	"scsi_hba_device_config",		\
271 	"scsi_hba_device_configchild",		\
272 	"scsi_hba_device_createchild",		\
273 						\
274 	"scsi_busctl_initchild",		\
275 	"-",					\
276 	"-",					\
277 	"-",					\
278 						\
279 	"scsi_hba_bus_unconfig",		\
280 	"scsi_hba_bus_unconfigone",		\
281 	"scsi_hba_bus_unconfigall_spi",		\
282 	"scsi_busctl_uninitchild",		\
283 						\
284 	"scsi_hba_remove_node",			\
285 	"-",					\
286 	"-",					\
287 	"-",					\
288 						\
289 	"scsi_hba_mscsi_bus_config_port",	\
290 	"scsi_hba_mscsi_define_port",		\
291 	"-",					\
292 	"-",					\
293 						\
294 	"scsi_hba_badlun",			\
295 	"scsi_hba_pkt_alloc",			\
296 	"scsi_hba_devi_find",			\
297 	NULL }
298 
299 /*
300  * Tunable log message augmentation and filters: filters do not apply to
301  * SCSI_HBA_LOG_CE_MASK level or SCSI_HBA_LOG_NC component messages.
302  *
303  * An example set of /etc/system tunings to debug a SCSA HBA driver called
304  * "fp" might be:
305  *	echo "set scsi:scsi_hba_log_filter_level=0xff"	>> /etc/system
306  *	echo "set scsi:scsi_hba_log_filter_hba=\"fp\""	>> /etc/system
307  *	echo "set scsi:scsi_hba_log_info=0x5"		>> /etc/system
308  *	echo "set scsi:scsi_hba_log_mt_disable=0x6"	>> /etc/system
309  */
310 int		scsi_hba_log_filter_level =
311 			SCSI_HBA_LOGDIAG1 |
312 			0;
313 int		scsi_hba_log_filter_component =
314 			-1;		/* all components */
315 char		*scsi_hba_log_filter_hba = "\0\0\0\0\0\0\0\0\0\0\0\0";
316 int		scsi_hba_log_info =	/* augmentation: extra info to print */
317 			(0 << 0) |	/* 0x0001: process information */
318 			(0 << 1) |	/* 0x0002: full devices path */
319 			(0 << 2);	/* 0x0004: devinfo pointer */
320 int		scsi_hba_log_mt_disable =
321 			/* SCSI_ENUMERATION_MT_LUN_DISABLE | */
322 			/* SCSI_ENUMERATION_MT_TARGET_DISABLE | */
323 			0;
324 
325 /* static data for HBA logging subsystem */
326 static kmutex_t	scsi_hba_log_mutex;
327 static char	scsi_hba_log_i[512];
328 static char	scsi_hba_log_buf[512];
329 static char	scsi_hba_fmt[64];
330 static char	*scsi_hba_log_lab[] = SCSI_HBA_LOG_ASCII;
331 
332 /* Macros to use in source code */
333 #define	_LOG(level, component)	SCSI_HBA_LOG##level, SCSI_HBA_LOG_##component
334 #define	SCSI_HBA_LOG(x)	scsi_hba_log x
335 
336 /*PRINTFLIKE5*/
337 void
338 scsi_hba_log(int level, int component,
339     dev_info_t *self, dev_info_t *child, const char *fmt, ...)
340 {
341 	va_list		ap;
342 	int		clevel;
343 	char		*info;
344 	char		*clabel;
345 	char		*f;
346 	int		i;
347 
348 	/* derive self from child's parent */
349 	if ((self == NULL) && child)
350 		self = ddi_get_parent(child);
351 
352 	/* always allow filtering on TRACE calls */
353 	if ((level & SCSI_HBA_LOGTRACE) &&
354 	    ((scsi_hba_log_filter_level & SCSI_HBA_LOGTRACE) == 0))
355 		return;
356 
357 	/* no filtering of SCSI_HBA_LOG_CE_MASK or NC messages */
358 	if (((level & SCSI_HBA_LOG_CE_MASK) != level) &&
359 	    (component != SCSI_HBA_LOG_NC)) {
360 		/* filter on level */
361 		if ((level & scsi_hba_log_filter_level) == 0)
362 			return;
363 
364 		/* filter on component */
365 		if ((component & scsi_hba_log_filter_component) == 0)
366 			return;
367 
368 		/* filter on self */
369 		if (self && scsi_hba_log_filter_hba &&
370 		    *scsi_hba_log_filter_hba &&
371 		    ((ddi_driver_name(self) == NULL) ||
372 		    strcmp(ddi_driver_name(self), scsi_hba_log_filter_hba)))
373 			return;
374 	}
375 
376 
377 	/* determine the cmn_err form */
378 	clevel = ((level & SCSI_HBA_LOG_CE_MASK) == level) ? level : CE_CONT;
379 
380 	/* determine the component label, SCSI_HBA_LOG_NC has none */
381 	clabel = NULL;
382 	for (i = 0; scsi_hba_log_lab[i]; i++) {
383 		if (component & (1 << i)) {
384 			clabel = scsi_hba_log_lab[i];
385 			break;
386 		}
387 	}
388 
389 	if ((clabel == NULL) && (level & SCSI_HBA_LOGTRACE))
390 		clabel = "trace";
391 
392 	mutex_enter(&scsi_hba_log_mutex);
393 
394 	/* skip special first characters, we add them back below */
395 	f = (char *)fmt;
396 	if (*f && strchr("^!?", *f))
397 		f++;
398 	va_start(ap, fmt);
399 	(void) vsprintf(scsi_hba_log_buf, f, ap);
400 	va_end(ap);
401 
402 	/* augment message */
403 	info = scsi_hba_log_i;
404 	*info = '\0';
405 	if ((scsi_hba_log_info & 0x0001) && curproc && PTOU(curproc)->u_comm) {
406 		(void) sprintf(info, "%s[%d]%p ",
407 		    PTOU(curproc)->u_comm, curproc->p_pid, (void *)curthread);
408 		info += strlen(info);
409 	}
410 	if (self) {
411 		if ((scsi_hba_log_info & 0x0004) && (child || self)) {
412 			(void) sprintf(info, "%p ",
413 			    (void *)(child ? child : self));
414 			info += strlen(info);
415 		}
416 		if (scsi_hba_log_info & 0x0002)	{
417 			(void) ddi_pathname(child ? child : self, info);
418 			(void) strcat(info, " ");
419 			info += strlen(info);
420 		} else {
421 			(void) sprintf(info, "%s%d: ",
422 			    ddi_driver_name(self), ddi_get_instance(self));
423 			info += strlen(info);
424 			if (child) {
425 				if (i_ddi_node_state(child) < DS_INITIALIZED)
426 					(void) sprintf(info, "%s ",
427 					    ddi_node_name(child));
428 				else
429 					(void) sprintf(info, "%s@%s ",
430 					    ddi_node_name(child),
431 					    ddi_get_name_addr(child));
432 				info += strlen(info);
433 			}
434 		}
435 	}
436 
437 	/* special first characters must be in format string itself */
438 	f = scsi_hba_fmt;
439 	if (fmt[0] && strchr("^!?", fmt[0]))
440 		*f++ = fmt[0];
441 	(void) sprintf(f, "%s", clabel ? "%s: %s%s%s" : "%s%s%s");
442 	if (clabel)
443 		cmn_err(clevel, scsi_hba_fmt, clabel, scsi_hba_log_i,
444 		    scsi_hba_log_buf, clevel == CE_CONT ? "\n" : "");
445 	else
446 		cmn_err(clevel, scsi_hba_fmt, scsi_hba_log_i,
447 		    scsi_hba_log_buf, clevel == CE_CONT ? "\n" : "");
448 	mutex_exit(&scsi_hba_log_mutex);
449 }
450 
451 /*
452  * Called from _init() when loading "scsi" module
453  */
454 void
455 scsi_initialize_hba_interface()
456 {
457 	SCSI_HBA_LOG((_LOG(TRACE, NC), NULL, NULL,
458 	    "scsi_initialize_hba_interface"));
459 
460 	mutex_init(&scsi_log_mutex, NULL, MUTEX_DRIVER, NULL);
461 	mutex_init(&scsi_flag_nointr_mutex, NULL, MUTEX_DRIVER, NULL);
462 	cv_init(&scsi_flag_nointr_cv, NULL, CV_DRIVER, NULL);
463 	mutex_init(&scsi_hba_log_mutex, NULL, MUTEX_DRIVER, NULL);
464 }
465 
466 int
467 scsi_hba_pkt_constructor(void *buf, void *arg, int kmflag)
468 {
469 	struct scsi_pkt_cache_wrapper *pktw;
470 	struct scsi_pkt		*pkt;
471 	scsi_hba_tran_t		*tran = (scsi_hba_tran_t *)arg;
472 	int			pkt_len;
473 	char			*ptr;
474 
475 	/*
476 	 * allocate a chunk of memory for the following:
477 	 * scsi_pkt
478 	 * pcw_* fields
479 	 * pkt_ha_private
480 	 * pkt_cdbp, if needed
481 	 * (pkt_private always null)
482 	 * pkt_scbp, if needed
483 	 */
484 	pkt_len = tran->tran_hba_len + sizeof (struct scsi_pkt_cache_wrapper);
485 	if (tran->tran_hba_flags & SCSI_HBA_TRAN_CDB)
486 		pkt_len += DEFAULT_CDBLEN;
487 	if (tran->tran_hba_flags & SCSI_HBA_TRAN_SCB)
488 		pkt_len += DEFAULT_SCBLEN;
489 	bzero(buf, pkt_len);
490 
491 	ptr = buf;
492 	pktw = buf;
493 	ptr += sizeof (struct scsi_pkt_cache_wrapper);
494 	pkt = &(pktw->pcw_pkt);
495 	pkt->pkt_ha_private = (opaque_t)ptr;
496 
497 	pktw->pcw_magic = PKT_WRAPPER_MAGIC;	/* alloced correctly */
498 	/*
499 	 * keep track of the granularity at the time this handle was
500 	 * allocated
501 	 */
502 	pktw->pcw_granular = tran->tran_dma_attr.dma_attr_granular;
503 
504 	if (ddi_dma_alloc_handle(tran->tran_hba_dip,
505 	    &tran->tran_dma_attr,
506 	    kmflag == KM_SLEEP ? SLEEP_FUNC: NULL_FUNC, NULL,
507 	    &pkt->pkt_handle) != DDI_SUCCESS) {
508 
509 		return (-1);
510 	}
511 	ptr += tran->tran_hba_len;
512 	if (tran->tran_hba_flags & SCSI_HBA_TRAN_CDB) {
513 		pkt->pkt_cdbp = (opaque_t)ptr;
514 		ptr += DEFAULT_CDBLEN;
515 	}
516 	pkt->pkt_private = NULL;
517 	if (tran->tran_hba_flags & SCSI_HBA_TRAN_SCB)
518 		pkt->pkt_scbp = (opaque_t)ptr;
519 	if (tran->tran_pkt_constructor)
520 		return ((*tran->tran_pkt_constructor)(pkt, arg, kmflag));
521 	else
522 		return (0);
523 }
524 
525 #define	P_TO_TRAN(pkt)	((pkt)->pkt_address.a_hba_tran)
526 
527 void
528 scsi_hba_pkt_destructor(void *buf, void *arg)
529 {
530 	struct scsi_pkt_cache_wrapper *pktw = buf;
531 	struct scsi_pkt		*pkt = &(pktw->pcw_pkt);
532 	scsi_hba_tran_t		*tran = (scsi_hba_tran_t *)arg;
533 
534 	ASSERT(pktw->pcw_magic == PKT_WRAPPER_MAGIC);
535 	ASSERT((pktw->pcw_flags & PCW_BOUND) == 0);
536 	if (tran->tran_pkt_destructor)
537 		(*tran->tran_pkt_destructor)(pkt, arg);
538 
539 	/* make sure nobody messed with our pointers */
540 	ASSERT(pkt->pkt_ha_private == (opaque_t)((char *)pkt +
541 	    sizeof (struct scsi_pkt_cache_wrapper)));
542 	ASSERT(((tran->tran_hba_flags & SCSI_HBA_TRAN_SCB) == 0) ||
543 	    (pkt->pkt_scbp == (opaque_t)((char *)pkt +
544 	    tran->tran_hba_len +
545 	    (((tran->tran_hba_flags & SCSI_HBA_TRAN_CDB) == 0) ?
546 	    0 : DEFAULT_CDBLEN) +
547 	    DEFAULT_PRIVLEN + sizeof (struct scsi_pkt_cache_wrapper))));
548 	ASSERT(((tran->tran_hba_flags & SCSI_HBA_TRAN_CDB) == 0) ||
549 	    (pkt->pkt_cdbp == (opaque_t)((char *)pkt +
550 	    tran->tran_hba_len +
551 	    sizeof (struct scsi_pkt_cache_wrapper))));
552 	ASSERT(pkt->pkt_handle);
553 	ddi_dma_free_handle(&pkt->pkt_handle);
554 	pkt->pkt_handle = NULL;
555 	pkt->pkt_numcookies = 0;
556 	pktw->pcw_total_xfer = 0;
557 	pktw->pcw_totalwin = 0;
558 	pktw->pcw_curwin = 0;
559 }
560 
561 /*
562  * Called by an HBA from _init() to plumb in common SCSA bus_ops and
563  * cb_ops for the HBA's :devctl and :scsi minor nodes.
564  */
565 int
566 scsi_hba_init(struct modlinkage *modlp)
567 {
568 	struct dev_ops *hba_dev_ops;
569 
570 	SCSI_HBA_LOG((_LOG(TRACE, NC), NULL, NULL, "scsi_hba_init"));
571 
572 	/*
573 	 * Get a pointer to the dev_ops structure of the HBA and plumb our
574 	 * bus_ops vector into the HBA's dev_ops structure.
575 	 */
576 	hba_dev_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
577 	ASSERT(hba_dev_ops->devo_bus_ops == NULL);
578 	hba_dev_ops->devo_bus_ops = &scsi_hba_busops;
579 
580 	/*
581 	 * Plumb our cb_ops vector into the HBA's dev_ops structure to
582 	 * provide getinfo and hotplugging ioctl support if the HBA driver
583 	 * does not already provide this support.
584 	 */
585 	if (hba_dev_ops->devo_cb_ops == NULL) {
586 		hba_dev_ops->devo_cb_ops = &scsi_hba_cbops;
587 	}
588 	if (hba_dev_ops->devo_cb_ops->cb_open == scsi_hba_open) {
589 		ASSERT(hba_dev_ops->devo_cb_ops->cb_close == scsi_hba_close);
590 		hba_dev_ops->devo_getinfo = scsi_hba_info;
591 	}
592 	return (0);
593 }
594 
595 /*
596  * Called by an HBA attach(9E) to allocate a scsi_hba_tran structure. An HBA
597  * driver will then initialize the structure and then call
598  * scsi_hba_attach_setup.
599  */
600 /*ARGSUSED*/
601 scsi_hba_tran_t *
602 scsi_hba_tran_alloc(
603 	dev_info_t		*self,
604 	int			flags)
605 {
606 	scsi_hba_tran_t		*tran;
607 
608 	SCSI_HBA_LOG((_LOG(TRACE, NC), self, NULL, "scsi_hba_tran_alloc"));
609 
610 	tran = kmem_zalloc(sizeof (scsi_hba_tran_t),
611 	    (flags & SCSI_HBA_CANSLEEP) ? KM_SLEEP : KM_NOSLEEP);
612 
613 	tran->tran_interconnect_type = INTERCONNECT_PARALLEL;
614 	tran->tran_hba_flags |= SCSI_HBA_TRAN_ALLOC;
615 
616 	return (tran);
617 }
618 
619 /*
620  * Called by an HBA to free a scsi_hba_tran structure
621  */
622 void
623 scsi_hba_tran_free(
624 	scsi_hba_tran_t		*tran)
625 {
626 	SCSI_HBA_LOG((_LOG(TRACE, NC), NULL, NULL, "scsi_hba_tran_free"));
627 
628 	kmem_free(tran, sizeof (scsi_hba_tran_t));
629 }
630 
631 int
632 scsi_tran_ext_alloc(
633 	scsi_hba_tran_t		*tran,
634 	size_t			length,
635 	int			flags)
636 {
637 	void	*tran_ext;
638 	int	ret = DDI_FAILURE;
639 
640 	tran_ext = kmem_zalloc(length,
641 	    (flags & SCSI_HBA_CANSLEEP) ? KM_SLEEP : KM_NOSLEEP);
642 	if (tran_ext != NULL) {
643 		tran->tran_extension = tran_ext;
644 		ret = DDI_SUCCESS;
645 	}
646 	return (ret);
647 }
648 
649 void
650 scsi_tran_ext_free(
651 	scsi_hba_tran_t		*tran,
652 	size_t			length)
653 {
654 	if (tran->tran_extension != NULL) {
655 		kmem_free(tran->tran_extension, length);
656 		tran->tran_extension = NULL;
657 	}
658 }
659 
660 /*
661  * Obsolete: Called by an HBA to attach an instance of the driver
662  * Implement this older interface in terms of the new.
663  */
664 /*ARGSUSED*/
665 int
666 scsi_hba_attach(
667 	dev_info_t		*self,
668 	ddi_dma_lim_t		*hba_lim,
669 	scsi_hba_tran_t		*tran,
670 	int			flags,
671 	void			*hba_options)
672 {
673 	ddi_dma_attr_t		hba_dma_attr;
674 
675 	bzero(&hba_dma_attr, sizeof (ddi_dma_attr_t));
676 
677 	hba_dma_attr.dma_attr_burstsizes = hba_lim->dlim_burstsizes;
678 	hba_dma_attr.dma_attr_minxfer = hba_lim->dlim_minxfer;
679 
680 	return (scsi_hba_attach_setup(self, &hba_dma_attr, tran, flags));
681 }
682 
683 /*
684  * Called by an HBA to attach an instance of the driver.
685  */
686 int
687 scsi_hba_attach_setup(
688 	dev_info_t		*self,
689 	ddi_dma_attr_t		*hba_dma_attr,
690 	scsi_hba_tran_t		*tran,
691 	int			flags)
692 {
693 	struct dev_ops		*hba_dev_ops;
694 	int			id;
695 	int			capable;
696 	static const char	*interconnect[] = INTERCONNECT_TYPE_ASCII;
697 
698 	SCSI_HBA_LOG((_LOG(TRACE, NC), self, NULL, "scsi_hba_attach_setup"));
699 
700 	/*
701 	 * Verify correct scsi_hba_tran_t form:
702 	 *   o	both or none of tran_get_name/tran_get_addr.
703 	 */
704 	if ((tran->tran_get_name == NULL) ^
705 	    (tran->tran_get_bus_addr == NULL)) {
706 		SCSI_HBA_LOG((_LOG(WARN, ATTACH_SETUP), self, NULL,
707 		    "should support both or neither: "
708 		    "tran_get_name, tran_get_bus_addr"));
709 		return (DDI_FAILURE);
710 	}
711 
712 	/*
713 	 * Save all the important HBA information that must be accessed
714 	 * later by scsi_hba_bus_ctl(), and scsi_hba_map().
715 	 */
716 	tran->tran_hba_dip = self;
717 	tran->tran_hba_flags &= SCSI_HBA_TRAN_ALLOC;
718 	tran->tran_hba_flags |= (flags & ~SCSI_HBA_TRAN_ALLOC);
719 
720 	/*
721 	 * Note: we only need dma_attr_minxfer and dma_attr_burstsizes
722 	 * from the DMA attributes. scsi_hba_attach(9f) only
723 	 * guarantees that these two fields are initialized properly.
724 	 * If this changes, be sure to revisit the implementation
725 	 * of scsi_hba_attach(9F).
726 	 */
727 	(void) memcpy(&tran->tran_dma_attr, hba_dma_attr,
728 	    sizeof (ddi_dma_attr_t));
729 
730 	/* create kmem_cache, if needed */
731 	if (tran->tran_setup_pkt) {
732 		char tmp[96];
733 		int hbalen;
734 		int cmdlen = 0;
735 		int statuslen = 0;
736 
737 		ASSERT(tran->tran_init_pkt == NULL);
738 		ASSERT(tran->tran_destroy_pkt == NULL);
739 
740 		tran->tran_init_pkt = scsi_init_cache_pkt;
741 		tran->tran_destroy_pkt = scsi_free_cache_pkt;
742 		tran->tran_sync_pkt = scsi_sync_cache_pkt;
743 		tran->tran_dmafree = scsi_cache_dmafree;
744 
745 		hbalen = ROUNDUP(tran->tran_hba_len);
746 		if (flags & SCSI_HBA_TRAN_CDB)
747 			cmdlen = ROUNDUP(DEFAULT_CDBLEN);
748 		if (flags & SCSI_HBA_TRAN_SCB)
749 			statuslen = ROUNDUP(DEFAULT_SCBLEN);
750 
751 		(void) snprintf(tmp, sizeof (tmp), "pkt_cache_%s_%d",
752 		    ddi_driver_name(self), ddi_get_instance(self));
753 		tran->tran_pkt_cache_ptr = kmem_cache_create(tmp,
754 		    sizeof (struct scsi_pkt_cache_wrapper) +
755 		    hbalen + cmdlen + statuslen, 8,
756 		    scsi_hba_pkt_constructor, scsi_hba_pkt_destructor,
757 		    NULL, tran, NULL, 0);
758 	}
759 
760 	/*
761 	 * If the property does not already exist on self then see if we can
762 	 * pull it from further up the tree and define it on self. If the
763 	 * property does not exist above (including options.conf) then use the
764 	 * default value specified (global variable).
765 	 */
766 #define	CONFIG_INT_PROP(s, p, dv)	{				\
767 	if ((ddi_prop_exists(DDI_DEV_T_ANY, s,				\
768 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, p) == 0) &&		\
769 	    (ndi_prop_update_int(DDI_DEV_T_NONE, s, p,			\
770 	    ddi_prop_get_int(DDI_DEV_T_ANY, ddi_get_parent(s),		\
771 	    DDI_PROP_NOTPROM, p, dv)) != DDI_PROP_SUCCESS))		\
772 		SCSI_HBA_LOG((_LOG(WARN, ATTACH_SETUP), NULL, s,	\
773 		    "cannot create property '%s'", p));			\
774 	}
775 
776 	/*
777 	 * Attach scsi configuration property parameters not already defined
778 	 * via driver.conf to this instance of the HBA using global variable
779 	 * value.  Pulling things down from above us to use
780 	 * "DDI_PROP_NOTPROM | DDI_PROP_DONTPASS" for faster access.
781 	 */
782 	CONFIG_INT_PROP(self, "scsi-options", scsi_options);
783 	CONFIG_INT_PROP(self, "scsi-reset-delay", scsi_reset_delay);
784 	CONFIG_INT_PROP(self, "scsi-tag-age-limit", scsi_tag_age_limit);
785 	CONFIG_INT_PROP(self, "scsi-watchdog-tick", scsi_watchdog_tick);
786 	CONFIG_INT_PROP(self, "scsi-selection-timeout", scsi_selection_timeout);
787 
788 	/*
789 	 * cache the scsi-initiator-id as a property defined further up
790 	 * the tree or defined by OBP on the HBA node so can use
791 	 * "DDI_PROP_NOTPROM | DDI_PROP_DONTPASS" during enumeration.
792 	 * We perform the same type of operation that an HBA driver would
793 	 * use to obtain the 'initiator-id' capability.
794 	 */
795 	id = ddi_prop_get_int(DDI_DEV_T_ANY, self, 0, "initiator-id", -1);
796 	if (id == -1)
797 		id = ddi_prop_get_int(DDI_DEV_T_ANY, self, 0,
798 		    "scsi-initiator-id", -1);
799 	if (id != -1)
800 		CONFIG_INT_PROP(self, "scsi-initiator-id", id);
801 
802 	/* Establish 'initiator-interconnect-type' */
803 	if ((tran->tran_hba_flags & SCSI_HBA_TRAN_ALLOC) &&
804 	    (tran->tran_interconnect_type > 0) &&
805 	    (tran->tran_interconnect_type < INTERCONNECT_MAX)) {
806 		if (ndi_prop_update_string(DDI_DEV_T_NONE, self,
807 		    "initiator-interconnect-type",
808 		    (char *)interconnect[tran->tran_interconnect_type])
809 		    != DDI_PROP_SUCCESS) {
810 			SCSI_HBA_LOG((_LOG(WARN, ATTACH_SETUP), NULL, self,
811 			    "failed to establish "
812 			    "'initiator-interconnect-type'"));
813 			return (DDI_FAILURE);
814 		}
815 	}
816 
817 	/* SCSA iport driver_private (devi_driver_data) points to tran */
818 	ddi_set_driver_private(self, tran);
819 
820 	/*
821 	 * Create :devctl and :scsi minor nodes unless driver supplied its own
822 	 * open/close entry points
823 	 */
824 	hba_dev_ops = ddi_get_driver(self);
825 	ASSERT(hba_dev_ops != NULL);
826 	if (hba_dev_ops == NULL)
827 		return (DDI_FAILURE);
828 	if (hba_dev_ops->devo_cb_ops->cb_open == scsi_hba_open) {
829 		/*
830 		 * Make sure that instance number doesn't overflow
831 		 * when forming minor numbers.
832 		 */
833 		ASSERT(ddi_get_instance(self) <=
834 		    (L_MAXMIN >> INST_MINOR_SHIFT));
835 
836 		if ((ddi_create_minor_node(self, "devctl", S_IFCHR,
837 		    INST2DEVCTL(ddi_get_instance(self)),
838 		    DDI_NT_SCSI_NEXUS, 0) != DDI_SUCCESS) ||
839 		    (ddi_create_minor_node(self, "scsi", S_IFCHR,
840 		    INST2SCSI(ddi_get_instance(self)),
841 		    DDI_NT_SCSI_ATTACHMENT_POINT, 0) != DDI_SUCCESS)) {
842 			ddi_remove_minor_node(self, "devctl");
843 			ddi_remove_minor_node(self, "scsi");
844 			SCSI_HBA_LOG((_LOG(WARN, ATTACH_SETUP), self, NULL,
845 			    "cannot create devctl/scsi minor nodes"));
846 		}
847 	}
848 
849 	/*
850 	 * NOTE: SCSA maintains an 'fm-capable' domain, in tran_fm_capable,
851 	 * that is not dependent (limited by) the capabilities of its parents.
852 	 * For example a dip in a branch that is not DDI_FM_EREPORT_CAPABLE
853 	 * may report as capable, via tran_fm_capable, to its scsi_device
854 	 * children.
855 	 *
856 	 * Get 'fm-capable' property from driver.conf, if present. If not
857 	 * present, default to the scsi_fm_capable global (which has
858 	 * DDI_FM_EREPORT_CAPABLE set by default).
859 	 */
860 	if (tran->tran_fm_capable == DDI_FM_NOT_CAPABLE)
861 		tran->tran_fm_capable = ddi_prop_get_int(DDI_DEV_T_ANY, self,
862 		    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
863 		    "fm-capable", scsi_fm_capable);
864 
865 	/*
866 	 * If an HBA is *not* doing its own fma support by calling
867 	 * ddi_fm_init() prior to scsi_hba_attach_setup(), we provide a
868 	 * minimal common SCSA implementation so that scsi_device children
869 	 * can generate ereports via scsi_fm_ereport_post().  We use
870 	 * ddi_fm_capable() to detect an HBA calling ddi_fm_init() prior to
871 	 * scsi_hba_attach_setup().
872 	 */
873 	if (tran->tran_fm_capable &&
874 	    (ddi_fm_capable(self) == DDI_FM_NOT_CAPABLE)) {
875 		/*
876 		 * We are capable of something, pass our capabilities up
877 		 * the tree, but use a local variable so our parent can't
878 		 * limit our capabilities (we don't want our parent to
879 		 * clear DDI_FM_EREPORT_CAPABLE).
880 		 *
881 		 * NOTE: iblock cookies are not important because scsi
882 		 * HBAs always interrupt below LOCK_LEVEL.
883 		 */
884 		capable = tran->tran_fm_capable;
885 		ddi_fm_init(self, &capable, NULL);
886 
887 		/*
888 		 * Set SCSI_HBA_TRAN_FMSCSA bit to mark us as usiung the
889 		 * common minimal SCSA fm implementation -  we called
890 		 * ddi_fm_init(), so we are responsible for calling
891 		 * ddi_fm_fini() in scsi_hba_detach().
892 		 * NOTE: if ddi_fm_init fails in any reason, SKIP.
893 		 */
894 		if (DEVI(self)->devi_fmhdl)
895 			tran->tran_hba_flags |= SCSI_HBA_TRAN_FMSCSA;
896 	}
897 
898 	return (DDI_SUCCESS);
899 }
900 
901 /*
902  * Called by an HBA to detach an instance of the driver
903  */
904 int
905 scsi_hba_detach(dev_info_t *self)
906 {
907 	struct dev_ops		*hba_dev_ops;
908 	scsi_hba_tran_t		*tran;
909 
910 	SCSI_HBA_LOG((_LOG(TRACE, NC), self, NULL, "scsi_hba_detach"));
911 
912 	tran = ddi_get_driver_private(self);
913 	ASSERT(tran);
914 	if (tran == NULL)
915 		return (DDI_FAILURE);
916 	ASSERT(tran->tran_open_flag == 0);
917 	if (tran->tran_open_flag)
918 		return (DDI_FAILURE);
919 
920 	ddi_set_driver_private(self, NULL);
921 
922 	/*
923 	 * If we are taking care of mininal default fma implementation,
924 	 * call ddi_fm_fini(9F).
925 	 */
926 	if (tran->tran_hba_flags & SCSI_HBA_TRAN_FMSCSA) {
927 		ddi_fm_fini(self);
928 	}
929 
930 	hba_dev_ops = ddi_get_driver(self);
931 	ASSERT(hba_dev_ops != NULL);
932 	if (hba_dev_ops == NULL)
933 		return (DDI_FAILURE);
934 	if (hba_dev_ops->devo_cb_ops->cb_open == scsi_hba_open) {
935 		ddi_remove_minor_node(self, "devctl");
936 		ddi_remove_minor_node(self, "scsi");
937 	}
938 
939 
940 	/*
941 	 * XXX - scsi_transport.h states that these data fields should not be
942 	 * referenced by the HBA. However, to be consistent with
943 	 * scsi_hba_attach(), they are being reset.
944 	 */
945 	tran->tran_hba_dip = (dev_info_t *)NULL;
946 	tran->tran_hba_flags = 0;
947 	(void) memset(&tran->tran_dma_attr, 0, sizeof (ddi_dma_attr_t));
948 
949 	if (tran->tran_pkt_cache_ptr != NULL) {
950 		kmem_cache_destroy(tran->tran_pkt_cache_ptr);
951 		tran->tran_pkt_cache_ptr = NULL;
952 	}
953 
954 	return (DDI_SUCCESS);
955 }
956 
957 /*
958  * Called by an HBA from _fini()
959  */
960 void
961 scsi_hba_fini(struct modlinkage *modlp)
962 {
963 	struct dev_ops *hba_dev_ops;
964 
965 	SCSI_HBA_LOG((_LOG(TRACE, NC), NULL, NULL, "scsi_hba_fini"));
966 
967 	/* Get the devops structure of this module and clear bus_ops vector. */
968 	hba_dev_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
969 
970 	if (hba_dev_ops->devo_cb_ops == &scsi_hba_cbops)
971 		hba_dev_ops->devo_cb_ops = NULL;
972 
973 	if (hba_dev_ops->devo_getinfo == scsi_hba_info)
974 		hba_dev_ops->devo_getinfo = NULL;
975 
976 	hba_dev_ops->devo_bus_ops = (struct bus_ops *)NULL;
977 }
978 
979 static int
980 smp_busctl_reportdev(dev_info_t *child)
981 {
982 	dev_info_t		*self = ddi_get_parent(child);
983 	char			*smp_wwn;
984 
985 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child,
986 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
987 	    SMP_WWN, &smp_wwn) != DDI_SUCCESS) {
988 		return (DDI_FAILURE);
989 	}
990 	cmn_err(CE_CONT,
991 	    "?%s%d at %s%d: wwn %s\n",
992 	    ddi_driver_name(child), ddi_get_instance(child),
993 	    ddi_driver_name(self), ddi_get_instance(self),
994 	    smp_wwn);
995 	ddi_prop_free(smp_wwn);
996 	return (DDI_SUCCESS);
997 }
998 
999 static int
1000 smp_busctl_initchild(dev_info_t *child)
1001 {
1002 	dev_info_t		*self = ddi_get_parent(child);
1003 	scsi_hba_tran_t		*tran = ddi_get_driver_private(self);
1004 	struct smp_device	*smp;
1005 	char			addr[SCSI_MAXNAMELEN];
1006 	dev_info_t		*ndip;
1007 	char			*smp_wwn;
1008 	uint64_t		wwn;
1009 
1010 	ASSERT(tran);
1011 	if (tran == NULL)
1012 		return (DDI_FAILURE);
1013 
1014 	/*
1015 	 * Clone transport structure if requested, so the HBA can maintain
1016 	 * target-specific info, if necessary.
1017 	 *
1018 	 * NOTE: when mpt is converted to SCSI_HBA_ADDR_COMPLEX (and
1019 	 * smp_hba_private is added to struct smp_device) then
1020 	 * smp support of SCSI_HBA_TRAN_CLONE can be removed (i.e.
1021 	 * we can ASSERT that drivers with smp children must be
1022 	 * SCSI_HBA_ADDR_COMPLEX).
1023 	 */
1024 	if (tran->tran_hba_flags & SCSI_HBA_TRAN_CLONE) {
1025 		scsi_hba_tran_t	*clone =
1026 		    kmem_alloc(sizeof (scsi_hba_tran_t), KM_SLEEP);
1027 
1028 		bcopy(tran, clone, sizeof (scsi_hba_tran_t));
1029 		tran = clone;
1030 	}
1031 
1032 	smp = kmem_zalloc(sizeof (struct smp_device), KM_SLEEP);
1033 	smp->dip = child;
1034 	smp->smp_addr.a_hba_tran = tran;
1035 
1036 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child,
1037 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1038 	    SMP_WWN, &smp_wwn) != DDI_SUCCESS) {
1039 		return (DDI_FAILURE);
1040 	}
1041 
1042 	if (ddi_devid_str_to_wwn(smp_wwn, &wwn)) {
1043 		goto failure;
1044 	}
1045 
1046 	bcopy(&wwn, smp->smp_addr.a_wwn, SAS_WWN_BYTE_SIZE);
1047 	(void) snprintf(addr, SCSI_MAXNAMELEN, "w%s", smp_wwn);
1048 
1049 	/* Prevent duplicate nodes.  */
1050 	ndip = ndi_devi_find(self, ddi_node_name(child), addr);
1051 	if (ndip && (ndip != child)) {
1052 		goto failure;
1053 	}
1054 
1055 	ddi_set_name_addr(child, addr);
1056 	ddi_set_driver_private(child, smp);
1057 	ddi_prop_free(smp_wwn);
1058 	return (DDI_SUCCESS);
1059 
1060 failure:
1061 	kmem_free(smp, sizeof (struct smp_device));
1062 	if (tran->tran_hba_flags & SCSI_HBA_TRAN_CLONE) {
1063 		kmem_free(tran, sizeof (scsi_hba_tran_t));
1064 	}
1065 	ddi_prop_free(smp_wwn);
1066 	return (DDI_FAILURE);
1067 }
1068 
1069 static int
1070 smp_busctl_uninitchild(dev_info_t *child)
1071 {
1072 	dev_info_t		*self = ddi_get_parent(child);
1073 	struct smp_device	*smp = ddi_get_driver_private(child);
1074 	scsi_hba_tran_t		*tran = ddi_get_driver_private(self);
1075 
1076 	ASSERT(smp && tran);
1077 	if ((smp == NULL) || (tran == NULL))
1078 		return (DDI_FAILURE);
1079 
1080 	if (tran->tran_hba_flags & SCSI_HBA_TRAN_CLONE) {
1081 		tran = smp->smp_addr.a_hba_tran;
1082 		kmem_free(tran, sizeof (scsi_hba_tran_t));
1083 	}
1084 	kmem_free(smp, sizeof (*smp));
1085 
1086 	ddi_set_driver_private(child, NULL);
1087 	ddi_set_name_addr(child, NULL);
1088 	return (DDI_SUCCESS);
1089 }
1090 
1091 /*
1092  * Wrapper to scsi_get_name which takes a devinfo argument instead of a
1093  * scsi_device structure.
1094  */
1095 static int
1096 scsi_hba_name_child(dev_info_t *child, char *addr, int maxlen)
1097 {
1098 	struct scsi_device	*sd = ddi_get_driver_private(child);
1099 
1100 	/* nodes are named by tran_get_name or default "tgt,lun" */
1101 	if (sd && (scsi_get_name(sd, addr, maxlen) == 1))
1102 		return (DDI_SUCCESS);
1103 
1104 	return (DDI_FAILURE);
1105 }
1106 
1107 static int
1108 scsi_busctl_reportdev(dev_info_t *child)
1109 {
1110 	dev_info_t		*self = ddi_get_parent(child);
1111 	scsi_hba_tran_t		*tran = ddi_get_driver_private(self);
1112 	struct scsi_device	*sd = ddi_get_driver_private(child);
1113 	char			ua[SCSI_MAXNAMELEN];
1114 	char			ba[SCSI_MAXNAMELEN];
1115 
1116 	SCSI_HBA_LOG((_LOG(TRACE, BUS_CTL), NULL, child,
1117 	    "scsi_hba_bus_ctl REPORTDEV"));
1118 
1119 	ASSERT(tran && sd);
1120 	if ((tran == NULL) || (sd == NULL))
1121 		return (DDI_FAILURE);
1122 
1123 	/* get the unit_address and bus_addr information */
1124 	if ((scsi_get_name(sd, ua, sizeof (ua)) == 0) ||
1125 	    (scsi_get_bus_addr(sd, ba, sizeof (ba)) == 0)) {
1126 		SCSI_HBA_LOG((_LOG(DIAG1, BUS_CTL),
1127 		    NULL, child, "REPORTDEV failure"));
1128 		return (DDI_FAILURE);
1129 	}
1130 
1131 	if (tran->tran_get_name == NULL)
1132 		SCSI_HBA_LOG((_LOG(CONT, NC), NULL, NULL,
1133 		    "?%s%d at %s%d: %s",
1134 		    ddi_driver_name(child), ddi_get_instance(child),
1135 		    ddi_driver_name(self), ddi_get_instance(self), ba));
1136 	else
1137 		SCSI_HBA_LOG((_LOG(CONT, NC), NULL, NULL,
1138 		    "?%s%d at %s%d: name %s, bus address %s",
1139 		    ddi_driver_name(child), ddi_get_instance(child),
1140 		    ddi_driver_name(self), ddi_get_instance(self),
1141 		    ua, ba));
1142 	return (DDI_SUCCESS);
1143 }
1144 
1145 /*
1146  * scsi_busctl_initchild is called to initialize the SCSA transport for
1147  * communication with a particular child scsi target device. Successful
1148  * initialization requires properties on the node which describe the address
1149  * of the target device. If the address of the target device can't be
1150  * determined from properties then DDI_NOT_WELL_FORMED is returned. Nodes that
1151  * are DDI_NOT_WELL_FORMED are considered an implementation artifact.
1152  * The child may be one of the following types of devinfo nodes:
1153  *
1154  * OBP node:
1155  *	OBP does not enumerate target devices attached a SCSI bus. These
1156  *	template/stub/wildcard nodes are a legacy artifact for support of old
1157  *	driver loading methods. Since they have no properties,
1158  *	DDI_NOT_WELL_FORMED will be returned.
1159  *
1160  * SID node:
1161  *	The node may be either a:
1162  *	    o	probe/barrier SID node
1163  *	    o	a dynamic SID target node
1164  *	    o	a dynamic SID mscsi node
1165  *
1166  * driver.conf node: The situation for this nexus is different than most.
1167  *	Typically a driver.conf node definition is used to either define a
1168  *	new child devinfo node or to further decorate (via merge) a SID
1169  *	child with properties. In our case we use the nodes for *both*
1170  *	purposes.
1171  *
1172  * In both the SID node and driver.conf node cases we must form the nodes
1173  * "@addr" from the well-known scsi(9P) device unit-address properties on
1174  * the node.
1175  *
1176  * For HBA drivers that implement the deprecated tran_get_name interface,
1177  * "@addr" construction involves having that driver interpret properties via
1178  * scsi_hba_name_child -> scsi_get_name -> tran_get_name: there is no
1179  * requiremnt for the property names to be well-known.
1180  */
1181 static int
1182 scsi_busctl_initchild(dev_info_t *child)
1183 {
1184 	dev_info_t		*self = ddi_get_parent(child);
1185 	dev_info_t		*dup;
1186 	scsi_hba_tran_t		*tran;
1187 	struct scsi_device	*sd;
1188 	scsi_hba_tran_t		*tran_clone;
1189 	int			tgt = 0;
1190 	int			lun = 0;
1191 	int			sfunc = 0;
1192 	int			err = DDI_FAILURE;
1193 	char			addr[SCSI_MAXNAMELEN];
1194 
1195 	ASSERT(DEVI_BUSY_OWNED(self));
1196 	SCSI_HBA_LOG((_LOG(DIAG4, DEVICE_INITCHILD),
1197 	    NULL, child, "begin initchild"));
1198 
1199 	/*
1200 	 * For a driver like fp with multiple upper-layer-protocols
1201 	 * it is possible for scsi_hba_init in _init to plumb SCSA
1202 	 * and have the load of fcp (which does scsi_hba_attach_setup)
1203 	 * to fail.  In this case we may get here with a NULL hba.
1204 	 */
1205 	tran = ddi_get_driver_private(self);
1206 	if (tran == NULL)
1207 		return (DDI_NOT_WELL_FORMED);
1208 
1209 	/*
1210 	 * OBP may create template/stub/wildcard nodes for legacy driver
1211 	 * loading methods. These nodes have no properties, so we lack the
1212 	 * addressing properties to initchild them. Hide the node and return
1213 	 * DDI_NOT_WELL_FORMED.
1214 	 *
1215 	 * XXX need ndi_devi_has_properties(dip) type interface?
1216 	 *
1217 	 * XXX It would be nice if we could delete these ill formed nodes by
1218 	 * implementing a DDI_NOT_WELL_FORMED_DELETE return code. This can't
1219 	 * be done until leadville debug code removes its dependencies
1220 	 * on the devinfo still being present after a failed ndi_devi_online.
1221 	 */
1222 	if ((DEVI(child)->devi_hw_prop_ptr == NULL) &&
1223 	    (DEVI(child)->devi_drv_prop_ptr == NULL) &&
1224 	    (DEVI(child)->devi_sys_prop_ptr == NULL)) {
1225 		SCSI_HBA_LOG((_LOG(DIAG4, DEVICE_INITCHILD),
1226 		    NULL, child, "no properties"));
1227 		return (DDI_NOT_WELL_FORMED);
1228 	}
1229 
1230 	/* get legacy SPI addressing properties */
1231 	sfunc = ddi_prop_get_int(DDI_DEV_T_ANY, child,
1232 	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, SCSI_ADDR_PROP_SFUNC, -1);
1233 	lun = ddi_prop_get_int(DDI_DEV_T_ANY, child,
1234 	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0);
1235 	if ((tgt = ddi_prop_get_int(DDI_DEV_T_ANY, child,
1236 	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
1237 	    SCSI_ADDR_PROP_TARGET, -1)) == -1) {
1238 		tgt = 0;
1239 		/*
1240 		 * A driver.conf node for merging always has a target= property,
1241 		 * even if it is just a dummy that does not contain the real
1242 		 * target address. However drivers that register devids may
1243 		 * create stub driver.conf nodes without a target= property so
1244 		 * that pathological devid resolution works.
1245 		 */
1246 		if (ndi_dev_is_persistent_node(child) == 0) {
1247 			SCSI_HBA_LOG((_LOG(DIAG4, DEVICE_INITCHILD),
1248 			    NULL, child, "stub driver.conf node"));
1249 			return (DDI_NOT_WELL_FORMED);
1250 		}
1251 	}
1252 
1253 	/*
1254 	 * The scsi_address structure may not specify all the addressing
1255 	 * information. For an old HBA that doesn't support tran_get_name
1256 	 * (most pre-SCSI-3 HBAs) the scsi_address structure is still used,
1257 	 * so the target property must exist and the LUN must be < 256.
1258 	 */
1259 	if ((tran->tran_get_name == NULL) &&
1260 	    ((tgt >= USHRT_MAX) || (lun >= 256))) {
1261 		SCSI_HBA_LOG((_LOG(DIAG1, DEVICE_INITCHILD),
1262 		    NULL, child, "illegal or missing addressing properties"));
1263 		return (DDI_NOT_WELL_FORMED);
1264 	}
1265 
1266 	/*
1267 	 * We need to initialize a fair amount of our environment to invoke
1268 	 * tran_get_name (via scsi_hba_name_child and scsi_get_name) to
1269 	 * produce the "@addr" name from addressing properties. Allocate and
1270 	 * initialize scsi device structure.
1271 	 */
1272 	sd = kmem_zalloc(sizeof (struct scsi_device), KM_SLEEP);
1273 	mutex_init(&sd->sd_mutex, NULL, MUTEX_DRIVER, NULL);
1274 	sd->sd_dev = child;
1275 	sd->sd_pathinfo = NULL;
1276 	ddi_set_driver_private(child, sd);
1277 
1278 	if (tran->tran_hba_flags & SCSI_HBA_ADDR_COMPLEX) {
1279 		/*
1280 		 * For a SCSI_HBA_ADDR_COMPLEX transport we store a pointer to
1281 		 * scsi_device in the scsi_address structure.  This allows an
1282 		 * HBA driver to find its per-scsi_device private data
1283 		 * (accessable to the HBA given just the scsi_address by using
1284 		 *  scsi_address_device(9F)/scsi_device_hba_private_get(9F)).
1285 		 */
1286 		sd->sd_address.a.a_sd = sd;
1287 		tran_clone = NULL;
1288 	} else {
1289 		/*
1290 		 * Initialize the scsi_address so that a SCSI-2 target driver
1291 		 * talking to a SCSI-2 device on a SCSI-3 bus (spi) continues
1292 		 * to work. We skew the secondary function value so that we
1293 		 * can tell from the address structure if we are processing
1294 		 * a secondary function request.
1295 		 */
1296 		sd->sd_address.a_target = (ushort_t)tgt;
1297 		sd->sd_address.a_lun = (uchar_t)lun;
1298 		if (sfunc == -1)
1299 			sd->sd_address.a_sublun = (uchar_t)0;
1300 		else
1301 			sd->sd_address.a_sublun = (uchar_t)sfunc + 1;
1302 
1303 		/*
1304 		 * XXX TODO: apply target/lun limitation logic for SPI
1305 		 * binding_set. If spi this is based on scsi_options WIDE
1306 		 * NLUNS some forms of lun limitation are based on the
1307 		 * device @lun 0
1308 		 */
1309 
1310 		/*
1311 		 * Deprecated: Use SCSI_HBA_ADDR_COMPLEX:
1312 		 *   Clone transport structure if requested. Cloning allows
1313 		 *   an HBA to maintain target-specific information if
1314 		 *   necessary, such as target addressing information that
1315 		 *   does not adhere to the scsi_address structure format.
1316 		 */
1317 		if (tran->tran_hba_flags & SCSI_HBA_TRAN_CLONE) {
1318 			tran_clone = kmem_alloc(
1319 			    sizeof (scsi_hba_tran_t), KM_SLEEP);
1320 			bcopy((caddr_t)tran,
1321 			    (caddr_t)tran_clone, sizeof (scsi_hba_tran_t));
1322 			tran = tran_clone;
1323 			tran->tran_sd = sd;
1324 		} else {
1325 			tran_clone = NULL;
1326 			ASSERT(tran->tran_sd == NULL);
1327 		}
1328 	}
1329 
1330 	/* establish scsi_address pointer to the HBA's tran structure */
1331 	sd->sd_address.a_hba_tran = tran;
1332 
1333 	/*
1334 	 * This is a grotty hack that allows direct-access (non-scsa) drivers
1335 	 * (like chs, ata, and mlx which all make cmdk children) to put its
1336 	 * own vector in the 'a_hba_tran' field. When all the drivers that do
1337 	 * this are fixed, please remove this hack.
1338 	 *
1339 	 * NOTE: This hack is also shows up in the DEVP_TO_TRAN implementation
1340 	 * in scsi_confsubr.c.
1341 	 */
1342 	sd->sd_tran_safe = tran;
1343 
1344 	/* Establish the @addr name of the child. */
1345 	*addr = '\0';
1346 	if (scsi_hba_name_child(child, addr, SCSI_MAXNAMELEN) != DDI_SUCCESS) {
1347 		/*
1348 		 * Some driver.conf files add bogus target properties (relative
1349 		 * to their nexus representation of target) to their stub
1350 		 * nodes, causing the check above to not filter them.
1351 		 */
1352 		SCSI_HBA_LOG((_LOG(DIAG3, DEVICE_INITCHILD),
1353 		    NULL, child, "failed name_child"));
1354 		err = DDI_NOT_WELL_FORMED;
1355 		goto failure;
1356 	}
1357 	if (*addr == '\0') {
1358 		SCSI_HBA_LOG((_LOG(DIAG2, DEVICE_INITCHILD),
1359 		    NULL, child, "failed to establish @addr"));
1360 		err = DDI_NOT_WELL_FORMED;
1361 		goto failure;
1362 	}
1363 
1364 	/* set the node @addr string */
1365 	ddi_set_name_addr(child, addr);
1366 
1367 	/* prevent sibling duplicates */
1368 	dup = ndi_devi_find(self, ddi_node_name(child), addr);
1369 	if (dup && (dup != child)) {
1370 		SCSI_HBA_LOG((_LOG(DIAG4, DEVICE_INITCHILD),
1371 		    NULL, child, "@%s duplicate %p", addr, (void *)dup));
1372 		goto failure;
1373 	}
1374 
1375 	/* call HBA's target init entry point if it exists */
1376 	if (tran->tran_tgt_init != NULL) {
1377 		if ((*tran->tran_tgt_init)
1378 		    (self, child, tran, sd) != DDI_SUCCESS) {
1379 			SCSI_HBA_LOG((_LOG(DIAG2, DEVICE_INITCHILD),
1380 			    NULL, child, "@%s failed tran_tgt_init", addr));
1381 			goto failure;
1382 		}
1383 	}
1384 
1385 	SCSI_HBA_LOG((_LOG(DIAG3, DEVICE_INITCHILD),
1386 	    NULL, child, "@%s ok", addr));
1387 	return (DDI_SUCCESS);
1388 
1389 failure:
1390 	if (tran_clone)
1391 		kmem_free(tran_clone, sizeof (scsi_hba_tran_t));
1392 	mutex_destroy(&sd->sd_mutex);
1393 	kmem_free(sd, sizeof (*sd));
1394 	ddi_set_driver_private(child, NULL);
1395 	ddi_set_name_addr(child, NULL);
1396 
1397 	return (err);		/* remove the node */
1398 }
1399 
1400 static int
1401 scsi_busctl_uninitchild(dev_info_t *child)
1402 {
1403 	dev_info_t		*self = ddi_get_parent(child);
1404 	scsi_hba_tran_t		*tran = ddi_get_driver_private(self);
1405 	struct scsi_device	*sd = ddi_get_driver_private(child);
1406 	scsi_hba_tran_t		*tran_clone;
1407 
1408 	ASSERT(DEVI_BUSY_OWNED(self));
1409 
1410 	ASSERT(tran && sd);
1411 	if ((tran == NULL) || (sd == NULL))
1412 		return (DDI_FAILURE);
1413 
1414 	SCSI_HBA_LOG((_LOG(DIAG3, DEVICE_UNINITCHILD),
1415 	    NULL, child, "uninitchild %d %s@%s", i_ddi_node_state(child),
1416 	    ddi_node_name(child),
1417 	    ddi_get_name_addr(child) ? ddi_get_name_addr(child) : "XXX"));
1418 
1419 	if (tran->tran_hba_flags & SCSI_HBA_TRAN_CLONE) {
1420 		tran_clone = sd->sd_address.a_hba_tran;
1421 
1422 		/* ... grotty hack, involving sd_tran_safe, continued. */
1423 		if (tran_clone != sd->sd_tran_safe) {
1424 			tran_clone = sd->sd_tran_safe;
1425 #ifdef	DEBUG
1426 			/*
1427 			 * Complain so things get fixed and hack can, at
1428 			 * some point in time, be removed.
1429 			 */
1430 			cmn_err(CE_WARN, "scsi_busctl_uninitchild: '%s' is "
1431 			    "corrupting a_hba_tran",
1432 			    sd->sd_dev ? ddi_driver_name(sd->sd_dev) :
1433 			    "unknown_driver");
1434 #endif	/* DEBUG */
1435 		}
1436 
1437 		ASSERT(tran_clone->tran_hba_flags & SCSI_HBA_TRAN_CLONE);
1438 		ASSERT(tran_clone->tran_sd == sd);
1439 		tran = tran_clone;
1440 	} else {
1441 		tran_clone = NULL;
1442 		ASSERT(tran->tran_sd == NULL);
1443 	}
1444 
1445 	/*
1446 	 * To simplify host adapter drivers we guarantee that multiple
1447 	 * tran_tgt_init(9E) calls of the same unit address are never
1448 	 * active at the same time.
1449 	 */
1450 	if (tran->tran_tgt_free)
1451 		(*tran->tran_tgt_free) (self, child, tran, sd);
1452 
1453 	/*
1454 	 * If a inquiry data is still allocated (by scsi_probe()) we
1455 	 * free the allocation here. This keeps scsi_inq valid for the
1456 	 * same duration as the corresponding inquiry properties. It
1457 	 * also allows a tran_tgt_init() implementation that establishes
1458 	 * sd_inq (common/io/dktp/controller/ata/ata_disk.c) to deal
1459 	 * with deallocation in its tran_tgt_free (setting sd_inq back
1460 	 * to NULL) without upsetting the framework.
1461 	 */
1462 	if (sd->sd_inq) {
1463 		kmem_free(sd->sd_inq, SUN_INQSIZE);
1464 		sd->sd_inq = (struct scsi_inquiry *)NULL;
1465 	}
1466 
1467 	mutex_destroy(&sd->sd_mutex);
1468 	if (tran_clone)
1469 		kmem_free(tran_clone, sizeof (scsi_hba_tran_t));
1470 	kmem_free(sd, sizeof (*sd));
1471 
1472 	ddi_set_driver_private(child, NULL);
1473 	SCSI_HBA_LOG((_LOG(DIAG3, DEVICE_UNINITCHILD),
1474 	    NULL, child, "complete"));
1475 	ddi_set_name_addr(child, NULL);
1476 	return (DDI_SUCCESS);
1477 }
1478 
1479 /*
1480  * Generic bus_ctl operations for SCSI HBA's,
1481  * hiding the busctl interface from the HBA.
1482  */
1483 /*ARGSUSED*/
1484 static int
1485 scsi_hba_bus_ctl(
1486 	dev_info_t		*self,
1487 	dev_info_t		*child,
1488 	ddi_ctl_enum_t		op,
1489 	void			*arg,
1490 	void			*result)
1491 {
1492 	int			child_flavor_smp = 0;
1493 	int			val;
1494 	ddi_dma_attr_t		*attr;
1495 	scsi_hba_tran_t		*tran;
1496 
1497 	/* For some ops, child is 'arg'. */
1498 	if ((op == DDI_CTLOPS_INITCHILD) || (op == DDI_CTLOPS_UNINITCHILD))
1499 		child = (dev_info_t *)arg;
1500 
1501 	/* Determine the flavor of the child: smp .vs. scsi */
1502 	if (ddi_prop_exists(DDI_DEV_T_ANY, child,
1503 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, SMP_PROP))
1504 		child_flavor_smp = 1;
1505 
1506 	switch (op) {
1507 	case DDI_CTLOPS_INITCHILD:
1508 		if (child_flavor_smp)
1509 			return (smp_busctl_initchild(child));
1510 		else
1511 			return (scsi_busctl_initchild(child));
1512 
1513 	case DDI_CTLOPS_UNINITCHILD:
1514 		if (child_flavor_smp)
1515 			return (smp_busctl_uninitchild(child));
1516 		else
1517 			return (scsi_busctl_uninitchild(child));
1518 
1519 	case DDI_CTLOPS_REPORTDEV:
1520 		if (child_flavor_smp)
1521 			return (smp_busctl_reportdev(child));
1522 		else
1523 			return (scsi_busctl_reportdev(child));
1524 
1525 	case DDI_CTLOPS_IOMIN:
1526 		tran = ddi_get_driver_private(self);
1527 		ASSERT(tran);
1528 		if (tran == NULL)
1529 			return (DDI_FAILURE);
1530 
1531 		/*
1532 		 * The 'arg' value of nonzero indicates 'streaming'
1533 		 * mode. If in streaming mode, pick the largest
1534 		 * of our burstsizes available and say that that
1535 		 * is our minimum value (modulo what minxfer is).
1536 		 */
1537 		attr = &tran->tran_dma_attr;
1538 		val = *((int *)result);
1539 		val = maxbit(val, attr->dma_attr_minxfer);
1540 		*((int *)result) = maxbit(val, ((intptr_t)arg ?
1541 		    (1<<ddi_ffs(attr->dma_attr_burstsizes)-1) :
1542 		    (1<<(ddi_fls(attr->dma_attr_burstsizes)-1))));
1543 
1544 		return (ddi_ctlops(self, child, op, arg, result));
1545 
1546 	case DDI_CTLOPS_SIDDEV:
1547 		return (ndi_dev_is_persistent_node(child) ?
1548 		    DDI_SUCCESS : DDI_FAILURE);
1549 
1550 	/* XXX these should be handled */
1551 	case DDI_CTLOPS_POWER:
1552 	case DDI_CTLOPS_ATTACH:		/* DDI_PRE / DDI_POST attach */
1553 	case DDI_CTLOPS_DETACH:		/* DDI_PRE / DDI_POST detach */
1554 		return (DDI_SUCCESS);
1555 
1556 	/*
1557 	 * These ops correspond to functions that "shouldn't" be called
1558 	 * by a SCSI target driver. So we whine when we're called.
1559 	 */
1560 	case DDI_CTLOPS_DMAPMAPC:
1561 	case DDI_CTLOPS_REPORTINT:
1562 	case DDI_CTLOPS_REGSIZE:
1563 	case DDI_CTLOPS_NREGS:
1564 	case DDI_CTLOPS_SLAVEONLY:
1565 	case DDI_CTLOPS_AFFINITY:
1566 	case DDI_CTLOPS_POKE:
1567 	case DDI_CTLOPS_PEEK:
1568 		SCSI_HBA_LOG((_LOG(WARN, BUS_CTL), NULL, child,
1569 		    "invalid op (%d)", op));
1570 		return (DDI_FAILURE);
1571 
1572 	/* Everything else we pass up */
1573 	case DDI_CTLOPS_PTOB:
1574 	case DDI_CTLOPS_BTOP:
1575 	case DDI_CTLOPS_BTOPR:
1576 	case DDI_CTLOPS_DVMAPAGESIZE:
1577 	default:
1578 		return (ddi_ctlops(self, child, op, arg, result));
1579 	}
1580 }
1581 
1582 /*
1583  * Private wrapper for scsi_pkt's allocated via scsi_hba_pkt_alloc()
1584  */
1585 struct scsi_pkt_wrapper {
1586 	struct scsi_pkt		scsi_pkt;
1587 	int			pkt_wrapper_magic;
1588 	int			pkt_wrapper_len;
1589 };
1590 
1591 #if !defined(lint)
1592 _NOTE(SCHEME_PROTECTS_DATA("unique per thread", scsi_pkt_wrapper))
1593 _NOTE(SCHEME_PROTECTS_DATA("Unshared Data", dev_ops))
1594 #endif
1595 
1596 /*
1597  * Called by an HBA to allocate a scsi_pkt
1598  */
1599 /*ARGSUSED*/
1600 struct scsi_pkt *
1601 scsi_hba_pkt_alloc(
1602 	dev_info_t		*dip,
1603 	struct scsi_address	*ap,
1604 	int			cmdlen,
1605 	int			statuslen,
1606 	int			tgtlen,
1607 	int			hbalen,
1608 	int			(*callback)(caddr_t arg),
1609 	caddr_t			arg)
1610 {
1611 	struct scsi_pkt		*pkt;
1612 	struct scsi_pkt_wrapper	*hba_pkt;
1613 	caddr_t			p;
1614 	int			acmdlen, astatuslen, atgtlen, ahbalen;
1615 	int			pktlen;
1616 
1617 	/* Sanity check */
1618 	if (callback != SLEEP_FUNC && callback != NULL_FUNC)
1619 		SCSI_HBA_LOG((_LOG(WARN, PKT_ALLOC), dip, NULL,
1620 		    "callback must be either SLEEP_FUNC or NULL_FUNC"));
1621 
1622 	/*
1623 	 * Round up so everything gets allocated on long-word boundaries
1624 	 */
1625 	acmdlen = ROUNDUP(cmdlen);
1626 	astatuslen = ROUNDUP(statuslen);
1627 	atgtlen = ROUNDUP(tgtlen);
1628 	ahbalen = ROUNDUP(hbalen);
1629 	pktlen = sizeof (struct scsi_pkt_wrapper) +
1630 	    acmdlen + astatuslen + atgtlen + ahbalen;
1631 
1632 	hba_pkt = kmem_zalloc(pktlen,
1633 	    (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP);
1634 	if (hba_pkt == NULL) {
1635 		ASSERT(callback == NULL_FUNC);
1636 		return (NULL);
1637 	}
1638 
1639 	/*
1640 	 * Set up our private info on this pkt
1641 	 */
1642 	hba_pkt->pkt_wrapper_len = pktlen;
1643 	hba_pkt->pkt_wrapper_magic = PKT_WRAPPER_MAGIC;	/* alloced correctly */
1644 	pkt = &hba_pkt->scsi_pkt;
1645 
1646 	/*
1647 	 * Set up pointers to private data areas, cdb, and status.
1648 	 */
1649 	p = (caddr_t)(hba_pkt + 1);
1650 	if (hbalen > 0) {
1651 		pkt->pkt_ha_private = (opaque_t)p;
1652 		p += ahbalen;
1653 	}
1654 	if (tgtlen > 0) {
1655 		pkt->pkt_private = (opaque_t)p;
1656 		p += atgtlen;
1657 	}
1658 	if (statuslen > 0) {
1659 		pkt->pkt_scbp = (uchar_t *)p;
1660 		p += astatuslen;
1661 	}
1662 	if (cmdlen > 0) {
1663 		pkt->pkt_cdbp = (uchar_t *)p;
1664 	}
1665 
1666 	/*
1667 	 * Initialize the pkt's scsi_address
1668 	 */
1669 	pkt->pkt_address = *ap;
1670 
1671 	/*
1672 	 * NB: It may not be safe for drivers, esp target drivers, to depend
1673 	 * on the following fields being set until all the scsi_pkt
1674 	 * allocation violations discussed in scsi_pkt.h are all resolved.
1675 	 */
1676 	pkt->pkt_cdblen = cmdlen;
1677 	pkt->pkt_tgtlen = tgtlen;
1678 	pkt->pkt_scblen = statuslen;
1679 
1680 	return (pkt);
1681 }
1682 
1683 /*
1684  * Called by an HBA to free a scsi_pkt
1685  */
1686 /*ARGSUSED*/
1687 void
1688 scsi_hba_pkt_free(
1689 	struct scsi_address	*ap,
1690 	struct scsi_pkt		*pkt)
1691 {
1692 	kmem_free(pkt, ((struct scsi_pkt_wrapper *)pkt)->pkt_wrapper_len);
1693 }
1694 
1695 /*
1696  * Return 1 if the scsi_pkt used a proper allocator.
1697  *
1698  * The DDI does not allow a driver to allocate it's own scsi_pkt(9S), a
1699  * driver should not have *any* compiled in dependencies on "sizeof (struct
1700  * scsi_pkt)". While this has been the case for many years, a number of
1701  * drivers have still not been fixed. This function can be used to detect
1702  * improperly allocated scsi_pkt structures, and produce messages identifying
1703  * drivers that need to be fixed.
1704  *
1705  * While drivers in violation are being fixed, this function can also
1706  * be used by the framework to detect packets that violated allocation
1707  * rules.
1708  *
1709  * NB: It is possible, but very unlikely, for this code to return a false
1710  * positive (finding correct magic, but for wrong reasons).  Careful
1711  * consideration is needed for callers using this interface to condition
1712  * access to newer scsi_pkt fields (those after pkt_reason).
1713  *
1714  * NB: As an aid to minimizing the amount of work involved in 'fixing' legacy
1715  * drivers that violate scsi_*(9S) allocation rules, private
1716  * scsi_pkt_size()/scsi_size_clean() functions are available (see their
1717  * implementation for details).
1718  *
1719  * *** Non-legacy use of scsi_pkt_size() is discouraged. ***
1720  *
1721  * NB: When supporting broken HBA drivers is not longer a concern, this
1722  * code should be removed.
1723  */
1724 int
1725 scsi_pkt_allocated_correctly(struct scsi_pkt *pkt)
1726 {
1727 	struct scsi_pkt_wrapper	*hba_pkt = (struct scsi_pkt_wrapper *)pkt;
1728 	int	magic;
1729 	major_t	major;
1730 #ifdef	DEBUG
1731 	int	*pspwm, *pspcwm;
1732 
1733 	/*
1734 	 * We are getting scsi packets from two 'correct' wrapper schemes,
1735 	 * make sure we are looking at the same place in both to detect
1736 	 * proper allocation.
1737 	 */
1738 	pspwm = &((struct scsi_pkt_wrapper *)0)->pkt_wrapper_magic;
1739 	pspcwm = &((struct scsi_pkt_cache_wrapper *)0)->pcw_magic;
1740 	ASSERT(pspwm == pspcwm);
1741 #endif	/* DEBUG */
1742 
1743 
1744 	/*
1745 	 * Check to see if driver is scsi_size_clean(), assume it
1746 	 * is using the scsi_pkt_size() interface everywhere it needs to
1747 	 * if the driver indicates it is scsi_size_clean().
1748 	 */
1749 	major = ddi_driver_major(P_TO_TRAN(pkt)->tran_hba_dip);
1750 	if (devnamesp[major].dn_flags & DN_SCSI_SIZE_CLEAN)
1751 		return (1);		/* ok */
1752 
1753 	/*
1754 	 * Special case crossing a page boundary. If the scsi_pkt was not
1755 	 * allocated correctly, then accross a page boundary we have a
1756 	 * fault hazzard.
1757 	 */
1758 	if ((((uintptr_t)(&hba_pkt->scsi_pkt)) & MMU_PAGEMASK) ==
1759 	    (((uintptr_t)(&hba_pkt->pkt_wrapper_magic)) & MMU_PAGEMASK)) {
1760 		/* fastpath, no cross-page hazzard */
1761 		magic = hba_pkt->pkt_wrapper_magic;
1762 	} else {
1763 		/* add protection for cross-page hazzard */
1764 		if (ddi_peek32((dev_info_t *)NULL,
1765 		    &hba_pkt->pkt_wrapper_magic, &magic) == DDI_FAILURE) {
1766 			return (0);	/* violation */
1767 		}
1768 	}
1769 
1770 	/* properly allocated packet always has correct magic */
1771 	return ((magic == PKT_WRAPPER_MAGIC) ? 1 : 0);
1772 }
1773 
1774 /*
1775  * Private interfaces to simplify conversion of legacy drivers so they don't
1776  * depend on scsi_*(9S) size. Instead of using these private interface, HBA
1777  * drivers should use DDI sanctioned allocation methods:
1778  *
1779  *	scsi_pkt	Use scsi_hba_pkt_alloc(9F), or implement
1780  *			tran_setup_pkt(9E).
1781  *
1782  *	scsi_device	You are doing something strange/special, a scsi_device
1783  *			structure should only be allocated by scsi_hba.c
1784  *			initchild code or scsi_vhci.c code.
1785  *
1786  *	scsi_hba_tran	Use scsi_hba_tran_alloc(9F).
1787  */
1788 size_t
1789 scsi_pkt_size()
1790 {
1791 	return (sizeof (struct scsi_pkt));
1792 }
1793 
1794 size_t
1795 scsi_hba_tran_size()
1796 {
1797 	return (sizeof (scsi_hba_tran_t));
1798 }
1799 
1800 size_t
1801 scsi_device_size()
1802 {
1803 	return (sizeof (struct scsi_device));
1804 }
1805 
1806 /*
1807  * Legacy compliance to scsi_pkt(9S) allocation rules through use of
1808  * scsi_pkt_size() is detected by the 'scsi-size-clean' driver.conf property
1809  * or an HBA driver calling to scsi_size_clean() from attach(9E).  A driver
1810  * developer should only indicate that a legacy driver is clean after using
1811  * SCSI_SIZE_CLEAN_VERIFY to ensure compliance (see scsi_pkt.h).
1812  */
1813 void
1814 scsi_size_clean(dev_info_t *dip)
1815 {
1816 	major_t		major;
1817 	struct devnames	*dnp;
1818 
1819 	ASSERT(dip);
1820 	major = ddi_driver_major(dip);
1821 	ASSERT(major < devcnt);
1822 	if (major >= devcnt) {
1823 		cmn_err(CE_WARN, "scsi_pkt_size: bogus major: %d", major);
1824 		return;
1825 	}
1826 
1827 	/* Set DN_SCSI_SIZE_CLEAN flag in dn_flags. */
1828 	dnp = &devnamesp[major];
1829 	if ((dnp->dn_flags & DN_SCSI_SIZE_CLEAN) == 0) {
1830 		LOCK_DEV_OPS(&dnp->dn_lock);
1831 		dnp->dn_flags |= DN_SCSI_SIZE_CLEAN;
1832 		UNLOCK_DEV_OPS(&dnp->dn_lock);
1833 	}
1834 }
1835 
1836 
1837 /*
1838  * Called by an HBA to map strings to capability indices
1839  */
1840 int
1841 scsi_hba_lookup_capstr(
1842 	char			*capstr)
1843 {
1844 	/*
1845 	 * Capability strings: only add entries to mask the legacy
1846 	 * '_' vs. '-' misery.  All new capabilities should use '-',
1847 	 * and be captured be added to SCSI_CAP_ASCII.
1848 	 */
1849 	static struct cap_strings {
1850 		char	*cap_string;
1851 		int	cap_index;
1852 	} cap_strings[] = {
1853 		{ "dma_max",		SCSI_CAP_DMA_MAX		},
1854 		{ "msg_out",		SCSI_CAP_MSG_OUT		},
1855 		{ "wide_xfer",		SCSI_CAP_WIDE_XFER		},
1856 		{ NULL,			0				}
1857 	};
1858 	static char		*cap_ascii[] = SCSI_CAP_ASCII;
1859 	char			**cap;
1860 	int			i;
1861 	struct cap_strings	*cp;
1862 
1863 	for (cap = cap_ascii, i = 0; *cap != NULL; cap++, i++)
1864 		if (strcmp(*cap, capstr) == 0)
1865 			return (i);
1866 
1867 	for (cp = cap_strings; cp->cap_string != NULL; cp++)
1868 		if (strcmp(cp->cap_string, capstr) == 0)
1869 			return (cp->cap_index);
1870 
1871 	return (-1);
1872 }
1873 
1874 /*
1875  * Called by an HBA to determine if the system is in 'panic' state.
1876  */
1877 int
1878 scsi_hba_in_panic()
1879 {
1880 	return (panicstr != NULL);
1881 }
1882 
1883 /*
1884  * If a SCSI target driver attempts to mmap memory,
1885  * the buck stops here.
1886  */
1887 /*ARGSUSED*/
1888 static int
1889 scsi_hba_map_fault(
1890 	dev_info_t		*dip,
1891 	dev_info_t		*child,
1892 	struct hat		*hat,
1893 	struct seg		*seg,
1894 	caddr_t			addr,
1895 	struct devpage		*dp,
1896 	pfn_t			pfn,
1897 	uint_t			prot,
1898 	uint_t			lock)
1899 {
1900 	return (DDI_FAILURE);
1901 }
1902 
1903 static int
1904 scsi_hba_get_eventcookie(
1905 	dev_info_t		*self,
1906 	dev_info_t		*child,
1907 	char			*name,
1908 	ddi_eventcookie_t	*eventp)
1909 {
1910 	scsi_hba_tran_t		*tran;
1911 
1912 	tran = ddi_get_driver_private(self);
1913 	if (tran->tran_get_eventcookie &&
1914 	    ((*tran->tran_get_eventcookie)(self,
1915 	    child, name, eventp) == DDI_SUCCESS)) {
1916 		return (DDI_SUCCESS);
1917 	}
1918 
1919 	return (ndi_busop_get_eventcookie(self, child, name, eventp));
1920 }
1921 
1922 static int
1923 scsi_hba_add_eventcall(
1924 	dev_info_t		*self,
1925 	dev_info_t		*child,
1926 	ddi_eventcookie_t	event,
1927 	void			(*callback)(
1928 					dev_info_t *self,
1929 					ddi_eventcookie_t event,
1930 					void *arg,
1931 					void *bus_impldata),
1932 	void			*arg,
1933 	ddi_callback_id_t	*cb_id)
1934 {
1935 	scsi_hba_tran_t		*tran;
1936 
1937 	tran = ddi_get_driver_private(self);
1938 	if (tran->tran_add_eventcall &&
1939 	    ((*tran->tran_add_eventcall)(self, child,
1940 	    event, callback, arg, cb_id) == DDI_SUCCESS)) {
1941 		return (DDI_SUCCESS);
1942 	}
1943 
1944 	return (DDI_FAILURE);
1945 }
1946 
1947 static int
1948 scsi_hba_remove_eventcall(dev_info_t *self, ddi_callback_id_t cb_id)
1949 {
1950 	scsi_hba_tran_t		*tran;
1951 	ASSERT(cb_id);
1952 
1953 	tran = ddi_get_driver_private(self);
1954 	if (tran->tran_remove_eventcall &&
1955 	    ((*tran->tran_remove_eventcall)(
1956 	    self, cb_id) == DDI_SUCCESS)) {
1957 		return (DDI_SUCCESS);
1958 	}
1959 
1960 	return (DDI_FAILURE);
1961 }
1962 
1963 static int
1964 scsi_hba_post_event(
1965 	dev_info_t		*self,
1966 	dev_info_t		*child,
1967 	ddi_eventcookie_t	event,
1968 	void			*bus_impldata)
1969 {
1970 	scsi_hba_tran_t		*tran;
1971 
1972 	tran = ddi_get_driver_private(self);
1973 	if (tran->tran_post_event &&
1974 	    ((*tran->tran_post_event)(self,
1975 	    child, event, bus_impldata) == DDI_SUCCESS)) {
1976 		return (DDI_SUCCESS);
1977 	}
1978 
1979 	return (DDI_FAILURE);
1980 }
1981 
1982 /*
1983  * Default getinfo(9e) for scsi_hba
1984  */
1985 /* ARGSUSED */
1986 static int
1987 scsi_hba_info(dev_info_t *self, ddi_info_cmd_t infocmd, void *arg,
1988     void **result)
1989 {
1990 	int error = DDI_SUCCESS;
1991 
1992 	switch (infocmd) {
1993 	case DDI_INFO_DEVT2INSTANCE:
1994 		*result = (void *)(intptr_t)(MINOR2INST(getminor((dev_t)arg)));
1995 		break;
1996 	default:
1997 		error = DDI_FAILURE;
1998 	}
1999 	return (error);
2000 }
2001 
2002 /*
2003  * Default open and close routine for scsi_hba
2004  */
2005 /* ARGSUSED */
2006 int
2007 scsi_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp)
2008 {
2009 	dev_info_t	*self;
2010 	scsi_hba_tran_t	*tran;
2011 	int		rv = 0;
2012 
2013 	if (otyp != OTYP_CHR)
2014 		return (EINVAL);
2015 
2016 	if ((self = e_ddi_hold_devi_by_dev(*devp, 0)) == NULL)
2017 		return (ENXIO);
2018 
2019 	tran = ddi_get_driver_private(self);
2020 	if (tran == NULL) {
2021 		ddi_release_devi(self);
2022 		return (ENXIO);
2023 	}
2024 
2025 	/*
2026 	 * tran_open_flag bit field:
2027 	 *	0:	closed
2028 	 *	1:	shared open by minor at bit position
2029 	 *	1 at 31st bit:	exclusive open
2030 	 */
2031 	mutex_enter(&(tran->tran_open_lock));
2032 	if (flags & FEXCL) {
2033 		if (tran->tran_open_flag != 0) {
2034 			rv = EBUSY;		/* already open */
2035 		} else {
2036 			tran->tran_open_flag = TRAN_OPEN_EXCL;
2037 		}
2038 	} else {
2039 		if (tran->tran_open_flag == TRAN_OPEN_EXCL) {
2040 			rv = EBUSY;		/* already excl. open */
2041 		} else {
2042 			int minor = getminor(*devp) & TRAN_MINOR_MASK;
2043 			tran->tran_open_flag |= (1 << minor);
2044 			/*
2045 			 * Ensure that the last framework reserved minor
2046 			 * is unused. Otherwise, the exclusive open
2047 			 * mechanism may break.
2048 			 */
2049 			ASSERT(minor != 31);
2050 		}
2051 	}
2052 	mutex_exit(&(tran->tran_open_lock));
2053 
2054 	ddi_release_devi(self);
2055 	return (rv);
2056 }
2057 
2058 /* ARGSUSED */
2059 int
2060 scsi_hba_close(dev_t dev, int flag, int otyp, cred_t *credp)
2061 {
2062 	dev_info_t	*self;
2063 	scsi_hba_tran_t	*tran;
2064 
2065 	if (otyp != OTYP_CHR)
2066 		return (EINVAL);
2067 
2068 	if ((self = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
2069 		return (ENXIO);
2070 
2071 	tran = ddi_get_driver_private(self);
2072 	if (tran == NULL) {
2073 		ddi_release_devi(self);
2074 		return (ENXIO);
2075 	}
2076 
2077 	mutex_enter(&(tran->tran_open_lock));
2078 	if (tran->tran_open_flag == TRAN_OPEN_EXCL) {
2079 		tran->tran_open_flag = 0;
2080 	} else {
2081 		int minor = getminor(dev) & TRAN_MINOR_MASK;
2082 		tran->tran_open_flag &= ~(1 << minor);
2083 	}
2084 	mutex_exit(&(tran->tran_open_lock));
2085 
2086 	ddi_release_devi(self);
2087 	return (0);
2088 }
2089 
2090 /*
2091  * standard ioctl commands for SCSI hotplugging
2092  */
2093 /* ARGSUSED */
2094 int
2095 scsi_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
2096 	int *rvalp)
2097 {
2098 	dev_info_t		*self;
2099 	struct devctl_iocdata	*dcp = NULL;
2100 	dev_info_t		*child = NULL;
2101 	struct scsi_device	*sd;
2102 	scsi_hba_tran_t		*tran;
2103 	uint_t			bus_state;
2104 	int			rv = 0;
2105 	int			circ;
2106 
2107 	if ((self = e_ddi_hold_devi_by_dev(dev, 0)) == NULL) {
2108 		rv = ENXIO;
2109 		goto out;
2110 	}
2111 
2112 	if ((tran = ddi_get_driver_private(self)) == NULL) {
2113 		rv = ENXIO;
2114 		goto out;
2115 	}
2116 
2117 	/* Ioctls for which the generic implementation suffices. */
2118 	switch (cmd) {
2119 	case DEVCTL_DEVICE_GETSTATE:
2120 	case DEVCTL_DEVICE_ONLINE:
2121 	case DEVCTL_DEVICE_OFFLINE:
2122 	case DEVCTL_DEVICE_REMOVE:
2123 	case DEVCTL_BUS_GETSTATE:
2124 		rv = ndi_devctl_ioctl(self, cmd, arg, mode, 0);
2125 		goto out;
2126 	}
2127 
2128 	/* read devctl ioctl data */
2129 	if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS) {
2130 		rv = EFAULT;
2131 		goto out;
2132 	}
2133 
2134 	/* Ioctls that require child identification */
2135 	switch (cmd) {
2136 	case DEVCTL_DEVICE_RESET:
2137 		/* child identification from unit-address */
2138 		if (ndi_dc_getname(dcp) == NULL ||
2139 		    ndi_dc_getaddr(dcp) == NULL) {
2140 			rv = EINVAL;
2141 			goto out;
2142 		}
2143 
2144 		ndi_devi_enter(self, &circ);
2145 		child = ndi_devi_find(self,
2146 		    ndi_dc_getname(dcp), ndi_dc_getaddr(dcp));
2147 		if (child == NULL) {
2148 			ndi_devi_exit(self, circ);
2149 			rv = ENXIO;
2150 			goto out;
2151 		}
2152 		ndi_hold_devi(child);
2153 		ndi_devi_exit(self, circ);
2154 		break;
2155 
2156 	case DEVCTL_BUS_RESETALL:
2157 		/*
2158 		 * Find a child's scsi_address so we can invoke tran_reset
2159 		 * below.
2160 		 *
2161 		 * XXX If no child exists, one may to able to fake a child.
2162 		 *	This will be a enhancement for the future.
2163 		 *	For now, we fall back to BUS_RESET.
2164 		 * XXX We sould be looking at node state to get one
2165 		 *	that is initialized...
2166 		 */
2167 		ndi_devi_enter(self, &circ);
2168 		child = ddi_get_child(self);
2169 		sd = NULL;
2170 		while (child) {
2171 			/* XXX verify scsi_device 'flavor' of child */
2172 			if ((sd = ddi_get_driver_private(child)) != NULL) {
2173 				ndi_hold_devi(child);
2174 				break;
2175 			}
2176 			child = ddi_get_next_sibling(child);
2177 		}
2178 		ndi_devi_exit(self, circ);
2179 		break;
2180 	}
2181 
2182 	switch (cmd) {
2183 	case DEVCTL_DEVICE_RESET:
2184 		ASSERT(child);
2185 		if (tran->tran_reset == NULL)
2186 			rv = ENOTSUP;
2187 		else {
2188 			sd = ddi_get_driver_private(child);
2189 			/* XXX verify scsi_device 'flavor' of child */
2190 			if ((sd == NULL) ||
2191 			    (tran->tran_reset(&sd->sd_address,
2192 			    RESET_TARGET) != 1))
2193 				rv = EIO;
2194 		}
2195 		break;
2196 
2197 	case DEVCTL_BUS_QUIESCE:
2198 		if ((ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS) &&
2199 		    (bus_state == BUS_QUIESCED))
2200 			rv = EALREADY;
2201 		else if (tran->tran_quiesce == NULL)
2202 			rv = ENOTSUP;
2203 		else if ((*tran->tran_quiesce)(self) != 0)
2204 			rv = EIO;
2205 		else
2206 			(void) ndi_set_bus_state(self, BUS_QUIESCED);
2207 		break;
2208 
2209 	case DEVCTL_BUS_UNQUIESCE:
2210 		if ((ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS) &&
2211 		    (bus_state == BUS_ACTIVE))
2212 			rv = EALREADY;
2213 		else if (tran->tran_unquiesce == NULL)
2214 			rv = ENOTSUP;
2215 		else if ((*tran->tran_unquiesce)(self) != 0)
2216 			rv = EIO;
2217 		else
2218 			(void) ndi_set_bus_state(self, BUS_ACTIVE);
2219 		break;
2220 
2221 	case DEVCTL_BUS_RESET:
2222 		if (tran->tran_bus_reset == NULL)
2223 			rv = ENOTSUP;
2224 		else if ((*tran->tran_bus_reset)(self, RESET_BUS) != 1)
2225 			rv = EIO;
2226 		break;
2227 
2228 	case DEVCTL_BUS_RESETALL:
2229 		if (tran->tran_reset == NULL) {
2230 			rv = ENOTSUP;
2231 		} else {
2232 			if (sd) {
2233 				if ((*tran->tran_reset)
2234 				    (&sd->sd_address, RESET_ALL) != 1)
2235 					rv = EIO;
2236 			} else {
2237 				if ((tran->tran_bus_reset == NULL) ||
2238 				    ((*tran->tran_bus_reset)
2239 				    (self, RESET_BUS) != 1))
2240 					rv = EIO;
2241 			}
2242 		}
2243 		break;
2244 
2245 	case DEVCTL_BUS_CONFIGURE:
2246 		if (ndi_devi_config(self, NDI_DEVFS_CLEAN|
2247 		    NDI_DEVI_PERSIST|NDI_CONFIG_REPROBE) != NDI_SUCCESS) {
2248 			rv = EIO;
2249 		}
2250 		break;
2251 
2252 	case DEVCTL_BUS_UNCONFIGURE:
2253 		if (ndi_devi_unconfig(self,
2254 		    NDI_DEVI_REMOVE|NDI_DEVFS_CLEAN) != NDI_SUCCESS) {
2255 			rv = EBUSY;
2256 		}
2257 		break;
2258 
2259 	default:
2260 		rv = ENOTTY;
2261 	}
2262 
2263 out:	if (child)
2264 		ndi_rele_devi(child);
2265 	if (dcp)
2266 		ndi_dc_freehdl(dcp);
2267 	if (self)
2268 		ddi_release_devi(self);
2269 	return (rv);
2270 }
2271 
2272 /*ARGSUSED*/
2273 static int
2274 scsi_hba_fm_init_child(dev_info_t *self, dev_info_t *child, int cap,
2275     ddi_iblock_cookie_t *ibc)
2276 {
2277 	scsi_hba_tran_t	*tran = ddi_get_driver_private(self);
2278 
2279 	return (tran ? tran->tran_fm_capable : scsi_fm_capable);
2280 }
2281 
2282 static int
2283 scsi_hba_bus_power(dev_info_t *self, void *impl_arg, pm_bus_power_op_t op,
2284     void *arg, void *result)
2285 {
2286 	scsi_hba_tran_t	*tran;
2287 
2288 	tran = ddi_get_driver_private(self);
2289 	if (tran && tran->tran_bus_power) {
2290 		return (tran->tran_bus_power(self, impl_arg,
2291 		    op, arg, result));
2292 	}
2293 
2294 	return (pm_busop_bus_power(self, impl_arg, op, arg, result));
2295 }
2296 
2297 /*
2298  * Convert between normalized (SCSI-3) LUN format, as described by
2299  * scsi_lun_t, and a normalized lun64_t representation. The normalized
2300  * representation maps in a compatible way to SCSI-2 LUNs.
2301  *
2302  * SCSI-3 LUNs are 64 bits. SCSI-2 LUNs are 3 bits (up to 5 bits in
2303  * some non-compliant implementations). SCSI-3 will pass a (64-bit)
2304  * scsi_lun_t, but we need a representation from which we can for example,
2305  * make device names. For compatibility we represent 64-bit LUN numbers
2306  * in such a way that they appear like they would have under SCSI-2.
2307  * This means that the single level LUN number is in the lowest byte with
2308  * the second, third, and fourth level LUNs represented in successively
2309  * higher bytes. In particular, if (and only if) the first byte of a 64
2310  * bit LUN is zero, denoting "Peripheral Device Addressing Method" and
2311  * "Bus Identifier" zero, then the target implements LUNs compatible in
2312  * spirit with SCSI-2 LUNs (although under SCSI-3 there may be up to
2313  * 256 of them). Under SCSI-3 rules, a target is *required* to use
2314  * this format if it contains 256 or fewer Logical Units, none of which
2315  * are dependent logical units.
2316  *
2317  * These routines have knowledge of the structure and size of a scsi_lun_t.
2318  *
2319  * XXX Should these function be rewritten to take the scsi_lun_t *?
2320  */
2321 scsi_lun64_t
2322 scsi_lun_to_lun64(scsi_lun_t lun)
2323 {
2324 	scsi_lun64_t	lun64;
2325 
2326 	/* check address method and bus identifier */
2327 	if (lun.sl_lun1_msb == 0) {
2328 		/* single-level LUN */
2329 		lun64 = lun.sl_lun1_lsb;	/* extract the 8-bit LUN */
2330 
2331 		/* Ensure rest of LUN is zero, which it is supposed to be */
2332 		if ((lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) &&
2333 		    (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) &&
2334 		    (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) {
2335 			return (lun64);
2336 		}
2337 
2338 		/* Oops, we got a bad scsi_lun_t. Leave it in 64-bit form */
2339 		SCSI_HBA_LOG((_LOG(DIAG1, BADLUN), NULL, NULL,
2340 		    "lun_to_lun64 bad lun %" PRIx64, *(scsi_lun64_t *)&lun));
2341 	}
2342 
2343 	/*
2344 	 * We have a big LUN that is not backward compatible.
2345 	 * Construct a 64 bit number using the right byte order.
2346 	 */
2347 	lun64 =
2348 	    ((scsi_lun64_t)lun.sl_lun1_msb << 56) |
2349 	    ((scsi_lun64_t)lun.sl_lun1_lsb << 48) |
2350 	    ((scsi_lun64_t)lun.sl_lun2_msb << 40) |
2351 	    ((scsi_lun64_t)lun.sl_lun2_lsb << 32) |
2352 	    ((scsi_lun64_t)lun.sl_lun3_msb << 24) |
2353 	    ((scsi_lun64_t)lun.sl_lun3_lsb << 16) |
2354 	    ((scsi_lun64_t)lun.sl_lun4_msb <<  8) |
2355 	    (scsi_lun64_t)lun.sl_lun4_lsb;
2356 	return (lun64);
2357 }
2358 
2359 scsi_lun_t
2360 scsi_lun64_to_lun(scsi_lun64_t lun64)
2361 {
2362 	scsi_lun_t	lun;
2363 
2364 	if (lun64 < 256) {
2365 		/* This LUN is in compatibility format */
2366 		lun.sl_lun1_msb = 0;
2367 		lun.sl_lun1_lsb = (uchar_t)lun64;
2368 		lun.sl_lun2_msb = 0;
2369 		lun.sl_lun2_lsb = 0;
2370 		lun.sl_lun3_msb = 0;
2371 		lun.sl_lun3_lsb = 0;
2372 		lun.sl_lun4_msb = 0;
2373 		lun.sl_lun4_lsb = 0;
2374 	} else {
2375 		/* This in full 64 bit LUN format */
2376 		lun.sl_lun1_msb = (uchar_t)(lun64 >> 56);
2377 		lun.sl_lun1_lsb = (uchar_t)(lun64 >> 48);
2378 		lun.sl_lun2_msb = (uchar_t)(lun64 >> 40);
2379 		lun.sl_lun2_lsb = (uchar_t)(lun64 >> 32);
2380 		lun.sl_lun3_msb = (uchar_t)(lun64 >> 24);
2381 		lun.sl_lun3_lsb = (uchar_t)(lun64 >> 16);
2382 		lun.sl_lun4_msb = (uchar_t)(lun64 >>  8);
2383 		lun.sl_lun4_lsb = (uchar_t)(lun64);
2384 
2385 		/* Oops, bad LUN -- this is required to be nonzero */
2386 		if (lun.sl_lun1_msb == 0)
2387 			SCSI_HBA_LOG((_LOG(DIAG1, BADLUN), NULL, NULL,
2388 			    "lun64_to_lun bad lun %" PRIlun64, lun64));
2389 	}
2390 	return (lun);
2391 }
2392 
2393 /*
2394  * Return the lun from an address string. Either the lun is after the
2395  * first ',' or the entire addr is the lun. Return SCSI_LUN64_ILLEGAL
2396  * if the format is incorrect.
2397  *
2398  * If the addr specified has incorrect syntax (busconfig one of
2399  * bogus /devices path) then scsi_addr_to_lun64 can return SCSI_LUN64_ILLEGAL.
2400  */
2401 scsi_lun64_t
2402 scsi_addr_to_lun64(char *addr)
2403 {
2404 	scsi_lun64_t	lun64;
2405 	char		*s;
2406 	int		i;
2407 
2408 	if (addr) {
2409 		s = strchr(addr, ',');			/* "addr,lun[,sfunc]" */
2410 		if (s)
2411 			s++;				/* skip ',' */
2412 		else
2413 			s = addr;			/* "lun" */
2414 
2415 		for (lun64 = 0, i = 0; *s && (i < 16); s++, i++) {
2416 			if (*s >= '0' && *s <= '9')
2417 				lun64 = (lun64 << 4) + (*s - '0');
2418 			else if (*s >= 'A' && *s <= 'F')
2419 				lun64 = (lun64 << 4) + 10 + (*s - 'A');
2420 			else if (*s >= 'a' && *s <= 'f')
2421 				lun64 = (lun64 << 4) + 10 + (*s - 'a');
2422 			else
2423 				break;
2424 		}
2425 		if (*s && (*s != ','))		/* addr,lun[,sfunc] is OK */
2426 			lun64 = SCSI_LUN64_ILLEGAL;
2427 	} else
2428 		lun64 = SCSI_LUN64_ILLEGAL;
2429 
2430 	if (lun64 == SCSI_LUN64_ILLEGAL)
2431 		SCSI_HBA_LOG((_LOG(DIAG2, BADLUN), NULL, NULL,
2432 		    "addr_to_lun64 %s lun %" PRIlun64,
2433 		    addr ? addr : "NULL", lun64));
2434 	return (lun64);
2435 }
2436 
2437 /*
2438  * Convert scsi ascii string data to NULL terminated (semi) legal IEEE 1275
2439  * "compatible" (name) property form.
2440  *
2441  * For ASCII INQUIRY data, a one-way conversion algorithm is needed to take
2442  * SCSI_ASCII (20h - 7Eh) to a 1275-like compatible form. The 1275 spec allows
2443  * letters, digits, one ",", and ". _ + -", all limited by a maximum 31
2444  * character length. Since ", ." are used as separators in the compatible
2445  * string itself, they are converted to "_". All SCSI_ASCII characters that
2446  * are illegal in 1275, as well as any illegal SCSI_ASCII characters
2447  * encountered, are converted to "_". To reduce length, trailing blanks are
2448  * trimmed from SCSI_ASCII fields prior to conversion.
2449  *
2450  * Example: SCSI_ASCII "ST32550W SUN2.1G" -> "ST32550W_SUN2_1G"
2451  *
2452  * NOTE: the 1275 string form is always less than or equal to the scsi form.
2453  */
2454 static char *
2455 string_scsi_to_1275(char *s_1275, char *s_scsi, int len)
2456 {
2457 	(void) strncpy(s_1275, s_scsi, len);
2458 	s_1275[len--] = '\0';
2459 
2460 	while (len >= 0) {
2461 		if (s_1275[len] == ' ')
2462 			s_1275[len--] = '\0';	/* trim trailing " " */
2463 		else
2464 			break;
2465 	}
2466 
2467 	while (len >= 0) {
2468 		if (((s_1275[len] >= 'a') && (s_1275[len] <= 'z')) ||
2469 		    ((s_1275[len] >= 'A') && (s_1275[len] <= 'Z')) ||
2470 		    ((s_1275[len] >= '0') && (s_1275[len] <= '9')) ||
2471 		    (s_1275[len] == '_') ||
2472 		    (s_1275[len] == '+') ||
2473 		    (s_1275[len] == '-'))
2474 			len--;			/* legal 1275  */
2475 		else
2476 			s_1275[len--] = '_';	/* illegal SCSI_ASCII | 1275 */
2477 	}
2478 
2479 	return (s_1275);
2480 }
2481 
2482 /*
2483  * Given the inquiry data, binding_set, and dtype_node for a scsi device,
2484  * return the nodename and compatible property for the device. The "compatible"
2485  * concept comes from IEEE-1275. The compatible information is returned is in
2486  * the correct form for direct use defining the "compatible" string array
2487  * property. Internally, "compatible" is also used to determine the nodename
2488  * to return.
2489  *
2490  * This function is provided as a separate entry point for use by drivers that
2491  * currently issue their own non-SCSA inquiry command and perform their own
2492  * node creation based their own private compiled in tables. Converting these
2493  * drivers to use this interface provides a quick easy way of obtaining
2494  * consistency as well as the flexibility associated with the 1275 techniques.
2495  *
2496  * The dtype_node is passed as a separate argument (instead of having the
2497  * implementation use inq_dtype). It indicates that information about
2498  * a secondary function embedded service should be produced.
2499  *
2500  * Callers must always use scsi_hba_nodename_compatible_free, even if
2501  * *nodenamep is null, to free the nodename and compatible information
2502  * when done.
2503  *
2504  * If a nodename can't be determined then **compatiblep will point to a
2505  * diagnostic string containing all the compatible forms.
2506  *
2507  * NOTE: some compatible strings may violate the 31 character restriction
2508  * imposed by IEEE-1275. This is not a problem because Solaris does not care
2509  * about this 31 character limit.
2510  *
2511  * Each compatible form belongs to a form-group.  The form-groups currently
2512  * defined are generic ("scsiclass"), binding-set ("scsa.b"), and failover
2513  * ("scsa.f").
2514  *
2515  * The following compatible forms, in high to low precedence
2516  * order, are defined for SCSI target device nodes.
2517  *
2518  *  scsiclass,DDEEFFF.vVVVVVVVV.pPPPPPPPPPPPPPPPP.rRRRR	(1 *1&2)
2519  *  scsiclass,DDEE.vVVVVVVVV.pPPPPPPPPPPPPPPPP.rRRRR	(2 *1)
2520  *  scsiclass,DDFFF.vVVVVVVVV.pPPPPPPPPPPPPPPPP.rRRRR	(3 *2)
2521  *  scsiclass,DD.vVVVVVVVV.pPPPPPPPPPPPPPPPP.rRRRR	(4)
2522  *  scsiclass,DDEEFFF.vVVVVVVVV.pPPPPPPPPPPPPPPPP	(5 *1&2)
2523  *  scsiclass,DDEE.vVVVVVVVV.pPPPPPPPPPPPPPPPP		(6 *1)
2524  *  scsiclass,DDFFF.vVVVVVVVV.pPPPPPPPPPPPPPPPP		(7 *2)
2525  *  scsiclass,DD.vVVVVVVVV.pPPPPPPPPPPPPPPPP		(8)
2526  *  scsa,DD.bBBBBBBBB					(8.5 *3)
2527  *  scsiclass,DDEEFFF					(9 *1&2)
2528  *  scsiclass,DDEE					(10 *1)
2529  *  scsiclass,DDFFF					(11 *2)
2530  *  scsiclass,DD					(12)
2531  *  scsa.fFFF						(12.5 *4)
2532  *  scsiclass						(13)
2533  *
2534  *	  *1 only produced on a secondary function node
2535  *	  *2 only produced when generic form-group flags exist.
2536  *	  *3 only produced when binding-set form-group legacy support is needed
2537  *	  *4 only produced when failover form-group flags exist.
2538  *
2539  *	where:
2540  *
2541  *	v                       is the letter 'v'. Denotest the
2542  *				beginning of VVVVVVVV.
2543  *
2544  *	VVVVVVVV                Translated scsi_vendor.
2545  *
2546  *	p                       is the letter 'p'. Denotes the
2547  *				beginning of PPPPPPPPPPPPPPPP.
2548  *
2549  *	PPPPPPPPPPPPPPPP	Translated scsi_product.
2550  *
2551  *	r                       is the letter 'r'. Denotes the
2552  *				beginning of RRRR.
2553  *
2554  *	RRRR                    Translated scsi_revision.
2555  *
2556  *	DD                      is a two digit ASCII hexadecimal
2557  *				number. The value of the two digits is
2558  *				based one the SCSI "Peripheral device
2559  *				type" command set associated with the
2560  *				node. On a primary node this is the
2561  *				scsi_dtype of the primary command set,
2562  *				on a secondary node this is the
2563  *				scsi_dtype associated with the embedded
2564  *				function command set.
2565  *
2566  *	EE                      Same encoding used for DD. This form is
2567  *				only generated on secondary function
2568  *				nodes. The DD function is embedded in
2569  *				an EE device.
2570  *
2571  *	FFF                     Concatenation, in alphabetical order,
2572  *				of the flag characters within a form-group.
2573  *				For a given form-group, the following
2574  *				flags are defined.
2575  *
2576  *				scsiclass: (generic form-group):
2577  *				  R	Removable_Media: Used when
2578  *					inq_rmb is set.
2579  *
2580  *				scsa.f:	(failover form-group):
2581  *				  E	Explicit Target_Port_Group: Used
2582  *					when inq_tpgse is set and 'G' is
2583  *					alse present.
2584  *				  G	GUID: Used when a GUID can be
2585  *					generated for the device.
2586  *				  I	Implicit Target_Port_Group: Used
2587  *					when inq_tpgs is set and 'G' is
2588  *					also present.
2589  *
2590  *				Forms using FFF are only be generated
2591  *				if there are applicable flag
2592  *				characters.
2593  *
2594  *	b                       is the letter 'b'. Denotes the
2595  *				beginning of BBBBBBBB.
2596  *
2597  *	BBBBBBBB                Binding-set. Operating System Specific:
2598  *				scsi-binding-set property of HBA.
2599  */
2600 #define	NCOMPAT		(1 + (13 + 2) + 1)
2601 #define	COMPAT_LONGEST	(strlen( \
2602 	"scsiclass,DDEEFFF.vVVVVVVVV.pPPPPPPPPPPPPPPPP.rRRRR" + 1))
2603 
2604 /*
2605  * Private version with extra device 'identity' arguments to allow code
2606  * to determine GUID FFF support.
2607  */
2608 static void
2609 scsi_hba_identity_nodename_compatible_get(struct scsi_inquiry *inq,
2610     uchar_t *inq80, size_t inq80len, uchar_t *inq83, size_t inq83len,
2611     char *binding_set, int dtype_node, char *compat0,
2612     char **nodenamep, char ***compatiblep, int *ncompatiblep)
2613 {
2614 	char		vid[sizeof (inq->inq_vid) + 1 ];
2615 	char		pid[sizeof (inq->inq_pid) + 1];
2616 	char		rev[sizeof (inq->inq_revision) + 1];
2617 	char		gf[sizeof ("R\0")];
2618 	char		ff[sizeof ("EGI\0")];
2619 	int		dtype_device;
2620 	int		ncompat;		/* number of compatible */
2621 	char		**compatp;		/* compatible ptrs */
2622 	int		i;
2623 	char		*nname;			/* nodename */
2624 	char		*dname;			/* driver name */
2625 	char		**csp;
2626 	char		*p;
2627 	int		tlen;
2628 	int		len;
2629 	major_t		major;
2630 	ddi_devid_t	devid;
2631 	char		*guid;
2632 
2633 	/*
2634 	 * Nodename_aliases: This table was originally designed to be
2635 	 * implemented via a new nodename_aliases file - a peer to the
2636 	 * driver_aliases that selects a nodename based on compatible
2637 	 * forms in much the same say driver_aliases is used to select
2638 	 * driver bindings from compatible forms. Each compatible form
2639 	 * is an 'alias'. Until a more general need for a
2640 	 * nodename_aliases file exists, which may never occur, the
2641 	 * scsi mappings are described here via a compiled in table.
2642 	 *
2643 	 * This table contains nodename mappings for self-identifying
2644 	 * scsi devices enumerated by the Solaris kernel. For a given
2645 	 * device, the highest precedence "compatible" form with a
2646 	 * mapping is used to select the nodename for the device. This
2647 	 * will typically be a generic nodename, however in some legacy
2648 	 * compatibility cases a driver nodename mapping may be selected.
2649 	 *
2650 	 * Because of possible breakage associated with switching SCSI
2651 	 * target devices from driver nodenames to generic nodenames,
2652 	 * we are currently unable to support generic nodenames for all
2653 	 * SCSI devices (binding-sets). Although /devices paths are
2654 	 * defined as unstable, avoiding possible breakage is
2655 	 * important. Some of the newer SCSI transports (USB) already
2656 	 * use generic nodenames. All new SCSI transports and target
2657 	 * devices should use generic nodenames. At times this decision
2658 	 * may be architecture dependent (sparc .vs. intel) based on when
2659 	 * a transport was supported on a particular architecture.
2660 	 *
2661 	 * We provide a base set of generic nodename mappings based on
2662 	 * scsiclass dtype and higher-precedence driver nodename
2663 	 * mappings based on scsa "binding-set" to cover legacy
2664 	 * issues. The binding-set is typically associated with
2665 	 * "scsi-binding-set" property value of the HBA. The legacy
2666 	 * mappings are provided independent of whether the driver they
2667 	 * refer to is installed. This allows a correctly named node
2668 	 * be created at discovery time, and binding to occur when/if
2669 	 * an add_drv of the legacy driver occurs.
2670 	 *
2671 	 * We also have mappings for legacy SUN hardware that
2672 	 * misidentifies itself (enclosure services which identify
2673 	 * themselves as processors). All future hardware should use
2674 	 * the correct dtype.
2675 	 *
2676 	 * As SCSI HBAs are modified to use the SCSA interfaces for
2677 	 * self-identifying SCSI target devices (PSARC/2004/116) the
2678 	 * nodename_aliases table (PSARC/2004/420) should be augmented
2679 	 * with legacy mappings in order to maintain compatibility with
2680 	 * existing /devices paths, especially for devices that house
2681 	 * an OS. Failure to do this may cause upgrade problems.
2682 	 * Additions for new target devices or transports should not
2683 	 * add scsa binding-set compatible mappings.
2684 	 */
2685 	static struct nodename_aliases {
2686 		char	*na_nodename;		/* nodename */
2687 		char	*na_alias;		/* compatible form match */
2688 	} na[] = {
2689 	/* # mapping to generic nodenames based on scsi dtype */
2690 		{"disk",		"scsiclass,00"},
2691 		{"tape",		"scsiclass,01"},
2692 		{"printer",		"scsiclass,02"},
2693 		{"processor",		"scsiclass,03"},
2694 		{"worm",		"scsiclass,04"},
2695 		{"cdrom",		"scsiclass,05"},
2696 		{"scanner",		"scsiclass,06"},
2697 		{"optical-disk",	"scsiclass,07"},
2698 		{"medium-changer",	"scsiclass,08"},
2699 		{"obsolete",		"scsiclass,09"},
2700 		{"prepress-a",		"scsiclass,0a"},
2701 		{"prepress-b",		"scsiclass,0b"},
2702 		{"array-controller",	"scsiclass,0c"},
2703 		{"enclosure",		"scsiclass,0d"},
2704 		{"disk",		"scsiclass,0e"},
2705 		{"card-reader",		"scsiclass,0f"},
2706 		{"bridge",		"scsiclass,10"},
2707 		{"object-store",	"scsiclass,11"},
2708 		{"reserved",		"scsiclass,12"},
2709 		{"reserved",		"scsiclass,13"},
2710 		{"reserved",		"scsiclass,14"},
2711 		{"reserved",		"scsiclass,15"},
2712 		{"reserved",		"scsiclass,16"},
2713 		{"reserved",		"scsiclass,17"},
2714 		{"reserved",		"scsiclass,18"},
2715 		{"reserved",		"scsiclass,19"},
2716 		{"reserved",		"scsiclass,1a"},
2717 		{"reserved",		"scsiclass,1b"},
2718 		{"reserved",		"scsiclass,1c"},
2719 		{"reserved",		"scsiclass,1d"},
2720 		{"well-known-lun",	"scsiclass,1e"},
2721 		{"unknown",		"scsiclass,1f"},
2722 
2723 #ifdef	sparc
2724 	/* # legacy mapping to driver nodenames for fcp binding-set */
2725 		{"ssd",			"scsa,00.bfcp"},
2726 		{"st",			"scsa,01.bfcp"},
2727 		{"sgen",		"scsa,08.bfcp"},
2728 		{"ses",			"scsa,0d.bfcp"},
2729 
2730 	/* # legacy mapping to driver nodenames for vhci binding-set */
2731 		{"ssd",			"scsa,00.bvhci"},
2732 		{"st",			"scsa,01.bvhci"},
2733 		{"sgen",		"scsa,08.bvhci"},
2734 		{"ses",			"scsa,0d.bvhci"},
2735 #else	/* sparc */
2736 	/* # for x86 fcp and vhci use generic nodenames */
2737 #endif	/* sparc */
2738 
2739 #ifdef  notdef
2740 	/*
2741 	 * The following binding-set specific mappings are not being
2742 	 * delivered at this time, but are listed here as an examples of
2743 	 * the type of mappings needed.
2744 	 */
2745 
2746 	/* # legacy mapping to driver nodenames for spi binding-set */
2747 		{"sd",			"scsa,00.bspi"},
2748 		{"sd",			"scsa,05.bspi"},
2749 		{"sd",			"scsa,07.bspi"},
2750 		{"st",			"scsa,01.bspi"},
2751 		{"ses",			"scsa,0d.bspi"},
2752 
2753 	/* #				SUN misidentified spi hardware */
2754 		{"ses",			"scsiclass,03.vSUN.pD2"},
2755 		{"ses",			"scsiclass,03.vSYMBIOS.pD1000"},
2756 
2757 	/* # legacy mapping to driver nodenames for atapi binding-set */
2758 		{"sd",			"scsa,00.batapi"},
2759 		{"sd",			"scsa,05.batapi"},
2760 		{"sd",			"scsa,07.batapi"},
2761 		{"st",			"scsa,01.batapi"},
2762 		{"unknown",		"scsa,0d.batapi"},
2763 
2764 	/* # legacy mapping to generic nodenames for usb binding-set */
2765 		{"disk",		"scsa,05.busb"},
2766 		{"disk",		"scsa,07.busb"},
2767 		{"changer",		"scsa,08.busb"},
2768 		{"comm",		"scsa,09.busb"},
2769 		{"array_ctlr",		"scsa,0c.busb"},
2770 		{"esi",			"scsa,0d.busb"},
2771 #endif  /* notdef */
2772 
2773 	/*
2774 	 * mapping nodenames for mpt based on scsi dtype
2775 	 * for being compatible with the original node names
2776 	 * under mpt controller
2777 	 */
2778 		{"sd",			"scsa,00.bmpt"},
2779 		{"sd",			"scsa,05.bmpt"},
2780 		{"sd",			"scsa,07.bmpt"},
2781 		{"st",			"scsa,01.bmpt"},
2782 		{"ses",			"scsa,0d.bmpt"},
2783 		{"sgen",		"scsa,08.bmpt"},
2784 		{NULL,		NULL}
2785 	};
2786 	struct nodename_aliases *nap;
2787 
2788 	ASSERT(nodenamep && compatiblep && ncompatiblep &&
2789 	    (binding_set == NULL || (strlen(binding_set) <= 8)));
2790 	if ((nodenamep == NULL) || (compatiblep == NULL) ||
2791 	    (ncompatiblep == NULL))
2792 		return;
2793 
2794 	/*
2795 	 * In order to reduce runtime we allocate one block of memory that
2796 	 * contains both the NULL terminated array of pointers to compatible
2797 	 * forms and the individual compatible strings. This block is
2798 	 * somewhat larger than needed, but is short lived - it only exists
2799 	 * until the caller can transfer the information into the "compatible"
2800 	 * string array property and call scsi_hba_nodename_compatible_free.
2801 	 */
2802 	tlen = NCOMPAT * COMPAT_LONGEST;
2803 	compatp = kmem_alloc((NCOMPAT * sizeof (char *)) + tlen, KM_SLEEP);
2804 
2805 	/* convert inquiry data from SCSI ASCII to 1275 string */
2806 	(void) string_scsi_to_1275(vid, inq->inq_vid,
2807 	    sizeof (inq->inq_vid));
2808 	(void) string_scsi_to_1275(pid, inq->inq_pid,
2809 	    sizeof (inq->inq_pid));
2810 	(void) string_scsi_to_1275(rev, inq->inq_revision,
2811 	    sizeof (inq->inq_revision));
2812 	ASSERT((strlen(vid) <= sizeof (inq->inq_vid)) &&
2813 	    (strlen(pid) <= sizeof (inq->inq_pid)) &&
2814 	    (strlen(rev) <= sizeof (inq->inq_revision)));
2815 
2816 	/*
2817 	 * Form flags in ***ALPHABETICAL*** order within form-group:
2818 	 *
2819 	 * NOTE: When adding a new flag to an existing form-group, carefull
2820 	 * consideration must be given to not breaking existing bindings
2821 	 * based on that form-group.
2822 	 */
2823 
2824 	/*
2825 	 * generic form-group flags
2826 	 *   R	removable:
2827 	 *	Set when inq_rmb is set and for well known scsi dtypes. For a
2828 	 *	bus where the entire device is removable (like USB), we expect
2829 	 *	the HBA to intercept the inquiry data and set inq_rmb.
2830 	 *	Since OBP does not distinguish removable media in its generic
2831 	 *	name selection we avoid setting the 'R' flag if the root is not
2832 	 *	yet mounted.
2833 	 */
2834 	i = 0;
2835 	dtype_device = inq->inq_dtype & DTYPE_MASK;
2836 	if (rootvp && (inq->inq_rmb ||
2837 	    (dtype_device == DTYPE_WORM) ||
2838 	    (dtype_device == DTYPE_RODIRECT) ||
2839 	    (dtype_device == DTYPE_OPTICAL)))
2840 		gf[i++] = 'R';			/* removable */
2841 	gf[i] = '\0';
2842 
2843 	/*
2844 	 * failover form-group flags
2845 	 *   E	Explicit Target_Port_Group_Supported:
2846 	 *	Set for a device that has a GUID if inq_tpgse also set.
2847 	 *   G	GUID:
2848 	 *	Set when we have identity information, can determine a devid
2849 	 *	from the identity information, and can generate a guid from
2850 	 *	that devid.
2851 	 *   I	Implicit Target_Port_Group_Supported:
2852 	 *	Set for a device that has a GUID if inq_tpgs also set.
2853 	 */
2854 	i = 0;
2855 	if ((inq80 || inq83) &&
2856 	    (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST, NULL,
2857 	    (uchar_t *)inq, sizeof (*inq), inq80, inq80len, inq83, inq83len,
2858 	    &devid) == DDI_SUCCESS)) {
2859 		guid = ddi_devid_to_guid(devid);
2860 		ddi_devid_free(devid);
2861 	} else
2862 		guid = NULL;
2863 	if (guid && (inq->inq_tpgs & TPGS_FAILOVER_EXPLICIT))
2864 		ff[i++] = 'E';			/* EXPLICIT TPGS */
2865 	if (guid)
2866 		ff[i++] = 'G';			/* GUID */
2867 	if (guid && (inq->inq_tpgs & TPGS_FAILOVER_IMPLICIT))
2868 		ff[i++] = 'I';			/* IMPLICIT TPGS */
2869 	ff[i] = '\0';
2870 	if (guid)
2871 		ddi_devid_free_guid(guid);
2872 
2873 	/*
2874 	 * Construct all applicable compatible forms. See comment at the
2875 	 * head of the function for a description of the compatible forms.
2876 	 */
2877 	csp = compatp;
2878 	p = (char *)(compatp + NCOMPAT);
2879 
2880 	/* ( 0) driver (optional, not documented in scsi(4)) */
2881 	if (compat0) {
2882 		*csp++ = p;
2883 		(void) snprintf(p, tlen, "%s", compat0);
2884 		len = strlen(p) + 1;
2885 		p += len;
2886 		tlen -= len;
2887 	}
2888 
2889 	/* ( 1) scsiclass,DDEEFFF.vV.pP.rR */
2890 	if ((dtype_device != dtype_node) && *gf && *vid && *pid && *rev) {
2891 		*csp++ = p;
2892 		(void) snprintf(p, tlen, "scsiclass,%02x%02x%s.v%s.p%s.r%s",
2893 		    dtype_node, dtype_device, gf, vid, pid, rev);
2894 		len = strlen(p) + 1;
2895 		p += len;
2896 		tlen -= len;
2897 	}
2898 
2899 	/* ( 2) scsiclass,DDEE.vV.pP.rR */
2900 	if ((dtype_device != dtype_node) && *vid && *pid && *rev) {
2901 		*csp++ = p;
2902 		(void) snprintf(p, tlen, "scsiclass,%02x%02x.v%s.p%s.r%s",
2903 		    dtype_node, dtype_device, vid, pid, rev);
2904 		len = strlen(p) + 1;
2905 		p += len;
2906 		tlen -= len;
2907 	}
2908 
2909 	/* ( 3) scsiclass,DDFFF.vV.pP.rR */
2910 	if (*gf && *vid && *pid && *rev) {
2911 		*csp++ = p;
2912 		(void) snprintf(p, tlen, "scsiclass,%02x%s.v%s.p%s.r%s",
2913 		    dtype_node, gf, vid, pid, rev);
2914 		len = strlen(p) + 1;
2915 		p += len;
2916 		tlen -= len;
2917 	}
2918 
2919 	/* ( 4) scsiclass,DD.vV.pP.rR */
2920 	if (*vid && *pid && rev) {
2921 		*csp++ = p;
2922 		(void) snprintf(p, tlen, "scsiclass,%02x.v%s.p%s.r%s",
2923 		    dtype_node, vid, pid, rev);
2924 		len = strlen(p) + 1;
2925 		p += len;
2926 		tlen -= len;
2927 	}
2928 
2929 	/* ( 5) scsiclass,DDEEFFF.vV.pP */
2930 	if ((dtype_device != dtype_node) && *gf && *vid && *pid) {
2931 		*csp++ = p;
2932 		(void) snprintf(p, tlen, "scsiclass,%02x%02x%s.v%s.p%s",
2933 		    dtype_node, dtype_device, gf, vid, pid);
2934 		len = strlen(p) + 1;
2935 		p += len;
2936 		tlen -= len;
2937 	}
2938 
2939 	/* ( 6) scsiclass,DDEE.vV.pP */
2940 	if ((dtype_device != dtype_node) && *vid && *pid) {
2941 		*csp++ = p;
2942 		(void) snprintf(p, tlen, "scsiclass,%02x%02x.v%s.p%s",
2943 		    dtype_node, dtype_device, vid, pid);
2944 		len = strlen(p) + 1;
2945 		p += len;
2946 		tlen -= len;
2947 	}
2948 
2949 	/* ( 7) scsiclass,DDFFF.vV.pP */
2950 	if (*gf && *vid && *pid) {
2951 		*csp++ = p;
2952 		(void) snprintf(p, tlen, "scsiclass,%02x%s.v%s.p%s",
2953 		    dtype_node, gf, vid, pid);
2954 		len = strlen(p) + 1;
2955 		p += len;
2956 		tlen -= len;
2957 	}
2958 
2959 	/* ( 8) scsiclass,DD.vV.pP */
2960 	if (*vid && *pid) {
2961 		*csp++ = p;
2962 		(void) snprintf(p, tlen, "scsiclass,%02x.v%s.p%s",
2963 		    dtype_node, vid, pid);
2964 		len = strlen(p) + 1;
2965 		p += len;
2966 		tlen -= len;
2967 	}
2968 
2969 	/* (8.5) scsa,DD.bB (not documented in scsi(4)) */
2970 	if (binding_set) {
2971 		*csp++ = p;
2972 		(void) snprintf(p, tlen, "scsa,%02x.b%s",
2973 		    dtype_node, binding_set);
2974 		len = strlen(p) + 1;
2975 		p += len;
2976 		tlen -= len;
2977 	}
2978 
2979 	/* ( 9) scsiclass,DDEEFFF */
2980 	if ((dtype_device != dtype_node) && *gf) {
2981 		*csp++ = p;
2982 		(void) snprintf(p, tlen, "scsiclass,%02x%02x%s",
2983 		    dtype_node, dtype_device, gf);
2984 		len = strlen(p) + 1;
2985 		p += len;
2986 		tlen -= len;
2987 	}
2988 
2989 	/* (10) scsiclass,DDEE */
2990 	if (dtype_device != dtype_node) {
2991 		*csp++ = p;
2992 		(void) snprintf(p, tlen, "scsiclass,%02x%02x",
2993 		    dtype_node, dtype_device);
2994 		len = strlen(p) + 1;
2995 		p += len;
2996 		tlen -= len;
2997 	}
2998 
2999 	/* (11) scsiclass,DDFFF */
3000 	if (*gf) {
3001 		*csp++ = p;
3002 		(void) snprintf(p, tlen, "scsiclass,%02x%s",
3003 		    dtype_node, gf);
3004 		len = strlen(p) + 1;
3005 		p += len;
3006 		tlen -= len;
3007 	}
3008 
3009 	/* (12) scsiclass,DD */
3010 	*csp++ = p;
3011 	(void) snprintf(p, tlen, "scsiclass,%02x", dtype_node);
3012 	len = strlen(p) + 1;
3013 	p += len;
3014 	tlen -= len;
3015 
3016 	/* (12.5) scsa.fFFF */
3017 	if (*ff) {
3018 		*csp++ = p;
3019 		(void) snprintf(p, tlen, "scsa.f%s", ff);
3020 		len = strlen(p) + 1;
3021 		p += len;
3022 		tlen -= len;
3023 	}
3024 
3025 	/* (13) scsiclass */
3026 	*csp++ = p;
3027 	(void) snprintf(p, tlen, "scsiclass");
3028 	len = strlen(p) + 1;
3029 	p += len;
3030 	tlen -= len;
3031 	ASSERT(tlen >= 0);
3032 
3033 	*csp = NULL;			/* NULL terminate array of pointers */
3034 	ncompat = csp - compatp;
3035 
3036 	/*
3037 	 * When determining a nodename, a nodename_aliases specified
3038 	 * mapping has precedence over using a driver_aliases specified
3039 	 * driver binding as a nodename.
3040 	 *
3041 	 * See if any of the compatible forms have a nodename_aliases
3042 	 * specified nodename. These mappings are described by
3043 	 * nodename_aliases entries like:
3044 	 *
3045 	 *	disk		"scsiclass,00"
3046 	 *	enclosure	"scsiclass,03.vSYMBIOS.pD1000"
3047 	 *	ssd		"scsa,00.bfcp"
3048 	 *
3049 	 * All nodename_aliases mappings should idealy be to generic
3050 	 * names, however a higher precedence legacy mapping to a
3051 	 * driver name may exist. The highest precedence mapping
3052 	 * provides the nodename, so legacy driver nodename mappings
3053 	 * (if they exist) take precedence over generic nodename
3054 	 * mappings.
3055 	 */
3056 	for (nname = NULL, csp = compatp; (nname == NULL) && *csp; csp++) {
3057 		for (nap = na; nap->na_nodename; nap++) {
3058 			if (strcmp(*csp, nap->na_alias) == 0) {
3059 				nname = nap->na_nodename;
3060 				break;
3061 			}
3062 		}
3063 	}
3064 
3065 	/*
3066 	 * If no nodename_aliases mapping exists then use the
3067 	 * driver_aliases specified driver binding as a nodename.
3068 	 * Determine the driver based on compatible (which may
3069 	 * have the passed in compat0 as the first item). The
3070 	 * driver_aliases file has entries like
3071 	 *
3072 	 *	sd	"scsiclass,00"
3073 	 *
3074 	 * that map compatible forms to specific drivers. These
3075 	 * entries are established by add_drv. We use the most specific
3076 	 * driver binding as the nodename. This matches the eventual
3077 	 * ddi_driver_compatible_major() binding that will be
3078 	 * established by bind_node()
3079 	 */
3080 	if (nname == NULL) {
3081 		for (dname = NULL, csp = compatp; *csp; csp++) {
3082 			major = ddi_name_to_major(*csp);
3083 			if ((major == (major_t)-1) ||
3084 			    (devnamesp[major].dn_flags & DN_DRIVER_REMOVED))
3085 				continue;
3086 			if (dname = ddi_major_to_name(major))
3087 				break;
3088 		}
3089 		nname = dname;
3090 	}
3091 
3092 	/* return results */
3093 	if (nname) {
3094 		*nodenamep = kmem_alloc(strlen(nname) + 1, KM_SLEEP);
3095 		(void) strcpy(*nodenamep, nname);
3096 	} else {
3097 		*nodenamep = NULL;
3098 
3099 		/*
3100 		 * If no nodename could be determined return a special
3101 		 * 'compatible' to be used for a diagnostic message. This
3102 		 * compatible contains all compatible forms concatenated
3103 		 * into a single string pointed to by the first element.
3104 		 */
3105 		if (nname == NULL) {
3106 			for (csp = compatp; *(csp + 1); csp++)
3107 				*((*csp) + strlen(*csp)) = ' ';
3108 			*(compatp + 1) = NULL;
3109 			ncompat = 1;
3110 		}
3111 
3112 	}
3113 	*compatiblep = compatp;
3114 	*ncompatiblep = ncompat;
3115 }
3116 
3117 void
3118 scsi_hba_nodename_compatible_get(struct scsi_inquiry *inq,
3119     char *binding_set, int dtype_node, char *compat0,
3120     char **nodenamep, char ***compatiblep, int *ncompatiblep)
3121 {
3122 	scsi_hba_identity_nodename_compatible_get(inq,
3123 	    NULL, 0, NULL, 0, binding_set, dtype_node, compat0, nodenamep,
3124 	    compatiblep, ncompatiblep);
3125 }
3126 
3127 /*
3128  * Free allocations associated with scsi_hba_nodename_compatible_get or
3129  * scsi_hba_identity_nodename_compatible_get use.
3130  */
3131 void
3132 scsi_hba_nodename_compatible_free(char *nodename, char **compatible)
3133 {
3134 	if (nodename)
3135 		kmem_free(nodename, strlen(nodename) + 1);
3136 
3137 	if (compatible)
3138 		kmem_free(compatible, (NCOMPAT * sizeof (char *)) +
3139 		    (NCOMPAT * COMPAT_LONGEST));
3140 }
3141 
3142 static int
3143 scsi_hba_bus_config(dev_info_t *self, uint_t flag, ddi_bus_config_op_t op,
3144     void *arg, dev_info_t **childp)
3145 {
3146 	scsi_hba_tran_t	*tran;
3147 
3148 	tran = ddi_get_driver_private(self);
3149 	if (tran && tran->tran_bus_config) {
3150 		return (tran->tran_bus_config(self, flag, op, arg, childp));
3151 	}
3152 
3153 	/*
3154 	 * Force reprobe for BUS_CONFIG_ONE or when manually reconfiguring
3155 	 * via devfsadm(1m) to emulate deferred attach.
3156 	 * Reprobe only discovers driver.conf enumerated nodes, more
3157 	 * dynamic implementations probably require their own bus_config.
3158 	 */
3159 	if ((op == BUS_CONFIG_ONE) || (flag & NDI_DRV_CONF_REPROBE))
3160 		flag |= NDI_CONFIG_REPROBE;
3161 
3162 	return (ndi_busop_bus_config(self, flag, op, arg, childp, 0));
3163 }
3164 
3165 static int
3166 scsi_hba_bus_unconfig(dev_info_t *self, uint_t flag, ddi_bus_config_op_t op,
3167     void *arg)
3168 {
3169 	scsi_hba_tran_t	*tran;
3170 
3171 	tran = ddi_get_driver_private(self);
3172 	if (tran && tran->tran_bus_unconfig) {
3173 		return (tran->tran_bus_unconfig(self, flag, op, arg));
3174 	}
3175 	return (ndi_busop_bus_unconfig(self, flag, op, arg));
3176 }
3177