xref: /onnv-gate/usr/src/uts/common/os/devcfg.c (revision 662)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/note.h>
300Sstevel@tonic-gate #include <sys/t_lock.h>
310Sstevel@tonic-gate #include <sys/cmn_err.h>
320Sstevel@tonic-gate #include <sys/instance.h>
330Sstevel@tonic-gate #include <sys/conf.h>
340Sstevel@tonic-gate #include <sys/stat.h>
350Sstevel@tonic-gate #include <sys/ddi.h>
360Sstevel@tonic-gate #include <sys/hwconf.h>
370Sstevel@tonic-gate #include <sys/sunddi.h>
380Sstevel@tonic-gate #include <sys/sunndi.h>
390Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
400Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
410Sstevel@tonic-gate #include <sys/modctl.h>
420Sstevel@tonic-gate #include <sys/dacf.h>
430Sstevel@tonic-gate #include <sys/promif.h>
440Sstevel@tonic-gate #include <sys/cpuvar.h>
450Sstevel@tonic-gate #include <sys/pathname.h>
460Sstevel@tonic-gate #include <sys/taskq.h>
470Sstevel@tonic-gate #include <sys/sysevent.h>
480Sstevel@tonic-gate #include <sys/sunmdi.h>
490Sstevel@tonic-gate #include <sys/stream.h>
500Sstevel@tonic-gate #include <sys/strsubr.h>
510Sstevel@tonic-gate #include <sys/fs/snode.h>
520Sstevel@tonic-gate #include <sys/fs/dv_node.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 {
830Sstevel@tonic-gate 	dnode_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 {
970Sstevel@tonic-gate 	char *deviname;
980Sstevel@tonic-gate 	struct brevq_node *sibling;
990Sstevel@tonic-gate 	struct brevq_node *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 
1150Sstevel@tonic-gate /* block all future dev_info state changes */
1160Sstevel@tonic-gate static hrtime_t volatile devinfo_freeze = 0;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate /* number of dev_info attaches/detaches currently in progress */
1190Sstevel@tonic-gate static ulong_t devinfo_attach_detach = 0;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate extern kmutex_t global_vhci_lock;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate /*
1240Sstevel@tonic-gate  * The devinfo snapshot cache and related variables.
1250Sstevel@tonic-gate  * The only field in the di_cache structure that needs initialization
1260Sstevel@tonic-gate  * is the mutex (cache_lock). However, since this is an adaptive mutex
1270Sstevel@tonic-gate  * (MUTEX_DEFAULT) - it is automatically initialized by being allocated
1280Sstevel@tonic-gate  * in zeroed memory (static storage class). Therefore no explicit
1290Sstevel@tonic-gate  * initialization of the di_cache structure is needed.
1300Sstevel@tonic-gate  */
1310Sstevel@tonic-gate struct di_cache	di_cache = {1};
1320Sstevel@tonic-gate int		di_cache_debug = 0;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate /* For ddvis, which needs pseudo children under PCI */
1350Sstevel@tonic-gate int pci_allow_pseudo_children = 0;
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate /*
1380Sstevel@tonic-gate  * The following switch is for service people, in case a
1390Sstevel@tonic-gate  * 3rd party driver depends on identify(9e) being called.
1400Sstevel@tonic-gate  */
1410Sstevel@tonic-gate int identify_9e = 0;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate int mtc_off;					/* turn off mt config */
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate static kmem_cache_t *ddi_node_cache;		/* devinfo node cache */
1460Sstevel@tonic-gate static devinfo_log_header_t *devinfo_audit_log;	/* devinfo log */
1470Sstevel@tonic-gate static int devinfo_log_size;			/* size in pages */
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate static int lookup_compatible(dev_info_t *, uint_t);
1500Sstevel@tonic-gate static char *encode_composite_string(char **, uint_t, size_t *, uint_t);
1510Sstevel@tonic-gate static void link_to_driver_list(dev_info_t *);
1520Sstevel@tonic-gate static void unlink_from_driver_list(dev_info_t *);
1530Sstevel@tonic-gate static void add_to_dn_list(struct devnames *, dev_info_t *);
1540Sstevel@tonic-gate static void remove_from_dn_list(struct devnames *, dev_info_t *);
1550Sstevel@tonic-gate static dev_info_t *find_child_by_callback(dev_info_t *, char *, char *,
1560Sstevel@tonic-gate     int (*)(dev_info_t *, char *, int));
1570Sstevel@tonic-gate static dev_info_t *find_duplicate_child();
1580Sstevel@tonic-gate static void add_global_props(dev_info_t *);
1590Sstevel@tonic-gate static void remove_global_props(dev_info_t *);
1600Sstevel@tonic-gate static int uninit_node(dev_info_t *);
1610Sstevel@tonic-gate static void da_log_init(void);
1620Sstevel@tonic-gate static void da_log_enter(dev_info_t *);
1630Sstevel@tonic-gate static int walk_devs(dev_info_t *, int (*f)(dev_info_t *, void *), void *, int);
1640Sstevel@tonic-gate static int reset_nexus_flags(dev_info_t *, void *);
1650Sstevel@tonic-gate static void ddi_optimize_dtree(dev_info_t *);
1660Sstevel@tonic-gate static int is_leaf_node(dev_info_t *);
1670Sstevel@tonic-gate static struct mt_config_handle *mt_config_init(dev_info_t *, dev_info_t **,
1680Sstevel@tonic-gate     int, major_t, int, struct brevq_node **);
1690Sstevel@tonic-gate static void mt_config_children(struct mt_config_handle *);
1700Sstevel@tonic-gate static void mt_config_driver(struct mt_config_handle *);
1710Sstevel@tonic-gate static int mt_config_fini(struct mt_config_handle *);
1720Sstevel@tonic-gate static int devi_unconfig_common(dev_info_t *, dev_info_t **, int, major_t,
1730Sstevel@tonic-gate     struct brevq_node **);
1740Sstevel@tonic-gate static int
1750Sstevel@tonic-gate ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
1760Sstevel@tonic-gate     dev_info_t **childp, int flags);
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate /*
1790Sstevel@tonic-gate  * dev_info cache and node management
1800Sstevel@tonic-gate  */
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate /* initialize dev_info node cache */
1830Sstevel@tonic-gate void
1840Sstevel@tonic-gate i_ddi_node_cache_init()
1850Sstevel@tonic-gate {
1860Sstevel@tonic-gate 	ASSERT(ddi_node_cache == NULL);
1870Sstevel@tonic-gate 	ddi_node_cache = kmem_cache_create("dev_info_node_cache",
1880Sstevel@tonic-gate 	    sizeof (struct dev_info), 0, NULL, NULL, NULL, NULL, NULL, 0);
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	if (ddidebug & DDI_AUDIT)
1910Sstevel@tonic-gate 		da_log_init();
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate /*
1950Sstevel@tonic-gate  * Allocating a dev_info node, callable from interrupt context with KM_NOSLEEP
1960Sstevel@tonic-gate  * The allocated node has a reference count of 0.
1970Sstevel@tonic-gate  */
1980Sstevel@tonic-gate dev_info_t *
1990Sstevel@tonic-gate i_ddi_alloc_node(dev_info_t *pdip, char *node_name, dnode_t nodeid,
2000Sstevel@tonic-gate     int instance, ddi_prop_t *sys_prop, int flag)
2010Sstevel@tonic-gate {
2020Sstevel@tonic-gate 	struct dev_info *devi;
2030Sstevel@tonic-gate 	struct devi_nodeid *elem;
2040Sstevel@tonic-gate 	static char failed[] = "i_ddi_alloc_node: out of memory";
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	ASSERT(node_name != NULL);
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	if ((devi = kmem_cache_alloc(ddi_node_cache, flag)) == NULL) {
2090Sstevel@tonic-gate 		cmn_err(CE_NOTE, failed);
2100Sstevel@tonic-gate 		return (NULL);
2110Sstevel@tonic-gate 	}
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	bzero(devi, sizeof (struct dev_info));
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	if (devinfo_audit_log) {
2160Sstevel@tonic-gate 		devi->devi_audit = kmem_zalloc(sizeof (devinfo_audit_t), flag);
2170Sstevel@tonic-gate 		if (devi->devi_audit == NULL)
2180Sstevel@tonic-gate 			goto fail;
2190Sstevel@tonic-gate 	}
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	if ((devi->devi_node_name = i_ddi_strdup(node_name, flag)) == NULL)
2220Sstevel@tonic-gate 		goto fail;
2230Sstevel@tonic-gate 	/* default binding name is node name */
2240Sstevel@tonic-gate 	devi->devi_binding_name = devi->devi_node_name;
2250Sstevel@tonic-gate 	devi->devi_major = (major_t)-1;	/* unbound by default */
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	/*
2280Sstevel@tonic-gate 	 * Make a copy of system property
2290Sstevel@tonic-gate 	 */
2300Sstevel@tonic-gate 	if (sys_prop &&
2310Sstevel@tonic-gate 	    (devi->devi_sys_prop_ptr = i_ddi_prop_list_dup(sys_prop, flag))
2320Sstevel@tonic-gate 	    == NULL)
2330Sstevel@tonic-gate 		goto fail;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	/*
2360Sstevel@tonic-gate 	 * Assign devi_nodeid, devi_node_class, devi_node_attributes
2370Sstevel@tonic-gate 	 * according to the following algorithm:
2380Sstevel@tonic-gate 	 *
2390Sstevel@tonic-gate 	 * nodeid arg			node class		node attributes
2400Sstevel@tonic-gate 	 *
2410Sstevel@tonic-gate 	 * DEVI_PSEUDO_NODEID		DDI_NC_PSEUDO		A
2420Sstevel@tonic-gate 	 * DEVI_SID_NODEID		DDI_NC_PSEUDO		A,P
2430Sstevel@tonic-gate 	 * other			DDI_NC_PROM		P
2440Sstevel@tonic-gate 	 *
2450Sstevel@tonic-gate 	 * Where A = DDI_AUTO_ASSIGNED_NODEID (auto-assign a nodeid)
2460Sstevel@tonic-gate 	 * and	 P = DDI_PERSISTENT
2470Sstevel@tonic-gate 	 *
2480Sstevel@tonic-gate 	 * auto-assigned nodeids are also auto-freed.
2490Sstevel@tonic-gate 	 */
2500Sstevel@tonic-gate 	switch (nodeid) {
2510Sstevel@tonic-gate 	case DEVI_SID_NODEID:
2520Sstevel@tonic-gate 		devi->devi_node_attributes = DDI_PERSISTENT;
2530Sstevel@tonic-gate 		if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL)
2540Sstevel@tonic-gate 			goto fail;
2550Sstevel@tonic-gate 		/*FALLTHROUGH*/
2560Sstevel@tonic-gate 	case DEVI_PSEUDO_NODEID:
2570Sstevel@tonic-gate 		devi->devi_node_attributes |= DDI_AUTO_ASSIGNED_NODEID;
2580Sstevel@tonic-gate 		devi->devi_node_class = DDI_NC_PSEUDO;
2590Sstevel@tonic-gate 		if (impl_ddi_alloc_nodeid(&devi->devi_nodeid)) {
2600Sstevel@tonic-gate 			panic("i_ddi_alloc_node: out of nodeids");
2610Sstevel@tonic-gate 			/*NOTREACHED*/
2620Sstevel@tonic-gate 		}
2630Sstevel@tonic-gate 		break;
2640Sstevel@tonic-gate 	default:
2650Sstevel@tonic-gate 		if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL)
2660Sstevel@tonic-gate 			goto fail;
2670Sstevel@tonic-gate 		/*
2680Sstevel@tonic-gate 		 * the nodetype is 'prom', try to 'take' the nodeid now.
2690Sstevel@tonic-gate 		 * This requires memory allocation, so check for failure.
2700Sstevel@tonic-gate 		 */
2710Sstevel@tonic-gate 		if (impl_ddi_take_nodeid(nodeid, flag) != 0) {
2720Sstevel@tonic-gate 			kmem_free(elem, sizeof (*elem));
2730Sstevel@tonic-gate 			goto fail;
2740Sstevel@tonic-gate 		}
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 		devi->devi_nodeid = nodeid;
2770Sstevel@tonic-gate 		devi->devi_node_class = DDI_NC_PROM;
2780Sstevel@tonic-gate 		devi->devi_node_attributes = DDI_PERSISTENT;
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	}
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node((dev_info_t *)devi)) {
2830Sstevel@tonic-gate 		mutex_enter(&devimap->dno_lock);
2840Sstevel@tonic-gate 		elem->next = devimap->dno_free;
2850Sstevel@tonic-gate 		devimap->dno_free = elem;
2860Sstevel@tonic-gate 		mutex_exit(&devimap->dno_lock);
2870Sstevel@tonic-gate 	}
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	/*
2900Sstevel@tonic-gate 	 * Instance is normally initialized to -1. In a few special
2910Sstevel@tonic-gate 	 * cases, the caller may specify an instance (e.g. CPU nodes).
2920Sstevel@tonic-gate 	 */
2930Sstevel@tonic-gate 	devi->devi_instance = instance;
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	/*
2960Sstevel@tonic-gate 	 * set parent and bus_ctl parent
2970Sstevel@tonic-gate 	 */
2980Sstevel@tonic-gate 	devi->devi_parent = DEVI(pdip);
2990Sstevel@tonic-gate 	devi->devi_bus_ctl = DEVI(pdip);
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
3020Sstevel@tonic-gate 	    "i_ddi_alloc_node: name=%s id=%d\n", node_name, devi->devi_nodeid));
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	cv_init(&(devi->devi_cv), NULL, CV_DEFAULT, NULL);
3050Sstevel@tonic-gate 	mutex_init(&(devi->devi_lock), NULL, MUTEX_DEFAULT, NULL);
3060Sstevel@tonic-gate 	mutex_init(&(devi->devi_pm_lock), NULL, MUTEX_DEFAULT, NULL);
3070Sstevel@tonic-gate 	mutex_init(&(devi->devi_pm_busy_lock), NULL, MUTEX_DEFAULT, NULL);
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	i_ddi_set_node_state((dev_info_t *)devi, DS_PROTO);
3100Sstevel@tonic-gate 	da_log_enter((dev_info_t *)devi);
3110Sstevel@tonic-gate 	return ((dev_info_t *)devi);
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate fail:
3140Sstevel@tonic-gate 	if (devi->devi_sys_prop_ptr)
3150Sstevel@tonic-gate 		i_ddi_prop_list_delete(devi->devi_sys_prop_ptr);
3160Sstevel@tonic-gate 	if (devi->devi_node_name)
3170Sstevel@tonic-gate 		kmem_free(devi->devi_node_name, strlen(node_name) + 1);
3180Sstevel@tonic-gate 	if (devi->devi_audit)
3190Sstevel@tonic-gate 		kmem_free(devi->devi_audit, sizeof (devinfo_audit_t));
3200Sstevel@tonic-gate 	kmem_cache_free(ddi_node_cache, devi);
3210Sstevel@tonic-gate 	cmn_err(CE_NOTE, failed);
3220Sstevel@tonic-gate 	return (NULL);
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate /*
3260Sstevel@tonic-gate  * free a dev_info structure.
3270Sstevel@tonic-gate  * NB. Not callable from interrupt since impl_ddi_free_nodeid may block.
3280Sstevel@tonic-gate  */
3290Sstevel@tonic-gate void
3300Sstevel@tonic-gate i_ddi_free_node(dev_info_t *dip)
3310Sstevel@tonic-gate {
3320Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
3330Sstevel@tonic-gate 	struct devi_nodeid *elem;
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	ASSERT(devi->devi_ref == 0);
3360Sstevel@tonic-gate 	ASSERT(devi->devi_addr == NULL);
3370Sstevel@tonic-gate 	ASSERT(devi->devi_node_state == DS_PROTO);
3380Sstevel@tonic-gate 	ASSERT(devi->devi_child == NULL);
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	if (devi->devi_intr_p)
3410Sstevel@tonic-gate 		i_ddi_intr_devi_fini((dev_info_t *)devi);
3420Sstevel@tonic-gate 
343439Scth 	/* free devi_addr_buf allocated by ddi_set_name_addr() */
344439Scth 	if (devi->devi_addr_buf)
345439Scth 		kmem_free(devi->devi_addr_buf, 2 * MAXNAMELEN);
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	if (i_ndi_dev_is_auto_assigned_node(dip))
3480Sstevel@tonic-gate 		impl_ddi_free_nodeid(DEVI(dip)->devi_nodeid);
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
3510Sstevel@tonic-gate 		mutex_enter(&devimap->dno_lock);
3520Sstevel@tonic-gate 		ASSERT(devimap->dno_free);
3530Sstevel@tonic-gate 		elem = devimap->dno_free;
3540Sstevel@tonic-gate 		devimap->dno_free = elem->next;
3550Sstevel@tonic-gate 		mutex_exit(&devimap->dno_lock);
3560Sstevel@tonic-gate 		kmem_free(elem, sizeof (*elem));
3570Sstevel@tonic-gate 	}
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	if (DEVI(dip)->devi_compat_names)
3600Sstevel@tonic-gate 		kmem_free(DEVI(dip)->devi_compat_names,
3610Sstevel@tonic-gate 		    DEVI(dip)->devi_compat_length);
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	ddi_prop_remove_all(dip);	/* remove driver properties */
3640Sstevel@tonic-gate 	if (devi->devi_sys_prop_ptr)
3650Sstevel@tonic-gate 		i_ddi_prop_list_delete(devi->devi_sys_prop_ptr);
3660Sstevel@tonic-gate 	if (devi->devi_hw_prop_ptr)
3670Sstevel@tonic-gate 		i_ddi_prop_list_delete(devi->devi_hw_prop_ptr);
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_INVAL);
3700Sstevel@tonic-gate 	da_log_enter(dip);
3710Sstevel@tonic-gate 	if (devi->devi_audit) {
3720Sstevel@tonic-gate 		kmem_free(devi->devi_audit, sizeof (devinfo_audit_t));
3730Sstevel@tonic-gate 	}
3740Sstevel@tonic-gate 	kmem_free(devi->devi_node_name, strlen(devi->devi_node_name) + 1);
3750Sstevel@tonic-gate 	if (devi->devi_device_class)
3760Sstevel@tonic-gate 		kmem_free(devi->devi_device_class,
3770Sstevel@tonic-gate 		    strlen(devi->devi_device_class) + 1);
3780Sstevel@tonic-gate 	cv_destroy(&(devi->devi_cv));
3790Sstevel@tonic-gate 	mutex_destroy(&(devi->devi_lock));
3800Sstevel@tonic-gate 	mutex_destroy(&(devi->devi_pm_lock));
3810Sstevel@tonic-gate 	mutex_destroy(&(devi->devi_pm_busy_lock));
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	kmem_cache_free(ddi_node_cache, devi);
3840Sstevel@tonic-gate }
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate /*
3880Sstevel@tonic-gate  * Node state transitions
3890Sstevel@tonic-gate  */
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate /*
3920Sstevel@tonic-gate  * Change the node name
3930Sstevel@tonic-gate  */
3940Sstevel@tonic-gate int
3950Sstevel@tonic-gate ndi_devi_set_nodename(dev_info_t *dip, char *name, int flags)
3960Sstevel@tonic-gate {
3970Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags))
3980Sstevel@tonic-gate 	char *nname, *oname;
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	ASSERT(dip && name);
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	oname = DEVI(dip)->devi_node_name;
4030Sstevel@tonic-gate 	if (strcmp(oname, name) == 0)
4040Sstevel@tonic-gate 		return (DDI_SUCCESS);
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	/*
4070Sstevel@tonic-gate 	 * pcicfg_fix_ethernet requires a name change after node
4080Sstevel@tonic-gate 	 * is linked into the tree. When pcicfg is fixed, we
4090Sstevel@tonic-gate 	 * should only allow name change in DS_PROTO state.
4100Sstevel@tonic-gate 	 */
4110Sstevel@tonic-gate 	if (i_ddi_node_state(dip) >= DS_BOUND) {
4120Sstevel@tonic-gate 		/*
4130Sstevel@tonic-gate 		 * Don't allow name change once node is bound
4140Sstevel@tonic-gate 		 */
4150Sstevel@tonic-gate 		cmn_err(CE_NOTE,
4160Sstevel@tonic-gate 		    "ndi_devi_set_nodename: node already bound dip = %p,"
4170Sstevel@tonic-gate 		    " %s -> %s", (void *)dip, ddi_node_name(dip), name);
4180Sstevel@tonic-gate 		return (NDI_FAILURE);
4190Sstevel@tonic-gate 	}
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	nname = i_ddi_strdup(name, KM_SLEEP);
4220Sstevel@tonic-gate 	DEVI(dip)->devi_node_name = nname;
4230Sstevel@tonic-gate 	i_ddi_set_binding_name(dip, nname);
4240Sstevel@tonic-gate 	kmem_free(oname, strlen(oname) + 1);
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	da_log_enter(dip);
4270Sstevel@tonic-gate 	return (NDI_SUCCESS);
4280Sstevel@tonic-gate }
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate void
4310Sstevel@tonic-gate i_ddi_add_devimap(dev_info_t *dip)
4320Sstevel@tonic-gate {
4330Sstevel@tonic-gate 	struct devi_nodeid *elem;
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	ASSERT(dip);
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	if (!ndi_dev_is_persistent_node(dip))
4380Sstevel@tonic-gate 		return;
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 	ASSERT(ddi_get_parent(dip) == NULL || (DEVI_VHCI_NODE(dip)) ||
4410Sstevel@tonic-gate 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	mutex_enter(&devimap->dno_lock);
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	ASSERT(devimap->dno_free);
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 	elem = devimap->dno_free;
4480Sstevel@tonic-gate 	devimap->dno_free = elem->next;
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	elem->nodeid = ddi_get_nodeid(dip);
4510Sstevel@tonic-gate 	elem->dip = dip;
4520Sstevel@tonic-gate 	elem->next = devimap->dno_head;
4530Sstevel@tonic-gate 	devimap->dno_head = elem;
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	devimap->dno_list_length++;
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	mutex_exit(&devimap->dno_lock);
4580Sstevel@tonic-gate }
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate static int
4610Sstevel@tonic-gate i_ddi_remove_devimap(dev_info_t *dip)
4620Sstevel@tonic-gate {
4630Sstevel@tonic-gate 	struct devi_nodeid *prev, *elem;
4640Sstevel@tonic-gate 	static const char *fcn = "i_ddi_remove_devimap";
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	ASSERT(dip);
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	if (!ndi_dev_is_persistent_node(dip))
4690Sstevel@tonic-gate 		return (DDI_SUCCESS);
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	mutex_enter(&devimap->dno_lock);
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	/*
4740Sstevel@tonic-gate 	 * The following check is done with dno_lock held
4750Sstevel@tonic-gate 	 * to prevent race between dip removal and
4760Sstevel@tonic-gate 	 * e_ddi_prom_node_to_dip()
4770Sstevel@tonic-gate 	 */
4780Sstevel@tonic-gate 	if (e_ddi_devi_holdcnt(dip)) {
4790Sstevel@tonic-gate 		mutex_exit(&devimap->dno_lock);
4800Sstevel@tonic-gate 		return (DDI_FAILURE);
4810Sstevel@tonic-gate 	}
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	ASSERT(devimap->dno_head);
4840Sstevel@tonic-gate 	ASSERT(devimap->dno_list_length > 0);
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	prev = NULL;
4870Sstevel@tonic-gate 	for (elem = devimap->dno_head; elem; elem = elem->next) {
4880Sstevel@tonic-gate 		if (elem->dip == dip) {
4890Sstevel@tonic-gate 			ASSERT(elem->nodeid == ddi_get_nodeid(dip));
4900Sstevel@tonic-gate 			break;
4910Sstevel@tonic-gate 		}
4920Sstevel@tonic-gate 		prev = elem;
4930Sstevel@tonic-gate 	}
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	if (elem && prev)
4960Sstevel@tonic-gate 		prev->next = elem->next;
4970Sstevel@tonic-gate 	else if (elem)
4980Sstevel@tonic-gate 		devimap->dno_head = elem->next;
4990Sstevel@tonic-gate 	else
5000Sstevel@tonic-gate 		panic("%s: devinfo node(%p) not found",
5010Sstevel@tonic-gate 		    fcn, (void *)dip);
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	devimap->dno_list_length--;
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	elem->nodeid = 0;
5060Sstevel@tonic-gate 	elem->dip = NULL;
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	elem->next = devimap->dno_free;
5090Sstevel@tonic-gate 	devimap->dno_free = elem;
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	mutex_exit(&devimap->dno_lock);
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	return (DDI_SUCCESS);
5140Sstevel@tonic-gate }
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate /*
5170Sstevel@tonic-gate  * Link this node into the devinfo tree and add to orphan list
5180Sstevel@tonic-gate  * Not callable from interrupt context
5190Sstevel@tonic-gate  */
5200Sstevel@tonic-gate static void
5210Sstevel@tonic-gate link_node(dev_info_t *dip)
5220Sstevel@tonic-gate {
5230Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
5240Sstevel@tonic-gate 	struct dev_info *parent = devi->devi_parent;
5250Sstevel@tonic-gate 	dev_info_t **dipp;
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	ASSERT(parent);	/* never called for root node */
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "link_node: parent = %s child = %s\n",
5300Sstevel@tonic-gate 	    parent->devi_node_name, devi->devi_node_name));
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	/*
5330Sstevel@tonic-gate 	 * Hold the global_vhci_lock before linking any direct
5340Sstevel@tonic-gate 	 * children of rootnex driver. This special lock protects
5350Sstevel@tonic-gate 	 * linking and unlinking for rootnext direct children.
5360Sstevel@tonic-gate 	 */
5370Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
5380Sstevel@tonic-gate 		mutex_enter(&global_vhci_lock);
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	/*
5410Sstevel@tonic-gate 	 * attach the node to end of the list unless the node is already there
5420Sstevel@tonic-gate 	 */
5430Sstevel@tonic-gate 	dipp = (dev_info_t **)(&DEVI(parent)->devi_child);
5440Sstevel@tonic-gate 	while (*dipp && (*dipp != dip)) {
5450Sstevel@tonic-gate 		dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling);
5460Sstevel@tonic-gate 	}
5470Sstevel@tonic-gate 	ASSERT(*dipp == NULL);	/* node is not linked */
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	/*
5500Sstevel@tonic-gate 	 * Now that we are in the tree, update the devi-nodeid map.
5510Sstevel@tonic-gate 	 */
5520Sstevel@tonic-gate 	i_ddi_add_devimap(dip);
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 	/*
5550Sstevel@tonic-gate 	 * This is a temporary workaround for Bug 4618861.
5560Sstevel@tonic-gate 	 * We keep the scsi_vhci nexus node on the left side of the devinfo
5570Sstevel@tonic-gate 	 * tree (under the root nexus driver), so that virtual nodes under
5580Sstevel@tonic-gate 	 * scsi_vhci will be SUSPENDed first and RESUMEd last.  This ensures
5590Sstevel@tonic-gate 	 * that the pHCI nodes are active during times when their clients
5600Sstevel@tonic-gate 	 * may be depending on them.  This workaround embodies the knowledge
5610Sstevel@tonic-gate 	 * that system PM and CPR both traverse the tree left-to-right during
5620Sstevel@tonic-gate 	 * SUSPEND and right-to-left during RESUME.
5630Sstevel@tonic-gate 	 */
5640Sstevel@tonic-gate 	if (strcmp(devi->devi_name, "scsi_vhci") == 0) {
5650Sstevel@tonic-gate 		/* Add scsi_vhci to beginning of list */
5660Sstevel@tonic-gate 		ASSERT((dev_info_t *)parent == top_devinfo);
5670Sstevel@tonic-gate 		/* scsi_vhci under rootnex */
5680Sstevel@tonic-gate 		devi->devi_sibling = parent->devi_child;
5690Sstevel@tonic-gate 		parent->devi_child = devi;
5700Sstevel@tonic-gate 	} else {
5710Sstevel@tonic-gate 		/* Add to end of list */
5720Sstevel@tonic-gate 		*dipp = dip;
5730Sstevel@tonic-gate 		DEVI(dip)->devi_sibling = NULL;
5740Sstevel@tonic-gate 	}
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	/*
5770Sstevel@tonic-gate 	 * Release the global_vhci_lock before linking any direct
5780Sstevel@tonic-gate 	 * children of rootnex driver.
5790Sstevel@tonic-gate 	 */
5800Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
5810Sstevel@tonic-gate 		mutex_exit(&global_vhci_lock);
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	/* persistent nodes go on orphan list */
5840Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip))
5850Sstevel@tonic-gate 		add_to_dn_list(&orphanlist, dip);
5860Sstevel@tonic-gate }
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate /*
5890Sstevel@tonic-gate  * Unlink this node from the devinfo tree
5900Sstevel@tonic-gate  */
5910Sstevel@tonic-gate static int
5920Sstevel@tonic-gate unlink_node(dev_info_t *dip)
5930Sstevel@tonic-gate {
5940Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
5950Sstevel@tonic-gate 	struct dev_info *parent = devi->devi_parent;
5960Sstevel@tonic-gate 	dev_info_t **dipp;
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	ASSERT(parent != NULL);
5990Sstevel@tonic-gate 	ASSERT(devi->devi_node_state == DS_LINKED);
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "unlink_node: name = %s\n",
6020Sstevel@tonic-gate 	    ddi_node_name(dip)));
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	/* check references */
6050Sstevel@tonic-gate 	if (devi->devi_ref || i_ddi_remove_devimap(dip) != DDI_SUCCESS)
6060Sstevel@tonic-gate 		return (DDI_FAILURE);
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	/*
6090Sstevel@tonic-gate 	 * Hold the global_vhci_lock before linking any direct
6100Sstevel@tonic-gate 	 * children of rootnex driver.
6110Sstevel@tonic-gate 	 */
6120Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
6130Sstevel@tonic-gate 		mutex_enter(&global_vhci_lock);
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	dipp = (dev_info_t **)(&DEVI(parent)->devi_child);
6160Sstevel@tonic-gate 	while (*dipp && (*dipp != dip)) {
6170Sstevel@tonic-gate 		dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling);
6180Sstevel@tonic-gate 	}
6190Sstevel@tonic-gate 	if (*dipp) {
6200Sstevel@tonic-gate 		*dipp = (dev_info_t *)(devi->devi_sibling);
6210Sstevel@tonic-gate 		devi->devi_sibling = NULL;
6220Sstevel@tonic-gate 	} else {
6230Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE, "unlink_node: %s not linked",
6240Sstevel@tonic-gate 		    devi->devi_node_name));
6250Sstevel@tonic-gate 	}
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	/*
6280Sstevel@tonic-gate 	 * Release the global_vhci_lock before linking any direct
6290Sstevel@tonic-gate 	 * children of rootnex driver.
6300Sstevel@tonic-gate 	 */
6310Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
6320Sstevel@tonic-gate 		mutex_exit(&global_vhci_lock);
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	/* Remove node from orphan list */
6350Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
6360Sstevel@tonic-gate 		remove_from_dn_list(&orphanlist, dip);
6370Sstevel@tonic-gate 	}
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	return (DDI_SUCCESS);
6400Sstevel@tonic-gate }
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate /*
6430Sstevel@tonic-gate  * Bind this devinfo node to a driver. If compat is NON-NULL, try that first.
6440Sstevel@tonic-gate  * Else, use the node-name.
6450Sstevel@tonic-gate  *
6460Sstevel@tonic-gate  * NOTE: IEEE1275 specifies that nodename should be tried before compatible.
6470Sstevel@tonic-gate  *	Solaris implementation binds nodename after compatible.
6480Sstevel@tonic-gate  *
6490Sstevel@tonic-gate  * If we find a binding,
6500Sstevel@tonic-gate  * - set the binding name to the the string,
6510Sstevel@tonic-gate  * - set major number to driver major
6520Sstevel@tonic-gate  *
6530Sstevel@tonic-gate  * If we don't find a binding,
6540Sstevel@tonic-gate  * - return failure
6550Sstevel@tonic-gate  */
6560Sstevel@tonic-gate static int
6570Sstevel@tonic-gate bind_node(dev_info_t *dip)
6580Sstevel@tonic-gate {
6590Sstevel@tonic-gate 	char *p = NULL;
6600Sstevel@tonic-gate 	major_t major = (major_t)(major_t)-1;
6610Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
6620Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	ASSERT(devi->devi_node_state == DS_LINKED);
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "bind_node: 0x%p(name = %s)\n",
6670Sstevel@tonic-gate 	    (void *)dip, ddi_node_name(dip)));
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
6700Sstevel@tonic-gate 	if (DEVI(dip)->devi_flags & DEVI_NO_BIND) {
6710Sstevel@tonic-gate 		mutex_exit(&DEVI(dip)->devi_lock);
6720Sstevel@tonic-gate 		return (DDI_FAILURE);
6730Sstevel@tonic-gate 	}
6740Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	/* find the driver with most specific binding using compatible */
6770Sstevel@tonic-gate 	major = ddi_compatible_driver_major(dip, &p);
6780Sstevel@tonic-gate 	if (major == (major_t)-1)
6790Sstevel@tonic-gate 		return (DDI_FAILURE);
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	devi->devi_major = major;
6820Sstevel@tonic-gate 	if (p != NULL) {
6830Sstevel@tonic-gate 		i_ddi_set_binding_name(dip, p);
6840Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "bind_node: %s bound to %s\n",
6850Sstevel@tonic-gate 		    devi->devi_node_name, p));
6860Sstevel@tonic-gate 	}
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	/* Link node to per-driver list */
6890Sstevel@tonic-gate 	link_to_driver_list(dip);
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	/*
6920Sstevel@tonic-gate 	 * reset parent flag so that nexus will merge .conf props
6930Sstevel@tonic-gate 	 */
6940Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
6950Sstevel@tonic-gate 		mutex_enter(&DEVI(parent)->devi_lock);
6960Sstevel@tonic-gate 		DEVI(parent)->devi_flags &=
6970Sstevel@tonic-gate 		    ~(DEVI_ATTACHED_CHILDREN|DEVI_MADE_CHILDREN);
6980Sstevel@tonic-gate 		mutex_exit(&DEVI(parent)->devi_lock);
6990Sstevel@tonic-gate 	}
7000Sstevel@tonic-gate 	return (DDI_SUCCESS);
7010Sstevel@tonic-gate }
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate /*
7040Sstevel@tonic-gate  * Unbind this devinfo node
7050Sstevel@tonic-gate  * Called before the node is destroyed or driver is removed from system
7060Sstevel@tonic-gate  */
7070Sstevel@tonic-gate static int
7080Sstevel@tonic-gate unbind_node(dev_info_t *dip)
7090Sstevel@tonic-gate {
7100Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_node_state == DS_BOUND);
7110Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_major != (major_t)-1);
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	/* check references */
7140Sstevel@tonic-gate 	if (DEVI(dip)->devi_ref)
7150Sstevel@tonic-gate 		return (DDI_FAILURE);
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "unbind_node: 0x%p(name = %s)\n",
7180Sstevel@tonic-gate 	    (void *)dip, ddi_node_name(dip)));
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	unlink_from_driver_list(dip);
7210Sstevel@tonic-gate 	DEVI(dip)->devi_major = (major_t)-1;
7220Sstevel@tonic-gate 	return (DDI_SUCCESS);
7230Sstevel@tonic-gate }
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate /*
7260Sstevel@tonic-gate  * Initialize a node: calls the parent nexus' bus_ctl ops to do the operation.
7270Sstevel@tonic-gate  * Must hold parent and per-driver list while calling this function.
7280Sstevel@tonic-gate  * A successful init_node() returns with an active ndi_hold_devi() hold on
7290Sstevel@tonic-gate  * the parent.
7300Sstevel@tonic-gate  */
7310Sstevel@tonic-gate static int
7320Sstevel@tonic-gate init_node(dev_info_t *dip)
7330Sstevel@tonic-gate {
7340Sstevel@tonic-gate 	int error;
7350Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
7360Sstevel@tonic-gate 	int (*f)(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *, void *);
7370Sstevel@tonic-gate 	char *path;
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_BOUND);
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 	/* should be DS_READY except for pcmcia ... */
7420Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(pdip) >= DS_PROBED);
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7450Sstevel@tonic-gate 	(void) ddi_pathname(dip, path);
7460Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "init_node: entry: path %s 0x%p\n",
7470Sstevel@tonic-gate 	    path, (void *)dip));
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	/*
7500Sstevel@tonic-gate 	 * The parent must have a bus_ctl operation.
7510Sstevel@tonic-gate 	 */
7520Sstevel@tonic-gate 	if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
7530Sstevel@tonic-gate 	    (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_ctl) == NULL) {
7540Sstevel@tonic-gate 		error = DDI_FAILURE;
7550Sstevel@tonic-gate 		goto out;
7560Sstevel@tonic-gate 	}
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 	add_global_props(dip);
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 	/*
7610Sstevel@tonic-gate 	 * Invoke the parent's bus_ctl operation with the DDI_CTLOPS_INITCHILD
7620Sstevel@tonic-gate 	 * command to transform the child to canonical form 1. If there
7630Sstevel@tonic-gate 	 * is an error, ddi_remove_child should be called, to clean up.
7640Sstevel@tonic-gate 	 */
7650Sstevel@tonic-gate 	error = (*f)(pdip, pdip, DDI_CTLOPS_INITCHILD, dip, NULL);
7660Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
7670Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "init_node: %s 0x%p failed\n",
7680Sstevel@tonic-gate 		    path, (void *)dip));
7690Sstevel@tonic-gate 		remove_global_props(dip);
7700Sstevel@tonic-gate 		/* in case nexus driver didn't clear this field */
7710Sstevel@tonic-gate 		ddi_set_name_addr(dip, NULL);
7720Sstevel@tonic-gate 		error = DDI_FAILURE;
7730Sstevel@tonic-gate 		goto out;
7740Sstevel@tonic-gate 	}
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	ndi_hold_devi(pdip);
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	/* check for duplicate nodes */
7790Sstevel@tonic-gate 	if (find_duplicate_child(pdip, dip) != NULL) {
7800Sstevel@tonic-gate 		/* recompute path after initchild for @addr information */
7810Sstevel@tonic-gate 		(void) ddi_pathname(dip, path);
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 		/*
7840Sstevel@tonic-gate 		 * uninit_node() the duplicate - a successful uninit_node()
7850Sstevel@tonic-gate 		 * does a ndi_rele_devi
7860Sstevel@tonic-gate 		 */
7870Sstevel@tonic-gate 		if ((error = uninit_node(dip)) != DDI_SUCCESS) {
7880Sstevel@tonic-gate 			ndi_rele_devi(pdip);
7890Sstevel@tonic-gate 			cmn_err(CE_WARN, "init_node: uninit of duplicate "
7900Sstevel@tonic-gate 			    "node %s failed", path);
7910Sstevel@tonic-gate 		}
7920Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "init_node: duplicate uninit "
7930Sstevel@tonic-gate 		    "%s 0x%p%s\n", path, (void *)dip,
7940Sstevel@tonic-gate 		    (error == DDI_SUCCESS) ? "" : " failed"));
7950Sstevel@tonic-gate 		error = DDI_FAILURE;
7960Sstevel@tonic-gate 		goto out;
7970Sstevel@tonic-gate 	}
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 	/*
8000Sstevel@tonic-gate 	 * Apply multi-parent/deep-nexus optimization to the new node
8010Sstevel@tonic-gate 	 */
8020Sstevel@tonic-gate 	DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
8030Sstevel@tonic-gate 	ddi_optimize_dtree(dip);
8040Sstevel@tonic-gate 	error = DDI_SUCCESS;
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate out:	kmem_free(path, MAXPATHLEN);
8070Sstevel@tonic-gate 	return (error);
8080Sstevel@tonic-gate }
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate /*
8110Sstevel@tonic-gate  * Uninitialize node
8120Sstevel@tonic-gate  * The per-driver list must be held busy during the call.
8130Sstevel@tonic-gate  * A successful uninit_node() releases the init_node() hold on
8140Sstevel@tonic-gate  * the parent by calling ndi_rele_devi().
8150Sstevel@tonic-gate  */
8160Sstevel@tonic-gate static int
8170Sstevel@tonic-gate uninit_node(dev_info_t *dip)
8180Sstevel@tonic-gate {
8190Sstevel@tonic-gate 	int node_state_entry;
8200Sstevel@tonic-gate 	dev_info_t *pdip;
8210Sstevel@tonic-gate 	struct dev_ops *ops;
8220Sstevel@tonic-gate 	int (*f)();
8230Sstevel@tonic-gate 	int error;
8240Sstevel@tonic-gate 	char *addr;
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 	/*
8270Sstevel@tonic-gate 	 * Don't check for references here or else a ref-counted
8280Sstevel@tonic-gate 	 * dip cannot be downgraded by the framework.
8290Sstevel@tonic-gate 	 */
8300Sstevel@tonic-gate 	node_state_entry = i_ddi_node_state(dip);
8310Sstevel@tonic-gate 	ASSERT((node_state_entry == DS_BOUND) ||
8320Sstevel@tonic-gate 		(node_state_entry == DS_INITIALIZED));
8330Sstevel@tonic-gate 	pdip = ddi_get_parent(dip);
8340Sstevel@tonic-gate 	ASSERT(pdip);
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "uninit_node: 0x%p(%s%d)\n",
8370Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 	if (((ops = ddi_get_driver(pdip)) == NULL) ||
8400Sstevel@tonic-gate 	    (ops->devo_bus_ops == NULL) ||
8410Sstevel@tonic-gate 	    ((f = ops->devo_bus_ops->bus_ctl) == NULL)) {
8420Sstevel@tonic-gate 		return (DDI_FAILURE);
8430Sstevel@tonic-gate 	}
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 	/*
8460Sstevel@tonic-gate 	 * save the @addr prior to DDI_CTLOPS_UNINITCHILD for use in
8470Sstevel@tonic-gate 	 * freeing the instance if it succeeds.
8480Sstevel@tonic-gate 	 */
8490Sstevel@tonic-gate 	if (node_state_entry == DS_INITIALIZED) {
8500Sstevel@tonic-gate 		addr = ddi_get_name_addr(dip);
8510Sstevel@tonic-gate 		if (addr)
8520Sstevel@tonic-gate 			addr = i_ddi_strdup(addr, KM_SLEEP);
8530Sstevel@tonic-gate 	} else {
8540Sstevel@tonic-gate 		addr = NULL;
8550Sstevel@tonic-gate 	}
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	error = (*f)(pdip, pdip, DDI_CTLOPS_UNINITCHILD, dip, (void *)NULL);
8580Sstevel@tonic-gate 	if (error == DDI_SUCCESS) {
8590Sstevel@tonic-gate 		/* if uninitchild forgot to set devi_addr to NULL do it now */
8600Sstevel@tonic-gate 		ddi_set_name_addr(dip, NULL);
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 		/*
8630Sstevel@tonic-gate 		 * Free instance number. This is a no-op if instance has
8640Sstevel@tonic-gate 		 * been kept by probe_node().  Avoid free when we are called
8650Sstevel@tonic-gate 		 * from init_node (DS_BOUND) because the instance has not yet
8660Sstevel@tonic-gate 		 * been assigned.
8670Sstevel@tonic-gate 		 */
8680Sstevel@tonic-gate 		if (node_state_entry == DS_INITIALIZED) {
8690Sstevel@tonic-gate 			e_ddi_free_instance(dip, addr);
8700Sstevel@tonic-gate 			DEVI(dip)->devi_instance = -1;
8710Sstevel@tonic-gate 		}
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 		/* release the init_node hold */
8740Sstevel@tonic-gate 		ndi_rele_devi(pdip);
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 		remove_global_props(dip);
8770Sstevel@tonic-gate 		e_ddi_prop_remove_all(dip);
8780Sstevel@tonic-gate 	} else {
8790Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "uninit_node failed: 0x%p(%s%d)\n",
8800Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
8810Sstevel@tonic-gate 	}
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	if (addr)
8840Sstevel@tonic-gate 		kmem_free(addr, strlen(addr) + 1);
8850Sstevel@tonic-gate 	return (error);
8860Sstevel@tonic-gate }
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate /*
8890Sstevel@tonic-gate  * Invoke driver's probe entry point to probe for existence of hardware.
8900Sstevel@tonic-gate  * Keep instance permanent for successful probe and leaf nodes.
8910Sstevel@tonic-gate  *
8920Sstevel@tonic-gate  * Per-driver list must be held busy while calling this function.
8930Sstevel@tonic-gate  */
8940Sstevel@tonic-gate static int
8950Sstevel@tonic-gate probe_node(dev_info_t *dip)
8960Sstevel@tonic-gate {
8970Sstevel@tonic-gate 	int rv;
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_INITIALIZED);
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "probe_node: 0x%p(%s%d)\n",
9020Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	/* temporarily hold the driver while we probe */
9050Sstevel@tonic-gate 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
9060Sstevel@tonic-gate 	if (DEVI(dip)->devi_ops == NULL) {
9070Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
9080Sstevel@tonic-gate 		    "probe_node: 0x%p(%s%d) cannot load driver\n",
9090Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
9100Sstevel@tonic-gate 		return (DDI_FAILURE);
9110Sstevel@tonic-gate 	}
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 	if (identify_9e != 0)
9140Sstevel@tonic-gate 		(void) devi_identify(dip);
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 	rv = devi_probe(dip);
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	/* release the driver now that probe is complete */
9190Sstevel@tonic-gate 	ndi_rele_driver(dip);
9200Sstevel@tonic-gate 	DEVI(dip)->devi_ops = NULL;
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 	switch (rv) {
9230Sstevel@tonic-gate 	case DDI_PROBE_SUCCESS:			/* found */
9240Sstevel@tonic-gate 	case DDI_PROBE_DONTCARE:		/* ddi_dev_is_sid */
9250Sstevel@tonic-gate 		e_ddi_keep_instance(dip);	/* persist instance */
9260Sstevel@tonic-gate 		rv = DDI_SUCCESS;
9270Sstevel@tonic-gate 		break;
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	case DDI_PROBE_PARTIAL:			/* maybe later */
9300Sstevel@tonic-gate 	case DDI_PROBE_FAILURE:			/* not found */
9310Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
9320Sstevel@tonic-gate 		    "probe_node: 0x%p(%s%d) no hardware found%s\n",
9330Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip),
9340Sstevel@tonic-gate 		    (rv == DDI_PROBE_PARTIAL) ? " yet" : ""));
9350Sstevel@tonic-gate 		rv = DDI_FAILURE;
9360Sstevel@tonic-gate 		break;
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	default:
9390Sstevel@tonic-gate #ifdef	DEBUG
9400Sstevel@tonic-gate 		cmn_err(CE_WARN, "probe_node: %s%d: illegal probe(9E) value",
9410Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
9420Sstevel@tonic-gate #endif	/* DEBUG */
9430Sstevel@tonic-gate 		rv = DDI_FAILURE;
9440Sstevel@tonic-gate 		break;
9450Sstevel@tonic-gate 	}
9460Sstevel@tonic-gate 	return (rv);
9470Sstevel@tonic-gate }
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate /*
9500Sstevel@tonic-gate  * Unprobe a node. Simply reset the node state.
9510Sstevel@tonic-gate  * Per-driver list must be held busy while calling this function.
9520Sstevel@tonic-gate  */
9530Sstevel@tonic-gate static int
9540Sstevel@tonic-gate unprobe_node(dev_info_t *dip)
9550Sstevel@tonic-gate {
9560Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_PROBED);
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 	/*
9590Sstevel@tonic-gate 	 * Don't check for references here or else a ref-counted
9600Sstevel@tonic-gate 	 * dip cannot be downgraded by the framework.
9610Sstevel@tonic-gate 	 */
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "unprobe_node: 0x%p(name = %s)\n",
9640Sstevel@tonic-gate 	    (void *)dip, ddi_node_name(dip)));
9650Sstevel@tonic-gate 	return (DDI_SUCCESS);
9660Sstevel@tonic-gate }
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate /*
9690Sstevel@tonic-gate  * Attach devinfo node.
9700Sstevel@tonic-gate  * Per-driver list must be held busy.
9710Sstevel@tonic-gate  */
9720Sstevel@tonic-gate static int
9730Sstevel@tonic-gate attach_node(dev_info_t *dip)
9740Sstevel@tonic-gate {
9750Sstevel@tonic-gate 	int rv;
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_PROBED);
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d)\n",
9800Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate 	/*
9830Sstevel@tonic-gate 	 * Tell mpxio framework that a node is about to online.
9840Sstevel@tonic-gate 	 */
9850Sstevel@tonic-gate 	if ((rv = mdi_devi_online(dip, 0)) != NDI_SUCCESS) {
9860Sstevel@tonic-gate 		return (DDI_FAILURE);
9870Sstevel@tonic-gate 	}
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 	/* no recursive attachment */
9900Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_ops == NULL);
9910Sstevel@tonic-gate 
9920Sstevel@tonic-gate 	/*
9930Sstevel@tonic-gate 	 * Hold driver the node is bound to.
9940Sstevel@tonic-gate 	 */
9950Sstevel@tonic-gate 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
9960Sstevel@tonic-gate 	if (DEVI(dip)->devi_ops == NULL) {
9970Sstevel@tonic-gate 		/*
9980Sstevel@tonic-gate 		 * We were able to load driver for probing, so we should
9990Sstevel@tonic-gate 		 * not get here unless something really bad happened.
10000Sstevel@tonic-gate 		 */
10010Sstevel@tonic-gate 		cmn_err(CE_WARN, "attach_node: no driver for major %d",
10020Sstevel@tonic-gate 		    DEVI(dip)->devi_major);
10030Sstevel@tonic-gate 		return (DDI_FAILURE);
10040Sstevel@tonic-gate 	}
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	if (NEXUS_DRV(DEVI(dip)->devi_ops))
10070Sstevel@tonic-gate 		DEVI(dip)->devi_taskq = ddi_taskq_create(dip,
10080Sstevel@tonic-gate 		    "nexus_enum_tq", 1,
10090Sstevel@tonic-gate 		    TASKQ_DEFAULTPRI, 0);
10100Sstevel@tonic-gate 
1011495Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
10120Sstevel@tonic-gate 	DEVI_SET_ATTACHING(dip);
10130Sstevel@tonic-gate 	DEVI_SET_NEED_RESET(dip);
1014495Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
1015495Scth 
10160Sstevel@tonic-gate 	rv = devi_attach(dip, DDI_ATTACH);
1017495Scth 
1018495Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
10190Sstevel@tonic-gate 	if (rv != DDI_SUCCESS)
10200Sstevel@tonic-gate 		DEVI_CLR_NEED_RESET(dip);
10210Sstevel@tonic-gate 	DEVI_CLR_ATTACHING(dip);
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 	if (rv != DDI_SUCCESS) {
1024438Scth 		/* ensure that devids are unregistered */
10250Sstevel@tonic-gate 		if (DEVI(dip)->devi_flags & DEVI_REGISTERED_DEVID) {
1026438Scth 			DEVI(dip)->devi_flags &= ~DEVI_REGISTERED_DEVID;
1027438Scth 			mutex_exit(&DEVI(dip)->devi_lock);
1028438Scth 
10290Sstevel@tonic-gate 			e_devid_cache_unregister(dip);
1030438Scth 		} else
1031438Scth 			mutex_exit(&DEVI(dip)->devi_lock);
1032438Scth 
10330Sstevel@tonic-gate 		/*
10340Sstevel@tonic-gate 		 * Cleanup dacf reservations
10350Sstevel@tonic-gate 		 */
10360Sstevel@tonic-gate 		mutex_enter(&dacf_lock);
10370Sstevel@tonic-gate 		dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH);
10380Sstevel@tonic-gate 		dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH);
10390Sstevel@tonic-gate 		mutex_exit(&dacf_lock);
10400Sstevel@tonic-gate 		if (DEVI(dip)->devi_taskq)
10410Sstevel@tonic-gate 			ddi_taskq_destroy(DEVI(dip)->devi_taskq);
10420Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate 		/* release the driver if attach failed */
10450Sstevel@tonic-gate 		ndi_rele_driver(dip);
10460Sstevel@tonic-gate 		DEVI(dip)->devi_ops = NULL;
10470Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d) failed\n",
10480Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
10490Sstevel@tonic-gate 		return (DDI_FAILURE);
1050495Scth 	} else
1051495Scth 		mutex_exit(&DEVI(dip)->devi_lock);
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 	/* successful attach, return with driver held */
1054495Scth 
10550Sstevel@tonic-gate 	return (DDI_SUCCESS);
10560Sstevel@tonic-gate }
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate /*
10590Sstevel@tonic-gate  * Detach devinfo node.
10600Sstevel@tonic-gate  * Per-driver list must be held busy.
10610Sstevel@tonic-gate  */
10620Sstevel@tonic-gate static int
10630Sstevel@tonic-gate detach_node(dev_info_t *dip, uint_t flag)
10640Sstevel@tonic-gate {
106553Scth 	struct devnames	*dnp;
106653Scth 	int		rv;
106753Scth 
10680Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_ATTACHED);
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 	/* check references */
10710Sstevel@tonic-gate 	if (DEVI(dip)->devi_ref)
10720Sstevel@tonic-gate 		return (DDI_FAILURE);
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "detach_node: 0x%p(%s%d)\n",
10750Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 	/* Offline the device node with the mpxio framework. */
10780Sstevel@tonic-gate 	if (mdi_devi_offline(dip, flag) != NDI_SUCCESS) {
10790Sstevel@tonic-gate 		return (DDI_FAILURE);
10800Sstevel@tonic-gate 	}
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 	/* drain the taskq */
10830Sstevel@tonic-gate 	if (DEVI(dip)->devi_taskq)
10840Sstevel@tonic-gate 		ddi_taskq_wait(DEVI(dip)->devi_taskq);
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	rv = devi_detach(dip, DDI_DETACH);
1087495Scth 	if (rv == DDI_SUCCESS) {
1088495Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
10890Sstevel@tonic-gate 		DEVI_CLR_NEED_RESET(dip);
1090495Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
1091495Scth 	}
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 	if (rv != DDI_SUCCESS) {
10940Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
10950Sstevel@tonic-gate 		    "detach_node: 0x%p(%s%d) failed\n",
10960Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
10970Sstevel@tonic-gate 		return (DDI_FAILURE);
10980Sstevel@tonic-gate 	}
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate 	/* destroy the taskq */
11010Sstevel@tonic-gate 	if (DEVI(dip)->devi_taskq) {
11020Sstevel@tonic-gate 		ddi_taskq_destroy(DEVI(dip)->devi_taskq);
11030Sstevel@tonic-gate 		DEVI(dip)->devi_taskq = NULL;
11040Sstevel@tonic-gate 	}
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate 	/* Cleanup dacf reservations */
11070Sstevel@tonic-gate 	mutex_enter(&dacf_lock);
11080Sstevel@tonic-gate 	dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH);
11090Sstevel@tonic-gate 	dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH);
11100Sstevel@tonic-gate 	mutex_exit(&dacf_lock);
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 	/* Remove properties and minor nodes in case driver forgots */
11130Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
11140Sstevel@tonic-gate 	ddi_prop_remove_all(dip);
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 	/* a detached node can't have attached or .conf children */
11170Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
11180Sstevel@tonic-gate 	DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN|DEVI_ATTACHED_CHILDREN);
1119438Scth 
1120438Scth 	/* ensure that devids registered during attach are unregistered */
11210Sstevel@tonic-gate 	if (DEVI(dip)->devi_flags & DEVI_REGISTERED_DEVID) {
11220Sstevel@tonic-gate 		DEVI(dip)->devi_flags &= ~DEVI_REGISTERED_DEVID;
1123438Scth 		mutex_exit(&DEVI(dip)->devi_lock);
1124438Scth 
1125438Scth 		e_devid_cache_unregister(dip);
1126438Scth 	} else
1127438Scth 		mutex_exit(&DEVI(dip)->devi_lock);
11280Sstevel@tonic-gate 
112953Scth 	/*
113053Scth 	 * If the instance has successfully detached in detach_driver() context,
113153Scth 	 * clear DN_DRIVER_HELD for correct ddi_hold_installed_driver()
113253Scth 	 * behavior. Consumers like qassociate() depend on this (via clnopen()).
113353Scth 	 */
113453Scth 	if (flag & NDI_DETACH_DRIVER) {
113553Scth 		dnp = &(devnamesp[DEVI(dip)->devi_major]);
113653Scth 		LOCK_DEV_OPS(&dnp->dn_lock);
113753Scth 		dnp->dn_flags &= ~DN_DRIVER_HELD;
113853Scth 		UNLOCK_DEV_OPS(&dnp->dn_lock);
113953Scth 	}
114053Scth 
11410Sstevel@tonic-gate 	/* successful detach, release the driver */
11420Sstevel@tonic-gate 	ndi_rele_driver(dip);
11430Sstevel@tonic-gate 	DEVI(dip)->devi_ops = NULL;
11440Sstevel@tonic-gate 	return (DDI_SUCCESS);
11450Sstevel@tonic-gate }
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate /*
11480Sstevel@tonic-gate  * Run dacf post_attach routines
11490Sstevel@tonic-gate  */
11500Sstevel@tonic-gate static int
11510Sstevel@tonic-gate postattach_node(dev_info_t *dip)
11520Sstevel@tonic-gate {
11530Sstevel@tonic-gate 	int rval;
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 	/*
11560Sstevel@tonic-gate 	 * For hotplug busses like USB, it's possible that devices
11570Sstevel@tonic-gate 	 * are removed but dip is still around. We don't want to
11580Sstevel@tonic-gate 	 * run dacf routines as part of detach failure recovery.
11590Sstevel@tonic-gate 	 *
11600Sstevel@tonic-gate 	 * Pretend success until we figure out how to prevent
11610Sstevel@tonic-gate 	 * access to such devinfo nodes.
11620Sstevel@tonic-gate 	 */
11630Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_REMOVED(dip))
11640Sstevel@tonic-gate 		return (DDI_SUCCESS);
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate 	/*
11670Sstevel@tonic-gate 	 * if dacf_postattach failed, report it to the framework
11680Sstevel@tonic-gate 	 * so that it can be retried later at the open time.
11690Sstevel@tonic-gate 	 */
11700Sstevel@tonic-gate 	mutex_enter(&dacf_lock);
11710Sstevel@tonic-gate 	rval = dacfc_postattach(dip);
11720Sstevel@tonic-gate 	mutex_exit(&dacf_lock);
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 	/*
11750Sstevel@tonic-gate 	 * Plumbing during postattach may fail because of the
11760Sstevel@tonic-gate 	 * underlying device is not ready. This will fail ndi_devi_config()
11770Sstevel@tonic-gate 	 * in dv_filldir() and a warning message is issued. The message
11780Sstevel@tonic-gate 	 * from here will explain what happened
11790Sstevel@tonic-gate 	 */
11800Sstevel@tonic-gate 	if (rval != DACF_SUCCESS) {
11810Sstevel@tonic-gate 		cmn_err(CE_WARN, "Postattach failed for %s%d\n",
11820Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
11830Sstevel@tonic-gate 		return (DDI_FAILURE);
11840Sstevel@tonic-gate 	}
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate 	return (DDI_SUCCESS);
11870Sstevel@tonic-gate }
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate /*
11900Sstevel@tonic-gate  * Run dacf pre-detach routines
11910Sstevel@tonic-gate  */
11920Sstevel@tonic-gate static int
11930Sstevel@tonic-gate predetach_node(dev_info_t *dip, uint_t flag)
11940Sstevel@tonic-gate {
11950Sstevel@tonic-gate 	int ret;
11960Sstevel@tonic-gate 
11970Sstevel@tonic-gate 	/*
11980Sstevel@tonic-gate 	 * Don't auto-detach if DDI_FORCEATTACH or DDI_NO_AUTODETACH
11990Sstevel@tonic-gate 	 * properties are set.
12000Sstevel@tonic-gate 	 */
12010Sstevel@tonic-gate 	if (flag & NDI_AUTODETACH) {
12020Sstevel@tonic-gate 		struct devnames *dnp;
12030Sstevel@tonic-gate 		int pflag = DDI_PROP_NOTPROM | DDI_PROP_DONTPASS;
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 		if ((ddi_prop_get_int(DDI_DEV_T_ANY, dip,
12060Sstevel@tonic-gate 			pflag, DDI_FORCEATTACH, 0) == 1) ||
12070Sstevel@tonic-gate 		    (ddi_prop_get_int(DDI_DEV_T_ANY, dip,
12080Sstevel@tonic-gate 			pflag, DDI_NO_AUTODETACH, 0) == 1))
12090Sstevel@tonic-gate 			return (DDI_FAILURE);
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 		/* check for driver global version of DDI_NO_AUTODETACH */
12120Sstevel@tonic-gate 		dnp = &devnamesp[DEVI(dip)->devi_major];
12130Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
12140Sstevel@tonic-gate 		if (dnp->dn_flags & DN_NO_AUTODETACH) {
12150Sstevel@tonic-gate 			UNLOCK_DEV_OPS(&dnp->dn_lock);
12160Sstevel@tonic-gate 			return (DDI_FAILURE);
12170Sstevel@tonic-gate 		}
12180Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
12190Sstevel@tonic-gate 	}
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate 	mutex_enter(&dacf_lock);
12220Sstevel@tonic-gate 	ret = dacfc_predetach(dip);
12230Sstevel@tonic-gate 	mutex_exit(&dacf_lock);
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate 	return (ret);
12260Sstevel@tonic-gate }
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate /*
12290Sstevel@tonic-gate  * Wrapper for making multiple state transitions
12300Sstevel@tonic-gate  */
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate /*
12330Sstevel@tonic-gate  * i_ndi_config_node: upgrade dev_info node into a specified state.
12340Sstevel@tonic-gate  * It is a bit tricky because the locking protocol changes before and
12350Sstevel@tonic-gate  * after a node is bound to a driver. All locks are held external to
12360Sstevel@tonic-gate  * this function.
12370Sstevel@tonic-gate  */
12380Sstevel@tonic-gate int
12390Sstevel@tonic-gate i_ndi_config_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag)
12400Sstevel@tonic-gate {
12410Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flag))
12420Sstevel@tonic-gate 	int rv = DDI_SUCCESS;
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 	while ((i_ddi_node_state(dip) < state) && (rv == DDI_SUCCESS)) {
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 		/* don't allow any more changes to the device tree */
12490Sstevel@tonic-gate 		if (devinfo_freeze) {
12500Sstevel@tonic-gate 			rv = DDI_FAILURE;
12510Sstevel@tonic-gate 			break;
12520Sstevel@tonic-gate 		}
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 		switch (i_ddi_node_state(dip)) {
12550Sstevel@tonic-gate 		case DS_PROTO:
12560Sstevel@tonic-gate 			/*
12570Sstevel@tonic-gate 			 * only caller can reference this node, no external
12580Sstevel@tonic-gate 			 * locking needed.
12590Sstevel@tonic-gate 			 */
12600Sstevel@tonic-gate 			link_node(dip);
12610Sstevel@tonic-gate 			i_ddi_set_node_state(dip, DS_LINKED);
12620Sstevel@tonic-gate 			break;
12630Sstevel@tonic-gate 		case DS_LINKED:
12640Sstevel@tonic-gate 			/*
12650Sstevel@tonic-gate 			 * Three code path may attempt to bind a node:
12660Sstevel@tonic-gate 			 * - boot code
12670Sstevel@tonic-gate 			 * - add_drv
12680Sstevel@tonic-gate 			 * - hotplug thread
12690Sstevel@tonic-gate 			 * Boot code is single threaded, add_drv synchronize
12700Sstevel@tonic-gate 			 * on a userland lock, and hotplug synchronize on
12710Sstevel@tonic-gate 			 * hotplug_lk. There could be a race between add_drv
12720Sstevel@tonic-gate 			 * and hotplug thread. We'll live with this until the
12730Sstevel@tonic-gate 			 * conversion to top-down loading.
12740Sstevel@tonic-gate 			 */
12750Sstevel@tonic-gate 			if ((rv = bind_node(dip)) == DDI_SUCCESS)
12760Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_BOUND);
12770Sstevel@tonic-gate 			break;
12780Sstevel@tonic-gate 		case DS_BOUND:
12790Sstevel@tonic-gate 			/*
12800Sstevel@tonic-gate 			 * The following transitions synchronizes on the
12810Sstevel@tonic-gate 			 * per-driver busy changing flag, since we already
12820Sstevel@tonic-gate 			 * have a driver.
12830Sstevel@tonic-gate 			 */
12840Sstevel@tonic-gate 			if ((rv = init_node(dip)) == DDI_SUCCESS)
12850Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_INITIALIZED);
12860Sstevel@tonic-gate 			break;
12870Sstevel@tonic-gate 		case DS_INITIALIZED:
12880Sstevel@tonic-gate 			if ((rv = probe_node(dip)) == DDI_SUCCESS)
12890Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_PROBED);
12900Sstevel@tonic-gate 			break;
12910Sstevel@tonic-gate 		case DS_PROBED:
12920Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, 1);
12930Sstevel@tonic-gate 			if ((rv = attach_node(dip)) == DDI_SUCCESS)
12940Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_ATTACHED);
12950Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, -1);
12960Sstevel@tonic-gate 			break;
12970Sstevel@tonic-gate 		case DS_ATTACHED:
12980Sstevel@tonic-gate 			if ((rv = postattach_node(dip)) == DDI_SUCCESS)
12990Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_READY);
13000Sstevel@tonic-gate 			break;
13010Sstevel@tonic-gate 		case DS_READY:
13020Sstevel@tonic-gate 			break;
13030Sstevel@tonic-gate 		default:
13040Sstevel@tonic-gate 			/* should never reach here */
13050Sstevel@tonic-gate 			ASSERT("unknown devinfo state");
13060Sstevel@tonic-gate 		}
13070Sstevel@tonic-gate 	}
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate 	if (ddidebug & DDI_AUDIT)
13100Sstevel@tonic-gate 		da_log_enter(dip);
13110Sstevel@tonic-gate 	return (rv);
13120Sstevel@tonic-gate }
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate /*
13150Sstevel@tonic-gate  * i_ndi_unconfig_node: downgrade dev_info node into a specified state.
13160Sstevel@tonic-gate  */
13170Sstevel@tonic-gate int
13180Sstevel@tonic-gate i_ndi_unconfig_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag)
13190Sstevel@tonic-gate {
13200Sstevel@tonic-gate 	int rv = DDI_SUCCESS;
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
13230Sstevel@tonic-gate 
13240Sstevel@tonic-gate 	while ((i_ddi_node_state(dip) > state) && (rv == DDI_SUCCESS)) {
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 		/* don't allow any more changes to the device tree */
13270Sstevel@tonic-gate 		if (devinfo_freeze) {
13280Sstevel@tonic-gate 			rv = DDI_FAILURE;
13290Sstevel@tonic-gate 			break;
13300Sstevel@tonic-gate 		}
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate 		switch (i_ddi_node_state(dip)) {
13330Sstevel@tonic-gate 		case DS_PROTO:
13340Sstevel@tonic-gate 			break;
13350Sstevel@tonic-gate 		case DS_LINKED:
13360Sstevel@tonic-gate 			/*
13370Sstevel@tonic-gate 			 * Persistent nodes are only removed by hotplug code
13380Sstevel@tonic-gate 			 * .conf nodes synchronizes on per-driver list.
13390Sstevel@tonic-gate 			 */
13400Sstevel@tonic-gate 			if ((rv = unlink_node(dip)) == DDI_SUCCESS)
13410Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_PROTO);
13420Sstevel@tonic-gate 			break;
13430Sstevel@tonic-gate 		case DS_BOUND:
13440Sstevel@tonic-gate 			/*
13450Sstevel@tonic-gate 			 * The following transitions synchronizes on the
13460Sstevel@tonic-gate 			 * per-driver busy changing flag, since we already
13470Sstevel@tonic-gate 			 * have a driver.
13480Sstevel@tonic-gate 			 */
13490Sstevel@tonic-gate 			if ((rv = unbind_node(dip)) == DDI_SUCCESS)
13500Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_LINKED);
13510Sstevel@tonic-gate 			break;
13520Sstevel@tonic-gate 		case DS_INITIALIZED:
13530Sstevel@tonic-gate 			if ((rv = uninit_node(dip)) == DDI_SUCCESS)
13540Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_BOUND);
13550Sstevel@tonic-gate 			break;
13560Sstevel@tonic-gate 		case DS_PROBED:
13570Sstevel@tonic-gate 			if ((rv = unprobe_node(dip)) == DDI_SUCCESS)
13580Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_INITIALIZED);
13590Sstevel@tonic-gate 			break;
13600Sstevel@tonic-gate 		case DS_ATTACHED:
13610Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, 1);
1362495Scth 
1363495Scth 			mutex_enter(&(DEVI(dip)->devi_lock));
13640Sstevel@tonic-gate 			DEVI_SET_DETACHING(dip);
1365495Scth 			mutex_exit(&(DEVI(dip)->devi_lock));
1366495Scth 
13670Sstevel@tonic-gate 			membar_enter();	/* ensure visibility for hold_devi */
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate 			if ((rv = detach_node(dip, flag)) == DDI_SUCCESS)
13700Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_PROBED);
1371495Scth 
1372495Scth 			mutex_enter(&(DEVI(dip)->devi_lock));
13730Sstevel@tonic-gate 			DEVI_CLR_DETACHING(dip);
1374495Scth 			mutex_exit(&(DEVI(dip)->devi_lock));
1375495Scth 
13760Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, -1);
13770Sstevel@tonic-gate 			break;
13780Sstevel@tonic-gate 		case DS_READY:
13790Sstevel@tonic-gate 			if ((rv = predetach_node(dip, flag)) == DDI_SUCCESS)
13800Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_ATTACHED);
13810Sstevel@tonic-gate 			break;
13820Sstevel@tonic-gate 		default:
13830Sstevel@tonic-gate 			ASSERT("unknown devinfo state");
13840Sstevel@tonic-gate 		}
13850Sstevel@tonic-gate 	}
13860Sstevel@tonic-gate 	da_log_enter(dip);
13870Sstevel@tonic-gate 	return (rv);
13880Sstevel@tonic-gate }
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate /*
13910Sstevel@tonic-gate  * ddi_initchild: transform node to DS_INITIALIZED state
13920Sstevel@tonic-gate  */
13930Sstevel@tonic-gate int
13940Sstevel@tonic-gate ddi_initchild(dev_info_t *parent, dev_info_t *proto)
13950Sstevel@tonic-gate {
13960Sstevel@tonic-gate 	int ret, circ;
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
13990Sstevel@tonic-gate 	ret = i_ndi_config_node(proto, DS_INITIALIZED, 0);
14000Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
14010Sstevel@tonic-gate 
14020Sstevel@tonic-gate 	return (ret);
14030Sstevel@tonic-gate }
14040Sstevel@tonic-gate 
14050Sstevel@tonic-gate /*
14060Sstevel@tonic-gate  * ddi_uninitchild: transform node down to DS_BOUND state
14070Sstevel@tonic-gate  */
14080Sstevel@tonic-gate int
14090Sstevel@tonic-gate ddi_uninitchild(dev_info_t *dip)
14100Sstevel@tonic-gate {
14110Sstevel@tonic-gate 	int ret, circ;
14120Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
14130Sstevel@tonic-gate 	ASSERT(parent);
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
14160Sstevel@tonic-gate 	ret = i_ndi_unconfig_node(dip, DS_BOUND, 0);
14170Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 	return (ret);
14200Sstevel@tonic-gate }
14210Sstevel@tonic-gate 
14220Sstevel@tonic-gate /*
14230Sstevel@tonic-gate  * i_ddi_attachchild: transform node to DS_READY state
14240Sstevel@tonic-gate  */
14250Sstevel@tonic-gate static int
14260Sstevel@tonic-gate i_ddi_attachchild(dev_info_t *dip)
14270Sstevel@tonic-gate {
14280Sstevel@tonic-gate 	int ret, circ;
14290Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
14300Sstevel@tonic-gate 	ASSERT(parent);
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate 	if ((i_ddi_node_state(dip) < DS_BOUND) || DEVI_IS_DEVICE_OFFLINE(dip))
14330Sstevel@tonic-gate 		return (DDI_FAILURE);
14340Sstevel@tonic-gate 
14350Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
14360Sstevel@tonic-gate 	ret = i_ndi_config_node(dip, DS_READY, 0);
14370Sstevel@tonic-gate 	if (ret == NDI_SUCCESS) {
14380Sstevel@tonic-gate 		ret = DDI_SUCCESS;
14390Sstevel@tonic-gate 	} else {
14400Sstevel@tonic-gate 		/*
14410Sstevel@tonic-gate 		 * Take it down to DS_INITIALIZED so pm_pre_probe is run
14420Sstevel@tonic-gate 		 * on the next attach
14430Sstevel@tonic-gate 		 */
14440Sstevel@tonic-gate 		(void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0);
14450Sstevel@tonic-gate 		ret = DDI_FAILURE;
14460Sstevel@tonic-gate 	}
14470Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
14480Sstevel@tonic-gate 
14490Sstevel@tonic-gate 	return (ret);
14500Sstevel@tonic-gate }
14510Sstevel@tonic-gate 
14520Sstevel@tonic-gate /*
14530Sstevel@tonic-gate  * i_ddi_detachchild: transform node down to DS_PROBED state
14540Sstevel@tonic-gate  *	If it fails, put it back to DS_READY state.
14550Sstevel@tonic-gate  * NOTE: A node that fails detach may be at DS_ATTACHED instead
14560Sstevel@tonic-gate  * of DS_READY for a small amount of time.
14570Sstevel@tonic-gate  */
14580Sstevel@tonic-gate static int
14590Sstevel@tonic-gate i_ddi_detachchild(dev_info_t *dip, uint_t flags)
14600Sstevel@tonic-gate {
14610Sstevel@tonic-gate 	int ret, circ;
14620Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
14630Sstevel@tonic-gate 	ASSERT(parent);
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
14660Sstevel@tonic-gate 	ret = i_ndi_unconfig_node(dip, DS_PROBED, flags);
14670Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
14680Sstevel@tonic-gate 		(void) i_ndi_config_node(dip, DS_READY, 0);
14690Sstevel@tonic-gate 	else
14700Sstevel@tonic-gate 		/* allow pm_pre_probe to reestablish pm state */
14710Sstevel@tonic-gate 		(void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0);
14720Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate 	return (ret);
14750Sstevel@tonic-gate }
14760Sstevel@tonic-gate 
14770Sstevel@tonic-gate /*
14780Sstevel@tonic-gate  * Add a child and bind to driver
14790Sstevel@tonic-gate  */
14800Sstevel@tonic-gate dev_info_t *
14810Sstevel@tonic-gate ddi_add_child(dev_info_t *pdip, char *name, uint_t nodeid, uint_t unit)
14820Sstevel@tonic-gate {
14830Sstevel@tonic-gate 	int circ;
14840Sstevel@tonic-gate 	dev_info_t *dip;
14850Sstevel@tonic-gate 
14860Sstevel@tonic-gate 	/* allocate a new node */
14870Sstevel@tonic-gate 	dip = i_ddi_alloc_node(pdip, name, nodeid, (int)unit, NULL, KM_SLEEP);
14880Sstevel@tonic-gate 
14890Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
14900Sstevel@tonic-gate 	(void) i_ndi_config_node(dip, DS_BOUND, 0);
14910Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
14920Sstevel@tonic-gate 	return (dip);
14930Sstevel@tonic-gate }
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate /*
14960Sstevel@tonic-gate  * ddi_remove_child: remove the dip. The parent must be attached and held
14970Sstevel@tonic-gate  */
14980Sstevel@tonic-gate int
14990Sstevel@tonic-gate ddi_remove_child(dev_info_t *dip, int dummy)
15000Sstevel@tonic-gate {
15010Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dummy))
15020Sstevel@tonic-gate 	int circ, ret;
15030Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
15040Sstevel@tonic-gate 	ASSERT(parent);
15050Sstevel@tonic-gate 
15060Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate 	/*
15090Sstevel@tonic-gate 	 * If we still have children, for example SID nodes marked
15100Sstevel@tonic-gate 	 * as persistent but not attached, attempt to remove them.
15110Sstevel@tonic-gate 	 */
15120Sstevel@tonic-gate 	if (DEVI(dip)->devi_child) {
15130Sstevel@tonic-gate 		ret = ndi_devi_unconfig(dip, NDI_DEVI_REMOVE);
15140Sstevel@tonic-gate 		if (ret != NDI_SUCCESS) {
15150Sstevel@tonic-gate 			ndi_devi_exit(parent, circ);
15160Sstevel@tonic-gate 			return (DDI_FAILURE);
15170Sstevel@tonic-gate 		}
15180Sstevel@tonic-gate 		ASSERT(DEVI(dip)->devi_child == NULL);
15190Sstevel@tonic-gate 	}
15200Sstevel@tonic-gate 
15210Sstevel@tonic-gate 	ret = i_ndi_unconfig_node(dip, DS_PROTO, 0);
15220Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
15230Sstevel@tonic-gate 
15240Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
15250Sstevel@tonic-gate 		return (ret);
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_PROTO);
15280Sstevel@tonic-gate 	i_ddi_free_node(dip);
15290Sstevel@tonic-gate 	return (DDI_SUCCESS);
15300Sstevel@tonic-gate }
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate /*
15330Sstevel@tonic-gate  * NDI wrappers for ref counting, node allocation, and transitions
15340Sstevel@tonic-gate  */
15350Sstevel@tonic-gate 
15360Sstevel@tonic-gate /*
15370Sstevel@tonic-gate  * Hold/release the devinfo node itself.
15380Sstevel@tonic-gate  * Caller is assumed to prevent the devi from detaching during this call
15390Sstevel@tonic-gate  */
15400Sstevel@tonic-gate void
15410Sstevel@tonic-gate ndi_hold_devi(dev_info_t *dip)
15420Sstevel@tonic-gate {
15430Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
15440Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_ref >= 0);
15450Sstevel@tonic-gate 	DEVI(dip)->devi_ref++;
15460Sstevel@tonic-gate 	membar_enter();			/* make sure stores are flushed */
15470Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
15480Sstevel@tonic-gate }
15490Sstevel@tonic-gate 
15500Sstevel@tonic-gate void
15510Sstevel@tonic-gate ndi_rele_devi(dev_info_t *dip)
15520Sstevel@tonic-gate {
15530Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_ref > 0);
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
15560Sstevel@tonic-gate 	DEVI(dip)->devi_ref--;
15570Sstevel@tonic-gate 	membar_enter();			/* make sure stores are flushed */
15580Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
15590Sstevel@tonic-gate }
15600Sstevel@tonic-gate 
15610Sstevel@tonic-gate int
15620Sstevel@tonic-gate e_ddi_devi_holdcnt(dev_info_t *dip)
15630Sstevel@tonic-gate {
15640Sstevel@tonic-gate 	return (DEVI(dip)->devi_ref);
15650Sstevel@tonic-gate }
15660Sstevel@tonic-gate 
15670Sstevel@tonic-gate /*
15680Sstevel@tonic-gate  * Hold/release the driver the devinfo node is bound to.
15690Sstevel@tonic-gate  */
15700Sstevel@tonic-gate struct dev_ops *
15710Sstevel@tonic-gate ndi_hold_driver(dev_info_t *dip)
15720Sstevel@tonic-gate {
15730Sstevel@tonic-gate 	if (i_ddi_node_state(dip) < DS_BOUND)
15740Sstevel@tonic-gate 		return (NULL);
15750Sstevel@tonic-gate 
15760Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_major != -1);
15770Sstevel@tonic-gate 	return (mod_hold_dev_by_major(DEVI(dip)->devi_major));
15780Sstevel@tonic-gate }
15790Sstevel@tonic-gate 
15800Sstevel@tonic-gate void
15810Sstevel@tonic-gate ndi_rele_driver(dev_info_t *dip)
15820Sstevel@tonic-gate {
15830Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) >= DS_BOUND);
15840Sstevel@tonic-gate 	mod_rele_dev_by_major(DEVI(dip)->devi_major);
15850Sstevel@tonic-gate }
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate /*
15880Sstevel@tonic-gate  * Single thread entry into devinfo node for modifying its children.
15890Sstevel@tonic-gate  * To verify in ASSERTS use DEVI_BUSY_OWNED macro.
15900Sstevel@tonic-gate  */
15910Sstevel@tonic-gate void
15920Sstevel@tonic-gate ndi_devi_enter(dev_info_t *dip, int *circular)
15930Sstevel@tonic-gate {
15940Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
15950Sstevel@tonic-gate 	ASSERT(dip != NULL);
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate 	mutex_enter(&devi->devi_lock);
15980Sstevel@tonic-gate 	if (devi->devi_busy_thread == curthread) {
15990Sstevel@tonic-gate 		devi->devi_circular++;
16000Sstevel@tonic-gate 	} else {
16010Sstevel@tonic-gate 		while (DEVI_BUSY_CHANGING(devi) && !panicstr)
16020Sstevel@tonic-gate 			cv_wait(&(devi->devi_cv), &(devi->devi_lock));
16030Sstevel@tonic-gate 		if (panicstr) {
16040Sstevel@tonic-gate 			mutex_exit(&devi->devi_lock);
16050Sstevel@tonic-gate 			return;
16060Sstevel@tonic-gate 		}
16070Sstevel@tonic-gate 		devi->devi_flags |= DEVI_BUSY;
16080Sstevel@tonic-gate 		devi->devi_busy_thread = curthread;
16090Sstevel@tonic-gate 	}
16100Sstevel@tonic-gate 	*circular = devi->devi_circular;
16110Sstevel@tonic-gate 	mutex_exit(&devi->devi_lock);
16120Sstevel@tonic-gate }
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate /*
16150Sstevel@tonic-gate  * Release ndi_devi_enter or successful ndi_devi_tryenter.
16160Sstevel@tonic-gate  */
16170Sstevel@tonic-gate void
16180Sstevel@tonic-gate ndi_devi_exit(dev_info_t *dip, int circular)
16190Sstevel@tonic-gate {
16200Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
16210Sstevel@tonic-gate 	ASSERT(dip != NULL);
16220Sstevel@tonic-gate 
16230Sstevel@tonic-gate 	if (panicstr)
16240Sstevel@tonic-gate 		return;
16250Sstevel@tonic-gate 
16260Sstevel@tonic-gate 	mutex_enter(&(devi->devi_lock));
16270Sstevel@tonic-gate 	if (circular != 0) {
16280Sstevel@tonic-gate 		devi->devi_circular--;
16290Sstevel@tonic-gate 	} else {
16300Sstevel@tonic-gate 		devi->devi_flags &= ~DEVI_BUSY;
16310Sstevel@tonic-gate 		ASSERT(devi->devi_busy_thread == curthread);
16320Sstevel@tonic-gate 		devi->devi_busy_thread = NULL;
16330Sstevel@tonic-gate 		cv_broadcast(&(devi->devi_cv));
16340Sstevel@tonic-gate 	}
16350Sstevel@tonic-gate 	mutex_exit(&(devi->devi_lock));
16360Sstevel@tonic-gate }
16370Sstevel@tonic-gate 
16380Sstevel@tonic-gate /*
16390Sstevel@tonic-gate  * Attempt to single thread entry into devinfo node for modifying its children.
16400Sstevel@tonic-gate  */
16410Sstevel@tonic-gate int
16420Sstevel@tonic-gate ndi_devi_tryenter(dev_info_t *dip, int *circular)
16430Sstevel@tonic-gate {
16440Sstevel@tonic-gate 	int rval = 1;		   /* assume we enter */
16450Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
16460Sstevel@tonic-gate 	ASSERT(dip != NULL);
16470Sstevel@tonic-gate 
16480Sstevel@tonic-gate 	mutex_enter(&devi->devi_lock);
16490Sstevel@tonic-gate 	if (devi->devi_busy_thread == (void *)curthread) {
16500Sstevel@tonic-gate 		devi->devi_circular++;
16510Sstevel@tonic-gate 	} else {
16520Sstevel@tonic-gate 		if (!DEVI_BUSY_CHANGING(devi)) {
16530Sstevel@tonic-gate 			devi->devi_flags |= DEVI_BUSY;
16540Sstevel@tonic-gate 			devi->devi_busy_thread = (void *)curthread;
16550Sstevel@tonic-gate 		} else {
16560Sstevel@tonic-gate 			rval = 0;	/* devi is busy */
16570Sstevel@tonic-gate 		}
16580Sstevel@tonic-gate 	}
16590Sstevel@tonic-gate 	*circular = devi->devi_circular;
16600Sstevel@tonic-gate 	mutex_exit(&devi->devi_lock);
16610Sstevel@tonic-gate 	return (rval);
16620Sstevel@tonic-gate }
16630Sstevel@tonic-gate 
16640Sstevel@tonic-gate /*
16650Sstevel@tonic-gate  * Allocate and initialize a new dev_info structure.
16660Sstevel@tonic-gate  *
16670Sstevel@tonic-gate  * This routine may be called at interrupt time by a nexus in
16680Sstevel@tonic-gate  * response to a hotplug event, therefore memory allocations are
16690Sstevel@tonic-gate  * not allowed to sleep.
16700Sstevel@tonic-gate  */
16710Sstevel@tonic-gate int
16720Sstevel@tonic-gate ndi_devi_alloc(dev_info_t *parent, char *node_name, dnode_t nodeid,
16730Sstevel@tonic-gate     dev_info_t **ret_dip)
16740Sstevel@tonic-gate {
16750Sstevel@tonic-gate 	ASSERT(node_name != NULL);
16760Sstevel@tonic-gate 	ASSERT(ret_dip != NULL);
16770Sstevel@tonic-gate 
16780Sstevel@tonic-gate 	*ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL,
16790Sstevel@tonic-gate 	    KM_NOSLEEP);
16800Sstevel@tonic-gate 	if (*ret_dip == NULL) {
16810Sstevel@tonic-gate 		return (NDI_NOMEM);
16820Sstevel@tonic-gate 	}
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate 	return (NDI_SUCCESS);
16850Sstevel@tonic-gate }
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate /*
16880Sstevel@tonic-gate  * Allocate and initialize a new dev_info structure
16890Sstevel@tonic-gate  * This routine may sleep and should not be called at interrupt time
16900Sstevel@tonic-gate  */
16910Sstevel@tonic-gate void
16920Sstevel@tonic-gate ndi_devi_alloc_sleep(dev_info_t *parent, char *node_name, dnode_t nodeid,
16930Sstevel@tonic-gate     dev_info_t **ret_dip)
16940Sstevel@tonic-gate {
16950Sstevel@tonic-gate 	ASSERT(node_name != NULL);
16960Sstevel@tonic-gate 	ASSERT(ret_dip != NULL);
16970Sstevel@tonic-gate 
16980Sstevel@tonic-gate 	*ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL,
16990Sstevel@tonic-gate 	    KM_SLEEP);
17000Sstevel@tonic-gate 	ASSERT(*ret_dip);
17010Sstevel@tonic-gate }
17020Sstevel@tonic-gate 
17030Sstevel@tonic-gate /*
17040Sstevel@tonic-gate  * Remove an initialized (but not yet attached) dev_info
17050Sstevel@tonic-gate  * node from it's parent.
17060Sstevel@tonic-gate  */
17070Sstevel@tonic-gate int
17080Sstevel@tonic-gate ndi_devi_free(dev_info_t *dip)
17090Sstevel@tonic-gate {
17100Sstevel@tonic-gate 	ASSERT(dip != NULL);
17110Sstevel@tonic-gate 
17120Sstevel@tonic-gate 	if (i_ddi_node_state(dip) >= DS_INITIALIZED)
17130Sstevel@tonic-gate 		return (DDI_FAILURE);
17140Sstevel@tonic-gate 
17150Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_free: %s%d (%p)\n",
17160Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate 	(void) ddi_remove_child(dip, 0);
17190Sstevel@tonic-gate 
17200Sstevel@tonic-gate 	return (NDI_SUCCESS);
17210Sstevel@tonic-gate }
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate /*
17240Sstevel@tonic-gate  * ndi_devi_bind_driver() binds a driver to a given device. If it fails
17250Sstevel@tonic-gate  * to bind the driver, it returns an appropriate error back. Some drivers
17260Sstevel@tonic-gate  * may want to know if the actually failed to bind.
17270Sstevel@tonic-gate  */
17280Sstevel@tonic-gate int
17290Sstevel@tonic-gate ndi_devi_bind_driver(dev_info_t *dip, uint_t flags)
17300Sstevel@tonic-gate {
17310Sstevel@tonic-gate 	int ret = NDI_FAILURE;
17320Sstevel@tonic-gate 	int circ;
17330Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
17340Sstevel@tonic-gate 	ASSERT(pdip);
17350Sstevel@tonic-gate 
17360Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
17370Sstevel@tonic-gate 	    "ndi_devi_bind_driver: %s%d (%p) flags: %x\n",
17380Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
17390Sstevel@tonic-gate 
17400Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
17410Sstevel@tonic-gate 	if (i_ndi_config_node(dip, DS_BOUND, flags) == DDI_SUCCESS)
17420Sstevel@tonic-gate 		ret = NDI_SUCCESS;
17430Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
17440Sstevel@tonic-gate 
17450Sstevel@tonic-gate 	return (ret);
17460Sstevel@tonic-gate }
17470Sstevel@tonic-gate 
17480Sstevel@tonic-gate /*
17490Sstevel@tonic-gate  * ndi_devi_unbind_driver: unbind the dip
17500Sstevel@tonic-gate  */
17510Sstevel@tonic-gate static int
17520Sstevel@tonic-gate ndi_devi_unbind_driver(dev_info_t *dip)
17530Sstevel@tonic-gate {
17540Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
17550Sstevel@tonic-gate 
17560Sstevel@tonic-gate 	return (i_ndi_unconfig_node(dip, DS_LINKED, 0));
17570Sstevel@tonic-gate }
17580Sstevel@tonic-gate 
17590Sstevel@tonic-gate /*
17600Sstevel@tonic-gate  * Misc. help routines called by framework only
17610Sstevel@tonic-gate  */
17620Sstevel@tonic-gate 
17630Sstevel@tonic-gate /*
17640Sstevel@tonic-gate  * Get the state of node
17650Sstevel@tonic-gate  */
17660Sstevel@tonic-gate ddi_node_state_t
17670Sstevel@tonic-gate i_ddi_node_state(dev_info_t *dip)
17680Sstevel@tonic-gate {
17690Sstevel@tonic-gate 	return (DEVI(dip)->devi_node_state);
17700Sstevel@tonic-gate }
17710Sstevel@tonic-gate 
17720Sstevel@tonic-gate /*
17730Sstevel@tonic-gate  * Set the state of node
17740Sstevel@tonic-gate  */
17750Sstevel@tonic-gate void
17760Sstevel@tonic-gate i_ddi_set_node_state(dev_info_t *dip, ddi_node_state_t state)
17770Sstevel@tonic-gate {
17780Sstevel@tonic-gate 	DEVI(dip)->devi_node_state = state;
17790Sstevel@tonic-gate 	membar_enter();			/* make sure stores are flushed */
17800Sstevel@tonic-gate }
17810Sstevel@tonic-gate 
17820Sstevel@tonic-gate /*
17830Sstevel@tonic-gate  * Common function for finding a node in a sibling list given name and addr.
17840Sstevel@tonic-gate  *
17850Sstevel@tonic-gate  * By default, name is matched with devi_node_name. The following
17860Sstevel@tonic-gate  * alternative match strategies are supported:
17870Sstevel@tonic-gate  *
17880Sstevel@tonic-gate  *	FIND_NAME_BY_DRIVER: A match on driver name bound to node is conducted.
17890Sstevel@tonic-gate  *		This support is used for support of OBP generic names and
17900Sstevel@tonic-gate  *		for the conversion from driver names to generic names.  When
17910Sstevel@tonic-gate  *		more consistency in the generic name environment is achieved
17920Sstevel@tonic-gate  *		(and not needed for upgrade) this support can be removed.
17930Sstevel@tonic-gate  *
17940Sstevel@tonic-gate  * If a child is not named (dev_addr == NULL), there are three
17950Sstevel@tonic-gate  * possible actions:
17960Sstevel@tonic-gate  *
17970Sstevel@tonic-gate  *	(1) skip it
17980Sstevel@tonic-gate  *	(2) FIND_ADDR_BY_INIT: bring child to DS_INITIALIZED state
17990Sstevel@tonic-gate  *	(3) FIND_ADDR_BY_CALLBACK: use a caller-supplied callback function
18000Sstevel@tonic-gate  */
18010Sstevel@tonic-gate #define	FIND_NAME_BY_DRIVER	0x01
18020Sstevel@tonic-gate #define	FIND_ADDR_BY_INIT	0x10
18030Sstevel@tonic-gate #define	FIND_ADDR_BY_CALLBACK	0x20
18040Sstevel@tonic-gate 
18050Sstevel@tonic-gate static dev_info_t *
18060Sstevel@tonic-gate find_sibling(dev_info_t *head, char *cname, char *caddr, uint_t flag,
18070Sstevel@tonic-gate     int (*callback)(dev_info_t *, char *, int))
18080Sstevel@tonic-gate {
18090Sstevel@tonic-gate 	dev_info_t	*dip;
18100Sstevel@tonic-gate 	char		*addr, *buf;
18110Sstevel@tonic-gate 	major_t		major;
18120Sstevel@tonic-gate 
18130Sstevel@tonic-gate 	/* only one way to name a node */
18140Sstevel@tonic-gate 	ASSERT(((flag & FIND_ADDR_BY_INIT) == 0) ||
18150Sstevel@tonic-gate 	    ((flag & FIND_ADDR_BY_CALLBACK) == 0));
18160Sstevel@tonic-gate 
18170Sstevel@tonic-gate 	if (flag & FIND_NAME_BY_DRIVER) {
18180Sstevel@tonic-gate 		major = ddi_name_to_major(cname);
18190Sstevel@tonic-gate 		if (major == (major_t)-1)
18200Sstevel@tonic-gate 			return (NULL);
18210Sstevel@tonic-gate 	}
18220Sstevel@tonic-gate 
18230Sstevel@tonic-gate 	/* preallocate buffer of naming node by callback */
18240Sstevel@tonic-gate 	if (flag & FIND_ADDR_BY_CALLBACK)
18250Sstevel@tonic-gate 		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
18260Sstevel@tonic-gate 
18270Sstevel@tonic-gate 	/*
18280Sstevel@tonic-gate 	 * Walk the child list to find a match
18290Sstevel@tonic-gate 	 */
18300Sstevel@tonic-gate 
18310Sstevel@tonic-gate 	for (dip = head; dip; dip = ddi_get_next_sibling(dip)) {
18320Sstevel@tonic-gate 		if (flag & FIND_NAME_BY_DRIVER) {
18330Sstevel@tonic-gate 			/* match driver major */
18340Sstevel@tonic-gate 			if (DEVI(dip)->devi_major != major)
18350Sstevel@tonic-gate 				continue;
18360Sstevel@tonic-gate 		} else {
18370Sstevel@tonic-gate 			/* match node name */
18380Sstevel@tonic-gate 			if (strcmp(cname, DEVI(dip)->devi_node_name) != 0)
18390Sstevel@tonic-gate 				continue;
18400Sstevel@tonic-gate 		}
18410Sstevel@tonic-gate 
18420Sstevel@tonic-gate 		if ((addr = DEVI(dip)->devi_addr) == NULL) {
18430Sstevel@tonic-gate 			/* name the child based on the flag */
18440Sstevel@tonic-gate 			if (flag & FIND_ADDR_BY_INIT) {
18450Sstevel@tonic-gate 				if (ddi_initchild(ddi_get_parent(dip), dip)
18460Sstevel@tonic-gate 				    != DDI_SUCCESS)
18470Sstevel@tonic-gate 					continue;
18480Sstevel@tonic-gate 				addr = DEVI(dip)->devi_addr;
18490Sstevel@tonic-gate 			} else if (flag & FIND_ADDR_BY_CALLBACK) {
18500Sstevel@tonic-gate 				if ((callback == NULL) || (callback(
18510Sstevel@tonic-gate 				    dip, buf, MAXNAMELEN) != DDI_SUCCESS))
18520Sstevel@tonic-gate 					continue;
18530Sstevel@tonic-gate 				addr = buf;
18540Sstevel@tonic-gate 			} else {
18550Sstevel@tonic-gate 				continue;	/* skip */
18560Sstevel@tonic-gate 			}
18570Sstevel@tonic-gate 		}
18580Sstevel@tonic-gate 
18590Sstevel@tonic-gate 		/* match addr */
18600Sstevel@tonic-gate 		ASSERT(addr != NULL);
18610Sstevel@tonic-gate 		if (strcmp(caddr, addr) == 0)
18620Sstevel@tonic-gate 			break;	/* node found */
18630Sstevel@tonic-gate 
18640Sstevel@tonic-gate 	}
18650Sstevel@tonic-gate 	if (flag & FIND_ADDR_BY_CALLBACK)
18660Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
18670Sstevel@tonic-gate 	return (dip);
18680Sstevel@tonic-gate }
18690Sstevel@tonic-gate 
18700Sstevel@tonic-gate /*
18710Sstevel@tonic-gate  * Find child of pdip with name: cname@caddr
18720Sstevel@tonic-gate  * Called by init_node() to look for duplicate nodes
18730Sstevel@tonic-gate  */
18740Sstevel@tonic-gate static dev_info_t *
18750Sstevel@tonic-gate find_duplicate_child(dev_info_t *pdip, dev_info_t *dip)
18760Sstevel@tonic-gate {
18770Sstevel@tonic-gate 	dev_info_t *dup;
18780Sstevel@tonic-gate 	char *cname = DEVI(dip)->devi_node_name;
18790Sstevel@tonic-gate 	char *caddr = DEVI(dip)->devi_addr;
18800Sstevel@tonic-gate 
18810Sstevel@tonic-gate 	/* search nodes before dip */
18820Sstevel@tonic-gate 	dup = find_sibling(ddi_get_child(pdip), cname, caddr, 0, NULL);
18830Sstevel@tonic-gate 	if (dup != dip)
18840Sstevel@tonic-gate 		return (dup);
18850Sstevel@tonic-gate 
18860Sstevel@tonic-gate 	/*
18870Sstevel@tonic-gate 	 * search nodes after dip; normally this is not needed,
18880Sstevel@tonic-gate 	 */
18890Sstevel@tonic-gate 	return (find_sibling(ddi_get_next_sibling(dip), cname, caddr,
18900Sstevel@tonic-gate 	    0, NULL));
18910Sstevel@tonic-gate }
18920Sstevel@tonic-gate 
18930Sstevel@tonic-gate /*
18940Sstevel@tonic-gate  * Find a child of a given name and address, using a callback to name
18950Sstevel@tonic-gate  * unnamed children. cname is the binding name.
18960Sstevel@tonic-gate  */
18970Sstevel@tonic-gate static dev_info_t *
18980Sstevel@tonic-gate find_child_by_callback(dev_info_t *pdip, char *cname, char *caddr,
18990Sstevel@tonic-gate     int (*name_node)(dev_info_t *, char *, int))
19000Sstevel@tonic-gate {
19010Sstevel@tonic-gate 	return (find_sibling(ddi_get_child(pdip), cname, caddr,
19020Sstevel@tonic-gate 	    FIND_NAME_BY_DRIVER|FIND_ADDR_BY_CALLBACK, name_node));
19030Sstevel@tonic-gate }
19040Sstevel@tonic-gate 
19050Sstevel@tonic-gate /*
19060Sstevel@tonic-gate  * Find a child of a given name and address, invoking initchild to name
19070Sstevel@tonic-gate  * unnamed children. cname is the node name.
19080Sstevel@tonic-gate  */
19090Sstevel@tonic-gate static dev_info_t *
19100Sstevel@tonic-gate find_child_by_name(dev_info_t *pdip, char *cname, char *caddr)
19110Sstevel@tonic-gate {
19120Sstevel@tonic-gate 	dev_info_t	*dip;
19130Sstevel@tonic-gate 
19140Sstevel@tonic-gate 	/* attempt search without changing state of preceeding siblings */
19150Sstevel@tonic-gate 	dip = find_sibling(ddi_get_child(pdip), cname, caddr, 0, NULL);
19160Sstevel@tonic-gate 	if (dip)
19170Sstevel@tonic-gate 		return (dip);
19180Sstevel@tonic-gate 
19190Sstevel@tonic-gate 	return (find_sibling(ddi_get_child(pdip), cname, caddr,
19200Sstevel@tonic-gate 	    FIND_ADDR_BY_INIT, NULL));
19210Sstevel@tonic-gate }
19220Sstevel@tonic-gate 
19230Sstevel@tonic-gate /*
19240Sstevel@tonic-gate  * Find a child of a given name and address, invoking initchild to name
19250Sstevel@tonic-gate  * unnamed children. cname is the node name.
19260Sstevel@tonic-gate  */
19270Sstevel@tonic-gate static dev_info_t *
19280Sstevel@tonic-gate find_child_by_driver(dev_info_t *pdip, char *cname, char *caddr)
19290Sstevel@tonic-gate {
19300Sstevel@tonic-gate 	dev_info_t	*dip;
19310Sstevel@tonic-gate 
19320Sstevel@tonic-gate 	/* attempt search without changing state of preceeding siblings */
19330Sstevel@tonic-gate 	dip = find_sibling(ddi_get_child(pdip), cname, caddr,
19340Sstevel@tonic-gate 	    FIND_NAME_BY_DRIVER, NULL);
19350Sstevel@tonic-gate 	if (dip)
19360Sstevel@tonic-gate 		return (dip);
19370Sstevel@tonic-gate 
19380Sstevel@tonic-gate 	return (find_sibling(ddi_get_child(pdip), cname, caddr,
19390Sstevel@tonic-gate 	    FIND_NAME_BY_DRIVER|FIND_ADDR_BY_INIT, NULL));
19400Sstevel@tonic-gate }
19410Sstevel@tonic-gate 
19420Sstevel@tonic-gate /*
19430Sstevel@tonic-gate  * Deleting a property list. Take care, since some property structures
19440Sstevel@tonic-gate  * may not be fully built.
19450Sstevel@tonic-gate  */
19460Sstevel@tonic-gate void
19470Sstevel@tonic-gate i_ddi_prop_list_delete(ddi_prop_t *prop)
19480Sstevel@tonic-gate {
19490Sstevel@tonic-gate 	while (prop) {
19500Sstevel@tonic-gate 		ddi_prop_t *next = prop->prop_next;
19510Sstevel@tonic-gate 		if (prop->prop_name)
19520Sstevel@tonic-gate 			kmem_free(prop->prop_name, strlen(prop->prop_name) + 1);
19530Sstevel@tonic-gate 		if ((prop->prop_len != 0) && prop->prop_val)
19540Sstevel@tonic-gate 			kmem_free(prop->prop_val, prop->prop_len);
19550Sstevel@tonic-gate 		kmem_free(prop, sizeof (struct ddi_prop));
19560Sstevel@tonic-gate 		prop = next;
19570Sstevel@tonic-gate 	}
19580Sstevel@tonic-gate }
19590Sstevel@tonic-gate 
19600Sstevel@tonic-gate /*
19610Sstevel@tonic-gate  * Duplicate property list
19620Sstevel@tonic-gate  */
19630Sstevel@tonic-gate ddi_prop_t *
19640Sstevel@tonic-gate i_ddi_prop_list_dup(ddi_prop_t *prop, uint_t flag)
19650Sstevel@tonic-gate {
19660Sstevel@tonic-gate 	ddi_prop_t *result, *prev, *copy;
19670Sstevel@tonic-gate 
19680Sstevel@tonic-gate 	if (prop == NULL)
19690Sstevel@tonic-gate 		return (NULL);
19700Sstevel@tonic-gate 
19710Sstevel@tonic-gate 	result = prev = NULL;
19720Sstevel@tonic-gate 	for (; prop != NULL; prop = prop->prop_next) {
19730Sstevel@tonic-gate 		ASSERT(prop->prop_name != NULL);
19740Sstevel@tonic-gate 		copy = kmem_zalloc(sizeof (struct ddi_prop), flag);
19750Sstevel@tonic-gate 		if (copy == NULL)
19760Sstevel@tonic-gate 			goto fail;
19770Sstevel@tonic-gate 
19780Sstevel@tonic-gate 		copy->prop_dev = prop->prop_dev;
19790Sstevel@tonic-gate 		copy->prop_flags = prop->prop_flags;
19800Sstevel@tonic-gate 		copy->prop_name = i_ddi_strdup(prop->prop_name, flag);
19810Sstevel@tonic-gate 		if (copy->prop_name == NULL)
19820Sstevel@tonic-gate 			goto fail;
19830Sstevel@tonic-gate 
19840Sstevel@tonic-gate 		if ((copy->prop_len = prop->prop_len) != 0) {
19850Sstevel@tonic-gate 			copy->prop_val = kmem_zalloc(prop->prop_len, flag);
19860Sstevel@tonic-gate 			if (copy->prop_val == NULL)
19870Sstevel@tonic-gate 				goto fail;
19880Sstevel@tonic-gate 
19890Sstevel@tonic-gate 			bcopy(prop->prop_val, copy->prop_val, prop->prop_len);
19900Sstevel@tonic-gate 		}
19910Sstevel@tonic-gate 
19920Sstevel@tonic-gate 		if (prev == NULL)
19930Sstevel@tonic-gate 			result = prev = copy;
19940Sstevel@tonic-gate 		else
19950Sstevel@tonic-gate 			prev->prop_next = copy;
19960Sstevel@tonic-gate 		prev = copy;
19970Sstevel@tonic-gate 	}
19980Sstevel@tonic-gate 	return (result);
19990Sstevel@tonic-gate 
20000Sstevel@tonic-gate fail:
20010Sstevel@tonic-gate 	i_ddi_prop_list_delete(result);
20020Sstevel@tonic-gate 	return (NULL);
20030Sstevel@tonic-gate }
20040Sstevel@tonic-gate 
20050Sstevel@tonic-gate /*
20060Sstevel@tonic-gate  * Create a reference property list, currently used only for
20070Sstevel@tonic-gate  * driver global properties. Created with ref count of 1.
20080Sstevel@tonic-gate  */
20090Sstevel@tonic-gate ddi_prop_list_t *
20100Sstevel@tonic-gate i_ddi_prop_list_create(ddi_prop_t *props)
20110Sstevel@tonic-gate {
20120Sstevel@tonic-gate 	ddi_prop_list_t *list = kmem_alloc(sizeof (*list), KM_SLEEP);
20130Sstevel@tonic-gate 	list->prop_list = props;
20140Sstevel@tonic-gate 	list->prop_ref = 1;
20150Sstevel@tonic-gate 	return (list);
20160Sstevel@tonic-gate }
20170Sstevel@tonic-gate 
20180Sstevel@tonic-gate /*
20190Sstevel@tonic-gate  * Increment/decrement reference count. The reference is
20200Sstevel@tonic-gate  * protected by dn_lock. The only interfaces modifying
20210Sstevel@tonic-gate  * dn_global_prop_ptr is in impl_make[free]_parlist().
20220Sstevel@tonic-gate  */
20230Sstevel@tonic-gate void
20240Sstevel@tonic-gate i_ddi_prop_list_hold(ddi_prop_list_t *prop_list, struct devnames *dnp)
20250Sstevel@tonic-gate {
20260Sstevel@tonic-gate 	ASSERT(prop_list->prop_ref >= 0);
20270Sstevel@tonic-gate 	ASSERT(mutex_owned(&dnp->dn_lock));
20280Sstevel@tonic-gate 	prop_list->prop_ref++;
20290Sstevel@tonic-gate }
20300Sstevel@tonic-gate 
20310Sstevel@tonic-gate void
20320Sstevel@tonic-gate i_ddi_prop_list_rele(ddi_prop_list_t *prop_list, struct devnames *dnp)
20330Sstevel@tonic-gate {
20340Sstevel@tonic-gate 	ASSERT(prop_list->prop_ref > 0);
20350Sstevel@tonic-gate 	ASSERT(mutex_owned(&dnp->dn_lock));
20360Sstevel@tonic-gate 	prop_list->prop_ref--;
20370Sstevel@tonic-gate 
20380Sstevel@tonic-gate 	if (prop_list->prop_ref == 0) {
20390Sstevel@tonic-gate 		i_ddi_prop_list_delete(prop_list->prop_list);
20400Sstevel@tonic-gate 		kmem_free(prop_list, sizeof (*prop_list));
20410Sstevel@tonic-gate 	}
20420Sstevel@tonic-gate }
20430Sstevel@tonic-gate 
20440Sstevel@tonic-gate /*
20450Sstevel@tonic-gate  * Free table of classes by drivers
20460Sstevel@tonic-gate  */
20470Sstevel@tonic-gate void
20480Sstevel@tonic-gate i_ddi_free_exported_classes(char **classes, int n)
20490Sstevel@tonic-gate {
20500Sstevel@tonic-gate 	if ((n == 0) || (classes == NULL))
20510Sstevel@tonic-gate 		return;
20520Sstevel@tonic-gate 
20530Sstevel@tonic-gate 	kmem_free(classes, n * sizeof (char *));
20540Sstevel@tonic-gate }
20550Sstevel@tonic-gate 
20560Sstevel@tonic-gate /*
20570Sstevel@tonic-gate  * Get all classes exported by dip
20580Sstevel@tonic-gate  */
20590Sstevel@tonic-gate int
20600Sstevel@tonic-gate i_ddi_get_exported_classes(dev_info_t *dip, char ***classes)
20610Sstevel@tonic-gate {
20620Sstevel@tonic-gate 	extern void lock_hw_class_list();
20630Sstevel@tonic-gate 	extern void unlock_hw_class_list();
20640Sstevel@tonic-gate 	extern int get_class(const char *, char **);
20650Sstevel@tonic-gate 
20660Sstevel@tonic-gate 	static char *rootclass = "root";
20670Sstevel@tonic-gate 	int n = 0, nclass = 0;
20680Sstevel@tonic-gate 	char **buf;
20690Sstevel@tonic-gate 
20700Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) >= DS_BOUND);
20710Sstevel@tonic-gate 
20720Sstevel@tonic-gate 	if (dip == ddi_root_node())	/* rootnode exports class "root" */
20730Sstevel@tonic-gate 		nclass = 1;
20740Sstevel@tonic-gate 	lock_hw_class_list();
20750Sstevel@tonic-gate 	nclass += get_class(ddi_driver_name(dip), NULL);
20760Sstevel@tonic-gate 	if (nclass == 0) {
20770Sstevel@tonic-gate 		unlock_hw_class_list();
20780Sstevel@tonic-gate 		return (0);		/* no class exported */
20790Sstevel@tonic-gate 	}
20800Sstevel@tonic-gate 
20810Sstevel@tonic-gate 	*classes = buf = kmem_alloc(nclass * sizeof (char *), KM_SLEEP);
20820Sstevel@tonic-gate 	if (dip == ddi_root_node()) {
20830Sstevel@tonic-gate 		*buf++ = rootclass;
20840Sstevel@tonic-gate 		n = 1;
20850Sstevel@tonic-gate 	}
20860Sstevel@tonic-gate 	n += get_class(ddi_driver_name(dip), buf);
20870Sstevel@tonic-gate 	unlock_hw_class_list();
20880Sstevel@tonic-gate 
20890Sstevel@tonic-gate 	ASSERT(n == nclass);    /* make sure buf wasn't overrun */
20900Sstevel@tonic-gate 	return (nclass);
20910Sstevel@tonic-gate }
20920Sstevel@tonic-gate 
20930Sstevel@tonic-gate /*
20940Sstevel@tonic-gate  * Helper functions, returns NULL if no memory.
20950Sstevel@tonic-gate  */
20960Sstevel@tonic-gate char *
20970Sstevel@tonic-gate i_ddi_strdup(char *str, uint_t flag)
20980Sstevel@tonic-gate {
20990Sstevel@tonic-gate 	char *copy;
21000Sstevel@tonic-gate 
21010Sstevel@tonic-gate 	if (str == NULL)
21020Sstevel@tonic-gate 		return (NULL);
21030Sstevel@tonic-gate 
21040Sstevel@tonic-gate 	copy = kmem_alloc(strlen(str) + 1, flag);
21050Sstevel@tonic-gate 	if (copy == NULL)
21060Sstevel@tonic-gate 		return (NULL);
21070Sstevel@tonic-gate 
21080Sstevel@tonic-gate 	(void) strcpy(copy, str);
21090Sstevel@tonic-gate 	return (copy);
21100Sstevel@tonic-gate }
21110Sstevel@tonic-gate 
21120Sstevel@tonic-gate /*
21130Sstevel@tonic-gate  * Load driver.conf file for major. Load all if major == -1.
21140Sstevel@tonic-gate  *
21150Sstevel@tonic-gate  * This is called
21160Sstevel@tonic-gate  * - early in boot after devnames array is initialized
21170Sstevel@tonic-gate  * - from vfs code when certain file systems are mounted
21180Sstevel@tonic-gate  * - from add_drv when a new driver is added
21190Sstevel@tonic-gate  */
21200Sstevel@tonic-gate int
21210Sstevel@tonic-gate i_ddi_load_drvconf(major_t major)
21220Sstevel@tonic-gate {
21230Sstevel@tonic-gate 	extern int modrootloaded;
21240Sstevel@tonic-gate 
21250Sstevel@tonic-gate 	major_t low, high, m;
21260Sstevel@tonic-gate 
21270Sstevel@tonic-gate 	if (major == (major_t)-1) {
21280Sstevel@tonic-gate 		low = 0;
21290Sstevel@tonic-gate 		high = devcnt - 1;
21300Sstevel@tonic-gate 	} else {
21310Sstevel@tonic-gate 		if (major >= devcnt)
21320Sstevel@tonic-gate 			return (EINVAL);
21330Sstevel@tonic-gate 		low = high = major;
21340Sstevel@tonic-gate 	}
21350Sstevel@tonic-gate 
21360Sstevel@tonic-gate 	for (m = low; m <= high; m++) {
21370Sstevel@tonic-gate 		struct devnames *dnp = &devnamesp[m];
21380Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
21390Sstevel@tonic-gate 		dnp->dn_flags &= ~DN_DRIVER_HELD;
21400Sstevel@tonic-gate 		(void) impl_make_parlist(m);
21410Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
21420Sstevel@tonic-gate 	}
21430Sstevel@tonic-gate 
21440Sstevel@tonic-gate 	if (modrootloaded) {
21450Sstevel@tonic-gate 		ddi_walk_devs(ddi_root_node(), reset_nexus_flags,
21460Sstevel@tonic-gate 		    (void *)(uintptr_t)major);
21470Sstevel@tonic-gate 	}
21480Sstevel@tonic-gate 
21490Sstevel@tonic-gate 	/* build dn_list from old entries in path_to_inst */
21500Sstevel@tonic-gate 	e_ddi_unorphan_instance_nos();
21510Sstevel@tonic-gate 	return (0);
21520Sstevel@tonic-gate }
21530Sstevel@tonic-gate 
21540Sstevel@tonic-gate /*
21550Sstevel@tonic-gate  * Unload a specific driver.conf.
21560Sstevel@tonic-gate  * Don't support unload all because it doesn't make any sense
21570Sstevel@tonic-gate  */
21580Sstevel@tonic-gate int
21590Sstevel@tonic-gate i_ddi_unload_drvconf(major_t major)
21600Sstevel@tonic-gate {
21610Sstevel@tonic-gate 	int error;
21620Sstevel@tonic-gate 	struct devnames *dnp;
21630Sstevel@tonic-gate 
21640Sstevel@tonic-gate 	if (major >= devcnt)
21650Sstevel@tonic-gate 		return (EINVAL);
21660Sstevel@tonic-gate 
21670Sstevel@tonic-gate 	/*
21680Sstevel@tonic-gate 	 * Take the per-driver lock while unloading driver.conf
21690Sstevel@tonic-gate 	 */
21700Sstevel@tonic-gate 	dnp = &devnamesp[major];
21710Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
21720Sstevel@tonic-gate 	error = impl_free_parlist(major);
21730Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
21740Sstevel@tonic-gate 	return (error);
21750Sstevel@tonic-gate }
21760Sstevel@tonic-gate 
21770Sstevel@tonic-gate /*
21780Sstevel@tonic-gate  * Merge a .conf node. This is called by nexus drivers to augment
21790Sstevel@tonic-gate  * hw node with properties specified in driver.conf file. This function
21800Sstevel@tonic-gate  * takes a callback routine to name nexus children.
21810Sstevel@tonic-gate  * The parent node must be held busy.
21820Sstevel@tonic-gate  *
21830Sstevel@tonic-gate  * It returns DDI_SUCCESS if the node is merged and DDI_FAILURE otherwise.
21840Sstevel@tonic-gate  */
21850Sstevel@tonic-gate int
21860Sstevel@tonic-gate ndi_merge_node(dev_info_t *dip, int (*name_node)(dev_info_t *, char *, int))
21870Sstevel@tonic-gate {
21880Sstevel@tonic-gate 	dev_info_t *hwdip;
21890Sstevel@tonic-gate 
21900Sstevel@tonic-gate 	ASSERT(ndi_dev_is_persistent_node(dip) == 0);
21910Sstevel@tonic-gate 	ASSERT(ddi_get_name_addr(dip) != NULL);
21920Sstevel@tonic-gate 
21930Sstevel@tonic-gate 	hwdip = find_child_by_callback(ddi_get_parent(dip),
21940Sstevel@tonic-gate 	    ddi_binding_name(dip), ddi_get_name_addr(dip), name_node);
21950Sstevel@tonic-gate 
21960Sstevel@tonic-gate 	/*
21970Sstevel@tonic-gate 	 * Look for the hardware node that is the target of the merge;
21980Sstevel@tonic-gate 	 * return failure if not found.
21990Sstevel@tonic-gate 	 */
22000Sstevel@tonic-gate 	if ((hwdip == NULL) || (hwdip == dip)) {
22010Sstevel@tonic-gate 		char *buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
22020Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_WARN, "No HW node to merge conf node %s",
22030Sstevel@tonic-gate 		    ddi_deviname(dip, buf)));
22040Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
22050Sstevel@tonic-gate 		return (DDI_FAILURE);
22060Sstevel@tonic-gate 	}
22070Sstevel@tonic-gate 
22080Sstevel@tonic-gate 	/*
22090Sstevel@tonic-gate 	 * Make sure the hardware node is uninitialized and has no property.
22100Sstevel@tonic-gate 	 * This may not be the case if new .conf files are load after some
22110Sstevel@tonic-gate 	 * hardware nodes have already been initialized and attached.
22120Sstevel@tonic-gate 	 *
22130Sstevel@tonic-gate 	 * N.B. We return success here because the node was *intended*
22140Sstevel@tonic-gate 	 * 	to be a merge node because there is a hw node with the name.
22150Sstevel@tonic-gate 	 */
22160Sstevel@tonic-gate 	mutex_enter(&DEVI(hwdip)->devi_lock);
22170Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(hwdip) == 0) {
22180Sstevel@tonic-gate 		char *buf;
22190Sstevel@tonic-gate 		mutex_exit(&DEVI(hwdip)->devi_lock);
22200Sstevel@tonic-gate 
22210Sstevel@tonic-gate 		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
22220Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE, "Duplicate .conf node %s",
22230Sstevel@tonic-gate 		    ddi_deviname(dip, buf)));
22240Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
22250Sstevel@tonic-gate 		return (DDI_SUCCESS);
22260Sstevel@tonic-gate 	}
22270Sstevel@tonic-gate 
22280Sstevel@tonic-gate 	/*
22290Sstevel@tonic-gate 	 * If it is possible that the hardware has already been touched
22300Sstevel@tonic-gate 	 * then don't merge.
22310Sstevel@tonic-gate 	 */
22320Sstevel@tonic-gate 	if (i_ddi_node_state(hwdip) >= DS_INITIALIZED ||
22330Sstevel@tonic-gate 	    (DEVI(hwdip)->devi_sys_prop_ptr != NULL) ||
22340Sstevel@tonic-gate 	    (DEVI(hwdip)->devi_drv_prop_ptr != NULL)) {
22350Sstevel@tonic-gate 		char *buf;
22360Sstevel@tonic-gate 		mutex_exit(&DEVI(hwdip)->devi_lock);
22370Sstevel@tonic-gate 
22380Sstevel@tonic-gate 		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
22390Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE,
22400Sstevel@tonic-gate 		    "!Cannot merge .conf node %s with hw node %p "
22410Sstevel@tonic-gate 		    "-- not in proper state",
22420Sstevel@tonic-gate 		    ddi_deviname(dip, buf), (void *)hwdip));
22430Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
22440Sstevel@tonic-gate 		return (DDI_SUCCESS);
22450Sstevel@tonic-gate 	}
22460Sstevel@tonic-gate 
22470Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
22480Sstevel@tonic-gate 	DEVI(hwdip)->devi_sys_prop_ptr = DEVI(dip)->devi_sys_prop_ptr;
22490Sstevel@tonic-gate 	DEVI(hwdip)->devi_drv_prop_ptr = DEVI(dip)->devi_drv_prop_ptr;
22500Sstevel@tonic-gate 	DEVI(dip)->devi_sys_prop_ptr = NULL;
22510Sstevel@tonic-gate 	DEVI(dip)->devi_drv_prop_ptr = NULL;
22520Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
22530Sstevel@tonic-gate 	mutex_exit(&DEVI(hwdip)->devi_lock);
22540Sstevel@tonic-gate 
22550Sstevel@tonic-gate 	return (DDI_SUCCESS);
22560Sstevel@tonic-gate }
22570Sstevel@tonic-gate 
22580Sstevel@tonic-gate /*
22590Sstevel@tonic-gate  * Merge a "wildcard" .conf node. This is called by nexus drivers to
22600Sstevel@tonic-gate  * augment a set of hw node with properties specified in driver.conf file.
22610Sstevel@tonic-gate  * The parent node must be held busy.
22620Sstevel@tonic-gate  *
22630Sstevel@tonic-gate  * There is no failure mode, since the nexus may or may not have child
22640Sstevel@tonic-gate  * node bound the driver specified by the wildcard node.
22650Sstevel@tonic-gate  */
22660Sstevel@tonic-gate void
22670Sstevel@tonic-gate ndi_merge_wildcard_node(dev_info_t *dip)
22680Sstevel@tonic-gate {
22690Sstevel@tonic-gate 	dev_info_t *hwdip;
22700Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
22710Sstevel@tonic-gate 	major_t major = ddi_driver_major(dip);
22720Sstevel@tonic-gate 
22730Sstevel@tonic-gate 	/* never attempt to merge a hw node */
22740Sstevel@tonic-gate 	ASSERT(ndi_dev_is_persistent_node(dip) == 0);
22750Sstevel@tonic-gate 	/* must be bound to a driver major number */
22760Sstevel@tonic-gate 	ASSERT(major != (major_t)-1);
22770Sstevel@tonic-gate 
22780Sstevel@tonic-gate 	/*
22790Sstevel@tonic-gate 	 * Walk the child list to find all nodes bound to major
22800Sstevel@tonic-gate 	 * and copy properties.
22810Sstevel@tonic-gate 	 */
22820Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
22830Sstevel@tonic-gate 	for (hwdip = ddi_get_child(pdip); hwdip;
22840Sstevel@tonic-gate 	    hwdip = ddi_get_next_sibling(hwdip)) {
22850Sstevel@tonic-gate 		/*
22860Sstevel@tonic-gate 		 * Skip nodes not bound to same driver
22870Sstevel@tonic-gate 		 */
22880Sstevel@tonic-gate 		if (ddi_driver_major(hwdip) != major)
22890Sstevel@tonic-gate 			continue;
22900Sstevel@tonic-gate 
22910Sstevel@tonic-gate 		/*
22920Sstevel@tonic-gate 		 * Skip .conf nodes
22930Sstevel@tonic-gate 		 */
22940Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(hwdip) == 0)
22950Sstevel@tonic-gate 			continue;
22960Sstevel@tonic-gate 
22970Sstevel@tonic-gate 		/*
22980Sstevel@tonic-gate 		 * Make sure the node is uninitialized and has no property.
22990Sstevel@tonic-gate 		 */
23000Sstevel@tonic-gate 		mutex_enter(&DEVI(hwdip)->devi_lock);
23010Sstevel@tonic-gate 		if (i_ddi_node_state(hwdip) >= DS_INITIALIZED ||
23020Sstevel@tonic-gate 		    (DEVI(hwdip)->devi_sys_prop_ptr != NULL) ||
23030Sstevel@tonic-gate 		    (DEVI(hwdip)->devi_drv_prop_ptr != NULL)) {
23040Sstevel@tonic-gate 			mutex_exit(&DEVI(hwdip)->devi_lock);
23050Sstevel@tonic-gate 			NDI_CONFIG_DEBUG((CE_NOTE, "HW node %p state not "
23060Sstevel@tonic-gate 			    "suitable for merging wildcard conf node %s",
23070Sstevel@tonic-gate 			    (void *)hwdip, ddi_node_name(dip)));
23080Sstevel@tonic-gate 			continue;
23090Sstevel@tonic-gate 		}
23100Sstevel@tonic-gate 
23110Sstevel@tonic-gate 		DEVI(hwdip)->devi_sys_prop_ptr =
23120Sstevel@tonic-gate 		    i_ddi_prop_list_dup(DEVI(dip)->devi_sys_prop_ptr, KM_SLEEP);
23130Sstevel@tonic-gate 		DEVI(hwdip)->devi_drv_prop_ptr =
23140Sstevel@tonic-gate 		    i_ddi_prop_list_dup(DEVI(dip)->devi_drv_prop_ptr, KM_SLEEP);
23150Sstevel@tonic-gate 		mutex_exit(&DEVI(hwdip)->devi_lock);
23160Sstevel@tonic-gate 	}
23170Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
23180Sstevel@tonic-gate }
23190Sstevel@tonic-gate 
23200Sstevel@tonic-gate /*
23210Sstevel@tonic-gate  * Return the major number based on the compatible property. This interface
23220Sstevel@tonic-gate  * may be used in situations where we are trying to detect if a better driver
23230Sstevel@tonic-gate  * now exists for a device, so it must use the 'compatible' property.  If
23240Sstevel@tonic-gate  * a non-NULL formp is specified and the binding was based on compatible then
23250Sstevel@tonic-gate  * return the pointer to the form used in *formp.
23260Sstevel@tonic-gate  */
23270Sstevel@tonic-gate major_t
23280Sstevel@tonic-gate ddi_compatible_driver_major(dev_info_t *dip, char **formp)
23290Sstevel@tonic-gate {
23300Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
23310Sstevel@tonic-gate 	void		*compat;
23320Sstevel@tonic-gate 	size_t		len;
23330Sstevel@tonic-gate 	char		*p = NULL;
23340Sstevel@tonic-gate 	major_t		major = (major_t)-1;
23350Sstevel@tonic-gate 
23360Sstevel@tonic-gate 	if (formp)
23370Sstevel@tonic-gate 		*formp = NULL;
23380Sstevel@tonic-gate 
23390Sstevel@tonic-gate 	/* look up compatible property */
23400Sstevel@tonic-gate 	(void) lookup_compatible(dip, KM_SLEEP);
23410Sstevel@tonic-gate 	compat = (void *)(devi->devi_compat_names);
23420Sstevel@tonic-gate 	len = devi->devi_compat_length;
23430Sstevel@tonic-gate 
23440Sstevel@tonic-gate 	/* find the highest precedence compatible form with a driver binding */
23450Sstevel@tonic-gate 	while ((p = prom_decode_composite_string(compat, len, p)) != NULL) {
23460Sstevel@tonic-gate 		major = ddi_name_to_major(p);
23470Sstevel@tonic-gate 		if ((major != (major_t)-1) &&
23480Sstevel@tonic-gate 		    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED)) {
23490Sstevel@tonic-gate 			if (formp)
23500Sstevel@tonic-gate 				*formp = p;
23510Sstevel@tonic-gate 			return (major);
23520Sstevel@tonic-gate 		}
23530Sstevel@tonic-gate 	}
23540Sstevel@tonic-gate 
23550Sstevel@tonic-gate 	/*
23560Sstevel@tonic-gate 	 * none of the compatible forms have a driver binding, see if
23570Sstevel@tonic-gate 	 * the node name has a driver binding.
23580Sstevel@tonic-gate 	 */
23590Sstevel@tonic-gate 	major = ddi_name_to_major(ddi_node_name(dip));
23600Sstevel@tonic-gate 	if ((major != (major_t)-1) &&
23610Sstevel@tonic-gate 	    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED))
23620Sstevel@tonic-gate 		return (major);
23630Sstevel@tonic-gate 
23640Sstevel@tonic-gate 	/* no driver */
23650Sstevel@tonic-gate 	return ((major_t)-1);
23660Sstevel@tonic-gate }
23670Sstevel@tonic-gate 
23680Sstevel@tonic-gate /*
23690Sstevel@tonic-gate  * Static help functions
23700Sstevel@tonic-gate  */
23710Sstevel@tonic-gate 
23720Sstevel@tonic-gate /*
23730Sstevel@tonic-gate  * lookup the "compatible" property and cache it's contents in the
23740Sstevel@tonic-gate  * device node.
23750Sstevel@tonic-gate  */
23760Sstevel@tonic-gate static int
23770Sstevel@tonic-gate lookup_compatible(dev_info_t *dip, uint_t flag)
23780Sstevel@tonic-gate {
23790Sstevel@tonic-gate 	int rv;
23800Sstevel@tonic-gate 	int prop_flags;
23810Sstevel@tonic-gate 	uint_t ncompatstrs;
23820Sstevel@tonic-gate 	char **compatstrpp;
23830Sstevel@tonic-gate 	char *di_compat_strp;
23840Sstevel@tonic-gate 	size_t di_compat_strlen;
23850Sstevel@tonic-gate 
23860Sstevel@tonic-gate 	if (DEVI(dip)->devi_compat_names) {
23870Sstevel@tonic-gate 		return (DDI_SUCCESS);
23880Sstevel@tonic-gate 	}
23890Sstevel@tonic-gate 
23900Sstevel@tonic-gate 	prop_flags = DDI_PROP_TYPE_STRING | DDI_PROP_DONTPASS;
23910Sstevel@tonic-gate 
23920Sstevel@tonic-gate 	if (flag & KM_NOSLEEP) {
23930Sstevel@tonic-gate 		prop_flags |= DDI_PROP_DONTSLEEP;
23940Sstevel@tonic-gate 	}
23950Sstevel@tonic-gate 
23960Sstevel@tonic-gate 	if (ndi_dev_is_prom_node(dip) == 0) {
23970Sstevel@tonic-gate 		prop_flags |= DDI_PROP_NOTPROM;
23980Sstevel@tonic-gate 	}
23990Sstevel@tonic-gate 
24000Sstevel@tonic-gate 	rv = ddi_prop_lookup_common(DDI_DEV_T_ANY, dip, prop_flags,
24010Sstevel@tonic-gate 	    "compatible", &compatstrpp, &ncompatstrs,
24020Sstevel@tonic-gate 	    ddi_prop_fm_decode_strings);
24030Sstevel@tonic-gate 
24040Sstevel@tonic-gate 	if (rv == DDI_PROP_NOT_FOUND) {
24050Sstevel@tonic-gate 		return (DDI_SUCCESS);
24060Sstevel@tonic-gate 	}
24070Sstevel@tonic-gate 
24080Sstevel@tonic-gate 	if (rv != DDI_PROP_SUCCESS) {
24090Sstevel@tonic-gate 		return (DDI_FAILURE);
24100Sstevel@tonic-gate 	}
24110Sstevel@tonic-gate 
24120Sstevel@tonic-gate 	/*
24130Sstevel@tonic-gate 	 * encode the compatible property data in the dev_info node
24140Sstevel@tonic-gate 	 */
24150Sstevel@tonic-gate 	rv = DDI_SUCCESS;
24160Sstevel@tonic-gate 	if (ncompatstrs != 0) {
24170Sstevel@tonic-gate 		di_compat_strp = encode_composite_string(compatstrpp,
24180Sstevel@tonic-gate 		    ncompatstrs, &di_compat_strlen, flag);
24190Sstevel@tonic-gate 		if (di_compat_strp != NULL) {
24200Sstevel@tonic-gate 			DEVI(dip)->devi_compat_names = di_compat_strp;
24210Sstevel@tonic-gate 			DEVI(dip)->devi_compat_length = di_compat_strlen;
24220Sstevel@tonic-gate 		} else {
24230Sstevel@tonic-gate 			rv = DDI_FAILURE;
24240Sstevel@tonic-gate 		}
24250Sstevel@tonic-gate 	}
24260Sstevel@tonic-gate 	ddi_prop_free(compatstrpp);
24270Sstevel@tonic-gate 	return (rv);
24280Sstevel@tonic-gate }
24290Sstevel@tonic-gate 
24300Sstevel@tonic-gate /*
24310Sstevel@tonic-gate  * Create a composite string from a list of strings.
24320Sstevel@tonic-gate  *
24330Sstevel@tonic-gate  * A composite string consists of a single buffer containing one
24340Sstevel@tonic-gate  * or more NULL terminated strings.
24350Sstevel@tonic-gate  */
24360Sstevel@tonic-gate static char *
24370Sstevel@tonic-gate encode_composite_string(char **strings, uint_t nstrings, size_t *retsz,
24380Sstevel@tonic-gate     uint_t flag)
24390Sstevel@tonic-gate {
24400Sstevel@tonic-gate 	uint_t index;
24410Sstevel@tonic-gate 	char  **strpp;
24420Sstevel@tonic-gate 	uint_t slen;
24430Sstevel@tonic-gate 	size_t cbuf_sz = 0;
24440Sstevel@tonic-gate 	char *cbuf_p;
24450Sstevel@tonic-gate 	char *cbuf_ip;
24460Sstevel@tonic-gate 
24470Sstevel@tonic-gate 	if (strings == NULL || nstrings == 0 || retsz == NULL) {
24480Sstevel@tonic-gate 		return (NULL);
24490Sstevel@tonic-gate 	}
24500Sstevel@tonic-gate 
24510Sstevel@tonic-gate 	for (index = 0, strpp = strings; index < nstrings; index++)
24520Sstevel@tonic-gate 		cbuf_sz += strlen(*(strpp++)) + 1;
24530Sstevel@tonic-gate 
24540Sstevel@tonic-gate 	if ((cbuf_p = kmem_alloc(cbuf_sz, flag)) == NULL) {
24550Sstevel@tonic-gate 		cmn_err(CE_NOTE,
24560Sstevel@tonic-gate 		    "?failed to allocate device node compatstr");
24570Sstevel@tonic-gate 		return (NULL);
24580Sstevel@tonic-gate 	}
24590Sstevel@tonic-gate 
24600Sstevel@tonic-gate 	cbuf_ip = cbuf_p;
24610Sstevel@tonic-gate 	for (index = 0, strpp = strings; index < nstrings; index++) {
24620Sstevel@tonic-gate 		slen = strlen(*strpp);
24630Sstevel@tonic-gate 		bcopy(*(strpp++), cbuf_ip, slen);
24640Sstevel@tonic-gate 		cbuf_ip += slen;
24650Sstevel@tonic-gate 		*(cbuf_ip++) = '\0';
24660Sstevel@tonic-gate 	}
24670Sstevel@tonic-gate 
24680Sstevel@tonic-gate 	*retsz = cbuf_sz;
24690Sstevel@tonic-gate 	return (cbuf_p);
24700Sstevel@tonic-gate }
24710Sstevel@tonic-gate 
24720Sstevel@tonic-gate static void
24730Sstevel@tonic-gate link_to_driver_list(dev_info_t *dip)
24740Sstevel@tonic-gate {
24750Sstevel@tonic-gate 	major_t major = DEVI(dip)->devi_major;
24760Sstevel@tonic-gate 	struct devnames *dnp;
24770Sstevel@tonic-gate 
24780Sstevel@tonic-gate 	ASSERT(major != (major_t)-1);
24790Sstevel@tonic-gate 
24800Sstevel@tonic-gate 	/*
24810Sstevel@tonic-gate 	 * Remove from orphan list
24820Sstevel@tonic-gate 	 */
24830Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
24840Sstevel@tonic-gate 		dnp = &orphanlist;
24850Sstevel@tonic-gate 		remove_from_dn_list(dnp, dip);
24860Sstevel@tonic-gate 	}
24870Sstevel@tonic-gate 
24880Sstevel@tonic-gate 	/*
24890Sstevel@tonic-gate 	 * Add to per driver list
24900Sstevel@tonic-gate 	 */
24910Sstevel@tonic-gate 	dnp = &devnamesp[major];
24920Sstevel@tonic-gate 	add_to_dn_list(dnp, dip);
24930Sstevel@tonic-gate }
24940Sstevel@tonic-gate 
24950Sstevel@tonic-gate static void
24960Sstevel@tonic-gate unlink_from_driver_list(dev_info_t *dip)
24970Sstevel@tonic-gate {
24980Sstevel@tonic-gate 	major_t major = DEVI(dip)->devi_major;
24990Sstevel@tonic-gate 	struct devnames *dnp;
25000Sstevel@tonic-gate 
25010Sstevel@tonic-gate 	ASSERT(major != (major_t)-1);
25020Sstevel@tonic-gate 
25030Sstevel@tonic-gate 	/*
25040Sstevel@tonic-gate 	 * Remove from per-driver list
25050Sstevel@tonic-gate 	 */
25060Sstevel@tonic-gate 	dnp = &devnamesp[major];
25070Sstevel@tonic-gate 	remove_from_dn_list(dnp, dip);
25080Sstevel@tonic-gate 
25090Sstevel@tonic-gate 	/*
25100Sstevel@tonic-gate 	 * Add to orphan list
25110Sstevel@tonic-gate 	 */
25120Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
25130Sstevel@tonic-gate 		dnp = &orphanlist;
25140Sstevel@tonic-gate 		add_to_dn_list(dnp, dip);
25150Sstevel@tonic-gate 	}
25160Sstevel@tonic-gate }
25170Sstevel@tonic-gate 
25180Sstevel@tonic-gate /*
25190Sstevel@tonic-gate  * scan the per-driver list looking for dev_info "dip"
25200Sstevel@tonic-gate  */
25210Sstevel@tonic-gate static dev_info_t *
25220Sstevel@tonic-gate in_dn_list(struct devnames *dnp, dev_info_t *dip)
25230Sstevel@tonic-gate {
25240Sstevel@tonic-gate 	struct dev_info *idevi;
25250Sstevel@tonic-gate 
25260Sstevel@tonic-gate 	if ((idevi = DEVI(dnp->dn_head)) == NULL)
25270Sstevel@tonic-gate 		return (NULL);
25280Sstevel@tonic-gate 
25290Sstevel@tonic-gate 	while (idevi) {
25300Sstevel@tonic-gate 		if (idevi == DEVI(dip))
25310Sstevel@tonic-gate 			return (dip);
25320Sstevel@tonic-gate 		idevi = idevi->devi_next;
25330Sstevel@tonic-gate 	}
25340Sstevel@tonic-gate 	return (NULL);
25350Sstevel@tonic-gate }
25360Sstevel@tonic-gate 
25370Sstevel@tonic-gate /*
25380Sstevel@tonic-gate  * insert devinfo node 'dip' into the per-driver instance list
25390Sstevel@tonic-gate  * headed by 'dnp'
25400Sstevel@tonic-gate  *
25410Sstevel@tonic-gate  * Nodes on the per-driver list are ordered: HW - SID - PSEUDO.  The order is
25420Sstevel@tonic-gate  * required for merging of .conf file data to work properly.
25430Sstevel@tonic-gate  */
25440Sstevel@tonic-gate static void
25450Sstevel@tonic-gate add_to_ordered_dn_list(struct devnames *dnp, dev_info_t *dip)
25460Sstevel@tonic-gate {
25470Sstevel@tonic-gate 	dev_info_t **dipp;
25480Sstevel@tonic-gate 
25490Sstevel@tonic-gate 	ASSERT(mutex_owned(&(dnp->dn_lock)));
25500Sstevel@tonic-gate 
25510Sstevel@tonic-gate 	dipp = &dnp->dn_head;
25520Sstevel@tonic-gate 	if (ndi_dev_is_prom_node(dip)) {
25530Sstevel@tonic-gate 		/*
25540Sstevel@tonic-gate 		 * Find the first non-prom node or end of list
25550Sstevel@tonic-gate 		 */
25560Sstevel@tonic-gate 		while (*dipp && (ndi_dev_is_prom_node(*dipp) != 0)) {
25570Sstevel@tonic-gate 			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
25580Sstevel@tonic-gate 		}
25590Sstevel@tonic-gate 	} else if (ndi_dev_is_persistent_node(dip)) {
25600Sstevel@tonic-gate 		/*
25610Sstevel@tonic-gate 		 * Find the first non-persistent node
25620Sstevel@tonic-gate 		 */
25630Sstevel@tonic-gate 		while (*dipp && (ndi_dev_is_persistent_node(*dipp) != 0)) {
25640Sstevel@tonic-gate 			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
25650Sstevel@tonic-gate 		}
25660Sstevel@tonic-gate 	} else {
25670Sstevel@tonic-gate 		/*
25680Sstevel@tonic-gate 		 * Find the end of the list
25690Sstevel@tonic-gate 		 */
25700Sstevel@tonic-gate 		while (*dipp) {
25710Sstevel@tonic-gate 			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
25720Sstevel@tonic-gate 		}
25730Sstevel@tonic-gate 	}
25740Sstevel@tonic-gate 
25750Sstevel@tonic-gate 	DEVI(dip)->devi_next = DEVI(*dipp);
25760Sstevel@tonic-gate 	*dipp = dip;
25770Sstevel@tonic-gate }
25780Sstevel@tonic-gate 
25790Sstevel@tonic-gate /*
25800Sstevel@tonic-gate  * add a list of device nodes to the device node list in the
25810Sstevel@tonic-gate  * devnames structure
25820Sstevel@tonic-gate  */
25830Sstevel@tonic-gate static void
25840Sstevel@tonic-gate add_to_dn_list(struct devnames *dnp, dev_info_t *dip)
25850Sstevel@tonic-gate {
25860Sstevel@tonic-gate 	/*
25870Sstevel@tonic-gate 	 * Look to see if node already exists
25880Sstevel@tonic-gate 	 */
25890Sstevel@tonic-gate 	LOCK_DEV_OPS(&(dnp->dn_lock));
25900Sstevel@tonic-gate 	if (in_dn_list(dnp, dip)) {
25910Sstevel@tonic-gate 		cmn_err(CE_NOTE, "add_to_dn_list: node %s already in list",
25920Sstevel@tonic-gate 		    DEVI(dip)->devi_node_name);
25930Sstevel@tonic-gate 	} else {
25940Sstevel@tonic-gate 		add_to_ordered_dn_list(dnp, dip);
25950Sstevel@tonic-gate 	}
25960Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
25970Sstevel@tonic-gate }
25980Sstevel@tonic-gate 
25990Sstevel@tonic-gate static void
26000Sstevel@tonic-gate remove_from_dn_list(struct devnames *dnp, dev_info_t *dip)
26010Sstevel@tonic-gate {
26020Sstevel@tonic-gate 	dev_info_t **plist;
26030Sstevel@tonic-gate 
26040Sstevel@tonic-gate 	LOCK_DEV_OPS(&(dnp->dn_lock));
26050Sstevel@tonic-gate 
26060Sstevel@tonic-gate 	plist = (dev_info_t **)&dnp->dn_head;
26070Sstevel@tonic-gate 	while (*plist && (*plist != dip)) {
26080Sstevel@tonic-gate 		plist = (dev_info_t **)&DEVI(*plist)->devi_next;
26090Sstevel@tonic-gate 	}
26100Sstevel@tonic-gate 
26110Sstevel@tonic-gate 	if (*plist != NULL) {
26120Sstevel@tonic-gate 		ASSERT(*plist == dip);
26130Sstevel@tonic-gate 		*plist = (dev_info_t *)(DEVI(dip)->devi_next);
26140Sstevel@tonic-gate 		DEVI(dip)->devi_next = NULL;
26150Sstevel@tonic-gate 	} else {
26160Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE,
26170Sstevel@tonic-gate 		    "remove_from_dn_list: node %s not found in list",
26180Sstevel@tonic-gate 		    DEVI(dip)->devi_node_name));
26190Sstevel@tonic-gate 	}
26200Sstevel@tonic-gate 
26210Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
26220Sstevel@tonic-gate }
26230Sstevel@tonic-gate 
26240Sstevel@tonic-gate /*
26250Sstevel@tonic-gate  * Add and remove reference driver global property list
26260Sstevel@tonic-gate  */
26270Sstevel@tonic-gate static void
26280Sstevel@tonic-gate add_global_props(dev_info_t *dip)
26290Sstevel@tonic-gate {
26300Sstevel@tonic-gate 	struct devnames *dnp;
26310Sstevel@tonic-gate 	ddi_prop_list_t *plist;
26320Sstevel@tonic-gate 
26330Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_global_prop_list == NULL);
26340Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_major != (major_t)-1);
26350Sstevel@tonic-gate 
26360Sstevel@tonic-gate 	dnp = &devnamesp[DEVI(dip)->devi_major];
26370Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
26380Sstevel@tonic-gate 	plist = dnp->dn_global_prop_ptr;
26390Sstevel@tonic-gate 	if (plist == NULL) {
26400Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
26410Sstevel@tonic-gate 		return;
26420Sstevel@tonic-gate 	}
26430Sstevel@tonic-gate 	i_ddi_prop_list_hold(plist, dnp);
26440Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
26450Sstevel@tonic-gate 
26460Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
26470Sstevel@tonic-gate 	DEVI(dip)->devi_global_prop_list = plist;
26480Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
26490Sstevel@tonic-gate }
26500Sstevel@tonic-gate 
26510Sstevel@tonic-gate static void
26520Sstevel@tonic-gate remove_global_props(dev_info_t *dip)
26530Sstevel@tonic-gate {
26540Sstevel@tonic-gate 	ddi_prop_list_t *proplist;
26550Sstevel@tonic-gate 
26560Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
26570Sstevel@tonic-gate 	proplist = DEVI(dip)->devi_global_prop_list;
26580Sstevel@tonic-gate 	DEVI(dip)->devi_global_prop_list = NULL;
26590Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
26600Sstevel@tonic-gate 
26610Sstevel@tonic-gate 	if (proplist) {
26620Sstevel@tonic-gate 		major_t major;
26630Sstevel@tonic-gate 		struct devnames *dnp;
26640Sstevel@tonic-gate 
26650Sstevel@tonic-gate 		major = ddi_driver_major(dip);
26660Sstevel@tonic-gate 		ASSERT(major != (major_t)-1);
26670Sstevel@tonic-gate 		dnp = &devnamesp[major];
26680Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
26690Sstevel@tonic-gate 		i_ddi_prop_list_rele(proplist, dnp);
26700Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
26710Sstevel@tonic-gate 	}
26720Sstevel@tonic-gate }
26730Sstevel@tonic-gate 
26740Sstevel@tonic-gate #ifdef DEBUG
26750Sstevel@tonic-gate /*
26760Sstevel@tonic-gate  * Set this variable to '0' to disable the optimization,
26770Sstevel@tonic-gate  * and to 2 to print debug message.
26780Sstevel@tonic-gate  */
26790Sstevel@tonic-gate static int optimize_dtree = 1;
26800Sstevel@tonic-gate 
26810Sstevel@tonic-gate static void
26820Sstevel@tonic-gate debug_dtree(dev_info_t *devi, struct dev_info *adevi, char *service)
26830Sstevel@tonic-gate {
26840Sstevel@tonic-gate 	char *adeviname, *buf;
26850Sstevel@tonic-gate 
26860Sstevel@tonic-gate 	/*
26870Sstevel@tonic-gate 	 * Don't print unless optimize dtree is set to 2+
26880Sstevel@tonic-gate 	 */
26890Sstevel@tonic-gate 	if (optimize_dtree <= 1)
26900Sstevel@tonic-gate 		return;
26910Sstevel@tonic-gate 
26920Sstevel@tonic-gate 	buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
26930Sstevel@tonic-gate 	adeviname = ddi_deviname((dev_info_t *)adevi, buf);
26940Sstevel@tonic-gate 	if (*adeviname == '\0')
26950Sstevel@tonic-gate 		adeviname = "root";
26960Sstevel@tonic-gate 
26970Sstevel@tonic-gate 	cmn_err(CE_CONT, "%s %s -> %s\n",
26980Sstevel@tonic-gate 	    ddi_deviname(devi, buf), service, adeviname);
26990Sstevel@tonic-gate 
27000Sstevel@tonic-gate 	kmem_free(buf, MAXNAMELEN);
27010Sstevel@tonic-gate }
27020Sstevel@tonic-gate #else /* DEBUG */
27030Sstevel@tonic-gate #define	debug_dtree(a1, a2, a3)	 /* nothing */
27040Sstevel@tonic-gate #endif  /* DEBUG */
27050Sstevel@tonic-gate 
27060Sstevel@tonic-gate static void
27070Sstevel@tonic-gate ddi_optimize_dtree(dev_info_t *devi)
27080Sstevel@tonic-gate {
27090Sstevel@tonic-gate 	struct dev_info *pdevi;
27100Sstevel@tonic-gate 	struct bus_ops *b;
27110Sstevel@tonic-gate 
27120Sstevel@tonic-gate 	pdevi = DEVI(devi)->devi_parent;
27130Sstevel@tonic-gate 	ASSERT(pdevi);
27140Sstevel@tonic-gate 
27150Sstevel@tonic-gate 	/*
27160Sstevel@tonic-gate 	 * Set the unoptimized values
27170Sstevel@tonic-gate 	 */
27180Sstevel@tonic-gate 	DEVI(devi)->devi_bus_map_fault = pdevi;
27190Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_map = pdevi;
27200Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_allochdl = pdevi;
27210Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_freehdl = pdevi;
27220Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_bindhdl = pdevi;
27230Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_bindfunc =
27240Sstevel@tonic-gate 	pdevi->devi_ops->devo_bus_ops->bus_dma_bindhdl;
27250Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_unbindhdl = pdevi;
27260Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_unbindfunc =
27270Sstevel@tonic-gate 	    pdevi->devi_ops->devo_bus_ops->bus_dma_unbindhdl;
27280Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_flush = pdevi;
27290Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_win = pdevi;
27300Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_ctl = pdevi;
27310Sstevel@tonic-gate 	DEVI(devi)->devi_bus_ctl = pdevi;
27320Sstevel@tonic-gate 
27330Sstevel@tonic-gate #ifdef DEBUG
27340Sstevel@tonic-gate 	if (optimize_dtree == 0)
27350Sstevel@tonic-gate 		return;
27360Sstevel@tonic-gate #endif /* DEBUG */
27370Sstevel@tonic-gate 
27380Sstevel@tonic-gate 	b = pdevi->devi_ops->devo_bus_ops;
27390Sstevel@tonic-gate 
27400Sstevel@tonic-gate 	if (i_ddi_map_fault == b->bus_map_fault) {
27410Sstevel@tonic-gate 		DEVI(devi)->devi_bus_map_fault = pdevi->devi_bus_map_fault;
27420Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_map_fault,
27430Sstevel@tonic-gate 		    "bus_map_fault");
27440Sstevel@tonic-gate 	}
27450Sstevel@tonic-gate 
27460Sstevel@tonic-gate 	if (ddi_dma_map == b->bus_dma_map) {
27470Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_map = pdevi->devi_bus_dma_map;
27480Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_map, "bus_dma_map");
27490Sstevel@tonic-gate 	}
27500Sstevel@tonic-gate 
27510Sstevel@tonic-gate 	if (ddi_dma_allochdl == b->bus_dma_allochdl) {
27520Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_allochdl =
27530Sstevel@tonic-gate 		    pdevi->devi_bus_dma_allochdl;
27540Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_allochdl,
27550Sstevel@tonic-gate 		    "bus_dma_allochdl");
27560Sstevel@tonic-gate 	}
27570Sstevel@tonic-gate 
27580Sstevel@tonic-gate 	if (ddi_dma_freehdl == b->bus_dma_freehdl) {
27590Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_freehdl = pdevi->devi_bus_dma_freehdl;
27600Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_freehdl,
27610Sstevel@tonic-gate 		    "bus_dma_freehdl");
27620Sstevel@tonic-gate 	}
27630Sstevel@tonic-gate 
27640Sstevel@tonic-gate 	if (ddi_dma_bindhdl == b->bus_dma_bindhdl) {
27650Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_bindhdl = pdevi->devi_bus_dma_bindhdl;
27660Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_bindfunc =
27670Sstevel@tonic-gate 		    pdevi->devi_bus_dma_bindhdl->devi_ops->
27680Sstevel@tonic-gate 		    devo_bus_ops->bus_dma_bindhdl;
27690Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_bindhdl,
27700Sstevel@tonic-gate 		    "bus_dma_bindhdl");
27710Sstevel@tonic-gate 	}
27720Sstevel@tonic-gate 
27730Sstevel@tonic-gate 	if (ddi_dma_unbindhdl == b->bus_dma_unbindhdl) {
27740Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_unbindhdl =
27750Sstevel@tonic-gate 		    pdevi->devi_bus_dma_unbindhdl;
27760Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_unbindfunc =
27770Sstevel@tonic-gate 		    pdevi->devi_bus_dma_unbindhdl->devi_ops->
27780Sstevel@tonic-gate 		    devo_bus_ops->bus_dma_unbindhdl;
27790Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_unbindhdl,
27800Sstevel@tonic-gate 		    "bus_dma_unbindhdl");
27810Sstevel@tonic-gate 	}
27820Sstevel@tonic-gate 
27830Sstevel@tonic-gate 	if (ddi_dma_flush == b->bus_dma_flush) {
27840Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_flush = pdevi->devi_bus_dma_flush;
27850Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_flush,
27860Sstevel@tonic-gate 		    "bus_dma_flush");
27870Sstevel@tonic-gate 	}
27880Sstevel@tonic-gate 
27890Sstevel@tonic-gate 	if (ddi_dma_win == b->bus_dma_win) {
27900Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_win = pdevi->devi_bus_dma_win;
27910Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_win,
27920Sstevel@tonic-gate 		    "bus_dma_win");
27930Sstevel@tonic-gate 	}
27940Sstevel@tonic-gate 
27950Sstevel@tonic-gate 	if (ddi_dma_mctl == b->bus_dma_ctl) {
27960Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_ctl = pdevi->devi_bus_dma_ctl;
27970Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_ctl, "bus_dma_ctl");
27980Sstevel@tonic-gate 	}
27990Sstevel@tonic-gate 
28000Sstevel@tonic-gate 	if (ddi_ctlops == b->bus_ctl) {
28010Sstevel@tonic-gate 		DEVI(devi)->devi_bus_ctl = pdevi->devi_bus_ctl;
28020Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_ctl, "bus_ctl");
28030Sstevel@tonic-gate 	}
28040Sstevel@tonic-gate }
28050Sstevel@tonic-gate 
28060Sstevel@tonic-gate #define	MIN_DEVINFO_LOG_SIZE	max_ncpus
28070Sstevel@tonic-gate #define	MAX_DEVINFO_LOG_SIZE	max_ncpus * 10
28080Sstevel@tonic-gate 
28090Sstevel@tonic-gate static void
28100Sstevel@tonic-gate da_log_init()
28110Sstevel@tonic-gate {
28120Sstevel@tonic-gate 	devinfo_log_header_t *dh;
28130Sstevel@tonic-gate 	int logsize = devinfo_log_size;
28140Sstevel@tonic-gate 
28150Sstevel@tonic-gate 	if (logsize == 0)
28160Sstevel@tonic-gate 		logsize = MIN_DEVINFO_LOG_SIZE;
28170Sstevel@tonic-gate 	else if (logsize > MAX_DEVINFO_LOG_SIZE)
28180Sstevel@tonic-gate 		logsize = MAX_DEVINFO_LOG_SIZE;
28190Sstevel@tonic-gate 
28200Sstevel@tonic-gate 	dh = kmem_alloc(logsize * PAGESIZE, KM_SLEEP);
28210Sstevel@tonic-gate 	mutex_init(&dh->dh_lock, NULL, MUTEX_DEFAULT, NULL);
28220Sstevel@tonic-gate 	dh->dh_max = ((logsize * PAGESIZE) - sizeof (*dh)) /
28230Sstevel@tonic-gate 	    sizeof (devinfo_audit_t) + 1;
28240Sstevel@tonic-gate 	dh->dh_curr = -1;
28250Sstevel@tonic-gate 	dh->dh_hits = 0;
28260Sstevel@tonic-gate 
28270Sstevel@tonic-gate 	devinfo_audit_log = dh;
28280Sstevel@tonic-gate }
28290Sstevel@tonic-gate 
28300Sstevel@tonic-gate /*
28310Sstevel@tonic-gate  * Log the stack trace in per-devinfo audit structure and also enter
28320Sstevel@tonic-gate  * it into a system wide log for recording the time history.
28330Sstevel@tonic-gate  */
28340Sstevel@tonic-gate static void
28350Sstevel@tonic-gate da_log_enter(dev_info_t *dip)
28360Sstevel@tonic-gate {
28370Sstevel@tonic-gate 	devinfo_audit_t *da_log, *da = DEVI(dip)->devi_audit;
28380Sstevel@tonic-gate 	devinfo_log_header_t *dh = devinfo_audit_log;
28390Sstevel@tonic-gate 
28400Sstevel@tonic-gate 	if (devinfo_audit_log == NULL)
28410Sstevel@tonic-gate 		return;
28420Sstevel@tonic-gate 
28430Sstevel@tonic-gate 	ASSERT(da != NULL);
28440Sstevel@tonic-gate 
28450Sstevel@tonic-gate 	da->da_devinfo = dip;
28460Sstevel@tonic-gate 	da->da_timestamp = gethrtime();
28470Sstevel@tonic-gate 	da->da_thread = curthread;
28480Sstevel@tonic-gate 	da->da_node_state = DEVI(dip)->devi_node_state;
28490Sstevel@tonic-gate 	da->da_device_state = DEVI(dip)->devi_state;
28500Sstevel@tonic-gate 	da->da_depth = getpcstack(da->da_stack, DDI_STACK_DEPTH);
28510Sstevel@tonic-gate 
28520Sstevel@tonic-gate 	/*
28530Sstevel@tonic-gate 	 * Copy into common log and note the location for tracing history
28540Sstevel@tonic-gate 	 */
28550Sstevel@tonic-gate 	mutex_enter(&dh->dh_lock);
28560Sstevel@tonic-gate 	dh->dh_hits++;
28570Sstevel@tonic-gate 	dh->dh_curr++;
28580Sstevel@tonic-gate 	if (dh->dh_curr >= dh->dh_max)
28590Sstevel@tonic-gate 		dh->dh_curr -= dh->dh_max;
28600Sstevel@tonic-gate 	da_log = &dh->dh_entry[dh->dh_curr];
28610Sstevel@tonic-gate 	mutex_exit(&dh->dh_lock);
28620Sstevel@tonic-gate 
28630Sstevel@tonic-gate 	bcopy(da, da_log, sizeof (devinfo_audit_t));
28640Sstevel@tonic-gate 	da->da_lastlog = da_log;
28650Sstevel@tonic-gate }
28660Sstevel@tonic-gate 
28670Sstevel@tonic-gate static void
28680Sstevel@tonic-gate attach_drivers()
28690Sstevel@tonic-gate {
28700Sstevel@tonic-gate 	int i;
28710Sstevel@tonic-gate 	for (i = 0; i < devcnt; i++) {
28720Sstevel@tonic-gate 		struct devnames *dnp = &devnamesp[i];
28730Sstevel@tonic-gate 		if ((dnp->dn_flags & DN_FORCE_ATTACH) &&
28740Sstevel@tonic-gate 		    (ddi_hold_installed_driver((major_t)i) != NULL))
28750Sstevel@tonic-gate 			ddi_rele_driver((major_t)i);
28760Sstevel@tonic-gate 	}
28770Sstevel@tonic-gate }
28780Sstevel@tonic-gate 
28790Sstevel@tonic-gate /*
28800Sstevel@tonic-gate  * Launch a thread to force attach drivers. This avoids penalty on boot time.
28810Sstevel@tonic-gate  */
28820Sstevel@tonic-gate void
28830Sstevel@tonic-gate i_ddi_forceattach_drivers()
28840Sstevel@tonic-gate {
28850Sstevel@tonic-gate 	/*
28860Sstevel@tonic-gate 	 * On i386, the USB drivers need to load and take over from the
28870Sstevel@tonic-gate 	 * SMM BIOS drivers ASAP after consconfig(), so make sure they
28880Sstevel@tonic-gate 	 * get loaded right here rather than letting the thread do it.
28890Sstevel@tonic-gate 	 *
28900Sstevel@tonic-gate 	 * The order here is important.  EHCI must be loaded first, as
28910Sstevel@tonic-gate 	 * we have observed many systems on which hangs occur if the
28920Sstevel@tonic-gate 	 * {U,O}HCI companion controllers take over control from the BIOS
28930Sstevel@tonic-gate 	 * before EHCI does.  These hangs are also caused by BIOSes leaving
28940Sstevel@tonic-gate 	 * interrupt-on-port-change enabled in the ehci controller, so that
28950Sstevel@tonic-gate 	 * when uhci/ohci reset themselves, it induces a port change on
28960Sstevel@tonic-gate 	 * the ehci companion controller.  Since there's no interrupt handler
28970Sstevel@tonic-gate 	 * installed at the time, the moment that interrupt is unmasked, an
28980Sstevel@tonic-gate 	 * interrupt storm will occur.  All this is averted when ehci is
28990Sstevel@tonic-gate 	 * loaded first.  And now you know..... the REST of the story.
29000Sstevel@tonic-gate 	 *
29010Sstevel@tonic-gate 	 * Regardless of platform, ehci needs to initialize first to avoid
29020Sstevel@tonic-gate 	 * unnecessary connects and disconnects on the companion controller
29030Sstevel@tonic-gate 	 * when ehci sets up the routing.
29040Sstevel@tonic-gate 	 */
29050Sstevel@tonic-gate 	(void) ddi_hold_installed_driver(ddi_name_to_major("ehci"));
29060Sstevel@tonic-gate 	(void) ddi_hold_installed_driver(ddi_name_to_major("uhci"));
29070Sstevel@tonic-gate 	(void) ddi_hold_installed_driver(ddi_name_to_major("ohci"));
29080Sstevel@tonic-gate 
29090Sstevel@tonic-gate 	(void) thread_create(NULL, 0, (void (*)())attach_drivers, NULL, 0, &p0,
29100Sstevel@tonic-gate 	    TS_RUN, minclsyspri);
29110Sstevel@tonic-gate }
29120Sstevel@tonic-gate 
29130Sstevel@tonic-gate /*
29140Sstevel@tonic-gate  * This is a private DDI interface for optimizing boot performance.
29150Sstevel@tonic-gate  * I/O subsystem initialization is considered complete when devfsadm
29160Sstevel@tonic-gate  * is executed.
29170Sstevel@tonic-gate  *
29180Sstevel@tonic-gate  * NOTE: The start of syseventd in S60devfsadm happen to be convenient
29190Sstevel@tonic-gate  *	indicator for the completion of I/O initialization during boot.
29200Sstevel@tonic-gate  *	The implementation should be replaced by something more robust.
29210Sstevel@tonic-gate  */
29220Sstevel@tonic-gate int
29230Sstevel@tonic-gate i_ddi_io_initialized()
29240Sstevel@tonic-gate {
29250Sstevel@tonic-gate 	extern int sysevent_daemon_init;
29260Sstevel@tonic-gate 	return (sysevent_daemon_init);
29270Sstevel@tonic-gate }
29280Sstevel@tonic-gate 
29290Sstevel@tonic-gate 
29300Sstevel@tonic-gate /*
29310Sstevel@tonic-gate  * device tree walking
29320Sstevel@tonic-gate  */
29330Sstevel@tonic-gate 
29340Sstevel@tonic-gate struct walk_elem {
29350Sstevel@tonic-gate 	struct walk_elem *next;
29360Sstevel@tonic-gate 	dev_info_t *dip;
29370Sstevel@tonic-gate };
29380Sstevel@tonic-gate 
29390Sstevel@tonic-gate static void
29400Sstevel@tonic-gate free_list(struct walk_elem *list)
29410Sstevel@tonic-gate {
29420Sstevel@tonic-gate 	while (list) {
29430Sstevel@tonic-gate 		struct walk_elem *next = list->next;
29440Sstevel@tonic-gate 		kmem_free(list, sizeof (*list));
29450Sstevel@tonic-gate 		list = next;
29460Sstevel@tonic-gate 	}
29470Sstevel@tonic-gate }
29480Sstevel@tonic-gate 
29490Sstevel@tonic-gate static void
29500Sstevel@tonic-gate append_node(struct walk_elem **list, dev_info_t *dip)
29510Sstevel@tonic-gate {
29520Sstevel@tonic-gate 	struct walk_elem *tail;
29530Sstevel@tonic-gate 	struct walk_elem *elem = kmem_alloc(sizeof (*elem), KM_SLEEP);
29540Sstevel@tonic-gate 
29550Sstevel@tonic-gate 	elem->next = NULL;
29560Sstevel@tonic-gate 	elem->dip = dip;
29570Sstevel@tonic-gate 
29580Sstevel@tonic-gate 	if (*list == NULL) {
29590Sstevel@tonic-gate 		*list = elem;
29600Sstevel@tonic-gate 		return;
29610Sstevel@tonic-gate 	}
29620Sstevel@tonic-gate 
29630Sstevel@tonic-gate 	tail = *list;
29640Sstevel@tonic-gate 	while (tail->next)
29650Sstevel@tonic-gate 		tail = tail->next;
29660Sstevel@tonic-gate 
29670Sstevel@tonic-gate 	tail->next = elem;
29680Sstevel@tonic-gate }
29690Sstevel@tonic-gate 
29700Sstevel@tonic-gate /*
29710Sstevel@tonic-gate  * The implementation of ddi_walk_devs().
29720Sstevel@tonic-gate  */
29730Sstevel@tonic-gate static int
29740Sstevel@tonic-gate walk_devs(dev_info_t *dip, int (*f)(dev_info_t *, void *), void *arg,
29750Sstevel@tonic-gate     int do_locking)
29760Sstevel@tonic-gate {
29770Sstevel@tonic-gate 	struct walk_elem *head = NULL;
29780Sstevel@tonic-gate 
29790Sstevel@tonic-gate 	/*
29800Sstevel@tonic-gate 	 * Do it in two passes. First pass invoke callback on each
29810Sstevel@tonic-gate 	 * dip on the sibling list. Second pass invoke callback on
29820Sstevel@tonic-gate 	 * children of each dip.
29830Sstevel@tonic-gate 	 */
29840Sstevel@tonic-gate 	while (dip) {
29850Sstevel@tonic-gate 		switch ((*f)(dip, arg)) {
29860Sstevel@tonic-gate 		case DDI_WALK_TERMINATE:
29870Sstevel@tonic-gate 			free_list(head);
29880Sstevel@tonic-gate 			return (DDI_WALK_TERMINATE);
29890Sstevel@tonic-gate 
29900Sstevel@tonic-gate 		case DDI_WALK_PRUNESIB:
29910Sstevel@tonic-gate 			/* ignore sibling by setting dip to NULL */
29920Sstevel@tonic-gate 			append_node(&head, dip);
29930Sstevel@tonic-gate 			dip = NULL;
29940Sstevel@tonic-gate 			break;
29950Sstevel@tonic-gate 
29960Sstevel@tonic-gate 		case DDI_WALK_PRUNECHILD:
29970Sstevel@tonic-gate 			/* don't worry about children */
29980Sstevel@tonic-gate 			dip = ddi_get_next_sibling(dip);
29990Sstevel@tonic-gate 			break;
30000Sstevel@tonic-gate 
30010Sstevel@tonic-gate 		case DDI_WALK_CONTINUE:
30020Sstevel@tonic-gate 		default:
30030Sstevel@tonic-gate 			append_node(&head, dip);
30040Sstevel@tonic-gate 			dip = ddi_get_next_sibling(dip);
30050Sstevel@tonic-gate 			break;
30060Sstevel@tonic-gate 		}
30070Sstevel@tonic-gate 
30080Sstevel@tonic-gate 	}
30090Sstevel@tonic-gate 
30100Sstevel@tonic-gate 	/* second pass */
30110Sstevel@tonic-gate 	while (head) {
30120Sstevel@tonic-gate 		int circ;
30130Sstevel@tonic-gate 		struct walk_elem *next = head->next;
30140Sstevel@tonic-gate 
30150Sstevel@tonic-gate 		if (do_locking)
30160Sstevel@tonic-gate 			ndi_devi_enter(head->dip, &circ);
30170Sstevel@tonic-gate 		if (walk_devs(ddi_get_child(head->dip), f, arg, do_locking) ==
30180Sstevel@tonic-gate 		    DDI_WALK_TERMINATE) {
30190Sstevel@tonic-gate 			if (do_locking)
30200Sstevel@tonic-gate 				ndi_devi_exit(head->dip, circ);
30210Sstevel@tonic-gate 			free_list(head);
30220Sstevel@tonic-gate 			return (DDI_WALK_TERMINATE);
30230Sstevel@tonic-gate 		}
30240Sstevel@tonic-gate 		if (do_locking)
30250Sstevel@tonic-gate 			ndi_devi_exit(head->dip, circ);
30260Sstevel@tonic-gate 		kmem_free(head, sizeof (*head));
30270Sstevel@tonic-gate 		head = next;
30280Sstevel@tonic-gate 	}
30290Sstevel@tonic-gate 
30300Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
30310Sstevel@tonic-gate }
30320Sstevel@tonic-gate 
30330Sstevel@tonic-gate /*
30340Sstevel@tonic-gate  * This general-purpose routine traverses the tree of dev_info nodes,
30350Sstevel@tonic-gate  * starting from the given node, and calls the given function for each
30360Sstevel@tonic-gate  * node that it finds with the current node and the pointer arg (which
30370Sstevel@tonic-gate  * can point to a structure of information that the function
30380Sstevel@tonic-gate  * needs) as arguments.
30390Sstevel@tonic-gate  *
30400Sstevel@tonic-gate  * It does the walk a layer at a time, not depth-first. The given function
30410Sstevel@tonic-gate  * must return one of the following values:
30420Sstevel@tonic-gate  *	DDI_WALK_CONTINUE
30430Sstevel@tonic-gate  *	DDI_WALK_PRUNESIB
30440Sstevel@tonic-gate  *	DDI_WALK_PRUNECHILD
30450Sstevel@tonic-gate  *	DDI_WALK_TERMINATE
30460Sstevel@tonic-gate  *
30470Sstevel@tonic-gate  * N.B. Since we walk the sibling list, the caller must ensure that
30480Sstevel@tonic-gate  *	the parent of dip is held against changes, unless the parent
30490Sstevel@tonic-gate  *	is rootnode.  ndi_devi_enter() on the parent is sufficient.
30500Sstevel@tonic-gate  *
30510Sstevel@tonic-gate  *	To avoid deadlock situations, caller must not attempt to
30520Sstevel@tonic-gate  *	configure/unconfigure/remove device node in (*f)(), nor should
30530Sstevel@tonic-gate  *	it attempt to recurse on other nodes in the system.
30540Sstevel@tonic-gate  *
30550Sstevel@tonic-gate  *	This is not callable from device autoconfiguration routines.
30560Sstevel@tonic-gate  *	They include, but not limited to, _init(9e), _fini(9e), probe(9e),
30570Sstevel@tonic-gate  *	attach(9e), and detach(9e).
30580Sstevel@tonic-gate  */
30590Sstevel@tonic-gate 
30600Sstevel@tonic-gate void
30610Sstevel@tonic-gate ddi_walk_devs(dev_info_t *dip, int (*f)(dev_info_t *, void *), void *arg)
30620Sstevel@tonic-gate {
30630Sstevel@tonic-gate 
30640Sstevel@tonic-gate 	ASSERT(dip == NULL || ddi_get_parent(dip) == NULL ||
30650Sstevel@tonic-gate 		DEVI_BUSY_OWNED(ddi_get_parent(dip)));
30660Sstevel@tonic-gate 
30670Sstevel@tonic-gate 	(void) walk_devs(dip, f, arg, 1);
30680Sstevel@tonic-gate }
30690Sstevel@tonic-gate 
30700Sstevel@tonic-gate /*
30710Sstevel@tonic-gate  * This is a general-purpose routine traverses the per-driver list
30720Sstevel@tonic-gate  * and calls the given function for each node. must return one of
30730Sstevel@tonic-gate  * the following values:
30740Sstevel@tonic-gate  *	DDI_WALK_CONTINUE
30750Sstevel@tonic-gate  *	DDI_WALK_TERMINATE
30760Sstevel@tonic-gate  *
30770Sstevel@tonic-gate  * N.B. The same restrictions from ddi_walk_devs() apply.
30780Sstevel@tonic-gate  */
30790Sstevel@tonic-gate 
30800Sstevel@tonic-gate void
30810Sstevel@tonic-gate e_ddi_walk_driver(char *drv, int (*f)(dev_info_t *, void *), void *arg)
30820Sstevel@tonic-gate {
30830Sstevel@tonic-gate 	major_t major;
30840Sstevel@tonic-gate 	struct devnames *dnp;
30850Sstevel@tonic-gate 	dev_info_t *dip;
30860Sstevel@tonic-gate 
30870Sstevel@tonic-gate 	major = ddi_name_to_major(drv);
30880Sstevel@tonic-gate 	if (major == (major_t)-1)
30890Sstevel@tonic-gate 		return;
30900Sstevel@tonic-gate 
30910Sstevel@tonic-gate 	dnp = &devnamesp[major];
30920Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
30930Sstevel@tonic-gate 	dip = dnp->dn_head;
30940Sstevel@tonic-gate 	while (dip) {
30950Sstevel@tonic-gate 		ndi_hold_devi(dip);
30960Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
30970Sstevel@tonic-gate 		if ((*f)(dip, arg) == DDI_WALK_TERMINATE) {
30980Sstevel@tonic-gate 			ndi_rele_devi(dip);
30990Sstevel@tonic-gate 			return;
31000Sstevel@tonic-gate 		}
31010Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
31020Sstevel@tonic-gate 		ndi_rele_devi(dip);
31030Sstevel@tonic-gate 		dip = ddi_get_next(dip);
31040Sstevel@tonic-gate 	}
31050Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
31060Sstevel@tonic-gate }
31070Sstevel@tonic-gate 
31080Sstevel@tonic-gate /*
31090Sstevel@tonic-gate  * argument to i_find_devi, a devinfo node search callback function.
31100Sstevel@tonic-gate  */
31110Sstevel@tonic-gate struct match_info {
31120Sstevel@tonic-gate 	dev_info_t	*dip;		/* result */
31130Sstevel@tonic-gate 	char		*nodename;	/* if non-null, nodename must match */
31140Sstevel@tonic-gate 	int		instance;	/* if != -1, instance must match */
31150Sstevel@tonic-gate 	int		attached;	/* if != 0, state >= DS_ATTACHED */
31160Sstevel@tonic-gate };
31170Sstevel@tonic-gate 
31180Sstevel@tonic-gate static int
31190Sstevel@tonic-gate i_find_devi(dev_info_t *dip, void *arg)
31200Sstevel@tonic-gate {
31210Sstevel@tonic-gate 	struct match_info *info = (struct match_info *)arg;
31220Sstevel@tonic-gate 
31230Sstevel@tonic-gate 	if (((info->nodename == NULL) ||
31240Sstevel@tonic-gate 		(strcmp(ddi_node_name(dip), info->nodename) == 0)) &&
31250Sstevel@tonic-gate 	    ((info->instance == -1) ||
31260Sstevel@tonic-gate 		(ddi_get_instance(dip) == info->instance)) &&
31270Sstevel@tonic-gate 	    ((info->attached == 0) ||
31280Sstevel@tonic-gate 		(i_ddi_node_state(dip) >= DS_ATTACHED))) {
31290Sstevel@tonic-gate 		info->dip = dip;
31300Sstevel@tonic-gate 		ndi_hold_devi(dip);
31310Sstevel@tonic-gate 		return (DDI_WALK_TERMINATE);
31320Sstevel@tonic-gate 	}
31330Sstevel@tonic-gate 
31340Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
31350Sstevel@tonic-gate }
31360Sstevel@tonic-gate 
31370Sstevel@tonic-gate /*
31380Sstevel@tonic-gate  * Find dip with a known node name and instance and return with it held
31390Sstevel@tonic-gate  */
31400Sstevel@tonic-gate dev_info_t *
31410Sstevel@tonic-gate ddi_find_devinfo(char *nodename, int instance, int attached)
31420Sstevel@tonic-gate {
31430Sstevel@tonic-gate 	struct match_info	info;
31440Sstevel@tonic-gate 
31450Sstevel@tonic-gate 	info.nodename = nodename;
31460Sstevel@tonic-gate 	info.instance = instance;
31470Sstevel@tonic-gate 	info.attached = attached;
31480Sstevel@tonic-gate 	info.dip = NULL;
31490Sstevel@tonic-gate 
31500Sstevel@tonic-gate 	ddi_walk_devs(ddi_root_node(), i_find_devi, &info);
31510Sstevel@tonic-gate 	return (info.dip);
31520Sstevel@tonic-gate }
31530Sstevel@tonic-gate 
31540Sstevel@tonic-gate /*
31550Sstevel@tonic-gate  * Parse for name, addr, and minor names. Some args may be NULL.
31560Sstevel@tonic-gate  */
31570Sstevel@tonic-gate void
31580Sstevel@tonic-gate i_ddi_parse_name(char *name, char **nodename, char **addrname, char **minorname)
31590Sstevel@tonic-gate {
31600Sstevel@tonic-gate 	char *cp;
31610Sstevel@tonic-gate 	static char nulladdrname[] = "";
31620Sstevel@tonic-gate 
31630Sstevel@tonic-gate 	/* default values */
31640Sstevel@tonic-gate 	if (nodename)
31650Sstevel@tonic-gate 		*nodename = name;
31660Sstevel@tonic-gate 	if (addrname)
31670Sstevel@tonic-gate 		*addrname = nulladdrname;
31680Sstevel@tonic-gate 	if (minorname)
31690Sstevel@tonic-gate 		*minorname = NULL;
31700Sstevel@tonic-gate 
31710Sstevel@tonic-gate 	cp = name;
31720Sstevel@tonic-gate 	while (*cp != '\0') {
31730Sstevel@tonic-gate 		if (addrname && *cp == '@') {
31740Sstevel@tonic-gate 			*addrname = cp + 1;
31750Sstevel@tonic-gate 			*cp = '\0';
31760Sstevel@tonic-gate 		} else if (minorname && *cp == ':') {
31770Sstevel@tonic-gate 			*minorname = cp + 1;
31780Sstevel@tonic-gate 			*cp = '\0';
31790Sstevel@tonic-gate 		}
31800Sstevel@tonic-gate 		++cp;
31810Sstevel@tonic-gate 	}
31820Sstevel@tonic-gate }
31830Sstevel@tonic-gate 
31840Sstevel@tonic-gate static char *
31850Sstevel@tonic-gate child_path_to_driver(dev_info_t *parent, char *child_name, char *unit_address)
31860Sstevel@tonic-gate {
31870Sstevel@tonic-gate 	char *p, *drvname = NULL;
31880Sstevel@tonic-gate 	major_t maj;
31890Sstevel@tonic-gate 
31900Sstevel@tonic-gate 	/*
31910Sstevel@tonic-gate 	 * Construct the pathname and ask the implementation
31920Sstevel@tonic-gate 	 * if it can do a driver = f(pathname) for us, if not
31930Sstevel@tonic-gate 	 * we'll just default to using the node-name that
31940Sstevel@tonic-gate 	 * was given to us.  We want to do this first to
31950Sstevel@tonic-gate 	 * allow the platform to use 'generic' names for
31960Sstevel@tonic-gate 	 * legacy device drivers.
31970Sstevel@tonic-gate 	 */
31980Sstevel@tonic-gate 	p = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
31990Sstevel@tonic-gate 	(void) ddi_pathname(parent, p);
32000Sstevel@tonic-gate 	(void) strcat(p, "/");
32010Sstevel@tonic-gate 	(void) strcat(p, child_name);
32020Sstevel@tonic-gate 	if (unit_address && *unit_address) {
32030Sstevel@tonic-gate 		(void) strcat(p, "@");
32040Sstevel@tonic-gate 		(void) strcat(p, unit_address);
32050Sstevel@tonic-gate 	}
32060Sstevel@tonic-gate 
32070Sstevel@tonic-gate 	/*
32080Sstevel@tonic-gate 	 * Get the binding. If there is none, return the child_name
32090Sstevel@tonic-gate 	 * and let the caller deal with it.
32100Sstevel@tonic-gate 	 */
32110Sstevel@tonic-gate 	maj = path_to_major(p);
32120Sstevel@tonic-gate 
32130Sstevel@tonic-gate 	kmem_free(p, MAXPATHLEN);
32140Sstevel@tonic-gate 
32150Sstevel@tonic-gate 	if (maj != (major_t)-1)
32160Sstevel@tonic-gate 		drvname = ddi_major_to_name(maj);
32170Sstevel@tonic-gate 	if (drvname == NULL)
32180Sstevel@tonic-gate 		drvname = child_name;
32190Sstevel@tonic-gate 
32200Sstevel@tonic-gate 	return (drvname);
32210Sstevel@tonic-gate }
32220Sstevel@tonic-gate 
32230Sstevel@tonic-gate 
32240Sstevel@tonic-gate /*
32250Sstevel@tonic-gate  * Given the pathname of a device, fill in the dev_info_t value and/or the
32260Sstevel@tonic-gate  * dev_t value and/or the spectype, depending on which parameters are non-NULL.
32270Sstevel@tonic-gate  * If there is an error, this function returns -1.
32280Sstevel@tonic-gate  *
32290Sstevel@tonic-gate  * NOTE: If this function returns the dev_info_t structure, then it
32300Sstevel@tonic-gate  * does so with a hold on the devi. Caller should ensure that they get
32310Sstevel@tonic-gate  * decremented via ddi_release_devi() or ndi_rele_devi();
32320Sstevel@tonic-gate  *
32330Sstevel@tonic-gate  * This function can be invoked in the boot case for a pathname without
32340Sstevel@tonic-gate  * device argument (:xxxx), traditionally treated as a minor name.
32350Sstevel@tonic-gate  * In this case, we do the following
32360Sstevel@tonic-gate  * (1) search the minor node of type DDM_DEFAULT.
32370Sstevel@tonic-gate  * (2) if no DDM_DEFAULT minor exists, then the first non-alias minor is chosen.
32380Sstevel@tonic-gate  * (3) if neither exists, a dev_t is faked with minor number = instance.
32390Sstevel@tonic-gate  * As of S9 FCS, no instance of #1 exists. #2 is used by several platforms
32400Sstevel@tonic-gate  * to default the boot partition to :a possibly by other OBP definitions.
32410Sstevel@tonic-gate  * #3 is used for booting off network interfaces, most SPARC network
32420Sstevel@tonic-gate  * drivers support Style-2 only, so only DDM_ALIAS minor exists.
32430Sstevel@tonic-gate  *
32440Sstevel@tonic-gate  * It is possible for OBP to present device args at the end of the path as
32450Sstevel@tonic-gate  * well as in the middle. For example, with IB the following strings are
32460Sstevel@tonic-gate  * valid boot paths.
32470Sstevel@tonic-gate  *	a /pci@8,700000/ib@1,2:port=1,pkey=ff,dhcp,...
32480Sstevel@tonic-gate  *	b /pci@8,700000/ib@1,1:port=1/ioc@xxxxxx,yyyyyyy:dhcp
32490Sstevel@tonic-gate  * Case (a), we first look for minor node "port=1,pkey...".
32500Sstevel@tonic-gate  * Failing that, we will pass "port=1,pkey..." to the bus_config
32510Sstevel@tonic-gate  * entry point of ib (HCA) driver.
32520Sstevel@tonic-gate  * Case (b), configure ib@1,1 as usual. Then invoke ib's bus_config
32530Sstevel@tonic-gate  * with argument "ioc@xxxxxxx,yyyyyyy:port=1". After configuring
32540Sstevel@tonic-gate  * the ioc, look for minor node dhcp. If not found, pass ":dhcp"
32550Sstevel@tonic-gate  * to ioc's bus_config entry point.
32560Sstevel@tonic-gate  */
32570Sstevel@tonic-gate int
32580Sstevel@tonic-gate resolve_pathname(char *pathname,
32590Sstevel@tonic-gate 	dev_info_t **dipp, dev_t *devtp, int *spectypep)
32600Sstevel@tonic-gate {
32610Sstevel@tonic-gate 	int error;
32620Sstevel@tonic-gate 	dev_info_t *parent, *child;
32630Sstevel@tonic-gate 	struct pathname pn;
32640Sstevel@tonic-gate 	char *component, *config_name;
32650Sstevel@tonic-gate 	char *minorname = NULL;
32660Sstevel@tonic-gate 	char *prev_minor = NULL;
32670Sstevel@tonic-gate 	dev_t devt = NODEV;
32680Sstevel@tonic-gate 	int spectype;
32690Sstevel@tonic-gate 	struct ddi_minor_data *dmn;
32700Sstevel@tonic-gate 
32710Sstevel@tonic-gate 	if (*pathname != '/')
32720Sstevel@tonic-gate 		return (EINVAL);
32730Sstevel@tonic-gate 	parent = ddi_root_node();	/* Begin at the top of the tree */
32740Sstevel@tonic-gate 
32750Sstevel@tonic-gate 	if (error = pn_get(pathname, UIO_SYSSPACE, &pn))
32760Sstevel@tonic-gate 		return (error);
32770Sstevel@tonic-gate 	pn_skipslash(&pn);
32780Sstevel@tonic-gate 
32790Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(parent) >= DS_ATTACHED);
32800Sstevel@tonic-gate 	ndi_hold_devi(parent);
32810Sstevel@tonic-gate 
32820Sstevel@tonic-gate 	component = kmem_alloc(MAXNAMELEN, KM_SLEEP);
32830Sstevel@tonic-gate 	config_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
32840Sstevel@tonic-gate 
32850Sstevel@tonic-gate 	while (pn_pathleft(&pn)) {
32860Sstevel@tonic-gate 		/* remember prev minor (:xxx) in the middle of path */
32870Sstevel@tonic-gate 		if (minorname)
32880Sstevel@tonic-gate 			prev_minor = i_ddi_strdup(minorname, KM_SLEEP);
32890Sstevel@tonic-gate 
32900Sstevel@tonic-gate 		/* Get component and chop off minorname */
32910Sstevel@tonic-gate 		(void) pn_getcomponent(&pn, component);
32920Sstevel@tonic-gate 		i_ddi_parse_name(component, NULL, NULL, &minorname);
32930Sstevel@tonic-gate 
32940Sstevel@tonic-gate 		if (prev_minor == NULL) {
32950Sstevel@tonic-gate 			(void) snprintf(config_name, MAXNAMELEN, "%s",
32960Sstevel@tonic-gate 			    component);
32970Sstevel@tonic-gate 		} else {
32980Sstevel@tonic-gate 			(void) snprintf(config_name, MAXNAMELEN, "%s:%s",
32990Sstevel@tonic-gate 			    component, prev_minor);
33000Sstevel@tonic-gate 			kmem_free(prev_minor, strlen(prev_minor) + 1);
33010Sstevel@tonic-gate 			prev_minor = NULL;
33020Sstevel@tonic-gate 		}
33030Sstevel@tonic-gate 
33040Sstevel@tonic-gate 		/*
33050Sstevel@tonic-gate 		 * Find and configure the child
33060Sstevel@tonic-gate 		 */
33070Sstevel@tonic-gate 		if (ndi_devi_config_one(parent, config_name, &child,
33080Sstevel@tonic-gate 		    NDI_PROMNAME | NDI_NO_EVENT) != NDI_SUCCESS) {
33090Sstevel@tonic-gate 			ndi_rele_devi(parent);
33100Sstevel@tonic-gate 			pn_free(&pn);
33110Sstevel@tonic-gate 			kmem_free(component, MAXNAMELEN);
33120Sstevel@tonic-gate 			kmem_free(config_name, MAXNAMELEN);
33130Sstevel@tonic-gate 			return (-1);
33140Sstevel@tonic-gate 		}
33150Sstevel@tonic-gate 
33160Sstevel@tonic-gate 		ASSERT(i_ddi_node_state(child) >= DS_ATTACHED);
33170Sstevel@tonic-gate 		ndi_rele_devi(parent);
33180Sstevel@tonic-gate 		parent = child;
33190Sstevel@tonic-gate 		pn_skipslash(&pn);
33200Sstevel@tonic-gate 	}
33210Sstevel@tonic-gate 
33220Sstevel@tonic-gate 	/*
33230Sstevel@tonic-gate 	 * First look for a minor node matching minorname.
33240Sstevel@tonic-gate 	 * Failing that, try to pass minorname to bus_config().
33250Sstevel@tonic-gate 	 */
33260Sstevel@tonic-gate 	if (minorname && i_ddi_minorname_to_devtspectype(parent,
33270Sstevel@tonic-gate 	    minorname, &devt, &spectype) == DDI_FAILURE) {
33280Sstevel@tonic-gate 		(void) snprintf(config_name, MAXNAMELEN, "%s", minorname);
33290Sstevel@tonic-gate 		if (ndi_devi_config_obp_args(parent,
33300Sstevel@tonic-gate 		    config_name, &child, 0) != NDI_SUCCESS) {
33310Sstevel@tonic-gate 			ndi_rele_devi(parent);
33320Sstevel@tonic-gate 			pn_free(&pn);
33330Sstevel@tonic-gate 			kmem_free(component, MAXNAMELEN);
33340Sstevel@tonic-gate 			kmem_free(config_name, MAXNAMELEN);
33350Sstevel@tonic-gate 			NDI_CONFIG_DEBUG((CE_NOTE,
33360Sstevel@tonic-gate 			    "%s: minor node not found\n", pathname));
33370Sstevel@tonic-gate 			return (-1);
33380Sstevel@tonic-gate 		}
33390Sstevel@tonic-gate 		minorname = NULL;	/* look for default minor */
33400Sstevel@tonic-gate 		ASSERT(i_ddi_node_state(child) >= DS_ATTACHED);
33410Sstevel@tonic-gate 		ndi_rele_devi(parent);
33420Sstevel@tonic-gate 		parent = child;
33430Sstevel@tonic-gate 	}
33440Sstevel@tonic-gate 
33450Sstevel@tonic-gate 	if (devtp || spectypep) {
33460Sstevel@tonic-gate 		if (minorname == NULL) {
33470Sstevel@tonic-gate 			/* search for a default entry */
33480Sstevel@tonic-gate 			mutex_enter(&(DEVI(parent)->devi_lock));
33490Sstevel@tonic-gate 			for (dmn = DEVI(parent)->devi_minor; dmn;
33500Sstevel@tonic-gate 			    dmn = dmn->next) {
33510Sstevel@tonic-gate 				if (dmn->type == DDM_DEFAULT) {
33520Sstevel@tonic-gate 					devt = dmn->ddm_dev;
33530Sstevel@tonic-gate 					spectype = dmn->ddm_spec_type;
33540Sstevel@tonic-gate 					break;
33550Sstevel@tonic-gate 				}
33560Sstevel@tonic-gate 			}
33570Sstevel@tonic-gate 
33580Sstevel@tonic-gate 			if (devt == NODEV) {
33590Sstevel@tonic-gate 				/*
33600Sstevel@tonic-gate 				 * No default minor node, try the first one;
33610Sstevel@tonic-gate 				 * else, assume 1-1 instance-minor mapping
33620Sstevel@tonic-gate 				 */
33630Sstevel@tonic-gate 				dmn = DEVI(parent)->devi_minor;
33640Sstevel@tonic-gate 				if (dmn && ((dmn->type == DDM_MINOR) ||
33650Sstevel@tonic-gate 				    (dmn->type == DDM_INTERNAL_PATH))) {
33660Sstevel@tonic-gate 					devt = dmn->ddm_dev;
33670Sstevel@tonic-gate 					spectype = dmn->ddm_spec_type;
33680Sstevel@tonic-gate 				} else {
33690Sstevel@tonic-gate 					devt = makedevice(
33700Sstevel@tonic-gate 					    DEVI(parent)->devi_major,
33710Sstevel@tonic-gate 					    ddi_get_instance(parent));
33720Sstevel@tonic-gate 					spectype = S_IFCHR;
33730Sstevel@tonic-gate 				}
33740Sstevel@tonic-gate 			}
33750Sstevel@tonic-gate 			mutex_exit(&(DEVI(parent)->devi_lock));
33760Sstevel@tonic-gate 		}
33770Sstevel@tonic-gate 		if (devtp)
33780Sstevel@tonic-gate 			*devtp = devt;
33790Sstevel@tonic-gate 		if (spectypep)
33800Sstevel@tonic-gate 			*spectypep = spectype;
33810Sstevel@tonic-gate 	}
33820Sstevel@tonic-gate 
33830Sstevel@tonic-gate 	pn_free(&pn);
33840Sstevel@tonic-gate 	kmem_free(component, MAXNAMELEN);
33850Sstevel@tonic-gate 	kmem_free(config_name, MAXNAMELEN);
33860Sstevel@tonic-gate 
33870Sstevel@tonic-gate 	/*
33880Sstevel@tonic-gate 	 * If there is no error, return the appropriate parameters
33890Sstevel@tonic-gate 	 */
33900Sstevel@tonic-gate 	if (dipp != NULL)
33910Sstevel@tonic-gate 		*dipp = parent;
33920Sstevel@tonic-gate 	else {
33930Sstevel@tonic-gate 		/*
33940Sstevel@tonic-gate 		 * We should really keep the ref count to keep the node from
33950Sstevel@tonic-gate 		 * detaching but ddi_pathname_to_dev_t() specifies a NULL dipp,
33960Sstevel@tonic-gate 		 * so we have no way of passing back the held dip.  Not holding
33970Sstevel@tonic-gate 		 * the dip allows detaches to occur - which can cause problems
33980Sstevel@tonic-gate 		 * for subsystems which call ddi_pathname_to_dev_t (console).
33990Sstevel@tonic-gate 		 *
34000Sstevel@tonic-gate 		 * Instead of holding the dip, we place a ddi-no-autodetach
34010Sstevel@tonic-gate 		 * property on the node to prevent auto detaching.
34020Sstevel@tonic-gate 		 *
34030Sstevel@tonic-gate 		 * The right fix is to remove ddi_pathname_to_dev_t and replace
34040Sstevel@tonic-gate 		 * it, and all references, with a call that specifies a dipp.
34050Sstevel@tonic-gate 		 * In addition, the callers of this new interfaces would then
34060Sstevel@tonic-gate 		 * need to call ndi_rele_devi when the reference is complete.
34070Sstevel@tonic-gate 		 */
34080Sstevel@tonic-gate 		(void) ddi_prop_update_int(DDI_DEV_T_NONE, parent,
34090Sstevel@tonic-gate 		    DDI_NO_AUTODETACH, 1);
34100Sstevel@tonic-gate 		ndi_rele_devi(parent);
34110Sstevel@tonic-gate 	}
34120Sstevel@tonic-gate 
34130Sstevel@tonic-gate 	return (0);
34140Sstevel@tonic-gate }
34150Sstevel@tonic-gate 
34160Sstevel@tonic-gate /*
34170Sstevel@tonic-gate  * Given the pathname of a device, return the dev_t of the corresponding
34180Sstevel@tonic-gate  * device.  Returns NODEV on failure.
34190Sstevel@tonic-gate  *
34200Sstevel@tonic-gate  * Note that this call sets the DDI_NO_AUTODETACH property on the devinfo node.
34210Sstevel@tonic-gate  */
34220Sstevel@tonic-gate dev_t
34230Sstevel@tonic-gate ddi_pathname_to_dev_t(char *pathname)
34240Sstevel@tonic-gate {
34250Sstevel@tonic-gate 	dev_t devt;
34260Sstevel@tonic-gate 	int error;
34270Sstevel@tonic-gate 
34280Sstevel@tonic-gate 	error = resolve_pathname(pathname, NULL, &devt, NULL);
34290Sstevel@tonic-gate 
34300Sstevel@tonic-gate 	return (error ? NODEV : devt);
34310Sstevel@tonic-gate }
34320Sstevel@tonic-gate 
34330Sstevel@tonic-gate /*
34340Sstevel@tonic-gate  * Translate a prom pathname to kernel devfs pathname.
34350Sstevel@tonic-gate  * Caller is assumed to allocate devfspath memory of
34360Sstevel@tonic-gate  * size at least MAXPATHLEN
34370Sstevel@tonic-gate  *
34380Sstevel@tonic-gate  * The prom pathname may not include minor name, but
34390Sstevel@tonic-gate  * devfs pathname has a minor name portion.
34400Sstevel@tonic-gate  */
34410Sstevel@tonic-gate int
34420Sstevel@tonic-gate i_ddi_prompath_to_devfspath(char *prompath, char *devfspath)
34430Sstevel@tonic-gate {
34440Sstevel@tonic-gate 	dev_t		devt = (dev_t)NODEV;
34450Sstevel@tonic-gate 	dev_info_t	*dip = NULL;
34460Sstevel@tonic-gate 	char		*minor_name = NULL;
34470Sstevel@tonic-gate 	int		spectype;
34480Sstevel@tonic-gate 	int		error;
34490Sstevel@tonic-gate 
34500Sstevel@tonic-gate 	error = resolve_pathname(prompath, &dip, &devt, &spectype);
34510Sstevel@tonic-gate 	if (error)
34520Sstevel@tonic-gate 		return (DDI_FAILURE);
34530Sstevel@tonic-gate 	ASSERT(dip && devt != NODEV);
34540Sstevel@tonic-gate 
34550Sstevel@tonic-gate 	/*
34560Sstevel@tonic-gate 	 * Get in-kernel devfs pathname
34570Sstevel@tonic-gate 	 */
34580Sstevel@tonic-gate 	(void) ddi_pathname(dip, devfspath);
34590Sstevel@tonic-gate 
34600Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
34610Sstevel@tonic-gate 	minor_name = i_ddi_devtspectype_to_minorname(dip, devt, spectype);
34620Sstevel@tonic-gate 	if (minor_name) {
34630Sstevel@tonic-gate 		(void) strcat(devfspath, ":");
34640Sstevel@tonic-gate 		(void) strcat(devfspath, minor_name);
34650Sstevel@tonic-gate 	} else {
34660Sstevel@tonic-gate 		/*
34670Sstevel@tonic-gate 		 * If minor_name is NULL, we have an alias minor node.
34680Sstevel@tonic-gate 		 * So manufacture a path to the corresponding clone minor.
34690Sstevel@tonic-gate 		 */
34700Sstevel@tonic-gate 		(void) snprintf(devfspath, MAXPATHLEN, "%s:%s",
34710Sstevel@tonic-gate 		    CLONE_PATH, ddi_driver_name(dip));
34720Sstevel@tonic-gate 	}
34730Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
34740Sstevel@tonic-gate 
34750Sstevel@tonic-gate 	/* release hold from resolve_pathname() */
34760Sstevel@tonic-gate 	ndi_rele_devi(dip);
34770Sstevel@tonic-gate 	return (0);
34780Sstevel@tonic-gate }
34790Sstevel@tonic-gate 
34800Sstevel@tonic-gate /*
34810Sstevel@tonic-gate  * Reset all the pure leaf drivers on the system at halt time
34820Sstevel@tonic-gate  */
34830Sstevel@tonic-gate static int
34840Sstevel@tonic-gate reset_leaf_device(dev_info_t *dip, void *arg)
34850Sstevel@tonic-gate {
34860Sstevel@tonic-gate 	_NOTE(ARGUNUSED(arg))
34870Sstevel@tonic-gate 	struct dev_ops *ops;
34880Sstevel@tonic-gate 
34890Sstevel@tonic-gate 	/* if the device doesn't need to be reset then there's nothing to do */
34900Sstevel@tonic-gate 	if (!DEVI_NEED_RESET(dip))
34910Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
34920Sstevel@tonic-gate 
34930Sstevel@tonic-gate 	/*
34940Sstevel@tonic-gate 	 * if the device isn't a char/block device or doesn't have a
34950Sstevel@tonic-gate 	 * reset entry point then there's nothing to do.
34960Sstevel@tonic-gate 	 */
34970Sstevel@tonic-gate 	ops = ddi_get_driver(dip);
34980Sstevel@tonic-gate 	if ((ops == NULL) || (ops->devo_cb_ops == NULL) ||
34990Sstevel@tonic-gate 	    (ops->devo_reset == nodev) || (ops->devo_reset == nulldev) ||
35000Sstevel@tonic-gate 	    (ops->devo_reset == NULL))
35010Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
35020Sstevel@tonic-gate 
35030Sstevel@tonic-gate 	if (DEVI_IS_ATTACHING(dip) || DEVI_IS_DETACHING(dip)) {
35040Sstevel@tonic-gate 		static char path[MAXPATHLEN];
35050Sstevel@tonic-gate 
35060Sstevel@tonic-gate 		/*
35070Sstevel@tonic-gate 		 * bad news, this device has blocked in it's attach or
35080Sstevel@tonic-gate 		 * detach routine, which means it not safe to call it's
35090Sstevel@tonic-gate 		 * devo_reset() entry point.
35100Sstevel@tonic-gate 		 */
35110Sstevel@tonic-gate 		cmn_err(CE_WARN, "unable to reset device: %s",
35120Sstevel@tonic-gate 		    ddi_pathname(dip, path));
35130Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
35140Sstevel@tonic-gate 	}
35150Sstevel@tonic-gate 
35160Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_NOTE, "resetting %s%d\n",
35170Sstevel@tonic-gate 		ddi_driver_name(dip), ddi_get_instance(dip)));
35180Sstevel@tonic-gate 
35190Sstevel@tonic-gate 	(void) devi_reset(dip, DDI_RESET_FORCE);
35200Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
35210Sstevel@tonic-gate }
35220Sstevel@tonic-gate 
35230Sstevel@tonic-gate void
35240Sstevel@tonic-gate reset_leaves(void)
35250Sstevel@tonic-gate {
35260Sstevel@tonic-gate 	/*
35270Sstevel@tonic-gate 	 * if we're reached here, the device tree better not be changing.
35280Sstevel@tonic-gate 	 * so either devinfo_freeze better be set or we better be panicing.
35290Sstevel@tonic-gate 	 */
35300Sstevel@tonic-gate 	ASSERT(devinfo_freeze || panicstr);
35310Sstevel@tonic-gate 
35320Sstevel@tonic-gate 	(void) walk_devs(top_devinfo, reset_leaf_device, NULL, 0);
35330Sstevel@tonic-gate }
35340Sstevel@tonic-gate 
35350Sstevel@tonic-gate /*
35360Sstevel@tonic-gate  * devtree_freeze() must be called before reset_leaves() during a
35370Sstevel@tonic-gate  * normal system shutdown.  It attempts to ensure that there are no
35380Sstevel@tonic-gate  * outstanding attach or detach operations in progress when reset_leaves()
35390Sstevel@tonic-gate  * is invoked.  It must be called before the system becomes single-threaded
35400Sstevel@tonic-gate  * because device attach and detach are multi-threaded operations.  (note
35410Sstevel@tonic-gate  * that during system shutdown the system doesn't actually become
35420Sstevel@tonic-gate  * single-thread since other threads still exist, but the shutdown thread
35430Sstevel@tonic-gate  * will disable preemption for itself, raise it's pil, and stop all the
35440Sstevel@tonic-gate  * other cpus in the system there by effectively making the system
35450Sstevel@tonic-gate  * single-threaded.)
35460Sstevel@tonic-gate  */
35470Sstevel@tonic-gate void
35480Sstevel@tonic-gate devtree_freeze(void)
35490Sstevel@tonic-gate {
35500Sstevel@tonic-gate 	int delayed = 0;
35510Sstevel@tonic-gate 
35520Sstevel@tonic-gate 	/* if we're panicing then the device tree isn't going to be changing */
35530Sstevel@tonic-gate 	if (panicstr)
35540Sstevel@tonic-gate 		return;
35550Sstevel@tonic-gate 
35560Sstevel@tonic-gate 	/* stop all dev_info state changes in the device tree */
35570Sstevel@tonic-gate 	devinfo_freeze = gethrtime();
35580Sstevel@tonic-gate 
35590Sstevel@tonic-gate 	/*
35600Sstevel@tonic-gate 	 * if we're not panicing and there are on-going attach or detach
35610Sstevel@tonic-gate 	 * operations, wait for up to 3 seconds for them to finish.  This
35620Sstevel@tonic-gate 	 * is a randomly chosen interval but this should be ok because:
35630Sstevel@tonic-gate 	 * - 3 seconds is very small relative to the deadman timer.
35640Sstevel@tonic-gate 	 * - normal attach and detach operations should be very quick.
35650Sstevel@tonic-gate 	 * - attach and detach operations are fairly rare.
35660Sstevel@tonic-gate 	 */
35670Sstevel@tonic-gate 	while (!panicstr && atomic_add_long_nv(&devinfo_attach_detach, 0) &&
35680Sstevel@tonic-gate 	    (delayed < 3)) {
35690Sstevel@tonic-gate 		delayed += 1;
35700Sstevel@tonic-gate 
35710Sstevel@tonic-gate 		/* do a sleeping wait for one second */
35720Sstevel@tonic-gate 		ASSERT(!servicing_interrupt());
35730Sstevel@tonic-gate 		delay(drv_usectohz(MICROSEC));
35740Sstevel@tonic-gate 	}
35750Sstevel@tonic-gate }
35760Sstevel@tonic-gate 
35770Sstevel@tonic-gate static int
35780Sstevel@tonic-gate bind_dip(dev_info_t *dip, void *arg)
35790Sstevel@tonic-gate {
35800Sstevel@tonic-gate 	_NOTE(ARGUNUSED(arg))
35810Sstevel@tonic-gate 	if (i_ddi_node_state(dip) < DS_BOUND)
35820Sstevel@tonic-gate 		(void) ndi_devi_bind_driver(dip, 0);
35830Sstevel@tonic-gate 
35840Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
35850Sstevel@tonic-gate }
35860Sstevel@tonic-gate 
35870Sstevel@tonic-gate void
35880Sstevel@tonic-gate i_ddi_bind_devs(void)
35890Sstevel@tonic-gate {
35900Sstevel@tonic-gate 	ddi_walk_devs(top_devinfo, bind_dip, (void *)NULL);
35910Sstevel@tonic-gate }
35920Sstevel@tonic-gate 
35930Sstevel@tonic-gate static int
35940Sstevel@tonic-gate unbind_children(dev_info_t *dip, void *arg)
35950Sstevel@tonic-gate {
35960Sstevel@tonic-gate 	int circ;
35970Sstevel@tonic-gate 	dev_info_t *cdip;
35980Sstevel@tonic-gate 	major_t major = (major_t)(uintptr_t)arg;
35990Sstevel@tonic-gate 
36000Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
36010Sstevel@tonic-gate 	cdip = ddi_get_child(dip);
36020Sstevel@tonic-gate 	/*
36030Sstevel@tonic-gate 	 * We are called either from rem_drv or update_drv.
36040Sstevel@tonic-gate 	 * In both cases, we unbind persistent nodes and destroy
36050Sstevel@tonic-gate 	 * .conf nodes. In the case of rem_drv, this will be the
36060Sstevel@tonic-gate 	 * final state. In the case of update_drv, i_ddi_bind_devs()
36070Sstevel@tonic-gate 	 * will be invoked later to reenumerate (new) driver.conf
36080Sstevel@tonic-gate 	 * rebind persistent nodes.
36090Sstevel@tonic-gate 	 */
36100Sstevel@tonic-gate 	while (cdip) {
36110Sstevel@tonic-gate 		dev_info_t *next = ddi_get_next_sibling(cdip);
36120Sstevel@tonic-gate 		if ((i_ddi_node_state(cdip) > DS_INITIALIZED) ||
36130Sstevel@tonic-gate 		    (ddi_driver_major(cdip) != major)) {
36140Sstevel@tonic-gate 			cdip = next;
36150Sstevel@tonic-gate 			continue;
36160Sstevel@tonic-gate 		}
36170Sstevel@tonic-gate 		(void) ndi_devi_unbind_driver(cdip);
36180Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(cdip) == 0)
36190Sstevel@tonic-gate 			(void) ddi_remove_child(cdip, 0);
36200Sstevel@tonic-gate 		cdip = next;
36210Sstevel@tonic-gate 	}
36220Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
36230Sstevel@tonic-gate 
36240Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
36250Sstevel@tonic-gate }
36260Sstevel@tonic-gate 
36270Sstevel@tonic-gate void
36280Sstevel@tonic-gate i_ddi_unbind_devs(major_t major)
36290Sstevel@tonic-gate {
36300Sstevel@tonic-gate 	ddi_walk_devs(top_devinfo, unbind_children, (void *)(uintptr_t)major);
36310Sstevel@tonic-gate }
36320Sstevel@tonic-gate 
36330Sstevel@tonic-gate /*
36340Sstevel@tonic-gate  * I/O Hotplug control
36350Sstevel@tonic-gate  */
36360Sstevel@tonic-gate 
36370Sstevel@tonic-gate /*
36380Sstevel@tonic-gate  * create and attach a dev_info node from a .conf file spec
36390Sstevel@tonic-gate  */
36400Sstevel@tonic-gate static void
36410Sstevel@tonic-gate init_spec_child(dev_info_t *pdip, struct hwc_spec *specp, uint_t flags)
36420Sstevel@tonic-gate {
36430Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags))
36440Sstevel@tonic-gate 	dev_info_t *dip;
36450Sstevel@tonic-gate 	char *node_name;
36460Sstevel@tonic-gate 
36470Sstevel@tonic-gate 	if (((node_name = specp->hwc_devi_name) == NULL) ||
36480Sstevel@tonic-gate 	    (ddi_name_to_major(node_name) == (major_t)-1)) {
36490Sstevel@tonic-gate 		char *tmp = node_name;
36500Sstevel@tonic-gate 		if (tmp == NULL)
36510Sstevel@tonic-gate 			tmp = "<none>";
36520Sstevel@tonic-gate 		cmn_err(CE_CONT,
36530Sstevel@tonic-gate 		    "init_spec_child: parent=%s, bad spec (%s)\n",
36540Sstevel@tonic-gate 		    ddi_node_name(pdip), tmp);
36550Sstevel@tonic-gate 		return;
36560Sstevel@tonic-gate 	}
36570Sstevel@tonic-gate 
36580Sstevel@tonic-gate 	dip = i_ddi_alloc_node(pdip, node_name, (dnode_t)DEVI_PSEUDO_NODEID,
36590Sstevel@tonic-gate 	    -1, specp->hwc_devi_sys_prop_ptr, KM_SLEEP);
36600Sstevel@tonic-gate 
36610Sstevel@tonic-gate 	if (dip == NULL)
36620Sstevel@tonic-gate 		return;
36630Sstevel@tonic-gate 
36640Sstevel@tonic-gate 	if (ddi_initchild(pdip, dip) != DDI_SUCCESS)
36650Sstevel@tonic-gate 		(void) ddi_remove_child(dip, 0);
36660Sstevel@tonic-gate }
36670Sstevel@tonic-gate 
36680Sstevel@tonic-gate /*
36690Sstevel@tonic-gate  * Lookup hwc specs from hash tables and make children from the spec
36700Sstevel@tonic-gate  * Because some .conf children are "merge" nodes, we also initialize
36710Sstevel@tonic-gate  * .conf children to merge properties onto hardware nodes.
36720Sstevel@tonic-gate  *
36730Sstevel@tonic-gate  * The pdip must be held busy.
36740Sstevel@tonic-gate  */
36750Sstevel@tonic-gate int
36760Sstevel@tonic-gate i_ndi_make_spec_children(dev_info_t *pdip, uint_t flags)
36770Sstevel@tonic-gate {
36780Sstevel@tonic-gate 	extern struct hwc_spec *hwc_get_child_spec(dev_info_t *, major_t);
3679298Scth 	int			circ;
3680298Scth 	struct hwc_spec		*list, *spec;
3681298Scth 
3682298Scth 	ndi_devi_enter(pdip, &circ);
3683298Scth 	if (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN) {
3684298Scth 		ndi_devi_exit(pdip, circ);
36850Sstevel@tonic-gate 		return (DDI_SUCCESS);
3686298Scth 	}
36870Sstevel@tonic-gate 
36880Sstevel@tonic-gate 	list = hwc_get_child_spec(pdip, (major_t)-1);
36890Sstevel@tonic-gate 	for (spec = list; spec != NULL; spec = spec->hwc_next) {
36900Sstevel@tonic-gate 		init_spec_child(pdip, spec, flags);
36910Sstevel@tonic-gate 	}
36920Sstevel@tonic-gate 	hwc_free_spec_list(list);
36930Sstevel@tonic-gate 
36940Sstevel@tonic-gate 	mutex_enter(&DEVI(pdip)->devi_lock);
36950Sstevel@tonic-gate 	DEVI(pdip)->devi_flags |= DEVI_MADE_CHILDREN;
36960Sstevel@tonic-gate 	mutex_exit(&DEVI(pdip)->devi_lock);
3697298Scth 	ndi_devi_exit(pdip, circ);
36980Sstevel@tonic-gate 	return (DDI_SUCCESS);
36990Sstevel@tonic-gate }
37000Sstevel@tonic-gate 
37010Sstevel@tonic-gate /*
37020Sstevel@tonic-gate  * Run initchild on all child nodes such that instance assignment
37030Sstevel@tonic-gate  * for multiport network cards are contiguous.
37040Sstevel@tonic-gate  *
37050Sstevel@tonic-gate  * The pdip must be held busy.
37060Sstevel@tonic-gate  */
37070Sstevel@tonic-gate static void
37080Sstevel@tonic-gate i_ndi_init_hw_children(dev_info_t *pdip, uint_t flags)
37090Sstevel@tonic-gate {
37100Sstevel@tonic-gate 	dev_info_t *dip;
37110Sstevel@tonic-gate 
37120Sstevel@tonic-gate 	ASSERT(DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
37130Sstevel@tonic-gate 
37140Sstevel@tonic-gate 	/* contiguous instance assignment */
37150Sstevel@tonic-gate 	e_ddi_enter_instance();
37160Sstevel@tonic-gate 	dip = ddi_get_child(pdip);
37170Sstevel@tonic-gate 	while (dip) {
37180Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(dip))
37190Sstevel@tonic-gate 			(void) i_ndi_config_node(dip, DS_INITIALIZED, flags);
37200Sstevel@tonic-gate 		dip = ddi_get_next_sibling(dip);
37210Sstevel@tonic-gate 	}
37220Sstevel@tonic-gate 	e_ddi_exit_instance();
37230Sstevel@tonic-gate }
37240Sstevel@tonic-gate 
37250Sstevel@tonic-gate /*
37260Sstevel@tonic-gate  * report device status
37270Sstevel@tonic-gate  */
37280Sstevel@tonic-gate static void
37290Sstevel@tonic-gate i_ndi_devi_report_status_change(dev_info_t *dip, char *path)
37300Sstevel@tonic-gate {
37310Sstevel@tonic-gate 	char *status;
37320Sstevel@tonic-gate 
37330Sstevel@tonic-gate 	if (!DEVI_NEED_REPORT(dip) ||
37340Sstevel@tonic-gate 	    (i_ddi_node_state(dip) < DS_INITIALIZED)) {
37350Sstevel@tonic-gate 		return;
37360Sstevel@tonic-gate 	}
37370Sstevel@tonic-gate 
37380Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
37390Sstevel@tonic-gate 		status = "offline";
37400Sstevel@tonic-gate 	} else if (DEVI_IS_DEVICE_DOWN(dip)) {
37410Sstevel@tonic-gate 		status = "down";
37420Sstevel@tonic-gate 	} else if (DEVI_IS_BUS_QUIESCED(dip)) {
37430Sstevel@tonic-gate 		status = "quiesced";
37440Sstevel@tonic-gate 	} else if (DEVI_IS_BUS_DOWN(dip)) {
37450Sstevel@tonic-gate 		status = "down";
37460Sstevel@tonic-gate 	} else if (i_ddi_node_state(dip) == DS_READY) {
37470Sstevel@tonic-gate 		status = "online";
37480Sstevel@tonic-gate 	} else {
37490Sstevel@tonic-gate 		status = "unknown";
37500Sstevel@tonic-gate 	}
37510Sstevel@tonic-gate 
37520Sstevel@tonic-gate 	if (path == NULL) {
37530Sstevel@tonic-gate 		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
37540Sstevel@tonic-gate 		cmn_err(CE_CONT, "?%s (%s%d) %s\n",
37550Sstevel@tonic-gate 			ddi_pathname(dip, path), ddi_driver_name(dip),
37560Sstevel@tonic-gate 			ddi_get_instance(dip), status);
37570Sstevel@tonic-gate 		kmem_free(path, MAXPATHLEN);
37580Sstevel@tonic-gate 	} else {
37590Sstevel@tonic-gate 		cmn_err(CE_CONT, "?%s (%s%d) %s\n",
37600Sstevel@tonic-gate 			path, ddi_driver_name(dip),
37610Sstevel@tonic-gate 			ddi_get_instance(dip), status);
37620Sstevel@tonic-gate 	}
37630Sstevel@tonic-gate 
3764495Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
37650Sstevel@tonic-gate 	DEVI_REPORT_DONE(dip);
3766495Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
37670Sstevel@tonic-gate }
37680Sstevel@tonic-gate 
37690Sstevel@tonic-gate /*
37700Sstevel@tonic-gate  * log a notification that a dev_info node has been configured.
37710Sstevel@tonic-gate  */
37720Sstevel@tonic-gate static int
37730Sstevel@tonic-gate i_log_devfs_add_devinfo(dev_info_t *dip, uint_t flags)
37740Sstevel@tonic-gate {
37750Sstevel@tonic-gate 	int se_err;
37760Sstevel@tonic-gate 	char *pathname;
37770Sstevel@tonic-gate 	sysevent_t *ev;
37780Sstevel@tonic-gate 	sysevent_id_t eid;
37790Sstevel@tonic-gate 	sysevent_value_t se_val;
37800Sstevel@tonic-gate 	sysevent_attr_list_t *ev_attr_list = NULL;
37810Sstevel@tonic-gate 	char *class_name;
37820Sstevel@tonic-gate 	int no_transport = 0;
37830Sstevel@tonic-gate 
37840Sstevel@tonic-gate 	ASSERT(dip);
37850Sstevel@tonic-gate 
37860Sstevel@tonic-gate 	/*
37870Sstevel@tonic-gate 	 * Invalidate the devinfo snapshot cache
37880Sstevel@tonic-gate 	 */
37890Sstevel@tonic-gate 	i_ddi_di_cache_invalidate(KM_SLEEP);
37900Sstevel@tonic-gate 
37910Sstevel@tonic-gate 	/* do not generate ESC_DEVFS_DEVI_ADD event during boot */
37920Sstevel@tonic-gate 	if (!i_ddi_io_initialized())
37930Sstevel@tonic-gate 		return (DDI_SUCCESS);
37940Sstevel@tonic-gate 
37950Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_ADD, EP_DDI, SE_SLEEP);
37960Sstevel@tonic-gate 
37970Sstevel@tonic-gate 	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
37980Sstevel@tonic-gate 
37990Sstevel@tonic-gate 	(void) ddi_pathname(dip, pathname);
38000Sstevel@tonic-gate 	ASSERT(strlen(pathname));
38010Sstevel@tonic-gate 
38020Sstevel@tonic-gate 	se_val.value_type = SE_DATA_TYPE_STRING;
38030Sstevel@tonic-gate 	se_val.value.sv_string = pathname;
38040Sstevel@tonic-gate 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
38050Sstevel@tonic-gate 	    &se_val, SE_SLEEP) != 0) {
38060Sstevel@tonic-gate 		goto fail;
38070Sstevel@tonic-gate 	}
38080Sstevel@tonic-gate 
38090Sstevel@tonic-gate 	/* add the device class attribute */
38100Sstevel@tonic-gate 	if ((class_name = i_ddi_devi_class(dip)) != NULL) {
38110Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_STRING;
38120Sstevel@tonic-gate 		se_val.value.sv_string = class_name;
38130Sstevel@tonic-gate 
38140Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
38150Sstevel@tonic-gate 		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
38160Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
38170Sstevel@tonic-gate 			goto fail;
38180Sstevel@tonic-gate 		}
38190Sstevel@tonic-gate 	}
38200Sstevel@tonic-gate 
38210Sstevel@tonic-gate 	/*
38220Sstevel@tonic-gate 	 * must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
38230Sstevel@tonic-gate 	 * in which case the branch event will be logged by the caller
38240Sstevel@tonic-gate 	 * after the entire branch has been configured.
38250Sstevel@tonic-gate 	 */
38260Sstevel@tonic-gate 	if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
38270Sstevel@tonic-gate 		/*
38280Sstevel@tonic-gate 		 * Instead of logging a separate branch event just add
38290Sstevel@tonic-gate 		 * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
38300Sstevel@tonic-gate 		 * generate a EC_DEV_BRANCH event.
38310Sstevel@tonic-gate 		 */
38320Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_INT32;
38330Sstevel@tonic-gate 		se_val.value.sv_int32 = 1;
38340Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
38350Sstevel@tonic-gate 		    DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
38360Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
38370Sstevel@tonic-gate 			goto fail;
38380Sstevel@tonic-gate 		}
38390Sstevel@tonic-gate 	}
38400Sstevel@tonic-gate 
38410Sstevel@tonic-gate 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
38420Sstevel@tonic-gate 		sysevent_free_attr(ev_attr_list);
38430Sstevel@tonic-gate 		goto fail;
38440Sstevel@tonic-gate 	}
38450Sstevel@tonic-gate 
38460Sstevel@tonic-gate 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
38470Sstevel@tonic-gate 		if (se_err == SE_NO_TRANSPORT)
38480Sstevel@tonic-gate 			no_transport = 1;
38490Sstevel@tonic-gate 		goto fail;
38500Sstevel@tonic-gate 	}
38510Sstevel@tonic-gate 
38520Sstevel@tonic-gate 	sysevent_free(ev);
38530Sstevel@tonic-gate 	kmem_free(pathname, MAXPATHLEN);
38540Sstevel@tonic-gate 
38550Sstevel@tonic-gate 	return (DDI_SUCCESS);
38560Sstevel@tonic-gate 
38570Sstevel@tonic-gate fail:
38580Sstevel@tonic-gate 	cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_ADD event for %s%s",
38590Sstevel@tonic-gate 	    pathname, (no_transport) ? " (syseventd not responding)" : "");
38600Sstevel@tonic-gate 
38610Sstevel@tonic-gate 	cmn_err(CE_WARN, "/dev may not be current for driver %s. "
38620Sstevel@tonic-gate 	    "Run devfsadm -i %s",
38630Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_driver_name(dip));
38640Sstevel@tonic-gate 
38650Sstevel@tonic-gate 	sysevent_free(ev);
38660Sstevel@tonic-gate 	kmem_free(pathname, MAXPATHLEN);
38670Sstevel@tonic-gate 	return (DDI_SUCCESS);
38680Sstevel@tonic-gate }
38690Sstevel@tonic-gate 
38700Sstevel@tonic-gate /*
38710Sstevel@tonic-gate  * log a notification that a dev_info node has been unconfigured.
38720Sstevel@tonic-gate  */
38730Sstevel@tonic-gate static int
38740Sstevel@tonic-gate i_log_devfs_remove_devinfo(char *pathname, char *class_name, char *driver_name,
38750Sstevel@tonic-gate     int instance, uint_t flags)
38760Sstevel@tonic-gate {
38770Sstevel@tonic-gate 	sysevent_t *ev;
38780Sstevel@tonic-gate 	sysevent_id_t eid;
38790Sstevel@tonic-gate 	sysevent_value_t se_val;
38800Sstevel@tonic-gate 	sysevent_attr_list_t *ev_attr_list = NULL;
38810Sstevel@tonic-gate 	int se_err;
38820Sstevel@tonic-gate 	int no_transport = 0;
38830Sstevel@tonic-gate 
38840Sstevel@tonic-gate 	i_ddi_di_cache_invalidate(KM_SLEEP);
38850Sstevel@tonic-gate 
38860Sstevel@tonic-gate 	if (!i_ddi_io_initialized())
38870Sstevel@tonic-gate 		return (DDI_SUCCESS);
38880Sstevel@tonic-gate 
38890Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_REMOVE, EP_DDI, SE_SLEEP);
38900Sstevel@tonic-gate 
38910Sstevel@tonic-gate 	se_val.value_type = SE_DATA_TYPE_STRING;
38920Sstevel@tonic-gate 	se_val.value.sv_string = pathname;
38930Sstevel@tonic-gate 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
38940Sstevel@tonic-gate 	    &se_val, SE_SLEEP) != 0) {
38950Sstevel@tonic-gate 		goto fail;
38960Sstevel@tonic-gate 	}
38970Sstevel@tonic-gate 
38980Sstevel@tonic-gate 	if (class_name) {
38990Sstevel@tonic-gate 		/* add the device class, driver name and instance attributes */
39000Sstevel@tonic-gate 
39010Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_STRING;
39020Sstevel@tonic-gate 		se_val.value.sv_string = class_name;
39030Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
39040Sstevel@tonic-gate 		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
39050Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
39060Sstevel@tonic-gate 			goto fail;
39070Sstevel@tonic-gate 		}
39080Sstevel@tonic-gate 
39090Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_STRING;
39100Sstevel@tonic-gate 		se_val.value.sv_string = driver_name;
39110Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
39120Sstevel@tonic-gate 		    DEVFS_DRIVER_NAME, &se_val, SE_SLEEP) != 0) {
39130Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
39140Sstevel@tonic-gate 			goto fail;
39150Sstevel@tonic-gate 		}
39160Sstevel@tonic-gate 
39170Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_INT32;
39180Sstevel@tonic-gate 		se_val.value.sv_int32 = instance;
39190Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
39200Sstevel@tonic-gate 		    DEVFS_INSTANCE, &se_val, SE_SLEEP) != 0) {
39210Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
39220Sstevel@tonic-gate 			goto fail;
39230Sstevel@tonic-gate 		}
39240Sstevel@tonic-gate 	}
39250Sstevel@tonic-gate 
39260Sstevel@tonic-gate 	/*
39270Sstevel@tonic-gate 	 * must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
39280Sstevel@tonic-gate 	 * in which case the branch event will be logged by the caller
39290Sstevel@tonic-gate 	 * after the entire branch has been unconfigured.
39300Sstevel@tonic-gate 	 */
39310Sstevel@tonic-gate 	if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
39320Sstevel@tonic-gate 		/*
39330Sstevel@tonic-gate 		 * Instead of logging a separate branch event just add
39340Sstevel@tonic-gate 		 * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
39350Sstevel@tonic-gate 		 * generate a EC_DEV_BRANCH event.
39360Sstevel@tonic-gate 		 */
39370Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_INT32;
39380Sstevel@tonic-gate 		se_val.value.sv_int32 = 1;
39390Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
39400Sstevel@tonic-gate 		    DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
39410Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
39420Sstevel@tonic-gate 			goto fail;
39430Sstevel@tonic-gate 		}
39440Sstevel@tonic-gate 	}
39450Sstevel@tonic-gate 
39460Sstevel@tonic-gate 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
39470Sstevel@tonic-gate 		sysevent_free_attr(ev_attr_list);
39480Sstevel@tonic-gate 		goto fail;
39490Sstevel@tonic-gate 	}
39500Sstevel@tonic-gate 
39510Sstevel@tonic-gate 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
39520Sstevel@tonic-gate 		if (se_err == SE_NO_TRANSPORT)
39530Sstevel@tonic-gate 			no_transport = 1;
39540Sstevel@tonic-gate 		goto fail;
39550Sstevel@tonic-gate 	}
39560Sstevel@tonic-gate 
39570Sstevel@tonic-gate 	sysevent_free(ev);
39580Sstevel@tonic-gate 	return (DDI_SUCCESS);
39590Sstevel@tonic-gate 
39600Sstevel@tonic-gate fail:
39610Sstevel@tonic-gate 	sysevent_free(ev);
39620Sstevel@tonic-gate 	cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_REMOVE event for %s%s",
39630Sstevel@tonic-gate 	    pathname, (no_transport) ? " (syseventd not responding)" : "");
39640Sstevel@tonic-gate 	return (DDI_SUCCESS);
39650Sstevel@tonic-gate }
39660Sstevel@tonic-gate 
39670Sstevel@tonic-gate /*
39680Sstevel@tonic-gate  * log an event that a dev_info branch has been configured or unconfigured.
39690Sstevel@tonic-gate  */
39700Sstevel@tonic-gate static int
39710Sstevel@tonic-gate i_log_devfs_branch(char *node_path, char *subclass)
39720Sstevel@tonic-gate {
39730Sstevel@tonic-gate 	int se_err;
39740Sstevel@tonic-gate 	sysevent_t *ev;
39750Sstevel@tonic-gate 	sysevent_id_t eid;
39760Sstevel@tonic-gate 	sysevent_value_t se_val;
39770Sstevel@tonic-gate 	sysevent_attr_list_t *ev_attr_list = NULL;
39780Sstevel@tonic-gate 	int no_transport = 0;
39790Sstevel@tonic-gate 
39800Sstevel@tonic-gate 	/* do not generate the event during boot */
39810Sstevel@tonic-gate 	if (!i_ddi_io_initialized())
39820Sstevel@tonic-gate 		return (DDI_SUCCESS);
39830Sstevel@tonic-gate 
39840Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, subclass, EP_DDI, SE_SLEEP);
39850Sstevel@tonic-gate 
39860Sstevel@tonic-gate 	se_val.value_type = SE_DATA_TYPE_STRING;
39870Sstevel@tonic-gate 	se_val.value.sv_string = node_path;
39880Sstevel@tonic-gate 
39890Sstevel@tonic-gate 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
39900Sstevel@tonic-gate 	    &se_val, SE_SLEEP) != 0) {
39910Sstevel@tonic-gate 		goto fail;
39920Sstevel@tonic-gate 	}
39930Sstevel@tonic-gate 
39940Sstevel@tonic-gate 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
39950Sstevel@tonic-gate 		sysevent_free_attr(ev_attr_list);
39960Sstevel@tonic-gate 		goto fail;
39970Sstevel@tonic-gate 	}
39980Sstevel@tonic-gate 
39990Sstevel@tonic-gate 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
40000Sstevel@tonic-gate 		if (se_err == SE_NO_TRANSPORT)
40010Sstevel@tonic-gate 			no_transport = 1;
40020Sstevel@tonic-gate 		goto fail;
40030Sstevel@tonic-gate 	}
40040Sstevel@tonic-gate 
40050Sstevel@tonic-gate 	sysevent_free(ev);
40060Sstevel@tonic-gate 	return (DDI_SUCCESS);
40070Sstevel@tonic-gate 
40080Sstevel@tonic-gate fail:
40090Sstevel@tonic-gate 	cmn_err(CE_WARN, "failed to log %s branch event for %s%s",
40100Sstevel@tonic-gate 	    subclass, node_path,
40110Sstevel@tonic-gate 	    (no_transport) ? " (syseventd not responding)" : "");
40120Sstevel@tonic-gate 
40130Sstevel@tonic-gate 	sysevent_free(ev);
40140Sstevel@tonic-gate 	return (DDI_FAILURE);
40150Sstevel@tonic-gate }
40160Sstevel@tonic-gate 
40170Sstevel@tonic-gate /*
40180Sstevel@tonic-gate  * log an event that a dev_info tree branch has been configured.
40190Sstevel@tonic-gate  */
40200Sstevel@tonic-gate static int
40210Sstevel@tonic-gate i_log_devfs_branch_add(dev_info_t *dip)
40220Sstevel@tonic-gate {
40230Sstevel@tonic-gate 	char *node_path;
40240Sstevel@tonic-gate 	int rv;
40250Sstevel@tonic-gate 
40260Sstevel@tonic-gate 	node_path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
40270Sstevel@tonic-gate 	(void) ddi_pathname(dip, node_path);
40280Sstevel@tonic-gate 	rv = i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_ADD);
40290Sstevel@tonic-gate 	kmem_free(node_path, MAXPATHLEN);
40300Sstevel@tonic-gate 
40310Sstevel@tonic-gate 	return (rv);
40320Sstevel@tonic-gate }
40330Sstevel@tonic-gate 
40340Sstevel@tonic-gate /*
40350Sstevel@tonic-gate  * log an event that a dev_info tree branch has been unconfigured.
40360Sstevel@tonic-gate  */
40370Sstevel@tonic-gate static int
40380Sstevel@tonic-gate i_log_devfs_branch_remove(char *node_path)
40390Sstevel@tonic-gate {
40400Sstevel@tonic-gate 	return (i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_REMOVE));
40410Sstevel@tonic-gate }
40420Sstevel@tonic-gate 
40430Sstevel@tonic-gate /*
40440Sstevel@tonic-gate  * enqueue the dip's deviname on the branch event queue.
40450Sstevel@tonic-gate  */
40460Sstevel@tonic-gate static struct brevq_node *
40470Sstevel@tonic-gate brevq_enqueue(struct brevq_node **brevqp, dev_info_t *dip,
40480Sstevel@tonic-gate     struct brevq_node *child)
40490Sstevel@tonic-gate {
40500Sstevel@tonic-gate 	struct brevq_node *brn;
40510Sstevel@tonic-gate 	char *deviname;
40520Sstevel@tonic-gate 
40530Sstevel@tonic-gate 	deviname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
40540Sstevel@tonic-gate 	(void) ddi_deviname(dip, deviname);
40550Sstevel@tonic-gate 
40560Sstevel@tonic-gate 	brn = kmem_zalloc(sizeof (*brn), KM_SLEEP);
40570Sstevel@tonic-gate 	brn->deviname = i_ddi_strdup(deviname, KM_SLEEP);
40580Sstevel@tonic-gate 	kmem_free(deviname, MAXNAMELEN);
40590Sstevel@tonic-gate 	brn->child = child;
40600Sstevel@tonic-gate 	brn->sibling = *brevqp;
40610Sstevel@tonic-gate 	*brevqp = brn;
40620Sstevel@tonic-gate 
40630Sstevel@tonic-gate 	return (brn);
40640Sstevel@tonic-gate }
40650Sstevel@tonic-gate 
40660Sstevel@tonic-gate /*
40670Sstevel@tonic-gate  * free the memory allocated for the elements on the branch event queue.
40680Sstevel@tonic-gate  */
40690Sstevel@tonic-gate static void
40700Sstevel@tonic-gate free_brevq(struct brevq_node *brevq)
40710Sstevel@tonic-gate {
40720Sstevel@tonic-gate 	struct brevq_node *brn, *next_brn;
40730Sstevel@tonic-gate 
40740Sstevel@tonic-gate 	for (brn = brevq; brn != NULL; brn = next_brn) {
40750Sstevel@tonic-gate 		next_brn = brn->sibling;
40760Sstevel@tonic-gate 		ASSERT(brn->child == NULL);
40770Sstevel@tonic-gate 		kmem_free(brn->deviname, strlen(brn->deviname) + 1);
40780Sstevel@tonic-gate 		kmem_free(brn, sizeof (*brn));
40790Sstevel@tonic-gate 	}
40800Sstevel@tonic-gate }
40810Sstevel@tonic-gate 
40820Sstevel@tonic-gate /*
40830Sstevel@tonic-gate  * log the events queued up on the branch event queue and free the
40840Sstevel@tonic-gate  * associated memory.
40850Sstevel@tonic-gate  *
40860Sstevel@tonic-gate  * node_path must have been allocated with at least MAXPATHLEN bytes.
40870Sstevel@tonic-gate  */
40880Sstevel@tonic-gate static void
40890Sstevel@tonic-gate log_and_free_brevq(char *node_path, struct brevq_node *brevq)
40900Sstevel@tonic-gate {
40910Sstevel@tonic-gate 	struct brevq_node *brn;
40920Sstevel@tonic-gate 	char *p;
40930Sstevel@tonic-gate 
40940Sstevel@tonic-gate 	p = node_path + strlen(node_path);
40950Sstevel@tonic-gate 	for (brn = brevq; brn != NULL; brn = brn->sibling) {
40960Sstevel@tonic-gate 		(void) strcpy(p, brn->deviname);
40970Sstevel@tonic-gate 		(void) i_log_devfs_branch_remove(node_path);
40980Sstevel@tonic-gate 	}
40990Sstevel@tonic-gate 	*p = '\0';
41000Sstevel@tonic-gate 
41010Sstevel@tonic-gate 	free_brevq(brevq);
41020Sstevel@tonic-gate }
41030Sstevel@tonic-gate 
41040Sstevel@tonic-gate /*
41050Sstevel@tonic-gate  * log the events queued up on the branch event queue and free the
41060Sstevel@tonic-gate  * associated memory. Same as the previous function but operates on dip.
41070Sstevel@tonic-gate  */
41080Sstevel@tonic-gate static void
41090Sstevel@tonic-gate log_and_free_brevq_dip(dev_info_t *dip, struct brevq_node *brevq)
41100Sstevel@tonic-gate {
41110Sstevel@tonic-gate 	char *path;
41120Sstevel@tonic-gate 
41130Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
41140Sstevel@tonic-gate 	(void) ddi_pathname(dip, path);
41150Sstevel@tonic-gate 	log_and_free_brevq(path, brevq);
41160Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
41170Sstevel@tonic-gate }
41180Sstevel@tonic-gate 
41190Sstevel@tonic-gate /*
41200Sstevel@tonic-gate  * log the outstanding branch remove events for the grand children of the dip
41210Sstevel@tonic-gate  * and free the associated memory.
41220Sstevel@tonic-gate  */
41230Sstevel@tonic-gate static void
41240Sstevel@tonic-gate log_and_free_br_events_on_grand_children(dev_info_t *dip,
41250Sstevel@tonic-gate     struct brevq_node *brevq)
41260Sstevel@tonic-gate {
41270Sstevel@tonic-gate 	struct brevq_node *brn;
41280Sstevel@tonic-gate 	char *path;
41290Sstevel@tonic-gate 	char *p;
41300Sstevel@tonic-gate 
41310Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
41320Sstevel@tonic-gate 	(void) ddi_pathname(dip, path);
41330Sstevel@tonic-gate 	p = path + strlen(path);
41340Sstevel@tonic-gate 	for (brn = brevq; brn != NULL; brn = brn->sibling) {
41350Sstevel@tonic-gate 		if (brn->child) {
41360Sstevel@tonic-gate 			(void) strcpy(p, brn->deviname);
41370Sstevel@tonic-gate 			/* now path contains the node path to the dip's child */
41380Sstevel@tonic-gate 			log_and_free_brevq(path, brn->child);
41390Sstevel@tonic-gate 			brn->child = NULL;
41400Sstevel@tonic-gate 		}
41410Sstevel@tonic-gate 	}
41420Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
41430Sstevel@tonic-gate }
41440Sstevel@tonic-gate 
41450Sstevel@tonic-gate /*
41460Sstevel@tonic-gate  * log and cleanup branch remove events for the grand children of the dip.
41470Sstevel@tonic-gate  */
41480Sstevel@tonic-gate static void
41490Sstevel@tonic-gate cleanup_br_events_on_grand_children(dev_info_t *dip, struct brevq_node **brevqp)
41500Sstevel@tonic-gate {
41510Sstevel@tonic-gate 	dev_info_t *child;
41520Sstevel@tonic-gate 	struct brevq_node *brevq, *brn, *prev_brn, *next_brn;
41530Sstevel@tonic-gate 	char *path;
41540Sstevel@tonic-gate 	int circ;
41550Sstevel@tonic-gate 
41560Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
41570Sstevel@tonic-gate 	prev_brn = NULL;
41580Sstevel@tonic-gate 	brevq = *brevqp;
41590Sstevel@tonic-gate 
41600Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
41610Sstevel@tonic-gate 	for (brn = brevq; brn != NULL; brn = next_brn) {
41620Sstevel@tonic-gate 		next_brn = brn->sibling;
41630Sstevel@tonic-gate 		for (child = ddi_get_child(dip); child != NULL;
41640Sstevel@tonic-gate 		    child = ddi_get_next_sibling(child)) {
41650Sstevel@tonic-gate 			if (i_ddi_node_state(child) >= DS_INITIALIZED) {
41660Sstevel@tonic-gate 				(void) ddi_deviname(child, path);
41670Sstevel@tonic-gate 				if (strcmp(path, brn->deviname) == 0)
41680Sstevel@tonic-gate 					break;
41690Sstevel@tonic-gate 			}
41700Sstevel@tonic-gate 		}
41710Sstevel@tonic-gate 
41720Sstevel@tonic-gate 		if (child != NULL && !(DEVI_EVREMOVE(child))) {
41730Sstevel@tonic-gate 			/*
41740Sstevel@tonic-gate 			 * Event state is not REMOVE. So branch remove event
41750Sstevel@tonic-gate 			 * is not going be generated on brn->child.
41760Sstevel@tonic-gate 			 * If any branch remove events were queued up on
41770Sstevel@tonic-gate 			 * brn->child log them and remove the brn
41780Sstevel@tonic-gate 			 * from the queue.
41790Sstevel@tonic-gate 			 */
41800Sstevel@tonic-gate 			if (brn->child) {
41810Sstevel@tonic-gate 				(void) ddi_pathname(dip, path);
41820Sstevel@tonic-gate 				(void) strcat(path, brn->deviname);
41830Sstevel@tonic-gate 				log_and_free_brevq(path, brn->child);
41840Sstevel@tonic-gate 			}
41850Sstevel@tonic-gate 
41860Sstevel@tonic-gate 			if (prev_brn)
41870Sstevel@tonic-gate 				prev_brn->sibling = next_brn;
41880Sstevel@tonic-gate 			else
41890Sstevel@tonic-gate 				*brevqp = next_brn;
41900Sstevel@tonic-gate 
41910Sstevel@tonic-gate 			kmem_free(brn->deviname, strlen(brn->deviname) + 1);
41920Sstevel@tonic-gate 			kmem_free(brn, sizeof (*brn));
41930Sstevel@tonic-gate 		} else {
41940Sstevel@tonic-gate 			/*
41950Sstevel@tonic-gate 			 * Free up the outstanding branch remove events
41960Sstevel@tonic-gate 			 * queued on brn->child since brn->child
41970Sstevel@tonic-gate 			 * itself is eligible for branch remove event.
41980Sstevel@tonic-gate 			 */
41990Sstevel@tonic-gate 			if (brn->child) {
42000Sstevel@tonic-gate 				free_brevq(brn->child);
42010Sstevel@tonic-gate 				brn->child = NULL;
42020Sstevel@tonic-gate 			}
42030Sstevel@tonic-gate 			prev_brn = brn;
42040Sstevel@tonic-gate 		}
42050Sstevel@tonic-gate 	}
42060Sstevel@tonic-gate 
42070Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
42080Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
42090Sstevel@tonic-gate }
42100Sstevel@tonic-gate 
42110Sstevel@tonic-gate static int
42120Sstevel@tonic-gate need_remove_event(dev_info_t *dip, int flags)
42130Sstevel@tonic-gate {
42140Sstevel@tonic-gate 	if ((flags & (NDI_NO_EVENT | NDI_AUTODETACH)) == 0 &&
42150Sstevel@tonic-gate 	    (flags & (NDI_DEVI_OFFLINE | NDI_UNCONFIG | NDI_DEVI_REMOVE)) &&
42160Sstevel@tonic-gate 	    !(DEVI_EVREMOVE(dip)))
42170Sstevel@tonic-gate 		return (1);
42180Sstevel@tonic-gate 	else
42190Sstevel@tonic-gate 		return (0);
42200Sstevel@tonic-gate }
42210Sstevel@tonic-gate 
42220Sstevel@tonic-gate /*
42230Sstevel@tonic-gate  * Unconfigure children/descendants of the dip.
42240Sstevel@tonic-gate  *
42250Sstevel@tonic-gate  * If the operation involves a branch event NDI_BRANCH_EVENT_OP is set
42260Sstevel@tonic-gate  * through out the unconfiguration. On successful return *brevqp is set to
42270Sstevel@tonic-gate  * a queue of dip's child devinames for which branch remove events need
42280Sstevel@tonic-gate  * to be generated.
42290Sstevel@tonic-gate  */
42300Sstevel@tonic-gate static int
42310Sstevel@tonic-gate devi_unconfig_branch(dev_info_t *dip, dev_info_t **dipp, int flags,
42320Sstevel@tonic-gate     struct brevq_node **brevqp)
42330Sstevel@tonic-gate {
42340Sstevel@tonic-gate 	int rval;
42350Sstevel@tonic-gate 
42360Sstevel@tonic-gate 	*brevqp = NULL;
42370Sstevel@tonic-gate 
42380Sstevel@tonic-gate 	if ((!(flags & NDI_BRANCH_EVENT_OP)) && need_remove_event(dip, flags))
42390Sstevel@tonic-gate 		flags |= NDI_BRANCH_EVENT_OP;
42400Sstevel@tonic-gate 
42410Sstevel@tonic-gate 	if (flags & NDI_BRANCH_EVENT_OP) {
42420Sstevel@tonic-gate 		rval = devi_unconfig_common(dip, dipp, flags, (major_t)-1,
42430Sstevel@tonic-gate 		    brevqp);
42440Sstevel@tonic-gate 
42450Sstevel@tonic-gate 		if (rval != NDI_SUCCESS && (*brevqp)) {
42460Sstevel@tonic-gate 			log_and_free_brevq_dip(dip, *brevqp);
42470Sstevel@tonic-gate 			*brevqp = NULL;
42480Sstevel@tonic-gate 		}
42490Sstevel@tonic-gate 	} else
42500Sstevel@tonic-gate 		rval = devi_unconfig_common(dip, dipp, flags, (major_t)-1,
42510Sstevel@tonic-gate 		    NULL);
42520Sstevel@tonic-gate 
42530Sstevel@tonic-gate 	return (rval);
42540Sstevel@tonic-gate }
42550Sstevel@tonic-gate 
42560Sstevel@tonic-gate /*
42570Sstevel@tonic-gate  * If the dip is already bound to a driver transition to DS_INITIALIZED
42580Sstevel@tonic-gate  * in order to generate an event in the case where the node was left in
42590Sstevel@tonic-gate  * DS_BOUND state since boot (never got attached) and the node is now
42600Sstevel@tonic-gate  * being offlined.
42610Sstevel@tonic-gate  */
42620Sstevel@tonic-gate static void
42630Sstevel@tonic-gate init_bound_node_ev(dev_info_t *pdip, dev_info_t *dip, int flags)
42640Sstevel@tonic-gate {
42650Sstevel@tonic-gate 	if (need_remove_event(dip, flags) &&
42660Sstevel@tonic-gate 	    i_ddi_node_state(dip) == DS_BOUND &&
42670Sstevel@tonic-gate 	    i_ddi_node_state(pdip) >= DS_ATTACHED &&
42680Sstevel@tonic-gate 	    !(DEVI_IS_DEVICE_OFFLINE(dip)))
42690Sstevel@tonic-gate 		(void) ddi_initchild(pdip, dip);
42700Sstevel@tonic-gate }
42710Sstevel@tonic-gate 
42720Sstevel@tonic-gate /*
42730Sstevel@tonic-gate  * attach a node/branch with parent already held busy
42740Sstevel@tonic-gate  */
42750Sstevel@tonic-gate static int
42760Sstevel@tonic-gate devi_attach_node(dev_info_t *dip, uint_t flags)
42770Sstevel@tonic-gate {
4278495Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
42790Sstevel@tonic-gate 	if (flags & NDI_DEVI_ONLINE) {
4280495Scth 		if (i_ddi_node_state(dip) != DS_READY)
4281495Scth 			DEVI_SET_REPORT(dip);
42820Sstevel@tonic-gate 		DEVI_SET_DEVICE_ONLINE(dip);
42830Sstevel@tonic-gate 	}
42840Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
4285495Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
42860Sstevel@tonic-gate 		return (NDI_FAILURE);
42870Sstevel@tonic-gate 	}
4288495Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
42890Sstevel@tonic-gate 
42900Sstevel@tonic-gate 	if (i_ddi_attachchild(dip) != DDI_SUCCESS) {
4291495Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
42920Sstevel@tonic-gate 		DEVI_SET_EVUNINIT(dip);
4293495Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
4294495Scth 
42950Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(dip))
42960Sstevel@tonic-gate 			(void) ddi_uninitchild(dip);
42970Sstevel@tonic-gate 		else {
42980Sstevel@tonic-gate 			/*
42990Sstevel@tonic-gate 			 * Delete .conf nodes and nodes that are not
43000Sstevel@tonic-gate 			 * well formed.
43010Sstevel@tonic-gate 			 */
43020Sstevel@tonic-gate 			(void) ddi_remove_child(dip, 0);
43030Sstevel@tonic-gate 		}
43040Sstevel@tonic-gate 		return (NDI_FAILURE);
43050Sstevel@tonic-gate 	}
43060Sstevel@tonic-gate 
43070Sstevel@tonic-gate 	i_ndi_devi_report_status_change(dip, NULL);
43080Sstevel@tonic-gate 
43090Sstevel@tonic-gate 	/*
43100Sstevel@tonic-gate 	 * log an event, but not during devfs lookups in which case
43110Sstevel@tonic-gate 	 * NDI_NO_EVENT is set.
43120Sstevel@tonic-gate 	 */
43130Sstevel@tonic-gate 	if ((flags & NDI_NO_EVENT) == 0 && !(DEVI_EVADD(dip))) {
43140Sstevel@tonic-gate 		(void) i_log_devfs_add_devinfo(dip, flags);
4315495Scth 
4316495Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
43170Sstevel@tonic-gate 		DEVI_SET_EVADD(dip);
4318495Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
4319495Scth 	} else if (!(flags & NDI_NO_EVENT_STATE_CHNG)) {
4320495Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
43210Sstevel@tonic-gate 		DEVI_SET_EVADD(dip);
4322495Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
4323495Scth 	}
43240Sstevel@tonic-gate 
43250Sstevel@tonic-gate 	return (NDI_SUCCESS);
43260Sstevel@tonic-gate }
43270Sstevel@tonic-gate 
43280Sstevel@tonic-gate /*
43290Sstevel@tonic-gate  * Configure all children of a nexus, assuming all spec children have
43300Sstevel@tonic-gate  * been made.
43310Sstevel@tonic-gate  */
43320Sstevel@tonic-gate static int
43330Sstevel@tonic-gate devi_attach_children(dev_info_t *pdip, uint_t flags, major_t major)
43340Sstevel@tonic-gate {
43350Sstevel@tonic-gate 	dev_info_t *dip;
43360Sstevel@tonic-gate 
43370Sstevel@tonic-gate 	ASSERT(DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
43380Sstevel@tonic-gate 
43390Sstevel@tonic-gate 	dip = ddi_get_child(pdip);
43400Sstevel@tonic-gate 	while (dip) {
43410Sstevel@tonic-gate 		/*
43420Sstevel@tonic-gate 		 * NOTE: devi_attach_node() may remove the dip
43430Sstevel@tonic-gate 		 */
43440Sstevel@tonic-gate 		dev_info_t *next = ddi_get_next_sibling(dip);
43450Sstevel@tonic-gate 
43460Sstevel@tonic-gate 		/*
43470Sstevel@tonic-gate 		 * Configure all nexus nodes or leaf nodes with
43480Sstevel@tonic-gate 		 * matching driver major
43490Sstevel@tonic-gate 		 */
43500Sstevel@tonic-gate 		if ((major == (major_t)-1) ||
43510Sstevel@tonic-gate 		    (major == ddi_driver_major(dip)) ||
43520Sstevel@tonic-gate 		    ((flags & NDI_CONFIG) && (is_leaf_node(dip) == 0)))
43530Sstevel@tonic-gate 			(void) devi_attach_node(dip, flags);
43540Sstevel@tonic-gate 		dip = next;
43550Sstevel@tonic-gate 	}
43560Sstevel@tonic-gate 
43570Sstevel@tonic-gate 	return (NDI_SUCCESS);
43580Sstevel@tonic-gate }
43590Sstevel@tonic-gate 
43600Sstevel@tonic-gate /* internal function to config immediate children */
43610Sstevel@tonic-gate static int
43620Sstevel@tonic-gate config_immediate_children(dev_info_t *pdip, uint_t flags, major_t major)
43630Sstevel@tonic-gate {
43640Sstevel@tonic-gate 	int circ;
43650Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(pdip) >= DS_ATTACHED);
43660Sstevel@tonic-gate 
43670Sstevel@tonic-gate 	if (!NEXUS_DRV(ddi_get_driver(pdip)))
43680Sstevel@tonic-gate 		return (NDI_SUCCESS);
43690Sstevel@tonic-gate 
43700Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
43710Sstevel@tonic-gate 	    "config_immediate_children: %s%d (%p), flags=%x\n",
43720Sstevel@tonic-gate 	    ddi_driver_name(pdip), ddi_get_instance(pdip),
43730Sstevel@tonic-gate 	    (void *)pdip, flags));
43740Sstevel@tonic-gate 
43750Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
43760Sstevel@tonic-gate 
43770Sstevel@tonic-gate 	if (flags & NDI_CONFIG_REPROBE) {
43780Sstevel@tonic-gate 		mutex_enter(&DEVI(pdip)->devi_lock);
43790Sstevel@tonic-gate 		DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
43800Sstevel@tonic-gate 		mutex_exit(&DEVI(pdip)->devi_lock);
43810Sstevel@tonic-gate 	}
43820Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(pdip, flags);
43830Sstevel@tonic-gate 	i_ndi_init_hw_children(pdip, flags);
43840Sstevel@tonic-gate 	(void) devi_attach_children(pdip, flags, major);
43850Sstevel@tonic-gate 
43860Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
43870Sstevel@tonic-gate 
43880Sstevel@tonic-gate 	return (NDI_SUCCESS);
43890Sstevel@tonic-gate }
43900Sstevel@tonic-gate 
43910Sstevel@tonic-gate /* internal function to config grand children */
43920Sstevel@tonic-gate static int
43930Sstevel@tonic-gate config_grand_children(dev_info_t *pdip, uint_t flags, major_t major)
43940Sstevel@tonic-gate {
43950Sstevel@tonic-gate 	struct mt_config_handle *hdl;
43960Sstevel@tonic-gate 
43970Sstevel@tonic-gate 	/* multi-threaded configuration of child nexus */
43980Sstevel@tonic-gate 	hdl = mt_config_init(pdip, NULL, flags, major, MT_CONFIG_OP, NULL);
43990Sstevel@tonic-gate 	mt_config_children(hdl);
44000Sstevel@tonic-gate 
44010Sstevel@tonic-gate 	return (mt_config_fini(hdl));	/* wait for threads to exit */
44020Sstevel@tonic-gate }
44030Sstevel@tonic-gate 
44040Sstevel@tonic-gate /*
44050Sstevel@tonic-gate  * Common function for device tree configuration,
44060Sstevel@tonic-gate  * either BUS_CONFIG_ALL or BUS_CONFIG_DRIVER.
44070Sstevel@tonic-gate  * The NDI_CONFIG flag causes recursive configuration of
44080Sstevel@tonic-gate  * grandchildren, devfs usage should not recurse.
44090Sstevel@tonic-gate  */
44100Sstevel@tonic-gate static int
44110Sstevel@tonic-gate devi_config_common(dev_info_t *dip, int flags, major_t major)
44120Sstevel@tonic-gate {
44130Sstevel@tonic-gate 	int error;
44140Sstevel@tonic-gate 	int (*f)();
44150Sstevel@tonic-gate 
44160Sstevel@tonic-gate 	if (i_ddi_node_state(dip) <  DS_READY)
44170Sstevel@tonic-gate 		return (NDI_FAILURE);
44180Sstevel@tonic-gate 
44190Sstevel@tonic-gate 	if (pm_pre_config(dip, NULL) != DDI_SUCCESS)
44200Sstevel@tonic-gate 		return (NDI_FAILURE);
44210Sstevel@tonic-gate 
44220Sstevel@tonic-gate 	if ((DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
44230Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
44240Sstevel@tonic-gate 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
44250Sstevel@tonic-gate 		error = config_immediate_children(dip, flags, major);
44260Sstevel@tonic-gate 	} else {
44270Sstevel@tonic-gate 		/* call bus_config entry point */
44280Sstevel@tonic-gate 		ddi_bus_config_op_t bus_op = (major == (major_t)-1) ?
44290Sstevel@tonic-gate 		    BUS_CONFIG_ALL : BUS_CONFIG_DRIVER;
44300Sstevel@tonic-gate 		error = (*f)(dip,
44310Sstevel@tonic-gate 		    flags, bus_op, (void *)(uintptr_t)major, NULL, 0);
44320Sstevel@tonic-gate 	}
44330Sstevel@tonic-gate 
44340Sstevel@tonic-gate 	if (error) {
44350Sstevel@tonic-gate 		pm_post_config(dip, NULL);
44360Sstevel@tonic-gate 		return (error);
44370Sstevel@tonic-gate 	}
44380Sstevel@tonic-gate 
44390Sstevel@tonic-gate 	/*
44400Sstevel@tonic-gate 	 * Some callers, notably SCSI, need to mark the devfs cache
44410Sstevel@tonic-gate 	 * to be rebuilt together with the config operation.
44420Sstevel@tonic-gate 	 */
44430Sstevel@tonic-gate 	if (flags & NDI_DEVFS_CLEAN)
44440Sstevel@tonic-gate 		(void) devfs_clean(dip, NULL, 0);
44450Sstevel@tonic-gate 
44460Sstevel@tonic-gate 	if (flags & NDI_CONFIG)
44470Sstevel@tonic-gate 		(void) config_grand_children(dip, flags, major);
44480Sstevel@tonic-gate 
44490Sstevel@tonic-gate 	pm_post_config(dip, NULL);
44500Sstevel@tonic-gate 
44510Sstevel@tonic-gate 	return (NDI_SUCCESS);
44520Sstevel@tonic-gate }
44530Sstevel@tonic-gate 
44540Sstevel@tonic-gate /*
44550Sstevel@tonic-gate  * Framework entry point for BUS_CONFIG_ALL
44560Sstevel@tonic-gate  */
44570Sstevel@tonic-gate int
44580Sstevel@tonic-gate ndi_devi_config(dev_info_t *dip, int flags)
44590Sstevel@tonic-gate {
44600Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
44610Sstevel@tonic-gate 	    "ndi_devi_config: par = %s%d (%p), flags = 0x%x\n",
44620Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
44630Sstevel@tonic-gate 
44640Sstevel@tonic-gate 	return (devi_config_common(dip, flags, (major_t)-1));
44650Sstevel@tonic-gate }
44660Sstevel@tonic-gate 
44670Sstevel@tonic-gate /*
44680Sstevel@tonic-gate  * Framework entry point for BUS_CONFIG_DRIVER, bound to major
44690Sstevel@tonic-gate  */
44700Sstevel@tonic-gate int
44710Sstevel@tonic-gate ndi_devi_config_driver(dev_info_t *dip, int flags, major_t major)
44720Sstevel@tonic-gate {
44730Sstevel@tonic-gate 	/* don't abuse this function */
44740Sstevel@tonic-gate 	ASSERT(major != (major_t)-1);
44750Sstevel@tonic-gate 
44760Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
44770Sstevel@tonic-gate 	    "ndi_devi_config_driver: par = %s%d (%p), flags = 0x%x\n",
44780Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
44790Sstevel@tonic-gate 
44800Sstevel@tonic-gate 	return (devi_config_common(dip, flags, major));
44810Sstevel@tonic-gate }
44820Sstevel@tonic-gate 
44830Sstevel@tonic-gate /*
44840Sstevel@tonic-gate  * called by nexus drivers to configure/unconfigure its children
44850Sstevel@tonic-gate  */
44860Sstevel@tonic-gate static int
44870Sstevel@tonic-gate devi_config_one(dev_info_t *pdip, char *devnm, dev_info_t **dipp,
44880Sstevel@tonic-gate     uint_t flags, clock_t timeout)
44890Sstevel@tonic-gate {
44900Sstevel@tonic-gate 	int circ, probed, rv;
44910Sstevel@tonic-gate 	dev_info_t *dip = NULL;
44920Sstevel@tonic-gate 	char *name, *addr, *drivername = NULL;
44930Sstevel@tonic-gate 	clock_t end_time;	/* 60 sec */
44940Sstevel@tonic-gate 
44950Sstevel@tonic-gate 	if (!NEXUS_DRV(ddi_get_driver(pdip)))
44960Sstevel@tonic-gate 		return (NDI_FAILURE);
44970Sstevel@tonic-gate 
44980Sstevel@tonic-gate 	if (MDI_PHCI(pdip)) {
44990Sstevel@tonic-gate 		/* Call mdi_ to configure the child */
45000Sstevel@tonic-gate 		rv = mdi_devi_config_one(pdip, devnm, dipp, flags, timeout);
45010Sstevel@tonic-gate 		if (rv == MDI_SUCCESS)
45020Sstevel@tonic-gate 			return (NDI_SUCCESS);
45030Sstevel@tonic-gate 
45040Sstevel@tonic-gate 		/*
45050Sstevel@tonic-gate 		 * Normally, we should return failure here.
45060Sstevel@tonic-gate 		 *
45070Sstevel@tonic-gate 		 * Leadville implemented an unfortunate fallback mechanism.
45080Sstevel@tonic-gate 		 * If a target is non-standard and scsi_vhci doesn't know
45090Sstevel@tonic-gate 		 * how to do failover, then the node is enumerated under
45100Sstevel@tonic-gate 		 * phci. Leadville specifies NDI_MDI_FALLBACK flag to
45110Sstevel@tonic-gate 		 * maintain the old behavior.
45120Sstevel@tonic-gate 		 */
45130Sstevel@tonic-gate 		if ((flags & NDI_MDI_FALLBACK) == 0)
45140Sstevel@tonic-gate 			return (NDI_FAILURE);
45150Sstevel@tonic-gate 	}
45160Sstevel@tonic-gate 
45170Sstevel@tonic-gate 	/* split name into "name@addr" parts */
45180Sstevel@tonic-gate 	i_ddi_parse_name(devnm, &name, &addr, NULL);
45190Sstevel@tonic-gate 
45200Sstevel@tonic-gate 	if (flags & NDI_PROMNAME) {
45210Sstevel@tonic-gate 		/*
45220Sstevel@tonic-gate 		 * We may have a genericname on a system that creates
45230Sstevel@tonic-gate 		 * drivername nodes (from .conf files).  Find the drivername
45240Sstevel@tonic-gate 		 * by nodeid. If we can't find a node with devnm as the
45250Sstevel@tonic-gate 		 * node name then we search by drivername.  This allows an
45260Sstevel@tonic-gate 		 * implementation to supply a genericly named boot path (disk)
45270Sstevel@tonic-gate 		 * and locate drivename nodes (sd).
45280Sstevel@tonic-gate 		 */
45290Sstevel@tonic-gate 		drivername = child_path_to_driver(pdip, name, addr);
45300Sstevel@tonic-gate 	}
45310Sstevel@tonic-gate 
45320Sstevel@tonic-gate 	if (timeout > 0) {
45330Sstevel@tonic-gate 		end_time = ddi_get_lbolt() + timeout;
45340Sstevel@tonic-gate 	}
45350Sstevel@tonic-gate 
45360Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
45370Sstevel@tonic-gate 
45380Sstevel@tonic-gate reprobe:
45390Sstevel@tonic-gate 	probed = (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
45400Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(pdip, flags);
45410Sstevel@tonic-gate 	for (;;) {
45420Sstevel@tonic-gate 		dip = find_child_by_name(pdip, name, addr);
45430Sstevel@tonic-gate 		/*
45440Sstevel@tonic-gate 		 * Search for a node bound to the drivername driver with
45450Sstevel@tonic-gate 		 * the specified "@addr".
45460Sstevel@tonic-gate 		 */
45470Sstevel@tonic-gate 		if (dip == NULL && drivername)
45480Sstevel@tonic-gate 			dip = find_child_by_driver(pdip, drivername, addr);
45490Sstevel@tonic-gate 
45500Sstevel@tonic-gate 		if (dip || timeout <= 0 || ddi_get_lbolt() >= end_time)
45510Sstevel@tonic-gate 			break;
45520Sstevel@tonic-gate 
45530Sstevel@tonic-gate 		/*
45540Sstevel@tonic-gate 		 * Wait up to end_time for asynchronous enumeration
45550Sstevel@tonic-gate 		 */
45560Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
45570Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT,
45580Sstevel@tonic-gate 		    "%s%d: waiting for child %s@%s, timeout %ld",
45590Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
45600Sstevel@tonic-gate 		    name, addr, timeout));
45610Sstevel@tonic-gate 
45620Sstevel@tonic-gate 		mutex_enter(&DEVI(pdip)->devi_lock);
45630Sstevel@tonic-gate 		(void) cv_timedwait(&DEVI(pdip)->devi_cv,
45640Sstevel@tonic-gate 		    &DEVI(pdip)->devi_lock, end_time);
45650Sstevel@tonic-gate 		mutex_exit(&DEVI(pdip)->devi_lock);
45660Sstevel@tonic-gate 		ndi_devi_enter(pdip, &circ);
45670Sstevel@tonic-gate 		(void) i_ndi_make_spec_children(pdip, flags);
45680Sstevel@tonic-gate 	}
45690Sstevel@tonic-gate 
45700Sstevel@tonic-gate 	if ((dip == NULL) && probed && (flags & NDI_CONFIG_REPROBE) &&
45710Sstevel@tonic-gate 	    i_ddi_io_initialized()) {
45720Sstevel@tonic-gate 		/*
45730Sstevel@tonic-gate 		 * reenumerate .conf nodes and probe again
45740Sstevel@tonic-gate 		 */
45750Sstevel@tonic-gate 		mutex_enter(&DEVI(pdip)->devi_lock);
45760Sstevel@tonic-gate 		DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
45770Sstevel@tonic-gate 		mutex_exit(&DEVI(pdip)->devi_lock);
45780Sstevel@tonic-gate 		goto reprobe;
45790Sstevel@tonic-gate 	}
45800Sstevel@tonic-gate 
45810Sstevel@tonic-gate 	if (addr[0] != '\0')
45820Sstevel@tonic-gate 		*(addr - 1) = '@';
45830Sstevel@tonic-gate 
45840Sstevel@tonic-gate 	if (dip == NULL || devi_attach_node(dip, flags) != NDI_SUCCESS) {
45850Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
45860Sstevel@tonic-gate 		return (NDI_FAILURE);
45870Sstevel@tonic-gate 	}
45880Sstevel@tonic-gate 
45890Sstevel@tonic-gate 	*dipp = dip;
45900Sstevel@tonic-gate 	ndi_hold_devi(dip);
45910Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
45920Sstevel@tonic-gate 	return (NDI_SUCCESS);
45930Sstevel@tonic-gate }
45940Sstevel@tonic-gate 
45950Sstevel@tonic-gate /*
45960Sstevel@tonic-gate  * Enumerate and attach a child specified by name 'devnm'.
45970Sstevel@tonic-gate  * Called by devfs lookup and DR to perform a BUS_CONFIG_ONE.
45980Sstevel@tonic-gate  * Note: devfs does not make use of NDI_CONFIG to configure
45990Sstevel@tonic-gate  * an entire branch.
46000Sstevel@tonic-gate  */
46010Sstevel@tonic-gate int
46020Sstevel@tonic-gate ndi_devi_config_one(dev_info_t *dip, char *devnm, dev_info_t **dipp, int flags)
46030Sstevel@tonic-gate {
46040Sstevel@tonic-gate 	int error;
46050Sstevel@tonic-gate 	int (*f)();
46060Sstevel@tonic-gate 	int branch_event = 0;
46070Sstevel@tonic-gate 
46080Sstevel@tonic-gate 	ASSERT(dipp);
46090Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) >= DS_ATTACHED);
46100Sstevel@tonic-gate 
46110Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
46120Sstevel@tonic-gate 	    "ndi_devi_config_one: par = %s%d (%p), child = %s\n",
46130Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, devnm));
46140Sstevel@tonic-gate 
46150Sstevel@tonic-gate 	if (pm_pre_config(dip, devnm) != DDI_SUCCESS)
46160Sstevel@tonic-gate 		return (NDI_FAILURE);
46170Sstevel@tonic-gate 
46180Sstevel@tonic-gate 	if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
46190Sstevel@tonic-gate 	    (flags & NDI_CONFIG)) {
46200Sstevel@tonic-gate 		flags |= NDI_BRANCH_EVENT_OP;
46210Sstevel@tonic-gate 		branch_event = 1;
46220Sstevel@tonic-gate 	}
46230Sstevel@tonic-gate 
46240Sstevel@tonic-gate 	if ((DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
46250Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
46260Sstevel@tonic-gate 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
46270Sstevel@tonic-gate 		error = devi_config_one(dip, devnm, dipp, flags, 0);
46280Sstevel@tonic-gate 	} else {
46290Sstevel@tonic-gate 		/* call bus_config entry point */
46300Sstevel@tonic-gate 		error = (*f)(dip, flags, BUS_CONFIG_ONE, (void *)devnm, dipp);
46310Sstevel@tonic-gate 	}
46320Sstevel@tonic-gate 
46330Sstevel@tonic-gate 	if (error || (flags & NDI_CONFIG) == 0) {
46340Sstevel@tonic-gate 		pm_post_config(dip, devnm);
46350Sstevel@tonic-gate 		return (error);
46360Sstevel@tonic-gate 	}
46370Sstevel@tonic-gate 
46380Sstevel@tonic-gate 	/*
46390Sstevel@tonic-gate 	 * DR usage ((i.e. call with NDI_CONFIG) recursively configures
46400Sstevel@tonic-gate 	 * grandchildren, performing a BUS_CONFIG_ALL from the node attached
46410Sstevel@tonic-gate 	 * by the BUS_CONFIG_ONE.
46420Sstevel@tonic-gate 	 */
46430Sstevel@tonic-gate 	ASSERT(*dipp);
46440Sstevel@tonic-gate 
46450Sstevel@tonic-gate 	error = devi_config_common(*dipp, flags, (major_t)-1);
46460Sstevel@tonic-gate 
46470Sstevel@tonic-gate 	pm_post_config(dip, devnm);
46480Sstevel@tonic-gate 
46490Sstevel@tonic-gate 	if (branch_event)
46500Sstevel@tonic-gate 		(void) i_log_devfs_branch_add(*dipp);
46510Sstevel@tonic-gate 
46520Sstevel@tonic-gate 	return (error);
46530Sstevel@tonic-gate }
46540Sstevel@tonic-gate 
46550Sstevel@tonic-gate 
46560Sstevel@tonic-gate /*
46570Sstevel@tonic-gate  * Enumerate and attach a child specified by name 'devnm'.
46580Sstevel@tonic-gate  * Called during configure the OBP options. This configures
46590Sstevel@tonic-gate  * only one node.
46600Sstevel@tonic-gate  */
46610Sstevel@tonic-gate static int
46620Sstevel@tonic-gate ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
46630Sstevel@tonic-gate     dev_info_t **childp, int flags)
46640Sstevel@tonic-gate {
46650Sstevel@tonic-gate 	int error;
46660Sstevel@tonic-gate 	int (*f)();
46670Sstevel@tonic-gate 
46680Sstevel@tonic-gate 	ASSERT(childp);
46690Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(parent) >= DS_ATTACHED);
46700Sstevel@tonic-gate 
46710Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_config_obp_args: "
46720Sstevel@tonic-gate 	    "par = %s%d (%p), child = %s\n", ddi_driver_name(parent),
46730Sstevel@tonic-gate 	    ddi_get_instance(parent), (void *)parent, devnm));
46740Sstevel@tonic-gate 
46750Sstevel@tonic-gate 	if ((DEVI(parent)->devi_ops->devo_bus_ops == NULL) ||
46760Sstevel@tonic-gate 	    (DEVI(parent)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
46770Sstevel@tonic-gate 	    (f = DEVI(parent)->devi_ops->devo_bus_ops->bus_config) == NULL) {
46780Sstevel@tonic-gate 		error = NDI_FAILURE;
46790Sstevel@tonic-gate 	} else {
46800Sstevel@tonic-gate 		/* call bus_config entry point */
46810Sstevel@tonic-gate 		error = (*f)(parent, flags,
46820Sstevel@tonic-gate 		    BUS_CONFIG_OBP_ARGS, (void *)devnm, childp);
46830Sstevel@tonic-gate 	}
46840Sstevel@tonic-gate 	return (error);
46850Sstevel@tonic-gate }
46860Sstevel@tonic-gate 
46870Sstevel@tonic-gate 
46880Sstevel@tonic-gate /*
46890Sstevel@tonic-gate  * detach a node with parent already held busy
46900Sstevel@tonic-gate  */
46910Sstevel@tonic-gate static int
46920Sstevel@tonic-gate devi_detach_node(dev_info_t *dip, uint_t flags)
46930Sstevel@tonic-gate {
46940Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
46950Sstevel@tonic-gate 	int ret = NDI_SUCCESS;
46960Sstevel@tonic-gate 	ddi_eventcookie_t cookie;
46970Sstevel@tonic-gate 
46980Sstevel@tonic-gate 	if (flags & NDI_POST_EVENT) {
46990Sstevel@tonic-gate 		if (pdip && i_ddi_node_state(pdip) >= DS_ATTACHED) {
47000Sstevel@tonic-gate 			if (ddi_get_eventcookie(dip, DDI_DEVI_REMOVE_EVENT,
47010Sstevel@tonic-gate 			    &cookie) == NDI_SUCCESS)
47020Sstevel@tonic-gate 				(void) ndi_post_event(dip, dip, cookie, NULL);
47030Sstevel@tonic-gate 		}
47040Sstevel@tonic-gate 	}
47050Sstevel@tonic-gate 
47060Sstevel@tonic-gate 	if (i_ddi_detachchild(dip, flags) != DDI_SUCCESS)
47070Sstevel@tonic-gate 		return (NDI_FAILURE);
47080Sstevel@tonic-gate 
47090Sstevel@tonic-gate 	if (flags & NDI_AUTODETACH)
47100Sstevel@tonic-gate 		return (NDI_SUCCESS);
47110Sstevel@tonic-gate 
47120Sstevel@tonic-gate 	/*
47130Sstevel@tonic-gate 	 * For DR, even bound nodes may need to have offline
47140Sstevel@tonic-gate 	 * flag set.
47150Sstevel@tonic-gate 	 */
47160Sstevel@tonic-gate 	if (flags & NDI_DEVI_OFFLINE) {
4717495Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
47180Sstevel@tonic-gate 		DEVI_SET_DEVICE_OFFLINE(dip);
4719495Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
47200Sstevel@tonic-gate 	}
47210Sstevel@tonic-gate 
47220Sstevel@tonic-gate 	if (i_ddi_node_state(dip) == DS_INITIALIZED) {
47230Sstevel@tonic-gate 		char *path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
47240Sstevel@tonic-gate 		(void) ddi_pathname(dip, path);
47250Sstevel@tonic-gate 		if (flags & NDI_DEVI_OFFLINE)
47260Sstevel@tonic-gate 			i_ndi_devi_report_status_change(dip, path);
47270Sstevel@tonic-gate 
47280Sstevel@tonic-gate 		if (need_remove_event(dip, flags)) {
47290Sstevel@tonic-gate 			(void) i_log_devfs_remove_devinfo(path,
47300Sstevel@tonic-gate 			    i_ddi_devi_class(dip),
47310Sstevel@tonic-gate 			    (char *)ddi_driver_name(dip),
47320Sstevel@tonic-gate 			    ddi_get_instance(dip),
47330Sstevel@tonic-gate 			    flags);
4734495Scth 			mutex_enter(&(DEVI(dip)->devi_lock));
47350Sstevel@tonic-gate 			DEVI_SET_EVREMOVE(dip);
4736495Scth 			mutex_exit(&(DEVI(dip)->devi_lock));
47370Sstevel@tonic-gate 		}
47380Sstevel@tonic-gate 		kmem_free(path, MAXPATHLEN);
47390Sstevel@tonic-gate 	}
47400Sstevel@tonic-gate 
47410Sstevel@tonic-gate 	if (flags & (NDI_UNCONFIG | NDI_DEVI_REMOVE)) {
47420Sstevel@tonic-gate 		ret = ddi_uninitchild(dip);
47430Sstevel@tonic-gate 		if (ret == NDI_SUCCESS) {
47440Sstevel@tonic-gate 			/*
47450Sstevel@tonic-gate 			 * Remove uninitialized pseudo nodes because
47460Sstevel@tonic-gate 			 * system props are lost and the node cannot be
47470Sstevel@tonic-gate 			 * reattached.
47480Sstevel@tonic-gate 			 */
47490Sstevel@tonic-gate 			if (!ndi_dev_is_persistent_node(dip))
47500Sstevel@tonic-gate 				flags |= NDI_DEVI_REMOVE;
47510Sstevel@tonic-gate 
47520Sstevel@tonic-gate 			if (flags & NDI_DEVI_REMOVE)
47530Sstevel@tonic-gate 				ret = ddi_remove_child(dip, 0);
47540Sstevel@tonic-gate 		}
47550Sstevel@tonic-gate 	}
47560Sstevel@tonic-gate 
47570Sstevel@tonic-gate 	return (ret);
47580Sstevel@tonic-gate }
47590Sstevel@tonic-gate 
47600Sstevel@tonic-gate /*
47610Sstevel@tonic-gate  * unconfigure immediate children of bus nexus device
47620Sstevel@tonic-gate  */
47630Sstevel@tonic-gate static int
47640Sstevel@tonic-gate unconfig_immediate_children(
47650Sstevel@tonic-gate 	dev_info_t *dip,
47660Sstevel@tonic-gate 	dev_info_t **dipp,
47670Sstevel@tonic-gate 	int flags,
47680Sstevel@tonic-gate 	major_t major)
47690Sstevel@tonic-gate {
47700Sstevel@tonic-gate 	int rv = NDI_SUCCESS, circ;
47710Sstevel@tonic-gate 	dev_info_t *child;
47720Sstevel@tonic-gate 
47730Sstevel@tonic-gate 	ASSERT(dipp == NULL || *dipp == NULL);
47740Sstevel@tonic-gate 
47750Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
47760Sstevel@tonic-gate 	child = ddi_get_child(dip);
47770Sstevel@tonic-gate 	while (child) {
47780Sstevel@tonic-gate 		dev_info_t *next = ddi_get_next_sibling(child);
47790Sstevel@tonic-gate 		if ((major != (major_t)-1) &&
47800Sstevel@tonic-gate 		    (major != ddi_driver_major(child))) {
47810Sstevel@tonic-gate 			child = next;
47820Sstevel@tonic-gate 			continue;
47830Sstevel@tonic-gate 		}
47840Sstevel@tonic-gate 
47850Sstevel@tonic-gate 		/* skip nexus nodes during autodetach */
47860Sstevel@tonic-gate 		if ((flags & NDI_AUTODETACH) && !is_leaf_node(child)) {
47870Sstevel@tonic-gate 			child = next;
47880Sstevel@tonic-gate 			continue;
47890Sstevel@tonic-gate 		}
47900Sstevel@tonic-gate 
47910Sstevel@tonic-gate 		if (devi_detach_node(child, flags) != NDI_SUCCESS) {
47920Sstevel@tonic-gate 			if (dipp && *dipp == NULL) {
47930Sstevel@tonic-gate 				ndi_hold_devi(child);
47940Sstevel@tonic-gate 				*dipp = child;
47950Sstevel@tonic-gate 			}
47960Sstevel@tonic-gate 			rv = NDI_FAILURE;
47970Sstevel@tonic-gate 		}
47980Sstevel@tonic-gate 
47990Sstevel@tonic-gate 		/*
48000Sstevel@tonic-gate 		 * Continue upon failure--best effort algorithm
48010Sstevel@tonic-gate 		 */
48020Sstevel@tonic-gate 		child = next;
48030Sstevel@tonic-gate 	}
48040Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
48050Sstevel@tonic-gate 	return (rv);
48060Sstevel@tonic-gate }
48070Sstevel@tonic-gate 
48080Sstevel@tonic-gate /*
48090Sstevel@tonic-gate  * unconfigure grand children of bus nexus device
48100Sstevel@tonic-gate  */
48110Sstevel@tonic-gate static int
48120Sstevel@tonic-gate unconfig_grand_children(
48130Sstevel@tonic-gate 	dev_info_t *dip,
48140Sstevel@tonic-gate 	dev_info_t **dipp,
48150Sstevel@tonic-gate 	int flags,
48160Sstevel@tonic-gate 	major_t major,
48170Sstevel@tonic-gate 	struct brevq_node **brevqp)
48180Sstevel@tonic-gate {
48190Sstevel@tonic-gate 	struct mt_config_handle *hdl;
48200Sstevel@tonic-gate 
48210Sstevel@tonic-gate 	if (brevqp)
48220Sstevel@tonic-gate 		*brevqp = NULL;
48230Sstevel@tonic-gate 
48240Sstevel@tonic-gate 	/* multi-threaded configuration of child nexus */
48250Sstevel@tonic-gate 	hdl = mt_config_init(dip, dipp, flags, major, MT_UNCONFIG_OP, brevqp);
48260Sstevel@tonic-gate 	mt_config_children(hdl);
48270Sstevel@tonic-gate 
48280Sstevel@tonic-gate 	return (mt_config_fini(hdl));	/* wait for threads to exit */
48290Sstevel@tonic-gate }
48300Sstevel@tonic-gate 
48310Sstevel@tonic-gate /*
48320Sstevel@tonic-gate  * Unconfigure children/descendants of the dip.
48330Sstevel@tonic-gate  *
48340Sstevel@tonic-gate  * If brevqp is not NULL, on return *brevqp is set to a queue of dip's
48350Sstevel@tonic-gate  * child devinames for which branch remove events need to be generated.
48360Sstevel@tonic-gate  */
48370Sstevel@tonic-gate static int
48380Sstevel@tonic-gate devi_unconfig_common(
48390Sstevel@tonic-gate 	dev_info_t *dip,
48400Sstevel@tonic-gate 	dev_info_t **dipp,
48410Sstevel@tonic-gate 	int flags,
48420Sstevel@tonic-gate 	major_t major,
48430Sstevel@tonic-gate 	struct brevq_node **brevqp)
48440Sstevel@tonic-gate {
48450Sstevel@tonic-gate 	int rv;
48460Sstevel@tonic-gate 	int pm_cookie;
48470Sstevel@tonic-gate 	int (*f)();
48480Sstevel@tonic-gate 	ddi_bus_config_op_t bus_op;
48490Sstevel@tonic-gate 
48500Sstevel@tonic-gate 	if (dipp)
48510Sstevel@tonic-gate 		*dipp = NULL;
48520Sstevel@tonic-gate 	if (brevqp)
48530Sstevel@tonic-gate 		*brevqp = NULL;
48540Sstevel@tonic-gate 
48550Sstevel@tonic-gate 	/*
48560Sstevel@tonic-gate 	 * Power up the dip if it is powered off.  If the flag bit
48570Sstevel@tonic-gate 	 * NDI_AUTODETACH is set and the dip is not at its full power,
48580Sstevel@tonic-gate 	 * skip the rest of the branch.
48590Sstevel@tonic-gate 	 */
48600Sstevel@tonic-gate 	if (pm_pre_unconfig(dip, flags, &pm_cookie, NULL) != DDI_SUCCESS)
48610Sstevel@tonic-gate 		return ((flags & NDI_AUTODETACH) ? NDI_SUCCESS :
48620Sstevel@tonic-gate 		    NDI_FAILURE);
48630Sstevel@tonic-gate 
48640Sstevel@tonic-gate 	/*
48650Sstevel@tonic-gate 	 * Some callers, notably SCSI, need to clear out the devfs
48660Sstevel@tonic-gate 	 * cache together with the unconfig to prevent stale entries.
48670Sstevel@tonic-gate 	 */
48680Sstevel@tonic-gate 	if (flags & NDI_DEVFS_CLEAN)
48690Sstevel@tonic-gate 		(void) devfs_clean(dip, NULL, 0);
48700Sstevel@tonic-gate 
48710Sstevel@tonic-gate 	rv = unconfig_grand_children(dip, dipp, flags, major, brevqp);
48720Sstevel@tonic-gate 
48730Sstevel@tonic-gate 	if ((rv != NDI_SUCCESS) && ((flags & NDI_AUTODETACH) == 0)) {
48740Sstevel@tonic-gate 		if (brevqp && *brevqp) {
48750Sstevel@tonic-gate 			log_and_free_br_events_on_grand_children(dip, *brevqp);
48760Sstevel@tonic-gate 			free_brevq(*brevqp);
48770Sstevel@tonic-gate 			*brevqp = NULL;
48780Sstevel@tonic-gate 		}
48790Sstevel@tonic-gate 		pm_post_unconfig(dip, pm_cookie, NULL);
48800Sstevel@tonic-gate 		return (rv);
48810Sstevel@tonic-gate 	}
48820Sstevel@tonic-gate 
48830Sstevel@tonic-gate 	if (dipp && *dipp) {
48840Sstevel@tonic-gate 		ndi_rele_devi(*dipp);
48850Sstevel@tonic-gate 		*dipp = NULL;
48860Sstevel@tonic-gate 	}
48870Sstevel@tonic-gate 
48880Sstevel@tonic-gate 	/*
48890Sstevel@tonic-gate 	 * It is possible to have a detached nexus with children
48900Sstevel@tonic-gate 	 * and grandchildren (for example: a branch consisting
48910Sstevel@tonic-gate 	 * entirely of bound nodes.) Since the nexus is detached
48920Sstevel@tonic-gate 	 * the bus_unconfig entry point cannot be used to remove
48930Sstevel@tonic-gate 	 * or unconfigure the descendants.
48940Sstevel@tonic-gate 	 */
48950Sstevel@tonic-gate 	if (i_ddi_node_state(dip) < DS_ATTACHED ||
48960Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
48970Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
48980Sstevel@tonic-gate 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
48990Sstevel@tonic-gate 		rv = unconfig_immediate_children(dip, dipp, flags, major);
49000Sstevel@tonic-gate 	} else {
49010Sstevel@tonic-gate 		/*
49020Sstevel@tonic-gate 		 * call bus_unconfig entry point
49030Sstevel@tonic-gate 		 * It should reset nexus flags if unconfigure succeeds.
49040Sstevel@tonic-gate 		 */
49050Sstevel@tonic-gate 		bus_op = (major == (major_t)-1) ?
49060Sstevel@tonic-gate 		    BUS_UNCONFIG_ALL : BUS_UNCONFIG_DRIVER;
49070Sstevel@tonic-gate 		rv = (*f)(dip, flags, bus_op, (void *)(uintptr_t)major);
49080Sstevel@tonic-gate 	}
49090Sstevel@tonic-gate 
49100Sstevel@tonic-gate 	pm_post_unconfig(dip, pm_cookie, NULL);
49110Sstevel@tonic-gate 
49120Sstevel@tonic-gate 	if (brevqp && *brevqp)
49130Sstevel@tonic-gate 		cleanup_br_events_on_grand_children(dip, brevqp);
49140Sstevel@tonic-gate 
49150Sstevel@tonic-gate 	return (rv);
49160Sstevel@tonic-gate }
49170Sstevel@tonic-gate 
49180Sstevel@tonic-gate /*
49190Sstevel@tonic-gate  * called by devfs/framework to unconfigure children bound to major
49200Sstevel@tonic-gate  * If NDI_AUTODETACH is specified, this is invoked by either the
49210Sstevel@tonic-gate  * moduninstall daemon or the modunload -i 0 command.
49220Sstevel@tonic-gate  */
49230Sstevel@tonic-gate int
49240Sstevel@tonic-gate ndi_devi_unconfig_driver(dev_info_t *dip, int flags, major_t major)
49250Sstevel@tonic-gate {
49260Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
49270Sstevel@tonic-gate 	    "ndi_devi_unconfig_driver: par = %s%d (%p), flags = 0x%x\n",
49280Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
49290Sstevel@tonic-gate 
49300Sstevel@tonic-gate 	return (devi_unconfig_common(dip, NULL, flags, major, NULL));
49310Sstevel@tonic-gate }
49320Sstevel@tonic-gate 
49330Sstevel@tonic-gate int
49340Sstevel@tonic-gate ndi_devi_unconfig(dev_info_t *dip, int flags)
49350Sstevel@tonic-gate {
49360Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
49370Sstevel@tonic-gate 	    "ndi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
49380Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
49390Sstevel@tonic-gate 
49400Sstevel@tonic-gate 	return (devi_unconfig_common(dip, NULL, flags, (major_t)-1, NULL));
49410Sstevel@tonic-gate }
49420Sstevel@tonic-gate 
49430Sstevel@tonic-gate int
49440Sstevel@tonic-gate e_ddi_devi_unconfig(dev_info_t *dip, dev_info_t **dipp, int flags)
49450Sstevel@tonic-gate {
49460Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
49470Sstevel@tonic-gate 	    "e_ddi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
49480Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
49490Sstevel@tonic-gate 
49500Sstevel@tonic-gate 	return (devi_unconfig_common(dip, dipp, flags, (major_t)-1, NULL));
49510Sstevel@tonic-gate }
49520Sstevel@tonic-gate 
49530Sstevel@tonic-gate /*
49540Sstevel@tonic-gate  * Unconfigure child by name
49550Sstevel@tonic-gate  */
49560Sstevel@tonic-gate static int
49570Sstevel@tonic-gate devi_unconfig_one(dev_info_t *pdip, char *devnm, int flags)
49580Sstevel@tonic-gate {
49590Sstevel@tonic-gate 	int rv, circ;
49600Sstevel@tonic-gate 	dev_info_t *child;
49610Sstevel@tonic-gate 
49620Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
49630Sstevel@tonic-gate 	child = ndi_devi_findchild(pdip, devnm);
49640Sstevel@tonic-gate 	if (child == NULL) {
49650Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
49660Sstevel@tonic-gate 		    "devi_unconfig_one: %s not found\n", devnm));
49670Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
49680Sstevel@tonic-gate 		return (NDI_SUCCESS);
49690Sstevel@tonic-gate 	}
49700Sstevel@tonic-gate 	rv = devi_detach_node(child, flags);
49710Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
49720Sstevel@tonic-gate 	return (rv);
49730Sstevel@tonic-gate }
49740Sstevel@tonic-gate 
49750Sstevel@tonic-gate int
49760Sstevel@tonic-gate ndi_devi_unconfig_one(
49770Sstevel@tonic-gate 	dev_info_t *pdip,
49780Sstevel@tonic-gate 	char *devnm,
49790Sstevel@tonic-gate 	dev_info_t **dipp,
49800Sstevel@tonic-gate 	int flags)
49810Sstevel@tonic-gate {
49820Sstevel@tonic-gate 	int (*f)();
49830Sstevel@tonic-gate 	int circ, rv;
49840Sstevel@tonic-gate 	int pm_cookie;
49850Sstevel@tonic-gate 	dev_info_t *child;
49860Sstevel@tonic-gate 	struct brevq_node *brevq = NULL;
49870Sstevel@tonic-gate 
49880Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(pdip) >= DS_ATTACHED);
49890Sstevel@tonic-gate 
49900Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
49910Sstevel@tonic-gate 	    "ndi_devi_unconfig_one: par = %s%d (%p), child = %s\n",
49920Sstevel@tonic-gate 	    ddi_driver_name(pdip), ddi_get_instance(pdip),
49930Sstevel@tonic-gate 	    (void *)pdip, devnm));
49940Sstevel@tonic-gate 
49950Sstevel@tonic-gate 	if (pm_pre_unconfig(pdip, flags, &pm_cookie, devnm) != DDI_SUCCESS)
49960Sstevel@tonic-gate 		return (NDI_FAILURE);
49970Sstevel@tonic-gate 
49980Sstevel@tonic-gate 	if (dipp)
49990Sstevel@tonic-gate 		*dipp = NULL;
50000Sstevel@tonic-gate 
50010Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
50020Sstevel@tonic-gate 	child = ndi_devi_findchild(pdip, devnm);
50030Sstevel@tonic-gate 	if (child == NULL) {
50040Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_unconfig_one: %s"
50050Sstevel@tonic-gate 		    " not found\n", devnm));
50060Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
50070Sstevel@tonic-gate 		pm_post_unconfig(pdip, pm_cookie, devnm);
50080Sstevel@tonic-gate 		return (NDI_SUCCESS);
50090Sstevel@tonic-gate 	}
50100Sstevel@tonic-gate 
50110Sstevel@tonic-gate 	/*
50120Sstevel@tonic-gate 	 * Unconfigure children/descendants of named child
50130Sstevel@tonic-gate 	 */
50140Sstevel@tonic-gate 	rv = devi_unconfig_branch(child, dipp, flags | NDI_UNCONFIG, &brevq);
50150Sstevel@tonic-gate 	if (rv != NDI_SUCCESS)
50160Sstevel@tonic-gate 		goto out;
50170Sstevel@tonic-gate 
50180Sstevel@tonic-gate 	init_bound_node_ev(pdip, child, flags);
50190Sstevel@tonic-gate 
50200Sstevel@tonic-gate 	if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
50210Sstevel@tonic-gate 	    (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
50220Sstevel@tonic-gate 	    (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
50230Sstevel@tonic-gate 		rv = devi_detach_node(child, flags);
50240Sstevel@tonic-gate 	} else {
50250Sstevel@tonic-gate 		/* call bus_config entry point */
50260Sstevel@tonic-gate 		rv = (*f)(pdip, flags, BUS_UNCONFIG_ONE, (void *)devnm);
50270Sstevel@tonic-gate 	}
50280Sstevel@tonic-gate 
50290Sstevel@tonic-gate 	if (brevq) {
50300Sstevel@tonic-gate 		if (rv != NDI_SUCCESS)
50310Sstevel@tonic-gate 			log_and_free_brevq_dip(child, brevq);
50320Sstevel@tonic-gate 		else
50330Sstevel@tonic-gate 			free_brevq(brevq);
50340Sstevel@tonic-gate 	}
50350Sstevel@tonic-gate 
50360Sstevel@tonic-gate 	if (dipp && rv != NDI_SUCCESS) {
50370Sstevel@tonic-gate 		ndi_hold_devi(child);
50380Sstevel@tonic-gate 		ASSERT(*dipp == NULL);
50390Sstevel@tonic-gate 		*dipp = child;
50400Sstevel@tonic-gate 	}
50410Sstevel@tonic-gate 
50420Sstevel@tonic-gate out:
50430Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
50440Sstevel@tonic-gate 	pm_post_unconfig(pdip, pm_cookie, devnm);
50450Sstevel@tonic-gate 
50460Sstevel@tonic-gate 	return (rv);
50470Sstevel@tonic-gate }
50480Sstevel@tonic-gate 
50490Sstevel@tonic-gate struct async_arg {
50500Sstevel@tonic-gate 	dev_info_t *dip;
50510Sstevel@tonic-gate 	uint_t flags;
50520Sstevel@tonic-gate };
50530Sstevel@tonic-gate 
50540Sstevel@tonic-gate /*
50550Sstevel@tonic-gate  * Common async handler for:
50560Sstevel@tonic-gate  *	ndi_devi_bind_driver_async
50570Sstevel@tonic-gate  *	ndi_devi_online_async
50580Sstevel@tonic-gate  */
50590Sstevel@tonic-gate static int
50600Sstevel@tonic-gate i_ndi_devi_async_common(dev_info_t *dip, uint_t flags, void (*func)())
50610Sstevel@tonic-gate {
50620Sstevel@tonic-gate 	int tqflag;
50630Sstevel@tonic-gate 	int kmflag;
50640Sstevel@tonic-gate 	struct async_arg *arg;
50650Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
50660Sstevel@tonic-gate 
50670Sstevel@tonic-gate 	ASSERT(pdip);
50680Sstevel@tonic-gate 	ASSERT(DEVI(pdip)->devi_taskq);
50690Sstevel@tonic-gate 	ASSERT(ndi_dev_is_persistent_node(dip));
50700Sstevel@tonic-gate 
50710Sstevel@tonic-gate 	if (flags & NDI_NOSLEEP) {
50720Sstevel@tonic-gate 		kmflag = KM_NOSLEEP;
50730Sstevel@tonic-gate 		tqflag = TQ_NOSLEEP;
50740Sstevel@tonic-gate 	} else {
50750Sstevel@tonic-gate 		kmflag = KM_SLEEP;
50760Sstevel@tonic-gate 		tqflag = TQ_SLEEP;
50770Sstevel@tonic-gate 	}
50780Sstevel@tonic-gate 
50790Sstevel@tonic-gate 	arg = kmem_alloc(sizeof (*arg), kmflag);
50800Sstevel@tonic-gate 	if (arg == NULL)
50810Sstevel@tonic-gate 		goto fail;
50820Sstevel@tonic-gate 
50830Sstevel@tonic-gate 	arg->flags = flags;
50840Sstevel@tonic-gate 	arg->dip = dip;
50850Sstevel@tonic-gate 	if (ddi_taskq_dispatch(DEVI(pdip)->devi_taskq, func, arg, tqflag) ==
50860Sstevel@tonic-gate 	    DDI_SUCCESS) {
50870Sstevel@tonic-gate 		return (NDI_SUCCESS);
50880Sstevel@tonic-gate 	}
50890Sstevel@tonic-gate 
50900Sstevel@tonic-gate fail:
50910Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "%s%d: ddi_taskq_dispatch failed",
50920Sstevel@tonic-gate 	    ddi_driver_name(pdip), ddi_get_instance(pdip)));
50930Sstevel@tonic-gate 
50940Sstevel@tonic-gate 	if (arg)
50950Sstevel@tonic-gate 		kmem_free(arg, sizeof (*arg));
50960Sstevel@tonic-gate 	return (NDI_FAILURE);
50970Sstevel@tonic-gate }
50980Sstevel@tonic-gate 
50990Sstevel@tonic-gate static void
51000Sstevel@tonic-gate i_ndi_devi_bind_driver_cb(struct async_arg *arg)
51010Sstevel@tonic-gate {
51020Sstevel@tonic-gate 	(void) ndi_devi_bind_driver(arg->dip, arg->flags);
51030Sstevel@tonic-gate 	kmem_free(arg, sizeof (*arg));
51040Sstevel@tonic-gate }
51050Sstevel@tonic-gate 
51060Sstevel@tonic-gate int
51070Sstevel@tonic-gate ndi_devi_bind_driver_async(dev_info_t *dip, uint_t flags)
51080Sstevel@tonic-gate {
51090Sstevel@tonic-gate 	return (i_ndi_devi_async_common(dip, flags,
51100Sstevel@tonic-gate 	    (void (*)())i_ndi_devi_bind_driver_cb));
51110Sstevel@tonic-gate }
51120Sstevel@tonic-gate 
51130Sstevel@tonic-gate /*
51140Sstevel@tonic-gate  * place the devinfo in the ONLINE state.
51150Sstevel@tonic-gate  */
51160Sstevel@tonic-gate int
51170Sstevel@tonic-gate ndi_devi_online(dev_info_t *dip, uint_t flags)
51180Sstevel@tonic-gate {
51190Sstevel@tonic-gate 	int circ, rv;
51200Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
51210Sstevel@tonic-gate 	int branch_event = 0;
51220Sstevel@tonic-gate 
51230Sstevel@tonic-gate 	ASSERT(pdip);
51240Sstevel@tonic-gate 
51250Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_online: %s%d (%p)\n",
51260Sstevel@tonic-gate 		ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
51270Sstevel@tonic-gate 
51280Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
51290Sstevel@tonic-gate 	/* bind child before merging .conf nodes */
51300Sstevel@tonic-gate 	rv = i_ndi_config_node(dip, DS_BOUND, flags);
51310Sstevel@tonic-gate 	if (rv != NDI_SUCCESS) {
51320Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
51330Sstevel@tonic-gate 		return (rv);
51340Sstevel@tonic-gate 	}
51350Sstevel@tonic-gate 
51360Sstevel@tonic-gate 	/* merge .conf properties */
51370Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(pdip, flags);
51380Sstevel@tonic-gate 
51390Sstevel@tonic-gate 	flags |= (NDI_DEVI_ONLINE | NDI_CONFIG);
51400Sstevel@tonic-gate 
51410Sstevel@tonic-gate 	if (flags & NDI_NO_EVENT) {
51420Sstevel@tonic-gate 		/*
51430Sstevel@tonic-gate 		 * Caller is specifically asking for not to generate an event.
51440Sstevel@tonic-gate 		 * Set the following flag so that devi_attach_node() don't
51450Sstevel@tonic-gate 		 * change the event state.
51460Sstevel@tonic-gate 		 */
51470Sstevel@tonic-gate 		flags |= NDI_NO_EVENT_STATE_CHNG;
51480Sstevel@tonic-gate 	}
51490Sstevel@tonic-gate 
51500Sstevel@tonic-gate 	if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
51510Sstevel@tonic-gate 	    ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip))) {
51520Sstevel@tonic-gate 		flags |= NDI_BRANCH_EVENT_OP;
51530Sstevel@tonic-gate 		branch_event = 1;
51540Sstevel@tonic-gate 	}
51550Sstevel@tonic-gate 
51560Sstevel@tonic-gate 	/*
51570Sstevel@tonic-gate 	 * devi_attach_node() may remove dip on failure
51580Sstevel@tonic-gate 	 */
51590Sstevel@tonic-gate 	if ((rv = devi_attach_node(dip, flags)) == NDI_SUCCESS) {
51600Sstevel@tonic-gate 		if ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip)) {
51610Sstevel@tonic-gate 			(void) ndi_devi_config(dip, flags);
51620Sstevel@tonic-gate 		}
51630Sstevel@tonic-gate 
51640Sstevel@tonic-gate 		if (branch_event)
51650Sstevel@tonic-gate 			(void) i_log_devfs_branch_add(dip);
51660Sstevel@tonic-gate 	}
51670Sstevel@tonic-gate 
51680Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
51690Sstevel@tonic-gate 
51700Sstevel@tonic-gate 	/*
51710Sstevel@tonic-gate 	 * Notify devfs that we have a new node. Devfs needs to invalidate
51720Sstevel@tonic-gate 	 * cached directory contents.
51730Sstevel@tonic-gate 	 *
51740Sstevel@tonic-gate 	 * For PCMCIA devices, it is possible the pdip is not fully
51750Sstevel@tonic-gate 	 * attached. In this case, calling back into devfs will
51760Sstevel@tonic-gate 	 * result in a loop or assertion error. Hence, the check
51770Sstevel@tonic-gate 	 * on node state.
51780Sstevel@tonic-gate 	 *
51790Sstevel@tonic-gate 	 * If we own parent lock, this is part of a branch operation.
51800Sstevel@tonic-gate 	 * We skip the devfs_clean() step because the cache invalidation
51810Sstevel@tonic-gate 	 * is done higher up in the device tree.
51820Sstevel@tonic-gate 	 */
51830Sstevel@tonic-gate 	if (rv == NDI_SUCCESS && i_ddi_node_state(pdip) == DS_READY &&
51840Sstevel@tonic-gate 	    !DEVI_BUSY_OWNED(pdip))
51850Sstevel@tonic-gate 		(void) devfs_clean(pdip, NULL, 0);
51860Sstevel@tonic-gate 	return (rv);
51870Sstevel@tonic-gate }
51880Sstevel@tonic-gate 
51890Sstevel@tonic-gate static void
51900Sstevel@tonic-gate i_ndi_devi_online_cb(struct async_arg *arg)
51910Sstevel@tonic-gate {
51920Sstevel@tonic-gate 	(void) ndi_devi_online(arg->dip, arg->flags);
51930Sstevel@tonic-gate 	kmem_free(arg, sizeof (*arg));
51940Sstevel@tonic-gate }
51950Sstevel@tonic-gate 
51960Sstevel@tonic-gate int
51970Sstevel@tonic-gate ndi_devi_online_async(dev_info_t *dip, uint_t flags)
51980Sstevel@tonic-gate {
51990Sstevel@tonic-gate 	/* mark child as need config if requested. */
5200495Scth 	if (flags & NDI_CONFIG) {
5201495Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
52020Sstevel@tonic-gate 		DEVI_SET_NDI_CONFIG(dip);
5203495Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
5204495Scth 	}
52050Sstevel@tonic-gate 
52060Sstevel@tonic-gate 	return (i_ndi_devi_async_common(dip, flags,
52070Sstevel@tonic-gate 	    (void (*)())i_ndi_devi_online_cb));
52080Sstevel@tonic-gate }
52090Sstevel@tonic-gate 
52100Sstevel@tonic-gate /*
52110Sstevel@tonic-gate  * Take a device node Offline
52120Sstevel@tonic-gate  * To take a device Offline means to detach the device instance from
52130Sstevel@tonic-gate  * the driver and prevent devfs requests from re-attaching the device
52140Sstevel@tonic-gate  * instance.
52150Sstevel@tonic-gate  *
52160Sstevel@tonic-gate  * The flag NDI_DEVI_REMOVE causes removes the device node from
52170Sstevel@tonic-gate  * the driver list and the device tree. In this case, the device
52180Sstevel@tonic-gate  * is assumed to be removed from the system.
52190Sstevel@tonic-gate  */
52200Sstevel@tonic-gate int
52210Sstevel@tonic-gate ndi_devi_offline(dev_info_t *dip, uint_t flags)
52220Sstevel@tonic-gate {
52230Sstevel@tonic-gate 	int circ, rval = 0;
52240Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
52250Sstevel@tonic-gate 	struct brevq_node *brevq = NULL;
52260Sstevel@tonic-gate 
52270Sstevel@tonic-gate 	ASSERT(pdip);
52280Sstevel@tonic-gate 
52290Sstevel@tonic-gate 	flags |= NDI_DEVI_OFFLINE;
52300Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
52310Sstevel@tonic-gate 	if (i_ddi_node_state(dip) == DS_READY) {
52320Sstevel@tonic-gate 		/*
52330Sstevel@tonic-gate 		 * If dip is in DS_READY state, there may be cached dv_nodes
52340Sstevel@tonic-gate 		 * referencing this dip, so we invoke devfs code path.
52350Sstevel@tonic-gate 		 * Note that we must release busy changing on pdip to
52360Sstevel@tonic-gate 		 * avoid deadlock against devfs.
52370Sstevel@tonic-gate 		 */
52380Sstevel@tonic-gate 		char *devname = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
52390Sstevel@tonic-gate 		(void) ddi_deviname(dip, devname);
52400Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
52410Sstevel@tonic-gate 
52420Sstevel@tonic-gate 		/*
52430Sstevel@tonic-gate 		 * If we own parent lock, this is part of a branch
52440Sstevel@tonic-gate 		 * operation. We skip the devfs_clean() step.
52450Sstevel@tonic-gate 		 */
52460Sstevel@tonic-gate 		if (!DEVI_BUSY_OWNED(pdip))
52470Sstevel@tonic-gate 			rval = devfs_clean(pdip, devname + 1, DV_CLEAN_FORCE);
52480Sstevel@tonic-gate 		kmem_free(devname, MAXNAMELEN + 1);
52490Sstevel@tonic-gate 
52500Sstevel@tonic-gate 		if (rval == 0)
52510Sstevel@tonic-gate 			rval = devi_unconfig_branch(dip, NULL,
52520Sstevel@tonic-gate 			    flags|NDI_UNCONFIG, &brevq);
52530Sstevel@tonic-gate 		if (rval)
52540Sstevel@tonic-gate 			return (NDI_FAILURE);
52550Sstevel@tonic-gate 
52560Sstevel@tonic-gate 		ndi_devi_enter(pdip, &circ);
52570Sstevel@tonic-gate 	}
52580Sstevel@tonic-gate 
52590Sstevel@tonic-gate 	init_bound_node_ev(pdip, dip, flags);
52600Sstevel@tonic-gate 
52610Sstevel@tonic-gate 	rval = devi_detach_node(dip, flags);
52620Sstevel@tonic-gate 	if (brevq) {
52630Sstevel@tonic-gate 		if (rval != NDI_SUCCESS)
52640Sstevel@tonic-gate 			log_and_free_brevq_dip(dip, brevq);
52650Sstevel@tonic-gate 		else
52660Sstevel@tonic-gate 			free_brevq(brevq);
52670Sstevel@tonic-gate 	}
52680Sstevel@tonic-gate 
52690Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
52700Sstevel@tonic-gate 
52710Sstevel@tonic-gate 	return (rval);
52720Sstevel@tonic-gate }
52730Sstevel@tonic-gate 
52740Sstevel@tonic-gate /*
52750Sstevel@tonic-gate  * Find the child dev_info node of parent nexus 'p' whose name
52760Sstevel@tonic-gate  * matches "cname@caddr".  Recommend use of ndi_devi_findchild() instead.
52770Sstevel@tonic-gate  */
52780Sstevel@tonic-gate dev_info_t *
52790Sstevel@tonic-gate ndi_devi_find(dev_info_t *pdip, char *cname, char *caddr)
52800Sstevel@tonic-gate {
52810Sstevel@tonic-gate 	dev_info_t *child;
52820Sstevel@tonic-gate 	int circ;
52830Sstevel@tonic-gate 
52840Sstevel@tonic-gate 	if (pdip == NULL || cname == NULL || caddr == NULL)
52850Sstevel@tonic-gate 		return ((dev_info_t *)NULL);
52860Sstevel@tonic-gate 
52870Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
52880Sstevel@tonic-gate 	child = find_sibling(ddi_get_child(pdip), cname, caddr, 0, NULL);
52890Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
52900Sstevel@tonic-gate 	return (child);
52910Sstevel@tonic-gate }
52920Sstevel@tonic-gate 
52930Sstevel@tonic-gate /*
52940Sstevel@tonic-gate  * Find the child dev_info node of parent nexus 'p' whose name
52950Sstevel@tonic-gate  * matches devname "name@addr".  Permits caller to hold the parent.
52960Sstevel@tonic-gate  */
52970Sstevel@tonic-gate dev_info_t *
52980Sstevel@tonic-gate ndi_devi_findchild(dev_info_t *pdip, char *devname)
52990Sstevel@tonic-gate {
53000Sstevel@tonic-gate 	dev_info_t *child;
53010Sstevel@tonic-gate 	char	*cname, *caddr;
53020Sstevel@tonic-gate 	char	*devstr;
53030Sstevel@tonic-gate 
53040Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(pdip));
53050Sstevel@tonic-gate 
53060Sstevel@tonic-gate 	devstr = i_ddi_strdup(devname, KM_SLEEP);
53070Sstevel@tonic-gate 	i_ddi_parse_name(devstr, &cname, &caddr, NULL);
53080Sstevel@tonic-gate 
53090Sstevel@tonic-gate 	if (cname == NULL || caddr == NULL) {
53100Sstevel@tonic-gate 		kmem_free(devstr, strlen(devname)+1);
53110Sstevel@tonic-gate 		return ((dev_info_t *)NULL);
53120Sstevel@tonic-gate 	}
53130Sstevel@tonic-gate 
53140Sstevel@tonic-gate 	child = find_sibling(ddi_get_child(pdip), cname, caddr, 0, NULL);
53150Sstevel@tonic-gate 	kmem_free(devstr, strlen(devname)+1);
53160Sstevel@tonic-gate 	return (child);
53170Sstevel@tonic-gate }
53180Sstevel@tonic-gate 
53190Sstevel@tonic-gate /*
53200Sstevel@tonic-gate  * Misc. routines called by framework only
53210Sstevel@tonic-gate  */
53220Sstevel@tonic-gate 
53230Sstevel@tonic-gate /*
53240Sstevel@tonic-gate  * Clear the DEVI_MADE_CHILDREN/DEVI_ATTACHED_CHILDREN flags
53250Sstevel@tonic-gate  * if new child spec has been added.
53260Sstevel@tonic-gate  */
53270Sstevel@tonic-gate static int
53280Sstevel@tonic-gate reset_nexus_flags(dev_info_t *dip, void *arg)
53290Sstevel@tonic-gate {
5330298Scth 	struct hwc_spec	*list;
5331298Scth 	int		circ;
53320Sstevel@tonic-gate 
53330Sstevel@tonic-gate 	if (((DEVI(dip)->devi_flags & DEVI_MADE_CHILDREN) == 0) ||
53340Sstevel@tonic-gate 	    ((list = hwc_get_child_spec(dip, (major_t)(uintptr_t)arg)) == NULL))
53350Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
53360Sstevel@tonic-gate 
53370Sstevel@tonic-gate 	hwc_free_spec_list(list);
5338298Scth 
5339298Scth 	/* coordinate child state update */
5340298Scth 	ndi_devi_enter(dip, &circ);
53410Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
53420Sstevel@tonic-gate 	DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN | DEVI_ATTACHED_CHILDREN);
53430Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
5344298Scth 	ndi_devi_exit(dip, circ);
53450Sstevel@tonic-gate 
53460Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
53470Sstevel@tonic-gate }
53480Sstevel@tonic-gate 
53490Sstevel@tonic-gate /*
53500Sstevel@tonic-gate  * Helper functions, returns NULL if no memory.
53510Sstevel@tonic-gate  */
53520Sstevel@tonic-gate 
53530Sstevel@tonic-gate /*
53540Sstevel@tonic-gate  * path_to_major:
53550Sstevel@tonic-gate  *
53560Sstevel@tonic-gate  * Return an alternate driver name binding for the leaf device
53570Sstevel@tonic-gate  * of the given pathname, if there is one. The purpose of this
53580Sstevel@tonic-gate  * function is to deal with generic pathnames. The default action
53590Sstevel@tonic-gate  * for platforms that can't do this (ie: x86 or any platform that
53600Sstevel@tonic-gate  * does not have prom_finddevice functionality, which matches
53610Sstevel@tonic-gate  * nodenames and unit-addresses without the drivers participation)
53620Sstevel@tonic-gate  * is to return (major_t)-1.
53630Sstevel@tonic-gate  *
53640Sstevel@tonic-gate  * Used in loadrootmodules() in the swapgeneric module to
53650Sstevel@tonic-gate  * associate a given pathname with a given leaf driver.
53660Sstevel@tonic-gate  *
53670Sstevel@tonic-gate  */
53680Sstevel@tonic-gate major_t
53690Sstevel@tonic-gate path_to_major(char *path)
53700Sstevel@tonic-gate {
53710Sstevel@tonic-gate 	dev_info_t *dip;
53720Sstevel@tonic-gate 	char *p, *q;
53730Sstevel@tonic-gate 	dnode_t nodeid;
53740Sstevel@tonic-gate 	major_t maj;
53750Sstevel@tonic-gate 
53760Sstevel@tonic-gate 	/*
53770Sstevel@tonic-gate 	 * Get the nodeid of the given pathname, if such a mapping exists.
53780Sstevel@tonic-gate 	 */
53790Sstevel@tonic-gate 	dip = NULL;
53800Sstevel@tonic-gate 	nodeid = prom_finddevice(path);
53810Sstevel@tonic-gate 	if (nodeid != OBP_BADNODE) {
53820Sstevel@tonic-gate 		/*
53830Sstevel@tonic-gate 		 * Find the nodeid in our copy of the device tree and return
53840Sstevel@tonic-gate 		 * whatever name we used to bind this node to a driver.
53850Sstevel@tonic-gate 		 */
53860Sstevel@tonic-gate 		dip = e_ddi_nodeid_to_dip(nodeid);
53870Sstevel@tonic-gate 	}
53880Sstevel@tonic-gate 
53890Sstevel@tonic-gate 	if (dip == NULL) {
53900Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_WARN,
53910Sstevel@tonic-gate 		    "path_to_major: can't bind <%s>\n", path));
53920Sstevel@tonic-gate 		return ((major_t)-1);
53930Sstevel@tonic-gate 	}
53940Sstevel@tonic-gate 
53950Sstevel@tonic-gate 	/*
53960Sstevel@tonic-gate 	 * If we're bound to something other than the nodename,
53970Sstevel@tonic-gate 	 * note that in the message buffer and system log.
53980Sstevel@tonic-gate 	 */
53990Sstevel@tonic-gate 	p = ddi_binding_name(dip);
54000Sstevel@tonic-gate 	q = ddi_node_name(dip);
54010Sstevel@tonic-gate 	if (p && q && (strcmp(p, q) != 0))
54020Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s bound to %s\n",
54030Sstevel@tonic-gate 		    path, p));
54040Sstevel@tonic-gate 
54050Sstevel@tonic-gate 	maj = ddi_name_to_major(p);
54060Sstevel@tonic-gate 
54070Sstevel@tonic-gate 	ndi_rele_devi(dip);		/* release node held during walk */
54080Sstevel@tonic-gate 
54090Sstevel@tonic-gate 	return (maj);
54100Sstevel@tonic-gate }
54110Sstevel@tonic-gate 
54120Sstevel@tonic-gate /*
54130Sstevel@tonic-gate  * Return the held dip for the specified major and instance, attempting to do
54140Sstevel@tonic-gate  * an attach if specified. Return NULL if the devi can't be found or put in
54150Sstevel@tonic-gate  * the proper state. The caller must release the hold via ddi_release_devi if
54160Sstevel@tonic-gate  * a non-NULL value is returned.
54170Sstevel@tonic-gate  *
54180Sstevel@tonic-gate  * Some callers expect to be able to perform a hold_devi() while in a context
54190Sstevel@tonic-gate  * where using ndi_devi_enter() to ensure the hold might cause deadlock (see
54200Sstevel@tonic-gate  * open-from-attach code in consconfig_dacf.c). Such special-case callers
54210Sstevel@tonic-gate  * must ensure that an ndi_devi_enter(parent)/ndi_devi_hold() from a safe
54220Sstevel@tonic-gate  * context is already active. The hold_devi() implementation must accommodate
54230Sstevel@tonic-gate  * these callers.
54240Sstevel@tonic-gate  */
54250Sstevel@tonic-gate static dev_info_t *
54260Sstevel@tonic-gate hold_devi(major_t major, int instance, int flags)
54270Sstevel@tonic-gate {
54280Sstevel@tonic-gate 	struct devnames	*dnp;
54290Sstevel@tonic-gate 	dev_info_t	*dip;
54300Sstevel@tonic-gate 	char		*path;
54310Sstevel@tonic-gate 
54320Sstevel@tonic-gate 	if ((major >= devcnt) || (instance == -1))
54330Sstevel@tonic-gate 		return (NULL);
54340Sstevel@tonic-gate 
54350Sstevel@tonic-gate 	/* try to find the instance in the per driver list */
54360Sstevel@tonic-gate 	dnp = &(devnamesp[major]);
54370Sstevel@tonic-gate 	LOCK_DEV_OPS(&(dnp->dn_lock));
54380Sstevel@tonic-gate 	for (dip = dnp->dn_head; dip;
54390Sstevel@tonic-gate 	    dip = (dev_info_t *)DEVI(dip)->devi_next) {
54400Sstevel@tonic-gate 		/* skip node if instance field is not valid */
54410Sstevel@tonic-gate 		if (i_ddi_node_state(dip) < DS_INITIALIZED)
54420Sstevel@tonic-gate 			continue;
54430Sstevel@tonic-gate 
54440Sstevel@tonic-gate 		/* look for instance match */
54450Sstevel@tonic-gate 		if (DEVI(dip)->devi_instance == instance) {
54460Sstevel@tonic-gate 			/*
54470Sstevel@tonic-gate 			 * To accommodate callers that can't block in
54480Sstevel@tonic-gate 			 * ndi_devi_enter() we do an ndi_devi_hold(), and
54490Sstevel@tonic-gate 			 * afterwards check that the node is in a state where
54500Sstevel@tonic-gate 			 * the hold prevents detach(). If we did not manage to
54510Sstevel@tonic-gate 			 * prevent detach then we ndi_rele_devi() and perform
54520Sstevel@tonic-gate 			 * the slow path below (which can result in a blocking
54530Sstevel@tonic-gate 			 * ndi_devi_enter() while driving attach top-down).
54540Sstevel@tonic-gate 			 * This code depends on the ordering of
54550Sstevel@tonic-gate 			 * DEVI_SET_DETACHING and the devi_ref check in the
54560Sstevel@tonic-gate 			 * detach_node() code path.
54570Sstevel@tonic-gate 			 */
54580Sstevel@tonic-gate 			ndi_hold_devi(dip);
54590Sstevel@tonic-gate 			if ((i_ddi_node_state(dip) >= DS_ATTACHED) &&
54600Sstevel@tonic-gate 			    !DEVI_IS_DETACHING(dip)) {
54610Sstevel@tonic-gate 				UNLOCK_DEV_OPS(&(dnp->dn_lock));
54620Sstevel@tonic-gate 				return (dip);	/* fast-path with devi held */
54630Sstevel@tonic-gate 			}
54640Sstevel@tonic-gate 			ndi_rele_devi(dip);
54650Sstevel@tonic-gate 
54660Sstevel@tonic-gate 			/* try slow-path */
54670Sstevel@tonic-gate 			dip = NULL;
54680Sstevel@tonic-gate 			break;
54690Sstevel@tonic-gate 		}
54700Sstevel@tonic-gate 	}
54710Sstevel@tonic-gate 	ASSERT(dip == NULL);
54720Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
54730Sstevel@tonic-gate 
54740Sstevel@tonic-gate 	if (flags & E_DDI_HOLD_DEVI_NOATTACH)
54750Sstevel@tonic-gate 		return (NULL);		/* told not to drive attach */
54760Sstevel@tonic-gate 
54770Sstevel@tonic-gate 	/* slow-path may block, so it should not occur from interrupt */
54780Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
54790Sstevel@tonic-gate 	if (servicing_interrupt())
54800Sstevel@tonic-gate 		return (NULL);
54810Sstevel@tonic-gate 
54820Sstevel@tonic-gate 	/* reconstruct the path and drive attach by path through devfs. */
54830Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
54840Sstevel@tonic-gate 	if (e_ddi_majorinstance_to_path(major, instance, path) == 0)
54850Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_path(path, flags);
54860Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
54870Sstevel@tonic-gate 	return (dip);			/* with devi held */
54880Sstevel@tonic-gate }
54890Sstevel@tonic-gate 
54900Sstevel@tonic-gate /*
54910Sstevel@tonic-gate  * The {e_}ddi_hold_devi{_by_{instance|dev|path}} hold the devinfo node
54920Sstevel@tonic-gate  * associated with the specified arguments.  This hold should be released
54930Sstevel@tonic-gate  * by calling ddi_release_devi.
54940Sstevel@tonic-gate  *
54950Sstevel@tonic-gate  * The E_DDI_HOLD_DEVI_NOATTACH flag argument allows the caller to to specify
54960Sstevel@tonic-gate  * a failure return if the node is not already attached.
54970Sstevel@tonic-gate  *
54980Sstevel@tonic-gate  * NOTE: by the time we make e_ddi_hold_devi public, we should be able to reuse
54990Sstevel@tonic-gate  * ddi_hold_devi again.
55000Sstevel@tonic-gate  */
55010Sstevel@tonic-gate dev_info_t *
55020Sstevel@tonic-gate ddi_hold_devi_by_instance(major_t major, int instance, int flags)
55030Sstevel@tonic-gate {
55040Sstevel@tonic-gate 	return (hold_devi(major, instance, flags));
55050Sstevel@tonic-gate }
55060Sstevel@tonic-gate 
55070Sstevel@tonic-gate dev_info_t *
55080Sstevel@tonic-gate e_ddi_hold_devi_by_dev(dev_t dev, int flags)
55090Sstevel@tonic-gate {
55100Sstevel@tonic-gate 	major_t	major = getmajor(dev);
55110Sstevel@tonic-gate 	dev_info_t	*dip;
55120Sstevel@tonic-gate 	struct dev_ops	*ops;
55130Sstevel@tonic-gate 	dev_info_t	*ddip = NULL;
55140Sstevel@tonic-gate 
55150Sstevel@tonic-gate 	dip = hold_devi(major, dev_to_instance(dev), flags);
55160Sstevel@tonic-gate 
55170Sstevel@tonic-gate 	/*
55180Sstevel@tonic-gate 	 * The rest of this routine is legacy support for drivers that
55190Sstevel@tonic-gate 	 * have broken DDI_INFO_DEVT2INSTANCE implementations but may have
55200Sstevel@tonic-gate 	 * functional DDI_INFO_DEVT2DEVINFO implementations.  This code will
55210Sstevel@tonic-gate 	 * diagnose inconsistency and, for maximum compatibility with legacy
55220Sstevel@tonic-gate 	 * drivers, give preference to the drivers DDI_INFO_DEVT2DEVINFO
55230Sstevel@tonic-gate 	 * implementation over the above derived dip based the driver's
55240Sstevel@tonic-gate 	 * DDI_INFO_DEVT2INSTANCE implementation. This legacy support should
55250Sstevel@tonic-gate 	 * be removed when DDI_INFO_DEVT2DEVINFO is deprecated.
55260Sstevel@tonic-gate 	 *
55270Sstevel@tonic-gate 	 * NOTE: The following code has a race condition. DEVT2DEVINFO
55280Sstevel@tonic-gate 	 *	returns a dip which is not held. By the time we ref ddip,
55290Sstevel@tonic-gate 	 *	it could have been freed. The saving grace is that for
55300Sstevel@tonic-gate 	 *	most drivers, the dip returned from hold_devi() is the
55310Sstevel@tonic-gate 	 *	same one as the one returned by DEVT2DEVINFO, so we are
55320Sstevel@tonic-gate 	 *	safe for drivers with the correct getinfo(9e) impl.
55330Sstevel@tonic-gate 	 */
55340Sstevel@tonic-gate 	if (((ops = ddi_hold_driver(major)) != NULL) &&
55350Sstevel@tonic-gate 	    CB_DRV_INSTALLED(ops) && ops->devo_getinfo)  {
55360Sstevel@tonic-gate 		if ((*ops->devo_getinfo)(NULL, DDI_INFO_DEVT2DEVINFO,
55370Sstevel@tonic-gate 		    (void *)dev, (void **)&ddip) != DDI_SUCCESS)
55380Sstevel@tonic-gate 			ddip = NULL;
55390Sstevel@tonic-gate 	}
55400Sstevel@tonic-gate 
55410Sstevel@tonic-gate 	/* give preference to the driver returned DEVT2DEVINFO dip */
55420Sstevel@tonic-gate 	if (ddip && (dip != ddip)) {
55430Sstevel@tonic-gate #ifdef	DEBUG
55440Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: inconsistent getinfo(9E) implementation",
55450Sstevel@tonic-gate 		    ddi_driver_name(ddip));
55460Sstevel@tonic-gate #endif	/* DEBUG */
55470Sstevel@tonic-gate 		ndi_hold_devi(ddip);
55480Sstevel@tonic-gate 		if (dip)
55490Sstevel@tonic-gate 			ndi_rele_devi(dip);
55500Sstevel@tonic-gate 		dip = ddip;
55510Sstevel@tonic-gate 	}
55520Sstevel@tonic-gate 
55530Sstevel@tonic-gate 	if (ops)
55540Sstevel@tonic-gate 		ddi_rele_driver(major);
55550Sstevel@tonic-gate 
55560Sstevel@tonic-gate 	return (dip);
55570Sstevel@tonic-gate }
55580Sstevel@tonic-gate 
55590Sstevel@tonic-gate /*
55600Sstevel@tonic-gate  * For compatibility only. Do not call this function!
55610Sstevel@tonic-gate  */
55620Sstevel@tonic-gate dev_info_t *
55630Sstevel@tonic-gate e_ddi_get_dev_info(dev_t dev, vtype_t type)
55640Sstevel@tonic-gate {
55650Sstevel@tonic-gate 	dev_info_t *dip = NULL;
55660Sstevel@tonic-gate 	if (getmajor(dev) >= devcnt)
55670Sstevel@tonic-gate 		return (NULL);
55680Sstevel@tonic-gate 
55690Sstevel@tonic-gate 	switch (type) {
55700Sstevel@tonic-gate 	case VCHR:
55710Sstevel@tonic-gate 	case VBLK:
55720Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
55730Sstevel@tonic-gate 	default:
55740Sstevel@tonic-gate 		break;
55750Sstevel@tonic-gate 	}
55760Sstevel@tonic-gate 
55770Sstevel@tonic-gate 	/*
55780Sstevel@tonic-gate 	 * For compatibility reasons, we can only return the dip with
55790Sstevel@tonic-gate 	 * the driver ref count held. This is not a safe thing to do.
55800Sstevel@tonic-gate 	 * For certain broken third-party software, we are willing
55810Sstevel@tonic-gate 	 * to venture into unknown territory.
55820Sstevel@tonic-gate 	 */
55830Sstevel@tonic-gate 	if (dip) {
55840Sstevel@tonic-gate 		(void) ndi_hold_driver(dip);
55850Sstevel@tonic-gate 		ndi_rele_devi(dip);
55860Sstevel@tonic-gate 	}
55870Sstevel@tonic-gate 	return (dip);
55880Sstevel@tonic-gate }
55890Sstevel@tonic-gate 
55900Sstevel@tonic-gate dev_info_t *
55910Sstevel@tonic-gate e_ddi_hold_devi_by_path(char *path, int flags)
55920Sstevel@tonic-gate {
55930Sstevel@tonic-gate 	dev_info_t	*dip;
55940Sstevel@tonic-gate 
55950Sstevel@tonic-gate 	/* can't specify NOATTACH by path */
55960Sstevel@tonic-gate 	ASSERT(!(flags & E_DDI_HOLD_DEVI_NOATTACH));
55970Sstevel@tonic-gate 
55980Sstevel@tonic-gate 	return (resolve_pathname(path, &dip, NULL, NULL) ? NULL : dip);
55990Sstevel@tonic-gate }
56000Sstevel@tonic-gate 
56010Sstevel@tonic-gate void
56020Sstevel@tonic-gate e_ddi_hold_devi(dev_info_t *dip)
56030Sstevel@tonic-gate {
56040Sstevel@tonic-gate 	ndi_hold_devi(dip);
56050Sstevel@tonic-gate }
56060Sstevel@tonic-gate 
56070Sstevel@tonic-gate void
56080Sstevel@tonic-gate ddi_release_devi(dev_info_t *dip)
56090Sstevel@tonic-gate {
56100Sstevel@tonic-gate 	ndi_rele_devi(dip);
56110Sstevel@tonic-gate }
56120Sstevel@tonic-gate 
56130Sstevel@tonic-gate /*
56140Sstevel@tonic-gate  * Associate a streams queue with a devinfo node
56150Sstevel@tonic-gate  * NOTE: This function is called by STREAM driver's put procedure.
56160Sstevel@tonic-gate  *	It cannot block.
56170Sstevel@tonic-gate  */
56180Sstevel@tonic-gate void
56190Sstevel@tonic-gate ddi_assoc_queue_with_devi(queue_t *q, dev_info_t *dip)
56200Sstevel@tonic-gate {
56210Sstevel@tonic-gate 	queue_t *rq = _RD(q);
56220Sstevel@tonic-gate 	struct stdata *stp;
56230Sstevel@tonic-gate 	vnode_t *vp;
56240Sstevel@tonic-gate 
56250Sstevel@tonic-gate 	/* set flag indicating that ddi_assoc_queue_with_devi was called */
56260Sstevel@tonic-gate 	mutex_enter(QLOCK(rq));
56270Sstevel@tonic-gate 	rq->q_flag |= _QASSOCIATED;
56280Sstevel@tonic-gate 	mutex_exit(QLOCK(rq));
56290Sstevel@tonic-gate 
56300Sstevel@tonic-gate 	/* get the vnode associated with the queue */
56310Sstevel@tonic-gate 	stp = STREAM(rq);
56320Sstevel@tonic-gate 	vp = stp->sd_vnode;
56330Sstevel@tonic-gate 	ASSERT(vp);
56340Sstevel@tonic-gate 
56350Sstevel@tonic-gate 	/* change the hardware association of the vnode */
56360Sstevel@tonic-gate 	spec_assoc_vp_with_devi(vp, dip);
56370Sstevel@tonic-gate }
56380Sstevel@tonic-gate 
56390Sstevel@tonic-gate /*
56400Sstevel@tonic-gate  * ddi_install_driver(name)
56410Sstevel@tonic-gate  *
56420Sstevel@tonic-gate  * Driver installation is currently a byproduct of driver loading.  This
56430Sstevel@tonic-gate  * may change.
56440Sstevel@tonic-gate  */
56450Sstevel@tonic-gate int
56460Sstevel@tonic-gate ddi_install_driver(char *name)
56470Sstevel@tonic-gate {
56480Sstevel@tonic-gate 	major_t major = ddi_name_to_major(name);
56490Sstevel@tonic-gate 
56500Sstevel@tonic-gate 	if ((major == (major_t)-1) ||
56510Sstevel@tonic-gate 	    (ddi_hold_installed_driver(major) == NULL)) {
56520Sstevel@tonic-gate 		return (DDI_FAILURE);
56530Sstevel@tonic-gate 	}
56540Sstevel@tonic-gate 	ddi_rele_driver(major);
56550Sstevel@tonic-gate 	return (DDI_SUCCESS);
56560Sstevel@tonic-gate }
56570Sstevel@tonic-gate 
56580Sstevel@tonic-gate struct dev_ops *
56590Sstevel@tonic-gate ddi_hold_driver(major_t major)
56600Sstevel@tonic-gate {
56610Sstevel@tonic-gate 	return (mod_hold_dev_by_major(major));
56620Sstevel@tonic-gate }
56630Sstevel@tonic-gate 
56640Sstevel@tonic-gate 
56650Sstevel@tonic-gate void
56660Sstevel@tonic-gate ddi_rele_driver(major_t major)
56670Sstevel@tonic-gate {
56680Sstevel@tonic-gate 	mod_rele_dev_by_major(major);
56690Sstevel@tonic-gate }
56700Sstevel@tonic-gate 
56710Sstevel@tonic-gate 
56720Sstevel@tonic-gate /*
56730Sstevel@tonic-gate  * This is called during boot to force attachment order of special dips
56740Sstevel@tonic-gate  * dip must be referenced via ndi_hold_devi()
56750Sstevel@tonic-gate  */
56760Sstevel@tonic-gate int
56770Sstevel@tonic-gate i_ddi_attach_node_hierarchy(dev_info_t *dip)
56780Sstevel@tonic-gate {
56790Sstevel@tonic-gate 	dev_info_t *parent;
56800Sstevel@tonic-gate 
56810Sstevel@tonic-gate 	if (i_ddi_node_state(dip) == DS_READY)
56820Sstevel@tonic-gate 		return (DDI_SUCCESS);
56830Sstevel@tonic-gate 
56840Sstevel@tonic-gate 	/*
56850Sstevel@tonic-gate 	 * Attach parent dip
56860Sstevel@tonic-gate 	 */
56870Sstevel@tonic-gate 	parent = ddi_get_parent(dip);
56880Sstevel@tonic-gate 	if (i_ddi_attach_node_hierarchy(parent) != DDI_SUCCESS)
56890Sstevel@tonic-gate 		return (DDI_FAILURE);
56900Sstevel@tonic-gate 
56910Sstevel@tonic-gate 	/*
56920Sstevel@tonic-gate 	 * Expand .conf nodes under this parent
56930Sstevel@tonic-gate 	 */
56940Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(parent, 0);
56950Sstevel@tonic-gate 	return (i_ddi_attachchild(dip));
56960Sstevel@tonic-gate }
56970Sstevel@tonic-gate 
56980Sstevel@tonic-gate /* keep this function static */
56990Sstevel@tonic-gate static int
57000Sstevel@tonic-gate attach_driver_nodes(major_t major)
57010Sstevel@tonic-gate {
57020Sstevel@tonic-gate 	struct devnames *dnp;
57030Sstevel@tonic-gate 	dev_info_t *dip;
57040Sstevel@tonic-gate 	int error = DDI_FAILURE;
57050Sstevel@tonic-gate 
57060Sstevel@tonic-gate 	dnp = &devnamesp[major];
57070Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
57080Sstevel@tonic-gate 	dip = dnp->dn_head;
57090Sstevel@tonic-gate 	while (dip) {
57100Sstevel@tonic-gate 		ndi_hold_devi(dip);
57110Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
57120Sstevel@tonic-gate 		if (i_ddi_attach_node_hierarchy(dip) == DDI_SUCCESS)
57130Sstevel@tonic-gate 			error = DDI_SUCCESS;
57140Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
57150Sstevel@tonic-gate 		ndi_rele_devi(dip);
57160Sstevel@tonic-gate 		dip = ddi_get_next(dip);
57170Sstevel@tonic-gate 	}
57180Sstevel@tonic-gate 	if (error == DDI_SUCCESS)
57190Sstevel@tonic-gate 		dnp->dn_flags |= DN_NO_AUTODETACH;
57200Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
57210Sstevel@tonic-gate 
57220Sstevel@tonic-gate 
57230Sstevel@tonic-gate 	return (error);
57240Sstevel@tonic-gate }
57250Sstevel@tonic-gate 
57260Sstevel@tonic-gate /*
57270Sstevel@tonic-gate  * i_ddi_attach_hw_nodes configures and attaches all hw nodes
57280Sstevel@tonic-gate  * bound to a specific driver. This function replaces calls to
57290Sstevel@tonic-gate  * ddi_hold_installed_driver() for drivers with no .conf
57300Sstevel@tonic-gate  * enumerated nodes.
57310Sstevel@tonic-gate  *
57320Sstevel@tonic-gate  * This facility is typically called at boot time to attach
57330Sstevel@tonic-gate  * platform-specific hardware nodes, such as ppm nodes on xcal
57340Sstevel@tonic-gate  * and grover and keyswitch nodes on cherrystone. It does not
57350Sstevel@tonic-gate  * deal with .conf enumerated node. Calling it beyond the boot
57360Sstevel@tonic-gate  * process is strongly discouraged.
57370Sstevel@tonic-gate  */
57380Sstevel@tonic-gate int
57390Sstevel@tonic-gate i_ddi_attach_hw_nodes(char *driver)
57400Sstevel@tonic-gate {
57410Sstevel@tonic-gate 	major_t major;
57420Sstevel@tonic-gate 
57430Sstevel@tonic-gate 	major = ddi_name_to_major(driver);
57440Sstevel@tonic-gate 	if (major == (major_t)-1)
57450Sstevel@tonic-gate 		return (DDI_FAILURE);
57460Sstevel@tonic-gate 
57470Sstevel@tonic-gate 	return (attach_driver_nodes(major));
57480Sstevel@tonic-gate }
57490Sstevel@tonic-gate 
57500Sstevel@tonic-gate /*
57510Sstevel@tonic-gate  * i_ddi_attach_pseudo_node configures pseudo drivers which
57520Sstevel@tonic-gate  * has a single node. The .conf nodes must be enumerated
57530Sstevel@tonic-gate  * before calling this interface. The dip is held attached
57540Sstevel@tonic-gate  * upon returning.
57550Sstevel@tonic-gate  *
57560Sstevel@tonic-gate  * This facility should only be called only at boot time
57570Sstevel@tonic-gate  * by the I/O framework.
57580Sstevel@tonic-gate  */
57590Sstevel@tonic-gate dev_info_t *
57600Sstevel@tonic-gate i_ddi_attach_pseudo_node(char *driver)
57610Sstevel@tonic-gate {
57620Sstevel@tonic-gate 	major_t major;
57630Sstevel@tonic-gate 	dev_info_t *dip;
57640Sstevel@tonic-gate 
57650Sstevel@tonic-gate 	major = ddi_name_to_major(driver);
57660Sstevel@tonic-gate 	if (major == (major_t)-1)
57670Sstevel@tonic-gate 		return (NULL);
57680Sstevel@tonic-gate 
57690Sstevel@tonic-gate 	if (attach_driver_nodes(major) != DDI_SUCCESS)
57700Sstevel@tonic-gate 		return (NULL);
57710Sstevel@tonic-gate 
57720Sstevel@tonic-gate 	dip = devnamesp[major].dn_head;
57730Sstevel@tonic-gate 	ASSERT(dip && ddi_get_next(dip) == NULL);
57740Sstevel@tonic-gate 	ndi_hold_devi(dip);
57750Sstevel@tonic-gate 	return (dip);
57760Sstevel@tonic-gate }
57770Sstevel@tonic-gate 
57780Sstevel@tonic-gate static void
57790Sstevel@tonic-gate diplist_to_parent_major(dev_info_t *head, char parents[])
57800Sstevel@tonic-gate {
57810Sstevel@tonic-gate 	major_t major;
57820Sstevel@tonic-gate 	dev_info_t *dip, *pdip;
57830Sstevel@tonic-gate 
57840Sstevel@tonic-gate 	for (dip = head; dip != NULL; dip = ddi_get_next(dip)) {
57850Sstevel@tonic-gate 		pdip = ddi_get_parent(dip);
57860Sstevel@tonic-gate 		ASSERT(pdip);	/* disallow rootnex.conf nodes */
57870Sstevel@tonic-gate 		major = ddi_driver_major(pdip);
57880Sstevel@tonic-gate 		if ((major != (major_t)-1) && parents[major] == 0)
57890Sstevel@tonic-gate 			parents[major] = 1;
57900Sstevel@tonic-gate 	}
57910Sstevel@tonic-gate }
57920Sstevel@tonic-gate 
57930Sstevel@tonic-gate /*
57940Sstevel@tonic-gate  * Call ddi_hold_installed_driver() on each parent major
57950Sstevel@tonic-gate  * and invoke mt_config_driver() to attach child major.
57960Sstevel@tonic-gate  * This is part of the implementation of ddi_hold_installed_driver.
57970Sstevel@tonic-gate  */
57980Sstevel@tonic-gate static int
57990Sstevel@tonic-gate attach_driver_by_parent(major_t child_major, char parents[])
58000Sstevel@tonic-gate {
58010Sstevel@tonic-gate 	major_t par_major;
58020Sstevel@tonic-gate 	struct mt_config_handle *hdl;
58030Sstevel@tonic-gate 	int flags = NDI_DEVI_PERSIST | NDI_NO_EVENT;
58040Sstevel@tonic-gate 
58050Sstevel@tonic-gate 	hdl = mt_config_init(NULL, NULL, flags, child_major, MT_CONFIG_OP,
58060Sstevel@tonic-gate 	    NULL);
58070Sstevel@tonic-gate 	for (par_major = 0; par_major < devcnt; par_major++) {
58080Sstevel@tonic-gate 		/* disallow recursion on the same driver */
58090Sstevel@tonic-gate 		if (parents[par_major] == 0 || par_major == child_major)
58100Sstevel@tonic-gate 			continue;
58110Sstevel@tonic-gate 		if (ddi_hold_installed_driver(par_major) == NULL)
58120Sstevel@tonic-gate 			continue;
58130Sstevel@tonic-gate 		hdl->mtc_parmajor = par_major;
58140Sstevel@tonic-gate 		mt_config_driver(hdl);
58150Sstevel@tonic-gate 		ddi_rele_driver(par_major);
58160Sstevel@tonic-gate 	}
58170Sstevel@tonic-gate 	(void) mt_config_fini(hdl);
58180Sstevel@tonic-gate 
58190Sstevel@tonic-gate 	return (i_ddi_devs_attached(child_major));
58200Sstevel@tonic-gate }
58210Sstevel@tonic-gate 
58220Sstevel@tonic-gate int
58230Sstevel@tonic-gate i_ddi_devs_attached(major_t major)
58240Sstevel@tonic-gate {
58250Sstevel@tonic-gate 	dev_info_t *dip;
58260Sstevel@tonic-gate 	struct devnames *dnp;
58270Sstevel@tonic-gate 	int error = DDI_FAILURE;
58280Sstevel@tonic-gate 
58290Sstevel@tonic-gate 	/* check for attached instances */
58300Sstevel@tonic-gate 	dnp = &devnamesp[major];
58310Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
58320Sstevel@tonic-gate 	for (dip = dnp->dn_head; dip != NULL; dip = ddi_get_next(dip)) {
58330Sstevel@tonic-gate 		if (i_ddi_node_state(dip) >= DS_ATTACHED) {
58340Sstevel@tonic-gate 			error = DDI_SUCCESS;
58350Sstevel@tonic-gate 			break;
58360Sstevel@tonic-gate 		}
58370Sstevel@tonic-gate 	}
58380Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
58390Sstevel@tonic-gate 
58400Sstevel@tonic-gate 	return (error);
58410Sstevel@tonic-gate }
58420Sstevel@tonic-gate 
58430Sstevel@tonic-gate /*
58440Sstevel@tonic-gate  * ddi_hold_installed_driver configures and attaches all
58450Sstevel@tonic-gate  * instances of the specified driver. To accomplish this
58460Sstevel@tonic-gate  * it configures and attaches all possible parents of
58470Sstevel@tonic-gate  * the driver, enumerated both in h/w nodes and in the
58480Sstevel@tonic-gate  * driver's .conf file.
58490Sstevel@tonic-gate  *
58500Sstevel@tonic-gate  * NOTE: This facility is for compatibility purposes only and will
58510Sstevel@tonic-gate  *	eventually go away. Its usage is strongly discouraged.
58520Sstevel@tonic-gate  */
58530Sstevel@tonic-gate static void
58540Sstevel@tonic-gate enter_driver(struct devnames *dnp)
58550Sstevel@tonic-gate {
58560Sstevel@tonic-gate 	mutex_enter(&dnp->dn_lock);
58570Sstevel@tonic-gate 	ASSERT(dnp->dn_busy_thread != curthread);
58580Sstevel@tonic-gate 	while (dnp->dn_flags & DN_DRIVER_BUSY)
58590Sstevel@tonic-gate 		cv_wait(&dnp->dn_wait, &dnp->dn_lock);
58600Sstevel@tonic-gate 	dnp->dn_flags |= DN_DRIVER_BUSY;
58610Sstevel@tonic-gate 	dnp->dn_busy_thread = curthread;
58620Sstevel@tonic-gate 	mutex_exit(&dnp->dn_lock);
58630Sstevel@tonic-gate }
58640Sstevel@tonic-gate 
58650Sstevel@tonic-gate static void
58660Sstevel@tonic-gate exit_driver(struct devnames *dnp)
58670Sstevel@tonic-gate {
58680Sstevel@tonic-gate 	mutex_enter(&dnp->dn_lock);
58690Sstevel@tonic-gate 	ASSERT(dnp->dn_busy_thread == curthread);
58700Sstevel@tonic-gate 	dnp->dn_flags &= ~DN_DRIVER_BUSY;
58710Sstevel@tonic-gate 	dnp->dn_busy_thread = NULL;
58720Sstevel@tonic-gate 	cv_broadcast(&dnp->dn_wait);
58730Sstevel@tonic-gate 	mutex_exit(&dnp->dn_lock);
58740Sstevel@tonic-gate }
58750Sstevel@tonic-gate 
58760Sstevel@tonic-gate struct dev_ops *
58770Sstevel@tonic-gate ddi_hold_installed_driver(major_t major)
58780Sstevel@tonic-gate {
58790Sstevel@tonic-gate 	struct dev_ops *ops;
58800Sstevel@tonic-gate 	struct devnames *dnp;
58810Sstevel@tonic-gate 	char *parents;
58820Sstevel@tonic-gate 	int error;
58830Sstevel@tonic-gate 
58840Sstevel@tonic-gate 	ops = ddi_hold_driver(major);
58850Sstevel@tonic-gate 	if (ops == NULL)
58860Sstevel@tonic-gate 		return (NULL);
58870Sstevel@tonic-gate 
58880Sstevel@tonic-gate 	/*
58890Sstevel@tonic-gate 	 * Return immediately if all the attach operations associated
58900Sstevel@tonic-gate 	 * with a ddi_hold_installed_driver() call have already been done.
58910Sstevel@tonic-gate 	 */
58920Sstevel@tonic-gate 	dnp = &devnamesp[major];
58930Sstevel@tonic-gate 	enter_driver(dnp);
58940Sstevel@tonic-gate 	if (dnp->dn_flags & DN_DRIVER_HELD) {
58950Sstevel@tonic-gate 		exit_driver(dnp);
58960Sstevel@tonic-gate 		if (i_ddi_devs_attached(major) == DDI_SUCCESS)
58970Sstevel@tonic-gate 			return (ops);
58980Sstevel@tonic-gate 		ddi_rele_driver(major);
58990Sstevel@tonic-gate 		return (NULL);
59000Sstevel@tonic-gate 	}
59010Sstevel@tonic-gate 
59020Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
59030Sstevel@tonic-gate 	dnp->dn_flags |= (DN_DRIVER_HELD | DN_NO_AUTODETACH);
59040Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
59050Sstevel@tonic-gate 
59060Sstevel@tonic-gate 	DCOMPATPRINTF((CE_CONT,
59070Sstevel@tonic-gate 	    "ddi_hold_installed_driver: %s\n", dnp->dn_name));
59080Sstevel@tonic-gate 
59090Sstevel@tonic-gate 	/*
59100Sstevel@tonic-gate 	 * When the driver has no .conf children, it is sufficient
59110Sstevel@tonic-gate 	 * to attach existing nodes in the device tree. Nodes not
59120Sstevel@tonic-gate 	 * enumerated by the OBP are not attached.
59130Sstevel@tonic-gate 	 */
59140Sstevel@tonic-gate 	if (dnp->dn_pl == NULL) {
59150Sstevel@tonic-gate 		if (attach_driver_nodes(major) == DDI_SUCCESS) {
59160Sstevel@tonic-gate 			exit_driver(dnp);
59170Sstevel@tonic-gate 			return (ops);
59180Sstevel@tonic-gate 		}
59190Sstevel@tonic-gate 		exit_driver(dnp);
59200Sstevel@tonic-gate 		ddi_rele_driver(major);
59210Sstevel@tonic-gate 		return (NULL);
59220Sstevel@tonic-gate 	}
59230Sstevel@tonic-gate 
59240Sstevel@tonic-gate 	/*
59250Sstevel@tonic-gate 	 * Driver has .conf nodes. We find all possible parents
59260Sstevel@tonic-gate 	 * and recursively all ddi_hold_installed_driver on the
59270Sstevel@tonic-gate 	 * parent driver; then we invoke ndi_config_driver()
59280Sstevel@tonic-gate 	 * on all possible parent node in parallel to speed up
59290Sstevel@tonic-gate 	 * performance.
59300Sstevel@tonic-gate 	 */
59310Sstevel@tonic-gate 	parents = kmem_zalloc(devcnt * sizeof (char), KM_SLEEP);
59320Sstevel@tonic-gate 
59330Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
59340Sstevel@tonic-gate 	/* find .conf parents */
59350Sstevel@tonic-gate 	(void) impl_parlist_to_major(dnp->dn_pl, parents);
59360Sstevel@tonic-gate 	/* find hw node parents */
59370Sstevel@tonic-gate 	diplist_to_parent_major(dnp->dn_head, parents);
59380Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
59390Sstevel@tonic-gate 
59400Sstevel@tonic-gate 	error = attach_driver_by_parent(major, parents);
59410Sstevel@tonic-gate 	kmem_free(parents, devcnt * sizeof (char));
59420Sstevel@tonic-gate 	if (error == DDI_SUCCESS) {
59430Sstevel@tonic-gate 		exit_driver(dnp);
59440Sstevel@tonic-gate 		return (ops);
59450Sstevel@tonic-gate 	}
59460Sstevel@tonic-gate 
59470Sstevel@tonic-gate 	exit_driver(dnp);
59480Sstevel@tonic-gate 	ddi_rele_driver(major);
59490Sstevel@tonic-gate 	return (NULL);
59500Sstevel@tonic-gate }
59510Sstevel@tonic-gate 
59520Sstevel@tonic-gate /*
59530Sstevel@tonic-gate  * Default bus_config entry point for nexus drivers
59540Sstevel@tonic-gate  */
59550Sstevel@tonic-gate int
59560Sstevel@tonic-gate ndi_busop_bus_config(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
59570Sstevel@tonic-gate     void *arg, dev_info_t **child, clock_t timeout)
59580Sstevel@tonic-gate {
59590Sstevel@tonic-gate 	major_t major;
59600Sstevel@tonic-gate 
59610Sstevel@tonic-gate 	/*
59620Sstevel@tonic-gate 	 * A timeout of 30 minutes or more is probably a mistake
59630Sstevel@tonic-gate 	 * This is intended to catch uses where timeout is in
59640Sstevel@tonic-gate 	 * the wrong units.  timeout must be in units of ticks.
59650Sstevel@tonic-gate 	 */
59660Sstevel@tonic-gate 	ASSERT(timeout < SEC_TO_TICK(1800));
59670Sstevel@tonic-gate 
59680Sstevel@tonic-gate 	major = (major_t)-1;
59690Sstevel@tonic-gate 	switch (op) {
59700Sstevel@tonic-gate 	case BUS_CONFIG_ONE:
59710Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config %s timeout=%ld\n",
59720Sstevel@tonic-gate 			ddi_driver_name(pdip), ddi_get_instance(pdip),
59730Sstevel@tonic-gate 			(char *)arg, timeout));
59740Sstevel@tonic-gate 		return (devi_config_one(pdip, (char *)arg, child, flags,
59750Sstevel@tonic-gate 		    timeout));
59760Sstevel@tonic-gate 
59770Sstevel@tonic-gate 	case BUS_CONFIG_DRIVER:
59780Sstevel@tonic-gate 		major = (major_t)(uintptr_t)arg;
59790Sstevel@tonic-gate 		/*FALLTHROUGH*/
59800Sstevel@tonic-gate 	case BUS_CONFIG_ALL:
59810Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config timeout=%ld\n",
59820Sstevel@tonic-gate 			ddi_driver_name(pdip), ddi_get_instance(pdip),
59830Sstevel@tonic-gate 			timeout));
59840Sstevel@tonic-gate 		if (timeout > 0) {
59850Sstevel@tonic-gate 			NDI_DEBUG(flags, (CE_CONT,
59860Sstevel@tonic-gate 			    "%s%d: bus config all timeout=%ld\n",
59870Sstevel@tonic-gate 			    ddi_driver_name(pdip), ddi_get_instance(pdip),
59880Sstevel@tonic-gate 			    timeout));
59890Sstevel@tonic-gate 			delay(timeout);
59900Sstevel@tonic-gate 		}
59910Sstevel@tonic-gate 		return (config_immediate_children(pdip, flags, major));
59920Sstevel@tonic-gate 
59930Sstevel@tonic-gate 	default:
59940Sstevel@tonic-gate 		return (NDI_FAILURE);
59950Sstevel@tonic-gate 	}
59960Sstevel@tonic-gate 	/*NOTREACHED*/
59970Sstevel@tonic-gate }
59980Sstevel@tonic-gate 
59990Sstevel@tonic-gate /*
60000Sstevel@tonic-gate  * Default busop bus_unconfig handler for nexus drivers
60010Sstevel@tonic-gate  */
60020Sstevel@tonic-gate int
60030Sstevel@tonic-gate ndi_busop_bus_unconfig(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
60040Sstevel@tonic-gate     void *arg)
60050Sstevel@tonic-gate {
60060Sstevel@tonic-gate 	major_t major;
60070Sstevel@tonic-gate 
60080Sstevel@tonic-gate 	major = (major_t)-1;
60090Sstevel@tonic-gate 	switch (op) {
60100Sstevel@tonic-gate 	case BUS_UNCONFIG_ONE:
60110Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig %s\n",
60120Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
60130Sstevel@tonic-gate 		    (char *)arg));
60140Sstevel@tonic-gate 		return (devi_unconfig_one(pdip, (char *)arg, flags));
60150Sstevel@tonic-gate 
60160Sstevel@tonic-gate 	case BUS_UNCONFIG_DRIVER:
60170Sstevel@tonic-gate 		major = (major_t)(uintptr_t)arg;
60180Sstevel@tonic-gate 		/*FALLTHROUGH*/
60190Sstevel@tonic-gate 	case BUS_UNCONFIG_ALL:
60200Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig all\n",
60210Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip)));
60220Sstevel@tonic-gate 		return (unconfig_immediate_children(pdip, NULL, flags, major));
60230Sstevel@tonic-gate 
60240Sstevel@tonic-gate 	default:
60250Sstevel@tonic-gate 		return (NDI_FAILURE);
60260Sstevel@tonic-gate 	}
60270Sstevel@tonic-gate 	/*NOTREACHED*/
60280Sstevel@tonic-gate }
60290Sstevel@tonic-gate 
60300Sstevel@tonic-gate /*
60310Sstevel@tonic-gate  * dummy functions to be removed
60320Sstevel@tonic-gate  */
60330Sstevel@tonic-gate void
60340Sstevel@tonic-gate impl_rem_dev_props(dev_info_t *dip)
60350Sstevel@tonic-gate {
60360Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip))
60370Sstevel@tonic-gate 	/* do nothing */
60380Sstevel@tonic-gate }
60390Sstevel@tonic-gate 
60400Sstevel@tonic-gate /*
60410Sstevel@tonic-gate  * Determine if a node is a leaf node. If not sure, return false (0).
60420Sstevel@tonic-gate  */
60430Sstevel@tonic-gate static int
60440Sstevel@tonic-gate is_leaf_node(dev_info_t *dip)
60450Sstevel@tonic-gate {
60460Sstevel@tonic-gate 	major_t major = ddi_driver_major(dip);
60470Sstevel@tonic-gate 
60480Sstevel@tonic-gate 	if (major == (major_t)-1)
60490Sstevel@tonic-gate 		return (0);
60500Sstevel@tonic-gate 
60510Sstevel@tonic-gate 	return (devnamesp[major].dn_flags & DN_LEAF_DRIVER);
60520Sstevel@tonic-gate }
60530Sstevel@tonic-gate 
60540Sstevel@tonic-gate /*
60550Sstevel@tonic-gate  * Multithreaded [un]configuration
60560Sstevel@tonic-gate  */
60570Sstevel@tonic-gate static struct mt_config_handle *
60580Sstevel@tonic-gate mt_config_init(dev_info_t *pdip, dev_info_t **dipp, int flags,
60590Sstevel@tonic-gate     major_t major, int op, struct brevq_node **brevqp)
60600Sstevel@tonic-gate {
60610Sstevel@tonic-gate 	struct mt_config_handle	*hdl = kmem_alloc(sizeof (*hdl), KM_SLEEP);
60620Sstevel@tonic-gate 
60630Sstevel@tonic-gate 	mutex_init(&hdl->mtc_lock, NULL, MUTEX_DEFAULT, NULL);
60640Sstevel@tonic-gate 	cv_init(&hdl->mtc_cv, NULL, CV_DEFAULT, NULL);
60650Sstevel@tonic-gate 	hdl->mtc_pdip = pdip;
60660Sstevel@tonic-gate 	hdl->mtc_fdip = dipp;
60670Sstevel@tonic-gate 	hdl->mtc_parmajor = (major_t)-1;
60680Sstevel@tonic-gate 	hdl->mtc_flags = flags;
60690Sstevel@tonic-gate 	hdl->mtc_major = major;
60700Sstevel@tonic-gate 	hdl->mtc_thr_count = 0;
60710Sstevel@tonic-gate 	hdl->mtc_op = op;
60720Sstevel@tonic-gate 	hdl->mtc_error = 0;
60730Sstevel@tonic-gate 	hdl->mtc_brevqp = brevqp;
60740Sstevel@tonic-gate 
60750Sstevel@tonic-gate #ifdef DEBUG
60760Sstevel@tonic-gate 	gethrestime(&hdl->start_time);
60770Sstevel@tonic-gate 	hdl->total_time = 0;
60780Sstevel@tonic-gate #endif /* DEBUG */
60790Sstevel@tonic-gate 
60800Sstevel@tonic-gate 	return (hdl);
60810Sstevel@tonic-gate }
60820Sstevel@tonic-gate 
60830Sstevel@tonic-gate #ifdef DEBUG
60840Sstevel@tonic-gate static int
60850Sstevel@tonic-gate time_diff_in_msec(timestruc_t start, timestruc_t end)
60860Sstevel@tonic-gate {
60870Sstevel@tonic-gate 	int	nsec, sec;
60880Sstevel@tonic-gate 
60890Sstevel@tonic-gate 	sec = end.tv_sec - start.tv_sec;
60900Sstevel@tonic-gate 	nsec = end.tv_nsec - start.tv_nsec;
60910Sstevel@tonic-gate 	if (nsec < 0) {
60920Sstevel@tonic-gate 		nsec += NANOSEC;
60930Sstevel@tonic-gate 		sec -= 1;
60940Sstevel@tonic-gate 	}
60950Sstevel@tonic-gate 
60960Sstevel@tonic-gate 	return (sec * (NANOSEC >> 20) + (nsec >> 20));
60970Sstevel@tonic-gate }
60980Sstevel@tonic-gate 
60990Sstevel@tonic-gate #endif	/* DEBUG */
61000Sstevel@tonic-gate 
61010Sstevel@tonic-gate static int
61020Sstevel@tonic-gate mt_config_fini(struct mt_config_handle *hdl)
61030Sstevel@tonic-gate {
61040Sstevel@tonic-gate 	int		rv;
61050Sstevel@tonic-gate #ifdef DEBUG
61060Sstevel@tonic-gate 	int		real_time;
61070Sstevel@tonic-gate 	timestruc_t	end_time;
61080Sstevel@tonic-gate #endif /* DEBUG */
61090Sstevel@tonic-gate 
61100Sstevel@tonic-gate 	mutex_enter(&hdl->mtc_lock);
61110Sstevel@tonic-gate 	while (hdl->mtc_thr_count > 0)
61120Sstevel@tonic-gate 		cv_wait(&hdl->mtc_cv, &hdl->mtc_lock);
61130Sstevel@tonic-gate 	rv = hdl->mtc_error;
61140Sstevel@tonic-gate 	mutex_exit(&hdl->mtc_lock);
61150Sstevel@tonic-gate 
61160Sstevel@tonic-gate #ifdef DEBUG
61170Sstevel@tonic-gate 	gethrestime(&end_time);
61180Sstevel@tonic-gate 	real_time = time_diff_in_msec(hdl->start_time, end_time);
61190Sstevel@tonic-gate 	if ((ddidebug & DDI_MTCONFIG) && hdl->mtc_pdip)
61200Sstevel@tonic-gate 		cmn_err(CE_NOTE,
61210Sstevel@tonic-gate 		    "config %s%d: total time %d msec, real time %d msec",
61220Sstevel@tonic-gate 			ddi_driver_name(hdl->mtc_pdip),
61230Sstevel@tonic-gate 			ddi_get_instance(hdl->mtc_pdip),
61240Sstevel@tonic-gate 			hdl->total_time, real_time);
61250Sstevel@tonic-gate #endif /* DEBUG */
61260Sstevel@tonic-gate 
61270Sstevel@tonic-gate 	cv_destroy(&hdl->mtc_cv);
61280Sstevel@tonic-gate 	mutex_destroy(&hdl->mtc_lock);
61290Sstevel@tonic-gate 	kmem_free(hdl, sizeof (*hdl));
61300Sstevel@tonic-gate 
61310Sstevel@tonic-gate 	return (rv);
61320Sstevel@tonic-gate }
61330Sstevel@tonic-gate 
61340Sstevel@tonic-gate struct mt_config_data {
61350Sstevel@tonic-gate 	struct mt_config_handle	*mtc_hdl;
61360Sstevel@tonic-gate 	dev_info_t		*mtc_dip;
61370Sstevel@tonic-gate 	major_t			mtc_major;
61380Sstevel@tonic-gate 	int			mtc_flags;
61390Sstevel@tonic-gate 	struct brevq_node	*mtc_brn;
61400Sstevel@tonic-gate 	struct mt_config_data	*mtc_next;
61410Sstevel@tonic-gate };
61420Sstevel@tonic-gate 
61430Sstevel@tonic-gate static void
61440Sstevel@tonic-gate mt_config_thread(void *arg)
61450Sstevel@tonic-gate {
61460Sstevel@tonic-gate 	struct mt_config_data	*mcd = (struct mt_config_data *)arg;
61470Sstevel@tonic-gate 	struct mt_config_handle	*hdl = mcd->mtc_hdl;
61480Sstevel@tonic-gate 	dev_info_t		*dip = mcd->mtc_dip;
61490Sstevel@tonic-gate 	dev_info_t		*rdip, **dipp;
61500Sstevel@tonic-gate 	major_t			major = mcd->mtc_major;
61510Sstevel@tonic-gate 	int			flags = mcd->mtc_flags;
61520Sstevel@tonic-gate 	int			rv = 0;
61530Sstevel@tonic-gate 
61540Sstevel@tonic-gate #ifdef DEBUG
61550Sstevel@tonic-gate 	timestruc_t start_time, end_time;
61560Sstevel@tonic-gate 	gethrestime(&start_time);
61570Sstevel@tonic-gate #endif /* DEBUG */
61580Sstevel@tonic-gate 
61590Sstevel@tonic-gate 	rdip = NULL;
61600Sstevel@tonic-gate 	dipp = hdl->mtc_fdip ? &rdip : NULL;
61610Sstevel@tonic-gate 
61620Sstevel@tonic-gate 	switch (hdl->mtc_op) {
61630Sstevel@tonic-gate 	case MT_CONFIG_OP:
61640Sstevel@tonic-gate 		rv = devi_config_common(dip, flags, major);
61650Sstevel@tonic-gate 		break;
61660Sstevel@tonic-gate 	case MT_UNCONFIG_OP:
61670Sstevel@tonic-gate 		if (mcd->mtc_brn) {
61680Sstevel@tonic-gate 			struct brevq_node *brevq = NULL;
61690Sstevel@tonic-gate 			rv = devi_unconfig_common(dip, dipp, flags, major,
61700Sstevel@tonic-gate 			    &brevq);
61710Sstevel@tonic-gate 			mcd->mtc_brn->child = brevq;
61720Sstevel@tonic-gate 		} else
61730Sstevel@tonic-gate 			rv = devi_unconfig_common(dip, dipp, flags, major,
61740Sstevel@tonic-gate 			    NULL);
61750Sstevel@tonic-gate 		break;
61760Sstevel@tonic-gate 	}
61770Sstevel@tonic-gate 
61780Sstevel@tonic-gate 	mutex_enter(&hdl->mtc_lock);
61790Sstevel@tonic-gate #ifdef DEBUG
61800Sstevel@tonic-gate 	gethrestime(&end_time);
61810Sstevel@tonic-gate 	hdl->total_time += time_diff_in_msec(start_time, end_time);
61820Sstevel@tonic-gate #endif /* DEBUG */
61830Sstevel@tonic-gate 	if (rv != NDI_SUCCESS)
61840Sstevel@tonic-gate 		hdl->mtc_error = rv;
61850Sstevel@tonic-gate 	if (hdl->mtc_fdip && *hdl->mtc_fdip == NULL) {
61860Sstevel@tonic-gate 		*hdl->mtc_fdip = rdip;
61870Sstevel@tonic-gate 		rdip = NULL;
61880Sstevel@tonic-gate 	}
61890Sstevel@tonic-gate 
61900Sstevel@tonic-gate 	if (--hdl->mtc_thr_count == 0)
61910Sstevel@tonic-gate 		cv_broadcast(&hdl->mtc_cv);
61920Sstevel@tonic-gate 	mutex_exit(&hdl->mtc_lock);
61930Sstevel@tonic-gate 
61940Sstevel@tonic-gate 	if (rdip) {
61950Sstevel@tonic-gate 		ASSERT(rv != NDI_SUCCESS);
61960Sstevel@tonic-gate 		ndi_rele_devi(rdip);
61970Sstevel@tonic-gate 	}
61980Sstevel@tonic-gate 
61990Sstevel@tonic-gate 	ndi_rele_devi(dip);
62000Sstevel@tonic-gate 	kmem_free(mcd, sizeof (*mcd));
62010Sstevel@tonic-gate }
62020Sstevel@tonic-gate 
62030Sstevel@tonic-gate /*
62040Sstevel@tonic-gate  * Multi-threaded config/unconfig of child nexus
62050Sstevel@tonic-gate  */
62060Sstevel@tonic-gate static void
62070Sstevel@tonic-gate mt_config_children(struct mt_config_handle *hdl)
62080Sstevel@tonic-gate {
62090Sstevel@tonic-gate 	dev_info_t		*pdip = hdl->mtc_pdip;
62100Sstevel@tonic-gate 	major_t			major = hdl->mtc_major;
62110Sstevel@tonic-gate 	dev_info_t		*dip;
62120Sstevel@tonic-gate 	int			circ;
62130Sstevel@tonic-gate 	struct brevq_node	*brn = NULL;
62140Sstevel@tonic-gate 	struct mt_config_data	*mcd_head = NULL;
62150Sstevel@tonic-gate 	struct mt_config_data	*mcd_tail = NULL;
62160Sstevel@tonic-gate 	struct mt_config_data	*mcd;
62170Sstevel@tonic-gate #ifdef DEBUG
62180Sstevel@tonic-gate 	timestruc_t		end_time;
62190Sstevel@tonic-gate 
62200Sstevel@tonic-gate 	/* Update total_time in handle */
62210Sstevel@tonic-gate 	gethrestime(&end_time);
62220Sstevel@tonic-gate 	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
62230Sstevel@tonic-gate #endif
62240Sstevel@tonic-gate 
62250Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
62260Sstevel@tonic-gate 	dip = ddi_get_child(pdip);
62270Sstevel@tonic-gate 	while (dip) {
62280Sstevel@tonic-gate 		if (hdl->mtc_op == MT_UNCONFIG_OP && hdl->mtc_brevqp &&
62290Sstevel@tonic-gate 		    !(DEVI_EVREMOVE(dip)) &&
62300Sstevel@tonic-gate 		    i_ddi_node_state(dip) >= DS_INITIALIZED) {
62310Sstevel@tonic-gate 			/*
62320Sstevel@tonic-gate 			 * Enqueue this dip's deviname.
62330Sstevel@tonic-gate 			 * No need to hold a lock while enqueuing since this
62340Sstevel@tonic-gate 			 * is the only thread doing the enqueue and no one
62350Sstevel@tonic-gate 			 * walks the queue while we are in multithreaded
62360Sstevel@tonic-gate 			 * unconfiguration.
62370Sstevel@tonic-gate 			 */
62380Sstevel@tonic-gate 			brn = brevq_enqueue(hdl->mtc_brevqp, dip, NULL);
62390Sstevel@tonic-gate 		}
62400Sstevel@tonic-gate 
62410Sstevel@tonic-gate 		/*
62420Sstevel@tonic-gate 		 * Hold the child that we are processing so he does not get
62430Sstevel@tonic-gate 		 * removed. The corrisponding ndi_rele_devi() for children
62440Sstevel@tonic-gate 		 * that are not being skipped is done at the end of
62450Sstevel@tonic-gate 		 * mt_config_thread().
62460Sstevel@tonic-gate 		 */
62470Sstevel@tonic-gate 		ndi_hold_devi(dip);
62480Sstevel@tonic-gate 
62490Sstevel@tonic-gate 		/*
62500Sstevel@tonic-gate 		 * skip leaf nodes and (for configure) nodes not
62510Sstevel@tonic-gate 		 * fully attached.
62520Sstevel@tonic-gate 		 */
62530Sstevel@tonic-gate 		if (is_leaf_node(dip) ||
62540Sstevel@tonic-gate 		    (hdl->mtc_op == MT_CONFIG_OP &&
62550Sstevel@tonic-gate 		    i_ddi_node_state(dip) < DS_READY)) {
62560Sstevel@tonic-gate 			ndi_rele_devi(dip);
62570Sstevel@tonic-gate 			dip = ddi_get_next_sibling(dip);
62580Sstevel@tonic-gate 			continue;
62590Sstevel@tonic-gate 		}
62600Sstevel@tonic-gate 
62610Sstevel@tonic-gate 		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
62620Sstevel@tonic-gate 		mcd->mtc_dip = dip;
62630Sstevel@tonic-gate 		mcd->mtc_hdl = hdl;
62640Sstevel@tonic-gate 		mcd->mtc_brn = brn;
62650Sstevel@tonic-gate 
62660Sstevel@tonic-gate 		/*
62670Sstevel@tonic-gate 		 * Switch a 'driver' operation to an 'all' operation below a
62680Sstevel@tonic-gate 		 * node bound to the driver.
62690Sstevel@tonic-gate 		 */
6270*662Sszhou 		if ((major == (major_t)-1) || (major == ddi_driver_major(dip)))
62710Sstevel@tonic-gate 			mcd->mtc_major = (major_t)-1;
62720Sstevel@tonic-gate 		else
62730Sstevel@tonic-gate 			mcd->mtc_major = major;
62740Sstevel@tonic-gate 
62750Sstevel@tonic-gate 		/*
62760Sstevel@tonic-gate 		 * The unconfig-driver to unconfig-all conversion above
62770Sstevel@tonic-gate 		 * constitutes an autodetach for NDI_DETACH_DRIVER calls,
62780Sstevel@tonic-gate 		 * set NDI_AUTODETACH.
62790Sstevel@tonic-gate 		 */
62800Sstevel@tonic-gate 		mcd->mtc_flags = hdl->mtc_flags;
62810Sstevel@tonic-gate 		if ((mcd->mtc_flags & NDI_DETACH_DRIVER) &&
62820Sstevel@tonic-gate 		    (hdl->mtc_op == MT_UNCONFIG_OP) &&
62830Sstevel@tonic-gate 		    (major == ddi_driver_major(pdip)))
62840Sstevel@tonic-gate 			mcd->mtc_flags |= NDI_AUTODETACH;
62850Sstevel@tonic-gate 
62860Sstevel@tonic-gate 		mutex_enter(&hdl->mtc_lock);
62870Sstevel@tonic-gate 		hdl->mtc_thr_count++;
62880Sstevel@tonic-gate 		mutex_exit(&hdl->mtc_lock);
62890Sstevel@tonic-gate 
62900Sstevel@tonic-gate 		/*
62910Sstevel@tonic-gate 		 * Add to end of list to process after ndi_devi_exit to avoid
62920Sstevel@tonic-gate 		 * locking differences depending on value of mtc_off.
62930Sstevel@tonic-gate 		 */
62940Sstevel@tonic-gate 		mcd->mtc_next = NULL;
62950Sstevel@tonic-gate 		if (mcd_head == NULL)
62960Sstevel@tonic-gate 			mcd_head = mcd;
62970Sstevel@tonic-gate 		else
62980Sstevel@tonic-gate 			mcd_tail->mtc_next = mcd;
62990Sstevel@tonic-gate 		mcd_tail = mcd;
63000Sstevel@tonic-gate 
63010Sstevel@tonic-gate 		dip = ddi_get_next_sibling(dip);
63020Sstevel@tonic-gate 	}
63030Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
63040Sstevel@tonic-gate 
63050Sstevel@tonic-gate 	/* go through the list of held children */
63060Sstevel@tonic-gate 	for (mcd = mcd_head; mcd; mcd = mcd_head) {
63070Sstevel@tonic-gate 		mcd_head = mcd->mtc_next;
63080Sstevel@tonic-gate 		if (mtc_off)
63090Sstevel@tonic-gate 			mt_config_thread(mcd);
63100Sstevel@tonic-gate 		else
63110Sstevel@tonic-gate 			(void) thread_create(NULL, 0, mt_config_thread, mcd,
63120Sstevel@tonic-gate 			    0, &p0, TS_RUN, minclsyspri);
63130Sstevel@tonic-gate 	}
63140Sstevel@tonic-gate }
63150Sstevel@tonic-gate 
63160Sstevel@tonic-gate static void
63170Sstevel@tonic-gate mt_config_driver(struct mt_config_handle *hdl)
63180Sstevel@tonic-gate {
63190Sstevel@tonic-gate 	major_t			par_major = hdl->mtc_parmajor;
63200Sstevel@tonic-gate 	major_t			major = hdl->mtc_major;
63210Sstevel@tonic-gate 	struct devnames		*dnp = &devnamesp[par_major];
63220Sstevel@tonic-gate 	dev_info_t		*dip;
63230Sstevel@tonic-gate 	struct mt_config_data	*mcd_head = NULL;
63240Sstevel@tonic-gate 	struct mt_config_data	*mcd_tail = NULL;
63250Sstevel@tonic-gate 	struct mt_config_data	*mcd;
63260Sstevel@tonic-gate #ifdef DEBUG
63270Sstevel@tonic-gate 	timestruc_t		end_time;
63280Sstevel@tonic-gate 
63290Sstevel@tonic-gate 	/* Update total_time in handle */
63300Sstevel@tonic-gate 	gethrestime(&end_time);
63310Sstevel@tonic-gate 	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
63320Sstevel@tonic-gate #endif
63330Sstevel@tonic-gate 	ASSERT(par_major != (major_t)-1);
63340Sstevel@tonic-gate 	ASSERT(major != (major_t)-1);
63350Sstevel@tonic-gate 
63360Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
63370Sstevel@tonic-gate 	dip = devnamesp[par_major].dn_head;
63380Sstevel@tonic-gate 	while (dip) {
63390Sstevel@tonic-gate 		/*
63400Sstevel@tonic-gate 		 * Hold the child that we are processing so he does not get
63410Sstevel@tonic-gate 		 * removed. The corrisponding ndi_rele_devi() for children
63420Sstevel@tonic-gate 		 * that are not being skipped is done at the end of
63430Sstevel@tonic-gate 		 * mt_config_thread().
63440Sstevel@tonic-gate 		 */
63450Sstevel@tonic-gate 		ndi_hold_devi(dip);
63460Sstevel@tonic-gate 
63470Sstevel@tonic-gate 		/* skip leaf nodes and nodes not fully attached */
63480Sstevel@tonic-gate 		if ((i_ddi_node_state(dip) < DS_READY) || is_leaf_node(dip)) {
63490Sstevel@tonic-gate 			ndi_rele_devi(dip);
63500Sstevel@tonic-gate 			dip = ddi_get_next(dip);
63510Sstevel@tonic-gate 			continue;
63520Sstevel@tonic-gate 		}
63530Sstevel@tonic-gate 
63540Sstevel@tonic-gate 		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
63550Sstevel@tonic-gate 		mcd->mtc_dip = dip;
63560Sstevel@tonic-gate 		mcd->mtc_hdl = hdl;
63570Sstevel@tonic-gate 		mcd->mtc_major = major;
63580Sstevel@tonic-gate 		mcd->mtc_flags = hdl->mtc_flags;
63590Sstevel@tonic-gate 
63600Sstevel@tonic-gate 		mutex_enter(&hdl->mtc_lock);
63610Sstevel@tonic-gate 		hdl->mtc_thr_count++;
63620Sstevel@tonic-gate 		mutex_exit(&hdl->mtc_lock);
63630Sstevel@tonic-gate 
63640Sstevel@tonic-gate 		/*
63650Sstevel@tonic-gate 		 * Add to end of list to process after UNLOCK_DEV_OPS to avoid
63660Sstevel@tonic-gate 		 * locking differences depending on value of mtc_off.
63670Sstevel@tonic-gate 		 */
63680Sstevel@tonic-gate 		mcd->mtc_next = NULL;
63690Sstevel@tonic-gate 		if (mcd_head == NULL)
63700Sstevel@tonic-gate 			mcd_head = mcd;
63710Sstevel@tonic-gate 		else
63720Sstevel@tonic-gate 			mcd_tail->mtc_next = mcd;
63730Sstevel@tonic-gate 		mcd_tail = mcd;
63740Sstevel@tonic-gate 
63750Sstevel@tonic-gate 		dip = ddi_get_next(dip);
63760Sstevel@tonic-gate 	}
63770Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
63780Sstevel@tonic-gate 
63790Sstevel@tonic-gate 	/* go through the list of held children */
63800Sstevel@tonic-gate 	for (mcd = mcd_head; mcd; mcd = mcd_head) {
63810Sstevel@tonic-gate 		mcd_head = mcd->mtc_next;
63820Sstevel@tonic-gate 		if (mtc_off)
63830Sstevel@tonic-gate 			mt_config_thread(mcd);
63840Sstevel@tonic-gate 		else
63850Sstevel@tonic-gate 			(void) thread_create(NULL, 0, mt_config_thread, mcd,
63860Sstevel@tonic-gate 			    0, &p0, TS_RUN, minclsyspri);
63870Sstevel@tonic-gate 	}
63880Sstevel@tonic-gate }
63890Sstevel@tonic-gate 
63900Sstevel@tonic-gate /*
63910Sstevel@tonic-gate  * Given the nodeid for a persistent (PROM or SID) node, return
63920Sstevel@tonic-gate  * the corresponding devinfo node
63930Sstevel@tonic-gate  * NOTE: This function will return NULL for .conf nodeids.
63940Sstevel@tonic-gate  */
63950Sstevel@tonic-gate dev_info_t *
63960Sstevel@tonic-gate e_ddi_nodeid_to_dip(dnode_t nodeid)
63970Sstevel@tonic-gate {
63980Sstevel@tonic-gate 	dev_info_t		*dip = NULL;
63990Sstevel@tonic-gate 	struct devi_nodeid	*prev, *elem;
64000Sstevel@tonic-gate 
64010Sstevel@tonic-gate 	mutex_enter(&devimap->dno_lock);
64020Sstevel@tonic-gate 
64030Sstevel@tonic-gate 	prev = NULL;
64040Sstevel@tonic-gate 	for (elem = devimap->dno_head; elem; elem = elem->next) {
64050Sstevel@tonic-gate 		if (elem->nodeid == nodeid) {
64060Sstevel@tonic-gate 			ndi_hold_devi(elem->dip);
64070Sstevel@tonic-gate 			dip = elem->dip;
64080Sstevel@tonic-gate 			break;
64090Sstevel@tonic-gate 		}
64100Sstevel@tonic-gate 		prev = elem;
64110Sstevel@tonic-gate 	}
64120Sstevel@tonic-gate 
64130Sstevel@tonic-gate 	/*
64140Sstevel@tonic-gate 	 * Move to head for faster lookup next time
64150Sstevel@tonic-gate 	 */
64160Sstevel@tonic-gate 	if (elem && prev) {
64170Sstevel@tonic-gate 		prev->next = elem->next;
64180Sstevel@tonic-gate 		elem->next = devimap->dno_head;
64190Sstevel@tonic-gate 		devimap->dno_head = elem;
64200Sstevel@tonic-gate 	}
64210Sstevel@tonic-gate 
64220Sstevel@tonic-gate 	mutex_exit(&devimap->dno_lock);
64230Sstevel@tonic-gate 	return (dip);
64240Sstevel@tonic-gate }
64250Sstevel@tonic-gate 
64260Sstevel@tonic-gate static void
64270Sstevel@tonic-gate free_cache_task(void *arg)
64280Sstevel@tonic-gate {
64290Sstevel@tonic-gate 	ASSERT(arg == NULL);
64300Sstevel@tonic-gate 
64310Sstevel@tonic-gate 	mutex_enter(&di_cache.cache_lock);
64320Sstevel@tonic-gate 
64330Sstevel@tonic-gate 	/*
64340Sstevel@tonic-gate 	 * The cache can be invalidated without holding the lock
64350Sstevel@tonic-gate 	 * but it can be made valid again only while the lock is held.
64360Sstevel@tonic-gate 	 * So if the cache is invalid when the lock is held, it will
64370Sstevel@tonic-gate 	 * stay invalid until lock is released.
64380Sstevel@tonic-gate 	 */
64390Sstevel@tonic-gate 	if (!di_cache.cache_valid)
64400Sstevel@tonic-gate 		i_ddi_di_cache_free(&di_cache);
64410Sstevel@tonic-gate 
64420Sstevel@tonic-gate 	mutex_exit(&di_cache.cache_lock);
64430Sstevel@tonic-gate 
64440Sstevel@tonic-gate 	if (di_cache_debug)
64450Sstevel@tonic-gate 		cmn_err(CE_NOTE, "system_taskq: di_cache freed");
64460Sstevel@tonic-gate }
64470Sstevel@tonic-gate 
64480Sstevel@tonic-gate extern int modrootloaded;
64490Sstevel@tonic-gate 
64500Sstevel@tonic-gate void
64510Sstevel@tonic-gate i_ddi_di_cache_free(struct di_cache *cache)
64520Sstevel@tonic-gate {
64530Sstevel@tonic-gate 	int	error;
64540Sstevel@tonic-gate 
64550Sstevel@tonic-gate 	ASSERT(mutex_owned(&cache->cache_lock));
64560Sstevel@tonic-gate 
64570Sstevel@tonic-gate 	if (cache->cache_size) {
64580Sstevel@tonic-gate 		ASSERT(cache->cache_size > 0);
64590Sstevel@tonic-gate 		ASSERT(cache->cache_data);
64600Sstevel@tonic-gate 
64610Sstevel@tonic-gate 		kmem_free(cache->cache_data, cache->cache_size);
64620Sstevel@tonic-gate 		cache->cache_data = NULL;
64630Sstevel@tonic-gate 		cache->cache_size = 0;
64640Sstevel@tonic-gate 
64650Sstevel@tonic-gate 		if (di_cache_debug)
64660Sstevel@tonic-gate 			cmn_err(CE_NOTE, "i_ddi_di_cache_free: freed cachemem");
64670Sstevel@tonic-gate 	} else {
64680Sstevel@tonic-gate 		ASSERT(cache->cache_data == NULL);
64690Sstevel@tonic-gate 		if (di_cache_debug)
64700Sstevel@tonic-gate 			cmn_err(CE_NOTE, "i_ddi_di_cache_free: NULL cache");
64710Sstevel@tonic-gate 	}
64720Sstevel@tonic-gate 
64730Sstevel@tonic-gate 	if (!modrootloaded || rootvp == NULL || vn_is_readonly(rootvp)) {
64740Sstevel@tonic-gate 		if (di_cache_debug) {
64750Sstevel@tonic-gate 			cmn_err(CE_WARN, "/ not mounted/RDONLY. Skip unlink");
64760Sstevel@tonic-gate 		}
64770Sstevel@tonic-gate 		return;
64780Sstevel@tonic-gate 	}
64790Sstevel@tonic-gate 
64800Sstevel@tonic-gate 	error = vn_remove(DI_CACHE_FILE, UIO_SYSSPACE, RMFILE);
64810Sstevel@tonic-gate 	if (di_cache_debug && error && error != ENOENT) {
64820Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: unlink failed: %d", DI_CACHE_FILE, error);
64830Sstevel@tonic-gate 	} else if (di_cache_debug && !error) {
64840Sstevel@tonic-gate 		cmn_err(CE_NOTE, "i_ddi_di_cache_free: unlinked cache file");
64850Sstevel@tonic-gate 	}
64860Sstevel@tonic-gate }
64870Sstevel@tonic-gate 
64880Sstevel@tonic-gate void
64890Sstevel@tonic-gate i_ddi_di_cache_invalidate(int kmflag)
64900Sstevel@tonic-gate {
64910Sstevel@tonic-gate 	uint_t	flag;
64920Sstevel@tonic-gate 
64930Sstevel@tonic-gate 	if (!modrootloaded || !i_ddi_io_initialized()) {
64940Sstevel@tonic-gate 		if (di_cache_debug)
64950Sstevel@tonic-gate 			cmn_err(CE_NOTE, "I/O not inited. Skipping invalidate");
64960Sstevel@tonic-gate 		return;
64970Sstevel@tonic-gate 	}
64980Sstevel@tonic-gate 
64990Sstevel@tonic-gate 	/*
65000Sstevel@tonic-gate 	 * Invalidate the in-core cache
65010Sstevel@tonic-gate 	 */
65020Sstevel@tonic-gate 	atomic_and_32(&di_cache.cache_valid, 0);
65030Sstevel@tonic-gate 
65040Sstevel@tonic-gate 	flag = (kmflag == KM_SLEEP) ? TQ_SLEEP : TQ_NOSLEEP;
65050Sstevel@tonic-gate 
65060Sstevel@tonic-gate 	(void) taskq_dispatch(system_taskq, free_cache_task, NULL, flag);
65070Sstevel@tonic-gate 
65080Sstevel@tonic-gate 	if (di_cache_debug) {
65090Sstevel@tonic-gate 		cmn_err(CE_NOTE, "invalidation with km_flag: %s",
65100Sstevel@tonic-gate 		    kmflag == KM_SLEEP ? "KM_SLEEP" : "KM_NOSLEEP");
65110Sstevel@tonic-gate 	}
65120Sstevel@tonic-gate }
65130Sstevel@tonic-gate 
65140Sstevel@tonic-gate 
65150Sstevel@tonic-gate static void
65160Sstevel@tonic-gate i_bind_vhci_node(dev_info_t *dip)
65170Sstevel@tonic-gate {
65180Sstevel@tonic-gate 	char	*node_name;
65190Sstevel@tonic-gate 
65200Sstevel@tonic-gate 	node_name = i_ddi_strdup(ddi_node_name(dip), KM_SLEEP);
65210Sstevel@tonic-gate 	i_ddi_set_binding_name(dip, node_name);
65220Sstevel@tonic-gate 	DEVI(dip)->devi_major = ddi_name_to_major(node_name);
65230Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_BOUND);
65240Sstevel@tonic-gate }
65250Sstevel@tonic-gate 
65260Sstevel@tonic-gate 
65270Sstevel@tonic-gate static void
65280Sstevel@tonic-gate i_free_vhci_bind_name(dev_info_t *dip)
65290Sstevel@tonic-gate {
65300Sstevel@tonic-gate 	if (DEVI(dip)->devi_binding_name) {
65310Sstevel@tonic-gate 		kmem_free(DEVI(dip)->devi_binding_name,
65320Sstevel@tonic-gate 		    sizeof (ddi_node_name(dip)));
65330Sstevel@tonic-gate 	}
65340Sstevel@tonic-gate }
65350Sstevel@tonic-gate 
65360Sstevel@tonic-gate 
65370Sstevel@tonic-gate static char vhci_node_addr[2];
65380Sstevel@tonic-gate 
65390Sstevel@tonic-gate static int
65400Sstevel@tonic-gate i_init_vhci_node(dev_info_t *dip)
65410Sstevel@tonic-gate {
65420Sstevel@tonic-gate 	add_global_props(dip);
65430Sstevel@tonic-gate 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
65440Sstevel@tonic-gate 	if (DEVI(dip)->devi_ops == NULL)
65450Sstevel@tonic-gate 		return (-1);
65460Sstevel@tonic-gate 
65470Sstevel@tonic-gate 	DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
65480Sstevel@tonic-gate 	e_ddi_keep_instance(dip);
65490Sstevel@tonic-gate 	vhci_node_addr[0]	= '\0';
65500Sstevel@tonic-gate 	ddi_set_name_addr(dip, vhci_node_addr);
65510Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_INITIALIZED);
65520Sstevel@tonic-gate 	return (0);
65530Sstevel@tonic-gate }
65540Sstevel@tonic-gate 
65550Sstevel@tonic-gate static void
65560Sstevel@tonic-gate i_link_vhci_node(dev_info_t *dip)
65570Sstevel@tonic-gate {
65580Sstevel@tonic-gate 	/*
65590Sstevel@tonic-gate 	 * scsi_vhci should be kept left most of the device tree.
65600Sstevel@tonic-gate 	 */
65610Sstevel@tonic-gate 	mutex_enter(&global_vhci_lock);
65620Sstevel@tonic-gate 	if (scsi_vhci_dip) {
65630Sstevel@tonic-gate 		DEVI(dip)->devi_sibling = DEVI(scsi_vhci_dip)->devi_sibling;
65640Sstevel@tonic-gate 		DEVI(scsi_vhci_dip)->devi_sibling = DEVI(dip);
65650Sstevel@tonic-gate 	} else {
65660Sstevel@tonic-gate 		DEVI(dip)->devi_sibling = DEVI(top_devinfo)->devi_child;
65670Sstevel@tonic-gate 		DEVI(top_devinfo)->devi_child = DEVI(dip);
65680Sstevel@tonic-gate 	}
65690Sstevel@tonic-gate 	mutex_exit(&global_vhci_lock);
65700Sstevel@tonic-gate }
65710Sstevel@tonic-gate 
65720Sstevel@tonic-gate 
65730Sstevel@tonic-gate /*
65740Sstevel@tonic-gate  * This a special routine to enumerate vhci node (child of rootnex
65750Sstevel@tonic-gate  * node) without holding the ndi_devi_enter() lock. The device node
65760Sstevel@tonic-gate  * is allocated, initialized and brought into DS_READY state before
65770Sstevel@tonic-gate  * inserting into the device tree. The VHCI node is handcrafted
65780Sstevel@tonic-gate  * here to bring the node to DS_READY, similar to rootnex node.
65790Sstevel@tonic-gate  *
65800Sstevel@tonic-gate  * The global_vhci_lock protects linking the node into the device
65810Sstevel@tonic-gate  * as same lock is held before linking/unlinking any direct child
65820Sstevel@tonic-gate  * of rootnex children.
65830Sstevel@tonic-gate  *
65840Sstevel@tonic-gate  * This routine is a workaround to handle a possible deadlock
65850Sstevel@tonic-gate  * that occurs while trying to enumerate node in a different sub-tree
65860Sstevel@tonic-gate  * during _init/_attach entry points.
65870Sstevel@tonic-gate  */
65880Sstevel@tonic-gate /*ARGSUSED*/
65890Sstevel@tonic-gate dev_info_t *
65900Sstevel@tonic-gate ndi_devi_config_vhci(char *drvname, int flags)
65910Sstevel@tonic-gate {
65920Sstevel@tonic-gate 	struct devnames		*dnp;
65930Sstevel@tonic-gate 	dev_info_t		*dip;
65940Sstevel@tonic-gate 	major_t			major = ddi_name_to_major(drvname);
65950Sstevel@tonic-gate 
65960Sstevel@tonic-gate 	if (major == -1)
65970Sstevel@tonic-gate 		return (NULL);
65980Sstevel@tonic-gate 
65990Sstevel@tonic-gate 	/* Make sure we create the VHCI node only once */
66000Sstevel@tonic-gate 	dnp = &devnamesp[major];
66010Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
66020Sstevel@tonic-gate 	if (dnp->dn_head) {
66030Sstevel@tonic-gate 		dip = dnp->dn_head;
66040Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
66050Sstevel@tonic-gate 		return (dip);
66060Sstevel@tonic-gate 	}
66070Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
66080Sstevel@tonic-gate 
66090Sstevel@tonic-gate 	/* Allocate the VHCI node */
66100Sstevel@tonic-gate 	ndi_devi_alloc_sleep(top_devinfo, drvname, DEVI_SID_NODEID, &dip);
66110Sstevel@tonic-gate 	ndi_hold_devi(dip);
66120Sstevel@tonic-gate 
66130Sstevel@tonic-gate 	/* Mark the node as VHCI */
66140Sstevel@tonic-gate 	DEVI(dip)->devi_node_attributes |= DDI_VHCI_NODE;
66150Sstevel@tonic-gate 
66160Sstevel@tonic-gate 	i_ddi_add_devimap(dip);
66170Sstevel@tonic-gate 	i_bind_vhci_node(dip);
66180Sstevel@tonic-gate 	if (i_init_vhci_node(dip) == -1) {
66190Sstevel@tonic-gate 		i_free_vhci_bind_name(dip);
66200Sstevel@tonic-gate 		ndi_rele_devi(dip);
66210Sstevel@tonic-gate 		(void) ndi_devi_free(dip);
66220Sstevel@tonic-gate 		return (NULL);
66230Sstevel@tonic-gate 	}
66240Sstevel@tonic-gate 
6625495Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
66260Sstevel@tonic-gate 	DEVI_SET_ATTACHING(dip);
6627495Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
6628495Scth 
66290Sstevel@tonic-gate 	if (devi_attach(dip, DDI_ATTACH) != DDI_SUCCESS) {
66300Sstevel@tonic-gate 		cmn_err(CE_CONT, "Could not attach %s driver", drvname);
66310Sstevel@tonic-gate 		e_ddi_free_instance(dip, vhci_node_addr);
66320Sstevel@tonic-gate 		i_free_vhci_bind_name(dip);
66330Sstevel@tonic-gate 		ndi_rele_devi(dip);
66340Sstevel@tonic-gate 		(void) ndi_devi_free(dip);
66350Sstevel@tonic-gate 		return (NULL);
66360Sstevel@tonic-gate 	}
6637495Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
66380Sstevel@tonic-gate 	DEVI_CLR_ATTACHING(dip);
6639495Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
66400Sstevel@tonic-gate 
66410Sstevel@tonic-gate 	i_link_vhci_node(dip);
66420Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_READY);
66430Sstevel@tonic-gate 
66440Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
66450Sstevel@tonic-gate 	dnp->dn_flags |= DN_DRIVER_HELD;
66460Sstevel@tonic-gate 	dnp->dn_head = dip;
66470Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
66480Sstevel@tonic-gate 
66490Sstevel@tonic-gate 	i_ndi_devi_report_status_change(dip, NULL);
66500Sstevel@tonic-gate 
66510Sstevel@tonic-gate 	return (dip);
66520Sstevel@tonic-gate }
66530Sstevel@tonic-gate 
66540Sstevel@tonic-gate /*
66550Sstevel@tonic-gate  * ibt_hw_is_present() returns 0 when there is no IB hardware actively
66560Sstevel@tonic-gate  * running.  This is primarily useful for modules like rpcmod which
66570Sstevel@tonic-gate  * needs a quick check to decide whether or not it should try to use
66580Sstevel@tonic-gate  * InfiniBand
66590Sstevel@tonic-gate  */
66600Sstevel@tonic-gate int ib_hw_status = 0;
66610Sstevel@tonic-gate int
66620Sstevel@tonic-gate ibt_hw_is_present()
66630Sstevel@tonic-gate {
66640Sstevel@tonic-gate 	return (ib_hw_status);
66650Sstevel@tonic-gate }
6666