xref: /titanic_41/usr/src/uts/common/os/devcfg.c (revision f9722deaa8da9978617bd4b5c9130f219e127193)
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 #define	PCI_EX_CLASS	"pciexclass"
3621 #define	PCI_EX		"pciex"
3622 #define	PCI_CLASS	"pciclass"
3623 #define	PCI		"pci"
3624 
3625 int
3626 ddi_is_pci_dip(dev_info_t *dip)
3627 {
3628 	char	*prop = NULL;
3629 
3630 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
3631 	    "compatible", &prop) == DDI_PROP_SUCCESS) {
3632 		ASSERT(prop);
3633 		if (strncmp(prop, PCI_EX_CLASS, sizeof (PCI_EX_CLASS) - 1)
3634 		    == 0 ||
3635 		    strncmp(prop, PCI_EX, sizeof (PCI_EX)- 1)
3636 		    == 0 ||
3637 		    strncmp(prop, PCI_CLASS, sizeof (PCI_CLASS) - 1)
3638 		    == 0 ||
3639 		    strncmp(prop, PCI, sizeof (PCI) - 1)
3640 		    == 0) {
3641 			ddi_prop_free(prop);
3642 			return (1);
3643 		}
3644 	}
3645 
3646 	if (prop != NULL) {
3647 		ddi_prop_free(prop);
3648 	}
3649 
3650 	return (0);
3651 }
3652 
3653 /*
3654  * Given the pathname of a device, fill in the dev_info_t value and/or the
3655  * dev_t value and/or the spectype, depending on which parameters are non-NULL.
3656  * If there is an error, this function returns -1.
3657  *
3658  * NOTE: If this function returns the dev_info_t structure, then it
3659  * does so with a hold on the devi. Caller should ensure that they get
3660  * decremented via ddi_release_devi() or ndi_rele_devi();
3661  *
3662  * This function can be invoked in the boot case for a pathname without
3663  * device argument (:xxxx), traditionally treated as a minor name.
3664  * In this case, we do the following
3665  * (1) search the minor node of type DDM_DEFAULT.
3666  * (2) if no DDM_DEFAULT minor exists, then the first non-alias minor is chosen.
3667  * (3) if neither exists, a dev_t is faked with minor number = instance.
3668  * As of S9 FCS, no instance of #1 exists. #2 is used by several platforms
3669  * to default the boot partition to :a possibly by other OBP definitions.
3670  * #3 is used for booting off network interfaces, most SPARC network
3671  * drivers support Style-2 only, so only DDM_ALIAS minor exists.
3672  *
3673  * It is possible for OBP to present device args at the end of the path as
3674  * well as in the middle. For example, with IB the following strings are
3675  * valid boot paths.
3676  *	a /pci@8,700000/ib@1,2:port=1,pkey=ff,dhcp,...
3677  *	b /pci@8,700000/ib@1,1:port=1/ioc@xxxxxx,yyyyyyy:dhcp
3678  * Case (a), we first look for minor node "port=1,pkey...".
3679  * Failing that, we will pass "port=1,pkey..." to the bus_config
3680  * entry point of ib (HCA) driver.
3681  * Case (b), configure ib@1,1 as usual. Then invoke ib's bus_config
3682  * with argument "ioc@xxxxxxx,yyyyyyy:port=1". After configuring
3683  * the ioc, look for minor node dhcp. If not found, pass ":dhcp"
3684  * to ioc's bus_config entry point.
3685  */
3686 static int
3687 parse_pathname(char *pathname,
3688 	dev_info_t **dipp, dev_t *devtp, int *spectypep, dev_info_t **pci_dipp)
3689 {
3690 	int			error;
3691 	dev_info_t		*parent, *child;
3692 	struct pathname		pn;
3693 	char			*component, *config_name;
3694 	char			*minorname = NULL;
3695 	char			*prev_minor = NULL;
3696 	dev_t			devt = NODEV;
3697 	int			spectype;
3698 	struct ddi_minor_data	*dmn;
3699 	int			circ;
3700 
3701 	if (pci_dipp)
3702 		*pci_dipp = NULL;
3703 
3704 	if (*pathname != '/')
3705 		return (EINVAL);
3706 	parent = ddi_root_node();	/* Begin at the top of the tree */
3707 
3708 	if (error = pn_get(pathname, UIO_SYSSPACE, &pn))
3709 		return (error);
3710 	pn_skipslash(&pn);
3711 
3712 	ASSERT(i_ddi_devi_attached(parent));
3713 	ndi_hold_devi(parent);
3714 
3715 	component = kmem_alloc(MAXNAMELEN, KM_SLEEP);
3716 	config_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
3717 
3718 	while (pn_pathleft(&pn)) {
3719 		/* remember prev minor (:xxx) in the middle of path */
3720 		if (minorname)
3721 			prev_minor = i_ddi_strdup(minorname, KM_SLEEP);
3722 
3723 		/* Get component and chop off minorname */
3724 		(void) pn_getcomponent(&pn, component);
3725 		i_ddi_parse_name(component, NULL, NULL, &minorname);
3726 
3727 		if (prev_minor == NULL) {
3728 			(void) snprintf(config_name, MAXNAMELEN, "%s",
3729 			    component);
3730 		} else {
3731 			(void) snprintf(config_name, MAXNAMELEN, "%s:%s",
3732 			    component, prev_minor);
3733 			kmem_free(prev_minor, strlen(prev_minor) + 1);
3734 			prev_minor = NULL;
3735 		}
3736 
3737 		/*
3738 		 * Find and configure the child
3739 		 */
3740 		if (ndi_devi_config_one(parent, config_name, &child,
3741 		    NDI_PROMNAME | NDI_NO_EVENT) != NDI_SUCCESS) {
3742 			ndi_rele_devi(parent);
3743 			pn_free(&pn);
3744 			kmem_free(component, MAXNAMELEN);
3745 			kmem_free(config_name, MAXNAMELEN);
3746 			if (pci_dipp && *pci_dipp) {
3747 				ndi_rele_devi(*pci_dipp);
3748 				*pci_dipp = NULL;
3749 			}
3750 			return (-1);
3751 		}
3752 
3753 		ASSERT(i_ddi_devi_attached(child));
3754 		ndi_rele_devi(parent);
3755 		parent = child;
3756 		pn_skipslash(&pn);
3757 		if (pci_dipp) {
3758 			if (ddi_is_pci_dip(child)) {
3759 				ndi_hold_devi(child);
3760 				if (*pci_dipp != NULL) {
3761 					ndi_rele_devi(*pci_dipp);
3762 				}
3763 				*pci_dipp = child;
3764 			}
3765 		}
3766 	}
3767 
3768 	/*
3769 	 * First look for a minor node matching minorname.
3770 	 * Failing that, try to pass minorname to bus_config().
3771 	 */
3772 	if (minorname && i_ddi_minorname_to_devtspectype(parent,
3773 	    minorname, &devt, &spectype) == DDI_FAILURE) {
3774 		(void) snprintf(config_name, MAXNAMELEN, "%s", minorname);
3775 		if (ndi_devi_config_obp_args(parent,
3776 		    config_name, &child, 0) != NDI_SUCCESS) {
3777 			ndi_rele_devi(parent);
3778 			pn_free(&pn);
3779 			kmem_free(component, MAXNAMELEN);
3780 			kmem_free(config_name, MAXNAMELEN);
3781 			NDI_CONFIG_DEBUG((CE_NOTE,
3782 			    "%s: minor node not found\n", pathname));
3783 			if (pci_dipp && *pci_dipp) {
3784 				ndi_rele_devi(*pci_dipp);
3785 				*pci_dipp = NULL;
3786 			}
3787 			return (-1);
3788 		}
3789 		minorname = NULL;	/* look for default minor */
3790 		ASSERT(i_ddi_devi_attached(child));
3791 		ndi_rele_devi(parent);
3792 		parent = child;
3793 	}
3794 
3795 	if (devtp || spectypep) {
3796 		if (minorname == NULL) {
3797 			/*
3798 			 * Search for a default entry with an active
3799 			 * ndi_devi_enter to protect the devi_minor list.
3800 			 */
3801 			ndi_devi_enter(parent, &circ);
3802 			for (dmn = DEVI(parent)->devi_minor; dmn;
3803 			    dmn = dmn->next) {
3804 				if (dmn->type == DDM_DEFAULT) {
3805 					devt = dmn->ddm_dev;
3806 					spectype = dmn->ddm_spec_type;
3807 					break;
3808 				}
3809 			}
3810 
3811 			if (devt == NODEV) {
3812 				/*
3813 				 * No default minor node, try the first one;
3814 				 * else, assume 1-1 instance-minor mapping
3815 				 */
3816 				dmn = DEVI(parent)->devi_minor;
3817 				if (dmn && ((dmn->type == DDM_MINOR) ||
3818 				    (dmn->type == DDM_INTERNAL_PATH))) {
3819 					devt = dmn->ddm_dev;
3820 					spectype = dmn->ddm_spec_type;
3821 				} else {
3822 					devt = makedevice(
3823 					    DEVI(parent)->devi_major,
3824 					    ddi_get_instance(parent));
3825 					spectype = S_IFCHR;
3826 				}
3827 			}
3828 			ndi_devi_exit(parent, circ);
3829 		}
3830 		if (devtp)
3831 			*devtp = devt;
3832 		if (spectypep)
3833 			*spectypep = spectype;
3834 	}
3835 
3836 	pn_free(&pn);
3837 	kmem_free(component, MAXNAMELEN);
3838 	kmem_free(config_name, MAXNAMELEN);
3839 
3840 	/*
3841 	 * If there is no error, return the appropriate parameters
3842 	 */
3843 	if (dipp != NULL)
3844 		*dipp = parent;
3845 	else if (pci_dipp == NULL) {
3846 		/*
3847 		 * We should really keep the ref count to keep the node from
3848 		 * detaching but ddi_pathname_to_dev_t() specifies a NULL dipp,
3849 		 * so we have no way of passing back the held dip.  Not holding
3850 		 * the dip allows detaches to occur - which can cause problems
3851 		 * for subsystems which call ddi_pathname_to_dev_t (console).
3852 		 *
3853 		 * Instead of holding the dip, we place a ddi-no-autodetach
3854 		 * property on the node to prevent auto detaching.
3855 		 *
3856 		 * The right fix is to remove ddi_pathname_to_dev_t and replace
3857 		 * it, and all references, with a call that specifies a dipp.
3858 		 * In addition, the callers of this new interfaces would then
3859 		 * need to call ndi_rele_devi when the reference is complete.
3860 		 *
3861 		 * NOTE: If pci_dipp is non-NULL we are only interested
3862 		 * in the PCI parent which is returned held. No need to hold
3863 		 * the leaf dip.
3864 		 */
3865 		(void) ddi_prop_update_int(DDI_DEV_T_NONE, parent,
3866 		    DDI_NO_AUTODETACH, 1);
3867 		ndi_rele_devi(parent);
3868 	}
3869 
3870 	return (0);
3871 }
3872 
3873 int
3874 resolve_pathname(char *pathname,
3875 	dev_info_t **dipp, dev_t *devtp, int *spectypep)
3876 {
3877 	return (parse_pathname(pathname, dipp, devtp, spectypep, NULL));
3878 }
3879 
3880 int
3881 ddi_find_pci_parent(char *pathname, dev_info_t **pci_dipp)
3882 {
3883 	return (parse_pathname(pathname, NULL, NULL, NULL, pci_dipp));
3884 }
3885 
3886 /*
3887  * Given the pathname of a device, return the dev_t of the corresponding
3888  * device.  Returns NODEV on failure.
3889  *
3890  * Note that this call sets the DDI_NO_AUTODETACH property on the devinfo node.
3891  */
3892 dev_t
3893 ddi_pathname_to_dev_t(char *pathname)
3894 {
3895 	dev_t devt;
3896 	int error;
3897 
3898 	error = resolve_pathname(pathname, NULL, &devt, NULL);
3899 
3900 	return (error ? NODEV : devt);
3901 }
3902 
3903 /*
3904  * Translate a prom pathname to kernel devfs pathname.
3905  * Caller is assumed to allocate devfspath memory of
3906  * size at least MAXPATHLEN
3907  *
3908  * The prom pathname may not include minor name, but
3909  * devfs pathname has a minor name portion.
3910  */
3911 int
3912 i_ddi_prompath_to_devfspath(char *prompath, char *devfspath)
3913 {
3914 	dev_t		devt = (dev_t)NODEV;
3915 	dev_info_t	*dip = NULL;
3916 	char		*minor_name = NULL;
3917 	int		spectype;
3918 	int		error;
3919 	int		circ;
3920 
3921 	error = resolve_pathname(prompath, &dip, &devt, &spectype);
3922 	if (error)
3923 		return (DDI_FAILURE);
3924 	ASSERT(dip && devt != NODEV);
3925 
3926 	/*
3927 	 * Get in-kernel devfs pathname
3928 	 */
3929 	(void) ddi_pathname(dip, devfspath);
3930 
3931 	ndi_devi_enter(dip, &circ);
3932 	minor_name = i_ddi_devtspectype_to_minorname(dip, devt, spectype);
3933 	if (minor_name) {
3934 		(void) strcat(devfspath, ":");
3935 		(void) strcat(devfspath, minor_name);
3936 	} else {
3937 		/*
3938 		 * If minor_name is NULL, we have an alias minor node.
3939 		 * So manufacture a path to the corresponding clone minor.
3940 		 */
3941 		(void) snprintf(devfspath, MAXPATHLEN, "%s:%s",
3942 		    CLONE_PATH, ddi_driver_name(dip));
3943 	}
3944 	ndi_devi_exit(dip, circ);
3945 
3946 	/* release hold from resolve_pathname() */
3947 	ndi_rele_devi(dip);
3948 	return (0);
3949 }
3950 
3951 /*
3952  * Reset all the pure leaf drivers on the system at halt time
3953  */
3954 static int
3955 reset_leaf_device(dev_info_t *dip, void *arg)
3956 {
3957 	_NOTE(ARGUNUSED(arg))
3958 	struct dev_ops *ops;
3959 
3960 	/* if the device doesn't need to be reset then there's nothing to do */
3961 	if (!DEVI_NEED_RESET(dip))
3962 		return (DDI_WALK_CONTINUE);
3963 
3964 	/*
3965 	 * if the device isn't a char/block device or doesn't have a
3966 	 * reset entry point then there's nothing to do.
3967 	 */
3968 	ops = ddi_get_driver(dip);
3969 	if ((ops == NULL) || (ops->devo_cb_ops == NULL) ||
3970 	    (ops->devo_reset == nodev) || (ops->devo_reset == nulldev) ||
3971 	    (ops->devo_reset == NULL))
3972 		return (DDI_WALK_CONTINUE);
3973 
3974 	if (DEVI_IS_ATTACHING(dip) || DEVI_IS_DETACHING(dip)) {
3975 		static char path[MAXPATHLEN];
3976 
3977 		/*
3978 		 * bad news, this device has blocked in it's attach or
3979 		 * detach routine, which means it not safe to call it's
3980 		 * devo_reset() entry point.
3981 		 */
3982 		cmn_err(CE_WARN, "unable to reset device: %s",
3983 		    ddi_pathname(dip, path));
3984 		return (DDI_WALK_CONTINUE);
3985 	}
3986 
3987 	NDI_CONFIG_DEBUG((CE_NOTE, "resetting %s%d\n",
3988 	    ddi_driver_name(dip), ddi_get_instance(dip)));
3989 
3990 	(void) devi_reset(dip, DDI_RESET_FORCE);
3991 	return (DDI_WALK_CONTINUE);
3992 }
3993 
3994 void
3995 reset_leaves(void)
3996 {
3997 	/*
3998 	 * if we're reached here, the device tree better not be changing.
3999 	 * so either devinfo_freeze better be set or we better be panicing.
4000 	 */
4001 	ASSERT(devinfo_freeze || panicstr);
4002 
4003 	(void) walk_devs(top_devinfo, reset_leaf_device, NULL, 0);
4004 }
4005 
4006 /*
4007  * devtree_freeze() must be called before reset_leaves() during a
4008  * normal system shutdown.  It attempts to ensure that there are no
4009  * outstanding attach or detach operations in progress when reset_leaves()
4010  * is invoked.  It must be called before the system becomes single-threaded
4011  * because device attach and detach are multi-threaded operations.  (note
4012  * that during system shutdown the system doesn't actually become
4013  * single-thread since other threads still exist, but the shutdown thread
4014  * will disable preemption for itself, raise it's pil, and stop all the
4015  * other cpus in the system there by effectively making the system
4016  * single-threaded.)
4017  */
4018 void
4019 devtree_freeze(void)
4020 {
4021 	int delayed = 0;
4022 
4023 	/* if we're panicing then the device tree isn't going to be changing */
4024 	if (panicstr)
4025 		return;
4026 
4027 	/* stop all dev_info state changes in the device tree */
4028 	devinfo_freeze = gethrtime();
4029 
4030 	/*
4031 	 * if we're not panicing and there are on-going attach or detach
4032 	 * operations, wait for up to 3 seconds for them to finish.  This
4033 	 * is a randomly chosen interval but this should be ok because:
4034 	 * - 3 seconds is very small relative to the deadman timer.
4035 	 * - normal attach and detach operations should be very quick.
4036 	 * - attach and detach operations are fairly rare.
4037 	 */
4038 	while (!panicstr && atomic_add_long_nv(&devinfo_attach_detach, 0) &&
4039 	    (delayed < 3)) {
4040 		delayed += 1;
4041 
4042 		/* do a sleeping wait for one second */
4043 		ASSERT(!servicing_interrupt());
4044 		delay(drv_usectohz(MICROSEC));
4045 	}
4046 }
4047 
4048 static int
4049 bind_dip(dev_info_t *dip, void *arg)
4050 {
4051 	_NOTE(ARGUNUSED(arg))
4052 	char	*path;
4053 	major_t	major, pmajor;
4054 
4055 	/*
4056 	 * If the node is currently bound to the wrong driver, try to unbind
4057 	 * so that we can rebind to the correct driver.
4058 	 */
4059 	if (i_ddi_node_state(dip) >= DS_BOUND) {
4060 		major = ddi_compatible_driver_major(dip, NULL);
4061 		if ((DEVI(dip)->devi_major == major) &&
4062 		    (i_ddi_node_state(dip) >= DS_INITIALIZED)) {
4063 			/*
4064 			 * Check for a path-oriented driver alias that
4065 			 * takes precedence over current driver binding.
4066 			 */
4067 			path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4068 			(void) ddi_pathname(dip, path);
4069 			pmajor = ddi_name_to_major(path);
4070 			if ((pmajor != DDI_MAJOR_T_NONE) &&
4071 			    !(devnamesp[pmajor].dn_flags & DN_DRIVER_REMOVED))
4072 				major = pmajor;
4073 			kmem_free(path, MAXPATHLEN);
4074 		}
4075 
4076 		/* attempt unbind if current driver is incorrect */
4077 		if ((major != DDI_MAJOR_T_NONE) &&
4078 		    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED) &&
4079 		    (major != DEVI(dip)->devi_major))
4080 			(void) ndi_devi_unbind_driver(dip);
4081 	}
4082 
4083 	/* If unbound, try to bind to a driver */
4084 	if (i_ddi_node_state(dip) < DS_BOUND)
4085 		(void) ndi_devi_bind_driver(dip, 0);
4086 
4087 	return (DDI_WALK_CONTINUE);
4088 }
4089 
4090 void
4091 i_ddi_bind_devs(void)
4092 {
4093 	/* flush devfs so that ndi_devi_unbind_driver will work when possible */
4094 	(void) devfs_clean(top_devinfo, NULL, 0);
4095 
4096 	ddi_walk_devs(top_devinfo, bind_dip, (void *)NULL);
4097 }
4098 
4099 static int
4100 unbind_children(dev_info_t *dip, void *arg)
4101 {
4102 	int circ;
4103 	dev_info_t *cdip;
4104 	major_t major = (major_t)(uintptr_t)arg;
4105 
4106 	ndi_devi_enter(dip, &circ);
4107 	cdip = ddi_get_child(dip);
4108 	/*
4109 	 * We are called either from rem_drv or update_drv.
4110 	 * In both cases, we unbind persistent nodes and destroy
4111 	 * .conf nodes. In the case of rem_drv, this will be the
4112 	 * final state. In the case of update_drv, i_ddi_bind_devs()
4113 	 * will be invoked later to reenumerate (new) driver.conf
4114 	 * rebind persistent nodes.
4115 	 */
4116 	while (cdip) {
4117 		dev_info_t *next = ddi_get_next_sibling(cdip);
4118 		if ((i_ddi_node_state(cdip) > DS_INITIALIZED) ||
4119 		    (ddi_driver_major(cdip) != major)) {
4120 			cdip = next;
4121 			continue;
4122 		}
4123 		(void) ndi_devi_unbind_driver(cdip);
4124 		if (ndi_dev_is_persistent_node(cdip) == 0)
4125 			(void) ddi_remove_child(cdip, 0);
4126 		cdip = next;
4127 	}
4128 	ndi_devi_exit(dip, circ);
4129 
4130 	return (DDI_WALK_CONTINUE);
4131 }
4132 
4133 void
4134 i_ddi_unbind_devs(major_t major)
4135 {
4136 	ddi_walk_devs(top_devinfo, unbind_children, (void *)(uintptr_t)major);
4137 }
4138 
4139 /*
4140  * I/O Hotplug control
4141  */
4142 
4143 /*
4144  * create and attach a dev_info node from a .conf file spec
4145  */
4146 static void
4147 init_spec_child(dev_info_t *pdip, struct hwc_spec *specp, uint_t flags)
4148 {
4149 	_NOTE(ARGUNUSED(flags))
4150 	dev_info_t *dip;
4151 	char *node_name;
4152 
4153 	if (((node_name = specp->hwc_devi_name) == NULL) ||
4154 	    (ddi_name_to_major(node_name) == DDI_MAJOR_T_NONE)) {
4155 		char *tmp = node_name;
4156 		if (tmp == NULL)
4157 			tmp = "<none>";
4158 		cmn_err(CE_CONT,
4159 		    "init_spec_child: parent=%s, bad spec (%s)\n",
4160 		    ddi_node_name(pdip), tmp);
4161 		return;
4162 	}
4163 
4164 	dip = i_ddi_alloc_node(pdip, node_name, (pnode_t)DEVI_PSEUDO_NODEID,
4165 	    -1, specp->hwc_devi_sys_prop_ptr, KM_SLEEP);
4166 
4167 	if (dip == NULL)
4168 		return;
4169 
4170 	if (ddi_initchild(pdip, dip) != DDI_SUCCESS)
4171 		(void) ddi_remove_child(dip, 0);
4172 }
4173 
4174 /*
4175  * Lookup hwc specs from hash tables and make children from the spec
4176  * Because some .conf children are "merge" nodes, we also initialize
4177  * .conf children to merge properties onto hardware nodes.
4178  *
4179  * The pdip must be held busy.
4180  */
4181 int
4182 i_ndi_make_spec_children(dev_info_t *pdip, uint_t flags)
4183 {
4184 	extern struct hwc_spec *hwc_get_child_spec(dev_info_t *, major_t);
4185 	int			circ;
4186 	struct hwc_spec		*list, *spec;
4187 
4188 	ndi_devi_enter(pdip, &circ);
4189 	if (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN) {
4190 		ndi_devi_exit(pdip, circ);
4191 		return (DDI_SUCCESS);
4192 	}
4193 
4194 	list = hwc_get_child_spec(pdip, DDI_MAJOR_T_NONE);
4195 	for (spec = list; spec != NULL; spec = spec->hwc_next) {
4196 		init_spec_child(pdip, spec, flags);
4197 	}
4198 	hwc_free_spec_list(list);
4199 
4200 	mutex_enter(&DEVI(pdip)->devi_lock);
4201 	DEVI(pdip)->devi_flags |= DEVI_MADE_CHILDREN;
4202 	mutex_exit(&DEVI(pdip)->devi_lock);
4203 	ndi_devi_exit(pdip, circ);
4204 	return (DDI_SUCCESS);
4205 }
4206 
4207 /*
4208  * Run initchild on all child nodes such that instance assignment
4209  * for multiport network cards are contiguous.
4210  *
4211  * The pdip must be held busy.
4212  */
4213 static void
4214 i_ndi_init_hw_children(dev_info_t *pdip, uint_t flags)
4215 {
4216 	dev_info_t *dip;
4217 
4218 	ASSERT(DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
4219 
4220 	/* contiguous instance assignment */
4221 	e_ddi_enter_instance();
4222 	dip = ddi_get_child(pdip);
4223 	while (dip) {
4224 		if (ndi_dev_is_persistent_node(dip))
4225 			(void) i_ndi_config_node(dip, DS_INITIALIZED, flags);
4226 		dip = ddi_get_next_sibling(dip);
4227 	}
4228 	e_ddi_exit_instance();
4229 }
4230 
4231 /*
4232  * report device status
4233  */
4234 static void
4235 i_ndi_devi_report_status_change(dev_info_t *dip, char *path)
4236 {
4237 	char *status;
4238 
4239 	if (!DEVI_NEED_REPORT(dip) ||
4240 	    (i_ddi_node_state(dip) < DS_INITIALIZED)) {
4241 		return;
4242 	}
4243 
4244 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
4245 		status = "offline";
4246 	} else if (DEVI_IS_DEVICE_DOWN(dip)) {
4247 		status = "down";
4248 	} else if (DEVI_IS_BUS_QUIESCED(dip)) {
4249 		status = "quiesced";
4250 	} else if (DEVI_IS_BUS_DOWN(dip)) {
4251 		status = "down";
4252 	} else if (i_ddi_devi_attached(dip)) {
4253 		status = "online";
4254 	} else {
4255 		status = "unknown";
4256 	}
4257 
4258 	if (path == NULL) {
4259 		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4260 		cmn_err(CE_CONT, "?%s (%s%d) %s\n",
4261 		    ddi_pathname(dip, path), ddi_driver_name(dip),
4262 		    ddi_get_instance(dip), status);
4263 		kmem_free(path, MAXPATHLEN);
4264 	} else {
4265 		cmn_err(CE_CONT, "?%s (%s%d) %s\n",
4266 		    path, ddi_driver_name(dip),
4267 		    ddi_get_instance(dip), status);
4268 	}
4269 
4270 	mutex_enter(&(DEVI(dip)->devi_lock));
4271 	DEVI_REPORT_DONE(dip);
4272 	mutex_exit(&(DEVI(dip)->devi_lock));
4273 }
4274 
4275 /*
4276  * log a notification that a dev_info node has been configured.
4277  */
4278 static int
4279 i_log_devfs_add_devinfo(dev_info_t *dip, uint_t flags)
4280 {
4281 	int se_err;
4282 	char *pathname;
4283 	sysevent_t *ev;
4284 	sysevent_id_t eid;
4285 	sysevent_value_t se_val;
4286 	sysevent_attr_list_t *ev_attr_list = NULL;
4287 	char *class_name;
4288 	int no_transport = 0;
4289 
4290 	ASSERT(dip);
4291 
4292 	/*
4293 	 * Invalidate the devinfo snapshot cache
4294 	 */
4295 	i_ddi_di_cache_invalidate(KM_SLEEP);
4296 
4297 	/* do not generate ESC_DEVFS_DEVI_ADD event during boot */
4298 	if (!i_ddi_io_initialized())
4299 		return (DDI_SUCCESS);
4300 
4301 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_ADD, EP_DDI, SE_SLEEP);
4302 
4303 	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4304 
4305 	(void) ddi_pathname(dip, pathname);
4306 	ASSERT(strlen(pathname));
4307 
4308 	se_val.value_type = SE_DATA_TYPE_STRING;
4309 	se_val.value.sv_string = pathname;
4310 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
4311 	    &se_val, SE_SLEEP) != 0) {
4312 		goto fail;
4313 	}
4314 
4315 	/* add the device class attribute */
4316 	if ((class_name = i_ddi_devi_class(dip)) != NULL) {
4317 		se_val.value_type = SE_DATA_TYPE_STRING;
4318 		se_val.value.sv_string = class_name;
4319 
4320 		if (sysevent_add_attr(&ev_attr_list,
4321 		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
4322 			sysevent_free_attr(ev_attr_list);
4323 			goto fail;
4324 		}
4325 	}
4326 
4327 	/*
4328 	 * must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
4329 	 * in which case the branch event will be logged by the caller
4330 	 * after the entire branch has been configured.
4331 	 */
4332 	if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
4333 		/*
4334 		 * Instead of logging a separate branch event just add
4335 		 * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
4336 		 * generate a EC_DEV_BRANCH event.
4337 		 */
4338 		se_val.value_type = SE_DATA_TYPE_INT32;
4339 		se_val.value.sv_int32 = 1;
4340 		if (sysevent_add_attr(&ev_attr_list,
4341 		    DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
4342 			sysevent_free_attr(ev_attr_list);
4343 			goto fail;
4344 		}
4345 	}
4346 
4347 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
4348 		sysevent_free_attr(ev_attr_list);
4349 		goto fail;
4350 	}
4351 
4352 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
4353 		if (se_err == SE_NO_TRANSPORT)
4354 			no_transport = 1;
4355 		goto fail;
4356 	}
4357 
4358 	sysevent_free(ev);
4359 	kmem_free(pathname, MAXPATHLEN);
4360 
4361 	return (DDI_SUCCESS);
4362 
4363 fail:
4364 	cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_ADD event for %s%s",
4365 	    pathname, (no_transport) ? " (syseventd not responding)" : "");
4366 
4367 	cmn_err(CE_WARN, "/dev may not be current for driver %s. "
4368 	    "Run devfsadm -i %s",
4369 	    ddi_driver_name(dip), ddi_driver_name(dip));
4370 
4371 	sysevent_free(ev);
4372 	kmem_free(pathname, MAXPATHLEN);
4373 	return (DDI_SUCCESS);
4374 }
4375 
4376 /*
4377  * log a notification that a dev_info node has been unconfigured.
4378  */
4379 static int
4380 i_log_devfs_remove_devinfo(char *pathname, char *class_name, char *driver_name,
4381     int instance, uint_t flags)
4382 {
4383 	sysevent_t *ev;
4384 	sysevent_id_t eid;
4385 	sysevent_value_t se_val;
4386 	sysevent_attr_list_t *ev_attr_list = NULL;
4387 	int se_err;
4388 	int no_transport = 0;
4389 
4390 	i_ddi_di_cache_invalidate(KM_SLEEP);
4391 
4392 	if (!i_ddi_io_initialized())
4393 		return (DDI_SUCCESS);
4394 
4395 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_REMOVE, EP_DDI, SE_SLEEP);
4396 
4397 	se_val.value_type = SE_DATA_TYPE_STRING;
4398 	se_val.value.sv_string = pathname;
4399 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
4400 	    &se_val, SE_SLEEP) != 0) {
4401 		goto fail;
4402 	}
4403 
4404 	if (class_name) {
4405 		/* add the device class, driver name and instance attributes */
4406 
4407 		se_val.value_type = SE_DATA_TYPE_STRING;
4408 		se_val.value.sv_string = class_name;
4409 		if (sysevent_add_attr(&ev_attr_list,
4410 		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
4411 			sysevent_free_attr(ev_attr_list);
4412 			goto fail;
4413 		}
4414 
4415 		se_val.value_type = SE_DATA_TYPE_STRING;
4416 		se_val.value.sv_string = driver_name;
4417 		if (sysevent_add_attr(&ev_attr_list,
4418 		    DEVFS_DRIVER_NAME, &se_val, SE_SLEEP) != 0) {
4419 			sysevent_free_attr(ev_attr_list);
4420 			goto fail;
4421 		}
4422 
4423 		se_val.value_type = SE_DATA_TYPE_INT32;
4424 		se_val.value.sv_int32 = instance;
4425 		if (sysevent_add_attr(&ev_attr_list,
4426 		    DEVFS_INSTANCE, &se_val, SE_SLEEP) != 0) {
4427 			sysevent_free_attr(ev_attr_list);
4428 			goto fail;
4429 		}
4430 	}
4431 
4432 	/*
4433 	 * must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
4434 	 * in which case the branch event will be logged by the caller
4435 	 * after the entire branch has been unconfigured.
4436 	 */
4437 	if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
4438 		/*
4439 		 * Instead of logging a separate branch event just add
4440 		 * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
4441 		 * generate a EC_DEV_BRANCH event.
4442 		 */
4443 		se_val.value_type = SE_DATA_TYPE_INT32;
4444 		se_val.value.sv_int32 = 1;
4445 		if (sysevent_add_attr(&ev_attr_list,
4446 		    DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
4447 			sysevent_free_attr(ev_attr_list);
4448 			goto fail;
4449 		}
4450 	}
4451 
4452 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
4453 		sysevent_free_attr(ev_attr_list);
4454 		goto fail;
4455 	}
4456 
4457 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
4458 		if (se_err == SE_NO_TRANSPORT)
4459 			no_transport = 1;
4460 		goto fail;
4461 	}
4462 
4463 	sysevent_free(ev);
4464 	return (DDI_SUCCESS);
4465 
4466 fail:
4467 	sysevent_free(ev);
4468 	cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_REMOVE event for %s%s",
4469 	    pathname, (no_transport) ? " (syseventd not responding)" : "");
4470 	return (DDI_SUCCESS);
4471 }
4472 
4473 /*
4474  * log an event that a dev_info branch has been configured or unconfigured.
4475  */
4476 static int
4477 i_log_devfs_branch(char *node_path, char *subclass)
4478 {
4479 	int se_err;
4480 	sysevent_t *ev;
4481 	sysevent_id_t eid;
4482 	sysevent_value_t se_val;
4483 	sysevent_attr_list_t *ev_attr_list = NULL;
4484 	int no_transport = 0;
4485 
4486 	/* do not generate the event during boot */
4487 	if (!i_ddi_io_initialized())
4488 		return (DDI_SUCCESS);
4489 
4490 	ev = sysevent_alloc(EC_DEVFS, subclass, EP_DDI, SE_SLEEP);
4491 
4492 	se_val.value_type = SE_DATA_TYPE_STRING;
4493 	se_val.value.sv_string = node_path;
4494 
4495 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
4496 	    &se_val, SE_SLEEP) != 0) {
4497 		goto fail;
4498 	}
4499 
4500 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
4501 		sysevent_free_attr(ev_attr_list);
4502 		goto fail;
4503 	}
4504 
4505 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
4506 		if (se_err == SE_NO_TRANSPORT)
4507 			no_transport = 1;
4508 		goto fail;
4509 	}
4510 
4511 	sysevent_free(ev);
4512 	return (DDI_SUCCESS);
4513 
4514 fail:
4515 	cmn_err(CE_WARN, "failed to log %s branch event for %s%s",
4516 	    subclass, node_path,
4517 	    (no_transport) ? " (syseventd not responding)" : "");
4518 
4519 	sysevent_free(ev);
4520 	return (DDI_FAILURE);
4521 }
4522 
4523 /*
4524  * log an event that a dev_info tree branch has been configured.
4525  */
4526 static int
4527 i_log_devfs_branch_add(dev_info_t *dip)
4528 {
4529 	char *node_path;
4530 	int rv;
4531 
4532 	node_path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4533 	(void) ddi_pathname(dip, node_path);
4534 	rv = i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_ADD);
4535 	kmem_free(node_path, MAXPATHLEN);
4536 
4537 	return (rv);
4538 }
4539 
4540 /*
4541  * log an event that a dev_info tree branch has been unconfigured.
4542  */
4543 static int
4544 i_log_devfs_branch_remove(char *node_path)
4545 {
4546 	return (i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_REMOVE));
4547 }
4548 
4549 /*
4550  * enqueue the dip's deviname on the branch event queue.
4551  */
4552 static struct brevq_node *
4553 brevq_enqueue(struct brevq_node **brevqp, dev_info_t *dip,
4554     struct brevq_node *child)
4555 {
4556 	struct brevq_node *brn;
4557 	char *deviname;
4558 
4559 	deviname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
4560 	(void) ddi_deviname(dip, deviname);
4561 
4562 	brn = kmem_zalloc(sizeof (*brn), KM_SLEEP);
4563 	brn->brn_deviname = i_ddi_strdup(deviname, KM_SLEEP);
4564 	kmem_free(deviname, MAXNAMELEN);
4565 	brn->brn_child = child;
4566 	brn->brn_sibling = *brevqp;
4567 	*brevqp = brn;
4568 
4569 	return (brn);
4570 }
4571 
4572 /*
4573  * free the memory allocated for the elements on the branch event queue.
4574  */
4575 static void
4576 free_brevq(struct brevq_node *brevq)
4577 {
4578 	struct brevq_node *brn, *next_brn;
4579 
4580 	for (brn = brevq; brn != NULL; brn = next_brn) {
4581 		next_brn = brn->brn_sibling;
4582 		ASSERT(brn->brn_child == NULL);
4583 		kmem_free(brn->brn_deviname, strlen(brn->brn_deviname) + 1);
4584 		kmem_free(brn, sizeof (*brn));
4585 	}
4586 }
4587 
4588 /*
4589  * log the events queued up on the branch event queue and free the
4590  * associated memory.
4591  *
4592  * node_path must have been allocated with at least MAXPATHLEN bytes.
4593  */
4594 static void
4595 log_and_free_brevq(char *node_path, struct brevq_node *brevq)
4596 {
4597 	struct brevq_node *brn;
4598 	char *p;
4599 
4600 	p = node_path + strlen(node_path);
4601 	for (brn = brevq; brn != NULL; brn = brn->brn_sibling) {
4602 		(void) strcpy(p, brn->brn_deviname);
4603 		(void) i_log_devfs_branch_remove(node_path);
4604 	}
4605 	*p = '\0';
4606 
4607 	free_brevq(brevq);
4608 }
4609 
4610 /*
4611  * log the events queued up on the branch event queue and free the
4612  * associated memory. Same as the previous function but operates on dip.
4613  */
4614 static void
4615 log_and_free_brevq_dip(dev_info_t *dip, struct brevq_node *brevq)
4616 {
4617 	char *path;
4618 
4619 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4620 	(void) ddi_pathname(dip, path);
4621 	log_and_free_brevq(path, brevq);
4622 	kmem_free(path, MAXPATHLEN);
4623 }
4624 
4625 /*
4626  * log the outstanding branch remove events for the grand children of the dip
4627  * and free the associated memory.
4628  */
4629 static void
4630 log_and_free_br_events_on_grand_children(dev_info_t *dip,
4631     struct brevq_node *brevq)
4632 {
4633 	struct brevq_node *brn;
4634 	char *path;
4635 	char *p;
4636 
4637 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4638 	(void) ddi_pathname(dip, path);
4639 	p = path + strlen(path);
4640 	for (brn = brevq; brn != NULL; brn = brn->brn_sibling) {
4641 		if (brn->brn_child) {
4642 			(void) strcpy(p, brn->brn_deviname);
4643 			/* now path contains the node path to the dip's child */
4644 			log_and_free_brevq(path, brn->brn_child);
4645 			brn->brn_child = NULL;
4646 		}
4647 	}
4648 	kmem_free(path, MAXPATHLEN);
4649 }
4650 
4651 /*
4652  * log and cleanup branch remove events for the grand children of the dip.
4653  */
4654 static void
4655 cleanup_br_events_on_grand_children(dev_info_t *dip, struct brevq_node **brevqp)
4656 {
4657 	dev_info_t *child;
4658 	struct brevq_node *brevq, *brn, *prev_brn, *next_brn;
4659 	char *path;
4660 	int circ;
4661 
4662 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4663 	prev_brn = NULL;
4664 	brevq = *brevqp;
4665 
4666 	ndi_devi_enter(dip, &circ);
4667 	for (brn = brevq; brn != NULL; brn = next_brn) {
4668 		next_brn = brn->brn_sibling;
4669 		for (child = ddi_get_child(dip); child != NULL;
4670 		    child = ddi_get_next_sibling(child)) {
4671 			if (i_ddi_node_state(child) >= DS_INITIALIZED) {
4672 				(void) ddi_deviname(child, path);
4673 				if (strcmp(path, brn->brn_deviname) == 0)
4674 					break;
4675 			}
4676 		}
4677 
4678 		if (child != NULL && !(DEVI_EVREMOVE(child))) {
4679 			/*
4680 			 * Event state is not REMOVE. So branch remove event
4681 			 * is not going be generated on brn->brn_child.
4682 			 * If any branch remove events were queued up on
4683 			 * brn->brn_child log them and remove the brn
4684 			 * from the queue.
4685 			 */
4686 			if (brn->brn_child) {
4687 				(void) ddi_pathname(dip, path);
4688 				(void) strcat(path, brn->brn_deviname);
4689 				log_and_free_brevq(path, brn->brn_child);
4690 			}
4691 
4692 			if (prev_brn)
4693 				prev_brn->brn_sibling = next_brn;
4694 			else
4695 				*brevqp = next_brn;
4696 
4697 			kmem_free(brn->brn_deviname,
4698 			    strlen(brn->brn_deviname) + 1);
4699 			kmem_free(brn, sizeof (*brn));
4700 		} else {
4701 			/*
4702 			 * Free up the outstanding branch remove events
4703 			 * queued on brn->brn_child since brn->brn_child
4704 			 * itself is eligible for branch remove event.
4705 			 */
4706 			if (brn->brn_child) {
4707 				free_brevq(brn->brn_child);
4708 				brn->brn_child = NULL;
4709 			}
4710 			prev_brn = brn;
4711 		}
4712 	}
4713 
4714 	ndi_devi_exit(dip, circ);
4715 	kmem_free(path, MAXPATHLEN);
4716 }
4717 
4718 static int
4719 need_remove_event(dev_info_t *dip, int flags)
4720 {
4721 	if ((flags & (NDI_NO_EVENT | NDI_AUTODETACH)) == 0 &&
4722 	    (flags & (NDI_DEVI_OFFLINE | NDI_UNCONFIG | NDI_DEVI_REMOVE)) &&
4723 	    !(DEVI_EVREMOVE(dip)))
4724 		return (1);
4725 	else
4726 		return (0);
4727 }
4728 
4729 /*
4730  * Unconfigure children/descendants of the dip.
4731  *
4732  * If the operation involves a branch event NDI_BRANCH_EVENT_OP is set
4733  * through out the unconfiguration. On successful return *brevqp is set to
4734  * a queue of dip's child devinames for which branch remove events need
4735  * to be generated.
4736  */
4737 static int
4738 devi_unconfig_branch(dev_info_t *dip, dev_info_t **dipp, int flags,
4739     struct brevq_node **brevqp)
4740 {
4741 	int rval;
4742 
4743 	*brevqp = NULL;
4744 
4745 	if ((!(flags & NDI_BRANCH_EVENT_OP)) && need_remove_event(dip, flags))
4746 		flags |= NDI_BRANCH_EVENT_OP;
4747 
4748 	if (flags & NDI_BRANCH_EVENT_OP) {
4749 		rval = devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE,
4750 		    brevqp);
4751 
4752 		if (rval != NDI_SUCCESS && (*brevqp)) {
4753 			log_and_free_brevq_dip(dip, *brevqp);
4754 			*brevqp = NULL;
4755 		}
4756 	} else
4757 		rval = devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE,
4758 		    NULL);
4759 
4760 	return (rval);
4761 }
4762 
4763 /*
4764  * If the dip is already bound to a driver transition to DS_INITIALIZED
4765  * in order to generate an event in the case where the node was left in
4766  * DS_BOUND state since boot (never got attached) and the node is now
4767  * being offlined.
4768  */
4769 static void
4770 init_bound_node_ev(dev_info_t *pdip, dev_info_t *dip, int flags)
4771 {
4772 	if (need_remove_event(dip, flags) &&
4773 	    i_ddi_node_state(dip) == DS_BOUND &&
4774 	    i_ddi_devi_attached(pdip) && !DEVI_IS_DEVICE_OFFLINE(dip))
4775 		(void) ddi_initchild(pdip, dip);
4776 }
4777 
4778 /*
4779  * attach a node/branch with parent already held busy
4780  */
4781 static int
4782 devi_attach_node(dev_info_t *dip, uint_t flags)
4783 {
4784 	dev_info_t *pdip = ddi_get_parent(dip);
4785 
4786 	ASSERT(pdip && DEVI_BUSY_OWNED(pdip));
4787 
4788 	mutex_enter(&(DEVI(dip)->devi_lock));
4789 	if (flags & NDI_DEVI_ONLINE) {
4790 		if (!i_ddi_devi_attached(dip))
4791 			DEVI_SET_REPORT(dip);
4792 		DEVI_SET_DEVICE_ONLINE(dip);
4793 	}
4794 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
4795 		mutex_exit(&(DEVI(dip)->devi_lock));
4796 		return (NDI_FAILURE);
4797 	}
4798 	mutex_exit(&(DEVI(dip)->devi_lock));
4799 
4800 	if (i_ddi_attachchild(dip) != DDI_SUCCESS) {
4801 		mutex_enter(&(DEVI(dip)->devi_lock));
4802 		DEVI_SET_EVUNINIT(dip);
4803 		mutex_exit(&(DEVI(dip)->devi_lock));
4804 
4805 		if (ndi_dev_is_persistent_node(dip))
4806 			(void) ddi_uninitchild(dip);
4807 		else {
4808 			/*
4809 			 * Delete .conf nodes and nodes that are not
4810 			 * well formed.
4811 			 */
4812 			(void) ddi_remove_child(dip, 0);
4813 		}
4814 		return (NDI_FAILURE);
4815 	}
4816 
4817 	i_ndi_devi_report_status_change(dip, NULL);
4818 
4819 	/*
4820 	 * log an event, but not during devfs lookups in which case
4821 	 * NDI_NO_EVENT is set.
4822 	 */
4823 	if ((flags & NDI_NO_EVENT) == 0 && !(DEVI_EVADD(dip))) {
4824 		(void) i_log_devfs_add_devinfo(dip, flags);
4825 
4826 		mutex_enter(&(DEVI(dip)->devi_lock));
4827 		DEVI_SET_EVADD(dip);
4828 		mutex_exit(&(DEVI(dip)->devi_lock));
4829 	} else if (!(flags & NDI_NO_EVENT_STATE_CHNG)) {
4830 		mutex_enter(&(DEVI(dip)->devi_lock));
4831 		DEVI_SET_EVADD(dip);
4832 		mutex_exit(&(DEVI(dip)->devi_lock));
4833 	}
4834 
4835 	return (NDI_SUCCESS);
4836 }
4837 
4838 /* internal function to config immediate children */
4839 static int
4840 config_immediate_children(dev_info_t *pdip, uint_t flags, major_t major)
4841 {
4842 	dev_info_t	*child, *next;
4843 	int		circ;
4844 
4845 	ASSERT(i_ddi_devi_attached(pdip));
4846 
4847 	if (!NEXUS_DRV(ddi_get_driver(pdip)))
4848 		return (NDI_SUCCESS);
4849 
4850 	NDI_CONFIG_DEBUG((CE_CONT,
4851 	    "config_immediate_children: %s%d (%p), flags=%x\n",
4852 	    ddi_driver_name(pdip), ddi_get_instance(pdip),
4853 	    (void *)pdip, flags));
4854 
4855 	ndi_devi_enter(pdip, &circ);
4856 
4857 	if (flags & NDI_CONFIG_REPROBE) {
4858 		mutex_enter(&DEVI(pdip)->devi_lock);
4859 		DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
4860 		mutex_exit(&DEVI(pdip)->devi_lock);
4861 	}
4862 	(void) i_ndi_make_spec_children(pdip, flags);
4863 	i_ndi_init_hw_children(pdip, flags);
4864 
4865 	child = ddi_get_child(pdip);
4866 	while (child) {
4867 		/* NOTE: devi_attach_node() may remove the dip */
4868 		next = ddi_get_next_sibling(child);
4869 
4870 		/*
4871 		 * Configure all nexus nodes or leaf nodes with
4872 		 * matching driver major
4873 		 */
4874 		if ((major == DDI_MAJOR_T_NONE) ||
4875 		    (major == ddi_driver_major(child)) ||
4876 		    ((flags & NDI_CONFIG) && (is_leaf_node(child) == 0)))
4877 			(void) devi_attach_node(child, flags);
4878 		child = next;
4879 	}
4880 
4881 	ndi_devi_exit(pdip, circ);
4882 
4883 	return (NDI_SUCCESS);
4884 }
4885 
4886 /* internal function to config grand children */
4887 static int
4888 config_grand_children(dev_info_t *pdip, uint_t flags, major_t major)
4889 {
4890 	struct mt_config_handle *hdl;
4891 
4892 	/* multi-threaded configuration of child nexus */
4893 	hdl = mt_config_init(pdip, NULL, flags, major, MT_CONFIG_OP, NULL);
4894 	mt_config_children(hdl);
4895 
4896 	return (mt_config_fini(hdl));	/* wait for threads to exit */
4897 }
4898 
4899 /*
4900  * Common function for device tree configuration,
4901  * either BUS_CONFIG_ALL or BUS_CONFIG_DRIVER.
4902  * The NDI_CONFIG flag causes recursive configuration of
4903  * grandchildren, devfs usage should not recurse.
4904  */
4905 static int
4906 devi_config_common(dev_info_t *dip, int flags, major_t major)
4907 {
4908 	int error;
4909 	int (*f)();
4910 
4911 	if (!i_ddi_devi_attached(dip))
4912 		return (NDI_FAILURE);
4913 
4914 	if (pm_pre_config(dip, NULL) != DDI_SUCCESS)
4915 		return (NDI_FAILURE);
4916 
4917 	if ((DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
4918 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
4919 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
4920 		error = config_immediate_children(dip, flags, major);
4921 	} else {
4922 		/* call bus_config entry point */
4923 		ddi_bus_config_op_t bus_op = (major == DDI_MAJOR_T_NONE) ?
4924 		    BUS_CONFIG_ALL : BUS_CONFIG_DRIVER;
4925 		error = (*f)(dip,
4926 		    flags, bus_op, (void *)(uintptr_t)major, NULL, 0);
4927 	}
4928 
4929 	if (error) {
4930 		pm_post_config(dip, NULL);
4931 		return (error);
4932 	}
4933 
4934 	/*
4935 	 * Some callers, notably SCSI, need to mark the devfs cache
4936 	 * to be rebuilt together with the config operation.
4937 	 */
4938 	if (flags & NDI_DEVFS_CLEAN)
4939 		(void) devfs_clean(dip, NULL, 0);
4940 
4941 	if (flags & NDI_CONFIG)
4942 		(void) config_grand_children(dip, flags, major);
4943 
4944 	pm_post_config(dip, NULL);
4945 
4946 	return (NDI_SUCCESS);
4947 }
4948 
4949 /*
4950  * Framework entry point for BUS_CONFIG_ALL
4951  */
4952 int
4953 ndi_devi_config(dev_info_t *dip, int flags)
4954 {
4955 	NDI_CONFIG_DEBUG((CE_CONT,
4956 	    "ndi_devi_config: par = %s%d (%p), flags = 0x%x\n",
4957 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
4958 
4959 	return (devi_config_common(dip, flags, DDI_MAJOR_T_NONE));
4960 }
4961 
4962 /*
4963  * Framework entry point for BUS_CONFIG_DRIVER, bound to major
4964  */
4965 int
4966 ndi_devi_config_driver(dev_info_t *dip, int flags, major_t major)
4967 {
4968 	/* don't abuse this function */
4969 	ASSERT(major != DDI_MAJOR_T_NONE);
4970 
4971 	NDI_CONFIG_DEBUG((CE_CONT,
4972 	    "ndi_devi_config_driver: par = %s%d (%p), flags = 0x%x\n",
4973 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
4974 
4975 	return (devi_config_common(dip, flags, major));
4976 }
4977 
4978 /*
4979  * Called by nexus drivers to configure its children.
4980  */
4981 static int
4982 devi_config_one(dev_info_t *pdip, char *devnm, dev_info_t **cdipp,
4983     uint_t flags, clock_t timeout)
4984 {
4985 	dev_info_t	*vdip = NULL;
4986 	char		*drivername = NULL;
4987 	int		find_by_addr = 0;
4988 	char		*name, *addr;
4989 	int		v_circ, p_circ;
4990 	clock_t		end_time;	/* 60 sec */
4991 	int		probed;
4992 	dev_info_t	*cdip;
4993 	mdi_pathinfo_t	*cpip;
4994 
4995 	*cdipp = NULL;
4996 
4997 	if (!NEXUS_DRV(ddi_get_driver(pdip)))
4998 		return (NDI_FAILURE);
4999 
5000 	/* split name into "name@addr" parts */
5001 	i_ddi_parse_name(devnm, &name, &addr, NULL);
5002 
5003 	/*
5004 	 * If the nexus is a pHCI and we are not processing a pHCI from
5005 	 * mdi bus_config code then we need to know the vHCI.
5006 	 */
5007 	if (MDI_PHCI(pdip))
5008 		vdip = mdi_devi_get_vdip(pdip);
5009 
5010 	/*
5011 	 * We may have a genericname on a system that creates drivername
5012 	 * nodes (from .conf files).  Find the drivername by nodeid. If we
5013 	 * can't find a node with devnm as the node name then we search by
5014 	 * drivername.  This allows an implementation to supply a genericly
5015 	 * named boot path (disk) and locate drivename nodes (sd).  The
5016 	 * NDI_PROMNAME flag does not apply to /devices/pseudo paths.
5017 	 */
5018 	if ((flags & NDI_PROMNAME) && (pdip != pseudo_dip)) {
5019 		drivername = child_path_to_driver(pdip, name, addr);
5020 		find_by_addr = 1;
5021 	}
5022 
5023 	/*
5024 	 * Determine end_time: This routine should *not* be called with a
5025 	 * constant non-zero timeout argument, the caller should be adjusting
5026 	 * the timeout argument relative to when it *started* its asynchronous
5027 	 * enumeration.
5028 	 */
5029 	if (timeout > 0)
5030 		end_time = ddi_get_lbolt() + timeout;
5031 
5032 	for (;;) {
5033 		/*
5034 		 * For pHCI, enter (vHCI, pHCI) and search for pathinfo/client
5035 		 * child - break out of for(;;) loop if child found.
5036 		 * NOTE: Lock order for ndi_devi_enter is (vHCI, pHCI).
5037 		 */
5038 		if (vdip) {
5039 			/* use mdi_devi_enter ordering */
5040 			ndi_devi_enter(vdip, &v_circ);
5041 			ndi_devi_enter(pdip, &p_circ);
5042 			cpip = mdi_pi_find(pdip, NULL, addr);
5043 			cdip = mdi_pi_get_client(cpip);
5044 			if (cdip)
5045 				break;
5046 		} else
5047 			ndi_devi_enter(pdip, &p_circ);
5048 
5049 		/*
5050 		 * When not a  vHCI or not all pHCI devices are required to
5051 		 * enumerated under the vHCI (NDI_MDI_FALLBACK) search for
5052 		 * devinfo child.
5053 		 */
5054 		if ((vdip == NULL) || (flags & NDI_MDI_FALLBACK)) {
5055 			/* determine if .conf nodes already built */
5056 			probed = (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
5057 
5058 			/*
5059 			 * Search for child by name, if not found then search
5060 			 * for a node bound to the drivername driver with the
5061 			 * specified "@addr". Break out of for(;;) loop if
5062 			 * child found.  To support path-oriented aliases
5063 			 * binding on boot-device, we do a search_by_addr too.
5064 			 */
5065 again:			(void) i_ndi_make_spec_children(pdip, flags);
5066 			cdip = find_child_by_name(pdip, name, addr);
5067 			if ((cdip == NULL) && drivername)
5068 				cdip = find_child_by_driver(pdip,
5069 				    drivername, addr);
5070 			if ((cdip == NULL) && find_by_addr)
5071 				cdip = find_child_by_addr(pdip, addr);
5072 			if (cdip)
5073 				break;
5074 
5075 			/*
5076 			 * determine if we should reenumerate .conf nodes
5077 			 * and look for child again.
5078 			 */
5079 			if (probed &&
5080 			    i_ddi_io_initialized() &&
5081 			    (flags & NDI_CONFIG_REPROBE) &&
5082 			    ((timeout <= 0) || (ddi_get_lbolt() >= end_time))) {
5083 				probed = 0;
5084 				mutex_enter(&DEVI(pdip)->devi_lock);
5085 				DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
5086 				mutex_exit(&DEVI(pdip)->devi_lock);
5087 				goto again;
5088 			}
5089 		}
5090 
5091 		/* break out of for(;;) if time expired */
5092 		if ((timeout <= 0) || (ddi_get_lbolt() >= end_time))
5093 			break;
5094 
5095 		/*
5096 		 * Child not found, exit and wait for asynchronous enumeration
5097 		 * to add child (or timeout). The addition of a new child (vhci
5098 		 * or phci) requires the asynchronous enumeration thread to
5099 		 * ndi_devi_enter/ndi_devi_exit. This exit will signal devi_cv
5100 		 * and cause us to return from ndi_devi_exit_and_wait, after
5101 		 * which we loop and search for the requested child again.
5102 		 */
5103 		NDI_DEBUG(flags, (CE_CONT,
5104 		    "%s%d: waiting for child %s@%s, timeout %ld",
5105 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
5106 		    name, addr, timeout));
5107 		if (vdip) {
5108 			/*
5109 			 * Mark vHCI for pHCI ndi_devi_exit broadcast.
5110 			 */
5111 			mutex_enter(&DEVI(vdip)->devi_lock);
5112 			DEVI(vdip)->devi_flags |=
5113 			    DEVI_PHCI_SIGNALS_VHCI;
5114 			mutex_exit(&DEVI(vdip)->devi_lock);
5115 			ndi_devi_exit(pdip, p_circ);
5116 
5117 			/*
5118 			 * NB: There is a small race window from above
5119 			 * ndi_devi_exit() of pdip to cv_wait() in
5120 			 * ndi_devi_exit_and_wait() which can result in
5121 			 * not immediately finding a new pHCI child
5122 			 * of a pHCI that uses NDI_MDI_FAILBACK.
5123 			 */
5124 			ndi_devi_exit_and_wait(vdip, v_circ, end_time);
5125 		} else {
5126 			ndi_devi_exit_and_wait(pdip, p_circ, end_time);
5127 		}
5128 	}
5129 
5130 	/* done with paddr, fixup i_ddi_parse_name '@'->'\0' change */
5131 	if (addr && *addr != '\0')
5132 		*(addr - 1) = '@';
5133 
5134 	/* attach and hold the child, returning pointer to child */
5135 	if (cdip && (devi_attach_node(cdip, flags) == NDI_SUCCESS)) {
5136 		ndi_hold_devi(cdip);
5137 		*cdipp = cdip;
5138 	}
5139 
5140 	ndi_devi_exit(pdip, p_circ);
5141 	if (vdip)
5142 		ndi_devi_exit(vdip, v_circ);
5143 	return (*cdipp ? NDI_SUCCESS : NDI_FAILURE);
5144 }
5145 
5146 /*
5147  * Enumerate and attach a child specified by name 'devnm'.
5148  * Called by devfs lookup and DR to perform a BUS_CONFIG_ONE.
5149  * Note: devfs does not make use of NDI_CONFIG to configure
5150  * an entire branch.
5151  */
5152 int
5153 ndi_devi_config_one(dev_info_t *dip, char *devnm, dev_info_t **dipp, int flags)
5154 {
5155 	int error;
5156 	int (*f)();
5157 	int branch_event = 0;
5158 
5159 	ASSERT(dipp);
5160 	ASSERT(i_ddi_devi_attached(dip));
5161 
5162 	NDI_CONFIG_DEBUG((CE_CONT,
5163 	    "ndi_devi_config_one: par = %s%d (%p), child = %s\n",
5164 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, devnm));
5165 
5166 	if (pm_pre_config(dip, devnm) != DDI_SUCCESS)
5167 		return (NDI_FAILURE);
5168 
5169 	if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
5170 	    (flags & NDI_CONFIG)) {
5171 		flags |= NDI_BRANCH_EVENT_OP;
5172 		branch_event = 1;
5173 	}
5174 
5175 	if ((DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
5176 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
5177 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
5178 		error = devi_config_one(dip, devnm, dipp, flags, 0);
5179 	} else {
5180 		/* call bus_config entry point */
5181 		error = (*f)(dip, flags, BUS_CONFIG_ONE, (void *)devnm, dipp);
5182 	}
5183 
5184 	if (error || (flags & NDI_CONFIG) == 0) {
5185 		pm_post_config(dip, devnm);
5186 		return (error);
5187 	}
5188 
5189 	/*
5190 	 * DR usage (i.e. call with NDI_CONFIG) recursively configures
5191 	 * grandchildren, performing a BUS_CONFIG_ALL from the node attached
5192 	 * by the BUS_CONFIG_ONE.
5193 	 */
5194 	ASSERT(*dipp);
5195 
5196 	error = devi_config_common(*dipp, flags, DDI_MAJOR_T_NONE);
5197 
5198 	pm_post_config(dip, devnm);
5199 
5200 	if (branch_event)
5201 		(void) i_log_devfs_branch_add(*dipp);
5202 
5203 	return (error);
5204 }
5205 
5206 
5207 /*
5208  * Enumerate and attach a child specified by name 'devnm'.
5209  * Called during configure the OBP options. This configures
5210  * only one node.
5211  */
5212 static int
5213 ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
5214     dev_info_t **childp, int flags)
5215 {
5216 	int error;
5217 	int (*f)();
5218 
5219 	ASSERT(childp);
5220 	ASSERT(i_ddi_devi_attached(parent));
5221 
5222 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_config_obp_args: "
5223 	    "par = %s%d (%p), child = %s\n", ddi_driver_name(parent),
5224 	    ddi_get_instance(parent), (void *)parent, devnm));
5225 
5226 	if ((DEVI(parent)->devi_ops->devo_bus_ops == NULL) ||
5227 	    (DEVI(parent)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
5228 	    (f = DEVI(parent)->devi_ops->devo_bus_ops->bus_config) == NULL) {
5229 		error = NDI_FAILURE;
5230 	} else {
5231 		/* call bus_config entry point */
5232 		error = (*f)(parent, flags,
5233 		    BUS_CONFIG_OBP_ARGS, (void *)devnm, childp);
5234 	}
5235 	return (error);
5236 }
5237 
5238 /*
5239  * Pay attention, the following is a bit tricky:
5240  * There are three possible cases when constraints are applied
5241  *
5242  *	- A constraint is applied and the offline is disallowed.
5243  *	  Simply return failure and block the offline
5244  *
5245  *	- A constraint is applied and the offline is allowed.
5246  *	  Mark the dip as having passed the constraint and allow
5247  *	  offline to proceed.
5248  *
5249  *	- A constraint is not applied. Allow the offline to proceed for now.
5250  *
5251  * In the latter two cases we allow the offline to proceed. If the
5252  * offline succeeds (no users) everything is fine. It is ok for an unused
5253  * device to be offlined even if no constraints were imposed on the offline.
5254  * If the offline fails because there are users, we look at the constraint
5255  * flag on the dip. If the constraint flag is set (implying that it passed
5256  * a constraint) we allow the dip to be retired. If not, we don't allow
5257  * the retire. This ensures that we don't allow unconstrained retire.
5258  */
5259 int
5260 e_ddi_offline_notify(dev_info_t *dip)
5261 {
5262 	int retval;
5263 	int constraint;
5264 	int failure;
5265 
5266 	RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): entered: dip=%p",
5267 	    (void *) dip));
5268 
5269 	constraint = 0;
5270 	failure = 0;
5271 
5272 	/*
5273 	 * Start with userland constraints first - applied via device contracts
5274 	 */
5275 	retval = contract_device_offline(dip, DDI_DEV_T_ANY, 0);
5276 	switch (retval) {
5277 	case CT_NACK:
5278 		RIO_DEBUG((CE_NOTE, "Received NACK for dip=%p", (void *)dip));
5279 		failure = 1;
5280 		goto out;
5281 	case CT_ACK:
5282 		constraint = 1;
5283 		RIO_DEBUG((CE_NOTE, "Received ACK for dip=%p", (void *)dip));
5284 		break;
5285 	case CT_NONE:
5286 		/* no contracts */
5287 		RIO_DEBUG((CE_NOTE, "No contracts on dip=%p", (void *)dip));
5288 		break;
5289 	default:
5290 		ASSERT(retval == CT_NONE);
5291 	}
5292 
5293 	/*
5294 	 * Next, use LDI to impose kernel constraints
5295 	 */
5296 	retval = ldi_invoke_notify(dip, DDI_DEV_T_ANY, 0, LDI_EV_OFFLINE, NULL);
5297 	switch (retval) {
5298 	case LDI_EV_FAILURE:
5299 		contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_FAILURE);
5300 		RIO_DEBUG((CE_NOTE, "LDI callback failed on dip=%p",
5301 		    (void *)dip));
5302 		failure = 1;
5303 		goto out;
5304 	case LDI_EV_SUCCESS:
5305 		constraint = 1;
5306 		RIO_DEBUG((CE_NOTE, "LDI callback success on dip=%p",
5307 		    (void *)dip));
5308 		break;
5309 	case LDI_EV_NONE:
5310 		/* no matching LDI callbacks */
5311 		RIO_DEBUG((CE_NOTE, "No LDI callbacks for dip=%p",
5312 		    (void *)dip));
5313 		break;
5314 	default:
5315 		ASSERT(retval == LDI_EV_NONE);
5316 	}
5317 
5318 out:
5319 	mutex_enter(&(DEVI(dip)->devi_lock));
5320 	if ((DEVI(dip)->devi_flags & DEVI_RETIRING) && failure) {
5321 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): setting "
5322 		    "BLOCKED flag. dip=%p", (void *)dip));
5323 		DEVI(dip)->devi_flags |= DEVI_R_BLOCKED;
5324 		if (DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT) {
5325 			RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): "
5326 			    "blocked. clearing RCM CONSTRAINT flag. dip=%p",
5327 			    (void *)dip));
5328 			DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
5329 		}
5330 	} else if ((DEVI(dip)->devi_flags & DEVI_RETIRING) && constraint) {
5331 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): setting "
5332 		    "CONSTRAINT flag. dip=%p", (void *)dip));
5333 		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
5334 	} else if ((DEVI(dip)->devi_flags & DEVI_RETIRING) &&
5335 	    DEVI(dip)->devi_ref == 0) {
5336 		/* also allow retire if device is not in use */
5337 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): device not in "
5338 		    "use. Setting CONSTRAINT flag. dip=%p", (void *)dip));
5339 		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
5340 	} else {
5341 		/*
5342 		 * Note: We cannot ASSERT here that DEVI_R_CONSTRAINT is
5343 		 * not set, since other sources (such as RCM) may have
5344 		 * set the flag.
5345 		 */
5346 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): not setting "
5347 		    "constraint flag. dip=%p", (void *)dip));
5348 	}
5349 	mutex_exit(&(DEVI(dip)->devi_lock));
5350 
5351 
5352 	RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): exit: dip=%p",
5353 	    (void *) dip));
5354 
5355 	return (failure ? DDI_FAILURE : DDI_SUCCESS);
5356 }
5357 
5358 void
5359 e_ddi_offline_finalize(dev_info_t *dip, int result)
5360 {
5361 	RIO_DEBUG((CE_NOTE, "e_ddi_offline_finalize(): entry: result=%s, "
5362 	    "dip=%p", result == DDI_SUCCESS ? "SUCCESS" : "FAILURE",
5363 	    (void *)dip));
5364 
5365 	contract_device_negend(dip, DDI_DEV_T_ANY, 0,  result == DDI_SUCCESS ?
5366 	    CT_EV_SUCCESS : CT_EV_FAILURE);
5367 
5368 	ldi_invoke_finalize(dip, DDI_DEV_T_ANY, 0,
5369 	    LDI_EV_OFFLINE, result == DDI_SUCCESS ?
5370 	    LDI_EV_SUCCESS : LDI_EV_FAILURE, NULL);
5371 
5372 	RIO_VERBOSE((CE_NOTE, "e_ddi_offline_finalize(): exit: dip=%p",
5373 	    (void *)dip));
5374 }
5375 
5376 void
5377 e_ddi_degrade_finalize(dev_info_t *dip)
5378 {
5379 	RIO_DEBUG((CE_NOTE, "e_ddi_degrade_finalize(): entry: "
5380 	    "result always = DDI_SUCCESS, dip=%p", (void *)dip));
5381 
5382 	contract_device_degrade(dip, DDI_DEV_T_ANY, 0);
5383 	contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_SUCCESS);
5384 
5385 	ldi_invoke_finalize(dip, DDI_DEV_T_ANY, 0, LDI_EV_DEGRADE,
5386 	    LDI_EV_SUCCESS, NULL);
5387 
5388 	RIO_VERBOSE((CE_NOTE, "e_ddi_degrade_finalize(): exit: dip=%p",
5389 	    (void *)dip));
5390 }
5391 
5392 void
5393 e_ddi_undegrade_finalize(dev_info_t *dip)
5394 {
5395 	RIO_DEBUG((CE_NOTE, "e_ddi_undegrade_finalize(): entry: "
5396 	    "result always = DDI_SUCCESS, dip=%p", (void *)dip));
5397 
5398 	contract_device_undegrade(dip, DDI_DEV_T_ANY, 0);
5399 	contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_SUCCESS);
5400 
5401 	RIO_VERBOSE((CE_NOTE, "e_ddi_undegrade_finalize(): exit: dip=%p",
5402 	    (void *)dip));
5403 }
5404 
5405 /*
5406  * detach a node with parent already held busy
5407  */
5408 static int
5409 devi_detach_node(dev_info_t *dip, uint_t flags)
5410 {
5411 	dev_info_t *pdip = ddi_get_parent(dip);
5412 	int ret = NDI_SUCCESS;
5413 	ddi_eventcookie_t cookie;
5414 
5415 	ASSERT(pdip && DEVI_BUSY_OWNED(pdip));
5416 
5417 	/*
5418 	 * Invoke notify if offlining
5419 	 */
5420 	if (flags & NDI_DEVI_OFFLINE) {
5421 		RIO_DEBUG((CE_NOTE, "devi_detach_node: offlining dip=%p",
5422 		    (void *)dip));
5423 		if (e_ddi_offline_notify(dip) != DDI_SUCCESS) {
5424 			RIO_DEBUG((CE_NOTE, "devi_detach_node: offline NACKed"
5425 			    "dip=%p", (void *)dip));
5426 			return (NDI_FAILURE);
5427 		}
5428 	}
5429 
5430 	if (flags & NDI_POST_EVENT) {
5431 		if (i_ddi_devi_attached(pdip)) {
5432 			if (ddi_get_eventcookie(dip, DDI_DEVI_REMOVE_EVENT,
5433 			    &cookie) == NDI_SUCCESS)
5434 				(void) ndi_post_event(dip, dip, cookie, NULL);
5435 		}
5436 	}
5437 
5438 	if (i_ddi_detachchild(dip, flags) != DDI_SUCCESS) {
5439 		if (flags & NDI_DEVI_OFFLINE) {
5440 			RIO_DEBUG((CE_NOTE, "devi_detach_node: offline failed."
5441 			    " Calling e_ddi_offline_finalize with result=%d. "
5442 			    "dip=%p", DDI_FAILURE, (void *)dip));
5443 			e_ddi_offline_finalize(dip, DDI_FAILURE);
5444 		}
5445 		return (NDI_FAILURE);
5446 	}
5447 
5448 	if (flags & NDI_DEVI_OFFLINE) {
5449 		RIO_DEBUG((CE_NOTE, "devi_detach_node: offline succeeded."
5450 		    " Calling e_ddi_offline_finalize with result=%d, "
5451 		    "dip=%p", DDI_SUCCESS, (void *)dip));
5452 		e_ddi_offline_finalize(dip, DDI_SUCCESS);
5453 	}
5454 
5455 	if (flags & NDI_AUTODETACH)
5456 		return (NDI_SUCCESS);
5457 
5458 	/*
5459 	 * For DR, even bound nodes may need to have offline
5460 	 * flag set.
5461 	 */
5462 	if (flags & NDI_DEVI_OFFLINE) {
5463 		mutex_enter(&(DEVI(dip)->devi_lock));
5464 		DEVI_SET_DEVICE_OFFLINE(dip);
5465 		mutex_exit(&(DEVI(dip)->devi_lock));
5466 	}
5467 
5468 	if (i_ddi_node_state(dip) == DS_INITIALIZED) {
5469 		char *path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
5470 		(void) ddi_pathname(dip, path);
5471 		if (flags & NDI_DEVI_OFFLINE)
5472 			i_ndi_devi_report_status_change(dip, path);
5473 
5474 		if (need_remove_event(dip, flags)) {
5475 			(void) i_log_devfs_remove_devinfo(path,
5476 			    i_ddi_devi_class(dip),
5477 			    (char *)ddi_driver_name(dip),
5478 			    ddi_get_instance(dip),
5479 			    flags);
5480 			mutex_enter(&(DEVI(dip)->devi_lock));
5481 			DEVI_SET_EVREMOVE(dip);
5482 			mutex_exit(&(DEVI(dip)->devi_lock));
5483 		}
5484 		kmem_free(path, MAXPATHLEN);
5485 	}
5486 
5487 	if (flags & (NDI_UNCONFIG | NDI_DEVI_REMOVE)) {
5488 		ret = ddi_uninitchild(dip);
5489 		if (ret == NDI_SUCCESS) {
5490 			/*
5491 			 * Remove uninitialized pseudo nodes because
5492 			 * system props are lost and the node cannot be
5493 			 * reattached.
5494 			 */
5495 			if (!ndi_dev_is_persistent_node(dip))
5496 				flags |= NDI_DEVI_REMOVE;
5497 
5498 			if (flags & NDI_DEVI_REMOVE)
5499 				ret = ddi_remove_child(dip, 0);
5500 		}
5501 	}
5502 
5503 	return (ret);
5504 }
5505 
5506 /*
5507  * unconfigure immediate children of bus nexus device
5508  */
5509 static int
5510 unconfig_immediate_children(
5511 	dev_info_t *dip,
5512 	dev_info_t **dipp,
5513 	int flags,
5514 	major_t major)
5515 {
5516 	int rv = NDI_SUCCESS;
5517 	int circ, vcirc;
5518 	dev_info_t *child;
5519 	dev_info_t *vdip = NULL;
5520 	dev_info_t *next;
5521 
5522 	ASSERT(dipp == NULL || *dipp == NULL);
5523 
5524 	/*
5525 	 * Scan forward to see if we will be processing a pHCI child. If we
5526 	 * have a child that is a pHCI and vHCI and pHCI are not siblings then
5527 	 * enter vHCI before parent(pHCI) to prevent deadlock with mpxio
5528 	 * Client power management operations.
5529 	 */
5530 	ndi_devi_enter(dip, &circ);
5531 	for (child = ddi_get_child(dip); child;
5532 	    child = ddi_get_next_sibling(child)) {
5533 		/* skip same nodes we skip below */
5534 		if (((major != DDI_MAJOR_T_NONE) &&
5535 		    (major != ddi_driver_major(child))) ||
5536 		    ((flags & NDI_AUTODETACH) && !is_leaf_node(child)))
5537 			continue;
5538 
5539 		if (MDI_PHCI(child)) {
5540 			vdip = mdi_devi_get_vdip(child);
5541 			/*
5542 			 * If vHCI and vHCI is not a sibling of pHCI
5543 			 * then enter in (vHCI, parent(pHCI)) order.
5544 			 */
5545 			if (vdip && (ddi_get_parent(vdip) != dip)) {
5546 				ndi_devi_exit(dip, circ);
5547 
5548 				/* use mdi_devi_enter ordering */
5549 				ndi_devi_enter(vdip, &vcirc);
5550 				ndi_devi_enter(dip, &circ);
5551 				break;
5552 			} else
5553 				vdip = NULL;
5554 		}
5555 	}
5556 
5557 	child = ddi_get_child(dip);
5558 	while (child) {
5559 		next = ddi_get_next_sibling(child);
5560 
5561 		if ((major != DDI_MAJOR_T_NONE) &&
5562 		    (major != ddi_driver_major(child))) {
5563 			child = next;
5564 			continue;
5565 		}
5566 
5567 		/* skip nexus nodes during autodetach */
5568 		if ((flags & NDI_AUTODETACH) && !is_leaf_node(child)) {
5569 			child = next;
5570 			continue;
5571 		}
5572 
5573 		if (devi_detach_node(child, flags) != NDI_SUCCESS) {
5574 			if (dipp && *dipp == NULL) {
5575 				ndi_hold_devi(child);
5576 				*dipp = child;
5577 			}
5578 			rv = NDI_FAILURE;
5579 		}
5580 
5581 		/*
5582 		 * Continue upon failure--best effort algorithm
5583 		 */
5584 		child = next;
5585 	}
5586 
5587 	ndi_devi_exit(dip, circ);
5588 	if (vdip)
5589 		ndi_devi_exit(vdip, vcirc);
5590 
5591 	return (rv);
5592 }
5593 
5594 /*
5595  * unconfigure grand children of bus nexus device
5596  */
5597 static int
5598 unconfig_grand_children(
5599 	dev_info_t *dip,
5600 	dev_info_t **dipp,
5601 	int flags,
5602 	major_t major,
5603 	struct brevq_node **brevqp)
5604 {
5605 	struct mt_config_handle *hdl;
5606 
5607 	if (brevqp)
5608 		*brevqp = NULL;
5609 
5610 	/* multi-threaded configuration of child nexus */
5611 	hdl = mt_config_init(dip, dipp, flags, major, MT_UNCONFIG_OP, brevqp);
5612 	mt_config_children(hdl);
5613 
5614 	return (mt_config_fini(hdl));	/* wait for threads to exit */
5615 }
5616 
5617 /*
5618  * Unconfigure children/descendants of the dip.
5619  *
5620  * If brevqp is not NULL, on return *brevqp is set to a queue of dip's
5621  * child devinames for which branch remove events need to be generated.
5622  */
5623 static int
5624 devi_unconfig_common(
5625 	dev_info_t *dip,
5626 	dev_info_t **dipp,
5627 	int flags,
5628 	major_t major,
5629 	struct brevq_node **brevqp)
5630 {
5631 	int rv;
5632 	int pm_cookie;
5633 	int (*f)();
5634 	ddi_bus_config_op_t bus_op;
5635 
5636 	if (dipp)
5637 		*dipp = NULL;
5638 	if (brevqp)
5639 		*brevqp = NULL;
5640 
5641 	/*
5642 	 * Power up the dip if it is powered off.  If the flag bit
5643 	 * NDI_AUTODETACH is set and the dip is not at its full power,
5644 	 * skip the rest of the branch.
5645 	 */
5646 	if (pm_pre_unconfig(dip, flags, &pm_cookie, NULL) != DDI_SUCCESS)
5647 		return ((flags & NDI_AUTODETACH) ? NDI_SUCCESS :
5648 		    NDI_FAILURE);
5649 
5650 	/*
5651 	 * Some callers, notably SCSI, need to clear out the devfs
5652 	 * cache together with the unconfig to prevent stale entries.
5653 	 */
5654 	if (flags & NDI_DEVFS_CLEAN)
5655 		(void) devfs_clean(dip, NULL, 0);
5656 
5657 	rv = unconfig_grand_children(dip, dipp, flags, major, brevqp);
5658 
5659 	if ((rv != NDI_SUCCESS) && ((flags & NDI_AUTODETACH) == 0)) {
5660 		if (brevqp && *brevqp) {
5661 			log_and_free_br_events_on_grand_children(dip, *brevqp);
5662 			free_brevq(*brevqp);
5663 			*brevqp = NULL;
5664 		}
5665 		pm_post_unconfig(dip, pm_cookie, NULL);
5666 		return (rv);
5667 	}
5668 
5669 	if (dipp && *dipp) {
5670 		ndi_rele_devi(*dipp);
5671 		*dipp = NULL;
5672 	}
5673 
5674 	/*
5675 	 * It is possible to have a detached nexus with children
5676 	 * and grandchildren (for example: a branch consisting
5677 	 * entirely of bound nodes.) Since the nexus is detached
5678 	 * the bus_unconfig entry point cannot be used to remove
5679 	 * or unconfigure the descendants.
5680 	 */
5681 	if (!i_ddi_devi_attached(dip) ||
5682 	    (DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
5683 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
5684 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
5685 		rv = unconfig_immediate_children(dip, dipp, flags, major);
5686 	} else {
5687 		/*
5688 		 * call bus_unconfig entry point
5689 		 * It should reset nexus flags if unconfigure succeeds.
5690 		 */
5691 		bus_op = (major == DDI_MAJOR_T_NONE) ?
5692 		    BUS_UNCONFIG_ALL : BUS_UNCONFIG_DRIVER;
5693 		rv = (*f)(dip, flags, bus_op, (void *)(uintptr_t)major);
5694 	}
5695 
5696 	pm_post_unconfig(dip, pm_cookie, NULL);
5697 
5698 	if (brevqp && *brevqp)
5699 		cleanup_br_events_on_grand_children(dip, brevqp);
5700 
5701 	return (rv);
5702 }
5703 
5704 /*
5705  * called by devfs/framework to unconfigure children bound to major
5706  * If NDI_AUTODETACH is specified, this is invoked by either the
5707  * moduninstall daemon or the modunload -i 0 command.
5708  */
5709 int
5710 ndi_devi_unconfig_driver(dev_info_t *dip, int flags, major_t major)
5711 {
5712 	NDI_CONFIG_DEBUG((CE_CONT,
5713 	    "ndi_devi_unconfig_driver: par = %s%d (%p), flags = 0x%x\n",
5714 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
5715 
5716 	return (devi_unconfig_common(dip, NULL, flags, major, NULL));
5717 }
5718 
5719 int
5720 ndi_devi_unconfig(dev_info_t *dip, int flags)
5721 {
5722 	NDI_CONFIG_DEBUG((CE_CONT,
5723 	    "ndi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
5724 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
5725 
5726 	return (devi_unconfig_common(dip, NULL, flags, DDI_MAJOR_T_NONE, NULL));
5727 }
5728 
5729 int
5730 e_ddi_devi_unconfig(dev_info_t *dip, dev_info_t **dipp, int flags)
5731 {
5732 	NDI_CONFIG_DEBUG((CE_CONT,
5733 	    "e_ddi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
5734 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
5735 
5736 	return (devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE, NULL));
5737 }
5738 
5739 /*
5740  * Unconfigure child by name
5741  */
5742 static int
5743 devi_unconfig_one(dev_info_t *pdip, char *devnm, int flags)
5744 {
5745 	int		rv, circ;
5746 	dev_info_t	*child;
5747 	dev_info_t	*vdip = NULL;
5748 	int		v_circ;
5749 
5750 	ndi_devi_enter(pdip, &circ);
5751 	child = ndi_devi_findchild(pdip, devnm);
5752 
5753 	/*
5754 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
5755 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
5756 	 * management operations.
5757 	 */
5758 	if (child && MDI_PHCI(child)) {
5759 		vdip = mdi_devi_get_vdip(child);
5760 		if (vdip && (ddi_get_parent(vdip) != pdip)) {
5761 			ndi_devi_exit(pdip, circ);
5762 
5763 			/* use mdi_devi_enter ordering */
5764 			ndi_devi_enter(vdip, &v_circ);
5765 			ndi_devi_enter(pdip, &circ);
5766 			child = ndi_devi_findchild(pdip, devnm);
5767 		} else
5768 			vdip = NULL;
5769 	}
5770 
5771 	if (child) {
5772 		rv = devi_detach_node(child, flags);
5773 	} else {
5774 		NDI_CONFIG_DEBUG((CE_CONT,
5775 		    "devi_unconfig_one: %s not found\n", devnm));
5776 		rv = NDI_SUCCESS;
5777 	}
5778 
5779 	ndi_devi_exit(pdip, circ);
5780 	if (vdip)
5781 		ndi_devi_exit(vdip, v_circ);
5782 
5783 	return (rv);
5784 }
5785 
5786 int
5787 ndi_devi_unconfig_one(
5788 	dev_info_t *pdip,
5789 	char *devnm,
5790 	dev_info_t **dipp,
5791 	int flags)
5792 {
5793 	int		(*f)();
5794 	int		circ, rv;
5795 	int		pm_cookie;
5796 	dev_info_t	*child;
5797 	dev_info_t	*vdip = NULL;
5798 	int		v_circ;
5799 	struct brevq_node *brevq = NULL;
5800 
5801 	ASSERT(i_ddi_devi_attached(pdip));
5802 
5803 	NDI_CONFIG_DEBUG((CE_CONT,
5804 	    "ndi_devi_unconfig_one: par = %s%d (%p), child = %s\n",
5805 	    ddi_driver_name(pdip), ddi_get_instance(pdip),
5806 	    (void *)pdip, devnm));
5807 
5808 	if (pm_pre_unconfig(pdip, flags, &pm_cookie, devnm) != DDI_SUCCESS)
5809 		return (NDI_FAILURE);
5810 
5811 	if (dipp)
5812 		*dipp = NULL;
5813 
5814 	ndi_devi_enter(pdip, &circ);
5815 	child = ndi_devi_findchild(pdip, devnm);
5816 
5817 	/*
5818 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
5819 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
5820 	 * management operations.
5821 	 */
5822 	if (child && MDI_PHCI(child)) {
5823 		vdip = mdi_devi_get_vdip(child);
5824 		if (vdip && (ddi_get_parent(vdip) != pdip)) {
5825 			ndi_devi_exit(pdip, circ);
5826 
5827 			/* use mdi_devi_enter ordering */
5828 			ndi_devi_enter(vdip, &v_circ);
5829 			ndi_devi_enter(pdip, &circ);
5830 			child = ndi_devi_findchild(pdip, devnm);
5831 		} else
5832 			vdip = NULL;
5833 	}
5834 
5835 	if (child == NULL) {
5836 		NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_unconfig_one: %s"
5837 		    " not found\n", devnm));
5838 		rv = NDI_SUCCESS;
5839 		goto out;
5840 	}
5841 
5842 	/*
5843 	 * Unconfigure children/descendants of named child
5844 	 */
5845 	rv = devi_unconfig_branch(child, dipp, flags | NDI_UNCONFIG, &brevq);
5846 	if (rv != NDI_SUCCESS)
5847 		goto out;
5848 
5849 	init_bound_node_ev(pdip, child, flags);
5850 
5851 	if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
5852 	    (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
5853 	    (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
5854 		rv = devi_detach_node(child, flags);
5855 	} else {
5856 		/* call bus_config entry point */
5857 		rv = (*f)(pdip, flags, BUS_UNCONFIG_ONE, (void *)devnm);
5858 	}
5859 
5860 	if (brevq) {
5861 		if (rv != NDI_SUCCESS)
5862 			log_and_free_brevq_dip(child, brevq);
5863 		else
5864 			free_brevq(brevq);
5865 	}
5866 
5867 	if (dipp && rv != NDI_SUCCESS) {
5868 		ndi_hold_devi(child);
5869 		ASSERT(*dipp == NULL);
5870 		*dipp = child;
5871 	}
5872 
5873 out:
5874 	ndi_devi_exit(pdip, circ);
5875 	if (vdip)
5876 		ndi_devi_exit(vdip, v_circ);
5877 
5878 	pm_post_unconfig(pdip, pm_cookie, devnm);
5879 
5880 	return (rv);
5881 }
5882 
5883 struct async_arg {
5884 	dev_info_t *dip;
5885 	uint_t flags;
5886 };
5887 
5888 /*
5889  * Common async handler for:
5890  *	ndi_devi_bind_driver_async
5891  *	ndi_devi_online_async
5892  */
5893 static int
5894 i_ndi_devi_async_common(dev_info_t *dip, uint_t flags, void (*func)())
5895 {
5896 	int tqflag;
5897 	int kmflag;
5898 	struct async_arg *arg;
5899 	dev_info_t *pdip = ddi_get_parent(dip);
5900 
5901 	ASSERT(pdip);
5902 	ASSERT(DEVI(pdip)->devi_taskq);
5903 	ASSERT(ndi_dev_is_persistent_node(dip));
5904 
5905 	if (flags & NDI_NOSLEEP) {
5906 		kmflag = KM_NOSLEEP;
5907 		tqflag = TQ_NOSLEEP;
5908 	} else {
5909 		kmflag = KM_SLEEP;
5910 		tqflag = TQ_SLEEP;
5911 	}
5912 
5913 	arg = kmem_alloc(sizeof (*arg), kmflag);
5914 	if (arg == NULL)
5915 		goto fail;
5916 
5917 	arg->flags = flags;
5918 	arg->dip = dip;
5919 	if (ddi_taskq_dispatch(DEVI(pdip)->devi_taskq, func, arg, tqflag) ==
5920 	    DDI_SUCCESS) {
5921 		return (NDI_SUCCESS);
5922 	}
5923 
5924 fail:
5925 	NDI_CONFIG_DEBUG((CE_CONT, "%s%d: ddi_taskq_dispatch failed",
5926 	    ddi_driver_name(pdip), ddi_get_instance(pdip)));
5927 
5928 	if (arg)
5929 		kmem_free(arg, sizeof (*arg));
5930 	return (NDI_FAILURE);
5931 }
5932 
5933 static void
5934 i_ndi_devi_bind_driver_cb(struct async_arg *arg)
5935 {
5936 	(void) ndi_devi_bind_driver(arg->dip, arg->flags);
5937 	kmem_free(arg, sizeof (*arg));
5938 }
5939 
5940 int
5941 ndi_devi_bind_driver_async(dev_info_t *dip, uint_t flags)
5942 {
5943 	return (i_ndi_devi_async_common(dip, flags,
5944 	    (void (*)())i_ndi_devi_bind_driver_cb));
5945 }
5946 
5947 /*
5948  * place the devinfo in the ONLINE state.
5949  */
5950 int
5951 ndi_devi_online(dev_info_t *dip, uint_t flags)
5952 {
5953 	int circ, rv;
5954 	dev_info_t *pdip = ddi_get_parent(dip);
5955 	int branch_event = 0;
5956 
5957 	ASSERT(pdip);
5958 
5959 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_online: %s%d (%p)\n",
5960 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
5961 
5962 	ndi_devi_enter(pdip, &circ);
5963 	/* bind child before merging .conf nodes */
5964 	rv = i_ndi_config_node(dip, DS_BOUND, flags);
5965 	if (rv != NDI_SUCCESS) {
5966 		ndi_devi_exit(pdip, circ);
5967 		return (rv);
5968 	}
5969 
5970 	/* merge .conf properties */
5971 	(void) i_ndi_make_spec_children(pdip, flags);
5972 
5973 	flags |= (NDI_DEVI_ONLINE | NDI_CONFIG);
5974 
5975 	if (flags & NDI_NO_EVENT) {
5976 		/*
5977 		 * Caller is specifically asking for not to generate an event.
5978 		 * Set the following flag so that devi_attach_node() don't
5979 		 * change the event state.
5980 		 */
5981 		flags |= NDI_NO_EVENT_STATE_CHNG;
5982 	}
5983 
5984 	if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
5985 	    ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip))) {
5986 		flags |= NDI_BRANCH_EVENT_OP;
5987 		branch_event = 1;
5988 	}
5989 
5990 	/*
5991 	 * devi_attach_node() may remove dip on failure
5992 	 */
5993 	if ((rv = devi_attach_node(dip, flags)) == NDI_SUCCESS) {
5994 		if ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip)) {
5995 			(void) ndi_devi_config(dip, flags);
5996 		}
5997 
5998 		if (branch_event)
5999 			(void) i_log_devfs_branch_add(dip);
6000 	}
6001 
6002 	ndi_devi_exit(pdip, circ);
6003 
6004 	/*
6005 	 * Notify devfs that we have a new node. Devfs needs to invalidate
6006 	 * cached directory contents.
6007 	 *
6008 	 * For PCMCIA devices, it is possible the pdip is not fully
6009 	 * attached. In this case, calling back into devfs will
6010 	 * result in a loop or assertion error. Hence, the check
6011 	 * on node state.
6012 	 *
6013 	 * If we own parent lock, this is part of a branch operation.
6014 	 * We skip the devfs_clean() step because the cache invalidation
6015 	 * is done higher up in the device tree.
6016 	 */
6017 	if (rv == NDI_SUCCESS && i_ddi_devi_attached(pdip) &&
6018 	    !DEVI_BUSY_OWNED(pdip))
6019 		(void) devfs_clean(pdip, NULL, 0);
6020 	return (rv);
6021 }
6022 
6023 static void
6024 i_ndi_devi_online_cb(struct async_arg *arg)
6025 {
6026 	(void) ndi_devi_online(arg->dip, arg->flags);
6027 	kmem_free(arg, sizeof (*arg));
6028 }
6029 
6030 int
6031 ndi_devi_online_async(dev_info_t *dip, uint_t flags)
6032 {
6033 	/* mark child as need config if requested. */
6034 	if (flags & NDI_CONFIG) {
6035 		mutex_enter(&(DEVI(dip)->devi_lock));
6036 		DEVI_SET_NDI_CONFIG(dip);
6037 		mutex_exit(&(DEVI(dip)->devi_lock));
6038 	}
6039 
6040 	return (i_ndi_devi_async_common(dip, flags,
6041 	    (void (*)())i_ndi_devi_online_cb));
6042 }
6043 
6044 /*
6045  * Take a device node Offline
6046  * To take a device Offline means to detach the device instance from
6047  * the driver and prevent devfs requests from re-attaching the device
6048  * instance.
6049  *
6050  * The flag NDI_DEVI_REMOVE causes removes the device node from
6051  * the driver list and the device tree. In this case, the device
6052  * is assumed to be removed from the system.
6053  */
6054 int
6055 ndi_devi_offline(dev_info_t *dip, uint_t flags)
6056 {
6057 	int		circ, rval = 0;
6058 	dev_info_t	*pdip = ddi_get_parent(dip);
6059 	dev_info_t	*vdip = NULL;
6060 	int		v_circ;
6061 	struct brevq_node *brevq = NULL;
6062 
6063 	ASSERT(pdip);
6064 
6065 	flags |= NDI_DEVI_OFFLINE;
6066 
6067 	/*
6068 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
6069 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
6070 	 * management operations.
6071 	 */
6072 	if (MDI_PHCI(dip)) {
6073 		vdip = mdi_devi_get_vdip(dip);
6074 		if (vdip && (ddi_get_parent(vdip) != pdip))
6075 			ndi_devi_enter(vdip, &v_circ);
6076 		else
6077 			vdip = NULL;
6078 	}
6079 	ndi_devi_enter(pdip, &circ);
6080 
6081 	if (i_ddi_node_state(dip) == DS_READY) {
6082 		/*
6083 		 * If dip is in DS_READY state, there may be cached dv_nodes
6084 		 * referencing this dip, so we invoke devfs code path.
6085 		 * Note that we must release busy changing on pdip to
6086 		 * avoid deadlock against devfs.
6087 		 */
6088 		char *devname = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
6089 		(void) ddi_deviname(dip, devname);
6090 
6091 		ndi_devi_exit(pdip, circ);
6092 		if (vdip)
6093 			ndi_devi_exit(vdip, v_circ);
6094 
6095 		/*
6096 		 * If we own parent lock, this is part of a branch
6097 		 * operation. We skip the devfs_clean() step.
6098 		 */
6099 		if (!DEVI_BUSY_OWNED(pdip))
6100 			(void) devfs_clean(pdip, devname + 1, DV_CLEAN_FORCE);
6101 		kmem_free(devname, MAXNAMELEN + 1);
6102 
6103 		rval = devi_unconfig_branch(dip, NULL, flags|NDI_UNCONFIG,
6104 		    &brevq);
6105 
6106 		if (rval)
6107 			return (NDI_FAILURE);
6108 
6109 		if (vdip)
6110 			ndi_devi_enter(vdip, &v_circ);
6111 		ndi_devi_enter(pdip, &circ);
6112 	}
6113 
6114 	init_bound_node_ev(pdip, dip, flags);
6115 
6116 	rval = devi_detach_node(dip, flags);
6117 	if (brevq) {
6118 		if (rval != NDI_SUCCESS)
6119 			log_and_free_brevq_dip(dip, brevq);
6120 		else
6121 			free_brevq(brevq);
6122 	}
6123 
6124 	ndi_devi_exit(pdip, circ);
6125 	if (vdip)
6126 		ndi_devi_exit(vdip, v_circ);
6127 
6128 	return (rval);
6129 }
6130 
6131 /*
6132  * Find the child dev_info node of parent nexus 'p' whose name
6133  * matches "cname@caddr".  Recommend use of ndi_devi_findchild() instead.
6134  */
6135 dev_info_t *
6136 ndi_devi_find(dev_info_t *pdip, char *cname, char *caddr)
6137 {
6138 	dev_info_t *child;
6139 	int circ;
6140 
6141 	if (pdip == NULL || cname == NULL || caddr == NULL)
6142 		return ((dev_info_t *)NULL);
6143 
6144 	ndi_devi_enter(pdip, &circ);
6145 	child = find_sibling(ddi_get_child(pdip), cname, caddr,
6146 	    FIND_NODE_BY_NODENAME, NULL);
6147 	ndi_devi_exit(pdip, circ);
6148 	return (child);
6149 }
6150 
6151 /*
6152  * Find the child dev_info node of parent nexus 'p' whose name
6153  * matches devname "name@addr".  Permits caller to hold the parent.
6154  */
6155 dev_info_t *
6156 ndi_devi_findchild(dev_info_t *pdip, char *devname)
6157 {
6158 	dev_info_t *child;
6159 	char	*cname, *caddr;
6160 	char	*devstr;
6161 
6162 	ASSERT(DEVI_BUSY_OWNED(pdip));
6163 
6164 	devstr = i_ddi_strdup(devname, KM_SLEEP);
6165 	i_ddi_parse_name(devstr, &cname, &caddr, NULL);
6166 
6167 	if (cname == NULL || caddr == NULL) {
6168 		kmem_free(devstr, strlen(devname)+1);
6169 		return ((dev_info_t *)NULL);
6170 	}
6171 
6172 	child = find_sibling(ddi_get_child(pdip), cname, caddr,
6173 	    FIND_NODE_BY_NODENAME, NULL);
6174 	kmem_free(devstr, strlen(devname)+1);
6175 	return (child);
6176 }
6177 
6178 /*
6179  * Misc. routines called by framework only
6180  */
6181 
6182 /*
6183  * Clear the DEVI_MADE_CHILDREN/DEVI_ATTACHED_CHILDREN flags
6184  * if new child spec has been added.
6185  */
6186 static int
6187 reset_nexus_flags(dev_info_t *dip, void *arg)
6188 {
6189 	struct hwc_spec	*list;
6190 	int		circ;
6191 
6192 	if (((DEVI(dip)->devi_flags & DEVI_MADE_CHILDREN) == 0) ||
6193 	    ((list = hwc_get_child_spec(dip, (major_t)(uintptr_t)arg)) == NULL))
6194 		return (DDI_WALK_CONTINUE);
6195 
6196 	hwc_free_spec_list(list);
6197 
6198 	/* coordinate child state update */
6199 	ndi_devi_enter(dip, &circ);
6200 	mutex_enter(&DEVI(dip)->devi_lock);
6201 	DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN | DEVI_ATTACHED_CHILDREN);
6202 	mutex_exit(&DEVI(dip)->devi_lock);
6203 	ndi_devi_exit(dip, circ);
6204 
6205 	return (DDI_WALK_CONTINUE);
6206 }
6207 
6208 /*
6209  * Helper functions, returns NULL if no memory.
6210  */
6211 
6212 /*
6213  * path_to_major:
6214  *
6215  * Return an alternate driver name binding for the leaf device
6216  * of the given pathname, if there is one. The purpose of this
6217  * function is to deal with generic pathnames. The default action
6218  * for platforms that can't do this (ie: x86 or any platform that
6219  * does not have prom_finddevice functionality, which matches
6220  * nodenames and unit-addresses without the drivers participation)
6221  * is to return DDI_MAJOR_T_NONE.
6222  *
6223  * Used in loadrootmodules() in the swapgeneric module to
6224  * associate a given pathname with a given leaf driver.
6225  *
6226  */
6227 major_t
6228 path_to_major(char *path)
6229 {
6230 	dev_info_t *dip;
6231 	char *p, *q;
6232 	pnode_t nodeid;
6233 	major_t major;
6234 
6235 	/* check for path-oriented alias */
6236 	major = ddi_name_to_major(path);
6237 	if ((major != DDI_MAJOR_T_NONE) &&
6238 	    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED)) {
6239 		NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s path bound %s\n",
6240 		    path, ddi_major_to_name(major)));
6241 		return (major);
6242 	}
6243 
6244 	/*
6245 	 * Get the nodeid of the given pathname, if such a mapping exists.
6246 	 */
6247 	dip = NULL;
6248 	nodeid = prom_finddevice(path);
6249 	if (nodeid != OBP_BADNODE) {
6250 		/*
6251 		 * Find the nodeid in our copy of the device tree and return
6252 		 * whatever name we used to bind this node to a driver.
6253 		 */
6254 		dip = e_ddi_nodeid_to_dip(nodeid);
6255 	}
6256 
6257 	if (dip == NULL) {
6258 		NDI_CONFIG_DEBUG((CE_WARN,
6259 		    "path_to_major: can't bind <%s>\n", path));
6260 		return (DDI_MAJOR_T_NONE);
6261 	}
6262 
6263 	/*
6264 	 * If we're bound to something other than the nodename,
6265 	 * note that in the message buffer and system log.
6266 	 */
6267 	p = ddi_binding_name(dip);
6268 	q = ddi_node_name(dip);
6269 	if (p && q && (strcmp(p, q) != 0))
6270 		NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s bound to %s\n",
6271 		    path, p));
6272 
6273 	major = ddi_name_to_major(p);
6274 
6275 	ndi_rele_devi(dip);		/* release e_ddi_nodeid_to_dip hold */
6276 
6277 	return (major);
6278 }
6279 
6280 /*
6281  * Return the held dip for the specified major and instance, attempting to do
6282  * an attach if specified. Return NULL if the devi can't be found or put in
6283  * the proper state. The caller must release the hold via ddi_release_devi if
6284  * a non-NULL value is returned.
6285  *
6286  * Some callers expect to be able to perform a hold_devi() while in a context
6287  * where using ndi_devi_enter() to ensure the hold might cause deadlock (see
6288  * open-from-attach code in consconfig_dacf.c). Such special-case callers
6289  * must ensure that an ndi_devi_enter(parent)/ndi_devi_hold() from a safe
6290  * context is already active. The hold_devi() implementation must accommodate
6291  * these callers.
6292  */
6293 static dev_info_t *
6294 hold_devi(major_t major, int instance, int flags)
6295 {
6296 	struct devnames	*dnp;
6297 	dev_info_t	*dip;
6298 	char		*path;
6299 
6300 	if ((major >= devcnt) || (instance == -1))
6301 		return (NULL);
6302 
6303 	/* try to find the instance in the per driver list */
6304 	dnp = &(devnamesp[major]);
6305 	LOCK_DEV_OPS(&(dnp->dn_lock));
6306 	for (dip = dnp->dn_head; dip;
6307 	    dip = (dev_info_t *)DEVI(dip)->devi_next) {
6308 		/* skip node if instance field is not valid */
6309 		if (i_ddi_node_state(dip) < DS_INITIALIZED)
6310 			continue;
6311 
6312 		/* look for instance match */
6313 		if (DEVI(dip)->devi_instance == instance) {
6314 			/*
6315 			 * To accommodate callers that can't block in
6316 			 * ndi_devi_enter() we do an ndi_devi_hold(), and
6317 			 * afterwards check that the node is in a state where
6318 			 * the hold prevents detach(). If we did not manage to
6319 			 * prevent detach then we ndi_rele_devi() and perform
6320 			 * the slow path below (which can result in a blocking
6321 			 * ndi_devi_enter() while driving attach top-down).
6322 			 * This code depends on the ordering of
6323 			 * DEVI_SET_DETACHING and the devi_ref check in the
6324 			 * detach_node() code path.
6325 			 */
6326 			ndi_hold_devi(dip);
6327 			if (i_ddi_devi_attached(dip) &&
6328 			    !DEVI_IS_DETACHING(dip)) {
6329 				UNLOCK_DEV_OPS(&(dnp->dn_lock));
6330 				return (dip);	/* fast-path with devi held */
6331 			}
6332 			ndi_rele_devi(dip);
6333 
6334 			/* try slow-path */
6335 			dip = NULL;
6336 			break;
6337 		}
6338 	}
6339 	ASSERT(dip == NULL);
6340 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
6341 
6342 	if (flags & E_DDI_HOLD_DEVI_NOATTACH)
6343 		return (NULL);		/* told not to drive attach */
6344 
6345 	/* slow-path may block, so it should not occur from interrupt */
6346 	ASSERT(!servicing_interrupt());
6347 	if (servicing_interrupt())
6348 		return (NULL);
6349 
6350 	/* reconstruct the path and drive attach by path through devfs. */
6351 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
6352 	if (e_ddi_majorinstance_to_path(major, instance, path) == 0)
6353 		dip = e_ddi_hold_devi_by_path(path, flags);
6354 	kmem_free(path, MAXPATHLEN);
6355 	return (dip);			/* with devi held */
6356 }
6357 
6358 /*
6359  * The {e_}ddi_hold_devi{_by_{instance|dev|path}} hold the devinfo node
6360  * associated with the specified arguments.  This hold should be released
6361  * by calling ddi_release_devi.
6362  *
6363  * The E_DDI_HOLD_DEVI_NOATTACH flag argument allows the caller to to specify
6364  * a failure return if the node is not already attached.
6365  *
6366  * NOTE: by the time we make e_ddi_hold_devi public, we should be able to reuse
6367  * ddi_hold_devi again.
6368  */
6369 dev_info_t *
6370 ddi_hold_devi_by_instance(major_t major, int instance, int flags)
6371 {
6372 	return (hold_devi(major, instance, flags));
6373 }
6374 
6375 dev_info_t *
6376 e_ddi_hold_devi_by_dev(dev_t dev, int flags)
6377 {
6378 	major_t	major = getmajor(dev);
6379 	dev_info_t	*dip;
6380 	struct dev_ops	*ops;
6381 	dev_info_t	*ddip = NULL;
6382 
6383 	dip = hold_devi(major, dev_to_instance(dev), flags);
6384 
6385 	/*
6386 	 * The rest of this routine is legacy support for drivers that
6387 	 * have broken DDI_INFO_DEVT2INSTANCE implementations but may have
6388 	 * functional DDI_INFO_DEVT2DEVINFO implementations.  This code will
6389 	 * diagnose inconsistency and, for maximum compatibility with legacy
6390 	 * drivers, give preference to the drivers DDI_INFO_DEVT2DEVINFO
6391 	 * implementation over the above derived dip based the driver's
6392 	 * DDI_INFO_DEVT2INSTANCE implementation. This legacy support should
6393 	 * be removed when DDI_INFO_DEVT2DEVINFO is deprecated.
6394 	 *
6395 	 * NOTE: The following code has a race condition. DEVT2DEVINFO
6396 	 *	returns a dip which is not held. By the time we ref ddip,
6397 	 *	it could have been freed. The saving grace is that for
6398 	 *	most drivers, the dip returned from hold_devi() is the
6399 	 *	same one as the one returned by DEVT2DEVINFO, so we are
6400 	 *	safe for drivers with the correct getinfo(9e) impl.
6401 	 */
6402 	if (((ops = ddi_hold_driver(major)) != NULL) &&
6403 	    CB_DRV_INSTALLED(ops) && ops->devo_getinfo)  {
6404 		if ((*ops->devo_getinfo)(NULL, DDI_INFO_DEVT2DEVINFO,
6405 		    (void *)dev, (void **)&ddip) != DDI_SUCCESS)
6406 			ddip = NULL;
6407 	}
6408 
6409 	/* give preference to the driver returned DEVT2DEVINFO dip */
6410 	if (ddip && (dip != ddip)) {
6411 #ifdef	DEBUG
6412 		cmn_err(CE_WARN, "%s: inconsistent getinfo(9E) implementation",
6413 		    ddi_driver_name(ddip));
6414 #endif	/* DEBUG */
6415 		ndi_hold_devi(ddip);
6416 		if (dip)
6417 			ndi_rele_devi(dip);
6418 		dip = ddip;
6419 	}
6420 
6421 	if (ops)
6422 		ddi_rele_driver(major);
6423 
6424 	return (dip);
6425 }
6426 
6427 /*
6428  * For compatibility only. Do not call this function!
6429  */
6430 dev_info_t *
6431 e_ddi_get_dev_info(dev_t dev, vtype_t type)
6432 {
6433 	dev_info_t *dip = NULL;
6434 	if (getmajor(dev) >= devcnt)
6435 		return (NULL);
6436 
6437 	switch (type) {
6438 	case VCHR:
6439 	case VBLK:
6440 		dip = e_ddi_hold_devi_by_dev(dev, 0);
6441 	default:
6442 		break;
6443 	}
6444 
6445 	/*
6446 	 * For compatibility reasons, we can only return the dip with
6447 	 * the driver ref count held. This is not a safe thing to do.
6448 	 * For certain broken third-party software, we are willing
6449 	 * to venture into unknown territory.
6450 	 */
6451 	if (dip) {
6452 		(void) ndi_hold_driver(dip);
6453 		ndi_rele_devi(dip);
6454 	}
6455 	return (dip);
6456 }
6457 
6458 dev_info_t *
6459 e_ddi_hold_devi_by_path(char *path, int flags)
6460 {
6461 	dev_info_t	*dip;
6462 
6463 	/* can't specify NOATTACH by path */
6464 	ASSERT(!(flags & E_DDI_HOLD_DEVI_NOATTACH));
6465 
6466 	return (resolve_pathname(path, &dip, NULL, NULL) ? NULL : dip);
6467 }
6468 
6469 void
6470 e_ddi_hold_devi(dev_info_t *dip)
6471 {
6472 	ndi_hold_devi(dip);
6473 }
6474 
6475 void
6476 ddi_release_devi(dev_info_t *dip)
6477 {
6478 	ndi_rele_devi(dip);
6479 }
6480 
6481 /*
6482  * Associate a streams queue with a devinfo node
6483  * NOTE: This function is called by STREAM driver's put procedure.
6484  *	It cannot block.
6485  */
6486 void
6487 ddi_assoc_queue_with_devi(queue_t *q, dev_info_t *dip)
6488 {
6489 	queue_t *rq = _RD(q);
6490 	struct stdata *stp;
6491 	vnode_t *vp;
6492 
6493 	/* set flag indicating that ddi_assoc_queue_with_devi was called */
6494 	mutex_enter(QLOCK(rq));
6495 	rq->q_flag |= _QASSOCIATED;
6496 	mutex_exit(QLOCK(rq));
6497 
6498 	/* get the vnode associated with the queue */
6499 	stp = STREAM(rq);
6500 	vp = stp->sd_vnode;
6501 	ASSERT(vp);
6502 
6503 	/* change the hardware association of the vnode */
6504 	spec_assoc_vp_with_devi(vp, dip);
6505 }
6506 
6507 /*
6508  * ddi_install_driver(name)
6509  *
6510  * Driver installation is currently a byproduct of driver loading.  This
6511  * may change.
6512  */
6513 int
6514 ddi_install_driver(char *name)
6515 {
6516 	major_t major = ddi_name_to_major(name);
6517 
6518 	if ((major == DDI_MAJOR_T_NONE) ||
6519 	    (ddi_hold_installed_driver(major) == NULL)) {
6520 		return (DDI_FAILURE);
6521 	}
6522 	ddi_rele_driver(major);
6523 	return (DDI_SUCCESS);
6524 }
6525 
6526 struct dev_ops *
6527 ddi_hold_driver(major_t major)
6528 {
6529 	return (mod_hold_dev_by_major(major));
6530 }
6531 
6532 
6533 void
6534 ddi_rele_driver(major_t major)
6535 {
6536 	mod_rele_dev_by_major(major);
6537 }
6538 
6539 
6540 /*
6541  * This is called during boot to force attachment order of special dips
6542  * dip must be referenced via ndi_hold_devi()
6543  */
6544 int
6545 i_ddi_attach_node_hierarchy(dev_info_t *dip)
6546 {
6547 	dev_info_t	*parent;
6548 	int		ret, circ;
6549 
6550 	/*
6551 	 * Recurse up until attached parent is found.
6552 	 */
6553 	if (i_ddi_devi_attached(dip))
6554 		return (DDI_SUCCESS);
6555 	parent = ddi_get_parent(dip);
6556 	if (i_ddi_attach_node_hierarchy(parent) != DDI_SUCCESS)
6557 		return (DDI_FAILURE);
6558 
6559 	/*
6560 	 * Come top-down, expanding .conf nodes under this parent
6561 	 * and driving attach.
6562 	 */
6563 	ndi_devi_enter(parent, &circ);
6564 	(void) i_ndi_make_spec_children(parent, 0);
6565 	ret = i_ddi_attachchild(dip);
6566 	ndi_devi_exit(parent, circ);
6567 
6568 	return (ret);
6569 }
6570 
6571 /* keep this function static */
6572 static int
6573 attach_driver_nodes(major_t major)
6574 {
6575 	struct devnames *dnp;
6576 	dev_info_t *dip;
6577 	int error = DDI_FAILURE;
6578 
6579 	dnp = &devnamesp[major];
6580 	LOCK_DEV_OPS(&dnp->dn_lock);
6581 	dip = dnp->dn_head;
6582 	while (dip) {
6583 		ndi_hold_devi(dip);
6584 		UNLOCK_DEV_OPS(&dnp->dn_lock);
6585 		if (i_ddi_attach_node_hierarchy(dip) == DDI_SUCCESS)
6586 			error = DDI_SUCCESS;
6587 		LOCK_DEV_OPS(&dnp->dn_lock);
6588 		ndi_rele_devi(dip);
6589 		dip = ddi_get_next(dip);
6590 	}
6591 	if (error == DDI_SUCCESS)
6592 		dnp->dn_flags |= DN_NO_AUTODETACH;
6593 	UNLOCK_DEV_OPS(&dnp->dn_lock);
6594 
6595 
6596 	return (error);
6597 }
6598 
6599 /*
6600  * i_ddi_attach_hw_nodes configures and attaches all hw nodes
6601  * bound to a specific driver. This function replaces calls to
6602  * ddi_hold_installed_driver() for drivers with no .conf
6603  * enumerated nodes.
6604  *
6605  * This facility is typically called at boot time to attach
6606  * platform-specific hardware nodes, such as ppm nodes on xcal
6607  * and grover and keyswitch nodes on cherrystone. It does not
6608  * deal with .conf enumerated node. Calling it beyond the boot
6609  * process is strongly discouraged.
6610  */
6611 int
6612 i_ddi_attach_hw_nodes(char *driver)
6613 {
6614 	major_t major;
6615 
6616 	major = ddi_name_to_major(driver);
6617 	if (major == DDI_MAJOR_T_NONE)
6618 		return (DDI_FAILURE);
6619 
6620 	return (attach_driver_nodes(major));
6621 }
6622 
6623 /*
6624  * i_ddi_attach_pseudo_node configures pseudo drivers which
6625  * has a single node. The .conf nodes must be enumerated
6626  * before calling this interface. The dip is held attached
6627  * upon returning.
6628  *
6629  * This facility should only be called only at boot time
6630  * by the I/O framework.
6631  */
6632 dev_info_t *
6633 i_ddi_attach_pseudo_node(char *driver)
6634 {
6635 	major_t major;
6636 	dev_info_t *dip;
6637 
6638 	major = ddi_name_to_major(driver);
6639 	if (major == DDI_MAJOR_T_NONE)
6640 		return (NULL);
6641 
6642 	if (attach_driver_nodes(major) != DDI_SUCCESS)
6643 		return (NULL);
6644 
6645 	dip = devnamesp[major].dn_head;
6646 	ASSERT(dip && ddi_get_next(dip) == NULL);
6647 	ndi_hold_devi(dip);
6648 	return (dip);
6649 }
6650 
6651 static void
6652 diplist_to_parent_major(dev_info_t *head, char parents[])
6653 {
6654 	major_t major;
6655 	dev_info_t *dip, *pdip;
6656 
6657 	for (dip = head; dip != NULL; dip = ddi_get_next(dip)) {
6658 		pdip = ddi_get_parent(dip);
6659 		ASSERT(pdip);	/* disallow rootnex.conf nodes */
6660 		major = ddi_driver_major(pdip);
6661 		if ((major != DDI_MAJOR_T_NONE) && parents[major] == 0)
6662 			parents[major] = 1;
6663 	}
6664 }
6665 
6666 /*
6667  * Call ddi_hold_installed_driver() on each parent major
6668  * and invoke mt_config_driver() to attach child major.
6669  * This is part of the implementation of ddi_hold_installed_driver.
6670  */
6671 static int
6672 attach_driver_by_parent(major_t child_major, char parents[])
6673 {
6674 	major_t par_major;
6675 	struct mt_config_handle *hdl;
6676 	int flags = NDI_DEVI_PERSIST | NDI_NO_EVENT;
6677 
6678 	hdl = mt_config_init(NULL, NULL, flags, child_major, MT_CONFIG_OP,
6679 	    NULL);
6680 	for (par_major = 0; par_major < devcnt; par_major++) {
6681 		/* disallow recursion on the same driver */
6682 		if (parents[par_major] == 0 || par_major == child_major)
6683 			continue;
6684 		if (ddi_hold_installed_driver(par_major) == NULL)
6685 			continue;
6686 		hdl->mtc_parmajor = par_major;
6687 		mt_config_driver(hdl);
6688 		ddi_rele_driver(par_major);
6689 	}
6690 	(void) mt_config_fini(hdl);
6691 
6692 	return (i_ddi_devs_attached(child_major));
6693 }
6694 
6695 int
6696 i_ddi_devs_attached(major_t major)
6697 {
6698 	dev_info_t *dip;
6699 	struct devnames *dnp;
6700 	int error = DDI_FAILURE;
6701 
6702 	/* check for attached instances */
6703 	dnp = &devnamesp[major];
6704 	LOCK_DEV_OPS(&dnp->dn_lock);
6705 	for (dip = dnp->dn_head; dip != NULL; dip = ddi_get_next(dip)) {
6706 		if (i_ddi_devi_attached(dip)) {
6707 			error = DDI_SUCCESS;
6708 			break;
6709 		}
6710 	}
6711 	UNLOCK_DEV_OPS(&dnp->dn_lock);
6712 
6713 	return (error);
6714 }
6715 
6716 int
6717 i_ddi_minor_node_count(dev_info_t *ddip, const char *node_type)
6718 {
6719 	int			circ;
6720 	struct ddi_minor_data	*dp;
6721 	int			count = 0;
6722 
6723 	ndi_devi_enter(ddip, &circ);
6724 	for (dp = DEVI(ddip)->devi_minor; dp != NULL; dp = dp->next) {
6725 		if (strcmp(dp->ddm_node_type, node_type) == 0)
6726 			count++;
6727 	}
6728 	ndi_devi_exit(ddip, circ);
6729 	return (count);
6730 }
6731 
6732 /*
6733  * ddi_hold_installed_driver configures and attaches all
6734  * instances of the specified driver. To accomplish this
6735  * it configures and attaches all possible parents of
6736  * the driver, enumerated both in h/w nodes and in the
6737  * driver's .conf file.
6738  *
6739  * NOTE: This facility is for compatibility purposes only and will
6740  *	eventually go away. Its usage is strongly discouraged.
6741  */
6742 static void
6743 enter_driver(struct devnames *dnp)
6744 {
6745 	mutex_enter(&dnp->dn_lock);
6746 	ASSERT(dnp->dn_busy_thread != curthread);
6747 	while (dnp->dn_flags & DN_DRIVER_BUSY)
6748 		cv_wait(&dnp->dn_wait, &dnp->dn_lock);
6749 	dnp->dn_flags |= DN_DRIVER_BUSY;
6750 	dnp->dn_busy_thread = curthread;
6751 	mutex_exit(&dnp->dn_lock);
6752 }
6753 
6754 static void
6755 exit_driver(struct devnames *dnp)
6756 {
6757 	mutex_enter(&dnp->dn_lock);
6758 	ASSERT(dnp->dn_busy_thread == curthread);
6759 	dnp->dn_flags &= ~DN_DRIVER_BUSY;
6760 	dnp->dn_busy_thread = NULL;
6761 	cv_broadcast(&dnp->dn_wait);
6762 	mutex_exit(&dnp->dn_lock);
6763 }
6764 
6765 struct dev_ops *
6766 ddi_hold_installed_driver(major_t major)
6767 {
6768 	struct dev_ops *ops;
6769 	struct devnames *dnp;
6770 	char *parents;
6771 	int error;
6772 
6773 	ops = ddi_hold_driver(major);
6774 	if (ops == NULL)
6775 		return (NULL);
6776 
6777 	/*
6778 	 * Return immediately if all the attach operations associated
6779 	 * with a ddi_hold_installed_driver() call have already been done.
6780 	 */
6781 	dnp = &devnamesp[major];
6782 	enter_driver(dnp);
6783 	if (dnp->dn_flags & DN_DRIVER_HELD) {
6784 		exit_driver(dnp);
6785 		if (i_ddi_devs_attached(major) == DDI_SUCCESS)
6786 			return (ops);
6787 		ddi_rele_driver(major);
6788 		return (NULL);
6789 	}
6790 
6791 	LOCK_DEV_OPS(&dnp->dn_lock);
6792 	dnp->dn_flags |= (DN_DRIVER_HELD | DN_NO_AUTODETACH);
6793 	UNLOCK_DEV_OPS(&dnp->dn_lock);
6794 
6795 	DCOMPATPRINTF((CE_CONT,
6796 	    "ddi_hold_installed_driver: %s\n", dnp->dn_name));
6797 
6798 	/*
6799 	 * When the driver has no .conf children, it is sufficient
6800 	 * to attach existing nodes in the device tree. Nodes not
6801 	 * enumerated by the OBP are not attached.
6802 	 */
6803 	if (dnp->dn_pl == NULL) {
6804 		if (attach_driver_nodes(major) == DDI_SUCCESS) {
6805 			exit_driver(dnp);
6806 			return (ops);
6807 		}
6808 		exit_driver(dnp);
6809 		ddi_rele_driver(major);
6810 		return (NULL);
6811 	}
6812 
6813 	/*
6814 	 * Driver has .conf nodes. We find all possible parents
6815 	 * and recursively all ddi_hold_installed_driver on the
6816 	 * parent driver; then we invoke ndi_config_driver()
6817 	 * on all possible parent node in parallel to speed up
6818 	 * performance.
6819 	 */
6820 	parents = kmem_zalloc(devcnt * sizeof (char), KM_SLEEP);
6821 
6822 	LOCK_DEV_OPS(&dnp->dn_lock);
6823 	/* find .conf parents */
6824 	(void) impl_parlist_to_major(dnp->dn_pl, parents);
6825 	/* find hw node parents */
6826 	diplist_to_parent_major(dnp->dn_head, parents);
6827 	UNLOCK_DEV_OPS(&dnp->dn_lock);
6828 
6829 	error = attach_driver_by_parent(major, parents);
6830 	kmem_free(parents, devcnt * sizeof (char));
6831 	if (error == DDI_SUCCESS) {
6832 		exit_driver(dnp);
6833 		return (ops);
6834 	}
6835 
6836 	exit_driver(dnp);
6837 	ddi_rele_driver(major);
6838 	return (NULL);
6839 }
6840 
6841 /*
6842  * Default bus_config entry point for nexus drivers
6843  */
6844 int
6845 ndi_busop_bus_config(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
6846     void *arg, dev_info_t **child, clock_t timeout)
6847 {
6848 	major_t major;
6849 
6850 	/*
6851 	 * A timeout of 30 minutes or more is probably a mistake
6852 	 * This is intended to catch uses where timeout is in
6853 	 * the wrong units.  timeout must be in units of ticks.
6854 	 */
6855 	ASSERT(timeout < SEC_TO_TICK(1800));
6856 
6857 	major = DDI_MAJOR_T_NONE;
6858 	switch (op) {
6859 	case BUS_CONFIG_ONE:
6860 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config %s timeout=%ld\n",
6861 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
6862 		    (char *)arg, timeout));
6863 		return (devi_config_one(pdip, (char *)arg, child, flags,
6864 		    timeout));
6865 
6866 	case BUS_CONFIG_DRIVER:
6867 		major = (major_t)(uintptr_t)arg;
6868 		/*FALLTHROUGH*/
6869 	case BUS_CONFIG_ALL:
6870 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config timeout=%ld\n",
6871 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
6872 		    timeout));
6873 		if (timeout > 0) {
6874 			NDI_DEBUG(flags, (CE_CONT,
6875 			    "%s%d: bus config all timeout=%ld\n",
6876 			    ddi_driver_name(pdip), ddi_get_instance(pdip),
6877 			    timeout));
6878 			delay(timeout);
6879 		}
6880 		return (config_immediate_children(pdip, flags, major));
6881 
6882 	default:
6883 		return (NDI_FAILURE);
6884 	}
6885 	/*NOTREACHED*/
6886 }
6887 
6888 /*
6889  * Default busop bus_unconfig handler for nexus drivers
6890  */
6891 int
6892 ndi_busop_bus_unconfig(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
6893     void *arg)
6894 {
6895 	major_t major;
6896 
6897 	major = DDI_MAJOR_T_NONE;
6898 	switch (op) {
6899 	case BUS_UNCONFIG_ONE:
6900 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig %s\n",
6901 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
6902 		    (char *)arg));
6903 		return (devi_unconfig_one(pdip, (char *)arg, flags));
6904 
6905 	case BUS_UNCONFIG_DRIVER:
6906 		major = (major_t)(uintptr_t)arg;
6907 		/*FALLTHROUGH*/
6908 	case BUS_UNCONFIG_ALL:
6909 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig all\n",
6910 		    ddi_driver_name(pdip), ddi_get_instance(pdip)));
6911 		return (unconfig_immediate_children(pdip, NULL, flags, major));
6912 
6913 	default:
6914 		return (NDI_FAILURE);
6915 	}
6916 	/*NOTREACHED*/
6917 }
6918 
6919 /*
6920  * dummy functions to be removed
6921  */
6922 void
6923 impl_rem_dev_props(dev_info_t *dip)
6924 {
6925 	_NOTE(ARGUNUSED(dip))
6926 	/* do nothing */
6927 }
6928 
6929 /*
6930  * Determine if a node is a leaf node. If not sure, return false (0).
6931  */
6932 static int
6933 is_leaf_node(dev_info_t *dip)
6934 {
6935 	major_t major = ddi_driver_major(dip);
6936 
6937 	if (major == DDI_MAJOR_T_NONE)
6938 		return (0);
6939 
6940 	return (devnamesp[major].dn_flags & DN_LEAF_DRIVER);
6941 }
6942 
6943 /*
6944  * Multithreaded [un]configuration
6945  */
6946 static struct mt_config_handle *
6947 mt_config_init(dev_info_t *pdip, dev_info_t **dipp, int flags,
6948     major_t major, int op, struct brevq_node **brevqp)
6949 {
6950 	struct mt_config_handle	*hdl = kmem_alloc(sizeof (*hdl), KM_SLEEP);
6951 
6952 	mutex_init(&hdl->mtc_lock, NULL, MUTEX_DEFAULT, NULL);
6953 	cv_init(&hdl->mtc_cv, NULL, CV_DEFAULT, NULL);
6954 	hdl->mtc_pdip = pdip;
6955 	hdl->mtc_fdip = dipp;
6956 	hdl->mtc_parmajor = DDI_MAJOR_T_NONE;
6957 	hdl->mtc_flags = flags;
6958 	hdl->mtc_major = major;
6959 	hdl->mtc_thr_count = 0;
6960 	hdl->mtc_op = op;
6961 	hdl->mtc_error = 0;
6962 	hdl->mtc_brevqp = brevqp;
6963 
6964 #ifdef DEBUG
6965 	gethrestime(&hdl->start_time);
6966 	hdl->total_time = 0;
6967 #endif /* DEBUG */
6968 
6969 	return (hdl);
6970 }
6971 
6972 #ifdef DEBUG
6973 static int
6974 time_diff_in_msec(timestruc_t start, timestruc_t end)
6975 {
6976 	int	nsec, sec;
6977 
6978 	sec = end.tv_sec - start.tv_sec;
6979 	nsec = end.tv_nsec - start.tv_nsec;
6980 	if (nsec < 0) {
6981 		nsec += NANOSEC;
6982 		sec -= 1;
6983 	}
6984 
6985 	return (sec * (NANOSEC >> 20) + (nsec >> 20));
6986 }
6987 
6988 #endif	/* DEBUG */
6989 
6990 static int
6991 mt_config_fini(struct mt_config_handle *hdl)
6992 {
6993 	int		rv;
6994 #ifdef DEBUG
6995 	int		real_time;
6996 	timestruc_t	end_time;
6997 #endif /* DEBUG */
6998 
6999 	mutex_enter(&hdl->mtc_lock);
7000 	while (hdl->mtc_thr_count > 0)
7001 		cv_wait(&hdl->mtc_cv, &hdl->mtc_lock);
7002 	rv = hdl->mtc_error;
7003 	mutex_exit(&hdl->mtc_lock);
7004 
7005 #ifdef DEBUG
7006 	gethrestime(&end_time);
7007 	real_time = time_diff_in_msec(hdl->start_time, end_time);
7008 	if ((ddidebug & DDI_MTCONFIG) && hdl->mtc_pdip)
7009 		cmn_err(CE_NOTE,
7010 		    "config %s%d: total time %d msec, real time %d msec",
7011 		    ddi_driver_name(hdl->mtc_pdip),
7012 		    ddi_get_instance(hdl->mtc_pdip),
7013 		    hdl->total_time, real_time);
7014 #endif /* DEBUG */
7015 
7016 	cv_destroy(&hdl->mtc_cv);
7017 	mutex_destroy(&hdl->mtc_lock);
7018 	kmem_free(hdl, sizeof (*hdl));
7019 
7020 	return (rv);
7021 }
7022 
7023 struct mt_config_data {
7024 	struct mt_config_handle	*mtc_hdl;
7025 	dev_info_t		*mtc_dip;
7026 	major_t			mtc_major;
7027 	int			mtc_flags;
7028 	struct brevq_node	*mtc_brn;
7029 	struct mt_config_data	*mtc_next;
7030 };
7031 
7032 static void
7033 mt_config_thread(void *arg)
7034 {
7035 	struct mt_config_data	*mcd = (struct mt_config_data *)arg;
7036 	struct mt_config_handle	*hdl = mcd->mtc_hdl;
7037 	dev_info_t		*dip = mcd->mtc_dip;
7038 	dev_info_t		*rdip, **dipp;
7039 	major_t			major = mcd->mtc_major;
7040 	int			flags = mcd->mtc_flags;
7041 	int			rv = 0;
7042 
7043 #ifdef DEBUG
7044 	timestruc_t start_time, end_time;
7045 	gethrestime(&start_time);
7046 #endif /* DEBUG */
7047 
7048 	rdip = NULL;
7049 	dipp = hdl->mtc_fdip ? &rdip : NULL;
7050 
7051 	switch (hdl->mtc_op) {
7052 	case MT_CONFIG_OP:
7053 		rv = devi_config_common(dip, flags, major);
7054 		break;
7055 	case MT_UNCONFIG_OP:
7056 		if (mcd->mtc_brn) {
7057 			struct brevq_node *brevq = NULL;
7058 			rv = devi_unconfig_common(dip, dipp, flags, major,
7059 			    &brevq);
7060 			mcd->mtc_brn->brn_child = brevq;
7061 		} else
7062 			rv = devi_unconfig_common(dip, dipp, flags, major,
7063 			    NULL);
7064 		break;
7065 	}
7066 
7067 	mutex_enter(&hdl->mtc_lock);
7068 #ifdef DEBUG
7069 	gethrestime(&end_time);
7070 	hdl->total_time += time_diff_in_msec(start_time, end_time);
7071 #endif /* DEBUG */
7072 
7073 	if ((rv != NDI_SUCCESS) && (hdl->mtc_error == 0)) {
7074 		hdl->mtc_error = rv;
7075 #ifdef	DEBUG
7076 		if ((ddidebug & DDI_DEBUG) && (major != DDI_MAJOR_T_NONE)) {
7077 			char	*path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7078 
7079 			(void) ddi_pathname(dip, path);
7080 			cmn_err(CE_NOTE, "mt_config_thread: "
7081 			    "op %d.%d.%x at %s failed %d",
7082 			    hdl->mtc_op, major, flags, path, rv);
7083 			kmem_free(path, MAXPATHLEN);
7084 		}
7085 #endif	/* DEBUG */
7086 	}
7087 
7088 	if (hdl->mtc_fdip && *hdl->mtc_fdip == NULL) {
7089 		*hdl->mtc_fdip = rdip;
7090 		rdip = NULL;
7091 	}
7092 
7093 	if (rdip) {
7094 		ASSERT(rv != NDI_SUCCESS);
7095 		ndi_rele_devi(rdip);
7096 	}
7097 
7098 	ndi_rele_devi(dip);
7099 
7100 	if (--hdl->mtc_thr_count == 0)
7101 		cv_broadcast(&hdl->mtc_cv);
7102 	mutex_exit(&hdl->mtc_lock);
7103 	kmem_free(mcd, sizeof (*mcd));
7104 }
7105 
7106 /*
7107  * Multi-threaded config/unconfig of child nexus
7108  */
7109 static void
7110 mt_config_children(struct mt_config_handle *hdl)
7111 {
7112 	dev_info_t		*pdip = hdl->mtc_pdip;
7113 	major_t			major = hdl->mtc_major;
7114 	dev_info_t		*dip;
7115 	int			circ;
7116 	struct brevq_node	*brn;
7117 	struct mt_config_data	*mcd_head = NULL;
7118 	struct mt_config_data	*mcd_tail = NULL;
7119 	struct mt_config_data	*mcd;
7120 #ifdef DEBUG
7121 	timestruc_t		end_time;
7122 
7123 	/* Update total_time in handle */
7124 	gethrestime(&end_time);
7125 	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
7126 #endif
7127 
7128 	ndi_devi_enter(pdip, &circ);
7129 	dip = ddi_get_child(pdip);
7130 	while (dip) {
7131 		if (hdl->mtc_op == MT_UNCONFIG_OP && hdl->mtc_brevqp &&
7132 		    !(DEVI_EVREMOVE(dip)) &&
7133 		    i_ddi_node_state(dip) >= DS_INITIALIZED) {
7134 			/*
7135 			 * Enqueue this dip's deviname.
7136 			 * No need to hold a lock while enqueuing since this
7137 			 * is the only thread doing the enqueue and no one
7138 			 * walks the queue while we are in multithreaded
7139 			 * unconfiguration.
7140 			 */
7141 			brn = brevq_enqueue(hdl->mtc_brevqp, dip, NULL);
7142 		} else
7143 			brn = NULL;
7144 
7145 		/*
7146 		 * Hold the child that we are processing so he does not get
7147 		 * removed. The corrisponding ndi_rele_devi() for children
7148 		 * that are not being skipped is done at the end of
7149 		 * mt_config_thread().
7150 		 */
7151 		ndi_hold_devi(dip);
7152 
7153 		/*
7154 		 * skip leaf nodes and (for configure) nodes not
7155 		 * fully attached.
7156 		 */
7157 		if (is_leaf_node(dip) ||
7158 		    (hdl->mtc_op == MT_CONFIG_OP &&
7159 		    i_ddi_node_state(dip) < DS_READY)) {
7160 			ndi_rele_devi(dip);
7161 			dip = ddi_get_next_sibling(dip);
7162 			continue;
7163 		}
7164 
7165 		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
7166 		mcd->mtc_dip = dip;
7167 		mcd->mtc_hdl = hdl;
7168 		mcd->mtc_brn = brn;
7169 
7170 		/*
7171 		 * Switch a 'driver' operation to an 'all' operation below a
7172 		 * node bound to the driver.
7173 		 */
7174 		if ((major == DDI_MAJOR_T_NONE) ||
7175 		    (major == ddi_driver_major(dip)))
7176 			mcd->mtc_major = DDI_MAJOR_T_NONE;
7177 		else
7178 			mcd->mtc_major = major;
7179 
7180 		/*
7181 		 * The unconfig-driver to unconfig-all conversion above
7182 		 * constitutes an autodetach for NDI_DETACH_DRIVER calls,
7183 		 * set NDI_AUTODETACH.
7184 		 */
7185 		mcd->mtc_flags = hdl->mtc_flags;
7186 		if ((mcd->mtc_flags & NDI_DETACH_DRIVER) &&
7187 		    (hdl->mtc_op == MT_UNCONFIG_OP) &&
7188 		    (major == ddi_driver_major(pdip)))
7189 			mcd->mtc_flags |= NDI_AUTODETACH;
7190 
7191 		mutex_enter(&hdl->mtc_lock);
7192 		hdl->mtc_thr_count++;
7193 		mutex_exit(&hdl->mtc_lock);
7194 
7195 		/*
7196 		 * Add to end of list to process after ndi_devi_exit to avoid
7197 		 * locking differences depending on value of mtc_off.
7198 		 */
7199 		mcd->mtc_next = NULL;
7200 		if (mcd_head == NULL)
7201 			mcd_head = mcd;
7202 		else
7203 			mcd_tail->mtc_next = mcd;
7204 		mcd_tail = mcd;
7205 
7206 		dip = ddi_get_next_sibling(dip);
7207 	}
7208 	ndi_devi_exit(pdip, circ);
7209 
7210 	/* go through the list of held children */
7211 	for (mcd = mcd_head; mcd; mcd = mcd_head) {
7212 		mcd_head = mcd->mtc_next;
7213 		if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
7214 			mt_config_thread(mcd);
7215 		else
7216 			(void) thread_create(NULL, 0, mt_config_thread, mcd,
7217 			    0, &p0, TS_RUN, minclsyspri);
7218 	}
7219 }
7220 
7221 static void
7222 mt_config_driver(struct mt_config_handle *hdl)
7223 {
7224 	major_t			par_major = hdl->mtc_parmajor;
7225 	major_t			major = hdl->mtc_major;
7226 	struct devnames		*dnp = &devnamesp[par_major];
7227 	dev_info_t		*dip;
7228 	struct mt_config_data	*mcd_head = NULL;
7229 	struct mt_config_data	*mcd_tail = NULL;
7230 	struct mt_config_data	*mcd;
7231 #ifdef DEBUG
7232 	timestruc_t		end_time;
7233 
7234 	/* Update total_time in handle */
7235 	gethrestime(&end_time);
7236 	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
7237 #endif
7238 	ASSERT(par_major != DDI_MAJOR_T_NONE);
7239 	ASSERT(major != DDI_MAJOR_T_NONE);
7240 
7241 	LOCK_DEV_OPS(&dnp->dn_lock);
7242 	dip = devnamesp[par_major].dn_head;
7243 	while (dip) {
7244 		/*
7245 		 * Hold the child that we are processing so he does not get
7246 		 * removed. The corrisponding ndi_rele_devi() for children
7247 		 * that are not being skipped is done at the end of
7248 		 * mt_config_thread().
7249 		 */
7250 		ndi_hold_devi(dip);
7251 
7252 		/* skip leaf nodes and nodes not fully attached */
7253 		if (!i_ddi_devi_attached(dip) || is_leaf_node(dip)) {
7254 			ndi_rele_devi(dip);
7255 			dip = ddi_get_next(dip);
7256 			continue;
7257 		}
7258 
7259 		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
7260 		mcd->mtc_dip = dip;
7261 		mcd->mtc_hdl = hdl;
7262 		mcd->mtc_major = major;
7263 		mcd->mtc_flags = hdl->mtc_flags;
7264 
7265 		mutex_enter(&hdl->mtc_lock);
7266 		hdl->mtc_thr_count++;
7267 		mutex_exit(&hdl->mtc_lock);
7268 
7269 		/*
7270 		 * Add to end of list to process after UNLOCK_DEV_OPS to avoid
7271 		 * locking differences depending on value of mtc_off.
7272 		 */
7273 		mcd->mtc_next = NULL;
7274 		if (mcd_head == NULL)
7275 			mcd_head = mcd;
7276 		else
7277 			mcd_tail->mtc_next = mcd;
7278 		mcd_tail = mcd;
7279 
7280 		dip = ddi_get_next(dip);
7281 	}
7282 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7283 
7284 	/* go through the list of held children */
7285 	for (mcd = mcd_head; mcd; mcd = mcd_head) {
7286 		mcd_head = mcd->mtc_next;
7287 		if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
7288 			mt_config_thread(mcd);
7289 		else
7290 			(void) thread_create(NULL, 0, mt_config_thread, mcd,
7291 			    0, &p0, TS_RUN, minclsyspri);
7292 	}
7293 }
7294 
7295 /*
7296  * Given the nodeid for a persistent (PROM or SID) node, return
7297  * the corresponding devinfo node
7298  * NOTE: This function will return NULL for .conf nodeids.
7299  */
7300 dev_info_t *
7301 e_ddi_nodeid_to_dip(pnode_t nodeid)
7302 {
7303 	dev_info_t		*dip = NULL;
7304 	struct devi_nodeid	*prev, *elem;
7305 
7306 	mutex_enter(&devimap->dno_lock);
7307 
7308 	prev = NULL;
7309 	for (elem = devimap->dno_head; elem; elem = elem->next) {
7310 		if (elem->nodeid == nodeid) {
7311 			ndi_hold_devi(elem->dip);
7312 			dip = elem->dip;
7313 			break;
7314 		}
7315 		prev = elem;
7316 	}
7317 
7318 	/*
7319 	 * Move to head for faster lookup next time
7320 	 */
7321 	if (elem && prev) {
7322 		prev->next = elem->next;
7323 		elem->next = devimap->dno_head;
7324 		devimap->dno_head = elem;
7325 	}
7326 
7327 	mutex_exit(&devimap->dno_lock);
7328 	return (dip);
7329 }
7330 
7331 static void
7332 free_cache_task(void *arg)
7333 {
7334 	ASSERT(arg == NULL);
7335 
7336 	mutex_enter(&di_cache.cache_lock);
7337 
7338 	/*
7339 	 * The cache can be invalidated without holding the lock
7340 	 * but it can be made valid again only while the lock is held.
7341 	 * So if the cache is invalid when the lock is held, it will
7342 	 * stay invalid until lock is released.
7343 	 */
7344 	if (!di_cache.cache_valid)
7345 		i_ddi_di_cache_free(&di_cache);
7346 
7347 	mutex_exit(&di_cache.cache_lock);
7348 
7349 	if (di_cache_debug)
7350 		cmn_err(CE_NOTE, "system_taskq: di_cache freed");
7351 }
7352 
7353 extern int modrootloaded;
7354 
7355 void
7356 i_ddi_di_cache_free(struct di_cache *cache)
7357 {
7358 	int	error;
7359 
7360 	ASSERT(mutex_owned(&cache->cache_lock));
7361 
7362 	if (cache->cache_size) {
7363 		ASSERT(cache->cache_size > 0);
7364 		ASSERT(cache->cache_data);
7365 
7366 		kmem_free(cache->cache_data, cache->cache_size);
7367 		cache->cache_data = NULL;
7368 		cache->cache_size = 0;
7369 
7370 		if (di_cache_debug)
7371 			cmn_err(CE_NOTE, "i_ddi_di_cache_free: freed cachemem");
7372 	} else {
7373 		ASSERT(cache->cache_data == NULL);
7374 		if (di_cache_debug)
7375 			cmn_err(CE_NOTE, "i_ddi_di_cache_free: NULL cache");
7376 	}
7377 
7378 	if (!modrootloaded || rootvp == NULL ||
7379 	    vn_is_readonly(rootvp) || sys_shutdown) {
7380 		if (di_cache_debug) {
7381 			cmn_err(CE_WARN, "/ not mounted/RDONLY. Skip unlink");
7382 		}
7383 		return;
7384 	}
7385 
7386 	error = vn_remove(DI_CACHE_FILE, UIO_SYSSPACE, RMFILE);
7387 	if (di_cache_debug && error && error != ENOENT) {
7388 		cmn_err(CE_WARN, "%s: unlink failed: %d", DI_CACHE_FILE, error);
7389 	} else if (di_cache_debug && !error) {
7390 		cmn_err(CE_NOTE, "i_ddi_di_cache_free: unlinked cache file");
7391 	}
7392 }
7393 
7394 void
7395 i_ddi_di_cache_invalidate(int kmflag)
7396 {
7397 	int	cache_valid;
7398 
7399 	if (!modrootloaded || !i_ddi_io_initialized()) {
7400 		if (di_cache_debug)
7401 			cmn_err(CE_NOTE, "I/O not inited. Skipping invalidate");
7402 		return;
7403 	}
7404 
7405 	/* Increment devtree generation number. */
7406 	atomic_inc_ulong(&devtree_gen);
7407 
7408 	/* Invalidate the in-core cache and dispatch free on valid->invalid */
7409 	cache_valid = atomic_swap_uint(&di_cache.cache_valid, 0);
7410 	if (cache_valid) {
7411 		(void) taskq_dispatch(system_taskq, free_cache_task, NULL,
7412 		    (kmflag == KM_SLEEP) ? TQ_SLEEP : TQ_NOSLEEP);
7413 	}
7414 
7415 	if (di_cache_debug) {
7416 		cmn_err(CE_NOTE, "invalidation with km_flag: %s",
7417 		    kmflag == KM_SLEEP ? "KM_SLEEP" : "KM_NOSLEEP");
7418 	}
7419 }
7420 
7421 
7422 static void
7423 i_bind_vhci_node(dev_info_t *dip)
7424 {
7425 	DEVI(dip)->devi_major = ddi_name_to_major(ddi_node_name(dip));
7426 	i_ddi_set_node_state(dip, DS_BOUND);
7427 }
7428 
7429 static char vhci_node_addr[2];
7430 
7431 static int
7432 i_init_vhci_node(dev_info_t *dip)
7433 {
7434 	add_global_props(dip);
7435 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
7436 	if (DEVI(dip)->devi_ops == NULL)
7437 		return (-1);
7438 
7439 	DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
7440 	e_ddi_keep_instance(dip);
7441 	vhci_node_addr[0]	= '\0';
7442 	ddi_set_name_addr(dip, vhci_node_addr);
7443 	i_ddi_set_node_state(dip, DS_INITIALIZED);
7444 	return (0);
7445 }
7446 
7447 static void
7448 i_link_vhci_node(dev_info_t *dip)
7449 {
7450 	ASSERT(MUTEX_HELD(&global_vhci_lock));
7451 
7452 	/*
7453 	 * scsi_vhci should be kept left most of the device tree.
7454 	 */
7455 	if (scsi_vhci_dip) {
7456 		DEVI(dip)->devi_sibling = DEVI(scsi_vhci_dip)->devi_sibling;
7457 		DEVI(scsi_vhci_dip)->devi_sibling = DEVI(dip);
7458 	} else {
7459 		DEVI(dip)->devi_sibling = DEVI(top_devinfo)->devi_child;
7460 		DEVI(top_devinfo)->devi_child = DEVI(dip);
7461 	}
7462 }
7463 
7464 
7465 /*
7466  * This a special routine to enumerate vhci node (child of rootnex
7467  * node) without holding the ndi_devi_enter() lock. The device node
7468  * is allocated, initialized and brought into DS_READY state before
7469  * inserting into the device tree. The VHCI node is handcrafted
7470  * here to bring the node to DS_READY, similar to rootnex node.
7471  *
7472  * The global_vhci_lock protects linking the node into the device
7473  * as same lock is held before linking/unlinking any direct child
7474  * of rootnex children.
7475  *
7476  * This routine is a workaround to handle a possible deadlock
7477  * that occurs while trying to enumerate node in a different sub-tree
7478  * during _init/_attach entry points.
7479  */
7480 /*ARGSUSED*/
7481 dev_info_t *
7482 ndi_devi_config_vhci(char *drvname, int flags)
7483 {
7484 	struct devnames		*dnp;
7485 	dev_info_t		*dip;
7486 	major_t			major = ddi_name_to_major(drvname);
7487 
7488 	if (major == -1)
7489 		return (NULL);
7490 
7491 	/* Make sure we create the VHCI node only once */
7492 	dnp = &devnamesp[major];
7493 	LOCK_DEV_OPS(&dnp->dn_lock);
7494 	if (dnp->dn_head) {
7495 		dip = dnp->dn_head;
7496 		UNLOCK_DEV_OPS(&dnp->dn_lock);
7497 		return (dip);
7498 	}
7499 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7500 
7501 	/* Allocate the VHCI node */
7502 	ndi_devi_alloc_sleep(top_devinfo, drvname, DEVI_SID_NODEID, &dip);
7503 	ndi_hold_devi(dip);
7504 
7505 	/* Mark the node as VHCI */
7506 	DEVI(dip)->devi_node_attributes |= DDI_VHCI_NODE;
7507 
7508 	i_ddi_add_devimap(dip);
7509 	i_bind_vhci_node(dip);
7510 	if (i_init_vhci_node(dip) == -1) {
7511 		ndi_rele_devi(dip);
7512 		(void) ndi_devi_free(dip);
7513 		return (NULL);
7514 	}
7515 
7516 	mutex_enter(&(DEVI(dip)->devi_lock));
7517 	DEVI_SET_ATTACHING(dip);
7518 	mutex_exit(&(DEVI(dip)->devi_lock));
7519 
7520 	if (devi_attach(dip, DDI_ATTACH) != DDI_SUCCESS) {
7521 		cmn_err(CE_CONT, "Could not attach %s driver", drvname);
7522 		e_ddi_free_instance(dip, vhci_node_addr);
7523 		ndi_rele_devi(dip);
7524 		(void) ndi_devi_free(dip);
7525 		return (NULL);
7526 	}
7527 	mutex_enter(&(DEVI(dip)->devi_lock));
7528 	DEVI_CLR_ATTACHING(dip);
7529 	mutex_exit(&(DEVI(dip)->devi_lock));
7530 
7531 	mutex_enter(&global_vhci_lock);
7532 	i_link_vhci_node(dip);
7533 	mutex_exit(&global_vhci_lock);
7534 	i_ddi_set_node_state(dip, DS_READY);
7535 
7536 	LOCK_DEV_OPS(&dnp->dn_lock);
7537 	dnp->dn_flags |= DN_DRIVER_HELD;
7538 	dnp->dn_head = dip;
7539 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7540 
7541 	i_ndi_devi_report_status_change(dip, NULL);
7542 
7543 	return (dip);
7544 }
7545 
7546 /*
7547  * ibt_hw_is_present() returns 0 when there is no IB hardware actively
7548  * running.  This is primarily useful for modules like rpcmod which
7549  * needs a quick check to decide whether or not it should try to use
7550  * InfiniBand
7551  */
7552 int ib_hw_status = 0;
7553 int
7554 ibt_hw_is_present()
7555 {
7556 	return (ib_hw_status);
7557 }
7558 
7559 /*
7560  * ASSERT that constraint flag is not set and then set the "retire attempt"
7561  * flag.
7562  */
7563 int
7564 e_ddi_mark_retiring(dev_info_t *dip, void *arg)
7565 {
7566 	char	**cons_array = (char **)arg;
7567 	char	*path;
7568 	int	constraint;
7569 	int	i;
7570 
7571 	constraint = 0;
7572 	if (cons_array) {
7573 		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7574 		(void) ddi_pathname(dip, path);
7575 		for (i = 0; cons_array[i] != NULL; i++) {
7576 			if (strcmp(path, cons_array[i]) == 0) {
7577 				constraint = 1;
7578 				break;
7579 			}
7580 		}
7581 		kmem_free(path, MAXPATHLEN);
7582 	}
7583 
7584 	mutex_enter(&DEVI(dip)->devi_lock);
7585 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
7586 	DEVI(dip)->devi_flags |= DEVI_RETIRING;
7587 	if (constraint)
7588 		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
7589 	mutex_exit(&DEVI(dip)->devi_lock);
7590 
7591 	RIO_VERBOSE((CE_NOTE, "marked dip as undergoing retire process dip=%p",
7592 	    (void *)dip));
7593 
7594 	if (constraint)
7595 		RIO_DEBUG((CE_NOTE, "marked dip as constrained, dip=%p",
7596 		    (void *)dip));
7597 
7598 	if (MDI_PHCI(dip))
7599 		mdi_phci_mark_retiring(dip, cons_array);
7600 
7601 	return (DDI_WALK_CONTINUE);
7602 }
7603 
7604 static void
7605 free_array(char **cons_array)
7606 {
7607 	int	i;
7608 
7609 	if (cons_array == NULL)
7610 		return;
7611 
7612 	for (i = 0; cons_array[i] != NULL; i++) {
7613 		kmem_free(cons_array[i], strlen(cons_array[i]) + 1);
7614 	}
7615 	kmem_free(cons_array, (i+1) * sizeof (char *));
7616 }
7617 
7618 /*
7619  * Walk *every* node in subtree and check if it blocks, allows or has no
7620  * comment on a proposed retire.
7621  */
7622 int
7623 e_ddi_retire_notify(dev_info_t *dip, void *arg)
7624 {
7625 	int	*constraint = (int *)arg;
7626 
7627 	RIO_DEBUG((CE_NOTE, "retire notify: dip = %p", (void *)dip));
7628 
7629 	(void) e_ddi_offline_notify(dip);
7630 
7631 	mutex_enter(&(DEVI(dip)->devi_lock));
7632 	if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) {
7633 		RIO_DEBUG((CE_WARN, "retire notify: dip in retire "
7634 		    "subtree is not marked: dip = %p", (void *)dip));
7635 		*constraint = 0;
7636 	} else if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) {
7637 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
7638 		RIO_DEBUG((CE_NOTE, "retire notify: BLOCKED: dip = %p",
7639 		    (void *)dip));
7640 		*constraint = 0;
7641 	} else if (!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT)) {
7642 		RIO_DEBUG((CE_NOTE, "retire notify: NO CONSTRAINT: "
7643 		    "dip = %p", (void *)dip));
7644 		*constraint = 0;
7645 	} else {
7646 		RIO_DEBUG((CE_NOTE, "retire notify: CONSTRAINT set: "
7647 		    "dip = %p", (void *)dip));
7648 	}
7649 	mutex_exit(&DEVI(dip)->devi_lock);
7650 
7651 	if (MDI_PHCI(dip))
7652 		mdi_phci_retire_notify(dip, constraint);
7653 
7654 	return (DDI_WALK_CONTINUE);
7655 }
7656 
7657 int
7658 e_ddi_retire_finalize(dev_info_t *dip, void *arg)
7659 {
7660 	int constraint = *(int *)arg;
7661 	int finalize;
7662 	int phci_only;
7663 
7664 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
7665 
7666 	mutex_enter(&DEVI(dip)->devi_lock);
7667 	if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) {
7668 		RIO_DEBUG((CE_WARN,
7669 		    "retire: unmarked dip(%p) in retire subtree",
7670 		    (void *)dip));
7671 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRED));
7672 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
7673 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
7674 		mutex_exit(&DEVI(dip)->devi_lock);
7675 		return (DDI_WALK_CONTINUE);
7676 	}
7677 
7678 	/*
7679 	 * retire the device if constraints have been applied
7680 	 * or if the device is not in use
7681 	 */
7682 	finalize = 0;
7683 	if (constraint) {
7684 		ASSERT(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT);
7685 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
7686 		DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
7687 		DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
7688 		DEVI(dip)->devi_flags |= DEVI_RETIRED;
7689 		mutex_exit(&DEVI(dip)->devi_lock);
7690 		(void) spec_fence_snode(dip, NULL);
7691 		RIO_DEBUG((CE_NOTE, "Fenced off: dip = %p", (void *)dip));
7692 		e_ddi_offline_finalize(dip, DDI_SUCCESS);
7693 	} else {
7694 		if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) {
7695 			ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
7696 			DEVI(dip)->devi_flags &= ~DEVI_R_BLOCKED;
7697 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
7698 			/* we have already finalized during notify */
7699 		} else if (DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT) {
7700 			DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
7701 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
7702 			finalize = 1;
7703 		} else {
7704 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
7705 			/*
7706 			 * even if no contracts, need to call finalize
7707 			 * to clear the contract barrier on the dip
7708 			 */
7709 			finalize = 1;
7710 		}
7711 		mutex_exit(&DEVI(dip)->devi_lock);
7712 		RIO_DEBUG((CE_NOTE, "finalize: NOT retired: dip = %p",
7713 		    (void *)dip));
7714 		if (finalize)
7715 			e_ddi_offline_finalize(dip, DDI_FAILURE);
7716 	}
7717 
7718 	/*
7719 	 * phci_only variable indicates no client checking, just
7720 	 * offline the PHCI. We set that to 0 to enable client
7721 	 * checking
7722 	 */
7723 	phci_only = 0;
7724 	if (MDI_PHCI(dip))
7725 		mdi_phci_retire_finalize(dip, phci_only);
7726 
7727 	return (DDI_WALK_CONTINUE);
7728 }
7729 
7730 /*
7731  * Returns
7732  * 	DDI_SUCCESS if constraints allow retire
7733  *	DDI_FAILURE if constraints don't allow retire.
7734  * cons_array is a NULL terminated array of node paths for
7735  * which constraints have already been applied.
7736  */
7737 int
7738 e_ddi_retire_device(char *path, char **cons_array)
7739 {
7740 	dev_info_t	*dip;
7741 	dev_info_t	*pdip;
7742 	int		circ;
7743 	int		circ2;
7744 	int		constraint;
7745 	char		*devnm;
7746 
7747 	/*
7748 	 * First, lookup the device
7749 	 */
7750 	dip = e_ddi_hold_devi_by_path(path, 0);
7751 	if (dip == NULL) {
7752 		/*
7753 		 * device does not exist. This device cannot be
7754 		 * a critical device since it is not in use. Thus
7755 		 * this device is always retireable. Return DDI_SUCCESS
7756 		 * to indicate this. If this device is ever
7757 		 * instantiated, I/O framework will consult the
7758 		 * the persistent retire store, mark it as
7759 		 * retired and fence it off.
7760 		 */
7761 		RIO_DEBUG((CE_NOTE, "Retire device: device doesn't exist."
7762 		    " NOP. Just returning SUCCESS. path=%s", path));
7763 		free_array(cons_array);
7764 		return (DDI_SUCCESS);
7765 	}
7766 
7767 	RIO_DEBUG((CE_NOTE, "Retire device: found dip = %p.", (void *)dip));
7768 
7769 	pdip = ddi_get_parent(dip);
7770 	ndi_hold_devi(pdip);
7771 
7772 	/*
7773 	 * Run devfs_clean() in case dip has no constraints and is
7774 	 * not in use, so is retireable but there are dv_nodes holding
7775 	 * ref-count on the dip. Note that devfs_clean() always returns
7776 	 * success.
7777 	 */
7778 	devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
7779 	(void) ddi_deviname(dip, devnm);
7780 	(void) devfs_clean(pdip, devnm + 1, DV_CLEAN_FORCE);
7781 	kmem_free(devnm, MAXNAMELEN + 1);
7782 
7783 	ndi_devi_enter(pdip, &circ);
7784 
7785 	/* release hold from e_ddi_hold_devi_by_path */
7786 	ndi_rele_devi(dip);
7787 
7788 	/*
7789 	 * If it cannot make a determination, is_leaf_node() assumes
7790 	 * dip is a nexus.
7791 	 */
7792 	(void) e_ddi_mark_retiring(dip, cons_array);
7793 	if (!is_leaf_node(dip)) {
7794 		ndi_devi_enter(dip, &circ2);
7795 		ddi_walk_devs(ddi_get_child(dip), e_ddi_mark_retiring,
7796 		    cons_array);
7797 		ndi_devi_exit(dip, circ2);
7798 	}
7799 	free_array(cons_array);
7800 
7801 	/*
7802 	 * apply constraints
7803 	 */
7804 	RIO_DEBUG((CE_NOTE, "retire: subtree retire notify: path = %s", path));
7805 
7806 	constraint = 1;	/* assume constraints allow retire */
7807 	(void) e_ddi_retire_notify(dip, &constraint);
7808 	if (!is_leaf_node(dip)) {
7809 		ndi_devi_enter(dip, &circ2);
7810 		ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_notify,
7811 		    &constraint);
7812 		ndi_devi_exit(dip, circ2);
7813 	}
7814 
7815 	/*
7816 	 * Now finalize the retire
7817 	 */
7818 	(void) e_ddi_retire_finalize(dip, &constraint);
7819 	if (!is_leaf_node(dip)) {
7820 		ndi_devi_enter(dip, &circ2);
7821 		ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_finalize,
7822 		    &constraint);
7823 		ndi_devi_exit(dip, circ2);
7824 	}
7825 
7826 	if (!constraint) {
7827 		RIO_DEBUG((CE_WARN, "retire failed: path = %s", path));
7828 	} else {
7829 		RIO_DEBUG((CE_NOTE, "retire succeeded: path = %s", path));
7830 	}
7831 
7832 	ndi_devi_exit(pdip, circ);
7833 	ndi_rele_devi(pdip);
7834 	return (constraint ? DDI_SUCCESS : DDI_FAILURE);
7835 }
7836 
7837 static int
7838 unmark_and_unfence(dev_info_t *dip, void *arg)
7839 {
7840 	char	*path = (char *)arg;
7841 
7842 	ASSERT(path);
7843 
7844 	(void) ddi_pathname(dip, path);
7845 
7846 	mutex_enter(&DEVI(dip)->devi_lock);
7847 	DEVI(dip)->devi_flags &= ~DEVI_RETIRED;
7848 	DEVI_SET_DEVICE_ONLINE(dip);
7849 	mutex_exit(&DEVI(dip)->devi_lock);
7850 
7851 	RIO_VERBOSE((CE_NOTE, "Cleared RETIRED flag: dip=%p, path=%s",
7852 	    (void *)dip, path));
7853 
7854 	(void) spec_unfence_snode(dip);
7855 	RIO_DEBUG((CE_NOTE, "Unfenced device: %s", path));
7856 
7857 	if (MDI_PHCI(dip))
7858 		mdi_phci_unretire(dip);
7859 
7860 	return (DDI_WALK_CONTINUE);
7861 }
7862 
7863 struct find_dip {
7864 	char	*fd_buf;
7865 	char	*fd_path;
7866 	dev_info_t *fd_dip;
7867 };
7868 
7869 static int
7870 find_dip_fcn(dev_info_t *dip, void *arg)
7871 {
7872 	struct find_dip *findp = (struct find_dip *)arg;
7873 
7874 	(void) ddi_pathname(dip, findp->fd_buf);
7875 
7876 	if (strcmp(findp->fd_path, findp->fd_buf) != 0)
7877 		return (DDI_WALK_CONTINUE);
7878 
7879 	ndi_hold_devi(dip);
7880 	findp->fd_dip = dip;
7881 
7882 	return (DDI_WALK_TERMINATE);
7883 }
7884 
7885 int
7886 e_ddi_unretire_device(char *path)
7887 {
7888 	int		circ;
7889 	int		circ2;
7890 	char		*path2;
7891 	dev_info_t	*pdip;
7892 	dev_info_t	*dip;
7893 	struct find_dip	 find_dip;
7894 
7895 	ASSERT(path);
7896 	ASSERT(*path == '/');
7897 
7898 	if (strcmp(path, "/") == 0) {
7899 		cmn_err(CE_WARN, "Root node cannot be retired. Skipping "
7900 		    "device unretire: %s", path);
7901 		return (0);
7902 	}
7903 
7904 	/*
7905 	 * We can't lookup the dip (corresponding to path) via
7906 	 * e_ddi_hold_devi_by_path() because the dip may be offline
7907 	 * and may not attach. Use ddi_walk_devs() instead;
7908 	 */
7909 	find_dip.fd_buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7910 	find_dip.fd_path = path;
7911 	find_dip.fd_dip = NULL;
7912 
7913 	pdip = ddi_root_node();
7914 
7915 	ndi_devi_enter(pdip, &circ);
7916 	ddi_walk_devs(ddi_get_child(pdip), find_dip_fcn, &find_dip);
7917 	ndi_devi_exit(pdip, circ);
7918 
7919 	kmem_free(find_dip.fd_buf, MAXPATHLEN);
7920 
7921 	if (find_dip.fd_dip == NULL) {
7922 		cmn_err(CE_WARN, "Device not found in device tree. Skipping "
7923 		    "device unretire: %s", path);
7924 		return (0);
7925 	}
7926 
7927 	dip = find_dip.fd_dip;
7928 
7929 	pdip = ddi_get_parent(dip);
7930 
7931 	ndi_hold_devi(pdip);
7932 
7933 	ndi_devi_enter(pdip, &circ);
7934 
7935 	path2 = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7936 
7937 	(void) unmark_and_unfence(dip, path2);
7938 	if (!is_leaf_node(dip)) {
7939 		ndi_devi_enter(dip, &circ2);
7940 		ddi_walk_devs(ddi_get_child(dip), unmark_and_unfence, path2);
7941 		ndi_devi_exit(dip, circ2);
7942 	}
7943 
7944 	kmem_free(path2, MAXPATHLEN);
7945 
7946 	/* release hold from find_dip_fcn() */
7947 	ndi_rele_devi(dip);
7948 
7949 	ndi_devi_exit(pdip, circ);
7950 
7951 	ndi_rele_devi(pdip);
7952 
7953 	return (0);
7954 }
7955 
7956 /*
7957  * Called before attach on a dip that has been retired.
7958  */
7959 static int
7960 mark_and_fence(dev_info_t *dip, void *arg)
7961 {
7962 	char    *fencepath = (char *)arg;
7963 
7964 	/*
7965 	 * We have already decided to retire this device. The various
7966 	 * constraint checking should not be set.
7967 	 * NOTE that the retire flag may already be set due to
7968 	 * fenced -> detach -> fenced transitions.
7969 	 */
7970 	mutex_enter(&DEVI(dip)->devi_lock);
7971 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
7972 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
7973 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRING));
7974 	DEVI(dip)->devi_flags |= DEVI_RETIRED;
7975 	mutex_exit(&DEVI(dip)->devi_lock);
7976 	RIO_VERBOSE((CE_NOTE, "marked as RETIRED dip=%p", (void *)dip));
7977 
7978 	if (fencepath) {
7979 		(void) spec_fence_snode(dip, NULL);
7980 		RIO_DEBUG((CE_NOTE, "Fenced: %s",
7981 		    ddi_pathname(dip, fencepath)));
7982 	}
7983 
7984 	return (DDI_WALK_CONTINUE);
7985 }
7986 
7987 /*
7988  * Checks the retire database and:
7989  *
7990  * - if device is present in the retire database, marks the device retired
7991  *   and fences it off.
7992  * - if device is not in retire database, allows the device to attach normally
7993  *
7994  * To be called only by framework attach code on first attach attempt.
7995  *
7996  */
7997 static void
7998 i_ddi_check_retire(dev_info_t *dip)
7999 {
8000 	char		*path;
8001 	dev_info_t	*pdip;
8002 	int		circ;
8003 	int		phci_only;
8004 
8005 	pdip = ddi_get_parent(dip);
8006 
8007 	/*
8008 	 * Root dip is treated special and doesn't take this code path.
8009 	 * Also root can never be retired.
8010 	 */
8011 	ASSERT(pdip);
8012 	ASSERT(DEVI_BUSY_OWNED(pdip));
8013 	ASSERT(i_ddi_node_state(dip) < DS_ATTACHED);
8014 
8015 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8016 
8017 	(void) ddi_pathname(dip, path);
8018 
8019 	RIO_VERBOSE((CE_NOTE, "Checking if dip should attach: dip=%p, path=%s",
8020 	    (void *)dip, path));
8021 
8022 	/*
8023 	 * Check if this device is in the "retired" store i.e.  should
8024 	 * be retired. If not, we have nothing to do.
8025 	 */
8026 	if (e_ddi_device_retired(path) == 0) {
8027 		RIO_VERBOSE((CE_NOTE, "device is NOT retired: path=%s", path));
8028 		kmem_free(path, MAXPATHLEN);
8029 		return;
8030 	}
8031 
8032 	RIO_DEBUG((CE_NOTE, "attach: device is retired: path=%s", path));
8033 
8034 	/*
8035 	 * Mark dips and fence off snodes (if any)
8036 	 */
8037 	RIO_DEBUG((CE_NOTE, "attach: Mark and fence subtree: path=%s", path));
8038 	(void) mark_and_fence(dip, path);
8039 	if (!is_leaf_node(dip)) {
8040 		ndi_devi_enter(dip, &circ);
8041 		ddi_walk_devs(ddi_get_child(dip), mark_and_fence, path);
8042 		ndi_devi_exit(dip, circ);
8043 	}
8044 
8045 	kmem_free(path, MAXPATHLEN);
8046 
8047 	/*
8048 	 * We don't want to check the client. We just want to
8049 	 * offline the PHCI
8050 	 */
8051 	phci_only = 1;
8052 	if (MDI_PHCI(dip))
8053 		mdi_phci_retire_finalize(dip, phci_only);
8054 }
8055