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