xref: /onnv-gate/usr/src/uts/common/os/devcfg.c (revision 4145)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51961Scth  * Common Development and Distribution License (the "License").
61961Scth  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*4145Scth  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/note.h>
290Sstevel@tonic-gate #include <sys/t_lock.h>
300Sstevel@tonic-gate #include <sys/cmn_err.h>
310Sstevel@tonic-gate #include <sys/instance.h>
320Sstevel@tonic-gate #include <sys/conf.h>
330Sstevel@tonic-gate #include <sys/stat.h>
340Sstevel@tonic-gate #include <sys/ddi.h>
350Sstevel@tonic-gate #include <sys/hwconf.h>
360Sstevel@tonic-gate #include <sys/sunddi.h>
370Sstevel@tonic-gate #include <sys/sunndi.h>
380Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
390Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
400Sstevel@tonic-gate #include <sys/modctl.h>
410Sstevel@tonic-gate #include <sys/dacf.h>
420Sstevel@tonic-gate #include <sys/promif.h>
430Sstevel@tonic-gate #include <sys/cpuvar.h>
440Sstevel@tonic-gate #include <sys/pathname.h>
450Sstevel@tonic-gate #include <sys/taskq.h>
460Sstevel@tonic-gate #include <sys/sysevent.h>
470Sstevel@tonic-gate #include <sys/sunmdi.h>
480Sstevel@tonic-gate #include <sys/stream.h>
490Sstevel@tonic-gate #include <sys/strsubr.h>
500Sstevel@tonic-gate #include <sys/fs/snode.h>
510Sstevel@tonic-gate #include <sys/fs/dv_node.h>
522621Sllai1 #include <sys/reboot.h>
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #ifdef DEBUG
550Sstevel@tonic-gate int ddidebug = DDI_AUDIT;
560Sstevel@tonic-gate #else
570Sstevel@tonic-gate int ddidebug = 0;
580Sstevel@tonic-gate #endif
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #define	MT_CONFIG_OP	0
610Sstevel@tonic-gate #define	MT_UNCONFIG_OP	1
620Sstevel@tonic-gate 
630Sstevel@tonic-gate /* Multi-threaded configuration */
640Sstevel@tonic-gate struct mt_config_handle {
650Sstevel@tonic-gate 	kmutex_t mtc_lock;
660Sstevel@tonic-gate 	kcondvar_t mtc_cv;
670Sstevel@tonic-gate 	int mtc_thr_count;
680Sstevel@tonic-gate 	dev_info_t *mtc_pdip;	/* parent dip for mt_config_children */
690Sstevel@tonic-gate 	dev_info_t **mtc_fdip;	/* "a" dip where unconfigure failed */
700Sstevel@tonic-gate 	major_t mtc_parmajor;	/* parent major for mt_config_driver */
710Sstevel@tonic-gate 	major_t mtc_major;
720Sstevel@tonic-gate 	int mtc_flags;
730Sstevel@tonic-gate 	int mtc_op;		/* config or unconfig */
740Sstevel@tonic-gate 	int mtc_error;		/* operation error */
750Sstevel@tonic-gate 	struct brevq_node **mtc_brevqp;	/* outstanding branch events queue */
760Sstevel@tonic-gate #ifdef DEBUG
770Sstevel@tonic-gate 	int total_time;
780Sstevel@tonic-gate 	timestruc_t start_time;
790Sstevel@tonic-gate #endif /* DEBUG */
800Sstevel@tonic-gate };
810Sstevel@tonic-gate 
820Sstevel@tonic-gate struct devi_nodeid {
83789Sahrens 	pnode_t nodeid;
840Sstevel@tonic-gate 	dev_info_t *dip;
850Sstevel@tonic-gate 	struct devi_nodeid *next;
860Sstevel@tonic-gate };
870Sstevel@tonic-gate 
880Sstevel@tonic-gate struct devi_nodeid_list {
890Sstevel@tonic-gate 	kmutex_t dno_lock;		/* Protects other fields */
900Sstevel@tonic-gate 	struct devi_nodeid *dno_head;	/* list of devi nodeid elements */
910Sstevel@tonic-gate 	struct devi_nodeid *dno_free;	/* Free list */
920Sstevel@tonic-gate 	uint_t dno_list_length;		/* number of dips in list */
930Sstevel@tonic-gate };
940Sstevel@tonic-gate 
950Sstevel@tonic-gate /* used to keep track of branch remove events to be generated */
960Sstevel@tonic-gate struct brevq_node {
971317Scth 	char *brn_deviname;
981317Scth 	struct brevq_node *brn_sibling;
991317Scth 	struct brevq_node *brn_child;
1000Sstevel@tonic-gate };
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate static struct devi_nodeid_list devi_nodeid_list;
1030Sstevel@tonic-gate static struct devi_nodeid_list *devimap = &devi_nodeid_list;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate /*
1060Sstevel@tonic-gate  * Well known nodes which are attached first at boot time.
1070Sstevel@tonic-gate  */
1080Sstevel@tonic-gate dev_info_t *top_devinfo;		/* root of device tree */
1090Sstevel@tonic-gate dev_info_t *options_dip;
1100Sstevel@tonic-gate dev_info_t *pseudo_dip;
1110Sstevel@tonic-gate dev_info_t *clone_dip;
1120Sstevel@tonic-gate dev_info_t *scsi_vhci_dip;		/* MPXIO dip */
1130Sstevel@tonic-gate major_t clone_major;
1140Sstevel@tonic-gate 
1152621Sllai1 /*
1162621Sllai1  * A non-global zone's /dev is derived from the device tree.
1172621Sllai1  * This generation number serves to indicate when a zone's
1182621Sllai1  * /dev may need to be updated.
1192621Sllai1  */
1202621Sllai1 volatile ulong_t devtree_gen;		/* generation number */
1212621Sllai1 
1220Sstevel@tonic-gate /* block all future dev_info state changes */
1230Sstevel@tonic-gate static hrtime_t volatile devinfo_freeze = 0;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate /* number of dev_info attaches/detaches currently in progress */
1260Sstevel@tonic-gate static ulong_t devinfo_attach_detach = 0;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate extern kmutex_t global_vhci_lock;
1290Sstevel@tonic-gate 
1302621Sllai1 /* bitset of DS_SYSAVAIL & DS_RECONFIG - no races, no lock */
1312621Sllai1 static int devname_state = 0;
1322621Sllai1 
1330Sstevel@tonic-gate /*
1340Sstevel@tonic-gate  * The devinfo snapshot cache and related variables.
1350Sstevel@tonic-gate  * The only field in the di_cache structure that needs initialization
1360Sstevel@tonic-gate  * is the mutex (cache_lock). However, since this is an adaptive mutex
1370Sstevel@tonic-gate  * (MUTEX_DEFAULT) - it is automatically initialized by being allocated
1380Sstevel@tonic-gate  * in zeroed memory (static storage class). Therefore no explicit
1390Sstevel@tonic-gate  * initialization of the di_cache structure is needed.
1400Sstevel@tonic-gate  */
1410Sstevel@tonic-gate struct di_cache	di_cache = {1};
1420Sstevel@tonic-gate int		di_cache_debug = 0;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate /* For ddvis, which needs pseudo children under PCI */
1450Sstevel@tonic-gate int pci_allow_pseudo_children = 0;
1460Sstevel@tonic-gate 
147*4145Scth /* Allow path-oriented alias driver binding on driver.conf enumerated nodes */
148*4145Scth int driver_conf_allow_path_alias = 1;
149*4145Scth 
1500Sstevel@tonic-gate /*
1510Sstevel@tonic-gate  * The following switch is for service people, in case a
1520Sstevel@tonic-gate  * 3rd party driver depends on identify(9e) being called.
1530Sstevel@tonic-gate  */
1540Sstevel@tonic-gate int identify_9e = 0;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate int mtc_off;					/* turn off mt config */
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate static kmem_cache_t *ddi_node_cache;		/* devinfo node cache */
1590Sstevel@tonic-gate static devinfo_log_header_t *devinfo_audit_log;	/* devinfo log */
1600Sstevel@tonic-gate static int devinfo_log_size;			/* size in pages */
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate static int lookup_compatible(dev_info_t *, uint_t);
1630Sstevel@tonic-gate static char *encode_composite_string(char **, uint_t, size_t *, uint_t);
1640Sstevel@tonic-gate static void link_to_driver_list(dev_info_t *);
1650Sstevel@tonic-gate static void unlink_from_driver_list(dev_info_t *);
1660Sstevel@tonic-gate static void add_to_dn_list(struct devnames *, dev_info_t *);
1670Sstevel@tonic-gate static void remove_from_dn_list(struct devnames *, dev_info_t *);
1680Sstevel@tonic-gate static dev_info_t *find_child_by_callback(dev_info_t *, char *, char *,
1690Sstevel@tonic-gate     int (*)(dev_info_t *, char *, int));
1700Sstevel@tonic-gate static dev_info_t *find_duplicate_child();
1710Sstevel@tonic-gate static void add_global_props(dev_info_t *);
1720Sstevel@tonic-gate static void remove_global_props(dev_info_t *);
1730Sstevel@tonic-gate static int uninit_node(dev_info_t *);
1740Sstevel@tonic-gate static void da_log_init(void);
1750Sstevel@tonic-gate static void da_log_enter(dev_info_t *);
1760Sstevel@tonic-gate static int walk_devs(dev_info_t *, int (*f)(dev_info_t *, void *), void *, int);
1770Sstevel@tonic-gate static int reset_nexus_flags(dev_info_t *, void *);
1780Sstevel@tonic-gate static void ddi_optimize_dtree(dev_info_t *);
1790Sstevel@tonic-gate static int is_leaf_node(dev_info_t *);
1800Sstevel@tonic-gate static struct mt_config_handle *mt_config_init(dev_info_t *, dev_info_t **,
1810Sstevel@tonic-gate     int, major_t, int, struct brevq_node **);
1820Sstevel@tonic-gate static void mt_config_children(struct mt_config_handle *);
1830Sstevel@tonic-gate static void mt_config_driver(struct mt_config_handle *);
1840Sstevel@tonic-gate static int mt_config_fini(struct mt_config_handle *);
1850Sstevel@tonic-gate static int devi_unconfig_common(dev_info_t *, dev_info_t **, int, major_t,
1860Sstevel@tonic-gate     struct brevq_node **);
1870Sstevel@tonic-gate static int
1880Sstevel@tonic-gate ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
1890Sstevel@tonic-gate     dev_info_t **childp, int flags);
1901093Shiremath static void i_link_vhci_node(dev_info_t *);
1912155Scth static void ndi_devi_exit_and_wait(dev_info_t *dip,
1922155Scth     int circular, clock_t end_time);
193*4145Scth static int ndi_devi_unbind_driver(dev_info_t *dip);
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate /*
1960Sstevel@tonic-gate  * dev_info cache and node management
1970Sstevel@tonic-gate  */
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate /* initialize dev_info node cache */
2000Sstevel@tonic-gate void
2010Sstevel@tonic-gate i_ddi_node_cache_init()
2020Sstevel@tonic-gate {
2030Sstevel@tonic-gate 	ASSERT(ddi_node_cache == NULL);
2040Sstevel@tonic-gate 	ddi_node_cache = kmem_cache_create("dev_info_node_cache",
2050Sstevel@tonic-gate 	    sizeof (struct dev_info), 0, NULL, NULL, NULL, NULL, NULL, 0);
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	if (ddidebug & DDI_AUDIT)
2080Sstevel@tonic-gate 		da_log_init();
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate /*
2120Sstevel@tonic-gate  * Allocating a dev_info node, callable from interrupt context with KM_NOSLEEP
2130Sstevel@tonic-gate  * The allocated node has a reference count of 0.
2140Sstevel@tonic-gate  */
2150Sstevel@tonic-gate dev_info_t *
216789Sahrens i_ddi_alloc_node(dev_info_t *pdip, char *node_name, pnode_t nodeid,
2170Sstevel@tonic-gate     int instance, ddi_prop_t *sys_prop, int flag)
2180Sstevel@tonic-gate {
2190Sstevel@tonic-gate 	struct dev_info *devi;
2200Sstevel@tonic-gate 	struct devi_nodeid *elem;
2210Sstevel@tonic-gate 	static char failed[] = "i_ddi_alloc_node: out of memory";
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	ASSERT(node_name != NULL);
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	if ((devi = kmem_cache_alloc(ddi_node_cache, flag)) == NULL) {
2260Sstevel@tonic-gate 		cmn_err(CE_NOTE, failed);
2270Sstevel@tonic-gate 		return (NULL);
2280Sstevel@tonic-gate 	}
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	bzero(devi, sizeof (struct dev_info));
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	if (devinfo_audit_log) {
2330Sstevel@tonic-gate 		devi->devi_audit = kmem_zalloc(sizeof (devinfo_audit_t), flag);
2340Sstevel@tonic-gate 		if (devi->devi_audit == NULL)
2350Sstevel@tonic-gate 			goto fail;
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	if ((devi->devi_node_name = i_ddi_strdup(node_name, flag)) == NULL)
2390Sstevel@tonic-gate 		goto fail;
240*4145Scth 
2410Sstevel@tonic-gate 	/* default binding name is node name */
2420Sstevel@tonic-gate 	devi->devi_binding_name = devi->devi_node_name;
243*4145Scth 	devi->devi_major = (major_t)-1;		/* unbound by default */
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	/*
2460Sstevel@tonic-gate 	 * Make a copy of system property
2470Sstevel@tonic-gate 	 */
2480Sstevel@tonic-gate 	if (sys_prop &&
2490Sstevel@tonic-gate 	    (devi->devi_sys_prop_ptr = i_ddi_prop_list_dup(sys_prop, flag))
2500Sstevel@tonic-gate 	    == NULL)
2510Sstevel@tonic-gate 		goto fail;
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	/*
2540Sstevel@tonic-gate 	 * Assign devi_nodeid, devi_node_class, devi_node_attributes
2550Sstevel@tonic-gate 	 * according to the following algorithm:
2560Sstevel@tonic-gate 	 *
2570Sstevel@tonic-gate 	 * nodeid arg			node class		node attributes
2580Sstevel@tonic-gate 	 *
2590Sstevel@tonic-gate 	 * DEVI_PSEUDO_NODEID		DDI_NC_PSEUDO		A
2600Sstevel@tonic-gate 	 * DEVI_SID_NODEID		DDI_NC_PSEUDO		A,P
2610Sstevel@tonic-gate 	 * other			DDI_NC_PROM		P
2620Sstevel@tonic-gate 	 *
2630Sstevel@tonic-gate 	 * Where A = DDI_AUTO_ASSIGNED_NODEID (auto-assign a nodeid)
2640Sstevel@tonic-gate 	 * and	 P = DDI_PERSISTENT
2650Sstevel@tonic-gate 	 *
2660Sstevel@tonic-gate 	 * auto-assigned nodeids are also auto-freed.
2670Sstevel@tonic-gate 	 */
2680Sstevel@tonic-gate 	switch (nodeid) {
2690Sstevel@tonic-gate 	case DEVI_SID_NODEID:
2700Sstevel@tonic-gate 		devi->devi_node_attributes = DDI_PERSISTENT;
2710Sstevel@tonic-gate 		if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL)
2720Sstevel@tonic-gate 			goto fail;
2730Sstevel@tonic-gate 		/*FALLTHROUGH*/
2740Sstevel@tonic-gate 	case DEVI_PSEUDO_NODEID:
2750Sstevel@tonic-gate 		devi->devi_node_attributes |= DDI_AUTO_ASSIGNED_NODEID;
2760Sstevel@tonic-gate 		devi->devi_node_class = DDI_NC_PSEUDO;
2770Sstevel@tonic-gate 		if (impl_ddi_alloc_nodeid(&devi->devi_nodeid)) {
2780Sstevel@tonic-gate 			panic("i_ddi_alloc_node: out of nodeids");
2790Sstevel@tonic-gate 			/*NOTREACHED*/
2800Sstevel@tonic-gate 		}
2810Sstevel@tonic-gate 		break;
2820Sstevel@tonic-gate 	default:
2830Sstevel@tonic-gate 		if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL)
2840Sstevel@tonic-gate 			goto fail;
2850Sstevel@tonic-gate 		/*
2860Sstevel@tonic-gate 		 * the nodetype is 'prom', try to 'take' the nodeid now.
2870Sstevel@tonic-gate 		 * This requires memory allocation, so check for failure.
2880Sstevel@tonic-gate 		 */
2890Sstevel@tonic-gate 		if (impl_ddi_take_nodeid(nodeid, flag) != 0) {
2900Sstevel@tonic-gate 			kmem_free(elem, sizeof (*elem));
2910Sstevel@tonic-gate 			goto fail;
2920Sstevel@tonic-gate 		}
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 		devi->devi_nodeid = nodeid;
2950Sstevel@tonic-gate 		devi->devi_node_class = DDI_NC_PROM;
2960Sstevel@tonic-gate 		devi->devi_node_attributes = DDI_PERSISTENT;
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	}
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node((dev_info_t *)devi)) {
3010Sstevel@tonic-gate 		mutex_enter(&devimap->dno_lock);
3020Sstevel@tonic-gate 		elem->next = devimap->dno_free;
3030Sstevel@tonic-gate 		devimap->dno_free = elem;
3040Sstevel@tonic-gate 		mutex_exit(&devimap->dno_lock);
3050Sstevel@tonic-gate 	}
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	/*
3080Sstevel@tonic-gate 	 * Instance is normally initialized to -1. In a few special
3090Sstevel@tonic-gate 	 * cases, the caller may specify an instance (e.g. CPU nodes).
3100Sstevel@tonic-gate 	 */
3110Sstevel@tonic-gate 	devi->devi_instance = instance;
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	/*
3140Sstevel@tonic-gate 	 * set parent and bus_ctl parent
3150Sstevel@tonic-gate 	 */
3160Sstevel@tonic-gate 	devi->devi_parent = DEVI(pdip);
3170Sstevel@tonic-gate 	devi->devi_bus_ctl = DEVI(pdip);
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
3200Sstevel@tonic-gate 	    "i_ddi_alloc_node: name=%s id=%d\n", node_name, devi->devi_nodeid));
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	cv_init(&(devi->devi_cv), NULL, CV_DEFAULT, NULL);
3230Sstevel@tonic-gate 	mutex_init(&(devi->devi_lock), NULL, MUTEX_DEFAULT, NULL);
3240Sstevel@tonic-gate 	mutex_init(&(devi->devi_pm_lock), NULL, MUTEX_DEFAULT, NULL);
3250Sstevel@tonic-gate 	mutex_init(&(devi->devi_pm_busy_lock), NULL, MUTEX_DEFAULT, NULL);
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	i_ddi_set_node_state((dev_info_t *)devi, DS_PROTO);
3280Sstevel@tonic-gate 	da_log_enter((dev_info_t *)devi);
3290Sstevel@tonic-gate 	return ((dev_info_t *)devi);
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate fail:
3320Sstevel@tonic-gate 	if (devi->devi_sys_prop_ptr)
3330Sstevel@tonic-gate 		i_ddi_prop_list_delete(devi->devi_sys_prop_ptr);
3340Sstevel@tonic-gate 	if (devi->devi_node_name)
3350Sstevel@tonic-gate 		kmem_free(devi->devi_node_name, strlen(node_name) + 1);
3360Sstevel@tonic-gate 	if (devi->devi_audit)
3370Sstevel@tonic-gate 		kmem_free(devi->devi_audit, sizeof (devinfo_audit_t));
3380Sstevel@tonic-gate 	kmem_cache_free(ddi_node_cache, devi);
3390Sstevel@tonic-gate 	cmn_err(CE_NOTE, failed);
3400Sstevel@tonic-gate 	return (NULL);
3410Sstevel@tonic-gate }
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate /*
3440Sstevel@tonic-gate  * free a dev_info structure.
3450Sstevel@tonic-gate  * NB. Not callable from interrupt since impl_ddi_free_nodeid may block.
3460Sstevel@tonic-gate  */
3470Sstevel@tonic-gate void
3480Sstevel@tonic-gate i_ddi_free_node(dev_info_t *dip)
3490Sstevel@tonic-gate {
3500Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
3510Sstevel@tonic-gate 	struct devi_nodeid *elem;
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	ASSERT(devi->devi_ref == 0);
3540Sstevel@tonic-gate 	ASSERT(devi->devi_addr == NULL);
3550Sstevel@tonic-gate 	ASSERT(devi->devi_node_state == DS_PROTO);
3560Sstevel@tonic-gate 	ASSERT(devi->devi_child == NULL);
3570Sstevel@tonic-gate 
358439Scth 	/* free devi_addr_buf allocated by ddi_set_name_addr() */
359439Scth 	if (devi->devi_addr_buf)
360439Scth 		kmem_free(devi->devi_addr_buf, 2 * MAXNAMELEN);
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	if (i_ndi_dev_is_auto_assigned_node(dip))
3630Sstevel@tonic-gate 		impl_ddi_free_nodeid(DEVI(dip)->devi_nodeid);
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
3660Sstevel@tonic-gate 		mutex_enter(&devimap->dno_lock);
3670Sstevel@tonic-gate 		ASSERT(devimap->dno_free);
3680Sstevel@tonic-gate 		elem = devimap->dno_free;
3690Sstevel@tonic-gate 		devimap->dno_free = elem->next;
3700Sstevel@tonic-gate 		mutex_exit(&devimap->dno_lock);
3710Sstevel@tonic-gate 		kmem_free(elem, sizeof (*elem));
3720Sstevel@tonic-gate 	}
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	if (DEVI(dip)->devi_compat_names)
3750Sstevel@tonic-gate 		kmem_free(DEVI(dip)->devi_compat_names,
3760Sstevel@tonic-gate 		    DEVI(dip)->devi_compat_length);
377*4145Scth 	if (DEVI(dip)->devi_rebinding_name)
378*4145Scth 		kmem_free(DEVI(dip)->devi_rebinding_name,
379*4145Scth 		    strlen(DEVI(dip)->devi_rebinding_name) + 1);
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	ddi_prop_remove_all(dip);	/* remove driver properties */
3820Sstevel@tonic-gate 	if (devi->devi_sys_prop_ptr)
3830Sstevel@tonic-gate 		i_ddi_prop_list_delete(devi->devi_sys_prop_ptr);
3840Sstevel@tonic-gate 	if (devi->devi_hw_prop_ptr)
3850Sstevel@tonic-gate 		i_ddi_prop_list_delete(devi->devi_hw_prop_ptr);
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_INVAL);
3880Sstevel@tonic-gate 	da_log_enter(dip);
3890Sstevel@tonic-gate 	if (devi->devi_audit) {
3900Sstevel@tonic-gate 		kmem_free(devi->devi_audit, sizeof (devinfo_audit_t));
3910Sstevel@tonic-gate 	}
3920Sstevel@tonic-gate 	kmem_free(devi->devi_node_name, strlen(devi->devi_node_name) + 1);
3930Sstevel@tonic-gate 	if (devi->devi_device_class)
3940Sstevel@tonic-gate 		kmem_free(devi->devi_device_class,
3950Sstevel@tonic-gate 		    strlen(devi->devi_device_class) + 1);
3960Sstevel@tonic-gate 	cv_destroy(&(devi->devi_cv));
3970Sstevel@tonic-gate 	mutex_destroy(&(devi->devi_lock));
3980Sstevel@tonic-gate 	mutex_destroy(&(devi->devi_pm_lock));
3990Sstevel@tonic-gate 	mutex_destroy(&(devi->devi_pm_busy_lock));
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 	kmem_cache_free(ddi_node_cache, devi);
4020Sstevel@tonic-gate }
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate /*
4060Sstevel@tonic-gate  * Node state transitions
4070Sstevel@tonic-gate  */
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate /*
4100Sstevel@tonic-gate  * Change the node name
4110Sstevel@tonic-gate  */
4120Sstevel@tonic-gate int
4130Sstevel@tonic-gate ndi_devi_set_nodename(dev_info_t *dip, char *name, int flags)
4140Sstevel@tonic-gate {
4150Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags))
4160Sstevel@tonic-gate 	char *nname, *oname;
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	ASSERT(dip && name);
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	oname = DEVI(dip)->devi_node_name;
4210Sstevel@tonic-gate 	if (strcmp(oname, name) == 0)
4220Sstevel@tonic-gate 		return (DDI_SUCCESS);
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	/*
4250Sstevel@tonic-gate 	 * pcicfg_fix_ethernet requires a name change after node
4260Sstevel@tonic-gate 	 * is linked into the tree. When pcicfg is fixed, we
4270Sstevel@tonic-gate 	 * should only allow name change in DS_PROTO state.
4280Sstevel@tonic-gate 	 */
4290Sstevel@tonic-gate 	if (i_ddi_node_state(dip) >= DS_BOUND) {
4300Sstevel@tonic-gate 		/*
4310Sstevel@tonic-gate 		 * Don't allow name change once node is bound
4320Sstevel@tonic-gate 		 */
4330Sstevel@tonic-gate 		cmn_err(CE_NOTE,
4340Sstevel@tonic-gate 		    "ndi_devi_set_nodename: node already bound dip = %p,"
4350Sstevel@tonic-gate 		    " %s -> %s", (void *)dip, ddi_node_name(dip), name);
4360Sstevel@tonic-gate 		return (NDI_FAILURE);
4370Sstevel@tonic-gate 	}
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	nname = i_ddi_strdup(name, KM_SLEEP);
4400Sstevel@tonic-gate 	DEVI(dip)->devi_node_name = nname;
4410Sstevel@tonic-gate 	i_ddi_set_binding_name(dip, nname);
4420Sstevel@tonic-gate 	kmem_free(oname, strlen(oname) + 1);
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	da_log_enter(dip);
4450Sstevel@tonic-gate 	return (NDI_SUCCESS);
4460Sstevel@tonic-gate }
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate void
4490Sstevel@tonic-gate i_ddi_add_devimap(dev_info_t *dip)
4500Sstevel@tonic-gate {
4510Sstevel@tonic-gate 	struct devi_nodeid *elem;
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	ASSERT(dip);
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	if (!ndi_dev_is_persistent_node(dip))
4560Sstevel@tonic-gate 		return;
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	ASSERT(ddi_get_parent(dip) == NULL || (DEVI_VHCI_NODE(dip)) ||
4590Sstevel@tonic-gate 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 	mutex_enter(&devimap->dno_lock);
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	ASSERT(devimap->dno_free);
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	elem = devimap->dno_free;
4660Sstevel@tonic-gate 	devimap->dno_free = elem->next;
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	elem->nodeid = ddi_get_nodeid(dip);
4690Sstevel@tonic-gate 	elem->dip = dip;
4700Sstevel@tonic-gate 	elem->next = devimap->dno_head;
4710Sstevel@tonic-gate 	devimap->dno_head = elem;
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	devimap->dno_list_length++;
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	mutex_exit(&devimap->dno_lock);
4760Sstevel@tonic-gate }
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate static int
4790Sstevel@tonic-gate i_ddi_remove_devimap(dev_info_t *dip)
4800Sstevel@tonic-gate {
4810Sstevel@tonic-gate 	struct devi_nodeid *prev, *elem;
4820Sstevel@tonic-gate 	static const char *fcn = "i_ddi_remove_devimap";
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	ASSERT(dip);
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	if (!ndi_dev_is_persistent_node(dip))
4870Sstevel@tonic-gate 		return (DDI_SUCCESS);
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	mutex_enter(&devimap->dno_lock);
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	/*
4920Sstevel@tonic-gate 	 * The following check is done with dno_lock held
4930Sstevel@tonic-gate 	 * to prevent race between dip removal and
4940Sstevel@tonic-gate 	 * e_ddi_prom_node_to_dip()
4950Sstevel@tonic-gate 	 */
4960Sstevel@tonic-gate 	if (e_ddi_devi_holdcnt(dip)) {
4970Sstevel@tonic-gate 		mutex_exit(&devimap->dno_lock);
4980Sstevel@tonic-gate 		return (DDI_FAILURE);
4990Sstevel@tonic-gate 	}
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	ASSERT(devimap->dno_head);
5020Sstevel@tonic-gate 	ASSERT(devimap->dno_list_length > 0);
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 	prev = NULL;
5050Sstevel@tonic-gate 	for (elem = devimap->dno_head; elem; elem = elem->next) {
5060Sstevel@tonic-gate 		if (elem->dip == dip) {
5070Sstevel@tonic-gate 			ASSERT(elem->nodeid == ddi_get_nodeid(dip));
5080Sstevel@tonic-gate 			break;
5090Sstevel@tonic-gate 		}
5100Sstevel@tonic-gate 		prev = elem;
5110Sstevel@tonic-gate 	}
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	if (elem && prev)
5140Sstevel@tonic-gate 		prev->next = elem->next;
5150Sstevel@tonic-gate 	else if (elem)
5160Sstevel@tonic-gate 		devimap->dno_head = elem->next;
5170Sstevel@tonic-gate 	else
5180Sstevel@tonic-gate 		panic("%s: devinfo node(%p) not found",
5190Sstevel@tonic-gate 		    fcn, (void *)dip);
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	devimap->dno_list_length--;
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	elem->nodeid = 0;
5240Sstevel@tonic-gate 	elem->dip = NULL;
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	elem->next = devimap->dno_free;
5270Sstevel@tonic-gate 	devimap->dno_free = elem;
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	mutex_exit(&devimap->dno_lock);
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	return (DDI_SUCCESS);
5320Sstevel@tonic-gate }
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate /*
5350Sstevel@tonic-gate  * Link this node into the devinfo tree and add to orphan list
5360Sstevel@tonic-gate  * Not callable from interrupt context
5370Sstevel@tonic-gate  */
5380Sstevel@tonic-gate static void
5390Sstevel@tonic-gate link_node(dev_info_t *dip)
5400Sstevel@tonic-gate {
5410Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
5420Sstevel@tonic-gate 	struct dev_info *parent = devi->devi_parent;
5430Sstevel@tonic-gate 	dev_info_t **dipp;
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	ASSERT(parent);	/* never called for root node */
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "link_node: parent = %s child = %s\n",
5480Sstevel@tonic-gate 	    parent->devi_node_name, devi->devi_node_name));
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	/*
5510Sstevel@tonic-gate 	 * Hold the global_vhci_lock before linking any direct
5520Sstevel@tonic-gate 	 * children of rootnex driver. This special lock protects
5530Sstevel@tonic-gate 	 * linking and unlinking for rootnext direct children.
5540Sstevel@tonic-gate 	 */
5550Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
5560Sstevel@tonic-gate 		mutex_enter(&global_vhci_lock);
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	/*
5590Sstevel@tonic-gate 	 * attach the node to end of the list unless the node is already there
5600Sstevel@tonic-gate 	 */
5610Sstevel@tonic-gate 	dipp = (dev_info_t **)(&DEVI(parent)->devi_child);
5620Sstevel@tonic-gate 	while (*dipp && (*dipp != dip)) {
5630Sstevel@tonic-gate 		dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling);
5640Sstevel@tonic-gate 	}
5650Sstevel@tonic-gate 	ASSERT(*dipp == NULL);	/* node is not linked */
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	/*
5680Sstevel@tonic-gate 	 * Now that we are in the tree, update the devi-nodeid map.
5690Sstevel@tonic-gate 	 */
5700Sstevel@tonic-gate 	i_ddi_add_devimap(dip);
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	/*
5730Sstevel@tonic-gate 	 * This is a temporary workaround for Bug 4618861.
5740Sstevel@tonic-gate 	 * We keep the scsi_vhci nexus node on the left side of the devinfo
5750Sstevel@tonic-gate 	 * tree (under the root nexus driver), so that virtual nodes under
5760Sstevel@tonic-gate 	 * scsi_vhci will be SUSPENDed first and RESUMEd last.  This ensures
5770Sstevel@tonic-gate 	 * that the pHCI nodes are active during times when their clients
5780Sstevel@tonic-gate 	 * may be depending on them.  This workaround embodies the knowledge
5790Sstevel@tonic-gate 	 * that system PM and CPR both traverse the tree left-to-right during
5800Sstevel@tonic-gate 	 * SUSPEND and right-to-left during RESUME.
5811093Shiremath 	 * Extending the workaround to IB Nexus/VHCI
5821093Shiremath 	 * driver also.
5830Sstevel@tonic-gate 	 */
584*4145Scth 	if (strcmp(devi->devi_binding_name, "scsi_vhci") == 0) {
5850Sstevel@tonic-gate 		/* Add scsi_vhci to beginning of list */
5860Sstevel@tonic-gate 		ASSERT((dev_info_t *)parent == top_devinfo);
5870Sstevel@tonic-gate 		/* scsi_vhci under rootnex */
5880Sstevel@tonic-gate 		devi->devi_sibling = parent->devi_child;
5890Sstevel@tonic-gate 		parent->devi_child = devi;
590*4145Scth 	} else if (strcmp(devi->devi_binding_name, "ib") == 0) {
5911093Shiremath 		i_link_vhci_node(dip);
5920Sstevel@tonic-gate 	} else {
5930Sstevel@tonic-gate 		/* Add to end of list */
5940Sstevel@tonic-gate 		*dipp = dip;
5950Sstevel@tonic-gate 		DEVI(dip)->devi_sibling = NULL;
5960Sstevel@tonic-gate 	}
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	/*
5990Sstevel@tonic-gate 	 * Release the global_vhci_lock before linking any direct
6000Sstevel@tonic-gate 	 * children of rootnex driver.
6010Sstevel@tonic-gate 	 */
6020Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
6030Sstevel@tonic-gate 		mutex_exit(&global_vhci_lock);
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	/* persistent nodes go on orphan list */
6060Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip))
6070Sstevel@tonic-gate 		add_to_dn_list(&orphanlist, dip);
6080Sstevel@tonic-gate }
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate /*
6110Sstevel@tonic-gate  * Unlink this node from the devinfo tree
6120Sstevel@tonic-gate  */
6130Sstevel@tonic-gate static int
6140Sstevel@tonic-gate unlink_node(dev_info_t *dip)
6150Sstevel@tonic-gate {
6160Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
6170Sstevel@tonic-gate 	struct dev_info *parent = devi->devi_parent;
6180Sstevel@tonic-gate 	dev_info_t **dipp;
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	ASSERT(parent != NULL);
6210Sstevel@tonic-gate 	ASSERT(devi->devi_node_state == DS_LINKED);
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "unlink_node: name = %s\n",
6240Sstevel@tonic-gate 	    ddi_node_name(dip)));
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	/* check references */
6270Sstevel@tonic-gate 	if (devi->devi_ref || i_ddi_remove_devimap(dip) != DDI_SUCCESS)
6280Sstevel@tonic-gate 		return (DDI_FAILURE);
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 	/*
6310Sstevel@tonic-gate 	 * Hold the global_vhci_lock before linking any direct
6320Sstevel@tonic-gate 	 * children of rootnex driver.
6330Sstevel@tonic-gate 	 */
6340Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
6350Sstevel@tonic-gate 		mutex_enter(&global_vhci_lock);
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	dipp = (dev_info_t **)(&DEVI(parent)->devi_child);
6380Sstevel@tonic-gate 	while (*dipp && (*dipp != dip)) {
6390Sstevel@tonic-gate 		dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling);
6400Sstevel@tonic-gate 	}
6410Sstevel@tonic-gate 	if (*dipp) {
6420Sstevel@tonic-gate 		*dipp = (dev_info_t *)(devi->devi_sibling);
6430Sstevel@tonic-gate 		devi->devi_sibling = NULL;
6440Sstevel@tonic-gate 	} else {
6450Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE, "unlink_node: %s not linked",
6460Sstevel@tonic-gate 		    devi->devi_node_name));
6470Sstevel@tonic-gate 	}
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	/*
6500Sstevel@tonic-gate 	 * Release the global_vhci_lock before linking any direct
6510Sstevel@tonic-gate 	 * children of rootnex driver.
6520Sstevel@tonic-gate 	 */
6530Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
6540Sstevel@tonic-gate 		mutex_exit(&global_vhci_lock);
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	/* Remove node from orphan list */
6570Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
6580Sstevel@tonic-gate 		remove_from_dn_list(&orphanlist, dip);
6590Sstevel@tonic-gate 	}
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	return (DDI_SUCCESS);
6620Sstevel@tonic-gate }
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate /*
6650Sstevel@tonic-gate  * Bind this devinfo node to a driver. If compat is NON-NULL, try that first.
6660Sstevel@tonic-gate  * Else, use the node-name.
6670Sstevel@tonic-gate  *
6680Sstevel@tonic-gate  * NOTE: IEEE1275 specifies that nodename should be tried before compatible.
6690Sstevel@tonic-gate  *	Solaris implementation binds nodename after compatible.
6700Sstevel@tonic-gate  *
6710Sstevel@tonic-gate  * If we find a binding,
6720Sstevel@tonic-gate  * - set the binding name to the the string,
6730Sstevel@tonic-gate  * - set major number to driver major
6740Sstevel@tonic-gate  *
6750Sstevel@tonic-gate  * If we don't find a binding,
6760Sstevel@tonic-gate  * - return failure
6770Sstevel@tonic-gate  */
6780Sstevel@tonic-gate static int
6790Sstevel@tonic-gate bind_node(dev_info_t *dip)
6800Sstevel@tonic-gate {
6810Sstevel@tonic-gate 	char *p = NULL;
6820Sstevel@tonic-gate 	major_t major = (major_t)(major_t)-1;
6830Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
6840Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 	ASSERT(devi->devi_node_state == DS_LINKED);
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "bind_node: 0x%p(name = %s)\n",
6890Sstevel@tonic-gate 	    (void *)dip, ddi_node_name(dip)));
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
6920Sstevel@tonic-gate 	if (DEVI(dip)->devi_flags & DEVI_NO_BIND) {
6930Sstevel@tonic-gate 		mutex_exit(&DEVI(dip)->devi_lock);
6940Sstevel@tonic-gate 		return (DDI_FAILURE);
6950Sstevel@tonic-gate 	}
6960Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	/* find the driver with most specific binding using compatible */
6990Sstevel@tonic-gate 	major = ddi_compatible_driver_major(dip, &p);
7000Sstevel@tonic-gate 	if (major == (major_t)-1)
7010Sstevel@tonic-gate 		return (DDI_FAILURE);
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	devi->devi_major = major;
7040Sstevel@tonic-gate 	if (p != NULL) {
7050Sstevel@tonic-gate 		i_ddi_set_binding_name(dip, p);
7060Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "bind_node: %s bound to %s\n",
7070Sstevel@tonic-gate 		    devi->devi_node_name, p));
7080Sstevel@tonic-gate 	}
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	/* Link node to per-driver list */
7110Sstevel@tonic-gate 	link_to_driver_list(dip);
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	/*
7140Sstevel@tonic-gate 	 * reset parent flag so that nexus will merge .conf props
7150Sstevel@tonic-gate 	 */
7160Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
7170Sstevel@tonic-gate 		mutex_enter(&DEVI(parent)->devi_lock);
7180Sstevel@tonic-gate 		DEVI(parent)->devi_flags &=
7190Sstevel@tonic-gate 		    ~(DEVI_ATTACHED_CHILDREN|DEVI_MADE_CHILDREN);
7200Sstevel@tonic-gate 		mutex_exit(&DEVI(parent)->devi_lock);
7210Sstevel@tonic-gate 	}
7220Sstevel@tonic-gate 	return (DDI_SUCCESS);
7230Sstevel@tonic-gate }
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate /*
7260Sstevel@tonic-gate  * Unbind this devinfo node
7270Sstevel@tonic-gate  * Called before the node is destroyed or driver is removed from system
7280Sstevel@tonic-gate  */
7290Sstevel@tonic-gate static int
7300Sstevel@tonic-gate unbind_node(dev_info_t *dip)
7310Sstevel@tonic-gate {
7320Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_node_state == DS_BOUND);
7330Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_major != (major_t)-1);
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 	/* check references */
7360Sstevel@tonic-gate 	if (DEVI(dip)->devi_ref)
7370Sstevel@tonic-gate 		return (DDI_FAILURE);
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "unbind_node: 0x%p(name = %s)\n",
7400Sstevel@tonic-gate 	    (void *)dip, ddi_node_name(dip)));
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	unlink_from_driver_list(dip);
743*4145Scth 
7440Sstevel@tonic-gate 	DEVI(dip)->devi_major = (major_t)-1;
745*4145Scth 	DEVI(dip)->devi_binding_name = DEVI(dip)->devi_node_name;
7460Sstevel@tonic-gate 	return (DDI_SUCCESS);
7470Sstevel@tonic-gate }
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate /*
7500Sstevel@tonic-gate  * Initialize a node: calls the parent nexus' bus_ctl ops to do the operation.
7510Sstevel@tonic-gate  * Must hold parent and per-driver list while calling this function.
7520Sstevel@tonic-gate  * A successful init_node() returns with an active ndi_hold_devi() hold on
7530Sstevel@tonic-gate  * the parent.
7540Sstevel@tonic-gate  */
7550Sstevel@tonic-gate static int
7560Sstevel@tonic-gate init_node(dev_info_t *dip)
7570Sstevel@tonic-gate {
7580Sstevel@tonic-gate 	int error;
7590Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
7600Sstevel@tonic-gate 	int (*f)(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *, void *);
7610Sstevel@tonic-gate 	char *path;
762*4145Scth 	major_t	major;
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_BOUND);
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	/* should be DS_READY except for pcmcia ... */
7670Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(pdip) >= DS_PROBED);
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7700Sstevel@tonic-gate 	(void) ddi_pathname(dip, path);
7710Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "init_node: entry: path %s 0x%p\n",
7720Sstevel@tonic-gate 	    path, (void *)dip));
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 	/*
7750Sstevel@tonic-gate 	 * The parent must have a bus_ctl operation.
7760Sstevel@tonic-gate 	 */
7770Sstevel@tonic-gate 	if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
7780Sstevel@tonic-gate 	    (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_ctl) == NULL) {
7790Sstevel@tonic-gate 		error = DDI_FAILURE;
7800Sstevel@tonic-gate 		goto out;
7810Sstevel@tonic-gate 	}
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	add_global_props(dip);
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	/*
7860Sstevel@tonic-gate 	 * Invoke the parent's bus_ctl operation with the DDI_CTLOPS_INITCHILD
7870Sstevel@tonic-gate 	 * command to transform the child to canonical form 1. If there
7880Sstevel@tonic-gate 	 * is an error, ddi_remove_child should be called, to clean up.
7890Sstevel@tonic-gate 	 */
7900Sstevel@tonic-gate 	error = (*f)(pdip, pdip, DDI_CTLOPS_INITCHILD, dip, NULL);
7910Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
7920Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "init_node: %s 0x%p failed\n",
7930Sstevel@tonic-gate 		    path, (void *)dip));
7940Sstevel@tonic-gate 		remove_global_props(dip);
7950Sstevel@tonic-gate 		/* in case nexus driver didn't clear this field */
7960Sstevel@tonic-gate 		ddi_set_name_addr(dip, NULL);
7970Sstevel@tonic-gate 		error = DDI_FAILURE;
7980Sstevel@tonic-gate 		goto out;
7990Sstevel@tonic-gate 	}
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	ndi_hold_devi(pdip);
8020Sstevel@tonic-gate 
803*4145Scth 	/* recompute path after initchild for @addr information */
804*4145Scth 	(void) ddi_pathname(dip, path);
805*4145Scth 
806*4145Scth 	/* Check for duplicate nodes */
8070Sstevel@tonic-gate 	if (find_duplicate_child(pdip, dip) != NULL) {
8080Sstevel@tonic-gate 		/*
8090Sstevel@tonic-gate 		 * uninit_node() the duplicate - a successful uninit_node()
810*4145Scth 		 * does a ndi_rele_devi.
8110Sstevel@tonic-gate 		 */
8120Sstevel@tonic-gate 		if ((error = uninit_node(dip)) != DDI_SUCCESS) {
8130Sstevel@tonic-gate 			ndi_rele_devi(pdip);
8140Sstevel@tonic-gate 			cmn_err(CE_WARN, "init_node: uninit of duplicate "
8150Sstevel@tonic-gate 			    "node %s failed", path);
8160Sstevel@tonic-gate 		}
8170Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "init_node: duplicate uninit "
8180Sstevel@tonic-gate 		    "%s 0x%p%s\n", path, (void *)dip,
8190Sstevel@tonic-gate 		    (error == DDI_SUCCESS) ? "" : " failed"));
8200Sstevel@tonic-gate 		error = DDI_FAILURE;
8210Sstevel@tonic-gate 		goto out;
8220Sstevel@tonic-gate 	}
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	/*
825*4145Scth 	 * Check to see if we have a path-oriented driver alias that overrides
826*4145Scth 	 * the current driver binding. If so, we need to rebind. This check
827*4145Scth 	 * needs to be delayed until after a successful DDI_CTLOPS_INITCHILD,
828*4145Scth 	 * so the unit-address is established on the last component of the path.
829*4145Scth 	 *
830*4145Scth 	 * NOTE: Allowing a path-oriented alias to change the driver binding
831*4145Scth 	 * of a driver.conf node results in non-intuitive property behavior.
832*4145Scth 	 * We provide a tunable (driver_conf_allow_path_alias) to control
833*4145Scth 	 * this behavior. See uninit_node() for more details.
834*4145Scth 	 *
835*4145Scth 	 * NOTE: If you are adding a path-oriented alias for the boot device,
836*4145Scth 	 * and there is mismatch between OBP and the kernel in regard to
837*4145Scth 	 * generic name use, like "disk" .vs. "ssd", then you will need
838*4145Scth 	 * to add a path-oriented alias for both paths.
839*4145Scth 	 */
840*4145Scth 	major = ddi_name_to_major(path);
841*4145Scth 	if ((major != (major_t)-1) &&
842*4145Scth 	    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED) &&
843*4145Scth 	    (major != DEVI(dip)->devi_major) &&
844*4145Scth 	    (ndi_dev_is_persistent_node(dip) || driver_conf_allow_path_alias)) {
845*4145Scth 
846*4145Scth 		/* Mark node for rebind processing. */
847*4145Scth 		mutex_enter(&DEVI(dip)->devi_lock);
848*4145Scth 		DEVI(dip)->devi_flags |= DEVI_REBIND;
849*4145Scth 		mutex_exit(&DEVI(dip)->devi_lock);
850*4145Scth 
851*4145Scth 		/*
852*4145Scth 		 * uninit_node() current binding - a successful uninit_node()
853*4145Scth 		 * does a ndi_rele_devi.
854*4145Scth 		 */
855*4145Scth 		if ((error = uninit_node(dip)) != DDI_SUCCESS) {
856*4145Scth 			ndi_rele_devi(pdip);
857*4145Scth 			cmn_err(CE_WARN, "init_node: uninit for rebind "
858*4145Scth 			    "of node %s failed", path);
859*4145Scth 			goto out;
860*4145Scth 		}
861*4145Scth 
862*4145Scth 		/* Unbind: demote the node back to DS_LINKED.  */
863*4145Scth 		if ((error = ndi_devi_unbind_driver(dip)) != DDI_SUCCESS) {
864*4145Scth 			cmn_err(CE_WARN, "init_node: unbind for rebind "
865*4145Scth 			    "of node %s failed", path);
866*4145Scth 			goto out;
867*4145Scth 		}
868*4145Scth 
869*4145Scth 		/* establish rebinding name */
870*4145Scth 		if (DEVI(dip)->devi_rebinding_name == NULL)
871*4145Scth 			DEVI(dip)->devi_rebinding_name =
872*4145Scth 			    i_ddi_strdup(path, KM_SLEEP);
873*4145Scth 
874*4145Scth 		/*
875*4145Scth 		 * Now that we are demoted and marked for rebind, repromote.
876*4145Scth 		 * We need to do this in steps, instead of just calling
877*4145Scth 		 * ddi_initchild, so that we can redo the merge operation
878*4145Scth 		 * after we are rebound to the path-bound driver.
879*4145Scth 		 *
880*4145Scth 		 * Start by rebinding node to the path-bound driver.
881*4145Scth 		 */
882*4145Scth 		if ((error = ndi_devi_bind_driver(dip, 0)) != DDI_SUCCESS) {
883*4145Scth 			cmn_err(CE_WARN, "init_node: rebind "
884*4145Scth 			    "of node %s failed", path);
885*4145Scth 			goto out;
886*4145Scth 		}
887*4145Scth 
888*4145Scth 		/*
889*4145Scth 		 * If the node is not a driver.conf node then merge
890*4145Scth 		 * driver.conf properties from new path-bound driver.conf.
891*4145Scth 		 */
892*4145Scth 		if (ndi_dev_is_persistent_node(dip))
893*4145Scth 			(void) i_ndi_make_spec_children(pdip, 0);
894*4145Scth 
895*4145Scth 		/*
896*4145Scth 		 * Now that we have taken care of merge, repromote back
897*4145Scth 		 * to DS_INITIALIZED.
898*4145Scth 		 */
899*4145Scth 		error = ddi_initchild(pdip, dip);
900*4145Scth 		NDI_CONFIG_DEBUG((CE_CONT, "init_node: rebind "
901*4145Scth 		    "%s 0x%p\n", path, (void *)dip));
902*4145Scth 		goto out;
903*4145Scth 	}
904*4145Scth 
905*4145Scth 	/*
9060Sstevel@tonic-gate 	 * Apply multi-parent/deep-nexus optimization to the new node
9070Sstevel@tonic-gate 	 */
9080Sstevel@tonic-gate 	DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
9090Sstevel@tonic-gate 	ddi_optimize_dtree(dip);
9100Sstevel@tonic-gate 	error = DDI_SUCCESS;
9110Sstevel@tonic-gate 
912*4145Scth out:	if (error != DDI_SUCCESS) {
913*4145Scth 		/* On failure ensure that DEVI_REBIND is cleared */
914*4145Scth 		mutex_enter(&DEVI(dip)->devi_lock);
915*4145Scth 		DEVI(dip)->devi_flags &= ~DEVI_REBIND;
916*4145Scth 		mutex_exit(&DEVI(dip)->devi_lock);
917*4145Scth 	}
918*4145Scth 	kmem_free(path, MAXPATHLEN);
9190Sstevel@tonic-gate 	return (error);
9200Sstevel@tonic-gate }
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate /*
9230Sstevel@tonic-gate  * Uninitialize node
9240Sstevel@tonic-gate  * The per-driver list must be held busy during the call.
9250Sstevel@tonic-gate  * A successful uninit_node() releases the init_node() hold on
9260Sstevel@tonic-gate  * the parent by calling ndi_rele_devi().
9270Sstevel@tonic-gate  */
9280Sstevel@tonic-gate static int
9290Sstevel@tonic-gate uninit_node(dev_info_t *dip)
9300Sstevel@tonic-gate {
9310Sstevel@tonic-gate 	int node_state_entry;
9320Sstevel@tonic-gate 	dev_info_t *pdip;
9330Sstevel@tonic-gate 	struct dev_ops *ops;
9340Sstevel@tonic-gate 	int (*f)();
9350Sstevel@tonic-gate 	int error;
9360Sstevel@tonic-gate 	char *addr;
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	/*
9390Sstevel@tonic-gate 	 * Don't check for references here or else a ref-counted
9400Sstevel@tonic-gate 	 * dip cannot be downgraded by the framework.
9410Sstevel@tonic-gate 	 */
9420Sstevel@tonic-gate 	node_state_entry = i_ddi_node_state(dip);
9430Sstevel@tonic-gate 	ASSERT((node_state_entry == DS_BOUND) ||
9440Sstevel@tonic-gate 		(node_state_entry == DS_INITIALIZED));
9450Sstevel@tonic-gate 	pdip = ddi_get_parent(dip);
9460Sstevel@tonic-gate 	ASSERT(pdip);
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "uninit_node: 0x%p(%s%d)\n",
9490Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 	if (((ops = ddi_get_driver(pdip)) == NULL) ||
9520Sstevel@tonic-gate 	    (ops->devo_bus_ops == NULL) ||
9530Sstevel@tonic-gate 	    ((f = ops->devo_bus_ops->bus_ctl) == NULL)) {
9540Sstevel@tonic-gate 		return (DDI_FAILURE);
9550Sstevel@tonic-gate 	}
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 	/*
9580Sstevel@tonic-gate 	 * save the @addr prior to DDI_CTLOPS_UNINITCHILD for use in
9590Sstevel@tonic-gate 	 * freeing the instance if it succeeds.
9600Sstevel@tonic-gate 	 */
9610Sstevel@tonic-gate 	if (node_state_entry == DS_INITIALIZED) {
9620Sstevel@tonic-gate 		addr = ddi_get_name_addr(dip);
9630Sstevel@tonic-gate 		if (addr)
9640Sstevel@tonic-gate 			addr = i_ddi_strdup(addr, KM_SLEEP);
9650Sstevel@tonic-gate 	} else {
9660Sstevel@tonic-gate 		addr = NULL;
9670Sstevel@tonic-gate 	}
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 	error = (*f)(pdip, pdip, DDI_CTLOPS_UNINITCHILD, dip, (void *)NULL);
9700Sstevel@tonic-gate 	if (error == DDI_SUCCESS) {
9710Sstevel@tonic-gate 		/* if uninitchild forgot to set devi_addr to NULL do it now */
9720Sstevel@tonic-gate 		ddi_set_name_addr(dip, NULL);
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 		/*
9750Sstevel@tonic-gate 		 * Free instance number. This is a no-op if instance has
9760Sstevel@tonic-gate 		 * been kept by probe_node().  Avoid free when we are called
9770Sstevel@tonic-gate 		 * from init_node (DS_BOUND) because the instance has not yet
9780Sstevel@tonic-gate 		 * been assigned.
9790Sstevel@tonic-gate 		 */
9800Sstevel@tonic-gate 		if (node_state_entry == DS_INITIALIZED) {
9810Sstevel@tonic-gate 			e_ddi_free_instance(dip, addr);
9820Sstevel@tonic-gate 			DEVI(dip)->devi_instance = -1;
9830Sstevel@tonic-gate 		}
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 		/* release the init_node hold */
9860Sstevel@tonic-gate 		ndi_rele_devi(pdip);
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate 		remove_global_props(dip);
989*4145Scth 
990*4145Scth 		/*
991*4145Scth 		 * NOTE: The decision on whether to allow a path-oriented
992*4145Scth 		 * rebind of a driver.conf enumerated node is made by
993*4145Scth 		 * init_node() based on driver_conf_allow_path_alias. The
994*4145Scth 		 * rebind code below prevents deletion of system properties
995*4145Scth 		 * on driver.conf nodes.
996*4145Scth 		 *
997*4145Scth 		 * When driver_conf_allow_path_alias is set, property behavior
998*4145Scth 		 * on rebound driver.conf file is non-intuitive. For a
999*4145Scth 		 * driver.conf node, the unit-address properties come from
1000*4145Scth 		 * the driver.conf file as system properties. Removing system
1001*4145Scth 		 * properties from a driver.conf node makes the node
1002*4145Scth 		 * useless (we get node without unit-address properties) - so
1003*4145Scth 		 * we leave system properties in place. The result is a node
1004*4145Scth 		 * where system properties come from the node being rebound,
1005*4145Scth 		 * and global properties come from the driver.conf file
1006*4145Scth 		 * of the driver we are rebinding to.  If we could determine
1007*4145Scth 		 * that the path-oriented alias driver.conf file defined a
1008*4145Scth 		 * node at the same unit address, it would be best to use
1009*4145Scth 		 * that node and avoid the non-intuitive property behavior.
1010*4145Scth 		 * Unfortunately, the current "merge" code does not support
1011*4145Scth 		 * this, so we live with the non-intuitive property behavior.
1012*4145Scth 		 */
1013*4145Scth 		if (!((ndi_dev_is_persistent_node(dip) == 0) &&
1014*4145Scth 		    (DEVI(dip)->devi_flags & DEVI_REBIND)))
1015*4145Scth 			e_ddi_prop_remove_all(dip);
10160Sstevel@tonic-gate 	} else {
10170Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "uninit_node failed: 0x%p(%s%d)\n",
10180Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
10190Sstevel@tonic-gate 	}
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 	if (addr)
10220Sstevel@tonic-gate 		kmem_free(addr, strlen(addr) + 1);
10230Sstevel@tonic-gate 	return (error);
10240Sstevel@tonic-gate }
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate /*
10270Sstevel@tonic-gate  * Invoke driver's probe entry point to probe for existence of hardware.
10280Sstevel@tonic-gate  * Keep instance permanent for successful probe and leaf nodes.
10290Sstevel@tonic-gate  *
10300Sstevel@tonic-gate  * Per-driver list must be held busy while calling this function.
10310Sstevel@tonic-gate  */
10320Sstevel@tonic-gate static int
10330Sstevel@tonic-gate probe_node(dev_info_t *dip)
10340Sstevel@tonic-gate {
10350Sstevel@tonic-gate 	int rv;
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_INITIALIZED);
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "probe_node: 0x%p(%s%d)\n",
10400Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 	/* temporarily hold the driver while we probe */
10430Sstevel@tonic-gate 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
10440Sstevel@tonic-gate 	if (DEVI(dip)->devi_ops == NULL) {
10450Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
10460Sstevel@tonic-gate 		    "probe_node: 0x%p(%s%d) cannot load driver\n",
10470Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
10480Sstevel@tonic-gate 		return (DDI_FAILURE);
10490Sstevel@tonic-gate 	}
10500Sstevel@tonic-gate 
10510Sstevel@tonic-gate 	if (identify_9e != 0)
10520Sstevel@tonic-gate 		(void) devi_identify(dip);
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 	rv = devi_probe(dip);
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 	/* release the driver now that probe is complete */
10570Sstevel@tonic-gate 	ndi_rele_driver(dip);
10580Sstevel@tonic-gate 	DEVI(dip)->devi_ops = NULL;
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 	switch (rv) {
10610Sstevel@tonic-gate 	case DDI_PROBE_SUCCESS:			/* found */
10620Sstevel@tonic-gate 	case DDI_PROBE_DONTCARE:		/* ddi_dev_is_sid */
10630Sstevel@tonic-gate 		e_ddi_keep_instance(dip);	/* persist instance */
10640Sstevel@tonic-gate 		rv = DDI_SUCCESS;
10650Sstevel@tonic-gate 		break;
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 	case DDI_PROBE_PARTIAL:			/* maybe later */
10680Sstevel@tonic-gate 	case DDI_PROBE_FAILURE:			/* not found */
10690Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
10700Sstevel@tonic-gate 		    "probe_node: 0x%p(%s%d) no hardware found%s\n",
10710Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip),
10720Sstevel@tonic-gate 		    (rv == DDI_PROBE_PARTIAL) ? " yet" : ""));
10730Sstevel@tonic-gate 		rv = DDI_FAILURE;
10740Sstevel@tonic-gate 		break;
10750Sstevel@tonic-gate 
10760Sstevel@tonic-gate 	default:
10770Sstevel@tonic-gate #ifdef	DEBUG
10780Sstevel@tonic-gate 		cmn_err(CE_WARN, "probe_node: %s%d: illegal probe(9E) value",
10790Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
10800Sstevel@tonic-gate #endif	/* DEBUG */
10810Sstevel@tonic-gate 		rv = DDI_FAILURE;
10820Sstevel@tonic-gate 		break;
10830Sstevel@tonic-gate 	}
10840Sstevel@tonic-gate 	return (rv);
10850Sstevel@tonic-gate }
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate /*
10880Sstevel@tonic-gate  * Unprobe a node. Simply reset the node state.
10890Sstevel@tonic-gate  * Per-driver list must be held busy while calling this function.
10900Sstevel@tonic-gate  */
10910Sstevel@tonic-gate static int
10920Sstevel@tonic-gate unprobe_node(dev_info_t *dip)
10930Sstevel@tonic-gate {
10940Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_PROBED);
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 	/*
10970Sstevel@tonic-gate 	 * Don't check for references here or else a ref-counted
10980Sstevel@tonic-gate 	 * dip cannot be downgraded by the framework.
10990Sstevel@tonic-gate 	 */
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "unprobe_node: 0x%p(name = %s)\n",
11020Sstevel@tonic-gate 	    (void *)dip, ddi_node_name(dip)));
11030Sstevel@tonic-gate 	return (DDI_SUCCESS);
11040Sstevel@tonic-gate }
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate /*
11070Sstevel@tonic-gate  * Attach devinfo node.
11080Sstevel@tonic-gate  * Per-driver list must be held busy.
11090Sstevel@tonic-gate  */
11100Sstevel@tonic-gate static int
11110Sstevel@tonic-gate attach_node(dev_info_t *dip)
11120Sstevel@tonic-gate {
11130Sstevel@tonic-gate 	int rv;
11140Sstevel@tonic-gate 
11152155Scth 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
11160Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_PROBED);
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d)\n",
11190Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 	/*
11220Sstevel@tonic-gate 	 * Tell mpxio framework that a node is about to online.
11230Sstevel@tonic-gate 	 */
11240Sstevel@tonic-gate 	if ((rv = mdi_devi_online(dip, 0)) != NDI_SUCCESS) {
11250Sstevel@tonic-gate 		return (DDI_FAILURE);
11260Sstevel@tonic-gate 	}
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 	/* no recursive attachment */
11290Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_ops == NULL);
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 	/*
11320Sstevel@tonic-gate 	 * Hold driver the node is bound to.
11330Sstevel@tonic-gate 	 */
11340Sstevel@tonic-gate 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
11350Sstevel@tonic-gate 	if (DEVI(dip)->devi_ops == NULL) {
11360Sstevel@tonic-gate 		/*
11370Sstevel@tonic-gate 		 * We were able to load driver for probing, so we should
11380Sstevel@tonic-gate 		 * not get here unless something really bad happened.
11390Sstevel@tonic-gate 		 */
11400Sstevel@tonic-gate 		cmn_err(CE_WARN, "attach_node: no driver for major %d",
11410Sstevel@tonic-gate 		    DEVI(dip)->devi_major);
11420Sstevel@tonic-gate 		return (DDI_FAILURE);
11430Sstevel@tonic-gate 	}
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 	if (NEXUS_DRV(DEVI(dip)->devi_ops))
11460Sstevel@tonic-gate 		DEVI(dip)->devi_taskq = ddi_taskq_create(dip,
11470Sstevel@tonic-gate 		    "nexus_enum_tq", 1,
11480Sstevel@tonic-gate 		    TASKQ_DEFAULTPRI, 0);
11490Sstevel@tonic-gate 
1150495Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
11512009Sdm120769 	DEVI_SET_ATTACHING(dip);
11520Sstevel@tonic-gate 	DEVI_SET_NEED_RESET(dip);
1153495Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
1154495Scth 
11550Sstevel@tonic-gate 	rv = devi_attach(dip, DDI_ATTACH);
1156495Scth 
11572009Sdm120769 	mutex_enter(&(DEVI(dip)->devi_lock));
11582009Sdm120769 	DEVI_CLR_ATTACHING(dip);
11592009Sdm120769 
11601961Scth 	if (rv != DDI_SUCCESS) {
11612155Scth 		DEVI_CLR_NEED_RESET(dip);
11622155Scth 
1163438Scth 		/* ensure that devids are unregistered */
11640Sstevel@tonic-gate 		if (DEVI(dip)->devi_flags & DEVI_REGISTERED_DEVID) {
1165438Scth 			DEVI(dip)->devi_flags &= ~DEVI_REGISTERED_DEVID;
1166438Scth 			mutex_exit(&DEVI(dip)->devi_lock);
1167438Scth 
11680Sstevel@tonic-gate 			e_devid_cache_unregister(dip);
1169438Scth 		} else
1170438Scth 			mutex_exit(&DEVI(dip)->devi_lock);
1171438Scth 
11720Sstevel@tonic-gate 		/*
11730Sstevel@tonic-gate 		 * Cleanup dacf reservations
11740Sstevel@tonic-gate 		 */
11750Sstevel@tonic-gate 		mutex_enter(&dacf_lock);
11760Sstevel@tonic-gate 		dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH);
11770Sstevel@tonic-gate 		dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH);
11780Sstevel@tonic-gate 		mutex_exit(&dacf_lock);
11790Sstevel@tonic-gate 		if (DEVI(dip)->devi_taskq)
11800Sstevel@tonic-gate 			ddi_taskq_destroy(DEVI(dip)->devi_taskq);
11810Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
11820Sstevel@tonic-gate 
11830Sstevel@tonic-gate 		/* release the driver if attach failed */
11840Sstevel@tonic-gate 		ndi_rele_driver(dip);
11850Sstevel@tonic-gate 		DEVI(dip)->devi_ops = NULL;
11860Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d) failed\n",
11870Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
11880Sstevel@tonic-gate 		return (DDI_FAILURE);
11892009Sdm120769 	} else
11902009Sdm120769 		mutex_exit(&DEVI(dip)->devi_lock);
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 	/* successful attach, return with driver held */
1193495Scth 
11940Sstevel@tonic-gate 	return (DDI_SUCCESS);
11950Sstevel@tonic-gate }
11960Sstevel@tonic-gate 
11970Sstevel@tonic-gate /*
11980Sstevel@tonic-gate  * Detach devinfo node.
11990Sstevel@tonic-gate  * Per-driver list must be held busy.
12000Sstevel@tonic-gate  */
12010Sstevel@tonic-gate static int
12020Sstevel@tonic-gate detach_node(dev_info_t *dip, uint_t flag)
12030Sstevel@tonic-gate {
120453Scth 	struct devnames	*dnp;
120553Scth 	int		rv;
120653Scth 
12072155Scth 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
12080Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_ATTACHED);
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate 	/* check references */
12110Sstevel@tonic-gate 	if (DEVI(dip)->devi_ref)
12120Sstevel@tonic-gate 		return (DDI_FAILURE);
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "detach_node: 0x%p(%s%d)\n",
12150Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
12160Sstevel@tonic-gate 
12172155Scth 	/*
12182155Scth 	 * NOTE: If we are processing a pHCI node then the calling code
12192155Scth 	 * must detect this and ndi_devi_enter() in (vHCI, parent(pHCI))
12202155Scth 	 * order unless pHCI and vHCI are siblings.  Code paths leading
12212155Scth 	 * here that must ensure this ordering include:
12222155Scth 	 * unconfig_immediate_children(), devi_unconfig_one(),
12232155Scth 	 * ndi_devi_unconfig_one(), ndi_devi_offline().
12242155Scth 	 */
12252155Scth 	ASSERT(!MDI_PHCI(dip) ||
12262155Scth 	    (ddi_get_parent(mdi_devi_get_vdip(dip)) == ddi_get_parent(dip)) ||
12272155Scth 	    DEVI_BUSY_OWNED(mdi_devi_get_vdip(dip)));
12282155Scth 
12290Sstevel@tonic-gate 	/* Offline the device node with the mpxio framework. */
12300Sstevel@tonic-gate 	if (mdi_devi_offline(dip, flag) != NDI_SUCCESS) {
12310Sstevel@tonic-gate 		return (DDI_FAILURE);
12320Sstevel@tonic-gate 	}
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate 	/* drain the taskq */
12350Sstevel@tonic-gate 	if (DEVI(dip)->devi_taskq)
12360Sstevel@tonic-gate 		ddi_taskq_wait(DEVI(dip)->devi_taskq);
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 	rv = devi_detach(dip, DDI_DETACH);
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate 	if (rv != DDI_SUCCESS) {
12410Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
12420Sstevel@tonic-gate 		    "detach_node: 0x%p(%s%d) failed\n",
12430Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
12440Sstevel@tonic-gate 		return (DDI_FAILURE);
12450Sstevel@tonic-gate 	}
12460Sstevel@tonic-gate 
12472155Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
12482155Scth 	DEVI_CLR_NEED_RESET(dip);
12492155Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
12502155Scth 
12510Sstevel@tonic-gate 	/* destroy the taskq */
12520Sstevel@tonic-gate 	if (DEVI(dip)->devi_taskq) {
12530Sstevel@tonic-gate 		ddi_taskq_destroy(DEVI(dip)->devi_taskq);
12540Sstevel@tonic-gate 		DEVI(dip)->devi_taskq = NULL;
12550Sstevel@tonic-gate 	}
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate 	/* Cleanup dacf reservations */
12580Sstevel@tonic-gate 	mutex_enter(&dacf_lock);
12590Sstevel@tonic-gate 	dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH);
12600Sstevel@tonic-gate 	dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH);
12610Sstevel@tonic-gate 	mutex_exit(&dacf_lock);
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 	/* Remove properties and minor nodes in case driver forgots */
12640Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
12650Sstevel@tonic-gate 	ddi_prop_remove_all(dip);
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 	/* a detached node can't have attached or .conf children */
12680Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
12690Sstevel@tonic-gate 	DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN|DEVI_ATTACHED_CHILDREN);
1270438Scth 
1271438Scth 	/* ensure that devids registered during attach are unregistered */
12720Sstevel@tonic-gate 	if (DEVI(dip)->devi_flags & DEVI_REGISTERED_DEVID) {
12730Sstevel@tonic-gate 		DEVI(dip)->devi_flags &= ~DEVI_REGISTERED_DEVID;
1274438Scth 		mutex_exit(&DEVI(dip)->devi_lock);
1275438Scth 
1276438Scth 		e_devid_cache_unregister(dip);
1277438Scth 	} else
1278438Scth 		mutex_exit(&DEVI(dip)->devi_lock);
12790Sstevel@tonic-gate 
128053Scth 	/*
128153Scth 	 * If the instance has successfully detached in detach_driver() context,
128253Scth 	 * clear DN_DRIVER_HELD for correct ddi_hold_installed_driver()
128353Scth 	 * behavior. Consumers like qassociate() depend on this (via clnopen()).
128453Scth 	 */
128553Scth 	if (flag & NDI_DETACH_DRIVER) {
128653Scth 		dnp = &(devnamesp[DEVI(dip)->devi_major]);
128753Scth 		LOCK_DEV_OPS(&dnp->dn_lock);
128853Scth 		dnp->dn_flags &= ~DN_DRIVER_HELD;
128953Scth 		UNLOCK_DEV_OPS(&dnp->dn_lock);
129053Scth 	}
129153Scth 
12920Sstevel@tonic-gate 	/* successful detach, release the driver */
12930Sstevel@tonic-gate 	ndi_rele_driver(dip);
12940Sstevel@tonic-gate 	DEVI(dip)->devi_ops = NULL;
12950Sstevel@tonic-gate 	return (DDI_SUCCESS);
12960Sstevel@tonic-gate }
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate /*
12990Sstevel@tonic-gate  * Run dacf post_attach routines
13000Sstevel@tonic-gate  */
13010Sstevel@tonic-gate static int
13020Sstevel@tonic-gate postattach_node(dev_info_t *dip)
13030Sstevel@tonic-gate {
13040Sstevel@tonic-gate 	int rval;
13050Sstevel@tonic-gate 
13060Sstevel@tonic-gate 	/*
13070Sstevel@tonic-gate 	 * For hotplug busses like USB, it's possible that devices
13080Sstevel@tonic-gate 	 * are removed but dip is still around. We don't want to
13090Sstevel@tonic-gate 	 * run dacf routines as part of detach failure recovery.
13100Sstevel@tonic-gate 	 *
13110Sstevel@tonic-gate 	 * Pretend success until we figure out how to prevent
13120Sstevel@tonic-gate 	 * access to such devinfo nodes.
13130Sstevel@tonic-gate 	 */
13140Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_REMOVED(dip))
13150Sstevel@tonic-gate 		return (DDI_SUCCESS);
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate 	/*
13180Sstevel@tonic-gate 	 * if dacf_postattach failed, report it to the framework
13190Sstevel@tonic-gate 	 * so that it can be retried later at the open time.
13200Sstevel@tonic-gate 	 */
13210Sstevel@tonic-gate 	mutex_enter(&dacf_lock);
13220Sstevel@tonic-gate 	rval = dacfc_postattach(dip);
13230Sstevel@tonic-gate 	mutex_exit(&dacf_lock);
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 	/*
13260Sstevel@tonic-gate 	 * Plumbing during postattach may fail because of the
13270Sstevel@tonic-gate 	 * underlying device is not ready. This will fail ndi_devi_config()
13280Sstevel@tonic-gate 	 * in dv_filldir() and a warning message is issued. The message
13290Sstevel@tonic-gate 	 * from here will explain what happened
13300Sstevel@tonic-gate 	 */
13310Sstevel@tonic-gate 	if (rval != DACF_SUCCESS) {
13320Sstevel@tonic-gate 		cmn_err(CE_WARN, "Postattach failed for %s%d\n",
13330Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
13340Sstevel@tonic-gate 		return (DDI_FAILURE);
13350Sstevel@tonic-gate 	}
13360Sstevel@tonic-gate 
13370Sstevel@tonic-gate 	return (DDI_SUCCESS);
13380Sstevel@tonic-gate }
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate /*
13410Sstevel@tonic-gate  * Run dacf pre-detach routines
13420Sstevel@tonic-gate  */
13430Sstevel@tonic-gate static int
13440Sstevel@tonic-gate predetach_node(dev_info_t *dip, uint_t flag)
13450Sstevel@tonic-gate {
13460Sstevel@tonic-gate 	int ret;
13470Sstevel@tonic-gate 
13480Sstevel@tonic-gate 	/*
13490Sstevel@tonic-gate 	 * Don't auto-detach if DDI_FORCEATTACH or DDI_NO_AUTODETACH
13500Sstevel@tonic-gate 	 * properties are set.
13510Sstevel@tonic-gate 	 */
13520Sstevel@tonic-gate 	if (flag & NDI_AUTODETACH) {
13530Sstevel@tonic-gate 		struct devnames *dnp;
13540Sstevel@tonic-gate 		int pflag = DDI_PROP_NOTPROM | DDI_PROP_DONTPASS;
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 		if ((ddi_prop_get_int(DDI_DEV_T_ANY, dip,
13570Sstevel@tonic-gate 			pflag, DDI_FORCEATTACH, 0) == 1) ||
13580Sstevel@tonic-gate 		    (ddi_prop_get_int(DDI_DEV_T_ANY, dip,
13590Sstevel@tonic-gate 			pflag, DDI_NO_AUTODETACH, 0) == 1))
13600Sstevel@tonic-gate 			return (DDI_FAILURE);
13610Sstevel@tonic-gate 
13620Sstevel@tonic-gate 		/* check for driver global version of DDI_NO_AUTODETACH */
13630Sstevel@tonic-gate 		dnp = &devnamesp[DEVI(dip)->devi_major];
13640Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
13650Sstevel@tonic-gate 		if (dnp->dn_flags & DN_NO_AUTODETACH) {
13660Sstevel@tonic-gate 			UNLOCK_DEV_OPS(&dnp->dn_lock);
13670Sstevel@tonic-gate 			return (DDI_FAILURE);
13680Sstevel@tonic-gate 		}
13690Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
13700Sstevel@tonic-gate 	}
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 	mutex_enter(&dacf_lock);
13730Sstevel@tonic-gate 	ret = dacfc_predetach(dip);
13740Sstevel@tonic-gate 	mutex_exit(&dacf_lock);
13750Sstevel@tonic-gate 
13760Sstevel@tonic-gate 	return (ret);
13770Sstevel@tonic-gate }
13780Sstevel@tonic-gate 
13790Sstevel@tonic-gate /*
13800Sstevel@tonic-gate  * Wrapper for making multiple state transitions
13810Sstevel@tonic-gate  */
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate /*
13840Sstevel@tonic-gate  * i_ndi_config_node: upgrade dev_info node into a specified state.
13850Sstevel@tonic-gate  * It is a bit tricky because the locking protocol changes before and
13860Sstevel@tonic-gate  * after a node is bound to a driver. All locks are held external to
13870Sstevel@tonic-gate  * this function.
13880Sstevel@tonic-gate  */
13890Sstevel@tonic-gate int
13900Sstevel@tonic-gate i_ndi_config_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag)
13910Sstevel@tonic-gate {
13920Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flag))
13930Sstevel@tonic-gate 	int rv = DDI_SUCCESS;
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate 	while ((i_ddi_node_state(dip) < state) && (rv == DDI_SUCCESS)) {
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate 		/* don't allow any more changes to the device tree */
14000Sstevel@tonic-gate 		if (devinfo_freeze) {
14010Sstevel@tonic-gate 			rv = DDI_FAILURE;
14020Sstevel@tonic-gate 			break;
14030Sstevel@tonic-gate 		}
14040Sstevel@tonic-gate 
14050Sstevel@tonic-gate 		switch (i_ddi_node_state(dip)) {
14060Sstevel@tonic-gate 		case DS_PROTO:
14070Sstevel@tonic-gate 			/*
14080Sstevel@tonic-gate 			 * only caller can reference this node, no external
14090Sstevel@tonic-gate 			 * locking needed.
14100Sstevel@tonic-gate 			 */
14110Sstevel@tonic-gate 			link_node(dip);
14120Sstevel@tonic-gate 			i_ddi_set_node_state(dip, DS_LINKED);
14130Sstevel@tonic-gate 			break;
14140Sstevel@tonic-gate 		case DS_LINKED:
14150Sstevel@tonic-gate 			/*
14160Sstevel@tonic-gate 			 * Three code path may attempt to bind a node:
14170Sstevel@tonic-gate 			 * - boot code
14180Sstevel@tonic-gate 			 * - add_drv
14190Sstevel@tonic-gate 			 * - hotplug thread
14200Sstevel@tonic-gate 			 * Boot code is single threaded, add_drv synchronize
14210Sstevel@tonic-gate 			 * on a userland lock, and hotplug synchronize on
14220Sstevel@tonic-gate 			 * hotplug_lk. There could be a race between add_drv
14230Sstevel@tonic-gate 			 * and hotplug thread. We'll live with this until the
14240Sstevel@tonic-gate 			 * conversion to top-down loading.
14250Sstevel@tonic-gate 			 */
14260Sstevel@tonic-gate 			if ((rv = bind_node(dip)) == DDI_SUCCESS)
14270Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_BOUND);
1428*4145Scth 
14290Sstevel@tonic-gate 			break;
14300Sstevel@tonic-gate 		case DS_BOUND:
14310Sstevel@tonic-gate 			/*
14320Sstevel@tonic-gate 			 * The following transitions synchronizes on the
14330Sstevel@tonic-gate 			 * per-driver busy changing flag, since we already
14340Sstevel@tonic-gate 			 * have a driver.
14350Sstevel@tonic-gate 			 */
14360Sstevel@tonic-gate 			if ((rv = init_node(dip)) == DDI_SUCCESS)
14370Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_INITIALIZED);
14380Sstevel@tonic-gate 			break;
14390Sstevel@tonic-gate 		case DS_INITIALIZED:
14400Sstevel@tonic-gate 			if ((rv = probe_node(dip)) == DDI_SUCCESS)
14410Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_PROBED);
14420Sstevel@tonic-gate 			break;
14430Sstevel@tonic-gate 		case DS_PROBED:
14440Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, 1);
14450Sstevel@tonic-gate 			if ((rv = attach_node(dip)) == DDI_SUCCESS)
14460Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_ATTACHED);
14470Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, -1);
14480Sstevel@tonic-gate 			break;
14490Sstevel@tonic-gate 		case DS_ATTACHED:
14500Sstevel@tonic-gate 			if ((rv = postattach_node(dip)) == DDI_SUCCESS)
14510Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_READY);
14520Sstevel@tonic-gate 			break;
14530Sstevel@tonic-gate 		case DS_READY:
14540Sstevel@tonic-gate 			break;
14550Sstevel@tonic-gate 		default:
14560Sstevel@tonic-gate 			/* should never reach here */
14570Sstevel@tonic-gate 			ASSERT("unknown devinfo state");
14580Sstevel@tonic-gate 		}
14590Sstevel@tonic-gate 	}
14600Sstevel@tonic-gate 
14610Sstevel@tonic-gate 	if (ddidebug & DDI_AUDIT)
14620Sstevel@tonic-gate 		da_log_enter(dip);
14630Sstevel@tonic-gate 	return (rv);
14640Sstevel@tonic-gate }
14650Sstevel@tonic-gate 
14660Sstevel@tonic-gate /*
14670Sstevel@tonic-gate  * i_ndi_unconfig_node: downgrade dev_info node into a specified state.
14680Sstevel@tonic-gate  */
14690Sstevel@tonic-gate int
14700Sstevel@tonic-gate i_ndi_unconfig_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag)
14710Sstevel@tonic-gate {
14720Sstevel@tonic-gate 	int rv = DDI_SUCCESS;
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate 	while ((i_ddi_node_state(dip) > state) && (rv == DDI_SUCCESS)) {
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate 		/* don't allow any more changes to the device tree */
14790Sstevel@tonic-gate 		if (devinfo_freeze) {
14800Sstevel@tonic-gate 			rv = DDI_FAILURE;
14810Sstevel@tonic-gate 			break;
14820Sstevel@tonic-gate 		}
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate 		switch (i_ddi_node_state(dip)) {
14850Sstevel@tonic-gate 		case DS_PROTO:
14860Sstevel@tonic-gate 			break;
14870Sstevel@tonic-gate 		case DS_LINKED:
14880Sstevel@tonic-gate 			/*
14890Sstevel@tonic-gate 			 * Persistent nodes are only removed by hotplug code
14900Sstevel@tonic-gate 			 * .conf nodes synchronizes on per-driver list.
14910Sstevel@tonic-gate 			 */
14920Sstevel@tonic-gate 			if ((rv = unlink_node(dip)) == DDI_SUCCESS)
14930Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_PROTO);
14940Sstevel@tonic-gate 			break;
14950Sstevel@tonic-gate 		case DS_BOUND:
14960Sstevel@tonic-gate 			/*
14970Sstevel@tonic-gate 			 * The following transitions synchronizes on the
14980Sstevel@tonic-gate 			 * per-driver busy changing flag, since we already
14990Sstevel@tonic-gate 			 * have a driver.
15000Sstevel@tonic-gate 			 */
15010Sstevel@tonic-gate 			if ((rv = unbind_node(dip)) == DDI_SUCCESS)
15020Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_LINKED);
15030Sstevel@tonic-gate 			break;
15040Sstevel@tonic-gate 		case DS_INITIALIZED:
15050Sstevel@tonic-gate 			if ((rv = uninit_node(dip)) == DDI_SUCCESS)
15060Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_BOUND);
15070Sstevel@tonic-gate 			break;
15080Sstevel@tonic-gate 		case DS_PROBED:
15090Sstevel@tonic-gate 			if ((rv = unprobe_node(dip)) == DDI_SUCCESS)
15100Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_INITIALIZED);
15110Sstevel@tonic-gate 			break;
15120Sstevel@tonic-gate 		case DS_ATTACHED:
15130Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, 1);
1514495Scth 
1515495Scth 			mutex_enter(&(DEVI(dip)->devi_lock));
15160Sstevel@tonic-gate 			DEVI_SET_DETACHING(dip);
1517495Scth 			mutex_exit(&(DEVI(dip)->devi_lock));
1518495Scth 
15190Sstevel@tonic-gate 			membar_enter();	/* ensure visibility for hold_devi */
15200Sstevel@tonic-gate 
15210Sstevel@tonic-gate 			if ((rv = detach_node(dip, flag)) == DDI_SUCCESS)
15220Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_PROBED);
1523495Scth 
1524495Scth 			mutex_enter(&(DEVI(dip)->devi_lock));
15250Sstevel@tonic-gate 			DEVI_CLR_DETACHING(dip);
1526495Scth 			mutex_exit(&(DEVI(dip)->devi_lock));
1527495Scth 
15280Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, -1);
15290Sstevel@tonic-gate 			break;
15300Sstevel@tonic-gate 		case DS_READY:
15310Sstevel@tonic-gate 			if ((rv = predetach_node(dip, flag)) == DDI_SUCCESS)
15320Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_ATTACHED);
15330Sstevel@tonic-gate 			break;
15340Sstevel@tonic-gate 		default:
15350Sstevel@tonic-gate 			ASSERT("unknown devinfo state");
15360Sstevel@tonic-gate 		}
15370Sstevel@tonic-gate 	}
15380Sstevel@tonic-gate 	da_log_enter(dip);
15390Sstevel@tonic-gate 	return (rv);
15400Sstevel@tonic-gate }
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate /*
15430Sstevel@tonic-gate  * ddi_initchild: transform node to DS_INITIALIZED state
15440Sstevel@tonic-gate  */
15450Sstevel@tonic-gate int
15460Sstevel@tonic-gate ddi_initchild(dev_info_t *parent, dev_info_t *proto)
15470Sstevel@tonic-gate {
15480Sstevel@tonic-gate 	int ret, circ;
15490Sstevel@tonic-gate 
15500Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
15510Sstevel@tonic-gate 	ret = i_ndi_config_node(proto, DS_INITIALIZED, 0);
15520Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
15530Sstevel@tonic-gate 
15540Sstevel@tonic-gate 	return (ret);
15550Sstevel@tonic-gate }
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate /*
15580Sstevel@tonic-gate  * ddi_uninitchild: transform node down to DS_BOUND state
15590Sstevel@tonic-gate  */
15600Sstevel@tonic-gate int
15610Sstevel@tonic-gate ddi_uninitchild(dev_info_t *dip)
15620Sstevel@tonic-gate {
15630Sstevel@tonic-gate 	int ret, circ;
15640Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
15650Sstevel@tonic-gate 	ASSERT(parent);
15660Sstevel@tonic-gate 
15670Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
15680Sstevel@tonic-gate 	ret = i_ndi_unconfig_node(dip, DS_BOUND, 0);
15690Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate 	return (ret);
15720Sstevel@tonic-gate }
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate /*
15751333Scth  * i_ddi_attachchild: transform node to DS_READY/i_ddi_devi_attached() state
15760Sstevel@tonic-gate  */
15770Sstevel@tonic-gate static int
15780Sstevel@tonic-gate i_ddi_attachchild(dev_info_t *dip)
15790Sstevel@tonic-gate {
15802155Scth 	dev_info_t	*parent = ddi_get_parent(dip);
15812155Scth 	int		ret;
15822155Scth 
15832155Scth 	ASSERT(parent && DEVI_BUSY_OWNED(parent));
15840Sstevel@tonic-gate 
15850Sstevel@tonic-gate 	if ((i_ddi_node_state(dip) < DS_BOUND) || DEVI_IS_DEVICE_OFFLINE(dip))
15860Sstevel@tonic-gate 		return (DDI_FAILURE);
15870Sstevel@tonic-gate 
15880Sstevel@tonic-gate 	ret = i_ndi_config_node(dip, DS_READY, 0);
15890Sstevel@tonic-gate 	if (ret == NDI_SUCCESS) {
15900Sstevel@tonic-gate 		ret = DDI_SUCCESS;
15910Sstevel@tonic-gate 	} else {
15920Sstevel@tonic-gate 		/*
15930Sstevel@tonic-gate 		 * Take it down to DS_INITIALIZED so pm_pre_probe is run
15940Sstevel@tonic-gate 		 * on the next attach
15950Sstevel@tonic-gate 		 */
15960Sstevel@tonic-gate 		(void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0);
15970Sstevel@tonic-gate 		ret = DDI_FAILURE;
15980Sstevel@tonic-gate 	}
15990Sstevel@tonic-gate 
16000Sstevel@tonic-gate 	return (ret);
16010Sstevel@tonic-gate }
16020Sstevel@tonic-gate 
16030Sstevel@tonic-gate /*
16040Sstevel@tonic-gate  * i_ddi_detachchild: transform node down to DS_PROBED state
16050Sstevel@tonic-gate  *	If it fails, put it back to DS_READY state.
16060Sstevel@tonic-gate  * NOTE: A node that fails detach may be at DS_ATTACHED instead
16071333Scth  * of DS_READY for a small amount of time - this is the source of
16081333Scth  * transient DS_READY->DS_ATTACHED->DS_READY state changes.
16090Sstevel@tonic-gate  */
16100Sstevel@tonic-gate static int
16110Sstevel@tonic-gate i_ddi_detachchild(dev_info_t *dip, uint_t flags)
16120Sstevel@tonic-gate {
16132155Scth 	dev_info_t	*parent = ddi_get_parent(dip);
16142155Scth 	int		ret;
16152155Scth 
16162155Scth 	ASSERT(parent && DEVI_BUSY_OWNED(parent));
16172155Scth 
16180Sstevel@tonic-gate 	ret = i_ndi_unconfig_node(dip, DS_PROBED, flags);
16190Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
16200Sstevel@tonic-gate 		(void) i_ndi_config_node(dip, DS_READY, 0);
16210Sstevel@tonic-gate 	else
16220Sstevel@tonic-gate 		/* allow pm_pre_probe to reestablish pm state */
16230Sstevel@tonic-gate 		(void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0);
16240Sstevel@tonic-gate 	return (ret);
16250Sstevel@tonic-gate }
16260Sstevel@tonic-gate 
16270Sstevel@tonic-gate /*
16280Sstevel@tonic-gate  * Add a child and bind to driver
16290Sstevel@tonic-gate  */
16300Sstevel@tonic-gate dev_info_t *
16310Sstevel@tonic-gate ddi_add_child(dev_info_t *pdip, char *name, uint_t nodeid, uint_t unit)
16320Sstevel@tonic-gate {
16330Sstevel@tonic-gate 	int circ;
16340Sstevel@tonic-gate 	dev_info_t *dip;
16350Sstevel@tonic-gate 
16360Sstevel@tonic-gate 	/* allocate a new node */
16370Sstevel@tonic-gate 	dip = i_ddi_alloc_node(pdip, name, nodeid, (int)unit, NULL, KM_SLEEP);
16380Sstevel@tonic-gate 
16390Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
16400Sstevel@tonic-gate 	(void) i_ndi_config_node(dip, DS_BOUND, 0);
16410Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
16420Sstevel@tonic-gate 	return (dip);
16430Sstevel@tonic-gate }
16440Sstevel@tonic-gate 
16450Sstevel@tonic-gate /*
16460Sstevel@tonic-gate  * ddi_remove_child: remove the dip. The parent must be attached and held
16470Sstevel@tonic-gate  */
16480Sstevel@tonic-gate int
16490Sstevel@tonic-gate ddi_remove_child(dev_info_t *dip, int dummy)
16500Sstevel@tonic-gate {
16510Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dummy))
16520Sstevel@tonic-gate 	int circ, ret;
16530Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
16540Sstevel@tonic-gate 	ASSERT(parent);
16550Sstevel@tonic-gate 
16560Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
16570Sstevel@tonic-gate 
16580Sstevel@tonic-gate 	/*
16590Sstevel@tonic-gate 	 * If we still have children, for example SID nodes marked
16600Sstevel@tonic-gate 	 * as persistent but not attached, attempt to remove them.
16610Sstevel@tonic-gate 	 */
16620Sstevel@tonic-gate 	if (DEVI(dip)->devi_child) {
16630Sstevel@tonic-gate 		ret = ndi_devi_unconfig(dip, NDI_DEVI_REMOVE);
16640Sstevel@tonic-gate 		if (ret != NDI_SUCCESS) {
16650Sstevel@tonic-gate 			ndi_devi_exit(parent, circ);
16660Sstevel@tonic-gate 			return (DDI_FAILURE);
16670Sstevel@tonic-gate 		}
16680Sstevel@tonic-gate 		ASSERT(DEVI(dip)->devi_child == NULL);
16690Sstevel@tonic-gate 	}
16700Sstevel@tonic-gate 
16710Sstevel@tonic-gate 	ret = i_ndi_unconfig_node(dip, DS_PROTO, 0);
16720Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
16730Sstevel@tonic-gate 
16740Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
16750Sstevel@tonic-gate 		return (ret);
16760Sstevel@tonic-gate 
16770Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_PROTO);
16780Sstevel@tonic-gate 	i_ddi_free_node(dip);
16790Sstevel@tonic-gate 	return (DDI_SUCCESS);
16800Sstevel@tonic-gate }
16810Sstevel@tonic-gate 
16820Sstevel@tonic-gate /*
16830Sstevel@tonic-gate  * NDI wrappers for ref counting, node allocation, and transitions
16840Sstevel@tonic-gate  */
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate /*
16870Sstevel@tonic-gate  * Hold/release the devinfo node itself.
16880Sstevel@tonic-gate  * Caller is assumed to prevent the devi from detaching during this call
16890Sstevel@tonic-gate  */
16900Sstevel@tonic-gate void
16910Sstevel@tonic-gate ndi_hold_devi(dev_info_t *dip)
16920Sstevel@tonic-gate {
16930Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
16940Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_ref >= 0);
16950Sstevel@tonic-gate 	DEVI(dip)->devi_ref++;
16960Sstevel@tonic-gate 	membar_enter();			/* make sure stores are flushed */
16970Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
16980Sstevel@tonic-gate }
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate void
17010Sstevel@tonic-gate ndi_rele_devi(dev_info_t *dip)
17020Sstevel@tonic-gate {
17030Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_ref > 0);
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
17060Sstevel@tonic-gate 	DEVI(dip)->devi_ref--;
17070Sstevel@tonic-gate 	membar_enter();			/* make sure stores are flushed */
17080Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
17090Sstevel@tonic-gate }
17100Sstevel@tonic-gate 
17110Sstevel@tonic-gate int
17120Sstevel@tonic-gate e_ddi_devi_holdcnt(dev_info_t *dip)
17130Sstevel@tonic-gate {
17140Sstevel@tonic-gate 	return (DEVI(dip)->devi_ref);
17150Sstevel@tonic-gate }
17160Sstevel@tonic-gate 
17170Sstevel@tonic-gate /*
17180Sstevel@tonic-gate  * Hold/release the driver the devinfo node is bound to.
17190Sstevel@tonic-gate  */
17200Sstevel@tonic-gate struct dev_ops *
17210Sstevel@tonic-gate ndi_hold_driver(dev_info_t *dip)
17220Sstevel@tonic-gate {
17230Sstevel@tonic-gate 	if (i_ddi_node_state(dip) < DS_BOUND)
17240Sstevel@tonic-gate 		return (NULL);
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_major != -1);
17270Sstevel@tonic-gate 	return (mod_hold_dev_by_major(DEVI(dip)->devi_major));
17280Sstevel@tonic-gate }
17290Sstevel@tonic-gate 
17300Sstevel@tonic-gate void
17310Sstevel@tonic-gate ndi_rele_driver(dev_info_t *dip)
17320Sstevel@tonic-gate {
17330Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) >= DS_BOUND);
17340Sstevel@tonic-gate 	mod_rele_dev_by_major(DEVI(dip)->devi_major);
17350Sstevel@tonic-gate }
17360Sstevel@tonic-gate 
17370Sstevel@tonic-gate /*
17380Sstevel@tonic-gate  * Single thread entry into devinfo node for modifying its children.
17390Sstevel@tonic-gate  * To verify in ASSERTS use DEVI_BUSY_OWNED macro.
17400Sstevel@tonic-gate  */
17410Sstevel@tonic-gate void
17420Sstevel@tonic-gate ndi_devi_enter(dev_info_t *dip, int *circular)
17430Sstevel@tonic-gate {
17440Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
17450Sstevel@tonic-gate 	ASSERT(dip != NULL);
17460Sstevel@tonic-gate 
17472155Scth 	/* for vHCI, enforce (vHCI, pHCI) ndi_deve_enter() order */
17482155Scth 	ASSERT(!MDI_VHCI(dip) || (mdi_devi_pdip_entered(dip) == 0) ||
17492155Scth 	    DEVI_BUSY_OWNED(dip));
17502155Scth 
17510Sstevel@tonic-gate 	mutex_enter(&devi->devi_lock);
17520Sstevel@tonic-gate 	if (devi->devi_busy_thread == curthread) {
17530Sstevel@tonic-gate 		devi->devi_circular++;
17540Sstevel@tonic-gate 	} else {
17550Sstevel@tonic-gate 		while (DEVI_BUSY_CHANGING(devi) && !panicstr)
17560Sstevel@tonic-gate 			cv_wait(&(devi->devi_cv), &(devi->devi_lock));
17570Sstevel@tonic-gate 		if (panicstr) {
17580Sstevel@tonic-gate 			mutex_exit(&devi->devi_lock);
17590Sstevel@tonic-gate 			return;
17600Sstevel@tonic-gate 		}
17610Sstevel@tonic-gate 		devi->devi_flags |= DEVI_BUSY;
17620Sstevel@tonic-gate 		devi->devi_busy_thread = curthread;
17630Sstevel@tonic-gate 	}
17640Sstevel@tonic-gate 	*circular = devi->devi_circular;
17650Sstevel@tonic-gate 	mutex_exit(&devi->devi_lock);
17660Sstevel@tonic-gate }
17670Sstevel@tonic-gate 
17680Sstevel@tonic-gate /*
17690Sstevel@tonic-gate  * Release ndi_devi_enter or successful ndi_devi_tryenter.
17700Sstevel@tonic-gate  */
17710Sstevel@tonic-gate void
17720Sstevel@tonic-gate ndi_devi_exit(dev_info_t *dip, int circular)
17730Sstevel@tonic-gate {
17742155Scth 	struct dev_info	*devi = DEVI(dip);
17752155Scth 	struct dev_info	*vdevi;
17760Sstevel@tonic-gate 	ASSERT(dip != NULL);
17770Sstevel@tonic-gate 
17780Sstevel@tonic-gate 	if (panicstr)
17790Sstevel@tonic-gate 		return;
17800Sstevel@tonic-gate 
17810Sstevel@tonic-gate 	mutex_enter(&(devi->devi_lock));
17820Sstevel@tonic-gate 	if (circular != 0) {
17830Sstevel@tonic-gate 		devi->devi_circular--;
17840Sstevel@tonic-gate 	} else {
17850Sstevel@tonic-gate 		devi->devi_flags &= ~DEVI_BUSY;
17860Sstevel@tonic-gate 		ASSERT(devi->devi_busy_thread == curthread);
17870Sstevel@tonic-gate 		devi->devi_busy_thread = NULL;
17880Sstevel@tonic-gate 		cv_broadcast(&(devi->devi_cv));
17890Sstevel@tonic-gate 	}
17900Sstevel@tonic-gate 	mutex_exit(&(devi->devi_lock));
17912155Scth 
17922155Scth 	/*
17932155Scth 	 * For pHCI exit we issue a broadcast to vHCI for ndi_devi_config_one()
17942155Scth 	 * doing cv_wait on vHCI.
17952155Scth 	 */
17962155Scth 	if (MDI_PHCI(dip)) {
17972155Scth 		vdevi = DEVI(mdi_devi_get_vdip(dip));
17982155Scth 		if (vdevi) {
17992155Scth 			mutex_enter(&(vdevi->devi_lock));
18002155Scth 			if (vdevi->devi_flags & DEVI_PHCI_SIGNALS_VHCI) {
18012155Scth 				vdevi->devi_flags &= ~DEVI_PHCI_SIGNALS_VHCI;
18022155Scth 				cv_broadcast(&(vdevi->devi_cv));
18032155Scth 			}
18042155Scth 			mutex_exit(&(vdevi->devi_lock));
18052155Scth 		}
18062155Scth 	}
18072155Scth }
18082155Scth 
18092155Scth /*
18102155Scth  * Release ndi_devi_enter and wait for possibility of new children, avoiding
18112155Scth  * possibility of missing broadcast before getting to cv_timedwait().
18122155Scth  */
18132155Scth static void
18142155Scth ndi_devi_exit_and_wait(dev_info_t *dip, int circular, clock_t end_time)
18152155Scth {
18162155Scth 	struct dev_info	*devi = DEVI(dip);
18172155Scth 	ASSERT(dip != NULL);
18182155Scth 
18192155Scth 	if (panicstr)
18202155Scth 		return;
18212155Scth 
18222155Scth 	/*
18232155Scth 	 * We are called to wait for of a new child, and new child can
18242155Scth 	 * only be added if circular is zero.
18252155Scth 	 */
18262155Scth 	ASSERT(circular == 0);
18272155Scth 
18282155Scth 	/* like ndi_devi_exit with circular of zero */
18292155Scth 	mutex_enter(&(devi->devi_lock));
18302155Scth 	devi->devi_flags &= ~DEVI_BUSY;
18312155Scth 	ASSERT(devi->devi_busy_thread == curthread);
18322155Scth 	devi->devi_busy_thread = NULL;
18332155Scth 	cv_broadcast(&(devi->devi_cv));
18342155Scth 
18352155Scth 	/* now wait for new children while still holding devi_lock */
18362155Scth 	(void) cv_timedwait(&devi->devi_cv, &(devi->devi_lock), end_time);
18372155Scth 	mutex_exit(&(devi->devi_lock));
18380Sstevel@tonic-gate }
18390Sstevel@tonic-gate 
18400Sstevel@tonic-gate /*
18410Sstevel@tonic-gate  * Attempt to single thread entry into devinfo node for modifying its children.
18420Sstevel@tonic-gate  */
18430Sstevel@tonic-gate int
18440Sstevel@tonic-gate ndi_devi_tryenter(dev_info_t *dip, int *circular)
18450Sstevel@tonic-gate {
18460Sstevel@tonic-gate 	int rval = 1;		   /* assume we enter */
18470Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
18480Sstevel@tonic-gate 	ASSERT(dip != NULL);
18490Sstevel@tonic-gate 
18500Sstevel@tonic-gate 	mutex_enter(&devi->devi_lock);
18510Sstevel@tonic-gate 	if (devi->devi_busy_thread == (void *)curthread) {
18520Sstevel@tonic-gate 		devi->devi_circular++;
18530Sstevel@tonic-gate 	} else {
18540Sstevel@tonic-gate 		if (!DEVI_BUSY_CHANGING(devi)) {
18550Sstevel@tonic-gate 			devi->devi_flags |= DEVI_BUSY;
18560Sstevel@tonic-gate 			devi->devi_busy_thread = (void *)curthread;
18570Sstevel@tonic-gate 		} else {
18580Sstevel@tonic-gate 			rval = 0;	/* devi is busy */
18590Sstevel@tonic-gate 		}
18600Sstevel@tonic-gate 	}
18610Sstevel@tonic-gate 	*circular = devi->devi_circular;
18620Sstevel@tonic-gate 	mutex_exit(&devi->devi_lock);
18630Sstevel@tonic-gate 	return (rval);
18640Sstevel@tonic-gate }
18650Sstevel@tonic-gate 
18660Sstevel@tonic-gate /*
18670Sstevel@tonic-gate  * Allocate and initialize a new dev_info structure.
18680Sstevel@tonic-gate  *
18690Sstevel@tonic-gate  * This routine may be called at interrupt time by a nexus in
18700Sstevel@tonic-gate  * response to a hotplug event, therefore memory allocations are
18710Sstevel@tonic-gate  * not allowed to sleep.
18720Sstevel@tonic-gate  */
18730Sstevel@tonic-gate int
1874789Sahrens ndi_devi_alloc(dev_info_t *parent, char *node_name, pnode_t nodeid,
18750Sstevel@tonic-gate     dev_info_t **ret_dip)
18760Sstevel@tonic-gate {
18770Sstevel@tonic-gate 	ASSERT(node_name != NULL);
18780Sstevel@tonic-gate 	ASSERT(ret_dip != NULL);
18790Sstevel@tonic-gate 
18800Sstevel@tonic-gate 	*ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL,
18810Sstevel@tonic-gate 	    KM_NOSLEEP);
18820Sstevel@tonic-gate 	if (*ret_dip == NULL) {
18830Sstevel@tonic-gate 		return (NDI_NOMEM);
18840Sstevel@tonic-gate 	}
18850Sstevel@tonic-gate 
18860Sstevel@tonic-gate 	return (NDI_SUCCESS);
18870Sstevel@tonic-gate }
18880Sstevel@tonic-gate 
18890Sstevel@tonic-gate /*
18900Sstevel@tonic-gate  * Allocate and initialize a new dev_info structure
18910Sstevel@tonic-gate  * This routine may sleep and should not be called at interrupt time
18920Sstevel@tonic-gate  */
18930Sstevel@tonic-gate void
1894789Sahrens ndi_devi_alloc_sleep(dev_info_t *parent, char *node_name, pnode_t nodeid,
18950Sstevel@tonic-gate     dev_info_t **ret_dip)
18960Sstevel@tonic-gate {
18970Sstevel@tonic-gate 	ASSERT(node_name != NULL);
18980Sstevel@tonic-gate 	ASSERT(ret_dip != NULL);
18990Sstevel@tonic-gate 
19000Sstevel@tonic-gate 	*ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL,
19010Sstevel@tonic-gate 	    KM_SLEEP);
19020Sstevel@tonic-gate 	ASSERT(*ret_dip);
19030Sstevel@tonic-gate }
19040Sstevel@tonic-gate 
19050Sstevel@tonic-gate /*
19060Sstevel@tonic-gate  * Remove an initialized (but not yet attached) dev_info
19070Sstevel@tonic-gate  * node from it's parent.
19080Sstevel@tonic-gate  */
19090Sstevel@tonic-gate int
19100Sstevel@tonic-gate ndi_devi_free(dev_info_t *dip)
19110Sstevel@tonic-gate {
19120Sstevel@tonic-gate 	ASSERT(dip != NULL);
19130Sstevel@tonic-gate 
19140Sstevel@tonic-gate 	if (i_ddi_node_state(dip) >= DS_INITIALIZED)
19150Sstevel@tonic-gate 		return (DDI_FAILURE);
19160Sstevel@tonic-gate 
19170Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_free: %s%d (%p)\n",
19180Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
19190Sstevel@tonic-gate 
19200Sstevel@tonic-gate 	(void) ddi_remove_child(dip, 0);
19210Sstevel@tonic-gate 
19220Sstevel@tonic-gate 	return (NDI_SUCCESS);
19230Sstevel@tonic-gate }
19240Sstevel@tonic-gate 
19250Sstevel@tonic-gate /*
19260Sstevel@tonic-gate  * ndi_devi_bind_driver() binds a driver to a given device. If it fails
19270Sstevel@tonic-gate  * to bind the driver, it returns an appropriate error back. Some drivers
19280Sstevel@tonic-gate  * may want to know if the actually failed to bind.
19290Sstevel@tonic-gate  */
19300Sstevel@tonic-gate int
19310Sstevel@tonic-gate ndi_devi_bind_driver(dev_info_t *dip, uint_t flags)
19320Sstevel@tonic-gate {
19330Sstevel@tonic-gate 	int ret = NDI_FAILURE;
19340Sstevel@tonic-gate 	int circ;
19350Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
19360Sstevel@tonic-gate 	ASSERT(pdip);
19370Sstevel@tonic-gate 
19380Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
19390Sstevel@tonic-gate 	    "ndi_devi_bind_driver: %s%d (%p) flags: %x\n",
19400Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
19410Sstevel@tonic-gate 
19420Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
19430Sstevel@tonic-gate 	if (i_ndi_config_node(dip, DS_BOUND, flags) == DDI_SUCCESS)
19440Sstevel@tonic-gate 		ret = NDI_SUCCESS;
19450Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
19460Sstevel@tonic-gate 
19470Sstevel@tonic-gate 	return (ret);
19480Sstevel@tonic-gate }
19490Sstevel@tonic-gate 
19500Sstevel@tonic-gate /*
19510Sstevel@tonic-gate  * ndi_devi_unbind_driver: unbind the dip
19520Sstevel@tonic-gate  */
19530Sstevel@tonic-gate static int
19540Sstevel@tonic-gate ndi_devi_unbind_driver(dev_info_t *dip)
19550Sstevel@tonic-gate {
19560Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
19570Sstevel@tonic-gate 
19580Sstevel@tonic-gate 	return (i_ndi_unconfig_node(dip, DS_LINKED, 0));
19590Sstevel@tonic-gate }
19600Sstevel@tonic-gate 
19610Sstevel@tonic-gate /*
19620Sstevel@tonic-gate  * Misc. help routines called by framework only
19630Sstevel@tonic-gate  */
19640Sstevel@tonic-gate 
19650Sstevel@tonic-gate /*
19660Sstevel@tonic-gate  * Get the state of node
19670Sstevel@tonic-gate  */
19680Sstevel@tonic-gate ddi_node_state_t
19690Sstevel@tonic-gate i_ddi_node_state(dev_info_t *dip)
19700Sstevel@tonic-gate {
19710Sstevel@tonic-gate 	return (DEVI(dip)->devi_node_state);
19720Sstevel@tonic-gate }
19730Sstevel@tonic-gate 
19740Sstevel@tonic-gate /*
19750Sstevel@tonic-gate  * Set the state of node
19760Sstevel@tonic-gate  */
19770Sstevel@tonic-gate void
19780Sstevel@tonic-gate i_ddi_set_node_state(dev_info_t *dip, ddi_node_state_t state)
19790Sstevel@tonic-gate {
19800Sstevel@tonic-gate 	DEVI(dip)->devi_node_state = state;
19810Sstevel@tonic-gate 	membar_enter();			/* make sure stores are flushed */
19820Sstevel@tonic-gate }
19830Sstevel@tonic-gate 
19840Sstevel@tonic-gate /*
19851333Scth  * Determine if node is attached. The implementation accommodates transient
19861333Scth  * DS_READY->DS_ATTACHED->DS_READY state changes.  Outside this file, this
19871333Scth  * function should be instead of i_ddi_node_state() DS_ATTACHED/DS_READY
19881333Scth  * state checks.
19891333Scth  */
19901333Scth int
19911333Scth i_ddi_devi_attached(dev_info_t *dip)
19921333Scth {
19931333Scth 	return (DEVI(dip)->devi_node_state >= DS_ATTACHED);
19941333Scth }
19951333Scth 
19961333Scth /*
19970Sstevel@tonic-gate  * Common function for finding a node in a sibling list given name and addr.
19980Sstevel@tonic-gate  *
19990Sstevel@tonic-gate  * By default, name is matched with devi_node_name. The following
20000Sstevel@tonic-gate  * alternative match strategies are supported:
20010Sstevel@tonic-gate  *
2002*4145Scth  *	FIND_NODE_BY_NODENAME: Match on node name - typical use.
2003*4145Scth  *	FIND_NODE_BY_DRIVER: A match on driver name bound to node is conducted.
20040Sstevel@tonic-gate  *		This support is used for support of OBP generic names and
2005*4145Scth  *		for the conversion from driver names to generic names. When
20060Sstevel@tonic-gate  *		more consistency in the generic name environment is achieved
20070Sstevel@tonic-gate  *		(and not needed for upgrade) this support can be removed.
2008*4145Scth  *	FIND_NODE_BY_ADDR: Match on just the addr.
2009*4145Scth  *		This support is only used/needed during boot to match
2010*4145Scth  *		a node bound via a path-based driver alias.
20110Sstevel@tonic-gate  *
20120Sstevel@tonic-gate  * If a child is not named (dev_addr == NULL), there are three
20130Sstevel@tonic-gate  * possible actions:
20140Sstevel@tonic-gate  *
20150Sstevel@tonic-gate  *	(1) skip it
20160Sstevel@tonic-gate  *	(2) FIND_ADDR_BY_INIT: bring child to DS_INITIALIZED state
20170Sstevel@tonic-gate  *	(3) FIND_ADDR_BY_CALLBACK: use a caller-supplied callback function
20180Sstevel@tonic-gate  */
2019*4145Scth #define	FIND_NODE_BY_NODENAME	0x01
2020*4145Scth #define	FIND_NODE_BY_DRIVER	0x02
2021*4145Scth #define	FIND_NODE_BY_ADDR	0x04
20220Sstevel@tonic-gate #define	FIND_ADDR_BY_INIT	0x10
20230Sstevel@tonic-gate #define	FIND_ADDR_BY_CALLBACK	0x20
20240Sstevel@tonic-gate 
20250Sstevel@tonic-gate static dev_info_t *
20260Sstevel@tonic-gate find_sibling(dev_info_t *head, char *cname, char *caddr, uint_t flag,
20270Sstevel@tonic-gate     int (*callback)(dev_info_t *, char *, int))
20280Sstevel@tonic-gate {
20290Sstevel@tonic-gate 	dev_info_t	*dip;
20300Sstevel@tonic-gate 	char		*addr, *buf;
20310Sstevel@tonic-gate 	major_t		major;
2032*4145Scth 	uint_t		by;
2033*4145Scth 
2034*4145Scth 	/* only one way to find a node */
2035*4145Scth 	by = flag &
2036*4145Scth 	    (FIND_NODE_BY_DRIVER | FIND_NODE_BY_NODENAME | FIND_NODE_BY_ADDR);
2037*4145Scth 	ASSERT(by && BIT_ONLYONESET(by));
20380Sstevel@tonic-gate 
20390Sstevel@tonic-gate 	/* only one way to name a node */
20400Sstevel@tonic-gate 	ASSERT(((flag & FIND_ADDR_BY_INIT) == 0) ||
20410Sstevel@tonic-gate 	    ((flag & FIND_ADDR_BY_CALLBACK) == 0));
20420Sstevel@tonic-gate 
2043*4145Scth 	if (by == FIND_NODE_BY_DRIVER) {
20440Sstevel@tonic-gate 		major = ddi_name_to_major(cname);
20450Sstevel@tonic-gate 		if (major == (major_t)-1)
20460Sstevel@tonic-gate 			return (NULL);
20470Sstevel@tonic-gate 	}
20480Sstevel@tonic-gate 
20490Sstevel@tonic-gate 	/* preallocate buffer of naming node by callback */
20500Sstevel@tonic-gate 	if (flag & FIND_ADDR_BY_CALLBACK)
20510Sstevel@tonic-gate 		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
20520Sstevel@tonic-gate 
20530Sstevel@tonic-gate 	/*
20540Sstevel@tonic-gate 	 * Walk the child list to find a match
20550Sstevel@tonic-gate 	 */
20560Sstevel@tonic-gate 
20570Sstevel@tonic-gate 	for (dip = head; dip; dip = ddi_get_next_sibling(dip)) {
2058*4145Scth 		if (by == FIND_NODE_BY_NODENAME) {
2059*4145Scth 			/* match node name */
2060*4145Scth 			if (strcmp(cname, DEVI(dip)->devi_node_name) != 0)
2061*4145Scth 				continue;
2062*4145Scth 		} else if (by == FIND_NODE_BY_DRIVER) {
20630Sstevel@tonic-gate 			/* match driver major */
20640Sstevel@tonic-gate 			if (DEVI(dip)->devi_major != major)
20650Sstevel@tonic-gate 				continue;
20660Sstevel@tonic-gate 		}
20670Sstevel@tonic-gate 
20680Sstevel@tonic-gate 		if ((addr = DEVI(dip)->devi_addr) == NULL) {
20690Sstevel@tonic-gate 			/* name the child based on the flag */
20700Sstevel@tonic-gate 			if (flag & FIND_ADDR_BY_INIT) {
20710Sstevel@tonic-gate 				if (ddi_initchild(ddi_get_parent(dip), dip)
20720Sstevel@tonic-gate 				    != DDI_SUCCESS)
20730Sstevel@tonic-gate 					continue;
20740Sstevel@tonic-gate 				addr = DEVI(dip)->devi_addr;
20750Sstevel@tonic-gate 			} else if (flag & FIND_ADDR_BY_CALLBACK) {
20760Sstevel@tonic-gate 				if ((callback == NULL) || (callback(
20770Sstevel@tonic-gate 				    dip, buf, MAXNAMELEN) != DDI_SUCCESS))
20780Sstevel@tonic-gate 					continue;
20790Sstevel@tonic-gate 				addr = buf;
20800Sstevel@tonic-gate 			} else {
20810Sstevel@tonic-gate 				continue;	/* skip */
20820Sstevel@tonic-gate 			}
20830Sstevel@tonic-gate 		}
20840Sstevel@tonic-gate 
20850Sstevel@tonic-gate 		/* match addr */
20860Sstevel@tonic-gate 		ASSERT(addr != NULL);
20870Sstevel@tonic-gate 		if (strcmp(caddr, addr) == 0)
20880Sstevel@tonic-gate 			break;	/* node found */
20890Sstevel@tonic-gate 
20900Sstevel@tonic-gate 	}
20910Sstevel@tonic-gate 	if (flag & FIND_ADDR_BY_CALLBACK)
20920Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
20930Sstevel@tonic-gate 	return (dip);
20940Sstevel@tonic-gate }
20950Sstevel@tonic-gate 
20960Sstevel@tonic-gate /*
20970Sstevel@tonic-gate  * Find child of pdip with name: cname@caddr
20980Sstevel@tonic-gate  * Called by init_node() to look for duplicate nodes
20990Sstevel@tonic-gate  */
21000Sstevel@tonic-gate static dev_info_t *
21010Sstevel@tonic-gate find_duplicate_child(dev_info_t *pdip, dev_info_t *dip)
21020Sstevel@tonic-gate {
21030Sstevel@tonic-gate 	dev_info_t *dup;
21040Sstevel@tonic-gate 	char *cname = DEVI(dip)->devi_node_name;
21050Sstevel@tonic-gate 	char *caddr = DEVI(dip)->devi_addr;
21060Sstevel@tonic-gate 
21070Sstevel@tonic-gate 	/* search nodes before dip */
2108*4145Scth 	dup = find_sibling(ddi_get_child(pdip), cname, caddr,
2109*4145Scth 	    FIND_NODE_BY_NODENAME, NULL);
21100Sstevel@tonic-gate 	if (dup != dip)
21110Sstevel@tonic-gate 		return (dup);
21120Sstevel@tonic-gate 
21130Sstevel@tonic-gate 	/*
21140Sstevel@tonic-gate 	 * search nodes after dip; normally this is not needed,
21150Sstevel@tonic-gate 	 */
21160Sstevel@tonic-gate 	return (find_sibling(ddi_get_next_sibling(dip), cname, caddr,
2117*4145Scth 	    FIND_NODE_BY_NODENAME, NULL));
21180Sstevel@tonic-gate }
21190Sstevel@tonic-gate 
21200Sstevel@tonic-gate /*
21210Sstevel@tonic-gate  * Find a child of a given name and address, using a callback to name
21220Sstevel@tonic-gate  * unnamed children. cname is the binding name.
21230Sstevel@tonic-gate  */
21240Sstevel@tonic-gate static dev_info_t *
21250Sstevel@tonic-gate find_child_by_callback(dev_info_t *pdip, char *cname, char *caddr,
21260Sstevel@tonic-gate     int (*name_node)(dev_info_t *, char *, int))
21270Sstevel@tonic-gate {
21280Sstevel@tonic-gate 	return (find_sibling(ddi_get_child(pdip), cname, caddr,
2129*4145Scth 	    FIND_NODE_BY_DRIVER|FIND_ADDR_BY_CALLBACK, name_node));
21300Sstevel@tonic-gate }
21310Sstevel@tonic-gate 
21320Sstevel@tonic-gate /*
21330Sstevel@tonic-gate  * Find a child of a given name and address, invoking initchild to name
21340Sstevel@tonic-gate  * unnamed children. cname is the node name.
21350Sstevel@tonic-gate  */
21360Sstevel@tonic-gate static dev_info_t *
21370Sstevel@tonic-gate find_child_by_name(dev_info_t *pdip, char *cname, char *caddr)
21380Sstevel@tonic-gate {
21390Sstevel@tonic-gate 	dev_info_t	*dip;
21400Sstevel@tonic-gate 
2141*4145Scth 	/* attempt search without changing state of preceding siblings */
2142*4145Scth 	dip = find_sibling(ddi_get_child(pdip), cname, caddr,
2143*4145Scth 	    FIND_NODE_BY_NODENAME, NULL);
21440Sstevel@tonic-gate 	if (dip)
21450Sstevel@tonic-gate 		return (dip);
21460Sstevel@tonic-gate 
21470Sstevel@tonic-gate 	return (find_sibling(ddi_get_child(pdip), cname, caddr,
2148*4145Scth 	    FIND_NODE_BY_NODENAME|FIND_ADDR_BY_INIT, NULL));
21490Sstevel@tonic-gate }
21500Sstevel@tonic-gate 
21510Sstevel@tonic-gate /*
21520Sstevel@tonic-gate  * Find a child of a given name and address, invoking initchild to name
21530Sstevel@tonic-gate  * unnamed children. cname is the node name.
21540Sstevel@tonic-gate  */
21550Sstevel@tonic-gate static dev_info_t *
21560Sstevel@tonic-gate find_child_by_driver(dev_info_t *pdip, char *cname, char *caddr)
21570Sstevel@tonic-gate {
21580Sstevel@tonic-gate 	dev_info_t	*dip;
21590Sstevel@tonic-gate 
2160*4145Scth 	/* attempt search without changing state of preceding siblings */
21610Sstevel@tonic-gate 	dip = find_sibling(ddi_get_child(pdip), cname, caddr,
2162*4145Scth 	    FIND_NODE_BY_DRIVER, NULL);
21630Sstevel@tonic-gate 	if (dip)
21640Sstevel@tonic-gate 		return (dip);
21650Sstevel@tonic-gate 
21660Sstevel@tonic-gate 	return (find_sibling(ddi_get_child(pdip), cname, caddr,
2167*4145Scth 	    FIND_NODE_BY_DRIVER|FIND_ADDR_BY_INIT, NULL));
2168*4145Scth }
2169*4145Scth 
2170*4145Scth /*
2171*4145Scth  * Find a child of a given address, invoking initchild to name
2172*4145Scth  * unnamed children. cname is the node name.
2173*4145Scth  *
2174*4145Scth  * NOTE: This function is only used during boot. One would hope that
2175*4145Scth  * unique sibling unit-addresses on hardware branches of the tree would
2176*4145Scth  * be a requirement to avoid two drivers trying to control the same
2177*4145Scth  * piece of hardware. Unfortunately there are some cases where this
2178*4145Scth  * situation exists (/ssm@0,0/pci@1c,700000 /ssm@0,0/sghsc@1c,700000).
2179*4145Scth  * Until unit-address uniqueness of siblings is guaranteed, use of this
2180*4145Scth  * interface for purposes other than boot should be avoided.
2181*4145Scth  */
2182*4145Scth static dev_info_t *
2183*4145Scth find_child_by_addr(dev_info_t *pdip, char *caddr)
2184*4145Scth {
2185*4145Scth 	dev_info_t	*dip;
2186*4145Scth 
2187*4145Scth 	/* attempt search without changing state of preceding siblings */
2188*4145Scth 	dip = find_sibling(ddi_get_child(pdip), NULL, caddr,
2189*4145Scth 	    FIND_NODE_BY_ADDR, NULL);
2190*4145Scth 	if (dip)
2191*4145Scth 		return (dip);
2192*4145Scth 
2193*4145Scth 	return (find_sibling(ddi_get_child(pdip), NULL, caddr,
2194*4145Scth 	    FIND_NODE_BY_ADDR|FIND_ADDR_BY_INIT, NULL));
21950Sstevel@tonic-gate }
21960Sstevel@tonic-gate 
21970Sstevel@tonic-gate /*
21980Sstevel@tonic-gate  * Deleting a property list. Take care, since some property structures
21990Sstevel@tonic-gate  * may not be fully built.
22000Sstevel@tonic-gate  */
22010Sstevel@tonic-gate void
22020Sstevel@tonic-gate i_ddi_prop_list_delete(ddi_prop_t *prop)
22030Sstevel@tonic-gate {
22040Sstevel@tonic-gate 	while (prop) {
22050Sstevel@tonic-gate 		ddi_prop_t *next = prop->prop_next;
22060Sstevel@tonic-gate 		if (prop->prop_name)
22070Sstevel@tonic-gate 			kmem_free(prop->prop_name, strlen(prop->prop_name) + 1);
22080Sstevel@tonic-gate 		if ((prop->prop_len != 0) && prop->prop_val)
22090Sstevel@tonic-gate 			kmem_free(prop->prop_val, prop->prop_len);
22100Sstevel@tonic-gate 		kmem_free(prop, sizeof (struct ddi_prop));
22110Sstevel@tonic-gate 		prop = next;
22120Sstevel@tonic-gate 	}
22130Sstevel@tonic-gate }
22140Sstevel@tonic-gate 
22150Sstevel@tonic-gate /*
22160Sstevel@tonic-gate  * Duplicate property list
22170Sstevel@tonic-gate  */
22180Sstevel@tonic-gate ddi_prop_t *
22190Sstevel@tonic-gate i_ddi_prop_list_dup(ddi_prop_t *prop, uint_t flag)
22200Sstevel@tonic-gate {
22210Sstevel@tonic-gate 	ddi_prop_t *result, *prev, *copy;
22220Sstevel@tonic-gate 
22230Sstevel@tonic-gate 	if (prop == NULL)
22240Sstevel@tonic-gate 		return (NULL);
22250Sstevel@tonic-gate 
22260Sstevel@tonic-gate 	result = prev = NULL;
22270Sstevel@tonic-gate 	for (; prop != NULL; prop = prop->prop_next) {
22280Sstevel@tonic-gate 		ASSERT(prop->prop_name != NULL);
22290Sstevel@tonic-gate 		copy = kmem_zalloc(sizeof (struct ddi_prop), flag);
22300Sstevel@tonic-gate 		if (copy == NULL)
22310Sstevel@tonic-gate 			goto fail;
22320Sstevel@tonic-gate 
22330Sstevel@tonic-gate 		copy->prop_dev = prop->prop_dev;
22340Sstevel@tonic-gate 		copy->prop_flags = prop->prop_flags;
22350Sstevel@tonic-gate 		copy->prop_name = i_ddi_strdup(prop->prop_name, flag);
22360Sstevel@tonic-gate 		if (copy->prop_name == NULL)
22370Sstevel@tonic-gate 			goto fail;
22380Sstevel@tonic-gate 
22390Sstevel@tonic-gate 		if ((copy->prop_len = prop->prop_len) != 0) {
22400Sstevel@tonic-gate 			copy->prop_val = kmem_zalloc(prop->prop_len, flag);
22410Sstevel@tonic-gate 			if (copy->prop_val == NULL)
22420Sstevel@tonic-gate 				goto fail;
22430Sstevel@tonic-gate 
22440Sstevel@tonic-gate 			bcopy(prop->prop_val, copy->prop_val, prop->prop_len);
22450Sstevel@tonic-gate 		}
22460Sstevel@tonic-gate 
22470Sstevel@tonic-gate 		if (prev == NULL)
22480Sstevel@tonic-gate 			result = prev = copy;
22490Sstevel@tonic-gate 		else
22500Sstevel@tonic-gate 			prev->prop_next = copy;
22510Sstevel@tonic-gate 		prev = copy;
22520Sstevel@tonic-gate 	}
22530Sstevel@tonic-gate 	return (result);
22540Sstevel@tonic-gate 
22550Sstevel@tonic-gate fail:
22560Sstevel@tonic-gate 	i_ddi_prop_list_delete(result);
22570Sstevel@tonic-gate 	return (NULL);
22580Sstevel@tonic-gate }
22590Sstevel@tonic-gate 
22600Sstevel@tonic-gate /*
22610Sstevel@tonic-gate  * Create a reference property list, currently used only for
22620Sstevel@tonic-gate  * driver global properties. Created with ref count of 1.
22630Sstevel@tonic-gate  */
22640Sstevel@tonic-gate ddi_prop_list_t *
22650Sstevel@tonic-gate i_ddi_prop_list_create(ddi_prop_t *props)
22660Sstevel@tonic-gate {
22670Sstevel@tonic-gate 	ddi_prop_list_t *list = kmem_alloc(sizeof (*list), KM_SLEEP);
22680Sstevel@tonic-gate 	list->prop_list = props;
22690Sstevel@tonic-gate 	list->prop_ref = 1;
22700Sstevel@tonic-gate 	return (list);
22710Sstevel@tonic-gate }
22720Sstevel@tonic-gate 
22730Sstevel@tonic-gate /*
22740Sstevel@tonic-gate  * Increment/decrement reference count. The reference is
22750Sstevel@tonic-gate  * protected by dn_lock. The only interfaces modifying
22760Sstevel@tonic-gate  * dn_global_prop_ptr is in impl_make[free]_parlist().
22770Sstevel@tonic-gate  */
22780Sstevel@tonic-gate void
22790Sstevel@tonic-gate i_ddi_prop_list_hold(ddi_prop_list_t *prop_list, struct devnames *dnp)
22800Sstevel@tonic-gate {
22810Sstevel@tonic-gate 	ASSERT(prop_list->prop_ref >= 0);
22820Sstevel@tonic-gate 	ASSERT(mutex_owned(&dnp->dn_lock));
22830Sstevel@tonic-gate 	prop_list->prop_ref++;
22840Sstevel@tonic-gate }
22850Sstevel@tonic-gate 
22860Sstevel@tonic-gate void
22870Sstevel@tonic-gate i_ddi_prop_list_rele(ddi_prop_list_t *prop_list, struct devnames *dnp)
22880Sstevel@tonic-gate {
22890Sstevel@tonic-gate 	ASSERT(prop_list->prop_ref > 0);
22900Sstevel@tonic-gate 	ASSERT(mutex_owned(&dnp->dn_lock));
22910Sstevel@tonic-gate 	prop_list->prop_ref--;
22920Sstevel@tonic-gate 
22930Sstevel@tonic-gate 	if (prop_list->prop_ref == 0) {
22940Sstevel@tonic-gate 		i_ddi_prop_list_delete(prop_list->prop_list);
22950Sstevel@tonic-gate 		kmem_free(prop_list, sizeof (*prop_list));
22960Sstevel@tonic-gate 	}
22970Sstevel@tonic-gate }
22980Sstevel@tonic-gate 
22990Sstevel@tonic-gate /*
23000Sstevel@tonic-gate  * Free table of classes by drivers
23010Sstevel@tonic-gate  */
23020Sstevel@tonic-gate void
23030Sstevel@tonic-gate i_ddi_free_exported_classes(char **classes, int n)
23040Sstevel@tonic-gate {
23050Sstevel@tonic-gate 	if ((n == 0) || (classes == NULL))
23060Sstevel@tonic-gate 		return;
23070Sstevel@tonic-gate 
23080Sstevel@tonic-gate 	kmem_free(classes, n * sizeof (char *));
23090Sstevel@tonic-gate }
23100Sstevel@tonic-gate 
23110Sstevel@tonic-gate /*
23120Sstevel@tonic-gate  * Get all classes exported by dip
23130Sstevel@tonic-gate  */
23140Sstevel@tonic-gate int
23150Sstevel@tonic-gate i_ddi_get_exported_classes(dev_info_t *dip, char ***classes)
23160Sstevel@tonic-gate {
23170Sstevel@tonic-gate 	extern void lock_hw_class_list();
23180Sstevel@tonic-gate 	extern void unlock_hw_class_list();
23190Sstevel@tonic-gate 	extern int get_class(const char *, char **);
23200Sstevel@tonic-gate 
23210Sstevel@tonic-gate 	static char *rootclass = "root";
23220Sstevel@tonic-gate 	int n = 0, nclass = 0;
23230Sstevel@tonic-gate 	char **buf;
23240Sstevel@tonic-gate 
23250Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) >= DS_BOUND);
23260Sstevel@tonic-gate 
23270Sstevel@tonic-gate 	if (dip == ddi_root_node())	/* rootnode exports class "root" */
23280Sstevel@tonic-gate 		nclass = 1;
23290Sstevel@tonic-gate 	lock_hw_class_list();
23300Sstevel@tonic-gate 	nclass += get_class(ddi_driver_name(dip), NULL);
23310Sstevel@tonic-gate 	if (nclass == 0) {
23320Sstevel@tonic-gate 		unlock_hw_class_list();
23330Sstevel@tonic-gate 		return (0);		/* no class exported */
23340Sstevel@tonic-gate 	}
23350Sstevel@tonic-gate 
23360Sstevel@tonic-gate 	*classes = buf = kmem_alloc(nclass * sizeof (char *), KM_SLEEP);
23370Sstevel@tonic-gate 	if (dip == ddi_root_node()) {
23380Sstevel@tonic-gate 		*buf++ = rootclass;
23390Sstevel@tonic-gate 		n = 1;
23400Sstevel@tonic-gate 	}
23410Sstevel@tonic-gate 	n += get_class(ddi_driver_name(dip), buf);
23420Sstevel@tonic-gate 	unlock_hw_class_list();
23430Sstevel@tonic-gate 
23440Sstevel@tonic-gate 	ASSERT(n == nclass);    /* make sure buf wasn't overrun */
23450Sstevel@tonic-gate 	return (nclass);
23460Sstevel@tonic-gate }
23470Sstevel@tonic-gate 
23480Sstevel@tonic-gate /*
23490Sstevel@tonic-gate  * Helper functions, returns NULL if no memory.
23500Sstevel@tonic-gate  */
23510Sstevel@tonic-gate char *
23520Sstevel@tonic-gate i_ddi_strdup(char *str, uint_t flag)
23530Sstevel@tonic-gate {
23540Sstevel@tonic-gate 	char *copy;
23550Sstevel@tonic-gate 
23560Sstevel@tonic-gate 	if (str == NULL)
23570Sstevel@tonic-gate 		return (NULL);
23580Sstevel@tonic-gate 
23590Sstevel@tonic-gate 	copy = kmem_alloc(strlen(str) + 1, flag);
23600Sstevel@tonic-gate 	if (copy == NULL)
23610Sstevel@tonic-gate 		return (NULL);
23620Sstevel@tonic-gate 
23630Sstevel@tonic-gate 	(void) strcpy(copy, str);
23640Sstevel@tonic-gate 	return (copy);
23650Sstevel@tonic-gate }
23660Sstevel@tonic-gate 
23670Sstevel@tonic-gate /*
23680Sstevel@tonic-gate  * Load driver.conf file for major. Load all if major == -1.
23690Sstevel@tonic-gate  *
23700Sstevel@tonic-gate  * This is called
23710Sstevel@tonic-gate  * - early in boot after devnames array is initialized
23720Sstevel@tonic-gate  * - from vfs code when certain file systems are mounted
23730Sstevel@tonic-gate  * - from add_drv when a new driver is added
23740Sstevel@tonic-gate  */
23750Sstevel@tonic-gate int
23760Sstevel@tonic-gate i_ddi_load_drvconf(major_t major)
23770Sstevel@tonic-gate {
23780Sstevel@tonic-gate 	extern int modrootloaded;
23790Sstevel@tonic-gate 
23800Sstevel@tonic-gate 	major_t low, high, m;
23810Sstevel@tonic-gate 
23820Sstevel@tonic-gate 	if (major == (major_t)-1) {
23830Sstevel@tonic-gate 		low = 0;
23840Sstevel@tonic-gate 		high = devcnt - 1;
23850Sstevel@tonic-gate 	} else {
23860Sstevel@tonic-gate 		if (major >= devcnt)
23870Sstevel@tonic-gate 			return (EINVAL);
23880Sstevel@tonic-gate 		low = high = major;
23890Sstevel@tonic-gate 	}
23900Sstevel@tonic-gate 
23910Sstevel@tonic-gate 	for (m = low; m <= high; m++) {
23920Sstevel@tonic-gate 		struct devnames *dnp = &devnamesp[m];
23930Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
23940Sstevel@tonic-gate 		dnp->dn_flags &= ~DN_DRIVER_HELD;
23950Sstevel@tonic-gate 		(void) impl_make_parlist(m);
23960Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
23970Sstevel@tonic-gate 	}
23980Sstevel@tonic-gate 
23990Sstevel@tonic-gate 	if (modrootloaded) {
24000Sstevel@tonic-gate 		ddi_walk_devs(ddi_root_node(), reset_nexus_flags,
24010Sstevel@tonic-gate 		    (void *)(uintptr_t)major);
24020Sstevel@tonic-gate 	}
24030Sstevel@tonic-gate 
24040Sstevel@tonic-gate 	/* build dn_list from old entries in path_to_inst */
24050Sstevel@tonic-gate 	e_ddi_unorphan_instance_nos();
24060Sstevel@tonic-gate 	return (0);
24070Sstevel@tonic-gate }
24080Sstevel@tonic-gate 
24090Sstevel@tonic-gate /*
24100Sstevel@tonic-gate  * Unload a specific driver.conf.
24110Sstevel@tonic-gate  * Don't support unload all because it doesn't make any sense
24120Sstevel@tonic-gate  */
24130Sstevel@tonic-gate int
24140Sstevel@tonic-gate i_ddi_unload_drvconf(major_t major)
24150Sstevel@tonic-gate {
24160Sstevel@tonic-gate 	int error;
24170Sstevel@tonic-gate 	struct devnames *dnp;
24180Sstevel@tonic-gate 
24190Sstevel@tonic-gate 	if (major >= devcnt)
24200Sstevel@tonic-gate 		return (EINVAL);
24210Sstevel@tonic-gate 
24220Sstevel@tonic-gate 	/*
24230Sstevel@tonic-gate 	 * Take the per-driver lock while unloading driver.conf
24240Sstevel@tonic-gate 	 */
24250Sstevel@tonic-gate 	dnp = &devnamesp[major];
24260Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
24270Sstevel@tonic-gate 	error = impl_free_parlist(major);
24280Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
24290Sstevel@tonic-gate 	return (error);
24300Sstevel@tonic-gate }
24310Sstevel@tonic-gate 
24320Sstevel@tonic-gate /*
24330Sstevel@tonic-gate  * Merge a .conf node. This is called by nexus drivers to augment
24340Sstevel@tonic-gate  * hw node with properties specified in driver.conf file. This function
24350Sstevel@tonic-gate  * takes a callback routine to name nexus children.
24360Sstevel@tonic-gate  * The parent node must be held busy.
24370Sstevel@tonic-gate  *
24380Sstevel@tonic-gate  * It returns DDI_SUCCESS if the node is merged and DDI_FAILURE otherwise.
24390Sstevel@tonic-gate  */
24400Sstevel@tonic-gate int
24410Sstevel@tonic-gate ndi_merge_node(dev_info_t *dip, int (*name_node)(dev_info_t *, char *, int))
24420Sstevel@tonic-gate {
24430Sstevel@tonic-gate 	dev_info_t *hwdip;
24440Sstevel@tonic-gate 
24450Sstevel@tonic-gate 	ASSERT(ndi_dev_is_persistent_node(dip) == 0);
24460Sstevel@tonic-gate 	ASSERT(ddi_get_name_addr(dip) != NULL);
24470Sstevel@tonic-gate 
24480Sstevel@tonic-gate 	hwdip = find_child_by_callback(ddi_get_parent(dip),
24490Sstevel@tonic-gate 	    ddi_binding_name(dip), ddi_get_name_addr(dip), name_node);
24500Sstevel@tonic-gate 
24510Sstevel@tonic-gate 	/*
24520Sstevel@tonic-gate 	 * Look for the hardware node that is the target of the merge;
24530Sstevel@tonic-gate 	 * return failure if not found.
24540Sstevel@tonic-gate 	 */
24550Sstevel@tonic-gate 	if ((hwdip == NULL) || (hwdip == dip)) {
24560Sstevel@tonic-gate 		char *buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
24570Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_WARN, "No HW node to merge conf node %s",
24580Sstevel@tonic-gate 		    ddi_deviname(dip, buf)));
24590Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
24600Sstevel@tonic-gate 		return (DDI_FAILURE);
24610Sstevel@tonic-gate 	}
24620Sstevel@tonic-gate 
24630Sstevel@tonic-gate 	/*
24640Sstevel@tonic-gate 	 * Make sure the hardware node is uninitialized and has no property.
24650Sstevel@tonic-gate 	 * This may not be the case if new .conf files are load after some
24660Sstevel@tonic-gate 	 * hardware nodes have already been initialized and attached.
24670Sstevel@tonic-gate 	 *
24680Sstevel@tonic-gate 	 * N.B. We return success here because the node was *intended*
24690Sstevel@tonic-gate 	 * 	to be a merge node because there is a hw node with the name.
24700Sstevel@tonic-gate 	 */
24710Sstevel@tonic-gate 	mutex_enter(&DEVI(hwdip)->devi_lock);
24720Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(hwdip) == 0) {
24730Sstevel@tonic-gate 		char *buf;
24740Sstevel@tonic-gate 		mutex_exit(&DEVI(hwdip)->devi_lock);
24750Sstevel@tonic-gate 
24760Sstevel@tonic-gate 		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
24770Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE, "Duplicate .conf node %s",
24780Sstevel@tonic-gate 		    ddi_deviname(dip, buf)));
24790Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
24800Sstevel@tonic-gate 		return (DDI_SUCCESS);
24810Sstevel@tonic-gate 	}
24820Sstevel@tonic-gate 
24830Sstevel@tonic-gate 	/*
24840Sstevel@tonic-gate 	 * If it is possible that the hardware has already been touched
24850Sstevel@tonic-gate 	 * then don't merge.
24860Sstevel@tonic-gate 	 */
24870Sstevel@tonic-gate 	if (i_ddi_node_state(hwdip) >= DS_INITIALIZED ||
24880Sstevel@tonic-gate 	    (DEVI(hwdip)->devi_sys_prop_ptr != NULL) ||
24890Sstevel@tonic-gate 	    (DEVI(hwdip)->devi_drv_prop_ptr != NULL)) {
24900Sstevel@tonic-gate 		char *buf;
24910Sstevel@tonic-gate 		mutex_exit(&DEVI(hwdip)->devi_lock);
24920Sstevel@tonic-gate 
24930Sstevel@tonic-gate 		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
24940Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE,
24950Sstevel@tonic-gate 		    "!Cannot merge .conf node %s with hw node %p "
24960Sstevel@tonic-gate 		    "-- not in proper state",
24970Sstevel@tonic-gate 		    ddi_deviname(dip, buf), (void *)hwdip));
24980Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
24990Sstevel@tonic-gate 		return (DDI_SUCCESS);
25000Sstevel@tonic-gate 	}
25010Sstevel@tonic-gate 
25020Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
25030Sstevel@tonic-gate 	DEVI(hwdip)->devi_sys_prop_ptr = DEVI(dip)->devi_sys_prop_ptr;
25040Sstevel@tonic-gate 	DEVI(hwdip)->devi_drv_prop_ptr = DEVI(dip)->devi_drv_prop_ptr;
25050Sstevel@tonic-gate 	DEVI(dip)->devi_sys_prop_ptr = NULL;
25060Sstevel@tonic-gate 	DEVI(dip)->devi_drv_prop_ptr = NULL;
25070Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
25080Sstevel@tonic-gate 	mutex_exit(&DEVI(hwdip)->devi_lock);
25090Sstevel@tonic-gate 
25100Sstevel@tonic-gate 	return (DDI_SUCCESS);
25110Sstevel@tonic-gate }
25120Sstevel@tonic-gate 
25130Sstevel@tonic-gate /*
25140Sstevel@tonic-gate  * Merge a "wildcard" .conf node. This is called by nexus drivers to
25150Sstevel@tonic-gate  * augment a set of hw node with properties specified in driver.conf file.
25160Sstevel@tonic-gate  * The parent node must be held busy.
25170Sstevel@tonic-gate  *
25180Sstevel@tonic-gate  * There is no failure mode, since the nexus may or may not have child
25190Sstevel@tonic-gate  * node bound the driver specified by the wildcard node.
25200Sstevel@tonic-gate  */
25210Sstevel@tonic-gate void
25220Sstevel@tonic-gate ndi_merge_wildcard_node(dev_info_t *dip)
25230Sstevel@tonic-gate {
25240Sstevel@tonic-gate 	dev_info_t *hwdip;
25250Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
25260Sstevel@tonic-gate 	major_t major = ddi_driver_major(dip);
25270Sstevel@tonic-gate 
25280Sstevel@tonic-gate 	/* never attempt to merge a hw node */
25290Sstevel@tonic-gate 	ASSERT(ndi_dev_is_persistent_node(dip) == 0);
25300Sstevel@tonic-gate 	/* must be bound to a driver major number */
25310Sstevel@tonic-gate 	ASSERT(major != (major_t)-1);
25320Sstevel@tonic-gate 
25330Sstevel@tonic-gate 	/*
25340Sstevel@tonic-gate 	 * Walk the child list to find all nodes bound to major
25350Sstevel@tonic-gate 	 * and copy properties.
25360Sstevel@tonic-gate 	 */
25370Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
25380Sstevel@tonic-gate 	for (hwdip = ddi_get_child(pdip); hwdip;
25390Sstevel@tonic-gate 	    hwdip = ddi_get_next_sibling(hwdip)) {
25400Sstevel@tonic-gate 		/*
25410Sstevel@tonic-gate 		 * Skip nodes not bound to same driver
25420Sstevel@tonic-gate 		 */
25430Sstevel@tonic-gate 		if (ddi_driver_major(hwdip) != major)
25440Sstevel@tonic-gate 			continue;
25450Sstevel@tonic-gate 
25460Sstevel@tonic-gate 		/*
25470Sstevel@tonic-gate 		 * Skip .conf nodes
25480Sstevel@tonic-gate 		 */
25490Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(hwdip) == 0)
25500Sstevel@tonic-gate 			continue;
25510Sstevel@tonic-gate 
25520Sstevel@tonic-gate 		/*
25530Sstevel@tonic-gate 		 * Make sure the node is uninitialized and has no property.
25540Sstevel@tonic-gate 		 */
25550Sstevel@tonic-gate 		mutex_enter(&DEVI(hwdip)->devi_lock);
25560Sstevel@tonic-gate 		if (i_ddi_node_state(hwdip) >= DS_INITIALIZED ||
25570Sstevel@tonic-gate 		    (DEVI(hwdip)->devi_sys_prop_ptr != NULL) ||
25580Sstevel@tonic-gate 		    (DEVI(hwdip)->devi_drv_prop_ptr != NULL)) {
25590Sstevel@tonic-gate 			mutex_exit(&DEVI(hwdip)->devi_lock);
25600Sstevel@tonic-gate 			NDI_CONFIG_DEBUG((CE_NOTE, "HW node %p state not "
25610Sstevel@tonic-gate 			    "suitable for merging wildcard conf node %s",
25620Sstevel@tonic-gate 			    (void *)hwdip, ddi_node_name(dip)));
25630Sstevel@tonic-gate 			continue;
25640Sstevel@tonic-gate 		}
25650Sstevel@tonic-gate 
25660Sstevel@tonic-gate 		DEVI(hwdip)->devi_sys_prop_ptr =
25670Sstevel@tonic-gate 		    i_ddi_prop_list_dup(DEVI(dip)->devi_sys_prop_ptr, KM_SLEEP);
25680Sstevel@tonic-gate 		DEVI(hwdip)->devi_drv_prop_ptr =
25690Sstevel@tonic-gate 		    i_ddi_prop_list_dup(DEVI(dip)->devi_drv_prop_ptr, KM_SLEEP);
25700Sstevel@tonic-gate 		mutex_exit(&DEVI(hwdip)->devi_lock);
25710Sstevel@tonic-gate 	}
25720Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
25730Sstevel@tonic-gate }
25740Sstevel@tonic-gate 
25750Sstevel@tonic-gate /*
25760Sstevel@tonic-gate  * Return the major number based on the compatible property. This interface
25770Sstevel@tonic-gate  * may be used in situations where we are trying to detect if a better driver
25780Sstevel@tonic-gate  * now exists for a device, so it must use the 'compatible' property.  If
25790Sstevel@tonic-gate  * a non-NULL formp is specified and the binding was based on compatible then
25800Sstevel@tonic-gate  * return the pointer to the form used in *formp.
25810Sstevel@tonic-gate  */
25820Sstevel@tonic-gate major_t
25830Sstevel@tonic-gate ddi_compatible_driver_major(dev_info_t *dip, char **formp)
25840Sstevel@tonic-gate {
25850Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
25860Sstevel@tonic-gate 	void		*compat;
25870Sstevel@tonic-gate 	size_t		len;
25880Sstevel@tonic-gate 	char		*p = NULL;
25890Sstevel@tonic-gate 	major_t		major = (major_t)-1;
25900Sstevel@tonic-gate 
25910Sstevel@tonic-gate 	if (formp)
25920Sstevel@tonic-gate 		*formp = NULL;
25930Sstevel@tonic-gate 
2594*4145Scth 	/*
2595*4145Scth 	 * Highest precedence binding is a path-oriented alias. Since this
2596*4145Scth 	 * requires a 'path', this type of binding occurs via more obtuse
2597*4145Scth 	 * 'rebind'. The need for a path-oriented alias 'rebind' is detected
2598*4145Scth 	 * after a successful DDI_CTLOPS_INITCHILD to another driver: this is
2599*4145Scth 	 * is the first point at which the unit-address (or instance) of the
2600*4145Scth 	 * last component of the path is available (even though the path is
2601*4145Scth 	 * bound to the wrong driver at this point).
2602*4145Scth 	 */
2603*4145Scth 	if (devi->devi_flags & DEVI_REBIND) {
2604*4145Scth 		p = devi->devi_rebinding_name;
2605*4145Scth 		major = ddi_name_to_major(p);
2606*4145Scth 		if ((major != (major_t)-1) &&
2607*4145Scth 		    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED)) {
2608*4145Scth 			if (formp)
2609*4145Scth 				*formp = p;
2610*4145Scth 			return (major);
2611*4145Scth 		}
2612*4145Scth 
2613*4145Scth 		/*
2614*4145Scth 		 * If for some reason devi_rebinding_name no longer resolves
2615*4145Scth 		 * to a proper driver then clear DEVI_REBIND.
2616*4145Scth 		 */
2617*4145Scth 		mutex_enter(&devi->devi_lock);
2618*4145Scth 		devi->devi_flags &= ~DEVI_REBIND;
2619*4145Scth 		mutex_exit(&devi->devi_lock);
2620*4145Scth 	}
2621*4145Scth 
26220Sstevel@tonic-gate 	/* look up compatible property */
26230Sstevel@tonic-gate 	(void) lookup_compatible(dip, KM_SLEEP);
26240Sstevel@tonic-gate 	compat = (void *)(devi->devi_compat_names);
26250Sstevel@tonic-gate 	len = devi->devi_compat_length;
26260Sstevel@tonic-gate 
26270Sstevel@tonic-gate 	/* find the highest precedence compatible form with a driver binding */
26280Sstevel@tonic-gate 	while ((p = prom_decode_composite_string(compat, len, p)) != NULL) {
26290Sstevel@tonic-gate 		major = ddi_name_to_major(p);
26300Sstevel@tonic-gate 		if ((major != (major_t)-1) &&
26310Sstevel@tonic-gate 		    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED)) {
26320Sstevel@tonic-gate 			if (formp)
26330Sstevel@tonic-gate 				*formp = p;
26340Sstevel@tonic-gate 			return (major);
26350Sstevel@tonic-gate 		}
26360Sstevel@tonic-gate 	}
26370Sstevel@tonic-gate 
26380Sstevel@tonic-gate 	/*
26390Sstevel@tonic-gate 	 * none of the compatible forms have a driver binding, see if
26400Sstevel@tonic-gate 	 * the node name has a driver binding.
26410Sstevel@tonic-gate 	 */
26420Sstevel@tonic-gate 	major = ddi_name_to_major(ddi_node_name(dip));
26430Sstevel@tonic-gate 	if ((major != (major_t)-1) &&
26440Sstevel@tonic-gate 	    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED))
26450Sstevel@tonic-gate 		return (major);
26460Sstevel@tonic-gate 
26470Sstevel@tonic-gate 	/* no driver */
26480Sstevel@tonic-gate 	return ((major_t)-1);
26490Sstevel@tonic-gate }
26500Sstevel@tonic-gate 
26510Sstevel@tonic-gate /*
26520Sstevel@tonic-gate  * Static help functions
26530Sstevel@tonic-gate  */
26540Sstevel@tonic-gate 
26550Sstevel@tonic-gate /*
26560Sstevel@tonic-gate  * lookup the "compatible" property and cache it's contents in the
26570Sstevel@tonic-gate  * device node.
26580Sstevel@tonic-gate  */
26590Sstevel@tonic-gate static int
26600Sstevel@tonic-gate lookup_compatible(dev_info_t *dip, uint_t flag)
26610Sstevel@tonic-gate {
26620Sstevel@tonic-gate 	int rv;
26630Sstevel@tonic-gate 	int prop_flags;
26640Sstevel@tonic-gate 	uint_t ncompatstrs;
26650Sstevel@tonic-gate 	char **compatstrpp;
26660Sstevel@tonic-gate 	char *di_compat_strp;
26670Sstevel@tonic-gate 	size_t di_compat_strlen;
26680Sstevel@tonic-gate 
26690Sstevel@tonic-gate 	if (DEVI(dip)->devi_compat_names) {
26700Sstevel@tonic-gate 		return (DDI_SUCCESS);
26710Sstevel@tonic-gate 	}
26720Sstevel@tonic-gate 
26730Sstevel@tonic-gate 	prop_flags = DDI_PROP_TYPE_STRING | DDI_PROP_DONTPASS;
26740Sstevel@tonic-gate 
26750Sstevel@tonic-gate 	if (flag & KM_NOSLEEP) {
26760Sstevel@tonic-gate 		prop_flags |= DDI_PROP_DONTSLEEP;
26770Sstevel@tonic-gate 	}
26780Sstevel@tonic-gate 
26790Sstevel@tonic-gate 	if (ndi_dev_is_prom_node(dip) == 0) {
26800Sstevel@tonic-gate 		prop_flags |= DDI_PROP_NOTPROM;
26810Sstevel@tonic-gate 	}
26820Sstevel@tonic-gate 
26830Sstevel@tonic-gate 	rv = ddi_prop_lookup_common(DDI_DEV_T_ANY, dip, prop_flags,
26840Sstevel@tonic-gate 	    "compatible", &compatstrpp, &ncompatstrs,
26850Sstevel@tonic-gate 	    ddi_prop_fm_decode_strings);
26860Sstevel@tonic-gate 
26870Sstevel@tonic-gate 	if (rv == DDI_PROP_NOT_FOUND) {
26880Sstevel@tonic-gate 		return (DDI_SUCCESS);
26890Sstevel@tonic-gate 	}
26900Sstevel@tonic-gate 
26910Sstevel@tonic-gate 	if (rv != DDI_PROP_SUCCESS) {
26920Sstevel@tonic-gate 		return (DDI_FAILURE);
26930Sstevel@tonic-gate 	}
26940Sstevel@tonic-gate 
26950Sstevel@tonic-gate 	/*
26960Sstevel@tonic-gate 	 * encode the compatible property data in the dev_info node
26970Sstevel@tonic-gate 	 */
26980Sstevel@tonic-gate 	rv = DDI_SUCCESS;
26990Sstevel@tonic-gate 	if (ncompatstrs != 0) {
27000Sstevel@tonic-gate 		di_compat_strp = encode_composite_string(compatstrpp,
27010Sstevel@tonic-gate 		    ncompatstrs, &di_compat_strlen, flag);
27020Sstevel@tonic-gate 		if (di_compat_strp != NULL) {
27030Sstevel@tonic-gate 			DEVI(dip)->devi_compat_names = di_compat_strp;
27040Sstevel@tonic-gate 			DEVI(dip)->devi_compat_length = di_compat_strlen;
27050Sstevel@tonic-gate 		} else {
27060Sstevel@tonic-gate 			rv = DDI_FAILURE;
27070Sstevel@tonic-gate 		}
27080Sstevel@tonic-gate 	}
27090Sstevel@tonic-gate 	ddi_prop_free(compatstrpp);
27100Sstevel@tonic-gate 	return (rv);
27110Sstevel@tonic-gate }
27120Sstevel@tonic-gate 
27130Sstevel@tonic-gate /*
27140Sstevel@tonic-gate  * Create a composite string from a list of strings.
27150Sstevel@tonic-gate  *
27160Sstevel@tonic-gate  * A composite string consists of a single buffer containing one
27170Sstevel@tonic-gate  * or more NULL terminated strings.
27180Sstevel@tonic-gate  */
27190Sstevel@tonic-gate static char *
27200Sstevel@tonic-gate encode_composite_string(char **strings, uint_t nstrings, size_t *retsz,
27210Sstevel@tonic-gate     uint_t flag)
27220Sstevel@tonic-gate {
27230Sstevel@tonic-gate 	uint_t index;
27240Sstevel@tonic-gate 	char  **strpp;
27250Sstevel@tonic-gate 	uint_t slen;
27260Sstevel@tonic-gate 	size_t cbuf_sz = 0;
27270Sstevel@tonic-gate 	char *cbuf_p;
27280Sstevel@tonic-gate 	char *cbuf_ip;
27290Sstevel@tonic-gate 
27300Sstevel@tonic-gate 	if (strings == NULL || nstrings == 0 || retsz == NULL) {
27310Sstevel@tonic-gate 		return (NULL);
27320Sstevel@tonic-gate 	}
27330Sstevel@tonic-gate 
27340Sstevel@tonic-gate 	for (index = 0, strpp = strings; index < nstrings; index++)
27350Sstevel@tonic-gate 		cbuf_sz += strlen(*(strpp++)) + 1;
27360Sstevel@tonic-gate 
27370Sstevel@tonic-gate 	if ((cbuf_p = kmem_alloc(cbuf_sz, flag)) == NULL) {
27380Sstevel@tonic-gate 		cmn_err(CE_NOTE,
27390Sstevel@tonic-gate 		    "?failed to allocate device node compatstr");
27400Sstevel@tonic-gate 		return (NULL);
27410Sstevel@tonic-gate 	}
27420Sstevel@tonic-gate 
27430Sstevel@tonic-gate 	cbuf_ip = cbuf_p;
27440Sstevel@tonic-gate 	for (index = 0, strpp = strings; index < nstrings; index++) {
27450Sstevel@tonic-gate 		slen = strlen(*strpp);
27460Sstevel@tonic-gate 		bcopy(*(strpp++), cbuf_ip, slen);
27470Sstevel@tonic-gate 		cbuf_ip += slen;
27480Sstevel@tonic-gate 		*(cbuf_ip++) = '\0';
27490Sstevel@tonic-gate 	}
27500Sstevel@tonic-gate 
27510Sstevel@tonic-gate 	*retsz = cbuf_sz;
27520Sstevel@tonic-gate 	return (cbuf_p);
27530Sstevel@tonic-gate }
27540Sstevel@tonic-gate 
27550Sstevel@tonic-gate static void
27560Sstevel@tonic-gate link_to_driver_list(dev_info_t *dip)
27570Sstevel@tonic-gate {
27580Sstevel@tonic-gate 	major_t major = DEVI(dip)->devi_major;
27590Sstevel@tonic-gate 	struct devnames *dnp;
27600Sstevel@tonic-gate 
27610Sstevel@tonic-gate 	ASSERT(major != (major_t)-1);
27620Sstevel@tonic-gate 
27630Sstevel@tonic-gate 	/*
27640Sstevel@tonic-gate 	 * Remove from orphan list
27650Sstevel@tonic-gate 	 */
27660Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
27670Sstevel@tonic-gate 		dnp = &orphanlist;
27680Sstevel@tonic-gate 		remove_from_dn_list(dnp, dip);
27690Sstevel@tonic-gate 	}
27700Sstevel@tonic-gate 
27710Sstevel@tonic-gate 	/*
27720Sstevel@tonic-gate 	 * Add to per driver list
27730Sstevel@tonic-gate 	 */
27740Sstevel@tonic-gate 	dnp = &devnamesp[major];
27750Sstevel@tonic-gate 	add_to_dn_list(dnp, dip);
27760Sstevel@tonic-gate }
27770Sstevel@tonic-gate 
27780Sstevel@tonic-gate static void
27790Sstevel@tonic-gate unlink_from_driver_list(dev_info_t *dip)
27800Sstevel@tonic-gate {
27810Sstevel@tonic-gate 	major_t major = DEVI(dip)->devi_major;
27820Sstevel@tonic-gate 	struct devnames *dnp;
27830Sstevel@tonic-gate 
27840Sstevel@tonic-gate 	ASSERT(major != (major_t)-1);
27850Sstevel@tonic-gate 
27860Sstevel@tonic-gate 	/*
27870Sstevel@tonic-gate 	 * Remove from per-driver list
27880Sstevel@tonic-gate 	 */
27890Sstevel@tonic-gate 	dnp = &devnamesp[major];
27900Sstevel@tonic-gate 	remove_from_dn_list(dnp, dip);
27910Sstevel@tonic-gate 
27920Sstevel@tonic-gate 	/*
27930Sstevel@tonic-gate 	 * Add to orphan list
27940Sstevel@tonic-gate 	 */
27950Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
27960Sstevel@tonic-gate 		dnp = &orphanlist;
27970Sstevel@tonic-gate 		add_to_dn_list(dnp, dip);
27980Sstevel@tonic-gate 	}
27990Sstevel@tonic-gate }
28000Sstevel@tonic-gate 
28010Sstevel@tonic-gate /*
28020Sstevel@tonic-gate  * scan the per-driver list looking for dev_info "dip"
28030Sstevel@tonic-gate  */
28040Sstevel@tonic-gate static dev_info_t *
28050Sstevel@tonic-gate in_dn_list(struct devnames *dnp, dev_info_t *dip)
28060Sstevel@tonic-gate {
28070Sstevel@tonic-gate 	struct dev_info *idevi;
28080Sstevel@tonic-gate 
28090Sstevel@tonic-gate 	if ((idevi = DEVI(dnp->dn_head)) == NULL)
28100Sstevel@tonic-gate 		return (NULL);
28110Sstevel@tonic-gate 
28120Sstevel@tonic-gate 	while (idevi) {
28130Sstevel@tonic-gate 		if (idevi == DEVI(dip))
28140Sstevel@tonic-gate 			return (dip);
28150Sstevel@tonic-gate 		idevi = idevi->devi_next;
28160Sstevel@tonic-gate 	}
28170Sstevel@tonic-gate 	return (NULL);
28180Sstevel@tonic-gate }
28190Sstevel@tonic-gate 
28200Sstevel@tonic-gate /*
28210Sstevel@tonic-gate  * insert devinfo node 'dip' into the per-driver instance list
28220Sstevel@tonic-gate  * headed by 'dnp'
28230Sstevel@tonic-gate  *
28240Sstevel@tonic-gate  * Nodes on the per-driver list are ordered: HW - SID - PSEUDO.  The order is
28250Sstevel@tonic-gate  * required for merging of .conf file data to work properly.
28260Sstevel@tonic-gate  */
28270Sstevel@tonic-gate static void
28280Sstevel@tonic-gate add_to_ordered_dn_list(struct devnames *dnp, dev_info_t *dip)
28290Sstevel@tonic-gate {
28300Sstevel@tonic-gate 	dev_info_t **dipp;
28310Sstevel@tonic-gate 
28320Sstevel@tonic-gate 	ASSERT(mutex_owned(&(dnp->dn_lock)));
28330Sstevel@tonic-gate 
28340Sstevel@tonic-gate 	dipp = &dnp->dn_head;
28350Sstevel@tonic-gate 	if (ndi_dev_is_prom_node(dip)) {
28360Sstevel@tonic-gate 		/*
28370Sstevel@tonic-gate 		 * Find the first non-prom node or end of list
28380Sstevel@tonic-gate 		 */
28390Sstevel@tonic-gate 		while (*dipp && (ndi_dev_is_prom_node(*dipp) != 0)) {
28400Sstevel@tonic-gate 			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
28410Sstevel@tonic-gate 		}
28420Sstevel@tonic-gate 	} else if (ndi_dev_is_persistent_node(dip)) {
28430Sstevel@tonic-gate 		/*
28440Sstevel@tonic-gate 		 * Find the first non-persistent node
28450Sstevel@tonic-gate 		 */
28460Sstevel@tonic-gate 		while (*dipp && (ndi_dev_is_persistent_node(*dipp) != 0)) {
28470Sstevel@tonic-gate 			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
28480Sstevel@tonic-gate 		}
28490Sstevel@tonic-gate 	} else {
28500Sstevel@tonic-gate 		/*
28510Sstevel@tonic-gate 		 * Find the end of the list
28520Sstevel@tonic-gate 		 */
28530Sstevel@tonic-gate 		while (*dipp) {
28540Sstevel@tonic-gate 			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
28550Sstevel@tonic-gate 		}
28560Sstevel@tonic-gate 	}
28570Sstevel@tonic-gate 
28580Sstevel@tonic-gate 	DEVI(dip)->devi_next = DEVI(*dipp);
28590Sstevel@tonic-gate 	*dipp = dip;
28600Sstevel@tonic-gate }
28610Sstevel@tonic-gate 
28620Sstevel@tonic-gate /*
28630Sstevel@tonic-gate  * add a list of device nodes to the device node list in the
28640Sstevel@tonic-gate  * devnames structure
28650Sstevel@tonic-gate  */
28660Sstevel@tonic-gate static void
28670Sstevel@tonic-gate add_to_dn_list(struct devnames *dnp, dev_info_t *dip)
28680Sstevel@tonic-gate {
28690Sstevel@tonic-gate 	/*
28700Sstevel@tonic-gate 	 * Look to see if node already exists
28710Sstevel@tonic-gate 	 */
28720Sstevel@tonic-gate 	LOCK_DEV_OPS(&(dnp->dn_lock));
28730Sstevel@tonic-gate 	if (in_dn_list(dnp, dip)) {
28740Sstevel@tonic-gate 		cmn_err(CE_NOTE, "add_to_dn_list: node %s already in list",
28750Sstevel@tonic-gate 		    DEVI(dip)->devi_node_name);
28760Sstevel@tonic-gate 	} else {
28770Sstevel@tonic-gate 		add_to_ordered_dn_list(dnp, dip);
28780Sstevel@tonic-gate 	}
28790Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
28800Sstevel@tonic-gate }
28810Sstevel@tonic-gate 
28820Sstevel@tonic-gate static void
28830Sstevel@tonic-gate remove_from_dn_list(struct devnames *dnp, dev_info_t *dip)
28840Sstevel@tonic-gate {
28850Sstevel@tonic-gate 	dev_info_t **plist;
28860Sstevel@tonic-gate 
28870Sstevel@tonic-gate 	LOCK_DEV_OPS(&(dnp->dn_lock));
28880Sstevel@tonic-gate 
28890Sstevel@tonic-gate 	plist = (dev_info_t **)&dnp->dn_head;
28900Sstevel@tonic-gate 	while (*plist && (*plist != dip)) {
28910Sstevel@tonic-gate 		plist = (dev_info_t **)&DEVI(*plist)->devi_next;
28920Sstevel@tonic-gate 	}
28930Sstevel@tonic-gate 
28940Sstevel@tonic-gate 	if (*plist != NULL) {
28950Sstevel@tonic-gate 		ASSERT(*plist == dip);
28960Sstevel@tonic-gate 		*plist = (dev_info_t *)(DEVI(dip)->devi_next);
28970Sstevel@tonic-gate 		DEVI(dip)->devi_next = NULL;
28980Sstevel@tonic-gate 	} else {
28990Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE,
29000Sstevel@tonic-gate 		    "remove_from_dn_list: node %s not found in list",
29010Sstevel@tonic-gate 		    DEVI(dip)->devi_node_name));
29020Sstevel@tonic-gate 	}
29030Sstevel@tonic-gate 
29040Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
29050Sstevel@tonic-gate }
29060Sstevel@tonic-gate 
29070Sstevel@tonic-gate /*
29080Sstevel@tonic-gate  * Add and remove reference driver global property list
29090Sstevel@tonic-gate  */
29100Sstevel@tonic-gate static void
29110Sstevel@tonic-gate add_global_props(dev_info_t *dip)
29120Sstevel@tonic-gate {
29130Sstevel@tonic-gate 	struct devnames *dnp;
29140Sstevel@tonic-gate 	ddi_prop_list_t *plist;
29150Sstevel@tonic-gate 
29160Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_global_prop_list == NULL);
29170Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_major != (major_t)-1);
29180Sstevel@tonic-gate 
29190Sstevel@tonic-gate 	dnp = &devnamesp[DEVI(dip)->devi_major];
29200Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
29210Sstevel@tonic-gate 	plist = dnp->dn_global_prop_ptr;
29220Sstevel@tonic-gate 	if (plist == NULL) {
29230Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
29240Sstevel@tonic-gate 		return;
29250Sstevel@tonic-gate 	}
29260Sstevel@tonic-gate 	i_ddi_prop_list_hold(plist, dnp);
29270Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
29280Sstevel@tonic-gate 
29290Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
29300Sstevel@tonic-gate 	DEVI(dip)->devi_global_prop_list = plist;
29310Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
29320Sstevel@tonic-gate }
29330Sstevel@tonic-gate 
29340Sstevel@tonic-gate static void
29350Sstevel@tonic-gate remove_global_props(dev_info_t *dip)
29360Sstevel@tonic-gate {
29370Sstevel@tonic-gate 	ddi_prop_list_t *proplist;
29380Sstevel@tonic-gate 
29390Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
29400Sstevel@tonic-gate 	proplist = DEVI(dip)->devi_global_prop_list;
29410Sstevel@tonic-gate 	DEVI(dip)->devi_global_prop_list = NULL;
29420Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
29430Sstevel@tonic-gate 
29440Sstevel@tonic-gate 	if (proplist) {
29450Sstevel@tonic-gate 		major_t major;
29460Sstevel@tonic-gate 		struct devnames *dnp;
29470Sstevel@tonic-gate 
29480Sstevel@tonic-gate 		major = ddi_driver_major(dip);
29490Sstevel@tonic-gate 		ASSERT(major != (major_t)-1);
29500Sstevel@tonic-gate 		dnp = &devnamesp[major];
29510Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
29520Sstevel@tonic-gate 		i_ddi_prop_list_rele(proplist, dnp);
29530Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
29540Sstevel@tonic-gate 	}
29550Sstevel@tonic-gate }
29560Sstevel@tonic-gate 
29570Sstevel@tonic-gate #ifdef DEBUG
29580Sstevel@tonic-gate /*
29590Sstevel@tonic-gate  * Set this variable to '0' to disable the optimization,
29600Sstevel@tonic-gate  * and to 2 to print debug message.
29610Sstevel@tonic-gate  */
29620Sstevel@tonic-gate static int optimize_dtree = 1;
29630Sstevel@tonic-gate 
29640Sstevel@tonic-gate static void
29650Sstevel@tonic-gate debug_dtree(dev_info_t *devi, struct dev_info *adevi, char *service)
29660Sstevel@tonic-gate {
29670Sstevel@tonic-gate 	char *adeviname, *buf;
29680Sstevel@tonic-gate 
29690Sstevel@tonic-gate 	/*
29700Sstevel@tonic-gate 	 * Don't print unless optimize dtree is set to 2+
29710Sstevel@tonic-gate 	 */
29720Sstevel@tonic-gate 	if (optimize_dtree <= 1)
29730Sstevel@tonic-gate 		return;
29740Sstevel@tonic-gate 
29750Sstevel@tonic-gate 	buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
29760Sstevel@tonic-gate 	adeviname = ddi_deviname((dev_info_t *)adevi, buf);
29770Sstevel@tonic-gate 	if (*adeviname == '\0')
29780Sstevel@tonic-gate 		adeviname = "root";
29790Sstevel@tonic-gate 
29800Sstevel@tonic-gate 	cmn_err(CE_CONT, "%s %s -> %s\n",
29810Sstevel@tonic-gate 	    ddi_deviname(devi, buf), service, adeviname);
29820Sstevel@tonic-gate 
29830Sstevel@tonic-gate 	kmem_free(buf, MAXNAMELEN);
29840Sstevel@tonic-gate }
29850Sstevel@tonic-gate #else /* DEBUG */
29860Sstevel@tonic-gate #define	debug_dtree(a1, a2, a3)	 /* nothing */
29870Sstevel@tonic-gate #endif  /* DEBUG */
29880Sstevel@tonic-gate 
29890Sstevel@tonic-gate static void
29900Sstevel@tonic-gate ddi_optimize_dtree(dev_info_t *devi)
29910Sstevel@tonic-gate {
29920Sstevel@tonic-gate 	struct dev_info *pdevi;
29930Sstevel@tonic-gate 	struct bus_ops *b;
29940Sstevel@tonic-gate 
29950Sstevel@tonic-gate 	pdevi = DEVI(devi)->devi_parent;
29960Sstevel@tonic-gate 	ASSERT(pdevi);
29970Sstevel@tonic-gate 
29980Sstevel@tonic-gate 	/*
29990Sstevel@tonic-gate 	 * Set the unoptimized values
30000Sstevel@tonic-gate 	 */
30010Sstevel@tonic-gate 	DEVI(devi)->devi_bus_map_fault = pdevi;
30020Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_map = pdevi;
30030Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_allochdl = pdevi;
30040Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_freehdl = pdevi;
30050Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_bindhdl = pdevi;
30060Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_bindfunc =
30070Sstevel@tonic-gate 	pdevi->devi_ops->devo_bus_ops->bus_dma_bindhdl;
30080Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_unbindhdl = pdevi;
30090Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_unbindfunc =
30100Sstevel@tonic-gate 	    pdevi->devi_ops->devo_bus_ops->bus_dma_unbindhdl;
30110Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_flush = pdevi;
30120Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_win = pdevi;
30130Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_ctl = pdevi;
30140Sstevel@tonic-gate 	DEVI(devi)->devi_bus_ctl = pdevi;
30150Sstevel@tonic-gate 
30160Sstevel@tonic-gate #ifdef DEBUG
30170Sstevel@tonic-gate 	if (optimize_dtree == 0)
30180Sstevel@tonic-gate 		return;
30190Sstevel@tonic-gate #endif /* DEBUG */
30200Sstevel@tonic-gate 
30210Sstevel@tonic-gate 	b = pdevi->devi_ops->devo_bus_ops;
30220Sstevel@tonic-gate 
30230Sstevel@tonic-gate 	if (i_ddi_map_fault == b->bus_map_fault) {
30240Sstevel@tonic-gate 		DEVI(devi)->devi_bus_map_fault = pdevi->devi_bus_map_fault;
30250Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_map_fault,
30260Sstevel@tonic-gate 		    "bus_map_fault");
30270Sstevel@tonic-gate 	}
30280Sstevel@tonic-gate 
30290Sstevel@tonic-gate 	if (ddi_dma_map == b->bus_dma_map) {
30300Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_map = pdevi->devi_bus_dma_map;
30310Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_map, "bus_dma_map");
30320Sstevel@tonic-gate 	}
30330Sstevel@tonic-gate 
30340Sstevel@tonic-gate 	if (ddi_dma_allochdl == b->bus_dma_allochdl) {
30350Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_allochdl =
30360Sstevel@tonic-gate 		    pdevi->devi_bus_dma_allochdl;
30370Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_allochdl,
30380Sstevel@tonic-gate 		    "bus_dma_allochdl");
30390Sstevel@tonic-gate 	}
30400Sstevel@tonic-gate 
30410Sstevel@tonic-gate 	if (ddi_dma_freehdl == b->bus_dma_freehdl) {
30420Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_freehdl = pdevi->devi_bus_dma_freehdl;
30430Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_freehdl,
30440Sstevel@tonic-gate 		    "bus_dma_freehdl");
30450Sstevel@tonic-gate 	}
30460Sstevel@tonic-gate 
30470Sstevel@tonic-gate 	if (ddi_dma_bindhdl == b->bus_dma_bindhdl) {
30480Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_bindhdl = pdevi->devi_bus_dma_bindhdl;
30490Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_bindfunc =
30500Sstevel@tonic-gate 		    pdevi->devi_bus_dma_bindhdl->devi_ops->
30510Sstevel@tonic-gate 		    devo_bus_ops->bus_dma_bindhdl;
30520Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_bindhdl,
30530Sstevel@tonic-gate 		    "bus_dma_bindhdl");
30540Sstevel@tonic-gate 	}
30550Sstevel@tonic-gate 
30560Sstevel@tonic-gate 	if (ddi_dma_unbindhdl == b->bus_dma_unbindhdl) {
30570Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_unbindhdl =
30580Sstevel@tonic-gate 		    pdevi->devi_bus_dma_unbindhdl;
30590Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_unbindfunc =
30600Sstevel@tonic-gate 		    pdevi->devi_bus_dma_unbindhdl->devi_ops->
30610Sstevel@tonic-gate 		    devo_bus_ops->bus_dma_unbindhdl;
30620Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_unbindhdl,
30630Sstevel@tonic-gate 		    "bus_dma_unbindhdl");
30640Sstevel@tonic-gate 	}
30650Sstevel@tonic-gate 
30660Sstevel@tonic-gate 	if (ddi_dma_flush == b->bus_dma_flush) {
30670Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_flush = pdevi->devi_bus_dma_flush;
30680Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_flush,
30690Sstevel@tonic-gate 		    "bus_dma_flush");
30700Sstevel@tonic-gate 	}
30710Sstevel@tonic-gate 
30720Sstevel@tonic-gate 	if (ddi_dma_win == b->bus_dma_win) {
30730Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_win = pdevi->devi_bus_dma_win;
30740Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_win,
30750Sstevel@tonic-gate 		    "bus_dma_win");
30760Sstevel@tonic-gate 	}
30770Sstevel@tonic-gate 
30780Sstevel@tonic-gate 	if (ddi_dma_mctl == b->bus_dma_ctl) {
30790Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_ctl = pdevi->devi_bus_dma_ctl;
30800Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_ctl, "bus_dma_ctl");
30810Sstevel@tonic-gate 	}
30820Sstevel@tonic-gate 
30830Sstevel@tonic-gate 	if (ddi_ctlops == b->bus_ctl) {
30840Sstevel@tonic-gate 		DEVI(devi)->devi_bus_ctl = pdevi->devi_bus_ctl;
30850Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_ctl, "bus_ctl");
30860Sstevel@tonic-gate 	}
30870Sstevel@tonic-gate }
30880Sstevel@tonic-gate 
30890Sstevel@tonic-gate #define	MIN_DEVINFO_LOG_SIZE	max_ncpus
30900Sstevel@tonic-gate #define	MAX_DEVINFO_LOG_SIZE	max_ncpus * 10
30910Sstevel@tonic-gate 
30920Sstevel@tonic-gate static void
30930Sstevel@tonic-gate da_log_init()
30940Sstevel@tonic-gate {
30950Sstevel@tonic-gate 	devinfo_log_header_t *dh;
30960Sstevel@tonic-gate 	int logsize = devinfo_log_size;
30970Sstevel@tonic-gate 
30980Sstevel@tonic-gate 	if (logsize == 0)
30990Sstevel@tonic-gate 		logsize = MIN_DEVINFO_LOG_SIZE;
31000Sstevel@tonic-gate 	else if (logsize > MAX_DEVINFO_LOG_SIZE)
31010Sstevel@tonic-gate 		logsize = MAX_DEVINFO_LOG_SIZE;
31020Sstevel@tonic-gate 
31030Sstevel@tonic-gate 	dh = kmem_alloc(logsize * PAGESIZE, KM_SLEEP);
31040Sstevel@tonic-gate 	mutex_init(&dh->dh_lock, NULL, MUTEX_DEFAULT, NULL);
31050Sstevel@tonic-gate 	dh->dh_max = ((logsize * PAGESIZE) - sizeof (*dh)) /
31060Sstevel@tonic-gate 	    sizeof (devinfo_audit_t) + 1;
31070Sstevel@tonic-gate 	dh->dh_curr = -1;
31080Sstevel@tonic-gate 	dh->dh_hits = 0;
31090Sstevel@tonic-gate 
31100Sstevel@tonic-gate 	devinfo_audit_log = dh;
31110Sstevel@tonic-gate }
31120Sstevel@tonic-gate 
31130Sstevel@tonic-gate /*
31140Sstevel@tonic-gate  * Log the stack trace in per-devinfo audit structure and also enter
31150Sstevel@tonic-gate  * it into a system wide log for recording the time history.
31160Sstevel@tonic-gate  */
31170Sstevel@tonic-gate static void
31180Sstevel@tonic-gate da_log_enter(dev_info_t *dip)
31190Sstevel@tonic-gate {
31200Sstevel@tonic-gate 	devinfo_audit_t *da_log, *da = DEVI(dip)->devi_audit;
31210Sstevel@tonic-gate 	devinfo_log_header_t *dh = devinfo_audit_log;
31220Sstevel@tonic-gate 
31230Sstevel@tonic-gate 	if (devinfo_audit_log == NULL)
31240Sstevel@tonic-gate 		return;
31250Sstevel@tonic-gate 
31260Sstevel@tonic-gate 	ASSERT(da != NULL);
31270Sstevel@tonic-gate 
31280Sstevel@tonic-gate 	da->da_devinfo = dip;
31290Sstevel@tonic-gate 	da->da_timestamp = gethrtime();
31300Sstevel@tonic-gate 	da->da_thread = curthread;
31310Sstevel@tonic-gate 	da->da_node_state = DEVI(dip)->devi_node_state;
31320Sstevel@tonic-gate 	da->da_device_state = DEVI(dip)->devi_state;
31330Sstevel@tonic-gate 	da->da_depth = getpcstack(da->da_stack, DDI_STACK_DEPTH);
31340Sstevel@tonic-gate 
31350Sstevel@tonic-gate 	/*
31360Sstevel@tonic-gate 	 * Copy into common log and note the location for tracing history
31370Sstevel@tonic-gate 	 */
31380Sstevel@tonic-gate 	mutex_enter(&dh->dh_lock);
31390Sstevel@tonic-gate 	dh->dh_hits++;
31400Sstevel@tonic-gate 	dh->dh_curr++;
31410Sstevel@tonic-gate 	if (dh->dh_curr >= dh->dh_max)
31420Sstevel@tonic-gate 		dh->dh_curr -= dh->dh_max;
31430Sstevel@tonic-gate 	da_log = &dh->dh_entry[dh->dh_curr];
31440Sstevel@tonic-gate 	mutex_exit(&dh->dh_lock);
31450Sstevel@tonic-gate 
31460Sstevel@tonic-gate 	bcopy(da, da_log, sizeof (devinfo_audit_t));
31470Sstevel@tonic-gate 	da->da_lastlog = da_log;
31480Sstevel@tonic-gate }
31490Sstevel@tonic-gate 
31500Sstevel@tonic-gate static void
31510Sstevel@tonic-gate attach_drivers()
31520Sstevel@tonic-gate {
31530Sstevel@tonic-gate 	int i;
31540Sstevel@tonic-gate 	for (i = 0; i < devcnt; i++) {
31550Sstevel@tonic-gate 		struct devnames *dnp = &devnamesp[i];
31560Sstevel@tonic-gate 		if ((dnp->dn_flags & DN_FORCE_ATTACH) &&
31570Sstevel@tonic-gate 		    (ddi_hold_installed_driver((major_t)i) != NULL))
31580Sstevel@tonic-gate 			ddi_rele_driver((major_t)i);
31590Sstevel@tonic-gate 	}
31600Sstevel@tonic-gate }
31610Sstevel@tonic-gate 
31620Sstevel@tonic-gate /*
31630Sstevel@tonic-gate  * Launch a thread to force attach drivers. This avoids penalty on boot time.
31640Sstevel@tonic-gate  */
31650Sstevel@tonic-gate void
31660Sstevel@tonic-gate i_ddi_forceattach_drivers()
31670Sstevel@tonic-gate {
31680Sstevel@tonic-gate 	/*
31690Sstevel@tonic-gate 	 * On i386, the USB drivers need to load and take over from the
31700Sstevel@tonic-gate 	 * SMM BIOS drivers ASAP after consconfig(), so make sure they
31710Sstevel@tonic-gate 	 * get loaded right here rather than letting the thread do it.
31720Sstevel@tonic-gate 	 *
31730Sstevel@tonic-gate 	 * The order here is important.  EHCI must be loaded first, as
31740Sstevel@tonic-gate 	 * we have observed many systems on which hangs occur if the
31750Sstevel@tonic-gate 	 * {U,O}HCI companion controllers take over control from the BIOS
31760Sstevel@tonic-gate 	 * before EHCI does.  These hangs are also caused by BIOSes leaving
31770Sstevel@tonic-gate 	 * interrupt-on-port-change enabled in the ehci controller, so that
31780Sstevel@tonic-gate 	 * when uhci/ohci reset themselves, it induces a port change on
31790Sstevel@tonic-gate 	 * the ehci companion controller.  Since there's no interrupt handler
31800Sstevel@tonic-gate 	 * installed at the time, the moment that interrupt is unmasked, an
31810Sstevel@tonic-gate 	 * interrupt storm will occur.  All this is averted when ehci is
31820Sstevel@tonic-gate 	 * loaded first.  And now you know..... the REST of the story.
31830Sstevel@tonic-gate 	 *
31840Sstevel@tonic-gate 	 * Regardless of platform, ehci needs to initialize first to avoid
31850Sstevel@tonic-gate 	 * unnecessary connects and disconnects on the companion controller
31860Sstevel@tonic-gate 	 * when ehci sets up the routing.
31870Sstevel@tonic-gate 	 */
31880Sstevel@tonic-gate 	(void) ddi_hold_installed_driver(ddi_name_to_major("ehci"));
31890Sstevel@tonic-gate 	(void) ddi_hold_installed_driver(ddi_name_to_major("uhci"));
31900Sstevel@tonic-gate 	(void) ddi_hold_installed_driver(ddi_name_to_major("ohci"));
31910Sstevel@tonic-gate 
31921093Shiremath 	/*
31931093Shiremath 	 * Attach IB VHCI driver before the force-attach thread attaches the
31941093Shiremath 	 * IB HCA driver. IB HCA driver will fail if IB Nexus has not yet
31951093Shiremath 	 * been attached.
31961093Shiremath 	 */
31971093Shiremath 	(void) ddi_hold_installed_driver(ddi_name_to_major("ib"));
31981093Shiremath 
31990Sstevel@tonic-gate 	(void) thread_create(NULL, 0, (void (*)())attach_drivers, NULL, 0, &p0,
32000Sstevel@tonic-gate 	    TS_RUN, minclsyspri);
32010Sstevel@tonic-gate }
32020Sstevel@tonic-gate 
32030Sstevel@tonic-gate /*
32040Sstevel@tonic-gate  * This is a private DDI interface for optimizing boot performance.
32050Sstevel@tonic-gate  * I/O subsystem initialization is considered complete when devfsadm
32060Sstevel@tonic-gate  * is executed.
32070Sstevel@tonic-gate  *
32082621Sllai1  * NOTE: The start of syseventd happens to be a convenient indicator
32092621Sllai1  *	of the completion of I/O initialization during boot.
32100Sstevel@tonic-gate  *	The implementation should be replaced by something more robust.
32110Sstevel@tonic-gate  */
32120Sstevel@tonic-gate int
32130Sstevel@tonic-gate i_ddi_io_initialized()
32140Sstevel@tonic-gate {
32150Sstevel@tonic-gate 	extern int sysevent_daemon_init;
32160Sstevel@tonic-gate 	return (sysevent_daemon_init);
32170Sstevel@tonic-gate }
32180Sstevel@tonic-gate 
32192621Sllai1 /*
32202621Sllai1  * May be used to determine system boot state
32212621Sllai1  * "Available" means the system is for the most part up
32222621Sllai1  * and initialized, with all system services either up or
32232621Sllai1  * capable of being started.  This state is set by devfsadm
32242621Sllai1  * during the boot process.  The /dev filesystem infers
32252621Sllai1  * from this when implicit reconfig can be performed,
32262621Sllai1  * ie, devfsadm can be invoked.  Please avoid making
32272621Sllai1  * further use of this unless it's really necessary.
32282621Sllai1  */
32292621Sllai1 int
32302621Sllai1 i_ddi_sysavail()
32312621Sllai1 {
32322621Sllai1 	return (devname_state & DS_SYSAVAIL);
32332621Sllai1 }
32342621Sllai1 
32352621Sllai1 /*
32362621Sllai1  * May be used to determine if boot is a reconfigure boot.
32372621Sllai1  */
32382621Sllai1 int
32392621Sllai1 i_ddi_reconfig()
32402621Sllai1 {
32412621Sllai1 	return (devname_state & DS_RECONFIG);
32422621Sllai1 }
32432621Sllai1 
32442621Sllai1 /*
32452621Sllai1  * Note system services are up, inform /dev.
32462621Sllai1  */
32472621Sllai1 void
32482621Sllai1 i_ddi_set_sysavail()
32492621Sllai1 {
32502621Sllai1 	if ((devname_state & DS_SYSAVAIL) == 0) {
32512621Sllai1 		devname_state |= DS_SYSAVAIL;
32522621Sllai1 		sdev_devstate_change();
32532621Sllai1 	}
32542621Sllai1 }
32552621Sllai1 
32562621Sllai1 /*
32572621Sllai1  * Note reconfiguration boot, inform /dev.
32582621Sllai1  */
32592621Sllai1 void
32602621Sllai1 i_ddi_set_reconfig()
32612621Sllai1 {
32622621Sllai1 	if ((devname_state & DS_RECONFIG) == 0) {
32632621Sllai1 		devname_state |= DS_RECONFIG;
32642621Sllai1 		sdev_devstate_change();
32652621Sllai1 	}
32662621Sllai1 }
32672621Sllai1 
32680Sstevel@tonic-gate 
32690Sstevel@tonic-gate /*
32700Sstevel@tonic-gate  * device tree walking
32710Sstevel@tonic-gate  */
32720Sstevel@tonic-gate 
32730Sstevel@tonic-gate struct walk_elem {
32740Sstevel@tonic-gate 	struct walk_elem *next;
32750Sstevel@tonic-gate 	dev_info_t *dip;
32760Sstevel@tonic-gate };
32770Sstevel@tonic-gate 
32780Sstevel@tonic-gate static void
32790Sstevel@tonic-gate free_list(struct walk_elem *list)
32800Sstevel@tonic-gate {
32810Sstevel@tonic-gate 	while (list) {
32820Sstevel@tonic-gate 		struct walk_elem *next = list->next;
32830Sstevel@tonic-gate 		kmem_free(list, sizeof (*list));
32840Sstevel@tonic-gate 		list = next;
32850Sstevel@tonic-gate 	}
32860Sstevel@tonic-gate }
32870Sstevel@tonic-gate 
32880Sstevel@tonic-gate static void
32890Sstevel@tonic-gate append_node(struct walk_elem **list, dev_info_t *dip)
32900Sstevel@tonic-gate {
32910Sstevel@tonic-gate 	struct walk_elem *tail;
32920Sstevel@tonic-gate 	struct walk_elem *elem = kmem_alloc(sizeof (*elem), KM_SLEEP);
32930Sstevel@tonic-gate 
32940Sstevel@tonic-gate 	elem->next = NULL;
32950Sstevel@tonic-gate 	elem->dip = dip;
32960Sstevel@tonic-gate 
32970Sstevel@tonic-gate 	if (*list == NULL) {
32980Sstevel@tonic-gate 		*list = elem;
32990Sstevel@tonic-gate 		return;
33000Sstevel@tonic-gate 	}
33010Sstevel@tonic-gate 
33020Sstevel@tonic-gate 	tail = *list;
33030Sstevel@tonic-gate 	while (tail->next)
33040Sstevel@tonic-gate 		tail = tail->next;
33050Sstevel@tonic-gate 
33060Sstevel@tonic-gate 	tail->next = elem;
33070Sstevel@tonic-gate }
33080Sstevel@tonic-gate 
33090Sstevel@tonic-gate /*
33100Sstevel@tonic-gate  * The implementation of ddi_walk_devs().
33110Sstevel@tonic-gate  */
33120Sstevel@tonic-gate static int
33130Sstevel@tonic-gate walk_devs(dev_info_t *dip, int (*f)(dev_info_t *, void *), void *arg,
33140Sstevel@tonic-gate     int do_locking)
33150Sstevel@tonic-gate {
33160Sstevel@tonic-gate 	struct walk_elem *head = NULL;
33170Sstevel@tonic-gate 
33180Sstevel@tonic-gate 	/*
33190Sstevel@tonic-gate 	 * Do it in two passes. First pass invoke callback on each
33200Sstevel@tonic-gate 	 * dip on the sibling list. Second pass invoke callback on
33210Sstevel@tonic-gate 	 * children of each dip.
33220Sstevel@tonic-gate 	 */
33230Sstevel@tonic-gate 	while (dip) {
33240Sstevel@tonic-gate 		switch ((*f)(dip, arg)) {
33250Sstevel@tonic-gate 		case DDI_WALK_TERMINATE:
33260Sstevel@tonic-gate 			free_list(head);
33270Sstevel@tonic-gate 			return (DDI_WALK_TERMINATE);
33280Sstevel@tonic-gate 
33290Sstevel@tonic-gate 		case DDI_WALK_PRUNESIB:
33300Sstevel@tonic-gate 			/* ignore sibling by setting dip to NULL */
33310Sstevel@tonic-gate 			append_node(&head, dip);
33320Sstevel@tonic-gate 			dip = NULL;
33330Sstevel@tonic-gate 			break;
33340Sstevel@tonic-gate 
33350Sstevel@tonic-gate 		case DDI_WALK_PRUNECHILD:
33360Sstevel@tonic-gate 			/* don't worry about children */
33370Sstevel@tonic-gate 			dip = ddi_get_next_sibling(dip);
33380Sstevel@tonic-gate 			break;
33390Sstevel@tonic-gate 
33400Sstevel@tonic-gate 		case DDI_WALK_CONTINUE:
33410Sstevel@tonic-gate 		default:
33420Sstevel@tonic-gate 			append_node(&head, dip);
33430Sstevel@tonic-gate 			dip = ddi_get_next_sibling(dip);
33440Sstevel@tonic-gate 			break;
33450Sstevel@tonic-gate 		}
33460Sstevel@tonic-gate 
33470Sstevel@tonic-gate 	}
33480Sstevel@tonic-gate 
33490Sstevel@tonic-gate 	/* second pass */
33500Sstevel@tonic-gate 	while (head) {
33510Sstevel@tonic-gate 		int circ;
33520Sstevel@tonic-gate 		struct walk_elem *next = head->next;
33530Sstevel@tonic-gate 
33540Sstevel@tonic-gate 		if (do_locking)
33550Sstevel@tonic-gate 			ndi_devi_enter(head->dip, &circ);
33560Sstevel@tonic-gate 		if (walk_devs(ddi_get_child(head->dip), f, arg, do_locking) ==
33570Sstevel@tonic-gate 		    DDI_WALK_TERMINATE) {
33580Sstevel@tonic-gate 			if (do_locking)
33590Sstevel@tonic-gate 				ndi_devi_exit(head->dip, circ);
33600Sstevel@tonic-gate 			free_list(head);
33610Sstevel@tonic-gate 			return (DDI_WALK_TERMINATE);
33620Sstevel@tonic-gate 		}
33630Sstevel@tonic-gate 		if (do_locking)
33640Sstevel@tonic-gate 			ndi_devi_exit(head->dip, circ);
33650Sstevel@tonic-gate 		kmem_free(head, sizeof (*head));
33660Sstevel@tonic-gate 		head = next;
33670Sstevel@tonic-gate 	}
33680Sstevel@tonic-gate 
33690Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
33700Sstevel@tonic-gate }
33710Sstevel@tonic-gate 
33720Sstevel@tonic-gate /*
33730Sstevel@tonic-gate  * This general-purpose routine traverses the tree of dev_info nodes,
33740Sstevel@tonic-gate  * starting from the given node, and calls the given function for each
33750Sstevel@tonic-gate  * node that it finds with the current node and the pointer arg (which
33760Sstevel@tonic-gate  * can point to a structure of information that the function
33770Sstevel@tonic-gate  * needs) as arguments.
33780Sstevel@tonic-gate  *
33790Sstevel@tonic-gate  * It does the walk a layer at a time, not depth-first. The given function
33800Sstevel@tonic-gate  * must return one of the following values:
33810Sstevel@tonic-gate  *	DDI_WALK_CONTINUE
33820Sstevel@tonic-gate  *	DDI_WALK_PRUNESIB
33830Sstevel@tonic-gate  *	DDI_WALK_PRUNECHILD
33840Sstevel@tonic-gate  *	DDI_WALK_TERMINATE
33850Sstevel@tonic-gate  *
33860Sstevel@tonic-gate  * N.B. Since we walk the sibling list, the caller must ensure that
33870Sstevel@tonic-gate  *	the parent of dip is held against changes, unless the parent
33880Sstevel@tonic-gate  *	is rootnode.  ndi_devi_enter() on the parent is sufficient.
33890Sstevel@tonic-gate  *
33900Sstevel@tonic-gate  *	To avoid deadlock situations, caller must not attempt to
33910Sstevel@tonic-gate  *	configure/unconfigure/remove device node in (*f)(), nor should
33922155Scth  *	it attempt to recurse on other nodes in the system. Any
33932155Scth  *	ndi_devi_enter() done by (*f)() must occur 'at-or-below' the
33942155Scth  *	node entered prior to ddi_walk_devs(). Furthermore, if (*f)()
33952155Scth  *	does any multi-threading (in framework *or* in driver) then the
33962155Scth  *	ndi_devi_enter() calls done by dependent threads must be
33972155Scth  *	'strictly-below'.
33980Sstevel@tonic-gate  *
33990Sstevel@tonic-gate  *	This is not callable from device autoconfiguration routines.
34000Sstevel@tonic-gate  *	They include, but not limited to, _init(9e), _fini(9e), probe(9e),
34010Sstevel@tonic-gate  *	attach(9e), and detach(9e).
34020Sstevel@tonic-gate  */
34030Sstevel@tonic-gate 
34040Sstevel@tonic-gate void
34050Sstevel@tonic-gate ddi_walk_devs(dev_info_t *dip, int (*f)(dev_info_t *, void *), void *arg)
34060Sstevel@tonic-gate {
34070Sstevel@tonic-gate 
34080Sstevel@tonic-gate 	ASSERT(dip == NULL || ddi_get_parent(dip) == NULL ||
34090Sstevel@tonic-gate 		DEVI_BUSY_OWNED(ddi_get_parent(dip)));
34100Sstevel@tonic-gate 
34110Sstevel@tonic-gate 	(void) walk_devs(dip, f, arg, 1);
34120Sstevel@tonic-gate }
34130Sstevel@tonic-gate 
34140Sstevel@tonic-gate /*
34150Sstevel@tonic-gate  * This is a general-purpose routine traverses the per-driver list
34160Sstevel@tonic-gate  * and calls the given function for each node. must return one of
34170Sstevel@tonic-gate  * the following values:
34180Sstevel@tonic-gate  *	DDI_WALK_CONTINUE
34190Sstevel@tonic-gate  *	DDI_WALK_TERMINATE
34200Sstevel@tonic-gate  *
34210Sstevel@tonic-gate  * N.B. The same restrictions from ddi_walk_devs() apply.
34220Sstevel@tonic-gate  */
34230Sstevel@tonic-gate 
34240Sstevel@tonic-gate void
34250Sstevel@tonic-gate e_ddi_walk_driver(char *drv, int (*f)(dev_info_t *, void *), void *arg)
34260Sstevel@tonic-gate {
34270Sstevel@tonic-gate 	major_t major;
34280Sstevel@tonic-gate 	struct devnames *dnp;
34290Sstevel@tonic-gate 	dev_info_t *dip;
34300Sstevel@tonic-gate 
34310Sstevel@tonic-gate 	major = ddi_name_to_major(drv);
34320Sstevel@tonic-gate 	if (major == (major_t)-1)
34330Sstevel@tonic-gate 		return;
34340Sstevel@tonic-gate 
34350Sstevel@tonic-gate 	dnp = &devnamesp[major];
34360Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
34370Sstevel@tonic-gate 	dip = dnp->dn_head;
34380Sstevel@tonic-gate 	while (dip) {
34390Sstevel@tonic-gate 		ndi_hold_devi(dip);
34400Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
34410Sstevel@tonic-gate 		if ((*f)(dip, arg) == DDI_WALK_TERMINATE) {
34420Sstevel@tonic-gate 			ndi_rele_devi(dip);
34430Sstevel@tonic-gate 			return;
34440Sstevel@tonic-gate 		}
34450Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
34460Sstevel@tonic-gate 		ndi_rele_devi(dip);
34470Sstevel@tonic-gate 		dip = ddi_get_next(dip);
34480Sstevel@tonic-gate 	}
34490Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
34500Sstevel@tonic-gate }
34510Sstevel@tonic-gate 
34520Sstevel@tonic-gate /*
34530Sstevel@tonic-gate  * argument to i_find_devi, a devinfo node search callback function.
34540Sstevel@tonic-gate  */
34550Sstevel@tonic-gate struct match_info {
34560Sstevel@tonic-gate 	dev_info_t	*dip;		/* result */
34570Sstevel@tonic-gate 	char		*nodename;	/* if non-null, nodename must match */
34580Sstevel@tonic-gate 	int		instance;	/* if != -1, instance must match */
34591333Scth 	int		attached;	/* if != 0, i_ddi_devi_attached() */
34600Sstevel@tonic-gate };
34610Sstevel@tonic-gate 
34620Sstevel@tonic-gate static int
34630Sstevel@tonic-gate i_find_devi(dev_info_t *dip, void *arg)
34640Sstevel@tonic-gate {
34650Sstevel@tonic-gate 	struct match_info *info = (struct match_info *)arg;
34660Sstevel@tonic-gate 
34670Sstevel@tonic-gate 	if (((info->nodename == NULL) ||
34680Sstevel@tonic-gate 		(strcmp(ddi_node_name(dip), info->nodename) == 0)) &&
34690Sstevel@tonic-gate 	    ((info->instance == -1) ||
34700Sstevel@tonic-gate 		(ddi_get_instance(dip) == info->instance)) &&
34711333Scth 	    ((info->attached == 0) || i_ddi_devi_attached(dip))) {
34720Sstevel@tonic-gate 		info->dip = dip;
34730Sstevel@tonic-gate 		ndi_hold_devi(dip);
34740Sstevel@tonic-gate 		return (DDI_WALK_TERMINATE);
34750Sstevel@tonic-gate 	}
34760Sstevel@tonic-gate 
34770Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
34780Sstevel@tonic-gate }
34790Sstevel@tonic-gate 
34800Sstevel@tonic-gate /*
34810Sstevel@tonic-gate  * Find dip with a known node name and instance and return with it held
34820Sstevel@tonic-gate  */
34830Sstevel@tonic-gate dev_info_t *
34840Sstevel@tonic-gate ddi_find_devinfo(char *nodename, int instance, int attached)
34850Sstevel@tonic-gate {
34860Sstevel@tonic-gate 	struct match_info	info;
34870Sstevel@tonic-gate 
34880Sstevel@tonic-gate 	info.nodename = nodename;
34890Sstevel@tonic-gate 	info.instance = instance;
34900Sstevel@tonic-gate 	info.attached = attached;
34910Sstevel@tonic-gate 	info.dip = NULL;
34920Sstevel@tonic-gate 
34930Sstevel@tonic-gate 	ddi_walk_devs(ddi_root_node(), i_find_devi, &info);
34940Sstevel@tonic-gate 	return (info.dip);
34950Sstevel@tonic-gate }
34960Sstevel@tonic-gate 
34970Sstevel@tonic-gate /*
34980Sstevel@tonic-gate  * Parse for name, addr, and minor names. Some args may be NULL.
34990Sstevel@tonic-gate  */
35000Sstevel@tonic-gate void
35010Sstevel@tonic-gate i_ddi_parse_name(char *name, char **nodename, char **addrname, char **minorname)
35020Sstevel@tonic-gate {
35030Sstevel@tonic-gate 	char *cp;
35040Sstevel@tonic-gate 	static char nulladdrname[] = "";
35050Sstevel@tonic-gate 
35060Sstevel@tonic-gate 	/* default values */
35070Sstevel@tonic-gate 	if (nodename)
35080Sstevel@tonic-gate 		*nodename = name;
35090Sstevel@tonic-gate 	if (addrname)
35100Sstevel@tonic-gate 		*addrname = nulladdrname;
35110Sstevel@tonic-gate 	if (minorname)
35120Sstevel@tonic-gate 		*minorname = NULL;
35130Sstevel@tonic-gate 
35140Sstevel@tonic-gate 	cp = name;
35150Sstevel@tonic-gate 	while (*cp != '\0') {
35160Sstevel@tonic-gate 		if (addrname && *cp == '@') {
35170Sstevel@tonic-gate 			*addrname = cp + 1;
35180Sstevel@tonic-gate 			*cp = '\0';
35190Sstevel@tonic-gate 		} else if (minorname && *cp == ':') {
35200Sstevel@tonic-gate 			*minorname = cp + 1;
35210Sstevel@tonic-gate 			*cp = '\0';
35220Sstevel@tonic-gate 		}
35230Sstevel@tonic-gate 		++cp;
35240Sstevel@tonic-gate 	}
35250Sstevel@tonic-gate }
35260Sstevel@tonic-gate 
35270Sstevel@tonic-gate static char *
35280Sstevel@tonic-gate child_path_to_driver(dev_info_t *parent, char *child_name, char *unit_address)
35290Sstevel@tonic-gate {
35300Sstevel@tonic-gate 	char *p, *drvname = NULL;
35310Sstevel@tonic-gate 	major_t maj;
35320Sstevel@tonic-gate 
35330Sstevel@tonic-gate 	/*
35340Sstevel@tonic-gate 	 * Construct the pathname and ask the implementation
35350Sstevel@tonic-gate 	 * if it can do a driver = f(pathname) for us, if not
35360Sstevel@tonic-gate 	 * we'll just default to using the node-name that
35370Sstevel@tonic-gate 	 * was given to us.  We want to do this first to
35380Sstevel@tonic-gate 	 * allow the platform to use 'generic' names for
35390Sstevel@tonic-gate 	 * legacy device drivers.
35400Sstevel@tonic-gate 	 */
35410Sstevel@tonic-gate 	p = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
35420Sstevel@tonic-gate 	(void) ddi_pathname(parent, p);
35430Sstevel@tonic-gate 	(void) strcat(p, "/");
35440Sstevel@tonic-gate 	(void) strcat(p, child_name);
35450Sstevel@tonic-gate 	if (unit_address && *unit_address) {
35460Sstevel@tonic-gate 		(void) strcat(p, "@");
35470Sstevel@tonic-gate 		(void) strcat(p, unit_address);
35480Sstevel@tonic-gate 	}
35490Sstevel@tonic-gate 
35500Sstevel@tonic-gate 	/*
35510Sstevel@tonic-gate 	 * Get the binding. If there is none, return the child_name
35520Sstevel@tonic-gate 	 * and let the caller deal with it.
35530Sstevel@tonic-gate 	 */
35540Sstevel@tonic-gate 	maj = path_to_major(p);
35550Sstevel@tonic-gate 
35560Sstevel@tonic-gate 	kmem_free(p, MAXPATHLEN);
35570Sstevel@tonic-gate 
35580Sstevel@tonic-gate 	if (maj != (major_t)-1)
35590Sstevel@tonic-gate 		drvname = ddi_major_to_name(maj);
35600Sstevel@tonic-gate 	if (drvname == NULL)
35610Sstevel@tonic-gate 		drvname = child_name;
35620Sstevel@tonic-gate 
35630Sstevel@tonic-gate 	return (drvname);
35640Sstevel@tonic-gate }
35650Sstevel@tonic-gate 
35660Sstevel@tonic-gate 
35670Sstevel@tonic-gate /*
35680Sstevel@tonic-gate  * Given the pathname of a device, fill in the dev_info_t value and/or the
35690Sstevel@tonic-gate  * dev_t value and/or the spectype, depending on which parameters are non-NULL.
35700Sstevel@tonic-gate  * If there is an error, this function returns -1.
35710Sstevel@tonic-gate  *
35720Sstevel@tonic-gate  * NOTE: If this function returns the dev_info_t structure, then it
35730Sstevel@tonic-gate  * does so with a hold on the devi. Caller should ensure that they get
35740Sstevel@tonic-gate  * decremented via ddi_release_devi() or ndi_rele_devi();
35750Sstevel@tonic-gate  *
35760Sstevel@tonic-gate  * This function can be invoked in the boot case for a pathname without
35770Sstevel@tonic-gate  * device argument (:xxxx), traditionally treated as a minor name.
35780Sstevel@tonic-gate  * In this case, we do the following
35790Sstevel@tonic-gate  * (1) search the minor node of type DDM_DEFAULT.
35800Sstevel@tonic-gate  * (2) if no DDM_DEFAULT minor exists, then the first non-alias minor is chosen.
35810Sstevel@tonic-gate  * (3) if neither exists, a dev_t is faked with minor number = instance.
35820Sstevel@tonic-gate  * As of S9 FCS, no instance of #1 exists. #2 is used by several platforms
35830Sstevel@tonic-gate  * to default the boot partition to :a possibly by other OBP definitions.
35840Sstevel@tonic-gate  * #3 is used for booting off network interfaces, most SPARC network
35850Sstevel@tonic-gate  * drivers support Style-2 only, so only DDM_ALIAS minor exists.
35860Sstevel@tonic-gate  *
35870Sstevel@tonic-gate  * It is possible for OBP to present device args at the end of the path as
35880Sstevel@tonic-gate  * well as in the middle. For example, with IB the following strings are
35890Sstevel@tonic-gate  * valid boot paths.
35900Sstevel@tonic-gate  *	a /pci@8,700000/ib@1,2:port=1,pkey=ff,dhcp,...
35910Sstevel@tonic-gate  *	b /pci@8,700000/ib@1,1:port=1/ioc@xxxxxx,yyyyyyy:dhcp
35920Sstevel@tonic-gate  * Case (a), we first look for minor node "port=1,pkey...".
35930Sstevel@tonic-gate  * Failing that, we will pass "port=1,pkey..." to the bus_config
35940Sstevel@tonic-gate  * entry point of ib (HCA) driver.
35950Sstevel@tonic-gate  * Case (b), configure ib@1,1 as usual. Then invoke ib's bus_config
35960Sstevel@tonic-gate  * with argument "ioc@xxxxxxx,yyyyyyy:port=1". After configuring
35970Sstevel@tonic-gate  * the ioc, look for minor node dhcp. If not found, pass ":dhcp"
35980Sstevel@tonic-gate  * to ioc's bus_config entry point.
35990Sstevel@tonic-gate  */
36000Sstevel@tonic-gate int
36010Sstevel@tonic-gate resolve_pathname(char *pathname,
36020Sstevel@tonic-gate 	dev_info_t **dipp, dev_t *devtp, int *spectypep)
36030Sstevel@tonic-gate {
36040Sstevel@tonic-gate 	int error;
36050Sstevel@tonic-gate 	dev_info_t *parent, *child;
36060Sstevel@tonic-gate 	struct pathname pn;
36070Sstevel@tonic-gate 	char *component, *config_name;
36080Sstevel@tonic-gate 	char *minorname = NULL;
36090Sstevel@tonic-gate 	char *prev_minor = NULL;
36100Sstevel@tonic-gate 	dev_t devt = NODEV;
36110Sstevel@tonic-gate 	int spectype;
36120Sstevel@tonic-gate 	struct ddi_minor_data *dmn;
36130Sstevel@tonic-gate 
36140Sstevel@tonic-gate 	if (*pathname != '/')
36150Sstevel@tonic-gate 		return (EINVAL);
36160Sstevel@tonic-gate 	parent = ddi_root_node();	/* Begin at the top of the tree */
36170Sstevel@tonic-gate 
36180Sstevel@tonic-gate 	if (error = pn_get(pathname, UIO_SYSSPACE, &pn))
36190Sstevel@tonic-gate 		return (error);
36200Sstevel@tonic-gate 	pn_skipslash(&pn);
36210Sstevel@tonic-gate 
36221333Scth 	ASSERT(i_ddi_devi_attached(parent));
36230Sstevel@tonic-gate 	ndi_hold_devi(parent);
36240Sstevel@tonic-gate 
36250Sstevel@tonic-gate 	component = kmem_alloc(MAXNAMELEN, KM_SLEEP);
36260Sstevel@tonic-gate 	config_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
36270Sstevel@tonic-gate 
36280Sstevel@tonic-gate 	while (pn_pathleft(&pn)) {
36290Sstevel@tonic-gate 		/* remember prev minor (:xxx) in the middle of path */
36300Sstevel@tonic-gate 		if (minorname)
36310Sstevel@tonic-gate 			prev_minor = i_ddi_strdup(minorname, KM_SLEEP);
36320Sstevel@tonic-gate 
36330Sstevel@tonic-gate 		/* Get component and chop off minorname */
36340Sstevel@tonic-gate 		(void) pn_getcomponent(&pn, component);
36350Sstevel@tonic-gate 		i_ddi_parse_name(component, NULL, NULL, &minorname);
36360Sstevel@tonic-gate 
36370Sstevel@tonic-gate 		if (prev_minor == NULL) {
36380Sstevel@tonic-gate 			(void) snprintf(config_name, MAXNAMELEN, "%s",
36390Sstevel@tonic-gate 			    component);
36400Sstevel@tonic-gate 		} else {
36410Sstevel@tonic-gate 			(void) snprintf(config_name, MAXNAMELEN, "%s:%s",
36420Sstevel@tonic-gate 			    component, prev_minor);
36430Sstevel@tonic-gate 			kmem_free(prev_minor, strlen(prev_minor) + 1);
36440Sstevel@tonic-gate 			prev_minor = NULL;
36450Sstevel@tonic-gate 		}
36460Sstevel@tonic-gate 
36470Sstevel@tonic-gate 		/*
36480Sstevel@tonic-gate 		 * Find and configure the child
36490Sstevel@tonic-gate 		 */
36500Sstevel@tonic-gate 		if (ndi_devi_config_one(parent, config_name, &child,
36510Sstevel@tonic-gate 		    NDI_PROMNAME | NDI_NO_EVENT) != NDI_SUCCESS) {
36520Sstevel@tonic-gate 			ndi_rele_devi(parent);
36530Sstevel@tonic-gate 			pn_free(&pn);
36540Sstevel@tonic-gate 			kmem_free(component, MAXNAMELEN);
36550Sstevel@tonic-gate 			kmem_free(config_name, MAXNAMELEN);
36560Sstevel@tonic-gate 			return (-1);
36570Sstevel@tonic-gate 		}
36580Sstevel@tonic-gate 
36591333Scth 		ASSERT(i_ddi_devi_attached(child));
36600Sstevel@tonic-gate 		ndi_rele_devi(parent);
36610Sstevel@tonic-gate 		parent = child;
36620Sstevel@tonic-gate 		pn_skipslash(&pn);
36630Sstevel@tonic-gate 	}
36640Sstevel@tonic-gate 
36650Sstevel@tonic-gate 	/*
36660Sstevel@tonic-gate 	 * First look for a minor node matching minorname.
36670Sstevel@tonic-gate 	 * Failing that, try to pass minorname to bus_config().
36680Sstevel@tonic-gate 	 */
36690Sstevel@tonic-gate 	if (minorname && i_ddi_minorname_to_devtspectype(parent,
36700Sstevel@tonic-gate 	    minorname, &devt, &spectype) == DDI_FAILURE) {
36710Sstevel@tonic-gate 		(void) snprintf(config_name, MAXNAMELEN, "%s", minorname);
36720Sstevel@tonic-gate 		if (ndi_devi_config_obp_args(parent,
36730Sstevel@tonic-gate 		    config_name, &child, 0) != NDI_SUCCESS) {
36740Sstevel@tonic-gate 			ndi_rele_devi(parent);
36750Sstevel@tonic-gate 			pn_free(&pn);
36760Sstevel@tonic-gate 			kmem_free(component, MAXNAMELEN);
36770Sstevel@tonic-gate 			kmem_free(config_name, MAXNAMELEN);
36780Sstevel@tonic-gate 			NDI_CONFIG_DEBUG((CE_NOTE,
36790Sstevel@tonic-gate 			    "%s: minor node not found\n", pathname));
36800Sstevel@tonic-gate 			return (-1);
36810Sstevel@tonic-gate 		}
36820Sstevel@tonic-gate 		minorname = NULL;	/* look for default minor */
36831333Scth 		ASSERT(i_ddi_devi_attached(child));
36840Sstevel@tonic-gate 		ndi_rele_devi(parent);
36850Sstevel@tonic-gate 		parent = child;
36860Sstevel@tonic-gate 	}
36870Sstevel@tonic-gate 
36880Sstevel@tonic-gate 	if (devtp || spectypep) {
36890Sstevel@tonic-gate 		if (minorname == NULL) {
36900Sstevel@tonic-gate 			/* search for a default entry */
36910Sstevel@tonic-gate 			mutex_enter(&(DEVI(parent)->devi_lock));
36920Sstevel@tonic-gate 			for (dmn = DEVI(parent)->devi_minor; dmn;
36930Sstevel@tonic-gate 			    dmn = dmn->next) {
36940Sstevel@tonic-gate 				if (dmn->type == DDM_DEFAULT) {
36950Sstevel@tonic-gate 					devt = dmn->ddm_dev;
36960Sstevel@tonic-gate 					spectype = dmn->ddm_spec_type;
36970Sstevel@tonic-gate 					break;
36980Sstevel@tonic-gate 				}
36990Sstevel@tonic-gate 			}
37000Sstevel@tonic-gate 
37010Sstevel@tonic-gate 			if (devt == NODEV) {
37020Sstevel@tonic-gate 				/*
37030Sstevel@tonic-gate 				 * No default minor node, try the first one;
37040Sstevel@tonic-gate 				 * else, assume 1-1 instance-minor mapping
37050Sstevel@tonic-gate 				 */
37060Sstevel@tonic-gate 				dmn = DEVI(parent)->devi_minor;
37070Sstevel@tonic-gate 				if (dmn && ((dmn->type == DDM_MINOR) ||
37080Sstevel@tonic-gate 				    (dmn->type == DDM_INTERNAL_PATH))) {
37090Sstevel@tonic-gate 					devt = dmn->ddm_dev;
37100Sstevel@tonic-gate 					spectype = dmn->ddm_spec_type;
37110Sstevel@tonic-gate 				} else {
37120Sstevel@tonic-gate 					devt = makedevice(
37130Sstevel@tonic-gate 					    DEVI(parent)->devi_major,
37140Sstevel@tonic-gate 					    ddi_get_instance(parent));
37150Sstevel@tonic-gate 					spectype = S_IFCHR;
37160Sstevel@tonic-gate 				}
37170Sstevel@tonic-gate 			}
37180Sstevel@tonic-gate 			mutex_exit(&(DEVI(parent)->devi_lock));
37190Sstevel@tonic-gate 		}
37200Sstevel@tonic-gate 		if (devtp)
37210Sstevel@tonic-gate 			*devtp = devt;
37220Sstevel@tonic-gate 		if (spectypep)
37230Sstevel@tonic-gate 			*spectypep = spectype;
37240Sstevel@tonic-gate 	}
37250Sstevel@tonic-gate 
37260Sstevel@tonic-gate 	pn_free(&pn);
37270Sstevel@tonic-gate 	kmem_free(component, MAXNAMELEN);
37280Sstevel@tonic-gate 	kmem_free(config_name, MAXNAMELEN);
37290Sstevel@tonic-gate 
37300Sstevel@tonic-gate 	/*
37310Sstevel@tonic-gate 	 * If there is no error, return the appropriate parameters
37320Sstevel@tonic-gate 	 */
37330Sstevel@tonic-gate 	if (dipp != NULL)
37340Sstevel@tonic-gate 		*dipp = parent;
37350Sstevel@tonic-gate 	else {
37360Sstevel@tonic-gate 		/*
37370Sstevel@tonic-gate 		 * We should really keep the ref count to keep the node from
37380Sstevel@tonic-gate 		 * detaching but ddi_pathname_to_dev_t() specifies a NULL dipp,
37390Sstevel@tonic-gate 		 * so we have no way of passing back the held dip.  Not holding
37400Sstevel@tonic-gate 		 * the dip allows detaches to occur - which can cause problems
37410Sstevel@tonic-gate 		 * for subsystems which call ddi_pathname_to_dev_t (console).
37420Sstevel@tonic-gate 		 *
37430Sstevel@tonic-gate 		 * Instead of holding the dip, we place a ddi-no-autodetach
37440Sstevel@tonic-gate 		 * property on the node to prevent auto detaching.
37450Sstevel@tonic-gate 		 *
37460Sstevel@tonic-gate 		 * The right fix is to remove ddi_pathname_to_dev_t and replace
37470Sstevel@tonic-gate 		 * it, and all references, with a call that specifies a dipp.
37480Sstevel@tonic-gate 		 * In addition, the callers of this new interfaces would then
37490Sstevel@tonic-gate 		 * need to call ndi_rele_devi when the reference is complete.
37500Sstevel@tonic-gate 		 */
37510Sstevel@tonic-gate 		(void) ddi_prop_update_int(DDI_DEV_T_NONE, parent,
37520Sstevel@tonic-gate 		    DDI_NO_AUTODETACH, 1);
37530Sstevel@tonic-gate 		ndi_rele_devi(parent);
37540Sstevel@tonic-gate 	}
37550Sstevel@tonic-gate 
37560Sstevel@tonic-gate 	return (0);
37570Sstevel@tonic-gate }
37580Sstevel@tonic-gate 
37590Sstevel@tonic-gate /*
37600Sstevel@tonic-gate  * Given the pathname of a device, return the dev_t of the corresponding
37610Sstevel@tonic-gate  * device.  Returns NODEV on failure.
37620Sstevel@tonic-gate  *
37630Sstevel@tonic-gate  * Note that this call sets the DDI_NO_AUTODETACH property on the devinfo node.
37640Sstevel@tonic-gate  */
37650Sstevel@tonic-gate dev_t
37660Sstevel@tonic-gate ddi_pathname_to_dev_t(char *pathname)
37670Sstevel@tonic-gate {
37680Sstevel@tonic-gate 	dev_t devt;
37690Sstevel@tonic-gate 	int error;
37700Sstevel@tonic-gate 
37710Sstevel@tonic-gate 	error = resolve_pathname(pathname, NULL, &devt, NULL);
37720Sstevel@tonic-gate 
37730Sstevel@tonic-gate 	return (error ? NODEV : devt);
37740Sstevel@tonic-gate }
37750Sstevel@tonic-gate 
37760Sstevel@tonic-gate /*
37770Sstevel@tonic-gate  * Translate a prom pathname to kernel devfs pathname.
37780Sstevel@tonic-gate  * Caller is assumed to allocate devfspath memory of
37790Sstevel@tonic-gate  * size at least MAXPATHLEN
37800Sstevel@tonic-gate  *
37810Sstevel@tonic-gate  * The prom pathname may not include minor name, but
37820Sstevel@tonic-gate  * devfs pathname has a minor name portion.
37830Sstevel@tonic-gate  */
37840Sstevel@tonic-gate int
37850Sstevel@tonic-gate i_ddi_prompath_to_devfspath(char *prompath, char *devfspath)
37860Sstevel@tonic-gate {
37870Sstevel@tonic-gate 	dev_t		devt = (dev_t)NODEV;
37880Sstevel@tonic-gate 	dev_info_t	*dip = NULL;
37890Sstevel@tonic-gate 	char		*minor_name = NULL;
37900Sstevel@tonic-gate 	int		spectype;
37910Sstevel@tonic-gate 	int		error;
37920Sstevel@tonic-gate 
37930Sstevel@tonic-gate 	error = resolve_pathname(prompath, &dip, &devt, &spectype);
37940Sstevel@tonic-gate 	if (error)
37950Sstevel@tonic-gate 		return (DDI_FAILURE);
37960Sstevel@tonic-gate 	ASSERT(dip && devt != NODEV);
37970Sstevel@tonic-gate 
37980Sstevel@tonic-gate 	/*
37990Sstevel@tonic-gate 	 * Get in-kernel devfs pathname
38000Sstevel@tonic-gate 	 */
38010Sstevel@tonic-gate 	(void) ddi_pathname(dip, devfspath);
38020Sstevel@tonic-gate 
38030Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
38040Sstevel@tonic-gate 	minor_name = i_ddi_devtspectype_to_minorname(dip, devt, spectype);
38050Sstevel@tonic-gate 	if (minor_name) {
38060Sstevel@tonic-gate 		(void) strcat(devfspath, ":");
38070Sstevel@tonic-gate 		(void) strcat(devfspath, minor_name);
38080Sstevel@tonic-gate 	} else {
38090Sstevel@tonic-gate 		/*
38100Sstevel@tonic-gate 		 * If minor_name is NULL, we have an alias minor node.
38110Sstevel@tonic-gate 		 * So manufacture a path to the corresponding clone minor.
38120Sstevel@tonic-gate 		 */
38130Sstevel@tonic-gate 		(void) snprintf(devfspath, MAXPATHLEN, "%s:%s",
38140Sstevel@tonic-gate 		    CLONE_PATH, ddi_driver_name(dip));
38150Sstevel@tonic-gate 	}
38160Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
38170Sstevel@tonic-gate 
38180Sstevel@tonic-gate 	/* release hold from resolve_pathname() */
38190Sstevel@tonic-gate 	ndi_rele_devi(dip);
38200Sstevel@tonic-gate 	return (0);
38210Sstevel@tonic-gate }
38220Sstevel@tonic-gate 
38230Sstevel@tonic-gate /*
38240Sstevel@tonic-gate  * Reset all the pure leaf drivers on the system at halt time
38250Sstevel@tonic-gate  */
38260Sstevel@tonic-gate static int
38270Sstevel@tonic-gate reset_leaf_device(dev_info_t *dip, void *arg)
38280Sstevel@tonic-gate {
38290Sstevel@tonic-gate 	_NOTE(ARGUNUSED(arg))
38300Sstevel@tonic-gate 	struct dev_ops *ops;
38310Sstevel@tonic-gate 
38320Sstevel@tonic-gate 	/* if the device doesn't need to be reset then there's nothing to do */
38330Sstevel@tonic-gate 	if (!DEVI_NEED_RESET(dip))
38340Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
38350Sstevel@tonic-gate 
38360Sstevel@tonic-gate 	/*
38370Sstevel@tonic-gate 	 * if the device isn't a char/block device or doesn't have a
38380Sstevel@tonic-gate 	 * reset entry point then there's nothing to do.
38390Sstevel@tonic-gate 	 */
38400Sstevel@tonic-gate 	ops = ddi_get_driver(dip);
38410Sstevel@tonic-gate 	if ((ops == NULL) || (ops->devo_cb_ops == NULL) ||
38420Sstevel@tonic-gate 	    (ops->devo_reset == nodev) || (ops->devo_reset == nulldev) ||
38430Sstevel@tonic-gate 	    (ops->devo_reset == NULL))
38440Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
38450Sstevel@tonic-gate 
38460Sstevel@tonic-gate 	if (DEVI_IS_ATTACHING(dip) || DEVI_IS_DETACHING(dip)) {
38470Sstevel@tonic-gate 		static char path[MAXPATHLEN];
38480Sstevel@tonic-gate 
38490Sstevel@tonic-gate 		/*
38500Sstevel@tonic-gate 		 * bad news, this device has blocked in it's attach or
38510Sstevel@tonic-gate 		 * detach routine, which means it not safe to call it's
38520Sstevel@tonic-gate 		 * devo_reset() entry point.
38530Sstevel@tonic-gate 		 */
38540Sstevel@tonic-gate 		cmn_err(CE_WARN, "unable to reset device: %s",
38550Sstevel@tonic-gate 		    ddi_pathname(dip, path));
38560Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
38570Sstevel@tonic-gate 	}
38580Sstevel@tonic-gate 
38590Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_NOTE, "resetting %s%d\n",
38600Sstevel@tonic-gate 		ddi_driver_name(dip), ddi_get_instance(dip)));
38610Sstevel@tonic-gate 
38620Sstevel@tonic-gate 	(void) devi_reset(dip, DDI_RESET_FORCE);
38630Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
38640Sstevel@tonic-gate }
38650Sstevel@tonic-gate 
38660Sstevel@tonic-gate void
38670Sstevel@tonic-gate reset_leaves(void)
38680Sstevel@tonic-gate {
38690Sstevel@tonic-gate 	/*
38700Sstevel@tonic-gate 	 * if we're reached here, the device tree better not be changing.
38710Sstevel@tonic-gate 	 * so either devinfo_freeze better be set or we better be panicing.
38720Sstevel@tonic-gate 	 */
38730Sstevel@tonic-gate 	ASSERT(devinfo_freeze || panicstr);
38740Sstevel@tonic-gate 
38750Sstevel@tonic-gate 	(void) walk_devs(top_devinfo, reset_leaf_device, NULL, 0);
38760Sstevel@tonic-gate }
38770Sstevel@tonic-gate 
38780Sstevel@tonic-gate /*
38790Sstevel@tonic-gate  * devtree_freeze() must be called before reset_leaves() during a
38800Sstevel@tonic-gate  * normal system shutdown.  It attempts to ensure that there are no
38810Sstevel@tonic-gate  * outstanding attach or detach operations in progress when reset_leaves()
38820Sstevel@tonic-gate  * is invoked.  It must be called before the system becomes single-threaded
38830Sstevel@tonic-gate  * because device attach and detach are multi-threaded operations.  (note
38840Sstevel@tonic-gate  * that during system shutdown the system doesn't actually become
38850Sstevel@tonic-gate  * single-thread since other threads still exist, but the shutdown thread
38860Sstevel@tonic-gate  * will disable preemption for itself, raise it's pil, and stop all the
38870Sstevel@tonic-gate  * other cpus in the system there by effectively making the system
38880Sstevel@tonic-gate  * single-threaded.)
38890Sstevel@tonic-gate  */
38900Sstevel@tonic-gate void
38910Sstevel@tonic-gate devtree_freeze(void)
38920Sstevel@tonic-gate {
38930Sstevel@tonic-gate 	int delayed = 0;
38940Sstevel@tonic-gate 
38950Sstevel@tonic-gate 	/* if we're panicing then the device tree isn't going to be changing */
38960Sstevel@tonic-gate 	if (panicstr)
38970Sstevel@tonic-gate 		return;
38980Sstevel@tonic-gate 
38990Sstevel@tonic-gate 	/* stop all dev_info state changes in the device tree */
39000Sstevel@tonic-gate 	devinfo_freeze = gethrtime();
39010Sstevel@tonic-gate 
39020Sstevel@tonic-gate 	/*
39030Sstevel@tonic-gate 	 * if we're not panicing and there are on-going attach or detach
39040Sstevel@tonic-gate 	 * operations, wait for up to 3 seconds for them to finish.  This
39050Sstevel@tonic-gate 	 * is a randomly chosen interval but this should be ok because:
39060Sstevel@tonic-gate 	 * - 3 seconds is very small relative to the deadman timer.
39070Sstevel@tonic-gate 	 * - normal attach and detach operations should be very quick.
39080Sstevel@tonic-gate 	 * - attach and detach operations are fairly rare.
39090Sstevel@tonic-gate 	 */
39100Sstevel@tonic-gate 	while (!panicstr && atomic_add_long_nv(&devinfo_attach_detach, 0) &&
39110Sstevel@tonic-gate 	    (delayed < 3)) {
39120Sstevel@tonic-gate 		delayed += 1;
39130Sstevel@tonic-gate 
39140Sstevel@tonic-gate 		/* do a sleeping wait for one second */
39150Sstevel@tonic-gate 		ASSERT(!servicing_interrupt());
39160Sstevel@tonic-gate 		delay(drv_usectohz(MICROSEC));
39170Sstevel@tonic-gate 	}
39180Sstevel@tonic-gate }
39190Sstevel@tonic-gate 
39200Sstevel@tonic-gate static int
39210Sstevel@tonic-gate bind_dip(dev_info_t *dip, void *arg)
39220Sstevel@tonic-gate {
39230Sstevel@tonic-gate 	_NOTE(ARGUNUSED(arg))
3924*4145Scth 	char	*path;
3925*4145Scth 	major_t	major, pmajor;
3926*4145Scth 
3927*4145Scth 	/*
3928*4145Scth 	 * If the node is currently bound to the wrong driver, try to unbind
3929*4145Scth 	 * so that we can rebind to the correct driver.
3930*4145Scth 	 */
3931*4145Scth 	if (i_ddi_node_state(dip) >= DS_BOUND) {
3932*4145Scth 		major = ddi_compatible_driver_major(dip, NULL);
3933*4145Scth 		if ((DEVI(dip)->devi_major == major) &&
3934*4145Scth 		    (i_ddi_node_state(dip) >= DS_INITIALIZED)) {
3935*4145Scth 			/*
3936*4145Scth 			 * Check for a path-oriented driver alias that
3937*4145Scth 			 * takes precedence over current driver binding.
3938*4145Scth 			 */
3939*4145Scth 			path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3940*4145Scth 			(void) ddi_pathname(dip, path);
3941*4145Scth 			pmajor = ddi_name_to_major(path);
3942*4145Scth 			if ((pmajor != (major_t)-1) &&
3943*4145Scth 			    !(devnamesp[pmajor].dn_flags & DN_DRIVER_REMOVED))
3944*4145Scth 				major = pmajor;
3945*4145Scth 			kmem_free(path, MAXPATHLEN);
3946*4145Scth 		}
3947*4145Scth 
3948*4145Scth 		/* attempt unbind if current driver is incorrect */
3949*4145Scth 		if ((major != (major_t)-1) &&
3950*4145Scth 		    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED) &&
3951*4145Scth 		    (major != DEVI(dip)->devi_major))
3952*4145Scth 			(void) ndi_devi_unbind_driver(dip);
3953*4145Scth 	}
3954*4145Scth 
3955*4145Scth 	/* If unbound, try to bind to a driver */
39560Sstevel@tonic-gate 	if (i_ddi_node_state(dip) < DS_BOUND)
39570Sstevel@tonic-gate 		(void) ndi_devi_bind_driver(dip, 0);
39580Sstevel@tonic-gate 
39590Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
39600Sstevel@tonic-gate }
39610Sstevel@tonic-gate 
39620Sstevel@tonic-gate void
39630Sstevel@tonic-gate i_ddi_bind_devs(void)
39640Sstevel@tonic-gate {
3965*4145Scth 	/* flush devfs so that ndi_devi_unbind_driver will work when possible */
3966*4145Scth 	(void) devfs_clean(top_devinfo, NULL, 0);
3967*4145Scth 
39680Sstevel@tonic-gate 	ddi_walk_devs(top_devinfo, bind_dip, (void *)NULL);
39690Sstevel@tonic-gate }
39700Sstevel@tonic-gate 
39710Sstevel@tonic-gate static int
39720Sstevel@tonic-gate unbind_children(dev_info_t *dip, void *arg)
39730Sstevel@tonic-gate {
39740Sstevel@tonic-gate 	int circ;
39750Sstevel@tonic-gate 	dev_info_t *cdip;
39760Sstevel@tonic-gate 	major_t major = (major_t)(uintptr_t)arg;
39770Sstevel@tonic-gate 
39780Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
39790Sstevel@tonic-gate 	cdip = ddi_get_child(dip);
39800Sstevel@tonic-gate 	/*
39810Sstevel@tonic-gate 	 * We are called either from rem_drv or update_drv.
39820Sstevel@tonic-gate 	 * In both cases, we unbind persistent nodes and destroy
39830Sstevel@tonic-gate 	 * .conf nodes. In the case of rem_drv, this will be the
39840Sstevel@tonic-gate 	 * final state. In the case of update_drv, i_ddi_bind_devs()
39850Sstevel@tonic-gate 	 * will be invoked later to reenumerate (new) driver.conf
39860Sstevel@tonic-gate 	 * rebind persistent nodes.
39870Sstevel@tonic-gate 	 */
39880Sstevel@tonic-gate 	while (cdip) {
39890Sstevel@tonic-gate 		dev_info_t *next = ddi_get_next_sibling(cdip);
39900Sstevel@tonic-gate 		if ((i_ddi_node_state(cdip) > DS_INITIALIZED) ||
39910Sstevel@tonic-gate 		    (ddi_driver_major(cdip) != major)) {
39920Sstevel@tonic-gate 			cdip = next;
39930Sstevel@tonic-gate 			continue;
39940Sstevel@tonic-gate 		}
39950Sstevel@tonic-gate 		(void) ndi_devi_unbind_driver(cdip);
39960Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(cdip) == 0)
39970Sstevel@tonic-gate 			(void) ddi_remove_child(cdip, 0);
39980Sstevel@tonic-gate 		cdip = next;
39990Sstevel@tonic-gate 	}
40000Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
40010Sstevel@tonic-gate 
40020Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
40030Sstevel@tonic-gate }
40040Sstevel@tonic-gate 
40050Sstevel@tonic-gate void
40060Sstevel@tonic-gate i_ddi_unbind_devs(major_t major)
40070Sstevel@tonic-gate {
40080Sstevel@tonic-gate 	ddi_walk_devs(top_devinfo, unbind_children, (void *)(uintptr_t)major);
40090Sstevel@tonic-gate }
40100Sstevel@tonic-gate 
40110Sstevel@tonic-gate /*
40120Sstevel@tonic-gate  * I/O Hotplug control
40130Sstevel@tonic-gate  */
40140Sstevel@tonic-gate 
40150Sstevel@tonic-gate /*
40160Sstevel@tonic-gate  * create and attach a dev_info node from a .conf file spec
40170Sstevel@tonic-gate  */
40180Sstevel@tonic-gate static void
40190Sstevel@tonic-gate init_spec_child(dev_info_t *pdip, struct hwc_spec *specp, uint_t flags)
40200Sstevel@tonic-gate {
40210Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags))
40220Sstevel@tonic-gate 	dev_info_t *dip;
40230Sstevel@tonic-gate 	char *node_name;
40240Sstevel@tonic-gate 
40250Sstevel@tonic-gate 	if (((node_name = specp->hwc_devi_name) == NULL) ||
40260Sstevel@tonic-gate 	    (ddi_name_to_major(node_name) == (major_t)-1)) {
40270Sstevel@tonic-gate 		char *tmp = node_name;
40280Sstevel@tonic-gate 		if (tmp == NULL)
40290Sstevel@tonic-gate 			tmp = "<none>";
40300Sstevel@tonic-gate 		cmn_err(CE_CONT,
40310Sstevel@tonic-gate 		    "init_spec_child: parent=%s, bad spec (%s)\n",
40320Sstevel@tonic-gate 		    ddi_node_name(pdip), tmp);
40330Sstevel@tonic-gate 		return;
40340Sstevel@tonic-gate 	}
40350Sstevel@tonic-gate 
4036789Sahrens 	dip = i_ddi_alloc_node(pdip, node_name, (pnode_t)DEVI_PSEUDO_NODEID,
40370Sstevel@tonic-gate 	    -1, specp->hwc_devi_sys_prop_ptr, KM_SLEEP);
40380Sstevel@tonic-gate 
40390Sstevel@tonic-gate 	if (dip == NULL)
40400Sstevel@tonic-gate 		return;
40410Sstevel@tonic-gate 
40420Sstevel@tonic-gate 	if (ddi_initchild(pdip, dip) != DDI_SUCCESS)
40430Sstevel@tonic-gate 		(void) ddi_remove_child(dip, 0);
40440Sstevel@tonic-gate }
40450Sstevel@tonic-gate 
40460Sstevel@tonic-gate /*
40470Sstevel@tonic-gate  * Lookup hwc specs from hash tables and make children from the spec
40480Sstevel@tonic-gate  * Because some .conf children are "merge" nodes, we also initialize
40490Sstevel@tonic-gate  * .conf children to merge properties onto hardware nodes.
40500Sstevel@tonic-gate  *
40510Sstevel@tonic-gate  * The pdip must be held busy.
40520Sstevel@tonic-gate  */
40530Sstevel@tonic-gate int
40540Sstevel@tonic-gate i_ndi_make_spec_children(dev_info_t *pdip, uint_t flags)
40550Sstevel@tonic-gate {
40560Sstevel@tonic-gate 	extern struct hwc_spec *hwc_get_child_spec(dev_info_t *, major_t);
4057298Scth 	int			circ;
4058298Scth 	struct hwc_spec		*list, *spec;
4059298Scth 
4060298Scth 	ndi_devi_enter(pdip, &circ);
4061298Scth 	if (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN) {
4062298Scth 		ndi_devi_exit(pdip, circ);
40630Sstevel@tonic-gate 		return (DDI_SUCCESS);
4064298Scth 	}
40650Sstevel@tonic-gate 
40660Sstevel@tonic-gate 	list = hwc_get_child_spec(pdip, (major_t)-1);
40670Sstevel@tonic-gate 	for (spec = list; spec != NULL; spec = spec->hwc_next) {
40680Sstevel@tonic-gate 		init_spec_child(pdip, spec, flags);
40690Sstevel@tonic-gate 	}
40700Sstevel@tonic-gate 	hwc_free_spec_list(list);
40710Sstevel@tonic-gate 
40720Sstevel@tonic-gate 	mutex_enter(&DEVI(pdip)->devi_lock);
40730Sstevel@tonic-gate 	DEVI(pdip)->devi_flags |= DEVI_MADE_CHILDREN;
40740Sstevel@tonic-gate 	mutex_exit(&DEVI(pdip)->devi_lock);
4075298Scth 	ndi_devi_exit(pdip, circ);
40760Sstevel@tonic-gate 	return (DDI_SUCCESS);
40770Sstevel@tonic-gate }
40780Sstevel@tonic-gate 
40790Sstevel@tonic-gate /*
40800Sstevel@tonic-gate  * Run initchild on all child nodes such that instance assignment
40810Sstevel@tonic-gate  * for multiport network cards are contiguous.
40820Sstevel@tonic-gate  *
40830Sstevel@tonic-gate  * The pdip must be held busy.
40840Sstevel@tonic-gate  */
40850Sstevel@tonic-gate static void
40860Sstevel@tonic-gate i_ndi_init_hw_children(dev_info_t *pdip, uint_t flags)
40870Sstevel@tonic-gate {
40880Sstevel@tonic-gate 	dev_info_t *dip;
40890Sstevel@tonic-gate 
40900Sstevel@tonic-gate 	ASSERT(DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
40910Sstevel@tonic-gate 
40920Sstevel@tonic-gate 	/* contiguous instance assignment */
40930Sstevel@tonic-gate 	e_ddi_enter_instance();
40940Sstevel@tonic-gate 	dip = ddi_get_child(pdip);
40950Sstevel@tonic-gate 	while (dip) {
40960Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(dip))
40970Sstevel@tonic-gate 			(void) i_ndi_config_node(dip, DS_INITIALIZED, flags);
40980Sstevel@tonic-gate 		dip = ddi_get_next_sibling(dip);
40990Sstevel@tonic-gate 	}
41000Sstevel@tonic-gate 	e_ddi_exit_instance();
41010Sstevel@tonic-gate }
41020Sstevel@tonic-gate 
41030Sstevel@tonic-gate /*
41040Sstevel@tonic-gate  * report device status
41050Sstevel@tonic-gate  */
41060Sstevel@tonic-gate static void
41070Sstevel@tonic-gate i_ndi_devi_report_status_change(dev_info_t *dip, char *path)
41080Sstevel@tonic-gate {
41090Sstevel@tonic-gate 	char *status;
41100Sstevel@tonic-gate 
41110Sstevel@tonic-gate 	if (!DEVI_NEED_REPORT(dip) ||
41120Sstevel@tonic-gate 	    (i_ddi_node_state(dip) < DS_INITIALIZED)) {
41130Sstevel@tonic-gate 		return;
41140Sstevel@tonic-gate 	}
41150Sstevel@tonic-gate 
41160Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
41170Sstevel@tonic-gate 		status = "offline";
41180Sstevel@tonic-gate 	} else if (DEVI_IS_DEVICE_DOWN(dip)) {
41190Sstevel@tonic-gate 		status = "down";
41200Sstevel@tonic-gate 	} else if (DEVI_IS_BUS_QUIESCED(dip)) {
41210Sstevel@tonic-gate 		status = "quiesced";
41220Sstevel@tonic-gate 	} else if (DEVI_IS_BUS_DOWN(dip)) {
41230Sstevel@tonic-gate 		status = "down";
41241333Scth 	} else if (i_ddi_devi_attached(dip)) {
41250Sstevel@tonic-gate 		status = "online";
41260Sstevel@tonic-gate 	} else {
41270Sstevel@tonic-gate 		status = "unknown";
41280Sstevel@tonic-gate 	}
41290Sstevel@tonic-gate 
41300Sstevel@tonic-gate 	if (path == NULL) {
41310Sstevel@tonic-gate 		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
41320Sstevel@tonic-gate 		cmn_err(CE_CONT, "?%s (%s%d) %s\n",
41330Sstevel@tonic-gate 			ddi_pathname(dip, path), ddi_driver_name(dip),
41340Sstevel@tonic-gate 			ddi_get_instance(dip), status);
41350Sstevel@tonic-gate 		kmem_free(path, MAXPATHLEN);
41360Sstevel@tonic-gate 	} else {
41370Sstevel@tonic-gate 		cmn_err(CE_CONT, "?%s (%s%d) %s\n",
41380Sstevel@tonic-gate 			path, ddi_driver_name(dip),
41390Sstevel@tonic-gate 			ddi_get_instance(dip), status);
41400Sstevel@tonic-gate 	}
41410Sstevel@tonic-gate 
4142495Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
41430Sstevel@tonic-gate 	DEVI_REPORT_DONE(dip);
4144495Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
41450Sstevel@tonic-gate }
41460Sstevel@tonic-gate 
41470Sstevel@tonic-gate /*
41480Sstevel@tonic-gate  * log a notification that a dev_info node has been configured.
41490Sstevel@tonic-gate  */
41500Sstevel@tonic-gate static int
41510Sstevel@tonic-gate i_log_devfs_add_devinfo(dev_info_t *dip, uint_t flags)
41520Sstevel@tonic-gate {
41530Sstevel@tonic-gate 	int se_err;
41540Sstevel@tonic-gate 	char *pathname;
41550Sstevel@tonic-gate 	sysevent_t *ev;
41560Sstevel@tonic-gate 	sysevent_id_t eid;
41570Sstevel@tonic-gate 	sysevent_value_t se_val;
41580Sstevel@tonic-gate 	sysevent_attr_list_t *ev_attr_list = NULL;
41590Sstevel@tonic-gate 	char *class_name;
41600Sstevel@tonic-gate 	int no_transport = 0;
41610Sstevel@tonic-gate 
41620Sstevel@tonic-gate 	ASSERT(dip);
41630Sstevel@tonic-gate 
41640Sstevel@tonic-gate 	/*
41650Sstevel@tonic-gate 	 * Invalidate the devinfo snapshot cache
41660Sstevel@tonic-gate 	 */
41670Sstevel@tonic-gate 	i_ddi_di_cache_invalidate(KM_SLEEP);
41680Sstevel@tonic-gate 
41690Sstevel@tonic-gate 	/* do not generate ESC_DEVFS_DEVI_ADD event during boot */
41700Sstevel@tonic-gate 	if (!i_ddi_io_initialized())
41710Sstevel@tonic-gate 		return (DDI_SUCCESS);
41720Sstevel@tonic-gate 
41730Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_ADD, EP_DDI, SE_SLEEP);
41740Sstevel@tonic-gate 
41750Sstevel@tonic-gate 	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
41760Sstevel@tonic-gate 
41770Sstevel@tonic-gate 	(void) ddi_pathname(dip, pathname);
41780Sstevel@tonic-gate 	ASSERT(strlen(pathname));
41790Sstevel@tonic-gate 
41800Sstevel@tonic-gate 	se_val.value_type = SE_DATA_TYPE_STRING;
41810Sstevel@tonic-gate 	se_val.value.sv_string = pathname;
41820Sstevel@tonic-gate 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
41830Sstevel@tonic-gate 	    &se_val, SE_SLEEP) != 0) {
41840Sstevel@tonic-gate 		goto fail;
41850Sstevel@tonic-gate 	}
41860Sstevel@tonic-gate 
41870Sstevel@tonic-gate 	/* add the device class attribute */
41880Sstevel@tonic-gate 	if ((class_name = i_ddi_devi_class(dip)) != NULL) {
41890Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_STRING;
41900Sstevel@tonic-gate 		se_val.value.sv_string = class_name;
41910Sstevel@tonic-gate 
41920Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
41930Sstevel@tonic-gate 		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
41940Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
41950Sstevel@tonic-gate 			goto fail;
41960Sstevel@tonic-gate 		}
41970Sstevel@tonic-gate 	}
41980Sstevel@tonic-gate 
41990Sstevel@tonic-gate 	/*
42000Sstevel@tonic-gate 	 * must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
42010Sstevel@tonic-gate 	 * in which case the branch event will be logged by the caller
42020Sstevel@tonic-gate 	 * after the entire branch has been configured.
42030Sstevel@tonic-gate 	 */
42040Sstevel@tonic-gate 	if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
42050Sstevel@tonic-gate 		/*
42060Sstevel@tonic-gate 		 * Instead of logging a separate branch event just add
42070Sstevel@tonic-gate 		 * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
42080Sstevel@tonic-gate 		 * generate a EC_DEV_BRANCH event.
42090Sstevel@tonic-gate 		 */
42100Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_INT32;
42110Sstevel@tonic-gate 		se_val.value.sv_int32 = 1;
42120Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
42130Sstevel@tonic-gate 		    DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
42140Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
42150Sstevel@tonic-gate 			goto fail;
42160Sstevel@tonic-gate 		}
42170Sstevel@tonic-gate 	}
42180Sstevel@tonic-gate 
42190Sstevel@tonic-gate 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
42200Sstevel@tonic-gate 		sysevent_free_attr(ev_attr_list);
42210Sstevel@tonic-gate 		goto fail;
42220Sstevel@tonic-gate 	}
42230Sstevel@tonic-gate 
42240Sstevel@tonic-gate 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
42250Sstevel@tonic-gate 		if (se_err == SE_NO_TRANSPORT)
42260Sstevel@tonic-gate 			no_transport = 1;
42270Sstevel@tonic-gate 		goto fail;
42280Sstevel@tonic-gate 	}
42290Sstevel@tonic-gate 
42300Sstevel@tonic-gate 	sysevent_free(ev);
42310Sstevel@tonic-gate 	kmem_free(pathname, MAXPATHLEN);
42320Sstevel@tonic-gate 
42330Sstevel@tonic-gate 	return (DDI_SUCCESS);
42340Sstevel@tonic-gate 
42350Sstevel@tonic-gate fail:
42360Sstevel@tonic-gate 	cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_ADD event for %s%s",
42370Sstevel@tonic-gate 	    pathname, (no_transport) ? " (syseventd not responding)" : "");
42380Sstevel@tonic-gate 
42390Sstevel@tonic-gate 	cmn_err(CE_WARN, "/dev may not be current for driver %s. "
42400Sstevel@tonic-gate 	    "Run devfsadm -i %s",
42410Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_driver_name(dip));
42420Sstevel@tonic-gate 
42430Sstevel@tonic-gate 	sysevent_free(ev);
42440Sstevel@tonic-gate 	kmem_free(pathname, MAXPATHLEN);
42450Sstevel@tonic-gate 	return (DDI_SUCCESS);
42460Sstevel@tonic-gate }
42470Sstevel@tonic-gate 
42480Sstevel@tonic-gate /*
42490Sstevel@tonic-gate  * log a notification that a dev_info node has been unconfigured.
42500Sstevel@tonic-gate  */
42510Sstevel@tonic-gate static int
42520Sstevel@tonic-gate i_log_devfs_remove_devinfo(char *pathname, char *class_name, char *driver_name,
42530Sstevel@tonic-gate     int instance, uint_t flags)
42540Sstevel@tonic-gate {
42550Sstevel@tonic-gate 	sysevent_t *ev;
42560Sstevel@tonic-gate 	sysevent_id_t eid;
42570Sstevel@tonic-gate 	sysevent_value_t se_val;
42580Sstevel@tonic-gate 	sysevent_attr_list_t *ev_attr_list = NULL;
42590Sstevel@tonic-gate 	int se_err;
42600Sstevel@tonic-gate 	int no_transport = 0;
42610Sstevel@tonic-gate 
42620Sstevel@tonic-gate 	i_ddi_di_cache_invalidate(KM_SLEEP);
42630Sstevel@tonic-gate 
42640Sstevel@tonic-gate 	if (!i_ddi_io_initialized())
42650Sstevel@tonic-gate 		return (DDI_SUCCESS);
42660Sstevel@tonic-gate 
42670Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_REMOVE, EP_DDI, SE_SLEEP);
42680Sstevel@tonic-gate 
42690Sstevel@tonic-gate 	se_val.value_type = SE_DATA_TYPE_STRING;
42700Sstevel@tonic-gate 	se_val.value.sv_string = pathname;
42710Sstevel@tonic-gate 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
42720Sstevel@tonic-gate 	    &se_val, SE_SLEEP) != 0) {
42730Sstevel@tonic-gate 		goto fail;
42740Sstevel@tonic-gate 	}
42750Sstevel@tonic-gate 
42760Sstevel@tonic-gate 	if (class_name) {
42770Sstevel@tonic-gate 		/* add the device class, driver name and instance attributes */
42780Sstevel@tonic-gate 
42790Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_STRING;
42800Sstevel@tonic-gate 		se_val.value.sv_string = class_name;
42810Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
42820Sstevel@tonic-gate 		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
42830Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
42840Sstevel@tonic-gate 			goto fail;
42850Sstevel@tonic-gate 		}
42860Sstevel@tonic-gate 
42870Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_STRING;
42880Sstevel@tonic-gate 		se_val.value.sv_string = driver_name;
42890Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
42900Sstevel@tonic-gate 		    DEVFS_DRIVER_NAME, &se_val, SE_SLEEP) != 0) {
42910Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
42920Sstevel@tonic-gate 			goto fail;
42930Sstevel@tonic-gate 		}
42940Sstevel@tonic-gate 
42950Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_INT32;
42960Sstevel@tonic-gate 		se_val.value.sv_int32 = instance;
42970Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
42980Sstevel@tonic-gate 		    DEVFS_INSTANCE, &se_val, SE_SLEEP) != 0) {
42990Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
43000Sstevel@tonic-gate 			goto fail;
43010Sstevel@tonic-gate 		}
43020Sstevel@tonic-gate 	}
43030Sstevel@tonic-gate 
43040Sstevel@tonic-gate 	/*
43050Sstevel@tonic-gate 	 * must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
43060Sstevel@tonic-gate 	 * in which case the branch event will be logged by the caller
43070Sstevel@tonic-gate 	 * after the entire branch has been unconfigured.
43080Sstevel@tonic-gate 	 */
43090Sstevel@tonic-gate 	if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
43100Sstevel@tonic-gate 		/*
43110Sstevel@tonic-gate 		 * Instead of logging a separate branch event just add
43120Sstevel@tonic-gate 		 * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
43130Sstevel@tonic-gate 		 * generate a EC_DEV_BRANCH event.
43140Sstevel@tonic-gate 		 */
43150Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_INT32;
43160Sstevel@tonic-gate 		se_val.value.sv_int32 = 1;
43170Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
43180Sstevel@tonic-gate 		    DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
43190Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
43200Sstevel@tonic-gate 			goto fail;
43210Sstevel@tonic-gate 		}
43220Sstevel@tonic-gate 	}
43230Sstevel@tonic-gate 
43240Sstevel@tonic-gate 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
43250Sstevel@tonic-gate 		sysevent_free_attr(ev_attr_list);
43260Sstevel@tonic-gate 		goto fail;
43270Sstevel@tonic-gate 	}
43280Sstevel@tonic-gate 
43290Sstevel@tonic-gate 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
43300Sstevel@tonic-gate 		if (se_err == SE_NO_TRANSPORT)
43310Sstevel@tonic-gate 			no_transport = 1;
43320Sstevel@tonic-gate 		goto fail;
43330Sstevel@tonic-gate 	}
43340Sstevel@tonic-gate 
43350Sstevel@tonic-gate 	sysevent_free(ev);
43360Sstevel@tonic-gate 	return (DDI_SUCCESS);
43370Sstevel@tonic-gate 
43380Sstevel@tonic-gate fail:
43390Sstevel@tonic-gate 	sysevent_free(ev);
43400Sstevel@tonic-gate 	cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_REMOVE event for %s%s",
43410Sstevel@tonic-gate 	    pathname, (no_transport) ? " (syseventd not responding)" : "");
43420Sstevel@tonic-gate 	return (DDI_SUCCESS);
43430Sstevel@tonic-gate }
43440Sstevel@tonic-gate 
43450Sstevel@tonic-gate /*
43460Sstevel@tonic-gate  * log an event that a dev_info branch has been configured or unconfigured.
43470Sstevel@tonic-gate  */
43480Sstevel@tonic-gate static int
43490Sstevel@tonic-gate i_log_devfs_branch(char *node_path, char *subclass)
43500Sstevel@tonic-gate {
43510Sstevel@tonic-gate 	int se_err;
43520Sstevel@tonic-gate 	sysevent_t *ev;
43530Sstevel@tonic-gate 	sysevent_id_t eid;
43540Sstevel@tonic-gate 	sysevent_value_t se_val;
43550Sstevel@tonic-gate 	sysevent_attr_list_t *ev_attr_list = NULL;
43560Sstevel@tonic-gate 	int no_transport = 0;
43570Sstevel@tonic-gate 
43580Sstevel@tonic-gate 	/* do not generate the event during boot */
43590Sstevel@tonic-gate 	if (!i_ddi_io_initialized())
43600Sstevel@tonic-gate 		return (DDI_SUCCESS);
43610Sstevel@tonic-gate 
43620Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, subclass, EP_DDI, SE_SLEEP);
43630Sstevel@tonic-gate 
43640Sstevel@tonic-gate 	se_val.value_type = SE_DATA_TYPE_STRING;
43650Sstevel@tonic-gate 	se_val.value.sv_string = node_path;
43660Sstevel@tonic-gate 
43670Sstevel@tonic-gate 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
43680Sstevel@tonic-gate 	    &se_val, SE_SLEEP) != 0) {
43690Sstevel@tonic-gate 		goto fail;
43700Sstevel@tonic-gate 	}
43710Sstevel@tonic-gate 
43720Sstevel@tonic-gate 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
43730Sstevel@tonic-gate 		sysevent_free_attr(ev_attr_list);
43740Sstevel@tonic-gate 		goto fail;
43750Sstevel@tonic-gate 	}
43760Sstevel@tonic-gate 
43770Sstevel@tonic-gate 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
43780Sstevel@tonic-gate 		if (se_err == SE_NO_TRANSPORT)
43790Sstevel@tonic-gate 			no_transport = 1;
43800Sstevel@tonic-gate 		goto fail;
43810Sstevel@tonic-gate 	}
43820Sstevel@tonic-gate 
43830Sstevel@tonic-gate 	sysevent_free(ev);
43840Sstevel@tonic-gate 	return (DDI_SUCCESS);
43850Sstevel@tonic-gate 
43860Sstevel@tonic-gate fail:
43870Sstevel@tonic-gate 	cmn_err(CE_WARN, "failed to log %s branch event for %s%s",
43880Sstevel@tonic-gate 	    subclass, node_path,
43890Sstevel@tonic-gate 	    (no_transport) ? " (syseventd not responding)" : "");
43900Sstevel@tonic-gate 
43910Sstevel@tonic-gate 	sysevent_free(ev);
43920Sstevel@tonic-gate 	return (DDI_FAILURE);
43930Sstevel@tonic-gate }
43940Sstevel@tonic-gate 
43950Sstevel@tonic-gate /*
43960Sstevel@tonic-gate  * log an event that a dev_info tree branch has been configured.
43970Sstevel@tonic-gate  */
43980Sstevel@tonic-gate static int
43990Sstevel@tonic-gate i_log_devfs_branch_add(dev_info_t *dip)
44000Sstevel@tonic-gate {
44010Sstevel@tonic-gate 	char *node_path;
44020Sstevel@tonic-gate 	int rv;
44030Sstevel@tonic-gate 
44040Sstevel@tonic-gate 	node_path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
44050Sstevel@tonic-gate 	(void) ddi_pathname(dip, node_path);
44060Sstevel@tonic-gate 	rv = i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_ADD);
44070Sstevel@tonic-gate 	kmem_free(node_path, MAXPATHLEN);
44080Sstevel@tonic-gate 
44090Sstevel@tonic-gate 	return (rv);
44100Sstevel@tonic-gate }
44110Sstevel@tonic-gate 
44120Sstevel@tonic-gate /*
44130Sstevel@tonic-gate  * log an event that a dev_info tree branch has been unconfigured.
44140Sstevel@tonic-gate  */
44150Sstevel@tonic-gate static int
44160Sstevel@tonic-gate i_log_devfs_branch_remove(char *node_path)
44170Sstevel@tonic-gate {
44180Sstevel@tonic-gate 	return (i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_REMOVE));
44190Sstevel@tonic-gate }
44200Sstevel@tonic-gate 
44210Sstevel@tonic-gate /*
44220Sstevel@tonic-gate  * enqueue the dip's deviname on the branch event queue.
44230Sstevel@tonic-gate  */
44240Sstevel@tonic-gate static struct brevq_node *
44250Sstevel@tonic-gate brevq_enqueue(struct brevq_node **brevqp, dev_info_t *dip,
44260Sstevel@tonic-gate     struct brevq_node *child)
44270Sstevel@tonic-gate {
44280Sstevel@tonic-gate 	struct brevq_node *brn;
44290Sstevel@tonic-gate 	char *deviname;
44300Sstevel@tonic-gate 
44310Sstevel@tonic-gate 	deviname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
44320Sstevel@tonic-gate 	(void) ddi_deviname(dip, deviname);
44330Sstevel@tonic-gate 
44340Sstevel@tonic-gate 	brn = kmem_zalloc(sizeof (*brn), KM_SLEEP);
44351317Scth 	brn->brn_deviname = i_ddi_strdup(deviname, KM_SLEEP);
44360Sstevel@tonic-gate 	kmem_free(deviname, MAXNAMELEN);
44371317Scth 	brn->brn_child = child;
44381317Scth 	brn->brn_sibling = *brevqp;
44390Sstevel@tonic-gate 	*brevqp = brn;
44400Sstevel@tonic-gate 
44410Sstevel@tonic-gate 	return (brn);
44420Sstevel@tonic-gate }
44430Sstevel@tonic-gate 
44440Sstevel@tonic-gate /*
44450Sstevel@tonic-gate  * free the memory allocated for the elements on the branch event queue.
44460Sstevel@tonic-gate  */
44470Sstevel@tonic-gate static void
44480Sstevel@tonic-gate free_brevq(struct brevq_node *brevq)
44490Sstevel@tonic-gate {
44500Sstevel@tonic-gate 	struct brevq_node *brn, *next_brn;
44510Sstevel@tonic-gate 
44520Sstevel@tonic-gate 	for (brn = brevq; brn != NULL; brn = next_brn) {
44531317Scth 		next_brn = brn->brn_sibling;
44541317Scth 		ASSERT(brn->brn_child == NULL);
44551317Scth 		kmem_free(brn->brn_deviname, strlen(brn->brn_deviname) + 1);
44560Sstevel@tonic-gate 		kmem_free(brn, sizeof (*brn));
44570Sstevel@tonic-gate 	}
44580Sstevel@tonic-gate }
44590Sstevel@tonic-gate 
44600Sstevel@tonic-gate /*
44610Sstevel@tonic-gate  * log the events queued up on the branch event queue and free the
44620Sstevel@tonic-gate  * associated memory.
44630Sstevel@tonic-gate  *
44640Sstevel@tonic-gate  * node_path must have been allocated with at least MAXPATHLEN bytes.
44650Sstevel@tonic-gate  */
44660Sstevel@tonic-gate static void
44670Sstevel@tonic-gate log_and_free_brevq(char *node_path, struct brevq_node *brevq)
44680Sstevel@tonic-gate {
44690Sstevel@tonic-gate 	struct brevq_node *brn;
44700Sstevel@tonic-gate 	char *p;
44710Sstevel@tonic-gate 
44720Sstevel@tonic-gate 	p = node_path + strlen(node_path);
44731317Scth 	for (brn = brevq; brn != NULL; brn = brn->brn_sibling) {
44741317Scth 		(void) strcpy(p, brn->brn_deviname);
44750Sstevel@tonic-gate 		(void) i_log_devfs_branch_remove(node_path);
44760Sstevel@tonic-gate 	}
44770Sstevel@tonic-gate 	*p = '\0';
44780Sstevel@tonic-gate 
44790Sstevel@tonic-gate 	free_brevq(brevq);
44800Sstevel@tonic-gate }
44810Sstevel@tonic-gate 
44820Sstevel@tonic-gate /*
44830Sstevel@tonic-gate  * log the events queued up on the branch event queue and free the
44840Sstevel@tonic-gate  * associated memory. Same as the previous function but operates on dip.
44850Sstevel@tonic-gate  */
44860Sstevel@tonic-gate static void
44870Sstevel@tonic-gate log_and_free_brevq_dip(dev_info_t *dip, struct brevq_node *brevq)
44880Sstevel@tonic-gate {
44890Sstevel@tonic-gate 	char *path;
44900Sstevel@tonic-gate 
44910Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
44920Sstevel@tonic-gate 	(void) ddi_pathname(dip, path);
44930Sstevel@tonic-gate 	log_and_free_brevq(path, brevq);
44940Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
44950Sstevel@tonic-gate }
44960Sstevel@tonic-gate 
44970Sstevel@tonic-gate /*
44980Sstevel@tonic-gate  * log the outstanding branch remove events for the grand children of the dip
44990Sstevel@tonic-gate  * and free the associated memory.
45000Sstevel@tonic-gate  */
45010Sstevel@tonic-gate static void
45020Sstevel@tonic-gate log_and_free_br_events_on_grand_children(dev_info_t *dip,
45030Sstevel@tonic-gate     struct brevq_node *brevq)
45040Sstevel@tonic-gate {
45050Sstevel@tonic-gate 	struct brevq_node *brn;
45060Sstevel@tonic-gate 	char *path;
45070Sstevel@tonic-gate 	char *p;
45080Sstevel@tonic-gate 
45090Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
45100Sstevel@tonic-gate 	(void) ddi_pathname(dip, path);
45110Sstevel@tonic-gate 	p = path + strlen(path);
45121317Scth 	for (brn = brevq; brn != NULL; brn = brn->brn_sibling) {
45131317Scth 		if (brn->brn_child) {
45141317Scth 			(void) strcpy(p, brn->brn_deviname);
45150Sstevel@tonic-gate 			/* now path contains the node path to the dip's child */
45161317Scth 			log_and_free_brevq(path, brn->brn_child);
45171317Scth 			brn->brn_child = NULL;
45180Sstevel@tonic-gate 		}
45190Sstevel@tonic-gate 	}
45200Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
45210Sstevel@tonic-gate }
45220Sstevel@tonic-gate 
45230Sstevel@tonic-gate /*
45240Sstevel@tonic-gate  * log and cleanup branch remove events for the grand children of the dip.
45250Sstevel@tonic-gate  */
45260Sstevel@tonic-gate static void
45270Sstevel@tonic-gate cleanup_br_events_on_grand_children(dev_info_t *dip, struct brevq_node **brevqp)
45280Sstevel@tonic-gate {
45290Sstevel@tonic-gate 	dev_info_t *child;
45300Sstevel@tonic-gate 	struct brevq_node *brevq, *brn, *prev_brn, *next_brn;
45310Sstevel@tonic-gate 	char *path;
45320Sstevel@tonic-gate 	int circ;
45330Sstevel@tonic-gate 
45340Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
45350Sstevel@tonic-gate 	prev_brn = NULL;
45360Sstevel@tonic-gate 	brevq = *brevqp;
45370Sstevel@tonic-gate 
45380Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
45390Sstevel@tonic-gate 	for (brn = brevq; brn != NULL; brn = next_brn) {
45401317Scth 		next_brn = brn->brn_sibling;
45410Sstevel@tonic-gate 		for (child = ddi_get_child(dip); child != NULL;
45420Sstevel@tonic-gate 		    child = ddi_get_next_sibling(child)) {
45430Sstevel@tonic-gate 			if (i_ddi_node_state(child) >= DS_INITIALIZED) {
45440Sstevel@tonic-gate 				(void) ddi_deviname(child, path);
45451317Scth 				if (strcmp(path, brn->brn_deviname) == 0)
45460Sstevel@tonic-gate 					break;
45470Sstevel@tonic-gate 			}
45480Sstevel@tonic-gate 		}
45490Sstevel@tonic-gate 
45500Sstevel@tonic-gate 		if (child != NULL && !(DEVI_EVREMOVE(child))) {
45510Sstevel@tonic-gate 			/*
45520Sstevel@tonic-gate 			 * Event state is not REMOVE. So branch remove event
45531317Scth 			 * is not going be generated on brn->brn_child.
45540Sstevel@tonic-gate 			 * If any branch remove events were queued up on
45551317Scth 			 * brn->brn_child log them and remove the brn
45560Sstevel@tonic-gate 			 * from the queue.
45570Sstevel@tonic-gate 			 */
45581317Scth 			if (brn->brn_child) {
45590Sstevel@tonic-gate 				(void) ddi_pathname(dip, path);
45601317Scth 				(void) strcat(path, brn->brn_deviname);
45611317Scth 				log_and_free_brevq(path, brn->brn_child);
45620Sstevel@tonic-gate 			}
45630Sstevel@tonic-gate 
45640Sstevel@tonic-gate 			if (prev_brn)
45651317Scth 				prev_brn->brn_sibling = next_brn;
45660Sstevel@tonic-gate 			else
45670Sstevel@tonic-gate 				*brevqp = next_brn;
45680Sstevel@tonic-gate 
45691317Scth 			kmem_free(brn->brn_deviname,
45701317Scth 			    strlen(brn->brn_deviname) + 1);
45710Sstevel@tonic-gate 			kmem_free(brn, sizeof (*brn));
45720Sstevel@tonic-gate 		} else {
45730Sstevel@tonic-gate 			/*
45740Sstevel@tonic-gate 			 * Free up the outstanding branch remove events
45751317Scth 			 * queued on brn->brn_child since brn->brn_child
45760Sstevel@tonic-gate 			 * itself is eligible for branch remove event.
45770Sstevel@tonic-gate 			 */
45781317Scth 			if (brn->brn_child) {
45791317Scth 				free_brevq(brn->brn_child);
45801317Scth 				brn->brn_child = NULL;
45810Sstevel@tonic-gate 			}
45820Sstevel@tonic-gate 			prev_brn = brn;
45830Sstevel@tonic-gate 		}
45840Sstevel@tonic-gate 	}
45850Sstevel@tonic-gate 
45860Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
45870Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
45880Sstevel@tonic-gate }
45890Sstevel@tonic-gate 
45900Sstevel@tonic-gate static int
45910Sstevel@tonic-gate need_remove_event(dev_info_t *dip, int flags)
45920Sstevel@tonic-gate {
45930Sstevel@tonic-gate 	if ((flags & (NDI_NO_EVENT | NDI_AUTODETACH)) == 0 &&
45940Sstevel@tonic-gate 	    (flags & (NDI_DEVI_OFFLINE | NDI_UNCONFIG | NDI_DEVI_REMOVE)) &&
45950Sstevel@tonic-gate 	    !(DEVI_EVREMOVE(dip)))
45960Sstevel@tonic-gate 		return (1);
45970Sstevel@tonic-gate 	else
45980Sstevel@tonic-gate 		return (0);
45990Sstevel@tonic-gate }
46000Sstevel@tonic-gate 
46010Sstevel@tonic-gate /*
46020Sstevel@tonic-gate  * Unconfigure children/descendants of the dip.
46030Sstevel@tonic-gate  *
46040Sstevel@tonic-gate  * If the operation involves a branch event NDI_BRANCH_EVENT_OP is set
46050Sstevel@tonic-gate  * through out the unconfiguration. On successful return *brevqp is set to
46060Sstevel@tonic-gate  * a queue of dip's child devinames for which branch remove events need
46070Sstevel@tonic-gate  * to be generated.
46080Sstevel@tonic-gate  */
46090Sstevel@tonic-gate static int
46100Sstevel@tonic-gate devi_unconfig_branch(dev_info_t *dip, dev_info_t **dipp, int flags,
46110Sstevel@tonic-gate     struct brevq_node **brevqp)
46120Sstevel@tonic-gate {
46130Sstevel@tonic-gate 	int rval;
46140Sstevel@tonic-gate 
46150Sstevel@tonic-gate 	*brevqp = NULL;
46160Sstevel@tonic-gate 
46170Sstevel@tonic-gate 	if ((!(flags & NDI_BRANCH_EVENT_OP)) && need_remove_event(dip, flags))
46180Sstevel@tonic-gate 		flags |= NDI_BRANCH_EVENT_OP;
46190Sstevel@tonic-gate 
46200Sstevel@tonic-gate 	if (flags & NDI_BRANCH_EVENT_OP) {
46210Sstevel@tonic-gate 		rval = devi_unconfig_common(dip, dipp, flags, (major_t)-1,
46220Sstevel@tonic-gate 		    brevqp);
46230Sstevel@tonic-gate 
46240Sstevel@tonic-gate 		if (rval != NDI_SUCCESS && (*brevqp)) {
46250Sstevel@tonic-gate 			log_and_free_brevq_dip(dip, *brevqp);
46260Sstevel@tonic-gate 			*brevqp = NULL;
46270Sstevel@tonic-gate 		}
46280Sstevel@tonic-gate 	} else
46290Sstevel@tonic-gate 		rval = devi_unconfig_common(dip, dipp, flags, (major_t)-1,
46300Sstevel@tonic-gate 		    NULL);
46310Sstevel@tonic-gate 
46320Sstevel@tonic-gate 	return (rval);
46330Sstevel@tonic-gate }
46340Sstevel@tonic-gate 
46350Sstevel@tonic-gate /*
46360Sstevel@tonic-gate  * If the dip is already bound to a driver transition to DS_INITIALIZED
46370Sstevel@tonic-gate  * in order to generate an event in the case where the node was left in
46380Sstevel@tonic-gate  * DS_BOUND state since boot (never got attached) and the node is now
46390Sstevel@tonic-gate  * being offlined.
46400Sstevel@tonic-gate  */
46410Sstevel@tonic-gate static void
46420Sstevel@tonic-gate init_bound_node_ev(dev_info_t *pdip, dev_info_t *dip, int flags)
46430Sstevel@tonic-gate {
46440Sstevel@tonic-gate 	if (need_remove_event(dip, flags) &&
46450Sstevel@tonic-gate 	    i_ddi_node_state(dip) == DS_BOUND &&
46461333Scth 	    i_ddi_devi_attached(pdip) && !DEVI_IS_DEVICE_OFFLINE(dip))
46470Sstevel@tonic-gate 		(void) ddi_initchild(pdip, dip);
46480Sstevel@tonic-gate }
46490Sstevel@tonic-gate 
46500Sstevel@tonic-gate /*
46510Sstevel@tonic-gate  * attach a node/branch with parent already held busy
46520Sstevel@tonic-gate  */
46530Sstevel@tonic-gate static int
46540Sstevel@tonic-gate devi_attach_node(dev_info_t *dip, uint_t flags)
46550Sstevel@tonic-gate {
46562155Scth 	dev_info_t *pdip = ddi_get_parent(dip);
46572155Scth 
46582155Scth 	ASSERT(pdip && DEVI_BUSY_OWNED(pdip));
46592155Scth 
4660495Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
46610Sstevel@tonic-gate 	if (flags & NDI_DEVI_ONLINE) {
46621333Scth 		if (!i_ddi_devi_attached(dip))
4663495Scth 			DEVI_SET_REPORT(dip);
46640Sstevel@tonic-gate 		DEVI_SET_DEVICE_ONLINE(dip);
46650Sstevel@tonic-gate 	}
46660Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
4667495Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
46680Sstevel@tonic-gate 		return (NDI_FAILURE);
46690Sstevel@tonic-gate 	}
4670495Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
46710Sstevel@tonic-gate 
46720Sstevel@tonic-gate 	if (i_ddi_attachchild(dip) != DDI_SUCCESS) {
4673495Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
46740Sstevel@tonic-gate 		DEVI_SET_EVUNINIT(dip);
4675495Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
4676495Scth 
46770Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(dip))
46780Sstevel@tonic-gate 			(void) ddi_uninitchild(dip);
46790Sstevel@tonic-gate 		else {
46800Sstevel@tonic-gate 			/*
46810Sstevel@tonic-gate 			 * Delete .conf nodes and nodes that are not
46820Sstevel@tonic-gate 			 * well formed.
46830Sstevel@tonic-gate 			 */
46840Sstevel@tonic-gate 			(void) ddi_remove_child(dip, 0);
46850Sstevel@tonic-gate 		}
46860Sstevel@tonic-gate 		return (NDI_FAILURE);
46870Sstevel@tonic-gate 	}
46880Sstevel@tonic-gate 
46890Sstevel@tonic-gate 	i_ndi_devi_report_status_change(dip, NULL);
46900Sstevel@tonic-gate 
46910Sstevel@tonic-gate 	/*
46920Sstevel@tonic-gate 	 * log an event, but not during devfs lookups in which case
46930Sstevel@tonic-gate 	 * NDI_NO_EVENT is set.
46940Sstevel@tonic-gate 	 */
46950Sstevel@tonic-gate 	if ((flags & NDI_NO_EVENT) == 0 && !(DEVI_EVADD(dip))) {
46960Sstevel@tonic-gate 		(void) i_log_devfs_add_devinfo(dip, flags);
4697495Scth 
4698495Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
46990Sstevel@tonic-gate 		DEVI_SET_EVADD(dip);
4700495Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
4701495Scth 	} else if (!(flags & NDI_NO_EVENT_STATE_CHNG)) {
4702495Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
47030Sstevel@tonic-gate 		DEVI_SET_EVADD(dip);
4704495Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
4705495Scth 	}
47060Sstevel@tonic-gate 
47070Sstevel@tonic-gate 	return (NDI_SUCCESS);
47080Sstevel@tonic-gate }
47090Sstevel@tonic-gate 
47100Sstevel@tonic-gate /* internal function to config immediate children */
47110Sstevel@tonic-gate static int
47120Sstevel@tonic-gate config_immediate_children(dev_info_t *pdip, uint_t flags, major_t major)
47130Sstevel@tonic-gate {
47142155Scth 	dev_info_t	*child, *next;
47152155Scth 	int		circ;
47162155Scth 
47171333Scth 	ASSERT(i_ddi_devi_attached(pdip));
47180Sstevel@tonic-gate 
47190Sstevel@tonic-gate 	if (!NEXUS_DRV(ddi_get_driver(pdip)))
47200Sstevel@tonic-gate 		return (NDI_SUCCESS);
47210Sstevel@tonic-gate 
47220Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
47230Sstevel@tonic-gate 	    "config_immediate_children: %s%d (%p), flags=%x\n",
47240Sstevel@tonic-gate 	    ddi_driver_name(pdip), ddi_get_instance(pdip),
47250Sstevel@tonic-gate 	    (void *)pdip, flags));
47260Sstevel@tonic-gate 
47270Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
47280Sstevel@tonic-gate 
47290Sstevel@tonic-gate 	if (flags & NDI_CONFIG_REPROBE) {
47300Sstevel@tonic-gate 		mutex_enter(&DEVI(pdip)->devi_lock);
47310Sstevel@tonic-gate 		DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
47320Sstevel@tonic-gate 		mutex_exit(&DEVI(pdip)->devi_lock);
47330Sstevel@tonic-gate 	}
47340Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(pdip, flags);
47350Sstevel@tonic-gate 	i_ndi_init_hw_children(pdip, flags);
47362155Scth 
47372155Scth 	child = ddi_get_child(pdip);
47382155Scth 	while (child) {
47392155Scth 		/* NOTE: devi_attach_node() may remove the dip */
47402155Scth 		next = ddi_get_next_sibling(child);
47412155Scth 
47422155Scth 		/*
47432155Scth 		 * Configure all nexus nodes or leaf nodes with
47442155Scth 		 * matching driver major
47452155Scth 		 */
47462155Scth 		if ((major == (major_t)-1) ||
47472155Scth 		    (major == ddi_driver_major(child)) ||
47482155Scth 		    ((flags & NDI_CONFIG) && (is_leaf_node(child) == 0)))
47492155Scth 			(void) devi_attach_node(child, flags);
47502155Scth 		child = next;
47512155Scth 	}
47520Sstevel@tonic-gate 
47530Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
47540Sstevel@tonic-gate 
47550Sstevel@tonic-gate 	return (NDI_SUCCESS);
47560Sstevel@tonic-gate }
47570Sstevel@tonic-gate 
47580Sstevel@tonic-gate /* internal function to config grand children */
47590Sstevel@tonic-gate static int
47600Sstevel@tonic-gate config_grand_children(dev_info_t *pdip, uint_t flags, major_t major)
47610Sstevel@tonic-gate {
47620Sstevel@tonic-gate 	struct mt_config_handle *hdl;
47630Sstevel@tonic-gate 
47640Sstevel@tonic-gate 	/* multi-threaded configuration of child nexus */
47650Sstevel@tonic-gate 	hdl = mt_config_init(pdip, NULL, flags, major, MT_CONFIG_OP, NULL);
47660Sstevel@tonic-gate 	mt_config_children(hdl);
47670Sstevel@tonic-gate 
47680Sstevel@tonic-gate 	return (mt_config_fini(hdl));	/* wait for threads to exit */
47690Sstevel@tonic-gate }
47700Sstevel@tonic-gate 
47710Sstevel@tonic-gate /*
47720Sstevel@tonic-gate  * Common function for device tree configuration,
47730Sstevel@tonic-gate  * either BUS_CONFIG_ALL or BUS_CONFIG_DRIVER.
47740Sstevel@tonic-gate  * The NDI_CONFIG flag causes recursive configuration of
47750Sstevel@tonic-gate  * grandchildren, devfs usage should not recurse.
47760Sstevel@tonic-gate  */
47770Sstevel@tonic-gate static int
47780Sstevel@tonic-gate devi_config_common(dev_info_t *dip, int flags, major_t major)
47790Sstevel@tonic-gate {
47800Sstevel@tonic-gate 	int error;
47810Sstevel@tonic-gate 	int (*f)();
47820Sstevel@tonic-gate 
47831333Scth 	if (!i_ddi_devi_attached(dip))
47840Sstevel@tonic-gate 		return (NDI_FAILURE);
47850Sstevel@tonic-gate 
47860Sstevel@tonic-gate 	if (pm_pre_config(dip, NULL) != DDI_SUCCESS)
47870Sstevel@tonic-gate 		return (NDI_FAILURE);
47880Sstevel@tonic-gate 
47890Sstevel@tonic-gate 	if ((DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
47900Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
47910Sstevel@tonic-gate 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
47920Sstevel@tonic-gate 		error = config_immediate_children(dip, flags, major);
47930Sstevel@tonic-gate 	} else {
47940Sstevel@tonic-gate 		/* call bus_config entry point */
47950Sstevel@tonic-gate 		ddi_bus_config_op_t bus_op = (major == (major_t)-1) ?
47960Sstevel@tonic-gate 		    BUS_CONFIG_ALL : BUS_CONFIG_DRIVER;
47970Sstevel@tonic-gate 		error = (*f)(dip,
47980Sstevel@tonic-gate 		    flags, bus_op, (void *)(uintptr_t)major, NULL, 0);
47990Sstevel@tonic-gate 	}
48000Sstevel@tonic-gate 
48010Sstevel@tonic-gate 	if (error) {
48020Sstevel@tonic-gate 		pm_post_config(dip, NULL);
48030Sstevel@tonic-gate 		return (error);
48040Sstevel@tonic-gate 	}
48050Sstevel@tonic-gate 
48060Sstevel@tonic-gate 	/*
48070Sstevel@tonic-gate 	 * Some callers, notably SCSI, need to mark the devfs cache
48080Sstevel@tonic-gate 	 * to be rebuilt together with the config operation.
48090Sstevel@tonic-gate 	 */
48100Sstevel@tonic-gate 	if (flags & NDI_DEVFS_CLEAN)
48110Sstevel@tonic-gate 		(void) devfs_clean(dip, NULL, 0);
48120Sstevel@tonic-gate 
48130Sstevel@tonic-gate 	if (flags & NDI_CONFIG)
48140Sstevel@tonic-gate 		(void) config_grand_children(dip, flags, major);
48150Sstevel@tonic-gate 
48160Sstevel@tonic-gate 	pm_post_config(dip, NULL);
48170Sstevel@tonic-gate 
48180Sstevel@tonic-gate 	return (NDI_SUCCESS);
48190Sstevel@tonic-gate }
48200Sstevel@tonic-gate 
48210Sstevel@tonic-gate /*
48220Sstevel@tonic-gate  * Framework entry point for BUS_CONFIG_ALL
48230Sstevel@tonic-gate  */
48240Sstevel@tonic-gate int
48250Sstevel@tonic-gate ndi_devi_config(dev_info_t *dip, int flags)
48260Sstevel@tonic-gate {
48270Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
48280Sstevel@tonic-gate 	    "ndi_devi_config: par = %s%d (%p), flags = 0x%x\n",
48290Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
48300Sstevel@tonic-gate 
48310Sstevel@tonic-gate 	return (devi_config_common(dip, flags, (major_t)-1));
48320Sstevel@tonic-gate }
48330Sstevel@tonic-gate 
48340Sstevel@tonic-gate /*
48350Sstevel@tonic-gate  * Framework entry point for BUS_CONFIG_DRIVER, bound to major
48360Sstevel@tonic-gate  */
48370Sstevel@tonic-gate int
48380Sstevel@tonic-gate ndi_devi_config_driver(dev_info_t *dip, int flags, major_t major)
48390Sstevel@tonic-gate {
48400Sstevel@tonic-gate 	/* don't abuse this function */
48410Sstevel@tonic-gate 	ASSERT(major != (major_t)-1);
48420Sstevel@tonic-gate 
48430Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
48440Sstevel@tonic-gate 	    "ndi_devi_config_driver: par = %s%d (%p), flags = 0x%x\n",
48450Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
48460Sstevel@tonic-gate 
48470Sstevel@tonic-gate 	return (devi_config_common(dip, flags, major));
48480Sstevel@tonic-gate }
48490Sstevel@tonic-gate 
48500Sstevel@tonic-gate /*
48512155Scth  * Called by nexus drivers to configure its children.
48520Sstevel@tonic-gate  */
48530Sstevel@tonic-gate static int
48542155Scth devi_config_one(dev_info_t *pdip, char *devnm, dev_info_t **cdipp,
48550Sstevel@tonic-gate     uint_t flags, clock_t timeout)
48560Sstevel@tonic-gate {
48572155Scth 	dev_info_t	*vdip = NULL;
48582155Scth 	char		*drivername = NULL;
4859*4145Scth 	int		find_by_addr = 0;
48602155Scth 	char		*name, *addr;
48612155Scth 	int		v_circ, p_circ;
48622155Scth 	clock_t		end_time;	/* 60 sec */
48632155Scth 	int		probed;
48642155Scth 	dev_info_t	*cdip;
48652155Scth 	mdi_pathinfo_t	*cpip;
48662155Scth 
48672155Scth 	*cdipp = NULL;
48680Sstevel@tonic-gate 
48690Sstevel@tonic-gate 	if (!NEXUS_DRV(ddi_get_driver(pdip)))
48700Sstevel@tonic-gate 		return (NDI_FAILURE);
48710Sstevel@tonic-gate 
48720Sstevel@tonic-gate 	/* split name into "name@addr" parts */
48730Sstevel@tonic-gate 	i_ddi_parse_name(devnm, &name, &addr, NULL);
48740Sstevel@tonic-gate 
48752155Scth 	/*
48762155Scth 	 * If the nexus is a pHCI and we are not processing a pHCI from
48772155Scth 	 * mdi bus_config code then we need to know the vHCI.
48782155Scth 	 */
48792155Scth 	if (MDI_PHCI(pdip))
48802155Scth 		vdip = mdi_devi_get_vdip(pdip);
48812155Scth 
48822155Scth 	/*
48832155Scth 	 * We may have a genericname on a system that creates drivername
48842155Scth 	 * nodes (from .conf files).  Find the drivername by nodeid. If we
48852155Scth 	 * can't find a node with devnm as the node name then we search by
48862155Scth 	 * drivername.  This allows an implementation to supply a genericly
48872155Scth 	 * named boot path (disk) and locate drivename nodes (sd).
48882155Scth 	 */
4889*4145Scth 	if (flags & NDI_PROMNAME) {
48902009Sdm120769 		drivername = child_path_to_driver(pdip, name, addr);
4891*4145Scth 		find_by_addr = 1;
4892*4145Scth 	}
48932155Scth 
48942155Scth 	/*
48952155Scth 	 * Determine end_time: This routine should *not* be called with a
48962155Scth 	 * constant non-zero timeout argument, the caller should be adjusting
48972155Scth 	 * the timeout argument relative to when it *started* its asynchronous
48982155Scth 	 * enumeration.
48992155Scth 	 */
49002155Scth 	if (timeout > 0)
49012009Sdm120769 		end_time = ddi_get_lbolt() + timeout;
49022155Scth 
49032009Sdm120769 	for (;;) {
49041961Scth 		/*
49052155Scth 		 * For pHCI, enter (vHCI, pHCI) and search for pathinfo/client
49062155Scth 		 * child - break out of for(;;) loop if child found.
49072155Scth 		 * NOTE: Lock order for ndi_devi_enter is (vHCI, pHCI).
49082155Scth 		 */
49092155Scth 		if (vdip) {
49102155Scth 			/* use mdi_devi_enter ordering */
49112155Scth 			ndi_devi_enter(vdip, &v_circ);
49122155Scth 			ndi_devi_enter(pdip, &p_circ);
49132155Scth 			cpip = mdi_pi_find(pdip, NULL, addr);
49142155Scth 			cdip = mdi_pi_get_client(cpip);
49152155Scth 			if (cdip)
49162155Scth 				break;
49172155Scth 		} else
49182155Scth 			ndi_devi_enter(pdip, &p_circ);
49192155Scth 
49202155Scth 		/*
49212155Scth 		 * When not a  vHCI or not all pHCI devices are required to
49222155Scth 		 * enumerated under the vHCI (NDI_MDI_FALLBACK) search for
49232155Scth 		 * devinfo child.
49240Sstevel@tonic-gate 		 */
49252155Scth 		if ((vdip == NULL) || (flags & NDI_MDI_FALLBACK)) {
49262155Scth 			/* determine if .conf nodes already built */
49272155Scth 			probed = (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
49282155Scth 
49292155Scth 			/*
49302155Scth 			 * Search for child by name, if not found then search
49312155Scth 			 * for a node bound to the drivername driver with the
49322155Scth 			 * specified "@addr". Break out of for(;;) loop if
4933*4145Scth 			 * child found.  To support path-oriented aliases
4934*4145Scth 			 * binding on boot-device, we do a search_by_addr too.
49352155Scth 			 */
49362155Scth again:			(void) i_ndi_make_spec_children(pdip, flags);
49372155Scth 			cdip = find_child_by_name(pdip, name, addr);
49382155Scth 			if ((cdip == NULL) && drivername)
49392155Scth 				cdip = find_child_by_driver(pdip,
49402155Scth 				    drivername, addr);
4941*4145Scth 			if ((cdip == NULL) && find_by_addr)
4942*4145Scth 				cdip = find_child_by_addr(pdip, addr);
49432155Scth 			if (cdip)
49442155Scth 				break;
49452155Scth 
49462155Scth 			/*
49472155Scth 			 * determine if we should reenumerate .conf nodes
49482155Scth 			 * and look for child again.
49492155Scth 			 */
49502155Scth 			if (probed &&
49512155Scth 			    i_ddi_io_initialized() &&
49522155Scth 			    (flags & NDI_CONFIG_REPROBE) &&
49532155Scth 			    ((timeout <= 0) || (ddi_get_lbolt() >= end_time))) {
49542155Scth 				probed = 0;
49552155Scth 				mutex_enter(&DEVI(pdip)->devi_lock);
49562155Scth 				DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
49572155Scth 				mutex_exit(&DEVI(pdip)->devi_lock);
49582155Scth 				goto again;
49592155Scth 			}
49602155Scth 		}
49612155Scth 
49622155Scth 		/* break out of for(;;) if time expired */
49632155Scth 		if ((timeout <= 0) || (ddi_get_lbolt() >= end_time))
49640Sstevel@tonic-gate 			break;
49650Sstevel@tonic-gate 
49660Sstevel@tonic-gate 		/*
49672155Scth 		 * Child not found, exit and wait for asynchronous enumeration
49682155Scth 		 * to add child (or timeout). The addition of a new child (vhci
49692155Scth 		 * or phci) requires the asynchronous enumeration thread to
49702155Scth 		 * ndi_devi_enter/ndi_devi_exit. This exit will signal devi_cv
49712155Scth 		 * and cause us to return from ndi_devi_exit_and_wait, after
49722155Scth 		 * which we loop and search for the requested child again.
49730Sstevel@tonic-gate 		 */
49740Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT,
49750Sstevel@tonic-gate 		    "%s%d: waiting for child %s@%s, timeout %ld",
49760Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
49770Sstevel@tonic-gate 		    name, addr, timeout));
49782155Scth 		if (vdip) {
49792155Scth 			/*
49802155Scth 			 * Mark vHCI for pHCI ndi_devi_exit broadcast.
49812155Scth 			 */
49822155Scth 			mutex_enter(&DEVI(vdip)->devi_lock);
49832155Scth 			DEVI(vdip)->devi_flags |=
49842155Scth 			    DEVI_PHCI_SIGNALS_VHCI;
49852155Scth 			mutex_exit(&DEVI(vdip)->devi_lock);
49862155Scth 			ndi_devi_exit(pdip, p_circ);
49872155Scth 
49882155Scth 			/*
49892155Scth 			 * NB: There is a small race window from above
49902155Scth 			 * ndi_devi_exit() of pdip to cv_wait() in
49912155Scth 			 * ndi_devi_exit_and_wait() which can result in
49922155Scth 			 * not immediately finding a new pHCI child
49932155Scth 			 * of a pHCI that uses NDI_MDI_FAILBACK.
49942155Scth 			 */
49952155Scth 			ndi_devi_exit_and_wait(vdip, v_circ, end_time);
49962155Scth 		} else {
49972155Scth 			ndi_devi_exit_and_wait(pdip, p_circ, end_time);
49982155Scth 		}
49992155Scth 	}
50002155Scth 
50012155Scth 	/* done with paddr, fixup i_ddi_parse_name '@'->'\0' change */
50022155Scth 	if (addr && *addr != '\0')
50030Sstevel@tonic-gate 		*(addr - 1) = '@';
50040Sstevel@tonic-gate 
50052155Scth 	/* attach and hold the child, returning pointer to child */
50062155Scth 	if (cdip && (devi_attach_node(cdip, flags) == NDI_SUCCESS)) {
50072155Scth 		ndi_hold_devi(cdip);
50082155Scth 		*cdipp = cdip;
50092155Scth 	}
50102155Scth 
50112155Scth 	ndi_devi_exit(pdip, p_circ);
50122155Scth 	if (vdip)
50132155Scth 		ndi_devi_exit(vdip, v_circ);
50142155Scth 	return (*cdipp ? NDI_SUCCESS : NDI_FAILURE);
50150Sstevel@tonic-gate }
50160Sstevel@tonic-gate 
50170Sstevel@tonic-gate /*
50180Sstevel@tonic-gate  * Enumerate and attach a child specified by name 'devnm'.
50190Sstevel@tonic-gate  * Called by devfs lookup and DR to perform a BUS_CONFIG_ONE.
50200Sstevel@tonic-gate  * Note: devfs does not make use of NDI_CONFIG to configure
50210Sstevel@tonic-gate  * an entire branch.
50220Sstevel@tonic-gate  */
50230Sstevel@tonic-gate int
50240Sstevel@tonic-gate ndi_devi_config_one(dev_info_t *dip, char *devnm, dev_info_t **dipp, int flags)
50250Sstevel@tonic-gate {
50260Sstevel@tonic-gate 	int error;
50270Sstevel@tonic-gate 	int (*f)();
50280Sstevel@tonic-gate 	int branch_event = 0;
50290Sstevel@tonic-gate 
50300Sstevel@tonic-gate 	ASSERT(dipp);
50311333Scth 	ASSERT(i_ddi_devi_attached(dip));
50320Sstevel@tonic-gate 
50330Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
50340Sstevel@tonic-gate 	    "ndi_devi_config_one: par = %s%d (%p), child = %s\n",
50350Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, devnm));
50360Sstevel@tonic-gate 
50370Sstevel@tonic-gate 	if (pm_pre_config(dip, devnm) != DDI_SUCCESS)
50380Sstevel@tonic-gate 		return (NDI_FAILURE);
50390Sstevel@tonic-gate 
50400Sstevel@tonic-gate 	if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
50410Sstevel@tonic-gate 	    (flags & NDI_CONFIG)) {
50420Sstevel@tonic-gate 		flags |= NDI_BRANCH_EVENT_OP;
50430Sstevel@tonic-gate 		branch_event = 1;
50440Sstevel@tonic-gate 	}
50450Sstevel@tonic-gate 
50460Sstevel@tonic-gate 	if ((DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
50470Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
50480Sstevel@tonic-gate 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
50490Sstevel@tonic-gate 		error = devi_config_one(dip, devnm, dipp, flags, 0);
50500Sstevel@tonic-gate 	} else {
50510Sstevel@tonic-gate 		/* call bus_config entry point */
50520Sstevel@tonic-gate 		error = (*f)(dip, flags, BUS_CONFIG_ONE, (void *)devnm, dipp);
50530Sstevel@tonic-gate 	}
50540Sstevel@tonic-gate 
50550Sstevel@tonic-gate 	if (error || (flags & NDI_CONFIG) == 0) {
50560Sstevel@tonic-gate 		pm_post_config(dip, devnm);
50570Sstevel@tonic-gate 		return (error);
50580Sstevel@tonic-gate 	}
50590Sstevel@tonic-gate 
50600Sstevel@tonic-gate 	/*
5061*4145Scth 	 * DR usage (i.e. call with NDI_CONFIG) recursively configures
50620Sstevel@tonic-gate 	 * grandchildren, performing a BUS_CONFIG_ALL from the node attached
50630Sstevel@tonic-gate 	 * by the BUS_CONFIG_ONE.
50640Sstevel@tonic-gate 	 */
50650Sstevel@tonic-gate 	ASSERT(*dipp);
50660Sstevel@tonic-gate 
50670Sstevel@tonic-gate 	error = devi_config_common(*dipp, flags, (major_t)-1);
50680Sstevel@tonic-gate 
50690Sstevel@tonic-gate 	pm_post_config(dip, devnm);
50700Sstevel@tonic-gate 
50710Sstevel@tonic-gate 	if (branch_event)
50720Sstevel@tonic-gate 		(void) i_log_devfs_branch_add(*dipp);
50730Sstevel@tonic-gate 
50740Sstevel@tonic-gate 	return (error);
50750Sstevel@tonic-gate }
50760Sstevel@tonic-gate 
50770Sstevel@tonic-gate 
50780Sstevel@tonic-gate /*
50790Sstevel@tonic-gate  * Enumerate and attach a child specified by name 'devnm'.
50800Sstevel@tonic-gate  * Called during configure the OBP options. This configures
50810Sstevel@tonic-gate  * only one node.
50820Sstevel@tonic-gate  */
50830Sstevel@tonic-gate static int
50840Sstevel@tonic-gate ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
50850Sstevel@tonic-gate     dev_info_t **childp, int flags)
50860Sstevel@tonic-gate {
50870Sstevel@tonic-gate 	int error;
50880Sstevel@tonic-gate 	int (*f)();
50890Sstevel@tonic-gate 
50900Sstevel@tonic-gate 	ASSERT(childp);
50911333Scth 	ASSERT(i_ddi_devi_attached(parent));
50920Sstevel@tonic-gate 
50930Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_config_obp_args: "
50940Sstevel@tonic-gate 	    "par = %s%d (%p), child = %s\n", ddi_driver_name(parent),
50950Sstevel@tonic-gate 	    ddi_get_instance(parent), (void *)parent, devnm));
50960Sstevel@tonic-gate 
50970Sstevel@tonic-gate 	if ((DEVI(parent)->devi_ops->devo_bus_ops == NULL) ||
50980Sstevel@tonic-gate 	    (DEVI(parent)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
50990Sstevel@tonic-gate 	    (f = DEVI(parent)->devi_ops->devo_bus_ops->bus_config) == NULL) {
51000Sstevel@tonic-gate 		error = NDI_FAILURE;
51010Sstevel@tonic-gate 	} else {
51020Sstevel@tonic-gate 		/* call bus_config entry point */
51030Sstevel@tonic-gate 		error = (*f)(parent, flags,
51040Sstevel@tonic-gate 		    BUS_CONFIG_OBP_ARGS, (void *)devnm, childp);
51050Sstevel@tonic-gate 	}
51060Sstevel@tonic-gate 	return (error);
51070Sstevel@tonic-gate }
51080Sstevel@tonic-gate 
51090Sstevel@tonic-gate 
51100Sstevel@tonic-gate /*
51110Sstevel@tonic-gate  * detach a node with parent already held busy
51120Sstevel@tonic-gate  */
51130Sstevel@tonic-gate static int
51140Sstevel@tonic-gate devi_detach_node(dev_info_t *dip, uint_t flags)
51150Sstevel@tonic-gate {
51160Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
51170Sstevel@tonic-gate 	int ret = NDI_SUCCESS;
51180Sstevel@tonic-gate 	ddi_eventcookie_t cookie;
51190Sstevel@tonic-gate 
51202155Scth 	ASSERT(pdip && DEVI_BUSY_OWNED(pdip));
51212155Scth 
51220Sstevel@tonic-gate 	if (flags & NDI_POST_EVENT) {
51232155Scth 		if (i_ddi_devi_attached(pdip)) {
51240Sstevel@tonic-gate 			if (ddi_get_eventcookie(dip, DDI_DEVI_REMOVE_EVENT,
51250Sstevel@tonic-gate 			    &cookie) == NDI_SUCCESS)
51260Sstevel@tonic-gate 				(void) ndi_post_event(dip, dip, cookie, NULL);
51270Sstevel@tonic-gate 		}
51280Sstevel@tonic-gate 	}
51290Sstevel@tonic-gate 
51300Sstevel@tonic-gate 	if (i_ddi_detachchild(dip, flags) != DDI_SUCCESS)
51310Sstevel@tonic-gate 		return (NDI_FAILURE);
51320Sstevel@tonic-gate 
51330Sstevel@tonic-gate 	if (flags & NDI_AUTODETACH)
51340Sstevel@tonic-gate 		return (NDI_SUCCESS);
51350Sstevel@tonic-gate 
51360Sstevel@tonic-gate 	/*
51370Sstevel@tonic-gate 	 * For DR, even bound nodes may need to have offline
51380Sstevel@tonic-gate 	 * flag set.
51390Sstevel@tonic-gate 	 */
51400Sstevel@tonic-gate 	if (flags & NDI_DEVI_OFFLINE) {
5141495Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
51420Sstevel@tonic-gate 		DEVI_SET_DEVICE_OFFLINE(dip);
5143495Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
51440Sstevel@tonic-gate 	}
51450Sstevel@tonic-gate 
51460Sstevel@tonic-gate 	if (i_ddi_node_state(dip) == DS_INITIALIZED) {
51470Sstevel@tonic-gate 		char *path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
51480Sstevel@tonic-gate 		(void) ddi_pathname(dip, path);
51490Sstevel@tonic-gate 		if (flags & NDI_DEVI_OFFLINE)
51500Sstevel@tonic-gate 			i_ndi_devi_report_status_change(dip, path);
51510Sstevel@tonic-gate 
51520Sstevel@tonic-gate 		if (need_remove_event(dip, flags)) {
51530Sstevel@tonic-gate 			(void) i_log_devfs_remove_devinfo(path,
51540Sstevel@tonic-gate 			    i_ddi_devi_class(dip),
51550Sstevel@tonic-gate 			    (char *)ddi_driver_name(dip),
51560Sstevel@tonic-gate 			    ddi_get_instance(dip),
51570Sstevel@tonic-gate 			    flags);
5158495Scth 			mutex_enter(&(DEVI(dip)->devi_lock));
51590Sstevel@tonic-gate 			DEVI_SET_EVREMOVE(dip);
5160495Scth 			mutex_exit(&(DEVI(dip)->devi_lock));
51610Sstevel@tonic-gate 		}
51620Sstevel@tonic-gate 		kmem_free(path, MAXPATHLEN);
51630Sstevel@tonic-gate 	}
51640Sstevel@tonic-gate 
51650Sstevel@tonic-gate 	if (flags & (NDI_UNCONFIG | NDI_DEVI_REMOVE)) {
51660Sstevel@tonic-gate 		ret = ddi_uninitchild(dip);
51670Sstevel@tonic-gate 		if (ret == NDI_SUCCESS) {
51680Sstevel@tonic-gate 			/*
51690Sstevel@tonic-gate 			 * Remove uninitialized pseudo nodes because
51700Sstevel@tonic-gate 			 * system props are lost and the node cannot be
51710Sstevel@tonic-gate 			 * reattached.
51720Sstevel@tonic-gate 			 */
51730Sstevel@tonic-gate 			if (!ndi_dev_is_persistent_node(dip))
51740Sstevel@tonic-gate 				flags |= NDI_DEVI_REMOVE;
51750Sstevel@tonic-gate 
51760Sstevel@tonic-gate 			if (flags & NDI_DEVI_REMOVE)
51770Sstevel@tonic-gate 				ret = ddi_remove_child(dip, 0);
51780Sstevel@tonic-gate 		}
51790Sstevel@tonic-gate 	}
51800Sstevel@tonic-gate 
51810Sstevel@tonic-gate 	return (ret);
51820Sstevel@tonic-gate }
51830Sstevel@tonic-gate 
51840Sstevel@tonic-gate /*
51850Sstevel@tonic-gate  * unconfigure immediate children of bus nexus device
51860Sstevel@tonic-gate  */
51870Sstevel@tonic-gate static int
51880Sstevel@tonic-gate unconfig_immediate_children(
51890Sstevel@tonic-gate 	dev_info_t *dip,
51900Sstevel@tonic-gate 	dev_info_t **dipp,
51910Sstevel@tonic-gate 	int flags,
51920Sstevel@tonic-gate 	major_t major)
51930Sstevel@tonic-gate {
51942155Scth 	int rv = NDI_SUCCESS;
51952155Scth 	int circ, vcirc;
51960Sstevel@tonic-gate 	dev_info_t *child;
51972155Scth 	dev_info_t *vdip = NULL;
51982155Scth 	dev_info_t *next;
51990Sstevel@tonic-gate 
52000Sstevel@tonic-gate 	ASSERT(dipp == NULL || *dipp == NULL);
52010Sstevel@tonic-gate 
52022155Scth 	/*
52032155Scth 	 * Scan forward to see if we will be processing a pHCI child. If we
52042155Scth 	 * have a child that is a pHCI and vHCI and pHCI are not siblings then
52052155Scth 	 * enter vHCI before parent(pHCI) to prevent deadlock with mpxio
52062155Scth 	 * Client power management operations.
52072155Scth 	 */
52080Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
52092155Scth 	for (child = ddi_get_child(dip); child;
52102155Scth 	    child = ddi_get_next_sibling(child)) {
52112155Scth 		/* skip same nodes we skip below */
52122155Scth 		if (((major != (major_t)-1) &&
52132155Scth 		    (major != ddi_driver_major(child))) ||
52142155Scth 		    ((flags & NDI_AUTODETACH) && !is_leaf_node(child)))
52152155Scth 			continue;
52162155Scth 
52172155Scth 		if (MDI_PHCI(child)) {
52182155Scth 			vdip = mdi_devi_get_vdip(child);
52192155Scth 			/*
52202155Scth 			 * If vHCI and vHCI is not a sibling of pHCI
52212155Scth 			 * then enter in (vHCI, parent(pHCI)) order.
52222155Scth 			 */
52232155Scth 			if (vdip && (ddi_get_parent(vdip) != dip)) {
52242155Scth 				ndi_devi_exit(dip, circ);
52252155Scth 
52262155Scth 				/* use mdi_devi_enter ordering */
52272155Scth 				ndi_devi_enter(vdip, &vcirc);
52282155Scth 				ndi_devi_enter(dip, &circ);
52292155Scth 				break;
52302155Scth 			} else
52312155Scth 				vdip = NULL;
52322155Scth 		}
52332155Scth 	}
52342155Scth 
52350Sstevel@tonic-gate 	child = ddi_get_child(dip);
52360Sstevel@tonic-gate 	while (child) {
52372155Scth 		next = ddi_get_next_sibling(child);
52382155Scth 
52390Sstevel@tonic-gate 		if ((major != (major_t)-1) &&
52400Sstevel@tonic-gate 		    (major != ddi_driver_major(child))) {
52410Sstevel@tonic-gate 			child = next;
52420Sstevel@tonic-gate 			continue;
52430Sstevel@tonic-gate 		}
52440Sstevel@tonic-gate 
52450Sstevel@tonic-gate 		/* skip nexus nodes during autodetach */
52460Sstevel@tonic-gate 		if ((flags & NDI_AUTODETACH) && !is_leaf_node(child)) {
52470Sstevel@tonic-gate 			child = next;
52480Sstevel@tonic-gate 			continue;
52490Sstevel@tonic-gate 		}
52500Sstevel@tonic-gate 
52510Sstevel@tonic-gate 		if (devi_detach_node(child, flags) != NDI_SUCCESS) {
52520Sstevel@tonic-gate 			if (dipp && *dipp == NULL) {
52530Sstevel@tonic-gate 				ndi_hold_devi(child);
52540Sstevel@tonic-gate 				*dipp = child;
52550Sstevel@tonic-gate 			}
52560Sstevel@tonic-gate 			rv = NDI_FAILURE;
52570Sstevel@tonic-gate 		}
52580Sstevel@tonic-gate 
52590Sstevel@tonic-gate 		/*
52600Sstevel@tonic-gate 		 * Continue upon failure--best effort algorithm
52610Sstevel@tonic-gate 		 */
52620Sstevel@tonic-gate 		child = next;
52630Sstevel@tonic-gate 	}
52642155Scth 
52650Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
52662155Scth 	if (vdip)
52672155Scth 		ndi_devi_exit(vdip, vcirc);
52682155Scth 
52690Sstevel@tonic-gate 	return (rv);
52700Sstevel@tonic-gate }
52710Sstevel@tonic-gate 
52720Sstevel@tonic-gate /*
52730Sstevel@tonic-gate  * unconfigure grand children of bus nexus device
52740Sstevel@tonic-gate  */
52750Sstevel@tonic-gate static int
52760Sstevel@tonic-gate unconfig_grand_children(
52770Sstevel@tonic-gate 	dev_info_t *dip,
52780Sstevel@tonic-gate 	dev_info_t **dipp,
52790Sstevel@tonic-gate 	int flags,
52800Sstevel@tonic-gate 	major_t major,
52810Sstevel@tonic-gate 	struct brevq_node **brevqp)
52820Sstevel@tonic-gate {
52830Sstevel@tonic-gate 	struct mt_config_handle *hdl;
52840Sstevel@tonic-gate 
52850Sstevel@tonic-gate 	if (brevqp)
52860Sstevel@tonic-gate 		*brevqp = NULL;
52870Sstevel@tonic-gate 
52880Sstevel@tonic-gate 	/* multi-threaded configuration of child nexus */
52890Sstevel@tonic-gate 	hdl = mt_config_init(dip, dipp, flags, major, MT_UNCONFIG_OP, brevqp);
52900Sstevel@tonic-gate 	mt_config_children(hdl);
52910Sstevel@tonic-gate 
52920Sstevel@tonic-gate 	return (mt_config_fini(hdl));	/* wait for threads to exit */
52930Sstevel@tonic-gate }
52940Sstevel@tonic-gate 
52950Sstevel@tonic-gate /*
52960Sstevel@tonic-gate  * Unconfigure children/descendants of the dip.
52970Sstevel@tonic-gate  *
52980Sstevel@tonic-gate  * If brevqp is not NULL, on return *brevqp is set to a queue of dip's
52990Sstevel@tonic-gate  * child devinames for which branch remove events need to be generated.
53000Sstevel@tonic-gate  */
53010Sstevel@tonic-gate static int
53020Sstevel@tonic-gate devi_unconfig_common(
53030Sstevel@tonic-gate 	dev_info_t *dip,
53040Sstevel@tonic-gate 	dev_info_t **dipp,
53050Sstevel@tonic-gate 	int flags,
53060Sstevel@tonic-gate 	major_t major,
53070Sstevel@tonic-gate 	struct brevq_node **brevqp)
53080Sstevel@tonic-gate {
53090Sstevel@tonic-gate 	int rv;
53100Sstevel@tonic-gate 	int pm_cookie;
53110Sstevel@tonic-gate 	int (*f)();
53120Sstevel@tonic-gate 	ddi_bus_config_op_t bus_op;
53130Sstevel@tonic-gate 
53140Sstevel@tonic-gate 	if (dipp)
53150Sstevel@tonic-gate 		*dipp = NULL;
53160Sstevel@tonic-gate 	if (brevqp)
53170Sstevel@tonic-gate 		*brevqp = NULL;
53180Sstevel@tonic-gate 
53190Sstevel@tonic-gate 	/*
53200Sstevel@tonic-gate 	 * Power up the dip if it is powered off.  If the flag bit
53210Sstevel@tonic-gate 	 * NDI_AUTODETACH is set and the dip is not at its full power,
53220Sstevel@tonic-gate 	 * skip the rest of the branch.
53230Sstevel@tonic-gate 	 */
53240Sstevel@tonic-gate 	if (pm_pre_unconfig(dip, flags, &pm_cookie, NULL) != DDI_SUCCESS)
53250Sstevel@tonic-gate 		return ((flags & NDI_AUTODETACH) ? NDI_SUCCESS :
53260Sstevel@tonic-gate 		    NDI_FAILURE);
53270Sstevel@tonic-gate 
53280Sstevel@tonic-gate 	/*
53290Sstevel@tonic-gate 	 * Some callers, notably SCSI, need to clear out the devfs
53300Sstevel@tonic-gate 	 * cache together with the unconfig to prevent stale entries.
53310Sstevel@tonic-gate 	 */
53320Sstevel@tonic-gate 	if (flags & NDI_DEVFS_CLEAN)
53330Sstevel@tonic-gate 		(void) devfs_clean(dip, NULL, 0);
53340Sstevel@tonic-gate 
53350Sstevel@tonic-gate 	rv = unconfig_grand_children(dip, dipp, flags, major, brevqp);
53360Sstevel@tonic-gate 
53370Sstevel@tonic-gate 	if ((rv != NDI_SUCCESS) && ((flags & NDI_AUTODETACH) == 0)) {
53380Sstevel@tonic-gate 		if (brevqp && *brevqp) {
53390Sstevel@tonic-gate 			log_and_free_br_events_on_grand_children(dip, *brevqp);
53400Sstevel@tonic-gate 			free_brevq(*brevqp);
53410Sstevel@tonic-gate 			*brevqp = NULL;
53420Sstevel@tonic-gate 		}
53430Sstevel@tonic-gate 		pm_post_unconfig(dip, pm_cookie, NULL);
53440Sstevel@tonic-gate 		return (rv);
53450Sstevel@tonic-gate 	}
53460Sstevel@tonic-gate 
53470Sstevel@tonic-gate 	if (dipp && *dipp) {
53480Sstevel@tonic-gate 		ndi_rele_devi(*dipp);
53490Sstevel@tonic-gate 		*dipp = NULL;
53500Sstevel@tonic-gate 	}
53510Sstevel@tonic-gate 
53520Sstevel@tonic-gate 	/*
53530Sstevel@tonic-gate 	 * It is possible to have a detached nexus with children
53540Sstevel@tonic-gate 	 * and grandchildren (for example: a branch consisting
53550Sstevel@tonic-gate 	 * entirely of bound nodes.) Since the nexus is detached
53560Sstevel@tonic-gate 	 * the bus_unconfig entry point cannot be used to remove
53570Sstevel@tonic-gate 	 * or unconfigure the descendants.
53580Sstevel@tonic-gate 	 */
53591333Scth 	if (!i_ddi_devi_attached(dip) ||
53600Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
53610Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
53620Sstevel@tonic-gate 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
53630Sstevel@tonic-gate 		rv = unconfig_immediate_children(dip, dipp, flags, major);
53640Sstevel@tonic-gate 	} else {
53650Sstevel@tonic-gate 		/*
53660Sstevel@tonic-gate 		 * call bus_unconfig entry point
53670Sstevel@tonic-gate 		 * It should reset nexus flags if unconfigure succeeds.
53680Sstevel@tonic-gate 		 */
53690Sstevel@tonic-gate 		bus_op = (major == (major_t)-1) ?
53700Sstevel@tonic-gate 		    BUS_UNCONFIG_ALL : BUS_UNCONFIG_DRIVER;
53710Sstevel@tonic-gate 		rv = (*f)(dip, flags, bus_op, (void *)(uintptr_t)major);
53720Sstevel@tonic-gate 	}
53730Sstevel@tonic-gate 
53740Sstevel@tonic-gate 	pm_post_unconfig(dip, pm_cookie, NULL);
53750Sstevel@tonic-gate 
53760Sstevel@tonic-gate 	if (brevqp && *brevqp)
53770Sstevel@tonic-gate 		cleanup_br_events_on_grand_children(dip, brevqp);
53780Sstevel@tonic-gate 
53790Sstevel@tonic-gate 	return (rv);
53800Sstevel@tonic-gate }
53810Sstevel@tonic-gate 
53820Sstevel@tonic-gate /*
53830Sstevel@tonic-gate  * called by devfs/framework to unconfigure children bound to major
53840Sstevel@tonic-gate  * If NDI_AUTODETACH is specified, this is invoked by either the
53850Sstevel@tonic-gate  * moduninstall daemon or the modunload -i 0 command.
53860Sstevel@tonic-gate  */
53870Sstevel@tonic-gate int
53880Sstevel@tonic-gate ndi_devi_unconfig_driver(dev_info_t *dip, int flags, major_t major)
53890Sstevel@tonic-gate {
53900Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
53910Sstevel@tonic-gate 	    "ndi_devi_unconfig_driver: par = %s%d (%p), flags = 0x%x\n",
53920Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
53930Sstevel@tonic-gate 
53940Sstevel@tonic-gate 	return (devi_unconfig_common(dip, NULL, flags, major, NULL));
53950Sstevel@tonic-gate }
53960Sstevel@tonic-gate 
53970Sstevel@tonic-gate int
53980Sstevel@tonic-gate ndi_devi_unconfig(dev_info_t *dip, int flags)
53990Sstevel@tonic-gate {
54000Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
54010Sstevel@tonic-gate 	    "ndi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
54020Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
54030Sstevel@tonic-gate 
54040Sstevel@tonic-gate 	return (devi_unconfig_common(dip, NULL, flags, (major_t)-1, NULL));
54050Sstevel@tonic-gate }
54060Sstevel@tonic-gate 
54070Sstevel@tonic-gate int
54080Sstevel@tonic-gate e_ddi_devi_unconfig(dev_info_t *dip, dev_info_t **dipp, int flags)
54090Sstevel@tonic-gate {
54100Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
54110Sstevel@tonic-gate 	    "e_ddi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
54120Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
54130Sstevel@tonic-gate 
54140Sstevel@tonic-gate 	return (devi_unconfig_common(dip, dipp, flags, (major_t)-1, NULL));
54150Sstevel@tonic-gate }
54160Sstevel@tonic-gate 
54170Sstevel@tonic-gate /*
54180Sstevel@tonic-gate  * Unconfigure child by name
54190Sstevel@tonic-gate  */
54200Sstevel@tonic-gate static int
54210Sstevel@tonic-gate devi_unconfig_one(dev_info_t *pdip, char *devnm, int flags)
54220Sstevel@tonic-gate {
54232155Scth 	int		rv, circ;
54242155Scth 	dev_info_t	*child;
54252155Scth 	dev_info_t	*vdip = NULL;
54262155Scth 	int		v_circ;
54270Sstevel@tonic-gate 
54280Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
54290Sstevel@tonic-gate 	child = ndi_devi_findchild(pdip, devnm);
54302155Scth 
54312155Scth 	/*
54322155Scth 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
54332155Scth 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
54342155Scth 	 * management operations.
54352155Scth 	 */
54362155Scth 	if (child && MDI_PHCI(child)) {
54372155Scth 		vdip = mdi_devi_get_vdip(child);
54382155Scth 		if (vdip && (ddi_get_parent(vdip) != pdip)) {
54392155Scth 			ndi_devi_exit(pdip, circ);
54402155Scth 
54412155Scth 			/* use mdi_devi_enter ordering */
54422155Scth 			ndi_devi_enter(vdip, &v_circ);
54432155Scth 			ndi_devi_enter(pdip, &circ);
54442155Scth 			child = ndi_devi_findchild(pdip, devnm);
54452155Scth 		} else
54462155Scth 			vdip = NULL;
54472155Scth 	}
54482155Scth 
54492155Scth 	if (child) {
54502155Scth 		rv = devi_detach_node(child, flags);
54512155Scth 	} else {
54520Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
54530Sstevel@tonic-gate 		    "devi_unconfig_one: %s not found\n", devnm));
54542155Scth 		rv = NDI_SUCCESS;
54552155Scth 	}
54562155Scth 
54570Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
54582155Scth 	if (vdip)
54592155Scth 		ndi_devi_exit(pdip, v_circ);
54602155Scth 
54610Sstevel@tonic-gate 	return (rv);
54620Sstevel@tonic-gate }
54630Sstevel@tonic-gate 
54640Sstevel@tonic-gate int
54650Sstevel@tonic-gate ndi_devi_unconfig_one(
54660Sstevel@tonic-gate 	dev_info_t *pdip,
54670Sstevel@tonic-gate 	char *devnm,
54680Sstevel@tonic-gate 	dev_info_t **dipp,
54690Sstevel@tonic-gate 	int flags)
54700Sstevel@tonic-gate {
54712155Scth 	int		(*f)();
54722155Scth 	int		circ, rv;
54732155Scth 	int		pm_cookie;
54742155Scth 	dev_info_t	*child;
54752155Scth 	dev_info_t	*vdip = NULL;
54762155Scth 	int		v_circ;
54770Sstevel@tonic-gate 	struct brevq_node *brevq = NULL;
54780Sstevel@tonic-gate 
54791333Scth 	ASSERT(i_ddi_devi_attached(pdip));
54800Sstevel@tonic-gate 
54810Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
54820Sstevel@tonic-gate 	    "ndi_devi_unconfig_one: par = %s%d (%p), child = %s\n",
54830Sstevel@tonic-gate 	    ddi_driver_name(pdip), ddi_get_instance(pdip),
54840Sstevel@tonic-gate 	    (void *)pdip, devnm));
54850Sstevel@tonic-gate 
54860Sstevel@tonic-gate 	if (pm_pre_unconfig(pdip, flags, &pm_cookie, devnm) != DDI_SUCCESS)
54870Sstevel@tonic-gate 		return (NDI_FAILURE);
54880Sstevel@tonic-gate 
54890Sstevel@tonic-gate 	if (dipp)
54900Sstevel@tonic-gate 		*dipp = NULL;
54910Sstevel@tonic-gate 
54920Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
54930Sstevel@tonic-gate 	child = ndi_devi_findchild(pdip, devnm);
54942155Scth 
54952155Scth 	/*
54962155Scth 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
54972155Scth 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
54982155Scth 	 * management operations.
54992155Scth 	 */
55002155Scth 	if (child && MDI_PHCI(child)) {
55012155Scth 		vdip = mdi_devi_get_vdip(child);
55022155Scth 		if (vdip && (ddi_get_parent(vdip) != pdip)) {
55032155Scth 			ndi_devi_exit(pdip, circ);
55042155Scth 
55052155Scth 			/* use mdi_devi_enter ordering */
55062155Scth 			ndi_devi_enter(vdip, &v_circ);
55072155Scth 			ndi_devi_enter(pdip, &circ);
55082155Scth 			child = ndi_devi_findchild(pdip, devnm);
55092155Scth 		} else
55102155Scth 			vdip = NULL;
55112155Scth 	}
55122155Scth 
55130Sstevel@tonic-gate 	if (child == NULL) {
55140Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_unconfig_one: %s"
55150Sstevel@tonic-gate 		    " not found\n", devnm));
55162155Scth 		rv = NDI_SUCCESS;
55172155Scth 		goto out;
55180Sstevel@tonic-gate 	}
55190Sstevel@tonic-gate 
55200Sstevel@tonic-gate 	/*
55210Sstevel@tonic-gate 	 * Unconfigure children/descendants of named child
55220Sstevel@tonic-gate 	 */
55230Sstevel@tonic-gate 	rv = devi_unconfig_branch(child, dipp, flags | NDI_UNCONFIG, &brevq);
55240Sstevel@tonic-gate 	if (rv != NDI_SUCCESS)
55250Sstevel@tonic-gate 		goto out;
55260Sstevel@tonic-gate 
55270Sstevel@tonic-gate 	init_bound_node_ev(pdip, child, flags);
55280Sstevel@tonic-gate 
55290Sstevel@tonic-gate 	if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
55300Sstevel@tonic-gate 	    (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
55310Sstevel@tonic-gate 	    (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
55320Sstevel@tonic-gate 		rv = devi_detach_node(child, flags);
55330Sstevel@tonic-gate 	} else {
55340Sstevel@tonic-gate 		/* call bus_config entry point */
55350Sstevel@tonic-gate 		rv = (*f)(pdip, flags, BUS_UNCONFIG_ONE, (void *)devnm);
55360Sstevel@tonic-gate 	}
55370Sstevel@tonic-gate 
55380Sstevel@tonic-gate 	if (brevq) {
55390Sstevel@tonic-gate 		if (rv != NDI_SUCCESS)
55400Sstevel@tonic-gate 			log_and_free_brevq_dip(child, brevq);
55410Sstevel@tonic-gate 		else
55420Sstevel@tonic-gate 			free_brevq(brevq);
55430Sstevel@tonic-gate 	}
55440Sstevel@tonic-gate 
55450Sstevel@tonic-gate 	if (dipp && rv != NDI_SUCCESS) {
55460Sstevel@tonic-gate 		ndi_hold_devi(child);
55470Sstevel@tonic-gate 		ASSERT(*dipp == NULL);
55480Sstevel@tonic-gate 		*dipp = child;
55490Sstevel@tonic-gate 	}
55500Sstevel@tonic-gate 
55510Sstevel@tonic-gate out:
55520Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
55532155Scth 	if (vdip)
55542155Scth 		ndi_devi_exit(pdip, v_circ);
55552155Scth 
55560Sstevel@tonic-gate 	pm_post_unconfig(pdip, pm_cookie, devnm);
55570Sstevel@tonic-gate 
55580Sstevel@tonic-gate 	return (rv);
55590Sstevel@tonic-gate }
55600Sstevel@tonic-gate 
55610Sstevel@tonic-gate struct async_arg {
55620Sstevel@tonic-gate 	dev_info_t *dip;
55630Sstevel@tonic-gate 	uint_t flags;
55640Sstevel@tonic-gate };
55650Sstevel@tonic-gate 
55660Sstevel@tonic-gate /*
55670Sstevel@tonic-gate  * Common async handler for:
55680Sstevel@tonic-gate  *	ndi_devi_bind_driver_async
55690Sstevel@tonic-gate  *	ndi_devi_online_async
55700Sstevel@tonic-gate  */
55710Sstevel@tonic-gate static int
55720Sstevel@tonic-gate i_ndi_devi_async_common(dev_info_t *dip, uint_t flags, void (*func)())
55730Sstevel@tonic-gate {
55740Sstevel@tonic-gate 	int tqflag;
55750Sstevel@tonic-gate 	int kmflag;
55760Sstevel@tonic-gate 	struct async_arg *arg;
55770Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
55780Sstevel@tonic-gate 
55790Sstevel@tonic-gate 	ASSERT(pdip);
55800Sstevel@tonic-gate 	ASSERT(DEVI(pdip)->devi_taskq);
55810Sstevel@tonic-gate 	ASSERT(ndi_dev_is_persistent_node(dip));
55820Sstevel@tonic-gate 
55830Sstevel@tonic-gate 	if (flags & NDI_NOSLEEP) {
55840Sstevel@tonic-gate 		kmflag = KM_NOSLEEP;
55850Sstevel@tonic-gate 		tqflag = TQ_NOSLEEP;
55860Sstevel@tonic-gate 	} else {
55870Sstevel@tonic-gate 		kmflag = KM_SLEEP;
55880Sstevel@tonic-gate 		tqflag = TQ_SLEEP;
55890Sstevel@tonic-gate 	}
55900Sstevel@tonic-gate 
55910Sstevel@tonic-gate 	arg = kmem_alloc(sizeof (*arg), kmflag);
55920Sstevel@tonic-gate 	if (arg == NULL)
55930Sstevel@tonic-gate 		goto fail;
55940Sstevel@tonic-gate 
55950Sstevel@tonic-gate 	arg->flags = flags;
55960Sstevel@tonic-gate 	arg->dip = dip;
55970Sstevel@tonic-gate 	if (ddi_taskq_dispatch(DEVI(pdip)->devi_taskq, func, arg, tqflag) ==
55980Sstevel@tonic-gate 	    DDI_SUCCESS) {
55990Sstevel@tonic-gate 		return (NDI_SUCCESS);
56000Sstevel@tonic-gate 	}
56010Sstevel@tonic-gate 
56020Sstevel@tonic-gate fail:
56030Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "%s%d: ddi_taskq_dispatch failed",
56040Sstevel@tonic-gate 	    ddi_driver_name(pdip), ddi_get_instance(pdip)));
56050Sstevel@tonic-gate 
56060Sstevel@tonic-gate 	if (arg)
56070Sstevel@tonic-gate 		kmem_free(arg, sizeof (*arg));
56080Sstevel@tonic-gate 	return (NDI_FAILURE);
56090Sstevel@tonic-gate }
56100Sstevel@tonic-gate 
56110Sstevel@tonic-gate static void
56120Sstevel@tonic-gate i_ndi_devi_bind_driver_cb(struct async_arg *arg)
56130Sstevel@tonic-gate {
56140Sstevel@tonic-gate 	(void) ndi_devi_bind_driver(arg->dip, arg->flags);
56150Sstevel@tonic-gate 	kmem_free(arg, sizeof (*arg));
56160Sstevel@tonic-gate }
56170Sstevel@tonic-gate 
56180Sstevel@tonic-gate int
56190Sstevel@tonic-gate ndi_devi_bind_driver_async(dev_info_t *dip, uint_t flags)
56200Sstevel@tonic-gate {
56210Sstevel@tonic-gate 	return (i_ndi_devi_async_common(dip, flags,
56220Sstevel@tonic-gate 	    (void (*)())i_ndi_devi_bind_driver_cb));
56230Sstevel@tonic-gate }
56240Sstevel@tonic-gate 
56250Sstevel@tonic-gate /*
56260Sstevel@tonic-gate  * place the devinfo in the ONLINE state.
56270Sstevel@tonic-gate  */
56280Sstevel@tonic-gate int
56290Sstevel@tonic-gate ndi_devi_online(dev_info_t *dip, uint_t flags)
56300Sstevel@tonic-gate {
56310Sstevel@tonic-gate 	int circ, rv;
56320Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
56330Sstevel@tonic-gate 	int branch_event = 0;
56340Sstevel@tonic-gate 
56350Sstevel@tonic-gate 	ASSERT(pdip);
56360Sstevel@tonic-gate 
56370Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_online: %s%d (%p)\n",
56380Sstevel@tonic-gate 		ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
56390Sstevel@tonic-gate 
56400Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
56410Sstevel@tonic-gate 	/* bind child before merging .conf nodes */
56420Sstevel@tonic-gate 	rv = i_ndi_config_node(dip, DS_BOUND, flags);
56430Sstevel@tonic-gate 	if (rv != NDI_SUCCESS) {
56440Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
56450Sstevel@tonic-gate 		return (rv);
56460Sstevel@tonic-gate 	}
56470Sstevel@tonic-gate 
56480Sstevel@tonic-gate 	/* merge .conf properties */
56490Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(pdip, flags);
56500Sstevel@tonic-gate 
56512009Sdm120769 	flags |= (NDI_DEVI_ONLINE | NDI_CONFIG);
56520Sstevel@tonic-gate 
56530Sstevel@tonic-gate 	if (flags & NDI_NO_EVENT) {
56540Sstevel@tonic-gate 		/*
56550Sstevel@tonic-gate 		 * Caller is specifically asking for not to generate an event.
56560Sstevel@tonic-gate 		 * Set the following flag so that devi_attach_node() don't
56570Sstevel@tonic-gate 		 * change the event state.
56580Sstevel@tonic-gate 		 */
56590Sstevel@tonic-gate 		flags |= NDI_NO_EVENT_STATE_CHNG;
56600Sstevel@tonic-gate 	}
56610Sstevel@tonic-gate 
56620Sstevel@tonic-gate 	if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
56630Sstevel@tonic-gate 	    ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip))) {
56640Sstevel@tonic-gate 		flags |= NDI_BRANCH_EVENT_OP;
56650Sstevel@tonic-gate 		branch_event = 1;
56660Sstevel@tonic-gate 	}
56670Sstevel@tonic-gate 
56680Sstevel@tonic-gate 	/*
56690Sstevel@tonic-gate 	 * devi_attach_node() may remove dip on failure
56700Sstevel@tonic-gate 	 */
56710Sstevel@tonic-gate 	if ((rv = devi_attach_node(dip, flags)) == NDI_SUCCESS) {
56720Sstevel@tonic-gate 		if ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip)) {
56730Sstevel@tonic-gate 			(void) ndi_devi_config(dip, flags);
56740Sstevel@tonic-gate 		}
56750Sstevel@tonic-gate 
56760Sstevel@tonic-gate 		if (branch_event)
56770Sstevel@tonic-gate 			(void) i_log_devfs_branch_add(dip);
56780Sstevel@tonic-gate 	}
56790Sstevel@tonic-gate 
56800Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
56810Sstevel@tonic-gate 
56820Sstevel@tonic-gate 	/*
56830Sstevel@tonic-gate 	 * Notify devfs that we have a new node. Devfs needs to invalidate
56840Sstevel@tonic-gate 	 * cached directory contents.
56850Sstevel@tonic-gate 	 *
56860Sstevel@tonic-gate 	 * For PCMCIA devices, it is possible the pdip is not fully
56870Sstevel@tonic-gate 	 * attached. In this case, calling back into devfs will
56880Sstevel@tonic-gate 	 * result in a loop or assertion error. Hence, the check
56890Sstevel@tonic-gate 	 * on node state.
56900Sstevel@tonic-gate 	 *
56910Sstevel@tonic-gate 	 * If we own parent lock, this is part of a branch operation.
56920Sstevel@tonic-gate 	 * We skip the devfs_clean() step because the cache invalidation
56930Sstevel@tonic-gate 	 * is done higher up in the device tree.
56940Sstevel@tonic-gate 	 */
56951333Scth 	if (rv == NDI_SUCCESS && i_ddi_devi_attached(pdip) &&
56960Sstevel@tonic-gate 	    !DEVI_BUSY_OWNED(pdip))
56970Sstevel@tonic-gate 		(void) devfs_clean(pdip, NULL, 0);
56980Sstevel@tonic-gate 	return (rv);
56990Sstevel@tonic-gate }
57000Sstevel@tonic-gate 
57010Sstevel@tonic-gate static void
57020Sstevel@tonic-gate i_ndi_devi_online_cb(struct async_arg *arg)
57030Sstevel@tonic-gate {
57040Sstevel@tonic-gate 	(void) ndi_devi_online(arg->dip, arg->flags);
57050Sstevel@tonic-gate 	kmem_free(arg, sizeof (*arg));
57060Sstevel@tonic-gate }
57070Sstevel@tonic-gate 
57080Sstevel@tonic-gate int
57090Sstevel@tonic-gate ndi_devi_online_async(dev_info_t *dip, uint_t flags)
57100Sstevel@tonic-gate {
57110Sstevel@tonic-gate 	/* mark child as need config if requested. */
5712495Scth 	if (flags & NDI_CONFIG) {
5713495Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
57140Sstevel@tonic-gate 		DEVI_SET_NDI_CONFIG(dip);
5715495Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
5716495Scth 	}
57170Sstevel@tonic-gate 
57180Sstevel@tonic-gate 	return (i_ndi_devi_async_common(dip, flags,
57190Sstevel@tonic-gate 	    (void (*)())i_ndi_devi_online_cb));
57200Sstevel@tonic-gate }
57210Sstevel@tonic-gate 
57220Sstevel@tonic-gate /*
57230Sstevel@tonic-gate  * Take a device node Offline
57240Sstevel@tonic-gate  * To take a device Offline means to detach the device instance from
57250Sstevel@tonic-gate  * the driver and prevent devfs requests from re-attaching the device
57260Sstevel@tonic-gate  * instance.
57270Sstevel@tonic-gate  *
57280Sstevel@tonic-gate  * The flag NDI_DEVI_REMOVE causes removes the device node from
57290Sstevel@tonic-gate  * the driver list and the device tree. In this case, the device
57300Sstevel@tonic-gate  * is assumed to be removed from the system.
57310Sstevel@tonic-gate  */
57320Sstevel@tonic-gate int
57330Sstevel@tonic-gate ndi_devi_offline(dev_info_t *dip, uint_t flags)
57340Sstevel@tonic-gate {
57352155Scth 	int		circ, rval = 0;
57362155Scth 	dev_info_t	*pdip = ddi_get_parent(dip);
57372155Scth 	dev_info_t	*vdip = NULL;
57382155Scth 	int		v_circ;
57390Sstevel@tonic-gate 	struct brevq_node *brevq = NULL;
57400Sstevel@tonic-gate 
57410Sstevel@tonic-gate 	ASSERT(pdip);
57420Sstevel@tonic-gate 
57430Sstevel@tonic-gate 	flags |= NDI_DEVI_OFFLINE;
57442155Scth 
57452155Scth 	/*
57462155Scth 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
57472155Scth 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
57482155Scth 	 * management operations.
57492155Scth 	 */
57502155Scth 	if (MDI_PHCI(dip)) {
57512155Scth 		vdip = mdi_devi_get_vdip(dip);
57522155Scth 		if (vdip && (ddi_get_parent(vdip) != pdip))
57532155Scth 			ndi_devi_enter(vdip, &v_circ);
57542155Scth 		else
57552155Scth 			vdip = NULL;
57562155Scth 	}
57570Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
57582155Scth 
57590Sstevel@tonic-gate 	if (i_ddi_node_state(dip) == DS_READY) {
57600Sstevel@tonic-gate 		/*
57610Sstevel@tonic-gate 		 * If dip is in DS_READY state, there may be cached dv_nodes
57620Sstevel@tonic-gate 		 * referencing this dip, so we invoke devfs code path.
57630Sstevel@tonic-gate 		 * Note that we must release busy changing on pdip to
57640Sstevel@tonic-gate 		 * avoid deadlock against devfs.
57650Sstevel@tonic-gate 		 */
57660Sstevel@tonic-gate 		char *devname = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
57670Sstevel@tonic-gate 		(void) ddi_deviname(dip, devname);
57682155Scth 
57690Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
57702155Scth 		if (vdip)
57712155Scth 			ndi_devi_exit(vdip, v_circ);
57720Sstevel@tonic-gate 
57730Sstevel@tonic-gate 		/*
57740Sstevel@tonic-gate 		 * If we own parent lock, this is part of a branch
57750Sstevel@tonic-gate 		 * operation. We skip the devfs_clean() step.
57760Sstevel@tonic-gate 		 */
57770Sstevel@tonic-gate 		if (!DEVI_BUSY_OWNED(pdip))
57780Sstevel@tonic-gate 			rval = devfs_clean(pdip, devname + 1, DV_CLEAN_FORCE);
57790Sstevel@tonic-gate 		kmem_free(devname, MAXNAMELEN + 1);
57800Sstevel@tonic-gate 
57810Sstevel@tonic-gate 		if (rval == 0)
57820Sstevel@tonic-gate 			rval = devi_unconfig_branch(dip, NULL,
57830Sstevel@tonic-gate 			    flags|NDI_UNCONFIG, &brevq);
57840Sstevel@tonic-gate 		if (rval)
57850Sstevel@tonic-gate 			return (NDI_FAILURE);
57860Sstevel@tonic-gate 
57872155Scth 		if (vdip)
57882155Scth 			ndi_devi_enter(vdip, &v_circ);
57890Sstevel@tonic-gate 		ndi_devi_enter(pdip, &circ);
57900Sstevel@tonic-gate 	}
57910Sstevel@tonic-gate 
57920Sstevel@tonic-gate 	init_bound_node_ev(pdip, dip, flags);
57930Sstevel@tonic-gate 
57940Sstevel@tonic-gate 	rval = devi_detach_node(dip, flags);
57950Sstevel@tonic-gate 	if (brevq) {
57960Sstevel@tonic-gate 		if (rval != NDI_SUCCESS)
57970Sstevel@tonic-gate 			log_and_free_brevq_dip(dip, brevq);
57980Sstevel@tonic-gate 		else
57990Sstevel@tonic-gate 			free_brevq(brevq);
58000Sstevel@tonic-gate 	}
58010Sstevel@tonic-gate 
58020Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
58032155Scth 	if (vdip)
58042155Scth 		ndi_devi_exit(vdip, v_circ);
58050Sstevel@tonic-gate 
58060Sstevel@tonic-gate 	return (rval);
58070Sstevel@tonic-gate }
58080Sstevel@tonic-gate 
58090Sstevel@tonic-gate /*
58100Sstevel@tonic-gate  * Find the child dev_info node of parent nexus 'p' whose name
58110Sstevel@tonic-gate  * matches "cname@caddr".  Recommend use of ndi_devi_findchild() instead.
58120Sstevel@tonic-gate  */
58130Sstevel@tonic-gate dev_info_t *
58140Sstevel@tonic-gate ndi_devi_find(dev_info_t *pdip, char *cname, char *caddr)
58150Sstevel@tonic-gate {
58160Sstevel@tonic-gate 	dev_info_t *child;
58170Sstevel@tonic-gate 	int circ;
58180Sstevel@tonic-gate 
58190Sstevel@tonic-gate 	if (pdip == NULL || cname == NULL || caddr == NULL)
58200Sstevel@tonic-gate 		return ((dev_info_t *)NULL);
58210Sstevel@tonic-gate 
58220Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
5823*4145Scth 	child = find_sibling(ddi_get_child(pdip), cname, caddr,
5824*4145Scth 	    FIND_NODE_BY_NODENAME, NULL);
58250Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
58260Sstevel@tonic-gate 	return (child);
58270Sstevel@tonic-gate }
58280Sstevel@tonic-gate 
58290Sstevel@tonic-gate /*
58300Sstevel@tonic-gate  * Find the child dev_info node of parent nexus 'p' whose name
58310Sstevel@tonic-gate  * matches devname "name@addr".  Permits caller to hold the parent.
58320Sstevel@tonic-gate  */
58330Sstevel@tonic-gate dev_info_t *
58340Sstevel@tonic-gate ndi_devi_findchild(dev_info_t *pdip, char *devname)
58350Sstevel@tonic-gate {
58360Sstevel@tonic-gate 	dev_info_t *child;
58370Sstevel@tonic-gate 	char	*cname, *caddr;
58380Sstevel@tonic-gate 	char	*devstr;
58390Sstevel@tonic-gate 
58400Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(pdip));
58410Sstevel@tonic-gate 
58420Sstevel@tonic-gate 	devstr = i_ddi_strdup(devname, KM_SLEEP);
58430Sstevel@tonic-gate 	i_ddi_parse_name(devstr, &cname, &caddr, NULL);
58440Sstevel@tonic-gate 
58450Sstevel@tonic-gate 	if (cname == NULL || caddr == NULL) {
58460Sstevel@tonic-gate 		kmem_free(devstr, strlen(devname)+1);
58470Sstevel@tonic-gate 		return ((dev_info_t *)NULL);
58480Sstevel@tonic-gate 	}
58490Sstevel@tonic-gate 
5850*4145Scth 	child = find_sibling(ddi_get_child(pdip), cname, caddr,
5851*4145Scth 	    FIND_NODE_BY_NODENAME, NULL);
58520Sstevel@tonic-gate 	kmem_free(devstr, strlen(devname)+1);
58530Sstevel@tonic-gate 	return (child);
58540Sstevel@tonic-gate }
58550Sstevel@tonic-gate 
58560Sstevel@tonic-gate /*
58570Sstevel@tonic-gate  * Misc. routines called by framework only
58580Sstevel@tonic-gate  */
58590Sstevel@tonic-gate 
58600Sstevel@tonic-gate /*
58610Sstevel@tonic-gate  * Clear the DEVI_MADE_CHILDREN/DEVI_ATTACHED_CHILDREN flags
58620Sstevel@tonic-gate  * if new child spec has been added.
58630Sstevel@tonic-gate  */
58640Sstevel@tonic-gate static int
58650Sstevel@tonic-gate reset_nexus_flags(dev_info_t *dip, void *arg)
58660Sstevel@tonic-gate {
5867298Scth 	struct hwc_spec	*list;
5868298Scth 	int		circ;
58690Sstevel@tonic-gate 
58700Sstevel@tonic-gate 	if (((DEVI(dip)->devi_flags & DEVI_MADE_CHILDREN) == 0) ||
58710Sstevel@tonic-gate 	    ((list = hwc_get_child_spec(dip, (major_t)(uintptr_t)arg)) == NULL))
58720Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
58730Sstevel@tonic-gate 
58740Sstevel@tonic-gate 	hwc_free_spec_list(list);
5875298Scth 
5876298Scth 	/* coordinate child state update */
5877298Scth 	ndi_devi_enter(dip, &circ);
58780Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
58790Sstevel@tonic-gate 	DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN | DEVI_ATTACHED_CHILDREN);
58800Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
5881298Scth 	ndi_devi_exit(dip, circ);
58820Sstevel@tonic-gate 
58830Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
58840Sstevel@tonic-gate }
58850Sstevel@tonic-gate 
58860Sstevel@tonic-gate /*
58870Sstevel@tonic-gate  * Helper functions, returns NULL if no memory.
58880Sstevel@tonic-gate  */
58890Sstevel@tonic-gate 
58900Sstevel@tonic-gate /*
58910Sstevel@tonic-gate  * path_to_major:
58920Sstevel@tonic-gate  *
58930Sstevel@tonic-gate  * Return an alternate driver name binding for the leaf device
58940Sstevel@tonic-gate  * of the given pathname, if there is one. The purpose of this
58950Sstevel@tonic-gate  * function is to deal with generic pathnames. The default action
58960Sstevel@tonic-gate  * for platforms that can't do this (ie: x86 or any platform that
58970Sstevel@tonic-gate  * does not have prom_finddevice functionality, which matches
58980Sstevel@tonic-gate  * nodenames and unit-addresses without the drivers participation)
58990Sstevel@tonic-gate  * is to return (major_t)-1.
59000Sstevel@tonic-gate  *
59010Sstevel@tonic-gate  * Used in loadrootmodules() in the swapgeneric module to
59020Sstevel@tonic-gate  * associate a given pathname with a given leaf driver.
59030Sstevel@tonic-gate  *
59040Sstevel@tonic-gate  */
59050Sstevel@tonic-gate major_t
59060Sstevel@tonic-gate path_to_major(char *path)
59070Sstevel@tonic-gate {
59080Sstevel@tonic-gate 	dev_info_t *dip;
59090Sstevel@tonic-gate 	char *p, *q;
5910789Sahrens 	pnode_t nodeid;
5911*4145Scth 	major_t major;
5912*4145Scth 
5913*4145Scth 	/* check for path-oriented alias */
5914*4145Scth 	major = ddi_name_to_major(path);
5915*4145Scth 	if ((major != (major_t)-1) &&
5916*4145Scth 	    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED)) {
5917*4145Scth 		NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s path bound %s\n",
5918*4145Scth 		    path, ddi_major_to_name(major)));
5919*4145Scth 		return (major);
5920*4145Scth 	}
59210Sstevel@tonic-gate 
59220Sstevel@tonic-gate 	/*
59230Sstevel@tonic-gate 	 * Get the nodeid of the given pathname, if such a mapping exists.
59240Sstevel@tonic-gate 	 */
59250Sstevel@tonic-gate 	dip = NULL;
59260Sstevel@tonic-gate 	nodeid = prom_finddevice(path);
59270Sstevel@tonic-gate 	if (nodeid != OBP_BADNODE) {
59280Sstevel@tonic-gate 		/*
59290Sstevel@tonic-gate 		 * Find the nodeid in our copy of the device tree and return
59300Sstevel@tonic-gate 		 * whatever name we used to bind this node to a driver.
59310Sstevel@tonic-gate 		 */
59320Sstevel@tonic-gate 		dip = e_ddi_nodeid_to_dip(nodeid);
59330Sstevel@tonic-gate 	}
59340Sstevel@tonic-gate 
59350Sstevel@tonic-gate 	if (dip == NULL) {
59360Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_WARN,
59370Sstevel@tonic-gate 		    "path_to_major: can't bind <%s>\n", path));
59380Sstevel@tonic-gate 		return ((major_t)-1);
59390Sstevel@tonic-gate 	}
59400Sstevel@tonic-gate 
59410Sstevel@tonic-gate 	/*
59420Sstevel@tonic-gate 	 * If we're bound to something other than the nodename,
59430Sstevel@tonic-gate 	 * note that in the message buffer and system log.
59440Sstevel@tonic-gate 	 */
59450Sstevel@tonic-gate 	p = ddi_binding_name(dip);
59460Sstevel@tonic-gate 	q = ddi_node_name(dip);
59470Sstevel@tonic-gate 	if (p && q && (strcmp(p, q) != 0))
59480Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s bound to %s\n",
59490Sstevel@tonic-gate 		    path, p));
59500Sstevel@tonic-gate 
5951*4145Scth 	major = ddi_name_to_major(p);
5952*4145Scth 
5953*4145Scth 	ndi_rele_devi(dip);		/* release e_ddi_nodeid_to_dip hold */
5954*4145Scth 
5955*4145Scth 	return (major);
59560Sstevel@tonic-gate }
59570Sstevel@tonic-gate 
59580Sstevel@tonic-gate /*
59590Sstevel@tonic-gate  * Return the held dip for the specified major and instance, attempting to do
59600Sstevel@tonic-gate  * an attach if specified. Return NULL if the devi can't be found or put in
59610Sstevel@tonic-gate  * the proper state. The caller must release the hold via ddi_release_devi if
59620Sstevel@tonic-gate  * a non-NULL value is returned.
59630Sstevel@tonic-gate  *
59640Sstevel@tonic-gate  * Some callers expect to be able to perform a hold_devi() while in a context
59650Sstevel@tonic-gate  * where using ndi_devi_enter() to ensure the hold might cause deadlock (see
59660Sstevel@tonic-gate  * open-from-attach code in consconfig_dacf.c). Such special-case callers
59670Sstevel@tonic-gate  * must ensure that an ndi_devi_enter(parent)/ndi_devi_hold() from a safe
59680Sstevel@tonic-gate  * context is already active. The hold_devi() implementation must accommodate
59690Sstevel@tonic-gate  * these callers.
59700Sstevel@tonic-gate  */
59710Sstevel@tonic-gate static dev_info_t *
59720Sstevel@tonic-gate hold_devi(major_t major, int instance, int flags)
59730Sstevel@tonic-gate {
59740Sstevel@tonic-gate 	struct devnames	*dnp;
59750Sstevel@tonic-gate 	dev_info_t	*dip;
59760Sstevel@tonic-gate 	char		*path;
59770Sstevel@tonic-gate 
59780Sstevel@tonic-gate 	if ((major >= devcnt) || (instance == -1))
59790Sstevel@tonic-gate 		return (NULL);
59800Sstevel@tonic-gate 
59810Sstevel@tonic-gate 	/* try to find the instance in the per driver list */
59820Sstevel@tonic-gate 	dnp = &(devnamesp[major]);
59830Sstevel@tonic-gate 	LOCK_DEV_OPS(&(dnp->dn_lock));
59840Sstevel@tonic-gate 	for (dip = dnp->dn_head; dip;
59850Sstevel@tonic-gate 	    dip = (dev_info_t *)DEVI(dip)->devi_next) {
59860Sstevel@tonic-gate 		/* skip node if instance field is not valid */
59870Sstevel@tonic-gate 		if (i_ddi_node_state(dip) < DS_INITIALIZED)
59880Sstevel@tonic-gate 			continue;
59890Sstevel@tonic-gate 
59900Sstevel@tonic-gate 		/* look for instance match */
59910Sstevel@tonic-gate 		if (DEVI(dip)->devi_instance == instance) {
59920Sstevel@tonic-gate 			/*
59930Sstevel@tonic-gate 			 * To accommodate callers that can't block in
59940Sstevel@tonic-gate 			 * ndi_devi_enter() we do an ndi_devi_hold(), and
59950Sstevel@tonic-gate 			 * afterwards check that the node is in a state where
59960Sstevel@tonic-gate 			 * the hold prevents detach(). If we did not manage to
59970Sstevel@tonic-gate 			 * prevent detach then we ndi_rele_devi() and perform
59980Sstevel@tonic-gate 			 * the slow path below (which can result in a blocking
59990Sstevel@tonic-gate 			 * ndi_devi_enter() while driving attach top-down).
60000Sstevel@tonic-gate 			 * This code depends on the ordering of
60010Sstevel@tonic-gate 			 * DEVI_SET_DETACHING and the devi_ref check in the
60020Sstevel@tonic-gate 			 * detach_node() code path.
60030Sstevel@tonic-gate 			 */
60040Sstevel@tonic-gate 			ndi_hold_devi(dip);
60051333Scth 			if (i_ddi_devi_attached(dip) &&
60060Sstevel@tonic-gate 			    !DEVI_IS_DETACHING(dip)) {
60070Sstevel@tonic-gate 				UNLOCK_DEV_OPS(&(dnp->dn_lock));
60080Sstevel@tonic-gate 				return (dip);	/* fast-path with devi held */
60090Sstevel@tonic-gate 			}
60100Sstevel@tonic-gate 			ndi_rele_devi(dip);
60110Sstevel@tonic-gate 
60120Sstevel@tonic-gate 			/* try slow-path */
60130Sstevel@tonic-gate 			dip = NULL;
60140Sstevel@tonic-gate 			break;
60150Sstevel@tonic-gate 		}
60160Sstevel@tonic-gate 	}
60170Sstevel@tonic-gate 	ASSERT(dip == NULL);
60180Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
60190Sstevel@tonic-gate 
60200Sstevel@tonic-gate 	if (flags & E_DDI_HOLD_DEVI_NOATTACH)
60210Sstevel@tonic-gate 		return (NULL);		/* told not to drive attach */
60220Sstevel@tonic-gate 
60230Sstevel@tonic-gate 	/* slow-path may block, so it should not occur from interrupt */
60240Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
60250Sstevel@tonic-gate 	if (servicing_interrupt())
60260Sstevel@tonic-gate 		return (NULL);
60270Sstevel@tonic-gate 
60280Sstevel@tonic-gate 	/* reconstruct the path and drive attach by path through devfs. */
60290Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
60300Sstevel@tonic-gate 	if (e_ddi_majorinstance_to_path(major, instance, path) == 0)
60310Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_path(path, flags);
60320Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
60330Sstevel@tonic-gate 	return (dip);			/* with devi held */
60340Sstevel@tonic-gate }
60350Sstevel@tonic-gate 
60360Sstevel@tonic-gate /*
60370Sstevel@tonic-gate  * The {e_}ddi_hold_devi{_by_{instance|dev|path}} hold the devinfo node
60380Sstevel@tonic-gate  * associated with the specified arguments.  This hold should be released
60390Sstevel@tonic-gate  * by calling ddi_release_devi.
60400Sstevel@tonic-gate  *
60410Sstevel@tonic-gate  * The E_DDI_HOLD_DEVI_NOATTACH flag argument allows the caller to to specify
60420Sstevel@tonic-gate  * a failure return if the node is not already attached.
60430Sstevel@tonic-gate  *
60440Sstevel@tonic-gate  * NOTE: by the time we make e_ddi_hold_devi public, we should be able to reuse
60450Sstevel@tonic-gate  * ddi_hold_devi again.
60460Sstevel@tonic-gate  */
60470Sstevel@tonic-gate dev_info_t *
60480Sstevel@tonic-gate ddi_hold_devi_by_instance(major_t major, int instance, int flags)
60490Sstevel@tonic-gate {
60500Sstevel@tonic-gate 	return (hold_devi(major, instance, flags));
60510Sstevel@tonic-gate }
60520Sstevel@tonic-gate 
60530Sstevel@tonic-gate dev_info_t *
60540Sstevel@tonic-gate e_ddi_hold_devi_by_dev(dev_t dev, int flags)
60550Sstevel@tonic-gate {
60560Sstevel@tonic-gate 	major_t	major = getmajor(dev);
60570Sstevel@tonic-gate 	dev_info_t	*dip;
60580Sstevel@tonic-gate 	struct dev_ops	*ops;
60590Sstevel@tonic-gate 	dev_info_t	*ddip = NULL;
60600Sstevel@tonic-gate 
60610Sstevel@tonic-gate 	dip = hold_devi(major, dev_to_instance(dev), flags);
60620Sstevel@tonic-gate 
60630Sstevel@tonic-gate 	/*
60640Sstevel@tonic-gate 	 * The rest of this routine is legacy support for drivers that
60650Sstevel@tonic-gate 	 * have broken DDI_INFO_DEVT2INSTANCE implementations but may have
60660Sstevel@tonic-gate 	 * functional DDI_INFO_DEVT2DEVINFO implementations.  This code will
60670Sstevel@tonic-gate 	 * diagnose inconsistency and, for maximum compatibility with legacy
60680Sstevel@tonic-gate 	 * drivers, give preference to the drivers DDI_INFO_DEVT2DEVINFO
60690Sstevel@tonic-gate 	 * implementation over the above derived dip based the driver's
60700Sstevel@tonic-gate 	 * DDI_INFO_DEVT2INSTANCE implementation. This legacy support should
60710Sstevel@tonic-gate 	 * be removed when DDI_INFO_DEVT2DEVINFO is deprecated.
60720Sstevel@tonic-gate 	 *
60730Sstevel@tonic-gate 	 * NOTE: The following code has a race condition. DEVT2DEVINFO
60740Sstevel@tonic-gate 	 *	returns a dip which is not held. By the time we ref ddip,
60750Sstevel@tonic-gate 	 *	it could have been freed. The saving grace is that for
60760Sstevel@tonic-gate 	 *	most drivers, the dip returned from hold_devi() is the
60770Sstevel@tonic-gate 	 *	same one as the one returned by DEVT2DEVINFO, so we are
60780Sstevel@tonic-gate 	 *	safe for drivers with the correct getinfo(9e) impl.
60790Sstevel@tonic-gate 	 */
60800Sstevel@tonic-gate 	if (((ops = ddi_hold_driver(major)) != NULL) &&
60810Sstevel@tonic-gate 	    CB_DRV_INSTALLED(ops) && ops->devo_getinfo)  {
60820Sstevel@tonic-gate 		if ((*ops->devo_getinfo)(NULL, DDI_INFO_DEVT2DEVINFO,
60830Sstevel@tonic-gate 		    (void *)dev, (void **)&ddip) != DDI_SUCCESS)
60840Sstevel@tonic-gate 			ddip = NULL;
60850Sstevel@tonic-gate 	}
60860Sstevel@tonic-gate 
60870Sstevel@tonic-gate 	/* give preference to the driver returned DEVT2DEVINFO dip */
60880Sstevel@tonic-gate 	if (ddip && (dip != ddip)) {
60890Sstevel@tonic-gate #ifdef	DEBUG
60900Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: inconsistent getinfo(9E) implementation",
60910Sstevel@tonic-gate 		    ddi_driver_name(ddip));
60920Sstevel@tonic-gate #endif	/* DEBUG */
60930Sstevel@tonic-gate 		ndi_hold_devi(ddip);
60940Sstevel@tonic-gate 		if (dip)
60950Sstevel@tonic-gate 			ndi_rele_devi(dip);
60960Sstevel@tonic-gate 		dip = ddip;
60970Sstevel@tonic-gate 	}
60980Sstevel@tonic-gate 
60990Sstevel@tonic-gate 	if (ops)
61000Sstevel@tonic-gate 		ddi_rele_driver(major);
61010Sstevel@tonic-gate 
61020Sstevel@tonic-gate 	return (dip);
61030Sstevel@tonic-gate }
61040Sstevel@tonic-gate 
61050Sstevel@tonic-gate /*
61060Sstevel@tonic-gate  * For compatibility only. Do not call this function!
61070Sstevel@tonic-gate  */
61080Sstevel@tonic-gate dev_info_t *
61090Sstevel@tonic-gate e_ddi_get_dev_info(dev_t dev, vtype_t type)
61100Sstevel@tonic-gate {
61110Sstevel@tonic-gate 	dev_info_t *dip = NULL;
61120Sstevel@tonic-gate 	if (getmajor(dev) >= devcnt)
61130Sstevel@tonic-gate 		return (NULL);
61140Sstevel@tonic-gate 
61150Sstevel@tonic-gate 	switch (type) {
61160Sstevel@tonic-gate 	case VCHR:
61170Sstevel@tonic-gate 	case VBLK:
61180Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
61190Sstevel@tonic-gate 	default:
61200Sstevel@tonic-gate 		break;
61210Sstevel@tonic-gate 	}
61220Sstevel@tonic-gate 
61230Sstevel@tonic-gate 	/*
61240Sstevel@tonic-gate 	 * For compatibility reasons, we can only return the dip with
61250Sstevel@tonic-gate 	 * the driver ref count held. This is not a safe thing to do.
61260Sstevel@tonic-gate 	 * For certain broken third-party software, we are willing
61270Sstevel@tonic-gate 	 * to venture into unknown territory.
61280Sstevel@tonic-gate 	 */
61290Sstevel@tonic-gate 	if (dip) {
61300Sstevel@tonic-gate 		(void) ndi_hold_driver(dip);
61310Sstevel@tonic-gate 		ndi_rele_devi(dip);
61320Sstevel@tonic-gate 	}
61330Sstevel@tonic-gate 	return (dip);
61340Sstevel@tonic-gate }
61350Sstevel@tonic-gate 
61360Sstevel@tonic-gate dev_info_t *
61370Sstevel@tonic-gate e_ddi_hold_devi_by_path(char *path, int flags)
61380Sstevel@tonic-gate {
61390Sstevel@tonic-gate 	dev_info_t	*dip;
61400Sstevel@tonic-gate 
61410Sstevel@tonic-gate 	/* can't specify NOATTACH by path */
61420Sstevel@tonic-gate 	ASSERT(!(flags & E_DDI_HOLD_DEVI_NOATTACH));
61430Sstevel@tonic-gate 
61440Sstevel@tonic-gate 	return (resolve_pathname(path, &dip, NULL, NULL) ? NULL : dip);
61450Sstevel@tonic-gate }
61460Sstevel@tonic-gate 
61470Sstevel@tonic-gate void
61480Sstevel@tonic-gate e_ddi_hold_devi(dev_info_t *dip)
61490Sstevel@tonic-gate {
61500Sstevel@tonic-gate 	ndi_hold_devi(dip);
61510Sstevel@tonic-gate }
61520Sstevel@tonic-gate 
61530Sstevel@tonic-gate void
61540Sstevel@tonic-gate ddi_release_devi(dev_info_t *dip)
61550Sstevel@tonic-gate {
61560Sstevel@tonic-gate 	ndi_rele_devi(dip);
61570Sstevel@tonic-gate }
61580Sstevel@tonic-gate 
61590Sstevel@tonic-gate /*
61600Sstevel@tonic-gate  * Associate a streams queue with a devinfo node
61610Sstevel@tonic-gate  * NOTE: This function is called by STREAM driver's put procedure.
61620Sstevel@tonic-gate  *	It cannot block.
61630Sstevel@tonic-gate  */
61640Sstevel@tonic-gate void
61650Sstevel@tonic-gate ddi_assoc_queue_with_devi(queue_t *q, dev_info_t *dip)
61660Sstevel@tonic-gate {
61670Sstevel@tonic-gate 	queue_t *rq = _RD(q);
61680Sstevel@tonic-gate 	struct stdata *stp;
61690Sstevel@tonic-gate 	vnode_t *vp;
61700Sstevel@tonic-gate 
61710Sstevel@tonic-gate 	/* set flag indicating that ddi_assoc_queue_with_devi was called */
61720Sstevel@tonic-gate 	mutex_enter(QLOCK(rq));
61730Sstevel@tonic-gate 	rq->q_flag |= _QASSOCIATED;
61740Sstevel@tonic-gate 	mutex_exit(QLOCK(rq));
61750Sstevel@tonic-gate 
61760Sstevel@tonic-gate 	/* get the vnode associated with the queue */
61770Sstevel@tonic-gate 	stp = STREAM(rq);
61780Sstevel@tonic-gate 	vp = stp->sd_vnode;
61790Sstevel@tonic-gate 	ASSERT(vp);
61800Sstevel@tonic-gate 
61810Sstevel@tonic-gate 	/* change the hardware association of the vnode */
61820Sstevel@tonic-gate 	spec_assoc_vp_with_devi(vp, dip);
61830Sstevel@tonic-gate }
61840Sstevel@tonic-gate 
61850Sstevel@tonic-gate /*
61860Sstevel@tonic-gate  * ddi_install_driver(name)
61870Sstevel@tonic-gate  *
61880Sstevel@tonic-gate  * Driver installation is currently a byproduct of driver loading.  This
61890Sstevel@tonic-gate  * may change.
61900Sstevel@tonic-gate  */
61910Sstevel@tonic-gate int
61920Sstevel@tonic-gate ddi_install_driver(char *name)
61930Sstevel@tonic-gate {
61940Sstevel@tonic-gate 	major_t major = ddi_name_to_major(name);
61950Sstevel@tonic-gate 
61960Sstevel@tonic-gate 	if ((major == (major_t)-1) ||
61970Sstevel@tonic-gate 	    (ddi_hold_installed_driver(major) == NULL)) {
61980Sstevel@tonic-gate 		return (DDI_FAILURE);
61990Sstevel@tonic-gate 	}
62000Sstevel@tonic-gate 	ddi_rele_driver(major);
62010Sstevel@tonic-gate 	return (DDI_SUCCESS);
62020Sstevel@tonic-gate }
62030Sstevel@tonic-gate 
62040Sstevel@tonic-gate struct dev_ops *
62050Sstevel@tonic-gate ddi_hold_driver(major_t major)
62060Sstevel@tonic-gate {
62070Sstevel@tonic-gate 	return (mod_hold_dev_by_major(major));
62080Sstevel@tonic-gate }
62090Sstevel@tonic-gate 
62100Sstevel@tonic-gate 
62110Sstevel@tonic-gate void
62120Sstevel@tonic-gate ddi_rele_driver(major_t major)
62130Sstevel@tonic-gate {
62140Sstevel@tonic-gate 	mod_rele_dev_by_major(major);
62150Sstevel@tonic-gate }
62160Sstevel@tonic-gate 
62170Sstevel@tonic-gate 
62180Sstevel@tonic-gate /*
62190Sstevel@tonic-gate  * This is called during boot to force attachment order of special dips
62200Sstevel@tonic-gate  * dip must be referenced via ndi_hold_devi()
62210Sstevel@tonic-gate  */
62220Sstevel@tonic-gate int
62230Sstevel@tonic-gate i_ddi_attach_node_hierarchy(dev_info_t *dip)
62240Sstevel@tonic-gate {
62252155Scth 	dev_info_t	*parent;
62262155Scth 	int		ret, circ;
62272155Scth 
62282155Scth 	/*
62292155Scth 	 * Recurse up until attached parent is found.
62302155Scth 	 */
62312009Sdm120769 	if (i_ddi_devi_attached(dip))
62322009Sdm120769 		return (DDI_SUCCESS);
62330Sstevel@tonic-gate 	parent = ddi_get_parent(dip);
62340Sstevel@tonic-gate 	if (i_ddi_attach_node_hierarchy(parent) != DDI_SUCCESS)
62350Sstevel@tonic-gate 		return (DDI_FAILURE);
62360Sstevel@tonic-gate 
62370Sstevel@tonic-gate 	/*
62382155Scth 	 * Come top-down, expanding .conf nodes under this parent
62392155Scth 	 * and driving attach.
62400Sstevel@tonic-gate 	 */
62412155Scth 	ndi_devi_enter(parent, &circ);
62420Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(parent, 0);
62432155Scth 	ret = i_ddi_attachchild(dip);
62442155Scth 	ndi_devi_exit(parent, circ);
62452155Scth 
62462155Scth 	return (ret);
62470Sstevel@tonic-gate }
62480Sstevel@tonic-gate 
62490Sstevel@tonic-gate /* keep this function static */
62500Sstevel@tonic-gate static int
62510Sstevel@tonic-gate attach_driver_nodes(major_t major)
62520Sstevel@tonic-gate {
62530Sstevel@tonic-gate 	struct devnames *dnp;
62540Sstevel@tonic-gate 	dev_info_t *dip;
62550Sstevel@tonic-gate 	int error = DDI_FAILURE;
62560Sstevel@tonic-gate 
62570Sstevel@tonic-gate 	dnp = &devnamesp[major];
62580Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
62590Sstevel@tonic-gate 	dip = dnp->dn_head;
62600Sstevel@tonic-gate 	while (dip) {
62610Sstevel@tonic-gate 		ndi_hold_devi(dip);
62620Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
62630Sstevel@tonic-gate 		if (i_ddi_attach_node_hierarchy(dip) == DDI_SUCCESS)
62640Sstevel@tonic-gate 			error = DDI_SUCCESS;
62650Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
62660Sstevel@tonic-gate 		ndi_rele_devi(dip);
62670Sstevel@tonic-gate 		dip = ddi_get_next(dip);
62680Sstevel@tonic-gate 	}
62690Sstevel@tonic-gate 	if (error == DDI_SUCCESS)
62700Sstevel@tonic-gate 		dnp->dn_flags |= DN_NO_AUTODETACH;
62710Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
62720Sstevel@tonic-gate 
62730Sstevel@tonic-gate 
62740Sstevel@tonic-gate 	return (error);
62750Sstevel@tonic-gate }
62760Sstevel@tonic-gate 
62770Sstevel@tonic-gate /*
62780Sstevel@tonic-gate  * i_ddi_attach_hw_nodes configures and attaches all hw nodes
62790Sstevel@tonic-gate  * bound to a specific driver. This function replaces calls to
62800Sstevel@tonic-gate  * ddi_hold_installed_driver() for drivers with no .conf
62810Sstevel@tonic-gate  * enumerated nodes.
62820Sstevel@tonic-gate  *
62830Sstevel@tonic-gate  * This facility is typically called at boot time to attach
62840Sstevel@tonic-gate  * platform-specific hardware nodes, such as ppm nodes on xcal
62850Sstevel@tonic-gate  * and grover and keyswitch nodes on cherrystone. It does not
62860Sstevel@tonic-gate  * deal with .conf enumerated node. Calling it beyond the boot
62870Sstevel@tonic-gate  * process is strongly discouraged.
62880Sstevel@tonic-gate  */
62890Sstevel@tonic-gate int
62900Sstevel@tonic-gate i_ddi_attach_hw_nodes(char *driver)
62910Sstevel@tonic-gate {
62920Sstevel@tonic-gate 	major_t major;
62930Sstevel@tonic-gate 
62940Sstevel@tonic-gate 	major = ddi_name_to_major(driver);
62950Sstevel@tonic-gate 	if (major == (major_t)-1)
62960Sstevel@tonic-gate 		return (DDI_FAILURE);
62970Sstevel@tonic-gate 
62980Sstevel@tonic-gate 	return (attach_driver_nodes(major));
62990Sstevel@tonic-gate }
63000Sstevel@tonic-gate 
63010Sstevel@tonic-gate /*
63020Sstevel@tonic-gate  * i_ddi_attach_pseudo_node configures pseudo drivers which
63030Sstevel@tonic-gate  * has a single node. The .conf nodes must be enumerated
63040Sstevel@tonic-gate  * before calling this interface. The dip is held attached
63050Sstevel@tonic-gate  * upon returning.
63060Sstevel@tonic-gate  *
63070Sstevel@tonic-gate  * This facility should only be called only at boot time
63080Sstevel@tonic-gate  * by the I/O framework.
63090Sstevel@tonic-gate  */
63100Sstevel@tonic-gate dev_info_t *
63110Sstevel@tonic-gate i_ddi_attach_pseudo_node(char *driver)
63120Sstevel@tonic-gate {
63130Sstevel@tonic-gate 	major_t major;
63140Sstevel@tonic-gate 	dev_info_t *dip;
63150Sstevel@tonic-gate 
63160Sstevel@tonic-gate 	major = ddi_name_to_major(driver);
63170Sstevel@tonic-gate 	if (major == (major_t)-1)
63180Sstevel@tonic-gate 		return (NULL);
63190Sstevel@tonic-gate 
63200Sstevel@tonic-gate 	if (attach_driver_nodes(major) != DDI_SUCCESS)
63210Sstevel@tonic-gate 		return (NULL);
63220Sstevel@tonic-gate 
63230Sstevel@tonic-gate 	dip = devnamesp[major].dn_head;
63240Sstevel@tonic-gate 	ASSERT(dip && ddi_get_next(dip) == NULL);
63250Sstevel@tonic-gate 	ndi_hold_devi(dip);
63260Sstevel@tonic-gate 	return (dip);
63270Sstevel@tonic-gate }
63280Sstevel@tonic-gate 
63290Sstevel@tonic-gate static void
63300Sstevel@tonic-gate diplist_to_parent_major(dev_info_t *head, char parents[])
63310Sstevel@tonic-gate {
63320Sstevel@tonic-gate 	major_t major;
63330Sstevel@tonic-gate 	dev_info_t *dip, *pdip;
63340Sstevel@tonic-gate 
63350Sstevel@tonic-gate 	for (dip = head; dip != NULL; dip = ddi_get_next(dip)) {
63360Sstevel@tonic-gate 		pdip = ddi_get_parent(dip);
63370Sstevel@tonic-gate 		ASSERT(pdip);	/* disallow rootnex.conf nodes */
63380Sstevel@tonic-gate 		major = ddi_driver_major(pdip);
63390Sstevel@tonic-gate 		if ((major != (major_t)-1) && parents[major] == 0)
63400Sstevel@tonic-gate 			parents[major] = 1;
63410Sstevel@tonic-gate 	}
63420Sstevel@tonic-gate }
63430Sstevel@tonic-gate 
63440Sstevel@tonic-gate /*
63450Sstevel@tonic-gate  * Call ddi_hold_installed_driver() on each parent major
63460Sstevel@tonic-gate  * and invoke mt_config_driver() to attach child major.
63470Sstevel@tonic-gate  * This is part of the implementation of ddi_hold_installed_driver.
63480Sstevel@tonic-gate  */
63490Sstevel@tonic-gate static int
63500Sstevel@tonic-gate attach_driver_by_parent(major_t child_major, char parents[])
63510Sstevel@tonic-gate {
63520Sstevel@tonic-gate 	major_t par_major;
63530Sstevel@tonic-gate 	struct mt_config_handle *hdl;
63540Sstevel@tonic-gate 	int flags = NDI_DEVI_PERSIST | NDI_NO_EVENT;
63550Sstevel@tonic-gate 
63560Sstevel@tonic-gate 	hdl = mt_config_init(NULL, NULL, flags, child_major, MT_CONFIG_OP,
63570Sstevel@tonic-gate 	    NULL);
63580Sstevel@tonic-gate 	for (par_major = 0; par_major < devcnt; par_major++) {
63590Sstevel@tonic-gate 		/* disallow recursion on the same driver */
63600Sstevel@tonic-gate 		if (parents[par_major] == 0 || par_major == child_major)
63610Sstevel@tonic-gate 			continue;
63620Sstevel@tonic-gate 		if (ddi_hold_installed_driver(par_major) == NULL)
63630Sstevel@tonic-gate 			continue;
63640Sstevel@tonic-gate 		hdl->mtc_parmajor = par_major;
63650Sstevel@tonic-gate 		mt_config_driver(hdl);
63660Sstevel@tonic-gate 		ddi_rele_driver(par_major);
63670Sstevel@tonic-gate 	}
63680Sstevel@tonic-gate 	(void) mt_config_fini(hdl);
63690Sstevel@tonic-gate 
63700Sstevel@tonic-gate 	return (i_ddi_devs_attached(child_major));
63710Sstevel@tonic-gate }
63720Sstevel@tonic-gate 
63730Sstevel@tonic-gate int
63740Sstevel@tonic-gate i_ddi_devs_attached(major_t major)
63750Sstevel@tonic-gate {
63760Sstevel@tonic-gate 	dev_info_t *dip;
63770Sstevel@tonic-gate 	struct devnames *dnp;
63780Sstevel@tonic-gate 	int error = DDI_FAILURE;
63790Sstevel@tonic-gate 
63800Sstevel@tonic-gate 	/* check for attached instances */
63810Sstevel@tonic-gate 	dnp = &devnamesp[major];
63820Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
63830Sstevel@tonic-gate 	for (dip = dnp->dn_head; dip != NULL; dip = ddi_get_next(dip)) {
63841333Scth 		if (i_ddi_devi_attached(dip)) {
63850Sstevel@tonic-gate 			error = DDI_SUCCESS;
63860Sstevel@tonic-gate 			break;
63870Sstevel@tonic-gate 		}
63880Sstevel@tonic-gate 	}
63890Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
63900Sstevel@tonic-gate 
63910Sstevel@tonic-gate 	return (error);
63920Sstevel@tonic-gate }
63930Sstevel@tonic-gate 
63940Sstevel@tonic-gate /*
63950Sstevel@tonic-gate  * ddi_hold_installed_driver configures and attaches all
63960Sstevel@tonic-gate  * instances of the specified driver. To accomplish this
63970Sstevel@tonic-gate  * it configures and attaches all possible parents of
63980Sstevel@tonic-gate  * the driver, enumerated both in h/w nodes and in the
63990Sstevel@tonic-gate  * driver's .conf file.
64000Sstevel@tonic-gate  *
64010Sstevel@tonic-gate  * NOTE: This facility is for compatibility purposes only and will
64020Sstevel@tonic-gate  *	eventually go away. Its usage is strongly discouraged.
64030Sstevel@tonic-gate  */
64040Sstevel@tonic-gate static void
64050Sstevel@tonic-gate enter_driver(struct devnames *dnp)
64060Sstevel@tonic-gate {
64070Sstevel@tonic-gate 	mutex_enter(&dnp->dn_lock);
64080Sstevel@tonic-gate 	ASSERT(dnp->dn_busy_thread != curthread);
64090Sstevel@tonic-gate 	while (dnp->dn_flags & DN_DRIVER_BUSY)
64100Sstevel@tonic-gate 		cv_wait(&dnp->dn_wait, &dnp->dn_lock);
64110Sstevel@tonic-gate 	dnp->dn_flags |= DN_DRIVER_BUSY;
64120Sstevel@tonic-gate 	dnp->dn_busy_thread = curthread;
64130Sstevel@tonic-gate 	mutex_exit(&dnp->dn_lock);
64140Sstevel@tonic-gate }
64150Sstevel@tonic-gate 
64160Sstevel@tonic-gate static void
64170Sstevel@tonic-gate exit_driver(struct devnames *dnp)
64180Sstevel@tonic-gate {
64190Sstevel@tonic-gate 	mutex_enter(&dnp->dn_lock);
64200Sstevel@tonic-gate 	ASSERT(dnp->dn_busy_thread == curthread);
64210Sstevel@tonic-gate 	dnp->dn_flags &= ~DN_DRIVER_BUSY;
64220Sstevel@tonic-gate 	dnp->dn_busy_thread = NULL;
64230Sstevel@tonic-gate 	cv_broadcast(&dnp->dn_wait);
64240Sstevel@tonic-gate 	mutex_exit(&dnp->dn_lock);
64250Sstevel@tonic-gate }
64260Sstevel@tonic-gate 
64270Sstevel@tonic-gate struct dev_ops *
64280Sstevel@tonic-gate ddi_hold_installed_driver(major_t major)
64290Sstevel@tonic-gate {
64300Sstevel@tonic-gate 	struct dev_ops *ops;
64310Sstevel@tonic-gate 	struct devnames *dnp;
64320Sstevel@tonic-gate 	char *parents;
64330Sstevel@tonic-gate 	int error;
64340Sstevel@tonic-gate 
64350Sstevel@tonic-gate 	ops = ddi_hold_driver(major);
64360Sstevel@tonic-gate 	if (ops == NULL)
64370Sstevel@tonic-gate 		return (NULL);
64380Sstevel@tonic-gate 
64390Sstevel@tonic-gate 	/*
64400Sstevel@tonic-gate 	 * Return immediately if all the attach operations associated
64410Sstevel@tonic-gate 	 * with a ddi_hold_installed_driver() call have already been done.
64420Sstevel@tonic-gate 	 */
64430Sstevel@tonic-gate 	dnp = &devnamesp[major];
64440Sstevel@tonic-gate 	enter_driver(dnp);
64450Sstevel@tonic-gate 	if (dnp->dn_flags & DN_DRIVER_HELD) {
64460Sstevel@tonic-gate 		exit_driver(dnp);
64470Sstevel@tonic-gate 		if (i_ddi_devs_attached(major) == DDI_SUCCESS)
64480Sstevel@tonic-gate 			return (ops);
64490Sstevel@tonic-gate 		ddi_rele_driver(major);
64500Sstevel@tonic-gate 		return (NULL);
64510Sstevel@tonic-gate 	}
64520Sstevel@tonic-gate 
64530Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
64540Sstevel@tonic-gate 	dnp->dn_flags |= (DN_DRIVER_HELD | DN_NO_AUTODETACH);
64550Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
64560Sstevel@tonic-gate 
64570Sstevel@tonic-gate 	DCOMPATPRINTF((CE_CONT,
64580Sstevel@tonic-gate 	    "ddi_hold_installed_driver: %s\n", dnp->dn_name));
64590Sstevel@tonic-gate 
64600Sstevel@tonic-gate 	/*
64610Sstevel@tonic-gate 	 * When the driver has no .conf children, it is sufficient
64620Sstevel@tonic-gate 	 * to attach existing nodes in the device tree. Nodes not
64630Sstevel@tonic-gate 	 * enumerated by the OBP are not attached.
64640Sstevel@tonic-gate 	 */
64650Sstevel@tonic-gate 	if (dnp->dn_pl == NULL) {
64660Sstevel@tonic-gate 		if (attach_driver_nodes(major) == DDI_SUCCESS) {
64670Sstevel@tonic-gate 			exit_driver(dnp);
64680Sstevel@tonic-gate 			return (ops);
64690Sstevel@tonic-gate 		}
64700Sstevel@tonic-gate 		exit_driver(dnp);
64710Sstevel@tonic-gate 		ddi_rele_driver(major);
64720Sstevel@tonic-gate 		return (NULL);
64730Sstevel@tonic-gate 	}
64740Sstevel@tonic-gate 
64750Sstevel@tonic-gate 	/*
64760Sstevel@tonic-gate 	 * Driver has .conf nodes. We find all possible parents
64770Sstevel@tonic-gate 	 * and recursively all ddi_hold_installed_driver on the
64780Sstevel@tonic-gate 	 * parent driver; then we invoke ndi_config_driver()
64790Sstevel@tonic-gate 	 * on all possible parent node in parallel to speed up
64800Sstevel@tonic-gate 	 * performance.
64810Sstevel@tonic-gate 	 */
64820Sstevel@tonic-gate 	parents = kmem_zalloc(devcnt * sizeof (char), KM_SLEEP);
64830Sstevel@tonic-gate 
64840Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
64850Sstevel@tonic-gate 	/* find .conf parents */
64860Sstevel@tonic-gate 	(void) impl_parlist_to_major(dnp->dn_pl, parents);
64870Sstevel@tonic-gate 	/* find hw node parents */
64880Sstevel@tonic-gate 	diplist_to_parent_major(dnp->dn_head, parents);
64890Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
64900Sstevel@tonic-gate 
64910Sstevel@tonic-gate 	error = attach_driver_by_parent(major, parents);
64920Sstevel@tonic-gate 	kmem_free(parents, devcnt * sizeof (char));
64930Sstevel@tonic-gate 	if (error == DDI_SUCCESS) {
64940Sstevel@tonic-gate 		exit_driver(dnp);
64950Sstevel@tonic-gate 		return (ops);
64960Sstevel@tonic-gate 	}
64970Sstevel@tonic-gate 
64980Sstevel@tonic-gate 	exit_driver(dnp);
64990Sstevel@tonic-gate 	ddi_rele_driver(major);
65000Sstevel@tonic-gate 	return (NULL);
65010Sstevel@tonic-gate }
65020Sstevel@tonic-gate 
65030Sstevel@tonic-gate /*
65040Sstevel@tonic-gate  * Default bus_config entry point for nexus drivers
65050Sstevel@tonic-gate  */
65060Sstevel@tonic-gate int
65070Sstevel@tonic-gate ndi_busop_bus_config(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
65080Sstevel@tonic-gate     void *arg, dev_info_t **child, clock_t timeout)
65090Sstevel@tonic-gate {
65100Sstevel@tonic-gate 	major_t major;
65110Sstevel@tonic-gate 
65120Sstevel@tonic-gate 	/*
65130Sstevel@tonic-gate 	 * A timeout of 30 minutes or more is probably a mistake
65140Sstevel@tonic-gate 	 * This is intended to catch uses where timeout is in
65150Sstevel@tonic-gate 	 * the wrong units.  timeout must be in units of ticks.
65160Sstevel@tonic-gate 	 */
65170Sstevel@tonic-gate 	ASSERT(timeout < SEC_TO_TICK(1800));
65180Sstevel@tonic-gate 
65190Sstevel@tonic-gate 	major = (major_t)-1;
65200Sstevel@tonic-gate 	switch (op) {
65210Sstevel@tonic-gate 	case BUS_CONFIG_ONE:
65220Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config %s timeout=%ld\n",
65230Sstevel@tonic-gate 			ddi_driver_name(pdip), ddi_get_instance(pdip),
65240Sstevel@tonic-gate 			(char *)arg, timeout));
65250Sstevel@tonic-gate 		return (devi_config_one(pdip, (char *)arg, child, flags,
65260Sstevel@tonic-gate 		    timeout));
65270Sstevel@tonic-gate 
65280Sstevel@tonic-gate 	case BUS_CONFIG_DRIVER:
65290Sstevel@tonic-gate 		major = (major_t)(uintptr_t)arg;
65300Sstevel@tonic-gate 		/*FALLTHROUGH*/
65310Sstevel@tonic-gate 	case BUS_CONFIG_ALL:
65320Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config timeout=%ld\n",
65330Sstevel@tonic-gate 			ddi_driver_name(pdip), ddi_get_instance(pdip),
65340Sstevel@tonic-gate 			timeout));
65350Sstevel@tonic-gate 		if (timeout > 0) {
65360Sstevel@tonic-gate 			NDI_DEBUG(flags, (CE_CONT,
65370Sstevel@tonic-gate 			    "%s%d: bus config all timeout=%ld\n",
65380Sstevel@tonic-gate 			    ddi_driver_name(pdip), ddi_get_instance(pdip),
65390Sstevel@tonic-gate 			    timeout));
65400Sstevel@tonic-gate 			delay(timeout);
65410Sstevel@tonic-gate 		}
65420Sstevel@tonic-gate 		return (config_immediate_children(pdip, flags, major));
65430Sstevel@tonic-gate 
65440Sstevel@tonic-gate 	default:
65450Sstevel@tonic-gate 		return (NDI_FAILURE);
65460Sstevel@tonic-gate 	}
65470Sstevel@tonic-gate 	/*NOTREACHED*/
65480Sstevel@tonic-gate }
65490Sstevel@tonic-gate 
65500Sstevel@tonic-gate /*
65510Sstevel@tonic-gate  * Default busop bus_unconfig handler for nexus drivers
65520Sstevel@tonic-gate  */
65530Sstevel@tonic-gate int
65540Sstevel@tonic-gate ndi_busop_bus_unconfig(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
65550Sstevel@tonic-gate     void *arg)
65560Sstevel@tonic-gate {
65570Sstevel@tonic-gate 	major_t major;
65580Sstevel@tonic-gate 
65590Sstevel@tonic-gate 	major = (major_t)-1;
65600Sstevel@tonic-gate 	switch (op) {
65610Sstevel@tonic-gate 	case BUS_UNCONFIG_ONE:
65620Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig %s\n",
65630Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
65640Sstevel@tonic-gate 		    (char *)arg));
65650Sstevel@tonic-gate 		return (devi_unconfig_one(pdip, (char *)arg, flags));
65660Sstevel@tonic-gate 
65670Sstevel@tonic-gate 	case BUS_UNCONFIG_DRIVER:
65680Sstevel@tonic-gate 		major = (major_t)(uintptr_t)arg;
65690Sstevel@tonic-gate 		/*FALLTHROUGH*/
65700Sstevel@tonic-gate 	case BUS_UNCONFIG_ALL:
65710Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig all\n",
65720Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip)));
65730Sstevel@tonic-gate 		return (unconfig_immediate_children(pdip, NULL, flags, major));
65740Sstevel@tonic-gate 
65750Sstevel@tonic-gate 	default:
65760Sstevel@tonic-gate 		return (NDI_FAILURE);
65770Sstevel@tonic-gate 	}
65780Sstevel@tonic-gate 	/*NOTREACHED*/
65790Sstevel@tonic-gate }
65800Sstevel@tonic-gate 
65810Sstevel@tonic-gate /*
65820Sstevel@tonic-gate  * dummy functions to be removed
65830Sstevel@tonic-gate  */
65840Sstevel@tonic-gate void
65850Sstevel@tonic-gate impl_rem_dev_props(dev_info_t *dip)
65860Sstevel@tonic-gate {
65870Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip))
65880Sstevel@tonic-gate 	/* do nothing */
65890Sstevel@tonic-gate }
65900Sstevel@tonic-gate 
65910Sstevel@tonic-gate /*
65920Sstevel@tonic-gate  * Determine if a node is a leaf node. If not sure, return false (0).
65930Sstevel@tonic-gate  */
65940Sstevel@tonic-gate static int
65950Sstevel@tonic-gate is_leaf_node(dev_info_t *dip)
65960Sstevel@tonic-gate {
65970Sstevel@tonic-gate 	major_t major = ddi_driver_major(dip);
65980Sstevel@tonic-gate 
65990Sstevel@tonic-gate 	if (major == (major_t)-1)
66000Sstevel@tonic-gate 		return (0);
66010Sstevel@tonic-gate 
66020Sstevel@tonic-gate 	return (devnamesp[major].dn_flags & DN_LEAF_DRIVER);
66030Sstevel@tonic-gate }
66040Sstevel@tonic-gate 
66050Sstevel@tonic-gate /*
66060Sstevel@tonic-gate  * Multithreaded [un]configuration
66070Sstevel@tonic-gate  */
66080Sstevel@tonic-gate static struct mt_config_handle *
66090Sstevel@tonic-gate mt_config_init(dev_info_t *pdip, dev_info_t **dipp, int flags,
66100Sstevel@tonic-gate     major_t major, int op, struct brevq_node **brevqp)
66110Sstevel@tonic-gate {
66120Sstevel@tonic-gate 	struct mt_config_handle	*hdl = kmem_alloc(sizeof (*hdl), KM_SLEEP);
66130Sstevel@tonic-gate 
66140Sstevel@tonic-gate 	mutex_init(&hdl->mtc_lock, NULL, MUTEX_DEFAULT, NULL);
66150Sstevel@tonic-gate 	cv_init(&hdl->mtc_cv, NULL, CV_DEFAULT, NULL);
66160Sstevel@tonic-gate 	hdl->mtc_pdip = pdip;
66170Sstevel@tonic-gate 	hdl->mtc_fdip = dipp;
66180Sstevel@tonic-gate 	hdl->mtc_parmajor = (major_t)-1;
66190Sstevel@tonic-gate 	hdl->mtc_flags = flags;
66200Sstevel@tonic-gate 	hdl->mtc_major = major;
66210Sstevel@tonic-gate 	hdl->mtc_thr_count = 0;
66220Sstevel@tonic-gate 	hdl->mtc_op = op;
66230Sstevel@tonic-gate 	hdl->mtc_error = 0;
66240Sstevel@tonic-gate 	hdl->mtc_brevqp = brevqp;
66250Sstevel@tonic-gate 
66260Sstevel@tonic-gate #ifdef DEBUG
66270Sstevel@tonic-gate 	gethrestime(&hdl->start_time);
66280Sstevel@tonic-gate 	hdl->total_time = 0;
66290Sstevel@tonic-gate #endif /* DEBUG */
66300Sstevel@tonic-gate 
66310Sstevel@tonic-gate 	return (hdl);
66320Sstevel@tonic-gate }
66330Sstevel@tonic-gate 
66340Sstevel@tonic-gate #ifdef DEBUG
66350Sstevel@tonic-gate static int
66360Sstevel@tonic-gate time_diff_in_msec(timestruc_t start, timestruc_t end)
66370Sstevel@tonic-gate {
66380Sstevel@tonic-gate 	int	nsec, sec;
66390Sstevel@tonic-gate 
66400Sstevel@tonic-gate 	sec = end.tv_sec - start.tv_sec;
66410Sstevel@tonic-gate 	nsec = end.tv_nsec - start.tv_nsec;
66420Sstevel@tonic-gate 	if (nsec < 0) {
66430Sstevel@tonic-gate 		nsec += NANOSEC;
66440Sstevel@tonic-gate 		sec -= 1;
66450Sstevel@tonic-gate 	}
66460Sstevel@tonic-gate 
66470Sstevel@tonic-gate 	return (sec * (NANOSEC >> 20) + (nsec >> 20));
66480Sstevel@tonic-gate }
66490Sstevel@tonic-gate 
66500Sstevel@tonic-gate #endif	/* DEBUG */
66510Sstevel@tonic-gate 
66520Sstevel@tonic-gate static int
66530Sstevel@tonic-gate mt_config_fini(struct mt_config_handle *hdl)
66540Sstevel@tonic-gate {
66550Sstevel@tonic-gate 	int		rv;
66560Sstevel@tonic-gate #ifdef DEBUG
66570Sstevel@tonic-gate 	int		real_time;
66580Sstevel@tonic-gate 	timestruc_t	end_time;
66590Sstevel@tonic-gate #endif /* DEBUG */
66600Sstevel@tonic-gate 
66610Sstevel@tonic-gate 	mutex_enter(&hdl->mtc_lock);
66620Sstevel@tonic-gate 	while (hdl->mtc_thr_count > 0)
66630Sstevel@tonic-gate 		cv_wait(&hdl->mtc_cv, &hdl->mtc_lock);
66640Sstevel@tonic-gate 	rv = hdl->mtc_error;
66650Sstevel@tonic-gate 	mutex_exit(&hdl->mtc_lock);
66660Sstevel@tonic-gate 
66670Sstevel@tonic-gate #ifdef DEBUG
66680Sstevel@tonic-gate 	gethrestime(&end_time);
66690Sstevel@tonic-gate 	real_time = time_diff_in_msec(hdl->start_time, end_time);
66700Sstevel@tonic-gate 	if ((ddidebug & DDI_MTCONFIG) && hdl->mtc_pdip)
66710Sstevel@tonic-gate 		cmn_err(CE_NOTE,
66720Sstevel@tonic-gate 		    "config %s%d: total time %d msec, real time %d msec",
66730Sstevel@tonic-gate 			ddi_driver_name(hdl->mtc_pdip),
66740Sstevel@tonic-gate 			ddi_get_instance(hdl->mtc_pdip),
66750Sstevel@tonic-gate 			hdl->total_time, real_time);
66760Sstevel@tonic-gate #endif /* DEBUG */
66770Sstevel@tonic-gate 
66780Sstevel@tonic-gate 	cv_destroy(&hdl->mtc_cv);
66790Sstevel@tonic-gate 	mutex_destroy(&hdl->mtc_lock);
66800Sstevel@tonic-gate 	kmem_free(hdl, sizeof (*hdl));
66810Sstevel@tonic-gate 
66820Sstevel@tonic-gate 	return (rv);
66830Sstevel@tonic-gate }
66840Sstevel@tonic-gate 
66850Sstevel@tonic-gate struct mt_config_data {
66860Sstevel@tonic-gate 	struct mt_config_handle	*mtc_hdl;
66870Sstevel@tonic-gate 	dev_info_t		*mtc_dip;
66880Sstevel@tonic-gate 	major_t			mtc_major;
66890Sstevel@tonic-gate 	int			mtc_flags;
66900Sstevel@tonic-gate 	struct brevq_node	*mtc_brn;
66910Sstevel@tonic-gate 	struct mt_config_data	*mtc_next;
66920Sstevel@tonic-gate };
66930Sstevel@tonic-gate 
66940Sstevel@tonic-gate static void
66950Sstevel@tonic-gate mt_config_thread(void *arg)
66960Sstevel@tonic-gate {
66970Sstevel@tonic-gate 	struct mt_config_data	*mcd = (struct mt_config_data *)arg;
66980Sstevel@tonic-gate 	struct mt_config_handle	*hdl = mcd->mtc_hdl;
66990Sstevel@tonic-gate 	dev_info_t		*dip = mcd->mtc_dip;
67000Sstevel@tonic-gate 	dev_info_t		*rdip, **dipp;
67010Sstevel@tonic-gate 	major_t			major = mcd->mtc_major;
67020Sstevel@tonic-gate 	int			flags = mcd->mtc_flags;
67030Sstevel@tonic-gate 	int			rv = 0;
67040Sstevel@tonic-gate 
67050Sstevel@tonic-gate #ifdef DEBUG
67060Sstevel@tonic-gate 	timestruc_t start_time, end_time;
67070Sstevel@tonic-gate 	gethrestime(&start_time);
67080Sstevel@tonic-gate #endif /* DEBUG */
67090Sstevel@tonic-gate 
67100Sstevel@tonic-gate 	rdip = NULL;
67110Sstevel@tonic-gate 	dipp = hdl->mtc_fdip ? &rdip : NULL;
67120Sstevel@tonic-gate 
67130Sstevel@tonic-gate 	switch (hdl->mtc_op) {
67140Sstevel@tonic-gate 	case MT_CONFIG_OP:
67150Sstevel@tonic-gate 		rv = devi_config_common(dip, flags, major);
67160Sstevel@tonic-gate 		break;
67170Sstevel@tonic-gate 	case MT_UNCONFIG_OP:
67180Sstevel@tonic-gate 		if (mcd->mtc_brn) {
67190Sstevel@tonic-gate 			struct brevq_node *brevq = NULL;
67200Sstevel@tonic-gate 			rv = devi_unconfig_common(dip, dipp, flags, major,
67210Sstevel@tonic-gate 			    &brevq);
67221317Scth 			mcd->mtc_brn->brn_child = brevq;
67230Sstevel@tonic-gate 		} else
67240Sstevel@tonic-gate 			rv = devi_unconfig_common(dip, dipp, flags, major,
67250Sstevel@tonic-gate 			    NULL);
67260Sstevel@tonic-gate 		break;
67270Sstevel@tonic-gate 	}
67280Sstevel@tonic-gate 
67290Sstevel@tonic-gate 	mutex_enter(&hdl->mtc_lock);
67300Sstevel@tonic-gate #ifdef DEBUG
67310Sstevel@tonic-gate 	gethrestime(&end_time);
67320Sstevel@tonic-gate 	hdl->total_time += time_diff_in_msec(start_time, end_time);
67330Sstevel@tonic-gate #endif /* DEBUG */
67342155Scth 
67352155Scth 	if ((rv != NDI_SUCCESS) && (hdl->mtc_error == 0)) {
67360Sstevel@tonic-gate 		hdl->mtc_error = rv;
67372155Scth #ifdef	DEBUG
67382155Scth 		if ((ddidebug & DDI_DEBUG) && (major != (major_t)-1)) {
67392155Scth 			char	*path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
67402155Scth 
67412155Scth 			(void) ddi_pathname(dip, path);
67422155Scth 			cmn_err(CE_NOTE, "mt_config_thread: "
67432155Scth 			    "op %d.%d.%x at %s failed %d",
67442155Scth 			    hdl->mtc_op, major, flags, path, rv);
67452155Scth 			kmem_free(path, MAXPATHLEN);
67462155Scth 		}
67472155Scth #endif	/* DEBUG */
67482155Scth 	}
67492155Scth 
67500Sstevel@tonic-gate 	if (hdl->mtc_fdip && *hdl->mtc_fdip == NULL) {
67510Sstevel@tonic-gate 		*hdl->mtc_fdip = rdip;
67520Sstevel@tonic-gate 		rdip = NULL;
67530Sstevel@tonic-gate 	}
67540Sstevel@tonic-gate 
67550Sstevel@tonic-gate 	if (rdip) {
67560Sstevel@tonic-gate 		ASSERT(rv != NDI_SUCCESS);
67570Sstevel@tonic-gate 		ndi_rele_devi(rdip);
67580Sstevel@tonic-gate 	}
67590Sstevel@tonic-gate 
67600Sstevel@tonic-gate 	ndi_rele_devi(dip);
67611826Sbm42561 
67621826Sbm42561 	if (--hdl->mtc_thr_count == 0)
67631826Sbm42561 		cv_broadcast(&hdl->mtc_cv);
67641826Sbm42561 	mutex_exit(&hdl->mtc_lock);
67650Sstevel@tonic-gate 	kmem_free(mcd, sizeof (*mcd));
67660Sstevel@tonic-gate }
67670Sstevel@tonic-gate 
67680Sstevel@tonic-gate /*
67690Sstevel@tonic-gate  * Multi-threaded config/unconfig of child nexus
67700Sstevel@tonic-gate  */
67710Sstevel@tonic-gate static void
67720Sstevel@tonic-gate mt_config_children(struct mt_config_handle *hdl)
67730Sstevel@tonic-gate {
67740Sstevel@tonic-gate 	dev_info_t		*pdip = hdl->mtc_pdip;
67750Sstevel@tonic-gate 	major_t			major = hdl->mtc_major;
67760Sstevel@tonic-gate 	dev_info_t		*dip;
67770Sstevel@tonic-gate 	int			circ;
67781317Scth 	struct brevq_node	*brn;
67790Sstevel@tonic-gate 	struct mt_config_data	*mcd_head = NULL;
67800Sstevel@tonic-gate 	struct mt_config_data	*mcd_tail = NULL;
67810Sstevel@tonic-gate 	struct mt_config_data	*mcd;
67820Sstevel@tonic-gate #ifdef DEBUG
67830Sstevel@tonic-gate 	timestruc_t		end_time;
67840Sstevel@tonic-gate 
67850Sstevel@tonic-gate 	/* Update total_time in handle */
67860Sstevel@tonic-gate 	gethrestime(&end_time);
67870Sstevel@tonic-gate 	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
67880Sstevel@tonic-gate #endif
67890Sstevel@tonic-gate 
67900Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
67910Sstevel@tonic-gate 	dip = ddi_get_child(pdip);
67920Sstevel@tonic-gate 	while (dip) {
67930Sstevel@tonic-gate 		if (hdl->mtc_op == MT_UNCONFIG_OP && hdl->mtc_brevqp &&
67940Sstevel@tonic-gate 		    !(DEVI_EVREMOVE(dip)) &&
67950Sstevel@tonic-gate 		    i_ddi_node_state(dip) >= DS_INITIALIZED) {
67960Sstevel@tonic-gate 			/*
67970Sstevel@tonic-gate 			 * Enqueue this dip's deviname.
67980Sstevel@tonic-gate 			 * No need to hold a lock while enqueuing since this
67990Sstevel@tonic-gate 			 * is the only thread doing the enqueue and no one
68000Sstevel@tonic-gate 			 * walks the queue while we are in multithreaded
68010Sstevel@tonic-gate 			 * unconfiguration.
68020Sstevel@tonic-gate 			 */
68030Sstevel@tonic-gate 			brn = brevq_enqueue(hdl->mtc_brevqp, dip, NULL);
68041317Scth 		} else
68051317Scth 			brn = NULL;
68060Sstevel@tonic-gate 
68070Sstevel@tonic-gate 		/*
68080Sstevel@tonic-gate 		 * Hold the child that we are processing so he does not get
68090Sstevel@tonic-gate 		 * removed. The corrisponding ndi_rele_devi() for children
68100Sstevel@tonic-gate 		 * that are not being skipped is done at the end of
68110Sstevel@tonic-gate 		 * mt_config_thread().
68120Sstevel@tonic-gate 		 */
68130Sstevel@tonic-gate 		ndi_hold_devi(dip);
68140Sstevel@tonic-gate 
68150Sstevel@tonic-gate 		/*
68160Sstevel@tonic-gate 		 * skip leaf nodes and (for configure) nodes not
68170Sstevel@tonic-gate 		 * fully attached.
68180Sstevel@tonic-gate 		 */
68190Sstevel@tonic-gate 		if (is_leaf_node(dip) ||
68200Sstevel@tonic-gate 		    (hdl->mtc_op == MT_CONFIG_OP &&
68210Sstevel@tonic-gate 		    i_ddi_node_state(dip) < DS_READY)) {
68220Sstevel@tonic-gate 			ndi_rele_devi(dip);
68230Sstevel@tonic-gate 			dip = ddi_get_next_sibling(dip);
68240Sstevel@tonic-gate 			continue;
68250Sstevel@tonic-gate 		}
68260Sstevel@tonic-gate 
68270Sstevel@tonic-gate 		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
68280Sstevel@tonic-gate 		mcd->mtc_dip = dip;
68290Sstevel@tonic-gate 		mcd->mtc_hdl = hdl;
68300Sstevel@tonic-gate 		mcd->mtc_brn = brn;
68310Sstevel@tonic-gate 
68320Sstevel@tonic-gate 		/*
68330Sstevel@tonic-gate 		 * Switch a 'driver' operation to an 'all' operation below a
68340Sstevel@tonic-gate 		 * node bound to the driver.
68350Sstevel@tonic-gate 		 */
6836662Sszhou 		if ((major == (major_t)-1) || (major == ddi_driver_major(dip)))
68370Sstevel@tonic-gate 			mcd->mtc_major = (major_t)-1;
68380Sstevel@tonic-gate 		else
68390Sstevel@tonic-gate 			mcd->mtc_major = major;
68400Sstevel@tonic-gate 
68410Sstevel@tonic-gate 		/*
68420Sstevel@tonic-gate 		 * The unconfig-driver to unconfig-all conversion above
68430Sstevel@tonic-gate 		 * constitutes an autodetach for NDI_DETACH_DRIVER calls,
68440Sstevel@tonic-gate 		 * set NDI_AUTODETACH.
68450Sstevel@tonic-gate 		 */
68460Sstevel@tonic-gate 		mcd->mtc_flags = hdl->mtc_flags;
68470Sstevel@tonic-gate 		if ((mcd->mtc_flags & NDI_DETACH_DRIVER) &&
68480Sstevel@tonic-gate 		    (hdl->mtc_op == MT_UNCONFIG_OP) &&
68490Sstevel@tonic-gate 		    (major == ddi_driver_major(pdip)))
68500Sstevel@tonic-gate 			mcd->mtc_flags |= NDI_AUTODETACH;
68510Sstevel@tonic-gate 
68520Sstevel@tonic-gate 		mutex_enter(&hdl->mtc_lock);
68530Sstevel@tonic-gate 		hdl->mtc_thr_count++;
68540Sstevel@tonic-gate 		mutex_exit(&hdl->mtc_lock);
68550Sstevel@tonic-gate 
68560Sstevel@tonic-gate 		/*
68570Sstevel@tonic-gate 		 * Add to end of list to process after ndi_devi_exit to avoid
68580Sstevel@tonic-gate 		 * locking differences depending on value of mtc_off.
68590Sstevel@tonic-gate 		 */
68600Sstevel@tonic-gate 		mcd->mtc_next = NULL;
68610Sstevel@tonic-gate 		if (mcd_head == NULL)
68620Sstevel@tonic-gate 			mcd_head = mcd;
68630Sstevel@tonic-gate 		else
68640Sstevel@tonic-gate 			mcd_tail->mtc_next = mcd;
68650Sstevel@tonic-gate 		mcd_tail = mcd;
68660Sstevel@tonic-gate 
68670Sstevel@tonic-gate 		dip = ddi_get_next_sibling(dip);
68680Sstevel@tonic-gate 	}
68690Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
68700Sstevel@tonic-gate 
68710Sstevel@tonic-gate 	/* go through the list of held children */
68720Sstevel@tonic-gate 	for (mcd = mcd_head; mcd; mcd = mcd_head) {
68730Sstevel@tonic-gate 		mcd_head = mcd->mtc_next;
68742155Scth 		if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
68750Sstevel@tonic-gate 			mt_config_thread(mcd);
68760Sstevel@tonic-gate 		else
68770Sstevel@tonic-gate 			(void) thread_create(NULL, 0, mt_config_thread, mcd,
68780Sstevel@tonic-gate 			    0, &p0, TS_RUN, minclsyspri);
68790Sstevel@tonic-gate 	}
68800Sstevel@tonic-gate }
68810Sstevel@tonic-gate 
68820Sstevel@tonic-gate static void
68830Sstevel@tonic-gate mt_config_driver(struct mt_config_handle *hdl)
68840Sstevel@tonic-gate {
68850Sstevel@tonic-gate 	major_t			par_major = hdl->mtc_parmajor;
68860Sstevel@tonic-gate 	major_t			major = hdl->mtc_major;
68870Sstevel@tonic-gate 	struct devnames		*dnp = &devnamesp[par_major];
68880Sstevel@tonic-gate 	dev_info_t		*dip;
68890Sstevel@tonic-gate 	struct mt_config_data	*mcd_head = NULL;
68900Sstevel@tonic-gate 	struct mt_config_data	*mcd_tail = NULL;
68910Sstevel@tonic-gate 	struct mt_config_data	*mcd;
68920Sstevel@tonic-gate #ifdef DEBUG
68930Sstevel@tonic-gate 	timestruc_t		end_time;
68940Sstevel@tonic-gate 
68950Sstevel@tonic-gate 	/* Update total_time in handle */
68960Sstevel@tonic-gate 	gethrestime(&end_time);
68970Sstevel@tonic-gate 	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
68980Sstevel@tonic-gate #endif
68990Sstevel@tonic-gate 	ASSERT(par_major != (major_t)-1);
69000Sstevel@tonic-gate 	ASSERT(major != (major_t)-1);
69010Sstevel@tonic-gate 
69020Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
69030Sstevel@tonic-gate 	dip = devnamesp[par_major].dn_head;
69040Sstevel@tonic-gate 	while (dip) {
69050Sstevel@tonic-gate 		/*
69060Sstevel@tonic-gate 		 * Hold the child that we are processing so he does not get
69070Sstevel@tonic-gate 		 * removed. The corrisponding ndi_rele_devi() for children
69080Sstevel@tonic-gate 		 * that are not being skipped is done at the end of
69090Sstevel@tonic-gate 		 * mt_config_thread().
69100Sstevel@tonic-gate 		 */
69110Sstevel@tonic-gate 		ndi_hold_devi(dip);
69120Sstevel@tonic-gate 
69130Sstevel@tonic-gate 		/* skip leaf nodes and nodes not fully attached */
69141333Scth 		if (!i_ddi_devi_attached(dip) || is_leaf_node(dip)) {
69150Sstevel@tonic-gate 			ndi_rele_devi(dip);
69160Sstevel@tonic-gate 			dip = ddi_get_next(dip);
69170Sstevel@tonic-gate 			continue;
69180Sstevel@tonic-gate 		}
69190Sstevel@tonic-gate 
69200Sstevel@tonic-gate 		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
69210Sstevel@tonic-gate 		mcd->mtc_dip = dip;
69220Sstevel@tonic-gate 		mcd->mtc_hdl = hdl;
69230Sstevel@tonic-gate 		mcd->mtc_major = major;
69240Sstevel@tonic-gate 		mcd->mtc_flags = hdl->mtc_flags;
69250Sstevel@tonic-gate 
69260Sstevel@tonic-gate 		mutex_enter(&hdl->mtc_lock);
69270Sstevel@tonic-gate 		hdl->mtc_thr_count++;
69280Sstevel@tonic-gate 		mutex_exit(&hdl->mtc_lock);
69290Sstevel@tonic-gate 
69300Sstevel@tonic-gate 		/*
69310Sstevel@tonic-gate 		 * Add to end of list to process after UNLOCK_DEV_OPS to avoid
69320Sstevel@tonic-gate 		 * locking differences depending on value of mtc_off.
69330Sstevel@tonic-gate 		 */
69340Sstevel@tonic-gate 		mcd->mtc_next = NULL;
69350Sstevel@tonic-gate 		if (mcd_head == NULL)
69360Sstevel@tonic-gate 			mcd_head = mcd;
69370Sstevel@tonic-gate 		else
69380Sstevel@tonic-gate 			mcd_tail->mtc_next = mcd;
69390Sstevel@tonic-gate 		mcd_tail = mcd;
69400Sstevel@tonic-gate 
69410Sstevel@tonic-gate 		dip = ddi_get_next(dip);
69420Sstevel@tonic-gate 	}
69430Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
69440Sstevel@tonic-gate 
69450Sstevel@tonic-gate 	/* go through the list of held children */
69460Sstevel@tonic-gate 	for (mcd = mcd_head; mcd; mcd = mcd_head) {
69470Sstevel@tonic-gate 		mcd_head = mcd->mtc_next;
69482155Scth 		if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
69490Sstevel@tonic-gate 			mt_config_thread(mcd);
69500Sstevel@tonic-gate 		else
69510Sstevel@tonic-gate 			(void) thread_create(NULL, 0, mt_config_thread, mcd,
69520Sstevel@tonic-gate 			    0, &p0, TS_RUN, minclsyspri);
69530Sstevel@tonic-gate 	}
69540Sstevel@tonic-gate }
69550Sstevel@tonic-gate 
69560Sstevel@tonic-gate /*
69570Sstevel@tonic-gate  * Given the nodeid for a persistent (PROM or SID) node, return
69580Sstevel@tonic-gate  * the corresponding devinfo node
69590Sstevel@tonic-gate  * NOTE: This function will return NULL for .conf nodeids.
69600Sstevel@tonic-gate  */
69610Sstevel@tonic-gate dev_info_t *
6962789Sahrens e_ddi_nodeid_to_dip(pnode_t nodeid)
69630Sstevel@tonic-gate {
69640Sstevel@tonic-gate 	dev_info_t		*dip = NULL;
69650Sstevel@tonic-gate 	struct devi_nodeid	*prev, *elem;
69660Sstevel@tonic-gate 
69670Sstevel@tonic-gate 	mutex_enter(&devimap->dno_lock);
69680Sstevel@tonic-gate 
69690Sstevel@tonic-gate 	prev = NULL;
69700Sstevel@tonic-gate 	for (elem = devimap->dno_head; elem; elem = elem->next) {
69710Sstevel@tonic-gate 		if (elem->nodeid == nodeid) {
69720Sstevel@tonic-gate 			ndi_hold_devi(elem->dip);
69730Sstevel@tonic-gate 			dip = elem->dip;
69740Sstevel@tonic-gate 			break;
69750Sstevel@tonic-gate 		}
69760Sstevel@tonic-gate 		prev = elem;
69770Sstevel@tonic-gate 	}
69780Sstevel@tonic-gate 
69790Sstevel@tonic-gate 	/*
69800Sstevel@tonic-gate 	 * Move to head for faster lookup next time
69810Sstevel@tonic-gate 	 */
69820Sstevel@tonic-gate 	if (elem && prev) {
69830Sstevel@tonic-gate 		prev->next = elem->next;
69840Sstevel@tonic-gate 		elem->next = devimap->dno_head;
69850Sstevel@tonic-gate 		devimap->dno_head = elem;
69860Sstevel@tonic-gate 	}
69870Sstevel@tonic-gate 
69880Sstevel@tonic-gate 	mutex_exit(&devimap->dno_lock);
69890Sstevel@tonic-gate 	return (dip);
69900Sstevel@tonic-gate }
69910Sstevel@tonic-gate 
69920Sstevel@tonic-gate static void
69930Sstevel@tonic-gate free_cache_task(void *arg)
69940Sstevel@tonic-gate {
69950Sstevel@tonic-gate 	ASSERT(arg == NULL);
69960Sstevel@tonic-gate 
69970Sstevel@tonic-gate 	mutex_enter(&di_cache.cache_lock);
69980Sstevel@tonic-gate 
69990Sstevel@tonic-gate 	/*
70000Sstevel@tonic-gate 	 * The cache can be invalidated without holding the lock
70010Sstevel@tonic-gate 	 * but it can be made valid again only while the lock is held.
70020Sstevel@tonic-gate 	 * So if the cache is invalid when the lock is held, it will
70030Sstevel@tonic-gate 	 * stay invalid until lock is released.
70040Sstevel@tonic-gate 	 */
70050Sstevel@tonic-gate 	if (!di_cache.cache_valid)
70060Sstevel@tonic-gate 		i_ddi_di_cache_free(&di_cache);
70070Sstevel@tonic-gate 
70080Sstevel@tonic-gate 	mutex_exit(&di_cache.cache_lock);
70090Sstevel@tonic-gate 
70100Sstevel@tonic-gate 	if (di_cache_debug)
70110Sstevel@tonic-gate 		cmn_err(CE_NOTE, "system_taskq: di_cache freed");
70120Sstevel@tonic-gate }
70130Sstevel@tonic-gate 
70140Sstevel@tonic-gate extern int modrootloaded;
70150Sstevel@tonic-gate 
70160Sstevel@tonic-gate void
70170Sstevel@tonic-gate i_ddi_di_cache_free(struct di_cache *cache)
70180Sstevel@tonic-gate {
70190Sstevel@tonic-gate 	int	error;
70200Sstevel@tonic-gate 
70210Sstevel@tonic-gate 	ASSERT(mutex_owned(&cache->cache_lock));
70220Sstevel@tonic-gate 
70230Sstevel@tonic-gate 	if (cache->cache_size) {
70240Sstevel@tonic-gate 		ASSERT(cache->cache_size > 0);
70250Sstevel@tonic-gate 		ASSERT(cache->cache_data);
70260Sstevel@tonic-gate 
70270Sstevel@tonic-gate 		kmem_free(cache->cache_data, cache->cache_size);
70280Sstevel@tonic-gate 		cache->cache_data = NULL;
70290Sstevel@tonic-gate 		cache->cache_size = 0;
70300Sstevel@tonic-gate 
70310Sstevel@tonic-gate 		if (di_cache_debug)
70320Sstevel@tonic-gate 			cmn_err(CE_NOTE, "i_ddi_di_cache_free: freed cachemem");
70330Sstevel@tonic-gate 	} else {
70340Sstevel@tonic-gate 		ASSERT(cache->cache_data == NULL);
70350Sstevel@tonic-gate 		if (di_cache_debug)
70360Sstevel@tonic-gate 			cmn_err(CE_NOTE, "i_ddi_di_cache_free: NULL cache");
70370Sstevel@tonic-gate 	}
70380Sstevel@tonic-gate 
70390Sstevel@tonic-gate 	if (!modrootloaded || rootvp == NULL || vn_is_readonly(rootvp)) {
70400Sstevel@tonic-gate 		if (di_cache_debug) {
70410Sstevel@tonic-gate 			cmn_err(CE_WARN, "/ not mounted/RDONLY. Skip unlink");
70420Sstevel@tonic-gate 		}
70430Sstevel@tonic-gate 		return;
70440Sstevel@tonic-gate 	}
70450Sstevel@tonic-gate 
70460Sstevel@tonic-gate 	error = vn_remove(DI_CACHE_FILE, UIO_SYSSPACE, RMFILE);
70470Sstevel@tonic-gate 	if (di_cache_debug && error && error != ENOENT) {
70480Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: unlink failed: %d", DI_CACHE_FILE, error);
70490Sstevel@tonic-gate 	} else if (di_cache_debug && !error) {
70500Sstevel@tonic-gate 		cmn_err(CE_NOTE, "i_ddi_di_cache_free: unlinked cache file");
70510Sstevel@tonic-gate 	}
70520Sstevel@tonic-gate }
70530Sstevel@tonic-gate 
70540Sstevel@tonic-gate void
70550Sstevel@tonic-gate i_ddi_di_cache_invalidate(int kmflag)
70560Sstevel@tonic-gate {
70570Sstevel@tonic-gate 	uint_t	flag;
70580Sstevel@tonic-gate 
70590Sstevel@tonic-gate 	if (!modrootloaded || !i_ddi_io_initialized()) {
70600Sstevel@tonic-gate 		if (di_cache_debug)
70610Sstevel@tonic-gate 			cmn_err(CE_NOTE, "I/O not inited. Skipping invalidate");
70620Sstevel@tonic-gate 		return;
70630Sstevel@tonic-gate 	}
70640Sstevel@tonic-gate 
70650Sstevel@tonic-gate 	/*
70662621Sllai1 	 * Invalidate the in-core cache and
70672621Sllai1 	 * increment devtree generation number
70680Sstevel@tonic-gate 	 */
70690Sstevel@tonic-gate 	atomic_and_32(&di_cache.cache_valid, 0);
70702621Sllai1 	atomic_inc_ulong(&devtree_gen);
70710Sstevel@tonic-gate 
70720Sstevel@tonic-gate 	flag = (kmflag == KM_SLEEP) ? TQ_SLEEP : TQ_NOSLEEP;
70730Sstevel@tonic-gate 
70740Sstevel@tonic-gate 	(void) taskq_dispatch(system_taskq, free_cache_task, NULL, flag);
70750Sstevel@tonic-gate 
70760Sstevel@tonic-gate 	if (di_cache_debug) {
70770Sstevel@tonic-gate 		cmn_err(CE_NOTE, "invalidation with km_flag: %s",
70780Sstevel@tonic-gate 		    kmflag == KM_SLEEP ? "KM_SLEEP" : "KM_NOSLEEP");
70790Sstevel@tonic-gate 	}
70800Sstevel@tonic-gate }
70810Sstevel@tonic-gate 
70820Sstevel@tonic-gate 
70830Sstevel@tonic-gate static void
70840Sstevel@tonic-gate i_bind_vhci_node(dev_info_t *dip)
70850Sstevel@tonic-gate {
7086*4145Scth 	DEVI(dip)->devi_major = ddi_name_to_major(ddi_node_name(dip));
70870Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_BOUND);
70880Sstevel@tonic-gate }
70890Sstevel@tonic-gate 
70900Sstevel@tonic-gate static char vhci_node_addr[2];
70910Sstevel@tonic-gate 
70920Sstevel@tonic-gate static int
70930Sstevel@tonic-gate i_init_vhci_node(dev_info_t *dip)
70940Sstevel@tonic-gate {
70950Sstevel@tonic-gate 	add_global_props(dip);
70960Sstevel@tonic-gate 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
70970Sstevel@tonic-gate 	if (DEVI(dip)->devi_ops == NULL)
70980Sstevel@tonic-gate 		return (-1);
70990Sstevel@tonic-gate 
71000Sstevel@tonic-gate 	DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
71010Sstevel@tonic-gate 	e_ddi_keep_instance(dip);
71020Sstevel@tonic-gate 	vhci_node_addr[0]	= '\0';
71030Sstevel@tonic-gate 	ddi_set_name_addr(dip, vhci_node_addr);
71040Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_INITIALIZED);
71050Sstevel@tonic-gate 	return (0);
71060Sstevel@tonic-gate }
71070Sstevel@tonic-gate 
71080Sstevel@tonic-gate static void
71090Sstevel@tonic-gate i_link_vhci_node(dev_info_t *dip)
71100Sstevel@tonic-gate {
71111093Shiremath 	ASSERT(MUTEX_HELD(&global_vhci_lock));
71121093Shiremath 
71130Sstevel@tonic-gate 	/*
71140Sstevel@tonic-gate 	 * scsi_vhci should be kept left most of the device tree.
71150Sstevel@tonic-gate 	 */
71160Sstevel@tonic-gate 	if (scsi_vhci_dip) {
71170Sstevel@tonic-gate 		DEVI(dip)->devi_sibling = DEVI(scsi_vhci_dip)->devi_sibling;
71180Sstevel@tonic-gate 		DEVI(scsi_vhci_dip)->devi_sibling = DEVI(dip);
71190Sstevel@tonic-gate 	} else {
71200Sstevel@tonic-gate 		DEVI(dip)->devi_sibling = DEVI(top_devinfo)->devi_child;
71210Sstevel@tonic-gate 		DEVI(top_devinfo)->devi_child = DEVI(dip);
71220Sstevel@tonic-gate 	}
71230Sstevel@tonic-gate }
71240Sstevel@tonic-gate 
71250Sstevel@tonic-gate 
71260Sstevel@tonic-gate /*
71270Sstevel@tonic-gate  * This a special routine to enumerate vhci node (child of rootnex
71280Sstevel@tonic-gate  * node) without holding the ndi_devi_enter() lock. The device node
71290Sstevel@tonic-gate  * is allocated, initialized and brought into DS_READY state before
71300Sstevel@tonic-gate  * inserting into the device tree. The VHCI node is handcrafted
71310Sstevel@tonic-gate  * here to bring the node to DS_READY, similar to rootnex node.
71320Sstevel@tonic-gate  *
71330Sstevel@tonic-gate  * The global_vhci_lock protects linking the node into the device
71340Sstevel@tonic-gate  * as same lock is held before linking/unlinking any direct child
71350Sstevel@tonic-gate  * of rootnex children.
71360Sstevel@tonic-gate  *
71370Sstevel@tonic-gate  * This routine is a workaround to handle a possible deadlock
71380Sstevel@tonic-gate  * that occurs while trying to enumerate node in a different sub-tree
71390Sstevel@tonic-gate  * during _init/_attach entry points.
71400Sstevel@tonic-gate  */
71410Sstevel@tonic-gate /*ARGSUSED*/
71420Sstevel@tonic-gate dev_info_t *
71430Sstevel@tonic-gate ndi_devi_config_vhci(char *drvname, int flags)
71440Sstevel@tonic-gate {
71450Sstevel@tonic-gate 	struct devnames		*dnp;
71460Sstevel@tonic-gate 	dev_info_t		*dip;
71470Sstevel@tonic-gate 	major_t			major = ddi_name_to_major(drvname);
71480Sstevel@tonic-gate 
71490Sstevel@tonic-gate 	if (major == -1)
71500Sstevel@tonic-gate 		return (NULL);
71510Sstevel@tonic-gate 
71520Sstevel@tonic-gate 	/* Make sure we create the VHCI node only once */
71530Sstevel@tonic-gate 	dnp = &devnamesp[major];
71540Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
71550Sstevel@tonic-gate 	if (dnp->dn_head) {
71560Sstevel@tonic-gate 		dip = dnp->dn_head;
71570Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
71580Sstevel@tonic-gate 		return (dip);
71590Sstevel@tonic-gate 	}
71600Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
71610Sstevel@tonic-gate 
71620Sstevel@tonic-gate 	/* Allocate the VHCI node */
71630Sstevel@tonic-gate 	ndi_devi_alloc_sleep(top_devinfo, drvname, DEVI_SID_NODEID, &dip);
71640Sstevel@tonic-gate 	ndi_hold_devi(dip);
71650Sstevel@tonic-gate 
71660Sstevel@tonic-gate 	/* Mark the node as VHCI */
71670Sstevel@tonic-gate 	DEVI(dip)->devi_node_attributes |= DDI_VHCI_NODE;
71680Sstevel@tonic-gate 
71690Sstevel@tonic-gate 	i_ddi_add_devimap(dip);
71700Sstevel@tonic-gate 	i_bind_vhci_node(dip);
71710Sstevel@tonic-gate 	if (i_init_vhci_node(dip) == -1) {
71720Sstevel@tonic-gate 		ndi_rele_devi(dip);
71730Sstevel@tonic-gate 		(void) ndi_devi_free(dip);
71740Sstevel@tonic-gate 		return (NULL);
71750Sstevel@tonic-gate 	}
71760Sstevel@tonic-gate 
7177495Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
71780Sstevel@tonic-gate 	DEVI_SET_ATTACHING(dip);
7179495Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
7180495Scth 
71810Sstevel@tonic-gate 	if (devi_attach(dip, DDI_ATTACH) != DDI_SUCCESS) {
71820Sstevel@tonic-gate 		cmn_err(CE_CONT, "Could not attach %s driver", drvname);
71830Sstevel@tonic-gate 		e_ddi_free_instance(dip, vhci_node_addr);
71840Sstevel@tonic-gate 		ndi_rele_devi(dip);
71850Sstevel@tonic-gate 		(void) ndi_devi_free(dip);
71860Sstevel@tonic-gate 		return (NULL);
71870Sstevel@tonic-gate 	}
7188495Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
71890Sstevel@tonic-gate 	DEVI_CLR_ATTACHING(dip);
7190495Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
71910Sstevel@tonic-gate 
71921093Shiremath 	mutex_enter(&global_vhci_lock);
71930Sstevel@tonic-gate 	i_link_vhci_node(dip);
71941093Shiremath 	mutex_exit(&global_vhci_lock);
71950Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_READY);
71960Sstevel@tonic-gate 
71970Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
71980Sstevel@tonic-gate 	dnp->dn_flags |= DN_DRIVER_HELD;
71990Sstevel@tonic-gate 	dnp->dn_head = dip;
72000Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
72010Sstevel@tonic-gate 
72020Sstevel@tonic-gate 	i_ndi_devi_report_status_change(dip, NULL);
72030Sstevel@tonic-gate 
72040Sstevel@tonic-gate 	return (dip);
72050Sstevel@tonic-gate }
72060Sstevel@tonic-gate 
72070Sstevel@tonic-gate /*
72080Sstevel@tonic-gate  * ibt_hw_is_present() returns 0 when there is no IB hardware actively
72090Sstevel@tonic-gate  * running.  This is primarily useful for modules like rpcmod which
72100Sstevel@tonic-gate  * needs a quick check to decide whether or not it should try to use
72110Sstevel@tonic-gate  * InfiniBand
72120Sstevel@tonic-gate  */
72130Sstevel@tonic-gate int ib_hw_status = 0;
72140Sstevel@tonic-gate int
72150Sstevel@tonic-gate ibt_hw_is_present()
72160Sstevel@tonic-gate {
72170Sstevel@tonic-gate 	return (ib_hw_status);
72180Sstevel@tonic-gate }
7219