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