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