xref: /onnv-gate/usr/src/uts/common/os/devcfg.c (revision 9066)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51961Scth  * Common Development and Distribution License (the "License").
61961Scth  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
228640SVikram.Hegde@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/note.h>
270Sstevel@tonic-gate #include <sys/t_lock.h>
280Sstevel@tonic-gate #include <sys/cmn_err.h>
290Sstevel@tonic-gate #include <sys/instance.h>
300Sstevel@tonic-gate #include <sys/conf.h>
310Sstevel@tonic-gate #include <sys/stat.h>
320Sstevel@tonic-gate #include <sys/ddi.h>
330Sstevel@tonic-gate #include <sys/hwconf.h>
340Sstevel@tonic-gate #include <sys/sunddi.h>
350Sstevel@tonic-gate #include <sys/sunndi.h>
360Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
370Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
380Sstevel@tonic-gate #include <sys/modctl.h>
394845Svikram #include <sys/contract/device_impl.h>
400Sstevel@tonic-gate #include <sys/dacf.h>
410Sstevel@tonic-gate #include <sys/promif.h>
427656SSherry.Moore@Sun.COM #include <sys/pci.h>
430Sstevel@tonic-gate #include <sys/cpuvar.h>
440Sstevel@tonic-gate #include <sys/pathname.h>
450Sstevel@tonic-gate #include <sys/taskq.h>
460Sstevel@tonic-gate #include <sys/sysevent.h>
470Sstevel@tonic-gate #include <sys/sunmdi.h>
480Sstevel@tonic-gate #include <sys/stream.h>
490Sstevel@tonic-gate #include <sys/strsubr.h>
500Sstevel@tonic-gate #include <sys/fs/snode.h>
510Sstevel@tonic-gate #include <sys/fs/dv_node.h>
522621Sllai1 #include <sys/reboot.h>
534845Svikram #include <sys/sysmacros.h>
547656SSherry.Moore@Sun.COM #include <sys/systm.h>
554845Svikram #include <sys/sunldi.h>
564845Svikram #include <sys/sunldi_impl.h>
570Sstevel@tonic-gate 
588249SVikram.Hegde@Sun.COM #if defined(__i386) || defined(__amd64)
598249SVikram.Hegde@Sun.COM #if !defined(__xpv)
608249SVikram.Hegde@Sun.COM #include <sys/iommulib.h>
618249SVikram.Hegde@Sun.COM #endif
628249SVikram.Hegde@Sun.COM #endif
638249SVikram.Hegde@Sun.COM 
640Sstevel@tonic-gate #ifdef DEBUG
650Sstevel@tonic-gate int ddidebug = DDI_AUDIT;
660Sstevel@tonic-gate #else
670Sstevel@tonic-gate int ddidebug = 0;
680Sstevel@tonic-gate #endif
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #define	MT_CONFIG_OP	0
710Sstevel@tonic-gate #define	MT_UNCONFIG_OP	1
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /* Multi-threaded configuration */
740Sstevel@tonic-gate struct mt_config_handle {
750Sstevel@tonic-gate 	kmutex_t mtc_lock;
760Sstevel@tonic-gate 	kcondvar_t mtc_cv;
770Sstevel@tonic-gate 	int mtc_thr_count;
780Sstevel@tonic-gate 	dev_info_t *mtc_pdip;	/* parent dip for mt_config_children */
790Sstevel@tonic-gate 	dev_info_t **mtc_fdip;	/* "a" dip where unconfigure failed */
800Sstevel@tonic-gate 	major_t mtc_parmajor;	/* parent major for mt_config_driver */
810Sstevel@tonic-gate 	major_t mtc_major;
820Sstevel@tonic-gate 	int mtc_flags;
830Sstevel@tonic-gate 	int mtc_op;		/* config or unconfig */
840Sstevel@tonic-gate 	int mtc_error;		/* operation error */
850Sstevel@tonic-gate 	struct brevq_node **mtc_brevqp;	/* outstanding branch events queue */
860Sstevel@tonic-gate #ifdef DEBUG
870Sstevel@tonic-gate 	int total_time;
880Sstevel@tonic-gate 	timestruc_t start_time;
890Sstevel@tonic-gate #endif /* DEBUG */
900Sstevel@tonic-gate };
910Sstevel@tonic-gate 
920Sstevel@tonic-gate struct devi_nodeid {
93789Sahrens 	pnode_t nodeid;
940Sstevel@tonic-gate 	dev_info_t *dip;
950Sstevel@tonic-gate 	struct devi_nodeid *next;
960Sstevel@tonic-gate };
970Sstevel@tonic-gate 
980Sstevel@tonic-gate struct devi_nodeid_list {
990Sstevel@tonic-gate 	kmutex_t dno_lock;		/* Protects other fields */
1000Sstevel@tonic-gate 	struct devi_nodeid *dno_head;	/* list of devi nodeid elements */
1010Sstevel@tonic-gate 	struct devi_nodeid *dno_free;	/* Free list */
1020Sstevel@tonic-gate 	uint_t dno_list_length;		/* number of dips in list */
1030Sstevel@tonic-gate };
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate /* used to keep track of branch remove events to be generated */
1060Sstevel@tonic-gate struct brevq_node {
1071317Scth 	char *brn_deviname;
1081317Scth 	struct brevq_node *brn_sibling;
1091317Scth 	struct brevq_node *brn_child;
1100Sstevel@tonic-gate };
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate static struct devi_nodeid_list devi_nodeid_list;
1130Sstevel@tonic-gate static struct devi_nodeid_list *devimap = &devi_nodeid_list;
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate /*
1160Sstevel@tonic-gate  * Well known nodes which are attached first at boot time.
1170Sstevel@tonic-gate  */
1180Sstevel@tonic-gate dev_info_t *top_devinfo;		/* root of device tree */
1190Sstevel@tonic-gate dev_info_t *options_dip;
1200Sstevel@tonic-gate dev_info_t *pseudo_dip;
1210Sstevel@tonic-gate dev_info_t *clone_dip;
1220Sstevel@tonic-gate dev_info_t *scsi_vhci_dip;		/* MPXIO dip */
1230Sstevel@tonic-gate major_t clone_major;
1240Sstevel@tonic-gate 
1252621Sllai1 /*
1262621Sllai1  * A non-global zone's /dev is derived from the device tree.
1272621Sllai1  * This generation number serves to indicate when a zone's
1282621Sllai1  * /dev may need to be updated.
1292621Sllai1  */
1302621Sllai1 volatile ulong_t devtree_gen;		/* generation number */
1312621Sllai1 
1320Sstevel@tonic-gate /* block all future dev_info state changes */
1337656SSherry.Moore@Sun.COM hrtime_t volatile devinfo_freeze = 0;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate /* number of dev_info attaches/detaches currently in progress */
1360Sstevel@tonic-gate static ulong_t devinfo_attach_detach = 0;
1370Sstevel@tonic-gate 
1387576SJerry.Gilliam@Sun.COM extern int	sys_shutdown;
1390Sstevel@tonic-gate extern kmutex_t global_vhci_lock;
1400Sstevel@tonic-gate 
1412621Sllai1 /* bitset of DS_SYSAVAIL & DS_RECONFIG - no races, no lock */
1422621Sllai1 static int devname_state = 0;
1432621Sllai1 
1440Sstevel@tonic-gate /*
1450Sstevel@tonic-gate  * The devinfo snapshot cache and related variables.
1460Sstevel@tonic-gate  * The only field in the di_cache structure that needs initialization
1470Sstevel@tonic-gate  * is the mutex (cache_lock). However, since this is an adaptive mutex
1480Sstevel@tonic-gate  * (MUTEX_DEFAULT) - it is automatically initialized by being allocated
1490Sstevel@tonic-gate  * in zeroed memory (static storage class). Therefore no explicit
1500Sstevel@tonic-gate  * initialization of the di_cache structure is needed.
1510Sstevel@tonic-gate  */
1520Sstevel@tonic-gate struct di_cache	di_cache = {1};
1530Sstevel@tonic-gate int		di_cache_debug = 0;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate /* For ddvis, which needs pseudo children under PCI */
1560Sstevel@tonic-gate int pci_allow_pseudo_children = 0;
1570Sstevel@tonic-gate 
1584145Scth /* Allow path-oriented alias driver binding on driver.conf enumerated nodes */
1594145Scth int driver_conf_allow_path_alias = 1;
1604145Scth 
1610Sstevel@tonic-gate /*
1620Sstevel@tonic-gate  * The following switch is for service people, in case a
1630Sstevel@tonic-gate  * 3rd party driver depends on identify(9e) being called.
1640Sstevel@tonic-gate  */
1650Sstevel@tonic-gate int identify_9e = 0;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate int mtc_off;					/* turn off mt config */
1680Sstevel@tonic-gate 
1697656SSherry.Moore@Sun.COM int quiesce_debug = 0;
1707656SSherry.Moore@Sun.COM 
1710Sstevel@tonic-gate static kmem_cache_t *ddi_node_cache;		/* devinfo node cache */
1720Sstevel@tonic-gate static devinfo_log_header_t *devinfo_audit_log;	/* devinfo log */
1730Sstevel@tonic-gate static int devinfo_log_size;			/* size in pages */
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate static int lookup_compatible(dev_info_t *, uint_t);
1760Sstevel@tonic-gate static char *encode_composite_string(char **, uint_t, size_t *, uint_t);
1770Sstevel@tonic-gate static void link_to_driver_list(dev_info_t *);
1780Sstevel@tonic-gate static void unlink_from_driver_list(dev_info_t *);
1790Sstevel@tonic-gate static void add_to_dn_list(struct devnames *, dev_info_t *);
1800Sstevel@tonic-gate static void remove_from_dn_list(struct devnames *, dev_info_t *);
1810Sstevel@tonic-gate static dev_info_t *find_child_by_callback(dev_info_t *, char *, char *,
1820Sstevel@tonic-gate     int (*)(dev_info_t *, char *, int));
1830Sstevel@tonic-gate static dev_info_t *find_duplicate_child();
1840Sstevel@tonic-gate static void add_global_props(dev_info_t *);
1850Sstevel@tonic-gate static void remove_global_props(dev_info_t *);
1860Sstevel@tonic-gate static int uninit_node(dev_info_t *);
1870Sstevel@tonic-gate static void da_log_init(void);
1880Sstevel@tonic-gate static void da_log_enter(dev_info_t *);
1890Sstevel@tonic-gate static int walk_devs(dev_info_t *, int (*f)(dev_info_t *, void *), void *, int);
1900Sstevel@tonic-gate static int reset_nexus_flags(dev_info_t *, void *);
1910Sstevel@tonic-gate static void ddi_optimize_dtree(dev_info_t *);
1920Sstevel@tonic-gate static int is_leaf_node(dev_info_t *);
1930Sstevel@tonic-gate static struct mt_config_handle *mt_config_init(dev_info_t *, dev_info_t **,
1940Sstevel@tonic-gate     int, major_t, int, struct brevq_node **);
1950Sstevel@tonic-gate static void mt_config_children(struct mt_config_handle *);
1960Sstevel@tonic-gate static void mt_config_driver(struct mt_config_handle *);
1970Sstevel@tonic-gate static int mt_config_fini(struct mt_config_handle *);
1980Sstevel@tonic-gate static int devi_unconfig_common(dev_info_t *, dev_info_t **, int, major_t,
1990Sstevel@tonic-gate     struct brevq_node **);
2000Sstevel@tonic-gate static int
2010Sstevel@tonic-gate ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
2020Sstevel@tonic-gate     dev_info_t **childp, int flags);
2031093Shiremath static void i_link_vhci_node(dev_info_t *);
2042155Scth static void ndi_devi_exit_and_wait(dev_info_t *dip,
2052155Scth     int circular, clock_t end_time);
2064145Scth static int ndi_devi_unbind_driver(dev_info_t *dip);
2070Sstevel@tonic-gate 
2084845Svikram static void i_ddi_check_retire(dev_info_t *dip);
2094845Svikram 
2107656SSherry.Moore@Sun.COM static void quiesce_one_device(dev_info_t *, void *);
2114845Svikram 
2120Sstevel@tonic-gate /*
2130Sstevel@tonic-gate  * dev_info cache and node management
2140Sstevel@tonic-gate  */
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate /* initialize dev_info node cache */
2170Sstevel@tonic-gate void
2180Sstevel@tonic-gate i_ddi_node_cache_init()
2190Sstevel@tonic-gate {
2200Sstevel@tonic-gate 	ASSERT(ddi_node_cache == NULL);
2210Sstevel@tonic-gate 	ddi_node_cache = kmem_cache_create("dev_info_node_cache",
2220Sstevel@tonic-gate 	    sizeof (struct dev_info), 0, NULL, NULL, NULL, NULL, NULL, 0);
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	if (ddidebug & DDI_AUDIT)
2250Sstevel@tonic-gate 		da_log_init();
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate /*
2290Sstevel@tonic-gate  * Allocating a dev_info node, callable from interrupt context with KM_NOSLEEP
2300Sstevel@tonic-gate  * The allocated node has a reference count of 0.
2310Sstevel@tonic-gate  */
2320Sstevel@tonic-gate dev_info_t *
233789Sahrens i_ddi_alloc_node(dev_info_t *pdip, char *node_name, pnode_t nodeid,
2340Sstevel@tonic-gate     int instance, ddi_prop_t *sys_prop, int flag)
2350Sstevel@tonic-gate {
2360Sstevel@tonic-gate 	struct dev_info *devi;
2370Sstevel@tonic-gate 	struct devi_nodeid *elem;
2380Sstevel@tonic-gate 	static char failed[] = "i_ddi_alloc_node: out of memory";
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	ASSERT(node_name != NULL);
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	if ((devi = kmem_cache_alloc(ddi_node_cache, flag)) == NULL) {
2430Sstevel@tonic-gate 		cmn_err(CE_NOTE, failed);
2440Sstevel@tonic-gate 		return (NULL);
2450Sstevel@tonic-gate 	}
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	bzero(devi, sizeof (struct dev_info));
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	if (devinfo_audit_log) {
2500Sstevel@tonic-gate 		devi->devi_audit = kmem_zalloc(sizeof (devinfo_audit_t), flag);
2510Sstevel@tonic-gate 		if (devi->devi_audit == NULL)
2520Sstevel@tonic-gate 			goto fail;
2530Sstevel@tonic-gate 	}
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	if ((devi->devi_node_name = i_ddi_strdup(node_name, flag)) == NULL)
2560Sstevel@tonic-gate 		goto fail;
2574145Scth 
2580Sstevel@tonic-gate 	/* default binding name is node name */
2590Sstevel@tonic-gate 	devi->devi_binding_name = devi->devi_node_name;
2607009Scth 	devi->devi_major = DDI_MAJOR_T_NONE;	/* unbound by default */
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	/*
2630Sstevel@tonic-gate 	 * Make a copy of system property
2640Sstevel@tonic-gate 	 */
2650Sstevel@tonic-gate 	if (sys_prop &&
2660Sstevel@tonic-gate 	    (devi->devi_sys_prop_ptr = i_ddi_prop_list_dup(sys_prop, flag))
2670Sstevel@tonic-gate 	    == NULL)
2680Sstevel@tonic-gate 		goto fail;
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	/*
2710Sstevel@tonic-gate 	 * Assign devi_nodeid, devi_node_class, devi_node_attributes
2720Sstevel@tonic-gate 	 * according to the following algorithm:
2730Sstevel@tonic-gate 	 *
2740Sstevel@tonic-gate 	 * nodeid arg			node class		node attributes
2750Sstevel@tonic-gate 	 *
2760Sstevel@tonic-gate 	 * DEVI_PSEUDO_NODEID		DDI_NC_PSEUDO		A
2770Sstevel@tonic-gate 	 * DEVI_SID_NODEID		DDI_NC_PSEUDO		A,P
2788912SChris.Horne@Sun.COM 	 * DEVI_SID_HIDDEN_NODEID	DDI_NC_PSEUDO		A,P,H
2790Sstevel@tonic-gate 	 * other			DDI_NC_PROM		P
2800Sstevel@tonic-gate 	 *
2810Sstevel@tonic-gate 	 * Where A = DDI_AUTO_ASSIGNED_NODEID (auto-assign a nodeid)
2820Sstevel@tonic-gate 	 * and	 P = DDI_PERSISTENT
2838912SChris.Horne@Sun.COM 	 * and	 H = DDI_HIDDEN_NODE
2840Sstevel@tonic-gate 	 *
2850Sstevel@tonic-gate 	 * auto-assigned nodeids are also auto-freed.
2860Sstevel@tonic-gate 	 */
2878912SChris.Horne@Sun.COM 	devi->devi_node_attributes = 0;
2880Sstevel@tonic-gate 	switch (nodeid) {
2898912SChris.Horne@Sun.COM 	case DEVI_SID_HIDDEN_NODEID:
2908912SChris.Horne@Sun.COM 		devi->devi_node_attributes |= DDI_HIDDEN_NODE;
2918912SChris.Horne@Sun.COM 		/*FALLTHROUGH*/
2920Sstevel@tonic-gate 	case DEVI_SID_NODEID:
2938912SChris.Horne@Sun.COM 		devi->devi_node_attributes |= DDI_PERSISTENT;
2940Sstevel@tonic-gate 		if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL)
2950Sstevel@tonic-gate 			goto fail;
2960Sstevel@tonic-gate 		/*FALLTHROUGH*/
2970Sstevel@tonic-gate 	case DEVI_PSEUDO_NODEID:
2980Sstevel@tonic-gate 		devi->devi_node_attributes |= DDI_AUTO_ASSIGNED_NODEID;
2990Sstevel@tonic-gate 		devi->devi_node_class = DDI_NC_PSEUDO;
3000Sstevel@tonic-gate 		if (impl_ddi_alloc_nodeid(&devi->devi_nodeid)) {
3010Sstevel@tonic-gate 			panic("i_ddi_alloc_node: out of nodeids");
3020Sstevel@tonic-gate 			/*NOTREACHED*/
3030Sstevel@tonic-gate 		}
3040Sstevel@tonic-gate 		break;
3050Sstevel@tonic-gate 	default:
3060Sstevel@tonic-gate 		if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL)
3070Sstevel@tonic-gate 			goto fail;
3080Sstevel@tonic-gate 		/*
3090Sstevel@tonic-gate 		 * the nodetype is 'prom', try to 'take' the nodeid now.
3100Sstevel@tonic-gate 		 * This requires memory allocation, so check for failure.
3110Sstevel@tonic-gate 		 */
3120Sstevel@tonic-gate 		if (impl_ddi_take_nodeid(nodeid, flag) != 0) {
3130Sstevel@tonic-gate 			kmem_free(elem, sizeof (*elem));
3140Sstevel@tonic-gate 			goto fail;
3150Sstevel@tonic-gate 		}
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 		devi->devi_nodeid = nodeid;
3180Sstevel@tonic-gate 		devi->devi_node_class = DDI_NC_PROM;
3190Sstevel@tonic-gate 		devi->devi_node_attributes = DDI_PERSISTENT;
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	}
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node((dev_info_t *)devi)) {
3240Sstevel@tonic-gate 		mutex_enter(&devimap->dno_lock);
3250Sstevel@tonic-gate 		elem->next = devimap->dno_free;
3260Sstevel@tonic-gate 		devimap->dno_free = elem;
3270Sstevel@tonic-gate 		mutex_exit(&devimap->dno_lock);
3280Sstevel@tonic-gate 	}
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	/*
3310Sstevel@tonic-gate 	 * Instance is normally initialized to -1. In a few special
3320Sstevel@tonic-gate 	 * cases, the caller may specify an instance (e.g. CPU nodes).
3330Sstevel@tonic-gate 	 */
3340Sstevel@tonic-gate 	devi->devi_instance = instance;
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	/*
3370Sstevel@tonic-gate 	 * set parent and bus_ctl parent
3380Sstevel@tonic-gate 	 */
3390Sstevel@tonic-gate 	devi->devi_parent = DEVI(pdip);
3400Sstevel@tonic-gate 	devi->devi_bus_ctl = DEVI(pdip);
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
3430Sstevel@tonic-gate 	    "i_ddi_alloc_node: name=%s id=%d\n", node_name, devi->devi_nodeid));
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	cv_init(&(devi->devi_cv), NULL, CV_DEFAULT, NULL);
3460Sstevel@tonic-gate 	mutex_init(&(devi->devi_lock), NULL, MUTEX_DEFAULT, NULL);
3470Sstevel@tonic-gate 	mutex_init(&(devi->devi_pm_lock), NULL, MUTEX_DEFAULT, NULL);
3480Sstevel@tonic-gate 	mutex_init(&(devi->devi_pm_busy_lock), NULL, MUTEX_DEFAULT, NULL);
3490Sstevel@tonic-gate 
3504845Svikram 	RIO_TRACE((CE_NOTE, "i_ddi_alloc_node: Initing contract fields: "
3514845Svikram 	    "dip=%p, name=%s", (void *)devi, node_name));
3524845Svikram 
3534845Svikram 	mutex_init(&(devi->devi_ct_lock), NULL, MUTEX_DEFAULT, NULL);
3544845Svikram 	cv_init(&(devi->devi_ct_cv), NULL, CV_DEFAULT, NULL);
3554845Svikram 	devi->devi_ct_count = -1;	/* counter not in use if -1 */
3564845Svikram 	list_create(&(devi->devi_ct), sizeof (cont_device_t),
3574845Svikram 	    offsetof(cont_device_t, cond_next));
3584845Svikram 
3590Sstevel@tonic-gate 	i_ddi_set_node_state((dev_info_t *)devi, DS_PROTO);
3600Sstevel@tonic-gate 	da_log_enter((dev_info_t *)devi);
3610Sstevel@tonic-gate 	return ((dev_info_t *)devi);
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate fail:
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_node_name)
3670Sstevel@tonic-gate 		kmem_free(devi->devi_node_name, strlen(node_name) + 1);
3680Sstevel@tonic-gate 	if (devi->devi_audit)
3690Sstevel@tonic-gate 		kmem_free(devi->devi_audit, sizeof (devinfo_audit_t));
3700Sstevel@tonic-gate 	kmem_cache_free(ddi_node_cache, devi);
3710Sstevel@tonic-gate 	cmn_err(CE_NOTE, failed);
3720Sstevel@tonic-gate 	return (NULL);
3730Sstevel@tonic-gate }
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate /*
3760Sstevel@tonic-gate  * free a dev_info structure.
3770Sstevel@tonic-gate  * NB. Not callable from interrupt since impl_ddi_free_nodeid may block.
3780Sstevel@tonic-gate  */
3790Sstevel@tonic-gate void
3800Sstevel@tonic-gate i_ddi_free_node(dev_info_t *dip)
3810Sstevel@tonic-gate {
3820Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
3830Sstevel@tonic-gate 	struct devi_nodeid *elem;
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	ASSERT(devi->devi_ref == 0);
3860Sstevel@tonic-gate 	ASSERT(devi->devi_addr == NULL);
3870Sstevel@tonic-gate 	ASSERT(devi->devi_node_state == DS_PROTO);
3880Sstevel@tonic-gate 	ASSERT(devi->devi_child == NULL);
3890Sstevel@tonic-gate 
390439Scth 	/* free devi_addr_buf allocated by ddi_set_name_addr() */
391439Scth 	if (devi->devi_addr_buf)
392439Scth 		kmem_free(devi->devi_addr_buf, 2 * MAXNAMELEN);
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	if (i_ndi_dev_is_auto_assigned_node(dip))
3950Sstevel@tonic-gate 		impl_ddi_free_nodeid(DEVI(dip)->devi_nodeid);
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
3980Sstevel@tonic-gate 		mutex_enter(&devimap->dno_lock);
3990Sstevel@tonic-gate 		ASSERT(devimap->dno_free);
4000Sstevel@tonic-gate 		elem = devimap->dno_free;
4010Sstevel@tonic-gate 		devimap->dno_free = elem->next;
4020Sstevel@tonic-gate 		mutex_exit(&devimap->dno_lock);
4030Sstevel@tonic-gate 		kmem_free(elem, sizeof (*elem));
4040Sstevel@tonic-gate 	}
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	if (DEVI(dip)->devi_compat_names)
4070Sstevel@tonic-gate 		kmem_free(DEVI(dip)->devi_compat_names,
4080Sstevel@tonic-gate 		    DEVI(dip)->devi_compat_length);
4094145Scth 	if (DEVI(dip)->devi_rebinding_name)
4104145Scth 		kmem_free(DEVI(dip)->devi_rebinding_name,
4114145Scth 		    strlen(DEVI(dip)->devi_rebinding_name) + 1);
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	ddi_prop_remove_all(dip);	/* remove driver properties */
4140Sstevel@tonic-gate 	if (devi->devi_sys_prop_ptr)
4150Sstevel@tonic-gate 		i_ddi_prop_list_delete(devi->devi_sys_prop_ptr);
4160Sstevel@tonic-gate 	if (devi->devi_hw_prop_ptr)
4170Sstevel@tonic-gate 		i_ddi_prop_list_delete(devi->devi_hw_prop_ptr);
4180Sstevel@tonic-gate 
4196640Scth 	if (DEVI(dip)->devi_devid_str)
4206640Scth 		ddi_devid_str_free(DEVI(dip)->devi_devid_str);
4216640Scth 
4220Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_INVAL);
4230Sstevel@tonic-gate 	da_log_enter(dip);
4240Sstevel@tonic-gate 	if (devi->devi_audit) {
4250Sstevel@tonic-gate 		kmem_free(devi->devi_audit, sizeof (devinfo_audit_t));
4260Sstevel@tonic-gate 	}
4270Sstevel@tonic-gate 	if (devi->devi_device_class)
4280Sstevel@tonic-gate 		kmem_free(devi->devi_device_class,
4290Sstevel@tonic-gate 		    strlen(devi->devi_device_class) + 1);
4300Sstevel@tonic-gate 	cv_destroy(&(devi->devi_cv));
4310Sstevel@tonic-gate 	mutex_destroy(&(devi->devi_lock));
4320Sstevel@tonic-gate 	mutex_destroy(&(devi->devi_pm_lock));
4330Sstevel@tonic-gate 	mutex_destroy(&(devi->devi_pm_busy_lock));
4340Sstevel@tonic-gate 
4354845Svikram 	RIO_TRACE((CE_NOTE, "i_ddi_free_node: destroying contract fields: "
4364845Svikram 	    "dip=%p", (void *)dip));
4374845Svikram 	contract_device_remove_dip(dip);
4384845Svikram 	ASSERT(devi->devi_ct_count == -1);
4394845Svikram 	ASSERT(list_is_empty(&(devi->devi_ct)));
4404845Svikram 	cv_destroy(&(devi->devi_ct_cv));
4414845Svikram 	list_destroy(&(devi->devi_ct));
4424845Svikram 	/* free this last since contract_device_remove_dip() uses it */
4434845Svikram 	mutex_destroy(&(devi->devi_ct_lock));
4444845Svikram 	RIO_TRACE((CE_NOTE, "i_ddi_free_node: destroyed all contract fields: "
4454845Svikram 	    "dip=%p, name=%s", (void *)dip, devi->devi_node_name));
4464845Svikram 
4474845Svikram 	kmem_free(devi->devi_node_name, strlen(devi->devi_node_name) + 1);
4484845Svikram 
4490Sstevel@tonic-gate 	kmem_cache_free(ddi_node_cache, devi);
4500Sstevel@tonic-gate }
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate /*
4540Sstevel@tonic-gate  * Node state transitions
4550Sstevel@tonic-gate  */
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate /*
4580Sstevel@tonic-gate  * Change the node name
4590Sstevel@tonic-gate  */
4600Sstevel@tonic-gate int
4610Sstevel@tonic-gate ndi_devi_set_nodename(dev_info_t *dip, char *name, int flags)
4620Sstevel@tonic-gate {
4630Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags))
4640Sstevel@tonic-gate 	char *nname, *oname;
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	ASSERT(dip && name);
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	oname = DEVI(dip)->devi_node_name;
4690Sstevel@tonic-gate 	if (strcmp(oname, name) == 0)
4700Sstevel@tonic-gate 		return (DDI_SUCCESS);
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	/*
4730Sstevel@tonic-gate 	 * pcicfg_fix_ethernet requires a name change after node
4740Sstevel@tonic-gate 	 * is linked into the tree. When pcicfg is fixed, we
4750Sstevel@tonic-gate 	 * should only allow name change in DS_PROTO state.
4760Sstevel@tonic-gate 	 */
4770Sstevel@tonic-gate 	if (i_ddi_node_state(dip) >= DS_BOUND) {
4780Sstevel@tonic-gate 		/*
4790Sstevel@tonic-gate 		 * Don't allow name change once node is bound
4800Sstevel@tonic-gate 		 */
4810Sstevel@tonic-gate 		cmn_err(CE_NOTE,
4820Sstevel@tonic-gate 		    "ndi_devi_set_nodename: node already bound dip = %p,"
4830Sstevel@tonic-gate 		    " %s -> %s", (void *)dip, ddi_node_name(dip), name);
4840Sstevel@tonic-gate 		return (NDI_FAILURE);
4850Sstevel@tonic-gate 	}
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	nname = i_ddi_strdup(name, KM_SLEEP);
4880Sstevel@tonic-gate 	DEVI(dip)->devi_node_name = nname;
4890Sstevel@tonic-gate 	i_ddi_set_binding_name(dip, nname);
4900Sstevel@tonic-gate 	kmem_free(oname, strlen(oname) + 1);
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	da_log_enter(dip);
4930Sstevel@tonic-gate 	return (NDI_SUCCESS);
4940Sstevel@tonic-gate }
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate void
4970Sstevel@tonic-gate i_ddi_add_devimap(dev_info_t *dip)
4980Sstevel@tonic-gate {
4990Sstevel@tonic-gate 	struct devi_nodeid *elem;
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	ASSERT(dip);
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	if (!ndi_dev_is_persistent_node(dip))
5040Sstevel@tonic-gate 		return;
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	ASSERT(ddi_get_parent(dip) == NULL || (DEVI_VHCI_NODE(dip)) ||
5070Sstevel@tonic-gate 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 	mutex_enter(&devimap->dno_lock);
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	ASSERT(devimap->dno_free);
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	elem = devimap->dno_free;
5140Sstevel@tonic-gate 	devimap->dno_free = elem->next;
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	elem->nodeid = ddi_get_nodeid(dip);
5170Sstevel@tonic-gate 	elem->dip = dip;
5180Sstevel@tonic-gate 	elem->next = devimap->dno_head;
5190Sstevel@tonic-gate 	devimap->dno_head = elem;
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	devimap->dno_list_length++;
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	mutex_exit(&devimap->dno_lock);
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate static int
5270Sstevel@tonic-gate i_ddi_remove_devimap(dev_info_t *dip)
5280Sstevel@tonic-gate {
5290Sstevel@tonic-gate 	struct devi_nodeid *prev, *elem;
5300Sstevel@tonic-gate 	static const char *fcn = "i_ddi_remove_devimap";
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	ASSERT(dip);
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	if (!ndi_dev_is_persistent_node(dip))
5350Sstevel@tonic-gate 		return (DDI_SUCCESS);
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	mutex_enter(&devimap->dno_lock);
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	/*
5400Sstevel@tonic-gate 	 * The following check is done with dno_lock held
5410Sstevel@tonic-gate 	 * to prevent race between dip removal and
5420Sstevel@tonic-gate 	 * e_ddi_prom_node_to_dip()
5430Sstevel@tonic-gate 	 */
5440Sstevel@tonic-gate 	if (e_ddi_devi_holdcnt(dip)) {
5450Sstevel@tonic-gate 		mutex_exit(&devimap->dno_lock);
5460Sstevel@tonic-gate 		return (DDI_FAILURE);
5470Sstevel@tonic-gate 	}
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	ASSERT(devimap->dno_head);
5500Sstevel@tonic-gate 	ASSERT(devimap->dno_list_length > 0);
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	prev = NULL;
5530Sstevel@tonic-gate 	for (elem = devimap->dno_head; elem; elem = elem->next) {
5540Sstevel@tonic-gate 		if (elem->dip == dip) {
5550Sstevel@tonic-gate 			ASSERT(elem->nodeid == ddi_get_nodeid(dip));
5560Sstevel@tonic-gate 			break;
5570Sstevel@tonic-gate 		}
5580Sstevel@tonic-gate 		prev = elem;
5590Sstevel@tonic-gate 	}
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	if (elem && prev)
5620Sstevel@tonic-gate 		prev->next = elem->next;
5630Sstevel@tonic-gate 	else if (elem)
5640Sstevel@tonic-gate 		devimap->dno_head = elem->next;
5650Sstevel@tonic-gate 	else
5660Sstevel@tonic-gate 		panic("%s: devinfo node(%p) not found",
5670Sstevel@tonic-gate 		    fcn, (void *)dip);
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	devimap->dno_list_length--;
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	elem->nodeid = 0;
5720Sstevel@tonic-gate 	elem->dip = NULL;
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 	elem->next = devimap->dno_free;
5750Sstevel@tonic-gate 	devimap->dno_free = elem;
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	mutex_exit(&devimap->dno_lock);
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	return (DDI_SUCCESS);
5800Sstevel@tonic-gate }
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate /*
5830Sstevel@tonic-gate  * Link this node into the devinfo tree and add to orphan list
5840Sstevel@tonic-gate  * Not callable from interrupt context
5850Sstevel@tonic-gate  */
5860Sstevel@tonic-gate static void
5870Sstevel@tonic-gate link_node(dev_info_t *dip)
5880Sstevel@tonic-gate {
5890Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
5900Sstevel@tonic-gate 	struct dev_info *parent = devi->devi_parent;
5910Sstevel@tonic-gate 	dev_info_t **dipp;
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	ASSERT(parent);	/* never called for root node */
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "link_node: parent = %s child = %s\n",
5960Sstevel@tonic-gate 	    parent->devi_node_name, devi->devi_node_name));
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	/*
5990Sstevel@tonic-gate 	 * Hold the global_vhci_lock before linking any direct
6000Sstevel@tonic-gate 	 * children of rootnex driver. This special lock protects
6010Sstevel@tonic-gate 	 * linking and unlinking for rootnext direct children.
6020Sstevel@tonic-gate 	 */
6030Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
6040Sstevel@tonic-gate 		mutex_enter(&global_vhci_lock);
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 	/*
6070Sstevel@tonic-gate 	 * attach the node to end of the list unless the node is already there
6080Sstevel@tonic-gate 	 */
6090Sstevel@tonic-gate 	dipp = (dev_info_t **)(&DEVI(parent)->devi_child);
6100Sstevel@tonic-gate 	while (*dipp && (*dipp != dip)) {
6110Sstevel@tonic-gate 		dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling);
6120Sstevel@tonic-gate 	}
6130Sstevel@tonic-gate 	ASSERT(*dipp == NULL);	/* node is not linked */
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	/*
6160Sstevel@tonic-gate 	 * Now that we are in the tree, update the devi-nodeid map.
6170Sstevel@tonic-gate 	 */
6180Sstevel@tonic-gate 	i_ddi_add_devimap(dip);
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	/*
6210Sstevel@tonic-gate 	 * This is a temporary workaround for Bug 4618861.
6220Sstevel@tonic-gate 	 * We keep the scsi_vhci nexus node on the left side of the devinfo
6230Sstevel@tonic-gate 	 * tree (under the root nexus driver), so that virtual nodes under
6240Sstevel@tonic-gate 	 * scsi_vhci will be SUSPENDed first and RESUMEd last.  This ensures
6250Sstevel@tonic-gate 	 * that the pHCI nodes are active during times when their clients
6260Sstevel@tonic-gate 	 * may be depending on them.  This workaround embodies the knowledge
6270Sstevel@tonic-gate 	 * that system PM and CPR both traverse the tree left-to-right during
6280Sstevel@tonic-gate 	 * SUSPEND and right-to-left during RESUME.
6291093Shiremath 	 * Extending the workaround to IB Nexus/VHCI
6301093Shiremath 	 * driver also.
6310Sstevel@tonic-gate 	 */
6324145Scth 	if (strcmp(devi->devi_binding_name, "scsi_vhci") == 0) {
6330Sstevel@tonic-gate 		/* Add scsi_vhci to beginning of list */
6340Sstevel@tonic-gate 		ASSERT((dev_info_t *)parent == top_devinfo);
6350Sstevel@tonic-gate 		/* scsi_vhci under rootnex */
6360Sstevel@tonic-gate 		devi->devi_sibling = parent->devi_child;
6370Sstevel@tonic-gate 		parent->devi_child = devi;
6384145Scth 	} else if (strcmp(devi->devi_binding_name, "ib") == 0) {
6391093Shiremath 		i_link_vhci_node(dip);
6400Sstevel@tonic-gate 	} else {
6410Sstevel@tonic-gate 		/* Add to end of list */
6420Sstevel@tonic-gate 		*dipp = dip;
6430Sstevel@tonic-gate 		DEVI(dip)->devi_sibling = NULL;
6440Sstevel@tonic-gate 	}
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	/*
6470Sstevel@tonic-gate 	 * Release the global_vhci_lock before linking any direct
6480Sstevel@tonic-gate 	 * children of rootnex driver.
6490Sstevel@tonic-gate 	 */
6500Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
6510Sstevel@tonic-gate 		mutex_exit(&global_vhci_lock);
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	/* persistent nodes go on orphan list */
6540Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip))
6550Sstevel@tonic-gate 		add_to_dn_list(&orphanlist, dip);
6560Sstevel@tonic-gate }
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate /*
6590Sstevel@tonic-gate  * Unlink this node from the devinfo tree
6600Sstevel@tonic-gate  */
6610Sstevel@tonic-gate static int
6620Sstevel@tonic-gate unlink_node(dev_info_t *dip)
6630Sstevel@tonic-gate {
6640Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
6650Sstevel@tonic-gate 	struct dev_info *parent = devi->devi_parent;
6660Sstevel@tonic-gate 	dev_info_t **dipp;
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	ASSERT(parent != NULL);
6690Sstevel@tonic-gate 	ASSERT(devi->devi_node_state == DS_LINKED);
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "unlink_node: name = %s\n",
6720Sstevel@tonic-gate 	    ddi_node_name(dip)));
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	/* check references */
6750Sstevel@tonic-gate 	if (devi->devi_ref || i_ddi_remove_devimap(dip) != DDI_SUCCESS)
6760Sstevel@tonic-gate 		return (DDI_FAILURE);
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	/*
6790Sstevel@tonic-gate 	 * Hold the global_vhci_lock before linking any direct
6800Sstevel@tonic-gate 	 * children of rootnex driver.
6810Sstevel@tonic-gate 	 */
6820Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
6830Sstevel@tonic-gate 		mutex_enter(&global_vhci_lock);
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 	dipp = (dev_info_t **)(&DEVI(parent)->devi_child);
6860Sstevel@tonic-gate 	while (*dipp && (*dipp != dip)) {
6870Sstevel@tonic-gate 		dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling);
6880Sstevel@tonic-gate 	}
6890Sstevel@tonic-gate 	if (*dipp) {
6900Sstevel@tonic-gate 		*dipp = (dev_info_t *)(devi->devi_sibling);
6910Sstevel@tonic-gate 		devi->devi_sibling = NULL;
6920Sstevel@tonic-gate 	} else {
6930Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE, "unlink_node: %s not linked",
6940Sstevel@tonic-gate 		    devi->devi_node_name));
6950Sstevel@tonic-gate 	}
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 	/*
6980Sstevel@tonic-gate 	 * Release the global_vhci_lock before linking any direct
6990Sstevel@tonic-gate 	 * children of rootnex driver.
7000Sstevel@tonic-gate 	 */
7010Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
7020Sstevel@tonic-gate 		mutex_exit(&global_vhci_lock);
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 	/* Remove node from orphan list */
7050Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
7060Sstevel@tonic-gate 		remove_from_dn_list(&orphanlist, dip);
7070Sstevel@tonic-gate 	}
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 	return (DDI_SUCCESS);
7100Sstevel@tonic-gate }
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate /*
7130Sstevel@tonic-gate  * Bind this devinfo node to a driver. If compat is NON-NULL, try that first.
7140Sstevel@tonic-gate  * Else, use the node-name.
7150Sstevel@tonic-gate  *
7160Sstevel@tonic-gate  * NOTE: IEEE1275 specifies that nodename should be tried before compatible.
7170Sstevel@tonic-gate  *	Solaris implementation binds nodename after compatible.
7180Sstevel@tonic-gate  *
7190Sstevel@tonic-gate  * If we find a binding,
7208912SChris.Horne@Sun.COM  * - set the binding name to the string,
7210Sstevel@tonic-gate  * - set major number to driver major
7220Sstevel@tonic-gate  *
7230Sstevel@tonic-gate  * If we don't find a binding,
7240Sstevel@tonic-gate  * - return failure
7250Sstevel@tonic-gate  */
7260Sstevel@tonic-gate static int
7270Sstevel@tonic-gate bind_node(dev_info_t *dip)
7280Sstevel@tonic-gate {
7290Sstevel@tonic-gate 	char *p = NULL;
7307009Scth 	major_t major = DDI_MAJOR_T_NONE;
7310Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
7320Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 	ASSERT(devi->devi_node_state == DS_LINKED);
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "bind_node: 0x%p(name = %s)\n",
7370Sstevel@tonic-gate 	    (void *)dip, ddi_node_name(dip)));
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
7400Sstevel@tonic-gate 	if (DEVI(dip)->devi_flags & DEVI_NO_BIND) {
7410Sstevel@tonic-gate 		mutex_exit(&DEVI(dip)->devi_lock);
7420Sstevel@tonic-gate 		return (DDI_FAILURE);
7430Sstevel@tonic-gate 	}
7440Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 	/* find the driver with most specific binding using compatible */
7470Sstevel@tonic-gate 	major = ddi_compatible_driver_major(dip, &p);
7487009Scth 	if (major == DDI_MAJOR_T_NONE)
7490Sstevel@tonic-gate 		return (DDI_FAILURE);
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	devi->devi_major = major;
7520Sstevel@tonic-gate 	if (p != NULL) {
7530Sstevel@tonic-gate 		i_ddi_set_binding_name(dip, p);
7540Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "bind_node: %s bound to %s\n",
7550Sstevel@tonic-gate 		    devi->devi_node_name, p));
7560Sstevel@tonic-gate 	}
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 	/* Link node to per-driver list */
7590Sstevel@tonic-gate 	link_to_driver_list(dip);
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	/*
7620Sstevel@tonic-gate 	 * reset parent flag so that nexus will merge .conf props
7630Sstevel@tonic-gate 	 */
7640Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
7650Sstevel@tonic-gate 		mutex_enter(&DEVI(parent)->devi_lock);
7660Sstevel@tonic-gate 		DEVI(parent)->devi_flags &=
7670Sstevel@tonic-gate 		    ~(DEVI_ATTACHED_CHILDREN|DEVI_MADE_CHILDREN);
7680Sstevel@tonic-gate 		mutex_exit(&DEVI(parent)->devi_lock);
7690Sstevel@tonic-gate 	}
7700Sstevel@tonic-gate 	return (DDI_SUCCESS);
7710Sstevel@tonic-gate }
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate /*
7740Sstevel@tonic-gate  * Unbind this devinfo node
7750Sstevel@tonic-gate  * Called before the node is destroyed or driver is removed from system
7760Sstevel@tonic-gate  */
7770Sstevel@tonic-gate static int
7780Sstevel@tonic-gate unbind_node(dev_info_t *dip)
7790Sstevel@tonic-gate {
7800Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_node_state == DS_BOUND);
7817009Scth 	ASSERT(DEVI(dip)->devi_major != DDI_MAJOR_T_NONE);
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	/* check references */
7840Sstevel@tonic-gate 	if (DEVI(dip)->devi_ref)
7850Sstevel@tonic-gate 		return (DDI_FAILURE);
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "unbind_node: 0x%p(name = %s)\n",
7880Sstevel@tonic-gate 	    (void *)dip, ddi_node_name(dip)));
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	unlink_from_driver_list(dip);
7914145Scth 
7927009Scth 	DEVI(dip)->devi_major = DDI_MAJOR_T_NONE;
7934145Scth 	DEVI(dip)->devi_binding_name = DEVI(dip)->devi_node_name;
7940Sstevel@tonic-gate 	return (DDI_SUCCESS);
7950Sstevel@tonic-gate }
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate /*
7980Sstevel@tonic-gate  * Initialize a node: calls the parent nexus' bus_ctl ops to do the operation.
7990Sstevel@tonic-gate  * Must hold parent and per-driver list while calling this function.
8000Sstevel@tonic-gate  * A successful init_node() returns with an active ndi_hold_devi() hold on
8010Sstevel@tonic-gate  * the parent.
8020Sstevel@tonic-gate  */
8030Sstevel@tonic-gate static int
8040Sstevel@tonic-gate init_node(dev_info_t *dip)
8050Sstevel@tonic-gate {
8060Sstevel@tonic-gate 	int error;
8070Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
8080Sstevel@tonic-gate 	int (*f)(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *, void *);
8090Sstevel@tonic-gate 	char *path;
8104145Scth 	major_t	major;
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_BOUND);
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 	/* should be DS_READY except for pcmcia ... */
8150Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(pdip) >= DS_PROBED);
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8180Sstevel@tonic-gate 	(void) ddi_pathname(dip, path);
8190Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "init_node: entry: path %s 0x%p\n",
8200Sstevel@tonic-gate 	    path, (void *)dip));
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	/*
8230Sstevel@tonic-gate 	 * The parent must have a bus_ctl operation.
8240Sstevel@tonic-gate 	 */
8250Sstevel@tonic-gate 	if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
8260Sstevel@tonic-gate 	    (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_ctl) == NULL) {
8270Sstevel@tonic-gate 		error = DDI_FAILURE;
8280Sstevel@tonic-gate 		goto out;
8290Sstevel@tonic-gate 	}
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	add_global_props(dip);
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 	/*
8340Sstevel@tonic-gate 	 * Invoke the parent's bus_ctl operation with the DDI_CTLOPS_INITCHILD
8350Sstevel@tonic-gate 	 * command to transform the child to canonical form 1. If there
8360Sstevel@tonic-gate 	 * is an error, ddi_remove_child should be called, to clean up.
8370Sstevel@tonic-gate 	 */
8380Sstevel@tonic-gate 	error = (*f)(pdip, pdip, DDI_CTLOPS_INITCHILD, dip, NULL);
8390Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
8400Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "init_node: %s 0x%p failed\n",
8410Sstevel@tonic-gate 		    path, (void *)dip));
8420Sstevel@tonic-gate 		remove_global_props(dip);
8430Sstevel@tonic-gate 		/* in case nexus driver didn't clear this field */
8440Sstevel@tonic-gate 		ddi_set_name_addr(dip, NULL);
8450Sstevel@tonic-gate 		error = DDI_FAILURE;
8460Sstevel@tonic-gate 		goto out;
8470Sstevel@tonic-gate 	}
8480Sstevel@tonic-gate 
8494950Scth 	ndi_hold_devi(pdip);			/* initial hold of parent */
8500Sstevel@tonic-gate 
8514145Scth 	/* recompute path after initchild for @addr information */
8524145Scth 	(void) ddi_pathname(dip, path);
8534145Scth 
8544145Scth 	/* Check for duplicate nodes */
8550Sstevel@tonic-gate 	if (find_duplicate_child(pdip, dip) != NULL) {
8560Sstevel@tonic-gate 		/*
8570Sstevel@tonic-gate 		 * uninit_node() the duplicate - a successful uninit_node()
8584950Scth 		 * will release inital hold of parent using ndi_rele_devi().
8590Sstevel@tonic-gate 		 */
8600Sstevel@tonic-gate 		if ((error = uninit_node(dip)) != DDI_SUCCESS) {
8614950Scth 			ndi_rele_devi(pdip);	/* release initial hold */
8620Sstevel@tonic-gate 			cmn_err(CE_WARN, "init_node: uninit of duplicate "
8630Sstevel@tonic-gate 			    "node %s failed", path);
8640Sstevel@tonic-gate 		}
8650Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "init_node: duplicate uninit "
8660Sstevel@tonic-gate 		    "%s 0x%p%s\n", path, (void *)dip,
8670Sstevel@tonic-gate 		    (error == DDI_SUCCESS) ? "" : " failed"));
8680Sstevel@tonic-gate 		error = DDI_FAILURE;
8690Sstevel@tonic-gate 		goto out;
8700Sstevel@tonic-gate 	}
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 	/*
8734145Scth 	 * Check to see if we have a path-oriented driver alias that overrides
8744145Scth 	 * the current driver binding. If so, we need to rebind. This check
8754145Scth 	 * needs to be delayed until after a successful DDI_CTLOPS_INITCHILD,
8764145Scth 	 * so the unit-address is established on the last component of the path.
8774145Scth 	 *
8784145Scth 	 * NOTE: Allowing a path-oriented alias to change the driver binding
8794145Scth 	 * of a driver.conf node results in non-intuitive property behavior.
8804145Scth 	 * We provide a tunable (driver_conf_allow_path_alias) to control
8814145Scth 	 * this behavior. See uninit_node() for more details.
8824145Scth 	 *
8834145Scth 	 * NOTE: If you are adding a path-oriented alias for the boot device,
8844145Scth 	 * and there is mismatch between OBP and the kernel in regard to
8854145Scth 	 * generic name use, like "disk" .vs. "ssd", then you will need
8864145Scth 	 * to add a path-oriented alias for both paths.
8874145Scth 	 */
8884145Scth 	major = ddi_name_to_major(path);
8897009Scth 	if ((major != DDI_MAJOR_T_NONE) &&
8904145Scth 	    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED) &&
8914145Scth 	    (major != DEVI(dip)->devi_major) &&
8924145Scth 	    (ndi_dev_is_persistent_node(dip) || driver_conf_allow_path_alias)) {
8934145Scth 
8944145Scth 		/* Mark node for rebind processing. */
8954145Scth 		mutex_enter(&DEVI(dip)->devi_lock);
8964145Scth 		DEVI(dip)->devi_flags |= DEVI_REBIND;
8974145Scth 		mutex_exit(&DEVI(dip)->devi_lock);
8984145Scth 
8994145Scth 		/*
9004950Scth 		 * Add an extra hold on the parent to prevent it from ever
9014950Scth 		 * having a zero devi_ref during the child rebind process.
9024950Scth 		 * This is necessary to ensure that the parent will never
9034950Scth 		 * detach(9E) during the rebind.
9044950Scth 		 */
9054950Scth 		ndi_hold_devi(pdip);		/* extra hold of parent */
9064950Scth 
9074950Scth 		/*
9084145Scth 		 * uninit_node() current binding - a successful uninit_node()
9094950Scth 		 * will release extra hold of parent using ndi_rele_devi().
9104145Scth 		 */
9114145Scth 		if ((error = uninit_node(dip)) != DDI_SUCCESS) {
9124950Scth 			ndi_rele_devi(pdip);	/* release extra hold */
9134950Scth 			ndi_rele_devi(pdip);	/* release initial hold */
9144145Scth 			cmn_err(CE_WARN, "init_node: uninit for rebind "
9154145Scth 			    "of node %s failed", path);
9164145Scth 			goto out;
9174145Scth 		}
9184145Scth 
9194145Scth 		/* Unbind: demote the node back to DS_LINKED.  */
9204145Scth 		if ((error = ndi_devi_unbind_driver(dip)) != DDI_SUCCESS) {
9218912SChris.Horne@Sun.COM 			ndi_rele_devi(pdip);	/* release initial hold */
9224145Scth 			cmn_err(CE_WARN, "init_node: unbind for rebind "
9234145Scth 			    "of node %s failed", path);
9244145Scth 			goto out;
9254145Scth 		}
9264145Scth 
9274145Scth 		/* establish rebinding name */
9284145Scth 		if (DEVI(dip)->devi_rebinding_name == NULL)
9294145Scth 			DEVI(dip)->devi_rebinding_name =
9304145Scth 			    i_ddi_strdup(path, KM_SLEEP);
9314145Scth 
9324145Scth 		/*
9334145Scth 		 * Now that we are demoted and marked for rebind, repromote.
9344145Scth 		 * We need to do this in steps, instead of just calling
9354145Scth 		 * ddi_initchild, so that we can redo the merge operation
9364145Scth 		 * after we are rebound to the path-bound driver.
9374145Scth 		 *
9384145Scth 		 * Start by rebinding node to the path-bound driver.
9394145Scth 		 */
9404145Scth 		if ((error = ndi_devi_bind_driver(dip, 0)) != DDI_SUCCESS) {
9418912SChris.Horne@Sun.COM 			ndi_rele_devi(pdip);	/* release initial hold */
9424145Scth 			cmn_err(CE_WARN, "init_node: rebind "
9434145Scth 			    "of node %s failed", path);
9444145Scth 			goto out;
9454145Scth 		}
9464145Scth 
9474145Scth 		/*
9484145Scth 		 * If the node is not a driver.conf node then merge
9494145Scth 		 * driver.conf properties from new path-bound driver.conf.
9504145Scth 		 */
9514145Scth 		if (ndi_dev_is_persistent_node(dip))
9524145Scth 			(void) i_ndi_make_spec_children(pdip, 0);
9534145Scth 
9544145Scth 		/*
9554145Scth 		 * Now that we have taken care of merge, repromote back
9564145Scth 		 * to DS_INITIALIZED.
9574145Scth 		 */
9584145Scth 		error = ddi_initchild(pdip, dip);
9594145Scth 		NDI_CONFIG_DEBUG((CE_CONT, "init_node: rebind "
9604145Scth 		    "%s 0x%p\n", path, (void *)dip));
9614950Scth 
9624950Scth 		/*
9634950Scth 		 * Release our initial hold. If ddi_initchild() was
9647224Scth 		 * successful then it will return with the active hold.
9654950Scth 		 */
9664950Scth 		ndi_rele_devi(pdip);
9674145Scth 		goto out;
9684145Scth 	}
9694145Scth 
9704145Scth 	/*
9710Sstevel@tonic-gate 	 * Apply multi-parent/deep-nexus optimization to the new node
9720Sstevel@tonic-gate 	 */
9730Sstevel@tonic-gate 	DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
9740Sstevel@tonic-gate 	ddi_optimize_dtree(dip);
9754950Scth 	error = DDI_SUCCESS;		/* return with active hold */
9760Sstevel@tonic-gate 
9774145Scth out:	if (error != DDI_SUCCESS) {
9784145Scth 		/* On failure ensure that DEVI_REBIND is cleared */
9794145Scth 		mutex_enter(&DEVI(dip)->devi_lock);
9804145Scth 		DEVI(dip)->devi_flags &= ~DEVI_REBIND;
9814145Scth 		mutex_exit(&DEVI(dip)->devi_lock);
9824145Scth 	}
9834145Scth 	kmem_free(path, MAXPATHLEN);
9840Sstevel@tonic-gate 	return (error);
9850Sstevel@tonic-gate }
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate /*
9880Sstevel@tonic-gate  * Uninitialize node
9890Sstevel@tonic-gate  * The per-driver list must be held busy during the call.
9900Sstevel@tonic-gate  * A successful uninit_node() releases the init_node() hold on
9910Sstevel@tonic-gate  * the parent by calling ndi_rele_devi().
9920Sstevel@tonic-gate  */
9930Sstevel@tonic-gate static int
9940Sstevel@tonic-gate uninit_node(dev_info_t *dip)
9950Sstevel@tonic-gate {
9960Sstevel@tonic-gate 	int node_state_entry;
9970Sstevel@tonic-gate 	dev_info_t *pdip;
9980Sstevel@tonic-gate 	struct dev_ops *ops;
9990Sstevel@tonic-gate 	int (*f)();
10000Sstevel@tonic-gate 	int error;
10010Sstevel@tonic-gate 	char *addr;
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 	/*
10040Sstevel@tonic-gate 	 * Don't check for references here or else a ref-counted
10050Sstevel@tonic-gate 	 * dip cannot be downgraded by the framework.
10060Sstevel@tonic-gate 	 */
10070Sstevel@tonic-gate 	node_state_entry = i_ddi_node_state(dip);
10080Sstevel@tonic-gate 	ASSERT((node_state_entry == DS_BOUND) ||
10094950Scth 	    (node_state_entry == DS_INITIALIZED));
10100Sstevel@tonic-gate 	pdip = ddi_get_parent(dip);
10110Sstevel@tonic-gate 	ASSERT(pdip);
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "uninit_node: 0x%p(%s%d)\n",
10140Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 	if (((ops = ddi_get_driver(pdip)) == NULL) ||
10170Sstevel@tonic-gate 	    (ops->devo_bus_ops == NULL) ||
10180Sstevel@tonic-gate 	    ((f = ops->devo_bus_ops->bus_ctl) == NULL)) {
10190Sstevel@tonic-gate 		return (DDI_FAILURE);
10200Sstevel@tonic-gate 	}
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	/*
10230Sstevel@tonic-gate 	 * save the @addr prior to DDI_CTLOPS_UNINITCHILD for use in
10240Sstevel@tonic-gate 	 * freeing the instance if it succeeds.
10250Sstevel@tonic-gate 	 */
10260Sstevel@tonic-gate 	if (node_state_entry == DS_INITIALIZED) {
10270Sstevel@tonic-gate 		addr = ddi_get_name_addr(dip);
10280Sstevel@tonic-gate 		if (addr)
10290Sstevel@tonic-gate 			addr = i_ddi_strdup(addr, KM_SLEEP);
10300Sstevel@tonic-gate 	} else {
10310Sstevel@tonic-gate 		addr = NULL;
10320Sstevel@tonic-gate 	}
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 	error = (*f)(pdip, pdip, DDI_CTLOPS_UNINITCHILD, dip, (void *)NULL);
10350Sstevel@tonic-gate 	if (error == DDI_SUCCESS) {
1036*9066SPrasad.Singamsetty@Sun.COM 		/* ensure that devids are unregistered */
1037*9066SPrasad.Singamsetty@Sun.COM 		if (DEVI(dip)->devi_flags & DEVI_REGISTERED_DEVID) {
1038*9066SPrasad.Singamsetty@Sun.COM 			DEVI(dip)->devi_flags &= ~DEVI_REGISTERED_DEVID;
1039*9066SPrasad.Singamsetty@Sun.COM 			ddi_devid_unregister(dip);
1040*9066SPrasad.Singamsetty@Sun.COM 		}
1041*9066SPrasad.Singamsetty@Sun.COM 
10420Sstevel@tonic-gate 		/* if uninitchild forgot to set devi_addr to NULL do it now */
10430Sstevel@tonic-gate 		ddi_set_name_addr(dip, NULL);
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate 		/*
10460Sstevel@tonic-gate 		 * Free instance number. This is a no-op if instance has
10470Sstevel@tonic-gate 		 * been kept by probe_node().  Avoid free when we are called
10480Sstevel@tonic-gate 		 * from init_node (DS_BOUND) because the instance has not yet
10490Sstevel@tonic-gate 		 * been assigned.
10500Sstevel@tonic-gate 		 */
10510Sstevel@tonic-gate 		if (node_state_entry == DS_INITIALIZED) {
10520Sstevel@tonic-gate 			e_ddi_free_instance(dip, addr);
10530Sstevel@tonic-gate 			DEVI(dip)->devi_instance = -1;
10540Sstevel@tonic-gate 		}
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 		/* release the init_node hold */
10570Sstevel@tonic-gate 		ndi_rele_devi(pdip);
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 		remove_global_props(dip);
10604145Scth 
10614145Scth 		/*
10624145Scth 		 * NOTE: The decision on whether to allow a path-oriented
10634145Scth 		 * rebind of a driver.conf enumerated node is made by
10644145Scth 		 * init_node() based on driver_conf_allow_path_alias. The
10654145Scth 		 * rebind code below prevents deletion of system properties
10664145Scth 		 * on driver.conf nodes.
10674145Scth 		 *
10684145Scth 		 * When driver_conf_allow_path_alias is set, property behavior
10694145Scth 		 * on rebound driver.conf file is non-intuitive. For a
10704145Scth 		 * driver.conf node, the unit-address properties come from
10714145Scth 		 * the driver.conf file as system properties. Removing system
10724145Scth 		 * properties from a driver.conf node makes the node
10734145Scth 		 * useless (we get node without unit-address properties) - so
10744145Scth 		 * we leave system properties in place. The result is a node
10754145Scth 		 * where system properties come from the node being rebound,
10764145Scth 		 * and global properties come from the driver.conf file
10774145Scth 		 * of the driver we are rebinding to.  If we could determine
10784145Scth 		 * that the path-oriented alias driver.conf file defined a
10794145Scth 		 * node at the same unit address, it would be best to use
10804145Scth 		 * that node and avoid the non-intuitive property behavior.
10814145Scth 		 * Unfortunately, the current "merge" code does not support
10824145Scth 		 * this, so we live with the non-intuitive property behavior.
10834145Scth 		 */
10844145Scth 		if (!((ndi_dev_is_persistent_node(dip) == 0) &&
10854145Scth 		    (DEVI(dip)->devi_flags & DEVI_REBIND)))
10864145Scth 			e_ddi_prop_remove_all(dip);
10870Sstevel@tonic-gate 	} else {
10880Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "uninit_node failed: 0x%p(%s%d)\n",
10890Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
10900Sstevel@tonic-gate 	}
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate 	if (addr)
10930Sstevel@tonic-gate 		kmem_free(addr, strlen(addr) + 1);
10940Sstevel@tonic-gate 	return (error);
10950Sstevel@tonic-gate }
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate /*
10980Sstevel@tonic-gate  * Invoke driver's probe entry point to probe for existence of hardware.
10990Sstevel@tonic-gate  * Keep instance permanent for successful probe and leaf nodes.
11000Sstevel@tonic-gate  *
11010Sstevel@tonic-gate  * Per-driver list must be held busy while calling this function.
11020Sstevel@tonic-gate  */
11030Sstevel@tonic-gate static int
11040Sstevel@tonic-gate probe_node(dev_info_t *dip)
11050Sstevel@tonic-gate {
11060Sstevel@tonic-gate 	int rv;
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_INITIALIZED);
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "probe_node: 0x%p(%s%d)\n",
11110Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 	/* temporarily hold the driver while we probe */
11140Sstevel@tonic-gate 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
11150Sstevel@tonic-gate 	if (DEVI(dip)->devi_ops == NULL) {
11160Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
11170Sstevel@tonic-gate 		    "probe_node: 0x%p(%s%d) cannot load driver\n",
11180Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
11190Sstevel@tonic-gate 		return (DDI_FAILURE);
11200Sstevel@tonic-gate 	}
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 	if (identify_9e != 0)
11230Sstevel@tonic-gate 		(void) devi_identify(dip);
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate 	rv = devi_probe(dip);
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	/* release the driver now that probe is complete */
11280Sstevel@tonic-gate 	ndi_rele_driver(dip);
11290Sstevel@tonic-gate 	DEVI(dip)->devi_ops = NULL;
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 	switch (rv) {
11320Sstevel@tonic-gate 	case DDI_PROBE_SUCCESS:			/* found */
11330Sstevel@tonic-gate 	case DDI_PROBE_DONTCARE:		/* ddi_dev_is_sid */
11340Sstevel@tonic-gate 		e_ddi_keep_instance(dip);	/* persist instance */
11350Sstevel@tonic-gate 		rv = DDI_SUCCESS;
11360Sstevel@tonic-gate 		break;
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate 	case DDI_PROBE_PARTIAL:			/* maybe later */
11390Sstevel@tonic-gate 	case DDI_PROBE_FAILURE:			/* not found */
11400Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
11410Sstevel@tonic-gate 		    "probe_node: 0x%p(%s%d) no hardware found%s\n",
11420Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip),
11430Sstevel@tonic-gate 		    (rv == DDI_PROBE_PARTIAL) ? " yet" : ""));
11440Sstevel@tonic-gate 		rv = DDI_FAILURE;
11450Sstevel@tonic-gate 		break;
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate 	default:
11480Sstevel@tonic-gate #ifdef	DEBUG
11490Sstevel@tonic-gate 		cmn_err(CE_WARN, "probe_node: %s%d: illegal probe(9E) value",
11500Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
11510Sstevel@tonic-gate #endif	/* DEBUG */
11520Sstevel@tonic-gate 		rv = DDI_FAILURE;
11530Sstevel@tonic-gate 		break;
11540Sstevel@tonic-gate 	}
11550Sstevel@tonic-gate 	return (rv);
11560Sstevel@tonic-gate }
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate /*
11590Sstevel@tonic-gate  * Unprobe a node. Simply reset the node state.
11600Sstevel@tonic-gate  * Per-driver list must be held busy while calling this function.
11610Sstevel@tonic-gate  */
11620Sstevel@tonic-gate static int
11630Sstevel@tonic-gate unprobe_node(dev_info_t *dip)
11640Sstevel@tonic-gate {
11650Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_PROBED);
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate 	/*
11680Sstevel@tonic-gate 	 * Don't check for references here or else a ref-counted
11690Sstevel@tonic-gate 	 * dip cannot be downgraded by the framework.
11700Sstevel@tonic-gate 	 */
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "unprobe_node: 0x%p(name = %s)\n",
11730Sstevel@tonic-gate 	    (void *)dip, ddi_node_name(dip)));
11740Sstevel@tonic-gate 	return (DDI_SUCCESS);
11750Sstevel@tonic-gate }
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate /*
11780Sstevel@tonic-gate  * Attach devinfo node.
11790Sstevel@tonic-gate  * Per-driver list must be held busy.
11800Sstevel@tonic-gate  */
11810Sstevel@tonic-gate static int
11820Sstevel@tonic-gate attach_node(dev_info_t *dip)
11830Sstevel@tonic-gate {
11840Sstevel@tonic-gate 	int rv;
11850Sstevel@tonic-gate 
11862155Scth 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
11870Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_PROBED);
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d)\n",
11900Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 	/*
11930Sstevel@tonic-gate 	 * Tell mpxio framework that a node is about to online.
11940Sstevel@tonic-gate 	 */
11950Sstevel@tonic-gate 	if ((rv = mdi_devi_online(dip, 0)) != NDI_SUCCESS) {
11960Sstevel@tonic-gate 		return (DDI_FAILURE);
11970Sstevel@tonic-gate 	}
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 	/* no recursive attachment */
12000Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_ops == NULL);
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 	/*
12030Sstevel@tonic-gate 	 * Hold driver the node is bound to.
12040Sstevel@tonic-gate 	 */
12050Sstevel@tonic-gate 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
12060Sstevel@tonic-gate 	if (DEVI(dip)->devi_ops == NULL) {
12070Sstevel@tonic-gate 		/*
12080Sstevel@tonic-gate 		 * We were able to load driver for probing, so we should
12090Sstevel@tonic-gate 		 * not get here unless something really bad happened.
12100Sstevel@tonic-gate 		 */
12110Sstevel@tonic-gate 		cmn_err(CE_WARN, "attach_node: no driver for major %d",
12120Sstevel@tonic-gate 		    DEVI(dip)->devi_major);
12130Sstevel@tonic-gate 		return (DDI_FAILURE);
12140Sstevel@tonic-gate 	}
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 	if (NEXUS_DRV(DEVI(dip)->devi_ops))
12170Sstevel@tonic-gate 		DEVI(dip)->devi_taskq = ddi_taskq_create(dip,
12180Sstevel@tonic-gate 		    "nexus_enum_tq", 1,
12190Sstevel@tonic-gate 		    TASKQ_DEFAULTPRI, 0);
12200Sstevel@tonic-gate 
1221495Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
12222009Sdm120769 	DEVI_SET_ATTACHING(dip);
12230Sstevel@tonic-gate 	DEVI_SET_NEED_RESET(dip);
1224495Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
1225495Scth 
12260Sstevel@tonic-gate 	rv = devi_attach(dip, DDI_ATTACH);
1227495Scth 
12282009Sdm120769 	mutex_enter(&(DEVI(dip)->devi_lock));
12292009Sdm120769 	DEVI_CLR_ATTACHING(dip);
12302009Sdm120769 
12311961Scth 	if (rv != DDI_SUCCESS) {
12322155Scth 		DEVI_CLR_NEED_RESET(dip);
1233*9066SPrasad.Singamsetty@Sun.COM 		mutex_exit(&DEVI(dip)->devi_lock);
1234438Scth 
12350Sstevel@tonic-gate 		/*
12360Sstevel@tonic-gate 		 * Cleanup dacf reservations
12370Sstevel@tonic-gate 		 */
12380Sstevel@tonic-gate 		mutex_enter(&dacf_lock);
12390Sstevel@tonic-gate 		dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH);
12400Sstevel@tonic-gate 		dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH);
12410Sstevel@tonic-gate 		mutex_exit(&dacf_lock);
12420Sstevel@tonic-gate 		if (DEVI(dip)->devi_taskq)
12430Sstevel@tonic-gate 			ddi_taskq_destroy(DEVI(dip)->devi_taskq);
12440Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 		/* release the driver if attach failed */
12470Sstevel@tonic-gate 		ndi_rele_driver(dip);
12480Sstevel@tonic-gate 		DEVI(dip)->devi_ops = NULL;
12490Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d) failed\n",
12500Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
12510Sstevel@tonic-gate 		return (DDI_FAILURE);
12522009Sdm120769 	} else
12532009Sdm120769 		mutex_exit(&DEVI(dip)->devi_lock);
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate 	/* successful attach, return with driver held */
1256495Scth 
12570Sstevel@tonic-gate 	return (DDI_SUCCESS);
12580Sstevel@tonic-gate }
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate /*
12610Sstevel@tonic-gate  * Detach devinfo node.
12620Sstevel@tonic-gate  * Per-driver list must be held busy.
12630Sstevel@tonic-gate  */
12640Sstevel@tonic-gate static int
12650Sstevel@tonic-gate detach_node(dev_info_t *dip, uint_t flag)
12660Sstevel@tonic-gate {
126753Scth 	struct devnames	*dnp;
126853Scth 	int		rv;
126953Scth 
12702155Scth 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
12710Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_ATTACHED);
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 	/* check references */
12740Sstevel@tonic-gate 	if (DEVI(dip)->devi_ref)
12750Sstevel@tonic-gate 		return (DDI_FAILURE);
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "detach_node: 0x%p(%s%d)\n",
12780Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
12790Sstevel@tonic-gate 
12802155Scth 	/*
12812155Scth 	 * NOTE: If we are processing a pHCI node then the calling code
12822155Scth 	 * must detect this and ndi_devi_enter() in (vHCI, parent(pHCI))
12832155Scth 	 * order unless pHCI and vHCI are siblings.  Code paths leading
12842155Scth 	 * here that must ensure this ordering include:
12852155Scth 	 * unconfig_immediate_children(), devi_unconfig_one(),
12862155Scth 	 * ndi_devi_unconfig_one(), ndi_devi_offline().
12872155Scth 	 */
12882155Scth 	ASSERT(!MDI_PHCI(dip) ||
12892155Scth 	    (ddi_get_parent(mdi_devi_get_vdip(dip)) == ddi_get_parent(dip)) ||
12902155Scth 	    DEVI_BUSY_OWNED(mdi_devi_get_vdip(dip)));
12912155Scth 
12920Sstevel@tonic-gate 	/* Offline the device node with the mpxio framework. */
12930Sstevel@tonic-gate 	if (mdi_devi_offline(dip, flag) != NDI_SUCCESS) {
12940Sstevel@tonic-gate 		return (DDI_FAILURE);
12950Sstevel@tonic-gate 	}
12960Sstevel@tonic-gate 
12970Sstevel@tonic-gate 	/* drain the taskq */
12980Sstevel@tonic-gate 	if (DEVI(dip)->devi_taskq)
12990Sstevel@tonic-gate 		ddi_taskq_wait(DEVI(dip)->devi_taskq);
13000Sstevel@tonic-gate 
13010Sstevel@tonic-gate 	rv = devi_detach(dip, DDI_DETACH);
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate 	if (rv != DDI_SUCCESS) {
13040Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
13050Sstevel@tonic-gate 		    "detach_node: 0x%p(%s%d) failed\n",
13060Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
13070Sstevel@tonic-gate 		return (DDI_FAILURE);
13080Sstevel@tonic-gate 	}
13090Sstevel@tonic-gate 
13102155Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
13112155Scth 	DEVI_CLR_NEED_RESET(dip);
13122155Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
13132155Scth 
13148249SVikram.Hegde@Sun.COM #if defined(__i386) || defined(__amd64)
13158249SVikram.Hegde@Sun.COM #if !defined(__xpv)
13168249SVikram.Hegde@Sun.COM 	/*
13178249SVikram.Hegde@Sun.COM 	 * Close any iommulib mediated linkage to an IOMMU
13188249SVikram.Hegde@Sun.COM 	 */
13198249SVikram.Hegde@Sun.COM 	iommulib_nex_close(dip);
13208249SVikram.Hegde@Sun.COM #endif
13218249SVikram.Hegde@Sun.COM #endif
13228249SVikram.Hegde@Sun.COM 
13230Sstevel@tonic-gate 	/* destroy the taskq */
13240Sstevel@tonic-gate 	if (DEVI(dip)->devi_taskq) {
13250Sstevel@tonic-gate 		ddi_taskq_destroy(DEVI(dip)->devi_taskq);
13260Sstevel@tonic-gate 		DEVI(dip)->devi_taskq = NULL;
13270Sstevel@tonic-gate 	}
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate 	/* Cleanup dacf reservations */
13300Sstevel@tonic-gate 	mutex_enter(&dacf_lock);
13310Sstevel@tonic-gate 	dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH);
13320Sstevel@tonic-gate 	dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH);
13330Sstevel@tonic-gate 	mutex_exit(&dacf_lock);
13340Sstevel@tonic-gate 
13358860SMatthew.Jacob@Sun.COM 	/* remove any additional flavors that were added */
13368860SMatthew.Jacob@Sun.COM 	if (DEVI(dip)->devi_flavorv_n > 1 && DEVI(dip)->devi_flavorv != NULL) {
13378860SMatthew.Jacob@Sun.COM 		kmem_free(DEVI(dip)->devi_flavorv,
13388860SMatthew.Jacob@Sun.COM 		    (DEVI(dip)->devi_flavorv_n - 1) * sizeof (void *));
13398860SMatthew.Jacob@Sun.COM 		DEVI(dip)->devi_flavorv = NULL;
13408860SMatthew.Jacob@Sun.COM 	}
13418860SMatthew.Jacob@Sun.COM 
13420Sstevel@tonic-gate 	/* Remove properties and minor nodes in case driver forgots */
13430Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
13440Sstevel@tonic-gate 	ddi_prop_remove_all(dip);
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate 	/* a detached node can't have attached or .conf children */
13470Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
13480Sstevel@tonic-gate 	DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN|DEVI_ATTACHED_CHILDREN);
1349*9066SPrasad.Singamsetty@Sun.COM 	mutex_exit(&DEVI(dip)->devi_lock);
13500Sstevel@tonic-gate 
135153Scth 	/*
135253Scth 	 * If the instance has successfully detached in detach_driver() context,
135353Scth 	 * clear DN_DRIVER_HELD for correct ddi_hold_installed_driver()
135453Scth 	 * behavior. Consumers like qassociate() depend on this (via clnopen()).
135553Scth 	 */
135653Scth 	if (flag & NDI_DETACH_DRIVER) {
135753Scth 		dnp = &(devnamesp[DEVI(dip)->devi_major]);
135853Scth 		LOCK_DEV_OPS(&dnp->dn_lock);
135953Scth 		dnp->dn_flags &= ~DN_DRIVER_HELD;
136053Scth 		UNLOCK_DEV_OPS(&dnp->dn_lock);
136153Scth 	}
136253Scth 
13630Sstevel@tonic-gate 	/* successful detach, release the driver */
13640Sstevel@tonic-gate 	ndi_rele_driver(dip);
13650Sstevel@tonic-gate 	DEVI(dip)->devi_ops = NULL;
13660Sstevel@tonic-gate 	return (DDI_SUCCESS);
13670Sstevel@tonic-gate }
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate /*
13700Sstevel@tonic-gate  * Run dacf post_attach routines
13710Sstevel@tonic-gate  */
13720Sstevel@tonic-gate static int
13730Sstevel@tonic-gate postattach_node(dev_info_t *dip)
13740Sstevel@tonic-gate {
13750Sstevel@tonic-gate 	int rval;
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 	/*
13780Sstevel@tonic-gate 	 * For hotplug busses like USB, it's possible that devices
13790Sstevel@tonic-gate 	 * are removed but dip is still around. We don't want to
13800Sstevel@tonic-gate 	 * run dacf routines as part of detach failure recovery.
13810Sstevel@tonic-gate 	 *
13820Sstevel@tonic-gate 	 * Pretend success until we figure out how to prevent
13830Sstevel@tonic-gate 	 * access to such devinfo nodes.
13840Sstevel@tonic-gate 	 */
13850Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_REMOVED(dip))
13860Sstevel@tonic-gate 		return (DDI_SUCCESS);
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate 	/*
13890Sstevel@tonic-gate 	 * if dacf_postattach failed, report it to the framework
13900Sstevel@tonic-gate 	 * so that it can be retried later at the open time.
13910Sstevel@tonic-gate 	 */
13920Sstevel@tonic-gate 	mutex_enter(&dacf_lock);
13930Sstevel@tonic-gate 	rval = dacfc_postattach(dip);
13940Sstevel@tonic-gate 	mutex_exit(&dacf_lock);
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate 	/*
13970Sstevel@tonic-gate 	 * Plumbing during postattach may fail because of the
13980Sstevel@tonic-gate 	 * underlying device is not ready. This will fail ndi_devi_config()
13990Sstevel@tonic-gate 	 * in dv_filldir() and a warning message is issued. The message
14000Sstevel@tonic-gate 	 * from here will explain what happened
14010Sstevel@tonic-gate 	 */
14020Sstevel@tonic-gate 	if (rval != DACF_SUCCESS) {
14030Sstevel@tonic-gate 		cmn_err(CE_WARN, "Postattach failed for %s%d\n",
14040Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
14050Sstevel@tonic-gate 		return (DDI_FAILURE);
14060Sstevel@tonic-gate 	}
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate 	return (DDI_SUCCESS);
14090Sstevel@tonic-gate }
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate /*
14120Sstevel@tonic-gate  * Run dacf pre-detach routines
14130Sstevel@tonic-gate  */
14140Sstevel@tonic-gate static int
14150Sstevel@tonic-gate predetach_node(dev_info_t *dip, uint_t flag)
14160Sstevel@tonic-gate {
14170Sstevel@tonic-gate 	int ret;
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 	/*
14200Sstevel@tonic-gate 	 * Don't auto-detach if DDI_FORCEATTACH or DDI_NO_AUTODETACH
14210Sstevel@tonic-gate 	 * properties are set.
14220Sstevel@tonic-gate 	 */
14230Sstevel@tonic-gate 	if (flag & NDI_AUTODETACH) {
14240Sstevel@tonic-gate 		struct devnames *dnp;
14250Sstevel@tonic-gate 		int pflag = DDI_PROP_NOTPROM | DDI_PROP_DONTPASS;
14260Sstevel@tonic-gate 
14270Sstevel@tonic-gate 		if ((ddi_prop_get_int(DDI_DEV_T_ANY, dip,
14284950Scth 		    pflag, DDI_FORCEATTACH, 0) == 1) ||
14290Sstevel@tonic-gate 		    (ddi_prop_get_int(DDI_DEV_T_ANY, dip,
14304950Scth 		    pflag, DDI_NO_AUTODETACH, 0) == 1))
14310Sstevel@tonic-gate 			return (DDI_FAILURE);
14320Sstevel@tonic-gate 
14330Sstevel@tonic-gate 		/* check for driver global version of DDI_NO_AUTODETACH */
14340Sstevel@tonic-gate 		dnp = &devnamesp[DEVI(dip)->devi_major];
14350Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
14360Sstevel@tonic-gate 		if (dnp->dn_flags & DN_NO_AUTODETACH) {
14370Sstevel@tonic-gate 			UNLOCK_DEV_OPS(&dnp->dn_lock);
14380Sstevel@tonic-gate 			return (DDI_FAILURE);
14390Sstevel@tonic-gate 		}
14400Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
14410Sstevel@tonic-gate 	}
14420Sstevel@tonic-gate 
14430Sstevel@tonic-gate 	mutex_enter(&dacf_lock);
14440Sstevel@tonic-gate 	ret = dacfc_predetach(dip);
14450Sstevel@tonic-gate 	mutex_exit(&dacf_lock);
14460Sstevel@tonic-gate 
14470Sstevel@tonic-gate 	return (ret);
14480Sstevel@tonic-gate }
14490Sstevel@tonic-gate 
14500Sstevel@tonic-gate /*
14510Sstevel@tonic-gate  * Wrapper for making multiple state transitions
14520Sstevel@tonic-gate  */
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate /*
14550Sstevel@tonic-gate  * i_ndi_config_node: upgrade dev_info node into a specified state.
14560Sstevel@tonic-gate  * It is a bit tricky because the locking protocol changes before and
14570Sstevel@tonic-gate  * after a node is bound to a driver. All locks are held external to
14580Sstevel@tonic-gate  * this function.
14590Sstevel@tonic-gate  */
14600Sstevel@tonic-gate int
14610Sstevel@tonic-gate i_ndi_config_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag)
14620Sstevel@tonic-gate {
14630Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flag))
14640Sstevel@tonic-gate 	int rv = DDI_SUCCESS;
14650Sstevel@tonic-gate 
14660Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate 	while ((i_ddi_node_state(dip) < state) && (rv == DDI_SUCCESS)) {
14690Sstevel@tonic-gate 
14700Sstevel@tonic-gate 		/* don't allow any more changes to the device tree */
14710Sstevel@tonic-gate 		if (devinfo_freeze) {
14720Sstevel@tonic-gate 			rv = DDI_FAILURE;
14730Sstevel@tonic-gate 			break;
14740Sstevel@tonic-gate 		}
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate 		switch (i_ddi_node_state(dip)) {
14770Sstevel@tonic-gate 		case DS_PROTO:
14780Sstevel@tonic-gate 			/*
14790Sstevel@tonic-gate 			 * only caller can reference this node, no external
14800Sstevel@tonic-gate 			 * locking needed.
14810Sstevel@tonic-gate 			 */
14820Sstevel@tonic-gate 			link_node(dip);
14830Sstevel@tonic-gate 			i_ddi_set_node_state(dip, DS_LINKED);
14840Sstevel@tonic-gate 			break;
14850Sstevel@tonic-gate 		case DS_LINKED:
14860Sstevel@tonic-gate 			/*
14870Sstevel@tonic-gate 			 * Three code path may attempt to bind a node:
14880Sstevel@tonic-gate 			 * - boot code
14890Sstevel@tonic-gate 			 * - add_drv
14900Sstevel@tonic-gate 			 * - hotplug thread
14910Sstevel@tonic-gate 			 * Boot code is single threaded, add_drv synchronize
14920Sstevel@tonic-gate 			 * on a userland lock, and hotplug synchronize on
14930Sstevel@tonic-gate 			 * hotplug_lk. There could be a race between add_drv
14940Sstevel@tonic-gate 			 * and hotplug thread. We'll live with this until the
14950Sstevel@tonic-gate 			 * conversion to top-down loading.
14960Sstevel@tonic-gate 			 */
14970Sstevel@tonic-gate 			if ((rv = bind_node(dip)) == DDI_SUCCESS)
14980Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_BOUND);
14994145Scth 
15000Sstevel@tonic-gate 			break;
15010Sstevel@tonic-gate 		case DS_BOUND:
15020Sstevel@tonic-gate 			/*
15030Sstevel@tonic-gate 			 * The following transitions synchronizes on the
15040Sstevel@tonic-gate 			 * per-driver busy changing flag, since we already
15050Sstevel@tonic-gate 			 * have a driver.
15060Sstevel@tonic-gate 			 */
15070Sstevel@tonic-gate 			if ((rv = init_node(dip)) == DDI_SUCCESS)
15080Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_INITIALIZED);
15090Sstevel@tonic-gate 			break;
15100Sstevel@tonic-gate 		case DS_INITIALIZED:
15110Sstevel@tonic-gate 			if ((rv = probe_node(dip)) == DDI_SUCCESS)
15120Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_PROBED);
15130Sstevel@tonic-gate 			break;
15140Sstevel@tonic-gate 		case DS_PROBED:
15154845Svikram 			i_ddi_check_retire(dip);
15160Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, 1);
15170Sstevel@tonic-gate 			if ((rv = attach_node(dip)) == DDI_SUCCESS)
15180Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_ATTACHED);
15190Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, -1);
15200Sstevel@tonic-gate 			break;
15210Sstevel@tonic-gate 		case DS_ATTACHED:
15220Sstevel@tonic-gate 			if ((rv = postattach_node(dip)) == DDI_SUCCESS)
15230Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_READY);
15240Sstevel@tonic-gate 			break;
15250Sstevel@tonic-gate 		case DS_READY:
15260Sstevel@tonic-gate 			break;
15270Sstevel@tonic-gate 		default:
15280Sstevel@tonic-gate 			/* should never reach here */
15290Sstevel@tonic-gate 			ASSERT("unknown devinfo state");
15300Sstevel@tonic-gate 		}
15310Sstevel@tonic-gate 	}
15320Sstevel@tonic-gate 
15330Sstevel@tonic-gate 	if (ddidebug & DDI_AUDIT)
15340Sstevel@tonic-gate 		da_log_enter(dip);
15350Sstevel@tonic-gate 	return (rv);
15360Sstevel@tonic-gate }
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate /*
15390Sstevel@tonic-gate  * i_ndi_unconfig_node: downgrade dev_info node into a specified state.
15400Sstevel@tonic-gate  */
15410Sstevel@tonic-gate int
15420Sstevel@tonic-gate i_ndi_unconfig_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag)
15430Sstevel@tonic-gate {
15444950Scth 	int	rv = DDI_SUCCESS;
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
15470Sstevel@tonic-gate 
15480Sstevel@tonic-gate 	while ((i_ddi_node_state(dip) > state) && (rv == DDI_SUCCESS)) {
15490Sstevel@tonic-gate 
15500Sstevel@tonic-gate 		/* don't allow any more changes to the device tree */
15510Sstevel@tonic-gate 		if (devinfo_freeze) {
15520Sstevel@tonic-gate 			rv = DDI_FAILURE;
15530Sstevel@tonic-gate 			break;
15540Sstevel@tonic-gate 		}
15550Sstevel@tonic-gate 
15560Sstevel@tonic-gate 		switch (i_ddi_node_state(dip)) {
15570Sstevel@tonic-gate 		case DS_PROTO:
15580Sstevel@tonic-gate 			break;
15590Sstevel@tonic-gate 		case DS_LINKED:
15600Sstevel@tonic-gate 			/*
15610Sstevel@tonic-gate 			 * Persistent nodes are only removed by hotplug code
15620Sstevel@tonic-gate 			 * .conf nodes synchronizes on per-driver list.
15630Sstevel@tonic-gate 			 */
15640Sstevel@tonic-gate 			if ((rv = unlink_node(dip)) == DDI_SUCCESS)
15650Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_PROTO);
15660Sstevel@tonic-gate 			break;
15670Sstevel@tonic-gate 		case DS_BOUND:
15680Sstevel@tonic-gate 			/*
15690Sstevel@tonic-gate 			 * The following transitions synchronizes on the
15700Sstevel@tonic-gate 			 * per-driver busy changing flag, since we already
15710Sstevel@tonic-gate 			 * have a driver.
15720Sstevel@tonic-gate 			 */
15730Sstevel@tonic-gate 			if ((rv = unbind_node(dip)) == DDI_SUCCESS)
15740Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_LINKED);
15750Sstevel@tonic-gate 			break;
15760Sstevel@tonic-gate 		case DS_INITIALIZED:
15770Sstevel@tonic-gate 			if ((rv = uninit_node(dip)) == DDI_SUCCESS)
15780Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_BOUND);
15790Sstevel@tonic-gate 			break;
15800Sstevel@tonic-gate 		case DS_PROBED:
15810Sstevel@tonic-gate 			if ((rv = unprobe_node(dip)) == DDI_SUCCESS)
15820Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_INITIALIZED);
15830Sstevel@tonic-gate 			break;
15840Sstevel@tonic-gate 		case DS_ATTACHED:
15850Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, 1);
1586495Scth 
1587495Scth 			mutex_enter(&(DEVI(dip)->devi_lock));
15880Sstevel@tonic-gate 			DEVI_SET_DETACHING(dip);
1589495Scth 			mutex_exit(&(DEVI(dip)->devi_lock));
1590495Scth 
15910Sstevel@tonic-gate 			membar_enter();	/* ensure visibility for hold_devi */
15920Sstevel@tonic-gate 
15930Sstevel@tonic-gate 			if ((rv = detach_node(dip, flag)) == DDI_SUCCESS)
15940Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_PROBED);
1595495Scth 
1596495Scth 			mutex_enter(&(DEVI(dip)->devi_lock));
15970Sstevel@tonic-gate 			DEVI_CLR_DETACHING(dip);
1598495Scth 			mutex_exit(&(DEVI(dip)->devi_lock));
1599495Scth 
16000Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, -1);
16010Sstevel@tonic-gate 			break;
16020Sstevel@tonic-gate 		case DS_READY:
16030Sstevel@tonic-gate 			if ((rv = predetach_node(dip, flag)) == DDI_SUCCESS)
16040Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_ATTACHED);
16050Sstevel@tonic-gate 			break;
16060Sstevel@tonic-gate 		default:
16070Sstevel@tonic-gate 			ASSERT("unknown devinfo state");
16080Sstevel@tonic-gate 		}
16090Sstevel@tonic-gate 	}
16100Sstevel@tonic-gate 	da_log_enter(dip);
16110Sstevel@tonic-gate 	return (rv);
16120Sstevel@tonic-gate }
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate /*
16150Sstevel@tonic-gate  * ddi_initchild: transform node to DS_INITIALIZED state
16160Sstevel@tonic-gate  */
16170Sstevel@tonic-gate int
16180Sstevel@tonic-gate ddi_initchild(dev_info_t *parent, dev_info_t *proto)
16190Sstevel@tonic-gate {
16200Sstevel@tonic-gate 	int ret, circ;
16210Sstevel@tonic-gate 
16220Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
16230Sstevel@tonic-gate 	ret = i_ndi_config_node(proto, DS_INITIALIZED, 0);
16240Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
16250Sstevel@tonic-gate 
16260Sstevel@tonic-gate 	return (ret);
16270Sstevel@tonic-gate }
16280Sstevel@tonic-gate 
16290Sstevel@tonic-gate /*
16300Sstevel@tonic-gate  * ddi_uninitchild: transform node down to DS_BOUND state
16310Sstevel@tonic-gate  */
16320Sstevel@tonic-gate int
16330Sstevel@tonic-gate ddi_uninitchild(dev_info_t *dip)
16340Sstevel@tonic-gate {
16350Sstevel@tonic-gate 	int ret, circ;
16360Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
16370Sstevel@tonic-gate 	ASSERT(parent);
16380Sstevel@tonic-gate 
16390Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
16400Sstevel@tonic-gate 	ret = i_ndi_unconfig_node(dip, DS_BOUND, 0);
16410Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
16420Sstevel@tonic-gate 
16430Sstevel@tonic-gate 	return (ret);
16440Sstevel@tonic-gate }
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate /*
16471333Scth  * i_ddi_attachchild: transform node to DS_READY/i_ddi_devi_attached() state
16480Sstevel@tonic-gate  */
16490Sstevel@tonic-gate static int
16500Sstevel@tonic-gate i_ddi_attachchild(dev_info_t *dip)
16510Sstevel@tonic-gate {
16522155Scth 	dev_info_t	*parent = ddi_get_parent(dip);
16532155Scth 	int		ret;
16542155Scth 
16552155Scth 	ASSERT(parent && DEVI_BUSY_OWNED(parent));
16560Sstevel@tonic-gate 
16570Sstevel@tonic-gate 	if ((i_ddi_node_state(dip) < DS_BOUND) || DEVI_IS_DEVICE_OFFLINE(dip))
16580Sstevel@tonic-gate 		return (DDI_FAILURE);
16590Sstevel@tonic-gate 
16600Sstevel@tonic-gate 	ret = i_ndi_config_node(dip, DS_READY, 0);
16610Sstevel@tonic-gate 	if (ret == NDI_SUCCESS) {
16620Sstevel@tonic-gate 		ret = DDI_SUCCESS;
16630Sstevel@tonic-gate 	} else {
16640Sstevel@tonic-gate 		/*
16650Sstevel@tonic-gate 		 * Take it down to DS_INITIALIZED so pm_pre_probe is run
16660Sstevel@tonic-gate 		 * on the next attach
16670Sstevel@tonic-gate 		 */
16680Sstevel@tonic-gate 		(void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0);
16690Sstevel@tonic-gate 		ret = DDI_FAILURE;
16700Sstevel@tonic-gate 	}
16710Sstevel@tonic-gate 
16720Sstevel@tonic-gate 	return (ret);
16730Sstevel@tonic-gate }
16740Sstevel@tonic-gate 
16750Sstevel@tonic-gate /*
16760Sstevel@tonic-gate  * i_ddi_detachchild: transform node down to DS_PROBED state
16770Sstevel@tonic-gate  *	If it fails, put it back to DS_READY state.
16780Sstevel@tonic-gate  * NOTE: A node that fails detach may be at DS_ATTACHED instead
16791333Scth  * of DS_READY for a small amount of time - this is the source of
16801333Scth  * transient DS_READY->DS_ATTACHED->DS_READY state changes.
16810Sstevel@tonic-gate  */
16820Sstevel@tonic-gate static int
16830Sstevel@tonic-gate i_ddi_detachchild(dev_info_t *dip, uint_t flags)
16840Sstevel@tonic-gate {
16852155Scth 	dev_info_t	*parent = ddi_get_parent(dip);
16862155Scth 	int		ret;
16872155Scth 
16882155Scth 	ASSERT(parent && DEVI_BUSY_OWNED(parent));
16892155Scth 
16900Sstevel@tonic-gate 	ret = i_ndi_unconfig_node(dip, DS_PROBED, flags);
16910Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
16920Sstevel@tonic-gate 		(void) i_ndi_config_node(dip, DS_READY, 0);
16930Sstevel@tonic-gate 	else
16940Sstevel@tonic-gate 		/* allow pm_pre_probe to reestablish pm state */
16950Sstevel@tonic-gate 		(void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0);
16960Sstevel@tonic-gate 	return (ret);
16970Sstevel@tonic-gate }
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate /*
17000Sstevel@tonic-gate  * Add a child and bind to driver
17010Sstevel@tonic-gate  */
17020Sstevel@tonic-gate dev_info_t *
17030Sstevel@tonic-gate ddi_add_child(dev_info_t *pdip, char *name, uint_t nodeid, uint_t unit)
17040Sstevel@tonic-gate {
17050Sstevel@tonic-gate 	int circ;
17060Sstevel@tonic-gate 	dev_info_t *dip;
17070Sstevel@tonic-gate 
17080Sstevel@tonic-gate 	/* allocate a new node */
17090Sstevel@tonic-gate 	dip = i_ddi_alloc_node(pdip, name, nodeid, (int)unit, NULL, KM_SLEEP);
17100Sstevel@tonic-gate 
17110Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
17120Sstevel@tonic-gate 	(void) i_ndi_config_node(dip, DS_BOUND, 0);
17130Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
17140Sstevel@tonic-gate 	return (dip);
17150Sstevel@tonic-gate }
17160Sstevel@tonic-gate 
17170Sstevel@tonic-gate /*
17180Sstevel@tonic-gate  * ddi_remove_child: remove the dip. The parent must be attached and held
17190Sstevel@tonic-gate  */
17200Sstevel@tonic-gate int
17210Sstevel@tonic-gate ddi_remove_child(dev_info_t *dip, int dummy)
17220Sstevel@tonic-gate {
17230Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dummy))
17240Sstevel@tonic-gate 	int circ, ret;
17250Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
17260Sstevel@tonic-gate 	ASSERT(parent);
17270Sstevel@tonic-gate 
17280Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
17290Sstevel@tonic-gate 
17300Sstevel@tonic-gate 	/*
17310Sstevel@tonic-gate 	 * If we still have children, for example SID nodes marked
17320Sstevel@tonic-gate 	 * as persistent but not attached, attempt to remove them.
17330Sstevel@tonic-gate 	 */
17340Sstevel@tonic-gate 	if (DEVI(dip)->devi_child) {
17350Sstevel@tonic-gate 		ret = ndi_devi_unconfig(dip, NDI_DEVI_REMOVE);
17360Sstevel@tonic-gate 		if (ret != NDI_SUCCESS) {
17370Sstevel@tonic-gate 			ndi_devi_exit(parent, circ);
17380Sstevel@tonic-gate 			return (DDI_FAILURE);
17390Sstevel@tonic-gate 		}
17400Sstevel@tonic-gate 		ASSERT(DEVI(dip)->devi_child == NULL);
17410Sstevel@tonic-gate 	}
17420Sstevel@tonic-gate 
17430Sstevel@tonic-gate 	ret = i_ndi_unconfig_node(dip, DS_PROTO, 0);
17440Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
17450Sstevel@tonic-gate 
17460Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
17470Sstevel@tonic-gate 		return (ret);
17480Sstevel@tonic-gate 
17490Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_PROTO);
17500Sstevel@tonic-gate 	i_ddi_free_node(dip);
17510Sstevel@tonic-gate 	return (DDI_SUCCESS);
17520Sstevel@tonic-gate }
17530Sstevel@tonic-gate 
17540Sstevel@tonic-gate /*
17550Sstevel@tonic-gate  * NDI wrappers for ref counting, node allocation, and transitions
17560Sstevel@tonic-gate  */
17570Sstevel@tonic-gate 
17580Sstevel@tonic-gate /*
17590Sstevel@tonic-gate  * Hold/release the devinfo node itself.
17600Sstevel@tonic-gate  * Caller is assumed to prevent the devi from detaching during this call
17610Sstevel@tonic-gate  */
17620Sstevel@tonic-gate void
17630Sstevel@tonic-gate ndi_hold_devi(dev_info_t *dip)
17640Sstevel@tonic-gate {
17650Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
17660Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_ref >= 0);
17670Sstevel@tonic-gate 	DEVI(dip)->devi_ref++;
17680Sstevel@tonic-gate 	membar_enter();			/* make sure stores are flushed */
17690Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
17700Sstevel@tonic-gate }
17710Sstevel@tonic-gate 
17720Sstevel@tonic-gate void
17730Sstevel@tonic-gate ndi_rele_devi(dev_info_t *dip)
17740Sstevel@tonic-gate {
17750Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_ref > 0);
17760Sstevel@tonic-gate 
17770Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
17780Sstevel@tonic-gate 	DEVI(dip)->devi_ref--;
17790Sstevel@tonic-gate 	membar_enter();			/* make sure stores are flushed */
17800Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
17810Sstevel@tonic-gate }
17820Sstevel@tonic-gate 
17830Sstevel@tonic-gate int
17840Sstevel@tonic-gate e_ddi_devi_holdcnt(dev_info_t *dip)
17850Sstevel@tonic-gate {
17860Sstevel@tonic-gate 	return (DEVI(dip)->devi_ref);
17870Sstevel@tonic-gate }
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate /*
17900Sstevel@tonic-gate  * Hold/release the driver the devinfo node is bound to.
17910Sstevel@tonic-gate  */
17920Sstevel@tonic-gate struct dev_ops *
17930Sstevel@tonic-gate ndi_hold_driver(dev_info_t *dip)
17940Sstevel@tonic-gate {
17950Sstevel@tonic-gate 	if (i_ddi_node_state(dip) < DS_BOUND)
17960Sstevel@tonic-gate 		return (NULL);
17970Sstevel@tonic-gate 
17980Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_major != -1);
17990Sstevel@tonic-gate 	return (mod_hold_dev_by_major(DEVI(dip)->devi_major));
18000Sstevel@tonic-gate }
18010Sstevel@tonic-gate 
18020Sstevel@tonic-gate void
18030Sstevel@tonic-gate ndi_rele_driver(dev_info_t *dip)
18040Sstevel@tonic-gate {
18050Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) >= DS_BOUND);
18060Sstevel@tonic-gate 	mod_rele_dev_by_major(DEVI(dip)->devi_major);
18070Sstevel@tonic-gate }
18080Sstevel@tonic-gate 
18090Sstevel@tonic-gate /*
18107224Scth  * Single thread entry into devinfo node for modifying its children (devinfo,
18117224Scth  * pathinfo, and minor). To verify in ASSERTS use DEVI_BUSY_OWNED macro.
18120Sstevel@tonic-gate  */
18130Sstevel@tonic-gate void
18140Sstevel@tonic-gate ndi_devi_enter(dev_info_t *dip, int *circular)
18150Sstevel@tonic-gate {
18160Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
18170Sstevel@tonic-gate 	ASSERT(dip != NULL);
18180Sstevel@tonic-gate 
18192155Scth 	/* for vHCI, enforce (vHCI, pHCI) ndi_deve_enter() order */
18202155Scth 	ASSERT(!MDI_VHCI(dip) || (mdi_devi_pdip_entered(dip) == 0) ||
18212155Scth 	    DEVI_BUSY_OWNED(dip));
18222155Scth 
18230Sstevel@tonic-gate 	mutex_enter(&devi->devi_lock);
18240Sstevel@tonic-gate 	if (devi->devi_busy_thread == curthread) {
18250Sstevel@tonic-gate 		devi->devi_circular++;
18260Sstevel@tonic-gate 	} else {
18270Sstevel@tonic-gate 		while (DEVI_BUSY_CHANGING(devi) && !panicstr)
18280Sstevel@tonic-gate 			cv_wait(&(devi->devi_cv), &(devi->devi_lock));
18290Sstevel@tonic-gate 		if (panicstr) {
18300Sstevel@tonic-gate 			mutex_exit(&devi->devi_lock);
18310Sstevel@tonic-gate 			return;
18320Sstevel@tonic-gate 		}
18330Sstevel@tonic-gate 		devi->devi_flags |= DEVI_BUSY;
18340Sstevel@tonic-gate 		devi->devi_busy_thread = curthread;
18350Sstevel@tonic-gate 	}
18360Sstevel@tonic-gate 	*circular = devi->devi_circular;
18370Sstevel@tonic-gate 	mutex_exit(&devi->devi_lock);
18380Sstevel@tonic-gate }
18390Sstevel@tonic-gate 
18400Sstevel@tonic-gate /*
18410Sstevel@tonic-gate  * Release ndi_devi_enter or successful ndi_devi_tryenter.
18420Sstevel@tonic-gate  */
18430Sstevel@tonic-gate void
18440Sstevel@tonic-gate ndi_devi_exit(dev_info_t *dip, int circular)
18450Sstevel@tonic-gate {
18462155Scth 	struct dev_info	*devi = DEVI(dip);
18472155Scth 	struct dev_info	*vdevi;
18480Sstevel@tonic-gate 	ASSERT(dip != NULL);
18490Sstevel@tonic-gate 
18500Sstevel@tonic-gate 	if (panicstr)
18510Sstevel@tonic-gate 		return;
18520Sstevel@tonic-gate 
18530Sstevel@tonic-gate 	mutex_enter(&(devi->devi_lock));
18540Sstevel@tonic-gate 	if (circular != 0) {
18550Sstevel@tonic-gate 		devi->devi_circular--;
18560Sstevel@tonic-gate 	} else {
18570Sstevel@tonic-gate 		devi->devi_flags &= ~DEVI_BUSY;
18580Sstevel@tonic-gate 		ASSERT(devi->devi_busy_thread == curthread);
18590Sstevel@tonic-gate 		devi->devi_busy_thread = NULL;
18600Sstevel@tonic-gate 		cv_broadcast(&(devi->devi_cv));
18610Sstevel@tonic-gate 	}
18620Sstevel@tonic-gate 	mutex_exit(&(devi->devi_lock));
18632155Scth 
18642155Scth 	/*
18652155Scth 	 * For pHCI exit we issue a broadcast to vHCI for ndi_devi_config_one()
18662155Scth 	 * doing cv_wait on vHCI.
18672155Scth 	 */
18682155Scth 	if (MDI_PHCI(dip)) {
18692155Scth 		vdevi = DEVI(mdi_devi_get_vdip(dip));
18702155Scth 		if (vdevi) {
18712155Scth 			mutex_enter(&(vdevi->devi_lock));
18722155Scth 			if (vdevi->devi_flags & DEVI_PHCI_SIGNALS_VHCI) {
18732155Scth 				vdevi->devi_flags &= ~DEVI_PHCI_SIGNALS_VHCI;
18742155Scth 				cv_broadcast(&(vdevi->devi_cv));
18752155Scth 			}
18762155Scth 			mutex_exit(&(vdevi->devi_lock));
18772155Scth 		}
18782155Scth 	}
18792155Scth }
18802155Scth 
18812155Scth /*
18822155Scth  * Release ndi_devi_enter and wait for possibility of new children, avoiding
18832155Scth  * possibility of missing broadcast before getting to cv_timedwait().
18842155Scth  */
18852155Scth static void
18862155Scth ndi_devi_exit_and_wait(dev_info_t *dip, int circular, clock_t end_time)
18872155Scth {
18882155Scth 	struct dev_info	*devi = DEVI(dip);
18892155Scth 	ASSERT(dip != NULL);
18902155Scth 
18912155Scth 	if (panicstr)
18922155Scth 		return;
18932155Scth 
18942155Scth 	/*
18952155Scth 	 * We are called to wait for of a new child, and new child can
18962155Scth 	 * only be added if circular is zero.
18972155Scth 	 */
18982155Scth 	ASSERT(circular == 0);
18992155Scth 
19002155Scth 	/* like ndi_devi_exit with circular of zero */
19012155Scth 	mutex_enter(&(devi->devi_lock));
19022155Scth 	devi->devi_flags &= ~DEVI_BUSY;
19032155Scth 	ASSERT(devi->devi_busy_thread == curthread);
19042155Scth 	devi->devi_busy_thread = NULL;
19052155Scth 	cv_broadcast(&(devi->devi_cv));
19062155Scth 
19072155Scth 	/* now wait for new children while still holding devi_lock */
19082155Scth 	(void) cv_timedwait(&devi->devi_cv, &(devi->devi_lock), end_time);
19092155Scth 	mutex_exit(&(devi->devi_lock));
19100Sstevel@tonic-gate }
19110Sstevel@tonic-gate 
19120Sstevel@tonic-gate /*
19130Sstevel@tonic-gate  * Attempt to single thread entry into devinfo node for modifying its children.
19140Sstevel@tonic-gate  */
19150Sstevel@tonic-gate int
19160Sstevel@tonic-gate ndi_devi_tryenter(dev_info_t *dip, int *circular)
19170Sstevel@tonic-gate {
19180Sstevel@tonic-gate 	int rval = 1;		   /* assume we enter */
19190Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
19200Sstevel@tonic-gate 	ASSERT(dip != NULL);
19210Sstevel@tonic-gate 
19220Sstevel@tonic-gate 	mutex_enter(&devi->devi_lock);
19230Sstevel@tonic-gate 	if (devi->devi_busy_thread == (void *)curthread) {
19240Sstevel@tonic-gate 		devi->devi_circular++;
19250Sstevel@tonic-gate 	} else {
19260Sstevel@tonic-gate 		if (!DEVI_BUSY_CHANGING(devi)) {
19270Sstevel@tonic-gate 			devi->devi_flags |= DEVI_BUSY;
19280Sstevel@tonic-gate 			devi->devi_busy_thread = (void *)curthread;
19290Sstevel@tonic-gate 		} else {
19300Sstevel@tonic-gate 			rval = 0;	/* devi is busy */
19310Sstevel@tonic-gate 		}
19320Sstevel@tonic-gate 	}
19330Sstevel@tonic-gate 	*circular = devi->devi_circular;
19340Sstevel@tonic-gate 	mutex_exit(&devi->devi_lock);
19350Sstevel@tonic-gate 	return (rval);
19360Sstevel@tonic-gate }
19370Sstevel@tonic-gate 
19380Sstevel@tonic-gate /*
19390Sstevel@tonic-gate  * Allocate and initialize a new dev_info structure.
19400Sstevel@tonic-gate  *
19410Sstevel@tonic-gate  * This routine may be called at interrupt time by a nexus in
19420Sstevel@tonic-gate  * response to a hotplug event, therefore memory allocations are
19430Sstevel@tonic-gate  * not allowed to sleep.
19440Sstevel@tonic-gate  */
19450Sstevel@tonic-gate int
1946789Sahrens ndi_devi_alloc(dev_info_t *parent, char *node_name, pnode_t nodeid,
19470Sstevel@tonic-gate     dev_info_t **ret_dip)
19480Sstevel@tonic-gate {
19490Sstevel@tonic-gate 	ASSERT(node_name != NULL);
19500Sstevel@tonic-gate 	ASSERT(ret_dip != NULL);
19510Sstevel@tonic-gate 
19520Sstevel@tonic-gate 	*ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL,
19530Sstevel@tonic-gate 	    KM_NOSLEEP);
19540Sstevel@tonic-gate 	if (*ret_dip == NULL) {
19550Sstevel@tonic-gate 		return (NDI_NOMEM);
19560Sstevel@tonic-gate 	}
19570Sstevel@tonic-gate 
19580Sstevel@tonic-gate 	return (NDI_SUCCESS);
19590Sstevel@tonic-gate }
19600Sstevel@tonic-gate 
19610Sstevel@tonic-gate /*
19620Sstevel@tonic-gate  * Allocate and initialize a new dev_info structure
19630Sstevel@tonic-gate  * This routine may sleep and should not be called at interrupt time
19640Sstevel@tonic-gate  */
19650Sstevel@tonic-gate void
1966789Sahrens ndi_devi_alloc_sleep(dev_info_t *parent, char *node_name, pnode_t nodeid,
19670Sstevel@tonic-gate     dev_info_t **ret_dip)
19680Sstevel@tonic-gate {
19690Sstevel@tonic-gate 	ASSERT(node_name != NULL);
19700Sstevel@tonic-gate 	ASSERT(ret_dip != NULL);
19710Sstevel@tonic-gate 
19720Sstevel@tonic-gate 	*ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL,
19730Sstevel@tonic-gate 	    KM_SLEEP);
19740Sstevel@tonic-gate 	ASSERT(*ret_dip);
19750Sstevel@tonic-gate }
19760Sstevel@tonic-gate 
19770Sstevel@tonic-gate /*
19780Sstevel@tonic-gate  * Remove an initialized (but not yet attached) dev_info
19790Sstevel@tonic-gate  * node from it's parent.
19800Sstevel@tonic-gate  */
19810Sstevel@tonic-gate int
19820Sstevel@tonic-gate ndi_devi_free(dev_info_t *dip)
19830Sstevel@tonic-gate {
19840Sstevel@tonic-gate 	ASSERT(dip != NULL);
19850Sstevel@tonic-gate 
19860Sstevel@tonic-gate 	if (i_ddi_node_state(dip) >= DS_INITIALIZED)
19870Sstevel@tonic-gate 		return (DDI_FAILURE);
19880Sstevel@tonic-gate 
19890Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_free: %s%d (%p)\n",
19900Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
19910Sstevel@tonic-gate 
19920Sstevel@tonic-gate 	(void) ddi_remove_child(dip, 0);
19930Sstevel@tonic-gate 
19940Sstevel@tonic-gate 	return (NDI_SUCCESS);
19950Sstevel@tonic-gate }
19960Sstevel@tonic-gate 
19970Sstevel@tonic-gate /*
19980Sstevel@tonic-gate  * ndi_devi_bind_driver() binds a driver to a given device. If it fails
19990Sstevel@tonic-gate  * to bind the driver, it returns an appropriate error back. Some drivers
20000Sstevel@tonic-gate  * may want to know if the actually failed to bind.
20010Sstevel@tonic-gate  */
20020Sstevel@tonic-gate int
20030Sstevel@tonic-gate ndi_devi_bind_driver(dev_info_t *dip, uint_t flags)
20040Sstevel@tonic-gate {
20050Sstevel@tonic-gate 	int ret = NDI_FAILURE;
20060Sstevel@tonic-gate 	int circ;
20070Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
20080Sstevel@tonic-gate 	ASSERT(pdip);
20090Sstevel@tonic-gate 
20100Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
20110Sstevel@tonic-gate 	    "ndi_devi_bind_driver: %s%d (%p) flags: %x\n",
20120Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
20130Sstevel@tonic-gate 
20140Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
20150Sstevel@tonic-gate 	if (i_ndi_config_node(dip, DS_BOUND, flags) == DDI_SUCCESS)
20160Sstevel@tonic-gate 		ret = NDI_SUCCESS;
20170Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
20180Sstevel@tonic-gate 
20190Sstevel@tonic-gate 	return (ret);
20200Sstevel@tonic-gate }
20210Sstevel@tonic-gate 
20220Sstevel@tonic-gate /*
20230Sstevel@tonic-gate  * ndi_devi_unbind_driver: unbind the dip
20240Sstevel@tonic-gate  */
20250Sstevel@tonic-gate static int
20260Sstevel@tonic-gate ndi_devi_unbind_driver(dev_info_t *dip)
20270Sstevel@tonic-gate {
20280Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
20290Sstevel@tonic-gate 
20300Sstevel@tonic-gate 	return (i_ndi_unconfig_node(dip, DS_LINKED, 0));
20310Sstevel@tonic-gate }
20320Sstevel@tonic-gate 
20330Sstevel@tonic-gate /*
20340Sstevel@tonic-gate  * Misc. help routines called by framework only
20350Sstevel@tonic-gate  */
20360Sstevel@tonic-gate 
20370Sstevel@tonic-gate /*
20380Sstevel@tonic-gate  * Get the state of node
20390Sstevel@tonic-gate  */
20400Sstevel@tonic-gate ddi_node_state_t
20410Sstevel@tonic-gate i_ddi_node_state(dev_info_t *dip)
20420Sstevel@tonic-gate {
20430Sstevel@tonic-gate 	return (DEVI(dip)->devi_node_state);
20440Sstevel@tonic-gate }
20450Sstevel@tonic-gate 
20460Sstevel@tonic-gate /*
20470Sstevel@tonic-gate  * Set the state of node
20480Sstevel@tonic-gate  */
20490Sstevel@tonic-gate void
20500Sstevel@tonic-gate i_ddi_set_node_state(dev_info_t *dip, ddi_node_state_t state)
20510Sstevel@tonic-gate {
20520Sstevel@tonic-gate 	DEVI(dip)->devi_node_state = state;
20530Sstevel@tonic-gate 	membar_enter();			/* make sure stores are flushed */
20540Sstevel@tonic-gate }
20550Sstevel@tonic-gate 
20560Sstevel@tonic-gate /*
20571333Scth  * Determine if node is attached. The implementation accommodates transient
20581333Scth  * DS_READY->DS_ATTACHED->DS_READY state changes.  Outside this file, this
20591333Scth  * function should be instead of i_ddi_node_state() DS_ATTACHED/DS_READY
20601333Scth  * state checks.
20611333Scth  */
20621333Scth int
20631333Scth i_ddi_devi_attached(dev_info_t *dip)
20641333Scth {
20651333Scth 	return (DEVI(dip)->devi_node_state >= DS_ATTACHED);
20661333Scth }
20671333Scth 
20681333Scth /*
20690Sstevel@tonic-gate  * Common function for finding a node in a sibling list given name and addr.
20700Sstevel@tonic-gate  *
20710Sstevel@tonic-gate  * By default, name is matched with devi_node_name. The following
20720Sstevel@tonic-gate  * alternative match strategies are supported:
20730Sstevel@tonic-gate  *
20744145Scth  *	FIND_NODE_BY_NODENAME: Match on node name - typical use.
20758912SChris.Horne@Sun.COM  *
20764145Scth  *	FIND_NODE_BY_DRIVER: A match on driver name bound to node is conducted.
20770Sstevel@tonic-gate  *		This support is used for support of OBP generic names and
20784145Scth  *		for the conversion from driver names to generic names. When
20790Sstevel@tonic-gate  *		more consistency in the generic name environment is achieved
20800Sstevel@tonic-gate  *		(and not needed for upgrade) this support can be removed.
20818912SChris.Horne@Sun.COM  *
20824145Scth  *	FIND_NODE_BY_ADDR: Match on just the addr.
20834145Scth  *		This support is only used/needed during boot to match
20844145Scth  *		a node bound via a path-based driver alias.
20850Sstevel@tonic-gate  *
20860Sstevel@tonic-gate  * If a child is not named (dev_addr == NULL), there are three
20870Sstevel@tonic-gate  * possible actions:
20880Sstevel@tonic-gate  *
20890Sstevel@tonic-gate  *	(1) skip it
20900Sstevel@tonic-gate  *	(2) FIND_ADDR_BY_INIT: bring child to DS_INITIALIZED state
20910Sstevel@tonic-gate  *	(3) FIND_ADDR_BY_CALLBACK: use a caller-supplied callback function
20920Sstevel@tonic-gate  */
20934145Scth #define	FIND_NODE_BY_NODENAME	0x01
20944145Scth #define	FIND_NODE_BY_DRIVER	0x02
20954145Scth #define	FIND_NODE_BY_ADDR	0x04
20960Sstevel@tonic-gate #define	FIND_ADDR_BY_INIT	0x10
20970Sstevel@tonic-gate #define	FIND_ADDR_BY_CALLBACK	0x20
20980Sstevel@tonic-gate 
20990Sstevel@tonic-gate static dev_info_t *
21000Sstevel@tonic-gate find_sibling(dev_info_t *head, char *cname, char *caddr, uint_t flag,
21010Sstevel@tonic-gate     int (*callback)(dev_info_t *, char *, int))
21020Sstevel@tonic-gate {
21030Sstevel@tonic-gate 	dev_info_t	*dip;
21040Sstevel@tonic-gate 	char		*addr, *buf;
21050Sstevel@tonic-gate 	major_t		major;
21064145Scth 	uint_t		by;
21074145Scth 
21084145Scth 	/* only one way to find a node */
21094145Scth 	by = flag &
21104145Scth 	    (FIND_NODE_BY_DRIVER | FIND_NODE_BY_NODENAME | FIND_NODE_BY_ADDR);
21114145Scth 	ASSERT(by && BIT_ONLYONESET(by));
21120Sstevel@tonic-gate 
21130Sstevel@tonic-gate 	/* only one way to name a node */
21140Sstevel@tonic-gate 	ASSERT(((flag & FIND_ADDR_BY_INIT) == 0) ||
21150Sstevel@tonic-gate 	    ((flag & FIND_ADDR_BY_CALLBACK) == 0));
21160Sstevel@tonic-gate 
21174145Scth 	if (by == FIND_NODE_BY_DRIVER) {
21180Sstevel@tonic-gate 		major = ddi_name_to_major(cname);
21197009Scth 		if (major == DDI_MAJOR_T_NONE)
21200Sstevel@tonic-gate 			return (NULL);
21210Sstevel@tonic-gate 	}
21220Sstevel@tonic-gate 
21230Sstevel@tonic-gate 	/* preallocate buffer of naming node by callback */
21240Sstevel@tonic-gate 	if (flag & FIND_ADDR_BY_CALLBACK)
21250Sstevel@tonic-gate 		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
21260Sstevel@tonic-gate 
21270Sstevel@tonic-gate 	/*
21280Sstevel@tonic-gate 	 * Walk the child list to find a match
21290Sstevel@tonic-gate 	 */
21300Sstevel@tonic-gate 
21310Sstevel@tonic-gate 	for (dip = head; dip; dip = ddi_get_next_sibling(dip)) {
21324145Scth 		if (by == FIND_NODE_BY_NODENAME) {
21334145Scth 			/* match node name */
21344145Scth 			if (strcmp(cname, DEVI(dip)->devi_node_name) != 0)
21354145Scth 				continue;
21364145Scth 		} else if (by == FIND_NODE_BY_DRIVER) {
21370Sstevel@tonic-gate 			/* match driver major */
21380Sstevel@tonic-gate 			if (DEVI(dip)->devi_major != major)
21390Sstevel@tonic-gate 				continue;
21400Sstevel@tonic-gate 		}
21410Sstevel@tonic-gate 
21420Sstevel@tonic-gate 		if ((addr = DEVI(dip)->devi_addr) == NULL) {
21430Sstevel@tonic-gate 			/* name the child based on the flag */
21440Sstevel@tonic-gate 			if (flag & FIND_ADDR_BY_INIT) {
21450Sstevel@tonic-gate 				if (ddi_initchild(ddi_get_parent(dip), dip)
21460Sstevel@tonic-gate 				    != DDI_SUCCESS)
21470Sstevel@tonic-gate 					continue;
21480Sstevel@tonic-gate 				addr = DEVI(dip)->devi_addr;
21490Sstevel@tonic-gate 			} else if (flag & FIND_ADDR_BY_CALLBACK) {
21500Sstevel@tonic-gate 				if ((callback == NULL) || (callback(
21510Sstevel@tonic-gate 				    dip, buf, MAXNAMELEN) != DDI_SUCCESS))
21520Sstevel@tonic-gate 					continue;
21530Sstevel@tonic-gate 				addr = buf;
21540Sstevel@tonic-gate 			} else {
21550Sstevel@tonic-gate 				continue;	/* skip */
21560Sstevel@tonic-gate 			}
21570Sstevel@tonic-gate 		}
21580Sstevel@tonic-gate 
21590Sstevel@tonic-gate 		/* match addr */
21600Sstevel@tonic-gate 		ASSERT(addr != NULL);
21610Sstevel@tonic-gate 		if (strcmp(caddr, addr) == 0)
21620Sstevel@tonic-gate 			break;	/* node found */
21630Sstevel@tonic-gate 
21640Sstevel@tonic-gate 	}
21650Sstevel@tonic-gate 	if (flag & FIND_ADDR_BY_CALLBACK)
21660Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
21670Sstevel@tonic-gate 	return (dip);
21680Sstevel@tonic-gate }
21690Sstevel@tonic-gate 
21700Sstevel@tonic-gate /*
21710Sstevel@tonic-gate  * Find child of pdip with name: cname@caddr
21720Sstevel@tonic-gate  * Called by init_node() to look for duplicate nodes
21730Sstevel@tonic-gate  */
21740Sstevel@tonic-gate static dev_info_t *
21750Sstevel@tonic-gate find_duplicate_child(dev_info_t *pdip, dev_info_t *dip)
21760Sstevel@tonic-gate {
21770Sstevel@tonic-gate 	dev_info_t *dup;
21780Sstevel@tonic-gate 	char *cname = DEVI(dip)->devi_node_name;
21790Sstevel@tonic-gate 	char *caddr = DEVI(dip)->devi_addr;
21800Sstevel@tonic-gate 
21810Sstevel@tonic-gate 	/* search nodes before dip */
21824145Scth 	dup = find_sibling(ddi_get_child(pdip), cname, caddr,
21834145Scth 	    FIND_NODE_BY_NODENAME, NULL);
21840Sstevel@tonic-gate 	if (dup != dip)
21850Sstevel@tonic-gate 		return (dup);
21860Sstevel@tonic-gate 
21870Sstevel@tonic-gate 	/*
21880Sstevel@tonic-gate 	 * search nodes after dip; normally this is not needed,
21890Sstevel@tonic-gate 	 */
21900Sstevel@tonic-gate 	return (find_sibling(ddi_get_next_sibling(dip), cname, caddr,
21914145Scth 	    FIND_NODE_BY_NODENAME, NULL));
21920Sstevel@tonic-gate }
21930Sstevel@tonic-gate 
21940Sstevel@tonic-gate /*
21950Sstevel@tonic-gate  * Find a child of a given name and address, using a callback to name
21960Sstevel@tonic-gate  * unnamed children. cname is the binding name.
21970Sstevel@tonic-gate  */
21980Sstevel@tonic-gate static dev_info_t *
21990Sstevel@tonic-gate find_child_by_callback(dev_info_t *pdip, char *cname, char *caddr,
22000Sstevel@tonic-gate     int (*name_node)(dev_info_t *, char *, int))
22010Sstevel@tonic-gate {
22020Sstevel@tonic-gate 	return (find_sibling(ddi_get_child(pdip), cname, caddr,
22034145Scth 	    FIND_NODE_BY_DRIVER|FIND_ADDR_BY_CALLBACK, name_node));
22040Sstevel@tonic-gate }
22050Sstevel@tonic-gate 
22060Sstevel@tonic-gate /*
22070Sstevel@tonic-gate  * Find a child of a given name and address, invoking initchild to name
22080Sstevel@tonic-gate  * unnamed children. cname is the node name.
22090Sstevel@tonic-gate  */
22100Sstevel@tonic-gate static dev_info_t *
22110Sstevel@tonic-gate find_child_by_name(dev_info_t *pdip, char *cname, char *caddr)
22120Sstevel@tonic-gate {
22130Sstevel@tonic-gate 	dev_info_t	*dip;
22140Sstevel@tonic-gate 
22154145Scth 	/* attempt search without changing state of preceding siblings */
22164145Scth 	dip = find_sibling(ddi_get_child(pdip), cname, caddr,
22174145Scth 	    FIND_NODE_BY_NODENAME, NULL);
22180Sstevel@tonic-gate 	if (dip)
22190Sstevel@tonic-gate 		return (dip);
22200Sstevel@tonic-gate 
22210Sstevel@tonic-gate 	return (find_sibling(ddi_get_child(pdip), cname, caddr,
22224145Scth 	    FIND_NODE_BY_NODENAME|FIND_ADDR_BY_INIT, NULL));
22230Sstevel@tonic-gate }
22240Sstevel@tonic-gate 
22250Sstevel@tonic-gate /*
22260Sstevel@tonic-gate  * Find a child of a given name and address, invoking initchild to name
22270Sstevel@tonic-gate  * unnamed children. cname is the node name.
22280Sstevel@tonic-gate  */
22290Sstevel@tonic-gate static dev_info_t *
22300Sstevel@tonic-gate find_child_by_driver(dev_info_t *pdip, char *cname, char *caddr)
22310Sstevel@tonic-gate {
22320Sstevel@tonic-gate 	dev_info_t	*dip;
22330Sstevel@tonic-gate 
22344145Scth 	/* attempt search without changing state of preceding siblings */
22350Sstevel@tonic-gate 	dip = find_sibling(ddi_get_child(pdip), cname, caddr,
22364145Scth 	    FIND_NODE_BY_DRIVER, NULL);
22370Sstevel@tonic-gate 	if (dip)
22380Sstevel@tonic-gate 		return (dip);
22390Sstevel@tonic-gate 
22400Sstevel@tonic-gate 	return (find_sibling(ddi_get_child(pdip), cname, caddr,
22414145Scth 	    FIND_NODE_BY_DRIVER|FIND_ADDR_BY_INIT, NULL));
22424145Scth }
22434145Scth 
22444145Scth /*
22454145Scth  * Find a child of a given address, invoking initchild to name
22464145Scth  * unnamed children. cname is the node name.
22474145Scth  *
22484145Scth  * NOTE: This function is only used during boot. One would hope that
22494145Scth  * unique sibling unit-addresses on hardware branches of the tree would
22504145Scth  * be a requirement to avoid two drivers trying to control the same
22514145Scth  * piece of hardware. Unfortunately there are some cases where this
22524145Scth  * situation exists (/ssm@0,0/pci@1c,700000 /ssm@0,0/sghsc@1c,700000).
22534145Scth  * Until unit-address uniqueness of siblings is guaranteed, use of this
22544145Scth  * interface for purposes other than boot should be avoided.
22554145Scth  */
22564145Scth static dev_info_t *
22574145Scth find_child_by_addr(dev_info_t *pdip, char *caddr)
22584145Scth {
22594145Scth 	dev_info_t	*dip;
22604145Scth 
22614540Scth 	/* return NULL if called without a unit-address */
22624540Scth 	if ((caddr == NULL) || (*caddr == '\0'))
22634540Scth 		return (NULL);
22644540Scth 
22654145Scth 	/* attempt search without changing state of preceding siblings */
22664145Scth 	dip = find_sibling(ddi_get_child(pdip), NULL, caddr,
22674145Scth 	    FIND_NODE_BY_ADDR, NULL);
22684145Scth 	if (dip)
22694145Scth 		return (dip);
22704145Scth 
22714145Scth 	return (find_sibling(ddi_get_child(pdip), NULL, caddr,
22724145Scth 	    FIND_NODE_BY_ADDR|FIND_ADDR_BY_INIT, NULL));
22730Sstevel@tonic-gate }
22740Sstevel@tonic-gate 
22750Sstevel@tonic-gate /*
22760Sstevel@tonic-gate  * Deleting a property list. Take care, since some property structures
22770Sstevel@tonic-gate  * may not be fully built.
22780Sstevel@tonic-gate  */
22790Sstevel@tonic-gate void
22800Sstevel@tonic-gate i_ddi_prop_list_delete(ddi_prop_t *prop)
22810Sstevel@tonic-gate {
22820Sstevel@tonic-gate 	while (prop) {
22830Sstevel@tonic-gate 		ddi_prop_t *next = prop->prop_next;
22840Sstevel@tonic-gate 		if (prop->prop_name)
22850Sstevel@tonic-gate 			kmem_free(prop->prop_name, strlen(prop->prop_name) + 1);
22860Sstevel@tonic-gate 		if ((prop->prop_len != 0) && prop->prop_val)
22870Sstevel@tonic-gate 			kmem_free(prop->prop_val, prop->prop_len);
22880Sstevel@tonic-gate 		kmem_free(prop, sizeof (struct ddi_prop));
22890Sstevel@tonic-gate 		prop = next;
22900Sstevel@tonic-gate 	}
22910Sstevel@tonic-gate }
22920Sstevel@tonic-gate 
22930Sstevel@tonic-gate /*
22940Sstevel@tonic-gate  * Duplicate property list
22950Sstevel@tonic-gate  */
22960Sstevel@tonic-gate ddi_prop_t *
22970Sstevel@tonic-gate i_ddi_prop_list_dup(ddi_prop_t *prop, uint_t flag)
22980Sstevel@tonic-gate {
22990Sstevel@tonic-gate 	ddi_prop_t *result, *prev, *copy;
23000Sstevel@tonic-gate 
23010Sstevel@tonic-gate 	if (prop == NULL)
23020Sstevel@tonic-gate 		return (NULL);
23030Sstevel@tonic-gate 
23040Sstevel@tonic-gate 	result = prev = NULL;
23050Sstevel@tonic-gate 	for (; prop != NULL; prop = prop->prop_next) {
23060Sstevel@tonic-gate 		ASSERT(prop->prop_name != NULL);
23070Sstevel@tonic-gate 		copy = kmem_zalloc(sizeof (struct ddi_prop), flag);
23080Sstevel@tonic-gate 		if (copy == NULL)
23090Sstevel@tonic-gate 			goto fail;
23100Sstevel@tonic-gate 
23110Sstevel@tonic-gate 		copy->prop_dev = prop->prop_dev;
23120Sstevel@tonic-gate 		copy->prop_flags = prop->prop_flags;
23130Sstevel@tonic-gate 		copy->prop_name = i_ddi_strdup(prop->prop_name, flag);
23140Sstevel@tonic-gate 		if (copy->prop_name == NULL)
23150Sstevel@tonic-gate 			goto fail;
23160Sstevel@tonic-gate 
23170Sstevel@tonic-gate 		if ((copy->prop_len = prop->prop_len) != 0) {
23180Sstevel@tonic-gate 			copy->prop_val = kmem_zalloc(prop->prop_len, flag);
23190Sstevel@tonic-gate 			if (copy->prop_val == NULL)
23200Sstevel@tonic-gate 				goto fail;
23210Sstevel@tonic-gate 
23220Sstevel@tonic-gate 			bcopy(prop->prop_val, copy->prop_val, prop->prop_len);
23230Sstevel@tonic-gate 		}
23240Sstevel@tonic-gate 
23250Sstevel@tonic-gate 		if (prev == NULL)
23260Sstevel@tonic-gate 			result = prev = copy;
23270Sstevel@tonic-gate 		else
23280Sstevel@tonic-gate 			prev->prop_next = copy;
23290Sstevel@tonic-gate 		prev = copy;
23300Sstevel@tonic-gate 	}
23310Sstevel@tonic-gate 	return (result);
23320Sstevel@tonic-gate 
23330Sstevel@tonic-gate fail:
23340Sstevel@tonic-gate 	i_ddi_prop_list_delete(result);
23350Sstevel@tonic-gate 	return (NULL);
23360Sstevel@tonic-gate }
23370Sstevel@tonic-gate 
23380Sstevel@tonic-gate /*
23390Sstevel@tonic-gate  * Create a reference property list, currently used only for
23400Sstevel@tonic-gate  * driver global properties. Created with ref count of 1.
23410Sstevel@tonic-gate  */
23420Sstevel@tonic-gate ddi_prop_list_t *
23430Sstevel@tonic-gate i_ddi_prop_list_create(ddi_prop_t *props)
23440Sstevel@tonic-gate {
23450Sstevel@tonic-gate 	ddi_prop_list_t *list = kmem_alloc(sizeof (*list), KM_SLEEP);
23460Sstevel@tonic-gate 	list->prop_list = props;
23470Sstevel@tonic-gate 	list->prop_ref = 1;
23480Sstevel@tonic-gate 	return (list);
23490Sstevel@tonic-gate }
23500Sstevel@tonic-gate 
23510Sstevel@tonic-gate /*
23520Sstevel@tonic-gate  * Increment/decrement reference count. The reference is
23530Sstevel@tonic-gate  * protected by dn_lock. The only interfaces modifying
23540Sstevel@tonic-gate  * dn_global_prop_ptr is in impl_make[free]_parlist().
23550Sstevel@tonic-gate  */
23560Sstevel@tonic-gate void
23570Sstevel@tonic-gate i_ddi_prop_list_hold(ddi_prop_list_t *prop_list, struct devnames *dnp)
23580Sstevel@tonic-gate {
23590Sstevel@tonic-gate 	ASSERT(prop_list->prop_ref >= 0);
23600Sstevel@tonic-gate 	ASSERT(mutex_owned(&dnp->dn_lock));
23610Sstevel@tonic-gate 	prop_list->prop_ref++;
23620Sstevel@tonic-gate }
23630Sstevel@tonic-gate 
23640Sstevel@tonic-gate void
23650Sstevel@tonic-gate i_ddi_prop_list_rele(ddi_prop_list_t *prop_list, struct devnames *dnp)
23660Sstevel@tonic-gate {
23670Sstevel@tonic-gate 	ASSERT(prop_list->prop_ref > 0);
23680Sstevel@tonic-gate 	ASSERT(mutex_owned(&dnp->dn_lock));
23690Sstevel@tonic-gate 	prop_list->prop_ref--;
23700Sstevel@tonic-gate 
23710Sstevel@tonic-gate 	if (prop_list->prop_ref == 0) {
23720Sstevel@tonic-gate 		i_ddi_prop_list_delete(prop_list->prop_list);
23730Sstevel@tonic-gate 		kmem_free(prop_list, sizeof (*prop_list));
23740Sstevel@tonic-gate 	}
23750Sstevel@tonic-gate }
23760Sstevel@tonic-gate 
23770Sstevel@tonic-gate /*
23780Sstevel@tonic-gate  * Free table of classes by drivers
23790Sstevel@tonic-gate  */
23800Sstevel@tonic-gate void
23810Sstevel@tonic-gate i_ddi_free_exported_classes(char **classes, int n)
23820Sstevel@tonic-gate {
23830Sstevel@tonic-gate 	if ((n == 0) || (classes == NULL))
23840Sstevel@tonic-gate 		return;
23850Sstevel@tonic-gate 
23860Sstevel@tonic-gate 	kmem_free(classes, n * sizeof (char *));
23870Sstevel@tonic-gate }
23880Sstevel@tonic-gate 
23890Sstevel@tonic-gate /*
23900Sstevel@tonic-gate  * Get all classes exported by dip
23910Sstevel@tonic-gate  */
23920Sstevel@tonic-gate int
23930Sstevel@tonic-gate i_ddi_get_exported_classes(dev_info_t *dip, char ***classes)
23940Sstevel@tonic-gate {
23950Sstevel@tonic-gate 	extern void lock_hw_class_list();
23960Sstevel@tonic-gate 	extern void unlock_hw_class_list();
23970Sstevel@tonic-gate 	extern int get_class(const char *, char **);
23980Sstevel@tonic-gate 
23990Sstevel@tonic-gate 	static char *rootclass = "root";
24000Sstevel@tonic-gate 	int n = 0, nclass = 0;
24010Sstevel@tonic-gate 	char **buf;
24020Sstevel@tonic-gate 
24030Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) >= DS_BOUND);
24040Sstevel@tonic-gate 
24050Sstevel@tonic-gate 	if (dip == ddi_root_node())	/* rootnode exports class "root" */
24060Sstevel@tonic-gate 		nclass = 1;
24070Sstevel@tonic-gate 	lock_hw_class_list();
24080Sstevel@tonic-gate 	nclass += get_class(ddi_driver_name(dip), NULL);
24090Sstevel@tonic-gate 	if (nclass == 0) {
24100Sstevel@tonic-gate 		unlock_hw_class_list();
24110Sstevel@tonic-gate 		return (0);		/* no class exported */
24120Sstevel@tonic-gate 	}
24130Sstevel@tonic-gate 
24140Sstevel@tonic-gate 	*classes = buf = kmem_alloc(nclass * sizeof (char *), KM_SLEEP);
24150Sstevel@tonic-gate 	if (dip == ddi_root_node()) {
24160Sstevel@tonic-gate 		*buf++ = rootclass;
24170Sstevel@tonic-gate 		n = 1;
24180Sstevel@tonic-gate 	}
24190Sstevel@tonic-gate 	n += get_class(ddi_driver_name(dip), buf);
24200Sstevel@tonic-gate 	unlock_hw_class_list();
24210Sstevel@tonic-gate 
24220Sstevel@tonic-gate 	ASSERT(n == nclass);    /* make sure buf wasn't overrun */
24230Sstevel@tonic-gate 	return (nclass);
24240Sstevel@tonic-gate }
24250Sstevel@tonic-gate 
24260Sstevel@tonic-gate /*
24270Sstevel@tonic-gate  * Helper functions, returns NULL if no memory.
24280Sstevel@tonic-gate  */
24290Sstevel@tonic-gate char *
24300Sstevel@tonic-gate i_ddi_strdup(char *str, uint_t flag)
24310Sstevel@tonic-gate {
24320Sstevel@tonic-gate 	char *copy;
24330Sstevel@tonic-gate 
24340Sstevel@tonic-gate 	if (str == NULL)
24350Sstevel@tonic-gate 		return (NULL);
24360Sstevel@tonic-gate 
24370Sstevel@tonic-gate 	copy = kmem_alloc(strlen(str) + 1, flag);
24380Sstevel@tonic-gate 	if (copy == NULL)
24390Sstevel@tonic-gate 		return (NULL);
24400Sstevel@tonic-gate 
24410Sstevel@tonic-gate 	(void) strcpy(copy, str);
24420Sstevel@tonic-gate 	return (copy);
24430Sstevel@tonic-gate }
24440Sstevel@tonic-gate 
24450Sstevel@tonic-gate /*
24460Sstevel@tonic-gate  * Load driver.conf file for major. Load all if major == -1.
24470Sstevel@tonic-gate  *
24480Sstevel@tonic-gate  * This is called
24490Sstevel@tonic-gate  * - early in boot after devnames array is initialized
24500Sstevel@tonic-gate  * - from vfs code when certain file systems are mounted
24510Sstevel@tonic-gate  * - from add_drv when a new driver is added
24520Sstevel@tonic-gate  */
24530Sstevel@tonic-gate int
24540Sstevel@tonic-gate i_ddi_load_drvconf(major_t major)
24550Sstevel@tonic-gate {
24560Sstevel@tonic-gate 	extern int modrootloaded;
24570Sstevel@tonic-gate 
24580Sstevel@tonic-gate 	major_t low, high, m;
24590Sstevel@tonic-gate 
24607009Scth 	if (major == DDI_MAJOR_T_NONE) {
24610Sstevel@tonic-gate 		low = 0;
24620Sstevel@tonic-gate 		high = devcnt - 1;
24630Sstevel@tonic-gate 	} else {
24640Sstevel@tonic-gate 		if (major >= devcnt)
24650Sstevel@tonic-gate 			return (EINVAL);
24660Sstevel@tonic-gate 		low = high = major;
24670Sstevel@tonic-gate 	}
24680Sstevel@tonic-gate 
24690Sstevel@tonic-gate 	for (m = low; m <= high; m++) {
24700Sstevel@tonic-gate 		struct devnames *dnp = &devnamesp[m];
24710Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
24720Sstevel@tonic-gate 		dnp->dn_flags &= ~DN_DRIVER_HELD;
24730Sstevel@tonic-gate 		(void) impl_make_parlist(m);
24740Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
24750Sstevel@tonic-gate 	}
24760Sstevel@tonic-gate 
24770Sstevel@tonic-gate 	if (modrootloaded) {
24780Sstevel@tonic-gate 		ddi_walk_devs(ddi_root_node(), reset_nexus_flags,
24790Sstevel@tonic-gate 		    (void *)(uintptr_t)major);
24800Sstevel@tonic-gate 	}
24810Sstevel@tonic-gate 
24820Sstevel@tonic-gate 	/* build dn_list from old entries in path_to_inst */
24830Sstevel@tonic-gate 	e_ddi_unorphan_instance_nos();
24840Sstevel@tonic-gate 	return (0);
24850Sstevel@tonic-gate }
24860Sstevel@tonic-gate 
24870Sstevel@tonic-gate /*
24880Sstevel@tonic-gate  * Unload a specific driver.conf.
24890Sstevel@tonic-gate  * Don't support unload all because it doesn't make any sense
24900Sstevel@tonic-gate  */
24910Sstevel@tonic-gate int
24920Sstevel@tonic-gate i_ddi_unload_drvconf(major_t major)
24930Sstevel@tonic-gate {
24940Sstevel@tonic-gate 	int error;
24950Sstevel@tonic-gate 	struct devnames *dnp;
24960Sstevel@tonic-gate 
24970Sstevel@tonic-gate 	if (major >= devcnt)
24980Sstevel@tonic-gate 		return (EINVAL);
24990Sstevel@tonic-gate 
25000Sstevel@tonic-gate 	/*
25010Sstevel@tonic-gate 	 * Take the per-driver lock while unloading driver.conf
25020Sstevel@tonic-gate 	 */
25030Sstevel@tonic-gate 	dnp = &devnamesp[major];
25040Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
25050Sstevel@tonic-gate 	error = impl_free_parlist(major);
25060Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
25070Sstevel@tonic-gate 	return (error);
25080Sstevel@tonic-gate }
25090Sstevel@tonic-gate 
25100Sstevel@tonic-gate /*
25110Sstevel@tonic-gate  * Merge a .conf node. This is called by nexus drivers to augment
25120Sstevel@tonic-gate  * hw node with properties specified in driver.conf file. This function
25130Sstevel@tonic-gate  * takes a callback routine to name nexus children.
25140Sstevel@tonic-gate  * The parent node must be held busy.
25150Sstevel@tonic-gate  *
25160Sstevel@tonic-gate  * It returns DDI_SUCCESS if the node is merged and DDI_FAILURE otherwise.
25170Sstevel@tonic-gate  */
25180Sstevel@tonic-gate int
25190Sstevel@tonic-gate ndi_merge_node(dev_info_t *dip, int (*name_node)(dev_info_t *, char *, int))
25200Sstevel@tonic-gate {
25210Sstevel@tonic-gate 	dev_info_t *hwdip;
25220Sstevel@tonic-gate 
25230Sstevel@tonic-gate 	ASSERT(ndi_dev_is_persistent_node(dip) == 0);
25240Sstevel@tonic-gate 	ASSERT(ddi_get_name_addr(dip) != NULL);
25250Sstevel@tonic-gate 
25260Sstevel@tonic-gate 	hwdip = find_child_by_callback(ddi_get_parent(dip),
25270Sstevel@tonic-gate 	    ddi_binding_name(dip), ddi_get_name_addr(dip), name_node);
25280Sstevel@tonic-gate 
25290Sstevel@tonic-gate 	/*
25300Sstevel@tonic-gate 	 * Look for the hardware node that is the target of the merge;
25310Sstevel@tonic-gate 	 * return failure if not found.
25320Sstevel@tonic-gate 	 */
25330Sstevel@tonic-gate 	if ((hwdip == NULL) || (hwdip == dip)) {
25340Sstevel@tonic-gate 		char *buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
25350Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_WARN, "No HW node to merge conf node %s",
25360Sstevel@tonic-gate 		    ddi_deviname(dip, buf)));
25370Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
25380Sstevel@tonic-gate 		return (DDI_FAILURE);
25390Sstevel@tonic-gate 	}
25400Sstevel@tonic-gate 
25410Sstevel@tonic-gate 	/*
25420Sstevel@tonic-gate 	 * Make sure the hardware node is uninitialized and has no property.
25430Sstevel@tonic-gate 	 * This may not be the case if new .conf files are load after some
25440Sstevel@tonic-gate 	 * hardware nodes have already been initialized and attached.
25450Sstevel@tonic-gate 	 *
25460Sstevel@tonic-gate 	 * N.B. We return success here because the node was *intended*
25478912SChris.Horne@Sun.COM 	 *	to be a merge node because there is a hw node with the name.
25480Sstevel@tonic-gate 	 */
25490Sstevel@tonic-gate 	mutex_enter(&DEVI(hwdip)->devi_lock);
25500Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(hwdip) == 0) {
25510Sstevel@tonic-gate 		char *buf;
25520Sstevel@tonic-gate 		mutex_exit(&DEVI(hwdip)->devi_lock);
25530Sstevel@tonic-gate 
25540Sstevel@tonic-gate 		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
25550Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE, "Duplicate .conf node %s",
25560Sstevel@tonic-gate 		    ddi_deviname(dip, buf)));
25570Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
25580Sstevel@tonic-gate 		return (DDI_SUCCESS);
25590Sstevel@tonic-gate 	}
25600Sstevel@tonic-gate 
25610Sstevel@tonic-gate 	/*
25620Sstevel@tonic-gate 	 * If it is possible that the hardware has already been touched
25630Sstevel@tonic-gate 	 * then don't merge.
25640Sstevel@tonic-gate 	 */
25650Sstevel@tonic-gate 	if (i_ddi_node_state(hwdip) >= DS_INITIALIZED ||
25660Sstevel@tonic-gate 	    (DEVI(hwdip)->devi_sys_prop_ptr != NULL) ||
25670Sstevel@tonic-gate 	    (DEVI(hwdip)->devi_drv_prop_ptr != NULL)) {
25680Sstevel@tonic-gate 		char *buf;
25690Sstevel@tonic-gate 		mutex_exit(&DEVI(hwdip)->devi_lock);
25700Sstevel@tonic-gate 
25710Sstevel@tonic-gate 		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
25720Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE,
25730Sstevel@tonic-gate 		    "!Cannot merge .conf node %s with hw node %p "
25740Sstevel@tonic-gate 		    "-- not in proper state",
25750Sstevel@tonic-gate 		    ddi_deviname(dip, buf), (void *)hwdip));
25760Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
25770Sstevel@tonic-gate 		return (DDI_SUCCESS);
25780Sstevel@tonic-gate 	}
25790Sstevel@tonic-gate 
25800Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
25810Sstevel@tonic-gate 	DEVI(hwdip)->devi_sys_prop_ptr = DEVI(dip)->devi_sys_prop_ptr;
25820Sstevel@tonic-gate 	DEVI(hwdip)->devi_drv_prop_ptr = DEVI(dip)->devi_drv_prop_ptr;
25830Sstevel@tonic-gate 	DEVI(dip)->devi_sys_prop_ptr = NULL;
25840Sstevel@tonic-gate 	DEVI(dip)->devi_drv_prop_ptr = NULL;
25850Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
25860Sstevel@tonic-gate 	mutex_exit(&DEVI(hwdip)->devi_lock);
25870Sstevel@tonic-gate 
25880Sstevel@tonic-gate 	return (DDI_SUCCESS);
25890Sstevel@tonic-gate }
25900Sstevel@tonic-gate 
25910Sstevel@tonic-gate /*
25920Sstevel@tonic-gate  * Merge a "wildcard" .conf node. This is called by nexus drivers to
25930Sstevel@tonic-gate  * augment a set of hw node with properties specified in driver.conf file.
25940Sstevel@tonic-gate  * The parent node must be held busy.
25950Sstevel@tonic-gate  *
25960Sstevel@tonic-gate  * There is no failure mode, since the nexus may or may not have child
25970Sstevel@tonic-gate  * node bound the driver specified by the wildcard node.
25980Sstevel@tonic-gate  */
25990Sstevel@tonic-gate void
26000Sstevel@tonic-gate ndi_merge_wildcard_node(dev_info_t *dip)
26010Sstevel@tonic-gate {
26020Sstevel@tonic-gate 	dev_info_t *hwdip;
26030Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
26040Sstevel@tonic-gate 	major_t major = ddi_driver_major(dip);
26050Sstevel@tonic-gate 
26060Sstevel@tonic-gate 	/* never attempt to merge a hw node */
26070Sstevel@tonic-gate 	ASSERT(ndi_dev_is_persistent_node(dip) == 0);
26080Sstevel@tonic-gate 	/* must be bound to a driver major number */
26097009Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
26100Sstevel@tonic-gate 
26110Sstevel@tonic-gate 	/*
26120Sstevel@tonic-gate 	 * Walk the child list to find all nodes bound to major
26130Sstevel@tonic-gate 	 * and copy properties.
26140Sstevel@tonic-gate 	 */
26150Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
26167224Scth 	ASSERT(DEVI_BUSY_OWNED(pdip));
26170Sstevel@tonic-gate 	for (hwdip = ddi_get_child(pdip); hwdip;
26180Sstevel@tonic-gate 	    hwdip = ddi_get_next_sibling(hwdip)) {
26190Sstevel@tonic-gate 		/*
26200Sstevel@tonic-gate 		 * Skip nodes not bound to same driver
26210Sstevel@tonic-gate 		 */
26220Sstevel@tonic-gate 		if (ddi_driver_major(hwdip) != major)
26230Sstevel@tonic-gate 			continue;
26240Sstevel@tonic-gate 
26250Sstevel@tonic-gate 		/*
26260Sstevel@tonic-gate 		 * Skip .conf nodes
26270Sstevel@tonic-gate 		 */
26280Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(hwdip) == 0)
26290Sstevel@tonic-gate 			continue;
26300Sstevel@tonic-gate 
26310Sstevel@tonic-gate 		/*
26320Sstevel@tonic-gate 		 * Make sure the node is uninitialized and has no property.
26330Sstevel@tonic-gate 		 */
26340Sstevel@tonic-gate 		mutex_enter(&DEVI(hwdip)->devi_lock);
26350Sstevel@tonic-gate 		if (i_ddi_node_state(hwdip) >= DS_INITIALIZED ||
26360Sstevel@tonic-gate 		    (DEVI(hwdip)->devi_sys_prop_ptr != NULL) ||
26370Sstevel@tonic-gate 		    (DEVI(hwdip)->devi_drv_prop_ptr != NULL)) {
26380Sstevel@tonic-gate 			mutex_exit(&DEVI(hwdip)->devi_lock);
26390Sstevel@tonic-gate 			NDI_CONFIG_DEBUG((CE_NOTE, "HW node %p state not "
26400Sstevel@tonic-gate 			    "suitable for merging wildcard conf node %s",
26410Sstevel@tonic-gate 			    (void *)hwdip, ddi_node_name(dip)));
26420Sstevel@tonic-gate 			continue;
26430Sstevel@tonic-gate 		}
26440Sstevel@tonic-gate 
26450Sstevel@tonic-gate 		DEVI(hwdip)->devi_sys_prop_ptr =
26460Sstevel@tonic-gate 		    i_ddi_prop_list_dup(DEVI(dip)->devi_sys_prop_ptr, KM_SLEEP);
26470Sstevel@tonic-gate 		DEVI(hwdip)->devi_drv_prop_ptr =
26480Sstevel@tonic-gate 		    i_ddi_prop_list_dup(DEVI(dip)->devi_drv_prop_ptr, KM_SLEEP);
26490Sstevel@tonic-gate 		mutex_exit(&DEVI(hwdip)->devi_lock);
26500Sstevel@tonic-gate 	}
26510Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
26520Sstevel@tonic-gate }
26530Sstevel@tonic-gate 
26540Sstevel@tonic-gate /*
26550Sstevel@tonic-gate  * Return the major number based on the compatible property. This interface
26560Sstevel@tonic-gate  * may be used in situations where we are trying to detect if a better driver
26570Sstevel@tonic-gate  * now exists for a device, so it must use the 'compatible' property.  If
26580Sstevel@tonic-gate  * a non-NULL formp is specified and the binding was based on compatible then
26590Sstevel@tonic-gate  * return the pointer to the form used in *formp.
26600Sstevel@tonic-gate  */
26610Sstevel@tonic-gate major_t
26620Sstevel@tonic-gate ddi_compatible_driver_major(dev_info_t *dip, char **formp)
26630Sstevel@tonic-gate {
26640Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
26650Sstevel@tonic-gate 	void		*compat;
26660Sstevel@tonic-gate 	size_t		len;
26670Sstevel@tonic-gate 	char		*p = NULL;
26687009Scth 	major_t		major = DDI_MAJOR_T_NONE;
26690Sstevel@tonic-gate 
26700Sstevel@tonic-gate 	if (formp)
26710Sstevel@tonic-gate 		*formp = NULL;
26720Sstevel@tonic-gate 
26734145Scth 	/*
26744145Scth 	 * Highest precedence binding is a path-oriented alias. Since this
26754145Scth 	 * requires a 'path', this type of binding occurs via more obtuse
26764145Scth 	 * 'rebind'. The need for a path-oriented alias 'rebind' is detected
26774145Scth 	 * after a successful DDI_CTLOPS_INITCHILD to another driver: this is
26784145Scth 	 * is the first point at which the unit-address (or instance) of the
26794145Scth 	 * last component of the path is available (even though the path is
26804145Scth 	 * bound to the wrong driver at this point).
26814145Scth 	 */
26824145Scth 	if (devi->devi_flags & DEVI_REBIND) {
26834145Scth 		p = devi->devi_rebinding_name;
26844145Scth 		major = ddi_name_to_major(p);
26857009Scth 		if ((major != DDI_MAJOR_T_NONE) &&
26864145Scth 		    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED)) {
26874145Scth 			if (formp)
26884145Scth 				*formp = p;
26894145Scth 			return (major);
26904145Scth 		}
26914145Scth 
26924145Scth 		/*
26934145Scth 		 * If for some reason devi_rebinding_name no longer resolves
26944145Scth 		 * to a proper driver then clear DEVI_REBIND.
26954145Scth 		 */
26964145Scth 		mutex_enter(&devi->devi_lock);
26974145Scth 		devi->devi_flags &= ~DEVI_REBIND;
26984145Scth 		mutex_exit(&devi->devi_lock);
26994145Scth 	}
27004145Scth 
27010Sstevel@tonic-gate 	/* look up compatible property */
27020Sstevel@tonic-gate 	(void) lookup_compatible(dip, KM_SLEEP);
27030Sstevel@tonic-gate 	compat = (void *)(devi->devi_compat_names);
27040Sstevel@tonic-gate 	len = devi->devi_compat_length;
27050Sstevel@tonic-gate 
27060Sstevel@tonic-gate 	/* find the highest precedence compatible form with a driver binding */
27070Sstevel@tonic-gate 	while ((p = prom_decode_composite_string(compat, len, p)) != NULL) {
27080Sstevel@tonic-gate 		major = ddi_name_to_major(p);
27097009Scth 		if ((major != DDI_MAJOR_T_NONE) &&
27100Sstevel@tonic-gate 		    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED)) {
27110Sstevel@tonic-gate 			if (formp)
27120Sstevel@tonic-gate 				*formp = p;
27130Sstevel@tonic-gate 			return (major);
27140Sstevel@tonic-gate 		}
27150Sstevel@tonic-gate 	}
27160Sstevel@tonic-gate 
27170Sstevel@tonic-gate 	/*
27180Sstevel@tonic-gate 	 * none of the compatible forms have a driver binding, see if
27190Sstevel@tonic-gate 	 * the node name has a driver binding.
27200Sstevel@tonic-gate 	 */
27210Sstevel@tonic-gate 	major = ddi_name_to_major(ddi_node_name(dip));
27227009Scth 	if ((major != DDI_MAJOR_T_NONE) &&
27230Sstevel@tonic-gate 	    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED))
27240Sstevel@tonic-gate 		return (major);
27250Sstevel@tonic-gate 
27260Sstevel@tonic-gate 	/* no driver */
27277009Scth 	return (DDI_MAJOR_T_NONE);
27280Sstevel@tonic-gate }
27290Sstevel@tonic-gate 
27300Sstevel@tonic-gate /*
27310Sstevel@tonic-gate  * Static help functions
27320Sstevel@tonic-gate  */
27330Sstevel@tonic-gate 
27340Sstevel@tonic-gate /*
27350Sstevel@tonic-gate  * lookup the "compatible" property and cache it's contents in the
27360Sstevel@tonic-gate  * device node.
27370Sstevel@tonic-gate  */
27380Sstevel@tonic-gate static int
27390Sstevel@tonic-gate lookup_compatible(dev_info_t *dip, uint_t flag)
27400Sstevel@tonic-gate {
27410Sstevel@tonic-gate 	int rv;
27420Sstevel@tonic-gate 	int prop_flags;
27430Sstevel@tonic-gate 	uint_t ncompatstrs;
27440Sstevel@tonic-gate 	char **compatstrpp;
27450Sstevel@tonic-gate 	char *di_compat_strp;
27460Sstevel@tonic-gate 	size_t di_compat_strlen;
27470Sstevel@tonic-gate 
27480Sstevel@tonic-gate 	if (DEVI(dip)->devi_compat_names) {
27490Sstevel@tonic-gate 		return (DDI_SUCCESS);
27500Sstevel@tonic-gate 	}
27510Sstevel@tonic-gate 
27520Sstevel@tonic-gate 	prop_flags = DDI_PROP_TYPE_STRING | DDI_PROP_DONTPASS;
27530Sstevel@tonic-gate 
27540Sstevel@tonic-gate 	if (flag & KM_NOSLEEP) {
27550Sstevel@tonic-gate 		prop_flags |= DDI_PROP_DONTSLEEP;
27560Sstevel@tonic-gate 	}
27570Sstevel@tonic-gate 
27580Sstevel@tonic-gate 	if (ndi_dev_is_prom_node(dip) == 0) {
27590Sstevel@tonic-gate 		prop_flags |= DDI_PROP_NOTPROM;
27600Sstevel@tonic-gate 	}
27610Sstevel@tonic-gate 
27620Sstevel@tonic-gate 	rv = ddi_prop_lookup_common(DDI_DEV_T_ANY, dip, prop_flags,
27630Sstevel@tonic-gate 	    "compatible", &compatstrpp, &ncompatstrs,
27640Sstevel@tonic-gate 	    ddi_prop_fm_decode_strings);
27650Sstevel@tonic-gate 
27660Sstevel@tonic-gate 	if (rv == DDI_PROP_NOT_FOUND) {
27670Sstevel@tonic-gate 		return (DDI_SUCCESS);
27680Sstevel@tonic-gate 	}
27690Sstevel@tonic-gate 
27700Sstevel@tonic-gate 	if (rv != DDI_PROP_SUCCESS) {
27710Sstevel@tonic-gate 		return (DDI_FAILURE);
27720Sstevel@tonic-gate 	}
27730Sstevel@tonic-gate 
27740Sstevel@tonic-gate 	/*
27750Sstevel@tonic-gate 	 * encode the compatible property data in the dev_info node
27760Sstevel@tonic-gate 	 */
27770Sstevel@tonic-gate 	rv = DDI_SUCCESS;
27780Sstevel@tonic-gate 	if (ncompatstrs != 0) {
27790Sstevel@tonic-gate 		di_compat_strp = encode_composite_string(compatstrpp,
27800Sstevel@tonic-gate 		    ncompatstrs, &di_compat_strlen, flag);
27810Sstevel@tonic-gate 		if (di_compat_strp != NULL) {
27820Sstevel@tonic-gate 			DEVI(dip)->devi_compat_names = di_compat_strp;
27830Sstevel@tonic-gate 			DEVI(dip)->devi_compat_length = di_compat_strlen;
27840Sstevel@tonic-gate 		} else {
27850Sstevel@tonic-gate 			rv = DDI_FAILURE;
27860Sstevel@tonic-gate 		}
27870Sstevel@tonic-gate 	}
27880Sstevel@tonic-gate 	ddi_prop_free(compatstrpp);
27890Sstevel@tonic-gate 	return (rv);
27900Sstevel@tonic-gate }
27910Sstevel@tonic-gate 
27920Sstevel@tonic-gate /*
27930Sstevel@tonic-gate  * Create a composite string from a list of strings.
27940Sstevel@tonic-gate  *
27950Sstevel@tonic-gate  * A composite string consists of a single buffer containing one
27960Sstevel@tonic-gate  * or more NULL terminated strings.
27970Sstevel@tonic-gate  */
27980Sstevel@tonic-gate static char *
27990Sstevel@tonic-gate encode_composite_string(char **strings, uint_t nstrings, size_t *retsz,
28000Sstevel@tonic-gate     uint_t flag)
28010Sstevel@tonic-gate {
28020Sstevel@tonic-gate 	uint_t index;
28030Sstevel@tonic-gate 	char  **strpp;
28040Sstevel@tonic-gate 	uint_t slen;
28050Sstevel@tonic-gate 	size_t cbuf_sz = 0;
28060Sstevel@tonic-gate 	char *cbuf_p;
28070Sstevel@tonic-gate 	char *cbuf_ip;
28080Sstevel@tonic-gate 
28090Sstevel@tonic-gate 	if (strings == NULL || nstrings == 0 || retsz == NULL) {
28100Sstevel@tonic-gate 		return (NULL);
28110Sstevel@tonic-gate 	}
28120Sstevel@tonic-gate 
28130Sstevel@tonic-gate 	for (index = 0, strpp = strings; index < nstrings; index++)
28140Sstevel@tonic-gate 		cbuf_sz += strlen(*(strpp++)) + 1;
28150Sstevel@tonic-gate 
28160Sstevel@tonic-gate 	if ((cbuf_p = kmem_alloc(cbuf_sz, flag)) == NULL) {
28170Sstevel@tonic-gate 		cmn_err(CE_NOTE,
28180Sstevel@tonic-gate 		    "?failed to allocate device node compatstr");
28190Sstevel@tonic-gate 		return (NULL);
28200Sstevel@tonic-gate 	}
28210Sstevel@tonic-gate 
28220Sstevel@tonic-gate 	cbuf_ip = cbuf_p;
28230Sstevel@tonic-gate 	for (index = 0, strpp = strings; index < nstrings; index++) {
28240Sstevel@tonic-gate 		slen = strlen(*strpp);
28250Sstevel@tonic-gate 		bcopy(*(strpp++), cbuf_ip, slen);
28260Sstevel@tonic-gate 		cbuf_ip += slen;
28270Sstevel@tonic-gate 		*(cbuf_ip++) = '\0';
28280Sstevel@tonic-gate 	}
28290Sstevel@tonic-gate 
28300Sstevel@tonic-gate 	*retsz = cbuf_sz;
28310Sstevel@tonic-gate 	return (cbuf_p);
28320Sstevel@tonic-gate }
28330Sstevel@tonic-gate 
28340Sstevel@tonic-gate static void
28350Sstevel@tonic-gate link_to_driver_list(dev_info_t *dip)
28360Sstevel@tonic-gate {
28370Sstevel@tonic-gate 	major_t major = DEVI(dip)->devi_major;
28380Sstevel@tonic-gate 	struct devnames *dnp;
28390Sstevel@tonic-gate 
28407009Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
28410Sstevel@tonic-gate 
28420Sstevel@tonic-gate 	/*
28430Sstevel@tonic-gate 	 * Remove from orphan list
28440Sstevel@tonic-gate 	 */
28450Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
28460Sstevel@tonic-gate 		dnp = &orphanlist;
28470Sstevel@tonic-gate 		remove_from_dn_list(dnp, dip);
28480Sstevel@tonic-gate 	}
28490Sstevel@tonic-gate 
28500Sstevel@tonic-gate 	/*
28510Sstevel@tonic-gate 	 * Add to per driver list
28520Sstevel@tonic-gate 	 */
28530Sstevel@tonic-gate 	dnp = &devnamesp[major];
28540Sstevel@tonic-gate 	add_to_dn_list(dnp, dip);
28550Sstevel@tonic-gate }
28560Sstevel@tonic-gate 
28570Sstevel@tonic-gate static void
28580Sstevel@tonic-gate unlink_from_driver_list(dev_info_t *dip)
28590Sstevel@tonic-gate {
28600Sstevel@tonic-gate 	major_t major = DEVI(dip)->devi_major;
28610Sstevel@tonic-gate 	struct devnames *dnp;
28620Sstevel@tonic-gate 
28637009Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
28640Sstevel@tonic-gate 
28650Sstevel@tonic-gate 	/*
28660Sstevel@tonic-gate 	 * Remove from per-driver list
28670Sstevel@tonic-gate 	 */
28680Sstevel@tonic-gate 	dnp = &devnamesp[major];
28690Sstevel@tonic-gate 	remove_from_dn_list(dnp, dip);
28700Sstevel@tonic-gate 
28710Sstevel@tonic-gate 	/*
28720Sstevel@tonic-gate 	 * Add to orphan list
28730Sstevel@tonic-gate 	 */
28740Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
28750Sstevel@tonic-gate 		dnp = &orphanlist;
28760Sstevel@tonic-gate 		add_to_dn_list(dnp, dip);
28770Sstevel@tonic-gate 	}
28780Sstevel@tonic-gate }
28790Sstevel@tonic-gate 
28800Sstevel@tonic-gate /*
28810Sstevel@tonic-gate  * scan the per-driver list looking for dev_info "dip"
28820Sstevel@tonic-gate  */
28830Sstevel@tonic-gate static dev_info_t *
28840Sstevel@tonic-gate in_dn_list(struct devnames *dnp, dev_info_t *dip)
28850Sstevel@tonic-gate {
28860Sstevel@tonic-gate 	struct dev_info *idevi;
28870Sstevel@tonic-gate 
28880Sstevel@tonic-gate 	if ((idevi = DEVI(dnp->dn_head)) == NULL)
28890Sstevel@tonic-gate 		return (NULL);
28900Sstevel@tonic-gate 
28910Sstevel@tonic-gate 	while (idevi) {
28920Sstevel@tonic-gate 		if (idevi == DEVI(dip))
28930Sstevel@tonic-gate 			return (dip);
28940Sstevel@tonic-gate 		idevi = idevi->devi_next;
28950Sstevel@tonic-gate 	}
28960Sstevel@tonic-gate 	return (NULL);
28970Sstevel@tonic-gate }
28980Sstevel@tonic-gate 
28990Sstevel@tonic-gate /*
29000Sstevel@tonic-gate  * insert devinfo node 'dip' into the per-driver instance list
29010Sstevel@tonic-gate  * headed by 'dnp'
29020Sstevel@tonic-gate  *
29030Sstevel@tonic-gate  * Nodes on the per-driver list are ordered: HW - SID - PSEUDO.  The order is
29040Sstevel@tonic-gate  * required for merging of .conf file data to work properly.
29050Sstevel@tonic-gate  */
29060Sstevel@tonic-gate static void
29070Sstevel@tonic-gate add_to_ordered_dn_list(struct devnames *dnp, dev_info_t *dip)
29080Sstevel@tonic-gate {
29090Sstevel@tonic-gate 	dev_info_t **dipp;
29100Sstevel@tonic-gate 
29110Sstevel@tonic-gate 	ASSERT(mutex_owned(&(dnp->dn_lock)));
29120Sstevel@tonic-gate 
29130Sstevel@tonic-gate 	dipp = &dnp->dn_head;
29140Sstevel@tonic-gate 	if (ndi_dev_is_prom_node(dip)) {
29150Sstevel@tonic-gate 		/*
29160Sstevel@tonic-gate 		 * Find the first non-prom node or end of list
29170Sstevel@tonic-gate 		 */
29180Sstevel@tonic-gate 		while (*dipp && (ndi_dev_is_prom_node(*dipp) != 0)) {
29190Sstevel@tonic-gate 			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
29200Sstevel@tonic-gate 		}
29210Sstevel@tonic-gate 	} else if (ndi_dev_is_persistent_node(dip)) {
29220Sstevel@tonic-gate 		/*
29230Sstevel@tonic-gate 		 * Find the first non-persistent node
29240Sstevel@tonic-gate 		 */
29250Sstevel@tonic-gate 		while (*dipp && (ndi_dev_is_persistent_node(*dipp) != 0)) {
29260Sstevel@tonic-gate 			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
29270Sstevel@tonic-gate 		}
29280Sstevel@tonic-gate 	} else {
29290Sstevel@tonic-gate 		/*
29300Sstevel@tonic-gate 		 * Find the end of the list
29310Sstevel@tonic-gate 		 */
29320Sstevel@tonic-gate 		while (*dipp) {
29330Sstevel@tonic-gate 			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
29340Sstevel@tonic-gate 		}
29350Sstevel@tonic-gate 	}
29360Sstevel@tonic-gate 
29370Sstevel@tonic-gate 	DEVI(dip)->devi_next = DEVI(*dipp);
29380Sstevel@tonic-gate 	*dipp = dip;
29390Sstevel@tonic-gate }
29400Sstevel@tonic-gate 
29410Sstevel@tonic-gate /*
29420Sstevel@tonic-gate  * add a list of device nodes to the device node list in the
29430Sstevel@tonic-gate  * devnames structure
29440Sstevel@tonic-gate  */
29450Sstevel@tonic-gate static void
29460Sstevel@tonic-gate add_to_dn_list(struct devnames *dnp, dev_info_t *dip)
29470Sstevel@tonic-gate {
29480Sstevel@tonic-gate 	/*
29490Sstevel@tonic-gate 	 * Look to see if node already exists
29500Sstevel@tonic-gate 	 */
29510Sstevel@tonic-gate 	LOCK_DEV_OPS(&(dnp->dn_lock));
29520Sstevel@tonic-gate 	if (in_dn_list(dnp, dip)) {
29530Sstevel@tonic-gate 		cmn_err(CE_NOTE, "add_to_dn_list: node %s already in list",
29540Sstevel@tonic-gate 		    DEVI(dip)->devi_node_name);
29550Sstevel@tonic-gate 	} else {
29560Sstevel@tonic-gate 		add_to_ordered_dn_list(dnp, dip);
29570Sstevel@tonic-gate 	}
29580Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
29590Sstevel@tonic-gate }
29600Sstevel@tonic-gate 
29610Sstevel@tonic-gate static void
29620Sstevel@tonic-gate remove_from_dn_list(struct devnames *dnp, dev_info_t *dip)
29630Sstevel@tonic-gate {
29640Sstevel@tonic-gate 	dev_info_t **plist;
29650Sstevel@tonic-gate 
29660Sstevel@tonic-gate 	LOCK_DEV_OPS(&(dnp->dn_lock));
29670Sstevel@tonic-gate 
29680Sstevel@tonic-gate 	plist = (dev_info_t **)&dnp->dn_head;
29690Sstevel@tonic-gate 	while (*plist && (*plist != dip)) {
29700Sstevel@tonic-gate 		plist = (dev_info_t **)&DEVI(*plist)->devi_next;
29710Sstevel@tonic-gate 	}
29720Sstevel@tonic-gate 
29730Sstevel@tonic-gate 	if (*plist != NULL) {
29740Sstevel@tonic-gate 		ASSERT(*plist == dip);
29750Sstevel@tonic-gate 		*plist = (dev_info_t *)(DEVI(dip)->devi_next);
29760Sstevel@tonic-gate 		DEVI(dip)->devi_next = NULL;
29770Sstevel@tonic-gate 	} else {
29780Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE,
29790Sstevel@tonic-gate 		    "remove_from_dn_list: node %s not found in list",
29800Sstevel@tonic-gate 		    DEVI(dip)->devi_node_name));
29810Sstevel@tonic-gate 	}
29820Sstevel@tonic-gate 
29830Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
29840Sstevel@tonic-gate }
29850Sstevel@tonic-gate 
29860Sstevel@tonic-gate /*
29870Sstevel@tonic-gate  * Add and remove reference driver global property list
29880Sstevel@tonic-gate  */
29890Sstevel@tonic-gate static void
29900Sstevel@tonic-gate add_global_props(dev_info_t *dip)
29910Sstevel@tonic-gate {
29920Sstevel@tonic-gate 	struct devnames *dnp;
29930Sstevel@tonic-gate 	ddi_prop_list_t *plist;
29940Sstevel@tonic-gate 
29950Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_global_prop_list == NULL);
29967009Scth 	ASSERT(DEVI(dip)->devi_major != DDI_MAJOR_T_NONE);
29970Sstevel@tonic-gate 
29980Sstevel@tonic-gate 	dnp = &devnamesp[DEVI(dip)->devi_major];
29990Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
30000Sstevel@tonic-gate 	plist = dnp->dn_global_prop_ptr;
30010Sstevel@tonic-gate 	if (plist == NULL) {
30020Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
30030Sstevel@tonic-gate 		return;
30040Sstevel@tonic-gate 	}
30050Sstevel@tonic-gate 	i_ddi_prop_list_hold(plist, dnp);
30060Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
30070Sstevel@tonic-gate 
30080Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
30090Sstevel@tonic-gate 	DEVI(dip)->devi_global_prop_list = plist;
30100Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
30110Sstevel@tonic-gate }
30120Sstevel@tonic-gate 
30130Sstevel@tonic-gate static void
30140Sstevel@tonic-gate remove_global_props(dev_info_t *dip)
30150Sstevel@tonic-gate {
30160Sstevel@tonic-gate 	ddi_prop_list_t *proplist;
30170Sstevel@tonic-gate 
30180Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
30190Sstevel@tonic-gate 	proplist = DEVI(dip)->devi_global_prop_list;
30200Sstevel@tonic-gate 	DEVI(dip)->devi_global_prop_list = NULL;
30210Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
30220Sstevel@tonic-gate 
30230Sstevel@tonic-gate 	if (proplist) {
30240Sstevel@tonic-gate 		major_t major;
30250Sstevel@tonic-gate 		struct devnames *dnp;
30260Sstevel@tonic-gate 
30270Sstevel@tonic-gate 		major = ddi_driver_major(dip);
30287009Scth 		ASSERT(major != DDI_MAJOR_T_NONE);
30290Sstevel@tonic-gate 		dnp = &devnamesp[major];
30300Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
30310Sstevel@tonic-gate 		i_ddi_prop_list_rele(proplist, dnp);
30320Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
30330Sstevel@tonic-gate 	}
30340Sstevel@tonic-gate }
30350Sstevel@tonic-gate 
30360Sstevel@tonic-gate #ifdef DEBUG
30370Sstevel@tonic-gate /*
30380Sstevel@tonic-gate  * Set this variable to '0' to disable the optimization,
30390Sstevel@tonic-gate  * and to 2 to print debug message.
30400Sstevel@tonic-gate  */
30410Sstevel@tonic-gate static int optimize_dtree = 1;
30420Sstevel@tonic-gate 
30430Sstevel@tonic-gate static void
30440Sstevel@tonic-gate debug_dtree(dev_info_t *devi, struct dev_info *adevi, char *service)
30450Sstevel@tonic-gate {
30460Sstevel@tonic-gate 	char *adeviname, *buf;
30470Sstevel@tonic-gate 
30480Sstevel@tonic-gate 	/*
30490Sstevel@tonic-gate 	 * Don't print unless optimize dtree is set to 2+
30500Sstevel@tonic-gate 	 */
30510Sstevel@tonic-gate 	if (optimize_dtree <= 1)
30520Sstevel@tonic-gate 		return;
30530Sstevel@tonic-gate 
30540Sstevel@tonic-gate 	buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
30550Sstevel@tonic-gate 	adeviname = ddi_deviname((dev_info_t *)adevi, buf);
30560Sstevel@tonic-gate 	if (*adeviname == '\0')
30570Sstevel@tonic-gate 		adeviname = "root";
30580Sstevel@tonic-gate 
30590Sstevel@tonic-gate 	cmn_err(CE_CONT, "%s %s -> %s\n",
30600Sstevel@tonic-gate 	    ddi_deviname(devi, buf), service, adeviname);
30610Sstevel@tonic-gate 
30620Sstevel@tonic-gate 	kmem_free(buf, MAXNAMELEN);
30630Sstevel@tonic-gate }
30640Sstevel@tonic-gate #else /* DEBUG */
30650Sstevel@tonic-gate #define	debug_dtree(a1, a2, a3)	 /* nothing */
30660Sstevel@tonic-gate #endif  /* DEBUG */
30670Sstevel@tonic-gate 
30680Sstevel@tonic-gate static void
30690Sstevel@tonic-gate ddi_optimize_dtree(dev_info_t *devi)
30700Sstevel@tonic-gate {
30710Sstevel@tonic-gate 	struct dev_info *pdevi;
30720Sstevel@tonic-gate 	struct bus_ops *b;
30730Sstevel@tonic-gate 
30740Sstevel@tonic-gate 	pdevi = DEVI(devi)->devi_parent;
30750Sstevel@tonic-gate 	ASSERT(pdevi);
30760Sstevel@tonic-gate 
30770Sstevel@tonic-gate 	/*
30780Sstevel@tonic-gate 	 * Set the unoptimized values
30790Sstevel@tonic-gate 	 */
30800Sstevel@tonic-gate 	DEVI(devi)->devi_bus_map_fault = pdevi;
30810Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_map = pdevi;
30820Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_allochdl = pdevi;
30830Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_freehdl = pdevi;
30840Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_bindhdl = pdevi;
30850Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_bindfunc =
30864950Scth 	    pdevi->devi_ops->devo_bus_ops->bus_dma_bindhdl;
30870Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_unbindhdl = pdevi;
30880Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_unbindfunc =
30890Sstevel@tonic-gate 	    pdevi->devi_ops->devo_bus_ops->bus_dma_unbindhdl;
30900Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_flush = pdevi;
30910Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_win = pdevi;
30920Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_ctl = pdevi;
30930Sstevel@tonic-gate 	DEVI(devi)->devi_bus_ctl = pdevi;
30940Sstevel@tonic-gate 
30950Sstevel@tonic-gate #ifdef DEBUG
30960Sstevel@tonic-gate 	if (optimize_dtree == 0)
30970Sstevel@tonic-gate 		return;
30980Sstevel@tonic-gate #endif /* DEBUG */
30990Sstevel@tonic-gate 
31000Sstevel@tonic-gate 	b = pdevi->devi_ops->devo_bus_ops;
31010Sstevel@tonic-gate 
31020Sstevel@tonic-gate 	if (i_ddi_map_fault == b->bus_map_fault) {
31030Sstevel@tonic-gate 		DEVI(devi)->devi_bus_map_fault = pdevi->devi_bus_map_fault;
31040Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_map_fault,
31050Sstevel@tonic-gate 		    "bus_map_fault");
31060Sstevel@tonic-gate 	}
31070Sstevel@tonic-gate 
31080Sstevel@tonic-gate 	if (ddi_dma_map == b->bus_dma_map) {
31090Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_map = pdevi->devi_bus_dma_map;
31100Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_map, "bus_dma_map");
31110Sstevel@tonic-gate 	}
31120Sstevel@tonic-gate 
31130Sstevel@tonic-gate 	if (ddi_dma_allochdl == b->bus_dma_allochdl) {
31140Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_allochdl =
31150Sstevel@tonic-gate 		    pdevi->devi_bus_dma_allochdl;
31160Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_allochdl,
31170Sstevel@tonic-gate 		    "bus_dma_allochdl");
31180Sstevel@tonic-gate 	}
31190Sstevel@tonic-gate 
31200Sstevel@tonic-gate 	if (ddi_dma_freehdl == b->bus_dma_freehdl) {
31210Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_freehdl = pdevi->devi_bus_dma_freehdl;
31220Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_freehdl,
31230Sstevel@tonic-gate 		    "bus_dma_freehdl");
31240Sstevel@tonic-gate 	}
31250Sstevel@tonic-gate 
31260Sstevel@tonic-gate 	if (ddi_dma_bindhdl == b->bus_dma_bindhdl) {
31270Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_bindhdl = pdevi->devi_bus_dma_bindhdl;
31280Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_bindfunc =
31290Sstevel@tonic-gate 		    pdevi->devi_bus_dma_bindhdl->devi_ops->
31300Sstevel@tonic-gate 		    devo_bus_ops->bus_dma_bindhdl;
31310Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_bindhdl,
31320Sstevel@tonic-gate 		    "bus_dma_bindhdl");
31330Sstevel@tonic-gate 	}
31340Sstevel@tonic-gate 
31350Sstevel@tonic-gate 	if (ddi_dma_unbindhdl == b->bus_dma_unbindhdl) {
31360Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_unbindhdl =
31370Sstevel@tonic-gate 		    pdevi->devi_bus_dma_unbindhdl;
31380Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_unbindfunc =
31390Sstevel@tonic-gate 		    pdevi->devi_bus_dma_unbindhdl->devi_ops->
31400Sstevel@tonic-gate 		    devo_bus_ops->bus_dma_unbindhdl;
31410Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_unbindhdl,
31420Sstevel@tonic-gate 		    "bus_dma_unbindhdl");
31430Sstevel@tonic-gate 	}
31440Sstevel@tonic-gate 
31450Sstevel@tonic-gate 	if (ddi_dma_flush == b->bus_dma_flush) {
31460Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_flush = pdevi->devi_bus_dma_flush;
31470Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_flush,
31480Sstevel@tonic-gate 		    "bus_dma_flush");
31490Sstevel@tonic-gate 	}
31500Sstevel@tonic-gate 
31510Sstevel@tonic-gate 	if (ddi_dma_win == b->bus_dma_win) {
31520Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_win = pdevi->devi_bus_dma_win;
31530Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_win,
31540Sstevel@tonic-gate 		    "bus_dma_win");
31550Sstevel@tonic-gate 	}
31560Sstevel@tonic-gate 
31570Sstevel@tonic-gate 	if (ddi_dma_mctl == b->bus_dma_ctl) {
31580Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_ctl = pdevi->devi_bus_dma_ctl;
31590Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_ctl, "bus_dma_ctl");
31600Sstevel@tonic-gate 	}
31610Sstevel@tonic-gate 
31620Sstevel@tonic-gate 	if (ddi_ctlops == b->bus_ctl) {
31630Sstevel@tonic-gate 		DEVI(devi)->devi_bus_ctl = pdevi->devi_bus_ctl;
31640Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_ctl, "bus_ctl");
31650Sstevel@tonic-gate 	}
31660Sstevel@tonic-gate }
31670Sstevel@tonic-gate 
31680Sstevel@tonic-gate #define	MIN_DEVINFO_LOG_SIZE	max_ncpus
31690Sstevel@tonic-gate #define	MAX_DEVINFO_LOG_SIZE	max_ncpus * 10
31700Sstevel@tonic-gate 
31710Sstevel@tonic-gate static void
31720Sstevel@tonic-gate da_log_init()
31730Sstevel@tonic-gate {
31740Sstevel@tonic-gate 	devinfo_log_header_t *dh;
31750Sstevel@tonic-gate 	int logsize = devinfo_log_size;
31760Sstevel@tonic-gate 
31770Sstevel@tonic-gate 	if (logsize == 0)
31780Sstevel@tonic-gate 		logsize = MIN_DEVINFO_LOG_SIZE;
31790Sstevel@tonic-gate 	else if (logsize > MAX_DEVINFO_LOG_SIZE)
31800Sstevel@tonic-gate 		logsize = MAX_DEVINFO_LOG_SIZE;
31810Sstevel@tonic-gate 
31820Sstevel@tonic-gate 	dh = kmem_alloc(logsize * PAGESIZE, KM_SLEEP);
31830Sstevel@tonic-gate 	mutex_init(&dh->dh_lock, NULL, MUTEX_DEFAULT, NULL);
31840Sstevel@tonic-gate 	dh->dh_max = ((logsize * PAGESIZE) - sizeof (*dh)) /
31850Sstevel@tonic-gate 	    sizeof (devinfo_audit_t) + 1;
31860Sstevel@tonic-gate 	dh->dh_curr = -1;
31870Sstevel@tonic-gate 	dh->dh_hits = 0;
31880Sstevel@tonic-gate 
31890Sstevel@tonic-gate 	devinfo_audit_log = dh;
31900Sstevel@tonic-gate }
31910Sstevel@tonic-gate 
31920Sstevel@tonic-gate /*
31930Sstevel@tonic-gate  * Log the stack trace in per-devinfo audit structure and also enter
31940Sstevel@tonic-gate  * it into a system wide log for recording the time history.
31950Sstevel@tonic-gate  */
31960Sstevel@tonic-gate static void
31970Sstevel@tonic-gate da_log_enter(dev_info_t *dip)
31980Sstevel@tonic-gate {
31990Sstevel@tonic-gate 	devinfo_audit_t *da_log, *da = DEVI(dip)->devi_audit;
32000Sstevel@tonic-gate 	devinfo_log_header_t *dh = devinfo_audit_log;
32010Sstevel@tonic-gate 
32020Sstevel@tonic-gate 	if (devinfo_audit_log == NULL)
32030Sstevel@tonic-gate 		return;
32040Sstevel@tonic-gate 
32050Sstevel@tonic-gate 	ASSERT(da != NULL);
32060Sstevel@tonic-gate 
32070Sstevel@tonic-gate 	da->da_devinfo = dip;
32080Sstevel@tonic-gate 	da->da_timestamp = gethrtime();
32090Sstevel@tonic-gate 	da->da_thread = curthread;
32100Sstevel@tonic-gate 	da->da_node_state = DEVI(dip)->devi_node_state;
32110Sstevel@tonic-gate 	da->da_device_state = DEVI(dip)->devi_state;
32120Sstevel@tonic-gate 	da->da_depth = getpcstack(da->da_stack, DDI_STACK_DEPTH);
32130Sstevel@tonic-gate 
32140Sstevel@tonic-gate 	/*
32150Sstevel@tonic-gate 	 * Copy into common log and note the location for tracing history
32160Sstevel@tonic-gate 	 */
32170Sstevel@tonic-gate 	mutex_enter(&dh->dh_lock);
32180Sstevel@tonic-gate 	dh->dh_hits++;
32190Sstevel@tonic-gate 	dh->dh_curr++;
32200Sstevel@tonic-gate 	if (dh->dh_curr >= dh->dh_max)
32210Sstevel@tonic-gate 		dh->dh_curr -= dh->dh_max;
32220Sstevel@tonic-gate 	da_log = &dh->dh_entry[dh->dh_curr];
32230Sstevel@tonic-gate 	mutex_exit(&dh->dh_lock);
32240Sstevel@tonic-gate 
32250Sstevel@tonic-gate 	bcopy(da, da_log, sizeof (devinfo_audit_t));
32260Sstevel@tonic-gate 	da->da_lastlog = da_log;
32270Sstevel@tonic-gate }
32280Sstevel@tonic-gate 
32290Sstevel@tonic-gate static void
32300Sstevel@tonic-gate attach_drivers()
32310Sstevel@tonic-gate {
32320Sstevel@tonic-gate 	int i;
32330Sstevel@tonic-gate 	for (i = 0; i < devcnt; i++) {
32340Sstevel@tonic-gate 		struct devnames *dnp = &devnamesp[i];
32350Sstevel@tonic-gate 		if ((dnp->dn_flags & DN_FORCE_ATTACH) &&
32360Sstevel@tonic-gate 		    (ddi_hold_installed_driver((major_t)i) != NULL))
32370Sstevel@tonic-gate 			ddi_rele_driver((major_t)i);
32380Sstevel@tonic-gate 	}
32390Sstevel@tonic-gate }
32400Sstevel@tonic-gate 
32410Sstevel@tonic-gate /*
32420Sstevel@tonic-gate  * Launch a thread to force attach drivers. This avoids penalty on boot time.
32430Sstevel@tonic-gate  */
32440Sstevel@tonic-gate void
32450Sstevel@tonic-gate i_ddi_forceattach_drivers()
32460Sstevel@tonic-gate {
32470Sstevel@tonic-gate 	/*
32480Sstevel@tonic-gate 	 * On i386, the USB drivers need to load and take over from the
32490Sstevel@tonic-gate 	 * SMM BIOS drivers ASAP after consconfig(), so make sure they
32500Sstevel@tonic-gate 	 * get loaded right here rather than letting the thread do it.
32510Sstevel@tonic-gate 	 *
32520Sstevel@tonic-gate 	 * The order here is important.  EHCI must be loaded first, as
32530Sstevel@tonic-gate 	 * we have observed many systems on which hangs occur if the
32540Sstevel@tonic-gate 	 * {U,O}HCI companion controllers take over control from the BIOS
32550Sstevel@tonic-gate 	 * before EHCI does.  These hangs are also caused by BIOSes leaving
32560Sstevel@tonic-gate 	 * interrupt-on-port-change enabled in the ehci controller, so that
32570Sstevel@tonic-gate 	 * when uhci/ohci reset themselves, it induces a port change on
32580Sstevel@tonic-gate 	 * the ehci companion controller.  Since there's no interrupt handler
32590Sstevel@tonic-gate 	 * installed at the time, the moment that interrupt is unmasked, an
32600Sstevel@tonic-gate 	 * interrupt storm will occur.  All this is averted when ehci is
32610Sstevel@tonic-gate 	 * loaded first.  And now you know..... the REST of the story.
32620Sstevel@tonic-gate 	 *
32630Sstevel@tonic-gate 	 * Regardless of platform, ehci needs to initialize first to avoid
32640Sstevel@tonic-gate 	 * unnecessary connects and disconnects on the companion controller
32650Sstevel@tonic-gate 	 * when ehci sets up the routing.
32660Sstevel@tonic-gate 	 */
32670Sstevel@tonic-gate 	(void) ddi_hold_installed_driver(ddi_name_to_major("ehci"));
32680Sstevel@tonic-gate 	(void) ddi_hold_installed_driver(ddi_name_to_major("uhci"));
32690Sstevel@tonic-gate 	(void) ddi_hold_installed_driver(ddi_name_to_major("ohci"));
32700Sstevel@tonic-gate 
32711093Shiremath 	/*
32721093Shiremath 	 * Attach IB VHCI driver before the force-attach thread attaches the
32731093Shiremath 	 * IB HCA driver. IB HCA driver will fail if IB Nexus has not yet
32741093Shiremath 	 * been attached.
32751093Shiremath 	 */
32761093Shiremath 	(void) ddi_hold_installed_driver(ddi_name_to_major("ib"));
32771093Shiremath 
32780Sstevel@tonic-gate 	(void) thread_create(NULL, 0, (void (*)())attach_drivers, NULL, 0, &p0,
32790Sstevel@tonic-gate 	    TS_RUN, minclsyspri);
32800Sstevel@tonic-gate }
32810Sstevel@tonic-gate 
32820Sstevel@tonic-gate /*
32830Sstevel@tonic-gate  * This is a private DDI interface for optimizing boot performance.
32840Sstevel@tonic-gate  * I/O subsystem initialization is considered complete when devfsadm
32850Sstevel@tonic-gate  * is executed.
32860Sstevel@tonic-gate  *
32872621Sllai1  * NOTE: The start of syseventd happens to be a convenient indicator
32882621Sllai1  *	of the completion of I/O initialization during boot.
32890Sstevel@tonic-gate  *	The implementation should be replaced by something more robust.
32900Sstevel@tonic-gate  */
32910Sstevel@tonic-gate int
32920Sstevel@tonic-gate i_ddi_io_initialized()
32930Sstevel@tonic-gate {
32940Sstevel@tonic-gate 	extern int sysevent_daemon_init;
32950Sstevel@tonic-gate 	return (sysevent_daemon_init);
32960Sstevel@tonic-gate }
32970Sstevel@tonic-gate 
32982621Sllai1 /*
32992621Sllai1  * May be used to determine system boot state
33002621Sllai1  * "Available" means the system is for the most part up
33012621Sllai1  * and initialized, with all system services either up or
33022621Sllai1  * capable of being started.  This state is set by devfsadm
33032621Sllai1  * during the boot process.  The /dev filesystem infers
33042621Sllai1  * from this when implicit reconfig can be performed,
33052621Sllai1  * ie, devfsadm can be invoked.  Please avoid making
33062621Sllai1  * further use of this unless it's really necessary.
33072621Sllai1  */
33082621Sllai1 int
33092621Sllai1 i_ddi_sysavail()
33102621Sllai1 {
33112621Sllai1 	return (devname_state & DS_SYSAVAIL);
33122621Sllai1 }
33132621Sllai1 
33142621Sllai1 /*
33152621Sllai1  * May be used to determine if boot is a reconfigure boot.
33162621Sllai1  */
33172621Sllai1 int
33182621Sllai1 i_ddi_reconfig()
33192621Sllai1 {
33202621Sllai1 	return (devname_state & DS_RECONFIG);
33212621Sllai1 }
33222621Sllai1 
33232621Sllai1 /*
33242621Sllai1  * Note system services are up, inform /dev.
33252621Sllai1  */
33262621Sllai1 void
33272621Sllai1 i_ddi_set_sysavail()
33282621Sllai1 {
33292621Sllai1 	if ((devname_state & DS_SYSAVAIL) == 0) {
33302621Sllai1 		devname_state |= DS_SYSAVAIL;
33312621Sllai1 		sdev_devstate_change();
33322621Sllai1 	}
33332621Sllai1 }
33342621Sllai1 
33352621Sllai1 /*
33362621Sllai1  * Note reconfiguration boot, inform /dev.
33372621Sllai1  */
33382621Sllai1 void
33392621Sllai1 i_ddi_set_reconfig()
33402621Sllai1 {
33412621Sllai1 	if ((devname_state & DS_RECONFIG) == 0) {
33422621Sllai1 		devname_state |= DS_RECONFIG;
33432621Sllai1 		sdev_devstate_change();
33442621Sllai1 	}
33452621Sllai1 }
33462621Sllai1 
33470Sstevel@tonic-gate 
33480Sstevel@tonic-gate /*
33490Sstevel@tonic-gate  * device tree walking
33500Sstevel@tonic-gate  */
33510Sstevel@tonic-gate 
33520Sstevel@tonic-gate struct walk_elem {
33530Sstevel@tonic-gate 	struct walk_elem *next;
33540Sstevel@tonic-gate 	dev_info_t *dip;
33550Sstevel@tonic-gate };
33560Sstevel@tonic-gate 
33570Sstevel@tonic-gate static void
33580Sstevel@tonic-gate free_list(struct walk_elem *list)
33590Sstevel@tonic-gate {
33600Sstevel@tonic-gate 	while (list) {
33610Sstevel@tonic-gate 		struct walk_elem *next = list->next;
33620Sstevel@tonic-gate 		kmem_free(list, sizeof (*list));
33630Sstevel@tonic-gate 		list = next;
33640Sstevel@tonic-gate 	}
33650Sstevel@tonic-gate }
33660Sstevel@tonic-gate 
33670Sstevel@tonic-gate static void
33680Sstevel@tonic-gate append_node(struct walk_elem **list, dev_info_t *dip)
33690Sstevel@tonic-gate {
33700Sstevel@tonic-gate 	struct walk_elem *tail;
33710Sstevel@tonic-gate 	struct walk_elem *elem = kmem_alloc(sizeof (*elem), KM_SLEEP);
33720Sstevel@tonic-gate 
33730Sstevel@tonic-gate 	elem->next = NULL;
33740Sstevel@tonic-gate 	elem->dip = dip;
33750Sstevel@tonic-gate 
33760Sstevel@tonic-gate 	if (*list == NULL) {
33770Sstevel@tonic-gate 		*list = elem;
33780Sstevel@tonic-gate 		return;
33790Sstevel@tonic-gate 	}
33800Sstevel@tonic-gate 
33810Sstevel@tonic-gate 	tail = *list;
33820Sstevel@tonic-gate 	while (tail->next)
33830Sstevel@tonic-gate 		tail = tail->next;
33840Sstevel@tonic-gate 
33850Sstevel@tonic-gate 	tail->next = elem;
33860Sstevel@tonic-gate }
33870Sstevel@tonic-gate 
33880Sstevel@tonic-gate /*
33890Sstevel@tonic-gate  * The implementation of ddi_walk_devs().
33900Sstevel@tonic-gate  */
33910Sstevel@tonic-gate static int
33920Sstevel@tonic-gate walk_devs(dev_info_t *dip, int (*f)(dev_info_t *, void *), void *arg,
33930Sstevel@tonic-gate     int do_locking)
33940Sstevel@tonic-gate {
33950Sstevel@tonic-gate 	struct walk_elem *head = NULL;
33960Sstevel@tonic-gate 
33970Sstevel@tonic-gate 	/*
33980Sstevel@tonic-gate 	 * Do it in two passes. First pass invoke callback on each
33990Sstevel@tonic-gate 	 * dip on the sibling list. Second pass invoke callback on
34000Sstevel@tonic-gate 	 * children of each dip.
34010Sstevel@tonic-gate 	 */
34020Sstevel@tonic-gate 	while (dip) {
34030Sstevel@tonic-gate 		switch ((*f)(dip, arg)) {
34040Sstevel@tonic-gate 		case DDI_WALK_TERMINATE:
34050Sstevel@tonic-gate 			free_list(head);
34060Sstevel@tonic-gate 			return (DDI_WALK_TERMINATE);
34070Sstevel@tonic-gate 
34080Sstevel@tonic-gate 		case DDI_WALK_PRUNESIB:
34090Sstevel@tonic-gate 			/* ignore sibling by setting dip to NULL */
34100Sstevel@tonic-gate 			append_node(&head, dip);
34110Sstevel@tonic-gate 			dip = NULL;
34120Sstevel@tonic-gate 			break;
34130Sstevel@tonic-gate 
34140Sstevel@tonic-gate 		case DDI_WALK_PRUNECHILD:
34150Sstevel@tonic-gate 			/* don't worry about children */
34160Sstevel@tonic-gate 			dip = ddi_get_next_sibling(dip);
34170Sstevel@tonic-gate 			break;
34180Sstevel@tonic-gate 
34190Sstevel@tonic-gate 		case DDI_WALK_CONTINUE:
34200Sstevel@tonic-gate 		default:
34210Sstevel@tonic-gate 			append_node(&head, dip);
34220Sstevel@tonic-gate 			dip = ddi_get_next_sibling(dip);
34230Sstevel@tonic-gate 			break;
34240Sstevel@tonic-gate 		}
34250Sstevel@tonic-gate 
34260Sstevel@tonic-gate 	}
34270Sstevel@tonic-gate 
34280Sstevel@tonic-gate 	/* second pass */
34290Sstevel@tonic-gate 	while (head) {
34300Sstevel@tonic-gate 		int circ;
34310Sstevel@tonic-gate 		struct walk_elem *next = head->next;
34320Sstevel@tonic-gate 
34330Sstevel@tonic-gate 		if (do_locking)
34340Sstevel@tonic-gate 			ndi_devi_enter(head->dip, &circ);
34350Sstevel@tonic-gate 		if (walk_devs(ddi_get_child(head->dip), f, arg, do_locking) ==
34360Sstevel@tonic-gate 		    DDI_WALK_TERMINATE) {
34370Sstevel@tonic-gate 			if (do_locking)
34380Sstevel@tonic-gate 				ndi_devi_exit(head->dip, circ);
34390Sstevel@tonic-gate 			free_list(head);
34400Sstevel@tonic-gate 			return (DDI_WALK_TERMINATE);
34410Sstevel@tonic-gate 		}
34420Sstevel@tonic-gate 		if (do_locking)
34430Sstevel@tonic-gate 			ndi_devi_exit(head->dip, circ);
34440Sstevel@tonic-gate 		kmem_free(head, sizeof (*head));
34450Sstevel@tonic-gate 		head = next;
34460Sstevel@tonic-gate 	}
34470Sstevel@tonic-gate 
34480Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
34490Sstevel@tonic-gate }
34500Sstevel@tonic-gate 
34510Sstevel@tonic-gate /*
34520Sstevel@tonic-gate  * This general-purpose routine traverses the tree of dev_info nodes,
34530Sstevel@tonic-gate  * starting from the given node, and calls the given function for each
34540Sstevel@tonic-gate  * node that it finds with the current node and the pointer arg (which
34550Sstevel@tonic-gate  * can point to a structure of information that the function
34560Sstevel@tonic-gate  * needs) as arguments.
34570Sstevel@tonic-gate  *
34580Sstevel@tonic-gate  * It does the walk a layer at a time, not depth-first. The given function
34590Sstevel@tonic-gate  * must return one of the following values:
34600Sstevel@tonic-gate  *	DDI_WALK_CONTINUE
34610Sstevel@tonic-gate  *	DDI_WALK_PRUNESIB
34620Sstevel@tonic-gate  *	DDI_WALK_PRUNECHILD
34630Sstevel@tonic-gate  *	DDI_WALK_TERMINATE
34640Sstevel@tonic-gate  *
34650Sstevel@tonic-gate  * N.B. Since we walk the sibling list, the caller must ensure that
34660Sstevel@tonic-gate  *	the parent of dip is held against changes, unless the parent
34670Sstevel@tonic-gate  *	is rootnode.  ndi_devi_enter() on the parent is sufficient.
34680Sstevel@tonic-gate  *
34690Sstevel@tonic-gate  *	To avoid deadlock situations, caller must not attempt to
34700Sstevel@tonic-gate  *	configure/unconfigure/remove device node in (*f)(), nor should
34712155Scth  *	it attempt to recurse on other nodes in the system. Any
34722155Scth  *	ndi_devi_enter() done by (*f)() must occur 'at-or-below' the
34732155Scth  *	node entered prior to ddi_walk_devs(). Furthermore, if (*f)()
34742155Scth  *	does any multi-threading (in framework *or* in driver) then the
34752155Scth  *	ndi_devi_enter() calls done by dependent threads must be
34762155Scth  *	'strictly-below'.
34770Sstevel@tonic-gate  *
34780Sstevel@tonic-gate  *	This is not callable from device autoconfiguration routines.
34790Sstevel@tonic-gate  *	They include, but not limited to, _init(9e), _fini(9e), probe(9e),
34800Sstevel@tonic-gate  *	attach(9e), and detach(9e).
34810Sstevel@tonic-gate  */
34820Sstevel@tonic-gate 
34830Sstevel@tonic-gate void
34840Sstevel@tonic-gate ddi_walk_devs(dev_info_t *dip, int (*f)(dev_info_t *, void *), void *arg)
34850Sstevel@tonic-gate {
34860Sstevel@tonic-gate 
34870Sstevel@tonic-gate 	ASSERT(dip == NULL || ddi_get_parent(dip) == NULL ||
34884950Scth 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
34890Sstevel@tonic-gate 
34900Sstevel@tonic-gate 	(void) walk_devs(dip, f, arg, 1);
34910Sstevel@tonic-gate }
34920Sstevel@tonic-gate 
34930Sstevel@tonic-gate /*
34940Sstevel@tonic-gate  * This is a general-purpose routine traverses the per-driver list
34950Sstevel@tonic-gate  * and calls the given function for each node. must return one of
34960Sstevel@tonic-gate  * the following values:
34970Sstevel@tonic-gate  *	DDI_WALK_CONTINUE
34980Sstevel@tonic-gate  *	DDI_WALK_TERMINATE
34990Sstevel@tonic-gate  *
35000Sstevel@tonic-gate  * N.B. The same restrictions from ddi_walk_devs() apply.
35010Sstevel@tonic-gate  */
35020Sstevel@tonic-gate 
35030Sstevel@tonic-gate void
35040Sstevel@tonic-gate e_ddi_walk_driver(char *drv, int (*f)(dev_info_t *, void *), void *arg)
35050Sstevel@tonic-gate {
35060Sstevel@tonic-gate 	major_t major;
35070Sstevel@tonic-gate 	struct devnames *dnp;
35080Sstevel@tonic-gate 	dev_info_t *dip;
35090Sstevel@tonic-gate 
35100Sstevel@tonic-gate 	major = ddi_name_to_major(drv);
35117009Scth 	if (major == DDI_MAJOR_T_NONE)
35120Sstevel@tonic-gate 		return;
35130Sstevel@tonic-gate 
35140Sstevel@tonic-gate 	dnp = &devnamesp[major];
35150Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
35160Sstevel@tonic-gate 	dip = dnp->dn_head;
35170Sstevel@tonic-gate 	while (dip) {
35180Sstevel@tonic-gate 		ndi_hold_devi(dip);
35190Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
35200Sstevel@tonic-gate 		if ((*f)(dip, arg) == DDI_WALK_TERMINATE) {
35210Sstevel@tonic-gate 			ndi_rele_devi(dip);
35220Sstevel@tonic-gate 			return;
35230Sstevel@tonic-gate 		}
35240Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
35250Sstevel@tonic-gate 		ndi_rele_devi(dip);
35260Sstevel@tonic-gate 		dip = ddi_get_next(dip);
35270Sstevel@tonic-gate 	}
35280Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
35290Sstevel@tonic-gate }
35300Sstevel@tonic-gate 
35310Sstevel@tonic-gate /*
35320Sstevel@tonic-gate  * argument to i_find_devi, a devinfo node search callback function.
35330Sstevel@tonic-gate  */
35340Sstevel@tonic-gate struct match_info {
35350Sstevel@tonic-gate 	dev_info_t	*dip;		/* result */
35360Sstevel@tonic-gate 	char		*nodename;	/* if non-null, nodename must match */
35370Sstevel@tonic-gate 	int		instance;	/* if != -1, instance must match */
35381333Scth 	int		attached;	/* if != 0, i_ddi_devi_attached() */
35390Sstevel@tonic-gate };
35400Sstevel@tonic-gate 
35410Sstevel@tonic-gate static int
35420Sstevel@tonic-gate i_find_devi(dev_info_t *dip, void *arg)
35430Sstevel@tonic-gate {
35440Sstevel@tonic-gate 	struct match_info *info = (struct match_info *)arg;
35450Sstevel@tonic-gate 
35460Sstevel@tonic-gate 	if (((info->nodename == NULL) ||
35474950Scth 	    (strcmp(ddi_node_name(dip), info->nodename) == 0)) &&
35480Sstevel@tonic-gate 	    ((info->instance == -1) ||
35494950Scth 	    (ddi_get_instance(dip) == info->instance)) &&
35501333Scth 	    ((info->attached == 0) || i_ddi_devi_attached(dip))) {
35510Sstevel@tonic-gate 		info->dip = dip;
35520Sstevel@tonic-gate 		ndi_hold_devi(dip);
35530Sstevel@tonic-gate 		return (DDI_WALK_TERMINATE);
35540Sstevel@tonic-gate 	}
35550Sstevel@tonic-gate 
35560Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
35570Sstevel@tonic-gate }
35580Sstevel@tonic-gate 
35590Sstevel@tonic-gate /*
35600Sstevel@tonic-gate  * Find dip with a known node name and instance and return with it held
35610Sstevel@tonic-gate  */
35620Sstevel@tonic-gate dev_info_t *
35630Sstevel@tonic-gate ddi_find_devinfo(char *nodename, int instance, int attached)
35640Sstevel@tonic-gate {
35650Sstevel@tonic-gate 	struct match_info	info;
35660Sstevel@tonic-gate 
35670Sstevel@tonic-gate 	info.nodename = nodename;
35680Sstevel@tonic-gate 	info.instance = instance;
35690Sstevel@tonic-gate 	info.attached = attached;
35700Sstevel@tonic-gate 	info.dip = NULL;
35710Sstevel@tonic-gate 
35720Sstevel@tonic-gate 	ddi_walk_devs(ddi_root_node(), i_find_devi, &info);
35730Sstevel@tonic-gate 	return (info.dip);
35740Sstevel@tonic-gate }
35750Sstevel@tonic-gate 
35760Sstevel@tonic-gate /*
35770Sstevel@tonic-gate  * Parse for name, addr, and minor names. Some args may be NULL.
35780Sstevel@tonic-gate  */
35790Sstevel@tonic-gate void
35800Sstevel@tonic-gate i_ddi_parse_name(char *name, char **nodename, char **addrname, char **minorname)
35810Sstevel@tonic-gate {
35820Sstevel@tonic-gate 	char *cp;
35830Sstevel@tonic-gate 	static char nulladdrname[] = "";
35840Sstevel@tonic-gate 
35850Sstevel@tonic-gate 	/* default values */
35860Sstevel@tonic-gate 	if (nodename)
35870Sstevel@tonic-gate 		*nodename = name;
35880Sstevel@tonic-gate 	if (addrname)
35890Sstevel@tonic-gate 		*addrname = nulladdrname;
35900Sstevel@tonic-gate 	if (minorname)
35910Sstevel@tonic-gate 		*minorname = NULL;
35920Sstevel@tonic-gate 
35930Sstevel@tonic-gate 	cp = name;
35940Sstevel@tonic-gate 	while (*cp != '\0') {
35950Sstevel@tonic-gate 		if (addrname && *cp == '@') {
35960Sstevel@tonic-gate 			*addrname = cp + 1;
35970Sstevel@tonic-gate 			*cp = '\0';
35980Sstevel@tonic-gate 		} else if (minorname && *cp == ':') {
35990Sstevel@tonic-gate 			*minorname = cp + 1;
36000Sstevel@tonic-gate 			*cp = '\0';
36010Sstevel@tonic-gate 		}
36020Sstevel@tonic-gate 		++cp;
36030Sstevel@tonic-gate 	}
36040Sstevel@tonic-gate }
36050Sstevel@tonic-gate 
36060Sstevel@tonic-gate static char *
36070Sstevel@tonic-gate child_path_to_driver(dev_info_t *parent, char *child_name, char *unit_address)
36080Sstevel@tonic-gate {
36090Sstevel@tonic-gate 	char *p, *drvname = NULL;
36100Sstevel@tonic-gate 	major_t maj;
36110Sstevel@tonic-gate 
36120Sstevel@tonic-gate 	/*
36130Sstevel@tonic-gate 	 * Construct the pathname and ask the implementation
36140Sstevel@tonic-gate 	 * if it can do a driver = f(pathname) for us, if not
36150Sstevel@tonic-gate 	 * we'll just default to using the node-name that
36160Sstevel@tonic-gate 	 * was given to us.  We want to do this first to
36170Sstevel@tonic-gate 	 * allow the platform to use 'generic' names for
36180Sstevel@tonic-gate 	 * legacy device drivers.
36190Sstevel@tonic-gate 	 */
36200Sstevel@tonic-gate 	p = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
36210Sstevel@tonic-gate 	(void) ddi_pathname(parent, p);
36220Sstevel@tonic-gate 	(void) strcat(p, "/");
36230Sstevel@tonic-gate 	(void) strcat(p, child_name);
36240Sstevel@tonic-gate 	if (unit_address && *unit_address) {
36250Sstevel@tonic-gate 		(void) strcat(p, "@");
36260Sstevel@tonic-gate 		(void) strcat(p, unit_address);
36270Sstevel@tonic-gate 	}
36280Sstevel@tonic-gate 
36290Sstevel@tonic-gate 	/*
36300Sstevel@tonic-gate 	 * Get the binding. If there is none, return the child_name
36310Sstevel@tonic-gate 	 * and let the caller deal with it.
36320Sstevel@tonic-gate 	 */
36330Sstevel@tonic-gate 	maj = path_to_major(p);
36340Sstevel@tonic-gate 
36350Sstevel@tonic-gate 	kmem_free(p, MAXPATHLEN);
36360Sstevel@tonic-gate 
36377009Scth 	if (maj != DDI_MAJOR_T_NONE)
36380Sstevel@tonic-gate 		drvname = ddi_major_to_name(maj);
36390Sstevel@tonic-gate 	if (drvname == NULL)
36400Sstevel@tonic-gate 		drvname = child_name;
36410Sstevel@tonic-gate 
36420Sstevel@tonic-gate 	return (drvname);
36430Sstevel@tonic-gate }
36440Sstevel@tonic-gate 
36450Sstevel@tonic-gate 
36467613SVikram.Hegde@Sun.COM #define	PCI_EX_CLASS	"pciexclass"
36477613SVikram.Hegde@Sun.COM #define	PCI_EX		"pciex"
36487613SVikram.Hegde@Sun.COM #define	PCI_CLASS	"pciclass"
36497613SVikram.Hegde@Sun.COM #define	PCI		"pci"
36507613SVikram.Hegde@Sun.COM 
36517613SVikram.Hegde@Sun.COM int
36527613SVikram.Hegde@Sun.COM ddi_is_pci_dip(dev_info_t *dip)
36537613SVikram.Hegde@Sun.COM {
36547613SVikram.Hegde@Sun.COM 	char	*prop = NULL;
36557613SVikram.Hegde@Sun.COM 
36567613SVikram.Hegde@Sun.COM 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
36577613SVikram.Hegde@Sun.COM 	    "compatible", &prop) == DDI_PROP_SUCCESS) {
36587613SVikram.Hegde@Sun.COM 		ASSERT(prop);
36597613SVikram.Hegde@Sun.COM 		if (strncmp(prop, PCI_EX_CLASS, sizeof (PCI_EX_CLASS) - 1)
36607613SVikram.Hegde@Sun.COM 		    == 0 ||
36617613SVikram.Hegde@Sun.COM 		    strncmp(prop, PCI_EX, sizeof (PCI_EX)- 1)
36627613SVikram.Hegde@Sun.COM 		    == 0 ||
36637613SVikram.Hegde@Sun.COM 		    strncmp(prop, PCI_CLASS, sizeof (PCI_CLASS) - 1)
36647613SVikram.Hegde@Sun.COM 		    == 0 ||
36657613SVikram.Hegde@Sun.COM 		    strncmp(prop, PCI, sizeof (PCI) - 1)
36667613SVikram.Hegde@Sun.COM 		    == 0) {
36677613SVikram.Hegde@Sun.COM 			ddi_prop_free(prop);
36687613SVikram.Hegde@Sun.COM 			return (1);
36697613SVikram.Hegde@Sun.COM 		}
36707613SVikram.Hegde@Sun.COM 	}
36717613SVikram.Hegde@Sun.COM 
36727613SVikram.Hegde@Sun.COM 	if (prop != NULL) {
36737613SVikram.Hegde@Sun.COM 		ddi_prop_free(prop);
36747613SVikram.Hegde@Sun.COM 	}
36757613SVikram.Hegde@Sun.COM 
36767613SVikram.Hegde@Sun.COM 	return (0);
36777613SVikram.Hegde@Sun.COM }
36787613SVikram.Hegde@Sun.COM 
36790Sstevel@tonic-gate /*
36800Sstevel@tonic-gate  * Given the pathname of a device, fill in the dev_info_t value and/or the
36810Sstevel@tonic-gate  * dev_t value and/or the spectype, depending on which parameters are non-NULL.
36820Sstevel@tonic-gate  * If there is an error, this function returns -1.
36830Sstevel@tonic-gate  *
36840Sstevel@tonic-gate  * NOTE: If this function returns the dev_info_t structure, then it
36850Sstevel@tonic-gate  * does so with a hold on the devi. Caller should ensure that they get
36860Sstevel@tonic-gate  * decremented via ddi_release_devi() or ndi_rele_devi();
36870Sstevel@tonic-gate  *
36880Sstevel@tonic-gate  * This function can be invoked in the boot case for a pathname without
36890Sstevel@tonic-gate  * device argument (:xxxx), traditionally treated as a minor name.
36900Sstevel@tonic-gate  * In this case, we do the following
36910Sstevel@tonic-gate  * (1) search the minor node of type DDM_DEFAULT.
36920Sstevel@tonic-gate  * (2) if no DDM_DEFAULT minor exists, then the first non-alias minor is chosen.
36930Sstevel@tonic-gate  * (3) if neither exists, a dev_t is faked with minor number = instance.
36940Sstevel@tonic-gate  * As of S9 FCS, no instance of #1 exists. #2 is used by several platforms
36950Sstevel@tonic-gate  * to default the boot partition to :a possibly by other OBP definitions.
36960Sstevel@tonic-gate  * #3 is used for booting off network interfaces, most SPARC network
36970Sstevel@tonic-gate  * drivers support Style-2 only, so only DDM_ALIAS minor exists.
36980Sstevel@tonic-gate  *
36990Sstevel@tonic-gate  * It is possible for OBP to present device args at the end of the path as
37000Sstevel@tonic-gate  * well as in the middle. For example, with IB the following strings are
37010Sstevel@tonic-gate  * valid boot paths.
37020Sstevel@tonic-gate  *	a /pci@8,700000/ib@1,2:port=1,pkey=ff,dhcp,...
37030Sstevel@tonic-gate  *	b /pci@8,700000/ib@1,1:port=1/ioc@xxxxxx,yyyyyyy:dhcp
37040Sstevel@tonic-gate  * Case (a), we first look for minor node "port=1,pkey...".
37050Sstevel@tonic-gate  * Failing that, we will pass "port=1,pkey..." to the bus_config
37060Sstevel@tonic-gate  * entry point of ib (HCA) driver.
37070Sstevel@tonic-gate  * Case (b), configure ib@1,1 as usual. Then invoke ib's bus_config
37080Sstevel@tonic-gate  * with argument "ioc@xxxxxxx,yyyyyyy:port=1". After configuring
37090Sstevel@tonic-gate  * the ioc, look for minor node dhcp. If not found, pass ":dhcp"
37100Sstevel@tonic-gate  * to ioc's bus_config entry point.
37110Sstevel@tonic-gate  */
37127613SVikram.Hegde@Sun.COM static int
37137613SVikram.Hegde@Sun.COM parse_pathname(char *pathname,
37147613SVikram.Hegde@Sun.COM 	dev_info_t **dipp, dev_t *devtp, int *spectypep, dev_info_t **pci_dipp)
37150Sstevel@tonic-gate {
37167224Scth 	int			error;
37177224Scth 	dev_info_t		*parent, *child;
37187224Scth 	struct pathname		pn;
37197224Scth 	char			*component, *config_name;
37207224Scth 	char			*minorname = NULL;
37217224Scth 	char			*prev_minor = NULL;
37227224Scth 	dev_t			devt = NODEV;
37237224Scth 	int			spectype;
37247224Scth 	struct ddi_minor_data	*dmn;
37257224Scth 	int			circ;
37260Sstevel@tonic-gate 
37277613SVikram.Hegde@Sun.COM 	if (pci_dipp)
37287613SVikram.Hegde@Sun.COM 		*pci_dipp = NULL;
37297613SVikram.Hegde@Sun.COM 
37300Sstevel@tonic-gate 	if (*pathname != '/')
37310Sstevel@tonic-gate 		return (EINVAL);
37320Sstevel@tonic-gate 	parent = ddi_root_node();	/* Begin at the top of the tree */
37330Sstevel@tonic-gate 
37340Sstevel@tonic-gate 	if (error = pn_get(pathname, UIO_SYSSPACE, &pn))
37350Sstevel@tonic-gate 		return (error);
37360Sstevel@tonic-gate 	pn_skipslash(&pn);
37370Sstevel@tonic-gate 
37381333Scth 	ASSERT(i_ddi_devi_attached(parent));
37390Sstevel@tonic-gate 	ndi_hold_devi(parent);
37400Sstevel@tonic-gate 
37410Sstevel@tonic-gate 	component = kmem_alloc(MAXNAMELEN, KM_SLEEP);
37420Sstevel@tonic-gate 	config_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
37430Sstevel@tonic-gate 
37440Sstevel@tonic-gate 	while (pn_pathleft(&pn)) {
37450Sstevel@tonic-gate 		/* remember prev minor (:xxx) in the middle of path */
37460Sstevel@tonic-gate 		if (minorname)
37470Sstevel@tonic-gate 			prev_minor = i_ddi_strdup(minorname, KM_SLEEP);
37480Sstevel@tonic-gate 
37490Sstevel@tonic-gate 		/* Get component and chop off minorname */
37500Sstevel@tonic-gate 		(void) pn_getcomponent(&pn, component);
37510Sstevel@tonic-gate 		i_ddi_parse_name(component, NULL, NULL, &minorname);
37520Sstevel@tonic-gate 
37530Sstevel@tonic-gate 		if (prev_minor == NULL) {
37540Sstevel@tonic-gate 			(void) snprintf(config_name, MAXNAMELEN, "%s",
37550Sstevel@tonic-gate 			    component);
37560Sstevel@tonic-gate 		} else {
37570Sstevel@tonic-gate 			(void) snprintf(config_name, MAXNAMELEN, "%s:%s",
37580Sstevel@tonic-gate 			    component, prev_minor);
37590Sstevel@tonic-gate 			kmem_free(prev_minor, strlen(prev_minor) + 1);
37600Sstevel@tonic-gate 			prev_minor = NULL;
37610Sstevel@tonic-gate 		}
37620Sstevel@tonic-gate 
37630Sstevel@tonic-gate 		/*
37640Sstevel@tonic-gate 		 * Find and configure the child
37650Sstevel@tonic-gate 		 */
37660Sstevel@tonic-gate 		if (ndi_devi_config_one(parent, config_name, &child,
37670Sstevel@tonic-gate 		    NDI_PROMNAME | NDI_NO_EVENT) != NDI_SUCCESS) {
37680Sstevel@tonic-gate 			ndi_rele_devi(parent);
37690Sstevel@tonic-gate 			pn_free(&pn);
37700Sstevel@tonic-gate 			kmem_free(component, MAXNAMELEN);
37710Sstevel@tonic-gate 			kmem_free(config_name, MAXNAMELEN);
37727613SVikram.Hegde@Sun.COM 			if (pci_dipp && *pci_dipp) {
37737613SVikram.Hegde@Sun.COM 				ndi_rele_devi(*pci_dipp);
37747613SVikram.Hegde@Sun.COM 				*pci_dipp = NULL;
37757613SVikram.Hegde@Sun.COM 			}
37760Sstevel@tonic-gate 			return (-1);
37770Sstevel@tonic-gate 		}
37780Sstevel@tonic-gate 
37791333Scth 		ASSERT(i_ddi_devi_attached(child));
37800Sstevel@tonic-gate 		ndi_rele_devi(parent);
37810Sstevel@tonic-gate 		parent = child;
37820Sstevel@tonic-gate 		pn_skipslash(&pn);
37837613SVikram.Hegde@Sun.COM 		if (pci_dipp) {
37847613SVikram.Hegde@Sun.COM 			if (ddi_is_pci_dip(child)) {
37857613SVikram.Hegde@Sun.COM 				ndi_hold_devi(child);
37867613SVikram.Hegde@Sun.COM 				if (*pci_dipp != NULL) {
37877613SVikram.Hegde@Sun.COM 					ndi_rele_devi(*pci_dipp);
37887613SVikram.Hegde@Sun.COM 				}
37897613SVikram.Hegde@Sun.COM 				*pci_dipp = child;
37907613SVikram.Hegde@Sun.COM 			}
37917613SVikram.Hegde@Sun.COM 		}
37920Sstevel@tonic-gate 	}
37930Sstevel@tonic-gate 
37940Sstevel@tonic-gate 	/*
37950Sstevel@tonic-gate 	 * First look for a minor node matching minorname.
37960Sstevel@tonic-gate 	 * Failing that, try to pass minorname to bus_config().
37970Sstevel@tonic-gate 	 */
37980Sstevel@tonic-gate 	if (minorname && i_ddi_minorname_to_devtspectype(parent,
37990Sstevel@tonic-gate 	    minorname, &devt, &spectype) == DDI_FAILURE) {
38000Sstevel@tonic-gate 		(void) snprintf(config_name, MAXNAMELEN, "%s", minorname);
38010Sstevel@tonic-gate 		if (ndi_devi_config_obp_args(parent,
38020Sstevel@tonic-gate 		    config_name, &child, 0) != NDI_SUCCESS) {
38030Sstevel@tonic-gate 			ndi_rele_devi(parent);
38040Sstevel@tonic-gate 			pn_free(&pn);
38050Sstevel@tonic-gate 			kmem_free(component, MAXNAMELEN);
38060Sstevel@tonic-gate 			kmem_free(config_name, MAXNAMELEN);
38070Sstevel@tonic-gate 			NDI_CONFIG_DEBUG((CE_NOTE,
38080Sstevel@tonic-gate 			    "%s: minor node not found\n", pathname));
38097613SVikram.Hegde@Sun.COM 			if (pci_dipp && *pci_dipp) {
38107613SVikram.Hegde@Sun.COM 				ndi_rele_devi(*pci_dipp);
38117613SVikram.Hegde@Sun.COM 				*pci_dipp = NULL;
38127613SVikram.Hegde@Sun.COM 			}
38130Sstevel@tonic-gate 			return (-1);
38140Sstevel@tonic-gate 		}
38150Sstevel@tonic-gate 		minorname = NULL;	/* look for default minor */
38161333Scth 		ASSERT(i_ddi_devi_attached(child));
38170Sstevel@tonic-gate 		ndi_rele_devi(parent);
38180Sstevel@tonic-gate 		parent = child;
38190Sstevel@tonic-gate 	}
38200Sstevel@tonic-gate 
38210Sstevel@tonic-gate 	if (devtp || spectypep) {
38220Sstevel@tonic-gate 		if (minorname == NULL) {
38237224Scth 			/*
38247224Scth 			 * Search for a default entry with an active
38257224Scth 			 * ndi_devi_enter to protect the devi_minor list.
38267224Scth 			 */
38277224Scth 			ndi_devi_enter(parent, &circ);
38280Sstevel@tonic-gate 			for (dmn = DEVI(parent)->devi_minor; dmn;
38290Sstevel@tonic-gate 			    dmn = dmn->next) {
38300Sstevel@tonic-gate 				if (dmn->type == DDM_DEFAULT) {
38310Sstevel@tonic-gate 					devt = dmn->ddm_dev;
38320Sstevel@tonic-gate 					spectype = dmn->ddm_spec_type;
38330Sstevel@tonic-gate 					break;
38340Sstevel@tonic-gate 				}
38350Sstevel@tonic-gate 			}
38360Sstevel@tonic-gate 
38370Sstevel@tonic-gate 			if (devt == NODEV) {
38380Sstevel@tonic-gate 				/*
38390Sstevel@tonic-gate 				 * No default minor node, try the first one;
38400Sstevel@tonic-gate 				 * else, assume 1-1 instance-minor mapping
38410Sstevel@tonic-gate 				 */
38420Sstevel@tonic-gate 				dmn = DEVI(parent)->devi_minor;
38430Sstevel@tonic-gate 				if (dmn && ((dmn->type == DDM_MINOR) ||
38440Sstevel@tonic-gate 				    (dmn->type == DDM_INTERNAL_PATH))) {
38450Sstevel@tonic-gate 					devt = dmn->ddm_dev;
38460Sstevel@tonic-gate 					spectype = dmn->ddm_spec_type;
38470Sstevel@tonic-gate 				} else {
38480Sstevel@tonic-gate 					devt = makedevice(
38490Sstevel@tonic-gate 					    DEVI(parent)->devi_major,
38500Sstevel@tonic-gate 					    ddi_get_instance(parent));
38510Sstevel@tonic-gate 					spectype = S_IFCHR;
38520Sstevel@tonic-gate 				}
38530Sstevel@tonic-gate 			}
38547224Scth 			ndi_devi_exit(parent, circ);
38550Sstevel@tonic-gate 		}
38560Sstevel@tonic-gate 		if (devtp)
38570Sstevel@tonic-gate 			*devtp = devt;
38580Sstevel@tonic-gate 		if (spectypep)
38590Sstevel@tonic-gate 			*spectypep = spectype;
38600Sstevel@tonic-gate 	}
38610Sstevel@tonic-gate 
38620Sstevel@tonic-gate 	pn_free(&pn);
38630Sstevel@tonic-gate 	kmem_free(component, MAXNAMELEN);
38640Sstevel@tonic-gate 	kmem_free(config_name, MAXNAMELEN);
38650Sstevel@tonic-gate 
38660Sstevel@tonic-gate 	/*
38670Sstevel@tonic-gate 	 * If there is no error, return the appropriate parameters
38680Sstevel@tonic-gate 	 */
38690Sstevel@tonic-gate 	if (dipp != NULL)
38700Sstevel@tonic-gate 		*dipp = parent;
38717613SVikram.Hegde@Sun.COM 	else if (pci_dipp == NULL) {
38720Sstevel@tonic-gate 		/*
38730Sstevel@tonic-gate 		 * We should really keep the ref count to keep the node from
38740Sstevel@tonic-gate 		 * detaching but ddi_pathname_to_dev_t() specifies a NULL dipp,
38750Sstevel@tonic-gate 		 * so we have no way of passing back the held dip.  Not holding
38760Sstevel@tonic-gate 		 * the dip allows detaches to occur - which can cause problems
38770Sstevel@tonic-gate 		 * for subsystems which call ddi_pathname_to_dev_t (console).
38780Sstevel@tonic-gate 		 *
38790Sstevel@tonic-gate 		 * Instead of holding the dip, we place a ddi-no-autodetach
38800Sstevel@tonic-gate 		 * property on the node to prevent auto detaching.
38810Sstevel@tonic-gate 		 *
38820Sstevel@tonic-gate 		 * The right fix is to remove ddi_pathname_to_dev_t and replace
38830Sstevel@tonic-gate 		 * it, and all references, with a call that specifies a dipp.
38840Sstevel@tonic-gate 		 * In addition, the callers of this new interfaces would then
38850Sstevel@tonic-gate 		 * need to call ndi_rele_devi when the reference is complete.
38867613SVikram.Hegde@Sun.COM 		 *
38877613SVikram.Hegde@Sun.COM 		 * NOTE: If pci_dipp is non-NULL we are only interested
38887613SVikram.Hegde@Sun.COM 		 * in the PCI parent which is returned held. No need to hold
38897613SVikram.Hegde@Sun.COM 		 * the leaf dip.
38900Sstevel@tonic-gate 		 */
38910Sstevel@tonic-gate 		(void) ddi_prop_update_int(DDI_DEV_T_NONE, parent,
38920Sstevel@tonic-gate 		    DDI_NO_AUTODETACH, 1);
38930Sstevel@tonic-gate 		ndi_rele_devi(parent);
38940Sstevel@tonic-gate 	}
38950Sstevel@tonic-gate 
38960Sstevel@tonic-gate 	return (0);
38970Sstevel@tonic-gate }
38980Sstevel@tonic-gate 
38997613SVikram.Hegde@Sun.COM int
39007613SVikram.Hegde@Sun.COM resolve_pathname(char *pathname,
39017613SVikram.Hegde@Sun.COM 	dev_info_t **dipp, dev_t *devtp, int *spectypep)
39027613SVikram.Hegde@Sun.COM {
39037613SVikram.Hegde@Sun.COM 	return (parse_pathname(pathname, dipp, devtp, spectypep, NULL));
39047613SVikram.Hegde@Sun.COM }
39057613SVikram.Hegde@Sun.COM 
39067613SVikram.Hegde@Sun.COM int
39077613SVikram.Hegde@Sun.COM ddi_find_pci_parent(char *pathname, dev_info_t **pci_dipp)
39087613SVikram.Hegde@Sun.COM {
39097613SVikram.Hegde@Sun.COM 	return (parse_pathname(pathname, NULL, NULL, NULL, pci_dipp));
39107613SVikram.Hegde@Sun.COM }
39117613SVikram.Hegde@Sun.COM 
39120Sstevel@tonic-gate /*
39130Sstevel@tonic-gate  * Given the pathname of a device, return the dev_t of the corresponding
39140Sstevel@tonic-gate  * device.  Returns NODEV on failure.
39150Sstevel@tonic-gate  *
39160Sstevel@tonic-gate  * Note that this call sets the DDI_NO_AUTODETACH property on the devinfo node.
39170Sstevel@tonic-gate  */
39180Sstevel@tonic-gate dev_t
39190Sstevel@tonic-gate ddi_pathname_to_dev_t(char *pathname)
39200Sstevel@tonic-gate {
39210Sstevel@tonic-gate 	dev_t devt;
39220Sstevel@tonic-gate 	int error;
39230Sstevel@tonic-gate 
39240Sstevel@tonic-gate 	error = resolve_pathname(pathname, NULL, &devt, NULL);
39250Sstevel@tonic-gate 
39260Sstevel@tonic-gate 	return (error ? NODEV : devt);
39270Sstevel@tonic-gate }
39280Sstevel@tonic-gate 
39290Sstevel@tonic-gate /*
39300Sstevel@tonic-gate  * Translate a prom pathname to kernel devfs pathname.
39310Sstevel@tonic-gate  * Caller is assumed to allocate devfspath memory of
39320Sstevel@tonic-gate  * size at least MAXPATHLEN
39330Sstevel@tonic-gate  *
39340Sstevel@tonic-gate  * The prom pathname may not include minor name, but
39350Sstevel@tonic-gate  * devfs pathname has a minor name portion.
39360Sstevel@tonic-gate  */
39370Sstevel@tonic-gate int
39380Sstevel@tonic-gate i_ddi_prompath_to_devfspath(char *prompath, char *devfspath)
39390Sstevel@tonic-gate {
39400Sstevel@tonic-gate 	dev_t		devt = (dev_t)NODEV;
39410Sstevel@tonic-gate 	dev_info_t	*dip = NULL;
39420Sstevel@tonic-gate 	char		*minor_name = NULL;
39430Sstevel@tonic-gate 	int		spectype;
39440Sstevel@tonic-gate 	int		error;
39457224Scth 	int		circ;
39460Sstevel@tonic-gate 
39470Sstevel@tonic-gate 	error = resolve_pathname(prompath, &dip, &devt, &spectype);
39480Sstevel@tonic-gate 	if (error)
39490Sstevel@tonic-gate 		return (DDI_FAILURE);
39500Sstevel@tonic-gate 	ASSERT(dip && devt != NODEV);
39510Sstevel@tonic-gate 
39520Sstevel@tonic-gate 	/*
39530Sstevel@tonic-gate 	 * Get in-kernel devfs pathname
39540Sstevel@tonic-gate 	 */
39550Sstevel@tonic-gate 	(void) ddi_pathname(dip, devfspath);
39560Sstevel@tonic-gate 
39577224Scth 	ndi_devi_enter(dip, &circ);
39580Sstevel@tonic-gate 	minor_name = i_ddi_devtspectype_to_minorname(dip, devt, spectype);
39590Sstevel@tonic-gate 	if (minor_name) {
39600Sstevel@tonic-gate 		(void) strcat(devfspath, ":");
39610Sstevel@tonic-gate 		(void) strcat(devfspath, minor_name);
39620Sstevel@tonic-gate 	} else {
39630Sstevel@tonic-gate 		/*
39640Sstevel@tonic-gate 		 * If minor_name is NULL, we have an alias minor node.
39650Sstevel@tonic-gate 		 * So manufacture a path to the corresponding clone minor.
39660Sstevel@tonic-gate 		 */
39670Sstevel@tonic-gate 		(void) snprintf(devfspath, MAXPATHLEN, "%s:%s",
39680Sstevel@tonic-gate 		    CLONE_PATH, ddi_driver_name(dip));
39690Sstevel@tonic-gate 	}
39707224Scth 	ndi_devi_exit(dip, circ);
39710Sstevel@tonic-gate 
39720Sstevel@tonic-gate 	/* release hold from resolve_pathname() */
39730Sstevel@tonic-gate 	ndi_rele_devi(dip);
39740Sstevel@tonic-gate 	return (0);
39750Sstevel@tonic-gate }
39760Sstevel@tonic-gate 
39770Sstevel@tonic-gate /*
39787656SSherry.Moore@Sun.COM  * This function is intended to identify drivers that must quiesce for fast
39797656SSherry.Moore@Sun.COM  * reboot to succeed.  It does not claim to have more knowledge about the device
39807656SSherry.Moore@Sun.COM  * than its driver.  If a driver has implemented quiesce(), it will be invoked;
39817656SSherry.Moore@Sun.COM  * if a so identified driver does not manage any device that needs to be
39827656SSherry.Moore@Sun.COM  * quiesced, it must explicitly set its devo_quiesce dev_op to
39837656SSherry.Moore@Sun.COM  * ddi_quiesce_not_needed.
39847656SSherry.Moore@Sun.COM  */
39857656SSherry.Moore@Sun.COM static int skip_pseudo = 1;	/* Skip pseudo devices */
39867656SSherry.Moore@Sun.COM static int skip_non_hw = 1;	/* Skip devices with no hardware property */
39877656SSherry.Moore@Sun.COM static int
39887656SSherry.Moore@Sun.COM should_implement_quiesce(dev_info_t *dip)
39897656SSherry.Moore@Sun.COM {
39907656SSherry.Moore@Sun.COM 	struct dev_info *devi = DEVI(dip);
39917656SSherry.Moore@Sun.COM 	dev_info_t *pdip;
39927656SSherry.Moore@Sun.COM 
39937656SSherry.Moore@Sun.COM 	/*
39947656SSherry.Moore@Sun.COM 	 * If dip is pseudo and skip_pseudo is set, driver doesn't have to
39957656SSherry.Moore@Sun.COM 	 * implement quiesce().
39967656SSherry.Moore@Sun.COM 	 */
39977656SSherry.Moore@Sun.COM 	if (skip_pseudo &&
39987656SSherry.Moore@Sun.COM 	    strncmp(ddi_binding_name(dip), "pseudo", sizeof ("pseudo")) == 0)
39997656SSherry.Moore@Sun.COM 		return (0);
40007656SSherry.Moore@Sun.COM 
40017656SSherry.Moore@Sun.COM 	/*
40027656SSherry.Moore@Sun.COM 	 * If parent dip is pseudo and skip_pseudo is set, driver doesn't have
40037656SSherry.Moore@Sun.COM 	 * to implement quiesce().
40047656SSherry.Moore@Sun.COM 	 */
40057656SSherry.Moore@Sun.COM 	if (skip_pseudo && (pdip = ddi_get_parent(dip)) != NULL &&
40067656SSherry.Moore@Sun.COM 	    strncmp(ddi_binding_name(pdip), "pseudo", sizeof ("pseudo")) == 0)
40077656SSherry.Moore@Sun.COM 		return (0);
40087656SSherry.Moore@Sun.COM 
40097656SSherry.Moore@Sun.COM 	/*
40107656SSherry.Moore@Sun.COM 	 * If not attached, driver doesn't have to implement quiesce().
40117656SSherry.Moore@Sun.COM 	 */
40127656SSherry.Moore@Sun.COM 	if (!i_ddi_devi_attached(dip))
40137656SSherry.Moore@Sun.COM 		return (0);
40147656SSherry.Moore@Sun.COM 
40157656SSherry.Moore@Sun.COM 	/*
40167656SSherry.Moore@Sun.COM 	 * If dip has no hardware property and skip_non_hw is set,
40177656SSherry.Moore@Sun.COM 	 * driver doesn't have to implement quiesce().
40187656SSherry.Moore@Sun.COM 	 */
40197656SSherry.Moore@Sun.COM 	if (skip_non_hw && devi->devi_hw_prop_ptr == NULL)
40207656SSherry.Moore@Sun.COM 		return (0);
40217656SSherry.Moore@Sun.COM 
40227656SSherry.Moore@Sun.COM 	return (1);
40237656SSherry.Moore@Sun.COM }
40247656SSherry.Moore@Sun.COM 
40257656SSherry.Moore@Sun.COM static int
40267656SSherry.Moore@Sun.COM driver_has_quiesce(struct dev_ops *ops)
40277656SSherry.Moore@Sun.COM {
40287656SSherry.Moore@Sun.COM 	if ((ops->devo_rev >= 4) && (ops->devo_quiesce != nodev) &&
40297656SSherry.Moore@Sun.COM 	    (ops->devo_quiesce != NULL) && (ops->devo_quiesce != nulldev) &&
40307656SSherry.Moore@Sun.COM 	    (ops->devo_quiesce != ddi_quiesce_not_supported))
40317656SSherry.Moore@Sun.COM 		return (1);
40327656SSherry.Moore@Sun.COM 	else
40337656SSherry.Moore@Sun.COM 		return (0);
40347656SSherry.Moore@Sun.COM }
40357656SSherry.Moore@Sun.COM 
40367656SSherry.Moore@Sun.COM /*
40377656SSherry.Moore@Sun.COM  * Check to see if a driver has implemented the quiesce() DDI function.
40387656SSherry.Moore@Sun.COM  */
40397656SSherry.Moore@Sun.COM int
40407656SSherry.Moore@Sun.COM check_driver_quiesce(dev_info_t *dip, void *arg)
40417656SSherry.Moore@Sun.COM {
40427656SSherry.Moore@Sun.COM 	struct dev_ops *ops;
40437656SSherry.Moore@Sun.COM 
40447656SSherry.Moore@Sun.COM 	if (!should_implement_quiesce(dip))
40457656SSherry.Moore@Sun.COM 		return (DDI_WALK_CONTINUE);
40467656SSherry.Moore@Sun.COM 
40477656SSherry.Moore@Sun.COM 	if ((ops = ddi_get_driver(dip)) == NULL)
40487656SSherry.Moore@Sun.COM 		return (DDI_WALK_CONTINUE);
40497656SSherry.Moore@Sun.COM 
40507656SSherry.Moore@Sun.COM 	if (driver_has_quiesce(ops)) {
40517656SSherry.Moore@Sun.COM 		if ((quiesce_debug & 0x2) == 0x2) {
40527656SSherry.Moore@Sun.COM 			if (ops->devo_quiesce == ddi_quiesce_not_needed)
40537656SSherry.Moore@Sun.COM 				cmn_err(CE_CONT, "%s does not need to be "
40547656SSherry.Moore@Sun.COM 				    "quiesced", ddi_driver_name(dip));
40557656SSherry.Moore@Sun.COM 			else
40567656SSherry.Moore@Sun.COM 				cmn_err(CE_CONT, "%s has quiesce routine",
40577656SSherry.Moore@Sun.COM 				    ddi_driver_name(dip));
40587656SSherry.Moore@Sun.COM 		}
40597656SSherry.Moore@Sun.COM 	} else {
40607656SSherry.Moore@Sun.COM 		if (arg != NULL)
40617656SSherry.Moore@Sun.COM 			*((int *)arg) = -1;
40627656SSherry.Moore@Sun.COM 		cmn_err(CE_WARN, "%s has no quiesce()", ddi_driver_name(dip));
40637656SSherry.Moore@Sun.COM 	}
40647656SSherry.Moore@Sun.COM 
40657656SSherry.Moore@Sun.COM 	return (DDI_WALK_CONTINUE);
40667656SSherry.Moore@Sun.COM }
40677656SSherry.Moore@Sun.COM 
40687656SSherry.Moore@Sun.COM /*
40697656SSherry.Moore@Sun.COM  * Quiesce device.
40707656SSherry.Moore@Sun.COM  */
40717656SSherry.Moore@Sun.COM static void
40727656SSherry.Moore@Sun.COM quiesce_one_device(dev_info_t *dip, void *arg)
40737656SSherry.Moore@Sun.COM {
40747656SSherry.Moore@Sun.COM 	struct dev_ops *ops;
40757656SSherry.Moore@Sun.COM 	int should_quiesce = 0;
40767656SSherry.Moore@Sun.COM 
40777656SSherry.Moore@Sun.COM 	/*
40787656SSherry.Moore@Sun.COM 	 * If the device is not attached it doesn't need to be quiesced.
40797656SSherry.Moore@Sun.COM 	 */
40807656SSherry.Moore@Sun.COM 	if (!i_ddi_devi_attached(dip))
40817656SSherry.Moore@Sun.COM 		return;
40827656SSherry.Moore@Sun.COM 
40837656SSherry.Moore@Sun.COM 	if ((ops = ddi_get_driver(dip)) == NULL)
40847656SSherry.Moore@Sun.COM 		return;
40857656SSherry.Moore@Sun.COM 
40867656SSherry.Moore@Sun.COM 	should_quiesce = should_implement_quiesce(dip);
40877656SSherry.Moore@Sun.COM 
40887656SSherry.Moore@Sun.COM 	/*
40897656SSherry.Moore@Sun.COM 	 * If there's an implementation of quiesce(), always call it even if
40907656SSherry.Moore@Sun.COM 	 * some of the drivers don't have quiesce() or quiesce() have failed
40917656SSherry.Moore@Sun.COM 	 * so we can do force fast reboot.  The implementation of quiesce()
40927656SSherry.Moore@Sun.COM 	 * should not negatively affect a regular reboot.
40937656SSherry.Moore@Sun.COM 	 */
40947656SSherry.Moore@Sun.COM 	if (driver_has_quiesce(ops)) {
40957656SSherry.Moore@Sun.COM 		int rc = DDI_SUCCESS;
40967656SSherry.Moore@Sun.COM 
40977656SSherry.Moore@Sun.COM 		if (ops->devo_quiesce == ddi_quiesce_not_needed)
40987656SSherry.Moore@Sun.COM 			return;
40997656SSherry.Moore@Sun.COM 
41007656SSherry.Moore@Sun.COM 		rc = devi_quiesce(dip);
41017656SSherry.Moore@Sun.COM 
41027656SSherry.Moore@Sun.COM 		/* quiesce() should never fail */
41037656SSherry.Moore@Sun.COM 		ASSERT(rc == DDI_SUCCESS);
41047656SSherry.Moore@Sun.COM 
41057656SSherry.Moore@Sun.COM 		if (rc != DDI_SUCCESS && should_quiesce) {
41067656SSherry.Moore@Sun.COM 
41077656SSherry.Moore@Sun.COM 			if (arg != NULL)
41087656SSherry.Moore@Sun.COM 				*((int *)arg) = -1;
41097656SSherry.Moore@Sun.COM 		}
41107656SSherry.Moore@Sun.COM 	} else if (should_quiesce && arg != NULL) {
41117656SSherry.Moore@Sun.COM 		*((int *)arg) = -1;
41127656SSherry.Moore@Sun.COM 	}
41137656SSherry.Moore@Sun.COM }
41147656SSherry.Moore@Sun.COM 
41157656SSherry.Moore@Sun.COM /*
41167656SSherry.Moore@Sun.COM  * Traverse the dev info tree in a breadth-first manner so that we quiesce
41177656SSherry.Moore@Sun.COM  * children first.  All subtrees under the parent of dip will be quiesced.
41187656SSherry.Moore@Sun.COM  */
41197656SSherry.Moore@Sun.COM void
41207656SSherry.Moore@Sun.COM quiesce_devices(dev_info_t *dip, void *arg)
41217656SSherry.Moore@Sun.COM {
41227656SSherry.Moore@Sun.COM 	/*
41237656SSherry.Moore@Sun.COM 	 * if we're reached here, the device tree better not be changing.
41247656SSherry.Moore@Sun.COM 	 * so either devinfo_freeze better be set or we better be panicing.
41257656SSherry.Moore@Sun.COM 	 */
41267656SSherry.Moore@Sun.COM 	ASSERT(devinfo_freeze || panicstr);
41277656SSherry.Moore@Sun.COM 
41287656SSherry.Moore@Sun.COM 	for (; dip != NULL; dip = ddi_get_next_sibling(dip)) {
41297656SSherry.Moore@Sun.COM 		quiesce_devices(ddi_get_child(dip), arg);
41307656SSherry.Moore@Sun.COM 
41317656SSherry.Moore@Sun.COM 		quiesce_one_device(dip, arg);
41327656SSherry.Moore@Sun.COM 	}
41337656SSherry.Moore@Sun.COM }
41347656SSherry.Moore@Sun.COM 
41357656SSherry.Moore@Sun.COM /*
41360Sstevel@tonic-gate  * Reset all the pure leaf drivers on the system at halt time
41370Sstevel@tonic-gate  */
41380Sstevel@tonic-gate static int
41390Sstevel@tonic-gate reset_leaf_device(dev_info_t *dip, void *arg)
41400Sstevel@tonic-gate {
41410Sstevel@tonic-gate 	_NOTE(ARGUNUSED(arg))
41420Sstevel@tonic-gate 	struct dev_ops *ops;
41430Sstevel@tonic-gate 
41440Sstevel@tonic-gate 	/* if the device doesn't need to be reset then there's nothing to do */
41450Sstevel@tonic-gate 	if (!DEVI_NEED_RESET(dip))
41460Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
41470Sstevel@tonic-gate 
41480Sstevel@tonic-gate 	/*
41490Sstevel@tonic-gate 	 * if the device isn't a char/block device or doesn't have a
41500Sstevel@tonic-gate 	 * reset entry point then there's nothing to do.
41510Sstevel@tonic-gate 	 */
41520Sstevel@tonic-gate 	ops = ddi_get_driver(dip);
41530Sstevel@tonic-gate 	if ((ops == NULL) || (ops->devo_cb_ops == NULL) ||
41540Sstevel@tonic-gate 	    (ops->devo_reset == nodev) || (ops->devo_reset == nulldev) ||
41550Sstevel@tonic-gate 	    (ops->devo_reset == NULL))
41560Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
41570Sstevel@tonic-gate 
41580Sstevel@tonic-gate 	if (DEVI_IS_ATTACHING(dip) || DEVI_IS_DETACHING(dip)) {
41590Sstevel@tonic-gate 		static char path[MAXPATHLEN];
41600Sstevel@tonic-gate 
41610Sstevel@tonic-gate 		/*
41620Sstevel@tonic-gate 		 * bad news, this device has blocked in it's attach or
41630Sstevel@tonic-gate 		 * detach routine, which means it not safe to call it's
41640Sstevel@tonic-gate 		 * devo_reset() entry point.
41650Sstevel@tonic-gate 		 */
41660Sstevel@tonic-gate 		cmn_err(CE_WARN, "unable to reset device: %s",
41670Sstevel@tonic-gate 		    ddi_pathname(dip, path));
41680Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
41690Sstevel@tonic-gate 	}
41700Sstevel@tonic-gate 
41710Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_NOTE, "resetting %s%d\n",
41724950Scth 	    ddi_driver_name(dip), ddi_get_instance(dip)));
41730Sstevel@tonic-gate 
41740Sstevel@tonic-gate 	(void) devi_reset(dip, DDI_RESET_FORCE);
41750Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
41760Sstevel@tonic-gate }
41770Sstevel@tonic-gate 
41780Sstevel@tonic-gate void
41790Sstevel@tonic-gate reset_leaves(void)
41800Sstevel@tonic-gate {
41810Sstevel@tonic-gate 	/*
41820Sstevel@tonic-gate 	 * if we're reached here, the device tree better not be changing.
41830Sstevel@tonic-gate 	 * so either devinfo_freeze better be set or we better be panicing.
41840Sstevel@tonic-gate 	 */
41850Sstevel@tonic-gate 	ASSERT(devinfo_freeze || panicstr);
41860Sstevel@tonic-gate 
41870Sstevel@tonic-gate 	(void) walk_devs(top_devinfo, reset_leaf_device, NULL, 0);
41880Sstevel@tonic-gate }
41890Sstevel@tonic-gate 
41907656SSherry.Moore@Sun.COM 
41917656SSherry.Moore@Sun.COM /*
41927656SSherry.Moore@Sun.COM  * devtree_freeze() must be called before quiesce_devices() and reset_leaves()
41937656SSherry.Moore@Sun.COM  * during a normal system shutdown.  It attempts to ensure that there are no
41947656SSherry.Moore@Sun.COM  * outstanding attach or detach operations in progress when quiesce_devices() or
41957656SSherry.Moore@Sun.COM  * reset_leaves()is invoked.  It must be called before the system becomes
41967656SSherry.Moore@Sun.COM  * single-threaded because device attach and detach are multi-threaded
41977656SSherry.Moore@Sun.COM  * operations.  (note that during system shutdown the system doesn't actually
41987656SSherry.Moore@Sun.COM  * become single-thread since other threads still exist, but the shutdown thread
41997656SSherry.Moore@Sun.COM  * will disable preemption for itself, raise it's pil, and stop all the other
42007656SSherry.Moore@Sun.COM  * cpus in the system there by effectively making the system single-threaded.)
42010Sstevel@tonic-gate  */
42020Sstevel@tonic-gate void
42030Sstevel@tonic-gate devtree_freeze(void)
42040Sstevel@tonic-gate {
42050Sstevel@tonic-gate 	int delayed = 0;
42060Sstevel@tonic-gate 
42070Sstevel@tonic-gate 	/* if we're panicing then the device tree isn't going to be changing */
42080Sstevel@tonic-gate 	if (panicstr)
42090Sstevel@tonic-gate 		return;
42100Sstevel@tonic-gate 
42110Sstevel@tonic-gate 	/* stop all dev_info state changes in the device tree */
42120Sstevel@tonic-gate 	devinfo_freeze = gethrtime();
42130Sstevel@tonic-gate 
42140Sstevel@tonic-gate 	/*
42150Sstevel@tonic-gate 	 * if we're not panicing and there are on-going attach or detach
42160Sstevel@tonic-gate 	 * operations, wait for up to 3 seconds for them to finish.  This
42170Sstevel@tonic-gate 	 * is a randomly chosen interval but this should be ok because:
42180Sstevel@tonic-gate 	 * - 3 seconds is very small relative to the deadman timer.
42190Sstevel@tonic-gate 	 * - normal attach and detach operations should be very quick.
42200Sstevel@tonic-gate 	 * - attach and detach operations are fairly rare.
42210Sstevel@tonic-gate 	 */
42220Sstevel@tonic-gate 	while (!panicstr && atomic_add_long_nv(&devinfo_attach_detach, 0) &&
42230Sstevel@tonic-gate 	    (delayed < 3)) {
42240Sstevel@tonic-gate 		delayed += 1;
42250Sstevel@tonic-gate 
42260Sstevel@tonic-gate 		/* do a sleeping wait for one second */
42270Sstevel@tonic-gate 		ASSERT(!servicing_interrupt());
42280Sstevel@tonic-gate 		delay(drv_usectohz(MICROSEC));
42290Sstevel@tonic-gate 	}
42300Sstevel@tonic-gate }
42310Sstevel@tonic-gate 
42320Sstevel@tonic-gate static int
42330Sstevel@tonic-gate bind_dip(dev_info_t *dip, void *arg)
42340Sstevel@tonic-gate {
42350Sstevel@tonic-gate 	_NOTE(ARGUNUSED(arg))
42364145Scth 	char	*path;
42374145Scth 	major_t	major, pmajor;
42384145Scth 
42394145Scth 	/*
42404145Scth 	 * If the node is currently bound to the wrong driver, try to unbind
42414145Scth 	 * so that we can rebind to the correct driver.
42424145Scth 	 */
42434145Scth 	if (i_ddi_node_state(dip) >= DS_BOUND) {
42444145Scth 		major = ddi_compatible_driver_major(dip, NULL);
42454145Scth 		if ((DEVI(dip)->devi_major == major) &&
42464145Scth 		    (i_ddi_node_state(dip) >= DS_INITIALIZED)) {
42474145Scth 			/*
42484145Scth 			 * Check for a path-oriented driver alias that
42494145Scth 			 * takes precedence over current driver binding.
42504145Scth 			 */
42514145Scth 			path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
42524145Scth 			(void) ddi_pathname(dip, path);
42534145Scth 			pmajor = ddi_name_to_major(path);
42547009Scth 			if ((pmajor != DDI_MAJOR_T_NONE) &&
42554145Scth 			    !(devnamesp[pmajor].dn_flags & DN_DRIVER_REMOVED))
42564145Scth 				major = pmajor;
42574145Scth 			kmem_free(path, MAXPATHLEN);
42584145Scth 		}
42594145Scth 
42604145Scth 		/* attempt unbind if current driver is incorrect */
42617009Scth 		if ((major != DDI_MAJOR_T_NONE) &&
42624145Scth 		    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED) &&
42634145Scth 		    (major != DEVI(dip)->devi_major))
42644145Scth 			(void) ndi_devi_unbind_driver(dip);
42654145Scth 	}
42664145Scth 
42674145Scth 	/* If unbound, try to bind to a driver */
42680Sstevel@tonic-gate 	if (i_ddi_node_state(dip) < DS_BOUND)
42690Sstevel@tonic-gate 		(void) ndi_devi_bind_driver(dip, 0);
42700Sstevel@tonic-gate 
42710Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
42720Sstevel@tonic-gate }
42730Sstevel@tonic-gate 
42740Sstevel@tonic-gate void
42750Sstevel@tonic-gate i_ddi_bind_devs(void)
42760Sstevel@tonic-gate {
42774145Scth 	/* flush devfs so that ndi_devi_unbind_driver will work when possible */
42784145Scth 	(void) devfs_clean(top_devinfo, NULL, 0);
42794145Scth 
42800Sstevel@tonic-gate 	ddi_walk_devs(top_devinfo, bind_dip, (void *)NULL);
42810Sstevel@tonic-gate }
42820Sstevel@tonic-gate 
42838831SJerry.Gilliam@Sun.COM /* callback data for unbind_children_by_alias() */
42848831SJerry.Gilliam@Sun.COM typedef struct unbind_data {
42858831SJerry.Gilliam@Sun.COM 	major_t	drv_major;
42868831SJerry.Gilliam@Sun.COM 	char	*drv_alias;
42878831SJerry.Gilliam@Sun.COM 	int	ndevs_bound;
42888831SJerry.Gilliam@Sun.COM 	int	unbind_errors;
42898831SJerry.Gilliam@Sun.COM } unbind_data_t;
42908831SJerry.Gilliam@Sun.COM 
42918831SJerry.Gilliam@Sun.COM /*
42928831SJerry.Gilliam@Sun.COM  * A utility function provided for testing and support convenience
42938831SJerry.Gilliam@Sun.COM  * Called for each device during an upgrade_drv -d bound to the alias
42948831SJerry.Gilliam@Sun.COM  * that cannot be unbound due to device in use.
42958831SJerry.Gilliam@Sun.COM  */
42968831SJerry.Gilliam@Sun.COM static void
42978831SJerry.Gilliam@Sun.COM unbind_alias_dev_in_use(dev_info_t *dip, char *alias)
42988831SJerry.Gilliam@Sun.COM {
42998831SJerry.Gilliam@Sun.COM 	if (moddebug & MODDEBUG_BINDING) {
43008831SJerry.Gilliam@Sun.COM 		cmn_err(CE_CONT, "%s%d: state %d: bound to %s\n",
43018831SJerry.Gilliam@Sun.COM 		    ddi_driver_name(dip), ddi_get_instance(dip),
43028831SJerry.Gilliam@Sun.COM 		    i_ddi_node_state(dip), alias);
43038831SJerry.Gilliam@Sun.COM 	}
43048831SJerry.Gilliam@Sun.COM }
43058831SJerry.Gilliam@Sun.COM 
43068831SJerry.Gilliam@Sun.COM /*
43078831SJerry.Gilliam@Sun.COM  * walkdevs callback for unbind devices bound to specific driver
43088831SJerry.Gilliam@Sun.COM  * and alias.  Invoked within the context of update_drv -d <alias>.
43098831SJerry.Gilliam@Sun.COM  */
43100Sstevel@tonic-gate static int
43118831SJerry.Gilliam@Sun.COM unbind_children_by_alias(dev_info_t *dip, void *arg)
43128831SJerry.Gilliam@Sun.COM {
43138831SJerry.Gilliam@Sun.COM 	int		circ;
43148831SJerry.Gilliam@Sun.COM 	dev_info_t	*cdip;
43158831SJerry.Gilliam@Sun.COM 	dev_info_t	*next;
43168831SJerry.Gilliam@Sun.COM 	unbind_data_t	*ub = (unbind_data_t *)(uintptr_t)arg;
43178831SJerry.Gilliam@Sun.COM 	int		rv;
43188831SJerry.Gilliam@Sun.COM 
43198831SJerry.Gilliam@Sun.COM 	/*
43208831SJerry.Gilliam@Sun.COM 	 * We are called from update_drv to try to unbind a specific
43218831SJerry.Gilliam@Sun.COM 	 * set of aliases for a driver.  Unbind what persistent nodes
43228831SJerry.Gilliam@Sun.COM 	 * we can, and return the number of nodes which cannot be unbound.
43238831SJerry.Gilliam@Sun.COM 	 * If not all nodes can be unbound, update_drv leaves the
43248831SJerry.Gilliam@Sun.COM 	 * state of the driver binding files unchanged, except in
43258831SJerry.Gilliam@Sun.COM 	 * the case of -f.
43268831SJerry.Gilliam@Sun.COM 	 */
43270Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
43288831SJerry.Gilliam@Sun.COM 	for (cdip = ddi_get_child(dip); cdip; cdip = next) {
43298831SJerry.Gilliam@Sun.COM 		next = ddi_get_next_sibling(cdip);
43308831SJerry.Gilliam@Sun.COM 		if ((ddi_driver_major(cdip) != ub->drv_major) ||
43318831SJerry.Gilliam@Sun.COM 		    (strcmp(DEVI(cdip)->devi_node_name, ub->drv_alias) != 0))
43320Sstevel@tonic-gate 			continue;
43338831SJerry.Gilliam@Sun.COM 		if (i_ddi_node_state(cdip) >= DS_BOUND) {
43348831SJerry.Gilliam@Sun.COM 			rv = ndi_devi_unbind_driver(cdip);
43358831SJerry.Gilliam@Sun.COM 			if (rv != DDI_SUCCESS ||
43368831SJerry.Gilliam@Sun.COM 			    (i_ddi_node_state(cdip) >= DS_BOUND)) {
43378831SJerry.Gilliam@Sun.COM 				unbind_alias_dev_in_use(cdip, ub->drv_alias);
43388831SJerry.Gilliam@Sun.COM 				ub->ndevs_bound++;
43398831SJerry.Gilliam@Sun.COM 				continue;
43408831SJerry.Gilliam@Sun.COM 			}
43418831SJerry.Gilliam@Sun.COM 			if (ndi_dev_is_persistent_node(cdip) == 0)
43428831SJerry.Gilliam@Sun.COM 				(void) ddi_remove_child(cdip, 0);
43430Sstevel@tonic-gate 		}
43440Sstevel@tonic-gate 	}
43450Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
43460Sstevel@tonic-gate 
43470Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
43480Sstevel@tonic-gate }
43490Sstevel@tonic-gate 
43508831SJerry.Gilliam@Sun.COM /*
43518831SJerry.Gilliam@Sun.COM  * Unbind devices by driver & alias
43528831SJerry.Gilliam@Sun.COM  * Context: update_drv [-f] -d -i <alias> <driver>
43538831SJerry.Gilliam@Sun.COM  */
43548831SJerry.Gilliam@Sun.COM int
43558831SJerry.Gilliam@Sun.COM i_ddi_unbind_devs_by_alias(major_t major, char *alias)
43568831SJerry.Gilliam@Sun.COM {
43578831SJerry.Gilliam@Sun.COM 	unbind_data_t	*ub;
43588831SJerry.Gilliam@Sun.COM 	int		rv;
43598831SJerry.Gilliam@Sun.COM 
43608831SJerry.Gilliam@Sun.COM 	ub = kmem_zalloc(sizeof (*ub), KM_SLEEP);
43618831SJerry.Gilliam@Sun.COM 	ub->drv_major = major;
43628831SJerry.Gilliam@Sun.COM 	ub->drv_alias = alias;
43638831SJerry.Gilliam@Sun.COM 	ub->ndevs_bound = 0;
43648831SJerry.Gilliam@Sun.COM 	ub->unbind_errors = 0;
43658831SJerry.Gilliam@Sun.COM 
43668831SJerry.Gilliam@Sun.COM 	/* flush devfs so that ndi_devi_unbind_driver will work when possible */
43678831SJerry.Gilliam@Sun.COM 	devfs_clean(top_devinfo, NULL, 0);
43688831SJerry.Gilliam@Sun.COM 	ddi_walk_devs(top_devinfo, unbind_children_by_alias,
43698831SJerry.Gilliam@Sun.COM 	    (void *)(uintptr_t)ub);
43708831SJerry.Gilliam@Sun.COM 
43718831SJerry.Gilliam@Sun.COM 	/* return the number of devices remaining bound to the alias */
43728831SJerry.Gilliam@Sun.COM 	rv = ub->ndevs_bound + ub->unbind_errors;
43738831SJerry.Gilliam@Sun.COM 	kmem_free(ub, sizeof (*ub));
43748831SJerry.Gilliam@Sun.COM 	return (rv);
43758831SJerry.Gilliam@Sun.COM }
43768831SJerry.Gilliam@Sun.COM 
43778831SJerry.Gilliam@Sun.COM /*
43788831SJerry.Gilliam@Sun.COM  * walkdevs callback for unbind devices by driver
43798831SJerry.Gilliam@Sun.COM  */
43808831SJerry.Gilliam@Sun.COM static int
43818831SJerry.Gilliam@Sun.COM unbind_children_by_driver(dev_info_t *dip, void *arg)
43828831SJerry.Gilliam@Sun.COM {
43838831SJerry.Gilliam@Sun.COM 	int		circ;
43848831SJerry.Gilliam@Sun.COM 	dev_info_t	*cdip;
43858831SJerry.Gilliam@Sun.COM 	dev_info_t	*next;
43868831SJerry.Gilliam@Sun.COM 	major_t		major = (major_t)(uintptr_t)arg;
43878831SJerry.Gilliam@Sun.COM 	int		rv;
43888831SJerry.Gilliam@Sun.COM 
43898831SJerry.Gilliam@Sun.COM 	/*
43908831SJerry.Gilliam@Sun.COM 	 * We are called either from rem_drv or update_drv when reloading
43918831SJerry.Gilliam@Sun.COM 	 * a driver.conf file. In either case, we unbind persistent nodes
43928831SJerry.Gilliam@Sun.COM 	 * and destroy .conf nodes. In the case of rem_drv, this will be
43938831SJerry.Gilliam@Sun.COM 	 * the final state. In the case of update_drv,  i_ddi_bind_devs()
43948831SJerry.Gilliam@Sun.COM 	 * may be invoked later to re-enumerate (new) driver.conf rebind
43958831SJerry.Gilliam@Sun.COM 	 * persistent nodes.
43968831SJerry.Gilliam@Sun.COM 	 */
43978831SJerry.Gilliam@Sun.COM 	ndi_devi_enter(dip, &circ);
43988831SJerry.Gilliam@Sun.COM 	for (cdip = ddi_get_child(dip); cdip; cdip = next) {
43998831SJerry.Gilliam@Sun.COM 		next = ddi_get_next_sibling(cdip);
44008831SJerry.Gilliam@Sun.COM 		if (ddi_driver_major(cdip) != major)
44018831SJerry.Gilliam@Sun.COM 			continue;
44028831SJerry.Gilliam@Sun.COM 		if (i_ddi_node_state(cdip) >= DS_BOUND) {
44038831SJerry.Gilliam@Sun.COM 			rv = ndi_devi_unbind_driver(cdip);
44048831SJerry.Gilliam@Sun.COM 			if (rv == DDI_FAILURE ||
44058831SJerry.Gilliam@Sun.COM 			    (i_ddi_node_state(cdip) >= DS_BOUND))
44068831SJerry.Gilliam@Sun.COM 				continue;
44078831SJerry.Gilliam@Sun.COM 			if (ndi_dev_is_persistent_node(cdip) == 0)
44088831SJerry.Gilliam@Sun.COM 				(void) ddi_remove_child(cdip, 0);
44098831SJerry.Gilliam@Sun.COM 		}
44108831SJerry.Gilliam@Sun.COM 	}
44118831SJerry.Gilliam@Sun.COM 	ndi_devi_exit(dip, circ);
44128831SJerry.Gilliam@Sun.COM 
44138831SJerry.Gilliam@Sun.COM 	return (DDI_WALK_CONTINUE);
44148831SJerry.Gilliam@Sun.COM }
44158831SJerry.Gilliam@Sun.COM 
44168831SJerry.Gilliam@Sun.COM /*
44178831SJerry.Gilliam@Sun.COM  * Unbind devices by driver
44188831SJerry.Gilliam@Sun.COM  * Context: rem_drv or unload driver.conf
44198831SJerry.Gilliam@Sun.COM  */
44200Sstevel@tonic-gate void
44210Sstevel@tonic-gate i_ddi_unbind_devs(major_t major)
44220Sstevel@tonic-gate {
44238831SJerry.Gilliam@Sun.COM 	/* flush devfs so that ndi_devi_unbind_driver will work when possible */
44248831SJerry.Gilliam@Sun.COM 	devfs_clean(top_devinfo, NULL, 0);
44258831SJerry.Gilliam@Sun.COM 	ddi_walk_devs(top_devinfo, unbind_children_by_driver,
44268831SJerry.Gilliam@Sun.COM 	    (void *)(uintptr_t)major);
44270Sstevel@tonic-gate }
44280Sstevel@tonic-gate 
44290Sstevel@tonic-gate /*
44300Sstevel@tonic-gate  * I/O Hotplug control
44310Sstevel@tonic-gate  */
44320Sstevel@tonic-gate 
44330Sstevel@tonic-gate /*
44340Sstevel@tonic-gate  * create and attach a dev_info node from a .conf file spec
44350Sstevel@tonic-gate  */
44360Sstevel@tonic-gate static void
44370Sstevel@tonic-gate init_spec_child(dev_info_t *pdip, struct hwc_spec *specp, uint_t flags)
44380Sstevel@tonic-gate {
44390Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags))
44400Sstevel@tonic-gate 	dev_info_t *dip;
44410Sstevel@tonic-gate 	char *node_name;
44420Sstevel@tonic-gate 
44430Sstevel@tonic-gate 	if (((node_name = specp->hwc_devi_name) == NULL) ||
44447009Scth 	    (ddi_name_to_major(node_name) == DDI_MAJOR_T_NONE)) {
44450Sstevel@tonic-gate 		char *tmp = node_name;
44460Sstevel@tonic-gate 		if (tmp == NULL)
44470Sstevel@tonic-gate 			tmp = "<none>";
44480Sstevel@tonic-gate 		cmn_err(CE_CONT,
44490Sstevel@tonic-gate 		    "init_spec_child: parent=%s, bad spec (%s)\n",
44500Sstevel@tonic-gate 		    ddi_node_name(pdip), tmp);
44510Sstevel@tonic-gate 		return;
44520Sstevel@tonic-gate 	}
44530Sstevel@tonic-gate 
4454789Sahrens 	dip = i_ddi_alloc_node(pdip, node_name, (pnode_t)DEVI_PSEUDO_NODEID,
44550Sstevel@tonic-gate 	    -1, specp->hwc_devi_sys_prop_ptr, KM_SLEEP);
44560Sstevel@tonic-gate 
44570Sstevel@tonic-gate 	if (dip == NULL)
44580Sstevel@tonic-gate 		return;
44590Sstevel@tonic-gate 
44600Sstevel@tonic-gate 	if (ddi_initchild(pdip, dip) != DDI_SUCCESS)
44610Sstevel@tonic-gate 		(void) ddi_remove_child(dip, 0);
44620Sstevel@tonic-gate }
44630Sstevel@tonic-gate 
44640Sstevel@tonic-gate /*
44650Sstevel@tonic-gate  * Lookup hwc specs from hash tables and make children from the spec
44660Sstevel@tonic-gate  * Because some .conf children are "merge" nodes, we also initialize
44670Sstevel@tonic-gate  * .conf children to merge properties onto hardware nodes.
44680Sstevel@tonic-gate  *
44690Sstevel@tonic-gate  * The pdip must be held busy.
44700Sstevel@tonic-gate  */
44710Sstevel@tonic-gate int
44720Sstevel@tonic-gate i_ndi_make_spec_children(dev_info_t *pdip, uint_t flags)
44730Sstevel@tonic-gate {
44740Sstevel@tonic-gate 	extern struct hwc_spec *hwc_get_child_spec(dev_info_t *, major_t);
4475298Scth 	int			circ;
4476298Scth 	struct hwc_spec		*list, *spec;
4477298Scth 
4478298Scth 	ndi_devi_enter(pdip, &circ);
4479298Scth 	if (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN) {
4480298Scth 		ndi_devi_exit(pdip, circ);
44810Sstevel@tonic-gate 		return (DDI_SUCCESS);
4482298Scth 	}
44830Sstevel@tonic-gate 
44847009Scth 	list = hwc_get_child_spec(pdip, DDI_MAJOR_T_NONE);
44850Sstevel@tonic-gate 	for (spec = list; spec != NULL; spec = spec->hwc_next) {
44860Sstevel@tonic-gate 		init_spec_child(pdip, spec, flags);
44870Sstevel@tonic-gate 	}
44880Sstevel@tonic-gate 	hwc_free_spec_list(list);
44890Sstevel@tonic-gate 
44900Sstevel@tonic-gate 	mutex_enter(&DEVI(pdip)->devi_lock);
44910Sstevel@tonic-gate 	DEVI(pdip)->devi_flags |= DEVI_MADE_CHILDREN;
44920Sstevel@tonic-gate 	mutex_exit(&DEVI(pdip)->devi_lock);
4493298Scth 	ndi_devi_exit(pdip, circ);
44940Sstevel@tonic-gate 	return (DDI_SUCCESS);
44950Sstevel@tonic-gate }
44960Sstevel@tonic-gate 
44970Sstevel@tonic-gate /*
44980Sstevel@tonic-gate  * Run initchild on all child nodes such that instance assignment
44990Sstevel@tonic-gate  * for multiport network cards are contiguous.
45000Sstevel@tonic-gate  *
45010Sstevel@tonic-gate  * The pdip must be held busy.
45020Sstevel@tonic-gate  */
45030Sstevel@tonic-gate static void
45040Sstevel@tonic-gate i_ndi_init_hw_children(dev_info_t *pdip, uint_t flags)
45050Sstevel@tonic-gate {
45060Sstevel@tonic-gate 	dev_info_t *dip;
45070Sstevel@tonic-gate 
45080Sstevel@tonic-gate 	ASSERT(DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
45090Sstevel@tonic-gate 
45100Sstevel@tonic-gate 	/* contiguous instance assignment */
45110Sstevel@tonic-gate 	e_ddi_enter_instance();
45120Sstevel@tonic-gate 	dip = ddi_get_child(pdip);
45130Sstevel@tonic-gate 	while (dip) {
45140Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(dip))
45150Sstevel@tonic-gate 			(void) i_ndi_config_node(dip, DS_INITIALIZED, flags);
45160Sstevel@tonic-gate 		dip = ddi_get_next_sibling(dip);
45170Sstevel@tonic-gate 	}
45180Sstevel@tonic-gate 	e_ddi_exit_instance();
45190Sstevel@tonic-gate }
45200Sstevel@tonic-gate 
45210Sstevel@tonic-gate /*
45220Sstevel@tonic-gate  * report device status
45230Sstevel@tonic-gate  */
45240Sstevel@tonic-gate static void
45250Sstevel@tonic-gate i_ndi_devi_report_status_change(dev_info_t *dip, char *path)
45260Sstevel@tonic-gate {
45270Sstevel@tonic-gate 	char *status;
45280Sstevel@tonic-gate 
45290Sstevel@tonic-gate 	if (!DEVI_NEED_REPORT(dip) ||
45300Sstevel@tonic-gate 	    (i_ddi_node_state(dip) < DS_INITIALIZED)) {
45310Sstevel@tonic-gate 		return;
45320Sstevel@tonic-gate 	}
45330Sstevel@tonic-gate 
45340Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
45350Sstevel@tonic-gate 		status = "offline";
45360Sstevel@tonic-gate 	} else if (DEVI_IS_DEVICE_DOWN(dip)) {
45370Sstevel@tonic-gate 		status = "down";
45380Sstevel@tonic-gate 	} else if (DEVI_IS_BUS_QUIESCED(dip)) {
45390Sstevel@tonic-gate 		status = "quiesced";
45400Sstevel@tonic-gate 	} else if (DEVI_IS_BUS_DOWN(dip)) {
45410Sstevel@tonic-gate 		status = "down";
45421333Scth 	} else if (i_ddi_devi_attached(dip)) {
45430Sstevel@tonic-gate 		status = "online";
45440Sstevel@tonic-gate 	} else {
45450Sstevel@tonic-gate 		status = "unknown";
45460Sstevel@tonic-gate 	}
45470Sstevel@tonic-gate 
45480Sstevel@tonic-gate 	if (path == NULL) {
45490Sstevel@tonic-gate 		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
45500Sstevel@tonic-gate 		cmn_err(CE_CONT, "?%s (%s%d) %s\n",
45514950Scth 		    ddi_pathname(dip, path), ddi_driver_name(dip),
45524950Scth 		    ddi_get_instance(dip), status);
45530Sstevel@tonic-gate 		kmem_free(path, MAXPATHLEN);
45540Sstevel@tonic-gate 	} else {
45550Sstevel@tonic-gate 		cmn_err(CE_CONT, "?%s (%s%d) %s\n",
45564950Scth 		    path, ddi_driver_name(dip),
45574950Scth 		    ddi_get_instance(dip), status);
45580Sstevel@tonic-gate 	}
45590Sstevel@tonic-gate 
4560495Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
45610Sstevel@tonic-gate 	DEVI_REPORT_DONE(dip);
4562495Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
45630Sstevel@tonic-gate }
45640Sstevel@tonic-gate 
45650Sstevel@tonic-gate /*
45660Sstevel@tonic-gate  * log a notification that a dev_info node has been configured.
45670Sstevel@tonic-gate  */
45680Sstevel@tonic-gate static int
45690Sstevel@tonic-gate i_log_devfs_add_devinfo(dev_info_t *dip, uint_t flags)
45700Sstevel@tonic-gate {
45710Sstevel@tonic-gate 	int se_err;
45720Sstevel@tonic-gate 	char *pathname;
45730Sstevel@tonic-gate 	sysevent_t *ev;
45740Sstevel@tonic-gate 	sysevent_id_t eid;
45750Sstevel@tonic-gate 	sysevent_value_t se_val;
45760Sstevel@tonic-gate 	sysevent_attr_list_t *ev_attr_list = NULL;
45770Sstevel@tonic-gate 	char *class_name;
45780Sstevel@tonic-gate 	int no_transport = 0;
45790Sstevel@tonic-gate 
45800Sstevel@tonic-gate 	ASSERT(dip);
45810Sstevel@tonic-gate 
45820Sstevel@tonic-gate 	/*
45830Sstevel@tonic-gate 	 * Invalidate the devinfo snapshot cache
45840Sstevel@tonic-gate 	 */
45850Sstevel@tonic-gate 	i_ddi_di_cache_invalidate(KM_SLEEP);
45860Sstevel@tonic-gate 
45870Sstevel@tonic-gate 	/* do not generate ESC_DEVFS_DEVI_ADD event during boot */
45880Sstevel@tonic-gate 	if (!i_ddi_io_initialized())
45890Sstevel@tonic-gate 		return (DDI_SUCCESS);
45900Sstevel@tonic-gate 
45910Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_ADD, EP_DDI, SE_SLEEP);
45920Sstevel@tonic-gate 
45930Sstevel@tonic-gate 	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
45940Sstevel@tonic-gate 
45950Sstevel@tonic-gate 	(void) ddi_pathname(dip, pathname);
45960Sstevel@tonic-gate 	ASSERT(strlen(pathname));
45970Sstevel@tonic-gate 
45980Sstevel@tonic-gate 	se_val.value_type = SE_DATA_TYPE_STRING;
45990Sstevel@tonic-gate 	se_val.value.sv_string = pathname;
46000Sstevel@tonic-gate 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
46010Sstevel@tonic-gate 	    &se_val, SE_SLEEP) != 0) {
46020Sstevel@tonic-gate 		goto fail;
46030Sstevel@tonic-gate 	}
46040Sstevel@tonic-gate 
46050Sstevel@tonic-gate 	/* add the device class attribute */
46060Sstevel@tonic-gate 	if ((class_name = i_ddi_devi_class(dip)) != NULL) {
46070Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_STRING;
46080Sstevel@tonic-gate 		se_val.value.sv_string = class_name;
46090Sstevel@tonic-gate 
46100Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
46110Sstevel@tonic-gate 		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
46120Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
46130Sstevel@tonic-gate 			goto fail;
46140Sstevel@tonic-gate 		}
46150Sstevel@tonic-gate 	}
46160Sstevel@tonic-gate 
46170Sstevel@tonic-gate 	/*
46180Sstevel@tonic-gate 	 * must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
46190Sstevel@tonic-gate 	 * in which case the branch event will be logged by the caller
46200Sstevel@tonic-gate 	 * after the entire branch has been configured.
46210Sstevel@tonic-gate 	 */
46220Sstevel@tonic-gate 	if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
46230Sstevel@tonic-gate 		/*
46240Sstevel@tonic-gate 		 * Instead of logging a separate branch event just add
46250Sstevel@tonic-gate 		 * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
46260Sstevel@tonic-gate 		 * generate a EC_DEV_BRANCH event.
46270Sstevel@tonic-gate 		 */
46280Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_INT32;
46290Sstevel@tonic-gate 		se_val.value.sv_int32 = 1;
46300Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
46310Sstevel@tonic-gate 		    DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
46320Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
46330Sstevel@tonic-gate 			goto fail;
46340Sstevel@tonic-gate 		}
46350Sstevel@tonic-gate 	}
46360Sstevel@tonic-gate 
46370Sstevel@tonic-gate 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
46380Sstevel@tonic-gate 		sysevent_free_attr(ev_attr_list);
46390Sstevel@tonic-gate 		goto fail;
46400Sstevel@tonic-gate 	}
46410Sstevel@tonic-gate 
46420Sstevel@tonic-gate 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
46430Sstevel@tonic-gate 		if (se_err == SE_NO_TRANSPORT)
46440Sstevel@tonic-gate 			no_transport = 1;
46450Sstevel@tonic-gate 		goto fail;
46460Sstevel@tonic-gate 	}
46470Sstevel@tonic-gate 
46480Sstevel@tonic-gate 	sysevent_free(ev);
46490Sstevel@tonic-gate 	kmem_free(pathname, MAXPATHLEN);
46500Sstevel@tonic-gate 
46510Sstevel@tonic-gate 	return (DDI_SUCCESS);
46520Sstevel@tonic-gate 
46530Sstevel@tonic-gate fail:
46540Sstevel@tonic-gate 	cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_ADD event for %s%s",
46550Sstevel@tonic-gate 	    pathname, (no_transport) ? " (syseventd not responding)" : "");
46560Sstevel@tonic-gate 
46570Sstevel@tonic-gate 	cmn_err(CE_WARN, "/dev may not be current for driver %s. "
46580Sstevel@tonic-gate 	    "Run devfsadm -i %s",
46590Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_driver_name(dip));
46600Sstevel@tonic-gate 
46610Sstevel@tonic-gate 	sysevent_free(ev);
46620Sstevel@tonic-gate 	kmem_free(pathname, MAXPATHLEN);
46630Sstevel@tonic-gate 	return (DDI_SUCCESS);
46640Sstevel@tonic-gate }
46650Sstevel@tonic-gate 
46660Sstevel@tonic-gate /*
46670Sstevel@tonic-gate  * log a notification that a dev_info node has been unconfigured.
46680Sstevel@tonic-gate  */
46690Sstevel@tonic-gate static int
46700Sstevel@tonic-gate i_log_devfs_remove_devinfo(char *pathname, char *class_name, char *driver_name,
46710Sstevel@tonic-gate     int instance, uint_t flags)
46720Sstevel@tonic-gate {
46730Sstevel@tonic-gate 	sysevent_t *ev;
46740Sstevel@tonic-gate 	sysevent_id_t eid;
46750Sstevel@tonic-gate 	sysevent_value_t se_val;
46760Sstevel@tonic-gate 	sysevent_attr_list_t *ev_attr_list = NULL;
46770Sstevel@tonic-gate 	int se_err;
46780Sstevel@tonic-gate 	int no_transport = 0;
46790Sstevel@tonic-gate 
46800Sstevel@tonic-gate 	i_ddi_di_cache_invalidate(KM_SLEEP);
46810Sstevel@tonic-gate 
46820Sstevel@tonic-gate 	if (!i_ddi_io_initialized())
46830Sstevel@tonic-gate 		return (DDI_SUCCESS);
46840Sstevel@tonic-gate 
46850Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_REMOVE, EP_DDI, SE_SLEEP);
46860Sstevel@tonic-gate 
46870Sstevel@tonic-gate 	se_val.value_type = SE_DATA_TYPE_STRING;
46880Sstevel@tonic-gate 	se_val.value.sv_string = pathname;
46890Sstevel@tonic-gate 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
46900Sstevel@tonic-gate 	    &se_val, SE_SLEEP) != 0) {
46910Sstevel@tonic-gate 		goto fail;
46920Sstevel@tonic-gate 	}
46930Sstevel@tonic-gate 
46940Sstevel@tonic-gate 	if (class_name) {
46950Sstevel@tonic-gate 		/* add the device class, driver name and instance attributes */
46960Sstevel@tonic-gate 
46970Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_STRING;
46980Sstevel@tonic-gate 		se_val.value.sv_string = class_name;
46990Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
47000Sstevel@tonic-gate 		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
47010Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
47020Sstevel@tonic-gate 			goto fail;
47030Sstevel@tonic-gate 		}
47040Sstevel@tonic-gate 
47050Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_STRING;
47060Sstevel@tonic-gate 		se_val.value.sv_string = driver_name;
47070Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
47080Sstevel@tonic-gate 		    DEVFS_DRIVER_NAME, &se_val, SE_SLEEP) != 0) {
47090Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
47100Sstevel@tonic-gate 			goto fail;
47110Sstevel@tonic-gate 		}
47120Sstevel@tonic-gate 
47130Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_INT32;
47140Sstevel@tonic-gate 		se_val.value.sv_int32 = instance;
47150Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
47160Sstevel@tonic-gate 		    DEVFS_INSTANCE, &se_val, SE_SLEEP) != 0) {
47170Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
47180Sstevel@tonic-gate 			goto fail;
47190Sstevel@tonic-gate 		}
47200Sstevel@tonic-gate 	}
47210Sstevel@tonic-gate 
47220Sstevel@tonic-gate 	/*
47230Sstevel@tonic-gate 	 * must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
47240Sstevel@tonic-gate 	 * in which case the branch event will be logged by the caller
47250Sstevel@tonic-gate 	 * after the entire branch has been unconfigured.
47260Sstevel@tonic-gate 	 */
47270Sstevel@tonic-gate 	if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
47280Sstevel@tonic-gate 		/*
47290Sstevel@tonic-gate 		 * Instead of logging a separate branch event just add
47300Sstevel@tonic-gate 		 * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
47310Sstevel@tonic-gate 		 * generate a EC_DEV_BRANCH event.
47320Sstevel@tonic-gate 		 */
47330Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_INT32;
47340Sstevel@tonic-gate 		se_val.value.sv_int32 = 1;
47350Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
47360Sstevel@tonic-gate 		    DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
47370Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
47380Sstevel@tonic-gate 			goto fail;
47390Sstevel@tonic-gate 		}
47400Sstevel@tonic-gate 	}
47410Sstevel@tonic-gate 
47420Sstevel@tonic-gate 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
47430Sstevel@tonic-gate 		sysevent_free_attr(ev_attr_list);
47440Sstevel@tonic-gate 		goto fail;
47450Sstevel@tonic-gate 	}
47460Sstevel@tonic-gate 
47470Sstevel@tonic-gate 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
47480Sstevel@tonic-gate 		if (se_err == SE_NO_TRANSPORT)
47490Sstevel@tonic-gate 			no_transport = 1;
47500Sstevel@tonic-gate 		goto fail;
47510Sstevel@tonic-gate 	}
47520Sstevel@tonic-gate 
47530Sstevel@tonic-gate 	sysevent_free(ev);
47540Sstevel@tonic-gate 	return (DDI_SUCCESS);
47550Sstevel@tonic-gate 
47560Sstevel@tonic-gate fail:
47570Sstevel@tonic-gate 	sysevent_free(ev);
47580Sstevel@tonic-gate 	cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_REMOVE event for %s%s",
47590Sstevel@tonic-gate 	    pathname, (no_transport) ? " (syseventd not responding)" : "");
47600Sstevel@tonic-gate 	return (DDI_SUCCESS);
47610Sstevel@tonic-gate }
47620Sstevel@tonic-gate 
47630Sstevel@tonic-gate /*
47640Sstevel@tonic-gate  * log an event that a dev_info branch has been configured or unconfigured.
47650Sstevel@tonic-gate  */
47660Sstevel@tonic-gate static int
47670Sstevel@tonic-gate i_log_devfs_branch(char *node_path, char *subclass)
47680Sstevel@tonic-gate {
47690Sstevel@tonic-gate 	int se_err;
47700Sstevel@tonic-gate 	sysevent_t *ev;
47710Sstevel@tonic-gate 	sysevent_id_t eid;
47720Sstevel@tonic-gate 	sysevent_value_t se_val;
47730Sstevel@tonic-gate 	sysevent_attr_list_t *ev_attr_list = NULL;
47740Sstevel@tonic-gate 	int no_transport = 0;
47750Sstevel@tonic-gate 
47760Sstevel@tonic-gate 	/* do not generate the event during boot */
47770Sstevel@tonic-gate 	if (!i_ddi_io_initialized())
47780Sstevel@tonic-gate 		return (DDI_SUCCESS);
47790Sstevel@tonic-gate 
47800Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, subclass, EP_DDI, SE_SLEEP);
47810Sstevel@tonic-gate 
47820Sstevel@tonic-gate 	se_val.value_type = SE_DATA_TYPE_STRING;
47830Sstevel@tonic-gate 	se_val.value.sv_string = node_path;
47840Sstevel@tonic-gate 
47850Sstevel@tonic-gate 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
47860Sstevel@tonic-gate 	    &se_val, SE_SLEEP) != 0) {
47870Sstevel@tonic-gate 		goto fail;
47880Sstevel@tonic-gate 	}
47890Sstevel@tonic-gate 
47900Sstevel@tonic-gate 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
47910Sstevel@tonic-gate 		sysevent_free_attr(ev_attr_list);
47920Sstevel@tonic-gate 		goto fail;
47930Sstevel@tonic-gate 	}
47940Sstevel@tonic-gate 
47950Sstevel@tonic-gate 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
47960Sstevel@tonic-gate 		if (se_err == SE_NO_TRANSPORT)
47970Sstevel@tonic-gate 			no_transport = 1;
47980Sstevel@tonic-gate 		goto fail;
47990Sstevel@tonic-gate 	}
48000Sstevel@tonic-gate 
48010Sstevel@tonic-gate 	sysevent_free(ev);
48020Sstevel@tonic-gate 	return (DDI_SUCCESS);
48030Sstevel@tonic-gate 
48040Sstevel@tonic-gate fail:
48050Sstevel@tonic-gate 	cmn_err(CE_WARN, "failed to log %s branch event for %s%s",
48060Sstevel@tonic-gate 	    subclass, node_path,
48070Sstevel@tonic-gate 	    (no_transport) ? " (syseventd not responding)" : "");
48080Sstevel@tonic-gate 
48090Sstevel@tonic-gate 	sysevent_free(ev);
48100Sstevel@tonic-gate 	return (DDI_FAILURE);
48110Sstevel@tonic-gate }
48120Sstevel@tonic-gate 
48130Sstevel@tonic-gate /*
48140Sstevel@tonic-gate  * log an event that a dev_info tree branch has been configured.
48150Sstevel@tonic-gate  */
48160Sstevel@tonic-gate static int
48170Sstevel@tonic-gate i_log_devfs_branch_add(dev_info_t *dip)
48180Sstevel@tonic-gate {
48190Sstevel@tonic-gate 	char *node_path;
48200Sstevel@tonic-gate 	int rv;
48210Sstevel@tonic-gate 
48220Sstevel@tonic-gate 	node_path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
48230Sstevel@tonic-gate 	(void) ddi_pathname(dip, node_path);
48240Sstevel@tonic-gate 	rv = i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_ADD);
48250Sstevel@tonic-gate 	kmem_free(node_path, MAXPATHLEN);
48260Sstevel@tonic-gate 
48270Sstevel@tonic-gate 	return (rv);
48280Sstevel@tonic-gate }
48290Sstevel@tonic-gate 
48300Sstevel@tonic-gate /*
48310Sstevel@tonic-gate  * log an event that a dev_info tree branch has been unconfigured.
48320Sstevel@tonic-gate  */
48330Sstevel@tonic-gate static int
48340Sstevel@tonic-gate i_log_devfs_branch_remove(char *node_path)
48350Sstevel@tonic-gate {
48360Sstevel@tonic-gate 	return (i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_REMOVE));
48370Sstevel@tonic-gate }
48380Sstevel@tonic-gate 
48390Sstevel@tonic-gate /*
48400Sstevel@tonic-gate  * enqueue the dip's deviname on the branch event queue.
48410Sstevel@tonic-gate  */
48420Sstevel@tonic-gate static struct brevq_node *
48430Sstevel@tonic-gate brevq_enqueue(struct brevq_node **brevqp, dev_info_t *dip,
48440Sstevel@tonic-gate     struct brevq_node *child)
48450Sstevel@tonic-gate {
48460Sstevel@tonic-gate 	struct brevq_node *brn;
48470Sstevel@tonic-gate 	char *deviname;
48480Sstevel@tonic-gate 
48490Sstevel@tonic-gate 	deviname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
48500Sstevel@tonic-gate 	(void) ddi_deviname(dip, deviname);
48510Sstevel@tonic-gate 
48520Sstevel@tonic-gate 	brn = kmem_zalloc(sizeof (*brn), KM_SLEEP);
48531317Scth 	brn->brn_deviname = i_ddi_strdup(deviname, KM_SLEEP);
48540Sstevel@tonic-gate 	kmem_free(deviname, MAXNAMELEN);
48551317Scth 	brn->brn_child = child;
48561317Scth 	brn->brn_sibling = *brevqp;
48570Sstevel@tonic-gate 	*brevqp = brn;
48580Sstevel@tonic-gate 
48590Sstevel@tonic-gate 	return (brn);
48600Sstevel@tonic-gate }
48610Sstevel@tonic-gate 
48620Sstevel@tonic-gate /*
48630Sstevel@tonic-gate  * free the memory allocated for the elements on the branch event queue.
48640Sstevel@tonic-gate  */
48650Sstevel@tonic-gate static void
48660Sstevel@tonic-gate free_brevq(struct brevq_node *brevq)
48670Sstevel@tonic-gate {
48680Sstevel@tonic-gate 	struct brevq_node *brn, *next_brn;
48690Sstevel@tonic-gate 
48700Sstevel@tonic-gate 	for (brn = brevq; brn != NULL; brn = next_brn) {
48711317Scth 		next_brn = brn->brn_sibling;
48721317Scth 		ASSERT(brn->brn_child == NULL);
48731317Scth 		kmem_free(brn->brn_deviname, strlen(brn->brn_deviname) + 1);
48740Sstevel@tonic-gate 		kmem_free(brn, sizeof (*brn));
48750Sstevel@tonic-gate 	}
48760Sstevel@tonic-gate }
48770Sstevel@tonic-gate 
48780Sstevel@tonic-gate /*
48790Sstevel@tonic-gate  * log the events queued up on the branch event queue and free the
48800Sstevel@tonic-gate  * associated memory.
48810Sstevel@tonic-gate  *
48820Sstevel@tonic-gate  * node_path must have been allocated with at least MAXPATHLEN bytes.
48830Sstevel@tonic-gate  */
48840Sstevel@tonic-gate static void
48850Sstevel@tonic-gate log_and_free_brevq(char *node_path, struct brevq_node *brevq)
48860Sstevel@tonic-gate {
48870Sstevel@tonic-gate 	struct brevq_node *brn;
48880Sstevel@tonic-gate 	char *p;
48890Sstevel@tonic-gate 
48900Sstevel@tonic-gate 	p = node_path + strlen(node_path);
48911317Scth 	for (brn = brevq; brn != NULL; brn = brn->brn_sibling) {
48921317Scth 		(void) strcpy(p, brn->brn_deviname);
48930Sstevel@tonic-gate 		(void) i_log_devfs_branch_remove(node_path);
48940Sstevel@tonic-gate 	}
48950Sstevel@tonic-gate 	*p = '\0';
48960Sstevel@tonic-gate 
48970Sstevel@tonic-gate 	free_brevq(brevq);
48980Sstevel@tonic-gate }
48990Sstevel@tonic-gate 
49000Sstevel@tonic-gate /*
49010Sstevel@tonic-gate  * log the events queued up on the branch event queue and free the
49020Sstevel@tonic-gate  * associated memory. Same as the previous function but operates on dip.
49030Sstevel@tonic-gate  */
49040Sstevel@tonic-gate static void
49050Sstevel@tonic-gate log_and_free_brevq_dip(dev_info_t *dip, struct brevq_node *brevq)
49060Sstevel@tonic-gate {
49070Sstevel@tonic-gate 	char *path;
49080Sstevel@tonic-gate 
49090Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
49100Sstevel@tonic-gate 	(void) ddi_pathname(dip, path);
49110Sstevel@tonic-gate 	log_and_free_brevq(path, brevq);
49120Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
49130Sstevel@tonic-gate }
49140Sstevel@tonic-gate 
49150Sstevel@tonic-gate /*
49160Sstevel@tonic-gate  * log the outstanding branch remove events for the grand children of the dip
49170Sstevel@tonic-gate  * and free the associated memory.
49180Sstevel@tonic-gate  */
49190Sstevel@tonic-gate static void
49200Sstevel@tonic-gate log_and_free_br_events_on_grand_children(dev_info_t *dip,
49210Sstevel@tonic-gate     struct brevq_node *brevq)
49220Sstevel@tonic-gate {
49230Sstevel@tonic-gate 	struct brevq_node *brn;
49240Sstevel@tonic-gate 	char *path;
49250Sstevel@tonic-gate 	char *p;
49260Sstevel@tonic-gate 
49270Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
49280Sstevel@tonic-gate 	(void) ddi_pathname(dip, path);
49290Sstevel@tonic-gate 	p = path + strlen(path);
49301317Scth 	for (brn = brevq; brn != NULL; brn = brn->brn_sibling) {
49311317Scth 		if (brn->brn_child) {
49321317Scth 			(void) strcpy(p, brn->brn_deviname);
49330Sstevel@tonic-gate 			/* now path contains the node path to the dip's child */
49341317Scth 			log_and_free_brevq(path, brn->brn_child);
49351317Scth 			brn->brn_child = NULL;
49360Sstevel@tonic-gate 		}
49370Sstevel@tonic-gate 	}
49380Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
49390Sstevel@tonic-gate }
49400Sstevel@tonic-gate 
49410Sstevel@tonic-gate /*
49420Sstevel@tonic-gate  * log and cleanup branch remove events for the grand children of the dip.
49430Sstevel@tonic-gate  */
49440Sstevel@tonic-gate static void
49450Sstevel@tonic-gate cleanup_br_events_on_grand_children(dev_info_t *dip, struct brevq_node **brevqp)
49460Sstevel@tonic-gate {
49470Sstevel@tonic-gate 	dev_info_t *child;
49480Sstevel@tonic-gate 	struct brevq_node *brevq, *brn, *prev_brn, *next_brn;
49490Sstevel@tonic-gate 	char *path;
49500Sstevel@tonic-gate 	int circ;
49510Sstevel@tonic-gate 
49520Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
49530Sstevel@tonic-gate 	prev_brn = NULL;
49540Sstevel@tonic-gate 	brevq = *brevqp;
49550Sstevel@tonic-gate 
49560Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
49570Sstevel@tonic-gate 	for (brn = brevq; brn != NULL; brn = next_brn) {
49581317Scth 		next_brn = brn->brn_sibling;
49590Sstevel@tonic-gate 		for (child = ddi_get_child(dip); child != NULL;
49600Sstevel@tonic-gate 		    child = ddi_get_next_sibling(child)) {
49610Sstevel@tonic-gate 			if (i_ddi_node_state(child) >= DS_INITIALIZED) {
49620Sstevel@tonic-gate 				(void) ddi_deviname(child, path);
49631317Scth 				if (strcmp(path, brn->brn_deviname) == 0)
49640Sstevel@tonic-gate 					break;
49650Sstevel@tonic-gate 			}
49660Sstevel@tonic-gate 		}
49670Sstevel@tonic-gate 
49680Sstevel@tonic-gate 		if (child != NULL && !(DEVI_EVREMOVE(child))) {
49690Sstevel@tonic-gate 			/*
49700Sstevel@tonic-gate 			 * Event state is not REMOVE. So branch remove event
49711317Scth 			 * is not going be generated on brn->brn_child.
49720Sstevel@tonic-gate 			 * If any branch remove events were queued up on
49731317Scth 			 * brn->brn_child log them and remove the brn
49740Sstevel@tonic-gate 			 * from the queue.
49750Sstevel@tonic-gate 			 */
49761317Scth 			if (brn->brn_child) {
49770Sstevel@tonic-gate 				(void) ddi_pathname(dip, path);
49781317Scth 				(void) strcat(path, brn->brn_deviname);
49791317Scth 				log_and_free_brevq(path, brn->brn_child);
49800Sstevel@tonic-gate 			}
49810Sstevel@tonic-gate 
49820Sstevel@tonic-gate 			if (prev_brn)
49831317Scth 				prev_brn->brn_sibling = next_brn;
49840Sstevel@tonic-gate 			else
49850Sstevel@tonic-gate 				*brevqp = next_brn;
49860Sstevel@tonic-gate 
49871317Scth 			kmem_free(brn->brn_deviname,
49881317Scth 			    strlen(brn->brn_deviname) + 1);
49890Sstevel@tonic-gate 			kmem_free(brn, sizeof (*brn));
49900Sstevel@tonic-gate 		} else {
49910Sstevel@tonic-gate 			/*
49920Sstevel@tonic-gate 			 * Free up the outstanding branch remove events
49931317Scth 			 * queued on brn->brn_child since brn->brn_child
49940Sstevel@tonic-gate 			 * itself is eligible for branch remove event.
49950Sstevel@tonic-gate 			 */
49961317Scth 			if (brn->brn_child) {
49971317Scth 				free_brevq(brn->brn_child);
49981317Scth 				brn->brn_child = NULL;
49990Sstevel@tonic-gate 			}
50000Sstevel@tonic-gate 			prev_brn = brn;
50010Sstevel@tonic-gate 		}
50020Sstevel@tonic-gate 	}
50030Sstevel@tonic-gate 
50040Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
50050Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
50060Sstevel@tonic-gate }
50070Sstevel@tonic-gate 
50080Sstevel@tonic-gate static int
50090Sstevel@tonic-gate need_remove_event(dev_info_t *dip, int flags)
50100Sstevel@tonic-gate {
50110Sstevel@tonic-gate 	if ((flags & (NDI_NO_EVENT | NDI_AUTODETACH)) == 0 &&
50120Sstevel@tonic-gate 	    (flags & (NDI_DEVI_OFFLINE | NDI_UNCONFIG | NDI_DEVI_REMOVE)) &&
50130Sstevel@tonic-gate 	    !(DEVI_EVREMOVE(dip)))
50140Sstevel@tonic-gate 		return (1);
50150Sstevel@tonic-gate 	else
50160Sstevel@tonic-gate 		return (0);
50170Sstevel@tonic-gate }
50180Sstevel@tonic-gate 
50190Sstevel@tonic-gate /*
50200Sstevel@tonic-gate  * Unconfigure children/descendants of the dip.
50210Sstevel@tonic-gate  *
50220Sstevel@tonic-gate  * If the operation involves a branch event NDI_BRANCH_EVENT_OP is set
50230Sstevel@tonic-gate  * through out the unconfiguration. On successful return *brevqp is set to
50240Sstevel@tonic-gate  * a queue of dip's child devinames for which branch remove events need
50250Sstevel@tonic-gate  * to be generated.
50260Sstevel@tonic-gate  */
50270Sstevel@tonic-gate static int
50280Sstevel@tonic-gate devi_unconfig_branch(dev_info_t *dip, dev_info_t **dipp, int flags,
50290Sstevel@tonic-gate     struct brevq_node **brevqp)
50300Sstevel@tonic-gate {
50310Sstevel@tonic-gate 	int rval;
50320Sstevel@tonic-gate 
50330Sstevel@tonic-gate 	*brevqp = NULL;
50340Sstevel@tonic-gate 
50350Sstevel@tonic-gate 	if ((!(flags & NDI_BRANCH_EVENT_OP)) && need_remove_event(dip, flags))
50360Sstevel@tonic-gate 		flags |= NDI_BRANCH_EVENT_OP;
50370Sstevel@tonic-gate 
50380Sstevel@tonic-gate 	if (flags & NDI_BRANCH_EVENT_OP) {
50397009Scth 		rval = devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE,
50400Sstevel@tonic-gate 		    brevqp);
50410Sstevel@tonic-gate 
50420Sstevel@tonic-gate 		if (rval != NDI_SUCCESS && (*brevqp)) {
50430Sstevel@tonic-gate 			log_and_free_brevq_dip(dip, *brevqp);
50440Sstevel@tonic-gate 			*brevqp = NULL;
50450Sstevel@tonic-gate 		}
50460Sstevel@tonic-gate 	} else
50477009Scth 		rval = devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE,
50480Sstevel@tonic-gate 		    NULL);
50490Sstevel@tonic-gate 
50500Sstevel@tonic-gate 	return (rval);
50510Sstevel@tonic-gate }
50520Sstevel@tonic-gate 
50530Sstevel@tonic-gate /*
50540Sstevel@tonic-gate  * If the dip is already bound to a driver transition to DS_INITIALIZED
50550Sstevel@tonic-gate  * in order to generate an event in the case where the node was left in
50560Sstevel@tonic-gate  * DS_BOUND state since boot (never got attached) and the node is now
50570Sstevel@tonic-gate  * being offlined.
50580Sstevel@tonic-gate  */
50590Sstevel@tonic-gate static void
50600Sstevel@tonic-gate init_bound_node_ev(dev_info_t *pdip, dev_info_t *dip, int flags)
50610Sstevel@tonic-gate {
50620Sstevel@tonic-gate 	if (need_remove_event(dip, flags) &&
50630Sstevel@tonic-gate 	    i_ddi_node_state(dip) == DS_BOUND &&
50641333Scth 	    i_ddi_devi_attached(pdip) && !DEVI_IS_DEVICE_OFFLINE(dip))
50650Sstevel@tonic-gate 		(void) ddi_initchild(pdip, dip);
50660Sstevel@tonic-gate }
50670Sstevel@tonic-gate 
50680Sstevel@tonic-gate /*
50690Sstevel@tonic-gate  * attach a node/branch with parent already held busy
50700Sstevel@tonic-gate  */
50710Sstevel@tonic-gate static int
50720Sstevel@tonic-gate devi_attach_node(dev_info_t *dip, uint_t flags)
50730Sstevel@tonic-gate {
50742155Scth 	dev_info_t *pdip = ddi_get_parent(dip);
50752155Scth 
50762155Scth 	ASSERT(pdip && DEVI_BUSY_OWNED(pdip));
50772155Scth 
5078495Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
50790Sstevel@tonic-gate 	if (flags & NDI_DEVI_ONLINE) {
50801333Scth 		if (!i_ddi_devi_attached(dip))
5081495Scth 			DEVI_SET_REPORT(dip);
50820Sstevel@tonic-gate 		DEVI_SET_DEVICE_ONLINE(dip);
50830Sstevel@tonic-gate 	}
50840Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
5085495Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
50860Sstevel@tonic-gate 		return (NDI_FAILURE);
50870Sstevel@tonic-gate 	}
5088495Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
50890Sstevel@tonic-gate 
50900Sstevel@tonic-gate 	if (i_ddi_attachchild(dip) != DDI_SUCCESS) {
5091495Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
50920Sstevel@tonic-gate 		DEVI_SET_EVUNINIT(dip);
5093495Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
5094495Scth 
50950Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(dip))
50960Sstevel@tonic-gate 			(void) ddi_uninitchild(dip);
50970Sstevel@tonic-gate 		else {
50980Sstevel@tonic-gate 			/*
50990Sstevel@tonic-gate 			 * Delete .conf nodes and nodes that are not
51000Sstevel@tonic-gate 			 * well formed.
51010Sstevel@tonic-gate 			 */
51020Sstevel@tonic-gate 			(void) ddi_remove_child(dip, 0);
51030Sstevel@tonic-gate 		}
51040Sstevel@tonic-gate 		return (NDI_FAILURE);
51050Sstevel@tonic-gate 	}
51060Sstevel@tonic-gate 
51070Sstevel@tonic-gate 	i_ndi_devi_report_status_change(dip, NULL);
51080Sstevel@tonic-gate 
51090Sstevel@tonic-gate 	/*
51100Sstevel@tonic-gate 	 * log an event, but not during devfs lookups in which case
51110Sstevel@tonic-gate 	 * NDI_NO_EVENT is set.
51120Sstevel@tonic-gate 	 */
51130Sstevel@tonic-gate 	if ((flags & NDI_NO_EVENT) == 0 && !(DEVI_EVADD(dip))) {
51140Sstevel@tonic-gate 		(void) i_log_devfs_add_devinfo(dip, flags);
5115495Scth 
5116495Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
51170Sstevel@tonic-gate 		DEVI_SET_EVADD(dip);
5118495Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
5119495Scth 	} else if (!(flags & NDI_NO_EVENT_STATE_CHNG)) {
5120495Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
51210Sstevel@tonic-gate 		DEVI_SET_EVADD(dip);
5122495Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
5123495Scth 	}
51240Sstevel@tonic-gate 
51250Sstevel@tonic-gate 	return (NDI_SUCCESS);
51260Sstevel@tonic-gate }
51270Sstevel@tonic-gate 
51280Sstevel@tonic-gate /* internal function to config immediate children */
51290Sstevel@tonic-gate static int
51300Sstevel@tonic-gate config_immediate_children(dev_info_t *pdip, uint_t flags, major_t major)
51310Sstevel@tonic-gate {
51322155Scth 	dev_info_t	*child, *next;
51332155Scth 	int		circ;
51342155Scth 
51351333Scth 	ASSERT(i_ddi_devi_attached(pdip));
51360Sstevel@tonic-gate 
51370Sstevel@tonic-gate 	if (!NEXUS_DRV(ddi_get_driver(pdip)))
51380Sstevel@tonic-gate 		return (NDI_SUCCESS);
51390Sstevel@tonic-gate 
51400Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
51410Sstevel@tonic-gate 	    "config_immediate_children: %s%d (%p), flags=%x\n",
51420Sstevel@tonic-gate 	    ddi_driver_name(pdip), ddi_get_instance(pdip),
51430Sstevel@tonic-gate 	    (void *)pdip, flags));
51440Sstevel@tonic-gate 
51450Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
51460Sstevel@tonic-gate 
51470Sstevel@tonic-gate 	if (flags & NDI_CONFIG_REPROBE) {
51480Sstevel@tonic-gate 		mutex_enter(&DEVI(pdip)->devi_lock);
51490Sstevel@tonic-gate 		DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
51500Sstevel@tonic-gate 		mutex_exit(&DEVI(pdip)->devi_lock);
51510Sstevel@tonic-gate 	}
51520Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(pdip, flags);
51530Sstevel@tonic-gate 	i_ndi_init_hw_children(pdip, flags);
51542155Scth 
51552155Scth 	child = ddi_get_child(pdip);
51562155Scth 	while (child) {
51572155Scth 		/* NOTE: devi_attach_node() may remove the dip */
51582155Scth 		next = ddi_get_next_sibling(child);
51592155Scth 
51602155Scth 		/*
51612155Scth 		 * Configure all nexus nodes or leaf nodes with
51622155Scth 		 * matching driver major
51632155Scth 		 */
51647009Scth 		if ((major == DDI_MAJOR_T_NONE) ||
51652155Scth 		    (major == ddi_driver_major(child)) ||
51662155Scth 		    ((flags & NDI_CONFIG) && (is_leaf_node(child) == 0)))
51672155Scth 			(void) devi_attach_node(child, flags);
51682155Scth 		child = next;
51692155Scth 	}
51700Sstevel@tonic-gate 
51710Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
51720Sstevel@tonic-gate 
51730Sstevel@tonic-gate 	return (NDI_SUCCESS);
51740Sstevel@tonic-gate }
51750Sstevel@tonic-gate 
51760Sstevel@tonic-gate /* internal function to config grand children */
51770Sstevel@tonic-gate static int
51780Sstevel@tonic-gate config_grand_children(dev_info_t *pdip, uint_t flags, major_t major)
51790Sstevel@tonic-gate {
51800Sstevel@tonic-gate 	struct mt_config_handle *hdl;
51810Sstevel@tonic-gate 
51820Sstevel@tonic-gate 	/* multi-threaded configuration of child nexus */
51830Sstevel@tonic-gate 	hdl = mt_config_init(pdip, NULL, flags, major, MT_CONFIG_OP, NULL);
51840Sstevel@tonic-gate 	mt_config_children(hdl);
51850Sstevel@tonic-gate 
51860Sstevel@tonic-gate 	return (mt_config_fini(hdl));	/* wait for threads to exit */
51870Sstevel@tonic-gate }
51880Sstevel@tonic-gate 
51890Sstevel@tonic-gate /*
51900Sstevel@tonic-gate  * Common function for device tree configuration,
51910Sstevel@tonic-gate  * either BUS_CONFIG_ALL or BUS_CONFIG_DRIVER.
51920Sstevel@tonic-gate  * The NDI_CONFIG flag causes recursive configuration of
51930Sstevel@tonic-gate  * grandchildren, devfs usage should not recurse.
51940Sstevel@tonic-gate  */
51950Sstevel@tonic-gate static int
51960Sstevel@tonic-gate devi_config_common(dev_info_t *dip, int flags, major_t major)
51970Sstevel@tonic-gate {
51980Sstevel@tonic-gate 	int error;
51990Sstevel@tonic-gate 	int (*f)();
52000Sstevel@tonic-gate 
52011333Scth 	if (!i_ddi_devi_attached(dip))
52020Sstevel@tonic-gate 		return (NDI_FAILURE);
52030Sstevel@tonic-gate 
52040Sstevel@tonic-gate 	if (pm_pre_config(dip, NULL) != DDI_SUCCESS)
52050Sstevel@tonic-gate 		return (NDI_FAILURE);
52060Sstevel@tonic-gate 
52070Sstevel@tonic-gate 	if ((DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
52080Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
52090Sstevel@tonic-gate 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
52100Sstevel@tonic-gate 		error = config_immediate_children(dip, flags, major);
52110Sstevel@tonic-gate 	} else {
52120Sstevel@tonic-gate 		/* call bus_config entry point */
52137009Scth 		ddi_bus_config_op_t bus_op = (major == DDI_MAJOR_T_NONE) ?
52140Sstevel@tonic-gate 		    BUS_CONFIG_ALL : BUS_CONFIG_DRIVER;
52150Sstevel@tonic-gate 		error = (*f)(dip,
52160Sstevel@tonic-gate 		    flags, bus_op, (void *)(uintptr_t)major, NULL, 0);
52170Sstevel@tonic-gate 	}
52180Sstevel@tonic-gate 
52190Sstevel@tonic-gate 	if (error) {
52200Sstevel@tonic-gate 		pm_post_config(dip, NULL);
52210Sstevel@tonic-gate 		return (error);
52220Sstevel@tonic-gate 	}
52230Sstevel@tonic-gate 
52240Sstevel@tonic-gate 	/*
52250Sstevel@tonic-gate 	 * Some callers, notably SCSI, need to mark the devfs cache
52260Sstevel@tonic-gate 	 * to be rebuilt together with the config operation.
52270Sstevel@tonic-gate 	 */
52280Sstevel@tonic-gate 	if (flags & NDI_DEVFS_CLEAN)
52290Sstevel@tonic-gate 		(void) devfs_clean(dip, NULL, 0);
52300Sstevel@tonic-gate 
52310Sstevel@tonic-gate 	if (flags & NDI_CONFIG)
52320Sstevel@tonic-gate 		(void) config_grand_children(dip, flags, major);
52330Sstevel@tonic-gate 
52340Sstevel@tonic-gate 	pm_post_config(dip, NULL);
52350Sstevel@tonic-gate 
52360Sstevel@tonic-gate 	return (NDI_SUCCESS);
52370Sstevel@tonic-gate }
52380Sstevel@tonic-gate 
52390Sstevel@tonic-gate /*
52400Sstevel@tonic-gate  * Framework entry point for BUS_CONFIG_ALL
52410Sstevel@tonic-gate  */
52420Sstevel@tonic-gate int
52430Sstevel@tonic-gate ndi_devi_config(dev_info_t *dip, int flags)
52440Sstevel@tonic-gate {
52450Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
52460Sstevel@tonic-gate 	    "ndi_devi_config: par = %s%d (%p), flags = 0x%x\n",
52470Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
52480Sstevel@tonic-gate 
52497009Scth 	return (devi_config_common(dip, flags, DDI_MAJOR_T_NONE));
52500Sstevel@tonic-gate }
52510Sstevel@tonic-gate 
52520Sstevel@tonic-gate /*
52530Sstevel@tonic-gate  * Framework entry point for BUS_CONFIG_DRIVER, bound to major
52540Sstevel@tonic-gate  */
52550Sstevel@tonic-gate int
52560Sstevel@tonic-gate ndi_devi_config_driver(dev_info_t *dip, int flags, major_t major)
52570Sstevel@tonic-gate {
52580Sstevel@tonic-gate 	/* don't abuse this function */
52597009Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
52600Sstevel@tonic-gate 
52610Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
52620Sstevel@tonic-gate 	    "ndi_devi_config_driver: par = %s%d (%p), flags = 0x%x\n",
52630Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
52640Sstevel@tonic-gate 
52650Sstevel@tonic-gate 	return (devi_config_common(dip, flags, major));
52660Sstevel@tonic-gate }
52670Sstevel@tonic-gate 
52680Sstevel@tonic-gate /*
52692155Scth  * Called by nexus drivers to configure its children.
52700Sstevel@tonic-gate  */
52710Sstevel@tonic-gate static int
52722155Scth devi_config_one(dev_info_t *pdip, char *devnm, dev_info_t **cdipp,
52730Sstevel@tonic-gate     uint_t flags, clock_t timeout)
52740Sstevel@tonic-gate {
52752155Scth 	dev_info_t	*vdip = NULL;
52762155Scth 	char		*drivername = NULL;
52774145Scth 	int		find_by_addr = 0;
52782155Scth 	char		*name, *addr;
52792155Scth 	int		v_circ, p_circ;
52802155Scth 	clock_t		end_time;	/* 60 sec */
52812155Scth 	int		probed;
52822155Scth 	dev_info_t	*cdip;
52832155Scth 	mdi_pathinfo_t	*cpip;
52842155Scth 
52852155Scth 	*cdipp = NULL;
52860Sstevel@tonic-gate 
52870Sstevel@tonic-gate 	if (!NEXUS_DRV(ddi_get_driver(pdip)))
52880Sstevel@tonic-gate 		return (NDI_FAILURE);
52890Sstevel@tonic-gate 
52900Sstevel@tonic-gate 	/* split name into "name@addr" parts */
52910Sstevel@tonic-gate 	i_ddi_parse_name(devnm, &name, &addr, NULL);
52920Sstevel@tonic-gate 
52932155Scth 	/*
52942155Scth 	 * If the nexus is a pHCI and we are not processing a pHCI from
52952155Scth 	 * mdi bus_config code then we need to know the vHCI.
52962155Scth 	 */
52972155Scth 	if (MDI_PHCI(pdip))
52982155Scth 		vdip = mdi_devi_get_vdip(pdip);
52992155Scth 
53002155Scth 	/*
53012155Scth 	 * We may have a genericname on a system that creates drivername
53022155Scth 	 * nodes (from .conf files).  Find the drivername by nodeid. If we
53032155Scth 	 * can't find a node with devnm as the node name then we search by
53042155Scth 	 * drivername.  This allows an implementation to supply a genericly
53055742Scth 	 * named boot path (disk) and locate drivename nodes (sd).  The
53065742Scth 	 * NDI_PROMNAME flag does not apply to /devices/pseudo paths.
53072155Scth 	 */
53085742Scth 	if ((flags & NDI_PROMNAME) && (pdip != pseudo_dip)) {
53092009Sdm120769 		drivername = child_path_to_driver(pdip, name, addr);
53104145Scth 		find_by_addr = 1;
53114145Scth 	}
53122155Scth 
53132155Scth 	/*
53142155Scth 	 * Determine end_time: This routine should *not* be called with a
53152155Scth 	 * constant non-zero timeout argument, the caller should be adjusting
53162155Scth 	 * the timeout argument relative to when it *started* its asynchronous
53172155Scth 	 * enumeration.
53182155Scth 	 */
53192155Scth 	if (timeout > 0)
53202009Sdm120769 		end_time = ddi_get_lbolt() + timeout;
53212155Scth 
53222009Sdm120769 	for (;;) {
53231961Scth 		/*
53242155Scth 		 * For pHCI, enter (vHCI, pHCI) and search for pathinfo/client
53252155Scth 		 * child - break out of for(;;) loop if child found.
53262155Scth 		 * NOTE: Lock order for ndi_devi_enter is (vHCI, pHCI).
53272155Scth 		 */
53282155Scth 		if (vdip) {
53292155Scth 			/* use mdi_devi_enter ordering */
53302155Scth 			ndi_devi_enter(vdip, &v_circ);
53312155Scth 			ndi_devi_enter(pdip, &p_circ);
53322155Scth 			cpip = mdi_pi_find(pdip, NULL, addr);
53332155Scth 			cdip = mdi_pi_get_client(cpip);
53342155Scth 			if (cdip)
53352155Scth 				break;
53362155Scth 		} else
53372155Scth 			ndi_devi_enter(pdip, &p_circ);
53382155Scth 
53392155Scth 		/*
53402155Scth 		 * When not a  vHCI or not all pHCI devices are required to
53412155Scth 		 * enumerated under the vHCI (NDI_MDI_FALLBACK) search for
53422155Scth 		 * devinfo child.
53430Sstevel@tonic-gate 		 */
53442155Scth 		if ((vdip == NULL) || (flags & NDI_MDI_FALLBACK)) {
53452155Scth 			/* determine if .conf nodes already built */
53462155Scth 			probed = (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
53472155Scth 
53482155Scth 			/*
53492155Scth 			 * Search for child by name, if not found then search
53502155Scth 			 * for a node bound to the drivername driver with the
53512155Scth 			 * specified "@addr". Break out of for(;;) loop if
53524145Scth 			 * child found.  To support path-oriented aliases
53534145Scth 			 * binding on boot-device, we do a search_by_addr too.
53542155Scth 			 */
53552155Scth again:			(void) i_ndi_make_spec_children(pdip, flags);
53562155Scth 			cdip = find_child_by_name(pdip, name, addr);
53572155Scth 			if ((cdip == NULL) && drivername)
53582155Scth 				cdip = find_child_by_driver(pdip,
53592155Scth 				    drivername, addr);
53604145Scth 			if ((cdip == NULL) && find_by_addr)
53614145Scth 				cdip = find_child_by_addr(pdip, addr);
53622155Scth 			if (cdip)
53632155Scth 				break;
53642155Scth 
53652155Scth 			/*
53662155Scth 			 * determine if we should reenumerate .conf nodes
53672155Scth 			 * and look for child again.
53682155Scth 			 */
53692155Scth 			if (probed &&
53702155Scth 			    i_ddi_io_initialized() &&
53712155Scth 			    (flags & NDI_CONFIG_REPROBE) &&
53722155Scth 			    ((timeout <= 0) || (ddi_get_lbolt() >= end_time))) {
53732155Scth 				probed = 0;
53742155Scth 				mutex_enter(&DEVI(pdip)->devi_lock);
53752155Scth 				DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
53762155Scth 				mutex_exit(&DEVI(pdip)->devi_lock);
53772155Scth 				goto again;
53782155Scth 			}
53792155Scth 		}
53802155Scth 
53812155Scth 		/* break out of for(;;) if time expired */
53822155Scth 		if ((timeout <= 0) || (ddi_get_lbolt() >= end_time))
53830Sstevel@tonic-gate 			break;
53840Sstevel@tonic-gate 
53850Sstevel@tonic-gate 		/*
53862155Scth 		 * Child not found, exit and wait for asynchronous enumeration
53872155Scth 		 * to add child (or timeout). The addition of a new child (vhci
53882155Scth 		 * or phci) requires the asynchronous enumeration thread to
53892155Scth 		 * ndi_devi_enter/ndi_devi_exit. This exit will signal devi_cv
53902155Scth 		 * and cause us to return from ndi_devi_exit_and_wait, after
53912155Scth 		 * which we loop and search for the requested child again.
53920Sstevel@tonic-gate 		 */
53930Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT,
53940Sstevel@tonic-gate 		    "%s%d: waiting for child %s@%s, timeout %ld",
53950Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
53960Sstevel@tonic-gate 		    name, addr, timeout));
53972155Scth 		if (vdip) {
53982155Scth 			/*
53992155Scth 			 * Mark vHCI for pHCI ndi_devi_exit broadcast.
54002155Scth 			 */
54012155Scth 			mutex_enter(&DEVI(vdip)->devi_lock);
54022155Scth 			DEVI(vdip)->devi_flags |=
54032155Scth 			    DEVI_PHCI_SIGNALS_VHCI;
54042155Scth 			mutex_exit(&DEVI(vdip)->devi_lock);
54052155Scth 			ndi_devi_exit(pdip, p_circ);
54062155Scth 
54072155Scth 			/*
54082155Scth 			 * NB: There is a small race window from above
54092155Scth 			 * ndi_devi_exit() of pdip to cv_wait() in
54102155Scth 			 * ndi_devi_exit_and_wait() which can result in
54112155Scth 			 * not immediately finding a new pHCI child
54122155Scth 			 * of a pHCI that uses NDI_MDI_FAILBACK.
54132155Scth 			 */
54142155Scth 			ndi_devi_exit_and_wait(vdip, v_circ, end_time);
54152155Scth 		} else {
54162155Scth 			ndi_devi_exit_and_wait(pdip, p_circ, end_time);
54172155Scth 		}
54182155Scth 	}
54192155Scth 
54202155Scth 	/* done with paddr, fixup i_ddi_parse_name '@'->'\0' change */
54212155Scth 	if (addr && *addr != '\0')
54220Sstevel@tonic-gate 		*(addr - 1) = '@';
54230Sstevel@tonic-gate 
54242155Scth 	/* attach and hold the child, returning pointer to child */
54252155Scth 	if (cdip && (devi_attach_node(cdip, flags) == NDI_SUCCESS)) {
54262155Scth 		ndi_hold_devi(cdip);
54272155Scth 		*cdipp = cdip;
54282155Scth 	}
54292155Scth 
54302155Scth 	ndi_devi_exit(pdip, p_circ);
54312155Scth 	if (vdip)
54322155Scth 		ndi_devi_exit(vdip, v_circ);
54332155Scth 	return (*cdipp ? NDI_SUCCESS : NDI_FAILURE);
54340Sstevel@tonic-gate }
54350Sstevel@tonic-gate 
54360Sstevel@tonic-gate /*
54370Sstevel@tonic-gate  * Enumerate and attach a child specified by name 'devnm'.
54380Sstevel@tonic-gate  * Called by devfs lookup and DR to perform a BUS_CONFIG_ONE.
54390Sstevel@tonic-gate  * Note: devfs does not make use of NDI_CONFIG to configure
54400Sstevel@tonic-gate  * an entire branch.
54410Sstevel@tonic-gate  */
54420Sstevel@tonic-gate int
54430Sstevel@tonic-gate ndi_devi_config_one(dev_info_t *dip, char *devnm, dev_info_t **dipp, int flags)
54440Sstevel@tonic-gate {
54450Sstevel@tonic-gate 	int error;
54460Sstevel@tonic-gate 	int (*f)();
54470Sstevel@tonic-gate 	int branch_event = 0;
54480Sstevel@tonic-gate 
54490Sstevel@tonic-gate 	ASSERT(dipp);
54501333Scth 	ASSERT(i_ddi_devi_attached(dip));
54510Sstevel@tonic-gate 
54520Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
54530Sstevel@tonic-gate 	    "ndi_devi_config_one: par = %s%d (%p), child = %s\n",
54540Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, devnm));
54550Sstevel@tonic-gate 
54560Sstevel@tonic-gate 	if (pm_pre_config(dip, devnm) != DDI_SUCCESS)
54570Sstevel@tonic-gate 		return (NDI_FAILURE);
54580Sstevel@tonic-gate 
54590Sstevel@tonic-gate 	if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
54600Sstevel@tonic-gate 	    (flags & NDI_CONFIG)) {
54610Sstevel@tonic-gate 		flags |= NDI_BRANCH_EVENT_OP;
54620Sstevel@tonic-gate 		branch_event = 1;
54630Sstevel@tonic-gate 	}
54640Sstevel@tonic-gate 
54650Sstevel@tonic-gate 	if ((DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
54660Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
54670Sstevel@tonic-gate 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
54680Sstevel@tonic-gate 		error = devi_config_one(dip, devnm, dipp, flags, 0);
54690Sstevel@tonic-gate 	} else {
54700Sstevel@tonic-gate 		/* call bus_config entry point */
54710Sstevel@tonic-gate 		error = (*f)(dip, flags, BUS_CONFIG_ONE, (void *)devnm, dipp);
54720Sstevel@tonic-gate 	}
54730Sstevel@tonic-gate 
54740Sstevel@tonic-gate 	if (error || (flags & NDI_CONFIG) == 0) {
54750Sstevel@tonic-gate 		pm_post_config(dip, devnm);
54760Sstevel@tonic-gate 		return (error);
54770Sstevel@tonic-gate 	}
54780Sstevel@tonic-gate 
54790Sstevel@tonic-gate 	/*
54804145Scth 	 * DR usage (i.e. call with NDI_CONFIG) recursively configures
54810Sstevel@tonic-gate 	 * grandchildren, performing a BUS_CONFIG_ALL from the node attached
54820Sstevel@tonic-gate 	 * by the BUS_CONFIG_ONE.
54830Sstevel@tonic-gate 	 */
54840Sstevel@tonic-gate 	ASSERT(*dipp);
54850Sstevel@tonic-gate 
54867009Scth 	error = devi_config_common(*dipp, flags, DDI_MAJOR_T_NONE);
54870Sstevel@tonic-gate 
54880Sstevel@tonic-gate 	pm_post_config(dip, devnm);
54890Sstevel@tonic-gate 
54900Sstevel@tonic-gate 	if (branch_event)
54910Sstevel@tonic-gate 		(void) i_log_devfs_branch_add(*dipp);
54920Sstevel@tonic-gate 
54930Sstevel@tonic-gate 	return (error);
54940Sstevel@tonic-gate }
54950Sstevel@tonic-gate 
54960Sstevel@tonic-gate 
54970Sstevel@tonic-gate /*
54980Sstevel@tonic-gate  * Enumerate and attach a child specified by name 'devnm'.
54990Sstevel@tonic-gate  * Called during configure the OBP options. This configures
55000Sstevel@tonic-gate  * only one node.
55010Sstevel@tonic-gate  */
55020Sstevel@tonic-gate static int
55030Sstevel@tonic-gate ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
55040Sstevel@tonic-gate     dev_info_t **childp, int flags)
55050Sstevel@tonic-gate {
55060Sstevel@tonic-gate 	int error;
55070Sstevel@tonic-gate 	int (*f)();
55080Sstevel@tonic-gate 
55090Sstevel@tonic-gate 	ASSERT(childp);
55101333Scth 	ASSERT(i_ddi_devi_attached(parent));
55110Sstevel@tonic-gate 
55120Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_config_obp_args: "
55130Sstevel@tonic-gate 	    "par = %s%d (%p), child = %s\n", ddi_driver_name(parent),
55140Sstevel@tonic-gate 	    ddi_get_instance(parent), (void *)parent, devnm));
55150Sstevel@tonic-gate 
55160Sstevel@tonic-gate 	if ((DEVI(parent)->devi_ops->devo_bus_ops == NULL) ||
55170Sstevel@tonic-gate 	    (DEVI(parent)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
55180Sstevel@tonic-gate 	    (f = DEVI(parent)->devi_ops->devo_bus_ops->bus_config) == NULL) {
55190Sstevel@tonic-gate 		error = NDI_FAILURE;
55200Sstevel@tonic-gate 	} else {
55210Sstevel@tonic-gate 		/* call bus_config entry point */
55220Sstevel@tonic-gate 		error = (*f)(parent, flags,
55230Sstevel@tonic-gate 		    BUS_CONFIG_OBP_ARGS, (void *)devnm, childp);
55240Sstevel@tonic-gate 	}
55250Sstevel@tonic-gate 	return (error);
55260Sstevel@tonic-gate }
55270Sstevel@tonic-gate 
55284845Svikram /*
55294845Svikram  * Pay attention, the following is a bit tricky:
55304845Svikram  * There are three possible cases when constraints are applied
55314845Svikram  *
55324845Svikram  *	- A constraint is applied and the offline is disallowed.
55334845Svikram  *	  Simply return failure and block the offline
55344845Svikram  *
55354845Svikram  *	- A constraint is applied and the offline is allowed.
55364845Svikram  *	  Mark the dip as having passed the constraint and allow
55374845Svikram  *	  offline to proceed.
55384845Svikram  *
55394845Svikram  *	- A constraint is not applied. Allow the offline to proceed for now.
55404845Svikram  *
55414845Svikram  * In the latter two cases we allow the offline to proceed. If the
55424845Svikram  * offline succeeds (no users) everything is fine. It is ok for an unused
55434845Svikram  * device to be offlined even if no constraints were imposed on the offline.
55444845Svikram  * If the offline fails because there are users, we look at the constraint
55454845Svikram  * flag on the dip. If the constraint flag is set (implying that it passed
55464845Svikram  * a constraint) we allow the dip to be retired. If not, we don't allow
55474845Svikram  * the retire. This ensures that we don't allow unconstrained retire.
55484845Svikram  */
55494845Svikram int
55504845Svikram e_ddi_offline_notify(dev_info_t *dip)
55514845Svikram {
55524845Svikram 	int retval;
55534845Svikram 	int constraint;
55544845Svikram 	int failure;
55554845Svikram 
55564845Svikram 	RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): entered: dip=%p",
55574845Svikram 	    (void *) dip));
55584845Svikram 
55594845Svikram 	constraint = 0;
55604845Svikram 	failure = 0;
55614845Svikram 
55624845Svikram 	/*
55634845Svikram 	 * Start with userland constraints first - applied via device contracts
55644845Svikram 	 */
55654845Svikram 	retval = contract_device_offline(dip, DDI_DEV_T_ANY, 0);
55664845Svikram 	switch (retval) {
55674845Svikram 	case CT_NACK:
55684845Svikram 		RIO_DEBUG((CE_NOTE, "Received NACK for dip=%p", (void *)dip));
55694845Svikram 		failure = 1;
55704845Svikram 		goto out;
55714845Svikram 	case CT_ACK:
55724845Svikram 		constraint = 1;
55734845Svikram 		RIO_DEBUG((CE_NOTE, "Received ACK for dip=%p", (void *)dip));
55744845Svikram 		break;
55754845Svikram 	case CT_NONE:
55764845Svikram 		/* no contracts */
55774845Svikram 		RIO_DEBUG((CE_NOTE, "No contracts on dip=%p", (void *)dip));
55784845Svikram 		break;
55794845Svikram 	default:
55804845Svikram 		ASSERT(retval == CT_NONE);
55814845Svikram 	}
55824845Svikram 
55834845Svikram 	/*
55844845Svikram 	 * Next, use LDI to impose kernel constraints
55854845Svikram 	 */
55864845Svikram 	retval = ldi_invoke_notify(dip, DDI_DEV_T_ANY, 0, LDI_EV_OFFLINE, NULL);
55874845Svikram 	switch (retval) {
55884845Svikram 	case LDI_EV_FAILURE:
55894845Svikram 		contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_FAILURE);
55904845Svikram 		RIO_DEBUG((CE_NOTE, "LDI callback failed on dip=%p",
55914845Svikram 		    (void *)dip));
55924845Svikram 		failure = 1;
55934845Svikram 		goto out;
55944845Svikram 	case LDI_EV_SUCCESS:
55954845Svikram 		constraint = 1;
55964845Svikram 		RIO_DEBUG((CE_NOTE, "LDI callback success on dip=%p",
55974845Svikram 		    (void *)dip));
55984845Svikram 		break;
55994845Svikram 	case LDI_EV_NONE:
56004845Svikram 		/* no matching LDI callbacks */
56014845Svikram 		RIO_DEBUG((CE_NOTE, "No LDI callbacks for dip=%p",
56024845Svikram 		    (void *)dip));
56034845Svikram 		break;
56044845Svikram 	default:
56054845Svikram 		ASSERT(retval == LDI_EV_NONE);
56064845Svikram 	}
56074845Svikram 
56084845Svikram out:
56094845Svikram 	mutex_enter(&(DEVI(dip)->devi_lock));
56104845Svikram 	if ((DEVI(dip)->devi_flags & DEVI_RETIRING) && failure) {
56114845Svikram 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): setting "
56124845Svikram 		    "BLOCKED flag. dip=%p", (void *)dip));
56134845Svikram 		DEVI(dip)->devi_flags |= DEVI_R_BLOCKED;
56144845Svikram 		if (DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT) {
56154845Svikram 			RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): "
56164845Svikram 			    "blocked. clearing RCM CONSTRAINT flag. dip=%p",
56174845Svikram 			    (void *)dip));
56184845Svikram 			DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
56194845Svikram 		}
56204845Svikram 	} else if ((DEVI(dip)->devi_flags & DEVI_RETIRING) && constraint) {
56214845Svikram 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): setting "
56224845Svikram 		    "CONSTRAINT flag. dip=%p", (void *)dip));
56234845Svikram 		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
56244845Svikram 	} else if ((DEVI(dip)->devi_flags & DEVI_RETIRING) &&
56254845Svikram 	    DEVI(dip)->devi_ref == 0) {
56264845Svikram 		/* also allow retire if device is not in use */
56274845Svikram 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): device not in "
56284845Svikram 		    "use. Setting CONSTRAINT flag. dip=%p", (void *)dip));
56294845Svikram 		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
56304845Svikram 	} else {
56314845Svikram 		/*
56324845Svikram 		 * Note: We cannot ASSERT here that DEVI_R_CONSTRAINT is
56334845Svikram 		 * not set, since other sources (such as RCM) may have
56344845Svikram 		 * set the flag.
56354845Svikram 		 */
56364845Svikram 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): not setting "
56374845Svikram 		    "constraint flag. dip=%p", (void *)dip));
56384845Svikram 	}
56394845Svikram 	mutex_exit(&(DEVI(dip)->devi_lock));
56404845Svikram 
56414845Svikram 
56424845Svikram 	RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): exit: dip=%p",
56434845Svikram 	    (void *) dip));
56444845Svikram 
56454845Svikram 	return (failure ? DDI_FAILURE : DDI_SUCCESS);
56464845Svikram }
56474845Svikram 
56484845Svikram void
56494845Svikram e_ddi_offline_finalize(dev_info_t *dip, int result)
56504845Svikram {
56514845Svikram 	RIO_DEBUG((CE_NOTE, "e_ddi_offline_finalize(): entry: result=%s, "
56524845Svikram 	    "dip=%p", result == DDI_SUCCESS ? "SUCCESS" : "FAILURE",
56534845Svikram 	    (void *)dip));
56544845Svikram 
56554845Svikram 	contract_device_negend(dip, DDI_DEV_T_ANY, 0,  result == DDI_SUCCESS ?
56564845Svikram 	    CT_EV_SUCCESS : CT_EV_FAILURE);
56574845Svikram 
56584845Svikram 	ldi_invoke_finalize(dip, DDI_DEV_T_ANY, 0,
56594845Svikram 	    LDI_EV_OFFLINE, result == DDI_SUCCESS ?
56604845Svikram 	    LDI_EV_SUCCESS : LDI_EV_FAILURE, NULL);
56614845Svikram 
56624845Svikram 	RIO_VERBOSE((CE_NOTE, "e_ddi_offline_finalize(): exit: dip=%p",
56634845Svikram 	    (void *)dip));
56644845Svikram }
56654845Svikram 
56664845Svikram void
56674845Svikram e_ddi_degrade_finalize(dev_info_t *dip)
56684845Svikram {
56694845Svikram 	RIO_DEBUG((CE_NOTE, "e_ddi_degrade_finalize(): entry: "
56704845Svikram 	    "result always = DDI_SUCCESS, dip=%p", (void *)dip));
56714845Svikram 
56724845Svikram 	contract_device_degrade(dip, DDI_DEV_T_ANY, 0);
56734845Svikram 	contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_SUCCESS);
56744845Svikram 
56754845Svikram 	ldi_invoke_finalize(dip, DDI_DEV_T_ANY, 0, LDI_EV_DEGRADE,
56764845Svikram 	    LDI_EV_SUCCESS, NULL);
56774845Svikram 
56784845Svikram 	RIO_VERBOSE((CE_NOTE, "e_ddi_degrade_finalize(): exit: dip=%p",
56794845Svikram 	    (void *)dip));
56804845Svikram }
56814845Svikram 
56824845Svikram void
56834845Svikram e_ddi_undegrade_finalize(dev_info_t *dip)
56844845Svikram {
56854845Svikram 	RIO_DEBUG((CE_NOTE, "e_ddi_undegrade_finalize(): entry: "
56864845Svikram 	    "result always = DDI_SUCCESS, dip=%p", (void *)dip));
56874845Svikram 
56884845Svikram 	contract_device_undegrade(dip, DDI_DEV_T_ANY, 0);
56894845Svikram 	contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_SUCCESS);
56904845Svikram 
56914845Svikram 	RIO_VERBOSE((CE_NOTE, "e_ddi_undegrade_finalize(): exit: dip=%p",
56924845Svikram 	    (void *)dip));
56934845Svikram }
56940Sstevel@tonic-gate 
56950Sstevel@tonic-gate /*
56960Sstevel@tonic-gate  * detach a node with parent already held busy
56970Sstevel@tonic-gate  */
56980Sstevel@tonic-gate static int
56990Sstevel@tonic-gate devi_detach_node(dev_info_t *dip, uint_t flags)
57000Sstevel@tonic-gate {
57010Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
57020Sstevel@tonic-gate 	int ret = NDI_SUCCESS;
57030Sstevel@tonic-gate 	ddi_eventcookie_t cookie;
57048640SVikram.Hegde@Sun.COM 	char *path = NULL;
57058640SVikram.Hegde@Sun.COM 	char *class = NULL;
57068640SVikram.Hegde@Sun.COM 	char *driver = NULL;
57078640SVikram.Hegde@Sun.COM 	int instance = -1;
57088640SVikram.Hegde@Sun.COM 	int post_event = 0;
57090Sstevel@tonic-gate 
57102155Scth 	ASSERT(pdip && DEVI_BUSY_OWNED(pdip));
57112155Scth 
57124845Svikram 	/*
57134845Svikram 	 * Invoke notify if offlining
57144845Svikram 	 */
57154845Svikram 	if (flags & NDI_DEVI_OFFLINE) {
57164845Svikram 		RIO_DEBUG((CE_NOTE, "devi_detach_node: offlining dip=%p",
57174845Svikram 		    (void *)dip));
57184845Svikram 		if (e_ddi_offline_notify(dip) != DDI_SUCCESS) {
57194845Svikram 			RIO_DEBUG((CE_NOTE, "devi_detach_node: offline NACKed"
57204845Svikram 			    "dip=%p", (void *)dip));
57214845Svikram 			return (NDI_FAILURE);
57224845Svikram 		}
57234845Svikram 	}
57244845Svikram 
57250Sstevel@tonic-gate 	if (flags & NDI_POST_EVENT) {
57262155Scth 		if (i_ddi_devi_attached(pdip)) {
57270Sstevel@tonic-gate 			if (ddi_get_eventcookie(dip, DDI_DEVI_REMOVE_EVENT,
57280Sstevel@tonic-gate 			    &cookie) == NDI_SUCCESS)
57290Sstevel@tonic-gate 				(void) ndi_post_event(dip, dip, cookie, NULL);
57300Sstevel@tonic-gate 		}
57310Sstevel@tonic-gate 	}
57320Sstevel@tonic-gate 
57334845Svikram 	if (i_ddi_detachchild(dip, flags) != DDI_SUCCESS) {
57344845Svikram 		if (flags & NDI_DEVI_OFFLINE) {
57354845Svikram 			RIO_DEBUG((CE_NOTE, "devi_detach_node: offline failed."
57364845Svikram 			    " Calling e_ddi_offline_finalize with result=%d. "
57374845Svikram 			    "dip=%p", DDI_FAILURE, (void *)dip));
57384845Svikram 			e_ddi_offline_finalize(dip, DDI_FAILURE);
57394845Svikram 		}
57400Sstevel@tonic-gate 		return (NDI_FAILURE);
57414845Svikram 	}
57424845Svikram 
57434845Svikram 	if (flags & NDI_DEVI_OFFLINE) {
57444845Svikram 		RIO_DEBUG((CE_NOTE, "devi_detach_node: offline succeeded."
57454845Svikram 		    " Calling e_ddi_offline_finalize with result=%d, "
57464845Svikram 		    "dip=%p", DDI_SUCCESS, (void *)dip));
57474845Svikram 		e_ddi_offline_finalize(dip, DDI_SUCCESS);
57484845Svikram 	}
57490Sstevel@tonic-gate 
57500Sstevel@tonic-gate 	if (flags & NDI_AUTODETACH)
57510Sstevel@tonic-gate 		return (NDI_SUCCESS);
57520Sstevel@tonic-gate 
57530Sstevel@tonic-gate 	/*
57540Sstevel@tonic-gate 	 * For DR, even bound nodes may need to have offline
57550Sstevel@tonic-gate 	 * flag set.
57560Sstevel@tonic-gate 	 */
57570Sstevel@tonic-gate 	if (flags & NDI_DEVI_OFFLINE) {
5758495Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
57590Sstevel@tonic-gate 		DEVI_SET_DEVICE_OFFLINE(dip);
5760495Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
57610Sstevel@tonic-gate 	}
57620Sstevel@tonic-gate 
57630Sstevel@tonic-gate 	if (i_ddi_node_state(dip) == DS_INITIALIZED) {
57648640SVikram.Hegde@Sun.COM 		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
57650Sstevel@tonic-gate 		(void) ddi_pathname(dip, path);
57660Sstevel@tonic-gate 		if (flags & NDI_DEVI_OFFLINE)
57670Sstevel@tonic-gate 			i_ndi_devi_report_status_change(dip, path);
57680Sstevel@tonic-gate 
57690Sstevel@tonic-gate 		if (need_remove_event(dip, flags)) {
57708640SVikram.Hegde@Sun.COM 			post_event = 1;
57718640SVikram.Hegde@Sun.COM 			class = i_ddi_strdup(i_ddi_devi_class(dip), KM_SLEEP);
57728640SVikram.Hegde@Sun.COM 			driver = i_ddi_strdup((char *)ddi_driver_name(dip),
57738640SVikram.Hegde@Sun.COM 			    KM_SLEEP);
57748640SVikram.Hegde@Sun.COM 			instance = ddi_get_instance(dip);
57758640SVikram.Hegde@Sun.COM 
5776495Scth 			mutex_enter(&(DEVI(dip)->devi_lock));
57770Sstevel@tonic-gate 			DEVI_SET_EVREMOVE(dip);
5778495Scth 			mutex_exit(&(DEVI(dip)->devi_lock));
57790Sstevel@tonic-gate 		}
57800Sstevel@tonic-gate 	}
57810Sstevel@tonic-gate 
57820Sstevel@tonic-gate 	if (flags & (NDI_UNCONFIG | NDI_DEVI_REMOVE)) {
57830Sstevel@tonic-gate 		ret = ddi_uninitchild(dip);
57840Sstevel@tonic-gate 		if (ret == NDI_SUCCESS) {
57850Sstevel@tonic-gate 			/*
57860Sstevel@tonic-gate 			 * Remove uninitialized pseudo nodes because
57870Sstevel@tonic-gate 			 * system props are lost and the node cannot be
57880Sstevel@tonic-gate 			 * reattached.
57890Sstevel@tonic-gate 			 */
57900Sstevel@tonic-gate 			if (!ndi_dev_is_persistent_node(dip))
57910Sstevel@tonic-gate 				flags |= NDI_DEVI_REMOVE;
57920Sstevel@tonic-gate 
57938640SVikram.Hegde@Sun.COM 			if (flags & NDI_DEVI_REMOVE) {
57940Sstevel@tonic-gate 				ret = ddi_remove_child(dip, 0);
57958640SVikram.Hegde@Sun.COM 				if (post_event && ret == NDI_SUCCESS) {
57968640SVikram.Hegde@Sun.COM 					(void) i_log_devfs_remove_devinfo(path,
57978640SVikram.Hegde@Sun.COM 					    class, driver, instance, flags);
57988640SVikram.Hegde@Sun.COM 				}
57998640SVikram.Hegde@Sun.COM 			}
58008640SVikram.Hegde@Sun.COM 
58010Sstevel@tonic-gate 		}
58020Sstevel@tonic-gate 	}
58030Sstevel@tonic-gate 
58048640SVikram.Hegde@Sun.COM 	if (path)
58058640SVikram.Hegde@Sun.COM 		kmem_free(path, MAXPATHLEN);
58068640SVikram.Hegde@Sun.COM 	if (class)
58078640SVikram.Hegde@Sun.COM 		kmem_free(class, strlen(class) + 1);
58088640SVikram.Hegde@Sun.COM 	if (driver)
58098640SVikram.Hegde@Sun.COM 		kmem_free(driver, strlen(driver) + 1);
58108640SVikram.Hegde@Sun.COM 
58110Sstevel@tonic-gate 	return (ret);
58120Sstevel@tonic-gate }
58130Sstevel@tonic-gate 
58140Sstevel@tonic-gate /*
58150Sstevel@tonic-gate  * unconfigure immediate children of bus nexus device
58160Sstevel@tonic-gate  */
58170Sstevel@tonic-gate static int
58180Sstevel@tonic-gate unconfig_immediate_children(
58190Sstevel@tonic-gate 	dev_info_t *dip,
58200Sstevel@tonic-gate 	dev_info_t **dipp,
58210Sstevel@tonic-gate 	int flags,
58220Sstevel@tonic-gate 	major_t major)
58230Sstevel@tonic-gate {
58242155Scth 	int rv = NDI_SUCCESS;
58252155Scth 	int circ, vcirc;
58260Sstevel@tonic-gate 	dev_info_t *child;
58272155Scth 	dev_info_t *vdip = NULL;
58282155Scth 	dev_info_t *next;
58290Sstevel@tonic-gate 
58300Sstevel@tonic-gate 	ASSERT(dipp == NULL || *dipp == NULL);
58310Sstevel@tonic-gate 
58322155Scth 	/*
58332155Scth 	 * Scan forward to see if we will be processing a pHCI child. If we
58342155Scth 	 * have a child that is a pHCI and vHCI and pHCI are not siblings then
58352155Scth 	 * enter vHCI before parent(pHCI) to prevent deadlock with mpxio
58362155Scth 	 * Client power management operations.
58372155Scth 	 */
58380Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
58392155Scth 	for (child = ddi_get_child(dip); child;
58402155Scth 	    child = ddi_get_next_sibling(child)) {
58412155Scth 		/* skip same nodes we skip below */
58427009Scth 		if (((major != DDI_MAJOR_T_NONE) &&
58432155Scth 		    (major != ddi_driver_major(child))) ||
58442155Scth 		    ((flags & NDI_AUTODETACH) && !is_leaf_node(child)))
58452155Scth 			continue;
58462155Scth 
58472155Scth 		if (MDI_PHCI(child)) {
58482155Scth 			vdip = mdi_devi_get_vdip(child);
58492155Scth 			/*
58502155Scth 			 * If vHCI and vHCI is not a sibling of pHCI
58512155Scth 			 * then enter in (vHCI, parent(pHCI)) order.
58522155Scth 			 */
58532155Scth 			if (vdip && (ddi_get_parent(vdip) != dip)) {
58542155Scth 				ndi_devi_exit(dip, circ);
58552155Scth 
58562155Scth 				/* use mdi_devi_enter ordering */
58572155Scth 				ndi_devi_enter(vdip, &vcirc);
58582155Scth 				ndi_devi_enter(dip, &circ);
58592155Scth 				break;
58602155Scth 			} else
58612155Scth 				vdip = NULL;
58622155Scth 		}
58632155Scth 	}
58642155Scth 
58650Sstevel@tonic-gate 	child = ddi_get_child(dip);
58660Sstevel@tonic-gate 	while (child) {
58672155Scth 		next = ddi_get_next_sibling(child);
58682155Scth 
58697009Scth 		if ((major != DDI_MAJOR_T_NONE) &&
58700Sstevel@tonic-gate 		    (major != ddi_driver_major(child))) {
58710Sstevel@tonic-gate 			child = next;
58720Sstevel@tonic-gate 			continue;
58730Sstevel@tonic-gate 		}
58740Sstevel@tonic-gate 
58750Sstevel@tonic-gate 		/* skip nexus nodes during autodetach */
58760Sstevel@tonic-gate 		if ((flags & NDI_AUTODETACH) && !is_leaf_node(child)) {
58770Sstevel@tonic-gate 			child = next;
58780Sstevel@tonic-gate 			continue;
58790Sstevel@tonic-gate 		}
58800Sstevel@tonic-gate 
58810Sstevel@tonic-gate 		if (devi_detach_node(child, flags) != NDI_SUCCESS) {
58820Sstevel@tonic-gate 			if (dipp && *dipp == NULL) {
58830Sstevel@tonic-gate 				ndi_hold_devi(child);
58840Sstevel@tonic-gate 				*dipp = child;
58850Sstevel@tonic-gate 			}
58860Sstevel@tonic-gate 			rv = NDI_FAILURE;
58870Sstevel@tonic-gate 		}
58880Sstevel@tonic-gate 
58890Sstevel@tonic-gate 		/*
58900Sstevel@tonic-gate 		 * Continue upon failure--best effort algorithm
58910Sstevel@tonic-gate 		 */
58920Sstevel@tonic-gate 		child = next;
58930Sstevel@tonic-gate 	}
58942155Scth 
58950Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
58962155Scth 	if (vdip)
58972155Scth 		ndi_devi_exit(vdip, vcirc);
58982155Scth 
58990Sstevel@tonic-gate 	return (rv);
59000Sstevel@tonic-gate }
59010Sstevel@tonic-gate 
59020Sstevel@tonic-gate /*
59030Sstevel@tonic-gate  * unconfigure grand children of bus nexus device
59040Sstevel@tonic-gate  */
59050Sstevel@tonic-gate static int
59060Sstevel@tonic-gate unconfig_grand_children(
59070Sstevel@tonic-gate 	dev_info_t *dip,
59080Sstevel@tonic-gate 	dev_info_t **dipp,
59090Sstevel@tonic-gate 	int flags,
59100Sstevel@tonic-gate 	major_t major,
59110Sstevel@tonic-gate 	struct brevq_node **brevqp)
59120Sstevel@tonic-gate {
59130Sstevel@tonic-gate 	struct mt_config_handle *hdl;
59140Sstevel@tonic-gate 
59150Sstevel@tonic-gate 	if (brevqp)
59160Sstevel@tonic-gate 		*brevqp = NULL;
59170Sstevel@tonic-gate 
59180Sstevel@tonic-gate 	/* multi-threaded configuration of child nexus */
59190Sstevel@tonic-gate 	hdl = mt_config_init(dip, dipp, flags, major, MT_UNCONFIG_OP, brevqp);
59200Sstevel@tonic-gate 	mt_config_children(hdl);
59210Sstevel@tonic-gate 
59220Sstevel@tonic-gate 	return (mt_config_fini(hdl));	/* wait for threads to exit */
59230Sstevel@tonic-gate }
59240Sstevel@tonic-gate 
59250Sstevel@tonic-gate /*
59260Sstevel@tonic-gate  * Unconfigure children/descendants of the dip.
59270Sstevel@tonic-gate  *
59280Sstevel@tonic-gate  * If brevqp is not NULL, on return *brevqp is set to a queue of dip's
59290Sstevel@tonic-gate  * child devinames for which branch remove events need to be generated.
59300Sstevel@tonic-gate  */
59310Sstevel@tonic-gate static int
59320Sstevel@tonic-gate devi_unconfig_common(
59330Sstevel@tonic-gate 	dev_info_t *dip,
59340Sstevel@tonic-gate 	dev_info_t **dipp,
59350Sstevel@tonic-gate 	int flags,
59360Sstevel@tonic-gate 	major_t major,
59370Sstevel@tonic-gate 	struct brevq_node **brevqp)
59380Sstevel@tonic-gate {
59390Sstevel@tonic-gate 	int rv;
59400Sstevel@tonic-gate 	int pm_cookie;
59410Sstevel@tonic-gate 	int (*f)();
59420Sstevel@tonic-gate 	ddi_bus_config_op_t bus_op;
59430Sstevel@tonic-gate 
59440Sstevel@tonic-gate 	if (dipp)
59450Sstevel@tonic-gate 		*dipp = NULL;
59460Sstevel@tonic-gate 	if (brevqp)
59470Sstevel@tonic-gate 		*brevqp = NULL;
59480Sstevel@tonic-gate 
59490Sstevel@tonic-gate 	/*
59500Sstevel@tonic-gate 	 * Power up the dip if it is powered off.  If the flag bit
59510Sstevel@tonic-gate 	 * NDI_AUTODETACH is set and the dip is not at its full power,
59520Sstevel@tonic-gate 	 * skip the rest of the branch.
59530Sstevel@tonic-gate 	 */
59540Sstevel@tonic-gate 	if (pm_pre_unconfig(dip, flags, &pm_cookie, NULL) != DDI_SUCCESS)
59550Sstevel@tonic-gate 		return ((flags & NDI_AUTODETACH) ? NDI_SUCCESS :
59560Sstevel@tonic-gate 		    NDI_FAILURE);
59570Sstevel@tonic-gate 
59580Sstevel@tonic-gate 	/*
59590Sstevel@tonic-gate 	 * Some callers, notably SCSI, need to clear out the devfs
59600Sstevel@tonic-gate 	 * cache together with the unconfig to prevent stale entries.
59610Sstevel@tonic-gate 	 */
59620Sstevel@tonic-gate 	if (flags & NDI_DEVFS_CLEAN)
59630Sstevel@tonic-gate 		(void) devfs_clean(dip, NULL, 0);
59640Sstevel@tonic-gate 
59650Sstevel@tonic-gate 	rv = unconfig_grand_children(dip, dipp, flags, major, brevqp);
59660Sstevel@tonic-gate 
59670Sstevel@tonic-gate 	if ((rv != NDI_SUCCESS) && ((flags & NDI_AUTODETACH) == 0)) {
59680Sstevel@tonic-gate 		if (brevqp && *brevqp) {
59690Sstevel@tonic-gate 			log_and_free_br_events_on_grand_children(dip, *brevqp);
59700Sstevel@tonic-gate 			free_brevq(*brevqp);
59710Sstevel@tonic-gate 			*brevqp = NULL;
59720Sstevel@tonic-gate 		}
59730Sstevel@tonic-gate 		pm_post_unconfig(dip, pm_cookie, NULL);
59740Sstevel@tonic-gate 		return (rv);
59750Sstevel@tonic-gate 	}
59760Sstevel@tonic-gate 
59770Sstevel@tonic-gate 	if (dipp && *dipp) {
59780Sstevel@tonic-gate 		ndi_rele_devi(*dipp);
59790Sstevel@tonic-gate 		*dipp = NULL;
59800Sstevel@tonic-gate 	}
59810Sstevel@tonic-gate 
59820Sstevel@tonic-gate 	/*
59830Sstevel@tonic-gate 	 * It is possible to have a detached nexus with children
59840Sstevel@tonic-gate 	 * and grandchildren (for example: a branch consisting
59850Sstevel@tonic-gate 	 * entirely of bound nodes.) Since the nexus is detached
59860Sstevel@tonic-gate 	 * the bus_unconfig entry point cannot be used to remove
59870Sstevel@tonic-gate 	 * or unconfigure the descendants.
59880Sstevel@tonic-gate 	 */
59891333Scth 	if (!i_ddi_devi_attached(dip) ||
59900Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
59910Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
59920Sstevel@tonic-gate 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
59930Sstevel@tonic-gate 		rv = unconfig_immediate_children(dip, dipp, flags, major);
59940Sstevel@tonic-gate 	} else {
59950Sstevel@tonic-gate 		/*
59960Sstevel@tonic-gate 		 * call bus_unconfig entry point
59970Sstevel@tonic-gate 		 * It should reset nexus flags if unconfigure succeeds.
59980Sstevel@tonic-gate 		 */
59997009Scth 		bus_op = (major == DDI_MAJOR_T_NONE) ?
60000Sstevel@tonic-gate 		    BUS_UNCONFIG_ALL : BUS_UNCONFIG_DRIVER;
60010Sstevel@tonic-gate 		rv = (*f)(dip, flags, bus_op, (void *)(uintptr_t)major);
60020Sstevel@tonic-gate 	}
60030Sstevel@tonic-gate 
60040Sstevel@tonic-gate 	pm_post_unconfig(dip, pm_cookie, NULL);
60050Sstevel@tonic-gate 
60060Sstevel@tonic-gate 	if (brevqp && *brevqp)
60070Sstevel@tonic-gate 		cleanup_br_events_on_grand_children(dip, brevqp);
60080Sstevel@tonic-gate 
60090Sstevel@tonic-gate 	return (rv);
60100Sstevel@tonic-gate }
60110Sstevel@tonic-gate 
60120Sstevel@tonic-gate /*
60130Sstevel@tonic-gate  * called by devfs/framework to unconfigure children bound to major
60140Sstevel@tonic-gate  * If NDI_AUTODETACH is specified, this is invoked by either the
60150Sstevel@tonic-gate  * moduninstall daemon or the modunload -i 0 command.
60160Sstevel@tonic-gate  */
60170Sstevel@tonic-gate int
60180Sstevel@tonic-gate ndi_devi_unconfig_driver(dev_info_t *dip, int flags, major_t major)
60190Sstevel@tonic-gate {
60200Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
60210Sstevel@tonic-gate 	    "ndi_devi_unconfig_driver: par = %s%d (%p), flags = 0x%x\n",
60220Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
60230Sstevel@tonic-gate 
60240Sstevel@tonic-gate 	return (devi_unconfig_common(dip, NULL, flags, major, NULL));
60250Sstevel@tonic-gate }
60260Sstevel@tonic-gate 
60270Sstevel@tonic-gate int
60280Sstevel@tonic-gate ndi_devi_unconfig(dev_info_t *dip, int flags)
60290Sstevel@tonic-gate {
60300Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
60310Sstevel@tonic-gate 	    "ndi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
60320Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
60330Sstevel@tonic-gate 
60347009Scth 	return (devi_unconfig_common(dip, NULL, flags, DDI_MAJOR_T_NONE, NULL));
60350Sstevel@tonic-gate }
60360Sstevel@tonic-gate 
60370Sstevel@tonic-gate int
60380Sstevel@tonic-gate e_ddi_devi_unconfig(dev_info_t *dip, dev_info_t **dipp, int flags)
60390Sstevel@tonic-gate {
60400Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
60410Sstevel@tonic-gate 	    "e_ddi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
60420Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
60430Sstevel@tonic-gate 
60447009Scth 	return (devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE, NULL));
60450Sstevel@tonic-gate }
60460Sstevel@tonic-gate 
60470Sstevel@tonic-gate /*
60480Sstevel@tonic-gate  * Unconfigure child by name
60490Sstevel@tonic-gate  */
60500Sstevel@tonic-gate static int
60510Sstevel@tonic-gate devi_unconfig_one(dev_info_t *pdip, char *devnm, int flags)
60520Sstevel@tonic-gate {
60532155Scth 	int		rv, circ;
60542155Scth 	dev_info_t	*child;
60552155Scth 	dev_info_t	*vdip = NULL;
60562155Scth 	int		v_circ;
60570Sstevel@tonic-gate 
60580Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
60590Sstevel@tonic-gate 	child = ndi_devi_findchild(pdip, devnm);
60602155Scth 
60612155Scth 	/*
60622155Scth 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
60632155Scth 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
60642155Scth 	 * management operations.
60652155Scth 	 */
60662155Scth 	if (child && MDI_PHCI(child)) {
60672155Scth 		vdip = mdi_devi_get_vdip(child);
60682155Scth 		if (vdip && (ddi_get_parent(vdip) != pdip)) {
60692155Scth 			ndi_devi_exit(pdip, circ);
60702155Scth 
60712155Scth 			/* use mdi_devi_enter ordering */
60722155Scth 			ndi_devi_enter(vdip, &v_circ);
60732155Scth 			ndi_devi_enter(pdip, &circ);
60742155Scth 			child = ndi_devi_findchild(pdip, devnm);
60752155Scth 		} else
60762155Scth 			vdip = NULL;
60772155Scth 	}
60782155Scth 
60792155Scth 	if (child) {
60802155Scth 		rv = devi_detach_node(child, flags);
60812155Scth 	} else {
60820Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
60830Sstevel@tonic-gate 		    "devi_unconfig_one: %s not found\n", devnm));
60842155Scth 		rv = NDI_SUCCESS;
60852155Scth 	}
60862155Scth 
60870Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
60882155Scth 	if (vdip)
60897235Svikram 		ndi_devi_exit(vdip, v_circ);
60902155Scth 
60910Sstevel@tonic-gate 	return (rv);
60920Sstevel@tonic-gate }
60930Sstevel@tonic-gate 
60940Sstevel@tonic-gate int
60950Sstevel@tonic-gate ndi_devi_unconfig_one(
60960Sstevel@tonic-gate 	dev_info_t *pdip,
60970Sstevel@tonic-gate 	char *devnm,
60980Sstevel@tonic-gate 	dev_info_t **dipp,
60990Sstevel@tonic-gate 	int flags)
61000Sstevel@tonic-gate {
61012155Scth 	int		(*f)();
61022155Scth 	int		circ, rv;
61032155Scth 	int		pm_cookie;
61042155Scth 	dev_info_t	*child;
61052155Scth 	dev_info_t	*vdip = NULL;
61062155Scth 	int		v_circ;
61070Sstevel@tonic-gate 	struct brevq_node *brevq = NULL;
61080Sstevel@tonic-gate 
61091333Scth 	ASSERT(i_ddi_devi_attached(pdip));
61100Sstevel@tonic-gate 
61110Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
61120Sstevel@tonic-gate 	    "ndi_devi_unconfig_one: par = %s%d (%p), child = %s\n",
61130Sstevel@tonic-gate 	    ddi_driver_name(pdip), ddi_get_instance(pdip),
61140Sstevel@tonic-gate 	    (void *)pdip, devnm));
61150Sstevel@tonic-gate 
61160Sstevel@tonic-gate 	if (pm_pre_unconfig(pdip, flags, &pm_cookie, devnm) != DDI_SUCCESS)
61170Sstevel@tonic-gate 		return (NDI_FAILURE);
61180Sstevel@tonic-gate 
61190Sstevel@tonic-gate 	if (dipp)
61200Sstevel@tonic-gate 		*dipp = NULL;
61210Sstevel@tonic-gate 
61220Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
61230Sstevel@tonic-gate 	child = ndi_devi_findchild(pdip, devnm);
61242155Scth 
61252155Scth 	/*
61262155Scth 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
61272155Scth 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
61282155Scth 	 * management operations.
61292155Scth 	 */
61302155Scth 	if (child && MDI_PHCI(child)) {
61312155Scth 		vdip = mdi_devi_get_vdip(child);
61322155Scth 		if (vdip && (ddi_get_parent(vdip) != pdip)) {
61332155Scth 			ndi_devi_exit(pdip, circ);
61342155Scth 
61352155Scth 			/* use mdi_devi_enter ordering */
61362155Scth 			ndi_devi_enter(vdip, &v_circ);
61372155Scth 			ndi_devi_enter(pdip, &circ);
61382155Scth 			child = ndi_devi_findchild(pdip, devnm);
61392155Scth 		} else
61402155Scth 			vdip = NULL;
61412155Scth 	}
61422155Scth 
61430Sstevel@tonic-gate 	if (child == NULL) {
61440Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_unconfig_one: %s"
61450Sstevel@tonic-gate 		    " not found\n", devnm));
61462155Scth 		rv = NDI_SUCCESS;
61472155Scth 		goto out;
61480Sstevel@tonic-gate 	}
61490Sstevel@tonic-gate 
61500Sstevel@tonic-gate 	/*
61510Sstevel@tonic-gate 	 * Unconfigure children/descendants of named child
61520Sstevel@tonic-gate 	 */
61530Sstevel@tonic-gate 	rv = devi_unconfig_branch(child, dipp, flags | NDI_UNCONFIG, &brevq);
61540Sstevel@tonic-gate 	if (rv != NDI_SUCCESS)
61550Sstevel@tonic-gate 		goto out;
61560Sstevel@tonic-gate 
61570Sstevel@tonic-gate 	init_bound_node_ev(pdip, child, flags);
61580Sstevel@tonic-gate 
61590Sstevel@tonic-gate 	if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
61600Sstevel@tonic-gate 	    (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
61610Sstevel@tonic-gate 	    (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
61620Sstevel@tonic-gate 		rv = devi_detach_node(child, flags);
61630Sstevel@tonic-gate 	} else {
61640Sstevel@tonic-gate 		/* call bus_config entry point */
61650Sstevel@tonic-gate 		rv = (*f)(pdip, flags, BUS_UNCONFIG_ONE, (void *)devnm);
61660Sstevel@tonic-gate 	}
61670Sstevel@tonic-gate 
61680Sstevel@tonic-gate 	if (brevq) {
61690Sstevel@tonic-gate 		if (rv != NDI_SUCCESS)
61700Sstevel@tonic-gate 			log_and_free_brevq_dip(child, brevq);
61710Sstevel@tonic-gate 		else
61720Sstevel@tonic-gate 			free_brevq(brevq);
61730Sstevel@tonic-gate 	}
61740Sstevel@tonic-gate 
61750Sstevel@tonic-gate 	if (dipp && rv != NDI_SUCCESS) {
61760Sstevel@tonic-gate 		ndi_hold_devi(child);
61770Sstevel@tonic-gate 		ASSERT(*dipp == NULL);
61780Sstevel@tonic-gate 		*dipp = child;
61790Sstevel@tonic-gate 	}
61800Sstevel@tonic-gate 
61810Sstevel@tonic-gate out:
61820Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
61832155Scth 	if (vdip)
61847235Svikram 		ndi_devi_exit(vdip, v_circ);
61852155Scth 
61860Sstevel@tonic-gate 	pm_post_unconfig(pdip, pm_cookie, devnm);
61870Sstevel@tonic-gate 
61880Sstevel@tonic-gate 	return (rv);
61890Sstevel@tonic-gate }
61900Sstevel@tonic-gate 
61910Sstevel@tonic-gate struct async_arg {
61920Sstevel@tonic-gate 	dev_info_t *dip;
61930Sstevel@tonic-gate 	uint_t flags;
61940Sstevel@tonic-gate };
61950Sstevel@tonic-gate 
61960Sstevel@tonic-gate /*
61970Sstevel@tonic-gate  * Common async handler for:
61980Sstevel@tonic-gate  *	ndi_devi_bind_driver_async
61990Sstevel@tonic-gate  *	ndi_devi_online_async
62000Sstevel@tonic-gate  */
62010Sstevel@tonic-gate static int
62020Sstevel@tonic-gate i_ndi_devi_async_common(dev_info_t *dip, uint_t flags, void (*func)())
62030Sstevel@tonic-gate {
62040Sstevel@tonic-gate 	int tqflag;
62050Sstevel@tonic-gate 	int kmflag;
62060Sstevel@tonic-gate 	struct async_arg *arg;
62070Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
62080Sstevel@tonic-gate 
62090Sstevel@tonic-gate 	ASSERT(pdip);
62100Sstevel@tonic-gate 	ASSERT(DEVI(pdip)->devi_taskq);
62110Sstevel@tonic-gate 	ASSERT(ndi_dev_is_persistent_node(dip));
62120Sstevel@tonic-gate 
62130Sstevel@tonic-gate 	if (flags & NDI_NOSLEEP) {
62140Sstevel@tonic-gate 		kmflag = KM_NOSLEEP;
62150Sstevel@tonic-gate 		tqflag = TQ_NOSLEEP;
62160Sstevel@tonic-gate 	} else {
62170Sstevel@tonic-gate 		kmflag = KM_SLEEP;
62180Sstevel@tonic-gate 		tqflag = TQ_SLEEP;
62190Sstevel@tonic-gate 	}
62200Sstevel@tonic-gate 
62210Sstevel@tonic-gate 	arg = kmem_alloc(sizeof (*arg), kmflag);
62220Sstevel@tonic-gate 	if (arg == NULL)
62230Sstevel@tonic-gate 		goto fail;
62240Sstevel@tonic-gate 
62250Sstevel@tonic-gate 	arg->flags = flags;
62260Sstevel@tonic-gate 	arg->dip = dip;
62270Sstevel@tonic-gate 	if (ddi_taskq_dispatch(DEVI(pdip)->devi_taskq, func, arg, tqflag) ==
62280Sstevel@tonic-gate 	    DDI_SUCCESS) {
62290Sstevel@tonic-gate 		return (NDI_SUCCESS);
62300Sstevel@tonic-gate 	}
62310Sstevel@tonic-gate 
62320Sstevel@tonic-gate fail:
62330Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "%s%d: ddi_taskq_dispatch failed",
62340Sstevel@tonic-gate 	    ddi_driver_name(pdip), ddi_get_instance(pdip)));
62350Sstevel@tonic-gate 
62360Sstevel@tonic-gate 	if (arg)
62370Sstevel@tonic-gate 		kmem_free(arg, sizeof (*arg));
62380Sstevel@tonic-gate 	return (NDI_FAILURE);
62390Sstevel@tonic-gate }
62400Sstevel@tonic-gate 
62410Sstevel@tonic-gate static void
62420Sstevel@tonic-gate i_ndi_devi_bind_driver_cb(struct async_arg *arg)
62430Sstevel@tonic-gate {
62440Sstevel@tonic-gate 	(void) ndi_devi_bind_driver(arg->dip, arg->flags);
62450Sstevel@tonic-gate 	kmem_free(arg, sizeof (*arg));
62460Sstevel@tonic-gate }
62470Sstevel@tonic-gate 
62480Sstevel@tonic-gate int
62490Sstevel@tonic-gate ndi_devi_bind_driver_async(dev_info_t *dip, uint_t flags)
62500Sstevel@tonic-gate {
62510Sstevel@tonic-gate 	return (i_ndi_devi_async_common(dip, flags,
62520Sstevel@tonic-gate 	    (void (*)())i_ndi_devi_bind_driver_cb));
62530Sstevel@tonic-gate }
62540Sstevel@tonic-gate 
62550Sstevel@tonic-gate /*
62560Sstevel@tonic-gate  * place the devinfo in the ONLINE state.
62570Sstevel@tonic-gate  */
62580Sstevel@tonic-gate int
62590Sstevel@tonic-gate ndi_devi_online(dev_info_t *dip, uint_t flags)
62600Sstevel@tonic-gate {
62610Sstevel@tonic-gate 	int circ, rv;
62620Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
62630Sstevel@tonic-gate 	int branch_event = 0;
62640Sstevel@tonic-gate 
62650Sstevel@tonic-gate 	ASSERT(pdip);
62660Sstevel@tonic-gate 
62670Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_online: %s%d (%p)\n",
62684950Scth 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
62690Sstevel@tonic-gate 
62700Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
62710Sstevel@tonic-gate 	/* bind child before merging .conf nodes */
62720Sstevel@tonic-gate 	rv = i_ndi_config_node(dip, DS_BOUND, flags);
62730Sstevel@tonic-gate 	if (rv != NDI_SUCCESS) {
62740Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
62750Sstevel@tonic-gate 		return (rv);
62760Sstevel@tonic-gate 	}
62770Sstevel@tonic-gate 
62780Sstevel@tonic-gate 	/* merge .conf properties */
62790Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(pdip, flags);
62800Sstevel@tonic-gate 
62812009Sdm120769 	flags |= (NDI_DEVI_ONLINE | NDI_CONFIG);
62820Sstevel@tonic-gate 
62830Sstevel@tonic-gate 	if (flags & NDI_NO_EVENT) {
62840Sstevel@tonic-gate 		/*
62850Sstevel@tonic-gate 		 * Caller is specifically asking for not to generate an event.
62860Sstevel@tonic-gate 		 * Set the following flag so that devi_attach_node() don't
62870Sstevel@tonic-gate 		 * change the event state.
62880Sstevel@tonic-gate 		 */
62890Sstevel@tonic-gate 		flags |= NDI_NO_EVENT_STATE_CHNG;
62900Sstevel@tonic-gate 	}
62910Sstevel@tonic-gate 
62920Sstevel@tonic-gate 	if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
62930Sstevel@tonic-gate 	    ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip))) {
62940Sstevel@tonic-gate 		flags |= NDI_BRANCH_EVENT_OP;
62950Sstevel@tonic-gate 		branch_event = 1;
62960Sstevel@tonic-gate 	}
62970Sstevel@tonic-gate 
62980Sstevel@tonic-gate 	/*
62990Sstevel@tonic-gate 	 * devi_attach_node() may remove dip on failure
63000Sstevel@tonic-gate 	 */
63010Sstevel@tonic-gate 	if ((rv = devi_attach_node(dip, flags)) == NDI_SUCCESS) {
63020Sstevel@tonic-gate 		if ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip)) {
63030Sstevel@tonic-gate 			(void) ndi_devi_config(dip, flags);
63040Sstevel@tonic-gate 		}
63050Sstevel@tonic-gate 
63060Sstevel@tonic-gate 		if (branch_event)
63070Sstevel@tonic-gate 			(void) i_log_devfs_branch_add(dip);
63080Sstevel@tonic-gate 	}
63090Sstevel@tonic-gate 
63100Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
63110Sstevel@tonic-gate 
63120Sstevel@tonic-gate 	/*
63130Sstevel@tonic-gate 	 * Notify devfs that we have a new node. Devfs needs to invalidate
63140Sstevel@tonic-gate 	 * cached directory contents.
63150Sstevel@tonic-gate 	 *
63160Sstevel@tonic-gate 	 * For PCMCIA devices, it is possible the pdip is not fully
63170Sstevel@tonic-gate 	 * attached. In this case, calling back into devfs will
63180Sstevel@tonic-gate 	 * result in a loop or assertion error. Hence, the check
63190Sstevel@tonic-gate 	 * on node state.
63200Sstevel@tonic-gate 	 *
63210Sstevel@tonic-gate 	 * If we own parent lock, this is part of a branch operation.
63220Sstevel@tonic-gate 	 * We skip the devfs_clean() step because the cache invalidation
63230Sstevel@tonic-gate 	 * is done higher up in the device tree.
63240Sstevel@tonic-gate 	 */
63251333Scth 	if (rv == NDI_SUCCESS && i_ddi_devi_attached(pdip) &&
63260Sstevel@tonic-gate 	    !DEVI_BUSY_OWNED(pdip))
63270Sstevel@tonic-gate 		(void) devfs_clean(pdip, NULL, 0);
63280Sstevel@tonic-gate 	return (rv);
63290Sstevel@tonic-gate }
63300Sstevel@tonic-gate 
63310Sstevel@tonic-gate static void
63320Sstevel@tonic-gate i_ndi_devi_online_cb(struct async_arg *arg)
63330Sstevel@tonic-gate {
63340Sstevel@tonic-gate 	(void) ndi_devi_online(arg->dip, arg->flags);
63350Sstevel@tonic-gate 	kmem_free(arg, sizeof (*arg));
63360Sstevel@tonic-gate }
63370Sstevel@tonic-gate 
63380Sstevel@tonic-gate int
63390Sstevel@tonic-gate ndi_devi_online_async(dev_info_t *dip, uint_t flags)
63400Sstevel@tonic-gate {
63410Sstevel@tonic-gate 	/* mark child as need config if requested. */
6342495Scth 	if (flags & NDI_CONFIG) {
6343495Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
63440Sstevel@tonic-gate 		DEVI_SET_NDI_CONFIG(dip);
6345495Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
6346495Scth 	}
63470Sstevel@tonic-gate 
63480Sstevel@tonic-gate 	return (i_ndi_devi_async_common(dip, flags,
63490Sstevel@tonic-gate 	    (void (*)())i_ndi_devi_online_cb));
63500Sstevel@tonic-gate }
63510Sstevel@tonic-gate 
63520Sstevel@tonic-gate /*
63530Sstevel@tonic-gate  * Take a device node Offline
63540Sstevel@tonic-gate  * To take a device Offline means to detach the device instance from
63550Sstevel@tonic-gate  * the driver and prevent devfs requests from re-attaching the device
63560Sstevel@tonic-gate  * instance.
63570Sstevel@tonic-gate  *
63580Sstevel@tonic-gate  * The flag NDI_DEVI_REMOVE causes removes the device node from
63590Sstevel@tonic-gate  * the driver list and the device tree. In this case, the device
63600Sstevel@tonic-gate  * is assumed to be removed from the system.
63610Sstevel@tonic-gate  */
63620Sstevel@tonic-gate int
63630Sstevel@tonic-gate ndi_devi_offline(dev_info_t *dip, uint_t flags)
63640Sstevel@tonic-gate {
63652155Scth 	int		circ, rval = 0;
63662155Scth 	dev_info_t	*pdip = ddi_get_parent(dip);
63672155Scth 	dev_info_t	*vdip = NULL;
63682155Scth 	int		v_circ;
63690Sstevel@tonic-gate 	struct brevq_node *brevq = NULL;
63700Sstevel@tonic-gate 
63710Sstevel@tonic-gate 	ASSERT(pdip);
63720Sstevel@tonic-gate 
63730Sstevel@tonic-gate 	flags |= NDI_DEVI_OFFLINE;
63742155Scth 
63752155Scth 	/*
63762155Scth 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
63772155Scth 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
63782155Scth 	 * management operations.
63792155Scth 	 */
63802155Scth 	if (MDI_PHCI(dip)) {
63812155Scth 		vdip = mdi_devi_get_vdip(dip);
63822155Scth 		if (vdip && (ddi_get_parent(vdip) != pdip))
63832155Scth 			ndi_devi_enter(vdip, &v_circ);
63842155Scth 		else
63852155Scth 			vdip = NULL;
63862155Scth 	}
63870Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
63882155Scth 
63890Sstevel@tonic-gate 	if (i_ddi_node_state(dip) == DS_READY) {
63900Sstevel@tonic-gate 		/*
63910Sstevel@tonic-gate 		 * If dip is in DS_READY state, there may be cached dv_nodes
63920Sstevel@tonic-gate 		 * referencing this dip, so we invoke devfs code path.
63930Sstevel@tonic-gate 		 * Note that we must release busy changing on pdip to
63940Sstevel@tonic-gate 		 * avoid deadlock against devfs.
63950Sstevel@tonic-gate 		 */
63960Sstevel@tonic-gate 		char *devname = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
63970Sstevel@tonic-gate 		(void) ddi_deviname(dip, devname);
63982155Scth 
63990Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
64002155Scth 		if (vdip)
64012155Scth 			ndi_devi_exit(vdip, v_circ);
64020Sstevel@tonic-gate 
64030Sstevel@tonic-gate 		/*
64040Sstevel@tonic-gate 		 * If we own parent lock, this is part of a branch
64050Sstevel@tonic-gate 		 * operation. We skip the devfs_clean() step.
64060Sstevel@tonic-gate 		 */
64070Sstevel@tonic-gate 		if (!DEVI_BUSY_OWNED(pdip))
64084411Svikram 			(void) devfs_clean(pdip, devname + 1, DV_CLEAN_FORCE);
64090Sstevel@tonic-gate 		kmem_free(devname, MAXNAMELEN + 1);
64100Sstevel@tonic-gate 
64114411Svikram 		rval = devi_unconfig_branch(dip, NULL, flags|NDI_UNCONFIG,
64124411Svikram 		    &brevq);
64134411Svikram 
64140Sstevel@tonic-gate 		if (rval)
64150Sstevel@tonic-gate 			return (NDI_FAILURE);
64160Sstevel@tonic-gate 
64172155Scth 		if (vdip)
64182155Scth 			ndi_devi_enter(vdip, &v_circ);
64190Sstevel@tonic-gate 		ndi_devi_enter(pdip, &circ);
64200Sstevel@tonic-gate 	}
64210Sstevel@tonic-gate 
64220Sstevel@tonic-gate 	init_bound_node_ev(pdip, dip, flags);
64230Sstevel@tonic-gate 
64240Sstevel@tonic-gate 	rval = devi_detach_node(dip, flags);
64250Sstevel@tonic-gate 	if (brevq) {
64260Sstevel@tonic-gate 		if (rval != NDI_SUCCESS)
64270Sstevel@tonic-gate 			log_and_free_brevq_dip(dip, brevq);
64280Sstevel@tonic-gate 		else
64290Sstevel@tonic-gate 			free_brevq(brevq);
64300Sstevel@tonic-gate 	}
64310Sstevel@tonic-gate 
64320Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
64332155Scth 	if (vdip)
64342155Scth 		ndi_devi_exit(vdip, v_circ);
64350Sstevel@tonic-gate 
64360Sstevel@tonic-gate 	return (rval);
64370Sstevel@tonic-gate }
64380Sstevel@tonic-gate 
64390Sstevel@tonic-gate /*
64400Sstevel@tonic-gate  * Find the child dev_info node of parent nexus 'p' whose name
64410Sstevel@tonic-gate  * matches "cname@caddr".  Recommend use of ndi_devi_findchild() instead.
64420Sstevel@tonic-gate  */
64430Sstevel@tonic-gate dev_info_t *
64440Sstevel@tonic-gate ndi_devi_find(dev_info_t *pdip, char *cname, char *caddr)
64450Sstevel@tonic-gate {
64460Sstevel@tonic-gate 	dev_info_t *child;
64470Sstevel@tonic-gate 	int circ;
64480Sstevel@tonic-gate 
64490Sstevel@tonic-gate 	if (pdip == NULL || cname == NULL || caddr == NULL)
64500Sstevel@tonic-gate 		return ((dev_info_t *)NULL);
64510Sstevel@tonic-gate 
64520Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
64534145Scth 	child = find_sibling(ddi_get_child(pdip), cname, caddr,
64544145Scth 	    FIND_NODE_BY_NODENAME, NULL);
64550Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
64560Sstevel@tonic-gate 	return (child);
64570Sstevel@tonic-gate }
64580Sstevel@tonic-gate 
64590Sstevel@tonic-gate /*
64600Sstevel@tonic-gate  * Find the child dev_info node of parent nexus 'p' whose name
64610Sstevel@tonic-gate  * matches devname "name@addr".  Permits caller to hold the parent.
64620Sstevel@tonic-gate  */
64630Sstevel@tonic-gate dev_info_t *
64640Sstevel@tonic-gate ndi_devi_findchild(dev_info_t *pdip, char *devname)
64650Sstevel@tonic-gate {
64660Sstevel@tonic-gate 	dev_info_t *child;
64670Sstevel@tonic-gate 	char	*cname, *caddr;
64680Sstevel@tonic-gate 	char	*devstr;
64690Sstevel@tonic-gate 
64700Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(pdip));
64710Sstevel@tonic-gate 
64720Sstevel@tonic-gate 	devstr = i_ddi_strdup(devname, KM_SLEEP);
64730Sstevel@tonic-gate 	i_ddi_parse_name(devstr, &cname, &caddr, NULL);
64740Sstevel@tonic-gate 
64750Sstevel@tonic-gate 	if (cname == NULL || caddr == NULL) {
64760Sstevel@tonic-gate 		kmem_free(devstr, strlen(devname)+1);
64770Sstevel@tonic-gate 		return ((dev_info_t *)NULL);
64780Sstevel@tonic-gate 	}
64790Sstevel@tonic-gate 
64804145Scth 	child = find_sibling(ddi_get_child(pdip), cname, caddr,
64814145Scth 	    FIND_NODE_BY_NODENAME, NULL);
64820Sstevel@tonic-gate 	kmem_free(devstr, strlen(devname)+1);
64830Sstevel@tonic-gate 	return (child);
64840Sstevel@tonic-gate }
64850Sstevel@tonic-gate 
64860Sstevel@tonic-gate /*
64870Sstevel@tonic-gate  * Misc. routines called by framework only
64880Sstevel@tonic-gate  */
64890Sstevel@tonic-gate 
64900Sstevel@tonic-gate /*
64910Sstevel@tonic-gate  * Clear the DEVI_MADE_CHILDREN/DEVI_ATTACHED_CHILDREN flags
64920Sstevel@tonic-gate  * if new child spec has been added.
64930Sstevel@tonic-gate  */
64940Sstevel@tonic-gate static int
64950Sstevel@tonic-gate reset_nexus_flags(dev_info_t *dip, void *arg)
64960Sstevel@tonic-gate {
6497298Scth 	struct hwc_spec	*list;
6498298Scth 	int		circ;
64990Sstevel@tonic-gate 
65000Sstevel@tonic-gate 	if (((DEVI(dip)->devi_flags & DEVI_MADE_CHILDREN) == 0) ||
65010Sstevel@tonic-gate 	    ((list = hwc_get_child_spec(dip, (major_t)(uintptr_t)arg)) == NULL))
65020Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
65030Sstevel@tonic-gate 
65040Sstevel@tonic-gate 	hwc_free_spec_list(list);
6505298Scth 
6506298Scth 	/* coordinate child state update */
6507298Scth 	ndi_devi_enter(dip, &circ);
65080Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
65090Sstevel@tonic-gate 	DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN | DEVI_ATTACHED_CHILDREN);
65100Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
6511298Scth 	ndi_devi_exit(dip, circ);
65120Sstevel@tonic-gate 
65130Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
65140Sstevel@tonic-gate }
65150Sstevel@tonic-gate 
65160Sstevel@tonic-gate /*
65170Sstevel@tonic-gate  * Helper functions, returns NULL if no memory.
65180Sstevel@tonic-gate  */
65190Sstevel@tonic-gate 
65200Sstevel@tonic-gate /*
65210Sstevel@tonic-gate  * path_to_major:
65220Sstevel@tonic-gate  *
65230Sstevel@tonic-gate  * Return an alternate driver name binding for the leaf device
65240Sstevel@tonic-gate  * of the given pathname, if there is one. The purpose of this
65250Sstevel@tonic-gate  * function is to deal with generic pathnames. The default action
65260Sstevel@tonic-gate  * for platforms that can't do this (ie: x86 or any platform that
65270Sstevel@tonic-gate  * does not have prom_finddevice functionality, which matches
65280Sstevel@tonic-gate  * nodenames and unit-addresses without the drivers participation)
65297009Scth  * is to return DDI_MAJOR_T_NONE.
65300Sstevel@tonic-gate  *
65310Sstevel@tonic-gate  * Used in loadrootmodules() in the swapgeneric module to
65320Sstevel@tonic-gate  * associate a given pathname with a given leaf driver.
65330Sstevel@tonic-gate  *
65340Sstevel@tonic-gate  */
65350Sstevel@tonic-gate major_t
65360Sstevel@tonic-gate path_to_major(char *path)
65370Sstevel@tonic-gate {
65380Sstevel@tonic-gate 	dev_info_t *dip;
65390Sstevel@tonic-gate 	char *p, *q;
6540789Sahrens 	pnode_t nodeid;
65414145Scth 	major_t major;
65424145Scth 
65434145Scth 	/* check for path-oriented alias */
65444145Scth 	major = ddi_name_to_major(path);
65457009Scth 	if ((major != DDI_MAJOR_T_NONE) &&
65464145Scth 	    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED)) {
65474145Scth 		NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s path bound %s\n",
65484145Scth 		    path, ddi_major_to_name(major)));
65494145Scth 		return (major);
65504145Scth 	}
65510Sstevel@tonic-gate 
65520Sstevel@tonic-gate 	/*
65530Sstevel@tonic-gate 	 * Get the nodeid of the given pathname, if such a mapping exists.
65540Sstevel@tonic-gate 	 */
65550Sstevel@tonic-gate 	dip = NULL;
65560Sstevel@tonic-gate 	nodeid = prom_finddevice(path);
65570Sstevel@tonic-gate 	if (nodeid != OBP_BADNODE) {
65580Sstevel@tonic-gate 		/*
65590Sstevel@tonic-gate 		 * Find the nodeid in our copy of the device tree and return
65600Sstevel@tonic-gate 		 * whatever name we used to bind this node to a driver.
65610Sstevel@tonic-gate 		 */
65620Sstevel@tonic-gate 		dip = e_ddi_nodeid_to_dip(nodeid);
65630Sstevel@tonic-gate 	}
65640Sstevel@tonic-gate 
65650Sstevel@tonic-gate 	if (dip == NULL) {
65660Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_WARN,
65670Sstevel@tonic-gate 		    "path_to_major: can't bind <%s>\n", path));
65687009Scth 		return (DDI_MAJOR_T_NONE);
65690Sstevel@tonic-gate 	}
65700Sstevel@tonic-gate 
65710Sstevel@tonic-gate 	/*
65720Sstevel@tonic-gate 	 * If we're bound to something other than the nodename,
65730Sstevel@tonic-gate 	 * note that in the message buffer and system log.
65740Sstevel@tonic-gate 	 */
65750Sstevel@tonic-gate 	p = ddi_binding_name(dip);
65760Sstevel@tonic-gate 	q = ddi_node_name(dip);
65770Sstevel@tonic-gate 	if (p && q && (strcmp(p, q) != 0))
65780Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s bound to %s\n",
65790Sstevel@tonic-gate 		    path, p));
65800Sstevel@tonic-gate 
65814145Scth 	major = ddi_name_to_major(p);
65824145Scth 
65834145Scth 	ndi_rele_devi(dip);		/* release e_ddi_nodeid_to_dip hold */
65844145Scth 
65854145Scth 	return (major);
65860Sstevel@tonic-gate }
65870Sstevel@tonic-gate 
65880Sstevel@tonic-gate /*
65890Sstevel@tonic-gate  * Return the held dip for the specified major and instance, attempting to do
65900Sstevel@tonic-gate  * an attach if specified. Return NULL if the devi can't be found or put in
65910Sstevel@tonic-gate  * the proper state. The caller must release the hold via ddi_release_devi if
65920Sstevel@tonic-gate  * a non-NULL value is returned.
65930Sstevel@tonic-gate  *
65940Sstevel@tonic-gate  * Some callers expect to be able to perform a hold_devi() while in a context
65950Sstevel@tonic-gate  * where using ndi_devi_enter() to ensure the hold might cause deadlock (see
65960Sstevel@tonic-gate  * open-from-attach code in consconfig_dacf.c). Such special-case callers
65970Sstevel@tonic-gate  * must ensure that an ndi_devi_enter(parent)/ndi_devi_hold() from a safe
65980Sstevel@tonic-gate  * context is already active. The hold_devi() implementation must accommodate
65990Sstevel@tonic-gate  * these callers.
66000Sstevel@tonic-gate  */
66010Sstevel@tonic-gate static dev_info_t *
66020Sstevel@tonic-gate hold_devi(major_t major, int instance, int flags)
66030Sstevel@tonic-gate {
66040Sstevel@tonic-gate 	struct devnames	*dnp;
66050Sstevel@tonic-gate 	dev_info_t	*dip;
66060Sstevel@tonic-gate 	char		*path;
66079065SChris.Horne@Sun.COM 	char		*vpath;
66080Sstevel@tonic-gate 
66090Sstevel@tonic-gate 	if ((major >= devcnt) || (instance == -1))
66100Sstevel@tonic-gate 		return (NULL);
66110Sstevel@tonic-gate 
66120Sstevel@tonic-gate 	/* try to find the instance in the per driver list */
66130Sstevel@tonic-gate 	dnp = &(devnamesp[major]);
66140Sstevel@tonic-gate 	LOCK_DEV_OPS(&(dnp->dn_lock));
66150Sstevel@tonic-gate 	for (dip = dnp->dn_head; dip;
66160Sstevel@tonic-gate 	    dip = (dev_info_t *)DEVI(dip)->devi_next) {
66170Sstevel@tonic-gate 		/* skip node if instance field is not valid */
66180Sstevel@tonic-gate 		if (i_ddi_node_state(dip) < DS_INITIALIZED)
66190Sstevel@tonic-gate 			continue;
66200Sstevel@tonic-gate 
66210Sstevel@tonic-gate 		/* look for instance match */
66220Sstevel@tonic-gate 		if (DEVI(dip)->devi_instance == instance) {
66230Sstevel@tonic-gate 			/*
66240Sstevel@tonic-gate 			 * To accommodate callers that can't block in
66250Sstevel@tonic-gate 			 * ndi_devi_enter() we do an ndi_devi_hold(), and
66260Sstevel@tonic-gate 			 * afterwards check that the node is in a state where
66270Sstevel@tonic-gate 			 * the hold prevents detach(). If we did not manage to
66280Sstevel@tonic-gate 			 * prevent detach then we ndi_rele_devi() and perform
66290Sstevel@tonic-gate 			 * the slow path below (which can result in a blocking
66300Sstevel@tonic-gate 			 * ndi_devi_enter() while driving attach top-down).
66310Sstevel@tonic-gate 			 * This code depends on the ordering of
66320Sstevel@tonic-gate 			 * DEVI_SET_DETACHING and the devi_ref check in the
66330Sstevel@tonic-gate 			 * detach_node() code path.
66340Sstevel@tonic-gate 			 */
66350Sstevel@tonic-gate 			ndi_hold_devi(dip);
66361333Scth 			if (i_ddi_devi_attached(dip) &&
66370Sstevel@tonic-gate 			    !DEVI_IS_DETACHING(dip)) {
66380Sstevel@tonic-gate 				UNLOCK_DEV_OPS(&(dnp->dn_lock));
66390Sstevel@tonic-gate 				return (dip);	/* fast-path with devi held */
66400Sstevel@tonic-gate 			}
66410Sstevel@tonic-gate 			ndi_rele_devi(dip);
66420Sstevel@tonic-gate 
66430Sstevel@tonic-gate 			/* try slow-path */
66440Sstevel@tonic-gate 			dip = NULL;
66450Sstevel@tonic-gate 			break;
66460Sstevel@tonic-gate 		}
66470Sstevel@tonic-gate 	}
66480Sstevel@tonic-gate 	ASSERT(dip == NULL);
66490Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
66500Sstevel@tonic-gate 
66510Sstevel@tonic-gate 	if (flags & E_DDI_HOLD_DEVI_NOATTACH)
66520Sstevel@tonic-gate 		return (NULL);		/* told not to drive attach */
66530Sstevel@tonic-gate 
66540Sstevel@tonic-gate 	/* slow-path may block, so it should not occur from interrupt */
66550Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
66560Sstevel@tonic-gate 	if (servicing_interrupt())
66570Sstevel@tonic-gate 		return (NULL);
66580Sstevel@tonic-gate 
66590Sstevel@tonic-gate 	/* reconstruct the path and drive attach by path through devfs. */
66600Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
66619065SChris.Horne@Sun.COM 	if (e_ddi_majorinstance_to_path(major, instance, path) == 0) {
66620Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_path(path, flags);
66639065SChris.Horne@Sun.COM 
66649065SChris.Horne@Sun.COM 		/*
66659065SChris.Horne@Sun.COM 		 * Verify that we got the correct device - a path_to_inst file
66669065SChris.Horne@Sun.COM 		 * with a bogus/corrupt path (or a nexus that changes its
66679065SChris.Horne@Sun.COM 		 * unit-address format) could result in an incorrect answer
66689065SChris.Horne@Sun.COM 		 *
66699065SChris.Horne@Sun.COM 		 * Verify major, instance, and path.
66709065SChris.Horne@Sun.COM 		 */
66719065SChris.Horne@Sun.COM 		vpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
66729065SChris.Horne@Sun.COM 		if (dip &&
66739065SChris.Horne@Sun.COM 		    ((DEVI(dip)->devi_major != major) ||
66749065SChris.Horne@Sun.COM 		    ((DEVI(dip)->devi_instance != instance)) ||
66759065SChris.Horne@Sun.COM 		    (strcmp(path, ddi_pathname(dip, vpath)) != 0))) {
66769065SChris.Horne@Sun.COM 			ndi_rele_devi(dip);
66779065SChris.Horne@Sun.COM 			dip = NULL;	/* no answer better than wrong answer */
66789065SChris.Horne@Sun.COM 		}
66799065SChris.Horne@Sun.COM 		kmem_free(vpath, MAXPATHLEN);
66809065SChris.Horne@Sun.COM 	}
66810Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
66820Sstevel@tonic-gate 	return (dip);			/* with devi held */
66830Sstevel@tonic-gate }
66840Sstevel@tonic-gate 
66850Sstevel@tonic-gate /*
66860Sstevel@tonic-gate  * The {e_}ddi_hold_devi{_by_{instance|dev|path}} hold the devinfo node
66870Sstevel@tonic-gate  * associated with the specified arguments.  This hold should be released
66880Sstevel@tonic-gate  * by calling ddi_release_devi.
66890Sstevel@tonic-gate  *
66900Sstevel@tonic-gate  * The E_DDI_HOLD_DEVI_NOATTACH flag argument allows the caller to to specify
66910Sstevel@tonic-gate  * a failure return if the node is not already attached.
66920Sstevel@tonic-gate  *
66930Sstevel@tonic-gate  * NOTE: by the time we make e_ddi_hold_devi public, we should be able to reuse
66940Sstevel@tonic-gate  * ddi_hold_devi again.
66950Sstevel@tonic-gate  */
66960Sstevel@tonic-gate dev_info_t *
66970Sstevel@tonic-gate ddi_hold_devi_by_instance(major_t major, int instance, int flags)
66980Sstevel@tonic-gate {
66990Sstevel@tonic-gate 	return (hold_devi(major, instance, flags));
67000Sstevel@tonic-gate }
67010Sstevel@tonic-gate 
67020Sstevel@tonic-gate dev_info_t *
67030Sstevel@tonic-gate e_ddi_hold_devi_by_dev(dev_t dev, int flags)
67040Sstevel@tonic-gate {
67050Sstevel@tonic-gate 	major_t	major = getmajor(dev);
67060Sstevel@tonic-gate 	dev_info_t	*dip;
67070Sstevel@tonic-gate 	struct dev_ops	*ops;
67080Sstevel@tonic-gate 	dev_info_t	*ddip = NULL;
67090Sstevel@tonic-gate 
67100Sstevel@tonic-gate 	dip = hold_devi(major, dev_to_instance(dev), flags);
67110Sstevel@tonic-gate 
67120Sstevel@tonic-gate 	/*
67130Sstevel@tonic-gate 	 * The rest of this routine is legacy support for drivers that
67140Sstevel@tonic-gate 	 * have broken DDI_INFO_DEVT2INSTANCE implementations but may have
67150Sstevel@tonic-gate 	 * functional DDI_INFO_DEVT2DEVINFO implementations.  This code will
67160Sstevel@tonic-gate 	 * diagnose inconsistency and, for maximum compatibility with legacy
67170Sstevel@tonic-gate 	 * drivers, give preference to the drivers DDI_INFO_DEVT2DEVINFO
67180Sstevel@tonic-gate 	 * implementation over the above derived dip based the driver's
67190Sstevel@tonic-gate 	 * DDI_INFO_DEVT2INSTANCE implementation. This legacy support should
67200Sstevel@tonic-gate 	 * be removed when DDI_INFO_DEVT2DEVINFO is deprecated.
67210Sstevel@tonic-gate 	 *
67220Sstevel@tonic-gate 	 * NOTE: The following code has a race condition. DEVT2DEVINFO
67230Sstevel@tonic-gate 	 *	returns a dip which is not held. By the time we ref ddip,
67240Sstevel@tonic-gate 	 *	it could have been freed. The saving grace is that for
67250Sstevel@tonic-gate 	 *	most drivers, the dip returned from hold_devi() is the
67260Sstevel@tonic-gate 	 *	same one as the one returned by DEVT2DEVINFO, so we are
67270Sstevel@tonic-gate 	 *	safe for drivers with the correct getinfo(9e) impl.
67280Sstevel@tonic-gate 	 */
67290Sstevel@tonic-gate 	if (((ops = ddi_hold_driver(major)) != NULL) &&
67300Sstevel@tonic-gate 	    CB_DRV_INSTALLED(ops) && ops->devo_getinfo)  {
67310Sstevel@tonic-gate 		if ((*ops->devo_getinfo)(NULL, DDI_INFO_DEVT2DEVINFO,
67320Sstevel@tonic-gate 		    (void *)dev, (void **)&ddip) != DDI_SUCCESS)
67330Sstevel@tonic-gate 			ddip = NULL;
67340Sstevel@tonic-gate 	}
67350Sstevel@tonic-gate 
67360Sstevel@tonic-gate 	/* give preference to the driver returned DEVT2DEVINFO dip */
67370Sstevel@tonic-gate 	if (ddip && (dip != ddip)) {
67380Sstevel@tonic-gate #ifdef	DEBUG
67390Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: inconsistent getinfo(9E) implementation",
67400Sstevel@tonic-gate 		    ddi_driver_name(ddip));
67410Sstevel@tonic-gate #endif	/* DEBUG */
67420Sstevel@tonic-gate 		ndi_hold_devi(ddip);
67430Sstevel@tonic-gate 		if (dip)
67440Sstevel@tonic-gate 			ndi_rele_devi(dip);
67450Sstevel@tonic-gate 		dip = ddip;
67460Sstevel@tonic-gate 	}
67470Sstevel@tonic-gate 
67480Sstevel@tonic-gate 	if (ops)
67490Sstevel@tonic-gate 		ddi_rele_driver(major);
67500Sstevel@tonic-gate 
67510Sstevel@tonic-gate 	return (dip);
67520Sstevel@tonic-gate }
67530Sstevel@tonic-gate 
67540Sstevel@tonic-gate /*
67550Sstevel@tonic-gate  * For compatibility only. Do not call this function!
67560Sstevel@tonic-gate  */
67570Sstevel@tonic-gate dev_info_t *
67580Sstevel@tonic-gate e_ddi_get_dev_info(dev_t dev, vtype_t type)
67590Sstevel@tonic-gate {
67600Sstevel@tonic-gate 	dev_info_t *dip = NULL;
67610Sstevel@tonic-gate 	if (getmajor(dev) >= devcnt)
67620Sstevel@tonic-gate 		return (NULL);
67630Sstevel@tonic-gate 
67640Sstevel@tonic-gate 	switch (type) {
67650Sstevel@tonic-gate 	case VCHR:
67660Sstevel@tonic-gate 	case VBLK:
67670Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
67680Sstevel@tonic-gate 	default:
67690Sstevel@tonic-gate 		break;
67700Sstevel@tonic-gate 	}
67710Sstevel@tonic-gate 
67720Sstevel@tonic-gate 	/*
67730Sstevel@tonic-gate 	 * For compatibility reasons, we can only return the dip with
67740Sstevel@tonic-gate 	 * the driver ref count held. This is not a safe thing to do.
67750Sstevel@tonic-gate 	 * For certain broken third-party software, we are willing
67760Sstevel@tonic-gate 	 * to venture into unknown territory.
67770Sstevel@tonic-gate 	 */
67780Sstevel@tonic-gate 	if (dip) {
67790Sstevel@tonic-gate 		(void) ndi_hold_driver(dip);
67800Sstevel@tonic-gate 		ndi_rele_devi(dip);
67810Sstevel@tonic-gate 	}
67820Sstevel@tonic-gate 	return (dip);
67830Sstevel@tonic-gate }
67840Sstevel@tonic-gate 
67850Sstevel@tonic-gate dev_info_t *
67860Sstevel@tonic-gate e_ddi_hold_devi_by_path(char *path, int flags)
67870Sstevel@tonic-gate {
67880Sstevel@tonic-gate 	dev_info_t	*dip;
67890Sstevel@tonic-gate 
67900Sstevel@tonic-gate 	/* can't specify NOATTACH by path */
67910Sstevel@tonic-gate 	ASSERT(!(flags & E_DDI_HOLD_DEVI_NOATTACH));
67920Sstevel@tonic-gate 
67930Sstevel@tonic-gate 	return (resolve_pathname(path, &dip, NULL, NULL) ? NULL : dip);
67940Sstevel@tonic-gate }
67950Sstevel@tonic-gate 
67960Sstevel@tonic-gate void
67970Sstevel@tonic-gate e_ddi_hold_devi(dev_info_t *dip)
67980Sstevel@tonic-gate {
67990Sstevel@tonic-gate 	ndi_hold_devi(dip);
68000Sstevel@tonic-gate }
68010Sstevel@tonic-gate 
68020Sstevel@tonic-gate void
68030Sstevel@tonic-gate ddi_release_devi(dev_info_t *dip)
68040Sstevel@tonic-gate {
68050Sstevel@tonic-gate 	ndi_rele_devi(dip);
68060Sstevel@tonic-gate }
68070Sstevel@tonic-gate 
68080Sstevel@tonic-gate /*
68090Sstevel@tonic-gate  * Associate a streams queue with a devinfo node
68100Sstevel@tonic-gate  * NOTE: This function is called by STREAM driver's put procedure.
68110Sstevel@tonic-gate  *	It cannot block.
68120Sstevel@tonic-gate  */
68130Sstevel@tonic-gate void
68140Sstevel@tonic-gate ddi_assoc_queue_with_devi(queue_t *q, dev_info_t *dip)
68150Sstevel@tonic-gate {
68160Sstevel@tonic-gate 	queue_t *rq = _RD(q);
68170Sstevel@tonic-gate 	struct stdata *stp;
68180Sstevel@tonic-gate 	vnode_t *vp;
68190Sstevel@tonic-gate 
68200Sstevel@tonic-gate 	/* set flag indicating that ddi_assoc_queue_with_devi was called */
68210Sstevel@tonic-gate 	mutex_enter(QLOCK(rq));
68220Sstevel@tonic-gate 	rq->q_flag |= _QASSOCIATED;
68230Sstevel@tonic-gate 	mutex_exit(QLOCK(rq));
68240Sstevel@tonic-gate 
68250Sstevel@tonic-gate 	/* get the vnode associated with the queue */
68260Sstevel@tonic-gate 	stp = STREAM(rq);
68270Sstevel@tonic-gate 	vp = stp->sd_vnode;
68280Sstevel@tonic-gate 	ASSERT(vp);
68290Sstevel@tonic-gate 
68300Sstevel@tonic-gate 	/* change the hardware association of the vnode */
68310Sstevel@tonic-gate 	spec_assoc_vp_with_devi(vp, dip);
68320Sstevel@tonic-gate }
68330Sstevel@tonic-gate 
68340Sstevel@tonic-gate /*
68350Sstevel@tonic-gate  * ddi_install_driver(name)
68360Sstevel@tonic-gate  *
68370Sstevel@tonic-gate  * Driver installation is currently a byproduct of driver loading.  This
68380Sstevel@tonic-gate  * may change.
68390Sstevel@tonic-gate  */
68400Sstevel@tonic-gate int
68410Sstevel@tonic-gate ddi_install_driver(char *name)
68420Sstevel@tonic-gate {
68430Sstevel@tonic-gate 	major_t major = ddi_name_to_major(name);
68440Sstevel@tonic-gate 
68457009Scth 	if ((major == DDI_MAJOR_T_NONE) ||
68460Sstevel@tonic-gate 	    (ddi_hold_installed_driver(major) == NULL)) {
68470Sstevel@tonic-gate 		return (DDI_FAILURE);
68480Sstevel@tonic-gate 	}
68490Sstevel@tonic-gate 	ddi_rele_driver(major);
68500Sstevel@tonic-gate 	return (DDI_SUCCESS);
68510Sstevel@tonic-gate }
68520Sstevel@tonic-gate 
68530Sstevel@tonic-gate struct dev_ops *
68540Sstevel@tonic-gate ddi_hold_driver(major_t major)
68550Sstevel@tonic-gate {
68560Sstevel@tonic-gate 	return (mod_hold_dev_by_major(major));
68570Sstevel@tonic-gate }
68580Sstevel@tonic-gate 
68590Sstevel@tonic-gate 
68600Sstevel@tonic-gate void
68610Sstevel@tonic-gate ddi_rele_driver(major_t major)
68620Sstevel@tonic-gate {
68630Sstevel@tonic-gate 	mod_rele_dev_by_major(major);
68640Sstevel@tonic-gate }
68650Sstevel@tonic-gate 
68660Sstevel@tonic-gate 
68670Sstevel@tonic-gate /*
68680Sstevel@tonic-gate  * This is called during boot to force attachment order of special dips
68690Sstevel@tonic-gate  * dip must be referenced via ndi_hold_devi()
68700Sstevel@tonic-gate  */
68710Sstevel@tonic-gate int
68720Sstevel@tonic-gate i_ddi_attach_node_hierarchy(dev_info_t *dip)
68730Sstevel@tonic-gate {
68742155Scth 	dev_info_t	*parent;
68752155Scth 	int		ret, circ;
68762155Scth 
68772155Scth 	/*
68782155Scth 	 * Recurse up until attached parent is found.
68792155Scth 	 */
68802009Sdm120769 	if (i_ddi_devi_attached(dip))
68812009Sdm120769 		return (DDI_SUCCESS);
68820Sstevel@tonic-gate 	parent = ddi_get_parent(dip);
68830Sstevel@tonic-gate 	if (i_ddi_attach_node_hierarchy(parent) != DDI_SUCCESS)
68840Sstevel@tonic-gate 		return (DDI_FAILURE);
68850Sstevel@tonic-gate 
68860Sstevel@tonic-gate 	/*
68872155Scth 	 * Come top-down, expanding .conf nodes under this parent
68882155Scth 	 * and driving attach.
68890Sstevel@tonic-gate 	 */
68902155Scth 	ndi_devi_enter(parent, &circ);
68910Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(parent, 0);
68922155Scth 	ret = i_ddi_attachchild(dip);
68932155Scth 	ndi_devi_exit(parent, circ);
68942155Scth 
68952155Scth 	return (ret);
68960Sstevel@tonic-gate }
68970Sstevel@tonic-gate 
68980Sstevel@tonic-gate /* keep this function static */
68990Sstevel@tonic-gate static int
69000Sstevel@tonic-gate attach_driver_nodes(major_t major)
69010Sstevel@tonic-gate {
69020Sstevel@tonic-gate 	struct devnames *dnp;
69030Sstevel@tonic-gate 	dev_info_t *dip;
69040Sstevel@tonic-gate 	int error = DDI_FAILURE;
69050Sstevel@tonic-gate 
69060Sstevel@tonic-gate 	dnp = &devnamesp[major];
69070Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
69080Sstevel@tonic-gate 	dip = dnp->dn_head;
69090Sstevel@tonic-gate 	while (dip) {
69100Sstevel@tonic-gate 		ndi_hold_devi(dip);
69110Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
69120Sstevel@tonic-gate 		if (i_ddi_attach_node_hierarchy(dip) == DDI_SUCCESS)
69130Sstevel@tonic-gate 			error = DDI_SUCCESS;
69140Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
69150Sstevel@tonic-gate 		ndi_rele_devi(dip);
69160Sstevel@tonic-gate 		dip = ddi_get_next(dip);
69170Sstevel@tonic-gate 	}
69180Sstevel@tonic-gate 	if (error == DDI_SUCCESS)
69190Sstevel@tonic-gate 		dnp->dn_flags |= DN_NO_AUTODETACH;
69200Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
69210Sstevel@tonic-gate 
69220Sstevel@tonic-gate 
69230Sstevel@tonic-gate 	return (error);
69240Sstevel@tonic-gate }
69250Sstevel@tonic-gate 
69260Sstevel@tonic-gate /*
69270Sstevel@tonic-gate  * i_ddi_attach_hw_nodes configures and attaches all hw nodes
69280Sstevel@tonic-gate  * bound to a specific driver. This function replaces calls to
69290Sstevel@tonic-gate  * ddi_hold_installed_driver() for drivers with no .conf
69300Sstevel@tonic-gate  * enumerated nodes.
69310Sstevel@tonic-gate  *
69320Sstevel@tonic-gate  * This facility is typically called at boot time to attach
69330Sstevel@tonic-gate  * platform-specific hardware nodes, such as ppm nodes on xcal
69340Sstevel@tonic-gate  * and grover and keyswitch nodes on cherrystone. It does not
69350Sstevel@tonic-gate  * deal with .conf enumerated node. Calling it beyond the boot
69360Sstevel@tonic-gate  * process is strongly discouraged.
69370Sstevel@tonic-gate  */
69380Sstevel@tonic-gate int
69390Sstevel@tonic-gate i_ddi_attach_hw_nodes(char *driver)
69400Sstevel@tonic-gate {
69410Sstevel@tonic-gate 	major_t major;
69420Sstevel@tonic-gate 
69430Sstevel@tonic-gate 	major = ddi_name_to_major(driver);
69447009Scth 	if (major == DDI_MAJOR_T_NONE)
69450Sstevel@tonic-gate 		return (DDI_FAILURE);
69460Sstevel@tonic-gate 
69470Sstevel@tonic-gate 	return (attach_driver_nodes(major));
69480Sstevel@tonic-gate }
69490Sstevel@tonic-gate 
69500Sstevel@tonic-gate /*
69510Sstevel@tonic-gate  * i_ddi_attach_pseudo_node configures pseudo drivers which
69520Sstevel@tonic-gate  * has a single node. The .conf nodes must be enumerated
69530Sstevel@tonic-gate  * before calling this interface. The dip is held attached
69540Sstevel@tonic-gate  * upon returning.
69550Sstevel@tonic-gate  *
69560Sstevel@tonic-gate  * This facility should only be called only at boot time
69570Sstevel@tonic-gate  * by the I/O framework.
69580Sstevel@tonic-gate  */
69590Sstevel@tonic-gate dev_info_t *
69600Sstevel@tonic-gate i_ddi_attach_pseudo_node(char *driver)
69610Sstevel@tonic-gate {
69620Sstevel@tonic-gate 	major_t major;
69630Sstevel@tonic-gate 	dev_info_t *dip;
69640Sstevel@tonic-gate 
69650Sstevel@tonic-gate 	major = ddi_name_to_major(driver);
69667009Scth 	if (major == DDI_MAJOR_T_NONE)
69670Sstevel@tonic-gate 		return (NULL);
69680Sstevel@tonic-gate 
69690Sstevel@tonic-gate 	if (attach_driver_nodes(major) != DDI_SUCCESS)
69700Sstevel@tonic-gate 		return (NULL);
69710Sstevel@tonic-gate 
69720Sstevel@tonic-gate 	dip = devnamesp[major].dn_head;
69730Sstevel@tonic-gate 	ASSERT(dip && ddi_get_next(dip) == NULL);
69740Sstevel@tonic-gate 	ndi_hold_devi(dip);
69750Sstevel@tonic-gate 	return (dip);
69760Sstevel@tonic-gate }
69770Sstevel@tonic-gate 
69780Sstevel@tonic-gate static void
69790Sstevel@tonic-gate diplist_to_parent_major(dev_info_t *head, char parents[])
69800Sstevel@tonic-gate {
69810Sstevel@tonic-gate 	major_t major;
69820Sstevel@tonic-gate 	dev_info_t *dip, *pdip;
69830Sstevel@tonic-gate 
69840Sstevel@tonic-gate 	for (dip = head; dip != NULL; dip = ddi_get_next(dip)) {
69850Sstevel@tonic-gate 		pdip = ddi_get_parent(dip);
69860Sstevel@tonic-gate 		ASSERT(pdip);	/* disallow rootnex.conf nodes */
69870Sstevel@tonic-gate 		major = ddi_driver_major(pdip);
69887009Scth 		if ((major != DDI_MAJOR_T_NONE) && parents[major] == 0)
69890Sstevel@tonic-gate 			parents[major] = 1;
69900Sstevel@tonic-gate 	}
69910Sstevel@tonic-gate }
69920Sstevel@tonic-gate 
69930Sstevel@tonic-gate /*
69940Sstevel@tonic-gate  * Call ddi_hold_installed_driver() on each parent major
69950Sstevel@tonic-gate  * and invoke mt_config_driver() to attach child major.
69960Sstevel@tonic-gate  * This is part of the implementation of ddi_hold_installed_driver.
69970Sstevel@tonic-gate  */
69980Sstevel@tonic-gate static int
69990Sstevel@tonic-gate attach_driver_by_parent(major_t child_major, char parents[])
70000Sstevel@tonic-gate {
70010Sstevel@tonic-gate 	major_t par_major;
70020Sstevel@tonic-gate 	struct mt_config_handle *hdl;
70030Sstevel@tonic-gate 	int flags = NDI_DEVI_PERSIST | NDI_NO_EVENT;
70040Sstevel@tonic-gate 
70050Sstevel@tonic-gate 	hdl = mt_config_init(NULL, NULL, flags, child_major, MT_CONFIG_OP,
70060Sstevel@tonic-gate 	    NULL);
70070Sstevel@tonic-gate 	for (par_major = 0; par_major < devcnt; par_major++) {
70080Sstevel@tonic-gate 		/* disallow recursion on the same driver */
70090Sstevel@tonic-gate 		if (parents[par_major] == 0 || par_major == child_major)
70100Sstevel@tonic-gate 			continue;
70110Sstevel@tonic-gate 		if (ddi_hold_installed_driver(par_major) == NULL)
70120Sstevel@tonic-gate 			continue;
70130Sstevel@tonic-gate 		hdl->mtc_parmajor = par_major;
70140Sstevel@tonic-gate 		mt_config_driver(hdl);
70150Sstevel@tonic-gate 		ddi_rele_driver(par_major);
70160Sstevel@tonic-gate 	}
70170Sstevel@tonic-gate 	(void) mt_config_fini(hdl);
70180Sstevel@tonic-gate 
70190Sstevel@tonic-gate 	return (i_ddi_devs_attached(child_major));
70200Sstevel@tonic-gate }
70210Sstevel@tonic-gate 
70220Sstevel@tonic-gate int
70230Sstevel@tonic-gate i_ddi_devs_attached(major_t major)
70240Sstevel@tonic-gate {
70250Sstevel@tonic-gate 	dev_info_t *dip;
70260Sstevel@tonic-gate 	struct devnames *dnp;
70270Sstevel@tonic-gate 	int error = DDI_FAILURE;
70280Sstevel@tonic-gate 
70290Sstevel@tonic-gate 	/* check for attached instances */
70300Sstevel@tonic-gate 	dnp = &devnamesp[major];
70310Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
70320Sstevel@tonic-gate 	for (dip = dnp->dn_head; dip != NULL; dip = ddi_get_next(dip)) {
70331333Scth 		if (i_ddi_devi_attached(dip)) {
70340Sstevel@tonic-gate 			error = DDI_SUCCESS;
70350Sstevel@tonic-gate 			break;
70360Sstevel@tonic-gate 		}
70370Sstevel@tonic-gate 	}
70380Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
70390Sstevel@tonic-gate 
70400Sstevel@tonic-gate 	return (error);
70410Sstevel@tonic-gate }
70420Sstevel@tonic-gate 
70435895Syz147064 int
70445895Syz147064 i_ddi_minor_node_count(dev_info_t *ddip, const char *node_type)
70455895Syz147064 {
70467224Scth 	int			circ;
70477224Scth 	struct ddi_minor_data	*dp;
70487224Scth 	int			count = 0;
70497224Scth 
70507224Scth 	ndi_devi_enter(ddip, &circ);
70517224Scth 	for (dp = DEVI(ddip)->devi_minor; dp != NULL; dp = dp->next) {
70525895Syz147064 		if (strcmp(dp->ddm_node_type, node_type) == 0)
70535895Syz147064 			count++;
70547224Scth 	}
70557224Scth 	ndi_devi_exit(ddip, circ);
70565895Syz147064 	return (count);
70575895Syz147064 }
70585895Syz147064 
70590Sstevel@tonic-gate /*
70600Sstevel@tonic-gate  * ddi_hold_installed_driver configures and attaches all
70610Sstevel@tonic-gate  * instances of the specified driver. To accomplish this
70620Sstevel@tonic-gate  * it configures and attaches all possible parents of
70630Sstevel@tonic-gate  * the driver, enumerated both in h/w nodes and in the
70640Sstevel@tonic-gate  * driver's .conf file.
70650Sstevel@tonic-gate  *
70660Sstevel@tonic-gate  * NOTE: This facility is for compatibility purposes only and will
70670Sstevel@tonic-gate  *	eventually go away. Its usage is strongly discouraged.
70680Sstevel@tonic-gate  */
70690Sstevel@tonic-gate static void
70700Sstevel@tonic-gate enter_driver(struct devnames *dnp)
70710Sstevel@tonic-gate {
70720Sstevel@tonic-gate 	mutex_enter(&dnp->dn_lock);
70730Sstevel@tonic-gate 	ASSERT(dnp->dn_busy_thread != curthread);
70740Sstevel@tonic-gate 	while (dnp->dn_flags & DN_DRIVER_BUSY)
70750Sstevel@tonic-gate 		cv_wait(&dnp->dn_wait, &dnp->dn_lock);
70760Sstevel@tonic-gate 	dnp->dn_flags |= DN_DRIVER_BUSY;
70770Sstevel@tonic-gate 	dnp->dn_busy_thread = curthread;
70780Sstevel@tonic-gate 	mutex_exit(&dnp->dn_lock);
70790Sstevel@tonic-gate }
70800Sstevel@tonic-gate 
70810Sstevel@tonic-gate static void
70820Sstevel@tonic-gate exit_driver(struct devnames *dnp)
70830Sstevel@tonic-gate {
70840Sstevel@tonic-gate 	mutex_enter(&dnp->dn_lock);
70850Sstevel@tonic-gate 	ASSERT(dnp->dn_busy_thread == curthread);
70860Sstevel@tonic-gate 	dnp->dn_flags &= ~DN_DRIVER_BUSY;
70870Sstevel@tonic-gate 	dnp->dn_busy_thread = NULL;
70880Sstevel@tonic-gate 	cv_broadcast(&dnp->dn_wait);
70890Sstevel@tonic-gate 	mutex_exit(&dnp->dn_lock);
70900Sstevel@tonic-gate }
70910Sstevel@tonic-gate 
70920Sstevel@tonic-gate struct dev_ops *
70930Sstevel@tonic-gate ddi_hold_installed_driver(major_t major)
70940Sstevel@tonic-gate {
70950Sstevel@tonic-gate 	struct dev_ops *ops;
70960Sstevel@tonic-gate 	struct devnames *dnp;
70970Sstevel@tonic-gate 	char *parents;
70980Sstevel@tonic-gate 	int error;
70990Sstevel@tonic-gate 
71000Sstevel@tonic-gate 	ops = ddi_hold_driver(major);
71010Sstevel@tonic-gate 	if (ops == NULL)
71020Sstevel@tonic-gate 		return (NULL);
71030Sstevel@tonic-gate 
71040Sstevel@tonic-gate 	/*
71050Sstevel@tonic-gate 	 * Return immediately if all the attach operations associated
71060Sstevel@tonic-gate 	 * with a ddi_hold_installed_driver() call have already been done.
71070Sstevel@tonic-gate 	 */
71080Sstevel@tonic-gate 	dnp = &devnamesp[major];
71090Sstevel@tonic-gate 	enter_driver(dnp);
71100Sstevel@tonic-gate 	if (dnp->dn_flags & DN_DRIVER_HELD) {
71110Sstevel@tonic-gate 		exit_driver(dnp);
71120Sstevel@tonic-gate 		if (i_ddi_devs_attached(major) == DDI_SUCCESS)
71130Sstevel@tonic-gate 			return (ops);
71140Sstevel@tonic-gate 		ddi_rele_driver(major);
71150Sstevel@tonic-gate 		return (NULL);
71160Sstevel@tonic-gate 	}
71170Sstevel@tonic-gate 
71180Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
71190Sstevel@tonic-gate 	dnp->dn_flags |= (DN_DRIVER_HELD | DN_NO_AUTODETACH);
71200Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
71210Sstevel@tonic-gate 
71220Sstevel@tonic-gate 	DCOMPATPRINTF((CE_CONT,
71230Sstevel@tonic-gate 	    "ddi_hold_installed_driver: %s\n", dnp->dn_name));
71240Sstevel@tonic-gate 
71250Sstevel@tonic-gate 	/*
71260Sstevel@tonic-gate 	 * When the driver has no .conf children, it is sufficient
71270Sstevel@tonic-gate 	 * to attach existing nodes in the device tree. Nodes not
71280Sstevel@tonic-gate 	 * enumerated by the OBP are not attached.
71290Sstevel@tonic-gate 	 */
71300Sstevel@tonic-gate 	if (dnp->dn_pl == NULL) {
71310Sstevel@tonic-gate 		if (attach_driver_nodes(major) == DDI_SUCCESS) {
71320Sstevel@tonic-gate 			exit_driver(dnp);
71330Sstevel@tonic-gate 			return (ops);
71340Sstevel@tonic-gate 		}
71350Sstevel@tonic-gate 		exit_driver(dnp);
71360Sstevel@tonic-gate 		ddi_rele_driver(major);
71370Sstevel@tonic-gate 		return (NULL);
71380Sstevel@tonic-gate 	}
71390Sstevel@tonic-gate 
71400Sstevel@tonic-gate 	/*
71410Sstevel@tonic-gate 	 * Driver has .conf nodes. We find all possible parents
71420Sstevel@tonic-gate 	 * and recursively all ddi_hold_installed_driver on the
71430Sstevel@tonic-gate 	 * parent driver; then we invoke ndi_config_driver()
71440Sstevel@tonic-gate 	 * on all possible parent node in parallel to speed up
71450Sstevel@tonic-gate 	 * performance.
71460Sstevel@tonic-gate 	 */
71470Sstevel@tonic-gate 	parents = kmem_zalloc(devcnt * sizeof (char), KM_SLEEP);
71480Sstevel@tonic-gate 
71490Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
71500Sstevel@tonic-gate 	/* find .conf parents */
71510Sstevel@tonic-gate 	(void) impl_parlist_to_major(dnp->dn_pl, parents);
71520Sstevel@tonic-gate 	/* find hw node parents */
71530Sstevel@tonic-gate 	diplist_to_parent_major(dnp->dn_head, parents);
71540Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
71550Sstevel@tonic-gate 
71560Sstevel@tonic-gate 	error = attach_driver_by_parent(major, parents);
71570Sstevel@tonic-gate 	kmem_free(parents, devcnt * sizeof (char));
71580Sstevel@tonic-gate 	if (error == DDI_SUCCESS) {
71590Sstevel@tonic-gate 		exit_driver(dnp);
71600Sstevel@tonic-gate 		return (ops);
71610Sstevel@tonic-gate 	}
71620Sstevel@tonic-gate 
71630Sstevel@tonic-gate 	exit_driver(dnp);
71640Sstevel@tonic-gate 	ddi_rele_driver(major);
71650Sstevel@tonic-gate 	return (NULL);
71660Sstevel@tonic-gate }
71670Sstevel@tonic-gate 
71680Sstevel@tonic-gate /*
71690Sstevel@tonic-gate  * Default bus_config entry point for nexus drivers
71700Sstevel@tonic-gate  */
71710Sstevel@tonic-gate int
71720Sstevel@tonic-gate ndi_busop_bus_config(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
71730Sstevel@tonic-gate     void *arg, dev_info_t **child, clock_t timeout)
71740Sstevel@tonic-gate {
71750Sstevel@tonic-gate 	major_t major;
71760Sstevel@tonic-gate 
71770Sstevel@tonic-gate 	/*
71780Sstevel@tonic-gate 	 * A timeout of 30 minutes or more is probably a mistake
71790Sstevel@tonic-gate 	 * This is intended to catch uses where timeout is in
71800Sstevel@tonic-gate 	 * the wrong units.  timeout must be in units of ticks.
71810Sstevel@tonic-gate 	 */
71820Sstevel@tonic-gate 	ASSERT(timeout < SEC_TO_TICK(1800));
71830Sstevel@tonic-gate 
71847009Scth 	major = DDI_MAJOR_T_NONE;
71850Sstevel@tonic-gate 	switch (op) {
71860Sstevel@tonic-gate 	case BUS_CONFIG_ONE:
71870Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config %s timeout=%ld\n",
71884950Scth 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
71894950Scth 		    (char *)arg, timeout));
71900Sstevel@tonic-gate 		return (devi_config_one(pdip, (char *)arg, child, flags,
71910Sstevel@tonic-gate 		    timeout));
71920Sstevel@tonic-gate 
71930Sstevel@tonic-gate 	case BUS_CONFIG_DRIVER:
71940Sstevel@tonic-gate 		major = (major_t)(uintptr_t)arg;
71950Sstevel@tonic-gate 		/*FALLTHROUGH*/
71960Sstevel@tonic-gate 	case BUS_CONFIG_ALL:
71970Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config timeout=%ld\n",
71984950Scth 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
71994950Scth 		    timeout));
72000Sstevel@tonic-gate 		if (timeout > 0) {
72010Sstevel@tonic-gate 			NDI_DEBUG(flags, (CE_CONT,
72020Sstevel@tonic-gate 			    "%s%d: bus config all timeout=%ld\n",
72030Sstevel@tonic-gate 			    ddi_driver_name(pdip), ddi_get_instance(pdip),
72040Sstevel@tonic-gate 			    timeout));
72050Sstevel@tonic-gate 			delay(timeout);
72060Sstevel@tonic-gate 		}
72070Sstevel@tonic-gate 		return (config_immediate_children(pdip, flags, major));
72080Sstevel@tonic-gate 
72090Sstevel@tonic-gate 	default:
72100Sstevel@tonic-gate 		return (NDI_FAILURE);
72110Sstevel@tonic-gate 	}
72120Sstevel@tonic-gate 	/*NOTREACHED*/
72130Sstevel@tonic-gate }
72140Sstevel@tonic-gate 
72150Sstevel@tonic-gate /*
72160Sstevel@tonic-gate  * Default busop bus_unconfig handler for nexus drivers
72170Sstevel@tonic-gate  */
72180Sstevel@tonic-gate int
72190Sstevel@tonic-gate ndi_busop_bus_unconfig(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
72200Sstevel@tonic-gate     void *arg)
72210Sstevel@tonic-gate {
72220Sstevel@tonic-gate 	major_t major;
72230Sstevel@tonic-gate 
72247009Scth 	major = DDI_MAJOR_T_NONE;
72250Sstevel@tonic-gate 	switch (op) {
72260Sstevel@tonic-gate 	case BUS_UNCONFIG_ONE:
72270Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig %s\n",
72280Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
72290Sstevel@tonic-gate 		    (char *)arg));
72300Sstevel@tonic-gate 		return (devi_unconfig_one(pdip, (char *)arg, flags));
72310Sstevel@tonic-gate 
72320Sstevel@tonic-gate 	case BUS_UNCONFIG_DRIVER:
72330Sstevel@tonic-gate 		major = (major_t)(uintptr_t)arg;
72340Sstevel@tonic-gate 		/*FALLTHROUGH*/
72350Sstevel@tonic-gate 	case BUS_UNCONFIG_ALL:
72360Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig all\n",
72370Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip)));
72380Sstevel@tonic-gate 		return (unconfig_immediate_children(pdip, NULL, flags, major));
72390Sstevel@tonic-gate 
72400Sstevel@tonic-gate 	default:
72410Sstevel@tonic-gate 		return (NDI_FAILURE);
72420Sstevel@tonic-gate 	}
72430Sstevel@tonic-gate 	/*NOTREACHED*/
72440Sstevel@tonic-gate }
72450Sstevel@tonic-gate 
72460Sstevel@tonic-gate /*
72470Sstevel@tonic-gate  * dummy functions to be removed
72480Sstevel@tonic-gate  */
72490Sstevel@tonic-gate void
72500Sstevel@tonic-gate impl_rem_dev_props(dev_info_t *dip)
72510Sstevel@tonic-gate {
72520Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip))
72530Sstevel@tonic-gate 	/* do nothing */
72540Sstevel@tonic-gate }
72550Sstevel@tonic-gate 
72560Sstevel@tonic-gate /*
72570Sstevel@tonic-gate  * Determine if a node is a leaf node. If not sure, return false (0).
72580Sstevel@tonic-gate  */
72590Sstevel@tonic-gate static int
72600Sstevel@tonic-gate is_leaf_node(dev_info_t *dip)
72610Sstevel@tonic-gate {
72620Sstevel@tonic-gate 	major_t major = ddi_driver_major(dip);
72630Sstevel@tonic-gate 
72647009Scth 	if (major == DDI_MAJOR_T_NONE)
72650Sstevel@tonic-gate 		return (0);
72660Sstevel@tonic-gate 
72670Sstevel@tonic-gate 	return (devnamesp[major].dn_flags & DN_LEAF_DRIVER);
72680Sstevel@tonic-gate }
72690Sstevel@tonic-gate 
72700Sstevel@tonic-gate /*
72710Sstevel@tonic-gate  * Multithreaded [un]configuration
72720Sstevel@tonic-gate  */
72730Sstevel@tonic-gate static struct mt_config_handle *
72740Sstevel@tonic-gate mt_config_init(dev_info_t *pdip, dev_info_t **dipp, int flags,
72750Sstevel@tonic-gate     major_t major, int op, struct brevq_node **brevqp)
72760Sstevel@tonic-gate {
72770Sstevel@tonic-gate 	struct mt_config_handle	*hdl = kmem_alloc(sizeof (*hdl), KM_SLEEP);
72780Sstevel@tonic-gate 
72790Sstevel@tonic-gate 	mutex_init(&hdl->mtc_lock, NULL, MUTEX_DEFAULT, NULL);
72800Sstevel@tonic-gate 	cv_init(&hdl->mtc_cv, NULL, CV_DEFAULT, NULL);
72810Sstevel@tonic-gate 	hdl->mtc_pdip = pdip;
72820Sstevel@tonic-gate 	hdl->mtc_fdip = dipp;
72837009Scth 	hdl->mtc_parmajor = DDI_MAJOR_T_NONE;
72840Sstevel@tonic-gate 	hdl->mtc_flags = flags;
72850Sstevel@tonic-gate 	hdl->mtc_major = major;
72860Sstevel@tonic-gate 	hdl->mtc_thr_count = 0;
72870Sstevel@tonic-gate 	hdl->mtc_op = op;
72880Sstevel@tonic-gate 	hdl->mtc_error = 0;
72890Sstevel@tonic-gate 	hdl->mtc_brevqp = brevqp;
72900Sstevel@tonic-gate 
72910Sstevel@tonic-gate #ifdef DEBUG
72920Sstevel@tonic-gate 	gethrestime(&hdl->start_time);
72930Sstevel@tonic-gate 	hdl->total_time = 0;
72940Sstevel@tonic-gate #endif /* DEBUG */
72950Sstevel@tonic-gate 
72960Sstevel@tonic-gate 	return (hdl);
72970Sstevel@tonic-gate }
72980Sstevel@tonic-gate 
72990Sstevel@tonic-gate #ifdef DEBUG
73000Sstevel@tonic-gate static int
73010Sstevel@tonic-gate time_diff_in_msec(timestruc_t start, timestruc_t end)
73020Sstevel@tonic-gate {
73030Sstevel@tonic-gate 	int	nsec, sec;
73040Sstevel@tonic-gate 
73050Sstevel@tonic-gate 	sec = end.tv_sec - start.tv_sec;
73060Sstevel@tonic-gate 	nsec = end.tv_nsec - start.tv_nsec;
73070Sstevel@tonic-gate 	if (nsec < 0) {
73080Sstevel@tonic-gate 		nsec += NANOSEC;
73090Sstevel@tonic-gate 		sec -= 1;
73100Sstevel@tonic-gate 	}
73110Sstevel@tonic-gate 
73120Sstevel@tonic-gate 	return (sec * (NANOSEC >> 20) + (nsec >> 20));
73130Sstevel@tonic-gate }
73140Sstevel@tonic-gate 
73150Sstevel@tonic-gate #endif	/* DEBUG */
73160Sstevel@tonic-gate 
73170Sstevel@tonic-gate static int
73180Sstevel@tonic-gate mt_config_fini(struct mt_config_handle *hdl)
73190Sstevel@tonic-gate {
73200Sstevel@tonic-gate 	int		rv;
73210Sstevel@tonic-gate #ifdef DEBUG
73220Sstevel@tonic-gate 	int		real_time;
73230Sstevel@tonic-gate 	timestruc_t	end_time;
73240Sstevel@tonic-gate #endif /* DEBUG */
73250Sstevel@tonic-gate 
73260Sstevel@tonic-gate 	mutex_enter(&hdl->mtc_lock);
73270Sstevel@tonic-gate 	while (hdl->mtc_thr_count > 0)
73280Sstevel@tonic-gate 		cv_wait(&hdl->mtc_cv, &hdl->mtc_lock);
73290Sstevel@tonic-gate 	rv = hdl->mtc_error;
73300Sstevel@tonic-gate 	mutex_exit(&hdl->mtc_lock);
73310Sstevel@tonic-gate 
73320Sstevel@tonic-gate #ifdef DEBUG
73330Sstevel@tonic-gate 	gethrestime(&end_time);
73340Sstevel@tonic-gate 	real_time = time_diff_in_msec(hdl->start_time, end_time);
73350Sstevel@tonic-gate 	if ((ddidebug & DDI_MTCONFIG) && hdl->mtc_pdip)
73360Sstevel@tonic-gate 		cmn_err(CE_NOTE,
73370Sstevel@tonic-gate 		    "config %s%d: total time %d msec, real time %d msec",
73384950Scth 		    ddi_driver_name(hdl->mtc_pdip),
73394950Scth 		    ddi_get_instance(hdl->mtc_pdip),
73404950Scth 		    hdl->total_time, real_time);
73410Sstevel@tonic-gate #endif /* DEBUG */
73420Sstevel@tonic-gate 
73430Sstevel@tonic-gate 	cv_destroy(&hdl->mtc_cv);
73440Sstevel@tonic-gate 	mutex_destroy(&hdl->mtc_lock);
73450Sstevel@tonic-gate 	kmem_free(hdl, sizeof (*hdl));
73460Sstevel@tonic-gate 
73470Sstevel@tonic-gate 	return (rv);
73480Sstevel@tonic-gate }
73490Sstevel@tonic-gate 
73500Sstevel@tonic-gate struct mt_config_data {
73510Sstevel@tonic-gate 	struct mt_config_handle	*mtc_hdl;
73520Sstevel@tonic-gate 	dev_info_t		*mtc_dip;
73530Sstevel@tonic-gate 	major_t			mtc_major;
73540Sstevel@tonic-gate 	int			mtc_flags;
73550Sstevel@tonic-gate 	struct brevq_node	*mtc_brn;
73560Sstevel@tonic-gate 	struct mt_config_data	*mtc_next;
73570Sstevel@tonic-gate };
73580Sstevel@tonic-gate 
73590Sstevel@tonic-gate static void
73600Sstevel@tonic-gate mt_config_thread(void *arg)
73610Sstevel@tonic-gate {
73620Sstevel@tonic-gate 	struct mt_config_data	*mcd = (struct mt_config_data *)arg;
73630Sstevel@tonic-gate 	struct mt_config_handle	*hdl = mcd->mtc_hdl;
73640Sstevel@tonic-gate 	dev_info_t		*dip = mcd->mtc_dip;
73650Sstevel@tonic-gate 	dev_info_t		*rdip, **dipp;
73660Sstevel@tonic-gate 	major_t			major = mcd->mtc_major;
73670Sstevel@tonic-gate 	int			flags = mcd->mtc_flags;
73680Sstevel@tonic-gate 	int			rv = 0;
73690Sstevel@tonic-gate 
73700Sstevel@tonic-gate #ifdef DEBUG
73710Sstevel@tonic-gate 	timestruc_t start_time, end_time;
73720Sstevel@tonic-gate 	gethrestime(&start_time);
73730Sstevel@tonic-gate #endif /* DEBUG */
73740Sstevel@tonic-gate 
73750Sstevel@tonic-gate 	rdip = NULL;
73760Sstevel@tonic-gate 	dipp = hdl->mtc_fdip ? &rdip : NULL;
73770Sstevel@tonic-gate 
73780Sstevel@tonic-gate 	switch (hdl->mtc_op) {
73790Sstevel@tonic-gate 	case MT_CONFIG_OP:
73800Sstevel@tonic-gate 		rv = devi_config_common(dip, flags, major);
73810Sstevel@tonic-gate 		break;
73820Sstevel@tonic-gate 	case MT_UNCONFIG_OP:
73830Sstevel@tonic-gate 		if (mcd->mtc_brn) {
73840Sstevel@tonic-gate 			struct brevq_node *brevq = NULL;
73850Sstevel@tonic-gate 			rv = devi_unconfig_common(dip, dipp, flags, major,
73860Sstevel@tonic-gate 			    &brevq);
73871317Scth 			mcd->mtc_brn->brn_child = brevq;
73880Sstevel@tonic-gate 		} else
73890Sstevel@tonic-gate 			rv = devi_unconfig_common(dip, dipp, flags, major,
73900Sstevel@tonic-gate 			    NULL);
73910Sstevel@tonic-gate 		break;
73920Sstevel@tonic-gate 	}
73930Sstevel@tonic-gate 
73940Sstevel@tonic-gate 	mutex_enter(&hdl->mtc_lock);
73950Sstevel@tonic-gate #ifdef DEBUG
73960Sstevel@tonic-gate 	gethrestime(&end_time);
73970Sstevel@tonic-gate 	hdl->total_time += time_diff_in_msec(start_time, end_time);
73980Sstevel@tonic-gate #endif /* DEBUG */
73992155Scth 
74002155Scth 	if ((rv != NDI_SUCCESS) && (hdl->mtc_error == 0)) {
74010Sstevel@tonic-gate 		hdl->mtc_error = rv;
74022155Scth #ifdef	DEBUG
74037009Scth 		if ((ddidebug & DDI_DEBUG) && (major != DDI_MAJOR_T_NONE)) {
74042155Scth 			char	*path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
74052155Scth 
74062155Scth 			(void) ddi_pathname(dip, path);
74072155Scth 			cmn_err(CE_NOTE, "mt_config_thread: "
74082155Scth 			    "op %d.%d.%x at %s failed %d",
74092155Scth 			    hdl->mtc_op, major, flags, path, rv);
74102155Scth 			kmem_free(path, MAXPATHLEN);
74112155Scth 		}
74122155Scth #endif	/* DEBUG */
74132155Scth 	}
74142155Scth 
74150Sstevel@tonic-gate 	if (hdl->mtc_fdip && *hdl->mtc_fdip == NULL) {
74160Sstevel@tonic-gate 		*hdl->mtc_fdip = rdip;
74170Sstevel@tonic-gate 		rdip = NULL;
74180Sstevel@tonic-gate 	}
74190Sstevel@tonic-gate 
74200Sstevel@tonic-gate 	if (rdip) {
74210Sstevel@tonic-gate 		ASSERT(rv != NDI_SUCCESS);
74220Sstevel@tonic-gate 		ndi_rele_devi(rdip);
74230Sstevel@tonic-gate 	}
74240Sstevel@tonic-gate 
74250Sstevel@tonic-gate 	ndi_rele_devi(dip);
74261826Sbm42561 
74271826Sbm42561 	if (--hdl->mtc_thr_count == 0)
74281826Sbm42561 		cv_broadcast(&hdl->mtc_cv);
74291826Sbm42561 	mutex_exit(&hdl->mtc_lock);
74300Sstevel@tonic-gate 	kmem_free(mcd, sizeof (*mcd));
74310Sstevel@tonic-gate }
74320Sstevel@tonic-gate 
74330Sstevel@tonic-gate /*
74340Sstevel@tonic-gate  * Multi-threaded config/unconfig of child nexus
74350Sstevel@tonic-gate  */
74360Sstevel@tonic-gate static void
74370Sstevel@tonic-gate mt_config_children(struct mt_config_handle *hdl)
74380Sstevel@tonic-gate {
74390Sstevel@tonic-gate 	dev_info_t		*pdip = hdl->mtc_pdip;
74400Sstevel@tonic-gate 	major_t			major = hdl->mtc_major;
74410Sstevel@tonic-gate 	dev_info_t		*dip;
74420Sstevel@tonic-gate 	int			circ;
74431317Scth 	struct brevq_node	*brn;
74440Sstevel@tonic-gate 	struct mt_config_data	*mcd_head = NULL;
74450Sstevel@tonic-gate 	struct mt_config_data	*mcd_tail = NULL;
74460Sstevel@tonic-gate 	struct mt_config_data	*mcd;
74470Sstevel@tonic-gate #ifdef DEBUG
74480Sstevel@tonic-gate 	timestruc_t		end_time;
74490Sstevel@tonic-gate 
74500Sstevel@tonic-gate 	/* Update total_time in handle */
74510Sstevel@tonic-gate 	gethrestime(&end_time);
74520Sstevel@tonic-gate 	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
74530Sstevel@tonic-gate #endif
74540Sstevel@tonic-gate 
74550Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
74560Sstevel@tonic-gate 	dip = ddi_get_child(pdip);
74570Sstevel@tonic-gate 	while (dip) {
74580Sstevel@tonic-gate 		if (hdl->mtc_op == MT_UNCONFIG_OP && hdl->mtc_brevqp &&
74590Sstevel@tonic-gate 		    !(DEVI_EVREMOVE(dip)) &&
74600Sstevel@tonic-gate 		    i_ddi_node_state(dip) >= DS_INITIALIZED) {
74610Sstevel@tonic-gate 			/*
74620Sstevel@tonic-gate 			 * Enqueue this dip's deviname.
74630Sstevel@tonic-gate 			 * No need to hold a lock while enqueuing since this
74640Sstevel@tonic-gate 			 * is the only thread doing the enqueue and no one
74650Sstevel@tonic-gate 			 * walks the queue while we are in multithreaded
74660Sstevel@tonic-gate 			 * unconfiguration.
74670Sstevel@tonic-gate 			 */
74680Sstevel@tonic-gate 			brn = brevq_enqueue(hdl->mtc_brevqp, dip, NULL);
74691317Scth 		} else
74701317Scth 			brn = NULL;
74710Sstevel@tonic-gate 
74720Sstevel@tonic-gate 		/*
74730Sstevel@tonic-gate 		 * Hold the child that we are processing so he does not get
74740Sstevel@tonic-gate 		 * removed. The corrisponding ndi_rele_devi() for children
74750Sstevel@tonic-gate 		 * that are not being skipped is done at the end of
74760Sstevel@tonic-gate 		 * mt_config_thread().
74770Sstevel@tonic-gate 		 */
74780Sstevel@tonic-gate 		ndi_hold_devi(dip);
74790Sstevel@tonic-gate 
74800Sstevel@tonic-gate 		/*
74810Sstevel@tonic-gate 		 * skip leaf nodes and (for configure) nodes not
74820Sstevel@tonic-gate 		 * fully attached.
74830Sstevel@tonic-gate 		 */
74840Sstevel@tonic-gate 		if (is_leaf_node(dip) ||
74850Sstevel@tonic-gate 		    (hdl->mtc_op == MT_CONFIG_OP &&
74860Sstevel@tonic-gate 		    i_ddi_node_state(dip) < DS_READY)) {
74870Sstevel@tonic-gate 			ndi_rele_devi(dip);
74880Sstevel@tonic-gate 			dip = ddi_get_next_sibling(dip);
74890Sstevel@tonic-gate 			continue;
74900Sstevel@tonic-gate 		}
74910Sstevel@tonic-gate 
74920Sstevel@tonic-gate 		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
74930Sstevel@tonic-gate 		mcd->mtc_dip = dip;
74940Sstevel@tonic-gate 		mcd->mtc_hdl = hdl;
74950Sstevel@tonic-gate 		mcd->mtc_brn = brn;
74960Sstevel@tonic-gate 
74970Sstevel@tonic-gate 		/*
74980Sstevel@tonic-gate 		 * Switch a 'driver' operation to an 'all' operation below a
74990Sstevel@tonic-gate 		 * node bound to the driver.
75000Sstevel@tonic-gate 		 */
75017009Scth 		if ((major == DDI_MAJOR_T_NONE) ||
75027009Scth 		    (major == ddi_driver_major(dip)))
75037009Scth 			mcd->mtc_major = DDI_MAJOR_T_NONE;
75040Sstevel@tonic-gate 		else
75050Sstevel@tonic-gate 			mcd->mtc_major = major;
75060Sstevel@tonic-gate 
75070Sstevel@tonic-gate 		/*
75080Sstevel@tonic-gate 		 * The unconfig-driver to unconfig-all conversion above
75090Sstevel@tonic-gate 		 * constitutes an autodetach for NDI_DETACH_DRIVER calls,
75100Sstevel@tonic-gate 		 * set NDI_AUTODETACH.
75110Sstevel@tonic-gate 		 */
75120Sstevel@tonic-gate 		mcd->mtc_flags = hdl->mtc_flags;
75130Sstevel@tonic-gate 		if ((mcd->mtc_flags & NDI_DETACH_DRIVER) &&
75140Sstevel@tonic-gate 		    (hdl->mtc_op == MT_UNCONFIG_OP) &&
75150Sstevel@tonic-gate 		    (major == ddi_driver_major(pdip)))
75160Sstevel@tonic-gate 			mcd->mtc_flags |= NDI_AUTODETACH;
75170Sstevel@tonic-gate 
75180Sstevel@tonic-gate 		mutex_enter(&hdl->mtc_lock);
75190Sstevel@tonic-gate 		hdl->mtc_thr_count++;
75200Sstevel@tonic-gate 		mutex_exit(&hdl->mtc_lock);
75210Sstevel@tonic-gate 
75220Sstevel@tonic-gate 		/*
75230Sstevel@tonic-gate 		 * Add to end of list to process after ndi_devi_exit to avoid
75240Sstevel@tonic-gate 		 * locking differences depending on value of mtc_off.
75250Sstevel@tonic-gate 		 */
75260Sstevel@tonic-gate 		mcd->mtc_next = NULL;
75270Sstevel@tonic-gate 		if (mcd_head == NULL)
75280Sstevel@tonic-gate 			mcd_head = mcd;
75290Sstevel@tonic-gate 		else
75300Sstevel@tonic-gate 			mcd_tail->mtc_next = mcd;
75310Sstevel@tonic-gate 		mcd_tail = mcd;
75320Sstevel@tonic-gate 
75330Sstevel@tonic-gate 		dip = ddi_get_next_sibling(dip);
75340Sstevel@tonic-gate 	}
75350Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
75360Sstevel@tonic-gate 
75370Sstevel@tonic-gate 	/* go through the list of held children */
75380Sstevel@tonic-gate 	for (mcd = mcd_head; mcd; mcd = mcd_head) {
75390Sstevel@tonic-gate 		mcd_head = mcd->mtc_next;
75402155Scth 		if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
75410Sstevel@tonic-gate 			mt_config_thread(mcd);
75420Sstevel@tonic-gate 		else
75430Sstevel@tonic-gate 			(void) thread_create(NULL, 0, mt_config_thread, mcd,
75440Sstevel@tonic-gate 			    0, &p0, TS_RUN, minclsyspri);
75450Sstevel@tonic-gate 	}
75460Sstevel@tonic-gate }
75470Sstevel@tonic-gate 
75480Sstevel@tonic-gate static void
75490Sstevel@tonic-gate mt_config_driver(struct mt_config_handle *hdl)
75500Sstevel@tonic-gate {
75510Sstevel@tonic-gate 	major_t			par_major = hdl->mtc_parmajor;
75520Sstevel@tonic-gate 	major_t			major = hdl->mtc_major;
75530Sstevel@tonic-gate 	struct devnames		*dnp = &devnamesp[par_major];
75540Sstevel@tonic-gate 	dev_info_t		*dip;
75550Sstevel@tonic-gate 	struct mt_config_data	*mcd_head = NULL;
75560Sstevel@tonic-gate 	struct mt_config_data	*mcd_tail = NULL;
75570Sstevel@tonic-gate 	struct mt_config_data	*mcd;
75580Sstevel@tonic-gate #ifdef DEBUG
75590Sstevel@tonic-gate 	timestruc_t		end_time;
75600Sstevel@tonic-gate 
75610Sstevel@tonic-gate 	/* Update total_time in handle */
75620Sstevel@tonic-gate 	gethrestime(&end_time);
75630Sstevel@tonic-gate 	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
75640Sstevel@tonic-gate #endif
75657009Scth 	ASSERT(par_major != DDI_MAJOR_T_NONE);
75667009Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
75670Sstevel@tonic-gate 
75680Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
75690Sstevel@tonic-gate 	dip = devnamesp[par_major].dn_head;
75700Sstevel@tonic-gate 	while (dip) {
75710Sstevel@tonic-gate 		/*
75720Sstevel@tonic-gate 		 * Hold the child that we are processing so he does not get
75730Sstevel@tonic-gate 		 * removed. The corrisponding ndi_rele_devi() for children
75740Sstevel@tonic-gate 		 * that are not being skipped is done at the end of
75750Sstevel@tonic-gate 		 * mt_config_thread().
75760Sstevel@tonic-gate 		 */
75770Sstevel@tonic-gate 		ndi_hold_devi(dip);
75780Sstevel@tonic-gate 
75790Sstevel@tonic-gate 		/* skip leaf nodes and nodes not fully attached */
75801333Scth 		if (!i_ddi_devi_attached(dip) || is_leaf_node(dip)) {
75810Sstevel@tonic-gate 			ndi_rele_devi(dip);
75820Sstevel@tonic-gate 			dip = ddi_get_next(dip);
75830Sstevel@tonic-gate 			continue;
75840Sstevel@tonic-gate 		}
75850Sstevel@tonic-gate 
75860Sstevel@tonic-gate 		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
75870Sstevel@tonic-gate 		mcd->mtc_dip = dip;
75880Sstevel@tonic-gate 		mcd->mtc_hdl = hdl;
75890Sstevel@tonic-gate 		mcd->mtc_major = major;
75900Sstevel@tonic-gate 		mcd->mtc_flags = hdl->mtc_flags;
75910Sstevel@tonic-gate 
75920Sstevel@tonic-gate 		mutex_enter(&hdl->mtc_lock);
75930Sstevel@tonic-gate 		hdl->mtc_thr_count++;
75940Sstevel@tonic-gate 		mutex_exit(&hdl->mtc_lock);
75950Sstevel@tonic-gate 
75960Sstevel@tonic-gate 		/*
75970Sstevel@tonic-gate 		 * Add to end of list to process after UNLOCK_DEV_OPS to avoid
75980Sstevel@tonic-gate 		 * locking differences depending on value of mtc_off.
75990Sstevel@tonic-gate 		 */
76000Sstevel@tonic-gate 		mcd->mtc_next = NULL;
76010Sstevel@tonic-gate 		if (mcd_head == NULL)
76020Sstevel@tonic-gate 			mcd_head = mcd;
76030Sstevel@tonic-gate 		else
76040Sstevel@tonic-gate 			mcd_tail->mtc_next = mcd;
76050Sstevel@tonic-gate 		mcd_tail = mcd;
76060Sstevel@tonic-gate 
76070Sstevel@tonic-gate 		dip = ddi_get_next(dip);
76080Sstevel@tonic-gate 	}
76090Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
76100Sstevel@tonic-gate 
76110Sstevel@tonic-gate 	/* go through the list of held children */
76120Sstevel@tonic-gate 	for (mcd = mcd_head; mcd; mcd = mcd_head) {
76130Sstevel@tonic-gate 		mcd_head = mcd->mtc_next;
76142155Scth 		if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
76150Sstevel@tonic-gate 			mt_config_thread(mcd);
76160Sstevel@tonic-gate 		else
76170Sstevel@tonic-gate 			(void) thread_create(NULL, 0, mt_config_thread, mcd,
76180Sstevel@tonic-gate 			    0, &p0, TS_RUN, minclsyspri);
76190Sstevel@tonic-gate 	}
76200Sstevel@tonic-gate }
76210Sstevel@tonic-gate 
76220Sstevel@tonic-gate /*
76230Sstevel@tonic-gate  * Given the nodeid for a persistent (PROM or SID) node, return
76240Sstevel@tonic-gate  * the corresponding devinfo node
76250Sstevel@tonic-gate  * NOTE: This function will return NULL for .conf nodeids.
76260Sstevel@tonic-gate  */
76270Sstevel@tonic-gate dev_info_t *
7628789Sahrens e_ddi_nodeid_to_dip(pnode_t nodeid)
76290Sstevel@tonic-gate {
76300Sstevel@tonic-gate 	dev_info_t		*dip = NULL;
76310Sstevel@tonic-gate 	struct devi_nodeid	*prev, *elem;
76320Sstevel@tonic-gate 
76330Sstevel@tonic-gate 	mutex_enter(&devimap->dno_lock);
76340Sstevel@tonic-gate 
76350Sstevel@tonic-gate 	prev = NULL;
76360Sstevel@tonic-gate 	for (elem = devimap->dno_head; elem; elem = elem->next) {
76370Sstevel@tonic-gate 		if (elem->nodeid == nodeid) {
76380Sstevel@tonic-gate 			ndi_hold_devi(elem->dip);
76390Sstevel@tonic-gate 			dip = elem->dip;
76400Sstevel@tonic-gate 			break;
76410Sstevel@tonic-gate 		}
76420Sstevel@tonic-gate 		prev = elem;
76430Sstevel@tonic-gate 	}
76440Sstevel@tonic-gate 
76450Sstevel@tonic-gate 	/*
76460Sstevel@tonic-gate 	 * Move to head for faster lookup next time
76470Sstevel@tonic-gate 	 */
76480Sstevel@tonic-gate 	if (elem && prev) {
76490Sstevel@tonic-gate 		prev->next = elem->next;
76500Sstevel@tonic-gate 		elem->next = devimap->dno_head;
76510Sstevel@tonic-gate 		devimap->dno_head = elem;
76520Sstevel@tonic-gate 	}
76530Sstevel@tonic-gate 
76540Sstevel@tonic-gate 	mutex_exit(&devimap->dno_lock);
76550Sstevel@tonic-gate 	return (dip);
76560Sstevel@tonic-gate }
76570Sstevel@tonic-gate 
76580Sstevel@tonic-gate static void
76590Sstevel@tonic-gate free_cache_task(void *arg)
76600Sstevel@tonic-gate {
76610Sstevel@tonic-gate 	ASSERT(arg == NULL);
76620Sstevel@tonic-gate 
76630Sstevel@tonic-gate 	mutex_enter(&di_cache.cache_lock);
76640Sstevel@tonic-gate 
76650Sstevel@tonic-gate 	/*
76660Sstevel@tonic-gate 	 * The cache can be invalidated without holding the lock
76670Sstevel@tonic-gate 	 * but it can be made valid again only while the lock is held.
76680Sstevel@tonic-gate 	 * So if the cache is invalid when the lock is held, it will
76690Sstevel@tonic-gate 	 * stay invalid until lock is released.
76700Sstevel@tonic-gate 	 */
76710Sstevel@tonic-gate 	if (!di_cache.cache_valid)
76720Sstevel@tonic-gate 		i_ddi_di_cache_free(&di_cache);
76730Sstevel@tonic-gate 
76740Sstevel@tonic-gate 	mutex_exit(&di_cache.cache_lock);
76750Sstevel@tonic-gate 
76760Sstevel@tonic-gate 	if (di_cache_debug)
76770Sstevel@tonic-gate 		cmn_err(CE_NOTE, "system_taskq: di_cache freed");
76780Sstevel@tonic-gate }
76790Sstevel@tonic-gate 
76800Sstevel@tonic-gate extern int modrootloaded;
76810Sstevel@tonic-gate 
76820Sstevel@tonic-gate void
76830Sstevel@tonic-gate i_ddi_di_cache_free(struct di_cache *cache)
76840Sstevel@tonic-gate {
76850Sstevel@tonic-gate 	int	error;
76867656SSherry.Moore@Sun.COM 	extern int sys_shutdown;
76870Sstevel@tonic-gate 
76880Sstevel@tonic-gate 	ASSERT(mutex_owned(&cache->cache_lock));
76890Sstevel@tonic-gate 
76900Sstevel@tonic-gate 	if (cache->cache_size) {
76910Sstevel@tonic-gate 		ASSERT(cache->cache_size > 0);
76920Sstevel@tonic-gate 		ASSERT(cache->cache_data);
76930Sstevel@tonic-gate 
76940Sstevel@tonic-gate 		kmem_free(cache->cache_data, cache->cache_size);
76950Sstevel@tonic-gate 		cache->cache_data = NULL;
76960Sstevel@tonic-gate 		cache->cache_size = 0;
76970Sstevel@tonic-gate 
76980Sstevel@tonic-gate 		if (di_cache_debug)
76990Sstevel@tonic-gate 			cmn_err(CE_NOTE, "i_ddi_di_cache_free: freed cachemem");
77000Sstevel@tonic-gate 	} else {
77010Sstevel@tonic-gate 		ASSERT(cache->cache_data == NULL);
77020Sstevel@tonic-gate 		if (di_cache_debug)
77030Sstevel@tonic-gate 			cmn_err(CE_NOTE, "i_ddi_di_cache_free: NULL cache");
77040Sstevel@tonic-gate 	}
77050Sstevel@tonic-gate 
77067576SJerry.Gilliam@Sun.COM 	if (!modrootloaded || rootvp == NULL ||
77077576SJerry.Gilliam@Sun.COM 	    vn_is_readonly(rootvp) || sys_shutdown) {
77080Sstevel@tonic-gate 		if (di_cache_debug) {
77090Sstevel@tonic-gate 			cmn_err(CE_WARN, "/ not mounted/RDONLY. Skip unlink");
77100Sstevel@tonic-gate 		}
77110Sstevel@tonic-gate 		return;
77120Sstevel@tonic-gate 	}
77130Sstevel@tonic-gate 
77140Sstevel@tonic-gate 	error = vn_remove(DI_CACHE_FILE, UIO_SYSSPACE, RMFILE);
77150Sstevel@tonic-gate 	if (di_cache_debug && error && error != ENOENT) {
77160Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: unlink failed: %d", DI_CACHE_FILE, error);
77170Sstevel@tonic-gate 	} else if (di_cache_debug && !error) {
77180Sstevel@tonic-gate 		cmn_err(CE_NOTE, "i_ddi_di_cache_free: unlinked cache file");
77190Sstevel@tonic-gate 	}
77200Sstevel@tonic-gate }
77210Sstevel@tonic-gate 
77220Sstevel@tonic-gate void
77230Sstevel@tonic-gate i_ddi_di_cache_invalidate(int kmflag)
77240Sstevel@tonic-gate {
77256065Scth 	int	cache_valid;
77260Sstevel@tonic-gate 
77270Sstevel@tonic-gate 	if (!modrootloaded || !i_ddi_io_initialized()) {
77280Sstevel@tonic-gate 		if (di_cache_debug)
77290Sstevel@tonic-gate 			cmn_err(CE_NOTE, "I/O not inited. Skipping invalidate");
77300Sstevel@tonic-gate 		return;
77310Sstevel@tonic-gate 	}
77320Sstevel@tonic-gate 
77336065Scth 	/* Increment devtree generation number. */
77342621Sllai1 	atomic_inc_ulong(&devtree_gen);
77350Sstevel@tonic-gate 
77366065Scth 	/* Invalidate the in-core cache and dispatch free on valid->invalid */
77376065Scth 	cache_valid = atomic_swap_uint(&di_cache.cache_valid, 0);
77386065Scth 	if (cache_valid) {
77396065Scth 		(void) taskq_dispatch(system_taskq, free_cache_task, NULL,
77406065Scth 		    (kmflag == KM_SLEEP) ? TQ_SLEEP : TQ_NOSLEEP);
77416065Scth 	}
77420Sstevel@tonic-gate 
77430Sstevel@tonic-gate 	if (di_cache_debug) {
77440Sstevel@tonic-gate 		cmn_err(CE_NOTE, "invalidation with km_flag: %s",
77450Sstevel@tonic-gate 		    kmflag == KM_SLEEP ? "KM_SLEEP" : "KM_NOSLEEP");
77460Sstevel@tonic-gate 	}
77470Sstevel@tonic-gate }
77480Sstevel@tonic-gate 
77490Sstevel@tonic-gate 
77500Sstevel@tonic-gate static void
77510Sstevel@tonic-gate i_bind_vhci_node(dev_info_t *dip)
77520Sstevel@tonic-gate {
77534145Scth 	DEVI(dip)->devi_major = ddi_name_to_major(ddi_node_name(dip));
77540Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_BOUND);
77550Sstevel@tonic-gate }
77560Sstevel@tonic-gate 
77570Sstevel@tonic-gate static char vhci_node_addr[2];
77580Sstevel@tonic-gate 
77590Sstevel@tonic-gate static int
77600Sstevel@tonic-gate i_init_vhci_node(dev_info_t *dip)
77610Sstevel@tonic-gate {
77620Sstevel@tonic-gate 	add_global_props(dip);
77630Sstevel@tonic-gate 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
77640Sstevel@tonic-gate 	if (DEVI(dip)->devi_ops == NULL)
77650Sstevel@tonic-gate 		return (-1);
77660Sstevel@tonic-gate 
77670Sstevel@tonic-gate 	DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
77680Sstevel@tonic-gate 	e_ddi_keep_instance(dip);
77690Sstevel@tonic-gate 	vhci_node_addr[0]	= '\0';
77700Sstevel@tonic-gate 	ddi_set_name_addr(dip, vhci_node_addr);
77710Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_INITIALIZED);
77720Sstevel@tonic-gate 	return (0);
77730Sstevel@tonic-gate }
77740Sstevel@tonic-gate 
77750Sstevel@tonic-gate static void
77760Sstevel@tonic-gate i_link_vhci_node(dev_info_t *dip)
77770Sstevel@tonic-gate {
77781093Shiremath 	ASSERT(MUTEX_HELD(&global_vhci_lock));
77791093Shiremath 
77800Sstevel@tonic-gate 	/*
77810Sstevel@tonic-gate 	 * scsi_vhci should be kept left most of the device tree.
77820Sstevel@tonic-gate 	 */
77830Sstevel@tonic-gate 	if (scsi_vhci_dip) {
77840Sstevel@tonic-gate 		DEVI(dip)->devi_sibling = DEVI(scsi_vhci_dip)->devi_sibling;
77850Sstevel@tonic-gate 		DEVI(scsi_vhci_dip)->devi_sibling = DEVI(dip);
77860Sstevel@tonic-gate 	} else {
77870Sstevel@tonic-gate 		DEVI(dip)->devi_sibling = DEVI(top_devinfo)->devi_child;
77880Sstevel@tonic-gate 		DEVI(top_devinfo)->devi_child = DEVI(dip);
77890Sstevel@tonic-gate 	}
77900Sstevel@tonic-gate }
77910Sstevel@tonic-gate 
77920Sstevel@tonic-gate 
77930Sstevel@tonic-gate /*
77940Sstevel@tonic-gate  * This a special routine to enumerate vhci node (child of rootnex
77950Sstevel@tonic-gate  * node) without holding the ndi_devi_enter() lock. The device node
77960Sstevel@tonic-gate  * is allocated, initialized and brought into DS_READY state before
77970Sstevel@tonic-gate  * inserting into the device tree. The VHCI node is handcrafted
77980Sstevel@tonic-gate  * here to bring the node to DS_READY, similar to rootnex node.
77990Sstevel@tonic-gate  *
78000Sstevel@tonic-gate  * The global_vhci_lock protects linking the node into the device
78010Sstevel@tonic-gate  * as same lock is held before linking/unlinking any direct child
78020Sstevel@tonic-gate  * of rootnex children.
78030Sstevel@tonic-gate  *
78040Sstevel@tonic-gate  * This routine is a workaround to handle a possible deadlock
78050Sstevel@tonic-gate  * that occurs while trying to enumerate node in a different sub-tree
78060Sstevel@tonic-gate  * during _init/_attach entry points.
78070Sstevel@tonic-gate  */
78080Sstevel@tonic-gate /*ARGSUSED*/
78090Sstevel@tonic-gate dev_info_t *
78100Sstevel@tonic-gate ndi_devi_config_vhci(char *drvname, int flags)
78110Sstevel@tonic-gate {
78120Sstevel@tonic-gate 	struct devnames		*dnp;
78130Sstevel@tonic-gate 	dev_info_t		*dip;
78140Sstevel@tonic-gate 	major_t			major = ddi_name_to_major(drvname);
78150Sstevel@tonic-gate 
78160Sstevel@tonic-gate 	if (major == -1)
78170Sstevel@tonic-gate 		return (NULL);
78180Sstevel@tonic-gate 
78190Sstevel@tonic-gate 	/* Make sure we create the VHCI node only once */
78200Sstevel@tonic-gate 	dnp = &devnamesp[major];
78210Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
78220Sstevel@tonic-gate 	if (dnp->dn_head) {
78230Sstevel@tonic-gate 		dip = dnp->dn_head;
78240Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
78250Sstevel@tonic-gate 		return (dip);
78260Sstevel@tonic-gate 	}
78270Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
78280Sstevel@tonic-gate 
78290Sstevel@tonic-gate 	/* Allocate the VHCI node */
78300Sstevel@tonic-gate 	ndi_devi_alloc_sleep(top_devinfo, drvname, DEVI_SID_NODEID, &dip);
78310Sstevel@tonic-gate 	ndi_hold_devi(dip);
78320Sstevel@tonic-gate 
78330Sstevel@tonic-gate 	/* Mark the node as VHCI */
78340Sstevel@tonic-gate 	DEVI(dip)->devi_node_attributes |= DDI_VHCI_NODE;
78350Sstevel@tonic-gate 
78360Sstevel@tonic-gate 	i_ddi_add_devimap(dip);
78370Sstevel@tonic-gate 	i_bind_vhci_node(dip);
78380Sstevel@tonic-gate 	if (i_init_vhci_node(dip) == -1) {
78390Sstevel@tonic-gate 		ndi_rele_devi(dip);
78400Sstevel@tonic-gate 		(void) ndi_devi_free(dip);
78410Sstevel@tonic-gate 		return (NULL);
78420Sstevel@tonic-gate 	}
78430Sstevel@tonic-gate 
7844495Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
78450Sstevel@tonic-gate 	DEVI_SET_ATTACHING(dip);
7846495Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
7847495Scth 
78480Sstevel@tonic-gate 	if (devi_attach(dip, DDI_ATTACH) != DDI_SUCCESS) {
78490Sstevel@tonic-gate 		cmn_err(CE_CONT, "Could not attach %s driver", drvname);
78500Sstevel@tonic-gate 		e_ddi_free_instance(dip, vhci_node_addr);
78510Sstevel@tonic-gate 		ndi_rele_devi(dip);
78520Sstevel@tonic-gate 		(void) ndi_devi_free(dip);
78530Sstevel@tonic-gate 		return (NULL);
78540Sstevel@tonic-gate 	}
7855495Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
78560Sstevel@tonic-gate 	DEVI_CLR_ATTACHING(dip);
7857495Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
78580Sstevel@tonic-gate 
78591093Shiremath 	mutex_enter(&global_vhci_lock);
78600Sstevel@tonic-gate 	i_link_vhci_node(dip);
78611093Shiremath 	mutex_exit(&global_vhci_lock);
78620Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_READY);
78630Sstevel@tonic-gate 
78640Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
78650Sstevel@tonic-gate 	dnp->dn_flags |= DN_DRIVER_HELD;
78660Sstevel@tonic-gate 	dnp->dn_head = dip;
78670Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
78680Sstevel@tonic-gate 
78690Sstevel@tonic-gate 	i_ndi_devi_report_status_change(dip, NULL);
78700Sstevel@tonic-gate 
78710Sstevel@tonic-gate 	return (dip);
78720Sstevel@tonic-gate }
78730Sstevel@tonic-gate 
78740Sstevel@tonic-gate /*
78750Sstevel@tonic-gate  * ibt_hw_is_present() returns 0 when there is no IB hardware actively
78760Sstevel@tonic-gate  * running.  This is primarily useful for modules like rpcmod which
78770Sstevel@tonic-gate  * needs a quick check to decide whether or not it should try to use
78780Sstevel@tonic-gate  * InfiniBand
78790Sstevel@tonic-gate  */
78800Sstevel@tonic-gate int ib_hw_status = 0;
78810Sstevel@tonic-gate int
78820Sstevel@tonic-gate ibt_hw_is_present()
78830Sstevel@tonic-gate {
78840Sstevel@tonic-gate 	return (ib_hw_status);
78850Sstevel@tonic-gate }
78864845Svikram 
78874845Svikram /*
78884845Svikram  * ASSERT that constraint flag is not set and then set the "retire attempt"
78894845Svikram  * flag.
78904845Svikram  */
78914845Svikram int
78924845Svikram e_ddi_mark_retiring(dev_info_t *dip, void *arg)
78934845Svikram {
78944845Svikram 	char	**cons_array = (char **)arg;
78954845Svikram 	char	*path;
78964845Svikram 	int	constraint;
78974845Svikram 	int	i;
78984845Svikram 
78994845Svikram 	constraint = 0;
79004845Svikram 	if (cons_array) {
79014845Svikram 		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
79024845Svikram 		(void) ddi_pathname(dip, path);
79034845Svikram 		for (i = 0; cons_array[i] != NULL; i++) {
79044845Svikram 			if (strcmp(path, cons_array[i]) == 0) {
79054845Svikram 				constraint = 1;
79064845Svikram 				break;
79074845Svikram 			}
79084845Svikram 		}
79094845Svikram 		kmem_free(path, MAXPATHLEN);
79104845Svikram 	}
79114845Svikram 
79124845Svikram 	mutex_enter(&DEVI(dip)->devi_lock);
79134845Svikram 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
79144845Svikram 	DEVI(dip)->devi_flags |= DEVI_RETIRING;
79154845Svikram 	if (constraint)
79164845Svikram 		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
79174845Svikram 	mutex_exit(&DEVI(dip)->devi_lock);
79184845Svikram 
79194845Svikram 	RIO_VERBOSE((CE_NOTE, "marked dip as undergoing retire process dip=%p",
79204845Svikram 	    (void *)dip));
79214845Svikram 
79224845Svikram 	if (constraint)
79234845Svikram 		RIO_DEBUG((CE_NOTE, "marked dip as constrained, dip=%p",
79244845Svikram 		    (void *)dip));
79254845Svikram 
79264845Svikram 	if (MDI_PHCI(dip))
79274845Svikram 		mdi_phci_mark_retiring(dip, cons_array);
79284845Svikram 
79294845Svikram 	return (DDI_WALK_CONTINUE);
79304845Svikram }
79314845Svikram 
79324845Svikram static void
79334845Svikram free_array(char **cons_array)
79344845Svikram {
79354845Svikram 	int	i;
79364845Svikram 
79374845Svikram 	if (cons_array == NULL)
79384845Svikram 		return;
79394845Svikram 
79404845Svikram 	for (i = 0; cons_array[i] != NULL; i++) {
79414845Svikram 		kmem_free(cons_array[i], strlen(cons_array[i]) + 1);
79424845Svikram 	}
79434845Svikram 	kmem_free(cons_array, (i+1) * sizeof (char *));
79444845Svikram }
79454845Svikram 
79464845Svikram /*
79474845Svikram  * Walk *every* node in subtree and check if it blocks, allows or has no
79484845Svikram  * comment on a proposed retire.
79494845Svikram  */
79504845Svikram int
79514845Svikram e_ddi_retire_notify(dev_info_t *dip, void *arg)
79524845Svikram {
79534845Svikram 	int	*constraint = (int *)arg;
79544845Svikram 
79554845Svikram 	RIO_DEBUG((CE_NOTE, "retire notify: dip = %p", (void *)dip));
79564845Svikram 
79574845Svikram 	(void) e_ddi_offline_notify(dip);
79584845Svikram 
79594845Svikram 	mutex_enter(&(DEVI(dip)->devi_lock));
79604845Svikram 	if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) {
79614845Svikram 		RIO_DEBUG((CE_WARN, "retire notify: dip in retire "
79624845Svikram 		    "subtree is not marked: dip = %p", (void *)dip));
79634845Svikram 		*constraint = 0;
79644845Svikram 	} else if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) {
79654845Svikram 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
79664845Svikram 		RIO_DEBUG((CE_NOTE, "retire notify: BLOCKED: dip = %p",
79674845Svikram 		    (void *)dip));
79684845Svikram 		*constraint = 0;
79694845Svikram 	} else if (!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT)) {
79704845Svikram 		RIO_DEBUG((CE_NOTE, "retire notify: NO CONSTRAINT: "
79714845Svikram 		    "dip = %p", (void *)dip));
79724845Svikram 		*constraint = 0;
79734845Svikram 	} else {
79744845Svikram 		RIO_DEBUG((CE_NOTE, "retire notify: CONSTRAINT set: "
79754845Svikram 		    "dip = %p", (void *)dip));
79764845Svikram 	}
79774845Svikram 	mutex_exit(&DEVI(dip)->devi_lock);
79784845Svikram 
79794845Svikram 	if (MDI_PHCI(dip))
79804845Svikram 		mdi_phci_retire_notify(dip, constraint);
79814845Svikram 
79824845Svikram 	return (DDI_WALK_CONTINUE);
79834845Svikram }
79844845Svikram 
79854845Svikram int
79864845Svikram e_ddi_retire_finalize(dev_info_t *dip, void *arg)
79874845Svikram {
79884845Svikram 	int constraint = *(int *)arg;
79894845Svikram 	int finalize;
79904845Svikram 	int phci_only;
79914845Svikram 
79924845Svikram 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
79934845Svikram 
79944845Svikram 	mutex_enter(&DEVI(dip)->devi_lock);
79954845Svikram 	if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) {
79964845Svikram 		RIO_DEBUG((CE_WARN,
79974845Svikram 		    "retire: unmarked dip(%p) in retire subtree",
79984845Svikram 		    (void *)dip));
79994845Svikram 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRED));
80004845Svikram 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
80014845Svikram 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
80024845Svikram 		mutex_exit(&DEVI(dip)->devi_lock);
80034845Svikram 		return (DDI_WALK_CONTINUE);
80044845Svikram 	}
80054845Svikram 
80064845Svikram 	/*
80074845Svikram 	 * retire the device if constraints have been applied
80084845Svikram 	 * or if the device is not in use
80094845Svikram 	 */
80104845Svikram 	finalize = 0;
80114845Svikram 	if (constraint) {
80124845Svikram 		ASSERT(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT);
80134845Svikram 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
80144845Svikram 		DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
80154845Svikram 		DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
80164845Svikram 		DEVI(dip)->devi_flags |= DEVI_RETIRED;
80174845Svikram 		mutex_exit(&DEVI(dip)->devi_lock);
80184845Svikram 		(void) spec_fence_snode(dip, NULL);
80194845Svikram 		RIO_DEBUG((CE_NOTE, "Fenced off: dip = %p", (void *)dip));
80204845Svikram 		e_ddi_offline_finalize(dip, DDI_SUCCESS);
80214845Svikram 	} else {
80224845Svikram 		if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) {
80234845Svikram 			ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
80244845Svikram 			DEVI(dip)->devi_flags &= ~DEVI_R_BLOCKED;
80254845Svikram 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
80264845Svikram 			/* we have already finalized during notify */
80274845Svikram 		} else if (DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT) {
80284845Svikram 			DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
80294845Svikram 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
80304845Svikram 			finalize = 1;
80314845Svikram 		} else {
80324845Svikram 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
80334845Svikram 			/*
80344845Svikram 			 * even if no contracts, need to call finalize
80354845Svikram 			 * to clear the contract barrier on the dip
80364845Svikram 			 */
80374845Svikram 			finalize = 1;
80384845Svikram 		}
80394845Svikram 		mutex_exit(&DEVI(dip)->devi_lock);
80404845Svikram 		RIO_DEBUG((CE_NOTE, "finalize: NOT retired: dip = %p",
80414845Svikram 		    (void *)dip));
80424845Svikram 		if (finalize)
80434845Svikram 			e_ddi_offline_finalize(dip, DDI_FAILURE);
80444845Svikram 	}
80454845Svikram 
80464845Svikram 	/*
80474845Svikram 	 * phci_only variable indicates no client checking, just
80484845Svikram 	 * offline the PHCI. We set that to 0 to enable client
80494845Svikram 	 * checking
80504845Svikram 	 */
80514845Svikram 	phci_only = 0;
80524845Svikram 	if (MDI_PHCI(dip))
80534845Svikram 		mdi_phci_retire_finalize(dip, phci_only);
80544845Svikram 
80554845Svikram 	return (DDI_WALK_CONTINUE);
80564845Svikram }
80574845Svikram 
80584845Svikram /*
80594845Svikram  * Returns
80608912SChris.Horne@Sun.COM  *	DDI_SUCCESS if constraints allow retire
80614845Svikram  *	DDI_FAILURE if constraints don't allow retire.
80624845Svikram  * cons_array is a NULL terminated array of node paths for
80634845Svikram  * which constraints have already been applied.
80644845Svikram  */
80654845Svikram int
80664845Svikram e_ddi_retire_device(char *path, char **cons_array)
80674845Svikram {
80684845Svikram 	dev_info_t	*dip;
80694845Svikram 	dev_info_t	*pdip;
80704845Svikram 	int		circ;
80714845Svikram 	int		circ2;
80724845Svikram 	int		constraint;
80734845Svikram 	char		*devnm;
80744845Svikram 
80754845Svikram 	/*
80764845Svikram 	 * First, lookup the device
80774845Svikram 	 */
80784845Svikram 	dip = e_ddi_hold_devi_by_path(path, 0);
80794845Svikram 	if (dip == NULL) {
80804845Svikram 		/*
80814845Svikram 		 * device does not exist. This device cannot be
80824845Svikram 		 * a critical device since it is not in use. Thus
80834845Svikram 		 * this device is always retireable. Return DDI_SUCCESS
80844845Svikram 		 * to indicate this. If this device is ever
80854845Svikram 		 * instantiated, I/O framework will consult the
80864845Svikram 		 * the persistent retire store, mark it as
80874845Svikram 		 * retired and fence it off.
80884845Svikram 		 */
80894845Svikram 		RIO_DEBUG((CE_NOTE, "Retire device: device doesn't exist."
80904845Svikram 		    " NOP. Just returning SUCCESS. path=%s", path));
80914845Svikram 		free_array(cons_array);
80924845Svikram 		return (DDI_SUCCESS);
80934845Svikram 	}
80944845Svikram 
80954845Svikram 	RIO_DEBUG((CE_NOTE, "Retire device: found dip = %p.", (void *)dip));
80964845Svikram 
80974845Svikram 	pdip = ddi_get_parent(dip);
80984845Svikram 	ndi_hold_devi(pdip);
80994845Svikram 
81004845Svikram 	/*
81014845Svikram 	 * Run devfs_clean() in case dip has no constraints and is
81024845Svikram 	 * not in use, so is retireable but there are dv_nodes holding
81034845Svikram 	 * ref-count on the dip. Note that devfs_clean() always returns
81044845Svikram 	 * success.
81054845Svikram 	 */
81064845Svikram 	devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
81074845Svikram 	(void) ddi_deviname(dip, devnm);
81084845Svikram 	(void) devfs_clean(pdip, devnm + 1, DV_CLEAN_FORCE);
81094845Svikram 	kmem_free(devnm, MAXNAMELEN + 1);
81104845Svikram 
81114845Svikram 	ndi_devi_enter(pdip, &circ);
81124845Svikram 
81134845Svikram 	/* release hold from e_ddi_hold_devi_by_path */
81144845Svikram 	ndi_rele_devi(dip);
81154845Svikram 
81164845Svikram 	/*
81174845Svikram 	 * If it cannot make a determination, is_leaf_node() assumes
81184845Svikram 	 * dip is a nexus.
81194845Svikram 	 */
81204845Svikram 	(void) e_ddi_mark_retiring(dip, cons_array);
81214845Svikram 	if (!is_leaf_node(dip)) {
81224845Svikram 		ndi_devi_enter(dip, &circ2);
81234845Svikram 		ddi_walk_devs(ddi_get_child(dip), e_ddi_mark_retiring,
81244845Svikram 		    cons_array);
81254845Svikram 		ndi_devi_exit(dip, circ2);
81264845Svikram 	}
81274845Svikram 	free_array(cons_array);
81284845Svikram 
81294845Svikram 	/*
81304845Svikram 	 * apply constraints
81314845Svikram 	 */
81324845Svikram 	RIO_DEBUG((CE_NOTE, "retire: subtree retire notify: path = %s", path));
81334845Svikram 
81344845Svikram 	constraint = 1;	/* assume constraints allow retire */
81354845Svikram 	(void) e_ddi_retire_notify(dip, &constraint);
81364845Svikram 	if (!is_leaf_node(dip)) {
81374845Svikram 		ndi_devi_enter(dip, &circ2);
81384845Svikram 		ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_notify,
81394845Svikram 		    &constraint);
81404845Svikram 		ndi_devi_exit(dip, circ2);
81414845Svikram 	}
81424845Svikram 
81434845Svikram 	/*
81444845Svikram 	 * Now finalize the retire
81454845Svikram 	 */
81464845Svikram 	(void) e_ddi_retire_finalize(dip, &constraint);
81474845Svikram 	if (!is_leaf_node(dip)) {
81484845Svikram 		ndi_devi_enter(dip, &circ2);
81494845Svikram 		ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_finalize,
81504845Svikram 		    &constraint);
81514845Svikram 		ndi_devi_exit(dip, circ2);
81524845Svikram 	}
81534845Svikram 
81544845Svikram 	if (!constraint) {
81554845Svikram 		RIO_DEBUG((CE_WARN, "retire failed: path = %s", path));
81564845Svikram 	} else {
81574845Svikram 		RIO_DEBUG((CE_NOTE, "retire succeeded: path = %s", path));
81584845Svikram 	}
81594845Svikram 
81604845Svikram 	ndi_devi_exit(pdip, circ);
81614845Svikram 	ndi_rele_devi(pdip);
81624845Svikram 	return (constraint ? DDI_SUCCESS : DDI_FAILURE);
81634845Svikram }
81644845Svikram 
81654845Svikram static int
81664845Svikram unmark_and_unfence(dev_info_t *dip, void *arg)
81674845Svikram {
81684845Svikram 	char	*path = (char *)arg;
81694845Svikram 
81704845Svikram 	ASSERT(path);
81714845Svikram 
81724845Svikram 	(void) ddi_pathname(dip, path);
81734845Svikram 
81744845Svikram 	mutex_enter(&DEVI(dip)->devi_lock);
81754845Svikram 	DEVI(dip)->devi_flags &= ~DEVI_RETIRED;
81764845Svikram 	DEVI_SET_DEVICE_ONLINE(dip);
81774845Svikram 	mutex_exit(&DEVI(dip)->devi_lock);
81784845Svikram 
81794845Svikram 	RIO_VERBOSE((CE_NOTE, "Cleared RETIRED flag: dip=%p, path=%s",
81804845Svikram 	    (void *)dip, path));
81814845Svikram 
81824845Svikram 	(void) spec_unfence_snode(dip);
81834845Svikram 	RIO_DEBUG((CE_NOTE, "Unfenced device: %s", path));
81844845Svikram 
81854845Svikram 	if (MDI_PHCI(dip))
81864845Svikram 		mdi_phci_unretire(dip);
81874845Svikram 
81884845Svikram 	return (DDI_WALK_CONTINUE);
81894845Svikram }
81904845Svikram 
81914845Svikram struct find_dip {
81924845Svikram 	char	*fd_buf;
81934845Svikram 	char	*fd_path;
81944845Svikram 	dev_info_t *fd_dip;
81954845Svikram };
81964845Svikram 
81974845Svikram static int
81984845Svikram find_dip_fcn(dev_info_t *dip, void *arg)
81994845Svikram {
82004845Svikram 	struct find_dip *findp = (struct find_dip *)arg;
82014845Svikram 
82024845Svikram 	(void) ddi_pathname(dip, findp->fd_buf);
82034845Svikram 
82044845Svikram 	if (strcmp(findp->fd_path, findp->fd_buf) != 0)
82054845Svikram 		return (DDI_WALK_CONTINUE);
82064845Svikram 
82074845Svikram 	ndi_hold_devi(dip);
82084845Svikram 	findp->fd_dip = dip;
82094845Svikram 
82104845Svikram 	return (DDI_WALK_TERMINATE);
82114845Svikram }
82124845Svikram 
82134845Svikram int
82144845Svikram e_ddi_unretire_device(char *path)
82154845Svikram {
82164845Svikram 	int		circ;
82177235Svikram 	int		circ2;
82184845Svikram 	char		*path2;
82194845Svikram 	dev_info_t	*pdip;
82204845Svikram 	dev_info_t	*dip;
82214845Svikram 	struct find_dip	 find_dip;
82224845Svikram 
82234845Svikram 	ASSERT(path);
82244845Svikram 	ASSERT(*path == '/');
82254845Svikram 
82264845Svikram 	if (strcmp(path, "/") == 0) {
82274845Svikram 		cmn_err(CE_WARN, "Root node cannot be retired. Skipping "
82284845Svikram 		    "device unretire: %s", path);
82294845Svikram 		return (0);
82304845Svikram 	}
82314845Svikram 
82324845Svikram 	/*
82334845Svikram 	 * We can't lookup the dip (corresponding to path) via
82344845Svikram 	 * e_ddi_hold_devi_by_path() because the dip may be offline
82354845Svikram 	 * and may not attach. Use ddi_walk_devs() instead;
82364845Svikram 	 */
82374845Svikram 	find_dip.fd_buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
82384845Svikram 	find_dip.fd_path = path;
82394845Svikram 	find_dip.fd_dip = NULL;
82404845Svikram 
82414845Svikram 	pdip = ddi_root_node();
82424845Svikram 
82434845Svikram 	ndi_devi_enter(pdip, &circ);
82444845Svikram 	ddi_walk_devs(ddi_get_child(pdip), find_dip_fcn, &find_dip);
82454845Svikram 	ndi_devi_exit(pdip, circ);
82464845Svikram 
82474845Svikram 	kmem_free(find_dip.fd_buf, MAXPATHLEN);
82484845Svikram 
82494845Svikram 	if (find_dip.fd_dip == NULL) {
82504845Svikram 		cmn_err(CE_WARN, "Device not found in device tree. Skipping "
82514845Svikram 		    "device unretire: %s", path);
82524845Svikram 		return (0);
82534845Svikram 	}
82544845Svikram 
82554845Svikram 	dip = find_dip.fd_dip;
82564845Svikram 
82574845Svikram 	pdip = ddi_get_parent(dip);
82584845Svikram 
82594845Svikram 	ndi_hold_devi(pdip);
82604845Svikram 
82614845Svikram 	ndi_devi_enter(pdip, &circ);
82624845Svikram 
82634845Svikram 	path2 = kmem_alloc(MAXPATHLEN, KM_SLEEP);
82644845Svikram 
82654845Svikram 	(void) unmark_and_unfence(dip, path2);
82664845Svikram 	if (!is_leaf_node(dip)) {
82677235Svikram 		ndi_devi_enter(dip, &circ2);
82684845Svikram 		ddi_walk_devs(ddi_get_child(dip), unmark_and_unfence, path2);
82697235Svikram 		ndi_devi_exit(dip, circ2);
82704845Svikram 	}
82714845Svikram 
82724845Svikram 	kmem_free(path2, MAXPATHLEN);
82734845Svikram 
82744845Svikram 	/* release hold from find_dip_fcn() */
82754845Svikram 	ndi_rele_devi(dip);
82764845Svikram 
82774845Svikram 	ndi_devi_exit(pdip, circ);
82784845Svikram 
82794845Svikram 	ndi_rele_devi(pdip);
82804845Svikram 
82814845Svikram 	return (0);
82824845Svikram }
82834845Svikram 
82844845Svikram /*
82854845Svikram  * Called before attach on a dip that has been retired.
82864845Svikram  */
82874845Svikram static int
82884845Svikram mark_and_fence(dev_info_t *dip, void *arg)
82894845Svikram {
82904845Svikram 	char    *fencepath = (char *)arg;
82914845Svikram 
82924845Svikram 	/*
82934845Svikram 	 * We have already decided to retire this device. The various
82944845Svikram 	 * constraint checking should not be set.
82954845Svikram 	 * NOTE that the retire flag may already be set due to
82964845Svikram 	 * fenced -> detach -> fenced transitions.
82974845Svikram 	 */
82984845Svikram 	mutex_enter(&DEVI(dip)->devi_lock);
82994845Svikram 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
83004845Svikram 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
83014845Svikram 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRING));
83024845Svikram 	DEVI(dip)->devi_flags |= DEVI_RETIRED;
83034845Svikram 	mutex_exit(&DEVI(dip)->devi_lock);
83044845Svikram 	RIO_VERBOSE((CE_NOTE, "marked as RETIRED dip=%p", (void *)dip));
83054845Svikram 
83064845Svikram 	if (fencepath) {
83074845Svikram 		(void) spec_fence_snode(dip, NULL);
83084845Svikram 		RIO_DEBUG((CE_NOTE, "Fenced: %s",
83094845Svikram 		    ddi_pathname(dip, fencepath)));
83104845Svikram 	}
83114845Svikram 
83124845Svikram 	return (DDI_WALK_CONTINUE);
83134845Svikram }
83144845Svikram 
83154845Svikram /*
83164845Svikram  * Checks the retire database and:
83174845Svikram  *
83184845Svikram  * - if device is present in the retire database, marks the device retired
83194845Svikram  *   and fences it off.
83204845Svikram  * - if device is not in retire database, allows the device to attach normally
83214845Svikram  *
83224845Svikram  * To be called only by framework attach code on first attach attempt.
83234845Svikram  *
83244845Svikram  */
83254845Svikram static void
83264845Svikram i_ddi_check_retire(dev_info_t *dip)
83274845Svikram {
83284845Svikram 	char		*path;
83294845Svikram 	dev_info_t	*pdip;
83304845Svikram 	int		circ;
83314845Svikram 	int		phci_only;
83324845Svikram 
83334845Svikram 	pdip = ddi_get_parent(dip);
83344845Svikram 
83354845Svikram 	/*
83364845Svikram 	 * Root dip is treated special and doesn't take this code path.
83374845Svikram 	 * Also root can never be retired.
83384845Svikram 	 */
83394845Svikram 	ASSERT(pdip);
83404845Svikram 	ASSERT(DEVI_BUSY_OWNED(pdip));
83414845Svikram 	ASSERT(i_ddi_node_state(dip) < DS_ATTACHED);
83424845Svikram 
83434845Svikram 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
83444845Svikram 
83454845Svikram 	(void) ddi_pathname(dip, path);
83464845Svikram 
83474845Svikram 	RIO_VERBOSE((CE_NOTE, "Checking if dip should attach: dip=%p, path=%s",
83484845Svikram 	    (void *)dip, path));
83494845Svikram 
83504845Svikram 	/*
83514845Svikram 	 * Check if this device is in the "retired" store i.e.  should
83524845Svikram 	 * be retired. If not, we have nothing to do.
83534845Svikram 	 */
83544845Svikram 	if (e_ddi_device_retired(path) == 0) {
83554845Svikram 		RIO_VERBOSE((CE_NOTE, "device is NOT retired: path=%s", path));
83564845Svikram 		kmem_free(path, MAXPATHLEN);
83574845Svikram 		return;
83584845Svikram 	}
83594845Svikram 
83604845Svikram 	RIO_DEBUG((CE_NOTE, "attach: device is retired: path=%s", path));
83614845Svikram 
83624845Svikram 	/*
83634845Svikram 	 * Mark dips and fence off snodes (if any)
83644845Svikram 	 */
83654845Svikram 	RIO_DEBUG((CE_NOTE, "attach: Mark and fence subtree: path=%s", path));
83664845Svikram 	(void) mark_and_fence(dip, path);
83674845Svikram 	if (!is_leaf_node(dip)) {
83684845Svikram 		ndi_devi_enter(dip, &circ);
83694845Svikram 		ddi_walk_devs(ddi_get_child(dip), mark_and_fence, path);
83704845Svikram 		ndi_devi_exit(dip, circ);
83714845Svikram 	}
83724845Svikram 
83734845Svikram 	kmem_free(path, MAXPATHLEN);
83744845Svikram 
83754845Svikram 	/*
83764845Svikram 	 * We don't want to check the client. We just want to
83774845Svikram 	 * offline the PHCI
83784845Svikram 	 */
83794845Svikram 	phci_only = 1;
83804845Svikram 	if (MDI_PHCI(dip))
83814845Svikram 		mdi_phci_retire_finalize(dip, phci_only);
83824845Svikram }
8383