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