xref: /titanic_50/usr/src/uts/common/os/devcfg.c (revision 08d2fdc210e92c7a249e69a795663019da6f23ca)
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 /* callback data for unbind_children_by_alias() */
4277 typedef struct unbind_data {
4278 	major_t	drv_major;
4279 	char	*drv_alias;
4280 	int	ndevs_bound;
4281 	int	unbind_errors;
4282 } unbind_data_t;
4283 
4284 /*
4285  * A utility function provided for testing and support convenience
4286  * Called for each device during an upgrade_drv -d bound to the alias
4287  * that cannot be unbound due to device in use.
4288  */
4289 static void
4290 unbind_alias_dev_in_use(dev_info_t *dip, char *alias)
4291 {
4292 	if (moddebug & MODDEBUG_BINDING) {
4293 		cmn_err(CE_CONT, "%s%d: state %d: bound to %s\n",
4294 		    ddi_driver_name(dip), ddi_get_instance(dip),
4295 		    i_ddi_node_state(dip), alias);
4296 	}
4297 }
4298 
4299 /*
4300  * walkdevs callback for unbind devices bound to specific driver
4301  * and alias.  Invoked within the context of update_drv -d <alias>.
4302  */
4303 static int
4304 unbind_children_by_alias(dev_info_t *dip, void *arg)
4305 {
4306 	int		circ;
4307 	dev_info_t	*cdip;
4308 	dev_info_t	*next;
4309 	unbind_data_t	*ub = (unbind_data_t *)(uintptr_t)arg;
4310 	int		rv;
4311 
4312 	/*
4313 	 * We are called from update_drv to try to unbind a specific
4314 	 * set of aliases for a driver.  Unbind what persistent nodes
4315 	 * we can, and return the number of nodes which cannot be unbound.
4316 	 * If not all nodes can be unbound, update_drv leaves the
4317 	 * state of the driver binding files unchanged, except in
4318 	 * the case of -f.
4319 	 */
4320 	ndi_devi_enter(dip, &circ);
4321 	for (cdip = ddi_get_child(dip); cdip; cdip = next) {
4322 		next = ddi_get_next_sibling(cdip);
4323 		if ((ddi_driver_major(cdip) != ub->drv_major) ||
4324 		    (strcmp(DEVI(cdip)->devi_node_name, ub->drv_alias) != 0))
4325 			continue;
4326 		if (i_ddi_node_state(cdip) >= DS_BOUND) {
4327 			rv = ndi_devi_unbind_driver(cdip);
4328 			if (rv != DDI_SUCCESS ||
4329 			    (i_ddi_node_state(cdip) >= DS_BOUND)) {
4330 				unbind_alias_dev_in_use(cdip, ub->drv_alias);
4331 				ub->ndevs_bound++;
4332 				continue;
4333 			}
4334 			if (ndi_dev_is_persistent_node(cdip) == 0)
4335 				(void) ddi_remove_child(cdip, 0);
4336 		}
4337 	}
4338 	ndi_devi_exit(dip, circ);
4339 
4340 	return (DDI_WALK_CONTINUE);
4341 }
4342 
4343 /*
4344  * Unbind devices by driver & alias
4345  * Context: update_drv [-f] -d -i <alias> <driver>
4346  */
4347 int
4348 i_ddi_unbind_devs_by_alias(major_t major, char *alias)
4349 {
4350 	unbind_data_t	*ub;
4351 	int		rv;
4352 
4353 	ub = kmem_zalloc(sizeof (*ub), KM_SLEEP);
4354 	ub->drv_major = major;
4355 	ub->drv_alias = alias;
4356 	ub->ndevs_bound = 0;
4357 	ub->unbind_errors = 0;
4358 
4359 	/* flush devfs so that ndi_devi_unbind_driver will work when possible */
4360 	devfs_clean(top_devinfo, NULL, 0);
4361 	ddi_walk_devs(top_devinfo, unbind_children_by_alias,
4362 	    (void *)(uintptr_t)ub);
4363 
4364 	/* return the number of devices remaining bound to the alias */
4365 	rv = ub->ndevs_bound + ub->unbind_errors;
4366 	kmem_free(ub, sizeof (*ub));
4367 	return (rv);
4368 }
4369 
4370 /*
4371  * walkdevs callback for unbind devices by driver
4372  */
4373 static int
4374 unbind_children_by_driver(dev_info_t *dip, void *arg)
4375 {
4376 	int		circ;
4377 	dev_info_t	*cdip;
4378 	dev_info_t	*next;
4379 	major_t		major = (major_t)(uintptr_t)arg;
4380 	int		rv;
4381 
4382 	/*
4383 	 * We are called either from rem_drv or update_drv when reloading
4384 	 * a driver.conf file. In either case, we unbind persistent nodes
4385 	 * and destroy .conf nodes. In the case of rem_drv, this will be
4386 	 * the final state. In the case of update_drv,  i_ddi_bind_devs()
4387 	 * may be invoked later to re-enumerate (new) driver.conf rebind
4388 	 * persistent nodes.
4389 	 */
4390 	ndi_devi_enter(dip, &circ);
4391 	for (cdip = ddi_get_child(dip); cdip; cdip = next) {
4392 		next = ddi_get_next_sibling(cdip);
4393 		if (ddi_driver_major(cdip) != major)
4394 			continue;
4395 		if (i_ddi_node_state(cdip) >= DS_BOUND) {
4396 			rv = ndi_devi_unbind_driver(cdip);
4397 			if (rv == DDI_FAILURE ||
4398 			    (i_ddi_node_state(cdip) >= DS_BOUND))
4399 				continue;
4400 			if (ndi_dev_is_persistent_node(cdip) == 0)
4401 				(void) ddi_remove_child(cdip, 0);
4402 		}
4403 	}
4404 	ndi_devi_exit(dip, circ);
4405 
4406 	return (DDI_WALK_CONTINUE);
4407 }
4408 
4409 /*
4410  * Unbind devices by driver
4411  * Context: rem_drv or unload driver.conf
4412  */
4413 void
4414 i_ddi_unbind_devs(major_t major)
4415 {
4416 	/* flush devfs so that ndi_devi_unbind_driver will work when possible */
4417 	devfs_clean(top_devinfo, NULL, 0);
4418 	ddi_walk_devs(top_devinfo, unbind_children_by_driver,
4419 	    (void *)(uintptr_t)major);
4420 }
4421 
4422 /*
4423  * I/O Hotplug control
4424  */
4425 
4426 /*
4427  * create and attach a dev_info node from a .conf file spec
4428  */
4429 static void
4430 init_spec_child(dev_info_t *pdip, struct hwc_spec *specp, uint_t flags)
4431 {
4432 	_NOTE(ARGUNUSED(flags))
4433 	dev_info_t *dip;
4434 	char *node_name;
4435 
4436 	if (((node_name = specp->hwc_devi_name) == NULL) ||
4437 	    (ddi_name_to_major(node_name) == DDI_MAJOR_T_NONE)) {
4438 		char *tmp = node_name;
4439 		if (tmp == NULL)
4440 			tmp = "<none>";
4441 		cmn_err(CE_CONT,
4442 		    "init_spec_child: parent=%s, bad spec (%s)\n",
4443 		    ddi_node_name(pdip), tmp);
4444 		return;
4445 	}
4446 
4447 	dip = i_ddi_alloc_node(pdip, node_name, (pnode_t)DEVI_PSEUDO_NODEID,
4448 	    -1, specp->hwc_devi_sys_prop_ptr, KM_SLEEP);
4449 
4450 	if (dip == NULL)
4451 		return;
4452 
4453 	if (ddi_initchild(pdip, dip) != DDI_SUCCESS)
4454 		(void) ddi_remove_child(dip, 0);
4455 }
4456 
4457 /*
4458  * Lookup hwc specs from hash tables and make children from the spec
4459  * Because some .conf children are "merge" nodes, we also initialize
4460  * .conf children to merge properties onto hardware nodes.
4461  *
4462  * The pdip must be held busy.
4463  */
4464 int
4465 i_ndi_make_spec_children(dev_info_t *pdip, uint_t flags)
4466 {
4467 	extern struct hwc_spec *hwc_get_child_spec(dev_info_t *, major_t);
4468 	int			circ;
4469 	struct hwc_spec		*list, *spec;
4470 
4471 	ndi_devi_enter(pdip, &circ);
4472 	if (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN) {
4473 		ndi_devi_exit(pdip, circ);
4474 		return (DDI_SUCCESS);
4475 	}
4476 
4477 	list = hwc_get_child_spec(pdip, DDI_MAJOR_T_NONE);
4478 	for (spec = list; spec != NULL; spec = spec->hwc_next) {
4479 		init_spec_child(pdip, spec, flags);
4480 	}
4481 	hwc_free_spec_list(list);
4482 
4483 	mutex_enter(&DEVI(pdip)->devi_lock);
4484 	DEVI(pdip)->devi_flags |= DEVI_MADE_CHILDREN;
4485 	mutex_exit(&DEVI(pdip)->devi_lock);
4486 	ndi_devi_exit(pdip, circ);
4487 	return (DDI_SUCCESS);
4488 }
4489 
4490 /*
4491  * Run initchild on all child nodes such that instance assignment
4492  * for multiport network cards are contiguous.
4493  *
4494  * The pdip must be held busy.
4495  */
4496 static void
4497 i_ndi_init_hw_children(dev_info_t *pdip, uint_t flags)
4498 {
4499 	dev_info_t *dip;
4500 
4501 	ASSERT(DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
4502 
4503 	/* contiguous instance assignment */
4504 	e_ddi_enter_instance();
4505 	dip = ddi_get_child(pdip);
4506 	while (dip) {
4507 		if (ndi_dev_is_persistent_node(dip))
4508 			(void) i_ndi_config_node(dip, DS_INITIALIZED, flags);
4509 		dip = ddi_get_next_sibling(dip);
4510 	}
4511 	e_ddi_exit_instance();
4512 }
4513 
4514 /*
4515  * report device status
4516  */
4517 static void
4518 i_ndi_devi_report_status_change(dev_info_t *dip, char *path)
4519 {
4520 	char *status;
4521 
4522 	if (!DEVI_NEED_REPORT(dip) ||
4523 	    (i_ddi_node_state(dip) < DS_INITIALIZED)) {
4524 		return;
4525 	}
4526 
4527 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
4528 		status = "offline";
4529 	} else if (DEVI_IS_DEVICE_DOWN(dip)) {
4530 		status = "down";
4531 	} else if (DEVI_IS_BUS_QUIESCED(dip)) {
4532 		status = "quiesced";
4533 	} else if (DEVI_IS_BUS_DOWN(dip)) {
4534 		status = "down";
4535 	} else if (i_ddi_devi_attached(dip)) {
4536 		status = "online";
4537 	} else {
4538 		status = "unknown";
4539 	}
4540 
4541 	if (path == NULL) {
4542 		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4543 		cmn_err(CE_CONT, "?%s (%s%d) %s\n",
4544 		    ddi_pathname(dip, path), ddi_driver_name(dip),
4545 		    ddi_get_instance(dip), status);
4546 		kmem_free(path, MAXPATHLEN);
4547 	} else {
4548 		cmn_err(CE_CONT, "?%s (%s%d) %s\n",
4549 		    path, ddi_driver_name(dip),
4550 		    ddi_get_instance(dip), status);
4551 	}
4552 
4553 	mutex_enter(&(DEVI(dip)->devi_lock));
4554 	DEVI_REPORT_DONE(dip);
4555 	mutex_exit(&(DEVI(dip)->devi_lock));
4556 }
4557 
4558 /*
4559  * log a notification that a dev_info node has been configured.
4560  */
4561 static int
4562 i_log_devfs_add_devinfo(dev_info_t *dip, uint_t flags)
4563 {
4564 	int se_err;
4565 	char *pathname;
4566 	sysevent_t *ev;
4567 	sysevent_id_t eid;
4568 	sysevent_value_t se_val;
4569 	sysevent_attr_list_t *ev_attr_list = NULL;
4570 	char *class_name;
4571 	int no_transport = 0;
4572 
4573 	ASSERT(dip);
4574 
4575 	/*
4576 	 * Invalidate the devinfo snapshot cache
4577 	 */
4578 	i_ddi_di_cache_invalidate(KM_SLEEP);
4579 
4580 	/* do not generate ESC_DEVFS_DEVI_ADD event during boot */
4581 	if (!i_ddi_io_initialized())
4582 		return (DDI_SUCCESS);
4583 
4584 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_ADD, EP_DDI, SE_SLEEP);
4585 
4586 	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4587 
4588 	(void) ddi_pathname(dip, pathname);
4589 	ASSERT(strlen(pathname));
4590 
4591 	se_val.value_type = SE_DATA_TYPE_STRING;
4592 	se_val.value.sv_string = pathname;
4593 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
4594 	    &se_val, SE_SLEEP) != 0) {
4595 		goto fail;
4596 	}
4597 
4598 	/* add the device class attribute */
4599 	if ((class_name = i_ddi_devi_class(dip)) != NULL) {
4600 		se_val.value_type = SE_DATA_TYPE_STRING;
4601 		se_val.value.sv_string = class_name;
4602 
4603 		if (sysevent_add_attr(&ev_attr_list,
4604 		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
4605 			sysevent_free_attr(ev_attr_list);
4606 			goto fail;
4607 		}
4608 	}
4609 
4610 	/*
4611 	 * must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
4612 	 * in which case the branch event will be logged by the caller
4613 	 * after the entire branch has been configured.
4614 	 */
4615 	if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
4616 		/*
4617 		 * Instead of logging a separate branch event just add
4618 		 * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
4619 		 * generate a EC_DEV_BRANCH event.
4620 		 */
4621 		se_val.value_type = SE_DATA_TYPE_INT32;
4622 		se_val.value.sv_int32 = 1;
4623 		if (sysevent_add_attr(&ev_attr_list,
4624 		    DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
4625 			sysevent_free_attr(ev_attr_list);
4626 			goto fail;
4627 		}
4628 	}
4629 
4630 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
4631 		sysevent_free_attr(ev_attr_list);
4632 		goto fail;
4633 	}
4634 
4635 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
4636 		if (se_err == SE_NO_TRANSPORT)
4637 			no_transport = 1;
4638 		goto fail;
4639 	}
4640 
4641 	sysevent_free(ev);
4642 	kmem_free(pathname, MAXPATHLEN);
4643 
4644 	return (DDI_SUCCESS);
4645 
4646 fail:
4647 	cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_ADD event for %s%s",
4648 	    pathname, (no_transport) ? " (syseventd not responding)" : "");
4649 
4650 	cmn_err(CE_WARN, "/dev may not be current for driver %s. "
4651 	    "Run devfsadm -i %s",
4652 	    ddi_driver_name(dip), ddi_driver_name(dip));
4653 
4654 	sysevent_free(ev);
4655 	kmem_free(pathname, MAXPATHLEN);
4656 	return (DDI_SUCCESS);
4657 }
4658 
4659 /*
4660  * log a notification that a dev_info node has been unconfigured.
4661  */
4662 static int
4663 i_log_devfs_remove_devinfo(char *pathname, char *class_name, char *driver_name,
4664     int instance, uint_t flags)
4665 {
4666 	sysevent_t *ev;
4667 	sysevent_id_t eid;
4668 	sysevent_value_t se_val;
4669 	sysevent_attr_list_t *ev_attr_list = NULL;
4670 	int se_err;
4671 	int no_transport = 0;
4672 
4673 	i_ddi_di_cache_invalidate(KM_SLEEP);
4674 
4675 	if (!i_ddi_io_initialized())
4676 		return (DDI_SUCCESS);
4677 
4678 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_REMOVE, EP_DDI, SE_SLEEP);
4679 
4680 	se_val.value_type = SE_DATA_TYPE_STRING;
4681 	se_val.value.sv_string = pathname;
4682 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
4683 	    &se_val, SE_SLEEP) != 0) {
4684 		goto fail;
4685 	}
4686 
4687 	if (class_name) {
4688 		/* add the device class, driver name and instance attributes */
4689 
4690 		se_val.value_type = SE_DATA_TYPE_STRING;
4691 		se_val.value.sv_string = class_name;
4692 		if (sysevent_add_attr(&ev_attr_list,
4693 		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
4694 			sysevent_free_attr(ev_attr_list);
4695 			goto fail;
4696 		}
4697 
4698 		se_val.value_type = SE_DATA_TYPE_STRING;
4699 		se_val.value.sv_string = driver_name;
4700 		if (sysevent_add_attr(&ev_attr_list,
4701 		    DEVFS_DRIVER_NAME, &se_val, SE_SLEEP) != 0) {
4702 			sysevent_free_attr(ev_attr_list);
4703 			goto fail;
4704 		}
4705 
4706 		se_val.value_type = SE_DATA_TYPE_INT32;
4707 		se_val.value.sv_int32 = instance;
4708 		if (sysevent_add_attr(&ev_attr_list,
4709 		    DEVFS_INSTANCE, &se_val, SE_SLEEP) != 0) {
4710 			sysevent_free_attr(ev_attr_list);
4711 			goto fail;
4712 		}
4713 	}
4714 
4715 	/*
4716 	 * must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
4717 	 * in which case the branch event will be logged by the caller
4718 	 * after the entire branch has been unconfigured.
4719 	 */
4720 	if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
4721 		/*
4722 		 * Instead of logging a separate branch event just add
4723 		 * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
4724 		 * generate a EC_DEV_BRANCH event.
4725 		 */
4726 		se_val.value_type = SE_DATA_TYPE_INT32;
4727 		se_val.value.sv_int32 = 1;
4728 		if (sysevent_add_attr(&ev_attr_list,
4729 		    DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
4730 			sysevent_free_attr(ev_attr_list);
4731 			goto fail;
4732 		}
4733 	}
4734 
4735 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
4736 		sysevent_free_attr(ev_attr_list);
4737 		goto fail;
4738 	}
4739 
4740 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
4741 		if (se_err == SE_NO_TRANSPORT)
4742 			no_transport = 1;
4743 		goto fail;
4744 	}
4745 
4746 	sysevent_free(ev);
4747 	return (DDI_SUCCESS);
4748 
4749 fail:
4750 	sysevent_free(ev);
4751 	cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_REMOVE event for %s%s",
4752 	    pathname, (no_transport) ? " (syseventd not responding)" : "");
4753 	return (DDI_SUCCESS);
4754 }
4755 
4756 /*
4757  * log an event that a dev_info branch has been configured or unconfigured.
4758  */
4759 static int
4760 i_log_devfs_branch(char *node_path, char *subclass)
4761 {
4762 	int se_err;
4763 	sysevent_t *ev;
4764 	sysevent_id_t eid;
4765 	sysevent_value_t se_val;
4766 	sysevent_attr_list_t *ev_attr_list = NULL;
4767 	int no_transport = 0;
4768 
4769 	/* do not generate the event during boot */
4770 	if (!i_ddi_io_initialized())
4771 		return (DDI_SUCCESS);
4772 
4773 	ev = sysevent_alloc(EC_DEVFS, subclass, EP_DDI, SE_SLEEP);
4774 
4775 	se_val.value_type = SE_DATA_TYPE_STRING;
4776 	se_val.value.sv_string = node_path;
4777 
4778 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
4779 	    &se_val, SE_SLEEP) != 0) {
4780 		goto fail;
4781 	}
4782 
4783 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
4784 		sysevent_free_attr(ev_attr_list);
4785 		goto fail;
4786 	}
4787 
4788 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
4789 		if (se_err == SE_NO_TRANSPORT)
4790 			no_transport = 1;
4791 		goto fail;
4792 	}
4793 
4794 	sysevent_free(ev);
4795 	return (DDI_SUCCESS);
4796 
4797 fail:
4798 	cmn_err(CE_WARN, "failed to log %s branch event for %s%s",
4799 	    subclass, node_path,
4800 	    (no_transport) ? " (syseventd not responding)" : "");
4801 
4802 	sysevent_free(ev);
4803 	return (DDI_FAILURE);
4804 }
4805 
4806 /*
4807  * log an event that a dev_info tree branch has been configured.
4808  */
4809 static int
4810 i_log_devfs_branch_add(dev_info_t *dip)
4811 {
4812 	char *node_path;
4813 	int rv;
4814 
4815 	node_path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4816 	(void) ddi_pathname(dip, node_path);
4817 	rv = i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_ADD);
4818 	kmem_free(node_path, MAXPATHLEN);
4819 
4820 	return (rv);
4821 }
4822 
4823 /*
4824  * log an event that a dev_info tree branch has been unconfigured.
4825  */
4826 static int
4827 i_log_devfs_branch_remove(char *node_path)
4828 {
4829 	return (i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_REMOVE));
4830 }
4831 
4832 /*
4833  * enqueue the dip's deviname on the branch event queue.
4834  */
4835 static struct brevq_node *
4836 brevq_enqueue(struct brevq_node **brevqp, dev_info_t *dip,
4837     struct brevq_node *child)
4838 {
4839 	struct brevq_node *brn;
4840 	char *deviname;
4841 
4842 	deviname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
4843 	(void) ddi_deviname(dip, deviname);
4844 
4845 	brn = kmem_zalloc(sizeof (*brn), KM_SLEEP);
4846 	brn->brn_deviname = i_ddi_strdup(deviname, KM_SLEEP);
4847 	kmem_free(deviname, MAXNAMELEN);
4848 	brn->brn_child = child;
4849 	brn->brn_sibling = *brevqp;
4850 	*brevqp = brn;
4851 
4852 	return (brn);
4853 }
4854 
4855 /*
4856  * free the memory allocated for the elements on the branch event queue.
4857  */
4858 static void
4859 free_brevq(struct brevq_node *brevq)
4860 {
4861 	struct brevq_node *brn, *next_brn;
4862 
4863 	for (brn = brevq; brn != NULL; brn = next_brn) {
4864 		next_brn = brn->brn_sibling;
4865 		ASSERT(brn->brn_child == NULL);
4866 		kmem_free(brn->brn_deviname, strlen(brn->brn_deviname) + 1);
4867 		kmem_free(brn, sizeof (*brn));
4868 	}
4869 }
4870 
4871 /*
4872  * log the events queued up on the branch event queue and free the
4873  * associated memory.
4874  *
4875  * node_path must have been allocated with at least MAXPATHLEN bytes.
4876  */
4877 static void
4878 log_and_free_brevq(char *node_path, struct brevq_node *brevq)
4879 {
4880 	struct brevq_node *brn;
4881 	char *p;
4882 
4883 	p = node_path + strlen(node_path);
4884 	for (brn = brevq; brn != NULL; brn = brn->brn_sibling) {
4885 		(void) strcpy(p, brn->brn_deviname);
4886 		(void) i_log_devfs_branch_remove(node_path);
4887 	}
4888 	*p = '\0';
4889 
4890 	free_brevq(brevq);
4891 }
4892 
4893 /*
4894  * log the events queued up on the branch event queue and free the
4895  * associated memory. Same as the previous function but operates on dip.
4896  */
4897 static void
4898 log_and_free_brevq_dip(dev_info_t *dip, struct brevq_node *brevq)
4899 {
4900 	char *path;
4901 
4902 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4903 	(void) ddi_pathname(dip, path);
4904 	log_and_free_brevq(path, brevq);
4905 	kmem_free(path, MAXPATHLEN);
4906 }
4907 
4908 /*
4909  * log the outstanding branch remove events for the grand children of the dip
4910  * and free the associated memory.
4911  */
4912 static void
4913 log_and_free_br_events_on_grand_children(dev_info_t *dip,
4914     struct brevq_node *brevq)
4915 {
4916 	struct brevq_node *brn;
4917 	char *path;
4918 	char *p;
4919 
4920 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4921 	(void) ddi_pathname(dip, path);
4922 	p = path + strlen(path);
4923 	for (brn = brevq; brn != NULL; brn = brn->brn_sibling) {
4924 		if (brn->brn_child) {
4925 			(void) strcpy(p, brn->brn_deviname);
4926 			/* now path contains the node path to the dip's child */
4927 			log_and_free_brevq(path, brn->brn_child);
4928 			brn->brn_child = NULL;
4929 		}
4930 	}
4931 	kmem_free(path, MAXPATHLEN);
4932 }
4933 
4934 /*
4935  * log and cleanup branch remove events for the grand children of the dip.
4936  */
4937 static void
4938 cleanup_br_events_on_grand_children(dev_info_t *dip, struct brevq_node **brevqp)
4939 {
4940 	dev_info_t *child;
4941 	struct brevq_node *brevq, *brn, *prev_brn, *next_brn;
4942 	char *path;
4943 	int circ;
4944 
4945 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4946 	prev_brn = NULL;
4947 	brevq = *brevqp;
4948 
4949 	ndi_devi_enter(dip, &circ);
4950 	for (brn = brevq; brn != NULL; brn = next_brn) {
4951 		next_brn = brn->brn_sibling;
4952 		for (child = ddi_get_child(dip); child != NULL;
4953 		    child = ddi_get_next_sibling(child)) {
4954 			if (i_ddi_node_state(child) >= DS_INITIALIZED) {
4955 				(void) ddi_deviname(child, path);
4956 				if (strcmp(path, brn->brn_deviname) == 0)
4957 					break;
4958 			}
4959 		}
4960 
4961 		if (child != NULL && !(DEVI_EVREMOVE(child))) {
4962 			/*
4963 			 * Event state is not REMOVE. So branch remove event
4964 			 * is not going be generated on brn->brn_child.
4965 			 * If any branch remove events were queued up on
4966 			 * brn->brn_child log them and remove the brn
4967 			 * from the queue.
4968 			 */
4969 			if (brn->brn_child) {
4970 				(void) ddi_pathname(dip, path);
4971 				(void) strcat(path, brn->brn_deviname);
4972 				log_and_free_brevq(path, brn->brn_child);
4973 			}
4974 
4975 			if (prev_brn)
4976 				prev_brn->brn_sibling = next_brn;
4977 			else
4978 				*brevqp = next_brn;
4979 
4980 			kmem_free(brn->brn_deviname,
4981 			    strlen(brn->brn_deviname) + 1);
4982 			kmem_free(brn, sizeof (*brn));
4983 		} else {
4984 			/*
4985 			 * Free up the outstanding branch remove events
4986 			 * queued on brn->brn_child since brn->brn_child
4987 			 * itself is eligible for branch remove event.
4988 			 */
4989 			if (brn->brn_child) {
4990 				free_brevq(brn->brn_child);
4991 				brn->brn_child = NULL;
4992 			}
4993 			prev_brn = brn;
4994 		}
4995 	}
4996 
4997 	ndi_devi_exit(dip, circ);
4998 	kmem_free(path, MAXPATHLEN);
4999 }
5000 
5001 static int
5002 need_remove_event(dev_info_t *dip, int flags)
5003 {
5004 	if ((flags & (NDI_NO_EVENT | NDI_AUTODETACH)) == 0 &&
5005 	    (flags & (NDI_DEVI_OFFLINE | NDI_UNCONFIG | NDI_DEVI_REMOVE)) &&
5006 	    !(DEVI_EVREMOVE(dip)))
5007 		return (1);
5008 	else
5009 		return (0);
5010 }
5011 
5012 /*
5013  * Unconfigure children/descendants of the dip.
5014  *
5015  * If the operation involves a branch event NDI_BRANCH_EVENT_OP is set
5016  * through out the unconfiguration. On successful return *brevqp is set to
5017  * a queue of dip's child devinames for which branch remove events need
5018  * to be generated.
5019  */
5020 static int
5021 devi_unconfig_branch(dev_info_t *dip, dev_info_t **dipp, int flags,
5022     struct brevq_node **brevqp)
5023 {
5024 	int rval;
5025 
5026 	*brevqp = NULL;
5027 
5028 	if ((!(flags & NDI_BRANCH_EVENT_OP)) && need_remove_event(dip, flags))
5029 		flags |= NDI_BRANCH_EVENT_OP;
5030 
5031 	if (flags & NDI_BRANCH_EVENT_OP) {
5032 		rval = devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE,
5033 		    brevqp);
5034 
5035 		if (rval != NDI_SUCCESS && (*brevqp)) {
5036 			log_and_free_brevq_dip(dip, *brevqp);
5037 			*brevqp = NULL;
5038 		}
5039 	} else
5040 		rval = devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE,
5041 		    NULL);
5042 
5043 	return (rval);
5044 }
5045 
5046 /*
5047  * If the dip is already bound to a driver transition to DS_INITIALIZED
5048  * in order to generate an event in the case where the node was left in
5049  * DS_BOUND state since boot (never got attached) and the node is now
5050  * being offlined.
5051  */
5052 static void
5053 init_bound_node_ev(dev_info_t *pdip, dev_info_t *dip, int flags)
5054 {
5055 	if (need_remove_event(dip, flags) &&
5056 	    i_ddi_node_state(dip) == DS_BOUND &&
5057 	    i_ddi_devi_attached(pdip) && !DEVI_IS_DEVICE_OFFLINE(dip))
5058 		(void) ddi_initchild(pdip, dip);
5059 }
5060 
5061 /*
5062  * attach a node/branch with parent already held busy
5063  */
5064 static int
5065 devi_attach_node(dev_info_t *dip, uint_t flags)
5066 {
5067 	dev_info_t *pdip = ddi_get_parent(dip);
5068 
5069 	ASSERT(pdip && DEVI_BUSY_OWNED(pdip));
5070 
5071 	mutex_enter(&(DEVI(dip)->devi_lock));
5072 	if (flags & NDI_DEVI_ONLINE) {
5073 		if (!i_ddi_devi_attached(dip))
5074 			DEVI_SET_REPORT(dip);
5075 		DEVI_SET_DEVICE_ONLINE(dip);
5076 	}
5077 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
5078 		mutex_exit(&(DEVI(dip)->devi_lock));
5079 		return (NDI_FAILURE);
5080 	}
5081 	mutex_exit(&(DEVI(dip)->devi_lock));
5082 
5083 	if (i_ddi_attachchild(dip) != DDI_SUCCESS) {
5084 		mutex_enter(&(DEVI(dip)->devi_lock));
5085 		DEVI_SET_EVUNINIT(dip);
5086 		mutex_exit(&(DEVI(dip)->devi_lock));
5087 
5088 		if (ndi_dev_is_persistent_node(dip))
5089 			(void) ddi_uninitchild(dip);
5090 		else {
5091 			/*
5092 			 * Delete .conf nodes and nodes that are not
5093 			 * well formed.
5094 			 */
5095 			(void) ddi_remove_child(dip, 0);
5096 		}
5097 		return (NDI_FAILURE);
5098 	}
5099 
5100 	i_ndi_devi_report_status_change(dip, NULL);
5101 
5102 	/*
5103 	 * log an event, but not during devfs lookups in which case
5104 	 * NDI_NO_EVENT is set.
5105 	 */
5106 	if ((flags & NDI_NO_EVENT) == 0 && !(DEVI_EVADD(dip))) {
5107 		(void) i_log_devfs_add_devinfo(dip, flags);
5108 
5109 		mutex_enter(&(DEVI(dip)->devi_lock));
5110 		DEVI_SET_EVADD(dip);
5111 		mutex_exit(&(DEVI(dip)->devi_lock));
5112 	} else if (!(flags & NDI_NO_EVENT_STATE_CHNG)) {
5113 		mutex_enter(&(DEVI(dip)->devi_lock));
5114 		DEVI_SET_EVADD(dip);
5115 		mutex_exit(&(DEVI(dip)->devi_lock));
5116 	}
5117 
5118 	return (NDI_SUCCESS);
5119 }
5120 
5121 /* internal function to config immediate children */
5122 static int
5123 config_immediate_children(dev_info_t *pdip, uint_t flags, major_t major)
5124 {
5125 	dev_info_t	*child, *next;
5126 	int		circ;
5127 
5128 	ASSERT(i_ddi_devi_attached(pdip));
5129 
5130 	if (!NEXUS_DRV(ddi_get_driver(pdip)))
5131 		return (NDI_SUCCESS);
5132 
5133 	NDI_CONFIG_DEBUG((CE_CONT,
5134 	    "config_immediate_children: %s%d (%p), flags=%x\n",
5135 	    ddi_driver_name(pdip), ddi_get_instance(pdip),
5136 	    (void *)pdip, flags));
5137 
5138 	ndi_devi_enter(pdip, &circ);
5139 
5140 	if (flags & NDI_CONFIG_REPROBE) {
5141 		mutex_enter(&DEVI(pdip)->devi_lock);
5142 		DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
5143 		mutex_exit(&DEVI(pdip)->devi_lock);
5144 	}
5145 	(void) i_ndi_make_spec_children(pdip, flags);
5146 	i_ndi_init_hw_children(pdip, flags);
5147 
5148 	child = ddi_get_child(pdip);
5149 	while (child) {
5150 		/* NOTE: devi_attach_node() may remove the dip */
5151 		next = ddi_get_next_sibling(child);
5152 
5153 		/*
5154 		 * Configure all nexus nodes or leaf nodes with
5155 		 * matching driver major
5156 		 */
5157 		if ((major == DDI_MAJOR_T_NONE) ||
5158 		    (major == ddi_driver_major(child)) ||
5159 		    ((flags & NDI_CONFIG) && (is_leaf_node(child) == 0)))
5160 			(void) devi_attach_node(child, flags);
5161 		child = next;
5162 	}
5163 
5164 	ndi_devi_exit(pdip, circ);
5165 
5166 	return (NDI_SUCCESS);
5167 }
5168 
5169 /* internal function to config grand children */
5170 static int
5171 config_grand_children(dev_info_t *pdip, uint_t flags, major_t major)
5172 {
5173 	struct mt_config_handle *hdl;
5174 
5175 	/* multi-threaded configuration of child nexus */
5176 	hdl = mt_config_init(pdip, NULL, flags, major, MT_CONFIG_OP, NULL);
5177 	mt_config_children(hdl);
5178 
5179 	return (mt_config_fini(hdl));	/* wait for threads to exit */
5180 }
5181 
5182 /*
5183  * Common function for device tree configuration,
5184  * either BUS_CONFIG_ALL or BUS_CONFIG_DRIVER.
5185  * The NDI_CONFIG flag causes recursive configuration of
5186  * grandchildren, devfs usage should not recurse.
5187  */
5188 static int
5189 devi_config_common(dev_info_t *dip, int flags, major_t major)
5190 {
5191 	int error;
5192 	int (*f)();
5193 
5194 	if (!i_ddi_devi_attached(dip))
5195 		return (NDI_FAILURE);
5196 
5197 	if (pm_pre_config(dip, NULL) != DDI_SUCCESS)
5198 		return (NDI_FAILURE);
5199 
5200 	if ((DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
5201 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
5202 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
5203 		error = config_immediate_children(dip, flags, major);
5204 	} else {
5205 		/* call bus_config entry point */
5206 		ddi_bus_config_op_t bus_op = (major == DDI_MAJOR_T_NONE) ?
5207 		    BUS_CONFIG_ALL : BUS_CONFIG_DRIVER;
5208 		error = (*f)(dip,
5209 		    flags, bus_op, (void *)(uintptr_t)major, NULL, 0);
5210 	}
5211 
5212 	if (error) {
5213 		pm_post_config(dip, NULL);
5214 		return (error);
5215 	}
5216 
5217 	/*
5218 	 * Some callers, notably SCSI, need to mark the devfs cache
5219 	 * to be rebuilt together with the config operation.
5220 	 */
5221 	if (flags & NDI_DEVFS_CLEAN)
5222 		(void) devfs_clean(dip, NULL, 0);
5223 
5224 	if (flags & NDI_CONFIG)
5225 		(void) config_grand_children(dip, flags, major);
5226 
5227 	pm_post_config(dip, NULL);
5228 
5229 	return (NDI_SUCCESS);
5230 }
5231 
5232 /*
5233  * Framework entry point for BUS_CONFIG_ALL
5234  */
5235 int
5236 ndi_devi_config(dev_info_t *dip, int flags)
5237 {
5238 	NDI_CONFIG_DEBUG((CE_CONT,
5239 	    "ndi_devi_config: par = %s%d (%p), flags = 0x%x\n",
5240 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
5241 
5242 	return (devi_config_common(dip, flags, DDI_MAJOR_T_NONE));
5243 }
5244 
5245 /*
5246  * Framework entry point for BUS_CONFIG_DRIVER, bound to major
5247  */
5248 int
5249 ndi_devi_config_driver(dev_info_t *dip, int flags, major_t major)
5250 {
5251 	/* don't abuse this function */
5252 	ASSERT(major != DDI_MAJOR_T_NONE);
5253 
5254 	NDI_CONFIG_DEBUG((CE_CONT,
5255 	    "ndi_devi_config_driver: par = %s%d (%p), flags = 0x%x\n",
5256 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
5257 
5258 	return (devi_config_common(dip, flags, major));
5259 }
5260 
5261 /*
5262  * Called by nexus drivers to configure its children.
5263  */
5264 static int
5265 devi_config_one(dev_info_t *pdip, char *devnm, dev_info_t **cdipp,
5266     uint_t flags, clock_t timeout)
5267 {
5268 	dev_info_t	*vdip = NULL;
5269 	char		*drivername = NULL;
5270 	int		find_by_addr = 0;
5271 	char		*name, *addr;
5272 	int		v_circ, p_circ;
5273 	clock_t		end_time;	/* 60 sec */
5274 	int		probed;
5275 	dev_info_t	*cdip;
5276 	mdi_pathinfo_t	*cpip;
5277 
5278 	*cdipp = NULL;
5279 
5280 	if (!NEXUS_DRV(ddi_get_driver(pdip)))
5281 		return (NDI_FAILURE);
5282 
5283 	/* split name into "name@addr" parts */
5284 	i_ddi_parse_name(devnm, &name, &addr, NULL);
5285 
5286 	/*
5287 	 * If the nexus is a pHCI and we are not processing a pHCI from
5288 	 * mdi bus_config code then we need to know the vHCI.
5289 	 */
5290 	if (MDI_PHCI(pdip))
5291 		vdip = mdi_devi_get_vdip(pdip);
5292 
5293 	/*
5294 	 * We may have a genericname on a system that creates drivername
5295 	 * nodes (from .conf files).  Find the drivername by nodeid. If we
5296 	 * can't find a node with devnm as the node name then we search by
5297 	 * drivername.  This allows an implementation to supply a genericly
5298 	 * named boot path (disk) and locate drivename nodes (sd).  The
5299 	 * NDI_PROMNAME flag does not apply to /devices/pseudo paths.
5300 	 */
5301 	if ((flags & NDI_PROMNAME) && (pdip != pseudo_dip)) {
5302 		drivername = child_path_to_driver(pdip, name, addr);
5303 		find_by_addr = 1;
5304 	}
5305 
5306 	/*
5307 	 * Determine end_time: This routine should *not* be called with a
5308 	 * constant non-zero timeout argument, the caller should be adjusting
5309 	 * the timeout argument relative to when it *started* its asynchronous
5310 	 * enumeration.
5311 	 */
5312 	if (timeout > 0)
5313 		end_time = ddi_get_lbolt() + timeout;
5314 
5315 	for (;;) {
5316 		/*
5317 		 * For pHCI, enter (vHCI, pHCI) and search for pathinfo/client
5318 		 * child - break out of for(;;) loop if child found.
5319 		 * NOTE: Lock order for ndi_devi_enter is (vHCI, pHCI).
5320 		 */
5321 		if (vdip) {
5322 			/* use mdi_devi_enter ordering */
5323 			ndi_devi_enter(vdip, &v_circ);
5324 			ndi_devi_enter(pdip, &p_circ);
5325 			cpip = mdi_pi_find(pdip, NULL, addr);
5326 			cdip = mdi_pi_get_client(cpip);
5327 			if (cdip)
5328 				break;
5329 		} else
5330 			ndi_devi_enter(pdip, &p_circ);
5331 
5332 		/*
5333 		 * When not a  vHCI or not all pHCI devices are required to
5334 		 * enumerated under the vHCI (NDI_MDI_FALLBACK) search for
5335 		 * devinfo child.
5336 		 */
5337 		if ((vdip == NULL) || (flags & NDI_MDI_FALLBACK)) {
5338 			/* determine if .conf nodes already built */
5339 			probed = (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
5340 
5341 			/*
5342 			 * Search for child by name, if not found then search
5343 			 * for a node bound to the drivername driver with the
5344 			 * specified "@addr". Break out of for(;;) loop if
5345 			 * child found.  To support path-oriented aliases
5346 			 * binding on boot-device, we do a search_by_addr too.
5347 			 */
5348 again:			(void) i_ndi_make_spec_children(pdip, flags);
5349 			cdip = find_child_by_name(pdip, name, addr);
5350 			if ((cdip == NULL) && drivername)
5351 				cdip = find_child_by_driver(pdip,
5352 				    drivername, addr);
5353 			if ((cdip == NULL) && find_by_addr)
5354 				cdip = find_child_by_addr(pdip, addr);
5355 			if (cdip)
5356 				break;
5357 
5358 			/*
5359 			 * determine if we should reenumerate .conf nodes
5360 			 * and look for child again.
5361 			 */
5362 			if (probed &&
5363 			    i_ddi_io_initialized() &&
5364 			    (flags & NDI_CONFIG_REPROBE) &&
5365 			    ((timeout <= 0) || (ddi_get_lbolt() >= end_time))) {
5366 				probed = 0;
5367 				mutex_enter(&DEVI(pdip)->devi_lock);
5368 				DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
5369 				mutex_exit(&DEVI(pdip)->devi_lock);
5370 				goto again;
5371 			}
5372 		}
5373 
5374 		/* break out of for(;;) if time expired */
5375 		if ((timeout <= 0) || (ddi_get_lbolt() >= end_time))
5376 			break;
5377 
5378 		/*
5379 		 * Child not found, exit and wait for asynchronous enumeration
5380 		 * to add child (or timeout). The addition of a new child (vhci
5381 		 * or phci) requires the asynchronous enumeration thread to
5382 		 * ndi_devi_enter/ndi_devi_exit. This exit will signal devi_cv
5383 		 * and cause us to return from ndi_devi_exit_and_wait, after
5384 		 * which we loop and search for the requested child again.
5385 		 */
5386 		NDI_DEBUG(flags, (CE_CONT,
5387 		    "%s%d: waiting for child %s@%s, timeout %ld",
5388 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
5389 		    name, addr, timeout));
5390 		if (vdip) {
5391 			/*
5392 			 * Mark vHCI for pHCI ndi_devi_exit broadcast.
5393 			 */
5394 			mutex_enter(&DEVI(vdip)->devi_lock);
5395 			DEVI(vdip)->devi_flags |=
5396 			    DEVI_PHCI_SIGNALS_VHCI;
5397 			mutex_exit(&DEVI(vdip)->devi_lock);
5398 			ndi_devi_exit(pdip, p_circ);
5399 
5400 			/*
5401 			 * NB: There is a small race window from above
5402 			 * ndi_devi_exit() of pdip to cv_wait() in
5403 			 * ndi_devi_exit_and_wait() which can result in
5404 			 * not immediately finding a new pHCI child
5405 			 * of a pHCI that uses NDI_MDI_FAILBACK.
5406 			 */
5407 			ndi_devi_exit_and_wait(vdip, v_circ, end_time);
5408 		} else {
5409 			ndi_devi_exit_and_wait(pdip, p_circ, end_time);
5410 		}
5411 	}
5412 
5413 	/* done with paddr, fixup i_ddi_parse_name '@'->'\0' change */
5414 	if (addr && *addr != '\0')
5415 		*(addr - 1) = '@';
5416 
5417 	/* attach and hold the child, returning pointer to child */
5418 	if (cdip && (devi_attach_node(cdip, flags) == NDI_SUCCESS)) {
5419 		ndi_hold_devi(cdip);
5420 		*cdipp = cdip;
5421 	}
5422 
5423 	ndi_devi_exit(pdip, p_circ);
5424 	if (vdip)
5425 		ndi_devi_exit(vdip, v_circ);
5426 	return (*cdipp ? NDI_SUCCESS : NDI_FAILURE);
5427 }
5428 
5429 /*
5430  * Enumerate and attach a child specified by name 'devnm'.
5431  * Called by devfs lookup and DR to perform a BUS_CONFIG_ONE.
5432  * Note: devfs does not make use of NDI_CONFIG to configure
5433  * an entire branch.
5434  */
5435 int
5436 ndi_devi_config_one(dev_info_t *dip, char *devnm, dev_info_t **dipp, int flags)
5437 {
5438 	int error;
5439 	int (*f)();
5440 	int branch_event = 0;
5441 
5442 	ASSERT(dipp);
5443 	ASSERT(i_ddi_devi_attached(dip));
5444 
5445 	NDI_CONFIG_DEBUG((CE_CONT,
5446 	    "ndi_devi_config_one: par = %s%d (%p), child = %s\n",
5447 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, devnm));
5448 
5449 	if (pm_pre_config(dip, devnm) != DDI_SUCCESS)
5450 		return (NDI_FAILURE);
5451 
5452 	if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
5453 	    (flags & NDI_CONFIG)) {
5454 		flags |= NDI_BRANCH_EVENT_OP;
5455 		branch_event = 1;
5456 	}
5457 
5458 	if ((DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
5459 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
5460 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
5461 		error = devi_config_one(dip, devnm, dipp, flags, 0);
5462 	} else {
5463 		/* call bus_config entry point */
5464 		error = (*f)(dip, flags, BUS_CONFIG_ONE, (void *)devnm, dipp);
5465 	}
5466 
5467 	if (error || (flags & NDI_CONFIG) == 0) {
5468 		pm_post_config(dip, devnm);
5469 		return (error);
5470 	}
5471 
5472 	/*
5473 	 * DR usage (i.e. call with NDI_CONFIG) recursively configures
5474 	 * grandchildren, performing a BUS_CONFIG_ALL from the node attached
5475 	 * by the BUS_CONFIG_ONE.
5476 	 */
5477 	ASSERT(*dipp);
5478 
5479 	error = devi_config_common(*dipp, flags, DDI_MAJOR_T_NONE);
5480 
5481 	pm_post_config(dip, devnm);
5482 
5483 	if (branch_event)
5484 		(void) i_log_devfs_branch_add(*dipp);
5485 
5486 	return (error);
5487 }
5488 
5489 
5490 /*
5491  * Enumerate and attach a child specified by name 'devnm'.
5492  * Called during configure the OBP options. This configures
5493  * only one node.
5494  */
5495 static int
5496 ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
5497     dev_info_t **childp, int flags)
5498 {
5499 	int error;
5500 	int (*f)();
5501 
5502 	ASSERT(childp);
5503 	ASSERT(i_ddi_devi_attached(parent));
5504 
5505 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_config_obp_args: "
5506 	    "par = %s%d (%p), child = %s\n", ddi_driver_name(parent),
5507 	    ddi_get_instance(parent), (void *)parent, devnm));
5508 
5509 	if ((DEVI(parent)->devi_ops->devo_bus_ops == NULL) ||
5510 	    (DEVI(parent)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
5511 	    (f = DEVI(parent)->devi_ops->devo_bus_ops->bus_config) == NULL) {
5512 		error = NDI_FAILURE;
5513 	} else {
5514 		/* call bus_config entry point */
5515 		error = (*f)(parent, flags,
5516 		    BUS_CONFIG_OBP_ARGS, (void *)devnm, childp);
5517 	}
5518 	return (error);
5519 }
5520 
5521 /*
5522  * Pay attention, the following is a bit tricky:
5523  * There are three possible cases when constraints are applied
5524  *
5525  *	- A constraint is applied and the offline is disallowed.
5526  *	  Simply return failure and block the offline
5527  *
5528  *	- A constraint is applied and the offline is allowed.
5529  *	  Mark the dip as having passed the constraint and allow
5530  *	  offline to proceed.
5531  *
5532  *	- A constraint is not applied. Allow the offline to proceed for now.
5533  *
5534  * In the latter two cases we allow the offline to proceed. If the
5535  * offline succeeds (no users) everything is fine. It is ok for an unused
5536  * device to be offlined even if no constraints were imposed on the offline.
5537  * If the offline fails because there are users, we look at the constraint
5538  * flag on the dip. If the constraint flag is set (implying that it passed
5539  * a constraint) we allow the dip to be retired. If not, we don't allow
5540  * the retire. This ensures that we don't allow unconstrained retire.
5541  */
5542 int
5543 e_ddi_offline_notify(dev_info_t *dip)
5544 {
5545 	int retval;
5546 	int constraint;
5547 	int failure;
5548 
5549 	RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): entered: dip=%p",
5550 	    (void *) dip));
5551 
5552 	constraint = 0;
5553 	failure = 0;
5554 
5555 	/*
5556 	 * Start with userland constraints first - applied via device contracts
5557 	 */
5558 	retval = contract_device_offline(dip, DDI_DEV_T_ANY, 0);
5559 	switch (retval) {
5560 	case CT_NACK:
5561 		RIO_DEBUG((CE_NOTE, "Received NACK for dip=%p", (void *)dip));
5562 		failure = 1;
5563 		goto out;
5564 	case CT_ACK:
5565 		constraint = 1;
5566 		RIO_DEBUG((CE_NOTE, "Received ACK for dip=%p", (void *)dip));
5567 		break;
5568 	case CT_NONE:
5569 		/* no contracts */
5570 		RIO_DEBUG((CE_NOTE, "No contracts on dip=%p", (void *)dip));
5571 		break;
5572 	default:
5573 		ASSERT(retval == CT_NONE);
5574 	}
5575 
5576 	/*
5577 	 * Next, use LDI to impose kernel constraints
5578 	 */
5579 	retval = ldi_invoke_notify(dip, DDI_DEV_T_ANY, 0, LDI_EV_OFFLINE, NULL);
5580 	switch (retval) {
5581 	case LDI_EV_FAILURE:
5582 		contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_FAILURE);
5583 		RIO_DEBUG((CE_NOTE, "LDI callback failed on dip=%p",
5584 		    (void *)dip));
5585 		failure = 1;
5586 		goto out;
5587 	case LDI_EV_SUCCESS:
5588 		constraint = 1;
5589 		RIO_DEBUG((CE_NOTE, "LDI callback success on dip=%p",
5590 		    (void *)dip));
5591 		break;
5592 	case LDI_EV_NONE:
5593 		/* no matching LDI callbacks */
5594 		RIO_DEBUG((CE_NOTE, "No LDI callbacks for dip=%p",
5595 		    (void *)dip));
5596 		break;
5597 	default:
5598 		ASSERT(retval == LDI_EV_NONE);
5599 	}
5600 
5601 out:
5602 	mutex_enter(&(DEVI(dip)->devi_lock));
5603 	if ((DEVI(dip)->devi_flags & DEVI_RETIRING) && failure) {
5604 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): setting "
5605 		    "BLOCKED flag. dip=%p", (void *)dip));
5606 		DEVI(dip)->devi_flags |= DEVI_R_BLOCKED;
5607 		if (DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT) {
5608 			RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): "
5609 			    "blocked. clearing RCM CONSTRAINT flag. dip=%p",
5610 			    (void *)dip));
5611 			DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
5612 		}
5613 	} else if ((DEVI(dip)->devi_flags & DEVI_RETIRING) && constraint) {
5614 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): setting "
5615 		    "CONSTRAINT flag. dip=%p", (void *)dip));
5616 		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
5617 	} else if ((DEVI(dip)->devi_flags & DEVI_RETIRING) &&
5618 	    DEVI(dip)->devi_ref == 0) {
5619 		/* also allow retire if device is not in use */
5620 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): device not in "
5621 		    "use. Setting CONSTRAINT flag. dip=%p", (void *)dip));
5622 		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
5623 	} else {
5624 		/*
5625 		 * Note: We cannot ASSERT here that DEVI_R_CONSTRAINT is
5626 		 * not set, since other sources (such as RCM) may have
5627 		 * set the flag.
5628 		 */
5629 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): not setting "
5630 		    "constraint flag. dip=%p", (void *)dip));
5631 	}
5632 	mutex_exit(&(DEVI(dip)->devi_lock));
5633 
5634 
5635 	RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): exit: dip=%p",
5636 	    (void *) dip));
5637 
5638 	return (failure ? DDI_FAILURE : DDI_SUCCESS);
5639 }
5640 
5641 void
5642 e_ddi_offline_finalize(dev_info_t *dip, int result)
5643 {
5644 	RIO_DEBUG((CE_NOTE, "e_ddi_offline_finalize(): entry: result=%s, "
5645 	    "dip=%p", result == DDI_SUCCESS ? "SUCCESS" : "FAILURE",
5646 	    (void *)dip));
5647 
5648 	contract_device_negend(dip, DDI_DEV_T_ANY, 0,  result == DDI_SUCCESS ?
5649 	    CT_EV_SUCCESS : CT_EV_FAILURE);
5650 
5651 	ldi_invoke_finalize(dip, DDI_DEV_T_ANY, 0,
5652 	    LDI_EV_OFFLINE, result == DDI_SUCCESS ?
5653 	    LDI_EV_SUCCESS : LDI_EV_FAILURE, NULL);
5654 
5655 	RIO_VERBOSE((CE_NOTE, "e_ddi_offline_finalize(): exit: dip=%p",
5656 	    (void *)dip));
5657 }
5658 
5659 void
5660 e_ddi_degrade_finalize(dev_info_t *dip)
5661 {
5662 	RIO_DEBUG((CE_NOTE, "e_ddi_degrade_finalize(): entry: "
5663 	    "result always = DDI_SUCCESS, dip=%p", (void *)dip));
5664 
5665 	contract_device_degrade(dip, DDI_DEV_T_ANY, 0);
5666 	contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_SUCCESS);
5667 
5668 	ldi_invoke_finalize(dip, DDI_DEV_T_ANY, 0, LDI_EV_DEGRADE,
5669 	    LDI_EV_SUCCESS, NULL);
5670 
5671 	RIO_VERBOSE((CE_NOTE, "e_ddi_degrade_finalize(): exit: dip=%p",
5672 	    (void *)dip));
5673 }
5674 
5675 void
5676 e_ddi_undegrade_finalize(dev_info_t *dip)
5677 {
5678 	RIO_DEBUG((CE_NOTE, "e_ddi_undegrade_finalize(): entry: "
5679 	    "result always = DDI_SUCCESS, dip=%p", (void *)dip));
5680 
5681 	contract_device_undegrade(dip, DDI_DEV_T_ANY, 0);
5682 	contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_SUCCESS);
5683 
5684 	RIO_VERBOSE((CE_NOTE, "e_ddi_undegrade_finalize(): exit: dip=%p",
5685 	    (void *)dip));
5686 }
5687 
5688 /*
5689  * detach a node with parent already held busy
5690  */
5691 static int
5692 devi_detach_node(dev_info_t *dip, uint_t flags)
5693 {
5694 	dev_info_t *pdip = ddi_get_parent(dip);
5695 	int ret = NDI_SUCCESS;
5696 	ddi_eventcookie_t cookie;
5697 	char *path = NULL;
5698 	char *class = NULL;
5699 	char *driver = NULL;
5700 	int instance = -1;
5701 	int post_event = 0;
5702 
5703 	ASSERT(pdip && DEVI_BUSY_OWNED(pdip));
5704 
5705 	/*
5706 	 * Invoke notify if offlining
5707 	 */
5708 	if (flags & NDI_DEVI_OFFLINE) {
5709 		RIO_DEBUG((CE_NOTE, "devi_detach_node: offlining dip=%p",
5710 		    (void *)dip));
5711 		if (e_ddi_offline_notify(dip) != DDI_SUCCESS) {
5712 			RIO_DEBUG((CE_NOTE, "devi_detach_node: offline NACKed"
5713 			    "dip=%p", (void *)dip));
5714 			return (NDI_FAILURE);
5715 		}
5716 	}
5717 
5718 	if (flags & NDI_POST_EVENT) {
5719 		if (i_ddi_devi_attached(pdip)) {
5720 			if (ddi_get_eventcookie(dip, DDI_DEVI_REMOVE_EVENT,
5721 			    &cookie) == NDI_SUCCESS)
5722 				(void) ndi_post_event(dip, dip, cookie, NULL);
5723 		}
5724 	}
5725 
5726 	if (i_ddi_detachchild(dip, flags) != DDI_SUCCESS) {
5727 		if (flags & NDI_DEVI_OFFLINE) {
5728 			RIO_DEBUG((CE_NOTE, "devi_detach_node: offline failed."
5729 			    " Calling e_ddi_offline_finalize with result=%d. "
5730 			    "dip=%p", DDI_FAILURE, (void *)dip));
5731 			e_ddi_offline_finalize(dip, DDI_FAILURE);
5732 		}
5733 		return (NDI_FAILURE);
5734 	}
5735 
5736 	if (flags & NDI_DEVI_OFFLINE) {
5737 		RIO_DEBUG((CE_NOTE, "devi_detach_node: offline succeeded."
5738 		    " Calling e_ddi_offline_finalize with result=%d, "
5739 		    "dip=%p", DDI_SUCCESS, (void *)dip));
5740 		e_ddi_offline_finalize(dip, DDI_SUCCESS);
5741 	}
5742 
5743 	if (flags & NDI_AUTODETACH)
5744 		return (NDI_SUCCESS);
5745 
5746 	/*
5747 	 * For DR, even bound nodes may need to have offline
5748 	 * flag set.
5749 	 */
5750 	if (flags & NDI_DEVI_OFFLINE) {
5751 		mutex_enter(&(DEVI(dip)->devi_lock));
5752 		DEVI_SET_DEVICE_OFFLINE(dip);
5753 		mutex_exit(&(DEVI(dip)->devi_lock));
5754 	}
5755 
5756 	if (i_ddi_node_state(dip) == DS_INITIALIZED) {
5757 		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
5758 		(void) ddi_pathname(dip, path);
5759 		if (flags & NDI_DEVI_OFFLINE)
5760 			i_ndi_devi_report_status_change(dip, path);
5761 
5762 		if (need_remove_event(dip, flags)) {
5763 			post_event = 1;
5764 			class = i_ddi_strdup(i_ddi_devi_class(dip), KM_SLEEP);
5765 			driver = i_ddi_strdup((char *)ddi_driver_name(dip),
5766 			    KM_SLEEP);
5767 			instance = ddi_get_instance(dip);
5768 
5769 			mutex_enter(&(DEVI(dip)->devi_lock));
5770 			DEVI_SET_EVREMOVE(dip);
5771 			mutex_exit(&(DEVI(dip)->devi_lock));
5772 		}
5773 	}
5774 
5775 	if (flags & (NDI_UNCONFIG | NDI_DEVI_REMOVE)) {
5776 		ret = ddi_uninitchild(dip);
5777 		if (ret == NDI_SUCCESS) {
5778 			/*
5779 			 * Remove uninitialized pseudo nodes because
5780 			 * system props are lost and the node cannot be
5781 			 * reattached.
5782 			 */
5783 			if (!ndi_dev_is_persistent_node(dip))
5784 				flags |= NDI_DEVI_REMOVE;
5785 
5786 			if (flags & NDI_DEVI_REMOVE) {
5787 				ret = ddi_remove_child(dip, 0);
5788 				if (post_event && ret == NDI_SUCCESS) {
5789 					(void) i_log_devfs_remove_devinfo(path,
5790 					    class, driver, instance, flags);
5791 				}
5792 			}
5793 
5794 		}
5795 	}
5796 
5797 	if (path)
5798 		kmem_free(path, MAXPATHLEN);
5799 	if (class)
5800 		kmem_free(class, strlen(class) + 1);
5801 	if (driver)
5802 		kmem_free(driver, strlen(driver) + 1);
5803 
5804 	return (ret);
5805 }
5806 
5807 /*
5808  * unconfigure immediate children of bus nexus device
5809  */
5810 static int
5811 unconfig_immediate_children(
5812 	dev_info_t *dip,
5813 	dev_info_t **dipp,
5814 	int flags,
5815 	major_t major)
5816 {
5817 	int rv = NDI_SUCCESS;
5818 	int circ, vcirc;
5819 	dev_info_t *child;
5820 	dev_info_t *vdip = NULL;
5821 	dev_info_t *next;
5822 
5823 	ASSERT(dipp == NULL || *dipp == NULL);
5824 
5825 	/*
5826 	 * Scan forward to see if we will be processing a pHCI child. If we
5827 	 * have a child that is a pHCI and vHCI and pHCI are not siblings then
5828 	 * enter vHCI before parent(pHCI) to prevent deadlock with mpxio
5829 	 * Client power management operations.
5830 	 */
5831 	ndi_devi_enter(dip, &circ);
5832 	for (child = ddi_get_child(dip); child;
5833 	    child = ddi_get_next_sibling(child)) {
5834 		/* skip same nodes we skip below */
5835 		if (((major != DDI_MAJOR_T_NONE) &&
5836 		    (major != ddi_driver_major(child))) ||
5837 		    ((flags & NDI_AUTODETACH) && !is_leaf_node(child)))
5838 			continue;
5839 
5840 		if (MDI_PHCI(child)) {
5841 			vdip = mdi_devi_get_vdip(child);
5842 			/*
5843 			 * If vHCI and vHCI is not a sibling of pHCI
5844 			 * then enter in (vHCI, parent(pHCI)) order.
5845 			 */
5846 			if (vdip && (ddi_get_parent(vdip) != dip)) {
5847 				ndi_devi_exit(dip, circ);
5848 
5849 				/* use mdi_devi_enter ordering */
5850 				ndi_devi_enter(vdip, &vcirc);
5851 				ndi_devi_enter(dip, &circ);
5852 				break;
5853 			} else
5854 				vdip = NULL;
5855 		}
5856 	}
5857 
5858 	child = ddi_get_child(dip);
5859 	while (child) {
5860 		next = ddi_get_next_sibling(child);
5861 
5862 		if ((major != DDI_MAJOR_T_NONE) &&
5863 		    (major != ddi_driver_major(child))) {
5864 			child = next;
5865 			continue;
5866 		}
5867 
5868 		/* skip nexus nodes during autodetach */
5869 		if ((flags & NDI_AUTODETACH) && !is_leaf_node(child)) {
5870 			child = next;
5871 			continue;
5872 		}
5873 
5874 		if (devi_detach_node(child, flags) != NDI_SUCCESS) {
5875 			if (dipp && *dipp == NULL) {
5876 				ndi_hold_devi(child);
5877 				*dipp = child;
5878 			}
5879 			rv = NDI_FAILURE;
5880 		}
5881 
5882 		/*
5883 		 * Continue upon failure--best effort algorithm
5884 		 */
5885 		child = next;
5886 	}
5887 
5888 	ndi_devi_exit(dip, circ);
5889 	if (vdip)
5890 		ndi_devi_exit(vdip, vcirc);
5891 
5892 	return (rv);
5893 }
5894 
5895 /*
5896  * unconfigure grand children of bus nexus device
5897  */
5898 static int
5899 unconfig_grand_children(
5900 	dev_info_t *dip,
5901 	dev_info_t **dipp,
5902 	int flags,
5903 	major_t major,
5904 	struct brevq_node **brevqp)
5905 {
5906 	struct mt_config_handle *hdl;
5907 
5908 	if (brevqp)
5909 		*brevqp = NULL;
5910 
5911 	/* multi-threaded configuration of child nexus */
5912 	hdl = mt_config_init(dip, dipp, flags, major, MT_UNCONFIG_OP, brevqp);
5913 	mt_config_children(hdl);
5914 
5915 	return (mt_config_fini(hdl));	/* wait for threads to exit */
5916 }
5917 
5918 /*
5919  * Unconfigure children/descendants of the dip.
5920  *
5921  * If brevqp is not NULL, on return *brevqp is set to a queue of dip's
5922  * child devinames for which branch remove events need to be generated.
5923  */
5924 static int
5925 devi_unconfig_common(
5926 	dev_info_t *dip,
5927 	dev_info_t **dipp,
5928 	int flags,
5929 	major_t major,
5930 	struct brevq_node **brevqp)
5931 {
5932 	int rv;
5933 	int pm_cookie;
5934 	int (*f)();
5935 	ddi_bus_config_op_t bus_op;
5936 
5937 	if (dipp)
5938 		*dipp = NULL;
5939 	if (brevqp)
5940 		*brevqp = NULL;
5941 
5942 	/*
5943 	 * Power up the dip if it is powered off.  If the flag bit
5944 	 * NDI_AUTODETACH is set and the dip is not at its full power,
5945 	 * skip the rest of the branch.
5946 	 */
5947 	if (pm_pre_unconfig(dip, flags, &pm_cookie, NULL) != DDI_SUCCESS)
5948 		return ((flags & NDI_AUTODETACH) ? NDI_SUCCESS :
5949 		    NDI_FAILURE);
5950 
5951 	/*
5952 	 * Some callers, notably SCSI, need to clear out the devfs
5953 	 * cache together with the unconfig to prevent stale entries.
5954 	 */
5955 	if (flags & NDI_DEVFS_CLEAN)
5956 		(void) devfs_clean(dip, NULL, 0);
5957 
5958 	rv = unconfig_grand_children(dip, dipp, flags, major, brevqp);
5959 
5960 	if ((rv != NDI_SUCCESS) && ((flags & NDI_AUTODETACH) == 0)) {
5961 		if (brevqp && *brevqp) {
5962 			log_and_free_br_events_on_grand_children(dip, *brevqp);
5963 			free_brevq(*brevqp);
5964 			*brevqp = NULL;
5965 		}
5966 		pm_post_unconfig(dip, pm_cookie, NULL);
5967 		return (rv);
5968 	}
5969 
5970 	if (dipp && *dipp) {
5971 		ndi_rele_devi(*dipp);
5972 		*dipp = NULL;
5973 	}
5974 
5975 	/*
5976 	 * It is possible to have a detached nexus with children
5977 	 * and grandchildren (for example: a branch consisting
5978 	 * entirely of bound nodes.) Since the nexus is detached
5979 	 * the bus_unconfig entry point cannot be used to remove
5980 	 * or unconfigure the descendants.
5981 	 */
5982 	if (!i_ddi_devi_attached(dip) ||
5983 	    (DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
5984 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
5985 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
5986 		rv = unconfig_immediate_children(dip, dipp, flags, major);
5987 	} else {
5988 		/*
5989 		 * call bus_unconfig entry point
5990 		 * It should reset nexus flags if unconfigure succeeds.
5991 		 */
5992 		bus_op = (major == DDI_MAJOR_T_NONE) ?
5993 		    BUS_UNCONFIG_ALL : BUS_UNCONFIG_DRIVER;
5994 		rv = (*f)(dip, flags, bus_op, (void *)(uintptr_t)major);
5995 	}
5996 
5997 	pm_post_unconfig(dip, pm_cookie, NULL);
5998 
5999 	if (brevqp && *brevqp)
6000 		cleanup_br_events_on_grand_children(dip, brevqp);
6001 
6002 	return (rv);
6003 }
6004 
6005 /*
6006  * called by devfs/framework to unconfigure children bound to major
6007  * If NDI_AUTODETACH is specified, this is invoked by either the
6008  * moduninstall daemon or the modunload -i 0 command.
6009  */
6010 int
6011 ndi_devi_unconfig_driver(dev_info_t *dip, int flags, major_t major)
6012 {
6013 	NDI_CONFIG_DEBUG((CE_CONT,
6014 	    "ndi_devi_unconfig_driver: par = %s%d (%p), flags = 0x%x\n",
6015 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
6016 
6017 	return (devi_unconfig_common(dip, NULL, flags, major, NULL));
6018 }
6019 
6020 int
6021 ndi_devi_unconfig(dev_info_t *dip, int flags)
6022 {
6023 	NDI_CONFIG_DEBUG((CE_CONT,
6024 	    "ndi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
6025 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
6026 
6027 	return (devi_unconfig_common(dip, NULL, flags, DDI_MAJOR_T_NONE, NULL));
6028 }
6029 
6030 int
6031 e_ddi_devi_unconfig(dev_info_t *dip, dev_info_t **dipp, int flags)
6032 {
6033 	NDI_CONFIG_DEBUG((CE_CONT,
6034 	    "e_ddi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
6035 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
6036 
6037 	return (devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE, NULL));
6038 }
6039 
6040 /*
6041  * Unconfigure child by name
6042  */
6043 static int
6044 devi_unconfig_one(dev_info_t *pdip, char *devnm, int flags)
6045 {
6046 	int		rv, circ;
6047 	dev_info_t	*child;
6048 	dev_info_t	*vdip = NULL;
6049 	int		v_circ;
6050 
6051 	ndi_devi_enter(pdip, &circ);
6052 	child = ndi_devi_findchild(pdip, devnm);
6053 
6054 	/*
6055 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
6056 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
6057 	 * management operations.
6058 	 */
6059 	if (child && MDI_PHCI(child)) {
6060 		vdip = mdi_devi_get_vdip(child);
6061 		if (vdip && (ddi_get_parent(vdip) != pdip)) {
6062 			ndi_devi_exit(pdip, circ);
6063 
6064 			/* use mdi_devi_enter ordering */
6065 			ndi_devi_enter(vdip, &v_circ);
6066 			ndi_devi_enter(pdip, &circ);
6067 			child = ndi_devi_findchild(pdip, devnm);
6068 		} else
6069 			vdip = NULL;
6070 	}
6071 
6072 	if (child) {
6073 		rv = devi_detach_node(child, flags);
6074 	} else {
6075 		NDI_CONFIG_DEBUG((CE_CONT,
6076 		    "devi_unconfig_one: %s not found\n", devnm));
6077 		rv = NDI_SUCCESS;
6078 	}
6079 
6080 	ndi_devi_exit(pdip, circ);
6081 	if (vdip)
6082 		ndi_devi_exit(vdip, v_circ);
6083 
6084 	return (rv);
6085 }
6086 
6087 int
6088 ndi_devi_unconfig_one(
6089 	dev_info_t *pdip,
6090 	char *devnm,
6091 	dev_info_t **dipp,
6092 	int flags)
6093 {
6094 	int		(*f)();
6095 	int		circ, rv;
6096 	int		pm_cookie;
6097 	dev_info_t	*child;
6098 	dev_info_t	*vdip = NULL;
6099 	int		v_circ;
6100 	struct brevq_node *brevq = NULL;
6101 
6102 	ASSERT(i_ddi_devi_attached(pdip));
6103 
6104 	NDI_CONFIG_DEBUG((CE_CONT,
6105 	    "ndi_devi_unconfig_one: par = %s%d (%p), child = %s\n",
6106 	    ddi_driver_name(pdip), ddi_get_instance(pdip),
6107 	    (void *)pdip, devnm));
6108 
6109 	if (pm_pre_unconfig(pdip, flags, &pm_cookie, devnm) != DDI_SUCCESS)
6110 		return (NDI_FAILURE);
6111 
6112 	if (dipp)
6113 		*dipp = NULL;
6114 
6115 	ndi_devi_enter(pdip, &circ);
6116 	child = ndi_devi_findchild(pdip, devnm);
6117 
6118 	/*
6119 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
6120 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
6121 	 * management operations.
6122 	 */
6123 	if (child && MDI_PHCI(child)) {
6124 		vdip = mdi_devi_get_vdip(child);
6125 		if (vdip && (ddi_get_parent(vdip) != pdip)) {
6126 			ndi_devi_exit(pdip, circ);
6127 
6128 			/* use mdi_devi_enter ordering */
6129 			ndi_devi_enter(vdip, &v_circ);
6130 			ndi_devi_enter(pdip, &circ);
6131 			child = ndi_devi_findchild(pdip, devnm);
6132 		} else
6133 			vdip = NULL;
6134 	}
6135 
6136 	if (child == NULL) {
6137 		NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_unconfig_one: %s"
6138 		    " not found\n", devnm));
6139 		rv = NDI_SUCCESS;
6140 		goto out;
6141 	}
6142 
6143 	/*
6144 	 * Unconfigure children/descendants of named child
6145 	 */
6146 	rv = devi_unconfig_branch(child, dipp, flags | NDI_UNCONFIG, &brevq);
6147 	if (rv != NDI_SUCCESS)
6148 		goto out;
6149 
6150 	init_bound_node_ev(pdip, child, flags);
6151 
6152 	if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
6153 	    (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
6154 	    (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
6155 		rv = devi_detach_node(child, flags);
6156 	} else {
6157 		/* call bus_config entry point */
6158 		rv = (*f)(pdip, flags, BUS_UNCONFIG_ONE, (void *)devnm);
6159 	}
6160 
6161 	if (brevq) {
6162 		if (rv != NDI_SUCCESS)
6163 			log_and_free_brevq_dip(child, brevq);
6164 		else
6165 			free_brevq(brevq);
6166 	}
6167 
6168 	if (dipp && rv != NDI_SUCCESS) {
6169 		ndi_hold_devi(child);
6170 		ASSERT(*dipp == NULL);
6171 		*dipp = child;
6172 	}
6173 
6174 out:
6175 	ndi_devi_exit(pdip, circ);
6176 	if (vdip)
6177 		ndi_devi_exit(vdip, v_circ);
6178 
6179 	pm_post_unconfig(pdip, pm_cookie, devnm);
6180 
6181 	return (rv);
6182 }
6183 
6184 struct async_arg {
6185 	dev_info_t *dip;
6186 	uint_t flags;
6187 };
6188 
6189 /*
6190  * Common async handler for:
6191  *	ndi_devi_bind_driver_async
6192  *	ndi_devi_online_async
6193  */
6194 static int
6195 i_ndi_devi_async_common(dev_info_t *dip, uint_t flags, void (*func)())
6196 {
6197 	int tqflag;
6198 	int kmflag;
6199 	struct async_arg *arg;
6200 	dev_info_t *pdip = ddi_get_parent(dip);
6201 
6202 	ASSERT(pdip);
6203 	ASSERT(DEVI(pdip)->devi_taskq);
6204 	ASSERT(ndi_dev_is_persistent_node(dip));
6205 
6206 	if (flags & NDI_NOSLEEP) {
6207 		kmflag = KM_NOSLEEP;
6208 		tqflag = TQ_NOSLEEP;
6209 	} else {
6210 		kmflag = KM_SLEEP;
6211 		tqflag = TQ_SLEEP;
6212 	}
6213 
6214 	arg = kmem_alloc(sizeof (*arg), kmflag);
6215 	if (arg == NULL)
6216 		goto fail;
6217 
6218 	arg->flags = flags;
6219 	arg->dip = dip;
6220 	if (ddi_taskq_dispatch(DEVI(pdip)->devi_taskq, func, arg, tqflag) ==
6221 	    DDI_SUCCESS) {
6222 		return (NDI_SUCCESS);
6223 	}
6224 
6225 fail:
6226 	NDI_CONFIG_DEBUG((CE_CONT, "%s%d: ddi_taskq_dispatch failed",
6227 	    ddi_driver_name(pdip), ddi_get_instance(pdip)));
6228 
6229 	if (arg)
6230 		kmem_free(arg, sizeof (*arg));
6231 	return (NDI_FAILURE);
6232 }
6233 
6234 static void
6235 i_ndi_devi_bind_driver_cb(struct async_arg *arg)
6236 {
6237 	(void) ndi_devi_bind_driver(arg->dip, arg->flags);
6238 	kmem_free(arg, sizeof (*arg));
6239 }
6240 
6241 int
6242 ndi_devi_bind_driver_async(dev_info_t *dip, uint_t flags)
6243 {
6244 	return (i_ndi_devi_async_common(dip, flags,
6245 	    (void (*)())i_ndi_devi_bind_driver_cb));
6246 }
6247 
6248 /*
6249  * place the devinfo in the ONLINE state.
6250  */
6251 int
6252 ndi_devi_online(dev_info_t *dip, uint_t flags)
6253 {
6254 	int circ, rv;
6255 	dev_info_t *pdip = ddi_get_parent(dip);
6256 	int branch_event = 0;
6257 
6258 	ASSERT(pdip);
6259 
6260 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_online: %s%d (%p)\n",
6261 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
6262 
6263 	ndi_devi_enter(pdip, &circ);
6264 	/* bind child before merging .conf nodes */
6265 	rv = i_ndi_config_node(dip, DS_BOUND, flags);
6266 	if (rv != NDI_SUCCESS) {
6267 		ndi_devi_exit(pdip, circ);
6268 		return (rv);
6269 	}
6270 
6271 	/* merge .conf properties */
6272 	(void) i_ndi_make_spec_children(pdip, flags);
6273 
6274 	flags |= (NDI_DEVI_ONLINE | NDI_CONFIG);
6275 
6276 	if (flags & NDI_NO_EVENT) {
6277 		/*
6278 		 * Caller is specifically asking for not to generate an event.
6279 		 * Set the following flag so that devi_attach_node() don't
6280 		 * change the event state.
6281 		 */
6282 		flags |= NDI_NO_EVENT_STATE_CHNG;
6283 	}
6284 
6285 	if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
6286 	    ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip))) {
6287 		flags |= NDI_BRANCH_EVENT_OP;
6288 		branch_event = 1;
6289 	}
6290 
6291 	/*
6292 	 * devi_attach_node() may remove dip on failure
6293 	 */
6294 	if ((rv = devi_attach_node(dip, flags)) == NDI_SUCCESS) {
6295 		if ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip)) {
6296 			(void) ndi_devi_config(dip, flags);
6297 		}
6298 
6299 		if (branch_event)
6300 			(void) i_log_devfs_branch_add(dip);
6301 	}
6302 
6303 	ndi_devi_exit(pdip, circ);
6304 
6305 	/*
6306 	 * Notify devfs that we have a new node. Devfs needs to invalidate
6307 	 * cached directory contents.
6308 	 *
6309 	 * For PCMCIA devices, it is possible the pdip is not fully
6310 	 * attached. In this case, calling back into devfs will
6311 	 * result in a loop or assertion error. Hence, the check
6312 	 * on node state.
6313 	 *
6314 	 * If we own parent lock, this is part of a branch operation.
6315 	 * We skip the devfs_clean() step because the cache invalidation
6316 	 * is done higher up in the device tree.
6317 	 */
6318 	if (rv == NDI_SUCCESS && i_ddi_devi_attached(pdip) &&
6319 	    !DEVI_BUSY_OWNED(pdip))
6320 		(void) devfs_clean(pdip, NULL, 0);
6321 	return (rv);
6322 }
6323 
6324 static void
6325 i_ndi_devi_online_cb(struct async_arg *arg)
6326 {
6327 	(void) ndi_devi_online(arg->dip, arg->flags);
6328 	kmem_free(arg, sizeof (*arg));
6329 }
6330 
6331 int
6332 ndi_devi_online_async(dev_info_t *dip, uint_t flags)
6333 {
6334 	/* mark child as need config if requested. */
6335 	if (flags & NDI_CONFIG) {
6336 		mutex_enter(&(DEVI(dip)->devi_lock));
6337 		DEVI_SET_NDI_CONFIG(dip);
6338 		mutex_exit(&(DEVI(dip)->devi_lock));
6339 	}
6340 
6341 	return (i_ndi_devi_async_common(dip, flags,
6342 	    (void (*)())i_ndi_devi_online_cb));
6343 }
6344 
6345 /*
6346  * Take a device node Offline
6347  * To take a device Offline means to detach the device instance from
6348  * the driver and prevent devfs requests from re-attaching the device
6349  * instance.
6350  *
6351  * The flag NDI_DEVI_REMOVE causes removes the device node from
6352  * the driver list and the device tree. In this case, the device
6353  * is assumed to be removed from the system.
6354  */
6355 int
6356 ndi_devi_offline(dev_info_t *dip, uint_t flags)
6357 {
6358 	int		circ, rval = 0;
6359 	dev_info_t	*pdip = ddi_get_parent(dip);
6360 	dev_info_t	*vdip = NULL;
6361 	int		v_circ;
6362 	struct brevq_node *brevq = NULL;
6363 
6364 	ASSERT(pdip);
6365 
6366 	flags |= NDI_DEVI_OFFLINE;
6367 
6368 	/*
6369 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
6370 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
6371 	 * management operations.
6372 	 */
6373 	if (MDI_PHCI(dip)) {
6374 		vdip = mdi_devi_get_vdip(dip);
6375 		if (vdip && (ddi_get_parent(vdip) != pdip))
6376 			ndi_devi_enter(vdip, &v_circ);
6377 		else
6378 			vdip = NULL;
6379 	}
6380 	ndi_devi_enter(pdip, &circ);
6381 
6382 	if (i_ddi_node_state(dip) == DS_READY) {
6383 		/*
6384 		 * If dip is in DS_READY state, there may be cached dv_nodes
6385 		 * referencing this dip, so we invoke devfs code path.
6386 		 * Note that we must release busy changing on pdip to
6387 		 * avoid deadlock against devfs.
6388 		 */
6389 		char *devname = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
6390 		(void) ddi_deviname(dip, devname);
6391 
6392 		ndi_devi_exit(pdip, circ);
6393 		if (vdip)
6394 			ndi_devi_exit(vdip, v_circ);
6395 
6396 		/*
6397 		 * If we own parent lock, this is part of a branch
6398 		 * operation. We skip the devfs_clean() step.
6399 		 */
6400 		if (!DEVI_BUSY_OWNED(pdip))
6401 			(void) devfs_clean(pdip, devname + 1, DV_CLEAN_FORCE);
6402 		kmem_free(devname, MAXNAMELEN + 1);
6403 
6404 		rval = devi_unconfig_branch(dip, NULL, flags|NDI_UNCONFIG,
6405 		    &brevq);
6406 
6407 		if (rval)
6408 			return (NDI_FAILURE);
6409 
6410 		if (vdip)
6411 			ndi_devi_enter(vdip, &v_circ);
6412 		ndi_devi_enter(pdip, &circ);
6413 	}
6414 
6415 	init_bound_node_ev(pdip, dip, flags);
6416 
6417 	rval = devi_detach_node(dip, flags);
6418 	if (brevq) {
6419 		if (rval != NDI_SUCCESS)
6420 			log_and_free_brevq_dip(dip, brevq);
6421 		else
6422 			free_brevq(brevq);
6423 	}
6424 
6425 	ndi_devi_exit(pdip, circ);
6426 	if (vdip)
6427 		ndi_devi_exit(vdip, v_circ);
6428 
6429 	return (rval);
6430 }
6431 
6432 /*
6433  * Find the child dev_info node of parent nexus 'p' whose name
6434  * matches "cname@caddr".  Recommend use of ndi_devi_findchild() instead.
6435  */
6436 dev_info_t *
6437 ndi_devi_find(dev_info_t *pdip, char *cname, char *caddr)
6438 {
6439 	dev_info_t *child;
6440 	int circ;
6441 
6442 	if (pdip == NULL || cname == NULL || caddr == NULL)
6443 		return ((dev_info_t *)NULL);
6444 
6445 	ndi_devi_enter(pdip, &circ);
6446 	child = find_sibling(ddi_get_child(pdip), cname, caddr,
6447 	    FIND_NODE_BY_NODENAME, NULL);
6448 	ndi_devi_exit(pdip, circ);
6449 	return (child);
6450 }
6451 
6452 /*
6453  * Find the child dev_info node of parent nexus 'p' whose name
6454  * matches devname "name@addr".  Permits caller to hold the parent.
6455  */
6456 dev_info_t *
6457 ndi_devi_findchild(dev_info_t *pdip, char *devname)
6458 {
6459 	dev_info_t *child;
6460 	char	*cname, *caddr;
6461 	char	*devstr;
6462 
6463 	ASSERT(DEVI_BUSY_OWNED(pdip));
6464 
6465 	devstr = i_ddi_strdup(devname, KM_SLEEP);
6466 	i_ddi_parse_name(devstr, &cname, &caddr, NULL);
6467 
6468 	if (cname == NULL || caddr == NULL) {
6469 		kmem_free(devstr, strlen(devname)+1);
6470 		return ((dev_info_t *)NULL);
6471 	}
6472 
6473 	child = find_sibling(ddi_get_child(pdip), cname, caddr,
6474 	    FIND_NODE_BY_NODENAME, NULL);
6475 	kmem_free(devstr, strlen(devname)+1);
6476 	return (child);
6477 }
6478 
6479 /*
6480  * Misc. routines called by framework only
6481  */
6482 
6483 /*
6484  * Clear the DEVI_MADE_CHILDREN/DEVI_ATTACHED_CHILDREN flags
6485  * if new child spec has been added.
6486  */
6487 static int
6488 reset_nexus_flags(dev_info_t *dip, void *arg)
6489 {
6490 	struct hwc_spec	*list;
6491 	int		circ;
6492 
6493 	if (((DEVI(dip)->devi_flags & DEVI_MADE_CHILDREN) == 0) ||
6494 	    ((list = hwc_get_child_spec(dip, (major_t)(uintptr_t)arg)) == NULL))
6495 		return (DDI_WALK_CONTINUE);
6496 
6497 	hwc_free_spec_list(list);
6498 
6499 	/* coordinate child state update */
6500 	ndi_devi_enter(dip, &circ);
6501 	mutex_enter(&DEVI(dip)->devi_lock);
6502 	DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN | DEVI_ATTACHED_CHILDREN);
6503 	mutex_exit(&DEVI(dip)->devi_lock);
6504 	ndi_devi_exit(dip, circ);
6505 
6506 	return (DDI_WALK_CONTINUE);
6507 }
6508 
6509 /*
6510  * Helper functions, returns NULL if no memory.
6511  */
6512 
6513 /*
6514  * path_to_major:
6515  *
6516  * Return an alternate driver name binding for the leaf device
6517  * of the given pathname, if there is one. The purpose of this
6518  * function is to deal with generic pathnames. The default action
6519  * for platforms that can't do this (ie: x86 or any platform that
6520  * does not have prom_finddevice functionality, which matches
6521  * nodenames and unit-addresses without the drivers participation)
6522  * is to return DDI_MAJOR_T_NONE.
6523  *
6524  * Used in loadrootmodules() in the swapgeneric module to
6525  * associate a given pathname with a given leaf driver.
6526  *
6527  */
6528 major_t
6529 path_to_major(char *path)
6530 {
6531 	dev_info_t *dip;
6532 	char *p, *q;
6533 	pnode_t nodeid;
6534 	major_t major;
6535 
6536 	/* check for path-oriented alias */
6537 	major = ddi_name_to_major(path);
6538 	if ((major != DDI_MAJOR_T_NONE) &&
6539 	    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED)) {
6540 		NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s path bound %s\n",
6541 		    path, ddi_major_to_name(major)));
6542 		return (major);
6543 	}
6544 
6545 	/*
6546 	 * Get the nodeid of the given pathname, if such a mapping exists.
6547 	 */
6548 	dip = NULL;
6549 	nodeid = prom_finddevice(path);
6550 	if (nodeid != OBP_BADNODE) {
6551 		/*
6552 		 * Find the nodeid in our copy of the device tree and return
6553 		 * whatever name we used to bind this node to a driver.
6554 		 */
6555 		dip = e_ddi_nodeid_to_dip(nodeid);
6556 	}
6557 
6558 	if (dip == NULL) {
6559 		NDI_CONFIG_DEBUG((CE_WARN,
6560 		    "path_to_major: can't bind <%s>\n", path));
6561 		return (DDI_MAJOR_T_NONE);
6562 	}
6563 
6564 	/*
6565 	 * If we're bound to something other than the nodename,
6566 	 * note that in the message buffer and system log.
6567 	 */
6568 	p = ddi_binding_name(dip);
6569 	q = ddi_node_name(dip);
6570 	if (p && q && (strcmp(p, q) != 0))
6571 		NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s bound to %s\n",
6572 		    path, p));
6573 
6574 	major = ddi_name_to_major(p);
6575 
6576 	ndi_rele_devi(dip);		/* release e_ddi_nodeid_to_dip hold */
6577 
6578 	return (major);
6579 }
6580 
6581 /*
6582  * Return the held dip for the specified major and instance, attempting to do
6583  * an attach if specified. Return NULL if the devi can't be found or put in
6584  * the proper state. The caller must release the hold via ddi_release_devi if
6585  * a non-NULL value is returned.
6586  *
6587  * Some callers expect to be able to perform a hold_devi() while in a context
6588  * where using ndi_devi_enter() to ensure the hold might cause deadlock (see
6589  * open-from-attach code in consconfig_dacf.c). Such special-case callers
6590  * must ensure that an ndi_devi_enter(parent)/ndi_devi_hold() from a safe
6591  * context is already active. The hold_devi() implementation must accommodate
6592  * these callers.
6593  */
6594 static dev_info_t *
6595 hold_devi(major_t major, int instance, int flags)
6596 {
6597 	struct devnames	*dnp;
6598 	dev_info_t	*dip;
6599 	char		*path;
6600 
6601 	if ((major >= devcnt) || (instance == -1))
6602 		return (NULL);
6603 
6604 	/* try to find the instance in the per driver list */
6605 	dnp = &(devnamesp[major]);
6606 	LOCK_DEV_OPS(&(dnp->dn_lock));
6607 	for (dip = dnp->dn_head; dip;
6608 	    dip = (dev_info_t *)DEVI(dip)->devi_next) {
6609 		/* skip node if instance field is not valid */
6610 		if (i_ddi_node_state(dip) < DS_INITIALIZED)
6611 			continue;
6612 
6613 		/* look for instance match */
6614 		if (DEVI(dip)->devi_instance == instance) {
6615 			/*
6616 			 * To accommodate callers that can't block in
6617 			 * ndi_devi_enter() we do an ndi_devi_hold(), and
6618 			 * afterwards check that the node is in a state where
6619 			 * the hold prevents detach(). If we did not manage to
6620 			 * prevent detach then we ndi_rele_devi() and perform
6621 			 * the slow path below (which can result in a blocking
6622 			 * ndi_devi_enter() while driving attach top-down).
6623 			 * This code depends on the ordering of
6624 			 * DEVI_SET_DETACHING and the devi_ref check in the
6625 			 * detach_node() code path.
6626 			 */
6627 			ndi_hold_devi(dip);
6628 			if (i_ddi_devi_attached(dip) &&
6629 			    !DEVI_IS_DETACHING(dip)) {
6630 				UNLOCK_DEV_OPS(&(dnp->dn_lock));
6631 				return (dip);	/* fast-path with devi held */
6632 			}
6633 			ndi_rele_devi(dip);
6634 
6635 			/* try slow-path */
6636 			dip = NULL;
6637 			break;
6638 		}
6639 	}
6640 	ASSERT(dip == NULL);
6641 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
6642 
6643 	if (flags & E_DDI_HOLD_DEVI_NOATTACH)
6644 		return (NULL);		/* told not to drive attach */
6645 
6646 	/* slow-path may block, so it should not occur from interrupt */
6647 	ASSERT(!servicing_interrupt());
6648 	if (servicing_interrupt())
6649 		return (NULL);
6650 
6651 	/* reconstruct the path and drive attach by path through devfs. */
6652 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
6653 	if (e_ddi_majorinstance_to_path(major, instance, path) == 0)
6654 		dip = e_ddi_hold_devi_by_path(path, flags);
6655 	kmem_free(path, MAXPATHLEN);
6656 	return (dip);			/* with devi held */
6657 }
6658 
6659 /*
6660  * The {e_}ddi_hold_devi{_by_{instance|dev|path}} hold the devinfo node
6661  * associated with the specified arguments.  This hold should be released
6662  * by calling ddi_release_devi.
6663  *
6664  * The E_DDI_HOLD_DEVI_NOATTACH flag argument allows the caller to to specify
6665  * a failure return if the node is not already attached.
6666  *
6667  * NOTE: by the time we make e_ddi_hold_devi public, we should be able to reuse
6668  * ddi_hold_devi again.
6669  */
6670 dev_info_t *
6671 ddi_hold_devi_by_instance(major_t major, int instance, int flags)
6672 {
6673 	return (hold_devi(major, instance, flags));
6674 }
6675 
6676 dev_info_t *
6677 e_ddi_hold_devi_by_dev(dev_t dev, int flags)
6678 {
6679 	major_t	major = getmajor(dev);
6680 	dev_info_t	*dip;
6681 	struct dev_ops	*ops;
6682 	dev_info_t	*ddip = NULL;
6683 
6684 	dip = hold_devi(major, dev_to_instance(dev), flags);
6685 
6686 	/*
6687 	 * The rest of this routine is legacy support for drivers that
6688 	 * have broken DDI_INFO_DEVT2INSTANCE implementations but may have
6689 	 * functional DDI_INFO_DEVT2DEVINFO implementations.  This code will
6690 	 * diagnose inconsistency and, for maximum compatibility with legacy
6691 	 * drivers, give preference to the drivers DDI_INFO_DEVT2DEVINFO
6692 	 * implementation over the above derived dip based the driver's
6693 	 * DDI_INFO_DEVT2INSTANCE implementation. This legacy support should
6694 	 * be removed when DDI_INFO_DEVT2DEVINFO is deprecated.
6695 	 *
6696 	 * NOTE: The following code has a race condition. DEVT2DEVINFO
6697 	 *	returns a dip which is not held. By the time we ref ddip,
6698 	 *	it could have been freed. The saving grace is that for
6699 	 *	most drivers, the dip returned from hold_devi() is the
6700 	 *	same one as the one returned by DEVT2DEVINFO, so we are
6701 	 *	safe for drivers with the correct getinfo(9e) impl.
6702 	 */
6703 	if (((ops = ddi_hold_driver(major)) != NULL) &&
6704 	    CB_DRV_INSTALLED(ops) && ops->devo_getinfo)  {
6705 		if ((*ops->devo_getinfo)(NULL, DDI_INFO_DEVT2DEVINFO,
6706 		    (void *)dev, (void **)&ddip) != DDI_SUCCESS)
6707 			ddip = NULL;
6708 	}
6709 
6710 	/* give preference to the driver returned DEVT2DEVINFO dip */
6711 	if (ddip && (dip != ddip)) {
6712 #ifdef	DEBUG
6713 		cmn_err(CE_WARN, "%s: inconsistent getinfo(9E) implementation",
6714 		    ddi_driver_name(ddip));
6715 #endif	/* DEBUG */
6716 		ndi_hold_devi(ddip);
6717 		if (dip)
6718 			ndi_rele_devi(dip);
6719 		dip = ddip;
6720 	}
6721 
6722 	if (ops)
6723 		ddi_rele_driver(major);
6724 
6725 	return (dip);
6726 }
6727 
6728 /*
6729  * For compatibility only. Do not call this function!
6730  */
6731 dev_info_t *
6732 e_ddi_get_dev_info(dev_t dev, vtype_t type)
6733 {
6734 	dev_info_t *dip = NULL;
6735 	if (getmajor(dev) >= devcnt)
6736 		return (NULL);
6737 
6738 	switch (type) {
6739 	case VCHR:
6740 	case VBLK:
6741 		dip = e_ddi_hold_devi_by_dev(dev, 0);
6742 	default:
6743 		break;
6744 	}
6745 
6746 	/*
6747 	 * For compatibility reasons, we can only return the dip with
6748 	 * the driver ref count held. This is not a safe thing to do.
6749 	 * For certain broken third-party software, we are willing
6750 	 * to venture into unknown territory.
6751 	 */
6752 	if (dip) {
6753 		(void) ndi_hold_driver(dip);
6754 		ndi_rele_devi(dip);
6755 	}
6756 	return (dip);
6757 }
6758 
6759 dev_info_t *
6760 e_ddi_hold_devi_by_path(char *path, int flags)
6761 {
6762 	dev_info_t	*dip;
6763 
6764 	/* can't specify NOATTACH by path */
6765 	ASSERT(!(flags & E_DDI_HOLD_DEVI_NOATTACH));
6766 
6767 	return (resolve_pathname(path, &dip, NULL, NULL) ? NULL : dip);
6768 }
6769 
6770 void
6771 e_ddi_hold_devi(dev_info_t *dip)
6772 {
6773 	ndi_hold_devi(dip);
6774 }
6775 
6776 void
6777 ddi_release_devi(dev_info_t *dip)
6778 {
6779 	ndi_rele_devi(dip);
6780 }
6781 
6782 /*
6783  * Associate a streams queue with a devinfo node
6784  * NOTE: This function is called by STREAM driver's put procedure.
6785  *	It cannot block.
6786  */
6787 void
6788 ddi_assoc_queue_with_devi(queue_t *q, dev_info_t *dip)
6789 {
6790 	queue_t *rq = _RD(q);
6791 	struct stdata *stp;
6792 	vnode_t *vp;
6793 
6794 	/* set flag indicating that ddi_assoc_queue_with_devi was called */
6795 	mutex_enter(QLOCK(rq));
6796 	rq->q_flag |= _QASSOCIATED;
6797 	mutex_exit(QLOCK(rq));
6798 
6799 	/* get the vnode associated with the queue */
6800 	stp = STREAM(rq);
6801 	vp = stp->sd_vnode;
6802 	ASSERT(vp);
6803 
6804 	/* change the hardware association of the vnode */
6805 	spec_assoc_vp_with_devi(vp, dip);
6806 }
6807 
6808 /*
6809  * ddi_install_driver(name)
6810  *
6811  * Driver installation is currently a byproduct of driver loading.  This
6812  * may change.
6813  */
6814 int
6815 ddi_install_driver(char *name)
6816 {
6817 	major_t major = ddi_name_to_major(name);
6818 
6819 	if ((major == DDI_MAJOR_T_NONE) ||
6820 	    (ddi_hold_installed_driver(major) == NULL)) {
6821 		return (DDI_FAILURE);
6822 	}
6823 	ddi_rele_driver(major);
6824 	return (DDI_SUCCESS);
6825 }
6826 
6827 struct dev_ops *
6828 ddi_hold_driver(major_t major)
6829 {
6830 	return (mod_hold_dev_by_major(major));
6831 }
6832 
6833 
6834 void
6835 ddi_rele_driver(major_t major)
6836 {
6837 	mod_rele_dev_by_major(major);
6838 }
6839 
6840 
6841 /*
6842  * This is called during boot to force attachment order of special dips
6843  * dip must be referenced via ndi_hold_devi()
6844  */
6845 int
6846 i_ddi_attach_node_hierarchy(dev_info_t *dip)
6847 {
6848 	dev_info_t	*parent;
6849 	int		ret, circ;
6850 
6851 	/*
6852 	 * Recurse up until attached parent is found.
6853 	 */
6854 	if (i_ddi_devi_attached(dip))
6855 		return (DDI_SUCCESS);
6856 	parent = ddi_get_parent(dip);
6857 	if (i_ddi_attach_node_hierarchy(parent) != DDI_SUCCESS)
6858 		return (DDI_FAILURE);
6859 
6860 	/*
6861 	 * Come top-down, expanding .conf nodes under this parent
6862 	 * and driving attach.
6863 	 */
6864 	ndi_devi_enter(parent, &circ);
6865 	(void) i_ndi_make_spec_children(parent, 0);
6866 	ret = i_ddi_attachchild(dip);
6867 	ndi_devi_exit(parent, circ);
6868 
6869 	return (ret);
6870 }
6871 
6872 /* keep this function static */
6873 static int
6874 attach_driver_nodes(major_t major)
6875 {
6876 	struct devnames *dnp;
6877 	dev_info_t *dip;
6878 	int error = DDI_FAILURE;
6879 
6880 	dnp = &devnamesp[major];
6881 	LOCK_DEV_OPS(&dnp->dn_lock);
6882 	dip = dnp->dn_head;
6883 	while (dip) {
6884 		ndi_hold_devi(dip);
6885 		UNLOCK_DEV_OPS(&dnp->dn_lock);
6886 		if (i_ddi_attach_node_hierarchy(dip) == DDI_SUCCESS)
6887 			error = DDI_SUCCESS;
6888 		LOCK_DEV_OPS(&dnp->dn_lock);
6889 		ndi_rele_devi(dip);
6890 		dip = ddi_get_next(dip);
6891 	}
6892 	if (error == DDI_SUCCESS)
6893 		dnp->dn_flags |= DN_NO_AUTODETACH;
6894 	UNLOCK_DEV_OPS(&dnp->dn_lock);
6895 
6896 
6897 	return (error);
6898 }
6899 
6900 /*
6901  * i_ddi_attach_hw_nodes configures and attaches all hw nodes
6902  * bound to a specific driver. This function replaces calls to
6903  * ddi_hold_installed_driver() for drivers with no .conf
6904  * enumerated nodes.
6905  *
6906  * This facility is typically called at boot time to attach
6907  * platform-specific hardware nodes, such as ppm nodes on xcal
6908  * and grover and keyswitch nodes on cherrystone. It does not
6909  * deal with .conf enumerated node. Calling it beyond the boot
6910  * process is strongly discouraged.
6911  */
6912 int
6913 i_ddi_attach_hw_nodes(char *driver)
6914 {
6915 	major_t major;
6916 
6917 	major = ddi_name_to_major(driver);
6918 	if (major == DDI_MAJOR_T_NONE)
6919 		return (DDI_FAILURE);
6920 
6921 	return (attach_driver_nodes(major));
6922 }
6923 
6924 /*
6925  * i_ddi_attach_pseudo_node configures pseudo drivers which
6926  * has a single node. The .conf nodes must be enumerated
6927  * before calling this interface. The dip is held attached
6928  * upon returning.
6929  *
6930  * This facility should only be called only at boot time
6931  * by the I/O framework.
6932  */
6933 dev_info_t *
6934 i_ddi_attach_pseudo_node(char *driver)
6935 {
6936 	major_t major;
6937 	dev_info_t *dip;
6938 
6939 	major = ddi_name_to_major(driver);
6940 	if (major == DDI_MAJOR_T_NONE)
6941 		return (NULL);
6942 
6943 	if (attach_driver_nodes(major) != DDI_SUCCESS)
6944 		return (NULL);
6945 
6946 	dip = devnamesp[major].dn_head;
6947 	ASSERT(dip && ddi_get_next(dip) == NULL);
6948 	ndi_hold_devi(dip);
6949 	return (dip);
6950 }
6951 
6952 static void
6953 diplist_to_parent_major(dev_info_t *head, char parents[])
6954 {
6955 	major_t major;
6956 	dev_info_t *dip, *pdip;
6957 
6958 	for (dip = head; dip != NULL; dip = ddi_get_next(dip)) {
6959 		pdip = ddi_get_parent(dip);
6960 		ASSERT(pdip);	/* disallow rootnex.conf nodes */
6961 		major = ddi_driver_major(pdip);
6962 		if ((major != DDI_MAJOR_T_NONE) && parents[major] == 0)
6963 			parents[major] = 1;
6964 	}
6965 }
6966 
6967 /*
6968  * Call ddi_hold_installed_driver() on each parent major
6969  * and invoke mt_config_driver() to attach child major.
6970  * This is part of the implementation of ddi_hold_installed_driver.
6971  */
6972 static int
6973 attach_driver_by_parent(major_t child_major, char parents[])
6974 {
6975 	major_t par_major;
6976 	struct mt_config_handle *hdl;
6977 	int flags = NDI_DEVI_PERSIST | NDI_NO_EVENT;
6978 
6979 	hdl = mt_config_init(NULL, NULL, flags, child_major, MT_CONFIG_OP,
6980 	    NULL);
6981 	for (par_major = 0; par_major < devcnt; par_major++) {
6982 		/* disallow recursion on the same driver */
6983 		if (parents[par_major] == 0 || par_major == child_major)
6984 			continue;
6985 		if (ddi_hold_installed_driver(par_major) == NULL)
6986 			continue;
6987 		hdl->mtc_parmajor = par_major;
6988 		mt_config_driver(hdl);
6989 		ddi_rele_driver(par_major);
6990 	}
6991 	(void) mt_config_fini(hdl);
6992 
6993 	return (i_ddi_devs_attached(child_major));
6994 }
6995 
6996 int
6997 i_ddi_devs_attached(major_t major)
6998 {
6999 	dev_info_t *dip;
7000 	struct devnames *dnp;
7001 	int error = DDI_FAILURE;
7002 
7003 	/* check for attached instances */
7004 	dnp = &devnamesp[major];
7005 	LOCK_DEV_OPS(&dnp->dn_lock);
7006 	for (dip = dnp->dn_head; dip != NULL; dip = ddi_get_next(dip)) {
7007 		if (i_ddi_devi_attached(dip)) {
7008 			error = DDI_SUCCESS;
7009 			break;
7010 		}
7011 	}
7012 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7013 
7014 	return (error);
7015 }
7016 
7017 int
7018 i_ddi_minor_node_count(dev_info_t *ddip, const char *node_type)
7019 {
7020 	int			circ;
7021 	struct ddi_minor_data	*dp;
7022 	int			count = 0;
7023 
7024 	ndi_devi_enter(ddip, &circ);
7025 	for (dp = DEVI(ddip)->devi_minor; dp != NULL; dp = dp->next) {
7026 		if (strcmp(dp->ddm_node_type, node_type) == 0)
7027 			count++;
7028 	}
7029 	ndi_devi_exit(ddip, circ);
7030 	return (count);
7031 }
7032 
7033 /*
7034  * ddi_hold_installed_driver configures and attaches all
7035  * instances of the specified driver. To accomplish this
7036  * it configures and attaches all possible parents of
7037  * the driver, enumerated both in h/w nodes and in the
7038  * driver's .conf file.
7039  *
7040  * NOTE: This facility is for compatibility purposes only and will
7041  *	eventually go away. Its usage is strongly discouraged.
7042  */
7043 static void
7044 enter_driver(struct devnames *dnp)
7045 {
7046 	mutex_enter(&dnp->dn_lock);
7047 	ASSERT(dnp->dn_busy_thread != curthread);
7048 	while (dnp->dn_flags & DN_DRIVER_BUSY)
7049 		cv_wait(&dnp->dn_wait, &dnp->dn_lock);
7050 	dnp->dn_flags |= DN_DRIVER_BUSY;
7051 	dnp->dn_busy_thread = curthread;
7052 	mutex_exit(&dnp->dn_lock);
7053 }
7054 
7055 static void
7056 exit_driver(struct devnames *dnp)
7057 {
7058 	mutex_enter(&dnp->dn_lock);
7059 	ASSERT(dnp->dn_busy_thread == curthread);
7060 	dnp->dn_flags &= ~DN_DRIVER_BUSY;
7061 	dnp->dn_busy_thread = NULL;
7062 	cv_broadcast(&dnp->dn_wait);
7063 	mutex_exit(&dnp->dn_lock);
7064 }
7065 
7066 struct dev_ops *
7067 ddi_hold_installed_driver(major_t major)
7068 {
7069 	struct dev_ops *ops;
7070 	struct devnames *dnp;
7071 	char *parents;
7072 	int error;
7073 
7074 	ops = ddi_hold_driver(major);
7075 	if (ops == NULL)
7076 		return (NULL);
7077 
7078 	/*
7079 	 * Return immediately if all the attach operations associated
7080 	 * with a ddi_hold_installed_driver() call have already been done.
7081 	 */
7082 	dnp = &devnamesp[major];
7083 	enter_driver(dnp);
7084 	if (dnp->dn_flags & DN_DRIVER_HELD) {
7085 		exit_driver(dnp);
7086 		if (i_ddi_devs_attached(major) == DDI_SUCCESS)
7087 			return (ops);
7088 		ddi_rele_driver(major);
7089 		return (NULL);
7090 	}
7091 
7092 	LOCK_DEV_OPS(&dnp->dn_lock);
7093 	dnp->dn_flags |= (DN_DRIVER_HELD | DN_NO_AUTODETACH);
7094 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7095 
7096 	DCOMPATPRINTF((CE_CONT,
7097 	    "ddi_hold_installed_driver: %s\n", dnp->dn_name));
7098 
7099 	/*
7100 	 * When the driver has no .conf children, it is sufficient
7101 	 * to attach existing nodes in the device tree. Nodes not
7102 	 * enumerated by the OBP are not attached.
7103 	 */
7104 	if (dnp->dn_pl == NULL) {
7105 		if (attach_driver_nodes(major) == DDI_SUCCESS) {
7106 			exit_driver(dnp);
7107 			return (ops);
7108 		}
7109 		exit_driver(dnp);
7110 		ddi_rele_driver(major);
7111 		return (NULL);
7112 	}
7113 
7114 	/*
7115 	 * Driver has .conf nodes. We find all possible parents
7116 	 * and recursively all ddi_hold_installed_driver on the
7117 	 * parent driver; then we invoke ndi_config_driver()
7118 	 * on all possible parent node in parallel to speed up
7119 	 * performance.
7120 	 */
7121 	parents = kmem_zalloc(devcnt * sizeof (char), KM_SLEEP);
7122 
7123 	LOCK_DEV_OPS(&dnp->dn_lock);
7124 	/* find .conf parents */
7125 	(void) impl_parlist_to_major(dnp->dn_pl, parents);
7126 	/* find hw node parents */
7127 	diplist_to_parent_major(dnp->dn_head, parents);
7128 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7129 
7130 	error = attach_driver_by_parent(major, parents);
7131 	kmem_free(parents, devcnt * sizeof (char));
7132 	if (error == DDI_SUCCESS) {
7133 		exit_driver(dnp);
7134 		return (ops);
7135 	}
7136 
7137 	exit_driver(dnp);
7138 	ddi_rele_driver(major);
7139 	return (NULL);
7140 }
7141 
7142 /*
7143  * Default bus_config entry point for nexus drivers
7144  */
7145 int
7146 ndi_busop_bus_config(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
7147     void *arg, dev_info_t **child, clock_t timeout)
7148 {
7149 	major_t major;
7150 
7151 	/*
7152 	 * A timeout of 30 minutes or more is probably a mistake
7153 	 * This is intended to catch uses where timeout is in
7154 	 * the wrong units.  timeout must be in units of ticks.
7155 	 */
7156 	ASSERT(timeout < SEC_TO_TICK(1800));
7157 
7158 	major = DDI_MAJOR_T_NONE;
7159 	switch (op) {
7160 	case BUS_CONFIG_ONE:
7161 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config %s timeout=%ld\n",
7162 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
7163 		    (char *)arg, timeout));
7164 		return (devi_config_one(pdip, (char *)arg, child, flags,
7165 		    timeout));
7166 
7167 	case BUS_CONFIG_DRIVER:
7168 		major = (major_t)(uintptr_t)arg;
7169 		/*FALLTHROUGH*/
7170 	case BUS_CONFIG_ALL:
7171 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config timeout=%ld\n",
7172 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
7173 		    timeout));
7174 		if (timeout > 0) {
7175 			NDI_DEBUG(flags, (CE_CONT,
7176 			    "%s%d: bus config all timeout=%ld\n",
7177 			    ddi_driver_name(pdip), ddi_get_instance(pdip),
7178 			    timeout));
7179 			delay(timeout);
7180 		}
7181 		return (config_immediate_children(pdip, flags, major));
7182 
7183 	default:
7184 		return (NDI_FAILURE);
7185 	}
7186 	/*NOTREACHED*/
7187 }
7188 
7189 /*
7190  * Default busop bus_unconfig handler for nexus drivers
7191  */
7192 int
7193 ndi_busop_bus_unconfig(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
7194     void *arg)
7195 {
7196 	major_t major;
7197 
7198 	major = DDI_MAJOR_T_NONE;
7199 	switch (op) {
7200 	case BUS_UNCONFIG_ONE:
7201 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig %s\n",
7202 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
7203 		    (char *)arg));
7204 		return (devi_unconfig_one(pdip, (char *)arg, flags));
7205 
7206 	case BUS_UNCONFIG_DRIVER:
7207 		major = (major_t)(uintptr_t)arg;
7208 		/*FALLTHROUGH*/
7209 	case BUS_UNCONFIG_ALL:
7210 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig all\n",
7211 		    ddi_driver_name(pdip), ddi_get_instance(pdip)));
7212 		return (unconfig_immediate_children(pdip, NULL, flags, major));
7213 
7214 	default:
7215 		return (NDI_FAILURE);
7216 	}
7217 	/*NOTREACHED*/
7218 }
7219 
7220 /*
7221  * dummy functions to be removed
7222  */
7223 void
7224 impl_rem_dev_props(dev_info_t *dip)
7225 {
7226 	_NOTE(ARGUNUSED(dip))
7227 	/* do nothing */
7228 }
7229 
7230 /*
7231  * Determine if a node is a leaf node. If not sure, return false (0).
7232  */
7233 static int
7234 is_leaf_node(dev_info_t *dip)
7235 {
7236 	major_t major = ddi_driver_major(dip);
7237 
7238 	if (major == DDI_MAJOR_T_NONE)
7239 		return (0);
7240 
7241 	return (devnamesp[major].dn_flags & DN_LEAF_DRIVER);
7242 }
7243 
7244 /*
7245  * Multithreaded [un]configuration
7246  */
7247 static struct mt_config_handle *
7248 mt_config_init(dev_info_t *pdip, dev_info_t **dipp, int flags,
7249     major_t major, int op, struct brevq_node **brevqp)
7250 {
7251 	struct mt_config_handle	*hdl = kmem_alloc(sizeof (*hdl), KM_SLEEP);
7252 
7253 	mutex_init(&hdl->mtc_lock, NULL, MUTEX_DEFAULT, NULL);
7254 	cv_init(&hdl->mtc_cv, NULL, CV_DEFAULT, NULL);
7255 	hdl->mtc_pdip = pdip;
7256 	hdl->mtc_fdip = dipp;
7257 	hdl->mtc_parmajor = DDI_MAJOR_T_NONE;
7258 	hdl->mtc_flags = flags;
7259 	hdl->mtc_major = major;
7260 	hdl->mtc_thr_count = 0;
7261 	hdl->mtc_op = op;
7262 	hdl->mtc_error = 0;
7263 	hdl->mtc_brevqp = brevqp;
7264 
7265 #ifdef DEBUG
7266 	gethrestime(&hdl->start_time);
7267 	hdl->total_time = 0;
7268 #endif /* DEBUG */
7269 
7270 	return (hdl);
7271 }
7272 
7273 #ifdef DEBUG
7274 static int
7275 time_diff_in_msec(timestruc_t start, timestruc_t end)
7276 {
7277 	int	nsec, sec;
7278 
7279 	sec = end.tv_sec - start.tv_sec;
7280 	nsec = end.tv_nsec - start.tv_nsec;
7281 	if (nsec < 0) {
7282 		nsec += NANOSEC;
7283 		sec -= 1;
7284 	}
7285 
7286 	return (sec * (NANOSEC >> 20) + (nsec >> 20));
7287 }
7288 
7289 #endif	/* DEBUG */
7290 
7291 static int
7292 mt_config_fini(struct mt_config_handle *hdl)
7293 {
7294 	int		rv;
7295 #ifdef DEBUG
7296 	int		real_time;
7297 	timestruc_t	end_time;
7298 #endif /* DEBUG */
7299 
7300 	mutex_enter(&hdl->mtc_lock);
7301 	while (hdl->mtc_thr_count > 0)
7302 		cv_wait(&hdl->mtc_cv, &hdl->mtc_lock);
7303 	rv = hdl->mtc_error;
7304 	mutex_exit(&hdl->mtc_lock);
7305 
7306 #ifdef DEBUG
7307 	gethrestime(&end_time);
7308 	real_time = time_diff_in_msec(hdl->start_time, end_time);
7309 	if ((ddidebug & DDI_MTCONFIG) && hdl->mtc_pdip)
7310 		cmn_err(CE_NOTE,
7311 		    "config %s%d: total time %d msec, real time %d msec",
7312 		    ddi_driver_name(hdl->mtc_pdip),
7313 		    ddi_get_instance(hdl->mtc_pdip),
7314 		    hdl->total_time, real_time);
7315 #endif /* DEBUG */
7316 
7317 	cv_destroy(&hdl->mtc_cv);
7318 	mutex_destroy(&hdl->mtc_lock);
7319 	kmem_free(hdl, sizeof (*hdl));
7320 
7321 	return (rv);
7322 }
7323 
7324 struct mt_config_data {
7325 	struct mt_config_handle	*mtc_hdl;
7326 	dev_info_t		*mtc_dip;
7327 	major_t			mtc_major;
7328 	int			mtc_flags;
7329 	struct brevq_node	*mtc_brn;
7330 	struct mt_config_data	*mtc_next;
7331 };
7332 
7333 static void
7334 mt_config_thread(void *arg)
7335 {
7336 	struct mt_config_data	*mcd = (struct mt_config_data *)arg;
7337 	struct mt_config_handle	*hdl = mcd->mtc_hdl;
7338 	dev_info_t		*dip = mcd->mtc_dip;
7339 	dev_info_t		*rdip, **dipp;
7340 	major_t			major = mcd->mtc_major;
7341 	int			flags = mcd->mtc_flags;
7342 	int			rv = 0;
7343 
7344 #ifdef DEBUG
7345 	timestruc_t start_time, end_time;
7346 	gethrestime(&start_time);
7347 #endif /* DEBUG */
7348 
7349 	rdip = NULL;
7350 	dipp = hdl->mtc_fdip ? &rdip : NULL;
7351 
7352 	switch (hdl->mtc_op) {
7353 	case MT_CONFIG_OP:
7354 		rv = devi_config_common(dip, flags, major);
7355 		break;
7356 	case MT_UNCONFIG_OP:
7357 		if (mcd->mtc_brn) {
7358 			struct brevq_node *brevq = NULL;
7359 			rv = devi_unconfig_common(dip, dipp, flags, major,
7360 			    &brevq);
7361 			mcd->mtc_brn->brn_child = brevq;
7362 		} else
7363 			rv = devi_unconfig_common(dip, dipp, flags, major,
7364 			    NULL);
7365 		break;
7366 	}
7367 
7368 	mutex_enter(&hdl->mtc_lock);
7369 #ifdef DEBUG
7370 	gethrestime(&end_time);
7371 	hdl->total_time += time_diff_in_msec(start_time, end_time);
7372 #endif /* DEBUG */
7373 
7374 	if ((rv != NDI_SUCCESS) && (hdl->mtc_error == 0)) {
7375 		hdl->mtc_error = rv;
7376 #ifdef	DEBUG
7377 		if ((ddidebug & DDI_DEBUG) && (major != DDI_MAJOR_T_NONE)) {
7378 			char	*path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7379 
7380 			(void) ddi_pathname(dip, path);
7381 			cmn_err(CE_NOTE, "mt_config_thread: "
7382 			    "op %d.%d.%x at %s failed %d",
7383 			    hdl->mtc_op, major, flags, path, rv);
7384 			kmem_free(path, MAXPATHLEN);
7385 		}
7386 #endif	/* DEBUG */
7387 	}
7388 
7389 	if (hdl->mtc_fdip && *hdl->mtc_fdip == NULL) {
7390 		*hdl->mtc_fdip = rdip;
7391 		rdip = NULL;
7392 	}
7393 
7394 	if (rdip) {
7395 		ASSERT(rv != NDI_SUCCESS);
7396 		ndi_rele_devi(rdip);
7397 	}
7398 
7399 	ndi_rele_devi(dip);
7400 
7401 	if (--hdl->mtc_thr_count == 0)
7402 		cv_broadcast(&hdl->mtc_cv);
7403 	mutex_exit(&hdl->mtc_lock);
7404 	kmem_free(mcd, sizeof (*mcd));
7405 }
7406 
7407 /*
7408  * Multi-threaded config/unconfig of child nexus
7409  */
7410 static void
7411 mt_config_children(struct mt_config_handle *hdl)
7412 {
7413 	dev_info_t		*pdip = hdl->mtc_pdip;
7414 	major_t			major = hdl->mtc_major;
7415 	dev_info_t		*dip;
7416 	int			circ;
7417 	struct brevq_node	*brn;
7418 	struct mt_config_data	*mcd_head = NULL;
7419 	struct mt_config_data	*mcd_tail = NULL;
7420 	struct mt_config_data	*mcd;
7421 #ifdef DEBUG
7422 	timestruc_t		end_time;
7423 
7424 	/* Update total_time in handle */
7425 	gethrestime(&end_time);
7426 	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
7427 #endif
7428 
7429 	ndi_devi_enter(pdip, &circ);
7430 	dip = ddi_get_child(pdip);
7431 	while (dip) {
7432 		if (hdl->mtc_op == MT_UNCONFIG_OP && hdl->mtc_brevqp &&
7433 		    !(DEVI_EVREMOVE(dip)) &&
7434 		    i_ddi_node_state(dip) >= DS_INITIALIZED) {
7435 			/*
7436 			 * Enqueue this dip's deviname.
7437 			 * No need to hold a lock while enqueuing since this
7438 			 * is the only thread doing the enqueue and no one
7439 			 * walks the queue while we are in multithreaded
7440 			 * unconfiguration.
7441 			 */
7442 			brn = brevq_enqueue(hdl->mtc_brevqp, dip, NULL);
7443 		} else
7444 			brn = NULL;
7445 
7446 		/*
7447 		 * Hold the child that we are processing so he does not get
7448 		 * removed. The corrisponding ndi_rele_devi() for children
7449 		 * that are not being skipped is done at the end of
7450 		 * mt_config_thread().
7451 		 */
7452 		ndi_hold_devi(dip);
7453 
7454 		/*
7455 		 * skip leaf nodes and (for configure) nodes not
7456 		 * fully attached.
7457 		 */
7458 		if (is_leaf_node(dip) ||
7459 		    (hdl->mtc_op == MT_CONFIG_OP &&
7460 		    i_ddi_node_state(dip) < DS_READY)) {
7461 			ndi_rele_devi(dip);
7462 			dip = ddi_get_next_sibling(dip);
7463 			continue;
7464 		}
7465 
7466 		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
7467 		mcd->mtc_dip = dip;
7468 		mcd->mtc_hdl = hdl;
7469 		mcd->mtc_brn = brn;
7470 
7471 		/*
7472 		 * Switch a 'driver' operation to an 'all' operation below a
7473 		 * node bound to the driver.
7474 		 */
7475 		if ((major == DDI_MAJOR_T_NONE) ||
7476 		    (major == ddi_driver_major(dip)))
7477 			mcd->mtc_major = DDI_MAJOR_T_NONE;
7478 		else
7479 			mcd->mtc_major = major;
7480 
7481 		/*
7482 		 * The unconfig-driver to unconfig-all conversion above
7483 		 * constitutes an autodetach for NDI_DETACH_DRIVER calls,
7484 		 * set NDI_AUTODETACH.
7485 		 */
7486 		mcd->mtc_flags = hdl->mtc_flags;
7487 		if ((mcd->mtc_flags & NDI_DETACH_DRIVER) &&
7488 		    (hdl->mtc_op == MT_UNCONFIG_OP) &&
7489 		    (major == ddi_driver_major(pdip)))
7490 			mcd->mtc_flags |= NDI_AUTODETACH;
7491 
7492 		mutex_enter(&hdl->mtc_lock);
7493 		hdl->mtc_thr_count++;
7494 		mutex_exit(&hdl->mtc_lock);
7495 
7496 		/*
7497 		 * Add to end of list to process after ndi_devi_exit to avoid
7498 		 * locking differences depending on value of mtc_off.
7499 		 */
7500 		mcd->mtc_next = NULL;
7501 		if (mcd_head == NULL)
7502 			mcd_head = mcd;
7503 		else
7504 			mcd_tail->mtc_next = mcd;
7505 		mcd_tail = mcd;
7506 
7507 		dip = ddi_get_next_sibling(dip);
7508 	}
7509 	ndi_devi_exit(pdip, circ);
7510 
7511 	/* go through the list of held children */
7512 	for (mcd = mcd_head; mcd; mcd = mcd_head) {
7513 		mcd_head = mcd->mtc_next;
7514 		if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
7515 			mt_config_thread(mcd);
7516 		else
7517 			(void) thread_create(NULL, 0, mt_config_thread, mcd,
7518 			    0, &p0, TS_RUN, minclsyspri);
7519 	}
7520 }
7521 
7522 static void
7523 mt_config_driver(struct mt_config_handle *hdl)
7524 {
7525 	major_t			par_major = hdl->mtc_parmajor;
7526 	major_t			major = hdl->mtc_major;
7527 	struct devnames		*dnp = &devnamesp[par_major];
7528 	dev_info_t		*dip;
7529 	struct mt_config_data	*mcd_head = NULL;
7530 	struct mt_config_data	*mcd_tail = NULL;
7531 	struct mt_config_data	*mcd;
7532 #ifdef DEBUG
7533 	timestruc_t		end_time;
7534 
7535 	/* Update total_time in handle */
7536 	gethrestime(&end_time);
7537 	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
7538 #endif
7539 	ASSERT(par_major != DDI_MAJOR_T_NONE);
7540 	ASSERT(major != DDI_MAJOR_T_NONE);
7541 
7542 	LOCK_DEV_OPS(&dnp->dn_lock);
7543 	dip = devnamesp[par_major].dn_head;
7544 	while (dip) {
7545 		/*
7546 		 * Hold the child that we are processing so he does not get
7547 		 * removed. The corrisponding ndi_rele_devi() for children
7548 		 * that are not being skipped is done at the end of
7549 		 * mt_config_thread().
7550 		 */
7551 		ndi_hold_devi(dip);
7552 
7553 		/* skip leaf nodes and nodes not fully attached */
7554 		if (!i_ddi_devi_attached(dip) || is_leaf_node(dip)) {
7555 			ndi_rele_devi(dip);
7556 			dip = ddi_get_next(dip);
7557 			continue;
7558 		}
7559 
7560 		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
7561 		mcd->mtc_dip = dip;
7562 		mcd->mtc_hdl = hdl;
7563 		mcd->mtc_major = major;
7564 		mcd->mtc_flags = hdl->mtc_flags;
7565 
7566 		mutex_enter(&hdl->mtc_lock);
7567 		hdl->mtc_thr_count++;
7568 		mutex_exit(&hdl->mtc_lock);
7569 
7570 		/*
7571 		 * Add to end of list to process after UNLOCK_DEV_OPS to avoid
7572 		 * locking differences depending on value of mtc_off.
7573 		 */
7574 		mcd->mtc_next = NULL;
7575 		if (mcd_head == NULL)
7576 			mcd_head = mcd;
7577 		else
7578 			mcd_tail->mtc_next = mcd;
7579 		mcd_tail = mcd;
7580 
7581 		dip = ddi_get_next(dip);
7582 	}
7583 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7584 
7585 	/* go through the list of held children */
7586 	for (mcd = mcd_head; mcd; mcd = mcd_head) {
7587 		mcd_head = mcd->mtc_next;
7588 		if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
7589 			mt_config_thread(mcd);
7590 		else
7591 			(void) thread_create(NULL, 0, mt_config_thread, mcd,
7592 			    0, &p0, TS_RUN, minclsyspri);
7593 	}
7594 }
7595 
7596 /*
7597  * Given the nodeid for a persistent (PROM or SID) node, return
7598  * the corresponding devinfo node
7599  * NOTE: This function will return NULL for .conf nodeids.
7600  */
7601 dev_info_t *
7602 e_ddi_nodeid_to_dip(pnode_t nodeid)
7603 {
7604 	dev_info_t		*dip = NULL;
7605 	struct devi_nodeid	*prev, *elem;
7606 
7607 	mutex_enter(&devimap->dno_lock);
7608 
7609 	prev = NULL;
7610 	for (elem = devimap->dno_head; elem; elem = elem->next) {
7611 		if (elem->nodeid == nodeid) {
7612 			ndi_hold_devi(elem->dip);
7613 			dip = elem->dip;
7614 			break;
7615 		}
7616 		prev = elem;
7617 	}
7618 
7619 	/*
7620 	 * Move to head for faster lookup next time
7621 	 */
7622 	if (elem && prev) {
7623 		prev->next = elem->next;
7624 		elem->next = devimap->dno_head;
7625 		devimap->dno_head = elem;
7626 	}
7627 
7628 	mutex_exit(&devimap->dno_lock);
7629 	return (dip);
7630 }
7631 
7632 static void
7633 free_cache_task(void *arg)
7634 {
7635 	ASSERT(arg == NULL);
7636 
7637 	mutex_enter(&di_cache.cache_lock);
7638 
7639 	/*
7640 	 * The cache can be invalidated without holding the lock
7641 	 * but it can be made valid again only while the lock is held.
7642 	 * So if the cache is invalid when the lock is held, it will
7643 	 * stay invalid until lock is released.
7644 	 */
7645 	if (!di_cache.cache_valid)
7646 		i_ddi_di_cache_free(&di_cache);
7647 
7648 	mutex_exit(&di_cache.cache_lock);
7649 
7650 	if (di_cache_debug)
7651 		cmn_err(CE_NOTE, "system_taskq: di_cache freed");
7652 }
7653 
7654 extern int modrootloaded;
7655 
7656 void
7657 i_ddi_di_cache_free(struct di_cache *cache)
7658 {
7659 	int	error;
7660 	extern int sys_shutdown;
7661 
7662 	ASSERT(mutex_owned(&cache->cache_lock));
7663 
7664 	if (cache->cache_size) {
7665 		ASSERT(cache->cache_size > 0);
7666 		ASSERT(cache->cache_data);
7667 
7668 		kmem_free(cache->cache_data, cache->cache_size);
7669 		cache->cache_data = NULL;
7670 		cache->cache_size = 0;
7671 
7672 		if (di_cache_debug)
7673 			cmn_err(CE_NOTE, "i_ddi_di_cache_free: freed cachemem");
7674 	} else {
7675 		ASSERT(cache->cache_data == NULL);
7676 		if (di_cache_debug)
7677 			cmn_err(CE_NOTE, "i_ddi_di_cache_free: NULL cache");
7678 	}
7679 
7680 	if (!modrootloaded || rootvp == NULL ||
7681 	    vn_is_readonly(rootvp) || sys_shutdown) {
7682 		if (di_cache_debug) {
7683 			cmn_err(CE_WARN, "/ not mounted/RDONLY. Skip unlink");
7684 		}
7685 		return;
7686 	}
7687 
7688 	error = vn_remove(DI_CACHE_FILE, UIO_SYSSPACE, RMFILE);
7689 	if (di_cache_debug && error && error != ENOENT) {
7690 		cmn_err(CE_WARN, "%s: unlink failed: %d", DI_CACHE_FILE, error);
7691 	} else if (di_cache_debug && !error) {
7692 		cmn_err(CE_NOTE, "i_ddi_di_cache_free: unlinked cache file");
7693 	}
7694 }
7695 
7696 void
7697 i_ddi_di_cache_invalidate(int kmflag)
7698 {
7699 	int	cache_valid;
7700 
7701 	if (!modrootloaded || !i_ddi_io_initialized()) {
7702 		if (di_cache_debug)
7703 			cmn_err(CE_NOTE, "I/O not inited. Skipping invalidate");
7704 		return;
7705 	}
7706 
7707 	/* Increment devtree generation number. */
7708 	atomic_inc_ulong(&devtree_gen);
7709 
7710 	/* Invalidate the in-core cache and dispatch free on valid->invalid */
7711 	cache_valid = atomic_swap_uint(&di_cache.cache_valid, 0);
7712 	if (cache_valid) {
7713 		(void) taskq_dispatch(system_taskq, free_cache_task, NULL,
7714 		    (kmflag == KM_SLEEP) ? TQ_SLEEP : TQ_NOSLEEP);
7715 	}
7716 
7717 	if (di_cache_debug) {
7718 		cmn_err(CE_NOTE, "invalidation with km_flag: %s",
7719 		    kmflag == KM_SLEEP ? "KM_SLEEP" : "KM_NOSLEEP");
7720 	}
7721 }
7722 
7723 
7724 static void
7725 i_bind_vhci_node(dev_info_t *dip)
7726 {
7727 	DEVI(dip)->devi_major = ddi_name_to_major(ddi_node_name(dip));
7728 	i_ddi_set_node_state(dip, DS_BOUND);
7729 }
7730 
7731 static char vhci_node_addr[2];
7732 
7733 static int
7734 i_init_vhci_node(dev_info_t *dip)
7735 {
7736 	add_global_props(dip);
7737 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
7738 	if (DEVI(dip)->devi_ops == NULL)
7739 		return (-1);
7740 
7741 	DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
7742 	e_ddi_keep_instance(dip);
7743 	vhci_node_addr[0]	= '\0';
7744 	ddi_set_name_addr(dip, vhci_node_addr);
7745 	i_ddi_set_node_state(dip, DS_INITIALIZED);
7746 	return (0);
7747 }
7748 
7749 static void
7750 i_link_vhci_node(dev_info_t *dip)
7751 {
7752 	ASSERT(MUTEX_HELD(&global_vhci_lock));
7753 
7754 	/*
7755 	 * scsi_vhci should be kept left most of the device tree.
7756 	 */
7757 	if (scsi_vhci_dip) {
7758 		DEVI(dip)->devi_sibling = DEVI(scsi_vhci_dip)->devi_sibling;
7759 		DEVI(scsi_vhci_dip)->devi_sibling = DEVI(dip);
7760 	} else {
7761 		DEVI(dip)->devi_sibling = DEVI(top_devinfo)->devi_child;
7762 		DEVI(top_devinfo)->devi_child = DEVI(dip);
7763 	}
7764 }
7765 
7766 
7767 /*
7768  * This a special routine to enumerate vhci node (child of rootnex
7769  * node) without holding the ndi_devi_enter() lock. The device node
7770  * is allocated, initialized and brought into DS_READY state before
7771  * inserting into the device tree. The VHCI node is handcrafted
7772  * here to bring the node to DS_READY, similar to rootnex node.
7773  *
7774  * The global_vhci_lock protects linking the node into the device
7775  * as same lock is held before linking/unlinking any direct child
7776  * of rootnex children.
7777  *
7778  * This routine is a workaround to handle a possible deadlock
7779  * that occurs while trying to enumerate node in a different sub-tree
7780  * during _init/_attach entry points.
7781  */
7782 /*ARGSUSED*/
7783 dev_info_t *
7784 ndi_devi_config_vhci(char *drvname, int flags)
7785 {
7786 	struct devnames		*dnp;
7787 	dev_info_t		*dip;
7788 	major_t			major = ddi_name_to_major(drvname);
7789 
7790 	if (major == -1)
7791 		return (NULL);
7792 
7793 	/* Make sure we create the VHCI node only once */
7794 	dnp = &devnamesp[major];
7795 	LOCK_DEV_OPS(&dnp->dn_lock);
7796 	if (dnp->dn_head) {
7797 		dip = dnp->dn_head;
7798 		UNLOCK_DEV_OPS(&dnp->dn_lock);
7799 		return (dip);
7800 	}
7801 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7802 
7803 	/* Allocate the VHCI node */
7804 	ndi_devi_alloc_sleep(top_devinfo, drvname, DEVI_SID_NODEID, &dip);
7805 	ndi_hold_devi(dip);
7806 
7807 	/* Mark the node as VHCI */
7808 	DEVI(dip)->devi_node_attributes |= DDI_VHCI_NODE;
7809 
7810 	i_ddi_add_devimap(dip);
7811 	i_bind_vhci_node(dip);
7812 	if (i_init_vhci_node(dip) == -1) {
7813 		ndi_rele_devi(dip);
7814 		(void) ndi_devi_free(dip);
7815 		return (NULL);
7816 	}
7817 
7818 	mutex_enter(&(DEVI(dip)->devi_lock));
7819 	DEVI_SET_ATTACHING(dip);
7820 	mutex_exit(&(DEVI(dip)->devi_lock));
7821 
7822 	if (devi_attach(dip, DDI_ATTACH) != DDI_SUCCESS) {
7823 		cmn_err(CE_CONT, "Could not attach %s driver", drvname);
7824 		e_ddi_free_instance(dip, vhci_node_addr);
7825 		ndi_rele_devi(dip);
7826 		(void) ndi_devi_free(dip);
7827 		return (NULL);
7828 	}
7829 	mutex_enter(&(DEVI(dip)->devi_lock));
7830 	DEVI_CLR_ATTACHING(dip);
7831 	mutex_exit(&(DEVI(dip)->devi_lock));
7832 
7833 	mutex_enter(&global_vhci_lock);
7834 	i_link_vhci_node(dip);
7835 	mutex_exit(&global_vhci_lock);
7836 	i_ddi_set_node_state(dip, DS_READY);
7837 
7838 	LOCK_DEV_OPS(&dnp->dn_lock);
7839 	dnp->dn_flags |= DN_DRIVER_HELD;
7840 	dnp->dn_head = dip;
7841 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7842 
7843 	i_ndi_devi_report_status_change(dip, NULL);
7844 
7845 	return (dip);
7846 }
7847 
7848 /*
7849  * ibt_hw_is_present() returns 0 when there is no IB hardware actively
7850  * running.  This is primarily useful for modules like rpcmod which
7851  * needs a quick check to decide whether or not it should try to use
7852  * InfiniBand
7853  */
7854 int ib_hw_status = 0;
7855 int
7856 ibt_hw_is_present()
7857 {
7858 	return (ib_hw_status);
7859 }
7860 
7861 /*
7862  * ASSERT that constraint flag is not set and then set the "retire attempt"
7863  * flag.
7864  */
7865 int
7866 e_ddi_mark_retiring(dev_info_t *dip, void *arg)
7867 {
7868 	char	**cons_array = (char **)arg;
7869 	char	*path;
7870 	int	constraint;
7871 	int	i;
7872 
7873 	constraint = 0;
7874 	if (cons_array) {
7875 		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7876 		(void) ddi_pathname(dip, path);
7877 		for (i = 0; cons_array[i] != NULL; i++) {
7878 			if (strcmp(path, cons_array[i]) == 0) {
7879 				constraint = 1;
7880 				break;
7881 			}
7882 		}
7883 		kmem_free(path, MAXPATHLEN);
7884 	}
7885 
7886 	mutex_enter(&DEVI(dip)->devi_lock);
7887 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
7888 	DEVI(dip)->devi_flags |= DEVI_RETIRING;
7889 	if (constraint)
7890 		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
7891 	mutex_exit(&DEVI(dip)->devi_lock);
7892 
7893 	RIO_VERBOSE((CE_NOTE, "marked dip as undergoing retire process dip=%p",
7894 	    (void *)dip));
7895 
7896 	if (constraint)
7897 		RIO_DEBUG((CE_NOTE, "marked dip as constrained, dip=%p",
7898 		    (void *)dip));
7899 
7900 	if (MDI_PHCI(dip))
7901 		mdi_phci_mark_retiring(dip, cons_array);
7902 
7903 	return (DDI_WALK_CONTINUE);
7904 }
7905 
7906 static void
7907 free_array(char **cons_array)
7908 {
7909 	int	i;
7910 
7911 	if (cons_array == NULL)
7912 		return;
7913 
7914 	for (i = 0; cons_array[i] != NULL; i++) {
7915 		kmem_free(cons_array[i], strlen(cons_array[i]) + 1);
7916 	}
7917 	kmem_free(cons_array, (i+1) * sizeof (char *));
7918 }
7919 
7920 /*
7921  * Walk *every* node in subtree and check if it blocks, allows or has no
7922  * comment on a proposed retire.
7923  */
7924 int
7925 e_ddi_retire_notify(dev_info_t *dip, void *arg)
7926 {
7927 	int	*constraint = (int *)arg;
7928 
7929 	RIO_DEBUG((CE_NOTE, "retire notify: dip = %p", (void *)dip));
7930 
7931 	(void) e_ddi_offline_notify(dip);
7932 
7933 	mutex_enter(&(DEVI(dip)->devi_lock));
7934 	if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) {
7935 		RIO_DEBUG((CE_WARN, "retire notify: dip in retire "
7936 		    "subtree is not marked: dip = %p", (void *)dip));
7937 		*constraint = 0;
7938 	} else if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) {
7939 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
7940 		RIO_DEBUG((CE_NOTE, "retire notify: BLOCKED: dip = %p",
7941 		    (void *)dip));
7942 		*constraint = 0;
7943 	} else if (!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT)) {
7944 		RIO_DEBUG((CE_NOTE, "retire notify: NO CONSTRAINT: "
7945 		    "dip = %p", (void *)dip));
7946 		*constraint = 0;
7947 	} else {
7948 		RIO_DEBUG((CE_NOTE, "retire notify: CONSTRAINT set: "
7949 		    "dip = %p", (void *)dip));
7950 	}
7951 	mutex_exit(&DEVI(dip)->devi_lock);
7952 
7953 	if (MDI_PHCI(dip))
7954 		mdi_phci_retire_notify(dip, constraint);
7955 
7956 	return (DDI_WALK_CONTINUE);
7957 }
7958 
7959 int
7960 e_ddi_retire_finalize(dev_info_t *dip, void *arg)
7961 {
7962 	int constraint = *(int *)arg;
7963 	int finalize;
7964 	int phci_only;
7965 
7966 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
7967 
7968 	mutex_enter(&DEVI(dip)->devi_lock);
7969 	if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) {
7970 		RIO_DEBUG((CE_WARN,
7971 		    "retire: unmarked dip(%p) in retire subtree",
7972 		    (void *)dip));
7973 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRED));
7974 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
7975 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
7976 		mutex_exit(&DEVI(dip)->devi_lock);
7977 		return (DDI_WALK_CONTINUE);
7978 	}
7979 
7980 	/*
7981 	 * retire the device if constraints have been applied
7982 	 * or if the device is not in use
7983 	 */
7984 	finalize = 0;
7985 	if (constraint) {
7986 		ASSERT(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT);
7987 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
7988 		DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
7989 		DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
7990 		DEVI(dip)->devi_flags |= DEVI_RETIRED;
7991 		mutex_exit(&DEVI(dip)->devi_lock);
7992 		(void) spec_fence_snode(dip, NULL);
7993 		RIO_DEBUG((CE_NOTE, "Fenced off: dip = %p", (void *)dip));
7994 		e_ddi_offline_finalize(dip, DDI_SUCCESS);
7995 	} else {
7996 		if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) {
7997 			ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
7998 			DEVI(dip)->devi_flags &= ~DEVI_R_BLOCKED;
7999 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
8000 			/* we have already finalized during notify */
8001 		} else if (DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT) {
8002 			DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
8003 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
8004 			finalize = 1;
8005 		} else {
8006 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
8007 			/*
8008 			 * even if no contracts, need to call finalize
8009 			 * to clear the contract barrier on the dip
8010 			 */
8011 			finalize = 1;
8012 		}
8013 		mutex_exit(&DEVI(dip)->devi_lock);
8014 		RIO_DEBUG((CE_NOTE, "finalize: NOT retired: dip = %p",
8015 		    (void *)dip));
8016 		if (finalize)
8017 			e_ddi_offline_finalize(dip, DDI_FAILURE);
8018 	}
8019 
8020 	/*
8021 	 * phci_only variable indicates no client checking, just
8022 	 * offline the PHCI. We set that to 0 to enable client
8023 	 * checking
8024 	 */
8025 	phci_only = 0;
8026 	if (MDI_PHCI(dip))
8027 		mdi_phci_retire_finalize(dip, phci_only);
8028 
8029 	return (DDI_WALK_CONTINUE);
8030 }
8031 
8032 /*
8033  * Returns
8034  * 	DDI_SUCCESS if constraints allow retire
8035  *	DDI_FAILURE if constraints don't allow retire.
8036  * cons_array is a NULL terminated array of node paths for
8037  * which constraints have already been applied.
8038  */
8039 int
8040 e_ddi_retire_device(char *path, char **cons_array)
8041 {
8042 	dev_info_t	*dip;
8043 	dev_info_t	*pdip;
8044 	int		circ;
8045 	int		circ2;
8046 	int		constraint;
8047 	char		*devnm;
8048 
8049 	/*
8050 	 * First, lookup the device
8051 	 */
8052 	dip = e_ddi_hold_devi_by_path(path, 0);
8053 	if (dip == NULL) {
8054 		/*
8055 		 * device does not exist. This device cannot be
8056 		 * a critical device since it is not in use. Thus
8057 		 * this device is always retireable. Return DDI_SUCCESS
8058 		 * to indicate this. If this device is ever
8059 		 * instantiated, I/O framework will consult the
8060 		 * the persistent retire store, mark it as
8061 		 * retired and fence it off.
8062 		 */
8063 		RIO_DEBUG((CE_NOTE, "Retire device: device doesn't exist."
8064 		    " NOP. Just returning SUCCESS. path=%s", path));
8065 		free_array(cons_array);
8066 		return (DDI_SUCCESS);
8067 	}
8068 
8069 	RIO_DEBUG((CE_NOTE, "Retire device: found dip = %p.", (void *)dip));
8070 
8071 	pdip = ddi_get_parent(dip);
8072 	ndi_hold_devi(pdip);
8073 
8074 	/*
8075 	 * Run devfs_clean() in case dip has no constraints and is
8076 	 * not in use, so is retireable but there are dv_nodes holding
8077 	 * ref-count on the dip. Note that devfs_clean() always returns
8078 	 * success.
8079 	 */
8080 	devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
8081 	(void) ddi_deviname(dip, devnm);
8082 	(void) devfs_clean(pdip, devnm + 1, DV_CLEAN_FORCE);
8083 	kmem_free(devnm, MAXNAMELEN + 1);
8084 
8085 	ndi_devi_enter(pdip, &circ);
8086 
8087 	/* release hold from e_ddi_hold_devi_by_path */
8088 	ndi_rele_devi(dip);
8089 
8090 	/*
8091 	 * If it cannot make a determination, is_leaf_node() assumes
8092 	 * dip is a nexus.
8093 	 */
8094 	(void) e_ddi_mark_retiring(dip, cons_array);
8095 	if (!is_leaf_node(dip)) {
8096 		ndi_devi_enter(dip, &circ2);
8097 		ddi_walk_devs(ddi_get_child(dip), e_ddi_mark_retiring,
8098 		    cons_array);
8099 		ndi_devi_exit(dip, circ2);
8100 	}
8101 	free_array(cons_array);
8102 
8103 	/*
8104 	 * apply constraints
8105 	 */
8106 	RIO_DEBUG((CE_NOTE, "retire: subtree retire notify: path = %s", path));
8107 
8108 	constraint = 1;	/* assume constraints allow retire */
8109 	(void) e_ddi_retire_notify(dip, &constraint);
8110 	if (!is_leaf_node(dip)) {
8111 		ndi_devi_enter(dip, &circ2);
8112 		ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_notify,
8113 		    &constraint);
8114 		ndi_devi_exit(dip, circ2);
8115 	}
8116 
8117 	/*
8118 	 * Now finalize the retire
8119 	 */
8120 	(void) e_ddi_retire_finalize(dip, &constraint);
8121 	if (!is_leaf_node(dip)) {
8122 		ndi_devi_enter(dip, &circ2);
8123 		ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_finalize,
8124 		    &constraint);
8125 		ndi_devi_exit(dip, circ2);
8126 	}
8127 
8128 	if (!constraint) {
8129 		RIO_DEBUG((CE_WARN, "retire failed: path = %s", path));
8130 	} else {
8131 		RIO_DEBUG((CE_NOTE, "retire succeeded: path = %s", path));
8132 	}
8133 
8134 	ndi_devi_exit(pdip, circ);
8135 	ndi_rele_devi(pdip);
8136 	return (constraint ? DDI_SUCCESS : DDI_FAILURE);
8137 }
8138 
8139 static int
8140 unmark_and_unfence(dev_info_t *dip, void *arg)
8141 {
8142 	char	*path = (char *)arg;
8143 
8144 	ASSERT(path);
8145 
8146 	(void) ddi_pathname(dip, path);
8147 
8148 	mutex_enter(&DEVI(dip)->devi_lock);
8149 	DEVI(dip)->devi_flags &= ~DEVI_RETIRED;
8150 	DEVI_SET_DEVICE_ONLINE(dip);
8151 	mutex_exit(&DEVI(dip)->devi_lock);
8152 
8153 	RIO_VERBOSE((CE_NOTE, "Cleared RETIRED flag: dip=%p, path=%s",
8154 	    (void *)dip, path));
8155 
8156 	(void) spec_unfence_snode(dip);
8157 	RIO_DEBUG((CE_NOTE, "Unfenced device: %s", path));
8158 
8159 	if (MDI_PHCI(dip))
8160 		mdi_phci_unretire(dip);
8161 
8162 	return (DDI_WALK_CONTINUE);
8163 }
8164 
8165 struct find_dip {
8166 	char	*fd_buf;
8167 	char	*fd_path;
8168 	dev_info_t *fd_dip;
8169 };
8170 
8171 static int
8172 find_dip_fcn(dev_info_t *dip, void *arg)
8173 {
8174 	struct find_dip *findp = (struct find_dip *)arg;
8175 
8176 	(void) ddi_pathname(dip, findp->fd_buf);
8177 
8178 	if (strcmp(findp->fd_path, findp->fd_buf) != 0)
8179 		return (DDI_WALK_CONTINUE);
8180 
8181 	ndi_hold_devi(dip);
8182 	findp->fd_dip = dip;
8183 
8184 	return (DDI_WALK_TERMINATE);
8185 }
8186 
8187 int
8188 e_ddi_unretire_device(char *path)
8189 {
8190 	int		circ;
8191 	int		circ2;
8192 	char		*path2;
8193 	dev_info_t	*pdip;
8194 	dev_info_t	*dip;
8195 	struct find_dip	 find_dip;
8196 
8197 	ASSERT(path);
8198 	ASSERT(*path == '/');
8199 
8200 	if (strcmp(path, "/") == 0) {
8201 		cmn_err(CE_WARN, "Root node cannot be retired. Skipping "
8202 		    "device unretire: %s", path);
8203 		return (0);
8204 	}
8205 
8206 	/*
8207 	 * We can't lookup the dip (corresponding to path) via
8208 	 * e_ddi_hold_devi_by_path() because the dip may be offline
8209 	 * and may not attach. Use ddi_walk_devs() instead;
8210 	 */
8211 	find_dip.fd_buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8212 	find_dip.fd_path = path;
8213 	find_dip.fd_dip = NULL;
8214 
8215 	pdip = ddi_root_node();
8216 
8217 	ndi_devi_enter(pdip, &circ);
8218 	ddi_walk_devs(ddi_get_child(pdip), find_dip_fcn, &find_dip);
8219 	ndi_devi_exit(pdip, circ);
8220 
8221 	kmem_free(find_dip.fd_buf, MAXPATHLEN);
8222 
8223 	if (find_dip.fd_dip == NULL) {
8224 		cmn_err(CE_WARN, "Device not found in device tree. Skipping "
8225 		    "device unretire: %s", path);
8226 		return (0);
8227 	}
8228 
8229 	dip = find_dip.fd_dip;
8230 
8231 	pdip = ddi_get_parent(dip);
8232 
8233 	ndi_hold_devi(pdip);
8234 
8235 	ndi_devi_enter(pdip, &circ);
8236 
8237 	path2 = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8238 
8239 	(void) unmark_and_unfence(dip, path2);
8240 	if (!is_leaf_node(dip)) {
8241 		ndi_devi_enter(dip, &circ2);
8242 		ddi_walk_devs(ddi_get_child(dip), unmark_and_unfence, path2);
8243 		ndi_devi_exit(dip, circ2);
8244 	}
8245 
8246 	kmem_free(path2, MAXPATHLEN);
8247 
8248 	/* release hold from find_dip_fcn() */
8249 	ndi_rele_devi(dip);
8250 
8251 	ndi_devi_exit(pdip, circ);
8252 
8253 	ndi_rele_devi(pdip);
8254 
8255 	return (0);
8256 }
8257 
8258 /*
8259  * Called before attach on a dip that has been retired.
8260  */
8261 static int
8262 mark_and_fence(dev_info_t *dip, void *arg)
8263 {
8264 	char    *fencepath = (char *)arg;
8265 
8266 	/*
8267 	 * We have already decided to retire this device. The various
8268 	 * constraint checking should not be set.
8269 	 * NOTE that the retire flag may already be set due to
8270 	 * fenced -> detach -> fenced transitions.
8271 	 */
8272 	mutex_enter(&DEVI(dip)->devi_lock);
8273 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
8274 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
8275 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRING));
8276 	DEVI(dip)->devi_flags |= DEVI_RETIRED;
8277 	mutex_exit(&DEVI(dip)->devi_lock);
8278 	RIO_VERBOSE((CE_NOTE, "marked as RETIRED dip=%p", (void *)dip));
8279 
8280 	if (fencepath) {
8281 		(void) spec_fence_snode(dip, NULL);
8282 		RIO_DEBUG((CE_NOTE, "Fenced: %s",
8283 		    ddi_pathname(dip, fencepath)));
8284 	}
8285 
8286 	return (DDI_WALK_CONTINUE);
8287 }
8288 
8289 /*
8290  * Checks the retire database and:
8291  *
8292  * - if device is present in the retire database, marks the device retired
8293  *   and fences it off.
8294  * - if device is not in retire database, allows the device to attach normally
8295  *
8296  * To be called only by framework attach code on first attach attempt.
8297  *
8298  */
8299 static void
8300 i_ddi_check_retire(dev_info_t *dip)
8301 {
8302 	char		*path;
8303 	dev_info_t	*pdip;
8304 	int		circ;
8305 	int		phci_only;
8306 
8307 	pdip = ddi_get_parent(dip);
8308 
8309 	/*
8310 	 * Root dip is treated special and doesn't take this code path.
8311 	 * Also root can never be retired.
8312 	 */
8313 	ASSERT(pdip);
8314 	ASSERT(DEVI_BUSY_OWNED(pdip));
8315 	ASSERT(i_ddi_node_state(dip) < DS_ATTACHED);
8316 
8317 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8318 
8319 	(void) ddi_pathname(dip, path);
8320 
8321 	RIO_VERBOSE((CE_NOTE, "Checking if dip should attach: dip=%p, path=%s",
8322 	    (void *)dip, path));
8323 
8324 	/*
8325 	 * Check if this device is in the "retired" store i.e.  should
8326 	 * be retired. If not, we have nothing to do.
8327 	 */
8328 	if (e_ddi_device_retired(path) == 0) {
8329 		RIO_VERBOSE((CE_NOTE, "device is NOT retired: path=%s", path));
8330 		kmem_free(path, MAXPATHLEN);
8331 		return;
8332 	}
8333 
8334 	RIO_DEBUG((CE_NOTE, "attach: device is retired: path=%s", path));
8335 
8336 	/*
8337 	 * Mark dips and fence off snodes (if any)
8338 	 */
8339 	RIO_DEBUG((CE_NOTE, "attach: Mark and fence subtree: path=%s", path));
8340 	(void) mark_and_fence(dip, path);
8341 	if (!is_leaf_node(dip)) {
8342 		ndi_devi_enter(dip, &circ);
8343 		ddi_walk_devs(ddi_get_child(dip), mark_and_fence, path);
8344 		ndi_devi_exit(dip, circ);
8345 	}
8346 
8347 	kmem_free(path, MAXPATHLEN);
8348 
8349 	/*
8350 	 * We don't want to check the client. We just want to
8351 	 * offline the PHCI
8352 	 */
8353 	phci_only = 1;
8354 	if (MDI_PHCI(dip))
8355 		mdi_phci_retire_finalize(dip, phci_only);
8356 }
8357