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