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