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