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