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