xref: /illumos-gate/usr/src/uts/common/io/scsi/impl/scsi_hba.c (revision 20d217c8569fadc52e6956aa7fcc78efd8d1f1b5)
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 		/*
1131 		 * Set the 'ddi-config-driver-node' property on the nexus
1132 		 * node that notify attach_driver_nodes() to configure all
1133 		 * immediate children so that nodes which bind to the
1134 		 * same driver as parent are able to be added into per-driver
1135 		 * list.
1136 		 */
1137 		if (ndi_prop_create_boolean(DDI_DEV_T_NONE,
1138 		    self, "ddi-config-driver-node") != DDI_PROP_SUCCESS)
1139 			goto fail;
1140 	} else {
1141 		if (scsa_tran_setup(self, tran) != DDI_SUCCESS)
1142 			goto fail;
1143 	}
1144 
1145 	return (DDI_SUCCESS);
1146 
1147 fail:
1148 	(void) scsi_hba_detach(self);
1149 	return (DDI_FAILURE);
1150 }
1151 
1152 /*
1153  * Called by an HBA to detach an instance of the driver
1154  */
1155 int
1156 scsi_hba_detach(dev_info_t *self)
1157 {
1158 	scsi_hba_tran_t	*tran;
1159 
1160 	SCSI_HBA_LOG((_LOG_TRACE, self, NULL, __func__));
1161 
1162 	ASSERT(scsi_hba_iport_unit_address(self) == NULL);
1163 	if (scsi_hba_iport_unit_address(self))
1164 		return (DDI_FAILURE);
1165 
1166 	tran = ndi_flavorv_get(self, SCSA_FLAVOR_SCSI_DEVICE);
1167 	ASSERT(tran);
1168 	if (tran == NULL)
1169 		return (DDI_FAILURE);
1170 
1171 	ASSERT(tran->tran_open_flag == 0);
1172 	if (tran->tran_open_flag)
1173 		return (DDI_FAILURE);
1174 
1175 	if (!(tran->tran_hba_flags & SCSI_HBA_HBA))
1176 		scsa_tran_teardown(self, tran);
1177 	scsa_nexus_teardown(self, tran);
1178 
1179 	/*
1180 	 * XXX - scsi_transport.h states that these data fields should not be
1181 	 * referenced by the HBA. However, to be consistent with
1182 	 * scsi_hba_attach(), they are being reset.
1183 	 */
1184 	tran->tran_hba_dip = (dev_info_t *)NULL;
1185 	tran->tran_hba_flags = 0;
1186 	(void) memset(&tran->tran_dma_attr, 0, sizeof (ddi_dma_attr_t));
1187 
1188 	if (tran->tran_pkt_cache_ptr != NULL) {
1189 		kmem_cache_destroy(tran->tran_pkt_cache_ptr);
1190 		tran->tran_pkt_cache_ptr = NULL;
1191 	}
1192 
1193 	/* Teardown flavor of transport (and ddi_get_driver_private()) */
1194 	ndi_flavorv_set(self, SCSA_FLAVOR_SCSI_DEVICE, NULL);
1195 
1196 	return (DDI_SUCCESS);
1197 }
1198 
1199 /*
1200  * Called by an HBA from _fini()
1201  */
1202 void
1203 scsi_hba_fini(struct modlinkage *modlp)
1204 {
1205 	struct dev_ops *hba_dev_ops;
1206 
1207 	SCSI_HBA_LOG((_LOG_TRACE, NULL, NULL, __func__));
1208 
1209 	/* Get the devops structure of this module and clear bus_ops vector. */
1210 	hba_dev_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
1211 
1212 	if (hba_dev_ops->devo_cb_ops == &scsi_hba_cbops)
1213 		hba_dev_ops->devo_cb_ops = NULL;
1214 
1215 	if (hba_dev_ops->devo_getinfo == scsi_hba_info)
1216 		hba_dev_ops->devo_getinfo = NULL;
1217 
1218 	hba_dev_ops->devo_bus_ops = (struct bus_ops *)NULL;
1219 }
1220 
1221 /*
1222  * SAS specific functions
1223  */
1224 /*ARGSUSED*/
1225 sas_hba_tran_t *
1226 sas_hba_tran_alloc(
1227 	dev_info_t		*self,
1228 	int			flags)
1229 {
1230 	/* allocate SCSA flavors for self */
1231 	ndi_flavorv_alloc(self, SCSA_NFLAVORS);
1232 	return (kmem_zalloc(sizeof (sas_hba_tran_t), KM_SLEEP));
1233 }
1234 
1235 void
1236 sas_hba_tran_free(
1237 	sas_hba_tran_t		*tran)
1238 {
1239 	kmem_free(tran, sizeof (sas_hba_tran_t));
1240 }
1241 
1242 int
1243 sas_hba_attach_setup(
1244 	dev_info_t		*self,
1245 	sas_hba_tran_t		*tran)
1246 {
1247 	ASSERT(scsi_hba_iport_unit_address(self) == NULL);
1248 	if (scsi_hba_iport_unit_address(self))
1249 		return (DDI_FAILURE);
1250 	/*
1251 	 * The owner of the this devinfo_t was responsible
1252 	 * for informing the framework already about
1253 	 * additional flavors.
1254 	 */
1255 	ndi_flavorv_set(self, SCSA_FLAVOR_SMP, tran);
1256 	return (DDI_SUCCESS);
1257 }
1258 
1259 int
1260 sas_hba_detach(dev_info_t *self)
1261 {
1262 	ASSERT(scsi_hba_iport_unit_address(self) == NULL);
1263 	if (scsi_hba_iport_unit_address(self))
1264 		return (DDI_FAILURE);
1265 
1266 	ndi_flavorv_set(self, SCSA_FLAVOR_SMP, NULL);
1267 	return (DDI_SUCCESS);
1268 }
1269 
1270 /*
1271  * SMP child flavored functions
1272  */
1273 
1274 static int
1275 smp_busctl_reportdev(dev_info_t *child)
1276 {
1277 	dev_info_t		*self = ddi_get_parent(child);
1278 	char			*smp_wwn;
1279 
1280 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child,
1281 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1282 	    SMP_WWN, &smp_wwn) != DDI_SUCCESS) {
1283 		return (DDI_FAILURE);
1284 	}
1285 	SCSI_HBA_LOG((_LOG_NF(CONT), "?%s%d at %s%d: wwn %s\n",
1286 	    ddi_driver_name(child), ddi_get_instance(child),
1287 	    ddi_driver_name(self), ddi_get_instance(self), smp_wwn));
1288 	ddi_prop_free(smp_wwn);
1289 	return (DDI_SUCCESS);
1290 }
1291 
1292 static int
1293 smp_busctl_initchild(dev_info_t *child)
1294 {
1295 	dev_info_t		*self = ddi_get_parent(child);
1296 	sas_hba_tran_t		*tran = ndi_flavorv_get(self, SCSA_FLAVOR_SMP);
1297 	struct smp_device	*smp;
1298 	char			addr[SCSI_MAXNAMELEN];
1299 	dev_info_t		*ndip;
1300 	char			*smp_wwn = NULL;
1301 	uint64_t		wwn;
1302 
1303 	ASSERT(tran);
1304 	if (tran == NULL)
1305 		return (DDI_FAILURE);
1306 
1307 	smp = kmem_zalloc(sizeof (struct smp_device), KM_SLEEP);
1308 	smp->dip = child;
1309 	smp->smp_addr.a_hba_tran = tran;
1310 
1311 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child,
1312 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1313 	    SMP_WWN, &smp_wwn) != DDI_SUCCESS) {
1314 		goto failure;
1315 	}
1316 
1317 	if (scsi_wwnstr_to_wwn(smp_wwn, &wwn)) {
1318 		goto failure;
1319 	}
1320 
1321 	bcopy(&wwn, smp->smp_addr.a_wwn, SAS_WWN_BYTE_SIZE);
1322 	(void) snprintf(addr, SCSI_MAXNAMELEN, "w%s", smp_wwn);
1323 
1324 	/* Prevent duplicate nodes.  */
1325 	ndip = ndi_devi_find(self, ddi_node_name(child), addr);
1326 	if (ndip && (ndip != child)) {
1327 		goto failure;
1328 	}
1329 
1330 	ddi_set_name_addr(child, addr);
1331 	ddi_set_driver_private(child, smp);
1332 	ddi_prop_free(smp_wwn);
1333 	return (DDI_SUCCESS);
1334 
1335 failure:
1336 	kmem_free(smp, sizeof (struct smp_device));
1337 	if (smp_wwn) {
1338 		ddi_prop_free(smp_wwn);
1339 	}
1340 	return (DDI_FAILURE);
1341 }
1342 
1343 /*ARGSUSED*/
1344 static int
1345 smp_busctl_uninitchild(dev_info_t *child)
1346 {
1347 	struct smp_device	*smp = ddi_get_driver_private(child);
1348 
1349 	ASSERT(smp);
1350 	if (smp == NULL)
1351 		return (DDI_FAILURE);
1352 	kmem_free(smp, sizeof (*smp));
1353 	ddi_set_driver_private(child, NULL);
1354 	ddi_set_name_addr(child, NULL);
1355 	return (DDI_SUCCESS);
1356 }
1357 
1358 /*
1359  * Wrapper to scsi_get_name which takes a devinfo argument instead of a
1360  * scsi_device structure.
1361  */
1362 static int
1363 scsi_hba_name_child(dev_info_t *child, char *addr, int maxlen)
1364 {
1365 	struct scsi_device	*sd = ddi_get_driver_private(child);
1366 
1367 	/* nodes are named by tran_get_name or default "tgt,lun" */
1368 	if (sd && (scsi_get_name(sd, addr, maxlen) == 1))
1369 		return (DDI_SUCCESS);
1370 
1371 	return (DDI_FAILURE);
1372 }
1373 
1374 static int
1375 scsi_busctl_reportdev(dev_info_t *child)
1376 {
1377 	dev_info_t		*self = ddi_get_parent(child);
1378 	scsi_hba_tran_t		*tran = ddi_get_driver_private(self);
1379 	struct scsi_device	*sd = ddi_get_driver_private(child);
1380 	char			ua[SCSI_MAXNAMELEN];
1381 	char			ba[SCSI_MAXNAMELEN];
1382 
1383 	SCSI_HBA_LOG((_LOG_TRACE, NULL, child, __func__));
1384 
1385 	ASSERT(tran && sd);
1386 	if ((tran == NULL) || (sd == NULL))
1387 		return (DDI_FAILURE);
1388 
1389 	/* get the unit_address and bus_addr information */
1390 	if ((scsi_get_name(sd, ua, sizeof (ua)) == 0) ||
1391 	    (scsi_get_bus_addr(sd, ba, sizeof (ba)) == 0)) {
1392 		SCSI_HBA_LOG((_LOG(WARN), NULL, child, "REPORTDEV failure"));
1393 		return (DDI_FAILURE);
1394 	}
1395 
1396 	if (tran->tran_get_name == NULL)
1397 		SCSI_HBA_LOG((_LOG_NF(CONT), "?%s%d at %s%d: %s",
1398 		    ddi_driver_name(child), ddi_get_instance(child),
1399 		    ddi_driver_name(self), ddi_get_instance(self), ba));
1400 	else
1401 		SCSI_HBA_LOG((_LOG_NF(CONT),
1402 		    "?%s%d at %s%d: unit-address %s: %s",
1403 		    ddi_driver_name(child), ddi_get_instance(child),
1404 		    ddi_driver_name(self), ddi_get_instance(self), ua, ba));
1405 	return (DDI_SUCCESS);
1406 }
1407 
1408 /*
1409  * scsi_busctl_initchild is called to initialize the SCSA transport for
1410  * communication with a particular child scsi target device. Successful
1411  * initialization requires properties on the node which describe the address
1412  * of the target device. If the address of the target device can't be
1413  * determined from properties then DDI_NOT_WELL_FORMED is returned. Nodes that
1414  * are DDI_NOT_WELL_FORMED are considered an implementation artifact.
1415  * The child may be one of the following types of devinfo nodes:
1416  *
1417  * OBP node:
1418  *	OBP does not enumerate target devices attached a SCSI bus. These
1419  *	template/stub/wildcard nodes are a legacy artifact for support of old
1420  *	driver loading methods. Since they have no properties,
1421  *	DDI_NOT_WELL_FORMED will be returned.
1422  *
1423  * SID node:
1424  *	The node may be either a:
1425  *	    o	probe/barrier SID node
1426  *	    o	a dynamic SID target node
1427  *	    o	a dynamic SID mscsi node
1428  *
1429  * driver.conf node: The situation for this nexus is different than most.
1430  *	Typically a driver.conf node definition is used to either define a
1431  *	new child devinfo node or to further decorate (via merge) a SID
1432  *	child with properties. In our case we use the nodes for *both*
1433  *	purposes.
1434  *
1435  * In both the SID node and driver.conf node cases we must form the nodes
1436  * "@addr" from the well-known scsi(9P) device unit-address properties on
1437  * the node.
1438  *
1439  * For HBA drivers that implement the deprecated tran_get_name interface,
1440  * "@addr" construction involves having that driver interpret properties via
1441  * scsi_hba_name_child -> scsi_get_name -> tran_get_name: there is no
1442  * requiremnt for the property names to be well-known.
1443  */
1444 static int
1445 scsi_busctl_initchild(dev_info_t *child)
1446 {
1447 	dev_info_t		*self = ddi_get_parent(child);
1448 	dev_info_t		*dup;
1449 	scsi_hba_tran_t		*tran;
1450 	struct scsi_device	*sd;
1451 	scsi_hba_tran_t		*tran_clone;
1452 	int			tgt = 0;
1453 	int			lun = 0;
1454 	int			sfunc = 0;
1455 	int			err = DDI_FAILURE;
1456 	char			addr[SCSI_MAXNAMELEN];
1457 
1458 	ASSERT(DEVI_BUSY_OWNED(self));
1459 	SCSI_HBA_LOG((_LOG(4), NULL, child, "init begin"));
1460 
1461 	/*
1462 	 * For a driver like fp with multiple upper-layer-protocols
1463 	 * it is possible for scsi_hba_init in _init to plumb SCSA
1464 	 * and have the load of fcp (which does scsi_hba_attach_setup)
1465 	 * to fail.  In this case we may get here with a NULL hba.
1466 	 */
1467 	tran = ddi_get_driver_private(self);
1468 	if (tran == NULL)
1469 		return (DDI_NOT_WELL_FORMED);
1470 
1471 	/*
1472 	 * OBP may create template/stub/wildcard nodes for legacy driver
1473 	 * loading methods. These nodes have no properties, so we lack the
1474 	 * addressing properties to initchild them. Hide the node and return
1475 	 * DDI_NOT_WELL_FORMED.
1476 	 *
1477 	 * XXX need ndi_devi_has_properties(dip) type interface?
1478 	 *
1479 	 * XXX It would be nice if we could delete these ill formed nodes by
1480 	 * implementing a DDI_NOT_WELL_FORMED_DELETE return code. This can't
1481 	 * be done until leadville debug code removes its dependencies
1482 	 * on the devinfo still being present after a failed ndi_devi_online.
1483 	 */
1484 	if ((DEVI(child)->devi_hw_prop_ptr == NULL) &&
1485 	    (DEVI(child)->devi_drv_prop_ptr == NULL) &&
1486 	    (DEVI(child)->devi_sys_prop_ptr == NULL)) {
1487 		SCSI_HBA_LOG((_LOG(4), NULL, child,
1488 		    "init failed: no properties"));
1489 		return (DDI_NOT_WELL_FORMED);
1490 	}
1491 
1492 	/* get legacy SPI addressing properties */
1493 	sfunc = ddi_prop_get_int(DDI_DEV_T_ANY, child,
1494 	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, SCSI_ADDR_PROP_SFUNC, -1);
1495 	lun = ddi_prop_get_int(DDI_DEV_T_ANY, child,
1496 	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0);
1497 	if ((tgt = ddi_prop_get_int(DDI_DEV_T_ANY, child,
1498 	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
1499 	    SCSI_ADDR_PROP_TARGET, -1)) == -1) {
1500 		tgt = 0;
1501 		/*
1502 		 * A driver.conf node for merging always has a target= property,
1503 		 * even if it is just a dummy that does not contain the real
1504 		 * target address. However drivers that register devids may
1505 		 * create stub driver.conf nodes without a target= property so
1506 		 * that pathological devid resolution works.
1507 		 */
1508 		if (ndi_dev_is_persistent_node(child) == 0) {
1509 			SCSI_HBA_LOG((_LOG(4), NULL, child,
1510 			    "init failed: stub .conf node"));
1511 			return (DDI_NOT_WELL_FORMED);
1512 		}
1513 	}
1514 
1515 	/*
1516 	 * The scsi_address structure may not specify all the addressing
1517 	 * information. For an old HBA that doesn't support tran_get_name
1518 	 * (most pre-SCSI-3 HBAs) the scsi_address structure is still used,
1519 	 * so the target property must exist and the LUN must be < 256.
1520 	 */
1521 	if ((tran->tran_get_name == NULL) &&
1522 	    ((tgt >= USHRT_MAX) || (lun >= 256))) {
1523 		SCSI_HBA_LOG((_LOG(1), NULL, child,
1524 		    "init failed: illegal/missing properties"));
1525 		return (DDI_NOT_WELL_FORMED);
1526 	}
1527 
1528 	/*
1529 	 * We need to initialize a fair amount of our environment to invoke
1530 	 * tran_get_name (via scsi_hba_name_child and scsi_get_name) to
1531 	 * produce the "@addr" name from addressing properties. Allocate and
1532 	 * initialize scsi device structure.
1533 	 */
1534 	sd = kmem_zalloc(sizeof (struct scsi_device), KM_SLEEP);
1535 	mutex_init(&sd->sd_mutex, NULL, MUTEX_DRIVER, NULL);
1536 	sd->sd_dev = child;
1537 	sd->sd_pathinfo = NULL;
1538 	ddi_set_driver_private(child, sd);
1539 
1540 	if (tran->tran_hba_flags & SCSI_HBA_ADDR_COMPLEX) {
1541 		/*
1542 		 * For a SCSI_HBA_ADDR_COMPLEX transport we store a pointer to
1543 		 * scsi_device in the scsi_address structure.  This allows an
1544 		 * HBA driver to find its per-scsi_device private data
1545 		 * (accessable to the HBA given just the scsi_address by using
1546 		 *  scsi_address_device(9F)/scsi_device_hba_private_get(9F)).
1547 		 */
1548 		sd->sd_address.a.a_sd = sd;
1549 		tran_clone = NULL;
1550 	} else {
1551 		/*
1552 		 * Initialize the scsi_address so that a SCSI-2 target driver
1553 		 * talking to a SCSI-2 device on a SCSI-3 bus (spi) continues
1554 		 * to work. We skew the secondary function value so that we
1555 		 * can tell from the address structure if we are processing
1556 		 * a secondary function request.
1557 		 */
1558 		sd->sd_address.a_target = (ushort_t)tgt;
1559 		sd->sd_address.a_lun = (uchar_t)lun;
1560 		if (sfunc == -1)
1561 			sd->sd_address.a_sublun = (uchar_t)0;
1562 		else
1563 			sd->sd_address.a_sublun = (uchar_t)sfunc + 1;
1564 
1565 		/*
1566 		 * XXX TODO: apply target/lun limitation logic for SPI
1567 		 * binding_set. If spi this is based on scsi_options WIDE
1568 		 * NLUNS some forms of lun limitation are based on the
1569 		 * device @lun 0
1570 		 */
1571 
1572 		/*
1573 		 * Deprecated: Use SCSI_HBA_ADDR_COMPLEX:
1574 		 *   Clone transport structure if requested. Cloning allows
1575 		 *   an HBA to maintain target-specific information if
1576 		 *   necessary, such as target addressing information that
1577 		 *   does not adhere to the scsi_address structure format.
1578 		 */
1579 		if (tran->tran_hba_flags & SCSI_HBA_TRAN_CLONE) {
1580 			tran_clone = kmem_alloc(
1581 			    sizeof (scsi_hba_tran_t), KM_SLEEP);
1582 			bcopy((caddr_t)tran,
1583 			    (caddr_t)tran_clone, sizeof (scsi_hba_tran_t));
1584 			tran = tran_clone;
1585 			tran->tran_sd = sd;
1586 		} else {
1587 			tran_clone = NULL;
1588 			ASSERT(tran->tran_sd == NULL);
1589 		}
1590 	}
1591 
1592 	/* establish scsi_address pointer to the HBA's tran structure */
1593 	sd->sd_address.a_hba_tran = tran;
1594 
1595 	/*
1596 	 * This is a grotty hack that allows direct-access (non-scsa) drivers
1597 	 * (like chs, ata, and mlx which all make cmdk children) to put its
1598 	 * own vector in the 'a_hba_tran' field. When all the drivers that do
1599 	 * this are fixed, please remove this hack.
1600 	 *
1601 	 * NOTE: This hack is also shows up in the DEVP_TO_TRAN implementation
1602 	 * in scsi_confsubr.c.
1603 	 */
1604 	sd->sd_tran_safe = tran;
1605 
1606 	/* Establish the @addr name of the child. */
1607 	*addr = '\0';
1608 	if (scsi_hba_name_child(child, addr, SCSI_MAXNAMELEN) != DDI_SUCCESS) {
1609 		/*
1610 		 * Some driver.conf files add bogus target properties (relative
1611 		 * to their nexus representation of target) to their stub
1612 		 * nodes, causing the check above to not filter them.
1613 		 */
1614 		SCSI_HBA_LOG((_LOG(3), NULL, child,
1615 		    "init failed: scsi_busctl_ua call"));
1616 		err = DDI_NOT_WELL_FORMED;
1617 		goto failure;
1618 	}
1619 	if (*addr == '\0') {
1620 		SCSI_HBA_LOG((_LOG(2), NULL, child, "init failed: ua"));
1621 		ndi_devi_set_hidden(child);
1622 		err = DDI_NOT_WELL_FORMED;
1623 		goto failure;
1624 	}
1625 
1626 	/* set the node @addr string */
1627 	ddi_set_name_addr(child, addr);
1628 
1629 	/* prevent sibling duplicates */
1630 	dup = ndi_devi_find(self, ddi_node_name(child), addr);
1631 	if (dup && (dup != child)) {
1632 		SCSI_HBA_LOG((_LOG(4), NULL, child,
1633 		    "init failed: detected duplicate %p", (void *)dup));
1634 		goto failure;
1635 	}
1636 
1637 	/* call HBA's target init entry point if it exists */
1638 	if (tran->tran_tgt_init != NULL) {
1639 		SCSI_HBA_LOG((_LOG(4), NULL, child, "init tran_tgt_init"));
1640 		if ((*tran->tran_tgt_init)
1641 		    (self, child, tran, sd) != DDI_SUCCESS) {
1642 			SCSI_HBA_LOG((_LOG(2), NULL, child,
1643 			    "init failed: tran_tgt_init failed"));
1644 			goto failure;
1645 		}
1646 	}
1647 
1648 	SCSI_HBA_LOG((_LOG(3), NULL, child, "init successful"));
1649 	return (DDI_SUCCESS);
1650 
1651 failure:
1652 	if (tran_clone)
1653 		kmem_free(tran_clone, sizeof (scsi_hba_tran_t));
1654 	mutex_destroy(&sd->sd_mutex);
1655 	kmem_free(sd, sizeof (*sd));
1656 	ddi_set_driver_private(child, NULL);
1657 	ddi_set_name_addr(child, NULL);
1658 
1659 	return (err);		/* remove the node */
1660 }
1661 
1662 static int
1663 scsi_busctl_uninitchild(dev_info_t *child)
1664 {
1665 	dev_info_t		*self = ddi_get_parent(child);
1666 	scsi_hba_tran_t		*tran = ddi_get_driver_private(self);
1667 	struct scsi_device	*sd = ddi_get_driver_private(child);
1668 	scsi_hba_tran_t		*tran_clone;
1669 
1670 	ASSERT(DEVI_BUSY_OWNED(self));
1671 
1672 	ASSERT(tran && sd);
1673 	if ((tran == NULL) || (sd == NULL))
1674 		return (DDI_FAILURE);
1675 
1676 
1677 	SCSI_HBA_LOG((_LOG(3), NULL, child, "uninit begin"));
1678 
1679 	if (tran->tran_hba_flags & SCSI_HBA_TRAN_CLONE) {
1680 		tran_clone = sd->sd_address.a_hba_tran;
1681 
1682 		/* ... grotty hack, involving sd_tran_safe, continued. */
1683 		if (tran_clone != sd->sd_tran_safe) {
1684 			tran_clone = sd->sd_tran_safe;
1685 #ifdef	DEBUG
1686 			/*
1687 			 * Complain so things get fixed and hack can, at
1688 			 * some point in time, be removed.
1689 			 */
1690 			SCSI_HBA_LOG((_LOG(WARN), self, NULL,
1691 			    "'%s' is corrupting a_hba_tran", sd->sd_dev ?
1692 			    ddi_driver_name(sd->sd_dev) : "unknown_driver"));
1693 #endif	/* DEBUG */
1694 		}
1695 
1696 		ASSERT(tran_clone->tran_hba_flags & SCSI_HBA_TRAN_CLONE);
1697 		ASSERT(tran_clone->tran_sd == sd);
1698 		tran = tran_clone;
1699 	} else {
1700 		tran_clone = NULL;
1701 		ASSERT(tran->tran_sd == NULL);
1702 	}
1703 
1704 	/*
1705 	 * To simplify host adapter drivers we guarantee that multiple
1706 	 * tran_tgt_init(9E) calls of the same unit address are never
1707 	 * active at the same time.
1708 	 */
1709 	if (tran->tran_tgt_free)
1710 		(*tran->tran_tgt_free) (self, child, tran, sd);
1711 
1712 	/*
1713 	 * If a inquiry data is still allocated (by scsi_probe()) we
1714 	 * free the allocation here. This keeps scsi_inq valid for the
1715 	 * same duration as the corresponding inquiry properties. It
1716 	 * also allows a tran_tgt_init() implementation that establishes
1717 	 * sd_inq (common/io/dktp/controller/ata/ata_disk.c) to deal
1718 	 * with deallocation in its tran_tgt_free (setting sd_inq back
1719 	 * to NULL) without upsetting the framework.
1720 	 */
1721 	if (sd->sd_inq) {
1722 		kmem_free(sd->sd_inq, SUN_INQSIZE);
1723 		sd->sd_inq = (struct scsi_inquiry *)NULL;
1724 	}
1725 
1726 	mutex_destroy(&sd->sd_mutex);
1727 	if (tran_clone)
1728 		kmem_free(tran_clone, sizeof (scsi_hba_tran_t));
1729 	kmem_free(sd, sizeof (*sd));
1730 
1731 	ddi_set_driver_private(child, NULL);
1732 	SCSI_HBA_LOG((_LOG(3), NULL, child, "uninit complete"));
1733 	ddi_set_name_addr(child, NULL);
1734 	return (DDI_SUCCESS);
1735 }
1736 
1737 static int
1738 iport_busctl_reportdev(dev_info_t *child)
1739 {
1740 	dev_info_t	*self = ddi_get_parent(child);
1741 	char		*iport;
1742 
1743 
1744 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child,
1745 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1746 	    "scsi-iport", &iport) != DDI_SUCCESS)
1747 		return (DDI_FAILURE);
1748 
1749 	SCSI_HBA_LOG((_LOG_NF(CONT), "?%s%d at %s%d: iport %s\n",
1750 	    ddi_driver_name(child), ddi_get_instance(child),
1751 	    ddi_driver_name(self), ddi_get_instance(self),
1752 	    iport));
1753 
1754 	ddi_prop_free(iport);
1755 	return (DDI_SUCCESS);
1756 }
1757 
1758 /* uninitchild SCSA iport 'child' node */
1759 static int
1760 iport_busctl_uninitchild(dev_info_t *child)
1761 {
1762 	ddi_set_name_addr(child, NULL);
1763 	return (DDI_SUCCESS);
1764 }
1765 
1766 /* initchild SCSA iport 'child' node */
1767 static int
1768 iport_busctl_initchild(dev_info_t *child)
1769 {
1770 	dev_info_t	*self = ddi_get_parent(child);
1771 	dev_info_t	*dup = NULL;
1772 	char		addr[SCSI_MAXNAMELEN];
1773 	char		*iport;
1774 
1775 
1776 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child,
1777 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1778 	    "scsi-iport", &iport) != DDI_SUCCESS) {
1779 		return (DDI_NOT_WELL_FORMED);
1780 	}
1781 
1782 	(void) snprintf(addr, SCSI_MAXNAMELEN, "%s", iport);
1783 	ddi_prop_free(iport);
1784 
1785 
1786 	/* set the node @addr string */
1787 	ddi_set_name_addr(child, addr);
1788 
1789 	/* Prevent duplicate nodes.  */
1790 	dup = ndi_devi_find(self, ddi_node_name(child), addr);
1791 	if (dup && (dup != child)) {
1792 		ddi_set_name_addr(child, NULL);
1793 		return (DDI_FAILURE);
1794 	}
1795 
1796 	return (DDI_SUCCESS);
1797 }
1798 
1799 /* Uninitialize scsi_device flavor of transport on SCSA iport 'child' node. */
1800 static void
1801 iport_postdetach_tran_scsi_device(dev_info_t *child)
1802 {
1803 	scsi_hba_tran_t		*tran;
1804 
1805 	tran = ndi_flavorv_get(child, SCSA_FLAVOR_SCSI_DEVICE);
1806 	if (tran == NULL)
1807 		return;
1808 
1809 	scsa_tran_teardown(child, tran);
1810 	scsa_nexus_teardown(child, tran);
1811 
1812 	ndi_flavorv_set(child, SCSA_FLAVOR_SCSI_DEVICE, NULL);
1813 	scsi_hba_tran_free(tran);
1814 }
1815 
1816 /*
1817  * Initialize scsi_device flavor of transport on SCSA iport 'child' node.
1818  *
1819  * NOTE: Given our past history with SCSI_HBA_TRAN_CLONE (structure-copy tran
1820  * per scsi_device), using structure-copy of tran at the iport level should
1821  * not be a problem (the most risky thing is likely tran_hba_dip).
1822  */
1823 static void
1824 iport_preattach_tran_scsi_device(dev_info_t *child)
1825 {
1826 	dev_info_t	*hba = ddi_get_parent(child);
1827 	scsi_hba_tran_t	*htran;
1828 	scsi_hba_tran_t	*tran;
1829 
1830 	/* parent HBA node scsi_device tran is required */
1831 	htran = ndi_flavorv_get(hba, SCSA_FLAVOR_SCSI_DEVICE);
1832 	ASSERT(htran != NULL);
1833 	if (htran == NULL)
1834 		return;
1835 
1836 	/* Allocate iport child's scsi_device transport vector */
1837 	tran = scsi_hba_tran_alloc(child, SCSI_HBA_CANSLEEP);
1838 	if (tran == NULL)
1839 		return;
1840 
1841 	/* Structure-copy scsi_device transport of HBA to iport. */
1842 	*tran = *htran;
1843 
1844 	/*
1845 	 * Reset scsi_device transport fields not shared with the
1846 	 * parent, and not established below.
1847 	 */
1848 	tran->tran_open_flag = 0;
1849 	tran->tran_hba_private = NULL;
1850 	tran->tran_iport_dip = child;
1851 
1852 	/* Clear SCSI_HBA_SCSA flags (except TA) */
1853 	tran->tran_hba_flags &= ~SCSI_HBA_SCSA_FM;	/* clear parent state */
1854 	tran->tran_hba_flags |= SCSI_HBA_SCSA_TA;
1855 	tran->tran_hba_flags &= ~SCSI_HBA_HBA;		/* never HBA */
1856 
1857 	/* Establish flavor of transport (and ddi_get_driver_private()) */
1858 	ndi_flavorv_set(child, SCSA_FLAVOR_SCSI_DEVICE, tran);
1859 
1860 	/* Setup iport node */
1861 	if ((scsa_nexus_setup(child, tran) != DDI_SUCCESS) ||
1862 	    (scsa_tran_setup(child, tran) != DDI_SUCCESS)) {
1863 		iport_postdetach_tran_scsi_device(child);
1864 	}
1865 }
1866 
1867 /* Uninitialize smp_device flavor of transport on SCSA iport 'child' node. */
1868 static void
1869 iport_postdetach_tran_smp_device(dev_info_t *child)
1870 {
1871 	sas_hba_tran_t	*tran;
1872 
1873 	tran = ndi_flavorv_get(child, SCSA_FLAVOR_SMP);
1874 	if (tran == NULL)
1875 		return;
1876 
1877 	ndi_flavorv_set(child, SCSA_FLAVOR_SMP, NULL);
1878 	sas_hba_tran_free(tran);
1879 }
1880 
1881 /* Initialize smp_device flavor of transport on SCSA iport 'child' node. */
1882 static void
1883 iport_preattach_tran_smp_device(dev_info_t *child)
1884 {
1885 	dev_info_t	*hba = ddi_get_parent(child);
1886 	sas_hba_tran_t	*htran;
1887 	sas_hba_tran_t	*tran;
1888 
1889 	/* parent HBA node smp_device tran is optional */
1890 	htran = ndi_flavorv_get(hba, SCSA_FLAVOR_SMP);
1891 	if (htran == NULL) {
1892 		ndi_flavorv_set(child, SCSA_FLAVOR_SMP, NULL);
1893 		return;
1894 	}
1895 
1896 	/* Allocate iport child's smp_device transport vector */
1897 	tran = sas_hba_tran_alloc(child, 0);
1898 
1899 	/* Structure-copy smp_device transport of HBA to iport. */
1900 	*tran = *htran;
1901 
1902 	/* Establish flavor of transport */
1903 	ndi_flavorv_set(child, SCSA_FLAVOR_SMP, tran);
1904 }
1905 
1906 
1907 /*
1908  * Generic bus_ctl operations for SCSI HBA's,
1909  * hiding the busctl interface from the HBA.
1910  */
1911 /*ARGSUSED*/
1912 static int
1913 scsi_hba_bus_ctl(
1914 	dev_info_t		*self,
1915 	dev_info_t		*child,
1916 	ddi_ctl_enum_t		op,
1917 	void			*arg,
1918 	void			*result)
1919 {
1920 	int			child_flavor = 0;
1921 	int			val;
1922 	ddi_dma_attr_t		*attr;
1923 	scsi_hba_tran_t		*tran;
1924 	struct attachspec	*as;
1925 	struct detachspec	*ds;
1926 
1927 	/* For some ops, child is 'arg'. */
1928 	if ((op == DDI_CTLOPS_INITCHILD) || (op == DDI_CTLOPS_UNINITCHILD))
1929 		child = (dev_info_t *)arg;
1930 
1931 	/* Determine the flavor of the child: smp .vs. scsi */
1932 	child_flavor = ndi_flavor_get(child);
1933 
1934 	switch (op) {
1935 	case DDI_CTLOPS_INITCHILD:
1936 		switch (child_flavor) {
1937 		case SCSA_FLAVOR_IPORT:
1938 			return (iport_busctl_initchild(child));
1939 		case SCSA_FLAVOR_SMP:
1940 			return (smp_busctl_initchild(child));
1941 		default:
1942 			return (scsi_busctl_initchild(child));
1943 		}
1944 	case DDI_CTLOPS_UNINITCHILD:
1945 		switch (child_flavor) {
1946 		case SCSA_FLAVOR_IPORT:
1947 			return (iport_busctl_uninitchild(child));
1948 		case SCSA_FLAVOR_SMP:
1949 			return (smp_busctl_uninitchild(child));
1950 		default:
1951 			return (scsi_busctl_uninitchild(child));
1952 		}
1953 	case DDI_CTLOPS_REPORTDEV:
1954 		switch (child_flavor) {
1955 		case SCSA_FLAVOR_IPORT:
1956 			return (iport_busctl_reportdev(child));
1957 		case SCSA_FLAVOR_SMP:
1958 			return (smp_busctl_reportdev(child));
1959 		default:
1960 			return (scsi_busctl_reportdev(child));
1961 		}
1962 	case DDI_CTLOPS_ATTACH:
1963 		as = (struct attachspec *)arg;
1964 
1965 		if (child_flavor != SCSA_FLAVOR_IPORT)
1966 			return (DDI_SUCCESS);
1967 
1968 		/* iport processing */
1969 		if (as->when == DDI_PRE) {
1970 			/* setup pre attach(9E) */
1971 			iport_preattach_tran_scsi_device(child);
1972 			iport_preattach_tran_smp_device(child);
1973 		} else if ((as->when == DDI_POST) &&
1974 		    (as->result != DDI_SUCCESS)) {
1975 			/* cleanup if attach(9E) failed */
1976 			iport_postdetach_tran_scsi_device(child);
1977 			iport_postdetach_tran_smp_device(child);
1978 		}
1979 		return (DDI_SUCCESS);
1980 	case DDI_CTLOPS_DETACH:
1981 		ds = (struct detachspec *)arg;
1982 
1983 		if (child_flavor != SCSA_FLAVOR_IPORT)
1984 			return (DDI_SUCCESS);
1985 
1986 		/* iport processing */
1987 		if ((ds->when == DDI_POST) &&
1988 		    (ds->result == DDI_SUCCESS)) {
1989 			/* cleanup if detach(9E) was successful */
1990 			iport_postdetach_tran_scsi_device(child);
1991 			iport_postdetach_tran_smp_device(child);
1992 		}
1993 		return (DDI_SUCCESS);
1994 	case DDI_CTLOPS_IOMIN:
1995 		tran = ddi_get_driver_private(self);
1996 		ASSERT(tran);
1997 		if (tran == NULL)
1998 			return (DDI_FAILURE);
1999 
2000 		/*
2001 		 * The 'arg' value of nonzero indicates 'streaming'
2002 		 * mode. If in streaming mode, pick the largest
2003 		 * of our burstsizes available and say that that
2004 		 * is our minimum value (modulo what minxfer is).
2005 		 */
2006 		attr = &tran->tran_dma_attr;
2007 		val = *((int *)result);
2008 		val = maxbit(val, attr->dma_attr_minxfer);
2009 		*((int *)result) = maxbit(val, ((intptr_t)arg ?
2010 		    (1<<ddi_ffs(attr->dma_attr_burstsizes)-1) :
2011 		    (1<<(ddi_fls(attr->dma_attr_burstsizes)-1))));
2012 
2013 		return (ddi_ctlops(self, child, op, arg, result));
2014 
2015 	case DDI_CTLOPS_SIDDEV:
2016 		return (ndi_dev_is_persistent_node(child) ?
2017 		    DDI_SUCCESS : DDI_FAILURE);
2018 
2019 	/* XXX these should be handled */
2020 	case DDI_CTLOPS_POWER:
2021 		return (DDI_SUCCESS);
2022 
2023 	/*
2024 	 * These ops correspond to functions that "shouldn't" be called
2025 	 * by a SCSI target driver. So we whine when we're called.
2026 	 */
2027 	case DDI_CTLOPS_DMAPMAPC:
2028 	case DDI_CTLOPS_REPORTINT:
2029 	case DDI_CTLOPS_REGSIZE:
2030 	case DDI_CTLOPS_NREGS:
2031 	case DDI_CTLOPS_SLAVEONLY:
2032 	case DDI_CTLOPS_AFFINITY:
2033 	case DDI_CTLOPS_POKE:
2034 	case DDI_CTLOPS_PEEK:
2035 		SCSI_HBA_LOG((_LOG(WARN), self, NULL, "invalid op (%d)", op));
2036 		return (DDI_FAILURE);
2037 
2038 	/* Everything else we pass up */
2039 	case DDI_CTLOPS_PTOB:
2040 	case DDI_CTLOPS_BTOP:
2041 	case DDI_CTLOPS_BTOPR:
2042 	case DDI_CTLOPS_DVMAPAGESIZE:
2043 	default:
2044 		return (ddi_ctlops(self, child, op, arg, result));
2045 	}
2046 }
2047 
2048 /*
2049  * Private wrapper for scsi_pkt's allocated via scsi_hba_pkt_alloc()
2050  */
2051 struct scsi_pkt_wrapper {
2052 	struct scsi_pkt		scsi_pkt;
2053 	int			pkt_wrapper_magic;
2054 	int			pkt_wrapper_len;
2055 };
2056 
2057 #if !defined(lint)
2058 _NOTE(SCHEME_PROTECTS_DATA("unique per thread", scsi_pkt_wrapper))
2059 _NOTE(SCHEME_PROTECTS_DATA("Unshared Data", dev_ops))
2060 #endif
2061 
2062 /*
2063  * Called by an HBA to allocate a scsi_pkt
2064  */
2065 /*ARGSUSED*/
2066 struct scsi_pkt *
2067 scsi_hba_pkt_alloc(
2068 	dev_info_t		*dip,
2069 	struct scsi_address	*ap,
2070 	int			cmdlen,
2071 	int			statuslen,
2072 	int			tgtlen,
2073 	int			hbalen,
2074 	int			(*callback)(caddr_t arg),
2075 	caddr_t			arg)
2076 {
2077 	struct scsi_pkt		*pkt;
2078 	struct scsi_pkt_wrapper	*hba_pkt;
2079 	caddr_t			p;
2080 	int			acmdlen, astatuslen, atgtlen, ahbalen;
2081 	int			pktlen;
2082 
2083 	/* Sanity check */
2084 	if (callback != SLEEP_FUNC && callback != NULL_FUNC)
2085 		SCSI_HBA_LOG((_LOG(WARN), dip, NULL,
2086 		    "callback must be SLEEP_FUNC or NULL_FUNC"));
2087 
2088 	/*
2089 	 * Round up so everything gets allocated on long-word boundaries
2090 	 */
2091 	acmdlen = ROUNDUP(cmdlen);
2092 	astatuslen = ROUNDUP(statuslen);
2093 	atgtlen = ROUNDUP(tgtlen);
2094 	ahbalen = ROUNDUP(hbalen);
2095 	pktlen = sizeof (struct scsi_pkt_wrapper) +
2096 	    acmdlen + astatuslen + atgtlen + ahbalen;
2097 
2098 	hba_pkt = kmem_zalloc(pktlen,
2099 	    (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP);
2100 	if (hba_pkt == NULL) {
2101 		ASSERT(callback == NULL_FUNC);
2102 		return (NULL);
2103 	}
2104 
2105 	/*
2106 	 * Set up our private info on this pkt
2107 	 */
2108 	hba_pkt->pkt_wrapper_len = pktlen;
2109 	hba_pkt->pkt_wrapper_magic = PKT_WRAPPER_MAGIC;	/* alloced correctly */
2110 	pkt = &hba_pkt->scsi_pkt;
2111 
2112 	/*
2113 	 * Set up pointers to private data areas, cdb, and status.
2114 	 */
2115 	p = (caddr_t)(hba_pkt + 1);
2116 	if (hbalen > 0) {
2117 		pkt->pkt_ha_private = (opaque_t)p;
2118 		p += ahbalen;
2119 	}
2120 	if (tgtlen > 0) {
2121 		pkt->pkt_private = (opaque_t)p;
2122 		p += atgtlen;
2123 	}
2124 	if (statuslen > 0) {
2125 		pkt->pkt_scbp = (uchar_t *)p;
2126 		p += astatuslen;
2127 	}
2128 	if (cmdlen > 0) {
2129 		pkt->pkt_cdbp = (uchar_t *)p;
2130 	}
2131 
2132 	/*
2133 	 * Initialize the pkt's scsi_address
2134 	 */
2135 	pkt->pkt_address = *ap;
2136 
2137 	/*
2138 	 * NB: It may not be safe for drivers, esp target drivers, to depend
2139 	 * on the following fields being set until all the scsi_pkt
2140 	 * allocation violations discussed in scsi_pkt.h are all resolved.
2141 	 */
2142 	pkt->pkt_cdblen = cmdlen;
2143 	pkt->pkt_tgtlen = tgtlen;
2144 	pkt->pkt_scblen = statuslen;
2145 
2146 	return (pkt);
2147 }
2148 
2149 /*
2150  * Called by an HBA to free a scsi_pkt
2151  */
2152 /*ARGSUSED*/
2153 void
2154 scsi_hba_pkt_free(
2155 	struct scsi_address	*ap,
2156 	struct scsi_pkt		*pkt)
2157 {
2158 	kmem_free(pkt, ((struct scsi_pkt_wrapper *)pkt)->pkt_wrapper_len);
2159 }
2160 
2161 /*
2162  * Return 1 if the scsi_pkt used a proper allocator.
2163  *
2164  * The DDI does not allow a driver to allocate it's own scsi_pkt(9S), a
2165  * driver should not have *any* compiled in dependencies on "sizeof (struct
2166  * scsi_pkt)". While this has been the case for many years, a number of
2167  * drivers have still not been fixed. This function can be used to detect
2168  * improperly allocated scsi_pkt structures, and produce messages identifying
2169  * drivers that need to be fixed.
2170  *
2171  * While drivers in violation are being fixed, this function can also
2172  * be used by the framework to detect packets that violated allocation
2173  * rules.
2174  *
2175  * NB: It is possible, but very unlikely, for this code to return a false
2176  * positive (finding correct magic, but for wrong reasons).  Careful
2177  * consideration is needed for callers using this interface to condition
2178  * access to newer scsi_pkt fields (those after pkt_reason).
2179  *
2180  * NB: As an aid to minimizing the amount of work involved in 'fixing' legacy
2181  * drivers that violate scsi_*(9S) allocation rules, private
2182  * scsi_pkt_size()/scsi_size_clean() functions are available (see their
2183  * implementation for details).
2184  *
2185  * *** Non-legacy use of scsi_pkt_size() is discouraged. ***
2186  *
2187  * NB: When supporting broken HBA drivers is not longer a concern, this
2188  * code should be removed.
2189  */
2190 int
2191 scsi_pkt_allocated_correctly(struct scsi_pkt *pkt)
2192 {
2193 	struct scsi_pkt_wrapper	*hba_pkt = (struct scsi_pkt_wrapper *)pkt;
2194 	int	magic;
2195 	major_t	major;
2196 #ifdef	DEBUG
2197 	int	*pspwm, *pspcwm;
2198 
2199 	/*
2200 	 * We are getting scsi packets from two 'correct' wrapper schemes,
2201 	 * make sure we are looking at the same place in both to detect
2202 	 * proper allocation.
2203 	 */
2204 	pspwm = &((struct scsi_pkt_wrapper *)0)->pkt_wrapper_magic;
2205 	pspcwm = &((struct scsi_pkt_cache_wrapper *)0)->pcw_magic;
2206 	ASSERT(pspwm == pspcwm);
2207 #endif	/* DEBUG */
2208 
2209 
2210 	/*
2211 	 * Check to see if driver is scsi_size_clean(), assume it
2212 	 * is using the scsi_pkt_size() interface everywhere it needs to
2213 	 * if the driver indicates it is scsi_size_clean().
2214 	 */
2215 	major = ddi_driver_major(P_TO_TRAN(pkt)->tran_hba_dip);
2216 	if (devnamesp[major].dn_flags & DN_SCSI_SIZE_CLEAN)
2217 		return (1);		/* ok */
2218 
2219 	/*
2220 	 * Special case crossing a page boundary. If the scsi_pkt was not
2221 	 * allocated correctly, then across a page boundary we have a
2222 	 * fault hazard.
2223 	 */
2224 	if ((((uintptr_t)(&hba_pkt->scsi_pkt)) & MMU_PAGEMASK) ==
2225 	    (((uintptr_t)(&hba_pkt->pkt_wrapper_magic)) & MMU_PAGEMASK)) {
2226 		/* fastpath, no cross-page hazard */
2227 		magic = hba_pkt->pkt_wrapper_magic;
2228 	} else {
2229 		/* add protection for cross-page hazard */
2230 		if (ddi_peek32((dev_info_t *)NULL,
2231 		    &hba_pkt->pkt_wrapper_magic, &magic) == DDI_FAILURE) {
2232 			return (0);	/* violation */
2233 		}
2234 	}
2235 
2236 	/* properly allocated packet always has correct magic */
2237 	return ((magic == PKT_WRAPPER_MAGIC) ? 1 : 0);
2238 }
2239 
2240 /*
2241  * Private interfaces to simplify conversion of legacy drivers so they don't
2242  * depend on scsi_*(9S) size. Instead of using these private interface, HBA
2243  * drivers should use DDI sanctioned allocation methods:
2244  *
2245  *	scsi_pkt	Use scsi_hba_pkt_alloc(9F), or implement
2246  *			tran_setup_pkt(9E).
2247  *
2248  *	scsi_device	You are doing something strange/special, a scsi_device
2249  *			structure should only be allocated by scsi_hba.c
2250  *			initchild code or scsi_vhci.c code.
2251  *
2252  *	scsi_hba_tran	Use scsi_hba_tran_alloc(9F).
2253  */
2254 size_t
2255 scsi_pkt_size()
2256 {
2257 	return (sizeof (struct scsi_pkt));
2258 }
2259 
2260 size_t
2261 scsi_hba_tran_size()
2262 {
2263 	return (sizeof (scsi_hba_tran_t));
2264 }
2265 
2266 size_t
2267 scsi_device_size()
2268 {
2269 	return (sizeof (struct scsi_device));
2270 }
2271 
2272 /*
2273  * Legacy compliance to scsi_pkt(9S) allocation rules through use of
2274  * scsi_pkt_size() is detected by the 'scsi-size-clean' driver.conf property
2275  * or an HBA driver calling to scsi_size_clean() from attach(9E).  A driver
2276  * developer should only indicate that a legacy driver is clean after using
2277  * SCSI_SIZE_CLEAN_VERIFY to ensure compliance (see scsi_pkt.h).
2278  */
2279 void
2280 scsi_size_clean(dev_info_t *dip)
2281 {
2282 	major_t		major;
2283 	struct devnames	*dnp;
2284 
2285 	ASSERT(dip);
2286 	major = ddi_driver_major(dip);
2287 	ASSERT(major < devcnt);
2288 	if (major >= devcnt) {
2289 		SCSI_HBA_LOG((_LOG(WARN), dip, NULL,
2290 		    "scsi_pkt_size: bogus major: %d", major));
2291 		return;
2292 	}
2293 
2294 	/* Set DN_SCSI_SIZE_CLEAN flag in dn_flags. */
2295 	dnp = &devnamesp[major];
2296 	if ((dnp->dn_flags & DN_SCSI_SIZE_CLEAN) == 0) {
2297 		LOCK_DEV_OPS(&dnp->dn_lock);
2298 		dnp->dn_flags |= DN_SCSI_SIZE_CLEAN;
2299 		UNLOCK_DEV_OPS(&dnp->dn_lock);
2300 	}
2301 }
2302 
2303 
2304 /*
2305  * Called by an HBA to map strings to capability indices
2306  */
2307 int
2308 scsi_hba_lookup_capstr(
2309 	char			*capstr)
2310 {
2311 	/*
2312 	 * Capability strings: only add entries to mask the legacy
2313 	 * '_' vs. '-' misery.  All new capabilities should use '-',
2314 	 * and be captured be added to SCSI_CAP_ASCII.
2315 	 */
2316 	static struct cap_strings {
2317 		char	*cap_string;
2318 		int	cap_index;
2319 	} cap_strings[] = {
2320 		{ "dma_max",		SCSI_CAP_DMA_MAX		},
2321 		{ "msg_out",		SCSI_CAP_MSG_OUT		},
2322 		{ "wide_xfer",		SCSI_CAP_WIDE_XFER		},
2323 		{ NULL,			0				}
2324 	};
2325 	static char		*cap_ascii[] = SCSI_CAP_ASCII;
2326 	char			**cap;
2327 	int			i;
2328 	struct cap_strings	*cp;
2329 
2330 	for (cap = cap_ascii, i = 0; *cap != NULL; cap++, i++)
2331 		if (strcmp(*cap, capstr) == 0)
2332 			return (i);
2333 
2334 	for (cp = cap_strings; cp->cap_string != NULL; cp++)
2335 		if (strcmp(cp->cap_string, capstr) == 0)
2336 			return (cp->cap_index);
2337 
2338 	return (-1);
2339 }
2340 
2341 /*
2342  * Called by an HBA to determine if the system is in 'panic' state.
2343  */
2344 int
2345 scsi_hba_in_panic()
2346 {
2347 	return (panicstr != NULL);
2348 }
2349 
2350 /*
2351  * If a SCSI target driver attempts to mmap memory,
2352  * the buck stops here.
2353  */
2354 /*ARGSUSED*/
2355 static int
2356 scsi_hba_map_fault(
2357 	dev_info_t		*dip,
2358 	dev_info_t		*child,
2359 	struct hat		*hat,
2360 	struct seg		*seg,
2361 	caddr_t			addr,
2362 	struct devpage		*dp,
2363 	pfn_t			pfn,
2364 	uint_t			prot,
2365 	uint_t			lock)
2366 {
2367 	return (DDI_FAILURE);
2368 }
2369 
2370 static int
2371 scsi_hba_get_eventcookie(
2372 	dev_info_t		*self,
2373 	dev_info_t		*child,
2374 	char			*name,
2375 	ddi_eventcookie_t	*eventp)
2376 {
2377 	scsi_hba_tran_t		*tran;
2378 
2379 	tran = ddi_get_driver_private(self);
2380 	if (tran->tran_get_eventcookie &&
2381 	    ((*tran->tran_get_eventcookie)(self,
2382 	    child, name, eventp) == DDI_SUCCESS)) {
2383 		return (DDI_SUCCESS);
2384 	}
2385 
2386 	return (ndi_busop_get_eventcookie(self, child, name, eventp));
2387 }
2388 
2389 static int
2390 scsi_hba_add_eventcall(
2391 	dev_info_t		*self,
2392 	dev_info_t		*child,
2393 	ddi_eventcookie_t	event,
2394 	void			(*callback)(
2395 					dev_info_t *self,
2396 					ddi_eventcookie_t event,
2397 					void *arg,
2398 					void *bus_impldata),
2399 	void			*arg,
2400 	ddi_callback_id_t	*cb_id)
2401 {
2402 	scsi_hba_tran_t		*tran;
2403 
2404 	tran = ddi_get_driver_private(self);
2405 	if (tran->tran_add_eventcall &&
2406 	    ((*tran->tran_add_eventcall)(self, child,
2407 	    event, callback, arg, cb_id) == DDI_SUCCESS)) {
2408 		return (DDI_SUCCESS);
2409 	}
2410 
2411 	return (DDI_FAILURE);
2412 }
2413 
2414 static int
2415 scsi_hba_remove_eventcall(dev_info_t *self, ddi_callback_id_t cb_id)
2416 {
2417 	scsi_hba_tran_t		*tran;
2418 	ASSERT(cb_id);
2419 
2420 	tran = ddi_get_driver_private(self);
2421 	if (tran->tran_remove_eventcall &&
2422 	    ((*tran->tran_remove_eventcall)(
2423 	    self, cb_id) == DDI_SUCCESS)) {
2424 		return (DDI_SUCCESS);
2425 	}
2426 
2427 	return (DDI_FAILURE);
2428 }
2429 
2430 static int
2431 scsi_hba_post_event(
2432 	dev_info_t		*self,
2433 	dev_info_t		*child,
2434 	ddi_eventcookie_t	event,
2435 	void			*bus_impldata)
2436 {
2437 	scsi_hba_tran_t		*tran;
2438 
2439 	tran = ddi_get_driver_private(self);
2440 	if (tran->tran_post_event &&
2441 	    ((*tran->tran_post_event)(self,
2442 	    child, event, bus_impldata) == DDI_SUCCESS)) {
2443 		return (DDI_SUCCESS);
2444 	}
2445 
2446 	return (DDI_FAILURE);
2447 }
2448 
2449 /*
2450  * Default getinfo(9e) for scsi_hba
2451  */
2452 /* ARGSUSED */
2453 static int
2454 scsi_hba_info(dev_info_t *self, ddi_info_cmd_t infocmd, void *arg,
2455     void **result)
2456 {
2457 	int error = DDI_SUCCESS;
2458 
2459 	switch (infocmd) {
2460 	case DDI_INFO_DEVT2INSTANCE:
2461 		*result = (void *)(intptr_t)(MINOR2INST(getminor((dev_t)arg)));
2462 		break;
2463 	default:
2464 		error = DDI_FAILURE;
2465 	}
2466 	return (error);
2467 }
2468 
2469 /*
2470  * Default open and close routine for scsi_hba
2471  */
2472 /* ARGSUSED */
2473 int
2474 scsi_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp)
2475 {
2476 	dev_info_t	*self;
2477 	scsi_hba_tran_t	*tran;
2478 	int		rv = 0;
2479 
2480 	if (otyp != OTYP_CHR)
2481 		return (EINVAL);
2482 
2483 	if ((self = e_ddi_hold_devi_by_dev(*devp, 0)) == NULL)
2484 		return (ENXIO);
2485 
2486 	tran = ddi_get_driver_private(self);
2487 	if (tran == NULL) {
2488 		ddi_release_devi(self);
2489 		return (ENXIO);
2490 	}
2491 
2492 	/*
2493 	 * tran_open_flag bit field:
2494 	 *	0:	closed
2495 	 *	1:	shared open by minor at bit position
2496 	 *	1 at 31st bit:	exclusive open
2497 	 */
2498 	mutex_enter(&(tran->tran_open_lock));
2499 	if (flags & FEXCL) {
2500 		if (tran->tran_open_flag != 0) {
2501 			rv = EBUSY;		/* already open */
2502 		} else {
2503 			tran->tran_open_flag = TRAN_OPEN_EXCL;
2504 		}
2505 	} else {
2506 		if (tran->tran_open_flag == TRAN_OPEN_EXCL) {
2507 			rv = EBUSY;		/* already excl. open */
2508 		} else {
2509 			int minor = getminor(*devp) & TRAN_MINOR_MASK;
2510 			tran->tran_open_flag |= (1 << minor);
2511 			/*
2512 			 * Ensure that the last framework reserved minor
2513 			 * is unused. Otherwise, the exclusive open
2514 			 * mechanism may break.
2515 			 */
2516 			ASSERT(minor != 31);
2517 		}
2518 	}
2519 	mutex_exit(&(tran->tran_open_lock));
2520 
2521 	ddi_release_devi(self);
2522 	return (rv);
2523 }
2524 
2525 /* ARGSUSED */
2526 int
2527 scsi_hba_close(dev_t dev, int flag, int otyp, cred_t *credp)
2528 {
2529 	dev_info_t	*self;
2530 	scsi_hba_tran_t	*tran;
2531 
2532 	if (otyp != OTYP_CHR)
2533 		return (EINVAL);
2534 
2535 	if ((self = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
2536 		return (ENXIO);
2537 
2538 	tran = ddi_get_driver_private(self);
2539 	if (tran == NULL) {
2540 		ddi_release_devi(self);
2541 		return (ENXIO);
2542 	}
2543 
2544 	mutex_enter(&(tran->tran_open_lock));
2545 	if (tran->tran_open_flag == TRAN_OPEN_EXCL) {
2546 		tran->tran_open_flag = 0;
2547 	} else {
2548 		int minor = getminor(dev) & TRAN_MINOR_MASK;
2549 		tran->tran_open_flag &= ~(1 << minor);
2550 	}
2551 	mutex_exit(&(tran->tran_open_lock));
2552 
2553 	ddi_release_devi(self);
2554 	return (0);
2555 }
2556 
2557 /*
2558  * standard ioctl commands for SCSI hotplugging
2559  */
2560 /* ARGSUSED */
2561 int
2562 scsi_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
2563 	int *rvalp)
2564 {
2565 	dev_info_t		*self;
2566 	struct devctl_iocdata	*dcp = NULL;
2567 	dev_info_t		*child = NULL;
2568 	struct scsi_device	*sd;
2569 	scsi_hba_tran_t		*tran;
2570 	uint_t			bus_state;
2571 	int			rv = 0;
2572 	int			circ;
2573 
2574 	if ((self = e_ddi_hold_devi_by_dev(dev, 0)) == NULL) {
2575 		rv = ENXIO;
2576 		goto out;
2577 	}
2578 
2579 	if ((tran = ddi_get_driver_private(self)) == NULL) {
2580 		rv = ENXIO;
2581 		goto out;
2582 	}
2583 
2584 	/* Ioctls for which the generic implementation suffices. */
2585 	switch (cmd) {
2586 	case DEVCTL_DEVICE_GETSTATE:
2587 	case DEVCTL_DEVICE_ONLINE:
2588 	case DEVCTL_DEVICE_OFFLINE:
2589 	case DEVCTL_DEVICE_REMOVE:
2590 	case DEVCTL_BUS_GETSTATE:
2591 		rv = ndi_devctl_ioctl(self, cmd, arg, mode, 0);
2592 		goto out;
2593 	}
2594 
2595 	/* read devctl ioctl data */
2596 	if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS) {
2597 		rv = EFAULT;
2598 		goto out;
2599 	}
2600 
2601 	/* Ioctls that require child identification */
2602 	switch (cmd) {
2603 	case DEVCTL_DEVICE_RESET:
2604 		/* child identification from unit-address */
2605 		if (ndi_dc_getname(dcp) == NULL ||
2606 		    ndi_dc_getaddr(dcp) == NULL) {
2607 			rv = EINVAL;
2608 			goto out;
2609 		}
2610 
2611 		ndi_devi_enter(self, &circ);
2612 		child = ndi_devi_find(self,
2613 		    ndi_dc_getname(dcp), ndi_dc_getaddr(dcp));
2614 		if (child == NULL) {
2615 			ndi_devi_exit(self, circ);
2616 			rv = ENXIO;
2617 			goto out;
2618 		}
2619 		ndi_hold_devi(child);
2620 		ndi_devi_exit(self, circ);
2621 		break;
2622 
2623 	case DEVCTL_BUS_RESETALL:
2624 		/*
2625 		 * Find a child's scsi_address so we can invoke tran_reset
2626 		 * below.
2627 		 *
2628 		 * XXX If no child exists, one may to able to fake a child.
2629 		 *	This will be a enhancement for the future.
2630 		 *	For now, we fall back to BUS_RESET.
2631 		 * XXX We sould be looking at node state to get one
2632 		 *	that is initialized...
2633 		 */
2634 		ndi_devi_enter(self, &circ);
2635 		child = ddi_get_child(self);
2636 		sd = NULL;
2637 		while (child) {
2638 			/* XXX verify scsi_device 'flavor' of child */
2639 			if ((sd = ddi_get_driver_private(child)) != NULL) {
2640 				ndi_hold_devi(child);
2641 				break;
2642 			}
2643 			child = ddi_get_next_sibling(child);
2644 		}
2645 		ndi_devi_exit(self, circ);
2646 		break;
2647 	}
2648 
2649 	switch (cmd) {
2650 	case DEVCTL_DEVICE_RESET:
2651 		ASSERT(child);
2652 		if (tran->tran_reset == NULL)
2653 			rv = ENOTSUP;
2654 		else {
2655 			sd = ddi_get_driver_private(child);
2656 			/* XXX verify scsi_device 'flavor' of child */
2657 			if ((sd == NULL) ||
2658 			    (tran->tran_reset(&sd->sd_address,
2659 			    RESET_TARGET) != 1))
2660 				rv = EIO;
2661 		}
2662 		break;
2663 
2664 	case DEVCTL_BUS_QUIESCE:
2665 		if ((ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS) &&
2666 		    (bus_state == BUS_QUIESCED))
2667 			rv = EALREADY;
2668 		else if (tran->tran_quiesce == NULL)
2669 			rv = ENOTSUP;
2670 		else if ((*tran->tran_quiesce)(self) != 0)
2671 			rv = EIO;
2672 		else
2673 			(void) ndi_set_bus_state(self, BUS_QUIESCED);
2674 		break;
2675 
2676 	case DEVCTL_BUS_UNQUIESCE:
2677 		if ((ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS) &&
2678 		    (bus_state == BUS_ACTIVE))
2679 			rv = EALREADY;
2680 		else if (tran->tran_unquiesce == NULL)
2681 			rv = ENOTSUP;
2682 		else if ((*tran->tran_unquiesce)(self) != 0)
2683 			rv = EIO;
2684 		else
2685 			(void) ndi_set_bus_state(self, BUS_ACTIVE);
2686 		break;
2687 
2688 	case DEVCTL_BUS_RESET:
2689 		if (tran->tran_bus_reset == NULL)
2690 			rv = ENOTSUP;
2691 		else if ((*tran->tran_bus_reset)(self, RESET_BUS) != 1)
2692 			rv = EIO;
2693 		break;
2694 
2695 	case DEVCTL_BUS_RESETALL:
2696 		if (tran->tran_reset == NULL) {
2697 			rv = ENOTSUP;
2698 		} else {
2699 			if (sd) {
2700 				if ((*tran->tran_reset)
2701 				    (&sd->sd_address, RESET_ALL) != 1)
2702 					rv = EIO;
2703 			} else {
2704 				if ((tran->tran_bus_reset == NULL) ||
2705 				    ((*tran->tran_bus_reset)
2706 				    (self, RESET_BUS) != 1))
2707 					rv = EIO;
2708 			}
2709 		}
2710 		break;
2711 
2712 	case DEVCTL_BUS_CONFIGURE:
2713 		if (ndi_devi_config(self, NDI_DEVFS_CLEAN|
2714 		    NDI_DEVI_PERSIST|NDI_CONFIG_REPROBE) != NDI_SUCCESS) {
2715 			rv = EIO;
2716 		}
2717 		break;
2718 
2719 	case DEVCTL_BUS_UNCONFIGURE:
2720 		if (ndi_devi_unconfig(self,
2721 		    NDI_DEVI_REMOVE|NDI_DEVFS_CLEAN) != NDI_SUCCESS) {
2722 			rv = EBUSY;
2723 		}
2724 		break;
2725 
2726 	default:
2727 		rv = ENOTTY;
2728 	}
2729 
2730 out:	if (child)
2731 		ndi_rele_devi(child);
2732 	if (dcp)
2733 		ndi_dc_freehdl(dcp);
2734 	if (self)
2735 		ddi_release_devi(self);
2736 	return (rv);
2737 }
2738 
2739 /*ARGSUSED*/
2740 static int
2741 scsi_hba_fm_init_child(dev_info_t *self, dev_info_t *child, int cap,
2742     ddi_iblock_cookie_t *ibc)
2743 {
2744 	scsi_hba_tran_t	*tran = ddi_get_driver_private(self);
2745 
2746 	return (tran ? tran->tran_fm_capable : scsi_fm_capable);
2747 }
2748 
2749 static int
2750 scsi_hba_bus_power(dev_info_t *self, void *impl_arg, pm_bus_power_op_t op,
2751     void *arg, void *result)
2752 {
2753 	scsi_hba_tran_t	*tran;
2754 
2755 	tran = ddi_get_driver_private(self);
2756 	if (tran && tran->tran_bus_power) {
2757 		return (tran->tran_bus_power(self, impl_arg,
2758 		    op, arg, result));
2759 	}
2760 
2761 	return (pm_busop_bus_power(self, impl_arg, op, arg, result));
2762 }
2763 
2764 /*
2765  * Return the lun from an address string. Either the lun is after the
2766  * first ',' or the entire addr is the lun. Return SCSI_LUN64_ILLEGAL
2767  * if the format is incorrect.
2768  *
2769  * If the addr specified has incorrect syntax (busconfig one of
2770  * bogus /devices path) then scsi_addr_to_lun64 can return SCSI_LUN64_ILLEGAL.
2771  */
2772 scsi_lun64_t
2773 scsi_addr_to_lun64(char *addr)
2774 {
2775 	scsi_lun64_t	lun64;
2776 	char		*s;
2777 	int		i;
2778 
2779 	if (addr) {
2780 		s = strchr(addr, ',');			/* "addr,lun[,sfunc]" */
2781 		if (s)
2782 			s++;				/* skip ',' */
2783 		else
2784 			s = addr;			/* "lun" */
2785 
2786 		for (lun64 = 0, i = 0; *s && (i < 16); s++, i++) {
2787 			if (*s >= '0' && *s <= '9')
2788 				lun64 = (lun64 << 4) + (*s - '0');
2789 			else if (*s >= 'A' && *s <= 'F')
2790 				lun64 = (lun64 << 4) + 10 + (*s - 'A');
2791 			else if (*s >= 'a' && *s <= 'f')
2792 				lun64 = (lun64 << 4) + 10 + (*s - 'a');
2793 			else
2794 				break;
2795 		}
2796 		if (*s && (*s != ','))		/* addr,lun[,sfunc] is OK */
2797 			lun64 = SCSI_LUN64_ILLEGAL;
2798 	} else
2799 		lun64 = SCSI_LUN64_ILLEGAL;
2800 
2801 	if (lun64 == SCSI_LUN64_ILLEGAL)
2802 		SCSI_HBA_LOG((_LOG(2), NULL, NULL,
2803 		    "addr_to_lun64 %s lun %" PRIlun64,
2804 		    addr ? addr : "NULL", lun64));
2805 	return (lun64);
2806 }
2807 
2808 /*
2809  * Convert scsi ascii string data to NULL terminated (semi) legal IEEE 1275
2810  * "compatible" (name) property form.
2811  *
2812  * For ASCII INQUIRY data, a one-way conversion algorithm is needed to take
2813  * SCSI_ASCII (20h - 7Eh) to a 1275-like compatible form. The 1275 spec allows
2814  * letters, digits, one ",", and ". _ + -", all limited by a maximum 31
2815  * character length. Since ", ." are used as separators in the compatible
2816  * string itself, they are converted to "_". All SCSI_ASCII characters that
2817  * are illegal in 1275, as well as any illegal SCSI_ASCII characters
2818  * encountered, are converted to "_". To reduce length, trailing blanks are
2819  * trimmed from SCSI_ASCII fields prior to conversion.
2820  *
2821  * Example: SCSI_ASCII "ST32550W SUN2.1G" -> "ST32550W_SUN2_1G"
2822  *
2823  * NOTE: the 1275 string form is always less than or equal to the scsi form.
2824  */
2825 static char *
2826 string_scsi_to_1275(char *s_1275, char *s_scsi, int len)
2827 {
2828 	(void) strncpy(s_1275, s_scsi, len);
2829 	s_1275[len--] = '\0';
2830 
2831 	while (len >= 0) {
2832 		if (s_1275[len] == ' ')
2833 			s_1275[len--] = '\0';	/* trim trailing " " */
2834 		else
2835 			break;
2836 	}
2837 
2838 	while (len >= 0) {
2839 		if (((s_1275[len] >= 'a') && (s_1275[len] <= 'z')) ||
2840 		    ((s_1275[len] >= 'A') && (s_1275[len] <= 'Z')) ||
2841 		    ((s_1275[len] >= '0') && (s_1275[len] <= '9')) ||
2842 		    (s_1275[len] == '_') ||
2843 		    (s_1275[len] == '+') ||
2844 		    (s_1275[len] == '-'))
2845 			len--;			/* legal 1275  */
2846 		else
2847 			s_1275[len--] = '_';	/* illegal SCSI_ASCII | 1275 */
2848 	}
2849 
2850 	return (s_1275);
2851 }
2852 
2853 /*
2854  * Given the inquiry data, binding_set, and dtype_node for a scsi device,
2855  * return the nodename and compatible property for the device. The "compatible"
2856  * concept comes from IEEE-1275. The compatible information is returned is in
2857  * the correct form for direct use defining the "compatible" string array
2858  * property. Internally, "compatible" is also used to determine the nodename
2859  * to return.
2860  *
2861  * This function is provided as a separate entry point for use by drivers that
2862  * currently issue their own non-SCSA inquiry command and perform their own
2863  * node creation based their own private compiled in tables. Converting these
2864  * drivers to use this interface provides a quick easy way of obtaining
2865  * consistency as well as the flexibility associated with the 1275 techniques.
2866  *
2867  * The dtype_node is passed as a separate argument (instead of having the
2868  * implementation use inq_dtype). It indicates that information about
2869  * a secondary function embedded service should be produced.
2870  *
2871  * Callers must always use scsi_hba_nodename_compatible_free, even if
2872  * *nodenamep is null, to free the nodename and compatible information
2873  * when done.
2874  *
2875  * If a nodename can't be determined then **compatiblep will point to a
2876  * diagnostic string containing all the compatible forms.
2877  *
2878  * NOTE: some compatible strings may violate the 31 character restriction
2879  * imposed by IEEE-1275. This is not a problem because Solaris does not care
2880  * about this 31 character limit.
2881  *
2882  * Each compatible form belongs to a form-group.  The form-groups currently
2883  * defined are generic ("scsiclass"), binding-set ("scsa.b"), and failover
2884  * ("scsa.f").
2885  *
2886  * The following compatible forms, in high to low precedence
2887  * order, are defined for SCSI target device nodes.
2888  *
2889  *  scsiclass,DDEEFFF.vVVVVVVVV.pPPPPPPPPPPPPPPPP.rRRRR	(1 *1&2)
2890  *  scsiclass,DDEE.vVVVVVVVV.pPPPPPPPPPPPPPPPP.rRRRR	(2 *1)
2891  *  scsiclass,DDFFF.vVVVVVVVV.pPPPPPPPPPPPPPPPP.rRRRR	(3 *2)
2892  *  scsiclass,DD.vVVVVVVVV.pPPPPPPPPPPPPPPPP.rRRRR	(4)
2893  *  scsiclass,DDEEFFF.vVVVVVVVV.pPPPPPPPPPPPPPPPP	(5 *1&2)
2894  *  scsiclass,DDEE.vVVVVVVVV.pPPPPPPPPPPPPPPPP		(6 *1)
2895  *  scsiclass,DDFFF.vVVVVVVVV.pPPPPPPPPPPPPPPPP		(7 *2)
2896  *  scsiclass,DD.vVVVVVVVV.pPPPPPPPPPPPPPPPP		(8)
2897  *  scsa,DD.bBBBBBBBB					(8.5 *3)
2898  *  scsiclass,DDEEFFF					(9 *1&2)
2899  *  scsiclass,DDEE					(10 *1)
2900  *  scsiclass,DDFFF					(11 *2)
2901  *  scsiclass,DD					(12)
2902  *  scsa.fFFF						(12.5 *4)
2903  *  scsiclass						(13)
2904  *
2905  *	  *1 only produced on a secondary function node
2906  *	  *2 only produced when generic form-group flags exist.
2907  *	  *3 only produced when binding-set form-group legacy support is needed
2908  *	  *4 only produced when failover form-group flags exist.
2909  *
2910  *	where:
2911  *
2912  *	v                       is the letter 'v'. Denotest the
2913  *				beginning of VVVVVVVV.
2914  *
2915  *	VVVVVVVV                Translated scsi_vendor.
2916  *
2917  *	p                       is the letter 'p'. Denotes the
2918  *				beginning of PPPPPPPPPPPPPPPP.
2919  *
2920  *	PPPPPPPPPPPPPPPP	Translated scsi_product.
2921  *
2922  *	r                       is the letter 'r'. Denotes the
2923  *				beginning of RRRR.
2924  *
2925  *	RRRR                    Translated scsi_revision.
2926  *
2927  *	DD                      is a two digit ASCII hexadecimal
2928  *				number. The value of the two digits is
2929  *				based one the SCSI "Peripheral device
2930  *				type" command set associated with the
2931  *				node. On a primary node this is the
2932  *				scsi_dtype of the primary command set,
2933  *				on a secondary node this is the
2934  *				scsi_dtype associated with the embedded
2935  *				function command set.
2936  *
2937  *	EE                      Same encoding used for DD. This form is
2938  *				only generated on secondary function
2939  *				nodes. The DD function is embedded in
2940  *				an EE device.
2941  *
2942  *	FFF                     Concatenation, in alphabetical order,
2943  *				of the flag characters within a form-group.
2944  *				For a given form-group, the following
2945  *				flags are defined.
2946  *
2947  *				scsiclass: (generic form-group):
2948  *				  R	Removable_Media: Used when
2949  *					inq_rmb is set.
2950  *
2951  *				scsa.f:	(failover form-group):
2952  *				  E	Explicit Target_Port_Group: Used
2953  *					when inq_tpgse is set and 'G' is
2954  *					alse present.
2955  *				  G	GUID: Used when a GUID can be
2956  *					generated for the device.
2957  *				  I	Implicit Target_Port_Group: Used
2958  *					when inq_tpgs is set and 'G' is
2959  *					also present.
2960  *
2961  *				Forms using FFF are only be generated
2962  *				if there are applicable flag
2963  *				characters.
2964  *
2965  *	b                       is the letter 'b'. Denotes the
2966  *				beginning of BBBBBBBB.
2967  *
2968  *	BBBBBBBB                Binding-set. Operating System Specific:
2969  *				scsi-binding-set property of HBA.
2970  */
2971 #define	NCOMPAT		(1 + (13 + 2) + 1)
2972 #define	COMPAT_LONGEST	(strlen( \
2973 	"scsiclass,DDEEFFF.vVVVVVVVV.pPPPPPPPPPPPPPPPP.rRRRR" + 1))
2974 
2975 /*
2976  * Private version with extra device 'identity' arguments to allow code
2977  * to determine GUID FFF support.
2978  */
2979 static void
2980 scsi_hba_identity_nodename_compatible_get(struct scsi_inquiry *inq,
2981     uchar_t *inq80, size_t inq80len, uchar_t *inq83, size_t inq83len,
2982     char *binding_set, int dtype_node, char *compat0,
2983     char **nodenamep, char ***compatiblep, int *ncompatiblep)
2984 {
2985 	char		vid[sizeof (inq->inq_vid) + 1 ];
2986 	char		pid[sizeof (inq->inq_pid) + 1];
2987 	char		rev[sizeof (inq->inq_revision) + 1];
2988 	char		gf[sizeof ("R\0")];
2989 	char		ff[sizeof ("EGI\0")];
2990 	int		dtype_device;
2991 	int		ncompat;		/* number of compatible */
2992 	char		**compatp;		/* compatible ptrs */
2993 	int		i;
2994 	char		*nname;			/* nodename */
2995 	char		*dname;			/* driver name */
2996 	char		**csp;
2997 	char		*p;
2998 	int		tlen;
2999 	int		len;
3000 	major_t		major;
3001 	ddi_devid_t	devid;
3002 	char		*guid;
3003 
3004 	/*
3005 	 * Nodename_aliases: This table was originally designed to be
3006 	 * implemented via a new nodename_aliases file - a peer to the
3007 	 * driver_aliases that selects a nodename based on compatible
3008 	 * forms in much the same say driver_aliases is used to select
3009 	 * driver bindings from compatible forms. Each compatible form
3010 	 * is an 'alias'. Until a more general need for a
3011 	 * nodename_aliases file exists, which may never occur, the
3012 	 * scsi mappings are described here via a compiled in table.
3013 	 *
3014 	 * This table contains nodename mappings for self-identifying
3015 	 * scsi devices enumerated by the Solaris kernel. For a given
3016 	 * device, the highest precedence "compatible" form with a
3017 	 * mapping is used to select the nodename for the device. This
3018 	 * will typically be a generic nodename, however in some legacy
3019 	 * compatibility cases a driver nodename mapping may be selected.
3020 	 *
3021 	 * Because of possible breakage associated with switching SCSI
3022 	 * target devices from driver nodenames to generic nodenames,
3023 	 * we are currently unable to support generic nodenames for all
3024 	 * SCSI devices (binding-sets). Although /devices paths are
3025 	 * defined as unstable, avoiding possible breakage is
3026 	 * important. Some of the newer SCSI transports (USB) already
3027 	 * use generic nodenames. All new SCSI transports and target
3028 	 * devices should use generic nodenames. At times this decision
3029 	 * may be architecture dependent (sparc .vs. intel) based on when
3030 	 * a transport was supported on a particular architecture.
3031 	 *
3032 	 * We provide a base set of generic nodename mappings based on
3033 	 * scsiclass dtype and higher-precedence driver nodename
3034 	 * mappings based on scsa "binding-set" to cover legacy
3035 	 * issues. The binding-set is typically associated with
3036 	 * "scsi-binding-set" property value of the HBA. The legacy
3037 	 * mappings are provided independent of whether the driver they
3038 	 * refer to is installed. This allows a correctly named node
3039 	 * be created at discovery time, and binding to occur when/if
3040 	 * an add_drv of the legacy driver occurs.
3041 	 *
3042 	 * We also have mappings for legacy SUN hardware that
3043 	 * misidentifies itself (enclosure services which identify
3044 	 * themselves as processors). All future hardware should use
3045 	 * the correct dtype.
3046 	 *
3047 	 * As SCSI HBAs are modified to use the SCSA interfaces for
3048 	 * self-identifying SCSI target devices (PSARC/2004/116) the
3049 	 * nodename_aliases table (PSARC/2004/420) should be augmented
3050 	 * with legacy mappings in order to maintain compatibility with
3051 	 * existing /devices paths, especially for devices that house
3052 	 * an OS. Failure to do this may cause upgrade problems.
3053 	 * Additions for new target devices or transports should not
3054 	 * add scsa binding-set compatible mappings.
3055 	 */
3056 	static struct nodename_aliases {
3057 		char	*na_nodename;		/* nodename */
3058 		char	*na_alias;		/* compatible form match */
3059 	} na[] = {
3060 	/* # mapping to generic nodenames based on scsi dtype */
3061 		{"disk",		"scsiclass,00"},
3062 		{"tape",		"scsiclass,01"},
3063 		{"printer",		"scsiclass,02"},
3064 		{"processor",		"scsiclass,03"},
3065 		{"worm",		"scsiclass,04"},
3066 		{"cdrom",		"scsiclass,05"},
3067 		{"scanner",		"scsiclass,06"},
3068 		{"optical-disk",	"scsiclass,07"},
3069 		{"medium-changer",	"scsiclass,08"},
3070 		{"obsolete",		"scsiclass,09"},
3071 		{"prepress-a",		"scsiclass,0a"},
3072 		{"prepress-b",		"scsiclass,0b"},
3073 		{"array-controller",	"scsiclass,0c"},
3074 		{"enclosure",		"scsiclass,0d"},
3075 		{"disk",		"scsiclass,0e"},
3076 		{"card-reader",		"scsiclass,0f"},
3077 		{"bridge",		"scsiclass,10"},
3078 		{"object-store",	"scsiclass,11"},
3079 		{"reserved",		"scsiclass,12"},
3080 		{"reserved",		"scsiclass,13"},
3081 		{"reserved",		"scsiclass,14"},
3082 		{"reserved",		"scsiclass,15"},
3083 		{"reserved",		"scsiclass,16"},
3084 		{"reserved",		"scsiclass,17"},
3085 		{"reserved",		"scsiclass,18"},
3086 		{"reserved",		"scsiclass,19"},
3087 		{"reserved",		"scsiclass,1a"},
3088 		{"reserved",		"scsiclass,1b"},
3089 		{"reserved",		"scsiclass,1c"},
3090 		{"reserved",		"scsiclass,1d"},
3091 		{"well-known-lun",	"scsiclass,1e"},
3092 		{"unknown",		"scsiclass,1f"},
3093 
3094 #ifdef	sparc
3095 	/* # legacy mapping to driver nodenames for fcp binding-set */
3096 		{"ssd",			"scsa,00.bfcp"},
3097 		{"st",			"scsa,01.bfcp"},
3098 		{"sgen",		"scsa,08.bfcp"},
3099 		{"ses",			"scsa,0d.bfcp"},
3100 
3101 	/* # legacy mapping to driver nodenames for vhci binding-set */
3102 		{"ssd",			"scsa,00.bvhci"},
3103 		{"st",			"scsa,01.bvhci"},
3104 		{"sgen",		"scsa,08.bvhci"},
3105 		{"ses",			"scsa,0d.bvhci"},
3106 #else	/* sparc */
3107 	/* # for x86 fcp and vhci use generic nodenames */
3108 #endif	/* sparc */
3109 
3110 #ifdef  notdef
3111 	/*
3112 	 * The following binding-set specific mappings are not being
3113 	 * delivered at this time, but are listed here as an examples of
3114 	 * the type of mappings needed.
3115 	 */
3116 
3117 	/* # legacy mapping to driver nodenames for spi binding-set */
3118 		{"sd",			"scsa,00.bspi"},
3119 		{"sd",			"scsa,05.bspi"},
3120 		{"sd",			"scsa,07.bspi"},
3121 		{"st",			"scsa,01.bspi"},
3122 		{"ses",			"scsa,0d.bspi"},
3123 
3124 	/* #				SUN misidentified spi hardware */
3125 		{"ses",			"scsiclass,03.vSUN.pD2"},
3126 		{"ses",			"scsiclass,03.vSYMBIOS.pD1000"},
3127 
3128 	/* # legacy mapping to driver nodenames for atapi binding-set */
3129 		{"sd",			"scsa,00.batapi"},
3130 		{"sd",			"scsa,05.batapi"},
3131 		{"sd",			"scsa,07.batapi"},
3132 		{"st",			"scsa,01.batapi"},
3133 		{"unknown",		"scsa,0d.batapi"},
3134 
3135 	/* # legacy mapping to generic nodenames for usb binding-set */
3136 		{"disk",		"scsa,05.busb"},
3137 		{"disk",		"scsa,07.busb"},
3138 		{"changer",		"scsa,08.busb"},
3139 		{"comm",		"scsa,09.busb"},
3140 		{"array_ctlr",		"scsa,0c.busb"},
3141 		{"esi",			"scsa,0d.busb"},
3142 #endif  /* notdef */
3143 
3144 	/*
3145 	 * mapping nodenames for mpt based on scsi dtype
3146 	 * for being compatible with the original node names
3147 	 * under mpt controller
3148 	 */
3149 		{"sd",			"scsa,00.bmpt"},
3150 		{"sd",			"scsa,05.bmpt"},
3151 		{"sd",			"scsa,07.bmpt"},
3152 		{"st",			"scsa,01.bmpt"},
3153 		{"ses",			"scsa,0d.bmpt"},
3154 		{"sgen",		"scsa,08.bmpt"},
3155 		{NULL,		NULL}
3156 	};
3157 	struct nodename_aliases *nap;
3158 
3159 	ASSERT(nodenamep && compatiblep && ncompatiblep &&
3160 	    (binding_set == NULL || (strlen(binding_set) <= 8)));
3161 	if ((nodenamep == NULL) || (compatiblep == NULL) ||
3162 	    (ncompatiblep == NULL))
3163 		return;
3164 
3165 	/*
3166 	 * In order to reduce runtime we allocate one block of memory that
3167 	 * contains both the NULL terminated array of pointers to compatible
3168 	 * forms and the individual compatible strings. This block is
3169 	 * somewhat larger than needed, but is short lived - it only exists
3170 	 * until the caller can transfer the information into the "compatible"
3171 	 * string array property and call scsi_hba_nodename_compatible_free.
3172 	 */
3173 	tlen = NCOMPAT * COMPAT_LONGEST;
3174 	compatp = kmem_alloc((NCOMPAT * sizeof (char *)) + tlen, KM_SLEEP);
3175 
3176 	/* convert inquiry data from SCSI ASCII to 1275 string */
3177 	(void) string_scsi_to_1275(vid, inq->inq_vid,
3178 	    sizeof (inq->inq_vid));
3179 	(void) string_scsi_to_1275(pid, inq->inq_pid,
3180 	    sizeof (inq->inq_pid));
3181 	(void) string_scsi_to_1275(rev, inq->inq_revision,
3182 	    sizeof (inq->inq_revision));
3183 	ASSERT((strlen(vid) <= sizeof (inq->inq_vid)) &&
3184 	    (strlen(pid) <= sizeof (inq->inq_pid)) &&
3185 	    (strlen(rev) <= sizeof (inq->inq_revision)));
3186 
3187 	/*
3188 	 * Form flags in ***ALPHABETICAL*** order within form-group:
3189 	 *
3190 	 * NOTE: When adding a new flag to an existing form-group, carefull
3191 	 * consideration must be given to not breaking existing bindings
3192 	 * based on that form-group.
3193 	 */
3194 
3195 	/*
3196 	 * generic form-group flags
3197 	 *   R	removable:
3198 	 *	Set when inq_rmb is set and for well known scsi dtypes. For a
3199 	 *	bus where the entire device is removable (like USB), we expect
3200 	 *	the HBA to intercept the inquiry data and set inq_rmb.
3201 	 *	Since OBP does not distinguish removable media in its generic
3202 	 *	name selection we avoid setting the 'R' flag if the root is not
3203 	 *	yet mounted.
3204 	 */
3205 	i = 0;
3206 	dtype_device = inq->inq_dtype & DTYPE_MASK;
3207 	if (rootvp && (inq->inq_rmb ||
3208 	    (dtype_device == DTYPE_WORM) ||
3209 	    (dtype_device == DTYPE_RODIRECT) ||
3210 	    (dtype_device == DTYPE_OPTICAL)))
3211 		gf[i++] = 'R';			/* removable */
3212 	gf[i] = '\0';
3213 
3214 	/*
3215 	 * failover form-group flags
3216 	 *   E	Explicit Target_Port_Group_Supported:
3217 	 *	Set for a device that has a GUID if inq_tpgse also set.
3218 	 *   G	GUID:
3219 	 *	Set when we have identity information, can determine a devid
3220 	 *	from the identity information, and can generate a guid from
3221 	 *	that devid.
3222 	 *   I	Implicit Target_Port_Group_Supported:
3223 	 *	Set for a device that has a GUID if inq_tpgs also set.
3224 	 */
3225 	i = 0;
3226 	if ((inq80 || inq83) &&
3227 	    (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST, NULL,
3228 	    (uchar_t *)inq, sizeof (*inq), inq80, inq80len, inq83, inq83len,
3229 	    &devid) == DDI_SUCCESS)) {
3230 		guid = ddi_devid_to_guid(devid);
3231 		ddi_devid_free(devid);
3232 	} else
3233 		guid = NULL;
3234 	if (guid && (inq->inq_tpgs & TPGS_FAILOVER_EXPLICIT))
3235 		ff[i++] = 'E';			/* EXPLICIT TPGS */
3236 	if (guid)
3237 		ff[i++] = 'G';			/* GUID */
3238 	if (guid && (inq->inq_tpgs & TPGS_FAILOVER_IMPLICIT))
3239 		ff[i++] = 'I';			/* IMPLICIT TPGS */
3240 	ff[i] = '\0';
3241 	if (guid)
3242 		ddi_devid_free_guid(guid);
3243 
3244 	/*
3245 	 * Construct all applicable compatible forms. See comment at the
3246 	 * head of the function for a description of the compatible forms.
3247 	 */
3248 	csp = compatp;
3249 	p = (char *)(compatp + NCOMPAT);
3250 
3251 	/* ( 0) driver (optional, not documented in scsi(4)) */
3252 	if (compat0) {
3253 		*csp++ = p;
3254 		(void) snprintf(p, tlen, "%s", compat0);
3255 		len = strlen(p) + 1;
3256 		p += len;
3257 		tlen -= len;
3258 	}
3259 
3260 	/* ( 1) scsiclass,DDEEFFF.vV.pP.rR */
3261 	if ((dtype_device != dtype_node) && *gf && *vid && *pid && *rev) {
3262 		*csp++ = p;
3263 		(void) snprintf(p, tlen, "scsiclass,%02x%02x%s.v%s.p%s.r%s",
3264 		    dtype_node, dtype_device, gf, vid, pid, rev);
3265 		len = strlen(p) + 1;
3266 		p += len;
3267 		tlen -= len;
3268 	}
3269 
3270 	/* ( 2) scsiclass,DDEE.vV.pP.rR */
3271 	if ((dtype_device != dtype_node) && *vid && *pid && *rev) {
3272 		*csp++ = p;
3273 		(void) snprintf(p, tlen, "scsiclass,%02x%02x.v%s.p%s.r%s",
3274 		    dtype_node, dtype_device, vid, pid, rev);
3275 		len = strlen(p) + 1;
3276 		p += len;
3277 		tlen -= len;
3278 	}
3279 
3280 	/* ( 3) scsiclass,DDFFF.vV.pP.rR */
3281 	if (*gf && *vid && *pid && *rev) {
3282 		*csp++ = p;
3283 		(void) snprintf(p, tlen, "scsiclass,%02x%s.v%s.p%s.r%s",
3284 		    dtype_node, gf, vid, pid, rev);
3285 		len = strlen(p) + 1;
3286 		p += len;
3287 		tlen -= len;
3288 	}
3289 
3290 	/* ( 4) scsiclass,DD.vV.pP.rR */
3291 	if (*vid && *pid && rev) {
3292 		*csp++ = p;
3293 		(void) snprintf(p, tlen, "scsiclass,%02x.v%s.p%s.r%s",
3294 		    dtype_node, vid, pid, rev);
3295 		len = strlen(p) + 1;
3296 		p += len;
3297 		tlen -= len;
3298 	}
3299 
3300 	/* ( 5) scsiclass,DDEEFFF.vV.pP */
3301 	if ((dtype_device != dtype_node) && *gf && *vid && *pid) {
3302 		*csp++ = p;
3303 		(void) snprintf(p, tlen, "scsiclass,%02x%02x%s.v%s.p%s",
3304 		    dtype_node, dtype_device, gf, vid, pid);
3305 		len = strlen(p) + 1;
3306 		p += len;
3307 		tlen -= len;
3308 	}
3309 
3310 	/* ( 6) scsiclass,DDEE.vV.pP */
3311 	if ((dtype_device != dtype_node) && *vid && *pid) {
3312 		*csp++ = p;
3313 		(void) snprintf(p, tlen, "scsiclass,%02x%02x.v%s.p%s",
3314 		    dtype_node, dtype_device, vid, pid);
3315 		len = strlen(p) + 1;
3316 		p += len;
3317 		tlen -= len;
3318 	}
3319 
3320 	/* ( 7) scsiclass,DDFFF.vV.pP */
3321 	if (*gf && *vid && *pid) {
3322 		*csp++ = p;
3323 		(void) snprintf(p, tlen, "scsiclass,%02x%s.v%s.p%s",
3324 		    dtype_node, gf, vid, pid);
3325 		len = strlen(p) + 1;
3326 		p += len;
3327 		tlen -= len;
3328 	}
3329 
3330 	/* ( 8) scsiclass,DD.vV.pP */
3331 	if (*vid && *pid) {
3332 		*csp++ = p;
3333 		(void) snprintf(p, tlen, "scsiclass,%02x.v%s.p%s",
3334 		    dtype_node, vid, pid);
3335 		len = strlen(p) + 1;
3336 		p += len;
3337 		tlen -= len;
3338 	}
3339 
3340 	/* (8.5) scsa,DD.bB (not documented in scsi(4)) */
3341 	if (binding_set) {
3342 		*csp++ = p;
3343 		(void) snprintf(p, tlen, "scsa,%02x.b%s",
3344 		    dtype_node, binding_set);
3345 		len = strlen(p) + 1;
3346 		p += len;
3347 		tlen -= len;
3348 	}
3349 
3350 	/* ( 9) scsiclass,DDEEFFF */
3351 	if ((dtype_device != dtype_node) && *gf) {
3352 		*csp++ = p;
3353 		(void) snprintf(p, tlen, "scsiclass,%02x%02x%s",
3354 		    dtype_node, dtype_device, gf);
3355 		len = strlen(p) + 1;
3356 		p += len;
3357 		tlen -= len;
3358 	}
3359 
3360 	/* (10) scsiclass,DDEE */
3361 	if (dtype_device != dtype_node) {
3362 		*csp++ = p;
3363 		(void) snprintf(p, tlen, "scsiclass,%02x%02x",
3364 		    dtype_node, dtype_device);
3365 		len = strlen(p) + 1;
3366 		p += len;
3367 		tlen -= len;
3368 	}
3369 
3370 	/* (11) scsiclass,DDFFF */
3371 	if (*gf) {
3372 		*csp++ = p;
3373 		(void) snprintf(p, tlen, "scsiclass,%02x%s",
3374 		    dtype_node, gf);
3375 		len = strlen(p) + 1;
3376 		p += len;
3377 		tlen -= len;
3378 	}
3379 
3380 	/* (12) scsiclass,DD */
3381 	*csp++ = p;
3382 	(void) snprintf(p, tlen, "scsiclass,%02x", dtype_node);
3383 	len = strlen(p) + 1;
3384 	p += len;
3385 	tlen -= len;
3386 
3387 	/* (12.5) scsa.fFFF */
3388 	if (*ff) {
3389 		*csp++ = p;
3390 		(void) snprintf(p, tlen, "scsa.f%s", ff);
3391 		len = strlen(p) + 1;
3392 		p += len;
3393 		tlen -= len;
3394 	}
3395 
3396 	/* (13) scsiclass */
3397 	*csp++ = p;
3398 	(void) snprintf(p, tlen, "scsiclass");
3399 	len = strlen(p) + 1;
3400 	p += len;
3401 	tlen -= len;
3402 	ASSERT(tlen >= 0);
3403 
3404 	*csp = NULL;			/* NULL terminate array of pointers */
3405 	ncompat = csp - compatp;
3406 
3407 	/*
3408 	 * When determining a nodename, a nodename_aliases specified
3409 	 * mapping has precedence over using a driver_aliases specified
3410 	 * driver binding as a nodename.
3411 	 *
3412 	 * See if any of the compatible forms have a nodename_aliases
3413 	 * specified nodename. These mappings are described by
3414 	 * nodename_aliases entries like:
3415 	 *
3416 	 *	disk		"scsiclass,00"
3417 	 *	enclosure	"scsiclass,03.vSYMBIOS.pD1000"
3418 	 *	ssd		"scsa,00.bfcp"
3419 	 *
3420 	 * All nodename_aliases mappings should idealy be to generic
3421 	 * names, however a higher precedence legacy mapping to a
3422 	 * driver name may exist. The highest precedence mapping
3423 	 * provides the nodename, so legacy driver nodename mappings
3424 	 * (if they exist) take precedence over generic nodename
3425 	 * mappings.
3426 	 */
3427 	for (nname = NULL, csp = compatp; (nname == NULL) && *csp; csp++) {
3428 		for (nap = na; nap->na_nodename; nap++) {
3429 			if (strcmp(*csp, nap->na_alias) == 0) {
3430 				nname = nap->na_nodename;
3431 				break;
3432 			}
3433 		}
3434 	}
3435 
3436 	/*
3437 	 * If no nodename_aliases mapping exists then use the
3438 	 * driver_aliases specified driver binding as a nodename.
3439 	 * Determine the driver based on compatible (which may
3440 	 * have the passed in compat0 as the first item). The
3441 	 * driver_aliases file has entries like
3442 	 *
3443 	 *	sd	"scsiclass,00"
3444 	 *
3445 	 * that map compatible forms to specific drivers. These
3446 	 * entries are established by add_drv. We use the most specific
3447 	 * driver binding as the nodename. This matches the eventual
3448 	 * ddi_driver_compatible_major() binding that will be
3449 	 * established by bind_node()
3450 	 */
3451 	if (nname == NULL) {
3452 		for (dname = NULL, csp = compatp; *csp; csp++) {
3453 			major = ddi_name_to_major(*csp);
3454 			if ((major == (major_t)-1) ||
3455 			    (devnamesp[major].dn_flags & DN_DRIVER_REMOVED))
3456 				continue;
3457 			if (dname = ddi_major_to_name(major))
3458 				break;
3459 		}
3460 		nname = dname;
3461 	}
3462 
3463 	/* return results */
3464 	if (nname) {
3465 		*nodenamep = kmem_alloc(strlen(nname) + 1, KM_SLEEP);
3466 		(void) strcpy(*nodenamep, nname);
3467 	} else {
3468 		*nodenamep = NULL;
3469 
3470 		/*
3471 		 * If no nodename could be determined return a special
3472 		 * 'compatible' to be used for a diagnostic message. This
3473 		 * compatible contains all compatible forms concatenated
3474 		 * into a single string pointed to by the first element.
3475 		 */
3476 		if (nname == NULL) {
3477 			for (csp = compatp; *(csp + 1); csp++)
3478 				*((*csp) + strlen(*csp)) = ' ';
3479 			*(compatp + 1) = NULL;
3480 			ncompat = 1;
3481 		}
3482 
3483 	}
3484 	*compatiblep = compatp;
3485 	*ncompatiblep = ncompat;
3486 }
3487 
3488 void
3489 scsi_hba_nodename_compatible_get(struct scsi_inquiry *inq,
3490     char *binding_set, int dtype_node, char *compat0,
3491     char **nodenamep, char ***compatiblep, int *ncompatiblep)
3492 {
3493 	scsi_hba_identity_nodename_compatible_get(inq,
3494 	    NULL, 0, NULL, 0, binding_set, dtype_node, compat0, nodenamep,
3495 	    compatiblep, ncompatiblep);
3496 }
3497 
3498 /*
3499  * Free allocations associated with scsi_hba_nodename_compatible_get or
3500  * scsi_hba_identity_nodename_compatible_get use.
3501  */
3502 void
3503 scsi_hba_nodename_compatible_free(char *nodename, char **compatible)
3504 {
3505 	if (nodename)
3506 		kmem_free(nodename, strlen(nodename) + 1);
3507 
3508 	if (compatible)
3509 		kmem_free(compatible, (NCOMPAT * sizeof (char *)) +
3510 		    (NCOMPAT * COMPAT_LONGEST));
3511 }
3512 
3513 /* scsi_device property interfaces */
3514 #define	_TYPE_DEFINED(flags)						\
3515 	(((flags & SCSI_DEVICE_PROP_TYPE_MSK) == SCSI_DEVICE_PROP_PATH) || \
3516 	((flags & SCSI_DEVICE_PROP_TYPE_MSK) == SCSI_DEVICE_PROP_DEVICE))
3517 
3518 #define	_DEVICE_PIP(sd, flags)						\
3519 	((((flags & SCSI_DEVICE_PROP_TYPE_MSK) == SCSI_DEVICE_PROP_PATH) && \
3520 	sd->sd_pathinfo) ? (mdi_pathinfo_t *)sd->sd_pathinfo : NULL)
3521 
3522 /* return the unit_address associated with a scsi_device */
3523 char *
3524 scsi_device_unit_address(struct scsi_device *sd)
3525 {
3526 	mdi_pathinfo_t	*pip;
3527 
3528 	ASSERT(sd && sd->sd_dev);
3529 	if ((sd == NULL) || (sd->sd_dev == NULL))
3530 		return (NULL);
3531 
3532 	pip = _DEVICE_PIP(sd, SCSI_DEVICE_PROP_PATH);
3533 	if (pip)
3534 		return (mdi_pi_get_addr(pip));
3535 	else
3536 		return (ddi_get_name_addr(sd->sd_dev));
3537 }
3538 
3539 int
3540 scsi_device_prop_get_int(struct scsi_device *sd, uint_t flags,
3541     char *name, int defval)
3542 {
3543 	mdi_pathinfo_t	*pip;
3544 	int		v = defval;
3545 	int		data;
3546 	int		rv;
3547 
3548 	ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
3549 	if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
3550 	    !_TYPE_DEFINED(flags))
3551 		return (v);
3552 
3553 	pip = _DEVICE_PIP(sd, flags);
3554 	if (pip) {
3555 		rv = mdi_prop_lookup_int(pip, name, &data);
3556 		if (rv == DDI_PROP_SUCCESS)
3557 			v = data;
3558 	} else
3559 		v = ddi_prop_get_int(DDI_DEV_T_ANY, sd->sd_dev,
3560 		    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, name, v);
3561 	return (v);
3562 }
3563 
3564 
3565 int64_t
3566 scsi_device_prop_get_int64(struct scsi_device *sd, uint_t flags,
3567     char *name, int64_t defval)
3568 {
3569 	mdi_pathinfo_t	*pip;
3570 	int64_t		v = defval;
3571 	int64_t		data;
3572 	int		rv;
3573 
3574 	ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
3575 	if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
3576 	    !_TYPE_DEFINED(flags))
3577 		return (v);
3578 
3579 	pip = _DEVICE_PIP(sd, flags);
3580 	if (pip) {
3581 		rv = mdi_prop_lookup_int64(pip, name, &data);
3582 		if (rv == DDI_PROP_SUCCESS)
3583 			v = data;
3584 	} else
3585 		v = ddi_prop_get_int64(DDI_DEV_T_ANY, sd->sd_dev,
3586 		    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, name, v);
3587 	return (v);
3588 }
3589 
3590 int
3591 scsi_device_prop_lookup_byte_array(struct scsi_device *sd, uint_t flags,
3592     char *name, uchar_t **data, uint_t *nelements)
3593 {
3594 	mdi_pathinfo_t	*pip;
3595 	int		rv;
3596 
3597 	ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
3598 	if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
3599 	    !_TYPE_DEFINED(flags))
3600 		return (DDI_PROP_INVAL_ARG);
3601 
3602 	pip = _DEVICE_PIP(sd, flags);
3603 	if (pip)
3604 		rv = mdi_prop_lookup_byte_array(pip, name, data, nelements);
3605 	else
3606 		rv = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, sd->sd_dev,
3607 		    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
3608 		    name, data, nelements);
3609 	return (rv);
3610 }
3611 
3612 int
3613 scsi_device_prop_lookup_int_array(struct scsi_device *sd, uint_t flags,
3614     char *name, int **data, uint_t *nelements)
3615 {
3616 	mdi_pathinfo_t	*pip;
3617 	int		rv;
3618 
3619 	ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
3620 	if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
3621 	    !_TYPE_DEFINED(flags))
3622 		return (DDI_PROP_INVAL_ARG);
3623 
3624 	pip = _DEVICE_PIP(sd, flags);
3625 	if (pip)
3626 		rv = mdi_prop_lookup_int_array(pip, name, data, nelements);
3627 	else
3628 		rv = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, sd->sd_dev,
3629 		    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
3630 		    name, data, nelements);
3631 	return (rv);
3632 }
3633 
3634 
3635 int
3636 scsi_device_prop_lookup_string(struct scsi_device *sd, uint_t flags,
3637     char *name, char **data)
3638 {
3639 	mdi_pathinfo_t	*pip;
3640 	int		rv;
3641 
3642 	ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
3643 	if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
3644 	    !_TYPE_DEFINED(flags))
3645 		return (DDI_PROP_INVAL_ARG);
3646 
3647 	pip = _DEVICE_PIP(sd, flags);
3648 	if (pip)
3649 		rv = mdi_prop_lookup_string(pip, name, data);
3650 	else
3651 		rv = ddi_prop_lookup_string(DDI_DEV_T_ANY, sd->sd_dev,
3652 		    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
3653 		    name, data);
3654 	return (rv);
3655 }
3656 
3657 int
3658 scsi_device_prop_lookup_string_array(struct scsi_device *sd, uint_t flags,
3659     char *name, char ***data, uint_t *nelements)
3660 {
3661 	mdi_pathinfo_t	*pip;
3662 	int		rv;
3663 
3664 	ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
3665 	if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
3666 	    !_TYPE_DEFINED(flags))
3667 		return (DDI_PROP_INVAL_ARG);
3668 
3669 	pip = _DEVICE_PIP(sd, flags);
3670 	if (pip)
3671 		rv = mdi_prop_lookup_string_array(pip, name, data, nelements);
3672 	else
3673 		rv = ddi_prop_lookup_string_array(DDI_DEV_T_ANY, sd->sd_dev,
3674 		    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
3675 		    name, data, nelements);
3676 	return (rv);
3677 }
3678 
3679 int
3680 scsi_device_prop_update_byte_array(struct scsi_device *sd, uint_t flags,
3681     char *name, uchar_t *data, uint_t nelements)
3682 {
3683 	mdi_pathinfo_t	*pip;
3684 	int		rv;
3685 
3686 	ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
3687 	if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
3688 	    !_TYPE_DEFINED(flags))
3689 		return (DDI_PROP_INVAL_ARG);
3690 
3691 	pip = _DEVICE_PIP(sd, flags);
3692 	if (pip)
3693 		rv = mdi_prop_update_byte_array(pip, name, data, nelements);
3694 	else
3695 		rv = ndi_prop_update_byte_array(DDI_DEV_T_NONE, sd->sd_dev,
3696 		    name, data, nelements);
3697 	return (rv);
3698 }
3699 
3700 int
3701 scsi_device_prop_update_int(struct scsi_device *sd, uint_t flags,
3702     char *name, int data)
3703 {
3704 	mdi_pathinfo_t	*pip;
3705 	int		rv;
3706 
3707 	ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
3708 	if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
3709 	    !_TYPE_DEFINED(flags))
3710 		return (DDI_PROP_INVAL_ARG);
3711 
3712 	pip = _DEVICE_PIP(sd, flags);
3713 	if (pip)
3714 		rv = mdi_prop_update_int(pip, name, data);
3715 	else
3716 		rv = ndi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
3717 		    name, data);
3718 	return (rv);
3719 }
3720 
3721 int
3722 scsi_device_prop_update_int64(struct scsi_device *sd, uint_t flags,
3723     char *name, int64_t data)
3724 {
3725 	mdi_pathinfo_t	*pip;
3726 	int		rv;
3727 
3728 	ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
3729 	if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
3730 	    !_TYPE_DEFINED(flags))
3731 		return (DDI_PROP_INVAL_ARG);
3732 
3733 	pip = _DEVICE_PIP(sd, flags);
3734 	if (pip)
3735 		rv = mdi_prop_update_int64(pip, name, data);
3736 	else
3737 		rv = ndi_prop_update_int64(DDI_DEV_T_NONE, sd->sd_dev,
3738 		    name, data);
3739 	return (rv);
3740 }
3741 
3742 int
3743 scsi_device_prop_update_int_array(struct scsi_device *sd, uint_t flags,
3744     char *name, int *data, uint_t nelements)
3745 {
3746 	mdi_pathinfo_t	*pip;
3747 	int		rv;
3748 
3749 	ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
3750 	if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
3751 	    !_TYPE_DEFINED(flags))
3752 		return (DDI_PROP_INVAL_ARG);
3753 
3754 	pip = _DEVICE_PIP(sd, flags);
3755 	if (pip)
3756 		rv = mdi_prop_update_int_array(pip, name, data, nelements);
3757 	else
3758 		rv = ndi_prop_update_int_array(DDI_DEV_T_NONE, sd->sd_dev,
3759 		    name, data, nelements);
3760 	return (rv);
3761 }
3762 
3763 int
3764 scsi_device_prop_update_string(struct scsi_device *sd, uint_t flags,
3765     char *name, char *data)
3766 {
3767 	mdi_pathinfo_t	*pip;
3768 	int		rv;
3769 
3770 	ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
3771 	if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
3772 	    !_TYPE_DEFINED(flags))
3773 		return (DDI_PROP_INVAL_ARG);
3774 
3775 	pip = _DEVICE_PIP(sd, flags);
3776 	if (pip)
3777 		rv = mdi_prop_update_string(pip, name, data);
3778 	else
3779 		rv = ndi_prop_update_string(DDI_DEV_T_NONE, sd->sd_dev,
3780 		    name, data);
3781 	return (rv);
3782 }
3783 
3784 int
3785 scsi_device_prop_update_string_array(struct scsi_device *sd, uint_t flags,
3786     char *name, char **data, uint_t nelements)
3787 {
3788 	mdi_pathinfo_t	*pip;
3789 	int		rv;
3790 
3791 	ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
3792 	if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
3793 	    !_TYPE_DEFINED(flags))
3794 		return (DDI_PROP_INVAL_ARG);
3795 
3796 	pip = _DEVICE_PIP(sd, flags);
3797 	if (pip)
3798 		rv = mdi_prop_update_string_array(pip, name, data, nelements);
3799 	else
3800 		rv = ndi_prop_update_string_array(DDI_DEV_T_NONE, sd->sd_dev,
3801 		    name, data, nelements);
3802 	return (rv);
3803 }
3804 
3805 int
3806 scsi_device_prop_remove(struct scsi_device *sd, uint_t flags, char *name)
3807 {
3808 	mdi_pathinfo_t	*pip;
3809 	int		rv;
3810 
3811 	ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
3812 	if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
3813 	    !_TYPE_DEFINED(flags))
3814 		return (DDI_PROP_INVAL_ARG);
3815 
3816 	pip = _DEVICE_PIP(sd, flags);
3817 	if (pip)
3818 		rv = mdi_prop_remove(pip, name);
3819 	else
3820 		rv = ndi_prop_remove(DDI_DEV_T_NONE, sd->sd_dev, name);
3821 	return (rv);
3822 }
3823 
3824 void
3825 scsi_device_prop_free(struct scsi_device *sd, uint_t flags, void *data)
3826 {
3827 	mdi_pathinfo_t	*pip;
3828 
3829 	ASSERT(sd && data && sd->sd_dev && _TYPE_DEFINED(flags));
3830 	if ((sd == NULL) || (data == NULL) || (sd->sd_dev == NULL) ||
3831 	    !_TYPE_DEFINED(flags))
3832 		return;
3833 
3834 	pip = _DEVICE_PIP(sd, flags);
3835 	if (pip)
3836 		(void) mdi_prop_free(data);
3837 	else
3838 		ddi_prop_free(data);
3839 }
3840 
3841 /*ARGSUSED*/
3842 /*
3843  * Search/create the specified iport node
3844  */
3845 static dev_info_t *
3846 scsi_hba_bus_config_port(dev_info_t *self, char *nameaddr)
3847 {
3848 	dev_info_t	*child;
3849 	char		*mcompatible, *addr;
3850 
3851 	/*
3852 	 * See if the iport node already exists.
3853 	 */
3854 
3855 	if (child = ndi_devi_findchild(self, nameaddr)) {
3856 		return (child);
3857 	}
3858 
3859 	/* allocate and initialize a new "iport" node */
3860 	ndi_devi_alloc_sleep(self, "iport", DEVI_SID_NODEID, &child);
3861 	ASSERT(child);
3862 	/*
3863 	 * Set the flavor of the child to be IPORT flavored
3864 	 */
3865 	ndi_flavor_set(child, SCSA_FLAVOR_IPORT);
3866 
3867 	/*
3868 	 * Add the "scsi-iport" addressing property for this child. This
3869 	 * property is used to identify a iport node, and to represent the
3870 	 * nodes @addr form via node properties.
3871 	 *
3872 	 * Add "compatible" property to the "scsi-iport" node to cause it bind
3873 	 * to the same driver as the HBA  driver.
3874 	 *
3875 	 * Give the HBA a chance, via tran_set_name_prop, to set additional
3876 	 * iport node properties or to change the "compatible" binding
3877 	 * prior to init_child.
3878 	 *
3879 	 * NOTE: the order of these operations is important so that
3880 	 * scsi_hba_iport works when called.
3881 	 */
3882 	mcompatible = ddi_binding_name(self);
3883 	addr = nameaddr + strlen("iport@");
3884 
3885 	if ((ndi_prop_update_string(DDI_DEV_T_NONE, child,
3886 	    "scsi-iport", addr) != DDI_PROP_SUCCESS) ||
3887 	    (ndi_prop_update_string_array(DDI_DEV_T_NONE, child,
3888 	    "compatible", &mcompatible, 1) != DDI_PROP_SUCCESS) ||
3889 	    ddi_pathname_obp_set(child, NULL) != DDI_SUCCESS) {
3890 		SCSI_HBA_LOG((_LOG(WARN), self, NULL,
3891 		    "scsi_hba_bus_config_port:%s failed dynamic decoration",
3892 		    nameaddr));
3893 		(void) ddi_remove_child(child, 0);
3894 		child = NULL;
3895 	} else {
3896 		if (ddi_initchild(self, child) != DDI_SUCCESS) {
3897 			ndi_prop_remove_all(child);
3898 			(void) ndi_devi_free(child);
3899 			child = NULL;
3900 		}
3901 	}
3902 
3903 	return (child);
3904 }
3905 
3906 #ifdef	sparc
3907 /* ARGSUSED */
3908 static int
3909 scsi_hba_bus_config_prom_node(dev_info_t *self, uint_t flags,
3910     void *arg, dev_info_t **childp)
3911 {
3912 	char		**iports;
3913 	int		circ, i;
3914 	int		ret = NDI_FAILURE;
3915 	unsigned int	num_iports = 0;
3916 	dev_info_t	*pdip = NULL;
3917 	char		*addr = NULL;
3918 
3919 	/* check to see if this is an HBA that defined scsi iports */
3920 	ret = ddi_prop_lookup_string_array(DDI_DEV_T_ANY, self,
3921 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "scsi-iports", &iports,
3922 	    &num_iports);
3923 
3924 	if (ret != DDI_SUCCESS) {
3925 		return (ret);
3926 	}
3927 
3928 	ASSERT(num_iports > 0);
3929 
3930 	addr = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
3931 
3932 	ret = NDI_FAILURE;
3933 
3934 	ndi_devi_enter(self, &circ);
3935 
3936 	/* create iport nodes for each scsi port/bus */
3937 	for (i = 0; i < num_iports; i++) {
3938 		bzero(addr, SCSI_MAXNAMELEN);
3939 		/* Prepend the iport name */
3940 		(void) snprintf(addr, SCSI_MAXNAMELEN, "iport@%s",
3941 		    iports[i]);
3942 		if (pdip = scsi_hba_bus_config_port(self, addr)) {
3943 			if (ndi_busop_bus_config(self, NDI_NO_EVENT,
3944 			    BUS_CONFIG_ONE, addr, &pdip, 0) !=
3945 			    NDI_SUCCESS) {
3946 				continue;
3947 			}
3948 			/*
3949 			 * Try to configure child under iport see wehter
3950 			 * request node is the child of the iport node
3951 			 */
3952 			if (ndi_devi_config_one(pdip, arg, childp,
3953 			    NDI_NO_EVENT) == NDI_SUCCESS) {
3954 				ret = NDI_SUCCESS;
3955 				break;
3956 			}
3957 		}
3958 	}
3959 
3960 	ndi_devi_exit(self, circ);
3961 
3962 	kmem_free(addr, SCSI_MAXNAMELEN);
3963 
3964 	ddi_prop_free(iports);
3965 
3966 	return (ret);
3967 }
3968 #endif
3969 
3970 /*
3971  * Perform iport port/bus bus_config.
3972  */
3973 static int
3974 scsi_hba_bus_config_iports(dev_info_t *self, uint_t flags,
3975     ddi_bus_config_op_t op, void *arg, dev_info_t **childp)
3976 {
3977 	char		*nameaddr, *addr;
3978 	char		**iports;
3979 	int		circ, i;
3980 	int		ret = NDI_FAILURE;
3981 	unsigned int	num_iports = 0;
3982 
3983 	/* check to see if this is an HBA that defined scsi iports */
3984 	ret = ddi_prop_lookup_string_array(DDI_DEV_T_ANY, self,
3985 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "scsi-iports", &iports,
3986 	    &num_iports);
3987 
3988 	if (ret != DDI_SUCCESS) {
3989 		return (ret);
3990 	}
3991 
3992 	ASSERT(num_iports > 0);
3993 
3994 	ndi_devi_enter(self, &circ);
3995 
3996 	switch (op) {
3997 	case BUS_CONFIG_ONE:
3998 		/* return if this operation is not against an iport node */
3999 		nameaddr = (char *)arg;
4000 		if ((nameaddr == NULL) ||
4001 		    (strncmp(nameaddr, "iport@", strlen("iport@")) != 0)) {
4002 			ret = NDI_FAILURE;
4003 			ndi_devi_exit(self, circ);
4004 			return (ret);
4005 		}
4006 
4007 		/*
4008 		 * parse the port number from "iport@%x"
4009 		 * XXX use atoi (hex)
4010 		 */
4011 		addr = nameaddr + strlen("iport@");
4012 
4013 		/* check to see if this port was registered */
4014 		for (i = 0; i < num_iports; i++) {
4015 			if (strcmp((iports[i]), addr) == 0)
4016 				break;
4017 		}
4018 
4019 		if (i == num_iports) {
4020 			ret = NDI_FAILURE;
4021 			break;
4022 		}
4023 
4024 		/* create the iport node */
4025 		if (scsi_hba_bus_config_port(self, nameaddr)) {
4026 			ret = NDI_SUCCESS;
4027 		}
4028 		break;
4029 	case BUS_CONFIG_ALL:
4030 	case BUS_CONFIG_DRIVER:
4031 		addr = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
4032 		/* create iport nodes for each scsi port/bus */
4033 		for (i = 0; i < num_iports; i++) {
4034 			bzero(addr, SCSI_MAXNAMELEN);
4035 			/* Prepend the iport name */
4036 			(void) snprintf(addr, SCSI_MAXNAMELEN, "iport@%s",
4037 			    iports[i]);
4038 			(void) scsi_hba_bus_config_port(self, addr);
4039 		}
4040 
4041 		kmem_free(addr, SCSI_MAXNAMELEN);
4042 		ret = NDI_SUCCESS;
4043 		break;
4044 	}
4045 	if (ret == NDI_SUCCESS) {
4046 #ifdef sparc
4047 		/*
4048 		 * Mask NDI_PROMNAME since PROM doesn't have iport
4049 		 * node at all.
4050 		 */
4051 		flags &= (~NDI_PROMNAME);
4052 #endif
4053 		ret = ndi_busop_bus_config(self, flags, op,
4054 		    arg, childp, 0);
4055 	}
4056 	ndi_devi_exit(self, circ);
4057 
4058 	ddi_prop_free(iports);
4059 
4060 	return (ret);
4061 }
4062 
4063 
4064 /*ARGSUSED*/
4065 static int
4066 scsi_hba_bus_config(dev_info_t *self, uint_t flag, ddi_bus_config_op_t op,
4067     void *arg, dev_info_t **childp)
4068 {
4069 	scsi_hba_tran_t	*tran = NULL;
4070 
4071 	tran = ddi_get_driver_private(self);
4072 
4073 	if (tran && (tran->tran_hba_flags & SCSI_HBA_HBA)) {
4074 #ifdef	sparc
4075 		char *nameaddr = NULL;
4076 		nameaddr = (char *)arg;
4077 		switch (op) {
4078 		case BUS_CONFIG_ONE:
4079 			if (nameaddr == NULL)
4080 				return (NDI_FAILURE);
4081 			if (strncmp(nameaddr, "iport", strlen("iport")) == 0) {
4082 				break;
4083 			}
4084 			/*
4085 			 * If this operation is not against an iport node, it's
4086 			 * possible the operation is requested to configure
4087 			 * root disk by OBP. Unfortunately, prom path is without
4088 			 * iport string in the boot path.
4089 			 */
4090 			if (strncmp(nameaddr, "disk@", strlen("disk@")) == 0) {
4091 				return (scsi_hba_bus_config_prom_node(self,
4092 				    flag, arg, childp));
4093 			}
4094 			break;
4095 		default:
4096 			break;
4097 		}
4098 #endif
4099 		/*
4100 		 * The request is to configure multi-port HBA.
4101 		 * Now start to configure iports, for the end
4102 		 * devices attached to iport, should be configured
4103 		 * by bus_configure routine of iport
4104 		 */
4105 		return (scsi_hba_bus_config_iports(self, flag, op, arg,
4106 		    childp));
4107 	}
4108 
4109 #ifdef sparc
4110 	if (scsi_hba_iport_unit_address(self)) {
4111 		flag &= (~NDI_PROMNAME);
4112 	}
4113 #endif
4114 	if (tran && tran->tran_bus_config) {
4115 		return (tran->tran_bus_config(self, flag, op, arg, childp));
4116 	}
4117 
4118 	/*
4119 	 * Force reprobe for BUS_CONFIG_ONE or when manually reconfiguring
4120 	 * via devfsadm(1m) to emulate deferred attach.
4121 	 * Reprobe only discovers driver.conf enumerated nodes, more
4122 	 * dynamic implementations probably require their own bus_config.
4123 	 */
4124 	if ((op == BUS_CONFIG_ONE) || (flag & NDI_DRV_CONF_REPROBE))
4125 		flag |= NDI_CONFIG_REPROBE;
4126 
4127 	return (ndi_busop_bus_config(self, flag, op, arg, childp, 0));
4128 }
4129 
4130 static int
4131 scsi_hba_bus_unconfig(dev_info_t *self, uint_t flag, ddi_bus_config_op_t op,
4132     void *arg)
4133 {
4134 	scsi_hba_tran_t	*tran = NULL;
4135 
4136 	tran = ddi_get_driver_private(self);
4137 
4138 	if (tran && tran->tran_bus_unconfig) {
4139 		return (tran->tran_bus_unconfig(self, flag, op, arg));
4140 	}
4141 	return (ndi_busop_bus_unconfig(self, flag, op, arg));
4142 }
4143 
4144 void
4145 scsi_hba_pkt_comp(struct scsi_pkt *pkt)
4146 {
4147 	ASSERT(pkt);
4148 	if (pkt->pkt_comp == NULL)
4149 		return;
4150 
4151 	/*
4152 	 * For HBA drivers that implement tran_setup_pkt(9E), if we are
4153 	 * completing a 'consistent' mode DMA operation then we must
4154 	 * perform dma_sync prior to calling pkt_comp to ensure that
4155 	 * the target driver sees the correct data in memory.
4156 	 */
4157 	ASSERT((pkt->pkt_flags & FLAG_NOINTR) == 0);
4158 	if (((pkt->pkt_dma_flags & DDI_DMA_CONSISTENT) &&
4159 	    (pkt->pkt_dma_flags & DDI_DMA_READ)) &&
4160 	    ((P_TO_TRAN(pkt)->tran_setup_pkt) != NULL)) {
4161 		scsi_sync_pkt(pkt);
4162 	}
4163 	(*pkt->pkt_comp)(pkt);
4164 }
4165