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