xref: /titanic_51/usr/src/uts/common/os/devcfg.c (revision 7f0b8309074a5d8e9f9d8ffe7aad7bb0b1ee6b1f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/note.h>
27 #include <sys/t_lock.h>
28 #include <sys/cmn_err.h>
29 #include <sys/instance.h>
30 #include <sys/conf.h>
31 #include <sys/stat.h>
32 #include <sys/ddi.h>
33 #include <sys/hwconf.h>
34 #include <sys/sunddi.h>
35 #include <sys/sunndi.h>
36 #include <sys/ddi_impldefs.h>
37 #include <sys/ndi_impldefs.h>
38 #include <sys/modctl.h>
39 #include <sys/contract/device_impl.h>
40 #include <sys/dacf.h>
41 #include <sys/promif.h>
42 #include <sys/pci.h>
43 #include <sys/cpuvar.h>
44 #include <sys/pathname.h>
45 #include <sys/taskq.h>
46 #include <sys/sysevent.h>
47 #include <sys/sunmdi.h>
48 #include <sys/stream.h>
49 #include <sys/strsubr.h>
50 #include <sys/fs/snode.h>
51 #include <sys/fs/dv_node.h>
52 #include <sys/reboot.h>
53 #include <sys/sysmacros.h>
54 #include <sys/systm.h>
55 #include <sys/sunldi.h>
56 #include <sys/sunldi_impl.h>
57 
58 #if defined(__i386) || defined(__amd64)
59 #if !defined(__xpv)
60 #include <sys/iommulib.h>
61 #endif
62 #endif
63 
64 #ifdef DEBUG
65 int ddidebug = DDI_AUDIT;
66 #else
67 int ddidebug = 0;
68 #endif
69 
70 #define	MT_CONFIG_OP	0
71 #define	MT_UNCONFIG_OP	1
72 
73 /* Multi-threaded configuration */
74 struct mt_config_handle {
75 	kmutex_t mtc_lock;
76 	kcondvar_t mtc_cv;
77 	int mtc_thr_count;
78 	dev_info_t *mtc_pdip;	/* parent dip for mt_config_children */
79 	dev_info_t **mtc_fdip;	/* "a" dip where unconfigure failed */
80 	major_t mtc_parmajor;	/* parent major for mt_config_driver */
81 	major_t mtc_major;
82 	int mtc_flags;
83 	int mtc_op;		/* config or unconfig */
84 	int mtc_error;		/* operation error */
85 	struct brevq_node **mtc_brevqp;	/* outstanding branch events queue */
86 #ifdef DEBUG
87 	int total_time;
88 	timestruc_t start_time;
89 #endif /* DEBUG */
90 };
91 
92 struct devi_nodeid {
93 	pnode_t nodeid;
94 	dev_info_t *dip;
95 	struct devi_nodeid *next;
96 };
97 
98 struct devi_nodeid_list {
99 	kmutex_t dno_lock;		/* Protects other fields */
100 	struct devi_nodeid *dno_head;	/* list of devi nodeid elements */
101 	struct devi_nodeid *dno_free;	/* Free list */
102 	uint_t dno_list_length;		/* number of dips in list */
103 };
104 
105 /* used to keep track of branch remove events to be generated */
106 struct brevq_node {
107 	char *brn_deviname;
108 	struct brevq_node *brn_sibling;
109 	struct brevq_node *brn_child;
110 };
111 
112 static struct devi_nodeid_list devi_nodeid_list;
113 static struct devi_nodeid_list *devimap = &devi_nodeid_list;
114 
115 /*
116  * Well known nodes which are attached first at boot time.
117  */
118 dev_info_t *top_devinfo;		/* root of device tree */
119 dev_info_t *options_dip;
120 dev_info_t *pseudo_dip;
121 dev_info_t *clone_dip;
122 dev_info_t *scsi_vhci_dip;		/* MPXIO dip */
123 major_t clone_major;
124 
125 /*
126  * A non-global zone's /dev is derived from the device tree.
127  * This generation number serves to indicate when a zone's
128  * /dev may need to be updated.
129  */
130 volatile ulong_t devtree_gen;		/* generation number */
131 
132 /* block all future dev_info state changes */
133 hrtime_t volatile devinfo_freeze = 0;
134 
135 /* number of dev_info attaches/detaches currently in progress */
136 static ulong_t devinfo_attach_detach = 0;
137 
138 extern int	sys_shutdown;
139 extern kmutex_t global_vhci_lock;
140 
141 /* bitset of DS_SYSAVAIL & DS_RECONFIG - no races, no lock */
142 static int devname_state = 0;
143 
144 /*
145  * The devinfo snapshot cache and related variables.
146  * The only field in the di_cache structure that needs initialization
147  * is the mutex (cache_lock). However, since this is an adaptive mutex
148  * (MUTEX_DEFAULT) - it is automatically initialized by being allocated
149  * in zeroed memory (static storage class). Therefore no explicit
150  * initialization of the di_cache structure is needed.
151  */
152 struct di_cache	di_cache = {1};
153 int		di_cache_debug = 0;
154 
155 /* For ddvis, which needs pseudo children under PCI */
156 int pci_allow_pseudo_children = 0;
157 
158 /* Allow path-oriented alias driver binding on driver.conf enumerated nodes */
159 int driver_conf_allow_path_alias = 1;
160 
161 /*
162  * The following switch is for service people, in case a
163  * 3rd party driver depends on identify(9e) being called.
164  */
165 int identify_9e = 0;
166 
167 int mtc_off;					/* turn off mt config */
168 
169 int quiesce_debug = 0;
170 
171 static kmem_cache_t *ddi_node_cache;		/* devinfo node cache */
172 static devinfo_log_header_t *devinfo_audit_log;	/* devinfo log */
173 static int devinfo_log_size;			/* size in pages */
174 
175 static int lookup_compatible(dev_info_t *, uint_t);
176 static char *encode_composite_string(char **, uint_t, size_t *, uint_t);
177 static void link_to_driver_list(dev_info_t *);
178 static void unlink_from_driver_list(dev_info_t *);
179 static void add_to_dn_list(struct devnames *, dev_info_t *);
180 static void remove_from_dn_list(struct devnames *, dev_info_t *);
181 static dev_info_t *find_child_by_callback(dev_info_t *, char *, char *,
182     int (*)(dev_info_t *, char *, int));
183 static dev_info_t *find_duplicate_child();
184 static void add_global_props(dev_info_t *);
185 static void remove_global_props(dev_info_t *);
186 static int uninit_node(dev_info_t *);
187 static void da_log_init(void);
188 static void da_log_enter(dev_info_t *);
189 static int walk_devs(dev_info_t *, int (*f)(dev_info_t *, void *), void *, int);
190 static int reset_nexus_flags(dev_info_t *, void *);
191 static void ddi_optimize_dtree(dev_info_t *);
192 static int is_leaf_node(dev_info_t *);
193 static struct mt_config_handle *mt_config_init(dev_info_t *, dev_info_t **,
194     int, major_t, int, struct brevq_node **);
195 static void mt_config_children(struct mt_config_handle *);
196 static void mt_config_driver(struct mt_config_handle *);
197 static int mt_config_fini(struct mt_config_handle *);
198 static int devi_unconfig_common(dev_info_t *, dev_info_t **, int, major_t,
199     struct brevq_node **);
200 static int
201 ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
202     dev_info_t **childp, int flags);
203 static void i_link_vhci_node(dev_info_t *);
204 static void ndi_devi_exit_and_wait(dev_info_t *dip,
205     int circular, clock_t end_time);
206 static int ndi_devi_unbind_driver(dev_info_t *dip);
207 
208 static void i_ddi_check_retire(dev_info_t *dip);
209 
210 static void quiesce_one_device(dev_info_t *, void *);
211 
212 /*
213  * dev_info cache and node management
214  */
215 
216 /* initialize dev_info node cache */
217 void
218 i_ddi_node_cache_init()
219 {
220 	ASSERT(ddi_node_cache == NULL);
221 	ddi_node_cache = kmem_cache_create("dev_info_node_cache",
222 	    sizeof (struct dev_info), 0, NULL, NULL, NULL, NULL, NULL, 0);
223 
224 	if (ddidebug & DDI_AUDIT)
225 		da_log_init();
226 }
227 
228 /*
229  * Allocating a dev_info node, callable from interrupt context with KM_NOSLEEP
230  * The allocated node has a reference count of 0.
231  */
232 dev_info_t *
233 i_ddi_alloc_node(dev_info_t *pdip, char *node_name, pnode_t nodeid,
234     int instance, ddi_prop_t *sys_prop, int flag)
235 {
236 	struct dev_info *devi;
237 	struct devi_nodeid *elem;
238 	static char failed[] = "i_ddi_alloc_node: out of memory";
239 
240 	ASSERT(node_name != NULL);
241 
242 	if ((devi = kmem_cache_alloc(ddi_node_cache, flag)) == NULL) {
243 		cmn_err(CE_NOTE, failed);
244 		return (NULL);
245 	}
246 
247 	bzero(devi, sizeof (struct dev_info));
248 
249 	if (devinfo_audit_log) {
250 		devi->devi_audit = kmem_zalloc(sizeof (devinfo_audit_t), flag);
251 		if (devi->devi_audit == NULL)
252 			goto fail;
253 	}
254 
255 	if ((devi->devi_node_name = i_ddi_strdup(node_name, flag)) == NULL)
256 		goto fail;
257 
258 	/* default binding name is node name */
259 	devi->devi_binding_name = devi->devi_node_name;
260 	devi->devi_major = DDI_MAJOR_T_NONE;	/* unbound by default */
261 
262 	/*
263 	 * Make a copy of system property
264 	 */
265 	if (sys_prop &&
266 	    (devi->devi_sys_prop_ptr = i_ddi_prop_list_dup(sys_prop, flag))
267 	    == NULL)
268 		goto fail;
269 
270 	/*
271 	 * Assign devi_nodeid, devi_node_class, devi_node_attributes
272 	 * according to the following algorithm:
273 	 *
274 	 * nodeid arg			node class		node attributes
275 	 *
276 	 * DEVI_PSEUDO_NODEID		DDI_NC_PSEUDO		A
277 	 * DEVI_SID_NODEID		DDI_NC_PSEUDO		A,P
278 	 * other			DDI_NC_PROM		P
279 	 *
280 	 * Where A = DDI_AUTO_ASSIGNED_NODEID (auto-assign a nodeid)
281 	 * and	 P = DDI_PERSISTENT
282 	 *
283 	 * auto-assigned nodeids are also auto-freed.
284 	 */
285 	switch (nodeid) {
286 	case DEVI_SID_NODEID:
287 		devi->devi_node_attributes = DDI_PERSISTENT;
288 		if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL)
289 			goto fail;
290 		/*FALLTHROUGH*/
291 	case DEVI_PSEUDO_NODEID:
292 		devi->devi_node_attributes |= DDI_AUTO_ASSIGNED_NODEID;
293 		devi->devi_node_class = DDI_NC_PSEUDO;
294 		if (impl_ddi_alloc_nodeid(&devi->devi_nodeid)) {
295 			panic("i_ddi_alloc_node: out of nodeids");
296 			/*NOTREACHED*/
297 		}
298 		break;
299 	default:
300 		if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL)
301 			goto fail;
302 		/*
303 		 * the nodetype is 'prom', try to 'take' the nodeid now.
304 		 * This requires memory allocation, so check for failure.
305 		 */
306 		if (impl_ddi_take_nodeid(nodeid, flag) != 0) {
307 			kmem_free(elem, sizeof (*elem));
308 			goto fail;
309 		}
310 
311 		devi->devi_nodeid = nodeid;
312 		devi->devi_node_class = DDI_NC_PROM;
313 		devi->devi_node_attributes = DDI_PERSISTENT;
314 
315 	}
316 
317 	if (ndi_dev_is_persistent_node((dev_info_t *)devi)) {
318 		mutex_enter(&devimap->dno_lock);
319 		elem->next = devimap->dno_free;
320 		devimap->dno_free = elem;
321 		mutex_exit(&devimap->dno_lock);
322 	}
323 
324 	/*
325 	 * Instance is normally initialized to -1. In a few special
326 	 * cases, the caller may specify an instance (e.g. CPU nodes).
327 	 */
328 	devi->devi_instance = instance;
329 
330 	/*
331 	 * set parent and bus_ctl parent
332 	 */
333 	devi->devi_parent = DEVI(pdip);
334 	devi->devi_bus_ctl = DEVI(pdip);
335 
336 	NDI_CONFIG_DEBUG((CE_CONT,
337 	    "i_ddi_alloc_node: name=%s id=%d\n", node_name, devi->devi_nodeid));
338 
339 	cv_init(&(devi->devi_cv), NULL, CV_DEFAULT, NULL);
340 	mutex_init(&(devi->devi_lock), NULL, MUTEX_DEFAULT, NULL);
341 	mutex_init(&(devi->devi_pm_lock), NULL, MUTEX_DEFAULT, NULL);
342 	mutex_init(&(devi->devi_pm_busy_lock), NULL, MUTEX_DEFAULT, NULL);
343 
344 	RIO_TRACE((CE_NOTE, "i_ddi_alloc_node: Initing contract fields: "
345 	    "dip=%p, name=%s", (void *)devi, node_name));
346 
347 	mutex_init(&(devi->devi_ct_lock), NULL, MUTEX_DEFAULT, NULL);
348 	cv_init(&(devi->devi_ct_cv), NULL, CV_DEFAULT, NULL);
349 	devi->devi_ct_count = -1;	/* counter not in use if -1 */
350 	list_create(&(devi->devi_ct), sizeof (cont_device_t),
351 	    offsetof(cont_device_t, cond_next));
352 
353 	i_ddi_set_node_state((dev_info_t *)devi, DS_PROTO);
354 	da_log_enter((dev_info_t *)devi);
355 	return ((dev_info_t *)devi);
356 
357 fail:
358 	if (devi->devi_sys_prop_ptr)
359 		i_ddi_prop_list_delete(devi->devi_sys_prop_ptr);
360 	if (devi->devi_node_name)
361 		kmem_free(devi->devi_node_name, strlen(node_name) + 1);
362 	if (devi->devi_audit)
363 		kmem_free(devi->devi_audit, sizeof (devinfo_audit_t));
364 	kmem_cache_free(ddi_node_cache, devi);
365 	cmn_err(CE_NOTE, failed);
366 	return (NULL);
367 }
368 
369 /*
370  * free a dev_info structure.
371  * NB. Not callable from interrupt since impl_ddi_free_nodeid may block.
372  */
373 void
374 i_ddi_free_node(dev_info_t *dip)
375 {
376 	struct dev_info *devi = DEVI(dip);
377 	struct devi_nodeid *elem;
378 
379 	ASSERT(devi->devi_ref == 0);
380 	ASSERT(devi->devi_addr == NULL);
381 	ASSERT(devi->devi_node_state == DS_PROTO);
382 	ASSERT(devi->devi_child == NULL);
383 
384 	/* free devi_addr_buf allocated by ddi_set_name_addr() */
385 	if (devi->devi_addr_buf)
386 		kmem_free(devi->devi_addr_buf, 2 * MAXNAMELEN);
387 
388 	if (i_ndi_dev_is_auto_assigned_node(dip))
389 		impl_ddi_free_nodeid(DEVI(dip)->devi_nodeid);
390 
391 	if (ndi_dev_is_persistent_node(dip)) {
392 		mutex_enter(&devimap->dno_lock);
393 		ASSERT(devimap->dno_free);
394 		elem = devimap->dno_free;
395 		devimap->dno_free = elem->next;
396 		mutex_exit(&devimap->dno_lock);
397 		kmem_free(elem, sizeof (*elem));
398 	}
399 
400 	if (DEVI(dip)->devi_compat_names)
401 		kmem_free(DEVI(dip)->devi_compat_names,
402 		    DEVI(dip)->devi_compat_length);
403 	if (DEVI(dip)->devi_rebinding_name)
404 		kmem_free(DEVI(dip)->devi_rebinding_name,
405 		    strlen(DEVI(dip)->devi_rebinding_name) + 1);
406 
407 	ddi_prop_remove_all(dip);	/* remove driver properties */
408 	if (devi->devi_sys_prop_ptr)
409 		i_ddi_prop_list_delete(devi->devi_sys_prop_ptr);
410 	if (devi->devi_hw_prop_ptr)
411 		i_ddi_prop_list_delete(devi->devi_hw_prop_ptr);
412 
413 	if (DEVI(dip)->devi_devid_str)
414 		ddi_devid_str_free(DEVI(dip)->devi_devid_str);
415 
416 	i_ddi_set_node_state(dip, DS_INVAL);
417 	da_log_enter(dip);
418 	if (devi->devi_audit) {
419 		kmem_free(devi->devi_audit, sizeof (devinfo_audit_t));
420 	}
421 	if (devi->devi_device_class)
422 		kmem_free(devi->devi_device_class,
423 		    strlen(devi->devi_device_class) + 1);
424 	cv_destroy(&(devi->devi_cv));
425 	mutex_destroy(&(devi->devi_lock));
426 	mutex_destroy(&(devi->devi_pm_lock));
427 	mutex_destroy(&(devi->devi_pm_busy_lock));
428 
429 	RIO_TRACE((CE_NOTE, "i_ddi_free_node: destroying contract fields: "
430 	    "dip=%p", (void *)dip));
431 	contract_device_remove_dip(dip);
432 	ASSERT(devi->devi_ct_count == -1);
433 	ASSERT(list_is_empty(&(devi->devi_ct)));
434 	cv_destroy(&(devi->devi_ct_cv));
435 	list_destroy(&(devi->devi_ct));
436 	/* free this last since contract_device_remove_dip() uses it */
437 	mutex_destroy(&(devi->devi_ct_lock));
438 	RIO_TRACE((CE_NOTE, "i_ddi_free_node: destroyed all contract fields: "
439 	    "dip=%p, name=%s", (void *)dip, devi->devi_node_name));
440 
441 	kmem_free(devi->devi_node_name, strlen(devi->devi_node_name) + 1);
442 
443 	kmem_cache_free(ddi_node_cache, devi);
444 }
445 
446 
447 /*
448  * Node state transitions
449  */
450 
451 /*
452  * Change the node name
453  */
454 int
455 ndi_devi_set_nodename(dev_info_t *dip, char *name, int flags)
456 {
457 	_NOTE(ARGUNUSED(flags))
458 	char *nname, *oname;
459 
460 	ASSERT(dip && name);
461 
462 	oname = DEVI(dip)->devi_node_name;
463 	if (strcmp(oname, name) == 0)
464 		return (DDI_SUCCESS);
465 
466 	/*
467 	 * pcicfg_fix_ethernet requires a name change after node
468 	 * is linked into the tree. When pcicfg is fixed, we
469 	 * should only allow name change in DS_PROTO state.
470 	 */
471 	if (i_ddi_node_state(dip) >= DS_BOUND) {
472 		/*
473 		 * Don't allow name change once node is bound
474 		 */
475 		cmn_err(CE_NOTE,
476 		    "ndi_devi_set_nodename: node already bound dip = %p,"
477 		    " %s -> %s", (void *)dip, ddi_node_name(dip), name);
478 		return (NDI_FAILURE);
479 	}
480 
481 	nname = i_ddi_strdup(name, KM_SLEEP);
482 	DEVI(dip)->devi_node_name = nname;
483 	i_ddi_set_binding_name(dip, nname);
484 	kmem_free(oname, strlen(oname) + 1);
485 
486 	da_log_enter(dip);
487 	return (NDI_SUCCESS);
488 }
489 
490 void
491 i_ddi_add_devimap(dev_info_t *dip)
492 {
493 	struct devi_nodeid *elem;
494 
495 	ASSERT(dip);
496 
497 	if (!ndi_dev_is_persistent_node(dip))
498 		return;
499 
500 	ASSERT(ddi_get_parent(dip) == NULL || (DEVI_VHCI_NODE(dip)) ||
501 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
502 
503 	mutex_enter(&devimap->dno_lock);
504 
505 	ASSERT(devimap->dno_free);
506 
507 	elem = devimap->dno_free;
508 	devimap->dno_free = elem->next;
509 
510 	elem->nodeid = ddi_get_nodeid(dip);
511 	elem->dip = dip;
512 	elem->next = devimap->dno_head;
513 	devimap->dno_head = elem;
514 
515 	devimap->dno_list_length++;
516 
517 	mutex_exit(&devimap->dno_lock);
518 }
519 
520 static int
521 i_ddi_remove_devimap(dev_info_t *dip)
522 {
523 	struct devi_nodeid *prev, *elem;
524 	static const char *fcn = "i_ddi_remove_devimap";
525 
526 	ASSERT(dip);
527 
528 	if (!ndi_dev_is_persistent_node(dip))
529 		return (DDI_SUCCESS);
530 
531 	mutex_enter(&devimap->dno_lock);
532 
533 	/*
534 	 * The following check is done with dno_lock held
535 	 * to prevent race between dip removal and
536 	 * e_ddi_prom_node_to_dip()
537 	 */
538 	if (e_ddi_devi_holdcnt(dip)) {
539 		mutex_exit(&devimap->dno_lock);
540 		return (DDI_FAILURE);
541 	}
542 
543 	ASSERT(devimap->dno_head);
544 	ASSERT(devimap->dno_list_length > 0);
545 
546 	prev = NULL;
547 	for (elem = devimap->dno_head; elem; elem = elem->next) {
548 		if (elem->dip == dip) {
549 			ASSERT(elem->nodeid == ddi_get_nodeid(dip));
550 			break;
551 		}
552 		prev = elem;
553 	}
554 
555 	if (elem && prev)
556 		prev->next = elem->next;
557 	else if (elem)
558 		devimap->dno_head = elem->next;
559 	else
560 		panic("%s: devinfo node(%p) not found",
561 		    fcn, (void *)dip);
562 
563 	devimap->dno_list_length--;
564 
565 	elem->nodeid = 0;
566 	elem->dip = NULL;
567 
568 	elem->next = devimap->dno_free;
569 	devimap->dno_free = elem;
570 
571 	mutex_exit(&devimap->dno_lock);
572 
573 	return (DDI_SUCCESS);
574 }
575 
576 /*
577  * Link this node into the devinfo tree and add to orphan list
578  * Not callable from interrupt context
579  */
580 static void
581 link_node(dev_info_t *dip)
582 {
583 	struct dev_info *devi = DEVI(dip);
584 	struct dev_info *parent = devi->devi_parent;
585 	dev_info_t **dipp;
586 
587 	ASSERT(parent);	/* never called for root node */
588 
589 	NDI_CONFIG_DEBUG((CE_CONT, "link_node: parent = %s child = %s\n",
590 	    parent->devi_node_name, devi->devi_node_name));
591 
592 	/*
593 	 * Hold the global_vhci_lock before linking any direct
594 	 * children of rootnex driver. This special lock protects
595 	 * linking and unlinking for rootnext direct children.
596 	 */
597 	if ((dev_info_t *)parent == ddi_root_node())
598 		mutex_enter(&global_vhci_lock);
599 
600 	/*
601 	 * attach the node to end of the list unless the node is already there
602 	 */
603 	dipp = (dev_info_t **)(&DEVI(parent)->devi_child);
604 	while (*dipp && (*dipp != dip)) {
605 		dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling);
606 	}
607 	ASSERT(*dipp == NULL);	/* node is not linked */
608 
609 	/*
610 	 * Now that we are in the tree, update the devi-nodeid map.
611 	 */
612 	i_ddi_add_devimap(dip);
613 
614 	/*
615 	 * This is a temporary workaround for Bug 4618861.
616 	 * We keep the scsi_vhci nexus node on the left side of the devinfo
617 	 * tree (under the root nexus driver), so that virtual nodes under
618 	 * scsi_vhci will be SUSPENDed first and RESUMEd last.  This ensures
619 	 * that the pHCI nodes are active during times when their clients
620 	 * may be depending on them.  This workaround embodies the knowledge
621 	 * that system PM and CPR both traverse the tree left-to-right during
622 	 * SUSPEND and right-to-left during RESUME.
623 	 * Extending the workaround to IB Nexus/VHCI
624 	 * driver also.
625 	 */
626 	if (strcmp(devi->devi_binding_name, "scsi_vhci") == 0) {
627 		/* Add scsi_vhci to beginning of list */
628 		ASSERT((dev_info_t *)parent == top_devinfo);
629 		/* scsi_vhci under rootnex */
630 		devi->devi_sibling = parent->devi_child;
631 		parent->devi_child = devi;
632 	} else if (strcmp(devi->devi_binding_name, "ib") == 0) {
633 		i_link_vhci_node(dip);
634 	} else {
635 		/* Add to end of list */
636 		*dipp = dip;
637 		DEVI(dip)->devi_sibling = NULL;
638 	}
639 
640 	/*
641 	 * Release the global_vhci_lock before linking any direct
642 	 * children of rootnex driver.
643 	 */
644 	if ((dev_info_t *)parent == ddi_root_node())
645 		mutex_exit(&global_vhci_lock);
646 
647 	/* persistent nodes go on orphan list */
648 	if (ndi_dev_is_persistent_node(dip))
649 		add_to_dn_list(&orphanlist, dip);
650 }
651 
652 /*
653  * Unlink this node from the devinfo tree
654  */
655 static int
656 unlink_node(dev_info_t *dip)
657 {
658 	struct dev_info *devi = DEVI(dip);
659 	struct dev_info *parent = devi->devi_parent;
660 	dev_info_t **dipp;
661 
662 	ASSERT(parent != NULL);
663 	ASSERT(devi->devi_node_state == DS_LINKED);
664 
665 	NDI_CONFIG_DEBUG((CE_CONT, "unlink_node: name = %s\n",
666 	    ddi_node_name(dip)));
667 
668 	/* check references */
669 	if (devi->devi_ref || i_ddi_remove_devimap(dip) != DDI_SUCCESS)
670 		return (DDI_FAILURE);
671 
672 	/*
673 	 * Hold the global_vhci_lock before linking any direct
674 	 * children of rootnex driver.
675 	 */
676 	if ((dev_info_t *)parent == ddi_root_node())
677 		mutex_enter(&global_vhci_lock);
678 
679 	dipp = (dev_info_t **)(&DEVI(parent)->devi_child);
680 	while (*dipp && (*dipp != dip)) {
681 		dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling);
682 	}
683 	if (*dipp) {
684 		*dipp = (dev_info_t *)(devi->devi_sibling);
685 		devi->devi_sibling = NULL;
686 	} else {
687 		NDI_CONFIG_DEBUG((CE_NOTE, "unlink_node: %s not linked",
688 		    devi->devi_node_name));
689 	}
690 
691 	/*
692 	 * Release the global_vhci_lock before linking any direct
693 	 * children of rootnex driver.
694 	 */
695 	if ((dev_info_t *)parent == ddi_root_node())
696 		mutex_exit(&global_vhci_lock);
697 
698 	/* Remove node from orphan list */
699 	if (ndi_dev_is_persistent_node(dip)) {
700 		remove_from_dn_list(&orphanlist, dip);
701 	}
702 
703 	return (DDI_SUCCESS);
704 }
705 
706 /*
707  * Bind this devinfo node to a driver. If compat is NON-NULL, try that first.
708  * Else, use the node-name.
709  *
710  * NOTE: IEEE1275 specifies that nodename should be tried before compatible.
711  *	Solaris implementation binds nodename after compatible.
712  *
713  * If we find a binding,
714  * - set the binding name to the the string,
715  * - set major number to driver major
716  *
717  * If we don't find a binding,
718  * - return failure
719  */
720 static int
721 bind_node(dev_info_t *dip)
722 {
723 	char *p = NULL;
724 	major_t major = DDI_MAJOR_T_NONE;
725 	struct dev_info *devi = DEVI(dip);
726 	dev_info_t *parent = ddi_get_parent(dip);
727 
728 	ASSERT(devi->devi_node_state == DS_LINKED);
729 
730 	NDI_CONFIG_DEBUG((CE_CONT, "bind_node: 0x%p(name = %s)\n",
731 	    (void *)dip, ddi_node_name(dip)));
732 
733 	mutex_enter(&DEVI(dip)->devi_lock);
734 	if (DEVI(dip)->devi_flags & DEVI_NO_BIND) {
735 		mutex_exit(&DEVI(dip)->devi_lock);
736 		return (DDI_FAILURE);
737 	}
738 	mutex_exit(&DEVI(dip)->devi_lock);
739 
740 	/* find the driver with most specific binding using compatible */
741 	major = ddi_compatible_driver_major(dip, &p);
742 	if (major == DDI_MAJOR_T_NONE)
743 		return (DDI_FAILURE);
744 
745 	devi->devi_major = major;
746 	if (p != NULL) {
747 		i_ddi_set_binding_name(dip, p);
748 		NDI_CONFIG_DEBUG((CE_CONT, "bind_node: %s bound to %s\n",
749 		    devi->devi_node_name, p));
750 	}
751 
752 	/* Link node to per-driver list */
753 	link_to_driver_list(dip);
754 
755 	/*
756 	 * reset parent flag so that nexus will merge .conf props
757 	 */
758 	if (ndi_dev_is_persistent_node(dip)) {
759 		mutex_enter(&DEVI(parent)->devi_lock);
760 		DEVI(parent)->devi_flags &=
761 		    ~(DEVI_ATTACHED_CHILDREN|DEVI_MADE_CHILDREN);
762 		mutex_exit(&DEVI(parent)->devi_lock);
763 	}
764 	return (DDI_SUCCESS);
765 }
766 
767 /*
768  * Unbind this devinfo node
769  * Called before the node is destroyed or driver is removed from system
770  */
771 static int
772 unbind_node(dev_info_t *dip)
773 {
774 	ASSERT(DEVI(dip)->devi_node_state == DS_BOUND);
775 	ASSERT(DEVI(dip)->devi_major != DDI_MAJOR_T_NONE);
776 
777 	/* check references */
778 	if (DEVI(dip)->devi_ref)
779 		return (DDI_FAILURE);
780 
781 	NDI_CONFIG_DEBUG((CE_CONT, "unbind_node: 0x%p(name = %s)\n",
782 	    (void *)dip, ddi_node_name(dip)));
783 
784 	unlink_from_driver_list(dip);
785 
786 	DEVI(dip)->devi_major = DDI_MAJOR_T_NONE;
787 	DEVI(dip)->devi_binding_name = DEVI(dip)->devi_node_name;
788 	return (DDI_SUCCESS);
789 }
790 
791 /*
792  * Initialize a node: calls the parent nexus' bus_ctl ops to do the operation.
793  * Must hold parent and per-driver list while calling this function.
794  * A successful init_node() returns with an active ndi_hold_devi() hold on
795  * the parent.
796  */
797 static int
798 init_node(dev_info_t *dip)
799 {
800 	int error;
801 	dev_info_t *pdip = ddi_get_parent(dip);
802 	int (*f)(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *, void *);
803 	char *path;
804 	major_t	major;
805 
806 	ASSERT(i_ddi_node_state(dip) == DS_BOUND);
807 
808 	/* should be DS_READY except for pcmcia ... */
809 	ASSERT(i_ddi_node_state(pdip) >= DS_PROBED);
810 
811 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
812 	(void) ddi_pathname(dip, path);
813 	NDI_CONFIG_DEBUG((CE_CONT, "init_node: entry: path %s 0x%p\n",
814 	    path, (void *)dip));
815 
816 	/*
817 	 * The parent must have a bus_ctl operation.
818 	 */
819 	if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
820 	    (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_ctl) == NULL) {
821 		error = DDI_FAILURE;
822 		goto out;
823 	}
824 
825 	add_global_props(dip);
826 
827 	/*
828 	 * Invoke the parent's bus_ctl operation with the DDI_CTLOPS_INITCHILD
829 	 * command to transform the child to canonical form 1. If there
830 	 * is an error, ddi_remove_child should be called, to clean up.
831 	 */
832 	error = (*f)(pdip, pdip, DDI_CTLOPS_INITCHILD, dip, NULL);
833 	if (error != DDI_SUCCESS) {
834 		NDI_CONFIG_DEBUG((CE_CONT, "init_node: %s 0x%p failed\n",
835 		    path, (void *)dip));
836 		remove_global_props(dip);
837 		/* in case nexus driver didn't clear this field */
838 		ddi_set_name_addr(dip, NULL);
839 		error = DDI_FAILURE;
840 		goto out;
841 	}
842 
843 	ndi_hold_devi(pdip);			/* initial hold of parent */
844 
845 	/* recompute path after initchild for @addr information */
846 	(void) ddi_pathname(dip, path);
847 
848 	/* Check for duplicate nodes */
849 	if (find_duplicate_child(pdip, dip) != NULL) {
850 		/*
851 		 * uninit_node() the duplicate - a successful uninit_node()
852 		 * will release inital hold of parent using ndi_rele_devi().
853 		 */
854 		if ((error = uninit_node(dip)) != DDI_SUCCESS) {
855 			ndi_rele_devi(pdip);	/* release initial hold */
856 			cmn_err(CE_WARN, "init_node: uninit of duplicate "
857 			    "node %s failed", path);
858 		}
859 		NDI_CONFIG_DEBUG((CE_CONT, "init_node: duplicate uninit "
860 		    "%s 0x%p%s\n", path, (void *)dip,
861 		    (error == DDI_SUCCESS) ? "" : " failed"));
862 		error = DDI_FAILURE;
863 		goto out;
864 	}
865 
866 	/*
867 	 * Check to see if we have a path-oriented driver alias that overrides
868 	 * the current driver binding. If so, we need to rebind. This check
869 	 * needs to be delayed until after a successful DDI_CTLOPS_INITCHILD,
870 	 * so the unit-address is established on the last component of the path.
871 	 *
872 	 * NOTE: Allowing a path-oriented alias to change the driver binding
873 	 * of a driver.conf node results in non-intuitive property behavior.
874 	 * We provide a tunable (driver_conf_allow_path_alias) to control
875 	 * this behavior. See uninit_node() for more details.
876 	 *
877 	 * NOTE: If you are adding a path-oriented alias for the boot device,
878 	 * and there is mismatch between OBP and the kernel in regard to
879 	 * generic name use, like "disk" .vs. "ssd", then you will need
880 	 * to add a path-oriented alias for both paths.
881 	 */
882 	major = ddi_name_to_major(path);
883 	if ((major != DDI_MAJOR_T_NONE) &&
884 	    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED) &&
885 	    (major != DEVI(dip)->devi_major) &&
886 	    (ndi_dev_is_persistent_node(dip) || driver_conf_allow_path_alias)) {
887 
888 		/* Mark node for rebind processing. */
889 		mutex_enter(&DEVI(dip)->devi_lock);
890 		DEVI(dip)->devi_flags |= DEVI_REBIND;
891 		mutex_exit(&DEVI(dip)->devi_lock);
892 
893 		/*
894 		 * Add an extra hold on the parent to prevent it from ever
895 		 * having a zero devi_ref during the child rebind process.
896 		 * This is necessary to ensure that the parent will never
897 		 * detach(9E) during the rebind.
898 		 */
899 		ndi_hold_devi(pdip);		/* extra hold of parent */
900 
901 		/*
902 		 * uninit_node() current binding - a successful uninit_node()
903 		 * will release extra hold of parent using ndi_rele_devi().
904 		 */
905 		if ((error = uninit_node(dip)) != DDI_SUCCESS) {
906 			ndi_rele_devi(pdip);	/* release extra hold */
907 			ndi_rele_devi(pdip);	/* release initial hold */
908 			cmn_err(CE_WARN, "init_node: uninit for rebind "
909 			    "of node %s failed", path);
910 			goto out;
911 		}
912 
913 		/* Unbind: demote the node back to DS_LINKED.  */
914 		if ((error = ndi_devi_unbind_driver(dip)) != DDI_SUCCESS) {
915 			ndi_rele_devi(pdip);	/* relrease initial hold */
916 			cmn_err(CE_WARN, "init_node: unbind for rebind "
917 			    "of node %s failed", path);
918 			goto out;
919 		}
920 
921 		/* establish rebinding name */
922 		if (DEVI(dip)->devi_rebinding_name == NULL)
923 			DEVI(dip)->devi_rebinding_name =
924 			    i_ddi_strdup(path, KM_SLEEP);
925 
926 		/*
927 		 * Now that we are demoted and marked for rebind, repromote.
928 		 * We need to do this in steps, instead of just calling
929 		 * ddi_initchild, so that we can redo the merge operation
930 		 * after we are rebound to the path-bound driver.
931 		 *
932 		 * Start by rebinding node to the path-bound driver.
933 		 */
934 		if ((error = ndi_devi_bind_driver(dip, 0)) != DDI_SUCCESS) {
935 			ndi_rele_devi(pdip);	/* relrease initial hold */
936 			cmn_err(CE_WARN, "init_node: rebind "
937 			    "of node %s failed", path);
938 			goto out;
939 		}
940 
941 		/*
942 		 * If the node is not a driver.conf node then merge
943 		 * driver.conf properties from new path-bound driver.conf.
944 		 */
945 		if (ndi_dev_is_persistent_node(dip))
946 			(void) i_ndi_make_spec_children(pdip, 0);
947 
948 		/*
949 		 * Now that we have taken care of merge, repromote back
950 		 * to DS_INITIALIZED.
951 		 */
952 		error = ddi_initchild(pdip, dip);
953 		NDI_CONFIG_DEBUG((CE_CONT, "init_node: rebind "
954 		    "%s 0x%p\n", path, (void *)dip));
955 
956 		/*
957 		 * Release our initial hold. If ddi_initchild() was
958 		 * successful then it will return with the active hold.
959 		 */
960 		ndi_rele_devi(pdip);
961 		goto out;
962 	}
963 
964 	/*
965 	 * Apply multi-parent/deep-nexus optimization to the new node
966 	 */
967 	DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
968 	ddi_optimize_dtree(dip);
969 	error = DDI_SUCCESS;		/* return with active hold */
970 
971 out:	if (error != DDI_SUCCESS) {
972 		/* On failure ensure that DEVI_REBIND is cleared */
973 		mutex_enter(&DEVI(dip)->devi_lock);
974 		DEVI(dip)->devi_flags &= ~DEVI_REBIND;
975 		mutex_exit(&DEVI(dip)->devi_lock);
976 	}
977 	kmem_free(path, MAXPATHLEN);
978 	return (error);
979 }
980 
981 /*
982  * Uninitialize node
983  * The per-driver list must be held busy during the call.
984  * A successful uninit_node() releases the init_node() hold on
985  * the parent by calling ndi_rele_devi().
986  */
987 static int
988 uninit_node(dev_info_t *dip)
989 {
990 	int node_state_entry;
991 	dev_info_t *pdip;
992 	struct dev_ops *ops;
993 	int (*f)();
994 	int error;
995 	char *addr;
996 
997 	/*
998 	 * Don't check for references here or else a ref-counted
999 	 * dip cannot be downgraded by the framework.
1000 	 */
1001 	node_state_entry = i_ddi_node_state(dip);
1002 	ASSERT((node_state_entry == DS_BOUND) ||
1003 	    (node_state_entry == DS_INITIALIZED));
1004 	pdip = ddi_get_parent(dip);
1005 	ASSERT(pdip);
1006 
1007 	NDI_CONFIG_DEBUG((CE_CONT, "uninit_node: 0x%p(%s%d)\n",
1008 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1009 
1010 	if (((ops = ddi_get_driver(pdip)) == NULL) ||
1011 	    (ops->devo_bus_ops == NULL) ||
1012 	    ((f = ops->devo_bus_ops->bus_ctl) == NULL)) {
1013 		return (DDI_FAILURE);
1014 	}
1015 
1016 	/*
1017 	 * save the @addr prior to DDI_CTLOPS_UNINITCHILD for use in
1018 	 * freeing the instance if it succeeds.
1019 	 */
1020 	if (node_state_entry == DS_INITIALIZED) {
1021 		addr = ddi_get_name_addr(dip);
1022 		if (addr)
1023 			addr = i_ddi_strdup(addr, KM_SLEEP);
1024 	} else {
1025 		addr = NULL;
1026 	}
1027 
1028 	error = (*f)(pdip, pdip, DDI_CTLOPS_UNINITCHILD, dip, (void *)NULL);
1029 	if (error == DDI_SUCCESS) {
1030 		/* if uninitchild forgot to set devi_addr to NULL do it now */
1031 		ddi_set_name_addr(dip, NULL);
1032 
1033 		/*
1034 		 * Free instance number. This is a no-op if instance has
1035 		 * been kept by probe_node().  Avoid free when we are called
1036 		 * from init_node (DS_BOUND) because the instance has not yet
1037 		 * been assigned.
1038 		 */
1039 		if (node_state_entry == DS_INITIALIZED) {
1040 			e_ddi_free_instance(dip, addr);
1041 			DEVI(dip)->devi_instance = -1;
1042 		}
1043 
1044 		/* release the init_node hold */
1045 		ndi_rele_devi(pdip);
1046 
1047 		remove_global_props(dip);
1048 
1049 		/*
1050 		 * NOTE: The decision on whether to allow a path-oriented
1051 		 * rebind of a driver.conf enumerated node is made by
1052 		 * init_node() based on driver_conf_allow_path_alias. The
1053 		 * rebind code below prevents deletion of system properties
1054 		 * on driver.conf nodes.
1055 		 *
1056 		 * When driver_conf_allow_path_alias is set, property behavior
1057 		 * on rebound driver.conf file is non-intuitive. For a
1058 		 * driver.conf node, the unit-address properties come from
1059 		 * the driver.conf file as system properties. Removing system
1060 		 * properties from a driver.conf node makes the node
1061 		 * useless (we get node without unit-address properties) - so
1062 		 * we leave system properties in place. The result is a node
1063 		 * where system properties come from the node being rebound,
1064 		 * and global properties come from the driver.conf file
1065 		 * of the driver we are rebinding to.  If we could determine
1066 		 * that the path-oriented alias driver.conf file defined a
1067 		 * node at the same unit address, it would be best to use
1068 		 * that node and avoid the non-intuitive property behavior.
1069 		 * Unfortunately, the current "merge" code does not support
1070 		 * this, so we live with the non-intuitive property behavior.
1071 		 */
1072 		if (!((ndi_dev_is_persistent_node(dip) == 0) &&
1073 		    (DEVI(dip)->devi_flags & DEVI_REBIND)))
1074 			e_ddi_prop_remove_all(dip);
1075 	} else {
1076 		NDI_CONFIG_DEBUG((CE_CONT, "uninit_node failed: 0x%p(%s%d)\n",
1077 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1078 	}
1079 
1080 	if (addr)
1081 		kmem_free(addr, strlen(addr) + 1);
1082 	return (error);
1083 }
1084 
1085 /*
1086  * Invoke driver's probe entry point to probe for existence of hardware.
1087  * Keep instance permanent for successful probe and leaf nodes.
1088  *
1089  * Per-driver list must be held busy while calling this function.
1090  */
1091 static int
1092 probe_node(dev_info_t *dip)
1093 {
1094 	int rv;
1095 
1096 	ASSERT(i_ddi_node_state(dip) == DS_INITIALIZED);
1097 
1098 	NDI_CONFIG_DEBUG((CE_CONT, "probe_node: 0x%p(%s%d)\n",
1099 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1100 
1101 	/* temporarily hold the driver while we probe */
1102 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
1103 	if (DEVI(dip)->devi_ops == NULL) {
1104 		NDI_CONFIG_DEBUG((CE_CONT,
1105 		    "probe_node: 0x%p(%s%d) cannot load driver\n",
1106 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1107 		return (DDI_FAILURE);
1108 	}
1109 
1110 	if (identify_9e != 0)
1111 		(void) devi_identify(dip);
1112 
1113 	rv = devi_probe(dip);
1114 
1115 	/* release the driver now that probe is complete */
1116 	ndi_rele_driver(dip);
1117 	DEVI(dip)->devi_ops = NULL;
1118 
1119 	switch (rv) {
1120 	case DDI_PROBE_SUCCESS:			/* found */
1121 	case DDI_PROBE_DONTCARE:		/* ddi_dev_is_sid */
1122 		e_ddi_keep_instance(dip);	/* persist instance */
1123 		rv = DDI_SUCCESS;
1124 		break;
1125 
1126 	case DDI_PROBE_PARTIAL:			/* maybe later */
1127 	case DDI_PROBE_FAILURE:			/* not found */
1128 		NDI_CONFIG_DEBUG((CE_CONT,
1129 		    "probe_node: 0x%p(%s%d) no hardware found%s\n",
1130 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip),
1131 		    (rv == DDI_PROBE_PARTIAL) ? " yet" : ""));
1132 		rv = DDI_FAILURE;
1133 		break;
1134 
1135 	default:
1136 #ifdef	DEBUG
1137 		cmn_err(CE_WARN, "probe_node: %s%d: illegal probe(9E) value",
1138 		    ddi_driver_name(dip), ddi_get_instance(dip));
1139 #endif	/* DEBUG */
1140 		rv = DDI_FAILURE;
1141 		break;
1142 	}
1143 	return (rv);
1144 }
1145 
1146 /*
1147  * Unprobe a node. Simply reset the node state.
1148  * Per-driver list must be held busy while calling this function.
1149  */
1150 static int
1151 unprobe_node(dev_info_t *dip)
1152 {
1153 	ASSERT(i_ddi_node_state(dip) == DS_PROBED);
1154 
1155 	/*
1156 	 * Don't check for references here or else a ref-counted
1157 	 * dip cannot be downgraded by the framework.
1158 	 */
1159 
1160 	NDI_CONFIG_DEBUG((CE_CONT, "unprobe_node: 0x%p(name = %s)\n",
1161 	    (void *)dip, ddi_node_name(dip)));
1162 	return (DDI_SUCCESS);
1163 }
1164 
1165 /*
1166  * Attach devinfo node.
1167  * Per-driver list must be held busy.
1168  */
1169 static int
1170 attach_node(dev_info_t *dip)
1171 {
1172 	int rv;
1173 
1174 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
1175 	ASSERT(i_ddi_node_state(dip) == DS_PROBED);
1176 
1177 	NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d)\n",
1178 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1179 
1180 	/*
1181 	 * Tell mpxio framework that a node is about to online.
1182 	 */
1183 	if ((rv = mdi_devi_online(dip, 0)) != NDI_SUCCESS) {
1184 		return (DDI_FAILURE);
1185 	}
1186 
1187 	/* no recursive attachment */
1188 	ASSERT(DEVI(dip)->devi_ops == NULL);
1189 
1190 	/*
1191 	 * Hold driver the node is bound to.
1192 	 */
1193 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
1194 	if (DEVI(dip)->devi_ops == NULL) {
1195 		/*
1196 		 * We were able to load driver for probing, so we should
1197 		 * not get here unless something really bad happened.
1198 		 */
1199 		cmn_err(CE_WARN, "attach_node: no driver for major %d",
1200 		    DEVI(dip)->devi_major);
1201 		return (DDI_FAILURE);
1202 	}
1203 
1204 	if (NEXUS_DRV(DEVI(dip)->devi_ops))
1205 		DEVI(dip)->devi_taskq = ddi_taskq_create(dip,
1206 		    "nexus_enum_tq", 1,
1207 		    TASKQ_DEFAULTPRI, 0);
1208 
1209 	mutex_enter(&(DEVI(dip)->devi_lock));
1210 	DEVI_SET_ATTACHING(dip);
1211 	DEVI_SET_NEED_RESET(dip);
1212 	mutex_exit(&(DEVI(dip)->devi_lock));
1213 
1214 	rv = devi_attach(dip, DDI_ATTACH);
1215 
1216 	mutex_enter(&(DEVI(dip)->devi_lock));
1217 	DEVI_CLR_ATTACHING(dip);
1218 
1219 	if (rv != DDI_SUCCESS) {
1220 		DEVI_CLR_NEED_RESET(dip);
1221 
1222 		/* ensure that devids are unregistered */
1223 		if (DEVI(dip)->devi_flags & DEVI_REGISTERED_DEVID) {
1224 			DEVI(dip)->devi_flags &= ~DEVI_REGISTERED_DEVID;
1225 			mutex_exit(&DEVI(dip)->devi_lock);
1226 			ddi_devid_unregister(dip);
1227 		} else
1228 			mutex_exit(&DEVI(dip)->devi_lock);
1229 
1230 		/*
1231 		 * Cleanup dacf reservations
1232 		 */
1233 		mutex_enter(&dacf_lock);
1234 		dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH);
1235 		dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH);
1236 		mutex_exit(&dacf_lock);
1237 		if (DEVI(dip)->devi_taskq)
1238 			ddi_taskq_destroy(DEVI(dip)->devi_taskq);
1239 		ddi_remove_minor_node(dip, NULL);
1240 
1241 		/* release the driver if attach failed */
1242 		ndi_rele_driver(dip);
1243 		DEVI(dip)->devi_ops = NULL;
1244 		NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d) failed\n",
1245 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1246 		return (DDI_FAILURE);
1247 	} else
1248 		mutex_exit(&DEVI(dip)->devi_lock);
1249 
1250 	/* successful attach, return with driver held */
1251 
1252 	return (DDI_SUCCESS);
1253 }
1254 
1255 /*
1256  * Detach devinfo node.
1257  * Per-driver list must be held busy.
1258  */
1259 static int
1260 detach_node(dev_info_t *dip, uint_t flag)
1261 {
1262 	struct devnames	*dnp;
1263 	int		rv;
1264 
1265 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
1266 	ASSERT(i_ddi_node_state(dip) == DS_ATTACHED);
1267 
1268 	/* check references */
1269 	if (DEVI(dip)->devi_ref)
1270 		return (DDI_FAILURE);
1271 
1272 	NDI_CONFIG_DEBUG((CE_CONT, "detach_node: 0x%p(%s%d)\n",
1273 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1274 
1275 	/*
1276 	 * NOTE: If we are processing a pHCI node then the calling code
1277 	 * must detect this and ndi_devi_enter() in (vHCI, parent(pHCI))
1278 	 * order unless pHCI and vHCI are siblings.  Code paths leading
1279 	 * here that must ensure this ordering include:
1280 	 * unconfig_immediate_children(), devi_unconfig_one(),
1281 	 * ndi_devi_unconfig_one(), ndi_devi_offline().
1282 	 */
1283 	ASSERT(!MDI_PHCI(dip) ||
1284 	    (ddi_get_parent(mdi_devi_get_vdip(dip)) == ddi_get_parent(dip)) ||
1285 	    DEVI_BUSY_OWNED(mdi_devi_get_vdip(dip)));
1286 
1287 	/* Offline the device node with the mpxio framework. */
1288 	if (mdi_devi_offline(dip, flag) != NDI_SUCCESS) {
1289 		return (DDI_FAILURE);
1290 	}
1291 
1292 	/* drain the taskq */
1293 	if (DEVI(dip)->devi_taskq)
1294 		ddi_taskq_wait(DEVI(dip)->devi_taskq);
1295 
1296 	rv = devi_detach(dip, DDI_DETACH);
1297 
1298 	if (rv != DDI_SUCCESS) {
1299 		NDI_CONFIG_DEBUG((CE_CONT,
1300 		    "detach_node: 0x%p(%s%d) failed\n",
1301 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1302 		return (DDI_FAILURE);
1303 	}
1304 
1305 	mutex_enter(&(DEVI(dip)->devi_lock));
1306 	DEVI_CLR_NEED_RESET(dip);
1307 	mutex_exit(&(DEVI(dip)->devi_lock));
1308 
1309 #if defined(__i386) || defined(__amd64)
1310 #if !defined(__xpv)
1311 	/*
1312 	 * Close any iommulib mediated linkage to an IOMMU
1313 	 */
1314 	iommulib_nex_close(dip);
1315 #endif
1316 #endif
1317 
1318 	/* destroy the taskq */
1319 	if (DEVI(dip)->devi_taskq) {
1320 		ddi_taskq_destroy(DEVI(dip)->devi_taskq);
1321 		DEVI(dip)->devi_taskq = NULL;
1322 	}
1323 
1324 	/* Cleanup dacf reservations */
1325 	mutex_enter(&dacf_lock);
1326 	dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH);
1327 	dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH);
1328 	mutex_exit(&dacf_lock);
1329 
1330 	/* remove any additional flavors that were added */
1331 	if (DEVI(dip)->devi_flavorv_n > 1 && DEVI(dip)->devi_flavorv != NULL) {
1332 		kmem_free(DEVI(dip)->devi_flavorv,
1333 		    (DEVI(dip)->devi_flavorv_n - 1) * sizeof (void *));
1334 		DEVI(dip)->devi_flavorv = NULL;
1335 	}
1336 
1337 	/* Remove properties and minor nodes in case driver forgots */
1338 	ddi_remove_minor_node(dip, NULL);
1339 	ddi_prop_remove_all(dip);
1340 
1341 	/* a detached node can't have attached or .conf children */
1342 	mutex_enter(&DEVI(dip)->devi_lock);
1343 	DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN|DEVI_ATTACHED_CHILDREN);
1344 
1345 	/* ensure that devids registered during attach are unregistered */
1346 	if (DEVI(dip)->devi_flags & DEVI_REGISTERED_DEVID) {
1347 		DEVI(dip)->devi_flags &= ~DEVI_REGISTERED_DEVID;
1348 		mutex_exit(&DEVI(dip)->devi_lock);
1349 		ddi_devid_unregister(dip);
1350 	} else
1351 		mutex_exit(&DEVI(dip)->devi_lock);
1352 
1353 	/*
1354 	 * If the instance has successfully detached in detach_driver() context,
1355 	 * clear DN_DRIVER_HELD for correct ddi_hold_installed_driver()
1356 	 * behavior. Consumers like qassociate() depend on this (via clnopen()).
1357 	 */
1358 	if (flag & NDI_DETACH_DRIVER) {
1359 		dnp = &(devnamesp[DEVI(dip)->devi_major]);
1360 		LOCK_DEV_OPS(&dnp->dn_lock);
1361 		dnp->dn_flags &= ~DN_DRIVER_HELD;
1362 		UNLOCK_DEV_OPS(&dnp->dn_lock);
1363 	}
1364 
1365 	/* successful detach, release the driver */
1366 	ndi_rele_driver(dip);
1367 	DEVI(dip)->devi_ops = NULL;
1368 	return (DDI_SUCCESS);
1369 }
1370 
1371 /*
1372  * Run dacf post_attach routines
1373  */
1374 static int
1375 postattach_node(dev_info_t *dip)
1376 {
1377 	int rval;
1378 
1379 	/*
1380 	 * For hotplug busses like USB, it's possible that devices
1381 	 * are removed but dip is still around. We don't want to
1382 	 * run dacf routines as part of detach failure recovery.
1383 	 *
1384 	 * Pretend success until we figure out how to prevent
1385 	 * access to such devinfo nodes.
1386 	 */
1387 	if (DEVI_IS_DEVICE_REMOVED(dip))
1388 		return (DDI_SUCCESS);
1389 
1390 	/*
1391 	 * if dacf_postattach failed, report it to the framework
1392 	 * so that it can be retried later at the open time.
1393 	 */
1394 	mutex_enter(&dacf_lock);
1395 	rval = dacfc_postattach(dip);
1396 	mutex_exit(&dacf_lock);
1397 
1398 	/*
1399 	 * Plumbing during postattach may fail because of the
1400 	 * underlying device is not ready. This will fail ndi_devi_config()
1401 	 * in dv_filldir() and a warning message is issued. The message
1402 	 * from here will explain what happened
1403 	 */
1404 	if (rval != DACF_SUCCESS) {
1405 		cmn_err(CE_WARN, "Postattach failed for %s%d\n",
1406 		    ddi_driver_name(dip), ddi_get_instance(dip));
1407 		return (DDI_FAILURE);
1408 	}
1409 
1410 	return (DDI_SUCCESS);
1411 }
1412 
1413 /*
1414  * Run dacf pre-detach routines
1415  */
1416 static int
1417 predetach_node(dev_info_t *dip, uint_t flag)
1418 {
1419 	int ret;
1420 
1421 	/*
1422 	 * Don't auto-detach if DDI_FORCEATTACH or DDI_NO_AUTODETACH
1423 	 * properties are set.
1424 	 */
1425 	if (flag & NDI_AUTODETACH) {
1426 		struct devnames *dnp;
1427 		int pflag = DDI_PROP_NOTPROM | DDI_PROP_DONTPASS;
1428 
1429 		if ((ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1430 		    pflag, DDI_FORCEATTACH, 0) == 1) ||
1431 		    (ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1432 		    pflag, DDI_NO_AUTODETACH, 0) == 1))
1433 			return (DDI_FAILURE);
1434 
1435 		/* check for driver global version of DDI_NO_AUTODETACH */
1436 		dnp = &devnamesp[DEVI(dip)->devi_major];
1437 		LOCK_DEV_OPS(&dnp->dn_lock);
1438 		if (dnp->dn_flags & DN_NO_AUTODETACH) {
1439 			UNLOCK_DEV_OPS(&dnp->dn_lock);
1440 			return (DDI_FAILURE);
1441 		}
1442 		UNLOCK_DEV_OPS(&dnp->dn_lock);
1443 	}
1444 
1445 	mutex_enter(&dacf_lock);
1446 	ret = dacfc_predetach(dip);
1447 	mutex_exit(&dacf_lock);
1448 
1449 	return (ret);
1450 }
1451 
1452 /*
1453  * Wrapper for making multiple state transitions
1454  */
1455 
1456 /*
1457  * i_ndi_config_node: upgrade dev_info node into a specified state.
1458  * It is a bit tricky because the locking protocol changes before and
1459  * after a node is bound to a driver. All locks are held external to
1460  * this function.
1461  */
1462 int
1463 i_ndi_config_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag)
1464 {
1465 	_NOTE(ARGUNUSED(flag))
1466 	int rv = DDI_SUCCESS;
1467 
1468 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
1469 
1470 	while ((i_ddi_node_state(dip) < state) && (rv == DDI_SUCCESS)) {
1471 
1472 		/* don't allow any more changes to the device tree */
1473 		if (devinfo_freeze) {
1474 			rv = DDI_FAILURE;
1475 			break;
1476 		}
1477 
1478 		switch (i_ddi_node_state(dip)) {
1479 		case DS_PROTO:
1480 			/*
1481 			 * only caller can reference this node, no external
1482 			 * locking needed.
1483 			 */
1484 			link_node(dip);
1485 			i_ddi_set_node_state(dip, DS_LINKED);
1486 			break;
1487 		case DS_LINKED:
1488 			/*
1489 			 * Three code path may attempt to bind a node:
1490 			 * - boot code
1491 			 * - add_drv
1492 			 * - hotplug thread
1493 			 * Boot code is single threaded, add_drv synchronize
1494 			 * on a userland lock, and hotplug synchronize on
1495 			 * hotplug_lk. There could be a race between add_drv
1496 			 * and hotplug thread. We'll live with this until the
1497 			 * conversion to top-down loading.
1498 			 */
1499 			if ((rv = bind_node(dip)) == DDI_SUCCESS)
1500 				i_ddi_set_node_state(dip, DS_BOUND);
1501 
1502 			break;
1503 		case DS_BOUND:
1504 			/*
1505 			 * The following transitions synchronizes on the
1506 			 * per-driver busy changing flag, since we already
1507 			 * have a driver.
1508 			 */
1509 			if ((rv = init_node(dip)) == DDI_SUCCESS)
1510 				i_ddi_set_node_state(dip, DS_INITIALIZED);
1511 			break;
1512 		case DS_INITIALIZED:
1513 			if ((rv = probe_node(dip)) == DDI_SUCCESS)
1514 				i_ddi_set_node_state(dip, DS_PROBED);
1515 			break;
1516 		case DS_PROBED:
1517 			i_ddi_check_retire(dip);
1518 			atomic_add_long(&devinfo_attach_detach, 1);
1519 			if ((rv = attach_node(dip)) == DDI_SUCCESS)
1520 				i_ddi_set_node_state(dip, DS_ATTACHED);
1521 			atomic_add_long(&devinfo_attach_detach, -1);
1522 			break;
1523 		case DS_ATTACHED:
1524 			if ((rv = postattach_node(dip)) == DDI_SUCCESS)
1525 				i_ddi_set_node_state(dip, DS_READY);
1526 			break;
1527 		case DS_READY:
1528 			break;
1529 		default:
1530 			/* should never reach here */
1531 			ASSERT("unknown devinfo state");
1532 		}
1533 	}
1534 
1535 	if (ddidebug & DDI_AUDIT)
1536 		da_log_enter(dip);
1537 	return (rv);
1538 }
1539 
1540 /*
1541  * i_ndi_unconfig_node: downgrade dev_info node into a specified state.
1542  */
1543 int
1544 i_ndi_unconfig_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag)
1545 {
1546 	int	rv = DDI_SUCCESS;
1547 
1548 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
1549 
1550 	while ((i_ddi_node_state(dip) > state) && (rv == DDI_SUCCESS)) {
1551 
1552 		/* don't allow any more changes to the device tree */
1553 		if (devinfo_freeze) {
1554 			rv = DDI_FAILURE;
1555 			break;
1556 		}
1557 
1558 		switch (i_ddi_node_state(dip)) {
1559 		case DS_PROTO:
1560 			break;
1561 		case DS_LINKED:
1562 			/*
1563 			 * Persistent nodes are only removed by hotplug code
1564 			 * .conf nodes synchronizes on per-driver list.
1565 			 */
1566 			if ((rv = unlink_node(dip)) == DDI_SUCCESS)
1567 				i_ddi_set_node_state(dip, DS_PROTO);
1568 			break;
1569 		case DS_BOUND:
1570 			/*
1571 			 * The following transitions synchronizes on the
1572 			 * per-driver busy changing flag, since we already
1573 			 * have a driver.
1574 			 */
1575 			if ((rv = unbind_node(dip)) == DDI_SUCCESS)
1576 				i_ddi_set_node_state(dip, DS_LINKED);
1577 			break;
1578 		case DS_INITIALIZED:
1579 			if ((rv = uninit_node(dip)) == DDI_SUCCESS)
1580 				i_ddi_set_node_state(dip, DS_BOUND);
1581 			break;
1582 		case DS_PROBED:
1583 			if ((rv = unprobe_node(dip)) == DDI_SUCCESS)
1584 				i_ddi_set_node_state(dip, DS_INITIALIZED);
1585 			break;
1586 		case DS_ATTACHED:
1587 			atomic_add_long(&devinfo_attach_detach, 1);
1588 
1589 			mutex_enter(&(DEVI(dip)->devi_lock));
1590 			DEVI_SET_DETACHING(dip);
1591 			mutex_exit(&(DEVI(dip)->devi_lock));
1592 
1593 			membar_enter();	/* ensure visibility for hold_devi */
1594 
1595 			if ((rv = detach_node(dip, flag)) == DDI_SUCCESS)
1596 				i_ddi_set_node_state(dip, DS_PROBED);
1597 
1598 			mutex_enter(&(DEVI(dip)->devi_lock));
1599 			DEVI_CLR_DETACHING(dip);
1600 			mutex_exit(&(DEVI(dip)->devi_lock));
1601 
1602 			atomic_add_long(&devinfo_attach_detach, -1);
1603 			break;
1604 		case DS_READY:
1605 			if ((rv = predetach_node(dip, flag)) == DDI_SUCCESS)
1606 				i_ddi_set_node_state(dip, DS_ATTACHED);
1607 			break;
1608 		default:
1609 			ASSERT("unknown devinfo state");
1610 		}
1611 	}
1612 	da_log_enter(dip);
1613 	return (rv);
1614 }
1615 
1616 /*
1617  * ddi_initchild: transform node to DS_INITIALIZED state
1618  */
1619 int
1620 ddi_initchild(dev_info_t *parent, dev_info_t *proto)
1621 {
1622 	int ret, circ;
1623 
1624 	ndi_devi_enter(parent, &circ);
1625 	ret = i_ndi_config_node(proto, DS_INITIALIZED, 0);
1626 	ndi_devi_exit(parent, circ);
1627 
1628 	return (ret);
1629 }
1630 
1631 /*
1632  * ddi_uninitchild: transform node down to DS_BOUND state
1633  */
1634 int
1635 ddi_uninitchild(dev_info_t *dip)
1636 {
1637 	int ret, circ;
1638 	dev_info_t *parent = ddi_get_parent(dip);
1639 	ASSERT(parent);
1640 
1641 	ndi_devi_enter(parent, &circ);
1642 	ret = i_ndi_unconfig_node(dip, DS_BOUND, 0);
1643 	ndi_devi_exit(parent, circ);
1644 
1645 	return (ret);
1646 }
1647 
1648 /*
1649  * i_ddi_attachchild: transform node to DS_READY/i_ddi_devi_attached() state
1650  */
1651 static int
1652 i_ddi_attachchild(dev_info_t *dip)
1653 {
1654 	dev_info_t	*parent = ddi_get_parent(dip);
1655 	int		ret;
1656 
1657 	ASSERT(parent && DEVI_BUSY_OWNED(parent));
1658 
1659 	if ((i_ddi_node_state(dip) < DS_BOUND) || DEVI_IS_DEVICE_OFFLINE(dip))
1660 		return (DDI_FAILURE);
1661 
1662 	ret = i_ndi_config_node(dip, DS_READY, 0);
1663 	if (ret == NDI_SUCCESS) {
1664 		ret = DDI_SUCCESS;
1665 	} else {
1666 		/*
1667 		 * Take it down to DS_INITIALIZED so pm_pre_probe is run
1668 		 * on the next attach
1669 		 */
1670 		(void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0);
1671 		ret = DDI_FAILURE;
1672 	}
1673 
1674 	return (ret);
1675 }
1676 
1677 /*
1678  * i_ddi_detachchild: transform node down to DS_PROBED state
1679  *	If it fails, put it back to DS_READY state.
1680  * NOTE: A node that fails detach may be at DS_ATTACHED instead
1681  * of DS_READY for a small amount of time - this is the source of
1682  * transient DS_READY->DS_ATTACHED->DS_READY state changes.
1683  */
1684 static int
1685 i_ddi_detachchild(dev_info_t *dip, uint_t flags)
1686 {
1687 	dev_info_t	*parent = ddi_get_parent(dip);
1688 	int		ret;
1689 
1690 	ASSERT(parent && DEVI_BUSY_OWNED(parent));
1691 
1692 	ret = i_ndi_unconfig_node(dip, DS_PROBED, flags);
1693 	if (ret != DDI_SUCCESS)
1694 		(void) i_ndi_config_node(dip, DS_READY, 0);
1695 	else
1696 		/* allow pm_pre_probe to reestablish pm state */
1697 		(void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0);
1698 	return (ret);
1699 }
1700 
1701 /*
1702  * Add a child and bind to driver
1703  */
1704 dev_info_t *
1705 ddi_add_child(dev_info_t *pdip, char *name, uint_t nodeid, uint_t unit)
1706 {
1707 	int circ;
1708 	dev_info_t *dip;
1709 
1710 	/* allocate a new node */
1711 	dip = i_ddi_alloc_node(pdip, name, nodeid, (int)unit, NULL, KM_SLEEP);
1712 
1713 	ndi_devi_enter(pdip, &circ);
1714 	(void) i_ndi_config_node(dip, DS_BOUND, 0);
1715 	ndi_devi_exit(pdip, circ);
1716 	return (dip);
1717 }
1718 
1719 /*
1720  * ddi_remove_child: remove the dip. The parent must be attached and held
1721  */
1722 int
1723 ddi_remove_child(dev_info_t *dip, int dummy)
1724 {
1725 	_NOTE(ARGUNUSED(dummy))
1726 	int circ, ret;
1727 	dev_info_t *parent = ddi_get_parent(dip);
1728 	ASSERT(parent);
1729 
1730 	ndi_devi_enter(parent, &circ);
1731 
1732 	/*
1733 	 * If we still have children, for example SID nodes marked
1734 	 * as persistent but not attached, attempt to remove them.
1735 	 */
1736 	if (DEVI(dip)->devi_child) {
1737 		ret = ndi_devi_unconfig(dip, NDI_DEVI_REMOVE);
1738 		if (ret != NDI_SUCCESS) {
1739 			ndi_devi_exit(parent, circ);
1740 			return (DDI_FAILURE);
1741 		}
1742 		ASSERT(DEVI(dip)->devi_child == NULL);
1743 	}
1744 
1745 	ret = i_ndi_unconfig_node(dip, DS_PROTO, 0);
1746 	ndi_devi_exit(parent, circ);
1747 
1748 	if (ret != DDI_SUCCESS)
1749 		return (ret);
1750 
1751 	ASSERT(i_ddi_node_state(dip) == DS_PROTO);
1752 	i_ddi_free_node(dip);
1753 	return (DDI_SUCCESS);
1754 }
1755 
1756 /*
1757  * NDI wrappers for ref counting, node allocation, and transitions
1758  */
1759 
1760 /*
1761  * Hold/release the devinfo node itself.
1762  * Caller is assumed to prevent the devi from detaching during this call
1763  */
1764 void
1765 ndi_hold_devi(dev_info_t *dip)
1766 {
1767 	mutex_enter(&DEVI(dip)->devi_lock);
1768 	ASSERT(DEVI(dip)->devi_ref >= 0);
1769 	DEVI(dip)->devi_ref++;
1770 	membar_enter();			/* make sure stores are flushed */
1771 	mutex_exit(&DEVI(dip)->devi_lock);
1772 }
1773 
1774 void
1775 ndi_rele_devi(dev_info_t *dip)
1776 {
1777 	ASSERT(DEVI(dip)->devi_ref > 0);
1778 
1779 	mutex_enter(&DEVI(dip)->devi_lock);
1780 	DEVI(dip)->devi_ref--;
1781 	membar_enter();			/* make sure stores are flushed */
1782 	mutex_exit(&DEVI(dip)->devi_lock);
1783 }
1784 
1785 int
1786 e_ddi_devi_holdcnt(dev_info_t *dip)
1787 {
1788 	return (DEVI(dip)->devi_ref);
1789 }
1790 
1791 /*
1792  * Hold/release the driver the devinfo node is bound to.
1793  */
1794 struct dev_ops *
1795 ndi_hold_driver(dev_info_t *dip)
1796 {
1797 	if (i_ddi_node_state(dip) < DS_BOUND)
1798 		return (NULL);
1799 
1800 	ASSERT(DEVI(dip)->devi_major != -1);
1801 	return (mod_hold_dev_by_major(DEVI(dip)->devi_major));
1802 }
1803 
1804 void
1805 ndi_rele_driver(dev_info_t *dip)
1806 {
1807 	ASSERT(i_ddi_node_state(dip) >= DS_BOUND);
1808 	mod_rele_dev_by_major(DEVI(dip)->devi_major);
1809 }
1810 
1811 /*
1812  * Single thread entry into devinfo node for modifying its children (devinfo,
1813  * pathinfo, and minor). To verify in ASSERTS use DEVI_BUSY_OWNED macro.
1814  */
1815 void
1816 ndi_devi_enter(dev_info_t *dip, int *circular)
1817 {
1818 	struct dev_info *devi = DEVI(dip);
1819 	ASSERT(dip != NULL);
1820 
1821 	/* for vHCI, enforce (vHCI, pHCI) ndi_deve_enter() order */
1822 	ASSERT(!MDI_VHCI(dip) || (mdi_devi_pdip_entered(dip) == 0) ||
1823 	    DEVI_BUSY_OWNED(dip));
1824 
1825 	mutex_enter(&devi->devi_lock);
1826 	if (devi->devi_busy_thread == curthread) {
1827 		devi->devi_circular++;
1828 	} else {
1829 		while (DEVI_BUSY_CHANGING(devi) && !panicstr)
1830 			cv_wait(&(devi->devi_cv), &(devi->devi_lock));
1831 		if (panicstr) {
1832 			mutex_exit(&devi->devi_lock);
1833 			return;
1834 		}
1835 		devi->devi_flags |= DEVI_BUSY;
1836 		devi->devi_busy_thread = curthread;
1837 	}
1838 	*circular = devi->devi_circular;
1839 	mutex_exit(&devi->devi_lock);
1840 }
1841 
1842 /*
1843  * Release ndi_devi_enter or successful ndi_devi_tryenter.
1844  */
1845 void
1846 ndi_devi_exit(dev_info_t *dip, int circular)
1847 {
1848 	struct dev_info	*devi = DEVI(dip);
1849 	struct dev_info	*vdevi;
1850 	ASSERT(dip != NULL);
1851 
1852 	if (panicstr)
1853 		return;
1854 
1855 	mutex_enter(&(devi->devi_lock));
1856 	if (circular != 0) {
1857 		devi->devi_circular--;
1858 	} else {
1859 		devi->devi_flags &= ~DEVI_BUSY;
1860 		ASSERT(devi->devi_busy_thread == curthread);
1861 		devi->devi_busy_thread = NULL;
1862 		cv_broadcast(&(devi->devi_cv));
1863 	}
1864 	mutex_exit(&(devi->devi_lock));
1865 
1866 	/*
1867 	 * For pHCI exit we issue a broadcast to vHCI for ndi_devi_config_one()
1868 	 * doing cv_wait on vHCI.
1869 	 */
1870 	if (MDI_PHCI(dip)) {
1871 		vdevi = DEVI(mdi_devi_get_vdip(dip));
1872 		if (vdevi) {
1873 			mutex_enter(&(vdevi->devi_lock));
1874 			if (vdevi->devi_flags & DEVI_PHCI_SIGNALS_VHCI) {
1875 				vdevi->devi_flags &= ~DEVI_PHCI_SIGNALS_VHCI;
1876 				cv_broadcast(&(vdevi->devi_cv));
1877 			}
1878 			mutex_exit(&(vdevi->devi_lock));
1879 		}
1880 	}
1881 }
1882 
1883 /*
1884  * Release ndi_devi_enter and wait for possibility of new children, avoiding
1885  * possibility of missing broadcast before getting to cv_timedwait().
1886  */
1887 static void
1888 ndi_devi_exit_and_wait(dev_info_t *dip, int circular, clock_t end_time)
1889 {
1890 	struct dev_info	*devi = DEVI(dip);
1891 	ASSERT(dip != NULL);
1892 
1893 	if (panicstr)
1894 		return;
1895 
1896 	/*
1897 	 * We are called to wait for of a new child, and new child can
1898 	 * only be added if circular is zero.
1899 	 */
1900 	ASSERT(circular == 0);
1901 
1902 	/* like ndi_devi_exit with circular of zero */
1903 	mutex_enter(&(devi->devi_lock));
1904 	devi->devi_flags &= ~DEVI_BUSY;
1905 	ASSERT(devi->devi_busy_thread == curthread);
1906 	devi->devi_busy_thread = NULL;
1907 	cv_broadcast(&(devi->devi_cv));
1908 
1909 	/* now wait for new children while still holding devi_lock */
1910 	(void) cv_timedwait(&devi->devi_cv, &(devi->devi_lock), end_time);
1911 	mutex_exit(&(devi->devi_lock));
1912 }
1913 
1914 /*
1915  * Attempt to single thread entry into devinfo node for modifying its children.
1916  */
1917 int
1918 ndi_devi_tryenter(dev_info_t *dip, int *circular)
1919 {
1920 	int rval = 1;		   /* assume we enter */
1921 	struct dev_info *devi = DEVI(dip);
1922 	ASSERT(dip != NULL);
1923 
1924 	mutex_enter(&devi->devi_lock);
1925 	if (devi->devi_busy_thread == (void *)curthread) {
1926 		devi->devi_circular++;
1927 	} else {
1928 		if (!DEVI_BUSY_CHANGING(devi)) {
1929 			devi->devi_flags |= DEVI_BUSY;
1930 			devi->devi_busy_thread = (void *)curthread;
1931 		} else {
1932 			rval = 0;	/* devi is busy */
1933 		}
1934 	}
1935 	*circular = devi->devi_circular;
1936 	mutex_exit(&devi->devi_lock);
1937 	return (rval);
1938 }
1939 
1940 /*
1941  * Allocate and initialize a new dev_info structure.
1942  *
1943  * This routine may be called at interrupt time by a nexus in
1944  * response to a hotplug event, therefore memory allocations are
1945  * not allowed to sleep.
1946  */
1947 int
1948 ndi_devi_alloc(dev_info_t *parent, char *node_name, pnode_t nodeid,
1949     dev_info_t **ret_dip)
1950 {
1951 	ASSERT(node_name != NULL);
1952 	ASSERT(ret_dip != NULL);
1953 
1954 	*ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL,
1955 	    KM_NOSLEEP);
1956 	if (*ret_dip == NULL) {
1957 		return (NDI_NOMEM);
1958 	}
1959 
1960 	return (NDI_SUCCESS);
1961 }
1962 
1963 /*
1964  * Allocate and initialize a new dev_info structure
1965  * This routine may sleep and should not be called at interrupt time
1966  */
1967 void
1968 ndi_devi_alloc_sleep(dev_info_t *parent, char *node_name, pnode_t nodeid,
1969     dev_info_t **ret_dip)
1970 {
1971 	ASSERT(node_name != NULL);
1972 	ASSERT(ret_dip != NULL);
1973 
1974 	*ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL,
1975 	    KM_SLEEP);
1976 	ASSERT(*ret_dip);
1977 }
1978 
1979 /*
1980  * Remove an initialized (but not yet attached) dev_info
1981  * node from it's parent.
1982  */
1983 int
1984 ndi_devi_free(dev_info_t *dip)
1985 {
1986 	ASSERT(dip != NULL);
1987 
1988 	if (i_ddi_node_state(dip) >= DS_INITIALIZED)
1989 		return (DDI_FAILURE);
1990 
1991 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_free: %s%d (%p)\n",
1992 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
1993 
1994 	(void) ddi_remove_child(dip, 0);
1995 
1996 	return (NDI_SUCCESS);
1997 }
1998 
1999 /*
2000  * ndi_devi_bind_driver() binds a driver to a given device. If it fails
2001  * to bind the driver, it returns an appropriate error back. Some drivers
2002  * may want to know if the actually failed to bind.
2003  */
2004 int
2005 ndi_devi_bind_driver(dev_info_t *dip, uint_t flags)
2006 {
2007 	int ret = NDI_FAILURE;
2008 	int circ;
2009 	dev_info_t *pdip = ddi_get_parent(dip);
2010 	ASSERT(pdip);
2011 
2012 	NDI_CONFIG_DEBUG((CE_CONT,
2013 	    "ndi_devi_bind_driver: %s%d (%p) flags: %x\n",
2014 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
2015 
2016 	ndi_devi_enter(pdip, &circ);
2017 	if (i_ndi_config_node(dip, DS_BOUND, flags) == DDI_SUCCESS)
2018 		ret = NDI_SUCCESS;
2019 	ndi_devi_exit(pdip, circ);
2020 
2021 	return (ret);
2022 }
2023 
2024 /*
2025  * ndi_devi_unbind_driver: unbind the dip
2026  */
2027 static int
2028 ndi_devi_unbind_driver(dev_info_t *dip)
2029 {
2030 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
2031 
2032 	return (i_ndi_unconfig_node(dip, DS_LINKED, 0));
2033 }
2034 
2035 /*
2036  * Misc. help routines called by framework only
2037  */
2038 
2039 /*
2040  * Get the state of node
2041  */
2042 ddi_node_state_t
2043 i_ddi_node_state(dev_info_t *dip)
2044 {
2045 	return (DEVI(dip)->devi_node_state);
2046 }
2047 
2048 /*
2049  * Set the state of node
2050  */
2051 void
2052 i_ddi_set_node_state(dev_info_t *dip, ddi_node_state_t state)
2053 {
2054 	DEVI(dip)->devi_node_state = state;
2055 	membar_enter();			/* make sure stores are flushed */
2056 }
2057 
2058 /*
2059  * Determine if node is attached. The implementation accommodates transient
2060  * DS_READY->DS_ATTACHED->DS_READY state changes.  Outside this file, this
2061  * function should be instead of i_ddi_node_state() DS_ATTACHED/DS_READY
2062  * state checks.
2063  */
2064 int
2065 i_ddi_devi_attached(dev_info_t *dip)
2066 {
2067 	return (DEVI(dip)->devi_node_state >= DS_ATTACHED);
2068 }
2069 
2070 /*
2071  * Common function for finding a node in a sibling list given name and addr.
2072  *
2073  * By default, name is matched with devi_node_name. The following
2074  * alternative match strategies are supported:
2075  *
2076  *	FIND_NODE_BY_NODENAME: Match on node name - typical use.
2077  *	FIND_NODE_BY_DRIVER: A match on driver name bound to node is conducted.
2078  *		This support is used for support of OBP generic names and
2079  *		for the conversion from driver names to generic names. When
2080  *		more consistency in the generic name environment is achieved
2081  *		(and not needed for upgrade) this support can be removed.
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 
6608 	if ((major >= devcnt) || (instance == -1))
6609 		return (NULL);
6610 
6611 	/* try to find the instance in the per driver list */
6612 	dnp = &(devnamesp[major]);
6613 	LOCK_DEV_OPS(&(dnp->dn_lock));
6614 	for (dip = dnp->dn_head; dip;
6615 	    dip = (dev_info_t *)DEVI(dip)->devi_next) {
6616 		/* skip node if instance field is not valid */
6617 		if (i_ddi_node_state(dip) < DS_INITIALIZED)
6618 			continue;
6619 
6620 		/* look for instance match */
6621 		if (DEVI(dip)->devi_instance == instance) {
6622 			/*
6623 			 * To accommodate callers that can't block in
6624 			 * ndi_devi_enter() we do an ndi_devi_hold(), and
6625 			 * afterwards check that the node is in a state where
6626 			 * the hold prevents detach(). If we did not manage to
6627 			 * prevent detach then we ndi_rele_devi() and perform
6628 			 * the slow path below (which can result in a blocking
6629 			 * ndi_devi_enter() while driving attach top-down).
6630 			 * This code depends on the ordering of
6631 			 * DEVI_SET_DETACHING and the devi_ref check in the
6632 			 * detach_node() code path.
6633 			 */
6634 			ndi_hold_devi(dip);
6635 			if (i_ddi_devi_attached(dip) &&
6636 			    !DEVI_IS_DETACHING(dip)) {
6637 				UNLOCK_DEV_OPS(&(dnp->dn_lock));
6638 				return (dip);	/* fast-path with devi held */
6639 			}
6640 			ndi_rele_devi(dip);
6641 
6642 			/* try slow-path */
6643 			dip = NULL;
6644 			break;
6645 		}
6646 	}
6647 	ASSERT(dip == NULL);
6648 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
6649 
6650 	if (flags & E_DDI_HOLD_DEVI_NOATTACH)
6651 		return (NULL);		/* told not to drive attach */
6652 
6653 	/* slow-path may block, so it should not occur from interrupt */
6654 	ASSERT(!servicing_interrupt());
6655 	if (servicing_interrupt())
6656 		return (NULL);
6657 
6658 	/* reconstruct the path and drive attach by path through devfs. */
6659 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
6660 	if (e_ddi_majorinstance_to_path(major, instance, path) == 0)
6661 		dip = e_ddi_hold_devi_by_path(path, flags);
6662 	kmem_free(path, MAXPATHLEN);
6663 	return (dip);			/* with devi held */
6664 }
6665 
6666 /*
6667  * The {e_}ddi_hold_devi{_by_{instance|dev|path}} hold the devinfo node
6668  * associated with the specified arguments.  This hold should be released
6669  * by calling ddi_release_devi.
6670  *
6671  * The E_DDI_HOLD_DEVI_NOATTACH flag argument allows the caller to to specify
6672  * a failure return if the node is not already attached.
6673  *
6674  * NOTE: by the time we make e_ddi_hold_devi public, we should be able to reuse
6675  * ddi_hold_devi again.
6676  */
6677 dev_info_t *
6678 ddi_hold_devi_by_instance(major_t major, int instance, int flags)
6679 {
6680 	return (hold_devi(major, instance, flags));
6681 }
6682 
6683 dev_info_t *
6684 e_ddi_hold_devi_by_dev(dev_t dev, int flags)
6685 {
6686 	major_t	major = getmajor(dev);
6687 	dev_info_t	*dip;
6688 	struct dev_ops	*ops;
6689 	dev_info_t	*ddip = NULL;
6690 
6691 	dip = hold_devi(major, dev_to_instance(dev), flags);
6692 
6693 	/*
6694 	 * The rest of this routine is legacy support for drivers that
6695 	 * have broken DDI_INFO_DEVT2INSTANCE implementations but may have
6696 	 * functional DDI_INFO_DEVT2DEVINFO implementations.  This code will
6697 	 * diagnose inconsistency and, for maximum compatibility with legacy
6698 	 * drivers, give preference to the drivers DDI_INFO_DEVT2DEVINFO
6699 	 * implementation over the above derived dip based the driver's
6700 	 * DDI_INFO_DEVT2INSTANCE implementation. This legacy support should
6701 	 * be removed when DDI_INFO_DEVT2DEVINFO is deprecated.
6702 	 *
6703 	 * NOTE: The following code has a race condition. DEVT2DEVINFO
6704 	 *	returns a dip which is not held. By the time we ref ddip,
6705 	 *	it could have been freed. The saving grace is that for
6706 	 *	most drivers, the dip returned from hold_devi() is the
6707 	 *	same one as the one returned by DEVT2DEVINFO, so we are
6708 	 *	safe for drivers with the correct getinfo(9e) impl.
6709 	 */
6710 	if (((ops = ddi_hold_driver(major)) != NULL) &&
6711 	    CB_DRV_INSTALLED(ops) && ops->devo_getinfo)  {
6712 		if ((*ops->devo_getinfo)(NULL, DDI_INFO_DEVT2DEVINFO,
6713 		    (void *)dev, (void **)&ddip) != DDI_SUCCESS)
6714 			ddip = NULL;
6715 	}
6716 
6717 	/* give preference to the driver returned DEVT2DEVINFO dip */
6718 	if (ddip && (dip != ddip)) {
6719 #ifdef	DEBUG
6720 		cmn_err(CE_WARN, "%s: inconsistent getinfo(9E) implementation",
6721 		    ddi_driver_name(ddip));
6722 #endif	/* DEBUG */
6723 		ndi_hold_devi(ddip);
6724 		if (dip)
6725 			ndi_rele_devi(dip);
6726 		dip = ddip;
6727 	}
6728 
6729 	if (ops)
6730 		ddi_rele_driver(major);
6731 
6732 	return (dip);
6733 }
6734 
6735 /*
6736  * For compatibility only. Do not call this function!
6737  */
6738 dev_info_t *
6739 e_ddi_get_dev_info(dev_t dev, vtype_t type)
6740 {
6741 	dev_info_t *dip = NULL;
6742 	if (getmajor(dev) >= devcnt)
6743 		return (NULL);
6744 
6745 	switch (type) {
6746 	case VCHR:
6747 	case VBLK:
6748 		dip = e_ddi_hold_devi_by_dev(dev, 0);
6749 	default:
6750 		break;
6751 	}
6752 
6753 	/*
6754 	 * For compatibility reasons, we can only return the dip with
6755 	 * the driver ref count held. This is not a safe thing to do.
6756 	 * For certain broken third-party software, we are willing
6757 	 * to venture into unknown territory.
6758 	 */
6759 	if (dip) {
6760 		(void) ndi_hold_driver(dip);
6761 		ndi_rele_devi(dip);
6762 	}
6763 	return (dip);
6764 }
6765 
6766 dev_info_t *
6767 e_ddi_hold_devi_by_path(char *path, int flags)
6768 {
6769 	dev_info_t	*dip;
6770 
6771 	/* can't specify NOATTACH by path */
6772 	ASSERT(!(flags & E_DDI_HOLD_DEVI_NOATTACH));
6773 
6774 	return (resolve_pathname(path, &dip, NULL, NULL) ? NULL : dip);
6775 }
6776 
6777 void
6778 e_ddi_hold_devi(dev_info_t *dip)
6779 {
6780 	ndi_hold_devi(dip);
6781 }
6782 
6783 void
6784 ddi_release_devi(dev_info_t *dip)
6785 {
6786 	ndi_rele_devi(dip);
6787 }
6788 
6789 /*
6790  * Associate a streams queue with a devinfo node
6791  * NOTE: This function is called by STREAM driver's put procedure.
6792  *	It cannot block.
6793  */
6794 void
6795 ddi_assoc_queue_with_devi(queue_t *q, dev_info_t *dip)
6796 {
6797 	queue_t *rq = _RD(q);
6798 	struct stdata *stp;
6799 	vnode_t *vp;
6800 
6801 	/* set flag indicating that ddi_assoc_queue_with_devi was called */
6802 	mutex_enter(QLOCK(rq));
6803 	rq->q_flag |= _QASSOCIATED;
6804 	mutex_exit(QLOCK(rq));
6805 
6806 	/* get the vnode associated with the queue */
6807 	stp = STREAM(rq);
6808 	vp = stp->sd_vnode;
6809 	ASSERT(vp);
6810 
6811 	/* change the hardware association of the vnode */
6812 	spec_assoc_vp_with_devi(vp, dip);
6813 }
6814 
6815 /*
6816  * ddi_install_driver(name)
6817  *
6818  * Driver installation is currently a byproduct of driver loading.  This
6819  * may change.
6820  */
6821 int
6822 ddi_install_driver(char *name)
6823 {
6824 	major_t major = ddi_name_to_major(name);
6825 
6826 	if ((major == DDI_MAJOR_T_NONE) ||
6827 	    (ddi_hold_installed_driver(major) == NULL)) {
6828 		return (DDI_FAILURE);
6829 	}
6830 	ddi_rele_driver(major);
6831 	return (DDI_SUCCESS);
6832 }
6833 
6834 struct dev_ops *
6835 ddi_hold_driver(major_t major)
6836 {
6837 	return (mod_hold_dev_by_major(major));
6838 }
6839 
6840 
6841 void
6842 ddi_rele_driver(major_t major)
6843 {
6844 	mod_rele_dev_by_major(major);
6845 }
6846 
6847 
6848 /*
6849  * This is called during boot to force attachment order of special dips
6850  * dip must be referenced via ndi_hold_devi()
6851  */
6852 int
6853 i_ddi_attach_node_hierarchy(dev_info_t *dip)
6854 {
6855 	dev_info_t	*parent;
6856 	int		ret, circ;
6857 
6858 	/*
6859 	 * Recurse up until attached parent is found.
6860 	 */
6861 	if (i_ddi_devi_attached(dip))
6862 		return (DDI_SUCCESS);
6863 	parent = ddi_get_parent(dip);
6864 	if (i_ddi_attach_node_hierarchy(parent) != DDI_SUCCESS)
6865 		return (DDI_FAILURE);
6866 
6867 	/*
6868 	 * Come top-down, expanding .conf nodes under this parent
6869 	 * and driving attach.
6870 	 */
6871 	ndi_devi_enter(parent, &circ);
6872 	(void) i_ndi_make_spec_children(parent, 0);
6873 	ret = i_ddi_attachchild(dip);
6874 	ndi_devi_exit(parent, circ);
6875 
6876 	return (ret);
6877 }
6878 
6879 /* keep this function static */
6880 static int
6881 attach_driver_nodes(major_t major)
6882 {
6883 	struct devnames *dnp;
6884 	dev_info_t *dip;
6885 	int error = DDI_FAILURE;
6886 
6887 	dnp = &devnamesp[major];
6888 	LOCK_DEV_OPS(&dnp->dn_lock);
6889 	dip = dnp->dn_head;
6890 	while (dip) {
6891 		ndi_hold_devi(dip);
6892 		UNLOCK_DEV_OPS(&dnp->dn_lock);
6893 		if (i_ddi_attach_node_hierarchy(dip) == DDI_SUCCESS)
6894 			error = DDI_SUCCESS;
6895 		LOCK_DEV_OPS(&dnp->dn_lock);
6896 		ndi_rele_devi(dip);
6897 		dip = ddi_get_next(dip);
6898 	}
6899 	if (error == DDI_SUCCESS)
6900 		dnp->dn_flags |= DN_NO_AUTODETACH;
6901 	UNLOCK_DEV_OPS(&dnp->dn_lock);
6902 
6903 
6904 	return (error);
6905 }
6906 
6907 /*
6908  * i_ddi_attach_hw_nodes configures and attaches all hw nodes
6909  * bound to a specific driver. This function replaces calls to
6910  * ddi_hold_installed_driver() for drivers with no .conf
6911  * enumerated nodes.
6912  *
6913  * This facility is typically called at boot time to attach
6914  * platform-specific hardware nodes, such as ppm nodes on xcal
6915  * and grover and keyswitch nodes on cherrystone. It does not
6916  * deal with .conf enumerated node. Calling it beyond the boot
6917  * process is strongly discouraged.
6918  */
6919 int
6920 i_ddi_attach_hw_nodes(char *driver)
6921 {
6922 	major_t major;
6923 
6924 	major = ddi_name_to_major(driver);
6925 	if (major == DDI_MAJOR_T_NONE)
6926 		return (DDI_FAILURE);
6927 
6928 	return (attach_driver_nodes(major));
6929 }
6930 
6931 /*
6932  * i_ddi_attach_pseudo_node configures pseudo drivers which
6933  * has a single node. The .conf nodes must be enumerated
6934  * before calling this interface. The dip is held attached
6935  * upon returning.
6936  *
6937  * This facility should only be called only at boot time
6938  * by the I/O framework.
6939  */
6940 dev_info_t *
6941 i_ddi_attach_pseudo_node(char *driver)
6942 {
6943 	major_t major;
6944 	dev_info_t *dip;
6945 
6946 	major = ddi_name_to_major(driver);
6947 	if (major == DDI_MAJOR_T_NONE)
6948 		return (NULL);
6949 
6950 	if (attach_driver_nodes(major) != DDI_SUCCESS)
6951 		return (NULL);
6952 
6953 	dip = devnamesp[major].dn_head;
6954 	ASSERT(dip && ddi_get_next(dip) == NULL);
6955 	ndi_hold_devi(dip);
6956 	return (dip);
6957 }
6958 
6959 static void
6960 diplist_to_parent_major(dev_info_t *head, char parents[])
6961 {
6962 	major_t major;
6963 	dev_info_t *dip, *pdip;
6964 
6965 	for (dip = head; dip != NULL; dip = ddi_get_next(dip)) {
6966 		pdip = ddi_get_parent(dip);
6967 		ASSERT(pdip);	/* disallow rootnex.conf nodes */
6968 		major = ddi_driver_major(pdip);
6969 		if ((major != DDI_MAJOR_T_NONE) && parents[major] == 0)
6970 			parents[major] = 1;
6971 	}
6972 }
6973 
6974 /*
6975  * Call ddi_hold_installed_driver() on each parent major
6976  * and invoke mt_config_driver() to attach child major.
6977  * This is part of the implementation of ddi_hold_installed_driver.
6978  */
6979 static int
6980 attach_driver_by_parent(major_t child_major, char parents[])
6981 {
6982 	major_t par_major;
6983 	struct mt_config_handle *hdl;
6984 	int flags = NDI_DEVI_PERSIST | NDI_NO_EVENT;
6985 
6986 	hdl = mt_config_init(NULL, NULL, flags, child_major, MT_CONFIG_OP,
6987 	    NULL);
6988 	for (par_major = 0; par_major < devcnt; par_major++) {
6989 		/* disallow recursion on the same driver */
6990 		if (parents[par_major] == 0 || par_major == child_major)
6991 			continue;
6992 		if (ddi_hold_installed_driver(par_major) == NULL)
6993 			continue;
6994 		hdl->mtc_parmajor = par_major;
6995 		mt_config_driver(hdl);
6996 		ddi_rele_driver(par_major);
6997 	}
6998 	(void) mt_config_fini(hdl);
6999 
7000 	return (i_ddi_devs_attached(child_major));
7001 }
7002 
7003 int
7004 i_ddi_devs_attached(major_t major)
7005 {
7006 	dev_info_t *dip;
7007 	struct devnames *dnp;
7008 	int error = DDI_FAILURE;
7009 
7010 	/* check for attached instances */
7011 	dnp = &devnamesp[major];
7012 	LOCK_DEV_OPS(&dnp->dn_lock);
7013 	for (dip = dnp->dn_head; dip != NULL; dip = ddi_get_next(dip)) {
7014 		if (i_ddi_devi_attached(dip)) {
7015 			error = DDI_SUCCESS;
7016 			break;
7017 		}
7018 	}
7019 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7020 
7021 	return (error);
7022 }
7023 
7024 int
7025 i_ddi_minor_node_count(dev_info_t *ddip, const char *node_type)
7026 {
7027 	int			circ;
7028 	struct ddi_minor_data	*dp;
7029 	int			count = 0;
7030 
7031 	ndi_devi_enter(ddip, &circ);
7032 	for (dp = DEVI(ddip)->devi_minor; dp != NULL; dp = dp->next) {
7033 		if (strcmp(dp->ddm_node_type, node_type) == 0)
7034 			count++;
7035 	}
7036 	ndi_devi_exit(ddip, circ);
7037 	return (count);
7038 }
7039 
7040 /*
7041  * ddi_hold_installed_driver configures and attaches all
7042  * instances of the specified driver. To accomplish this
7043  * it configures and attaches all possible parents of
7044  * the driver, enumerated both in h/w nodes and in the
7045  * driver's .conf file.
7046  *
7047  * NOTE: This facility is for compatibility purposes only and will
7048  *	eventually go away. Its usage is strongly discouraged.
7049  */
7050 static void
7051 enter_driver(struct devnames *dnp)
7052 {
7053 	mutex_enter(&dnp->dn_lock);
7054 	ASSERT(dnp->dn_busy_thread != curthread);
7055 	while (dnp->dn_flags & DN_DRIVER_BUSY)
7056 		cv_wait(&dnp->dn_wait, &dnp->dn_lock);
7057 	dnp->dn_flags |= DN_DRIVER_BUSY;
7058 	dnp->dn_busy_thread = curthread;
7059 	mutex_exit(&dnp->dn_lock);
7060 }
7061 
7062 static void
7063 exit_driver(struct devnames *dnp)
7064 {
7065 	mutex_enter(&dnp->dn_lock);
7066 	ASSERT(dnp->dn_busy_thread == curthread);
7067 	dnp->dn_flags &= ~DN_DRIVER_BUSY;
7068 	dnp->dn_busy_thread = NULL;
7069 	cv_broadcast(&dnp->dn_wait);
7070 	mutex_exit(&dnp->dn_lock);
7071 }
7072 
7073 struct dev_ops *
7074 ddi_hold_installed_driver(major_t major)
7075 {
7076 	struct dev_ops *ops;
7077 	struct devnames *dnp;
7078 	char *parents;
7079 	int error;
7080 
7081 	ops = ddi_hold_driver(major);
7082 	if (ops == NULL)
7083 		return (NULL);
7084 
7085 	/*
7086 	 * Return immediately if all the attach operations associated
7087 	 * with a ddi_hold_installed_driver() call have already been done.
7088 	 */
7089 	dnp = &devnamesp[major];
7090 	enter_driver(dnp);
7091 	if (dnp->dn_flags & DN_DRIVER_HELD) {
7092 		exit_driver(dnp);
7093 		if (i_ddi_devs_attached(major) == DDI_SUCCESS)
7094 			return (ops);
7095 		ddi_rele_driver(major);
7096 		return (NULL);
7097 	}
7098 
7099 	LOCK_DEV_OPS(&dnp->dn_lock);
7100 	dnp->dn_flags |= (DN_DRIVER_HELD | DN_NO_AUTODETACH);
7101 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7102 
7103 	DCOMPATPRINTF((CE_CONT,
7104 	    "ddi_hold_installed_driver: %s\n", dnp->dn_name));
7105 
7106 	/*
7107 	 * When the driver has no .conf children, it is sufficient
7108 	 * to attach existing nodes in the device tree. Nodes not
7109 	 * enumerated by the OBP are not attached.
7110 	 */
7111 	if (dnp->dn_pl == NULL) {
7112 		if (attach_driver_nodes(major) == DDI_SUCCESS) {
7113 			exit_driver(dnp);
7114 			return (ops);
7115 		}
7116 		exit_driver(dnp);
7117 		ddi_rele_driver(major);
7118 		return (NULL);
7119 	}
7120 
7121 	/*
7122 	 * Driver has .conf nodes. We find all possible parents
7123 	 * and recursively all ddi_hold_installed_driver on the
7124 	 * parent driver; then we invoke ndi_config_driver()
7125 	 * on all possible parent node in parallel to speed up
7126 	 * performance.
7127 	 */
7128 	parents = kmem_zalloc(devcnt * sizeof (char), KM_SLEEP);
7129 
7130 	LOCK_DEV_OPS(&dnp->dn_lock);
7131 	/* find .conf parents */
7132 	(void) impl_parlist_to_major(dnp->dn_pl, parents);
7133 	/* find hw node parents */
7134 	diplist_to_parent_major(dnp->dn_head, parents);
7135 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7136 
7137 	error = attach_driver_by_parent(major, parents);
7138 	kmem_free(parents, devcnt * sizeof (char));
7139 	if (error == DDI_SUCCESS) {
7140 		exit_driver(dnp);
7141 		return (ops);
7142 	}
7143 
7144 	exit_driver(dnp);
7145 	ddi_rele_driver(major);
7146 	return (NULL);
7147 }
7148 
7149 /*
7150  * Default bus_config entry point for nexus drivers
7151  */
7152 int
7153 ndi_busop_bus_config(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
7154     void *arg, dev_info_t **child, clock_t timeout)
7155 {
7156 	major_t major;
7157 
7158 	/*
7159 	 * A timeout of 30 minutes or more is probably a mistake
7160 	 * This is intended to catch uses where timeout is in
7161 	 * the wrong units.  timeout must be in units of ticks.
7162 	 */
7163 	ASSERT(timeout < SEC_TO_TICK(1800));
7164 
7165 	major = DDI_MAJOR_T_NONE;
7166 	switch (op) {
7167 	case BUS_CONFIG_ONE:
7168 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config %s timeout=%ld\n",
7169 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
7170 		    (char *)arg, timeout));
7171 		return (devi_config_one(pdip, (char *)arg, child, flags,
7172 		    timeout));
7173 
7174 	case BUS_CONFIG_DRIVER:
7175 		major = (major_t)(uintptr_t)arg;
7176 		/*FALLTHROUGH*/
7177 	case BUS_CONFIG_ALL:
7178 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config timeout=%ld\n",
7179 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
7180 		    timeout));
7181 		if (timeout > 0) {
7182 			NDI_DEBUG(flags, (CE_CONT,
7183 			    "%s%d: bus config all timeout=%ld\n",
7184 			    ddi_driver_name(pdip), ddi_get_instance(pdip),
7185 			    timeout));
7186 			delay(timeout);
7187 		}
7188 		return (config_immediate_children(pdip, flags, major));
7189 
7190 	default:
7191 		return (NDI_FAILURE);
7192 	}
7193 	/*NOTREACHED*/
7194 }
7195 
7196 /*
7197  * Default busop bus_unconfig handler for nexus drivers
7198  */
7199 int
7200 ndi_busop_bus_unconfig(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
7201     void *arg)
7202 {
7203 	major_t major;
7204 
7205 	major = DDI_MAJOR_T_NONE;
7206 	switch (op) {
7207 	case BUS_UNCONFIG_ONE:
7208 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig %s\n",
7209 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
7210 		    (char *)arg));
7211 		return (devi_unconfig_one(pdip, (char *)arg, flags));
7212 
7213 	case BUS_UNCONFIG_DRIVER:
7214 		major = (major_t)(uintptr_t)arg;
7215 		/*FALLTHROUGH*/
7216 	case BUS_UNCONFIG_ALL:
7217 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig all\n",
7218 		    ddi_driver_name(pdip), ddi_get_instance(pdip)));
7219 		return (unconfig_immediate_children(pdip, NULL, flags, major));
7220 
7221 	default:
7222 		return (NDI_FAILURE);
7223 	}
7224 	/*NOTREACHED*/
7225 }
7226 
7227 /*
7228  * dummy functions to be removed
7229  */
7230 void
7231 impl_rem_dev_props(dev_info_t *dip)
7232 {
7233 	_NOTE(ARGUNUSED(dip))
7234 	/* do nothing */
7235 }
7236 
7237 /*
7238  * Determine if a node is a leaf node. If not sure, return false (0).
7239  */
7240 static int
7241 is_leaf_node(dev_info_t *dip)
7242 {
7243 	major_t major = ddi_driver_major(dip);
7244 
7245 	if (major == DDI_MAJOR_T_NONE)
7246 		return (0);
7247 
7248 	return (devnamesp[major].dn_flags & DN_LEAF_DRIVER);
7249 }
7250 
7251 /*
7252  * Multithreaded [un]configuration
7253  */
7254 static struct mt_config_handle *
7255 mt_config_init(dev_info_t *pdip, dev_info_t **dipp, int flags,
7256     major_t major, int op, struct brevq_node **brevqp)
7257 {
7258 	struct mt_config_handle	*hdl = kmem_alloc(sizeof (*hdl), KM_SLEEP);
7259 
7260 	mutex_init(&hdl->mtc_lock, NULL, MUTEX_DEFAULT, NULL);
7261 	cv_init(&hdl->mtc_cv, NULL, CV_DEFAULT, NULL);
7262 	hdl->mtc_pdip = pdip;
7263 	hdl->mtc_fdip = dipp;
7264 	hdl->mtc_parmajor = DDI_MAJOR_T_NONE;
7265 	hdl->mtc_flags = flags;
7266 	hdl->mtc_major = major;
7267 	hdl->mtc_thr_count = 0;
7268 	hdl->mtc_op = op;
7269 	hdl->mtc_error = 0;
7270 	hdl->mtc_brevqp = brevqp;
7271 
7272 #ifdef DEBUG
7273 	gethrestime(&hdl->start_time);
7274 	hdl->total_time = 0;
7275 #endif /* DEBUG */
7276 
7277 	return (hdl);
7278 }
7279 
7280 #ifdef DEBUG
7281 static int
7282 time_diff_in_msec(timestruc_t start, timestruc_t end)
7283 {
7284 	int	nsec, sec;
7285 
7286 	sec = end.tv_sec - start.tv_sec;
7287 	nsec = end.tv_nsec - start.tv_nsec;
7288 	if (nsec < 0) {
7289 		nsec += NANOSEC;
7290 		sec -= 1;
7291 	}
7292 
7293 	return (sec * (NANOSEC >> 20) + (nsec >> 20));
7294 }
7295 
7296 #endif	/* DEBUG */
7297 
7298 static int
7299 mt_config_fini(struct mt_config_handle *hdl)
7300 {
7301 	int		rv;
7302 #ifdef DEBUG
7303 	int		real_time;
7304 	timestruc_t	end_time;
7305 #endif /* DEBUG */
7306 
7307 	mutex_enter(&hdl->mtc_lock);
7308 	while (hdl->mtc_thr_count > 0)
7309 		cv_wait(&hdl->mtc_cv, &hdl->mtc_lock);
7310 	rv = hdl->mtc_error;
7311 	mutex_exit(&hdl->mtc_lock);
7312 
7313 #ifdef DEBUG
7314 	gethrestime(&end_time);
7315 	real_time = time_diff_in_msec(hdl->start_time, end_time);
7316 	if ((ddidebug & DDI_MTCONFIG) && hdl->mtc_pdip)
7317 		cmn_err(CE_NOTE,
7318 		    "config %s%d: total time %d msec, real time %d msec",
7319 		    ddi_driver_name(hdl->mtc_pdip),
7320 		    ddi_get_instance(hdl->mtc_pdip),
7321 		    hdl->total_time, real_time);
7322 #endif /* DEBUG */
7323 
7324 	cv_destroy(&hdl->mtc_cv);
7325 	mutex_destroy(&hdl->mtc_lock);
7326 	kmem_free(hdl, sizeof (*hdl));
7327 
7328 	return (rv);
7329 }
7330 
7331 struct mt_config_data {
7332 	struct mt_config_handle	*mtc_hdl;
7333 	dev_info_t		*mtc_dip;
7334 	major_t			mtc_major;
7335 	int			mtc_flags;
7336 	struct brevq_node	*mtc_brn;
7337 	struct mt_config_data	*mtc_next;
7338 };
7339 
7340 static void
7341 mt_config_thread(void *arg)
7342 {
7343 	struct mt_config_data	*mcd = (struct mt_config_data *)arg;
7344 	struct mt_config_handle	*hdl = mcd->mtc_hdl;
7345 	dev_info_t		*dip = mcd->mtc_dip;
7346 	dev_info_t		*rdip, **dipp;
7347 	major_t			major = mcd->mtc_major;
7348 	int			flags = mcd->mtc_flags;
7349 	int			rv = 0;
7350 
7351 #ifdef DEBUG
7352 	timestruc_t start_time, end_time;
7353 	gethrestime(&start_time);
7354 #endif /* DEBUG */
7355 
7356 	rdip = NULL;
7357 	dipp = hdl->mtc_fdip ? &rdip : NULL;
7358 
7359 	switch (hdl->mtc_op) {
7360 	case MT_CONFIG_OP:
7361 		rv = devi_config_common(dip, flags, major);
7362 		break;
7363 	case MT_UNCONFIG_OP:
7364 		if (mcd->mtc_brn) {
7365 			struct brevq_node *brevq = NULL;
7366 			rv = devi_unconfig_common(dip, dipp, flags, major,
7367 			    &brevq);
7368 			mcd->mtc_brn->brn_child = brevq;
7369 		} else
7370 			rv = devi_unconfig_common(dip, dipp, flags, major,
7371 			    NULL);
7372 		break;
7373 	}
7374 
7375 	mutex_enter(&hdl->mtc_lock);
7376 #ifdef DEBUG
7377 	gethrestime(&end_time);
7378 	hdl->total_time += time_diff_in_msec(start_time, end_time);
7379 #endif /* DEBUG */
7380 
7381 	if ((rv != NDI_SUCCESS) && (hdl->mtc_error == 0)) {
7382 		hdl->mtc_error = rv;
7383 #ifdef	DEBUG
7384 		if ((ddidebug & DDI_DEBUG) && (major != DDI_MAJOR_T_NONE)) {
7385 			char	*path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7386 
7387 			(void) ddi_pathname(dip, path);
7388 			cmn_err(CE_NOTE, "mt_config_thread: "
7389 			    "op %d.%d.%x at %s failed %d",
7390 			    hdl->mtc_op, major, flags, path, rv);
7391 			kmem_free(path, MAXPATHLEN);
7392 		}
7393 #endif	/* DEBUG */
7394 	}
7395 
7396 	if (hdl->mtc_fdip && *hdl->mtc_fdip == NULL) {
7397 		*hdl->mtc_fdip = rdip;
7398 		rdip = NULL;
7399 	}
7400 
7401 	if (rdip) {
7402 		ASSERT(rv != NDI_SUCCESS);
7403 		ndi_rele_devi(rdip);
7404 	}
7405 
7406 	ndi_rele_devi(dip);
7407 
7408 	if (--hdl->mtc_thr_count == 0)
7409 		cv_broadcast(&hdl->mtc_cv);
7410 	mutex_exit(&hdl->mtc_lock);
7411 	kmem_free(mcd, sizeof (*mcd));
7412 }
7413 
7414 /*
7415  * Multi-threaded config/unconfig of child nexus
7416  */
7417 static void
7418 mt_config_children(struct mt_config_handle *hdl)
7419 {
7420 	dev_info_t		*pdip = hdl->mtc_pdip;
7421 	major_t			major = hdl->mtc_major;
7422 	dev_info_t		*dip;
7423 	int			circ;
7424 	struct brevq_node	*brn;
7425 	struct mt_config_data	*mcd_head = NULL;
7426 	struct mt_config_data	*mcd_tail = NULL;
7427 	struct mt_config_data	*mcd;
7428 #ifdef DEBUG
7429 	timestruc_t		end_time;
7430 
7431 	/* Update total_time in handle */
7432 	gethrestime(&end_time);
7433 	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
7434 #endif
7435 
7436 	ndi_devi_enter(pdip, &circ);
7437 	dip = ddi_get_child(pdip);
7438 	while (dip) {
7439 		if (hdl->mtc_op == MT_UNCONFIG_OP && hdl->mtc_brevqp &&
7440 		    !(DEVI_EVREMOVE(dip)) &&
7441 		    i_ddi_node_state(dip) >= DS_INITIALIZED) {
7442 			/*
7443 			 * Enqueue this dip's deviname.
7444 			 * No need to hold a lock while enqueuing since this
7445 			 * is the only thread doing the enqueue and no one
7446 			 * walks the queue while we are in multithreaded
7447 			 * unconfiguration.
7448 			 */
7449 			brn = brevq_enqueue(hdl->mtc_brevqp, dip, NULL);
7450 		} else
7451 			brn = NULL;
7452 
7453 		/*
7454 		 * Hold the child that we are processing so he does not get
7455 		 * removed. The corrisponding ndi_rele_devi() for children
7456 		 * that are not being skipped is done at the end of
7457 		 * mt_config_thread().
7458 		 */
7459 		ndi_hold_devi(dip);
7460 
7461 		/*
7462 		 * skip leaf nodes and (for configure) nodes not
7463 		 * fully attached.
7464 		 */
7465 		if (is_leaf_node(dip) ||
7466 		    (hdl->mtc_op == MT_CONFIG_OP &&
7467 		    i_ddi_node_state(dip) < DS_READY)) {
7468 			ndi_rele_devi(dip);
7469 			dip = ddi_get_next_sibling(dip);
7470 			continue;
7471 		}
7472 
7473 		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
7474 		mcd->mtc_dip = dip;
7475 		mcd->mtc_hdl = hdl;
7476 		mcd->mtc_brn = brn;
7477 
7478 		/*
7479 		 * Switch a 'driver' operation to an 'all' operation below a
7480 		 * node bound to the driver.
7481 		 */
7482 		if ((major == DDI_MAJOR_T_NONE) ||
7483 		    (major == ddi_driver_major(dip)))
7484 			mcd->mtc_major = DDI_MAJOR_T_NONE;
7485 		else
7486 			mcd->mtc_major = major;
7487 
7488 		/*
7489 		 * The unconfig-driver to unconfig-all conversion above
7490 		 * constitutes an autodetach for NDI_DETACH_DRIVER calls,
7491 		 * set NDI_AUTODETACH.
7492 		 */
7493 		mcd->mtc_flags = hdl->mtc_flags;
7494 		if ((mcd->mtc_flags & NDI_DETACH_DRIVER) &&
7495 		    (hdl->mtc_op == MT_UNCONFIG_OP) &&
7496 		    (major == ddi_driver_major(pdip)))
7497 			mcd->mtc_flags |= NDI_AUTODETACH;
7498 
7499 		mutex_enter(&hdl->mtc_lock);
7500 		hdl->mtc_thr_count++;
7501 		mutex_exit(&hdl->mtc_lock);
7502 
7503 		/*
7504 		 * Add to end of list to process after ndi_devi_exit to avoid
7505 		 * locking differences depending on value of mtc_off.
7506 		 */
7507 		mcd->mtc_next = NULL;
7508 		if (mcd_head == NULL)
7509 			mcd_head = mcd;
7510 		else
7511 			mcd_tail->mtc_next = mcd;
7512 		mcd_tail = mcd;
7513 
7514 		dip = ddi_get_next_sibling(dip);
7515 	}
7516 	ndi_devi_exit(pdip, circ);
7517 
7518 	/* go through the list of held children */
7519 	for (mcd = mcd_head; mcd; mcd = mcd_head) {
7520 		mcd_head = mcd->mtc_next;
7521 		if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
7522 			mt_config_thread(mcd);
7523 		else
7524 			(void) thread_create(NULL, 0, mt_config_thread, mcd,
7525 			    0, &p0, TS_RUN, minclsyspri);
7526 	}
7527 }
7528 
7529 static void
7530 mt_config_driver(struct mt_config_handle *hdl)
7531 {
7532 	major_t			par_major = hdl->mtc_parmajor;
7533 	major_t			major = hdl->mtc_major;
7534 	struct devnames		*dnp = &devnamesp[par_major];
7535 	dev_info_t		*dip;
7536 	struct mt_config_data	*mcd_head = NULL;
7537 	struct mt_config_data	*mcd_tail = NULL;
7538 	struct mt_config_data	*mcd;
7539 #ifdef DEBUG
7540 	timestruc_t		end_time;
7541 
7542 	/* Update total_time in handle */
7543 	gethrestime(&end_time);
7544 	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
7545 #endif
7546 	ASSERT(par_major != DDI_MAJOR_T_NONE);
7547 	ASSERT(major != DDI_MAJOR_T_NONE);
7548 
7549 	LOCK_DEV_OPS(&dnp->dn_lock);
7550 	dip = devnamesp[par_major].dn_head;
7551 	while (dip) {
7552 		/*
7553 		 * Hold the child that we are processing so he does not get
7554 		 * removed. The corrisponding ndi_rele_devi() for children
7555 		 * that are not being skipped is done at the end of
7556 		 * mt_config_thread().
7557 		 */
7558 		ndi_hold_devi(dip);
7559 
7560 		/* skip leaf nodes and nodes not fully attached */
7561 		if (!i_ddi_devi_attached(dip) || is_leaf_node(dip)) {
7562 			ndi_rele_devi(dip);
7563 			dip = ddi_get_next(dip);
7564 			continue;
7565 		}
7566 
7567 		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
7568 		mcd->mtc_dip = dip;
7569 		mcd->mtc_hdl = hdl;
7570 		mcd->mtc_major = major;
7571 		mcd->mtc_flags = hdl->mtc_flags;
7572 
7573 		mutex_enter(&hdl->mtc_lock);
7574 		hdl->mtc_thr_count++;
7575 		mutex_exit(&hdl->mtc_lock);
7576 
7577 		/*
7578 		 * Add to end of list to process after UNLOCK_DEV_OPS to avoid
7579 		 * locking differences depending on value of mtc_off.
7580 		 */
7581 		mcd->mtc_next = NULL;
7582 		if (mcd_head == NULL)
7583 			mcd_head = mcd;
7584 		else
7585 			mcd_tail->mtc_next = mcd;
7586 		mcd_tail = mcd;
7587 
7588 		dip = ddi_get_next(dip);
7589 	}
7590 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7591 
7592 	/* go through the list of held children */
7593 	for (mcd = mcd_head; mcd; mcd = mcd_head) {
7594 		mcd_head = mcd->mtc_next;
7595 		if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
7596 			mt_config_thread(mcd);
7597 		else
7598 			(void) thread_create(NULL, 0, mt_config_thread, mcd,
7599 			    0, &p0, TS_RUN, minclsyspri);
7600 	}
7601 }
7602 
7603 /*
7604  * Given the nodeid for a persistent (PROM or SID) node, return
7605  * the corresponding devinfo node
7606  * NOTE: This function will return NULL for .conf nodeids.
7607  */
7608 dev_info_t *
7609 e_ddi_nodeid_to_dip(pnode_t nodeid)
7610 {
7611 	dev_info_t		*dip = NULL;
7612 	struct devi_nodeid	*prev, *elem;
7613 
7614 	mutex_enter(&devimap->dno_lock);
7615 
7616 	prev = NULL;
7617 	for (elem = devimap->dno_head; elem; elem = elem->next) {
7618 		if (elem->nodeid == nodeid) {
7619 			ndi_hold_devi(elem->dip);
7620 			dip = elem->dip;
7621 			break;
7622 		}
7623 		prev = elem;
7624 	}
7625 
7626 	/*
7627 	 * Move to head for faster lookup next time
7628 	 */
7629 	if (elem && prev) {
7630 		prev->next = elem->next;
7631 		elem->next = devimap->dno_head;
7632 		devimap->dno_head = elem;
7633 	}
7634 
7635 	mutex_exit(&devimap->dno_lock);
7636 	return (dip);
7637 }
7638 
7639 static void
7640 free_cache_task(void *arg)
7641 {
7642 	ASSERT(arg == NULL);
7643 
7644 	mutex_enter(&di_cache.cache_lock);
7645 
7646 	/*
7647 	 * The cache can be invalidated without holding the lock
7648 	 * but it can be made valid again only while the lock is held.
7649 	 * So if the cache is invalid when the lock is held, it will
7650 	 * stay invalid until lock is released.
7651 	 */
7652 	if (!di_cache.cache_valid)
7653 		i_ddi_di_cache_free(&di_cache);
7654 
7655 	mutex_exit(&di_cache.cache_lock);
7656 
7657 	if (di_cache_debug)
7658 		cmn_err(CE_NOTE, "system_taskq: di_cache freed");
7659 }
7660 
7661 extern int modrootloaded;
7662 
7663 void
7664 i_ddi_di_cache_free(struct di_cache *cache)
7665 {
7666 	int	error;
7667 	extern int sys_shutdown;
7668 
7669 	ASSERT(mutex_owned(&cache->cache_lock));
7670 
7671 	if (cache->cache_size) {
7672 		ASSERT(cache->cache_size > 0);
7673 		ASSERT(cache->cache_data);
7674 
7675 		kmem_free(cache->cache_data, cache->cache_size);
7676 		cache->cache_data = NULL;
7677 		cache->cache_size = 0;
7678 
7679 		if (di_cache_debug)
7680 			cmn_err(CE_NOTE, "i_ddi_di_cache_free: freed cachemem");
7681 	} else {
7682 		ASSERT(cache->cache_data == NULL);
7683 		if (di_cache_debug)
7684 			cmn_err(CE_NOTE, "i_ddi_di_cache_free: NULL cache");
7685 	}
7686 
7687 	if (!modrootloaded || rootvp == NULL ||
7688 	    vn_is_readonly(rootvp) || sys_shutdown) {
7689 		if (di_cache_debug) {
7690 			cmn_err(CE_WARN, "/ not mounted/RDONLY. Skip unlink");
7691 		}
7692 		return;
7693 	}
7694 
7695 	error = vn_remove(DI_CACHE_FILE, UIO_SYSSPACE, RMFILE);
7696 	if (di_cache_debug && error && error != ENOENT) {
7697 		cmn_err(CE_WARN, "%s: unlink failed: %d", DI_CACHE_FILE, error);
7698 	} else if (di_cache_debug && !error) {
7699 		cmn_err(CE_NOTE, "i_ddi_di_cache_free: unlinked cache file");
7700 	}
7701 }
7702 
7703 void
7704 i_ddi_di_cache_invalidate(int kmflag)
7705 {
7706 	int	cache_valid;
7707 
7708 	if (!modrootloaded || !i_ddi_io_initialized()) {
7709 		if (di_cache_debug)
7710 			cmn_err(CE_NOTE, "I/O not inited. Skipping invalidate");
7711 		return;
7712 	}
7713 
7714 	/* Increment devtree generation number. */
7715 	atomic_inc_ulong(&devtree_gen);
7716 
7717 	/* Invalidate the in-core cache and dispatch free on valid->invalid */
7718 	cache_valid = atomic_swap_uint(&di_cache.cache_valid, 0);
7719 	if (cache_valid) {
7720 		(void) taskq_dispatch(system_taskq, free_cache_task, NULL,
7721 		    (kmflag == KM_SLEEP) ? TQ_SLEEP : TQ_NOSLEEP);
7722 	}
7723 
7724 	if (di_cache_debug) {
7725 		cmn_err(CE_NOTE, "invalidation with km_flag: %s",
7726 		    kmflag == KM_SLEEP ? "KM_SLEEP" : "KM_NOSLEEP");
7727 	}
7728 }
7729 
7730 
7731 static void
7732 i_bind_vhci_node(dev_info_t *dip)
7733 {
7734 	DEVI(dip)->devi_major = ddi_name_to_major(ddi_node_name(dip));
7735 	i_ddi_set_node_state(dip, DS_BOUND);
7736 }
7737 
7738 static char vhci_node_addr[2];
7739 
7740 static int
7741 i_init_vhci_node(dev_info_t *dip)
7742 {
7743 	add_global_props(dip);
7744 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
7745 	if (DEVI(dip)->devi_ops == NULL)
7746 		return (-1);
7747 
7748 	DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
7749 	e_ddi_keep_instance(dip);
7750 	vhci_node_addr[0]	= '\0';
7751 	ddi_set_name_addr(dip, vhci_node_addr);
7752 	i_ddi_set_node_state(dip, DS_INITIALIZED);
7753 	return (0);
7754 }
7755 
7756 static void
7757 i_link_vhci_node(dev_info_t *dip)
7758 {
7759 	ASSERT(MUTEX_HELD(&global_vhci_lock));
7760 
7761 	/*
7762 	 * scsi_vhci should be kept left most of the device tree.
7763 	 */
7764 	if (scsi_vhci_dip) {
7765 		DEVI(dip)->devi_sibling = DEVI(scsi_vhci_dip)->devi_sibling;
7766 		DEVI(scsi_vhci_dip)->devi_sibling = DEVI(dip);
7767 	} else {
7768 		DEVI(dip)->devi_sibling = DEVI(top_devinfo)->devi_child;
7769 		DEVI(top_devinfo)->devi_child = DEVI(dip);
7770 	}
7771 }
7772 
7773 
7774 /*
7775  * This a special routine to enumerate vhci node (child of rootnex
7776  * node) without holding the ndi_devi_enter() lock. The device node
7777  * is allocated, initialized and brought into DS_READY state before
7778  * inserting into the device tree. The VHCI node is handcrafted
7779  * here to bring the node to DS_READY, similar to rootnex node.
7780  *
7781  * The global_vhci_lock protects linking the node into the device
7782  * as same lock is held before linking/unlinking any direct child
7783  * of rootnex children.
7784  *
7785  * This routine is a workaround to handle a possible deadlock
7786  * that occurs while trying to enumerate node in a different sub-tree
7787  * during _init/_attach entry points.
7788  */
7789 /*ARGSUSED*/
7790 dev_info_t *
7791 ndi_devi_config_vhci(char *drvname, int flags)
7792 {
7793 	struct devnames		*dnp;
7794 	dev_info_t		*dip;
7795 	major_t			major = ddi_name_to_major(drvname);
7796 
7797 	if (major == -1)
7798 		return (NULL);
7799 
7800 	/* Make sure we create the VHCI node only once */
7801 	dnp = &devnamesp[major];
7802 	LOCK_DEV_OPS(&dnp->dn_lock);
7803 	if (dnp->dn_head) {
7804 		dip = dnp->dn_head;
7805 		UNLOCK_DEV_OPS(&dnp->dn_lock);
7806 		return (dip);
7807 	}
7808 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7809 
7810 	/* Allocate the VHCI node */
7811 	ndi_devi_alloc_sleep(top_devinfo, drvname, DEVI_SID_NODEID, &dip);
7812 	ndi_hold_devi(dip);
7813 
7814 	/* Mark the node as VHCI */
7815 	DEVI(dip)->devi_node_attributes |= DDI_VHCI_NODE;
7816 
7817 	i_ddi_add_devimap(dip);
7818 	i_bind_vhci_node(dip);
7819 	if (i_init_vhci_node(dip) == -1) {
7820 		ndi_rele_devi(dip);
7821 		(void) ndi_devi_free(dip);
7822 		return (NULL);
7823 	}
7824 
7825 	mutex_enter(&(DEVI(dip)->devi_lock));
7826 	DEVI_SET_ATTACHING(dip);
7827 	mutex_exit(&(DEVI(dip)->devi_lock));
7828 
7829 	if (devi_attach(dip, DDI_ATTACH) != DDI_SUCCESS) {
7830 		cmn_err(CE_CONT, "Could not attach %s driver", drvname);
7831 		e_ddi_free_instance(dip, vhci_node_addr);
7832 		ndi_rele_devi(dip);
7833 		(void) ndi_devi_free(dip);
7834 		return (NULL);
7835 	}
7836 	mutex_enter(&(DEVI(dip)->devi_lock));
7837 	DEVI_CLR_ATTACHING(dip);
7838 	mutex_exit(&(DEVI(dip)->devi_lock));
7839 
7840 	mutex_enter(&global_vhci_lock);
7841 	i_link_vhci_node(dip);
7842 	mutex_exit(&global_vhci_lock);
7843 	i_ddi_set_node_state(dip, DS_READY);
7844 
7845 	LOCK_DEV_OPS(&dnp->dn_lock);
7846 	dnp->dn_flags |= DN_DRIVER_HELD;
7847 	dnp->dn_head = dip;
7848 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7849 
7850 	i_ndi_devi_report_status_change(dip, NULL);
7851 
7852 	return (dip);
7853 }
7854 
7855 /*
7856  * ibt_hw_is_present() returns 0 when there is no IB hardware actively
7857  * running.  This is primarily useful for modules like rpcmod which
7858  * needs a quick check to decide whether or not it should try to use
7859  * InfiniBand
7860  */
7861 int ib_hw_status = 0;
7862 int
7863 ibt_hw_is_present()
7864 {
7865 	return (ib_hw_status);
7866 }
7867 
7868 /*
7869  * ASSERT that constraint flag is not set and then set the "retire attempt"
7870  * flag.
7871  */
7872 int
7873 e_ddi_mark_retiring(dev_info_t *dip, void *arg)
7874 {
7875 	char	**cons_array = (char **)arg;
7876 	char	*path;
7877 	int	constraint;
7878 	int	i;
7879 
7880 	constraint = 0;
7881 	if (cons_array) {
7882 		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7883 		(void) ddi_pathname(dip, path);
7884 		for (i = 0; cons_array[i] != NULL; i++) {
7885 			if (strcmp(path, cons_array[i]) == 0) {
7886 				constraint = 1;
7887 				break;
7888 			}
7889 		}
7890 		kmem_free(path, MAXPATHLEN);
7891 	}
7892 
7893 	mutex_enter(&DEVI(dip)->devi_lock);
7894 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
7895 	DEVI(dip)->devi_flags |= DEVI_RETIRING;
7896 	if (constraint)
7897 		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
7898 	mutex_exit(&DEVI(dip)->devi_lock);
7899 
7900 	RIO_VERBOSE((CE_NOTE, "marked dip as undergoing retire process dip=%p",
7901 	    (void *)dip));
7902 
7903 	if (constraint)
7904 		RIO_DEBUG((CE_NOTE, "marked dip as constrained, dip=%p",
7905 		    (void *)dip));
7906 
7907 	if (MDI_PHCI(dip))
7908 		mdi_phci_mark_retiring(dip, cons_array);
7909 
7910 	return (DDI_WALK_CONTINUE);
7911 }
7912 
7913 static void
7914 free_array(char **cons_array)
7915 {
7916 	int	i;
7917 
7918 	if (cons_array == NULL)
7919 		return;
7920 
7921 	for (i = 0; cons_array[i] != NULL; i++) {
7922 		kmem_free(cons_array[i], strlen(cons_array[i]) + 1);
7923 	}
7924 	kmem_free(cons_array, (i+1) * sizeof (char *));
7925 }
7926 
7927 /*
7928  * Walk *every* node in subtree and check if it blocks, allows or has no
7929  * comment on a proposed retire.
7930  */
7931 int
7932 e_ddi_retire_notify(dev_info_t *dip, void *arg)
7933 {
7934 	int	*constraint = (int *)arg;
7935 
7936 	RIO_DEBUG((CE_NOTE, "retire notify: dip = %p", (void *)dip));
7937 
7938 	(void) e_ddi_offline_notify(dip);
7939 
7940 	mutex_enter(&(DEVI(dip)->devi_lock));
7941 	if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) {
7942 		RIO_DEBUG((CE_WARN, "retire notify: dip in retire "
7943 		    "subtree is not marked: dip = %p", (void *)dip));
7944 		*constraint = 0;
7945 	} else if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) {
7946 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
7947 		RIO_DEBUG((CE_NOTE, "retire notify: BLOCKED: dip = %p",
7948 		    (void *)dip));
7949 		*constraint = 0;
7950 	} else if (!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT)) {
7951 		RIO_DEBUG((CE_NOTE, "retire notify: NO CONSTRAINT: "
7952 		    "dip = %p", (void *)dip));
7953 		*constraint = 0;
7954 	} else {
7955 		RIO_DEBUG((CE_NOTE, "retire notify: CONSTRAINT set: "
7956 		    "dip = %p", (void *)dip));
7957 	}
7958 	mutex_exit(&DEVI(dip)->devi_lock);
7959 
7960 	if (MDI_PHCI(dip))
7961 		mdi_phci_retire_notify(dip, constraint);
7962 
7963 	return (DDI_WALK_CONTINUE);
7964 }
7965 
7966 int
7967 e_ddi_retire_finalize(dev_info_t *dip, void *arg)
7968 {
7969 	int constraint = *(int *)arg;
7970 	int finalize;
7971 	int phci_only;
7972 
7973 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
7974 
7975 	mutex_enter(&DEVI(dip)->devi_lock);
7976 	if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) {
7977 		RIO_DEBUG((CE_WARN,
7978 		    "retire: unmarked dip(%p) in retire subtree",
7979 		    (void *)dip));
7980 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRED));
7981 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
7982 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
7983 		mutex_exit(&DEVI(dip)->devi_lock);
7984 		return (DDI_WALK_CONTINUE);
7985 	}
7986 
7987 	/*
7988 	 * retire the device if constraints have been applied
7989 	 * or if the device is not in use
7990 	 */
7991 	finalize = 0;
7992 	if (constraint) {
7993 		ASSERT(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT);
7994 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
7995 		DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
7996 		DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
7997 		DEVI(dip)->devi_flags |= DEVI_RETIRED;
7998 		mutex_exit(&DEVI(dip)->devi_lock);
7999 		(void) spec_fence_snode(dip, NULL);
8000 		RIO_DEBUG((CE_NOTE, "Fenced off: dip = %p", (void *)dip));
8001 		e_ddi_offline_finalize(dip, DDI_SUCCESS);
8002 	} else {
8003 		if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) {
8004 			ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
8005 			DEVI(dip)->devi_flags &= ~DEVI_R_BLOCKED;
8006 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
8007 			/* we have already finalized during notify */
8008 		} else if (DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT) {
8009 			DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
8010 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
8011 			finalize = 1;
8012 		} else {
8013 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
8014 			/*
8015 			 * even if no contracts, need to call finalize
8016 			 * to clear the contract barrier on the dip
8017 			 */
8018 			finalize = 1;
8019 		}
8020 		mutex_exit(&DEVI(dip)->devi_lock);
8021 		RIO_DEBUG((CE_NOTE, "finalize: NOT retired: dip = %p",
8022 		    (void *)dip));
8023 		if (finalize)
8024 			e_ddi_offline_finalize(dip, DDI_FAILURE);
8025 	}
8026 
8027 	/*
8028 	 * phci_only variable indicates no client checking, just
8029 	 * offline the PHCI. We set that to 0 to enable client
8030 	 * checking
8031 	 */
8032 	phci_only = 0;
8033 	if (MDI_PHCI(dip))
8034 		mdi_phci_retire_finalize(dip, phci_only);
8035 
8036 	return (DDI_WALK_CONTINUE);
8037 }
8038 
8039 /*
8040  * Returns
8041  * 	DDI_SUCCESS if constraints allow retire
8042  *	DDI_FAILURE if constraints don't allow retire.
8043  * cons_array is a NULL terminated array of node paths for
8044  * which constraints have already been applied.
8045  */
8046 int
8047 e_ddi_retire_device(char *path, char **cons_array)
8048 {
8049 	dev_info_t	*dip;
8050 	dev_info_t	*pdip;
8051 	int		circ;
8052 	int		circ2;
8053 	int		constraint;
8054 	char		*devnm;
8055 
8056 	/*
8057 	 * First, lookup the device
8058 	 */
8059 	dip = e_ddi_hold_devi_by_path(path, 0);
8060 	if (dip == NULL) {
8061 		/*
8062 		 * device does not exist. This device cannot be
8063 		 * a critical device since it is not in use. Thus
8064 		 * this device is always retireable. Return DDI_SUCCESS
8065 		 * to indicate this. If this device is ever
8066 		 * instantiated, I/O framework will consult the
8067 		 * the persistent retire store, mark it as
8068 		 * retired and fence it off.
8069 		 */
8070 		RIO_DEBUG((CE_NOTE, "Retire device: device doesn't exist."
8071 		    " NOP. Just returning SUCCESS. path=%s", path));
8072 		free_array(cons_array);
8073 		return (DDI_SUCCESS);
8074 	}
8075 
8076 	RIO_DEBUG((CE_NOTE, "Retire device: found dip = %p.", (void *)dip));
8077 
8078 	pdip = ddi_get_parent(dip);
8079 	ndi_hold_devi(pdip);
8080 
8081 	/*
8082 	 * Run devfs_clean() in case dip has no constraints and is
8083 	 * not in use, so is retireable but there are dv_nodes holding
8084 	 * ref-count on the dip. Note that devfs_clean() always returns
8085 	 * success.
8086 	 */
8087 	devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
8088 	(void) ddi_deviname(dip, devnm);
8089 	(void) devfs_clean(pdip, devnm + 1, DV_CLEAN_FORCE);
8090 	kmem_free(devnm, MAXNAMELEN + 1);
8091 
8092 	ndi_devi_enter(pdip, &circ);
8093 
8094 	/* release hold from e_ddi_hold_devi_by_path */
8095 	ndi_rele_devi(dip);
8096 
8097 	/*
8098 	 * If it cannot make a determination, is_leaf_node() assumes
8099 	 * dip is a nexus.
8100 	 */
8101 	(void) e_ddi_mark_retiring(dip, cons_array);
8102 	if (!is_leaf_node(dip)) {
8103 		ndi_devi_enter(dip, &circ2);
8104 		ddi_walk_devs(ddi_get_child(dip), e_ddi_mark_retiring,
8105 		    cons_array);
8106 		ndi_devi_exit(dip, circ2);
8107 	}
8108 	free_array(cons_array);
8109 
8110 	/*
8111 	 * apply constraints
8112 	 */
8113 	RIO_DEBUG((CE_NOTE, "retire: subtree retire notify: path = %s", path));
8114 
8115 	constraint = 1;	/* assume constraints allow retire */
8116 	(void) e_ddi_retire_notify(dip, &constraint);
8117 	if (!is_leaf_node(dip)) {
8118 		ndi_devi_enter(dip, &circ2);
8119 		ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_notify,
8120 		    &constraint);
8121 		ndi_devi_exit(dip, circ2);
8122 	}
8123 
8124 	/*
8125 	 * Now finalize the retire
8126 	 */
8127 	(void) e_ddi_retire_finalize(dip, &constraint);
8128 	if (!is_leaf_node(dip)) {
8129 		ndi_devi_enter(dip, &circ2);
8130 		ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_finalize,
8131 		    &constraint);
8132 		ndi_devi_exit(dip, circ2);
8133 	}
8134 
8135 	if (!constraint) {
8136 		RIO_DEBUG((CE_WARN, "retire failed: path = %s", path));
8137 	} else {
8138 		RIO_DEBUG((CE_NOTE, "retire succeeded: path = %s", path));
8139 	}
8140 
8141 	ndi_devi_exit(pdip, circ);
8142 	ndi_rele_devi(pdip);
8143 	return (constraint ? DDI_SUCCESS : DDI_FAILURE);
8144 }
8145 
8146 static int
8147 unmark_and_unfence(dev_info_t *dip, void *arg)
8148 {
8149 	char	*path = (char *)arg;
8150 
8151 	ASSERT(path);
8152 
8153 	(void) ddi_pathname(dip, path);
8154 
8155 	mutex_enter(&DEVI(dip)->devi_lock);
8156 	DEVI(dip)->devi_flags &= ~DEVI_RETIRED;
8157 	DEVI_SET_DEVICE_ONLINE(dip);
8158 	mutex_exit(&DEVI(dip)->devi_lock);
8159 
8160 	RIO_VERBOSE((CE_NOTE, "Cleared RETIRED flag: dip=%p, path=%s",
8161 	    (void *)dip, path));
8162 
8163 	(void) spec_unfence_snode(dip);
8164 	RIO_DEBUG((CE_NOTE, "Unfenced device: %s", path));
8165 
8166 	if (MDI_PHCI(dip))
8167 		mdi_phci_unretire(dip);
8168 
8169 	return (DDI_WALK_CONTINUE);
8170 }
8171 
8172 struct find_dip {
8173 	char	*fd_buf;
8174 	char	*fd_path;
8175 	dev_info_t *fd_dip;
8176 };
8177 
8178 static int
8179 find_dip_fcn(dev_info_t *dip, void *arg)
8180 {
8181 	struct find_dip *findp = (struct find_dip *)arg;
8182 
8183 	(void) ddi_pathname(dip, findp->fd_buf);
8184 
8185 	if (strcmp(findp->fd_path, findp->fd_buf) != 0)
8186 		return (DDI_WALK_CONTINUE);
8187 
8188 	ndi_hold_devi(dip);
8189 	findp->fd_dip = dip;
8190 
8191 	return (DDI_WALK_TERMINATE);
8192 }
8193 
8194 int
8195 e_ddi_unretire_device(char *path)
8196 {
8197 	int		circ;
8198 	int		circ2;
8199 	char		*path2;
8200 	dev_info_t	*pdip;
8201 	dev_info_t	*dip;
8202 	struct find_dip	 find_dip;
8203 
8204 	ASSERT(path);
8205 	ASSERT(*path == '/');
8206 
8207 	if (strcmp(path, "/") == 0) {
8208 		cmn_err(CE_WARN, "Root node cannot be retired. Skipping "
8209 		    "device unretire: %s", path);
8210 		return (0);
8211 	}
8212 
8213 	/*
8214 	 * We can't lookup the dip (corresponding to path) via
8215 	 * e_ddi_hold_devi_by_path() because the dip may be offline
8216 	 * and may not attach. Use ddi_walk_devs() instead;
8217 	 */
8218 	find_dip.fd_buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8219 	find_dip.fd_path = path;
8220 	find_dip.fd_dip = NULL;
8221 
8222 	pdip = ddi_root_node();
8223 
8224 	ndi_devi_enter(pdip, &circ);
8225 	ddi_walk_devs(ddi_get_child(pdip), find_dip_fcn, &find_dip);
8226 	ndi_devi_exit(pdip, circ);
8227 
8228 	kmem_free(find_dip.fd_buf, MAXPATHLEN);
8229 
8230 	if (find_dip.fd_dip == NULL) {
8231 		cmn_err(CE_WARN, "Device not found in device tree. Skipping "
8232 		    "device unretire: %s", path);
8233 		return (0);
8234 	}
8235 
8236 	dip = find_dip.fd_dip;
8237 
8238 	pdip = ddi_get_parent(dip);
8239 
8240 	ndi_hold_devi(pdip);
8241 
8242 	ndi_devi_enter(pdip, &circ);
8243 
8244 	path2 = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8245 
8246 	(void) unmark_and_unfence(dip, path2);
8247 	if (!is_leaf_node(dip)) {
8248 		ndi_devi_enter(dip, &circ2);
8249 		ddi_walk_devs(ddi_get_child(dip), unmark_and_unfence, path2);
8250 		ndi_devi_exit(dip, circ2);
8251 	}
8252 
8253 	kmem_free(path2, MAXPATHLEN);
8254 
8255 	/* release hold from find_dip_fcn() */
8256 	ndi_rele_devi(dip);
8257 
8258 	ndi_devi_exit(pdip, circ);
8259 
8260 	ndi_rele_devi(pdip);
8261 
8262 	return (0);
8263 }
8264 
8265 /*
8266  * Called before attach on a dip that has been retired.
8267  */
8268 static int
8269 mark_and_fence(dev_info_t *dip, void *arg)
8270 {
8271 	char    *fencepath = (char *)arg;
8272 
8273 	/*
8274 	 * We have already decided to retire this device. The various
8275 	 * constraint checking should not be set.
8276 	 * NOTE that the retire flag may already be set due to
8277 	 * fenced -> detach -> fenced transitions.
8278 	 */
8279 	mutex_enter(&DEVI(dip)->devi_lock);
8280 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
8281 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
8282 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRING));
8283 	DEVI(dip)->devi_flags |= DEVI_RETIRED;
8284 	mutex_exit(&DEVI(dip)->devi_lock);
8285 	RIO_VERBOSE((CE_NOTE, "marked as RETIRED dip=%p", (void *)dip));
8286 
8287 	if (fencepath) {
8288 		(void) spec_fence_snode(dip, NULL);
8289 		RIO_DEBUG((CE_NOTE, "Fenced: %s",
8290 		    ddi_pathname(dip, fencepath)));
8291 	}
8292 
8293 	return (DDI_WALK_CONTINUE);
8294 }
8295 
8296 /*
8297  * Checks the retire database and:
8298  *
8299  * - if device is present in the retire database, marks the device retired
8300  *   and fences it off.
8301  * - if device is not in retire database, allows the device to attach normally
8302  *
8303  * To be called only by framework attach code on first attach attempt.
8304  *
8305  */
8306 static void
8307 i_ddi_check_retire(dev_info_t *dip)
8308 {
8309 	char		*path;
8310 	dev_info_t	*pdip;
8311 	int		circ;
8312 	int		phci_only;
8313 
8314 	pdip = ddi_get_parent(dip);
8315 
8316 	/*
8317 	 * Root dip is treated special and doesn't take this code path.
8318 	 * Also root can never be retired.
8319 	 */
8320 	ASSERT(pdip);
8321 	ASSERT(DEVI_BUSY_OWNED(pdip));
8322 	ASSERT(i_ddi_node_state(dip) < DS_ATTACHED);
8323 
8324 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8325 
8326 	(void) ddi_pathname(dip, path);
8327 
8328 	RIO_VERBOSE((CE_NOTE, "Checking if dip should attach: dip=%p, path=%s",
8329 	    (void *)dip, path));
8330 
8331 	/*
8332 	 * Check if this device is in the "retired" store i.e.  should
8333 	 * be retired. If not, we have nothing to do.
8334 	 */
8335 	if (e_ddi_device_retired(path) == 0) {
8336 		RIO_VERBOSE((CE_NOTE, "device is NOT retired: path=%s", path));
8337 		kmem_free(path, MAXPATHLEN);
8338 		return;
8339 	}
8340 
8341 	RIO_DEBUG((CE_NOTE, "attach: device is retired: path=%s", path));
8342 
8343 	/*
8344 	 * Mark dips and fence off snodes (if any)
8345 	 */
8346 	RIO_DEBUG((CE_NOTE, "attach: Mark and fence subtree: path=%s", path));
8347 	(void) mark_and_fence(dip, path);
8348 	if (!is_leaf_node(dip)) {
8349 		ndi_devi_enter(dip, &circ);
8350 		ddi_walk_devs(ddi_get_child(dip), mark_and_fence, path);
8351 		ndi_devi_exit(dip, circ);
8352 	}
8353 
8354 	kmem_free(path, MAXPATHLEN);
8355 
8356 	/*
8357 	 * We don't want to check the client. We just want to
8358 	 * offline the PHCI
8359 	 */
8360 	phci_only = 1;
8361 	if (MDI_PHCI(dip))
8362 		mdi_phci_retire_finalize(dip, phci_only);
8363 }
8364