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