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