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