xref: /illumos-gate/usr/src/uts/common/os/devcfg.c (revision c9eab9d4e096bb9b983e9b007577edfa73c32eff)
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 2009 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 	char *path = NULL;
5592 	char *class = NULL;
5593 	char *driver = NULL;
5594 	int instance = -1;
5595 	int post_event = 0;
5596 
5597 	ASSERT(pdip && DEVI_BUSY_OWNED(pdip));
5598 
5599 	/*
5600 	 * Invoke notify if offlining
5601 	 */
5602 	if (flags & NDI_DEVI_OFFLINE) {
5603 		RIO_DEBUG((CE_NOTE, "devi_detach_node: offlining dip=%p",
5604 		    (void *)dip));
5605 		if (e_ddi_offline_notify(dip) != DDI_SUCCESS) {
5606 			RIO_DEBUG((CE_NOTE, "devi_detach_node: offline NACKed"
5607 			    "dip=%p", (void *)dip));
5608 			return (NDI_FAILURE);
5609 		}
5610 	}
5611 
5612 	if (flags & NDI_POST_EVENT) {
5613 		if (i_ddi_devi_attached(pdip)) {
5614 			if (ddi_get_eventcookie(dip, DDI_DEVI_REMOVE_EVENT,
5615 			    &cookie) == NDI_SUCCESS)
5616 				(void) ndi_post_event(dip, dip, cookie, NULL);
5617 		}
5618 	}
5619 
5620 	if (i_ddi_detachchild(dip, flags) != DDI_SUCCESS) {
5621 		if (flags & NDI_DEVI_OFFLINE) {
5622 			RIO_DEBUG((CE_NOTE, "devi_detach_node: offline failed."
5623 			    " Calling e_ddi_offline_finalize with result=%d. "
5624 			    "dip=%p", DDI_FAILURE, (void *)dip));
5625 			e_ddi_offline_finalize(dip, DDI_FAILURE);
5626 		}
5627 		return (NDI_FAILURE);
5628 	}
5629 
5630 	if (flags & NDI_DEVI_OFFLINE) {
5631 		RIO_DEBUG((CE_NOTE, "devi_detach_node: offline succeeded."
5632 		    " Calling e_ddi_offline_finalize with result=%d, "
5633 		    "dip=%p", DDI_SUCCESS, (void *)dip));
5634 		e_ddi_offline_finalize(dip, DDI_SUCCESS);
5635 	}
5636 
5637 	if (flags & NDI_AUTODETACH)
5638 		return (NDI_SUCCESS);
5639 
5640 	/*
5641 	 * For DR, even bound nodes may need to have offline
5642 	 * flag set.
5643 	 */
5644 	if (flags & NDI_DEVI_OFFLINE) {
5645 		mutex_enter(&(DEVI(dip)->devi_lock));
5646 		DEVI_SET_DEVICE_OFFLINE(dip);
5647 		mutex_exit(&(DEVI(dip)->devi_lock));
5648 	}
5649 
5650 	if (i_ddi_node_state(dip) == DS_INITIALIZED) {
5651 		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
5652 		(void) ddi_pathname(dip, path);
5653 		if (flags & NDI_DEVI_OFFLINE)
5654 			i_ndi_devi_report_status_change(dip, path);
5655 
5656 		if (need_remove_event(dip, flags)) {
5657 			post_event = 1;
5658 			class = i_ddi_strdup(i_ddi_devi_class(dip), KM_SLEEP);
5659 			driver = i_ddi_strdup((char *)ddi_driver_name(dip),
5660 			    KM_SLEEP);
5661 			instance = ddi_get_instance(dip);
5662 
5663 			mutex_enter(&(DEVI(dip)->devi_lock));
5664 			DEVI_SET_EVREMOVE(dip);
5665 			mutex_exit(&(DEVI(dip)->devi_lock));
5666 		}
5667 	}
5668 
5669 	if (flags & (NDI_UNCONFIG | NDI_DEVI_REMOVE)) {
5670 		ret = ddi_uninitchild(dip);
5671 		if (ret == NDI_SUCCESS) {
5672 			/*
5673 			 * Remove uninitialized pseudo nodes because
5674 			 * system props are lost and the node cannot be
5675 			 * reattached.
5676 			 */
5677 			if (!ndi_dev_is_persistent_node(dip))
5678 				flags |= NDI_DEVI_REMOVE;
5679 
5680 			if (flags & NDI_DEVI_REMOVE) {
5681 				ret = ddi_remove_child(dip, 0);
5682 				if (post_event && ret == NDI_SUCCESS) {
5683 					(void) i_log_devfs_remove_devinfo(path,
5684 					    class, driver, instance, flags);
5685 				}
5686 			}
5687 
5688 		}
5689 	}
5690 
5691 	if (path)
5692 		kmem_free(path, MAXPATHLEN);
5693 	if (class)
5694 		kmem_free(class, strlen(class) + 1);
5695 	if (driver)
5696 		kmem_free(driver, strlen(driver) + 1);
5697 
5698 	return (ret);
5699 }
5700 
5701 /*
5702  * unconfigure immediate children of bus nexus device
5703  */
5704 static int
5705 unconfig_immediate_children(
5706 	dev_info_t *dip,
5707 	dev_info_t **dipp,
5708 	int flags,
5709 	major_t major)
5710 {
5711 	int rv = NDI_SUCCESS;
5712 	int circ, vcirc;
5713 	dev_info_t *child;
5714 	dev_info_t *vdip = NULL;
5715 	dev_info_t *next;
5716 
5717 	ASSERT(dipp == NULL || *dipp == NULL);
5718 
5719 	/*
5720 	 * Scan forward to see if we will be processing a pHCI child. If we
5721 	 * have a child that is a pHCI and vHCI and pHCI are not siblings then
5722 	 * enter vHCI before parent(pHCI) to prevent deadlock with mpxio
5723 	 * Client power management operations.
5724 	 */
5725 	ndi_devi_enter(dip, &circ);
5726 	for (child = ddi_get_child(dip); child;
5727 	    child = ddi_get_next_sibling(child)) {
5728 		/* skip same nodes we skip below */
5729 		if (((major != DDI_MAJOR_T_NONE) &&
5730 		    (major != ddi_driver_major(child))) ||
5731 		    ((flags & NDI_AUTODETACH) && !is_leaf_node(child)))
5732 			continue;
5733 
5734 		if (MDI_PHCI(child)) {
5735 			vdip = mdi_devi_get_vdip(child);
5736 			/*
5737 			 * If vHCI and vHCI is not a sibling of pHCI
5738 			 * then enter in (vHCI, parent(pHCI)) order.
5739 			 */
5740 			if (vdip && (ddi_get_parent(vdip) != dip)) {
5741 				ndi_devi_exit(dip, circ);
5742 
5743 				/* use mdi_devi_enter ordering */
5744 				ndi_devi_enter(vdip, &vcirc);
5745 				ndi_devi_enter(dip, &circ);
5746 				break;
5747 			} else
5748 				vdip = NULL;
5749 		}
5750 	}
5751 
5752 	child = ddi_get_child(dip);
5753 	while (child) {
5754 		next = ddi_get_next_sibling(child);
5755 
5756 		if ((major != DDI_MAJOR_T_NONE) &&
5757 		    (major != ddi_driver_major(child))) {
5758 			child = next;
5759 			continue;
5760 		}
5761 
5762 		/* skip nexus nodes during autodetach */
5763 		if ((flags & NDI_AUTODETACH) && !is_leaf_node(child)) {
5764 			child = next;
5765 			continue;
5766 		}
5767 
5768 		if (devi_detach_node(child, flags) != NDI_SUCCESS) {
5769 			if (dipp && *dipp == NULL) {
5770 				ndi_hold_devi(child);
5771 				*dipp = child;
5772 			}
5773 			rv = NDI_FAILURE;
5774 		}
5775 
5776 		/*
5777 		 * Continue upon failure--best effort algorithm
5778 		 */
5779 		child = next;
5780 	}
5781 
5782 	ndi_devi_exit(dip, circ);
5783 	if (vdip)
5784 		ndi_devi_exit(vdip, vcirc);
5785 
5786 	return (rv);
5787 }
5788 
5789 /*
5790  * unconfigure grand children of bus nexus device
5791  */
5792 static int
5793 unconfig_grand_children(
5794 	dev_info_t *dip,
5795 	dev_info_t **dipp,
5796 	int flags,
5797 	major_t major,
5798 	struct brevq_node **brevqp)
5799 {
5800 	struct mt_config_handle *hdl;
5801 
5802 	if (brevqp)
5803 		*brevqp = NULL;
5804 
5805 	/* multi-threaded configuration of child nexus */
5806 	hdl = mt_config_init(dip, dipp, flags, major, MT_UNCONFIG_OP, brevqp);
5807 	mt_config_children(hdl);
5808 
5809 	return (mt_config_fini(hdl));	/* wait for threads to exit */
5810 }
5811 
5812 /*
5813  * Unconfigure children/descendants of the dip.
5814  *
5815  * If brevqp is not NULL, on return *brevqp is set to a queue of dip's
5816  * child devinames for which branch remove events need to be generated.
5817  */
5818 static int
5819 devi_unconfig_common(
5820 	dev_info_t *dip,
5821 	dev_info_t **dipp,
5822 	int flags,
5823 	major_t major,
5824 	struct brevq_node **brevqp)
5825 {
5826 	int rv;
5827 	int pm_cookie;
5828 	int (*f)();
5829 	ddi_bus_config_op_t bus_op;
5830 
5831 	if (dipp)
5832 		*dipp = NULL;
5833 	if (brevqp)
5834 		*brevqp = NULL;
5835 
5836 	/*
5837 	 * Power up the dip if it is powered off.  If the flag bit
5838 	 * NDI_AUTODETACH is set and the dip is not at its full power,
5839 	 * skip the rest of the branch.
5840 	 */
5841 	if (pm_pre_unconfig(dip, flags, &pm_cookie, NULL) != DDI_SUCCESS)
5842 		return ((flags & NDI_AUTODETACH) ? NDI_SUCCESS :
5843 		    NDI_FAILURE);
5844 
5845 	/*
5846 	 * Some callers, notably SCSI, need to clear out the devfs
5847 	 * cache together with the unconfig to prevent stale entries.
5848 	 */
5849 	if (flags & NDI_DEVFS_CLEAN)
5850 		(void) devfs_clean(dip, NULL, 0);
5851 
5852 	rv = unconfig_grand_children(dip, dipp, flags, major, brevqp);
5853 
5854 	if ((rv != NDI_SUCCESS) && ((flags & NDI_AUTODETACH) == 0)) {
5855 		if (brevqp && *brevqp) {
5856 			log_and_free_br_events_on_grand_children(dip, *brevqp);
5857 			free_brevq(*brevqp);
5858 			*brevqp = NULL;
5859 		}
5860 		pm_post_unconfig(dip, pm_cookie, NULL);
5861 		return (rv);
5862 	}
5863 
5864 	if (dipp && *dipp) {
5865 		ndi_rele_devi(*dipp);
5866 		*dipp = NULL;
5867 	}
5868 
5869 	/*
5870 	 * It is possible to have a detached nexus with children
5871 	 * and grandchildren (for example: a branch consisting
5872 	 * entirely of bound nodes.) Since the nexus is detached
5873 	 * the bus_unconfig entry point cannot be used to remove
5874 	 * or unconfigure the descendants.
5875 	 */
5876 	if (!i_ddi_devi_attached(dip) ||
5877 	    (DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
5878 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
5879 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
5880 		rv = unconfig_immediate_children(dip, dipp, flags, major);
5881 	} else {
5882 		/*
5883 		 * call bus_unconfig entry point
5884 		 * It should reset nexus flags if unconfigure succeeds.
5885 		 */
5886 		bus_op = (major == DDI_MAJOR_T_NONE) ?
5887 		    BUS_UNCONFIG_ALL : BUS_UNCONFIG_DRIVER;
5888 		rv = (*f)(dip, flags, bus_op, (void *)(uintptr_t)major);
5889 	}
5890 
5891 	pm_post_unconfig(dip, pm_cookie, NULL);
5892 
5893 	if (brevqp && *brevqp)
5894 		cleanup_br_events_on_grand_children(dip, brevqp);
5895 
5896 	return (rv);
5897 }
5898 
5899 /*
5900  * called by devfs/framework to unconfigure children bound to major
5901  * If NDI_AUTODETACH is specified, this is invoked by either the
5902  * moduninstall daemon or the modunload -i 0 command.
5903  */
5904 int
5905 ndi_devi_unconfig_driver(dev_info_t *dip, int flags, major_t major)
5906 {
5907 	NDI_CONFIG_DEBUG((CE_CONT,
5908 	    "ndi_devi_unconfig_driver: par = %s%d (%p), flags = 0x%x\n",
5909 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
5910 
5911 	return (devi_unconfig_common(dip, NULL, flags, major, NULL));
5912 }
5913 
5914 int
5915 ndi_devi_unconfig(dev_info_t *dip, int flags)
5916 {
5917 	NDI_CONFIG_DEBUG((CE_CONT,
5918 	    "ndi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
5919 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
5920 
5921 	return (devi_unconfig_common(dip, NULL, flags, DDI_MAJOR_T_NONE, NULL));
5922 }
5923 
5924 int
5925 e_ddi_devi_unconfig(dev_info_t *dip, dev_info_t **dipp, int flags)
5926 {
5927 	NDI_CONFIG_DEBUG((CE_CONT,
5928 	    "e_ddi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
5929 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
5930 
5931 	return (devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE, NULL));
5932 }
5933 
5934 /*
5935  * Unconfigure child by name
5936  */
5937 static int
5938 devi_unconfig_one(dev_info_t *pdip, char *devnm, int flags)
5939 {
5940 	int		rv, circ;
5941 	dev_info_t	*child;
5942 	dev_info_t	*vdip = NULL;
5943 	int		v_circ;
5944 
5945 	ndi_devi_enter(pdip, &circ);
5946 	child = ndi_devi_findchild(pdip, devnm);
5947 
5948 	/*
5949 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
5950 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
5951 	 * management operations.
5952 	 */
5953 	if (child && MDI_PHCI(child)) {
5954 		vdip = mdi_devi_get_vdip(child);
5955 		if (vdip && (ddi_get_parent(vdip) != pdip)) {
5956 			ndi_devi_exit(pdip, circ);
5957 
5958 			/* use mdi_devi_enter ordering */
5959 			ndi_devi_enter(vdip, &v_circ);
5960 			ndi_devi_enter(pdip, &circ);
5961 			child = ndi_devi_findchild(pdip, devnm);
5962 		} else
5963 			vdip = NULL;
5964 	}
5965 
5966 	if (child) {
5967 		rv = devi_detach_node(child, flags);
5968 	} else {
5969 		NDI_CONFIG_DEBUG((CE_CONT,
5970 		    "devi_unconfig_one: %s not found\n", devnm));
5971 		rv = NDI_SUCCESS;
5972 	}
5973 
5974 	ndi_devi_exit(pdip, circ);
5975 	if (vdip)
5976 		ndi_devi_exit(vdip, v_circ);
5977 
5978 	return (rv);
5979 }
5980 
5981 int
5982 ndi_devi_unconfig_one(
5983 	dev_info_t *pdip,
5984 	char *devnm,
5985 	dev_info_t **dipp,
5986 	int flags)
5987 {
5988 	int		(*f)();
5989 	int		circ, rv;
5990 	int		pm_cookie;
5991 	dev_info_t	*child;
5992 	dev_info_t	*vdip = NULL;
5993 	int		v_circ;
5994 	struct brevq_node *brevq = NULL;
5995 
5996 	ASSERT(i_ddi_devi_attached(pdip));
5997 
5998 	NDI_CONFIG_DEBUG((CE_CONT,
5999 	    "ndi_devi_unconfig_one: par = %s%d (%p), child = %s\n",
6000 	    ddi_driver_name(pdip), ddi_get_instance(pdip),
6001 	    (void *)pdip, devnm));
6002 
6003 	if (pm_pre_unconfig(pdip, flags, &pm_cookie, devnm) != DDI_SUCCESS)
6004 		return (NDI_FAILURE);
6005 
6006 	if (dipp)
6007 		*dipp = NULL;
6008 
6009 	ndi_devi_enter(pdip, &circ);
6010 	child = ndi_devi_findchild(pdip, devnm);
6011 
6012 	/*
6013 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
6014 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
6015 	 * management operations.
6016 	 */
6017 	if (child && MDI_PHCI(child)) {
6018 		vdip = mdi_devi_get_vdip(child);
6019 		if (vdip && (ddi_get_parent(vdip) != pdip)) {
6020 			ndi_devi_exit(pdip, circ);
6021 
6022 			/* use mdi_devi_enter ordering */
6023 			ndi_devi_enter(vdip, &v_circ);
6024 			ndi_devi_enter(pdip, &circ);
6025 			child = ndi_devi_findchild(pdip, devnm);
6026 		} else
6027 			vdip = NULL;
6028 	}
6029 
6030 	if (child == NULL) {
6031 		NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_unconfig_one: %s"
6032 		    " not found\n", devnm));
6033 		rv = NDI_SUCCESS;
6034 		goto out;
6035 	}
6036 
6037 	/*
6038 	 * Unconfigure children/descendants of named child
6039 	 */
6040 	rv = devi_unconfig_branch(child, dipp, flags | NDI_UNCONFIG, &brevq);
6041 	if (rv != NDI_SUCCESS)
6042 		goto out;
6043 
6044 	init_bound_node_ev(pdip, child, flags);
6045 
6046 	if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
6047 	    (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
6048 	    (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
6049 		rv = devi_detach_node(child, flags);
6050 	} else {
6051 		/* call bus_config entry point */
6052 		rv = (*f)(pdip, flags, BUS_UNCONFIG_ONE, (void *)devnm);
6053 	}
6054 
6055 	if (brevq) {
6056 		if (rv != NDI_SUCCESS)
6057 			log_and_free_brevq_dip(child, brevq);
6058 		else
6059 			free_brevq(brevq);
6060 	}
6061 
6062 	if (dipp && rv != NDI_SUCCESS) {
6063 		ndi_hold_devi(child);
6064 		ASSERT(*dipp == NULL);
6065 		*dipp = child;
6066 	}
6067 
6068 out:
6069 	ndi_devi_exit(pdip, circ);
6070 	if (vdip)
6071 		ndi_devi_exit(vdip, v_circ);
6072 
6073 	pm_post_unconfig(pdip, pm_cookie, devnm);
6074 
6075 	return (rv);
6076 }
6077 
6078 struct async_arg {
6079 	dev_info_t *dip;
6080 	uint_t flags;
6081 };
6082 
6083 /*
6084  * Common async handler for:
6085  *	ndi_devi_bind_driver_async
6086  *	ndi_devi_online_async
6087  */
6088 static int
6089 i_ndi_devi_async_common(dev_info_t *dip, uint_t flags, void (*func)())
6090 {
6091 	int tqflag;
6092 	int kmflag;
6093 	struct async_arg *arg;
6094 	dev_info_t *pdip = ddi_get_parent(dip);
6095 
6096 	ASSERT(pdip);
6097 	ASSERT(DEVI(pdip)->devi_taskq);
6098 	ASSERT(ndi_dev_is_persistent_node(dip));
6099 
6100 	if (flags & NDI_NOSLEEP) {
6101 		kmflag = KM_NOSLEEP;
6102 		tqflag = TQ_NOSLEEP;
6103 	} else {
6104 		kmflag = KM_SLEEP;
6105 		tqflag = TQ_SLEEP;
6106 	}
6107 
6108 	arg = kmem_alloc(sizeof (*arg), kmflag);
6109 	if (arg == NULL)
6110 		goto fail;
6111 
6112 	arg->flags = flags;
6113 	arg->dip = dip;
6114 	if (ddi_taskq_dispatch(DEVI(pdip)->devi_taskq, func, arg, tqflag) ==
6115 	    DDI_SUCCESS) {
6116 		return (NDI_SUCCESS);
6117 	}
6118 
6119 fail:
6120 	NDI_CONFIG_DEBUG((CE_CONT, "%s%d: ddi_taskq_dispatch failed",
6121 	    ddi_driver_name(pdip), ddi_get_instance(pdip)));
6122 
6123 	if (arg)
6124 		kmem_free(arg, sizeof (*arg));
6125 	return (NDI_FAILURE);
6126 }
6127 
6128 static void
6129 i_ndi_devi_bind_driver_cb(struct async_arg *arg)
6130 {
6131 	(void) ndi_devi_bind_driver(arg->dip, arg->flags);
6132 	kmem_free(arg, sizeof (*arg));
6133 }
6134 
6135 int
6136 ndi_devi_bind_driver_async(dev_info_t *dip, uint_t flags)
6137 {
6138 	return (i_ndi_devi_async_common(dip, flags,
6139 	    (void (*)())i_ndi_devi_bind_driver_cb));
6140 }
6141 
6142 /*
6143  * place the devinfo in the ONLINE state.
6144  */
6145 int
6146 ndi_devi_online(dev_info_t *dip, uint_t flags)
6147 {
6148 	int circ, rv;
6149 	dev_info_t *pdip = ddi_get_parent(dip);
6150 	int branch_event = 0;
6151 
6152 	ASSERT(pdip);
6153 
6154 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_online: %s%d (%p)\n",
6155 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
6156 
6157 	ndi_devi_enter(pdip, &circ);
6158 	/* bind child before merging .conf nodes */
6159 	rv = i_ndi_config_node(dip, DS_BOUND, flags);
6160 	if (rv != NDI_SUCCESS) {
6161 		ndi_devi_exit(pdip, circ);
6162 		return (rv);
6163 	}
6164 
6165 	/* merge .conf properties */
6166 	(void) i_ndi_make_spec_children(pdip, flags);
6167 
6168 	flags |= (NDI_DEVI_ONLINE | NDI_CONFIG);
6169 
6170 	if (flags & NDI_NO_EVENT) {
6171 		/*
6172 		 * Caller is specifically asking for not to generate an event.
6173 		 * Set the following flag so that devi_attach_node() don't
6174 		 * change the event state.
6175 		 */
6176 		flags |= NDI_NO_EVENT_STATE_CHNG;
6177 	}
6178 
6179 	if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
6180 	    ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip))) {
6181 		flags |= NDI_BRANCH_EVENT_OP;
6182 		branch_event = 1;
6183 	}
6184 
6185 	/*
6186 	 * devi_attach_node() may remove dip on failure
6187 	 */
6188 	if ((rv = devi_attach_node(dip, flags)) == NDI_SUCCESS) {
6189 		if ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip)) {
6190 			(void) ndi_devi_config(dip, flags);
6191 		}
6192 
6193 		if (branch_event)
6194 			(void) i_log_devfs_branch_add(dip);
6195 	}
6196 
6197 	ndi_devi_exit(pdip, circ);
6198 
6199 	/*
6200 	 * Notify devfs that we have a new node. Devfs needs to invalidate
6201 	 * cached directory contents.
6202 	 *
6203 	 * For PCMCIA devices, it is possible the pdip is not fully
6204 	 * attached. In this case, calling back into devfs will
6205 	 * result in a loop or assertion error. Hence, the check
6206 	 * on node state.
6207 	 *
6208 	 * If we own parent lock, this is part of a branch operation.
6209 	 * We skip the devfs_clean() step because the cache invalidation
6210 	 * is done higher up in the device tree.
6211 	 */
6212 	if (rv == NDI_SUCCESS && i_ddi_devi_attached(pdip) &&
6213 	    !DEVI_BUSY_OWNED(pdip))
6214 		(void) devfs_clean(pdip, NULL, 0);
6215 	return (rv);
6216 }
6217 
6218 static void
6219 i_ndi_devi_online_cb(struct async_arg *arg)
6220 {
6221 	(void) ndi_devi_online(arg->dip, arg->flags);
6222 	kmem_free(arg, sizeof (*arg));
6223 }
6224 
6225 int
6226 ndi_devi_online_async(dev_info_t *dip, uint_t flags)
6227 {
6228 	/* mark child as need config if requested. */
6229 	if (flags & NDI_CONFIG) {
6230 		mutex_enter(&(DEVI(dip)->devi_lock));
6231 		DEVI_SET_NDI_CONFIG(dip);
6232 		mutex_exit(&(DEVI(dip)->devi_lock));
6233 	}
6234 
6235 	return (i_ndi_devi_async_common(dip, flags,
6236 	    (void (*)())i_ndi_devi_online_cb));
6237 }
6238 
6239 /*
6240  * Take a device node Offline
6241  * To take a device Offline means to detach the device instance from
6242  * the driver and prevent devfs requests from re-attaching the device
6243  * instance.
6244  *
6245  * The flag NDI_DEVI_REMOVE causes removes the device node from
6246  * the driver list and the device tree. In this case, the device
6247  * is assumed to be removed from the system.
6248  */
6249 int
6250 ndi_devi_offline(dev_info_t *dip, uint_t flags)
6251 {
6252 	int		circ, rval = 0;
6253 	dev_info_t	*pdip = ddi_get_parent(dip);
6254 	dev_info_t	*vdip = NULL;
6255 	int		v_circ;
6256 	struct brevq_node *brevq = NULL;
6257 
6258 	ASSERT(pdip);
6259 
6260 	flags |= NDI_DEVI_OFFLINE;
6261 
6262 	/*
6263 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
6264 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
6265 	 * management operations.
6266 	 */
6267 	if (MDI_PHCI(dip)) {
6268 		vdip = mdi_devi_get_vdip(dip);
6269 		if (vdip && (ddi_get_parent(vdip) != pdip))
6270 			ndi_devi_enter(vdip, &v_circ);
6271 		else
6272 			vdip = NULL;
6273 	}
6274 	ndi_devi_enter(pdip, &circ);
6275 
6276 	if (i_ddi_node_state(dip) == DS_READY) {
6277 		/*
6278 		 * If dip is in DS_READY state, there may be cached dv_nodes
6279 		 * referencing this dip, so we invoke devfs code path.
6280 		 * Note that we must release busy changing on pdip to
6281 		 * avoid deadlock against devfs.
6282 		 */
6283 		char *devname = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
6284 		(void) ddi_deviname(dip, devname);
6285 
6286 		ndi_devi_exit(pdip, circ);
6287 		if (vdip)
6288 			ndi_devi_exit(vdip, v_circ);
6289 
6290 		/*
6291 		 * If we own parent lock, this is part of a branch
6292 		 * operation. We skip the devfs_clean() step.
6293 		 */
6294 		if (!DEVI_BUSY_OWNED(pdip))
6295 			(void) devfs_clean(pdip, devname + 1, DV_CLEAN_FORCE);
6296 		kmem_free(devname, MAXNAMELEN + 1);
6297 
6298 		rval = devi_unconfig_branch(dip, NULL, flags|NDI_UNCONFIG,
6299 		    &brevq);
6300 
6301 		if (rval)
6302 			return (NDI_FAILURE);
6303 
6304 		if (vdip)
6305 			ndi_devi_enter(vdip, &v_circ);
6306 		ndi_devi_enter(pdip, &circ);
6307 	}
6308 
6309 	init_bound_node_ev(pdip, dip, flags);
6310 
6311 	rval = devi_detach_node(dip, flags);
6312 	if (brevq) {
6313 		if (rval != NDI_SUCCESS)
6314 			log_and_free_brevq_dip(dip, brevq);
6315 		else
6316 			free_brevq(brevq);
6317 	}
6318 
6319 	ndi_devi_exit(pdip, circ);
6320 	if (vdip)
6321 		ndi_devi_exit(vdip, v_circ);
6322 
6323 	return (rval);
6324 }
6325 
6326 /*
6327  * Find the child dev_info node of parent nexus 'p' whose name
6328  * matches "cname@caddr".  Recommend use of ndi_devi_findchild() instead.
6329  */
6330 dev_info_t *
6331 ndi_devi_find(dev_info_t *pdip, char *cname, char *caddr)
6332 {
6333 	dev_info_t *child;
6334 	int circ;
6335 
6336 	if (pdip == NULL || cname == NULL || caddr == NULL)
6337 		return ((dev_info_t *)NULL);
6338 
6339 	ndi_devi_enter(pdip, &circ);
6340 	child = find_sibling(ddi_get_child(pdip), cname, caddr,
6341 	    FIND_NODE_BY_NODENAME, NULL);
6342 	ndi_devi_exit(pdip, circ);
6343 	return (child);
6344 }
6345 
6346 /*
6347  * Find the child dev_info node of parent nexus 'p' whose name
6348  * matches devname "name@addr".  Permits caller to hold the parent.
6349  */
6350 dev_info_t *
6351 ndi_devi_findchild(dev_info_t *pdip, char *devname)
6352 {
6353 	dev_info_t *child;
6354 	char	*cname, *caddr;
6355 	char	*devstr;
6356 
6357 	ASSERT(DEVI_BUSY_OWNED(pdip));
6358 
6359 	devstr = i_ddi_strdup(devname, KM_SLEEP);
6360 	i_ddi_parse_name(devstr, &cname, &caddr, NULL);
6361 
6362 	if (cname == NULL || caddr == NULL) {
6363 		kmem_free(devstr, strlen(devname)+1);
6364 		return ((dev_info_t *)NULL);
6365 	}
6366 
6367 	child = find_sibling(ddi_get_child(pdip), cname, caddr,
6368 	    FIND_NODE_BY_NODENAME, NULL);
6369 	kmem_free(devstr, strlen(devname)+1);
6370 	return (child);
6371 }
6372 
6373 /*
6374  * Misc. routines called by framework only
6375  */
6376 
6377 /*
6378  * Clear the DEVI_MADE_CHILDREN/DEVI_ATTACHED_CHILDREN flags
6379  * if new child spec has been added.
6380  */
6381 static int
6382 reset_nexus_flags(dev_info_t *dip, void *arg)
6383 {
6384 	struct hwc_spec	*list;
6385 	int		circ;
6386 
6387 	if (((DEVI(dip)->devi_flags & DEVI_MADE_CHILDREN) == 0) ||
6388 	    ((list = hwc_get_child_spec(dip, (major_t)(uintptr_t)arg)) == NULL))
6389 		return (DDI_WALK_CONTINUE);
6390 
6391 	hwc_free_spec_list(list);
6392 
6393 	/* coordinate child state update */
6394 	ndi_devi_enter(dip, &circ);
6395 	mutex_enter(&DEVI(dip)->devi_lock);
6396 	DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN | DEVI_ATTACHED_CHILDREN);
6397 	mutex_exit(&DEVI(dip)->devi_lock);
6398 	ndi_devi_exit(dip, circ);
6399 
6400 	return (DDI_WALK_CONTINUE);
6401 }
6402 
6403 /*
6404  * Helper functions, returns NULL if no memory.
6405  */
6406 
6407 /*
6408  * path_to_major:
6409  *
6410  * Return an alternate driver name binding for the leaf device
6411  * of the given pathname, if there is one. The purpose of this
6412  * function is to deal with generic pathnames. The default action
6413  * for platforms that can't do this (ie: x86 or any platform that
6414  * does not have prom_finddevice functionality, which matches
6415  * nodenames and unit-addresses without the drivers participation)
6416  * is to return DDI_MAJOR_T_NONE.
6417  *
6418  * Used in loadrootmodules() in the swapgeneric module to
6419  * associate a given pathname with a given leaf driver.
6420  *
6421  */
6422 major_t
6423 path_to_major(char *path)
6424 {
6425 	dev_info_t *dip;
6426 	char *p, *q;
6427 	pnode_t nodeid;
6428 	major_t major;
6429 
6430 	/* check for path-oriented alias */
6431 	major = ddi_name_to_major(path);
6432 	if ((major != DDI_MAJOR_T_NONE) &&
6433 	    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED)) {
6434 		NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s path bound %s\n",
6435 		    path, ddi_major_to_name(major)));
6436 		return (major);
6437 	}
6438 
6439 	/*
6440 	 * Get the nodeid of the given pathname, if such a mapping exists.
6441 	 */
6442 	dip = NULL;
6443 	nodeid = prom_finddevice(path);
6444 	if (nodeid != OBP_BADNODE) {
6445 		/*
6446 		 * Find the nodeid in our copy of the device tree and return
6447 		 * whatever name we used to bind this node to a driver.
6448 		 */
6449 		dip = e_ddi_nodeid_to_dip(nodeid);
6450 	}
6451 
6452 	if (dip == NULL) {
6453 		NDI_CONFIG_DEBUG((CE_WARN,
6454 		    "path_to_major: can't bind <%s>\n", path));
6455 		return (DDI_MAJOR_T_NONE);
6456 	}
6457 
6458 	/*
6459 	 * If we're bound to something other than the nodename,
6460 	 * note that in the message buffer and system log.
6461 	 */
6462 	p = ddi_binding_name(dip);
6463 	q = ddi_node_name(dip);
6464 	if (p && q && (strcmp(p, q) != 0))
6465 		NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s bound to %s\n",
6466 		    path, p));
6467 
6468 	major = ddi_name_to_major(p);
6469 
6470 	ndi_rele_devi(dip);		/* release e_ddi_nodeid_to_dip hold */
6471 
6472 	return (major);
6473 }
6474 
6475 /*
6476  * Return the held dip for the specified major and instance, attempting to do
6477  * an attach if specified. Return NULL if the devi can't be found or put in
6478  * the proper state. The caller must release the hold via ddi_release_devi if
6479  * a non-NULL value is returned.
6480  *
6481  * Some callers expect to be able to perform a hold_devi() while in a context
6482  * where using ndi_devi_enter() to ensure the hold might cause deadlock (see
6483  * open-from-attach code in consconfig_dacf.c). Such special-case callers
6484  * must ensure that an ndi_devi_enter(parent)/ndi_devi_hold() from a safe
6485  * context is already active. The hold_devi() implementation must accommodate
6486  * these callers.
6487  */
6488 static dev_info_t *
6489 hold_devi(major_t major, int instance, int flags)
6490 {
6491 	struct devnames	*dnp;
6492 	dev_info_t	*dip;
6493 	char		*path;
6494 
6495 	if ((major >= devcnt) || (instance == -1))
6496 		return (NULL);
6497 
6498 	/* try to find the instance in the per driver list */
6499 	dnp = &(devnamesp[major]);
6500 	LOCK_DEV_OPS(&(dnp->dn_lock));
6501 	for (dip = dnp->dn_head; dip;
6502 	    dip = (dev_info_t *)DEVI(dip)->devi_next) {
6503 		/* skip node if instance field is not valid */
6504 		if (i_ddi_node_state(dip) < DS_INITIALIZED)
6505 			continue;
6506 
6507 		/* look for instance match */
6508 		if (DEVI(dip)->devi_instance == instance) {
6509 			/*
6510 			 * To accommodate callers that can't block in
6511 			 * ndi_devi_enter() we do an ndi_devi_hold(), and
6512 			 * afterwards check that the node is in a state where
6513 			 * the hold prevents detach(). If we did not manage to
6514 			 * prevent detach then we ndi_rele_devi() and perform
6515 			 * the slow path below (which can result in a blocking
6516 			 * ndi_devi_enter() while driving attach top-down).
6517 			 * This code depends on the ordering of
6518 			 * DEVI_SET_DETACHING and the devi_ref check in the
6519 			 * detach_node() code path.
6520 			 */
6521 			ndi_hold_devi(dip);
6522 			if (i_ddi_devi_attached(dip) &&
6523 			    !DEVI_IS_DETACHING(dip)) {
6524 				UNLOCK_DEV_OPS(&(dnp->dn_lock));
6525 				return (dip);	/* fast-path with devi held */
6526 			}
6527 			ndi_rele_devi(dip);
6528 
6529 			/* try slow-path */
6530 			dip = NULL;
6531 			break;
6532 		}
6533 	}
6534 	ASSERT(dip == NULL);
6535 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
6536 
6537 	if (flags & E_DDI_HOLD_DEVI_NOATTACH)
6538 		return (NULL);		/* told not to drive attach */
6539 
6540 	/* slow-path may block, so it should not occur from interrupt */
6541 	ASSERT(!servicing_interrupt());
6542 	if (servicing_interrupt())
6543 		return (NULL);
6544 
6545 	/* reconstruct the path and drive attach by path through devfs. */
6546 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
6547 	if (e_ddi_majorinstance_to_path(major, instance, path) == 0)
6548 		dip = e_ddi_hold_devi_by_path(path, flags);
6549 	kmem_free(path, MAXPATHLEN);
6550 	return (dip);			/* with devi held */
6551 }
6552 
6553 /*
6554  * The {e_}ddi_hold_devi{_by_{instance|dev|path}} hold the devinfo node
6555  * associated with the specified arguments.  This hold should be released
6556  * by calling ddi_release_devi.
6557  *
6558  * The E_DDI_HOLD_DEVI_NOATTACH flag argument allows the caller to to specify
6559  * a failure return if the node is not already attached.
6560  *
6561  * NOTE: by the time we make e_ddi_hold_devi public, we should be able to reuse
6562  * ddi_hold_devi again.
6563  */
6564 dev_info_t *
6565 ddi_hold_devi_by_instance(major_t major, int instance, int flags)
6566 {
6567 	return (hold_devi(major, instance, flags));
6568 }
6569 
6570 dev_info_t *
6571 e_ddi_hold_devi_by_dev(dev_t dev, int flags)
6572 {
6573 	major_t	major = getmajor(dev);
6574 	dev_info_t	*dip;
6575 	struct dev_ops	*ops;
6576 	dev_info_t	*ddip = NULL;
6577 
6578 	dip = hold_devi(major, dev_to_instance(dev), flags);
6579 
6580 	/*
6581 	 * The rest of this routine is legacy support for drivers that
6582 	 * have broken DDI_INFO_DEVT2INSTANCE implementations but may have
6583 	 * functional DDI_INFO_DEVT2DEVINFO implementations.  This code will
6584 	 * diagnose inconsistency and, for maximum compatibility with legacy
6585 	 * drivers, give preference to the drivers DDI_INFO_DEVT2DEVINFO
6586 	 * implementation over the above derived dip based the driver's
6587 	 * DDI_INFO_DEVT2INSTANCE implementation. This legacy support should
6588 	 * be removed when DDI_INFO_DEVT2DEVINFO is deprecated.
6589 	 *
6590 	 * NOTE: The following code has a race condition. DEVT2DEVINFO
6591 	 *	returns a dip which is not held. By the time we ref ddip,
6592 	 *	it could have been freed. The saving grace is that for
6593 	 *	most drivers, the dip returned from hold_devi() is the
6594 	 *	same one as the one returned by DEVT2DEVINFO, so we are
6595 	 *	safe for drivers with the correct getinfo(9e) impl.
6596 	 */
6597 	if (((ops = ddi_hold_driver(major)) != NULL) &&
6598 	    CB_DRV_INSTALLED(ops) && ops->devo_getinfo)  {
6599 		if ((*ops->devo_getinfo)(NULL, DDI_INFO_DEVT2DEVINFO,
6600 		    (void *)dev, (void **)&ddip) != DDI_SUCCESS)
6601 			ddip = NULL;
6602 	}
6603 
6604 	/* give preference to the driver returned DEVT2DEVINFO dip */
6605 	if (ddip && (dip != ddip)) {
6606 #ifdef	DEBUG
6607 		cmn_err(CE_WARN, "%s: inconsistent getinfo(9E) implementation",
6608 		    ddi_driver_name(ddip));
6609 #endif	/* DEBUG */
6610 		ndi_hold_devi(ddip);
6611 		if (dip)
6612 			ndi_rele_devi(dip);
6613 		dip = ddip;
6614 	}
6615 
6616 	if (ops)
6617 		ddi_rele_driver(major);
6618 
6619 	return (dip);
6620 }
6621 
6622 /*
6623  * For compatibility only. Do not call this function!
6624  */
6625 dev_info_t *
6626 e_ddi_get_dev_info(dev_t dev, vtype_t type)
6627 {
6628 	dev_info_t *dip = NULL;
6629 	if (getmajor(dev) >= devcnt)
6630 		return (NULL);
6631 
6632 	switch (type) {
6633 	case VCHR:
6634 	case VBLK:
6635 		dip = e_ddi_hold_devi_by_dev(dev, 0);
6636 	default:
6637 		break;
6638 	}
6639 
6640 	/*
6641 	 * For compatibility reasons, we can only return the dip with
6642 	 * the driver ref count held. This is not a safe thing to do.
6643 	 * For certain broken third-party software, we are willing
6644 	 * to venture into unknown territory.
6645 	 */
6646 	if (dip) {
6647 		(void) ndi_hold_driver(dip);
6648 		ndi_rele_devi(dip);
6649 	}
6650 	return (dip);
6651 }
6652 
6653 dev_info_t *
6654 e_ddi_hold_devi_by_path(char *path, int flags)
6655 {
6656 	dev_info_t	*dip;
6657 
6658 	/* can't specify NOATTACH by path */
6659 	ASSERT(!(flags & E_DDI_HOLD_DEVI_NOATTACH));
6660 
6661 	return (resolve_pathname(path, &dip, NULL, NULL) ? NULL : dip);
6662 }
6663 
6664 void
6665 e_ddi_hold_devi(dev_info_t *dip)
6666 {
6667 	ndi_hold_devi(dip);
6668 }
6669 
6670 void
6671 ddi_release_devi(dev_info_t *dip)
6672 {
6673 	ndi_rele_devi(dip);
6674 }
6675 
6676 /*
6677  * Associate a streams queue with a devinfo node
6678  * NOTE: This function is called by STREAM driver's put procedure.
6679  *	It cannot block.
6680  */
6681 void
6682 ddi_assoc_queue_with_devi(queue_t *q, dev_info_t *dip)
6683 {
6684 	queue_t *rq = _RD(q);
6685 	struct stdata *stp;
6686 	vnode_t *vp;
6687 
6688 	/* set flag indicating that ddi_assoc_queue_with_devi was called */
6689 	mutex_enter(QLOCK(rq));
6690 	rq->q_flag |= _QASSOCIATED;
6691 	mutex_exit(QLOCK(rq));
6692 
6693 	/* get the vnode associated with the queue */
6694 	stp = STREAM(rq);
6695 	vp = stp->sd_vnode;
6696 	ASSERT(vp);
6697 
6698 	/* change the hardware association of the vnode */
6699 	spec_assoc_vp_with_devi(vp, dip);
6700 }
6701 
6702 /*
6703  * ddi_install_driver(name)
6704  *
6705  * Driver installation is currently a byproduct of driver loading.  This
6706  * may change.
6707  */
6708 int
6709 ddi_install_driver(char *name)
6710 {
6711 	major_t major = ddi_name_to_major(name);
6712 
6713 	if ((major == DDI_MAJOR_T_NONE) ||
6714 	    (ddi_hold_installed_driver(major) == NULL)) {
6715 		return (DDI_FAILURE);
6716 	}
6717 	ddi_rele_driver(major);
6718 	return (DDI_SUCCESS);
6719 }
6720 
6721 struct dev_ops *
6722 ddi_hold_driver(major_t major)
6723 {
6724 	return (mod_hold_dev_by_major(major));
6725 }
6726 
6727 
6728 void
6729 ddi_rele_driver(major_t major)
6730 {
6731 	mod_rele_dev_by_major(major);
6732 }
6733 
6734 
6735 /*
6736  * This is called during boot to force attachment order of special dips
6737  * dip must be referenced via ndi_hold_devi()
6738  */
6739 int
6740 i_ddi_attach_node_hierarchy(dev_info_t *dip)
6741 {
6742 	dev_info_t	*parent;
6743 	int		ret, circ;
6744 
6745 	/*
6746 	 * Recurse up until attached parent is found.
6747 	 */
6748 	if (i_ddi_devi_attached(dip))
6749 		return (DDI_SUCCESS);
6750 	parent = ddi_get_parent(dip);
6751 	if (i_ddi_attach_node_hierarchy(parent) != DDI_SUCCESS)
6752 		return (DDI_FAILURE);
6753 
6754 	/*
6755 	 * Come top-down, expanding .conf nodes under this parent
6756 	 * and driving attach.
6757 	 */
6758 	ndi_devi_enter(parent, &circ);
6759 	(void) i_ndi_make_spec_children(parent, 0);
6760 	ret = i_ddi_attachchild(dip);
6761 	ndi_devi_exit(parent, circ);
6762 
6763 	return (ret);
6764 }
6765 
6766 /* keep this function static */
6767 static int
6768 attach_driver_nodes(major_t major)
6769 {
6770 	struct devnames *dnp;
6771 	dev_info_t *dip;
6772 	int error = DDI_FAILURE;
6773 
6774 	dnp = &devnamesp[major];
6775 	LOCK_DEV_OPS(&dnp->dn_lock);
6776 	dip = dnp->dn_head;
6777 	while (dip) {
6778 		ndi_hold_devi(dip);
6779 		UNLOCK_DEV_OPS(&dnp->dn_lock);
6780 		if (i_ddi_attach_node_hierarchy(dip) == DDI_SUCCESS)
6781 			error = DDI_SUCCESS;
6782 		LOCK_DEV_OPS(&dnp->dn_lock);
6783 		ndi_rele_devi(dip);
6784 		dip = ddi_get_next(dip);
6785 	}
6786 	if (error == DDI_SUCCESS)
6787 		dnp->dn_flags |= DN_NO_AUTODETACH;
6788 	UNLOCK_DEV_OPS(&dnp->dn_lock);
6789 
6790 
6791 	return (error);
6792 }
6793 
6794 /*
6795  * i_ddi_attach_hw_nodes configures and attaches all hw nodes
6796  * bound to a specific driver. This function replaces calls to
6797  * ddi_hold_installed_driver() for drivers with no .conf
6798  * enumerated nodes.
6799  *
6800  * This facility is typically called at boot time to attach
6801  * platform-specific hardware nodes, such as ppm nodes on xcal
6802  * and grover and keyswitch nodes on cherrystone. It does not
6803  * deal with .conf enumerated node. Calling it beyond the boot
6804  * process is strongly discouraged.
6805  */
6806 int
6807 i_ddi_attach_hw_nodes(char *driver)
6808 {
6809 	major_t major;
6810 
6811 	major = ddi_name_to_major(driver);
6812 	if (major == DDI_MAJOR_T_NONE)
6813 		return (DDI_FAILURE);
6814 
6815 	return (attach_driver_nodes(major));
6816 }
6817 
6818 /*
6819  * i_ddi_attach_pseudo_node configures pseudo drivers which
6820  * has a single node. The .conf nodes must be enumerated
6821  * before calling this interface. The dip is held attached
6822  * upon returning.
6823  *
6824  * This facility should only be called only at boot time
6825  * by the I/O framework.
6826  */
6827 dev_info_t *
6828 i_ddi_attach_pseudo_node(char *driver)
6829 {
6830 	major_t major;
6831 	dev_info_t *dip;
6832 
6833 	major = ddi_name_to_major(driver);
6834 	if (major == DDI_MAJOR_T_NONE)
6835 		return (NULL);
6836 
6837 	if (attach_driver_nodes(major) != DDI_SUCCESS)
6838 		return (NULL);
6839 
6840 	dip = devnamesp[major].dn_head;
6841 	ASSERT(dip && ddi_get_next(dip) == NULL);
6842 	ndi_hold_devi(dip);
6843 	return (dip);
6844 }
6845 
6846 static void
6847 diplist_to_parent_major(dev_info_t *head, char parents[])
6848 {
6849 	major_t major;
6850 	dev_info_t *dip, *pdip;
6851 
6852 	for (dip = head; dip != NULL; dip = ddi_get_next(dip)) {
6853 		pdip = ddi_get_parent(dip);
6854 		ASSERT(pdip);	/* disallow rootnex.conf nodes */
6855 		major = ddi_driver_major(pdip);
6856 		if ((major != DDI_MAJOR_T_NONE) && parents[major] == 0)
6857 			parents[major] = 1;
6858 	}
6859 }
6860 
6861 /*
6862  * Call ddi_hold_installed_driver() on each parent major
6863  * and invoke mt_config_driver() to attach child major.
6864  * This is part of the implementation of ddi_hold_installed_driver.
6865  */
6866 static int
6867 attach_driver_by_parent(major_t child_major, char parents[])
6868 {
6869 	major_t par_major;
6870 	struct mt_config_handle *hdl;
6871 	int flags = NDI_DEVI_PERSIST | NDI_NO_EVENT;
6872 
6873 	hdl = mt_config_init(NULL, NULL, flags, child_major, MT_CONFIG_OP,
6874 	    NULL);
6875 	for (par_major = 0; par_major < devcnt; par_major++) {
6876 		/* disallow recursion on the same driver */
6877 		if (parents[par_major] == 0 || par_major == child_major)
6878 			continue;
6879 		if (ddi_hold_installed_driver(par_major) == NULL)
6880 			continue;
6881 		hdl->mtc_parmajor = par_major;
6882 		mt_config_driver(hdl);
6883 		ddi_rele_driver(par_major);
6884 	}
6885 	(void) mt_config_fini(hdl);
6886 
6887 	return (i_ddi_devs_attached(child_major));
6888 }
6889 
6890 int
6891 i_ddi_devs_attached(major_t major)
6892 {
6893 	dev_info_t *dip;
6894 	struct devnames *dnp;
6895 	int error = DDI_FAILURE;
6896 
6897 	/* check for attached instances */
6898 	dnp = &devnamesp[major];
6899 	LOCK_DEV_OPS(&dnp->dn_lock);
6900 	for (dip = dnp->dn_head; dip != NULL; dip = ddi_get_next(dip)) {
6901 		if (i_ddi_devi_attached(dip)) {
6902 			error = DDI_SUCCESS;
6903 			break;
6904 		}
6905 	}
6906 	UNLOCK_DEV_OPS(&dnp->dn_lock);
6907 
6908 	return (error);
6909 }
6910 
6911 int
6912 i_ddi_minor_node_count(dev_info_t *ddip, const char *node_type)
6913 {
6914 	int			circ;
6915 	struct ddi_minor_data	*dp;
6916 	int			count = 0;
6917 
6918 	ndi_devi_enter(ddip, &circ);
6919 	for (dp = DEVI(ddip)->devi_minor; dp != NULL; dp = dp->next) {
6920 		if (strcmp(dp->ddm_node_type, node_type) == 0)
6921 			count++;
6922 	}
6923 	ndi_devi_exit(ddip, circ);
6924 	return (count);
6925 }
6926 
6927 /*
6928  * ddi_hold_installed_driver configures and attaches all
6929  * instances of the specified driver. To accomplish this
6930  * it configures and attaches all possible parents of
6931  * the driver, enumerated both in h/w nodes and in the
6932  * driver's .conf file.
6933  *
6934  * NOTE: This facility is for compatibility purposes only and will
6935  *	eventually go away. Its usage is strongly discouraged.
6936  */
6937 static void
6938 enter_driver(struct devnames *dnp)
6939 {
6940 	mutex_enter(&dnp->dn_lock);
6941 	ASSERT(dnp->dn_busy_thread != curthread);
6942 	while (dnp->dn_flags & DN_DRIVER_BUSY)
6943 		cv_wait(&dnp->dn_wait, &dnp->dn_lock);
6944 	dnp->dn_flags |= DN_DRIVER_BUSY;
6945 	dnp->dn_busy_thread = curthread;
6946 	mutex_exit(&dnp->dn_lock);
6947 }
6948 
6949 static void
6950 exit_driver(struct devnames *dnp)
6951 {
6952 	mutex_enter(&dnp->dn_lock);
6953 	ASSERT(dnp->dn_busy_thread == curthread);
6954 	dnp->dn_flags &= ~DN_DRIVER_BUSY;
6955 	dnp->dn_busy_thread = NULL;
6956 	cv_broadcast(&dnp->dn_wait);
6957 	mutex_exit(&dnp->dn_lock);
6958 }
6959 
6960 struct dev_ops *
6961 ddi_hold_installed_driver(major_t major)
6962 {
6963 	struct dev_ops *ops;
6964 	struct devnames *dnp;
6965 	char *parents;
6966 	int error;
6967 
6968 	ops = ddi_hold_driver(major);
6969 	if (ops == NULL)
6970 		return (NULL);
6971 
6972 	/*
6973 	 * Return immediately if all the attach operations associated
6974 	 * with a ddi_hold_installed_driver() call have already been done.
6975 	 */
6976 	dnp = &devnamesp[major];
6977 	enter_driver(dnp);
6978 	if (dnp->dn_flags & DN_DRIVER_HELD) {
6979 		exit_driver(dnp);
6980 		if (i_ddi_devs_attached(major) == DDI_SUCCESS)
6981 			return (ops);
6982 		ddi_rele_driver(major);
6983 		return (NULL);
6984 	}
6985 
6986 	LOCK_DEV_OPS(&dnp->dn_lock);
6987 	dnp->dn_flags |= (DN_DRIVER_HELD | DN_NO_AUTODETACH);
6988 	UNLOCK_DEV_OPS(&dnp->dn_lock);
6989 
6990 	DCOMPATPRINTF((CE_CONT,
6991 	    "ddi_hold_installed_driver: %s\n", dnp->dn_name));
6992 
6993 	/*
6994 	 * When the driver has no .conf children, it is sufficient
6995 	 * to attach existing nodes in the device tree. Nodes not
6996 	 * enumerated by the OBP are not attached.
6997 	 */
6998 	if (dnp->dn_pl == NULL) {
6999 		if (attach_driver_nodes(major) == DDI_SUCCESS) {
7000 			exit_driver(dnp);
7001 			return (ops);
7002 		}
7003 		exit_driver(dnp);
7004 		ddi_rele_driver(major);
7005 		return (NULL);
7006 	}
7007 
7008 	/*
7009 	 * Driver has .conf nodes. We find all possible parents
7010 	 * and recursively all ddi_hold_installed_driver on the
7011 	 * parent driver; then we invoke ndi_config_driver()
7012 	 * on all possible parent node in parallel to speed up
7013 	 * performance.
7014 	 */
7015 	parents = kmem_zalloc(devcnt * sizeof (char), KM_SLEEP);
7016 
7017 	LOCK_DEV_OPS(&dnp->dn_lock);
7018 	/* find .conf parents */
7019 	(void) impl_parlist_to_major(dnp->dn_pl, parents);
7020 	/* find hw node parents */
7021 	diplist_to_parent_major(dnp->dn_head, parents);
7022 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7023 
7024 	error = attach_driver_by_parent(major, parents);
7025 	kmem_free(parents, devcnt * sizeof (char));
7026 	if (error == DDI_SUCCESS) {
7027 		exit_driver(dnp);
7028 		return (ops);
7029 	}
7030 
7031 	exit_driver(dnp);
7032 	ddi_rele_driver(major);
7033 	return (NULL);
7034 }
7035 
7036 /*
7037  * Default bus_config entry point for nexus drivers
7038  */
7039 int
7040 ndi_busop_bus_config(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
7041     void *arg, dev_info_t **child, clock_t timeout)
7042 {
7043 	major_t major;
7044 
7045 	/*
7046 	 * A timeout of 30 minutes or more is probably a mistake
7047 	 * This is intended to catch uses where timeout is in
7048 	 * the wrong units.  timeout must be in units of ticks.
7049 	 */
7050 	ASSERT(timeout < SEC_TO_TICK(1800));
7051 
7052 	major = DDI_MAJOR_T_NONE;
7053 	switch (op) {
7054 	case BUS_CONFIG_ONE:
7055 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config %s timeout=%ld\n",
7056 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
7057 		    (char *)arg, timeout));
7058 		return (devi_config_one(pdip, (char *)arg, child, flags,
7059 		    timeout));
7060 
7061 	case BUS_CONFIG_DRIVER:
7062 		major = (major_t)(uintptr_t)arg;
7063 		/*FALLTHROUGH*/
7064 	case BUS_CONFIG_ALL:
7065 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config timeout=%ld\n",
7066 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
7067 		    timeout));
7068 		if (timeout > 0) {
7069 			NDI_DEBUG(flags, (CE_CONT,
7070 			    "%s%d: bus config all timeout=%ld\n",
7071 			    ddi_driver_name(pdip), ddi_get_instance(pdip),
7072 			    timeout));
7073 			delay(timeout);
7074 		}
7075 		return (config_immediate_children(pdip, flags, major));
7076 
7077 	default:
7078 		return (NDI_FAILURE);
7079 	}
7080 	/*NOTREACHED*/
7081 }
7082 
7083 /*
7084  * Default busop bus_unconfig handler for nexus drivers
7085  */
7086 int
7087 ndi_busop_bus_unconfig(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
7088     void *arg)
7089 {
7090 	major_t major;
7091 
7092 	major = DDI_MAJOR_T_NONE;
7093 	switch (op) {
7094 	case BUS_UNCONFIG_ONE:
7095 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig %s\n",
7096 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
7097 		    (char *)arg));
7098 		return (devi_unconfig_one(pdip, (char *)arg, flags));
7099 
7100 	case BUS_UNCONFIG_DRIVER:
7101 		major = (major_t)(uintptr_t)arg;
7102 		/*FALLTHROUGH*/
7103 	case BUS_UNCONFIG_ALL:
7104 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig all\n",
7105 		    ddi_driver_name(pdip), ddi_get_instance(pdip)));
7106 		return (unconfig_immediate_children(pdip, NULL, flags, major));
7107 
7108 	default:
7109 		return (NDI_FAILURE);
7110 	}
7111 	/*NOTREACHED*/
7112 }
7113 
7114 /*
7115  * dummy functions to be removed
7116  */
7117 void
7118 impl_rem_dev_props(dev_info_t *dip)
7119 {
7120 	_NOTE(ARGUNUSED(dip))
7121 	/* do nothing */
7122 }
7123 
7124 /*
7125  * Determine if a node is a leaf node. If not sure, return false (0).
7126  */
7127 static int
7128 is_leaf_node(dev_info_t *dip)
7129 {
7130 	major_t major = ddi_driver_major(dip);
7131 
7132 	if (major == DDI_MAJOR_T_NONE)
7133 		return (0);
7134 
7135 	return (devnamesp[major].dn_flags & DN_LEAF_DRIVER);
7136 }
7137 
7138 /*
7139  * Multithreaded [un]configuration
7140  */
7141 static struct mt_config_handle *
7142 mt_config_init(dev_info_t *pdip, dev_info_t **dipp, int flags,
7143     major_t major, int op, struct brevq_node **brevqp)
7144 {
7145 	struct mt_config_handle	*hdl = kmem_alloc(sizeof (*hdl), KM_SLEEP);
7146 
7147 	mutex_init(&hdl->mtc_lock, NULL, MUTEX_DEFAULT, NULL);
7148 	cv_init(&hdl->mtc_cv, NULL, CV_DEFAULT, NULL);
7149 	hdl->mtc_pdip = pdip;
7150 	hdl->mtc_fdip = dipp;
7151 	hdl->mtc_parmajor = DDI_MAJOR_T_NONE;
7152 	hdl->mtc_flags = flags;
7153 	hdl->mtc_major = major;
7154 	hdl->mtc_thr_count = 0;
7155 	hdl->mtc_op = op;
7156 	hdl->mtc_error = 0;
7157 	hdl->mtc_brevqp = brevqp;
7158 
7159 #ifdef DEBUG
7160 	gethrestime(&hdl->start_time);
7161 	hdl->total_time = 0;
7162 #endif /* DEBUG */
7163 
7164 	return (hdl);
7165 }
7166 
7167 #ifdef DEBUG
7168 static int
7169 time_diff_in_msec(timestruc_t start, timestruc_t end)
7170 {
7171 	int	nsec, sec;
7172 
7173 	sec = end.tv_sec - start.tv_sec;
7174 	nsec = end.tv_nsec - start.tv_nsec;
7175 	if (nsec < 0) {
7176 		nsec += NANOSEC;
7177 		sec -= 1;
7178 	}
7179 
7180 	return (sec * (NANOSEC >> 20) + (nsec >> 20));
7181 }
7182 
7183 #endif	/* DEBUG */
7184 
7185 static int
7186 mt_config_fini(struct mt_config_handle *hdl)
7187 {
7188 	int		rv;
7189 #ifdef DEBUG
7190 	int		real_time;
7191 	timestruc_t	end_time;
7192 #endif /* DEBUG */
7193 
7194 	mutex_enter(&hdl->mtc_lock);
7195 	while (hdl->mtc_thr_count > 0)
7196 		cv_wait(&hdl->mtc_cv, &hdl->mtc_lock);
7197 	rv = hdl->mtc_error;
7198 	mutex_exit(&hdl->mtc_lock);
7199 
7200 #ifdef DEBUG
7201 	gethrestime(&end_time);
7202 	real_time = time_diff_in_msec(hdl->start_time, end_time);
7203 	if ((ddidebug & DDI_MTCONFIG) && hdl->mtc_pdip)
7204 		cmn_err(CE_NOTE,
7205 		    "config %s%d: total time %d msec, real time %d msec",
7206 		    ddi_driver_name(hdl->mtc_pdip),
7207 		    ddi_get_instance(hdl->mtc_pdip),
7208 		    hdl->total_time, real_time);
7209 #endif /* DEBUG */
7210 
7211 	cv_destroy(&hdl->mtc_cv);
7212 	mutex_destroy(&hdl->mtc_lock);
7213 	kmem_free(hdl, sizeof (*hdl));
7214 
7215 	return (rv);
7216 }
7217 
7218 struct mt_config_data {
7219 	struct mt_config_handle	*mtc_hdl;
7220 	dev_info_t		*mtc_dip;
7221 	major_t			mtc_major;
7222 	int			mtc_flags;
7223 	struct brevq_node	*mtc_brn;
7224 	struct mt_config_data	*mtc_next;
7225 };
7226 
7227 static void
7228 mt_config_thread(void *arg)
7229 {
7230 	struct mt_config_data	*mcd = (struct mt_config_data *)arg;
7231 	struct mt_config_handle	*hdl = mcd->mtc_hdl;
7232 	dev_info_t		*dip = mcd->mtc_dip;
7233 	dev_info_t		*rdip, **dipp;
7234 	major_t			major = mcd->mtc_major;
7235 	int			flags = mcd->mtc_flags;
7236 	int			rv = 0;
7237 
7238 #ifdef DEBUG
7239 	timestruc_t start_time, end_time;
7240 	gethrestime(&start_time);
7241 #endif /* DEBUG */
7242 
7243 	rdip = NULL;
7244 	dipp = hdl->mtc_fdip ? &rdip : NULL;
7245 
7246 	switch (hdl->mtc_op) {
7247 	case MT_CONFIG_OP:
7248 		rv = devi_config_common(dip, flags, major);
7249 		break;
7250 	case MT_UNCONFIG_OP:
7251 		if (mcd->mtc_brn) {
7252 			struct brevq_node *brevq = NULL;
7253 			rv = devi_unconfig_common(dip, dipp, flags, major,
7254 			    &brevq);
7255 			mcd->mtc_brn->brn_child = brevq;
7256 		} else
7257 			rv = devi_unconfig_common(dip, dipp, flags, major,
7258 			    NULL);
7259 		break;
7260 	}
7261 
7262 	mutex_enter(&hdl->mtc_lock);
7263 #ifdef DEBUG
7264 	gethrestime(&end_time);
7265 	hdl->total_time += time_diff_in_msec(start_time, end_time);
7266 #endif /* DEBUG */
7267 
7268 	if ((rv != NDI_SUCCESS) && (hdl->mtc_error == 0)) {
7269 		hdl->mtc_error = rv;
7270 #ifdef	DEBUG
7271 		if ((ddidebug & DDI_DEBUG) && (major != DDI_MAJOR_T_NONE)) {
7272 			char	*path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7273 
7274 			(void) ddi_pathname(dip, path);
7275 			cmn_err(CE_NOTE, "mt_config_thread: "
7276 			    "op %d.%d.%x at %s failed %d",
7277 			    hdl->mtc_op, major, flags, path, rv);
7278 			kmem_free(path, MAXPATHLEN);
7279 		}
7280 #endif	/* DEBUG */
7281 	}
7282 
7283 	if (hdl->mtc_fdip && *hdl->mtc_fdip == NULL) {
7284 		*hdl->mtc_fdip = rdip;
7285 		rdip = NULL;
7286 	}
7287 
7288 	if (rdip) {
7289 		ASSERT(rv != NDI_SUCCESS);
7290 		ndi_rele_devi(rdip);
7291 	}
7292 
7293 	ndi_rele_devi(dip);
7294 
7295 	if (--hdl->mtc_thr_count == 0)
7296 		cv_broadcast(&hdl->mtc_cv);
7297 	mutex_exit(&hdl->mtc_lock);
7298 	kmem_free(mcd, sizeof (*mcd));
7299 }
7300 
7301 /*
7302  * Multi-threaded config/unconfig of child nexus
7303  */
7304 static void
7305 mt_config_children(struct mt_config_handle *hdl)
7306 {
7307 	dev_info_t		*pdip = hdl->mtc_pdip;
7308 	major_t			major = hdl->mtc_major;
7309 	dev_info_t		*dip;
7310 	int			circ;
7311 	struct brevq_node	*brn;
7312 	struct mt_config_data	*mcd_head = NULL;
7313 	struct mt_config_data	*mcd_tail = NULL;
7314 	struct mt_config_data	*mcd;
7315 #ifdef DEBUG
7316 	timestruc_t		end_time;
7317 
7318 	/* Update total_time in handle */
7319 	gethrestime(&end_time);
7320 	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
7321 #endif
7322 
7323 	ndi_devi_enter(pdip, &circ);
7324 	dip = ddi_get_child(pdip);
7325 	while (dip) {
7326 		if (hdl->mtc_op == MT_UNCONFIG_OP && hdl->mtc_brevqp &&
7327 		    !(DEVI_EVREMOVE(dip)) &&
7328 		    i_ddi_node_state(dip) >= DS_INITIALIZED) {
7329 			/*
7330 			 * Enqueue this dip's deviname.
7331 			 * No need to hold a lock while enqueuing since this
7332 			 * is the only thread doing the enqueue and no one
7333 			 * walks the queue while we are in multithreaded
7334 			 * unconfiguration.
7335 			 */
7336 			brn = brevq_enqueue(hdl->mtc_brevqp, dip, NULL);
7337 		} else
7338 			brn = NULL;
7339 
7340 		/*
7341 		 * Hold the child that we are processing so he does not get
7342 		 * removed. The corrisponding ndi_rele_devi() for children
7343 		 * that are not being skipped is done at the end of
7344 		 * mt_config_thread().
7345 		 */
7346 		ndi_hold_devi(dip);
7347 
7348 		/*
7349 		 * skip leaf nodes and (for configure) nodes not
7350 		 * fully attached.
7351 		 */
7352 		if (is_leaf_node(dip) ||
7353 		    (hdl->mtc_op == MT_CONFIG_OP &&
7354 		    i_ddi_node_state(dip) < DS_READY)) {
7355 			ndi_rele_devi(dip);
7356 			dip = ddi_get_next_sibling(dip);
7357 			continue;
7358 		}
7359 
7360 		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
7361 		mcd->mtc_dip = dip;
7362 		mcd->mtc_hdl = hdl;
7363 		mcd->mtc_brn = brn;
7364 
7365 		/*
7366 		 * Switch a 'driver' operation to an 'all' operation below a
7367 		 * node bound to the driver.
7368 		 */
7369 		if ((major == DDI_MAJOR_T_NONE) ||
7370 		    (major == ddi_driver_major(dip)))
7371 			mcd->mtc_major = DDI_MAJOR_T_NONE;
7372 		else
7373 			mcd->mtc_major = major;
7374 
7375 		/*
7376 		 * The unconfig-driver to unconfig-all conversion above
7377 		 * constitutes an autodetach for NDI_DETACH_DRIVER calls,
7378 		 * set NDI_AUTODETACH.
7379 		 */
7380 		mcd->mtc_flags = hdl->mtc_flags;
7381 		if ((mcd->mtc_flags & NDI_DETACH_DRIVER) &&
7382 		    (hdl->mtc_op == MT_UNCONFIG_OP) &&
7383 		    (major == ddi_driver_major(pdip)))
7384 			mcd->mtc_flags |= NDI_AUTODETACH;
7385 
7386 		mutex_enter(&hdl->mtc_lock);
7387 		hdl->mtc_thr_count++;
7388 		mutex_exit(&hdl->mtc_lock);
7389 
7390 		/*
7391 		 * Add to end of list to process after ndi_devi_exit to avoid
7392 		 * locking differences depending on value of mtc_off.
7393 		 */
7394 		mcd->mtc_next = NULL;
7395 		if (mcd_head == NULL)
7396 			mcd_head = mcd;
7397 		else
7398 			mcd_tail->mtc_next = mcd;
7399 		mcd_tail = mcd;
7400 
7401 		dip = ddi_get_next_sibling(dip);
7402 	}
7403 	ndi_devi_exit(pdip, circ);
7404 
7405 	/* go through the list of held children */
7406 	for (mcd = mcd_head; mcd; mcd = mcd_head) {
7407 		mcd_head = mcd->mtc_next;
7408 		if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
7409 			mt_config_thread(mcd);
7410 		else
7411 			(void) thread_create(NULL, 0, mt_config_thread, mcd,
7412 			    0, &p0, TS_RUN, minclsyspri);
7413 	}
7414 }
7415 
7416 static void
7417 mt_config_driver(struct mt_config_handle *hdl)
7418 {
7419 	major_t			par_major = hdl->mtc_parmajor;
7420 	major_t			major = hdl->mtc_major;
7421 	struct devnames		*dnp = &devnamesp[par_major];
7422 	dev_info_t		*dip;
7423 	struct mt_config_data	*mcd_head = NULL;
7424 	struct mt_config_data	*mcd_tail = NULL;
7425 	struct mt_config_data	*mcd;
7426 #ifdef DEBUG
7427 	timestruc_t		end_time;
7428 
7429 	/* Update total_time in handle */
7430 	gethrestime(&end_time);
7431 	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
7432 #endif
7433 	ASSERT(par_major != DDI_MAJOR_T_NONE);
7434 	ASSERT(major != DDI_MAJOR_T_NONE);
7435 
7436 	LOCK_DEV_OPS(&dnp->dn_lock);
7437 	dip = devnamesp[par_major].dn_head;
7438 	while (dip) {
7439 		/*
7440 		 * Hold the child that we are processing so he does not get
7441 		 * removed. The corrisponding ndi_rele_devi() for children
7442 		 * that are not being skipped is done at the end of
7443 		 * mt_config_thread().
7444 		 */
7445 		ndi_hold_devi(dip);
7446 
7447 		/* skip leaf nodes and nodes not fully attached */
7448 		if (!i_ddi_devi_attached(dip) || is_leaf_node(dip)) {
7449 			ndi_rele_devi(dip);
7450 			dip = ddi_get_next(dip);
7451 			continue;
7452 		}
7453 
7454 		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
7455 		mcd->mtc_dip = dip;
7456 		mcd->mtc_hdl = hdl;
7457 		mcd->mtc_major = major;
7458 		mcd->mtc_flags = hdl->mtc_flags;
7459 
7460 		mutex_enter(&hdl->mtc_lock);
7461 		hdl->mtc_thr_count++;
7462 		mutex_exit(&hdl->mtc_lock);
7463 
7464 		/*
7465 		 * Add to end of list to process after UNLOCK_DEV_OPS to avoid
7466 		 * locking differences depending on value of mtc_off.
7467 		 */
7468 		mcd->mtc_next = NULL;
7469 		if (mcd_head == NULL)
7470 			mcd_head = mcd;
7471 		else
7472 			mcd_tail->mtc_next = mcd;
7473 		mcd_tail = mcd;
7474 
7475 		dip = ddi_get_next(dip);
7476 	}
7477 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7478 
7479 	/* go through the list of held children */
7480 	for (mcd = mcd_head; mcd; mcd = mcd_head) {
7481 		mcd_head = mcd->mtc_next;
7482 		if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
7483 			mt_config_thread(mcd);
7484 		else
7485 			(void) thread_create(NULL, 0, mt_config_thread, mcd,
7486 			    0, &p0, TS_RUN, minclsyspri);
7487 	}
7488 }
7489 
7490 /*
7491  * Given the nodeid for a persistent (PROM or SID) node, return
7492  * the corresponding devinfo node
7493  * NOTE: This function will return NULL for .conf nodeids.
7494  */
7495 dev_info_t *
7496 e_ddi_nodeid_to_dip(pnode_t nodeid)
7497 {
7498 	dev_info_t		*dip = NULL;
7499 	struct devi_nodeid	*prev, *elem;
7500 
7501 	mutex_enter(&devimap->dno_lock);
7502 
7503 	prev = NULL;
7504 	for (elem = devimap->dno_head; elem; elem = elem->next) {
7505 		if (elem->nodeid == nodeid) {
7506 			ndi_hold_devi(elem->dip);
7507 			dip = elem->dip;
7508 			break;
7509 		}
7510 		prev = elem;
7511 	}
7512 
7513 	/*
7514 	 * Move to head for faster lookup next time
7515 	 */
7516 	if (elem && prev) {
7517 		prev->next = elem->next;
7518 		elem->next = devimap->dno_head;
7519 		devimap->dno_head = elem;
7520 	}
7521 
7522 	mutex_exit(&devimap->dno_lock);
7523 	return (dip);
7524 }
7525 
7526 static void
7527 free_cache_task(void *arg)
7528 {
7529 	ASSERT(arg == NULL);
7530 
7531 	mutex_enter(&di_cache.cache_lock);
7532 
7533 	/*
7534 	 * The cache can be invalidated without holding the lock
7535 	 * but it can be made valid again only while the lock is held.
7536 	 * So if the cache is invalid when the lock is held, it will
7537 	 * stay invalid until lock is released.
7538 	 */
7539 	if (!di_cache.cache_valid)
7540 		i_ddi_di_cache_free(&di_cache);
7541 
7542 	mutex_exit(&di_cache.cache_lock);
7543 
7544 	if (di_cache_debug)
7545 		cmn_err(CE_NOTE, "system_taskq: di_cache freed");
7546 }
7547 
7548 extern int modrootloaded;
7549 
7550 void
7551 i_ddi_di_cache_free(struct di_cache *cache)
7552 {
7553 	int	error;
7554 	extern int sys_shutdown;
7555 
7556 	ASSERT(mutex_owned(&cache->cache_lock));
7557 
7558 	if (cache->cache_size) {
7559 		ASSERT(cache->cache_size > 0);
7560 		ASSERT(cache->cache_data);
7561 
7562 		kmem_free(cache->cache_data, cache->cache_size);
7563 		cache->cache_data = NULL;
7564 		cache->cache_size = 0;
7565 
7566 		if (di_cache_debug)
7567 			cmn_err(CE_NOTE, "i_ddi_di_cache_free: freed cachemem");
7568 	} else {
7569 		ASSERT(cache->cache_data == NULL);
7570 		if (di_cache_debug)
7571 			cmn_err(CE_NOTE, "i_ddi_di_cache_free: NULL cache");
7572 	}
7573 
7574 	if (!modrootloaded || rootvp == NULL ||
7575 	    vn_is_readonly(rootvp) || sys_shutdown) {
7576 		if (di_cache_debug) {
7577 			cmn_err(CE_WARN, "/ not mounted/RDONLY. Skip unlink");
7578 		}
7579 		return;
7580 	}
7581 
7582 	error = vn_remove(DI_CACHE_FILE, UIO_SYSSPACE, RMFILE);
7583 	if (di_cache_debug && error && error != ENOENT) {
7584 		cmn_err(CE_WARN, "%s: unlink failed: %d", DI_CACHE_FILE, error);
7585 	} else if (di_cache_debug && !error) {
7586 		cmn_err(CE_NOTE, "i_ddi_di_cache_free: unlinked cache file");
7587 	}
7588 }
7589 
7590 void
7591 i_ddi_di_cache_invalidate(int kmflag)
7592 {
7593 	int	cache_valid;
7594 
7595 	if (!modrootloaded || !i_ddi_io_initialized()) {
7596 		if (di_cache_debug)
7597 			cmn_err(CE_NOTE, "I/O not inited. Skipping invalidate");
7598 		return;
7599 	}
7600 
7601 	/* Increment devtree generation number. */
7602 	atomic_inc_ulong(&devtree_gen);
7603 
7604 	/* Invalidate the in-core cache and dispatch free on valid->invalid */
7605 	cache_valid = atomic_swap_uint(&di_cache.cache_valid, 0);
7606 	if (cache_valid) {
7607 		(void) taskq_dispatch(system_taskq, free_cache_task, NULL,
7608 		    (kmflag == KM_SLEEP) ? TQ_SLEEP : TQ_NOSLEEP);
7609 	}
7610 
7611 	if (di_cache_debug) {
7612 		cmn_err(CE_NOTE, "invalidation with km_flag: %s",
7613 		    kmflag == KM_SLEEP ? "KM_SLEEP" : "KM_NOSLEEP");
7614 	}
7615 }
7616 
7617 
7618 static void
7619 i_bind_vhci_node(dev_info_t *dip)
7620 {
7621 	DEVI(dip)->devi_major = ddi_name_to_major(ddi_node_name(dip));
7622 	i_ddi_set_node_state(dip, DS_BOUND);
7623 }
7624 
7625 static char vhci_node_addr[2];
7626 
7627 static int
7628 i_init_vhci_node(dev_info_t *dip)
7629 {
7630 	add_global_props(dip);
7631 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
7632 	if (DEVI(dip)->devi_ops == NULL)
7633 		return (-1);
7634 
7635 	DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
7636 	e_ddi_keep_instance(dip);
7637 	vhci_node_addr[0]	= '\0';
7638 	ddi_set_name_addr(dip, vhci_node_addr);
7639 	i_ddi_set_node_state(dip, DS_INITIALIZED);
7640 	return (0);
7641 }
7642 
7643 static void
7644 i_link_vhci_node(dev_info_t *dip)
7645 {
7646 	ASSERT(MUTEX_HELD(&global_vhci_lock));
7647 
7648 	/*
7649 	 * scsi_vhci should be kept left most of the device tree.
7650 	 */
7651 	if (scsi_vhci_dip) {
7652 		DEVI(dip)->devi_sibling = DEVI(scsi_vhci_dip)->devi_sibling;
7653 		DEVI(scsi_vhci_dip)->devi_sibling = DEVI(dip);
7654 	} else {
7655 		DEVI(dip)->devi_sibling = DEVI(top_devinfo)->devi_child;
7656 		DEVI(top_devinfo)->devi_child = DEVI(dip);
7657 	}
7658 }
7659 
7660 
7661 /*
7662  * This a special routine to enumerate vhci node (child of rootnex
7663  * node) without holding the ndi_devi_enter() lock. The device node
7664  * is allocated, initialized and brought into DS_READY state before
7665  * inserting into the device tree. The VHCI node is handcrafted
7666  * here to bring the node to DS_READY, similar to rootnex node.
7667  *
7668  * The global_vhci_lock protects linking the node into the device
7669  * as same lock is held before linking/unlinking any direct child
7670  * of rootnex children.
7671  *
7672  * This routine is a workaround to handle a possible deadlock
7673  * that occurs while trying to enumerate node in a different sub-tree
7674  * during _init/_attach entry points.
7675  */
7676 /*ARGSUSED*/
7677 dev_info_t *
7678 ndi_devi_config_vhci(char *drvname, int flags)
7679 {
7680 	struct devnames		*dnp;
7681 	dev_info_t		*dip;
7682 	major_t			major = ddi_name_to_major(drvname);
7683 
7684 	if (major == -1)
7685 		return (NULL);
7686 
7687 	/* Make sure we create the VHCI node only once */
7688 	dnp = &devnamesp[major];
7689 	LOCK_DEV_OPS(&dnp->dn_lock);
7690 	if (dnp->dn_head) {
7691 		dip = dnp->dn_head;
7692 		UNLOCK_DEV_OPS(&dnp->dn_lock);
7693 		return (dip);
7694 	}
7695 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7696 
7697 	/* Allocate the VHCI node */
7698 	ndi_devi_alloc_sleep(top_devinfo, drvname, DEVI_SID_NODEID, &dip);
7699 	ndi_hold_devi(dip);
7700 
7701 	/* Mark the node as VHCI */
7702 	DEVI(dip)->devi_node_attributes |= DDI_VHCI_NODE;
7703 
7704 	i_ddi_add_devimap(dip);
7705 	i_bind_vhci_node(dip);
7706 	if (i_init_vhci_node(dip) == -1) {
7707 		ndi_rele_devi(dip);
7708 		(void) ndi_devi_free(dip);
7709 		return (NULL);
7710 	}
7711 
7712 	mutex_enter(&(DEVI(dip)->devi_lock));
7713 	DEVI_SET_ATTACHING(dip);
7714 	mutex_exit(&(DEVI(dip)->devi_lock));
7715 
7716 	if (devi_attach(dip, DDI_ATTACH) != DDI_SUCCESS) {
7717 		cmn_err(CE_CONT, "Could not attach %s driver", drvname);
7718 		e_ddi_free_instance(dip, vhci_node_addr);
7719 		ndi_rele_devi(dip);
7720 		(void) ndi_devi_free(dip);
7721 		return (NULL);
7722 	}
7723 	mutex_enter(&(DEVI(dip)->devi_lock));
7724 	DEVI_CLR_ATTACHING(dip);
7725 	mutex_exit(&(DEVI(dip)->devi_lock));
7726 
7727 	mutex_enter(&global_vhci_lock);
7728 	i_link_vhci_node(dip);
7729 	mutex_exit(&global_vhci_lock);
7730 	i_ddi_set_node_state(dip, DS_READY);
7731 
7732 	LOCK_DEV_OPS(&dnp->dn_lock);
7733 	dnp->dn_flags |= DN_DRIVER_HELD;
7734 	dnp->dn_head = dip;
7735 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7736 
7737 	i_ndi_devi_report_status_change(dip, NULL);
7738 
7739 	return (dip);
7740 }
7741 
7742 /*
7743  * ibt_hw_is_present() returns 0 when there is no IB hardware actively
7744  * running.  This is primarily useful for modules like rpcmod which
7745  * needs a quick check to decide whether or not it should try to use
7746  * InfiniBand
7747  */
7748 int ib_hw_status = 0;
7749 int
7750 ibt_hw_is_present()
7751 {
7752 	return (ib_hw_status);
7753 }
7754 
7755 /*
7756  * ASSERT that constraint flag is not set and then set the "retire attempt"
7757  * flag.
7758  */
7759 int
7760 e_ddi_mark_retiring(dev_info_t *dip, void *arg)
7761 {
7762 	char	**cons_array = (char **)arg;
7763 	char	*path;
7764 	int	constraint;
7765 	int	i;
7766 
7767 	constraint = 0;
7768 	if (cons_array) {
7769 		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7770 		(void) ddi_pathname(dip, path);
7771 		for (i = 0; cons_array[i] != NULL; i++) {
7772 			if (strcmp(path, cons_array[i]) == 0) {
7773 				constraint = 1;
7774 				break;
7775 			}
7776 		}
7777 		kmem_free(path, MAXPATHLEN);
7778 	}
7779 
7780 	mutex_enter(&DEVI(dip)->devi_lock);
7781 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
7782 	DEVI(dip)->devi_flags |= DEVI_RETIRING;
7783 	if (constraint)
7784 		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
7785 	mutex_exit(&DEVI(dip)->devi_lock);
7786 
7787 	RIO_VERBOSE((CE_NOTE, "marked dip as undergoing retire process dip=%p",
7788 	    (void *)dip));
7789 
7790 	if (constraint)
7791 		RIO_DEBUG((CE_NOTE, "marked dip as constrained, dip=%p",
7792 		    (void *)dip));
7793 
7794 	if (MDI_PHCI(dip))
7795 		mdi_phci_mark_retiring(dip, cons_array);
7796 
7797 	return (DDI_WALK_CONTINUE);
7798 }
7799 
7800 static void
7801 free_array(char **cons_array)
7802 {
7803 	int	i;
7804 
7805 	if (cons_array == NULL)
7806 		return;
7807 
7808 	for (i = 0; cons_array[i] != NULL; i++) {
7809 		kmem_free(cons_array[i], strlen(cons_array[i]) + 1);
7810 	}
7811 	kmem_free(cons_array, (i+1) * sizeof (char *));
7812 }
7813 
7814 /*
7815  * Walk *every* node in subtree and check if it blocks, allows or has no
7816  * comment on a proposed retire.
7817  */
7818 int
7819 e_ddi_retire_notify(dev_info_t *dip, void *arg)
7820 {
7821 	int	*constraint = (int *)arg;
7822 
7823 	RIO_DEBUG((CE_NOTE, "retire notify: dip = %p", (void *)dip));
7824 
7825 	(void) e_ddi_offline_notify(dip);
7826 
7827 	mutex_enter(&(DEVI(dip)->devi_lock));
7828 	if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) {
7829 		RIO_DEBUG((CE_WARN, "retire notify: dip in retire "
7830 		    "subtree is not marked: dip = %p", (void *)dip));
7831 		*constraint = 0;
7832 	} else if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) {
7833 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
7834 		RIO_DEBUG((CE_NOTE, "retire notify: BLOCKED: dip = %p",
7835 		    (void *)dip));
7836 		*constraint = 0;
7837 	} else if (!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT)) {
7838 		RIO_DEBUG((CE_NOTE, "retire notify: NO CONSTRAINT: "
7839 		    "dip = %p", (void *)dip));
7840 		*constraint = 0;
7841 	} else {
7842 		RIO_DEBUG((CE_NOTE, "retire notify: CONSTRAINT set: "
7843 		    "dip = %p", (void *)dip));
7844 	}
7845 	mutex_exit(&DEVI(dip)->devi_lock);
7846 
7847 	if (MDI_PHCI(dip))
7848 		mdi_phci_retire_notify(dip, constraint);
7849 
7850 	return (DDI_WALK_CONTINUE);
7851 }
7852 
7853 int
7854 e_ddi_retire_finalize(dev_info_t *dip, void *arg)
7855 {
7856 	int constraint = *(int *)arg;
7857 	int finalize;
7858 	int phci_only;
7859 
7860 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
7861 
7862 	mutex_enter(&DEVI(dip)->devi_lock);
7863 	if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) {
7864 		RIO_DEBUG((CE_WARN,
7865 		    "retire: unmarked dip(%p) in retire subtree",
7866 		    (void *)dip));
7867 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRED));
7868 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
7869 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
7870 		mutex_exit(&DEVI(dip)->devi_lock);
7871 		return (DDI_WALK_CONTINUE);
7872 	}
7873 
7874 	/*
7875 	 * retire the device if constraints have been applied
7876 	 * or if the device is not in use
7877 	 */
7878 	finalize = 0;
7879 	if (constraint) {
7880 		ASSERT(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT);
7881 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
7882 		DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
7883 		DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
7884 		DEVI(dip)->devi_flags |= DEVI_RETIRED;
7885 		mutex_exit(&DEVI(dip)->devi_lock);
7886 		(void) spec_fence_snode(dip, NULL);
7887 		RIO_DEBUG((CE_NOTE, "Fenced off: dip = %p", (void *)dip));
7888 		e_ddi_offline_finalize(dip, DDI_SUCCESS);
7889 	} else {
7890 		if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) {
7891 			ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
7892 			DEVI(dip)->devi_flags &= ~DEVI_R_BLOCKED;
7893 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
7894 			/* we have already finalized during notify */
7895 		} else if (DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT) {
7896 			DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
7897 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
7898 			finalize = 1;
7899 		} else {
7900 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
7901 			/*
7902 			 * even if no contracts, need to call finalize
7903 			 * to clear the contract barrier on the dip
7904 			 */
7905 			finalize = 1;
7906 		}
7907 		mutex_exit(&DEVI(dip)->devi_lock);
7908 		RIO_DEBUG((CE_NOTE, "finalize: NOT retired: dip = %p",
7909 		    (void *)dip));
7910 		if (finalize)
7911 			e_ddi_offline_finalize(dip, DDI_FAILURE);
7912 	}
7913 
7914 	/*
7915 	 * phci_only variable indicates no client checking, just
7916 	 * offline the PHCI. We set that to 0 to enable client
7917 	 * checking
7918 	 */
7919 	phci_only = 0;
7920 	if (MDI_PHCI(dip))
7921 		mdi_phci_retire_finalize(dip, phci_only);
7922 
7923 	return (DDI_WALK_CONTINUE);
7924 }
7925 
7926 /*
7927  * Returns
7928  * 	DDI_SUCCESS if constraints allow retire
7929  *	DDI_FAILURE if constraints don't allow retire.
7930  * cons_array is a NULL terminated array of node paths for
7931  * which constraints have already been applied.
7932  */
7933 int
7934 e_ddi_retire_device(char *path, char **cons_array)
7935 {
7936 	dev_info_t	*dip;
7937 	dev_info_t	*pdip;
7938 	int		circ;
7939 	int		circ2;
7940 	int		constraint;
7941 	char		*devnm;
7942 
7943 	/*
7944 	 * First, lookup the device
7945 	 */
7946 	dip = e_ddi_hold_devi_by_path(path, 0);
7947 	if (dip == NULL) {
7948 		/*
7949 		 * device does not exist. This device cannot be
7950 		 * a critical device since it is not in use. Thus
7951 		 * this device is always retireable. Return DDI_SUCCESS
7952 		 * to indicate this. If this device is ever
7953 		 * instantiated, I/O framework will consult the
7954 		 * the persistent retire store, mark it as
7955 		 * retired and fence it off.
7956 		 */
7957 		RIO_DEBUG((CE_NOTE, "Retire device: device doesn't exist."
7958 		    " NOP. Just returning SUCCESS. path=%s", path));
7959 		free_array(cons_array);
7960 		return (DDI_SUCCESS);
7961 	}
7962 
7963 	RIO_DEBUG((CE_NOTE, "Retire device: found dip = %p.", (void *)dip));
7964 
7965 	pdip = ddi_get_parent(dip);
7966 	ndi_hold_devi(pdip);
7967 
7968 	/*
7969 	 * Run devfs_clean() in case dip has no constraints and is
7970 	 * not in use, so is retireable but there are dv_nodes holding
7971 	 * ref-count on the dip. Note that devfs_clean() always returns
7972 	 * success.
7973 	 */
7974 	devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
7975 	(void) ddi_deviname(dip, devnm);
7976 	(void) devfs_clean(pdip, devnm + 1, DV_CLEAN_FORCE);
7977 	kmem_free(devnm, MAXNAMELEN + 1);
7978 
7979 	ndi_devi_enter(pdip, &circ);
7980 
7981 	/* release hold from e_ddi_hold_devi_by_path */
7982 	ndi_rele_devi(dip);
7983 
7984 	/*
7985 	 * If it cannot make a determination, is_leaf_node() assumes
7986 	 * dip is a nexus.
7987 	 */
7988 	(void) e_ddi_mark_retiring(dip, cons_array);
7989 	if (!is_leaf_node(dip)) {
7990 		ndi_devi_enter(dip, &circ2);
7991 		ddi_walk_devs(ddi_get_child(dip), e_ddi_mark_retiring,
7992 		    cons_array);
7993 		ndi_devi_exit(dip, circ2);
7994 	}
7995 	free_array(cons_array);
7996 
7997 	/*
7998 	 * apply constraints
7999 	 */
8000 	RIO_DEBUG((CE_NOTE, "retire: subtree retire notify: path = %s", path));
8001 
8002 	constraint = 1;	/* assume constraints allow retire */
8003 	(void) e_ddi_retire_notify(dip, &constraint);
8004 	if (!is_leaf_node(dip)) {
8005 		ndi_devi_enter(dip, &circ2);
8006 		ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_notify,
8007 		    &constraint);
8008 		ndi_devi_exit(dip, circ2);
8009 	}
8010 
8011 	/*
8012 	 * Now finalize the retire
8013 	 */
8014 	(void) e_ddi_retire_finalize(dip, &constraint);
8015 	if (!is_leaf_node(dip)) {
8016 		ndi_devi_enter(dip, &circ2);
8017 		ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_finalize,
8018 		    &constraint);
8019 		ndi_devi_exit(dip, circ2);
8020 	}
8021 
8022 	if (!constraint) {
8023 		RIO_DEBUG((CE_WARN, "retire failed: path = %s", path));
8024 	} else {
8025 		RIO_DEBUG((CE_NOTE, "retire succeeded: path = %s", path));
8026 	}
8027 
8028 	ndi_devi_exit(pdip, circ);
8029 	ndi_rele_devi(pdip);
8030 	return (constraint ? DDI_SUCCESS : DDI_FAILURE);
8031 }
8032 
8033 static int
8034 unmark_and_unfence(dev_info_t *dip, void *arg)
8035 {
8036 	char	*path = (char *)arg;
8037 
8038 	ASSERT(path);
8039 
8040 	(void) ddi_pathname(dip, path);
8041 
8042 	mutex_enter(&DEVI(dip)->devi_lock);
8043 	DEVI(dip)->devi_flags &= ~DEVI_RETIRED;
8044 	DEVI_SET_DEVICE_ONLINE(dip);
8045 	mutex_exit(&DEVI(dip)->devi_lock);
8046 
8047 	RIO_VERBOSE((CE_NOTE, "Cleared RETIRED flag: dip=%p, path=%s",
8048 	    (void *)dip, path));
8049 
8050 	(void) spec_unfence_snode(dip);
8051 	RIO_DEBUG((CE_NOTE, "Unfenced device: %s", path));
8052 
8053 	if (MDI_PHCI(dip))
8054 		mdi_phci_unretire(dip);
8055 
8056 	return (DDI_WALK_CONTINUE);
8057 }
8058 
8059 struct find_dip {
8060 	char	*fd_buf;
8061 	char	*fd_path;
8062 	dev_info_t *fd_dip;
8063 };
8064 
8065 static int
8066 find_dip_fcn(dev_info_t *dip, void *arg)
8067 {
8068 	struct find_dip *findp = (struct find_dip *)arg;
8069 
8070 	(void) ddi_pathname(dip, findp->fd_buf);
8071 
8072 	if (strcmp(findp->fd_path, findp->fd_buf) != 0)
8073 		return (DDI_WALK_CONTINUE);
8074 
8075 	ndi_hold_devi(dip);
8076 	findp->fd_dip = dip;
8077 
8078 	return (DDI_WALK_TERMINATE);
8079 }
8080 
8081 int
8082 e_ddi_unretire_device(char *path)
8083 {
8084 	int		circ;
8085 	int		circ2;
8086 	char		*path2;
8087 	dev_info_t	*pdip;
8088 	dev_info_t	*dip;
8089 	struct find_dip	 find_dip;
8090 
8091 	ASSERT(path);
8092 	ASSERT(*path == '/');
8093 
8094 	if (strcmp(path, "/") == 0) {
8095 		cmn_err(CE_WARN, "Root node cannot be retired. Skipping "
8096 		    "device unretire: %s", path);
8097 		return (0);
8098 	}
8099 
8100 	/*
8101 	 * We can't lookup the dip (corresponding to path) via
8102 	 * e_ddi_hold_devi_by_path() because the dip may be offline
8103 	 * and may not attach. Use ddi_walk_devs() instead;
8104 	 */
8105 	find_dip.fd_buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8106 	find_dip.fd_path = path;
8107 	find_dip.fd_dip = NULL;
8108 
8109 	pdip = ddi_root_node();
8110 
8111 	ndi_devi_enter(pdip, &circ);
8112 	ddi_walk_devs(ddi_get_child(pdip), find_dip_fcn, &find_dip);
8113 	ndi_devi_exit(pdip, circ);
8114 
8115 	kmem_free(find_dip.fd_buf, MAXPATHLEN);
8116 
8117 	if (find_dip.fd_dip == NULL) {
8118 		cmn_err(CE_WARN, "Device not found in device tree. Skipping "
8119 		    "device unretire: %s", path);
8120 		return (0);
8121 	}
8122 
8123 	dip = find_dip.fd_dip;
8124 
8125 	pdip = ddi_get_parent(dip);
8126 
8127 	ndi_hold_devi(pdip);
8128 
8129 	ndi_devi_enter(pdip, &circ);
8130 
8131 	path2 = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8132 
8133 	(void) unmark_and_unfence(dip, path2);
8134 	if (!is_leaf_node(dip)) {
8135 		ndi_devi_enter(dip, &circ2);
8136 		ddi_walk_devs(ddi_get_child(dip), unmark_and_unfence, path2);
8137 		ndi_devi_exit(dip, circ2);
8138 	}
8139 
8140 	kmem_free(path2, MAXPATHLEN);
8141 
8142 	/* release hold from find_dip_fcn() */
8143 	ndi_rele_devi(dip);
8144 
8145 	ndi_devi_exit(pdip, circ);
8146 
8147 	ndi_rele_devi(pdip);
8148 
8149 	return (0);
8150 }
8151 
8152 /*
8153  * Called before attach on a dip that has been retired.
8154  */
8155 static int
8156 mark_and_fence(dev_info_t *dip, void *arg)
8157 {
8158 	char    *fencepath = (char *)arg;
8159 
8160 	/*
8161 	 * We have already decided to retire this device. The various
8162 	 * constraint checking should not be set.
8163 	 * NOTE that the retire flag may already be set due to
8164 	 * fenced -> detach -> fenced transitions.
8165 	 */
8166 	mutex_enter(&DEVI(dip)->devi_lock);
8167 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
8168 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
8169 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRING));
8170 	DEVI(dip)->devi_flags |= DEVI_RETIRED;
8171 	mutex_exit(&DEVI(dip)->devi_lock);
8172 	RIO_VERBOSE((CE_NOTE, "marked as RETIRED dip=%p", (void *)dip));
8173 
8174 	if (fencepath) {
8175 		(void) spec_fence_snode(dip, NULL);
8176 		RIO_DEBUG((CE_NOTE, "Fenced: %s",
8177 		    ddi_pathname(dip, fencepath)));
8178 	}
8179 
8180 	return (DDI_WALK_CONTINUE);
8181 }
8182 
8183 /*
8184  * Checks the retire database and:
8185  *
8186  * - if device is present in the retire database, marks the device retired
8187  *   and fences it off.
8188  * - if device is not in retire database, allows the device to attach normally
8189  *
8190  * To be called only by framework attach code on first attach attempt.
8191  *
8192  */
8193 static void
8194 i_ddi_check_retire(dev_info_t *dip)
8195 {
8196 	char		*path;
8197 	dev_info_t	*pdip;
8198 	int		circ;
8199 	int		phci_only;
8200 
8201 	pdip = ddi_get_parent(dip);
8202 
8203 	/*
8204 	 * Root dip is treated special and doesn't take this code path.
8205 	 * Also root can never be retired.
8206 	 */
8207 	ASSERT(pdip);
8208 	ASSERT(DEVI_BUSY_OWNED(pdip));
8209 	ASSERT(i_ddi_node_state(dip) < DS_ATTACHED);
8210 
8211 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8212 
8213 	(void) ddi_pathname(dip, path);
8214 
8215 	RIO_VERBOSE((CE_NOTE, "Checking if dip should attach: dip=%p, path=%s",
8216 	    (void *)dip, path));
8217 
8218 	/*
8219 	 * Check if this device is in the "retired" store i.e.  should
8220 	 * be retired. If not, we have nothing to do.
8221 	 */
8222 	if (e_ddi_device_retired(path) == 0) {
8223 		RIO_VERBOSE((CE_NOTE, "device is NOT retired: path=%s", path));
8224 		kmem_free(path, MAXPATHLEN);
8225 		return;
8226 	}
8227 
8228 	RIO_DEBUG((CE_NOTE, "attach: device is retired: path=%s", path));
8229 
8230 	/*
8231 	 * Mark dips and fence off snodes (if any)
8232 	 */
8233 	RIO_DEBUG((CE_NOTE, "attach: Mark and fence subtree: path=%s", path));
8234 	(void) mark_and_fence(dip, path);
8235 	if (!is_leaf_node(dip)) {
8236 		ndi_devi_enter(dip, &circ);
8237 		ddi_walk_devs(ddi_get_child(dip), mark_and_fence, path);
8238 		ndi_devi_exit(dip, circ);
8239 	}
8240 
8241 	kmem_free(path, MAXPATHLEN);
8242 
8243 	/*
8244 	 * We don't want to check the client. We just want to
8245 	 * offline the PHCI
8246 	 */
8247 	phci_only = 1;
8248 	if (MDI_PHCI(dip))
8249 		mdi_phci_retire_finalize(dip, phci_only);
8250 }
8251