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