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