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