xref: /titanic_51/usr/src/uts/common/os/devcfg.c (revision cbdcbd056f15c9c9fd82d5543b3a502677c4d391)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/note.h>
27 #include <sys/t_lock.h>
28 #include <sys/cmn_err.h>
29 #include <sys/instance.h>
30 #include <sys/conf.h>
31 #include <sys/stat.h>
32 #include <sys/ddi.h>
33 #include <sys/hwconf.h>
34 #include <sys/sunddi.h>
35 #include <sys/sunndi.h>
36 #include <sys/ddi_impldefs.h>
37 #include <sys/ndi_impldefs.h>
38 #include <sys/modctl.h>
39 #include <sys/contract/device_impl.h>
40 #include <sys/dacf.h>
41 #include <sys/promif.h>
42 #include <sys/pci.h>
43 #include <sys/cpuvar.h>
44 #include <sys/pathname.h>
45 #include <sys/taskq.h>
46 #include <sys/sysevent.h>
47 #include <sys/sunmdi.h>
48 #include <sys/stream.h>
49 #include <sys/strsubr.h>
50 #include <sys/fs/snode.h>
51 #include <sys/fs/dv_node.h>
52 #include <sys/reboot.h>
53 #include <sys/sysmacros.h>
54 #include <sys/systm.h>
55 #include <sys/fs/sdev_impl.h>
56 #include <sys/sunldi.h>
57 #include <sys/sunldi_impl.h>
58 #include <sys/bootprops.h>
59 
60 
61 #if defined(__i386) || defined(__amd64)
62 #if !defined(__xpv)
63 #include <sys/iommulib.h>
64 #endif
65 #endif
66 
67 #ifdef DEBUG
68 int ddidebug = DDI_AUDIT;
69 #else
70 int ddidebug = 0;
71 #endif
72 
73 #define	MT_CONFIG_OP	0
74 #define	MT_UNCONFIG_OP	1
75 
76 /* Multi-threaded configuration */
77 struct mt_config_handle {
78 	kmutex_t mtc_lock;
79 	kcondvar_t mtc_cv;
80 	int mtc_thr_count;
81 	dev_info_t *mtc_pdip;	/* parent dip for mt_config_children */
82 	dev_info_t **mtc_fdip;	/* "a" dip where unconfigure failed */
83 	major_t mtc_parmajor;	/* parent major for mt_config_driver */
84 	major_t mtc_major;
85 	int mtc_flags;
86 	int mtc_op;		/* config or unconfig */
87 	int mtc_error;		/* operation error */
88 	struct brevq_node **mtc_brevqp;	/* outstanding branch events queue */
89 #ifdef DEBUG
90 	int total_time;
91 	timestruc_t start_time;
92 #endif /* DEBUG */
93 };
94 
95 struct devi_nodeid {
96 	pnode_t nodeid;
97 	dev_info_t *dip;
98 	struct devi_nodeid *next;
99 };
100 
101 struct devi_nodeid_list {
102 	kmutex_t dno_lock;		/* Protects other fields */
103 	struct devi_nodeid *dno_head;	/* list of devi nodeid elements */
104 	struct devi_nodeid *dno_free;	/* Free list */
105 	uint_t dno_list_length;		/* number of dips in list */
106 };
107 
108 /* used to keep track of branch remove events to be generated */
109 struct brevq_node {
110 	char *brn_deviname;
111 	struct brevq_node *brn_sibling;
112 	struct brevq_node *brn_child;
113 };
114 
115 static struct devi_nodeid_list devi_nodeid_list;
116 static struct devi_nodeid_list *devimap = &devi_nodeid_list;
117 
118 /*
119  * Well known nodes which are attached first at boot time.
120  */
121 dev_info_t *top_devinfo;		/* root of device tree */
122 dev_info_t *options_dip;
123 dev_info_t *pseudo_dip;
124 dev_info_t *clone_dip;
125 dev_info_t *scsi_vhci_dip;		/* MPXIO dip */
126 major_t clone_major;
127 
128 /*
129  * A non-global zone's /dev is derived from the device tree.
130  * This generation number serves to indicate when a zone's
131  * /dev may need to be updated.
132  */
133 volatile ulong_t devtree_gen;		/* generation number */
134 
135 /* block all future dev_info state changes */
136 hrtime_t volatile devinfo_freeze = 0;
137 
138 /* number of dev_info attaches/detaches currently in progress */
139 static ulong_t devinfo_attach_detach = 0;
140 
141 extern int	sys_shutdown;
142 extern kmutex_t global_vhci_lock;
143 
144 /* bitset of DS_SYSAVAIL & DS_RECONFIG - no races, no lock */
145 static int devname_state = 0;
146 
147 /*
148  * The devinfo snapshot cache and related variables.
149  * The only field in the di_cache structure that needs initialization
150  * is the mutex (cache_lock). However, since this is an adaptive mutex
151  * (MUTEX_DEFAULT) - it is automatically initialized by being allocated
152  * in zeroed memory (static storage class). Therefore no explicit
153  * initialization of the di_cache structure is needed.
154  */
155 struct di_cache	di_cache = {1};
156 int		di_cache_debug = 0;
157 
158 /* For ddvis, which needs pseudo children under PCI */
159 int pci_allow_pseudo_children = 0;
160 
161 /* Allow path-oriented alias driver binding on driver.conf enumerated nodes */
162 int driver_conf_allow_path_alias = 1;
163 
164 /*
165  * The following switch is for service people, in case a
166  * 3rd party driver depends on identify(9e) being called.
167  */
168 int identify_9e = 0;
169 
170 int mtc_off;					/* turn off mt config */
171 
172 int quiesce_debug = 0;
173 
174 static kmem_cache_t *ddi_node_cache;		/* devinfo node cache */
175 static devinfo_log_header_t *devinfo_audit_log;	/* devinfo log */
176 static int devinfo_log_size;			/* size in pages */
177 
178 static int lookup_compatible(dev_info_t *, uint_t);
179 static char *encode_composite_string(char **, uint_t, size_t *, uint_t);
180 static void link_to_driver_list(dev_info_t *);
181 static void unlink_from_driver_list(dev_info_t *);
182 static void add_to_dn_list(struct devnames *, dev_info_t *);
183 static void remove_from_dn_list(struct devnames *, dev_info_t *);
184 static dev_info_t *find_duplicate_child();
185 static void add_global_props(dev_info_t *);
186 static void remove_global_props(dev_info_t *);
187 static int uninit_node(dev_info_t *);
188 static void da_log_init(void);
189 static void da_log_enter(dev_info_t *);
190 static int walk_devs(dev_info_t *, int (*f)(dev_info_t *, void *), void *, int);
191 static int reset_nexus_flags(dev_info_t *, void *);
192 static void ddi_optimize_dtree(dev_info_t *);
193 static int is_leaf_node(dev_info_t *);
194 static struct mt_config_handle *mt_config_init(dev_info_t *, dev_info_t **,
195     int, major_t, int, struct brevq_node **);
196 static void mt_config_children(struct mt_config_handle *);
197 static void mt_config_driver(struct mt_config_handle *);
198 static int mt_config_fini(struct mt_config_handle *);
199 static int devi_unconfig_common(dev_info_t *, dev_info_t **, int, major_t,
200     struct brevq_node **);
201 static int
202 ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
203     dev_info_t **childp, int flags);
204 static void i_link_vhci_node(dev_info_t *);
205 static void ndi_devi_exit_and_wait(dev_info_t *dip,
206     int circular, clock_t end_time);
207 static int ndi_devi_unbind_driver(dev_info_t *dip);
208 
209 static void i_ddi_check_retire(dev_info_t *dip);
210 
211 static void quiesce_one_device(dev_info_t *, void *);
212 
213 /*
214  * dev_info cache and node management
215  */
216 
217 /* initialize dev_info node cache */
218 void
219 i_ddi_node_cache_init()
220 {
221 	ASSERT(ddi_node_cache == NULL);
222 	ddi_node_cache = kmem_cache_create("dev_info_node_cache",
223 	    sizeof (struct dev_info), 0, NULL, NULL, NULL, NULL, NULL, 0);
224 
225 	if (ddidebug & DDI_AUDIT)
226 		da_log_init();
227 }
228 
229 /*
230  * Allocating a dev_info node, callable from interrupt context with KM_NOSLEEP
231  * The allocated node has a reference count of 0.
232  */
233 dev_info_t *
234 i_ddi_alloc_node(dev_info_t *pdip, char *node_name, pnode_t nodeid,
235     int instance, ddi_prop_t *sys_prop, int flag)
236 {
237 	struct dev_info *devi;
238 	struct devi_nodeid *elem;
239 	static char failed[] = "i_ddi_alloc_node: out of memory";
240 
241 	ASSERT(node_name != NULL);
242 
243 	if ((devi = kmem_cache_alloc(ddi_node_cache, flag)) == NULL) {
244 		cmn_err(CE_NOTE, failed);
245 		return (NULL);
246 	}
247 
248 	bzero(devi, sizeof (struct dev_info));
249 
250 	if (devinfo_audit_log) {
251 		devi->devi_audit = kmem_zalloc(sizeof (devinfo_audit_t), flag);
252 		if (devi->devi_audit == NULL)
253 			goto fail;
254 	}
255 
256 	if ((devi->devi_node_name = i_ddi_strdup(node_name, flag)) == NULL)
257 		goto fail;
258 
259 	/* default binding name is node name */
260 	devi->devi_binding_name = devi->devi_node_name;
261 	devi->devi_major = DDI_MAJOR_T_NONE;	/* unbound by default */
262 
263 	/*
264 	 * Make a copy of system property
265 	 */
266 	if (sys_prop &&
267 	    (devi->devi_sys_prop_ptr = i_ddi_prop_list_dup(sys_prop, flag))
268 	    == NULL)
269 		goto fail;
270 
271 	/*
272 	 * Assign devi_nodeid, devi_node_class, devi_node_attributes
273 	 * according to the following algorithm:
274 	 *
275 	 * nodeid arg			node class		node attributes
276 	 *
277 	 * DEVI_PSEUDO_NODEID		DDI_NC_PSEUDO		A
278 	 * DEVI_SID_NODEID		DDI_NC_PSEUDO		A,P
279 	 * DEVI_SID_HIDDEN_NODEID	DDI_NC_PSEUDO		A,P,H
280 	 * DEVI_SID_HP_NODEID		DDI_NC_PSEUDO		A,P,h
281 	 * DEVI_SID_HP_HIDDEN_NODEID	DDI_NC_PSEUDO		A,P,H,h
282 	 * other			DDI_NC_PROM		P
283 	 *
284 	 * Where A = DDI_AUTO_ASSIGNED_NODEID (auto-assign a nodeid)
285 	 * and	 P = DDI_PERSISTENT
286 	 * and	 H = DDI_HIDDEN_NODE
287 	 * and	 h = DDI_HOTPLUG_NODE
288 	 *
289 	 * auto-assigned nodeids are also auto-freed.
290 	 */
291 	devi->devi_node_attributes = 0;
292 	switch (nodeid) {
293 	case DEVI_SID_HIDDEN_NODEID:
294 		devi->devi_node_attributes |= DDI_HIDDEN_NODE;
295 		goto sid;
296 
297 	case DEVI_SID_HP_NODEID:
298 		devi->devi_node_attributes |= DDI_HOTPLUG_NODE;
299 		goto sid;
300 
301 	case DEVI_SID_HP_HIDDEN_NODEID:
302 		devi->devi_node_attributes |= DDI_HIDDEN_NODE;
303 		devi->devi_node_attributes |= DDI_HOTPLUG_NODE;
304 		goto sid;
305 
306 	case DEVI_SID_NODEID:
307 sid:		devi->devi_node_attributes |= DDI_PERSISTENT;
308 		if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL)
309 			goto fail;
310 		/*FALLTHROUGH*/
311 
312 	case DEVI_PSEUDO_NODEID:
313 		devi->devi_node_attributes |= DDI_AUTO_ASSIGNED_NODEID;
314 		devi->devi_node_class = DDI_NC_PSEUDO;
315 		if (impl_ddi_alloc_nodeid(&devi->devi_nodeid)) {
316 			panic("i_ddi_alloc_node: out of nodeids");
317 			/*NOTREACHED*/
318 		}
319 		break;
320 
321 	default:
322 		if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL)
323 			goto fail;
324 
325 		/*
326 		 * the nodetype is 'prom', try to 'take' the nodeid now.
327 		 * This requires memory allocation, so check for failure.
328 		 */
329 		if (impl_ddi_take_nodeid(nodeid, flag) != 0) {
330 			kmem_free(elem, sizeof (*elem));
331 			goto fail;
332 		}
333 
334 		devi->devi_nodeid = nodeid;
335 		devi->devi_node_class = DDI_NC_PROM;
336 		devi->devi_node_attributes = DDI_PERSISTENT;
337 		break;
338 	}
339 
340 	if (ndi_dev_is_persistent_node((dev_info_t *)devi)) {
341 		mutex_enter(&devimap->dno_lock);
342 		elem->next = devimap->dno_free;
343 		devimap->dno_free = elem;
344 		mutex_exit(&devimap->dno_lock);
345 	}
346 
347 	/*
348 	 * Instance is normally initialized to -1. In a few special
349 	 * cases, the caller may specify an instance (e.g. CPU nodes).
350 	 */
351 	devi->devi_instance = instance;
352 
353 	/*
354 	 * set parent and bus_ctl parent
355 	 */
356 	devi->devi_parent = DEVI(pdip);
357 	devi->devi_bus_ctl = DEVI(pdip);
358 
359 	NDI_CONFIG_DEBUG((CE_CONT,
360 	    "i_ddi_alloc_node: name=%s id=%d\n", node_name, devi->devi_nodeid));
361 
362 	cv_init(&(devi->devi_cv), NULL, CV_DEFAULT, NULL);
363 	mutex_init(&(devi->devi_lock), NULL, MUTEX_DEFAULT, NULL);
364 	mutex_init(&(devi->devi_pm_lock), NULL, MUTEX_DEFAULT, NULL);
365 	mutex_init(&(devi->devi_pm_busy_lock), NULL, MUTEX_DEFAULT, NULL);
366 
367 	RIO_TRACE((CE_NOTE, "i_ddi_alloc_node: Initing contract fields: "
368 	    "dip=%p, name=%s", (void *)devi, node_name));
369 
370 	mutex_init(&(devi->devi_ct_lock), NULL, MUTEX_DEFAULT, NULL);
371 	cv_init(&(devi->devi_ct_cv), NULL, CV_DEFAULT, NULL);
372 	devi->devi_ct_count = -1;	/* counter not in use if -1 */
373 	list_create(&(devi->devi_ct), sizeof (cont_device_t),
374 	    offsetof(cont_device_t, cond_next));
375 
376 	i_ddi_set_node_state((dev_info_t *)devi, DS_PROTO);
377 	da_log_enter((dev_info_t *)devi);
378 	return ((dev_info_t *)devi);
379 
380 fail:
381 	if (devi->devi_sys_prop_ptr)
382 		i_ddi_prop_list_delete(devi->devi_sys_prop_ptr);
383 	if (devi->devi_node_name)
384 		kmem_free(devi->devi_node_name, strlen(node_name) + 1);
385 	if (devi->devi_audit)
386 		kmem_free(devi->devi_audit, sizeof (devinfo_audit_t));
387 	kmem_cache_free(ddi_node_cache, devi);
388 	cmn_err(CE_NOTE, failed);
389 	return (NULL);
390 }
391 
392 /*
393  * free a dev_info structure.
394  * NB. Not callable from interrupt since impl_ddi_free_nodeid may block.
395  */
396 void
397 i_ddi_free_node(dev_info_t *dip)
398 {
399 	struct dev_info *devi = DEVI(dip);
400 	struct devi_nodeid *elem;
401 #if defined(__x86) && !defined(__xpv)
402 	gfx_entry_t *gfxp;
403 	extern void *gfx_devinfo_list;
404 #endif
405 
406 	ASSERT(devi->devi_ref == 0);
407 	ASSERT(devi->devi_addr == NULL);
408 	ASSERT(devi->devi_node_state == DS_PROTO);
409 	ASSERT(devi->devi_child == NULL);
410 	ASSERT(devi->devi_hp_hdlp == NULL);
411 
412 #if defined(__x86) && !defined(__xpv)
413 	for (gfxp = gfx_devinfo_list; gfxp; gfxp = gfxp->g_next) {
414 		if (gfxp->g_dip == dip) {
415 			gfxp->g_dip = NULL;
416 			while (gfxp->g_ref)
417 				;
418 		}
419 	}
420 	membar_producer();
421 #endif
422 	/* free devi_addr_buf allocated by ddi_set_name_addr() */
423 	if (devi->devi_addr_buf)
424 		kmem_free(devi->devi_addr_buf, 2 * MAXNAMELEN);
425 
426 	if (i_ndi_dev_is_auto_assigned_node(dip))
427 		impl_ddi_free_nodeid(DEVI(dip)->devi_nodeid);
428 
429 	if (ndi_dev_is_persistent_node(dip)) {
430 		mutex_enter(&devimap->dno_lock);
431 		ASSERT(devimap->dno_free);
432 		elem = devimap->dno_free;
433 		devimap->dno_free = elem->next;
434 		mutex_exit(&devimap->dno_lock);
435 		kmem_free(elem, sizeof (*elem));
436 	}
437 
438 	if (DEVI(dip)->devi_compat_names)
439 		kmem_free(DEVI(dip)->devi_compat_names,
440 		    DEVI(dip)->devi_compat_length);
441 	if (DEVI(dip)->devi_rebinding_name)
442 		kmem_free(DEVI(dip)->devi_rebinding_name,
443 		    strlen(DEVI(dip)->devi_rebinding_name) + 1);
444 
445 	ddi_prop_remove_all(dip);	/* remove driver properties */
446 	if (devi->devi_sys_prop_ptr)
447 		i_ddi_prop_list_delete(devi->devi_sys_prop_ptr);
448 	if (devi->devi_hw_prop_ptr)
449 		i_ddi_prop_list_delete(devi->devi_hw_prop_ptr);
450 
451 	if (DEVI(dip)->devi_devid_str)
452 		ddi_devid_str_free(DEVI(dip)->devi_devid_str);
453 
454 	i_ddi_set_node_state(dip, DS_INVAL);
455 	da_log_enter(dip);
456 	if (devi->devi_audit) {
457 		kmem_free(devi->devi_audit, sizeof (devinfo_audit_t));
458 	}
459 	if (devi->devi_device_class)
460 		kmem_free(devi->devi_device_class,
461 		    strlen(devi->devi_device_class) + 1);
462 	cv_destroy(&(devi->devi_cv));
463 	mutex_destroy(&(devi->devi_lock));
464 	mutex_destroy(&(devi->devi_pm_lock));
465 	mutex_destroy(&(devi->devi_pm_busy_lock));
466 
467 	RIO_TRACE((CE_NOTE, "i_ddi_free_node: destroying contract fields: "
468 	    "dip=%p", (void *)dip));
469 	contract_device_remove_dip(dip);
470 	ASSERT(devi->devi_ct_count == -1);
471 	ASSERT(list_is_empty(&(devi->devi_ct)));
472 	cv_destroy(&(devi->devi_ct_cv));
473 	list_destroy(&(devi->devi_ct));
474 	/* free this last since contract_device_remove_dip() uses it */
475 	mutex_destroy(&(devi->devi_ct_lock));
476 	RIO_TRACE((CE_NOTE, "i_ddi_free_node: destroyed all contract fields: "
477 	    "dip=%p, name=%s", (void *)dip, devi->devi_node_name));
478 
479 	kmem_free(devi->devi_node_name, strlen(devi->devi_node_name) + 1);
480 
481 	kmem_cache_free(ddi_node_cache, devi);
482 }
483 
484 
485 /*
486  * Node state transitions
487  */
488 
489 /*
490  * Change the node name
491  */
492 int
493 ndi_devi_set_nodename(dev_info_t *dip, char *name, int flags)
494 {
495 	_NOTE(ARGUNUSED(flags))
496 	char *nname, *oname;
497 
498 	ASSERT(dip && name);
499 
500 	oname = DEVI(dip)->devi_node_name;
501 	if (strcmp(oname, name) == 0)
502 		return (DDI_SUCCESS);
503 
504 	/*
505 	 * pcicfg_fix_ethernet requires a name change after node
506 	 * is linked into the tree. When pcicfg is fixed, we
507 	 * should only allow name change in DS_PROTO state.
508 	 */
509 	if (i_ddi_node_state(dip) >= DS_BOUND) {
510 		/*
511 		 * Don't allow name change once node is bound
512 		 */
513 		cmn_err(CE_NOTE,
514 		    "ndi_devi_set_nodename: node already bound dip = %p,"
515 		    " %s -> %s", (void *)dip, ddi_node_name(dip), name);
516 		return (NDI_FAILURE);
517 	}
518 
519 	nname = i_ddi_strdup(name, KM_SLEEP);
520 	DEVI(dip)->devi_node_name = nname;
521 	i_ddi_set_binding_name(dip, nname);
522 	kmem_free(oname, strlen(oname) + 1);
523 
524 	da_log_enter(dip);
525 	return (NDI_SUCCESS);
526 }
527 
528 void
529 i_ddi_add_devimap(dev_info_t *dip)
530 {
531 	struct devi_nodeid *elem;
532 
533 	ASSERT(dip);
534 
535 	if (!ndi_dev_is_persistent_node(dip))
536 		return;
537 
538 	ASSERT(ddi_get_parent(dip) == NULL || (DEVI_VHCI_NODE(dip)) ||
539 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
540 
541 	mutex_enter(&devimap->dno_lock);
542 
543 	ASSERT(devimap->dno_free);
544 
545 	elem = devimap->dno_free;
546 	devimap->dno_free = elem->next;
547 
548 	elem->nodeid = ddi_get_nodeid(dip);
549 	elem->dip = dip;
550 	elem->next = devimap->dno_head;
551 	devimap->dno_head = elem;
552 
553 	devimap->dno_list_length++;
554 
555 	mutex_exit(&devimap->dno_lock);
556 }
557 
558 static int
559 i_ddi_remove_devimap(dev_info_t *dip)
560 {
561 	struct devi_nodeid *prev, *elem;
562 	static const char *fcn = "i_ddi_remove_devimap";
563 
564 	ASSERT(dip);
565 
566 	if (!ndi_dev_is_persistent_node(dip))
567 		return (DDI_SUCCESS);
568 
569 	mutex_enter(&devimap->dno_lock);
570 
571 	/*
572 	 * The following check is done with dno_lock held
573 	 * to prevent race between dip removal and
574 	 * e_ddi_prom_node_to_dip()
575 	 */
576 	if (e_ddi_devi_holdcnt(dip)) {
577 		mutex_exit(&devimap->dno_lock);
578 		return (DDI_FAILURE);
579 	}
580 
581 	ASSERT(devimap->dno_head);
582 	ASSERT(devimap->dno_list_length > 0);
583 
584 	prev = NULL;
585 	for (elem = devimap->dno_head; elem; elem = elem->next) {
586 		if (elem->dip == dip) {
587 			ASSERT(elem->nodeid == ddi_get_nodeid(dip));
588 			break;
589 		}
590 		prev = elem;
591 	}
592 
593 	if (elem && prev)
594 		prev->next = elem->next;
595 	else if (elem)
596 		devimap->dno_head = elem->next;
597 	else
598 		panic("%s: devinfo node(%p) not found",
599 		    fcn, (void *)dip);
600 
601 	devimap->dno_list_length--;
602 
603 	elem->nodeid = 0;
604 	elem->dip = NULL;
605 
606 	elem->next = devimap->dno_free;
607 	devimap->dno_free = elem;
608 
609 	mutex_exit(&devimap->dno_lock);
610 
611 	return (DDI_SUCCESS);
612 }
613 
614 /*
615  * Link this node into the devinfo tree and add to orphan list
616  * Not callable from interrupt context
617  */
618 static void
619 link_node(dev_info_t *dip)
620 {
621 	struct dev_info *devi = DEVI(dip);
622 	struct dev_info *parent = devi->devi_parent;
623 	dev_info_t **dipp;
624 
625 	ASSERT(parent);	/* never called for root node */
626 
627 	NDI_CONFIG_DEBUG((CE_CONT, "link_node: parent = %s child = %s\n",
628 	    parent->devi_node_name, devi->devi_node_name));
629 
630 	/*
631 	 * Hold the global_vhci_lock before linking any direct
632 	 * children of rootnex driver. This special lock protects
633 	 * linking and unlinking for rootnext direct children.
634 	 */
635 	if ((dev_info_t *)parent == ddi_root_node())
636 		mutex_enter(&global_vhci_lock);
637 
638 	/*
639 	 * attach the node to end of the list unless the node is already there
640 	 */
641 	dipp = (dev_info_t **)(&DEVI(parent)->devi_child);
642 	while (*dipp && (*dipp != dip)) {
643 		dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling);
644 	}
645 	ASSERT(*dipp == NULL);	/* node is not linked */
646 
647 	/*
648 	 * Now that we are in the tree, update the devi-nodeid map.
649 	 */
650 	i_ddi_add_devimap(dip);
651 
652 	/*
653 	 * This is a temporary workaround for Bug 4618861.
654 	 * We keep the scsi_vhci nexus node on the left side of the devinfo
655 	 * tree (under the root nexus driver), so that virtual nodes under
656 	 * scsi_vhci will be SUSPENDed first and RESUMEd last.	This ensures
657 	 * that the pHCI nodes are active during times when their clients
658 	 * may be depending on them.  This workaround embodies the knowledge
659 	 * that system PM and CPR both traverse the tree left-to-right during
660 	 * SUSPEND and right-to-left during RESUME.
661 	 * Extending the workaround to IB Nexus/VHCI
662 	 * driver also.
663 	 */
664 	if (strcmp(devi->devi_binding_name, "scsi_vhci") == 0) {
665 		/* Add scsi_vhci to beginning of list */
666 		ASSERT((dev_info_t *)parent == top_devinfo);
667 		/* scsi_vhci under rootnex */
668 		devi->devi_sibling = parent->devi_child;
669 		parent->devi_child = devi;
670 	} else if (strcmp(devi->devi_binding_name, "ib") == 0) {
671 		i_link_vhci_node(dip);
672 	} else {
673 		/* Add to end of list */
674 		*dipp = dip;
675 		DEVI(dip)->devi_sibling = NULL;
676 	}
677 
678 	/*
679 	 * Release the global_vhci_lock before linking any direct
680 	 * children of rootnex driver.
681 	 */
682 	if ((dev_info_t *)parent == ddi_root_node())
683 		mutex_exit(&global_vhci_lock);
684 
685 	/* persistent nodes go on orphan list */
686 	if (ndi_dev_is_persistent_node(dip))
687 		add_to_dn_list(&orphanlist, dip);
688 }
689 
690 /*
691  * Unlink this node from the devinfo tree
692  */
693 static int
694 unlink_node(dev_info_t *dip)
695 {
696 	struct dev_info *devi = DEVI(dip);
697 	struct dev_info *parent = devi->devi_parent;
698 	dev_info_t **dipp;
699 	ddi_hp_cn_handle_t *hdlp;
700 
701 	ASSERT(parent != NULL);
702 	ASSERT(devi->devi_node_state == DS_LINKED);
703 
704 	NDI_CONFIG_DEBUG((CE_CONT, "unlink_node: name = %s\n",
705 	    ddi_node_name(dip)));
706 
707 	/* check references */
708 	if (devi->devi_ref || i_ddi_remove_devimap(dip) != DDI_SUCCESS)
709 		return (DDI_FAILURE);
710 
711 	/*
712 	 * Hold the global_vhci_lock before linking any direct
713 	 * children of rootnex driver.
714 	 */
715 	if ((dev_info_t *)parent == ddi_root_node())
716 		mutex_enter(&global_vhci_lock);
717 
718 	dipp = (dev_info_t **)(&DEVI(parent)->devi_child);
719 	while (*dipp && (*dipp != dip)) {
720 		dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling);
721 	}
722 	if (*dipp) {
723 		*dipp = (dev_info_t *)(devi->devi_sibling);
724 		devi->devi_sibling = NULL;
725 	} else {
726 		NDI_CONFIG_DEBUG((CE_NOTE, "unlink_node: %s not linked",
727 		    devi->devi_node_name));
728 	}
729 
730 	/*
731 	 * Release the global_vhci_lock before linking any direct
732 	 * children of rootnex driver.
733 	 */
734 	if ((dev_info_t *)parent == ddi_root_node())
735 		mutex_exit(&global_vhci_lock);
736 
737 	/* Remove node from orphan list */
738 	if (ndi_dev_is_persistent_node(dip)) {
739 		remove_from_dn_list(&orphanlist, dip);
740 	}
741 
742 	/* Update parent's hotplug handle list */
743 	for (hdlp = DEVI(parent)->devi_hp_hdlp; hdlp; hdlp = hdlp->next) {
744 		if (hdlp->cn_info.cn_child == dip)
745 			hdlp->cn_info.cn_child = NULL;
746 	}
747 	return (DDI_SUCCESS);
748 }
749 
750 /*
751  * Bind this devinfo node to a driver. If compat is NON-NULL, try that first.
752  * Else, use the node-name.
753  *
754  * NOTE: IEEE1275 specifies that nodename should be tried before compatible.
755  *	Solaris implementation binds nodename after compatible.
756  *
757  * If we find a binding,
758  * - set the binding name to the string,
759  * - set major number to driver major
760  *
761  * If we don't find a binding,
762  * - return failure
763  */
764 static int
765 bind_node(dev_info_t *dip)
766 {
767 	char *p = NULL;
768 	major_t major = DDI_MAJOR_T_NONE;
769 	struct dev_info *devi = DEVI(dip);
770 	dev_info_t *parent = ddi_get_parent(dip);
771 
772 	ASSERT(devi->devi_node_state == DS_LINKED);
773 
774 	NDI_CONFIG_DEBUG((CE_CONT, "bind_node: 0x%p(name = %s)\n",
775 	    (void *)dip, ddi_node_name(dip)));
776 
777 	mutex_enter(&DEVI(dip)->devi_lock);
778 	if (DEVI(dip)->devi_flags & DEVI_NO_BIND) {
779 		mutex_exit(&DEVI(dip)->devi_lock);
780 		return (DDI_FAILURE);
781 	}
782 	mutex_exit(&DEVI(dip)->devi_lock);
783 
784 	/* find the driver with most specific binding using compatible */
785 	major = ddi_compatible_driver_major(dip, &p);
786 	if (major == DDI_MAJOR_T_NONE)
787 		return (DDI_FAILURE);
788 
789 	devi->devi_major = major;
790 	if (p != NULL) {
791 		i_ddi_set_binding_name(dip, p);
792 		NDI_CONFIG_DEBUG((CE_CONT, "bind_node: %s bound to %s\n",
793 		    devi->devi_node_name, p));
794 	}
795 
796 	/* Link node to per-driver list */
797 	link_to_driver_list(dip);
798 
799 	/*
800 	 * reset parent flag so that nexus will merge .conf props
801 	 */
802 	if (ndi_dev_is_persistent_node(dip)) {
803 		mutex_enter(&DEVI(parent)->devi_lock);
804 		DEVI(parent)->devi_flags &=
805 		    ~(DEVI_ATTACHED_CHILDREN|DEVI_MADE_CHILDREN);
806 		mutex_exit(&DEVI(parent)->devi_lock);
807 	}
808 	return (DDI_SUCCESS);
809 }
810 
811 /*
812  * Unbind this devinfo node
813  * Called before the node is destroyed or driver is removed from system
814  */
815 static int
816 unbind_node(dev_info_t *dip)
817 {
818 	ASSERT(DEVI(dip)->devi_node_state == DS_BOUND);
819 	ASSERT(DEVI(dip)->devi_major != DDI_MAJOR_T_NONE);
820 
821 	/* check references */
822 	if (DEVI(dip)->devi_ref)
823 		return (DDI_FAILURE);
824 
825 	NDI_CONFIG_DEBUG((CE_CONT, "unbind_node: 0x%p(name = %s)\n",
826 	    (void *)dip, ddi_node_name(dip)));
827 
828 	unlink_from_driver_list(dip);
829 
830 	DEVI(dip)->devi_major = DDI_MAJOR_T_NONE;
831 	DEVI(dip)->devi_binding_name = DEVI(dip)->devi_node_name;
832 	return (DDI_SUCCESS);
833 }
834 
835 /*
836  * Initialize a node: calls the parent nexus' bus_ctl ops to do the operation.
837  * Must hold parent and per-driver list while calling this function.
838  * A successful init_node() returns with an active ndi_hold_devi() hold on
839  * the parent.
840  */
841 static int
842 init_node(dev_info_t *dip)
843 {
844 	int error;
845 	dev_info_t *pdip = ddi_get_parent(dip);
846 	int (*f)(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *, void *);
847 	char *path;
848 	major_t	major;
849 
850 	ASSERT(i_ddi_node_state(dip) == DS_BOUND);
851 
852 	/* should be DS_READY except for pcmcia ... */
853 	ASSERT(i_ddi_node_state(pdip) >= DS_PROBED);
854 
855 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
856 	(void) ddi_pathname(dip, path);
857 	NDI_CONFIG_DEBUG((CE_CONT, "init_node: entry: path %s 0x%p\n",
858 	    path, (void *)dip));
859 
860 	/*
861 	 * The parent must have a bus_ctl operation.
862 	 */
863 	if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
864 	    (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_ctl) == NULL) {
865 		error = DDI_FAILURE;
866 		goto out;
867 	}
868 
869 	add_global_props(dip);
870 
871 	/*
872 	 * Invoke the parent's bus_ctl operation with the DDI_CTLOPS_INITCHILD
873 	 * command to transform the child to canonical form 1. If there
874 	 * is an error, ddi_remove_child should be called, to clean up.
875 	 */
876 	error = (*f)(pdip, pdip, DDI_CTLOPS_INITCHILD, dip, NULL);
877 	if (error != DDI_SUCCESS) {
878 		NDI_CONFIG_DEBUG((CE_CONT, "init_node: %s 0x%p failed\n",
879 		    path, (void *)dip));
880 		remove_global_props(dip);
881 		/* in case nexus driver didn't clear this field */
882 		ddi_set_name_addr(dip, NULL);
883 		error = DDI_FAILURE;
884 		goto out;
885 	}
886 
887 	ndi_hold_devi(pdip);			/* initial hold of parent */
888 
889 	/* recompute path after initchild for @addr information */
890 	(void) ddi_pathname(dip, path);
891 
892 	/* Check for duplicate nodes */
893 	if (find_duplicate_child(pdip, dip) != NULL) {
894 		/*
895 		 * uninit_node() the duplicate - a successful uninit_node()
896 		 * will release inital hold of parent using ndi_rele_devi().
897 		 */
898 		if ((error = uninit_node(dip)) != DDI_SUCCESS) {
899 			ndi_rele_devi(pdip);	/* release initial hold */
900 			cmn_err(CE_WARN, "init_node: uninit of duplicate "
901 			    "node %s failed", path);
902 		}
903 		NDI_CONFIG_DEBUG((CE_CONT, "init_node: duplicate uninit "
904 		    "%s 0x%p%s\n", path, (void *)dip,
905 		    (error == DDI_SUCCESS) ? "" : " failed"));
906 		error = DDI_FAILURE;
907 		goto out;
908 	}
909 
910 	/*
911 	 * Check to see if we have a path-oriented driver alias that overrides
912 	 * the current driver binding. If so, we need to rebind. This check
913 	 * needs to be delayed until after a successful DDI_CTLOPS_INITCHILD,
914 	 * so the unit-address is established on the last component of the path.
915 	 *
916 	 * NOTE: Allowing a path-oriented alias to change the driver binding
917 	 * of a driver.conf node results in non-intuitive property behavior.
918 	 * We provide a tunable (driver_conf_allow_path_alias) to control
919 	 * this behavior. See uninit_node() for more details.
920 	 *
921 	 * NOTE: If you are adding a path-oriented alias for the boot device,
922 	 * and there is mismatch between OBP and the kernel in regard to
923 	 * generic name use, like "disk" .vs. "ssd", then you will need
924 	 * to add a path-oriented alias for both paths.
925 	 */
926 	major = ddi_name_to_major(path);
927 	if (driver_installed(major) && (major != DEVI(dip)->devi_major) &&
928 	    (ndi_dev_is_persistent_node(dip) || driver_conf_allow_path_alias)) {
929 
930 		/* Mark node for rebind processing. */
931 		mutex_enter(&DEVI(dip)->devi_lock);
932 		DEVI(dip)->devi_flags |= DEVI_REBIND;
933 		mutex_exit(&DEVI(dip)->devi_lock);
934 
935 		/*
936 		 * Add an extra hold on the parent to prevent it from ever
937 		 * having a zero devi_ref during the child rebind process.
938 		 * This is necessary to ensure that the parent will never
939 		 * detach(9E) during the rebind.
940 		 */
941 		ndi_hold_devi(pdip);		/* extra hold of parent */
942 
943 		/*
944 		 * uninit_node() current binding - a successful uninit_node()
945 		 * will release extra hold of parent using ndi_rele_devi().
946 		 */
947 		if ((error = uninit_node(dip)) != DDI_SUCCESS) {
948 			ndi_rele_devi(pdip);	/* release extra hold */
949 			ndi_rele_devi(pdip);	/* release initial hold */
950 			cmn_err(CE_WARN, "init_node: uninit for rebind "
951 			    "of node %s failed", path);
952 			goto out;
953 		}
954 
955 		/* Unbind: demote the node back to DS_LINKED.  */
956 		if ((error = ndi_devi_unbind_driver(dip)) != DDI_SUCCESS) {
957 			ndi_rele_devi(pdip);	/* release initial hold */
958 			cmn_err(CE_WARN, "init_node: unbind for rebind "
959 			    "of node %s failed", path);
960 			goto out;
961 		}
962 
963 		/* establish rebinding name */
964 		if (DEVI(dip)->devi_rebinding_name == NULL)
965 			DEVI(dip)->devi_rebinding_name =
966 			    i_ddi_strdup(path, KM_SLEEP);
967 
968 		/*
969 		 * Now that we are demoted and marked for rebind, repromote.
970 		 * We need to do this in steps, instead of just calling
971 		 * ddi_initchild, so that we can redo the merge operation
972 		 * after we are rebound to the path-bound driver.
973 		 *
974 		 * Start by rebinding node to the path-bound driver.
975 		 */
976 		if ((error = ndi_devi_bind_driver(dip, 0)) != DDI_SUCCESS) {
977 			ndi_rele_devi(pdip);	/* release initial hold */
978 			cmn_err(CE_WARN, "init_node: rebind "
979 			    "of node %s failed", path);
980 			goto out;
981 		}
982 
983 		/*
984 		 * If the node is not a driver.conf node then merge
985 		 * driver.conf properties from new path-bound driver.conf.
986 		 */
987 		if (ndi_dev_is_persistent_node(dip))
988 			(void) i_ndi_make_spec_children(pdip, 0);
989 
990 		/*
991 		 * Now that we have taken care of merge, repromote back
992 		 * to DS_INITIALIZED.
993 		 */
994 		error = ddi_initchild(pdip, dip);
995 		NDI_CONFIG_DEBUG((CE_CONT, "init_node: rebind "
996 		    "%s 0x%p\n", path, (void *)dip));
997 
998 		/*
999 		 * Release our initial hold. If ddi_initchild() was
1000 		 * successful then it will return with the active hold.
1001 		 */
1002 		ndi_rele_devi(pdip);
1003 		goto out;
1004 	}
1005 
1006 	/*
1007 	 * Apply multi-parent/deep-nexus optimization to the new node
1008 	 */
1009 	DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
1010 	ddi_optimize_dtree(dip);
1011 	error = DDI_SUCCESS;		/* return with active hold */
1012 
1013 out:	if (error != DDI_SUCCESS) {
1014 		/* On failure ensure that DEVI_REBIND is cleared */
1015 		mutex_enter(&DEVI(dip)->devi_lock);
1016 		DEVI(dip)->devi_flags &= ~DEVI_REBIND;
1017 		mutex_exit(&DEVI(dip)->devi_lock);
1018 	}
1019 	kmem_free(path, MAXPATHLEN);
1020 	return (error);
1021 }
1022 
1023 /*
1024  * Uninitialize node
1025  * The per-driver list must be held busy during the call.
1026  * A successful uninit_node() releases the init_node() hold on
1027  * the parent by calling ndi_rele_devi().
1028  */
1029 static int
1030 uninit_node(dev_info_t *dip)
1031 {
1032 	int node_state_entry;
1033 	dev_info_t *pdip;
1034 	struct dev_ops *ops;
1035 	int (*f)();
1036 	int error;
1037 	char *addr;
1038 
1039 	/*
1040 	 * Don't check for references here or else a ref-counted
1041 	 * dip cannot be downgraded by the framework.
1042 	 */
1043 	node_state_entry = i_ddi_node_state(dip);
1044 	ASSERT((node_state_entry == DS_BOUND) ||
1045 	    (node_state_entry == DS_INITIALIZED));
1046 	pdip = ddi_get_parent(dip);
1047 	ASSERT(pdip);
1048 
1049 	NDI_CONFIG_DEBUG((CE_CONT, "uninit_node: 0x%p(%s%d)\n",
1050 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1051 
1052 	if (((ops = ddi_get_driver(pdip)) == NULL) ||
1053 	    (ops->devo_bus_ops == NULL) ||
1054 	    ((f = ops->devo_bus_ops->bus_ctl) == NULL)) {
1055 		return (DDI_FAILURE);
1056 	}
1057 
1058 	/*
1059 	 * save the @addr prior to DDI_CTLOPS_UNINITCHILD for use in
1060 	 * freeing the instance if it succeeds.
1061 	 */
1062 	if (node_state_entry == DS_INITIALIZED) {
1063 		addr = ddi_get_name_addr(dip);
1064 		if (addr)
1065 			addr = i_ddi_strdup(addr, KM_SLEEP);
1066 	} else {
1067 		addr = NULL;
1068 	}
1069 
1070 	error = (*f)(pdip, pdip, DDI_CTLOPS_UNINITCHILD, dip, (void *)NULL);
1071 	if (error == DDI_SUCCESS) {
1072 		/* ensure that devids are unregistered */
1073 		if (DEVI(dip)->devi_flags & DEVI_REGISTERED_DEVID) {
1074 			DEVI(dip)->devi_flags &= ~DEVI_REGISTERED_DEVID;
1075 			ddi_devid_unregister(dip);
1076 		}
1077 
1078 		/* if uninitchild forgot to set devi_addr to NULL do it now */
1079 		ddi_set_name_addr(dip, NULL);
1080 
1081 		/*
1082 		 * Free instance number. This is a no-op if instance has
1083 		 * been kept by probe_node().  Avoid free when we are called
1084 		 * from init_node (DS_BOUND) because the instance has not yet
1085 		 * been assigned.
1086 		 */
1087 		if (node_state_entry == DS_INITIALIZED) {
1088 			e_ddi_free_instance(dip, addr);
1089 			DEVI(dip)->devi_instance = -1;
1090 		}
1091 
1092 		/* release the init_node hold */
1093 		ndi_rele_devi(pdip);
1094 
1095 		remove_global_props(dip);
1096 
1097 		/*
1098 		 * NOTE: The decision on whether to allow a path-oriented
1099 		 * rebind of a driver.conf enumerated node is made by
1100 		 * init_node() based on driver_conf_allow_path_alias. The
1101 		 * rebind code below prevents deletion of system properties
1102 		 * on driver.conf nodes.
1103 		 *
1104 		 * When driver_conf_allow_path_alias is set, property behavior
1105 		 * on rebound driver.conf file is non-intuitive. For a
1106 		 * driver.conf node, the unit-address properties come from
1107 		 * the driver.conf file as system properties. Removing system
1108 		 * properties from a driver.conf node makes the node
1109 		 * useless (we get node without unit-address properties) - so
1110 		 * we leave system properties in place. The result is a node
1111 		 * where system properties come from the node being rebound,
1112 		 * and global properties come from the driver.conf file
1113 		 * of the driver we are rebinding to.  If we could determine
1114 		 * that the path-oriented alias driver.conf file defined a
1115 		 * node at the same unit address, it would be best to use
1116 		 * that node and avoid the non-intuitive property behavior.
1117 		 * Unfortunately, the current "merge" code does not support
1118 		 * this, so we live with the non-intuitive property behavior.
1119 		 */
1120 		if (!((ndi_dev_is_persistent_node(dip) == 0) &&
1121 		    (DEVI(dip)->devi_flags & DEVI_REBIND)))
1122 			e_ddi_prop_remove_all(dip);
1123 	} else {
1124 		NDI_CONFIG_DEBUG((CE_CONT, "uninit_node failed: 0x%p(%s%d)\n",
1125 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1126 	}
1127 
1128 	if (addr)
1129 		kmem_free(addr, strlen(addr) + 1);
1130 	return (error);
1131 }
1132 
1133 /*
1134  * Invoke driver's probe entry point to probe for existence of hardware.
1135  * Keep instance permanent for successful probe and leaf nodes.
1136  *
1137  * Per-driver list must be held busy while calling this function.
1138  */
1139 static int
1140 probe_node(dev_info_t *dip)
1141 {
1142 	int rv;
1143 
1144 	ASSERT(i_ddi_node_state(dip) == DS_INITIALIZED);
1145 
1146 	NDI_CONFIG_DEBUG((CE_CONT, "probe_node: 0x%p(%s%d)\n",
1147 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1148 
1149 	/* temporarily hold the driver while we probe */
1150 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
1151 	if (DEVI(dip)->devi_ops == NULL) {
1152 		NDI_CONFIG_DEBUG((CE_CONT,
1153 		    "probe_node: 0x%p(%s%d) cannot load driver\n",
1154 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1155 		return (DDI_FAILURE);
1156 	}
1157 
1158 	if (identify_9e != 0)
1159 		(void) devi_identify(dip);
1160 
1161 	rv = devi_probe(dip);
1162 
1163 	/* release the driver now that probe is complete */
1164 	ndi_rele_driver(dip);
1165 	DEVI(dip)->devi_ops = NULL;
1166 
1167 	switch (rv) {
1168 	case DDI_PROBE_SUCCESS:			/* found */
1169 	case DDI_PROBE_DONTCARE:		/* ddi_dev_is_sid */
1170 		e_ddi_keep_instance(dip);	/* persist instance */
1171 		rv = DDI_SUCCESS;
1172 		break;
1173 
1174 	case DDI_PROBE_PARTIAL:			/* maybe later */
1175 	case DDI_PROBE_FAILURE:			/* not found */
1176 		NDI_CONFIG_DEBUG((CE_CONT,
1177 		    "probe_node: 0x%p(%s%d) no hardware found%s\n",
1178 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip),
1179 		    (rv == DDI_PROBE_PARTIAL) ? " yet" : ""));
1180 		rv = DDI_FAILURE;
1181 		break;
1182 
1183 	default:
1184 #ifdef	DEBUG
1185 		cmn_err(CE_WARN, "probe_node: %s%d: illegal probe(9E) value",
1186 		    ddi_driver_name(dip), ddi_get_instance(dip));
1187 #endif	/* DEBUG */
1188 		rv = DDI_FAILURE;
1189 		break;
1190 	}
1191 	return (rv);
1192 }
1193 
1194 /*
1195  * Unprobe a node. Simply reset the node state.
1196  * Per-driver list must be held busy while calling this function.
1197  */
1198 static int
1199 unprobe_node(dev_info_t *dip)
1200 {
1201 	ASSERT(i_ddi_node_state(dip) == DS_PROBED);
1202 
1203 	/*
1204 	 * Don't check for references here or else a ref-counted
1205 	 * dip cannot be downgraded by the framework.
1206 	 */
1207 
1208 	NDI_CONFIG_DEBUG((CE_CONT, "unprobe_node: 0x%p(name = %s)\n",
1209 	    (void *)dip, ddi_node_name(dip)));
1210 	return (DDI_SUCCESS);
1211 }
1212 
1213 /*
1214  * Attach devinfo node.
1215  * Per-driver list must be held busy.
1216  */
1217 static int
1218 attach_node(dev_info_t *dip)
1219 {
1220 	int rv;
1221 
1222 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
1223 	ASSERT(i_ddi_node_state(dip) == DS_PROBED);
1224 
1225 	NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d)\n",
1226 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1227 
1228 	/*
1229 	 * Tell mpxio framework that a node is about to online.
1230 	 */
1231 	if ((rv = mdi_devi_online(dip, 0)) != NDI_SUCCESS) {
1232 		return (DDI_FAILURE);
1233 	}
1234 
1235 	/* no recursive attachment */
1236 	ASSERT(DEVI(dip)->devi_ops == NULL);
1237 
1238 	/*
1239 	 * Hold driver the node is bound to.
1240 	 */
1241 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
1242 	if (DEVI(dip)->devi_ops == NULL) {
1243 		/*
1244 		 * We were able to load driver for probing, so we should
1245 		 * not get here unless something really bad happened.
1246 		 */
1247 		cmn_err(CE_WARN, "attach_node: no driver for major %d",
1248 		    DEVI(dip)->devi_major);
1249 		return (DDI_FAILURE);
1250 	}
1251 
1252 	if (NEXUS_DRV(DEVI(dip)->devi_ops))
1253 		DEVI(dip)->devi_taskq = ddi_taskq_create(dip,
1254 		    "nexus_enum_tq", 1,
1255 		    TASKQ_DEFAULTPRI, 0);
1256 
1257 	mutex_enter(&(DEVI(dip)->devi_lock));
1258 	DEVI_SET_ATTACHING(dip);
1259 	DEVI_SET_NEED_RESET(dip);
1260 	mutex_exit(&(DEVI(dip)->devi_lock));
1261 
1262 	rv = devi_attach(dip, DDI_ATTACH);
1263 
1264 	mutex_enter(&(DEVI(dip)->devi_lock));
1265 	DEVI_CLR_ATTACHING(dip);
1266 
1267 	if (rv != DDI_SUCCESS) {
1268 		DEVI_CLR_NEED_RESET(dip);
1269 		mutex_exit(&DEVI(dip)->devi_lock);
1270 
1271 		/*
1272 		 * Cleanup dacf reservations
1273 		 */
1274 		mutex_enter(&dacf_lock);
1275 		dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH);
1276 		dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH);
1277 		mutex_exit(&dacf_lock);
1278 		if (DEVI(dip)->devi_taskq)
1279 			ddi_taskq_destroy(DEVI(dip)->devi_taskq);
1280 		ddi_remove_minor_node(dip, NULL);
1281 
1282 		/* release the driver if attach failed */
1283 		ndi_rele_driver(dip);
1284 		DEVI(dip)->devi_ops = NULL;
1285 		NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d) failed\n",
1286 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1287 		return (DDI_FAILURE);
1288 	} else
1289 		mutex_exit(&DEVI(dip)->devi_lock);
1290 
1291 	/* successful attach, return with driver held */
1292 
1293 	return (DDI_SUCCESS);
1294 }
1295 
1296 /*
1297  * Detach devinfo node.
1298  * Per-driver list must be held busy.
1299  */
1300 static int
1301 detach_node(dev_info_t *dip, uint_t flag)
1302 {
1303 	struct devnames	*dnp;
1304 	int		rv;
1305 
1306 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
1307 	ASSERT(i_ddi_node_state(dip) == DS_ATTACHED);
1308 
1309 	/* check references */
1310 	if (DEVI(dip)->devi_ref)
1311 		return (DDI_FAILURE);
1312 
1313 	NDI_CONFIG_DEBUG((CE_CONT, "detach_node: 0x%p(%s%d)\n",
1314 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1315 
1316 	/*
1317 	 * NOTE: If we are processing a pHCI node then the calling code
1318 	 * must detect this and ndi_devi_enter() in (vHCI, parent(pHCI))
1319 	 * order unless pHCI and vHCI are siblings.  Code paths leading
1320 	 * here that must ensure this ordering include:
1321 	 * unconfig_immediate_children(), devi_unconfig_one(),
1322 	 * ndi_devi_unconfig_one(), ndi_devi_offline().
1323 	 */
1324 	ASSERT(!MDI_PHCI(dip) ||
1325 	    (ddi_get_parent(mdi_devi_get_vdip(dip)) == ddi_get_parent(dip)) ||
1326 	    DEVI_BUSY_OWNED(mdi_devi_get_vdip(dip)));
1327 
1328 	/* Offline the device node with the mpxio framework. */
1329 	if (mdi_devi_offline(dip, flag) != NDI_SUCCESS) {
1330 		return (DDI_FAILURE);
1331 	}
1332 
1333 	/* drain the taskq */
1334 	if (DEVI(dip)->devi_taskq)
1335 		ddi_taskq_wait(DEVI(dip)->devi_taskq);
1336 
1337 	rv = devi_detach(dip, DDI_DETACH);
1338 
1339 	if (rv != DDI_SUCCESS) {
1340 		NDI_CONFIG_DEBUG((CE_CONT,
1341 		    "detach_node: 0x%p(%s%d) failed\n",
1342 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1343 		return (DDI_FAILURE);
1344 	}
1345 
1346 	mutex_enter(&(DEVI(dip)->devi_lock));
1347 	DEVI_CLR_NEED_RESET(dip);
1348 	mutex_exit(&(DEVI(dip)->devi_lock));
1349 
1350 #if defined(__i386) || defined(__amd64)
1351 #if !defined(__xpv)
1352 	/*
1353 	 * Close any iommulib mediated linkage to an IOMMU
1354 	 */
1355 	iommulib_nex_close(dip);
1356 #endif
1357 #endif
1358 
1359 	/* destroy the taskq */
1360 	if (DEVI(dip)->devi_taskq) {
1361 		ddi_taskq_destroy(DEVI(dip)->devi_taskq);
1362 		DEVI(dip)->devi_taskq = NULL;
1363 	}
1364 
1365 	/* Cleanup dacf reservations */
1366 	mutex_enter(&dacf_lock);
1367 	dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH);
1368 	dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH);
1369 	mutex_exit(&dacf_lock);
1370 
1371 	/* remove any additional flavors that were added */
1372 	if (DEVI(dip)->devi_flavorv_n > 1 && DEVI(dip)->devi_flavorv != NULL) {
1373 		kmem_free(DEVI(dip)->devi_flavorv,
1374 		    (DEVI(dip)->devi_flavorv_n - 1) * sizeof (void *));
1375 		DEVI(dip)->devi_flavorv = NULL;
1376 	}
1377 
1378 	/* Remove properties and minor nodes in case driver forgots */
1379 	ddi_remove_minor_node(dip, NULL);
1380 	ddi_prop_remove_all(dip);
1381 
1382 	/* a detached node can't have attached or .conf children */
1383 	mutex_enter(&DEVI(dip)->devi_lock);
1384 	DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN|DEVI_ATTACHED_CHILDREN);
1385 	mutex_exit(&DEVI(dip)->devi_lock);
1386 
1387 	/*
1388 	 * If the instance has successfully detached in detach_driver() context,
1389 	 * clear DN_DRIVER_HELD for correct ddi_hold_installed_driver()
1390 	 * behavior. Consumers like qassociate() depend on this (via clnopen()).
1391 	 */
1392 	if (flag & NDI_DETACH_DRIVER) {
1393 		dnp = &(devnamesp[DEVI(dip)->devi_major]);
1394 		LOCK_DEV_OPS(&dnp->dn_lock);
1395 		dnp->dn_flags &= ~DN_DRIVER_HELD;
1396 		UNLOCK_DEV_OPS(&dnp->dn_lock);
1397 	}
1398 
1399 	/* successful detach, release the driver */
1400 	ndi_rele_driver(dip);
1401 	DEVI(dip)->devi_ops = NULL;
1402 	return (DDI_SUCCESS);
1403 }
1404 
1405 /*
1406  * Run dacf post_attach routines
1407  */
1408 static int
1409 postattach_node(dev_info_t *dip)
1410 {
1411 	int rval;
1412 
1413 	/*
1414 	 * For hotplug busses like USB, it's possible that devices
1415 	 * are removed but dip is still around. We don't want to
1416 	 * run dacf routines as part of detach failure recovery.
1417 	 *
1418 	 * Pretend success until we figure out how to prevent
1419 	 * access to such devinfo nodes.
1420 	 */
1421 	if (DEVI_IS_DEVICE_REMOVED(dip))
1422 		return (DDI_SUCCESS);
1423 
1424 	/*
1425 	 * if dacf_postattach failed, report it to the framework
1426 	 * so that it can be retried later at the open time.
1427 	 */
1428 	mutex_enter(&dacf_lock);
1429 	rval = dacfc_postattach(dip);
1430 	mutex_exit(&dacf_lock);
1431 
1432 	/*
1433 	 * Plumbing during postattach may fail because of the
1434 	 * underlying device is not ready. This will fail ndi_devi_config()
1435 	 * in dv_filldir() and a warning message is issued. The message
1436 	 * from here will explain what happened
1437 	 */
1438 	if (rval != DACF_SUCCESS) {
1439 		cmn_err(CE_WARN, "Postattach failed for %s%d\n",
1440 		    ddi_driver_name(dip), ddi_get_instance(dip));
1441 		return (DDI_FAILURE);
1442 	}
1443 
1444 	return (DDI_SUCCESS);
1445 }
1446 
1447 /*
1448  * Run dacf pre-detach routines
1449  */
1450 static int
1451 predetach_node(dev_info_t *dip, uint_t flag)
1452 {
1453 	int ret;
1454 
1455 	/*
1456 	 * Don't auto-detach if DDI_FORCEATTACH or DDI_NO_AUTODETACH
1457 	 * properties are set.
1458 	 */
1459 	if (flag & NDI_AUTODETACH) {
1460 		struct devnames *dnp;
1461 		int pflag = DDI_PROP_NOTPROM | DDI_PROP_DONTPASS;
1462 
1463 		if ((ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1464 		    pflag, DDI_FORCEATTACH, 0) == 1) ||
1465 		    (ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1466 		    pflag, DDI_NO_AUTODETACH, 0) == 1))
1467 			return (DDI_FAILURE);
1468 
1469 		/* check for driver global version of DDI_NO_AUTODETACH */
1470 		dnp = &devnamesp[DEVI(dip)->devi_major];
1471 		LOCK_DEV_OPS(&dnp->dn_lock);
1472 		if (dnp->dn_flags & DN_NO_AUTODETACH) {
1473 			UNLOCK_DEV_OPS(&dnp->dn_lock);
1474 			return (DDI_FAILURE);
1475 		}
1476 		UNLOCK_DEV_OPS(&dnp->dn_lock);
1477 	}
1478 
1479 	mutex_enter(&dacf_lock);
1480 	ret = dacfc_predetach(dip);
1481 	mutex_exit(&dacf_lock);
1482 
1483 	return (ret);
1484 }
1485 
1486 /*
1487  * Wrapper for making multiple state transitions
1488  */
1489 
1490 /*
1491  * i_ndi_config_node: upgrade dev_info node into a specified state.
1492  * It is a bit tricky because the locking protocol changes before and
1493  * after a node is bound to a driver. All locks are held external to
1494  * this function.
1495  */
1496 int
1497 i_ndi_config_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag)
1498 {
1499 	_NOTE(ARGUNUSED(flag))
1500 	int rv = DDI_SUCCESS;
1501 
1502 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
1503 
1504 	while ((i_ddi_node_state(dip) < state) && (rv == DDI_SUCCESS)) {
1505 
1506 		/* don't allow any more changes to the device tree */
1507 		if (devinfo_freeze) {
1508 			rv = DDI_FAILURE;
1509 			break;
1510 		}
1511 
1512 		switch (i_ddi_node_state(dip)) {
1513 		case DS_PROTO:
1514 			/*
1515 			 * only caller can reference this node, no external
1516 			 * locking needed.
1517 			 */
1518 			link_node(dip);
1519 			i_ddi_set_node_state(dip, DS_LINKED);
1520 			break;
1521 		case DS_LINKED:
1522 			/*
1523 			 * Three code path may attempt to bind a node:
1524 			 * - boot code
1525 			 * - add_drv
1526 			 * - hotplug thread
1527 			 * Boot code is single threaded, add_drv synchronize
1528 			 * on a userland lock, and hotplug synchronize on
1529 			 * hotplug_lk. There could be a race between add_drv
1530 			 * and hotplug thread. We'll live with this until the
1531 			 * conversion to top-down loading.
1532 			 */
1533 			if ((rv = bind_node(dip)) == DDI_SUCCESS)
1534 				i_ddi_set_node_state(dip, DS_BOUND);
1535 
1536 			break;
1537 		case DS_BOUND:
1538 			/*
1539 			 * The following transitions synchronizes on the
1540 			 * per-driver busy changing flag, since we already
1541 			 * have a driver.
1542 			 */
1543 			if ((rv = init_node(dip)) == DDI_SUCCESS)
1544 				i_ddi_set_node_state(dip, DS_INITIALIZED);
1545 			break;
1546 		case DS_INITIALIZED:
1547 			if ((rv = probe_node(dip)) == DDI_SUCCESS)
1548 				i_ddi_set_node_state(dip, DS_PROBED);
1549 			break;
1550 		case DS_PROBED:
1551 			i_ddi_check_retire(dip);
1552 			atomic_add_long(&devinfo_attach_detach, 1);
1553 			if ((rv = attach_node(dip)) == DDI_SUCCESS)
1554 				i_ddi_set_node_state(dip, DS_ATTACHED);
1555 			atomic_add_long(&devinfo_attach_detach, -1);
1556 			break;
1557 		case DS_ATTACHED:
1558 			if ((rv = postattach_node(dip)) == DDI_SUCCESS)
1559 				i_ddi_set_node_state(dip, DS_READY);
1560 			break;
1561 		case DS_READY:
1562 			break;
1563 		default:
1564 			/* should never reach here */
1565 			ASSERT("unknown devinfo state");
1566 		}
1567 	}
1568 
1569 	if (ddidebug & DDI_AUDIT)
1570 		da_log_enter(dip);
1571 	return (rv);
1572 }
1573 
1574 /*
1575  * i_ndi_unconfig_node: downgrade dev_info node into a specified state.
1576  */
1577 int
1578 i_ndi_unconfig_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag)
1579 {
1580 	int	rv = DDI_SUCCESS;
1581 
1582 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
1583 
1584 	while ((i_ddi_node_state(dip) > state) && (rv == DDI_SUCCESS)) {
1585 
1586 		/* don't allow any more changes to the device tree */
1587 		if (devinfo_freeze) {
1588 			rv = DDI_FAILURE;
1589 			break;
1590 		}
1591 
1592 		switch (i_ddi_node_state(dip)) {
1593 		case DS_PROTO:
1594 			break;
1595 		case DS_LINKED:
1596 			/*
1597 			 * Persistent nodes are only removed by hotplug code
1598 			 * .conf nodes synchronizes on per-driver list.
1599 			 */
1600 			if ((rv = unlink_node(dip)) == DDI_SUCCESS)
1601 				i_ddi_set_node_state(dip, DS_PROTO);
1602 			break;
1603 		case DS_BOUND:
1604 			/*
1605 			 * The following transitions synchronizes on the
1606 			 * per-driver busy changing flag, since we already
1607 			 * have a driver.
1608 			 */
1609 			if ((rv = unbind_node(dip)) == DDI_SUCCESS)
1610 				i_ddi_set_node_state(dip, DS_LINKED);
1611 			break;
1612 		case DS_INITIALIZED:
1613 			if ((rv = uninit_node(dip)) == DDI_SUCCESS)
1614 				i_ddi_set_node_state(dip, DS_BOUND);
1615 			break;
1616 		case DS_PROBED:
1617 			if ((rv = unprobe_node(dip)) == DDI_SUCCESS)
1618 				i_ddi_set_node_state(dip, DS_INITIALIZED);
1619 			break;
1620 		case DS_ATTACHED:
1621 			atomic_add_long(&devinfo_attach_detach, 1);
1622 
1623 			mutex_enter(&(DEVI(dip)->devi_lock));
1624 			DEVI_SET_DETACHING(dip);
1625 			mutex_exit(&(DEVI(dip)->devi_lock));
1626 
1627 			membar_enter();	/* ensure visibility for hold_devi */
1628 
1629 			if ((rv = detach_node(dip, flag)) == DDI_SUCCESS)
1630 				i_ddi_set_node_state(dip, DS_PROBED);
1631 
1632 			mutex_enter(&(DEVI(dip)->devi_lock));
1633 			DEVI_CLR_DETACHING(dip);
1634 			mutex_exit(&(DEVI(dip)->devi_lock));
1635 
1636 			atomic_add_long(&devinfo_attach_detach, -1);
1637 			break;
1638 		case DS_READY:
1639 			if ((rv = predetach_node(dip, flag)) == DDI_SUCCESS)
1640 				i_ddi_set_node_state(dip, DS_ATTACHED);
1641 			break;
1642 		default:
1643 			ASSERT("unknown devinfo state");
1644 		}
1645 	}
1646 	da_log_enter(dip);
1647 	return (rv);
1648 }
1649 
1650 /*
1651  * ddi_initchild: transform node to DS_INITIALIZED state
1652  */
1653 int
1654 ddi_initchild(dev_info_t *parent, dev_info_t *proto)
1655 {
1656 	int ret, circ;
1657 
1658 	ndi_devi_enter(parent, &circ);
1659 	ret = i_ndi_config_node(proto, DS_INITIALIZED, 0);
1660 	ndi_devi_exit(parent, circ);
1661 
1662 	return (ret);
1663 }
1664 
1665 /*
1666  * ddi_uninitchild: transform node down to DS_BOUND state
1667  */
1668 int
1669 ddi_uninitchild(dev_info_t *dip)
1670 {
1671 	int ret, circ;
1672 	dev_info_t *parent = ddi_get_parent(dip);
1673 	ASSERT(parent);
1674 
1675 	ndi_devi_enter(parent, &circ);
1676 	ret = i_ndi_unconfig_node(dip, DS_BOUND, 0);
1677 	ndi_devi_exit(parent, circ);
1678 
1679 	return (ret);
1680 }
1681 
1682 /*
1683  * i_ddi_attachchild: transform node to DS_READY/i_ddi_devi_attached() state
1684  */
1685 static int
1686 i_ddi_attachchild(dev_info_t *dip)
1687 {
1688 	dev_info_t	*parent = ddi_get_parent(dip);
1689 	int		ret;
1690 
1691 	ASSERT(parent && DEVI_BUSY_OWNED(parent));
1692 
1693 	if ((i_ddi_node_state(dip) < DS_BOUND) || DEVI_IS_DEVICE_OFFLINE(dip))
1694 		return (DDI_FAILURE);
1695 
1696 	ret = i_ndi_config_node(dip, DS_READY, 0);
1697 	if (ret == NDI_SUCCESS) {
1698 		ret = DDI_SUCCESS;
1699 	} else {
1700 		/*
1701 		 * Take it down to DS_INITIALIZED so pm_pre_probe is run
1702 		 * on the next attach
1703 		 */
1704 		(void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0);
1705 		ret = DDI_FAILURE;
1706 	}
1707 
1708 	return (ret);
1709 }
1710 
1711 /*
1712  * i_ddi_detachchild: transform node down to DS_PROBED state
1713  *	If it fails, put it back to DS_READY state.
1714  * NOTE: A node that fails detach may be at DS_ATTACHED instead
1715  * of DS_READY for a small amount of time - this is the source of
1716  * transient DS_READY->DS_ATTACHED->DS_READY state changes.
1717  */
1718 static int
1719 i_ddi_detachchild(dev_info_t *dip, uint_t flags)
1720 {
1721 	dev_info_t	*parent = ddi_get_parent(dip);
1722 	int		ret;
1723 
1724 	ASSERT(parent && DEVI_BUSY_OWNED(parent));
1725 
1726 	ret = i_ndi_unconfig_node(dip, DS_PROBED, flags);
1727 	if (ret != DDI_SUCCESS)
1728 		(void) i_ndi_config_node(dip, DS_READY, 0);
1729 	else
1730 		/* allow pm_pre_probe to reestablish pm state */
1731 		(void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0);
1732 	return (ret);
1733 }
1734 
1735 /*
1736  * Add a child and bind to driver
1737  */
1738 dev_info_t *
1739 ddi_add_child(dev_info_t *pdip, char *name, uint_t nodeid, uint_t unit)
1740 {
1741 	int circ;
1742 	dev_info_t *dip;
1743 
1744 	/* allocate a new node */
1745 	dip = i_ddi_alloc_node(pdip, name, nodeid, (int)unit, NULL, KM_SLEEP);
1746 
1747 	ndi_devi_enter(pdip, &circ);
1748 	(void) i_ndi_config_node(dip, DS_BOUND, 0);
1749 	ndi_devi_exit(pdip, circ);
1750 	return (dip);
1751 }
1752 
1753 /*
1754  * ddi_remove_child: remove the dip. The parent must be attached and held
1755  */
1756 int
1757 ddi_remove_child(dev_info_t *dip, int dummy)
1758 {
1759 	_NOTE(ARGUNUSED(dummy))
1760 	int circ, ret;
1761 	dev_info_t *parent = ddi_get_parent(dip);
1762 	ASSERT(parent);
1763 
1764 	ndi_devi_enter(parent, &circ);
1765 
1766 	/*
1767 	 * If we still have children, for example SID nodes marked
1768 	 * as persistent but not attached, attempt to remove them.
1769 	 */
1770 	if (DEVI(dip)->devi_child) {
1771 		ret = ndi_devi_unconfig(dip, NDI_DEVI_REMOVE);
1772 		if (ret != NDI_SUCCESS) {
1773 			ndi_devi_exit(parent, circ);
1774 			return (DDI_FAILURE);
1775 		}
1776 		ASSERT(DEVI(dip)->devi_child == NULL);
1777 	}
1778 
1779 	ret = i_ndi_unconfig_node(dip, DS_PROTO, 0);
1780 	ndi_devi_exit(parent, circ);
1781 
1782 	if (ret != DDI_SUCCESS)
1783 		return (ret);
1784 
1785 	ASSERT(i_ddi_node_state(dip) == DS_PROTO);
1786 	i_ddi_free_node(dip);
1787 	return (DDI_SUCCESS);
1788 }
1789 
1790 /*
1791  * NDI wrappers for ref counting, node allocation, and transitions
1792  */
1793 
1794 /*
1795  * Hold/release the devinfo node itself.
1796  * Caller is assumed to prevent the devi from detaching during this call
1797  */
1798 void
1799 ndi_hold_devi(dev_info_t *dip)
1800 {
1801 	mutex_enter(&DEVI(dip)->devi_lock);
1802 	ASSERT(DEVI(dip)->devi_ref >= 0);
1803 	DEVI(dip)->devi_ref++;
1804 	membar_enter();			/* make sure stores are flushed */
1805 	mutex_exit(&DEVI(dip)->devi_lock);
1806 }
1807 
1808 void
1809 ndi_rele_devi(dev_info_t *dip)
1810 {
1811 	ASSERT(DEVI(dip)->devi_ref > 0);
1812 
1813 	mutex_enter(&DEVI(dip)->devi_lock);
1814 	DEVI(dip)->devi_ref--;
1815 	membar_enter();			/* make sure stores are flushed */
1816 	mutex_exit(&DEVI(dip)->devi_lock);
1817 }
1818 
1819 int
1820 e_ddi_devi_holdcnt(dev_info_t *dip)
1821 {
1822 	return (DEVI(dip)->devi_ref);
1823 }
1824 
1825 /*
1826  * Hold/release the driver the devinfo node is bound to.
1827  */
1828 struct dev_ops *
1829 ndi_hold_driver(dev_info_t *dip)
1830 {
1831 	if (i_ddi_node_state(dip) < DS_BOUND)
1832 		return (NULL);
1833 
1834 	ASSERT(DEVI(dip)->devi_major != -1);
1835 	return (mod_hold_dev_by_major(DEVI(dip)->devi_major));
1836 }
1837 
1838 void
1839 ndi_rele_driver(dev_info_t *dip)
1840 {
1841 	ASSERT(i_ddi_node_state(dip) >= DS_BOUND);
1842 	mod_rele_dev_by_major(DEVI(dip)->devi_major);
1843 }
1844 
1845 /*
1846  * Single thread entry into devinfo node for modifying its children (devinfo,
1847  * pathinfo, and minor). To verify in ASSERTS use DEVI_BUSY_OWNED macro.
1848  */
1849 void
1850 ndi_devi_enter(dev_info_t *dip, int *circular)
1851 {
1852 	struct dev_info *devi = DEVI(dip);
1853 	ASSERT(dip != NULL);
1854 
1855 	/* for vHCI, enforce (vHCI, pHCI) ndi_deve_enter() order */
1856 	ASSERT(!MDI_VHCI(dip) || (mdi_devi_pdip_entered(dip) == 0) ||
1857 	    DEVI_BUSY_OWNED(dip));
1858 
1859 	mutex_enter(&devi->devi_lock);
1860 	if (devi->devi_busy_thread == curthread) {
1861 		devi->devi_circular++;
1862 	} else {
1863 		while (DEVI_BUSY_CHANGING(devi) && !panicstr)
1864 			cv_wait(&(devi->devi_cv), &(devi->devi_lock));
1865 		if (panicstr) {
1866 			mutex_exit(&devi->devi_lock);
1867 			return;
1868 		}
1869 		devi->devi_flags |= DEVI_BUSY;
1870 		devi->devi_busy_thread = curthread;
1871 	}
1872 	*circular = devi->devi_circular;
1873 	mutex_exit(&devi->devi_lock);
1874 }
1875 
1876 /*
1877  * Release ndi_devi_enter or successful ndi_devi_tryenter.
1878  */
1879 void
1880 ndi_devi_exit(dev_info_t *dip, int circular)
1881 {
1882 	struct dev_info	*devi = DEVI(dip);
1883 	struct dev_info	*vdevi;
1884 	ASSERT(dip != NULL);
1885 
1886 	if (panicstr)
1887 		return;
1888 
1889 	mutex_enter(&(devi->devi_lock));
1890 	if (circular != 0) {
1891 		devi->devi_circular--;
1892 	} else {
1893 		devi->devi_flags &= ~DEVI_BUSY;
1894 		ASSERT(devi->devi_busy_thread == curthread);
1895 		devi->devi_busy_thread = NULL;
1896 		cv_broadcast(&(devi->devi_cv));
1897 	}
1898 	mutex_exit(&(devi->devi_lock));
1899 
1900 	/*
1901 	 * For pHCI exit we issue a broadcast to vHCI for ndi_devi_config_one()
1902 	 * doing cv_wait on vHCI.
1903 	 */
1904 	if (MDI_PHCI(dip)) {
1905 		vdevi = DEVI(mdi_devi_get_vdip(dip));
1906 		if (vdevi) {
1907 			mutex_enter(&(vdevi->devi_lock));
1908 			if (vdevi->devi_flags & DEVI_PHCI_SIGNALS_VHCI) {
1909 				vdevi->devi_flags &= ~DEVI_PHCI_SIGNALS_VHCI;
1910 				cv_broadcast(&(vdevi->devi_cv));
1911 			}
1912 			mutex_exit(&(vdevi->devi_lock));
1913 		}
1914 	}
1915 }
1916 
1917 /*
1918  * Release ndi_devi_enter and wait for possibility of new children, avoiding
1919  * possibility of missing broadcast before getting to cv_timedwait().
1920  */
1921 static void
1922 ndi_devi_exit_and_wait(dev_info_t *dip, int circular, clock_t end_time)
1923 {
1924 	struct dev_info	*devi = DEVI(dip);
1925 	ASSERT(dip != NULL);
1926 
1927 	if (panicstr)
1928 		return;
1929 
1930 	/*
1931 	 * We are called to wait for of a new child, and new child can
1932 	 * only be added if circular is zero.
1933 	 */
1934 	ASSERT(circular == 0);
1935 
1936 	/* like ndi_devi_exit with circular of zero */
1937 	mutex_enter(&(devi->devi_lock));
1938 	devi->devi_flags &= ~DEVI_BUSY;
1939 	ASSERT(devi->devi_busy_thread == curthread);
1940 	devi->devi_busy_thread = NULL;
1941 	cv_broadcast(&(devi->devi_cv));
1942 
1943 	/* now wait for new children while still holding devi_lock */
1944 	(void) cv_timedwait(&devi->devi_cv, &(devi->devi_lock), end_time);
1945 	mutex_exit(&(devi->devi_lock));
1946 }
1947 
1948 /*
1949  * Attempt to single thread entry into devinfo node for modifying its children.
1950  */
1951 int
1952 ndi_devi_tryenter(dev_info_t *dip, int *circular)
1953 {
1954 	int rval = 1;		   /* assume we enter */
1955 	struct dev_info *devi = DEVI(dip);
1956 	ASSERT(dip != NULL);
1957 
1958 	mutex_enter(&devi->devi_lock);
1959 	if (devi->devi_busy_thread == (void *)curthread) {
1960 		devi->devi_circular++;
1961 	} else {
1962 		if (!DEVI_BUSY_CHANGING(devi)) {
1963 			devi->devi_flags |= DEVI_BUSY;
1964 			devi->devi_busy_thread = (void *)curthread;
1965 		} else {
1966 			rval = 0;	/* devi is busy */
1967 		}
1968 	}
1969 	*circular = devi->devi_circular;
1970 	mutex_exit(&devi->devi_lock);
1971 	return (rval);
1972 }
1973 
1974 /*
1975  * Allocate and initialize a new dev_info structure.
1976  *
1977  * This routine may be called at interrupt time by a nexus in
1978  * response to a hotplug event, therefore memory allocations are
1979  * not allowed to sleep.
1980  */
1981 int
1982 ndi_devi_alloc(dev_info_t *parent, char *node_name, pnode_t nodeid,
1983     dev_info_t **ret_dip)
1984 {
1985 	ASSERT(node_name != NULL);
1986 	ASSERT(ret_dip != NULL);
1987 
1988 	*ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL,
1989 	    KM_NOSLEEP);
1990 	if (*ret_dip == NULL) {
1991 		return (NDI_NOMEM);
1992 	}
1993 
1994 	return (NDI_SUCCESS);
1995 }
1996 
1997 /*
1998  * Allocate and initialize a new dev_info structure
1999  * This routine may sleep and should not be called at interrupt time
2000  */
2001 void
2002 ndi_devi_alloc_sleep(dev_info_t *parent, char *node_name, pnode_t nodeid,
2003     dev_info_t **ret_dip)
2004 {
2005 	ASSERT(node_name != NULL);
2006 	ASSERT(ret_dip != NULL);
2007 
2008 	*ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL,
2009 	    KM_SLEEP);
2010 	ASSERT(*ret_dip);
2011 }
2012 
2013 /*
2014  * Remove an initialized (but not yet attached) dev_info
2015  * node from it's parent.
2016  */
2017 int
2018 ndi_devi_free(dev_info_t *dip)
2019 {
2020 	ASSERT(dip != NULL);
2021 
2022 	if (i_ddi_node_state(dip) >= DS_INITIALIZED)
2023 		return (DDI_FAILURE);
2024 
2025 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_free: %s%d (%p)\n",
2026 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
2027 
2028 	(void) ddi_remove_child(dip, 0);
2029 
2030 	return (NDI_SUCCESS);
2031 }
2032 
2033 /*
2034  * ndi_devi_bind_driver() binds a driver to a given device. If it fails
2035  * to bind the driver, it returns an appropriate error back. Some drivers
2036  * may want to know if the actually failed to bind.
2037  */
2038 int
2039 ndi_devi_bind_driver(dev_info_t *dip, uint_t flags)
2040 {
2041 	int ret = NDI_FAILURE;
2042 	int circ;
2043 	dev_info_t *pdip = ddi_get_parent(dip);
2044 	ASSERT(pdip);
2045 
2046 	NDI_CONFIG_DEBUG((CE_CONT,
2047 	    "ndi_devi_bind_driver: %s%d (%p) flags: %x\n",
2048 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
2049 
2050 	ndi_devi_enter(pdip, &circ);
2051 	if (i_ndi_config_node(dip, DS_BOUND, flags) == DDI_SUCCESS)
2052 		ret = NDI_SUCCESS;
2053 	ndi_devi_exit(pdip, circ);
2054 
2055 	return (ret);
2056 }
2057 
2058 /*
2059  * ndi_devi_unbind_driver: unbind the dip
2060  */
2061 static int
2062 ndi_devi_unbind_driver(dev_info_t *dip)
2063 {
2064 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
2065 
2066 	return (i_ndi_unconfig_node(dip, DS_LINKED, 0));
2067 }
2068 
2069 /*
2070  * Misc. help routines called by framework only
2071  */
2072 
2073 /*
2074  * Get the state of node
2075  */
2076 ddi_node_state_t
2077 i_ddi_node_state(dev_info_t *dip)
2078 {
2079 	return (DEVI(dip)->devi_node_state);
2080 }
2081 
2082 /*
2083  * Set the state of node
2084  */
2085 void
2086 i_ddi_set_node_state(dev_info_t *dip, ddi_node_state_t state)
2087 {
2088 	DEVI(dip)->devi_node_state = state;
2089 	membar_enter();			/* make sure stores are flushed */
2090 }
2091 
2092 /*
2093  * Determine if node is attached. The implementation accommodates transient
2094  * DS_READY->DS_ATTACHED->DS_READY state changes.  Outside this file, this
2095  * function should be instead of i_ddi_node_state() DS_ATTACHED/DS_READY
2096  * state checks.
2097  */
2098 int
2099 i_ddi_devi_attached(dev_info_t *dip)
2100 {
2101 	return (DEVI(dip)->devi_node_state >= DS_ATTACHED);
2102 }
2103 
2104 /*
2105  * Common function for finding a node in a sibling list given name and addr.
2106  *
2107  * By default, name is matched with devi_node_name. The following
2108  * alternative match strategies are supported:
2109  *
2110  *	FIND_NODE_BY_NODENAME: Match on node name - typical use.
2111  *
2112  *	FIND_NODE_BY_DRIVER: A match on driver name bound to node is conducted.
2113  *		This support is used for support of OBP generic names and
2114  *		for the conversion from driver names to generic names. When
2115  *		more consistency in the generic name environment is achieved
2116  *		(and not needed for upgrade) this support can be removed.
2117  *
2118  *	FIND_NODE_BY_ADDR: Match on just the addr.
2119  *		This support is only used/needed during boot to match
2120  *		a node bound via a path-based driver alias.
2121  *
2122  * If a child is not named (dev_addr == NULL), there are three
2123  * possible actions:
2124  *
2125  *	(1) skip it
2126  *	(2) FIND_ADDR_BY_INIT: bring child to DS_INITIALIZED state
2127  *	(3) FIND_ADDR_BY_CALLBACK: use a caller-supplied callback function
2128  */
2129 #define	FIND_NODE_BY_NODENAME	0x01
2130 #define	FIND_NODE_BY_DRIVER	0x02
2131 #define	FIND_NODE_BY_ADDR	0x04
2132 #define	FIND_ADDR_BY_INIT	0x10
2133 #define	FIND_ADDR_BY_CALLBACK	0x20
2134 
2135 static dev_info_t *
2136 find_sibling(dev_info_t *head, char *cname, char *caddr, uint_t flag,
2137     int (*callback)(dev_info_t *, char *, int))
2138 {
2139 	dev_info_t	*dip;
2140 	char		*addr, *buf;
2141 	major_t		major;
2142 	uint_t		by;
2143 
2144 	/* only one way to find a node */
2145 	by = flag &
2146 	    (FIND_NODE_BY_DRIVER | FIND_NODE_BY_NODENAME | FIND_NODE_BY_ADDR);
2147 	ASSERT(by && BIT_ONLYONESET(by));
2148 
2149 	/* only one way to name a node */
2150 	ASSERT(((flag & FIND_ADDR_BY_INIT) == 0) ||
2151 	    ((flag & FIND_ADDR_BY_CALLBACK) == 0));
2152 
2153 	if (by == FIND_NODE_BY_DRIVER) {
2154 		major = ddi_name_to_major(cname);
2155 		if (major == DDI_MAJOR_T_NONE)
2156 			return (NULL);
2157 	}
2158 
2159 	/* preallocate buffer of naming node by callback */
2160 	if (flag & FIND_ADDR_BY_CALLBACK)
2161 		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
2162 
2163 	/*
2164 	 * Walk the child list to find a match
2165 	 */
2166 	if (head == NULL)
2167 		return (NULL);
2168 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(head)));
2169 	for (dip = head; dip; dip = ddi_get_next_sibling(dip)) {
2170 		if (by == FIND_NODE_BY_NODENAME) {
2171 			/* match node name */
2172 			if (strcmp(cname, DEVI(dip)->devi_node_name) != 0)
2173 				continue;
2174 		} else if (by == FIND_NODE_BY_DRIVER) {
2175 			/* match driver major */
2176 			if (DEVI(dip)->devi_major != major)
2177 				continue;
2178 		}
2179 
2180 		if ((addr = DEVI(dip)->devi_addr) == NULL) {
2181 			/* name the child based on the flag */
2182 			if (flag & FIND_ADDR_BY_INIT) {
2183 				if (ddi_initchild(ddi_get_parent(dip), dip)
2184 				    != DDI_SUCCESS)
2185 					continue;
2186 				addr = DEVI(dip)->devi_addr;
2187 			} else if (flag & FIND_ADDR_BY_CALLBACK) {
2188 				if ((callback == NULL) || (callback(
2189 				    dip, buf, MAXNAMELEN) != DDI_SUCCESS))
2190 					continue;
2191 				addr = buf;
2192 			} else {
2193 				continue;	/* skip */
2194 			}
2195 		}
2196 
2197 		/* match addr */
2198 		ASSERT(addr != NULL);
2199 		if (strcmp(caddr, addr) == 0)
2200 			break;	/* node found */
2201 
2202 	}
2203 	if (flag & FIND_ADDR_BY_CALLBACK)
2204 		kmem_free(buf, MAXNAMELEN);
2205 	return (dip);
2206 }
2207 
2208 /*
2209  * Find child of pdip with name: cname@caddr
2210  * Called by init_node() to look for duplicate nodes
2211  */
2212 static dev_info_t *
2213 find_duplicate_child(dev_info_t *pdip, dev_info_t *dip)
2214 {
2215 	dev_info_t *dup;
2216 	char *cname = DEVI(dip)->devi_node_name;
2217 	char *caddr = DEVI(dip)->devi_addr;
2218 
2219 	/* search nodes before dip */
2220 	dup = find_sibling(ddi_get_child(pdip), cname, caddr,
2221 	    FIND_NODE_BY_NODENAME, NULL);
2222 	if (dup != dip)
2223 		return (dup);
2224 
2225 	/*
2226 	 * search nodes after dip; normally this is not needed,
2227 	 */
2228 	return (find_sibling(ddi_get_next_sibling(dip), cname, caddr,
2229 	    FIND_NODE_BY_NODENAME, NULL));
2230 }
2231 
2232 /*
2233  * Find a child of a given name and address, using a callback to name
2234  * unnamed children. cname is the binding name.
2235  */
2236 dev_info_t *
2237 ndi_devi_findchild_by_callback(dev_info_t *pdip, char *dname, char *ua,
2238     int (*make_ua)(dev_info_t *, char *, int))
2239 {
2240 	int	by = FIND_ADDR_BY_CALLBACK;
2241 
2242 	ASSERT(DEVI_BUSY_OWNED(pdip));
2243 	by |= dname ? FIND_NODE_BY_DRIVER : FIND_NODE_BY_ADDR;
2244 	return (find_sibling(ddi_get_child(pdip), dname, ua, by, make_ua));
2245 }
2246 
2247 /*
2248  * Find a child of a given name and address, invoking initchild to name
2249  * unnamed children. cname is the node name.
2250  */
2251 static dev_info_t *
2252 find_child_by_name(dev_info_t *pdip, char *cname, char *caddr)
2253 {
2254 	dev_info_t	*dip;
2255 
2256 	/* attempt search without changing state of preceding siblings */
2257 	dip = find_sibling(ddi_get_child(pdip), cname, caddr,
2258 	    FIND_NODE_BY_NODENAME, NULL);
2259 	if (dip)
2260 		return (dip);
2261 
2262 	return (find_sibling(ddi_get_child(pdip), cname, caddr,
2263 	    FIND_NODE_BY_NODENAME|FIND_ADDR_BY_INIT, NULL));
2264 }
2265 
2266 /*
2267  * Find a child of a given name and address, invoking initchild to name
2268  * unnamed children. cname is the node name.
2269  */
2270 static dev_info_t *
2271 find_child_by_driver(dev_info_t *pdip, char *cname, char *caddr)
2272 {
2273 	dev_info_t	*dip;
2274 
2275 	/* attempt search without changing state of preceding siblings */
2276 	dip = find_sibling(ddi_get_child(pdip), cname, caddr,
2277 	    FIND_NODE_BY_DRIVER, NULL);
2278 	if (dip)
2279 		return (dip);
2280 
2281 	return (find_sibling(ddi_get_child(pdip), cname, caddr,
2282 	    FIND_NODE_BY_DRIVER|FIND_ADDR_BY_INIT, NULL));
2283 }
2284 
2285 /*
2286  * Find a child of a given address, invoking initchild to name
2287  * unnamed children. cname is the node name.
2288  *
2289  * NOTE: This function is only used during boot. One would hope that
2290  * unique sibling unit-addresses on hardware branches of the tree would
2291  * be a requirement to avoid two drivers trying to control the same
2292  * piece of hardware. Unfortunately there are some cases where this
2293  * situation exists (/ssm@0,0/pci@1c,700000 /ssm@0,0/sghsc@1c,700000).
2294  * Until unit-address uniqueness of siblings is guaranteed, use of this
2295  * interface for purposes other than boot should be avoided.
2296  */
2297 static dev_info_t *
2298 find_child_by_addr(dev_info_t *pdip, char *caddr)
2299 {
2300 	dev_info_t	*dip;
2301 
2302 	/* return NULL if called without a unit-address */
2303 	if ((caddr == NULL) || (*caddr == '\0'))
2304 		return (NULL);
2305 
2306 	/* attempt search without changing state of preceding siblings */
2307 	dip = find_sibling(ddi_get_child(pdip), NULL, caddr,
2308 	    FIND_NODE_BY_ADDR, NULL);
2309 	if (dip)
2310 		return (dip);
2311 
2312 	return (find_sibling(ddi_get_child(pdip), NULL, caddr,
2313 	    FIND_NODE_BY_ADDR|FIND_ADDR_BY_INIT, NULL));
2314 }
2315 
2316 /*
2317  * Deleting a property list. Take care, since some property structures
2318  * may not be fully built.
2319  */
2320 void
2321 i_ddi_prop_list_delete(ddi_prop_t *prop)
2322 {
2323 	while (prop) {
2324 		ddi_prop_t *next = prop->prop_next;
2325 		if (prop->prop_name)
2326 			kmem_free(prop->prop_name, strlen(prop->prop_name) + 1);
2327 		if ((prop->prop_len != 0) && prop->prop_val)
2328 			kmem_free(prop->prop_val, prop->prop_len);
2329 		kmem_free(prop, sizeof (struct ddi_prop));
2330 		prop = next;
2331 	}
2332 }
2333 
2334 /*
2335  * Duplicate property list
2336  */
2337 ddi_prop_t *
2338 i_ddi_prop_list_dup(ddi_prop_t *prop, uint_t flag)
2339 {
2340 	ddi_prop_t *result, *prev, *copy;
2341 
2342 	if (prop == NULL)
2343 		return (NULL);
2344 
2345 	result = prev = NULL;
2346 	for (; prop != NULL; prop = prop->prop_next) {
2347 		ASSERT(prop->prop_name != NULL);
2348 		copy = kmem_zalloc(sizeof (struct ddi_prop), flag);
2349 		if (copy == NULL)
2350 			goto fail;
2351 
2352 		copy->prop_dev = prop->prop_dev;
2353 		copy->prop_flags = prop->prop_flags;
2354 		copy->prop_name = i_ddi_strdup(prop->prop_name, flag);
2355 		if (copy->prop_name == NULL)
2356 			goto fail;
2357 
2358 		if ((copy->prop_len = prop->prop_len) != 0) {
2359 			copy->prop_val = kmem_zalloc(prop->prop_len, flag);
2360 			if (copy->prop_val == NULL)
2361 				goto fail;
2362 
2363 			bcopy(prop->prop_val, copy->prop_val, prop->prop_len);
2364 		}
2365 
2366 		if (prev == NULL)
2367 			result = prev = copy;
2368 		else
2369 			prev->prop_next = copy;
2370 		prev = copy;
2371 	}
2372 	return (result);
2373 
2374 fail:
2375 	i_ddi_prop_list_delete(result);
2376 	return (NULL);
2377 }
2378 
2379 /*
2380  * Create a reference property list, currently used only for
2381  * driver global properties. Created with ref count of 1.
2382  */
2383 ddi_prop_list_t *
2384 i_ddi_prop_list_create(ddi_prop_t *props)
2385 {
2386 	ddi_prop_list_t *list = kmem_alloc(sizeof (*list), KM_SLEEP);
2387 	list->prop_list = props;
2388 	list->prop_ref = 1;
2389 	return (list);
2390 }
2391 
2392 /*
2393  * Increment/decrement reference count. The reference is
2394  * protected by dn_lock. The only interfaces modifying
2395  * dn_global_prop_ptr is in impl_make[free]_parlist().
2396  */
2397 void
2398 i_ddi_prop_list_hold(ddi_prop_list_t *prop_list, struct devnames *dnp)
2399 {
2400 	ASSERT(prop_list->prop_ref >= 0);
2401 	ASSERT(mutex_owned(&dnp->dn_lock));
2402 	prop_list->prop_ref++;
2403 }
2404 
2405 void
2406 i_ddi_prop_list_rele(ddi_prop_list_t *prop_list, struct devnames *dnp)
2407 {
2408 	ASSERT(prop_list->prop_ref > 0);
2409 	ASSERT(mutex_owned(&dnp->dn_lock));
2410 	prop_list->prop_ref--;
2411 
2412 	if (prop_list->prop_ref == 0) {
2413 		i_ddi_prop_list_delete(prop_list->prop_list);
2414 		kmem_free(prop_list, sizeof (*prop_list));
2415 	}
2416 }
2417 
2418 /*
2419  * Free table of classes by drivers
2420  */
2421 void
2422 i_ddi_free_exported_classes(char **classes, int n)
2423 {
2424 	if ((n == 0) || (classes == NULL))
2425 		return;
2426 
2427 	kmem_free(classes, n * sizeof (char *));
2428 }
2429 
2430 /*
2431  * Get all classes exported by dip
2432  */
2433 int
2434 i_ddi_get_exported_classes(dev_info_t *dip, char ***classes)
2435 {
2436 	extern void lock_hw_class_list();
2437 	extern void unlock_hw_class_list();
2438 	extern int get_class(const char *, char **);
2439 
2440 	static char *rootclass = "root";
2441 	int n = 0, nclass = 0;
2442 	char **buf;
2443 
2444 	ASSERT(i_ddi_node_state(dip) >= DS_BOUND);
2445 
2446 	if (dip == ddi_root_node())	/* rootnode exports class "root" */
2447 		nclass = 1;
2448 	lock_hw_class_list();
2449 	nclass += get_class(ddi_driver_name(dip), NULL);
2450 	if (nclass == 0) {
2451 		unlock_hw_class_list();
2452 		return (0);		/* no class exported */
2453 	}
2454 
2455 	*classes = buf = kmem_alloc(nclass * sizeof (char *), KM_SLEEP);
2456 	if (dip == ddi_root_node()) {
2457 		*buf++ = rootclass;
2458 		n = 1;
2459 	}
2460 	n += get_class(ddi_driver_name(dip), buf);
2461 	unlock_hw_class_list();
2462 
2463 	ASSERT(n == nclass);	/* make sure buf wasn't overrun */
2464 	return (nclass);
2465 }
2466 
2467 /*
2468  * Helper functions, returns NULL if no memory.
2469  */
2470 char *
2471 i_ddi_strdup(char *str, uint_t flag)
2472 {
2473 	char *copy;
2474 
2475 	if (str == NULL)
2476 		return (NULL);
2477 
2478 	copy = kmem_alloc(strlen(str) + 1, flag);
2479 	if (copy == NULL)
2480 		return (NULL);
2481 
2482 	(void) strcpy(copy, str);
2483 	return (copy);
2484 }
2485 
2486 /*
2487  * Load driver.conf file for major. Load all if major == -1.
2488  *
2489  * This is called
2490  * - early in boot after devnames array is initialized
2491  * - from vfs code when certain file systems are mounted
2492  * - from add_drv when a new driver is added
2493  */
2494 int
2495 i_ddi_load_drvconf(major_t major)
2496 {
2497 	extern int modrootloaded;
2498 
2499 	major_t low, high, m;
2500 
2501 	if (major == DDI_MAJOR_T_NONE) {
2502 		low = 0;
2503 		high = devcnt - 1;
2504 	} else {
2505 		if (major >= devcnt)
2506 			return (EINVAL);
2507 		low = high = major;
2508 	}
2509 
2510 	for (m = low; m <= high; m++) {
2511 		struct devnames *dnp = &devnamesp[m];
2512 		LOCK_DEV_OPS(&dnp->dn_lock);
2513 		dnp->dn_flags &= ~(DN_DRIVER_HELD|DN_DRIVER_INACTIVE);
2514 		(void) impl_make_parlist(m);
2515 		UNLOCK_DEV_OPS(&dnp->dn_lock);
2516 	}
2517 
2518 	if (modrootloaded) {
2519 		ddi_walk_devs(ddi_root_node(), reset_nexus_flags,
2520 		    (void *)(uintptr_t)major);
2521 	}
2522 
2523 	/* build dn_list from old entries in path_to_inst */
2524 	e_ddi_unorphan_instance_nos();
2525 	return (0);
2526 }
2527 
2528 /*
2529  * Unload a specific driver.conf.
2530  * Don't support unload all because it doesn't make any sense
2531  */
2532 int
2533 i_ddi_unload_drvconf(major_t major)
2534 {
2535 	int error;
2536 	struct devnames *dnp;
2537 
2538 	if (major >= devcnt)
2539 		return (EINVAL);
2540 
2541 	/*
2542 	 * Take the per-driver lock while unloading driver.conf
2543 	 */
2544 	dnp = &devnamesp[major];
2545 	LOCK_DEV_OPS(&dnp->dn_lock);
2546 	error = impl_free_parlist(major);
2547 	UNLOCK_DEV_OPS(&dnp->dn_lock);
2548 	return (error);
2549 }
2550 
2551 /*
2552  * Merge a .conf node. This is called by nexus drivers to augment
2553  * hw node with properties specified in driver.conf file. This function
2554  * takes a callback routine to name nexus children.
2555  * The parent node must be held busy.
2556  *
2557  * It returns DDI_SUCCESS if the node is merged and DDI_FAILURE otherwise.
2558  */
2559 int
2560 ndi_merge_node(dev_info_t *dip, int (*make_ua)(dev_info_t *, char *, int))
2561 {
2562 	dev_info_t *hwdip;
2563 
2564 	ASSERT(ndi_dev_is_persistent_node(dip) == 0);
2565 	ASSERT(ddi_get_name_addr(dip) != NULL);
2566 
2567 	hwdip = ndi_devi_findchild_by_callback(ddi_get_parent(dip),
2568 	    ddi_binding_name(dip), ddi_get_name_addr(dip), make_ua);
2569 
2570 	/*
2571 	 * Look for the hardware node that is the target of the merge;
2572 	 * return failure if not found.
2573 	 */
2574 	if ((hwdip == NULL) || (hwdip == dip)) {
2575 		char *buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
2576 		NDI_CONFIG_DEBUG((CE_WARN, "No HW node to merge conf node %s",
2577 		    ddi_deviname(dip, buf)));
2578 		kmem_free(buf, MAXNAMELEN);
2579 		return (DDI_FAILURE);
2580 	}
2581 
2582 	/*
2583 	 * Make sure the hardware node is uninitialized and has no property.
2584 	 * This may not be the case if new .conf files are load after some
2585 	 * hardware nodes have already been initialized and attached.
2586 	 *
2587 	 * N.B. We return success here because the node was *intended*
2588 	 *	to be a merge node because there is a hw node with the name.
2589 	 */
2590 	mutex_enter(&DEVI(hwdip)->devi_lock);
2591 	if (ndi_dev_is_persistent_node(hwdip) == 0) {
2592 		char *buf;
2593 		mutex_exit(&DEVI(hwdip)->devi_lock);
2594 
2595 		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
2596 		NDI_CONFIG_DEBUG((CE_NOTE, "Duplicate .conf node %s",
2597 		    ddi_deviname(dip, buf)));
2598 		kmem_free(buf, MAXNAMELEN);
2599 		return (DDI_SUCCESS);
2600 	}
2601 
2602 	/*
2603 	 * If it is possible that the hardware has already been touched
2604 	 * then don't merge.
2605 	 */
2606 	if (i_ddi_node_state(hwdip) >= DS_INITIALIZED ||
2607 	    (DEVI(hwdip)->devi_sys_prop_ptr != NULL) ||
2608 	    (DEVI(hwdip)->devi_drv_prop_ptr != NULL)) {
2609 		char *buf;
2610 		mutex_exit(&DEVI(hwdip)->devi_lock);
2611 
2612 		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
2613 		NDI_CONFIG_DEBUG((CE_NOTE,
2614 		    "!Cannot merge .conf node %s with hw node %p "
2615 		    "-- not in proper state",
2616 		    ddi_deviname(dip, buf), (void *)hwdip));
2617 		kmem_free(buf, MAXNAMELEN);
2618 		return (DDI_SUCCESS);
2619 	}
2620 
2621 	mutex_enter(&DEVI(dip)->devi_lock);
2622 	DEVI(hwdip)->devi_sys_prop_ptr = DEVI(dip)->devi_sys_prop_ptr;
2623 	DEVI(hwdip)->devi_drv_prop_ptr = DEVI(dip)->devi_drv_prop_ptr;
2624 	DEVI(dip)->devi_sys_prop_ptr = NULL;
2625 	DEVI(dip)->devi_drv_prop_ptr = NULL;
2626 	mutex_exit(&DEVI(dip)->devi_lock);
2627 	mutex_exit(&DEVI(hwdip)->devi_lock);
2628 
2629 	return (DDI_SUCCESS);
2630 }
2631 
2632 /*
2633  * Merge a "wildcard" .conf node. This is called by nexus drivers to
2634  * augment a set of hw node with properties specified in driver.conf file.
2635  * The parent node must be held busy.
2636  *
2637  * There is no failure mode, since the nexus may or may not have child
2638  * node bound the driver specified by the wildcard node.
2639  */
2640 void
2641 ndi_merge_wildcard_node(dev_info_t *dip)
2642 {
2643 	dev_info_t *hwdip;
2644 	dev_info_t *pdip = ddi_get_parent(dip);
2645 	major_t major = ddi_driver_major(dip);
2646 
2647 	/* never attempt to merge a hw node */
2648 	ASSERT(ndi_dev_is_persistent_node(dip) == 0);
2649 	/* must be bound to a driver major number */
2650 	ASSERT(major != DDI_MAJOR_T_NONE);
2651 
2652 	/*
2653 	 * Walk the child list to find all nodes bound to major
2654 	 * and copy properties.
2655 	 */
2656 	mutex_enter(&DEVI(dip)->devi_lock);
2657 	ASSERT(DEVI_BUSY_OWNED(pdip));
2658 	for (hwdip = ddi_get_child(pdip); hwdip;
2659 	    hwdip = ddi_get_next_sibling(hwdip)) {
2660 		/*
2661 		 * Skip nodes not bound to same driver
2662 		 */
2663 		if (ddi_driver_major(hwdip) != major)
2664 			continue;
2665 
2666 		/*
2667 		 * Skip .conf nodes
2668 		 */
2669 		if (ndi_dev_is_persistent_node(hwdip) == 0)
2670 			continue;
2671 
2672 		/*
2673 		 * Make sure the node is uninitialized and has no property.
2674 		 */
2675 		mutex_enter(&DEVI(hwdip)->devi_lock);
2676 		if (i_ddi_node_state(hwdip) >= DS_INITIALIZED ||
2677 		    (DEVI(hwdip)->devi_sys_prop_ptr != NULL) ||
2678 		    (DEVI(hwdip)->devi_drv_prop_ptr != NULL)) {
2679 			mutex_exit(&DEVI(hwdip)->devi_lock);
2680 			NDI_CONFIG_DEBUG((CE_NOTE, "HW node %p state not "
2681 			    "suitable for merging wildcard conf node %s",
2682 			    (void *)hwdip, ddi_node_name(dip)));
2683 			continue;
2684 		}
2685 
2686 		DEVI(hwdip)->devi_sys_prop_ptr =
2687 		    i_ddi_prop_list_dup(DEVI(dip)->devi_sys_prop_ptr, KM_SLEEP);
2688 		DEVI(hwdip)->devi_drv_prop_ptr =
2689 		    i_ddi_prop_list_dup(DEVI(dip)->devi_drv_prop_ptr, KM_SLEEP);
2690 		mutex_exit(&DEVI(hwdip)->devi_lock);
2691 	}
2692 	mutex_exit(&DEVI(dip)->devi_lock);
2693 }
2694 
2695 /*
2696  * Return the major number based on the compatible property. This interface
2697  * may be used in situations where we are trying to detect if a better driver
2698  * now exists for a device, so it must use the 'compatible' property.  If
2699  * a non-NULL formp is specified and the binding was based on compatible then
2700  * return the pointer to the form used in *formp.
2701  */
2702 major_t
2703 ddi_compatible_driver_major(dev_info_t *dip, char **formp)
2704 {
2705 	struct dev_info *devi = DEVI(dip);
2706 	void		*compat;
2707 	size_t		len;
2708 	char		*p = NULL;
2709 	major_t		major = DDI_MAJOR_T_NONE;
2710 
2711 	if (formp)
2712 		*formp = NULL;
2713 
2714 	/*
2715 	 * Highest precedence binding is a path-oriented alias. Since this
2716 	 * requires a 'path', this type of binding occurs via more obtuse
2717 	 * 'rebind'. The need for a path-oriented alias 'rebind' is detected
2718 	 * after a successful DDI_CTLOPS_INITCHILD to another driver: this is
2719 	 * is the first point at which the unit-address (or instance) of the
2720 	 * last component of the path is available (even though the path is
2721 	 * bound to the wrong driver at this point).
2722 	 */
2723 	if (devi->devi_flags & DEVI_REBIND) {
2724 		p = devi->devi_rebinding_name;
2725 		major = ddi_name_to_major(p);
2726 		if (driver_installed(major)) {
2727 			if (formp)
2728 				*formp = p;
2729 			return (major);
2730 		}
2731 
2732 		/*
2733 		 * If for some reason devi_rebinding_name no longer resolves
2734 		 * to a proper driver then clear DEVI_REBIND.
2735 		 */
2736 		mutex_enter(&devi->devi_lock);
2737 		devi->devi_flags &= ~DEVI_REBIND;
2738 		mutex_exit(&devi->devi_lock);
2739 	}
2740 
2741 	/* look up compatible property */
2742 	(void) lookup_compatible(dip, KM_SLEEP);
2743 	compat = (void *)(devi->devi_compat_names);
2744 	len = devi->devi_compat_length;
2745 
2746 	/* find the highest precedence compatible form with a driver binding */
2747 	while ((p = prom_decode_composite_string(compat, len, p)) != NULL) {
2748 		major = ddi_name_to_major(p);
2749 		if (driver_installed(major)) {
2750 			if (formp)
2751 				*formp = p;
2752 			return (major);
2753 		}
2754 	}
2755 
2756 	/*
2757 	 * none of the compatible forms have a driver binding, see if
2758 	 * the node name has a driver binding.
2759 	 */
2760 	major = ddi_name_to_major(ddi_node_name(dip));
2761 	if (driver_installed(major))
2762 		return (major);
2763 
2764 	/* no driver */
2765 	return (DDI_MAJOR_T_NONE);
2766 }
2767 
2768 /*
2769  * Static help functions
2770  */
2771 
2772 /*
2773  * lookup the "compatible" property and cache it's contents in the
2774  * device node.
2775  */
2776 static int
2777 lookup_compatible(dev_info_t *dip, uint_t flag)
2778 {
2779 	int rv;
2780 	int prop_flags;
2781 	uint_t ncompatstrs;
2782 	char **compatstrpp;
2783 	char *di_compat_strp;
2784 	size_t di_compat_strlen;
2785 
2786 	if (DEVI(dip)->devi_compat_names) {
2787 		return (DDI_SUCCESS);
2788 	}
2789 
2790 	prop_flags = DDI_PROP_TYPE_STRING | DDI_PROP_DONTPASS;
2791 
2792 	if (flag & KM_NOSLEEP) {
2793 		prop_flags |= DDI_PROP_DONTSLEEP;
2794 	}
2795 
2796 	if (ndi_dev_is_prom_node(dip) == 0) {
2797 		prop_flags |= DDI_PROP_NOTPROM;
2798 	}
2799 
2800 	rv = ddi_prop_lookup_common(DDI_DEV_T_ANY, dip, prop_flags,
2801 	    "compatible", &compatstrpp, &ncompatstrs,
2802 	    ddi_prop_fm_decode_strings);
2803 
2804 	if (rv == DDI_PROP_NOT_FOUND) {
2805 		return (DDI_SUCCESS);
2806 	}
2807 
2808 	if (rv != DDI_PROP_SUCCESS) {
2809 		return (DDI_FAILURE);
2810 	}
2811 
2812 	/*
2813 	 * encode the compatible property data in the dev_info node
2814 	 */
2815 	rv = DDI_SUCCESS;
2816 	if (ncompatstrs != 0) {
2817 		di_compat_strp = encode_composite_string(compatstrpp,
2818 		    ncompatstrs, &di_compat_strlen, flag);
2819 		if (di_compat_strp != NULL) {
2820 			DEVI(dip)->devi_compat_names = di_compat_strp;
2821 			DEVI(dip)->devi_compat_length = di_compat_strlen;
2822 		} else {
2823 			rv = DDI_FAILURE;
2824 		}
2825 	}
2826 	ddi_prop_free(compatstrpp);
2827 	return (rv);
2828 }
2829 
2830 /*
2831  * Create a composite string from a list of strings.
2832  *
2833  * A composite string consists of a single buffer containing one
2834  * or more NULL terminated strings.
2835  */
2836 static char *
2837 encode_composite_string(char **strings, uint_t nstrings, size_t *retsz,
2838     uint_t flag)
2839 {
2840 	uint_t index;
2841 	char  **strpp;
2842 	uint_t slen;
2843 	size_t cbuf_sz = 0;
2844 	char *cbuf_p;
2845 	char *cbuf_ip;
2846 
2847 	if (strings == NULL || nstrings == 0 || retsz == NULL) {
2848 		return (NULL);
2849 	}
2850 
2851 	for (index = 0, strpp = strings; index < nstrings; index++)
2852 		cbuf_sz += strlen(*(strpp++)) + 1;
2853 
2854 	if ((cbuf_p = kmem_alloc(cbuf_sz, flag)) == NULL) {
2855 		cmn_err(CE_NOTE,
2856 		    "?failed to allocate device node compatstr");
2857 		return (NULL);
2858 	}
2859 
2860 	cbuf_ip = cbuf_p;
2861 	for (index = 0, strpp = strings; index < nstrings; index++) {
2862 		slen = strlen(*strpp);
2863 		bcopy(*(strpp++), cbuf_ip, slen);
2864 		cbuf_ip += slen;
2865 		*(cbuf_ip++) = '\0';
2866 	}
2867 
2868 	*retsz = cbuf_sz;
2869 	return (cbuf_p);
2870 }
2871 
2872 static void
2873 link_to_driver_list(dev_info_t *dip)
2874 {
2875 	major_t major = DEVI(dip)->devi_major;
2876 	struct devnames *dnp;
2877 
2878 	ASSERT(major != DDI_MAJOR_T_NONE);
2879 
2880 	/*
2881 	 * Remove from orphan list
2882 	 */
2883 	if (ndi_dev_is_persistent_node(dip)) {
2884 		dnp = &orphanlist;
2885 		remove_from_dn_list(dnp, dip);
2886 	}
2887 
2888 	/*
2889 	 * Add to per driver list
2890 	 */
2891 	dnp = &devnamesp[major];
2892 	add_to_dn_list(dnp, dip);
2893 }
2894 
2895 static void
2896 unlink_from_driver_list(dev_info_t *dip)
2897 {
2898 	major_t major = DEVI(dip)->devi_major;
2899 	struct devnames *dnp;
2900 
2901 	ASSERT(major != DDI_MAJOR_T_NONE);
2902 
2903 	/*
2904 	 * Remove from per-driver list
2905 	 */
2906 	dnp = &devnamesp[major];
2907 	remove_from_dn_list(dnp, dip);
2908 
2909 	/*
2910 	 * Add to orphan list
2911 	 */
2912 	if (ndi_dev_is_persistent_node(dip)) {
2913 		dnp = &orphanlist;
2914 		add_to_dn_list(dnp, dip);
2915 	}
2916 }
2917 
2918 /*
2919  * scan the per-driver list looking for dev_info "dip"
2920  */
2921 static dev_info_t *
2922 in_dn_list(struct devnames *dnp, dev_info_t *dip)
2923 {
2924 	struct dev_info *idevi;
2925 
2926 	if ((idevi = DEVI(dnp->dn_head)) == NULL)
2927 		return (NULL);
2928 
2929 	while (idevi) {
2930 		if (idevi == DEVI(dip))
2931 			return (dip);
2932 		idevi = idevi->devi_next;
2933 	}
2934 	return (NULL);
2935 }
2936 
2937 /*
2938  * insert devinfo node 'dip' into the per-driver instance list
2939  * headed by 'dnp'
2940  *
2941  * Nodes on the per-driver list are ordered: HW - SID - PSEUDO.  The order is
2942  * required for merging of .conf file data to work properly.
2943  */
2944 static void
2945 add_to_ordered_dn_list(struct devnames *dnp, dev_info_t *dip)
2946 {
2947 	dev_info_t **dipp;
2948 
2949 	ASSERT(mutex_owned(&(dnp->dn_lock)));
2950 
2951 	dipp = &dnp->dn_head;
2952 	if (ndi_dev_is_prom_node(dip)) {
2953 		/*
2954 		 * Find the first non-prom node or end of list
2955 		 */
2956 		while (*dipp && (ndi_dev_is_prom_node(*dipp) != 0)) {
2957 			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
2958 		}
2959 	} else if (ndi_dev_is_persistent_node(dip)) {
2960 		/*
2961 		 * Find the first non-persistent node
2962 		 */
2963 		while (*dipp && (ndi_dev_is_persistent_node(*dipp) != 0)) {
2964 			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
2965 		}
2966 	} else {
2967 		/*
2968 		 * Find the end of the list
2969 		 */
2970 		while (*dipp) {
2971 			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
2972 		}
2973 	}
2974 
2975 	DEVI(dip)->devi_next = DEVI(*dipp);
2976 	*dipp = dip;
2977 }
2978 
2979 /*
2980  * add a list of device nodes to the device node list in the
2981  * devnames structure
2982  */
2983 static void
2984 add_to_dn_list(struct devnames *dnp, dev_info_t *dip)
2985 {
2986 	/*
2987 	 * Look to see if node already exists
2988 	 */
2989 	LOCK_DEV_OPS(&(dnp->dn_lock));
2990 	if (in_dn_list(dnp, dip)) {
2991 		cmn_err(CE_NOTE, "add_to_dn_list: node %s already in list",
2992 		    DEVI(dip)->devi_node_name);
2993 	} else {
2994 		add_to_ordered_dn_list(dnp, dip);
2995 	}
2996 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
2997 }
2998 
2999 static void
3000 remove_from_dn_list(struct devnames *dnp, dev_info_t *dip)
3001 {
3002 	dev_info_t **plist;
3003 
3004 	LOCK_DEV_OPS(&(dnp->dn_lock));
3005 
3006 	plist = (dev_info_t **)&dnp->dn_head;
3007 	while (*plist && (*plist != dip)) {
3008 		plist = (dev_info_t **)&DEVI(*plist)->devi_next;
3009 	}
3010 
3011 	if (*plist != NULL) {
3012 		ASSERT(*plist == dip);
3013 		*plist = (dev_info_t *)(DEVI(dip)->devi_next);
3014 		DEVI(dip)->devi_next = NULL;
3015 	} else {
3016 		NDI_CONFIG_DEBUG((CE_NOTE,
3017 		    "remove_from_dn_list: node %s not found in list",
3018 		    DEVI(dip)->devi_node_name));
3019 	}
3020 
3021 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
3022 }
3023 
3024 /*
3025  * Add and remove reference driver global property list
3026  */
3027 static void
3028 add_global_props(dev_info_t *dip)
3029 {
3030 	struct devnames *dnp;
3031 	ddi_prop_list_t *plist;
3032 
3033 	ASSERT(DEVI(dip)->devi_global_prop_list == NULL);
3034 	ASSERT(DEVI(dip)->devi_major != DDI_MAJOR_T_NONE);
3035 
3036 	dnp = &devnamesp[DEVI(dip)->devi_major];
3037 	LOCK_DEV_OPS(&dnp->dn_lock);
3038 	plist = dnp->dn_global_prop_ptr;
3039 	if (plist == NULL) {
3040 		UNLOCK_DEV_OPS(&dnp->dn_lock);
3041 		return;
3042 	}
3043 	i_ddi_prop_list_hold(plist, dnp);
3044 	UNLOCK_DEV_OPS(&dnp->dn_lock);
3045 
3046 	mutex_enter(&DEVI(dip)->devi_lock);
3047 	DEVI(dip)->devi_global_prop_list = plist;
3048 	mutex_exit(&DEVI(dip)->devi_lock);
3049 }
3050 
3051 static void
3052 remove_global_props(dev_info_t *dip)
3053 {
3054 	ddi_prop_list_t *proplist;
3055 
3056 	mutex_enter(&DEVI(dip)->devi_lock);
3057 	proplist = DEVI(dip)->devi_global_prop_list;
3058 	DEVI(dip)->devi_global_prop_list = NULL;
3059 	mutex_exit(&DEVI(dip)->devi_lock);
3060 
3061 	if (proplist) {
3062 		major_t major;
3063 		struct devnames *dnp;
3064 
3065 		major = ddi_driver_major(dip);
3066 		ASSERT(major != DDI_MAJOR_T_NONE);
3067 		dnp = &devnamesp[major];
3068 		LOCK_DEV_OPS(&dnp->dn_lock);
3069 		i_ddi_prop_list_rele(proplist, dnp);
3070 		UNLOCK_DEV_OPS(&dnp->dn_lock);
3071 	}
3072 }
3073 
3074 #ifdef DEBUG
3075 /*
3076  * Set this variable to '0' to disable the optimization,
3077  * and to 2 to print debug message.
3078  */
3079 static int optimize_dtree = 1;
3080 
3081 static void
3082 debug_dtree(dev_info_t *devi, struct dev_info *adevi, char *service)
3083 {
3084 	char *adeviname, *buf;
3085 
3086 	/*
3087 	 * Don't print unless optimize dtree is set to 2+
3088 	 */
3089 	if (optimize_dtree <= 1)
3090 		return;
3091 
3092 	buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
3093 	adeviname = ddi_deviname((dev_info_t *)adevi, buf);
3094 	if (*adeviname == '\0')
3095 		adeviname = "root";
3096 
3097 	cmn_err(CE_CONT, "%s %s -> %s\n",
3098 	    ddi_deviname(devi, buf), service, adeviname);
3099 
3100 	kmem_free(buf, MAXNAMELEN);
3101 }
3102 #else /* DEBUG */
3103 #define	debug_dtree(a1, a2, a3)	 /* nothing */
3104 #endif	/* DEBUG */
3105 
3106 static void
3107 ddi_optimize_dtree(dev_info_t *devi)
3108 {
3109 	struct dev_info *pdevi;
3110 	struct bus_ops *b;
3111 
3112 	pdevi = DEVI(devi)->devi_parent;
3113 	ASSERT(pdevi);
3114 
3115 	/*
3116 	 * Set the unoptimized values
3117 	 */
3118 	DEVI(devi)->devi_bus_map_fault = pdevi;
3119 	DEVI(devi)->devi_bus_dma_map = pdevi;
3120 	DEVI(devi)->devi_bus_dma_allochdl = pdevi;
3121 	DEVI(devi)->devi_bus_dma_freehdl = pdevi;
3122 	DEVI(devi)->devi_bus_dma_bindhdl = pdevi;
3123 	DEVI(devi)->devi_bus_dma_bindfunc =
3124 	    pdevi->devi_ops->devo_bus_ops->bus_dma_bindhdl;
3125 	DEVI(devi)->devi_bus_dma_unbindhdl = pdevi;
3126 	DEVI(devi)->devi_bus_dma_unbindfunc =
3127 	    pdevi->devi_ops->devo_bus_ops->bus_dma_unbindhdl;
3128 	DEVI(devi)->devi_bus_dma_flush = pdevi;
3129 	DEVI(devi)->devi_bus_dma_win = pdevi;
3130 	DEVI(devi)->devi_bus_dma_ctl = pdevi;
3131 	DEVI(devi)->devi_bus_ctl = pdevi;
3132 
3133 #ifdef DEBUG
3134 	if (optimize_dtree == 0)
3135 		return;
3136 #endif /* DEBUG */
3137 
3138 	b = pdevi->devi_ops->devo_bus_ops;
3139 
3140 	if (i_ddi_map_fault == b->bus_map_fault) {
3141 		DEVI(devi)->devi_bus_map_fault = pdevi->devi_bus_map_fault;
3142 		debug_dtree(devi, DEVI(devi)->devi_bus_map_fault,
3143 		    "bus_map_fault");
3144 	}
3145 
3146 	if (ddi_dma_map == b->bus_dma_map) {
3147 		DEVI(devi)->devi_bus_dma_map = pdevi->devi_bus_dma_map;
3148 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_map, "bus_dma_map");
3149 	}
3150 
3151 	if (ddi_dma_allochdl == b->bus_dma_allochdl) {
3152 		DEVI(devi)->devi_bus_dma_allochdl =
3153 		    pdevi->devi_bus_dma_allochdl;
3154 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_allochdl,
3155 		    "bus_dma_allochdl");
3156 	}
3157 
3158 	if (ddi_dma_freehdl == b->bus_dma_freehdl) {
3159 		DEVI(devi)->devi_bus_dma_freehdl = pdevi->devi_bus_dma_freehdl;
3160 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_freehdl,
3161 		    "bus_dma_freehdl");
3162 	}
3163 
3164 	if (ddi_dma_bindhdl == b->bus_dma_bindhdl) {
3165 		DEVI(devi)->devi_bus_dma_bindhdl = pdevi->devi_bus_dma_bindhdl;
3166 		DEVI(devi)->devi_bus_dma_bindfunc =
3167 		    pdevi->devi_bus_dma_bindhdl->devi_ops->
3168 		    devo_bus_ops->bus_dma_bindhdl;
3169 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_bindhdl,
3170 		    "bus_dma_bindhdl");
3171 	}
3172 
3173 	if (ddi_dma_unbindhdl == b->bus_dma_unbindhdl) {
3174 		DEVI(devi)->devi_bus_dma_unbindhdl =
3175 		    pdevi->devi_bus_dma_unbindhdl;
3176 		DEVI(devi)->devi_bus_dma_unbindfunc =
3177 		    pdevi->devi_bus_dma_unbindhdl->devi_ops->
3178 		    devo_bus_ops->bus_dma_unbindhdl;
3179 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_unbindhdl,
3180 		    "bus_dma_unbindhdl");
3181 	}
3182 
3183 	if (ddi_dma_flush == b->bus_dma_flush) {
3184 		DEVI(devi)->devi_bus_dma_flush = pdevi->devi_bus_dma_flush;
3185 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_flush,
3186 		    "bus_dma_flush");
3187 	}
3188 
3189 	if (ddi_dma_win == b->bus_dma_win) {
3190 		DEVI(devi)->devi_bus_dma_win = pdevi->devi_bus_dma_win;
3191 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_win,
3192 		    "bus_dma_win");
3193 	}
3194 
3195 	if (ddi_dma_mctl == b->bus_dma_ctl) {
3196 		DEVI(devi)->devi_bus_dma_ctl = pdevi->devi_bus_dma_ctl;
3197 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_ctl, "bus_dma_ctl");
3198 	}
3199 
3200 	if (ddi_ctlops == b->bus_ctl) {
3201 		DEVI(devi)->devi_bus_ctl = pdevi->devi_bus_ctl;
3202 		debug_dtree(devi, DEVI(devi)->devi_bus_ctl, "bus_ctl");
3203 	}
3204 }
3205 
3206 #define	MIN_DEVINFO_LOG_SIZE	max_ncpus
3207 #define	MAX_DEVINFO_LOG_SIZE	max_ncpus * 10
3208 
3209 static void
3210 da_log_init()
3211 {
3212 	devinfo_log_header_t *dh;
3213 	int logsize = devinfo_log_size;
3214 
3215 	if (logsize == 0)
3216 		logsize = MIN_DEVINFO_LOG_SIZE;
3217 	else if (logsize > MAX_DEVINFO_LOG_SIZE)
3218 		logsize = MAX_DEVINFO_LOG_SIZE;
3219 
3220 	dh = kmem_alloc(logsize * PAGESIZE, KM_SLEEP);
3221 	mutex_init(&dh->dh_lock, NULL, MUTEX_DEFAULT, NULL);
3222 	dh->dh_max = ((logsize * PAGESIZE) - sizeof (*dh)) /
3223 	    sizeof (devinfo_audit_t) + 1;
3224 	dh->dh_curr = -1;
3225 	dh->dh_hits = 0;
3226 
3227 	devinfo_audit_log = dh;
3228 }
3229 
3230 /*
3231  * Log the stack trace in per-devinfo audit structure and also enter
3232  * it into a system wide log for recording the time history.
3233  */
3234 static void
3235 da_log_enter(dev_info_t *dip)
3236 {
3237 	devinfo_audit_t *da_log, *da = DEVI(dip)->devi_audit;
3238 	devinfo_log_header_t *dh = devinfo_audit_log;
3239 
3240 	if (devinfo_audit_log == NULL)
3241 		return;
3242 
3243 	ASSERT(da != NULL);
3244 
3245 	da->da_devinfo = dip;
3246 	da->da_timestamp = gethrtime();
3247 	da->da_thread = curthread;
3248 	da->da_node_state = DEVI(dip)->devi_node_state;
3249 	da->da_device_state = DEVI(dip)->devi_state;
3250 	da->da_depth = getpcstack(da->da_stack, DDI_STACK_DEPTH);
3251 
3252 	/*
3253 	 * Copy into common log and note the location for tracing history
3254 	 */
3255 	mutex_enter(&dh->dh_lock);
3256 	dh->dh_hits++;
3257 	dh->dh_curr++;
3258 	if (dh->dh_curr >= dh->dh_max)
3259 		dh->dh_curr -= dh->dh_max;
3260 	da_log = &dh->dh_entry[dh->dh_curr];
3261 	mutex_exit(&dh->dh_lock);
3262 
3263 	bcopy(da, da_log, sizeof (devinfo_audit_t));
3264 	da->da_lastlog = da_log;
3265 }
3266 
3267 static void
3268 attach_drivers()
3269 {
3270 	int i;
3271 	for (i = 0; i < devcnt; i++) {
3272 		struct devnames *dnp = &devnamesp[i];
3273 		if ((dnp->dn_flags & DN_FORCE_ATTACH) &&
3274 		    (ddi_hold_installed_driver((major_t)i) != NULL))
3275 			ddi_rele_driver((major_t)i);
3276 	}
3277 }
3278 
3279 /*
3280  * Launch a thread to force attach drivers. This avoids penalty on boot time.
3281  */
3282 void
3283 i_ddi_forceattach_drivers()
3284 {
3285 	/*
3286 	 * On i386, the USB drivers need to load and take over from the
3287 	 * SMM BIOS drivers ASAP after consconfig(), so make sure they
3288 	 * get loaded right here rather than letting the thread do it.
3289 	 *
3290 	 * The order here is important.  EHCI must be loaded first, as
3291 	 * we have observed many systems on which hangs occur if the
3292 	 * {U,O}HCI companion controllers take over control from the BIOS
3293 	 * before EHCI does.  These hangs are also caused by BIOSes leaving
3294 	 * interrupt-on-port-change enabled in the ehci controller, so that
3295 	 * when uhci/ohci reset themselves, it induces a port change on
3296 	 * the ehci companion controller.  Since there's no interrupt handler
3297 	 * installed at the time, the moment that interrupt is unmasked, an
3298 	 * interrupt storm will occur.	All this is averted when ehci is
3299 	 * loaded first.  And now you know..... the REST of the story.
3300 	 *
3301 	 * Regardless of platform, ehci needs to initialize first to avoid
3302 	 * unnecessary connects and disconnects on the companion controller
3303 	 * when ehci sets up the routing.
3304 	 */
3305 	(void) ddi_hold_installed_driver(ddi_name_to_major("ehci"));
3306 	(void) ddi_hold_installed_driver(ddi_name_to_major("uhci"));
3307 	(void) ddi_hold_installed_driver(ddi_name_to_major("ohci"));
3308 
3309 	/*
3310 	 * Attach IB VHCI driver before the force-attach thread attaches the
3311 	 * IB HCA driver. IB HCA driver will fail if IB Nexus has not yet
3312 	 * been attached.
3313 	 */
3314 	(void) ddi_hold_installed_driver(ddi_name_to_major("ib"));
3315 
3316 	(void) thread_create(NULL, 0, (void (*)())attach_drivers, NULL, 0, &p0,
3317 	    TS_RUN, minclsyspri);
3318 }
3319 
3320 /*
3321  * This is a private DDI interface for optimizing boot performance.
3322  * I/O subsystem initialization is considered complete when devfsadm
3323  * is executed.
3324  *
3325  * NOTE: The start of syseventd happens to be a convenient indicator
3326  *	of the completion of I/O initialization during boot.
3327  *	The implementation should be replaced by something more robust.
3328  */
3329 int
3330 i_ddi_io_initialized()
3331 {
3332 	extern int sysevent_daemon_init;
3333 	return (sysevent_daemon_init);
3334 }
3335 
3336 /*
3337  * May be used to determine system boot state
3338  * "Available" means the system is for the most part up
3339  * and initialized, with all system services either up or
3340  * capable of being started.  This state is set by devfsadm
3341  * during the boot process.  The /dev filesystem infers
3342  * from this when implicit reconfig can be performed,
3343  * ie, devfsadm can be invoked.  Please avoid making
3344  * further use of this unless it's really necessary.
3345  */
3346 int
3347 i_ddi_sysavail()
3348 {
3349 	return (devname_state & DS_SYSAVAIL);
3350 }
3351 
3352 /*
3353  * May be used to determine if boot is a reconfigure boot.
3354  */
3355 int
3356 i_ddi_reconfig()
3357 {
3358 	return (devname_state & DS_RECONFIG);
3359 }
3360 
3361 /*
3362  * Note system services are up, inform /dev.
3363  */
3364 void
3365 i_ddi_set_sysavail()
3366 {
3367 	if ((devname_state & DS_SYSAVAIL) == 0) {
3368 		devname_state |= DS_SYSAVAIL;
3369 		sdev_devstate_change();
3370 	}
3371 }
3372 
3373 /*
3374  * Note reconfiguration boot, inform /dev.
3375  */
3376 void
3377 i_ddi_set_reconfig()
3378 {
3379 	if ((devname_state & DS_RECONFIG) == 0) {
3380 		devname_state |= DS_RECONFIG;
3381 		sdev_devstate_change();
3382 	}
3383 }
3384 
3385 
3386 /*
3387  * device tree walking
3388  */
3389 
3390 struct walk_elem {
3391 	struct walk_elem *next;
3392 	dev_info_t *dip;
3393 };
3394 
3395 static void
3396 free_list(struct walk_elem *list)
3397 {
3398 	while (list) {
3399 		struct walk_elem *next = list->next;
3400 		kmem_free(list, sizeof (*list));
3401 		list = next;
3402 	}
3403 }
3404 
3405 static void
3406 append_node(struct walk_elem **list, dev_info_t *dip)
3407 {
3408 	struct walk_elem *tail;
3409 	struct walk_elem *elem = kmem_alloc(sizeof (*elem), KM_SLEEP);
3410 
3411 	elem->next = NULL;
3412 	elem->dip = dip;
3413 
3414 	if (*list == NULL) {
3415 		*list = elem;
3416 		return;
3417 	}
3418 
3419 	tail = *list;
3420 	while (tail->next)
3421 		tail = tail->next;
3422 
3423 	tail->next = elem;
3424 }
3425 
3426 /*
3427  * The implementation of ddi_walk_devs().
3428  */
3429 static int
3430 walk_devs(dev_info_t *dip, int (*f)(dev_info_t *, void *), void *arg,
3431     int do_locking)
3432 {
3433 	struct walk_elem *head = NULL;
3434 
3435 	/*
3436 	 * Do it in two passes. First pass invoke callback on each
3437 	 * dip on the sibling list. Second pass invoke callback on
3438 	 * children of each dip.
3439 	 */
3440 	while (dip) {
3441 		switch ((*f)(dip, arg)) {
3442 		case DDI_WALK_TERMINATE:
3443 			free_list(head);
3444 			return (DDI_WALK_TERMINATE);
3445 
3446 		case DDI_WALK_PRUNESIB:
3447 			/* ignore sibling by setting dip to NULL */
3448 			append_node(&head, dip);
3449 			dip = NULL;
3450 			break;
3451 
3452 		case DDI_WALK_PRUNECHILD:
3453 			/* don't worry about children */
3454 			dip = ddi_get_next_sibling(dip);
3455 			break;
3456 
3457 		case DDI_WALK_CONTINUE:
3458 		default:
3459 			append_node(&head, dip);
3460 			dip = ddi_get_next_sibling(dip);
3461 			break;
3462 		}
3463 
3464 	}
3465 
3466 	/* second pass */
3467 	while (head) {
3468 		int circ;
3469 		struct walk_elem *next = head->next;
3470 
3471 		if (do_locking)
3472 			ndi_devi_enter(head->dip, &circ);
3473 		if (walk_devs(ddi_get_child(head->dip), f, arg, do_locking) ==
3474 		    DDI_WALK_TERMINATE) {
3475 			if (do_locking)
3476 				ndi_devi_exit(head->dip, circ);
3477 			free_list(head);
3478 			return (DDI_WALK_TERMINATE);
3479 		}
3480 		if (do_locking)
3481 			ndi_devi_exit(head->dip, circ);
3482 		kmem_free(head, sizeof (*head));
3483 		head = next;
3484 	}
3485 
3486 	return (DDI_WALK_CONTINUE);
3487 }
3488 
3489 /*
3490  * This general-purpose routine traverses the tree of dev_info nodes,
3491  * starting from the given node, and calls the given function for each
3492  * node that it finds with the current node and the pointer arg (which
3493  * can point to a structure of information that the function
3494  * needs) as arguments.
3495  *
3496  * It does the walk a layer at a time, not depth-first. The given function
3497  * must return one of the following values:
3498  *	DDI_WALK_CONTINUE
3499  *	DDI_WALK_PRUNESIB
3500  *	DDI_WALK_PRUNECHILD
3501  *	DDI_WALK_TERMINATE
3502  *
3503  * N.B. Since we walk the sibling list, the caller must ensure that
3504  *	the parent of dip is held against changes, unless the parent
3505  *	is rootnode.  ndi_devi_enter() on the parent is sufficient.
3506  *
3507  *	To avoid deadlock situations, caller must not attempt to
3508  *	configure/unconfigure/remove device node in (*f)(), nor should
3509  *	it attempt to recurse on other nodes in the system. Any
3510  *	ndi_devi_enter() done by (*f)() must occur 'at-or-below' the
3511  *	node entered prior to ddi_walk_devs(). Furthermore, if (*f)()
3512  *	does any multi-threading (in framework *or* in driver) then the
3513  *	ndi_devi_enter() calls done by dependent threads must be
3514  *	'strictly-below'.
3515  *
3516  *	This is not callable from device autoconfiguration routines.
3517  *	They include, but not limited to, _init(9e), _fini(9e), probe(9e),
3518  *	attach(9e), and detach(9e).
3519  */
3520 
3521 void
3522 ddi_walk_devs(dev_info_t *dip, int (*f)(dev_info_t *, void *), void *arg)
3523 {
3524 
3525 	ASSERT(dip == NULL || ddi_get_parent(dip) == NULL ||
3526 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
3527 
3528 	(void) walk_devs(dip, f, arg, 1);
3529 }
3530 
3531 /*
3532  * This is a general-purpose routine traverses the per-driver list
3533  * and calls the given function for each node. must return one of
3534  * the following values:
3535  *	DDI_WALK_CONTINUE
3536  *	DDI_WALK_TERMINATE
3537  *
3538  * N.B. The same restrictions from ddi_walk_devs() apply.
3539  */
3540 
3541 void
3542 e_ddi_walk_driver(char *drv, int (*f)(dev_info_t *, void *), void *arg)
3543 {
3544 	major_t major;
3545 	struct devnames *dnp;
3546 	dev_info_t *dip;
3547 
3548 	major = ddi_name_to_major(drv);
3549 	if (major == DDI_MAJOR_T_NONE)
3550 		return;
3551 
3552 	dnp = &devnamesp[major];
3553 	LOCK_DEV_OPS(&dnp->dn_lock);
3554 	dip = dnp->dn_head;
3555 	while (dip) {
3556 		ndi_hold_devi(dip);
3557 		UNLOCK_DEV_OPS(&dnp->dn_lock);
3558 		if ((*f)(dip, arg) == DDI_WALK_TERMINATE) {
3559 			ndi_rele_devi(dip);
3560 			return;
3561 		}
3562 		LOCK_DEV_OPS(&dnp->dn_lock);
3563 		ndi_rele_devi(dip);
3564 		dip = ddi_get_next(dip);
3565 	}
3566 	UNLOCK_DEV_OPS(&dnp->dn_lock);
3567 }
3568 
3569 /*
3570  * argument to i_find_devi, a devinfo node search callback function.
3571  */
3572 struct match_info {
3573 	dev_info_t	*dip;		/* result */
3574 	char		*nodename;	/* if non-null, nodename must match */
3575 	int		instance;	/* if != -1, instance must match */
3576 	int		attached;	/* if != 0, i_ddi_devi_attached() */
3577 };
3578 
3579 static int
3580 i_find_devi(dev_info_t *dip, void *arg)
3581 {
3582 	struct match_info *info = (struct match_info *)arg;
3583 
3584 	if (((info->nodename == NULL) ||
3585 	    (strcmp(ddi_node_name(dip), info->nodename) == 0)) &&
3586 	    ((info->instance == -1) ||
3587 	    (ddi_get_instance(dip) == info->instance)) &&
3588 	    ((info->attached == 0) || i_ddi_devi_attached(dip))) {
3589 		info->dip = dip;
3590 		ndi_hold_devi(dip);
3591 		return (DDI_WALK_TERMINATE);
3592 	}
3593 
3594 	return (DDI_WALK_CONTINUE);
3595 }
3596 
3597 /*
3598  * Find dip with a known node name and instance and return with it held
3599  */
3600 dev_info_t *
3601 ddi_find_devinfo(char *nodename, int instance, int attached)
3602 {
3603 	struct match_info	info;
3604 
3605 	info.nodename = nodename;
3606 	info.instance = instance;
3607 	info.attached = attached;
3608 	info.dip = NULL;
3609 
3610 	ddi_walk_devs(ddi_root_node(), i_find_devi, &info);
3611 	return (info.dip);
3612 }
3613 
3614 extern ib_boot_prop_t *iscsiboot_prop;
3615 static void
3616 i_ddi_parse_iscsi_name(char *name, char **nodename, char **addrname,
3617     char **minorname)
3618 {
3619 	char *cp, *colon;
3620 	static char nulladdrname[] = "";
3621 
3622 	/* default values */
3623 	if (nodename)
3624 		*nodename = name;
3625 	if (addrname)
3626 		*addrname = nulladdrname;
3627 	if (minorname)
3628 		*minorname = NULL;
3629 
3630 	cp = colon = name;
3631 	while (*cp != '\0') {
3632 		if (addrname && *cp == '@') {
3633 			*addrname = cp + 1;
3634 			*cp = '\0';
3635 		} else if (minorname && *cp == ':') {
3636 			*minorname = cp + 1;
3637 			colon = cp;
3638 		}
3639 		++cp;
3640 	}
3641 	if (colon != name) {
3642 		*colon = '\0';
3643 	}
3644 }
3645 
3646 /*
3647  * Parse for name, addr, and minor names. Some args may be NULL.
3648  */
3649 void
3650 i_ddi_parse_name(char *name, char **nodename, char **addrname, char **minorname)
3651 {
3652 	char *cp;
3653 	static char nulladdrname[] = "";
3654 
3655 	/* default values */
3656 	if (nodename)
3657 		*nodename = name;
3658 	if (addrname)
3659 		*addrname = nulladdrname;
3660 	if (minorname)
3661 		*minorname = NULL;
3662 
3663 	cp = name;
3664 	while (*cp != '\0') {
3665 		if (addrname && *cp == '@') {
3666 			*addrname = cp + 1;
3667 			*cp = '\0';
3668 		} else if (minorname && *cp == ':') {
3669 			*minorname = cp + 1;
3670 			*cp = '\0';
3671 		}
3672 		++cp;
3673 	}
3674 }
3675 
3676 static char *
3677 child_path_to_driver(dev_info_t *parent, char *child_name, char *unit_address)
3678 {
3679 	char *p, *drvname = NULL;
3680 	major_t maj;
3681 
3682 	/*
3683 	 * Construct the pathname and ask the implementation
3684 	 * if it can do a driver = f(pathname) for us, if not
3685 	 * we'll just default to using the node-name that
3686 	 * was given to us.  We want to do this first to
3687 	 * allow the platform to use 'generic' names for
3688 	 * legacy device drivers.
3689 	 */
3690 	p = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
3691 	(void) ddi_pathname(parent, p);
3692 	(void) strcat(p, "/");
3693 	(void) strcat(p, child_name);
3694 	if (unit_address && *unit_address) {
3695 		(void) strcat(p, "@");
3696 		(void) strcat(p, unit_address);
3697 	}
3698 
3699 	/*
3700 	 * Get the binding. If there is none, return the child_name
3701 	 * and let the caller deal with it.
3702 	 */
3703 	maj = path_to_major(p);
3704 
3705 	kmem_free(p, MAXPATHLEN);
3706 
3707 	if (maj != DDI_MAJOR_T_NONE)
3708 		drvname = ddi_major_to_name(maj);
3709 	if (drvname == NULL)
3710 		drvname = child_name;
3711 
3712 	return (drvname);
3713 }
3714 
3715 
3716 #define	PCI_EX_CLASS	"pciexclass"
3717 #define	PCI_EX		"pciex"
3718 #define	PCI_CLASS	"pciclass"
3719 #define	PCI		"pci"
3720 
3721 int
3722 ddi_is_pci_dip(dev_info_t *dip)
3723 {
3724 	char	*prop = NULL;
3725 
3726 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
3727 	    "compatible", &prop) == DDI_PROP_SUCCESS) {
3728 		ASSERT(prop);
3729 		if (strncmp(prop, PCI_EX_CLASS, sizeof (PCI_EX_CLASS) - 1)
3730 		    == 0 ||
3731 		    strncmp(prop, PCI_EX, sizeof (PCI_EX)- 1)
3732 		    == 0 ||
3733 		    strncmp(prop, PCI_CLASS, sizeof (PCI_CLASS) - 1)
3734 		    == 0 ||
3735 		    strncmp(prop, PCI, sizeof (PCI) - 1)
3736 		    == 0) {
3737 			ddi_prop_free(prop);
3738 			return (1);
3739 		}
3740 	}
3741 
3742 	if (prop != NULL) {
3743 		ddi_prop_free(prop);
3744 	}
3745 
3746 	return (0);
3747 }
3748 
3749 /*
3750  * Given the pathname of a device, fill in the dev_info_t value and/or the
3751  * dev_t value and/or the spectype, depending on which parameters are non-NULL.
3752  * If there is an error, this function returns -1.
3753  *
3754  * NOTE: If this function returns the dev_info_t structure, then it
3755  * does so with a hold on the devi. Caller should ensure that they get
3756  * decremented via ddi_release_devi() or ndi_rele_devi();
3757  *
3758  * This function can be invoked in the boot case for a pathname without
3759  * device argument (:xxxx), traditionally treated as a minor name.
3760  * In this case, we do the following
3761  * (1) search the minor node of type DDM_DEFAULT.
3762  * (2) if no DDM_DEFAULT minor exists, then the first non-alias minor is chosen.
3763  * (3) if neither exists, a dev_t is faked with minor number = instance.
3764  * As of S9 FCS, no instance of #1 exists. #2 is used by several platforms
3765  * to default the boot partition to :a possibly by other OBP definitions.
3766  * #3 is used for booting off network interfaces, most SPARC network
3767  * drivers support Style-2 only, so only DDM_ALIAS minor exists.
3768  *
3769  * It is possible for OBP to present device args at the end of the path as
3770  * well as in the middle. For example, with IB the following strings are
3771  * valid boot paths.
3772  *	a /pci@8,700000/ib@1,2:port=1,pkey=ff,dhcp,...
3773  *	b /pci@8,700000/ib@1,1:port=1/ioc@xxxxxx,yyyyyyy:dhcp
3774  * Case (a), we first look for minor node "port=1,pkey...".
3775  * Failing that, we will pass "port=1,pkey..." to the bus_config
3776  * entry point of ib (HCA) driver.
3777  * Case (b), configure ib@1,1 as usual. Then invoke ib's bus_config
3778  * with argument "ioc@xxxxxxx,yyyyyyy:port=1". After configuring
3779  * the ioc, look for minor node dhcp. If not found, pass ":dhcp"
3780  * to ioc's bus_config entry point.
3781  */
3782 int
3783 resolve_pathname(char *pathname,
3784 	dev_info_t **dipp, dev_t *devtp, int *spectypep)
3785 {
3786 	int			error;
3787 	dev_info_t		*parent, *child;
3788 	struct pathname		pn;
3789 	char			*component, *config_name;
3790 	char			*minorname = NULL;
3791 	char			*prev_minor = NULL;
3792 	dev_t			devt = NODEV;
3793 	int			spectype;
3794 	struct ddi_minor_data	*dmn;
3795 	int			circ;
3796 
3797 	if (*pathname != '/')
3798 		return (EINVAL);
3799 	parent = ddi_root_node();	/* Begin at the top of the tree */
3800 
3801 	if (error = pn_get(pathname, UIO_SYSSPACE, &pn))
3802 		return (error);
3803 	pn_skipslash(&pn);
3804 
3805 	ASSERT(i_ddi_devi_attached(parent));
3806 	ndi_hold_devi(parent);
3807 
3808 	component = kmem_alloc(MAXNAMELEN, KM_SLEEP);
3809 	config_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
3810 
3811 	while (pn_pathleft(&pn)) {
3812 		/* remember prev minor (:xxx) in the middle of path */
3813 		if (minorname)
3814 			prev_minor = i_ddi_strdup(minorname, KM_SLEEP);
3815 
3816 		/* Get component and chop off minorname */
3817 		(void) pn_getcomponent(&pn, component);
3818 		if ((iscsiboot_prop != NULL) &&
3819 		    (strcmp((DEVI(parent)->devi_node_name), "iscsi") == 0)) {
3820 			i_ddi_parse_iscsi_name(component, NULL, NULL,
3821 			    &minorname);
3822 		} else {
3823 			i_ddi_parse_name(component, NULL, NULL, &minorname);
3824 		}
3825 		if (prev_minor == NULL) {
3826 			(void) snprintf(config_name, MAXNAMELEN, "%s",
3827 			    component);
3828 		} else {
3829 			(void) snprintf(config_name, MAXNAMELEN, "%s:%s",
3830 			    component, prev_minor);
3831 			kmem_free(prev_minor, strlen(prev_minor) + 1);
3832 			prev_minor = NULL;
3833 		}
3834 
3835 		/*
3836 		 * Find and configure the child
3837 		 */
3838 		if (ndi_devi_config_one(parent, config_name, &child,
3839 		    NDI_PROMNAME | NDI_NO_EVENT) != NDI_SUCCESS) {
3840 			ndi_rele_devi(parent);
3841 			pn_free(&pn);
3842 			kmem_free(component, MAXNAMELEN);
3843 			kmem_free(config_name, MAXNAMELEN);
3844 			return (-1);
3845 		}
3846 
3847 		ASSERT(i_ddi_devi_attached(child));
3848 		ndi_rele_devi(parent);
3849 		parent = child;
3850 		pn_skipslash(&pn);
3851 	}
3852 
3853 	/*
3854 	 * First look for a minor node matching minorname.
3855 	 * Failing that, try to pass minorname to bus_config().
3856 	 */
3857 	if (minorname && i_ddi_minorname_to_devtspectype(parent,
3858 	    minorname, &devt, &spectype) == DDI_FAILURE) {
3859 		(void) snprintf(config_name, MAXNAMELEN, "%s", minorname);
3860 		if (ndi_devi_config_obp_args(parent,
3861 		    config_name, &child, 0) != NDI_SUCCESS) {
3862 			ndi_rele_devi(parent);
3863 			pn_free(&pn);
3864 			kmem_free(component, MAXNAMELEN);
3865 			kmem_free(config_name, MAXNAMELEN);
3866 			NDI_CONFIG_DEBUG((CE_NOTE,
3867 			    "%s: minor node not found\n", pathname));
3868 			return (-1);
3869 		}
3870 		minorname = NULL;	/* look for default minor */
3871 		ASSERT(i_ddi_devi_attached(child));
3872 		ndi_rele_devi(parent);
3873 		parent = child;
3874 	}
3875 
3876 	if (devtp || spectypep) {
3877 		if (minorname == NULL) {
3878 			/*
3879 			 * Search for a default entry with an active
3880 			 * ndi_devi_enter to protect the devi_minor list.
3881 			 */
3882 			ndi_devi_enter(parent, &circ);
3883 			for (dmn = DEVI(parent)->devi_minor; dmn;
3884 			    dmn = dmn->next) {
3885 				if (dmn->type == DDM_DEFAULT) {
3886 					devt = dmn->ddm_dev;
3887 					spectype = dmn->ddm_spec_type;
3888 					break;
3889 				}
3890 			}
3891 
3892 			if (devt == NODEV) {
3893 				/*
3894 				 * No default minor node, try the first one;
3895 				 * else, assume 1-1 instance-minor mapping
3896 				 */
3897 				dmn = DEVI(parent)->devi_minor;
3898 				if (dmn && ((dmn->type == DDM_MINOR) ||
3899 				    (dmn->type == DDM_INTERNAL_PATH))) {
3900 					devt = dmn->ddm_dev;
3901 					spectype = dmn->ddm_spec_type;
3902 				} else {
3903 					devt = makedevice(
3904 					    DEVI(parent)->devi_major,
3905 					    ddi_get_instance(parent));
3906 					spectype = S_IFCHR;
3907 				}
3908 			}
3909 			ndi_devi_exit(parent, circ);
3910 		}
3911 		if (devtp)
3912 			*devtp = devt;
3913 		if (spectypep)
3914 			*spectypep = spectype;
3915 	}
3916 
3917 	pn_free(&pn);
3918 	kmem_free(component, MAXNAMELEN);
3919 	kmem_free(config_name, MAXNAMELEN);
3920 
3921 	/*
3922 	 * If there is no error, return the appropriate parameters
3923 	 */
3924 	if (dipp != NULL)
3925 		*dipp = parent;
3926 	else {
3927 		/*
3928 		 * We should really keep the ref count to keep the node from
3929 		 * detaching but ddi_pathname_to_dev_t() specifies a NULL dipp,
3930 		 * so we have no way of passing back the held dip.  Not holding
3931 		 * the dip allows detaches to occur - which can cause problems
3932 		 * for subsystems which call ddi_pathname_to_dev_t (console).
3933 		 *
3934 		 * Instead of holding the dip, we place a ddi-no-autodetach
3935 		 * property on the node to prevent auto detaching.
3936 		 *
3937 		 * The right fix is to remove ddi_pathname_to_dev_t and replace
3938 		 * it, and all references, with a call that specifies a dipp.
3939 		 * In addition, the callers of this new interfaces would then
3940 		 * need to call ndi_rele_devi when the reference is complete.
3941 		 *
3942 		 */
3943 		(void) ddi_prop_update_int(DDI_DEV_T_NONE, parent,
3944 		    DDI_NO_AUTODETACH, 1);
3945 		ndi_rele_devi(parent);
3946 	}
3947 
3948 	return (0);
3949 }
3950 
3951 /*
3952  * Given the pathname of a device, return the dev_t of the corresponding
3953  * device.  Returns NODEV on failure.
3954  *
3955  * Note that this call sets the DDI_NO_AUTODETACH property on the devinfo node.
3956  */
3957 dev_t
3958 ddi_pathname_to_dev_t(char *pathname)
3959 {
3960 	dev_t devt;
3961 	int error;
3962 
3963 	error = resolve_pathname(pathname, NULL, &devt, NULL);
3964 
3965 	return (error ? NODEV : devt);
3966 }
3967 
3968 /*
3969  * Translate a prom pathname to kernel devfs pathname.
3970  * Caller is assumed to allocate devfspath memory of
3971  * size at least MAXPATHLEN
3972  *
3973  * The prom pathname may not include minor name, but
3974  * devfs pathname has a minor name portion.
3975  */
3976 int
3977 i_ddi_prompath_to_devfspath(char *prompath, char *devfspath)
3978 {
3979 	dev_t		devt = (dev_t)NODEV;
3980 	dev_info_t	*dip = NULL;
3981 	char		*minor_name = NULL;
3982 	int		spectype;
3983 	int		error;
3984 	int		circ;
3985 
3986 	error = resolve_pathname(prompath, &dip, &devt, &spectype);
3987 	if (error)
3988 		return (DDI_FAILURE);
3989 	ASSERT(dip && devt != NODEV);
3990 
3991 	/*
3992 	 * Get in-kernel devfs pathname
3993 	 */
3994 	(void) ddi_pathname(dip, devfspath);
3995 
3996 	ndi_devi_enter(dip, &circ);
3997 	minor_name = i_ddi_devtspectype_to_minorname(dip, devt, spectype);
3998 	if (minor_name) {
3999 		(void) strcat(devfspath, ":");
4000 		(void) strcat(devfspath, minor_name);
4001 	} else {
4002 		/*
4003 		 * If minor_name is NULL, we have an alias minor node.
4004 		 * So manufacture a path to the corresponding clone minor.
4005 		 */
4006 		(void) snprintf(devfspath, MAXPATHLEN, "%s:%s",
4007 		    CLONE_PATH, ddi_driver_name(dip));
4008 	}
4009 	ndi_devi_exit(dip, circ);
4010 
4011 	/* release hold from resolve_pathname() */
4012 	ndi_rele_devi(dip);
4013 	return (0);
4014 }
4015 
4016 /*
4017  * This function is intended to identify drivers that must quiesce for fast
4018  * reboot to succeed.  It does not claim to have more knowledge about the device
4019  * than its driver.  If a driver has implemented quiesce(), it will be invoked;
4020  * if a so identified driver does not manage any device that needs to be
4021  * quiesced, it must explicitly set its devo_quiesce dev_op to
4022  * ddi_quiesce_not_needed.
4023  */
4024 static int skip_pseudo = 1;	/* Skip pseudo devices */
4025 static int skip_non_hw = 1;	/* Skip devices with no hardware property */
4026 static int
4027 should_implement_quiesce(dev_info_t *dip)
4028 {
4029 	struct dev_info *devi = DEVI(dip);
4030 	dev_info_t *pdip;
4031 
4032 	/*
4033 	 * If dip is pseudo and skip_pseudo is set, driver doesn't have to
4034 	 * implement quiesce().
4035 	 */
4036 	if (skip_pseudo &&
4037 	    strncmp(ddi_binding_name(dip), "pseudo", sizeof ("pseudo")) == 0)
4038 		return (0);
4039 
4040 	/*
4041 	 * If parent dip is pseudo and skip_pseudo is set, driver doesn't have
4042 	 * to implement quiesce().
4043 	 */
4044 	if (skip_pseudo && (pdip = ddi_get_parent(dip)) != NULL &&
4045 	    strncmp(ddi_binding_name(pdip), "pseudo", sizeof ("pseudo")) == 0)
4046 		return (0);
4047 
4048 	/*
4049 	 * If not attached, driver doesn't have to implement quiesce().
4050 	 */
4051 	if (!i_ddi_devi_attached(dip))
4052 		return (0);
4053 
4054 	/*
4055 	 * If dip has no hardware property and skip_non_hw is set,
4056 	 * driver doesn't have to implement quiesce().
4057 	 */
4058 	if (skip_non_hw && devi->devi_hw_prop_ptr == NULL)
4059 		return (0);
4060 
4061 	return (1);
4062 }
4063 
4064 static int
4065 driver_has_quiesce(struct dev_ops *ops)
4066 {
4067 	if ((ops->devo_rev >= 4) && (ops->devo_quiesce != nodev) &&
4068 	    (ops->devo_quiesce != NULL) && (ops->devo_quiesce != nulldev) &&
4069 	    (ops->devo_quiesce != ddi_quiesce_not_supported))
4070 		return (1);
4071 	else
4072 		return (0);
4073 }
4074 
4075 /*
4076  * Check to see if a driver has implemented the quiesce() DDI function.
4077  */
4078 int
4079 check_driver_quiesce(dev_info_t *dip, void *arg)
4080 {
4081 	struct dev_ops *ops;
4082 
4083 	if (!should_implement_quiesce(dip))
4084 		return (DDI_WALK_CONTINUE);
4085 
4086 	if ((ops = ddi_get_driver(dip)) == NULL)
4087 		return (DDI_WALK_CONTINUE);
4088 
4089 	if (driver_has_quiesce(ops)) {
4090 		if ((quiesce_debug & 0x2) == 0x2) {
4091 			if (ops->devo_quiesce == ddi_quiesce_not_needed)
4092 				cmn_err(CE_CONT, "%s does not need to be "
4093 				    "quiesced", ddi_driver_name(dip));
4094 			else
4095 				cmn_err(CE_CONT, "%s has quiesce routine",
4096 				    ddi_driver_name(dip));
4097 		}
4098 	} else {
4099 		if (arg != NULL)
4100 			*((int *)arg) = -1;
4101 		cmn_err(CE_WARN, "%s has no quiesce()", ddi_driver_name(dip));
4102 	}
4103 
4104 	return (DDI_WALK_CONTINUE);
4105 }
4106 
4107 /*
4108  * Quiesce device.
4109  */
4110 static void
4111 quiesce_one_device(dev_info_t *dip, void *arg)
4112 {
4113 	struct dev_ops *ops;
4114 	int should_quiesce = 0;
4115 
4116 	/*
4117 	 * If the device is not attached it doesn't need to be quiesced.
4118 	 */
4119 	if (!i_ddi_devi_attached(dip))
4120 		return;
4121 
4122 	if ((ops = ddi_get_driver(dip)) == NULL)
4123 		return;
4124 
4125 	should_quiesce = should_implement_quiesce(dip);
4126 
4127 	/*
4128 	 * If there's an implementation of quiesce(), always call it even if
4129 	 * some of the drivers don't have quiesce() or quiesce() have failed
4130 	 * so we can do force fast reboot.  The implementation of quiesce()
4131 	 * should not negatively affect a regular reboot.
4132 	 */
4133 	if (driver_has_quiesce(ops)) {
4134 		int rc = DDI_SUCCESS;
4135 
4136 		if (ops->devo_quiesce == ddi_quiesce_not_needed)
4137 			return;
4138 
4139 		rc = devi_quiesce(dip);
4140 
4141 		/* quiesce() should never fail */
4142 		ASSERT(rc == DDI_SUCCESS);
4143 
4144 		if (rc != DDI_SUCCESS && should_quiesce) {
4145 
4146 			if (arg != NULL)
4147 				*((int *)arg) = -1;
4148 		}
4149 	} else if (should_quiesce && arg != NULL) {
4150 		*((int *)arg) = -1;
4151 	}
4152 }
4153 
4154 /*
4155  * Traverse the dev info tree in a breadth-first manner so that we quiesce
4156  * children first.  All subtrees under the parent of dip will be quiesced.
4157  */
4158 void
4159 quiesce_devices(dev_info_t *dip, void *arg)
4160 {
4161 	/*
4162 	 * if we're reached here, the device tree better not be changing.
4163 	 * so either devinfo_freeze better be set or we better be panicing.
4164 	 */
4165 	ASSERT(devinfo_freeze || panicstr);
4166 
4167 	for (; dip != NULL; dip = ddi_get_next_sibling(dip)) {
4168 		quiesce_devices(ddi_get_child(dip), arg);
4169 
4170 		quiesce_one_device(dip, arg);
4171 	}
4172 }
4173 
4174 /*
4175  * Reset all the pure leaf drivers on the system at halt time
4176  */
4177 static int
4178 reset_leaf_device(dev_info_t *dip, void *arg)
4179 {
4180 	_NOTE(ARGUNUSED(arg))
4181 	struct dev_ops *ops;
4182 
4183 	/* if the device doesn't need to be reset then there's nothing to do */
4184 	if (!DEVI_NEED_RESET(dip))
4185 		return (DDI_WALK_CONTINUE);
4186 
4187 	/*
4188 	 * if the device isn't a char/block device or doesn't have a
4189 	 * reset entry point then there's nothing to do.
4190 	 */
4191 	ops = ddi_get_driver(dip);
4192 	if ((ops == NULL) || (ops->devo_cb_ops == NULL) ||
4193 	    (ops->devo_reset == nodev) || (ops->devo_reset == nulldev) ||
4194 	    (ops->devo_reset == NULL))
4195 		return (DDI_WALK_CONTINUE);
4196 
4197 	if (DEVI_IS_ATTACHING(dip) || DEVI_IS_DETACHING(dip)) {
4198 		static char path[MAXPATHLEN];
4199 
4200 		/*
4201 		 * bad news, this device has blocked in it's attach or
4202 		 * detach routine, which means it not safe to call it's
4203 		 * devo_reset() entry point.
4204 		 */
4205 		cmn_err(CE_WARN, "unable to reset device: %s",
4206 		    ddi_pathname(dip, path));
4207 		return (DDI_WALK_CONTINUE);
4208 	}
4209 
4210 	NDI_CONFIG_DEBUG((CE_NOTE, "resetting %s%d\n",
4211 	    ddi_driver_name(dip), ddi_get_instance(dip)));
4212 
4213 	(void) devi_reset(dip, DDI_RESET_FORCE);
4214 	return (DDI_WALK_CONTINUE);
4215 }
4216 
4217 void
4218 reset_leaves(void)
4219 {
4220 	/*
4221 	 * if we're reached here, the device tree better not be changing.
4222 	 * so either devinfo_freeze better be set or we better be panicing.
4223 	 */
4224 	ASSERT(devinfo_freeze || panicstr);
4225 
4226 	(void) walk_devs(top_devinfo, reset_leaf_device, NULL, 0);
4227 }
4228 
4229 
4230 /*
4231  * devtree_freeze() must be called before quiesce_devices() and reset_leaves()
4232  * during a normal system shutdown.  It attempts to ensure that there are no
4233  * outstanding attach or detach operations in progress when quiesce_devices() or
4234  * reset_leaves()is invoked.  It must be called before the system becomes
4235  * single-threaded because device attach and detach are multi-threaded
4236  * operations.	(note that during system shutdown the system doesn't actually
4237  * become single-thread since other threads still exist, but the shutdown thread
4238  * will disable preemption for itself, raise it's pil, and stop all the other
4239  * cpus in the system there by effectively making the system single-threaded.)
4240  */
4241 void
4242 devtree_freeze(void)
4243 {
4244 	int delayed = 0;
4245 
4246 	/* if we're panicing then the device tree isn't going to be changing */
4247 	if (panicstr)
4248 		return;
4249 
4250 	/* stop all dev_info state changes in the device tree */
4251 	devinfo_freeze = gethrtime();
4252 
4253 	/*
4254 	 * if we're not panicing and there are on-going attach or detach
4255 	 * operations, wait for up to 3 seconds for them to finish.  This
4256 	 * is a randomly chosen interval but this should be ok because:
4257 	 * - 3 seconds is very small relative to the deadman timer.
4258 	 * - normal attach and detach operations should be very quick.
4259 	 * - attach and detach operations are fairly rare.
4260 	 */
4261 	while (!panicstr && atomic_add_long_nv(&devinfo_attach_detach, 0) &&
4262 	    (delayed < 3)) {
4263 		delayed += 1;
4264 
4265 		/* do a sleeping wait for one second */
4266 		ASSERT(!servicing_interrupt());
4267 		delay(drv_usectohz(MICROSEC));
4268 	}
4269 }
4270 
4271 static int
4272 bind_dip(dev_info_t *dip, void *arg)
4273 {
4274 	_NOTE(ARGUNUSED(arg))
4275 	char	*path;
4276 	major_t	major, pmajor;
4277 
4278 	/*
4279 	 * If the node is currently bound to the wrong driver, try to unbind
4280 	 * so that we can rebind to the correct driver.
4281 	 */
4282 	if (i_ddi_node_state(dip) >= DS_BOUND) {
4283 		major = ddi_compatible_driver_major(dip, NULL);
4284 		if ((DEVI(dip)->devi_major == major) &&
4285 		    (i_ddi_node_state(dip) >= DS_INITIALIZED)) {
4286 			/*
4287 			 * Check for a path-oriented driver alias that
4288 			 * takes precedence over current driver binding.
4289 			 */
4290 			path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4291 			(void) ddi_pathname(dip, path);
4292 			pmajor = ddi_name_to_major(path);
4293 			if (driver_installed(pmajor))
4294 				major = pmajor;
4295 			kmem_free(path, MAXPATHLEN);
4296 		}
4297 
4298 		/* attempt unbind if current driver is incorrect */
4299 		if (driver_installed(major) &&
4300 		    (major != DEVI(dip)->devi_major))
4301 			(void) ndi_devi_unbind_driver(dip);
4302 	}
4303 
4304 	/* If unbound, try to bind to a driver */
4305 	if (i_ddi_node_state(dip) < DS_BOUND)
4306 		(void) ndi_devi_bind_driver(dip, 0);
4307 
4308 	return (DDI_WALK_CONTINUE);
4309 }
4310 
4311 void
4312 i_ddi_bind_devs(void)
4313 {
4314 	/* flush devfs so that ndi_devi_unbind_driver will work when possible */
4315 	(void) devfs_clean(top_devinfo, NULL, 0);
4316 
4317 	ddi_walk_devs(top_devinfo, bind_dip, (void *)NULL);
4318 }
4319 
4320 /* callback data for unbind_children_by_alias() */
4321 typedef struct unbind_data {
4322 	major_t	drv_major;
4323 	char	*drv_alias;
4324 	int	ndevs_bound;
4325 	int	unbind_errors;
4326 } unbind_data_t;
4327 
4328 /*
4329  * A utility function provided for testing and support convenience
4330  * Called for each device during an upgrade_drv -d bound to the alias
4331  * that cannot be unbound due to device in use.
4332  */
4333 static void
4334 unbind_alias_dev_in_use(dev_info_t *dip, char *alias)
4335 {
4336 	if (moddebug & MODDEBUG_BINDING) {
4337 		cmn_err(CE_CONT, "%s%d: state %d: bound to %s\n",
4338 		    ddi_driver_name(dip), ddi_get_instance(dip),
4339 		    i_ddi_node_state(dip), alias);
4340 	}
4341 }
4342 
4343 /*
4344  * walkdevs callback for unbind devices bound to specific driver
4345  * and alias.  Invoked within the context of update_drv -d <alias>.
4346  */
4347 static int
4348 unbind_children_by_alias(dev_info_t *dip, void *arg)
4349 {
4350 	int		circ;
4351 	dev_info_t	*cdip;
4352 	dev_info_t	*next;
4353 	unbind_data_t	*ub = (unbind_data_t *)(uintptr_t)arg;
4354 	int		rv;
4355 
4356 	/*
4357 	 * We are called from update_drv to try to unbind a specific
4358 	 * set of aliases for a driver.  Unbind what persistent nodes
4359 	 * we can, and return the number of nodes which cannot be unbound.
4360 	 * If not all nodes can be unbound, update_drv leaves the
4361 	 * state of the driver binding files unchanged, except in
4362 	 * the case of -f.
4363 	 */
4364 	ndi_devi_enter(dip, &circ);
4365 	for (cdip = ddi_get_child(dip); cdip; cdip = next) {
4366 		next = ddi_get_next_sibling(cdip);
4367 		if ((ddi_driver_major(cdip) != ub->drv_major) ||
4368 		    (strcmp(DEVI(cdip)->devi_node_name, ub->drv_alias) != 0))
4369 			continue;
4370 		if (i_ddi_node_state(cdip) >= DS_BOUND) {
4371 			rv = ndi_devi_unbind_driver(cdip);
4372 			if (rv != DDI_SUCCESS ||
4373 			    (i_ddi_node_state(cdip) >= DS_BOUND)) {
4374 				unbind_alias_dev_in_use(cdip, ub->drv_alias);
4375 				ub->ndevs_bound++;
4376 				continue;
4377 			}
4378 			if (ndi_dev_is_persistent_node(cdip) == 0)
4379 				(void) ddi_remove_child(cdip, 0);
4380 		}
4381 	}
4382 	ndi_devi_exit(dip, circ);
4383 
4384 	return (DDI_WALK_CONTINUE);
4385 }
4386 
4387 /*
4388  * Unbind devices by driver & alias
4389  * Context: update_drv [-f] -d -i <alias> <driver>
4390  */
4391 int
4392 i_ddi_unbind_devs_by_alias(major_t major, char *alias)
4393 {
4394 	unbind_data_t	*ub;
4395 	int		rv;
4396 
4397 	ub = kmem_zalloc(sizeof (*ub), KM_SLEEP);
4398 	ub->drv_major = major;
4399 	ub->drv_alias = alias;
4400 	ub->ndevs_bound = 0;
4401 	ub->unbind_errors = 0;
4402 
4403 	/* flush devfs so that ndi_devi_unbind_driver will work when possible */
4404 	(void) devfs_clean(top_devinfo, NULL, 0);
4405 	ddi_walk_devs(top_devinfo, unbind_children_by_alias,
4406 	    (void *)(uintptr_t)ub);
4407 
4408 	/* return the number of devices remaining bound to the alias */
4409 	rv = ub->ndevs_bound + ub->unbind_errors;
4410 	kmem_free(ub, sizeof (*ub));
4411 	return (rv);
4412 }
4413 
4414 /*
4415  * walkdevs callback for unbind devices by driver
4416  */
4417 static int
4418 unbind_children_by_driver(dev_info_t *dip, void *arg)
4419 {
4420 	int		circ;
4421 	dev_info_t	*cdip;
4422 	dev_info_t	*next;
4423 	major_t		major = (major_t)(uintptr_t)arg;
4424 	int		rv;
4425 
4426 	/*
4427 	 * We are called either from rem_drv or update_drv when reloading
4428 	 * a driver.conf file. In either case, we unbind persistent nodes
4429 	 * and destroy .conf nodes. In the case of rem_drv, this will be
4430 	 * the final state. In the case of update_drv,	i_ddi_bind_devs()
4431 	 * may be invoked later to re-enumerate (new) driver.conf rebind
4432 	 * persistent nodes.
4433 	 */
4434 	ndi_devi_enter(dip, &circ);
4435 	for (cdip = ddi_get_child(dip); cdip; cdip = next) {
4436 		next = ddi_get_next_sibling(cdip);
4437 		if (ddi_driver_major(cdip) != major)
4438 			continue;
4439 		if (i_ddi_node_state(cdip) >= DS_BOUND) {
4440 			rv = ndi_devi_unbind_driver(cdip);
4441 			if (rv == DDI_FAILURE ||
4442 			    (i_ddi_node_state(cdip) >= DS_BOUND))
4443 				continue;
4444 			if (ndi_dev_is_persistent_node(cdip) == 0)
4445 				(void) ddi_remove_child(cdip, 0);
4446 		}
4447 	}
4448 	ndi_devi_exit(dip, circ);
4449 
4450 	return (DDI_WALK_CONTINUE);
4451 }
4452 
4453 /*
4454  * Unbind devices by driver
4455  * Context: rem_drv or unload driver.conf
4456  */
4457 void
4458 i_ddi_unbind_devs(major_t major)
4459 {
4460 	/* flush devfs so that ndi_devi_unbind_driver will work when possible */
4461 	(void) devfs_clean(top_devinfo, NULL, 0);
4462 	ddi_walk_devs(top_devinfo, unbind_children_by_driver,
4463 	    (void *)(uintptr_t)major);
4464 }
4465 
4466 /*
4467  * I/O Hotplug control
4468  */
4469 
4470 /*
4471  * create and attach a dev_info node from a .conf file spec
4472  */
4473 static void
4474 init_spec_child(dev_info_t *pdip, struct hwc_spec *specp, uint_t flags)
4475 {
4476 	_NOTE(ARGUNUSED(flags))
4477 	dev_info_t *dip;
4478 	char *node_name;
4479 
4480 	if (((node_name = specp->hwc_devi_name) == NULL) ||
4481 	    (ddi_name_to_major(node_name) == DDI_MAJOR_T_NONE)) {
4482 		char *tmp = node_name;
4483 		if (tmp == NULL)
4484 			tmp = "<none>";
4485 		cmn_err(CE_CONT,
4486 		    "init_spec_child: parent=%s, bad spec (%s)\n",
4487 		    ddi_node_name(pdip), tmp);
4488 		return;
4489 	}
4490 
4491 	dip = i_ddi_alloc_node(pdip, node_name, (pnode_t)DEVI_PSEUDO_NODEID,
4492 	    -1, specp->hwc_devi_sys_prop_ptr, KM_SLEEP);
4493 
4494 	if (dip == NULL)
4495 		return;
4496 
4497 	if (ddi_initchild(pdip, dip) != DDI_SUCCESS)
4498 		(void) ddi_remove_child(dip, 0);
4499 }
4500 
4501 /*
4502  * Lookup hwc specs from hash tables and make children from the spec
4503  * Because some .conf children are "merge" nodes, we also initialize
4504  * .conf children to merge properties onto hardware nodes.
4505  *
4506  * The pdip must be held busy.
4507  */
4508 int
4509 i_ndi_make_spec_children(dev_info_t *pdip, uint_t flags)
4510 {
4511 	extern struct hwc_spec *hwc_get_child_spec(dev_info_t *, major_t);
4512 	int			circ;
4513 	struct hwc_spec		*list, *spec;
4514 
4515 	ndi_devi_enter(pdip, &circ);
4516 	if (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN) {
4517 		ndi_devi_exit(pdip, circ);
4518 		return (DDI_SUCCESS);
4519 	}
4520 
4521 	list = hwc_get_child_spec(pdip, DDI_MAJOR_T_NONE);
4522 	for (spec = list; spec != NULL; spec = spec->hwc_next) {
4523 		init_spec_child(pdip, spec, flags);
4524 	}
4525 	hwc_free_spec_list(list);
4526 
4527 	mutex_enter(&DEVI(pdip)->devi_lock);
4528 	DEVI(pdip)->devi_flags |= DEVI_MADE_CHILDREN;
4529 	mutex_exit(&DEVI(pdip)->devi_lock);
4530 	ndi_devi_exit(pdip, circ);
4531 	return (DDI_SUCCESS);
4532 }
4533 
4534 /*
4535  * Run initchild on all child nodes such that instance assignment
4536  * for multiport network cards are contiguous.
4537  *
4538  * The pdip must be held busy.
4539  */
4540 static void
4541 i_ndi_init_hw_children(dev_info_t *pdip, uint_t flags)
4542 {
4543 	dev_info_t *dip;
4544 
4545 	ASSERT(DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
4546 
4547 	/* contiguous instance assignment */
4548 	e_ddi_enter_instance();
4549 	dip = ddi_get_child(pdip);
4550 	while (dip) {
4551 		if (ndi_dev_is_persistent_node(dip))
4552 			(void) i_ndi_config_node(dip, DS_INITIALIZED, flags);
4553 		dip = ddi_get_next_sibling(dip);
4554 	}
4555 	e_ddi_exit_instance();
4556 }
4557 
4558 /*
4559  * report device status
4560  */
4561 static void
4562 i_ndi_devi_report_status_change(dev_info_t *dip, char *path)
4563 {
4564 	char *status;
4565 
4566 	if (!DEVI_NEED_REPORT(dip) ||
4567 	    (i_ddi_node_state(dip) < DS_INITIALIZED) ||
4568 	    ndi_dev_is_hidden_node(dip)) {
4569 		return;
4570 	}
4571 
4572 	/* Invalidate the devinfo snapshot cache */
4573 	i_ddi_di_cache_invalidate();
4574 
4575 	if (DEVI_IS_DEVICE_REMOVED(dip)) {
4576 		status = "removed";
4577 	} else if (DEVI_IS_DEVICE_OFFLINE(dip)) {
4578 		status = "offline";
4579 	} else if (DEVI_IS_DEVICE_DOWN(dip)) {
4580 		status = "down";
4581 	} else if (DEVI_IS_BUS_QUIESCED(dip)) {
4582 		status = "quiesced";
4583 	} else if (DEVI_IS_BUS_DOWN(dip)) {
4584 		status = "down";
4585 	} else if (i_ddi_devi_attached(dip)) {
4586 		status = "online";
4587 	} else {
4588 		status = "unknown";
4589 	}
4590 
4591 	if (path == NULL) {
4592 		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4593 		cmn_err(CE_CONT, "?%s (%s%d) %s\n",
4594 		    ddi_pathname(dip, path), ddi_driver_name(dip),
4595 		    ddi_get_instance(dip), status);
4596 		kmem_free(path, MAXPATHLEN);
4597 	} else {
4598 		cmn_err(CE_CONT, "?%s (%s%d) %s\n",
4599 		    path, ddi_driver_name(dip),
4600 		    ddi_get_instance(dip), status);
4601 	}
4602 
4603 	mutex_enter(&(DEVI(dip)->devi_lock));
4604 	DEVI_REPORT_DONE(dip);
4605 	mutex_exit(&(DEVI(dip)->devi_lock));
4606 }
4607 
4608 /*
4609  * log a notification that a dev_info node has been configured.
4610  */
4611 static int
4612 i_log_devfs_add_devinfo(dev_info_t *dip, uint_t flags)
4613 {
4614 	int			se_err;
4615 	char			*pathname;
4616 	sysevent_t		*ev;
4617 	sysevent_id_t		eid;
4618 	sysevent_value_t	se_val;
4619 	sysevent_attr_list_t	*ev_attr_list = NULL;
4620 	char			*class_name;
4621 	int			no_transport = 0;
4622 
4623 	ASSERT(dip && ddi_get_parent(dip) &&
4624 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
4625 
4626 	/* do not generate ESC_DEVFS_DEVI_ADD event during boot */
4627 	if (!i_ddi_io_initialized())
4628 		return (DDI_SUCCESS);
4629 
4630 	/* Invalidate the devinfo snapshot cache */
4631 	i_ddi_di_cache_invalidate();
4632 
4633 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_ADD, EP_DDI, SE_SLEEP);
4634 
4635 	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4636 
4637 	(void) ddi_pathname(dip, pathname);
4638 	ASSERT(strlen(pathname));
4639 
4640 	se_val.value_type = SE_DATA_TYPE_STRING;
4641 	se_val.value.sv_string = pathname;
4642 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
4643 	    &se_val, SE_SLEEP) != 0) {
4644 		goto fail;
4645 	}
4646 
4647 	/* add the device class attribute */
4648 	if ((class_name = i_ddi_devi_class(dip)) != NULL) {
4649 		se_val.value_type = SE_DATA_TYPE_STRING;
4650 		se_val.value.sv_string = class_name;
4651 
4652 		if (sysevent_add_attr(&ev_attr_list,
4653 		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
4654 			sysevent_free_attr(ev_attr_list);
4655 			goto fail;
4656 		}
4657 	}
4658 
4659 	/*
4660 	 * must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
4661 	 * in which case the branch event will be logged by the caller
4662 	 * after the entire branch has been configured.
4663 	 */
4664 	if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
4665 		/*
4666 		 * Instead of logging a separate branch event just add
4667 		 * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
4668 		 * generate a EC_DEV_BRANCH event.
4669 		 */
4670 		se_val.value_type = SE_DATA_TYPE_INT32;
4671 		se_val.value.sv_int32 = 1;
4672 		if (sysevent_add_attr(&ev_attr_list,
4673 		    DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
4674 			sysevent_free_attr(ev_attr_list);
4675 			goto fail;
4676 		}
4677 	}
4678 
4679 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
4680 		sysevent_free_attr(ev_attr_list);
4681 		goto fail;
4682 	}
4683 
4684 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
4685 		if (se_err == SE_NO_TRANSPORT)
4686 			no_transport = 1;
4687 		goto fail;
4688 	}
4689 
4690 	sysevent_free(ev);
4691 	kmem_free(pathname, MAXPATHLEN);
4692 
4693 	return (DDI_SUCCESS);
4694 
4695 fail:
4696 	cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_ADD event for %s%s",
4697 	    pathname, (no_transport) ? " (syseventd not responding)" : "");
4698 
4699 	cmn_err(CE_WARN, "/dev may not be current for driver %s. "
4700 	    "Run devfsadm -i %s",
4701 	    ddi_driver_name(dip), ddi_driver_name(dip));
4702 
4703 	sysevent_free(ev);
4704 	kmem_free(pathname, MAXPATHLEN);
4705 	return (DDI_SUCCESS);
4706 }
4707 
4708 /*
4709  * log a notification that a dev_info node has been unconfigured.
4710  */
4711 static int
4712 i_log_devfs_remove_devinfo(char *pathname, char *class_name, char *driver_name,
4713     int instance, uint_t flags)
4714 {
4715 	sysevent_t		*ev;
4716 	sysevent_id_t		eid;
4717 	sysevent_value_t	se_val;
4718 	sysevent_attr_list_t	*ev_attr_list = NULL;
4719 	int			se_err;
4720 	int			no_transport = 0;
4721 
4722 	if (!i_ddi_io_initialized())
4723 		return (DDI_SUCCESS);
4724 
4725 	/* Invalidate the devinfo snapshot cache */
4726 	i_ddi_di_cache_invalidate();
4727 
4728 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_REMOVE, EP_DDI, SE_SLEEP);
4729 
4730 	se_val.value_type = SE_DATA_TYPE_STRING;
4731 	se_val.value.sv_string = pathname;
4732 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
4733 	    &se_val, SE_SLEEP) != 0) {
4734 		goto fail;
4735 	}
4736 
4737 	if (class_name) {
4738 		/* add the device class, driver name and instance attributes */
4739 
4740 		se_val.value_type = SE_DATA_TYPE_STRING;
4741 		se_val.value.sv_string = class_name;
4742 		if (sysevent_add_attr(&ev_attr_list,
4743 		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
4744 			sysevent_free_attr(ev_attr_list);
4745 			goto fail;
4746 		}
4747 
4748 		se_val.value_type = SE_DATA_TYPE_STRING;
4749 		se_val.value.sv_string = driver_name;
4750 		if (sysevent_add_attr(&ev_attr_list,
4751 		    DEVFS_DRIVER_NAME, &se_val, SE_SLEEP) != 0) {
4752 			sysevent_free_attr(ev_attr_list);
4753 			goto fail;
4754 		}
4755 
4756 		se_val.value_type = SE_DATA_TYPE_INT32;
4757 		se_val.value.sv_int32 = instance;
4758 		if (sysevent_add_attr(&ev_attr_list,
4759 		    DEVFS_INSTANCE, &se_val, SE_SLEEP) != 0) {
4760 			sysevent_free_attr(ev_attr_list);
4761 			goto fail;
4762 		}
4763 	}
4764 
4765 	/*
4766 	 * must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
4767 	 * in which case the branch event will be logged by the caller
4768 	 * after the entire branch has been unconfigured.
4769 	 */
4770 	if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
4771 		/*
4772 		 * Instead of logging a separate branch event just add
4773 		 * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
4774 		 * generate a EC_DEV_BRANCH event.
4775 		 */
4776 		se_val.value_type = SE_DATA_TYPE_INT32;
4777 		se_val.value.sv_int32 = 1;
4778 		if (sysevent_add_attr(&ev_attr_list,
4779 		    DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
4780 			sysevent_free_attr(ev_attr_list);
4781 			goto fail;
4782 		}
4783 	}
4784 
4785 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
4786 		sysevent_free_attr(ev_attr_list);
4787 		goto fail;
4788 	}
4789 
4790 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
4791 		if (se_err == SE_NO_TRANSPORT)
4792 			no_transport = 1;
4793 		goto fail;
4794 	}
4795 
4796 	sysevent_free(ev);
4797 	return (DDI_SUCCESS);
4798 
4799 fail:
4800 	sysevent_free(ev);
4801 	cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_REMOVE event for %s%s",
4802 	    pathname, (no_transport) ? " (syseventd not responding)" : "");
4803 	return (DDI_SUCCESS);
4804 }
4805 
4806 static void
4807 i_ddi_log_devfs_device_remove(dev_info_t *dip)
4808 {
4809 	char	*path;
4810 
4811 	ASSERT(dip && ddi_get_parent(dip) &&
4812 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
4813 	ASSERT(DEVI_IS_DEVICE_REMOVED(dip));
4814 
4815 	ASSERT(i_ddi_node_state(dip) >= DS_INITIALIZED);
4816 	if (i_ddi_node_state(dip) < DS_INITIALIZED)
4817 		return;
4818 
4819 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4820 	(void) i_log_devfs_remove_devinfo(ddi_pathname(dip, path),
4821 	    i_ddi_devi_class(dip), (char *)ddi_driver_name(dip),
4822 	    ddi_get_instance(dip), 0);
4823 	kmem_free(path, MAXPATHLEN);
4824 }
4825 
4826 static void
4827 i_ddi_log_devfs_device_insert(dev_info_t *dip)
4828 {
4829 	ASSERT(dip && ddi_get_parent(dip) &&
4830 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
4831 	ASSERT(!DEVI_IS_DEVICE_REMOVED(dip));
4832 
4833 	(void) i_log_devfs_add_devinfo(dip, 0);
4834 }
4835 
4836 
4837 /*
4838  * log an event that a dev_info branch has been configured or unconfigured.
4839  */
4840 static int
4841 i_log_devfs_branch(char *node_path, char *subclass)
4842 {
4843 	int se_err;
4844 	sysevent_t *ev;
4845 	sysevent_id_t eid;
4846 	sysevent_value_t se_val;
4847 	sysevent_attr_list_t *ev_attr_list = NULL;
4848 	int no_transport = 0;
4849 
4850 	/* do not generate the event during boot */
4851 	if (!i_ddi_io_initialized())
4852 		return (DDI_SUCCESS);
4853 
4854 	/* Invalidate the devinfo snapshot cache */
4855 	i_ddi_di_cache_invalidate();
4856 
4857 	ev = sysevent_alloc(EC_DEVFS, subclass, EP_DDI, SE_SLEEP);
4858 
4859 	se_val.value_type = SE_DATA_TYPE_STRING;
4860 	se_val.value.sv_string = node_path;
4861 
4862 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
4863 	    &se_val, SE_SLEEP) != 0) {
4864 		goto fail;
4865 	}
4866 
4867 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
4868 		sysevent_free_attr(ev_attr_list);
4869 		goto fail;
4870 	}
4871 
4872 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
4873 		if (se_err == SE_NO_TRANSPORT)
4874 			no_transport = 1;
4875 		goto fail;
4876 	}
4877 
4878 	sysevent_free(ev);
4879 	return (DDI_SUCCESS);
4880 
4881 fail:
4882 	cmn_err(CE_WARN, "failed to log %s branch event for %s%s",
4883 	    subclass, node_path,
4884 	    (no_transport) ? " (syseventd not responding)" : "");
4885 
4886 	sysevent_free(ev);
4887 	return (DDI_FAILURE);
4888 }
4889 
4890 /*
4891  * log an event that a dev_info tree branch has been configured.
4892  */
4893 static int
4894 i_log_devfs_branch_add(dev_info_t *dip)
4895 {
4896 	char *node_path;
4897 	int rv;
4898 
4899 	node_path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4900 	(void) ddi_pathname(dip, node_path);
4901 	rv = i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_ADD);
4902 	kmem_free(node_path, MAXPATHLEN);
4903 
4904 	return (rv);
4905 }
4906 
4907 /*
4908  * log an event that a dev_info tree branch has been unconfigured.
4909  */
4910 static int
4911 i_log_devfs_branch_remove(char *node_path)
4912 {
4913 	return (i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_REMOVE));
4914 }
4915 
4916 /*
4917  * enqueue the dip's deviname on the branch event queue.
4918  */
4919 static struct brevq_node *
4920 brevq_enqueue(struct brevq_node **brevqp, dev_info_t *dip,
4921     struct brevq_node *child)
4922 {
4923 	struct brevq_node *brn;
4924 	char *deviname;
4925 
4926 	deviname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
4927 	(void) ddi_deviname(dip, deviname);
4928 
4929 	brn = kmem_zalloc(sizeof (*brn), KM_SLEEP);
4930 	brn->brn_deviname = i_ddi_strdup(deviname, KM_SLEEP);
4931 	kmem_free(deviname, MAXNAMELEN);
4932 	brn->brn_child = child;
4933 	brn->brn_sibling = *brevqp;
4934 	*brevqp = brn;
4935 
4936 	return (brn);
4937 }
4938 
4939 /*
4940  * free the memory allocated for the elements on the branch event queue.
4941  */
4942 static void
4943 free_brevq(struct brevq_node *brevq)
4944 {
4945 	struct brevq_node *brn, *next_brn;
4946 
4947 	for (brn = brevq; brn != NULL; brn = next_brn) {
4948 		next_brn = brn->brn_sibling;
4949 		ASSERT(brn->brn_child == NULL);
4950 		kmem_free(brn->brn_deviname, strlen(brn->brn_deviname) + 1);
4951 		kmem_free(brn, sizeof (*brn));
4952 	}
4953 }
4954 
4955 /*
4956  * log the events queued up on the branch event queue and free the
4957  * associated memory.
4958  *
4959  * node_path must have been allocated with at least MAXPATHLEN bytes.
4960  */
4961 static void
4962 log_and_free_brevq(char *node_path, struct brevq_node *brevq)
4963 {
4964 	struct brevq_node *brn;
4965 	char *p;
4966 
4967 	p = node_path + strlen(node_path);
4968 	for (brn = brevq; brn != NULL; brn = brn->brn_sibling) {
4969 		(void) strcpy(p, brn->brn_deviname);
4970 		(void) i_log_devfs_branch_remove(node_path);
4971 	}
4972 	*p = '\0';
4973 
4974 	free_brevq(brevq);
4975 }
4976 
4977 /*
4978  * log the events queued up on the branch event queue and free the
4979  * associated memory. Same as the previous function but operates on dip.
4980  */
4981 static void
4982 log_and_free_brevq_dip(dev_info_t *dip, struct brevq_node *brevq)
4983 {
4984 	char *path;
4985 
4986 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4987 	(void) ddi_pathname(dip, path);
4988 	log_and_free_brevq(path, brevq);
4989 	kmem_free(path, MAXPATHLEN);
4990 }
4991 
4992 /*
4993  * log the outstanding branch remove events for the grand children of the dip
4994  * and free the associated memory.
4995  */
4996 static void
4997 log_and_free_br_events_on_grand_children(dev_info_t *dip,
4998     struct brevq_node *brevq)
4999 {
5000 	struct brevq_node *brn;
5001 	char *path;
5002 	char *p;
5003 
5004 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
5005 	(void) ddi_pathname(dip, path);
5006 	p = path + strlen(path);
5007 	for (brn = brevq; brn != NULL; brn = brn->brn_sibling) {
5008 		if (brn->brn_child) {
5009 			(void) strcpy(p, brn->brn_deviname);
5010 			/* now path contains the node path to the dip's child */
5011 			log_and_free_brevq(path, brn->brn_child);
5012 			brn->brn_child = NULL;
5013 		}
5014 	}
5015 	kmem_free(path, MAXPATHLEN);
5016 }
5017 
5018 /*
5019  * log and cleanup branch remove events for the grand children of the dip.
5020  */
5021 static void
5022 cleanup_br_events_on_grand_children(dev_info_t *dip, struct brevq_node **brevqp)
5023 {
5024 	dev_info_t *child;
5025 	struct brevq_node *brevq, *brn, *prev_brn, *next_brn;
5026 	char *path;
5027 	int circ;
5028 
5029 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
5030 	prev_brn = NULL;
5031 	brevq = *brevqp;
5032 
5033 	ndi_devi_enter(dip, &circ);
5034 	for (brn = brevq; brn != NULL; brn = next_brn) {
5035 		next_brn = brn->brn_sibling;
5036 		for (child = ddi_get_child(dip); child != NULL;
5037 		    child = ddi_get_next_sibling(child)) {
5038 			if (i_ddi_node_state(child) >= DS_INITIALIZED) {
5039 				(void) ddi_deviname(child, path);
5040 				if (strcmp(path, brn->brn_deviname) == 0)
5041 					break;
5042 			}
5043 		}
5044 
5045 		if (child != NULL && !(DEVI_EVREMOVE(child))) {
5046 			/*
5047 			 * Event state is not REMOVE. So branch remove event
5048 			 * is not going be generated on brn->brn_child.
5049 			 * If any branch remove events were queued up on
5050 			 * brn->brn_child log them and remove the brn
5051 			 * from the queue.
5052 			 */
5053 			if (brn->brn_child) {
5054 				(void) ddi_pathname(dip, path);
5055 				(void) strcat(path, brn->brn_deviname);
5056 				log_and_free_brevq(path, brn->brn_child);
5057 			}
5058 
5059 			if (prev_brn)
5060 				prev_brn->brn_sibling = next_brn;
5061 			else
5062 				*brevqp = next_brn;
5063 
5064 			kmem_free(brn->brn_deviname,
5065 			    strlen(brn->brn_deviname) + 1);
5066 			kmem_free(brn, sizeof (*brn));
5067 		} else {
5068 			/*
5069 			 * Free up the outstanding branch remove events
5070 			 * queued on brn->brn_child since brn->brn_child
5071 			 * itself is eligible for branch remove event.
5072 			 */
5073 			if (brn->brn_child) {
5074 				free_brevq(brn->brn_child);
5075 				brn->brn_child = NULL;
5076 			}
5077 			prev_brn = brn;
5078 		}
5079 	}
5080 
5081 	ndi_devi_exit(dip, circ);
5082 	kmem_free(path, MAXPATHLEN);
5083 }
5084 
5085 static int
5086 need_remove_event(dev_info_t *dip, int flags)
5087 {
5088 	if ((flags & (NDI_NO_EVENT | NDI_AUTODETACH)) == 0 &&
5089 	    (flags & (NDI_DEVI_OFFLINE | NDI_UNCONFIG | NDI_DEVI_REMOVE)) &&
5090 	    !(DEVI_EVREMOVE(dip)))
5091 		return (1);
5092 	else
5093 		return (0);
5094 }
5095 
5096 /*
5097  * Unconfigure children/descendants of the dip.
5098  *
5099  * If the operation involves a branch event NDI_BRANCH_EVENT_OP is set
5100  * through out the unconfiguration. On successful return *brevqp is set to
5101  * a queue of dip's child devinames for which branch remove events need
5102  * to be generated.
5103  */
5104 static int
5105 devi_unconfig_branch(dev_info_t *dip, dev_info_t **dipp, int flags,
5106     struct brevq_node **brevqp)
5107 {
5108 	int rval;
5109 
5110 	*brevqp = NULL;
5111 
5112 	if ((!(flags & NDI_BRANCH_EVENT_OP)) && need_remove_event(dip, flags))
5113 		flags |= NDI_BRANCH_EVENT_OP;
5114 
5115 	if (flags & NDI_BRANCH_EVENT_OP) {
5116 		rval = devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE,
5117 		    brevqp);
5118 
5119 		if (rval != NDI_SUCCESS && (*brevqp)) {
5120 			log_and_free_brevq_dip(dip, *brevqp);
5121 			*brevqp = NULL;
5122 		}
5123 	} else
5124 		rval = devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE,
5125 		    NULL);
5126 
5127 	return (rval);
5128 }
5129 
5130 /*
5131  * If the dip is already bound to a driver transition to DS_INITIALIZED
5132  * in order to generate an event in the case where the node was left in
5133  * DS_BOUND state since boot (never got attached) and the node is now
5134  * being offlined.
5135  */
5136 static void
5137 init_bound_node_ev(dev_info_t *pdip, dev_info_t *dip, int flags)
5138 {
5139 	if (need_remove_event(dip, flags) &&
5140 	    i_ddi_node_state(dip) == DS_BOUND &&
5141 	    i_ddi_devi_attached(pdip) && !DEVI_IS_DEVICE_OFFLINE(dip))
5142 		(void) ddi_initchild(pdip, dip);
5143 }
5144 
5145 /*
5146  * attach a node/branch with parent already held busy
5147  */
5148 static int
5149 devi_attach_node(dev_info_t *dip, uint_t flags)
5150 {
5151 	dev_info_t *pdip = ddi_get_parent(dip);
5152 
5153 	ASSERT(pdip && DEVI_BUSY_OWNED(pdip));
5154 
5155 	mutex_enter(&(DEVI(dip)->devi_lock));
5156 	if (flags & NDI_DEVI_ONLINE) {
5157 		if (!i_ddi_devi_attached(dip))
5158 			DEVI_SET_REPORT(dip);
5159 		DEVI_SET_DEVICE_ONLINE(dip);
5160 	}
5161 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
5162 		mutex_exit(&(DEVI(dip)->devi_lock));
5163 		return (NDI_FAILURE);
5164 	}
5165 	mutex_exit(&(DEVI(dip)->devi_lock));
5166 
5167 	if (i_ddi_attachchild(dip) != DDI_SUCCESS) {
5168 		mutex_enter(&(DEVI(dip)->devi_lock));
5169 		DEVI_SET_EVUNINIT(dip);
5170 		mutex_exit(&(DEVI(dip)->devi_lock));
5171 
5172 		if (ndi_dev_is_persistent_node(dip))
5173 			(void) ddi_uninitchild(dip);
5174 		else {
5175 			/*
5176 			 * Delete .conf nodes and nodes that are not
5177 			 * well formed.
5178 			 */
5179 			(void) ddi_remove_child(dip, 0);
5180 		}
5181 		return (NDI_FAILURE);
5182 	}
5183 
5184 	i_ndi_devi_report_status_change(dip, NULL);
5185 
5186 	/*
5187 	 * log an event, but not during devfs lookups in which case
5188 	 * NDI_NO_EVENT is set.
5189 	 */
5190 	if ((flags & NDI_NO_EVENT) == 0 && !(DEVI_EVADD(dip))) {
5191 		(void) i_log_devfs_add_devinfo(dip, flags);
5192 
5193 		mutex_enter(&(DEVI(dip)->devi_lock));
5194 		DEVI_SET_EVADD(dip);
5195 		mutex_exit(&(DEVI(dip)->devi_lock));
5196 	} else if (!(flags & NDI_NO_EVENT_STATE_CHNG)) {
5197 		mutex_enter(&(DEVI(dip)->devi_lock));
5198 		DEVI_SET_EVADD(dip);
5199 		mutex_exit(&(DEVI(dip)->devi_lock));
5200 	}
5201 
5202 	return (NDI_SUCCESS);
5203 }
5204 
5205 /* internal function to config immediate children */
5206 static int
5207 config_immediate_children(dev_info_t *pdip, uint_t flags, major_t major)
5208 {
5209 	dev_info_t	*child, *next;
5210 	int		circ;
5211 
5212 	ASSERT(i_ddi_devi_attached(pdip));
5213 
5214 	if (!NEXUS_DRV(ddi_get_driver(pdip)))
5215 		return (NDI_SUCCESS);
5216 
5217 	NDI_CONFIG_DEBUG((CE_CONT,
5218 	    "config_immediate_children: %s%d (%p), flags=%x\n",
5219 	    ddi_driver_name(pdip), ddi_get_instance(pdip),
5220 	    (void *)pdip, flags));
5221 
5222 	ndi_devi_enter(pdip, &circ);
5223 
5224 	if (flags & NDI_CONFIG_REPROBE) {
5225 		mutex_enter(&DEVI(pdip)->devi_lock);
5226 		DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
5227 		mutex_exit(&DEVI(pdip)->devi_lock);
5228 	}
5229 	(void) i_ndi_make_spec_children(pdip, flags);
5230 	i_ndi_init_hw_children(pdip, flags);
5231 
5232 	child = ddi_get_child(pdip);
5233 	while (child) {
5234 		/* NOTE: devi_attach_node() may remove the dip */
5235 		next = ddi_get_next_sibling(child);
5236 
5237 		/*
5238 		 * Configure all nexus nodes or leaf nodes with
5239 		 * matching driver major
5240 		 */
5241 		if ((major == DDI_MAJOR_T_NONE) ||
5242 		    (major == ddi_driver_major(child)) ||
5243 		    ((flags & NDI_CONFIG) && (is_leaf_node(child) == 0)))
5244 			(void) devi_attach_node(child, flags);
5245 		child = next;
5246 	}
5247 
5248 	ndi_devi_exit(pdip, circ);
5249 
5250 	return (NDI_SUCCESS);
5251 }
5252 
5253 /* internal function to config grand children */
5254 static int
5255 config_grand_children(dev_info_t *pdip, uint_t flags, major_t major)
5256 {
5257 	struct mt_config_handle *hdl;
5258 
5259 	/* multi-threaded configuration of child nexus */
5260 	hdl = mt_config_init(pdip, NULL, flags, major, MT_CONFIG_OP, NULL);
5261 	mt_config_children(hdl);
5262 
5263 	return (mt_config_fini(hdl));	/* wait for threads to exit */
5264 }
5265 
5266 /*
5267  * Common function for device tree configuration,
5268  * either BUS_CONFIG_ALL or BUS_CONFIG_DRIVER.
5269  * The NDI_CONFIG flag causes recursive configuration of
5270  * grandchildren, devfs usage should not recurse.
5271  */
5272 static int
5273 devi_config_common(dev_info_t *dip, int flags, major_t major)
5274 {
5275 	int error;
5276 	int (*f)();
5277 
5278 	if (!i_ddi_devi_attached(dip))
5279 		return (NDI_FAILURE);
5280 
5281 	if (pm_pre_config(dip, NULL) != DDI_SUCCESS)
5282 		return (NDI_FAILURE);
5283 
5284 	if ((DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
5285 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
5286 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
5287 		error = config_immediate_children(dip, flags, major);
5288 	} else {
5289 		/* call bus_config entry point */
5290 		ddi_bus_config_op_t bus_op = (major == DDI_MAJOR_T_NONE) ?
5291 		    BUS_CONFIG_ALL : BUS_CONFIG_DRIVER;
5292 		error = (*f)(dip,
5293 		    flags, bus_op, (void *)(uintptr_t)major, NULL, 0);
5294 	}
5295 
5296 	if (error) {
5297 		pm_post_config(dip, NULL);
5298 		return (error);
5299 	}
5300 
5301 	/*
5302 	 * Some callers, notably SCSI, need to mark the devfs cache
5303 	 * to be rebuilt together with the config operation.
5304 	 */
5305 	if (flags & NDI_DEVFS_CLEAN)
5306 		(void) devfs_clean(dip, NULL, 0);
5307 
5308 	if (flags & NDI_CONFIG)
5309 		(void) config_grand_children(dip, flags, major);
5310 
5311 	pm_post_config(dip, NULL);
5312 
5313 	return (NDI_SUCCESS);
5314 }
5315 
5316 /*
5317  * Framework entry point for BUS_CONFIG_ALL
5318  */
5319 int
5320 ndi_devi_config(dev_info_t *dip, int flags)
5321 {
5322 	NDI_CONFIG_DEBUG((CE_CONT,
5323 	    "ndi_devi_config: par = %s%d (%p), flags = 0x%x\n",
5324 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
5325 
5326 	return (devi_config_common(dip, flags, DDI_MAJOR_T_NONE));
5327 }
5328 
5329 /*
5330  * Framework entry point for BUS_CONFIG_DRIVER, bound to major
5331  */
5332 int
5333 ndi_devi_config_driver(dev_info_t *dip, int flags, major_t major)
5334 {
5335 	/* don't abuse this function */
5336 	ASSERT(major != DDI_MAJOR_T_NONE);
5337 
5338 	NDI_CONFIG_DEBUG((CE_CONT,
5339 	    "ndi_devi_config_driver: par = %s%d (%p), flags = 0x%x\n",
5340 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
5341 
5342 	return (devi_config_common(dip, flags, major));
5343 }
5344 
5345 /*
5346  * Called by nexus drivers to configure its children.
5347  */
5348 static int
5349 devi_config_one(dev_info_t *pdip, char *devnm, dev_info_t **cdipp,
5350     uint_t flags, clock_t timeout)
5351 {
5352 	dev_info_t	*vdip = NULL;
5353 	char		*drivername = NULL;
5354 	int		find_by_addr = 0;
5355 	char		*name, *addr;
5356 	int		v_circ, p_circ;
5357 	clock_t		end_time;	/* 60 sec */
5358 	int		probed;
5359 	dev_info_t	*cdip;
5360 	mdi_pathinfo_t	*cpip;
5361 
5362 	*cdipp = NULL;
5363 
5364 	if (!NEXUS_DRV(ddi_get_driver(pdip)))
5365 		return (NDI_FAILURE);
5366 
5367 	/* split name into "name@addr" parts */
5368 	i_ddi_parse_name(devnm, &name, &addr, NULL);
5369 
5370 	/*
5371 	 * If the nexus is a pHCI and we are not processing a pHCI from
5372 	 * mdi bus_config code then we need to know the vHCI.
5373 	 */
5374 	if (MDI_PHCI(pdip))
5375 		vdip = mdi_devi_get_vdip(pdip);
5376 
5377 	/*
5378 	 * We may have a genericname on a system that creates drivername
5379 	 * nodes (from .conf files).  Find the drivername by nodeid. If we
5380 	 * can't find a node with devnm as the node name then we search by
5381 	 * drivername.	This allows an implementation to supply a genericly
5382 	 * named boot path (disk) and locate drivename nodes (sd).  The
5383 	 * NDI_PROMNAME flag does not apply to /devices/pseudo paths.
5384 	 */
5385 	if ((flags & NDI_PROMNAME) && (pdip != pseudo_dip)) {
5386 		drivername = child_path_to_driver(pdip, name, addr);
5387 		find_by_addr = 1;
5388 	}
5389 
5390 	/*
5391 	 * Determine end_time: This routine should *not* be called with a
5392 	 * constant non-zero timeout argument, the caller should be adjusting
5393 	 * the timeout argument relative to when it *started* its asynchronous
5394 	 * enumeration.
5395 	 */
5396 	if (timeout > 0)
5397 		end_time = ddi_get_lbolt() + timeout;
5398 
5399 	for (;;) {
5400 		/*
5401 		 * For pHCI, enter (vHCI, pHCI) and search for pathinfo/client
5402 		 * child - break out of for(;;) loop if child found.
5403 		 * NOTE: Lock order for ndi_devi_enter is (vHCI, pHCI).
5404 		 */
5405 		if (vdip) {
5406 			/* use mdi_devi_enter ordering */
5407 			ndi_devi_enter(vdip, &v_circ);
5408 			ndi_devi_enter(pdip, &p_circ);
5409 			cpip = mdi_pi_find(pdip, NULL, addr);
5410 			cdip = mdi_pi_get_client(cpip);
5411 			if (cdip)
5412 				break;
5413 		} else
5414 			ndi_devi_enter(pdip, &p_circ);
5415 
5416 		/*
5417 		 * When not a  vHCI or not all pHCI devices are required to
5418 		 * enumerated under the vHCI (NDI_MDI_FALLBACK) search for
5419 		 * devinfo child.
5420 		 */
5421 		if ((vdip == NULL) || (flags & NDI_MDI_FALLBACK)) {
5422 			/* determine if .conf nodes already built */
5423 			probed = (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
5424 
5425 			/*
5426 			 * Search for child by name, if not found then search
5427 			 * for a node bound to the drivername driver with the
5428 			 * specified "@addr". Break out of for(;;) loop if
5429 			 * child found.  To support path-oriented aliases
5430 			 * binding on boot-device, we do a search_by_addr too.
5431 			 */
5432 again:			(void) i_ndi_make_spec_children(pdip, flags);
5433 			cdip = find_child_by_name(pdip, name, addr);
5434 			if ((cdip == NULL) && drivername)
5435 				cdip = find_child_by_driver(pdip,
5436 				    drivername, addr);
5437 			if ((cdip == NULL) && find_by_addr)
5438 				cdip = find_child_by_addr(pdip, addr);
5439 			if (cdip)
5440 				break;
5441 
5442 			/*
5443 			 * determine if we should reenumerate .conf nodes
5444 			 * and look for child again.
5445 			 */
5446 			if (probed &&
5447 			    i_ddi_io_initialized() &&
5448 			    (flags & NDI_CONFIG_REPROBE) &&
5449 			    ((timeout <= 0) || (ddi_get_lbolt() >= end_time))) {
5450 				probed = 0;
5451 				mutex_enter(&DEVI(pdip)->devi_lock);
5452 				DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
5453 				mutex_exit(&DEVI(pdip)->devi_lock);
5454 				goto again;
5455 			}
5456 		}
5457 
5458 		/* break out of for(;;) if time expired */
5459 		if ((timeout <= 0) || (ddi_get_lbolt() >= end_time))
5460 			break;
5461 
5462 		/*
5463 		 * Child not found, exit and wait for asynchronous enumeration
5464 		 * to add child (or timeout). The addition of a new child (vhci
5465 		 * or phci) requires the asynchronous enumeration thread to
5466 		 * ndi_devi_enter/ndi_devi_exit. This exit will signal devi_cv
5467 		 * and cause us to return from ndi_devi_exit_and_wait, after
5468 		 * which we loop and search for the requested child again.
5469 		 */
5470 		NDI_DEBUG(flags, (CE_CONT,
5471 		    "%s%d: waiting for child %s@%s, timeout %ld",
5472 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
5473 		    name, addr, timeout));
5474 		if (vdip) {
5475 			/*
5476 			 * Mark vHCI for pHCI ndi_devi_exit broadcast.
5477 			 */
5478 			mutex_enter(&DEVI(vdip)->devi_lock);
5479 			DEVI(vdip)->devi_flags |=
5480 			    DEVI_PHCI_SIGNALS_VHCI;
5481 			mutex_exit(&DEVI(vdip)->devi_lock);
5482 			ndi_devi_exit(pdip, p_circ);
5483 
5484 			/*
5485 			 * NB: There is a small race window from above
5486 			 * ndi_devi_exit() of pdip to cv_wait() in
5487 			 * ndi_devi_exit_and_wait() which can result in
5488 			 * not immediately finding a new pHCI child
5489 			 * of a pHCI that uses NDI_MDI_FAILBACK.
5490 			 */
5491 			ndi_devi_exit_and_wait(vdip, v_circ, end_time);
5492 		} else {
5493 			ndi_devi_exit_and_wait(pdip, p_circ, end_time);
5494 		}
5495 	}
5496 
5497 	/* done with paddr, fixup i_ddi_parse_name '@'->'\0' change */
5498 	if (addr && *addr != '\0')
5499 		*(addr - 1) = '@';
5500 
5501 	/* attach and hold the child, returning pointer to child */
5502 	if (cdip && (devi_attach_node(cdip, flags) == NDI_SUCCESS)) {
5503 		ndi_hold_devi(cdip);
5504 		*cdipp = cdip;
5505 	}
5506 
5507 	ndi_devi_exit(pdip, p_circ);
5508 	if (vdip)
5509 		ndi_devi_exit(vdip, v_circ);
5510 	return (*cdipp ? NDI_SUCCESS : NDI_FAILURE);
5511 }
5512 
5513 /*
5514  * Enumerate and attach a child specified by name 'devnm'.
5515  * Called by devfs lookup and DR to perform a BUS_CONFIG_ONE.
5516  * Note: devfs does not make use of NDI_CONFIG to configure
5517  * an entire branch.
5518  */
5519 int
5520 ndi_devi_config_one(dev_info_t *dip, char *devnm, dev_info_t **dipp, int flags)
5521 {
5522 	int error;
5523 	int (*f)();
5524 	int branch_event = 0;
5525 
5526 	ASSERT(dipp);
5527 	ASSERT(i_ddi_devi_attached(dip));
5528 
5529 	NDI_CONFIG_DEBUG((CE_CONT,
5530 	    "ndi_devi_config_one: par = %s%d (%p), child = %s\n",
5531 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, devnm));
5532 
5533 	if (pm_pre_config(dip, devnm) != DDI_SUCCESS)
5534 		return (NDI_FAILURE);
5535 
5536 	if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
5537 	    (flags & NDI_CONFIG)) {
5538 		flags |= NDI_BRANCH_EVENT_OP;
5539 		branch_event = 1;
5540 	}
5541 
5542 	if ((DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
5543 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
5544 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
5545 		error = devi_config_one(dip, devnm, dipp, flags, 0);
5546 	} else {
5547 		/* call bus_config entry point */
5548 		error = (*f)(dip, flags, BUS_CONFIG_ONE, (void *)devnm, dipp);
5549 	}
5550 
5551 	if (error || (flags & NDI_CONFIG) == 0) {
5552 		pm_post_config(dip, devnm);
5553 		return (error);
5554 	}
5555 
5556 	/*
5557 	 * DR usage (i.e. call with NDI_CONFIG) recursively configures
5558 	 * grandchildren, performing a BUS_CONFIG_ALL from the node attached
5559 	 * by the BUS_CONFIG_ONE.
5560 	 */
5561 	ASSERT(*dipp);
5562 	error = devi_config_common(*dipp, flags, DDI_MAJOR_T_NONE);
5563 
5564 	pm_post_config(dip, devnm);
5565 
5566 	if (branch_event)
5567 		(void) i_log_devfs_branch_add(*dipp);
5568 
5569 	return (error);
5570 }
5571 
5572 
5573 /*
5574  * Enumerate and attach a child specified by name 'devnm'.
5575  * Called during configure the OBP options. This configures
5576  * only one node.
5577  */
5578 static int
5579 ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
5580     dev_info_t **childp, int flags)
5581 {
5582 	int error;
5583 	int (*f)();
5584 
5585 	ASSERT(childp);
5586 	ASSERT(i_ddi_devi_attached(parent));
5587 
5588 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_config_obp_args: "
5589 	    "par = %s%d (%p), child = %s\n", ddi_driver_name(parent),
5590 	    ddi_get_instance(parent), (void *)parent, devnm));
5591 
5592 	if ((DEVI(parent)->devi_ops->devo_bus_ops == NULL) ||
5593 	    (DEVI(parent)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
5594 	    (f = DEVI(parent)->devi_ops->devo_bus_ops->bus_config) == NULL) {
5595 		error = NDI_FAILURE;
5596 	} else {
5597 		/* call bus_config entry point */
5598 		error = (*f)(parent, flags,
5599 		    BUS_CONFIG_OBP_ARGS, (void *)devnm, childp);
5600 	}
5601 	return (error);
5602 }
5603 
5604 /*
5605  * Pay attention, the following is a bit tricky:
5606  * There are three possible cases when constraints are applied
5607  *
5608  *	- A constraint is applied and the offline is disallowed.
5609  *	  Simply return failure and block the offline
5610  *
5611  *	- A constraint is applied and the offline is allowed.
5612  *	  Mark the dip as having passed the constraint and allow
5613  *	  offline to proceed.
5614  *
5615  *	- A constraint is not applied. Allow the offline to proceed for now.
5616  *
5617  * In the latter two cases we allow the offline to proceed. If the
5618  * offline succeeds (no users) everything is fine. It is ok for an unused
5619  * device to be offlined even if no constraints were imposed on the offline.
5620  * If the offline fails because there are users, we look at the constraint
5621  * flag on the dip. If the constraint flag is set (implying that it passed
5622  * a constraint) we allow the dip to be retired. If not, we don't allow
5623  * the retire. This ensures that we don't allow unconstrained retire.
5624  */
5625 int
5626 e_ddi_offline_notify(dev_info_t *dip)
5627 {
5628 	int retval;
5629 	int constraint;
5630 	int failure;
5631 
5632 	RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): entered: dip=%p",
5633 	    (void *) dip));
5634 
5635 	constraint = 0;
5636 	failure = 0;
5637 
5638 	/*
5639 	 * Start with userland constraints first - applied via device contracts
5640 	 */
5641 	retval = contract_device_offline(dip, DDI_DEV_T_ANY, 0);
5642 	switch (retval) {
5643 	case CT_NACK:
5644 		RIO_DEBUG((CE_NOTE, "Received NACK for dip=%p", (void *)dip));
5645 		failure = 1;
5646 		goto out;
5647 	case CT_ACK:
5648 		constraint = 1;
5649 		RIO_DEBUG((CE_NOTE, "Received ACK for dip=%p", (void *)dip));
5650 		break;
5651 	case CT_NONE:
5652 		/* no contracts */
5653 		RIO_DEBUG((CE_NOTE, "No contracts on dip=%p", (void *)dip));
5654 		break;
5655 	default:
5656 		ASSERT(retval == CT_NONE);
5657 	}
5658 
5659 	/*
5660 	 * Next, use LDI to impose kernel constraints
5661 	 */
5662 	retval = ldi_invoke_notify(dip, DDI_DEV_T_ANY, 0, LDI_EV_OFFLINE, NULL);
5663 	switch (retval) {
5664 	case LDI_EV_FAILURE:
5665 		contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_FAILURE);
5666 		RIO_DEBUG((CE_NOTE, "LDI callback failed on dip=%p",
5667 		    (void *)dip));
5668 		failure = 1;
5669 		goto out;
5670 	case LDI_EV_SUCCESS:
5671 		constraint = 1;
5672 		RIO_DEBUG((CE_NOTE, "LDI callback success on dip=%p",
5673 		    (void *)dip));
5674 		break;
5675 	case LDI_EV_NONE:
5676 		/* no matching LDI callbacks */
5677 		RIO_DEBUG((CE_NOTE, "No LDI callbacks for dip=%p",
5678 		    (void *)dip));
5679 		break;
5680 	default:
5681 		ASSERT(retval == LDI_EV_NONE);
5682 	}
5683 
5684 out:
5685 	mutex_enter(&(DEVI(dip)->devi_lock));
5686 	if ((DEVI(dip)->devi_flags & DEVI_RETIRING) && failure) {
5687 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): setting "
5688 		    "BLOCKED flag. dip=%p", (void *)dip));
5689 		DEVI(dip)->devi_flags |= DEVI_R_BLOCKED;
5690 		if (DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT) {
5691 			RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): "
5692 			    "blocked. clearing RCM CONSTRAINT flag. dip=%p",
5693 			    (void *)dip));
5694 			DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
5695 		}
5696 	} else if ((DEVI(dip)->devi_flags & DEVI_RETIRING) && constraint) {
5697 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): setting "
5698 		    "CONSTRAINT flag. dip=%p", (void *)dip));
5699 		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
5700 	} else if ((DEVI(dip)->devi_flags & DEVI_RETIRING) &&
5701 	    DEVI(dip)->devi_ref == 0) {
5702 		/* also allow retire if device is not in use */
5703 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): device not in "
5704 		    "use. Setting CONSTRAINT flag. dip=%p", (void *)dip));
5705 		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
5706 	} else {
5707 		/*
5708 		 * Note: We cannot ASSERT here that DEVI_R_CONSTRAINT is
5709 		 * not set, since other sources (such as RCM) may have
5710 		 * set the flag.
5711 		 */
5712 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): not setting "
5713 		    "constraint flag. dip=%p", (void *)dip));
5714 	}
5715 	mutex_exit(&(DEVI(dip)->devi_lock));
5716 
5717 
5718 	RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): exit: dip=%p",
5719 	    (void *) dip));
5720 
5721 	return (failure ? DDI_FAILURE : DDI_SUCCESS);
5722 }
5723 
5724 void
5725 e_ddi_offline_finalize(dev_info_t *dip, int result)
5726 {
5727 	RIO_DEBUG((CE_NOTE, "e_ddi_offline_finalize(): entry: result=%s, "
5728 	    "dip=%p", result == DDI_SUCCESS ? "SUCCESS" : "FAILURE",
5729 	    (void *)dip));
5730 
5731 	contract_device_negend(dip, DDI_DEV_T_ANY, 0,  result == DDI_SUCCESS ?
5732 	    CT_EV_SUCCESS : CT_EV_FAILURE);
5733 
5734 	ldi_invoke_finalize(dip, DDI_DEV_T_ANY, 0,
5735 	    LDI_EV_OFFLINE, result == DDI_SUCCESS ?
5736 	    LDI_EV_SUCCESS : LDI_EV_FAILURE, NULL);
5737 
5738 	RIO_VERBOSE((CE_NOTE, "e_ddi_offline_finalize(): exit: dip=%p",
5739 	    (void *)dip));
5740 }
5741 
5742 void
5743 e_ddi_degrade_finalize(dev_info_t *dip)
5744 {
5745 	RIO_DEBUG((CE_NOTE, "e_ddi_degrade_finalize(): entry: "
5746 	    "result always = DDI_SUCCESS, dip=%p", (void *)dip));
5747 
5748 	contract_device_degrade(dip, DDI_DEV_T_ANY, 0);
5749 	contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_SUCCESS);
5750 
5751 	ldi_invoke_finalize(dip, DDI_DEV_T_ANY, 0, LDI_EV_DEGRADE,
5752 	    LDI_EV_SUCCESS, NULL);
5753 
5754 	RIO_VERBOSE((CE_NOTE, "e_ddi_degrade_finalize(): exit: dip=%p",
5755 	    (void *)dip));
5756 }
5757 
5758 void
5759 e_ddi_undegrade_finalize(dev_info_t *dip)
5760 {
5761 	RIO_DEBUG((CE_NOTE, "e_ddi_undegrade_finalize(): entry: "
5762 	    "result always = DDI_SUCCESS, dip=%p", (void *)dip));
5763 
5764 	contract_device_undegrade(dip, DDI_DEV_T_ANY, 0);
5765 	contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_SUCCESS);
5766 
5767 	RIO_VERBOSE((CE_NOTE, "e_ddi_undegrade_finalize(): exit: dip=%p",
5768 	    (void *)dip));
5769 }
5770 
5771 /*
5772  * detach a node with parent already held busy
5773  */
5774 static int
5775 devi_detach_node(dev_info_t *dip, uint_t flags)
5776 {
5777 	dev_info_t *pdip = ddi_get_parent(dip);
5778 	int ret = NDI_SUCCESS;
5779 	ddi_eventcookie_t cookie;
5780 	char *path = NULL;
5781 	char *class = NULL;
5782 	char *driver = NULL;
5783 	int instance = -1;
5784 	int post_event = 0;
5785 
5786 	ASSERT(pdip && DEVI_BUSY_OWNED(pdip));
5787 
5788 	/*
5789 	 * Invoke notify if offlining
5790 	 */
5791 	if (flags & NDI_DEVI_OFFLINE) {
5792 		RIO_DEBUG((CE_NOTE, "devi_detach_node: offlining dip=%p",
5793 		    (void *)dip));
5794 		if (e_ddi_offline_notify(dip) != DDI_SUCCESS) {
5795 			RIO_DEBUG((CE_NOTE, "devi_detach_node: offline NACKed"
5796 			    "dip=%p", (void *)dip));
5797 			return (NDI_FAILURE);
5798 		}
5799 	}
5800 
5801 	if (flags & NDI_POST_EVENT) {
5802 		if (i_ddi_devi_attached(pdip)) {
5803 			if (ddi_get_eventcookie(dip, DDI_DEVI_REMOVE_EVENT,
5804 			    &cookie) == NDI_SUCCESS)
5805 				(void) ndi_post_event(dip, dip, cookie, NULL);
5806 		}
5807 	}
5808 
5809 	if (i_ddi_detachchild(dip, flags) != DDI_SUCCESS) {
5810 		if (flags & NDI_DEVI_OFFLINE) {
5811 			RIO_DEBUG((CE_NOTE, "devi_detach_node: offline failed."
5812 			    " Calling e_ddi_offline_finalize with result=%d. "
5813 			    "dip=%p", DDI_FAILURE, (void *)dip));
5814 			e_ddi_offline_finalize(dip, DDI_FAILURE);
5815 		}
5816 		return (NDI_FAILURE);
5817 	}
5818 
5819 	if (flags & NDI_DEVI_OFFLINE) {
5820 		RIO_DEBUG((CE_NOTE, "devi_detach_node: offline succeeded."
5821 		    " Calling e_ddi_offline_finalize with result=%d, "
5822 		    "dip=%p", DDI_SUCCESS, (void *)dip));
5823 		e_ddi_offline_finalize(dip, DDI_SUCCESS);
5824 	}
5825 
5826 	if (flags & NDI_AUTODETACH)
5827 		return (NDI_SUCCESS);
5828 
5829 	/*
5830 	 * For DR, even bound nodes may need to have offline
5831 	 * flag set.
5832 	 */
5833 	if (flags & NDI_DEVI_OFFLINE) {
5834 		mutex_enter(&(DEVI(dip)->devi_lock));
5835 		DEVI_SET_DEVICE_OFFLINE(dip);
5836 		mutex_exit(&(DEVI(dip)->devi_lock));
5837 	}
5838 
5839 	if (i_ddi_node_state(dip) == DS_INITIALIZED) {
5840 		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
5841 		(void) ddi_pathname(dip, path);
5842 		if (flags & NDI_DEVI_OFFLINE)
5843 			i_ndi_devi_report_status_change(dip, path);
5844 
5845 		if (need_remove_event(dip, flags)) {
5846 			post_event = 1;
5847 			class = i_ddi_strdup(i_ddi_devi_class(dip), KM_SLEEP);
5848 			driver = i_ddi_strdup((char *)ddi_driver_name(dip),
5849 			    KM_SLEEP);
5850 			instance = ddi_get_instance(dip);
5851 
5852 			mutex_enter(&(DEVI(dip)->devi_lock));
5853 			DEVI_SET_EVREMOVE(dip);
5854 			mutex_exit(&(DEVI(dip)->devi_lock));
5855 		}
5856 	}
5857 
5858 	if (flags & (NDI_UNCONFIG | NDI_DEVI_REMOVE)) {
5859 		ret = ddi_uninitchild(dip);
5860 		if (ret == NDI_SUCCESS) {
5861 			/*
5862 			 * Remove uninitialized pseudo nodes because
5863 			 * system props are lost and the node cannot be
5864 			 * reattached.
5865 			 */
5866 			if (!ndi_dev_is_persistent_node(dip))
5867 				flags |= NDI_DEVI_REMOVE;
5868 
5869 			if (flags & NDI_DEVI_REMOVE) {
5870 				ret = ddi_remove_child(dip, 0);
5871 				if (post_event && ret == NDI_SUCCESS) {
5872 					(void) i_log_devfs_remove_devinfo(path,
5873 					    class, driver, instance, flags);
5874 				}
5875 			}
5876 
5877 		}
5878 	}
5879 
5880 	if (path)
5881 		kmem_free(path, MAXPATHLEN);
5882 	if (class)
5883 		kmem_free(class, strlen(class) + 1);
5884 	if (driver)
5885 		kmem_free(driver, strlen(driver) + 1);
5886 
5887 	return (ret);
5888 }
5889 
5890 /*
5891  * unconfigure immediate children of bus nexus device
5892  */
5893 static int
5894 unconfig_immediate_children(
5895 	dev_info_t *dip,
5896 	dev_info_t **dipp,
5897 	int flags,
5898 	major_t major)
5899 {
5900 	int rv = NDI_SUCCESS;
5901 	int circ, vcirc;
5902 	dev_info_t *child;
5903 	dev_info_t *vdip = NULL;
5904 	dev_info_t *next;
5905 
5906 	ASSERT(dipp == NULL || *dipp == NULL);
5907 
5908 	/*
5909 	 * Scan forward to see if we will be processing a pHCI child. If we
5910 	 * have a child that is a pHCI and vHCI and pHCI are not siblings then
5911 	 * enter vHCI before parent(pHCI) to prevent deadlock with mpxio
5912 	 * Client power management operations.
5913 	 */
5914 	ndi_devi_enter(dip, &circ);
5915 	for (child = ddi_get_child(dip); child;
5916 	    child = ddi_get_next_sibling(child)) {
5917 		/* skip same nodes we skip below */
5918 		if (((major != DDI_MAJOR_T_NONE) &&
5919 		    (major != ddi_driver_major(child))) ||
5920 		    ((flags & NDI_AUTODETACH) && !is_leaf_node(child)))
5921 			continue;
5922 
5923 		if (MDI_PHCI(child)) {
5924 			vdip = mdi_devi_get_vdip(child);
5925 			/*
5926 			 * If vHCI and vHCI is not a sibling of pHCI
5927 			 * then enter in (vHCI, parent(pHCI)) order.
5928 			 */
5929 			if (vdip && (ddi_get_parent(vdip) != dip)) {
5930 				ndi_devi_exit(dip, circ);
5931 
5932 				/* use mdi_devi_enter ordering */
5933 				ndi_devi_enter(vdip, &vcirc);
5934 				ndi_devi_enter(dip, &circ);
5935 				break;
5936 			} else
5937 				vdip = NULL;
5938 		}
5939 	}
5940 
5941 	child = ddi_get_child(dip);
5942 	while (child) {
5943 		next = ddi_get_next_sibling(child);
5944 
5945 		if ((major != DDI_MAJOR_T_NONE) &&
5946 		    (major != ddi_driver_major(child))) {
5947 			child = next;
5948 			continue;
5949 		}
5950 
5951 		/* skip nexus nodes during autodetach */
5952 		if ((flags & NDI_AUTODETACH) && !is_leaf_node(child)) {
5953 			child = next;
5954 			continue;
5955 		}
5956 
5957 		if (devi_detach_node(child, flags) != NDI_SUCCESS) {
5958 			if (dipp && *dipp == NULL) {
5959 				ndi_hold_devi(child);
5960 				*dipp = child;
5961 			}
5962 			rv = NDI_FAILURE;
5963 		}
5964 
5965 		/*
5966 		 * Continue upon failure--best effort algorithm
5967 		 */
5968 		child = next;
5969 	}
5970 
5971 	ndi_devi_exit(dip, circ);
5972 	if (vdip)
5973 		ndi_devi_exit(vdip, vcirc);
5974 
5975 	return (rv);
5976 }
5977 
5978 /*
5979  * unconfigure grand children of bus nexus device
5980  */
5981 static int
5982 unconfig_grand_children(
5983 	dev_info_t *dip,
5984 	dev_info_t **dipp,
5985 	int flags,
5986 	major_t major,
5987 	struct brevq_node **brevqp)
5988 {
5989 	struct mt_config_handle *hdl;
5990 
5991 	if (brevqp)
5992 		*brevqp = NULL;
5993 
5994 	/* multi-threaded configuration of child nexus */
5995 	hdl = mt_config_init(dip, dipp, flags, major, MT_UNCONFIG_OP, brevqp);
5996 	mt_config_children(hdl);
5997 
5998 	return (mt_config_fini(hdl));	/* wait for threads to exit */
5999 }
6000 
6001 /*
6002  * Unconfigure children/descendants of the dip.
6003  *
6004  * If brevqp is not NULL, on return *brevqp is set to a queue of dip's
6005  * child devinames for which branch remove events need to be generated.
6006  */
6007 static int
6008 devi_unconfig_common(
6009 	dev_info_t *dip,
6010 	dev_info_t **dipp,
6011 	int flags,
6012 	major_t major,
6013 	struct brevq_node **brevqp)
6014 {
6015 	int rv;
6016 	int pm_cookie;
6017 	int (*f)();
6018 	ddi_bus_config_op_t bus_op;
6019 
6020 	if (dipp)
6021 		*dipp = NULL;
6022 	if (brevqp)
6023 		*brevqp = NULL;
6024 
6025 	/*
6026 	 * Power up the dip if it is powered off.  If the flag bit
6027 	 * NDI_AUTODETACH is set and the dip is not at its full power,
6028 	 * skip the rest of the branch.
6029 	 */
6030 	if (pm_pre_unconfig(dip, flags, &pm_cookie, NULL) != DDI_SUCCESS)
6031 		return ((flags & NDI_AUTODETACH) ? NDI_SUCCESS :
6032 		    NDI_FAILURE);
6033 
6034 	/*
6035 	 * Some callers, notably SCSI, need to clear out the devfs
6036 	 * cache together with the unconfig to prevent stale entries.
6037 	 */
6038 	if (flags & NDI_DEVFS_CLEAN)
6039 		(void) devfs_clean(dip, NULL, 0);
6040 
6041 	rv = unconfig_grand_children(dip, dipp, flags, major, brevqp);
6042 
6043 	if ((rv != NDI_SUCCESS) && ((flags & NDI_AUTODETACH) == 0)) {
6044 		if (brevqp && *brevqp) {
6045 			log_and_free_br_events_on_grand_children(dip, *brevqp);
6046 			free_brevq(*brevqp);
6047 			*brevqp = NULL;
6048 		}
6049 		pm_post_unconfig(dip, pm_cookie, NULL);
6050 		return (rv);
6051 	}
6052 
6053 	if (dipp && *dipp) {
6054 		ndi_rele_devi(*dipp);
6055 		*dipp = NULL;
6056 	}
6057 
6058 	/*
6059 	 * It is possible to have a detached nexus with children
6060 	 * and grandchildren (for example: a branch consisting
6061 	 * entirely of bound nodes.) Since the nexus is detached
6062 	 * the bus_unconfig entry point cannot be used to remove
6063 	 * or unconfigure the descendants.
6064 	 */
6065 	if (!i_ddi_devi_attached(dip) ||
6066 	    (DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
6067 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
6068 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
6069 		rv = unconfig_immediate_children(dip, dipp, flags, major);
6070 	} else {
6071 		/*
6072 		 * call bus_unconfig entry point
6073 		 * It should reset nexus flags if unconfigure succeeds.
6074 		 */
6075 		bus_op = (major == DDI_MAJOR_T_NONE) ?
6076 		    BUS_UNCONFIG_ALL : BUS_UNCONFIG_DRIVER;
6077 		rv = (*f)(dip, flags, bus_op, (void *)(uintptr_t)major);
6078 	}
6079 
6080 	pm_post_unconfig(dip, pm_cookie, NULL);
6081 
6082 	if (brevqp && *brevqp)
6083 		cleanup_br_events_on_grand_children(dip, brevqp);
6084 
6085 	return (rv);
6086 }
6087 
6088 /*
6089  * called by devfs/framework to unconfigure children bound to major
6090  * If NDI_AUTODETACH is specified, this is invoked by either the
6091  * moduninstall daemon or the modunload -i 0 command.
6092  */
6093 int
6094 ndi_devi_unconfig_driver(dev_info_t *dip, int flags, major_t major)
6095 {
6096 	NDI_CONFIG_DEBUG((CE_CONT,
6097 	    "ndi_devi_unconfig_driver: par = %s%d (%p), flags = 0x%x\n",
6098 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
6099 
6100 	return (devi_unconfig_common(dip, NULL, flags, major, NULL));
6101 }
6102 
6103 int
6104 ndi_devi_unconfig(dev_info_t *dip, int flags)
6105 {
6106 	NDI_CONFIG_DEBUG((CE_CONT,
6107 	    "ndi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
6108 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
6109 
6110 	return (devi_unconfig_common(dip, NULL, flags, DDI_MAJOR_T_NONE, NULL));
6111 }
6112 
6113 int
6114 e_ddi_devi_unconfig(dev_info_t *dip, dev_info_t **dipp, int flags)
6115 {
6116 	NDI_CONFIG_DEBUG((CE_CONT,
6117 	    "e_ddi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
6118 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
6119 
6120 	return (devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE, NULL));
6121 }
6122 
6123 /*
6124  * Unconfigure child by name
6125  */
6126 static int
6127 devi_unconfig_one(dev_info_t *pdip, char *devnm, int flags)
6128 {
6129 	int		rv, circ;
6130 	dev_info_t	*child;
6131 	dev_info_t	*vdip = NULL;
6132 	int		v_circ;
6133 
6134 	ndi_devi_enter(pdip, &circ);
6135 	child = ndi_devi_findchild(pdip, devnm);
6136 
6137 	/*
6138 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
6139 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
6140 	 * management operations.
6141 	 */
6142 	if (child && MDI_PHCI(child)) {
6143 		vdip = mdi_devi_get_vdip(child);
6144 		if (vdip && (ddi_get_parent(vdip) != pdip)) {
6145 			ndi_devi_exit(pdip, circ);
6146 
6147 			/* use mdi_devi_enter ordering */
6148 			ndi_devi_enter(vdip, &v_circ);
6149 			ndi_devi_enter(pdip, &circ);
6150 			child = ndi_devi_findchild(pdip, devnm);
6151 		} else
6152 			vdip = NULL;
6153 	}
6154 
6155 	if (child) {
6156 		rv = devi_detach_node(child, flags);
6157 	} else {
6158 		NDI_CONFIG_DEBUG((CE_CONT,
6159 		    "devi_unconfig_one: %s not found\n", devnm));
6160 		rv = NDI_SUCCESS;
6161 	}
6162 
6163 	ndi_devi_exit(pdip, circ);
6164 	if (vdip)
6165 		ndi_devi_exit(vdip, v_circ);
6166 
6167 	return (rv);
6168 }
6169 
6170 int
6171 ndi_devi_unconfig_one(
6172 	dev_info_t *pdip,
6173 	char *devnm,
6174 	dev_info_t **dipp,
6175 	int flags)
6176 {
6177 	int		(*f)();
6178 	int		circ, rv;
6179 	int		pm_cookie;
6180 	dev_info_t	*child;
6181 	dev_info_t	*vdip = NULL;
6182 	int		v_circ;
6183 	struct brevq_node *brevq = NULL;
6184 
6185 	ASSERT(i_ddi_devi_attached(pdip));
6186 
6187 	NDI_CONFIG_DEBUG((CE_CONT,
6188 	    "ndi_devi_unconfig_one: par = %s%d (%p), child = %s\n",
6189 	    ddi_driver_name(pdip), ddi_get_instance(pdip),
6190 	    (void *)pdip, devnm));
6191 
6192 	if (pm_pre_unconfig(pdip, flags, &pm_cookie, devnm) != DDI_SUCCESS)
6193 		return (NDI_FAILURE);
6194 
6195 	if (dipp)
6196 		*dipp = NULL;
6197 
6198 	ndi_devi_enter(pdip, &circ);
6199 	child = ndi_devi_findchild(pdip, devnm);
6200 
6201 	/*
6202 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
6203 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
6204 	 * management operations.
6205 	 */
6206 	if (child && MDI_PHCI(child)) {
6207 		vdip = mdi_devi_get_vdip(child);
6208 		if (vdip && (ddi_get_parent(vdip) != pdip)) {
6209 			ndi_devi_exit(pdip, circ);
6210 
6211 			/* use mdi_devi_enter ordering */
6212 			ndi_devi_enter(vdip, &v_circ);
6213 			ndi_devi_enter(pdip, &circ);
6214 			child = ndi_devi_findchild(pdip, devnm);
6215 		} else
6216 			vdip = NULL;
6217 	}
6218 
6219 	if (child == NULL) {
6220 		NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_unconfig_one: %s"
6221 		    " not found\n", devnm));
6222 		rv = NDI_SUCCESS;
6223 		goto out;
6224 	}
6225 
6226 	/*
6227 	 * Unconfigure children/descendants of named child
6228 	 */
6229 	rv = devi_unconfig_branch(child, dipp, flags | NDI_UNCONFIG, &brevq);
6230 	if (rv != NDI_SUCCESS)
6231 		goto out;
6232 
6233 	init_bound_node_ev(pdip, child, flags);
6234 
6235 	if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
6236 	    (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
6237 	    (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
6238 		rv = devi_detach_node(child, flags);
6239 	} else {
6240 		/* call bus_config entry point */
6241 		rv = (*f)(pdip, flags, BUS_UNCONFIG_ONE, (void *)devnm);
6242 	}
6243 
6244 	if (brevq) {
6245 		if (rv != NDI_SUCCESS)
6246 			log_and_free_brevq_dip(child, brevq);
6247 		else
6248 			free_brevq(brevq);
6249 	}
6250 
6251 	if (dipp && rv != NDI_SUCCESS) {
6252 		ndi_hold_devi(child);
6253 		ASSERT(*dipp == NULL);
6254 		*dipp = child;
6255 	}
6256 
6257 out:
6258 	ndi_devi_exit(pdip, circ);
6259 	if (vdip)
6260 		ndi_devi_exit(vdip, v_circ);
6261 
6262 	pm_post_unconfig(pdip, pm_cookie, devnm);
6263 
6264 	return (rv);
6265 }
6266 
6267 struct async_arg {
6268 	dev_info_t *dip;
6269 	uint_t flags;
6270 };
6271 
6272 /*
6273  * Common async handler for:
6274  *	ndi_devi_bind_driver_async
6275  *	ndi_devi_online_async
6276  */
6277 static int
6278 i_ndi_devi_async_common(dev_info_t *dip, uint_t flags, void (*func)())
6279 {
6280 	int tqflag;
6281 	int kmflag;
6282 	struct async_arg *arg;
6283 	dev_info_t *pdip = ddi_get_parent(dip);
6284 
6285 	ASSERT(pdip);
6286 	ASSERT(DEVI(pdip)->devi_taskq);
6287 	ASSERT(ndi_dev_is_persistent_node(dip));
6288 
6289 	if (flags & NDI_NOSLEEP) {
6290 		kmflag = KM_NOSLEEP;
6291 		tqflag = TQ_NOSLEEP;
6292 	} else {
6293 		kmflag = KM_SLEEP;
6294 		tqflag = TQ_SLEEP;
6295 	}
6296 
6297 	arg = kmem_alloc(sizeof (*arg), kmflag);
6298 	if (arg == NULL)
6299 		goto fail;
6300 
6301 	arg->flags = flags;
6302 	arg->dip = dip;
6303 	if (ddi_taskq_dispatch(DEVI(pdip)->devi_taskq, func, arg, tqflag) ==
6304 	    DDI_SUCCESS) {
6305 		return (NDI_SUCCESS);
6306 	}
6307 
6308 fail:
6309 	NDI_CONFIG_DEBUG((CE_CONT, "%s%d: ddi_taskq_dispatch failed",
6310 	    ddi_driver_name(pdip), ddi_get_instance(pdip)));
6311 
6312 	if (arg)
6313 		kmem_free(arg, sizeof (*arg));
6314 	return (NDI_FAILURE);
6315 }
6316 
6317 static void
6318 i_ndi_devi_bind_driver_cb(struct async_arg *arg)
6319 {
6320 	(void) ndi_devi_bind_driver(arg->dip, arg->flags);
6321 	kmem_free(arg, sizeof (*arg));
6322 }
6323 
6324 int
6325 ndi_devi_bind_driver_async(dev_info_t *dip, uint_t flags)
6326 {
6327 	return (i_ndi_devi_async_common(dip, flags,
6328 	    (void (*)())i_ndi_devi_bind_driver_cb));
6329 }
6330 
6331 /*
6332  * place the devinfo in the ONLINE state.
6333  */
6334 int
6335 ndi_devi_online(dev_info_t *dip, uint_t flags)
6336 {
6337 	int circ, rv;
6338 	dev_info_t *pdip = ddi_get_parent(dip);
6339 	int branch_event = 0;
6340 
6341 	ASSERT(pdip);
6342 
6343 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_online: %s%d (%p)\n",
6344 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
6345 
6346 	ndi_devi_enter(pdip, &circ);
6347 	/* bind child before merging .conf nodes */
6348 	rv = i_ndi_config_node(dip, DS_BOUND, flags);
6349 	if (rv != NDI_SUCCESS) {
6350 		ndi_devi_exit(pdip, circ);
6351 		return (rv);
6352 	}
6353 
6354 	/* merge .conf properties */
6355 	(void) i_ndi_make_spec_children(pdip, flags);
6356 
6357 	flags |= (NDI_DEVI_ONLINE | NDI_CONFIG);
6358 
6359 	if (flags & NDI_NO_EVENT) {
6360 		/*
6361 		 * Caller is specifically asking for not to generate an event.
6362 		 * Set the following flag so that devi_attach_node() don't
6363 		 * change the event state.
6364 		 */
6365 		flags |= NDI_NO_EVENT_STATE_CHNG;
6366 	}
6367 
6368 	if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
6369 	    ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip))) {
6370 		flags |= NDI_BRANCH_EVENT_OP;
6371 		branch_event = 1;
6372 	}
6373 
6374 	/*
6375 	 * devi_attach_node() may remove dip on failure
6376 	 */
6377 	if ((rv = devi_attach_node(dip, flags)) == NDI_SUCCESS) {
6378 		if ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip)) {
6379 			(void) ndi_devi_config(dip, flags);
6380 		}
6381 
6382 		if (branch_event)
6383 			(void) i_log_devfs_branch_add(dip);
6384 	}
6385 
6386 	ndi_devi_exit(pdip, circ);
6387 
6388 	/*
6389 	 * Notify devfs that we have a new node. Devfs needs to invalidate
6390 	 * cached directory contents.
6391 	 *
6392 	 * For PCMCIA devices, it is possible the pdip is not fully
6393 	 * attached. In this case, calling back into devfs will
6394 	 * result in a loop or assertion error. Hence, the check
6395 	 * on node state.
6396 	 *
6397 	 * If we own parent lock, this is part of a branch operation.
6398 	 * We skip the devfs_clean() step because the cache invalidation
6399 	 * is done higher up in the device tree.
6400 	 */
6401 	if (rv == NDI_SUCCESS && i_ddi_devi_attached(pdip) &&
6402 	    !DEVI_BUSY_OWNED(pdip))
6403 		(void) devfs_clean(pdip, NULL, 0);
6404 	return (rv);
6405 }
6406 
6407 static void
6408 i_ndi_devi_online_cb(struct async_arg *arg)
6409 {
6410 	(void) ndi_devi_online(arg->dip, arg->flags);
6411 	kmem_free(arg, sizeof (*arg));
6412 }
6413 
6414 int
6415 ndi_devi_online_async(dev_info_t *dip, uint_t flags)
6416 {
6417 	/* mark child as need config if requested. */
6418 	if (flags & NDI_CONFIG) {
6419 		mutex_enter(&(DEVI(dip)->devi_lock));
6420 		DEVI_SET_NDI_CONFIG(dip);
6421 		mutex_exit(&(DEVI(dip)->devi_lock));
6422 	}
6423 
6424 	return (i_ndi_devi_async_common(dip, flags,
6425 	    (void (*)())i_ndi_devi_online_cb));
6426 }
6427 
6428 /*
6429  * Take a device node Offline
6430  * To take a device Offline means to detach the device instance from
6431  * the driver and prevent devfs requests from re-attaching the device
6432  * instance.
6433  *
6434  * The flag NDI_DEVI_REMOVE causes removes the device node from
6435  * the driver list and the device tree. In this case, the device
6436  * is assumed to be removed from the system.
6437  */
6438 int
6439 ndi_devi_offline(dev_info_t *dip, uint_t flags)
6440 {
6441 	int		circ, rval = 0;
6442 	dev_info_t	*pdip = ddi_get_parent(dip);
6443 	dev_info_t	*vdip = NULL;
6444 	int		v_circ;
6445 	struct brevq_node *brevq = NULL;
6446 
6447 	ASSERT(pdip);
6448 
6449 	flags |= NDI_DEVI_OFFLINE;
6450 
6451 	/*
6452 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
6453 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
6454 	 * management operations.
6455 	 */
6456 	if (MDI_PHCI(dip)) {
6457 		vdip = mdi_devi_get_vdip(dip);
6458 		if (vdip && (ddi_get_parent(vdip) != pdip))
6459 			ndi_devi_enter(vdip, &v_circ);
6460 		else
6461 			vdip = NULL;
6462 	}
6463 	ndi_devi_enter(pdip, &circ);
6464 
6465 	if (i_ddi_devi_attached(dip)) {
6466 		/*
6467 		 * If dip is in DS_READY state, there may be cached dv_nodes
6468 		 * referencing this dip, so we invoke devfs code path.
6469 		 * Note that we must release busy changing on pdip to
6470 		 * avoid deadlock against devfs.
6471 		 */
6472 		char *devname = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
6473 		(void) ddi_deviname(dip, devname);
6474 
6475 		ndi_devi_exit(pdip, circ);
6476 		if (vdip)
6477 			ndi_devi_exit(vdip, v_circ);
6478 
6479 		/*
6480 		 * If we are explictly told to clean, then clean. If we own the
6481 		 * parent lock then this is part of a branch operation, and we
6482 		 * skip the devfs_clean() step.
6483 		 *
6484 		 * NOTE: A thread performing a devfs file system lookup/
6485 		 * bus_config can't call devfs_clean to unconfig without
6486 		 * causing rwlock problems in devfs. For ndi_devi_offline, this
6487 		 * means that the NDI_DEVFS_CLEAN flag is safe from ioctl code
6488 		 * or from an async hotplug thread, but is not safe from a
6489 		 * nexus driver's bus_config implementation.
6490 		 */
6491 		if ((flags & NDI_DEVFS_CLEAN) ||
6492 		    (!DEVI_BUSY_OWNED(pdip)))
6493 			(void) devfs_clean(pdip, devname + 1, DV_CLEAN_FORCE);
6494 
6495 		kmem_free(devname, MAXNAMELEN + 1);
6496 
6497 		rval = devi_unconfig_branch(dip, NULL, flags|NDI_UNCONFIG,
6498 		    &brevq);
6499 
6500 		if (rval)
6501 			return (NDI_FAILURE);
6502 
6503 		if (vdip)
6504 			ndi_devi_enter(vdip, &v_circ);
6505 		ndi_devi_enter(pdip, &circ);
6506 	}
6507 
6508 	init_bound_node_ev(pdip, dip, flags);
6509 
6510 	rval = devi_detach_node(dip, flags);
6511 	if (brevq) {
6512 		if (rval != NDI_SUCCESS)
6513 			log_and_free_brevq_dip(dip, brevq);
6514 		else
6515 			free_brevq(brevq);
6516 	}
6517 
6518 	ndi_devi_exit(pdip, circ);
6519 	if (vdip)
6520 		ndi_devi_exit(vdip, v_circ);
6521 
6522 	return (rval);
6523 }
6524 
6525 /*
6526  * Find the child dev_info node of parent nexus 'p' whose unit address
6527  * matches "cname@caddr".  Recommend use of ndi_devi_findchild() instead.
6528  */
6529 dev_info_t *
6530 ndi_devi_find(dev_info_t *pdip, char *cname, char *caddr)
6531 {
6532 	dev_info_t *child;
6533 	int circ;
6534 
6535 	if (pdip == NULL || cname == NULL || caddr == NULL)
6536 		return ((dev_info_t *)NULL);
6537 
6538 	ndi_devi_enter(pdip, &circ);
6539 	child = find_sibling(ddi_get_child(pdip), cname, caddr,
6540 	    FIND_NODE_BY_NODENAME, NULL);
6541 	ndi_devi_exit(pdip, circ);
6542 	return (child);
6543 }
6544 
6545 /*
6546  * Find the child dev_info node of parent nexus 'p' whose unit address
6547  * matches devname "name@addr".  Permits caller to hold the parent.
6548  */
6549 dev_info_t *
6550 ndi_devi_findchild(dev_info_t *pdip, char *devname)
6551 {
6552 	dev_info_t *child;
6553 	char	*cname, *caddr;
6554 	char	*devstr;
6555 
6556 	ASSERT(DEVI_BUSY_OWNED(pdip));
6557 
6558 	devstr = i_ddi_strdup(devname, KM_SLEEP);
6559 	i_ddi_parse_name(devstr, &cname, &caddr, NULL);
6560 
6561 	if (cname == NULL || caddr == NULL) {
6562 		kmem_free(devstr, strlen(devname)+1);
6563 		return ((dev_info_t *)NULL);
6564 	}
6565 
6566 	child = find_sibling(ddi_get_child(pdip), cname, caddr,
6567 	    FIND_NODE_BY_NODENAME, NULL);
6568 	kmem_free(devstr, strlen(devname)+1);
6569 	return (child);
6570 }
6571 
6572 /*
6573  * Misc. routines called by framework only
6574  */
6575 
6576 /*
6577  * Clear the DEVI_MADE_CHILDREN/DEVI_ATTACHED_CHILDREN flags
6578  * if new child spec has been added.
6579  */
6580 static int
6581 reset_nexus_flags(dev_info_t *dip, void *arg)
6582 {
6583 	struct hwc_spec	*list;
6584 	int		circ;
6585 
6586 	if (((DEVI(dip)->devi_flags & DEVI_MADE_CHILDREN) == 0) ||
6587 	    ((list = hwc_get_child_spec(dip, (major_t)(uintptr_t)arg)) == NULL))
6588 		return (DDI_WALK_CONTINUE);
6589 
6590 	hwc_free_spec_list(list);
6591 
6592 	/* coordinate child state update */
6593 	ndi_devi_enter(dip, &circ);
6594 	mutex_enter(&DEVI(dip)->devi_lock);
6595 	DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN | DEVI_ATTACHED_CHILDREN);
6596 	mutex_exit(&DEVI(dip)->devi_lock);
6597 	ndi_devi_exit(dip, circ);
6598 
6599 	return (DDI_WALK_CONTINUE);
6600 }
6601 
6602 /*
6603  * Helper functions, returns NULL if no memory.
6604  */
6605 
6606 /*
6607  * path_to_major:
6608  *
6609  * Return an alternate driver name binding for the leaf device
6610  * of the given pathname, if there is one. The purpose of this
6611  * function is to deal with generic pathnames. The default action
6612  * for platforms that can't do this (ie: x86 or any platform that
6613  * does not have prom_finddevice functionality, which matches
6614  * nodenames and unit-addresses without the drivers participation)
6615  * is to return DDI_MAJOR_T_NONE.
6616  *
6617  * Used in loadrootmodules() in the swapgeneric module to
6618  * associate a given pathname with a given leaf driver.
6619  *
6620  */
6621 major_t
6622 path_to_major(char *path)
6623 {
6624 	dev_info_t *dip;
6625 	char *p, *q;
6626 	pnode_t nodeid;
6627 	major_t major;
6628 
6629 	/* check for path-oriented alias */
6630 	major = ddi_name_to_major(path);
6631 	if (driver_installed(major)) {
6632 		NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s path bound %s\n",
6633 		    path, ddi_major_to_name(major)));
6634 		return (major);
6635 	}
6636 
6637 	/*
6638 	 * Get the nodeid of the given pathname, if such a mapping exists.
6639 	 */
6640 	dip = NULL;
6641 	nodeid = prom_finddevice(path);
6642 	if (nodeid != OBP_BADNODE) {
6643 		/*
6644 		 * Find the nodeid in our copy of the device tree and return
6645 		 * whatever name we used to bind this node to a driver.
6646 		 */
6647 		dip = e_ddi_nodeid_to_dip(nodeid);
6648 	}
6649 
6650 	if (dip == NULL) {
6651 		NDI_CONFIG_DEBUG((CE_WARN,
6652 		    "path_to_major: can't bind <%s>\n", path));
6653 		return (DDI_MAJOR_T_NONE);
6654 	}
6655 
6656 	/*
6657 	 * If we're bound to something other than the nodename,
6658 	 * note that in the message buffer and system log.
6659 	 */
6660 	p = ddi_binding_name(dip);
6661 	q = ddi_node_name(dip);
6662 	if (p && q && (strcmp(p, q) != 0))
6663 		NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s bound to %s\n",
6664 		    path, p));
6665 
6666 	major = ddi_name_to_major(p);
6667 
6668 	ndi_rele_devi(dip);		/* release e_ddi_nodeid_to_dip hold */
6669 
6670 	return (major);
6671 }
6672 
6673 /*
6674  * Return the held dip for the specified major and instance, attempting to do
6675  * an attach if specified. Return NULL if the devi can't be found or put in
6676  * the proper state. The caller must release the hold via ddi_release_devi if
6677  * a non-NULL value is returned.
6678  *
6679  * Some callers expect to be able to perform a hold_devi() while in a context
6680  * where using ndi_devi_enter() to ensure the hold might cause deadlock (see
6681  * open-from-attach code in consconfig_dacf.c). Such special-case callers
6682  * must ensure that an ndi_devi_enter(parent)/ndi_hold_devi() from a safe
6683  * context is already active. The hold_devi() implementation must accommodate
6684  * these callers.
6685  */
6686 static dev_info_t *
6687 hold_devi(major_t major, int instance, int flags)
6688 {
6689 	struct devnames	*dnp;
6690 	dev_info_t	*dip;
6691 	char		*path;
6692 	char		*vpath;
6693 
6694 	if ((major >= devcnt) || (instance == -1))
6695 		return (NULL);
6696 
6697 	/* try to find the instance in the per driver list */
6698 	dnp = &(devnamesp[major]);
6699 	LOCK_DEV_OPS(&(dnp->dn_lock));
6700 	for (dip = dnp->dn_head; dip;
6701 	    dip = (dev_info_t *)DEVI(dip)->devi_next) {
6702 		/* skip node if instance field is not valid */
6703 		if (i_ddi_node_state(dip) < DS_INITIALIZED)
6704 			continue;
6705 
6706 		/* look for instance match */
6707 		if (DEVI(dip)->devi_instance == instance) {
6708 			/*
6709 			 * To accommodate callers that can't block in
6710 			 * ndi_devi_enter() we do an ndi_hold_devi(), and
6711 			 * afterwards check that the node is in a state where
6712 			 * the hold prevents detach(). If we did not manage to
6713 			 * prevent detach then we ndi_rele_devi() and perform
6714 			 * the slow path below (which can result in a blocking
6715 			 * ndi_devi_enter() while driving attach top-down).
6716 			 * This code depends on the ordering of
6717 			 * DEVI_SET_DETACHING and the devi_ref check in the
6718 			 * detach_node() code path.
6719 			 */
6720 			ndi_hold_devi(dip);
6721 			if (i_ddi_devi_attached(dip) &&
6722 			    !DEVI_IS_DETACHING(dip)) {
6723 				UNLOCK_DEV_OPS(&(dnp->dn_lock));
6724 				return (dip);	/* fast-path with devi held */
6725 			}
6726 			ndi_rele_devi(dip);
6727 
6728 			/* try slow-path */
6729 			dip = NULL;
6730 			break;
6731 		}
6732 	}
6733 	ASSERT(dip == NULL);
6734 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
6735 
6736 	if (flags & E_DDI_HOLD_DEVI_NOATTACH)
6737 		return (NULL);		/* told not to drive attach */
6738 
6739 	/* slow-path may block, so it should not occur from interrupt */
6740 	ASSERT(!servicing_interrupt());
6741 	if (servicing_interrupt())
6742 		return (NULL);
6743 
6744 	/* reconstruct the path and drive attach by path through devfs. */
6745 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
6746 	if (e_ddi_majorinstance_to_path(major, instance, path) == 0) {
6747 		dip = e_ddi_hold_devi_by_path(path, flags);
6748 
6749 		/*
6750 		 * Verify that we got the correct device - a path_to_inst file
6751 		 * with a bogus/corrupt path (or a nexus that changes its
6752 		 * unit-address format) could result in an incorrect answer
6753 		 *
6754 		 * Verify major, instance, and path.
6755 		 */
6756 		vpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
6757 		if (dip &&
6758 		    ((DEVI(dip)->devi_major != major) ||
6759 		    ((DEVI(dip)->devi_instance != instance)) ||
6760 		    (strcmp(path, ddi_pathname(dip, vpath)) != 0))) {
6761 			ndi_rele_devi(dip);
6762 			dip = NULL;	/* no answer better than wrong answer */
6763 		}
6764 		kmem_free(vpath, MAXPATHLEN);
6765 	}
6766 	kmem_free(path, MAXPATHLEN);
6767 	return (dip);			/* with devi held */
6768 }
6769 
6770 /*
6771  * The {e_}ddi_hold_devi{_by_{instance|dev|path}} hold the devinfo node
6772  * associated with the specified arguments.  This hold should be released
6773  * by calling ddi_release_devi.
6774  *
6775  * The E_DDI_HOLD_DEVI_NOATTACH flag argument allows the caller to to specify
6776  * a failure return if the node is not already attached.
6777  *
6778  * NOTE: by the time we make e_ddi_hold_devi public, we should be able to reuse
6779  * ddi_hold_devi again.
6780  */
6781 dev_info_t *
6782 ddi_hold_devi_by_instance(major_t major, int instance, int flags)
6783 {
6784 	return (hold_devi(major, instance, flags));
6785 }
6786 
6787 dev_info_t *
6788 e_ddi_hold_devi_by_dev(dev_t dev, int flags)
6789 {
6790 	major_t	major = getmajor(dev);
6791 	dev_info_t	*dip;
6792 	struct dev_ops	*ops;
6793 	dev_info_t	*ddip = NULL;
6794 
6795 	dip = hold_devi(major, dev_to_instance(dev), flags);
6796 
6797 	/*
6798 	 * The rest of this routine is legacy support for drivers that
6799 	 * have broken DDI_INFO_DEVT2INSTANCE implementations but may have
6800 	 * functional DDI_INFO_DEVT2DEVINFO implementations.  This code will
6801 	 * diagnose inconsistency and, for maximum compatibility with legacy
6802 	 * drivers, give preference to the drivers DDI_INFO_DEVT2DEVINFO
6803 	 * implementation over the above derived dip based the driver's
6804 	 * DDI_INFO_DEVT2INSTANCE implementation. This legacy support should
6805 	 * be removed when DDI_INFO_DEVT2DEVINFO is deprecated.
6806 	 *
6807 	 * NOTE: The following code has a race condition. DEVT2DEVINFO
6808 	 *	returns a dip which is not held. By the time we ref ddip,
6809 	 *	it could have been freed. The saving grace is that for
6810 	 *	most drivers, the dip returned from hold_devi() is the
6811 	 *	same one as the one returned by DEVT2DEVINFO, so we are
6812 	 *	safe for drivers with the correct getinfo(9e) impl.
6813 	 */
6814 	if (((ops = ddi_hold_driver(major)) != NULL) &&
6815 	    CB_DRV_INSTALLED(ops) && ops->devo_getinfo)  {
6816 		if ((*ops->devo_getinfo)(NULL, DDI_INFO_DEVT2DEVINFO,
6817 		    (void *)dev, (void **)&ddip) != DDI_SUCCESS)
6818 			ddip = NULL;
6819 	}
6820 
6821 	/* give preference to the driver returned DEVT2DEVINFO dip */
6822 	if (ddip && (dip != ddip)) {
6823 #ifdef	DEBUG
6824 		cmn_err(CE_WARN, "%s: inconsistent getinfo(9E) implementation",
6825 		    ddi_driver_name(ddip));
6826 #endif	/* DEBUG */
6827 		ndi_hold_devi(ddip);
6828 		if (dip)
6829 			ndi_rele_devi(dip);
6830 		dip = ddip;
6831 	}
6832 
6833 	if (ops)
6834 		ddi_rele_driver(major);
6835 
6836 	return (dip);
6837 }
6838 
6839 /*
6840  * For compatibility only. Do not call this function!
6841  */
6842 dev_info_t *
6843 e_ddi_get_dev_info(dev_t dev, vtype_t type)
6844 {
6845 	dev_info_t *dip = NULL;
6846 	if (getmajor(dev) >= devcnt)
6847 		return (NULL);
6848 
6849 	switch (type) {
6850 	case VCHR:
6851 	case VBLK:
6852 		dip = e_ddi_hold_devi_by_dev(dev, 0);
6853 	default:
6854 		break;
6855 	}
6856 
6857 	/*
6858 	 * For compatibility reasons, we can only return the dip with
6859 	 * the driver ref count held. This is not a safe thing to do.
6860 	 * For certain broken third-party software, we are willing
6861 	 * to venture into unknown territory.
6862 	 */
6863 	if (dip) {
6864 		(void) ndi_hold_driver(dip);
6865 		ndi_rele_devi(dip);
6866 	}
6867 	return (dip);
6868 }
6869 
6870 dev_info_t *
6871 e_ddi_hold_devi_by_path(char *path, int flags)
6872 {
6873 	dev_info_t	*dip;
6874 
6875 	/* can't specify NOATTACH by path */
6876 	ASSERT(!(flags & E_DDI_HOLD_DEVI_NOATTACH));
6877 
6878 	return (resolve_pathname(path, &dip, NULL, NULL) ? NULL : dip);
6879 }
6880 
6881 void
6882 e_ddi_hold_devi(dev_info_t *dip)
6883 {
6884 	ndi_hold_devi(dip);
6885 }
6886 
6887 void
6888 ddi_release_devi(dev_info_t *dip)
6889 {
6890 	ndi_rele_devi(dip);
6891 }
6892 
6893 /*
6894  * Associate a streams queue with a devinfo node
6895  * NOTE: This function is called by STREAM driver's put procedure.
6896  *	It cannot block.
6897  */
6898 void
6899 ddi_assoc_queue_with_devi(queue_t *q, dev_info_t *dip)
6900 {
6901 	queue_t *rq = _RD(q);
6902 	struct stdata *stp;
6903 	vnode_t *vp;
6904 
6905 	/* set flag indicating that ddi_assoc_queue_with_devi was called */
6906 	mutex_enter(QLOCK(rq));
6907 	rq->q_flag |= _QASSOCIATED;
6908 	mutex_exit(QLOCK(rq));
6909 
6910 	/* get the vnode associated with the queue */
6911 	stp = STREAM(rq);
6912 	vp = stp->sd_vnode;
6913 	ASSERT(vp);
6914 
6915 	/* change the hardware association of the vnode */
6916 	spec_assoc_vp_with_devi(vp, dip);
6917 }
6918 
6919 /*
6920  * ddi_install_driver(name)
6921  *
6922  * Driver installation is currently a byproduct of driver loading.  This
6923  * may change.
6924  */
6925 int
6926 ddi_install_driver(char *name)
6927 {
6928 	major_t major = ddi_name_to_major(name);
6929 
6930 	if ((major == DDI_MAJOR_T_NONE) ||
6931 	    (ddi_hold_installed_driver(major) == NULL)) {
6932 		return (DDI_FAILURE);
6933 	}
6934 	ddi_rele_driver(major);
6935 	return (DDI_SUCCESS);
6936 }
6937 
6938 struct dev_ops *
6939 ddi_hold_driver(major_t major)
6940 {
6941 	return (mod_hold_dev_by_major(major));
6942 }
6943 
6944 
6945 void
6946 ddi_rele_driver(major_t major)
6947 {
6948 	mod_rele_dev_by_major(major);
6949 }
6950 
6951 
6952 /*
6953  * This is called during boot to force attachment order of special dips
6954  * dip must be referenced via ndi_hold_devi()
6955  */
6956 int
6957 i_ddi_attach_node_hierarchy(dev_info_t *dip)
6958 {
6959 	dev_info_t	*parent;
6960 	int		ret, circ;
6961 
6962 	/*
6963 	 * Recurse up until attached parent is found.
6964 	 */
6965 	if (i_ddi_devi_attached(dip))
6966 		return (DDI_SUCCESS);
6967 	parent = ddi_get_parent(dip);
6968 	if (i_ddi_attach_node_hierarchy(parent) != DDI_SUCCESS)
6969 		return (DDI_FAILURE);
6970 
6971 	/*
6972 	 * Come top-down, expanding .conf nodes under this parent
6973 	 * and driving attach.
6974 	 */
6975 	ndi_devi_enter(parent, &circ);
6976 	(void) i_ndi_make_spec_children(parent, 0);
6977 	ret = i_ddi_attachchild(dip);
6978 	ndi_devi_exit(parent, circ);
6979 
6980 	return (ret);
6981 }
6982 
6983 /* keep this function static */
6984 static int
6985 attach_driver_nodes(major_t major)
6986 {
6987 	struct devnames *dnp;
6988 	dev_info_t *dip;
6989 	int error = DDI_FAILURE;
6990 	int circ;
6991 
6992 	dnp = &devnamesp[major];
6993 	LOCK_DEV_OPS(&dnp->dn_lock);
6994 	dip = dnp->dn_head;
6995 	while (dip) {
6996 		ndi_hold_devi(dip);
6997 		UNLOCK_DEV_OPS(&dnp->dn_lock);
6998 		if (i_ddi_attach_node_hierarchy(dip) == DDI_SUCCESS)
6999 			error = DDI_SUCCESS;
7000 		/*
7001 		 * Set the 'ddi-config-driver-node' property on a nexus
7002 		 * node to cause attach_driver_nodes() to configure all
7003 		 * immediate children of the nexus. This property should
7004 		 * be set on nodes with immediate children that bind to
7005 		 * the same driver as parent.
7006 		 */
7007 		if ((error == DDI_SUCCESS) && (ddi_prop_exists(DDI_DEV_T_ANY,
7008 		    dip, DDI_PROP_DONTPASS, "ddi-config-driver-node"))) {
7009 			ndi_devi_enter(dip, &circ);
7010 			(void) ndi_devi_config(dip, NDI_NO_EVENT);
7011 			ndi_devi_exit(dip, circ);
7012 		}
7013 		LOCK_DEV_OPS(&dnp->dn_lock);
7014 		ndi_rele_devi(dip);
7015 		dip = ddi_get_next(dip);
7016 	}
7017 	if (error == DDI_SUCCESS)
7018 		dnp->dn_flags |= DN_NO_AUTODETACH;
7019 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7020 
7021 
7022 	return (error);
7023 }
7024 
7025 /*
7026  * i_ddi_attach_hw_nodes configures and attaches all hw nodes
7027  * bound to a specific driver. This function replaces calls to
7028  * ddi_hold_installed_driver() for drivers with no .conf
7029  * enumerated nodes.
7030  *
7031  * This facility is typically called at boot time to attach
7032  * platform-specific hardware nodes, such as ppm nodes on xcal
7033  * and grover and keyswitch nodes on cherrystone. It does not
7034  * deal with .conf enumerated node. Calling it beyond the boot
7035  * process is strongly discouraged.
7036  */
7037 int
7038 i_ddi_attach_hw_nodes(char *driver)
7039 {
7040 	major_t major;
7041 
7042 	major = ddi_name_to_major(driver);
7043 	if (major == DDI_MAJOR_T_NONE)
7044 		return (DDI_FAILURE);
7045 
7046 	return (attach_driver_nodes(major));
7047 }
7048 
7049 /*
7050  * i_ddi_attach_pseudo_node configures pseudo drivers which
7051  * has a single node. The .conf nodes must be enumerated
7052  * before calling this interface. The dip is held attached
7053  * upon returning.
7054  *
7055  * This facility should only be called only at boot time
7056  * by the I/O framework.
7057  */
7058 dev_info_t *
7059 i_ddi_attach_pseudo_node(char *driver)
7060 {
7061 	major_t major;
7062 	dev_info_t *dip;
7063 
7064 	major = ddi_name_to_major(driver);
7065 	if (major == DDI_MAJOR_T_NONE)
7066 		return (NULL);
7067 
7068 	if (attach_driver_nodes(major) != DDI_SUCCESS)
7069 		return (NULL);
7070 
7071 	dip = devnamesp[major].dn_head;
7072 	ASSERT(dip && ddi_get_next(dip) == NULL);
7073 	ndi_hold_devi(dip);
7074 	return (dip);
7075 }
7076 
7077 static void
7078 diplist_to_parent_major(dev_info_t *head, char parents[])
7079 {
7080 	major_t major;
7081 	dev_info_t *dip, *pdip;
7082 
7083 	for (dip = head; dip != NULL; dip = ddi_get_next(dip)) {
7084 		pdip = ddi_get_parent(dip);
7085 		ASSERT(pdip);	/* disallow rootnex.conf nodes */
7086 		major = ddi_driver_major(pdip);
7087 		if ((major != DDI_MAJOR_T_NONE) && parents[major] == 0)
7088 			parents[major] = 1;
7089 	}
7090 }
7091 
7092 /*
7093  * Call ddi_hold_installed_driver() on each parent major
7094  * and invoke mt_config_driver() to attach child major.
7095  * This is part of the implementation of ddi_hold_installed_driver.
7096  */
7097 static int
7098 attach_driver_by_parent(major_t child_major, char parents[])
7099 {
7100 	major_t par_major;
7101 	struct mt_config_handle *hdl;
7102 	int flags = NDI_DEVI_PERSIST | NDI_NO_EVENT;
7103 
7104 	hdl = mt_config_init(NULL, NULL, flags, child_major, MT_CONFIG_OP,
7105 	    NULL);
7106 	for (par_major = 0; par_major < devcnt; par_major++) {
7107 		/* disallow recursion on the same driver */
7108 		if (parents[par_major] == 0 || par_major == child_major)
7109 			continue;
7110 		if (ddi_hold_installed_driver(par_major) == NULL)
7111 			continue;
7112 		hdl->mtc_parmajor = par_major;
7113 		mt_config_driver(hdl);
7114 		ddi_rele_driver(par_major);
7115 	}
7116 	(void) mt_config_fini(hdl);
7117 
7118 	return (i_ddi_devs_attached(child_major));
7119 }
7120 
7121 int
7122 i_ddi_devs_attached(major_t major)
7123 {
7124 	dev_info_t *dip;
7125 	struct devnames *dnp;
7126 	int error = DDI_FAILURE;
7127 
7128 	/* check for attached instances */
7129 	dnp = &devnamesp[major];
7130 	LOCK_DEV_OPS(&dnp->dn_lock);
7131 	for (dip = dnp->dn_head; dip != NULL; dip = ddi_get_next(dip)) {
7132 		if (i_ddi_devi_attached(dip)) {
7133 			error = DDI_SUCCESS;
7134 			break;
7135 		}
7136 	}
7137 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7138 
7139 	return (error);
7140 }
7141 
7142 int
7143 i_ddi_minor_node_count(dev_info_t *ddip, const char *node_type)
7144 {
7145 	int			circ;
7146 	struct ddi_minor_data	*dp;
7147 	int			count = 0;
7148 
7149 	ndi_devi_enter(ddip, &circ);
7150 	for (dp = DEVI(ddip)->devi_minor; dp != NULL; dp = dp->next) {
7151 		if (strcmp(dp->ddm_node_type, node_type) == 0)
7152 			count++;
7153 	}
7154 	ndi_devi_exit(ddip, circ);
7155 	return (count);
7156 }
7157 
7158 /*
7159  * ddi_hold_installed_driver configures and attaches all
7160  * instances of the specified driver. To accomplish this
7161  * it configures and attaches all possible parents of
7162  * the driver, enumerated both in h/w nodes and in the
7163  * driver's .conf file.
7164  *
7165  * NOTE: This facility is for compatibility purposes only and will
7166  *	eventually go away. Its usage is strongly discouraged.
7167  */
7168 static void
7169 enter_driver(struct devnames *dnp)
7170 {
7171 	mutex_enter(&dnp->dn_lock);
7172 	ASSERT(dnp->dn_busy_thread != curthread);
7173 	while (dnp->dn_flags & DN_DRIVER_BUSY)
7174 		cv_wait(&dnp->dn_wait, &dnp->dn_lock);
7175 	dnp->dn_flags |= DN_DRIVER_BUSY;
7176 	dnp->dn_busy_thread = curthread;
7177 	mutex_exit(&dnp->dn_lock);
7178 }
7179 
7180 static void
7181 exit_driver(struct devnames *dnp)
7182 {
7183 	mutex_enter(&dnp->dn_lock);
7184 	ASSERT(dnp->dn_busy_thread == curthread);
7185 	dnp->dn_flags &= ~DN_DRIVER_BUSY;
7186 	dnp->dn_busy_thread = NULL;
7187 	cv_broadcast(&dnp->dn_wait);
7188 	mutex_exit(&dnp->dn_lock);
7189 }
7190 
7191 struct dev_ops *
7192 ddi_hold_installed_driver(major_t major)
7193 {
7194 	struct dev_ops *ops;
7195 	struct devnames *dnp;
7196 	char *parents;
7197 	int error;
7198 
7199 	ops = ddi_hold_driver(major);
7200 	if (ops == NULL)
7201 		return (NULL);
7202 
7203 	/*
7204 	 * Return immediately if all the attach operations associated
7205 	 * with a ddi_hold_installed_driver() call have already been done.
7206 	 */
7207 	dnp = &devnamesp[major];
7208 	enter_driver(dnp);
7209 	ASSERT(driver_installed(major));
7210 
7211 	if (dnp->dn_flags & DN_DRIVER_HELD) {
7212 		exit_driver(dnp);
7213 		if (i_ddi_devs_attached(major) == DDI_SUCCESS)
7214 			return (ops);
7215 		ddi_rele_driver(major);
7216 		return (NULL);
7217 	}
7218 
7219 	LOCK_DEV_OPS(&dnp->dn_lock);
7220 	dnp->dn_flags |= (DN_DRIVER_HELD | DN_NO_AUTODETACH);
7221 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7222 
7223 	DCOMPATPRINTF((CE_CONT,
7224 	    "ddi_hold_installed_driver: %s\n", dnp->dn_name));
7225 
7226 	/*
7227 	 * When the driver has no .conf children, it is sufficient
7228 	 * to attach existing nodes in the device tree. Nodes not
7229 	 * enumerated by the OBP are not attached.
7230 	 */
7231 	if (dnp->dn_pl == NULL) {
7232 		if (attach_driver_nodes(major) == DDI_SUCCESS) {
7233 			exit_driver(dnp);
7234 			return (ops);
7235 		}
7236 		exit_driver(dnp);
7237 		ddi_rele_driver(major);
7238 		return (NULL);
7239 	}
7240 
7241 	/*
7242 	 * Driver has .conf nodes. We find all possible parents
7243 	 * and recursively all ddi_hold_installed_driver on the
7244 	 * parent driver; then we invoke ndi_config_driver()
7245 	 * on all possible parent node in parallel to speed up
7246 	 * performance.
7247 	 */
7248 	parents = kmem_zalloc(devcnt * sizeof (char), KM_SLEEP);
7249 
7250 	LOCK_DEV_OPS(&dnp->dn_lock);
7251 	/* find .conf parents */
7252 	(void) impl_parlist_to_major(dnp->dn_pl, parents);
7253 	/* find hw node parents */
7254 	diplist_to_parent_major(dnp->dn_head, parents);
7255 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7256 
7257 	error = attach_driver_by_parent(major, parents);
7258 	kmem_free(parents, devcnt * sizeof (char));
7259 	if (error == DDI_SUCCESS) {
7260 		exit_driver(dnp);
7261 		return (ops);
7262 	}
7263 
7264 	exit_driver(dnp);
7265 	ddi_rele_driver(major);
7266 	return (NULL);
7267 }
7268 
7269 /*
7270  * Default bus_config entry point for nexus drivers
7271  */
7272 int
7273 ndi_busop_bus_config(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
7274     void *arg, dev_info_t **child, clock_t timeout)
7275 {
7276 	major_t major;
7277 
7278 	/*
7279 	 * A timeout of 30 minutes or more is probably a mistake
7280 	 * This is intended to catch uses where timeout is in
7281 	 * the wrong units.  timeout must be in units of ticks.
7282 	 */
7283 	ASSERT(timeout < SEC_TO_TICK(1800));
7284 
7285 	major = DDI_MAJOR_T_NONE;
7286 	switch (op) {
7287 	case BUS_CONFIG_ONE:
7288 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config %s timeout=%ld\n",
7289 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
7290 		    (char *)arg, timeout));
7291 		return (devi_config_one(pdip, (char *)arg, child, flags,
7292 		    timeout));
7293 
7294 	case BUS_CONFIG_DRIVER:
7295 		major = (major_t)(uintptr_t)arg;
7296 		/*FALLTHROUGH*/
7297 	case BUS_CONFIG_ALL:
7298 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config timeout=%ld\n",
7299 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
7300 		    timeout));
7301 		if (timeout > 0) {
7302 			NDI_DEBUG(flags, (CE_CONT,
7303 			    "%s%d: bus config all timeout=%ld\n",
7304 			    ddi_driver_name(pdip), ddi_get_instance(pdip),
7305 			    timeout));
7306 			delay(timeout);
7307 		}
7308 		return (config_immediate_children(pdip, flags, major));
7309 
7310 	default:
7311 		return (NDI_FAILURE);
7312 	}
7313 	/*NOTREACHED*/
7314 }
7315 
7316 /*
7317  * Default busop bus_unconfig handler for nexus drivers
7318  */
7319 int
7320 ndi_busop_bus_unconfig(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
7321     void *arg)
7322 {
7323 	major_t major;
7324 
7325 	major = DDI_MAJOR_T_NONE;
7326 	switch (op) {
7327 	case BUS_UNCONFIG_ONE:
7328 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig %s\n",
7329 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
7330 		    (char *)arg));
7331 		return (devi_unconfig_one(pdip, (char *)arg, flags));
7332 
7333 	case BUS_UNCONFIG_DRIVER:
7334 		major = (major_t)(uintptr_t)arg;
7335 		/*FALLTHROUGH*/
7336 	case BUS_UNCONFIG_ALL:
7337 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig all\n",
7338 		    ddi_driver_name(pdip), ddi_get_instance(pdip)));
7339 		return (unconfig_immediate_children(pdip, NULL, flags, major));
7340 
7341 	default:
7342 		return (NDI_FAILURE);
7343 	}
7344 	/*NOTREACHED*/
7345 }
7346 
7347 /*
7348  * dummy functions to be removed
7349  */
7350 void
7351 impl_rem_dev_props(dev_info_t *dip)
7352 {
7353 	_NOTE(ARGUNUSED(dip))
7354 	/* do nothing */
7355 }
7356 
7357 /*
7358  * Determine if a node is a leaf node. If not sure, return false (0).
7359  */
7360 static int
7361 is_leaf_node(dev_info_t *dip)
7362 {
7363 	major_t major = ddi_driver_major(dip);
7364 
7365 	if (major == DDI_MAJOR_T_NONE)
7366 		return (0);
7367 
7368 	return (devnamesp[major].dn_flags & DN_LEAF_DRIVER);
7369 }
7370 
7371 /*
7372  * Multithreaded [un]configuration
7373  */
7374 static struct mt_config_handle *
7375 mt_config_init(dev_info_t *pdip, dev_info_t **dipp, int flags,
7376     major_t major, int op, struct brevq_node **brevqp)
7377 {
7378 	struct mt_config_handle	*hdl = kmem_alloc(sizeof (*hdl), KM_SLEEP);
7379 
7380 	mutex_init(&hdl->mtc_lock, NULL, MUTEX_DEFAULT, NULL);
7381 	cv_init(&hdl->mtc_cv, NULL, CV_DEFAULT, NULL);
7382 	hdl->mtc_pdip = pdip;
7383 	hdl->mtc_fdip = dipp;
7384 	hdl->mtc_parmajor = DDI_MAJOR_T_NONE;
7385 	hdl->mtc_flags = flags;
7386 	hdl->mtc_major = major;
7387 	hdl->mtc_thr_count = 0;
7388 	hdl->mtc_op = op;
7389 	hdl->mtc_error = 0;
7390 	hdl->mtc_brevqp = brevqp;
7391 
7392 #ifdef DEBUG
7393 	gethrestime(&hdl->start_time);
7394 	hdl->total_time = 0;
7395 #endif /* DEBUG */
7396 
7397 	return (hdl);
7398 }
7399 
7400 #ifdef DEBUG
7401 static int
7402 time_diff_in_msec(timestruc_t start, timestruc_t end)
7403 {
7404 	int	nsec, sec;
7405 
7406 	sec = end.tv_sec - start.tv_sec;
7407 	nsec = end.tv_nsec - start.tv_nsec;
7408 	if (nsec < 0) {
7409 		nsec += NANOSEC;
7410 		sec -= 1;
7411 	}
7412 
7413 	return (sec * (NANOSEC >> 20) + (nsec >> 20));
7414 }
7415 
7416 #endif	/* DEBUG */
7417 
7418 static int
7419 mt_config_fini(struct mt_config_handle *hdl)
7420 {
7421 	int		rv;
7422 #ifdef DEBUG
7423 	int		real_time;
7424 	timestruc_t	end_time;
7425 #endif /* DEBUG */
7426 
7427 	mutex_enter(&hdl->mtc_lock);
7428 	while (hdl->mtc_thr_count > 0)
7429 		cv_wait(&hdl->mtc_cv, &hdl->mtc_lock);
7430 	rv = hdl->mtc_error;
7431 	mutex_exit(&hdl->mtc_lock);
7432 
7433 #ifdef DEBUG
7434 	gethrestime(&end_time);
7435 	real_time = time_diff_in_msec(hdl->start_time, end_time);
7436 	if ((ddidebug & DDI_MTCONFIG) && hdl->mtc_pdip)
7437 		cmn_err(CE_NOTE,
7438 		    "config %s%d: total time %d msec, real time %d msec",
7439 		    ddi_driver_name(hdl->mtc_pdip),
7440 		    ddi_get_instance(hdl->mtc_pdip),
7441 		    hdl->total_time, real_time);
7442 #endif /* DEBUG */
7443 
7444 	cv_destroy(&hdl->mtc_cv);
7445 	mutex_destroy(&hdl->mtc_lock);
7446 	kmem_free(hdl, sizeof (*hdl));
7447 
7448 	return (rv);
7449 }
7450 
7451 struct mt_config_data {
7452 	struct mt_config_handle	*mtc_hdl;
7453 	dev_info_t		*mtc_dip;
7454 	major_t			mtc_major;
7455 	int			mtc_flags;
7456 	struct brevq_node	*mtc_brn;
7457 	struct mt_config_data	*mtc_next;
7458 };
7459 
7460 static void
7461 mt_config_thread(void *arg)
7462 {
7463 	struct mt_config_data	*mcd = (struct mt_config_data *)arg;
7464 	struct mt_config_handle	*hdl = mcd->mtc_hdl;
7465 	dev_info_t		*dip = mcd->mtc_dip;
7466 	dev_info_t		*rdip, **dipp;
7467 	major_t			major = mcd->mtc_major;
7468 	int			flags = mcd->mtc_flags;
7469 	int			rv = 0;
7470 
7471 #ifdef DEBUG
7472 	timestruc_t start_time, end_time;
7473 	gethrestime(&start_time);
7474 #endif /* DEBUG */
7475 
7476 	rdip = NULL;
7477 	dipp = hdl->mtc_fdip ? &rdip : NULL;
7478 
7479 	switch (hdl->mtc_op) {
7480 	case MT_CONFIG_OP:
7481 		rv = devi_config_common(dip, flags, major);
7482 		break;
7483 	case MT_UNCONFIG_OP:
7484 		if (mcd->mtc_brn) {
7485 			struct brevq_node *brevq = NULL;
7486 			rv = devi_unconfig_common(dip, dipp, flags, major,
7487 			    &brevq);
7488 			mcd->mtc_brn->brn_child = brevq;
7489 		} else
7490 			rv = devi_unconfig_common(dip, dipp, flags, major,
7491 			    NULL);
7492 		break;
7493 	}
7494 
7495 	mutex_enter(&hdl->mtc_lock);
7496 #ifdef DEBUG
7497 	gethrestime(&end_time);
7498 	hdl->total_time += time_diff_in_msec(start_time, end_time);
7499 #endif /* DEBUG */
7500 
7501 	if ((rv != NDI_SUCCESS) && (hdl->mtc_error == 0)) {
7502 		hdl->mtc_error = rv;
7503 #ifdef	DEBUG
7504 		if ((ddidebug & DDI_DEBUG) && (major != DDI_MAJOR_T_NONE)) {
7505 			char	*path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7506 
7507 			(void) ddi_pathname(dip, path);
7508 			cmn_err(CE_NOTE, "mt_config_thread: "
7509 			    "op %d.%d.%x at %s failed %d",
7510 			    hdl->mtc_op, major, flags, path, rv);
7511 			kmem_free(path, MAXPATHLEN);
7512 		}
7513 #endif	/* DEBUG */
7514 	}
7515 
7516 	if (hdl->mtc_fdip && *hdl->mtc_fdip == NULL) {
7517 		*hdl->mtc_fdip = rdip;
7518 		rdip = NULL;
7519 	}
7520 
7521 	if (rdip) {
7522 		ASSERT(rv != NDI_SUCCESS);
7523 		ndi_rele_devi(rdip);
7524 	}
7525 
7526 	ndi_rele_devi(dip);
7527 
7528 	if (--hdl->mtc_thr_count == 0)
7529 		cv_broadcast(&hdl->mtc_cv);
7530 	mutex_exit(&hdl->mtc_lock);
7531 	kmem_free(mcd, sizeof (*mcd));
7532 }
7533 
7534 /*
7535  * Multi-threaded config/unconfig of child nexus
7536  */
7537 static void
7538 mt_config_children(struct mt_config_handle *hdl)
7539 {
7540 	dev_info_t		*pdip = hdl->mtc_pdip;
7541 	major_t			major = hdl->mtc_major;
7542 	dev_info_t		*dip;
7543 	int			circ;
7544 	struct brevq_node	*brn;
7545 	struct mt_config_data	*mcd_head = NULL;
7546 	struct mt_config_data	*mcd_tail = NULL;
7547 	struct mt_config_data	*mcd;
7548 #ifdef DEBUG
7549 	timestruc_t		end_time;
7550 
7551 	/* Update total_time in handle */
7552 	gethrestime(&end_time);
7553 	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
7554 #endif
7555 
7556 	ndi_devi_enter(pdip, &circ);
7557 	dip = ddi_get_child(pdip);
7558 	while (dip) {
7559 		if (hdl->mtc_op == MT_UNCONFIG_OP && hdl->mtc_brevqp &&
7560 		    !(DEVI_EVREMOVE(dip)) &&
7561 		    i_ddi_node_state(dip) >= DS_INITIALIZED) {
7562 			/*
7563 			 * Enqueue this dip's deviname.
7564 			 * No need to hold a lock while enqueuing since this
7565 			 * is the only thread doing the enqueue and no one
7566 			 * walks the queue while we are in multithreaded
7567 			 * unconfiguration.
7568 			 */
7569 			brn = brevq_enqueue(hdl->mtc_brevqp, dip, NULL);
7570 		} else
7571 			brn = NULL;
7572 
7573 		/*
7574 		 * Hold the child that we are processing so he does not get
7575 		 * removed. The corrisponding ndi_rele_devi() for children
7576 		 * that are not being skipped is done at the end of
7577 		 * mt_config_thread().
7578 		 */
7579 		ndi_hold_devi(dip);
7580 
7581 		/*
7582 		 * skip leaf nodes and (for configure) nodes not
7583 		 * fully attached.
7584 		 */
7585 		if (is_leaf_node(dip) ||
7586 		    (hdl->mtc_op == MT_CONFIG_OP &&
7587 		    i_ddi_node_state(dip) < DS_READY)) {
7588 			ndi_rele_devi(dip);
7589 			dip = ddi_get_next_sibling(dip);
7590 			continue;
7591 		}
7592 
7593 		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
7594 		mcd->mtc_dip = dip;
7595 		mcd->mtc_hdl = hdl;
7596 		mcd->mtc_brn = brn;
7597 
7598 		/*
7599 		 * Switch a 'driver' operation to an 'all' operation below a
7600 		 * node bound to the driver.
7601 		 */
7602 		if ((major == DDI_MAJOR_T_NONE) ||
7603 		    (major == ddi_driver_major(dip)))
7604 			mcd->mtc_major = DDI_MAJOR_T_NONE;
7605 		else
7606 			mcd->mtc_major = major;
7607 
7608 		/*
7609 		 * The unconfig-driver to unconfig-all conversion above
7610 		 * constitutes an autodetach for NDI_DETACH_DRIVER calls,
7611 		 * set NDI_AUTODETACH.
7612 		 */
7613 		mcd->mtc_flags = hdl->mtc_flags;
7614 		if ((mcd->mtc_flags & NDI_DETACH_DRIVER) &&
7615 		    (hdl->mtc_op == MT_UNCONFIG_OP) &&
7616 		    (major == ddi_driver_major(pdip)))
7617 			mcd->mtc_flags |= NDI_AUTODETACH;
7618 
7619 		mutex_enter(&hdl->mtc_lock);
7620 		hdl->mtc_thr_count++;
7621 		mutex_exit(&hdl->mtc_lock);
7622 
7623 		/*
7624 		 * Add to end of list to process after ndi_devi_exit to avoid
7625 		 * locking differences depending on value of mtc_off.
7626 		 */
7627 		mcd->mtc_next = NULL;
7628 		if (mcd_head == NULL)
7629 			mcd_head = mcd;
7630 		else
7631 			mcd_tail->mtc_next = mcd;
7632 		mcd_tail = mcd;
7633 
7634 		dip = ddi_get_next_sibling(dip);
7635 	}
7636 	ndi_devi_exit(pdip, circ);
7637 
7638 	/* go through the list of held children */
7639 	for (mcd = mcd_head; mcd; mcd = mcd_head) {
7640 		mcd_head = mcd->mtc_next;
7641 		if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
7642 			mt_config_thread(mcd);
7643 		else
7644 			(void) thread_create(NULL, 0, mt_config_thread, mcd,
7645 			    0, &p0, TS_RUN, minclsyspri);
7646 	}
7647 }
7648 
7649 static void
7650 mt_config_driver(struct mt_config_handle *hdl)
7651 {
7652 	major_t			par_major = hdl->mtc_parmajor;
7653 	major_t			major = hdl->mtc_major;
7654 	struct devnames		*dnp = &devnamesp[par_major];
7655 	dev_info_t		*dip;
7656 	struct mt_config_data	*mcd_head = NULL;
7657 	struct mt_config_data	*mcd_tail = NULL;
7658 	struct mt_config_data	*mcd;
7659 #ifdef DEBUG
7660 	timestruc_t		end_time;
7661 
7662 	/* Update total_time in handle */
7663 	gethrestime(&end_time);
7664 	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
7665 #endif
7666 	ASSERT(par_major != DDI_MAJOR_T_NONE);
7667 	ASSERT(major != DDI_MAJOR_T_NONE);
7668 
7669 	LOCK_DEV_OPS(&dnp->dn_lock);
7670 	dip = devnamesp[par_major].dn_head;
7671 	while (dip) {
7672 		/*
7673 		 * Hold the child that we are processing so he does not get
7674 		 * removed. The corrisponding ndi_rele_devi() for children
7675 		 * that are not being skipped is done at the end of
7676 		 * mt_config_thread().
7677 		 */
7678 		ndi_hold_devi(dip);
7679 
7680 		/* skip leaf nodes and nodes not fully attached */
7681 		if (!i_ddi_devi_attached(dip) || is_leaf_node(dip)) {
7682 			ndi_rele_devi(dip);
7683 			dip = ddi_get_next(dip);
7684 			continue;
7685 		}
7686 
7687 		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
7688 		mcd->mtc_dip = dip;
7689 		mcd->mtc_hdl = hdl;
7690 		mcd->mtc_major = major;
7691 		mcd->mtc_flags = hdl->mtc_flags;
7692 
7693 		mutex_enter(&hdl->mtc_lock);
7694 		hdl->mtc_thr_count++;
7695 		mutex_exit(&hdl->mtc_lock);
7696 
7697 		/*
7698 		 * Add to end of list to process after UNLOCK_DEV_OPS to avoid
7699 		 * locking differences depending on value of mtc_off.
7700 		 */
7701 		mcd->mtc_next = NULL;
7702 		if (mcd_head == NULL)
7703 			mcd_head = mcd;
7704 		else
7705 			mcd_tail->mtc_next = mcd;
7706 		mcd_tail = mcd;
7707 
7708 		dip = ddi_get_next(dip);
7709 	}
7710 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7711 
7712 	/* go through the list of held children */
7713 	for (mcd = mcd_head; mcd; mcd = mcd_head) {
7714 		mcd_head = mcd->mtc_next;
7715 		if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
7716 			mt_config_thread(mcd);
7717 		else
7718 			(void) thread_create(NULL, 0, mt_config_thread, mcd,
7719 			    0, &p0, TS_RUN, minclsyspri);
7720 	}
7721 }
7722 
7723 /*
7724  * Given the nodeid for a persistent (PROM or SID) node, return
7725  * the corresponding devinfo node
7726  * NOTE: This function will return NULL for .conf nodeids.
7727  */
7728 dev_info_t *
7729 e_ddi_nodeid_to_dip(pnode_t nodeid)
7730 {
7731 	dev_info_t		*dip = NULL;
7732 	struct devi_nodeid	*prev, *elem;
7733 
7734 	mutex_enter(&devimap->dno_lock);
7735 
7736 	prev = NULL;
7737 	for (elem = devimap->dno_head; elem; elem = elem->next) {
7738 		if (elem->nodeid == nodeid) {
7739 			ndi_hold_devi(elem->dip);
7740 			dip = elem->dip;
7741 			break;
7742 		}
7743 		prev = elem;
7744 	}
7745 
7746 	/*
7747 	 * Move to head for faster lookup next time
7748 	 */
7749 	if (elem && prev) {
7750 		prev->next = elem->next;
7751 		elem->next = devimap->dno_head;
7752 		devimap->dno_head = elem;
7753 	}
7754 
7755 	mutex_exit(&devimap->dno_lock);
7756 	return (dip);
7757 }
7758 
7759 static void
7760 free_cache_task(void *arg)
7761 {
7762 	ASSERT(arg == NULL);
7763 
7764 	mutex_enter(&di_cache.cache_lock);
7765 
7766 	/*
7767 	 * The cache can be invalidated without holding the lock
7768 	 * but it can be made valid again only while the lock is held.
7769 	 * So if the cache is invalid when the lock is held, it will
7770 	 * stay invalid until lock is released.
7771 	 */
7772 	if (!di_cache.cache_valid)
7773 		i_ddi_di_cache_free(&di_cache);
7774 
7775 	mutex_exit(&di_cache.cache_lock);
7776 
7777 	if (di_cache_debug)
7778 		cmn_err(CE_NOTE, "system_taskq: di_cache freed");
7779 }
7780 
7781 extern int modrootloaded;
7782 
7783 void
7784 i_ddi_di_cache_free(struct di_cache *cache)
7785 {
7786 	int	error;
7787 	extern int sys_shutdown;
7788 
7789 	ASSERT(mutex_owned(&cache->cache_lock));
7790 
7791 	if (cache->cache_size) {
7792 		ASSERT(cache->cache_size > 0);
7793 		ASSERT(cache->cache_data);
7794 
7795 		kmem_free(cache->cache_data, cache->cache_size);
7796 		cache->cache_data = NULL;
7797 		cache->cache_size = 0;
7798 
7799 		if (di_cache_debug)
7800 			cmn_err(CE_NOTE, "i_ddi_di_cache_free: freed cachemem");
7801 	} else {
7802 		ASSERT(cache->cache_data == NULL);
7803 		if (di_cache_debug)
7804 			cmn_err(CE_NOTE, "i_ddi_di_cache_free: NULL cache");
7805 	}
7806 
7807 	if (!modrootloaded || rootvp == NULL ||
7808 	    vn_is_readonly(rootvp) || sys_shutdown) {
7809 		if (di_cache_debug) {
7810 			cmn_err(CE_WARN, "/ not mounted/RDONLY. Skip unlink");
7811 		}
7812 		return;
7813 	}
7814 
7815 	error = vn_remove(DI_CACHE_FILE, UIO_SYSSPACE, RMFILE);
7816 	if (di_cache_debug && error && error != ENOENT) {
7817 		cmn_err(CE_WARN, "%s: unlink failed: %d", DI_CACHE_FILE, error);
7818 	} else if (di_cache_debug && !error) {
7819 		cmn_err(CE_NOTE, "i_ddi_di_cache_free: unlinked cache file");
7820 	}
7821 }
7822 
7823 void
7824 i_ddi_di_cache_invalidate()
7825 {
7826 	int	cache_valid;
7827 
7828 	if (!modrootloaded || !i_ddi_io_initialized()) {
7829 		if (di_cache_debug)
7830 			cmn_err(CE_NOTE, "I/O not inited. Skipping invalidate");
7831 		return;
7832 	}
7833 
7834 	/* Increment devtree generation number. */
7835 	atomic_inc_ulong(&devtree_gen);
7836 
7837 	/* Invalidate the in-core cache and dispatch free on valid->invalid */
7838 	cache_valid = atomic_swap_uint(&di_cache.cache_valid, 0);
7839 	if (cache_valid) {
7840 		/*
7841 		 * This is an optimization to start cleaning up a cached
7842 		 * snapshot early.  For this reason, it is OK for
7843 		 * taskq_dispatach to fail (and it is OK to not track calling
7844 		 * context relative to sleep, and assume NOSLEEP).
7845 		 */
7846 		(void) taskq_dispatch(system_taskq, free_cache_task, NULL,
7847 		    TQ_NOSLEEP);
7848 	}
7849 
7850 	if (di_cache_debug) {
7851 		cmn_err(CE_NOTE, "invalidation");
7852 	}
7853 }
7854 
7855 
7856 static void
7857 i_bind_vhci_node(dev_info_t *dip)
7858 {
7859 	DEVI(dip)->devi_major = ddi_name_to_major(ddi_node_name(dip));
7860 	i_ddi_set_node_state(dip, DS_BOUND);
7861 }
7862 
7863 static char vhci_node_addr[2];
7864 
7865 static int
7866 i_init_vhci_node(dev_info_t *dip)
7867 {
7868 	add_global_props(dip);
7869 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
7870 	if (DEVI(dip)->devi_ops == NULL)
7871 		return (-1);
7872 
7873 	DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
7874 	e_ddi_keep_instance(dip);
7875 	vhci_node_addr[0]	= '\0';
7876 	ddi_set_name_addr(dip, vhci_node_addr);
7877 	i_ddi_set_node_state(dip, DS_INITIALIZED);
7878 	return (0);
7879 }
7880 
7881 static void
7882 i_link_vhci_node(dev_info_t *dip)
7883 {
7884 	ASSERT(MUTEX_HELD(&global_vhci_lock));
7885 
7886 	/*
7887 	 * scsi_vhci should be kept left most of the device tree.
7888 	 */
7889 	if (scsi_vhci_dip) {
7890 		DEVI(dip)->devi_sibling = DEVI(scsi_vhci_dip)->devi_sibling;
7891 		DEVI(scsi_vhci_dip)->devi_sibling = DEVI(dip);
7892 	} else {
7893 		DEVI(dip)->devi_sibling = DEVI(top_devinfo)->devi_child;
7894 		DEVI(top_devinfo)->devi_child = DEVI(dip);
7895 	}
7896 }
7897 
7898 
7899 /*
7900  * This a special routine to enumerate vhci node (child of rootnex
7901  * node) without holding the ndi_devi_enter() lock. The device node
7902  * is allocated, initialized and brought into DS_READY state before
7903  * inserting into the device tree. The VHCI node is handcrafted
7904  * here to bring the node to DS_READY, similar to rootnex node.
7905  *
7906  * The global_vhci_lock protects linking the node into the device
7907  * as same lock is held before linking/unlinking any direct child
7908  * of rootnex children.
7909  *
7910  * This routine is a workaround to handle a possible deadlock
7911  * that occurs while trying to enumerate node in a different sub-tree
7912  * during _init/_attach entry points.
7913  */
7914 /*ARGSUSED*/
7915 dev_info_t *
7916 ndi_devi_config_vhci(char *drvname, int flags)
7917 {
7918 	struct devnames		*dnp;
7919 	dev_info_t		*dip;
7920 	major_t			major = ddi_name_to_major(drvname);
7921 
7922 	if (major == -1)
7923 		return (NULL);
7924 
7925 	/* Make sure we create the VHCI node only once */
7926 	dnp = &devnamesp[major];
7927 	LOCK_DEV_OPS(&dnp->dn_lock);
7928 	if (dnp->dn_head) {
7929 		dip = dnp->dn_head;
7930 		UNLOCK_DEV_OPS(&dnp->dn_lock);
7931 		return (dip);
7932 	}
7933 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7934 
7935 	/* Allocate the VHCI node */
7936 	ndi_devi_alloc_sleep(top_devinfo, drvname, DEVI_SID_NODEID, &dip);
7937 	ndi_hold_devi(dip);
7938 
7939 	/* Mark the node as VHCI */
7940 	DEVI(dip)->devi_node_attributes |= DDI_VHCI_NODE;
7941 
7942 	i_ddi_add_devimap(dip);
7943 	i_bind_vhci_node(dip);
7944 	if (i_init_vhci_node(dip) == -1) {
7945 		ndi_rele_devi(dip);
7946 		(void) ndi_devi_free(dip);
7947 		return (NULL);
7948 	}
7949 
7950 	mutex_enter(&(DEVI(dip)->devi_lock));
7951 	DEVI_SET_ATTACHING(dip);
7952 	mutex_exit(&(DEVI(dip)->devi_lock));
7953 
7954 	if (devi_attach(dip, DDI_ATTACH) != DDI_SUCCESS) {
7955 		cmn_err(CE_CONT, "Could not attach %s driver", drvname);
7956 		e_ddi_free_instance(dip, vhci_node_addr);
7957 		ndi_rele_devi(dip);
7958 		(void) ndi_devi_free(dip);
7959 		return (NULL);
7960 	}
7961 	mutex_enter(&(DEVI(dip)->devi_lock));
7962 	DEVI_CLR_ATTACHING(dip);
7963 	mutex_exit(&(DEVI(dip)->devi_lock));
7964 
7965 	mutex_enter(&global_vhci_lock);
7966 	i_link_vhci_node(dip);
7967 	mutex_exit(&global_vhci_lock);
7968 	i_ddi_set_node_state(dip, DS_READY);
7969 
7970 	LOCK_DEV_OPS(&dnp->dn_lock);
7971 	dnp->dn_flags |= DN_DRIVER_HELD;
7972 	dnp->dn_head = dip;
7973 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7974 
7975 	i_ndi_devi_report_status_change(dip, NULL);
7976 
7977 	return (dip);
7978 }
7979 
7980 /*
7981  * Maintain DEVI_DEVICE_REMOVED hotplug devi_state for remove/reinsert hotplug
7982  * of open devices. Currently, because of tight coupling between the devfs file
7983  * system and the Solaris device tree, a driver can't always make the device
7984  * tree state (esp devi_node_state) match device hardware hotplug state. Until
7985  * resolved, to overcome this deficiency we use the following interfaces that
7986  * maintain the DEVI_DEVICE_REMOVED devi_state status bit.  These interface
7987  * report current state, and drive operation (like events and cache
7988  * invalidation) when a driver changes remove/insert state of an open device.
7989  *
7990  * The ndi_devi_device_isremoved() returns 1 if the device is currently removed.
7991  *
7992  * The ndi_devi_device_remove() interface declares the device as removed, and
7993  * returns 1 if there was a state change associated with this declaration.
7994  *
7995  * The ndi_devi_device_insert() declares the device as inserted, and returns 1
7996  * if there was a state change associated with this declaration.
7997  */
7998 int
7999 ndi_devi_device_isremoved(dev_info_t *dip)
8000 {
8001 	return (DEVI_IS_DEVICE_REMOVED(dip));
8002 }
8003 
8004 int
8005 ndi_devi_device_remove(dev_info_t *dip)
8006 {
8007 	ASSERT(dip && ddi_get_parent(dip) &&
8008 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
8009 
8010 	/* Return if already marked removed. */
8011 	if (ndi_devi_device_isremoved(dip))
8012 		return (0);
8013 
8014 	/* Mark the device as having been physically removed. */
8015 	mutex_enter(&(DEVI(dip)->devi_lock));
8016 	ndi_devi_set_hidden(dip);	/* invisible: lookup/snapshot */
8017 	DEVI_SET_DEVICE_REMOVED(dip);
8018 	DEVI_SET_EVREMOVE(dip);		/* this clears EVADD too */
8019 	mutex_exit(&(DEVI(dip)->devi_lock));
8020 
8021 	/* report remove (as 'removed') */
8022 	i_ndi_devi_report_status_change(dip, NULL);
8023 
8024 	/*
8025 	 * Invalidate the cache to ensure accurate
8026 	 * (di_state() & DI_DEVICE_REMOVED).
8027 	 */
8028 	i_ddi_di_cache_invalidate();
8029 
8030 	/*
8031 	 * Generate sysevent for those interested in removal (either directly
8032 	 * via EC_DEVFS or indirectly via devfsadmd generated EC_DEV).
8033 	 */
8034 	i_ddi_log_devfs_device_remove(dip);
8035 
8036 	return (1);		/* DEVICE_REMOVED state changed */
8037 }
8038 
8039 int
8040 ndi_devi_device_insert(dev_info_t *dip)
8041 {
8042 	ASSERT(dip && ddi_get_parent(dip) &&
8043 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
8044 
8045 	/* Return if not marked removed. */
8046 	if (!ndi_devi_device_isremoved(dip))
8047 		return (0);
8048 
8049 	/* Mark the device as having been physically reinserted. */
8050 	mutex_enter(&(DEVI(dip)->devi_lock));
8051 	ndi_devi_clr_hidden(dip);	/* visible: lookup/snapshot */
8052 	DEVI_SET_DEVICE_REINSERTED(dip);
8053 	DEVI_SET_EVADD(dip);		/* this clears EVREMOVE too */
8054 	mutex_exit(&(DEVI(dip)->devi_lock));
8055 
8056 	/* report insert (as 'online') */
8057 	i_ndi_devi_report_status_change(dip, NULL);
8058 
8059 	/*
8060 	 * Invalidate the cache to ensure accurate
8061 	 * (di_state() & DI_DEVICE_REMOVED).
8062 	 */
8063 	i_ddi_di_cache_invalidate();
8064 
8065 	/*
8066 	 * Generate sysevent for those interested in removal (either directly
8067 	 * via EC_DEVFS or indirectly via devfsadmd generated EC_DEV).
8068 	 */
8069 	i_ddi_log_devfs_device_insert(dip);
8070 
8071 	return (1);		/* DEVICE_REMOVED state changed */
8072 }
8073 
8074 /*
8075  * ibt_hw_is_present() returns 0 when there is no IB hardware actively
8076  * running.  This is primarily useful for modules like rpcmod which
8077  * needs a quick check to decide whether or not it should try to use
8078  * InfiniBand
8079  */
8080 int ib_hw_status = 0;
8081 int
8082 ibt_hw_is_present()
8083 {
8084 	return (ib_hw_status);
8085 }
8086 
8087 /*
8088  * ASSERT that constraint flag is not set and then set the "retire attempt"
8089  * flag.
8090  */
8091 int
8092 e_ddi_mark_retiring(dev_info_t *dip, void *arg)
8093 {
8094 	char	**cons_array = (char **)arg;
8095 	char	*path;
8096 	int	constraint;
8097 	int	i;
8098 
8099 	constraint = 0;
8100 	if (cons_array) {
8101 		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8102 		(void) ddi_pathname(dip, path);
8103 		for (i = 0; cons_array[i] != NULL; i++) {
8104 			if (strcmp(path, cons_array[i]) == 0) {
8105 				constraint = 1;
8106 				break;
8107 			}
8108 		}
8109 		kmem_free(path, MAXPATHLEN);
8110 	}
8111 
8112 	mutex_enter(&DEVI(dip)->devi_lock);
8113 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
8114 	DEVI(dip)->devi_flags |= DEVI_RETIRING;
8115 	if (constraint)
8116 		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
8117 	mutex_exit(&DEVI(dip)->devi_lock);
8118 
8119 	RIO_VERBOSE((CE_NOTE, "marked dip as undergoing retire process dip=%p",
8120 	    (void *)dip));
8121 
8122 	if (constraint)
8123 		RIO_DEBUG((CE_NOTE, "marked dip as constrained, dip=%p",
8124 		    (void *)dip));
8125 
8126 	if (MDI_PHCI(dip))
8127 		mdi_phci_mark_retiring(dip, cons_array);
8128 
8129 	return (DDI_WALK_CONTINUE);
8130 }
8131 
8132 static void
8133 free_array(char **cons_array)
8134 {
8135 	int	i;
8136 
8137 	if (cons_array == NULL)
8138 		return;
8139 
8140 	for (i = 0; cons_array[i] != NULL; i++) {
8141 		kmem_free(cons_array[i], strlen(cons_array[i]) + 1);
8142 	}
8143 	kmem_free(cons_array, (i+1) * sizeof (char *));
8144 }
8145 
8146 /*
8147  * Walk *every* node in subtree and check if it blocks, allows or has no
8148  * comment on a proposed retire.
8149  */
8150 int
8151 e_ddi_retire_notify(dev_info_t *dip, void *arg)
8152 {
8153 	int	*constraint = (int *)arg;
8154 
8155 	RIO_DEBUG((CE_NOTE, "retire notify: dip = %p", (void *)dip));
8156 
8157 	(void) e_ddi_offline_notify(dip);
8158 
8159 	mutex_enter(&(DEVI(dip)->devi_lock));
8160 	if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) {
8161 		RIO_DEBUG((CE_WARN, "retire notify: dip in retire "
8162 		    "subtree is not marked: dip = %p", (void *)dip));
8163 		*constraint = 0;
8164 	} else if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) {
8165 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
8166 		RIO_DEBUG((CE_NOTE, "retire notify: BLOCKED: dip = %p",
8167 		    (void *)dip));
8168 		*constraint = 0;
8169 	} else if (!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT)) {
8170 		RIO_DEBUG((CE_NOTE, "retire notify: NO CONSTRAINT: "
8171 		    "dip = %p", (void *)dip));
8172 		*constraint = 0;
8173 	} else {
8174 		RIO_DEBUG((CE_NOTE, "retire notify: CONSTRAINT set: "
8175 		    "dip = %p", (void *)dip));
8176 	}
8177 	mutex_exit(&DEVI(dip)->devi_lock);
8178 
8179 	if (MDI_PHCI(dip))
8180 		mdi_phci_retire_notify(dip, constraint);
8181 
8182 	return (DDI_WALK_CONTINUE);
8183 }
8184 
8185 int
8186 e_ddi_retire_finalize(dev_info_t *dip, void *arg)
8187 {
8188 	int constraint = *(int *)arg;
8189 	int finalize;
8190 	int phci_only;
8191 
8192 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
8193 
8194 	mutex_enter(&DEVI(dip)->devi_lock);
8195 	if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) {
8196 		RIO_DEBUG((CE_WARN,
8197 		    "retire: unmarked dip(%p) in retire subtree",
8198 		    (void *)dip));
8199 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRED));
8200 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
8201 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
8202 		mutex_exit(&DEVI(dip)->devi_lock);
8203 		return (DDI_WALK_CONTINUE);
8204 	}
8205 
8206 	/*
8207 	 * retire the device if constraints have been applied
8208 	 * or if the device is not in use
8209 	 */
8210 	finalize = 0;
8211 	if (constraint) {
8212 		ASSERT(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT);
8213 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
8214 		DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
8215 		DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
8216 		DEVI(dip)->devi_flags |= DEVI_RETIRED;
8217 		mutex_exit(&DEVI(dip)->devi_lock);
8218 		(void) spec_fence_snode(dip, NULL);
8219 		RIO_DEBUG((CE_NOTE, "Fenced off: dip = %p", (void *)dip));
8220 		e_ddi_offline_finalize(dip, DDI_SUCCESS);
8221 	} else {
8222 		if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) {
8223 			ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
8224 			DEVI(dip)->devi_flags &= ~DEVI_R_BLOCKED;
8225 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
8226 			/* we have already finalized during notify */
8227 		} else if (DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT) {
8228 			DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
8229 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
8230 			finalize = 1;
8231 		} else {
8232 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
8233 			/*
8234 			 * even if no contracts, need to call finalize
8235 			 * to clear the contract barrier on the dip
8236 			 */
8237 			finalize = 1;
8238 		}
8239 		mutex_exit(&DEVI(dip)->devi_lock);
8240 		RIO_DEBUG((CE_NOTE, "finalize: NOT retired: dip = %p",
8241 		    (void *)dip));
8242 		if (finalize)
8243 			e_ddi_offline_finalize(dip, DDI_FAILURE);
8244 	}
8245 
8246 	/*
8247 	 * phci_only variable indicates no client checking, just
8248 	 * offline the PHCI. We set that to 0 to enable client
8249 	 * checking
8250 	 */
8251 	phci_only = 0;
8252 	if (MDI_PHCI(dip))
8253 		mdi_phci_retire_finalize(dip, phci_only);
8254 
8255 	return (DDI_WALK_CONTINUE);
8256 }
8257 
8258 /*
8259  * Returns
8260  *	DDI_SUCCESS if constraints allow retire
8261  *	DDI_FAILURE if constraints don't allow retire.
8262  * cons_array is a NULL terminated array of node paths for
8263  * which constraints have already been applied.
8264  */
8265 int
8266 e_ddi_retire_device(char *path, char **cons_array)
8267 {
8268 	dev_info_t	*dip;
8269 	dev_info_t	*pdip;
8270 	int		circ;
8271 	int		circ2;
8272 	int		constraint;
8273 	char		*devnm;
8274 
8275 	/*
8276 	 * First, lookup the device
8277 	 */
8278 	dip = e_ddi_hold_devi_by_path(path, 0);
8279 	if (dip == NULL) {
8280 		/*
8281 		 * device does not exist. This device cannot be
8282 		 * a critical device since it is not in use. Thus
8283 		 * this device is always retireable. Return DDI_SUCCESS
8284 		 * to indicate this. If this device is ever
8285 		 * instantiated, I/O framework will consult the
8286 		 * the persistent retire store, mark it as
8287 		 * retired and fence it off.
8288 		 */
8289 		RIO_DEBUG((CE_NOTE, "Retire device: device doesn't exist."
8290 		    " NOP. Just returning SUCCESS. path=%s", path));
8291 		free_array(cons_array);
8292 		return (DDI_SUCCESS);
8293 	}
8294 
8295 	RIO_DEBUG((CE_NOTE, "Retire device: found dip = %p.", (void *)dip));
8296 
8297 	pdip = ddi_get_parent(dip);
8298 	ndi_hold_devi(pdip);
8299 
8300 	/*
8301 	 * Run devfs_clean() in case dip has no constraints and is
8302 	 * not in use, so is retireable but there are dv_nodes holding
8303 	 * ref-count on the dip. Note that devfs_clean() always returns
8304 	 * success.
8305 	 */
8306 	devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
8307 	(void) ddi_deviname(dip, devnm);
8308 	(void) devfs_clean(pdip, devnm + 1, DV_CLEAN_FORCE);
8309 	kmem_free(devnm, MAXNAMELEN + 1);
8310 
8311 	ndi_devi_enter(pdip, &circ);
8312 
8313 	/* release hold from e_ddi_hold_devi_by_path */
8314 	ndi_rele_devi(dip);
8315 
8316 	/*
8317 	 * If it cannot make a determination, is_leaf_node() assumes
8318 	 * dip is a nexus.
8319 	 */
8320 	(void) e_ddi_mark_retiring(dip, cons_array);
8321 	if (!is_leaf_node(dip)) {
8322 		ndi_devi_enter(dip, &circ2);
8323 		ddi_walk_devs(ddi_get_child(dip), e_ddi_mark_retiring,
8324 		    cons_array);
8325 		ndi_devi_exit(dip, circ2);
8326 	}
8327 	free_array(cons_array);
8328 
8329 	/*
8330 	 * apply constraints
8331 	 */
8332 	RIO_DEBUG((CE_NOTE, "retire: subtree retire notify: path = %s", path));
8333 
8334 	constraint = 1;	/* assume constraints allow retire */
8335 	(void) e_ddi_retire_notify(dip, &constraint);
8336 	if (!is_leaf_node(dip)) {
8337 		ndi_devi_enter(dip, &circ2);
8338 		ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_notify,
8339 		    &constraint);
8340 		ndi_devi_exit(dip, circ2);
8341 	}
8342 
8343 	/*
8344 	 * Now finalize the retire
8345 	 */
8346 	(void) e_ddi_retire_finalize(dip, &constraint);
8347 	if (!is_leaf_node(dip)) {
8348 		ndi_devi_enter(dip, &circ2);
8349 		ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_finalize,
8350 		    &constraint);
8351 		ndi_devi_exit(dip, circ2);
8352 	}
8353 
8354 	if (!constraint) {
8355 		RIO_DEBUG((CE_WARN, "retire failed: path = %s", path));
8356 	} else {
8357 		RIO_DEBUG((CE_NOTE, "retire succeeded: path = %s", path));
8358 	}
8359 
8360 	ndi_devi_exit(pdip, circ);
8361 	ndi_rele_devi(pdip);
8362 	return (constraint ? DDI_SUCCESS : DDI_FAILURE);
8363 }
8364 
8365 static int
8366 unmark_and_unfence(dev_info_t *dip, void *arg)
8367 {
8368 	char	*path = (char *)arg;
8369 
8370 	ASSERT(path);
8371 
8372 	(void) ddi_pathname(dip, path);
8373 
8374 	mutex_enter(&DEVI(dip)->devi_lock);
8375 	DEVI(dip)->devi_flags &= ~DEVI_RETIRED;
8376 	DEVI_SET_DEVICE_ONLINE(dip);
8377 	mutex_exit(&DEVI(dip)->devi_lock);
8378 
8379 	RIO_VERBOSE((CE_NOTE, "Cleared RETIRED flag: dip=%p, path=%s",
8380 	    (void *)dip, path));
8381 
8382 	(void) spec_unfence_snode(dip);
8383 	RIO_DEBUG((CE_NOTE, "Unfenced device: %s", path));
8384 
8385 	if (MDI_PHCI(dip))
8386 		mdi_phci_unretire(dip);
8387 
8388 	return (DDI_WALK_CONTINUE);
8389 }
8390 
8391 struct find_dip {
8392 	char	*fd_buf;
8393 	char	*fd_path;
8394 	dev_info_t *fd_dip;
8395 };
8396 
8397 static int
8398 find_dip_fcn(dev_info_t *dip, void *arg)
8399 {
8400 	struct find_dip *findp = (struct find_dip *)arg;
8401 
8402 	(void) ddi_pathname(dip, findp->fd_buf);
8403 
8404 	if (strcmp(findp->fd_path, findp->fd_buf) != 0)
8405 		return (DDI_WALK_CONTINUE);
8406 
8407 	ndi_hold_devi(dip);
8408 	findp->fd_dip = dip;
8409 
8410 	return (DDI_WALK_TERMINATE);
8411 }
8412 
8413 int
8414 e_ddi_unretire_device(char *path)
8415 {
8416 	int		circ;
8417 	int		circ2;
8418 	char		*path2;
8419 	dev_info_t	*pdip;
8420 	dev_info_t	*dip;
8421 	struct find_dip	 find_dip;
8422 
8423 	ASSERT(path);
8424 	ASSERT(*path == '/');
8425 
8426 	if (strcmp(path, "/") == 0) {
8427 		cmn_err(CE_WARN, "Root node cannot be retired. Skipping "
8428 		    "device unretire: %s", path);
8429 		return (0);
8430 	}
8431 
8432 	/*
8433 	 * We can't lookup the dip (corresponding to path) via
8434 	 * e_ddi_hold_devi_by_path() because the dip may be offline
8435 	 * and may not attach. Use ddi_walk_devs() instead;
8436 	 */
8437 	find_dip.fd_buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8438 	find_dip.fd_path = path;
8439 	find_dip.fd_dip = NULL;
8440 
8441 	pdip = ddi_root_node();
8442 
8443 	ndi_devi_enter(pdip, &circ);
8444 	ddi_walk_devs(ddi_get_child(pdip), find_dip_fcn, &find_dip);
8445 	ndi_devi_exit(pdip, circ);
8446 
8447 	kmem_free(find_dip.fd_buf, MAXPATHLEN);
8448 
8449 	if (find_dip.fd_dip == NULL) {
8450 		cmn_err(CE_WARN, "Device not found in device tree. Skipping "
8451 		    "device unretire: %s", path);
8452 		return (0);
8453 	}
8454 
8455 	dip = find_dip.fd_dip;
8456 
8457 	pdip = ddi_get_parent(dip);
8458 
8459 	ndi_hold_devi(pdip);
8460 
8461 	ndi_devi_enter(pdip, &circ);
8462 
8463 	path2 = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8464 
8465 	(void) unmark_and_unfence(dip, path2);
8466 	if (!is_leaf_node(dip)) {
8467 		ndi_devi_enter(dip, &circ2);
8468 		ddi_walk_devs(ddi_get_child(dip), unmark_and_unfence, path2);
8469 		ndi_devi_exit(dip, circ2);
8470 	}
8471 
8472 	kmem_free(path2, MAXPATHLEN);
8473 
8474 	/* release hold from find_dip_fcn() */
8475 	ndi_rele_devi(dip);
8476 
8477 	ndi_devi_exit(pdip, circ);
8478 
8479 	ndi_rele_devi(pdip);
8480 
8481 	return (0);
8482 }
8483 
8484 /*
8485  * Called before attach on a dip that has been retired.
8486  */
8487 static int
8488 mark_and_fence(dev_info_t *dip, void *arg)
8489 {
8490 	char	*fencepath = (char *)arg;
8491 
8492 	/*
8493 	 * We have already decided to retire this device. The various
8494 	 * constraint checking should not be set.
8495 	 * NOTE that the retire flag may already be set due to
8496 	 * fenced -> detach -> fenced transitions.
8497 	 */
8498 	mutex_enter(&DEVI(dip)->devi_lock);
8499 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
8500 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
8501 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRING));
8502 	DEVI(dip)->devi_flags |= DEVI_RETIRED;
8503 	mutex_exit(&DEVI(dip)->devi_lock);
8504 	RIO_VERBOSE((CE_NOTE, "marked as RETIRED dip=%p", (void *)dip));
8505 
8506 	if (fencepath) {
8507 		(void) spec_fence_snode(dip, NULL);
8508 		RIO_DEBUG((CE_NOTE, "Fenced: %s",
8509 		    ddi_pathname(dip, fencepath)));
8510 	}
8511 
8512 	return (DDI_WALK_CONTINUE);
8513 }
8514 
8515 /*
8516  * Checks the retire database and:
8517  *
8518  * - if device is present in the retire database, marks the device retired
8519  *   and fences it off.
8520  * - if device is not in retire database, allows the device to attach normally
8521  *
8522  * To be called only by framework attach code on first attach attempt.
8523  *
8524  */
8525 static void
8526 i_ddi_check_retire(dev_info_t *dip)
8527 {
8528 	char		*path;
8529 	dev_info_t	*pdip;
8530 	int		circ;
8531 	int		phci_only;
8532 
8533 	pdip = ddi_get_parent(dip);
8534 
8535 	/*
8536 	 * Root dip is treated special and doesn't take this code path.
8537 	 * Also root can never be retired.
8538 	 */
8539 	ASSERT(pdip);
8540 	ASSERT(DEVI_BUSY_OWNED(pdip));
8541 	ASSERT(i_ddi_node_state(dip) < DS_ATTACHED);
8542 
8543 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8544 
8545 	(void) ddi_pathname(dip, path);
8546 
8547 	RIO_VERBOSE((CE_NOTE, "Checking if dip should attach: dip=%p, path=%s",
8548 	    (void *)dip, path));
8549 
8550 	/*
8551 	 * Check if this device is in the "retired" store i.e.	should
8552 	 * be retired. If not, we have nothing to do.
8553 	 */
8554 	if (e_ddi_device_retired(path) == 0) {
8555 		RIO_VERBOSE((CE_NOTE, "device is NOT retired: path=%s", path));
8556 		kmem_free(path, MAXPATHLEN);
8557 		return;
8558 	}
8559 
8560 	RIO_DEBUG((CE_NOTE, "attach: device is retired: path=%s", path));
8561 
8562 	/*
8563 	 * Mark dips and fence off snodes (if any)
8564 	 */
8565 	RIO_DEBUG((CE_NOTE, "attach: Mark and fence subtree: path=%s", path));
8566 	(void) mark_and_fence(dip, path);
8567 	if (!is_leaf_node(dip)) {
8568 		ndi_devi_enter(dip, &circ);
8569 		ddi_walk_devs(ddi_get_child(dip), mark_and_fence, path);
8570 		ndi_devi_exit(dip, circ);
8571 	}
8572 
8573 	kmem_free(path, MAXPATHLEN);
8574 
8575 	/*
8576 	 * We don't want to check the client. We just want to
8577 	 * offline the PHCI
8578 	 */
8579 	phci_only = 1;
8580 	if (MDI_PHCI(dip))
8581 		mdi_phci_retire_finalize(dip, phci_only);
8582 }
8583