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