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