xref: /illumos-gate/usr/src/uts/common/io/scsi/impl/scsi_hba.c (revision 46b592853d0f4f11781b6b0a7533f267c6aee132)
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/scsi/generic/sas.h>
33 #include <sys/file.h>
34 #include <sys/disp.h>			/* for minclsyspri */
35 #include <sys/ddi_impldefs.h>
36 #include <sys/ndi_impldefs.h>
37 #include <sys/sunndi.h>
38 #include <sys/ddi.h>
39 #include <sys/sunmdi.h>
40 #include <sys/mdi_impldefs.h>
41 #include <sys/callb.h>
42 #include <sys/epm.h>
43 #include <sys/damap.h>
44 #include <sys/time.h>
45 #include <sys/sunldi.h>
46 
47 extern struct scsi_pkt *scsi_init_cache_pkt(struct scsi_address *,
48 		    struct scsi_pkt *, struct buf *, int, int, int, int,
49 		    int (*)(caddr_t), caddr_t);
50 extern void	scsi_free_cache_pkt(struct scsi_address *, struct scsi_pkt *);
51 extern void	scsi_cache_dmafree(struct scsi_address *, struct scsi_pkt *);
52 extern void	scsi_sync_cache_pkt(struct scsi_address *, struct scsi_pkt *);
53 extern int	modrootloaded;
54 
55 /*
56  * Round up all allocations so that we can guarantee
57  * long-long alignment.  This is the same alignment
58  * provided by kmem_alloc().
59  */
60 #define	ROUNDUP(x)	(((x) + 0x07) & ~0x07)
61 
62 /* Magic number to track correct allocations in wrappers */
63 #define	PKT_WRAPPER_MAGIC	0xa110ced	/* alloced correctly */
64 
65 kmutex_t	scsi_flag_nointr_mutex;
66 kcondvar_t	scsi_flag_nointr_cv;
67 kmutex_t	scsi_log_mutex;
68 
69 /* asynchronous probe barrier deletion data structures */
70 static kmutex_t	scsi_hba_barrier_mutex;
71 static kcondvar_t	scsi_hba_barrier_cv;
72 static struct scsi_hba_barrier {
73 	struct scsi_hba_barrier	*barrier_next;
74 	clock_t			barrier_endtime;
75 	dev_info_t		*barrier_probe;
76 }		*scsi_hba_barrier_list;
77 static int	scsi_hba_devi_is_barrier(dev_info_t *probe);
78 static void	scsi_hba_barrier_tran_tgt_free(dev_info_t *probe);
79 static void	scsi_hba_barrier_add(dev_info_t *probe, int seconds);
80 static int	scsi_hba_remove_node(dev_info_t *child);
81 static void	scsi_hba_barrier_daemon(void *arg);
82 
83 /* LUN-change ASC/ASCQ processing data structures (stage1 and stage2) */
84 static kmutex_t		scsi_lunchg1_mutex;
85 static kcondvar_t	scsi_lunchg1_cv;
86 static struct scsi_pkt	*scsi_lunchg1_list;
87 static void		scsi_lunchg1_daemon(void *arg);
88 static kmutex_t		scsi_lunchg2_mutex;
89 static kcondvar_t	scsi_lunchg2_cv;
90 static struct scsi_lunchg2 {
91 	struct scsi_lunchg2	*lunchg2_next;
92 	char			*lunchg2_path;
93 }			*scsi_lunchg2_list;
94 static void		scsi_lunchg2_daemon(void *arg);
95 
96 static int	scsi_hba_find_child(dev_info_t *self, char *name, char *addr,
97     int init, dev_info_t **dchildp, mdi_pathinfo_t **pchildp, int *ppi);
98 
99 /* return value defines for scsi_hba_find_child */
100 #define	CHILD_TYPE_NONE		0
101 #define	CHILD_TYPE_DEVINFO	1
102 #define	CHILD_TYPE_PATHINFO	2
103 
104 /*
105  * Enumeration code path currently being followed. SE_BUSCONFIG results in
106  * DEVI_SID_NODEID, and SE_HP (hotplug) results in DEVI_SID_HP_NODEID.
107  *
108  * Since hotplug enumeration is based on information obtained from hardware
109  * (tgtmap/report_lun) the type/severity of enumeration error messages is
110  * sometimes based SE_HP (indirectly via ndi_dev_is_hotplug_node()). By
111  * convention, these messages all contain "enumeration failed during ...".
112  */
113 typedef enum { SE_BUSCONFIG = 0, SE_HP = 1 } scsi_enum_t;
114 
115 /* compatible properties of driver to use during probe/enumeration operations */
116 static char	*compatible_probe = "scsa,probe";
117 static char	*compatible_nodev = "scsa,nodev";
118 static char	*scsi_probe_ascii[] = SCSIPROBE_ASCII;
119 
120 /* number of LUNs we attempt to get on the first SCMD_REPORT_LUNS command */
121 int	scsi_lunrpt_default_max = 256;
122 int	scsi_lunrpt_timeout = 3;	/* seconds */
123 
124 /* 'scsi-binding-set' value for legacy enumerated 'spi' transports */
125 char	*scsi_binding_set_spi = "spi";
126 
127 /* enable NDI_DEVI_DEBUG for bus_[un]config operations */
128 int	scsi_hba_busconfig_debug = 0;
129 
130 /* number of probe serilization messages */
131 int	scsi_hba_wait_msg = 5;
132 
133 /*
134  * Establish the timeout used to cache (in the probe node) the fact that the
135  * device does not exist. This replaces the target specific probe cache.
136  */
137 int	scsi_hba_barrier_timeout = (60);		/* seconds */
138 
139 /*
140  * Structure for scsi_hba_tgtmap_* implementation.
141  *
142  * Every call to scsi_hba_tgtmap_set_begin will increment tgtmap_reports,
143  * and a call to scsi_hba_tgtmap_set_end will reset tgtmap_reports to zero.
144  * If, in scsi_hba_tgtmap_set_begin, we detect a tgtmap_reports value of
145  * scsi_hba_tgtmap_reports_max we produce a message to indicate that
146  * the caller is never completing an observation (i.e. we are not making
147  * any forward progress). If this message occurs, it indicates that the
148  * solaris hotplug ramifications at the target and lun level are no longer
149  * tracking.
150  *
151  * NOTE: LUNMAPSIZE OK for now, but should be dynamic in reportlun code.
152  */
153 typedef struct impl_scsi_tgtmap {
154 	scsi_hba_tran_t *tgtmap_tran;
155 	int		tgtmap_reports;		/* _begin without _end */
156 	int		tgtmap_noisy;
157 	scsi_tgt_activate_cb_t tgtmap_activate_cb;
158 	scsi_tgt_deactivate_cb_t tgtmap_deactivate_cb;
159 	void		*tgtmap_mappriv;
160 	damap_t		*tgtmap_dam[SCSI_TGT_NTYPES];
161 } impl_scsi_tgtmap_t;
162 #define	LUNMAPSIZE 256		/* 256 LUNs/target */
163 
164 /* Produce warning if number of begins without an end exceed this value */
165 int	scsi_hba_tgtmap_reports_max = 256;
166 
167 /* Prototype for static dev_ops devo_*() functions */
168 static int	scsi_hba_info(
169 			dev_info_t		*self,
170 			ddi_info_cmd_t		infocmd,
171 			void			*arg,
172 			void			**result);
173 
174 /* Prototypes for static bus_ops bus_*() functions */
175 static int	scsi_hba_bus_ctl(
176 			dev_info_t		*self,
177 			dev_info_t		*child,
178 			ddi_ctl_enum_t		op,
179 			void			*arg,
180 			void			*result);
181 
182 static int	scsi_hba_map_fault(
183 			dev_info_t		*self,
184 			dev_info_t		*child,
185 			struct hat		*hat,
186 			struct seg		*seg,
187 			caddr_t			addr,
188 			struct devpage		*dp,
189 			pfn_t			pfn,
190 			uint_t			prot,
191 			uint_t			lock);
192 
193 static int	scsi_hba_get_eventcookie(
194 			dev_info_t		*self,
195 			dev_info_t		*child,
196 			char			*name,
197 			ddi_eventcookie_t	*eventp);
198 
199 static int	scsi_hba_add_eventcall(
200 			dev_info_t		*self,
201 			dev_info_t		*child,
202 			ddi_eventcookie_t	event,
203 			void			(*callback)(
204 				dev_info_t		*dip,
205 				ddi_eventcookie_t	event,
206 				void			*arg,
207 				void			*bus_impldata),
208 			void			*arg,
209 			ddi_callback_id_t	*cb_id);
210 
211 static int	scsi_hba_remove_eventcall(
212 			dev_info_t		*self,
213 			ddi_callback_id_t	id);
214 
215 static int	scsi_hba_post_event(
216 			dev_info_t		*self,
217 			dev_info_t		*child,
218 			ddi_eventcookie_t	event,
219 			void			*bus_impldata);
220 
221 static int	scsi_hba_bus_config(
222 			dev_info_t		*self,
223 			uint_t			flags,
224 			ddi_bus_config_op_t	op,
225 			void			*arg,
226 			dev_info_t		**childp);
227 
228 static int	scsi_hba_bus_unconfig(
229 			dev_info_t		*self,
230 			uint_t			flags,
231 			ddi_bus_config_op_t	op,
232 			void			*arg);
233 
234 static int	scsi_hba_fm_init_child(
235 			dev_info_t		*self,
236 			dev_info_t		*child,
237 			int			cap,
238 			ddi_iblock_cookie_t	*ibc);
239 
240 static int	scsi_hba_bus_power(
241 			dev_info_t		*self,
242 			void			*impl_arg,
243 			pm_bus_power_op_t	op,
244 			void			*arg,
245 			void			*result);
246 
247 /* bus_ops vector for SCSI HBA's. */
248 static struct bus_ops scsi_hba_busops = {
249 	BUSO_REV,
250 	nullbusmap,			/* bus_map */
251 	NULL,				/* bus_get_intrspec */
252 	NULL,				/* bus_add_intrspec */
253 	NULL,				/* bus_remove_intrspec */
254 	scsi_hba_map_fault,		/* bus_map_fault */
255 	ddi_dma_map,			/* bus_dma_map */
256 	ddi_dma_allochdl,		/* bus_dma_allochdl */
257 	ddi_dma_freehdl,		/* bus_dma_freehdl */
258 	ddi_dma_bindhdl,		/* bus_dma_bindhdl */
259 	ddi_dma_unbindhdl,		/* bus_unbindhdl */
260 	ddi_dma_flush,			/* bus_dma_flush */
261 	ddi_dma_win,			/* bus_dma_win */
262 	ddi_dma_mctl,			/* bus_dma_ctl */
263 	scsi_hba_bus_ctl,		/* bus_ctl */
264 	ddi_bus_prop_op,		/* bus_prop_op */
265 	scsi_hba_get_eventcookie,	/* bus_get_eventcookie */
266 	scsi_hba_add_eventcall,		/* bus_add_eventcall */
267 	scsi_hba_remove_eventcall,	/* bus_remove_eventcall */
268 	scsi_hba_post_event,		/* bus_post_event */
269 	NULL,				/* bus_intr_ctl */
270 	scsi_hba_bus_config,		/* bus_config */
271 	scsi_hba_bus_unconfig,		/* bus_unconfig */
272 	scsi_hba_fm_init_child,		/* bus_fm_init */
273 	NULL,				/* bus_fm_fini */
274 	NULL,				/* bus_fm_access_enter */
275 	NULL,				/* bus_fm_access_exit */
276 	scsi_hba_bus_power		/* bus_power */
277 };
278 
279 /* cb_ops for hotplug :devctl and :scsi support */
280 static struct cb_ops scsi_hba_cbops = {
281 	scsi_hba_open,
282 	scsi_hba_close,
283 	nodev,			/* strategy */
284 	nodev,			/* print */
285 	nodev,			/* dump */
286 	nodev,			/* read */
287 	nodev,			/* write */
288 	scsi_hba_ioctl,		/* ioctl */
289 	nodev,			/* devmap */
290 	nodev,			/* mmap */
291 	nodev,			/* segmap */
292 	nochpoll,		/* poll */
293 	ddi_prop_op,		/* prop_op */
294 	NULL,			/* stream */
295 	D_NEW|D_MP|D_HOTPLUG,	/* cb_flag */
296 	CB_REV,			/* rev */
297 	nodev,			/* int (*cb_aread)() */
298 	nodev			/* int (*cb_awrite)() */
299 };
300 
301 /* Prototypes for static scsi_hba.c/SCSA private lunmap interfaces */
302 static int	scsi_lunmap_create(
303 			dev_info_t		*self,
304 			impl_scsi_tgtmap_t	*tgtmap,
305 			char			*tgt_addr);
306 static void	scsi_lunmap_destroy(
307 			dev_info_t		*self,
308 			impl_scsi_tgtmap_t	*tgtmap,
309 			char			*tgt_addr);
310 static void	scsi_lunmap_set_begin(
311 			dev_info_t		*self,
312 			damap_t			*lundam);
313 static int	scsi_lunmap_set_add(
314 			dev_info_t		*self,
315 			damap_t			*lundam,
316 			char			*taddr,
317 			scsi_lun64_t		lun_num,
318 			int			lun_sfunc);
319 static void	scsi_lunmap_set_end(
320 			dev_info_t		*self,
321 			damap_t			*lundam);
322 
323 /* Prototypes for static misc. scsi_hba.c private bus_config interfaces */
324 static int scsi_hba_bus_config_iports(dev_info_t *self, uint_t flags,
325     ddi_bus_config_op_t op, void *arg, dev_info_t **childp);
326 static int scsi_hba_bus_config_spi(dev_info_t *self, uint_t flags,
327     ddi_bus_config_op_t op, void *arg, dev_info_t **childp);
328 static dev_info_t *scsi_hba_bus_config_port(dev_info_t *self,
329     char *nameaddr, scsi_enum_t se);
330 
331 #ifdef	sparc
332 static int scsi_hba_bus_config_prom_node(dev_info_t *self, uint_t flags,
333     void *arg, dev_info_t **childp);
334 #endif	/* sparc */
335 
336 
337 /*
338  * SCSI_HBA_LOG is used for all messages. A logging level is specified when
339  * generating a message. Some levels correspond directly to cmn_err levels,
340  * some are associated with increasing levels diagnostic/debug output (LOG1-4),
341  * and others are associated with specific levels of interface (LOGMAP).
342  * For _LOG() messages, a __func__ prefix will identify the function origin
343  * of the message. For _LOG_NF messages, there is no function prefix or
344  * self/child context. Filtering of messages is provided based on logging
345  * level, but messages with cmn_err logging level and messages generated
346  * generated with _LOG_NF() are never filtered.
347  *
348  * For debugging, more complete information can be displayed with each message
349  * (full device path and pointer values) by adjusting scsi_hba_log_info.
350  */
351 /* logging levels */
352 #define	SCSI_HBA_LOGCONT	CE_CONT
353 #define	SCSI_HBA_LOGNOTE	CE_NOTE
354 #define	SCSI_HBA_LOGWARN	CE_WARN
355 #define	SCSI_HBA_LOGPANIC	CE_PANIC
356 #define	SCSI_HBA_LOGIGNORE	CE_IGNORE
357 #define	SCSI_HBA_LOG_CE_MASK	0x0000000F	/* no filter for these levels */
358 #define	SCSI_HBA_LOG1		0x00000010	/* DIAG1 level enable */
359 #define	SCSI_HBA_LOG2		0x00000020	/* DIAG2 level enable */
360 #define	SCSI_HBA_LOG3		0x00000040	/* DIAG3 level enable */
361 #define	SCSI_HBA_LOG4		0x00000080	/* DIAG4 level enable */
362 #define	SCSI_HBA_LOGMAPPHY	0x00000100	/* MAPPHY level enable */
363 #define	SCSI_HBA_LOGMAPIPT	0x00000200	/* MAPIPT level enable */
364 #define	SCSI_HBA_LOGMAPTGT	0x00000400	/* MAPTGT level enable */
365 #define	SCSI_HBA_LOGMAPLUN	0x00000800	/* MAPLUN level enable */
366 #define	SCSI_HBA_LOGMAPCFG	0x00001000	/* MAPCFG level enable */
367 #define	SCSI_HBA_LOGMAPUNCFG	0x00002000	/* MAPUNCFG level enable */
368 #define	SCSI_HBA_LOGTRACE	0x00010000	/* TRACE enable */
369 #if (CE_CONT | CE_NOTE | CE_WARN | CE_PANIC | CE_IGNORE) > SCSI_HBA_LOG_CE_MASK
370 Error, problem with CE_ definitions
371 #endif
372 
373 /*
374  * Tunable log message augmentation and filters: filters do not apply to
375  * SCSI_HBA_LOG_CE_MASK level messages or LOG_NF() messages.
376  *
377  * An example set of /etc/system tunings to simplify debug a SCSA pHCI HBA
378  * driver called "pmcs", including "scsi_vhci" operation, by capturing
379  * log information in the system log might be:
380  *
381  * echo "set scsi:scsi_hba_log_filter_level=0x3ff0"		>> /etc/system
382  * echo "set scsi:scsi_hba_log_filter_phci=\"pmcs\""		>> /etc/system
383  * echo "set scsi:scsi_hba_log_filter_vhci=\"scsi_vhci\""	>> /etc/system
384  *
385  * To capture information on just HBA-SCSAv3 *map operation, use
386  * echo "set scsi:scsi_hba_log_filter_level=0x3f10"		>> /etc/system
387  *
388  * For debugging an HBA driver, you may also want to set:
389  *
390  * echo "set scsi:scsi_hba_log_align=1"				>> /etc/system
391  * echo "set scsi:scsi_hba_log_mt_disable=0x6"			>> /etc/system
392  * echo "set mtc_off=1"						>> /etc/system
393  * echo "set mdi_mtc_off=1"					>> /etc/system
394  * echo "set scsi:scsi_hba_log_fcif=0"				>> /etc/system
395  */
396 int		scsi_hba_log_filter_level =
397 			SCSI_HBA_LOG1 |
398 			0;
399 char		*scsi_hba_log_filter_phci = "\0\0\0\0\0\0\0\0\0\0\0\0";
400 char		*scsi_hba_log_filter_vhci = "\0\0\0\0\0\0\0\0\0\0\0\0";
401 int		scsi_hba_log_align = 0;	/* NOTE: will not cause truncation */
402 int		scsi_hba_log_fcif = '!'; /* "^!?" first char in format */
403 					/* NOTE: iff level > SCSI_HBA_LOG1 */
404 					/* '\0'0x00 -> console and system log */
405 					/* '^' 0x5e -> console_only */
406 					/* '!' 0x21 -> system log only */
407 					/* '?' 0x2F -> See cmn_err(9F) */
408 int		scsi_hba_log_info =	/* augmentation: extra info output */
409 			(0 << 0) |	/* 0x0001: process information */
410 			(0 << 1) |	/* 0x0002: full /devices path */
411 			(0 << 2);	/* 0x0004: devinfo pointer */
412 
413 int		scsi_hba_log_mt_disable =
414 			/* SCSI_ENUMERATION_MT_LUN_DISABLE |	(ie 0x02) */
415 			/* SCSI_ENUMERATION_MT_TARGET_DISABLE |	(ie 0x04) */
416 			0;
417 
418 /* static data for HBA logging subsystem */
419 static kmutex_t	scsi_hba_log_mutex;
420 static char	scsi_hba_log_i[512];
421 static char	scsi_hba_log_buf[512];
422 static char	scsi_hba_fmt[512];
423 
424 /* Macros to use in scsi_hba.c source code below */
425 #define	SCSI_HBA_LOG(x)	scsi_hba_log x
426 #define	_LOG(level)	SCSI_HBA_LOG##level, __func__
427 #define	_MAP(map)	SCSI_HBA_LOGMAP##map, __func__
428 #define	_LOG_NF(level)	SCSI_HBA_LOG##level, NULL, NULL, NULL
429 #define	_LOG_TRACE	_LOG(TRACE)
430 #define	_LOGLUN		_MAP(LUN)
431 #define	_LOGTGT		_MAP(TGT)
432 #define	_LOGIPT		_MAP(IPT)
433 #define	_LOGPHY		_MAP(PHY)
434 #define	_LOGCFG		_MAP(CFG)
435 #define	_LOGUNCFG	_MAP(UNCFG)
436 
437 /*PRINTFLIKE5*/
438 static void
439 scsi_hba_log(int level, const char *func, dev_info_t *self, dev_info_t *child,
440     const char *fmt, ...)
441 {
442 	va_list		ap;
443 	int		clevel;
444 	int		align;
445 	char		*info;
446 	char		*f;
447 	char		*ua;
448 
449 	/* derive self from child's parent */
450 	if ((self == NULL) && child)
451 		self = ddi_get_parent(child);
452 
453 	/* no filtering of SCSI_HBA_LOG_CE_MASK or LOG_NF messages */
454 	if (((level & SCSI_HBA_LOG_CE_MASK) != level) && (func != NULL)) {
455 		/* scsi_hba_log_filter_level: filter on level as bitmask */
456 		if ((level & scsi_hba_log_filter_level) == 0)
457 			return;
458 
459 		/* scsi_hba_log_filter_phci/vhci: on name of driver */
460 		if (*scsi_hba_log_filter_phci &&
461 		    ((self == NULL) ||
462 		    (ddi_driver_name(self) == NULL) ||
463 		    strcmp(ddi_driver_name(self), scsi_hba_log_filter_phci))) {
464 			/* does not match pHCI, check vHCI */
465 			if (*scsi_hba_log_filter_vhci &&
466 			    ((self == NULL) ||
467 			    (ddi_driver_name(self) == NULL) ||
468 			    strcmp(ddi_driver_name(self),
469 			    scsi_hba_log_filter_vhci))) {
470 				/* does not match vHCI */
471 				return;
472 			}
473 		}
474 
475 
476 		/* passed filters, determine align */
477 		align = scsi_hba_log_align;
478 
479 		/* shorten func for filtered output */
480 		if (strncmp(func, "scsi_hba_", 9) == 0)
481 			func += 9;
482 		if (strncmp(func, "scsi_", 5) == 0)
483 			func += 5;
484 	} else {
485 		/* don't align output that is never filtered */
486 		align = 0;
487 	}
488 
489 	/* determine the cmn_err form from the level */
490 	clevel = ((level & SCSI_HBA_LOG_CE_MASK) == level) ? level : CE_CONT;
491 
492 	/* protect common buffers used to format output */
493 	mutex_enter(&scsi_hba_log_mutex);
494 
495 	/* skip special first characters, we add them back below */
496 	f = (char *)fmt;
497 	if (*f && strchr("^!?", *f))
498 		f++;
499 	va_start(ap, fmt);
500 	(void) vsprintf(scsi_hba_log_buf, f, ap);
501 	va_end(ap);
502 
503 	/* augment message with 'information' */
504 	info = scsi_hba_log_i;
505 	*info = '\0';
506 	if ((scsi_hba_log_info & 0x0001) && curproc && PTOU(curproc)->u_comm) {
507 		(void) sprintf(info, "%s[%d]%p ",
508 		    PTOU(curproc)->u_comm, curproc->p_pid, (void *)curthread);
509 		info += strlen(info);
510 	}
511 	if (self) {
512 		if ((scsi_hba_log_info & 0x0004) && (child || self)) {
513 			(void) sprintf(info, "%p ",
514 			    (void *)(child ? child : self));
515 			info += strlen(info);
516 		}
517 		if (scsi_hba_log_info & 0x0002)	{
518 			(void) ddi_pathname(child ? child : self, info);
519 			(void) strcat(info, " ");
520 			info += strlen(info);
521 		}
522 
523 		/* always provide 'default' information about self &child */
524 		(void) sprintf(info, "%s%d ", ddi_driver_name(self),
525 		    ddi_get_instance(self));
526 		info += strlen(info);
527 		if (child) {
528 			ua = ddi_get_name_addr(child);
529 			(void) sprintf(info, "%s@%s ",
530 			    ddi_node_name(child), (ua && *ua) ? ua : "");
531 			info += strlen(info);
532 		}
533 	}
534 
535 	/* turn off alignment if truncation would occur */
536 	if (align && ((strlen(func) > 18) || (strlen(scsi_hba_log_i) > 36)))
537 		align = 0;
538 
539 	/* adjust for aligned output */
540 	if (align) {
541 		if (func == NULL)
542 			func = "";
543 		/* remove trailing blank with align output */
544 		if ((info != scsi_hba_log_i) && (*(info -1) == '\b'))
545 			*(info - 1) = '\0';
546 	}
547 
548 	/* special "first character in format" must be in format itself */
549 	f = scsi_hba_fmt;
550 	if (fmt[0] && strchr("^!?", fmt[0]))
551 		*f++ = fmt[0];
552 	else if (scsi_hba_log_fcif && (level > SCSI_HBA_LOG1))
553 		*f++ = (char)scsi_hba_log_fcif;		/* add global fcif */
554 	if (align)
555 		(void) sprintf(f, "%s", "%-18.18s: %36.36s: %s%s");
556 	else
557 		(void) sprintf(f, "%s", func ? "%s: %s%s%s" : "%s%s%s");
558 
559 	if (func)
560 		cmn_err(clevel, scsi_hba_fmt, func, scsi_hba_log_i,
561 		    scsi_hba_log_buf, clevel == CE_CONT ? "\n" : "");
562 	else
563 		cmn_err(clevel, scsi_hba_fmt, scsi_hba_log_i,
564 		    scsi_hba_log_buf, clevel == CE_CONT ? "\n" : "");
565 	mutex_exit(&scsi_hba_log_mutex);
566 }
567 
568 /*
569  * scsi_hba version of [nm]di_devi_enter/[nm]di_devi_exit that detects if HBA
570  * is a PHCI, and chooses mdi/ndi locking implementation.
571  */
572 static void
573 scsi_hba_devi_enter(dev_info_t *self, int *circp)
574 {
575 	if (MDI_PHCI(self))
576 		mdi_devi_enter(self, circp);
577 	else
578 		ndi_devi_enter(self, circp);
579 }
580 
581 static int
582 scsi_hba_devi_tryenter(dev_info_t *self, int *circp)
583 {
584 	if (MDI_PHCI(self))
585 		return (mdi_devi_tryenter(self, circp));
586 	else
587 		return (ndi_devi_tryenter(self, circp));
588 }
589 
590 static void
591 scsi_hba_devi_exit(dev_info_t *self, int circ)
592 {
593 	if (MDI_PHCI(self))
594 		mdi_devi_exit(self, circ);
595 	else
596 		ndi_devi_exit(self, circ);
597 }
598 
599 static void
600 scsi_hba_devi_enter_phci(dev_info_t *self, int *circp)
601 {
602 	if (MDI_PHCI(self))
603 		mdi_devi_enter_phci(self, circp);
604 }
605 
606 static void
607 scsi_hba_devi_exit_phci(dev_info_t *self, int circ)
608 {
609 	if (MDI_PHCI(self))
610 		mdi_devi_exit_phci(self, circ);
611 }
612 
613 static int
614 scsi_hba_dev_is_sid(dev_info_t *child)
615 {
616 	/*
617 	 * Use ndi_dev_is_persistent_node instead of ddi_dev_is_sid to avoid
618 	 * any possible locking issues in mixed nexus devctl code (like usb).
619 	 */
620 	return (ndi_dev_is_persistent_node(child));
621 }
622 
623 /*
624  * Called from _init() when loading "scsi" module
625  */
626 void
627 scsi_initialize_hba_interface()
628 {
629 	SCSI_HBA_LOG((_LOG_TRACE, NULL, NULL, __func__));
630 
631 	/* We need "scsiprobe" and "scsinodev" as an alias or a driver. */
632 	if (ddi_name_to_major(compatible_probe) == DDI_MAJOR_T_NONE) {
633 		SCSI_HBA_LOG((_LOG_NF(WARN), "failed to resolve '%s' "
634 		    "driver alias, defaulting to 'nulldriver'",
635 		    compatible_probe));
636 
637 		/* If no "nulldriver" driver nothing will work... */
638 		compatible_probe = "nulldriver";
639 		if (ddi_name_to_major(compatible_probe) == DDI_MAJOR_T_NONE)
640 			SCSI_HBA_LOG((_LOG_NF(WARN), "no probe '%s' driver, "
641 			    "system misconfigured", compatible_probe));
642 	}
643 	if (ddi_name_to_major(compatible_nodev) == DDI_MAJOR_T_NONE) {
644 		SCSI_HBA_LOG((_LOG_NF(WARN), "failed to resolve '%s' "
645 		    "driver alias, defaulting to 'nulldriver'",
646 		    compatible_nodev));
647 
648 		/* If no "nulldriver" driver nothing will work... */
649 		compatible_nodev = "nulldriver";
650 		if (ddi_name_to_major(compatible_nodev) == DDI_MAJOR_T_NONE)
651 			SCSI_HBA_LOG((_LOG_NF(WARN), "no nodev '%s' driver, "
652 			    "system misconfigured", compatible_nodev));
653 	}
654 
655 	/*
656 	 * Verify our special node name "probe" will not be used in other ways.
657 	 * Don't expect things to work if they are.
658 	 */
659 	if (ddi_major_to_name(ddi_name_to_major("probe")))
660 		SCSI_HBA_LOG((_LOG_NF(WARN),
661 		    "driver already using special node name 'probe'"));
662 
663 	mutex_init(&scsi_log_mutex, NULL, MUTEX_DRIVER, NULL);
664 	mutex_init(&scsi_flag_nointr_mutex, NULL, MUTEX_DRIVER, NULL);
665 	cv_init(&scsi_flag_nointr_cv, NULL, CV_DRIVER, NULL);
666 	mutex_init(&scsi_hba_log_mutex, NULL, MUTEX_DRIVER, NULL);
667 
668 	/* initialize the asynchronous barrier deletion daemon */
669 	mutex_init(&scsi_hba_barrier_mutex, NULL, MUTEX_DRIVER, NULL);
670 	cv_init(&scsi_hba_barrier_cv, NULL, CV_DRIVER, NULL);
671 	(void) thread_create(NULL, 0,
672 	    (void (*)())scsi_hba_barrier_daemon, NULL,
673 	    0, &p0, TS_RUN, minclsyspri);
674 
675 	/* initialize lun change ASC/ASCQ processing daemon (stage1 & stage2) */
676 	mutex_init(&scsi_lunchg1_mutex, NULL, MUTEX_DRIVER, NULL);
677 	cv_init(&scsi_lunchg1_cv, NULL, CV_DRIVER, NULL);
678 	(void) thread_create(NULL, 0,
679 	    (void (*)())scsi_lunchg1_daemon, NULL,
680 	    0, &p0, TS_RUN, minclsyspri);
681 	mutex_init(&scsi_lunchg2_mutex, NULL, MUTEX_DRIVER, NULL);
682 	cv_init(&scsi_lunchg2_cv, NULL, CV_DRIVER, NULL);
683 	(void) thread_create(NULL, 0,
684 	    (void (*)())scsi_lunchg2_daemon, NULL,
685 	    0, &p0, TS_RUN, minclsyspri);
686 }
687 
688 int
689 scsi_hba_pkt_constructor(void *buf, void *arg, int kmflag)
690 {
691 	struct scsi_pkt_cache_wrapper *pktw;
692 	struct scsi_pkt		*pkt;
693 	scsi_hba_tran_t		*tran = (scsi_hba_tran_t *)arg;
694 	int			pkt_len;
695 	char			*ptr;
696 
697 	/*
698 	 * allocate a chunk of memory for the following:
699 	 * scsi_pkt
700 	 * pcw_* fields
701 	 * pkt_ha_private
702 	 * pkt_cdbp, if needed
703 	 * (pkt_private always null)
704 	 * pkt_scbp, if needed
705 	 */
706 	pkt_len = tran->tran_hba_len + sizeof (struct scsi_pkt_cache_wrapper);
707 	if (tran->tran_hba_flags & SCSI_HBA_TRAN_CDB)
708 		pkt_len += DEFAULT_CDBLEN;
709 	if (tran->tran_hba_flags & SCSI_HBA_TRAN_SCB)
710 		pkt_len += DEFAULT_SCBLEN;
711 	bzero(buf, pkt_len);
712 
713 	ptr = buf;
714 	pktw = buf;
715 	ptr += sizeof (struct scsi_pkt_cache_wrapper);
716 	pkt = &(pktw->pcw_pkt);
717 	pkt->pkt_ha_private = (opaque_t)ptr;
718 
719 	pktw->pcw_magic = PKT_WRAPPER_MAGIC;	/* alloced correctly */
720 	/*
721 	 * keep track of the granularity at the time this handle was
722 	 * allocated
723 	 */
724 	pktw->pcw_granular = tran->tran_dma_attr.dma_attr_granular;
725 
726 	if (ddi_dma_alloc_handle(tran->tran_hba_dip, &tran->tran_dma_attr,
727 	    kmflag == KM_SLEEP ? SLEEP_FUNC: NULL_FUNC, NULL,
728 	    &pkt->pkt_handle) != DDI_SUCCESS) {
729 
730 		return (-1);
731 	}
732 	ptr += tran->tran_hba_len;
733 	if (tran->tran_hba_flags & SCSI_HBA_TRAN_CDB) {
734 		pkt->pkt_cdbp = (opaque_t)ptr;
735 		ptr += DEFAULT_CDBLEN;
736 	}
737 	pkt->pkt_private = NULL;
738 	if (tran->tran_hba_flags & SCSI_HBA_TRAN_SCB)
739 		pkt->pkt_scbp = (opaque_t)ptr;
740 	if (tran->tran_pkt_constructor)
741 		return ((*tran->tran_pkt_constructor)(pkt, arg, kmflag));
742 	else
743 		return (0);
744 }
745 
746 #define	P_TO_TRAN(pkt)	((pkt)->pkt_address.a_hba_tran)
747 
748 void
749 scsi_hba_pkt_destructor(void *buf, void *arg)
750 {
751 	struct scsi_pkt_cache_wrapper *pktw = buf;
752 	struct scsi_pkt		*pkt = &(pktw->pcw_pkt);
753 	scsi_hba_tran_t		*tran = (scsi_hba_tran_t *)arg;
754 
755 	ASSERT(pktw->pcw_magic == PKT_WRAPPER_MAGIC);
756 	ASSERT((pktw->pcw_flags & PCW_BOUND) == 0);
757 	if (tran->tran_pkt_destructor)
758 		(*tran->tran_pkt_destructor)(pkt, arg);
759 
760 	/* make sure nobody messed with our pointers */
761 	ASSERT(pkt->pkt_ha_private == (opaque_t)((char *)pkt +
762 	    sizeof (struct scsi_pkt_cache_wrapper)));
763 	ASSERT(((tran->tran_hba_flags & SCSI_HBA_TRAN_SCB) == 0) ||
764 	    (pkt->pkt_scbp == (opaque_t)((char *)pkt +
765 	    tran->tran_hba_len +
766 	    (((tran->tran_hba_flags & SCSI_HBA_TRAN_CDB) == 0) ?
767 	    0 : DEFAULT_CDBLEN) +
768 	    DEFAULT_PRIVLEN + sizeof (struct scsi_pkt_cache_wrapper))));
769 	ASSERT(((tran->tran_hba_flags & SCSI_HBA_TRAN_CDB) == 0) ||
770 	    (pkt->pkt_cdbp == (opaque_t)((char *)pkt +
771 	    tran->tran_hba_len +
772 	    sizeof (struct scsi_pkt_cache_wrapper))));
773 	ASSERT(pkt->pkt_handle);
774 	ddi_dma_free_handle(&pkt->pkt_handle);
775 	pkt->pkt_handle = NULL;
776 	pkt->pkt_numcookies = 0;
777 	pktw->pcw_total_xfer = 0;
778 	pktw->pcw_totalwin = 0;
779 	pktw->pcw_curwin = 0;
780 }
781 
782 /*
783  * Called by an HBA from _init() to plumb in common SCSA bus_ops and
784  * cb_ops for the HBA's :devctl and :scsi minor nodes.
785  */
786 int
787 scsi_hba_init(struct modlinkage *modlp)
788 {
789 	struct dev_ops *hba_dev_ops;
790 
791 	SCSI_HBA_LOG((_LOG_TRACE, NULL, NULL, __func__));
792 
793 	/*
794 	 * Get a pointer to the dev_ops structure of the HBA and plumb our
795 	 * bus_ops vector into the HBA's dev_ops structure.
796 	 */
797 	hba_dev_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
798 	ASSERT(hba_dev_ops->devo_bus_ops == NULL);
799 	hba_dev_ops->devo_bus_ops = &scsi_hba_busops;
800 
801 	/*
802 	 * Plumb our cb_ops vector into the HBA's dev_ops structure to
803 	 * provide getinfo and hotplugging ioctl support if the HBA driver
804 	 * does not already provide this support.
805 	 */
806 	if (hba_dev_ops->devo_cb_ops == NULL) {
807 		hba_dev_ops->devo_cb_ops = &scsi_hba_cbops;
808 	}
809 	if (hba_dev_ops->devo_cb_ops->cb_open == scsi_hba_open) {
810 		ASSERT(hba_dev_ops->devo_cb_ops->cb_close == scsi_hba_close);
811 		hba_dev_ops->devo_getinfo = scsi_hba_info;
812 	}
813 	return (0);
814 }
815 
816 /*
817  * Called by an HBA attach(9E) to allocate a scsi_hba_tran(9S) structure. An
818  * HBA driver will then initialize the structure and then call
819  * scsi_hba_attach_setup(9F).
820  */
821 /*ARGSUSED*/
822 scsi_hba_tran_t *
823 scsi_hba_tran_alloc(
824 	dev_info_t		*self,
825 	int			flags)
826 {
827 	scsi_hba_tran_t		*tran;
828 
829 	SCSI_HBA_LOG((_LOG_TRACE, self, NULL, __func__));
830 
831 	/* allocate SCSA flavors for self */
832 	ndi_flavorv_alloc(self, SCSA_NFLAVORS);
833 
834 	tran = kmem_zalloc(sizeof (scsi_hba_tran_t),
835 	    (flags & SCSI_HBA_CANSLEEP) ? KM_SLEEP : KM_NOSLEEP);
836 
837 	if (tran) {
838 		tran->tran_interconnect_type = INTERCONNECT_PARALLEL;
839 
840 		/*
841 		 * HBA driver called scsi_hba_tran_alloc(), so tran structure
842 		 * is proper size and unused/newer fields are zero.
843 		 *
844 		 * NOTE: We use SCSA_HBA_SCSA_TA as an obtuse form of
845 		 * versioning to detect old HBA drivers that do not use
846 		 * scsi_hba_tran_alloc, and would present garbage data
847 		 * (instead of valid/zero data) for newer tran fields.
848 		 */
849 		tran->tran_hba_flags |= SCSI_HBA_SCSA_TA;
850 	}
851 
852 	return (tran);
853 }
854 
855 /*
856  * Called by an HBA to free a scsi_hba_tran structure
857  */
858 void
859 scsi_hba_tran_free(
860 	scsi_hba_tran_t		*tran)
861 {
862 	SCSI_HBA_LOG((_LOG_TRACE, tran->tran_hba_dip, NULL, __func__));
863 
864 	kmem_free(tran, sizeof (scsi_hba_tran_t));
865 }
866 
867 int
868 scsi_tran_ext_alloc(
869 	scsi_hba_tran_t		*tran,
870 	size_t			length,
871 	int			flags)
872 {
873 	void	*tran_ext;
874 	int	ret = DDI_FAILURE;
875 
876 	tran_ext = kmem_zalloc(length,
877 	    (flags & SCSI_HBA_CANSLEEP) ? KM_SLEEP : KM_NOSLEEP);
878 	if (tran_ext != NULL) {
879 		tran->tran_extension = tran_ext;
880 		ret = DDI_SUCCESS;
881 	}
882 	return (ret);
883 }
884 
885 void
886 scsi_tran_ext_free(
887 	scsi_hba_tran_t		*tran,
888 	size_t			length)
889 {
890 	if (tran->tran_extension != NULL) {
891 		kmem_free(tran->tran_extension, length);
892 		tran->tran_extension = NULL;
893 	}
894 }
895 
896 /*
897  * Obsolete: Called by an HBA to attach an instance of the driver
898  * Implement this older interface in terms of the new.
899  */
900 /*ARGSUSED*/
901 int
902 scsi_hba_attach(
903 	dev_info_t		*self,
904 	ddi_dma_lim_t		*hba_lim,
905 	scsi_hba_tran_t		*tran,
906 	int			flags,
907 	void			*hba_options)
908 {
909 	ddi_dma_attr_t		hba_dma_attr;
910 
911 	bzero(&hba_dma_attr, sizeof (ddi_dma_attr_t));
912 	hba_dma_attr.dma_attr_burstsizes = hba_lim->dlim_burstsizes;
913 	hba_dma_attr.dma_attr_minxfer = hba_lim->dlim_minxfer;
914 
915 	return (scsi_hba_attach_setup(self, &hba_dma_attr, tran, flags));
916 }
917 
918 /*
919  * Common nexus teardown code: used by both scsi_hba_detach() on SCSA HBA node
920  * and iport_postdetach_tran_scsi_device() on a SCSA HBA iport node (and for
921  * failure cleanup). Undo scsa_nexus_setup in reverse order.
922  *
923  * NOTE: Since we are in the Solaris IO framework, we can depend on
924  * undocumented cleanup operations performed by other parts of the framework:
925  * like detach_node() calling ddi_prop_remove_all() and
926  * ddi_remove_minor_node(,NULL).
927  */
928 static void
929 scsa_nexus_teardown(dev_info_t *self, scsi_hba_tran_t	*tran)
930 {
931 	/* Teardown FMA. */
932 	if (tran->tran_hba_flags & SCSI_HBA_SCSA_FM) {
933 		ddi_fm_fini(self);
934 		tran->tran_hba_flags &= ~SCSI_HBA_SCSA_FM;
935 	}
936 }
937 
938 /*
939  * Common nexus setup code: used by both scsi_hba_attach_setup() on SCSA HBA
940  * node and iport_preattach_tran_scsi_device() on a SCSA HBA iport node.
941  *
942  * This code makes no assumptions about tran use by scsi_device children.
943  */
944 static int
945 scsa_nexus_setup(dev_info_t *self, scsi_hba_tran_t *tran)
946 {
947 	int		capable;
948 	int		scsa_minor;
949 
950 	/*
951 	 * NOTE: SCSA maintains an 'fm-capable' domain, in tran_fm_capable,
952 	 * that is not dependent (limited by) the capabilities of its parents.
953 	 * For example a devinfo node in a branch that is not
954 	 * DDI_FM_EREPORT_CAPABLE may report as capable, via tran_fm_capable,
955 	 * to its scsi_device children.
956 	 *
957 	 * Get 'fm-capable' property from driver.conf, if present. If not
958 	 * present, default to the scsi_fm_capable global (which has
959 	 * DDI_FM_EREPORT_CAPABLE set by default).
960 	 */
961 	if (tran->tran_fm_capable == DDI_FM_NOT_CAPABLE)
962 		tran->tran_fm_capable = ddi_prop_get_int(DDI_DEV_T_ANY, self,
963 		    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
964 		    "fm-capable", scsi_fm_capable);
965 
966 	/*
967 	 * If an HBA is *not* doing its own fma support by calling
968 	 * ddi_fm_init() prior to scsi_hba_attach_setup(), we provide a minimal
969 	 * common SCSA implementation so that scsi_device children can generate
970 	 * ereports via scsi_fm_ereport_post().  We use ddi_fm_capable() to
971 	 * detect an HBA calling ddi_fm_init() prior to scsi_hba_attach_setup().
972 	 */
973 	if (tran->tran_fm_capable &&
974 	    (ddi_fm_capable(self) == DDI_FM_NOT_CAPABLE)) {
975 		/*
976 		 * We are capable of something, pass our capabilities up the
977 		 * tree, but use a local variable so our parent can't limit
978 		 * our capabilities (we don't want our parent to clear
979 		 * DDI_FM_EREPORT_CAPABLE).
980 		 *
981 		 * NOTE: iblock cookies are not important because scsi HBAs
982 		 * always interrupt below LOCK_LEVEL.
983 		 */
984 		capable = tran->tran_fm_capable;
985 		ddi_fm_init(self, &capable, NULL);
986 
987 		/*
988 		 * Set SCSI_HBA_SCSA_FM bit to mark us as using the common
989 		 * minimal SCSA fm implementation -  we called ddi_fm_init(),
990 		 * so we are responsible for calling ddi_fm_fini() in
991 		 * scsi_hba_detach().
992 		 *
993 		 * NOTE: if ddi_fm_init fails to establish handle, SKIP cleanup.
994 		 */
995 		if (DEVI(self)->devi_fmhdl)
996 			tran->tran_hba_flags |= SCSI_HBA_SCSA_FM;
997 	}
998 
999 	/* If SCSA responsible for for minor nodes, create :devctl minor. */
1000 	scsa_minor = (ddi_get_driver(self)->devo_cb_ops->cb_open ==
1001 	    scsi_hba_open) ? 1 : 0;
1002 	if (scsa_minor && ((ddi_create_minor_node(self, "devctl", S_IFCHR,
1003 	    INST2DEVCTL(ddi_get_instance(self)), DDI_NT_SCSI_NEXUS, 0) !=
1004 	    DDI_SUCCESS))) {
1005 		SCSI_HBA_LOG((_LOG(WARN), self, NULL,
1006 		    "can't create :devctl minor node"));
1007 		goto fail;
1008 	}
1009 
1010 	return (DDI_SUCCESS);
1011 
1012 fail:	scsa_nexus_teardown(self, tran);
1013 	return (DDI_FAILURE);
1014 }
1015 
1016 /*
1017  * Common tran teardown code: used by iport_postdetach_tran_scsi_device() on a
1018  * SCSA HBA iport node and (possibly) by scsi_hba_detach() on SCSA HBA node
1019  * (and for failure cleanup). Undo scsa_tran_setup in reverse order.
1020  *
1021  * NOTE: Since we are in the Solaris IO framework, we can depend on
1022  * undocumented cleanup operations performed by other parts of the framework:
1023  * like detach_node() calling ddi_prop_remove_all() and
1024  * ddi_remove_minor_node(,NULL).
1025  */
1026 static void
1027 scsa_tran_teardown(dev_info_t *self, scsi_hba_tran_t *tran)
1028 {
1029 	tran->tran_iport_dip = NULL;
1030 
1031 	/* Teardown pHCI registration */
1032 	if (tran->tran_hba_flags & SCSI_HBA_SCSA_PHCI) {
1033 		(void) mdi_phci_unregister(self, 0);
1034 		tran->tran_hba_flags &= ~SCSI_HBA_SCSA_PHCI;
1035 	}
1036 }
1037 
1038 /*
1039  * Common tran setup code: used by iport_preattach_tran_scsi_device() on a
1040  * SCSA HBA iport node and (possibly) by scsi_hba_attach_setup() on SCSA HBA
1041  * node.
1042  */
1043 static int
1044 scsa_tran_setup(dev_info_t *self, scsi_hba_tran_t *tran)
1045 {
1046 	int			scsa_minor;
1047 	int			id;
1048 	char			*scsi_binding_set;
1049 	static const char	*interconnect[] = INTERCONNECT_TYPE_ASCII;
1050 
1051 	SCSI_HBA_LOG((_LOG_TRACE, self, NULL, __func__));
1052 
1053 	/* If SCSA responsible for for minor nodes, create ":scsi" */
1054 	scsa_minor = (ddi_get_driver(self)->devo_cb_ops->cb_open ==
1055 	    scsi_hba_open) ? 1 : 0;
1056 	if (scsa_minor && (ddi_create_minor_node(self, "scsi", S_IFCHR,
1057 	    INST2SCSI(ddi_get_instance(self)),
1058 	    DDI_NT_SCSI_ATTACHMENT_POINT, 0) != DDI_SUCCESS)) {
1059 		SCSI_HBA_LOG((_LOG(WARN), self, NULL,
1060 		    "can't create :scsi minor node"));
1061 		goto fail;
1062 	}
1063 
1064 	/*
1065 	 * If the property does not already exist on self then see if we can
1066 	 * pull it from further up the tree and define it on self. If the
1067 	 * property does not exist above (including options.conf) then use the
1068 	 * default value specified (global variable). We pull things down from
1069 	 * above for faster "DDI_PROP_NOTPROM | DDI_PROP_DONTPASS" runtime
1070 	 * access.
1071 	 *
1072 	 * Future: Should we avoid creating properties when value == global?
1073 	 */
1074 #define	CONFIG_INT_PROP(s, p, dv)	{			\
1075 	if ((ddi_prop_exists(DDI_DEV_T_ANY, s,			\
1076 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, p) == 0) &&	\
1077 	    (ndi_prop_update_int(DDI_DEV_T_NONE, s, p,		\
1078 	    ddi_prop_get_int(DDI_DEV_T_ANY, ddi_get_parent(s),	\
1079 	    DDI_PROP_NOTPROM, p, dv)) != DDI_PROP_SUCCESS))	\
1080 		SCSI_HBA_LOG((_LOG(WARN), NULL, s,		\
1081 		    "can't create property '%s'", p));		\
1082 	}
1083 
1084 	/* Decorate with scsi configuration properties */
1085 	CONFIG_INT_PROP(self, "scsi-enumeration", scsi_enumeration);
1086 	CONFIG_INT_PROP(self, "scsi-options", scsi_options);
1087 	CONFIG_INT_PROP(self, "scsi-reset-delay", scsi_reset_delay);
1088 	CONFIG_INT_PROP(self, "scsi-watchdog-tick", scsi_watchdog_tick);
1089 	CONFIG_INT_PROP(self, "scsi-selection-timeout", scsi_selection_timeout);
1090 	CONFIG_INT_PROP(self, "scsi-tag-age-limit", scsi_tag_age_limit);
1091 
1092 	/*
1093 	 * Pull down the scsi-initiator-id from further up the tree, or as
1094 	 * defined by OBP. Place on node for faster access. NOTE: there is
1095 	 * some confusion about what the name of the property should be.
1096 	 */
1097 	id = ddi_prop_get_int(DDI_DEV_T_ANY, self, 0, "initiator-id", -1);
1098 	if (id == -1)
1099 		id = ddi_prop_get_int(DDI_DEV_T_ANY, self, 0,
1100 		    "scsi-initiator-id", -1);
1101 	if (id != -1)
1102 		CONFIG_INT_PROP(self, "scsi-initiator-id", id);
1103 
1104 	/*
1105 	 * If we are responsible for tran allocation, establish
1106 	 * 'initiator-interconnect-type'.
1107 	 */
1108 	if ((tran->tran_hba_flags & SCSI_HBA_SCSA_TA) &&
1109 	    (tran->tran_interconnect_type > 0) &&
1110 	    (tran->tran_interconnect_type < INTERCONNECT_MAX)) {
1111 		if (ndi_prop_update_string(DDI_DEV_T_NONE, self,
1112 		    "initiator-interconnect-type",
1113 		    (char *)interconnect[tran->tran_interconnect_type])
1114 		    != DDI_PROP_SUCCESS) {
1115 			SCSI_HBA_LOG((_LOG(WARN), self, NULL,
1116 			    "failed to establish "
1117 			    "'initiator-interconnect-type'"));
1118 			goto fail;
1119 		}
1120 	}
1121 
1122 	/*
1123 	 * The 'scsi-binding-set' property can be defined in driver.conf
1124 	 * files of legacy drivers on an as-needed basis. If 'scsi-binding-set'
1125 	 * is not driver.conf defined, and the HBA is not implementing its own
1126 	 * private bus_config, we define scsi-binding-set to the default
1127 	 * 'spi' legacy value.
1128 	 *
1129 	 * NOTE: This default 'spi' value will be deleted if an HBA driver
1130 	 * ends up using the scsi_hba_tgtmap_create() enumeration services.
1131 	 *
1132 	 * NOTE: If we were ever to decide to derive 'scsi-binding-set' from
1133 	 * the IEEE-1275 'device_type' property then this is where that code
1134 	 * should go - there is not enough consistency in 'device_type' to do
1135 	 * this correctly at this point in time.
1136 	 */
1137 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, self,
1138 	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, "scsi-binding-set",
1139 	    &scsi_binding_set) == DDI_PROP_SUCCESS) {
1140 		SCSI_HBA_LOG((_LOG(2), NULL, self,
1141 		    "external 'scsi-binding-set' \"%s\"", scsi_binding_set));
1142 		ddi_prop_free(scsi_binding_set);
1143 	} else if (scsi_binding_set_spi &&
1144 	    ((tran->tran_bus_config == NULL) ||
1145 	    (tran->tran_bus_config == scsi_hba_bus_config_spi))) {
1146 		if (ndi_prop_update_string(DDI_DEV_T_NONE, self,
1147 		    "scsi-binding-set", scsi_binding_set_spi) !=
1148 		    DDI_PROP_SUCCESS) {
1149 			SCSI_HBA_LOG((_LOG(WARN), self, NULL,
1150 			    "failed to establish 'scsi_binding_set' default"));
1151 			goto fail;
1152 		}
1153 		SCSI_HBA_LOG((_LOG(2), NULL, self,
1154 		    "default 'scsi-binding-set' \"%s\"", scsi_binding_set_spi));
1155 	} else
1156 		SCSI_HBA_LOG((_LOG(2), NULL, self,
1157 		    "no 'scsi-binding-set'"));
1158 
1159 	/*
1160 	 * If SCSI_HBA_TRAN_PHCI is set, take care of pHCI registration of the
1161 	 * initiator.
1162 	 */
1163 	if ((tran->tran_hba_flags & SCSI_HBA_TRAN_PHCI) &&
1164 	    (mdi_phci_register(MDI_HCI_CLASS_SCSI, self, 0) == MDI_SUCCESS))
1165 		tran->tran_hba_flags |= SCSI_HBA_SCSA_PHCI;
1166 
1167 	/* NOTE: tran_hba_dip is for DMA operation at the HBA node level */
1168 	tran->tran_iport_dip = self;		/* for iport association */
1169 	return (DDI_SUCCESS);
1170 
1171 fail:	scsa_tran_teardown(self, tran);
1172 	return (DDI_FAILURE);
1173 }
1174 
1175 /*
1176  * Called by a SCSA HBA driver to attach an instance of the driver to
1177  * SCSA HBA node  enumerated by PCI.
1178  */
1179 int
1180 scsi_hba_attach_setup(
1181 	dev_info_t		*self,
1182 	ddi_dma_attr_t		*hba_dma_attr,
1183 	scsi_hba_tran_t		*tran,
1184 	int			flags)
1185 {
1186 	int			len;
1187 	char			cache_name[96];
1188 
1189 	SCSI_HBA_LOG((_LOG_TRACE, self, NULL, __func__));
1190 
1191 	/*
1192 	 * Verify that we are a driver so other code does not need to
1193 	 * check for NULL ddi_get_driver() result.
1194 	 */
1195 	if (ddi_get_driver(self) == NULL)
1196 		return (DDI_FAILURE);
1197 
1198 	/*
1199 	 * Verify that we are called on a SCSA HBA node (function enumerated
1200 	 * by PCI), not on an iport node.
1201 	 */
1202 	ASSERT(scsi_hba_iport_unit_address(self) == NULL);
1203 	if (scsi_hba_iport_unit_address(self))
1204 		return (DDI_FAILURE);		/* self can't be an iport */
1205 
1206 	/* Caller must provide the tran. */
1207 	ASSERT(tran);
1208 	if (tran == NULL)
1209 		return (DDI_FAILURE);
1210 
1211 	/*
1212 	 * Verify correct scsi_hba_tran_t form:
1213 	 *
1214 	 * o Both or none of tran_get_name/tran_get_addr.
1215 	 *   NOTE: Older  SCSA HBA drivers for SCSI transports with addressing
1216 	 *   that did not fit the SPI "struct scsi_address" model were required
1217 	 *   to implement tran_get_name and tran_get_addr. This is no longer
1218 	 *   true - modern transport drivers should now use common SCSA
1219 	 *   enumeration services.  The SCSA enumeration code will represent
1220 	 *   the unit-address using well-known address properties
1221 	 *   (SCSI_ADDR_PROP_TARGET_PORT, SCSI_ADDR_PROP_LUN64) during
1222 	 *   devinfo/pathinfo node creation. The HBA driver can obtain values
1223 	 *   using scsi_device_prop_lookup_*() from its tran_tgt_init(9E).
1224 	 *
1225 	 */
1226 	if ((tran->tran_get_name == NULL) ^ (tran->tran_get_bus_addr == NULL)) {
1227 		SCSI_HBA_LOG((_LOG(WARN), self, NULL,
1228 		    "should support both or neither: "
1229 		    "tran_get_name, tran_get_bus_addr"));
1230 		return (DDI_FAILURE);
1231 	}
1232 
1233 	/*
1234 	 * Establish the devinfo context of this tran structure, preserving
1235 	 * knowledge of how the tran was allocated.
1236 	 */
1237 	tran->tran_hba_dip = self;		/* for DMA */
1238 	tran->tran_hba_flags = (flags & ~SCSI_HBA_SCSA_TA) |
1239 	    (tran->tran_hba_flags & SCSI_HBA_SCSA_TA);
1240 
1241 	/* Establish flavor of transport (and ddi_get_driver_private()) */
1242 	ndi_flavorv_set(self, SCSA_FLAVOR_SCSI_DEVICE, tran);
1243 
1244 	/*
1245 	 * Note: We only need dma_attr_minxfer and dma_attr_burstsizes
1246 	 * from the DMA attributes. scsi_hba_attach(9f) only guarantees
1247 	 * that these two fields are initialized properly. If this
1248 	 * changes, be sure to revisit the implementation of
1249 	 * scsi_hba_attach(9F).
1250 	 */
1251 	(void) memcpy(&tran->tran_dma_attr, hba_dma_attr,
1252 	    sizeof (ddi_dma_attr_t));
1253 
1254 	/* Create tran_setup_pkt(9E) kmem_cache. */
1255 	if (tran->tran_setup_pkt) {
1256 		ASSERT(tran->tran_init_pkt == NULL);
1257 		ASSERT(tran->tran_destroy_pkt == NULL);
1258 		if (tran->tran_init_pkt || tran->tran_destroy_pkt)
1259 			goto fail;
1260 
1261 		tran->tran_init_pkt = scsi_init_cache_pkt;
1262 		tran->tran_destroy_pkt = scsi_free_cache_pkt;
1263 		tran->tran_sync_pkt = scsi_sync_cache_pkt;
1264 		tran->tran_dmafree = scsi_cache_dmafree;
1265 
1266 		len = sizeof (struct scsi_pkt_cache_wrapper);
1267 		len += ROUNDUP(tran->tran_hba_len);
1268 		if (tran->tran_hba_flags & SCSI_HBA_TRAN_CDB)
1269 			len += ROUNDUP(DEFAULT_CDBLEN);
1270 		if (tran->tran_hba_flags & SCSI_HBA_TRAN_SCB)
1271 			len += ROUNDUP(DEFAULT_SCBLEN);
1272 
1273 		(void) snprintf(cache_name, sizeof (cache_name),
1274 		    "pkt_cache_%s_%d", ddi_driver_name(self),
1275 		    ddi_get_instance(self));
1276 
1277 		tran->tran_pkt_cache_ptr = kmem_cache_create(
1278 		    cache_name, len, 8, scsi_hba_pkt_constructor,
1279 		    scsi_hba_pkt_destructor, NULL, tran, NULL, 0);
1280 	}
1281 
1282 	/* Perform node setup independent of initiator role */
1283 	if (scsa_nexus_setup(self, tran) != DDI_SUCCESS)
1284 		goto fail;
1285 
1286 	/*
1287 	 * The SCSI_HBA_HBA flag is passed to scsi_hba_attach_setup when the
1288 	 * HBA driver knows that *all* children of the SCSA HBA node will be
1289 	 * 'iports'. If the SCSA HBA node can have iport children and also
1290 	 * function as an initiator for xxx_device children then it should
1291 	 * not specify SCSI_HBA_HBA in its scsi_hba_attach_setup call. An
1292 	 * HBA driver that does not manage iports should not set SCSA_HBA_HBA.
1293 	 */
1294 	if (tran->tran_hba_flags & SCSI_HBA_HBA) {
1295 		/*
1296 		 * Set the 'ddi-config-driver-node' property on the nexus
1297 		 * node that notify attach_driver_nodes() to configure all
1298 		 * immediate children so that nodes which bind to the
1299 		 * same driver as parent are able to be added into per-driver
1300 		 * list.
1301 		 */
1302 		if (ndi_prop_create_boolean(DDI_DEV_T_NONE,
1303 		    self, "ddi-config-driver-node") != DDI_PROP_SUCCESS)
1304 			goto fail;
1305 	} else {
1306 		if (scsa_tran_setup(self, tran) != DDI_SUCCESS)
1307 			goto fail;
1308 	}
1309 
1310 	return (DDI_SUCCESS);
1311 
1312 fail:	(void) scsi_hba_detach(self);
1313 	return (DDI_FAILURE);
1314 }
1315 
1316 /*
1317  * Called by an HBA to detach an instance of the driver. This may be called
1318  * for SCSA HBA nodes and for SCSA iport nodes.
1319  */
1320 int
1321 scsi_hba_detach(dev_info_t *self)
1322 {
1323 	scsi_hba_tran_t		*tran;
1324 
1325 	ASSERT(scsi_hba_iport_unit_address(self) == NULL);
1326 	if (scsi_hba_iport_unit_address(self))
1327 		return (DDI_FAILURE);		/* self can't be an iport */
1328 
1329 	/* Check all error return conditions upfront */
1330 	tran = ndi_flavorv_get(self, SCSA_FLAVOR_SCSI_DEVICE);
1331 	ASSERT(tran);
1332 	if (tran == NULL)
1333 		return (DDI_FAILURE);
1334 
1335 	ASSERT(tran->tran_open_flag == 0);
1336 	if (tran->tran_open_flag)
1337 		return (DDI_FAILURE);
1338 
1339 	if (!(tran->tran_hba_flags & SCSI_HBA_HBA))
1340 		scsa_tran_teardown(self, tran);
1341 	scsa_nexus_teardown(self, tran);
1342 
1343 	/* Teardown tran_setup_pkt(9E) kmem_cache. */
1344 	if (tran->tran_pkt_cache_ptr) {
1345 		kmem_cache_destroy(tran->tran_pkt_cache_ptr);
1346 		tran->tran_pkt_cache_ptr = NULL;
1347 	}
1348 
1349 	(void) memset(&tran->tran_dma_attr, 0, sizeof (ddi_dma_attr_t));
1350 
1351 	/* Teardown flavor of transport (and ddi_get_driver_private()) */
1352 	ndi_flavorv_set(self, SCSA_FLAVOR_SCSI_DEVICE, NULL);
1353 
1354 	tran->tran_hba_dip = NULL;
1355 
1356 	return (DDI_SUCCESS);
1357 }
1358 
1359 
1360 /*
1361  * Called by an HBA from _fini()
1362  */
1363 void
1364 scsi_hba_fini(struct modlinkage *modlp)
1365 {
1366 	struct dev_ops *hba_dev_ops;
1367 
1368 	SCSI_HBA_LOG((_LOG_TRACE, NULL, NULL, __func__));
1369 
1370 	/* Get the devops structure of this module and clear bus_ops vector. */
1371 	hba_dev_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
1372 
1373 	if (hba_dev_ops->devo_cb_ops == &scsi_hba_cbops)
1374 		hba_dev_ops->devo_cb_ops = NULL;
1375 
1376 	if (hba_dev_ops->devo_getinfo == scsi_hba_info)
1377 		hba_dev_ops->devo_getinfo = NULL;
1378 
1379 	hba_dev_ops->devo_bus_ops = (struct bus_ops *)NULL;
1380 }
1381 
1382 /*
1383  * SAS specific functions
1384  */
1385 sas_hba_tran_t *
1386 sas_hba_tran_alloc(dev_info_t *self)
1387 {
1388 	/* allocate SCSA flavors for self */
1389 	ndi_flavorv_alloc(self, SCSA_NFLAVORS);
1390 	return (kmem_zalloc(sizeof (sas_hba_tran_t), KM_SLEEP));
1391 }
1392 
1393 void
1394 sas_hba_tran_free(sas_hba_tran_t *tran)
1395 {
1396 	kmem_free(tran, sizeof (sas_hba_tran_t));
1397 }
1398 
1399 int
1400 sas_hba_attach_setup(
1401 	dev_info_t		*self,
1402 	sas_hba_tran_t		*tran)
1403 {
1404 	ASSERT(scsi_hba_iport_unit_address(self) == NULL);
1405 	if (scsi_hba_iport_unit_address(self))
1406 		return (DDI_FAILURE);		/* self can't be an iport */
1407 
1408 	/*
1409 	 * The owner of the this devinfo_t was responsible
1410 	 * for informing the framework already about
1411 	 * additional flavors.
1412 	 */
1413 	ndi_flavorv_set(self, SCSA_FLAVOR_SMP, tran);
1414 	return (DDI_SUCCESS);
1415 }
1416 
1417 int
1418 sas_hba_detach(dev_info_t *self)
1419 {
1420 	ASSERT(scsi_hba_iport_unit_address(self) == NULL);
1421 	if (scsi_hba_iport_unit_address(self))
1422 		return (DDI_FAILURE);		/* self can't be an iport */
1423 
1424 	ndi_flavorv_set(self, SCSA_FLAVOR_SMP, NULL);
1425 	return (DDI_SUCCESS);
1426 }
1427 
1428 /*
1429  * SMP child flavored functions
1430  */
1431 static int
1432 smp_busctl_ua(dev_info_t *child, char *addr, int maxlen)
1433 {
1434 	char		*tport;
1435 	char		*wwn;
1436 
1437 	/* limit ndi_devi_findchild_by_callback to expected flavor */
1438 	if (ndi_flavor_get(child) != SCSA_FLAVOR_SMP)
1439 		return (DDI_FAILURE);
1440 
1441 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child,
1442 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1443 	    SCSI_ADDR_PROP_TARGET_PORT, &tport) == DDI_SUCCESS) {
1444 		(void) snprintf(addr, maxlen, "%s", tport);
1445 		ddi_prop_free(tport);
1446 		return (DDI_SUCCESS);
1447 	}
1448 
1449 	/*
1450 	 * NOTE: the following code should be deleted when mpt is changed to
1451 	 * use SCSI_ADDR_PROP_TARGET_PORT instead of SMP_WWN.
1452 	 */
1453 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child,
1454 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1455 	    SMP_WWN, &wwn) == DDI_SUCCESS) {
1456 		(void) snprintf(addr, maxlen, "w%s", wwn);
1457 		ddi_prop_free(wwn);
1458 		return (DDI_SUCCESS);
1459 	}
1460 	return (DDI_FAILURE);
1461 }
1462 
1463 static int
1464 smp_busctl_reportdev(dev_info_t *child)
1465 {
1466 	dev_info_t	*self = ddi_get_parent(child);
1467 	char		*tport;
1468 	char		*wwn;
1469 
1470 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child,
1471 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1472 	    SCSI_ADDR_PROP_TARGET_PORT, &tport) == DDI_SUCCESS) {
1473 		SCSI_HBA_LOG((_LOG_NF(CONT), "?%s%d at %s%d: target-port %s",
1474 		    ddi_driver_name(child), ddi_get_instance(child),
1475 		    ddi_driver_name(self), ddi_get_instance(self), tport));
1476 		ddi_prop_free(tport);
1477 		return (DDI_SUCCESS);
1478 	}
1479 
1480 	/*
1481 	 * NOTE: the following code should be deleted when mpt is changed to
1482 	 * use SCSI_ADDR_PROP_TARGET_PORT instead of SMP_WWN.
1483 	 */
1484 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child,
1485 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1486 	    SMP_WWN, &wwn) == DDI_SUCCESS) {
1487 		SCSI_HBA_LOG((_LOG_NF(CONT), "?%s%d at %s%d: wwn %s",
1488 		    ddi_driver_name(child), ddi_get_instance(child),
1489 		    ddi_driver_name(self), ddi_get_instance(self), wwn));
1490 		ddi_prop_free(wwn);
1491 		return (DDI_SUCCESS);
1492 	}
1493 	return (DDI_FAILURE);
1494 }
1495 
1496 static int
1497 smp_busctl_initchild(dev_info_t *child)
1498 {
1499 	dev_info_t		*self = ddi_get_parent(child);
1500 	sas_hba_tran_t		*tran;
1501 	dev_info_t		*dup;
1502 	char			addr[SCSI_MAXNAMELEN];
1503 	struct smp_device	*smp;
1504 	uint64_t		wwn;
1505 
1506 	tran = ndi_flavorv_get(self, SCSA_FLAVOR_SMP);
1507 	ASSERT(tran);
1508 	if (tran == NULL)
1509 		return (DDI_FAILURE);
1510 
1511 	if (smp_busctl_ua(child, addr, sizeof (addr)) != DDI_SUCCESS)
1512 		return (DDI_NOT_WELL_FORMED);
1513 	if (scsi_wwnstr_to_wwn(addr, &wwn))
1514 		return (DDI_NOT_WELL_FORMED);
1515 
1516 	/* Prevent duplicate nodes.  */
1517 	dup = ndi_devi_findchild_by_callback(self, ddi_node_name(child), addr,
1518 	    smp_busctl_ua);
1519 	if (dup) {
1520 		ASSERT(ndi_flavor_get(dup) == SCSA_FLAVOR_SMP);
1521 		if (ndi_flavor_get(dup) != SCSA_FLAVOR_SMP) {
1522 			SCSI_HBA_LOG((_LOG(1), NULL, child,
1523 			    "init failed: %s@%s: not SMP flavored",
1524 			    ddi_node_name(child), addr));
1525 			return (DDI_FAILURE);
1526 		}
1527 		if (dup != child) {
1528 			SCSI_HBA_LOG((_LOG(4), NULL, child,
1529 			    "init failed: %s@%s: detected duplicate %p",
1530 			    ddi_node_name(child), addr, (void *)dup));
1531 			return (DDI_FAILURE);
1532 		}
1533 	}
1534 
1535 
1536 	/* set the node @addr string */
1537 	ddi_set_name_addr(child, addr);
1538 
1539 	/* Allocate and initialize smp_device. */
1540 	smp = kmem_zalloc(sizeof (struct smp_device), KM_SLEEP);
1541 	smp->dip = child;
1542 	smp->smp_addr.a_hba_tran = tran;
1543 	bcopy(&wwn, smp->smp_addr.a_wwn, SAS_WWN_BYTE_SIZE);
1544 
1545 	ddi_set_driver_private(child, smp);
1546 
1547 	if (tran->tran_smp_init &&
1548 	    ((*tran->tran_smp_init)(self, child, tran, smp) != DDI_SUCCESS)) {
1549 		kmem_free(smp, sizeof (struct smp_device));
1550 		if (ndi_dev_is_hotplug_node(child))
1551 			SCSI_HBA_LOG((_LOG(WARN), NULL, child,
1552 			    "enumeration failed during tran_smp_init"));
1553 		else
1554 			SCSI_HBA_LOG((_LOG(2), NULL, child,
1555 			    "enumeration failed during tran_smp_init"));
1556 		ddi_set_driver_private(child, NULL);
1557 		ddi_set_name_addr(child, NULL);
1558 		return (DDI_FAILURE);
1559 	}
1560 
1561 	return (DDI_SUCCESS);
1562 }
1563 
1564 /*ARGSUSED*/
1565 static int
1566 smp_busctl_uninitchild(dev_info_t *child)
1567 {
1568 	dev_info_t		*self = ddi_get_parent(child);
1569 	struct smp_device	*smp = ddi_get_driver_private(child);
1570 	sas_hba_tran_t		*tran;
1571 
1572 	tran = ndi_flavorv_get(self, SCSA_FLAVOR_SMP);
1573 	ASSERT(smp && tran);
1574 	if ((smp == NULL) || (tran == NULL))
1575 		return (DDI_FAILURE);
1576 
1577 	if (tran->tran_smp_free) {
1578 		(*tran->tran_smp_free) (self, child, tran, smp);
1579 	}
1580 	kmem_free(smp, sizeof (*smp));
1581 	ddi_set_driver_private(child, NULL);
1582 	ddi_set_name_addr(child, NULL);
1583 	return (DDI_SUCCESS);
1584 }
1585 
1586 /* Find an "smp" child at the specified address. */
1587 static dev_info_t *
1588 smp_hba_find_child(dev_info_t *self, char *addr)
1589 {
1590 	dev_info_t	*child;
1591 
1592 	/* Search "smp" devinfo child at specified address. */
1593 	ASSERT(self && DEVI_BUSY_OWNED(self) && addr);
1594 	for (child = ddi_get_child(self); child;
1595 	    child = ddi_get_next_sibling(child)) {
1596 		/* skip non-"smp" nodes */
1597 		if (ndi_flavor_get(child) != SCSA_FLAVOR_SMP)
1598 			continue;
1599 
1600 		/* Attempt initchild to establish unit-address */
1601 		if (i_ddi_node_state(child) < DS_INITIALIZED)
1602 			(void) ddi_initchild(self, child);
1603 
1604 		/* Verify state and non-NULL unit-address. */
1605 		if ((i_ddi_node_state(child) < DS_INITIALIZED) ||
1606 		    (ddi_get_name_addr(child) == NULL))
1607 			continue;
1608 
1609 		/* Return "smp" child if unit-address matches. */
1610 		if (strcmp(ddi_get_name_addr(child), addr) == 0)
1611 			return (child);
1612 	}
1613 	return (NULL);
1614 }
1615 
1616 /*
1617  * Search for "smp" child of self at the specified address. If found, online
1618  * and return with a hold.  Unlike general SCSI configuration, we can assume
1619  * the the device is actually there when we are called (i.e., device is
1620  * created by hotplug, not by bus_config).
1621  */
1622 int
1623 smp_hba_bus_config(dev_info_t *self, char *addr, dev_info_t **childp)
1624 {
1625 	dev_info_t	*child;
1626 	int		circ;
1627 
1628 	ASSERT(self && addr && childp);
1629 	*childp = NULL;
1630 
1631 	/* Search for "smp" child. */
1632 	scsi_hba_devi_enter(self, &circ);
1633 	if ((child = smp_hba_find_child(self, addr)) == NULL) {
1634 		scsi_hba_devi_exit(self, circ);
1635 		return (NDI_FAILURE);
1636 	}
1637 
1638 	/* Attempt online. */
1639 	if (ndi_devi_online(child, 0) != NDI_SUCCESS) {
1640 		scsi_hba_devi_exit(self, circ);
1641 		return (NDI_FAILURE);
1642 	}
1643 
1644 	/* On success, return with active hold. */
1645 	ndi_hold_devi(child);
1646 	scsi_hba_devi_exit(self, circ);
1647 	*childp = child;
1648 	return (NDI_SUCCESS);
1649 }
1650 
1651 
1652 
1653 /* Create "smp" child devinfo node at specified unit-address. */
1654 int
1655 smp_hba_bus_config_taddr(dev_info_t *self, char *addr)
1656 {
1657 	dev_info_t	*child;
1658 	int		circ;
1659 
1660 	/* Search for "smp" child. */
1661 	scsi_hba_devi_enter(self, &circ);
1662 	child = smp_hba_find_child(self, addr);
1663 	if (child) {
1664 		/* Child exists, note if this was a new reinsert. */
1665 		if (ndi_devi_device_insert(child))
1666 			SCSI_HBA_LOG((_LOGCFG, self, NULL,
1667 			    "devinfo smp@%s device_reinsert", addr));
1668 
1669 		scsi_hba_devi_exit(self, circ);
1670 		return (NDI_SUCCESS);
1671 	}
1672 
1673 	/* Allocate "smp" child devinfo node and establish flavor of child. */
1674 	ndi_devi_alloc_sleep(self, "smp", DEVI_SID_HP_NODEID, &child);
1675 	ndi_flavor_set(child, SCSA_FLAVOR_SMP);
1676 
1677 	/* Add unit-address property to child. */
1678 	if (ndi_prop_update_string(DDI_DEV_T_NONE, child,
1679 	    SCSI_ADDR_PROP_TARGET_PORT, addr) != DDI_PROP_SUCCESS) {
1680 		(void) ndi_devi_free(child);
1681 		scsi_hba_devi_exit(self, circ);
1682 		return (NDI_FAILURE);
1683 	}
1684 
1685 	/* Attempt to online the new "smp" node. */
1686 	(void) ndi_devi_online(child, 0);
1687 
1688 	scsi_hba_devi_exit(self, circ);
1689 	return (NDI_SUCCESS);
1690 }
1691 
1692 /*
1693  * Wrapper to scsi_ua_get which takes a devinfo argument instead of a
1694  * scsi_device structure.
1695  */
1696 static int
1697 scsi_busctl_ua(dev_info_t *child, char *addr, int maxlen)
1698 {
1699 	struct scsi_device	*sd;
1700 
1701 	/* limit ndi_devi_findchild_by_callback to expected flavor */
1702 	if (ndi_flavor_get(child) != SCSA_FLAVOR_SCSI_DEVICE)
1703 		return (DDI_FAILURE);
1704 
1705 	/* nodes are named by tran_get_name or default "tgt,lun" */
1706 	sd = ddi_get_driver_private(child);
1707 	if (sd && (scsi_ua_get(sd, addr, maxlen) == 1))
1708 		return (DDI_SUCCESS);
1709 
1710 	return (DDI_FAILURE);
1711 }
1712 
1713 static int
1714 scsi_busctl_reportdev(dev_info_t *child)
1715 {
1716 	dev_info_t		*self = ddi_get_parent(child);
1717 	struct scsi_device	*sd = ddi_get_driver_private(child);
1718 	scsi_hba_tran_t		*tran;
1719 	char			ua[SCSI_MAXNAMELEN];
1720 	char			ra[SCSI_MAXNAMELEN];
1721 
1722 	SCSI_HBA_LOG((_LOG_TRACE, NULL, child, __func__));
1723 
1724 	tran = ndi_flavorv_get(self, SCSA_FLAVOR_SCSI_DEVICE);
1725 	ASSERT(tran && sd);
1726 	if ((tran == NULL) || (sd == NULL))
1727 		return (DDI_FAILURE);
1728 
1729 	/* get the unit_address and bus_addr information */
1730 	if ((scsi_ua_get(sd, ua, sizeof (ua)) == 0) ||
1731 	    (scsi_ua_get_reportdev(sd, ra, sizeof (ra)) == 0)) {
1732 		SCSI_HBA_LOG((_LOG(WARN), NULL, child, "REPORTDEV failure"));
1733 		return (DDI_FAILURE);
1734 	}
1735 
1736 	if (tran->tran_get_name == NULL)
1737 		SCSI_HBA_LOG((_LOG_NF(CONT), "?%s%d at %s%d: %s",
1738 		    ddi_driver_name(child), ddi_get_instance(child),
1739 		    ddi_driver_name(self), ddi_get_instance(self), ra));
1740 	else if (*ra)
1741 		SCSI_HBA_LOG((_LOG_NF(CONT),
1742 		    "?%s%d at %s%d: unit-address %s: %s",
1743 		    ddi_driver_name(child), ddi_get_instance(child),
1744 		    ddi_driver_name(self), ddi_get_instance(self), ua, ra));
1745 	else
1746 		SCSI_HBA_LOG((_LOG_NF(CONT),
1747 		    "?%s%d at %s%d: unit-address %s",
1748 		    ddi_driver_name(child), ddi_get_instance(child),
1749 		    ddi_driver_name(self), ddi_get_instance(self), ua));
1750 
1751 	return (DDI_SUCCESS);
1752 }
1753 
1754 
1755 /*
1756  * scsi_busctl_initchild is called to initialize the SCSA transport for
1757  * communication with a particular child scsi target device. Successful
1758  * initialization requires properties on the node which describe the address
1759  * of the target device. If the address of the target device can't be
1760  * determined from properties then DDI_NOT_WELL_FORMED is returned. Nodes that
1761  * are DDI_NOT_WELL_FORMED are considered an implementation artifact and
1762  * are hidden from devinfo snapshots by calling ndi_devi_set_hidden().
1763  * The child may be one of the following types of devinfo nodes:
1764  *
1765  * OBP node:
1766  *	OBP does not enumerate target devices attached a SCSI bus. These
1767  *	template/stub/wild-card nodes are a legacy artifact for support of old
1768  *	driver loading methods. Since they have no properties,
1769  *	DDI_NOT_WELL_FORMED will be returned.
1770  *
1771  * SID node:
1772  *	The node may be either a:
1773  *	    o	probe/barrier SID node
1774  *	    o	a dynamic SID target node
1775  *
1776  * driver.conf node: The situation for this nexus is different than most.
1777  *	Typically a driver.conf node definition is used to either define a
1778  *	new child devinfo node or to further decorate (via merge) a SID
1779  *	child with properties. In our case we use the nodes for *both*
1780  *	purposes.
1781  *
1782  * In both the SID node and driver.conf node cases we must form the nodes
1783  * "@addr" from the well-known scsi(9P) device unit-address properties on
1784  * the node.
1785  *
1786  * For HBA drivers that implement the deprecated tran_get_name interface,
1787  * "@addr" construction involves having that driver interpret properties via
1788  * scsi_busctl_ua -> scsi_ua_get -> tran_get_name: there is no
1789  * requirement for the property names to be well-known.
1790  *
1791  * NOTE: We don't currently support "merge".  When this support is added a
1792  * specific property, like "unit-address", should *always* identify a
1793  * driver.conf node that needs to be merged into a specific SID node. When
1794  * enumeration is enabled, a .conf node without the "unit-address" property
1795  * should be ignored.  The best way to establish the "unit-address" property
1796  * would be to have the system assign parent= and unit-address= from an
1797  * instance=# driver.conf entry (by using the instance tree).
1798  */
1799 static int
1800 scsi_busctl_initchild(dev_info_t *child)
1801 {
1802 	dev_info_t		*self = ddi_get_parent(child);
1803 	dev_info_t		*dup;
1804 	scsi_hba_tran_t		*tran;
1805 	struct scsi_device	*sd;
1806 	scsi_hba_tran_t		*tran_clone;
1807 	char			*class;
1808 	int			tgt;
1809 	int			lun;
1810 	int			sfunc;
1811 	int			err = DDI_FAILURE;
1812 	char			addr[SCSI_MAXNAMELEN];
1813 
1814 	ASSERT(DEVI_BUSY_OWNED(self));
1815 	SCSI_HBA_LOG((_LOG(4), NULL, child, "init begin"));
1816 
1817 	/*
1818 	 * For a driver like fp with multiple upper-layer-protocols
1819 	 * it is possible for scsi_hba_init in _init to plumb SCSA
1820 	 * and have the load of fcp (which does scsi_hba_attach_setup)
1821 	 * to fail.  In this case we may get here with a NULL hba.
1822 	 */
1823 	tran = ndi_flavorv_get(self, SCSA_FLAVOR_SCSI_DEVICE);
1824 	if (tran == NULL)
1825 		return (DDI_NOT_WELL_FORMED);
1826 
1827 	/*
1828 	 * OBP may create template/stub/wild-card nodes for legacy driver
1829 	 * loading methods. These nodes have no properties, so we lack the
1830 	 * addressing properties to initchild them. Hide the node and return
1831 	 * DDI_NOT_WELL_FORMED.
1832 	 *
1833 	 * Future: define/use a ndi_devi_has_properties(dip) type interface.
1834 	 *
1835 	 * NOTE: It would be nice if we could delete these ill formed nodes by
1836 	 * implementing a DDI_NOT_WELL_FORMED_DELETE return code. This can't
1837 	 * be done until leadville debug code removes its dependencies
1838 	 * on the devinfo still being present after a failed ndi_devi_online.
1839 	 */
1840 	if ((DEVI(child)->devi_hw_prop_ptr == NULL) &&
1841 	    (DEVI(child)->devi_drv_prop_ptr == NULL) &&
1842 	    (DEVI(child)->devi_sys_prop_ptr == NULL)) {
1843 		SCSI_HBA_LOG((_LOG(4), NULL, child,
1844 		    "init failed: no properties"));
1845 		ndi_devi_set_hidden(child);
1846 		return (DDI_NOT_WELL_FORMED);
1847 	}
1848 
1849 	/* get legacy SPI addressing properties */
1850 	if ((tgt = ddi_prop_get_int(DDI_DEV_T_ANY, child,
1851 	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
1852 	    SCSI_ADDR_PROP_TARGET, -1)) == -1) {
1853 		tgt = 0;
1854 		/*
1855 		 * A driver.conf node for merging always has a target= property,
1856 		 * even if it is just a dummy that does not contain the real
1857 		 * target address. However drivers that register devids may
1858 		 * create stub driver.conf nodes without a target= property so
1859 		 * that pathological devid resolution works. Hide the stub
1860 		 * node and return DDI_NOT_WELL_FORMED.
1861 		 */
1862 		if (!scsi_hba_dev_is_sid(child)) {
1863 			SCSI_HBA_LOG((_LOG(4), NULL, child,
1864 			    "init failed: stub .conf node"));
1865 			ndi_devi_set_hidden(child);
1866 			return (DDI_NOT_WELL_FORMED);
1867 		}
1868 	}
1869 	lun = ddi_prop_get_int(DDI_DEV_T_ANY, child,
1870 	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0);
1871 	sfunc = ddi_prop_get_int(DDI_DEV_T_ANY, child,
1872 	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, SCSI_ADDR_PROP_SFUNC, -1);
1873 
1874 	/*
1875 	 * The scsi_address structure may not specify all the addressing
1876 	 * information. For an old HBA that doesn't support tran_get_name
1877 	 * (most pre-SCSI-3 HBAs) the scsi_address structure is still used,
1878 	 * so the target property must exist and the LUN must be < 256.
1879 	 */
1880 	if ((tran->tran_get_name == NULL) &&
1881 	    ((tgt >= USHRT_MAX) || (lun >= 256))) {
1882 		SCSI_HBA_LOG((_LOG(1), NULL, child,
1883 		    "init failed: illegal/missing properties"));
1884 		ndi_devi_set_hidden(child);
1885 		return (DDI_NOT_WELL_FORMED);
1886 	}
1887 
1888 	/*
1889 	 * We need to initialize a fair amount of our environment to invoke
1890 	 * tran_get_name (via scsi_busctl_ua and scsi_ua_get) to
1891 	 * produce the "@addr" name from addressing properties. Allocate and
1892 	 * initialize scsi device structure.
1893 	 */
1894 	sd = kmem_zalloc(sizeof (struct scsi_device), KM_SLEEP);
1895 	mutex_init(&sd->sd_mutex, NULL, MUTEX_DRIVER, NULL);
1896 	sd->sd_dev = child;
1897 	sd->sd_pathinfo = NULL;
1898 	sd->sd_uninit_prevent = 0;
1899 	ddi_set_driver_private(child, sd);
1900 
1901 	if (tran->tran_hba_flags & SCSI_HBA_ADDR_COMPLEX) {
1902 		/*
1903 		 * For a SCSI_HBA_ADDR_COMPLEX transport we store a pointer to
1904 		 * scsi_device in the scsi_address structure.  This allows an
1905 		 * HBA driver to find its per-scsi_device private data
1906 		 * (accessible to the HBA given just the scsi_address by using
1907 		 *  scsi_address_device(9F)/scsi_device_hba_private_get(9F)).
1908 		 */
1909 		sd->sd_address.a.a_sd = sd;
1910 		tran_clone = NULL;
1911 	} else {
1912 		/*
1913 		 * Initialize the scsi_address so that a SCSI-2 target driver
1914 		 * talking to a SCSI-2 device on a SCSI-3 bus (spi) continues
1915 		 * to work. We skew the secondary function value so that we
1916 		 * can tell from the address structure if we are processing
1917 		 * a secondary function request.
1918 		 */
1919 		sd->sd_address.a_target = (ushort_t)tgt;
1920 		sd->sd_address.a_lun = (uchar_t)lun;
1921 		if (sfunc == -1)
1922 			sd->sd_address.a_sublun = (uchar_t)0;
1923 		else
1924 			sd->sd_address.a_sublun = (uchar_t)sfunc + 1;
1925 
1926 		/*
1927 		 * NOTE: Don't limit LUNs to scsi_options value because a
1928 		 * scsi_device discovered via SPI dynamic enumeration might
1929 		 * still support SCMD_REPORT_LUNS.
1930 		 */
1931 
1932 		/*
1933 		 * Deprecated: Use SCSI_HBA_ADDR_COMPLEX:
1934 		 *   Clone transport structure if requested. Cloning allows
1935 		 *   an HBA to maintain target-specific information if
1936 		 *   necessary, such as target addressing information that
1937 		 *   does not adhere to the scsi_address structure format.
1938 		 */
1939 		if (tran->tran_hba_flags & SCSI_HBA_TRAN_CLONE) {
1940 			tran_clone = kmem_alloc(
1941 			    sizeof (scsi_hba_tran_t), KM_SLEEP);
1942 			bcopy((caddr_t)tran,
1943 			    (caddr_t)tran_clone, sizeof (scsi_hba_tran_t));
1944 			tran = tran_clone;
1945 			tran->tran_sd = sd;
1946 		} else {
1947 			tran_clone = NULL;
1948 			ASSERT(tran->tran_sd == NULL);
1949 		}
1950 	}
1951 
1952 	/* establish scsi_address pointer to the HBA's tran structure */
1953 	sd->sd_address.a_hba_tran = tran;
1954 
1955 	/*
1956 	 * This is a grotty hack that allows direct-access (non-scsa) drivers
1957 	 * (like chs, ata, and mlx which all make cmdk children) to put its
1958 	 * own vector in the 'a_hba_tran' field. When all the drivers that do
1959 	 * this are fixed, please remove this hack.
1960 	 *
1961 	 * NOTE: This hack is also shows up in the DEVP_TO_TRAN implementation
1962 	 * in scsi_confsubr.c.
1963 	 */
1964 	sd->sd_tran_safe = tran;
1965 
1966 	/*
1967 	 * If the class property is not already established, set it to "scsi".
1968 	 * This is done so that parent= driver.conf nodes have class.
1969 	 */
1970 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child,
1971 	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, "class",
1972 	    &class) == DDI_PROP_SUCCESS) {
1973 		ddi_prop_free(class);
1974 	} else if (ndi_prop_update_string(DDI_DEV_T_NONE, child,
1975 	    "class", "scsi") != DDI_PROP_SUCCESS) {
1976 		SCSI_HBA_LOG((_LOG(2), NULL, child, "init failed: class"));
1977 		ndi_devi_set_hidden(child);
1978 		err = DDI_NOT_WELL_FORMED;
1979 		goto failure;
1980 	}
1981 
1982 	/* Establish the @addr name of the child. */
1983 	*addr = '\0';
1984 	if (scsi_busctl_ua(child, addr, sizeof (addr)) != DDI_SUCCESS) {
1985 		/*
1986 		 * Some driver.conf files add bogus target properties (relative
1987 		 * to their nexus representation of target) to their stub
1988 		 * nodes, causing the check above to not filter them.
1989 		 */
1990 		SCSI_HBA_LOG((_LOG(3), NULL, child,
1991 		    "init failed: scsi_busctl_ua call"));
1992 		ndi_devi_set_hidden(child);
1993 		err = DDI_NOT_WELL_FORMED;
1994 		goto failure;
1995 	}
1996 	if (*addr == '\0') {
1997 		SCSI_HBA_LOG((_LOG(2), NULL, child, "init failed: ua"));
1998 		ndi_devi_set_hidden(child);
1999 		err = DDI_NOT_WELL_FORMED;
2000 		goto failure;
2001 	}
2002 
2003 	/* Prevent duplicate nodes.  */
2004 	dup = ndi_devi_findchild_by_callback(self, ddi_node_name(child), addr,
2005 	    scsi_busctl_ua);
2006 	if (dup) {
2007 		ASSERT(ndi_flavor_get(dup) == SCSA_FLAVOR_SCSI_DEVICE);
2008 		if (ndi_flavor_get(dup) != SCSA_FLAVOR_SCSI_DEVICE) {
2009 			SCSI_HBA_LOG((_LOG(1), NULL, child,
2010 			    "init failed: %s@%s: not SCSI_DEVICE flavored",
2011 			    ddi_node_name(child), addr));
2012 			goto failure;
2013 		}
2014 		if (dup != child) {
2015 			SCSI_HBA_LOG((_LOG(4), NULL, child,
2016 			    "init failed: %s@%s: detected duplicate %p",
2017 			    ddi_node_name(child), addr, (void *)dup));
2018 			goto failure;
2019 		}
2020 	}
2021 
2022 	/* set the node @addr string */
2023 	ddi_set_name_addr(child, addr);
2024 
2025 	/* call HBA's target init entry point if it exists */
2026 	if (tran->tran_tgt_init != NULL) {
2027 		SCSI_HBA_LOG((_LOG(4), NULL, child, "init tran_tgt_init"));
2028 
2029 		if ((*tran->tran_tgt_init)
2030 		    (self, child, tran, sd) != DDI_SUCCESS) {
2031 			if (ndi_dev_is_hotplug_node(child))
2032 				SCSI_HBA_LOG((_LOG(WARN), NULL, child,
2033 				    "enumeration failed during tran_tgt_init"));
2034 			else
2035 				SCSI_HBA_LOG((_LOG(2), NULL, child,
2036 				    "enumeration failed during tran_tgt_init"));
2037 			goto failure;
2038 		}
2039 	}
2040 
2041 	SCSI_HBA_LOG((_LOG(3), NULL, child, "init successful"));
2042 	return (DDI_SUCCESS);
2043 
2044 failure:
2045 	if (tran_clone)
2046 		kmem_free(tran_clone, sizeof (scsi_hba_tran_t));
2047 	mutex_destroy(&sd->sd_mutex);
2048 	kmem_free(sd, sizeof (*sd));
2049 	ddi_set_driver_private(child, NULL);
2050 	ddi_set_name_addr(child, NULL);
2051 
2052 	return (err);		/* remove the node */
2053 }
2054 
2055 static int
2056 scsi_busctl_uninitchild(dev_info_t *child)
2057 {
2058 	dev_info_t		*self = ddi_get_parent(child);
2059 	struct scsi_device	*sd = ddi_get_driver_private(child);
2060 	scsi_hba_tran_t		*tran;
2061 	scsi_hba_tran_t		*tran_clone;
2062 
2063 	ASSERT(DEVI_BUSY_OWNED(self));
2064 
2065 	tran = ndi_flavorv_get(self, SCSA_FLAVOR_SCSI_DEVICE);
2066 	ASSERT(tran && sd);
2067 	if ((tran == NULL) || (sd == NULL))
2068 		return (DDI_FAILURE);
2069 
2070 	/*
2071 	 * We use sd_uninit_prevent to avoid uninitializing barrier/probe
2072 	 * nodes that are still in use. Since barrier/probe nodes are not
2073 	 * attached we can't prevent their state demotion via ndi_hold_devi.
2074 	 */
2075 	if (sd->sd_uninit_prevent) {
2076 		SCSI_HBA_LOG((_LOG(2), NULL, child, "uninit prevented"));
2077 		return (DDI_FAILURE);
2078 	}
2079 
2080 	/*
2081 	 * Don't uninitialize a client node if it still has paths.
2082 	 */
2083 	if (MDI_CLIENT(child) && mdi_client_get_path_count(child)) {
2084 		SCSI_HBA_LOG((_LOG(2), NULL, child,
2085 		    "uninit prevented, client has paths"));
2086 		return (DDI_FAILURE);
2087 	}
2088 
2089 	SCSI_HBA_LOG((_LOG(3), NULL, child, "uninit begin"));
2090 
2091 	if (tran->tran_hba_flags & SCSI_HBA_TRAN_CLONE) {
2092 		tran_clone = sd->sd_address.a_hba_tran;
2093 
2094 		/* ... grotty hack, involving sd_tran_safe, continued. */
2095 		if (tran_clone != sd->sd_tran_safe) {
2096 			tran_clone = sd->sd_tran_safe;
2097 #ifdef	DEBUG
2098 			/*
2099 			 * Complain so things get fixed and hack can, at
2100 			 * some point in time, be removed.
2101 			 */
2102 			SCSI_HBA_LOG((_LOG(WARN), self, NULL,
2103 			    "'%s' is corrupting a_hba_tran", sd->sd_dev ?
2104 			    ddi_driver_name(sd->sd_dev) : "unknown_driver"));
2105 #endif	/* DEBUG */
2106 		}
2107 
2108 		ASSERT(tran_clone->tran_hba_flags & SCSI_HBA_TRAN_CLONE);
2109 		ASSERT(tran_clone->tran_sd == sd);
2110 		tran = tran_clone;
2111 	} else {
2112 		tran_clone = NULL;
2113 		ASSERT(tran->tran_sd == NULL);
2114 	}
2115 
2116 	/*
2117 	 * To simplify host adapter drivers we guarantee that multiple
2118 	 * tran_tgt_init(9E) calls of the same unit address are never
2119 	 * active at the same time.  This requires that we call
2120 	 * tran_tgt_free on probe/barrier nodes directly prior to
2121 	 * uninitchild.
2122 	 *
2123 	 * NOTE: To correctly support SCSI_HBA_TRAN_CLONE, we must use
2124 	 * the (possibly cloned) hba_tran pointer from the scsi_device
2125 	 * instead of hba_tran.
2126 	 */
2127 	if (tran->tran_tgt_free) {
2128 		if (!scsi_hba_devi_is_barrier(child)) {
2129 			SCSI_HBA_LOG((_LOG(4), NULL, child,
2130 			    "uninit tran_tgt_free"));
2131 
2132 			(*tran->tran_tgt_free) (self, child, tran, sd);
2133 		} else {
2134 			SCSI_HBA_LOG((_LOG(4), NULL, child,
2135 			    "uninit tran_tgt_free already done"));
2136 		}
2137 	}
2138 
2139 	/*
2140 	 * If a inquiry data is still allocated (by scsi_probe()) we
2141 	 * free the allocation here. This keeps scsi_inq valid for the
2142 	 * same duration as the corresponding inquiry properties. It
2143 	 * also allows a tran_tgt_init() implementation that establishes
2144 	 * sd_inq to deal with deallocation in its tran_tgt_free
2145 	 * (setting sd_inq back to NULL) without upsetting the
2146 	 * framework. Moving the inquiry free here also allows setting
2147 	 * of sd_uninit_prevent to preserve the data for lun0 based
2148 	 * scsi_get_device_type_scsi_options() calls.
2149 	 */
2150 	if (sd->sd_inq) {
2151 		kmem_free(sd->sd_inq, SUN_INQSIZE);
2152 		sd->sd_inq = (struct scsi_inquiry *)NULL;
2153 	}
2154 
2155 	mutex_destroy(&sd->sd_mutex);
2156 	if (tran_clone)
2157 		kmem_free(tran_clone, sizeof (scsi_hba_tran_t));
2158 	kmem_free(sd, sizeof (*sd));
2159 
2160 	ddi_set_driver_private(child, NULL);
2161 	SCSI_HBA_LOG((_LOG(3), NULL, child, "uninit complete"));
2162 	ddi_set_name_addr(child, NULL);
2163 	return (DDI_SUCCESS);
2164 }
2165 
2166 static int
2167 iport_busctl_ua(dev_info_t *child, char *addr, int maxlen)
2168 {
2169 	char	*iport_ua;
2170 
2171 	/* limit ndi_devi_findchild_by_callback to expected flavor */
2172 	if (ndi_flavor_get(child) != SCSA_FLAVOR_IPORT)
2173 		return (DDI_FAILURE);
2174 
2175 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child,
2176 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
2177 	    SCSI_ADDR_PROP_IPORTUA, &iport_ua) != DDI_SUCCESS) {
2178 		return (DDI_FAILURE);
2179 	}
2180 
2181 	(void) snprintf(addr, maxlen, "%s", iport_ua);
2182 	ddi_prop_free(iport_ua);
2183 	return (DDI_SUCCESS);
2184 }
2185 
2186 static int
2187 iport_busctl_reportdev(dev_info_t *child)
2188 {
2189 	dev_info_t	*self = ddi_get_parent(child);
2190 	char		*iport_ua;
2191 	char		*initiator_port = NULL;
2192 
2193 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child,
2194 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
2195 	    SCSI_ADDR_PROP_IPORTUA, &iport_ua) != DDI_SUCCESS)
2196 		return (DDI_FAILURE);
2197 
2198 	(void) ddi_prop_lookup_string(DDI_DEV_T_ANY, child,
2199 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
2200 	    SCSI_ADDR_PROP_INITIATOR_PORT, &initiator_port);
2201 
2202 	if (initiator_port) {
2203 		SCSI_HBA_LOG((_LOG_NF(CONT),
2204 		    "?%s%d at %s%d: %s %s %s %s",
2205 		    ddi_driver_name(child), ddi_get_instance(child),
2206 		    ddi_driver_name(self), ddi_get_instance(self),
2207 		    SCSI_ADDR_PROP_INITIATOR_PORT, initiator_port,
2208 		    SCSI_ADDR_PROP_IPORTUA, iport_ua));
2209 		ddi_prop_free(initiator_port);
2210 	} else {
2211 		SCSI_HBA_LOG((_LOG_NF(CONT), "?%s%d at %s%d: %s %s",
2212 		    ddi_driver_name(child), ddi_get_instance(child),
2213 		    ddi_driver_name(self), ddi_get_instance(self),
2214 		    SCSI_ADDR_PROP_IPORTUA, iport_ua));
2215 	}
2216 	ddi_prop_free(iport_ua);
2217 	return (DDI_SUCCESS);
2218 }
2219 
2220 /* initchild SCSA iport 'child' node */
2221 static int
2222 iport_busctl_initchild(dev_info_t *child)
2223 {
2224 	dev_info_t	*self = ddi_get_parent(child);
2225 	dev_info_t	*dup = NULL;
2226 	char		addr[SCSI_MAXNAMELEN];
2227 
2228 	if (iport_busctl_ua(child, addr, sizeof (addr)) != DDI_SUCCESS)
2229 		return (DDI_NOT_WELL_FORMED);
2230 
2231 	/* Prevent duplicate nodes.  */
2232 	dup = ndi_devi_findchild_by_callback(self, ddi_node_name(child), addr,
2233 	    iport_busctl_ua);
2234 	if (dup) {
2235 		ASSERT(ndi_flavor_get(dup) == SCSA_FLAVOR_IPORT);
2236 		if (ndi_flavor_get(dup) != SCSA_FLAVOR_IPORT) {
2237 			SCSI_HBA_LOG((_LOG(1), NULL, child,
2238 			    "init failed: %s@%s: not IPORT flavored",
2239 			    ddi_node_name(child), addr));
2240 			return (DDI_FAILURE);
2241 		}
2242 		if (dup != child) {
2243 			SCSI_HBA_LOG((_LOG(4), NULL, child,
2244 			    "init failed: %s@%s: detected duplicate %p",
2245 			    ddi_node_name(child), addr, (void *)dup));
2246 			return (DDI_FAILURE);
2247 		}
2248 	}
2249 
2250 	/* set the node @addr string */
2251 	ddi_set_name_addr(child, addr);
2252 
2253 	return (DDI_SUCCESS);
2254 }
2255 
2256 /* uninitchild SCSA iport 'child' node */
2257 static int
2258 iport_busctl_uninitchild(dev_info_t *child)
2259 {
2260 	ddi_set_name_addr(child, NULL);
2261 	return (DDI_SUCCESS);
2262 }
2263 
2264 /* Uninitialize scsi_device flavor of transport on SCSA iport 'child' node. */
2265 static void
2266 iport_postdetach_tran_scsi_device(dev_info_t *child)
2267 {
2268 	scsi_hba_tran_t		*tran;
2269 
2270 	tran = ndi_flavorv_get(child, SCSA_FLAVOR_SCSI_DEVICE);
2271 	if (tran == NULL)
2272 		return;
2273 
2274 	scsa_tran_teardown(child, tran);
2275 	scsa_nexus_teardown(child, tran);
2276 
2277 	ndi_flavorv_set(child, SCSA_FLAVOR_SCSI_DEVICE, NULL);
2278 	scsi_hba_tran_free(tran);
2279 }
2280 
2281 /* Initialize scsi_device flavor of transport on SCSA iport 'child' node. */
2282 static void
2283 iport_preattach_tran_scsi_device(dev_info_t *child)
2284 {
2285 	dev_info_t	*hba = ddi_get_parent(child);
2286 	scsi_hba_tran_t	*htran;
2287 	scsi_hba_tran_t	*tran;
2288 
2289 	/* parent HBA node scsi_device tran is required */
2290 	htran = ndi_flavorv_get(hba, SCSA_FLAVOR_SCSI_DEVICE);
2291 	ASSERT(htran);
2292 
2293 	/* Allocate iport child's scsi_device transport vector */
2294 	tran = scsi_hba_tran_alloc(child, SCSI_HBA_CANSLEEP);
2295 	ASSERT(tran);
2296 
2297 	/* Structure-copy scsi_device transport of HBA to iport. */
2298 	*tran = *htran;
2299 
2300 	/*
2301 	 * Reset scsi_device transport fields not shared with the
2302 	 * parent, and not established below.
2303 	 */
2304 	tran->tran_open_flag = 0;
2305 	tran->tran_hba_private = NULL;
2306 
2307 	/* Establish the devinfo context of this tran structure. */
2308 	tran->tran_iport_dip = child;
2309 
2310 	/* Clear SCSI_HBA_SCSA flags (except TA) */
2311 	tran->tran_hba_flags &=
2312 	    ~(SCSI_HBA_SCSA_FM | SCSI_HBA_SCSA_PHCI);	/* clear parent state */
2313 	tran->tran_hba_flags |= SCSI_HBA_SCSA_TA;	/* always TA */
2314 	tran->tran_hba_flags &= ~SCSI_HBA_HBA;		/* never HBA */
2315 
2316 	/* Establish flavor of transport (and ddi_get_driver_private()) */
2317 	ndi_flavorv_set(child, SCSA_FLAVOR_SCSI_DEVICE, tran);
2318 
2319 	/* Setup iport node */
2320 	if ((scsa_nexus_setup(child, tran) != DDI_SUCCESS) ||
2321 	    (scsa_tran_setup(child, tran) != DDI_SUCCESS))
2322 		iport_postdetach_tran_scsi_device(child);
2323 }
2324 
2325 /* Uninitialize smp_device flavor of transport on SCSA iport 'child' node. */
2326 static void
2327 iport_postdetach_tran_smp_device(dev_info_t *child)
2328 {
2329 	sas_hba_tran_t	*tran;
2330 
2331 	tran = ndi_flavorv_get(child, SCSA_FLAVOR_SMP);
2332 	if (tran == NULL)
2333 		return;
2334 
2335 	ndi_flavorv_set(child, SCSA_FLAVOR_SMP, NULL);
2336 	sas_hba_tran_free(tran);
2337 }
2338 
2339 /* Initialize smp_device flavor of transport on SCSA iport 'child' node. */
2340 static void
2341 iport_preattach_tran_smp_device(dev_info_t *child)
2342 {
2343 	dev_info_t	*hba = ddi_get_parent(child);
2344 	sas_hba_tran_t	*htran;
2345 	sas_hba_tran_t	*tran;
2346 
2347 	/* parent HBA node smp_device tran is optional */
2348 	htran = ndi_flavorv_get(hba, SCSA_FLAVOR_SMP);
2349 	if (htran == NULL) {
2350 		ndi_flavorv_set(child, SCSA_FLAVOR_SMP, NULL);
2351 		return;
2352 	}
2353 
2354 	/* Allocate iport child's smp_device transport vector */
2355 	tran = sas_hba_tran_alloc(child);
2356 
2357 	/* Structure-copy smp_device transport of HBA to iport. */
2358 	*tran = *htran;
2359 
2360 	/* Establish flavor of transport */
2361 	ndi_flavorv_set(child, SCSA_FLAVOR_SMP, tran);
2362 }
2363 
2364 /*
2365  * Generic bus_ctl operations for SCSI HBA's,
2366  * hiding the busctl interface from the HBA.
2367  */
2368 /*ARGSUSED*/
2369 static int
2370 scsi_hba_bus_ctl(
2371 	dev_info_t		*self,
2372 	dev_info_t		*child,
2373 	ddi_ctl_enum_t		op,
2374 	void			*arg,
2375 	void			*result)
2376 {
2377 	int			child_flavor = 0;
2378 	int			val;
2379 	ddi_dma_attr_t		*attr;
2380 	scsi_hba_tran_t		*tran;
2381 	struct attachspec	*as;
2382 	struct detachspec	*ds;
2383 
2384 	/* For some ops, child is 'arg'. */
2385 	if ((op == DDI_CTLOPS_INITCHILD) || (op == DDI_CTLOPS_UNINITCHILD))
2386 		child = (dev_info_t *)arg;
2387 
2388 	/* Determine the flavor of the child: scsi, smp, iport */
2389 	child_flavor = ndi_flavor_get(child);
2390 
2391 	switch (op) {
2392 	case DDI_CTLOPS_INITCHILD:
2393 		switch (child_flavor) {
2394 		case SCSA_FLAVOR_SCSI_DEVICE:
2395 			return (scsi_busctl_initchild(child));
2396 		case SCSA_FLAVOR_SMP:
2397 			return (smp_busctl_initchild(child));
2398 		case SCSA_FLAVOR_IPORT:
2399 			return (iport_busctl_initchild(child));
2400 		default:
2401 			return (DDI_FAILURE);
2402 		}
2403 		/* NOTREACHED */
2404 
2405 	case DDI_CTLOPS_UNINITCHILD:
2406 		switch (child_flavor) {
2407 		case SCSA_FLAVOR_SCSI_DEVICE:
2408 			return (scsi_busctl_uninitchild(child));
2409 		case SCSA_FLAVOR_SMP:
2410 			return (smp_busctl_uninitchild(child));
2411 		case SCSA_FLAVOR_IPORT:
2412 			return (iport_busctl_uninitchild(child));
2413 		default:
2414 			return (DDI_FAILURE);
2415 		}
2416 		/* NOTREACHED */
2417 
2418 	case DDI_CTLOPS_REPORTDEV:
2419 		switch (child_flavor) {
2420 		case SCSA_FLAVOR_SCSI_DEVICE:
2421 			return (scsi_busctl_reportdev(child));
2422 		case SCSA_FLAVOR_SMP:
2423 			return (smp_busctl_reportdev(child));
2424 		case SCSA_FLAVOR_IPORT:
2425 			return (iport_busctl_reportdev(child));
2426 		default:
2427 			return (DDI_FAILURE);
2428 		}
2429 		/* NOTREACHED */
2430 
2431 	case DDI_CTLOPS_ATTACH:
2432 		as = (struct attachspec *)arg;
2433 
2434 		if (child_flavor != SCSA_FLAVOR_IPORT)
2435 			return (DDI_SUCCESS);
2436 
2437 		/* iport processing */
2438 		if (as->when == DDI_PRE) {
2439 			/* setup pre attach(9E) */
2440 			iport_preattach_tran_scsi_device(child);
2441 			iport_preattach_tran_smp_device(child);
2442 		} else if ((as->when == DDI_POST) &&
2443 		    (as->result != DDI_SUCCESS)) {
2444 			/* cleanup if attach(9E) failed */
2445 			iport_postdetach_tran_scsi_device(child);
2446 			iport_postdetach_tran_smp_device(child);
2447 		}
2448 		return (DDI_SUCCESS);
2449 
2450 	case DDI_CTLOPS_DETACH:
2451 		ds = (struct detachspec *)arg;
2452 
2453 		if (child_flavor != SCSA_FLAVOR_IPORT)
2454 			return (DDI_SUCCESS);
2455 
2456 		/* iport processing */
2457 		if ((ds->when == DDI_POST) &&
2458 		    (ds->result == DDI_SUCCESS)) {
2459 			/* cleanup if detach(9E) was successful */
2460 			iport_postdetach_tran_scsi_device(child);
2461 			iport_postdetach_tran_smp_device(child);
2462 		}
2463 		return (DDI_SUCCESS);
2464 
2465 	case DDI_CTLOPS_IOMIN:
2466 		tran = ddi_get_driver_private(self);
2467 		ASSERT(tran);
2468 		if (tran == NULL)
2469 			return (DDI_FAILURE);
2470 
2471 		/*
2472 		 * The 'arg' value of nonzero indicates 'streaming'
2473 		 * mode. If in streaming mode, pick the largest
2474 		 * of our burstsizes available and say that that
2475 		 * is our minimum value (modulo what minxfer is).
2476 		 */
2477 		attr = &tran->tran_dma_attr;
2478 		val = *((int *)result);
2479 		val = maxbit(val, attr->dma_attr_minxfer);
2480 		*((int *)result) = maxbit(val, ((intptr_t)arg ?
2481 		    (1<<ddi_ffs(attr->dma_attr_burstsizes)-1) :
2482 		    (1<<(ddi_fls(attr->dma_attr_burstsizes)-1))));
2483 
2484 		return (ddi_ctlops(self, child, op, arg, result));
2485 
2486 	case DDI_CTLOPS_SIDDEV:
2487 		return (ndi_dev_is_persistent_node(child) ?
2488 		    DDI_SUCCESS : DDI_FAILURE);
2489 
2490 	case DDI_CTLOPS_POWER:
2491 		return (DDI_SUCCESS);
2492 
2493 	/*
2494 	 * These ops correspond to functions that "shouldn't" be called
2495 	 * by a SCSI target driver. So we whine when we're called.
2496 	 */
2497 	case DDI_CTLOPS_DMAPMAPC:
2498 	case DDI_CTLOPS_REPORTINT:
2499 	case DDI_CTLOPS_REGSIZE:
2500 	case DDI_CTLOPS_NREGS:
2501 	case DDI_CTLOPS_SLAVEONLY:
2502 	case DDI_CTLOPS_AFFINITY:
2503 	case DDI_CTLOPS_POKE:
2504 	case DDI_CTLOPS_PEEK:
2505 		SCSI_HBA_LOG((_LOG(WARN), self, NULL, "invalid op (%d)", op));
2506 		return (DDI_FAILURE);
2507 
2508 	/* Everything else we pass up */
2509 	case DDI_CTLOPS_PTOB:
2510 	case DDI_CTLOPS_BTOP:
2511 	case DDI_CTLOPS_BTOPR:
2512 	case DDI_CTLOPS_DVMAPAGESIZE:
2513 	default:
2514 		return (ddi_ctlops(self, child, op, arg, result));
2515 	}
2516 	/* NOTREACHED */
2517 }
2518 
2519 /*
2520  * Private wrapper for scsi_pkt's allocated via scsi_hba_pkt_alloc()
2521  */
2522 struct scsi_pkt_wrapper {
2523 	struct scsi_pkt		scsi_pkt;
2524 	int			pkt_wrapper_magic;
2525 	int			pkt_wrapper_len;
2526 };
2527 
2528 #if !defined(lint)
2529 _NOTE(SCHEME_PROTECTS_DATA("unique per thread", scsi_pkt_wrapper))
2530 _NOTE(SCHEME_PROTECTS_DATA("Unshared Data", dev_ops))
2531 #endif
2532 
2533 /*
2534  * Called by an HBA to allocate a scsi_pkt
2535  */
2536 /*ARGSUSED*/
2537 struct scsi_pkt *
2538 scsi_hba_pkt_alloc(
2539 	dev_info_t		*self,
2540 	struct scsi_address	*ap,
2541 	int			cmdlen,
2542 	int			statuslen,
2543 	int			tgtlen,
2544 	int			hbalen,
2545 	int			(*callback)(caddr_t arg),
2546 	caddr_t			arg)
2547 {
2548 	struct scsi_pkt		*pkt;
2549 	struct scsi_pkt_wrapper	*hba_pkt;
2550 	caddr_t			p;
2551 	int			acmdlen, astatuslen, atgtlen, ahbalen;
2552 	int			pktlen;
2553 
2554 	/* Sanity check */
2555 	if (callback != SLEEP_FUNC && callback != NULL_FUNC)
2556 		SCSI_HBA_LOG((_LOG(WARN), self, NULL,
2557 		    "callback must be SLEEP_FUNC or NULL_FUNC"));
2558 
2559 	/*
2560 	 * Round up so everything gets allocated on long-word boundaries
2561 	 */
2562 	acmdlen = ROUNDUP(cmdlen);
2563 	astatuslen = ROUNDUP(statuslen);
2564 	atgtlen = ROUNDUP(tgtlen);
2565 	ahbalen = ROUNDUP(hbalen);
2566 	pktlen = sizeof (struct scsi_pkt_wrapper) +
2567 	    acmdlen + astatuslen + atgtlen + ahbalen;
2568 
2569 	hba_pkt = kmem_zalloc(pktlen,
2570 	    (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP);
2571 	if (hba_pkt == NULL) {
2572 		ASSERT(callback == NULL_FUNC);
2573 		return (NULL);
2574 	}
2575 
2576 	/*
2577 	 * Set up our private info on this pkt
2578 	 */
2579 	hba_pkt->pkt_wrapper_len = pktlen;
2580 	hba_pkt->pkt_wrapper_magic = PKT_WRAPPER_MAGIC;	/* alloced correctly */
2581 	pkt = &hba_pkt->scsi_pkt;
2582 
2583 	/*
2584 	 * Set up pointers to private data areas, cdb, and status.
2585 	 */
2586 	p = (caddr_t)(hba_pkt + 1);
2587 	if (hbalen > 0) {
2588 		pkt->pkt_ha_private = (opaque_t)p;
2589 		p += ahbalen;
2590 	}
2591 	if (tgtlen > 0) {
2592 		pkt->pkt_private = (opaque_t)p;
2593 		p += atgtlen;
2594 	}
2595 	if (statuslen > 0) {
2596 		pkt->pkt_scbp = (uchar_t *)p;
2597 		p += astatuslen;
2598 	}
2599 	if (cmdlen > 0) {
2600 		pkt->pkt_cdbp = (uchar_t *)p;
2601 	}
2602 
2603 	/*
2604 	 * Initialize the pkt's scsi_address
2605 	 */
2606 	pkt->pkt_address = *ap;
2607 
2608 	/*
2609 	 * NB: It may not be safe for drivers, esp target drivers, to depend
2610 	 * on the following fields being set until all the scsi_pkt
2611 	 * allocation violations discussed in scsi_pkt.h are all resolved.
2612 	 */
2613 	pkt->pkt_cdblen = cmdlen;
2614 	pkt->pkt_tgtlen = tgtlen;
2615 	pkt->pkt_scblen = statuslen;
2616 
2617 	return (pkt);
2618 }
2619 
2620 /*
2621  * Called by an HBA to free a scsi_pkt
2622  */
2623 /*ARGSUSED*/
2624 void
2625 scsi_hba_pkt_free(
2626 	struct scsi_address	*ap,
2627 	struct scsi_pkt		*pkt)
2628 {
2629 	kmem_free(pkt, ((struct scsi_pkt_wrapper *)pkt)->pkt_wrapper_len);
2630 }
2631 
2632 /*
2633  * Return 1 if the scsi_pkt used a proper allocator.
2634  *
2635  * The DDI does not allow a driver to allocate it's own scsi_pkt(9S), a
2636  * driver should not have *any* compiled in dependencies on "sizeof (struct
2637  * scsi_pkt)". While this has been the case for many years, a number of
2638  * drivers have still not been fixed. This function can be used to detect
2639  * improperly allocated scsi_pkt structures, and produce messages identifying
2640  * drivers that need to be fixed.
2641  *
2642  * While drivers in violation are being fixed, this function can also
2643  * be used by the framework to detect packets that violated allocation
2644  * rules.
2645  *
2646  * NB: It is possible, but very unlikely, for this code to return a false
2647  * positive (finding correct magic, but for wrong reasons).  Careful
2648  * consideration is needed for callers using this interface to condition
2649  * access to newer scsi_pkt fields (those after pkt_reason).
2650  *
2651  * NB: As an aid to minimizing the amount of work involved in 'fixing' legacy
2652  * drivers that violate scsi_*(9S) allocation rules, private
2653  * scsi_pkt_size()/scsi_size_clean() functions are available (see their
2654  * implementation for details).
2655  *
2656  * *** Non-legacy use of scsi_pkt_size() is discouraged. ***
2657  *
2658  * NB: When supporting broken HBA drivers is not longer a concern, this
2659  * code should be removed.
2660  */
2661 int
2662 scsi_pkt_allocated_correctly(struct scsi_pkt *pkt)
2663 {
2664 	struct scsi_pkt_wrapper	*hba_pkt = (struct scsi_pkt_wrapper *)pkt;
2665 	int	magic;
2666 	major_t	major;
2667 #ifdef	DEBUG
2668 	int	*pspwm, *pspcwm;
2669 
2670 	/*
2671 	 * We are getting scsi packets from two 'correct' wrapper schemes,
2672 	 * make sure we are looking at the same place in both to detect
2673 	 * proper allocation.
2674 	 */
2675 	pspwm = &((struct scsi_pkt_wrapper *)0)->pkt_wrapper_magic;
2676 	pspcwm = &((struct scsi_pkt_cache_wrapper *)0)->pcw_magic;
2677 	ASSERT(pspwm == pspcwm);
2678 #endif	/* DEBUG */
2679 
2680 
2681 	/*
2682 	 * Check to see if driver is scsi_size_clean(), assume it
2683 	 * is using the scsi_pkt_size() interface everywhere it needs to
2684 	 * if the driver indicates it is scsi_size_clean().
2685 	 */
2686 	major = ddi_driver_major(P_TO_TRAN(pkt)->tran_hba_dip);
2687 	if (devnamesp[major].dn_flags & DN_SCSI_SIZE_CLEAN)
2688 		return (1);		/* ok */
2689 
2690 	/*
2691 	 * Special case crossing a page boundary. If the scsi_pkt was not
2692 	 * allocated correctly, then across a page boundary we have a
2693 	 * fault hazard.
2694 	 */
2695 	if ((((uintptr_t)(&hba_pkt->scsi_pkt)) & MMU_PAGEMASK) ==
2696 	    (((uintptr_t)(&hba_pkt->pkt_wrapper_magic)) & MMU_PAGEMASK)) {
2697 		/* fastpath, no cross-page hazard */
2698 		magic = hba_pkt->pkt_wrapper_magic;
2699 	} else {
2700 		/* add protection for cross-page hazard */
2701 		if (ddi_peek32((dev_info_t *)NULL,
2702 		    &hba_pkt->pkt_wrapper_magic, &magic) == DDI_FAILURE) {
2703 			return (0);	/* violation */
2704 		}
2705 	}
2706 
2707 	/* properly allocated packet always has correct magic */
2708 	return ((magic == PKT_WRAPPER_MAGIC) ? 1 : 0);
2709 }
2710 
2711 /*
2712  * Private interfaces to simplify conversion of legacy drivers so they don't
2713  * depend on scsi_*(9S) size. Instead of using these private interface, HBA
2714  * drivers should use DDI sanctioned allocation methods:
2715  *
2716  *	scsi_pkt	Use scsi_hba_pkt_alloc(9F), or implement
2717  *			tran_setup_pkt(9E).
2718  *
2719  *	scsi_device	You are doing something strange/special, a scsi_device
2720  *			structure should only be allocated by scsi_hba.c
2721  *			initchild code or scsi_vhci.c code.
2722  *
2723  *	scsi_hba_tran	Use scsi_hba_tran_alloc(9F).
2724  */
2725 size_t
2726 scsi_pkt_size()
2727 {
2728 	return (sizeof (struct scsi_pkt));
2729 }
2730 
2731 size_t
2732 scsi_hba_tran_size()
2733 {
2734 	return (sizeof (scsi_hba_tran_t));
2735 }
2736 
2737 size_t
2738 scsi_device_size()
2739 {
2740 	return (sizeof (struct scsi_device));
2741 }
2742 
2743 /*
2744  * Legacy compliance to scsi_pkt(9S) allocation rules through use of
2745  * scsi_pkt_size() is detected by the 'scsi-size-clean' driver.conf property
2746  * or an HBA driver calling to scsi_size_clean() from attach(9E).  A driver
2747  * developer should only indicate that a legacy driver is clean after using
2748  * SCSI_SIZE_CLEAN_VERIFY to ensure compliance (see scsi_pkt.h).
2749  */
2750 void
2751 scsi_size_clean(dev_info_t *self)
2752 {
2753 	major_t		major;
2754 	struct devnames	*dnp;
2755 
2756 	ASSERT(self);
2757 	major = ddi_driver_major(self);
2758 	ASSERT(major < devcnt);
2759 	if (major >= devcnt) {
2760 		SCSI_HBA_LOG((_LOG(WARN), self, NULL,
2761 		    "scsi_pkt_size: bogus major: %d", major));
2762 		return;
2763 	}
2764 
2765 	/* Set DN_SCSI_SIZE_CLEAN flag in dn_flags. */
2766 	dnp = &devnamesp[major];
2767 	if ((dnp->dn_flags & DN_SCSI_SIZE_CLEAN) == 0) {
2768 		LOCK_DEV_OPS(&dnp->dn_lock);
2769 		dnp->dn_flags |= DN_SCSI_SIZE_CLEAN;
2770 		UNLOCK_DEV_OPS(&dnp->dn_lock);
2771 	}
2772 }
2773 
2774 
2775 /*
2776  * Called by an HBA to map strings to capability indices
2777  */
2778 int
2779 scsi_hba_lookup_capstr(
2780 	char			*capstr)
2781 {
2782 	/*
2783 	 * Capability strings: only add entries to mask the legacy
2784 	 * '_' vs. '-' misery.  All new capabilities should use '-',
2785 	 * and be captured be added to SCSI_CAP_ASCII.
2786 	 */
2787 	static struct cap_strings {
2788 		char	*cap_string;
2789 		int	cap_index;
2790 	} cap_strings[] = {
2791 		{ "dma_max",		SCSI_CAP_DMA_MAX		},
2792 		{ "msg_out",		SCSI_CAP_MSG_OUT		},
2793 		{ "wide_xfer",		SCSI_CAP_WIDE_XFER		},
2794 		{ NULL,			0				}
2795 	};
2796 	static char		*cap_ascii[] = SCSI_CAP_ASCII;
2797 	char			**cap;
2798 	int			i;
2799 	struct cap_strings	*cp;
2800 
2801 	for (cap = cap_ascii, i = 0; *cap != NULL; cap++, i++)
2802 		if (strcmp(*cap, capstr) == 0)
2803 			return (i);
2804 
2805 	for (cp = cap_strings; cp->cap_string != NULL; cp++)
2806 		if (strcmp(cp->cap_string, capstr) == 0)
2807 			return (cp->cap_index);
2808 
2809 	return (-1);
2810 }
2811 
2812 /*
2813  * Called by an HBA to determine if the system is in 'panic' state.
2814  */
2815 int
2816 scsi_hba_in_panic()
2817 {
2818 	return (panicstr != NULL);
2819 }
2820 
2821 /*
2822  * If a SCSI target driver attempts to mmap memory,
2823  * the buck stops here.
2824  */
2825 /*ARGSUSED*/
2826 static int
2827 scsi_hba_map_fault(
2828 	dev_info_t		*self,
2829 	dev_info_t		*child,
2830 	struct hat		*hat,
2831 	struct seg		*seg,
2832 	caddr_t			addr,
2833 	struct devpage		*dp,
2834 	pfn_t			pfn,
2835 	uint_t			prot,
2836 	uint_t			lock)
2837 {
2838 	return (DDI_FAILURE);
2839 }
2840 
2841 static int
2842 scsi_hba_get_eventcookie(
2843 	dev_info_t		*self,
2844 	dev_info_t		*child,
2845 	char			*name,
2846 	ddi_eventcookie_t	*eventp)
2847 {
2848 	scsi_hba_tran_t		*tran;
2849 
2850 	tran = ddi_get_driver_private(self);
2851 	if (tran->tran_get_eventcookie &&
2852 	    ((*tran->tran_get_eventcookie)(self,
2853 	    child, name, eventp) == DDI_SUCCESS)) {
2854 		return (DDI_SUCCESS);
2855 	}
2856 
2857 	return (ndi_busop_get_eventcookie(self, child, name, eventp));
2858 }
2859 
2860 static int
2861 scsi_hba_add_eventcall(
2862 	dev_info_t		*self,
2863 	dev_info_t		*child,
2864 	ddi_eventcookie_t	event,
2865 	void			(*callback)(
2866 					dev_info_t *self,
2867 					ddi_eventcookie_t event,
2868 					void *arg,
2869 					void *bus_impldata),
2870 	void			*arg,
2871 	ddi_callback_id_t	*cb_id)
2872 {
2873 	scsi_hba_tran_t		*tran;
2874 
2875 	tran = ddi_get_driver_private(self);
2876 	if (tran->tran_add_eventcall &&
2877 	    ((*tran->tran_add_eventcall)(self, child,
2878 	    event, callback, arg, cb_id) == DDI_SUCCESS)) {
2879 		return (DDI_SUCCESS);
2880 	}
2881 
2882 	return (DDI_FAILURE);
2883 }
2884 
2885 static int
2886 scsi_hba_remove_eventcall(dev_info_t *self, ddi_callback_id_t cb_id)
2887 {
2888 	scsi_hba_tran_t		*tran;
2889 	ASSERT(cb_id);
2890 
2891 	tran = ddi_get_driver_private(self);
2892 	if (tran->tran_remove_eventcall &&
2893 	    ((*tran->tran_remove_eventcall)(
2894 	    self, cb_id) == DDI_SUCCESS)) {
2895 		return (DDI_SUCCESS);
2896 	}
2897 
2898 	return (DDI_FAILURE);
2899 }
2900 
2901 static int
2902 scsi_hba_post_event(
2903 	dev_info_t		*self,
2904 	dev_info_t		*child,
2905 	ddi_eventcookie_t	event,
2906 	void			*bus_impldata)
2907 {
2908 	scsi_hba_tran_t		*tran;
2909 
2910 	tran = ddi_get_driver_private(self);
2911 	if (tran->tran_post_event &&
2912 	    ((*tran->tran_post_event)(self,
2913 	    child, event, bus_impldata) == DDI_SUCCESS)) {
2914 		return (DDI_SUCCESS);
2915 	}
2916 
2917 	return (DDI_FAILURE);
2918 }
2919 
2920 /*
2921  * Default getinfo(9e) for scsi_hba
2922  */
2923 /* ARGSUSED */
2924 static int
2925 scsi_hba_info(dev_info_t *self, ddi_info_cmd_t infocmd, void *arg,
2926     void **result)
2927 {
2928 	int error = DDI_SUCCESS;
2929 
2930 	switch (infocmd) {
2931 	case DDI_INFO_DEVT2INSTANCE:
2932 		*result = (void *)(intptr_t)(MINOR2INST(getminor((dev_t)arg)));
2933 		break;
2934 	default:
2935 		error = DDI_FAILURE;
2936 	}
2937 	return (error);
2938 }
2939 
2940 /*
2941  * Default open and close routine for scsi_hba
2942  */
2943 /* ARGSUSED */
2944 int
2945 scsi_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp)
2946 {
2947 	dev_info_t	*self;
2948 	scsi_hba_tran_t	*tran;
2949 	int		rv = 0;
2950 
2951 	if (otyp != OTYP_CHR)
2952 		return (EINVAL);
2953 
2954 	if ((self = e_ddi_hold_devi_by_dev(*devp, 0)) == NULL)
2955 		return (ENXIO);
2956 
2957 	tran = ddi_get_driver_private(self);
2958 	if (tran == NULL) {
2959 		ddi_release_devi(self);
2960 		return (ENXIO);
2961 	}
2962 
2963 	/*
2964 	 * tran_open_flag bit field:
2965 	 *	0:	closed
2966 	 *	1:	shared open by minor at bit position
2967 	 *	1 at 31st bit:	exclusive open
2968 	 */
2969 	mutex_enter(&(tran->tran_open_lock));
2970 	if (flags & FEXCL) {
2971 		if (tran->tran_open_flag != 0) {
2972 			rv = EBUSY;		/* already open */
2973 		} else {
2974 			tran->tran_open_flag = TRAN_OPEN_EXCL;
2975 		}
2976 	} else {
2977 		if (tran->tran_open_flag == TRAN_OPEN_EXCL) {
2978 			rv = EBUSY;		/* already excl. open */
2979 		} else {
2980 			int minor = getminor(*devp) & TRAN_MINOR_MASK;
2981 			tran->tran_open_flag |= (1 << minor);
2982 			/*
2983 			 * Ensure that the last framework reserved minor
2984 			 * is unused. Otherwise, the exclusive open
2985 			 * mechanism may break.
2986 			 */
2987 			ASSERT(minor != 31);
2988 		}
2989 	}
2990 	mutex_exit(&(tran->tran_open_lock));
2991 
2992 	ddi_release_devi(self);
2993 	return (rv);
2994 }
2995 
2996 /* ARGSUSED */
2997 int
2998 scsi_hba_close(dev_t dev, int flag, int otyp, cred_t *credp)
2999 {
3000 	dev_info_t	*self;
3001 	scsi_hba_tran_t	*tran;
3002 
3003 	if (otyp != OTYP_CHR)
3004 		return (EINVAL);
3005 
3006 	if ((self = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
3007 		return (ENXIO);
3008 
3009 	tran = ddi_get_driver_private(self);
3010 	if (tran == NULL) {
3011 		ddi_release_devi(self);
3012 		return (ENXIO);
3013 	}
3014 
3015 	mutex_enter(&(tran->tran_open_lock));
3016 	if (tran->tran_open_flag == TRAN_OPEN_EXCL) {
3017 		tran->tran_open_flag = 0;
3018 	} else {
3019 		int minor = getminor(dev) & TRAN_MINOR_MASK;
3020 		tran->tran_open_flag &= ~(1 << minor);
3021 	}
3022 	mutex_exit(&(tran->tran_open_lock));
3023 
3024 	ddi_release_devi(self);
3025 	return (0);
3026 }
3027 
3028 /*
3029  * standard ioctl commands for SCSI hotplugging
3030  */
3031 /* ARGSUSED */
3032 int
3033 scsi_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
3034     int *rvalp)
3035 {
3036 	dev_info_t		*self;
3037 	struct devctl_iocdata	*dcp = NULL;
3038 	dev_info_t		*child = NULL;
3039 	mdi_pathinfo_t		*path = NULL;
3040 	struct scsi_device	*sd;
3041 	scsi_hba_tran_t		*tran;
3042 	uint_t			bus_state;
3043 	int			rv = 0;
3044 	int			circ;
3045 	char			*name;
3046 	char			*addr;
3047 
3048 	self = e_ddi_hold_devi_by_dev(dev, 0);
3049 	if (self == NULL) {
3050 		rv = ENXIO;
3051 		goto out;
3052 	}
3053 
3054 	tran = ddi_get_driver_private(self);
3055 	if (tran == NULL) {
3056 		rv = ENXIO;
3057 		goto out;
3058 	}
3059 
3060 	/* Ioctls for which the generic implementation suffices. */
3061 	switch (cmd) {
3062 	case DEVCTL_BUS_GETSTATE:
3063 		rv = ndi_devctl_ioctl(self, cmd, arg, mode, 0);
3064 		goto out;
3065 	}
3066 
3067 	/* read devctl ioctl data */
3068 	if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS) {
3069 		rv = EFAULT;
3070 		goto out;
3071 	}
3072 
3073 	/* Ioctls that require child identification */
3074 	switch (cmd) {
3075 	case DEVCTL_DEVICE_GETSTATE:
3076 	case DEVCTL_DEVICE_ONLINE:
3077 	case DEVCTL_DEVICE_OFFLINE:
3078 	case DEVCTL_DEVICE_REMOVE:
3079 	case DEVCTL_DEVICE_RESET:
3080 		name = ndi_dc_getname(dcp);
3081 		addr = ndi_dc_getaddr(dcp);
3082 		if ((name == NULL) || (addr == NULL)) {
3083 			rv = EINVAL;
3084 			goto out;
3085 		}
3086 
3087 		/*
3088 		 * Find child with name@addr - might find a devinfo
3089 		 * child (child), a pathinfo child (path), or nothing.
3090 		 */
3091 		scsi_hba_devi_enter(self, &circ);
3092 
3093 		(void) scsi_hba_find_child(self, name, addr,
3094 		    1, &child, &path, NULL);
3095 		if (path) {
3096 			/* Found a pathinfo */
3097 			ASSERT(path && (child == NULL));
3098 			mdi_hold_path(path);
3099 			scsi_hba_devi_exit_phci(self, circ);
3100 			sd = NULL;
3101 		} else if (child) {
3102 			/* Found a devinfo */
3103 			ASSERT(child && (path == NULL));
3104 
3105 			/* verify scsi_device of child */
3106 			if (ndi_flavor_get(child) == SCSA_FLAVOR_SCSI_DEVICE)
3107 				sd = ddi_get_driver_private(child);
3108 			else
3109 				sd = NULL;
3110 		} else {
3111 			ASSERT((path == NULL) && (child == NULL));
3112 			scsi_hba_devi_exit(self, circ);
3113 			rv = ENXIO;			/* found nothing */
3114 			goto out;
3115 		}
3116 		break;
3117 
3118 	case DEVCTL_BUS_RESETALL:	/* ioctl that operate on any child */
3119 		/*
3120 		 * Find a child's scsi_address so we can invoke tran_reset.
3121 		 *
3122 		 * Future: If no child exists, we could fake a child. This will
3123 		 * be a enhancement for the future - for now, we fall back to
3124 		 * BUS_RESET.
3125 		 */
3126 		scsi_hba_devi_enter(self, &circ);
3127 		child = ddi_get_child(self);
3128 		sd = NULL;
3129 		while (child) {
3130 			/* verify scsi_device of child */
3131 			if (ndi_flavor_get(child) == SCSA_FLAVOR_SCSI_DEVICE)
3132 				sd = ddi_get_driver_private(child);
3133 			if (sd != NULL) {
3134 				/*
3135 				 * NOTE: node has a scsi_device structure, so
3136 				 * it must be initialized.
3137 				 */
3138 				ndi_hold_devi(child);
3139 				break;
3140 			}
3141 			child = ddi_get_next_sibling(child);
3142 		}
3143 		scsi_hba_devi_exit(self, circ);
3144 		break;
3145 	}
3146 
3147 	switch (cmd) {
3148 	case DEVCTL_DEVICE_GETSTATE:
3149 		if (path) {
3150 			if (mdi_dc_return_dev_state(path, dcp) != MDI_SUCCESS)
3151 				rv = EFAULT;
3152 		} else if (child) {
3153 			if (ndi_dc_return_dev_state(child, dcp) != NDI_SUCCESS)
3154 				rv = EFAULT;
3155 		} else {
3156 			rv = ENXIO;
3157 		}
3158 		break;
3159 
3160 	case DEVCTL_DEVICE_RESET:
3161 		if (sd == NULL) {
3162 			rv = ENOTTY;
3163 			break;
3164 		}
3165 		if (tran->tran_reset == NULL) {
3166 			rv = ENOTSUP;
3167 			break;
3168 		}
3169 
3170 		/* Start with the small stick */
3171 		if (scsi_reset(&sd->sd_address, RESET_LUN) == 1)
3172 			break;		/* LUN reset worked */
3173 		if (scsi_reset(&sd->sd_address, RESET_TARGET) != 1)
3174 			rv = EIO;	/* Target reset failed */
3175 		break;
3176 
3177 	case DEVCTL_BUS_QUIESCE:
3178 		if ((ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS) &&
3179 		    (bus_state == BUS_QUIESCED))
3180 			rv = EALREADY;
3181 		else if (tran->tran_quiesce == NULL)
3182 			rv = ENOTSUP; /* man ioctl(7I) says ENOTTY */
3183 		else if (tran->tran_quiesce(self) != 0)
3184 			rv = EIO;
3185 		else if (ndi_set_bus_state(self, BUS_QUIESCED) != NDI_SUCCESS)
3186 			rv = EIO;
3187 		break;
3188 
3189 	case DEVCTL_BUS_UNQUIESCE:
3190 		if ((ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS) &&
3191 		    (bus_state == BUS_ACTIVE))
3192 			rv = EALREADY;
3193 		else if (tran->tran_unquiesce == NULL)
3194 			rv = ENOTSUP; /* man ioctl(7I) says ENOTTY */
3195 		else if (tran->tran_unquiesce(self) != 0)
3196 			rv = EIO;
3197 		else if (ndi_set_bus_state(self, BUS_ACTIVE) != NDI_SUCCESS)
3198 			rv = EIO;
3199 		break;
3200 
3201 	case DEVCTL_BUS_RESET:
3202 		if (tran->tran_bus_reset == NULL)
3203 			rv = ENOTSUP; /* man ioctl(7I) says ENOTTY */
3204 		else if (tran->tran_bus_reset(self, RESET_BUS) != 1)
3205 			rv = EIO;
3206 		break;
3207 
3208 	case DEVCTL_BUS_RESETALL:
3209 		if ((sd != NULL) &&
3210 		    (scsi_reset(&sd->sd_address, RESET_ALL) == 1)) {
3211 			break;		/* reset all worked */
3212 		}
3213 		if (tran->tran_bus_reset == NULL) {
3214 			rv = ENOTSUP; /* man ioctl(7I) says ENOTTY */
3215 			break;
3216 		}
3217 		if (tran->tran_bus_reset(self, RESET_BUS) != 1)
3218 			rv = EIO;	/* bus reset failed */
3219 		break;
3220 
3221 	case DEVCTL_BUS_CONFIGURE:
3222 		if (ndi_devi_config(self, NDI_DEVFS_CLEAN | NDI_DEVI_PERSIST |
3223 		    NDI_CONFIG_REPROBE) != NDI_SUCCESS) {
3224 			rv = EIO;
3225 		}
3226 		break;
3227 
3228 	case DEVCTL_BUS_UNCONFIGURE:
3229 		if (ndi_devi_unconfig(self,
3230 		    NDI_DEVFS_CLEAN | NDI_DEVI_REMOVE) != NDI_SUCCESS) {
3231 			rv = EBUSY;
3232 		}
3233 		break;
3234 
3235 	case DEVCTL_DEVICE_ONLINE:
3236 		ASSERT(child || path);
3237 		if (path) {
3238 			if (mdi_pi_online(path, NDI_USER_REQ) != MDI_SUCCESS)
3239 				rv = EIO;
3240 		} else {
3241 			if (ndi_devi_online(child, 0) != NDI_SUCCESS)
3242 				rv = EIO;
3243 		}
3244 		break;
3245 
3246 	case DEVCTL_DEVICE_OFFLINE:
3247 		ASSERT(child || path);
3248 		if (sd != NULL)
3249 			(void) scsi_clear_task_set(&sd->sd_address);
3250 		if (path) {
3251 			if (mdi_pi_offline(path, NDI_USER_REQ) != MDI_SUCCESS)
3252 				rv = EIO;
3253 		} else {
3254 			if (ndi_devi_offline(child,
3255 			    NDI_DEVFS_CLEAN) != NDI_SUCCESS)
3256 				rv = EIO;
3257 		}
3258 		break;
3259 
3260 	case DEVCTL_DEVICE_REMOVE:
3261 		ASSERT(child || path);
3262 		if (sd != NULL)
3263 			(void) scsi_clear_task_set(&sd->sd_address);
3264 		if (path) {
3265 			/* NOTE: don't pass NDI_DEVI_REMOVE to mdi_pi_offline */
3266 			if (mdi_pi_offline(path, NDI_USER_REQ) == MDI_SUCCESS) {
3267 				scsi_hba_devi_enter_phci(self, &circ);
3268 				mdi_rele_path(path);
3269 
3270 				/* ... here is the DEVICE_REMOVE part. */
3271 				(void) mdi_pi_free(path, 0);
3272 				path = NULL;
3273 			} else {
3274 				rv = EIO;
3275 			}
3276 		} else {
3277 			if (ndi_devi_offline(child,
3278 			    NDI_DEVFS_CLEAN | NDI_DEVI_REMOVE) != NDI_SUCCESS)
3279 				rv = EIO;
3280 		}
3281 		break;
3282 
3283 	default:
3284 		ASSERT(dcp != NULL);
3285 		rv = ENOTTY;
3286 		break;
3287 	}
3288 
3289 	/* all done -- clean up and return */
3290 out:
3291 	/* release hold on what we found */
3292 	if (path) {
3293 		scsi_hba_devi_enter_phci(self, &circ);
3294 		mdi_rele_path(path);
3295 	}
3296 	if (path || child)
3297 		scsi_hba_devi_exit(self, circ);
3298 
3299 	if (dcp)
3300 		ndi_dc_freehdl(dcp);
3301 
3302 	if (self)
3303 		ddi_release_devi(self);
3304 
3305 	*rvalp = rv;
3306 
3307 	return (rv);
3308 }
3309 
3310 /*ARGSUSED*/
3311 static int
3312 scsi_hba_fm_init_child(dev_info_t *self, dev_info_t *child, int cap,
3313     ddi_iblock_cookie_t *ibc)
3314 {
3315 	scsi_hba_tran_t	*tran = ddi_get_driver_private(self);
3316 
3317 	return (tran ? tran->tran_fm_capable : scsi_fm_capable);
3318 }
3319 
3320 static int
3321 scsi_hba_bus_power(dev_info_t *self, void *impl_arg, pm_bus_power_op_t op,
3322     void *arg, void *result)
3323 {
3324 	scsi_hba_tran_t	*tran;
3325 
3326 	tran = ddi_get_driver_private(self);
3327 	if (tran && tran->tran_bus_power) {
3328 		return (tran->tran_bus_power(self, impl_arg,
3329 		    op, arg, result));
3330 	}
3331 
3332 	return (pm_busop_bus_power(self, impl_arg, op, arg, result));
3333 }
3334 
3335 /*
3336  * Return the lun64 value from a address string: "addr,lun[,sfunc]". Either
3337  * the lun is after the first ',' or the entire address string is the lun.
3338  * Return SCSI_LUN64_ILLEGAL if the format is incorrect. A lun64 is at most
3339  * 16 hex digits long.
3340  *
3341  * If the address string specified has incorrect syntax (busconfig one of
3342  * bogus /devices path) then scsi_addr_to_lun64 can return SCSI_LUN64_ILLEGAL.
3343  */
3344 static scsi_lun64_t
3345 scsi_addr_to_lun64(char *addr)
3346 {
3347 	scsi_lun64_t	lun64;
3348 	char		*s;
3349 	int		i;
3350 
3351 	if (addr) {
3352 		s = strchr(addr, ',');			/* "addr,lun" */
3353 		if (s)
3354 			s++;				/* skip ',', at lun */
3355 		else
3356 			s = addr;			/* "lun" */
3357 
3358 		for (lun64 = 0, i = 0; *s && (i < 16); s++, i++) {
3359 			if (*s >= '0' && *s <= '9')
3360 				lun64 = (lun64 << 4) + (*s - '0');
3361 			else if (*s >= 'A' && *s <= 'F')
3362 				lun64 = (lun64 << 4) + 10 + (*s - 'A');
3363 			else if (*s >= 'a' && *s <= 'f')
3364 				lun64 = (lun64 << 4) + 10 + (*s - 'a');
3365 			else
3366 				break;
3367 		}
3368 		if (*s && (*s != ','))		/* [,sfunc] is OK */
3369 			lun64 = SCSI_LUN64_ILLEGAL;
3370 	} else
3371 		lun64 = SCSI_LUN64_ILLEGAL;
3372 
3373 	if (lun64 == SCSI_LUN64_ILLEGAL)
3374 		SCSI_HBA_LOG((_LOG(2), NULL, NULL,
3375 		    "addr_to_lun64 %s lun %" PRIlun64,
3376 		    addr ? addr : "NULL", lun64));
3377 	return (lun64);
3378 }
3379 
3380 /*
3381  * Return the sfunc value from a address string: "addr,lun[,sfunc]". Either the
3382  * sfunc is after the second ',' or the entire address string is the sfunc.
3383  * Return -1 if there is only one ',' in the address string or the string is
3384  * invalid. An sfunc is at most two hex digits long.
3385  */
3386 static int
3387 scsi_addr_to_sfunc(char *addr)
3388 {
3389 	int		sfunc;
3390 	char		*s;
3391 	int		i;
3392 
3393 	if (addr) {
3394 		s = strchr(addr, ',');			/* "addr,lun" */
3395 		if (s) {
3396 			s++;				/* skip ',', at lun */
3397 			s = strchr(s, ',');		/* "lun,sfunc]" */
3398 			if (s == NULL)
3399 				return (-1);		/* no ",sfunc" */
3400 			s++;				/* skip ',', at sfunc */
3401 		} else
3402 			s = addr;			/* "sfunc" */
3403 
3404 		for (sfunc = 0, i = 0; *s && (i < 2); s++, i++) {
3405 			if (*s >= '0' && *s <= '9')
3406 				sfunc = (sfunc << 4) + (*s - '0');
3407 			else if (*s >= 'A' && *s <= 'F')
3408 				sfunc = (sfunc << 4) + 10 + (*s - 'A');
3409 			else if (*s >= 'a' && *s <= 'f')
3410 				sfunc = (sfunc << 4) + 10 + (*s - 'a');
3411 			else
3412 				break;
3413 		}
3414 		if (*s)
3415 			sfunc = -1;			/* illegal */
3416 	} else
3417 		sfunc = -1;
3418 	return (sfunc);
3419 }
3420 
3421 /*
3422  * Convert scsi ascii string data to NULL terminated (semi) legal IEEE 1275
3423  * "compatible" (name) property form.
3424  *
3425  * For ASCII INQUIRY data, a one-way conversion algorithm is needed to take
3426  * SCSI_ASCII (20h - 7Eh) to a 1275-like compatible form. The 1275 spec allows
3427  * letters, digits, one ",", and ". _ + -", all limited by a maximum 31
3428  * character length. Since ", ." are used as separators in the compatible
3429  * string itself, they are converted to "_". All SCSI_ASCII characters that
3430  * are illegal in 1275, as well as any illegal SCSI_ASCII characters
3431  * encountered, are converted to "_". To reduce length, trailing blanks are
3432  * trimmed from SCSI_ASCII fields prior to conversion.
3433  *
3434  * Example: SCSI_ASCII "ST32550W SUN2.1G" -> "ST32550W_SUN2_1G"
3435  *
3436  * NOTE: the 1275 string form is always less than or equal to the scsi form.
3437  */
3438 static char *
3439 string_scsi_to_1275(char *s_1275, char *s_scsi, int len)
3440 {
3441 	(void) strncpy(s_1275, s_scsi, len);
3442 	s_1275[len--] = '\0';
3443 
3444 	while (len >= 0) {
3445 		if (s_1275[len] == ' ')
3446 			s_1275[len--] = '\0';	/* trim trailing " " */
3447 		else
3448 			break;
3449 	}
3450 
3451 	while (len >= 0) {
3452 		if (((s_1275[len] >= 'a') && (s_1275[len] <= 'z')) ||
3453 		    ((s_1275[len] >= 'A') && (s_1275[len] <= 'Z')) ||
3454 		    ((s_1275[len] >= '0') && (s_1275[len] <= '9')) ||
3455 		    (s_1275[len] == '_') ||
3456 		    (s_1275[len] == '+') ||
3457 		    (s_1275[len] == '-'))
3458 			len--;			/* legal 1275  */
3459 		else
3460 			s_1275[len--] = '_';	/* illegal SCSI_ASCII | 1275 */
3461 	}
3462 
3463 	return (s_1275);
3464 }
3465 
3466 /*
3467  * Given the inquiry data, binding_set, and dtype_node for a scsi device,
3468  * return the nodename and compatible property for the device. The "compatible"
3469  * concept comes from IEEE-1275. The compatible information is returned is in
3470  * the correct form for direct use defining the "compatible" string array
3471  * property. Internally, "compatible" is also used to determine the nodename
3472  * to return.
3473  *
3474  * This function is provided as a separate entry point for use by drivers that
3475  * currently issue their own non-SCSA inquiry command and perform their own
3476  * node creation based their own private compiled in tables. Converting these
3477  * drivers to use this interface provides a quick easy way of obtaining
3478  * consistency as well as the flexibility associated with the 1275 techniques.
3479  *
3480  * The dtype_node is passed as a separate argument (instead of having the
3481  * implementation use inq_dtype). It indicates that information about
3482  * a secondary function embedded service should be produced.
3483  *
3484  * Callers must always use scsi_hba_nodename_compatible_free, even if
3485  * *nodenamep is null, to free the nodename and compatible information
3486  * when done.
3487  *
3488  * If a nodename can't be determined then **compatiblep will point to a
3489  * diagnostic string containing all the compatible forms.
3490  *
3491  * NOTE: some compatible strings may violate the 31 character restriction
3492  * imposed by IEEE-1275. This is not a problem because Solaris does not care
3493  * about this 31 character limit.
3494  *
3495  * Each compatible form belongs to a form-group.  The form-groups currently
3496  * defined are generic ("scsiclass"), binding-set ("scsa.b"), and failover
3497  * ("scsa.f").
3498  *
3499  * The following compatible forms, in high to low precedence
3500  * order, are defined for SCSI target device nodes.
3501  *
3502  *  scsiclass,DDEEFFF.vVVVVVVVV.pPPPPPPPPPPPPPPPP.rRRRR	(1 *1&2)
3503  *  scsiclass,DDEE.vVVVVVVVV.pPPPPPPPPPPPPPPPP.rRRRR	(2 *1)
3504  *  scsiclass,DDFFF.vVVVVVVVV.pPPPPPPPPPPPPPPPP.rRRRR	(3 *2)
3505  *  scsiclass,DD.vVVVVVVVV.pPPPPPPPPPPPPPPPP.rRRRR	(4)
3506  *  scsiclass,DDEEFFF.vVVVVVVVV.pPPPPPPPPPPPPPPPP	(5 *1&2)
3507  *  scsiclass,DDEE.vVVVVVVVV.pPPPPPPPPPPPPPPPP		(6 *1)
3508  *  scsiclass,DDFFF.vVVVVVVVV.pPPPPPPPPPPPPPPPP		(7 *2)
3509  *  scsiclass,DD.vVVVVVVVV.pPPPPPPPPPPPPPPPP		(8)
3510  *  scsa,DD.bBBBBBBBB					(8.5 *3)
3511  *  scsiclass,DDEEFFF					(9 *1&2)
3512  *  scsiclass,DDEE					(10 *1)
3513  *  scsiclass,DDFFF					(11 *2)
3514  *  scsiclass,DD					(12)
3515  *  scsa.fFFF						(12.5 *4)
3516  *  scsiclass						(13)
3517  *
3518  *	  *1 only produced on a secondary function node
3519  *	  *2 only produced when generic form-group flags exist.
3520  *	  *3 only produced when binding-set form-group legacy support is needed
3521  *	  *4 only produced when failover form-group flags exist.
3522  *
3523  *	where:
3524  *
3525  *	v			is the letter 'v'. Denotest the
3526  *				beginning of VVVVVVVV.
3527  *
3528  *	VVVVVVVV		Translated scsi_vendor.
3529  *
3530  *	p			is the letter 'p'. Denotes the
3531  *				beginning of PPPPPPPPPPPPPPPP.
3532  *
3533  *	PPPPPPPPPPPPPPPP	Translated scsi_product.
3534  *
3535  *	r			is the letter 'r'. Denotes the
3536  *				beginning of RRRR.
3537  *
3538  *	RRRR			Translated scsi_revision.
3539  *
3540  *	DD			is a two digit ASCII hexadecimal
3541  *				number. The value of the two digits is
3542  *				based one the SCSI "Peripheral device
3543  *				type" command set associated with the
3544  *				node. On a primary node this is the
3545  *				scsi_dtype of the primary command set,
3546  *				on a secondary node this is the
3547  *				scsi_dtype associated with the secondary
3548  *				function embedded command set.
3549  *
3550  *	EE			Same encoding used for DD. This form is
3551  *				only generated on secondary function
3552  *				nodes. The DD secondary function is embedded
3553  *				in an EE device.
3554  *
3555  *	FFF			Concatenation, in alphabetical order,
3556  *				of the flag characters within a form-group.
3557  *				For a given form-group, the following
3558  *				flags are defined.
3559  *
3560  *				scsiclass: (generic form-group):
3561  *				  R	Removable_Media: Used when
3562  *					inq_rmb is set.
3563  *				  S	SAF-TE device: Used when
3564  *					inquiry information indicates
3565  *					SAF-TE devices.
3566  *
3567  *				scsa.f:	(failover form-group):
3568  *				  E	Explicit Target_Port_Group: Used
3569  *					when inq_tpgse is set and 'G' is
3570  *					alse present.
3571  *				  G	GUID: Used when a GUID can be
3572  *					generated for the device.
3573  *				  I	Implicit Target_Port_Group: Used
3574  *					when inq_tpgs is set and 'G' is
3575  *					also present.
3576  *
3577  *				Forms using FFF are only be generated
3578  *				if there are applicable flag
3579  *				characters.
3580  *
3581  *	b			is the letter 'b'. Denotes the
3582  *				beginning of BBBBBBBB.
3583  *
3584  *	BBBBBBBB		Binding-set. Operating System Specific:
3585  *				scsi-binding-set property of HBA.
3586  */
3587 #define	NCOMPAT		(1 + (13 + 2) + 1)
3588 #define	COMPAT_LONGEST	(strlen( \
3589 	"scsiclass,DDEEFFF.vVVVVVVVV.pPPPPPPPPPPPPPPPP.rRRRR" + 1))
3590 
3591 /*
3592  * Private version with extra device 'identity' arguments to allow code
3593  * to determine GUID FFF support.
3594  */
3595 static void
3596 scsi_hba_ident_nodename_compatible_get(struct scsi_inquiry *inq,
3597     uchar_t *inq80, size_t inq80len, uchar_t *inq83, size_t inq83len,
3598     char *binding_set, int dtype_node, char *compat0,
3599     char **nodenamep, char **drivernamep,
3600     char ***compatiblep, int *ncompatiblep)
3601 {
3602 	char		vid[sizeof (inq->inq_vid) + 1 ];
3603 	char		pid[sizeof (inq->inq_pid) + 1];
3604 	char		rev[sizeof (inq->inq_revision) + 1];
3605 	char		gf[sizeof ("R\0")];
3606 	char		ff[sizeof ("EGI\0")];
3607 	int		dtype_device;
3608 	int		ncompat;		/* number of compatible */
3609 	char		**compatp;		/* compatible ptrs */
3610 	int		i;
3611 	char		*nname;			/* nodename */
3612 	char		*dname;			/* driver name */
3613 	char		**csp;
3614 	char		*p;
3615 	int		tlen;
3616 	int		len;
3617 	major_t		major;
3618 	ddi_devid_t	devid;
3619 	char		*guid;
3620 	uchar_t		*iqd = (uchar_t *)inq;
3621 
3622 	/*
3623 	 * Nodename_aliases: This table was originally designed to be
3624 	 * implemented via a new nodename_aliases file - a peer to the
3625 	 * driver_aliases that selects a nodename based on compatible
3626 	 * forms in much the same say driver_aliases is used to select
3627 	 * driver bindings from compatible forms. Each compatible form
3628 	 * is an 'alias'. Until a more general need for a
3629 	 * nodename_aliases file exists, which may never occur, the
3630 	 * scsi mappings are described here via a compiled in table.
3631 	 *
3632 	 * This table contains nodename mappings for self-identifying
3633 	 * scsi devices enumerated by the Solaris kernel. For a given
3634 	 * device, the highest precedence "compatible" form with a
3635 	 * mapping is used to select the nodename for the device. This
3636 	 * will typically be a generic nodename, however in some legacy
3637 	 * compatibility cases a driver nodename mapping may be selected.
3638 	 *
3639 	 * Because of possible breakage associated with switching SCSI
3640 	 * target devices from driver nodenames to generic nodenames,
3641 	 * we are currently unable to support generic nodenames for all
3642 	 * SCSI devices (binding-sets). Although /devices paths are
3643 	 * defined as unstable, avoiding possible breakage is
3644 	 * important. Some of the newer SCSI transports (USB) already
3645 	 * use generic nodenames. All new SCSI transports and target
3646 	 * devices should use generic nodenames. At times this decision
3647 	 * may be architecture dependent (sparc .vs. intel) based on when
3648 	 * a transport was supported on a particular architecture.
3649 	 *
3650 	 * We provide a base set of generic nodename mappings based on
3651 	 * scsiclass dtype and higher-precedence driver nodename
3652 	 * mappings based on scsa "binding-set" to cover legacy
3653 	 * issues. The binding-set is typically associated with
3654 	 * "scsi-binding-set" property value of the HBA. The legacy
3655 	 * mappings are provided independent of whether the driver they
3656 	 * refer to is installed. This allows a correctly named node
3657 	 * be created at discovery time, and binding to occur when/if
3658 	 * an add_drv of the legacy driver occurs.
3659 	 *
3660 	 * We also have mappings for legacy SUN hardware that
3661 	 * misidentifies itself (enclosure services which identify
3662 	 * themselves as processors). All future hardware should use
3663 	 * the correct dtype.
3664 	 *
3665 	 * As SCSI HBAs are modified to use the SCSA interfaces for
3666 	 * self-identifying SCSI target devices (PSARC/2004/116) the
3667 	 * nodename_aliases table (PSARC/2004/420) should be augmented
3668 	 * with legacy mappings in order to maintain compatibility with
3669 	 * existing /devices paths, especially for devices that house
3670 	 * an OS. Failure to do this may cause upgrade problems.
3671 	 * Additions for new target devices or transports should not
3672 	 * add scsa binding-set compatible mappings.
3673 	 */
3674 	static struct nodename_aliases {
3675 		char	*na_nodename;		/* nodename */
3676 		char	*na_alias;		/* compatible form match */
3677 	} na[] = {
3678 	/* # mapping to generic nodenames based on scsi dtype */
3679 		{"disk",		"scsiclass,00"},
3680 		{"tape",		"scsiclass,01"},
3681 		{"printer",		"scsiclass,02"},
3682 		{"processor",		"scsiclass,03"},
3683 		{"worm",		"scsiclass,04"},
3684 		{"cdrom",		"scsiclass,05"},
3685 		{"scanner",		"scsiclass,06"},
3686 		{"optical-disk",	"scsiclass,07"},
3687 		{"medium-changer",	"scsiclass,08"},
3688 		{"obsolete",		"scsiclass,09"},
3689 		{"prepress-a",		"scsiclass,0a"},
3690 		{"prepress-b",		"scsiclass,0b"},
3691 		{"array-controller",	"scsiclass,0c"},
3692 		{"enclosure",		"scsiclass,0d"},
3693 		{"disk",		"scsiclass,0e"},
3694 		{"card-reader",		"scsiclass,0f"},
3695 		{"bridge",		"scsiclass,10"},
3696 		{"object-store",	"scsiclass,11"},
3697 		{"reserved",		"scsiclass,12"},
3698 		{"reserved",		"scsiclass,13"},
3699 		{"reserved",		"scsiclass,14"},
3700 		{"reserved",		"scsiclass,15"},
3701 		{"reserved",		"scsiclass,16"},
3702 		{"reserved",		"scsiclass,17"},
3703 		{"reserved",		"scsiclass,18"},
3704 		{"reserved",		"scsiclass,19"},
3705 		{"reserved",		"scsiclass,1a"},
3706 		{"reserved",		"scsiclass,1b"},
3707 		{"reserved",		"scsiclass,1c"},
3708 		{"reserved",		"scsiclass,1d"},
3709 		{"well-known-lun",	"scsiclass,1e"},
3710 		{"unknown",		"scsiclass,1f"},
3711 
3712 #ifdef	sparc
3713 	/* # legacy mapping to driver nodenames for fcp binding-set */
3714 		{"ssd",			"scsa,00.bfcp"},
3715 		{"st",			"scsa,01.bfcp"},
3716 		{"sgen",		"scsa,08.bfcp"},
3717 		{"ses",			"scsa,0d.bfcp"},
3718 
3719 	/* # legacy mapping to driver nodenames for vhci binding-set */
3720 		{"ssd",			"scsa,00.bvhci"},
3721 		{"st",			"scsa,01.bvhci"},
3722 		{"sgen",		"scsa,08.bvhci"},
3723 		{"ses",			"scsa,0d.bvhci"},
3724 #else	/* sparc */
3725 	/* # for x86 fcp and vhci use generic nodenames */
3726 #endif	/* sparc */
3727 
3728 	/* # legacy mapping to driver nodenames for spi binding-set */
3729 		{"sd",			"scsa,00.bspi"},
3730 		{"sd",			"scsa,05.bspi"},
3731 		{"sd",			"scsa,07.bspi"},
3732 		{"st",			"scsa,01.bspi"},
3733 		{"ses",			"scsa,0d.bspi"},
3734 
3735 	/* #				SUN misidentified spi hardware */
3736 		{"ses",			"scsiclass,03.vSUN.pD2"},
3737 		{"ses",			"scsiclass,03.vSYMBIOS.pD1000"},
3738 
3739 	/* # legacy mapping to driver nodenames for atapi binding-set */
3740 		{"sd",			"scsa,00.batapi"},
3741 		{"sd",			"scsa,05.batapi"},
3742 		{"sd",			"scsa,07.batapi"},
3743 		{"st",			"scsa,01.batapi"},
3744 		{"unknown",		"scsa,0d.batapi"},
3745 
3746 	/* # legacy mapping to generic nodenames for usb binding-set */
3747 		{"disk",		"scsa,05.busb"},
3748 		{"disk",		"scsa,07.busb"},
3749 		{"changer",		"scsa,08.busb"},
3750 		{"comm",		"scsa,09.busb"},
3751 		{"array_ctlr",		"scsa,0c.busb"},
3752 		{"esi",			"scsa,0d.busb"},
3753 
3754 	/*
3755 	 * mapping nodenames for mpt based on scsi dtype
3756 	 * for being compatible with the original node names
3757 	 * under mpt controller
3758 	 */
3759 		{"sd",			"scsa,00.bmpt"},
3760 		{"sd",			"scsa,05.bmpt"},
3761 		{"sd",			"scsa,07.bmpt"},
3762 		{"st",			"scsa,01.bmpt"},
3763 		{"ses",			"scsa,0d.bmpt"},
3764 		{"sgen",		"scsa,08.bmpt"},
3765 		{NULL,		NULL}
3766 	};
3767 	struct nodename_aliases *nap;
3768 
3769 	/* NOTE: drivernamep can be NULL */
3770 	ASSERT(nodenamep && compatiblep && ncompatiblep &&
3771 	    (binding_set == NULL || (strlen(binding_set) <= 8)));
3772 	if ((nodenamep == NULL) || (compatiblep == NULL) ||
3773 	    (ncompatiblep == NULL))
3774 		return;
3775 
3776 	/*
3777 	 * In order to reduce runtime we allocate one block of memory that
3778 	 * contains both the NULL terminated array of pointers to compatible
3779 	 * forms and the individual compatible strings. This block is
3780 	 * somewhat larger than needed, but is short lived - it only exists
3781 	 * until the caller can transfer the information into the "compatible"
3782 	 * string array property and call scsi_hba_nodename_compatible_free.
3783 	 */
3784 	tlen = NCOMPAT * COMPAT_LONGEST;
3785 	compatp = kmem_alloc((NCOMPAT * sizeof (char *)) + tlen, KM_SLEEP);
3786 
3787 	/* convert inquiry data from SCSI ASCII to 1275 string */
3788 	(void) string_scsi_to_1275(vid, inq->inq_vid,
3789 	    sizeof (inq->inq_vid));
3790 	(void) string_scsi_to_1275(pid, inq->inq_pid,
3791 	    sizeof (inq->inq_pid));
3792 	(void) string_scsi_to_1275(rev, inq->inq_revision,
3793 	    sizeof (inq->inq_revision));
3794 	ASSERT((strlen(vid) <= sizeof (inq->inq_vid)) &&
3795 	    (strlen(pid) <= sizeof (inq->inq_pid)) &&
3796 	    (strlen(rev) <= sizeof (inq->inq_revision)));
3797 
3798 	/*
3799 	 * Form flags in ***ALPHABETICAL*** order within form-group:
3800 	 *
3801 	 * NOTE: When adding a new flag to an existing form-group, careful
3802 	 * consideration must be given to not breaking existing bindings
3803 	 * based on that form-group.
3804 	 */
3805 
3806 	/*
3807 	 * generic form-group flags
3808 	 *   R	removable:
3809 	 *	Set when inq_rmb is set and for well known scsi dtypes. For a
3810 	 *	bus where the entire device is removable (like USB), we expect
3811 	 *	the HBA to intercept the inquiry data and set inq_rmb.
3812 	 *	Since OBP does not distinguish removable media in its generic
3813 	 *	name selection we avoid setting the 'R' flag if the root is not
3814 	 *	yet mounted.
3815 	 *   S	SAF-TE device
3816 	 *	Set when the device type is SAT-TE.
3817 	 */
3818 	i = 0;
3819 	dtype_device = inq->inq_dtype & DTYPE_MASK;
3820 	if (modrootloaded && (inq->inq_rmb ||
3821 	    (dtype_device == DTYPE_WORM) ||
3822 	    (dtype_device == DTYPE_RODIRECT) ||
3823 	    (dtype_device == DTYPE_OPTICAL)))
3824 		gf[i++] = 'R';			/* removable */
3825 	gf[i] = '\0';
3826 
3827 	if (modrootloaded &&
3828 	    (dtype_device == DTYPE_PROCESSOR) &&
3829 	    (strncmp((char *)&iqd[44], "SAF-TE", 4) == 0))
3830 		gf[i++] = 'S';
3831 	gf[i] = '\0';
3832 
3833 	/*
3834 	 * failover form-group flags
3835 	 *   E	Explicit Target_Port_Group_Supported:
3836 	 *	Set for a device that has a GUID if inq_tpgse also set.
3837 	 *   G	GUID:
3838 	 *	Set when we have identity information, can determine a devid
3839 	 *	from the identity information, and can generate a guid from
3840 	 *	that devid.
3841 	 *   I	Implicit Target_Port_Group_Supported:
3842 	 *	Set for a device that has a GUID if inq_tpgs also set.
3843 	 */
3844 	i = 0;
3845 	if ((inq80 || inq83) &&
3846 	    (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST, NULL,
3847 	    (uchar_t *)inq, sizeof (*inq), inq80, inq80len, inq83, inq83len,
3848 	    &devid) == DDI_SUCCESS)) {
3849 		guid = ddi_devid_to_guid(devid);
3850 		ddi_devid_free(devid);
3851 	} else
3852 		guid = NULL;
3853 	if (guid && (inq->inq_tpgs & TPGS_FAILOVER_EXPLICIT))
3854 		ff[i++] = 'E';			/* EXPLICIT TPGS */
3855 	if (guid)
3856 		ff[i++] = 'G';			/* GUID */
3857 	if (guid && (inq->inq_tpgs & TPGS_FAILOVER_IMPLICIT))
3858 		ff[i++] = 'I';			/* IMPLICIT TPGS */
3859 	ff[i] = '\0';
3860 	if (guid)
3861 		ddi_devid_free_guid(guid);
3862 
3863 	/*
3864 	 * Construct all applicable compatible forms. See comment at the
3865 	 * head of the function for a description of the compatible forms.
3866 	 */
3867 	csp = compatp;
3868 	p = (char *)(compatp + NCOMPAT);
3869 
3870 	/* ( 0) driver (optional, not documented in scsi(4)) */
3871 	if (compat0) {
3872 		*csp++ = p;
3873 		(void) snprintf(p, tlen, "%s", compat0);
3874 		len = strlen(p) + 1;
3875 		p += len;
3876 		tlen -= len;
3877 	}
3878 
3879 	/* ( 1) scsiclass,DDEEFFF.vV.pP.rR */
3880 	if ((dtype_device != dtype_node) && *gf && *vid && *pid && *rev) {
3881 		*csp++ = p;
3882 		(void) snprintf(p, tlen, "scsiclass,%02x%02x%s.v%s.p%s.r%s",
3883 		    dtype_node, dtype_device, gf, vid, pid, rev);
3884 		len = strlen(p) + 1;
3885 		p += len;
3886 		tlen -= len;
3887 	}
3888 
3889 	/* ( 2) scsiclass,DDEE.vV.pP.rR */
3890 	if ((dtype_device != dtype_node) && *vid && *pid && *rev) {
3891 		*csp++ = p;
3892 		(void) snprintf(p, tlen, "scsiclass,%02x%02x.v%s.p%s.r%s",
3893 		    dtype_node, dtype_device, vid, pid, rev);
3894 		len = strlen(p) + 1;
3895 		p += len;
3896 		tlen -= len;
3897 	}
3898 
3899 	/* ( 3) scsiclass,DDFFF.vV.pP.rR */
3900 	if (*gf && *vid && *pid && *rev) {
3901 		*csp++ = p;
3902 		(void) snprintf(p, tlen, "scsiclass,%02x%s.v%s.p%s.r%s",
3903 		    dtype_node, gf, vid, pid, rev);
3904 		len = strlen(p) + 1;
3905 		p += len;
3906 		tlen -= len;
3907 	}
3908 
3909 	/* ( 4) scsiclass,DD.vV.pP.rR */
3910 	if (*vid && *pid && rev) {
3911 		*csp++ = p;
3912 		(void) snprintf(p, tlen, "scsiclass,%02x.v%s.p%s.r%s",
3913 		    dtype_node, vid, pid, rev);
3914 		len = strlen(p) + 1;
3915 		p += len;
3916 		tlen -= len;
3917 	}
3918 
3919 	/* ( 5) scsiclass,DDEEFFF.vV.pP */
3920 	if ((dtype_device != dtype_node) && *gf && *vid && *pid) {
3921 		*csp++ = p;
3922 		(void) snprintf(p, tlen, "scsiclass,%02x%02x%s.v%s.p%s",
3923 		    dtype_node, dtype_device, gf, vid, pid);
3924 		len = strlen(p) + 1;
3925 		p += len;
3926 		tlen -= len;
3927 	}
3928 
3929 	/* ( 6) scsiclass,DDEE.vV.pP */
3930 	if ((dtype_device != dtype_node) && *vid && *pid) {
3931 		*csp++ = p;
3932 		(void) snprintf(p, tlen, "scsiclass,%02x%02x.v%s.p%s",
3933 		    dtype_node, dtype_device, vid, pid);
3934 		len = strlen(p) + 1;
3935 		p += len;
3936 		tlen -= len;
3937 	}
3938 
3939 	/* ( 7) scsiclass,DDFFF.vV.pP */
3940 	if (*gf && *vid && *pid) {
3941 		*csp++ = p;
3942 		(void) snprintf(p, tlen, "scsiclass,%02x%s.v%s.p%s",
3943 		    dtype_node, gf, vid, pid);
3944 		len = strlen(p) + 1;
3945 		p += len;
3946 		tlen -= len;
3947 	}
3948 
3949 	/* ( 8) scsiclass,DD.vV.pP */
3950 	if (*vid && *pid) {
3951 		*csp++ = p;
3952 		(void) snprintf(p, tlen, "scsiclass,%02x.v%s.p%s",
3953 		    dtype_node, vid, pid);
3954 		len = strlen(p) + 1;
3955 		p += len;
3956 		tlen -= len;
3957 	}
3958 
3959 	/* (8.5) scsa,DD.bB (not documented in scsi(4)) */
3960 	if (binding_set) {
3961 		*csp++ = p;
3962 		(void) snprintf(p, tlen, "scsa,%02x.b%s",
3963 		    dtype_node, binding_set);
3964 		len = strlen(p) + 1;
3965 		p += len;
3966 		tlen -= len;
3967 	}
3968 
3969 	/* ( 9) scsiclass,DDEEFFF */
3970 	if ((dtype_device != dtype_node) && *gf) {
3971 		*csp++ = p;
3972 		(void) snprintf(p, tlen, "scsiclass,%02x%02x%s",
3973 		    dtype_node, dtype_device, gf);
3974 		len = strlen(p) + 1;
3975 		p += len;
3976 		tlen -= len;
3977 	}
3978 
3979 	/* (10) scsiclass,DDEE */
3980 	if (dtype_device != dtype_node) {
3981 		*csp++ = p;
3982 		(void) snprintf(p, tlen, "scsiclass,%02x%02x",
3983 		    dtype_node, dtype_device);
3984 		len = strlen(p) + 1;
3985 		p += len;
3986 		tlen -= len;
3987 	}
3988 
3989 	/* (11) scsiclass,DDFFF */
3990 	if (*gf) {
3991 		*csp++ = p;
3992 		(void) snprintf(p, tlen, "scsiclass,%02x%s",
3993 		    dtype_node, gf);
3994 		len = strlen(p) + 1;
3995 		p += len;
3996 		tlen -= len;
3997 	}
3998 
3999 	/* (12) scsiclass,DD */
4000 	*csp++ = p;
4001 	(void) snprintf(p, tlen, "scsiclass,%02x", dtype_node);
4002 	len = strlen(p) + 1;
4003 	p += len;
4004 	tlen -= len;
4005 
4006 	/* (12.5) scsa.fFFF */
4007 	if (*ff) {
4008 		*csp++ = p;
4009 		(void) snprintf(p, tlen, "scsa.f%s", ff);
4010 		len = strlen(p) + 1;
4011 		p += len;
4012 		tlen -= len;
4013 	}
4014 
4015 	/* (13) scsiclass */
4016 	*csp++ = p;
4017 	(void) snprintf(p, tlen, "scsiclass");
4018 	len = strlen(p) + 1;
4019 	p += len;
4020 	tlen -= len;
4021 	ASSERT(tlen >= 0);
4022 
4023 	*csp = NULL;			/* NULL terminate array of pointers */
4024 	ncompat = csp - compatp;
4025 
4026 	/*
4027 	 * When determining a nodename, a nodename_aliases specified
4028 	 * mapping has precedence over using a driver_aliases specified
4029 	 * driver binding as a nodename.
4030 	 *
4031 	 * See if any of the compatible forms have a nodename_aliases
4032 	 * specified nodename. These mappings are described by
4033 	 * nodename_aliases entries like:
4034 	 *
4035 	 *	disk		"scsiclass,00"
4036 	 *	enclosure	"scsiclass,03.vSYMBIOS.pD1000"
4037 	 *	ssd		"scsa,00.bfcp"
4038 	 *
4039 	 * All nodename_aliases mappings should idealy be to generic
4040 	 * names, however a higher precedence legacy mapping to a
4041 	 * driver name may exist. The highest precedence mapping
4042 	 * provides the nodename, so legacy driver nodename mappings
4043 	 * (if they exist) take precedence over generic nodename
4044 	 * mappings.
4045 	 */
4046 	for (nname = NULL, csp = compatp; (nname == NULL) && *csp; csp++) {
4047 		for (nap = na; nap->na_nodename; nap++) {
4048 			if (strcmp(*csp, nap->na_alias) == 0) {
4049 				nname = nap->na_nodename;
4050 				break;
4051 			}
4052 		}
4053 	}
4054 
4055 	/*
4056 	 * Determine the driver name based on compatible (which may
4057 	 * have the passed in compat0 as the first item). The driver_aliases
4058 	 * file has entries like
4059 	 *
4060 	 *	sd	"scsiclass,00"
4061 	 *
4062 	 * that map compatible forms to specific drivers. These entries are
4063 	 * established by add_drv/update_drv. We use the most specific
4064 	 * driver binding as the nodename. This matches the eventual
4065 	 * ddi_driver_compatible_major() binding that will be
4066 	 * established by bind_node()
4067 	 */
4068 	for (dname = NULL, csp = compatp; *csp; csp++) {
4069 		major = ddi_name_to_major(*csp);
4070 		if ((major == DDI_MAJOR_T_NONE) ||
4071 		    (devnamesp[major].dn_flags & DN_DRIVER_REMOVED))
4072 			continue;
4073 		if (dname = ddi_major_to_name(major))
4074 			break;
4075 	}
4076 
4077 	/*
4078 	 * If no nodename_aliases mapping exists then use the
4079 	 * driver_aliases specified driver binding as a nodename.
4080 	 */
4081 	if (nname == NULL)
4082 		nname = dname;
4083 
4084 	/* return results */
4085 	if (nname) {
4086 		*nodenamep = kmem_alloc(strlen(nname) + 1, KM_SLEEP);
4087 		(void) strcpy(*nodenamep, nname);
4088 	} else {
4089 		*nodenamep = NULL;
4090 
4091 		/*
4092 		 * If no nodename could be determined return a special
4093 		 * 'compatible' to be used for a diagnostic message. This
4094 		 * compatible contains all compatible forms concatenated
4095 		 * into a single string pointed to by the first element.
4096 		 */
4097 		for (csp = compatp; *(csp + 1); csp++)
4098 			*((*csp) + strlen(*csp)) = ' ';
4099 		*(compatp + 1) = NULL;
4100 		ncompat = 1;
4101 
4102 	}
4103 	if (drivernamep) {
4104 		if (dname) {
4105 			*drivernamep = kmem_alloc(strlen(dname) + 1, KM_SLEEP);
4106 			(void) strcpy(*drivernamep, dname);
4107 		} else
4108 			*drivernamep = NULL;
4109 	}
4110 	*compatiblep = compatp;
4111 	*ncompatiblep = ncompat;
4112 }
4113 
4114 /*
4115  * Free allocations associated with scsi_hba_ident_nodename_compatible_get.
4116  */
4117 static void
4118 scsi_hba_ident_nodename_compatible_free(char *nodename, char *drivername,
4119     char **compatible)
4120 {
4121 	if (nodename)
4122 		kmem_free(nodename, strlen(nodename) + 1);
4123 	if (drivername)
4124 		kmem_free(drivername, strlen(drivername) + 1);
4125 	if (compatible)
4126 		kmem_free(compatible, (NCOMPAT * sizeof (char *)) +
4127 		    (NCOMPAT * COMPAT_LONGEST));
4128 }
4129 
4130 void
4131 scsi_hba_nodename_compatible_get(struct scsi_inquiry *inq,
4132     char *binding_set, int dtype_node, char *compat0,
4133     char **nodenamep, char ***compatiblep, int *ncompatiblep)
4134 {
4135 	scsi_hba_ident_nodename_compatible_get(inq,
4136 	    NULL, 0, NULL, 0, binding_set, dtype_node, compat0, nodenamep,
4137 	    NULL, compatiblep, ncompatiblep);
4138 }
4139 
4140 void
4141 scsi_hba_nodename_compatible_free(char *nodename, char **compatible)
4142 {
4143 	scsi_hba_ident_nodename_compatible_free(nodename, NULL, compatible);
4144 }
4145 
4146 /* return the unit_address associated with a scsi_device */
4147 char *
4148 scsi_device_unit_address(struct scsi_device *sd)
4149 {
4150 	mdi_pathinfo_t	*pip;
4151 
4152 	ASSERT(sd && sd->sd_dev);
4153 	if ((sd == NULL) || (sd->sd_dev == NULL))
4154 		return (NULL);
4155 
4156 	pip = (mdi_pathinfo_t *)sd->sd_pathinfo;
4157 	if (pip)
4158 		return (mdi_pi_get_addr(pip));
4159 	else
4160 		return (ddi_get_name_addr(sd->sd_dev));
4161 }
4162 
4163 /* scsi_device property interfaces */
4164 #define	_TYPE_DEFINED(flags)						\
4165 	(((flags & SCSI_DEVICE_PROP_TYPE_MSK) == SCSI_DEVICE_PROP_PATH) || \
4166 	((flags & SCSI_DEVICE_PROP_TYPE_MSK) == SCSI_DEVICE_PROP_DEVICE))
4167 
4168 #define	_DEVICE_PIP(sd, flags)						\
4169 	((((flags & SCSI_DEVICE_PROP_TYPE_MSK) == SCSI_DEVICE_PROP_PATH) && \
4170 	sd->sd_pathinfo) ? (mdi_pathinfo_t *)sd->sd_pathinfo : NULL)
4171 
4172 int
4173 scsi_device_prop_get_int(struct scsi_device *sd, uint_t flags,
4174     char *name, int defval)
4175 {
4176 	mdi_pathinfo_t	*pip;
4177 	int		v = defval;
4178 	int		data;
4179 	int		rv;
4180 
4181 	ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
4182 	if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
4183 	    !_TYPE_DEFINED(flags))
4184 		return (v);
4185 
4186 	pip = _DEVICE_PIP(sd, flags);
4187 	if (pip) {
4188 		rv = mdi_prop_lookup_int(pip, name, &data);
4189 		if (rv == DDI_PROP_SUCCESS)
4190 			v = data;
4191 	} else
4192 		v = ddi_prop_get_int(DDI_DEV_T_ANY, sd->sd_dev,
4193 		    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, name, v);
4194 	return (v);
4195 }
4196 
4197 
4198 int64_t
4199 scsi_device_prop_get_int64(struct scsi_device *sd, uint_t flags,
4200     char *name, int64_t defval)
4201 {
4202 	mdi_pathinfo_t	*pip;
4203 	int64_t		v = defval;
4204 	int64_t		data;
4205 	int		rv;
4206 
4207 	ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
4208 	if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
4209 	    !_TYPE_DEFINED(flags))
4210 		return (v);
4211 
4212 	pip = _DEVICE_PIP(sd, flags);
4213 	if (pip) {
4214 		rv = mdi_prop_lookup_int64(pip, name, &data);
4215 		if (rv == DDI_PROP_SUCCESS)
4216 			v = data;
4217 	} else
4218 		v = ddi_prop_get_int64(DDI_DEV_T_ANY, sd->sd_dev,
4219 		    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, name, v);
4220 	return (v);
4221 }
4222 
4223 int
4224 scsi_device_prop_lookup_byte_array(struct scsi_device *sd, uint_t flags,
4225     char *name, uchar_t **data, uint_t *nelements)
4226 {
4227 	mdi_pathinfo_t	*pip;
4228 	int		rv;
4229 
4230 	ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
4231 	if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
4232 	    !_TYPE_DEFINED(flags))
4233 		return (DDI_PROP_INVAL_ARG);
4234 
4235 	pip = _DEVICE_PIP(sd, flags);
4236 	if (pip)
4237 		rv = mdi_prop_lookup_byte_array(pip, name, data, nelements);
4238 	else
4239 		rv = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, sd->sd_dev,
4240 		    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
4241 		    name, data, nelements);
4242 	return (rv);
4243 }
4244 
4245 int
4246 scsi_device_prop_lookup_int_array(struct scsi_device *sd, uint_t flags,
4247     char *name, int **data, uint_t *nelements)
4248 {
4249 	mdi_pathinfo_t	*pip;
4250 	int		rv;
4251 
4252 	ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
4253 	if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
4254 	    !_TYPE_DEFINED(flags))
4255 		return (DDI_PROP_INVAL_ARG);
4256 
4257 	pip = _DEVICE_PIP(sd, flags);
4258 	if (pip)
4259 		rv = mdi_prop_lookup_int_array(pip, name, data, nelements);
4260 	else
4261 		rv = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, sd->sd_dev,
4262 		    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
4263 		    name, data, nelements);
4264 	return (rv);
4265 }
4266 
4267 
4268 int
4269 scsi_device_prop_lookup_string(struct scsi_device *sd, uint_t flags,
4270     char *name, char **data)
4271 {
4272 	mdi_pathinfo_t	*pip;
4273 	int		rv;
4274 
4275 	ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
4276 	if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
4277 	    !_TYPE_DEFINED(flags))
4278 		return (DDI_PROP_INVAL_ARG);
4279 
4280 	pip = _DEVICE_PIP(sd, flags);
4281 	if (pip)
4282 		rv = mdi_prop_lookup_string(pip, name, data);
4283 	else
4284 		rv = ddi_prop_lookup_string(DDI_DEV_T_ANY, sd->sd_dev,
4285 		    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
4286 		    name, data);
4287 	return (rv);
4288 }
4289 
4290 int
4291 scsi_device_prop_lookup_string_array(struct scsi_device *sd, uint_t flags,
4292     char *name, char ***data, uint_t *nelements)
4293 {
4294 	mdi_pathinfo_t	*pip;
4295 	int		rv;
4296 
4297 	ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
4298 	if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
4299 	    !_TYPE_DEFINED(flags))
4300 		return (DDI_PROP_INVAL_ARG);
4301 
4302 	pip = _DEVICE_PIP(sd, flags);
4303 	if (pip)
4304 		rv = mdi_prop_lookup_string_array(pip, name, data, nelements);
4305 	else
4306 		rv = ddi_prop_lookup_string_array(DDI_DEV_T_ANY, sd->sd_dev,
4307 		    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
4308 		    name, data, nelements);
4309 	return (rv);
4310 }
4311 
4312 int
4313 scsi_device_prop_update_byte_array(struct scsi_device *sd, uint_t flags,
4314     char *name, uchar_t *data, uint_t nelements)
4315 {
4316 	mdi_pathinfo_t	*pip;
4317 	int		rv;
4318 
4319 	ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
4320 	if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
4321 	    !_TYPE_DEFINED(flags))
4322 		return (DDI_PROP_INVAL_ARG);
4323 
4324 	pip = _DEVICE_PIP(sd, flags);
4325 	if (pip)
4326 		rv = mdi_prop_update_byte_array(pip, name, data, nelements);
4327 	else
4328 		rv = ndi_prop_update_byte_array(DDI_DEV_T_NONE, sd->sd_dev,
4329 		    name, data, nelements);
4330 	return (rv);
4331 }
4332 
4333 int
4334 scsi_device_prop_update_int(struct scsi_device *sd, uint_t flags,
4335     char *name, int data)
4336 {
4337 	mdi_pathinfo_t	*pip;
4338 	int		rv;
4339 
4340 	ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
4341 	if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
4342 	    !_TYPE_DEFINED(flags))
4343 		return (DDI_PROP_INVAL_ARG);
4344 
4345 	pip = _DEVICE_PIP(sd, flags);
4346 	if (pip)
4347 		rv = mdi_prop_update_int(pip, name, data);
4348 	else
4349 		rv = ndi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
4350 		    name, data);
4351 	return (rv);
4352 }
4353 
4354 int
4355 scsi_device_prop_update_int64(struct scsi_device *sd, uint_t flags,
4356     char *name, int64_t data)
4357 {
4358 	mdi_pathinfo_t	*pip;
4359 	int		rv;
4360 
4361 	ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
4362 	if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
4363 	    !_TYPE_DEFINED(flags))
4364 		return (DDI_PROP_INVAL_ARG);
4365 
4366 	pip = _DEVICE_PIP(sd, flags);
4367 	if (pip)
4368 		rv = mdi_prop_update_int64(pip, name, data);
4369 	else
4370 		rv = ndi_prop_update_int64(DDI_DEV_T_NONE, sd->sd_dev,
4371 		    name, data);
4372 	return (rv);
4373 }
4374 
4375 int
4376 scsi_device_prop_update_int_array(struct scsi_device *sd, uint_t flags,
4377     char *name, int *data, uint_t nelements)
4378 {
4379 	mdi_pathinfo_t	*pip;
4380 	int		rv;
4381 
4382 	ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
4383 	if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
4384 	    !_TYPE_DEFINED(flags))
4385 		return (DDI_PROP_INVAL_ARG);
4386 
4387 	pip = _DEVICE_PIP(sd, flags);
4388 	if (pip)
4389 		rv = mdi_prop_update_int_array(pip, name, data, nelements);
4390 	else
4391 		rv = ndi_prop_update_int_array(DDI_DEV_T_NONE, sd->sd_dev,
4392 		    name, data, nelements);
4393 	return (rv);
4394 }
4395 
4396 int
4397 scsi_device_prop_update_string(struct scsi_device *sd, uint_t flags,
4398     char *name, char *data)
4399 {
4400 	mdi_pathinfo_t	*pip;
4401 	int		rv;
4402 
4403 	ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
4404 	if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
4405 	    !_TYPE_DEFINED(flags))
4406 		return (DDI_PROP_INVAL_ARG);
4407 
4408 	pip = _DEVICE_PIP(sd, flags);
4409 	if (pip)
4410 		rv = mdi_prop_update_string(pip, name, data);
4411 	else
4412 		rv = ndi_prop_update_string(DDI_DEV_T_NONE, sd->sd_dev,
4413 		    name, data);
4414 	return (rv);
4415 }
4416 
4417 int
4418 scsi_device_prop_update_string_array(struct scsi_device *sd, uint_t flags,
4419     char *name, char **data, uint_t nelements)
4420 {
4421 	mdi_pathinfo_t	*pip;
4422 	int		rv;
4423 
4424 	ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
4425 	if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
4426 	    !_TYPE_DEFINED(flags))
4427 		return (DDI_PROP_INVAL_ARG);
4428 
4429 	pip = _DEVICE_PIP(sd, flags);
4430 	if (pip)
4431 		rv = mdi_prop_update_string_array(pip, name, data, nelements);
4432 	else
4433 		rv = ndi_prop_update_string_array(DDI_DEV_T_NONE, sd->sd_dev,
4434 		    name, data, nelements);
4435 	return (rv);
4436 }
4437 
4438 int
4439 scsi_device_prop_remove(struct scsi_device *sd, uint_t flags, char *name)
4440 {
4441 	mdi_pathinfo_t	*pip;
4442 	int		rv;
4443 
4444 	ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
4445 	if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
4446 	    !_TYPE_DEFINED(flags))
4447 		return (DDI_PROP_INVAL_ARG);
4448 
4449 	pip = _DEVICE_PIP(sd, flags);
4450 	if (pip)
4451 		rv = mdi_prop_remove(pip, name);
4452 	else
4453 		rv = ndi_prop_remove(DDI_DEV_T_NONE, sd->sd_dev, name);
4454 	return (rv);
4455 }
4456 
4457 void
4458 scsi_device_prop_free(struct scsi_device *sd, uint_t flags, void *data)
4459 {
4460 	mdi_pathinfo_t	*pip;
4461 
4462 	ASSERT(sd && data && sd->sd_dev && _TYPE_DEFINED(flags));
4463 	if ((sd == NULL) || (data == NULL) || (sd->sd_dev == NULL) ||
4464 	    !_TYPE_DEFINED(flags))
4465 		return;
4466 
4467 	pip = _DEVICE_PIP(sd, flags);
4468 	if (pip)
4469 		(void) mdi_prop_free(data);
4470 	else
4471 		ddi_prop_free(data);
4472 }
4473 
4474 /*
4475  * scsi_hba_ua_set: given "unit-address" string, set properties.
4476  *
4477  * Function to set the properties on a devinfo or pathinfo node from
4478  * the "unit-address" part of a "name@unit-address" /devices path 'name'
4479  * string.
4480  *
4481  * This function works in conjunction with scsi_ua_get()/scsi_hba_ua_get()
4482  * (and possibly with an HBA driver's tran_tgt_init() implementation).
4483  */
4484 static int
4485 scsi_hba_ua_set(char *ua, dev_info_t *dchild, mdi_pathinfo_t *pchild)
4486 {
4487 	char		*p;
4488 	int		tgt;
4489 	char		*tgt_port_end;
4490 	char		*tgt_port;
4491 	int		tgt_port_len;
4492 	int		sfunc;
4493 	scsi_lun64_t	lun64;
4494 
4495 	/* Caller must choose to decorate devinfo *or* pathinfo */
4496 	ASSERT((dchild != NULL) ^ (pchild != NULL));
4497 	if (dchild && pchild)
4498 		return (0);
4499 
4500 	/*
4501 	 * generic implementation based on "tgt,lun[,sfunc]" address form.
4502 	 * parse hex "tgt" part of "tgt,lun[,sfunc]"
4503 	 */
4504 	p = ua;
4505 	tgt_port_end = NULL;
4506 	for (tgt = 0; *p && *p != ','; p++) {
4507 		if (*p >= '0' && *p <= '9')
4508 			tgt = (tgt << 4) + (*p - '0');
4509 		else if (*p >= 'a' && *p <= 'f')
4510 			tgt = (tgt << 4) + 10 + (*p - 'a');
4511 		else
4512 			tgt = -1;		/* non-numeric */
4513 
4514 		/*
4515 		 * if non-numeric or our of range set tgt to -1 and
4516 		 * skip forward
4517 		 */
4518 		if (tgt < 0) {
4519 			tgt = -1;
4520 			for (; *p && *p != ','; p++)
4521 				;
4522 			break;
4523 		}
4524 	}
4525 	tgt_port_end = p;
4526 
4527 	/* parse hex ",lun" part of "tgt,lun[,sfunc]" */
4528 	if (*p)
4529 		p++;
4530 	for (lun64 = 0; *p && *p != ','; p++) {
4531 		if (*p >= '0' && *p <= '9')
4532 			lun64 = (lun64 << 4) + (*p - '0');
4533 		else if (*p >= 'a' && *p <= 'f')
4534 			lun64 = (lun64 << 4) + 10 + (*p - 'a');
4535 		else
4536 			return (0);
4537 	}
4538 
4539 	/* parse hex ",sfunc" part of "tgt,lun[,sfunc]" */
4540 	if (*p) {
4541 		p++;
4542 		for (sfunc = 0; *p; p++) {
4543 			if (*p >= '0' && *p <= '9')
4544 				sfunc = (sfunc << 4) + (*p - '0');
4545 			else if (*p >= 'a' && *p <= 'f')
4546 				sfunc = (sfunc << 4) + 10 + (*p - 'a');
4547 			else
4548 				return (0);
4549 		}
4550 	} else
4551 		sfunc = -1;
4552 
4553 	if (dchild) {
4554 		/*
4555 		 * Decorate a devinfo node with unit address properties.
4556 		 * This adds the the addressing properties needed to
4557 		 * DDI_CTLOPS_UNINITCHILD the devinfo node (i.e. perform
4558 		 * the reverse operation - form unit address from properties).
4559 		 */
4560 		if ((tgt != -1) && (ndi_prop_update_int(DDI_DEV_T_NONE, dchild,
4561 		    SCSI_ADDR_PROP_TARGET, tgt) != DDI_PROP_SUCCESS))
4562 			return (0);
4563 
4564 		if (tgt_port_end) {
4565 			tgt_port_len = tgt_port_end - ua + 1;
4566 			tgt_port = kmem_alloc(tgt_port_len, KM_SLEEP);
4567 			(void) strlcpy(tgt_port, ua, tgt_port_len);
4568 			if (ndi_prop_update_string(DDI_DEV_T_NONE, dchild,
4569 			    SCSI_ADDR_PROP_TARGET_PORT, tgt_port) !=
4570 			    DDI_PROP_SUCCESS) {
4571 				kmem_free(tgt_port, tgt_port_len);
4572 				return (0);
4573 			}
4574 			kmem_free(tgt_port, tgt_port_len);
4575 		}
4576 
4577 		/* Set the appropriate lun properties. */
4578 		if (lun64 < SCSI_32LUNS_PER_TARGET) {
4579 			if (ndi_prop_update_int(DDI_DEV_T_NONE, dchild,
4580 			    SCSI_ADDR_PROP_LUN, (int)lun64) != DDI_PROP_SUCCESS)
4581 				return (0);
4582 		}
4583 		if (ndi_prop_update_int64(DDI_DEV_T_NONE, dchild,
4584 		    SCSI_ADDR_PROP_LUN64, lun64) != DDI_PROP_SUCCESS)
4585 			return (0);
4586 
4587 		/* Set the sfunc property */
4588 		if ((sfunc != -1) &&
4589 		    (ndi_prop_update_int(DDI_DEV_T_NONE, dchild,
4590 		    SCSI_ADDR_PROP_SFUNC, (int)sfunc) != DDI_PROP_SUCCESS))
4591 			return (0);
4592 	} else if (pchild) {
4593 		/*
4594 		 * Decorate a pathinfo node with unit address properties.
4595 		 */
4596 		if ((tgt != -1) && (mdi_prop_update_int(pchild,
4597 		    SCSI_ADDR_PROP_TARGET, tgt) != DDI_PROP_SUCCESS))
4598 			return (0);
4599 
4600 		if (tgt_port_end) {
4601 			tgt_port_len = tgt_port_end - ua + 1;
4602 			tgt_port = kmem_alloc(tgt_port_len, KM_SLEEP);
4603 			(void) strlcpy(tgt_port, ua, tgt_port_len);
4604 			if (mdi_prop_update_string(pchild,
4605 			    SCSI_ADDR_PROP_TARGET_PORT, tgt_port) !=
4606 			    DDI_PROP_SUCCESS) {
4607 				kmem_free(tgt_port, tgt_port_len);
4608 				return (0);
4609 			}
4610 			kmem_free(tgt_port, tgt_port_len);
4611 		}
4612 
4613 		/* Set the appropriate lun properties */
4614 		if (lun64 < SCSI_32LUNS_PER_TARGET) {
4615 			if (mdi_prop_update_int(pchild, SCSI_ADDR_PROP_LUN,
4616 			    (int)lun64) != DDI_PROP_SUCCESS)
4617 				return (0);
4618 		}
4619 
4620 		if (mdi_prop_update_int64(pchild, SCSI_ADDR_PROP_LUN64,
4621 		    lun64) != DDI_PROP_SUCCESS)
4622 			return (0);
4623 
4624 		/* Set the sfunc property */
4625 		if ((sfunc != -1) &&
4626 		    (mdi_prop_update_int(pchild,
4627 		    SCSI_ADDR_PROP_SFUNC, (int)sfunc) != DDI_PROP_SUCCESS))
4628 			return (0);
4629 	}
4630 	return (1);
4631 }
4632 
4633 /*
4634  * Private ndi_devi_find/mdi_pi_find implementation - find the child
4635  * dev_info/path_info of self whose phci name matches "name@caddr".
4636  * We have our own implementation because we need to search with both
4637  * forms of sibling lists (dev_info and path_info) and we need to be able
4638  * to search with a NULL name in order to find siblings already associated
4639  * with a given unit-address (same @addr). NOTE: NULL name search will never
4640  * return probe node.
4641  *
4642  * If pchildp is NULL and we find a pathinfo child, we return the client
4643  * devinfo node in *dchildp.
4644  *
4645  * The init flag argument should be clear when called from places where
4646  * recursion could occur (like scsi_busctl_initchild) and when the caller
4647  * has already performed a search for name@addr with init set (performance).
4648  *
4649  * Future: Integrate ndi_devi_findchild_by_callback into scsi_hba_find_child.
4650  */
4651 static int
4652 scsi_hba_find_child(dev_info_t *self, char *name, char *addr, int init,
4653     dev_info_t **dchildp, mdi_pathinfo_t **pchildp, int *ppi)
4654 {
4655 	dev_info_t	*dchild;	/* devinfo child */
4656 	mdi_pathinfo_t	*pchild;	/* pathinfo child */
4657 	int		found = CHILD_TYPE_NONE;
4658 	char		*daddr;
4659 
4660 	ASSERT(self && DEVI_BUSY_OWNED(self));
4661 	ASSERT(addr && dchildp);
4662 	if ((self == NULL) || (addr == NULL) || (dchildp == NULL))
4663 		return (CHILD_TYPE_NONE);
4664 
4665 	*dchildp = NULL;
4666 	if (pchildp)
4667 		*pchildp = NULL;
4668 	if (ppi)
4669 		*ppi = 0;
4670 
4671 	/* Walk devinfo child list to find a match */
4672 	for (dchild = ddi_get_child(self); dchild;
4673 	    dchild = ddi_get_next_sibling(dchild)) {
4674 		if (i_ddi_node_state(dchild) < DS_INITIALIZED)
4675 			continue;
4676 
4677 		daddr = ddi_get_name_addr(dchild);
4678 		if (daddr && (strcmp(addr, daddr) == 0) &&
4679 		    ((name == NULL) ||
4680 		    (strcmp(name, DEVI(dchild)->devi_node_name) == 0))) {
4681 			/*
4682 			 * If we are asked to find "anything" at a given
4683 			 * unit-address (name == NULL), we don't realy want
4684 			 * to find the 'probe' node. The existance of
4685 			 * a probe node on a 'name == NULL' search should
4686 			 * fail.  This will trigger slow-path code where
4687 			 * we explicity look for, and synchronize against,
4688 			 * a node named "probe" at the unit-address.
4689 			 */
4690 			if ((name == NULL) &&
4691 			    scsi_hba_devi_is_barrier(dchild)) {
4692 				SCSI_HBA_LOG((_LOG(4), NULL, dchild,
4693 				    "%s@%s 'probe' devinfo found, skip",
4694 				    name ? name : "", addr));
4695 				continue;
4696 			}
4697 
4698 			/* We have found a match. */
4699 			found |= CHILD_TYPE_DEVINFO;
4700 			SCSI_HBA_LOG((_LOG(4), NULL, dchild,
4701 			    "%s@%s devinfo found", name ? name : "", addr));
4702 			*dchildp = dchild;		/* devinfo found */
4703 			break;
4704 		}
4705 	}
4706 
4707 	/*
4708 	 * Walk pathinfo child list to find a match.
4709 	 *
4710 	 * NOTE: Unlike devinfo nodes, pathinfo nodes have a string searchable
4711 	 * unit-address from creation - so there is no need for an 'init'
4712 	 * search block of code for pathinfo nodes below.
4713 	 */
4714 	pchild = mdi_pi_find(self, NULL, addr);
4715 	if (pchild) {
4716 		/*
4717 		 * NOTE: If name specified and we match a pathinfo unit
4718 		 * address, we don't check the client node name.
4719 		 */
4720 		if (ppi)
4721 			*ppi = mdi_pi_get_path_instance(pchild);
4722 		found |= CHILD_TYPE_PATHINFO;
4723 
4724 		if (pchildp) {
4725 			SCSI_HBA_LOG((_LOG(4), self, NULL,
4726 			    "%s pathinfo found", mdi_pi_spathname(pchild)));
4727 			*pchildp = pchild;		/* pathinfo found */
4728 		} else if (*dchildp == NULL) {
4729 			/*
4730 			 * Did not find a devinfo node, found a pathinfo node,
4731 			 * but caller did not ask us to return a pathinfo node:
4732 			 * we return the 'client' devinfo node instead (but
4733 			 * with CHILD_TYPE_PATHINFO 'found' return value).
4734 			 */
4735 			dchild = mdi_pi_get_client(pchild);
4736 			SCSI_HBA_LOG((_LOG(4), NULL, dchild,
4737 			    "%s pathinfo found, client switch",
4738 			    mdi_pi_spathname(pchild)));
4739 
4740 			/*
4741 			 * A pathinfo node always has a 'client' devinfo node,
4742 			 * but we need to ensure that the 'client' is
4743 			 * initialized and has a scsi_device structure too.
4744 			 */
4745 			ASSERT(dchild);
4746 			if (i_ddi_node_state(dchild) < DS_INITIALIZED) {
4747 				SCSI_HBA_LOG((_LOG(4), NULL, dchild,
4748 				    "%s found client, initchild",
4749 				    mdi_pi_spathname(pchild)));
4750 				(void) ddi_initchild(ddi_get_parent(dchild),
4751 				    dchild);
4752 			}
4753 			if (i_ddi_node_state(dchild) >= DS_INITIALIZED) {
4754 				/* client found and initialized */
4755 				*dchildp = dchild;
4756 			} else {
4757 				SCSI_HBA_LOG((_LOG(4), NULL, dchild,
4758 				    "%s found client, but failed initchild",
4759 				    mdi_pi_spathname(pchild)));
4760 			}
4761 		}
4762 	}
4763 
4764 	/* Try devinfo again with initchild of uninitialized nodes */
4765 	if ((found == CHILD_TYPE_NONE) && init) {
4766 		for (dchild = ddi_get_child(self); dchild;
4767 		    dchild = ddi_get_next_sibling(dchild)) {
4768 			/* skip if checked above */
4769 			if (i_ddi_node_state(dchild) >= DS_INITIALIZED)
4770 				continue;
4771 			/* attempt initchild to establish unit-address */
4772 			(void) ddi_initchild(self, dchild);
4773 			if (i_ddi_node_state(dchild) < DS_INITIALIZED)
4774 				continue;
4775 			daddr = ddi_get_name_addr(dchild);
4776 			if (daddr &&
4777 			    ((name == NULL) || (strcmp(name,
4778 			    DEVI(dchild)->devi_node_name) == 0)) &&
4779 			    (strcmp(addr, daddr) == 0)) {
4780 				found |= CHILD_TYPE_DEVINFO;
4781 				SCSI_HBA_LOG((_LOG(4), NULL, dchild,
4782 				    "%s@%s devinfo found post initchild",
4783 				    name ? name : "", addr));
4784 				*dchildp = dchild;	/* devinfo found */
4785 				break;	/* node found */
4786 			}
4787 		}
4788 	}
4789 
4790 	/*
4791 	 * We should never find devinfo and pathinfo at the same
4792 	 * unit-address.
4793 	 */
4794 	ASSERT(found != (CHILD_TYPE_DEVINFO | CHILD_TYPE_PATHINFO));
4795 	if (found == (CHILD_TYPE_DEVINFO | CHILD_TYPE_PATHINFO)) {
4796 		found = CHILD_TYPE_NONE;
4797 		*dchildp = NULL;
4798 		*pchildp = NULL;
4799 	}
4800 	return (found);
4801 }
4802 
4803 /*
4804  * Given information about a child device (contained on probe node) construct
4805  * and return a pointer to the dynamic SID devinfo node associated with the
4806  * device. In the creation of this SID node a compatible property for the
4807  * device is formed and used to establish a nodename (via
4808  * /etc/nodename_aliases) and to bind a driver (via /etc/driver_aliases).
4809  *
4810  * If this routine is called then we got a response from a device and
4811  * obtained the inquiry data from the device. Some inquiry results indicate
4812  * that the specific LUN we addressed does not exist, and we don't want to
4813  * bind a standard target driver to the node we create. Even though the
4814  * specific LUN is not usable, the framework may still want to bind a
4815  * target driver to the device for internal communication with the device -
4816  * an example would be issuing a report_lun to enumerate other LUNs under a
4817  * DPQ_NEVER LUN0. Another example would be wanting to known that the
4818  * DPQ_NEVER LUN0 device exists in BUS_CONFIG_ONE for non-existent LUN
4819  * caching optimizations. To support this we let the caller specify a
4820  * compatible property (or driver). If LUN0 inquiry data indicates that the
4821  * LUN does not exist then we establish compat0 as the highest precedence(0)
4822  * compatible form. If used, this compat0 driver will never be called on to
4823  * issue external commands to the device.
4824  *
4825  * If no driver binds to the device using driver_alias we establish the driver
4826  * passed in as the node name.
4827  */
4828 static int
4829 scsi_device_createchild(dev_info_t *self, char *addr, scsi_enum_t se,
4830     struct scsi_device *sdprobe, dev_info_t **dchildp, mdi_pathinfo_t **pchildp)
4831 {
4832 	scsi_lun64_t		lun64;
4833 	int			dtype;
4834 	int			dpq;
4835 	int			dpq_vu;
4836 	int			dtype_node;
4837 	int			lunexists;
4838 	char			*compat0;
4839 	char			*nname;
4840 	char			**compat = NULL;
4841 	int			ncompat;
4842 	dev_info_t		*dchild = NULL;
4843 	mdi_pathinfo_t		*pchild = NULL;
4844 	dev_info_t		*probe = sdprobe->sd_dev;
4845 	struct scsi_inquiry	*inq = sdprobe->sd_inq;
4846 	uchar_t			*inq80 = NULL;
4847 	uchar_t			*inq83 = NULL;
4848 	uint_t			inq80len, inq83len;
4849 	char			*binding_set = NULL;
4850 	char			*dname = NULL;
4851 	ddi_devid_t		devid;
4852 	int			have_devid = 0;
4853 	char			*devid_str;
4854 	char			*guid = NULL;
4855 
4856 	ASSERT(self && addr && *addr && DEVI_BUSY_OWNED(self));
4857 	ASSERT(dchildp && pchildp);
4858 
4859 	/*
4860 	 * Determine the lun and whether the lun exists. We may need to create
4861 	 * a node for LUN0 (with compat0 driver binding) even if the lun does
4862 	 * not exist - so we can run report_lun to find additional LUNs.
4863 	 */
4864 	lun64 = scsi_addr_to_lun64(addr);
4865 	dtype = inq->inq_dtype & DTYPE_MASK;		/* device */
4866 	dpq = inq->inq_dtype & DPQ_MASK;
4867 	dpq_vu = inq->inq_dtype & DPQ_VUNIQ ? 1 : 0;
4868 
4869 	dtype_node = scsi_addr_to_sfunc(addr);		/* secondary function */
4870 	if (dtype_node == -1)
4871 		dtype_node = dtype;			/* node for device */
4872 
4873 	lunexists = (dtype != dtype_node) ||		/* override */
4874 	    ((dpq_vu == 0) && (dpq == DPQ_POSSIBLE)) ||	/* ANSII */
4875 	    (dpq_vu && (lun64 == 0));			/* VU LUN0 */
4876 	if (dtype == DTYPE_UNKNOWN)
4877 		lunexists = 0;
4878 
4879 	SCSI_HBA_LOG((_LOG(4), self, NULL,
4880 	    "@%s dtype %x %x dpq_vu %d dpq %x: %d",
4881 	    addr, dtype, dtype_node, dpq_vu, dpq, lunexists));
4882 
4883 	/* A non-existent LUN0 uses compatible_nodev. */
4884 	if (lunexists) {
4885 		compat0 = NULL;				/* compat0 not needed */
4886 	} else if (lun64 == 0) {
4887 		compat0 = compatible_nodev;
4888 		SCSI_HBA_LOG((_LOG(2), self, NULL,
4889 		    "@%s lun 0 with compat0 %s", addr, compat0));
4890 	} else
4891 		goto out;				/* no node created */
4892 
4893 	/* Obtain identity information from probe node. */
4894 	if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, probe,
4895 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "inquiry-page-80",
4896 	    &inq80, &inq80len) != DDI_PROP_SUCCESS)
4897 		inq80 = NULL;
4898 	if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, probe,
4899 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "inquiry-page-83",
4900 	    &inq83, &inq83len) != DDI_PROP_SUCCESS)
4901 		inq83 = NULL;
4902 
4903 	/* Get "scsi-binding-set" property (if there is one). */
4904 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, self,
4905 	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
4906 	    "scsi-binding-set", &binding_set) == DDI_PROP_SUCCESS)
4907 		SCSI_HBA_LOG((_LOG(2), NULL, probe,
4908 		    "binding_set '%s'", binding_set));
4909 
4910 	/* determine the node name and compatible information */
4911 	scsi_hba_ident_nodename_compatible_get(inq,
4912 	    inq80, inq80len, inq83, inq83len, binding_set, dtype_node,
4913 	    compat0, &nname, &dname, &compat, &ncompat);
4914 
4915 	if (nname == NULL) {
4916 		/*
4917 		 * We will not be able to create a node because we could not
4918 		 * determine a node name. Print out a NODRIVER level warning
4919 		 * message with the compatible forms for the device. Note that
4920 		 * there may be a driver.conf node that attaches to the device,
4921 		 * which is why we only produce this warning message for debug
4922 		 * kernels.
4923 		 */
4924 		SCSI_HBA_LOG((_LOG(1), NULL, self,
4925 		    "no node_name for device @%s:\n         compatible: %s",
4926 		    addr, *compat));
4927 		goto out;
4928 	}
4929 
4930 	/*
4931 	 * FUTURE: some day we may want an accurate "compatible" on the probe
4932 	 * node so that vhci_is_dev_supported() in scsi_vhci could, at
4933 	 * least in part, determine/configure based on "compatible".
4934 	 *
4935 	 *	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, probe,
4936 	 *	    "compatible", compat, ncompat) != DDI_PROP_SUCCESS) {
4937 	 *		SCSI_HBA_LOG((_LOG(3), self, NULL,
4938 	 *		    "%s@%s failed probe compatible decoration",
4939 	 *		    nname, addr));
4940 	 *		goto out;
4941 	 *	}
4942 	 */
4943 
4944 	/* Encode devid from identity information. */
4945 	if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST, dname,
4946 	    (uchar_t *)inq, sizeof (*inq), inq80, inq80len, inq83, inq83len,
4947 	    &devid) == DDI_SUCCESS) {
4948 		have_devid = 1;
4949 
4950 		/* Attempt to form guid from devid. */
4951 		guid = ddi_devid_to_guid(devid);
4952 
4953 		/* Produce string devid for debug. */
4954 		devid_str = ddi_devid_str_encode(devid, NULL);
4955 		SCSI_HBA_LOG((_LOG(3), self, probe, "devid '%s' guid '%s'",
4956 		    devid_str ? devid_str : "NULL", guid ? guid : "NULL"));
4957 		ddi_devid_str_free(devid_str);
4958 	}
4959 
4960 
4961 	/*
4962 	 * Determine if the device should be enumerated as under the vHCI
4963 	 * (client node) or under the pHCI. By convention scsi_vhci expects
4964 	 * the "cinfo" argument identity information to be represented as a
4965 	 * devinfo node with the needed information (i.e. the pHCI probe node).
4966 	 */
4967 	if ((guid == NULL) ||
4968 	    (mdi_is_dev_supported(MDI_HCI_CLASS_SCSI, self, sdprobe) !=
4969 	    MDI_SUCCESS)) {
4970 		SCSI_HBA_LOG((_LOG(3), self, probe, "==> devinfo"));
4971 
4972 		/*
4973 		 * Enumerate under pHCI:
4974 		 *
4975 		 * Create dynamic SID dchild node. No attempt is made to
4976 		 * transfer information (except the addressing and identity
4977 		 * information) from the probe node to the dynamic node since
4978 		 * there may be HBA specific side effects that the framework
4979 		 * does not known how to transfer.
4980 		 */
4981 		ndi_devi_alloc_sleep(self, nname,
4982 		    (se == SE_HP) ? DEVI_SID_HP_NODEID : DEVI_SID_NODEID,
4983 		    &dchild);
4984 		ASSERT(dchild);
4985 
4986 		/*
4987 		 * Decorate new node with addressing properties (via
4988 		 * scsi_hba_ua_set()), compatible, identity information, and
4989 		 * class.
4990 		 */
4991 		if ((scsi_hba_ua_set(addr, dchild, NULL) == 0) ||
4992 		    (ndi_prop_update_string_array(DDI_DEV_T_NONE, dchild,
4993 		    "compatible", compat, ncompat) != DDI_PROP_SUCCESS) ||
4994 		    (inq80 && (ndi_prop_update_byte_array(DDI_DEV_T_NONE,
4995 		    dchild, "inquiry-page-80", inq80, inq80len) !=
4996 		    DDI_PROP_SUCCESS)) ||
4997 		    (inq83 && (ndi_prop_update_byte_array(DDI_DEV_T_NONE,
4998 		    dchild, "inquiry-page-83", inq83, inq83len) !=
4999 		    DDI_PROP_SUCCESS)) ||
5000 		    (ndi_prop_update_string(DDI_DEV_T_NONE, dchild,
5001 		    "class", "scsi") != DDI_PROP_SUCCESS)) {
5002 			SCSI_HBA_LOG((_LOG(2), self, NULL,
5003 			    "@%s failed devinfo decoration", addr));
5004 			(void) scsi_hba_remove_node(dchild);
5005 			dchild = NULL;
5006 			goto out;
5007 		}
5008 
5009 		/* Bind the driver */
5010 		if (ndi_devi_bind_driver(dchild, 0) != NDI_SUCCESS) {
5011 			/* need to bind in order to register a devid */
5012 			SCSI_HBA_LOG((_LOGCFG, NULL, dchild,
5013 			    "devinfo @%s created, no driver-> "
5014 			    "no devid_register", addr));
5015 			goto out;
5016 		}
5017 
5018 		/* Register devid */
5019 		if (have_devid) {
5020 			if (ddi_devid_register(dchild, devid) == DDI_FAILURE)
5021 				SCSI_HBA_LOG((_LOG(1), NULL, dchild,
5022 				    "devinfo @%s created, devid failed", addr));
5023 			else
5024 				SCSI_HBA_LOG((_LOG(2), NULL, dchild,
5025 				    "devinfo @%s created devid", addr));
5026 		} else
5027 			SCSI_HBA_LOG((_LOG(2), NULL, dchild,
5028 			    "devinfo @%s created, no devid", addr));
5029 	} else {
5030 		/*
5031 		 * Enumerate under vHCI:
5032 		 *
5033 		 * Create a pathinfo pchild node.
5034 		 */
5035 		SCSI_HBA_LOG((_LOG(3), self, probe, "==>pathinfo"));
5036 
5037 		if (mdi_pi_alloc_compatible(self, nname, guid, addr, compat,
5038 		    ncompat, 0, &pchild) != MDI_SUCCESS) {
5039 			SCSI_HBA_LOG((_LOG(2), self, probe,
5040 			    "pathinfo alloc failed"));
5041 			goto out;
5042 		}
5043 		ASSERT(pchild);
5044 
5045 		/*
5046 		 * Decorate new node with addressing properties via
5047 		 * scsi_hba_ua_set().
5048 		 */
5049 		if (scsi_hba_ua_set(addr, NULL, pchild) == 0) {
5050 			SCSI_HBA_LOG((_LOG(1), self, NULL,
5051 			    "%s pathinfo decoration failed",
5052 			    mdi_pi_spathname(pchild)));
5053 
5054 			/* For hotplug, path exists even on failure. */
5055 			if (se != SE_HP)
5056 				(void) mdi_pi_free(pchild, 0);
5057 			goto out;
5058 		}
5059 	}
5060 
5061 	/* free the node name and compatible information */
5062 out:	if (have_devid)
5063 		ddi_devid_free(devid);
5064 	if (guid)
5065 		ddi_devid_free_guid(guid);
5066 	if (compat)
5067 		scsi_hba_ident_nodename_compatible_free(nname, dname, compat);
5068 	if (inq80)
5069 		ddi_prop_free(inq80);
5070 	if (inq83)
5071 		ddi_prop_free(inq83);
5072 	if (binding_set)
5073 		ddi_prop_free(binding_set);
5074 
5075 	/* return child_type results */
5076 	ASSERT(!(dchild && pchild));
5077 	if (dchild) {
5078 		*dchildp = dchild;
5079 		*pchildp = NULL;
5080 		return (CHILD_TYPE_DEVINFO);
5081 	}
5082 	if (pchild) {
5083 		*dchildp = NULL;
5084 		*pchildp = pchild;
5085 		return (CHILD_TYPE_PATHINFO);
5086 	}
5087 	return (CHILD_TYPE_NONE);
5088 }
5089 
5090 /*
5091  * Call scsi_device_createchild and then initchild the new node.
5092  */
5093 static dev_info_t *
5094 scsi_device_configchild(dev_info_t *self, char *addr, scsi_enum_t se,
5095     struct scsi_device *sdprobe, int *circp, int *ppi)
5096 {
5097 	int		child_type;
5098 	dev_info_t	*dchild;
5099 	mdi_pathinfo_t	*pchild;
5100 	dev_info_t	*child;
5101 	int		rval;
5102 
5103 	ASSERT(self && addr && *addr && DEVI_BUSY_OWNED(self));
5104 	if (ppi)
5105 		*ppi = 0;
5106 
5107 	child_type = scsi_device_createchild(self, addr, se, sdprobe,
5108 	    &dchild, &pchild);
5109 
5110 	/*
5111 	 * Prevent multiple initialized (tran_tgt_init) nodes associated with
5112 	 * the same @addr at the same time by calling tran_tgt_free() on the
5113 	 * probe node prior to promotion of the 'real' node.  After the call
5114 	 * to scsi_hba_barrier_tran_tgt_free(), the HBA no longer has any
5115 	 * probe node context.
5116 	 */
5117 	scsi_hba_barrier_tran_tgt_free(sdprobe->sd_dev);
5118 
5119 	switch (child_type) {
5120 	case CHILD_TYPE_NONE:
5121 		child = NULL;
5122 		break;
5123 
5124 	case CHILD_TYPE_PATHINFO:
5125 		/*
5126 		 * Online pathinfo: Hold the path and exit the pHCI while
5127 		 * calling mdi_pi_online() to avoid deadlock with power
5128 		 * management of pHCI.
5129 		 */
5130 		ASSERT(MDI_PHCI(self));
5131 		mdi_hold_path(pchild);
5132 		scsi_hba_devi_exit_phci(self, *circp);
5133 
5134 		rval = mdi_pi_online(pchild, 0);
5135 
5136 		scsi_hba_devi_enter_phci(self, circp);
5137 		mdi_rele_path(pchild);
5138 
5139 		if (rval != MDI_SUCCESS) {
5140 			SCSI_HBA_LOG((_LOG(2), self, NULL,
5141 			    "%s pathinfo online failed",
5142 			    mdi_pi_spathname(pchild)));
5143 
5144 			/* For hotplug, path exists even when online fails */
5145 			if (se != SE_HP)
5146 				(void) mdi_pi_free(pchild, 0);
5147 			return (NULL);
5148 		}
5149 
5150 		/*
5151 		 * Return the path_instance of the pathinfo node.
5152 		 *
5153 		 * NOTE: We assume that sd_inq is not path-specific.
5154 		 */
5155 		if (ppi)
5156 			*ppi = mdi_pi_get_path_instance(pchild);
5157 
5158 
5159 		/*
5160 		 * Fallthrough into CHILD_TYPE_DEVINFO code to promote
5161 		 * the 'client' devinfo node as a dchild.
5162 		 */
5163 		dchild = mdi_pi_get_client(pchild);
5164 		SCSI_HBA_LOG((_LOG(4), NULL, dchild,
5165 		    "pathinfo online successful"));
5166 		/* FALLTHROUGH */
5167 
5168 	case CHILD_TYPE_DEVINFO:
5169 		/*
5170 		 * For now, we ndi_devi_online() the child because some other
5171 		 * parts of the IO framework, like degenerate devid code,
5172 		 * depend on bus_config driving nodes to DS_ATTACHED. At some
5173 		 * point in the future, to keep things light-weight, we would
5174 		 * like to change the ndi_devi_online call below to be
5175 		 *
5176 		 *	if (ddi_initchild(self, dchild) != DDI_SUCCESS)
5177 		 *
5178 		 * This would promote the node so that framework code could
5179 		 * find the child with an @addr search, but does not incur
5180 		 * attach(9E) overhead for BUS_CONFIG_ALL cases where the
5181 		 * framework is not interested in attach of the node.
5182 		 *
5183 		 * NOTE: If the addr specified has incorrect syntax (busconfig
5184 		 * one of bogus /devices path) then call below can fail.
5185 		 */
5186 		if (ndi_devi_online(dchild, 0) != NDI_SUCCESS) {
5187 			SCSI_HBA_LOG((_LOG(2), NULL, dchild,
5188 			    "devinfo online failed"));
5189 
5190 			/* failed online does not remove the node */
5191 			(void) scsi_hba_remove_node(dchild);
5192 			return (NULL);
5193 		}
5194 		SCSI_HBA_LOG((_LOG(4), NULL, dchild,
5195 		    "devinfo initchild successful"));
5196 		child = dchild;
5197 		break;
5198 	}
5199 	return (child);
5200 }
5201 
5202 void
5203 scsi_hba_pkt_comp(struct scsi_pkt *pkt)
5204 {
5205 	scsi_hba_tran_t	*tran;
5206 	uint8_t		*sensep;
5207 
5208 	ASSERT(pkt);
5209 	if (pkt->pkt_comp == NULL)
5210 		return;
5211 
5212 	/*
5213 	 * For HBA drivers that implement tran_setup_pkt(9E), if we are
5214 	 * completing a 'consistent' mode DMA operation then we must
5215 	 * perform dma_sync prior to calling pkt_comp to ensure that
5216 	 * the target driver sees the correct data in memory.
5217 	 */
5218 	ASSERT((pkt->pkt_flags & FLAG_NOINTR) == 0);
5219 	if (((pkt->pkt_dma_flags & DDI_DMA_CONSISTENT) &&
5220 	    (pkt->pkt_dma_flags & DDI_DMA_READ)) &&
5221 	    ((P_TO_TRAN(pkt)->tran_setup_pkt) != NULL)) {
5222 		scsi_sync_pkt(pkt);
5223 	}
5224 
5225 	/*
5226 	 * If the HBA driver is using SCSAv3 scsi_hba_tgtmap_create enumeration
5227 	 * then we detect the special ASC/ASCQ completion codes that indicate
5228 	 * that the lun configuration of a target has changed. Since we need to
5229 	 * be determine scsi_device given scsi_address enbedded in
5230 	 * scsi_pkt (via scsi_address_device(9F)), we also require use of
5231 	 * SCSI_HBA_ADDR_COMPLEX.
5232 	 */
5233 	tran = pkt->pkt_address.a_hba_tran;
5234 	ASSERT(tran);
5235 	if ((tran->tran_tgtmap == NULL) ||
5236 	    !(tran->tran_hba_flags & SCSI_HBA_ADDR_COMPLEX))
5237 		goto comp;		/* not using tgtmap */
5238 
5239 	/*
5240 	 * Check for lun-change notification and queue the scsi_pkt for
5241 	 * lunchg1 processing. The 'pkt_comp' call to the target driver
5242 	 * is part of lunchg1 processing.
5243 	 */
5244 	if ((pkt->pkt_reason == CMD_CMPLT) &&
5245 	    (((*pkt->pkt_scbp) & STATUS_MASK) == STATUS_CHECK) &&
5246 	    (pkt->pkt_state & STATE_ARQ_DONE)) {
5247 		sensep = (uint8_t *)&(((struct scsi_arq_status *)(uintptr_t)
5248 		    (pkt->pkt_scbp))->sts_sensedata);
5249 		if (((scsi_sense_key(sensep) == KEY_UNIT_ATTENTION) &&
5250 		    (scsi_sense_asc(sensep) == 0x3f) &&
5251 		    (scsi_sense_ascq(sensep) == 0x0e)) ||
5252 
5253 		    ((scsi_sense_key(sensep) == KEY_UNIT_ATTENTION) &&
5254 		    (scsi_sense_asc(sensep) == 0x25) &&
5255 		    (scsi_sense_ascq(sensep) == 0x00))) {
5256 			/*
5257 			 * The host adaptor is done with the packet, we use
5258 			 * pkt_stmp stage-temporary to link the packet for
5259 			 * lunchg1 processing.
5260 			 *
5261 			 * NOTE: pkt_ha_private is not available since its use
5262 			 * extends to tran_teardown_pkt.
5263 			 */
5264 			mutex_enter(&scsi_lunchg1_mutex);
5265 			pkt->pkt_stmp = scsi_lunchg1_list;
5266 			scsi_lunchg1_list = pkt;
5267 			if (pkt->pkt_stmp == NULL)
5268 				(void) cv_signal(&scsi_lunchg1_cv);
5269 			mutex_exit(&scsi_lunchg1_mutex);
5270 			return;
5271 		}
5272 	}
5273 
5274 comp:	(*pkt->pkt_comp)(pkt);
5275 }
5276 
5277 /*
5278  * return 1 if the specified node is a barrier/probe node
5279  */
5280 static int
5281 scsi_hba_devi_is_barrier(dev_info_t *probe)
5282 {
5283 	if (probe && (strcmp(ddi_node_name(probe), "probe") == 0))
5284 		return (1);
5285 	return (0);
5286 }
5287 
5288 /*
5289  * A host adapter driver is easier to write if we prevent multiple initialized
5290  * (tran_tgt_init) scsi_device structures to the same unit-address at the same
5291  * time.  We prevent this from occurring all the time during the barrier/probe
5292  * node to real child hand-off by calling scsi_hba_barrier_tran_tgt_free
5293  * on the probe node prior to ddi_inichild of the 'real' node.  As part of
5294  * this early tran_tgt_free implementation, we must also call this function
5295  * as we put a probe node on the scsi_hba_barrier_list.
5296  */
5297 static void
5298 scsi_hba_barrier_tran_tgt_free(dev_info_t *probe)
5299 {
5300 	struct scsi_device	*sdprobe;
5301 	dev_info_t		*self;
5302 	scsi_hba_tran_t		*tran;
5303 
5304 	ASSERT(probe && scsi_hba_devi_is_barrier(probe));
5305 
5306 	/* Return if we never called tran_tgt_init(9E). */
5307 	if (i_ddi_node_state(probe) < DS_INITIALIZED)
5308 		return;
5309 
5310 	sdprobe = ddi_get_driver_private(probe);
5311 	self = ddi_get_parent(probe);
5312 	ASSERT(sdprobe && self);
5313 	tran = ddi_get_driver_private(self);
5314 	ASSERT(tran);
5315 
5316 	if (tran->tran_tgt_free) {
5317 		/*
5318 		 * To correctly support TRAN_CLONE, we need to use the same
5319 		 * cloned scsi_hba_tran(9S) structure for both tran_tgt_init(9E)
5320 		 * and tran_tgt_free(9E).
5321 		 */
5322 		if (tran->tran_hba_flags & SCSI_HBA_TRAN_CLONE)
5323 			tran = sdprobe->sd_address.a_hba_tran;
5324 		SCSI_HBA_LOG((_LOG(4), NULL, probe, "tran_tgt_free EARLY"));
5325 
5326 		(*tran->tran_tgt_free) (self, probe, tran, sdprobe);
5327 	}
5328 }
5329 
5330 /*
5331  * Add an entry to the list of barrier nodes to be asynchronously deleted by
5332  * the scsi_hba_barrier_daemon after the specified timeout. Nodes on
5333  * the barrier list are used to implement the bus_config probe cache
5334  * of non-existent devices. The nodes are at DS_INITIALIZED, so their
5335  * @addr is established for searching. Since devi_ref of a DS_INITIALIZED
5336  * node will *not* prevent demotion, demotion is prevented by setting
5337  * sd_uninit_prevent. Devinfo snapshots attempt to attach probe cache
5338  * nodes, and on failure attempt to demote the node (without the participation
5339  * of bus_unconfig) to DS_BOUND - this demotion is prevented via
5340  * sd_uninit_prevent causing any attempted DDI_CTLOPS_UNINITCHILD to fail.
5341  * Probe nodes are bound to nulldriver. The list is sorted by
5342  * expiration time.
5343  *
5344  * NOTE: If we drove a probe node to DS_ATTACHED, we could use ndi_hold_devi()
5345  * to prevent demotion (instead of sd_uninit_prevent).
5346  */
5347 static void
5348 scsi_hba_barrier_add(dev_info_t *probe, int seconds)
5349 {
5350 	struct scsi_hba_barrier	*nb;
5351 	struct scsi_hba_barrier	*b;
5352 	struct scsi_hba_barrier	**bp;
5353 	clock_t			endtime;
5354 
5355 	ASSERT(scsi_hba_devi_is_barrier(probe));
5356 
5357 	/* HBA is no longer responsible for nodes on the barrier list. */
5358 	scsi_hba_barrier_tran_tgt_free(probe);
5359 
5360 	nb = kmem_alloc(sizeof (struct scsi_hba_barrier), KM_SLEEP);
5361 	mutex_enter(&scsi_hba_barrier_mutex);
5362 	endtime = ddi_get_lbolt() + drv_usectohz(seconds * MICROSEC);
5363 	for (bp = &scsi_hba_barrier_list; (b = *bp) != NULL;
5364 	    bp = &b->barrier_next)
5365 		if (b->barrier_endtime > endtime)
5366 			break;
5367 	nb->barrier_next = *bp;
5368 	nb->barrier_endtime = endtime;
5369 	nb->barrier_probe = probe;
5370 	*bp = nb;
5371 	if (bp == &scsi_hba_barrier_list)
5372 		(void) cv_signal(&scsi_hba_barrier_cv);
5373 	mutex_exit(&scsi_hba_barrier_mutex);
5374 }
5375 
5376 /*
5377  * Attempt to remove devinfo node node, return 1 if removed. We don't
5378  * don't try to remove barrier nodes that have sd_uninit_prevent set
5379  * (even though they should fail device_uninitchild).
5380  */
5381 static int
5382 scsi_hba_remove_node(dev_info_t *child)
5383 {
5384 	dev_info_t		*self = ddi_get_parent(child);
5385 	struct scsi_device	*sd;
5386 	int			circ;
5387 	int			remove = 1;
5388 	int			ret = 0;
5389 	char			na[SCSI_MAXNAMELEN];
5390 
5391 	scsi_hba_devi_enter(self, &circ);
5392 
5393 	/* Honor sd_uninit_prevent on barrier nodes */
5394 	if (scsi_hba_devi_is_barrier(child)) {
5395 		sd = ddi_get_driver_private(child);
5396 		if (sd && sd->sd_uninit_prevent)
5397 			remove = 0;
5398 	}
5399 
5400 	if (remove) {
5401 		(void) ddi_deviname(child, na);
5402 		if (ddi_remove_child(child, 0) != DDI_SUCCESS) {
5403 			SCSI_HBA_LOG((_LOG(2), NULL, child,
5404 			    "remove_node failed"));
5405 		} else {
5406 			child = NULL;		/* child is gone */
5407 			SCSI_HBA_LOG((_LOG(4), self, NULL,
5408 			    "remove_node removed %s", *na ? &na[1] : na));
5409 			ret = 1;
5410 		}
5411 	} else {
5412 		SCSI_HBA_LOG((_LOG(4), NULL, child, "remove_node prevented"));
5413 	}
5414 	scsi_hba_devi_exit(self, circ);
5415 	return (ret);
5416 }
5417 
5418 /*
5419  * The asynchronous barrier deletion daemon. Waits for a barrier timeout
5420  * to expire, then deletes the barrier (removes it as a child).
5421  */
5422 /*ARGSUSED*/
5423 static void
5424 scsi_hba_barrier_daemon(void *arg)
5425 {
5426 	struct scsi_hba_barrier	*b;
5427 	dev_info_t		*probe;
5428 	callb_cpr_t		cprinfo;
5429 	int			circ;
5430 	dev_info_t		*self;
5431 
5432 	CALLB_CPR_INIT(&cprinfo, &scsi_hba_barrier_mutex,
5433 	    callb_generic_cpr, "scsi_hba_barrier_daemon");
5434 again:	mutex_enter(&scsi_hba_barrier_mutex);
5435 	for (;;) {
5436 		b = scsi_hba_barrier_list;
5437 		if (b == NULL) {
5438 			/* all barriers expired, wait for barrier_add */
5439 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
5440 			(void) cv_wait(&scsi_hba_barrier_cv,
5441 			    &scsi_hba_barrier_mutex);
5442 			CALLB_CPR_SAFE_END(&cprinfo, &scsi_hba_barrier_mutex);
5443 		} else {
5444 			if (ddi_get_lbolt() >= b->barrier_endtime) {
5445 				/*
5446 				 * Drop and retry if ordering issue. Do this
5447 				 * before calling scsi_hba_remove_node() and
5448 				 * deadlocking.
5449 				 */
5450 				probe = b->barrier_probe;
5451 				self = ddi_get_parent(probe);
5452 				if (scsi_hba_devi_tryenter(self, &circ) == 0) {
5453 delay:					mutex_exit(&scsi_hba_barrier_mutex);
5454 					delay_random(5);
5455 					goto again;
5456 				}
5457 
5458 				/* process expired barrier */
5459 				if (!scsi_hba_remove_node(probe)) {
5460 					/* remove failed, delay and retry */
5461 					SCSI_HBA_LOG((_LOG(4), NULL, probe,
5462 					    "delay expire"));
5463 					scsi_hba_devi_exit(self, circ);
5464 					goto delay;
5465 				}
5466 				scsi_hba_barrier_list = b->barrier_next;
5467 				kmem_free(b, sizeof (struct scsi_hba_barrier));
5468 				scsi_hba_devi_exit(self, circ);
5469 			} else {
5470 				/* establish timeout for next barrier expire */
5471 				(void) cv_timedwait(&scsi_hba_barrier_cv,
5472 				    &scsi_hba_barrier_mutex,
5473 				    b->barrier_endtime);
5474 			}
5475 		}
5476 	}
5477 }
5478 
5479 /*
5480  * Remove all barriers associated with the specified HBA. This is called
5481  * from from the bus_unconfig implementation to remove probe nodes associated
5482  * with the specified HBA (self) so that probe nodes that have not expired
5483  * will not prevent DR of the HBA.
5484  */
5485 static void
5486 scsi_hba_barrier_purge(dev_info_t *self)
5487 {
5488 	struct scsi_hba_barrier	**bp;
5489 	struct scsi_hba_barrier	*b;
5490 
5491 	mutex_enter(&scsi_hba_barrier_mutex);
5492 	for (bp = &scsi_hba_barrier_list; (b = *bp) != NULL; ) {
5493 		if (ddi_get_parent(b->barrier_probe) == self) {
5494 			if (scsi_hba_remove_node(b->barrier_probe)) {
5495 				*bp = b->barrier_next;
5496 				kmem_free(b, sizeof (struct scsi_hba_barrier));
5497 			} else {
5498 				SCSI_HBA_LOG((_LOG(4), NULL, b->barrier_probe,
5499 				    "skip purge"));
5500 			}
5501 		} else
5502 			bp = &b->barrier_next;
5503 	}
5504 
5505 	mutex_exit(&scsi_hba_barrier_mutex);
5506 }
5507 
5508 /*
5509  * LUN-change processing daemons: processing occurs in two stages:
5510  *
5511  * Stage 1:	Daemon waits for a lunchg1 queued scsi_pkt, dequeues the pkt,
5512  *		forms the path, completes the scsi_pkt (pkt_comp), and
5513  *		queues the path for stage 2 processing. The use of stage 1
5514  *		avoids issues related to memory allocation in interrupt context
5515  *		(scsi_hba_pkt_comp()). We delay the pkt_comp completion until
5516  *		after lunchg1 processing forms the path for stage 2 - this is
5517  *		done to prevent the target driver from detaching until the
5518  *		path formation is complete (driver with outstanding commands
5519  *		should not detach).
5520  *
5521  * Stage 2:	Daemon waits for a lunchg2 queued request, dequeues the
5522  *		request, and opens the path using ldi_open_by_name(). The
5523  *		path opened uses a special "@taddr,*" unit address that will
5524  *		trigger lun enumeration in scsi_hba_bus_configone(). We
5525  *		trigger lun enumeration in stage 2 to avoid problems when
5526  *		initial ASC/ASCQ trigger occurs during discovery.
5527  */
5528 /*ARGSUSED*/
5529 static void
5530 scsi_lunchg1_daemon(void *arg)
5531 {
5532 	callb_cpr_t		cprinfo;
5533 	struct scsi_pkt		*pkt;
5534 	scsi_hba_tran_t		*tran;
5535 	dev_info_t		*self;
5536 	struct scsi_device	*sd;
5537 	char			*ua, *p;
5538 	char			taddr[SCSI_MAXNAMELEN];
5539 	char			path[MAXPATHLEN];
5540 	struct scsi_lunchg2	*lunchg2;
5541 
5542 	CALLB_CPR_INIT(&cprinfo, &scsi_lunchg1_mutex,
5543 	    callb_generic_cpr, "scsi_lunchg1_daemon");
5544 	mutex_enter(&scsi_lunchg1_mutex);
5545 	for (;;) {
5546 		pkt = scsi_lunchg1_list;
5547 		if (pkt == NULL) {
5548 			/* All lunchg1 processing requests serviced, wait. */
5549 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
5550 			(void) cv_wait(&scsi_lunchg1_cv,
5551 			    &scsi_lunchg1_mutex);
5552 			CALLB_CPR_SAFE_END(&cprinfo, &scsi_lunchg1_mutex);
5553 			continue;
5554 		}
5555 
5556 		/* Unlink and perform lunchg1 processing on pkt. */
5557 		scsi_lunchg1_list = pkt->pkt_stmp;
5558 
5559 		/* Determine initiator port (self) from the pkt_address. */
5560 		tran = pkt->pkt_address.a_hba_tran;
5561 		ASSERT(tran && tran->tran_tgtmap && tran->tran_iport_dip);
5562 		self = tran->tran_iport_dip;
5563 
5564 		/*
5565 		 * Determine scsi_devie from pkt_address (depends on
5566 		 * SCSI_HBA_ADDR_COMPLEX).
5567 		 */
5568 		sd = scsi_address_device(&(pkt->pkt_address));
5569 		ASSERT(sd);
5570 		if (sd == NULL) {
5571 			(*pkt->pkt_comp)(pkt);
5572 			continue;
5573 		}
5574 
5575 		/* Determine unit-address from scsi_device. */
5576 		ua = scsi_device_unit_address(sd);
5577 
5578 		/* Extract taddr from the unit-address. */
5579 		for (p = taddr; (*ua != ',') && (*ua != '\0'); )
5580 			*p++ = *ua++;
5581 		*p = '\0';			/* NULL terminate taddr */
5582 
5583 		/*
5584 		 * Form path using special "@taddr,*" notation to trigger
5585 		 * lun enumeration.
5586 		 */
5587 		(void) ddi_pathname(self, path);
5588 		(void) strcat(path, "/luns@");
5589 		(void) strcat(path, taddr);
5590 		(void) strcat(path, ",*");
5591 
5592 		/*
5593 		 * Now that we have the path, complete the pkt that
5594 		 * triggered lunchg1 processing.
5595 		 */
5596 		(*pkt->pkt_comp)(pkt);
5597 
5598 		/* Allocate element for stage2 processing queue. */
5599 		lunchg2 = kmem_alloc(sizeof (*lunchg2), KM_SLEEP);
5600 		lunchg2->lunchg2_path = strdup(path);
5601 
5602 		/* Queue and dispatch to stage 2. */
5603 		SCSI_HBA_LOG((_LOG(2), self, NULL,
5604 		    "lunchg stage1: queue %s", lunchg2->lunchg2_path));
5605 		mutex_enter(&scsi_lunchg2_mutex);
5606 		lunchg2->lunchg2_next = scsi_lunchg2_list;
5607 		scsi_lunchg2_list = lunchg2;
5608 		if (lunchg2->lunchg2_next == NULL)
5609 			(void) cv_signal(&scsi_lunchg2_cv);
5610 		mutex_exit(&scsi_lunchg2_mutex);
5611 	}
5612 }
5613 
5614 /*ARGSUSED*/
5615 static void
5616 scsi_lunchg2_daemon(void *arg)
5617 {
5618 	callb_cpr_t		cprinfo;
5619 	struct scsi_lunchg2	*lunchg2;
5620 	ldi_ident_t		li;
5621 	ldi_handle_t		lh;
5622 
5623 	CALLB_CPR_INIT(&cprinfo, &scsi_lunchg2_mutex,
5624 	    callb_generic_cpr, "scsi_lunchg2_daemon");
5625 
5626 	li = ldi_ident_from_anon();
5627 	mutex_enter(&scsi_lunchg2_mutex);
5628 	for (;;) {
5629 		lunchg2 = scsi_lunchg2_list;
5630 		if (lunchg2 == NULL) {
5631 			/* All lunchg2 processing requests serviced, wait. */
5632 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
5633 			(void) cv_wait(&scsi_lunchg2_cv,
5634 			    &scsi_lunchg2_mutex);
5635 			CALLB_CPR_SAFE_END(&cprinfo, &scsi_lunchg2_mutex);
5636 			continue;
5637 		}
5638 
5639 		/* Unlink and perform lunchg2 processing on pkt. */
5640 		scsi_lunchg2_list = lunchg2->lunchg2_next;
5641 
5642 		/*
5643 		 * Open and close the path to trigger lun enumeration.  We
5644 		 * don't expect the open to succeed, but we do expect code in
5645 		 * scsi_hba_bus_configone() to trigger lun enumeration.
5646 		 */
5647 		SCSI_HBA_LOG((_LOG(2), NULL, NULL,
5648 		    "lunchg stage2: open %s", lunchg2->lunchg2_path));
5649 		if (ldi_open_by_name(lunchg2->lunchg2_path,
5650 		    FREAD, kcred, &lh, li) == 0)
5651 			(void) ldi_close(lh, FREAD, kcred);
5652 
5653 		/* Free path and linked element. */
5654 		strfree(lunchg2->lunchg2_path);
5655 		kmem_free(lunchg2, sizeof (*lunchg2));
5656 	}
5657 }
5658 
5659 /*
5660  * Enumerate a child at the specified @addr. If a device exists @addr then
5661  * ensure that we have the appropriately named devinfo node for it. Name is
5662  * NULL in the bus_config_all case. This routine has no knowledge of the
5663  * format of an @addr string or associated addressing properties.
5664  *
5665  * The caller must guarantee that there is an open scsi_hba_devi_enter on the
5666  * parent. We return the scsi_device structure for the child device. This
5667  * scsi_device structure is valid until the caller scsi_hba_devi_exit the
5668  * parent. The caller can add do ndi_hold_devi of the child prior to the
5669  * scsi_hba_devi_exit to extend the validity of the child.
5670  *
5671  * In some cases the returned scsi_device structure may be used to drive
5672  * additional SCMD_REPORT_LUNS operations by bus_config_all callers.
5673  *
5674  * The first operation performed is to see if there is a dynamic SID nodes
5675  * already attached at the specified "name@addr". This is the fastpath
5676  * case for resolving a reference to a node that has already been created.
5677  * All other references are serialized for a given @addr prior to probing
5678  * to determine the type of device, if any, at the specified @addr.
5679  * If no device is present then NDI_FAILURE is returned. The fact that a
5680  * device does not exist may be determined via the barrier/probe cache,
5681  * minimizing the probes of non-existent devices.
5682  *
5683  * When there is a device present the dynamic SID node is created based on
5684  * the device found. If a driver.conf node exists for the same @addr it
5685  * will either merge into the dynamic SID node (if the SID node bound to
5686  * that driver), or exist independently. To prevent the actions of one driver
5687  * causing side effects in another, code prevents multiple SID nodes from
5688  * binding to the same "@addr" at the same time. There is autodetach code
5689  * to allow one device to be replaced with another at the same @addr for
5690  * slot addressed SCSI bus implementations (SPI). For compatibility with
5691  * legacy driver.conf behavior, the code does not prevent multiple driver.conf
5692  * nodes from attaching to the same @addr at the same time.
5693  *
5694  * This routine may have the side effect of creating nodes for devices other
5695  * than the one being sought. It is possible that there is a different type of
5696  * target device at that target/lun address than we were asking for. In that
5697  * It is the caller's responsibility to determine whether the device we found,
5698  * if any, at the specified address, is the one it really wanted.
5699  */
5700 static struct scsi_device *
5701 scsi_device_config(dev_info_t *self, char *name, char *addr, scsi_enum_t se,
5702     int *circp, int *ppi)
5703 {
5704 	dev_info_t		*child = NULL;
5705 	dev_info_t		*probe = NULL;
5706 	struct scsi_device	*sdchild;
5707 	struct scsi_device	*sdprobe;
5708 	dev_info_t		*dsearch;
5709 	mdi_pathinfo_t		*psearch;
5710 	major_t			major;
5711 	int			sp;
5712 	int			pi = 0;
5713 	int			wait_msg = scsi_hba_wait_msg;
5714 	int			chg;
5715 
5716 	ASSERT(self && addr && DEVI_BUSY_OWNED(self));
5717 
5718 	SCSI_HBA_LOG((_LOG(4), self, NULL, "%s@%s wanted",
5719 	    name ? name : "", addr));
5720 
5721 	/* playing with "probe" node name is dangerous */
5722 	if (name && (strcmp(name, "probe") == 0))
5723 		return (NULL);
5724 
5725 	/*
5726 	 * NOTE: use 'goto done;' or 'goto fail;'. There should only be one
5727 	 * 'return' statement from here to the end of the function - the one
5728 	 * on the last line of the function.
5729 	 */
5730 
5731 	/*
5732 	 * Fastpath: search to see if we are requesting a named SID node that
5733 	 * already exists (we already created) - probe node does not count.
5734 	 * scsi_hba_find_child() does not hold the returned devinfo node, but
5735 	 * this is OK since the caller has a scsi_hba_devi_enter on the
5736 	 * attached parent HBA (self). The caller is responsible for attaching
5737 	 * and placing a hold on the child (directly via ndi_hold_devi or
5738 	 * indirectly via ndi_busop_bus_config) before doing an
5739 	 * scsi_hba_devi_exit on the parent.
5740 	 *
5741 	 * NOTE: This fastpath prevents detecting a driver binding change
5742 	 * (autodetach) if the same nodename is used for old and new binding.
5743 	 */
5744 	/* first call is with init set */
5745 	(void) scsi_hba_find_child(self, name, addr,
5746 	    1, &dsearch, NULL, &pi);
5747 	if (dsearch && scsi_hba_dev_is_sid(dsearch) &&
5748 	    !scsi_hba_devi_is_barrier(dsearch)) {
5749 		SCSI_HBA_LOG((_LOG(4), NULL, dsearch,
5750 		    "%s@%s devinfo fastpath", name ? name : "", addr));
5751 		child = dsearch;
5752 		goto done;
5753 	}
5754 
5755 	/*
5756 	 * Create a barrier devinfo node used to "probe" the device with. We
5757 	 * need to drive this node to DS_INITIALIZED so that the
5758 	 * DDI_CTLOPS_INITCHILD has occurred, bringing the SCSA transport to
5759 	 * a state useable state for issuing our "probe" commands. We establish
5760 	 * this barrier node with a node name of "probe" and compatible
5761 	 * property of "scsiprobe". The compatible property must be associated
5762 	 * in /etc/driver_aliases with a scsi target driver available in the
5763 	 * root file system (sd).
5764 	 *
5765 	 * The "probe" that we perform on the barrier node, after it is
5766 	 * DS_INITIALIZED, is used to find the information needed to create a
5767 	 * dynamic devinfo (SID) node. This "probe" is separate from the
5768 	 * probe(9E) call associated with the transition of a node from
5769 	 * DS_INITIALIZED to DS_PROBED. The probe(9E) call that eventually
5770 	 * occurs against the created SID node should find ddi_dev_is_sid and
5771 	 * just return DDI_PROBE_DONTCARE.
5772 	 *
5773 	 * Trying to avoid the use of a barrier node is not a good idea
5774 	 * because we may have an HBA driver that uses generic bus_config
5775 	 * (this code) but implements its own DDI_CTLOPS_INITCHILD with side
5776 	 * effects that we can't duplicate (such as the ATA nexus driver).
5777 	 *
5778 	 * The probe/barrier node plays an integral part of the locking scheme.
5779 	 * The objective is to single thread probes of the same device (same
5780 	 * @addr) while allowing parallelism for probes of different devices
5781 	 * with the same parent. At this point we are serialized on our self.
5782 	 * For parallelism we will need to release our self. Prior to release
5783 	 * we construct a barrier for probes of the same device to serialize
5784 	 * against. The "probe@addr" node acts as this barrier. An entering
5785 	 * thread must wait until the probe node does not exist - it can then
5786 	 * create and link the probe node - dropping the HBA (self) lock after
5787 	 * the node is linked and visible (after ddi_initchild). A side effect
5788 	 * of this is that transports should not "go over the wire" (i.e. do
5789 	 * things that incur significant delays) until after tran_target_init.
5790 	 * This means that the first "over the wire" operation should occur
5791 	 * at tran_target_probe time - when things are running in parallel
5792 	 * again.
5793 	 *
5794 	 * If the probe node exists then another probe with the same @addr is
5795 	 * in progress, we must wait until there is no probe in progress
5796 	 * before proceeding, and when we proceed we must continue to hold the
5797 	 * HBA (self) until we have linked a new probe node as a barrier.
5798 	 *
5799 	 * When a device is found to *not* exist, its probe/barrier node may be
5800 	 * marked with DEVICE_REMOVED with node deletion scheduled for some
5801 	 * future time (seconds). This asynchronous deletion allows the
5802 	 * framework to detect repeated requests to the same non-existent
5803 	 * device and avoid overhead associated with contacting a non-existent
5804 	 * device again and again.
5805 	 */
5806 	for (;;) {
5807 		/*
5808 		 * Search for probe node - they should only exist as devinfo
5809 		 * nodes.
5810 		 */
5811 		(void) scsi_hba_find_child(self, "probe", addr,
5812 		    0, &probe, &psearch, NULL);
5813 		if (probe == NULL) {
5814 			if (psearch)
5815 				SCSI_HBA_LOG((_LOG(2), self,
5816 				    mdi_pi_get_client(psearch),
5817 				    "???? @%s 'probe' search found "
5818 				    "pathinfo: %p", addr, (void *)psearch));
5819 			break;
5820 		}
5821 
5822 		/*
5823 		 * The barrier node may cache the non-existence of a device
5824 		 * by leaving the barrier node in place (with
5825 		 * DEVI_DEVICE_REMOVED flag set ) for some amount of time after
5826 		 * the failure of a probe. This flag is used to fail
5827 		 * additional probes until the barrier probe node is deleted,
5828 		 * which will occur from a timeout some time after a failed
5829 		 * probe. The failed probe will use DEVI_SET_DEVICE_REMOVED
5830 		 * and schedule probe node deletion from a timeout. The callers
5831 		 * scsi_hba_devi_exit on the way out of the first failure will
5832 		 * do the cv_broadcast associated with the cv_wait below - this
5833 		 * handles threads that wait prior to DEVI_DEVICE_REMOVED being
5834 		 * set.
5835 		 */
5836 		if (DEVI_IS_DEVICE_REMOVED(probe)) {
5837 			SCSI_HBA_LOG((_LOG(3), NULL, probe,
5838 			    "detected probe DEVICE_REMOVED"));
5839 			probe = NULL;	/* deletion already scheduled */
5840 			goto fail;
5841 		}
5842 
5843 		/*
5844 		 * Drop the lock on the HBA (self) and wait until the probe in
5845 		 * progress has completed. A changes in the sibling list from
5846 		 * removing the probe node will cause cv_wait to return
5847 		 * (scsi_hba_devi_exit does the cv_broadcast).
5848 		 */
5849 		if (wait_msg) {
5850 			wait_msg--;
5851 			SCSI_HBA_LOG((_LOG(2), NULL, probe,
5852 			    "exists, probe already in progress: %s", wait_msg ?
5853 			    "waiting..." : "last msg, but still waiting..."));
5854 		}
5855 
5856 		/*
5857 		 * NOTE: we could avoid rare case of one second delay by
5858 		 * implementing scsi_hba_devi_exit_and_wait based on
5859 		 * ndi/mdi_devi_exit_and_wait (and consider switching devcfg.c
5860 		 * code to use these ndi/mdi interfaces too).
5861 		 */
5862 		scsi_hba_devi_exit(self, *circp);
5863 		mutex_enter(&DEVI(self)->devi_lock);
5864 		(void) cv_timedwait(&DEVI(self)->devi_cv,
5865 		    &DEVI(self)->devi_lock,
5866 		    ddi_get_lbolt() + drv_usectohz(MICROSEC));
5867 		mutex_exit(&DEVI(self)->devi_lock);
5868 		scsi_hba_devi_enter(self, circp);
5869 	}
5870 	ASSERT(probe == NULL);
5871 
5872 	/*
5873 	 * Search to see if we are requesting a SID node that already exists.
5874 	 * We hold the HBA (self) and there is not another probe in progress at
5875 	 * the same @addr. scsi_hba_find_child() does not hold the returned
5876 	 * devinfo node but this is OK since we hold the HBA (self).
5877 	 */
5878 	if (name) {
5879 		(void) scsi_hba_find_child(self, name, addr,
5880 		    1, &dsearch, NULL, &pi);
5881 		if (dsearch && scsi_hba_dev_is_sid(dsearch)) {
5882 			SCSI_HBA_LOG((_LOG(4), NULL, dsearch,
5883 			    "%s@%s probe devinfo fastpath",
5884 			    name ? name : "", addr));
5885 			child = dsearch;
5886 			goto done;
5887 		}
5888 	}
5889 
5890 	/*
5891 	 * We are looking for a SID node that does not exist or a driver.conf
5892 	 * node.
5893 	 *
5894 	 * To avoid probe side effects, before we probe the device at the
5895 	 * specified address we need to check to see if there is already an
5896 	 * initialized child "@addr".
5897 	 *
5898 	 * o If we find an initialized SID child and name is NULL or matches
5899 	 *   the name or the name of the attached driver then we return the
5900 	 *   existing node.
5901 	 *
5902 	 * o If we find a non-matching SID node, we will attempt to autodetach
5903 	 *   and remove the node in preference to our new node.
5904 	 *
5905 	 * o If SID node found does not match and can't be autodetached, we
5906 	 *   fail: we only allow one SID node at an address.
5907 	 *
5908 	 * NOTE: This code depends on SID nodes showing up prior to
5909 	 * driver.conf nodes in the sibling list.
5910 	 */
5911 	for (;;) {
5912 		/* first NULL name call is with init set */
5913 		(void) scsi_hba_find_child(self, NULL, addr,
5914 		    1, &dsearch, NULL, &pi);
5915 		if (dsearch == NULL)
5916 			break;
5917 		ASSERT(!scsi_hba_devi_is_barrier(dsearch));
5918 
5919 		/*
5920 		 * To detect changes in driver binding that should attempt
5921 		 * autodetach we determine the major number of the driver
5922 		 * that should currently be associated with the device based
5923 		 * on the compatible property.
5924 		 */
5925 		major = DDI_MAJOR_T_NONE;
5926 		if (scsi_hba_dev_is_sid(dsearch))
5927 			major = ddi_compatible_driver_major(dsearch, NULL);
5928 		if ((major == DDI_MAJOR_T_NONE) && (name == NULL))
5929 			major = ddi_driver_major(dsearch);
5930 
5931 		if ((scsi_hba_dev_is_sid(dsearch) ||
5932 		    (i_ddi_node_state(dsearch) >= DS_INITIALIZED)) &&
5933 		    ((name == NULL) ||
5934 		    (strcmp(ddi_node_name(dsearch), name) == 0) ||
5935 		    (strcmp(ddi_driver_name(dsearch), name) == 0)) &&
5936 		    (major == ddi_driver_major(dsearch))) {
5937 			SCSI_HBA_LOG((_LOG(3), NULL, dsearch,
5938 			    "already attached @addr"));
5939 			child = dsearch;
5940 			goto done;
5941 		}
5942 
5943 		if (!scsi_hba_dev_is_sid(dsearch))
5944 			break;			/* driver.conf node */
5945 
5946 		/*
5947 		 * Implement autodetach of SID node for situations like a
5948 		 * previously "scsinodev" LUN0 coming into existence (or a
5949 		 * disk/tape on an SPI transport at same addr but never both
5950 		 * powered on at the same time). Try to autodetach the existing
5951 		 * SID node @addr. If that works, search again - otherwise fail.
5952 		 */
5953 		SCSI_HBA_LOG((_LOG(2), NULL, dsearch,
5954 		    "looking for %s@%s: SID @addr exists, autodetach",
5955 		    name ? name : "", addr));
5956 		if (!scsi_hba_remove_node(dsearch)) {
5957 			SCSI_HBA_LOG((_LOG(2), NULL, dsearch,
5958 			    "autodetach @%s failed: fail %s@%s",
5959 			    addr, name ? name : "", addr));
5960 			goto fail;
5961 		}
5962 		SCSI_HBA_LOG((_LOG(2), self, NULL, "autodetach @%s OK", addr));
5963 	}
5964 
5965 	/*
5966 	 * We will be creating a new SID node, allocate probe node
5967 	 * used to find out information about the device located @addr.
5968 	 * The probe node also acts as a barrier against additional
5969 	 * configuration at the same address, and in the case of non-existent
5970 	 * devices it will (for some amount of time) avoid re-learning that
5971 	 * the device does not exist on every reference. Once the probe
5972 	 * node is DS_LINKED we can drop the HBA (self).
5973 	 *
5974 	 * The probe node is allocated as a hidden node so that it does not
5975 	 * show up in devinfo snapshots.
5976 	 */
5977 	ndi_devi_alloc_sleep(self, "probe",
5978 	    (se == SE_HP) ? DEVI_SID_HP_HIDDEN_NODEID : DEVI_SID_HIDDEN_NODEID,
5979 	    &probe);
5980 	ASSERT(probe);
5981 
5982 	/*
5983 	 * Decorate the probe node with the property representation of @addr
5984 	 * unit-address string prior to initchild so that initchild can
5985 	 * construct the name of the node from properties and tran_tgt_init
5986 	 * implementation can determine what LUN is being referenced.
5987 	 *
5988 	 * If the addr specified has incorrect syntax (busconfig one of bogus
5989 	 * /devices path) then scsi_hba_ua_set can fail.  If the address
5990 	 * is not understood by the SCSA HBA driver then this operation will
5991 	 * work, but tran_tgt_init may still fail (for example the HBA
5992 	 * driver may not support secondary functions).
5993 	 */
5994 	if (scsi_hba_ua_set(addr, probe, NULL) == 0) {
5995 		SCSI_HBA_LOG((_LOG(2), NULL, probe,
5996 		    "@%s failed scsi_hba_ua_set", addr));
5997 		goto fail;
5998 	}
5999 
6000 	/*
6001 	 * Set the class property to "scsi". This is sufficient to distinguish
6002 	 * the node for HBAs that have multiple classes of children (like uata
6003 	 * - which has "dada" class for ATA children and "scsi" class for
6004 	 * ATAPI children) and may not use our scsi_busctl_initchild()
6005 	 * implementation. We also add a "compatible" property of "scsiprobe"
6006 	 * to select the probe driver.
6007 	 */
6008 	if ((ndi_prop_update_string(DDI_DEV_T_NONE, probe,
6009 	    "class", "scsi") != DDI_PROP_SUCCESS) ||
6010 	    (ndi_prop_update_string_array(DDI_DEV_T_NONE, probe,
6011 	    "compatible", &compatible_probe, 1) != DDI_PROP_SUCCESS)) {
6012 		SCSI_HBA_LOG((_LOG(1), NULL, probe,
6013 		    "@%s failed node decoration", addr));
6014 		goto fail;
6015 	}
6016 
6017 	/*
6018 	 * Promote probe node to DS_INITIALIZED so that transport can be used
6019 	 * for scsi_probe. After this the node is linked and visible as a
6020 	 * barrier for serialization of other @addr operations.
6021 	 *
6022 	 * NOTE: If we attached the probe node, we could get rid of
6023 	 * uninit_prevent.
6024 	 */
6025 	if (ddi_initchild(self, probe) != DDI_SUCCESS) {
6026 		SCSI_HBA_LOG((_LOG(2), NULL, probe,
6027 		    "@%s failed initchild", addr));
6028 
6029 		/* probe node will be removed in fail exit path */
6030 		goto fail;
6031 	}
6032 
6033 	/* get the scsi_device structure of the probe node */
6034 	sdprobe = ddi_get_driver_private(probe);
6035 	ASSERT(sdprobe);
6036 
6037 	/*
6038 	 * Do scsi_probe. The probe node is linked and visible as a barrier.
6039 	 * We prevent uninitialization of the probe node and drop our HBA (self)
6040 	 * while we run scsi_probe() of this "@addr". This allows the framework
6041 	 * to support multiple scsi_probes for different devices attached to
6042 	 * the same HBA (self) in parallel. We prevent node demotion of the
6043 	 * probe node from DS_INITIALIZED by setting sd_uninit_prevent. The
6044 	 * probe node can not be successfully demoted below DS_INITIALIZED
6045 	 * (scsi_busctl_uninitchild will fail) until we zero sd_uninit_prevent
6046 	 * as we are freeing the node via scsi_hba_remove_node(probe).
6047 	 */
6048 	sdprobe->sd_uninit_prevent++;
6049 	scsi_hba_devi_exit(self, *circp);
6050 	sp = scsi_probe(sdprobe, SLEEP_FUNC);
6051 
6052 	/* Introduce a small delay here to increase parallelism. */
6053 	delay_random(5);
6054 
6055 	if (sp == SCSIPROBE_EXISTS) {
6056 		/*
6057 		 * For a device that exists, while still running in parallel,
6058 		 * also get identity information from device. This is done
6059 		 * separate from scsi_probe/tran_tgt_probe/scsi_hba_probe
6060 		 * since the probe code path may still be used for HBAs
6061 		 * that don't use common bus_config services (we don't want
6062 		 * to expose that code path to a behavior change). This
6063 		 * operation is called 'identity' to avoid confusion with
6064 		 * deprecated identify(9E).
6065 		 *
6066 		 * Future: We may eventually want to allow HBA customization via
6067 		 * scsi_identity/tran_tgt_identity/scsi_device_identity, but for
6068 		 * now we just scsi_device_identity.
6069 		 *
6070 		 * The identity operation will establish additional properties
6071 		 * on the probe node related to device identity:
6072 		 *
6073 		 *	"inquiry-page-80"	byte array of SCSI page 80
6074 		 *	"inquiry-page-83"	byte array of SCSI page 83
6075 		 *
6076 		 * These properties will be used to generate a devid
6077 		 * (ddi_devid_scsi_encode) and guid - and to register
6078 		 * (ddi_devid_register) a devid for the device.
6079 		 *
6080 		 * If identify fails (non-zero return), the we had allocation
6081 		 * problems or the device returned inconsistent results then
6082 		 * we pretend that device does not exist.
6083 		 */
6084 		if (scsi_device_identity(sdprobe, SLEEP_FUNC)) {
6085 			if (ndi_dev_is_hotplug_node(probe))
6086 				SCSI_HBA_LOG((_LOG(WARN), NULL, probe,
6087 				    "enumeration failed during identity"));
6088 			else
6089 				SCSI_HBA_LOG((_LOG(2), NULL, probe,
6090 				    "enumeration failed during identity"));
6091 			sp = SCSIPROBE_FAILURE;
6092 		}
6093 
6094 		/*
6095 		 * Future: Is there anything more we can do here to help avoid
6096 		 * serialization on iport parent during scsi_device attach(9E)?
6097 		 */
6098 	}
6099 	scsi_hba_devi_enter(self, circp);
6100 	sdprobe->sd_uninit_prevent--;
6101 
6102 	if (sp != SCSIPROBE_EXISTS) {
6103 		if (ndi_dev_is_hotplug_node(probe))
6104 			SCSI_HBA_LOG((_LOG(WARN), NULL, probe,
6105 			    "enumeration failed during probe"));
6106 		else
6107 			SCSI_HBA_LOG((_LOG(2), NULL, probe,
6108 			    "enumeration failed durning probe"));
6109 
6110 		if (scsi_hba_barrier_timeout) {
6111 			/*
6112 			 * Target does not exist. Mark the barrier probe node
6113 			 * as DEVICE_REMOVED and schedule an asynchronous
6114 			 * deletion of the node in scsi_hba_barrier_timeout
6115 			 * seconds. We keep our hold on the probe node
6116 			 * until we are ready perform the asynchronous node
6117 			 * deletion.
6118 			 */
6119 			SCSI_HBA_LOG((_LOG(3), NULL, probe,
6120 			    "set probe DEVICE_REMOVED"));
6121 			mutex_enter(&DEVI(probe)->devi_lock);
6122 			DEVI_SET_DEVICE_REMOVED(probe);
6123 			mutex_exit(&DEVI(probe)->devi_lock);
6124 
6125 			scsi_hba_barrier_add(probe, scsi_hba_barrier_timeout);
6126 			probe = NULL;
6127 		}
6128 		goto fail;
6129 	}
6130 
6131 	/* Create the child node from the inquiry data in the probe node. */
6132 	if ((child = scsi_device_configchild(self, addr, se, sdprobe,
6133 	    circp, &pi)) == NULL) {
6134 		/*
6135 		 * This may fail because there was no driver binding identified
6136 		 * via driver_alias. We may still have a conf node.
6137 		 */
6138 		if (name) {
6139 			(void) scsi_hba_find_child(self, name, addr,
6140 			    0, &child, NULL, &pi);
6141 			if (child)
6142 				SCSI_HBA_LOG((_LOG(2), NULL, child,
6143 				    "using driver.conf driver binding"));
6144 		}
6145 		if (child == NULL) {
6146 			SCSI_HBA_LOG((_LOG(2), NULL, probe,
6147 			    "device not configured"));
6148 			goto fail;
6149 		}
6150 	}
6151 
6152 	/*
6153 	 * Transfer the inquiry data from the probe node to the child
6154 	 * SID node to avoid an extra scsi_probe. Callers depend on
6155 	 * established inquiry data for the returned scsi_device.
6156 	 */
6157 	sdchild = ddi_get_driver_private(child);
6158 	if (sdchild && (sdchild->sd_inq == NULL)) {
6159 		sdchild->sd_inq = sdprobe->sd_inq;
6160 		sdprobe->sd_inq = NULL;
6161 	}
6162 
6163 	/*
6164 	 * If we are doing a bus_configone and the node we created has the
6165 	 * wrong node and driver name then switch the return result to a
6166 	 * driver.conf node with the correct name - if such a node exists.
6167 	 */
6168 	if (name && (strcmp(ddi_node_name(child), name) != 0) &&
6169 	    (strcmp(ddi_driver_name(child), name) != 0)) {
6170 		(void) scsi_hba_find_child(self, name, addr,
6171 		    0, &dsearch, NULL, &pi);
6172 		if (dsearch == NULL) {
6173 			SCSI_HBA_LOG((_LOG(2), NULL, child,
6174 			    "wrong device configured %s@%s", name, addr));
6175 			/*
6176 			 * We can't remove when modrootloaded == 0 in case
6177 			 * boot-device a uses generic name and
6178 			 * scsi_hba_nodename_compatible_get() returned a
6179 			 * legacy binding-set driver oriented name.
6180 			 */
6181 			if (modrootloaded) {
6182 				(void) scsi_hba_remove_node(child);
6183 				child = NULL;
6184 				goto fail;
6185 			}
6186 		} else {
6187 			SCSI_HBA_LOG((_LOG(2), NULL, dsearch,
6188 			    "device configured, but switching to driver.conf"));
6189 			child = dsearch;
6190 		}
6191 	}
6192 
6193 	/* get the scsi_device structure from the node */
6194 	SCSI_HBA_LOG((_LOG(3), NULL, child, "device configured"));
6195 
6196 	if (child) {
6197 done:		ASSERT(child);
6198 		sdchild = ddi_get_driver_private(child);
6199 		ASSERT(sdchild);
6200 		/*
6201 		 * We may have ended up here after promotion of a previously
6202 		 * demoted node, where demotion deleted sd_inq data in
6203 		 * scsi_busctl_uninitchild.  We redo the scsi_probe() to
6204 		 * reestablish sd_inq.  We also want to redo the scsi_probe
6205 		 * for devices are currently device_isremove in order to
6206 		 * detect new device_insert.
6207 		 */
6208 		if ((sdchild->sd_inq == NULL) ||
6209 		    ndi_devi_device_isremoved(child)) {
6210 
6211 			/* hotplug_node can only be revived via hotplug. */
6212 			if ((se == SE_HP) || !ndi_dev_is_hotplug_node(child)) {
6213 				SCSI_HBA_LOG((_LOG(3), NULL, child,
6214 				    "scsi_probe() demoted devinfo"));
6215 
6216 				sp = scsi_probe(sdchild, SLEEP_FUNC);
6217 
6218 				if (sp == SCSIPROBE_EXISTS) {
6219 					ASSERT(sdchild->sd_inq);
6220 
6221 					/*
6222 					 * Devinfo child exists and we are
6223 					 * talking to the device, report
6224 					 * reinsert and note if this was a
6225 					 * new reinsert.
6226 					 */
6227 					chg = ndi_devi_device_insert(child);
6228 					SCSI_HBA_LOG((_LOGCFG, self, NULL,
6229 					    "devinfo %s@%s device_reinsert%s",
6230 					    name ? name : "", addr,
6231 					    chg ? "" : "ed already"));
6232 				} else {
6233 					if (se == SE_HP)
6234 						SCSI_HBA_LOG((_LOG(WARN),
6235 						    NULL, child,
6236 						    "enumeration failed during "
6237 						    "reprobe"));
6238 					else
6239 						SCSI_HBA_LOG((_LOG(2),
6240 						    NULL, child,
6241 						    "enumeration failed during "
6242 						    "reprobe"));
6243 
6244 					chg = ndi_devi_device_remove(child);
6245 					SCSI_HBA_LOG((_LOG(2), NULL, child,
6246 					    "%s device_remove%s",
6247 					    (sp > (sizeof (scsi_probe_ascii) /
6248 					    sizeof (scsi_probe_ascii[0]))) ?
6249 					    "UNKNOWN" : scsi_probe_ascii[sp],
6250 					    chg ? "" : "ed already"));
6251 
6252 					child = NULL;
6253 					sdchild = NULL;
6254 				}
6255 			} else {
6256 				SCSI_HBA_LOG((_LOG(2), NULL, child,
6257 				    "no reprobe"));
6258 
6259 				child = NULL;
6260 				sdchild = NULL;
6261 			}
6262 		}
6263 	} else {
6264 fail:		ASSERT(child == NULL);
6265 		sdchild = NULL;
6266 	}
6267 	if (probe) {
6268 		/*
6269 		 * Clean up probe node, destroying node if uninit_prevent
6270 		 * it is going to zero. Destroying the probe node (deleting
6271 		 * from the sibling list) will wake up any people waiting on
6272 		 * the probe node barrier.
6273 		 */
6274 		SCSI_HBA_LOG((_LOG(4), NULL, probe, "remove probe"));
6275 		if (!scsi_hba_remove_node(probe)) {
6276 			/*
6277 			 * Probe node removal should not fail, but if it
6278 			 * does we hand that responsibility over to the
6279 			 * async barrier deletion thread - other references
6280 			 * to the same unit-address can hang until the
6281 			 * probe node delete completes.
6282 			 */
6283 			SCSI_HBA_LOG((_LOG(4), NULL, probe,
6284 			    "remove probe failed, go async"));
6285 			scsi_hba_barrier_add(probe, 1);
6286 		}
6287 		probe = NULL;
6288 	}
6289 
6290 	/*
6291 	 * If we successfully resolved via a pathinfo node, we need to find
6292 	 * the pathinfo node and ensure that it is online (if possible). This
6293 	 * is done for the case where the device was open when
6294 	 * scsi_device_unconfig occurred, so mdi_pi_free did not occur. If the
6295 	 * device has now been reinserted, we want the path back online.
6296 	 * NOTE: This needs to occur after destruction of the probe node to
6297 	 * avoid ASSERT related to two nodes at the same unit-address.
6298 	 */
6299 	if (sdchild && pi && (probe == NULL)) {
6300 		ASSERT(MDI_PHCI(self));
6301 
6302 		(void) scsi_hba_find_child(self, NULL, addr,
6303 		    0, &dsearch, &psearch, NULL);
6304 		ASSERT((psearch == NULL) ||
6305 		    (mdi_pi_get_client(psearch) == child));
6306 
6307 		if (psearch && mdi_pi_device_isremoved(psearch)) {
6308 			/*
6309 			 * Verify that we can talk to the device, and if
6310 			 * so note if this is a new device_insert.
6311 			 *
6312 			 * NOTE: We depend on mdi_path_select(), when given
6313 			 * a specific path_instance, to select that path
6314 			 * even if the path is offline.
6315 			 *
6316 			 * NOTE: A Client node is not ndi_dev_is_hotplug_node().
6317 			 */
6318 			if (se == SE_HP) {
6319 				SCSI_HBA_LOG((_LOG(3), NULL, child,
6320 				    "%s scsi_probe() demoted pathinfo",
6321 				    mdi_pi_spathname(psearch)));
6322 
6323 				sp = scsi_hba_probe_pi(sdchild, SLEEP_FUNC, pi);
6324 
6325 				if (sp == SCSIPROBE_EXISTS) {
6326 					/*
6327 					 * Pathinfo child exists and we are
6328 					 * talking to the device, report
6329 					 * reinsert and note if this
6330 					 * was a new reinsert.
6331 					 */
6332 					chg = mdi_pi_device_insert(psearch);
6333 					SCSI_HBA_LOG((_LOGCFG, self, NULL,
6334 					    "pathinfo %s device_reinsert%s",
6335 					    mdi_pi_spathname(psearch),
6336 					    chg ? "" : "ed already"));
6337 
6338 					if (chg)
6339 						(void) mdi_pi_online(psearch,
6340 						    0);
6341 
6342 				} else {
6343 					SCSI_HBA_LOG((_LOG(WARN), NULL, child,
6344 					    "%s enumeration failed "
6345 					    "during reprobe",
6346 					    mdi_pi_spathname(psearch)));
6347 
6348 					child = NULL;
6349 					sdchild = NULL;
6350 				}
6351 
6352 			} else {
6353 				SCSI_HBA_LOG((_LOG(2), NULL, child,
6354 				    "%s no reprobe",
6355 				    mdi_pi_spathname(psearch)));
6356 
6357 				child = NULL;
6358 				sdchild = NULL;
6359 			}
6360 		}
6361 	}
6362 
6363 	/* If asked for path_instance, return it. */
6364 	if (ppi)
6365 		*ppi = pi;
6366 
6367 	return (sdchild);
6368 }
6369 
6370 static void
6371 scsi_device_unconfig(dev_info_t *self, char *name, char *addr, int *circp)
6372 {
6373 	dev_info_t		*child = NULL;
6374 	mdi_pathinfo_t		*path = NULL;
6375 	char			*spathname;
6376 	int			rval;
6377 
6378 	ASSERT(self && addr && DEVI_BUSY_OWNED(self));
6379 
6380 	/*
6381 	 * We have a catch-22. We may have a demoted node that we need to find
6382 	 * and offline/remove. To find the node it it isn't demoted, we
6383 	 * use scsi_hba_find_child. If it's demoted, we then use
6384 	 * ndi_devi_findchild_by_callback.
6385 	 */
6386 	(void) scsi_hba_find_child(self, name, addr, 0, &child, &path, NULL);
6387 
6388 	if ((child == NULL) && (path == NULL)) {
6389 		child = ndi_devi_findchild_by_callback(self, name, addr,
6390 		    scsi_busctl_ua);
6391 		if (child) {
6392 			SCSI_HBA_LOG((_LOGUNCFG, self, NULL,
6393 			    "devinfo %s@%s found by callback",
6394 			    name ? name : "", addr));
6395 			ASSERT(ndi_flavor_get(child) ==
6396 			    SCSA_FLAVOR_SCSI_DEVICE);
6397 			if (ndi_flavor_get(child) != SCSA_FLAVOR_SCSI_DEVICE) {
6398 				SCSI_HBA_LOG((_LOGUNCFG, self, NULL,
6399 				    "devinfo %s@%s not SCSI_DEVICE flavored",
6400 				    name ? name : "", addr));
6401 				child = NULL;
6402 			}
6403 		}
6404 	}
6405 
6406 	if (child) {
6407 		ASSERT(child && (path == NULL));
6408 
6409 		/* Don't unconfig probe nodes. */
6410 		if (scsi_hba_devi_is_barrier(child)) {
6411 			SCSI_HBA_LOG((_LOGUNCFG, self, NULL,
6412 			    "devinfo %s@%s is_barrier, skip",
6413 			    name ? name : "", addr));
6414 			return;
6415 		}
6416 
6417 		/* Attempt to offline/remove the devinfo node */
6418 		if (ndi_devi_offline(child,
6419 		    NDI_DEVFS_CLEAN | NDI_DEVI_REMOVE) == DDI_SUCCESS) {
6420 			SCSI_HBA_LOG((_LOGUNCFG, self, NULL,
6421 			    "devinfo %s@%s offlined and removed",
6422 			    name ? name : "", addr));
6423 		} else if (ndi_devi_device_remove(child)) {
6424 			/* Offline/remove failed, note new device_remove */
6425 			SCSI_HBA_LOG((_LOGUNCFG, self, NULL,
6426 			    "devinfo %s@%s offline failed, device_remove",
6427 			    name ? name : "", addr));
6428 		}
6429 	} else if (path) {
6430 		ASSERT(path && (child == NULL));
6431 
6432 		/*
6433 		 * Attempt to offline/remove the pathinfo node.
6434 		 *
6435 		 * NOTE: mdi_pi_offline of last path will fail if the
6436 		 * device is open (i.e. the client can't be offlined).
6437 		 *
6438 		 * NOTE: For mdi there is no REMOVE flag for mdi_pi_offline().
6439 		 * When mdi_pi_offline returns MDI_SUCCESS, we are responsible
6440 		 * for remove via mdi_pi_free().
6441 		 */
6442 		mdi_hold_path(path);
6443 		spathname = mdi_pi_spathname(path);	/* valid after free */
6444 		scsi_hba_devi_exit_phci(self, *circp);
6445 		rval = mdi_pi_offline(path, 0);
6446 		scsi_hba_devi_enter_phci(self, circp);
6447 		if (rval == MDI_SUCCESS) {
6448 			SCSI_HBA_LOG((_LOGUNCFG, self, NULL,
6449 			    "pathinfo %s offlined and removed", spathname));
6450 		} else if (mdi_pi_device_remove(path)) {
6451 			/* Offline/remove failed, note new device_remove */
6452 			SCSI_HBA_LOG((_LOGUNCFG, self, NULL,
6453 			    "pathinfo %s offline failed, "
6454 			    "device_remove", spathname));
6455 		}
6456 		mdi_rele_path(path);
6457 		if ((rval == MDI_SUCCESS) &&
6458 		    (mdi_pi_free(path, 0) != MDI_SUCCESS)) {	/* REMOVE */
6459 			SCSI_HBA_LOG((_LOGUNCFG, self, NULL,
6460 			    "pathinfo %s mdi_pi_free failed, "
6461 			    "device_remove", spathname));
6462 			(void) mdi_pi_device_remove(path);
6463 		}
6464 	} else {
6465 		ASSERT((path == NULL) && (child == NULL));
6466 
6467 		SCSI_HBA_LOG((_LOGUNCFG, self, NULL,
6468 		    "%s@%s not found", name ? name : "", addr));
6469 	}
6470 }
6471 
6472 /*
6473  * configure the device at the specified "@addr" address.
6474  */
6475 static struct scsi_device *
6476 scsi_hba_bus_configone_addr(dev_info_t *self, char *addr, scsi_enum_t se)
6477 {
6478 	int			circ;
6479 	struct scsi_device	*sd;
6480 
6481 	scsi_hba_devi_enter(self, &circ);
6482 	sd = scsi_device_config(self, NULL, addr, se, &circ, NULL);
6483 	scsi_hba_devi_exit(self, circ);
6484 	return (sd);
6485 }
6486 
6487 /*
6488  * unconfigure the device at the specified "@addr" address.
6489  */
6490 static void
6491 scsi_hba_bus_unconfigone_addr(dev_info_t *self, char *addr)
6492 {
6493 	int			circ;
6494 
6495 	scsi_hba_devi_enter(self, &circ);
6496 	(void) scsi_device_unconfig(self, NULL, addr, &circ);
6497 	scsi_hba_devi_exit(self, circ);
6498 }
6499 
6500 /*
6501  * The bus_config_all operations are multi-threaded for performance. A
6502  * separate thread per target and per LUN is used. The config handle is used
6503  * to coordinate all the threads at a given level and the config thread data
6504  * contains the required information for a specific thread to identify what it
6505  * is processing and the handle under which this is being processed.
6506  */
6507 
6508 /* multi-threaded config handle */
6509 struct	scsi_hba_mte_h {
6510 	dev_info_t		*h_self;	/* initiator port */
6511 	int			h_thr_count;
6512 	kmutex_t		h_lock;
6513 	kcondvar_t		h_cv;
6514 };
6515 
6516 /* target of 'self' config thread data */
6517 struct scsi_hba_mte_td {
6518 	struct scsi_hba_mte_h	*td_h;
6519 	char			*td_taddr;	/* target port */
6520 	int			td_mt;
6521 	scsi_enum_t		td_se;
6522 };
6523 
6524 /* Invoke callback on a vector of taddrs from multiple threads */
6525 static void
6526 scsi_hba_thread_taddrs(dev_info_t *self, char **taddrs, int mt,
6527     scsi_enum_t se, void (*callback)(void *arg))
6528 {
6529 	struct scsi_hba_mte_h	*h;	/* HBA header */
6530 	struct scsi_hba_mte_td	*td;	/* target data */
6531 	char			**taddr;
6532 
6533 	/* allocate and initialize the handle */
6534 	h = kmem_zalloc(sizeof (*h), KM_SLEEP);
6535 	mutex_init(&h->h_lock, NULL, MUTEX_DEFAULT, NULL);
6536 	cv_init(&h->h_cv, NULL, CV_DEFAULT, NULL);
6537 	h->h_self = self;
6538 
6539 	/* loop over all the targets */
6540 	for (taddr = taddrs; *taddr; taddr++) {
6541 		/* allocate a thread data structure for target */
6542 		td = kmem_alloc(sizeof (*td), KM_SLEEP);
6543 		td->td_h = h;
6544 		td->td_taddr = *taddr;
6545 		td->td_mt = mt;
6546 		td->td_se = se;
6547 
6548 		/* process the target */
6549 		mutex_enter(&h->h_lock);
6550 		h->h_thr_count++;
6551 		mutex_exit(&h->h_lock);
6552 
6553 		mt |= scsi_hba_log_mt_disable;
6554 		if (mt & SCSI_ENUMERATION_MT_LUN_DISABLE)
6555 			callback((void *)td);
6556 		else
6557 			(void) thread_create(NULL, 0, callback, (void *)td,
6558 			    0, &p0, TS_RUN, minclsyspri);
6559 	}
6560 
6561 	/* wait for all the target threads to complete */
6562 	mutex_enter(&h->h_lock);
6563 	while (h->h_thr_count > 0)
6564 		cv_wait(&h->h_cv, &h->h_lock);
6565 	mutex_exit(&h->h_lock);
6566 
6567 	/* free the handle */
6568 	cv_destroy(&h->h_cv);
6569 	mutex_destroy(&h->h_lock);
6570 	kmem_free(h, sizeof (*h));
6571 }
6572 
6573 
6574 /* lun/secondary function of lun0 config thread data */
6575 struct scsi_hba_mte_ld {
6576 	struct scsi_hba_mte_h	*ld_h;
6577 	char			*ld_taddr;	/* target port */
6578 	scsi_lun64_t		ld_lun64;	/* lun */
6579 	int			ld_sfunc;	/* secondary function */
6580 	scsi_enum_t		ld_se;
6581 };
6582 
6583 /*
6584  * Enumerate the LUNs and secondary functions of the specified target. The
6585  * target portion of the "@addr" is already represented as a string in the
6586  * thread data, we add a ",lun" representation to this and perform a
6587  * bus_configone byte of enumeration on that "@addr".
6588  */
6589 static void
6590 scsi_hba_enum_lsf_of_tgt_thr(void *arg)
6591 {
6592 	struct scsi_hba_mte_ld	*ld = (struct scsi_hba_mte_ld *)arg;
6593 	struct scsi_hba_mte_h	*h = ld->ld_h;
6594 	dev_info_t		*self = h->h_self;
6595 	char			addr[SCSI_MAXNAMELEN];
6596 
6597 	/* make string form of "@taddr,lun[,sfunc]" and see if it exists */
6598 	if (ld->ld_sfunc == -1)
6599 		(void) snprintf(addr, sizeof (addr),
6600 		    "%s,%" PRIx64, ld->ld_taddr, ld->ld_lun64);
6601 	else
6602 		(void) snprintf(addr, sizeof (addr),
6603 		    "%s,%" PRIx64 ",%x",
6604 		    ld->ld_taddr, ld->ld_lun64, ld->ld_sfunc);
6605 
6606 	/* configure device at that unit-address address */
6607 	(void) scsi_hba_bus_configone_addr(self, addr, ld->ld_se);
6608 
6609 	/* signal completion of this LUN thread to the target */
6610 	mutex_enter(&h->h_lock);
6611 	if (--h->h_thr_count == 0)
6612 		cv_broadcast(&h->h_cv);
6613 	mutex_exit(&h->h_lock);
6614 
6615 	/* free config thread data */
6616 	kmem_free(ld, sizeof (*ld));
6617 }
6618 
6619 /* Format of SCSI REPORT_LUNS report */
6620 typedef struct scsi_lunrpt {
6621 	uchar_t		lunrpt_len_msb;		/* # LUNs being reported */
6622 	uchar_t		lunrpt_len_mmsb;
6623 	uchar_t		lunrpt_len_mlsb;
6624 	uchar_t		lunrpt_len_lsb;
6625 	uchar_t		lunrpt_reserved[4];
6626 	scsi_lun_t	lunrpt_luns[1];		/* LUNs, variable size */
6627 } scsi_lunrpt_t;
6628 
6629 /*
6630  * scsi_device_reportluns()
6631  *
6632  * Callers of this routine should ensure that the 'sd0' scsi_device structure
6633  * and 'pi' path_instance specified are associated with a responding LUN0.
6634  * This should not be called for SCSI-1 devices.
6635  *
6636  * To get a LUN report, we must allocate a buffer. To know how big to make the
6637  * buffer, we must know the number of LUNs. To know the number of LUNs, we must
6638  * get a LUN report. We first issue a SCMD_REPORT_LUNS command using a
6639  * reasonably sized buffer that's big enough to report all LUNs for most
6640  * typical devices. If it turns out that we needed a bigger buffer, we attempt
6641  * to allocate a buffer of sufficient size, and reissue the command. If the
6642  * first command succeeds, but the second fails, we return whatever we were
6643  * able to get the first time. We return enough information for the caller to
6644  * tell whether he got all the LUNs or only a subset.
6645  *
6646  * If successful, we allocate an array of scsi_lun_t to hold the results. The
6647  * caller must kmem_free(*lunarrayp, *sizep) when finished with it. Upon
6648  * successful return return value is NDI_SUCCESS and:
6649  *
6650  *	*lunarrayp points to the allocated array,
6651  *	*nlunsp is the number of valid LUN entries in the array,
6652  *	*tlunsp is the total number of LUNs in the target,
6653  *	*sizep is the size of the lunarrayp array, which must be freed.
6654  *
6655  * If the *nlunsp is less than *tlunsp, then we were only able to retrieve a
6656  * subset of the total set of LUNs in the target.
6657  */
6658 static int
6659 scsi_device_reportluns(struct scsi_device *sd0, char *taddr, int pi,
6660     scsi_lun_t **lunarrayp, uint32_t *nlunsp, uint32_t *tlunsp, size_t *sizep)
6661 {
6662 	struct buf	*lunrpt_bp;
6663 	struct scsi_pkt *lunrpt_pkt;
6664 	scsi_lunrpt_t	*lunrpt;
6665 	uint32_t	bsize;
6666 	uint32_t	tluns, nluns;
6667 	int		default_maxluns = scsi_lunrpt_default_max;
6668 	dev_info_t	*child;
6669 
6670 	ASSERT(sd0 && lunarrayp && nlunsp && tlunsp && sizep);
6671 
6672 	/*
6673 	 * NOTE: child should only be used in SCSI_HBA_LOG context since with
6674 	 * vHCI enumeration it may be the vHCI 'client' devinfo child instead
6675 	 * of a child of the 'self' pHCI we are enumerating.
6676 	 */
6677 	child = sd0->sd_dev;
6678 
6679 	/* first try, look for up to scsi_lunrpt_default_max LUNs */
6680 	nluns = default_maxluns;
6681 
6682 again:	bsize = sizeof (struct scsi_lunrpt) +
6683 	    ((nluns - 1) * sizeof (struct scsi_lun));
6684 
6685 	lunrpt_bp = scsi_alloc_consistent_buf(&sd0->sd_address,
6686 	    (struct buf *)NULL, bsize, B_READ, SLEEP_FUNC, NULL);
6687 	if (lunrpt_bp == NULL) {
6688 		SCSI_HBA_LOG((_LOG(1), NULL, child, "failed alloc"));
6689 		return (NDI_NOMEM);
6690 	}
6691 
6692 	lunrpt_pkt = scsi_init_pkt(&sd0->sd_address,
6693 	    (struct scsi_pkt *)NULL, lunrpt_bp, CDB_GROUP5,
6694 	    sizeof (struct scsi_arq_status), 0, PKT_CONSISTENT,
6695 	    SLEEP_FUNC, NULL);
6696 	if (lunrpt_pkt == NULL) {
6697 		SCSI_HBA_LOG((_LOG(1), NULL, child, "failed init"));
6698 		scsi_free_consistent_buf(lunrpt_bp);
6699 		return (NDI_NOMEM);
6700 	}
6701 
6702 	(void) scsi_setup_cdb((union scsi_cdb *)lunrpt_pkt->pkt_cdbp,
6703 	    SCMD_REPORT_LUNS, 0, bsize, 0);
6704 
6705 	lunrpt_pkt->pkt_time = scsi_lunrpt_timeout;
6706 
6707 	/*
6708 	 * When sd0 is a vHCI scsi device, we need reportlun to be issued
6709 	 * against a specific LUN0 path_instance that we are enumerating.
6710 	 */
6711 	lunrpt_pkt->pkt_path_instance = pi;
6712 	lunrpt_pkt->pkt_flags |= FLAG_PKT_PATH_INSTANCE;
6713 
6714 	/*
6715 	 * NOTE: scsi_poll may not allow HBA specific recovery from TRAN_BUSY.
6716 	 */
6717 	if (scsi_poll(lunrpt_pkt) < 0) {
6718 		SCSI_HBA_LOG((_LOG(2), NULL, child, "reportlun not supported"));
6719 		scsi_destroy_pkt(lunrpt_pkt);
6720 		scsi_free_consistent_buf(lunrpt_bp);
6721 		return (NDI_FAILURE);
6722 	}
6723 
6724 	scsi_destroy_pkt(lunrpt_pkt);
6725 
6726 	lunrpt = (scsi_lunrpt_t *)lunrpt_bp->b_un.b_addr;
6727 
6728 	/* Compute the total number of LUNs in the target */
6729 	tluns = (((uint_t)lunrpt->lunrpt_len_msb << 24) |
6730 	    ((uint_t)lunrpt->lunrpt_len_mmsb << 16) |
6731 	    ((uint_t)lunrpt->lunrpt_len_mlsb << 8) |
6732 	    ((uint_t)lunrpt->lunrpt_len_lsb)) >> 3;
6733 
6734 	if (tluns == 0) {
6735 		/* Illegal response -- this target is broken */
6736 		SCSI_HBA_LOG((_LOG(1), NULL, child, "illegal tluns of zero"));
6737 		scsi_free_consistent_buf(lunrpt_bp);
6738 		return (DDI_NOT_WELL_FORMED);
6739 	}
6740 
6741 	if (tluns > nluns) {
6742 		/* have more than we allocated space for */
6743 		if (nluns == default_maxluns) {
6744 			/* first time around, reallocate larger */
6745 			scsi_free_consistent_buf(lunrpt_bp);
6746 			nluns = tluns;
6747 			goto again;
6748 		}
6749 
6750 		/* uh oh, we got a different tluns the second time! */
6751 		SCSI_HBA_LOG((_LOG(1), NULL, child,
6752 		    "tluns changed from %d to %d", nluns, tluns));
6753 	} else
6754 		nluns = tluns;
6755 
6756 	/*
6757 	 * Now we have:
6758 	 *	lunrpt_bp is the buffer we're using;
6759 	 *	tluns is the total number of LUNs the target says it has;
6760 	 *	nluns is the number of LUNs we were able to get into the buffer.
6761 	 *
6762 	 * Copy the data out of scarce iopb memory into regular kmem.
6763 	 * The caller must kmem_free(*lunarrayp, *sizep) when finished with it.
6764 	 */
6765 	*lunarrayp = (scsi_lun_t *)kmem_alloc(
6766 	    nluns * sizeof (scsi_lun_t), KM_SLEEP);
6767 	if (*lunarrayp == NULL) {
6768 		SCSI_HBA_LOG((_LOG(1), NULL, child, "NULL lunarray"));
6769 		scsi_free_consistent_buf(lunrpt_bp);
6770 		return (NDI_NOMEM);
6771 	}
6772 
6773 	*sizep = nluns * sizeof (scsi_lun_t);
6774 	*nlunsp = nluns;
6775 	*tlunsp = tluns;
6776 	bcopy((void *)&lunrpt->lunrpt_luns, (void *)*lunarrayp, *sizep);
6777 	scsi_free_consistent_buf(lunrpt_bp);
6778 	SCSI_HBA_LOG((_LOG(3), NULL, child,
6779 	    "@%s,0 path %d: %d/%d luns", taddr, pi, nluns, tluns));
6780 	return (NDI_SUCCESS);
6781 }
6782 
6783 /*
6784  * Enumerate all the LUNs and secondary functions of the specified 'taddr'
6785  * target port as accessed via 'self' pHCI.  Note that sd0 may be associated
6786  * with a child of the vHCI instead of 'self' - in this case the 'pi'
6787  * path_instance is used to ensure that the SCMD_REPORT_LUNS command is issued
6788  * through the 'self' pHCI path.
6789  *
6790  * We multi-thread across all the LUNs and secondary functions and enumerate
6791  * them. Which LUNs exist is based on SCMD_REPORT_LUNS data.
6792  *
6793  * The scsi_device we are called with should be for LUN0 and has been probed.
6794  *
6795  * This function is structured so that an HBA that has a different target
6796  * addressing structure can still use this function to enumerate the its
6797  * LUNs if it uses "taddr,lun" for its LUN space.
6798  *
6799  * We make assumptions about other LUNs associated with the target:
6800  *
6801  *	For SCSI-2 and SCSI-3 target we will issue the SCSI report_luns
6802  *	command. If this fails or we have a SCSI-1 then the number of
6803  *	LUNs is determined based on SCSI_OPTIONS_NLUNS. For a SCSI-1
6804  *	target we never probe above LUN 8, even if SCSI_OPTIONS_NLUNS
6805  *	indicates we should.
6806  *
6807  * HBA drivers wanting a different set of assumptions should implement their
6808  * own LUN enumeration code.
6809  */
6810 static int
6811 scsi_hba_enum_lsf_of_t(struct scsi_device *sd0,
6812     dev_info_t *self, char *taddr, int pi, int mt, scsi_enum_t se)
6813 {
6814 	dev_info_t		*child;
6815 	scsi_hba_tran_t		*tran;
6816 	impl_scsi_tgtmap_t	*tgtmap;
6817 	damap_id_t		tgtid;
6818 	damap_t			*tgtdam;
6819 	damap_t			*lundam = NULL;
6820 	struct scsi_hba_mte_h	*h;
6821 	struct scsi_hba_mte_ld	*ld;
6822 	int			aver;
6823 	scsi_lun_t		*lunp = NULL;
6824 	int			lun;
6825 	uint32_t		nluns;
6826 	uint32_t		tluns;
6827 	size_t			size;
6828 	scsi_lun64_t		lun64;
6829 	int			maxluns;
6830 
6831 	/*
6832 	 * If LUN0 failed then we have no other LUNs.
6833 	 *
6834 	 * NOTE: We need sd_inq to be valid to check ansi version. Since
6835 	 * scsi_unprobe is now a noop (sd_inq freeded in
6836 	 * scsi_busctl_uninitchild) sd_inq remains valid even if a target
6837 	 * driver detach(9E) occurs, resulting in a scsi_unprobe call
6838 	 * (sd_uninit_prevent keeps sd_inq valid by failing any
6839 	 * device_uninitchild attempts).
6840 	 */
6841 	ASSERT(sd0 && sd0->sd_uninit_prevent && sd0->sd_dev && sd0->sd_inq);
6842 	if ((sd0 == NULL) || (sd0->sd_dev == NULL) || (sd0->sd_inq == NULL)) {
6843 		SCSI_HBA_LOG((_LOG(1), NULL, sd0 ? sd0->sd_dev : NULL,
6844 		    "not setup correctly:%s%s%s",
6845 		    (sd0 == NULL) ? " device" : "",
6846 		    (sd0 && (sd0->sd_dev == NULL)) ? " dip" : "",
6847 		    (sd0 && (sd0->sd_inq == NULL)) ? " inq" : ""));
6848 		return (DDI_FAILURE);
6849 	}
6850 
6851 	/*
6852 	 * NOTE: child should only be used in SCSI_HBA_LOG context since with
6853 	 * vHCI enumeration it may be the vHCI 'client' devinfo child instead
6854 	 * of a child of the 'self' pHCI we are enumerating.
6855 	 */
6856 	child = sd0->sd_dev;
6857 
6858 	/* Determine if we are reporting lun observations into lunmap. */
6859 	tran = ndi_flavorv_get(self, SCSA_FLAVOR_SCSI_DEVICE);
6860 	tgtmap = (impl_scsi_tgtmap_t *)tran->tran_tgtmap;
6861 	if (tgtmap) {
6862 		tgtdam = tgtmap->tgtmap_dam[SCSI_TGT_SCSI_DEVICE];
6863 		tgtid = damap_lookup(tgtdam, taddr);
6864 		if (tgtid != NODAM) {
6865 			lundam = damap_id_priv_get(tgtdam, tgtid);
6866 			damap_id_rele(tgtdam, tgtid);
6867 			ASSERT(lundam);
6868 		}
6869 	}
6870 
6871 	if (lundam) {
6872 		/* If using lunmap, start the observation */
6873 		scsi_lunmap_set_begin(self, lundam);
6874 	} else {
6875 		/* allocate and initialize the LUN handle */
6876 		h = kmem_zalloc(sizeof (*h), KM_SLEEP);
6877 		mutex_init(&h->h_lock, NULL, MUTEX_DEFAULT, NULL);
6878 		cv_init(&h->h_cv, NULL, CV_DEFAULT, NULL);
6879 		h->h_self = self;
6880 	}
6881 
6882 	/* See if SCMD_REPORT_LUNS works for SCSI-2 and beyond */
6883 	aver = sd0->sd_inq->inq_ansi;
6884 	if ((aver >= SCSI_VERSION_2) && (scsi_device_reportluns(sd0,
6885 	    taddr, pi, &lunp, &nluns, &tluns, &size) == NDI_SUCCESS)) {
6886 
6887 		ASSERT(lunp && (size > 0) && (nluns > 0) && (tluns > 0));
6888 
6889 		/* loop over the reported LUNs */
6890 		SCSI_HBA_LOG((_LOG(2), NULL, child,
6891 		    "@%s,0 path %d: enumerating %d reported lun%s", taddr, pi,
6892 		    nluns, nluns > 1 ? "s" : ""));
6893 
6894 		for (lun = 0; lun < nluns; lun++) {
6895 			lun64 = scsi_lun_to_lun64(lunp[lun]);
6896 
6897 			if (lundam) {
6898 				if (scsi_lunmap_set_add(self, lundam,
6899 				    taddr, lun64, -1) != DDI_SUCCESS) {
6900 					SCSI_HBA_LOG((_LOG_NF(WARN),
6901 					    "@%s,%" PRIx64 " failed to create",
6902 					    taddr, lun64));
6903 				}
6904 			} else {
6905 				if (lun64 == 0)
6906 					continue;
6907 
6908 				/* allocate a thread data structure for LUN */
6909 				ld = kmem_alloc(sizeof (*ld), KM_SLEEP);
6910 				ld->ld_h = h;
6911 				ld->ld_taddr = taddr;
6912 				ld->ld_lun64 = lun64;
6913 				ld->ld_sfunc = -1;
6914 				ld->ld_se = se;
6915 
6916 				/* process the LUN */
6917 				mutex_enter(&h->h_lock);
6918 				h->h_thr_count++;
6919 				mutex_exit(&h->h_lock);
6920 
6921 				mt |= scsi_hba_log_mt_disable;
6922 				if (mt & SCSI_ENUMERATION_MT_TARGET_DISABLE)
6923 					scsi_hba_enum_lsf_of_tgt_thr(
6924 					    (void *)ld);
6925 				else
6926 					(void) thread_create(NULL, 0,
6927 					    scsi_hba_enum_lsf_of_tgt_thr,
6928 					    (void *)ld, 0, &p0, TS_RUN,
6929 					    minclsyspri);
6930 			}
6931 		}
6932 
6933 		/* free the LUN array allocated by scsi_device_reportluns */
6934 		kmem_free(lunp, size);
6935 	} else {
6936 		/* Couldn't get SCMD_REPORT_LUNS data */
6937 		if (aver >= SCSI_VERSION_3) {
6938 			if (se == SE_HP)
6939 				SCSI_HBA_LOG((_LOG(WARN), NULL, child,
6940 				    "enumeration failed during report_lun"));
6941 			else
6942 				SCSI_HBA_LOG((_LOG(2), NULL, child,
6943 				    "enumeration failed during report_lun"));
6944 		}
6945 
6946 		/* Determine the number of LUNs to enumerate. */
6947 		maxluns = scsi_get_scsi_maxluns(sd0);
6948 
6949 		/* loop over possible LUNs, skipping LUN0 */
6950 		if (maxluns > 1)
6951 			SCSI_HBA_LOG((_LOG(2), NULL, child,
6952 			    "@%s,0 path %d: enumerating luns 1-%d", taddr, pi,
6953 			    maxluns - 1));
6954 		else
6955 			SCSI_HBA_LOG((_LOG(2), NULL, child,
6956 			    "@%s,0 path %d: enumerating just lun0", taddr, pi));
6957 
6958 		for (lun64 = 0; lun64 < maxluns; lun64++) {
6959 			if (lundam) {
6960 				if (scsi_lunmap_set_add(self, lundam,
6961 				    taddr, lun64, -1) != DDI_SUCCESS) {
6962 					SCSI_HBA_LOG((_LOG_NF(WARN),
6963 					    "@%s,%" PRIx64 " failed to create",
6964 					    taddr, lun64));
6965 				}
6966 			} else {
6967 				if (lun64 == 0)
6968 					continue;
6969 
6970 				/* allocate a thread data structure for LUN */
6971 				ld = kmem_alloc(sizeof (*ld), KM_SLEEP);
6972 				ld->ld_h = h;
6973 				ld->ld_taddr = taddr;
6974 				ld->ld_lun64 = lun64;
6975 				ld->ld_sfunc = -1;
6976 				ld->ld_se = se;
6977 
6978 				/* process the LUN */
6979 				mutex_enter(&h->h_lock);
6980 				h->h_thr_count++;
6981 				mutex_exit(&h->h_lock);
6982 
6983 				mt |= scsi_hba_log_mt_disable;
6984 				if (mt & SCSI_ENUMERATION_MT_TARGET_DISABLE)
6985 					scsi_hba_enum_lsf_of_tgt_thr(
6986 					    (void *)ld);
6987 				else
6988 					(void) thread_create(NULL, 0,
6989 					    scsi_hba_enum_lsf_of_tgt_thr,
6990 					    (void *)ld, 0, &p0, TS_RUN,
6991 					    minclsyspri);
6992 			}
6993 		}
6994 	}
6995 
6996 	/*
6997 	 * If we have an embedded service as a secondary function on LUN0 and
6998 	 * the primary LUN0 function is different than the secondary function
6999 	 * then enumerate the secondary function. The sfunc value is the dtype
7000 	 * associated with the embedded service.
7001 	 *
7002 	 * inq_encserv: enclosure service and our dtype is not DTYPE_ESI
7003 	 * or DTYPE_UNKNOWN then create a separate DTYPE_ESI node for
7004 	 * enclosure service access.
7005 	 */
7006 	ASSERT(sd0->sd_inq);
7007 	if (sd0->sd_inq->inq_encserv &&
7008 	    ((sd0->sd_inq->inq_dtype & DTYPE_MASK) != DTYPE_UNKNOWN) &&
7009 	    ((sd0->sd_inq->inq_dtype & DTYPE_MASK) != DTYPE_ESI) &&
7010 	    ((sd0->sd_inq->inq_ansi >= SCSI_VERSION_3))) {
7011 		if (lundam) {
7012 			if (scsi_lunmap_set_add(self, lundam,
7013 			    taddr, 0, DTYPE_ESI) != DDI_SUCCESS) {
7014 				SCSI_HBA_LOG((_LOG_NF(WARN),
7015 				    "@%s,0,%x failed to create",
7016 				    taddr, DTYPE_ESI));
7017 			}
7018 		} else {
7019 			/* allocate a thread data structure for sfunc */
7020 			ld = kmem_alloc(sizeof (*ld), KM_SLEEP);
7021 			ld->ld_h = h;
7022 			ld->ld_taddr = taddr;
7023 			ld->ld_lun64 = 0;
7024 			ld->ld_sfunc = DTYPE_ESI;
7025 			ld->ld_se = se;
7026 
7027 			/* process the LUN */
7028 			mutex_enter(&h->h_lock);
7029 			h->h_thr_count++;
7030 			mutex_exit(&h->h_lock);
7031 
7032 			mt |= scsi_hba_log_mt_disable;
7033 			if (mt & SCSI_ENUMERATION_MT_TARGET_DISABLE)
7034 				scsi_hba_enum_lsf_of_tgt_thr((void *)ld);
7035 			else
7036 				(void) thread_create(NULL, 0,
7037 				    scsi_hba_enum_lsf_of_tgt_thr, (void *)ld,
7038 				    0, &p0, TS_RUN, minclsyspri);
7039 		}
7040 	}
7041 
7042 	/*
7043 	 * Future: Add secondary function support for:
7044 	 *	inq_mchngr (DTYPE_CHANGER)
7045 	 *	inq_sccs (DTYPE_ARRAY_CTRL)
7046 	 */
7047 
7048 	if (lundam) {
7049 		/* If using lunmap, end the observation */
7050 		scsi_lunmap_set_end(self, lundam);
7051 	} else {
7052 		/* wait for all the LUN threads of this target to complete */
7053 		mutex_enter(&h->h_lock);
7054 		while (h->h_thr_count > 0)
7055 			cv_wait(&h->h_cv, &h->h_lock);
7056 		mutex_exit(&h->h_lock);
7057 
7058 		/* free the target handle */
7059 		cv_destroy(&h->h_cv);
7060 		mutex_destroy(&h->h_lock);
7061 		kmem_free(h, sizeof (*h));
7062 	}
7063 
7064 	return (DDI_SUCCESS);
7065 }
7066 
7067 /*
7068  * Enumerate LUN0 and all other LUNs and secondary functions associated with
7069  * the specified target address.
7070  *
7071  * Return NDI_SUCCESS if we might have created a new node.
7072  * Return NDI_FAILURE if we definitely did not create a new node.
7073  */
7074 static int
7075 scsi_hba_bus_config_taddr(dev_info_t *self, char *taddr, int mt, scsi_enum_t se)
7076 {
7077 	char			addr[SCSI_MAXNAMELEN];
7078 	struct scsi_device	*sd;
7079 	int			circ;
7080 	int			ret;
7081 	int			pi;
7082 
7083 	/* See if LUN0 of the specified target exists. */
7084 	(void) snprintf(addr, sizeof (addr), "%s,0", taddr);
7085 
7086 	scsi_hba_devi_enter(self, &circ);
7087 	sd = scsi_device_config(self, NULL, addr, se, &circ, &pi);
7088 
7089 	if (sd) {
7090 		/*
7091 		 * LUN0 exists, enumerate all the other LUNs.
7092 		 *
7093 		 * With vHCI enumeration, when 'self' is a pHCI the sd
7094 		 * scsi_device may be associated with the vHCI 'client'.
7095 		 * In this case 'pi' is the path_instance needed to
7096 		 * continue enumeration communication LUN0 via 'self'
7097 		 * pHCI and specific 'taddr' target address.
7098 		 *
7099 		 * We prevent the removal of LUN0 until we are done with
7100 		 * prevent/allow because we must exit the parent for
7101 		 * multi-threaded scsi_hba_enum_lsf_of_t().
7102 		 *
7103 		 * NOTE: scsi_unprobe is a noop, sd->sd_inq is valid until
7104 		 * device_uninitchild - so sd_uninit_prevent keeps sd_inq valid
7105 		 * by failing any device_uninitchild attempts.
7106 		 */
7107 		ret = NDI_SUCCESS;
7108 		sd->sd_uninit_prevent++;
7109 		scsi_hba_devi_exit(self, circ);
7110 
7111 		(void) scsi_hba_enum_lsf_of_t(sd, self, taddr, pi, mt, se);
7112 
7113 		scsi_hba_devi_enter(self, &circ);
7114 		sd->sd_uninit_prevent--;
7115 	} else
7116 		ret = NDI_FAILURE;
7117 	scsi_hba_devi_exit(self, circ);
7118 	return (ret);
7119 }
7120 
7121 /* Config callout from scsi_hba_thread_taddrs */
7122 static void
7123 scsi_hba_taddr_config_thr(void *arg)
7124 {
7125 	struct scsi_hba_mte_td	*td = (struct scsi_hba_mte_td *)arg;
7126 	struct scsi_hba_mte_h	*h = td->td_h;
7127 
7128 	(void) scsi_hba_bus_config_taddr(h->h_self, td->td_taddr,
7129 	    td->td_mt, td->td_se);
7130 
7131 	/* signal completion of this target thread to the HBA */
7132 	mutex_enter(&h->h_lock);
7133 	if (--h->h_thr_count == 0)
7134 		cv_broadcast(&h->h_cv);
7135 	mutex_exit(&h->h_lock);
7136 
7137 	/* free config thread data */
7138 	kmem_free(td, sizeof (*td));
7139 }
7140 
7141 /*
7142  * Enumerate all the children of the specified SCSI parallel interface (spi).
7143  * An HBA associated with a non-parallel scsi bus should be using another bus
7144  * level enumeration implementation (possibly their own) and calling
7145  * scsi_hba_bus_config_taddr to do enumeration of devices associated with a
7146  * particular target address.
7147  *
7148  * On an spi bus the targets are sequentially enumerated based on the
7149  * width of the bus. We also take care to try to skip the HBAs own initiator
7150  * id. See scsi_hba_enum_lsf_of_t() for LUN and secondary function enumeration.
7151  *
7152  * Return NDI_SUCCESS if we might have created a new node.
7153  * Return NDI_FAILURE if we definitely did not create a new node.
7154  *
7155  * Note: At some point we may want to expose this interface in transport.h
7156  * if we find an hba that implements bus_config but still uses spi-like target
7157  * addresses.
7158  */
7159 static int
7160 scsi_hba_bus_configall_spi(dev_info_t *self, int mt)
7161 {
7162 	int	options;
7163 	int	ntargets;
7164 	int	id;
7165 	int	tgt;
7166 	char	**taddrs;
7167 	char	**taddr;
7168 	char	*tbuf;
7169 
7170 	/*
7171 	 * Find the number of targets supported on the bus. Look at the per
7172 	 * bus scsi-options property on the HBA node and check its
7173 	 * SCSI_OPTIONS_WIDE setting.
7174 	 */
7175 	options = ddi_prop_get_int(DDI_DEV_T_ANY, self,
7176 	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, "scsi-options", -1);
7177 	if ((options != -1) && ((options & SCSI_OPTIONS_WIDE) == 0))
7178 		ntargets = NTARGETS;			/* 8 */
7179 	else
7180 		ntargets = NTARGETS_WIDE;		/* 16 */
7181 
7182 	/*
7183 	 * Find the initiator-id for the HBA so we can skip that. We get the
7184 	 * cached value on the HBA node, established in scsi_hba_attach_setup.
7185 	 * If we were unable to determine the id then we rely on the HBA to
7186 	 * fail gracefully when asked to enumerate itself.
7187 	 */
7188 	id = ddi_prop_get_int(DDI_DEV_T_ANY, self,
7189 	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, "scsi-initiator-id", -1);
7190 	if (id > ntargets) {
7191 		SCSI_HBA_LOG((_LOG(1), self, NULL,
7192 		    "'scsi-initiator-id' bogus for %d target bus: %d",
7193 		    ntargets, id));
7194 		id = -1;
7195 	}
7196 	SCSI_HBA_LOG((_LOG(2), self, NULL,
7197 	    "enumerating targets 0-%d skip %d", ntargets, id));
7198 
7199 	/* form vector of target addresses */
7200 	taddrs = kmem_zalloc(sizeof (char *) * (ntargets + 1), KM_SLEEP);
7201 	for (tgt = 0, taddr = taddrs; tgt < ntargets; tgt++) {
7202 		/* skip initiator */
7203 		if (tgt == id)
7204 			continue;
7205 
7206 		/* convert to string and enumerate the target address */
7207 		tbuf = kmem_alloc(((tgt/16) + 1) + 1, KM_SLEEP);
7208 		(void) sprintf(tbuf, "%x", tgt);
7209 		ASSERT(strlen(tbuf) == ((tgt/16) + 1));
7210 		*taddr++ = tbuf;
7211 	}
7212 
7213 	/* null terminate vector of target addresses */
7214 	*taddr = NULL;
7215 
7216 	/* configure vector of target addresses */
7217 	scsi_hba_thread_taddrs(self, taddrs, mt, SE_BUSCONFIG,
7218 	    scsi_hba_taddr_config_thr);
7219 
7220 	/* free vector of target addresses */
7221 	for (taddr = taddrs; *taddr; taddr++)
7222 		kmem_free(*taddr, strlen(*taddr) + 1);
7223 	kmem_free(taddrs, sizeof (char *) * (ntargets + 1));
7224 	return (NDI_SUCCESS);
7225 }
7226 
7227 /*
7228  * Transport independent bus_configone BUS_CONFIG_ONE implementation.  Takes
7229  * same arguments, minus op, as scsi_hba_bus_config(), tran_bus_config(),
7230  * and scsi_hba_bus_config_spi().
7231  */
7232 int
7233 scsi_hba_bus_configone(dev_info_t *self, uint_t flags, char *arg,
7234     dev_info_t **childp)
7235 {
7236 	int			ret;
7237 	int			circ;
7238 	char			*name, *addr;
7239 	char			*lcp;
7240 	char			sc1, sc2;
7241 	char			nameaddr[SCSI_MAXNAMELEN];
7242 	extern int		i_ndi_make_spec_children(dev_info_t *, uint_t);
7243 	struct scsi_device	*sd0, *sd;
7244 	scsi_lun64_t		lun64;
7245 	int			mt;
7246 
7247 	/* parse_name modifies arg1, we must duplicate "name@addr" */
7248 	(void) strcpy(nameaddr, arg);
7249 	i_ddi_parse_name(nameaddr, &name, &addr, NULL);
7250 
7251 	/* verify the form of the node - we need an @addr */
7252 	if ((name == NULL) || (addr == NULL) ||
7253 	    (*name == '\0') || (*addr == '\0')) {
7254 		/*
7255 		 * OBP may create ill formed template/stub/wild-card
7256 		 * nodes (no @addr) for legacy driver loading methods -
7257 		 * ignore them.
7258 		 */
7259 		SCSI_HBA_LOG((_LOG(2), self, NULL, "%s ill formed", arg));
7260 		return (NDI_FAILURE);
7261 	}
7262 
7263 	/*
7264 	 * Check to see if this is a non-scsi flavor configuration operation.
7265 	 */
7266 	if (strcmp(name, "smp") == 0) {
7267 		/*
7268 		 * Configure the child, and if we're successful return with
7269 		 * active hold.
7270 		 */
7271 		return (smp_hba_bus_config(self, addr, childp));
7272 	}
7273 
7274 	/*
7275 	 * The framework does not ensure the creation of driver.conf
7276 	 * nodes prior to calling a nexus bus_config. For legacy
7277 	 * support of driver.conf file nodes we want to create our
7278 	 * driver.conf file children now so that we can detect if we
7279 	 * are being asked to bus_configone one of these nodes.
7280 	 *
7281 	 * Needing driver.conf file nodes prior to bus config is unique
7282 	 * to scsi_enumeration mixed mode (legacy driver.conf and
7283 	 * dynamic SID node) support. There is no general need for the
7284 	 * framework to make driver.conf children prior to bus_config.
7285 	 *
7286 	 * We enter our HBA (self) prior to scsi_device_config, and
7287 	 * pass it our circ. The scsi_device_config may exit the
7288 	 * HBA around scsi_probe() operations to allow for parallelism.
7289 	 * This is done after the probe node "@addr" is available as a
7290 	 * barrier to prevent parallel probes of the same device. The
7291 	 * probe node is also configured in a way that it can't be
7292 	 * removed by the framework until we are done with it.
7293 	 *
7294 	 * NOTE: The framework is currently preventing many parallel
7295 	 * sibling operations (such as attaches), so the parallelism
7296 	 * we are providing is of marginal use until that is improved.
7297 	 * The most logical way to solve this would be to have separate
7298 	 * target and lun nodes. This would be a large change in the
7299 	 * format of /devices paths and is not being pursued at this
7300 	 * time. The need for parallelism will become more of an issue
7301 	 * with top-down attach for mpxio/vhci and for iSCSI support.
7302 	 * We may want to eventually want a dual mode implementation,
7303 	 * where the HBA determines if we should construct separate
7304 	 * target and lun devinfo nodes.
7305 	 */
7306 	scsi_hba_devi_enter(self, &circ);
7307 	SCSI_HBA_LOG((_LOG(4), self, NULL, "%s@%s config_one", name, addr));
7308 	(void) i_ndi_make_spec_children(self, flags);
7309 
7310 	/*
7311 	 * For bus_configone, we make sure that we can find LUN0
7312 	 * first. This allows the delayed probe/barrier deletion for a
7313 	 * non-existent LUN0 (if enabled in scsi_device_config) to
7314 	 * cover all LUNs on the target. This is done to minimize the
7315 	 * number of independent target selection timeouts that occur
7316 	 * when a target with many LUNs is no longer accessible
7317 	 * (powered off). This removes the need for target driver
7318 	 * probe cache implementations.
7319 	 *
7320 	 * This optimization may not be desirable in a pure bridge
7321 	 * environment where targets on the other side of the bridge
7322 	 * show up as LUNs to the host. If we ever need to support
7323 	 * such a configuration then we should consider implementing a
7324 	 * SCSI_OPTIONS_ILUN0 bit.
7325 	 *
7326 	 * NOTE: we are *not* applying any target limitation filtering
7327 	 * to bus_configone, which means that we are relying on the
7328 	 * HBA tran_tgt_init entry point invoked by scsi_busctl_initchild
7329 	 * to fail.
7330 	 */
7331 	sd0 = (struct scsi_device *)-1;
7332 	lcp = strchr(addr, ',');		/* "addr,lun[,sfunc]" */
7333 	if (lcp) {
7334 		/*
7335 		 * With "tgt,lun[,sfunc]" addressing, multiple addressing levels
7336 		 * have been compressed into single devinfo node unit-address.
7337 		 * This presents a mismatch - there is no bus_config to discover
7338 		 * LUNs below a specific target, the only choice is to
7339 		 * BUS_CONFIG_ALL the HBA. To support BUS_CONFIG_ALL_LUNS below
7340 		 * a specific target, a bus_configone with lun address of "*"
7341 		 * triggers lun discovery below a target.
7342 		 */
7343 		if (*(lcp + 1) == '*') {
7344 			mt = ddi_prop_get_int(DDI_DEV_T_ANY, self,
7345 			    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
7346 			    "scsi-enumeration", scsi_enumeration);
7347 			SCSI_HBA_LOG((_LOG(2), self, NULL,
7348 			    "%s@%s lun enumeration triggered", name, addr));
7349 			*lcp = '\0';		/* turn ',' into '\0' */
7350 			scsi_hba_devi_exit(self, circ);
7351 			(void) scsi_hba_bus_config_taddr(self, addr,
7352 			    mt, SE_BUSCONFIG);
7353 			return (NDI_FAILURE);
7354 		}
7355 
7356 		/* convert hex lun number from ascii */
7357 		lun64 = scsi_addr_to_lun64(lcp + 1);
7358 
7359 		if ((lun64 != 0) && (lun64 != SCSI_LUN64_ILLEGAL)) {
7360 			/*
7361 			 * configure ",0" lun first, saving off
7362 			 * original lun characters.
7363 			 */
7364 			sc1 = *(lcp + 1);
7365 			sc2 = *(lcp + 2);
7366 			*(lcp + 1) = '0';
7367 			*(lcp + 2) = '\0';
7368 			sd0 = scsi_device_config(self, NULL, addr,
7369 			    SE_BUSCONFIG, &circ, NULL);
7370 
7371 			/* restore original lun */
7372 			*(lcp + 1) = sc1;
7373 			*(lcp + 2) = sc2;
7374 
7375 			/*
7376 			 * Apply maxlun filtering.
7377 			 *
7378 			 * Future: We still have the kludged
7379 			 * scsi_check_ss2_LUN_limit() filtering off
7380 			 * scsi_probe() to catch bogus driver.conf
7381 			 * entries.
7382 			 */
7383 			if (sd0 && (lun64 < SCSI_32LUNS_PER_TARGET) &&
7384 			    (lun64 >= scsi_get_scsi_maxluns(sd0))) {
7385 				sd0 = NULL;
7386 				SCSI_HBA_LOG((_LOG(4), self, NULL,
7387 				    "%s@%s filtered", name, addr));
7388 			} else
7389 				SCSI_HBA_LOG((_LOG(4), self, NULL,
7390 				    "%s@%s lun 0 %s", name, addr,
7391 				    sd0 ? "worked" : "failed"));
7392 		}
7393 	}
7394 
7395 	/*
7396 	 * configure the requested device if LUN0 exists or we were
7397 	 * unable to determine the lun format to determine if LUN0
7398 	 * exists.
7399 	 */
7400 	if (sd0) {
7401 		sd = scsi_device_config(self, name, addr,
7402 		    SE_BUSCONFIG, &circ, NULL);
7403 	} else {
7404 		sd = NULL;
7405 		SCSI_HBA_LOG((_LOG(2), self, NULL,
7406 		    "%s@%s no lun 0 or filtered lun", name, addr));
7407 	}
7408 
7409 	/*
7410 	 * We know what we found, to reduce overhead we finish BUS_CONFIG_ONE
7411 	 * processing without calling back to the frameworks
7412 	 * ndi_busop_bus_config (unless we goto framework below).
7413 	 *
7414 	 * If the reference is to a driver name and we created a generic name
7415 	 * (bound to that driver) we will still succeed.  This is important
7416 	 * for correctly resolving old drivername references to device that now
7417 	 * uses a generic names across the transition to generic naming. This
7418 	 * is effectively an internal implementation of the NDI_DRIVERNAME flag.
7419 	 *
7420 	 * We also need to special case the resolve_pathname OBP boot-device
7421 	 * case (modrootloaded == 0) where reference is to a generic name but
7422 	 * we created a legacy driver name node by returning just returning
7423 	 * the node created.
7424 	 */
7425 	if (sd && sd->sd_dev &&
7426 	    ((strcmp(ddi_node_name(sd->sd_dev), name) == 0) ||
7427 	    (strcmp(ddi_driver_name(sd->sd_dev), name) == 0) ||
7428 	    (modrootloaded == 0)) &&
7429 	    (ndi_devi_online(sd->sd_dev,
7430 	    flags & NDI_NO_EVENT) == NDI_SUCCESS)) {
7431 
7432 		/* device attached, return devinfo node with hold */
7433 		ret = NDI_SUCCESS;
7434 		*childp = sd->sd_dev;
7435 		ndi_hold_devi(sd->sd_dev);
7436 	} else {
7437 		/*
7438 		 * In the process of failing we may have added nodes to the HBA
7439 		 * (self), clearing DEVI_MADE_CHILDREN. To reduce the overhead
7440 		 * associated with the frameworks reaction to this we clear the
7441 		 * flag here.
7442 		 */
7443 		mutex_enter(&DEVI(self)->devi_lock);
7444 		DEVI(self)->devi_flags &= ~DEVI_MADE_CHILDREN;
7445 		mutex_exit(&DEVI(self)->devi_lock);
7446 		ret = NDI_FAILURE;
7447 
7448 		/*
7449 		 * The framework may still be able to succeed with
7450 		 * with its GENERIC_PROP code.
7451 		 */
7452 		scsi_hba_devi_exit(self, circ);
7453 		if (flags & NDI_DRV_CONF_REPROBE)
7454 			flags |= NDI_CONFIG_REPROBE;
7455 		flags |= NDI_MDI_FALLBACK;	/* devinfo&pathinfo children */
7456 		return (ndi_busop_bus_config(self, flags, BUS_CONFIG_ONE,
7457 		    (void *)arg, childp, 0));
7458 	}
7459 
7460 	scsi_hba_devi_exit(self, circ);
7461 	return (ret);
7462 }
7463 
7464 /*
7465  * Perform SCSI Parallel Interconnect bus_config
7466  */
7467 static int
7468 scsi_hba_bus_config_spi(dev_info_t *self, uint_t flags,
7469     ddi_bus_config_op_t op, void *arg, dev_info_t **childp)
7470 {
7471 	int			ret;
7472 	int			mt;
7473 
7474 	/*
7475 	 * Enumerate scsi target devices: See if we are doing generic dynamic
7476 	 * enumeration: if driver.conf has not specified the 'scsi-enumeration'
7477 	 * knob then use the global scsi_enumeration knob.
7478 	 */
7479 	mt = ddi_prop_get_int(DDI_DEV_T_ANY, self,
7480 	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
7481 	    "scsi-enumeration", scsi_enumeration);
7482 	if ((mt & SCSI_ENUMERATION_ENABLE) == 0) {
7483 		/*
7484 		 * Static driver.conf file enumeration:
7485 		 *
7486 		 * Force reprobe for BUS_CONFIG_ONE or when manually
7487 		 * reconfiguring via devfsadm(1m) to emulate deferred attach.
7488 		 * Reprobe only discovers driver.conf enumerated nodes, more
7489 		 * dynamic implementations probably require their own
7490 		 * bus_config.
7491 		 */
7492 		if ((op == BUS_CONFIG_ONE) || (flags & NDI_DRV_CONF_REPROBE))
7493 			flags |= NDI_CONFIG_REPROBE;
7494 		flags |= NDI_MDI_FALLBACK;	/* devinfo&pathinfo children */
7495 		return (ndi_busop_bus_config(self, flags, op, arg, childp, 0));
7496 	}
7497 
7498 	if (scsi_hba_busconfig_debug)
7499 		flags |= NDI_DEVI_DEBUG;
7500 
7501 	/*
7502 	 * Generic spi dynamic bus config enumeration to discover and enumerate
7503 	 * the target device nodes we are looking for.
7504 	 */
7505 	switch (op) {
7506 	case BUS_CONFIG_ONE:	/* enumerate the named child */
7507 		ret = scsi_hba_bus_configone(self, flags, (char *)arg, childp);
7508 		break;
7509 
7510 	case BUS_CONFIG_ALL:	/* enumerate all children on the bus */
7511 	case BUS_CONFIG_DRIVER: /* enumerate all children that bind to driver */
7512 		SCSI_HBA_LOG((_LOG(3), self, NULL,
7513 		    "BUS_CONFIG_%s mt %x",
7514 		    (op == BUS_CONFIG_ALL) ? "ALL" : "DRIVER", mt));
7515 
7516 		/*
7517 		 * Enumerate targets on SCSI parallel interconnect and let the
7518 		 * framework finish the operation (attach the nodes).
7519 		 */
7520 		if ((ret = scsi_hba_bus_configall_spi(self, mt)) == NDI_SUCCESS)
7521 			ret = ndi_busop_bus_config(self, flags, op,
7522 			    arg, childp, 0);
7523 		break;
7524 
7525 	default:
7526 		ret = NDI_FAILURE;
7527 		break;
7528 	}
7529 	return (ret);
7530 }
7531 
7532 /*
7533  * Perform SCSI Parallel Interconnect bus_unconfig
7534  */
7535 static int
7536 scsi_hba_bus_unconfig_spi(dev_info_t *self, uint_t flags,
7537     ddi_bus_config_op_t op, void *arg)
7538 {
7539 	int	mt;
7540 	int	circ;
7541 	int	ret;
7542 
7543 	/*
7544 	 * See if we are doing generic dynamic enumeration: if driver.conf has
7545 	 * not specified the 'scsi-enumeration' knob then use the global
7546 	 * scsi_enumeration knob.
7547 	 */
7548 	mt = ddi_prop_get_int(DDI_DEV_T_ANY, self,
7549 	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
7550 	    "scsi-enumeration", scsi_enumeration);
7551 	if ((mt & SCSI_ENUMERATION_ENABLE) == 0)
7552 		return (ndi_busop_bus_unconfig(self, flags, op, arg));
7553 
7554 	if (scsi_hba_busconfig_debug)
7555 		flags |= NDI_DEVI_DEBUG;
7556 
7557 	scsi_hba_devi_enter(self, &circ);
7558 	switch (op) {
7559 	case BUS_UNCONFIG_ONE:
7560 		SCSI_HBA_LOG((_LOG(3), self, NULL,
7561 		    "unconfig one: %s", (char *)arg));
7562 		ret = NDI_SUCCESS;
7563 		break;
7564 
7565 	case BUS_UNCONFIG_ALL:
7566 	case BUS_UNCONFIG_DRIVER:
7567 		ret = NDI_SUCCESS;
7568 		break;
7569 
7570 	default:
7571 		ret = NDI_FAILURE;
7572 		break;
7573 	}
7574 
7575 	/* Perform the generic default bus unconfig */
7576 	if (ret == NDI_SUCCESS)
7577 		ret = ndi_busop_bus_unconfig(self, flags, op, arg);
7578 
7579 	scsi_hba_devi_exit(self, circ);
7580 
7581 	return (ret);
7582 }
7583 
7584 static int
7585 scsi_hba_bus_config_tgtmap(dev_info_t *self, uint_t flags,
7586     ddi_bus_config_op_t op, void *arg, dev_info_t **childp)
7587 {
7588 	int ret = NDI_FAILURE;
7589 
7590 	switch (op) {
7591 	case BUS_CONFIG_ONE:
7592 		ret = scsi_hba_bus_configone(self, flags, arg, childp);
7593 		break;
7594 
7595 	case BUS_CONFIG_ALL:
7596 	case BUS_CONFIG_DRIVER:
7597 		ret = ndi_busop_bus_config(self, flags, op, arg, childp, 0);
7598 		break;
7599 
7600 	default:
7601 		break;
7602 	}
7603 
7604 	return (ret);
7605 }
7606 
7607 static int
7608 scsi_hba_bus_unconfig_tgtmap(dev_info_t *self, uint_t flags,
7609     ddi_bus_config_op_t op, void *arg)
7610 {
7611 	int ret = NDI_FAILURE;
7612 
7613 	switch (op) {
7614 	case BUS_UNCONFIG_ONE:
7615 	case BUS_UNCONFIG_DRIVER:
7616 	case BUS_UNCONFIG_ALL:
7617 		ret = NDI_SUCCESS;
7618 		break;
7619 	default:
7620 		break;
7621 	}
7622 
7623 	if (ret == NDI_SUCCESS) {
7624 		flags &= ~NDI_DEVI_REMOVE;
7625 		ret = ndi_busop_bus_unconfig(self, flags, op, arg);
7626 	}
7627 	return (ret);
7628 }
7629 
7630 static int
7631 scsi_hba_bus_config_iportmap(dev_info_t *self, uint_t flags,
7632     ddi_bus_config_op_t op, void *arg, dev_info_t **childp)
7633 {
7634 	dev_info_t	*child;
7635 	int		circ;
7636 	int		ret = NDI_FAILURE;
7637 
7638 	/*
7639 	 * MPXIO is never a sure thing (and we have mixed children), so
7640 	 * set NDI_NDI_FALLBACK so that ndi_busop_bus_config will
7641 	 * search for both devinfo and pathinfo children.
7642 	 *
7643 	 * Future: Remove NDI_MDI_FALLBACK since devcfg.c now looks for
7644 	 * devinfo/pathinfo children in parallel (instead of old way of
7645 	 * looking for one form of child and then doing "fallback" to
7646 	 * look for other form of child).
7647 	 */
7648 	flags |= NDI_MDI_FALLBACK;	/* devinfo&pathinfo children */
7649 	switch (op) {
7650 	case BUS_CONFIG_ONE:
7651 		scsi_hba_devi_enter(self, &circ);
7652 		/* create the iport node child */
7653 		if ((child = scsi_hba_bus_config_port(self, (char *)arg,
7654 		    SE_BUSCONFIG)) != NULL) {
7655 			if (childp) {
7656 				ndi_hold_devi(child);
7657 				*childp = child;
7658 			}
7659 			ret = NDI_SUCCESS;
7660 		}
7661 		scsi_hba_devi_exit(self, circ);
7662 		break;
7663 
7664 	case BUS_CONFIG_ALL:
7665 	case BUS_CONFIG_DRIVER:
7666 		ret = ndi_busop_bus_config(self, flags, op, arg, childp, 0);
7667 		break;
7668 
7669 	default:
7670 		break;
7671 	}
7672 	return (ret);
7673 }
7674 
7675 static int
7676 scsi_hba_bus_unconfig_iportmap(dev_info_t *self, uint_t flags,
7677     ddi_bus_config_op_t op, void *arg)
7678 {
7679 	flags &= ~NDI_DEVI_REMOVE;
7680 	return (ndi_busop_bus_unconfig(self, flags, op, arg));
7681 }
7682 
7683 /*
7684  * SCSI HBA bus config enumeration entry point. Called via the bus_ops
7685  * bus_config entry point for all SCSA HBA drivers.
7686  *
7687  *  o	If an HBA implements its own bus_config via tran_bus_config then we
7688  *	invoke it. An HBA that implements its own tran_bus_config entry	point
7689  *	may still call back into common SCSA code bus_config code for:
7690  *
7691  *	o SPI bus_config (scsi_hba_bus_spi)
7692  *	o LUN and secondary function enumeration (scsi_hba_enum_lsf_of_t()).
7693  *	o configuration of a specific device (scsi_device_config).
7694  *	o determining 1275 SCSI nodename and compatible property
7695  *	  (scsi_hba_nodename_compatible_get/_free).
7696  *
7697  *   o	Otherwise we implement a SCSI parallel interface (spi) bus config.
7698  *
7699  * Return NDI_SUCCESS if we might have created a new node.
7700  * Return NDI_FAILURE if we definitely did not create a new node.
7701  */
7702 static int
7703 scsi_hba_bus_config(dev_info_t *self, uint_t flags,
7704     ddi_bus_config_op_t op, void *arg, dev_info_t **childp)
7705 {
7706 	scsi_hba_tran_t	*tran;
7707 	int		ret;
7708 
7709 	/* make sure that we will not disappear */
7710 	ASSERT(DEVI(self)->devi_ref);
7711 
7712 	tran = ndi_flavorv_get(self, SCSA_FLAVOR_SCSI_DEVICE);
7713 	if (tran == NULL) {
7714 		/* NULL tran driver.conf config (used by cmdk). */
7715 		if ((op == BUS_CONFIG_ONE) || (flags & NDI_DRV_CONF_REPROBE))
7716 			flags |= NDI_CONFIG_REPROBE;
7717 		return (ndi_busop_bus_config(self, flags, op, arg, childp, 0));
7718 	}
7719 
7720 	/* Check if self is HBA-only node. */
7721 	if (tran->tran_hba_flags & SCSI_HBA_HBA) {
7722 		/* The bus_config request is to configure iports below HBA. */
7723 
7724 #ifdef	sparc
7725 		/*
7726 		 * Sparc's 'boot-device' OBP property value lacks an /iport@X/
7727 		 * component. Prior to the mount of root, we drive a disk@
7728 		 * BUS_CONFIG_ONE operatino down a level to resolve an
7729 		 * OBP 'boot-device' path.
7730 		 *
7731 		 * Future: Add (modrootloaded == 0) below, and insure that
7732 		 * all attempts bus_conf of 'bo_name' (in OBP form) occur
7733 		 * prior to 'modrootloaded = 1;' assignment in vfs_mountroot.
7734 		 */
7735 		if ((op == BUS_CONFIG_ONE) &&
7736 		    (strncmp((char *)arg, "disk@", strlen("disk@")) == 0)) {
7737 			return (scsi_hba_bus_config_prom_node(self,
7738 			    flags, arg, childp));
7739 		}
7740 #endif	/* sparc */
7741 
7742 		if (tran->tran_iportmap) {
7743 			/* config based on scsi_hba_iportmap API */
7744 			ret = scsi_hba_bus_config_iportmap(self,
7745 			    flags, op, arg, childp);
7746 		} else {
7747 			/* config based on 'iport_register' API */
7748 			ret = scsi_hba_bus_config_iports(self,
7749 			    flags, op, arg, childp);
7750 		}
7751 		return (ret);
7752 	}
7753 
7754 	/* Check to see how the iport/HBA does target/lun bus config. */
7755 	if (tran->tran_bus_config) {
7756 		/* HBA config based on Sun-private/legacy tran_bus_config */
7757 		ret = tran->tran_bus_config(self, flags, op, arg, childp);
7758 	} else if (tran->tran_tgtmap) {
7759 		/* SCSAv3 config based on scsi_hba_tgtmap_*() API */
7760 		ret =  scsi_hba_bus_config_tgtmap(self, flags, op, arg, childp);
7761 	} else {
7762 		/* SCSA config based on SCSI Parallel Interconnect */
7763 		ret = scsi_hba_bus_config_spi(self, flags, op, arg, childp);
7764 	}
7765 	return (ret);
7766 }
7767 
7768 /*
7769  * Called via the bus_ops bus_unconfig entry point for SCSI HBA drivers.
7770  */
7771 static int
7772 scsi_hba_bus_unconfig(dev_info_t *self, uint_t flags,
7773     ddi_bus_config_op_t op, void *arg)
7774 {
7775 	int		circ;
7776 	scsi_hba_tran_t	*tran;
7777 	int		ret;
7778 
7779 	tran = ddi_get_driver_private(self);
7780 	if (tran == NULL) {
7781 		/* NULL tran driver.conf unconfig (used by cmdk). */
7782 		return (ndi_busop_bus_unconfig(self, flags, op, arg));
7783 	}
7784 
7785 	/*
7786 	 * Purge barrier/probe node children. We do this prior to
7787 	 * tran_bus_unconfig in case the unconfig implementation calls back
7788 	 * into the common code at a different enumeration level, such a
7789 	 * scsi_device_config, which still creates barrier/probe nodes.
7790 	 */
7791 	scsi_hba_devi_enter(self, &circ);
7792 	scsi_hba_barrier_purge(self);
7793 	scsi_hba_devi_exit(self, circ);
7794 
7795 	/* Check if self is HBA-only node. */
7796 	if (tran->tran_hba_flags & SCSI_HBA_HBA) {
7797 		/* The bus_config request is to unconfigure iports below HBA. */
7798 		if (tran->tran_iportmap) {
7799 			/* unconfig based on scsi_hba_iportmap API */
7800 			ret = scsi_hba_bus_unconfig_iportmap(self,
7801 			    flags, op, arg);
7802 		}
7803 		return (ret);
7804 	}
7805 
7806 	/* Check to see how the iport/HBA does target/lun bus unconfig. */
7807 	if (tran->tran_bus_unconfig) {
7808 		/* HBA unconfig based on Sun-private/legacy tran_bus_unconfig */
7809 		ret = tran->tran_bus_unconfig(self, flags, op, arg);
7810 	} else if (tran->tran_tgtmap) {
7811 		/* SCSAv3 unconfig based on scsi_hba_tgtmap_*() API */
7812 		ret = scsi_hba_bus_unconfig_tgtmap(self, flags, op, arg);
7813 	} else {
7814 		/* SCSA unconfig based on SCSI Parallel Interconnect */
7815 		ret = scsi_hba_bus_unconfig_spi(self, flags, op, arg);
7816 	}
7817 	return (ret);
7818 }
7819 
7820 static void
7821 scsi_tgtmap_scsi_config(void *arg, damap_t *mapp, damap_id_list_t id_list)
7822 {
7823 	scsi_hba_tran_t		*tran = (scsi_hba_tran_t *)arg;
7824 	dev_info_t		*self = tran->tran_iport_dip;
7825 	impl_scsi_tgtmap_t	*tgtmap;
7826 	int			mt;
7827 	damap_id_t		tgtid;
7828 	int			ntargets;
7829 	char			**tgt_addrv;
7830 	char			**tgt_addr;
7831 
7832 	tgtmap = (impl_scsi_tgtmap_t *)tran->tran_tgtmap;
7833 	mt = ddi_prop_get_int(DDI_DEV_T_ANY, self,
7834 	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
7835 	    "scsi-enumeration", scsi_enumeration);
7836 
7837 	/* count the number of targets we need to config */
7838 	for (ntargets = 0,
7839 	    tgtid = damap_id_next(mapp, id_list, NODAM);
7840 	    tgtid != NODAM;
7841 	    tgtid = damap_id_next(mapp, id_list, tgtid))
7842 		ntargets++;
7843 
7844 	SCSI_HBA_LOG((_LOGTGT, self, NULL, "%s %d target%s",
7845 	    damap_name(mapp), ntargets, (ntargets == 1) ? "" : "s"));
7846 	if (ntargets == 0)
7847 		return;
7848 
7849 	/* allocate and form vector of addresses */
7850 	tgt_addrv = kmem_zalloc(sizeof (char *) * (ntargets + 1), KM_SLEEP);
7851 	for (tgt_addr = tgt_addrv,
7852 	    tgtid = damap_id_next(mapp, id_list, NODAM);
7853 	    tgtid != NODAM;
7854 	    tgtid = damap_id_next(mapp, id_list, tgtid),
7855 	    tgt_addr++) {
7856 		*tgt_addr = damap_id2addr(mapp, tgtid);
7857 
7858 		if (scsi_lunmap_create(self, tgtmap, *tgt_addr) != DDI_SUCCESS)
7859 			SCSI_HBA_LOG((_LOG_NF(WARN),
7860 			    "failed to create lunmap for %s", *tgt_addr));
7861 		else
7862 			SCSI_HBA_LOG((_LOGTGT, self, NULL,
7863 			    "%s @%s", damap_name(mapp), *tgt_addr));
7864 	}
7865 
7866 	/* null terminate vector */
7867 	*tgt_addr = NULL;
7868 
7869 	/* configure vector of addresses (with multi-threading) */
7870 	scsi_hba_thread_taddrs(self, tgt_addrv, mt, SE_HP,
7871 	    scsi_hba_taddr_config_thr);
7872 
7873 	/* free vector */
7874 	kmem_free(tgt_addrv, sizeof (char *) * (ntargets + 1));
7875 }
7876 
7877 static void
7878 scsi_tgtmap_scsi_unconfig(void *arg, damap_t *mapp, damap_id_list_t id_list)
7879 {
7880 	scsi_hba_tran_t		*tran = (scsi_hba_tran_t *)arg;
7881 	dev_info_t		*self = tran->tran_iport_dip;
7882 	impl_scsi_tgtmap_t	*tgtmap;
7883 	damap_id_t		tgtid;
7884 	char			*tgt_addr;
7885 
7886 	tgtmap = (impl_scsi_tgtmap_t *)tran->tran_tgtmap;
7887 
7888 	for (tgtid = damap_id_next(mapp, id_list, NODAM);
7889 	    tgtid != NODAM;
7890 	    tgtid = damap_id_next(mapp, id_list, tgtid)) {
7891 		tgt_addr = damap_id2addr(mapp, tgtid);
7892 
7893 		SCSI_HBA_LOG((_LOGTGT, self, NULL,
7894 		    "%s @%s", damap_name(mapp), tgt_addr));
7895 		scsi_lunmap_destroy(self, tgtmap, tgt_addr);
7896 	}
7897 }
7898 
7899 static void
7900 scsi_tgtmap_smp_config(void *arg, damap_t *mapp, damap_id_list_t id_list)
7901 {
7902 	scsi_hba_tran_t	*tran = (scsi_hba_tran_t *)arg;
7903 	dev_info_t	*self = tran->tran_iport_dip;
7904 	damap_id_t	tgtid;
7905 	char		*addr;
7906 
7907 	for (tgtid = damap_id_next(mapp, id_list, NODAM);
7908 	    tgtid != NODAM;
7909 	    tgtid = damap_id_next(mapp, id_list, tgtid)) {
7910 		addr = damap_id2addr(mapp, tgtid);
7911 		SCSI_HBA_LOG((_LOGTGT, self, NULL,
7912 		    "%s @%s", damap_name(mapp), addr));
7913 
7914 		(void) smp_hba_bus_config_taddr(self, addr);
7915 	}
7916 }
7917 
7918 static void
7919 scsi_tgtmap_smp_unconfig(void *arg, damap_t *mapp, damap_id_list_t id_list)
7920 {
7921 	scsi_hba_tran_t	*tran = (scsi_hba_tran_t *)arg;
7922 	dev_info_t	*self = tran->tran_iport_dip;
7923 	damap_id_t	tgtid;
7924 	char		*addr;
7925 	dev_info_t	*child;
7926 	char		nameaddr[SCSI_MAXNAMELEN];
7927 	int		circ;
7928 
7929 	for (tgtid = damap_id_next(mapp, id_list, NODAM);
7930 	    tgtid != NODAM;
7931 	    tgtid = damap_id_next(mapp, id_list, tgtid)) {
7932 		addr = damap_id2addr(mapp, tgtid);
7933 		SCSI_HBA_LOG((_LOGTGT, self, NULL,
7934 		    "%s @%s", damap_name(mapp), addr));
7935 
7936 		(void) snprintf(nameaddr, sizeof (nameaddr), "smp@%s", addr);
7937 		scsi_hba_devi_enter(self, &circ);
7938 		if ((child = ndi_devi_findchild(self, nameaddr)) == NULL) {
7939 			scsi_hba_devi_exit(self, circ);
7940 			continue;
7941 		}
7942 
7943 		if (ndi_devi_offline(child,
7944 		    NDI_DEVFS_CLEAN | NDI_DEVI_REMOVE) == DDI_SUCCESS) {
7945 			SCSI_HBA_LOG((_LOGUNCFG, self, NULL,
7946 			    "devinfo smp@%s offlined and removed", addr));
7947 		} else if (ndi_devi_device_remove(child)) {
7948 			/* Offline/remove failed, note new device_remove */
7949 			SCSI_HBA_LOG((_LOGUNCFG, self, NULL,
7950 			    "devinfo smp@%s offline failed, device_remove",
7951 			    addr));
7952 		}
7953 		scsi_hba_devi_exit(self, circ);
7954 	}
7955 }
7956 
7957 /* ARGSUSED1 */
7958 static void
7959 scsi_tgtmap_smp_activate(void *map_priv, char *tgt_addr, int addrid,
7960     void **tgt_privp)
7961 {
7962 	impl_scsi_tgtmap_t	*tgtmap = (impl_scsi_tgtmap_t *)map_priv;
7963 	dev_info_t		*self = tgtmap->tgtmap_tran->tran_iport_dip;
7964 
7965 	if (tgtmap->tgtmap_activate_cb) {
7966 		SCSI_HBA_LOG((_LOGTGT, self, NULL, "%s @%s activated",
7967 		    damap_name(tgtmap->tgtmap_dam[SCSI_TGT_SMP_DEVICE]),
7968 		    tgt_addr));
7969 
7970 		(*tgtmap->tgtmap_activate_cb)(tgtmap->tgtmap_mappriv,
7971 		    tgt_addr, SCSI_TGT_SMP_DEVICE, tgt_privp);
7972 	}
7973 }
7974 
7975 /* ARGSUSED1 */
7976 static void
7977 scsi_tgtmap_smp_deactivate(void *map_priv, char *tgt_addr, int addrid,
7978     void *tgt_privp)
7979 {
7980 	impl_scsi_tgtmap_t	*tgtmap = (impl_scsi_tgtmap_t *)map_priv;
7981 	dev_info_t		*self = tgtmap->tgtmap_tran->tran_iport_dip;
7982 
7983 	if (tgtmap->tgtmap_deactivate_cb) {
7984 		SCSI_HBA_LOG((_LOGTGT, self, NULL, "%s @%s deactivated",
7985 		    damap_name(tgtmap->tgtmap_dam[SCSI_TGT_SMP_DEVICE]),
7986 		    tgt_addr));
7987 
7988 
7989 		(*tgtmap->tgtmap_deactivate_cb)(tgtmap->tgtmap_mappriv,
7990 		    tgt_addr, SCSI_TGT_SMP_DEVICE, tgt_privp);
7991 	}
7992 }
7993 
7994 /* ARGSUSED1 */
7995 static void
7996 scsi_tgtmap_scsi_activate(void *map_priv, char *tgt_addr, int addrid,
7997     void **tgt_privp)
7998 {
7999 	impl_scsi_tgtmap_t	*tgtmap = (impl_scsi_tgtmap_t *)map_priv;
8000 	dev_info_t		*self = tgtmap->tgtmap_tran->tran_iport_dip;
8001 
8002 	if (tgtmap->tgtmap_activate_cb) {
8003 		SCSI_HBA_LOG((_LOGTGT, self, NULL, "%s @%s activated",
8004 		    damap_name(tgtmap->tgtmap_dam[SCSI_TGT_SCSI_DEVICE]),
8005 		    tgt_addr));
8006 
8007 		(*tgtmap->tgtmap_activate_cb)(tgtmap->tgtmap_mappriv,
8008 		    tgt_addr, SCSI_TGT_SCSI_DEVICE, tgt_privp);
8009 	}
8010 }
8011 
8012 /* ARGSUSED1 */
8013 static void
8014 scsi_tgtmap_scsi_deactivate(void *map_priv, char *tgt_addr, int addrid,
8015     void *tgt_privp)
8016 {
8017 	impl_scsi_tgtmap_t	*tgtmap = (impl_scsi_tgtmap_t *)map_priv;
8018 	dev_info_t		*self = tgtmap->tgtmap_tran->tran_iport_dip;
8019 
8020 	if (tgtmap->tgtmap_deactivate_cb) {
8021 		SCSI_HBA_LOG((_LOGTGT, self, NULL, "%s @%s deactivated",
8022 		    damap_name(tgtmap->tgtmap_dam[SCSI_TGT_SCSI_DEVICE]),
8023 		    tgt_addr));
8024 
8025 		(*tgtmap->tgtmap_deactivate_cb)(tgtmap->tgtmap_mappriv,
8026 		    tgt_addr, SCSI_TGT_SCSI_DEVICE, tgt_privp);
8027 
8028 	}
8029 }
8030 
8031 
8032 int
8033 scsi_hba_tgtmap_create(dev_info_t *self, scsi_tgtmap_mode_t mode,
8034     clock_t settle, int n_entries,
8035     void *tgtmap_priv, scsi_tgt_activate_cb_t activate_cb,
8036     scsi_tgt_deactivate_cb_t deactivate_cb,
8037     scsi_hba_tgtmap_t **handle)
8038 {
8039 	scsi_hba_tran_t		*tran;
8040 	damap_t			*mapp;
8041 	char			context[64];
8042 	impl_scsi_tgtmap_t	*tgtmap;
8043 	damap_rptmode_t		rpt_style;
8044 	char			*scsi_binding_set;
8045 
8046 	if (self == NULL || settle == 0 || n_entries == 0 || handle == NULL)
8047 		return (DDI_FAILURE);
8048 
8049 	*handle = NULL;
8050 
8051 	if (scsi_hba_iport_unit_address(self) == NULL)
8052 		return (DDI_FAILURE);
8053 
8054 	switch (mode) {
8055 	case SCSI_TM_FULLSET:
8056 		rpt_style = DAMAP_REPORT_FULLSET;
8057 		break;
8058 	case SCSI_TM_PERADDR:
8059 		rpt_style = DAMAP_REPORT_PERADDR;
8060 		break;
8061 	default:
8062 		return (DDI_FAILURE);
8063 	}
8064 
8065 	tran = (scsi_hba_tran_t *)ddi_get_driver_private(self);
8066 	ASSERT(tran);
8067 	if (tran == NULL)
8068 		return (DDI_FAILURE);
8069 
8070 	tgtmap = kmem_zalloc(sizeof (*tgtmap), KM_SLEEP);
8071 	tgtmap->tgtmap_tran = tran;
8072 	tgtmap->tgtmap_activate_cb = activate_cb;
8073 	tgtmap->tgtmap_deactivate_cb = deactivate_cb;
8074 	tgtmap->tgtmap_mappriv = tgtmap_priv;
8075 
8076 	(void) snprintf(context, sizeof (context), "%s%d.tgtmap.scsi",
8077 	    ddi_driver_name(self), ddi_get_instance(self));
8078 	SCSI_HBA_LOG((_LOGTGT, self, NULL, "%s", context));
8079 	if (damap_create(context, n_entries, rpt_style, settle,
8080 	    tgtmap, scsi_tgtmap_scsi_activate, scsi_tgtmap_scsi_deactivate,
8081 	    tran, scsi_tgtmap_scsi_config, scsi_tgtmap_scsi_unconfig,
8082 	    &mapp) != DAM_SUCCESS) {
8083 		kmem_free(tgtmap, sizeof (*tgtmap));
8084 		return (DDI_FAILURE);
8085 	}
8086 	tgtmap->tgtmap_dam[SCSI_TGT_SCSI_DEVICE] = mapp;
8087 
8088 	(void) snprintf(context, sizeof (context), "%s%d.tgtmap.smp",
8089 	    ddi_driver_name(self), ddi_get_instance(self));
8090 	SCSI_HBA_LOG((_LOGTGT, self, NULL, "%s", context));
8091 	if (damap_create(context, n_entries, rpt_style, settle,
8092 	    tgtmap, scsi_tgtmap_smp_activate, scsi_tgtmap_smp_deactivate,
8093 	    tran, scsi_tgtmap_smp_config, scsi_tgtmap_smp_unconfig,
8094 	    &mapp) != DAM_SUCCESS) {
8095 		damap_destroy(tgtmap->tgtmap_dam[SCSI_TGT_SCSI_DEVICE]);
8096 		kmem_free(tgtmap, sizeof (*tgtmap));
8097 		return (DDI_FAILURE);
8098 	}
8099 	tgtmap->tgtmap_dam[SCSI_TGT_SMP_DEVICE] = mapp;
8100 
8101 	tran->tran_tgtmap = (scsi_hba_tgtmap_t *)tgtmap;
8102 	*handle = (scsi_hba_tgtmap_t *)tgtmap;
8103 
8104 	/*
8105 	 * We have now set tran_tgtmap, marking the tran as using tgtmap
8106 	 * enumeration services.  To prevent the generation of legacy spi
8107 	 * 'binding-set' compatible forms, remove the 'scsi-binding-set'
8108 	 * property.
8109 	 */
8110 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, self,
8111 	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, "scsi-binding-set",
8112 	    &scsi_binding_set) == DDI_PROP_SUCCESS) {
8113 		if (strcmp(scsi_binding_set, scsi_binding_set_spi) == 0)
8114 			(void) ndi_prop_remove(DDI_DEV_T_NONE, self,
8115 			    "scsi-binding-set");
8116 		ddi_prop_free(scsi_binding_set);
8117 	}
8118 	return (DDI_SUCCESS);
8119 }
8120 
8121 void
8122 scsi_hba_tgtmap_destroy(scsi_hba_tgtmap_t *handle)
8123 {
8124 	impl_scsi_tgtmap_t	*tgtmap = (impl_scsi_tgtmap_t *)handle;
8125 	dev_info_t		*self = tgtmap->tgtmap_tran->tran_iport_dip;
8126 	int			i;
8127 
8128 	for (i = 0; i < SCSI_TGT_NTYPES; i++) {
8129 		if (tgtmap->tgtmap_dam[i]) {
8130 			SCSI_HBA_LOG((_LOGTGT, self, NULL,
8131 			    "%s", damap_name(tgtmap->tgtmap_dam[i])));
8132 
8133 			damap_destroy(tgtmap->tgtmap_dam[i]);
8134 		}
8135 	}
8136 	kmem_free(tgtmap, sizeof (*tgtmap));
8137 }
8138 
8139 static int
8140 scsi_tgtmap_sync(scsi_hba_tgtmap_t *handle)
8141 {
8142 	impl_scsi_tgtmap_t	*tgtmap = (impl_scsi_tgtmap_t *)handle;
8143 	dev_info_t		*self = tgtmap->tgtmap_tran->tran_iport_dip;
8144 	int			empty = 1;
8145 	int			i;
8146 
8147 	for (i = 0; i < SCSI_TGT_NTYPES; i++) {
8148 		if (tgtmap->tgtmap_dam[i]) {
8149 			SCSI_HBA_LOG((_LOGTGT, self, NULL, "%s sync begin",
8150 			    damap_name(tgtmap->tgtmap_dam[i])));
8151 
8152 			/* return 1 if all maps ended up empty */
8153 			empty &= damap_sync(tgtmap->tgtmap_dam[i]);
8154 
8155 			SCSI_HBA_LOG((_LOGTGT, self, NULL, "%s sync end",
8156 			    damap_name(tgtmap->tgtmap_dam[i])));
8157 		}
8158 	}
8159 	return (empty);
8160 }
8161 
8162 int
8163 scsi_hba_tgtmap_set_begin(scsi_hba_tgtmap_t *handle)
8164 {
8165 	impl_scsi_tgtmap_t	*tgtmap = (impl_scsi_tgtmap_t *)handle;
8166 	dev_info_t		*self = tgtmap->tgtmap_tran->tran_iport_dip;
8167 	char			*context;
8168 	int			rv = DDI_SUCCESS;
8169 	int			i;
8170 
8171 	for (i = 0; i < SCSI_TGT_NTYPES; i++) {
8172 		if (tgtmap->tgtmap_dam[i] == NULL)
8173 			continue;
8174 
8175 		context = damap_name(tgtmap->tgtmap_dam[i]);
8176 
8177 		if (i == SCSI_TGT_SCSI_DEVICE) {
8178 			/*
8179 			 * In scsi_device context, so we have the 'context'
8180 			 * string, diagnose the case where the tgtmap caller
8181 			 * is failing to make forward progress, i.e. the caller
8182 			 * is never completing an observation, and calling
8183 			 * scsi_hbg_tgtmap_set_end. If this occurs, the solaris
8184 			 * target/lun state may be out of sync with hardware.
8185 			 */
8186 			if (tgtmap->tgtmap_reports++ >=
8187 			    scsi_hba_tgtmap_reports_max) {
8188 				tgtmap->tgtmap_noisy++;
8189 				if (tgtmap->tgtmap_noisy == 1)
8190 					SCSI_HBA_LOG((_LOG(WARN), self, NULL,
8191 					    "%s: failing to complete a tgtmap "
8192 					    "observation", context));
8193 			}
8194 		}
8195 
8196 		if (damap_addrset_begin(
8197 		    tgtmap->tgtmap_dam[i]) != DAM_SUCCESS) {
8198 			SCSI_HBA_LOG((_LOGTGT, self, NULL, "%s FAIL", context));
8199 			rv = DDI_FAILURE;
8200 			continue;
8201 		}
8202 
8203 		SCSI_HBA_LOG((_LOGTGT, self, NULL, "%s", context));
8204 	}
8205 	return (rv);
8206 }
8207 
8208 
8209 int
8210 scsi_hba_tgtmap_set_add(scsi_hba_tgtmap_t *handle,
8211     scsi_tgtmap_tgt_type_t tgt_type, char *tgt_addr, void *tgt_priv)
8212 {
8213 	impl_scsi_tgtmap_t	*tgtmap = (impl_scsi_tgtmap_t *)handle;
8214 	dev_info_t		*self = tgtmap->tgtmap_tran->tran_iport_dip;
8215 
8216 	if (tgt_type >= SCSI_TGT_NTYPES || !tgtmap->tgtmap_dam[tgt_type])
8217 		return (DDI_FAILURE);
8218 
8219 	SCSI_HBA_LOG((_LOGTGT, self, NULL,
8220 	    "%s @%s", damap_name(tgtmap->tgtmap_dam[tgt_type]), tgt_addr));
8221 
8222 	return ((damap_addrset_add(tgtmap->tgtmap_dam[tgt_type], tgt_addr,
8223 	    NULL, NULL, tgt_priv) == DAM_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
8224 }
8225 
8226 /*ARGSUSED*/
8227 int
8228 scsi_hba_tgtmap_set_end(scsi_hba_tgtmap_t *handle, uint_t flags)
8229 {
8230 	impl_scsi_tgtmap_t	*tgtmap = (impl_scsi_tgtmap_t *)handle;
8231 	dev_info_t		*self = tgtmap->tgtmap_tran->tran_iport_dip;
8232 	char			*context;
8233 	int			rv = DDI_SUCCESS;
8234 	int			i;
8235 
8236 	tgtmap->tgtmap_reports = tgtmap->tgtmap_noisy = 0;
8237 
8238 	for (i = 0; i < SCSI_TGT_NTYPES; i++) {
8239 		if (tgtmap->tgtmap_dam[i] == NULL)
8240 			continue;
8241 
8242 		context = damap_name(tgtmap->tgtmap_dam[i]);
8243 		if (damap_addrset_end(
8244 		    tgtmap->tgtmap_dam[i], 0) != DAM_SUCCESS) {
8245 			SCSI_HBA_LOG((_LOGTGT, self, NULL, "%s FAIL", context));
8246 			rv = DDI_FAILURE;
8247 			continue;
8248 		}
8249 
8250 		SCSI_HBA_LOG((_LOGTGT, self, NULL, "%s", context));
8251 	}
8252 	return (rv);
8253 }
8254 
8255 int
8256 scsi_hba_tgtmap_tgt_add(scsi_hba_tgtmap_t *handle,
8257     scsi_tgtmap_tgt_type_t tgt_type, char *tgt_addr, void *tgt_priv)
8258 
8259 {
8260 	impl_scsi_tgtmap_t	*tgtmap = (impl_scsi_tgtmap_t *)handle;
8261 	dev_info_t		*self = tgtmap->tgtmap_tran->tran_iport_dip;
8262 
8263 	if (tgt_type >= SCSI_TGT_NTYPES || !tgtmap->tgtmap_dam[tgt_type])
8264 		return (DDI_FAILURE);
8265 
8266 	SCSI_HBA_LOG((_LOGTGT, self, NULL,
8267 	    "%s @%s", damap_name(tgtmap->tgtmap_dam[tgt_type]), tgt_addr));
8268 
8269 	return ((damap_addr_add(tgtmap->tgtmap_dam[tgt_type], tgt_addr, NULL,
8270 	    NULL, tgt_priv) == DAM_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
8271 }
8272 
8273 int
8274 scsi_hba_tgtmap_tgt_remove(scsi_hba_tgtmap_t *handle,
8275     scsi_tgtmap_tgt_type_t tgt_type, char *tgt_addr)
8276 {
8277 	impl_scsi_tgtmap_t	*tgtmap = (impl_scsi_tgtmap_t *)handle;
8278 	dev_info_t		*self = tgtmap->tgtmap_tran->tran_iport_dip;
8279 
8280 	if (tgt_type >= SCSI_TGT_NTYPES || !tgtmap->tgtmap_dam[tgt_type])
8281 		return (DDI_FAILURE);
8282 
8283 	SCSI_HBA_LOG((_LOGTGT, self, NULL,
8284 	    "%s @%s", damap_name(tgtmap->tgtmap_dam[tgt_type]), tgt_addr));
8285 
8286 	return ((damap_addr_del(tgtmap->tgtmap_dam[tgt_type],
8287 	    tgt_addr) == DAM_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
8288 }
8289 
8290 int
8291 scsi_hba_tgtmap_lookup(scsi_hba_tgtmap_t *handle,
8292     char *tgt_addr, scsi_tgtmap_tgt_type_t *r_type)
8293 {
8294 	impl_scsi_tgtmap_t	*tgtmap = (impl_scsi_tgtmap_t *)handle;
8295 	dev_info_t		*self = tgtmap->tgtmap_tran->tran_iport_dip;
8296 	damap_id_t		tgtid;
8297 	int			i;
8298 
8299 	for (i = 0; i < SCSI_TGT_NTYPES; i++) {
8300 		tgtid = damap_lookup(tgtmap->tgtmap_dam[i], tgt_addr);
8301 		if (tgtid != NODAM) {
8302 			*r_type = i;
8303 			SCSI_HBA_LOG((_LOG(3), self, NULL,
8304 			    "%s @%s found: type %d",
8305 			    damap_name(tgtmap->tgtmap_dam[i]), tgt_addr, i));
8306 			damap_id_rele(tgtmap->tgtmap_dam[i], tgtid);
8307 			return (DDI_SUCCESS);
8308 		}
8309 	}
8310 
8311 	SCSI_HBA_LOG((_LOG(3), self, NULL,
8312 	    "%s%d.tgtmap @%s not found",
8313 	    ddi_driver_name(self), ddi_get_instance(self), tgt_addr));
8314 	return (DDI_FAILURE);
8315 }
8316 
8317 /*
8318  * Return the unit-address of an 'iport' node, or NULL for non-iport node.
8319  */
8320 char *
8321 scsi_hba_iport_unit_address(dev_info_t *self)
8322 {
8323 	/*
8324 	 * NOTE: Since 'self' could be a SCSA iport node or a SCSA HBA node,
8325 	 * we can't use SCSA flavors: the flavor of a SCSA HBA node is not
8326 	 * established/owned by SCSA, it is established by the nexus that
8327 	 * created the SCSA HBA node (PCI) as a child.
8328 	 *
8329 	 * NOTE: If we want to support a node_name other than "iport" for
8330 	 * an iport node then we can add support for a "scsa-iport-node-name"
8331 	 * property on the SCSA HBA node.  A SCSA HBA driver would set this
8332 	 * property on the SCSA HBA node prior to using the iport API.
8333 	 */
8334 	if (strcmp(ddi_node_name(self), "iport") == 0)
8335 		return (ddi_get_name_addr(self));
8336 	else
8337 		return (NULL);
8338 }
8339 
8340 /*
8341  * Define a SCSI initiator port (bus/channel) for an HBA card that needs to
8342  * support multiple SCSI ports, but only has a single HBA devinfo node. This
8343  * function should be called from the HBA's attach(9E) implementation (when
8344  * processing the HBA devinfo node attach) after the number of SCSI ports on
8345  * the card is known or when the HBA driver DR handler detects a new port.
8346  * The function returns 0 on failure and 1 on success.
8347  *
8348  * The implementation will add the port value into the "scsi-iports" property
8349  * value maintained on the HBA node as. These properties are used by the generic
8350  * scsi bus_config implementation to dynamicaly enumerate the specified iport
8351  * children. The enumeration code will, on demand, create the appropriate
8352  * iport children with a SCSI_ADDR_PROP_IPORTUA unit address. This node will
8353  * bind to the same driver as the HBA node itself. This means that an HBA
8354  * driver that uses iports should expect probe(9E), attach(9E), and detach(9E)
8355  * calls on the iport children of the HBA.  If configuration for all ports was
8356  * already done during HBA node attach, the driver should just return
8357  * DDI_SUCCESS when confronted with an iport node.
8358  *
8359  * A maximum of 32 iport ports are supported per HBA devinfo node.
8360  *
8361  * A NULL "port" can be used to indicate that the framework should enumerate
8362  * target children on the HBA node itself, in addition to enumerating target
8363  * children on any iport nodes declared. There are two reasons that an HBA may
8364  * wish to have target children enumerated on both the HBA node and iport
8365  * node(s):
8366  *
8367  *   o  If, in the past, HBA hardware had only a single physical port but now
8368  *      supports multiple physical ports, the updated driver that supports
8369  *      multiple physical ports may want to avoid /devices path upgrade issues
8370  *      by enumerating the first physical port under the HBA instead of as a
8371  *      iport.
8372  *
8373  *   o  Some hardware RAID HBA controllers (mlx, chs, etc) support multiple
8374  *      SCSI physical ports configured so that various physical devices on
8375  *      the physical ports are amalgamated into virtual devices on a virtual
8376  *      port.  Amalgamated physical devices no longer appear to the host OS
8377  *      on the physical ports, but other non-amalgamated devices may still be
8378  *      visible on the physical ports.  These drivers use a model where the
8379  *      physical ports are iport nodes and the HBA node is the virtual port to
8380  *      the configured virtual devices.
8381  */
8382 int
8383 scsi_hba_iport_register(dev_info_t *self, char *port)
8384 {
8385 	unsigned int ports = 0;
8386 	int rval, i;
8387 	char **iports, **newiports;
8388 
8389 	ASSERT(self);
8390 	if (self == NULL)
8391 		return (DDI_FAILURE);
8392 
8393 	rval = ddi_prop_lookup_string_array(DDI_DEV_T_ANY, self,
8394 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "scsi-iports", &iports,
8395 	    &ports);
8396 
8397 	if (ports >= SCSI_HBA_MAX_IPORTS) {
8398 		ddi_prop_free(iports);
8399 		return (DDI_FAILURE);
8400 	}
8401 
8402 	if (rval == DDI_PROP_SUCCESS) {
8403 		for (i = 0; i < ports; i++) {
8404 			if (strcmp(port, iports[i]) == 0) {
8405 				/* iport already registered */
8406 				ddi_prop_free(iports);
8407 				return (DDI_SUCCESS);
8408 			}
8409 		}
8410 	}
8411 
8412 	newiports = kmem_alloc((sizeof (char *) * (ports + 1)), KM_SLEEP);
8413 
8414 	for (i = 0; i < ports; i++) {
8415 		newiports[i] = strdup(iports[i]);
8416 	}
8417 	newiports[ports] = strdup(port);
8418 	ports++;
8419 
8420 	if (ddi_prop_update_string_array(DDI_DEV_T_NONE, self,
8421 	    "scsi-iports", newiports, ports) != DDI_PROP_SUCCESS) {
8422 		SCSI_HBA_LOG((_LOG(WARN), self, NULL,
8423 		    "failed to establish %s %s",
8424 		    SCSI_ADDR_PROP_IPORTUA, port));
8425 		rval = DDI_FAILURE;
8426 	} else {
8427 		rval = DDI_SUCCESS;
8428 	}
8429 
8430 	/* If there is iport exist, free property */
8431 	if (ports > 1)
8432 		ddi_prop_free(iports);
8433 	for (i = 0; i < ports; i++) {
8434 		strfree(newiports[i]);
8435 	}
8436 	kmem_free(newiports, (sizeof (char *)) * ports);
8437 
8438 	return (rval);
8439 }
8440 
8441 /*
8442  * Check if the HBA has any scsi_hba_iport_register()ed children.
8443  */
8444 int
8445 scsi_hba_iport_exist(dev_info_t *self)
8446 {
8447 	unsigned int ports = 0;
8448 	char **iports;
8449 	int rval;
8450 
8451 	rval = ddi_prop_lookup_string_array(DDI_DEV_T_ANY, self,
8452 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "scsi-iports", &iports,
8453 	    &ports);
8454 
8455 	if (rval != DDI_PROP_SUCCESS)
8456 		return (0);
8457 
8458 	/* If there is now at least 1 iport, then iports is valid */
8459 	if (ports > 0) {
8460 		rval = 1;
8461 	} else
8462 		rval = 0;
8463 	ddi_prop_free(iports);
8464 
8465 	return (rval);
8466 }
8467 
8468 dev_info_t *
8469 scsi_hba_iport_find(dev_info_t *self, char *portnm)
8470 {
8471 	char		*addr = NULL;
8472 	char		**iports;
8473 	unsigned int	num_iports = 0;
8474 	int		rval = DDI_FAILURE;
8475 	int		i = 0;
8476 	dev_info_t	*child = NULL;
8477 
8478 	/* check to see if this is an HBA that defined scsi iports */
8479 	rval = ddi_prop_lookup_string_array(DDI_DEV_T_ANY, self,
8480 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "scsi-iports", &iports,
8481 	    &num_iports);
8482 
8483 	if (rval != DDI_SUCCESS) {
8484 		return (NULL);
8485 	}
8486 	ASSERT(num_iports > 0);
8487 
8488 	/* check to see if this port was registered */
8489 	for (i = 0; i < num_iports; i++) {
8490 		if (strcmp(iports[i], portnm) == 0)
8491 			break;
8492 	}
8493 
8494 	if (i == num_iports) {
8495 		child = NULL;
8496 		goto out;
8497 	}
8498 
8499 	addr = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
8500 	(void) snprintf(addr, SCSI_MAXNAMELEN, "iport@%s", portnm);
8501 	rval = ndi_devi_config_one(self, addr, &child, NDI_NO_EVENT);
8502 	kmem_free(addr, SCSI_MAXNAMELEN);
8503 
8504 	if (rval != DDI_SUCCESS) {
8505 		child = NULL;
8506 	}
8507 out:
8508 	ddi_prop_free(iports);
8509 	return (child);
8510 }
8511 
8512 /*
8513  * Search/create the specified iport node
8514  */
8515 static dev_info_t *
8516 scsi_hba_bus_config_port(dev_info_t *self, char *nameaddr, scsi_enum_t se)
8517 {
8518 	dev_info_t	*child;		/* iport child of HBA node */
8519 	scsi_hba_tran_t	*tran;
8520 	char		*addr;
8521 	char		*compat;
8522 
8523 	/*
8524 	 * See if the iport node already exists.
8525 	 */
8526 	addr = nameaddr + strlen("iport@");
8527 	if (child = ndi_devi_findchild(self, nameaddr)) {
8528 		if (ndi_devi_device_isremoved(child)) {
8529 			if ((se == SE_HP) || !ndi_dev_is_hotplug_node(child)) {
8530 				if (ndi_devi_device_insert(child))
8531 					SCSI_HBA_LOG((_LOGCFG, self, NULL,
8532 					    "devinfo iport@%s device_reinsert",
8533 					    addr));
8534 			} else
8535 				return (NULL);
8536 		}
8537 		return (child);
8538 	}
8539 
8540 
8541 	/*
8542 	 * If config based on scsi_hba_iportmap API, only allow create
8543 	 * from hotplug.
8544 	 */
8545 	tran = ndi_flavorv_get(self, SCSA_FLAVOR_SCSI_DEVICE);
8546 	ASSERT(tran);
8547 	if (tran->tran_iportmap && (se != SE_HP))
8548 		return (NULL);
8549 
8550 	/* allocate and initialize a new "iport" node */
8551 	ndi_devi_alloc_sleep(self, "iport",
8552 	    (se == SE_HP) ? DEVI_SID_HP_NODEID : DEVI_SID_NODEID,
8553 	    &child);
8554 	ASSERT(child);
8555 	/*
8556 	 * Set the flavor of the child to be IPORT flavored
8557 	 */
8558 	ndi_flavor_set(child, SCSA_FLAVOR_IPORT);
8559 
8560 	/*
8561 	 * Add the SCSI_ADDR_PROP_IPORTUA addressing property for this child.
8562 	 * This property is used to identify a iport node, and to represent the
8563 	 * nodes @addr form via node properties.
8564 	 *
8565 	 * Add "compatible" property to the "scsi-iport" node to cause it bind
8566 	 * to the same driver as the HBA  driver. Use the "driver" name
8567 	 * instead of the "binding name" to distinguish from hw node.
8568 	 *
8569 	 * Give the HBA a chance, via tran_set_name_prop, to set additional
8570 	 * iport node properties or to change the "compatible" binding
8571 	 * prior to init_child.
8572 	 *
8573 	 * NOTE: the order of these operations is important so that
8574 	 * scsi_hba_iport works when called.
8575 	 */
8576 	compat = (char *)ddi_driver_name(self);
8577 	if ((ndi_prop_update_string(DDI_DEV_T_NONE, child,
8578 	    SCSI_ADDR_PROP_IPORTUA, addr) != DDI_PROP_SUCCESS) ||
8579 	    (ndi_prop_update_string_array(DDI_DEV_T_NONE, child,
8580 	    "compatible", &compat, 1) != DDI_PROP_SUCCESS) ||
8581 	    ddi_pathname_obp_set(child, NULL) != DDI_SUCCESS) {
8582 		SCSI_HBA_LOG((_LOG_NF(WARN), "%s failed dynamic decoration",
8583 		    nameaddr));
8584 		(void) ddi_remove_child(child, 0);
8585 		child = NULL;
8586 	} else {
8587 		/*
8588 		 * Online/attach in order to get events so devfsadm will
8589 		 * create public names.
8590 		 */
8591 		ndi_hold_devi(child);
8592 		if (ndi_devi_online(child, 0) != NDI_SUCCESS) {
8593 			ndi_rele_devi(child);
8594 			ndi_prop_remove_all(child);
8595 			(void) ndi_devi_free(child);
8596 			child = NULL;
8597 		} else
8598 			ndi_rele_devi(child);
8599 	}
8600 
8601 	return (child);
8602 }
8603 
8604 #ifdef	sparc
8605 /*
8606  * Future: When iportmap boot support is added, consider rewriting this to
8607  * perform a scsi_hba_bus_config(BUS_CONFIG_ALL) on self (HBA) followed by
8608  * a scsi_hba_bus_config(BUS_CONFIG_ONE) on each child of self (each iport).
8609  */
8610 /* ARGSUSED */
8611 static int
8612 scsi_hba_bus_config_prom_node(dev_info_t *self, uint_t flags,
8613     void *arg, dev_info_t **childp)
8614 {
8615 	char		**iports;
8616 	int		circ, i;
8617 	int		ret = NDI_FAILURE;
8618 	unsigned int	num_iports = 0;
8619 	dev_info_t	*pdip = NULL;
8620 	char		*addr = NULL;
8621 
8622 	/* check to see if this is an HBA that defined scsi iports */
8623 	ret = ddi_prop_lookup_string_array(DDI_DEV_T_ANY, self,
8624 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "scsi-iports", &iports,
8625 	    &num_iports);
8626 
8627 	if (ret != DDI_SUCCESS) {
8628 		return (ret);
8629 	}
8630 
8631 	ASSERT(num_iports > 0);
8632 
8633 	addr = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
8634 
8635 	ret = NDI_FAILURE;
8636 
8637 	scsi_hba_devi_enter(self, &circ);
8638 
8639 	/* create iport nodes for each scsi port/bus */
8640 	for (i = 0; i < num_iports; i++) {
8641 		bzero(addr, SCSI_MAXNAMELEN);
8642 		/* Prepend the iport name */
8643 		(void) snprintf(addr, SCSI_MAXNAMELEN, "iport@%s",
8644 		    iports[i]);
8645 		if (pdip = scsi_hba_bus_config_port(self, addr, SE_HP)) {
8646 			if (ndi_busop_bus_config(self, NDI_NO_EVENT,
8647 			    BUS_CONFIG_ONE, addr, &pdip, 0) !=
8648 			    NDI_SUCCESS) {
8649 				continue;
8650 			}
8651 			/*
8652 			 * Try to configure child under iport see wehter
8653 			 * request node is the child of the iport node
8654 			 */
8655 			if (ndi_devi_config_one(pdip, arg, childp,
8656 			    NDI_NO_EVENT) == NDI_SUCCESS) {
8657 				ret = NDI_SUCCESS;
8658 				break;
8659 			}
8660 		}
8661 	}
8662 
8663 	scsi_hba_devi_exit(self, circ);
8664 
8665 	kmem_free(addr, SCSI_MAXNAMELEN);
8666 
8667 	ddi_prop_free(iports);
8668 
8669 	return (ret);
8670 }
8671 #endif
8672 
8673 /*
8674  * Perform iport port/bus bus_config.
8675  */
8676 static int
8677 scsi_hba_bus_config_iports(dev_info_t *self, uint_t flags,
8678     ddi_bus_config_op_t op, void *arg, dev_info_t **childp)
8679 {
8680 	char		*nameaddr, *addr;
8681 	char		**iports;
8682 	int		circ, i;
8683 	int		ret = NDI_FAILURE;
8684 	unsigned int	num_iports = 0;
8685 
8686 	/* check to see if this is an HBA that defined scsi iports */
8687 	ret = ddi_prop_lookup_string_array(DDI_DEV_T_ANY, self,
8688 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "scsi-iports", &iports,
8689 	    &num_iports);
8690 
8691 	if (ret != DDI_SUCCESS) {
8692 		return (ret);
8693 	}
8694 
8695 	ASSERT(num_iports > 0);
8696 
8697 	scsi_hba_devi_enter(self, &circ);
8698 
8699 	switch (op) {
8700 	case BUS_CONFIG_ONE:
8701 		/* return if this operation is not against an iport node */
8702 		nameaddr = (char *)arg;
8703 		if ((nameaddr == NULL) ||
8704 		    (strncmp(nameaddr, "iport@", strlen("iport@")) != 0)) {
8705 			ret = NDI_FAILURE;
8706 			scsi_hba_devi_exit(self, circ);
8707 			ddi_prop_free(iports);
8708 			return (ret);
8709 		}
8710 
8711 		/* parse the port number from "iport@%s" */
8712 		addr = nameaddr + strlen("iport@");
8713 
8714 		/* check to see if this port was registered */
8715 		for (i = 0; i < num_iports; i++) {
8716 			if (strcmp((iports[i]), addr) == 0)
8717 				break;
8718 		}
8719 
8720 		if (i == num_iports) {
8721 			ret = NDI_FAILURE;
8722 			break;
8723 		}
8724 
8725 		/* create the iport node child */
8726 		if (scsi_hba_bus_config_port(self, nameaddr, SE_BUSCONFIG)) {
8727 			ret = NDI_SUCCESS;
8728 		}
8729 		break;
8730 
8731 	case BUS_CONFIG_ALL:
8732 	case BUS_CONFIG_DRIVER:
8733 		addr = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
8734 		/* create iport nodes for each scsi port/bus */
8735 		for (i = 0; i < num_iports; i++) {
8736 			bzero(addr, SCSI_MAXNAMELEN);
8737 			/* Prepend the iport name */
8738 			(void) snprintf(addr, SCSI_MAXNAMELEN, "iport@%s",
8739 			    iports[i]);
8740 			(void) scsi_hba_bus_config_port(self, addr,
8741 			    SE_BUSCONFIG);
8742 		}
8743 
8744 		kmem_free(addr, SCSI_MAXNAMELEN);
8745 		ret = NDI_SUCCESS;
8746 		break;
8747 	}
8748 	if (ret == NDI_SUCCESS) {
8749 #ifdef sparc
8750 		/*
8751 		 * Mask NDI_PROMNAME since PROM doesn't have iport
8752 		 * node at all.
8753 		 */
8754 		flags &= (~NDI_PROMNAME);
8755 #endif
8756 		flags |= NDI_MDI_FALLBACK;	/* devinfo&pathinfo children */
8757 		ret = ndi_busop_bus_config(self, flags, op,
8758 		    arg, childp, 0);
8759 	}
8760 	scsi_hba_devi_exit(self, circ);
8761 
8762 	ddi_prop_free(iports);
8763 
8764 	return (ret);
8765 }
8766 
8767 typedef struct impl_scsi_iportmap {
8768 	dev_info_t	*iportmap_hba_dip;
8769 	damap_t		*iportmap_dam;
8770 } impl_scsi_iportmap_t;
8771 
8772 static void
8773 scsi_iportmap_config(void *arg, damap_t *mapp, damap_id_list_t id_list)
8774 {
8775 	dev_info_t	*self = (dev_info_t *)arg;
8776 	int		circ;
8777 	damap_id_t	tgtid;
8778 	char		nameaddr[SCSI_MAXNAMELEN];
8779 	char		*iport_addr;
8780 
8781 	scsi_hba_devi_enter(self, &circ);
8782 
8783 	for (tgtid = damap_id_next(mapp, id_list, NODAM);
8784 	    tgtid != NODAM;
8785 	    tgtid = damap_id_next(mapp, id_list, tgtid)) {
8786 		iport_addr = damap_id2addr(mapp, tgtid);
8787 		SCSI_HBA_LOG((_LOGIPT, self, NULL,
8788 		    "%s @%s", damap_name(mapp), iport_addr));
8789 
8790 		(void) snprintf(nameaddr, sizeof (nameaddr),
8791 		    "iport@%s", iport_addr);
8792 		(void) scsi_hba_bus_config_port(self, nameaddr, SE_HP);
8793 	}
8794 
8795 	scsi_hba_devi_exit(self, circ);
8796 }
8797 
8798 static void
8799 scsi_iportmap_unconfig(void *arg, damap_t *mapp, damap_id_list_t id_list)
8800 {
8801 	dev_info_t	*self = arg;
8802 	dev_info_t	*child;		/* iport child of HBA node */
8803 	int		circ;
8804 	damap_id_t	tgtid;
8805 	char		*addr;
8806 	char		nameaddr[SCSI_MAXNAMELEN];
8807 	scsi_hba_tran_t	*tran;
8808 	int		empty;
8809 
8810 	for (tgtid = damap_id_next(mapp, id_list, NODAM);
8811 	    tgtid != NODAM;
8812 	    tgtid = damap_id_next(mapp, id_list, tgtid)) {
8813 		addr = damap_id2addr(mapp, tgtid);
8814 		SCSI_HBA_LOG((_LOGIPT, self, NULL,
8815 		    "%s @%s", damap_name(mapp), addr));
8816 
8817 		(void) snprintf(nameaddr, sizeof (nameaddr), "iport@%s", addr);
8818 		scsi_hba_devi_enter(self, &circ);
8819 		if ((child = ndi_devi_findchild(self, nameaddr)) == NULL) {
8820 			scsi_hba_devi_exit(self, circ);
8821 			continue;
8822 		}
8823 
8824 		tran = ddi_get_driver_private(child);
8825 		ASSERT(tran);
8826 
8827 		ndi_hold_devi(child);
8828 		scsi_hba_devi_exit(self, circ);
8829 
8830 		/*
8831 		 * A begin/end (clear) against the iport's
8832 		 * tgtmap will trigger unconfigure of all
8833 		 * targets on the iport.
8834 		 *
8835 		 * Future: This bit of code only works if the
8836 		 * target map reporting style is are full
8837 		 * reports and not per-address. Maybe we
8838 		 * should plan on handling this by
8839 		 * auto-unconfiguration when destroying the
8840 		 * target map(s).
8841 		 */
8842 		(void) scsi_hba_tgtmap_set_begin(
8843 		    tran->tran_tgtmap);
8844 		(void) scsi_hba_tgtmap_set_end(
8845 		    tran->tran_tgtmap, 0);
8846 
8847 		/* wait for unconfigure */
8848 		empty = scsi_tgtmap_sync(tran->tran_tgtmap);
8849 
8850 		scsi_hba_devi_enter(self, &circ);
8851 		ndi_rele_devi(child);
8852 
8853 		/* If begin/end/sync ends in empty map, offline/remove. */
8854 		if (empty) {
8855 			if (ndi_devi_offline(child,
8856 			    NDI_DEVFS_CLEAN | NDI_DEVI_REMOVE) == DDI_SUCCESS) {
8857 				SCSI_HBA_LOG((_LOGUNCFG, self, NULL,
8858 				    "devinfo iport@%s offlined and removed",
8859 				    addr));
8860 			} else if (ndi_devi_device_remove(child)) {
8861 				/* Offline/rem failed, note new device_remove */
8862 				SCSI_HBA_LOG((_LOGUNCFG, self, NULL,
8863 				    "devinfo iport@%s offline failed, "
8864 				    "device_remove", addr));
8865 			}
8866 		}
8867 		scsi_hba_devi_exit(self, circ);
8868 	}
8869 
8870 }
8871 
8872 int
8873 scsi_hba_iportmap_create(dev_info_t *self, clock_t settle, int n_entries,
8874     scsi_hba_iportmap_t **handle)
8875 {
8876 	scsi_hba_tran_t		*tran;
8877 	damap_t			*mapp;
8878 	char			context[64];
8879 	impl_scsi_iportmap_t	*iportmap;
8880 
8881 	if (self == NULL || settle == 0 || n_entries == 0 || handle == NULL)
8882 		return (DDI_FAILURE);
8883 
8884 	*handle = NULL;
8885 
8886 	if (scsi_hba_iport_unit_address(self) != NULL)
8887 		return (DDI_FAILURE);
8888 
8889 	tran = (scsi_hba_tran_t *)ddi_get_driver_private(self);
8890 	ASSERT(tran);
8891 	if (tran == NULL)
8892 		return (DDI_FAILURE);
8893 
8894 	(void) snprintf(context, sizeof (context), "%s%d.iportmap",
8895 	    ddi_driver_name(self), ddi_get_instance(self));
8896 
8897 	if (damap_create(context, n_entries, DAMAP_REPORT_PERADDR, settle,
8898 	    NULL, NULL, NULL, self,
8899 	    scsi_iportmap_config, scsi_iportmap_unconfig, &mapp) !=
8900 	    DAM_SUCCESS) {
8901 		return (DDI_FAILURE);
8902 	}
8903 	iportmap = kmem_zalloc(sizeof (*iportmap), KM_SLEEP);
8904 	iportmap->iportmap_hba_dip = self;
8905 	iportmap->iportmap_dam = mapp;
8906 
8907 	tran->tran_iportmap = (scsi_hba_iportmap_t *)iportmap;
8908 	*handle = (scsi_hba_iportmap_t *)iportmap;
8909 
8910 	SCSI_HBA_LOG((_LOGIPT, self, NULL, "%s", damap_name(mapp)));
8911 	return (DDI_SUCCESS);
8912 }
8913 
8914 void
8915 scsi_hba_iportmap_destroy(scsi_hba_iportmap_t *handle)
8916 {
8917 	impl_scsi_iportmap_t	*iportmap = (impl_scsi_iportmap_t *)handle;
8918 	dev_info_t		*self = iportmap->iportmap_hba_dip;
8919 
8920 	SCSI_HBA_LOG((_LOGIPT, self, NULL,
8921 	    "%s", damap_name(iportmap->iportmap_dam)));
8922 
8923 	damap_destroy(iportmap->iportmap_dam);
8924 	kmem_free(iportmap, sizeof (*iportmap));
8925 }
8926 
8927 int
8928 scsi_hba_iportmap_iport_add(scsi_hba_iportmap_t *handle,
8929     char *iport_addr, void *iport_priv)
8930 {
8931 	impl_scsi_iportmap_t	*iportmap = (impl_scsi_iportmap_t *)handle;
8932 	dev_info_t		*self = iportmap->iportmap_hba_dip;
8933 
8934 	SCSI_HBA_LOG((_LOGIPT, self, NULL,
8935 	    "%s @%s", damap_name(iportmap->iportmap_dam), iport_addr));
8936 
8937 	return ((damap_addr_add(iportmap->iportmap_dam, iport_addr, NULL,
8938 	    NULL, iport_priv) == DAM_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
8939 }
8940 
8941 int
8942 scsi_hba_iportmap_iport_remove(scsi_hba_iportmap_t *handle,
8943     char *iport_addr)
8944 {
8945 	impl_scsi_iportmap_t	*iportmap = (impl_scsi_iportmap_t *)handle;
8946 	dev_info_t		*self = iportmap->iportmap_hba_dip;
8947 
8948 	SCSI_HBA_LOG((_LOGIPT, self, NULL,
8949 	    "%s @%s", damap_name(iportmap->iportmap_dam), iport_addr));
8950 
8951 	return ((damap_addr_del(iportmap->iportmap_dam,
8952 	    iport_addr) == DAM_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
8953 }
8954 
8955 int
8956 scsi_hba_iportmap_lookup(scsi_hba_iportmap_t *handle,
8957     char *iport_addr)
8958 {
8959 	impl_scsi_iportmap_t	*iportmap = (impl_scsi_iportmap_t *)handle;
8960 	dev_info_t		*self = iportmap->iportmap_hba_dip;
8961 	damap_id_t		iportid;
8962 
8963 	iportid = damap_lookup(iportmap->iportmap_dam, iport_addr);
8964 	if (iportid != NODAM) {
8965 		SCSI_HBA_LOG((_LOG(3), self, NULL,
8966 		    "%s @%s found",
8967 		    damap_name(iportmap->iportmap_dam), iport_addr));
8968 		damap_id_rele(iportmap->iportmap_dam, iportid);
8969 		return (DDI_SUCCESS);
8970 	}
8971 
8972 	SCSI_HBA_LOG((_LOG(3), self, NULL,
8973 	    "%s @%s not found",
8974 	    damap_name(iportmap->iportmap_dam), iport_addr));
8975 	return (DDI_FAILURE);
8976 }
8977 
8978 static void
8979 scsi_lunmap_config(void *arg, damap_t *lundam, damap_id_list_t id_list)
8980 {
8981 	impl_scsi_tgtmap_t	*tgtmap = (impl_scsi_tgtmap_t *)arg;
8982 	scsi_hba_tran_t		*tran = tgtmap->tgtmap_tran;
8983 	dev_info_t		*self = tran->tran_iport_dip;
8984 	damap_id_t		lunid;
8985 	char			*addr;
8986 
8987 	/* iterate over the LUNS we need to config */
8988 	for (lunid = damap_id_next(lundam, id_list, NODAM);
8989 	    lunid != NODAM;
8990 	    lunid = damap_id_next(lundam, id_list, lunid)) {
8991 		addr = damap_id2addr(lundam, lunid);
8992 		SCSI_HBA_LOG((_LOGLUN, self, NULL,
8993 		    "%s @%s", damap_name(lundam), addr));
8994 
8995 		(void) scsi_hba_bus_configone_addr(self, addr, SE_HP);
8996 	}
8997 }
8998 
8999 static void
9000 scsi_lunmap_unconfig(void *arg, damap_t *lundam, damap_id_list_t id_list)
9001 {
9002 	impl_scsi_tgtmap_t	*tgtmap = (impl_scsi_tgtmap_t *)arg;
9003 	scsi_hba_tran_t		*tran = tgtmap->tgtmap_tran;
9004 	dev_info_t		*self = tran->tran_iport_dip;
9005 	damap_id_t		lunid;
9006 	char			*addr;
9007 
9008 	for (lunid = damap_id_next(lundam, id_list, NODAM);
9009 	    lunid != NODAM;
9010 	    lunid = damap_id_next(lundam, id_list, lunid)) {
9011 		addr = damap_id2addr(lundam, lunid);
9012 		SCSI_HBA_LOG((_LOGLUN, self, NULL,
9013 		    "%s @%s", damap_name(lundam), addr));
9014 
9015 		scsi_hba_bus_unconfigone_addr(self, addr);
9016 	}
9017 }
9018 
9019 static int
9020 scsi_lunmap_create(dev_info_t *self, impl_scsi_tgtmap_t *tgtmap, char *taddr)
9021 {
9022 	char			context[64];
9023 	damap_t			*tgtdam;
9024 	damap_id_t		tgtid;
9025 	damap_t			*lundam;
9026 
9027 	(void) snprintf(context, sizeof (context), "%s%d.%s.lunmap",
9028 	    ddi_driver_name(self), ddi_get_instance(self), taddr);
9029 
9030 	tgtdam = tgtmap->tgtmap_dam[SCSI_TGT_SCSI_DEVICE];
9031 	tgtid = damap_lookup(tgtdam, taddr);
9032 	if (tgtid == NODAM) {
9033 		SCSI_HBA_LOG((_LOG(1), self, NULL,
9034 		    "target %s not found", context));
9035 		return (DDI_FAILURE);
9036 	}
9037 
9038 	lundam = damap_id_priv_get(tgtdam, tgtid);
9039 	if (lundam) {
9040 		SCSI_HBA_LOG((_LOG(1), self, NULL,
9041 		    "lunmap %s already created", context));
9042 		damap_id_rele(tgtdam, tgtid);
9043 		return (DDI_FAILURE);
9044 	}
9045 
9046 	/* NOTE: expected ref at tgtid/taddr: 2: caller + lookup. */
9047 	SCSI_HBA_LOG((_LOGLUN, self, NULL, "%s creat, id %d ref %d",
9048 	    context, tgtid, damap_id_ref(tgtdam, tgtid)));
9049 
9050 	/* create lundam */
9051 	if (damap_create(context, LUNMAPSIZE, DAMAP_REPORT_FULLSET, 1,
9052 	    NULL, NULL, NULL,
9053 	    tgtmap, scsi_lunmap_config, scsi_lunmap_unconfig,
9054 	    &lundam) != DAM_SUCCESS) {
9055 		SCSI_HBA_LOG((_LOG(1), self, NULL,
9056 		    "%s create failed, id %d ref %d",
9057 		    context, tgtid, damap_id_ref(tgtdam, tgtid)));
9058 		damap_id_rele(tgtdam, tgtid);
9059 		return (DDI_FAILURE);
9060 	}
9061 
9062 	/*
9063 	 * Return with damap_id_hold at tgtid/taddr from damap_lookup to
9064 	 * account for damap_id_prv_set below.
9065 	 */
9066 	damap_id_priv_set(tgtdam, tgtid, lundam);
9067 	return (DDI_SUCCESS);
9068 }
9069 
9070 static void
9071 scsi_lunmap_destroy(dev_info_t *self, impl_scsi_tgtmap_t *tgtmap, char *taddr)
9072 {
9073 	char			context[64];
9074 	damap_t			*tgtdam;
9075 	damap_id_t		tgtid;
9076 	damap_t			*lundam;
9077 
9078 	(void) snprintf(context, sizeof (context), "%s%d.%s.lunmap",
9079 	    ddi_driver_name(self), ddi_get_instance(self), taddr);
9080 
9081 	tgtdam = tgtmap->tgtmap_dam[SCSI_TGT_SCSI_DEVICE];
9082 	tgtid = damap_lookup(tgtdam, taddr);
9083 	if (tgtid == NODAM) {
9084 		SCSI_HBA_LOG((_LOG(1), self, NULL,
9085 		    "target %s not found", context));
9086 		return;
9087 	}
9088 
9089 	lundam = (damap_t *)damap_id_priv_get(tgtdam, tgtid);
9090 	if (lundam == NULL) {
9091 		damap_id_rele(tgtdam, tgtid);		/* from damap_lookup */
9092 		SCSI_HBA_LOG((_LOG(1), self, NULL,
9093 		    "lunmap %s already destroyed", context));
9094 		return;
9095 	}
9096 
9097 	/* NOTE: expected ref at tgtid/taddr: 3: priv_set + caller + lookup. */
9098 	SCSI_HBA_LOG((_LOGLUN, self, NULL, "%s, id %d ref %d",
9099 	    damap_name(lundam), tgtid, damap_id_ref(tgtdam, tgtid)));
9100 
9101 	/*
9102 	 * A begin/end (clear) against a target's lunmap will trigger
9103 	 * unconfigure of all LUNs on the target.
9104 	 */
9105 	scsi_lunmap_set_begin(self, lundam);
9106 	scsi_lunmap_set_end(self, lundam);
9107 
9108 	SCSI_HBA_LOG((_LOGLUN, self, NULL,
9109 	    "%s sync begin", damap_name(lundam)));
9110 
9111 	(void) damap_sync(lundam);	/* wait for unconfigure */
9112 
9113 	SCSI_HBA_LOG((_LOGLUN, self, NULL,
9114 	    "%s sync end", damap_name(lundam)));
9115 
9116 	damap_id_priv_set(tgtdam, tgtid, NULL);
9117 
9118 	/* release hold established by damap_lookup above */
9119 	damap_id_rele(tgtdam, tgtid);
9120 
9121 	/* release hold established since scsi_lunmap_create() */
9122 	damap_id_rele(tgtdam, tgtid);
9123 
9124 	damap_destroy(lundam);
9125 }
9126 
9127 static void
9128 scsi_lunmap_set_begin(dev_info_t *self, damap_t *lundam)
9129 {
9130 	SCSI_HBA_LOG((_LOGLUN, self, NULL, "%s", damap_name(lundam)));
9131 
9132 	(void) damap_addrset_begin(lundam);
9133 }
9134 
9135 static int
9136 scsi_lunmap_set_add(dev_info_t *self, damap_t *lundam,
9137     char *taddr, scsi_lun64_t lun64, int sfunc)
9138 {
9139 	char	ua[SCSI_MAXNAMELEN];
9140 
9141 	/* make unit address string form of "@taddr,lun[,sfunc]" */
9142 	if (sfunc == -1)
9143 		(void) snprintf(ua, sizeof (ua), "%s,%" PRIx64, taddr, lun64);
9144 	else
9145 		(void) snprintf(ua, sizeof (ua), "%s,%" PRIx64 ",%x",
9146 		    taddr, lun64, sfunc);
9147 
9148 	SCSI_HBA_LOG((_LOGLUN, self, NULL, "%s @%s", damap_name(lundam), ua));
9149 
9150 	return ((damap_addrset_add(lundam, ua, NULL, NULL,
9151 	    NULL) == DAM_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
9152 }
9153 
9154 static void
9155 scsi_lunmap_set_end(dev_info_t *self, damap_t *lundam)
9156 {
9157 	SCSI_HBA_LOG((_LOGLUN, self, NULL, "%s", damap_name(lundam)));
9158 
9159 	(void) damap_addrset_end(lundam, 0);
9160 }
9161 
9162 int
9163 scsi_lunmap_lookup(dev_info_t *self, damap_t *lundam, char *addr)
9164 {
9165 	damap_id_t		lunid;
9166 
9167 	if ((lunid = damap_lookup(lundam, addr)) != NODAM) {
9168 		SCSI_HBA_LOG((_LOG(3), self, NULL,
9169 		    "%s @%s found", damap_name(lundam), addr));
9170 		damap_id_rele(lundam, lunid);
9171 		return (DDI_SUCCESS);
9172 	}
9173 
9174 	SCSI_HBA_LOG((_LOG(3), self, NULL,
9175 	    "%s @%s not found", damap_name(lundam), addr));
9176 	return (DDI_FAILURE);
9177 }
9178 
9179 /*
9180  * phymap implementation
9181  *
9182  * We manage the timed aggregation of phys into a phy map * by creating a
9183  * SAS port construct (based upon 'name' of "local,remote" SAS addresses)
9184  * upon the first link up. As time goes on additional phys may join that port.
9185  * After an appropriate amount of settle time, we trigger the activation
9186  * callback which will then take the resultant bit mask of phys (phymask) in
9187  * the SAS port and use that to call back to the callback function
9188  * provided by the additional caller.
9189  *
9190  * We cross check to make sure that phys only exist in one SAS port at a
9191  * time by having soft state for each phy point back to the created
9192  * SAS port.
9193  *
9194  * NOTE: Make SAS_PHY_UA_LEN max(SAS_PHY_PHYMASK_LEN, SAS_PHY_NAME_LEN)
9195  * so we have enough space if sas_phymap_bitset2phymaskua phymask address
9196  * is already in use, and we end up using port name as unit address.
9197  */
9198 #define	SAS_PHY_NAME_FMT	"%" PRIx64 ",%" PRIx64
9199 #define	SAS_PHY_NAME_LEN	(16 + 1 + 16 + 1)
9200 #define	SAS_PHY_NPHY		(SAS2_PHYNUM_MAX + 1)
9201 #define	SAS_PHY_PHYMASK_LEN	((roundup(SAS_PHY_NPHY, 4)) / 4)
9202 #if	(SAS_PHY_PHYMASK_LEN > SAS_PHY_NAME_LEN)
9203 #define	SAS_PHY_UA_LEN		SAS_PHY_PHYMASK_LEN
9204 #else
9205 #define	SAS_PHY_UA_LEN		SAS_PHY_NAME_LEN
9206 #endif
9207 typedef struct impl_sas_phymap {
9208 	dev_info_t			*phymap_self;
9209 
9210 	kmutex_t			phymap_lock;
9211 	damap_t				*phymap_dam;
9212 	void				*phymap_phy2name;
9213 	ddi_soft_state_bystr		*phymap_name2phys;	/* bitset */
9214 	ddi_soft_state_bystr		*phymap_name2ua;
9215 	ddi_soft_state_bystr		*phymap_ua2name;
9216 
9217 	/* Noisy phy information - ensure forward progress for noisy phys */
9218 	int				phymap_phy_max;		/* max phy# */
9219 	int				phymap_reports;		/* per period */
9220 	int				phymap_reports_max;	/* scales */
9221 	int				phymap_phys_noisy;	/* detected */
9222 
9223 	/* These are for callbacks to the consumer. */
9224 	sas_phymap_activate_cb_t	phymap_acp;
9225 	sas_phymap_deactivate_cb_t	phymap_dcp;
9226 	void				*phymap_private;
9227 } impl_sas_phymap_t;
9228 
9229 /* Detect noisy phy: max changes per stabilization period per phy. */
9230 static int sas_phymap_phy_max_factor = 16;
9231 
9232 /*
9233  * Convert bitset into a unit-address string. The maximum string length would
9234  * be the maximum number of phys, rounded up by 4 and divided by 4.
9235  */
9236 static void
9237 sas_phymap_bitset2phymaskua(bitset_t *phys, char *buf)
9238 {
9239 	char			*ptr;
9240 	int			grp;
9241 	int			cur;
9242 	uint_t			bit;
9243 
9244 	bit = roundup(SAS_PHY_NPHY, 4);
9245 	grp = 4;
9246 	ptr = buf;
9247 	cur = 0;
9248 	do {
9249 		bit -= 1;
9250 		grp -= 1;
9251 		if (bitset_in_set(phys, bit)) {
9252 			cur |= (1 << grp);
9253 		}
9254 		if (grp == 0) {
9255 			grp = 4;
9256 			if (cur || ptr != buf) {
9257 				*ptr++ = "0123456789abcdef"[cur];
9258 				*ptr = 0;
9259 			}
9260 			cur = 0;
9261 		}
9262 	} while (bit != 0);
9263 	if (ptr == buf) {
9264 		*ptr++ = '0';
9265 		*ptr = 0;
9266 	}
9267 }
9268 
9269 static void
9270 sas_phymap_config(void *arg, damap_t *phydam, damap_id_list_t dl)
9271 {
9272 	impl_sas_phymap_t	*phymap = (impl_sas_phymap_t *)arg;
9273 	char			*context = damap_name(phymap->phymap_dam);
9274 	int			pairs;
9275 	damap_id_t		phyid;
9276 	char			*damn;
9277 	char			*name;
9278 	bitset_t		*phys;
9279 	char			*ua;
9280 	void			*ua_priv;
9281 
9282 	ASSERT(context);
9283 
9284 	mutex_enter(&phymap->phymap_lock);
9285 	phymap->phymap_reports = phymap->phymap_phys_noisy = 0;
9286 
9287 	/* count the number of local,remove pairs we need to config */
9288 	for (pairs = 0, phyid = damap_id_next(phydam, dl, NODAM);
9289 	    phyid != NODAM;
9290 	    phyid = damap_id_next(phydam, dl, phyid))
9291 		pairs++;
9292 	SCSI_HBA_LOG((_LOGPHY, phymap->phymap_self, NULL,
9293 	    "%s: config %d local,remote pairs", context, pairs));
9294 
9295 	for (phyid = damap_id_next(phydam, dl, NODAM);
9296 	    phyid != NODAM;
9297 	    phyid = damap_id_next(phydam, dl, phyid)) {
9298 		/* Get the name ("local,remote" address string) from damap. */
9299 		damn = damap_id2addr(phydam, phyid);
9300 
9301 		/* Get the bitset of phys currently forming the port. */
9302 		phys = ddi_soft_state_bystr_get(phymap->phymap_name2phys, damn);
9303 		if (phys == NULL) {
9304 			SCSI_HBA_LOG((_LOG_NF(WARN),
9305 			    "%s: %s: no phys", context, damn));
9306 			continue;
9307 		}
9308 
9309 		/* allocate, get, and initialize name index of name2ua map */
9310 		if (ddi_soft_state_bystr_zalloc(
9311 		    phymap->phymap_name2ua, damn) != DDI_SUCCESS) {
9312 			SCSI_HBA_LOG((_LOG_NF(WARN),
9313 			    "%s: %s: failed name2ua alloc", context, damn));
9314 			continue;
9315 		}
9316 		ua = ddi_soft_state_bystr_get(phymap->phymap_name2ua, damn);
9317 		if (ua == NULL) {
9318 			SCSI_HBA_LOG((_LOG_NF(WARN),
9319 			    "%s: %s: no name2ua", context, damn));
9320 			continue;
9321 		}
9322 		sas_phymap_bitset2phymaskua(phys, ua);		/* set ua */
9323 
9324 		/* see if phymask ua index already allocated in ua2name map */
9325 		name = ddi_soft_state_bystr_get(phymap->phymap_ua2name, ua);
9326 		if (name) {
9327 			/*
9328 			 * The 'phymask' sas_phymap_bitset2phymaskua ua is
9329 			 * already in use. This means that original phys have
9330 			 * formed into a new port, and that the original port
9331 			 * still exists (it has migrated to some completely
9332 			 * different set of phys). In this corner-case we use
9333 			 * "local,remote" name as a 'temporary' unit address.
9334 			 * Reset ua in name2ua map.
9335 			 */
9336 			(void) strlcpy(ua, damn, SAS_PHY_NAME_LEN);
9337 
9338 			name = ddi_soft_state_bystr_get(
9339 			    phymap->phymap_ua2name, ua);
9340 			if (name) {
9341 				/* The "local,remote" ua should be new... */
9342 				SCSI_HBA_LOG((_LOG_NF(WARN),
9343 				    "%s: %s ua already configured",
9344 				    context, ua));
9345 				continue;
9346 			}
9347 		}
9348 
9349 		/* allocate, get, and init ua index of ua2name map */
9350 		if (ddi_soft_state_bystr_zalloc(
9351 		    phymap->phymap_ua2name, ua) != DDI_SUCCESS) {
9352 			ddi_soft_state_bystr_free(
9353 			    phymap->phymap_name2ua, damn);
9354 			SCSI_HBA_LOG((_LOG_NF(WARN),
9355 			    "%s: %s: failed ua2name alloc",
9356 			    context, damn));
9357 			continue;
9358 		}
9359 		name = ddi_soft_state_bystr_get(
9360 		    phymap->phymap_ua2name, ua);
9361 		if (name == NULL) {
9362 			ddi_soft_state_bystr_free(
9363 			    phymap->phymap_name2ua, damn);
9364 			SCSI_HBA_LOG((_LOG_NF(WARN),
9365 			    "%s: %s: no ua2name", context, ua));
9366 			continue;
9367 		}
9368 
9369 		/* set name in ua2name map */
9370 		(void) strlcpy(name, damn, SAS_PHY_NAME_LEN);
9371 
9372 		SCSI_HBA_LOG((_LOGPHY, phymap->phymap_self, NULL,
9373 		    "%s: %s: ua %s: activate", context, damn, ua));
9374 		if (phymap->phymap_acp) {
9375 			mutex_exit(&phymap->phymap_lock);
9376 			ua_priv = NULL;
9377 			(phymap->phymap_acp)(phymap->phymap_private,
9378 			    ua, &ua_priv);
9379 			mutex_enter(&phymap->phymap_lock);
9380 
9381 			damap_id_priv_set(phydam, phyid, ua_priv);
9382 		}
9383 		SCSI_HBA_LOG((_LOGPHY, phymap->phymap_self, NULL,
9384 		    "%s: %s: ua %s: activate complete", context, damn, ua));
9385 	}
9386 
9387 	SCSI_HBA_LOG((_LOGPHY, phymap->phymap_self, NULL,
9388 	    "%s: config complete", context));
9389 	mutex_exit(&phymap->phymap_lock);
9390 }
9391 
9392 /*ARGSUSED*/
9393 static void
9394 sas_phymap_unconfig(void *arg, damap_t *phydam, damap_id_list_t dl)
9395 {
9396 	impl_sas_phymap_t	*phymap = (impl_sas_phymap_t *)arg;
9397 	char			*context = damap_name(phymap->phymap_dam);
9398 	int			pairs;
9399 	damap_id_t		phyid;
9400 	char			*damn;
9401 	char			*ua;
9402 	void			*ua_priv;
9403 
9404 	ASSERT(context);
9405 
9406 	mutex_enter(&phymap->phymap_lock);
9407 	phymap->phymap_reports = phymap->phymap_phys_noisy = 0;
9408 
9409 	/* count the number of local,remove pairs we need to unconfig */
9410 	for (pairs = 0, phyid = damap_id_next(phydam, dl, NODAM);
9411 	    phyid != NODAM;
9412 	    phyid = damap_id_next(phydam, dl, phyid))
9413 		pairs++;
9414 	SCSI_HBA_LOG((_LOGPHY, phymap->phymap_self, NULL,
9415 	    "%s: unconfig %d local,remote pairs", context, pairs));
9416 
9417 	for (phyid = damap_id_next(phydam, dl, NODAM);
9418 	    phyid != NODAM;
9419 	    phyid = damap_id_next(phydam, dl, phyid)) {
9420 		/* Get the name ("local,remote" address string) from damap. */
9421 		damn = damap_id2addr(phydam, phyid);
9422 
9423 		ua = ddi_soft_state_bystr_get(phymap->phymap_name2ua, damn);
9424 		if (ua == NULL) {
9425 			SCSI_HBA_LOG((_LOG_NF(WARN),
9426 			    "%s: %s: no name2ua", context, damn));
9427 			continue;
9428 		}
9429 
9430 		SCSI_HBA_LOG((_LOGPHY, phymap->phymap_self, NULL,
9431 		    "%s: %s: ua %s: deactivate", context, damn, ua));
9432 		if (phymap->phymap_dcp) {
9433 			ua_priv = damap_id_priv_get(phydam, phyid);
9434 			mutex_exit(&phymap->phymap_lock);
9435 			(phymap->phymap_dcp)(phymap->phymap_private,
9436 			    ua, ua_priv);
9437 			mutex_enter(&phymap->phymap_lock);
9438 		}
9439 		SCSI_HBA_LOG((_LOGPHY, phymap->phymap_self, NULL,
9440 		    "%s: %s: ua %s: deactivate complete", context, damn, ua));
9441 
9442 		/* delete ua<->name mappings */
9443 		ddi_soft_state_bystr_free(phymap->phymap_ua2name, ua);
9444 		ddi_soft_state_bystr_free(phymap->phymap_name2ua, damn);
9445 	}
9446 
9447 	SCSI_HBA_LOG((_LOGPHY, phymap->phymap_self, NULL,
9448 	    "%s: unconfig complete", context));
9449 	mutex_exit(&phymap->phymap_lock);
9450 }
9451 
9452 int
9453 sas_phymap_create(dev_info_t *self, clock_t settle,
9454     sas_phymap_mode_t mode, void *mode_argument, void *phymap_priv,
9455     sas_phymap_activate_cb_t  activate_cb,
9456     sas_phymap_deactivate_cb_t deactivate_cb,
9457     sas_phymap_t **handlep)
9458 {
9459 	_NOTE(ARGUNUSED(mode_argument));
9460 	char			context[64];
9461 	impl_sas_phymap_t	*phymap;
9462 
9463 	if (self == NULL || settle == 0 || handlep == NULL)
9464 		return (DDI_FAILURE);
9465 
9466 	if (mode != PHYMAP_MODE_SIMPLE)
9467 		return (DDI_FAILURE);
9468 
9469 	phymap = kmem_zalloc(sizeof (*phymap), KM_SLEEP);
9470 	phymap->phymap_self = self;
9471 	phymap->phymap_reports_max = 1 * sas_phymap_phy_max_factor;
9472 	phymap->phymap_acp = activate_cb;
9473 	phymap->phymap_dcp = deactivate_cb;
9474 	phymap->phymap_private = phymap_priv;
9475 	mutex_init(&phymap->phymap_lock, NULL, MUTEX_DRIVER, NULL);
9476 
9477 	(void) snprintf(context, sizeof (context), "%s%d.phymap",
9478 	    ddi_driver_name(self), ddi_get_instance(self));
9479 	SCSI_HBA_LOG((_LOGPHY, self, NULL, "%s", context));
9480 
9481 	if (damap_create(context, SAS_PHY_NPHY, DAMAP_REPORT_PERADDR, settle,
9482 	    NULL, NULL, NULL,
9483 	    phymap, sas_phymap_config, sas_phymap_unconfig,
9484 	    &phymap->phymap_dam) != DAM_SUCCESS)
9485 		goto fail;
9486 
9487 	if (ddi_soft_state_init(&phymap->phymap_phy2name,
9488 	    SAS_PHY_NAME_LEN, SAS_PHY_NPHY) != 0)
9489 		goto fail;
9490 
9491 	if (ddi_soft_state_bystr_init(&phymap->phymap_name2phys,
9492 	    sizeof (bitset_t), SAS_PHY_NPHY) != 0)
9493 		goto fail;
9494 
9495 	if (ddi_soft_state_bystr_init(&phymap->phymap_name2ua,
9496 	    SAS_PHY_UA_LEN, SAS_PHY_NPHY) != 0)
9497 		goto fail;
9498 	if (ddi_soft_state_bystr_init(&phymap->phymap_ua2name,
9499 	    SAS_PHY_NAME_LEN, SAS_PHY_NPHY) != 0)
9500 		goto fail;
9501 
9502 	*handlep = (sas_phymap_t *)phymap;
9503 	return (DDI_SUCCESS);
9504 
9505 fail:	sas_phymap_destroy((sas_phymap_t *)phymap);
9506 	*handlep = NULL;
9507 	return (DDI_FAILURE);
9508 }
9509 
9510 void
9511 sas_phymap_destroy(sas_phymap_t *handle)
9512 {
9513 	impl_sas_phymap_t	*phymap = (impl_sas_phymap_t *)handle;
9514 	char			*context;
9515 
9516 	context = phymap->phymap_dam ?
9517 	    damap_name(phymap->phymap_dam) : "unknown";
9518 	SCSI_HBA_LOG((_LOGPHY, phymap->phymap_self, NULL, "%s", context));
9519 
9520 	if (phymap->phymap_ua2name)
9521 		ddi_soft_state_bystr_fini(&phymap->phymap_ua2name);
9522 	if (phymap->phymap_name2ua)
9523 		ddi_soft_state_bystr_fini(&phymap->phymap_name2ua);
9524 
9525 	if (phymap->phymap_name2phys)
9526 		ddi_soft_state_bystr_fini(&phymap->phymap_name2phys);
9527 
9528 	if (phymap->phymap_phy2name)
9529 		ddi_soft_state_fini(&phymap->phymap_phy2name);
9530 
9531 	if (phymap->phymap_dam)
9532 		damap_destroy(phymap->phymap_dam);
9533 	mutex_destroy(&phymap->phymap_lock);
9534 	kmem_free(phymap, sizeof (*phymap));
9535 }
9536 
9537 
9538 int
9539 sas_phymap_phy_add(sas_phymap_t *handle,
9540     int phy, uint64_t local, uint64_t remote)
9541 {
9542 	impl_sas_phymap_t	*phymap = (impl_sas_phymap_t *)handle;
9543 	char			*context = damap_name(phymap->phymap_dam);
9544 	char			port[SAS_PHY_NAME_LEN];
9545 	char			*name;
9546 	bitset_t		*phys;
9547 	int			phy2name_allocated = 0;
9548 	int			name2phys_allocated = 0;
9549 	int			rv;
9550 
9551 	/* Create the SAS port name from the local and remote addresses. */
9552 	(void) snprintf(port, SAS_PHY_NAME_LEN, SAS_PHY_NAME_FMT,
9553 	    local, remote);
9554 
9555 	mutex_enter(&phymap->phymap_lock);
9556 	SCSI_HBA_LOG((_LOGPHY, phymap->phymap_self, NULL, "%s: %s: add phy %d",
9557 	    context, port, phy));
9558 
9559 	/* Check for conflict in phy2name map */
9560 	name = ddi_get_soft_state(phymap->phymap_phy2name, phy);
9561 	if (name) {
9562 		if (strcmp(name, port) != 0)
9563 			SCSI_HBA_LOG((_LOG_NF(WARN), "%s: %s: add phy %d: "
9564 			    "already in %s", context, port, phy, name));
9565 		else
9566 			SCSI_HBA_LOG((_LOG_NF(WARN), "%s: %s: add phy %d: "
9567 			    "duplicate add", context, port, phy));
9568 		mutex_exit(&phymap->phymap_lock);
9569 		return (DDI_FAILURE);
9570 	}
9571 
9572 	/* allocate, get, and initialize phy index of phy2name map */
9573 	if (ddi_soft_state_zalloc(
9574 	    phymap->phymap_phy2name, phy) != DDI_SUCCESS) {
9575 		SCSI_HBA_LOG((_LOG_NF(WARN),
9576 		    "%s: %s: failed phy2name alloc", context, port));
9577 		goto fail;
9578 	}
9579 	name = ddi_get_soft_state(phymap->phymap_phy2name, phy);
9580 	if (name == NULL) {
9581 		SCSI_HBA_LOG((_LOG_NF(WARN),
9582 		    "%s: %s: no phy2name", context, port));
9583 		goto fail;
9584 	}
9585 	phy2name_allocated = 1;
9586 	(void) strlcpy(name, port, SAS_PHY_NAME_LEN);	/* set name */
9587 
9588 	/* Find/alloc, initialize name index of name2phys map */
9589 	phys = ddi_soft_state_bystr_get(phymap->phymap_name2phys, name);
9590 	if (phys == NULL) {
9591 		if (ddi_soft_state_bystr_zalloc(phymap->phymap_name2phys,
9592 		    name) != DDI_SUCCESS) {
9593 			SCSI_HBA_LOG((_LOG_NF(WARN),
9594 			    "%s: %s: failed name2phys alloc", context, name));
9595 			goto fail;
9596 		}
9597 		phys = ddi_soft_state_bystr_get(phymap->phymap_name2phys, name);
9598 		if (phys == NULL) {
9599 			SCSI_HBA_LOG((_LOG_NF(WARN),
9600 			    "%s: %s: no name2phys", context, name));
9601 			goto fail;
9602 		}
9603 		name2phys_allocated = 1;
9604 
9605 		/* Initialize bitset of phys */
9606 		bitset_init(phys);
9607 		bitset_resize(phys, SAS_PHY_NPHY);
9608 
9609 		/* NOTE: no bitset_fini of phys needed */
9610 	}
9611 	ASSERT(phys);
9612 
9613 	/* Reflect 'add' in phys bitset. */
9614 	if (bitset_atomic_test_and_add(phys, phy) < 0) {
9615 		/* It is an error if the phy was already recorded. */
9616 		SCSI_HBA_LOG((_LOG_NF(WARN),
9617 		    "%s: %s: phy bit %d already in port", context, name, phy));
9618 		goto fail;
9619 	}
9620 
9621 	/*
9622 	 * Check to see if we have a new phy_max for this map, and if so
9623 	 * scale phymap_reports_max to the new number of phys.
9624 	 */
9625 	if (phy > phymap->phymap_phy_max) {
9626 		phymap->phymap_phy_max = phy + 1;
9627 		phymap->phymap_reports_max = phymap->phymap_phy_max *
9628 		    sas_phymap_phy_max_factor;
9629 	}
9630 
9631 	/*
9632 	 * If we have not reached phymap_reports_max, start/restart the
9633 	 * activate timer. Otherwise, if phymap->phymap_reports add/rem reports
9634 	 * ever exceeds phymap_reports_max due to noisy phys, then report the
9635 	 * noise and force stabilization by stopping reports into the damap.
9636 	 *
9637 	 * The first config/unconfig callout out of the damap will reset
9638 	 * phymap->phymap_reports.
9639 	 */
9640 	rv = DDI_SUCCESS;
9641 	if (phymap->phymap_reports++ < phymap->phymap_reports_max) {
9642 		if (damap_addr_add(phymap->phymap_dam, name,
9643 		    NULL, NULL, NULL) == DAM_SUCCESS) {
9644 			SCSI_HBA_LOG((_LOGPHY, phymap->phymap_self, NULL,
9645 			    "%s: %s: damap_addr_add", context, name));
9646 		} else {
9647 			SCSI_HBA_LOG((_LOG_NF(WARN),
9648 			    "%s: %s: damap_addr_add failed", context, name));
9649 			rv = DDI_FAILURE;
9650 		}
9651 	} else {
9652 		phymap->phymap_phys_noisy++;
9653 		if (phymap->phymap_phys_noisy == 1)
9654 			SCSI_HBA_LOG((_LOG_NF(WARN),
9655 			    "%s: %s: noisy phys", context, name));
9656 	}
9657 	mutex_exit(&phymap->phymap_lock);
9658 	return (rv);
9659 
9660 fail:	if (phy2name_allocated)
9661 		ddi_soft_state_free(phymap->phymap_phy2name, phy);
9662 	if (name2phys_allocated)
9663 		ddi_soft_state_bystr_free(phymap->phymap_name2phys, name);
9664 	mutex_exit(&phymap->phymap_lock);
9665 	return (DDI_FAILURE);
9666 }
9667 
9668 int
9669 sas_phymap_phy_rem(sas_phymap_t *handle, int phy)
9670 {
9671 	impl_sas_phymap_t	*phymap = (impl_sas_phymap_t *)handle;
9672 	char			*context = damap_name(phymap->phymap_dam);
9673 	char			*name;
9674 	bitset_t		*phys;
9675 	int			rv = DDI_FAILURE;
9676 
9677 	ASSERT(context);
9678 
9679 	mutex_enter(&phymap->phymap_lock);
9680 	phymap->phymap_reports++;
9681 
9682 	/* Find and free phy index of phy2name map */
9683 	name = ddi_get_soft_state(phymap->phymap_phy2name, phy);
9684 	if (name == NULL) {
9685 		SCSI_HBA_LOG((_LOG_NF(WARN), "%s: rem phy %d: never added",
9686 		    context, phy));
9687 		goto fail;
9688 	}
9689 	/* NOTE: always free phy index of phy2name map before return... */
9690 
9691 	SCSI_HBA_LOG((_LOGPHY, phymap->phymap_self, NULL, "%s: %s: rem phy %d",
9692 	    context, name, phy));
9693 
9694 	/* Get bitset of phys currently associated with named port. */
9695 	phys = ddi_soft_state_bystr_get(phymap->phymap_name2phys, name);
9696 	if (phys == NULL) {
9697 		SCSI_HBA_LOG((_LOG_NF(WARN), "%s: %s: name2phys failed",
9698 		    context, name));
9699 		goto fail;
9700 	}
9701 
9702 	/* Reflect 'rem' in phys bitset. */
9703 	if (bitset_atomic_test_and_del(phys, phy) < 0) {
9704 		/* It is an error if the phy wasn't one of the port's phys. */
9705 		SCSI_HBA_LOG((_LOG_NF(WARN),
9706 		    "%s: %s: phy bit %d not in port", context, name, phy));
9707 		goto fail;
9708 	}
9709 
9710 	/* If this was the last phy in the port, start the deactivate timer. */
9711 	if (bitset_is_null(phys) &&
9712 	    (phymap->phymap_reports++ < phymap->phymap_reports_max)) {
9713 		if (damap_addr_del(phymap->phymap_dam, name) == DAM_SUCCESS) {
9714 			SCSI_HBA_LOG((_LOGPHY, phymap->phymap_self, NULL,
9715 			    "%s: %s: damap_addr_del", context, name));
9716 		} else {
9717 			SCSI_HBA_LOG((_LOG_NF(WARN),
9718 			    "%s: %s: damap_addr_del failure", context, name));
9719 			goto fail;
9720 		}
9721 	}
9722 	rv = DDI_SUCCESS;
9723 
9724 	/* free phy index of phy2name map */
9725 fail:	if (name)
9726 		ddi_soft_state_free(phymap->phymap_phy2name, phy); /* free */
9727 	mutex_exit(&phymap->phymap_lock);
9728 	return (rv);
9729 }
9730 
9731 char *
9732 sas_phymap_lookup_ua(sas_phymap_t *handle, uint64_t local, uint64_t remote)
9733 {
9734 	impl_sas_phymap_t	*phymap = (impl_sas_phymap_t *)handle;
9735 	char			*context = damap_name(phymap->phymap_dam);
9736 	char			name[SAS_PHY_NAME_LEN];
9737 	char			*ua;
9738 
9739 	ASSERT(context);
9740 
9741 	(void) snprintf(name, SAS_PHY_NAME_LEN, SAS_PHY_NAME_FMT,
9742 	    local, remote);
9743 
9744 	mutex_enter(&phymap->phymap_lock);
9745 	ua = ddi_soft_state_bystr_get(phymap->phymap_name2ua, name);
9746 	SCSI_HBA_LOG((_LOG(3), phymap->phymap_self, NULL,
9747 	    "%s: %s: ua %s", context, name, ua ? ua : "NULL"));
9748 	mutex_exit(&phymap->phymap_lock);
9749 	return (ua);
9750 }
9751 
9752 void *
9753 sas_phymap_lookup_uapriv(sas_phymap_t *handle, char *ua)
9754 {
9755 	impl_sas_phymap_t	*phymap = (impl_sas_phymap_t *)handle;
9756 	char			*context = damap_name(phymap->phymap_dam);
9757 	char			*name;
9758 	damap_id_t		phyid;
9759 	void			*ua_priv = NULL;
9760 
9761 	ASSERT(context);
9762 
9763 	mutex_enter(&phymap->phymap_lock);
9764 	name = ddi_soft_state_bystr_get(phymap->phymap_ua2name, ua);
9765 	if (name) {
9766 		phyid = damap_lookup(phymap->phymap_dam, name);
9767 		if (phyid != NODAM) {
9768 			ua_priv = damap_id_priv_get(phymap->phymap_dam, phyid);
9769 			damap_id_rele(phymap->phymap_dam, phyid);
9770 		}
9771 	}
9772 
9773 	SCSI_HBA_LOG((_LOG(3), phymap->phymap_self, NULL,
9774 	    "%s: %s: ua %s ua_priv %p", context, name,
9775 	    ua ? ua : "NULL", ua_priv));
9776 	mutex_exit(&phymap->phymap_lock);
9777 	return (ua_priv);
9778 }
9779 
9780 int
9781 sas_phymap_uahasphys(sas_phymap_t *handle, char *ua)
9782 {
9783 	impl_sas_phymap_t	*phymap = (impl_sas_phymap_t *)handle;
9784 	char			*name;
9785 	bitset_t		*phys;
9786 	int			n = 0;
9787 
9788 	mutex_enter(&phymap->phymap_lock);
9789 	name = ddi_soft_state_bystr_get(phymap->phymap_ua2name, ua);
9790 	if (name) {
9791 		phys = ddi_soft_state_bystr_get(phymap->phymap_name2phys, name);
9792 		if (phys)
9793 			n = bitset_is_null(phys) ? 0 : 1;
9794 	}
9795 	mutex_exit(&phymap->phymap_lock);
9796 	return (n);
9797 }
9798 
9799 sas_phymap_phys_t *
9800 sas_phymap_ua2phys(sas_phymap_t *handle, char *ua)
9801 {
9802 	impl_sas_phymap_t	*phymap = (impl_sas_phymap_t *)handle;
9803 	char			*name;
9804 	bitset_t		*phys;
9805 	bitset_t		*cphys = NULL;
9806 
9807 	mutex_enter(&phymap->phymap_lock);
9808 	name = ddi_soft_state_bystr_get(phymap->phymap_ua2name, ua);
9809 	if (name == NULL)
9810 		goto fail;
9811 
9812 	phys = ddi_soft_state_bystr_get(phymap->phymap_name2phys, name);
9813 	if (phys == NULL)
9814 		goto fail;
9815 
9816 	/* dup the phys and return */
9817 	cphys = kmem_alloc(sizeof (*cphys), KM_SLEEP);
9818 	bitset_init(cphys);
9819 	bitset_resize(cphys, SAS_PHY_NPHY);
9820 	bitset_copy(phys, cphys);
9821 
9822 fail:	mutex_exit(&phymap->phymap_lock);
9823 	return ((sas_phymap_phys_t *)cphys);
9824 }
9825 
9826 int
9827 sas_phymap_phys_next(sas_phymap_phys_t *phys)
9828 {
9829 	bitset_t	*cphys = (bitset_t *)phys;
9830 	int		phy;
9831 
9832 	phy = bitset_find(cphys);
9833 	if (phy != -1)
9834 		bitset_del(cphys, phy);
9835 	return (phy);
9836 }
9837 
9838 void
9839 sas_phymap_phys_free(sas_phymap_phys_t *phys)
9840 {
9841 	bitset_t	*cphys = (bitset_t *)phys;
9842 
9843 	if (cphys) {
9844 		bitset_fini(cphys);
9845 		kmem_free(cphys, sizeof (*cphys));
9846 	}
9847 }
9848 
9849 char *
9850 sas_phymap_phy2ua(sas_phymap_t *handle, int phy)
9851 {
9852 	impl_sas_phymap_t	*phymap = (impl_sas_phymap_t *)handle;
9853 	char			*name;
9854 	char			*ua;
9855 	char			*rua = NULL;
9856 
9857 	mutex_enter(&phymap->phymap_lock);
9858 	name = ddi_get_soft_state(phymap->phymap_phy2name, phy);
9859 	if (name == NULL)
9860 		goto fail;
9861 	ua = ddi_soft_state_bystr_get(phymap->phymap_name2ua, name);
9862 	if (ua == NULL)
9863 		goto fail;
9864 
9865 	/* dup the ua and return */
9866 	rua = strdup(ua);
9867 
9868 fail:	mutex_exit(&phymap->phymap_lock);
9869 	return (rua);
9870 }
9871 
9872 void
9873 sas_phymap_ua_free(char *ua)
9874 {
9875 	if (ua)
9876 		strfree(ua);
9877 }
9878