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