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