xref: /titanic_51/usr/src/uts/common/os/devcfg.c (revision f3680c6ce4e182fbab2b5d7ea0f9989e952b81d2)
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 	/* Inform LDI_EV_DEVICE_REMOVE callbacks. */
4815 	ldi_invoke_finalize(dip, DDI_DEV_T_ANY, 0, LDI_EV_DEVICE_REMOVE,
4816 	    LDI_EV_SUCCESS, NULL);
4817 
4818 	/* Generate EC_DEVFS_DEVI_REMOVE sysevent. */
4819 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4820 	(void) i_log_devfs_remove_devinfo(ddi_pathname(dip, path),
4821 	    i_ddi_devi_class(dip), (char *)ddi_driver_name(dip),
4822 	    ddi_get_instance(dip), 0);
4823 	kmem_free(path, MAXPATHLEN);
4824 }
4825 
4826 static void
4827 i_ddi_log_devfs_device_insert(dev_info_t *dip)
4828 {
4829 	ASSERT(dip && ddi_get_parent(dip) &&
4830 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
4831 	ASSERT(!DEVI_IS_DEVICE_REMOVED(dip));
4832 
4833 	(void) i_log_devfs_add_devinfo(dip, 0);
4834 }
4835 
4836 
4837 /*
4838  * log an event that a dev_info branch has been configured or unconfigured.
4839  */
4840 static int
4841 i_log_devfs_branch(char *node_path, char *subclass)
4842 {
4843 	int se_err;
4844 	sysevent_t *ev;
4845 	sysevent_id_t eid;
4846 	sysevent_value_t se_val;
4847 	sysevent_attr_list_t *ev_attr_list = NULL;
4848 	int no_transport = 0;
4849 
4850 	/* do not generate the event during boot */
4851 	if (!i_ddi_io_initialized())
4852 		return (DDI_SUCCESS);
4853 
4854 	/* Invalidate the devinfo snapshot cache */
4855 	i_ddi_di_cache_invalidate();
4856 
4857 	ev = sysevent_alloc(EC_DEVFS, subclass, EP_DDI, SE_SLEEP);
4858 
4859 	se_val.value_type = SE_DATA_TYPE_STRING;
4860 	se_val.value.sv_string = node_path;
4861 
4862 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
4863 	    &se_val, SE_SLEEP) != 0) {
4864 		goto fail;
4865 	}
4866 
4867 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
4868 		sysevent_free_attr(ev_attr_list);
4869 		goto fail;
4870 	}
4871 
4872 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
4873 		if (se_err == SE_NO_TRANSPORT)
4874 			no_transport = 1;
4875 		goto fail;
4876 	}
4877 
4878 	sysevent_free(ev);
4879 	return (DDI_SUCCESS);
4880 
4881 fail:
4882 	cmn_err(CE_WARN, "failed to log %s branch event for %s%s",
4883 	    subclass, node_path,
4884 	    (no_transport) ? " (syseventd not responding)" : "");
4885 
4886 	sysevent_free(ev);
4887 	return (DDI_FAILURE);
4888 }
4889 
4890 /*
4891  * log an event that a dev_info tree branch has been configured.
4892  */
4893 static int
4894 i_log_devfs_branch_add(dev_info_t *dip)
4895 {
4896 	char *node_path;
4897 	int rv;
4898 
4899 	node_path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4900 	(void) ddi_pathname(dip, node_path);
4901 	rv = i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_ADD);
4902 	kmem_free(node_path, MAXPATHLEN);
4903 
4904 	return (rv);
4905 }
4906 
4907 /*
4908  * log an event that a dev_info tree branch has been unconfigured.
4909  */
4910 static int
4911 i_log_devfs_branch_remove(char *node_path)
4912 {
4913 	return (i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_REMOVE));
4914 }
4915 
4916 /*
4917  * enqueue the dip's deviname on the branch event queue.
4918  */
4919 static struct brevq_node *
4920 brevq_enqueue(struct brevq_node **brevqp, dev_info_t *dip,
4921     struct brevq_node *child)
4922 {
4923 	struct brevq_node *brn;
4924 	char *deviname;
4925 
4926 	deviname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
4927 	(void) ddi_deviname(dip, deviname);
4928 
4929 	brn = kmem_zalloc(sizeof (*brn), KM_SLEEP);
4930 	brn->brn_deviname = i_ddi_strdup(deviname, KM_SLEEP);
4931 	kmem_free(deviname, MAXNAMELEN);
4932 	brn->brn_child = child;
4933 	brn->brn_sibling = *brevqp;
4934 	*brevqp = brn;
4935 
4936 	return (brn);
4937 }
4938 
4939 /*
4940  * free the memory allocated for the elements on the branch event queue.
4941  */
4942 static void
4943 free_brevq(struct brevq_node *brevq)
4944 {
4945 	struct brevq_node *brn, *next_brn;
4946 
4947 	for (brn = brevq; brn != NULL; brn = next_brn) {
4948 		next_brn = brn->brn_sibling;
4949 		ASSERT(brn->brn_child == NULL);
4950 		kmem_free(brn->brn_deviname, strlen(brn->brn_deviname) + 1);
4951 		kmem_free(brn, sizeof (*brn));
4952 	}
4953 }
4954 
4955 /*
4956  * log the events queued up on the branch event queue and free the
4957  * associated memory.
4958  *
4959  * node_path must have been allocated with at least MAXPATHLEN bytes.
4960  */
4961 static void
4962 log_and_free_brevq(char *node_path, struct brevq_node *brevq)
4963 {
4964 	struct brevq_node *brn;
4965 	char *p;
4966 
4967 	p = node_path + strlen(node_path);
4968 	for (brn = brevq; brn != NULL; brn = brn->brn_sibling) {
4969 		(void) strcpy(p, brn->brn_deviname);
4970 		(void) i_log_devfs_branch_remove(node_path);
4971 	}
4972 	*p = '\0';
4973 
4974 	free_brevq(brevq);
4975 }
4976 
4977 /*
4978  * log the events queued up on the branch event queue and free the
4979  * associated memory. Same as the previous function but operates on dip.
4980  */
4981 static void
4982 log_and_free_brevq_dip(dev_info_t *dip, struct brevq_node *brevq)
4983 {
4984 	char *path;
4985 
4986 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4987 	(void) ddi_pathname(dip, path);
4988 	log_and_free_brevq(path, brevq);
4989 	kmem_free(path, MAXPATHLEN);
4990 }
4991 
4992 /*
4993  * log the outstanding branch remove events for the grand children of the dip
4994  * and free the associated memory.
4995  */
4996 static void
4997 log_and_free_br_events_on_grand_children(dev_info_t *dip,
4998     struct brevq_node *brevq)
4999 {
5000 	struct brevq_node *brn;
5001 	char *path;
5002 	char *p;
5003 
5004 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
5005 	(void) ddi_pathname(dip, path);
5006 	p = path + strlen(path);
5007 	for (brn = brevq; brn != NULL; brn = brn->brn_sibling) {
5008 		if (brn->brn_child) {
5009 			(void) strcpy(p, brn->brn_deviname);
5010 			/* now path contains the node path to the dip's child */
5011 			log_and_free_brevq(path, brn->brn_child);
5012 			brn->brn_child = NULL;
5013 		}
5014 	}
5015 	kmem_free(path, MAXPATHLEN);
5016 }
5017 
5018 /*
5019  * log and cleanup branch remove events for the grand children of the dip.
5020  */
5021 static void
5022 cleanup_br_events_on_grand_children(dev_info_t *dip, struct brevq_node **brevqp)
5023 {
5024 	dev_info_t *child;
5025 	struct brevq_node *brevq, *brn, *prev_brn, *next_brn;
5026 	char *path;
5027 	int circ;
5028 
5029 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
5030 	prev_brn = NULL;
5031 	brevq = *brevqp;
5032 
5033 	ndi_devi_enter(dip, &circ);
5034 	for (brn = brevq; brn != NULL; brn = next_brn) {
5035 		next_brn = brn->brn_sibling;
5036 		for (child = ddi_get_child(dip); child != NULL;
5037 		    child = ddi_get_next_sibling(child)) {
5038 			if (i_ddi_node_state(child) >= DS_INITIALIZED) {
5039 				(void) ddi_deviname(child, path);
5040 				if (strcmp(path, brn->brn_deviname) == 0)
5041 					break;
5042 			}
5043 		}
5044 
5045 		if (child != NULL && !(DEVI_EVREMOVE(child))) {
5046 			/*
5047 			 * Event state is not REMOVE. So branch remove event
5048 			 * is not going be generated on brn->brn_child.
5049 			 * If any branch remove events were queued up on
5050 			 * brn->brn_child log them and remove the brn
5051 			 * from the queue.
5052 			 */
5053 			if (brn->brn_child) {
5054 				(void) ddi_pathname(dip, path);
5055 				(void) strcat(path, brn->brn_deviname);
5056 				log_and_free_brevq(path, brn->brn_child);
5057 			}
5058 
5059 			if (prev_brn)
5060 				prev_brn->brn_sibling = next_brn;
5061 			else
5062 				*brevqp = next_brn;
5063 
5064 			kmem_free(brn->brn_deviname,
5065 			    strlen(brn->brn_deviname) + 1);
5066 			kmem_free(brn, sizeof (*brn));
5067 		} else {
5068 			/*
5069 			 * Free up the outstanding branch remove events
5070 			 * queued on brn->brn_child since brn->brn_child
5071 			 * itself is eligible for branch remove event.
5072 			 */
5073 			if (brn->brn_child) {
5074 				free_brevq(brn->brn_child);
5075 				brn->brn_child = NULL;
5076 			}
5077 			prev_brn = brn;
5078 		}
5079 	}
5080 
5081 	ndi_devi_exit(dip, circ);
5082 	kmem_free(path, MAXPATHLEN);
5083 }
5084 
5085 static int
5086 need_remove_event(dev_info_t *dip, int flags)
5087 {
5088 	if ((flags & (NDI_NO_EVENT | NDI_AUTODETACH)) == 0 &&
5089 	    (flags & (NDI_DEVI_OFFLINE | NDI_UNCONFIG | NDI_DEVI_REMOVE)) &&
5090 	    !(DEVI_EVREMOVE(dip)))
5091 		return (1);
5092 	else
5093 		return (0);
5094 }
5095 
5096 /*
5097  * Unconfigure children/descendants of the dip.
5098  *
5099  * If the operation involves a branch event NDI_BRANCH_EVENT_OP is set
5100  * through out the unconfiguration. On successful return *brevqp is set to
5101  * a queue of dip's child devinames for which branch remove events need
5102  * to be generated.
5103  */
5104 static int
5105 devi_unconfig_branch(dev_info_t *dip, dev_info_t **dipp, int flags,
5106     struct brevq_node **brevqp)
5107 {
5108 	int rval;
5109 
5110 	*brevqp = NULL;
5111 
5112 	if ((!(flags & NDI_BRANCH_EVENT_OP)) && need_remove_event(dip, flags))
5113 		flags |= NDI_BRANCH_EVENT_OP;
5114 
5115 	if (flags & NDI_BRANCH_EVENT_OP) {
5116 		rval = devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE,
5117 		    brevqp);
5118 
5119 		if (rval != NDI_SUCCESS && (*brevqp)) {
5120 			log_and_free_brevq_dip(dip, *brevqp);
5121 			*brevqp = NULL;
5122 		}
5123 	} else
5124 		rval = devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE,
5125 		    NULL);
5126 
5127 	return (rval);
5128 }
5129 
5130 /*
5131  * If the dip is already bound to a driver transition to DS_INITIALIZED
5132  * in order to generate an event in the case where the node was left in
5133  * DS_BOUND state since boot (never got attached) and the node is now
5134  * being offlined.
5135  */
5136 static void
5137 init_bound_node_ev(dev_info_t *pdip, dev_info_t *dip, int flags)
5138 {
5139 	if (need_remove_event(dip, flags) &&
5140 	    i_ddi_node_state(dip) == DS_BOUND &&
5141 	    i_ddi_devi_attached(pdip) && !DEVI_IS_DEVICE_OFFLINE(dip))
5142 		(void) ddi_initchild(pdip, dip);
5143 }
5144 
5145 /*
5146  * attach a node/branch with parent already held busy
5147  */
5148 static int
5149 devi_attach_node(dev_info_t *dip, uint_t flags)
5150 {
5151 	dev_info_t *pdip = ddi_get_parent(dip);
5152 
5153 	ASSERT(pdip && DEVI_BUSY_OWNED(pdip));
5154 
5155 	mutex_enter(&(DEVI(dip)->devi_lock));
5156 	if (flags & NDI_DEVI_ONLINE) {
5157 		if (!i_ddi_devi_attached(dip))
5158 			DEVI_SET_REPORT(dip);
5159 		DEVI_SET_DEVICE_ONLINE(dip);
5160 	}
5161 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
5162 		mutex_exit(&(DEVI(dip)->devi_lock));
5163 		return (NDI_FAILURE);
5164 	}
5165 	mutex_exit(&(DEVI(dip)->devi_lock));
5166 
5167 	if (i_ddi_attachchild(dip) != DDI_SUCCESS) {
5168 		mutex_enter(&(DEVI(dip)->devi_lock));
5169 		DEVI_SET_EVUNINIT(dip);
5170 		mutex_exit(&(DEVI(dip)->devi_lock));
5171 
5172 		if (ndi_dev_is_persistent_node(dip))
5173 			(void) ddi_uninitchild(dip);
5174 		else {
5175 			/*
5176 			 * Delete .conf nodes and nodes that are not
5177 			 * well formed.
5178 			 */
5179 			(void) ddi_remove_child(dip, 0);
5180 		}
5181 		return (NDI_FAILURE);
5182 	}
5183 
5184 	i_ndi_devi_report_status_change(dip, NULL);
5185 
5186 	/*
5187 	 * log an event, but not during devfs lookups in which case
5188 	 * NDI_NO_EVENT is set.
5189 	 */
5190 	if ((flags & NDI_NO_EVENT) == 0 && !(DEVI_EVADD(dip))) {
5191 		(void) i_log_devfs_add_devinfo(dip, flags);
5192 
5193 		mutex_enter(&(DEVI(dip)->devi_lock));
5194 		DEVI_SET_EVADD(dip);
5195 		mutex_exit(&(DEVI(dip)->devi_lock));
5196 	} else if (!(flags & NDI_NO_EVENT_STATE_CHNG)) {
5197 		mutex_enter(&(DEVI(dip)->devi_lock));
5198 		DEVI_SET_EVADD(dip);
5199 		mutex_exit(&(DEVI(dip)->devi_lock));
5200 	}
5201 
5202 	return (NDI_SUCCESS);
5203 }
5204 
5205 /* internal function to config immediate children */
5206 static int
5207 config_immediate_children(dev_info_t *pdip, uint_t flags, major_t major)
5208 {
5209 	dev_info_t	*child, *next;
5210 	int		circ;
5211 
5212 	ASSERT(i_ddi_devi_attached(pdip));
5213 
5214 	if (!NEXUS_DRV(ddi_get_driver(pdip)))
5215 		return (NDI_SUCCESS);
5216 
5217 	NDI_CONFIG_DEBUG((CE_CONT,
5218 	    "config_immediate_children: %s%d (%p), flags=%x\n",
5219 	    ddi_driver_name(pdip), ddi_get_instance(pdip),
5220 	    (void *)pdip, flags));
5221 
5222 	ndi_devi_enter(pdip, &circ);
5223 
5224 	if (flags & NDI_CONFIG_REPROBE) {
5225 		mutex_enter(&DEVI(pdip)->devi_lock);
5226 		DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
5227 		mutex_exit(&DEVI(pdip)->devi_lock);
5228 	}
5229 	(void) i_ndi_make_spec_children(pdip, flags);
5230 	i_ndi_init_hw_children(pdip, flags);
5231 
5232 	child = ddi_get_child(pdip);
5233 	while (child) {
5234 		/* NOTE: devi_attach_node() may remove the dip */
5235 		next = ddi_get_next_sibling(child);
5236 
5237 		/*
5238 		 * Configure all nexus nodes or leaf nodes with
5239 		 * matching driver major
5240 		 */
5241 		if ((major == DDI_MAJOR_T_NONE) ||
5242 		    (major == ddi_driver_major(child)) ||
5243 		    ((flags & NDI_CONFIG) && (is_leaf_node(child) == 0)))
5244 			(void) devi_attach_node(child, flags);
5245 		child = next;
5246 	}
5247 
5248 	ndi_devi_exit(pdip, circ);
5249 
5250 	return (NDI_SUCCESS);
5251 }
5252 
5253 /* internal function to config grand children */
5254 static int
5255 config_grand_children(dev_info_t *pdip, uint_t flags, major_t major)
5256 {
5257 	struct mt_config_handle *hdl;
5258 
5259 	/* multi-threaded configuration of child nexus */
5260 	hdl = mt_config_init(pdip, NULL, flags, major, MT_CONFIG_OP, NULL);
5261 	mt_config_children(hdl);
5262 
5263 	return (mt_config_fini(hdl));	/* wait for threads to exit */
5264 }
5265 
5266 /*
5267  * Common function for device tree configuration,
5268  * either BUS_CONFIG_ALL or BUS_CONFIG_DRIVER.
5269  * The NDI_CONFIG flag causes recursive configuration of
5270  * grandchildren, devfs usage should not recurse.
5271  */
5272 static int
5273 devi_config_common(dev_info_t *dip, int flags, major_t major)
5274 {
5275 	int error;
5276 	int (*f)();
5277 
5278 	if (!i_ddi_devi_attached(dip))
5279 		return (NDI_FAILURE);
5280 
5281 	if (pm_pre_config(dip, NULL) != DDI_SUCCESS)
5282 		return (NDI_FAILURE);
5283 
5284 	if ((DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
5285 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
5286 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
5287 		error = config_immediate_children(dip, flags, major);
5288 	} else {
5289 		/* call bus_config entry point */
5290 		ddi_bus_config_op_t bus_op = (major == DDI_MAJOR_T_NONE) ?
5291 		    BUS_CONFIG_ALL : BUS_CONFIG_DRIVER;
5292 		error = (*f)(dip,
5293 		    flags, bus_op, (void *)(uintptr_t)major, NULL, 0);
5294 	}
5295 
5296 	if (error) {
5297 		pm_post_config(dip, NULL);
5298 		return (error);
5299 	}
5300 
5301 	/*
5302 	 * Some callers, notably SCSI, need to mark the devfs cache
5303 	 * to be rebuilt together with the config operation.
5304 	 */
5305 	if (flags & NDI_DEVFS_CLEAN)
5306 		(void) devfs_clean(dip, NULL, 0);
5307 
5308 	if (flags & NDI_CONFIG)
5309 		(void) config_grand_children(dip, flags, major);
5310 
5311 	pm_post_config(dip, NULL);
5312 
5313 	return (NDI_SUCCESS);
5314 }
5315 
5316 /*
5317  * Framework entry point for BUS_CONFIG_ALL
5318  */
5319 int
5320 ndi_devi_config(dev_info_t *dip, int flags)
5321 {
5322 	NDI_CONFIG_DEBUG((CE_CONT,
5323 	    "ndi_devi_config: par = %s%d (%p), flags = 0x%x\n",
5324 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
5325 
5326 	return (devi_config_common(dip, flags, DDI_MAJOR_T_NONE));
5327 }
5328 
5329 /*
5330  * Framework entry point for BUS_CONFIG_DRIVER, bound to major
5331  */
5332 int
5333 ndi_devi_config_driver(dev_info_t *dip, int flags, major_t major)
5334 {
5335 	/* don't abuse this function */
5336 	ASSERT(major != DDI_MAJOR_T_NONE);
5337 
5338 	NDI_CONFIG_DEBUG((CE_CONT,
5339 	    "ndi_devi_config_driver: par = %s%d (%p), flags = 0x%x\n",
5340 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
5341 
5342 	return (devi_config_common(dip, flags, major));
5343 }
5344 
5345 /*
5346  * Called by nexus drivers to configure its children.
5347  */
5348 static int
5349 devi_config_one(dev_info_t *pdip, char *devnm, dev_info_t **cdipp,
5350     uint_t flags, clock_t timeout)
5351 {
5352 	dev_info_t	*vdip = NULL;
5353 	char		*drivername = NULL;
5354 	int		find_by_addr = 0;
5355 	char		*name, *addr;
5356 	int		v_circ, p_circ;
5357 	clock_t		end_time;	/* 60 sec */
5358 	int		probed;
5359 	dev_info_t	*cdip;
5360 	mdi_pathinfo_t	*cpip;
5361 
5362 	*cdipp = NULL;
5363 
5364 	if (!NEXUS_DRV(ddi_get_driver(pdip)))
5365 		return (NDI_FAILURE);
5366 
5367 	/* split name into "name@addr" parts */
5368 	i_ddi_parse_name(devnm, &name, &addr, NULL);
5369 
5370 	/*
5371 	 * If the nexus is a pHCI and we are not processing a pHCI from
5372 	 * mdi bus_config code then we need to know the vHCI.
5373 	 */
5374 	if (MDI_PHCI(pdip))
5375 		vdip = mdi_devi_get_vdip(pdip);
5376 
5377 	/*
5378 	 * We may have a genericname on a system that creates drivername
5379 	 * nodes (from .conf files).  Find the drivername by nodeid. If we
5380 	 * can't find a node with devnm as the node name then we search by
5381 	 * drivername.	This allows an implementation to supply a genericly
5382 	 * named boot path (disk) and locate drivename nodes (sd).  The
5383 	 * NDI_PROMNAME flag does not apply to /devices/pseudo paths.
5384 	 */
5385 	if ((flags & NDI_PROMNAME) && (pdip != pseudo_dip)) {
5386 		drivername = child_path_to_driver(pdip, name, addr);
5387 		find_by_addr = 1;
5388 	}
5389 
5390 	/*
5391 	 * Determine end_time: This routine should *not* be called with a
5392 	 * constant non-zero timeout argument, the caller should be adjusting
5393 	 * the timeout argument relative to when it *started* its asynchronous
5394 	 * enumeration.
5395 	 */
5396 	if (timeout > 0)
5397 		end_time = ddi_get_lbolt() + timeout;
5398 
5399 	for (;;) {
5400 		/*
5401 		 * For pHCI, enter (vHCI, pHCI) and search for pathinfo/client
5402 		 * child - break out of for(;;) loop if child found.
5403 		 * NOTE: Lock order for ndi_devi_enter is (vHCI, pHCI).
5404 		 */
5405 		if (vdip) {
5406 			/* use mdi_devi_enter ordering */
5407 			ndi_devi_enter(vdip, &v_circ);
5408 			ndi_devi_enter(pdip, &p_circ);
5409 			cpip = mdi_pi_find(pdip, NULL, addr);
5410 			cdip = mdi_pi_get_client(cpip);
5411 			if (cdip)
5412 				break;
5413 		} else
5414 			ndi_devi_enter(pdip, &p_circ);
5415 
5416 		/*
5417 		 * When not a  vHCI or not all pHCI devices are required to
5418 		 * enumerated under the vHCI (NDI_MDI_FALLBACK) search for
5419 		 * devinfo child.
5420 		 */
5421 		if ((vdip == NULL) || (flags & NDI_MDI_FALLBACK)) {
5422 			/* determine if .conf nodes already built */
5423 			probed = (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
5424 
5425 			/*
5426 			 * Search for child by name, if not found then search
5427 			 * for a node bound to the drivername driver with the
5428 			 * specified "@addr". Break out of for(;;) loop if
5429 			 * child found.  To support path-oriented aliases
5430 			 * binding on boot-device, we do a search_by_addr too.
5431 			 */
5432 again:			(void) i_ndi_make_spec_children(pdip, flags);
5433 			cdip = find_child_by_name(pdip, name, addr);
5434 			if ((cdip == NULL) && drivername)
5435 				cdip = find_child_by_driver(pdip,
5436 				    drivername, addr);
5437 			if ((cdip == NULL) && find_by_addr)
5438 				cdip = find_child_by_addr(pdip, addr);
5439 			if (cdip)
5440 				break;
5441 
5442 			/*
5443 			 * determine if we should reenumerate .conf nodes
5444 			 * and look for child again.
5445 			 */
5446 			if (probed &&
5447 			    i_ddi_io_initialized() &&
5448 			    (flags & NDI_CONFIG_REPROBE) &&
5449 			    ((timeout <= 0) || (ddi_get_lbolt() >= end_time))) {
5450 				probed = 0;
5451 				mutex_enter(&DEVI(pdip)->devi_lock);
5452 				DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
5453 				mutex_exit(&DEVI(pdip)->devi_lock);
5454 				goto again;
5455 			}
5456 		}
5457 
5458 		/* break out of for(;;) if time expired */
5459 		if ((timeout <= 0) || (ddi_get_lbolt() >= end_time))
5460 			break;
5461 
5462 		/*
5463 		 * Child not found, exit and wait for asynchronous enumeration
5464 		 * to add child (or timeout). The addition of a new child (vhci
5465 		 * or phci) requires the asynchronous enumeration thread to
5466 		 * ndi_devi_enter/ndi_devi_exit. This exit will signal devi_cv
5467 		 * and cause us to return from ndi_devi_exit_and_wait, after
5468 		 * which we loop and search for the requested child again.
5469 		 */
5470 		NDI_DEBUG(flags, (CE_CONT,
5471 		    "%s%d: waiting for child %s@%s, timeout %ld",
5472 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
5473 		    name, addr, timeout));
5474 		if (vdip) {
5475 			/*
5476 			 * Mark vHCI for pHCI ndi_devi_exit broadcast.
5477 			 */
5478 			mutex_enter(&DEVI(vdip)->devi_lock);
5479 			DEVI(vdip)->devi_flags |=
5480 			    DEVI_PHCI_SIGNALS_VHCI;
5481 			mutex_exit(&DEVI(vdip)->devi_lock);
5482 			ndi_devi_exit(pdip, p_circ);
5483 
5484 			/*
5485 			 * NB: There is a small race window from above
5486 			 * ndi_devi_exit() of pdip to cv_wait() in
5487 			 * ndi_devi_exit_and_wait() which can result in
5488 			 * not immediately finding a new pHCI child
5489 			 * of a pHCI that uses NDI_MDI_FAILBACK.
5490 			 */
5491 			ndi_devi_exit_and_wait(vdip, v_circ, end_time);
5492 		} else {
5493 			ndi_devi_exit_and_wait(pdip, p_circ, end_time);
5494 		}
5495 	}
5496 
5497 	/* done with paddr, fixup i_ddi_parse_name '@'->'\0' change */
5498 	if (addr && *addr != '\0')
5499 		*(addr - 1) = '@';
5500 
5501 	/* attach and hold the child, returning pointer to child */
5502 	if (cdip && (devi_attach_node(cdip, flags) == NDI_SUCCESS)) {
5503 		ndi_hold_devi(cdip);
5504 		*cdipp = cdip;
5505 	}
5506 
5507 	ndi_devi_exit(pdip, p_circ);
5508 	if (vdip)
5509 		ndi_devi_exit(vdip, v_circ);
5510 	return (*cdipp ? NDI_SUCCESS : NDI_FAILURE);
5511 }
5512 
5513 /*
5514  * Enumerate and attach a child specified by name 'devnm'.
5515  * Called by devfs lookup and DR to perform a BUS_CONFIG_ONE.
5516  * Note: devfs does not make use of NDI_CONFIG to configure
5517  * an entire branch.
5518  */
5519 int
5520 ndi_devi_config_one(dev_info_t *pdip, char *devnm, dev_info_t **dipp, int flags)
5521 {
5522 	int error;
5523 	int (*f)();
5524 	char *nmdup;
5525 	int duplen;
5526 	int branch_event = 0;
5527 
5528 	ASSERT(pdip);
5529 	ASSERT(devnm);
5530 	ASSERT(dipp);
5531 	ASSERT(i_ddi_devi_attached(pdip));
5532 
5533 	NDI_CONFIG_DEBUG((CE_CONT,
5534 	    "ndi_devi_config_one: par = %s%d (%p), child = %s\n",
5535 	    ddi_driver_name(pdip), ddi_get_instance(pdip),
5536 	    (void *)pdip, devnm));
5537 
5538 	*dipp = NULL;
5539 
5540 	if (pm_pre_config(pdip, devnm) != DDI_SUCCESS) {
5541 		cmn_err(CE_WARN, "preconfig failed: %s", devnm);
5542 		return (NDI_FAILURE);
5543 	}
5544 
5545 	if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
5546 	    (flags & NDI_CONFIG)) {
5547 		flags |= NDI_BRANCH_EVENT_OP;
5548 		branch_event = 1;
5549 	}
5550 
5551 	nmdup = strdup(devnm);
5552 	duplen = strlen(devnm) + 1;
5553 
5554 	if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
5555 	    (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
5556 	    (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
5557 		error = devi_config_one(pdip, devnm, dipp, flags, 0);
5558 	} else {
5559 		/* call bus_config entry point */
5560 		error = (*f)(pdip, flags, BUS_CONFIG_ONE, (void *)devnm, dipp);
5561 	}
5562 
5563 	if (error) {
5564 		*dipp = NULL;
5565 	}
5566 
5567 	/*
5568 	 * if we fail to lookup and this could be an alias, lookup currdip
5569 	 * To prevent recursive lookups into the same hash table, only
5570 	 * do the currdip lookups once the hash table init is complete.
5571 	 * Use tsd so that redirection doesn't recurse
5572 	 */
5573 	if (error) {
5574 		char *alias = kmem_alloc(MAXPATHLEN, KM_NOSLEEP);
5575 		if (alias == NULL) {
5576 			ddi_err(DER_PANIC, pdip, "alias alloc failed: %s",
5577 			    nmdup);
5578 		}
5579 		(void) ddi_pathname(pdip, alias);
5580 		(void) strlcat(alias, "/", MAXPATHLEN);
5581 		(void) strlcat(alias, nmdup, MAXPATHLEN);
5582 
5583 		*dipp = ddi_alias_redirect(alias);
5584 		error = (*dipp ? NDI_SUCCESS : NDI_FAILURE);
5585 
5586 		kmem_free(alias, MAXPATHLEN);
5587 	}
5588 	kmem_free(nmdup, duplen);
5589 
5590 	if (error || !(flags & NDI_CONFIG)) {
5591 		pm_post_config(pdip, devnm);
5592 		return (error);
5593 	}
5594 
5595 	/*
5596 	 * DR usage (i.e. call with NDI_CONFIG) recursively configures
5597 	 * grandchildren, performing a BUS_CONFIG_ALL from the node attached
5598 	 * by the BUS_CONFIG_ONE.
5599 	 */
5600 	ASSERT(*dipp);
5601 	error = devi_config_common(*dipp, flags, DDI_MAJOR_T_NONE);
5602 
5603 	pm_post_config(pdip, devnm);
5604 
5605 	if (branch_event)
5606 		(void) i_log_devfs_branch_add(*dipp);
5607 
5608 	return (error);
5609 }
5610 
5611 /*
5612  * Enumerate and attach a child specified by name 'devnm'.
5613  * Called during configure the OBP options. This configures
5614  * only one node.
5615  */
5616 static int
5617 ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
5618     dev_info_t **childp, int flags)
5619 {
5620 	int error;
5621 	int (*f)();
5622 
5623 	ASSERT(childp);
5624 	ASSERT(i_ddi_devi_attached(parent));
5625 
5626 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_config_obp_args: "
5627 	    "par = %s%d (%p), child = %s\n", ddi_driver_name(parent),
5628 	    ddi_get_instance(parent), (void *)parent, devnm));
5629 
5630 	if ((DEVI(parent)->devi_ops->devo_bus_ops == NULL) ||
5631 	    (DEVI(parent)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
5632 	    (f = DEVI(parent)->devi_ops->devo_bus_ops->bus_config) == NULL) {
5633 		error = NDI_FAILURE;
5634 	} else {
5635 		/* call bus_config entry point */
5636 		error = (*f)(parent, flags,
5637 		    BUS_CONFIG_OBP_ARGS, (void *)devnm, childp);
5638 	}
5639 	return (error);
5640 }
5641 
5642 /*
5643  * Pay attention, the following is a bit tricky:
5644  * There are three possible cases when constraints are applied
5645  *
5646  *	- A constraint is applied and the offline is disallowed.
5647  *	  Simply return failure and block the offline
5648  *
5649  *	- A constraint is applied and the offline is allowed.
5650  *	  Mark the dip as having passed the constraint and allow
5651  *	  offline to proceed.
5652  *
5653  *	- A constraint is not applied. Allow the offline to proceed for now.
5654  *
5655  * In the latter two cases we allow the offline to proceed. If the
5656  * offline succeeds (no users) everything is fine. It is ok for an unused
5657  * device to be offlined even if no constraints were imposed on the offline.
5658  * If the offline fails because there are users, we look at the constraint
5659  * flag on the dip. If the constraint flag is set (implying that it passed
5660  * a constraint) we allow the dip to be retired. If not, we don't allow
5661  * the retire. This ensures that we don't allow unconstrained retire.
5662  */
5663 int
5664 e_ddi_offline_notify(dev_info_t *dip)
5665 {
5666 	int retval;
5667 	int constraint;
5668 	int failure;
5669 
5670 	RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): entered: dip=%p",
5671 	    (void *) dip));
5672 
5673 	constraint = 0;
5674 	failure = 0;
5675 
5676 	/*
5677 	 * Start with userland constraints first - applied via device contracts
5678 	 */
5679 	retval = contract_device_offline(dip, DDI_DEV_T_ANY, 0);
5680 	switch (retval) {
5681 	case CT_NACK:
5682 		RIO_DEBUG((CE_NOTE, "Received NACK for dip=%p", (void *)dip));
5683 		failure = 1;
5684 		goto out;
5685 	case CT_ACK:
5686 		constraint = 1;
5687 		RIO_DEBUG((CE_NOTE, "Received ACK for dip=%p", (void *)dip));
5688 		break;
5689 	case CT_NONE:
5690 		/* no contracts */
5691 		RIO_DEBUG((CE_NOTE, "No contracts on dip=%p", (void *)dip));
5692 		break;
5693 	default:
5694 		ASSERT(retval == CT_NONE);
5695 	}
5696 
5697 	/*
5698 	 * Next, use LDI to impose kernel constraints
5699 	 */
5700 	retval = ldi_invoke_notify(dip, DDI_DEV_T_ANY, 0, LDI_EV_OFFLINE, NULL);
5701 	switch (retval) {
5702 	case LDI_EV_FAILURE:
5703 		contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_FAILURE);
5704 		RIO_DEBUG((CE_NOTE, "LDI callback failed on dip=%p",
5705 		    (void *)dip));
5706 		failure = 1;
5707 		goto out;
5708 	case LDI_EV_SUCCESS:
5709 		constraint = 1;
5710 		RIO_DEBUG((CE_NOTE, "LDI callback success on dip=%p",
5711 		    (void *)dip));
5712 		break;
5713 	case LDI_EV_NONE:
5714 		/* no matching LDI callbacks */
5715 		RIO_DEBUG((CE_NOTE, "No LDI callbacks for dip=%p",
5716 		    (void *)dip));
5717 		break;
5718 	default:
5719 		ASSERT(retval == LDI_EV_NONE);
5720 	}
5721 
5722 out:
5723 	mutex_enter(&(DEVI(dip)->devi_lock));
5724 	if ((DEVI(dip)->devi_flags & DEVI_RETIRING) && failure) {
5725 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): setting "
5726 		    "BLOCKED flag. dip=%p", (void *)dip));
5727 		DEVI(dip)->devi_flags |= DEVI_R_BLOCKED;
5728 		if (DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT) {
5729 			RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): "
5730 			    "blocked. clearing RCM CONSTRAINT flag. dip=%p",
5731 			    (void *)dip));
5732 			DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
5733 		}
5734 	} else if ((DEVI(dip)->devi_flags & DEVI_RETIRING) && constraint) {
5735 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): setting "
5736 		    "CONSTRAINT flag. dip=%p", (void *)dip));
5737 		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
5738 	} else if ((DEVI(dip)->devi_flags & DEVI_RETIRING) &&
5739 	    ((DEVI(dip)->devi_ops != NULL &&
5740 	    DEVI(dip)->devi_ops->devo_bus_ops != NULL) ||
5741 	    DEVI(dip)->devi_ref == 0)) {
5742 		/* also allow retire if nexus or if device is not in use */
5743 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): device not in "
5744 		    "use. Setting CONSTRAINT flag. dip=%p", (void *)dip));
5745 		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
5746 	} else {
5747 		/*
5748 		 * Note: We cannot ASSERT here that DEVI_R_CONSTRAINT is
5749 		 * not set, since other sources (such as RCM) may have
5750 		 * set the flag.
5751 		 */
5752 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): not setting "
5753 		    "constraint flag. dip=%p", (void *)dip));
5754 	}
5755 	mutex_exit(&(DEVI(dip)->devi_lock));
5756 
5757 
5758 	RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): exit: dip=%p",
5759 	    (void *) dip));
5760 
5761 	return (failure ? DDI_FAILURE : DDI_SUCCESS);
5762 }
5763 
5764 void
5765 e_ddi_offline_finalize(dev_info_t *dip, int result)
5766 {
5767 	RIO_DEBUG((CE_NOTE, "e_ddi_offline_finalize(): entry: result=%s, "
5768 	    "dip=%p", result == DDI_SUCCESS ? "SUCCESS" : "FAILURE",
5769 	    (void *)dip));
5770 
5771 	contract_device_negend(dip, DDI_DEV_T_ANY, 0,  result == DDI_SUCCESS ?
5772 	    CT_EV_SUCCESS : CT_EV_FAILURE);
5773 
5774 	ldi_invoke_finalize(dip, DDI_DEV_T_ANY, 0,
5775 	    LDI_EV_OFFLINE, result == DDI_SUCCESS ?
5776 	    LDI_EV_SUCCESS : LDI_EV_FAILURE, NULL);
5777 
5778 	RIO_VERBOSE((CE_NOTE, "e_ddi_offline_finalize(): exit: dip=%p",
5779 	    (void *)dip));
5780 }
5781 
5782 void
5783 e_ddi_degrade_finalize(dev_info_t *dip)
5784 {
5785 	RIO_DEBUG((CE_NOTE, "e_ddi_degrade_finalize(): entry: "
5786 	    "result always = DDI_SUCCESS, dip=%p", (void *)dip));
5787 
5788 	contract_device_degrade(dip, DDI_DEV_T_ANY, 0);
5789 	contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_SUCCESS);
5790 
5791 	ldi_invoke_finalize(dip, DDI_DEV_T_ANY, 0, LDI_EV_DEGRADE,
5792 	    LDI_EV_SUCCESS, NULL);
5793 
5794 	RIO_VERBOSE((CE_NOTE, "e_ddi_degrade_finalize(): exit: dip=%p",
5795 	    (void *)dip));
5796 }
5797 
5798 void
5799 e_ddi_undegrade_finalize(dev_info_t *dip)
5800 {
5801 	RIO_DEBUG((CE_NOTE, "e_ddi_undegrade_finalize(): entry: "
5802 	    "result always = DDI_SUCCESS, dip=%p", (void *)dip));
5803 
5804 	contract_device_undegrade(dip, DDI_DEV_T_ANY, 0);
5805 	contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_SUCCESS);
5806 
5807 	RIO_VERBOSE((CE_NOTE, "e_ddi_undegrade_finalize(): exit: dip=%p",
5808 	    (void *)dip));
5809 }
5810 
5811 /*
5812  * detach a node with parent already held busy
5813  */
5814 static int
5815 devi_detach_node(dev_info_t *dip, uint_t flags)
5816 {
5817 	dev_info_t *pdip = ddi_get_parent(dip);
5818 	int ret = NDI_SUCCESS;
5819 	ddi_eventcookie_t cookie;
5820 	char *path = NULL;
5821 	char *class = NULL;
5822 	char *driver = NULL;
5823 	int instance = -1;
5824 	int post_event = 0;
5825 
5826 	ASSERT(pdip && DEVI_BUSY_OWNED(pdip));
5827 
5828 	/*
5829 	 * Invoke notify if offlining
5830 	 */
5831 	if (flags & NDI_DEVI_OFFLINE) {
5832 		RIO_DEBUG((CE_NOTE, "devi_detach_node: offlining dip=%p",
5833 		    (void *)dip));
5834 		if (e_ddi_offline_notify(dip) != DDI_SUCCESS) {
5835 			RIO_DEBUG((CE_NOTE, "devi_detach_node: offline NACKed"
5836 			    "dip=%p", (void *)dip));
5837 			return (NDI_FAILURE);
5838 		}
5839 	}
5840 
5841 	if (flags & NDI_POST_EVENT) {
5842 		if (i_ddi_devi_attached(pdip)) {
5843 			if (ddi_get_eventcookie(dip, DDI_DEVI_REMOVE_EVENT,
5844 			    &cookie) == NDI_SUCCESS)
5845 				(void) ndi_post_event(dip, dip, cookie, NULL);
5846 		}
5847 	}
5848 
5849 	if (i_ddi_detachchild(dip, flags) != DDI_SUCCESS) {
5850 		if (flags & NDI_DEVI_OFFLINE) {
5851 			RIO_DEBUG((CE_NOTE, "devi_detach_node: offline failed."
5852 			    " Calling e_ddi_offline_finalize with result=%d. "
5853 			    "dip=%p", DDI_FAILURE, (void *)dip));
5854 			e_ddi_offline_finalize(dip, DDI_FAILURE);
5855 		}
5856 		return (NDI_FAILURE);
5857 	}
5858 
5859 	if (flags & NDI_DEVI_OFFLINE) {
5860 		RIO_DEBUG((CE_NOTE, "devi_detach_node: offline succeeded."
5861 		    " Calling e_ddi_offline_finalize with result=%d, "
5862 		    "dip=%p", DDI_SUCCESS, (void *)dip));
5863 		e_ddi_offline_finalize(dip, DDI_SUCCESS);
5864 	}
5865 
5866 	if (flags & NDI_AUTODETACH)
5867 		return (NDI_SUCCESS);
5868 
5869 	/*
5870 	 * For DR, even bound nodes may need to have offline
5871 	 * flag set.
5872 	 */
5873 	if (flags & NDI_DEVI_OFFLINE) {
5874 		mutex_enter(&(DEVI(dip)->devi_lock));
5875 		DEVI_SET_DEVICE_OFFLINE(dip);
5876 		mutex_exit(&(DEVI(dip)->devi_lock));
5877 	}
5878 
5879 	if (i_ddi_node_state(dip) == DS_INITIALIZED) {
5880 		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
5881 		(void) ddi_pathname(dip, path);
5882 		if (flags & NDI_DEVI_OFFLINE)
5883 			i_ndi_devi_report_status_change(dip, path);
5884 
5885 		if (need_remove_event(dip, flags)) {
5886 			post_event = 1;
5887 			class = i_ddi_strdup(i_ddi_devi_class(dip), KM_SLEEP);
5888 			driver = i_ddi_strdup((char *)ddi_driver_name(dip),
5889 			    KM_SLEEP);
5890 			instance = ddi_get_instance(dip);
5891 
5892 			mutex_enter(&(DEVI(dip)->devi_lock));
5893 			DEVI_SET_EVREMOVE(dip);
5894 			mutex_exit(&(DEVI(dip)->devi_lock));
5895 		}
5896 	}
5897 
5898 	if (flags & (NDI_UNCONFIG | NDI_DEVI_REMOVE)) {
5899 		ret = ddi_uninitchild(dip);
5900 		if (ret == NDI_SUCCESS) {
5901 			/*
5902 			 * Remove uninitialized pseudo nodes because
5903 			 * system props are lost and the node cannot be
5904 			 * reattached.
5905 			 */
5906 			if (!ndi_dev_is_persistent_node(dip))
5907 				flags |= NDI_DEVI_REMOVE;
5908 
5909 			if (flags & NDI_DEVI_REMOVE) {
5910 				/*
5911 				 * NOTE: If there is a consumer of LDI events,
5912 				 * ddi_uninitchild above would have failed
5913 				 * because of active devi_ref from ldi_open().
5914 				 */
5915 
5916 				ret = ddi_remove_child(dip, 0);
5917 				if (post_event && ret == NDI_SUCCESS) {
5918 					/* Generate EC_DEVFS_DEVI_REMOVE */
5919 					(void) i_log_devfs_remove_devinfo(path,
5920 					    class, driver, instance, flags);
5921 				}
5922 			}
5923 
5924 		}
5925 	}
5926 
5927 	if (path)
5928 		kmem_free(path, MAXPATHLEN);
5929 	if (class)
5930 		kmem_free(class, strlen(class) + 1);
5931 	if (driver)
5932 		kmem_free(driver, strlen(driver) + 1);
5933 
5934 	return (ret);
5935 }
5936 
5937 /*
5938  * unconfigure immediate children of bus nexus device
5939  */
5940 static int
5941 unconfig_immediate_children(
5942 	dev_info_t *dip,
5943 	dev_info_t **dipp,
5944 	int flags,
5945 	major_t major)
5946 {
5947 	int rv = NDI_SUCCESS;
5948 	int circ, vcirc;
5949 	dev_info_t *child;
5950 	dev_info_t *vdip = NULL;
5951 	dev_info_t *next;
5952 
5953 	ASSERT(dipp == NULL || *dipp == NULL);
5954 
5955 	/*
5956 	 * Scan forward to see if we will be processing a pHCI child. If we
5957 	 * have a child that is a pHCI and vHCI and pHCI are not siblings then
5958 	 * enter vHCI before parent(pHCI) to prevent deadlock with mpxio
5959 	 * Client power management operations.
5960 	 */
5961 	ndi_devi_enter(dip, &circ);
5962 	for (child = ddi_get_child(dip); child;
5963 	    child = ddi_get_next_sibling(child)) {
5964 		/* skip same nodes we skip below */
5965 		if (((major != DDI_MAJOR_T_NONE) &&
5966 		    (major != ddi_driver_major(child))) ||
5967 		    ((flags & NDI_AUTODETACH) && !is_leaf_node(child)))
5968 			continue;
5969 
5970 		if (MDI_PHCI(child)) {
5971 			vdip = mdi_devi_get_vdip(child);
5972 			/*
5973 			 * If vHCI and vHCI is not a sibling of pHCI
5974 			 * then enter in (vHCI, parent(pHCI)) order.
5975 			 */
5976 			if (vdip && (ddi_get_parent(vdip) != dip)) {
5977 				ndi_devi_exit(dip, circ);
5978 
5979 				/* use mdi_devi_enter ordering */
5980 				ndi_devi_enter(vdip, &vcirc);
5981 				ndi_devi_enter(dip, &circ);
5982 				break;
5983 			} else
5984 				vdip = NULL;
5985 		}
5986 	}
5987 
5988 	child = ddi_get_child(dip);
5989 	while (child) {
5990 		next = ddi_get_next_sibling(child);
5991 
5992 		if ((major != DDI_MAJOR_T_NONE) &&
5993 		    (major != ddi_driver_major(child))) {
5994 			child = next;
5995 			continue;
5996 		}
5997 
5998 		/* skip nexus nodes during autodetach */
5999 		if ((flags & NDI_AUTODETACH) && !is_leaf_node(child)) {
6000 			child = next;
6001 			continue;
6002 		}
6003 
6004 		if (devi_detach_node(child, flags) != NDI_SUCCESS) {
6005 			if (dipp && *dipp == NULL) {
6006 				ndi_hold_devi(child);
6007 				*dipp = child;
6008 			}
6009 			rv = NDI_FAILURE;
6010 		}
6011 
6012 		/*
6013 		 * Continue upon failure--best effort algorithm
6014 		 */
6015 		child = next;
6016 	}
6017 
6018 	ndi_devi_exit(dip, circ);
6019 	if (vdip)
6020 		ndi_devi_exit(vdip, vcirc);
6021 
6022 	return (rv);
6023 }
6024 
6025 /*
6026  * unconfigure grand children of bus nexus device
6027  */
6028 static int
6029 unconfig_grand_children(
6030 	dev_info_t *dip,
6031 	dev_info_t **dipp,
6032 	int flags,
6033 	major_t major,
6034 	struct brevq_node **brevqp)
6035 {
6036 	struct mt_config_handle *hdl;
6037 
6038 	if (brevqp)
6039 		*brevqp = NULL;
6040 
6041 	/* multi-threaded configuration of child nexus */
6042 	hdl = mt_config_init(dip, dipp, flags, major, MT_UNCONFIG_OP, brevqp);
6043 	mt_config_children(hdl);
6044 
6045 	return (mt_config_fini(hdl));	/* wait for threads to exit */
6046 }
6047 
6048 /*
6049  * Unconfigure children/descendants of the dip.
6050  *
6051  * If brevqp is not NULL, on return *brevqp is set to a queue of dip's
6052  * child devinames for which branch remove events need to be generated.
6053  */
6054 static int
6055 devi_unconfig_common(
6056 	dev_info_t *dip,
6057 	dev_info_t **dipp,
6058 	int flags,
6059 	major_t major,
6060 	struct brevq_node **brevqp)
6061 {
6062 	int rv;
6063 	int pm_cookie;
6064 	int (*f)();
6065 	ddi_bus_config_op_t bus_op;
6066 
6067 	if (dipp)
6068 		*dipp = NULL;
6069 	if (brevqp)
6070 		*brevqp = NULL;
6071 
6072 	/*
6073 	 * Power up the dip if it is powered off.  If the flag bit
6074 	 * NDI_AUTODETACH is set and the dip is not at its full power,
6075 	 * skip the rest of the branch.
6076 	 */
6077 	if (pm_pre_unconfig(dip, flags, &pm_cookie, NULL) != DDI_SUCCESS)
6078 		return ((flags & NDI_AUTODETACH) ? NDI_SUCCESS :
6079 		    NDI_FAILURE);
6080 
6081 	/*
6082 	 * Some callers, notably SCSI, need to clear out the devfs
6083 	 * cache together with the unconfig to prevent stale entries.
6084 	 */
6085 	if (flags & NDI_DEVFS_CLEAN)
6086 		(void) devfs_clean(dip, NULL, 0);
6087 
6088 	rv = unconfig_grand_children(dip, dipp, flags, major, brevqp);
6089 
6090 	if ((rv != NDI_SUCCESS) && ((flags & NDI_AUTODETACH) == 0)) {
6091 		if (brevqp && *brevqp) {
6092 			log_and_free_br_events_on_grand_children(dip, *brevqp);
6093 			free_brevq(*brevqp);
6094 			*brevqp = NULL;
6095 		}
6096 		pm_post_unconfig(dip, pm_cookie, NULL);
6097 		return (rv);
6098 	}
6099 
6100 	if (dipp && *dipp) {
6101 		ndi_rele_devi(*dipp);
6102 		*dipp = NULL;
6103 	}
6104 
6105 	/*
6106 	 * It is possible to have a detached nexus with children
6107 	 * and grandchildren (for example: a branch consisting
6108 	 * entirely of bound nodes.) Since the nexus is detached
6109 	 * the bus_unconfig entry point cannot be used to remove
6110 	 * or unconfigure the descendants.
6111 	 */
6112 	if (!i_ddi_devi_attached(dip) ||
6113 	    (DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
6114 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
6115 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
6116 		rv = unconfig_immediate_children(dip, dipp, flags, major);
6117 	} else {
6118 		/*
6119 		 * call bus_unconfig entry point
6120 		 * It should reset nexus flags if unconfigure succeeds.
6121 		 */
6122 		bus_op = (major == DDI_MAJOR_T_NONE) ?
6123 		    BUS_UNCONFIG_ALL : BUS_UNCONFIG_DRIVER;
6124 		rv = (*f)(dip, flags, bus_op, (void *)(uintptr_t)major);
6125 	}
6126 
6127 	pm_post_unconfig(dip, pm_cookie, NULL);
6128 
6129 	if (brevqp && *brevqp)
6130 		cleanup_br_events_on_grand_children(dip, brevqp);
6131 
6132 	return (rv);
6133 }
6134 
6135 /*
6136  * called by devfs/framework to unconfigure children bound to major
6137  * If NDI_AUTODETACH is specified, this is invoked by either the
6138  * moduninstall daemon or the modunload -i 0 command.
6139  */
6140 int
6141 ndi_devi_unconfig_driver(dev_info_t *dip, int flags, major_t major)
6142 {
6143 	NDI_CONFIG_DEBUG((CE_CONT,
6144 	    "ndi_devi_unconfig_driver: par = %s%d (%p), flags = 0x%x\n",
6145 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
6146 
6147 	return (devi_unconfig_common(dip, NULL, flags, major, NULL));
6148 }
6149 
6150 int
6151 ndi_devi_unconfig(dev_info_t *dip, int flags)
6152 {
6153 	NDI_CONFIG_DEBUG((CE_CONT,
6154 	    "ndi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
6155 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
6156 
6157 	return (devi_unconfig_common(dip, NULL, flags, DDI_MAJOR_T_NONE, NULL));
6158 }
6159 
6160 int
6161 e_ddi_devi_unconfig(dev_info_t *dip, dev_info_t **dipp, int flags)
6162 {
6163 	NDI_CONFIG_DEBUG((CE_CONT,
6164 	    "e_ddi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
6165 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
6166 
6167 	return (devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE, NULL));
6168 }
6169 
6170 /*
6171  * Unconfigure child by name
6172  */
6173 static int
6174 devi_unconfig_one(dev_info_t *pdip, char *devnm, int flags)
6175 {
6176 	int		rv, circ;
6177 	dev_info_t	*child;
6178 	dev_info_t	*vdip = NULL;
6179 	int		v_circ;
6180 
6181 	ndi_devi_enter(pdip, &circ);
6182 	child = ndi_devi_findchild(pdip, devnm);
6183 
6184 	/*
6185 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
6186 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
6187 	 * management operations.
6188 	 */
6189 	if (child && MDI_PHCI(child)) {
6190 		vdip = mdi_devi_get_vdip(child);
6191 		if (vdip && (ddi_get_parent(vdip) != pdip)) {
6192 			ndi_devi_exit(pdip, circ);
6193 
6194 			/* use mdi_devi_enter ordering */
6195 			ndi_devi_enter(vdip, &v_circ);
6196 			ndi_devi_enter(pdip, &circ);
6197 			child = ndi_devi_findchild(pdip, devnm);
6198 		} else
6199 			vdip = NULL;
6200 	}
6201 
6202 	if (child) {
6203 		rv = devi_detach_node(child, flags);
6204 	} else {
6205 		NDI_CONFIG_DEBUG((CE_CONT,
6206 		    "devi_unconfig_one: %s not found\n", devnm));
6207 		rv = NDI_SUCCESS;
6208 	}
6209 
6210 	ndi_devi_exit(pdip, circ);
6211 	if (vdip)
6212 		ndi_devi_exit(vdip, v_circ);
6213 
6214 	return (rv);
6215 }
6216 
6217 int
6218 ndi_devi_unconfig_one(
6219 	dev_info_t *pdip,
6220 	char *devnm,
6221 	dev_info_t **dipp,
6222 	int flags)
6223 {
6224 	int		(*f)();
6225 	int		circ, rv;
6226 	int		pm_cookie;
6227 	dev_info_t	*child;
6228 	dev_info_t	*vdip = NULL;
6229 	int		v_circ;
6230 	struct brevq_node *brevq = NULL;
6231 
6232 	ASSERT(i_ddi_devi_attached(pdip));
6233 
6234 	NDI_CONFIG_DEBUG((CE_CONT,
6235 	    "ndi_devi_unconfig_one: par = %s%d (%p), child = %s\n",
6236 	    ddi_driver_name(pdip), ddi_get_instance(pdip),
6237 	    (void *)pdip, devnm));
6238 
6239 	if (pm_pre_unconfig(pdip, flags, &pm_cookie, devnm) != DDI_SUCCESS)
6240 		return (NDI_FAILURE);
6241 
6242 	if (dipp)
6243 		*dipp = NULL;
6244 
6245 	ndi_devi_enter(pdip, &circ);
6246 	child = ndi_devi_findchild(pdip, devnm);
6247 
6248 	/*
6249 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
6250 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
6251 	 * management operations.
6252 	 */
6253 	if (child && MDI_PHCI(child)) {
6254 		vdip = mdi_devi_get_vdip(child);
6255 		if (vdip && (ddi_get_parent(vdip) != pdip)) {
6256 			ndi_devi_exit(pdip, circ);
6257 
6258 			/* use mdi_devi_enter ordering */
6259 			ndi_devi_enter(vdip, &v_circ);
6260 			ndi_devi_enter(pdip, &circ);
6261 			child = ndi_devi_findchild(pdip, devnm);
6262 		} else
6263 			vdip = NULL;
6264 	}
6265 
6266 	if (child == NULL) {
6267 		NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_unconfig_one: %s"
6268 		    " not found\n", devnm));
6269 		rv = NDI_SUCCESS;
6270 		goto out;
6271 	}
6272 
6273 	/*
6274 	 * Unconfigure children/descendants of named child
6275 	 */
6276 	rv = devi_unconfig_branch(child, dipp, flags | NDI_UNCONFIG, &brevq);
6277 	if (rv != NDI_SUCCESS)
6278 		goto out;
6279 
6280 	init_bound_node_ev(pdip, child, flags);
6281 
6282 	if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
6283 	    (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
6284 	    (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
6285 		rv = devi_detach_node(child, flags);
6286 	} else {
6287 		/* call bus_config entry point */
6288 		rv = (*f)(pdip, flags, BUS_UNCONFIG_ONE, (void *)devnm);
6289 	}
6290 
6291 	if (brevq) {
6292 		if (rv != NDI_SUCCESS)
6293 			log_and_free_brevq_dip(child, brevq);
6294 		else
6295 			free_brevq(brevq);
6296 	}
6297 
6298 	if (dipp && rv != NDI_SUCCESS) {
6299 		ndi_hold_devi(child);
6300 		ASSERT(*dipp == NULL);
6301 		*dipp = child;
6302 	}
6303 
6304 out:
6305 	ndi_devi_exit(pdip, circ);
6306 	if (vdip)
6307 		ndi_devi_exit(vdip, v_circ);
6308 
6309 	pm_post_unconfig(pdip, pm_cookie, devnm);
6310 
6311 	return (rv);
6312 }
6313 
6314 struct async_arg {
6315 	dev_info_t *dip;
6316 	uint_t flags;
6317 };
6318 
6319 /*
6320  * Common async handler for:
6321  *	ndi_devi_bind_driver_async
6322  *	ndi_devi_online_async
6323  */
6324 static int
6325 i_ndi_devi_async_common(dev_info_t *dip, uint_t flags, void (*func)())
6326 {
6327 	int tqflag;
6328 	int kmflag;
6329 	struct async_arg *arg;
6330 	dev_info_t *pdip = ddi_get_parent(dip);
6331 
6332 	ASSERT(pdip);
6333 	ASSERT(DEVI(pdip)->devi_taskq);
6334 	ASSERT(ndi_dev_is_persistent_node(dip));
6335 
6336 	if (flags & NDI_NOSLEEP) {
6337 		kmflag = KM_NOSLEEP;
6338 		tqflag = TQ_NOSLEEP;
6339 	} else {
6340 		kmflag = KM_SLEEP;
6341 		tqflag = TQ_SLEEP;
6342 	}
6343 
6344 	arg = kmem_alloc(sizeof (*arg), kmflag);
6345 	if (arg == NULL)
6346 		goto fail;
6347 
6348 	arg->flags = flags;
6349 	arg->dip = dip;
6350 	if (ddi_taskq_dispatch(DEVI(pdip)->devi_taskq, func, arg, tqflag) ==
6351 	    DDI_SUCCESS) {
6352 		return (NDI_SUCCESS);
6353 	}
6354 
6355 fail:
6356 	NDI_CONFIG_DEBUG((CE_CONT, "%s%d: ddi_taskq_dispatch failed",
6357 	    ddi_driver_name(pdip), ddi_get_instance(pdip)));
6358 
6359 	if (arg)
6360 		kmem_free(arg, sizeof (*arg));
6361 	return (NDI_FAILURE);
6362 }
6363 
6364 static void
6365 i_ndi_devi_bind_driver_cb(struct async_arg *arg)
6366 {
6367 	(void) ndi_devi_bind_driver(arg->dip, arg->flags);
6368 	kmem_free(arg, sizeof (*arg));
6369 }
6370 
6371 int
6372 ndi_devi_bind_driver_async(dev_info_t *dip, uint_t flags)
6373 {
6374 	return (i_ndi_devi_async_common(dip, flags,
6375 	    (void (*)())i_ndi_devi_bind_driver_cb));
6376 }
6377 
6378 /*
6379  * place the devinfo in the ONLINE state.
6380  */
6381 int
6382 ndi_devi_online(dev_info_t *dip, uint_t flags)
6383 {
6384 	int circ, rv;
6385 	dev_info_t *pdip = ddi_get_parent(dip);
6386 	int branch_event = 0;
6387 
6388 	ASSERT(pdip);
6389 
6390 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_online: %s%d (%p)\n",
6391 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
6392 
6393 	ndi_devi_enter(pdip, &circ);
6394 	/* bind child before merging .conf nodes */
6395 	rv = i_ndi_config_node(dip, DS_BOUND, flags);
6396 	if (rv != NDI_SUCCESS) {
6397 		ndi_devi_exit(pdip, circ);
6398 		return (rv);
6399 	}
6400 
6401 	/* merge .conf properties */
6402 	(void) i_ndi_make_spec_children(pdip, flags);
6403 
6404 	flags |= (NDI_DEVI_ONLINE | NDI_CONFIG);
6405 
6406 	if (flags & NDI_NO_EVENT) {
6407 		/*
6408 		 * Caller is specifically asking for not to generate an event.
6409 		 * Set the following flag so that devi_attach_node() don't
6410 		 * change the event state.
6411 		 */
6412 		flags |= NDI_NO_EVENT_STATE_CHNG;
6413 	}
6414 
6415 	if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
6416 	    ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip))) {
6417 		flags |= NDI_BRANCH_EVENT_OP;
6418 		branch_event = 1;
6419 	}
6420 
6421 	/*
6422 	 * devi_attach_node() may remove dip on failure
6423 	 */
6424 	if ((rv = devi_attach_node(dip, flags)) == NDI_SUCCESS) {
6425 		if ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip)) {
6426 			/*
6427 			 * Hold the attached dip, and exit the parent while
6428 			 * we drive configuration of children below the
6429 			 * attached dip.
6430 			 */
6431 			ndi_hold_devi(dip);
6432 			ndi_devi_exit(pdip, circ);
6433 
6434 			(void) ndi_devi_config(dip, flags);
6435 
6436 			ndi_devi_enter(pdip, &circ);
6437 			ndi_rele_devi(dip);
6438 		}
6439 
6440 		if (branch_event)
6441 			(void) i_log_devfs_branch_add(dip);
6442 	}
6443 
6444 	ndi_devi_exit(pdip, circ);
6445 
6446 	/*
6447 	 * Notify devfs that we have a new node. Devfs needs to invalidate
6448 	 * cached directory contents.
6449 	 *
6450 	 * For PCMCIA devices, it is possible the pdip is not fully
6451 	 * attached. In this case, calling back into devfs will
6452 	 * result in a loop or assertion error. Hence, the check
6453 	 * on node state.
6454 	 *
6455 	 * If we own parent lock, this is part of a branch operation.
6456 	 * We skip the devfs_clean() step because the cache invalidation
6457 	 * is done higher up in the device tree.
6458 	 */
6459 	if (rv == NDI_SUCCESS && i_ddi_devi_attached(pdip) &&
6460 	    !DEVI_BUSY_OWNED(pdip))
6461 		(void) devfs_clean(pdip, NULL, 0);
6462 	return (rv);
6463 }
6464 
6465 static void
6466 i_ndi_devi_online_cb(struct async_arg *arg)
6467 {
6468 	(void) ndi_devi_online(arg->dip, arg->flags);
6469 	kmem_free(arg, sizeof (*arg));
6470 }
6471 
6472 int
6473 ndi_devi_online_async(dev_info_t *dip, uint_t flags)
6474 {
6475 	/* mark child as need config if requested. */
6476 	if (flags & NDI_CONFIG) {
6477 		mutex_enter(&(DEVI(dip)->devi_lock));
6478 		DEVI_SET_NDI_CONFIG(dip);
6479 		mutex_exit(&(DEVI(dip)->devi_lock));
6480 	}
6481 
6482 	return (i_ndi_devi_async_common(dip, flags,
6483 	    (void (*)())i_ndi_devi_online_cb));
6484 }
6485 
6486 /*
6487  * Take a device node Offline
6488  * To take a device Offline means to detach the device instance from
6489  * the driver and prevent devfs requests from re-attaching the device
6490  * instance.
6491  *
6492  * The flag NDI_DEVI_REMOVE causes removes the device node from
6493  * the driver list and the device tree. In this case, the device
6494  * is assumed to be removed from the system.
6495  */
6496 int
6497 ndi_devi_offline(dev_info_t *dip, uint_t flags)
6498 {
6499 	int		circ, rval = 0;
6500 	dev_info_t	*pdip = ddi_get_parent(dip);
6501 	dev_info_t	*vdip = NULL;
6502 	int		v_circ;
6503 	struct brevq_node *brevq = NULL;
6504 
6505 	ASSERT(pdip);
6506 
6507 	flags |= NDI_DEVI_OFFLINE;
6508 
6509 	/*
6510 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
6511 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
6512 	 * management operations.
6513 	 */
6514 	if (MDI_PHCI(dip)) {
6515 		vdip = mdi_devi_get_vdip(dip);
6516 		if (vdip && (ddi_get_parent(vdip) != pdip))
6517 			ndi_devi_enter(vdip, &v_circ);
6518 		else
6519 			vdip = NULL;
6520 	}
6521 	ndi_devi_enter(pdip, &circ);
6522 
6523 	if (i_ddi_devi_attached(dip)) {
6524 		/*
6525 		 * If dip is in DS_READY state, there may be cached dv_nodes
6526 		 * referencing this dip, so we invoke devfs code path.
6527 		 * Note that we must release busy changing on pdip to
6528 		 * avoid deadlock against devfs.
6529 		 */
6530 		char *devname = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
6531 		(void) ddi_deviname(dip, devname);
6532 
6533 		ndi_devi_exit(pdip, circ);
6534 		if (vdip)
6535 			ndi_devi_exit(vdip, v_circ);
6536 
6537 		/*
6538 		 * If we are explictly told to clean, then clean. If we own the
6539 		 * parent lock then this is part of a branch operation, and we
6540 		 * skip the devfs_clean() step.
6541 		 *
6542 		 * NOTE: A thread performing a devfs file system lookup/
6543 		 * bus_config can't call devfs_clean to unconfig without
6544 		 * causing rwlock problems in devfs. For ndi_devi_offline, this
6545 		 * means that the NDI_DEVFS_CLEAN flag is safe from ioctl code
6546 		 * or from an async hotplug thread, but is not safe from a
6547 		 * nexus driver's bus_config implementation.
6548 		 */
6549 		if ((flags & NDI_DEVFS_CLEAN) ||
6550 		    (!DEVI_BUSY_OWNED(pdip)))
6551 			(void) devfs_clean(pdip, devname + 1, DV_CLEAN_FORCE);
6552 
6553 		kmem_free(devname, MAXNAMELEN + 1);
6554 
6555 		rval = devi_unconfig_branch(dip, NULL, flags|NDI_UNCONFIG,
6556 		    &brevq);
6557 
6558 		if (rval)
6559 			return (NDI_FAILURE);
6560 
6561 		if (vdip)
6562 			ndi_devi_enter(vdip, &v_circ);
6563 		ndi_devi_enter(pdip, &circ);
6564 	}
6565 
6566 	init_bound_node_ev(pdip, dip, flags);
6567 
6568 	rval = devi_detach_node(dip, flags);
6569 	if (brevq) {
6570 		if (rval != NDI_SUCCESS)
6571 			log_and_free_brevq_dip(dip, brevq);
6572 		else
6573 			free_brevq(brevq);
6574 	}
6575 
6576 	ndi_devi_exit(pdip, circ);
6577 	if (vdip)
6578 		ndi_devi_exit(vdip, v_circ);
6579 
6580 	return (rval);
6581 }
6582 
6583 /*
6584  * Find the child dev_info node of parent nexus 'p' whose unit address
6585  * matches "cname@caddr".  Recommend use of ndi_devi_findchild() instead.
6586  */
6587 dev_info_t *
6588 ndi_devi_find(dev_info_t *pdip, char *cname, char *caddr)
6589 {
6590 	dev_info_t *child;
6591 	int circ;
6592 
6593 	if (pdip == NULL || cname == NULL || caddr == NULL)
6594 		return ((dev_info_t *)NULL);
6595 
6596 	ndi_devi_enter(pdip, &circ);
6597 	child = find_sibling(ddi_get_child(pdip), cname, caddr,
6598 	    FIND_NODE_BY_NODENAME, NULL);
6599 	ndi_devi_exit(pdip, circ);
6600 	return (child);
6601 }
6602 
6603 /*
6604  * Find the child dev_info node of parent nexus 'p' whose unit address
6605  * matches devname "name@addr".  Permits caller to hold the parent.
6606  */
6607 dev_info_t *
6608 ndi_devi_findchild(dev_info_t *pdip, char *devname)
6609 {
6610 	dev_info_t *child;
6611 	char	*cname, *caddr;
6612 	char	*devstr;
6613 
6614 	ASSERT(DEVI_BUSY_OWNED(pdip));
6615 
6616 	devstr = i_ddi_strdup(devname, KM_SLEEP);
6617 	i_ddi_parse_name(devstr, &cname, &caddr, NULL);
6618 
6619 	if (cname == NULL || caddr == NULL) {
6620 		kmem_free(devstr, strlen(devname)+1);
6621 		return ((dev_info_t *)NULL);
6622 	}
6623 
6624 	child = find_sibling(ddi_get_child(pdip), cname, caddr,
6625 	    FIND_NODE_BY_NODENAME, NULL);
6626 	kmem_free(devstr, strlen(devname)+1);
6627 	return (child);
6628 }
6629 
6630 /*
6631  * Misc. routines called by framework only
6632  */
6633 
6634 /*
6635  * Clear the DEVI_MADE_CHILDREN/DEVI_ATTACHED_CHILDREN flags
6636  * if new child spec has been added.
6637  */
6638 static int
6639 reset_nexus_flags(dev_info_t *dip, void *arg)
6640 {
6641 	struct hwc_spec	*list;
6642 	int		circ;
6643 
6644 	if (((DEVI(dip)->devi_flags & DEVI_MADE_CHILDREN) == 0) ||
6645 	    ((list = hwc_get_child_spec(dip, (major_t)(uintptr_t)arg)) == NULL))
6646 		return (DDI_WALK_CONTINUE);
6647 
6648 	hwc_free_spec_list(list);
6649 
6650 	/* coordinate child state update */
6651 	ndi_devi_enter(dip, &circ);
6652 	mutex_enter(&DEVI(dip)->devi_lock);
6653 	DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN | DEVI_ATTACHED_CHILDREN);
6654 	mutex_exit(&DEVI(dip)->devi_lock);
6655 	ndi_devi_exit(dip, circ);
6656 
6657 	return (DDI_WALK_CONTINUE);
6658 }
6659 
6660 /*
6661  * Helper functions, returns NULL if no memory.
6662  */
6663 
6664 /*
6665  * path_to_major:
6666  *
6667  * Return an alternate driver name binding for the leaf device
6668  * of the given pathname, if there is one. The purpose of this
6669  * function is to deal with generic pathnames. The default action
6670  * for platforms that can't do this (ie: x86 or any platform that
6671  * does not have prom_finddevice functionality, which matches
6672  * nodenames and unit-addresses without the drivers participation)
6673  * is to return DDI_MAJOR_T_NONE.
6674  *
6675  * Used in loadrootmodules() in the swapgeneric module to
6676  * associate a given pathname with a given leaf driver.
6677  *
6678  */
6679 major_t
6680 path_to_major(char *path)
6681 {
6682 	dev_info_t *dip;
6683 	char *p, *q;
6684 	pnode_t nodeid;
6685 	major_t major;
6686 
6687 	/* check for path-oriented alias */
6688 	major = ddi_name_to_major(path);
6689 	if (driver_installed(major)) {
6690 		NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s path bound %s\n",
6691 		    path, ddi_major_to_name(major)));
6692 		return (major);
6693 	}
6694 
6695 	/*
6696 	 * Get the nodeid of the given pathname, if such a mapping exists.
6697 	 */
6698 	dip = NULL;
6699 	nodeid = prom_finddevice(path);
6700 	if (nodeid != OBP_BADNODE) {
6701 		/*
6702 		 * Find the nodeid in our copy of the device tree and return
6703 		 * whatever name we used to bind this node to a driver.
6704 		 */
6705 		dip = e_ddi_nodeid_to_dip(nodeid);
6706 	}
6707 
6708 	if (dip == NULL) {
6709 		NDI_CONFIG_DEBUG((CE_WARN,
6710 		    "path_to_major: can't bind <%s>\n", path));
6711 		return (DDI_MAJOR_T_NONE);
6712 	}
6713 
6714 	/*
6715 	 * If we're bound to something other than the nodename,
6716 	 * note that in the message buffer and system log.
6717 	 */
6718 	p = ddi_binding_name(dip);
6719 	q = ddi_node_name(dip);
6720 	if (p && q && (strcmp(p, q) != 0))
6721 		NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s bound to %s\n",
6722 		    path, p));
6723 
6724 	major = ddi_name_to_major(p);
6725 
6726 	ndi_rele_devi(dip);		/* release e_ddi_nodeid_to_dip hold */
6727 
6728 	return (major);
6729 }
6730 
6731 /*
6732  * Return the held dip for the specified major and instance, attempting to do
6733  * an attach if specified. Return NULL if the devi can't be found or put in
6734  * the proper state. The caller must release the hold via ddi_release_devi if
6735  * a non-NULL value is returned.
6736  *
6737  * Some callers expect to be able to perform a hold_devi() while in a context
6738  * where using ndi_devi_enter() to ensure the hold might cause deadlock (see
6739  * open-from-attach code in consconfig_dacf.c). Such special-case callers
6740  * must ensure that an ndi_devi_enter(parent)/ndi_hold_devi() from a safe
6741  * context is already active. The hold_devi() implementation must accommodate
6742  * these callers.
6743  */
6744 static dev_info_t *
6745 hold_devi(major_t major, int instance, int flags)
6746 {
6747 	struct devnames	*dnp;
6748 	dev_info_t	*dip;
6749 	char		*path;
6750 	char		*vpath;
6751 
6752 	if ((major >= devcnt) || (instance == -1))
6753 		return (NULL);
6754 
6755 	/* try to find the instance in the per driver list */
6756 	dnp = &(devnamesp[major]);
6757 	LOCK_DEV_OPS(&(dnp->dn_lock));
6758 	for (dip = dnp->dn_head; dip;
6759 	    dip = (dev_info_t *)DEVI(dip)->devi_next) {
6760 		/* skip node if instance field is not valid */
6761 		if (i_ddi_node_state(dip) < DS_INITIALIZED)
6762 			continue;
6763 
6764 		/* look for instance match */
6765 		if (DEVI(dip)->devi_instance == instance) {
6766 			/*
6767 			 * To accommodate callers that can't block in
6768 			 * ndi_devi_enter() we do an ndi_hold_devi(), and
6769 			 * afterwards check that the node is in a state where
6770 			 * the hold prevents detach(). If we did not manage to
6771 			 * prevent detach then we ndi_rele_devi() and perform
6772 			 * the slow path below (which can result in a blocking
6773 			 * ndi_devi_enter() while driving attach top-down).
6774 			 * This code depends on the ordering of
6775 			 * DEVI_SET_DETACHING and the devi_ref check in the
6776 			 * detach_node() code path.
6777 			 */
6778 			ndi_hold_devi(dip);
6779 			if (i_ddi_devi_attached(dip) &&
6780 			    !DEVI_IS_DETACHING(dip)) {
6781 				UNLOCK_DEV_OPS(&(dnp->dn_lock));
6782 				return (dip);	/* fast-path with devi held */
6783 			}
6784 			ndi_rele_devi(dip);
6785 
6786 			/* try slow-path */
6787 			dip = NULL;
6788 			break;
6789 		}
6790 	}
6791 	ASSERT(dip == NULL);
6792 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
6793 
6794 	if (flags & E_DDI_HOLD_DEVI_NOATTACH)
6795 		return (NULL);		/* told not to drive attach */
6796 
6797 	/* slow-path may block, so it should not occur from interrupt */
6798 	ASSERT(!servicing_interrupt());
6799 	if (servicing_interrupt())
6800 		return (NULL);
6801 
6802 	/* reconstruct the path and drive attach by path through devfs. */
6803 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
6804 	if (e_ddi_majorinstance_to_path(major, instance, path) == 0) {
6805 		dip = e_ddi_hold_devi_by_path(path, flags);
6806 
6807 		/*
6808 		 * Verify that we got the correct device - a path_to_inst file
6809 		 * with a bogus/corrupt path (or a nexus that changes its
6810 		 * unit-address format) could result in an incorrect answer
6811 		 *
6812 		 * Verify major, instance, and path.
6813 		 */
6814 		vpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
6815 		if (dip &&
6816 		    ((DEVI(dip)->devi_major != major) ||
6817 		    ((DEVI(dip)->devi_instance != instance)) ||
6818 		    (strcmp(path, ddi_pathname(dip, vpath)) != 0))) {
6819 			ndi_rele_devi(dip);
6820 			dip = NULL;	/* no answer better than wrong answer */
6821 		}
6822 		kmem_free(vpath, MAXPATHLEN);
6823 	}
6824 	kmem_free(path, MAXPATHLEN);
6825 	return (dip);			/* with devi held */
6826 }
6827 
6828 /*
6829  * The {e_}ddi_hold_devi{_by_{instance|dev|path}} hold the devinfo node
6830  * associated with the specified arguments.  This hold should be released
6831  * by calling ddi_release_devi.
6832  *
6833  * The E_DDI_HOLD_DEVI_NOATTACH flag argument allows the caller to to specify
6834  * a failure return if the node is not already attached.
6835  *
6836  * NOTE: by the time we make e_ddi_hold_devi public, we should be able to reuse
6837  * ddi_hold_devi again.
6838  */
6839 dev_info_t *
6840 ddi_hold_devi_by_instance(major_t major, int instance, int flags)
6841 {
6842 	return (hold_devi(major, instance, flags));
6843 }
6844 
6845 dev_info_t *
6846 e_ddi_hold_devi_by_dev(dev_t dev, int flags)
6847 {
6848 	major_t	major = getmajor(dev);
6849 	dev_info_t	*dip;
6850 	struct dev_ops	*ops;
6851 	dev_info_t	*ddip = NULL;
6852 
6853 	dip = hold_devi(major, dev_to_instance(dev), flags);
6854 
6855 	/*
6856 	 * The rest of this routine is legacy support for drivers that
6857 	 * have broken DDI_INFO_DEVT2INSTANCE implementations but may have
6858 	 * functional DDI_INFO_DEVT2DEVINFO implementations.  This code will
6859 	 * diagnose inconsistency and, for maximum compatibility with legacy
6860 	 * drivers, give preference to the drivers DDI_INFO_DEVT2DEVINFO
6861 	 * implementation over the above derived dip based the driver's
6862 	 * DDI_INFO_DEVT2INSTANCE implementation. This legacy support should
6863 	 * be removed when DDI_INFO_DEVT2DEVINFO is deprecated.
6864 	 *
6865 	 * NOTE: The following code has a race condition. DEVT2DEVINFO
6866 	 *	returns a dip which is not held. By the time we ref ddip,
6867 	 *	it could have been freed. The saving grace is that for
6868 	 *	most drivers, the dip returned from hold_devi() is the
6869 	 *	same one as the one returned by DEVT2DEVINFO, so we are
6870 	 *	safe for drivers with the correct getinfo(9e) impl.
6871 	 */
6872 	if (((ops = ddi_hold_driver(major)) != NULL) &&
6873 	    CB_DRV_INSTALLED(ops) && ops->devo_getinfo)  {
6874 		if ((*ops->devo_getinfo)(NULL, DDI_INFO_DEVT2DEVINFO,
6875 		    (void *)dev, (void **)&ddip) != DDI_SUCCESS)
6876 			ddip = NULL;
6877 	}
6878 
6879 	/* give preference to the driver returned DEVT2DEVINFO dip */
6880 	if (ddip && (dip != ddip)) {
6881 #ifdef	DEBUG
6882 		cmn_err(CE_WARN, "%s: inconsistent getinfo(9E) implementation",
6883 		    ddi_driver_name(ddip));
6884 #endif	/* DEBUG */
6885 		ndi_hold_devi(ddip);
6886 		if (dip)
6887 			ndi_rele_devi(dip);
6888 		dip = ddip;
6889 	}
6890 
6891 	if (ops)
6892 		ddi_rele_driver(major);
6893 
6894 	return (dip);
6895 }
6896 
6897 /*
6898  * For compatibility only. Do not call this function!
6899  */
6900 dev_info_t *
6901 e_ddi_get_dev_info(dev_t dev, vtype_t type)
6902 {
6903 	dev_info_t *dip = NULL;
6904 	if (getmajor(dev) >= devcnt)
6905 		return (NULL);
6906 
6907 	switch (type) {
6908 	case VCHR:
6909 	case VBLK:
6910 		dip = e_ddi_hold_devi_by_dev(dev, 0);
6911 	default:
6912 		break;
6913 	}
6914 
6915 	/*
6916 	 * For compatibility reasons, we can only return the dip with
6917 	 * the driver ref count held. This is not a safe thing to do.
6918 	 * For certain broken third-party software, we are willing
6919 	 * to venture into unknown territory.
6920 	 */
6921 	if (dip) {
6922 		(void) ndi_hold_driver(dip);
6923 		ndi_rele_devi(dip);
6924 	}
6925 	return (dip);
6926 }
6927 
6928 dev_info_t *
6929 e_ddi_hold_devi_by_path(char *path, int flags)
6930 {
6931 	dev_info_t	*dip;
6932 
6933 	/* can't specify NOATTACH by path */
6934 	ASSERT(!(flags & E_DDI_HOLD_DEVI_NOATTACH));
6935 
6936 	return (resolve_pathname(path, &dip, NULL, NULL) ? NULL : dip);
6937 }
6938 
6939 void
6940 e_ddi_hold_devi(dev_info_t *dip)
6941 {
6942 	ndi_hold_devi(dip);
6943 }
6944 
6945 void
6946 ddi_release_devi(dev_info_t *dip)
6947 {
6948 	ndi_rele_devi(dip);
6949 }
6950 
6951 /*
6952  * Associate a streams queue with a devinfo node
6953  * NOTE: This function is called by STREAM driver's put procedure.
6954  *	It cannot block.
6955  */
6956 void
6957 ddi_assoc_queue_with_devi(queue_t *q, dev_info_t *dip)
6958 {
6959 	queue_t *rq = _RD(q);
6960 	struct stdata *stp;
6961 	vnode_t *vp;
6962 
6963 	/* set flag indicating that ddi_assoc_queue_with_devi was called */
6964 	mutex_enter(QLOCK(rq));
6965 	rq->q_flag |= _QASSOCIATED;
6966 	mutex_exit(QLOCK(rq));
6967 
6968 	/* get the vnode associated with the queue */
6969 	stp = STREAM(rq);
6970 	vp = stp->sd_vnode;
6971 	ASSERT(vp);
6972 
6973 	/* change the hardware association of the vnode */
6974 	spec_assoc_vp_with_devi(vp, dip);
6975 }
6976 
6977 /*
6978  * ddi_install_driver(name)
6979  *
6980  * Driver installation is currently a byproduct of driver loading.  This
6981  * may change.
6982  */
6983 int
6984 ddi_install_driver(char *name)
6985 {
6986 	major_t major = ddi_name_to_major(name);
6987 
6988 	if ((major == DDI_MAJOR_T_NONE) ||
6989 	    (ddi_hold_installed_driver(major) == NULL)) {
6990 		return (DDI_FAILURE);
6991 	}
6992 	ddi_rele_driver(major);
6993 	return (DDI_SUCCESS);
6994 }
6995 
6996 struct dev_ops *
6997 ddi_hold_driver(major_t major)
6998 {
6999 	return (mod_hold_dev_by_major(major));
7000 }
7001 
7002 
7003 void
7004 ddi_rele_driver(major_t major)
7005 {
7006 	mod_rele_dev_by_major(major);
7007 }
7008 
7009 
7010 /*
7011  * This is called during boot to force attachment order of special dips
7012  * dip must be referenced via ndi_hold_devi()
7013  */
7014 int
7015 i_ddi_attach_node_hierarchy(dev_info_t *dip)
7016 {
7017 	dev_info_t	*parent;
7018 	int		ret, circ;
7019 
7020 	/*
7021 	 * Recurse up until attached parent is found.
7022 	 */
7023 	if (i_ddi_devi_attached(dip))
7024 		return (DDI_SUCCESS);
7025 	parent = ddi_get_parent(dip);
7026 	if (i_ddi_attach_node_hierarchy(parent) != DDI_SUCCESS)
7027 		return (DDI_FAILURE);
7028 
7029 	/*
7030 	 * Come top-down, expanding .conf nodes under this parent
7031 	 * and driving attach.
7032 	 */
7033 	ndi_devi_enter(parent, &circ);
7034 	(void) i_ndi_make_spec_children(parent, 0);
7035 	ret = i_ddi_attachchild(dip);
7036 	ndi_devi_exit(parent, circ);
7037 
7038 	return (ret);
7039 }
7040 
7041 /* keep this function static */
7042 static int
7043 attach_driver_nodes(major_t major)
7044 {
7045 	struct devnames *dnp;
7046 	dev_info_t *dip;
7047 	int error = DDI_FAILURE;
7048 
7049 	dnp = &devnamesp[major];
7050 	LOCK_DEV_OPS(&dnp->dn_lock);
7051 	dip = dnp->dn_head;
7052 	while (dip) {
7053 		ndi_hold_devi(dip);
7054 		UNLOCK_DEV_OPS(&dnp->dn_lock);
7055 		if (i_ddi_attach_node_hierarchy(dip) == DDI_SUCCESS)
7056 			error = DDI_SUCCESS;
7057 		/*
7058 		 * Set the 'ddi-config-driver-node' property on a nexus
7059 		 * node to cause attach_driver_nodes() to configure all
7060 		 * immediate children of the nexus. This property should
7061 		 * be set on nodes with immediate children that bind to
7062 		 * the same driver as parent.
7063 		 */
7064 		if ((error == DDI_SUCCESS) && (ddi_prop_exists(DDI_DEV_T_ANY,
7065 		    dip, DDI_PROP_DONTPASS, "ddi-config-driver-node"))) {
7066 			(void) ndi_devi_config(dip, NDI_NO_EVENT);
7067 		}
7068 		LOCK_DEV_OPS(&dnp->dn_lock);
7069 		ndi_rele_devi(dip);
7070 		dip = ddi_get_next(dip);
7071 	}
7072 	if (error == DDI_SUCCESS)
7073 		dnp->dn_flags |= DN_NO_AUTODETACH;
7074 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7075 
7076 
7077 	return (error);
7078 }
7079 
7080 /*
7081  * i_ddi_attach_hw_nodes configures and attaches all hw nodes
7082  * bound to a specific driver. This function replaces calls to
7083  * ddi_hold_installed_driver() for drivers with no .conf
7084  * enumerated nodes.
7085  *
7086  * This facility is typically called at boot time to attach
7087  * platform-specific hardware nodes, such as ppm nodes on xcal
7088  * and grover and keyswitch nodes on cherrystone. It does not
7089  * deal with .conf enumerated node. Calling it beyond the boot
7090  * process is strongly discouraged.
7091  */
7092 int
7093 i_ddi_attach_hw_nodes(char *driver)
7094 {
7095 	major_t major;
7096 
7097 	major = ddi_name_to_major(driver);
7098 	if (major == DDI_MAJOR_T_NONE)
7099 		return (DDI_FAILURE);
7100 
7101 	return (attach_driver_nodes(major));
7102 }
7103 
7104 /*
7105  * i_ddi_attach_pseudo_node configures pseudo drivers which
7106  * has a single node. The .conf nodes must be enumerated
7107  * before calling this interface. The dip is held attached
7108  * upon returning.
7109  *
7110  * This facility should only be called only at boot time
7111  * by the I/O framework.
7112  */
7113 dev_info_t *
7114 i_ddi_attach_pseudo_node(char *driver)
7115 {
7116 	major_t major;
7117 	dev_info_t *dip;
7118 
7119 	major = ddi_name_to_major(driver);
7120 	if (major == DDI_MAJOR_T_NONE)
7121 		return (NULL);
7122 
7123 	if (attach_driver_nodes(major) != DDI_SUCCESS)
7124 		return (NULL);
7125 
7126 	dip = devnamesp[major].dn_head;
7127 	ASSERT(dip && ddi_get_next(dip) == NULL);
7128 	ndi_hold_devi(dip);
7129 	return (dip);
7130 }
7131 
7132 static void
7133 diplist_to_parent_major(dev_info_t *head, char parents[])
7134 {
7135 	major_t major;
7136 	dev_info_t *dip, *pdip;
7137 
7138 	for (dip = head; dip != NULL; dip = ddi_get_next(dip)) {
7139 		pdip = ddi_get_parent(dip);
7140 		ASSERT(pdip);	/* disallow rootnex.conf nodes */
7141 		major = ddi_driver_major(pdip);
7142 		if ((major != DDI_MAJOR_T_NONE) && parents[major] == 0)
7143 			parents[major] = 1;
7144 	}
7145 }
7146 
7147 /*
7148  * Call ddi_hold_installed_driver() on each parent major
7149  * and invoke mt_config_driver() to attach child major.
7150  * This is part of the implementation of ddi_hold_installed_driver.
7151  */
7152 static int
7153 attach_driver_by_parent(major_t child_major, char parents[])
7154 {
7155 	major_t par_major;
7156 	struct mt_config_handle *hdl;
7157 	int flags = NDI_DEVI_PERSIST | NDI_NO_EVENT;
7158 
7159 	hdl = mt_config_init(NULL, NULL, flags, child_major, MT_CONFIG_OP,
7160 	    NULL);
7161 	for (par_major = 0; par_major < devcnt; par_major++) {
7162 		/* disallow recursion on the same driver */
7163 		if (parents[par_major] == 0 || par_major == child_major)
7164 			continue;
7165 		if (ddi_hold_installed_driver(par_major) == NULL)
7166 			continue;
7167 		hdl->mtc_parmajor = par_major;
7168 		mt_config_driver(hdl);
7169 		ddi_rele_driver(par_major);
7170 	}
7171 	(void) mt_config_fini(hdl);
7172 
7173 	return (i_ddi_devs_attached(child_major));
7174 }
7175 
7176 int
7177 i_ddi_devs_attached(major_t major)
7178 {
7179 	dev_info_t *dip;
7180 	struct devnames *dnp;
7181 	int error = DDI_FAILURE;
7182 
7183 	/* check for attached instances */
7184 	dnp = &devnamesp[major];
7185 	LOCK_DEV_OPS(&dnp->dn_lock);
7186 	for (dip = dnp->dn_head; dip != NULL; dip = ddi_get_next(dip)) {
7187 		if (i_ddi_devi_attached(dip)) {
7188 			error = DDI_SUCCESS;
7189 			break;
7190 		}
7191 	}
7192 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7193 
7194 	return (error);
7195 }
7196 
7197 int
7198 i_ddi_minor_node_count(dev_info_t *ddip, const char *node_type)
7199 {
7200 	int			circ;
7201 	struct ddi_minor_data	*dp;
7202 	int			count = 0;
7203 
7204 	ndi_devi_enter(ddip, &circ);
7205 	for (dp = DEVI(ddip)->devi_minor; dp != NULL; dp = dp->next) {
7206 		if (strcmp(dp->ddm_node_type, node_type) == 0)
7207 			count++;
7208 	}
7209 	ndi_devi_exit(ddip, circ);
7210 	return (count);
7211 }
7212 
7213 /*
7214  * ddi_hold_installed_driver configures and attaches all
7215  * instances of the specified driver. To accomplish this
7216  * it configures and attaches all possible parents of
7217  * the driver, enumerated both in h/w nodes and in the
7218  * driver's .conf file.
7219  *
7220  * NOTE: This facility is for compatibility purposes only and will
7221  *	eventually go away. Its usage is strongly discouraged.
7222  */
7223 static void
7224 enter_driver(struct devnames *dnp)
7225 {
7226 	mutex_enter(&dnp->dn_lock);
7227 	ASSERT(dnp->dn_busy_thread != curthread);
7228 	while (dnp->dn_flags & DN_DRIVER_BUSY)
7229 		cv_wait(&dnp->dn_wait, &dnp->dn_lock);
7230 	dnp->dn_flags |= DN_DRIVER_BUSY;
7231 	dnp->dn_busy_thread = curthread;
7232 	mutex_exit(&dnp->dn_lock);
7233 }
7234 
7235 static void
7236 exit_driver(struct devnames *dnp)
7237 {
7238 	mutex_enter(&dnp->dn_lock);
7239 	ASSERT(dnp->dn_busy_thread == curthread);
7240 	dnp->dn_flags &= ~DN_DRIVER_BUSY;
7241 	dnp->dn_busy_thread = NULL;
7242 	cv_broadcast(&dnp->dn_wait);
7243 	mutex_exit(&dnp->dn_lock);
7244 }
7245 
7246 struct dev_ops *
7247 ddi_hold_installed_driver(major_t major)
7248 {
7249 	struct dev_ops *ops;
7250 	struct devnames *dnp;
7251 	char *parents;
7252 	int error;
7253 
7254 	ops = ddi_hold_driver(major);
7255 	if (ops == NULL)
7256 		return (NULL);
7257 
7258 	/*
7259 	 * Return immediately if all the attach operations associated
7260 	 * with a ddi_hold_installed_driver() call have already been done.
7261 	 */
7262 	dnp = &devnamesp[major];
7263 	enter_driver(dnp);
7264 	ASSERT(driver_installed(major));
7265 
7266 	if (dnp->dn_flags & DN_DRIVER_HELD) {
7267 		exit_driver(dnp);
7268 		if (i_ddi_devs_attached(major) == DDI_SUCCESS)
7269 			return (ops);
7270 		ddi_rele_driver(major);
7271 		return (NULL);
7272 	}
7273 
7274 	LOCK_DEV_OPS(&dnp->dn_lock);
7275 	dnp->dn_flags |= (DN_DRIVER_HELD | DN_NO_AUTODETACH);
7276 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7277 
7278 	DCOMPATPRINTF((CE_CONT,
7279 	    "ddi_hold_installed_driver: %s\n", dnp->dn_name));
7280 
7281 	/*
7282 	 * When the driver has no .conf children, it is sufficient
7283 	 * to attach existing nodes in the device tree. Nodes not
7284 	 * enumerated by the OBP are not attached.
7285 	 */
7286 	if (dnp->dn_pl == NULL) {
7287 		if (attach_driver_nodes(major) == DDI_SUCCESS) {
7288 			exit_driver(dnp);
7289 			return (ops);
7290 		}
7291 		exit_driver(dnp);
7292 		ddi_rele_driver(major);
7293 		return (NULL);
7294 	}
7295 
7296 	/*
7297 	 * Driver has .conf nodes. We find all possible parents
7298 	 * and recursively all ddi_hold_installed_driver on the
7299 	 * parent driver; then we invoke ndi_config_driver()
7300 	 * on all possible parent node in parallel to speed up
7301 	 * performance.
7302 	 */
7303 	parents = kmem_zalloc(devcnt * sizeof (char), KM_SLEEP);
7304 
7305 	LOCK_DEV_OPS(&dnp->dn_lock);
7306 	/* find .conf parents */
7307 	(void) impl_parlist_to_major(dnp->dn_pl, parents);
7308 	/* find hw node parents */
7309 	diplist_to_parent_major(dnp->dn_head, parents);
7310 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7311 
7312 	error = attach_driver_by_parent(major, parents);
7313 	kmem_free(parents, devcnt * sizeof (char));
7314 	if (error == DDI_SUCCESS) {
7315 		exit_driver(dnp);
7316 		return (ops);
7317 	}
7318 
7319 	exit_driver(dnp);
7320 	ddi_rele_driver(major);
7321 	return (NULL);
7322 }
7323 
7324 /*
7325  * Default bus_config entry point for nexus drivers
7326  */
7327 int
7328 ndi_busop_bus_config(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
7329     void *arg, dev_info_t **child, clock_t timeout)
7330 {
7331 	major_t major;
7332 
7333 	/*
7334 	 * A timeout of 30 minutes or more is probably a mistake
7335 	 * This is intended to catch uses where timeout is in
7336 	 * the wrong units.  timeout must be in units of ticks.
7337 	 */
7338 	ASSERT(timeout < SEC_TO_TICK(1800));
7339 
7340 	major = DDI_MAJOR_T_NONE;
7341 	switch (op) {
7342 	case BUS_CONFIG_ONE:
7343 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config %s timeout=%ld\n",
7344 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
7345 		    (char *)arg, timeout));
7346 		return (devi_config_one(pdip, (char *)arg, child, flags,
7347 		    timeout));
7348 
7349 	case BUS_CONFIG_DRIVER:
7350 		major = (major_t)(uintptr_t)arg;
7351 		/*FALLTHROUGH*/
7352 	case BUS_CONFIG_ALL:
7353 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config timeout=%ld\n",
7354 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
7355 		    timeout));
7356 		if (timeout > 0) {
7357 			NDI_DEBUG(flags, (CE_CONT,
7358 			    "%s%d: bus config all timeout=%ld\n",
7359 			    ddi_driver_name(pdip), ddi_get_instance(pdip),
7360 			    timeout));
7361 			delay(timeout);
7362 		}
7363 		return (config_immediate_children(pdip, flags, major));
7364 
7365 	default:
7366 		return (NDI_FAILURE);
7367 	}
7368 	/*NOTREACHED*/
7369 }
7370 
7371 /*
7372  * Default busop bus_unconfig handler for nexus drivers
7373  */
7374 int
7375 ndi_busop_bus_unconfig(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
7376     void *arg)
7377 {
7378 	major_t major;
7379 
7380 	major = DDI_MAJOR_T_NONE;
7381 	switch (op) {
7382 	case BUS_UNCONFIG_ONE:
7383 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig %s\n",
7384 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
7385 		    (char *)arg));
7386 		return (devi_unconfig_one(pdip, (char *)arg, flags));
7387 
7388 	case BUS_UNCONFIG_DRIVER:
7389 		major = (major_t)(uintptr_t)arg;
7390 		/*FALLTHROUGH*/
7391 	case BUS_UNCONFIG_ALL:
7392 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig all\n",
7393 		    ddi_driver_name(pdip), ddi_get_instance(pdip)));
7394 		return (unconfig_immediate_children(pdip, NULL, flags, major));
7395 
7396 	default:
7397 		return (NDI_FAILURE);
7398 	}
7399 	/*NOTREACHED*/
7400 }
7401 
7402 /*
7403  * dummy functions to be removed
7404  */
7405 void
7406 impl_rem_dev_props(dev_info_t *dip)
7407 {
7408 	_NOTE(ARGUNUSED(dip))
7409 	/* do nothing */
7410 }
7411 
7412 /*
7413  * Determine if a node is a leaf node. If not sure, return false (0).
7414  */
7415 static int
7416 is_leaf_node(dev_info_t *dip)
7417 {
7418 	major_t major = ddi_driver_major(dip);
7419 
7420 	if (major == DDI_MAJOR_T_NONE)
7421 		return (0);
7422 
7423 	return (devnamesp[major].dn_flags & DN_LEAF_DRIVER);
7424 }
7425 
7426 /*
7427  * Multithreaded [un]configuration
7428  */
7429 static struct mt_config_handle *
7430 mt_config_init(dev_info_t *pdip, dev_info_t **dipp, int flags,
7431     major_t major, int op, struct brevq_node **brevqp)
7432 {
7433 	struct mt_config_handle	*hdl = kmem_alloc(sizeof (*hdl), KM_SLEEP);
7434 
7435 	mutex_init(&hdl->mtc_lock, NULL, MUTEX_DEFAULT, NULL);
7436 	cv_init(&hdl->mtc_cv, NULL, CV_DEFAULT, NULL);
7437 	hdl->mtc_pdip = pdip;
7438 	hdl->mtc_fdip = dipp;
7439 	hdl->mtc_parmajor = DDI_MAJOR_T_NONE;
7440 	hdl->mtc_flags = flags;
7441 	hdl->mtc_major = major;
7442 	hdl->mtc_thr_count = 0;
7443 	hdl->mtc_op = op;
7444 	hdl->mtc_error = 0;
7445 	hdl->mtc_brevqp = brevqp;
7446 
7447 #ifdef DEBUG
7448 	gethrestime(&hdl->start_time);
7449 	hdl->total_time = 0;
7450 #endif /* DEBUG */
7451 
7452 	return (hdl);
7453 }
7454 
7455 #ifdef DEBUG
7456 static int
7457 time_diff_in_msec(timestruc_t start, timestruc_t end)
7458 {
7459 	int	nsec, sec;
7460 
7461 	sec = end.tv_sec - start.tv_sec;
7462 	nsec = end.tv_nsec - start.tv_nsec;
7463 	if (nsec < 0) {
7464 		nsec += NANOSEC;
7465 		sec -= 1;
7466 	}
7467 
7468 	return (sec * (NANOSEC >> 20) + (nsec >> 20));
7469 }
7470 
7471 #endif	/* DEBUG */
7472 
7473 static int
7474 mt_config_fini(struct mt_config_handle *hdl)
7475 {
7476 	int		rv;
7477 #ifdef DEBUG
7478 	int		real_time;
7479 	timestruc_t	end_time;
7480 #endif /* DEBUG */
7481 
7482 	mutex_enter(&hdl->mtc_lock);
7483 	while (hdl->mtc_thr_count > 0)
7484 		cv_wait(&hdl->mtc_cv, &hdl->mtc_lock);
7485 	rv = hdl->mtc_error;
7486 	mutex_exit(&hdl->mtc_lock);
7487 
7488 #ifdef DEBUG
7489 	gethrestime(&end_time);
7490 	real_time = time_diff_in_msec(hdl->start_time, end_time);
7491 	if ((ddidebug & DDI_MTCONFIG) && hdl->mtc_pdip)
7492 		cmn_err(CE_NOTE,
7493 		    "config %s%d: total time %d msec, real time %d msec",
7494 		    ddi_driver_name(hdl->mtc_pdip),
7495 		    ddi_get_instance(hdl->mtc_pdip),
7496 		    hdl->total_time, real_time);
7497 #endif /* DEBUG */
7498 
7499 	cv_destroy(&hdl->mtc_cv);
7500 	mutex_destroy(&hdl->mtc_lock);
7501 	kmem_free(hdl, sizeof (*hdl));
7502 
7503 	return (rv);
7504 }
7505 
7506 struct mt_config_data {
7507 	struct mt_config_handle	*mtc_hdl;
7508 	dev_info_t		*mtc_dip;
7509 	major_t			mtc_major;
7510 	int			mtc_flags;
7511 	struct brevq_node	*mtc_brn;
7512 	struct mt_config_data	*mtc_next;
7513 };
7514 
7515 static void
7516 mt_config_thread(void *arg)
7517 {
7518 	struct mt_config_data	*mcd = (struct mt_config_data *)arg;
7519 	struct mt_config_handle	*hdl = mcd->mtc_hdl;
7520 	dev_info_t		*dip = mcd->mtc_dip;
7521 	dev_info_t		*rdip, **dipp;
7522 	major_t			major = mcd->mtc_major;
7523 	int			flags = mcd->mtc_flags;
7524 	int			rv = 0;
7525 
7526 #ifdef DEBUG
7527 	timestruc_t start_time, end_time;
7528 	gethrestime(&start_time);
7529 #endif /* DEBUG */
7530 
7531 	rdip = NULL;
7532 	dipp = hdl->mtc_fdip ? &rdip : NULL;
7533 
7534 	switch (hdl->mtc_op) {
7535 	case MT_CONFIG_OP:
7536 		rv = devi_config_common(dip, flags, major);
7537 		break;
7538 	case MT_UNCONFIG_OP:
7539 		if (mcd->mtc_brn) {
7540 			struct brevq_node *brevq = NULL;
7541 			rv = devi_unconfig_common(dip, dipp, flags, major,
7542 			    &brevq);
7543 			mcd->mtc_brn->brn_child = brevq;
7544 		} else
7545 			rv = devi_unconfig_common(dip, dipp, flags, major,
7546 			    NULL);
7547 		break;
7548 	}
7549 
7550 	mutex_enter(&hdl->mtc_lock);
7551 #ifdef DEBUG
7552 	gethrestime(&end_time);
7553 	hdl->total_time += time_diff_in_msec(start_time, end_time);
7554 #endif /* DEBUG */
7555 
7556 	if ((rv != NDI_SUCCESS) && (hdl->mtc_error == 0)) {
7557 		hdl->mtc_error = rv;
7558 #ifdef	DEBUG
7559 		if ((ddidebug & DDI_DEBUG) && (major != DDI_MAJOR_T_NONE)) {
7560 			char	*path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7561 
7562 			(void) ddi_pathname(dip, path);
7563 			cmn_err(CE_NOTE, "mt_config_thread: "
7564 			    "op %d.%d.%x at %s failed %d",
7565 			    hdl->mtc_op, major, flags, path, rv);
7566 			kmem_free(path, MAXPATHLEN);
7567 		}
7568 #endif	/* DEBUG */
7569 	}
7570 
7571 	if (hdl->mtc_fdip && *hdl->mtc_fdip == NULL) {
7572 		*hdl->mtc_fdip = rdip;
7573 		rdip = NULL;
7574 	}
7575 
7576 	if (rdip) {
7577 		ASSERT(rv != NDI_SUCCESS);
7578 		ndi_rele_devi(rdip);
7579 	}
7580 
7581 	ndi_rele_devi(dip);
7582 
7583 	if (--hdl->mtc_thr_count == 0)
7584 		cv_broadcast(&hdl->mtc_cv);
7585 	mutex_exit(&hdl->mtc_lock);
7586 	kmem_free(mcd, sizeof (*mcd));
7587 }
7588 
7589 /*
7590  * Multi-threaded config/unconfig of child nexus
7591  */
7592 static void
7593 mt_config_children(struct mt_config_handle *hdl)
7594 {
7595 	dev_info_t		*pdip = hdl->mtc_pdip;
7596 	major_t			major = hdl->mtc_major;
7597 	dev_info_t		*dip;
7598 	int			circ;
7599 	struct brevq_node	*brn;
7600 	struct mt_config_data	*mcd_head = NULL;
7601 	struct mt_config_data	*mcd_tail = NULL;
7602 	struct mt_config_data	*mcd;
7603 #ifdef DEBUG
7604 	timestruc_t		end_time;
7605 
7606 	/* Update total_time in handle */
7607 	gethrestime(&end_time);
7608 	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
7609 #endif
7610 
7611 	ndi_devi_enter(pdip, &circ);
7612 	dip = ddi_get_child(pdip);
7613 	while (dip) {
7614 		if (hdl->mtc_op == MT_UNCONFIG_OP && hdl->mtc_brevqp &&
7615 		    !(DEVI_EVREMOVE(dip)) &&
7616 		    i_ddi_node_state(dip) >= DS_INITIALIZED) {
7617 			/*
7618 			 * Enqueue this dip's deviname.
7619 			 * No need to hold a lock while enqueuing since this
7620 			 * is the only thread doing the enqueue and no one
7621 			 * walks the queue while we are in multithreaded
7622 			 * unconfiguration.
7623 			 */
7624 			brn = brevq_enqueue(hdl->mtc_brevqp, dip, NULL);
7625 		} else
7626 			brn = NULL;
7627 
7628 		/*
7629 		 * Hold the child that we are processing so he does not get
7630 		 * removed. The corrisponding ndi_rele_devi() for children
7631 		 * that are not being skipped is done at the end of
7632 		 * mt_config_thread().
7633 		 */
7634 		ndi_hold_devi(dip);
7635 
7636 		/*
7637 		 * skip leaf nodes and (for configure) nodes not
7638 		 * fully attached.
7639 		 */
7640 		if (is_leaf_node(dip) ||
7641 		    (hdl->mtc_op == MT_CONFIG_OP &&
7642 		    i_ddi_node_state(dip) < DS_READY)) {
7643 			ndi_rele_devi(dip);
7644 			dip = ddi_get_next_sibling(dip);
7645 			continue;
7646 		}
7647 
7648 		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
7649 		mcd->mtc_dip = dip;
7650 		mcd->mtc_hdl = hdl;
7651 		mcd->mtc_brn = brn;
7652 
7653 		/*
7654 		 * Switch a 'driver' operation to an 'all' operation below a
7655 		 * node bound to the driver.
7656 		 */
7657 		if ((major == DDI_MAJOR_T_NONE) ||
7658 		    (major == ddi_driver_major(dip)))
7659 			mcd->mtc_major = DDI_MAJOR_T_NONE;
7660 		else
7661 			mcd->mtc_major = major;
7662 
7663 		/*
7664 		 * The unconfig-driver to unconfig-all conversion above
7665 		 * constitutes an autodetach for NDI_DETACH_DRIVER calls,
7666 		 * set NDI_AUTODETACH.
7667 		 */
7668 		mcd->mtc_flags = hdl->mtc_flags;
7669 		if ((mcd->mtc_flags & NDI_DETACH_DRIVER) &&
7670 		    (hdl->mtc_op == MT_UNCONFIG_OP) &&
7671 		    (major == ddi_driver_major(pdip)))
7672 			mcd->mtc_flags |= NDI_AUTODETACH;
7673 
7674 		mutex_enter(&hdl->mtc_lock);
7675 		hdl->mtc_thr_count++;
7676 		mutex_exit(&hdl->mtc_lock);
7677 
7678 		/*
7679 		 * Add to end of list to process after ndi_devi_exit to avoid
7680 		 * locking differences depending on value of mtc_off.
7681 		 */
7682 		mcd->mtc_next = NULL;
7683 		if (mcd_head == NULL)
7684 			mcd_head = mcd;
7685 		else
7686 			mcd_tail->mtc_next = mcd;
7687 		mcd_tail = mcd;
7688 
7689 		dip = ddi_get_next_sibling(dip);
7690 	}
7691 	ndi_devi_exit(pdip, circ);
7692 
7693 	/* go through the list of held children */
7694 	for (mcd = mcd_head; mcd; mcd = mcd_head) {
7695 		mcd_head = mcd->mtc_next;
7696 		if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
7697 			mt_config_thread(mcd);
7698 		else
7699 			(void) thread_create(NULL, 0, mt_config_thread, mcd,
7700 			    0, &p0, TS_RUN, minclsyspri);
7701 	}
7702 }
7703 
7704 static void
7705 mt_config_driver(struct mt_config_handle *hdl)
7706 {
7707 	major_t			par_major = hdl->mtc_parmajor;
7708 	major_t			major = hdl->mtc_major;
7709 	struct devnames		*dnp = &devnamesp[par_major];
7710 	dev_info_t		*dip;
7711 	struct mt_config_data	*mcd_head = NULL;
7712 	struct mt_config_data	*mcd_tail = NULL;
7713 	struct mt_config_data	*mcd;
7714 #ifdef DEBUG
7715 	timestruc_t		end_time;
7716 
7717 	/* Update total_time in handle */
7718 	gethrestime(&end_time);
7719 	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
7720 #endif
7721 	ASSERT(par_major != DDI_MAJOR_T_NONE);
7722 	ASSERT(major != DDI_MAJOR_T_NONE);
7723 
7724 	LOCK_DEV_OPS(&dnp->dn_lock);
7725 	dip = devnamesp[par_major].dn_head;
7726 	while (dip) {
7727 		/*
7728 		 * Hold the child that we are processing so he does not get
7729 		 * removed. The corrisponding ndi_rele_devi() for children
7730 		 * that are not being skipped is done at the end of
7731 		 * mt_config_thread().
7732 		 */
7733 		ndi_hold_devi(dip);
7734 
7735 		/* skip leaf nodes and nodes not fully attached */
7736 		if (!i_ddi_devi_attached(dip) || is_leaf_node(dip)) {
7737 			ndi_rele_devi(dip);
7738 			dip = ddi_get_next(dip);
7739 			continue;
7740 		}
7741 
7742 		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
7743 		mcd->mtc_dip = dip;
7744 		mcd->mtc_hdl = hdl;
7745 		mcd->mtc_major = major;
7746 		mcd->mtc_flags = hdl->mtc_flags;
7747 
7748 		mutex_enter(&hdl->mtc_lock);
7749 		hdl->mtc_thr_count++;
7750 		mutex_exit(&hdl->mtc_lock);
7751 
7752 		/*
7753 		 * Add to end of list to process after UNLOCK_DEV_OPS to avoid
7754 		 * locking differences depending on value of mtc_off.
7755 		 */
7756 		mcd->mtc_next = NULL;
7757 		if (mcd_head == NULL)
7758 			mcd_head = mcd;
7759 		else
7760 			mcd_tail->mtc_next = mcd;
7761 		mcd_tail = mcd;
7762 
7763 		dip = ddi_get_next(dip);
7764 	}
7765 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7766 
7767 	/* go through the list of held children */
7768 	for (mcd = mcd_head; mcd; mcd = mcd_head) {
7769 		mcd_head = mcd->mtc_next;
7770 		if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
7771 			mt_config_thread(mcd);
7772 		else
7773 			(void) thread_create(NULL, 0, mt_config_thread, mcd,
7774 			    0, &p0, TS_RUN, minclsyspri);
7775 	}
7776 }
7777 
7778 /*
7779  * Given the nodeid for a persistent (PROM or SID) node, return
7780  * the corresponding devinfo node
7781  * NOTE: This function will return NULL for .conf nodeids.
7782  */
7783 dev_info_t *
7784 e_ddi_nodeid_to_dip(pnode_t nodeid)
7785 {
7786 	dev_info_t		*dip = NULL;
7787 	struct devi_nodeid	*prev, *elem;
7788 
7789 	mutex_enter(&devimap->dno_lock);
7790 
7791 	prev = NULL;
7792 	for (elem = devimap->dno_head; elem; elem = elem->next) {
7793 		if (elem->nodeid == nodeid) {
7794 			ndi_hold_devi(elem->dip);
7795 			dip = elem->dip;
7796 			break;
7797 		}
7798 		prev = elem;
7799 	}
7800 
7801 	/*
7802 	 * Move to head for faster lookup next time
7803 	 */
7804 	if (elem && prev) {
7805 		prev->next = elem->next;
7806 		elem->next = devimap->dno_head;
7807 		devimap->dno_head = elem;
7808 	}
7809 
7810 	mutex_exit(&devimap->dno_lock);
7811 	return (dip);
7812 }
7813 
7814 static void
7815 free_cache_task(void *arg)
7816 {
7817 	ASSERT(arg == NULL);
7818 
7819 	mutex_enter(&di_cache.cache_lock);
7820 
7821 	/*
7822 	 * The cache can be invalidated without holding the lock
7823 	 * but it can be made valid again only while the lock is held.
7824 	 * So if the cache is invalid when the lock is held, it will
7825 	 * stay invalid until lock is released.
7826 	 */
7827 	if (!di_cache.cache_valid)
7828 		i_ddi_di_cache_free(&di_cache);
7829 
7830 	mutex_exit(&di_cache.cache_lock);
7831 
7832 	if (di_cache_debug)
7833 		cmn_err(CE_NOTE, "system_taskq: di_cache freed");
7834 }
7835 
7836 extern int modrootloaded;
7837 
7838 void
7839 i_ddi_di_cache_free(struct di_cache *cache)
7840 {
7841 	int	error;
7842 	extern int sys_shutdown;
7843 
7844 	ASSERT(mutex_owned(&cache->cache_lock));
7845 
7846 	if (cache->cache_size) {
7847 		ASSERT(cache->cache_size > 0);
7848 		ASSERT(cache->cache_data);
7849 
7850 		kmem_free(cache->cache_data, cache->cache_size);
7851 		cache->cache_data = NULL;
7852 		cache->cache_size = 0;
7853 
7854 		if (di_cache_debug)
7855 			cmn_err(CE_NOTE, "i_ddi_di_cache_free: freed cachemem");
7856 	} else {
7857 		ASSERT(cache->cache_data == NULL);
7858 		if (di_cache_debug)
7859 			cmn_err(CE_NOTE, "i_ddi_di_cache_free: NULL cache");
7860 	}
7861 
7862 	if (!modrootloaded || rootvp == NULL ||
7863 	    vn_is_readonly(rootvp) || sys_shutdown) {
7864 		if (di_cache_debug) {
7865 			cmn_err(CE_WARN, "/ not mounted/RDONLY. Skip unlink");
7866 		}
7867 		return;
7868 	}
7869 
7870 	error = vn_remove(DI_CACHE_FILE, UIO_SYSSPACE, RMFILE);
7871 	if (di_cache_debug && error && error != ENOENT) {
7872 		cmn_err(CE_WARN, "%s: unlink failed: %d", DI_CACHE_FILE, error);
7873 	} else if (di_cache_debug && !error) {
7874 		cmn_err(CE_NOTE, "i_ddi_di_cache_free: unlinked cache file");
7875 	}
7876 }
7877 
7878 void
7879 i_ddi_di_cache_invalidate()
7880 {
7881 	int	cache_valid;
7882 
7883 	if (!modrootloaded || !i_ddi_io_initialized()) {
7884 		if (di_cache_debug)
7885 			cmn_err(CE_NOTE, "I/O not inited. Skipping invalidate");
7886 		return;
7887 	}
7888 
7889 	/* Increment devtree generation number. */
7890 	atomic_inc_ulong(&devtree_gen);
7891 
7892 	/* Invalidate the in-core cache and dispatch free on valid->invalid */
7893 	cache_valid = atomic_swap_uint(&di_cache.cache_valid, 0);
7894 	if (cache_valid) {
7895 		/*
7896 		 * This is an optimization to start cleaning up a cached
7897 		 * snapshot early.  For this reason, it is OK for
7898 		 * taskq_dispatach to fail (and it is OK to not track calling
7899 		 * context relative to sleep, and assume NOSLEEP).
7900 		 */
7901 		(void) taskq_dispatch(system_taskq, free_cache_task, NULL,
7902 		    TQ_NOSLEEP);
7903 	}
7904 
7905 	if (di_cache_debug) {
7906 		cmn_err(CE_NOTE, "invalidation");
7907 	}
7908 }
7909 
7910 
7911 static void
7912 i_bind_vhci_node(dev_info_t *dip)
7913 {
7914 	DEVI(dip)->devi_major = ddi_name_to_major(ddi_node_name(dip));
7915 	i_ddi_set_node_state(dip, DS_BOUND);
7916 }
7917 
7918 static char vhci_node_addr[2];
7919 
7920 static int
7921 i_init_vhci_node(dev_info_t *dip)
7922 {
7923 	add_global_props(dip);
7924 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
7925 	if (DEVI(dip)->devi_ops == NULL)
7926 		return (-1);
7927 
7928 	DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
7929 	e_ddi_keep_instance(dip);
7930 	vhci_node_addr[0]	= '\0';
7931 	ddi_set_name_addr(dip, vhci_node_addr);
7932 	i_ddi_set_node_state(dip, DS_INITIALIZED);
7933 	return (0);
7934 }
7935 
7936 static void
7937 i_link_vhci_node(dev_info_t *dip)
7938 {
7939 	ASSERT(MUTEX_HELD(&global_vhci_lock));
7940 
7941 	/*
7942 	 * scsi_vhci should be kept left most of the device tree.
7943 	 */
7944 	if (scsi_vhci_dip) {
7945 		DEVI(dip)->devi_sibling = DEVI(scsi_vhci_dip)->devi_sibling;
7946 		DEVI(scsi_vhci_dip)->devi_sibling = DEVI(dip);
7947 	} else {
7948 		DEVI(dip)->devi_sibling = DEVI(top_devinfo)->devi_child;
7949 		DEVI(top_devinfo)->devi_child = DEVI(dip);
7950 	}
7951 }
7952 
7953 
7954 /*
7955  * This a special routine to enumerate vhci node (child of rootnex
7956  * node) without holding the ndi_devi_enter() lock. The device node
7957  * is allocated, initialized and brought into DS_READY state before
7958  * inserting into the device tree. The VHCI node is handcrafted
7959  * here to bring the node to DS_READY, similar to rootnex node.
7960  *
7961  * The global_vhci_lock protects linking the node into the device
7962  * as same lock is held before linking/unlinking any direct child
7963  * of rootnex children.
7964  *
7965  * This routine is a workaround to handle a possible deadlock
7966  * that occurs while trying to enumerate node in a different sub-tree
7967  * during _init/_attach entry points.
7968  */
7969 /*ARGSUSED*/
7970 dev_info_t *
7971 ndi_devi_config_vhci(char *drvname, int flags)
7972 {
7973 	struct devnames		*dnp;
7974 	dev_info_t		*dip;
7975 	major_t			major = ddi_name_to_major(drvname);
7976 
7977 	if (major == -1)
7978 		return (NULL);
7979 
7980 	/* Make sure we create the VHCI node only once */
7981 	dnp = &devnamesp[major];
7982 	LOCK_DEV_OPS(&dnp->dn_lock);
7983 	if (dnp->dn_head) {
7984 		dip = dnp->dn_head;
7985 		UNLOCK_DEV_OPS(&dnp->dn_lock);
7986 		return (dip);
7987 	}
7988 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7989 
7990 	/* Allocate the VHCI node */
7991 	ndi_devi_alloc_sleep(top_devinfo, drvname, DEVI_SID_NODEID, &dip);
7992 	ndi_hold_devi(dip);
7993 
7994 	/* Mark the node as VHCI */
7995 	DEVI(dip)->devi_node_attributes |= DDI_VHCI_NODE;
7996 
7997 	i_ddi_add_devimap(dip);
7998 	i_bind_vhci_node(dip);
7999 	if (i_init_vhci_node(dip) == -1) {
8000 		ndi_rele_devi(dip);
8001 		(void) ndi_devi_free(dip);
8002 		return (NULL);
8003 	}
8004 
8005 	mutex_enter(&(DEVI(dip)->devi_lock));
8006 	DEVI_SET_ATTACHING(dip);
8007 	mutex_exit(&(DEVI(dip)->devi_lock));
8008 
8009 	if (devi_attach(dip, DDI_ATTACH) != DDI_SUCCESS) {
8010 		cmn_err(CE_CONT, "Could not attach %s driver", drvname);
8011 		e_ddi_free_instance(dip, vhci_node_addr);
8012 		ndi_rele_devi(dip);
8013 		(void) ndi_devi_free(dip);
8014 		return (NULL);
8015 	}
8016 	mutex_enter(&(DEVI(dip)->devi_lock));
8017 	DEVI_CLR_ATTACHING(dip);
8018 	mutex_exit(&(DEVI(dip)->devi_lock));
8019 
8020 	mutex_enter(&global_vhci_lock);
8021 	i_link_vhci_node(dip);
8022 	mutex_exit(&global_vhci_lock);
8023 	i_ddi_set_node_state(dip, DS_READY);
8024 
8025 	LOCK_DEV_OPS(&dnp->dn_lock);
8026 	dnp->dn_flags |= DN_DRIVER_HELD;
8027 	dnp->dn_head = dip;
8028 	UNLOCK_DEV_OPS(&dnp->dn_lock);
8029 
8030 	i_ndi_devi_report_status_change(dip, NULL);
8031 
8032 	return (dip);
8033 }
8034 
8035 /*
8036  * Maintain DEVI_DEVICE_REMOVED hotplug devi_state for remove/reinsert hotplug
8037  * of open devices. Currently, because of tight coupling between the devfs file
8038  * system and the Solaris device tree, a driver can't always make the device
8039  * tree state (esp devi_node_state) match device hardware hotplug state. Until
8040  * resolved, to overcome this deficiency we use the following interfaces that
8041  * maintain the DEVI_DEVICE_REMOVED devi_state status bit.  These interface
8042  * report current state, and drive operation (like events and cache
8043  * invalidation) when a driver changes remove/insert state of an open device.
8044  *
8045  * The ndi_devi_device_isremoved() returns 1 if the device is currently removed.
8046  *
8047  * The ndi_devi_device_remove() interface declares the device as removed, and
8048  * returns 1 if there was a state change associated with this declaration.
8049  *
8050  * The ndi_devi_device_insert() declares the device as inserted, and returns 1
8051  * if there was a state change associated with this declaration.
8052  */
8053 int
8054 ndi_devi_device_isremoved(dev_info_t *dip)
8055 {
8056 	return (DEVI_IS_DEVICE_REMOVED(dip));
8057 }
8058 
8059 int
8060 ndi_devi_device_remove(dev_info_t *dip)
8061 {
8062 	ASSERT(dip && ddi_get_parent(dip) &&
8063 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
8064 
8065 	/* Return if already marked removed. */
8066 	if (ndi_devi_device_isremoved(dip))
8067 		return (0);
8068 
8069 	/* Mark the device as having been physically removed. */
8070 	mutex_enter(&(DEVI(dip)->devi_lock));
8071 	ndi_devi_set_hidden(dip);	/* invisible: lookup/snapshot */
8072 	DEVI_SET_DEVICE_REMOVED(dip);
8073 	DEVI_SET_EVREMOVE(dip);		/* this clears EVADD too */
8074 	mutex_exit(&(DEVI(dip)->devi_lock));
8075 
8076 	/* report remove (as 'removed') */
8077 	i_ndi_devi_report_status_change(dip, NULL);
8078 
8079 	/*
8080 	 * Invalidate the cache to ensure accurate
8081 	 * (di_state() & DI_DEVICE_REMOVED).
8082 	 */
8083 	i_ddi_di_cache_invalidate();
8084 
8085 	/*
8086 	 * Generate sysevent for those interested in removal (either
8087 	 * directly via private EC_DEVFS or indirectly via devfsadmd
8088 	 * generated EC_DEV). This will generate LDI DEVICE_REMOVE
8089 	 * event too.
8090 	 */
8091 	i_ddi_log_devfs_device_remove(dip);
8092 
8093 	return (1);		/* DEVICE_REMOVED state changed */
8094 }
8095 
8096 int
8097 ndi_devi_device_insert(dev_info_t *dip)
8098 {
8099 	ASSERT(dip && ddi_get_parent(dip) &&
8100 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
8101 
8102 	/* Return if not marked removed. */
8103 	if (!ndi_devi_device_isremoved(dip))
8104 		return (0);
8105 
8106 	/* Mark the device as having been physically reinserted. */
8107 	mutex_enter(&(DEVI(dip)->devi_lock));
8108 	ndi_devi_clr_hidden(dip);	/* visible: lookup/snapshot */
8109 	DEVI_SET_DEVICE_REINSERTED(dip);
8110 	DEVI_SET_EVADD(dip);		/* this clears EVREMOVE too */
8111 	mutex_exit(&(DEVI(dip)->devi_lock));
8112 
8113 	/* report insert (as 'online') */
8114 	i_ndi_devi_report_status_change(dip, NULL);
8115 
8116 	/*
8117 	 * Invalidate the cache to ensure accurate
8118 	 * (di_state() & DI_DEVICE_REMOVED).
8119 	 */
8120 	i_ddi_di_cache_invalidate();
8121 
8122 	/*
8123 	 * Generate sysevent for those interested in removal (either directly
8124 	 * via EC_DEVFS or indirectly via devfsadmd generated EC_DEV).
8125 	 */
8126 	i_ddi_log_devfs_device_insert(dip);
8127 
8128 	return (1);		/* DEVICE_REMOVED state changed */
8129 }
8130 
8131 /*
8132  * ibt_hw_is_present() returns 0 when there is no IB hardware actively
8133  * running.  This is primarily useful for modules like rpcmod which
8134  * needs a quick check to decide whether or not it should try to use
8135  * InfiniBand
8136  */
8137 int ib_hw_status = 0;
8138 int
8139 ibt_hw_is_present()
8140 {
8141 	return (ib_hw_status);
8142 }
8143 
8144 /*
8145  * ASSERT that constraint flag is not set and then set the "retire attempt"
8146  * flag.
8147  */
8148 int
8149 e_ddi_mark_retiring(dev_info_t *dip, void *arg)
8150 {
8151 	char	**cons_array = (char **)arg;
8152 	char	*path;
8153 	int	constraint;
8154 	int	i;
8155 
8156 	constraint = 0;
8157 	if (cons_array) {
8158 		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8159 		(void) ddi_pathname(dip, path);
8160 		for (i = 0; cons_array[i] != NULL; i++) {
8161 			if (strcmp(path, cons_array[i]) == 0) {
8162 				constraint = 1;
8163 				break;
8164 			}
8165 		}
8166 		kmem_free(path, MAXPATHLEN);
8167 	}
8168 
8169 	mutex_enter(&DEVI(dip)->devi_lock);
8170 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
8171 	DEVI(dip)->devi_flags |= DEVI_RETIRING;
8172 	if (constraint)
8173 		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
8174 	mutex_exit(&DEVI(dip)->devi_lock);
8175 
8176 	RIO_VERBOSE((CE_NOTE, "marked dip as undergoing retire process dip=%p",
8177 	    (void *)dip));
8178 
8179 	if (constraint)
8180 		RIO_DEBUG((CE_NOTE, "marked dip as constrained, dip=%p",
8181 		    (void *)dip));
8182 
8183 	if (MDI_PHCI(dip))
8184 		mdi_phci_mark_retiring(dip, cons_array);
8185 
8186 	return (DDI_WALK_CONTINUE);
8187 }
8188 
8189 static void
8190 free_array(char **cons_array)
8191 {
8192 	int	i;
8193 
8194 	if (cons_array == NULL)
8195 		return;
8196 
8197 	for (i = 0; cons_array[i] != NULL; i++) {
8198 		kmem_free(cons_array[i], strlen(cons_array[i]) + 1);
8199 	}
8200 	kmem_free(cons_array, (i+1) * sizeof (char *));
8201 }
8202 
8203 /*
8204  * Walk *every* node in subtree and check if it blocks, allows or has no
8205  * comment on a proposed retire.
8206  */
8207 int
8208 e_ddi_retire_notify(dev_info_t *dip, void *arg)
8209 {
8210 	int	*constraint = (int *)arg;
8211 
8212 	RIO_DEBUG((CE_NOTE, "retire notify: dip = %p", (void *)dip));
8213 
8214 	(void) e_ddi_offline_notify(dip);
8215 
8216 	mutex_enter(&(DEVI(dip)->devi_lock));
8217 	if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) {
8218 		RIO_DEBUG((CE_WARN, "retire notify: dip in retire "
8219 		    "subtree is not marked: dip = %p", (void *)dip));
8220 		*constraint = 0;
8221 	} else if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) {
8222 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
8223 		RIO_DEBUG((CE_NOTE, "retire notify: BLOCKED: dip = %p",
8224 		    (void *)dip));
8225 		*constraint = 0;
8226 	} else if (!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT)) {
8227 		RIO_DEBUG((CE_NOTE, "retire notify: NO CONSTRAINT: "
8228 		    "dip = %p", (void *)dip));
8229 		*constraint = 0;
8230 	} else {
8231 		RIO_DEBUG((CE_NOTE, "retire notify: CONSTRAINT set: "
8232 		    "dip = %p", (void *)dip));
8233 	}
8234 	mutex_exit(&DEVI(dip)->devi_lock);
8235 
8236 	if (MDI_PHCI(dip))
8237 		mdi_phci_retire_notify(dip, constraint);
8238 
8239 	return (DDI_WALK_CONTINUE);
8240 }
8241 
8242 int
8243 e_ddi_retire_finalize(dev_info_t *dip, void *arg)
8244 {
8245 	int constraint = *(int *)arg;
8246 	int finalize;
8247 	int phci_only;
8248 
8249 	mutex_enter(&DEVI(dip)->devi_lock);
8250 	if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) {
8251 		RIO_DEBUG((CE_WARN,
8252 		    "retire: unmarked dip(%p) in retire subtree",
8253 		    (void *)dip));
8254 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRED));
8255 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
8256 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
8257 		mutex_exit(&DEVI(dip)->devi_lock);
8258 		return (DDI_WALK_CONTINUE);
8259 	}
8260 
8261 	/*
8262 	 * retire the device if constraints have been applied
8263 	 * or if the device is not in use
8264 	 */
8265 	finalize = 0;
8266 	if (constraint) {
8267 		ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
8268 
8269 		ASSERT(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT);
8270 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
8271 		DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
8272 		DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
8273 		DEVI(dip)->devi_flags |= DEVI_RETIRED;
8274 		mutex_exit(&DEVI(dip)->devi_lock);
8275 		(void) spec_fence_snode(dip, NULL);
8276 		RIO_DEBUG((CE_NOTE, "Fenced off: dip = %p", (void *)dip));
8277 		e_ddi_offline_finalize(dip, DDI_SUCCESS);
8278 	} else {
8279 		if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) {
8280 			ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
8281 			DEVI(dip)->devi_flags &= ~DEVI_R_BLOCKED;
8282 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
8283 			/* we have already finalized during notify */
8284 		} else if (DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT) {
8285 			DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
8286 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
8287 			finalize = 1;
8288 		} else {
8289 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
8290 			/*
8291 			 * even if no contracts, need to call finalize
8292 			 * to clear the contract barrier on the dip
8293 			 */
8294 			finalize = 1;
8295 		}
8296 		mutex_exit(&DEVI(dip)->devi_lock);
8297 		RIO_DEBUG((CE_NOTE, "finalize: NOT retired: dip = %p",
8298 		    (void *)dip));
8299 		if (finalize)
8300 			e_ddi_offline_finalize(dip, DDI_FAILURE);
8301 	}
8302 
8303 	/*
8304 	 * phci_only variable indicates no client checking, just
8305 	 * offline the PHCI. We set that to 0 to enable client
8306 	 * checking
8307 	 */
8308 	phci_only = 0;
8309 	if (MDI_PHCI(dip))
8310 		mdi_phci_retire_finalize(dip, phci_only, arg);
8311 
8312 	return (DDI_WALK_CONTINUE);
8313 }
8314 
8315 /*
8316  * Returns
8317  *	DDI_SUCCESS if constraints allow retire
8318  *	DDI_FAILURE if constraints don't allow retire.
8319  * cons_array is a NULL terminated array of node paths for
8320  * which constraints have already been applied.
8321  */
8322 int
8323 e_ddi_retire_device(char *path, char **cons_array)
8324 {
8325 	dev_info_t	*dip;
8326 	dev_info_t	*pdip;
8327 	int		circ;
8328 	int		circ2;
8329 	int		constraint;
8330 	char		*devnm;
8331 
8332 	/*
8333 	 * First, lookup the device
8334 	 */
8335 	dip = e_ddi_hold_devi_by_path(path, 0);
8336 	if (dip == NULL) {
8337 		/*
8338 		 * device does not exist. This device cannot be
8339 		 * a critical device since it is not in use. Thus
8340 		 * this device is always retireable. Return DDI_SUCCESS
8341 		 * to indicate this. If this device is ever
8342 		 * instantiated, I/O framework will consult the
8343 		 * the persistent retire store, mark it as
8344 		 * retired and fence it off.
8345 		 */
8346 		RIO_DEBUG((CE_NOTE, "Retire device: device doesn't exist."
8347 		    " NOP. Just returning SUCCESS. path=%s", path));
8348 		free_array(cons_array);
8349 		return (DDI_SUCCESS);
8350 	}
8351 
8352 	RIO_DEBUG((CE_NOTE, "Retire device: found dip = %p.", (void *)dip));
8353 
8354 	pdip = ddi_get_parent(dip);
8355 	ndi_hold_devi(pdip);
8356 
8357 	/*
8358 	 * Run devfs_clean() in case dip has no constraints and is
8359 	 * not in use, so is retireable but there are dv_nodes holding
8360 	 * ref-count on the dip. Note that devfs_clean() always returns
8361 	 * success.
8362 	 */
8363 	devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
8364 	(void) ddi_deviname(dip, devnm);
8365 	(void) devfs_clean(pdip, devnm + 1, DV_CLEAN_FORCE);
8366 	kmem_free(devnm, MAXNAMELEN + 1);
8367 
8368 	ndi_devi_enter(pdip, &circ);
8369 
8370 	/* release hold from e_ddi_hold_devi_by_path */
8371 	ndi_rele_devi(dip);
8372 
8373 	/*
8374 	 * If it cannot make a determination, is_leaf_node() assumes
8375 	 * dip is a nexus.
8376 	 */
8377 	(void) e_ddi_mark_retiring(dip, cons_array);
8378 	if (!is_leaf_node(dip)) {
8379 		ndi_devi_enter(dip, &circ2);
8380 		ddi_walk_devs(ddi_get_child(dip), e_ddi_mark_retiring,
8381 		    cons_array);
8382 		ndi_devi_exit(dip, circ2);
8383 	}
8384 	free_array(cons_array);
8385 
8386 	/*
8387 	 * apply constraints
8388 	 */
8389 	RIO_DEBUG((CE_NOTE, "retire: subtree retire notify: path = %s", path));
8390 
8391 	constraint = 1;	/* assume constraints allow retire */
8392 	(void) e_ddi_retire_notify(dip, &constraint);
8393 	if (!is_leaf_node(dip)) {
8394 		ndi_devi_enter(dip, &circ2);
8395 		ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_notify,
8396 		    &constraint);
8397 		ndi_devi_exit(dip, circ2);
8398 	}
8399 
8400 	/*
8401 	 * Now finalize the retire
8402 	 */
8403 	(void) e_ddi_retire_finalize(dip, &constraint);
8404 	if (!is_leaf_node(dip)) {
8405 		ndi_devi_enter(dip, &circ2);
8406 		ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_finalize,
8407 		    &constraint);
8408 		ndi_devi_exit(dip, circ2);
8409 	}
8410 
8411 	if (!constraint) {
8412 		RIO_DEBUG((CE_WARN, "retire failed: path = %s", path));
8413 	} else {
8414 		RIO_DEBUG((CE_NOTE, "retire succeeded: path = %s", path));
8415 	}
8416 
8417 	ndi_devi_exit(pdip, circ);
8418 	ndi_rele_devi(pdip);
8419 	return (constraint ? DDI_SUCCESS : DDI_FAILURE);
8420 }
8421 
8422 static int
8423 unmark_and_unfence(dev_info_t *dip, void *arg)
8424 {
8425 	char	*path = (char *)arg;
8426 
8427 	ASSERT(path);
8428 
8429 	(void) ddi_pathname(dip, path);
8430 
8431 	mutex_enter(&DEVI(dip)->devi_lock);
8432 	DEVI(dip)->devi_flags &= ~DEVI_RETIRED;
8433 	DEVI_SET_DEVICE_ONLINE(dip);
8434 	mutex_exit(&DEVI(dip)->devi_lock);
8435 
8436 	RIO_VERBOSE((CE_NOTE, "Cleared RETIRED flag: dip=%p, path=%s",
8437 	    (void *)dip, path));
8438 
8439 	(void) spec_unfence_snode(dip);
8440 	RIO_DEBUG((CE_NOTE, "Unfenced device: %s", path));
8441 
8442 	if (MDI_PHCI(dip))
8443 		mdi_phci_unretire(dip);
8444 
8445 	return (DDI_WALK_CONTINUE);
8446 }
8447 
8448 struct find_dip {
8449 	char	*fd_buf;
8450 	char	*fd_path;
8451 	dev_info_t *fd_dip;
8452 };
8453 
8454 static int
8455 find_dip_fcn(dev_info_t *dip, void *arg)
8456 {
8457 	struct find_dip *findp = (struct find_dip *)arg;
8458 
8459 	(void) ddi_pathname(dip, findp->fd_buf);
8460 
8461 	if (strcmp(findp->fd_path, findp->fd_buf) != 0)
8462 		return (DDI_WALK_CONTINUE);
8463 
8464 	ndi_hold_devi(dip);
8465 	findp->fd_dip = dip;
8466 
8467 	return (DDI_WALK_TERMINATE);
8468 }
8469 
8470 int
8471 e_ddi_unretire_device(char *path)
8472 {
8473 	int		circ;
8474 	int		circ2;
8475 	char		*path2;
8476 	dev_info_t	*pdip;
8477 	dev_info_t	*dip;
8478 	struct find_dip	 find_dip;
8479 
8480 	ASSERT(path);
8481 	ASSERT(*path == '/');
8482 
8483 	if (strcmp(path, "/") == 0) {
8484 		cmn_err(CE_WARN, "Root node cannot be retired. Skipping "
8485 		    "device unretire: %s", path);
8486 		return (0);
8487 	}
8488 
8489 	/*
8490 	 * We can't lookup the dip (corresponding to path) via
8491 	 * e_ddi_hold_devi_by_path() because the dip may be offline
8492 	 * and may not attach. Use ddi_walk_devs() instead;
8493 	 */
8494 	find_dip.fd_buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8495 	find_dip.fd_path = path;
8496 	find_dip.fd_dip = NULL;
8497 
8498 	pdip = ddi_root_node();
8499 
8500 	ndi_devi_enter(pdip, &circ);
8501 	ddi_walk_devs(ddi_get_child(pdip), find_dip_fcn, &find_dip);
8502 	ndi_devi_exit(pdip, circ);
8503 
8504 	kmem_free(find_dip.fd_buf, MAXPATHLEN);
8505 
8506 	if (find_dip.fd_dip == NULL) {
8507 		cmn_err(CE_WARN, "Device not found in device tree. Skipping "
8508 		    "device unretire: %s", path);
8509 		return (0);
8510 	}
8511 
8512 	dip = find_dip.fd_dip;
8513 
8514 	pdip = ddi_get_parent(dip);
8515 
8516 	ndi_hold_devi(pdip);
8517 
8518 	ndi_devi_enter(pdip, &circ);
8519 
8520 	path2 = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8521 
8522 	(void) unmark_and_unfence(dip, path2);
8523 	if (!is_leaf_node(dip)) {
8524 		ndi_devi_enter(dip, &circ2);
8525 		ddi_walk_devs(ddi_get_child(dip), unmark_and_unfence, path2);
8526 		ndi_devi_exit(dip, circ2);
8527 	}
8528 
8529 	kmem_free(path2, MAXPATHLEN);
8530 
8531 	/* release hold from find_dip_fcn() */
8532 	ndi_rele_devi(dip);
8533 
8534 	ndi_devi_exit(pdip, circ);
8535 
8536 	ndi_rele_devi(pdip);
8537 
8538 	return (0);
8539 }
8540 
8541 /*
8542  * Called before attach on a dip that has been retired.
8543  */
8544 static int
8545 mark_and_fence(dev_info_t *dip, void *arg)
8546 {
8547 	char	*fencepath = (char *)arg;
8548 
8549 	/*
8550 	 * We have already decided to retire this device. The various
8551 	 * constraint checking should not be set.
8552 	 * NOTE that the retire flag may already be set due to
8553 	 * fenced -> detach -> fenced transitions.
8554 	 */
8555 	mutex_enter(&DEVI(dip)->devi_lock);
8556 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
8557 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
8558 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRING));
8559 	DEVI(dip)->devi_flags |= DEVI_RETIRED;
8560 	mutex_exit(&DEVI(dip)->devi_lock);
8561 	RIO_VERBOSE((CE_NOTE, "marked as RETIRED dip=%p", (void *)dip));
8562 
8563 	if (fencepath) {
8564 		(void) spec_fence_snode(dip, NULL);
8565 		RIO_DEBUG((CE_NOTE, "Fenced: %s",
8566 		    ddi_pathname(dip, fencepath)));
8567 	}
8568 
8569 	return (DDI_WALK_CONTINUE);
8570 }
8571 
8572 /*
8573  * Checks the retire database and:
8574  *
8575  * - if device is present in the retire database, marks the device retired
8576  *   and fences it off.
8577  * - if device is not in retire database, allows the device to attach normally
8578  *
8579  * To be called only by framework attach code on first attach attempt.
8580  *
8581  */
8582 static void
8583 i_ddi_check_retire(dev_info_t *dip)
8584 {
8585 	char		*path;
8586 	dev_info_t	*pdip;
8587 	int		circ;
8588 	int		phci_only;
8589 	int		constraint;
8590 
8591 	pdip = ddi_get_parent(dip);
8592 
8593 	/*
8594 	 * Root dip is treated special and doesn't take this code path.
8595 	 * Also root can never be retired.
8596 	 */
8597 	ASSERT(pdip);
8598 	ASSERT(DEVI_BUSY_OWNED(pdip));
8599 	ASSERT(i_ddi_node_state(dip) < DS_ATTACHED);
8600 
8601 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8602 
8603 	(void) ddi_pathname(dip, path);
8604 
8605 	RIO_VERBOSE((CE_NOTE, "Checking if dip should attach: dip=%p, path=%s",
8606 	    (void *)dip, path));
8607 
8608 	/*
8609 	 * Check if this device is in the "retired" store i.e.	should
8610 	 * be retired. If not, we have nothing to do.
8611 	 */
8612 	if (e_ddi_device_retired(path) == 0) {
8613 		RIO_VERBOSE((CE_NOTE, "device is NOT retired: path=%s", path));
8614 		kmem_free(path, MAXPATHLEN);
8615 		return;
8616 	}
8617 
8618 	RIO_DEBUG((CE_NOTE, "attach: device is retired: path=%s", path));
8619 
8620 	/*
8621 	 * Mark dips and fence off snodes (if any)
8622 	 */
8623 	RIO_DEBUG((CE_NOTE, "attach: Mark and fence subtree: path=%s", path));
8624 	(void) mark_and_fence(dip, path);
8625 	if (!is_leaf_node(dip)) {
8626 		ndi_devi_enter(dip, &circ);
8627 		ddi_walk_devs(ddi_get_child(dip), mark_and_fence, path);
8628 		ndi_devi_exit(dip, circ);
8629 	}
8630 
8631 	kmem_free(path, MAXPATHLEN);
8632 
8633 	/*
8634 	 * We don't want to check the client. We just want to
8635 	 * offline the PHCI
8636 	 */
8637 	phci_only = 1;
8638 	constraint = 1;
8639 	if (MDI_PHCI(dip))
8640 		mdi_phci_retire_finalize(dip, phci_only, &constraint);
8641 }
8642 
8643 
8644 #define	VAL_ALIAS(array, x)	(strlen(array[x].pair_alias))
8645 #define	VAL_CURR(array, x)	(strlen(array[x].pair_curr))
8646 #define	SWAP(array, x, y)			\
8647 {						\
8648 	alias_pair_t tmpair = array[x];		\
8649 	array[x] = array[y];			\
8650 	array[y] = tmpair;			\
8651 }
8652 
8653 static int
8654 partition_curr(alias_pair_t *array, int start, int end)
8655 {
8656 	int	i = start - 1;
8657 	int	j = end + 1;
8658 	int	pivot = start;
8659 
8660 	for (;;) {
8661 		do {
8662 			j--;
8663 		} while (VAL_CURR(array, j) > VAL_CURR(array, pivot));
8664 
8665 		do {
8666 			i++;
8667 		} while (VAL_CURR(array, i) < VAL_CURR(array, pivot));
8668 
8669 		if (i < j)
8670 			SWAP(array, i, j)
8671 		else
8672 			return (j);
8673 	}
8674 }
8675 
8676 static int
8677 partition_aliases(alias_pair_t *array, int start, int end)
8678 {
8679 	int	i = start - 1;
8680 	int	j = end + 1;
8681 	int	pivot = start;
8682 
8683 	for (;;) {
8684 		do {
8685 			j--;
8686 		} while (VAL_ALIAS(array, j) > VAL_ALIAS(array, pivot));
8687 
8688 		do {
8689 			i++;
8690 		} while (VAL_ALIAS(array, i) < VAL_ALIAS(array, pivot));
8691 
8692 		if (i < j)
8693 			SWAP(array, i, j)
8694 		else
8695 			return (j);
8696 	}
8697 }
8698 static void
8699 sort_alias_pairs(alias_pair_t *array, int start, int end)
8700 {
8701 	int mid;
8702 
8703 	if (start < end) {
8704 		mid = partition_aliases(array, start, end);
8705 		sort_alias_pairs(array, start, mid);
8706 		sort_alias_pairs(array, mid + 1, end);
8707 	}
8708 }
8709 
8710 static void
8711 sort_curr_pairs(alias_pair_t *array, int start, int end)
8712 {
8713 	int mid;
8714 
8715 	if (start < end) {
8716 		mid = partition_curr(array, start, end);
8717 		sort_curr_pairs(array, start, mid);
8718 		sort_curr_pairs(array, mid + 1, end);
8719 	}
8720 }
8721 
8722 static void
8723 create_sorted_pairs(plat_alias_t *pali, int npali)
8724 {
8725 	int		i;
8726 	int		j;
8727 	int		k;
8728 	int		count;
8729 
8730 	count = 0;
8731 	for (i = 0; i < npali; i++) {
8732 		count += pali[i].pali_naliases;
8733 	}
8734 
8735 	ddi_aliases.dali_alias_pairs = kmem_zalloc(
8736 	    (sizeof (alias_pair_t)) * count, KM_NOSLEEP);
8737 	if (ddi_aliases.dali_alias_pairs == NULL) {
8738 		cmn_err(CE_PANIC, "alias path-pair alloc failed");
8739 		/*NOTREACHED*/
8740 	}
8741 
8742 	ddi_aliases.dali_curr_pairs = kmem_zalloc(
8743 	    (sizeof (alias_pair_t)) * count, KM_NOSLEEP);
8744 	if (ddi_aliases.dali_curr_pairs == NULL) {
8745 		cmn_err(CE_PANIC, "curr path-pair alloc failed");
8746 		/*NOTREACHED*/
8747 	}
8748 
8749 	for (i = 0, k = 0; i < npali; i++) {
8750 		for (j = 0; j < pali[i].pali_naliases; j++, k++) {
8751 			ddi_aliases.dali_alias_pairs[k].pair_curr =
8752 			    ddi_aliases.dali_curr_pairs[k].pair_curr =
8753 			    pali[i].pali_current;
8754 			ddi_aliases.dali_alias_pairs[k].pair_alias =
8755 			    ddi_aliases.dali_curr_pairs[k].pair_alias =
8756 			    pali[i].pali_aliases[j];
8757 		}
8758 	}
8759 
8760 	ASSERT(k == count);
8761 
8762 	ddi_aliases.dali_num_pairs = count;
8763 
8764 	/* Now sort the array based on length of pair_alias */
8765 	sort_alias_pairs(ddi_aliases.dali_alias_pairs, 0, count - 1);
8766 	sort_curr_pairs(ddi_aliases.dali_curr_pairs, 0, count - 1);
8767 }
8768 
8769 void
8770 ddi_register_aliases(plat_alias_t *pali, uint64_t npali)
8771 {
8772 
8773 	ASSERT((pali == NULL) ^ (npali != 0));
8774 
8775 	if (npali == 0) {
8776 		ddi_err(DER_PANIC, NULL, "npali == 0");
8777 		/*NOTREACHED*/
8778 	}
8779 
8780 	if (ddi_aliases_present == B_TRUE) {
8781 		ddi_err(DER_PANIC, NULL, "multiple init");
8782 		/*NOTREACHED*/
8783 	}
8784 
8785 	ddi_aliases.dali_alias_TLB = mod_hash_create_strhash(
8786 	    "ddi-alias-tlb", DDI_ALIAS_HASH_SIZE, mod_hash_null_valdtor);
8787 	if (ddi_aliases.dali_alias_TLB == NULL) {
8788 		ddi_err(DER_PANIC, NULL, "alias TLB hash alloc failed");
8789 		/*NOTREACHED*/
8790 	}
8791 
8792 	ddi_aliases.dali_curr_TLB = mod_hash_create_strhash(
8793 	    "ddi-curr-tlb", DDI_ALIAS_HASH_SIZE, mod_hash_null_valdtor);
8794 	if (ddi_aliases.dali_curr_TLB == NULL) {
8795 		ddi_err(DER_PANIC, NULL, "curr TLB hash alloc failed");
8796 		/*NOTREACHED*/
8797 	}
8798 
8799 	create_sorted_pairs(pali, npali);
8800 
8801 	tsd_create(&tsd_ddi_redirect, NULL);
8802 
8803 	ddi_aliases_present = B_TRUE;
8804 }
8805 
8806 static dev_info_t *
8807 path_to_dip(char *path)
8808 {
8809 	dev_info_t	*currdip;
8810 	int		error;
8811 	char		*pdup;
8812 
8813 	pdup = ddi_strdup(path, KM_NOSLEEP);
8814 	if (pdup == NULL) {
8815 		cmn_err(CE_PANIC, "path strdup failed: %s", path);
8816 		/*NOTREACHED*/
8817 	}
8818 
8819 	error = resolve_pathname(pdup, &currdip, NULL, NULL);
8820 
8821 	kmem_free(pdup, strlen(path) + 1);
8822 
8823 	return (error ? NULL : currdip);
8824 }
8825 
8826 dev_info_t *
8827 ddi_alias_to_currdip(char *alias, int i)
8828 {
8829 	alias_pair_t *pair;
8830 	char *curr;
8831 	dev_info_t *currdip = NULL;
8832 	char *aliasdup;
8833 	int rv, len;
8834 
8835 	pair = &(ddi_aliases.dali_alias_pairs[i]);
8836 	len = strlen(pair->pair_alias);
8837 
8838 	curr = NULL;
8839 	aliasdup = ddi_strdup(alias, KM_NOSLEEP);
8840 	if (aliasdup == NULL) {
8841 		cmn_err(CE_PANIC, "aliasdup alloc failed");
8842 		/*NOTREACHED*/
8843 	}
8844 
8845 	if (strncmp(alias, pair->pair_alias, len)  != 0)
8846 		goto out;
8847 
8848 	if (alias[len] != '/' && alias[len] != '\0')
8849 		goto out;
8850 
8851 	curr = kmem_alloc(MAXPATHLEN, KM_NOSLEEP);
8852 	if (curr == NULL) {
8853 		cmn_err(CE_PANIC, "curr alloc failed");
8854 		/*NOTREACHED*/
8855 	}
8856 	(void) strlcpy(curr, pair->pair_curr, MAXPATHLEN);
8857 	if (alias[len] == '/') {
8858 		(void) strlcat(curr, "/", MAXPATHLEN);
8859 		(void) strlcat(curr, &alias[len + 1], MAXPATHLEN);
8860 	}
8861 
8862 	currdip = path_to_dip(curr);
8863 
8864 out:
8865 	if (currdip) {
8866 		rv = mod_hash_insert(ddi_aliases.dali_alias_TLB,
8867 		    (mod_hash_key_t)aliasdup, (mod_hash_val_t)curr);
8868 		if (rv != 0) {
8869 			kmem_free(curr, MAXPATHLEN);
8870 			strfree(aliasdup);
8871 		}
8872 	} else {
8873 		rv = mod_hash_insert(ddi_aliases.dali_alias_TLB,
8874 		    (mod_hash_key_t)aliasdup, (mod_hash_val_t)NULL);
8875 		if (rv != 0) {
8876 			strfree(aliasdup);
8877 		}
8878 		if (curr)
8879 			kmem_free(curr, MAXPATHLEN);
8880 	}
8881 
8882 	return (currdip);
8883 }
8884 
8885 char *
8886 ddi_curr_to_alias(char *curr, int i)
8887 {
8888 	alias_pair_t	*pair;
8889 	char		*alias;
8890 	char		*currdup;
8891 	int		len;
8892 	int		rv;
8893 
8894 	pair = &(ddi_aliases.dali_curr_pairs[i]);
8895 
8896 	len = strlen(pair->pair_curr);
8897 
8898 	alias = NULL;
8899 
8900 	currdup = ddi_strdup(curr, KM_NOSLEEP);
8901 	if (currdup == NULL) {
8902 		cmn_err(CE_PANIC, "currdup alloc failed");
8903 		/*NOTREACHED*/
8904 	}
8905 
8906 	if (strncmp(curr, pair->pair_curr, len) != 0)
8907 		goto out;
8908 
8909 	if (curr[len] != '/' && curr[len] != '\0')
8910 		goto out;
8911 
8912 	alias = kmem_alloc(MAXPATHLEN, KM_NOSLEEP);
8913 	if (alias == NULL) {
8914 		cmn_err(CE_PANIC, "alias alloc failed");
8915 		/*NOTREACHED*/
8916 	}
8917 
8918 	(void) strlcpy(alias, pair->pair_alias, MAXPATHLEN);
8919 	if (curr[len] == '/') {
8920 		(void) strlcat(alias, "/", MAXPATHLEN);
8921 		(void) strlcat(alias, &curr[len + 1], MAXPATHLEN);
8922 	}
8923 
8924 	if (e_ddi_path_to_instance(alias) == NULL) {
8925 		kmem_free(alias, MAXPATHLEN);
8926 		alias = NULL;
8927 	}
8928 
8929 out:
8930 	rv = mod_hash_insert(ddi_aliases.dali_curr_TLB,
8931 	    (mod_hash_key_t)currdup, (mod_hash_val_t)alias);
8932 	if (rv != 0) {
8933 		strfree(currdup);
8934 	}
8935 
8936 	return (alias);
8937 }
8938 
8939 dev_info_t *
8940 ddi_alias_redirect(char *alias)
8941 {
8942 	char		*curr;
8943 	dev_info_t	*currdip;
8944 	int		i;
8945 
8946 	if (ddi_aliases_present == B_FALSE)
8947 		return (NULL);
8948 
8949 	if (tsd_get(tsd_ddi_redirect))
8950 		return (NULL);
8951 
8952 	(void) tsd_set(tsd_ddi_redirect, (void *)1);
8953 
8954 	ASSERT(ddi_aliases.dali_alias_TLB);
8955 	ASSERT(ddi_aliases.dali_alias_pairs);
8956 
8957 	curr = NULL;
8958 	if (mod_hash_find(ddi_aliases.dali_alias_TLB,
8959 	    (mod_hash_key_t)alias, (mod_hash_val_t *)&curr) == 0) {
8960 		currdip = curr ? path_to_dip(curr) : NULL;
8961 		goto out;
8962 	}
8963 
8964 	/* The TLB has no translation, do it the hard way */
8965 	currdip = NULL;
8966 	for (i = ddi_aliases.dali_num_pairs - 1; i >= 0; i--) {
8967 		currdip = ddi_alias_to_currdip(alias, i);
8968 		if (currdip)
8969 			break;
8970 	}
8971 out:
8972 	(void) tsd_set(tsd_ddi_redirect, NULL);
8973 
8974 	return (currdip);
8975 }
8976 
8977 char *
8978 ddi_curr_redirect(char *curr)
8979 {
8980 	char 	*alias;
8981 	int i;
8982 
8983 	if (ddi_aliases_present == B_FALSE)
8984 		return (NULL);
8985 
8986 	if (tsd_get(tsd_ddi_redirect))
8987 		return (NULL);
8988 
8989 	(void) tsd_set(tsd_ddi_redirect, (void *)1);
8990 
8991 	ASSERT(ddi_aliases.dali_curr_TLB);
8992 	ASSERT(ddi_aliases.dali_curr_pairs);
8993 
8994 	alias = NULL;
8995 	if (mod_hash_find(ddi_aliases.dali_curr_TLB,
8996 	    (mod_hash_key_t)curr, (mod_hash_val_t *)&alias) == 0) {
8997 		goto out;
8998 	}
8999 
9000 
9001 	/* The TLB has no translation, do it the slow way */
9002 	alias = NULL;
9003 	for (i = ddi_aliases.dali_num_pairs - 1; i >= 0; i--) {
9004 		alias = ddi_curr_to_alias(curr, i);
9005 		if (alias)
9006 			break;
9007 	}
9008 
9009 out:
9010 	(void) tsd_set(tsd_ddi_redirect, NULL);
9011 
9012 	return (alias);
9013 }
9014 
9015 void
9016 ddi_err(ddi_err_t ade, dev_info_t *rdip, const char *fmt, ...)
9017 {
9018 	va_list ap;
9019 	char strbuf[256];
9020 	char *buf;
9021 	size_t buflen, tlen;
9022 	int ce;
9023 	int de;
9024 	const char *fmtbad = "Invalid arguments to ddi_err()";
9025 
9026 	de = DER_CONT;
9027 	strbuf[1] = '\0';
9028 
9029 	switch (ade) {
9030 	case DER_CONS:
9031 		strbuf[0] = '^';
9032 		break;
9033 	case DER_LOG:
9034 		strbuf[0] = '!';
9035 		break;
9036 	case DER_VERB:
9037 		strbuf[0] = '?';
9038 		break;
9039 	default:
9040 		strbuf[0] = '\0';
9041 		de = ade;
9042 		break;
9043 	}
9044 
9045 	tlen = strlen(strbuf);
9046 	buf = strbuf + tlen;
9047 	buflen = sizeof (strbuf) - tlen;
9048 
9049 	if (rdip && ddi_get_instance(rdip) == -1) {
9050 		(void) snprintf(buf, buflen, "%s: ",
9051 		    ddi_driver_name(rdip));
9052 	} else if (rdip) {
9053 		(void) snprintf(buf, buflen, "%s%d: ",
9054 		    ddi_driver_name(rdip), ddi_get_instance(rdip));
9055 	}
9056 
9057 	tlen = strlen(strbuf);
9058 	buf = strbuf + tlen;
9059 	buflen = sizeof (strbuf) - tlen;
9060 
9061 	va_start(ap, fmt);
9062 	switch (de) {
9063 	case DER_CONT:
9064 		(void) vsnprintf(buf, buflen, fmt, ap);
9065 		if (ade != DER_CONT) {
9066 			(void) strlcat(strbuf, "\n", sizeof (strbuf));
9067 		}
9068 		ce = CE_CONT;
9069 		break;
9070 	case DER_NOTE:
9071 		(void) vsnprintf(buf, buflen, fmt, ap);
9072 		ce = CE_NOTE;
9073 		break;
9074 	case DER_WARN:
9075 		(void) vsnprintf(buf, buflen, fmt, ap);
9076 		ce = CE_WARN;
9077 		break;
9078 	case DER_MODE:
9079 		(void) vsnprintf(buf, buflen, fmt, ap);
9080 		if (ddi_err_panic == B_TRUE) {
9081 			ce = CE_PANIC;
9082 		} else {
9083 			ce = CE_WARN;
9084 		}
9085 		break;
9086 	case DER_DEBUG:
9087 		(void) snprintf(buf, buflen, "DEBUG: ");
9088 		tlen = strlen("DEBUG: ");
9089 		(void) vsnprintf(buf + tlen, buflen - tlen, fmt, ap);
9090 		ce = CE_CONT;
9091 		break;
9092 	case DER_PANIC:
9093 		(void) vsnprintf(buf, buflen, fmt, ap);
9094 		ce = CE_PANIC;
9095 		break;
9096 	case DER_INVALID:
9097 	default:
9098 		(void) snprintf(buf, buflen, fmtbad);
9099 		tlen = strlen(fmtbad);
9100 		(void) vsnprintf(buf + tlen, buflen - tlen, fmt, ap);
9101 		ce = CE_PANIC;
9102 		break;
9103 	}
9104 	va_end(ap);
9105 
9106 	cmn_err(ce, strbuf);
9107 }
9108 
9109 /*ARGSUSED*/
9110 void
9111 ddi_mem_update(uint64_t addr, uint64_t size)
9112 {
9113 #if defined(__x86) && !defined(__xpv)
9114 	extern void immu_physmem_update(uint64_t addr, uint64_t size);
9115 	immu_physmem_update(addr, size);
9116 #else
9117 	/*LINTED*/
9118 	;
9119 #endif
9120 }
9121