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