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