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