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