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