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