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