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