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