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