xref: /onnv-gate/usr/src/uts/common/os/devcfg.c (revision 7613)
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 /*
225895Syz147064  * Copyright 2008 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>
420Sstevel@tonic-gate #include <sys/cpuvar.h>
430Sstevel@tonic-gate #include <sys/pathname.h>
440Sstevel@tonic-gate #include <sys/taskq.h>
450Sstevel@tonic-gate #include <sys/sysevent.h>
460Sstevel@tonic-gate #include <sys/sunmdi.h>
470Sstevel@tonic-gate #include <sys/stream.h>
480Sstevel@tonic-gate #include <sys/strsubr.h>
490Sstevel@tonic-gate #include <sys/fs/snode.h>
500Sstevel@tonic-gate #include <sys/fs/dv_node.h>
512621Sllai1 #include <sys/reboot.h>
524845Svikram #include <sys/sysmacros.h>
534845Svikram #include <sys/sunldi.h>
544845Svikram #include <sys/sunldi_impl.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #ifdef DEBUG
570Sstevel@tonic-gate int ddidebug = DDI_AUDIT;
580Sstevel@tonic-gate #else
590Sstevel@tonic-gate int ddidebug = 0;
600Sstevel@tonic-gate #endif
610Sstevel@tonic-gate 
620Sstevel@tonic-gate #define	MT_CONFIG_OP	0
630Sstevel@tonic-gate #define	MT_UNCONFIG_OP	1
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /* Multi-threaded configuration */
660Sstevel@tonic-gate struct mt_config_handle {
670Sstevel@tonic-gate 	kmutex_t mtc_lock;
680Sstevel@tonic-gate 	kcondvar_t mtc_cv;
690Sstevel@tonic-gate 	int mtc_thr_count;
700Sstevel@tonic-gate 	dev_info_t *mtc_pdip;	/* parent dip for mt_config_children */
710Sstevel@tonic-gate 	dev_info_t **mtc_fdip;	/* "a" dip where unconfigure failed */
720Sstevel@tonic-gate 	major_t mtc_parmajor;	/* parent major for mt_config_driver */
730Sstevel@tonic-gate 	major_t mtc_major;
740Sstevel@tonic-gate 	int mtc_flags;
750Sstevel@tonic-gate 	int mtc_op;		/* config or unconfig */
760Sstevel@tonic-gate 	int mtc_error;		/* operation error */
770Sstevel@tonic-gate 	struct brevq_node **mtc_brevqp;	/* outstanding branch events queue */
780Sstevel@tonic-gate #ifdef DEBUG
790Sstevel@tonic-gate 	int total_time;
800Sstevel@tonic-gate 	timestruc_t start_time;
810Sstevel@tonic-gate #endif /* DEBUG */
820Sstevel@tonic-gate };
830Sstevel@tonic-gate 
840Sstevel@tonic-gate struct devi_nodeid {
85789Sahrens 	pnode_t nodeid;
860Sstevel@tonic-gate 	dev_info_t *dip;
870Sstevel@tonic-gate 	struct devi_nodeid *next;
880Sstevel@tonic-gate };
890Sstevel@tonic-gate 
900Sstevel@tonic-gate struct devi_nodeid_list {
910Sstevel@tonic-gate 	kmutex_t dno_lock;		/* Protects other fields */
920Sstevel@tonic-gate 	struct devi_nodeid *dno_head;	/* list of devi nodeid elements */
930Sstevel@tonic-gate 	struct devi_nodeid *dno_free;	/* Free list */
940Sstevel@tonic-gate 	uint_t dno_list_length;		/* number of dips in list */
950Sstevel@tonic-gate };
960Sstevel@tonic-gate 
970Sstevel@tonic-gate /* used to keep track of branch remove events to be generated */
980Sstevel@tonic-gate struct brevq_node {
991317Scth 	char *brn_deviname;
1001317Scth 	struct brevq_node *brn_sibling;
1011317Scth 	struct brevq_node *brn_child;
1020Sstevel@tonic-gate };
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate static struct devi_nodeid_list devi_nodeid_list;
1050Sstevel@tonic-gate static struct devi_nodeid_list *devimap = &devi_nodeid_list;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate /*
1080Sstevel@tonic-gate  * Well known nodes which are attached first at boot time.
1090Sstevel@tonic-gate  */
1100Sstevel@tonic-gate dev_info_t *top_devinfo;		/* root of device tree */
1110Sstevel@tonic-gate dev_info_t *options_dip;
1120Sstevel@tonic-gate dev_info_t *pseudo_dip;
1130Sstevel@tonic-gate dev_info_t *clone_dip;
1140Sstevel@tonic-gate dev_info_t *scsi_vhci_dip;		/* MPXIO dip */
1150Sstevel@tonic-gate major_t clone_major;
1160Sstevel@tonic-gate 
1172621Sllai1 /*
1182621Sllai1  * A non-global zone's /dev is derived from the device tree.
1192621Sllai1  * This generation number serves to indicate when a zone's
1202621Sllai1  * /dev may need to be updated.
1212621Sllai1  */
1222621Sllai1 volatile ulong_t devtree_gen;		/* generation number */
1232621Sllai1 
1240Sstevel@tonic-gate /* block all future dev_info state changes */
1250Sstevel@tonic-gate static hrtime_t volatile devinfo_freeze = 0;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate /* number of dev_info attaches/detaches currently in progress */
1280Sstevel@tonic-gate static ulong_t devinfo_attach_detach = 0;
1290Sstevel@tonic-gate 
1307576SJerry.Gilliam@Sun.COM extern int	sys_shutdown;
1310Sstevel@tonic-gate extern kmutex_t global_vhci_lock;
1320Sstevel@tonic-gate 
1332621Sllai1 /* bitset of DS_SYSAVAIL & DS_RECONFIG - no races, no lock */
1342621Sllai1 static int devname_state = 0;
1352621Sllai1 
1360Sstevel@tonic-gate /*
1370Sstevel@tonic-gate  * The devinfo snapshot cache and related variables.
1380Sstevel@tonic-gate  * The only field in the di_cache structure that needs initialization
1390Sstevel@tonic-gate  * is the mutex (cache_lock). However, since this is an adaptive mutex
1400Sstevel@tonic-gate  * (MUTEX_DEFAULT) - it is automatically initialized by being allocated
1410Sstevel@tonic-gate  * in zeroed memory (static storage class). Therefore no explicit
1420Sstevel@tonic-gate  * initialization of the di_cache structure is needed.
1430Sstevel@tonic-gate  */
1440Sstevel@tonic-gate struct di_cache	di_cache = {1};
1450Sstevel@tonic-gate int		di_cache_debug = 0;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate /* For ddvis, which needs pseudo children under PCI */
1480Sstevel@tonic-gate int pci_allow_pseudo_children = 0;
1490Sstevel@tonic-gate 
1504145Scth /* Allow path-oriented alias driver binding on driver.conf enumerated nodes */
1514145Scth int driver_conf_allow_path_alias = 1;
1524145Scth 
1530Sstevel@tonic-gate /*
1540Sstevel@tonic-gate  * The following switch is for service people, in case a
1550Sstevel@tonic-gate  * 3rd party driver depends on identify(9e) being called.
1560Sstevel@tonic-gate  */
1570Sstevel@tonic-gate int identify_9e = 0;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate int mtc_off;					/* turn off mt config */
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate static kmem_cache_t *ddi_node_cache;		/* devinfo node cache */
1620Sstevel@tonic-gate static devinfo_log_header_t *devinfo_audit_log;	/* devinfo log */
1630Sstevel@tonic-gate static int devinfo_log_size;			/* size in pages */
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate static int lookup_compatible(dev_info_t *, uint_t);
1660Sstevel@tonic-gate static char *encode_composite_string(char **, uint_t, size_t *, uint_t);
1670Sstevel@tonic-gate static void link_to_driver_list(dev_info_t *);
1680Sstevel@tonic-gate static void unlink_from_driver_list(dev_info_t *);
1690Sstevel@tonic-gate static void add_to_dn_list(struct devnames *, dev_info_t *);
1700Sstevel@tonic-gate static void remove_from_dn_list(struct devnames *, dev_info_t *);
1710Sstevel@tonic-gate static dev_info_t *find_child_by_callback(dev_info_t *, char *, char *,
1720Sstevel@tonic-gate     int (*)(dev_info_t *, char *, int));
1730Sstevel@tonic-gate static dev_info_t *find_duplicate_child();
1740Sstevel@tonic-gate static void add_global_props(dev_info_t *);
1750Sstevel@tonic-gate static void remove_global_props(dev_info_t *);
1760Sstevel@tonic-gate static int uninit_node(dev_info_t *);
1770Sstevel@tonic-gate static void da_log_init(void);
1780Sstevel@tonic-gate static void da_log_enter(dev_info_t *);
1790Sstevel@tonic-gate static int walk_devs(dev_info_t *, int (*f)(dev_info_t *, void *), void *, int);
1800Sstevel@tonic-gate static int reset_nexus_flags(dev_info_t *, void *);
1810Sstevel@tonic-gate static void ddi_optimize_dtree(dev_info_t *);
1820Sstevel@tonic-gate static int is_leaf_node(dev_info_t *);
1830Sstevel@tonic-gate static struct mt_config_handle *mt_config_init(dev_info_t *, dev_info_t **,
1840Sstevel@tonic-gate     int, major_t, int, struct brevq_node **);
1850Sstevel@tonic-gate static void mt_config_children(struct mt_config_handle *);
1860Sstevel@tonic-gate static void mt_config_driver(struct mt_config_handle *);
1870Sstevel@tonic-gate static int mt_config_fini(struct mt_config_handle *);
1880Sstevel@tonic-gate static int devi_unconfig_common(dev_info_t *, dev_info_t **, int, major_t,
1890Sstevel@tonic-gate     struct brevq_node **);
1900Sstevel@tonic-gate static int
1910Sstevel@tonic-gate ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
1920Sstevel@tonic-gate     dev_info_t **childp, int flags);
1931093Shiremath static void i_link_vhci_node(dev_info_t *);
1942155Scth static void ndi_devi_exit_and_wait(dev_info_t *dip,
1952155Scth     int circular, clock_t end_time);
1964145Scth static int ndi_devi_unbind_driver(dev_info_t *dip);
1970Sstevel@tonic-gate 
1984845Svikram static void i_ddi_check_retire(dev_info_t *dip);
1994845Svikram 
2004845Svikram 
2014845Svikram 
2020Sstevel@tonic-gate /*
2030Sstevel@tonic-gate  * dev_info cache and node management
2040Sstevel@tonic-gate  */
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate /* initialize dev_info node cache */
2070Sstevel@tonic-gate void
2080Sstevel@tonic-gate i_ddi_node_cache_init()
2090Sstevel@tonic-gate {
2100Sstevel@tonic-gate 	ASSERT(ddi_node_cache == NULL);
2110Sstevel@tonic-gate 	ddi_node_cache = kmem_cache_create("dev_info_node_cache",
2120Sstevel@tonic-gate 	    sizeof (struct dev_info), 0, NULL, NULL, NULL, NULL, NULL, 0);
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	if (ddidebug & DDI_AUDIT)
2150Sstevel@tonic-gate 		da_log_init();
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate /*
2190Sstevel@tonic-gate  * Allocating a dev_info node, callable from interrupt context with KM_NOSLEEP
2200Sstevel@tonic-gate  * The allocated node has a reference count of 0.
2210Sstevel@tonic-gate  */
2220Sstevel@tonic-gate dev_info_t *
223789Sahrens i_ddi_alloc_node(dev_info_t *pdip, char *node_name, pnode_t nodeid,
2240Sstevel@tonic-gate     int instance, ddi_prop_t *sys_prop, int flag)
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate 	struct dev_info *devi;
2270Sstevel@tonic-gate 	struct devi_nodeid *elem;
2280Sstevel@tonic-gate 	static char failed[] = "i_ddi_alloc_node: out of memory";
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	ASSERT(node_name != NULL);
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	if ((devi = kmem_cache_alloc(ddi_node_cache, flag)) == NULL) {
2330Sstevel@tonic-gate 		cmn_err(CE_NOTE, failed);
2340Sstevel@tonic-gate 		return (NULL);
2350Sstevel@tonic-gate 	}
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	bzero(devi, sizeof (struct dev_info));
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	if (devinfo_audit_log) {
2400Sstevel@tonic-gate 		devi->devi_audit = kmem_zalloc(sizeof (devinfo_audit_t), flag);
2410Sstevel@tonic-gate 		if (devi->devi_audit == NULL)
2420Sstevel@tonic-gate 			goto fail;
2430Sstevel@tonic-gate 	}
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	if ((devi->devi_node_name = i_ddi_strdup(node_name, flag)) == NULL)
2460Sstevel@tonic-gate 		goto fail;
2474145Scth 
2480Sstevel@tonic-gate 	/* default binding name is node name */
2490Sstevel@tonic-gate 	devi->devi_binding_name = devi->devi_node_name;
2507009Scth 	devi->devi_major = DDI_MAJOR_T_NONE;	/* unbound by default */
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	/*
2530Sstevel@tonic-gate 	 * Make a copy of system property
2540Sstevel@tonic-gate 	 */
2550Sstevel@tonic-gate 	if (sys_prop &&
2560Sstevel@tonic-gate 	    (devi->devi_sys_prop_ptr = i_ddi_prop_list_dup(sys_prop, flag))
2570Sstevel@tonic-gate 	    == NULL)
2580Sstevel@tonic-gate 		goto fail;
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	/*
2610Sstevel@tonic-gate 	 * Assign devi_nodeid, devi_node_class, devi_node_attributes
2620Sstevel@tonic-gate 	 * according to the following algorithm:
2630Sstevel@tonic-gate 	 *
2640Sstevel@tonic-gate 	 * nodeid arg			node class		node attributes
2650Sstevel@tonic-gate 	 *
2660Sstevel@tonic-gate 	 * DEVI_PSEUDO_NODEID		DDI_NC_PSEUDO		A
2670Sstevel@tonic-gate 	 * DEVI_SID_NODEID		DDI_NC_PSEUDO		A,P
2680Sstevel@tonic-gate 	 * other			DDI_NC_PROM		P
2690Sstevel@tonic-gate 	 *
2700Sstevel@tonic-gate 	 * Where A = DDI_AUTO_ASSIGNED_NODEID (auto-assign a nodeid)
2710Sstevel@tonic-gate 	 * and	 P = DDI_PERSISTENT
2720Sstevel@tonic-gate 	 *
2730Sstevel@tonic-gate 	 * auto-assigned nodeids are also auto-freed.
2740Sstevel@tonic-gate 	 */
2750Sstevel@tonic-gate 	switch (nodeid) {
2760Sstevel@tonic-gate 	case DEVI_SID_NODEID:
2770Sstevel@tonic-gate 		devi->devi_node_attributes = DDI_PERSISTENT;
2780Sstevel@tonic-gate 		if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL)
2790Sstevel@tonic-gate 			goto fail;
2800Sstevel@tonic-gate 		/*FALLTHROUGH*/
2810Sstevel@tonic-gate 	case DEVI_PSEUDO_NODEID:
2820Sstevel@tonic-gate 		devi->devi_node_attributes |= DDI_AUTO_ASSIGNED_NODEID;
2830Sstevel@tonic-gate 		devi->devi_node_class = DDI_NC_PSEUDO;
2840Sstevel@tonic-gate 		if (impl_ddi_alloc_nodeid(&devi->devi_nodeid)) {
2850Sstevel@tonic-gate 			panic("i_ddi_alloc_node: out of nodeids");
2860Sstevel@tonic-gate 			/*NOTREACHED*/
2870Sstevel@tonic-gate 		}
2880Sstevel@tonic-gate 		break;
2890Sstevel@tonic-gate 	default:
2900Sstevel@tonic-gate 		if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL)
2910Sstevel@tonic-gate 			goto fail;
2920Sstevel@tonic-gate 		/*
2930Sstevel@tonic-gate 		 * the nodetype is 'prom', try to 'take' the nodeid now.
2940Sstevel@tonic-gate 		 * This requires memory allocation, so check for failure.
2950Sstevel@tonic-gate 		 */
2960Sstevel@tonic-gate 		if (impl_ddi_take_nodeid(nodeid, flag) != 0) {
2970Sstevel@tonic-gate 			kmem_free(elem, sizeof (*elem));
2980Sstevel@tonic-gate 			goto fail;
2990Sstevel@tonic-gate 		}
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 		devi->devi_nodeid = nodeid;
3020Sstevel@tonic-gate 		devi->devi_node_class = DDI_NC_PROM;
3030Sstevel@tonic-gate 		devi->devi_node_attributes = DDI_PERSISTENT;
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 	}
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node((dev_info_t *)devi)) {
3080Sstevel@tonic-gate 		mutex_enter(&devimap->dno_lock);
3090Sstevel@tonic-gate 		elem->next = devimap->dno_free;
3100Sstevel@tonic-gate 		devimap->dno_free = elem;
3110Sstevel@tonic-gate 		mutex_exit(&devimap->dno_lock);
3120Sstevel@tonic-gate 	}
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	/*
3150Sstevel@tonic-gate 	 * Instance is normally initialized to -1. In a few special
3160Sstevel@tonic-gate 	 * cases, the caller may specify an instance (e.g. CPU nodes).
3170Sstevel@tonic-gate 	 */
3180Sstevel@tonic-gate 	devi->devi_instance = instance;
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	/*
3210Sstevel@tonic-gate 	 * set parent and bus_ctl parent
3220Sstevel@tonic-gate 	 */
3230Sstevel@tonic-gate 	devi->devi_parent = DEVI(pdip);
3240Sstevel@tonic-gate 	devi->devi_bus_ctl = DEVI(pdip);
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
3270Sstevel@tonic-gate 	    "i_ddi_alloc_node: name=%s id=%d\n", node_name, devi->devi_nodeid));
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	cv_init(&(devi->devi_cv), NULL, CV_DEFAULT, NULL);
3300Sstevel@tonic-gate 	mutex_init(&(devi->devi_lock), NULL, MUTEX_DEFAULT, NULL);
3310Sstevel@tonic-gate 	mutex_init(&(devi->devi_pm_lock), NULL, MUTEX_DEFAULT, NULL);
3320Sstevel@tonic-gate 	mutex_init(&(devi->devi_pm_busy_lock), NULL, MUTEX_DEFAULT, NULL);
3330Sstevel@tonic-gate 
3344845Svikram 	RIO_TRACE((CE_NOTE, "i_ddi_alloc_node: Initing contract fields: "
3354845Svikram 	    "dip=%p, name=%s", (void *)devi, node_name));
3364845Svikram 
3374845Svikram 	mutex_init(&(devi->devi_ct_lock), NULL, MUTEX_DEFAULT, NULL);
3384845Svikram 	cv_init(&(devi->devi_ct_cv), NULL, CV_DEFAULT, NULL);
3394845Svikram 	devi->devi_ct_count = -1;	/* counter not in use if -1 */
3404845Svikram 	list_create(&(devi->devi_ct), sizeof (cont_device_t),
3414845Svikram 	    offsetof(cont_device_t, cond_next));
3424845Svikram 
3430Sstevel@tonic-gate 	i_ddi_set_node_state((dev_info_t *)devi, DS_PROTO);
3440Sstevel@tonic-gate 	da_log_enter((dev_info_t *)devi);
3450Sstevel@tonic-gate 	return ((dev_info_t *)devi);
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate fail:
3480Sstevel@tonic-gate 	if (devi->devi_sys_prop_ptr)
3490Sstevel@tonic-gate 		i_ddi_prop_list_delete(devi->devi_sys_prop_ptr);
3500Sstevel@tonic-gate 	if (devi->devi_node_name)
3510Sstevel@tonic-gate 		kmem_free(devi->devi_node_name, strlen(node_name) + 1);
3520Sstevel@tonic-gate 	if (devi->devi_audit)
3530Sstevel@tonic-gate 		kmem_free(devi->devi_audit, sizeof (devinfo_audit_t));
3540Sstevel@tonic-gate 	kmem_cache_free(ddi_node_cache, devi);
3550Sstevel@tonic-gate 	cmn_err(CE_NOTE, failed);
3560Sstevel@tonic-gate 	return (NULL);
3570Sstevel@tonic-gate }
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate /*
3600Sstevel@tonic-gate  * free a dev_info structure.
3610Sstevel@tonic-gate  * NB. Not callable from interrupt since impl_ddi_free_nodeid may block.
3620Sstevel@tonic-gate  */
3630Sstevel@tonic-gate void
3640Sstevel@tonic-gate i_ddi_free_node(dev_info_t *dip)
3650Sstevel@tonic-gate {
3660Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
3670Sstevel@tonic-gate 	struct devi_nodeid *elem;
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	ASSERT(devi->devi_ref == 0);
3700Sstevel@tonic-gate 	ASSERT(devi->devi_addr == NULL);
3710Sstevel@tonic-gate 	ASSERT(devi->devi_node_state == DS_PROTO);
3720Sstevel@tonic-gate 	ASSERT(devi->devi_child == NULL);
3730Sstevel@tonic-gate 
374439Scth 	/* free devi_addr_buf allocated by ddi_set_name_addr() */
375439Scth 	if (devi->devi_addr_buf)
376439Scth 		kmem_free(devi->devi_addr_buf, 2 * MAXNAMELEN);
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	if (i_ndi_dev_is_auto_assigned_node(dip))
3790Sstevel@tonic-gate 		impl_ddi_free_nodeid(DEVI(dip)->devi_nodeid);
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
3820Sstevel@tonic-gate 		mutex_enter(&devimap->dno_lock);
3830Sstevel@tonic-gate 		ASSERT(devimap->dno_free);
3840Sstevel@tonic-gate 		elem = devimap->dno_free;
3850Sstevel@tonic-gate 		devimap->dno_free = elem->next;
3860Sstevel@tonic-gate 		mutex_exit(&devimap->dno_lock);
3870Sstevel@tonic-gate 		kmem_free(elem, sizeof (*elem));
3880Sstevel@tonic-gate 	}
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	if (DEVI(dip)->devi_compat_names)
3910Sstevel@tonic-gate 		kmem_free(DEVI(dip)->devi_compat_names,
3920Sstevel@tonic-gate 		    DEVI(dip)->devi_compat_length);
3934145Scth 	if (DEVI(dip)->devi_rebinding_name)
3944145Scth 		kmem_free(DEVI(dip)->devi_rebinding_name,
3954145Scth 		    strlen(DEVI(dip)->devi_rebinding_name) + 1);
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	ddi_prop_remove_all(dip);	/* remove driver properties */
3980Sstevel@tonic-gate 	if (devi->devi_sys_prop_ptr)
3990Sstevel@tonic-gate 		i_ddi_prop_list_delete(devi->devi_sys_prop_ptr);
4000Sstevel@tonic-gate 	if (devi->devi_hw_prop_ptr)
4010Sstevel@tonic-gate 		i_ddi_prop_list_delete(devi->devi_hw_prop_ptr);
4020Sstevel@tonic-gate 
4036640Scth 	if (DEVI(dip)->devi_devid_str)
4046640Scth 		ddi_devid_str_free(DEVI(dip)->devi_devid_str);
4056640Scth 
4060Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_INVAL);
4070Sstevel@tonic-gate 	da_log_enter(dip);
4080Sstevel@tonic-gate 	if (devi->devi_audit) {
4090Sstevel@tonic-gate 		kmem_free(devi->devi_audit, sizeof (devinfo_audit_t));
4100Sstevel@tonic-gate 	}
4110Sstevel@tonic-gate 	if (devi->devi_device_class)
4120Sstevel@tonic-gate 		kmem_free(devi->devi_device_class,
4130Sstevel@tonic-gate 		    strlen(devi->devi_device_class) + 1);
4140Sstevel@tonic-gate 	cv_destroy(&(devi->devi_cv));
4150Sstevel@tonic-gate 	mutex_destroy(&(devi->devi_lock));
4160Sstevel@tonic-gate 	mutex_destroy(&(devi->devi_pm_lock));
4170Sstevel@tonic-gate 	mutex_destroy(&(devi->devi_pm_busy_lock));
4180Sstevel@tonic-gate 
4194845Svikram 	RIO_TRACE((CE_NOTE, "i_ddi_free_node: destroying contract fields: "
4204845Svikram 	    "dip=%p", (void *)dip));
4214845Svikram 	contract_device_remove_dip(dip);
4224845Svikram 	ASSERT(devi->devi_ct_count == -1);
4234845Svikram 	ASSERT(list_is_empty(&(devi->devi_ct)));
4244845Svikram 	cv_destroy(&(devi->devi_ct_cv));
4254845Svikram 	list_destroy(&(devi->devi_ct));
4264845Svikram 	/* free this last since contract_device_remove_dip() uses it */
4274845Svikram 	mutex_destroy(&(devi->devi_ct_lock));
4284845Svikram 	RIO_TRACE((CE_NOTE, "i_ddi_free_node: destroyed all contract fields: "
4294845Svikram 	    "dip=%p, name=%s", (void *)dip, devi->devi_node_name));
4304845Svikram 
4314845Svikram 	kmem_free(devi->devi_node_name, strlen(devi->devi_node_name) + 1);
4324845Svikram 
4330Sstevel@tonic-gate 	kmem_cache_free(ddi_node_cache, devi);
4340Sstevel@tonic-gate }
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate /*
4380Sstevel@tonic-gate  * Node state transitions
4390Sstevel@tonic-gate  */
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate /*
4420Sstevel@tonic-gate  * Change the node name
4430Sstevel@tonic-gate  */
4440Sstevel@tonic-gate int
4450Sstevel@tonic-gate ndi_devi_set_nodename(dev_info_t *dip, char *name, int flags)
4460Sstevel@tonic-gate {
4470Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags))
4480Sstevel@tonic-gate 	char *nname, *oname;
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	ASSERT(dip && name);
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	oname = DEVI(dip)->devi_node_name;
4530Sstevel@tonic-gate 	if (strcmp(oname, name) == 0)
4540Sstevel@tonic-gate 		return (DDI_SUCCESS);
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 	/*
4570Sstevel@tonic-gate 	 * pcicfg_fix_ethernet requires a name change after node
4580Sstevel@tonic-gate 	 * is linked into the tree. When pcicfg is fixed, we
4590Sstevel@tonic-gate 	 * should only allow name change in DS_PROTO state.
4600Sstevel@tonic-gate 	 */
4610Sstevel@tonic-gate 	if (i_ddi_node_state(dip) >= DS_BOUND) {
4620Sstevel@tonic-gate 		/*
4630Sstevel@tonic-gate 		 * Don't allow name change once node is bound
4640Sstevel@tonic-gate 		 */
4650Sstevel@tonic-gate 		cmn_err(CE_NOTE,
4660Sstevel@tonic-gate 		    "ndi_devi_set_nodename: node already bound dip = %p,"
4670Sstevel@tonic-gate 		    " %s -> %s", (void *)dip, ddi_node_name(dip), name);
4680Sstevel@tonic-gate 		return (NDI_FAILURE);
4690Sstevel@tonic-gate 	}
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	nname = i_ddi_strdup(name, KM_SLEEP);
4720Sstevel@tonic-gate 	DEVI(dip)->devi_node_name = nname;
4730Sstevel@tonic-gate 	i_ddi_set_binding_name(dip, nname);
4740Sstevel@tonic-gate 	kmem_free(oname, strlen(oname) + 1);
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	da_log_enter(dip);
4770Sstevel@tonic-gate 	return (NDI_SUCCESS);
4780Sstevel@tonic-gate }
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate void
4810Sstevel@tonic-gate i_ddi_add_devimap(dev_info_t *dip)
4820Sstevel@tonic-gate {
4830Sstevel@tonic-gate 	struct devi_nodeid *elem;
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	ASSERT(dip);
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	if (!ndi_dev_is_persistent_node(dip))
4880Sstevel@tonic-gate 		return;
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	ASSERT(ddi_get_parent(dip) == NULL || (DEVI_VHCI_NODE(dip)) ||
4910Sstevel@tonic-gate 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	mutex_enter(&devimap->dno_lock);
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	ASSERT(devimap->dno_free);
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	elem = devimap->dno_free;
4980Sstevel@tonic-gate 	devimap->dno_free = elem->next;
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	elem->nodeid = ddi_get_nodeid(dip);
5010Sstevel@tonic-gate 	elem->dip = dip;
5020Sstevel@tonic-gate 	elem->next = devimap->dno_head;
5030Sstevel@tonic-gate 	devimap->dno_head = elem;
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	devimap->dno_list_length++;
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	mutex_exit(&devimap->dno_lock);
5080Sstevel@tonic-gate }
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate static int
5110Sstevel@tonic-gate i_ddi_remove_devimap(dev_info_t *dip)
5120Sstevel@tonic-gate {
5130Sstevel@tonic-gate 	struct devi_nodeid *prev, *elem;
5140Sstevel@tonic-gate 	static const char *fcn = "i_ddi_remove_devimap";
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	ASSERT(dip);
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 	if (!ndi_dev_is_persistent_node(dip))
5190Sstevel@tonic-gate 		return (DDI_SUCCESS);
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	mutex_enter(&devimap->dno_lock);
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	/*
5240Sstevel@tonic-gate 	 * The following check is done with dno_lock held
5250Sstevel@tonic-gate 	 * to prevent race between dip removal and
5260Sstevel@tonic-gate 	 * e_ddi_prom_node_to_dip()
5270Sstevel@tonic-gate 	 */
5280Sstevel@tonic-gate 	if (e_ddi_devi_holdcnt(dip)) {
5290Sstevel@tonic-gate 		mutex_exit(&devimap->dno_lock);
5300Sstevel@tonic-gate 		return (DDI_FAILURE);
5310Sstevel@tonic-gate 	}
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	ASSERT(devimap->dno_head);
5340Sstevel@tonic-gate 	ASSERT(devimap->dno_list_length > 0);
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	prev = NULL;
5370Sstevel@tonic-gate 	for (elem = devimap->dno_head; elem; elem = elem->next) {
5380Sstevel@tonic-gate 		if (elem->dip == dip) {
5390Sstevel@tonic-gate 			ASSERT(elem->nodeid == ddi_get_nodeid(dip));
5400Sstevel@tonic-gate 			break;
5410Sstevel@tonic-gate 		}
5420Sstevel@tonic-gate 		prev = elem;
5430Sstevel@tonic-gate 	}
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	if (elem && prev)
5460Sstevel@tonic-gate 		prev->next = elem->next;
5470Sstevel@tonic-gate 	else if (elem)
5480Sstevel@tonic-gate 		devimap->dno_head = elem->next;
5490Sstevel@tonic-gate 	else
5500Sstevel@tonic-gate 		panic("%s: devinfo node(%p) not found",
5510Sstevel@tonic-gate 		    fcn, (void *)dip);
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	devimap->dno_list_length--;
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	elem->nodeid = 0;
5560Sstevel@tonic-gate 	elem->dip = NULL;
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	elem->next = devimap->dno_free;
5590Sstevel@tonic-gate 	devimap->dno_free = elem;
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	mutex_exit(&devimap->dno_lock);
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	return (DDI_SUCCESS);
5640Sstevel@tonic-gate }
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate /*
5670Sstevel@tonic-gate  * Link this node into the devinfo tree and add to orphan list
5680Sstevel@tonic-gate  * Not callable from interrupt context
5690Sstevel@tonic-gate  */
5700Sstevel@tonic-gate static void
5710Sstevel@tonic-gate link_node(dev_info_t *dip)
5720Sstevel@tonic-gate {
5730Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
5740Sstevel@tonic-gate 	struct dev_info *parent = devi->devi_parent;
5750Sstevel@tonic-gate 	dev_info_t **dipp;
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	ASSERT(parent);	/* never called for root node */
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "link_node: parent = %s child = %s\n",
5800Sstevel@tonic-gate 	    parent->devi_node_name, devi->devi_node_name));
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	/*
5830Sstevel@tonic-gate 	 * Hold the global_vhci_lock before linking any direct
5840Sstevel@tonic-gate 	 * children of rootnex driver. This special lock protects
5850Sstevel@tonic-gate 	 * linking and unlinking for rootnext direct children.
5860Sstevel@tonic-gate 	 */
5870Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
5880Sstevel@tonic-gate 		mutex_enter(&global_vhci_lock);
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	/*
5910Sstevel@tonic-gate 	 * attach the node to end of the list unless the node is already there
5920Sstevel@tonic-gate 	 */
5930Sstevel@tonic-gate 	dipp = (dev_info_t **)(&DEVI(parent)->devi_child);
5940Sstevel@tonic-gate 	while (*dipp && (*dipp != dip)) {
5950Sstevel@tonic-gate 		dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling);
5960Sstevel@tonic-gate 	}
5970Sstevel@tonic-gate 	ASSERT(*dipp == NULL);	/* node is not linked */
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 	/*
6000Sstevel@tonic-gate 	 * Now that we are in the tree, update the devi-nodeid map.
6010Sstevel@tonic-gate 	 */
6020Sstevel@tonic-gate 	i_ddi_add_devimap(dip);
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	/*
6050Sstevel@tonic-gate 	 * This is a temporary workaround for Bug 4618861.
6060Sstevel@tonic-gate 	 * We keep the scsi_vhci nexus node on the left side of the devinfo
6070Sstevel@tonic-gate 	 * tree (under the root nexus driver), so that virtual nodes under
6080Sstevel@tonic-gate 	 * scsi_vhci will be SUSPENDed first and RESUMEd last.  This ensures
6090Sstevel@tonic-gate 	 * that the pHCI nodes are active during times when their clients
6100Sstevel@tonic-gate 	 * may be depending on them.  This workaround embodies the knowledge
6110Sstevel@tonic-gate 	 * that system PM and CPR both traverse the tree left-to-right during
6120Sstevel@tonic-gate 	 * SUSPEND and right-to-left during RESUME.
6131093Shiremath 	 * Extending the workaround to IB Nexus/VHCI
6141093Shiremath 	 * driver also.
6150Sstevel@tonic-gate 	 */
6164145Scth 	if (strcmp(devi->devi_binding_name, "scsi_vhci") == 0) {
6170Sstevel@tonic-gate 		/* Add scsi_vhci to beginning of list */
6180Sstevel@tonic-gate 		ASSERT((dev_info_t *)parent == top_devinfo);
6190Sstevel@tonic-gate 		/* scsi_vhci under rootnex */
6200Sstevel@tonic-gate 		devi->devi_sibling = parent->devi_child;
6210Sstevel@tonic-gate 		parent->devi_child = devi;
6224145Scth 	} else if (strcmp(devi->devi_binding_name, "ib") == 0) {
6231093Shiremath 		i_link_vhci_node(dip);
6240Sstevel@tonic-gate 	} else {
6250Sstevel@tonic-gate 		/* Add to end of list */
6260Sstevel@tonic-gate 		*dipp = dip;
6270Sstevel@tonic-gate 		DEVI(dip)->devi_sibling = NULL;
6280Sstevel@tonic-gate 	}
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 	/*
6310Sstevel@tonic-gate 	 * Release the global_vhci_lock before linking any direct
6320Sstevel@tonic-gate 	 * children of rootnex driver.
6330Sstevel@tonic-gate 	 */
6340Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
6350Sstevel@tonic-gate 		mutex_exit(&global_vhci_lock);
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	/* persistent nodes go on orphan list */
6380Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip))
6390Sstevel@tonic-gate 		add_to_dn_list(&orphanlist, dip);
6400Sstevel@tonic-gate }
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate /*
6430Sstevel@tonic-gate  * Unlink this node from the devinfo tree
6440Sstevel@tonic-gate  */
6450Sstevel@tonic-gate static int
6460Sstevel@tonic-gate unlink_node(dev_info_t *dip)
6470Sstevel@tonic-gate {
6480Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
6490Sstevel@tonic-gate 	struct dev_info *parent = devi->devi_parent;
6500Sstevel@tonic-gate 	dev_info_t **dipp;
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 	ASSERT(parent != NULL);
6530Sstevel@tonic-gate 	ASSERT(devi->devi_node_state == DS_LINKED);
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "unlink_node: name = %s\n",
6560Sstevel@tonic-gate 	    ddi_node_name(dip)));
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	/* check references */
6590Sstevel@tonic-gate 	if (devi->devi_ref || i_ddi_remove_devimap(dip) != DDI_SUCCESS)
6600Sstevel@tonic-gate 		return (DDI_FAILURE);
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 	/*
6630Sstevel@tonic-gate 	 * Hold the global_vhci_lock before linking any direct
6640Sstevel@tonic-gate 	 * children of rootnex driver.
6650Sstevel@tonic-gate 	 */
6660Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
6670Sstevel@tonic-gate 		mutex_enter(&global_vhci_lock);
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	dipp = (dev_info_t **)(&DEVI(parent)->devi_child);
6700Sstevel@tonic-gate 	while (*dipp && (*dipp != dip)) {
6710Sstevel@tonic-gate 		dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling);
6720Sstevel@tonic-gate 	}
6730Sstevel@tonic-gate 	if (*dipp) {
6740Sstevel@tonic-gate 		*dipp = (dev_info_t *)(devi->devi_sibling);
6750Sstevel@tonic-gate 		devi->devi_sibling = NULL;
6760Sstevel@tonic-gate 	} else {
6770Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE, "unlink_node: %s not linked",
6780Sstevel@tonic-gate 		    devi->devi_node_name));
6790Sstevel@tonic-gate 	}
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	/*
6820Sstevel@tonic-gate 	 * Release the global_vhci_lock before linking any direct
6830Sstevel@tonic-gate 	 * children of rootnex driver.
6840Sstevel@tonic-gate 	 */
6850Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
6860Sstevel@tonic-gate 		mutex_exit(&global_vhci_lock);
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	/* Remove node from orphan list */
6890Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
6900Sstevel@tonic-gate 		remove_from_dn_list(&orphanlist, dip);
6910Sstevel@tonic-gate 	}
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	return (DDI_SUCCESS);
6940Sstevel@tonic-gate }
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate /*
6970Sstevel@tonic-gate  * Bind this devinfo node to a driver. If compat is NON-NULL, try that first.
6980Sstevel@tonic-gate  * Else, use the node-name.
6990Sstevel@tonic-gate  *
7000Sstevel@tonic-gate  * NOTE: IEEE1275 specifies that nodename should be tried before compatible.
7010Sstevel@tonic-gate  *	Solaris implementation binds nodename after compatible.
7020Sstevel@tonic-gate  *
7030Sstevel@tonic-gate  * If we find a binding,
7040Sstevel@tonic-gate  * - set the binding name to the the string,
7050Sstevel@tonic-gate  * - set major number to driver major
7060Sstevel@tonic-gate  *
7070Sstevel@tonic-gate  * If we don't find a binding,
7080Sstevel@tonic-gate  * - return failure
7090Sstevel@tonic-gate  */
7100Sstevel@tonic-gate static int
7110Sstevel@tonic-gate bind_node(dev_info_t *dip)
7120Sstevel@tonic-gate {
7130Sstevel@tonic-gate 	char *p = NULL;
7147009Scth 	major_t major = DDI_MAJOR_T_NONE;
7150Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
7160Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	ASSERT(devi->devi_node_state == DS_LINKED);
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "bind_node: 0x%p(name = %s)\n",
7210Sstevel@tonic-gate 	    (void *)dip, ddi_node_name(dip)));
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
7240Sstevel@tonic-gate 	if (DEVI(dip)->devi_flags & DEVI_NO_BIND) {
7250Sstevel@tonic-gate 		mutex_exit(&DEVI(dip)->devi_lock);
7260Sstevel@tonic-gate 		return (DDI_FAILURE);
7270Sstevel@tonic-gate 	}
7280Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 	/* find the driver with most specific binding using compatible */
7310Sstevel@tonic-gate 	major = ddi_compatible_driver_major(dip, &p);
7327009Scth 	if (major == DDI_MAJOR_T_NONE)
7330Sstevel@tonic-gate 		return (DDI_FAILURE);
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 	devi->devi_major = major;
7360Sstevel@tonic-gate 	if (p != NULL) {
7370Sstevel@tonic-gate 		i_ddi_set_binding_name(dip, p);
7380Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "bind_node: %s bound to %s\n",
7390Sstevel@tonic-gate 		    devi->devi_node_name, p));
7400Sstevel@tonic-gate 	}
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	/* Link node to per-driver list */
7430Sstevel@tonic-gate 	link_to_driver_list(dip);
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 	/*
7460Sstevel@tonic-gate 	 * reset parent flag so that nexus will merge .conf props
7470Sstevel@tonic-gate 	 */
7480Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
7490Sstevel@tonic-gate 		mutex_enter(&DEVI(parent)->devi_lock);
7500Sstevel@tonic-gate 		DEVI(parent)->devi_flags &=
7510Sstevel@tonic-gate 		    ~(DEVI_ATTACHED_CHILDREN|DEVI_MADE_CHILDREN);
7520Sstevel@tonic-gate 		mutex_exit(&DEVI(parent)->devi_lock);
7530Sstevel@tonic-gate 	}
7540Sstevel@tonic-gate 	return (DDI_SUCCESS);
7550Sstevel@tonic-gate }
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate /*
7580Sstevel@tonic-gate  * Unbind this devinfo node
7590Sstevel@tonic-gate  * Called before the node is destroyed or driver is removed from system
7600Sstevel@tonic-gate  */
7610Sstevel@tonic-gate static int
7620Sstevel@tonic-gate unbind_node(dev_info_t *dip)
7630Sstevel@tonic-gate {
7640Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_node_state == DS_BOUND);
7657009Scth 	ASSERT(DEVI(dip)->devi_major != DDI_MAJOR_T_NONE);
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	/* check references */
7680Sstevel@tonic-gate 	if (DEVI(dip)->devi_ref)
7690Sstevel@tonic-gate 		return (DDI_FAILURE);
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "unbind_node: 0x%p(name = %s)\n",
7720Sstevel@tonic-gate 	    (void *)dip, ddi_node_name(dip)));
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 	unlink_from_driver_list(dip);
7754145Scth 
7767009Scth 	DEVI(dip)->devi_major = DDI_MAJOR_T_NONE;
7774145Scth 	DEVI(dip)->devi_binding_name = DEVI(dip)->devi_node_name;
7780Sstevel@tonic-gate 	return (DDI_SUCCESS);
7790Sstevel@tonic-gate }
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate /*
7820Sstevel@tonic-gate  * Initialize a node: calls the parent nexus' bus_ctl ops to do the operation.
7830Sstevel@tonic-gate  * Must hold parent and per-driver list while calling this function.
7840Sstevel@tonic-gate  * A successful init_node() returns with an active ndi_hold_devi() hold on
7850Sstevel@tonic-gate  * the parent.
7860Sstevel@tonic-gate  */
7870Sstevel@tonic-gate static int
7880Sstevel@tonic-gate init_node(dev_info_t *dip)
7890Sstevel@tonic-gate {
7900Sstevel@tonic-gate 	int error;
7910Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
7920Sstevel@tonic-gate 	int (*f)(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *, void *);
7930Sstevel@tonic-gate 	char *path;
7944145Scth 	major_t	major;
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_BOUND);
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	/* should be DS_READY except for pcmcia ... */
7990Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(pdip) >= DS_PROBED);
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8020Sstevel@tonic-gate 	(void) ddi_pathname(dip, path);
8030Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "init_node: entry: path %s 0x%p\n",
8040Sstevel@tonic-gate 	    path, (void *)dip));
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	/*
8070Sstevel@tonic-gate 	 * The parent must have a bus_ctl operation.
8080Sstevel@tonic-gate 	 */
8090Sstevel@tonic-gate 	if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
8100Sstevel@tonic-gate 	    (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_ctl) == NULL) {
8110Sstevel@tonic-gate 		error = DDI_FAILURE;
8120Sstevel@tonic-gate 		goto out;
8130Sstevel@tonic-gate 	}
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	add_global_props(dip);
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	/*
8180Sstevel@tonic-gate 	 * Invoke the parent's bus_ctl operation with the DDI_CTLOPS_INITCHILD
8190Sstevel@tonic-gate 	 * command to transform the child to canonical form 1. If there
8200Sstevel@tonic-gate 	 * is an error, ddi_remove_child should be called, to clean up.
8210Sstevel@tonic-gate 	 */
8220Sstevel@tonic-gate 	error = (*f)(pdip, pdip, DDI_CTLOPS_INITCHILD, dip, NULL);
8230Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
8240Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "init_node: %s 0x%p failed\n",
8250Sstevel@tonic-gate 		    path, (void *)dip));
8260Sstevel@tonic-gate 		remove_global_props(dip);
8270Sstevel@tonic-gate 		/* in case nexus driver didn't clear this field */
8280Sstevel@tonic-gate 		ddi_set_name_addr(dip, NULL);
8290Sstevel@tonic-gate 		error = DDI_FAILURE;
8300Sstevel@tonic-gate 		goto out;
8310Sstevel@tonic-gate 	}
8320Sstevel@tonic-gate 
8334950Scth 	ndi_hold_devi(pdip);			/* initial hold of parent */
8340Sstevel@tonic-gate 
8354145Scth 	/* recompute path after initchild for @addr information */
8364145Scth 	(void) ddi_pathname(dip, path);
8374145Scth 
8384145Scth 	/* Check for duplicate nodes */
8390Sstevel@tonic-gate 	if (find_duplicate_child(pdip, dip) != NULL) {
8400Sstevel@tonic-gate 		/*
8410Sstevel@tonic-gate 		 * uninit_node() the duplicate - a successful uninit_node()
8424950Scth 		 * will release inital hold of parent using ndi_rele_devi().
8430Sstevel@tonic-gate 		 */
8440Sstevel@tonic-gate 		if ((error = uninit_node(dip)) != DDI_SUCCESS) {
8454950Scth 			ndi_rele_devi(pdip);	/* release initial hold */
8460Sstevel@tonic-gate 			cmn_err(CE_WARN, "init_node: uninit of duplicate "
8470Sstevel@tonic-gate 			    "node %s failed", path);
8480Sstevel@tonic-gate 		}
8490Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "init_node: duplicate uninit "
8500Sstevel@tonic-gate 		    "%s 0x%p%s\n", path, (void *)dip,
8510Sstevel@tonic-gate 		    (error == DDI_SUCCESS) ? "" : " failed"));
8520Sstevel@tonic-gate 		error = DDI_FAILURE;
8530Sstevel@tonic-gate 		goto out;
8540Sstevel@tonic-gate 	}
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 	/*
8574145Scth 	 * Check to see if we have a path-oriented driver alias that overrides
8584145Scth 	 * the current driver binding. If so, we need to rebind. This check
8594145Scth 	 * needs to be delayed until after a successful DDI_CTLOPS_INITCHILD,
8604145Scth 	 * so the unit-address is established on the last component of the path.
8614145Scth 	 *
8624145Scth 	 * NOTE: Allowing a path-oriented alias to change the driver binding
8634145Scth 	 * of a driver.conf node results in non-intuitive property behavior.
8644145Scth 	 * We provide a tunable (driver_conf_allow_path_alias) to control
8654145Scth 	 * this behavior. See uninit_node() for more details.
8664145Scth 	 *
8674145Scth 	 * NOTE: If you are adding a path-oriented alias for the boot device,
8684145Scth 	 * and there is mismatch between OBP and the kernel in regard to
8694145Scth 	 * generic name use, like "disk" .vs. "ssd", then you will need
8704145Scth 	 * to add a path-oriented alias for both paths.
8714145Scth 	 */
8724145Scth 	major = ddi_name_to_major(path);
8737009Scth 	if ((major != DDI_MAJOR_T_NONE) &&
8744145Scth 	    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED) &&
8754145Scth 	    (major != DEVI(dip)->devi_major) &&
8764145Scth 	    (ndi_dev_is_persistent_node(dip) || driver_conf_allow_path_alias)) {
8774145Scth 
8784145Scth 		/* Mark node for rebind processing. */
8794145Scth 		mutex_enter(&DEVI(dip)->devi_lock);
8804145Scth 		DEVI(dip)->devi_flags |= DEVI_REBIND;
8814145Scth 		mutex_exit(&DEVI(dip)->devi_lock);
8824145Scth 
8834145Scth 		/*
8844950Scth 		 * Add an extra hold on the parent to prevent it from ever
8854950Scth 		 * having a zero devi_ref during the child rebind process.
8864950Scth 		 * This is necessary to ensure that the parent will never
8874950Scth 		 * detach(9E) during the rebind.
8884950Scth 		 */
8894950Scth 		ndi_hold_devi(pdip);		/* extra hold of parent */
8904950Scth 
8914950Scth 		/*
8924145Scth 		 * uninit_node() current binding - a successful uninit_node()
8934950Scth 		 * will release extra hold of parent using ndi_rele_devi().
8944145Scth 		 */
8954145Scth 		if ((error = uninit_node(dip)) != DDI_SUCCESS) {
8964950Scth 			ndi_rele_devi(pdip);	/* release extra hold */
8974950Scth 			ndi_rele_devi(pdip);	/* release initial hold */
8984145Scth 			cmn_err(CE_WARN, "init_node: uninit for rebind "
8994145Scth 			    "of node %s failed", path);
9004145Scth 			goto out;
9014145Scth 		}
9024145Scth 
9034145Scth 		/* Unbind: demote the node back to DS_LINKED.  */
9044145Scth 		if ((error = ndi_devi_unbind_driver(dip)) != DDI_SUCCESS) {
9054950Scth 			ndi_rele_devi(pdip);	/* relrease initial hold */
9064145Scth 			cmn_err(CE_WARN, "init_node: unbind for rebind "
9074145Scth 			    "of node %s failed", path);
9084145Scth 			goto out;
9094145Scth 		}
9104145Scth 
9114145Scth 		/* establish rebinding name */
9124145Scth 		if (DEVI(dip)->devi_rebinding_name == NULL)
9134145Scth 			DEVI(dip)->devi_rebinding_name =
9144145Scth 			    i_ddi_strdup(path, KM_SLEEP);
9154145Scth 
9164145Scth 		/*
9174145Scth 		 * Now that we are demoted and marked for rebind, repromote.
9184145Scth 		 * We need to do this in steps, instead of just calling
9194145Scth 		 * ddi_initchild, so that we can redo the merge operation
9204145Scth 		 * after we are rebound to the path-bound driver.
9214145Scth 		 *
9224145Scth 		 * Start by rebinding node to the path-bound driver.
9234145Scth 		 */
9244145Scth 		if ((error = ndi_devi_bind_driver(dip, 0)) != DDI_SUCCESS) {
9254950Scth 			ndi_rele_devi(pdip);	/* relrease initial hold */
9264145Scth 			cmn_err(CE_WARN, "init_node: rebind "
9274145Scth 			    "of node %s failed", path);
9284145Scth 			goto out;
9294145Scth 		}
9304145Scth 
9314145Scth 		/*
9324145Scth 		 * If the node is not a driver.conf node then merge
9334145Scth 		 * driver.conf properties from new path-bound driver.conf.
9344145Scth 		 */
9354145Scth 		if (ndi_dev_is_persistent_node(dip))
9364145Scth 			(void) i_ndi_make_spec_children(pdip, 0);
9374145Scth 
9384145Scth 		/*
9394145Scth 		 * Now that we have taken care of merge, repromote back
9404145Scth 		 * to DS_INITIALIZED.
9414145Scth 		 */
9424145Scth 		error = ddi_initchild(pdip, dip);
9434145Scth 		NDI_CONFIG_DEBUG((CE_CONT, "init_node: rebind "
9444145Scth 		    "%s 0x%p\n", path, (void *)dip));
9454950Scth 
9464950Scth 		/*
9474950Scth 		 * Release our initial hold. If ddi_initchild() was
9487224Scth 		 * successful then it will return with the active hold.
9494950Scth 		 */
9504950Scth 		ndi_rele_devi(pdip);
9514145Scth 		goto out;
9524145Scth 	}
9534145Scth 
9544145Scth 	/*
9550Sstevel@tonic-gate 	 * Apply multi-parent/deep-nexus optimization to the new node
9560Sstevel@tonic-gate 	 */
9570Sstevel@tonic-gate 	DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
9580Sstevel@tonic-gate 	ddi_optimize_dtree(dip);
9594950Scth 	error = DDI_SUCCESS;		/* return with active hold */
9600Sstevel@tonic-gate 
9614145Scth out:	if (error != DDI_SUCCESS) {
9624145Scth 		/* On failure ensure that DEVI_REBIND is cleared */
9634145Scth 		mutex_enter(&DEVI(dip)->devi_lock);
9644145Scth 		DEVI(dip)->devi_flags &= ~DEVI_REBIND;
9654145Scth 		mutex_exit(&DEVI(dip)->devi_lock);
9664145Scth 	}
9674145Scth 	kmem_free(path, MAXPATHLEN);
9680Sstevel@tonic-gate 	return (error);
9690Sstevel@tonic-gate }
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate /*
9720Sstevel@tonic-gate  * Uninitialize node
9730Sstevel@tonic-gate  * The per-driver list must be held busy during the call.
9740Sstevel@tonic-gate  * A successful uninit_node() releases the init_node() hold on
9750Sstevel@tonic-gate  * the parent by calling ndi_rele_devi().
9760Sstevel@tonic-gate  */
9770Sstevel@tonic-gate static int
9780Sstevel@tonic-gate uninit_node(dev_info_t *dip)
9790Sstevel@tonic-gate {
9800Sstevel@tonic-gate 	int node_state_entry;
9810Sstevel@tonic-gate 	dev_info_t *pdip;
9820Sstevel@tonic-gate 	struct dev_ops *ops;
9830Sstevel@tonic-gate 	int (*f)();
9840Sstevel@tonic-gate 	int error;
9850Sstevel@tonic-gate 	char *addr;
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 	/*
9880Sstevel@tonic-gate 	 * Don't check for references here or else a ref-counted
9890Sstevel@tonic-gate 	 * dip cannot be downgraded by the framework.
9900Sstevel@tonic-gate 	 */
9910Sstevel@tonic-gate 	node_state_entry = i_ddi_node_state(dip);
9920Sstevel@tonic-gate 	ASSERT((node_state_entry == DS_BOUND) ||
9934950Scth 	    (node_state_entry == DS_INITIALIZED));
9940Sstevel@tonic-gate 	pdip = ddi_get_parent(dip);
9950Sstevel@tonic-gate 	ASSERT(pdip);
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "uninit_node: 0x%p(%s%d)\n",
9980Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 	if (((ops = ddi_get_driver(pdip)) == NULL) ||
10010Sstevel@tonic-gate 	    (ops->devo_bus_ops == NULL) ||
10020Sstevel@tonic-gate 	    ((f = ops->devo_bus_ops->bus_ctl) == NULL)) {
10030Sstevel@tonic-gate 		return (DDI_FAILURE);
10040Sstevel@tonic-gate 	}
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	/*
10070Sstevel@tonic-gate 	 * save the @addr prior to DDI_CTLOPS_UNINITCHILD for use in
10080Sstevel@tonic-gate 	 * freeing the instance if it succeeds.
10090Sstevel@tonic-gate 	 */
10100Sstevel@tonic-gate 	if (node_state_entry == DS_INITIALIZED) {
10110Sstevel@tonic-gate 		addr = ddi_get_name_addr(dip);
10120Sstevel@tonic-gate 		if (addr)
10130Sstevel@tonic-gate 			addr = i_ddi_strdup(addr, KM_SLEEP);
10140Sstevel@tonic-gate 	} else {
10150Sstevel@tonic-gate 		addr = NULL;
10160Sstevel@tonic-gate 	}
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 	error = (*f)(pdip, pdip, DDI_CTLOPS_UNINITCHILD, dip, (void *)NULL);
10190Sstevel@tonic-gate 	if (error == DDI_SUCCESS) {
10200Sstevel@tonic-gate 		/* if uninitchild forgot to set devi_addr to NULL do it now */
10210Sstevel@tonic-gate 		ddi_set_name_addr(dip, NULL);
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 		/*
10240Sstevel@tonic-gate 		 * Free instance number. This is a no-op if instance has
10250Sstevel@tonic-gate 		 * been kept by probe_node().  Avoid free when we are called
10260Sstevel@tonic-gate 		 * from init_node (DS_BOUND) because the instance has not yet
10270Sstevel@tonic-gate 		 * been assigned.
10280Sstevel@tonic-gate 		 */
10290Sstevel@tonic-gate 		if (node_state_entry == DS_INITIALIZED) {
10300Sstevel@tonic-gate 			e_ddi_free_instance(dip, addr);
10310Sstevel@tonic-gate 			DEVI(dip)->devi_instance = -1;
10320Sstevel@tonic-gate 		}
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 		/* release the init_node hold */
10350Sstevel@tonic-gate 		ndi_rele_devi(pdip);
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 		remove_global_props(dip);
10384145Scth 
10394145Scth 		/*
10404145Scth 		 * NOTE: The decision on whether to allow a path-oriented
10414145Scth 		 * rebind of a driver.conf enumerated node is made by
10424145Scth 		 * init_node() based on driver_conf_allow_path_alias. The
10434145Scth 		 * rebind code below prevents deletion of system properties
10444145Scth 		 * on driver.conf nodes.
10454145Scth 		 *
10464145Scth 		 * When driver_conf_allow_path_alias is set, property behavior
10474145Scth 		 * on rebound driver.conf file is non-intuitive. For a
10484145Scth 		 * driver.conf node, the unit-address properties come from
10494145Scth 		 * the driver.conf file as system properties. Removing system
10504145Scth 		 * properties from a driver.conf node makes the node
10514145Scth 		 * useless (we get node without unit-address properties) - so
10524145Scth 		 * we leave system properties in place. The result is a node
10534145Scth 		 * where system properties come from the node being rebound,
10544145Scth 		 * and global properties come from the driver.conf file
10554145Scth 		 * of the driver we are rebinding to.  If we could determine
10564145Scth 		 * that the path-oriented alias driver.conf file defined a
10574145Scth 		 * node at the same unit address, it would be best to use
10584145Scth 		 * that node and avoid the non-intuitive property behavior.
10594145Scth 		 * Unfortunately, the current "merge" code does not support
10604145Scth 		 * this, so we live with the non-intuitive property behavior.
10614145Scth 		 */
10624145Scth 		if (!((ndi_dev_is_persistent_node(dip) == 0) &&
10634145Scth 		    (DEVI(dip)->devi_flags & DEVI_REBIND)))
10644145Scth 			e_ddi_prop_remove_all(dip);
10650Sstevel@tonic-gate 	} else {
10660Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "uninit_node failed: 0x%p(%s%d)\n",
10670Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
10680Sstevel@tonic-gate 	}
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 	if (addr)
10710Sstevel@tonic-gate 		kmem_free(addr, strlen(addr) + 1);
10720Sstevel@tonic-gate 	return (error);
10730Sstevel@tonic-gate }
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate /*
10760Sstevel@tonic-gate  * Invoke driver's probe entry point to probe for existence of hardware.
10770Sstevel@tonic-gate  * Keep instance permanent for successful probe and leaf nodes.
10780Sstevel@tonic-gate  *
10790Sstevel@tonic-gate  * Per-driver list must be held busy while calling this function.
10800Sstevel@tonic-gate  */
10810Sstevel@tonic-gate static int
10820Sstevel@tonic-gate probe_node(dev_info_t *dip)
10830Sstevel@tonic-gate {
10840Sstevel@tonic-gate 	int rv;
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_INITIALIZED);
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "probe_node: 0x%p(%s%d)\n",
10890Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 	/* temporarily hold the driver while we probe */
10920Sstevel@tonic-gate 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
10930Sstevel@tonic-gate 	if (DEVI(dip)->devi_ops == NULL) {
10940Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
10950Sstevel@tonic-gate 		    "probe_node: 0x%p(%s%d) cannot load driver\n",
10960Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
10970Sstevel@tonic-gate 		return (DDI_FAILURE);
10980Sstevel@tonic-gate 	}
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate 	if (identify_9e != 0)
11010Sstevel@tonic-gate 		(void) devi_identify(dip);
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 	rv = devi_probe(dip);
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate 	/* release the driver now that probe is complete */
11060Sstevel@tonic-gate 	ndi_rele_driver(dip);
11070Sstevel@tonic-gate 	DEVI(dip)->devi_ops = NULL;
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 	switch (rv) {
11100Sstevel@tonic-gate 	case DDI_PROBE_SUCCESS:			/* found */
11110Sstevel@tonic-gate 	case DDI_PROBE_DONTCARE:		/* ddi_dev_is_sid */
11120Sstevel@tonic-gate 		e_ddi_keep_instance(dip);	/* persist instance */
11130Sstevel@tonic-gate 		rv = DDI_SUCCESS;
11140Sstevel@tonic-gate 		break;
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 	case DDI_PROBE_PARTIAL:			/* maybe later */
11170Sstevel@tonic-gate 	case DDI_PROBE_FAILURE:			/* not found */
11180Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
11190Sstevel@tonic-gate 		    "probe_node: 0x%p(%s%d) no hardware found%s\n",
11200Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip),
11210Sstevel@tonic-gate 		    (rv == DDI_PROBE_PARTIAL) ? " yet" : ""));
11220Sstevel@tonic-gate 		rv = DDI_FAILURE;
11230Sstevel@tonic-gate 		break;
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate 	default:
11260Sstevel@tonic-gate #ifdef	DEBUG
11270Sstevel@tonic-gate 		cmn_err(CE_WARN, "probe_node: %s%d: illegal probe(9E) value",
11280Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
11290Sstevel@tonic-gate #endif	/* DEBUG */
11300Sstevel@tonic-gate 		rv = DDI_FAILURE;
11310Sstevel@tonic-gate 		break;
11320Sstevel@tonic-gate 	}
11330Sstevel@tonic-gate 	return (rv);
11340Sstevel@tonic-gate }
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate /*
11370Sstevel@tonic-gate  * Unprobe a node. Simply reset the node state.
11380Sstevel@tonic-gate  * Per-driver list must be held busy while calling this function.
11390Sstevel@tonic-gate  */
11400Sstevel@tonic-gate static int
11410Sstevel@tonic-gate unprobe_node(dev_info_t *dip)
11420Sstevel@tonic-gate {
11430Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_PROBED);
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 	/*
11460Sstevel@tonic-gate 	 * Don't check for references here or else a ref-counted
11470Sstevel@tonic-gate 	 * dip cannot be downgraded by the framework.
11480Sstevel@tonic-gate 	 */
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "unprobe_node: 0x%p(name = %s)\n",
11510Sstevel@tonic-gate 	    (void *)dip, ddi_node_name(dip)));
11520Sstevel@tonic-gate 	return (DDI_SUCCESS);
11530Sstevel@tonic-gate }
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate /*
11560Sstevel@tonic-gate  * Attach devinfo node.
11570Sstevel@tonic-gate  * Per-driver list must be held busy.
11580Sstevel@tonic-gate  */
11590Sstevel@tonic-gate static int
11600Sstevel@tonic-gate attach_node(dev_info_t *dip)
11610Sstevel@tonic-gate {
11620Sstevel@tonic-gate 	int rv;
11630Sstevel@tonic-gate 
11642155Scth 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
11650Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_PROBED);
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d)\n",
11680Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate 	/*
11710Sstevel@tonic-gate 	 * Tell mpxio framework that a node is about to online.
11720Sstevel@tonic-gate 	 */
11730Sstevel@tonic-gate 	if ((rv = mdi_devi_online(dip, 0)) != NDI_SUCCESS) {
11740Sstevel@tonic-gate 		return (DDI_FAILURE);
11750Sstevel@tonic-gate 	}
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate 	/* no recursive attachment */
11780Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_ops == NULL);
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 	/*
11810Sstevel@tonic-gate 	 * Hold driver the node is bound to.
11820Sstevel@tonic-gate 	 */
11830Sstevel@tonic-gate 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
11840Sstevel@tonic-gate 	if (DEVI(dip)->devi_ops == NULL) {
11850Sstevel@tonic-gate 		/*
11860Sstevel@tonic-gate 		 * We were able to load driver for probing, so we should
11870Sstevel@tonic-gate 		 * not get here unless something really bad happened.
11880Sstevel@tonic-gate 		 */
11890Sstevel@tonic-gate 		cmn_err(CE_WARN, "attach_node: no driver for major %d",
11900Sstevel@tonic-gate 		    DEVI(dip)->devi_major);
11910Sstevel@tonic-gate 		return (DDI_FAILURE);
11920Sstevel@tonic-gate 	}
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 	if (NEXUS_DRV(DEVI(dip)->devi_ops))
11950Sstevel@tonic-gate 		DEVI(dip)->devi_taskq = ddi_taskq_create(dip,
11960Sstevel@tonic-gate 		    "nexus_enum_tq", 1,
11970Sstevel@tonic-gate 		    TASKQ_DEFAULTPRI, 0);
11980Sstevel@tonic-gate 
1199495Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
12002009Sdm120769 	DEVI_SET_ATTACHING(dip);
12010Sstevel@tonic-gate 	DEVI_SET_NEED_RESET(dip);
1202495Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
1203495Scth 
12040Sstevel@tonic-gate 	rv = devi_attach(dip, DDI_ATTACH);
1205495Scth 
12062009Sdm120769 	mutex_enter(&(DEVI(dip)->devi_lock));
12072009Sdm120769 	DEVI_CLR_ATTACHING(dip);
12082009Sdm120769 
12091961Scth 	if (rv != DDI_SUCCESS) {
12102155Scth 		DEVI_CLR_NEED_RESET(dip);
12112155Scth 
1212438Scth 		/* ensure that devids are unregistered */
12130Sstevel@tonic-gate 		if (DEVI(dip)->devi_flags & DEVI_REGISTERED_DEVID) {
1214438Scth 			DEVI(dip)->devi_flags &= ~DEVI_REGISTERED_DEVID;
1215438Scth 			mutex_exit(&DEVI(dip)->devi_lock);
12166640Scth 			ddi_devid_unregister(dip);
1217438Scth 		} else
1218438Scth 			mutex_exit(&DEVI(dip)->devi_lock);
1219438Scth 
12200Sstevel@tonic-gate 		/*
12210Sstevel@tonic-gate 		 * Cleanup dacf reservations
12220Sstevel@tonic-gate 		 */
12230Sstevel@tonic-gate 		mutex_enter(&dacf_lock);
12240Sstevel@tonic-gate 		dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH);
12250Sstevel@tonic-gate 		dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH);
12260Sstevel@tonic-gate 		mutex_exit(&dacf_lock);
12270Sstevel@tonic-gate 		if (DEVI(dip)->devi_taskq)
12280Sstevel@tonic-gate 			ddi_taskq_destroy(DEVI(dip)->devi_taskq);
12290Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 		/* release the driver if attach failed */
12320Sstevel@tonic-gate 		ndi_rele_driver(dip);
12330Sstevel@tonic-gate 		DEVI(dip)->devi_ops = NULL;
12340Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d) failed\n",
12350Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
12360Sstevel@tonic-gate 		return (DDI_FAILURE);
12372009Sdm120769 	} else
12382009Sdm120769 		mutex_exit(&DEVI(dip)->devi_lock);
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate 	/* successful attach, return with driver held */
1241495Scth 
12420Sstevel@tonic-gate 	return (DDI_SUCCESS);
12430Sstevel@tonic-gate }
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate /*
12460Sstevel@tonic-gate  * Detach devinfo node.
12470Sstevel@tonic-gate  * Per-driver list must be held busy.
12480Sstevel@tonic-gate  */
12490Sstevel@tonic-gate static int
12500Sstevel@tonic-gate detach_node(dev_info_t *dip, uint_t flag)
12510Sstevel@tonic-gate {
125253Scth 	struct devnames	*dnp;
125353Scth 	int		rv;
125453Scth 
12552155Scth 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
12560Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_ATTACHED);
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate 	/* check references */
12590Sstevel@tonic-gate 	if (DEVI(dip)->devi_ref)
12600Sstevel@tonic-gate 		return (DDI_FAILURE);
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "detach_node: 0x%p(%s%d)\n",
12630Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
12640Sstevel@tonic-gate 
12652155Scth 	/*
12662155Scth 	 * NOTE: If we are processing a pHCI node then the calling code
12672155Scth 	 * must detect this and ndi_devi_enter() in (vHCI, parent(pHCI))
12682155Scth 	 * order unless pHCI and vHCI are siblings.  Code paths leading
12692155Scth 	 * here that must ensure this ordering include:
12702155Scth 	 * unconfig_immediate_children(), devi_unconfig_one(),
12712155Scth 	 * ndi_devi_unconfig_one(), ndi_devi_offline().
12722155Scth 	 */
12732155Scth 	ASSERT(!MDI_PHCI(dip) ||
12742155Scth 	    (ddi_get_parent(mdi_devi_get_vdip(dip)) == ddi_get_parent(dip)) ||
12752155Scth 	    DEVI_BUSY_OWNED(mdi_devi_get_vdip(dip)));
12762155Scth 
12770Sstevel@tonic-gate 	/* Offline the device node with the mpxio framework. */
12780Sstevel@tonic-gate 	if (mdi_devi_offline(dip, flag) != NDI_SUCCESS) {
12790Sstevel@tonic-gate 		return (DDI_FAILURE);
12800Sstevel@tonic-gate 	}
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 	/* drain the taskq */
12830Sstevel@tonic-gate 	if (DEVI(dip)->devi_taskq)
12840Sstevel@tonic-gate 		ddi_taskq_wait(DEVI(dip)->devi_taskq);
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate 	rv = devi_detach(dip, DDI_DETACH);
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 	if (rv != DDI_SUCCESS) {
12890Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
12900Sstevel@tonic-gate 		    "detach_node: 0x%p(%s%d) failed\n",
12910Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
12920Sstevel@tonic-gate 		return (DDI_FAILURE);
12930Sstevel@tonic-gate 	}
12940Sstevel@tonic-gate 
12952155Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
12962155Scth 	DEVI_CLR_NEED_RESET(dip);
12972155Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
12982155Scth 
12990Sstevel@tonic-gate 	/* destroy the taskq */
13000Sstevel@tonic-gate 	if (DEVI(dip)->devi_taskq) {
13010Sstevel@tonic-gate 		ddi_taskq_destroy(DEVI(dip)->devi_taskq);
13020Sstevel@tonic-gate 		DEVI(dip)->devi_taskq = NULL;
13030Sstevel@tonic-gate 	}
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate 	/* Cleanup dacf reservations */
13060Sstevel@tonic-gate 	mutex_enter(&dacf_lock);
13070Sstevel@tonic-gate 	dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH);
13080Sstevel@tonic-gate 	dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH);
13090Sstevel@tonic-gate 	mutex_exit(&dacf_lock);
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 	/* Remove properties and minor nodes in case driver forgots */
13120Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
13130Sstevel@tonic-gate 	ddi_prop_remove_all(dip);
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate 	/* a detached node can't have attached or .conf children */
13160Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
13170Sstevel@tonic-gate 	DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN|DEVI_ATTACHED_CHILDREN);
1318438Scth 
1319438Scth 	/* ensure that devids registered during attach are unregistered */
13200Sstevel@tonic-gate 	if (DEVI(dip)->devi_flags & DEVI_REGISTERED_DEVID) {
13210Sstevel@tonic-gate 		DEVI(dip)->devi_flags &= ~DEVI_REGISTERED_DEVID;
1322438Scth 		mutex_exit(&DEVI(dip)->devi_lock);
13236640Scth 		ddi_devid_unregister(dip);
1324438Scth 	} else
1325438Scth 		mutex_exit(&DEVI(dip)->devi_lock);
13260Sstevel@tonic-gate 
132753Scth 	/*
132853Scth 	 * If the instance has successfully detached in detach_driver() context,
132953Scth 	 * clear DN_DRIVER_HELD for correct ddi_hold_installed_driver()
133053Scth 	 * behavior. Consumers like qassociate() depend on this (via clnopen()).
133153Scth 	 */
133253Scth 	if (flag & NDI_DETACH_DRIVER) {
133353Scth 		dnp = &(devnamesp[DEVI(dip)->devi_major]);
133453Scth 		LOCK_DEV_OPS(&dnp->dn_lock);
133553Scth 		dnp->dn_flags &= ~DN_DRIVER_HELD;
133653Scth 		UNLOCK_DEV_OPS(&dnp->dn_lock);
133753Scth 	}
133853Scth 
13390Sstevel@tonic-gate 	/* successful detach, release the driver */
13400Sstevel@tonic-gate 	ndi_rele_driver(dip);
13410Sstevel@tonic-gate 	DEVI(dip)->devi_ops = NULL;
13420Sstevel@tonic-gate 	return (DDI_SUCCESS);
13430Sstevel@tonic-gate }
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate /*
13460Sstevel@tonic-gate  * Run dacf post_attach routines
13470Sstevel@tonic-gate  */
13480Sstevel@tonic-gate static int
13490Sstevel@tonic-gate postattach_node(dev_info_t *dip)
13500Sstevel@tonic-gate {
13510Sstevel@tonic-gate 	int rval;
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate 	/*
13540Sstevel@tonic-gate 	 * For hotplug busses like USB, it's possible that devices
13550Sstevel@tonic-gate 	 * are removed but dip is still around. We don't want to
13560Sstevel@tonic-gate 	 * run dacf routines as part of detach failure recovery.
13570Sstevel@tonic-gate 	 *
13580Sstevel@tonic-gate 	 * Pretend success until we figure out how to prevent
13590Sstevel@tonic-gate 	 * access to such devinfo nodes.
13600Sstevel@tonic-gate 	 */
13610Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_REMOVED(dip))
13620Sstevel@tonic-gate 		return (DDI_SUCCESS);
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate 	/*
13650Sstevel@tonic-gate 	 * if dacf_postattach failed, report it to the framework
13660Sstevel@tonic-gate 	 * so that it can be retried later at the open time.
13670Sstevel@tonic-gate 	 */
13680Sstevel@tonic-gate 	mutex_enter(&dacf_lock);
13690Sstevel@tonic-gate 	rval = dacfc_postattach(dip);
13700Sstevel@tonic-gate 	mutex_exit(&dacf_lock);
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 	/*
13730Sstevel@tonic-gate 	 * Plumbing during postattach may fail because of the
13740Sstevel@tonic-gate 	 * underlying device is not ready. This will fail ndi_devi_config()
13750Sstevel@tonic-gate 	 * in dv_filldir() and a warning message is issued. The message
13760Sstevel@tonic-gate 	 * from here will explain what happened
13770Sstevel@tonic-gate 	 */
13780Sstevel@tonic-gate 	if (rval != DACF_SUCCESS) {
13790Sstevel@tonic-gate 		cmn_err(CE_WARN, "Postattach failed for %s%d\n",
13800Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
13810Sstevel@tonic-gate 		return (DDI_FAILURE);
13820Sstevel@tonic-gate 	}
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate 	return (DDI_SUCCESS);
13850Sstevel@tonic-gate }
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate /*
13880Sstevel@tonic-gate  * Run dacf pre-detach routines
13890Sstevel@tonic-gate  */
13900Sstevel@tonic-gate static int
13910Sstevel@tonic-gate predetach_node(dev_info_t *dip, uint_t flag)
13920Sstevel@tonic-gate {
13930Sstevel@tonic-gate 	int ret;
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate 	/*
13960Sstevel@tonic-gate 	 * Don't auto-detach if DDI_FORCEATTACH or DDI_NO_AUTODETACH
13970Sstevel@tonic-gate 	 * properties are set.
13980Sstevel@tonic-gate 	 */
13990Sstevel@tonic-gate 	if (flag & NDI_AUTODETACH) {
14000Sstevel@tonic-gate 		struct devnames *dnp;
14010Sstevel@tonic-gate 		int pflag = DDI_PROP_NOTPROM | DDI_PROP_DONTPASS;
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate 		if ((ddi_prop_get_int(DDI_DEV_T_ANY, dip,
14044950Scth 		    pflag, DDI_FORCEATTACH, 0) == 1) ||
14050Sstevel@tonic-gate 		    (ddi_prop_get_int(DDI_DEV_T_ANY, dip,
14064950Scth 		    pflag, DDI_NO_AUTODETACH, 0) == 1))
14070Sstevel@tonic-gate 			return (DDI_FAILURE);
14080Sstevel@tonic-gate 
14090Sstevel@tonic-gate 		/* check for driver global version of DDI_NO_AUTODETACH */
14100Sstevel@tonic-gate 		dnp = &devnamesp[DEVI(dip)->devi_major];
14110Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
14120Sstevel@tonic-gate 		if (dnp->dn_flags & DN_NO_AUTODETACH) {
14130Sstevel@tonic-gate 			UNLOCK_DEV_OPS(&dnp->dn_lock);
14140Sstevel@tonic-gate 			return (DDI_FAILURE);
14150Sstevel@tonic-gate 		}
14160Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
14170Sstevel@tonic-gate 	}
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 	mutex_enter(&dacf_lock);
14200Sstevel@tonic-gate 	ret = dacfc_predetach(dip);
14210Sstevel@tonic-gate 	mutex_exit(&dacf_lock);
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate 	return (ret);
14240Sstevel@tonic-gate }
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate /*
14270Sstevel@tonic-gate  * Wrapper for making multiple state transitions
14280Sstevel@tonic-gate  */
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate /*
14310Sstevel@tonic-gate  * i_ndi_config_node: upgrade dev_info node into a specified state.
14320Sstevel@tonic-gate  * It is a bit tricky because the locking protocol changes before and
14330Sstevel@tonic-gate  * after a node is bound to a driver. All locks are held external to
14340Sstevel@tonic-gate  * this function.
14350Sstevel@tonic-gate  */
14360Sstevel@tonic-gate int
14370Sstevel@tonic-gate i_ndi_config_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag)
14380Sstevel@tonic-gate {
14390Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flag))
14400Sstevel@tonic-gate 	int rv = DDI_SUCCESS;
14410Sstevel@tonic-gate 
14420Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
14430Sstevel@tonic-gate 
14440Sstevel@tonic-gate 	while ((i_ddi_node_state(dip) < state) && (rv == DDI_SUCCESS)) {
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate 		/* don't allow any more changes to the device tree */
14470Sstevel@tonic-gate 		if (devinfo_freeze) {
14480Sstevel@tonic-gate 			rv = DDI_FAILURE;
14490Sstevel@tonic-gate 			break;
14500Sstevel@tonic-gate 		}
14510Sstevel@tonic-gate 
14520Sstevel@tonic-gate 		switch (i_ddi_node_state(dip)) {
14530Sstevel@tonic-gate 		case DS_PROTO:
14540Sstevel@tonic-gate 			/*
14550Sstevel@tonic-gate 			 * only caller can reference this node, no external
14560Sstevel@tonic-gate 			 * locking needed.
14570Sstevel@tonic-gate 			 */
14580Sstevel@tonic-gate 			link_node(dip);
14590Sstevel@tonic-gate 			i_ddi_set_node_state(dip, DS_LINKED);
14600Sstevel@tonic-gate 			break;
14610Sstevel@tonic-gate 		case DS_LINKED:
14620Sstevel@tonic-gate 			/*
14630Sstevel@tonic-gate 			 * Three code path may attempt to bind a node:
14640Sstevel@tonic-gate 			 * - boot code
14650Sstevel@tonic-gate 			 * - add_drv
14660Sstevel@tonic-gate 			 * - hotplug thread
14670Sstevel@tonic-gate 			 * Boot code is single threaded, add_drv synchronize
14680Sstevel@tonic-gate 			 * on a userland lock, and hotplug synchronize on
14690Sstevel@tonic-gate 			 * hotplug_lk. There could be a race between add_drv
14700Sstevel@tonic-gate 			 * and hotplug thread. We'll live with this until the
14710Sstevel@tonic-gate 			 * conversion to top-down loading.
14720Sstevel@tonic-gate 			 */
14730Sstevel@tonic-gate 			if ((rv = bind_node(dip)) == DDI_SUCCESS)
14740Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_BOUND);
14754145Scth 
14760Sstevel@tonic-gate 			break;
14770Sstevel@tonic-gate 		case DS_BOUND:
14780Sstevel@tonic-gate 			/*
14790Sstevel@tonic-gate 			 * The following transitions synchronizes on the
14800Sstevel@tonic-gate 			 * per-driver busy changing flag, since we already
14810Sstevel@tonic-gate 			 * have a driver.
14820Sstevel@tonic-gate 			 */
14830Sstevel@tonic-gate 			if ((rv = init_node(dip)) == DDI_SUCCESS)
14840Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_INITIALIZED);
14850Sstevel@tonic-gate 			break;
14860Sstevel@tonic-gate 		case DS_INITIALIZED:
14870Sstevel@tonic-gate 			if ((rv = probe_node(dip)) == DDI_SUCCESS)
14880Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_PROBED);
14890Sstevel@tonic-gate 			break;
14900Sstevel@tonic-gate 		case DS_PROBED:
14914845Svikram 			i_ddi_check_retire(dip);
14920Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, 1);
14930Sstevel@tonic-gate 			if ((rv = attach_node(dip)) == DDI_SUCCESS)
14940Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_ATTACHED);
14950Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, -1);
14960Sstevel@tonic-gate 			break;
14970Sstevel@tonic-gate 		case DS_ATTACHED:
14980Sstevel@tonic-gate 			if ((rv = postattach_node(dip)) == DDI_SUCCESS)
14990Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_READY);
15000Sstevel@tonic-gate 			break;
15010Sstevel@tonic-gate 		case DS_READY:
15020Sstevel@tonic-gate 			break;
15030Sstevel@tonic-gate 		default:
15040Sstevel@tonic-gate 			/* should never reach here */
15050Sstevel@tonic-gate 			ASSERT("unknown devinfo state");
15060Sstevel@tonic-gate 		}
15070Sstevel@tonic-gate 	}
15080Sstevel@tonic-gate 
15090Sstevel@tonic-gate 	if (ddidebug & DDI_AUDIT)
15100Sstevel@tonic-gate 		da_log_enter(dip);
15110Sstevel@tonic-gate 	return (rv);
15120Sstevel@tonic-gate }
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate /*
15150Sstevel@tonic-gate  * i_ndi_unconfig_node: downgrade dev_info node into a specified state.
15160Sstevel@tonic-gate  */
15170Sstevel@tonic-gate int
15180Sstevel@tonic-gate i_ndi_unconfig_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag)
15190Sstevel@tonic-gate {
15204950Scth 	int	rv = DDI_SUCCESS;
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
15230Sstevel@tonic-gate 
15240Sstevel@tonic-gate 	while ((i_ddi_node_state(dip) > state) && (rv == DDI_SUCCESS)) {
15250Sstevel@tonic-gate 
15260Sstevel@tonic-gate 		/* don't allow any more changes to the device tree */
15270Sstevel@tonic-gate 		if (devinfo_freeze) {
15280Sstevel@tonic-gate 			rv = DDI_FAILURE;
15290Sstevel@tonic-gate 			break;
15300Sstevel@tonic-gate 		}
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 		switch (i_ddi_node_state(dip)) {
15330Sstevel@tonic-gate 		case DS_PROTO:
15340Sstevel@tonic-gate 			break;
15350Sstevel@tonic-gate 		case DS_LINKED:
15360Sstevel@tonic-gate 			/*
15370Sstevel@tonic-gate 			 * Persistent nodes are only removed by hotplug code
15380Sstevel@tonic-gate 			 * .conf nodes synchronizes on per-driver list.
15390Sstevel@tonic-gate 			 */
15400Sstevel@tonic-gate 			if ((rv = unlink_node(dip)) == DDI_SUCCESS)
15410Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_PROTO);
15420Sstevel@tonic-gate 			break;
15430Sstevel@tonic-gate 		case DS_BOUND:
15440Sstevel@tonic-gate 			/*
15450Sstevel@tonic-gate 			 * The following transitions synchronizes on the
15460Sstevel@tonic-gate 			 * per-driver busy changing flag, since we already
15470Sstevel@tonic-gate 			 * have a driver.
15480Sstevel@tonic-gate 			 */
15490Sstevel@tonic-gate 			if ((rv = unbind_node(dip)) == DDI_SUCCESS)
15500Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_LINKED);
15510Sstevel@tonic-gate 			break;
15520Sstevel@tonic-gate 		case DS_INITIALIZED:
15530Sstevel@tonic-gate 			if ((rv = uninit_node(dip)) == DDI_SUCCESS)
15540Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_BOUND);
15550Sstevel@tonic-gate 			break;
15560Sstevel@tonic-gate 		case DS_PROBED:
15570Sstevel@tonic-gate 			if ((rv = unprobe_node(dip)) == DDI_SUCCESS)
15580Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_INITIALIZED);
15590Sstevel@tonic-gate 			break;
15600Sstevel@tonic-gate 		case DS_ATTACHED:
15610Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, 1);
1562495Scth 
1563495Scth 			mutex_enter(&(DEVI(dip)->devi_lock));
15640Sstevel@tonic-gate 			DEVI_SET_DETACHING(dip);
1565495Scth 			mutex_exit(&(DEVI(dip)->devi_lock));
1566495Scth 
15670Sstevel@tonic-gate 			membar_enter();	/* ensure visibility for hold_devi */
15680Sstevel@tonic-gate 
15690Sstevel@tonic-gate 			if ((rv = detach_node(dip, flag)) == DDI_SUCCESS)
15700Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_PROBED);
1571495Scth 
1572495Scth 			mutex_enter(&(DEVI(dip)->devi_lock));
15730Sstevel@tonic-gate 			DEVI_CLR_DETACHING(dip);
1574495Scth 			mutex_exit(&(DEVI(dip)->devi_lock));
1575495Scth 
15760Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, -1);
15770Sstevel@tonic-gate 			break;
15780Sstevel@tonic-gate 		case DS_READY:
15790Sstevel@tonic-gate 			if ((rv = predetach_node(dip, flag)) == DDI_SUCCESS)
15800Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_ATTACHED);
15810Sstevel@tonic-gate 			break;
15820Sstevel@tonic-gate 		default:
15830Sstevel@tonic-gate 			ASSERT("unknown devinfo state");
15840Sstevel@tonic-gate 		}
15850Sstevel@tonic-gate 	}
15860Sstevel@tonic-gate 	da_log_enter(dip);
15870Sstevel@tonic-gate 	return (rv);
15880Sstevel@tonic-gate }
15890Sstevel@tonic-gate 
15900Sstevel@tonic-gate /*
15910Sstevel@tonic-gate  * ddi_initchild: transform node to DS_INITIALIZED state
15920Sstevel@tonic-gate  */
15930Sstevel@tonic-gate int
15940Sstevel@tonic-gate ddi_initchild(dev_info_t *parent, dev_info_t *proto)
15950Sstevel@tonic-gate {
15960Sstevel@tonic-gate 	int ret, circ;
15970Sstevel@tonic-gate 
15980Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
15990Sstevel@tonic-gate 	ret = i_ndi_config_node(proto, DS_INITIALIZED, 0);
16000Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
16010Sstevel@tonic-gate 
16020Sstevel@tonic-gate 	return (ret);
16030Sstevel@tonic-gate }
16040Sstevel@tonic-gate 
16050Sstevel@tonic-gate /*
16060Sstevel@tonic-gate  * ddi_uninitchild: transform node down to DS_BOUND state
16070Sstevel@tonic-gate  */
16080Sstevel@tonic-gate int
16090Sstevel@tonic-gate ddi_uninitchild(dev_info_t *dip)
16100Sstevel@tonic-gate {
16110Sstevel@tonic-gate 	int ret, circ;
16120Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
16130Sstevel@tonic-gate 	ASSERT(parent);
16140Sstevel@tonic-gate 
16150Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
16160Sstevel@tonic-gate 	ret = i_ndi_unconfig_node(dip, DS_BOUND, 0);
16170Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 	return (ret);
16200Sstevel@tonic-gate }
16210Sstevel@tonic-gate 
16220Sstevel@tonic-gate /*
16231333Scth  * i_ddi_attachchild: transform node to DS_READY/i_ddi_devi_attached() state
16240Sstevel@tonic-gate  */
16250Sstevel@tonic-gate static int
16260Sstevel@tonic-gate i_ddi_attachchild(dev_info_t *dip)
16270Sstevel@tonic-gate {
16282155Scth 	dev_info_t	*parent = ddi_get_parent(dip);
16292155Scth 	int		ret;
16302155Scth 
16312155Scth 	ASSERT(parent && DEVI_BUSY_OWNED(parent));
16320Sstevel@tonic-gate 
16330Sstevel@tonic-gate 	if ((i_ddi_node_state(dip) < DS_BOUND) || DEVI_IS_DEVICE_OFFLINE(dip))
16340Sstevel@tonic-gate 		return (DDI_FAILURE);
16350Sstevel@tonic-gate 
16360Sstevel@tonic-gate 	ret = i_ndi_config_node(dip, DS_READY, 0);
16370Sstevel@tonic-gate 	if (ret == NDI_SUCCESS) {
16380Sstevel@tonic-gate 		ret = DDI_SUCCESS;
16390Sstevel@tonic-gate 	} else {
16400Sstevel@tonic-gate 		/*
16410Sstevel@tonic-gate 		 * Take it down to DS_INITIALIZED so pm_pre_probe is run
16420Sstevel@tonic-gate 		 * on the next attach
16430Sstevel@tonic-gate 		 */
16440Sstevel@tonic-gate 		(void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0);
16450Sstevel@tonic-gate 		ret = DDI_FAILURE;
16460Sstevel@tonic-gate 	}
16470Sstevel@tonic-gate 
16480Sstevel@tonic-gate 	return (ret);
16490Sstevel@tonic-gate }
16500Sstevel@tonic-gate 
16510Sstevel@tonic-gate /*
16520Sstevel@tonic-gate  * i_ddi_detachchild: transform node down to DS_PROBED state
16530Sstevel@tonic-gate  *	If it fails, put it back to DS_READY state.
16540Sstevel@tonic-gate  * NOTE: A node that fails detach may be at DS_ATTACHED instead
16551333Scth  * of DS_READY for a small amount of time - this is the source of
16561333Scth  * transient DS_READY->DS_ATTACHED->DS_READY state changes.
16570Sstevel@tonic-gate  */
16580Sstevel@tonic-gate static int
16590Sstevel@tonic-gate i_ddi_detachchild(dev_info_t *dip, uint_t flags)
16600Sstevel@tonic-gate {
16612155Scth 	dev_info_t	*parent = ddi_get_parent(dip);
16622155Scth 	int		ret;
16632155Scth 
16642155Scth 	ASSERT(parent && DEVI_BUSY_OWNED(parent));
16652155Scth 
16660Sstevel@tonic-gate 	ret = i_ndi_unconfig_node(dip, DS_PROBED, flags);
16670Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
16680Sstevel@tonic-gate 		(void) i_ndi_config_node(dip, DS_READY, 0);
16690Sstevel@tonic-gate 	else
16700Sstevel@tonic-gate 		/* allow pm_pre_probe to reestablish pm state */
16710Sstevel@tonic-gate 		(void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0);
16720Sstevel@tonic-gate 	return (ret);
16730Sstevel@tonic-gate }
16740Sstevel@tonic-gate 
16750Sstevel@tonic-gate /*
16760Sstevel@tonic-gate  * Add a child and bind to driver
16770Sstevel@tonic-gate  */
16780Sstevel@tonic-gate dev_info_t *
16790Sstevel@tonic-gate ddi_add_child(dev_info_t *pdip, char *name, uint_t nodeid, uint_t unit)
16800Sstevel@tonic-gate {
16810Sstevel@tonic-gate 	int circ;
16820Sstevel@tonic-gate 	dev_info_t *dip;
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate 	/* allocate a new node */
16850Sstevel@tonic-gate 	dip = i_ddi_alloc_node(pdip, name, nodeid, (int)unit, NULL, KM_SLEEP);
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
16880Sstevel@tonic-gate 	(void) i_ndi_config_node(dip, DS_BOUND, 0);
16890Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
16900Sstevel@tonic-gate 	return (dip);
16910Sstevel@tonic-gate }
16920Sstevel@tonic-gate 
16930Sstevel@tonic-gate /*
16940Sstevel@tonic-gate  * ddi_remove_child: remove the dip. The parent must be attached and held
16950Sstevel@tonic-gate  */
16960Sstevel@tonic-gate int
16970Sstevel@tonic-gate ddi_remove_child(dev_info_t *dip, int dummy)
16980Sstevel@tonic-gate {
16990Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dummy))
17000Sstevel@tonic-gate 	int circ, ret;
17010Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
17020Sstevel@tonic-gate 	ASSERT(parent);
17030Sstevel@tonic-gate 
17040Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
17050Sstevel@tonic-gate 
17060Sstevel@tonic-gate 	/*
17070Sstevel@tonic-gate 	 * If we still have children, for example SID nodes marked
17080Sstevel@tonic-gate 	 * as persistent but not attached, attempt to remove them.
17090Sstevel@tonic-gate 	 */
17100Sstevel@tonic-gate 	if (DEVI(dip)->devi_child) {
17110Sstevel@tonic-gate 		ret = ndi_devi_unconfig(dip, NDI_DEVI_REMOVE);
17120Sstevel@tonic-gate 		if (ret != NDI_SUCCESS) {
17130Sstevel@tonic-gate 			ndi_devi_exit(parent, circ);
17140Sstevel@tonic-gate 			return (DDI_FAILURE);
17150Sstevel@tonic-gate 		}
17160Sstevel@tonic-gate 		ASSERT(DEVI(dip)->devi_child == NULL);
17170Sstevel@tonic-gate 	}
17180Sstevel@tonic-gate 
17190Sstevel@tonic-gate 	ret = i_ndi_unconfig_node(dip, DS_PROTO, 0);
17200Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
17210Sstevel@tonic-gate 
17220Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
17230Sstevel@tonic-gate 		return (ret);
17240Sstevel@tonic-gate 
17250Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_PROTO);
17260Sstevel@tonic-gate 	i_ddi_free_node(dip);
17270Sstevel@tonic-gate 	return (DDI_SUCCESS);
17280Sstevel@tonic-gate }
17290Sstevel@tonic-gate 
17300Sstevel@tonic-gate /*
17310Sstevel@tonic-gate  * NDI wrappers for ref counting, node allocation, and transitions
17320Sstevel@tonic-gate  */
17330Sstevel@tonic-gate 
17340Sstevel@tonic-gate /*
17350Sstevel@tonic-gate  * Hold/release the devinfo node itself.
17360Sstevel@tonic-gate  * Caller is assumed to prevent the devi from detaching during this call
17370Sstevel@tonic-gate  */
17380Sstevel@tonic-gate void
17390Sstevel@tonic-gate ndi_hold_devi(dev_info_t *dip)
17400Sstevel@tonic-gate {
17410Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
17420Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_ref >= 0);
17430Sstevel@tonic-gate 	DEVI(dip)->devi_ref++;
17440Sstevel@tonic-gate 	membar_enter();			/* make sure stores are flushed */
17450Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
17460Sstevel@tonic-gate }
17470Sstevel@tonic-gate 
17480Sstevel@tonic-gate void
17490Sstevel@tonic-gate ndi_rele_devi(dev_info_t *dip)
17500Sstevel@tonic-gate {
17510Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_ref > 0);
17520Sstevel@tonic-gate 
17530Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
17540Sstevel@tonic-gate 	DEVI(dip)->devi_ref--;
17550Sstevel@tonic-gate 	membar_enter();			/* make sure stores are flushed */
17560Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
17570Sstevel@tonic-gate }
17580Sstevel@tonic-gate 
17590Sstevel@tonic-gate int
17600Sstevel@tonic-gate e_ddi_devi_holdcnt(dev_info_t *dip)
17610Sstevel@tonic-gate {
17620Sstevel@tonic-gate 	return (DEVI(dip)->devi_ref);
17630Sstevel@tonic-gate }
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate /*
17660Sstevel@tonic-gate  * Hold/release the driver the devinfo node is bound to.
17670Sstevel@tonic-gate  */
17680Sstevel@tonic-gate struct dev_ops *
17690Sstevel@tonic-gate ndi_hold_driver(dev_info_t *dip)
17700Sstevel@tonic-gate {
17710Sstevel@tonic-gate 	if (i_ddi_node_state(dip) < DS_BOUND)
17720Sstevel@tonic-gate 		return (NULL);
17730Sstevel@tonic-gate 
17740Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_major != -1);
17750Sstevel@tonic-gate 	return (mod_hold_dev_by_major(DEVI(dip)->devi_major));
17760Sstevel@tonic-gate }
17770Sstevel@tonic-gate 
17780Sstevel@tonic-gate void
17790Sstevel@tonic-gate ndi_rele_driver(dev_info_t *dip)
17800Sstevel@tonic-gate {
17810Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) >= DS_BOUND);
17820Sstevel@tonic-gate 	mod_rele_dev_by_major(DEVI(dip)->devi_major);
17830Sstevel@tonic-gate }
17840Sstevel@tonic-gate 
17850Sstevel@tonic-gate /*
17867224Scth  * Single thread entry into devinfo node for modifying its children (devinfo,
17877224Scth  * pathinfo, and minor). To verify in ASSERTS use DEVI_BUSY_OWNED macro.
17880Sstevel@tonic-gate  */
17890Sstevel@tonic-gate void
17900Sstevel@tonic-gate ndi_devi_enter(dev_info_t *dip, int *circular)
17910Sstevel@tonic-gate {
17920Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
17930Sstevel@tonic-gate 	ASSERT(dip != NULL);
17940Sstevel@tonic-gate 
17952155Scth 	/* for vHCI, enforce (vHCI, pHCI) ndi_deve_enter() order */
17962155Scth 	ASSERT(!MDI_VHCI(dip) || (mdi_devi_pdip_entered(dip) == 0) ||
17972155Scth 	    DEVI_BUSY_OWNED(dip));
17982155Scth 
17990Sstevel@tonic-gate 	mutex_enter(&devi->devi_lock);
18000Sstevel@tonic-gate 	if (devi->devi_busy_thread == curthread) {
18010Sstevel@tonic-gate 		devi->devi_circular++;
18020Sstevel@tonic-gate 	} else {
18030Sstevel@tonic-gate 		while (DEVI_BUSY_CHANGING(devi) && !panicstr)
18040Sstevel@tonic-gate 			cv_wait(&(devi->devi_cv), &(devi->devi_lock));
18050Sstevel@tonic-gate 		if (panicstr) {
18060Sstevel@tonic-gate 			mutex_exit(&devi->devi_lock);
18070Sstevel@tonic-gate 			return;
18080Sstevel@tonic-gate 		}
18090Sstevel@tonic-gate 		devi->devi_flags |= DEVI_BUSY;
18100Sstevel@tonic-gate 		devi->devi_busy_thread = curthread;
18110Sstevel@tonic-gate 	}
18120Sstevel@tonic-gate 	*circular = devi->devi_circular;
18130Sstevel@tonic-gate 	mutex_exit(&devi->devi_lock);
18140Sstevel@tonic-gate }
18150Sstevel@tonic-gate 
18160Sstevel@tonic-gate /*
18170Sstevel@tonic-gate  * Release ndi_devi_enter or successful ndi_devi_tryenter.
18180Sstevel@tonic-gate  */
18190Sstevel@tonic-gate void
18200Sstevel@tonic-gate ndi_devi_exit(dev_info_t *dip, int circular)
18210Sstevel@tonic-gate {
18222155Scth 	struct dev_info	*devi = DEVI(dip);
18232155Scth 	struct dev_info	*vdevi;
18240Sstevel@tonic-gate 	ASSERT(dip != NULL);
18250Sstevel@tonic-gate 
18260Sstevel@tonic-gate 	if (panicstr)
18270Sstevel@tonic-gate 		return;
18280Sstevel@tonic-gate 
18290Sstevel@tonic-gate 	mutex_enter(&(devi->devi_lock));
18300Sstevel@tonic-gate 	if (circular != 0) {
18310Sstevel@tonic-gate 		devi->devi_circular--;
18320Sstevel@tonic-gate 	} else {
18330Sstevel@tonic-gate 		devi->devi_flags &= ~DEVI_BUSY;
18340Sstevel@tonic-gate 		ASSERT(devi->devi_busy_thread == curthread);
18350Sstevel@tonic-gate 		devi->devi_busy_thread = NULL;
18360Sstevel@tonic-gate 		cv_broadcast(&(devi->devi_cv));
18370Sstevel@tonic-gate 	}
18380Sstevel@tonic-gate 	mutex_exit(&(devi->devi_lock));
18392155Scth 
18402155Scth 	/*
18412155Scth 	 * For pHCI exit we issue a broadcast to vHCI for ndi_devi_config_one()
18422155Scth 	 * doing cv_wait on vHCI.
18432155Scth 	 */
18442155Scth 	if (MDI_PHCI(dip)) {
18452155Scth 		vdevi = DEVI(mdi_devi_get_vdip(dip));
18462155Scth 		if (vdevi) {
18472155Scth 			mutex_enter(&(vdevi->devi_lock));
18482155Scth 			if (vdevi->devi_flags & DEVI_PHCI_SIGNALS_VHCI) {
18492155Scth 				vdevi->devi_flags &= ~DEVI_PHCI_SIGNALS_VHCI;
18502155Scth 				cv_broadcast(&(vdevi->devi_cv));
18512155Scth 			}
18522155Scth 			mutex_exit(&(vdevi->devi_lock));
18532155Scth 		}
18542155Scth 	}
18552155Scth }
18562155Scth 
18572155Scth /*
18582155Scth  * Release ndi_devi_enter and wait for possibility of new children, avoiding
18592155Scth  * possibility of missing broadcast before getting to cv_timedwait().
18602155Scth  */
18612155Scth static void
18622155Scth ndi_devi_exit_and_wait(dev_info_t *dip, int circular, clock_t end_time)
18632155Scth {
18642155Scth 	struct dev_info	*devi = DEVI(dip);
18652155Scth 	ASSERT(dip != NULL);
18662155Scth 
18672155Scth 	if (panicstr)
18682155Scth 		return;
18692155Scth 
18702155Scth 	/*
18712155Scth 	 * We are called to wait for of a new child, and new child can
18722155Scth 	 * only be added if circular is zero.
18732155Scth 	 */
18742155Scth 	ASSERT(circular == 0);
18752155Scth 
18762155Scth 	/* like ndi_devi_exit with circular of zero */
18772155Scth 	mutex_enter(&(devi->devi_lock));
18782155Scth 	devi->devi_flags &= ~DEVI_BUSY;
18792155Scth 	ASSERT(devi->devi_busy_thread == curthread);
18802155Scth 	devi->devi_busy_thread = NULL;
18812155Scth 	cv_broadcast(&(devi->devi_cv));
18822155Scth 
18832155Scth 	/* now wait for new children while still holding devi_lock */
18842155Scth 	(void) cv_timedwait(&devi->devi_cv, &(devi->devi_lock), end_time);
18852155Scth 	mutex_exit(&(devi->devi_lock));
18860Sstevel@tonic-gate }
18870Sstevel@tonic-gate 
18880Sstevel@tonic-gate /*
18890Sstevel@tonic-gate  * Attempt to single thread entry into devinfo node for modifying its children.
18900Sstevel@tonic-gate  */
18910Sstevel@tonic-gate int
18920Sstevel@tonic-gate ndi_devi_tryenter(dev_info_t *dip, int *circular)
18930Sstevel@tonic-gate {
18940Sstevel@tonic-gate 	int rval = 1;		   /* assume we enter */
18950Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
18960Sstevel@tonic-gate 	ASSERT(dip != NULL);
18970Sstevel@tonic-gate 
18980Sstevel@tonic-gate 	mutex_enter(&devi->devi_lock);
18990Sstevel@tonic-gate 	if (devi->devi_busy_thread == (void *)curthread) {
19000Sstevel@tonic-gate 		devi->devi_circular++;
19010Sstevel@tonic-gate 	} else {
19020Sstevel@tonic-gate 		if (!DEVI_BUSY_CHANGING(devi)) {
19030Sstevel@tonic-gate 			devi->devi_flags |= DEVI_BUSY;
19040Sstevel@tonic-gate 			devi->devi_busy_thread = (void *)curthread;
19050Sstevel@tonic-gate 		} else {
19060Sstevel@tonic-gate 			rval = 0;	/* devi is busy */
19070Sstevel@tonic-gate 		}
19080Sstevel@tonic-gate 	}
19090Sstevel@tonic-gate 	*circular = devi->devi_circular;
19100Sstevel@tonic-gate 	mutex_exit(&devi->devi_lock);
19110Sstevel@tonic-gate 	return (rval);
19120Sstevel@tonic-gate }
19130Sstevel@tonic-gate 
19140Sstevel@tonic-gate /*
19150Sstevel@tonic-gate  * Allocate and initialize a new dev_info structure.
19160Sstevel@tonic-gate  *
19170Sstevel@tonic-gate  * This routine may be called at interrupt time by a nexus in
19180Sstevel@tonic-gate  * response to a hotplug event, therefore memory allocations are
19190Sstevel@tonic-gate  * not allowed to sleep.
19200Sstevel@tonic-gate  */
19210Sstevel@tonic-gate int
1922789Sahrens ndi_devi_alloc(dev_info_t *parent, char *node_name, pnode_t nodeid,
19230Sstevel@tonic-gate     dev_info_t **ret_dip)
19240Sstevel@tonic-gate {
19250Sstevel@tonic-gate 	ASSERT(node_name != NULL);
19260Sstevel@tonic-gate 	ASSERT(ret_dip != NULL);
19270Sstevel@tonic-gate 
19280Sstevel@tonic-gate 	*ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL,
19290Sstevel@tonic-gate 	    KM_NOSLEEP);
19300Sstevel@tonic-gate 	if (*ret_dip == NULL) {
19310Sstevel@tonic-gate 		return (NDI_NOMEM);
19320Sstevel@tonic-gate 	}
19330Sstevel@tonic-gate 
19340Sstevel@tonic-gate 	return (NDI_SUCCESS);
19350Sstevel@tonic-gate }
19360Sstevel@tonic-gate 
19370Sstevel@tonic-gate /*
19380Sstevel@tonic-gate  * Allocate and initialize a new dev_info structure
19390Sstevel@tonic-gate  * This routine may sleep and should not be called at interrupt time
19400Sstevel@tonic-gate  */
19410Sstevel@tonic-gate void
1942789Sahrens ndi_devi_alloc_sleep(dev_info_t *parent, char *node_name, pnode_t nodeid,
19430Sstevel@tonic-gate     dev_info_t **ret_dip)
19440Sstevel@tonic-gate {
19450Sstevel@tonic-gate 	ASSERT(node_name != NULL);
19460Sstevel@tonic-gate 	ASSERT(ret_dip != NULL);
19470Sstevel@tonic-gate 
19480Sstevel@tonic-gate 	*ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL,
19490Sstevel@tonic-gate 	    KM_SLEEP);
19500Sstevel@tonic-gate 	ASSERT(*ret_dip);
19510Sstevel@tonic-gate }
19520Sstevel@tonic-gate 
19530Sstevel@tonic-gate /*
19540Sstevel@tonic-gate  * Remove an initialized (but not yet attached) dev_info
19550Sstevel@tonic-gate  * node from it's parent.
19560Sstevel@tonic-gate  */
19570Sstevel@tonic-gate int
19580Sstevel@tonic-gate ndi_devi_free(dev_info_t *dip)
19590Sstevel@tonic-gate {
19600Sstevel@tonic-gate 	ASSERT(dip != NULL);
19610Sstevel@tonic-gate 
19620Sstevel@tonic-gate 	if (i_ddi_node_state(dip) >= DS_INITIALIZED)
19630Sstevel@tonic-gate 		return (DDI_FAILURE);
19640Sstevel@tonic-gate 
19650Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_free: %s%d (%p)\n",
19660Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
19670Sstevel@tonic-gate 
19680Sstevel@tonic-gate 	(void) ddi_remove_child(dip, 0);
19690Sstevel@tonic-gate 
19700Sstevel@tonic-gate 	return (NDI_SUCCESS);
19710Sstevel@tonic-gate }
19720Sstevel@tonic-gate 
19730Sstevel@tonic-gate /*
19740Sstevel@tonic-gate  * ndi_devi_bind_driver() binds a driver to a given device. If it fails
19750Sstevel@tonic-gate  * to bind the driver, it returns an appropriate error back. Some drivers
19760Sstevel@tonic-gate  * may want to know if the actually failed to bind.
19770Sstevel@tonic-gate  */
19780Sstevel@tonic-gate int
19790Sstevel@tonic-gate ndi_devi_bind_driver(dev_info_t *dip, uint_t flags)
19800Sstevel@tonic-gate {
19810Sstevel@tonic-gate 	int ret = NDI_FAILURE;
19820Sstevel@tonic-gate 	int circ;
19830Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
19840Sstevel@tonic-gate 	ASSERT(pdip);
19850Sstevel@tonic-gate 
19860Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
19870Sstevel@tonic-gate 	    "ndi_devi_bind_driver: %s%d (%p) flags: %x\n",
19880Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
19890Sstevel@tonic-gate 
19900Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
19910Sstevel@tonic-gate 	if (i_ndi_config_node(dip, DS_BOUND, flags) == DDI_SUCCESS)
19920Sstevel@tonic-gate 		ret = NDI_SUCCESS;
19930Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
19940Sstevel@tonic-gate 
19950Sstevel@tonic-gate 	return (ret);
19960Sstevel@tonic-gate }
19970Sstevel@tonic-gate 
19980Sstevel@tonic-gate /*
19990Sstevel@tonic-gate  * ndi_devi_unbind_driver: unbind the dip
20000Sstevel@tonic-gate  */
20010Sstevel@tonic-gate static int
20020Sstevel@tonic-gate ndi_devi_unbind_driver(dev_info_t *dip)
20030Sstevel@tonic-gate {
20040Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
20050Sstevel@tonic-gate 
20060Sstevel@tonic-gate 	return (i_ndi_unconfig_node(dip, DS_LINKED, 0));
20070Sstevel@tonic-gate }
20080Sstevel@tonic-gate 
20090Sstevel@tonic-gate /*
20100Sstevel@tonic-gate  * Misc. help routines called by framework only
20110Sstevel@tonic-gate  */
20120Sstevel@tonic-gate 
20130Sstevel@tonic-gate /*
20140Sstevel@tonic-gate  * Get the state of node
20150Sstevel@tonic-gate  */
20160Sstevel@tonic-gate ddi_node_state_t
20170Sstevel@tonic-gate i_ddi_node_state(dev_info_t *dip)
20180Sstevel@tonic-gate {
20190Sstevel@tonic-gate 	return (DEVI(dip)->devi_node_state);
20200Sstevel@tonic-gate }
20210Sstevel@tonic-gate 
20220Sstevel@tonic-gate /*
20230Sstevel@tonic-gate  * Set the state of node
20240Sstevel@tonic-gate  */
20250Sstevel@tonic-gate void
20260Sstevel@tonic-gate i_ddi_set_node_state(dev_info_t *dip, ddi_node_state_t state)
20270Sstevel@tonic-gate {
20280Sstevel@tonic-gate 	DEVI(dip)->devi_node_state = state;
20290Sstevel@tonic-gate 	membar_enter();			/* make sure stores are flushed */
20300Sstevel@tonic-gate }
20310Sstevel@tonic-gate 
20320Sstevel@tonic-gate /*
20331333Scth  * Determine if node is attached. The implementation accommodates transient
20341333Scth  * DS_READY->DS_ATTACHED->DS_READY state changes.  Outside this file, this
20351333Scth  * function should be instead of i_ddi_node_state() DS_ATTACHED/DS_READY
20361333Scth  * state checks.
20371333Scth  */
20381333Scth int
20391333Scth i_ddi_devi_attached(dev_info_t *dip)
20401333Scth {
20411333Scth 	return (DEVI(dip)->devi_node_state >= DS_ATTACHED);
20421333Scth }
20431333Scth 
20441333Scth /*
20450Sstevel@tonic-gate  * Common function for finding a node in a sibling list given name and addr.
20460Sstevel@tonic-gate  *
20470Sstevel@tonic-gate  * By default, name is matched with devi_node_name. The following
20480Sstevel@tonic-gate  * alternative match strategies are supported:
20490Sstevel@tonic-gate  *
20504145Scth  *	FIND_NODE_BY_NODENAME: Match on node name - typical use.
20514145Scth  *	FIND_NODE_BY_DRIVER: A match on driver name bound to node is conducted.
20520Sstevel@tonic-gate  *		This support is used for support of OBP generic names and
20534145Scth  *		for the conversion from driver names to generic names. When
20540Sstevel@tonic-gate  *		more consistency in the generic name environment is achieved
20550Sstevel@tonic-gate  *		(and not needed for upgrade) this support can be removed.
20564145Scth  *	FIND_NODE_BY_ADDR: Match on just the addr.
20574145Scth  *		This support is only used/needed during boot to match
20584145Scth  *		a node bound via a path-based driver alias.
20590Sstevel@tonic-gate  *
20600Sstevel@tonic-gate  * If a child is not named (dev_addr == NULL), there are three
20610Sstevel@tonic-gate  * possible actions:
20620Sstevel@tonic-gate  *
20630Sstevel@tonic-gate  *	(1) skip it
20640Sstevel@tonic-gate  *	(2) FIND_ADDR_BY_INIT: bring child to DS_INITIALIZED state
20650Sstevel@tonic-gate  *	(3) FIND_ADDR_BY_CALLBACK: use a caller-supplied callback function
20660Sstevel@tonic-gate  */
20674145Scth #define	FIND_NODE_BY_NODENAME	0x01
20684145Scth #define	FIND_NODE_BY_DRIVER	0x02
20694145Scth #define	FIND_NODE_BY_ADDR	0x04
20700Sstevel@tonic-gate #define	FIND_ADDR_BY_INIT	0x10
20710Sstevel@tonic-gate #define	FIND_ADDR_BY_CALLBACK	0x20
20720Sstevel@tonic-gate 
20730Sstevel@tonic-gate static dev_info_t *
20740Sstevel@tonic-gate find_sibling(dev_info_t *head, char *cname, char *caddr, uint_t flag,
20750Sstevel@tonic-gate     int (*callback)(dev_info_t *, char *, int))
20760Sstevel@tonic-gate {
20770Sstevel@tonic-gate 	dev_info_t	*dip;
20780Sstevel@tonic-gate 	char		*addr, *buf;
20790Sstevel@tonic-gate 	major_t		major;
20804145Scth 	uint_t		by;
20814145Scth 
20824145Scth 	/* only one way to find a node */
20834145Scth 	by = flag &
20844145Scth 	    (FIND_NODE_BY_DRIVER | FIND_NODE_BY_NODENAME | FIND_NODE_BY_ADDR);
20854145Scth 	ASSERT(by && BIT_ONLYONESET(by));
20860Sstevel@tonic-gate 
20870Sstevel@tonic-gate 	/* only one way to name a node */
20880Sstevel@tonic-gate 	ASSERT(((flag & FIND_ADDR_BY_INIT) == 0) ||
20890Sstevel@tonic-gate 	    ((flag & FIND_ADDR_BY_CALLBACK) == 0));
20900Sstevel@tonic-gate 
20914145Scth 	if (by == FIND_NODE_BY_DRIVER) {
20920Sstevel@tonic-gate 		major = ddi_name_to_major(cname);
20937009Scth 		if (major == DDI_MAJOR_T_NONE)
20940Sstevel@tonic-gate 			return (NULL);
20950Sstevel@tonic-gate 	}
20960Sstevel@tonic-gate 
20970Sstevel@tonic-gate 	/* preallocate buffer of naming node by callback */
20980Sstevel@tonic-gate 	if (flag & FIND_ADDR_BY_CALLBACK)
20990Sstevel@tonic-gate 		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
21000Sstevel@tonic-gate 
21010Sstevel@tonic-gate 	/*
21020Sstevel@tonic-gate 	 * Walk the child list to find a match
21030Sstevel@tonic-gate 	 */
21040Sstevel@tonic-gate 
21050Sstevel@tonic-gate 	for (dip = head; dip; dip = ddi_get_next_sibling(dip)) {
21064145Scth 		if (by == FIND_NODE_BY_NODENAME) {
21074145Scth 			/* match node name */
21084145Scth 			if (strcmp(cname, DEVI(dip)->devi_node_name) != 0)
21094145Scth 				continue;
21104145Scth 		} else if (by == FIND_NODE_BY_DRIVER) {
21110Sstevel@tonic-gate 			/* match driver major */
21120Sstevel@tonic-gate 			if (DEVI(dip)->devi_major != major)
21130Sstevel@tonic-gate 				continue;
21140Sstevel@tonic-gate 		}
21150Sstevel@tonic-gate 
21160Sstevel@tonic-gate 		if ((addr = DEVI(dip)->devi_addr) == NULL) {
21170Sstevel@tonic-gate 			/* name the child based on the flag */
21180Sstevel@tonic-gate 			if (flag & FIND_ADDR_BY_INIT) {
21190Sstevel@tonic-gate 				if (ddi_initchild(ddi_get_parent(dip), dip)
21200Sstevel@tonic-gate 				    != DDI_SUCCESS)
21210Sstevel@tonic-gate 					continue;
21220Sstevel@tonic-gate 				addr = DEVI(dip)->devi_addr;
21230Sstevel@tonic-gate 			} else if (flag & FIND_ADDR_BY_CALLBACK) {
21240Sstevel@tonic-gate 				if ((callback == NULL) || (callback(
21250Sstevel@tonic-gate 				    dip, buf, MAXNAMELEN) != DDI_SUCCESS))
21260Sstevel@tonic-gate 					continue;
21270Sstevel@tonic-gate 				addr = buf;
21280Sstevel@tonic-gate 			} else {
21290Sstevel@tonic-gate 				continue;	/* skip */
21300Sstevel@tonic-gate 			}
21310Sstevel@tonic-gate 		}
21320Sstevel@tonic-gate 
21330Sstevel@tonic-gate 		/* match addr */
21340Sstevel@tonic-gate 		ASSERT(addr != NULL);
21350Sstevel@tonic-gate 		if (strcmp(caddr, addr) == 0)
21360Sstevel@tonic-gate 			break;	/* node found */
21370Sstevel@tonic-gate 
21380Sstevel@tonic-gate 	}
21390Sstevel@tonic-gate 	if (flag & FIND_ADDR_BY_CALLBACK)
21400Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
21410Sstevel@tonic-gate 	return (dip);
21420Sstevel@tonic-gate }
21430Sstevel@tonic-gate 
21440Sstevel@tonic-gate /*
21450Sstevel@tonic-gate  * Find child of pdip with name: cname@caddr
21460Sstevel@tonic-gate  * Called by init_node() to look for duplicate nodes
21470Sstevel@tonic-gate  */
21480Sstevel@tonic-gate static dev_info_t *
21490Sstevel@tonic-gate find_duplicate_child(dev_info_t *pdip, dev_info_t *dip)
21500Sstevel@tonic-gate {
21510Sstevel@tonic-gate 	dev_info_t *dup;
21520Sstevel@tonic-gate 	char *cname = DEVI(dip)->devi_node_name;
21530Sstevel@tonic-gate 	char *caddr = DEVI(dip)->devi_addr;
21540Sstevel@tonic-gate 
21550Sstevel@tonic-gate 	/* search nodes before dip */
21564145Scth 	dup = find_sibling(ddi_get_child(pdip), cname, caddr,
21574145Scth 	    FIND_NODE_BY_NODENAME, NULL);
21580Sstevel@tonic-gate 	if (dup != dip)
21590Sstevel@tonic-gate 		return (dup);
21600Sstevel@tonic-gate 
21610Sstevel@tonic-gate 	/*
21620Sstevel@tonic-gate 	 * search nodes after dip; normally this is not needed,
21630Sstevel@tonic-gate 	 */
21640Sstevel@tonic-gate 	return (find_sibling(ddi_get_next_sibling(dip), cname, caddr,
21654145Scth 	    FIND_NODE_BY_NODENAME, NULL));
21660Sstevel@tonic-gate }
21670Sstevel@tonic-gate 
21680Sstevel@tonic-gate /*
21690Sstevel@tonic-gate  * Find a child of a given name and address, using a callback to name
21700Sstevel@tonic-gate  * unnamed children. cname is the binding name.
21710Sstevel@tonic-gate  */
21720Sstevel@tonic-gate static dev_info_t *
21730Sstevel@tonic-gate find_child_by_callback(dev_info_t *pdip, char *cname, char *caddr,
21740Sstevel@tonic-gate     int (*name_node)(dev_info_t *, char *, int))
21750Sstevel@tonic-gate {
21760Sstevel@tonic-gate 	return (find_sibling(ddi_get_child(pdip), cname, caddr,
21774145Scth 	    FIND_NODE_BY_DRIVER|FIND_ADDR_BY_CALLBACK, name_node));
21780Sstevel@tonic-gate }
21790Sstevel@tonic-gate 
21800Sstevel@tonic-gate /*
21810Sstevel@tonic-gate  * Find a child of a given name and address, invoking initchild to name
21820Sstevel@tonic-gate  * unnamed children. cname is the node name.
21830Sstevel@tonic-gate  */
21840Sstevel@tonic-gate static dev_info_t *
21850Sstevel@tonic-gate find_child_by_name(dev_info_t *pdip, char *cname, char *caddr)
21860Sstevel@tonic-gate {
21870Sstevel@tonic-gate 	dev_info_t	*dip;
21880Sstevel@tonic-gate 
21894145Scth 	/* attempt search without changing state of preceding siblings */
21904145Scth 	dip = find_sibling(ddi_get_child(pdip), cname, caddr,
21914145Scth 	    FIND_NODE_BY_NODENAME, NULL);
21920Sstevel@tonic-gate 	if (dip)
21930Sstevel@tonic-gate 		return (dip);
21940Sstevel@tonic-gate 
21950Sstevel@tonic-gate 	return (find_sibling(ddi_get_child(pdip), cname, caddr,
21964145Scth 	    FIND_NODE_BY_NODENAME|FIND_ADDR_BY_INIT, NULL));
21970Sstevel@tonic-gate }
21980Sstevel@tonic-gate 
21990Sstevel@tonic-gate /*
22000Sstevel@tonic-gate  * Find a child of a given name and address, invoking initchild to name
22010Sstevel@tonic-gate  * unnamed children. cname is the node name.
22020Sstevel@tonic-gate  */
22030Sstevel@tonic-gate static dev_info_t *
22040Sstevel@tonic-gate find_child_by_driver(dev_info_t *pdip, char *cname, char *caddr)
22050Sstevel@tonic-gate {
22060Sstevel@tonic-gate 	dev_info_t	*dip;
22070Sstevel@tonic-gate 
22084145Scth 	/* attempt search without changing state of preceding siblings */
22090Sstevel@tonic-gate 	dip = find_sibling(ddi_get_child(pdip), cname, caddr,
22104145Scth 	    FIND_NODE_BY_DRIVER, NULL);
22110Sstevel@tonic-gate 	if (dip)
22120Sstevel@tonic-gate 		return (dip);
22130Sstevel@tonic-gate 
22140Sstevel@tonic-gate 	return (find_sibling(ddi_get_child(pdip), cname, caddr,
22154145Scth 	    FIND_NODE_BY_DRIVER|FIND_ADDR_BY_INIT, NULL));
22164145Scth }
22174145Scth 
22184145Scth /*
22194145Scth  * Find a child of a given address, invoking initchild to name
22204145Scth  * unnamed children. cname is the node name.
22214145Scth  *
22224145Scth  * NOTE: This function is only used during boot. One would hope that
22234145Scth  * unique sibling unit-addresses on hardware branches of the tree would
22244145Scth  * be a requirement to avoid two drivers trying to control the same
22254145Scth  * piece of hardware. Unfortunately there are some cases where this
22264145Scth  * situation exists (/ssm@0,0/pci@1c,700000 /ssm@0,0/sghsc@1c,700000).
22274145Scth  * Until unit-address uniqueness of siblings is guaranteed, use of this
22284145Scth  * interface for purposes other than boot should be avoided.
22294145Scth  */
22304145Scth static dev_info_t *
22314145Scth find_child_by_addr(dev_info_t *pdip, char *caddr)
22324145Scth {
22334145Scth 	dev_info_t	*dip;
22344145Scth 
22354540Scth 	/* return NULL if called without a unit-address */
22364540Scth 	if ((caddr == NULL) || (*caddr == '\0'))
22374540Scth 		return (NULL);
22384540Scth 
22394145Scth 	/* attempt search without changing state of preceding siblings */
22404145Scth 	dip = find_sibling(ddi_get_child(pdip), NULL, caddr,
22414145Scth 	    FIND_NODE_BY_ADDR, NULL);
22424145Scth 	if (dip)
22434145Scth 		return (dip);
22444145Scth 
22454145Scth 	return (find_sibling(ddi_get_child(pdip), NULL, caddr,
22464145Scth 	    FIND_NODE_BY_ADDR|FIND_ADDR_BY_INIT, NULL));
22470Sstevel@tonic-gate }
22480Sstevel@tonic-gate 
22490Sstevel@tonic-gate /*
22500Sstevel@tonic-gate  * Deleting a property list. Take care, since some property structures
22510Sstevel@tonic-gate  * may not be fully built.
22520Sstevel@tonic-gate  */
22530Sstevel@tonic-gate void
22540Sstevel@tonic-gate i_ddi_prop_list_delete(ddi_prop_t *prop)
22550Sstevel@tonic-gate {
22560Sstevel@tonic-gate 	while (prop) {
22570Sstevel@tonic-gate 		ddi_prop_t *next = prop->prop_next;
22580Sstevel@tonic-gate 		if (prop->prop_name)
22590Sstevel@tonic-gate 			kmem_free(prop->prop_name, strlen(prop->prop_name) + 1);
22600Sstevel@tonic-gate 		if ((prop->prop_len != 0) && prop->prop_val)
22610Sstevel@tonic-gate 			kmem_free(prop->prop_val, prop->prop_len);
22620Sstevel@tonic-gate 		kmem_free(prop, sizeof (struct ddi_prop));
22630Sstevel@tonic-gate 		prop = next;
22640Sstevel@tonic-gate 	}
22650Sstevel@tonic-gate }
22660Sstevel@tonic-gate 
22670Sstevel@tonic-gate /*
22680Sstevel@tonic-gate  * Duplicate property list
22690Sstevel@tonic-gate  */
22700Sstevel@tonic-gate ddi_prop_t *
22710Sstevel@tonic-gate i_ddi_prop_list_dup(ddi_prop_t *prop, uint_t flag)
22720Sstevel@tonic-gate {
22730Sstevel@tonic-gate 	ddi_prop_t *result, *prev, *copy;
22740Sstevel@tonic-gate 
22750Sstevel@tonic-gate 	if (prop == NULL)
22760Sstevel@tonic-gate 		return (NULL);
22770Sstevel@tonic-gate 
22780Sstevel@tonic-gate 	result = prev = NULL;
22790Sstevel@tonic-gate 	for (; prop != NULL; prop = prop->prop_next) {
22800Sstevel@tonic-gate 		ASSERT(prop->prop_name != NULL);
22810Sstevel@tonic-gate 		copy = kmem_zalloc(sizeof (struct ddi_prop), flag);
22820Sstevel@tonic-gate 		if (copy == NULL)
22830Sstevel@tonic-gate 			goto fail;
22840Sstevel@tonic-gate 
22850Sstevel@tonic-gate 		copy->prop_dev = prop->prop_dev;
22860Sstevel@tonic-gate 		copy->prop_flags = prop->prop_flags;
22870Sstevel@tonic-gate 		copy->prop_name = i_ddi_strdup(prop->prop_name, flag);
22880Sstevel@tonic-gate 		if (copy->prop_name == NULL)
22890Sstevel@tonic-gate 			goto fail;
22900Sstevel@tonic-gate 
22910Sstevel@tonic-gate 		if ((copy->prop_len = prop->prop_len) != 0) {
22920Sstevel@tonic-gate 			copy->prop_val = kmem_zalloc(prop->prop_len, flag);
22930Sstevel@tonic-gate 			if (copy->prop_val == NULL)
22940Sstevel@tonic-gate 				goto fail;
22950Sstevel@tonic-gate 
22960Sstevel@tonic-gate 			bcopy(prop->prop_val, copy->prop_val, prop->prop_len);
22970Sstevel@tonic-gate 		}
22980Sstevel@tonic-gate 
22990Sstevel@tonic-gate 		if (prev == NULL)
23000Sstevel@tonic-gate 			result = prev = copy;
23010Sstevel@tonic-gate 		else
23020Sstevel@tonic-gate 			prev->prop_next = copy;
23030Sstevel@tonic-gate 		prev = copy;
23040Sstevel@tonic-gate 	}
23050Sstevel@tonic-gate 	return (result);
23060Sstevel@tonic-gate 
23070Sstevel@tonic-gate fail:
23080Sstevel@tonic-gate 	i_ddi_prop_list_delete(result);
23090Sstevel@tonic-gate 	return (NULL);
23100Sstevel@tonic-gate }
23110Sstevel@tonic-gate 
23120Sstevel@tonic-gate /*
23130Sstevel@tonic-gate  * Create a reference property list, currently used only for
23140Sstevel@tonic-gate  * driver global properties. Created with ref count of 1.
23150Sstevel@tonic-gate  */
23160Sstevel@tonic-gate ddi_prop_list_t *
23170Sstevel@tonic-gate i_ddi_prop_list_create(ddi_prop_t *props)
23180Sstevel@tonic-gate {
23190Sstevel@tonic-gate 	ddi_prop_list_t *list = kmem_alloc(sizeof (*list), KM_SLEEP);
23200Sstevel@tonic-gate 	list->prop_list = props;
23210Sstevel@tonic-gate 	list->prop_ref = 1;
23220Sstevel@tonic-gate 	return (list);
23230Sstevel@tonic-gate }
23240Sstevel@tonic-gate 
23250Sstevel@tonic-gate /*
23260Sstevel@tonic-gate  * Increment/decrement reference count. The reference is
23270Sstevel@tonic-gate  * protected by dn_lock. The only interfaces modifying
23280Sstevel@tonic-gate  * dn_global_prop_ptr is in impl_make[free]_parlist().
23290Sstevel@tonic-gate  */
23300Sstevel@tonic-gate void
23310Sstevel@tonic-gate i_ddi_prop_list_hold(ddi_prop_list_t *prop_list, struct devnames *dnp)
23320Sstevel@tonic-gate {
23330Sstevel@tonic-gate 	ASSERT(prop_list->prop_ref >= 0);
23340Sstevel@tonic-gate 	ASSERT(mutex_owned(&dnp->dn_lock));
23350Sstevel@tonic-gate 	prop_list->prop_ref++;
23360Sstevel@tonic-gate }
23370Sstevel@tonic-gate 
23380Sstevel@tonic-gate void
23390Sstevel@tonic-gate i_ddi_prop_list_rele(ddi_prop_list_t *prop_list, struct devnames *dnp)
23400Sstevel@tonic-gate {
23410Sstevel@tonic-gate 	ASSERT(prop_list->prop_ref > 0);
23420Sstevel@tonic-gate 	ASSERT(mutex_owned(&dnp->dn_lock));
23430Sstevel@tonic-gate 	prop_list->prop_ref--;
23440Sstevel@tonic-gate 
23450Sstevel@tonic-gate 	if (prop_list->prop_ref == 0) {
23460Sstevel@tonic-gate 		i_ddi_prop_list_delete(prop_list->prop_list);
23470Sstevel@tonic-gate 		kmem_free(prop_list, sizeof (*prop_list));
23480Sstevel@tonic-gate 	}
23490Sstevel@tonic-gate }
23500Sstevel@tonic-gate 
23510Sstevel@tonic-gate /*
23520Sstevel@tonic-gate  * Free table of classes by drivers
23530Sstevel@tonic-gate  */
23540Sstevel@tonic-gate void
23550Sstevel@tonic-gate i_ddi_free_exported_classes(char **classes, int n)
23560Sstevel@tonic-gate {
23570Sstevel@tonic-gate 	if ((n == 0) || (classes == NULL))
23580Sstevel@tonic-gate 		return;
23590Sstevel@tonic-gate 
23600Sstevel@tonic-gate 	kmem_free(classes, n * sizeof (char *));
23610Sstevel@tonic-gate }
23620Sstevel@tonic-gate 
23630Sstevel@tonic-gate /*
23640Sstevel@tonic-gate  * Get all classes exported by dip
23650Sstevel@tonic-gate  */
23660Sstevel@tonic-gate int
23670Sstevel@tonic-gate i_ddi_get_exported_classes(dev_info_t *dip, char ***classes)
23680Sstevel@tonic-gate {
23690Sstevel@tonic-gate 	extern void lock_hw_class_list();
23700Sstevel@tonic-gate 	extern void unlock_hw_class_list();
23710Sstevel@tonic-gate 	extern int get_class(const char *, char **);
23720Sstevel@tonic-gate 
23730Sstevel@tonic-gate 	static char *rootclass = "root";
23740Sstevel@tonic-gate 	int n = 0, nclass = 0;
23750Sstevel@tonic-gate 	char **buf;
23760Sstevel@tonic-gate 
23770Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) >= DS_BOUND);
23780Sstevel@tonic-gate 
23790Sstevel@tonic-gate 	if (dip == ddi_root_node())	/* rootnode exports class "root" */
23800Sstevel@tonic-gate 		nclass = 1;
23810Sstevel@tonic-gate 	lock_hw_class_list();
23820Sstevel@tonic-gate 	nclass += get_class(ddi_driver_name(dip), NULL);
23830Sstevel@tonic-gate 	if (nclass == 0) {
23840Sstevel@tonic-gate 		unlock_hw_class_list();
23850Sstevel@tonic-gate 		return (0);		/* no class exported */
23860Sstevel@tonic-gate 	}
23870Sstevel@tonic-gate 
23880Sstevel@tonic-gate 	*classes = buf = kmem_alloc(nclass * sizeof (char *), KM_SLEEP);
23890Sstevel@tonic-gate 	if (dip == ddi_root_node()) {
23900Sstevel@tonic-gate 		*buf++ = rootclass;
23910Sstevel@tonic-gate 		n = 1;
23920Sstevel@tonic-gate 	}
23930Sstevel@tonic-gate 	n += get_class(ddi_driver_name(dip), buf);
23940Sstevel@tonic-gate 	unlock_hw_class_list();
23950Sstevel@tonic-gate 
23960Sstevel@tonic-gate 	ASSERT(n == nclass);    /* make sure buf wasn't overrun */
23970Sstevel@tonic-gate 	return (nclass);
23980Sstevel@tonic-gate }
23990Sstevel@tonic-gate 
24000Sstevel@tonic-gate /*
24010Sstevel@tonic-gate  * Helper functions, returns NULL if no memory.
24020Sstevel@tonic-gate  */
24030Sstevel@tonic-gate char *
24040Sstevel@tonic-gate i_ddi_strdup(char *str, uint_t flag)
24050Sstevel@tonic-gate {
24060Sstevel@tonic-gate 	char *copy;
24070Sstevel@tonic-gate 
24080Sstevel@tonic-gate 	if (str == NULL)
24090Sstevel@tonic-gate 		return (NULL);
24100Sstevel@tonic-gate 
24110Sstevel@tonic-gate 	copy = kmem_alloc(strlen(str) + 1, flag);
24120Sstevel@tonic-gate 	if (copy == NULL)
24130Sstevel@tonic-gate 		return (NULL);
24140Sstevel@tonic-gate 
24150Sstevel@tonic-gate 	(void) strcpy(copy, str);
24160Sstevel@tonic-gate 	return (copy);
24170Sstevel@tonic-gate }
24180Sstevel@tonic-gate 
24190Sstevel@tonic-gate /*
24200Sstevel@tonic-gate  * Load driver.conf file for major. Load all if major == -1.
24210Sstevel@tonic-gate  *
24220Sstevel@tonic-gate  * This is called
24230Sstevel@tonic-gate  * - early in boot after devnames array is initialized
24240Sstevel@tonic-gate  * - from vfs code when certain file systems are mounted
24250Sstevel@tonic-gate  * - from add_drv when a new driver is added
24260Sstevel@tonic-gate  */
24270Sstevel@tonic-gate int
24280Sstevel@tonic-gate i_ddi_load_drvconf(major_t major)
24290Sstevel@tonic-gate {
24300Sstevel@tonic-gate 	extern int modrootloaded;
24310Sstevel@tonic-gate 
24320Sstevel@tonic-gate 	major_t low, high, m;
24330Sstevel@tonic-gate 
24347009Scth 	if (major == DDI_MAJOR_T_NONE) {
24350Sstevel@tonic-gate 		low = 0;
24360Sstevel@tonic-gate 		high = devcnt - 1;
24370Sstevel@tonic-gate 	} else {
24380Sstevel@tonic-gate 		if (major >= devcnt)
24390Sstevel@tonic-gate 			return (EINVAL);
24400Sstevel@tonic-gate 		low = high = major;
24410Sstevel@tonic-gate 	}
24420Sstevel@tonic-gate 
24430Sstevel@tonic-gate 	for (m = low; m <= high; m++) {
24440Sstevel@tonic-gate 		struct devnames *dnp = &devnamesp[m];
24450Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
24460Sstevel@tonic-gate 		dnp->dn_flags &= ~DN_DRIVER_HELD;
24470Sstevel@tonic-gate 		(void) impl_make_parlist(m);
24480Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
24490Sstevel@tonic-gate 	}
24500Sstevel@tonic-gate 
24510Sstevel@tonic-gate 	if (modrootloaded) {
24520Sstevel@tonic-gate 		ddi_walk_devs(ddi_root_node(), reset_nexus_flags,
24530Sstevel@tonic-gate 		    (void *)(uintptr_t)major);
24540Sstevel@tonic-gate 	}
24550Sstevel@tonic-gate 
24560Sstevel@tonic-gate 	/* build dn_list from old entries in path_to_inst */
24570Sstevel@tonic-gate 	e_ddi_unorphan_instance_nos();
24580Sstevel@tonic-gate 	return (0);
24590Sstevel@tonic-gate }
24600Sstevel@tonic-gate 
24610Sstevel@tonic-gate /*
24620Sstevel@tonic-gate  * Unload a specific driver.conf.
24630Sstevel@tonic-gate  * Don't support unload all because it doesn't make any sense
24640Sstevel@tonic-gate  */
24650Sstevel@tonic-gate int
24660Sstevel@tonic-gate i_ddi_unload_drvconf(major_t major)
24670Sstevel@tonic-gate {
24680Sstevel@tonic-gate 	int error;
24690Sstevel@tonic-gate 	struct devnames *dnp;
24700Sstevel@tonic-gate 
24710Sstevel@tonic-gate 	if (major >= devcnt)
24720Sstevel@tonic-gate 		return (EINVAL);
24730Sstevel@tonic-gate 
24740Sstevel@tonic-gate 	/*
24750Sstevel@tonic-gate 	 * Take the per-driver lock while unloading driver.conf
24760Sstevel@tonic-gate 	 */
24770Sstevel@tonic-gate 	dnp = &devnamesp[major];
24780Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
24790Sstevel@tonic-gate 	error = impl_free_parlist(major);
24800Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
24810Sstevel@tonic-gate 	return (error);
24820Sstevel@tonic-gate }
24830Sstevel@tonic-gate 
24840Sstevel@tonic-gate /*
24850Sstevel@tonic-gate  * Merge a .conf node. This is called by nexus drivers to augment
24860Sstevel@tonic-gate  * hw node with properties specified in driver.conf file. This function
24870Sstevel@tonic-gate  * takes a callback routine to name nexus children.
24880Sstevel@tonic-gate  * The parent node must be held busy.
24890Sstevel@tonic-gate  *
24900Sstevel@tonic-gate  * It returns DDI_SUCCESS if the node is merged and DDI_FAILURE otherwise.
24910Sstevel@tonic-gate  */
24920Sstevel@tonic-gate int
24930Sstevel@tonic-gate ndi_merge_node(dev_info_t *dip, int (*name_node)(dev_info_t *, char *, int))
24940Sstevel@tonic-gate {
24950Sstevel@tonic-gate 	dev_info_t *hwdip;
24960Sstevel@tonic-gate 
24970Sstevel@tonic-gate 	ASSERT(ndi_dev_is_persistent_node(dip) == 0);
24980Sstevel@tonic-gate 	ASSERT(ddi_get_name_addr(dip) != NULL);
24990Sstevel@tonic-gate 
25000Sstevel@tonic-gate 	hwdip = find_child_by_callback(ddi_get_parent(dip),
25010Sstevel@tonic-gate 	    ddi_binding_name(dip), ddi_get_name_addr(dip), name_node);
25020Sstevel@tonic-gate 
25030Sstevel@tonic-gate 	/*
25040Sstevel@tonic-gate 	 * Look for the hardware node that is the target of the merge;
25050Sstevel@tonic-gate 	 * return failure if not found.
25060Sstevel@tonic-gate 	 */
25070Sstevel@tonic-gate 	if ((hwdip == NULL) || (hwdip == dip)) {
25080Sstevel@tonic-gate 		char *buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
25090Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_WARN, "No HW node to merge conf node %s",
25100Sstevel@tonic-gate 		    ddi_deviname(dip, buf)));
25110Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
25120Sstevel@tonic-gate 		return (DDI_FAILURE);
25130Sstevel@tonic-gate 	}
25140Sstevel@tonic-gate 
25150Sstevel@tonic-gate 	/*
25160Sstevel@tonic-gate 	 * Make sure the hardware node is uninitialized and has no property.
25170Sstevel@tonic-gate 	 * This may not be the case if new .conf files are load after some
25180Sstevel@tonic-gate 	 * hardware nodes have already been initialized and attached.
25190Sstevel@tonic-gate 	 *
25200Sstevel@tonic-gate 	 * N.B. We return success here because the node was *intended*
25210Sstevel@tonic-gate 	 * 	to be a merge node because there is a hw node with the name.
25220Sstevel@tonic-gate 	 */
25230Sstevel@tonic-gate 	mutex_enter(&DEVI(hwdip)->devi_lock);
25240Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(hwdip) == 0) {
25250Sstevel@tonic-gate 		char *buf;
25260Sstevel@tonic-gate 		mutex_exit(&DEVI(hwdip)->devi_lock);
25270Sstevel@tonic-gate 
25280Sstevel@tonic-gate 		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
25290Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE, "Duplicate .conf node %s",
25300Sstevel@tonic-gate 		    ddi_deviname(dip, buf)));
25310Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
25320Sstevel@tonic-gate 		return (DDI_SUCCESS);
25330Sstevel@tonic-gate 	}
25340Sstevel@tonic-gate 
25350Sstevel@tonic-gate 	/*
25360Sstevel@tonic-gate 	 * If it is possible that the hardware has already been touched
25370Sstevel@tonic-gate 	 * then don't merge.
25380Sstevel@tonic-gate 	 */
25390Sstevel@tonic-gate 	if (i_ddi_node_state(hwdip) >= DS_INITIALIZED ||
25400Sstevel@tonic-gate 	    (DEVI(hwdip)->devi_sys_prop_ptr != NULL) ||
25410Sstevel@tonic-gate 	    (DEVI(hwdip)->devi_drv_prop_ptr != NULL)) {
25420Sstevel@tonic-gate 		char *buf;
25430Sstevel@tonic-gate 		mutex_exit(&DEVI(hwdip)->devi_lock);
25440Sstevel@tonic-gate 
25450Sstevel@tonic-gate 		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
25460Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE,
25470Sstevel@tonic-gate 		    "!Cannot merge .conf node %s with hw node %p "
25480Sstevel@tonic-gate 		    "-- not in proper state",
25490Sstevel@tonic-gate 		    ddi_deviname(dip, buf), (void *)hwdip));
25500Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
25510Sstevel@tonic-gate 		return (DDI_SUCCESS);
25520Sstevel@tonic-gate 	}
25530Sstevel@tonic-gate 
25540Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
25550Sstevel@tonic-gate 	DEVI(hwdip)->devi_sys_prop_ptr = DEVI(dip)->devi_sys_prop_ptr;
25560Sstevel@tonic-gate 	DEVI(hwdip)->devi_drv_prop_ptr = DEVI(dip)->devi_drv_prop_ptr;
25570Sstevel@tonic-gate 	DEVI(dip)->devi_sys_prop_ptr = NULL;
25580Sstevel@tonic-gate 	DEVI(dip)->devi_drv_prop_ptr = NULL;
25590Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
25600Sstevel@tonic-gate 	mutex_exit(&DEVI(hwdip)->devi_lock);
25610Sstevel@tonic-gate 
25620Sstevel@tonic-gate 	return (DDI_SUCCESS);
25630Sstevel@tonic-gate }
25640Sstevel@tonic-gate 
25650Sstevel@tonic-gate /*
25660Sstevel@tonic-gate  * Merge a "wildcard" .conf node. This is called by nexus drivers to
25670Sstevel@tonic-gate  * augment a set of hw node with properties specified in driver.conf file.
25680Sstevel@tonic-gate  * The parent node must be held busy.
25690Sstevel@tonic-gate  *
25700Sstevel@tonic-gate  * There is no failure mode, since the nexus may or may not have child
25710Sstevel@tonic-gate  * node bound the driver specified by the wildcard node.
25720Sstevel@tonic-gate  */
25730Sstevel@tonic-gate void
25740Sstevel@tonic-gate ndi_merge_wildcard_node(dev_info_t *dip)
25750Sstevel@tonic-gate {
25760Sstevel@tonic-gate 	dev_info_t *hwdip;
25770Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
25780Sstevel@tonic-gate 	major_t major = ddi_driver_major(dip);
25790Sstevel@tonic-gate 
25800Sstevel@tonic-gate 	/* never attempt to merge a hw node */
25810Sstevel@tonic-gate 	ASSERT(ndi_dev_is_persistent_node(dip) == 0);
25820Sstevel@tonic-gate 	/* must be bound to a driver major number */
25837009Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
25840Sstevel@tonic-gate 
25850Sstevel@tonic-gate 	/*
25860Sstevel@tonic-gate 	 * Walk the child list to find all nodes bound to major
25870Sstevel@tonic-gate 	 * and copy properties.
25880Sstevel@tonic-gate 	 */
25890Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
25907224Scth 	ASSERT(DEVI_BUSY_OWNED(pdip));
25910Sstevel@tonic-gate 	for (hwdip = ddi_get_child(pdip); hwdip;
25920Sstevel@tonic-gate 	    hwdip = ddi_get_next_sibling(hwdip)) {
25930Sstevel@tonic-gate 		/*
25940Sstevel@tonic-gate 		 * Skip nodes not bound to same driver
25950Sstevel@tonic-gate 		 */
25960Sstevel@tonic-gate 		if (ddi_driver_major(hwdip) != major)
25970Sstevel@tonic-gate 			continue;
25980Sstevel@tonic-gate 
25990Sstevel@tonic-gate 		/*
26000Sstevel@tonic-gate 		 * Skip .conf nodes
26010Sstevel@tonic-gate 		 */
26020Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(hwdip) == 0)
26030Sstevel@tonic-gate 			continue;
26040Sstevel@tonic-gate 
26050Sstevel@tonic-gate 		/*
26060Sstevel@tonic-gate 		 * Make sure the node is uninitialized and has no property.
26070Sstevel@tonic-gate 		 */
26080Sstevel@tonic-gate 		mutex_enter(&DEVI(hwdip)->devi_lock);
26090Sstevel@tonic-gate 		if (i_ddi_node_state(hwdip) >= DS_INITIALIZED ||
26100Sstevel@tonic-gate 		    (DEVI(hwdip)->devi_sys_prop_ptr != NULL) ||
26110Sstevel@tonic-gate 		    (DEVI(hwdip)->devi_drv_prop_ptr != NULL)) {
26120Sstevel@tonic-gate 			mutex_exit(&DEVI(hwdip)->devi_lock);
26130Sstevel@tonic-gate 			NDI_CONFIG_DEBUG((CE_NOTE, "HW node %p state not "
26140Sstevel@tonic-gate 			    "suitable for merging wildcard conf node %s",
26150Sstevel@tonic-gate 			    (void *)hwdip, ddi_node_name(dip)));
26160Sstevel@tonic-gate 			continue;
26170Sstevel@tonic-gate 		}
26180Sstevel@tonic-gate 
26190Sstevel@tonic-gate 		DEVI(hwdip)->devi_sys_prop_ptr =
26200Sstevel@tonic-gate 		    i_ddi_prop_list_dup(DEVI(dip)->devi_sys_prop_ptr, KM_SLEEP);
26210Sstevel@tonic-gate 		DEVI(hwdip)->devi_drv_prop_ptr =
26220Sstevel@tonic-gate 		    i_ddi_prop_list_dup(DEVI(dip)->devi_drv_prop_ptr, KM_SLEEP);
26230Sstevel@tonic-gate 		mutex_exit(&DEVI(hwdip)->devi_lock);
26240Sstevel@tonic-gate 	}
26250Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
26260Sstevel@tonic-gate }
26270Sstevel@tonic-gate 
26280Sstevel@tonic-gate /*
26290Sstevel@tonic-gate  * Return the major number based on the compatible property. This interface
26300Sstevel@tonic-gate  * may be used in situations where we are trying to detect if a better driver
26310Sstevel@tonic-gate  * now exists for a device, so it must use the 'compatible' property.  If
26320Sstevel@tonic-gate  * a non-NULL formp is specified and the binding was based on compatible then
26330Sstevel@tonic-gate  * return the pointer to the form used in *formp.
26340Sstevel@tonic-gate  */
26350Sstevel@tonic-gate major_t
26360Sstevel@tonic-gate ddi_compatible_driver_major(dev_info_t *dip, char **formp)
26370Sstevel@tonic-gate {
26380Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
26390Sstevel@tonic-gate 	void		*compat;
26400Sstevel@tonic-gate 	size_t		len;
26410Sstevel@tonic-gate 	char		*p = NULL;
26427009Scth 	major_t		major = DDI_MAJOR_T_NONE;
26430Sstevel@tonic-gate 
26440Sstevel@tonic-gate 	if (formp)
26450Sstevel@tonic-gate 		*formp = NULL;
26460Sstevel@tonic-gate 
26474145Scth 	/*
26484145Scth 	 * Highest precedence binding is a path-oriented alias. Since this
26494145Scth 	 * requires a 'path', this type of binding occurs via more obtuse
26504145Scth 	 * 'rebind'. The need for a path-oriented alias 'rebind' is detected
26514145Scth 	 * after a successful DDI_CTLOPS_INITCHILD to another driver: this is
26524145Scth 	 * is the first point at which the unit-address (or instance) of the
26534145Scth 	 * last component of the path is available (even though the path is
26544145Scth 	 * bound to the wrong driver at this point).
26554145Scth 	 */
26564145Scth 	if (devi->devi_flags & DEVI_REBIND) {
26574145Scth 		p = devi->devi_rebinding_name;
26584145Scth 		major = ddi_name_to_major(p);
26597009Scth 		if ((major != DDI_MAJOR_T_NONE) &&
26604145Scth 		    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED)) {
26614145Scth 			if (formp)
26624145Scth 				*formp = p;
26634145Scth 			return (major);
26644145Scth 		}
26654145Scth 
26664145Scth 		/*
26674145Scth 		 * If for some reason devi_rebinding_name no longer resolves
26684145Scth 		 * to a proper driver then clear DEVI_REBIND.
26694145Scth 		 */
26704145Scth 		mutex_enter(&devi->devi_lock);
26714145Scth 		devi->devi_flags &= ~DEVI_REBIND;
26724145Scth 		mutex_exit(&devi->devi_lock);
26734145Scth 	}
26744145Scth 
26750Sstevel@tonic-gate 	/* look up compatible property */
26760Sstevel@tonic-gate 	(void) lookup_compatible(dip, KM_SLEEP);
26770Sstevel@tonic-gate 	compat = (void *)(devi->devi_compat_names);
26780Sstevel@tonic-gate 	len = devi->devi_compat_length;
26790Sstevel@tonic-gate 
26800Sstevel@tonic-gate 	/* find the highest precedence compatible form with a driver binding */
26810Sstevel@tonic-gate 	while ((p = prom_decode_composite_string(compat, len, p)) != NULL) {
26820Sstevel@tonic-gate 		major = ddi_name_to_major(p);
26837009Scth 		if ((major != DDI_MAJOR_T_NONE) &&
26840Sstevel@tonic-gate 		    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED)) {
26850Sstevel@tonic-gate 			if (formp)
26860Sstevel@tonic-gate 				*formp = p;
26870Sstevel@tonic-gate 			return (major);
26880Sstevel@tonic-gate 		}
26890Sstevel@tonic-gate 	}
26900Sstevel@tonic-gate 
26910Sstevel@tonic-gate 	/*
26920Sstevel@tonic-gate 	 * none of the compatible forms have a driver binding, see if
26930Sstevel@tonic-gate 	 * the node name has a driver binding.
26940Sstevel@tonic-gate 	 */
26950Sstevel@tonic-gate 	major = ddi_name_to_major(ddi_node_name(dip));
26967009Scth 	if ((major != DDI_MAJOR_T_NONE) &&
26970Sstevel@tonic-gate 	    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED))
26980Sstevel@tonic-gate 		return (major);
26990Sstevel@tonic-gate 
27000Sstevel@tonic-gate 	/* no driver */
27017009Scth 	return (DDI_MAJOR_T_NONE);
27020Sstevel@tonic-gate }
27030Sstevel@tonic-gate 
27040Sstevel@tonic-gate /*
27050Sstevel@tonic-gate  * Static help functions
27060Sstevel@tonic-gate  */
27070Sstevel@tonic-gate 
27080Sstevel@tonic-gate /*
27090Sstevel@tonic-gate  * lookup the "compatible" property and cache it's contents in the
27100Sstevel@tonic-gate  * device node.
27110Sstevel@tonic-gate  */
27120Sstevel@tonic-gate static int
27130Sstevel@tonic-gate lookup_compatible(dev_info_t *dip, uint_t flag)
27140Sstevel@tonic-gate {
27150Sstevel@tonic-gate 	int rv;
27160Sstevel@tonic-gate 	int prop_flags;
27170Sstevel@tonic-gate 	uint_t ncompatstrs;
27180Sstevel@tonic-gate 	char **compatstrpp;
27190Sstevel@tonic-gate 	char *di_compat_strp;
27200Sstevel@tonic-gate 	size_t di_compat_strlen;
27210Sstevel@tonic-gate 
27220Sstevel@tonic-gate 	if (DEVI(dip)->devi_compat_names) {
27230Sstevel@tonic-gate 		return (DDI_SUCCESS);
27240Sstevel@tonic-gate 	}
27250Sstevel@tonic-gate 
27260Sstevel@tonic-gate 	prop_flags = DDI_PROP_TYPE_STRING | DDI_PROP_DONTPASS;
27270Sstevel@tonic-gate 
27280Sstevel@tonic-gate 	if (flag & KM_NOSLEEP) {
27290Sstevel@tonic-gate 		prop_flags |= DDI_PROP_DONTSLEEP;
27300Sstevel@tonic-gate 	}
27310Sstevel@tonic-gate 
27320Sstevel@tonic-gate 	if (ndi_dev_is_prom_node(dip) == 0) {
27330Sstevel@tonic-gate 		prop_flags |= DDI_PROP_NOTPROM;
27340Sstevel@tonic-gate 	}
27350Sstevel@tonic-gate 
27360Sstevel@tonic-gate 	rv = ddi_prop_lookup_common(DDI_DEV_T_ANY, dip, prop_flags,
27370Sstevel@tonic-gate 	    "compatible", &compatstrpp, &ncompatstrs,
27380Sstevel@tonic-gate 	    ddi_prop_fm_decode_strings);
27390Sstevel@tonic-gate 
27400Sstevel@tonic-gate 	if (rv == DDI_PROP_NOT_FOUND) {
27410Sstevel@tonic-gate 		return (DDI_SUCCESS);
27420Sstevel@tonic-gate 	}
27430Sstevel@tonic-gate 
27440Sstevel@tonic-gate 	if (rv != DDI_PROP_SUCCESS) {
27450Sstevel@tonic-gate 		return (DDI_FAILURE);
27460Sstevel@tonic-gate 	}
27470Sstevel@tonic-gate 
27480Sstevel@tonic-gate 	/*
27490Sstevel@tonic-gate 	 * encode the compatible property data in the dev_info node
27500Sstevel@tonic-gate 	 */
27510Sstevel@tonic-gate 	rv = DDI_SUCCESS;
27520Sstevel@tonic-gate 	if (ncompatstrs != 0) {
27530Sstevel@tonic-gate 		di_compat_strp = encode_composite_string(compatstrpp,
27540Sstevel@tonic-gate 		    ncompatstrs, &di_compat_strlen, flag);
27550Sstevel@tonic-gate 		if (di_compat_strp != NULL) {
27560Sstevel@tonic-gate 			DEVI(dip)->devi_compat_names = di_compat_strp;
27570Sstevel@tonic-gate 			DEVI(dip)->devi_compat_length = di_compat_strlen;
27580Sstevel@tonic-gate 		} else {
27590Sstevel@tonic-gate 			rv = DDI_FAILURE;
27600Sstevel@tonic-gate 		}
27610Sstevel@tonic-gate 	}
27620Sstevel@tonic-gate 	ddi_prop_free(compatstrpp);
27630Sstevel@tonic-gate 	return (rv);
27640Sstevel@tonic-gate }
27650Sstevel@tonic-gate 
27660Sstevel@tonic-gate /*
27670Sstevel@tonic-gate  * Create a composite string from a list of strings.
27680Sstevel@tonic-gate  *
27690Sstevel@tonic-gate  * A composite string consists of a single buffer containing one
27700Sstevel@tonic-gate  * or more NULL terminated strings.
27710Sstevel@tonic-gate  */
27720Sstevel@tonic-gate static char *
27730Sstevel@tonic-gate encode_composite_string(char **strings, uint_t nstrings, size_t *retsz,
27740Sstevel@tonic-gate     uint_t flag)
27750Sstevel@tonic-gate {
27760Sstevel@tonic-gate 	uint_t index;
27770Sstevel@tonic-gate 	char  **strpp;
27780Sstevel@tonic-gate 	uint_t slen;
27790Sstevel@tonic-gate 	size_t cbuf_sz = 0;
27800Sstevel@tonic-gate 	char *cbuf_p;
27810Sstevel@tonic-gate 	char *cbuf_ip;
27820Sstevel@tonic-gate 
27830Sstevel@tonic-gate 	if (strings == NULL || nstrings == 0 || retsz == NULL) {
27840Sstevel@tonic-gate 		return (NULL);
27850Sstevel@tonic-gate 	}
27860Sstevel@tonic-gate 
27870Sstevel@tonic-gate 	for (index = 0, strpp = strings; index < nstrings; index++)
27880Sstevel@tonic-gate 		cbuf_sz += strlen(*(strpp++)) + 1;
27890Sstevel@tonic-gate 
27900Sstevel@tonic-gate 	if ((cbuf_p = kmem_alloc(cbuf_sz, flag)) == NULL) {
27910Sstevel@tonic-gate 		cmn_err(CE_NOTE,
27920Sstevel@tonic-gate 		    "?failed to allocate device node compatstr");
27930Sstevel@tonic-gate 		return (NULL);
27940Sstevel@tonic-gate 	}
27950Sstevel@tonic-gate 
27960Sstevel@tonic-gate 	cbuf_ip = cbuf_p;
27970Sstevel@tonic-gate 	for (index = 0, strpp = strings; index < nstrings; index++) {
27980Sstevel@tonic-gate 		slen = strlen(*strpp);
27990Sstevel@tonic-gate 		bcopy(*(strpp++), cbuf_ip, slen);
28000Sstevel@tonic-gate 		cbuf_ip += slen;
28010Sstevel@tonic-gate 		*(cbuf_ip++) = '\0';
28020Sstevel@tonic-gate 	}
28030Sstevel@tonic-gate 
28040Sstevel@tonic-gate 	*retsz = cbuf_sz;
28050Sstevel@tonic-gate 	return (cbuf_p);
28060Sstevel@tonic-gate }
28070Sstevel@tonic-gate 
28080Sstevel@tonic-gate static void
28090Sstevel@tonic-gate link_to_driver_list(dev_info_t *dip)
28100Sstevel@tonic-gate {
28110Sstevel@tonic-gate 	major_t major = DEVI(dip)->devi_major;
28120Sstevel@tonic-gate 	struct devnames *dnp;
28130Sstevel@tonic-gate 
28147009Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
28150Sstevel@tonic-gate 
28160Sstevel@tonic-gate 	/*
28170Sstevel@tonic-gate 	 * Remove from orphan list
28180Sstevel@tonic-gate 	 */
28190Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
28200Sstevel@tonic-gate 		dnp = &orphanlist;
28210Sstevel@tonic-gate 		remove_from_dn_list(dnp, dip);
28220Sstevel@tonic-gate 	}
28230Sstevel@tonic-gate 
28240Sstevel@tonic-gate 	/*
28250Sstevel@tonic-gate 	 * Add to per driver list
28260Sstevel@tonic-gate 	 */
28270Sstevel@tonic-gate 	dnp = &devnamesp[major];
28280Sstevel@tonic-gate 	add_to_dn_list(dnp, dip);
28290Sstevel@tonic-gate }
28300Sstevel@tonic-gate 
28310Sstevel@tonic-gate static void
28320Sstevel@tonic-gate unlink_from_driver_list(dev_info_t *dip)
28330Sstevel@tonic-gate {
28340Sstevel@tonic-gate 	major_t major = DEVI(dip)->devi_major;
28350Sstevel@tonic-gate 	struct devnames *dnp;
28360Sstevel@tonic-gate 
28377009Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
28380Sstevel@tonic-gate 
28390Sstevel@tonic-gate 	/*
28400Sstevel@tonic-gate 	 * Remove from per-driver list
28410Sstevel@tonic-gate 	 */
28420Sstevel@tonic-gate 	dnp = &devnamesp[major];
28430Sstevel@tonic-gate 	remove_from_dn_list(dnp, dip);
28440Sstevel@tonic-gate 
28450Sstevel@tonic-gate 	/*
28460Sstevel@tonic-gate 	 * Add to orphan list
28470Sstevel@tonic-gate 	 */
28480Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
28490Sstevel@tonic-gate 		dnp = &orphanlist;
28500Sstevel@tonic-gate 		add_to_dn_list(dnp, dip);
28510Sstevel@tonic-gate 	}
28520Sstevel@tonic-gate }
28530Sstevel@tonic-gate 
28540Sstevel@tonic-gate /*
28550Sstevel@tonic-gate  * scan the per-driver list looking for dev_info "dip"
28560Sstevel@tonic-gate  */
28570Sstevel@tonic-gate static dev_info_t *
28580Sstevel@tonic-gate in_dn_list(struct devnames *dnp, dev_info_t *dip)
28590Sstevel@tonic-gate {
28600Sstevel@tonic-gate 	struct dev_info *idevi;
28610Sstevel@tonic-gate 
28620Sstevel@tonic-gate 	if ((idevi = DEVI(dnp->dn_head)) == NULL)
28630Sstevel@tonic-gate 		return (NULL);
28640Sstevel@tonic-gate 
28650Sstevel@tonic-gate 	while (idevi) {
28660Sstevel@tonic-gate 		if (idevi == DEVI(dip))
28670Sstevel@tonic-gate 			return (dip);
28680Sstevel@tonic-gate 		idevi = idevi->devi_next;
28690Sstevel@tonic-gate 	}
28700Sstevel@tonic-gate 	return (NULL);
28710Sstevel@tonic-gate }
28720Sstevel@tonic-gate 
28730Sstevel@tonic-gate /*
28740Sstevel@tonic-gate  * insert devinfo node 'dip' into the per-driver instance list
28750Sstevel@tonic-gate  * headed by 'dnp'
28760Sstevel@tonic-gate  *
28770Sstevel@tonic-gate  * Nodes on the per-driver list are ordered: HW - SID - PSEUDO.  The order is
28780Sstevel@tonic-gate  * required for merging of .conf file data to work properly.
28790Sstevel@tonic-gate  */
28800Sstevel@tonic-gate static void
28810Sstevel@tonic-gate add_to_ordered_dn_list(struct devnames *dnp, dev_info_t *dip)
28820Sstevel@tonic-gate {
28830Sstevel@tonic-gate 	dev_info_t **dipp;
28840Sstevel@tonic-gate 
28850Sstevel@tonic-gate 	ASSERT(mutex_owned(&(dnp->dn_lock)));
28860Sstevel@tonic-gate 
28870Sstevel@tonic-gate 	dipp = &dnp->dn_head;
28880Sstevel@tonic-gate 	if (ndi_dev_is_prom_node(dip)) {
28890Sstevel@tonic-gate 		/*
28900Sstevel@tonic-gate 		 * Find the first non-prom node or end of list
28910Sstevel@tonic-gate 		 */
28920Sstevel@tonic-gate 		while (*dipp && (ndi_dev_is_prom_node(*dipp) != 0)) {
28930Sstevel@tonic-gate 			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
28940Sstevel@tonic-gate 		}
28950Sstevel@tonic-gate 	} else if (ndi_dev_is_persistent_node(dip)) {
28960Sstevel@tonic-gate 		/*
28970Sstevel@tonic-gate 		 * Find the first non-persistent node
28980Sstevel@tonic-gate 		 */
28990Sstevel@tonic-gate 		while (*dipp && (ndi_dev_is_persistent_node(*dipp) != 0)) {
29000Sstevel@tonic-gate 			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
29010Sstevel@tonic-gate 		}
29020Sstevel@tonic-gate 	} else {
29030Sstevel@tonic-gate 		/*
29040Sstevel@tonic-gate 		 * Find the end of the list
29050Sstevel@tonic-gate 		 */
29060Sstevel@tonic-gate 		while (*dipp) {
29070Sstevel@tonic-gate 			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
29080Sstevel@tonic-gate 		}
29090Sstevel@tonic-gate 	}
29100Sstevel@tonic-gate 
29110Sstevel@tonic-gate 	DEVI(dip)->devi_next = DEVI(*dipp);
29120Sstevel@tonic-gate 	*dipp = dip;
29130Sstevel@tonic-gate }
29140Sstevel@tonic-gate 
29150Sstevel@tonic-gate /*
29160Sstevel@tonic-gate  * add a list of device nodes to the device node list in the
29170Sstevel@tonic-gate  * devnames structure
29180Sstevel@tonic-gate  */
29190Sstevel@tonic-gate static void
29200Sstevel@tonic-gate add_to_dn_list(struct devnames *dnp, dev_info_t *dip)
29210Sstevel@tonic-gate {
29220Sstevel@tonic-gate 	/*
29230Sstevel@tonic-gate 	 * Look to see if node already exists
29240Sstevel@tonic-gate 	 */
29250Sstevel@tonic-gate 	LOCK_DEV_OPS(&(dnp->dn_lock));
29260Sstevel@tonic-gate 	if (in_dn_list(dnp, dip)) {
29270Sstevel@tonic-gate 		cmn_err(CE_NOTE, "add_to_dn_list: node %s already in list",
29280Sstevel@tonic-gate 		    DEVI(dip)->devi_node_name);
29290Sstevel@tonic-gate 	} else {
29300Sstevel@tonic-gate 		add_to_ordered_dn_list(dnp, dip);
29310Sstevel@tonic-gate 	}
29320Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
29330Sstevel@tonic-gate }
29340Sstevel@tonic-gate 
29350Sstevel@tonic-gate static void
29360Sstevel@tonic-gate remove_from_dn_list(struct devnames *dnp, dev_info_t *dip)
29370Sstevel@tonic-gate {
29380Sstevel@tonic-gate 	dev_info_t **plist;
29390Sstevel@tonic-gate 
29400Sstevel@tonic-gate 	LOCK_DEV_OPS(&(dnp->dn_lock));
29410Sstevel@tonic-gate 
29420Sstevel@tonic-gate 	plist = (dev_info_t **)&dnp->dn_head;
29430Sstevel@tonic-gate 	while (*plist && (*plist != dip)) {
29440Sstevel@tonic-gate 		plist = (dev_info_t **)&DEVI(*plist)->devi_next;
29450Sstevel@tonic-gate 	}
29460Sstevel@tonic-gate 
29470Sstevel@tonic-gate 	if (*plist != NULL) {
29480Sstevel@tonic-gate 		ASSERT(*plist == dip);
29490Sstevel@tonic-gate 		*plist = (dev_info_t *)(DEVI(dip)->devi_next);
29500Sstevel@tonic-gate 		DEVI(dip)->devi_next = NULL;
29510Sstevel@tonic-gate 	} else {
29520Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE,
29530Sstevel@tonic-gate 		    "remove_from_dn_list: node %s not found in list",
29540Sstevel@tonic-gate 		    DEVI(dip)->devi_node_name));
29550Sstevel@tonic-gate 	}
29560Sstevel@tonic-gate 
29570Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
29580Sstevel@tonic-gate }
29590Sstevel@tonic-gate 
29600Sstevel@tonic-gate /*
29610Sstevel@tonic-gate  * Add and remove reference driver global property list
29620Sstevel@tonic-gate  */
29630Sstevel@tonic-gate static void
29640Sstevel@tonic-gate add_global_props(dev_info_t *dip)
29650Sstevel@tonic-gate {
29660Sstevel@tonic-gate 	struct devnames *dnp;
29670Sstevel@tonic-gate 	ddi_prop_list_t *plist;
29680Sstevel@tonic-gate 
29690Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_global_prop_list == NULL);
29707009Scth 	ASSERT(DEVI(dip)->devi_major != DDI_MAJOR_T_NONE);
29710Sstevel@tonic-gate 
29720Sstevel@tonic-gate 	dnp = &devnamesp[DEVI(dip)->devi_major];
29730Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
29740Sstevel@tonic-gate 	plist = dnp->dn_global_prop_ptr;
29750Sstevel@tonic-gate 	if (plist == NULL) {
29760Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
29770Sstevel@tonic-gate 		return;
29780Sstevel@tonic-gate 	}
29790Sstevel@tonic-gate 	i_ddi_prop_list_hold(plist, dnp);
29800Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
29810Sstevel@tonic-gate 
29820Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
29830Sstevel@tonic-gate 	DEVI(dip)->devi_global_prop_list = plist;
29840Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
29850Sstevel@tonic-gate }
29860Sstevel@tonic-gate 
29870Sstevel@tonic-gate static void
29880Sstevel@tonic-gate remove_global_props(dev_info_t *dip)
29890Sstevel@tonic-gate {
29900Sstevel@tonic-gate 	ddi_prop_list_t *proplist;
29910Sstevel@tonic-gate 
29920Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
29930Sstevel@tonic-gate 	proplist = DEVI(dip)->devi_global_prop_list;
29940Sstevel@tonic-gate 	DEVI(dip)->devi_global_prop_list = NULL;
29950Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
29960Sstevel@tonic-gate 
29970Sstevel@tonic-gate 	if (proplist) {
29980Sstevel@tonic-gate 		major_t major;
29990Sstevel@tonic-gate 		struct devnames *dnp;
30000Sstevel@tonic-gate 
30010Sstevel@tonic-gate 		major = ddi_driver_major(dip);
30027009Scth 		ASSERT(major != DDI_MAJOR_T_NONE);
30030Sstevel@tonic-gate 		dnp = &devnamesp[major];
30040Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
30050Sstevel@tonic-gate 		i_ddi_prop_list_rele(proplist, dnp);
30060Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
30070Sstevel@tonic-gate 	}
30080Sstevel@tonic-gate }
30090Sstevel@tonic-gate 
30100Sstevel@tonic-gate #ifdef DEBUG
30110Sstevel@tonic-gate /*
30120Sstevel@tonic-gate  * Set this variable to '0' to disable the optimization,
30130Sstevel@tonic-gate  * and to 2 to print debug message.
30140Sstevel@tonic-gate  */
30150Sstevel@tonic-gate static int optimize_dtree = 1;
30160Sstevel@tonic-gate 
30170Sstevel@tonic-gate static void
30180Sstevel@tonic-gate debug_dtree(dev_info_t *devi, struct dev_info *adevi, char *service)
30190Sstevel@tonic-gate {
30200Sstevel@tonic-gate 	char *adeviname, *buf;
30210Sstevel@tonic-gate 
30220Sstevel@tonic-gate 	/*
30230Sstevel@tonic-gate 	 * Don't print unless optimize dtree is set to 2+
30240Sstevel@tonic-gate 	 */
30250Sstevel@tonic-gate 	if (optimize_dtree <= 1)
30260Sstevel@tonic-gate 		return;
30270Sstevel@tonic-gate 
30280Sstevel@tonic-gate 	buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
30290Sstevel@tonic-gate 	adeviname = ddi_deviname((dev_info_t *)adevi, buf);
30300Sstevel@tonic-gate 	if (*adeviname == '\0')
30310Sstevel@tonic-gate 		adeviname = "root";
30320Sstevel@tonic-gate 
30330Sstevel@tonic-gate 	cmn_err(CE_CONT, "%s %s -> %s\n",
30340Sstevel@tonic-gate 	    ddi_deviname(devi, buf), service, adeviname);
30350Sstevel@tonic-gate 
30360Sstevel@tonic-gate 	kmem_free(buf, MAXNAMELEN);
30370Sstevel@tonic-gate }
30380Sstevel@tonic-gate #else /* DEBUG */
30390Sstevel@tonic-gate #define	debug_dtree(a1, a2, a3)	 /* nothing */
30400Sstevel@tonic-gate #endif  /* DEBUG */
30410Sstevel@tonic-gate 
30420Sstevel@tonic-gate static void
30430Sstevel@tonic-gate ddi_optimize_dtree(dev_info_t *devi)
30440Sstevel@tonic-gate {
30450Sstevel@tonic-gate 	struct dev_info *pdevi;
30460Sstevel@tonic-gate 	struct bus_ops *b;
30470Sstevel@tonic-gate 
30480Sstevel@tonic-gate 	pdevi = DEVI(devi)->devi_parent;
30490Sstevel@tonic-gate 	ASSERT(pdevi);
30500Sstevel@tonic-gate 
30510Sstevel@tonic-gate 	/*
30520Sstevel@tonic-gate 	 * Set the unoptimized values
30530Sstevel@tonic-gate 	 */
30540Sstevel@tonic-gate 	DEVI(devi)->devi_bus_map_fault = pdevi;
30550Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_map = pdevi;
30560Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_allochdl = pdevi;
30570Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_freehdl = pdevi;
30580Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_bindhdl = pdevi;
30590Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_bindfunc =
30604950Scth 	    pdevi->devi_ops->devo_bus_ops->bus_dma_bindhdl;
30610Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_unbindhdl = pdevi;
30620Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_unbindfunc =
30630Sstevel@tonic-gate 	    pdevi->devi_ops->devo_bus_ops->bus_dma_unbindhdl;
30640Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_flush = pdevi;
30650Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_win = pdevi;
30660Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_ctl = pdevi;
30670Sstevel@tonic-gate 	DEVI(devi)->devi_bus_ctl = pdevi;
30680Sstevel@tonic-gate 
30690Sstevel@tonic-gate #ifdef DEBUG
30700Sstevel@tonic-gate 	if (optimize_dtree == 0)
30710Sstevel@tonic-gate 		return;
30720Sstevel@tonic-gate #endif /* DEBUG */
30730Sstevel@tonic-gate 
30740Sstevel@tonic-gate 	b = pdevi->devi_ops->devo_bus_ops;
30750Sstevel@tonic-gate 
30760Sstevel@tonic-gate 	if (i_ddi_map_fault == b->bus_map_fault) {
30770Sstevel@tonic-gate 		DEVI(devi)->devi_bus_map_fault = pdevi->devi_bus_map_fault;
30780Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_map_fault,
30790Sstevel@tonic-gate 		    "bus_map_fault");
30800Sstevel@tonic-gate 	}
30810Sstevel@tonic-gate 
30820Sstevel@tonic-gate 	if (ddi_dma_map == b->bus_dma_map) {
30830Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_map = pdevi->devi_bus_dma_map;
30840Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_map, "bus_dma_map");
30850Sstevel@tonic-gate 	}
30860Sstevel@tonic-gate 
30870Sstevel@tonic-gate 	if (ddi_dma_allochdl == b->bus_dma_allochdl) {
30880Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_allochdl =
30890Sstevel@tonic-gate 		    pdevi->devi_bus_dma_allochdl;
30900Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_allochdl,
30910Sstevel@tonic-gate 		    "bus_dma_allochdl");
30920Sstevel@tonic-gate 	}
30930Sstevel@tonic-gate 
30940Sstevel@tonic-gate 	if (ddi_dma_freehdl == b->bus_dma_freehdl) {
30950Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_freehdl = pdevi->devi_bus_dma_freehdl;
30960Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_freehdl,
30970Sstevel@tonic-gate 		    "bus_dma_freehdl");
30980Sstevel@tonic-gate 	}
30990Sstevel@tonic-gate 
31000Sstevel@tonic-gate 	if (ddi_dma_bindhdl == b->bus_dma_bindhdl) {
31010Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_bindhdl = pdevi->devi_bus_dma_bindhdl;
31020Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_bindfunc =
31030Sstevel@tonic-gate 		    pdevi->devi_bus_dma_bindhdl->devi_ops->
31040Sstevel@tonic-gate 		    devo_bus_ops->bus_dma_bindhdl;
31050Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_bindhdl,
31060Sstevel@tonic-gate 		    "bus_dma_bindhdl");
31070Sstevel@tonic-gate 	}
31080Sstevel@tonic-gate 
31090Sstevel@tonic-gate 	if (ddi_dma_unbindhdl == b->bus_dma_unbindhdl) {
31100Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_unbindhdl =
31110Sstevel@tonic-gate 		    pdevi->devi_bus_dma_unbindhdl;
31120Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_unbindfunc =
31130Sstevel@tonic-gate 		    pdevi->devi_bus_dma_unbindhdl->devi_ops->
31140Sstevel@tonic-gate 		    devo_bus_ops->bus_dma_unbindhdl;
31150Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_unbindhdl,
31160Sstevel@tonic-gate 		    "bus_dma_unbindhdl");
31170Sstevel@tonic-gate 	}
31180Sstevel@tonic-gate 
31190Sstevel@tonic-gate 	if (ddi_dma_flush == b->bus_dma_flush) {
31200Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_flush = pdevi->devi_bus_dma_flush;
31210Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_flush,
31220Sstevel@tonic-gate 		    "bus_dma_flush");
31230Sstevel@tonic-gate 	}
31240Sstevel@tonic-gate 
31250Sstevel@tonic-gate 	if (ddi_dma_win == b->bus_dma_win) {
31260Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_win = pdevi->devi_bus_dma_win;
31270Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_win,
31280Sstevel@tonic-gate 		    "bus_dma_win");
31290Sstevel@tonic-gate 	}
31300Sstevel@tonic-gate 
31310Sstevel@tonic-gate 	if (ddi_dma_mctl == b->bus_dma_ctl) {
31320Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_ctl = pdevi->devi_bus_dma_ctl;
31330Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_ctl, "bus_dma_ctl");
31340Sstevel@tonic-gate 	}
31350Sstevel@tonic-gate 
31360Sstevel@tonic-gate 	if (ddi_ctlops == b->bus_ctl) {
31370Sstevel@tonic-gate 		DEVI(devi)->devi_bus_ctl = pdevi->devi_bus_ctl;
31380Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_ctl, "bus_ctl");
31390Sstevel@tonic-gate 	}
31400Sstevel@tonic-gate }
31410Sstevel@tonic-gate 
31420Sstevel@tonic-gate #define	MIN_DEVINFO_LOG_SIZE	max_ncpus
31430Sstevel@tonic-gate #define	MAX_DEVINFO_LOG_SIZE	max_ncpus * 10
31440Sstevel@tonic-gate 
31450Sstevel@tonic-gate static void
31460Sstevel@tonic-gate da_log_init()
31470Sstevel@tonic-gate {
31480Sstevel@tonic-gate 	devinfo_log_header_t *dh;
31490Sstevel@tonic-gate 	int logsize = devinfo_log_size;
31500Sstevel@tonic-gate 
31510Sstevel@tonic-gate 	if (logsize == 0)
31520Sstevel@tonic-gate 		logsize = MIN_DEVINFO_LOG_SIZE;
31530Sstevel@tonic-gate 	else if (logsize > MAX_DEVINFO_LOG_SIZE)
31540Sstevel@tonic-gate 		logsize = MAX_DEVINFO_LOG_SIZE;
31550Sstevel@tonic-gate 
31560Sstevel@tonic-gate 	dh = kmem_alloc(logsize * PAGESIZE, KM_SLEEP);
31570Sstevel@tonic-gate 	mutex_init(&dh->dh_lock, NULL, MUTEX_DEFAULT, NULL);
31580Sstevel@tonic-gate 	dh->dh_max = ((logsize * PAGESIZE) - sizeof (*dh)) /
31590Sstevel@tonic-gate 	    sizeof (devinfo_audit_t) + 1;
31600Sstevel@tonic-gate 	dh->dh_curr = -1;
31610Sstevel@tonic-gate 	dh->dh_hits = 0;
31620Sstevel@tonic-gate 
31630Sstevel@tonic-gate 	devinfo_audit_log = dh;
31640Sstevel@tonic-gate }
31650Sstevel@tonic-gate 
31660Sstevel@tonic-gate /*
31670Sstevel@tonic-gate  * Log the stack trace in per-devinfo audit structure and also enter
31680Sstevel@tonic-gate  * it into a system wide log for recording the time history.
31690Sstevel@tonic-gate  */
31700Sstevel@tonic-gate static void
31710Sstevel@tonic-gate da_log_enter(dev_info_t *dip)
31720Sstevel@tonic-gate {
31730Sstevel@tonic-gate 	devinfo_audit_t *da_log, *da = DEVI(dip)->devi_audit;
31740Sstevel@tonic-gate 	devinfo_log_header_t *dh = devinfo_audit_log;
31750Sstevel@tonic-gate 
31760Sstevel@tonic-gate 	if (devinfo_audit_log == NULL)
31770Sstevel@tonic-gate 		return;
31780Sstevel@tonic-gate 
31790Sstevel@tonic-gate 	ASSERT(da != NULL);
31800Sstevel@tonic-gate 
31810Sstevel@tonic-gate 	da->da_devinfo = dip;
31820Sstevel@tonic-gate 	da->da_timestamp = gethrtime();
31830Sstevel@tonic-gate 	da->da_thread = curthread;
31840Sstevel@tonic-gate 	da->da_node_state = DEVI(dip)->devi_node_state;
31850Sstevel@tonic-gate 	da->da_device_state = DEVI(dip)->devi_state;
31860Sstevel@tonic-gate 	da->da_depth = getpcstack(da->da_stack, DDI_STACK_DEPTH);
31870Sstevel@tonic-gate 
31880Sstevel@tonic-gate 	/*
31890Sstevel@tonic-gate 	 * Copy into common log and note the location for tracing history
31900Sstevel@tonic-gate 	 */
31910Sstevel@tonic-gate 	mutex_enter(&dh->dh_lock);
31920Sstevel@tonic-gate 	dh->dh_hits++;
31930Sstevel@tonic-gate 	dh->dh_curr++;
31940Sstevel@tonic-gate 	if (dh->dh_curr >= dh->dh_max)
31950Sstevel@tonic-gate 		dh->dh_curr -= dh->dh_max;
31960Sstevel@tonic-gate 	da_log = &dh->dh_entry[dh->dh_curr];
31970Sstevel@tonic-gate 	mutex_exit(&dh->dh_lock);
31980Sstevel@tonic-gate 
31990Sstevel@tonic-gate 	bcopy(da, da_log, sizeof (devinfo_audit_t));
32000Sstevel@tonic-gate 	da->da_lastlog = da_log;
32010Sstevel@tonic-gate }
32020Sstevel@tonic-gate 
32030Sstevel@tonic-gate static void
32040Sstevel@tonic-gate attach_drivers()
32050Sstevel@tonic-gate {
32060Sstevel@tonic-gate 	int i;
32070Sstevel@tonic-gate 	for (i = 0; i < devcnt; i++) {
32080Sstevel@tonic-gate 		struct devnames *dnp = &devnamesp[i];
32090Sstevel@tonic-gate 		if ((dnp->dn_flags & DN_FORCE_ATTACH) &&
32100Sstevel@tonic-gate 		    (ddi_hold_installed_driver((major_t)i) != NULL))
32110Sstevel@tonic-gate 			ddi_rele_driver((major_t)i);
32120Sstevel@tonic-gate 	}
32130Sstevel@tonic-gate }
32140Sstevel@tonic-gate 
32150Sstevel@tonic-gate /*
32160Sstevel@tonic-gate  * Launch a thread to force attach drivers. This avoids penalty on boot time.
32170Sstevel@tonic-gate  */
32180Sstevel@tonic-gate void
32190Sstevel@tonic-gate i_ddi_forceattach_drivers()
32200Sstevel@tonic-gate {
32210Sstevel@tonic-gate 	/*
32220Sstevel@tonic-gate 	 * On i386, the USB drivers need to load and take over from the
32230Sstevel@tonic-gate 	 * SMM BIOS drivers ASAP after consconfig(), so make sure they
32240Sstevel@tonic-gate 	 * get loaded right here rather than letting the thread do it.
32250Sstevel@tonic-gate 	 *
32260Sstevel@tonic-gate 	 * The order here is important.  EHCI must be loaded first, as
32270Sstevel@tonic-gate 	 * we have observed many systems on which hangs occur if the
32280Sstevel@tonic-gate 	 * {U,O}HCI companion controllers take over control from the BIOS
32290Sstevel@tonic-gate 	 * before EHCI does.  These hangs are also caused by BIOSes leaving
32300Sstevel@tonic-gate 	 * interrupt-on-port-change enabled in the ehci controller, so that
32310Sstevel@tonic-gate 	 * when uhci/ohci reset themselves, it induces a port change on
32320Sstevel@tonic-gate 	 * the ehci companion controller.  Since there's no interrupt handler
32330Sstevel@tonic-gate 	 * installed at the time, the moment that interrupt is unmasked, an
32340Sstevel@tonic-gate 	 * interrupt storm will occur.  All this is averted when ehci is
32350Sstevel@tonic-gate 	 * loaded first.  And now you know..... the REST of the story.
32360Sstevel@tonic-gate 	 *
32370Sstevel@tonic-gate 	 * Regardless of platform, ehci needs to initialize first to avoid
32380Sstevel@tonic-gate 	 * unnecessary connects and disconnects on the companion controller
32390Sstevel@tonic-gate 	 * when ehci sets up the routing.
32400Sstevel@tonic-gate 	 */
32410Sstevel@tonic-gate 	(void) ddi_hold_installed_driver(ddi_name_to_major("ehci"));
32420Sstevel@tonic-gate 	(void) ddi_hold_installed_driver(ddi_name_to_major("uhci"));
32430Sstevel@tonic-gate 	(void) ddi_hold_installed_driver(ddi_name_to_major("ohci"));
32440Sstevel@tonic-gate 
32451093Shiremath 	/*
32461093Shiremath 	 * Attach IB VHCI driver before the force-attach thread attaches the
32471093Shiremath 	 * IB HCA driver. IB HCA driver will fail if IB Nexus has not yet
32481093Shiremath 	 * been attached.
32491093Shiremath 	 */
32501093Shiremath 	(void) ddi_hold_installed_driver(ddi_name_to_major("ib"));
32511093Shiremath 
32520Sstevel@tonic-gate 	(void) thread_create(NULL, 0, (void (*)())attach_drivers, NULL, 0, &p0,
32530Sstevel@tonic-gate 	    TS_RUN, minclsyspri);
32540Sstevel@tonic-gate }
32550Sstevel@tonic-gate 
32560Sstevel@tonic-gate /*
32570Sstevel@tonic-gate  * This is a private DDI interface for optimizing boot performance.
32580Sstevel@tonic-gate  * I/O subsystem initialization is considered complete when devfsadm
32590Sstevel@tonic-gate  * is executed.
32600Sstevel@tonic-gate  *
32612621Sllai1  * NOTE: The start of syseventd happens to be a convenient indicator
32622621Sllai1  *	of the completion of I/O initialization during boot.
32630Sstevel@tonic-gate  *	The implementation should be replaced by something more robust.
32640Sstevel@tonic-gate  */
32650Sstevel@tonic-gate int
32660Sstevel@tonic-gate i_ddi_io_initialized()
32670Sstevel@tonic-gate {
32680Sstevel@tonic-gate 	extern int sysevent_daemon_init;
32690Sstevel@tonic-gate 	return (sysevent_daemon_init);
32700Sstevel@tonic-gate }
32710Sstevel@tonic-gate 
32722621Sllai1 /*
32732621Sllai1  * May be used to determine system boot state
32742621Sllai1  * "Available" means the system is for the most part up
32752621Sllai1  * and initialized, with all system services either up or
32762621Sllai1  * capable of being started.  This state is set by devfsadm
32772621Sllai1  * during the boot process.  The /dev filesystem infers
32782621Sllai1  * from this when implicit reconfig can be performed,
32792621Sllai1  * ie, devfsadm can be invoked.  Please avoid making
32802621Sllai1  * further use of this unless it's really necessary.
32812621Sllai1  */
32822621Sllai1 int
32832621Sllai1 i_ddi_sysavail()
32842621Sllai1 {
32852621Sllai1 	return (devname_state & DS_SYSAVAIL);
32862621Sllai1 }
32872621Sllai1 
32882621Sllai1 /*
32892621Sllai1  * May be used to determine if boot is a reconfigure boot.
32902621Sllai1  */
32912621Sllai1 int
32922621Sllai1 i_ddi_reconfig()
32932621Sllai1 {
32942621Sllai1 	return (devname_state & DS_RECONFIG);
32952621Sllai1 }
32962621Sllai1 
32972621Sllai1 /*
32982621Sllai1  * Note system services are up, inform /dev.
32992621Sllai1  */
33002621Sllai1 void
33012621Sllai1 i_ddi_set_sysavail()
33022621Sllai1 {
33032621Sllai1 	if ((devname_state & DS_SYSAVAIL) == 0) {
33042621Sllai1 		devname_state |= DS_SYSAVAIL;
33052621Sllai1 		sdev_devstate_change();
33062621Sllai1 	}
33072621Sllai1 }
33082621Sllai1 
33092621Sllai1 /*
33102621Sllai1  * Note reconfiguration boot, inform /dev.
33112621Sllai1  */
33122621Sllai1 void
33132621Sllai1 i_ddi_set_reconfig()
33142621Sllai1 {
33152621Sllai1 	if ((devname_state & DS_RECONFIG) == 0) {
33162621Sllai1 		devname_state |= DS_RECONFIG;
33172621Sllai1 		sdev_devstate_change();
33182621Sllai1 	}
33192621Sllai1 }
33202621Sllai1 
33210Sstevel@tonic-gate 
33220Sstevel@tonic-gate /*
33230Sstevel@tonic-gate  * device tree walking
33240Sstevel@tonic-gate  */
33250Sstevel@tonic-gate 
33260Sstevel@tonic-gate struct walk_elem {
33270Sstevel@tonic-gate 	struct walk_elem *next;
33280Sstevel@tonic-gate 	dev_info_t *dip;
33290Sstevel@tonic-gate };
33300Sstevel@tonic-gate 
33310Sstevel@tonic-gate static void
33320Sstevel@tonic-gate free_list(struct walk_elem *list)
33330Sstevel@tonic-gate {
33340Sstevel@tonic-gate 	while (list) {
33350Sstevel@tonic-gate 		struct walk_elem *next = list->next;
33360Sstevel@tonic-gate 		kmem_free(list, sizeof (*list));
33370Sstevel@tonic-gate 		list = next;
33380Sstevel@tonic-gate 	}
33390Sstevel@tonic-gate }
33400Sstevel@tonic-gate 
33410Sstevel@tonic-gate static void
33420Sstevel@tonic-gate append_node(struct walk_elem **list, dev_info_t *dip)
33430Sstevel@tonic-gate {
33440Sstevel@tonic-gate 	struct walk_elem *tail;
33450Sstevel@tonic-gate 	struct walk_elem *elem = kmem_alloc(sizeof (*elem), KM_SLEEP);
33460Sstevel@tonic-gate 
33470Sstevel@tonic-gate 	elem->next = NULL;
33480Sstevel@tonic-gate 	elem->dip = dip;
33490Sstevel@tonic-gate 
33500Sstevel@tonic-gate 	if (*list == NULL) {
33510Sstevel@tonic-gate 		*list = elem;
33520Sstevel@tonic-gate 		return;
33530Sstevel@tonic-gate 	}
33540Sstevel@tonic-gate 
33550Sstevel@tonic-gate 	tail = *list;
33560Sstevel@tonic-gate 	while (tail->next)
33570Sstevel@tonic-gate 		tail = tail->next;
33580Sstevel@tonic-gate 
33590Sstevel@tonic-gate 	tail->next = elem;
33600Sstevel@tonic-gate }
33610Sstevel@tonic-gate 
33620Sstevel@tonic-gate /*
33630Sstevel@tonic-gate  * The implementation of ddi_walk_devs().
33640Sstevel@tonic-gate  */
33650Sstevel@tonic-gate static int
33660Sstevel@tonic-gate walk_devs(dev_info_t *dip, int (*f)(dev_info_t *, void *), void *arg,
33670Sstevel@tonic-gate     int do_locking)
33680Sstevel@tonic-gate {
33690Sstevel@tonic-gate 	struct walk_elem *head = NULL;
33700Sstevel@tonic-gate 
33710Sstevel@tonic-gate 	/*
33720Sstevel@tonic-gate 	 * Do it in two passes. First pass invoke callback on each
33730Sstevel@tonic-gate 	 * dip on the sibling list. Second pass invoke callback on
33740Sstevel@tonic-gate 	 * children of each dip.
33750Sstevel@tonic-gate 	 */
33760Sstevel@tonic-gate 	while (dip) {
33770Sstevel@tonic-gate 		switch ((*f)(dip, arg)) {
33780Sstevel@tonic-gate 		case DDI_WALK_TERMINATE:
33790Sstevel@tonic-gate 			free_list(head);
33800Sstevel@tonic-gate 			return (DDI_WALK_TERMINATE);
33810Sstevel@tonic-gate 
33820Sstevel@tonic-gate 		case DDI_WALK_PRUNESIB:
33830Sstevel@tonic-gate 			/* ignore sibling by setting dip to NULL */
33840Sstevel@tonic-gate 			append_node(&head, dip);
33850Sstevel@tonic-gate 			dip = NULL;
33860Sstevel@tonic-gate 			break;
33870Sstevel@tonic-gate 
33880Sstevel@tonic-gate 		case DDI_WALK_PRUNECHILD:
33890Sstevel@tonic-gate 			/* don't worry about children */
33900Sstevel@tonic-gate 			dip = ddi_get_next_sibling(dip);
33910Sstevel@tonic-gate 			break;
33920Sstevel@tonic-gate 
33930Sstevel@tonic-gate 		case DDI_WALK_CONTINUE:
33940Sstevel@tonic-gate 		default:
33950Sstevel@tonic-gate 			append_node(&head, dip);
33960Sstevel@tonic-gate 			dip = ddi_get_next_sibling(dip);
33970Sstevel@tonic-gate 			break;
33980Sstevel@tonic-gate 		}
33990Sstevel@tonic-gate 
34000Sstevel@tonic-gate 	}
34010Sstevel@tonic-gate 
34020Sstevel@tonic-gate 	/* second pass */
34030Sstevel@tonic-gate 	while (head) {
34040Sstevel@tonic-gate 		int circ;
34050Sstevel@tonic-gate 		struct walk_elem *next = head->next;
34060Sstevel@tonic-gate 
34070Sstevel@tonic-gate 		if (do_locking)
34080Sstevel@tonic-gate 			ndi_devi_enter(head->dip, &circ);
34090Sstevel@tonic-gate 		if (walk_devs(ddi_get_child(head->dip), f, arg, do_locking) ==
34100Sstevel@tonic-gate 		    DDI_WALK_TERMINATE) {
34110Sstevel@tonic-gate 			if (do_locking)
34120Sstevel@tonic-gate 				ndi_devi_exit(head->dip, circ);
34130Sstevel@tonic-gate 			free_list(head);
34140Sstevel@tonic-gate 			return (DDI_WALK_TERMINATE);
34150Sstevel@tonic-gate 		}
34160Sstevel@tonic-gate 		if (do_locking)
34170Sstevel@tonic-gate 			ndi_devi_exit(head->dip, circ);
34180Sstevel@tonic-gate 		kmem_free(head, sizeof (*head));
34190Sstevel@tonic-gate 		head = next;
34200Sstevel@tonic-gate 	}
34210Sstevel@tonic-gate 
34220Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
34230Sstevel@tonic-gate }
34240Sstevel@tonic-gate 
34250Sstevel@tonic-gate /*
34260Sstevel@tonic-gate  * This general-purpose routine traverses the tree of dev_info nodes,
34270Sstevel@tonic-gate  * starting from the given node, and calls the given function for each
34280Sstevel@tonic-gate  * node that it finds with the current node and the pointer arg (which
34290Sstevel@tonic-gate  * can point to a structure of information that the function
34300Sstevel@tonic-gate  * needs) as arguments.
34310Sstevel@tonic-gate  *
34320Sstevel@tonic-gate  * It does the walk a layer at a time, not depth-first. The given function
34330Sstevel@tonic-gate  * must return one of the following values:
34340Sstevel@tonic-gate  *	DDI_WALK_CONTINUE
34350Sstevel@tonic-gate  *	DDI_WALK_PRUNESIB
34360Sstevel@tonic-gate  *	DDI_WALK_PRUNECHILD
34370Sstevel@tonic-gate  *	DDI_WALK_TERMINATE
34380Sstevel@tonic-gate  *
34390Sstevel@tonic-gate  * N.B. Since we walk the sibling list, the caller must ensure that
34400Sstevel@tonic-gate  *	the parent of dip is held against changes, unless the parent
34410Sstevel@tonic-gate  *	is rootnode.  ndi_devi_enter() on the parent is sufficient.
34420Sstevel@tonic-gate  *
34430Sstevel@tonic-gate  *	To avoid deadlock situations, caller must not attempt to
34440Sstevel@tonic-gate  *	configure/unconfigure/remove device node in (*f)(), nor should
34452155Scth  *	it attempt to recurse on other nodes in the system. Any
34462155Scth  *	ndi_devi_enter() done by (*f)() must occur 'at-or-below' the
34472155Scth  *	node entered prior to ddi_walk_devs(). Furthermore, if (*f)()
34482155Scth  *	does any multi-threading (in framework *or* in driver) then the
34492155Scth  *	ndi_devi_enter() calls done by dependent threads must be
34502155Scth  *	'strictly-below'.
34510Sstevel@tonic-gate  *
34520Sstevel@tonic-gate  *	This is not callable from device autoconfiguration routines.
34530Sstevel@tonic-gate  *	They include, but not limited to, _init(9e), _fini(9e), probe(9e),
34540Sstevel@tonic-gate  *	attach(9e), and detach(9e).
34550Sstevel@tonic-gate  */
34560Sstevel@tonic-gate 
34570Sstevel@tonic-gate void
34580Sstevel@tonic-gate ddi_walk_devs(dev_info_t *dip, int (*f)(dev_info_t *, void *), void *arg)
34590Sstevel@tonic-gate {
34600Sstevel@tonic-gate 
34610Sstevel@tonic-gate 	ASSERT(dip == NULL || ddi_get_parent(dip) == NULL ||
34624950Scth 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
34630Sstevel@tonic-gate 
34640Sstevel@tonic-gate 	(void) walk_devs(dip, f, arg, 1);
34650Sstevel@tonic-gate }
34660Sstevel@tonic-gate 
34670Sstevel@tonic-gate /*
34680Sstevel@tonic-gate  * This is a general-purpose routine traverses the per-driver list
34690Sstevel@tonic-gate  * and calls the given function for each node. must return one of
34700Sstevel@tonic-gate  * the following values:
34710Sstevel@tonic-gate  *	DDI_WALK_CONTINUE
34720Sstevel@tonic-gate  *	DDI_WALK_TERMINATE
34730Sstevel@tonic-gate  *
34740Sstevel@tonic-gate  * N.B. The same restrictions from ddi_walk_devs() apply.
34750Sstevel@tonic-gate  */
34760Sstevel@tonic-gate 
34770Sstevel@tonic-gate void
34780Sstevel@tonic-gate e_ddi_walk_driver(char *drv, int (*f)(dev_info_t *, void *), void *arg)
34790Sstevel@tonic-gate {
34800Sstevel@tonic-gate 	major_t major;
34810Sstevel@tonic-gate 	struct devnames *dnp;
34820Sstevel@tonic-gate 	dev_info_t *dip;
34830Sstevel@tonic-gate 
34840Sstevel@tonic-gate 	major = ddi_name_to_major(drv);
34857009Scth 	if (major == DDI_MAJOR_T_NONE)
34860Sstevel@tonic-gate 		return;
34870Sstevel@tonic-gate 
34880Sstevel@tonic-gate 	dnp = &devnamesp[major];
34890Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
34900Sstevel@tonic-gate 	dip = dnp->dn_head;
34910Sstevel@tonic-gate 	while (dip) {
34920Sstevel@tonic-gate 		ndi_hold_devi(dip);
34930Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
34940Sstevel@tonic-gate 		if ((*f)(dip, arg) == DDI_WALK_TERMINATE) {
34950Sstevel@tonic-gate 			ndi_rele_devi(dip);
34960Sstevel@tonic-gate 			return;
34970Sstevel@tonic-gate 		}
34980Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
34990Sstevel@tonic-gate 		ndi_rele_devi(dip);
35000Sstevel@tonic-gate 		dip = ddi_get_next(dip);
35010Sstevel@tonic-gate 	}
35020Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
35030Sstevel@tonic-gate }
35040Sstevel@tonic-gate 
35050Sstevel@tonic-gate /*
35060Sstevel@tonic-gate  * argument to i_find_devi, a devinfo node search callback function.
35070Sstevel@tonic-gate  */
35080Sstevel@tonic-gate struct match_info {
35090Sstevel@tonic-gate 	dev_info_t	*dip;		/* result */
35100Sstevel@tonic-gate 	char		*nodename;	/* if non-null, nodename must match */
35110Sstevel@tonic-gate 	int		instance;	/* if != -1, instance must match */
35121333Scth 	int		attached;	/* if != 0, i_ddi_devi_attached() */
35130Sstevel@tonic-gate };
35140Sstevel@tonic-gate 
35150Sstevel@tonic-gate static int
35160Sstevel@tonic-gate i_find_devi(dev_info_t *dip, void *arg)
35170Sstevel@tonic-gate {
35180Sstevel@tonic-gate 	struct match_info *info = (struct match_info *)arg;
35190Sstevel@tonic-gate 
35200Sstevel@tonic-gate 	if (((info->nodename == NULL) ||
35214950Scth 	    (strcmp(ddi_node_name(dip), info->nodename) == 0)) &&
35220Sstevel@tonic-gate 	    ((info->instance == -1) ||
35234950Scth 	    (ddi_get_instance(dip) == info->instance)) &&
35241333Scth 	    ((info->attached == 0) || i_ddi_devi_attached(dip))) {
35250Sstevel@tonic-gate 		info->dip = dip;
35260Sstevel@tonic-gate 		ndi_hold_devi(dip);
35270Sstevel@tonic-gate 		return (DDI_WALK_TERMINATE);
35280Sstevel@tonic-gate 	}
35290Sstevel@tonic-gate 
35300Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
35310Sstevel@tonic-gate }
35320Sstevel@tonic-gate 
35330Sstevel@tonic-gate /*
35340Sstevel@tonic-gate  * Find dip with a known node name and instance and return with it held
35350Sstevel@tonic-gate  */
35360Sstevel@tonic-gate dev_info_t *
35370Sstevel@tonic-gate ddi_find_devinfo(char *nodename, int instance, int attached)
35380Sstevel@tonic-gate {
35390Sstevel@tonic-gate 	struct match_info	info;
35400Sstevel@tonic-gate 
35410Sstevel@tonic-gate 	info.nodename = nodename;
35420Sstevel@tonic-gate 	info.instance = instance;
35430Sstevel@tonic-gate 	info.attached = attached;
35440Sstevel@tonic-gate 	info.dip = NULL;
35450Sstevel@tonic-gate 
35460Sstevel@tonic-gate 	ddi_walk_devs(ddi_root_node(), i_find_devi, &info);
35470Sstevel@tonic-gate 	return (info.dip);
35480Sstevel@tonic-gate }
35490Sstevel@tonic-gate 
35500Sstevel@tonic-gate /*
35510Sstevel@tonic-gate  * Parse for name, addr, and minor names. Some args may be NULL.
35520Sstevel@tonic-gate  */
35530Sstevel@tonic-gate void
35540Sstevel@tonic-gate i_ddi_parse_name(char *name, char **nodename, char **addrname, char **minorname)
35550Sstevel@tonic-gate {
35560Sstevel@tonic-gate 	char *cp;
35570Sstevel@tonic-gate 	static char nulladdrname[] = "";
35580Sstevel@tonic-gate 
35590Sstevel@tonic-gate 	/* default values */
35600Sstevel@tonic-gate 	if (nodename)
35610Sstevel@tonic-gate 		*nodename = name;
35620Sstevel@tonic-gate 	if (addrname)
35630Sstevel@tonic-gate 		*addrname = nulladdrname;
35640Sstevel@tonic-gate 	if (minorname)
35650Sstevel@tonic-gate 		*minorname = NULL;
35660Sstevel@tonic-gate 
35670Sstevel@tonic-gate 	cp = name;
35680Sstevel@tonic-gate 	while (*cp != '\0') {
35690Sstevel@tonic-gate 		if (addrname && *cp == '@') {
35700Sstevel@tonic-gate 			*addrname = cp + 1;
35710Sstevel@tonic-gate 			*cp = '\0';
35720Sstevel@tonic-gate 		} else if (minorname && *cp == ':') {
35730Sstevel@tonic-gate 			*minorname = cp + 1;
35740Sstevel@tonic-gate 			*cp = '\0';
35750Sstevel@tonic-gate 		}
35760Sstevel@tonic-gate 		++cp;
35770Sstevel@tonic-gate 	}
35780Sstevel@tonic-gate }
35790Sstevel@tonic-gate 
35800Sstevel@tonic-gate static char *
35810Sstevel@tonic-gate child_path_to_driver(dev_info_t *parent, char *child_name, char *unit_address)
35820Sstevel@tonic-gate {
35830Sstevel@tonic-gate 	char *p, *drvname = NULL;
35840Sstevel@tonic-gate 	major_t maj;
35850Sstevel@tonic-gate 
35860Sstevel@tonic-gate 	/*
35870Sstevel@tonic-gate 	 * Construct the pathname and ask the implementation
35880Sstevel@tonic-gate 	 * if it can do a driver = f(pathname) for us, if not
35890Sstevel@tonic-gate 	 * we'll just default to using the node-name that
35900Sstevel@tonic-gate 	 * was given to us.  We want to do this first to
35910Sstevel@tonic-gate 	 * allow the platform to use 'generic' names for
35920Sstevel@tonic-gate 	 * legacy device drivers.
35930Sstevel@tonic-gate 	 */
35940Sstevel@tonic-gate 	p = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
35950Sstevel@tonic-gate 	(void) ddi_pathname(parent, p);
35960Sstevel@tonic-gate 	(void) strcat(p, "/");
35970Sstevel@tonic-gate 	(void) strcat(p, child_name);
35980Sstevel@tonic-gate 	if (unit_address && *unit_address) {
35990Sstevel@tonic-gate 		(void) strcat(p, "@");
36000Sstevel@tonic-gate 		(void) strcat(p, unit_address);
36010Sstevel@tonic-gate 	}
36020Sstevel@tonic-gate 
36030Sstevel@tonic-gate 	/*
36040Sstevel@tonic-gate 	 * Get the binding. If there is none, return the child_name
36050Sstevel@tonic-gate 	 * and let the caller deal with it.
36060Sstevel@tonic-gate 	 */
36070Sstevel@tonic-gate 	maj = path_to_major(p);
36080Sstevel@tonic-gate 
36090Sstevel@tonic-gate 	kmem_free(p, MAXPATHLEN);
36100Sstevel@tonic-gate 
36117009Scth 	if (maj != DDI_MAJOR_T_NONE)
36120Sstevel@tonic-gate 		drvname = ddi_major_to_name(maj);
36130Sstevel@tonic-gate 	if (drvname == NULL)
36140Sstevel@tonic-gate 		drvname = child_name;
36150Sstevel@tonic-gate 
36160Sstevel@tonic-gate 	return (drvname);
36170Sstevel@tonic-gate }
36180Sstevel@tonic-gate 
36190Sstevel@tonic-gate 
3620*7613SVikram.Hegde@Sun.COM #define	PCI_EX_CLASS	"pciexclass"
3621*7613SVikram.Hegde@Sun.COM #define	PCI_EX		"pciex"
3622*7613SVikram.Hegde@Sun.COM #define	PCI_CLASS	"pciclass"
3623*7613SVikram.Hegde@Sun.COM #define	PCI		"pci"
3624*7613SVikram.Hegde@Sun.COM 
3625*7613SVikram.Hegde@Sun.COM int
3626*7613SVikram.Hegde@Sun.COM ddi_is_pci_dip(dev_info_t *dip)
3627*7613SVikram.Hegde@Sun.COM {
3628*7613SVikram.Hegde@Sun.COM 	char	*prop = NULL;
3629*7613SVikram.Hegde@Sun.COM 
3630*7613SVikram.Hegde@Sun.COM 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
3631*7613SVikram.Hegde@Sun.COM 	    "compatible", &prop) == DDI_PROP_SUCCESS) {
3632*7613SVikram.Hegde@Sun.COM 		ASSERT(prop);
3633*7613SVikram.Hegde@Sun.COM 		if (strncmp(prop, PCI_EX_CLASS, sizeof (PCI_EX_CLASS) - 1)
3634*7613SVikram.Hegde@Sun.COM 		    == 0 ||
3635*7613SVikram.Hegde@Sun.COM 		    strncmp(prop, PCI_EX, sizeof (PCI_EX)- 1)
3636*7613SVikram.Hegde@Sun.COM 		    == 0 ||
3637*7613SVikram.Hegde@Sun.COM 		    strncmp(prop, PCI_CLASS, sizeof (PCI_CLASS) - 1)
3638*7613SVikram.Hegde@Sun.COM 		    == 0 ||
3639*7613SVikram.Hegde@Sun.COM 		    strncmp(prop, PCI, sizeof (PCI) - 1)
3640*7613SVikram.Hegde@Sun.COM 		    == 0) {
3641*7613SVikram.Hegde@Sun.COM 			ddi_prop_free(prop);
3642*7613SVikram.Hegde@Sun.COM 			return (1);
3643*7613SVikram.Hegde@Sun.COM 		}
3644*7613SVikram.Hegde@Sun.COM 	}
3645*7613SVikram.Hegde@Sun.COM 
3646*7613SVikram.Hegde@Sun.COM 	if (prop != NULL) {
3647*7613SVikram.Hegde@Sun.COM 		ddi_prop_free(prop);
3648*7613SVikram.Hegde@Sun.COM 	}
3649*7613SVikram.Hegde@Sun.COM 
3650*7613SVikram.Hegde@Sun.COM 	return (0);
3651*7613SVikram.Hegde@Sun.COM }
3652*7613SVikram.Hegde@Sun.COM 
36530Sstevel@tonic-gate /*
36540Sstevel@tonic-gate  * Given the pathname of a device, fill in the dev_info_t value and/or the
36550Sstevel@tonic-gate  * dev_t value and/or the spectype, depending on which parameters are non-NULL.
36560Sstevel@tonic-gate  * If there is an error, this function returns -1.
36570Sstevel@tonic-gate  *
36580Sstevel@tonic-gate  * NOTE: If this function returns the dev_info_t structure, then it
36590Sstevel@tonic-gate  * does so with a hold on the devi. Caller should ensure that they get
36600Sstevel@tonic-gate  * decremented via ddi_release_devi() or ndi_rele_devi();
36610Sstevel@tonic-gate  *
36620Sstevel@tonic-gate  * This function can be invoked in the boot case for a pathname without
36630Sstevel@tonic-gate  * device argument (:xxxx), traditionally treated as a minor name.
36640Sstevel@tonic-gate  * In this case, we do the following
36650Sstevel@tonic-gate  * (1) search the minor node of type DDM_DEFAULT.
36660Sstevel@tonic-gate  * (2) if no DDM_DEFAULT minor exists, then the first non-alias minor is chosen.
36670Sstevel@tonic-gate  * (3) if neither exists, a dev_t is faked with minor number = instance.
36680Sstevel@tonic-gate  * As of S9 FCS, no instance of #1 exists. #2 is used by several platforms
36690Sstevel@tonic-gate  * to default the boot partition to :a possibly by other OBP definitions.
36700Sstevel@tonic-gate  * #3 is used for booting off network interfaces, most SPARC network
36710Sstevel@tonic-gate  * drivers support Style-2 only, so only DDM_ALIAS minor exists.
36720Sstevel@tonic-gate  *
36730Sstevel@tonic-gate  * It is possible for OBP to present device args at the end of the path as
36740Sstevel@tonic-gate  * well as in the middle. For example, with IB the following strings are
36750Sstevel@tonic-gate  * valid boot paths.
36760Sstevel@tonic-gate  *	a /pci@8,700000/ib@1,2:port=1,pkey=ff,dhcp,...
36770Sstevel@tonic-gate  *	b /pci@8,700000/ib@1,1:port=1/ioc@xxxxxx,yyyyyyy:dhcp
36780Sstevel@tonic-gate  * Case (a), we first look for minor node "port=1,pkey...".
36790Sstevel@tonic-gate  * Failing that, we will pass "port=1,pkey..." to the bus_config
36800Sstevel@tonic-gate  * entry point of ib (HCA) driver.
36810Sstevel@tonic-gate  * Case (b), configure ib@1,1 as usual. Then invoke ib's bus_config
36820Sstevel@tonic-gate  * with argument "ioc@xxxxxxx,yyyyyyy:port=1". After configuring
36830Sstevel@tonic-gate  * the ioc, look for minor node dhcp. If not found, pass ":dhcp"
36840Sstevel@tonic-gate  * to ioc's bus_config entry point.
36850Sstevel@tonic-gate  */
3686*7613SVikram.Hegde@Sun.COM static int
3687*7613SVikram.Hegde@Sun.COM parse_pathname(char *pathname,
3688*7613SVikram.Hegde@Sun.COM 	dev_info_t **dipp, dev_t *devtp, int *spectypep, dev_info_t **pci_dipp)
36890Sstevel@tonic-gate {
36907224Scth 	int			error;
36917224Scth 	dev_info_t		*parent, *child;
36927224Scth 	struct pathname		pn;
36937224Scth 	char			*component, *config_name;
36947224Scth 	char			*minorname = NULL;
36957224Scth 	char			*prev_minor = NULL;
36967224Scth 	dev_t			devt = NODEV;
36977224Scth 	int			spectype;
36987224Scth 	struct ddi_minor_data	*dmn;
36997224Scth 	int			circ;
37000Sstevel@tonic-gate 
3701*7613SVikram.Hegde@Sun.COM 	if (pci_dipp)
3702*7613SVikram.Hegde@Sun.COM 		*pci_dipp = NULL;
3703*7613SVikram.Hegde@Sun.COM 
37040Sstevel@tonic-gate 	if (*pathname != '/')
37050Sstevel@tonic-gate 		return (EINVAL);
37060Sstevel@tonic-gate 	parent = ddi_root_node();	/* Begin at the top of the tree */
37070Sstevel@tonic-gate 
37080Sstevel@tonic-gate 	if (error = pn_get(pathname, UIO_SYSSPACE, &pn))
37090Sstevel@tonic-gate 		return (error);
37100Sstevel@tonic-gate 	pn_skipslash(&pn);
37110Sstevel@tonic-gate 
37121333Scth 	ASSERT(i_ddi_devi_attached(parent));
37130Sstevel@tonic-gate 	ndi_hold_devi(parent);
37140Sstevel@tonic-gate 
37150Sstevel@tonic-gate 	component = kmem_alloc(MAXNAMELEN, KM_SLEEP);
37160Sstevel@tonic-gate 	config_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
37170Sstevel@tonic-gate 
37180Sstevel@tonic-gate 	while (pn_pathleft(&pn)) {
37190Sstevel@tonic-gate 		/* remember prev minor (:xxx) in the middle of path */
37200Sstevel@tonic-gate 		if (minorname)
37210Sstevel@tonic-gate 			prev_minor = i_ddi_strdup(minorname, KM_SLEEP);
37220Sstevel@tonic-gate 
37230Sstevel@tonic-gate 		/* Get component and chop off minorname */
37240Sstevel@tonic-gate 		(void) pn_getcomponent(&pn, component);
37250Sstevel@tonic-gate 		i_ddi_parse_name(component, NULL, NULL, &minorname);
37260Sstevel@tonic-gate 
37270Sstevel@tonic-gate 		if (prev_minor == NULL) {
37280Sstevel@tonic-gate 			(void) snprintf(config_name, MAXNAMELEN, "%s",
37290Sstevel@tonic-gate 			    component);
37300Sstevel@tonic-gate 		} else {
37310Sstevel@tonic-gate 			(void) snprintf(config_name, MAXNAMELEN, "%s:%s",
37320Sstevel@tonic-gate 			    component, prev_minor);
37330Sstevel@tonic-gate 			kmem_free(prev_minor, strlen(prev_minor) + 1);
37340Sstevel@tonic-gate 			prev_minor = NULL;
37350Sstevel@tonic-gate 		}
37360Sstevel@tonic-gate 
37370Sstevel@tonic-gate 		/*
37380Sstevel@tonic-gate 		 * Find and configure the child
37390Sstevel@tonic-gate 		 */
37400Sstevel@tonic-gate 		if (ndi_devi_config_one(parent, config_name, &child,
37410Sstevel@tonic-gate 		    NDI_PROMNAME | NDI_NO_EVENT) != NDI_SUCCESS) {
37420Sstevel@tonic-gate 			ndi_rele_devi(parent);
37430Sstevel@tonic-gate 			pn_free(&pn);
37440Sstevel@tonic-gate 			kmem_free(component, MAXNAMELEN);
37450Sstevel@tonic-gate 			kmem_free(config_name, MAXNAMELEN);
3746*7613SVikram.Hegde@Sun.COM 			if (pci_dipp && *pci_dipp) {
3747*7613SVikram.Hegde@Sun.COM 				ndi_rele_devi(*pci_dipp);
3748*7613SVikram.Hegde@Sun.COM 				*pci_dipp = NULL;
3749*7613SVikram.Hegde@Sun.COM 			}
37500Sstevel@tonic-gate 			return (-1);
37510Sstevel@tonic-gate 		}
37520Sstevel@tonic-gate 
37531333Scth 		ASSERT(i_ddi_devi_attached(child));
37540Sstevel@tonic-gate 		ndi_rele_devi(parent);
37550Sstevel@tonic-gate 		parent = child;
37560Sstevel@tonic-gate 		pn_skipslash(&pn);
3757*7613SVikram.Hegde@Sun.COM 		if (pci_dipp) {
3758*7613SVikram.Hegde@Sun.COM 			if (ddi_is_pci_dip(child)) {
3759*7613SVikram.Hegde@Sun.COM 				ndi_hold_devi(child);
3760*7613SVikram.Hegde@Sun.COM 				if (*pci_dipp != NULL) {
3761*7613SVikram.Hegde@Sun.COM 					ndi_rele_devi(*pci_dipp);
3762*7613SVikram.Hegde@Sun.COM 				}
3763*7613SVikram.Hegde@Sun.COM 				*pci_dipp = child;
3764*7613SVikram.Hegde@Sun.COM 			}
3765*7613SVikram.Hegde@Sun.COM 		}
37660Sstevel@tonic-gate 	}
37670Sstevel@tonic-gate 
37680Sstevel@tonic-gate 	/*
37690Sstevel@tonic-gate 	 * First look for a minor node matching minorname.
37700Sstevel@tonic-gate 	 * Failing that, try to pass minorname to bus_config().
37710Sstevel@tonic-gate 	 */
37720Sstevel@tonic-gate 	if (minorname && i_ddi_minorname_to_devtspectype(parent,
37730Sstevel@tonic-gate 	    minorname, &devt, &spectype) == DDI_FAILURE) {
37740Sstevel@tonic-gate 		(void) snprintf(config_name, MAXNAMELEN, "%s", minorname);
37750Sstevel@tonic-gate 		if (ndi_devi_config_obp_args(parent,
37760Sstevel@tonic-gate 		    config_name, &child, 0) != NDI_SUCCESS) {
37770Sstevel@tonic-gate 			ndi_rele_devi(parent);
37780Sstevel@tonic-gate 			pn_free(&pn);
37790Sstevel@tonic-gate 			kmem_free(component, MAXNAMELEN);
37800Sstevel@tonic-gate 			kmem_free(config_name, MAXNAMELEN);
37810Sstevel@tonic-gate 			NDI_CONFIG_DEBUG((CE_NOTE,
37820Sstevel@tonic-gate 			    "%s: minor node not found\n", pathname));
3783*7613SVikram.Hegde@Sun.COM 			if (pci_dipp && *pci_dipp) {
3784*7613SVikram.Hegde@Sun.COM 				ndi_rele_devi(*pci_dipp);
3785*7613SVikram.Hegde@Sun.COM 				*pci_dipp = NULL;
3786*7613SVikram.Hegde@Sun.COM 			}
37870Sstevel@tonic-gate 			return (-1);
37880Sstevel@tonic-gate 		}
37890Sstevel@tonic-gate 		minorname = NULL;	/* look for default minor */
37901333Scth 		ASSERT(i_ddi_devi_attached(child));
37910Sstevel@tonic-gate 		ndi_rele_devi(parent);
37920Sstevel@tonic-gate 		parent = child;
37930Sstevel@tonic-gate 	}
37940Sstevel@tonic-gate 
37950Sstevel@tonic-gate 	if (devtp || spectypep) {
37960Sstevel@tonic-gate 		if (minorname == NULL) {
37977224Scth 			/*
37987224Scth 			 * Search for a default entry with an active
37997224Scth 			 * ndi_devi_enter to protect the devi_minor list.
38007224Scth 			 */
38017224Scth 			ndi_devi_enter(parent, &circ);
38020Sstevel@tonic-gate 			for (dmn = DEVI(parent)->devi_minor; dmn;
38030Sstevel@tonic-gate 			    dmn = dmn->next) {
38040Sstevel@tonic-gate 				if (dmn->type == DDM_DEFAULT) {
38050Sstevel@tonic-gate 					devt = dmn->ddm_dev;
38060Sstevel@tonic-gate 					spectype = dmn->ddm_spec_type;
38070Sstevel@tonic-gate 					break;
38080Sstevel@tonic-gate 				}
38090Sstevel@tonic-gate 			}
38100Sstevel@tonic-gate 
38110Sstevel@tonic-gate 			if (devt == NODEV) {
38120Sstevel@tonic-gate 				/*
38130Sstevel@tonic-gate 				 * No default minor node, try the first one;
38140Sstevel@tonic-gate 				 * else, assume 1-1 instance-minor mapping
38150Sstevel@tonic-gate 				 */
38160Sstevel@tonic-gate 				dmn = DEVI(parent)->devi_minor;
38170Sstevel@tonic-gate 				if (dmn && ((dmn->type == DDM_MINOR) ||
38180Sstevel@tonic-gate 				    (dmn->type == DDM_INTERNAL_PATH))) {
38190Sstevel@tonic-gate 					devt = dmn->ddm_dev;
38200Sstevel@tonic-gate 					spectype = dmn->ddm_spec_type;
38210Sstevel@tonic-gate 				} else {
38220Sstevel@tonic-gate 					devt = makedevice(
38230Sstevel@tonic-gate 					    DEVI(parent)->devi_major,
38240Sstevel@tonic-gate 					    ddi_get_instance(parent));
38250Sstevel@tonic-gate 					spectype = S_IFCHR;
38260Sstevel@tonic-gate 				}
38270Sstevel@tonic-gate 			}
38287224Scth 			ndi_devi_exit(parent, circ);
38290Sstevel@tonic-gate 		}
38300Sstevel@tonic-gate 		if (devtp)
38310Sstevel@tonic-gate 			*devtp = devt;
38320Sstevel@tonic-gate 		if (spectypep)
38330Sstevel@tonic-gate 			*spectypep = spectype;
38340Sstevel@tonic-gate 	}
38350Sstevel@tonic-gate 
38360Sstevel@tonic-gate 	pn_free(&pn);
38370Sstevel@tonic-gate 	kmem_free(component, MAXNAMELEN);
38380Sstevel@tonic-gate 	kmem_free(config_name, MAXNAMELEN);
38390Sstevel@tonic-gate 
38400Sstevel@tonic-gate 	/*
38410Sstevel@tonic-gate 	 * If there is no error, return the appropriate parameters
38420Sstevel@tonic-gate 	 */
38430Sstevel@tonic-gate 	if (dipp != NULL)
38440Sstevel@tonic-gate 		*dipp = parent;
3845*7613SVikram.Hegde@Sun.COM 	else if (pci_dipp == NULL) {
38460Sstevel@tonic-gate 		/*
38470Sstevel@tonic-gate 		 * We should really keep the ref count to keep the node from
38480Sstevel@tonic-gate 		 * detaching but ddi_pathname_to_dev_t() specifies a NULL dipp,
38490Sstevel@tonic-gate 		 * so we have no way of passing back the held dip.  Not holding
38500Sstevel@tonic-gate 		 * the dip allows detaches to occur - which can cause problems
38510Sstevel@tonic-gate 		 * for subsystems which call ddi_pathname_to_dev_t (console).
38520Sstevel@tonic-gate 		 *
38530Sstevel@tonic-gate 		 * Instead of holding the dip, we place a ddi-no-autodetach
38540Sstevel@tonic-gate 		 * property on the node to prevent auto detaching.
38550Sstevel@tonic-gate 		 *
38560Sstevel@tonic-gate 		 * The right fix is to remove ddi_pathname_to_dev_t and replace
38570Sstevel@tonic-gate 		 * it, and all references, with a call that specifies a dipp.
38580Sstevel@tonic-gate 		 * In addition, the callers of this new interfaces would then
38590Sstevel@tonic-gate 		 * need to call ndi_rele_devi when the reference is complete.
3860*7613SVikram.Hegde@Sun.COM 		 *
3861*7613SVikram.Hegde@Sun.COM 		 * NOTE: If pci_dipp is non-NULL we are only interested
3862*7613SVikram.Hegde@Sun.COM 		 * in the PCI parent which is returned held. No need to hold
3863*7613SVikram.Hegde@Sun.COM 		 * the leaf dip.
38640Sstevel@tonic-gate 		 */
38650Sstevel@tonic-gate 		(void) ddi_prop_update_int(DDI_DEV_T_NONE, parent,
38660Sstevel@tonic-gate 		    DDI_NO_AUTODETACH, 1);
38670Sstevel@tonic-gate 		ndi_rele_devi(parent);
38680Sstevel@tonic-gate 	}
38690Sstevel@tonic-gate 
38700Sstevel@tonic-gate 	return (0);
38710Sstevel@tonic-gate }
38720Sstevel@tonic-gate 
3873*7613SVikram.Hegde@Sun.COM int
3874*7613SVikram.Hegde@Sun.COM resolve_pathname(char *pathname,
3875*7613SVikram.Hegde@Sun.COM 	dev_info_t **dipp, dev_t *devtp, int *spectypep)
3876*7613SVikram.Hegde@Sun.COM {
3877*7613SVikram.Hegde@Sun.COM 	return (parse_pathname(pathname, dipp, devtp, spectypep, NULL));
3878*7613SVikram.Hegde@Sun.COM }
3879*7613SVikram.Hegde@Sun.COM 
3880*7613SVikram.Hegde@Sun.COM int
3881*7613SVikram.Hegde@Sun.COM ddi_find_pci_parent(char *pathname, dev_info_t **pci_dipp)
3882*7613SVikram.Hegde@Sun.COM {
3883*7613SVikram.Hegde@Sun.COM 	return (parse_pathname(pathname, NULL, NULL, NULL, pci_dipp));
3884*7613SVikram.Hegde@Sun.COM }
3885*7613SVikram.Hegde@Sun.COM 
38860Sstevel@tonic-gate /*
38870Sstevel@tonic-gate  * Given the pathname of a device, return the dev_t of the corresponding
38880Sstevel@tonic-gate  * device.  Returns NODEV on failure.
38890Sstevel@tonic-gate  *
38900Sstevel@tonic-gate  * Note that this call sets the DDI_NO_AUTODETACH property on the devinfo node.
38910Sstevel@tonic-gate  */
38920Sstevel@tonic-gate dev_t
38930Sstevel@tonic-gate ddi_pathname_to_dev_t(char *pathname)
38940Sstevel@tonic-gate {
38950Sstevel@tonic-gate 	dev_t devt;
38960Sstevel@tonic-gate 	int error;
38970Sstevel@tonic-gate 
38980Sstevel@tonic-gate 	error = resolve_pathname(pathname, NULL, &devt, NULL);
38990Sstevel@tonic-gate 
39000Sstevel@tonic-gate 	return (error ? NODEV : devt);
39010Sstevel@tonic-gate }
39020Sstevel@tonic-gate 
39030Sstevel@tonic-gate /*
39040Sstevel@tonic-gate  * Translate a prom pathname to kernel devfs pathname.
39050Sstevel@tonic-gate  * Caller is assumed to allocate devfspath memory of
39060Sstevel@tonic-gate  * size at least MAXPATHLEN
39070Sstevel@tonic-gate  *
39080Sstevel@tonic-gate  * The prom pathname may not include minor name, but
39090Sstevel@tonic-gate  * devfs pathname has a minor name portion.
39100Sstevel@tonic-gate  */
39110Sstevel@tonic-gate int
39120Sstevel@tonic-gate i_ddi_prompath_to_devfspath(char *prompath, char *devfspath)
39130Sstevel@tonic-gate {
39140Sstevel@tonic-gate 	dev_t		devt = (dev_t)NODEV;
39150Sstevel@tonic-gate 	dev_info_t	*dip = NULL;
39160Sstevel@tonic-gate 	char		*minor_name = NULL;
39170Sstevel@tonic-gate 	int		spectype;
39180Sstevel@tonic-gate 	int		error;
39197224Scth 	int		circ;
39200Sstevel@tonic-gate 
39210Sstevel@tonic-gate 	error = resolve_pathname(prompath, &dip, &devt, &spectype);
39220Sstevel@tonic-gate 	if (error)
39230Sstevel@tonic-gate 		return (DDI_FAILURE);
39240Sstevel@tonic-gate 	ASSERT(dip && devt != NODEV);
39250Sstevel@tonic-gate 
39260Sstevel@tonic-gate 	/*
39270Sstevel@tonic-gate 	 * Get in-kernel devfs pathname
39280Sstevel@tonic-gate 	 */
39290Sstevel@tonic-gate 	(void) ddi_pathname(dip, devfspath);
39300Sstevel@tonic-gate 
39317224Scth 	ndi_devi_enter(dip, &circ);
39320Sstevel@tonic-gate 	minor_name = i_ddi_devtspectype_to_minorname(dip, devt, spectype);
39330Sstevel@tonic-gate 	if (minor_name) {
39340Sstevel@tonic-gate 		(void) strcat(devfspath, ":");
39350Sstevel@tonic-gate 		(void) strcat(devfspath, minor_name);
39360Sstevel@tonic-gate 	} else {
39370Sstevel@tonic-gate 		/*
39380Sstevel@tonic-gate 		 * If minor_name is NULL, we have an alias minor node.
39390Sstevel@tonic-gate 		 * So manufacture a path to the corresponding clone minor.
39400Sstevel@tonic-gate 		 */
39410Sstevel@tonic-gate 		(void) snprintf(devfspath, MAXPATHLEN, "%s:%s",
39420Sstevel@tonic-gate 		    CLONE_PATH, ddi_driver_name(dip));
39430Sstevel@tonic-gate 	}
39447224Scth 	ndi_devi_exit(dip, circ);
39450Sstevel@tonic-gate 
39460Sstevel@tonic-gate 	/* release hold from resolve_pathname() */
39470Sstevel@tonic-gate 	ndi_rele_devi(dip);
39480Sstevel@tonic-gate 	return (0);
39490Sstevel@tonic-gate }
39500Sstevel@tonic-gate 
39510Sstevel@tonic-gate /*
39520Sstevel@tonic-gate  * Reset all the pure leaf drivers on the system at halt time
39530Sstevel@tonic-gate  */
39540Sstevel@tonic-gate static int
39550Sstevel@tonic-gate reset_leaf_device(dev_info_t *dip, void *arg)
39560Sstevel@tonic-gate {
39570Sstevel@tonic-gate 	_NOTE(ARGUNUSED(arg))
39580Sstevel@tonic-gate 	struct dev_ops *ops;
39590Sstevel@tonic-gate 
39600Sstevel@tonic-gate 	/* if the device doesn't need to be reset then there's nothing to do */
39610Sstevel@tonic-gate 	if (!DEVI_NEED_RESET(dip))
39620Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
39630Sstevel@tonic-gate 
39640Sstevel@tonic-gate 	/*
39650Sstevel@tonic-gate 	 * if the device isn't a char/block device or doesn't have a
39660Sstevel@tonic-gate 	 * reset entry point then there's nothing to do.
39670Sstevel@tonic-gate 	 */
39680Sstevel@tonic-gate 	ops = ddi_get_driver(dip);
39690Sstevel@tonic-gate 	if ((ops == NULL) || (ops->devo_cb_ops == NULL) ||
39700Sstevel@tonic-gate 	    (ops->devo_reset == nodev) || (ops->devo_reset == nulldev) ||
39710Sstevel@tonic-gate 	    (ops->devo_reset == NULL))
39720Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
39730Sstevel@tonic-gate 
39740Sstevel@tonic-gate 	if (DEVI_IS_ATTACHING(dip) || DEVI_IS_DETACHING(dip)) {
39750Sstevel@tonic-gate 		static char path[MAXPATHLEN];
39760Sstevel@tonic-gate 
39770Sstevel@tonic-gate 		/*
39780Sstevel@tonic-gate 		 * bad news, this device has blocked in it's attach or
39790Sstevel@tonic-gate 		 * detach routine, which means it not safe to call it's
39800Sstevel@tonic-gate 		 * devo_reset() entry point.
39810Sstevel@tonic-gate 		 */
39820Sstevel@tonic-gate 		cmn_err(CE_WARN, "unable to reset device: %s",
39830Sstevel@tonic-gate 		    ddi_pathname(dip, path));
39840Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
39850Sstevel@tonic-gate 	}
39860Sstevel@tonic-gate 
39870Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_NOTE, "resetting %s%d\n",
39884950Scth 	    ddi_driver_name(dip), ddi_get_instance(dip)));
39890Sstevel@tonic-gate 
39900Sstevel@tonic-gate 	(void) devi_reset(dip, DDI_RESET_FORCE);
39910Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
39920Sstevel@tonic-gate }
39930Sstevel@tonic-gate 
39940Sstevel@tonic-gate void
39950Sstevel@tonic-gate reset_leaves(void)
39960Sstevel@tonic-gate {
39970Sstevel@tonic-gate 	/*
39980Sstevel@tonic-gate 	 * if we're reached here, the device tree better not be changing.
39990Sstevel@tonic-gate 	 * so either devinfo_freeze better be set or we better be panicing.
40000Sstevel@tonic-gate 	 */
40010Sstevel@tonic-gate 	ASSERT(devinfo_freeze || panicstr);
40020Sstevel@tonic-gate 
40030Sstevel@tonic-gate 	(void) walk_devs(top_devinfo, reset_leaf_device, NULL, 0);
40040Sstevel@tonic-gate }
40050Sstevel@tonic-gate 
40060Sstevel@tonic-gate /*
40070Sstevel@tonic-gate  * devtree_freeze() must be called before reset_leaves() during a
40080Sstevel@tonic-gate  * normal system shutdown.  It attempts to ensure that there are no
40090Sstevel@tonic-gate  * outstanding attach or detach operations in progress when reset_leaves()
40100Sstevel@tonic-gate  * is invoked.  It must be called before the system becomes single-threaded
40110Sstevel@tonic-gate  * because device attach and detach are multi-threaded operations.  (note
40120Sstevel@tonic-gate  * that during system shutdown the system doesn't actually become
40130Sstevel@tonic-gate  * single-thread since other threads still exist, but the shutdown thread
40140Sstevel@tonic-gate  * will disable preemption for itself, raise it's pil, and stop all the
40150Sstevel@tonic-gate  * other cpus in the system there by effectively making the system
40160Sstevel@tonic-gate  * single-threaded.)
40170Sstevel@tonic-gate  */
40180Sstevel@tonic-gate void
40190Sstevel@tonic-gate devtree_freeze(void)
40200Sstevel@tonic-gate {
40210Sstevel@tonic-gate 	int delayed = 0;
40220Sstevel@tonic-gate 
40230Sstevel@tonic-gate 	/* if we're panicing then the device tree isn't going to be changing */
40240Sstevel@tonic-gate 	if (panicstr)
40250Sstevel@tonic-gate 		return;
40260Sstevel@tonic-gate 
40270Sstevel@tonic-gate 	/* stop all dev_info state changes in the device tree */
40280Sstevel@tonic-gate 	devinfo_freeze = gethrtime();
40290Sstevel@tonic-gate 
40300Sstevel@tonic-gate 	/*
40310Sstevel@tonic-gate 	 * if we're not panicing and there are on-going attach or detach
40320Sstevel@tonic-gate 	 * operations, wait for up to 3 seconds for them to finish.  This
40330Sstevel@tonic-gate 	 * is a randomly chosen interval but this should be ok because:
40340Sstevel@tonic-gate 	 * - 3 seconds is very small relative to the deadman timer.
40350Sstevel@tonic-gate 	 * - normal attach and detach operations should be very quick.
40360Sstevel@tonic-gate 	 * - attach and detach operations are fairly rare.
40370Sstevel@tonic-gate 	 */
40380Sstevel@tonic-gate 	while (!panicstr && atomic_add_long_nv(&devinfo_attach_detach, 0) &&
40390Sstevel@tonic-gate 	    (delayed < 3)) {
40400Sstevel@tonic-gate 		delayed += 1;
40410Sstevel@tonic-gate 
40420Sstevel@tonic-gate 		/* do a sleeping wait for one second */
40430Sstevel@tonic-gate 		ASSERT(!servicing_interrupt());
40440Sstevel@tonic-gate 		delay(drv_usectohz(MICROSEC));
40450Sstevel@tonic-gate 	}
40460Sstevel@tonic-gate }
40470Sstevel@tonic-gate 
40480Sstevel@tonic-gate static int
40490Sstevel@tonic-gate bind_dip(dev_info_t *dip, void *arg)
40500Sstevel@tonic-gate {
40510Sstevel@tonic-gate 	_NOTE(ARGUNUSED(arg))
40524145Scth 	char	*path;
40534145Scth 	major_t	major, pmajor;
40544145Scth 
40554145Scth 	/*
40564145Scth 	 * If the node is currently bound to the wrong driver, try to unbind
40574145Scth 	 * so that we can rebind to the correct driver.
40584145Scth 	 */
40594145Scth 	if (i_ddi_node_state(dip) >= DS_BOUND) {
40604145Scth 		major = ddi_compatible_driver_major(dip, NULL);
40614145Scth 		if ((DEVI(dip)->devi_major == major) &&
40624145Scth 		    (i_ddi_node_state(dip) >= DS_INITIALIZED)) {
40634145Scth 			/*
40644145Scth 			 * Check for a path-oriented driver alias that
40654145Scth 			 * takes precedence over current driver binding.
40664145Scth 			 */
40674145Scth 			path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
40684145Scth 			(void) ddi_pathname(dip, path);
40694145Scth 			pmajor = ddi_name_to_major(path);
40707009Scth 			if ((pmajor != DDI_MAJOR_T_NONE) &&
40714145Scth 			    !(devnamesp[pmajor].dn_flags & DN_DRIVER_REMOVED))
40724145Scth 				major = pmajor;
40734145Scth 			kmem_free(path, MAXPATHLEN);
40744145Scth 		}
40754145Scth 
40764145Scth 		/* attempt unbind if current driver is incorrect */
40777009Scth 		if ((major != DDI_MAJOR_T_NONE) &&
40784145Scth 		    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED) &&
40794145Scth 		    (major != DEVI(dip)->devi_major))
40804145Scth 			(void) ndi_devi_unbind_driver(dip);
40814145Scth 	}
40824145Scth 
40834145Scth 	/* If unbound, try to bind to a driver */
40840Sstevel@tonic-gate 	if (i_ddi_node_state(dip) < DS_BOUND)
40850Sstevel@tonic-gate 		(void) ndi_devi_bind_driver(dip, 0);
40860Sstevel@tonic-gate 
40870Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
40880Sstevel@tonic-gate }
40890Sstevel@tonic-gate 
40900Sstevel@tonic-gate void
40910Sstevel@tonic-gate i_ddi_bind_devs(void)
40920Sstevel@tonic-gate {
40934145Scth 	/* flush devfs so that ndi_devi_unbind_driver will work when possible */
40944145Scth 	(void) devfs_clean(top_devinfo, NULL, 0);
40954145Scth 
40960Sstevel@tonic-gate 	ddi_walk_devs(top_devinfo, bind_dip, (void *)NULL);
40970Sstevel@tonic-gate }
40980Sstevel@tonic-gate 
40990Sstevel@tonic-gate static int
41000Sstevel@tonic-gate unbind_children(dev_info_t *dip, void *arg)
41010Sstevel@tonic-gate {
41020Sstevel@tonic-gate 	int circ;
41030Sstevel@tonic-gate 	dev_info_t *cdip;
41040Sstevel@tonic-gate 	major_t major = (major_t)(uintptr_t)arg;
41050Sstevel@tonic-gate 
41060Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
41070Sstevel@tonic-gate 	cdip = ddi_get_child(dip);
41080Sstevel@tonic-gate 	/*
41090Sstevel@tonic-gate 	 * We are called either from rem_drv or update_drv.
41100Sstevel@tonic-gate 	 * In both cases, we unbind persistent nodes and destroy
41110Sstevel@tonic-gate 	 * .conf nodes. In the case of rem_drv, this will be the
41120Sstevel@tonic-gate 	 * final state. In the case of update_drv, i_ddi_bind_devs()
41130Sstevel@tonic-gate 	 * will be invoked later to reenumerate (new) driver.conf
41140Sstevel@tonic-gate 	 * rebind persistent nodes.
41150Sstevel@tonic-gate 	 */
41160Sstevel@tonic-gate 	while (cdip) {
41170Sstevel@tonic-gate 		dev_info_t *next = ddi_get_next_sibling(cdip);
41180Sstevel@tonic-gate 		if ((i_ddi_node_state(cdip) > DS_INITIALIZED) ||
41190Sstevel@tonic-gate 		    (ddi_driver_major(cdip) != major)) {
41200Sstevel@tonic-gate 			cdip = next;
41210Sstevel@tonic-gate 			continue;
41220Sstevel@tonic-gate 		}
41230Sstevel@tonic-gate 		(void) ndi_devi_unbind_driver(cdip);
41240Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(cdip) == 0)
41250Sstevel@tonic-gate 			(void) ddi_remove_child(cdip, 0);
41260Sstevel@tonic-gate 		cdip = next;
41270Sstevel@tonic-gate 	}
41280Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
41290Sstevel@tonic-gate 
41300Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
41310Sstevel@tonic-gate }
41320Sstevel@tonic-gate 
41330Sstevel@tonic-gate void
41340Sstevel@tonic-gate i_ddi_unbind_devs(major_t major)
41350Sstevel@tonic-gate {
41360Sstevel@tonic-gate 	ddi_walk_devs(top_devinfo, unbind_children, (void *)(uintptr_t)major);
41370Sstevel@tonic-gate }
41380Sstevel@tonic-gate 
41390Sstevel@tonic-gate /*
41400Sstevel@tonic-gate  * I/O Hotplug control
41410Sstevel@tonic-gate  */
41420Sstevel@tonic-gate 
41430Sstevel@tonic-gate /*
41440Sstevel@tonic-gate  * create and attach a dev_info node from a .conf file spec
41450Sstevel@tonic-gate  */
41460Sstevel@tonic-gate static void
41470Sstevel@tonic-gate init_spec_child(dev_info_t *pdip, struct hwc_spec *specp, uint_t flags)
41480Sstevel@tonic-gate {
41490Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags))
41500Sstevel@tonic-gate 	dev_info_t *dip;
41510Sstevel@tonic-gate 	char *node_name;
41520Sstevel@tonic-gate 
41530Sstevel@tonic-gate 	if (((node_name = specp->hwc_devi_name) == NULL) ||
41547009Scth 	    (ddi_name_to_major(node_name) == DDI_MAJOR_T_NONE)) {
41550Sstevel@tonic-gate 		char *tmp = node_name;
41560Sstevel@tonic-gate 		if (tmp == NULL)
41570Sstevel@tonic-gate 			tmp = "<none>";
41580Sstevel@tonic-gate 		cmn_err(CE_CONT,
41590Sstevel@tonic-gate 		    "init_spec_child: parent=%s, bad spec (%s)\n",
41600Sstevel@tonic-gate 		    ddi_node_name(pdip), tmp);
41610Sstevel@tonic-gate 		return;
41620Sstevel@tonic-gate 	}
41630Sstevel@tonic-gate 
4164789Sahrens 	dip = i_ddi_alloc_node(pdip, node_name, (pnode_t)DEVI_PSEUDO_NODEID,
41650Sstevel@tonic-gate 	    -1, specp->hwc_devi_sys_prop_ptr, KM_SLEEP);
41660Sstevel@tonic-gate 
41670Sstevel@tonic-gate 	if (dip == NULL)
41680Sstevel@tonic-gate 		return;
41690Sstevel@tonic-gate 
41700Sstevel@tonic-gate 	if (ddi_initchild(pdip, dip) != DDI_SUCCESS)
41710Sstevel@tonic-gate 		(void) ddi_remove_child(dip, 0);
41720Sstevel@tonic-gate }
41730Sstevel@tonic-gate 
41740Sstevel@tonic-gate /*
41750Sstevel@tonic-gate  * Lookup hwc specs from hash tables and make children from the spec
41760Sstevel@tonic-gate  * Because some .conf children are "merge" nodes, we also initialize
41770Sstevel@tonic-gate  * .conf children to merge properties onto hardware nodes.
41780Sstevel@tonic-gate  *
41790Sstevel@tonic-gate  * The pdip must be held busy.
41800Sstevel@tonic-gate  */
41810Sstevel@tonic-gate int
41820Sstevel@tonic-gate i_ndi_make_spec_children(dev_info_t *pdip, uint_t flags)
41830Sstevel@tonic-gate {
41840Sstevel@tonic-gate 	extern struct hwc_spec *hwc_get_child_spec(dev_info_t *, major_t);
4185298Scth 	int			circ;
4186298Scth 	struct hwc_spec		*list, *spec;
4187298Scth 
4188298Scth 	ndi_devi_enter(pdip, &circ);
4189298Scth 	if (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN) {
4190298Scth 		ndi_devi_exit(pdip, circ);
41910Sstevel@tonic-gate 		return (DDI_SUCCESS);
4192298Scth 	}
41930Sstevel@tonic-gate 
41947009Scth 	list = hwc_get_child_spec(pdip, DDI_MAJOR_T_NONE);
41950Sstevel@tonic-gate 	for (spec = list; spec != NULL; spec = spec->hwc_next) {
41960Sstevel@tonic-gate 		init_spec_child(pdip, spec, flags);
41970Sstevel@tonic-gate 	}
41980Sstevel@tonic-gate 	hwc_free_spec_list(list);
41990Sstevel@tonic-gate 
42000Sstevel@tonic-gate 	mutex_enter(&DEVI(pdip)->devi_lock);
42010Sstevel@tonic-gate 	DEVI(pdip)->devi_flags |= DEVI_MADE_CHILDREN;
42020Sstevel@tonic-gate 	mutex_exit(&DEVI(pdip)->devi_lock);
4203298Scth 	ndi_devi_exit(pdip, circ);
42040Sstevel@tonic-gate 	return (DDI_SUCCESS);
42050Sstevel@tonic-gate }
42060Sstevel@tonic-gate 
42070Sstevel@tonic-gate /*
42080Sstevel@tonic-gate  * Run initchild on all child nodes such that instance assignment
42090Sstevel@tonic-gate  * for multiport network cards are contiguous.
42100Sstevel@tonic-gate  *
42110Sstevel@tonic-gate  * The pdip must be held busy.
42120Sstevel@tonic-gate  */
42130Sstevel@tonic-gate static void
42140Sstevel@tonic-gate i_ndi_init_hw_children(dev_info_t *pdip, uint_t flags)
42150Sstevel@tonic-gate {
42160Sstevel@tonic-gate 	dev_info_t *dip;
42170Sstevel@tonic-gate 
42180Sstevel@tonic-gate 	ASSERT(DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
42190Sstevel@tonic-gate 
42200Sstevel@tonic-gate 	/* contiguous instance assignment */
42210Sstevel@tonic-gate 	e_ddi_enter_instance();
42220Sstevel@tonic-gate 	dip = ddi_get_child(pdip);
42230Sstevel@tonic-gate 	while (dip) {
42240Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(dip))
42250Sstevel@tonic-gate 			(void) i_ndi_config_node(dip, DS_INITIALIZED, flags);
42260Sstevel@tonic-gate 		dip = ddi_get_next_sibling(dip);
42270Sstevel@tonic-gate 	}
42280Sstevel@tonic-gate 	e_ddi_exit_instance();
42290Sstevel@tonic-gate }
42300Sstevel@tonic-gate 
42310Sstevel@tonic-gate /*
42320Sstevel@tonic-gate  * report device status
42330Sstevel@tonic-gate  */
42340Sstevel@tonic-gate static void
42350Sstevel@tonic-gate i_ndi_devi_report_status_change(dev_info_t *dip, char *path)
42360Sstevel@tonic-gate {
42370Sstevel@tonic-gate 	char *status;
42380Sstevel@tonic-gate 
42390Sstevel@tonic-gate 	if (!DEVI_NEED_REPORT(dip) ||
42400Sstevel@tonic-gate 	    (i_ddi_node_state(dip) < DS_INITIALIZED)) {
42410Sstevel@tonic-gate 		return;
42420Sstevel@tonic-gate 	}
42430Sstevel@tonic-gate 
42440Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
42450Sstevel@tonic-gate 		status = "offline";
42460Sstevel@tonic-gate 	} else if (DEVI_IS_DEVICE_DOWN(dip)) {
42470Sstevel@tonic-gate 		status = "down";
42480Sstevel@tonic-gate 	} else if (DEVI_IS_BUS_QUIESCED(dip)) {
42490Sstevel@tonic-gate 		status = "quiesced";
42500Sstevel@tonic-gate 	} else if (DEVI_IS_BUS_DOWN(dip)) {
42510Sstevel@tonic-gate 		status = "down";
42521333Scth 	} else if (i_ddi_devi_attached(dip)) {
42530Sstevel@tonic-gate 		status = "online";
42540Sstevel@tonic-gate 	} else {
42550Sstevel@tonic-gate 		status = "unknown";
42560Sstevel@tonic-gate 	}
42570Sstevel@tonic-gate 
42580Sstevel@tonic-gate 	if (path == NULL) {
42590Sstevel@tonic-gate 		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
42600Sstevel@tonic-gate 		cmn_err(CE_CONT, "?%s (%s%d) %s\n",
42614950Scth 		    ddi_pathname(dip, path), ddi_driver_name(dip),
42624950Scth 		    ddi_get_instance(dip), status);
42630Sstevel@tonic-gate 		kmem_free(path, MAXPATHLEN);
42640Sstevel@tonic-gate 	} else {
42650Sstevel@tonic-gate 		cmn_err(CE_CONT, "?%s (%s%d) %s\n",
42664950Scth 		    path, ddi_driver_name(dip),
42674950Scth 		    ddi_get_instance(dip), status);
42680Sstevel@tonic-gate 	}
42690Sstevel@tonic-gate 
4270495Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
42710Sstevel@tonic-gate 	DEVI_REPORT_DONE(dip);
4272495Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
42730Sstevel@tonic-gate }
42740Sstevel@tonic-gate 
42750Sstevel@tonic-gate /*
42760Sstevel@tonic-gate  * log a notification that a dev_info node has been configured.
42770Sstevel@tonic-gate  */
42780Sstevel@tonic-gate static int
42790Sstevel@tonic-gate i_log_devfs_add_devinfo(dev_info_t *dip, uint_t flags)
42800Sstevel@tonic-gate {
42810Sstevel@tonic-gate 	int se_err;
42820Sstevel@tonic-gate 	char *pathname;
42830Sstevel@tonic-gate 	sysevent_t *ev;
42840Sstevel@tonic-gate 	sysevent_id_t eid;
42850Sstevel@tonic-gate 	sysevent_value_t se_val;
42860Sstevel@tonic-gate 	sysevent_attr_list_t *ev_attr_list = NULL;
42870Sstevel@tonic-gate 	char *class_name;
42880Sstevel@tonic-gate 	int no_transport = 0;
42890Sstevel@tonic-gate 
42900Sstevel@tonic-gate 	ASSERT(dip);
42910Sstevel@tonic-gate 
42920Sstevel@tonic-gate 	/*
42930Sstevel@tonic-gate 	 * Invalidate the devinfo snapshot cache
42940Sstevel@tonic-gate 	 */
42950Sstevel@tonic-gate 	i_ddi_di_cache_invalidate(KM_SLEEP);
42960Sstevel@tonic-gate 
42970Sstevel@tonic-gate 	/* do not generate ESC_DEVFS_DEVI_ADD event during boot */
42980Sstevel@tonic-gate 	if (!i_ddi_io_initialized())
42990Sstevel@tonic-gate 		return (DDI_SUCCESS);
43000Sstevel@tonic-gate 
43010Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_ADD, EP_DDI, SE_SLEEP);
43020Sstevel@tonic-gate 
43030Sstevel@tonic-gate 	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
43040Sstevel@tonic-gate 
43050Sstevel@tonic-gate 	(void) ddi_pathname(dip, pathname);
43060Sstevel@tonic-gate 	ASSERT(strlen(pathname));
43070Sstevel@tonic-gate 
43080Sstevel@tonic-gate 	se_val.value_type = SE_DATA_TYPE_STRING;
43090Sstevel@tonic-gate 	se_val.value.sv_string = pathname;
43100Sstevel@tonic-gate 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
43110Sstevel@tonic-gate 	    &se_val, SE_SLEEP) != 0) {
43120Sstevel@tonic-gate 		goto fail;
43130Sstevel@tonic-gate 	}
43140Sstevel@tonic-gate 
43150Sstevel@tonic-gate 	/* add the device class attribute */
43160Sstevel@tonic-gate 	if ((class_name = i_ddi_devi_class(dip)) != NULL) {
43170Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_STRING;
43180Sstevel@tonic-gate 		se_val.value.sv_string = class_name;
43190Sstevel@tonic-gate 
43200Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
43210Sstevel@tonic-gate 		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
43220Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
43230Sstevel@tonic-gate 			goto fail;
43240Sstevel@tonic-gate 		}
43250Sstevel@tonic-gate 	}
43260Sstevel@tonic-gate 
43270Sstevel@tonic-gate 	/*
43280Sstevel@tonic-gate 	 * must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
43290Sstevel@tonic-gate 	 * in which case the branch event will be logged by the caller
43300Sstevel@tonic-gate 	 * after the entire branch has been configured.
43310Sstevel@tonic-gate 	 */
43320Sstevel@tonic-gate 	if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
43330Sstevel@tonic-gate 		/*
43340Sstevel@tonic-gate 		 * Instead of logging a separate branch event just add
43350Sstevel@tonic-gate 		 * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
43360Sstevel@tonic-gate 		 * generate a EC_DEV_BRANCH event.
43370Sstevel@tonic-gate 		 */
43380Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_INT32;
43390Sstevel@tonic-gate 		se_val.value.sv_int32 = 1;
43400Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
43410Sstevel@tonic-gate 		    DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
43420Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
43430Sstevel@tonic-gate 			goto fail;
43440Sstevel@tonic-gate 		}
43450Sstevel@tonic-gate 	}
43460Sstevel@tonic-gate 
43470Sstevel@tonic-gate 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
43480Sstevel@tonic-gate 		sysevent_free_attr(ev_attr_list);
43490Sstevel@tonic-gate 		goto fail;
43500Sstevel@tonic-gate 	}
43510Sstevel@tonic-gate 
43520Sstevel@tonic-gate 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
43530Sstevel@tonic-gate 		if (se_err == SE_NO_TRANSPORT)
43540Sstevel@tonic-gate 			no_transport = 1;
43550Sstevel@tonic-gate 		goto fail;
43560Sstevel@tonic-gate 	}
43570Sstevel@tonic-gate 
43580Sstevel@tonic-gate 	sysevent_free(ev);
43590Sstevel@tonic-gate 	kmem_free(pathname, MAXPATHLEN);
43600Sstevel@tonic-gate 
43610Sstevel@tonic-gate 	return (DDI_SUCCESS);
43620Sstevel@tonic-gate 
43630Sstevel@tonic-gate fail:
43640Sstevel@tonic-gate 	cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_ADD event for %s%s",
43650Sstevel@tonic-gate 	    pathname, (no_transport) ? " (syseventd not responding)" : "");
43660Sstevel@tonic-gate 
43670Sstevel@tonic-gate 	cmn_err(CE_WARN, "/dev may not be current for driver %s. "
43680Sstevel@tonic-gate 	    "Run devfsadm -i %s",
43690Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_driver_name(dip));
43700Sstevel@tonic-gate 
43710Sstevel@tonic-gate 	sysevent_free(ev);
43720Sstevel@tonic-gate 	kmem_free(pathname, MAXPATHLEN);
43730Sstevel@tonic-gate 	return (DDI_SUCCESS);
43740Sstevel@tonic-gate }
43750Sstevel@tonic-gate 
43760Sstevel@tonic-gate /*
43770Sstevel@tonic-gate  * log a notification that a dev_info node has been unconfigured.
43780Sstevel@tonic-gate  */
43790Sstevel@tonic-gate static int
43800Sstevel@tonic-gate i_log_devfs_remove_devinfo(char *pathname, char *class_name, char *driver_name,
43810Sstevel@tonic-gate     int instance, uint_t flags)
43820Sstevel@tonic-gate {
43830Sstevel@tonic-gate 	sysevent_t *ev;
43840Sstevel@tonic-gate 	sysevent_id_t eid;
43850Sstevel@tonic-gate 	sysevent_value_t se_val;
43860Sstevel@tonic-gate 	sysevent_attr_list_t *ev_attr_list = NULL;
43870Sstevel@tonic-gate 	int se_err;
43880Sstevel@tonic-gate 	int no_transport = 0;
43890Sstevel@tonic-gate 
43900Sstevel@tonic-gate 	i_ddi_di_cache_invalidate(KM_SLEEP);
43910Sstevel@tonic-gate 
43920Sstevel@tonic-gate 	if (!i_ddi_io_initialized())
43930Sstevel@tonic-gate 		return (DDI_SUCCESS);
43940Sstevel@tonic-gate 
43950Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_REMOVE, EP_DDI, SE_SLEEP);
43960Sstevel@tonic-gate 
43970Sstevel@tonic-gate 	se_val.value_type = SE_DATA_TYPE_STRING;
43980Sstevel@tonic-gate 	se_val.value.sv_string = pathname;
43990Sstevel@tonic-gate 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
44000Sstevel@tonic-gate 	    &se_val, SE_SLEEP) != 0) {
44010Sstevel@tonic-gate 		goto fail;
44020Sstevel@tonic-gate 	}
44030Sstevel@tonic-gate 
44040Sstevel@tonic-gate 	if (class_name) {
44050Sstevel@tonic-gate 		/* add the device class, driver name and instance attributes */
44060Sstevel@tonic-gate 
44070Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_STRING;
44080Sstevel@tonic-gate 		se_val.value.sv_string = class_name;
44090Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
44100Sstevel@tonic-gate 		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
44110Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
44120Sstevel@tonic-gate 			goto fail;
44130Sstevel@tonic-gate 		}
44140Sstevel@tonic-gate 
44150Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_STRING;
44160Sstevel@tonic-gate 		se_val.value.sv_string = driver_name;
44170Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
44180Sstevel@tonic-gate 		    DEVFS_DRIVER_NAME, &se_val, SE_SLEEP) != 0) {
44190Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
44200Sstevel@tonic-gate 			goto fail;
44210Sstevel@tonic-gate 		}
44220Sstevel@tonic-gate 
44230Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_INT32;
44240Sstevel@tonic-gate 		se_val.value.sv_int32 = instance;
44250Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
44260Sstevel@tonic-gate 		    DEVFS_INSTANCE, &se_val, SE_SLEEP) != 0) {
44270Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
44280Sstevel@tonic-gate 			goto fail;
44290Sstevel@tonic-gate 		}
44300Sstevel@tonic-gate 	}
44310Sstevel@tonic-gate 
44320Sstevel@tonic-gate 	/*
44330Sstevel@tonic-gate 	 * must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
44340Sstevel@tonic-gate 	 * in which case the branch event will be logged by the caller
44350Sstevel@tonic-gate 	 * after the entire branch has been unconfigured.
44360Sstevel@tonic-gate 	 */
44370Sstevel@tonic-gate 	if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
44380Sstevel@tonic-gate 		/*
44390Sstevel@tonic-gate 		 * Instead of logging a separate branch event just add
44400Sstevel@tonic-gate 		 * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
44410Sstevel@tonic-gate 		 * generate a EC_DEV_BRANCH event.
44420Sstevel@tonic-gate 		 */
44430Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_INT32;
44440Sstevel@tonic-gate 		se_val.value.sv_int32 = 1;
44450Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
44460Sstevel@tonic-gate 		    DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
44470Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
44480Sstevel@tonic-gate 			goto fail;
44490Sstevel@tonic-gate 		}
44500Sstevel@tonic-gate 	}
44510Sstevel@tonic-gate 
44520Sstevel@tonic-gate 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
44530Sstevel@tonic-gate 		sysevent_free_attr(ev_attr_list);
44540Sstevel@tonic-gate 		goto fail;
44550Sstevel@tonic-gate 	}
44560Sstevel@tonic-gate 
44570Sstevel@tonic-gate 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
44580Sstevel@tonic-gate 		if (se_err == SE_NO_TRANSPORT)
44590Sstevel@tonic-gate 			no_transport = 1;
44600Sstevel@tonic-gate 		goto fail;
44610Sstevel@tonic-gate 	}
44620Sstevel@tonic-gate 
44630Sstevel@tonic-gate 	sysevent_free(ev);
44640Sstevel@tonic-gate 	return (DDI_SUCCESS);
44650Sstevel@tonic-gate 
44660Sstevel@tonic-gate fail:
44670Sstevel@tonic-gate 	sysevent_free(ev);
44680Sstevel@tonic-gate 	cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_REMOVE event for %s%s",
44690Sstevel@tonic-gate 	    pathname, (no_transport) ? " (syseventd not responding)" : "");
44700Sstevel@tonic-gate 	return (DDI_SUCCESS);
44710Sstevel@tonic-gate }
44720Sstevel@tonic-gate 
44730Sstevel@tonic-gate /*
44740Sstevel@tonic-gate  * log an event that a dev_info branch has been configured or unconfigured.
44750Sstevel@tonic-gate  */
44760Sstevel@tonic-gate static int
44770Sstevel@tonic-gate i_log_devfs_branch(char *node_path, char *subclass)
44780Sstevel@tonic-gate {
44790Sstevel@tonic-gate 	int se_err;
44800Sstevel@tonic-gate 	sysevent_t *ev;
44810Sstevel@tonic-gate 	sysevent_id_t eid;
44820Sstevel@tonic-gate 	sysevent_value_t se_val;
44830Sstevel@tonic-gate 	sysevent_attr_list_t *ev_attr_list = NULL;
44840Sstevel@tonic-gate 	int no_transport = 0;
44850Sstevel@tonic-gate 
44860Sstevel@tonic-gate 	/* do not generate the event during boot */
44870Sstevel@tonic-gate 	if (!i_ddi_io_initialized())
44880Sstevel@tonic-gate 		return (DDI_SUCCESS);
44890Sstevel@tonic-gate 
44900Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, subclass, EP_DDI, SE_SLEEP);
44910Sstevel@tonic-gate 
44920Sstevel@tonic-gate 	se_val.value_type = SE_DATA_TYPE_STRING;
44930Sstevel@tonic-gate 	se_val.value.sv_string = node_path;
44940Sstevel@tonic-gate 
44950Sstevel@tonic-gate 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
44960Sstevel@tonic-gate 	    &se_val, SE_SLEEP) != 0) {
44970Sstevel@tonic-gate 		goto fail;
44980Sstevel@tonic-gate 	}
44990Sstevel@tonic-gate 
45000Sstevel@tonic-gate 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
45010Sstevel@tonic-gate 		sysevent_free_attr(ev_attr_list);
45020Sstevel@tonic-gate 		goto fail;
45030Sstevel@tonic-gate 	}
45040Sstevel@tonic-gate 
45050Sstevel@tonic-gate 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
45060Sstevel@tonic-gate 		if (se_err == SE_NO_TRANSPORT)
45070Sstevel@tonic-gate 			no_transport = 1;
45080Sstevel@tonic-gate 		goto fail;
45090Sstevel@tonic-gate 	}
45100Sstevel@tonic-gate 
45110Sstevel@tonic-gate 	sysevent_free(ev);
45120Sstevel@tonic-gate 	return (DDI_SUCCESS);
45130Sstevel@tonic-gate 
45140Sstevel@tonic-gate fail:
45150Sstevel@tonic-gate 	cmn_err(CE_WARN, "failed to log %s branch event for %s%s",
45160Sstevel@tonic-gate 	    subclass, node_path,
45170Sstevel@tonic-gate 	    (no_transport) ? " (syseventd not responding)" : "");
45180Sstevel@tonic-gate 
45190Sstevel@tonic-gate 	sysevent_free(ev);
45200Sstevel@tonic-gate 	return (DDI_FAILURE);
45210Sstevel@tonic-gate }
45220Sstevel@tonic-gate 
45230Sstevel@tonic-gate /*
45240Sstevel@tonic-gate  * log an event that a dev_info tree branch has been configured.
45250Sstevel@tonic-gate  */
45260Sstevel@tonic-gate static int
45270Sstevel@tonic-gate i_log_devfs_branch_add(dev_info_t *dip)
45280Sstevel@tonic-gate {
45290Sstevel@tonic-gate 	char *node_path;
45300Sstevel@tonic-gate 	int rv;
45310Sstevel@tonic-gate 
45320Sstevel@tonic-gate 	node_path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
45330Sstevel@tonic-gate 	(void) ddi_pathname(dip, node_path);
45340Sstevel@tonic-gate 	rv = i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_ADD);
45350Sstevel@tonic-gate 	kmem_free(node_path, MAXPATHLEN);
45360Sstevel@tonic-gate 
45370Sstevel@tonic-gate 	return (rv);
45380Sstevel@tonic-gate }
45390Sstevel@tonic-gate 
45400Sstevel@tonic-gate /*
45410Sstevel@tonic-gate  * log an event that a dev_info tree branch has been unconfigured.
45420Sstevel@tonic-gate  */
45430Sstevel@tonic-gate static int
45440Sstevel@tonic-gate i_log_devfs_branch_remove(char *node_path)
45450Sstevel@tonic-gate {
45460Sstevel@tonic-gate 	return (i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_REMOVE));
45470Sstevel@tonic-gate }
45480Sstevel@tonic-gate 
45490Sstevel@tonic-gate /*
45500Sstevel@tonic-gate  * enqueue the dip's deviname on the branch event queue.
45510Sstevel@tonic-gate  */
45520Sstevel@tonic-gate static struct brevq_node *
45530Sstevel@tonic-gate brevq_enqueue(struct brevq_node **brevqp, dev_info_t *dip,
45540Sstevel@tonic-gate     struct brevq_node *child)
45550Sstevel@tonic-gate {
45560Sstevel@tonic-gate 	struct brevq_node *brn;
45570Sstevel@tonic-gate 	char *deviname;
45580Sstevel@tonic-gate 
45590Sstevel@tonic-gate 	deviname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
45600Sstevel@tonic-gate 	(void) ddi_deviname(dip, deviname);
45610Sstevel@tonic-gate 
45620Sstevel@tonic-gate 	brn = kmem_zalloc(sizeof (*brn), KM_SLEEP);
45631317Scth 	brn->brn_deviname = i_ddi_strdup(deviname, KM_SLEEP);
45640Sstevel@tonic-gate 	kmem_free(deviname, MAXNAMELEN);
45651317Scth 	brn->brn_child = child;
45661317Scth 	brn->brn_sibling = *brevqp;
45670Sstevel@tonic-gate 	*brevqp = brn;
45680Sstevel@tonic-gate 
45690Sstevel@tonic-gate 	return (brn);
45700Sstevel@tonic-gate }
45710Sstevel@tonic-gate 
45720Sstevel@tonic-gate /*
45730Sstevel@tonic-gate  * free the memory allocated for the elements on the branch event queue.
45740Sstevel@tonic-gate  */
45750Sstevel@tonic-gate static void
45760Sstevel@tonic-gate free_brevq(struct brevq_node *brevq)
45770Sstevel@tonic-gate {
45780Sstevel@tonic-gate 	struct brevq_node *brn, *next_brn;
45790Sstevel@tonic-gate 
45800Sstevel@tonic-gate 	for (brn = brevq; brn != NULL; brn = next_brn) {
45811317Scth 		next_brn = brn->brn_sibling;
45821317Scth 		ASSERT(brn->brn_child == NULL);
45831317Scth 		kmem_free(brn->brn_deviname, strlen(brn->brn_deviname) + 1);
45840Sstevel@tonic-gate 		kmem_free(brn, sizeof (*brn));
45850Sstevel@tonic-gate 	}
45860Sstevel@tonic-gate }
45870Sstevel@tonic-gate 
45880Sstevel@tonic-gate /*
45890Sstevel@tonic-gate  * log the events queued up on the branch event queue and free the
45900Sstevel@tonic-gate  * associated memory.
45910Sstevel@tonic-gate  *
45920Sstevel@tonic-gate  * node_path must have been allocated with at least MAXPATHLEN bytes.
45930Sstevel@tonic-gate  */
45940Sstevel@tonic-gate static void
45950Sstevel@tonic-gate log_and_free_brevq(char *node_path, struct brevq_node *brevq)
45960Sstevel@tonic-gate {
45970Sstevel@tonic-gate 	struct brevq_node *brn;
45980Sstevel@tonic-gate 	char *p;
45990Sstevel@tonic-gate 
46000Sstevel@tonic-gate 	p = node_path + strlen(node_path);
46011317Scth 	for (brn = brevq; brn != NULL; brn = brn->brn_sibling) {
46021317Scth 		(void) strcpy(p, brn->brn_deviname);
46030Sstevel@tonic-gate 		(void) i_log_devfs_branch_remove(node_path);
46040Sstevel@tonic-gate 	}
46050Sstevel@tonic-gate 	*p = '\0';
46060Sstevel@tonic-gate 
46070Sstevel@tonic-gate 	free_brevq(brevq);
46080Sstevel@tonic-gate }
46090Sstevel@tonic-gate 
46100Sstevel@tonic-gate /*
46110Sstevel@tonic-gate  * log the events queued up on the branch event queue and free the
46120Sstevel@tonic-gate  * associated memory. Same as the previous function but operates on dip.
46130Sstevel@tonic-gate  */
46140Sstevel@tonic-gate static void
46150Sstevel@tonic-gate log_and_free_brevq_dip(dev_info_t *dip, struct brevq_node *brevq)
46160Sstevel@tonic-gate {
46170Sstevel@tonic-gate 	char *path;
46180Sstevel@tonic-gate 
46190Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
46200Sstevel@tonic-gate 	(void) ddi_pathname(dip, path);
46210Sstevel@tonic-gate 	log_and_free_brevq(path, brevq);
46220Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
46230Sstevel@tonic-gate }
46240Sstevel@tonic-gate 
46250Sstevel@tonic-gate /*
46260Sstevel@tonic-gate  * log the outstanding branch remove events for the grand children of the dip
46270Sstevel@tonic-gate  * and free the associated memory.
46280Sstevel@tonic-gate  */
46290Sstevel@tonic-gate static void
46300Sstevel@tonic-gate log_and_free_br_events_on_grand_children(dev_info_t *dip,
46310Sstevel@tonic-gate     struct brevq_node *brevq)
46320Sstevel@tonic-gate {
46330Sstevel@tonic-gate 	struct brevq_node *brn;
46340Sstevel@tonic-gate 	char *path;
46350Sstevel@tonic-gate 	char *p;
46360Sstevel@tonic-gate 
46370Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
46380Sstevel@tonic-gate 	(void) ddi_pathname(dip, path);
46390Sstevel@tonic-gate 	p = path + strlen(path);
46401317Scth 	for (brn = brevq; brn != NULL; brn = brn->brn_sibling) {
46411317Scth 		if (brn->brn_child) {
46421317Scth 			(void) strcpy(p, brn->brn_deviname);
46430Sstevel@tonic-gate 			/* now path contains the node path to the dip's child */
46441317Scth 			log_and_free_brevq(path, brn->brn_child);
46451317Scth 			brn->brn_child = NULL;
46460Sstevel@tonic-gate 		}
46470Sstevel@tonic-gate 	}
46480Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
46490Sstevel@tonic-gate }
46500Sstevel@tonic-gate 
46510Sstevel@tonic-gate /*
46520Sstevel@tonic-gate  * log and cleanup branch remove events for the grand children of the dip.
46530Sstevel@tonic-gate  */
46540Sstevel@tonic-gate static void
46550Sstevel@tonic-gate cleanup_br_events_on_grand_children(dev_info_t *dip, struct brevq_node **brevqp)
46560Sstevel@tonic-gate {
46570Sstevel@tonic-gate 	dev_info_t *child;
46580Sstevel@tonic-gate 	struct brevq_node *brevq, *brn, *prev_brn, *next_brn;
46590Sstevel@tonic-gate 	char *path;
46600Sstevel@tonic-gate 	int circ;
46610Sstevel@tonic-gate 
46620Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
46630Sstevel@tonic-gate 	prev_brn = NULL;
46640Sstevel@tonic-gate 	brevq = *brevqp;
46650Sstevel@tonic-gate 
46660Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
46670Sstevel@tonic-gate 	for (brn = brevq; brn != NULL; brn = next_brn) {
46681317Scth 		next_brn = brn->brn_sibling;
46690Sstevel@tonic-gate 		for (child = ddi_get_child(dip); child != NULL;
46700Sstevel@tonic-gate 		    child = ddi_get_next_sibling(child)) {
46710Sstevel@tonic-gate 			if (i_ddi_node_state(child) >= DS_INITIALIZED) {
46720Sstevel@tonic-gate 				(void) ddi_deviname(child, path);
46731317Scth 				if (strcmp(path, brn->brn_deviname) == 0)
46740Sstevel@tonic-gate 					break;
46750Sstevel@tonic-gate 			}
46760Sstevel@tonic-gate 		}
46770Sstevel@tonic-gate 
46780Sstevel@tonic-gate 		if (child != NULL && !(DEVI_EVREMOVE(child))) {
46790Sstevel@tonic-gate 			/*
46800Sstevel@tonic-gate 			 * Event state is not REMOVE. So branch remove event
46811317Scth 			 * is not going be generated on brn->brn_child.
46820Sstevel@tonic-gate 			 * If any branch remove events were queued up on
46831317Scth 			 * brn->brn_child log them and remove the brn
46840Sstevel@tonic-gate 			 * from the queue.
46850Sstevel@tonic-gate 			 */
46861317Scth 			if (brn->brn_child) {
46870Sstevel@tonic-gate 				(void) ddi_pathname(dip, path);
46881317Scth 				(void) strcat(path, brn->brn_deviname);
46891317Scth 				log_and_free_brevq(path, brn->brn_child);
46900Sstevel@tonic-gate 			}
46910Sstevel@tonic-gate 
46920Sstevel@tonic-gate 			if (prev_brn)
46931317Scth 				prev_brn->brn_sibling = next_brn;
46940Sstevel@tonic-gate 			else
46950Sstevel@tonic-gate 				*brevqp = next_brn;
46960Sstevel@tonic-gate 
46971317Scth 			kmem_free(brn->brn_deviname,
46981317Scth 			    strlen(brn->brn_deviname) + 1);
46990Sstevel@tonic-gate 			kmem_free(brn, sizeof (*brn));
47000Sstevel@tonic-gate 		} else {
47010Sstevel@tonic-gate 			/*
47020Sstevel@tonic-gate 			 * Free up the outstanding branch remove events
47031317Scth 			 * queued on brn->brn_child since brn->brn_child
47040Sstevel@tonic-gate 			 * itself is eligible for branch remove event.
47050Sstevel@tonic-gate 			 */
47061317Scth 			if (brn->brn_child) {
47071317Scth 				free_brevq(brn->brn_child);
47081317Scth 				brn->brn_child = NULL;
47090Sstevel@tonic-gate 			}
47100Sstevel@tonic-gate 			prev_brn = brn;
47110Sstevel@tonic-gate 		}
47120Sstevel@tonic-gate 	}
47130Sstevel@tonic-gate 
47140Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
47150Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
47160Sstevel@tonic-gate }
47170Sstevel@tonic-gate 
47180Sstevel@tonic-gate static int
47190Sstevel@tonic-gate need_remove_event(dev_info_t *dip, int flags)
47200Sstevel@tonic-gate {
47210Sstevel@tonic-gate 	if ((flags & (NDI_NO_EVENT | NDI_AUTODETACH)) == 0 &&
47220Sstevel@tonic-gate 	    (flags & (NDI_DEVI_OFFLINE | NDI_UNCONFIG | NDI_DEVI_REMOVE)) &&
47230Sstevel@tonic-gate 	    !(DEVI_EVREMOVE(dip)))
47240Sstevel@tonic-gate 		return (1);
47250Sstevel@tonic-gate 	else
47260Sstevel@tonic-gate 		return (0);
47270Sstevel@tonic-gate }
47280Sstevel@tonic-gate 
47290Sstevel@tonic-gate /*
47300Sstevel@tonic-gate  * Unconfigure children/descendants of the dip.
47310Sstevel@tonic-gate  *
47320Sstevel@tonic-gate  * If the operation involves a branch event NDI_BRANCH_EVENT_OP is set
47330Sstevel@tonic-gate  * through out the unconfiguration. On successful return *brevqp is set to
47340Sstevel@tonic-gate  * a queue of dip's child devinames for which branch remove events need
47350Sstevel@tonic-gate  * to be generated.
47360Sstevel@tonic-gate  */
47370Sstevel@tonic-gate static int
47380Sstevel@tonic-gate devi_unconfig_branch(dev_info_t *dip, dev_info_t **dipp, int flags,
47390Sstevel@tonic-gate     struct brevq_node **brevqp)
47400Sstevel@tonic-gate {
47410Sstevel@tonic-gate 	int rval;
47420Sstevel@tonic-gate 
47430Sstevel@tonic-gate 	*brevqp = NULL;
47440Sstevel@tonic-gate 
47450Sstevel@tonic-gate 	if ((!(flags & NDI_BRANCH_EVENT_OP)) && need_remove_event(dip, flags))
47460Sstevel@tonic-gate 		flags |= NDI_BRANCH_EVENT_OP;
47470Sstevel@tonic-gate 
47480Sstevel@tonic-gate 	if (flags & NDI_BRANCH_EVENT_OP) {
47497009Scth 		rval = devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE,
47500Sstevel@tonic-gate 		    brevqp);
47510Sstevel@tonic-gate 
47520Sstevel@tonic-gate 		if (rval != NDI_SUCCESS && (*brevqp)) {
47530Sstevel@tonic-gate 			log_and_free_brevq_dip(dip, *brevqp);
47540Sstevel@tonic-gate 			*brevqp = NULL;
47550Sstevel@tonic-gate 		}
47560Sstevel@tonic-gate 	} else
47577009Scth 		rval = devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE,
47580Sstevel@tonic-gate 		    NULL);
47590Sstevel@tonic-gate 
47600Sstevel@tonic-gate 	return (rval);
47610Sstevel@tonic-gate }
47620Sstevel@tonic-gate 
47630Sstevel@tonic-gate /*
47640Sstevel@tonic-gate  * If the dip is already bound to a driver transition to DS_INITIALIZED
47650Sstevel@tonic-gate  * in order to generate an event in the case where the node was left in
47660Sstevel@tonic-gate  * DS_BOUND state since boot (never got attached) and the node is now
47670Sstevel@tonic-gate  * being offlined.
47680Sstevel@tonic-gate  */
47690Sstevel@tonic-gate static void
47700Sstevel@tonic-gate init_bound_node_ev(dev_info_t *pdip, dev_info_t *dip, int flags)
47710Sstevel@tonic-gate {
47720Sstevel@tonic-gate 	if (need_remove_event(dip, flags) &&
47730Sstevel@tonic-gate 	    i_ddi_node_state(dip) == DS_BOUND &&
47741333Scth 	    i_ddi_devi_attached(pdip) && !DEVI_IS_DEVICE_OFFLINE(dip))
47750Sstevel@tonic-gate 		(void) ddi_initchild(pdip, dip);
47760Sstevel@tonic-gate }
47770Sstevel@tonic-gate 
47780Sstevel@tonic-gate /*
47790Sstevel@tonic-gate  * attach a node/branch with parent already held busy
47800Sstevel@tonic-gate  */
47810Sstevel@tonic-gate static int
47820Sstevel@tonic-gate devi_attach_node(dev_info_t *dip, uint_t flags)
47830Sstevel@tonic-gate {
47842155Scth 	dev_info_t *pdip = ddi_get_parent(dip);
47852155Scth 
47862155Scth 	ASSERT(pdip && DEVI_BUSY_OWNED(pdip));
47872155Scth 
4788495Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
47890Sstevel@tonic-gate 	if (flags & NDI_DEVI_ONLINE) {
47901333Scth 		if (!i_ddi_devi_attached(dip))
4791495Scth 			DEVI_SET_REPORT(dip);
47920Sstevel@tonic-gate 		DEVI_SET_DEVICE_ONLINE(dip);
47930Sstevel@tonic-gate 	}
47940Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
4795495Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
47960Sstevel@tonic-gate 		return (NDI_FAILURE);
47970Sstevel@tonic-gate 	}
4798495Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
47990Sstevel@tonic-gate 
48000Sstevel@tonic-gate 	if (i_ddi_attachchild(dip) != DDI_SUCCESS) {
4801495Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
48020Sstevel@tonic-gate 		DEVI_SET_EVUNINIT(dip);
4803495Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
4804495Scth 
48050Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(dip))
48060Sstevel@tonic-gate 			(void) ddi_uninitchild(dip);
48070Sstevel@tonic-gate 		else {
48080Sstevel@tonic-gate 			/*
48090Sstevel@tonic-gate 			 * Delete .conf nodes and nodes that are not
48100Sstevel@tonic-gate 			 * well formed.
48110Sstevel@tonic-gate 			 */
48120Sstevel@tonic-gate 			(void) ddi_remove_child(dip, 0);
48130Sstevel@tonic-gate 		}
48140Sstevel@tonic-gate 		return (NDI_FAILURE);
48150Sstevel@tonic-gate 	}
48160Sstevel@tonic-gate 
48170Sstevel@tonic-gate 	i_ndi_devi_report_status_change(dip, NULL);
48180Sstevel@tonic-gate 
48190Sstevel@tonic-gate 	/*
48200Sstevel@tonic-gate 	 * log an event, but not during devfs lookups in which case
48210Sstevel@tonic-gate 	 * NDI_NO_EVENT is set.
48220Sstevel@tonic-gate 	 */
48230Sstevel@tonic-gate 	if ((flags & NDI_NO_EVENT) == 0 && !(DEVI_EVADD(dip))) {
48240Sstevel@tonic-gate 		(void) i_log_devfs_add_devinfo(dip, flags);
4825495Scth 
4826495Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
48270Sstevel@tonic-gate 		DEVI_SET_EVADD(dip);
4828495Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
4829495Scth 	} else if (!(flags & NDI_NO_EVENT_STATE_CHNG)) {
4830495Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
48310Sstevel@tonic-gate 		DEVI_SET_EVADD(dip);
4832495Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
4833495Scth 	}
48340Sstevel@tonic-gate 
48350Sstevel@tonic-gate 	return (NDI_SUCCESS);
48360Sstevel@tonic-gate }
48370Sstevel@tonic-gate 
48380Sstevel@tonic-gate /* internal function to config immediate children */
48390Sstevel@tonic-gate static int
48400Sstevel@tonic-gate config_immediate_children(dev_info_t *pdip, uint_t flags, major_t major)
48410Sstevel@tonic-gate {
48422155Scth 	dev_info_t	*child, *next;
48432155Scth 	int		circ;
48442155Scth 
48451333Scth 	ASSERT(i_ddi_devi_attached(pdip));
48460Sstevel@tonic-gate 
48470Sstevel@tonic-gate 	if (!NEXUS_DRV(ddi_get_driver(pdip)))
48480Sstevel@tonic-gate 		return (NDI_SUCCESS);
48490Sstevel@tonic-gate 
48500Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
48510Sstevel@tonic-gate 	    "config_immediate_children: %s%d (%p), flags=%x\n",
48520Sstevel@tonic-gate 	    ddi_driver_name(pdip), ddi_get_instance(pdip),
48530Sstevel@tonic-gate 	    (void *)pdip, flags));
48540Sstevel@tonic-gate 
48550Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
48560Sstevel@tonic-gate 
48570Sstevel@tonic-gate 	if (flags & NDI_CONFIG_REPROBE) {
48580Sstevel@tonic-gate 		mutex_enter(&DEVI(pdip)->devi_lock);
48590Sstevel@tonic-gate 		DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
48600Sstevel@tonic-gate 		mutex_exit(&DEVI(pdip)->devi_lock);
48610Sstevel@tonic-gate 	}
48620Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(pdip, flags);
48630Sstevel@tonic-gate 	i_ndi_init_hw_children(pdip, flags);
48642155Scth 
48652155Scth 	child = ddi_get_child(pdip);
48662155Scth 	while (child) {
48672155Scth 		/* NOTE: devi_attach_node() may remove the dip */
48682155Scth 		next = ddi_get_next_sibling(child);
48692155Scth 
48702155Scth 		/*
48712155Scth 		 * Configure all nexus nodes or leaf nodes with
48722155Scth 		 * matching driver major
48732155Scth 		 */
48747009Scth 		if ((major == DDI_MAJOR_T_NONE) ||
48752155Scth 		    (major == ddi_driver_major(child)) ||
48762155Scth 		    ((flags & NDI_CONFIG) && (is_leaf_node(child) == 0)))
48772155Scth 			(void) devi_attach_node(child, flags);
48782155Scth 		child = next;
48792155Scth 	}
48800Sstevel@tonic-gate 
48810Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
48820Sstevel@tonic-gate 
48830Sstevel@tonic-gate 	return (NDI_SUCCESS);
48840Sstevel@tonic-gate }
48850Sstevel@tonic-gate 
48860Sstevel@tonic-gate /* internal function to config grand children */
48870Sstevel@tonic-gate static int
48880Sstevel@tonic-gate config_grand_children(dev_info_t *pdip, uint_t flags, major_t major)
48890Sstevel@tonic-gate {
48900Sstevel@tonic-gate 	struct mt_config_handle *hdl;
48910Sstevel@tonic-gate 
48920Sstevel@tonic-gate 	/* multi-threaded configuration of child nexus */
48930Sstevel@tonic-gate 	hdl = mt_config_init(pdip, NULL, flags, major, MT_CONFIG_OP, NULL);
48940Sstevel@tonic-gate 	mt_config_children(hdl);
48950Sstevel@tonic-gate 
48960Sstevel@tonic-gate 	return (mt_config_fini(hdl));	/* wait for threads to exit */
48970Sstevel@tonic-gate }
48980Sstevel@tonic-gate 
48990Sstevel@tonic-gate /*
49000Sstevel@tonic-gate  * Common function for device tree configuration,
49010Sstevel@tonic-gate  * either BUS_CONFIG_ALL or BUS_CONFIG_DRIVER.
49020Sstevel@tonic-gate  * The NDI_CONFIG flag causes recursive configuration of
49030Sstevel@tonic-gate  * grandchildren, devfs usage should not recurse.
49040Sstevel@tonic-gate  */
49050Sstevel@tonic-gate static int
49060Sstevel@tonic-gate devi_config_common(dev_info_t *dip, int flags, major_t major)
49070Sstevel@tonic-gate {
49080Sstevel@tonic-gate 	int error;
49090Sstevel@tonic-gate 	int (*f)();
49100Sstevel@tonic-gate 
49111333Scth 	if (!i_ddi_devi_attached(dip))
49120Sstevel@tonic-gate 		return (NDI_FAILURE);
49130Sstevel@tonic-gate 
49140Sstevel@tonic-gate 	if (pm_pre_config(dip, NULL) != DDI_SUCCESS)
49150Sstevel@tonic-gate 		return (NDI_FAILURE);
49160Sstevel@tonic-gate 
49170Sstevel@tonic-gate 	if ((DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
49180Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
49190Sstevel@tonic-gate 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
49200Sstevel@tonic-gate 		error = config_immediate_children(dip, flags, major);
49210Sstevel@tonic-gate 	} else {
49220Sstevel@tonic-gate 		/* call bus_config entry point */
49237009Scth 		ddi_bus_config_op_t bus_op = (major == DDI_MAJOR_T_NONE) ?
49240Sstevel@tonic-gate 		    BUS_CONFIG_ALL : BUS_CONFIG_DRIVER;
49250Sstevel@tonic-gate 		error = (*f)(dip,
49260Sstevel@tonic-gate 		    flags, bus_op, (void *)(uintptr_t)major, NULL, 0);
49270Sstevel@tonic-gate 	}
49280Sstevel@tonic-gate 
49290Sstevel@tonic-gate 	if (error) {
49300Sstevel@tonic-gate 		pm_post_config(dip, NULL);
49310Sstevel@tonic-gate 		return (error);
49320Sstevel@tonic-gate 	}
49330Sstevel@tonic-gate 
49340Sstevel@tonic-gate 	/*
49350Sstevel@tonic-gate 	 * Some callers, notably SCSI, need to mark the devfs cache
49360Sstevel@tonic-gate 	 * to be rebuilt together with the config operation.
49370Sstevel@tonic-gate 	 */
49380Sstevel@tonic-gate 	if (flags & NDI_DEVFS_CLEAN)
49390Sstevel@tonic-gate 		(void) devfs_clean(dip, NULL, 0);
49400Sstevel@tonic-gate 
49410Sstevel@tonic-gate 	if (flags & NDI_CONFIG)
49420Sstevel@tonic-gate 		(void) config_grand_children(dip, flags, major);
49430Sstevel@tonic-gate 
49440Sstevel@tonic-gate 	pm_post_config(dip, NULL);
49450Sstevel@tonic-gate 
49460Sstevel@tonic-gate 	return (NDI_SUCCESS);
49470Sstevel@tonic-gate }
49480Sstevel@tonic-gate 
49490Sstevel@tonic-gate /*
49500Sstevel@tonic-gate  * Framework entry point for BUS_CONFIG_ALL
49510Sstevel@tonic-gate  */
49520Sstevel@tonic-gate int
49530Sstevel@tonic-gate ndi_devi_config(dev_info_t *dip, int flags)
49540Sstevel@tonic-gate {
49550Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
49560Sstevel@tonic-gate 	    "ndi_devi_config: par = %s%d (%p), flags = 0x%x\n",
49570Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
49580Sstevel@tonic-gate 
49597009Scth 	return (devi_config_common(dip, flags, DDI_MAJOR_T_NONE));
49600Sstevel@tonic-gate }
49610Sstevel@tonic-gate 
49620Sstevel@tonic-gate /*
49630Sstevel@tonic-gate  * Framework entry point for BUS_CONFIG_DRIVER, bound to major
49640Sstevel@tonic-gate  */
49650Sstevel@tonic-gate int
49660Sstevel@tonic-gate ndi_devi_config_driver(dev_info_t *dip, int flags, major_t major)
49670Sstevel@tonic-gate {
49680Sstevel@tonic-gate 	/* don't abuse this function */
49697009Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
49700Sstevel@tonic-gate 
49710Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
49720Sstevel@tonic-gate 	    "ndi_devi_config_driver: par = %s%d (%p), flags = 0x%x\n",
49730Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
49740Sstevel@tonic-gate 
49750Sstevel@tonic-gate 	return (devi_config_common(dip, flags, major));
49760Sstevel@tonic-gate }
49770Sstevel@tonic-gate 
49780Sstevel@tonic-gate /*
49792155Scth  * Called by nexus drivers to configure its children.
49800Sstevel@tonic-gate  */
49810Sstevel@tonic-gate static int
49822155Scth devi_config_one(dev_info_t *pdip, char *devnm, dev_info_t **cdipp,
49830Sstevel@tonic-gate     uint_t flags, clock_t timeout)
49840Sstevel@tonic-gate {
49852155Scth 	dev_info_t	*vdip = NULL;
49862155Scth 	char		*drivername = NULL;
49874145Scth 	int		find_by_addr = 0;
49882155Scth 	char		*name, *addr;
49892155Scth 	int		v_circ, p_circ;
49902155Scth 	clock_t		end_time;	/* 60 sec */
49912155Scth 	int		probed;
49922155Scth 	dev_info_t	*cdip;
49932155Scth 	mdi_pathinfo_t	*cpip;
49942155Scth 
49952155Scth 	*cdipp = NULL;
49960Sstevel@tonic-gate 
49970Sstevel@tonic-gate 	if (!NEXUS_DRV(ddi_get_driver(pdip)))
49980Sstevel@tonic-gate 		return (NDI_FAILURE);
49990Sstevel@tonic-gate 
50000Sstevel@tonic-gate 	/* split name into "name@addr" parts */
50010Sstevel@tonic-gate 	i_ddi_parse_name(devnm, &name, &addr, NULL);
50020Sstevel@tonic-gate 
50032155Scth 	/*
50042155Scth 	 * If the nexus is a pHCI and we are not processing a pHCI from
50052155Scth 	 * mdi bus_config code then we need to know the vHCI.
50062155Scth 	 */
50072155Scth 	if (MDI_PHCI(pdip))
50082155Scth 		vdip = mdi_devi_get_vdip(pdip);
50092155Scth 
50102155Scth 	/*
50112155Scth 	 * We may have a genericname on a system that creates drivername
50122155Scth 	 * nodes (from .conf files).  Find the drivername by nodeid. If we
50132155Scth 	 * can't find a node with devnm as the node name then we search by
50142155Scth 	 * drivername.  This allows an implementation to supply a genericly
50155742Scth 	 * named boot path (disk) and locate drivename nodes (sd).  The
50165742Scth 	 * NDI_PROMNAME flag does not apply to /devices/pseudo paths.
50172155Scth 	 */
50185742Scth 	if ((flags & NDI_PROMNAME) && (pdip != pseudo_dip)) {
50192009Sdm120769 		drivername = child_path_to_driver(pdip, name, addr);
50204145Scth 		find_by_addr = 1;
50214145Scth 	}
50222155Scth 
50232155Scth 	/*
50242155Scth 	 * Determine end_time: This routine should *not* be called with a
50252155Scth 	 * constant non-zero timeout argument, the caller should be adjusting
50262155Scth 	 * the timeout argument relative to when it *started* its asynchronous
50272155Scth 	 * enumeration.
50282155Scth 	 */
50292155Scth 	if (timeout > 0)
50302009Sdm120769 		end_time = ddi_get_lbolt() + timeout;
50312155Scth 
50322009Sdm120769 	for (;;) {
50331961Scth 		/*
50342155Scth 		 * For pHCI, enter (vHCI, pHCI) and search for pathinfo/client
50352155Scth 		 * child - break out of for(;;) loop if child found.
50362155Scth 		 * NOTE: Lock order for ndi_devi_enter is (vHCI, pHCI).
50372155Scth 		 */
50382155Scth 		if (vdip) {
50392155Scth 			/* use mdi_devi_enter ordering */
50402155Scth 			ndi_devi_enter(vdip, &v_circ);
50412155Scth 			ndi_devi_enter(pdip, &p_circ);
50422155Scth 			cpip = mdi_pi_find(pdip, NULL, addr);
50432155Scth 			cdip = mdi_pi_get_client(cpip);
50442155Scth 			if (cdip)
50452155Scth 				break;
50462155Scth 		} else
50472155Scth 			ndi_devi_enter(pdip, &p_circ);
50482155Scth 
50492155Scth 		/*
50502155Scth 		 * When not a  vHCI or not all pHCI devices are required to
50512155Scth 		 * enumerated under the vHCI (NDI_MDI_FALLBACK) search for
50522155Scth 		 * devinfo child.
50530Sstevel@tonic-gate 		 */
50542155Scth 		if ((vdip == NULL) || (flags & NDI_MDI_FALLBACK)) {
50552155Scth 			/* determine if .conf nodes already built */
50562155Scth 			probed = (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
50572155Scth 
50582155Scth 			/*
50592155Scth 			 * Search for child by name, if not found then search
50602155Scth 			 * for a node bound to the drivername driver with the
50612155Scth 			 * specified "@addr". Break out of for(;;) loop if
50624145Scth 			 * child found.  To support path-oriented aliases
50634145Scth 			 * binding on boot-device, we do a search_by_addr too.
50642155Scth 			 */
50652155Scth again:			(void) i_ndi_make_spec_children(pdip, flags);
50662155Scth 			cdip = find_child_by_name(pdip, name, addr);
50672155Scth 			if ((cdip == NULL) && drivername)
50682155Scth 				cdip = find_child_by_driver(pdip,
50692155Scth 				    drivername, addr);
50704145Scth 			if ((cdip == NULL) && find_by_addr)
50714145Scth 				cdip = find_child_by_addr(pdip, addr);
50722155Scth 			if (cdip)
50732155Scth 				break;
50742155Scth 
50752155Scth 			/*
50762155Scth 			 * determine if we should reenumerate .conf nodes
50772155Scth 			 * and look for child again.
50782155Scth 			 */
50792155Scth 			if (probed &&
50802155Scth 			    i_ddi_io_initialized() &&
50812155Scth 			    (flags & NDI_CONFIG_REPROBE) &&
50822155Scth 			    ((timeout <= 0) || (ddi_get_lbolt() >= end_time))) {
50832155Scth 				probed = 0;
50842155Scth 				mutex_enter(&DEVI(pdip)->devi_lock);
50852155Scth 				DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
50862155Scth 				mutex_exit(&DEVI(pdip)->devi_lock);
50872155Scth 				goto again;
50882155Scth 			}
50892155Scth 		}
50902155Scth 
50912155Scth 		/* break out of for(;;) if time expired */
50922155Scth 		if ((timeout <= 0) || (ddi_get_lbolt() >= end_time))
50930Sstevel@tonic-gate 			break;
50940Sstevel@tonic-gate 
50950Sstevel@tonic-gate 		/*
50962155Scth 		 * Child not found, exit and wait for asynchronous enumeration
50972155Scth 		 * to add child (or timeout). The addition of a new child (vhci
50982155Scth 		 * or phci) requires the asynchronous enumeration thread to
50992155Scth 		 * ndi_devi_enter/ndi_devi_exit. This exit will signal devi_cv
51002155Scth 		 * and cause us to return from ndi_devi_exit_and_wait, after
51012155Scth 		 * which we loop and search for the requested child again.
51020Sstevel@tonic-gate 		 */
51030Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT,
51040Sstevel@tonic-gate 		    "%s%d: waiting for child %s@%s, timeout %ld",
51050Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
51060Sstevel@tonic-gate 		    name, addr, timeout));
51072155Scth 		if (vdip) {
51082155Scth 			/*
51092155Scth 			 * Mark vHCI for pHCI ndi_devi_exit broadcast.
51102155Scth 			 */
51112155Scth 			mutex_enter(&DEVI(vdip)->devi_lock);
51122155Scth 			DEVI(vdip)->devi_flags |=
51132155Scth 			    DEVI_PHCI_SIGNALS_VHCI;
51142155Scth 			mutex_exit(&DEVI(vdip)->devi_lock);
51152155Scth 			ndi_devi_exit(pdip, p_circ);
51162155Scth 
51172155Scth 			/*
51182155Scth 			 * NB: There is a small race window from above
51192155Scth 			 * ndi_devi_exit() of pdip to cv_wait() in
51202155Scth 			 * ndi_devi_exit_and_wait() which can result in
51212155Scth 			 * not immediately finding a new pHCI child
51222155Scth 			 * of a pHCI that uses NDI_MDI_FAILBACK.
51232155Scth 			 */
51242155Scth 			ndi_devi_exit_and_wait(vdip, v_circ, end_time);
51252155Scth 		} else {
51262155Scth 			ndi_devi_exit_and_wait(pdip, p_circ, end_time);
51272155Scth 		}
51282155Scth 	}
51292155Scth 
51302155Scth 	/* done with paddr, fixup i_ddi_parse_name '@'->'\0' change */
51312155Scth 	if (addr && *addr != '\0')
51320Sstevel@tonic-gate 		*(addr - 1) = '@';
51330Sstevel@tonic-gate 
51342155Scth 	/* attach and hold the child, returning pointer to child */
51352155Scth 	if (cdip && (devi_attach_node(cdip, flags) == NDI_SUCCESS)) {
51362155Scth 		ndi_hold_devi(cdip);
51372155Scth 		*cdipp = cdip;
51382155Scth 	}
51392155Scth 
51402155Scth 	ndi_devi_exit(pdip, p_circ);
51412155Scth 	if (vdip)
51422155Scth 		ndi_devi_exit(vdip, v_circ);
51432155Scth 	return (*cdipp ? NDI_SUCCESS : NDI_FAILURE);
51440Sstevel@tonic-gate }
51450Sstevel@tonic-gate 
51460Sstevel@tonic-gate /*
51470Sstevel@tonic-gate  * Enumerate and attach a child specified by name 'devnm'.
51480Sstevel@tonic-gate  * Called by devfs lookup and DR to perform a BUS_CONFIG_ONE.
51490Sstevel@tonic-gate  * Note: devfs does not make use of NDI_CONFIG to configure
51500Sstevel@tonic-gate  * an entire branch.
51510Sstevel@tonic-gate  */
51520Sstevel@tonic-gate int
51530Sstevel@tonic-gate ndi_devi_config_one(dev_info_t *dip, char *devnm, dev_info_t **dipp, int flags)
51540Sstevel@tonic-gate {
51550Sstevel@tonic-gate 	int error;
51560Sstevel@tonic-gate 	int (*f)();
51570Sstevel@tonic-gate 	int branch_event = 0;
51580Sstevel@tonic-gate 
51590Sstevel@tonic-gate 	ASSERT(dipp);
51601333Scth 	ASSERT(i_ddi_devi_attached(dip));
51610Sstevel@tonic-gate 
51620Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
51630Sstevel@tonic-gate 	    "ndi_devi_config_one: par = %s%d (%p), child = %s\n",
51640Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, devnm));
51650Sstevel@tonic-gate 
51660Sstevel@tonic-gate 	if (pm_pre_config(dip, devnm) != DDI_SUCCESS)
51670Sstevel@tonic-gate 		return (NDI_FAILURE);
51680Sstevel@tonic-gate 
51690Sstevel@tonic-gate 	if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
51700Sstevel@tonic-gate 	    (flags & NDI_CONFIG)) {
51710Sstevel@tonic-gate 		flags |= NDI_BRANCH_EVENT_OP;
51720Sstevel@tonic-gate 		branch_event = 1;
51730Sstevel@tonic-gate 	}
51740Sstevel@tonic-gate 
51750Sstevel@tonic-gate 	if ((DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
51760Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
51770Sstevel@tonic-gate 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
51780Sstevel@tonic-gate 		error = devi_config_one(dip, devnm, dipp, flags, 0);
51790Sstevel@tonic-gate 	} else {
51800Sstevel@tonic-gate 		/* call bus_config entry point */
51810Sstevel@tonic-gate 		error = (*f)(dip, flags, BUS_CONFIG_ONE, (void *)devnm, dipp);
51820Sstevel@tonic-gate 	}
51830Sstevel@tonic-gate 
51840Sstevel@tonic-gate 	if (error || (flags & NDI_CONFIG) == 0) {
51850Sstevel@tonic-gate 		pm_post_config(dip, devnm);
51860Sstevel@tonic-gate 		return (error);
51870Sstevel@tonic-gate 	}
51880Sstevel@tonic-gate 
51890Sstevel@tonic-gate 	/*
51904145Scth 	 * DR usage (i.e. call with NDI_CONFIG) recursively configures
51910Sstevel@tonic-gate 	 * grandchildren, performing a BUS_CONFIG_ALL from the node attached
51920Sstevel@tonic-gate 	 * by the BUS_CONFIG_ONE.
51930Sstevel@tonic-gate 	 */
51940Sstevel@tonic-gate 	ASSERT(*dipp);
51950Sstevel@tonic-gate 
51967009Scth 	error = devi_config_common(*dipp, flags, DDI_MAJOR_T_NONE);
51970Sstevel@tonic-gate 
51980Sstevel@tonic-gate 	pm_post_config(dip, devnm);
51990Sstevel@tonic-gate 
52000Sstevel@tonic-gate 	if (branch_event)
52010Sstevel@tonic-gate 		(void) i_log_devfs_branch_add(*dipp);
52020Sstevel@tonic-gate 
52030Sstevel@tonic-gate 	return (error);
52040Sstevel@tonic-gate }
52050Sstevel@tonic-gate 
52060Sstevel@tonic-gate 
52070Sstevel@tonic-gate /*
52080Sstevel@tonic-gate  * Enumerate and attach a child specified by name 'devnm'.
52090Sstevel@tonic-gate  * Called during configure the OBP options. This configures
52100Sstevel@tonic-gate  * only one node.
52110Sstevel@tonic-gate  */
52120Sstevel@tonic-gate static int
52130Sstevel@tonic-gate ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
52140Sstevel@tonic-gate     dev_info_t **childp, int flags)
52150Sstevel@tonic-gate {
52160Sstevel@tonic-gate 	int error;
52170Sstevel@tonic-gate 	int (*f)();
52180Sstevel@tonic-gate 
52190Sstevel@tonic-gate 	ASSERT(childp);
52201333Scth 	ASSERT(i_ddi_devi_attached(parent));
52210Sstevel@tonic-gate 
52220Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_config_obp_args: "
52230Sstevel@tonic-gate 	    "par = %s%d (%p), child = %s\n", ddi_driver_name(parent),
52240Sstevel@tonic-gate 	    ddi_get_instance(parent), (void *)parent, devnm));
52250Sstevel@tonic-gate 
52260Sstevel@tonic-gate 	if ((DEVI(parent)->devi_ops->devo_bus_ops == NULL) ||
52270Sstevel@tonic-gate 	    (DEVI(parent)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
52280Sstevel@tonic-gate 	    (f = DEVI(parent)->devi_ops->devo_bus_ops->bus_config) == NULL) {
52290Sstevel@tonic-gate 		error = NDI_FAILURE;
52300Sstevel@tonic-gate 	} else {
52310Sstevel@tonic-gate 		/* call bus_config entry point */
52320Sstevel@tonic-gate 		error = (*f)(parent, flags,
52330Sstevel@tonic-gate 		    BUS_CONFIG_OBP_ARGS, (void *)devnm, childp);
52340Sstevel@tonic-gate 	}
52350Sstevel@tonic-gate 	return (error);
52360Sstevel@tonic-gate }
52370Sstevel@tonic-gate 
52384845Svikram /*
52394845Svikram  * Pay attention, the following is a bit tricky:
52404845Svikram  * There are three possible cases when constraints are applied
52414845Svikram  *
52424845Svikram  *	- A constraint is applied and the offline is disallowed.
52434845Svikram  *	  Simply return failure and block the offline
52444845Svikram  *
52454845Svikram  *	- A constraint is applied and the offline is allowed.
52464845Svikram  *	  Mark the dip as having passed the constraint and allow
52474845Svikram  *	  offline to proceed.
52484845Svikram  *
52494845Svikram  *	- A constraint is not applied. Allow the offline to proceed for now.
52504845Svikram  *
52514845Svikram  * In the latter two cases we allow the offline to proceed. If the
52524845Svikram  * offline succeeds (no users) everything is fine. It is ok for an unused
52534845Svikram  * device to be offlined even if no constraints were imposed on the offline.
52544845Svikram  * If the offline fails because there are users, we look at the constraint
52554845Svikram  * flag on the dip. If the constraint flag is set (implying that it passed
52564845Svikram  * a constraint) we allow the dip to be retired. If not, we don't allow
52574845Svikram  * the retire. This ensures that we don't allow unconstrained retire.
52584845Svikram  */
52594845Svikram int
52604845Svikram e_ddi_offline_notify(dev_info_t *dip)
52614845Svikram {
52624845Svikram 	int retval;
52634845Svikram 	int constraint;
52644845Svikram 	int failure;
52654845Svikram 
52664845Svikram 	RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): entered: dip=%p",
52674845Svikram 	    (void *) dip));
52684845Svikram 
52694845Svikram 	constraint = 0;
52704845Svikram 	failure = 0;
52714845Svikram 
52724845Svikram 	/*
52734845Svikram 	 * Start with userland constraints first - applied via device contracts
52744845Svikram 	 */
52754845Svikram 	retval = contract_device_offline(dip, DDI_DEV_T_ANY, 0);
52764845Svikram 	switch (retval) {
52774845Svikram 	case CT_NACK:
52784845Svikram 		RIO_DEBUG((CE_NOTE, "Received NACK for dip=%p", (void *)dip));
52794845Svikram 		failure = 1;
52804845Svikram 		goto out;
52814845Svikram 	case CT_ACK:
52824845Svikram 		constraint = 1;
52834845Svikram 		RIO_DEBUG((CE_NOTE, "Received ACK for dip=%p", (void *)dip));
52844845Svikram 		break;
52854845Svikram 	case CT_NONE:
52864845Svikram 		/* no contracts */
52874845Svikram 		RIO_DEBUG((CE_NOTE, "No contracts on dip=%p", (void *)dip));
52884845Svikram 		break;
52894845Svikram 	default:
52904845Svikram 		ASSERT(retval == CT_NONE);
52914845Svikram 	}
52924845Svikram 
52934845Svikram 	/*
52944845Svikram 	 * Next, use LDI to impose kernel constraints
52954845Svikram 	 */
52964845Svikram 	retval = ldi_invoke_notify(dip, DDI_DEV_T_ANY, 0, LDI_EV_OFFLINE, NULL);
52974845Svikram 	switch (retval) {
52984845Svikram 	case LDI_EV_FAILURE:
52994845Svikram 		contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_FAILURE);
53004845Svikram 		RIO_DEBUG((CE_NOTE, "LDI callback failed on dip=%p",
53014845Svikram 		    (void *)dip));
53024845Svikram 		failure = 1;
53034845Svikram 		goto out;
53044845Svikram 	case LDI_EV_SUCCESS:
53054845Svikram 		constraint = 1;
53064845Svikram 		RIO_DEBUG((CE_NOTE, "LDI callback success on dip=%p",
53074845Svikram 		    (void *)dip));
53084845Svikram 		break;
53094845Svikram 	case LDI_EV_NONE:
53104845Svikram 		/* no matching LDI callbacks */
53114845Svikram 		RIO_DEBUG((CE_NOTE, "No LDI callbacks for dip=%p",
53124845Svikram 		    (void *)dip));
53134845Svikram 		break;
53144845Svikram 	default:
53154845Svikram 		ASSERT(retval == LDI_EV_NONE);
53164845Svikram 	}
53174845Svikram 
53184845Svikram out:
53194845Svikram 	mutex_enter(&(DEVI(dip)->devi_lock));
53204845Svikram 	if ((DEVI(dip)->devi_flags & DEVI_RETIRING) && failure) {
53214845Svikram 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): setting "
53224845Svikram 		    "BLOCKED flag. dip=%p", (void *)dip));
53234845Svikram 		DEVI(dip)->devi_flags |= DEVI_R_BLOCKED;
53244845Svikram 		if (DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT) {
53254845Svikram 			RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): "
53264845Svikram 			    "blocked. clearing RCM CONSTRAINT flag. dip=%p",
53274845Svikram 			    (void *)dip));
53284845Svikram 			DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
53294845Svikram 		}
53304845Svikram 	} else if ((DEVI(dip)->devi_flags & DEVI_RETIRING) && constraint) {
53314845Svikram 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): setting "
53324845Svikram 		    "CONSTRAINT flag. dip=%p", (void *)dip));
53334845Svikram 		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
53344845Svikram 	} else if ((DEVI(dip)->devi_flags & DEVI_RETIRING) &&
53354845Svikram 	    DEVI(dip)->devi_ref == 0) {
53364845Svikram 		/* also allow retire if device is not in use */
53374845Svikram 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): device not in "
53384845Svikram 		    "use. Setting CONSTRAINT flag. dip=%p", (void *)dip));
53394845Svikram 		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
53404845Svikram 	} else {
53414845Svikram 		/*
53424845Svikram 		 * Note: We cannot ASSERT here that DEVI_R_CONSTRAINT is
53434845Svikram 		 * not set, since other sources (such as RCM) may have
53444845Svikram 		 * set the flag.
53454845Svikram 		 */
53464845Svikram 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): not setting "
53474845Svikram 		    "constraint flag. dip=%p", (void *)dip));
53484845Svikram 	}
53494845Svikram 	mutex_exit(&(DEVI(dip)->devi_lock));
53504845Svikram 
53514845Svikram 
53524845Svikram 	RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): exit: dip=%p",
53534845Svikram 	    (void *) dip));
53544845Svikram 
53554845Svikram 	return (failure ? DDI_FAILURE : DDI_SUCCESS);
53564845Svikram }
53574845Svikram 
53584845Svikram void
53594845Svikram e_ddi_offline_finalize(dev_info_t *dip, int result)
53604845Svikram {
53614845Svikram 	RIO_DEBUG((CE_NOTE, "e_ddi_offline_finalize(): entry: result=%s, "
53624845Svikram 	    "dip=%p", result == DDI_SUCCESS ? "SUCCESS" : "FAILURE",
53634845Svikram 	    (void *)dip));
53644845Svikram 
53654845Svikram 	contract_device_negend(dip, DDI_DEV_T_ANY, 0,  result == DDI_SUCCESS ?
53664845Svikram 	    CT_EV_SUCCESS : CT_EV_FAILURE);
53674845Svikram 
53684845Svikram 	ldi_invoke_finalize(dip, DDI_DEV_T_ANY, 0,
53694845Svikram 	    LDI_EV_OFFLINE, result == DDI_SUCCESS ?
53704845Svikram 	    LDI_EV_SUCCESS : LDI_EV_FAILURE, NULL);
53714845Svikram 
53724845Svikram 	RIO_VERBOSE((CE_NOTE, "e_ddi_offline_finalize(): exit: dip=%p",
53734845Svikram 	    (void *)dip));
53744845Svikram }
53754845Svikram 
53764845Svikram void
53774845Svikram e_ddi_degrade_finalize(dev_info_t *dip)
53784845Svikram {
53794845Svikram 	RIO_DEBUG((CE_NOTE, "e_ddi_degrade_finalize(): entry: "
53804845Svikram 	    "result always = DDI_SUCCESS, dip=%p", (void *)dip));
53814845Svikram 
53824845Svikram 	contract_device_degrade(dip, DDI_DEV_T_ANY, 0);
53834845Svikram 	contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_SUCCESS);
53844845Svikram 
53854845Svikram 	ldi_invoke_finalize(dip, DDI_DEV_T_ANY, 0, LDI_EV_DEGRADE,
53864845Svikram 	    LDI_EV_SUCCESS, NULL);
53874845Svikram 
53884845Svikram 	RIO_VERBOSE((CE_NOTE, "e_ddi_degrade_finalize(): exit: dip=%p",
53894845Svikram 	    (void *)dip));
53904845Svikram }
53914845Svikram 
53924845Svikram void
53934845Svikram e_ddi_undegrade_finalize(dev_info_t *dip)
53944845Svikram {
53954845Svikram 	RIO_DEBUG((CE_NOTE, "e_ddi_undegrade_finalize(): entry: "
53964845Svikram 	    "result always = DDI_SUCCESS, dip=%p", (void *)dip));
53974845Svikram 
53984845Svikram 	contract_device_undegrade(dip, DDI_DEV_T_ANY, 0);
53994845Svikram 	contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_SUCCESS);
54004845Svikram 
54014845Svikram 	RIO_VERBOSE((CE_NOTE, "e_ddi_undegrade_finalize(): exit: dip=%p",
54024845Svikram 	    (void *)dip));
54034845Svikram }
54040Sstevel@tonic-gate 
54050Sstevel@tonic-gate /*
54060Sstevel@tonic-gate  * detach a node with parent already held busy
54070Sstevel@tonic-gate  */
54080Sstevel@tonic-gate static int
54090Sstevel@tonic-gate devi_detach_node(dev_info_t *dip, uint_t flags)
54100Sstevel@tonic-gate {
54110Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
54120Sstevel@tonic-gate 	int ret = NDI_SUCCESS;
54130Sstevel@tonic-gate 	ddi_eventcookie_t cookie;
54140Sstevel@tonic-gate 
54152155Scth 	ASSERT(pdip && DEVI_BUSY_OWNED(pdip));
54162155Scth 
54174845Svikram 	/*
54184845Svikram 	 * Invoke notify if offlining
54194845Svikram 	 */
54204845Svikram 	if (flags & NDI_DEVI_OFFLINE) {
54214845Svikram 		RIO_DEBUG((CE_NOTE, "devi_detach_node: offlining dip=%p",
54224845Svikram 		    (void *)dip));
54234845Svikram 		if (e_ddi_offline_notify(dip) != DDI_SUCCESS) {
54244845Svikram 			RIO_DEBUG((CE_NOTE, "devi_detach_node: offline NACKed"
54254845Svikram 			    "dip=%p", (void *)dip));
54264845Svikram 			return (NDI_FAILURE);
54274845Svikram 		}
54284845Svikram 	}
54294845Svikram 
54300Sstevel@tonic-gate 	if (flags & NDI_POST_EVENT) {
54312155Scth 		if (i_ddi_devi_attached(pdip)) {
54320Sstevel@tonic-gate 			if (ddi_get_eventcookie(dip, DDI_DEVI_REMOVE_EVENT,
54330Sstevel@tonic-gate 			    &cookie) == NDI_SUCCESS)
54340Sstevel@tonic-gate 				(void) ndi_post_event(dip, dip, cookie, NULL);
54350Sstevel@tonic-gate 		}
54360Sstevel@tonic-gate 	}
54370Sstevel@tonic-gate 
54384845Svikram 	if (i_ddi_detachchild(dip, flags) != DDI_SUCCESS) {
54394845Svikram 		if (flags & NDI_DEVI_OFFLINE) {
54404845Svikram 			RIO_DEBUG((CE_NOTE, "devi_detach_node: offline failed."
54414845Svikram 			    " Calling e_ddi_offline_finalize with result=%d. "
54424845Svikram 			    "dip=%p", DDI_FAILURE, (void *)dip));
54434845Svikram 			e_ddi_offline_finalize(dip, DDI_FAILURE);
54444845Svikram 		}
54450Sstevel@tonic-gate 		return (NDI_FAILURE);
54464845Svikram 	}
54474845Svikram 
54484845Svikram 	if (flags & NDI_DEVI_OFFLINE) {
54494845Svikram 		RIO_DEBUG((CE_NOTE, "devi_detach_node: offline succeeded."
54504845Svikram 		    " Calling e_ddi_offline_finalize with result=%d, "
54514845Svikram 		    "dip=%p", DDI_SUCCESS, (void *)dip));
54524845Svikram 		e_ddi_offline_finalize(dip, DDI_SUCCESS);
54534845Svikram 	}
54540Sstevel@tonic-gate 
54550Sstevel@tonic-gate 	if (flags & NDI_AUTODETACH)
54560Sstevel@tonic-gate 		return (NDI_SUCCESS);
54570Sstevel@tonic-gate 
54580Sstevel@tonic-gate 	/*
54590Sstevel@tonic-gate 	 * For DR, even bound nodes may need to have offline
54600Sstevel@tonic-gate 	 * flag set.
54610Sstevel@tonic-gate 	 */
54620Sstevel@tonic-gate 	if (flags & NDI_DEVI_OFFLINE) {
5463495Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
54640Sstevel@tonic-gate 		DEVI_SET_DEVICE_OFFLINE(dip);
5465495Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
54660Sstevel@tonic-gate 	}
54670Sstevel@tonic-gate 
54680Sstevel@tonic-gate 	if (i_ddi_node_state(dip) == DS_INITIALIZED) {
54690Sstevel@tonic-gate 		char *path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
54700Sstevel@tonic-gate 		(void) ddi_pathname(dip, path);
54710Sstevel@tonic-gate 		if (flags & NDI_DEVI_OFFLINE)
54720Sstevel@tonic-gate 			i_ndi_devi_report_status_change(dip, path);
54730Sstevel@tonic-gate 
54740Sstevel@tonic-gate 		if (need_remove_event(dip, flags)) {
54750Sstevel@tonic-gate 			(void) i_log_devfs_remove_devinfo(path,
54760Sstevel@tonic-gate 			    i_ddi_devi_class(dip),
54770Sstevel@tonic-gate 			    (char *)ddi_driver_name(dip),
54780Sstevel@tonic-gate 			    ddi_get_instance(dip),
54790Sstevel@tonic-gate 			    flags);
5480495Scth 			mutex_enter(&(DEVI(dip)->devi_lock));
54810Sstevel@tonic-gate 			DEVI_SET_EVREMOVE(dip);
5482495Scth 			mutex_exit(&(DEVI(dip)->devi_lock));
54830Sstevel@tonic-gate 		}
54840Sstevel@tonic-gate 		kmem_free(path, MAXPATHLEN);
54850Sstevel@tonic-gate 	}
54860Sstevel@tonic-gate 
54870Sstevel@tonic-gate 	if (flags & (NDI_UNCONFIG | NDI_DEVI_REMOVE)) {
54880Sstevel@tonic-gate 		ret = ddi_uninitchild(dip);
54890Sstevel@tonic-gate 		if (ret == NDI_SUCCESS) {
54900Sstevel@tonic-gate 			/*
54910Sstevel@tonic-gate 			 * Remove uninitialized pseudo nodes because
54920Sstevel@tonic-gate 			 * system props are lost and the node cannot be
54930Sstevel@tonic-gate 			 * reattached.
54940Sstevel@tonic-gate 			 */
54950Sstevel@tonic-gate 			if (!ndi_dev_is_persistent_node(dip))
54960Sstevel@tonic-gate 				flags |= NDI_DEVI_REMOVE;
54970Sstevel@tonic-gate 
54980Sstevel@tonic-gate 			if (flags & NDI_DEVI_REMOVE)
54990Sstevel@tonic-gate 				ret = ddi_remove_child(dip, 0);
55000Sstevel@tonic-gate 		}
55010Sstevel@tonic-gate 	}
55020Sstevel@tonic-gate 
55030Sstevel@tonic-gate 	return (ret);
55040Sstevel@tonic-gate }
55050Sstevel@tonic-gate 
55060Sstevel@tonic-gate /*
55070Sstevel@tonic-gate  * unconfigure immediate children of bus nexus device
55080Sstevel@tonic-gate  */
55090Sstevel@tonic-gate static int
55100Sstevel@tonic-gate unconfig_immediate_children(
55110Sstevel@tonic-gate 	dev_info_t *dip,
55120Sstevel@tonic-gate 	dev_info_t **dipp,
55130Sstevel@tonic-gate 	int flags,
55140Sstevel@tonic-gate 	major_t major)
55150Sstevel@tonic-gate {
55162155Scth 	int rv = NDI_SUCCESS;
55172155Scth 	int circ, vcirc;
55180Sstevel@tonic-gate 	dev_info_t *child;
55192155Scth 	dev_info_t *vdip = NULL;
55202155Scth 	dev_info_t *next;
55210Sstevel@tonic-gate 
55220Sstevel@tonic-gate 	ASSERT(dipp == NULL || *dipp == NULL);
55230Sstevel@tonic-gate 
55242155Scth 	/*
55252155Scth 	 * Scan forward to see if we will be processing a pHCI child. If we
55262155Scth 	 * have a child that is a pHCI and vHCI and pHCI are not siblings then
55272155Scth 	 * enter vHCI before parent(pHCI) to prevent deadlock with mpxio
55282155Scth 	 * Client power management operations.
55292155Scth 	 */
55300Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
55312155Scth 	for (child = ddi_get_child(dip); child;
55322155Scth 	    child = ddi_get_next_sibling(child)) {
55332155Scth 		/* skip same nodes we skip below */
55347009Scth 		if (((major != DDI_MAJOR_T_NONE) &&
55352155Scth 		    (major != ddi_driver_major(child))) ||
55362155Scth 		    ((flags & NDI_AUTODETACH) && !is_leaf_node(child)))
55372155Scth 			continue;
55382155Scth 
55392155Scth 		if (MDI_PHCI(child)) {
55402155Scth 			vdip = mdi_devi_get_vdip(child);
55412155Scth 			/*
55422155Scth 			 * If vHCI and vHCI is not a sibling of pHCI
55432155Scth 			 * then enter in (vHCI, parent(pHCI)) order.
55442155Scth 			 */
55452155Scth 			if (vdip && (ddi_get_parent(vdip) != dip)) {
55462155Scth 				ndi_devi_exit(dip, circ);
55472155Scth 
55482155Scth 				/* use mdi_devi_enter ordering */
55492155Scth 				ndi_devi_enter(vdip, &vcirc);
55502155Scth 				ndi_devi_enter(dip, &circ);
55512155Scth 				break;
55522155Scth 			} else
55532155Scth 				vdip = NULL;
55542155Scth 		}
55552155Scth 	}
55562155Scth 
55570Sstevel@tonic-gate 	child = ddi_get_child(dip);
55580Sstevel@tonic-gate 	while (child) {
55592155Scth 		next = ddi_get_next_sibling(child);
55602155Scth 
55617009Scth 		if ((major != DDI_MAJOR_T_NONE) &&
55620Sstevel@tonic-gate 		    (major != ddi_driver_major(child))) {
55630Sstevel@tonic-gate 			child = next;
55640Sstevel@tonic-gate 			continue;
55650Sstevel@tonic-gate 		}
55660Sstevel@tonic-gate 
55670Sstevel@tonic-gate 		/* skip nexus nodes during autodetach */
55680Sstevel@tonic-gate 		if ((flags & NDI_AUTODETACH) && !is_leaf_node(child)) {
55690Sstevel@tonic-gate 			child = next;
55700Sstevel@tonic-gate 			continue;
55710Sstevel@tonic-gate 		}
55720Sstevel@tonic-gate 
55730Sstevel@tonic-gate 		if (devi_detach_node(child, flags) != NDI_SUCCESS) {
55740Sstevel@tonic-gate 			if (dipp && *dipp == NULL) {
55750Sstevel@tonic-gate 				ndi_hold_devi(child);
55760Sstevel@tonic-gate 				*dipp = child;
55770Sstevel@tonic-gate 			}
55780Sstevel@tonic-gate 			rv = NDI_FAILURE;
55790Sstevel@tonic-gate 		}
55800Sstevel@tonic-gate 
55810Sstevel@tonic-gate 		/*
55820Sstevel@tonic-gate 		 * Continue upon failure--best effort algorithm
55830Sstevel@tonic-gate 		 */
55840Sstevel@tonic-gate 		child = next;
55850Sstevel@tonic-gate 	}
55862155Scth 
55870Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
55882155Scth 	if (vdip)
55892155Scth 		ndi_devi_exit(vdip, vcirc);
55902155Scth 
55910Sstevel@tonic-gate 	return (rv);
55920Sstevel@tonic-gate }
55930Sstevel@tonic-gate 
55940Sstevel@tonic-gate /*
55950Sstevel@tonic-gate  * unconfigure grand children of bus nexus device
55960Sstevel@tonic-gate  */
55970Sstevel@tonic-gate static int
55980Sstevel@tonic-gate unconfig_grand_children(
55990Sstevel@tonic-gate 	dev_info_t *dip,
56000Sstevel@tonic-gate 	dev_info_t **dipp,
56010Sstevel@tonic-gate 	int flags,
56020Sstevel@tonic-gate 	major_t major,
56030Sstevel@tonic-gate 	struct brevq_node **brevqp)
56040Sstevel@tonic-gate {
56050Sstevel@tonic-gate 	struct mt_config_handle *hdl;
56060Sstevel@tonic-gate 
56070Sstevel@tonic-gate 	if (brevqp)
56080Sstevel@tonic-gate 		*brevqp = NULL;
56090Sstevel@tonic-gate 
56100Sstevel@tonic-gate 	/* multi-threaded configuration of child nexus */
56110Sstevel@tonic-gate 	hdl = mt_config_init(dip, dipp, flags, major, MT_UNCONFIG_OP, brevqp);
56120Sstevel@tonic-gate 	mt_config_children(hdl);
56130Sstevel@tonic-gate 
56140Sstevel@tonic-gate 	return (mt_config_fini(hdl));	/* wait for threads to exit */
56150Sstevel@tonic-gate }
56160Sstevel@tonic-gate 
56170Sstevel@tonic-gate /*
56180Sstevel@tonic-gate  * Unconfigure children/descendants of the dip.
56190Sstevel@tonic-gate  *
56200Sstevel@tonic-gate  * If brevqp is not NULL, on return *brevqp is set to a queue of dip's
56210Sstevel@tonic-gate  * child devinames for which branch remove events need to be generated.
56220Sstevel@tonic-gate  */
56230Sstevel@tonic-gate static int
56240Sstevel@tonic-gate devi_unconfig_common(
56250Sstevel@tonic-gate 	dev_info_t *dip,
56260Sstevel@tonic-gate 	dev_info_t **dipp,
56270Sstevel@tonic-gate 	int flags,
56280Sstevel@tonic-gate 	major_t major,
56290Sstevel@tonic-gate 	struct brevq_node **brevqp)
56300Sstevel@tonic-gate {
56310Sstevel@tonic-gate 	int rv;
56320Sstevel@tonic-gate 	int pm_cookie;
56330Sstevel@tonic-gate 	int (*f)();
56340Sstevel@tonic-gate 	ddi_bus_config_op_t bus_op;
56350Sstevel@tonic-gate 
56360Sstevel@tonic-gate 	if (dipp)
56370Sstevel@tonic-gate 		*dipp = NULL;
56380Sstevel@tonic-gate 	if (brevqp)
56390Sstevel@tonic-gate 		*brevqp = NULL;
56400Sstevel@tonic-gate 
56410Sstevel@tonic-gate 	/*
56420Sstevel@tonic-gate 	 * Power up the dip if it is powered off.  If the flag bit
56430Sstevel@tonic-gate 	 * NDI_AUTODETACH is set and the dip is not at its full power,
56440Sstevel@tonic-gate 	 * skip the rest of the branch.
56450Sstevel@tonic-gate 	 */
56460Sstevel@tonic-gate 	if (pm_pre_unconfig(dip, flags, &pm_cookie, NULL) != DDI_SUCCESS)
56470Sstevel@tonic-gate 		return ((flags & NDI_AUTODETACH) ? NDI_SUCCESS :
56480Sstevel@tonic-gate 		    NDI_FAILURE);
56490Sstevel@tonic-gate 
56500Sstevel@tonic-gate 	/*
56510Sstevel@tonic-gate 	 * Some callers, notably SCSI, need to clear out the devfs
56520Sstevel@tonic-gate 	 * cache together with the unconfig to prevent stale entries.
56530Sstevel@tonic-gate 	 */
56540Sstevel@tonic-gate 	if (flags & NDI_DEVFS_CLEAN)
56550Sstevel@tonic-gate 		(void) devfs_clean(dip, NULL, 0);
56560Sstevel@tonic-gate 
56570Sstevel@tonic-gate 	rv = unconfig_grand_children(dip, dipp, flags, major, brevqp);
56580Sstevel@tonic-gate 
56590Sstevel@tonic-gate 	if ((rv != NDI_SUCCESS) && ((flags & NDI_AUTODETACH) == 0)) {
56600Sstevel@tonic-gate 		if (brevqp && *brevqp) {
56610Sstevel@tonic-gate 			log_and_free_br_events_on_grand_children(dip, *brevqp);
56620Sstevel@tonic-gate 			free_brevq(*brevqp);
56630Sstevel@tonic-gate 			*brevqp = NULL;
56640Sstevel@tonic-gate 		}
56650Sstevel@tonic-gate 		pm_post_unconfig(dip, pm_cookie, NULL);
56660Sstevel@tonic-gate 		return (rv);
56670Sstevel@tonic-gate 	}
56680Sstevel@tonic-gate 
56690Sstevel@tonic-gate 	if (dipp && *dipp) {
56700Sstevel@tonic-gate 		ndi_rele_devi(*dipp);
56710Sstevel@tonic-gate 		*dipp = NULL;
56720Sstevel@tonic-gate 	}
56730Sstevel@tonic-gate 
56740Sstevel@tonic-gate 	/*
56750Sstevel@tonic-gate 	 * It is possible to have a detached nexus with children
56760Sstevel@tonic-gate 	 * and grandchildren (for example: a branch consisting
56770Sstevel@tonic-gate 	 * entirely of bound nodes.) Since the nexus is detached
56780Sstevel@tonic-gate 	 * the bus_unconfig entry point cannot be used to remove
56790Sstevel@tonic-gate 	 * or unconfigure the descendants.
56800Sstevel@tonic-gate 	 */
56811333Scth 	if (!i_ddi_devi_attached(dip) ||
56820Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
56830Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
56840Sstevel@tonic-gate 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
56850Sstevel@tonic-gate 		rv = unconfig_immediate_children(dip, dipp, flags, major);
56860Sstevel@tonic-gate 	} else {
56870Sstevel@tonic-gate 		/*
56880Sstevel@tonic-gate 		 * call bus_unconfig entry point
56890Sstevel@tonic-gate 		 * It should reset nexus flags if unconfigure succeeds.
56900Sstevel@tonic-gate 		 */
56917009Scth 		bus_op = (major == DDI_MAJOR_T_NONE) ?
56920Sstevel@tonic-gate 		    BUS_UNCONFIG_ALL : BUS_UNCONFIG_DRIVER;
56930Sstevel@tonic-gate 		rv = (*f)(dip, flags, bus_op, (void *)(uintptr_t)major);
56940Sstevel@tonic-gate 	}
56950Sstevel@tonic-gate 
56960Sstevel@tonic-gate 	pm_post_unconfig(dip, pm_cookie, NULL);
56970Sstevel@tonic-gate 
56980Sstevel@tonic-gate 	if (brevqp && *brevqp)
56990Sstevel@tonic-gate 		cleanup_br_events_on_grand_children(dip, brevqp);
57000Sstevel@tonic-gate 
57010Sstevel@tonic-gate 	return (rv);
57020Sstevel@tonic-gate }
57030Sstevel@tonic-gate 
57040Sstevel@tonic-gate /*
57050Sstevel@tonic-gate  * called by devfs/framework to unconfigure children bound to major
57060Sstevel@tonic-gate  * If NDI_AUTODETACH is specified, this is invoked by either the
57070Sstevel@tonic-gate  * moduninstall daemon or the modunload -i 0 command.
57080Sstevel@tonic-gate  */
57090Sstevel@tonic-gate int
57100Sstevel@tonic-gate ndi_devi_unconfig_driver(dev_info_t *dip, int flags, major_t major)
57110Sstevel@tonic-gate {
57120Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
57130Sstevel@tonic-gate 	    "ndi_devi_unconfig_driver: par = %s%d (%p), flags = 0x%x\n",
57140Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
57150Sstevel@tonic-gate 
57160Sstevel@tonic-gate 	return (devi_unconfig_common(dip, NULL, flags, major, NULL));
57170Sstevel@tonic-gate }
57180Sstevel@tonic-gate 
57190Sstevel@tonic-gate int
57200Sstevel@tonic-gate ndi_devi_unconfig(dev_info_t *dip, int flags)
57210Sstevel@tonic-gate {
57220Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
57230Sstevel@tonic-gate 	    "ndi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
57240Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
57250Sstevel@tonic-gate 
57267009Scth 	return (devi_unconfig_common(dip, NULL, flags, DDI_MAJOR_T_NONE, NULL));
57270Sstevel@tonic-gate }
57280Sstevel@tonic-gate 
57290Sstevel@tonic-gate int
57300Sstevel@tonic-gate e_ddi_devi_unconfig(dev_info_t *dip, dev_info_t **dipp, int flags)
57310Sstevel@tonic-gate {
57320Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
57330Sstevel@tonic-gate 	    "e_ddi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
57340Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
57350Sstevel@tonic-gate 
57367009Scth 	return (devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE, NULL));
57370Sstevel@tonic-gate }
57380Sstevel@tonic-gate 
57390Sstevel@tonic-gate /*
57400Sstevel@tonic-gate  * Unconfigure child by name
57410Sstevel@tonic-gate  */
57420Sstevel@tonic-gate static int
57430Sstevel@tonic-gate devi_unconfig_one(dev_info_t *pdip, char *devnm, int flags)
57440Sstevel@tonic-gate {
57452155Scth 	int		rv, circ;
57462155Scth 	dev_info_t	*child;
57472155Scth 	dev_info_t	*vdip = NULL;
57482155Scth 	int		v_circ;
57490Sstevel@tonic-gate 
57500Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
57510Sstevel@tonic-gate 	child = ndi_devi_findchild(pdip, devnm);
57522155Scth 
57532155Scth 	/*
57542155Scth 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
57552155Scth 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
57562155Scth 	 * management operations.
57572155Scth 	 */
57582155Scth 	if (child && MDI_PHCI(child)) {
57592155Scth 		vdip = mdi_devi_get_vdip(child);
57602155Scth 		if (vdip && (ddi_get_parent(vdip) != pdip)) {
57612155Scth 			ndi_devi_exit(pdip, circ);
57622155Scth 
57632155Scth 			/* use mdi_devi_enter ordering */
57642155Scth 			ndi_devi_enter(vdip, &v_circ);
57652155Scth 			ndi_devi_enter(pdip, &circ);
57662155Scth 			child = ndi_devi_findchild(pdip, devnm);
57672155Scth 		} else
57682155Scth 			vdip = NULL;
57692155Scth 	}
57702155Scth 
57712155Scth 	if (child) {
57722155Scth 		rv = devi_detach_node(child, flags);
57732155Scth 	} else {
57740Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
57750Sstevel@tonic-gate 		    "devi_unconfig_one: %s not found\n", devnm));
57762155Scth 		rv = NDI_SUCCESS;
57772155Scth 	}
57782155Scth 
57790Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
57802155Scth 	if (vdip)
57817235Svikram 		ndi_devi_exit(vdip, v_circ);
57822155Scth 
57830Sstevel@tonic-gate 	return (rv);
57840Sstevel@tonic-gate }
57850Sstevel@tonic-gate 
57860Sstevel@tonic-gate int
57870Sstevel@tonic-gate ndi_devi_unconfig_one(
57880Sstevel@tonic-gate 	dev_info_t *pdip,
57890Sstevel@tonic-gate 	char *devnm,
57900Sstevel@tonic-gate 	dev_info_t **dipp,
57910Sstevel@tonic-gate 	int flags)
57920Sstevel@tonic-gate {
57932155Scth 	int		(*f)();
57942155Scth 	int		circ, rv;
57952155Scth 	int		pm_cookie;
57962155Scth 	dev_info_t	*child;
57972155Scth 	dev_info_t	*vdip = NULL;
57982155Scth 	int		v_circ;
57990Sstevel@tonic-gate 	struct brevq_node *brevq = NULL;
58000Sstevel@tonic-gate 
58011333Scth 	ASSERT(i_ddi_devi_attached(pdip));
58020Sstevel@tonic-gate 
58030Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
58040Sstevel@tonic-gate 	    "ndi_devi_unconfig_one: par = %s%d (%p), child = %s\n",
58050Sstevel@tonic-gate 	    ddi_driver_name(pdip), ddi_get_instance(pdip),
58060Sstevel@tonic-gate 	    (void *)pdip, devnm));
58070Sstevel@tonic-gate 
58080Sstevel@tonic-gate 	if (pm_pre_unconfig(pdip, flags, &pm_cookie, devnm) != DDI_SUCCESS)
58090Sstevel@tonic-gate 		return (NDI_FAILURE);
58100Sstevel@tonic-gate 
58110Sstevel@tonic-gate 	if (dipp)
58120Sstevel@tonic-gate 		*dipp = NULL;
58130Sstevel@tonic-gate 
58140Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
58150Sstevel@tonic-gate 	child = ndi_devi_findchild(pdip, devnm);
58162155Scth 
58172155Scth 	/*
58182155Scth 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
58192155Scth 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
58202155Scth 	 * management operations.
58212155Scth 	 */
58222155Scth 	if (child && MDI_PHCI(child)) {
58232155Scth 		vdip = mdi_devi_get_vdip(child);
58242155Scth 		if (vdip && (ddi_get_parent(vdip) != pdip)) {
58252155Scth 			ndi_devi_exit(pdip, circ);
58262155Scth 
58272155Scth 			/* use mdi_devi_enter ordering */
58282155Scth 			ndi_devi_enter(vdip, &v_circ);
58292155Scth 			ndi_devi_enter(pdip, &circ);
58302155Scth 			child = ndi_devi_findchild(pdip, devnm);
58312155Scth 		} else
58322155Scth 			vdip = NULL;
58332155Scth 	}
58342155Scth 
58350Sstevel@tonic-gate 	if (child == NULL) {
58360Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_unconfig_one: %s"
58370Sstevel@tonic-gate 		    " not found\n", devnm));
58382155Scth 		rv = NDI_SUCCESS;
58392155Scth 		goto out;
58400Sstevel@tonic-gate 	}
58410Sstevel@tonic-gate 
58420Sstevel@tonic-gate 	/*
58430Sstevel@tonic-gate 	 * Unconfigure children/descendants of named child
58440Sstevel@tonic-gate 	 */
58450Sstevel@tonic-gate 	rv = devi_unconfig_branch(child, dipp, flags | NDI_UNCONFIG, &brevq);
58460Sstevel@tonic-gate 	if (rv != NDI_SUCCESS)
58470Sstevel@tonic-gate 		goto out;
58480Sstevel@tonic-gate 
58490Sstevel@tonic-gate 	init_bound_node_ev(pdip, child, flags);
58500Sstevel@tonic-gate 
58510Sstevel@tonic-gate 	if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
58520Sstevel@tonic-gate 	    (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
58530Sstevel@tonic-gate 	    (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
58540Sstevel@tonic-gate 		rv = devi_detach_node(child, flags);
58550Sstevel@tonic-gate 	} else {
58560Sstevel@tonic-gate 		/* call bus_config entry point */
58570Sstevel@tonic-gate 		rv = (*f)(pdip, flags, BUS_UNCONFIG_ONE, (void *)devnm);
58580Sstevel@tonic-gate 	}
58590Sstevel@tonic-gate 
58600Sstevel@tonic-gate 	if (brevq) {
58610Sstevel@tonic-gate 		if (rv != NDI_SUCCESS)
58620Sstevel@tonic-gate 			log_and_free_brevq_dip(child, brevq);
58630Sstevel@tonic-gate 		else
58640Sstevel@tonic-gate 			free_brevq(brevq);
58650Sstevel@tonic-gate 	}
58660Sstevel@tonic-gate 
58670Sstevel@tonic-gate 	if (dipp && rv != NDI_SUCCESS) {
58680Sstevel@tonic-gate 		ndi_hold_devi(child);
58690Sstevel@tonic-gate 		ASSERT(*dipp == NULL);
58700Sstevel@tonic-gate 		*dipp = child;
58710Sstevel@tonic-gate 	}
58720Sstevel@tonic-gate 
58730Sstevel@tonic-gate out:
58740Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
58752155Scth 	if (vdip)
58767235Svikram 		ndi_devi_exit(vdip, v_circ);
58772155Scth 
58780Sstevel@tonic-gate 	pm_post_unconfig(pdip, pm_cookie, devnm);
58790Sstevel@tonic-gate 
58800Sstevel@tonic-gate 	return (rv);
58810Sstevel@tonic-gate }
58820Sstevel@tonic-gate 
58830Sstevel@tonic-gate struct async_arg {
58840Sstevel@tonic-gate 	dev_info_t *dip;
58850Sstevel@tonic-gate 	uint_t flags;
58860Sstevel@tonic-gate };
58870Sstevel@tonic-gate 
58880Sstevel@tonic-gate /*
58890Sstevel@tonic-gate  * Common async handler for:
58900Sstevel@tonic-gate  *	ndi_devi_bind_driver_async
58910Sstevel@tonic-gate  *	ndi_devi_online_async
58920Sstevel@tonic-gate  */
58930Sstevel@tonic-gate static int
58940Sstevel@tonic-gate i_ndi_devi_async_common(dev_info_t *dip, uint_t flags, void (*func)())
58950Sstevel@tonic-gate {
58960Sstevel@tonic-gate 	int tqflag;
58970Sstevel@tonic-gate 	int kmflag;
58980Sstevel@tonic-gate 	struct async_arg *arg;
58990Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
59000Sstevel@tonic-gate 
59010Sstevel@tonic-gate 	ASSERT(pdip);
59020Sstevel@tonic-gate 	ASSERT(DEVI(pdip)->devi_taskq);
59030Sstevel@tonic-gate 	ASSERT(ndi_dev_is_persistent_node(dip));
59040Sstevel@tonic-gate 
59050Sstevel@tonic-gate 	if (flags & NDI_NOSLEEP) {
59060Sstevel@tonic-gate 		kmflag = KM_NOSLEEP;
59070Sstevel@tonic-gate 		tqflag = TQ_NOSLEEP;
59080Sstevel@tonic-gate 	} else {
59090Sstevel@tonic-gate 		kmflag = KM_SLEEP;
59100Sstevel@tonic-gate 		tqflag = TQ_SLEEP;
59110Sstevel@tonic-gate 	}
59120Sstevel@tonic-gate 
59130Sstevel@tonic-gate 	arg = kmem_alloc(sizeof (*arg), kmflag);
59140Sstevel@tonic-gate 	if (arg == NULL)
59150Sstevel@tonic-gate 		goto fail;
59160Sstevel@tonic-gate 
59170Sstevel@tonic-gate 	arg->flags = flags;
59180Sstevel@tonic-gate 	arg->dip = dip;
59190Sstevel@tonic-gate 	if (ddi_taskq_dispatch(DEVI(pdip)->devi_taskq, func, arg, tqflag) ==
59200Sstevel@tonic-gate 	    DDI_SUCCESS) {
59210Sstevel@tonic-gate 		return (NDI_SUCCESS);
59220Sstevel@tonic-gate 	}
59230Sstevel@tonic-gate 
59240Sstevel@tonic-gate fail:
59250Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "%s%d: ddi_taskq_dispatch failed",
59260Sstevel@tonic-gate 	    ddi_driver_name(pdip), ddi_get_instance(pdip)));
59270Sstevel@tonic-gate 
59280Sstevel@tonic-gate 	if (arg)
59290Sstevel@tonic-gate 		kmem_free(arg, sizeof (*arg));
59300Sstevel@tonic-gate 	return (NDI_FAILURE);
59310Sstevel@tonic-gate }
59320Sstevel@tonic-gate 
59330Sstevel@tonic-gate static void
59340Sstevel@tonic-gate i_ndi_devi_bind_driver_cb(struct async_arg *arg)
59350Sstevel@tonic-gate {
59360Sstevel@tonic-gate 	(void) ndi_devi_bind_driver(arg->dip, arg->flags);
59370Sstevel@tonic-gate 	kmem_free(arg, sizeof (*arg));
59380Sstevel@tonic-gate }
59390Sstevel@tonic-gate 
59400Sstevel@tonic-gate int
59410Sstevel@tonic-gate ndi_devi_bind_driver_async(dev_info_t *dip, uint_t flags)
59420Sstevel@tonic-gate {
59430Sstevel@tonic-gate 	return (i_ndi_devi_async_common(dip, flags,
59440Sstevel@tonic-gate 	    (void (*)())i_ndi_devi_bind_driver_cb));
59450Sstevel@tonic-gate }
59460Sstevel@tonic-gate 
59470Sstevel@tonic-gate /*
59480Sstevel@tonic-gate  * place the devinfo in the ONLINE state.
59490Sstevel@tonic-gate  */
59500Sstevel@tonic-gate int
59510Sstevel@tonic-gate ndi_devi_online(dev_info_t *dip, uint_t flags)
59520Sstevel@tonic-gate {
59530Sstevel@tonic-gate 	int circ, rv;
59540Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
59550Sstevel@tonic-gate 	int branch_event = 0;
59560Sstevel@tonic-gate 
59570Sstevel@tonic-gate 	ASSERT(pdip);
59580Sstevel@tonic-gate 
59590Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_online: %s%d (%p)\n",
59604950Scth 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
59610Sstevel@tonic-gate 
59620Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
59630Sstevel@tonic-gate 	/* bind child before merging .conf nodes */
59640Sstevel@tonic-gate 	rv = i_ndi_config_node(dip, DS_BOUND, flags);
59650Sstevel@tonic-gate 	if (rv != NDI_SUCCESS) {
59660Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
59670Sstevel@tonic-gate 		return (rv);
59680Sstevel@tonic-gate 	}
59690Sstevel@tonic-gate 
59700Sstevel@tonic-gate 	/* merge .conf properties */
59710Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(pdip, flags);
59720Sstevel@tonic-gate 
59732009Sdm120769 	flags |= (NDI_DEVI_ONLINE | NDI_CONFIG);
59740Sstevel@tonic-gate 
59750Sstevel@tonic-gate 	if (flags & NDI_NO_EVENT) {
59760Sstevel@tonic-gate 		/*
59770Sstevel@tonic-gate 		 * Caller is specifically asking for not to generate an event.
59780Sstevel@tonic-gate 		 * Set the following flag so that devi_attach_node() don't
59790Sstevel@tonic-gate 		 * change the event state.
59800Sstevel@tonic-gate 		 */
59810Sstevel@tonic-gate 		flags |= NDI_NO_EVENT_STATE_CHNG;
59820Sstevel@tonic-gate 	}
59830Sstevel@tonic-gate 
59840Sstevel@tonic-gate 	if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
59850Sstevel@tonic-gate 	    ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip))) {
59860Sstevel@tonic-gate 		flags |= NDI_BRANCH_EVENT_OP;
59870Sstevel@tonic-gate 		branch_event = 1;
59880Sstevel@tonic-gate 	}
59890Sstevel@tonic-gate 
59900Sstevel@tonic-gate 	/*
59910Sstevel@tonic-gate 	 * devi_attach_node() may remove dip on failure
59920Sstevel@tonic-gate 	 */
59930Sstevel@tonic-gate 	if ((rv = devi_attach_node(dip, flags)) == NDI_SUCCESS) {
59940Sstevel@tonic-gate 		if ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip)) {
59950Sstevel@tonic-gate 			(void) ndi_devi_config(dip, flags);
59960Sstevel@tonic-gate 		}
59970Sstevel@tonic-gate 
59980Sstevel@tonic-gate 		if (branch_event)
59990Sstevel@tonic-gate 			(void) i_log_devfs_branch_add(dip);
60000Sstevel@tonic-gate 	}
60010Sstevel@tonic-gate 
60020Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
60030Sstevel@tonic-gate 
60040Sstevel@tonic-gate 	/*
60050Sstevel@tonic-gate 	 * Notify devfs that we have a new node. Devfs needs to invalidate
60060Sstevel@tonic-gate 	 * cached directory contents.
60070Sstevel@tonic-gate 	 *
60080Sstevel@tonic-gate 	 * For PCMCIA devices, it is possible the pdip is not fully
60090Sstevel@tonic-gate 	 * attached. In this case, calling back into devfs will
60100Sstevel@tonic-gate 	 * result in a loop or assertion error. Hence, the check
60110Sstevel@tonic-gate 	 * on node state.
60120Sstevel@tonic-gate 	 *
60130Sstevel@tonic-gate 	 * If we own parent lock, this is part of a branch operation.
60140Sstevel@tonic-gate 	 * We skip the devfs_clean() step because the cache invalidation
60150Sstevel@tonic-gate 	 * is done higher up in the device tree.
60160Sstevel@tonic-gate 	 */
60171333Scth 	if (rv == NDI_SUCCESS && i_ddi_devi_attached(pdip) &&
60180Sstevel@tonic-gate 	    !DEVI_BUSY_OWNED(pdip))
60190Sstevel@tonic-gate 		(void) devfs_clean(pdip, NULL, 0);
60200Sstevel@tonic-gate 	return (rv);
60210Sstevel@tonic-gate }
60220Sstevel@tonic-gate 
60230Sstevel@tonic-gate static void
60240Sstevel@tonic-gate i_ndi_devi_online_cb(struct async_arg *arg)
60250Sstevel@tonic-gate {
60260Sstevel@tonic-gate 	(void) ndi_devi_online(arg->dip, arg->flags);
60270Sstevel@tonic-gate 	kmem_free(arg, sizeof (*arg));
60280Sstevel@tonic-gate }
60290Sstevel@tonic-gate 
60300Sstevel@tonic-gate int
60310Sstevel@tonic-gate ndi_devi_online_async(dev_info_t *dip, uint_t flags)
60320Sstevel@tonic-gate {
60330Sstevel@tonic-gate 	/* mark child as need config if requested. */
6034495Scth 	if (flags & NDI_CONFIG) {
6035495Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
60360Sstevel@tonic-gate 		DEVI_SET_NDI_CONFIG(dip);
6037495Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
6038495Scth 	}
60390Sstevel@tonic-gate 
60400Sstevel@tonic-gate 	return (i_ndi_devi_async_common(dip, flags,
60410Sstevel@tonic-gate 	    (void (*)())i_ndi_devi_online_cb));
60420Sstevel@tonic-gate }
60430Sstevel@tonic-gate 
60440Sstevel@tonic-gate /*
60450Sstevel@tonic-gate  * Take a device node Offline
60460Sstevel@tonic-gate  * To take a device Offline means to detach the device instance from
60470Sstevel@tonic-gate  * the driver and prevent devfs requests from re-attaching the device
60480Sstevel@tonic-gate  * instance.
60490Sstevel@tonic-gate  *
60500Sstevel@tonic-gate  * The flag NDI_DEVI_REMOVE causes removes the device node from
60510Sstevel@tonic-gate  * the driver list and the device tree. In this case, the device
60520Sstevel@tonic-gate  * is assumed to be removed from the system.
60530Sstevel@tonic-gate  */
60540Sstevel@tonic-gate int
60550Sstevel@tonic-gate ndi_devi_offline(dev_info_t *dip, uint_t flags)
60560Sstevel@tonic-gate {
60572155Scth 	int		circ, rval = 0;
60582155Scth 	dev_info_t	*pdip = ddi_get_parent(dip);
60592155Scth 	dev_info_t	*vdip = NULL;
60602155Scth 	int		v_circ;
60610Sstevel@tonic-gate 	struct brevq_node *brevq = NULL;
60620Sstevel@tonic-gate 
60630Sstevel@tonic-gate 	ASSERT(pdip);
60640Sstevel@tonic-gate 
60650Sstevel@tonic-gate 	flags |= NDI_DEVI_OFFLINE;
60662155Scth 
60672155Scth 	/*
60682155Scth 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
60692155Scth 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
60702155Scth 	 * management operations.
60712155Scth 	 */
60722155Scth 	if (MDI_PHCI(dip)) {
60732155Scth 		vdip = mdi_devi_get_vdip(dip);
60742155Scth 		if (vdip && (ddi_get_parent(vdip) != pdip))
60752155Scth 			ndi_devi_enter(vdip, &v_circ);
60762155Scth 		else
60772155Scth 			vdip = NULL;
60782155Scth 	}
60790Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
60802155Scth 
60810Sstevel@tonic-gate 	if (i_ddi_node_state(dip) == DS_READY) {
60820Sstevel@tonic-gate 		/*
60830Sstevel@tonic-gate 		 * If dip is in DS_READY state, there may be cached dv_nodes
60840Sstevel@tonic-gate 		 * referencing this dip, so we invoke devfs code path.
60850Sstevel@tonic-gate 		 * Note that we must release busy changing on pdip to
60860Sstevel@tonic-gate 		 * avoid deadlock against devfs.
60870Sstevel@tonic-gate 		 */
60880Sstevel@tonic-gate 		char *devname = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
60890Sstevel@tonic-gate 		(void) ddi_deviname(dip, devname);
60902155Scth 
60910Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
60922155Scth 		if (vdip)
60932155Scth 			ndi_devi_exit(vdip, v_circ);
60940Sstevel@tonic-gate 
60950Sstevel@tonic-gate 		/*
60960Sstevel@tonic-gate 		 * If we own parent lock, this is part of a branch
60970Sstevel@tonic-gate 		 * operation. We skip the devfs_clean() step.
60980Sstevel@tonic-gate 		 */
60990Sstevel@tonic-gate 		if (!DEVI_BUSY_OWNED(pdip))
61004411Svikram 			(void) devfs_clean(pdip, devname + 1, DV_CLEAN_FORCE);
61010Sstevel@tonic-gate 		kmem_free(devname, MAXNAMELEN + 1);
61020Sstevel@tonic-gate 
61034411Svikram 		rval = devi_unconfig_branch(dip, NULL, flags|NDI_UNCONFIG,
61044411Svikram 		    &brevq);
61054411Svikram 
61060Sstevel@tonic-gate 		if (rval)
61070Sstevel@tonic-gate 			return (NDI_FAILURE);
61080Sstevel@tonic-gate 
61092155Scth 		if (vdip)
61102155Scth 			ndi_devi_enter(vdip, &v_circ);
61110Sstevel@tonic-gate 		ndi_devi_enter(pdip, &circ);
61120Sstevel@tonic-gate 	}
61130Sstevel@tonic-gate 
61140Sstevel@tonic-gate 	init_bound_node_ev(pdip, dip, flags);
61150Sstevel@tonic-gate 
61160Sstevel@tonic-gate 	rval = devi_detach_node(dip, flags);
61170Sstevel@tonic-gate 	if (brevq) {
61180Sstevel@tonic-gate 		if (rval != NDI_SUCCESS)
61190Sstevel@tonic-gate 			log_and_free_brevq_dip(dip, brevq);
61200Sstevel@tonic-gate 		else
61210Sstevel@tonic-gate 			free_brevq(brevq);
61220Sstevel@tonic-gate 	}
61230Sstevel@tonic-gate 
61240Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
61252155Scth 	if (vdip)
61262155Scth 		ndi_devi_exit(vdip, v_circ);
61270Sstevel@tonic-gate 
61280Sstevel@tonic-gate 	return (rval);
61290Sstevel@tonic-gate }
61300Sstevel@tonic-gate 
61310Sstevel@tonic-gate /*
61320Sstevel@tonic-gate  * Find the child dev_info node of parent nexus 'p' whose name
61330Sstevel@tonic-gate  * matches "cname@caddr".  Recommend use of ndi_devi_findchild() instead.
61340Sstevel@tonic-gate  */
61350Sstevel@tonic-gate dev_info_t *
61360Sstevel@tonic-gate ndi_devi_find(dev_info_t *pdip, char *cname, char *caddr)
61370Sstevel@tonic-gate {
61380Sstevel@tonic-gate 	dev_info_t *child;
61390Sstevel@tonic-gate 	int circ;
61400Sstevel@tonic-gate 
61410Sstevel@tonic-gate 	if (pdip == NULL || cname == NULL || caddr == NULL)
61420Sstevel@tonic-gate 		return ((dev_info_t *)NULL);
61430Sstevel@tonic-gate 
61440Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
61454145Scth 	child = find_sibling(ddi_get_child(pdip), cname, caddr,
61464145Scth 	    FIND_NODE_BY_NODENAME, NULL);
61470Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
61480Sstevel@tonic-gate 	return (child);
61490Sstevel@tonic-gate }
61500Sstevel@tonic-gate 
61510Sstevel@tonic-gate /*
61520Sstevel@tonic-gate  * Find the child dev_info node of parent nexus 'p' whose name
61530Sstevel@tonic-gate  * matches devname "name@addr".  Permits caller to hold the parent.
61540Sstevel@tonic-gate  */
61550Sstevel@tonic-gate dev_info_t *
61560Sstevel@tonic-gate ndi_devi_findchild(dev_info_t *pdip, char *devname)
61570Sstevel@tonic-gate {
61580Sstevel@tonic-gate 	dev_info_t *child;
61590Sstevel@tonic-gate 	char	*cname, *caddr;
61600Sstevel@tonic-gate 	char	*devstr;
61610Sstevel@tonic-gate 
61620Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(pdip));
61630Sstevel@tonic-gate 
61640Sstevel@tonic-gate 	devstr = i_ddi_strdup(devname, KM_SLEEP);
61650Sstevel@tonic-gate 	i_ddi_parse_name(devstr, &cname, &caddr, NULL);
61660Sstevel@tonic-gate 
61670Sstevel@tonic-gate 	if (cname == NULL || caddr == NULL) {
61680Sstevel@tonic-gate 		kmem_free(devstr, strlen(devname)+1);
61690Sstevel@tonic-gate 		return ((dev_info_t *)NULL);
61700Sstevel@tonic-gate 	}
61710Sstevel@tonic-gate 
61724145Scth 	child = find_sibling(ddi_get_child(pdip), cname, caddr,
61734145Scth 	    FIND_NODE_BY_NODENAME, NULL);
61740Sstevel@tonic-gate 	kmem_free(devstr, strlen(devname)+1);
61750Sstevel@tonic-gate 	return (child);
61760Sstevel@tonic-gate }
61770Sstevel@tonic-gate 
61780Sstevel@tonic-gate /*
61790Sstevel@tonic-gate  * Misc. routines called by framework only
61800Sstevel@tonic-gate  */
61810Sstevel@tonic-gate 
61820Sstevel@tonic-gate /*
61830Sstevel@tonic-gate  * Clear the DEVI_MADE_CHILDREN/DEVI_ATTACHED_CHILDREN flags
61840Sstevel@tonic-gate  * if new child spec has been added.
61850Sstevel@tonic-gate  */
61860Sstevel@tonic-gate static int
61870Sstevel@tonic-gate reset_nexus_flags(dev_info_t *dip, void *arg)
61880Sstevel@tonic-gate {
6189298Scth 	struct hwc_spec	*list;
6190298Scth 	int		circ;
61910Sstevel@tonic-gate 
61920Sstevel@tonic-gate 	if (((DEVI(dip)->devi_flags & DEVI_MADE_CHILDREN) == 0) ||
61930Sstevel@tonic-gate 	    ((list = hwc_get_child_spec(dip, (major_t)(uintptr_t)arg)) == NULL))
61940Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
61950Sstevel@tonic-gate 
61960Sstevel@tonic-gate 	hwc_free_spec_list(list);
6197298Scth 
6198298Scth 	/* coordinate child state update */
6199298Scth 	ndi_devi_enter(dip, &circ);
62000Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
62010Sstevel@tonic-gate 	DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN | DEVI_ATTACHED_CHILDREN);
62020Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
6203298Scth 	ndi_devi_exit(dip, circ);
62040Sstevel@tonic-gate 
62050Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
62060Sstevel@tonic-gate }
62070Sstevel@tonic-gate 
62080Sstevel@tonic-gate /*
62090Sstevel@tonic-gate  * Helper functions, returns NULL if no memory.
62100Sstevel@tonic-gate  */
62110Sstevel@tonic-gate 
62120Sstevel@tonic-gate /*
62130Sstevel@tonic-gate  * path_to_major:
62140Sstevel@tonic-gate  *
62150Sstevel@tonic-gate  * Return an alternate driver name binding for the leaf device
62160Sstevel@tonic-gate  * of the given pathname, if there is one. The purpose of this
62170Sstevel@tonic-gate  * function is to deal with generic pathnames. The default action
62180Sstevel@tonic-gate  * for platforms that can't do this (ie: x86 or any platform that
62190Sstevel@tonic-gate  * does not have prom_finddevice functionality, which matches
62200Sstevel@tonic-gate  * nodenames and unit-addresses without the drivers participation)
62217009Scth  * is to return DDI_MAJOR_T_NONE.
62220Sstevel@tonic-gate  *
62230Sstevel@tonic-gate  * Used in loadrootmodules() in the swapgeneric module to
62240Sstevel@tonic-gate  * associate a given pathname with a given leaf driver.
62250Sstevel@tonic-gate  *
62260Sstevel@tonic-gate  */
62270Sstevel@tonic-gate major_t
62280Sstevel@tonic-gate path_to_major(char *path)
62290Sstevel@tonic-gate {
62300Sstevel@tonic-gate 	dev_info_t *dip;
62310Sstevel@tonic-gate 	char *p, *q;
6232789Sahrens 	pnode_t nodeid;
62334145Scth 	major_t major;
62344145Scth 
62354145Scth 	/* check for path-oriented alias */
62364145Scth 	major = ddi_name_to_major(path);
62377009Scth 	if ((major != DDI_MAJOR_T_NONE) &&
62384145Scth 	    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED)) {
62394145Scth 		NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s path bound %s\n",
62404145Scth 		    path, ddi_major_to_name(major)));
62414145Scth 		return (major);
62424145Scth 	}
62430Sstevel@tonic-gate 
62440Sstevel@tonic-gate 	/*
62450Sstevel@tonic-gate 	 * Get the nodeid of the given pathname, if such a mapping exists.
62460Sstevel@tonic-gate 	 */
62470Sstevel@tonic-gate 	dip = NULL;
62480Sstevel@tonic-gate 	nodeid = prom_finddevice(path);
62490Sstevel@tonic-gate 	if (nodeid != OBP_BADNODE) {
62500Sstevel@tonic-gate 		/*
62510Sstevel@tonic-gate 		 * Find the nodeid in our copy of the device tree and return
62520Sstevel@tonic-gate 		 * whatever name we used to bind this node to a driver.
62530Sstevel@tonic-gate 		 */
62540Sstevel@tonic-gate 		dip = e_ddi_nodeid_to_dip(nodeid);
62550Sstevel@tonic-gate 	}
62560Sstevel@tonic-gate 
62570Sstevel@tonic-gate 	if (dip == NULL) {
62580Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_WARN,
62590Sstevel@tonic-gate 		    "path_to_major: can't bind <%s>\n", path));
62607009Scth 		return (DDI_MAJOR_T_NONE);
62610Sstevel@tonic-gate 	}
62620Sstevel@tonic-gate 
62630Sstevel@tonic-gate 	/*
62640Sstevel@tonic-gate 	 * If we're bound to something other than the nodename,
62650Sstevel@tonic-gate 	 * note that in the message buffer and system log.
62660Sstevel@tonic-gate 	 */
62670Sstevel@tonic-gate 	p = ddi_binding_name(dip);
62680Sstevel@tonic-gate 	q = ddi_node_name(dip);
62690Sstevel@tonic-gate 	if (p && q && (strcmp(p, q) != 0))
62700Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s bound to %s\n",
62710Sstevel@tonic-gate 		    path, p));
62720Sstevel@tonic-gate 
62734145Scth 	major = ddi_name_to_major(p);
62744145Scth 
62754145Scth 	ndi_rele_devi(dip);		/* release e_ddi_nodeid_to_dip hold */
62764145Scth 
62774145Scth 	return (major);
62780Sstevel@tonic-gate }
62790Sstevel@tonic-gate 
62800Sstevel@tonic-gate /*
62810Sstevel@tonic-gate  * Return the held dip for the specified major and instance, attempting to do
62820Sstevel@tonic-gate  * an attach if specified. Return NULL if the devi can't be found or put in
62830Sstevel@tonic-gate  * the proper state. The caller must release the hold via ddi_release_devi if
62840Sstevel@tonic-gate  * a non-NULL value is returned.
62850Sstevel@tonic-gate  *
62860Sstevel@tonic-gate  * Some callers expect to be able to perform a hold_devi() while in a context
62870Sstevel@tonic-gate  * where using ndi_devi_enter() to ensure the hold might cause deadlock (see
62880Sstevel@tonic-gate  * open-from-attach code in consconfig_dacf.c). Such special-case callers
62890Sstevel@tonic-gate  * must ensure that an ndi_devi_enter(parent)/ndi_devi_hold() from a safe
62900Sstevel@tonic-gate  * context is already active. The hold_devi() implementation must accommodate
62910Sstevel@tonic-gate  * these callers.
62920Sstevel@tonic-gate  */
62930Sstevel@tonic-gate static dev_info_t *
62940Sstevel@tonic-gate hold_devi(major_t major, int instance, int flags)
62950Sstevel@tonic-gate {
62960Sstevel@tonic-gate 	struct devnames	*dnp;
62970Sstevel@tonic-gate 	dev_info_t	*dip;
62980Sstevel@tonic-gate 	char		*path;
62990Sstevel@tonic-gate 
63000Sstevel@tonic-gate 	if ((major >= devcnt) || (instance == -1))
63010Sstevel@tonic-gate 		return (NULL);
63020Sstevel@tonic-gate 
63030Sstevel@tonic-gate 	/* try to find the instance in the per driver list */
63040Sstevel@tonic-gate 	dnp = &(devnamesp[major]);
63050Sstevel@tonic-gate 	LOCK_DEV_OPS(&(dnp->dn_lock));
63060Sstevel@tonic-gate 	for (dip = dnp->dn_head; dip;
63070Sstevel@tonic-gate 	    dip = (dev_info_t *)DEVI(dip)->devi_next) {
63080Sstevel@tonic-gate 		/* skip node if instance field is not valid */
63090Sstevel@tonic-gate 		if (i_ddi_node_state(dip) < DS_INITIALIZED)
63100Sstevel@tonic-gate 			continue;
63110Sstevel@tonic-gate 
63120Sstevel@tonic-gate 		/* look for instance match */
63130Sstevel@tonic-gate 		if (DEVI(dip)->devi_instance == instance) {
63140Sstevel@tonic-gate 			/*
63150Sstevel@tonic-gate 			 * To accommodate callers that can't block in
63160Sstevel@tonic-gate 			 * ndi_devi_enter() we do an ndi_devi_hold(), and
63170Sstevel@tonic-gate 			 * afterwards check that the node is in a state where
63180Sstevel@tonic-gate 			 * the hold prevents detach(). If we did not manage to
63190Sstevel@tonic-gate 			 * prevent detach then we ndi_rele_devi() and perform
63200Sstevel@tonic-gate 			 * the slow path below (which can result in a blocking
63210Sstevel@tonic-gate 			 * ndi_devi_enter() while driving attach top-down).
63220Sstevel@tonic-gate 			 * This code depends on the ordering of
63230Sstevel@tonic-gate 			 * DEVI_SET_DETACHING and the devi_ref check in the
63240Sstevel@tonic-gate 			 * detach_node() code path.
63250Sstevel@tonic-gate 			 */
63260Sstevel@tonic-gate 			ndi_hold_devi(dip);
63271333Scth 			if (i_ddi_devi_attached(dip) &&
63280Sstevel@tonic-gate 			    !DEVI_IS_DETACHING(dip)) {
63290Sstevel@tonic-gate 				UNLOCK_DEV_OPS(&(dnp->dn_lock));
63300Sstevel@tonic-gate 				return (dip);	/* fast-path with devi held */
63310Sstevel@tonic-gate 			}
63320Sstevel@tonic-gate 			ndi_rele_devi(dip);
63330Sstevel@tonic-gate 
63340Sstevel@tonic-gate 			/* try slow-path */
63350Sstevel@tonic-gate 			dip = NULL;
63360Sstevel@tonic-gate 			break;
63370Sstevel@tonic-gate 		}
63380Sstevel@tonic-gate 	}
63390Sstevel@tonic-gate 	ASSERT(dip == NULL);
63400Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
63410Sstevel@tonic-gate 
63420Sstevel@tonic-gate 	if (flags & E_DDI_HOLD_DEVI_NOATTACH)
63430Sstevel@tonic-gate 		return (NULL);		/* told not to drive attach */
63440Sstevel@tonic-gate 
63450Sstevel@tonic-gate 	/* slow-path may block, so it should not occur from interrupt */
63460Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
63470Sstevel@tonic-gate 	if (servicing_interrupt())
63480Sstevel@tonic-gate 		return (NULL);
63490Sstevel@tonic-gate 
63500Sstevel@tonic-gate 	/* reconstruct the path and drive attach by path through devfs. */
63510Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
63520Sstevel@tonic-gate 	if (e_ddi_majorinstance_to_path(major, instance, path) == 0)
63530Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_path(path, flags);
63540Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
63550Sstevel@tonic-gate 	return (dip);			/* with devi held */
63560Sstevel@tonic-gate }
63570Sstevel@tonic-gate 
63580Sstevel@tonic-gate /*
63590Sstevel@tonic-gate  * The {e_}ddi_hold_devi{_by_{instance|dev|path}} hold the devinfo node
63600Sstevel@tonic-gate  * associated with the specified arguments.  This hold should be released
63610Sstevel@tonic-gate  * by calling ddi_release_devi.
63620Sstevel@tonic-gate  *
63630Sstevel@tonic-gate  * The E_DDI_HOLD_DEVI_NOATTACH flag argument allows the caller to to specify
63640Sstevel@tonic-gate  * a failure return if the node is not already attached.
63650Sstevel@tonic-gate  *
63660Sstevel@tonic-gate  * NOTE: by the time we make e_ddi_hold_devi public, we should be able to reuse
63670Sstevel@tonic-gate  * ddi_hold_devi again.
63680Sstevel@tonic-gate  */
63690Sstevel@tonic-gate dev_info_t *
63700Sstevel@tonic-gate ddi_hold_devi_by_instance(major_t major, int instance, int flags)
63710Sstevel@tonic-gate {
63720Sstevel@tonic-gate 	return (hold_devi(major, instance, flags));
63730Sstevel@tonic-gate }
63740Sstevel@tonic-gate 
63750Sstevel@tonic-gate dev_info_t *
63760Sstevel@tonic-gate e_ddi_hold_devi_by_dev(dev_t dev, int flags)
63770Sstevel@tonic-gate {
63780Sstevel@tonic-gate 	major_t	major = getmajor(dev);
63790Sstevel@tonic-gate 	dev_info_t	*dip;
63800Sstevel@tonic-gate 	struct dev_ops	*ops;
63810Sstevel@tonic-gate 	dev_info_t	*ddip = NULL;
63820Sstevel@tonic-gate 
63830Sstevel@tonic-gate 	dip = hold_devi(major, dev_to_instance(dev), flags);
63840Sstevel@tonic-gate 
63850Sstevel@tonic-gate 	/*
63860Sstevel@tonic-gate 	 * The rest of this routine is legacy support for drivers that
63870Sstevel@tonic-gate 	 * have broken DDI_INFO_DEVT2INSTANCE implementations but may have
63880Sstevel@tonic-gate 	 * functional DDI_INFO_DEVT2DEVINFO implementations.  This code will
63890Sstevel@tonic-gate 	 * diagnose inconsistency and, for maximum compatibility with legacy
63900Sstevel@tonic-gate 	 * drivers, give preference to the drivers DDI_INFO_DEVT2DEVINFO
63910Sstevel@tonic-gate 	 * implementation over the above derived dip based the driver's
63920Sstevel@tonic-gate 	 * DDI_INFO_DEVT2INSTANCE implementation. This legacy support should
63930Sstevel@tonic-gate 	 * be removed when DDI_INFO_DEVT2DEVINFO is deprecated.
63940Sstevel@tonic-gate 	 *
63950Sstevel@tonic-gate 	 * NOTE: The following code has a race condition. DEVT2DEVINFO
63960Sstevel@tonic-gate 	 *	returns a dip which is not held. By the time we ref ddip,
63970Sstevel@tonic-gate 	 *	it could have been freed. The saving grace is that for
63980Sstevel@tonic-gate 	 *	most drivers, the dip returned from hold_devi() is the
63990Sstevel@tonic-gate 	 *	same one as the one returned by DEVT2DEVINFO, so we are
64000Sstevel@tonic-gate 	 *	safe for drivers with the correct getinfo(9e) impl.
64010Sstevel@tonic-gate 	 */
64020Sstevel@tonic-gate 	if (((ops = ddi_hold_driver(major)) != NULL) &&
64030Sstevel@tonic-gate 	    CB_DRV_INSTALLED(ops) && ops->devo_getinfo)  {
64040Sstevel@tonic-gate 		if ((*ops->devo_getinfo)(NULL, DDI_INFO_DEVT2DEVINFO,
64050Sstevel@tonic-gate 		    (void *)dev, (void **)&ddip) != DDI_SUCCESS)
64060Sstevel@tonic-gate 			ddip = NULL;
64070Sstevel@tonic-gate 	}
64080Sstevel@tonic-gate 
64090Sstevel@tonic-gate 	/* give preference to the driver returned DEVT2DEVINFO dip */
64100Sstevel@tonic-gate 	if (ddip && (dip != ddip)) {
64110Sstevel@tonic-gate #ifdef	DEBUG
64120Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: inconsistent getinfo(9E) implementation",
64130Sstevel@tonic-gate 		    ddi_driver_name(ddip));
64140Sstevel@tonic-gate #endif	/* DEBUG */
64150Sstevel@tonic-gate 		ndi_hold_devi(ddip);
64160Sstevel@tonic-gate 		if (dip)
64170Sstevel@tonic-gate 			ndi_rele_devi(dip);
64180Sstevel@tonic-gate 		dip = ddip;
64190Sstevel@tonic-gate 	}
64200Sstevel@tonic-gate 
64210Sstevel@tonic-gate 	if (ops)
64220Sstevel@tonic-gate 		ddi_rele_driver(major);
64230Sstevel@tonic-gate 
64240Sstevel@tonic-gate 	return (dip);
64250Sstevel@tonic-gate }
64260Sstevel@tonic-gate 
64270Sstevel@tonic-gate /*
64280Sstevel@tonic-gate  * For compatibility only. Do not call this function!
64290Sstevel@tonic-gate  */
64300Sstevel@tonic-gate dev_info_t *
64310Sstevel@tonic-gate e_ddi_get_dev_info(dev_t dev, vtype_t type)
64320Sstevel@tonic-gate {
64330Sstevel@tonic-gate 	dev_info_t *dip = NULL;
64340Sstevel@tonic-gate 	if (getmajor(dev) >= devcnt)
64350Sstevel@tonic-gate 		return (NULL);
64360Sstevel@tonic-gate 
64370Sstevel@tonic-gate 	switch (type) {
64380Sstevel@tonic-gate 	case VCHR:
64390Sstevel@tonic-gate 	case VBLK:
64400Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
64410Sstevel@tonic-gate 	default:
64420Sstevel@tonic-gate 		break;
64430Sstevel@tonic-gate 	}
64440Sstevel@tonic-gate 
64450Sstevel@tonic-gate 	/*
64460Sstevel@tonic-gate 	 * For compatibility reasons, we can only return the dip with
64470Sstevel@tonic-gate 	 * the driver ref count held. This is not a safe thing to do.
64480Sstevel@tonic-gate 	 * For certain broken third-party software, we are willing
64490Sstevel@tonic-gate 	 * to venture into unknown territory.
64500Sstevel@tonic-gate 	 */
64510Sstevel@tonic-gate 	if (dip) {
64520Sstevel@tonic-gate 		(void) ndi_hold_driver(dip);
64530Sstevel@tonic-gate 		ndi_rele_devi(dip);
64540Sstevel@tonic-gate 	}
64550Sstevel@tonic-gate 	return (dip);
64560Sstevel@tonic-gate }
64570Sstevel@tonic-gate 
64580Sstevel@tonic-gate dev_info_t *
64590Sstevel@tonic-gate e_ddi_hold_devi_by_path(char *path, int flags)
64600Sstevel@tonic-gate {
64610Sstevel@tonic-gate 	dev_info_t	*dip;
64620Sstevel@tonic-gate 
64630Sstevel@tonic-gate 	/* can't specify NOATTACH by path */
64640Sstevel@tonic-gate 	ASSERT(!(flags & E_DDI_HOLD_DEVI_NOATTACH));
64650Sstevel@tonic-gate 
64660Sstevel@tonic-gate 	return (resolve_pathname(path, &dip, NULL, NULL) ? NULL : dip);
64670Sstevel@tonic-gate }
64680Sstevel@tonic-gate 
64690Sstevel@tonic-gate void
64700Sstevel@tonic-gate e_ddi_hold_devi(dev_info_t *dip)
64710Sstevel@tonic-gate {
64720Sstevel@tonic-gate 	ndi_hold_devi(dip);
64730Sstevel@tonic-gate }
64740Sstevel@tonic-gate 
64750Sstevel@tonic-gate void
64760Sstevel@tonic-gate ddi_release_devi(dev_info_t *dip)
64770Sstevel@tonic-gate {
64780Sstevel@tonic-gate 	ndi_rele_devi(dip);
64790Sstevel@tonic-gate }
64800Sstevel@tonic-gate 
64810Sstevel@tonic-gate /*
64820Sstevel@tonic-gate  * Associate a streams queue with a devinfo node
64830Sstevel@tonic-gate  * NOTE: This function is called by STREAM driver's put procedure.
64840Sstevel@tonic-gate  *	It cannot block.
64850Sstevel@tonic-gate  */
64860Sstevel@tonic-gate void
64870Sstevel@tonic-gate ddi_assoc_queue_with_devi(queue_t *q, dev_info_t *dip)
64880Sstevel@tonic-gate {
64890Sstevel@tonic-gate 	queue_t *rq = _RD(q);
64900Sstevel@tonic-gate 	struct stdata *stp;
64910Sstevel@tonic-gate 	vnode_t *vp;
64920Sstevel@tonic-gate 
64930Sstevel@tonic-gate 	/* set flag indicating that ddi_assoc_queue_with_devi was called */
64940Sstevel@tonic-gate 	mutex_enter(QLOCK(rq));
64950Sstevel@tonic-gate 	rq->q_flag |= _QASSOCIATED;
64960Sstevel@tonic-gate 	mutex_exit(QLOCK(rq));
64970Sstevel@tonic-gate 
64980Sstevel@tonic-gate 	/* get the vnode associated with the queue */
64990Sstevel@tonic-gate 	stp = STREAM(rq);
65000Sstevel@tonic-gate 	vp = stp->sd_vnode;
65010Sstevel@tonic-gate 	ASSERT(vp);
65020Sstevel@tonic-gate 
65030Sstevel@tonic-gate 	/* change the hardware association of the vnode */
65040Sstevel@tonic-gate 	spec_assoc_vp_with_devi(vp, dip);
65050Sstevel@tonic-gate }
65060Sstevel@tonic-gate 
65070Sstevel@tonic-gate /*
65080Sstevel@tonic-gate  * ddi_install_driver(name)
65090Sstevel@tonic-gate  *
65100Sstevel@tonic-gate  * Driver installation is currently a byproduct of driver loading.  This
65110Sstevel@tonic-gate  * may change.
65120Sstevel@tonic-gate  */
65130Sstevel@tonic-gate int
65140Sstevel@tonic-gate ddi_install_driver(char *name)
65150Sstevel@tonic-gate {
65160Sstevel@tonic-gate 	major_t major = ddi_name_to_major(name);
65170Sstevel@tonic-gate 
65187009Scth 	if ((major == DDI_MAJOR_T_NONE) ||
65190Sstevel@tonic-gate 	    (ddi_hold_installed_driver(major) == NULL)) {
65200Sstevel@tonic-gate 		return (DDI_FAILURE);
65210Sstevel@tonic-gate 	}
65220Sstevel@tonic-gate 	ddi_rele_driver(major);
65230Sstevel@tonic-gate 	return (DDI_SUCCESS);
65240Sstevel@tonic-gate }
65250Sstevel@tonic-gate 
65260Sstevel@tonic-gate struct dev_ops *
65270Sstevel@tonic-gate ddi_hold_driver(major_t major)
65280Sstevel@tonic-gate {
65290Sstevel@tonic-gate 	return (mod_hold_dev_by_major(major));
65300Sstevel@tonic-gate }
65310Sstevel@tonic-gate 
65320Sstevel@tonic-gate 
65330Sstevel@tonic-gate void
65340Sstevel@tonic-gate ddi_rele_driver(major_t major)
65350Sstevel@tonic-gate {
65360Sstevel@tonic-gate 	mod_rele_dev_by_major(major);
65370Sstevel@tonic-gate }
65380Sstevel@tonic-gate 
65390Sstevel@tonic-gate 
65400Sstevel@tonic-gate /*
65410Sstevel@tonic-gate  * This is called during boot to force attachment order of special dips
65420Sstevel@tonic-gate  * dip must be referenced via ndi_hold_devi()
65430Sstevel@tonic-gate  */
65440Sstevel@tonic-gate int
65450Sstevel@tonic-gate i_ddi_attach_node_hierarchy(dev_info_t *dip)
65460Sstevel@tonic-gate {
65472155Scth 	dev_info_t	*parent;
65482155Scth 	int		ret, circ;
65492155Scth 
65502155Scth 	/*
65512155Scth 	 * Recurse up until attached parent is found.
65522155Scth 	 */
65532009Sdm120769 	if (i_ddi_devi_attached(dip))
65542009Sdm120769 		return (DDI_SUCCESS);
65550Sstevel@tonic-gate 	parent = ddi_get_parent(dip);
65560Sstevel@tonic-gate 	if (i_ddi_attach_node_hierarchy(parent) != DDI_SUCCESS)
65570Sstevel@tonic-gate 		return (DDI_FAILURE);
65580Sstevel@tonic-gate 
65590Sstevel@tonic-gate 	/*
65602155Scth 	 * Come top-down, expanding .conf nodes under this parent
65612155Scth 	 * and driving attach.
65620Sstevel@tonic-gate 	 */
65632155Scth 	ndi_devi_enter(parent, &circ);
65640Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(parent, 0);
65652155Scth 	ret = i_ddi_attachchild(dip);
65662155Scth 	ndi_devi_exit(parent, circ);
65672155Scth 
65682155Scth 	return (ret);
65690Sstevel@tonic-gate }
65700Sstevel@tonic-gate 
65710Sstevel@tonic-gate /* keep this function static */
65720Sstevel@tonic-gate static int
65730Sstevel@tonic-gate attach_driver_nodes(major_t major)
65740Sstevel@tonic-gate {
65750Sstevel@tonic-gate 	struct devnames *dnp;
65760Sstevel@tonic-gate 	dev_info_t *dip;
65770Sstevel@tonic-gate 	int error = DDI_FAILURE;
65780Sstevel@tonic-gate 
65790Sstevel@tonic-gate 	dnp = &devnamesp[major];
65800Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
65810Sstevel@tonic-gate 	dip = dnp->dn_head;
65820Sstevel@tonic-gate 	while (dip) {
65830Sstevel@tonic-gate 		ndi_hold_devi(dip);
65840Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
65850Sstevel@tonic-gate 		if (i_ddi_attach_node_hierarchy(dip) == DDI_SUCCESS)
65860Sstevel@tonic-gate 			error = DDI_SUCCESS;
65870Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
65880Sstevel@tonic-gate 		ndi_rele_devi(dip);
65890Sstevel@tonic-gate 		dip = ddi_get_next(dip);
65900Sstevel@tonic-gate 	}
65910Sstevel@tonic-gate 	if (error == DDI_SUCCESS)
65920Sstevel@tonic-gate 		dnp->dn_flags |= DN_NO_AUTODETACH;
65930Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
65940Sstevel@tonic-gate 
65950Sstevel@tonic-gate 
65960Sstevel@tonic-gate 	return (error);
65970Sstevel@tonic-gate }
65980Sstevel@tonic-gate 
65990Sstevel@tonic-gate /*
66000Sstevel@tonic-gate  * i_ddi_attach_hw_nodes configures and attaches all hw nodes
66010Sstevel@tonic-gate  * bound to a specific driver. This function replaces calls to
66020Sstevel@tonic-gate  * ddi_hold_installed_driver() for drivers with no .conf
66030Sstevel@tonic-gate  * enumerated nodes.
66040Sstevel@tonic-gate  *
66050Sstevel@tonic-gate  * This facility is typically called at boot time to attach
66060Sstevel@tonic-gate  * platform-specific hardware nodes, such as ppm nodes on xcal
66070Sstevel@tonic-gate  * and grover and keyswitch nodes on cherrystone. It does not
66080Sstevel@tonic-gate  * deal with .conf enumerated node. Calling it beyond the boot
66090Sstevel@tonic-gate  * process is strongly discouraged.
66100Sstevel@tonic-gate  */
66110Sstevel@tonic-gate int
66120Sstevel@tonic-gate i_ddi_attach_hw_nodes(char *driver)
66130Sstevel@tonic-gate {
66140Sstevel@tonic-gate 	major_t major;
66150Sstevel@tonic-gate 
66160Sstevel@tonic-gate 	major = ddi_name_to_major(driver);
66177009Scth 	if (major == DDI_MAJOR_T_NONE)
66180Sstevel@tonic-gate 		return (DDI_FAILURE);
66190Sstevel@tonic-gate 
66200Sstevel@tonic-gate 	return (attach_driver_nodes(major));
66210Sstevel@tonic-gate }
66220Sstevel@tonic-gate 
66230Sstevel@tonic-gate /*
66240Sstevel@tonic-gate  * i_ddi_attach_pseudo_node configures pseudo drivers which
66250Sstevel@tonic-gate  * has a single node. The .conf nodes must be enumerated
66260Sstevel@tonic-gate  * before calling this interface. The dip is held attached
66270Sstevel@tonic-gate  * upon returning.
66280Sstevel@tonic-gate  *
66290Sstevel@tonic-gate  * This facility should only be called only at boot time
66300Sstevel@tonic-gate  * by the I/O framework.
66310Sstevel@tonic-gate  */
66320Sstevel@tonic-gate dev_info_t *
66330Sstevel@tonic-gate i_ddi_attach_pseudo_node(char *driver)
66340Sstevel@tonic-gate {
66350Sstevel@tonic-gate 	major_t major;
66360Sstevel@tonic-gate 	dev_info_t *dip;
66370Sstevel@tonic-gate 
66380Sstevel@tonic-gate 	major = ddi_name_to_major(driver);
66397009Scth 	if (major == DDI_MAJOR_T_NONE)
66400Sstevel@tonic-gate 		return (NULL);
66410Sstevel@tonic-gate 
66420Sstevel@tonic-gate 	if (attach_driver_nodes(major) != DDI_SUCCESS)
66430Sstevel@tonic-gate 		return (NULL);
66440Sstevel@tonic-gate 
66450Sstevel@tonic-gate 	dip = devnamesp[major].dn_head;
66460Sstevel@tonic-gate 	ASSERT(dip && ddi_get_next(dip) == NULL);
66470Sstevel@tonic-gate 	ndi_hold_devi(dip);
66480Sstevel@tonic-gate 	return (dip);
66490Sstevel@tonic-gate }
66500Sstevel@tonic-gate 
66510Sstevel@tonic-gate static void
66520Sstevel@tonic-gate diplist_to_parent_major(dev_info_t *head, char parents[])
66530Sstevel@tonic-gate {
66540Sstevel@tonic-gate 	major_t major;
66550Sstevel@tonic-gate 	dev_info_t *dip, *pdip;
66560Sstevel@tonic-gate 
66570Sstevel@tonic-gate 	for (dip = head; dip != NULL; dip = ddi_get_next(dip)) {
66580Sstevel@tonic-gate 		pdip = ddi_get_parent(dip);
66590Sstevel@tonic-gate 		ASSERT(pdip);	/* disallow rootnex.conf nodes */
66600Sstevel@tonic-gate 		major = ddi_driver_major(pdip);
66617009Scth 		if ((major != DDI_MAJOR_T_NONE) && parents[major] == 0)
66620Sstevel@tonic-gate 			parents[major] = 1;
66630Sstevel@tonic-gate 	}
66640Sstevel@tonic-gate }
66650Sstevel@tonic-gate 
66660Sstevel@tonic-gate /*
66670Sstevel@tonic-gate  * Call ddi_hold_installed_driver() on each parent major
66680Sstevel@tonic-gate  * and invoke mt_config_driver() to attach child major.
66690Sstevel@tonic-gate  * This is part of the implementation of ddi_hold_installed_driver.
66700Sstevel@tonic-gate  */
66710Sstevel@tonic-gate static int
66720Sstevel@tonic-gate attach_driver_by_parent(major_t child_major, char parents[])
66730Sstevel@tonic-gate {
66740Sstevel@tonic-gate 	major_t par_major;
66750Sstevel@tonic-gate 	struct mt_config_handle *hdl;
66760Sstevel@tonic-gate 	int flags = NDI_DEVI_PERSIST | NDI_NO_EVENT;
66770Sstevel@tonic-gate 
66780Sstevel@tonic-gate 	hdl = mt_config_init(NULL, NULL, flags, child_major, MT_CONFIG_OP,
66790Sstevel@tonic-gate 	    NULL);
66800Sstevel@tonic-gate 	for (par_major = 0; par_major < devcnt; par_major++) {
66810Sstevel@tonic-gate 		/* disallow recursion on the same driver */
66820Sstevel@tonic-gate 		if (parents[par_major] == 0 || par_major == child_major)
66830Sstevel@tonic-gate 			continue;
66840Sstevel@tonic-gate 		if (ddi_hold_installed_driver(par_major) == NULL)
66850Sstevel@tonic-gate 			continue;
66860Sstevel@tonic-gate 		hdl->mtc_parmajor = par_major;
66870Sstevel@tonic-gate 		mt_config_driver(hdl);
66880Sstevel@tonic-gate 		ddi_rele_driver(par_major);
66890Sstevel@tonic-gate 	}
66900Sstevel@tonic-gate 	(void) mt_config_fini(hdl);
66910Sstevel@tonic-gate 
66920Sstevel@tonic-gate 	return (i_ddi_devs_attached(child_major));
66930Sstevel@tonic-gate }
66940Sstevel@tonic-gate 
66950Sstevel@tonic-gate int
66960Sstevel@tonic-gate i_ddi_devs_attached(major_t major)
66970Sstevel@tonic-gate {
66980Sstevel@tonic-gate 	dev_info_t *dip;
66990Sstevel@tonic-gate 	struct devnames *dnp;
67000Sstevel@tonic-gate 	int error = DDI_FAILURE;
67010Sstevel@tonic-gate 
67020Sstevel@tonic-gate 	/* check for attached instances */
67030Sstevel@tonic-gate 	dnp = &devnamesp[major];
67040Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
67050Sstevel@tonic-gate 	for (dip = dnp->dn_head; dip != NULL; dip = ddi_get_next(dip)) {
67061333Scth 		if (i_ddi_devi_attached(dip)) {
67070Sstevel@tonic-gate 			error = DDI_SUCCESS;
67080Sstevel@tonic-gate 			break;
67090Sstevel@tonic-gate 		}
67100Sstevel@tonic-gate 	}
67110Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
67120Sstevel@tonic-gate 
67130Sstevel@tonic-gate 	return (error);
67140Sstevel@tonic-gate }
67150Sstevel@tonic-gate 
67165895Syz147064 int
67175895Syz147064 i_ddi_minor_node_count(dev_info_t *ddip, const char *node_type)
67185895Syz147064 {
67197224Scth 	int			circ;
67207224Scth 	struct ddi_minor_data	*dp;
67217224Scth 	int			count = 0;
67227224Scth 
67237224Scth 	ndi_devi_enter(ddip, &circ);
67247224Scth 	for (dp = DEVI(ddip)->devi_minor; dp != NULL; dp = dp->next) {
67255895Syz147064 		if (strcmp(dp->ddm_node_type, node_type) == 0)
67265895Syz147064 			count++;
67277224Scth 	}
67287224Scth 	ndi_devi_exit(ddip, circ);
67295895Syz147064 	return (count);
67305895Syz147064 }
67315895Syz147064 
67320Sstevel@tonic-gate /*
67330Sstevel@tonic-gate  * ddi_hold_installed_driver configures and attaches all
67340Sstevel@tonic-gate  * instances of the specified driver. To accomplish this
67350Sstevel@tonic-gate  * it configures and attaches all possible parents of
67360Sstevel@tonic-gate  * the driver, enumerated both in h/w nodes and in the
67370Sstevel@tonic-gate  * driver's .conf file.
67380Sstevel@tonic-gate  *
67390Sstevel@tonic-gate  * NOTE: This facility is for compatibility purposes only and will
67400Sstevel@tonic-gate  *	eventually go away. Its usage is strongly discouraged.
67410Sstevel@tonic-gate  */
67420Sstevel@tonic-gate static void
67430Sstevel@tonic-gate enter_driver(struct devnames *dnp)
67440Sstevel@tonic-gate {
67450Sstevel@tonic-gate 	mutex_enter(&dnp->dn_lock);
67460Sstevel@tonic-gate 	ASSERT(dnp->dn_busy_thread != curthread);
67470Sstevel@tonic-gate 	while (dnp->dn_flags & DN_DRIVER_BUSY)
67480Sstevel@tonic-gate 		cv_wait(&dnp->dn_wait, &dnp->dn_lock);
67490Sstevel@tonic-gate 	dnp->dn_flags |= DN_DRIVER_BUSY;
67500Sstevel@tonic-gate 	dnp->dn_busy_thread = curthread;
67510Sstevel@tonic-gate 	mutex_exit(&dnp->dn_lock);
67520Sstevel@tonic-gate }
67530Sstevel@tonic-gate 
67540Sstevel@tonic-gate static void
67550Sstevel@tonic-gate exit_driver(struct devnames *dnp)
67560Sstevel@tonic-gate {
67570Sstevel@tonic-gate 	mutex_enter(&dnp->dn_lock);
67580Sstevel@tonic-gate 	ASSERT(dnp->dn_busy_thread == curthread);
67590Sstevel@tonic-gate 	dnp->dn_flags &= ~DN_DRIVER_BUSY;
67600Sstevel@tonic-gate 	dnp->dn_busy_thread = NULL;
67610Sstevel@tonic-gate 	cv_broadcast(&dnp->dn_wait);
67620Sstevel@tonic-gate 	mutex_exit(&dnp->dn_lock);
67630Sstevel@tonic-gate }
67640Sstevel@tonic-gate 
67650Sstevel@tonic-gate struct dev_ops *
67660Sstevel@tonic-gate ddi_hold_installed_driver(major_t major)
67670Sstevel@tonic-gate {
67680Sstevel@tonic-gate 	struct dev_ops *ops;
67690Sstevel@tonic-gate 	struct devnames *dnp;
67700Sstevel@tonic-gate 	char *parents;
67710Sstevel@tonic-gate 	int error;
67720Sstevel@tonic-gate 
67730Sstevel@tonic-gate 	ops = ddi_hold_driver(major);
67740Sstevel@tonic-gate 	if (ops == NULL)
67750Sstevel@tonic-gate 		return (NULL);
67760Sstevel@tonic-gate 
67770Sstevel@tonic-gate 	/*
67780Sstevel@tonic-gate 	 * Return immediately if all the attach operations associated
67790Sstevel@tonic-gate 	 * with a ddi_hold_installed_driver() call have already been done.
67800Sstevel@tonic-gate 	 */
67810Sstevel@tonic-gate 	dnp = &devnamesp[major];
67820Sstevel@tonic-gate 	enter_driver(dnp);
67830Sstevel@tonic-gate 	if (dnp->dn_flags & DN_DRIVER_HELD) {
67840Sstevel@tonic-gate 		exit_driver(dnp);
67850Sstevel@tonic-gate 		if (i_ddi_devs_attached(major) == DDI_SUCCESS)
67860Sstevel@tonic-gate 			return (ops);
67870Sstevel@tonic-gate 		ddi_rele_driver(major);
67880Sstevel@tonic-gate 		return (NULL);
67890Sstevel@tonic-gate 	}
67900Sstevel@tonic-gate 
67910Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
67920Sstevel@tonic-gate 	dnp->dn_flags |= (DN_DRIVER_HELD | DN_NO_AUTODETACH);
67930Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
67940Sstevel@tonic-gate 
67950Sstevel@tonic-gate 	DCOMPATPRINTF((CE_CONT,
67960Sstevel@tonic-gate 	    "ddi_hold_installed_driver: %s\n", dnp->dn_name));
67970Sstevel@tonic-gate 
67980Sstevel@tonic-gate 	/*
67990Sstevel@tonic-gate 	 * When the driver has no .conf children, it is sufficient
68000Sstevel@tonic-gate 	 * to attach existing nodes in the device tree. Nodes not
68010Sstevel@tonic-gate 	 * enumerated by the OBP are not attached.
68020Sstevel@tonic-gate 	 */
68030Sstevel@tonic-gate 	if (dnp->dn_pl == NULL) {
68040Sstevel@tonic-gate 		if (attach_driver_nodes(major) == DDI_SUCCESS) {
68050Sstevel@tonic-gate 			exit_driver(dnp);
68060Sstevel@tonic-gate 			return (ops);
68070Sstevel@tonic-gate 		}
68080Sstevel@tonic-gate 		exit_driver(dnp);
68090Sstevel@tonic-gate 		ddi_rele_driver(major);
68100Sstevel@tonic-gate 		return (NULL);
68110Sstevel@tonic-gate 	}
68120Sstevel@tonic-gate 
68130Sstevel@tonic-gate 	/*
68140Sstevel@tonic-gate 	 * Driver has .conf nodes. We find all possible parents
68150Sstevel@tonic-gate 	 * and recursively all ddi_hold_installed_driver on the
68160Sstevel@tonic-gate 	 * parent driver; then we invoke ndi_config_driver()
68170Sstevel@tonic-gate 	 * on all possible parent node in parallel to speed up
68180Sstevel@tonic-gate 	 * performance.
68190Sstevel@tonic-gate 	 */
68200Sstevel@tonic-gate 	parents = kmem_zalloc(devcnt * sizeof (char), KM_SLEEP);
68210Sstevel@tonic-gate 
68220Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
68230Sstevel@tonic-gate 	/* find .conf parents */
68240Sstevel@tonic-gate 	(void) impl_parlist_to_major(dnp->dn_pl, parents);
68250Sstevel@tonic-gate 	/* find hw node parents */
68260Sstevel@tonic-gate 	diplist_to_parent_major(dnp->dn_head, parents);
68270Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
68280Sstevel@tonic-gate 
68290Sstevel@tonic-gate 	error = attach_driver_by_parent(major, parents);
68300Sstevel@tonic-gate 	kmem_free(parents, devcnt * sizeof (char));
68310Sstevel@tonic-gate 	if (error == DDI_SUCCESS) {
68320Sstevel@tonic-gate 		exit_driver(dnp);
68330Sstevel@tonic-gate 		return (ops);
68340Sstevel@tonic-gate 	}
68350Sstevel@tonic-gate 
68360Sstevel@tonic-gate 	exit_driver(dnp);
68370Sstevel@tonic-gate 	ddi_rele_driver(major);
68380Sstevel@tonic-gate 	return (NULL);
68390Sstevel@tonic-gate }
68400Sstevel@tonic-gate 
68410Sstevel@tonic-gate /*
68420Sstevel@tonic-gate  * Default bus_config entry point for nexus drivers
68430Sstevel@tonic-gate  */
68440Sstevel@tonic-gate int
68450Sstevel@tonic-gate ndi_busop_bus_config(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
68460Sstevel@tonic-gate     void *arg, dev_info_t **child, clock_t timeout)
68470Sstevel@tonic-gate {
68480Sstevel@tonic-gate 	major_t major;
68490Sstevel@tonic-gate 
68500Sstevel@tonic-gate 	/*
68510Sstevel@tonic-gate 	 * A timeout of 30 minutes or more is probably a mistake
68520Sstevel@tonic-gate 	 * This is intended to catch uses where timeout is in
68530Sstevel@tonic-gate 	 * the wrong units.  timeout must be in units of ticks.
68540Sstevel@tonic-gate 	 */
68550Sstevel@tonic-gate 	ASSERT(timeout < SEC_TO_TICK(1800));
68560Sstevel@tonic-gate 
68577009Scth 	major = DDI_MAJOR_T_NONE;
68580Sstevel@tonic-gate 	switch (op) {
68590Sstevel@tonic-gate 	case BUS_CONFIG_ONE:
68600Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config %s timeout=%ld\n",
68614950Scth 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
68624950Scth 		    (char *)arg, timeout));
68630Sstevel@tonic-gate 		return (devi_config_one(pdip, (char *)arg, child, flags,
68640Sstevel@tonic-gate 		    timeout));
68650Sstevel@tonic-gate 
68660Sstevel@tonic-gate 	case BUS_CONFIG_DRIVER:
68670Sstevel@tonic-gate 		major = (major_t)(uintptr_t)arg;
68680Sstevel@tonic-gate 		/*FALLTHROUGH*/
68690Sstevel@tonic-gate 	case BUS_CONFIG_ALL:
68700Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config timeout=%ld\n",
68714950Scth 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
68724950Scth 		    timeout));
68730Sstevel@tonic-gate 		if (timeout > 0) {
68740Sstevel@tonic-gate 			NDI_DEBUG(flags, (CE_CONT,
68750Sstevel@tonic-gate 			    "%s%d: bus config all timeout=%ld\n",
68760Sstevel@tonic-gate 			    ddi_driver_name(pdip), ddi_get_instance(pdip),
68770Sstevel@tonic-gate 			    timeout));
68780Sstevel@tonic-gate 			delay(timeout);
68790Sstevel@tonic-gate 		}
68800Sstevel@tonic-gate 		return (config_immediate_children(pdip, flags, major));
68810Sstevel@tonic-gate 
68820Sstevel@tonic-gate 	default:
68830Sstevel@tonic-gate 		return (NDI_FAILURE);
68840Sstevel@tonic-gate 	}
68850Sstevel@tonic-gate 	/*NOTREACHED*/
68860Sstevel@tonic-gate }
68870Sstevel@tonic-gate 
68880Sstevel@tonic-gate /*
68890Sstevel@tonic-gate  * Default busop bus_unconfig handler for nexus drivers
68900Sstevel@tonic-gate  */
68910Sstevel@tonic-gate int
68920Sstevel@tonic-gate ndi_busop_bus_unconfig(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
68930Sstevel@tonic-gate     void *arg)
68940Sstevel@tonic-gate {
68950Sstevel@tonic-gate 	major_t major;
68960Sstevel@tonic-gate 
68977009Scth 	major = DDI_MAJOR_T_NONE;
68980Sstevel@tonic-gate 	switch (op) {
68990Sstevel@tonic-gate 	case BUS_UNCONFIG_ONE:
69000Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig %s\n",
69010Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
69020Sstevel@tonic-gate 		    (char *)arg));
69030Sstevel@tonic-gate 		return (devi_unconfig_one(pdip, (char *)arg, flags));
69040Sstevel@tonic-gate 
69050Sstevel@tonic-gate 	case BUS_UNCONFIG_DRIVER:
69060Sstevel@tonic-gate 		major = (major_t)(uintptr_t)arg;
69070Sstevel@tonic-gate 		/*FALLTHROUGH*/
69080Sstevel@tonic-gate 	case BUS_UNCONFIG_ALL:
69090Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig all\n",
69100Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip)));
69110Sstevel@tonic-gate 		return (unconfig_immediate_children(pdip, NULL, flags, major));
69120Sstevel@tonic-gate 
69130Sstevel@tonic-gate 	default:
69140Sstevel@tonic-gate 		return (NDI_FAILURE);
69150Sstevel@tonic-gate 	}
69160Sstevel@tonic-gate 	/*NOTREACHED*/
69170Sstevel@tonic-gate }
69180Sstevel@tonic-gate 
69190Sstevel@tonic-gate /*
69200Sstevel@tonic-gate  * dummy functions to be removed
69210Sstevel@tonic-gate  */
69220Sstevel@tonic-gate void
69230Sstevel@tonic-gate impl_rem_dev_props(dev_info_t *dip)
69240Sstevel@tonic-gate {
69250Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip))
69260Sstevel@tonic-gate 	/* do nothing */
69270Sstevel@tonic-gate }
69280Sstevel@tonic-gate 
69290Sstevel@tonic-gate /*
69300Sstevel@tonic-gate  * Determine if a node is a leaf node. If not sure, return false (0).
69310Sstevel@tonic-gate  */
69320Sstevel@tonic-gate static int
69330Sstevel@tonic-gate is_leaf_node(dev_info_t *dip)
69340Sstevel@tonic-gate {
69350Sstevel@tonic-gate 	major_t major = ddi_driver_major(dip);
69360Sstevel@tonic-gate 
69377009Scth 	if (major == DDI_MAJOR_T_NONE)
69380Sstevel@tonic-gate 		return (0);
69390Sstevel@tonic-gate 
69400Sstevel@tonic-gate 	return (devnamesp[major].dn_flags & DN_LEAF_DRIVER);
69410Sstevel@tonic-gate }
69420Sstevel@tonic-gate 
69430Sstevel@tonic-gate /*
69440Sstevel@tonic-gate  * Multithreaded [un]configuration
69450Sstevel@tonic-gate  */
69460Sstevel@tonic-gate static struct mt_config_handle *
69470Sstevel@tonic-gate mt_config_init(dev_info_t *pdip, dev_info_t **dipp, int flags,
69480Sstevel@tonic-gate     major_t major, int op, struct brevq_node **brevqp)
69490Sstevel@tonic-gate {
69500Sstevel@tonic-gate 	struct mt_config_handle	*hdl = kmem_alloc(sizeof (*hdl), KM_SLEEP);
69510Sstevel@tonic-gate 
69520Sstevel@tonic-gate 	mutex_init(&hdl->mtc_lock, NULL, MUTEX_DEFAULT, NULL);
69530Sstevel@tonic-gate 	cv_init(&hdl->mtc_cv, NULL, CV_DEFAULT, NULL);
69540Sstevel@tonic-gate 	hdl->mtc_pdip = pdip;
69550Sstevel@tonic-gate 	hdl->mtc_fdip = dipp;
69567009Scth 	hdl->mtc_parmajor = DDI_MAJOR_T_NONE;
69570Sstevel@tonic-gate 	hdl->mtc_flags = flags;
69580Sstevel@tonic-gate 	hdl->mtc_major = major;
69590Sstevel@tonic-gate 	hdl->mtc_thr_count = 0;
69600Sstevel@tonic-gate 	hdl->mtc_op = op;
69610Sstevel@tonic-gate 	hdl->mtc_error = 0;
69620Sstevel@tonic-gate 	hdl->mtc_brevqp = brevqp;
69630Sstevel@tonic-gate 
69640Sstevel@tonic-gate #ifdef DEBUG
69650Sstevel@tonic-gate 	gethrestime(&hdl->start_time);
69660Sstevel@tonic-gate 	hdl->total_time = 0;
69670Sstevel@tonic-gate #endif /* DEBUG */
69680Sstevel@tonic-gate 
69690Sstevel@tonic-gate 	return (hdl);
69700Sstevel@tonic-gate }
69710Sstevel@tonic-gate 
69720Sstevel@tonic-gate #ifdef DEBUG
69730Sstevel@tonic-gate static int
69740Sstevel@tonic-gate time_diff_in_msec(timestruc_t start, timestruc_t end)
69750Sstevel@tonic-gate {
69760Sstevel@tonic-gate 	int	nsec, sec;
69770Sstevel@tonic-gate 
69780Sstevel@tonic-gate 	sec = end.tv_sec - start.tv_sec;
69790Sstevel@tonic-gate 	nsec = end.tv_nsec - start.tv_nsec;
69800Sstevel@tonic-gate 	if (nsec < 0) {
69810Sstevel@tonic-gate 		nsec += NANOSEC;
69820Sstevel@tonic-gate 		sec -= 1;
69830Sstevel@tonic-gate 	}
69840Sstevel@tonic-gate 
69850Sstevel@tonic-gate 	return (sec * (NANOSEC >> 20) + (nsec >> 20));
69860Sstevel@tonic-gate }
69870Sstevel@tonic-gate 
69880Sstevel@tonic-gate #endif	/* DEBUG */
69890Sstevel@tonic-gate 
69900Sstevel@tonic-gate static int
69910Sstevel@tonic-gate mt_config_fini(struct mt_config_handle *hdl)
69920Sstevel@tonic-gate {
69930Sstevel@tonic-gate 	int		rv;
69940Sstevel@tonic-gate #ifdef DEBUG
69950Sstevel@tonic-gate 	int		real_time;
69960Sstevel@tonic-gate 	timestruc_t	end_time;
69970Sstevel@tonic-gate #endif /* DEBUG */
69980Sstevel@tonic-gate 
69990Sstevel@tonic-gate 	mutex_enter(&hdl->mtc_lock);
70000Sstevel@tonic-gate 	while (hdl->mtc_thr_count > 0)
70010Sstevel@tonic-gate 		cv_wait(&hdl->mtc_cv, &hdl->mtc_lock);
70020Sstevel@tonic-gate 	rv = hdl->mtc_error;
70030Sstevel@tonic-gate 	mutex_exit(&hdl->mtc_lock);
70040Sstevel@tonic-gate 
70050Sstevel@tonic-gate #ifdef DEBUG
70060Sstevel@tonic-gate 	gethrestime(&end_time);
70070Sstevel@tonic-gate 	real_time = time_diff_in_msec(hdl->start_time, end_time);
70080Sstevel@tonic-gate 	if ((ddidebug & DDI_MTCONFIG) && hdl->mtc_pdip)
70090Sstevel@tonic-gate 		cmn_err(CE_NOTE,
70100Sstevel@tonic-gate 		    "config %s%d: total time %d msec, real time %d msec",
70114950Scth 		    ddi_driver_name(hdl->mtc_pdip),
70124950Scth 		    ddi_get_instance(hdl->mtc_pdip),
70134950Scth 		    hdl->total_time, real_time);
70140Sstevel@tonic-gate #endif /* DEBUG */
70150Sstevel@tonic-gate 
70160Sstevel@tonic-gate 	cv_destroy(&hdl->mtc_cv);
70170Sstevel@tonic-gate 	mutex_destroy(&hdl->mtc_lock);
70180Sstevel@tonic-gate 	kmem_free(hdl, sizeof (*hdl));
70190Sstevel@tonic-gate 
70200Sstevel@tonic-gate 	return (rv);
70210Sstevel@tonic-gate }
70220Sstevel@tonic-gate 
70230Sstevel@tonic-gate struct mt_config_data {
70240Sstevel@tonic-gate 	struct mt_config_handle	*mtc_hdl;
70250Sstevel@tonic-gate 	dev_info_t		*mtc_dip;
70260Sstevel@tonic-gate 	major_t			mtc_major;
70270Sstevel@tonic-gate 	int			mtc_flags;
70280Sstevel@tonic-gate 	struct brevq_node	*mtc_brn;
70290Sstevel@tonic-gate 	struct mt_config_data	*mtc_next;
70300Sstevel@tonic-gate };
70310Sstevel@tonic-gate 
70320Sstevel@tonic-gate static void
70330Sstevel@tonic-gate mt_config_thread(void *arg)
70340Sstevel@tonic-gate {
70350Sstevel@tonic-gate 	struct mt_config_data	*mcd = (struct mt_config_data *)arg;
70360Sstevel@tonic-gate 	struct mt_config_handle	*hdl = mcd->mtc_hdl;
70370Sstevel@tonic-gate 	dev_info_t		*dip = mcd->mtc_dip;
70380Sstevel@tonic-gate 	dev_info_t		*rdip, **dipp;
70390Sstevel@tonic-gate 	major_t			major = mcd->mtc_major;
70400Sstevel@tonic-gate 	int			flags = mcd->mtc_flags;
70410Sstevel@tonic-gate 	int			rv = 0;
70420Sstevel@tonic-gate 
70430Sstevel@tonic-gate #ifdef DEBUG
70440Sstevel@tonic-gate 	timestruc_t start_time, end_time;
70450Sstevel@tonic-gate 	gethrestime(&start_time);
70460Sstevel@tonic-gate #endif /* DEBUG */
70470Sstevel@tonic-gate 
70480Sstevel@tonic-gate 	rdip = NULL;
70490Sstevel@tonic-gate 	dipp = hdl->mtc_fdip ? &rdip : NULL;
70500Sstevel@tonic-gate 
70510Sstevel@tonic-gate 	switch (hdl->mtc_op) {
70520Sstevel@tonic-gate 	case MT_CONFIG_OP:
70530Sstevel@tonic-gate 		rv = devi_config_common(dip, flags, major);
70540Sstevel@tonic-gate 		break;
70550Sstevel@tonic-gate 	case MT_UNCONFIG_OP:
70560Sstevel@tonic-gate 		if (mcd->mtc_brn) {
70570Sstevel@tonic-gate 			struct brevq_node *brevq = NULL;
70580Sstevel@tonic-gate 			rv = devi_unconfig_common(dip, dipp, flags, major,
70590Sstevel@tonic-gate 			    &brevq);
70601317Scth 			mcd->mtc_brn->brn_child = brevq;
70610Sstevel@tonic-gate 		} else
70620Sstevel@tonic-gate 			rv = devi_unconfig_common(dip, dipp, flags, major,
70630Sstevel@tonic-gate 			    NULL);
70640Sstevel@tonic-gate 		break;
70650Sstevel@tonic-gate 	}
70660Sstevel@tonic-gate 
70670Sstevel@tonic-gate 	mutex_enter(&hdl->mtc_lock);
70680Sstevel@tonic-gate #ifdef DEBUG
70690Sstevel@tonic-gate 	gethrestime(&end_time);
70700Sstevel@tonic-gate 	hdl->total_time += time_diff_in_msec(start_time, end_time);
70710Sstevel@tonic-gate #endif /* DEBUG */
70722155Scth 
70732155Scth 	if ((rv != NDI_SUCCESS) && (hdl->mtc_error == 0)) {
70740Sstevel@tonic-gate 		hdl->mtc_error = rv;
70752155Scth #ifdef	DEBUG
70767009Scth 		if ((ddidebug & DDI_DEBUG) && (major != DDI_MAJOR_T_NONE)) {
70772155Scth 			char	*path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
70782155Scth 
70792155Scth 			(void) ddi_pathname(dip, path);
70802155Scth 			cmn_err(CE_NOTE, "mt_config_thread: "
70812155Scth 			    "op %d.%d.%x at %s failed %d",
70822155Scth 			    hdl->mtc_op, major, flags, path, rv);
70832155Scth 			kmem_free(path, MAXPATHLEN);
70842155Scth 		}
70852155Scth #endif	/* DEBUG */
70862155Scth 	}
70872155Scth 
70880Sstevel@tonic-gate 	if (hdl->mtc_fdip && *hdl->mtc_fdip == NULL) {
70890Sstevel@tonic-gate 		*hdl->mtc_fdip = rdip;
70900Sstevel@tonic-gate 		rdip = NULL;
70910Sstevel@tonic-gate 	}
70920Sstevel@tonic-gate 
70930Sstevel@tonic-gate 	if (rdip) {
70940Sstevel@tonic-gate 		ASSERT(rv != NDI_SUCCESS);
70950Sstevel@tonic-gate 		ndi_rele_devi(rdip);
70960Sstevel@tonic-gate 	}
70970Sstevel@tonic-gate 
70980Sstevel@tonic-gate 	ndi_rele_devi(dip);
70991826Sbm42561 
71001826Sbm42561 	if (--hdl->mtc_thr_count == 0)
71011826Sbm42561 		cv_broadcast(&hdl->mtc_cv);
71021826Sbm42561 	mutex_exit(&hdl->mtc_lock);
71030Sstevel@tonic-gate 	kmem_free(mcd, sizeof (*mcd));
71040Sstevel@tonic-gate }
71050Sstevel@tonic-gate 
71060Sstevel@tonic-gate /*
71070Sstevel@tonic-gate  * Multi-threaded config/unconfig of child nexus
71080Sstevel@tonic-gate  */
71090Sstevel@tonic-gate static void
71100Sstevel@tonic-gate mt_config_children(struct mt_config_handle *hdl)
71110Sstevel@tonic-gate {
71120Sstevel@tonic-gate 	dev_info_t		*pdip = hdl->mtc_pdip;
71130Sstevel@tonic-gate 	major_t			major = hdl->mtc_major;
71140Sstevel@tonic-gate 	dev_info_t		*dip;
71150Sstevel@tonic-gate 	int			circ;
71161317Scth 	struct brevq_node	*brn;
71170Sstevel@tonic-gate 	struct mt_config_data	*mcd_head = NULL;
71180Sstevel@tonic-gate 	struct mt_config_data	*mcd_tail = NULL;
71190Sstevel@tonic-gate 	struct mt_config_data	*mcd;
71200Sstevel@tonic-gate #ifdef DEBUG
71210Sstevel@tonic-gate 	timestruc_t		end_time;
71220Sstevel@tonic-gate 
71230Sstevel@tonic-gate 	/* Update total_time in handle */
71240Sstevel@tonic-gate 	gethrestime(&end_time);
71250Sstevel@tonic-gate 	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
71260Sstevel@tonic-gate #endif
71270Sstevel@tonic-gate 
71280Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
71290Sstevel@tonic-gate 	dip = ddi_get_child(pdip);
71300Sstevel@tonic-gate 	while (dip) {
71310Sstevel@tonic-gate 		if (hdl->mtc_op == MT_UNCONFIG_OP && hdl->mtc_brevqp &&
71320Sstevel@tonic-gate 		    !(DEVI_EVREMOVE(dip)) &&
71330Sstevel@tonic-gate 		    i_ddi_node_state(dip) >= DS_INITIALIZED) {
71340Sstevel@tonic-gate 			/*
71350Sstevel@tonic-gate 			 * Enqueue this dip's deviname.
71360Sstevel@tonic-gate 			 * No need to hold a lock while enqueuing since this
71370Sstevel@tonic-gate 			 * is the only thread doing the enqueue and no one
71380Sstevel@tonic-gate 			 * walks the queue while we are in multithreaded
71390Sstevel@tonic-gate 			 * unconfiguration.
71400Sstevel@tonic-gate 			 */
71410Sstevel@tonic-gate 			brn = brevq_enqueue(hdl->mtc_brevqp, dip, NULL);
71421317Scth 		} else
71431317Scth 			brn = NULL;
71440Sstevel@tonic-gate 
71450Sstevel@tonic-gate 		/*
71460Sstevel@tonic-gate 		 * Hold the child that we are processing so he does not get
71470Sstevel@tonic-gate 		 * removed. The corrisponding ndi_rele_devi() for children
71480Sstevel@tonic-gate 		 * that are not being skipped is done at the end of
71490Sstevel@tonic-gate 		 * mt_config_thread().
71500Sstevel@tonic-gate 		 */
71510Sstevel@tonic-gate 		ndi_hold_devi(dip);
71520Sstevel@tonic-gate 
71530Sstevel@tonic-gate 		/*
71540Sstevel@tonic-gate 		 * skip leaf nodes and (for configure) nodes not
71550Sstevel@tonic-gate 		 * fully attached.
71560Sstevel@tonic-gate 		 */
71570Sstevel@tonic-gate 		if (is_leaf_node(dip) ||
71580Sstevel@tonic-gate 		    (hdl->mtc_op == MT_CONFIG_OP &&
71590Sstevel@tonic-gate 		    i_ddi_node_state(dip) < DS_READY)) {
71600Sstevel@tonic-gate 			ndi_rele_devi(dip);
71610Sstevel@tonic-gate 			dip = ddi_get_next_sibling(dip);
71620Sstevel@tonic-gate 			continue;
71630Sstevel@tonic-gate 		}
71640Sstevel@tonic-gate 
71650Sstevel@tonic-gate 		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
71660Sstevel@tonic-gate 		mcd->mtc_dip = dip;
71670Sstevel@tonic-gate 		mcd->mtc_hdl = hdl;
71680Sstevel@tonic-gate 		mcd->mtc_brn = brn;
71690Sstevel@tonic-gate 
71700Sstevel@tonic-gate 		/*
71710Sstevel@tonic-gate 		 * Switch a 'driver' operation to an 'all' operation below a
71720Sstevel@tonic-gate 		 * node bound to the driver.
71730Sstevel@tonic-gate 		 */
71747009Scth 		if ((major == DDI_MAJOR_T_NONE) ||
71757009Scth 		    (major == ddi_driver_major(dip)))
71767009Scth 			mcd->mtc_major = DDI_MAJOR_T_NONE;
71770Sstevel@tonic-gate 		else
71780Sstevel@tonic-gate 			mcd->mtc_major = major;
71790Sstevel@tonic-gate 
71800Sstevel@tonic-gate 		/*
71810Sstevel@tonic-gate 		 * The unconfig-driver to unconfig-all conversion above
71820Sstevel@tonic-gate 		 * constitutes an autodetach for NDI_DETACH_DRIVER calls,
71830Sstevel@tonic-gate 		 * set NDI_AUTODETACH.
71840Sstevel@tonic-gate 		 */
71850Sstevel@tonic-gate 		mcd->mtc_flags = hdl->mtc_flags;
71860Sstevel@tonic-gate 		if ((mcd->mtc_flags & NDI_DETACH_DRIVER) &&
71870Sstevel@tonic-gate 		    (hdl->mtc_op == MT_UNCONFIG_OP) &&
71880Sstevel@tonic-gate 		    (major == ddi_driver_major(pdip)))
71890Sstevel@tonic-gate 			mcd->mtc_flags |= NDI_AUTODETACH;
71900Sstevel@tonic-gate 
71910Sstevel@tonic-gate 		mutex_enter(&hdl->mtc_lock);
71920Sstevel@tonic-gate 		hdl->mtc_thr_count++;
71930Sstevel@tonic-gate 		mutex_exit(&hdl->mtc_lock);
71940Sstevel@tonic-gate 
71950Sstevel@tonic-gate 		/*
71960Sstevel@tonic-gate 		 * Add to end of list to process after ndi_devi_exit to avoid
71970Sstevel@tonic-gate 		 * locking differences depending on value of mtc_off.
71980Sstevel@tonic-gate 		 */
71990Sstevel@tonic-gate 		mcd->mtc_next = NULL;
72000Sstevel@tonic-gate 		if (mcd_head == NULL)
72010Sstevel@tonic-gate 			mcd_head = mcd;
72020Sstevel@tonic-gate 		else
72030Sstevel@tonic-gate 			mcd_tail->mtc_next = mcd;
72040Sstevel@tonic-gate 		mcd_tail = mcd;
72050Sstevel@tonic-gate 
72060Sstevel@tonic-gate 		dip = ddi_get_next_sibling(dip);
72070Sstevel@tonic-gate 	}
72080Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
72090Sstevel@tonic-gate 
72100Sstevel@tonic-gate 	/* go through the list of held children */
72110Sstevel@tonic-gate 	for (mcd = mcd_head; mcd; mcd = mcd_head) {
72120Sstevel@tonic-gate 		mcd_head = mcd->mtc_next;
72132155Scth 		if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
72140Sstevel@tonic-gate 			mt_config_thread(mcd);
72150Sstevel@tonic-gate 		else
72160Sstevel@tonic-gate 			(void) thread_create(NULL, 0, mt_config_thread, mcd,
72170Sstevel@tonic-gate 			    0, &p0, TS_RUN, minclsyspri);
72180Sstevel@tonic-gate 	}
72190Sstevel@tonic-gate }
72200Sstevel@tonic-gate 
72210Sstevel@tonic-gate static void
72220Sstevel@tonic-gate mt_config_driver(struct mt_config_handle *hdl)
72230Sstevel@tonic-gate {
72240Sstevel@tonic-gate 	major_t			par_major = hdl->mtc_parmajor;
72250Sstevel@tonic-gate 	major_t			major = hdl->mtc_major;
72260Sstevel@tonic-gate 	struct devnames		*dnp = &devnamesp[par_major];
72270Sstevel@tonic-gate 	dev_info_t		*dip;
72280Sstevel@tonic-gate 	struct mt_config_data	*mcd_head = NULL;
72290Sstevel@tonic-gate 	struct mt_config_data	*mcd_tail = NULL;
72300Sstevel@tonic-gate 	struct mt_config_data	*mcd;
72310Sstevel@tonic-gate #ifdef DEBUG
72320Sstevel@tonic-gate 	timestruc_t		end_time;
72330Sstevel@tonic-gate 
72340Sstevel@tonic-gate 	/* Update total_time in handle */
72350Sstevel@tonic-gate 	gethrestime(&end_time);
72360Sstevel@tonic-gate 	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
72370Sstevel@tonic-gate #endif
72387009Scth 	ASSERT(par_major != DDI_MAJOR_T_NONE);
72397009Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
72400Sstevel@tonic-gate 
72410Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
72420Sstevel@tonic-gate 	dip = devnamesp[par_major].dn_head;
72430Sstevel@tonic-gate 	while (dip) {
72440Sstevel@tonic-gate 		/*
72450Sstevel@tonic-gate 		 * Hold the child that we are processing so he does not get
72460Sstevel@tonic-gate 		 * removed. The corrisponding ndi_rele_devi() for children
72470Sstevel@tonic-gate 		 * that are not being skipped is done at the end of
72480Sstevel@tonic-gate 		 * mt_config_thread().
72490Sstevel@tonic-gate 		 */
72500Sstevel@tonic-gate 		ndi_hold_devi(dip);
72510Sstevel@tonic-gate 
72520Sstevel@tonic-gate 		/* skip leaf nodes and nodes not fully attached */
72531333Scth 		if (!i_ddi_devi_attached(dip) || is_leaf_node(dip)) {
72540Sstevel@tonic-gate 			ndi_rele_devi(dip);
72550Sstevel@tonic-gate 			dip = ddi_get_next(dip);
72560Sstevel@tonic-gate 			continue;
72570Sstevel@tonic-gate 		}
72580Sstevel@tonic-gate 
72590Sstevel@tonic-gate 		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
72600Sstevel@tonic-gate 		mcd->mtc_dip = dip;
72610Sstevel@tonic-gate 		mcd->mtc_hdl = hdl;
72620Sstevel@tonic-gate 		mcd->mtc_major = major;
72630Sstevel@tonic-gate 		mcd->mtc_flags = hdl->mtc_flags;
72640Sstevel@tonic-gate 
72650Sstevel@tonic-gate 		mutex_enter(&hdl->mtc_lock);
72660Sstevel@tonic-gate 		hdl->mtc_thr_count++;
72670Sstevel@tonic-gate 		mutex_exit(&hdl->mtc_lock);
72680Sstevel@tonic-gate 
72690Sstevel@tonic-gate 		/*
72700Sstevel@tonic-gate 		 * Add to end of list to process after UNLOCK_DEV_OPS to avoid
72710Sstevel@tonic-gate 		 * locking differences depending on value of mtc_off.
72720Sstevel@tonic-gate 		 */
72730Sstevel@tonic-gate 		mcd->mtc_next = NULL;
72740Sstevel@tonic-gate 		if (mcd_head == NULL)
72750Sstevel@tonic-gate 			mcd_head = mcd;
72760Sstevel@tonic-gate 		else
72770Sstevel@tonic-gate 			mcd_tail->mtc_next = mcd;
72780Sstevel@tonic-gate 		mcd_tail = mcd;
72790Sstevel@tonic-gate 
72800Sstevel@tonic-gate 		dip = ddi_get_next(dip);
72810Sstevel@tonic-gate 	}
72820Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
72830Sstevel@tonic-gate 
72840Sstevel@tonic-gate 	/* go through the list of held children */
72850Sstevel@tonic-gate 	for (mcd = mcd_head; mcd; mcd = mcd_head) {
72860Sstevel@tonic-gate 		mcd_head = mcd->mtc_next;
72872155Scth 		if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
72880Sstevel@tonic-gate 			mt_config_thread(mcd);
72890Sstevel@tonic-gate 		else
72900Sstevel@tonic-gate 			(void) thread_create(NULL, 0, mt_config_thread, mcd,
72910Sstevel@tonic-gate 			    0, &p0, TS_RUN, minclsyspri);
72920Sstevel@tonic-gate 	}
72930Sstevel@tonic-gate }
72940Sstevel@tonic-gate 
72950Sstevel@tonic-gate /*
72960Sstevel@tonic-gate  * Given the nodeid for a persistent (PROM or SID) node, return
72970Sstevel@tonic-gate  * the corresponding devinfo node
72980Sstevel@tonic-gate  * NOTE: This function will return NULL for .conf nodeids.
72990Sstevel@tonic-gate  */
73000Sstevel@tonic-gate dev_info_t *
7301789Sahrens e_ddi_nodeid_to_dip(pnode_t nodeid)
73020Sstevel@tonic-gate {
73030Sstevel@tonic-gate 	dev_info_t		*dip = NULL;
73040Sstevel@tonic-gate 	struct devi_nodeid	*prev, *elem;
73050Sstevel@tonic-gate 
73060Sstevel@tonic-gate 	mutex_enter(&devimap->dno_lock);
73070Sstevel@tonic-gate 
73080Sstevel@tonic-gate 	prev = NULL;
73090Sstevel@tonic-gate 	for (elem = devimap->dno_head; elem; elem = elem->next) {
73100Sstevel@tonic-gate 		if (elem->nodeid == nodeid) {
73110Sstevel@tonic-gate 			ndi_hold_devi(elem->dip);
73120Sstevel@tonic-gate 			dip = elem->dip;
73130Sstevel@tonic-gate 			break;
73140Sstevel@tonic-gate 		}
73150Sstevel@tonic-gate 		prev = elem;
73160Sstevel@tonic-gate 	}
73170Sstevel@tonic-gate 
73180Sstevel@tonic-gate 	/*
73190Sstevel@tonic-gate 	 * Move to head for faster lookup next time
73200Sstevel@tonic-gate 	 */
73210Sstevel@tonic-gate 	if (elem && prev) {
73220Sstevel@tonic-gate 		prev->next = elem->next;
73230Sstevel@tonic-gate 		elem->next = devimap->dno_head;
73240Sstevel@tonic-gate 		devimap->dno_head = elem;
73250Sstevel@tonic-gate 	}
73260Sstevel@tonic-gate 
73270Sstevel@tonic-gate 	mutex_exit(&devimap->dno_lock);
73280Sstevel@tonic-gate 	return (dip);
73290Sstevel@tonic-gate }
73300Sstevel@tonic-gate 
73310Sstevel@tonic-gate static void
73320Sstevel@tonic-gate free_cache_task(void *arg)
73330Sstevel@tonic-gate {
73340Sstevel@tonic-gate 	ASSERT(arg == NULL);
73350Sstevel@tonic-gate 
73360Sstevel@tonic-gate 	mutex_enter(&di_cache.cache_lock);
73370Sstevel@tonic-gate 
73380Sstevel@tonic-gate 	/*
73390Sstevel@tonic-gate 	 * The cache can be invalidated without holding the lock
73400Sstevel@tonic-gate 	 * but it can be made valid again only while the lock is held.
73410Sstevel@tonic-gate 	 * So if the cache is invalid when the lock is held, it will
73420Sstevel@tonic-gate 	 * stay invalid until lock is released.
73430Sstevel@tonic-gate 	 */
73440Sstevel@tonic-gate 	if (!di_cache.cache_valid)
73450Sstevel@tonic-gate 		i_ddi_di_cache_free(&di_cache);
73460Sstevel@tonic-gate 
73470Sstevel@tonic-gate 	mutex_exit(&di_cache.cache_lock);
73480Sstevel@tonic-gate 
73490Sstevel@tonic-gate 	if (di_cache_debug)
73500Sstevel@tonic-gate 		cmn_err(CE_NOTE, "system_taskq: di_cache freed");
73510Sstevel@tonic-gate }
73520Sstevel@tonic-gate 
73530Sstevel@tonic-gate extern int modrootloaded;
73540Sstevel@tonic-gate 
73550Sstevel@tonic-gate void
73560Sstevel@tonic-gate i_ddi_di_cache_free(struct di_cache *cache)
73570Sstevel@tonic-gate {
73580Sstevel@tonic-gate 	int	error;
73590Sstevel@tonic-gate 
73600Sstevel@tonic-gate 	ASSERT(mutex_owned(&cache->cache_lock));
73610Sstevel@tonic-gate 
73620Sstevel@tonic-gate 	if (cache->cache_size) {
73630Sstevel@tonic-gate 		ASSERT(cache->cache_size > 0);
73640Sstevel@tonic-gate 		ASSERT(cache->cache_data);
73650Sstevel@tonic-gate 
73660Sstevel@tonic-gate 		kmem_free(cache->cache_data, cache->cache_size);
73670Sstevel@tonic-gate 		cache->cache_data = NULL;
73680Sstevel@tonic-gate 		cache->cache_size = 0;
73690Sstevel@tonic-gate 
73700Sstevel@tonic-gate 		if (di_cache_debug)
73710Sstevel@tonic-gate 			cmn_err(CE_NOTE, "i_ddi_di_cache_free: freed cachemem");
73720Sstevel@tonic-gate 	} else {
73730Sstevel@tonic-gate 		ASSERT(cache->cache_data == NULL);
73740Sstevel@tonic-gate 		if (di_cache_debug)
73750Sstevel@tonic-gate 			cmn_err(CE_NOTE, "i_ddi_di_cache_free: NULL cache");
73760Sstevel@tonic-gate 	}
73770Sstevel@tonic-gate 
73787576SJerry.Gilliam@Sun.COM 	if (!modrootloaded || rootvp == NULL ||
73797576SJerry.Gilliam@Sun.COM 	    vn_is_readonly(rootvp) || sys_shutdown) {
73800Sstevel@tonic-gate 		if (di_cache_debug) {
73810Sstevel@tonic-gate 			cmn_err(CE_WARN, "/ not mounted/RDONLY. Skip unlink");
73820Sstevel@tonic-gate 		}
73830Sstevel@tonic-gate 		return;
73840Sstevel@tonic-gate 	}
73850Sstevel@tonic-gate 
73860Sstevel@tonic-gate 	error = vn_remove(DI_CACHE_FILE, UIO_SYSSPACE, RMFILE);
73870Sstevel@tonic-gate 	if (di_cache_debug && error && error != ENOENT) {
73880Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: unlink failed: %d", DI_CACHE_FILE, error);
73890Sstevel@tonic-gate 	} else if (di_cache_debug && !error) {
73900Sstevel@tonic-gate 		cmn_err(CE_NOTE, "i_ddi_di_cache_free: unlinked cache file");
73910Sstevel@tonic-gate 	}
73920Sstevel@tonic-gate }
73930Sstevel@tonic-gate 
73940Sstevel@tonic-gate void
73950Sstevel@tonic-gate i_ddi_di_cache_invalidate(int kmflag)
73960Sstevel@tonic-gate {
73976065Scth 	int	cache_valid;
73980Sstevel@tonic-gate 
73990Sstevel@tonic-gate 	if (!modrootloaded || !i_ddi_io_initialized()) {
74000Sstevel@tonic-gate 		if (di_cache_debug)
74010Sstevel@tonic-gate 			cmn_err(CE_NOTE, "I/O not inited. Skipping invalidate");
74020Sstevel@tonic-gate 		return;
74030Sstevel@tonic-gate 	}
74040Sstevel@tonic-gate 
74056065Scth 	/* Increment devtree generation number. */
74062621Sllai1 	atomic_inc_ulong(&devtree_gen);
74070Sstevel@tonic-gate 
74086065Scth 	/* Invalidate the in-core cache and dispatch free on valid->invalid */
74096065Scth 	cache_valid = atomic_swap_uint(&di_cache.cache_valid, 0);
74106065Scth 	if (cache_valid) {
74116065Scth 		(void) taskq_dispatch(system_taskq, free_cache_task, NULL,
74126065Scth 		    (kmflag == KM_SLEEP) ? TQ_SLEEP : TQ_NOSLEEP);
74136065Scth 	}
74140Sstevel@tonic-gate 
74150Sstevel@tonic-gate 	if (di_cache_debug) {
74160Sstevel@tonic-gate 		cmn_err(CE_NOTE, "invalidation with km_flag: %s",
74170Sstevel@tonic-gate 		    kmflag == KM_SLEEP ? "KM_SLEEP" : "KM_NOSLEEP");
74180Sstevel@tonic-gate 	}
74190Sstevel@tonic-gate }
74200Sstevel@tonic-gate 
74210Sstevel@tonic-gate 
74220Sstevel@tonic-gate static void
74230Sstevel@tonic-gate i_bind_vhci_node(dev_info_t *dip)
74240Sstevel@tonic-gate {
74254145Scth 	DEVI(dip)->devi_major = ddi_name_to_major(ddi_node_name(dip));
74260Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_BOUND);
74270Sstevel@tonic-gate }
74280Sstevel@tonic-gate 
74290Sstevel@tonic-gate static char vhci_node_addr[2];
74300Sstevel@tonic-gate 
74310Sstevel@tonic-gate static int
74320Sstevel@tonic-gate i_init_vhci_node(dev_info_t *dip)
74330Sstevel@tonic-gate {
74340Sstevel@tonic-gate 	add_global_props(dip);
74350Sstevel@tonic-gate 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
74360Sstevel@tonic-gate 	if (DEVI(dip)->devi_ops == NULL)
74370Sstevel@tonic-gate 		return (-1);
74380Sstevel@tonic-gate 
74390Sstevel@tonic-gate 	DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
74400Sstevel@tonic-gate 	e_ddi_keep_instance(dip);
74410Sstevel@tonic-gate 	vhci_node_addr[0]	= '\0';
74420Sstevel@tonic-gate 	ddi_set_name_addr(dip, vhci_node_addr);
74430Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_INITIALIZED);
74440Sstevel@tonic-gate 	return (0);
74450Sstevel@tonic-gate }
74460Sstevel@tonic-gate 
74470Sstevel@tonic-gate static void
74480Sstevel@tonic-gate i_link_vhci_node(dev_info_t *dip)
74490Sstevel@tonic-gate {
74501093Shiremath 	ASSERT(MUTEX_HELD(&global_vhci_lock));
74511093Shiremath 
74520Sstevel@tonic-gate 	/*
74530Sstevel@tonic-gate 	 * scsi_vhci should be kept left most of the device tree.
74540Sstevel@tonic-gate 	 */
74550Sstevel@tonic-gate 	if (scsi_vhci_dip) {
74560Sstevel@tonic-gate 		DEVI(dip)->devi_sibling = DEVI(scsi_vhci_dip)->devi_sibling;
74570Sstevel@tonic-gate 		DEVI(scsi_vhci_dip)->devi_sibling = DEVI(dip);
74580Sstevel@tonic-gate 	} else {
74590Sstevel@tonic-gate 		DEVI(dip)->devi_sibling = DEVI(top_devinfo)->devi_child;
74600Sstevel@tonic-gate 		DEVI(top_devinfo)->devi_child = DEVI(dip);
74610Sstevel@tonic-gate 	}
74620Sstevel@tonic-gate }
74630Sstevel@tonic-gate 
74640Sstevel@tonic-gate 
74650Sstevel@tonic-gate /*
74660Sstevel@tonic-gate  * This a special routine to enumerate vhci node (child of rootnex
74670Sstevel@tonic-gate  * node) without holding the ndi_devi_enter() lock. The device node
74680Sstevel@tonic-gate  * is allocated, initialized and brought into DS_READY state before
74690Sstevel@tonic-gate  * inserting into the device tree. The VHCI node is handcrafted
74700Sstevel@tonic-gate  * here to bring the node to DS_READY, similar to rootnex node.
74710Sstevel@tonic-gate  *
74720Sstevel@tonic-gate  * The global_vhci_lock protects linking the node into the device
74730Sstevel@tonic-gate  * as same lock is held before linking/unlinking any direct child
74740Sstevel@tonic-gate  * of rootnex children.
74750Sstevel@tonic-gate  *
74760Sstevel@tonic-gate  * This routine is a workaround to handle a possible deadlock
74770Sstevel@tonic-gate  * that occurs while trying to enumerate node in a different sub-tree
74780Sstevel@tonic-gate  * during _init/_attach entry points.
74790Sstevel@tonic-gate  */
74800Sstevel@tonic-gate /*ARGSUSED*/
74810Sstevel@tonic-gate dev_info_t *
74820Sstevel@tonic-gate ndi_devi_config_vhci(char *drvname, int flags)
74830Sstevel@tonic-gate {
74840Sstevel@tonic-gate 	struct devnames		*dnp;
74850Sstevel@tonic-gate 	dev_info_t		*dip;
74860Sstevel@tonic-gate 	major_t			major = ddi_name_to_major(drvname);
74870Sstevel@tonic-gate 
74880Sstevel@tonic-gate 	if (major == -1)
74890Sstevel@tonic-gate 		return (NULL);
74900Sstevel@tonic-gate 
74910Sstevel@tonic-gate 	/* Make sure we create the VHCI node only once */
74920Sstevel@tonic-gate 	dnp = &devnamesp[major];
74930Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
74940Sstevel@tonic-gate 	if (dnp->dn_head) {
74950Sstevel@tonic-gate 		dip = dnp->dn_head;
74960Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
74970Sstevel@tonic-gate 		return (dip);
74980Sstevel@tonic-gate 	}
74990Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
75000Sstevel@tonic-gate 
75010Sstevel@tonic-gate 	/* Allocate the VHCI node */
75020Sstevel@tonic-gate 	ndi_devi_alloc_sleep(top_devinfo, drvname, DEVI_SID_NODEID, &dip);
75030Sstevel@tonic-gate 	ndi_hold_devi(dip);
75040Sstevel@tonic-gate 
75050Sstevel@tonic-gate 	/* Mark the node as VHCI */
75060Sstevel@tonic-gate 	DEVI(dip)->devi_node_attributes |= DDI_VHCI_NODE;
75070Sstevel@tonic-gate 
75080Sstevel@tonic-gate 	i_ddi_add_devimap(dip);
75090Sstevel@tonic-gate 	i_bind_vhci_node(dip);
75100Sstevel@tonic-gate 	if (i_init_vhci_node(dip) == -1) {
75110Sstevel@tonic-gate 		ndi_rele_devi(dip);
75120Sstevel@tonic-gate 		(void) ndi_devi_free(dip);
75130Sstevel@tonic-gate 		return (NULL);
75140Sstevel@tonic-gate 	}
75150Sstevel@tonic-gate 
7516495Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
75170Sstevel@tonic-gate 	DEVI_SET_ATTACHING(dip);
7518495Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
7519495Scth 
75200Sstevel@tonic-gate 	if (devi_attach(dip, DDI_ATTACH) != DDI_SUCCESS) {
75210Sstevel@tonic-gate 		cmn_err(CE_CONT, "Could not attach %s driver", drvname);
75220Sstevel@tonic-gate 		e_ddi_free_instance(dip, vhci_node_addr);
75230Sstevel@tonic-gate 		ndi_rele_devi(dip);
75240Sstevel@tonic-gate 		(void) ndi_devi_free(dip);
75250Sstevel@tonic-gate 		return (NULL);
75260Sstevel@tonic-gate 	}
7527495Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
75280Sstevel@tonic-gate 	DEVI_CLR_ATTACHING(dip);
7529495Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
75300Sstevel@tonic-gate 
75311093Shiremath 	mutex_enter(&global_vhci_lock);
75320Sstevel@tonic-gate 	i_link_vhci_node(dip);
75331093Shiremath 	mutex_exit(&global_vhci_lock);
75340Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_READY);
75350Sstevel@tonic-gate 
75360Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
75370Sstevel@tonic-gate 	dnp->dn_flags |= DN_DRIVER_HELD;
75380Sstevel@tonic-gate 	dnp->dn_head = dip;
75390Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
75400Sstevel@tonic-gate 
75410Sstevel@tonic-gate 	i_ndi_devi_report_status_change(dip, NULL);
75420Sstevel@tonic-gate 
75430Sstevel@tonic-gate 	return (dip);
75440Sstevel@tonic-gate }
75450Sstevel@tonic-gate 
75460Sstevel@tonic-gate /*
75470Sstevel@tonic-gate  * ibt_hw_is_present() returns 0 when there is no IB hardware actively
75480Sstevel@tonic-gate  * running.  This is primarily useful for modules like rpcmod which
75490Sstevel@tonic-gate  * needs a quick check to decide whether or not it should try to use
75500Sstevel@tonic-gate  * InfiniBand
75510Sstevel@tonic-gate  */
75520Sstevel@tonic-gate int ib_hw_status = 0;
75530Sstevel@tonic-gate int
75540Sstevel@tonic-gate ibt_hw_is_present()
75550Sstevel@tonic-gate {
75560Sstevel@tonic-gate 	return (ib_hw_status);
75570Sstevel@tonic-gate }
75584845Svikram 
75594845Svikram /*
75604845Svikram  * ASSERT that constraint flag is not set and then set the "retire attempt"
75614845Svikram  * flag.
75624845Svikram  */
75634845Svikram int
75644845Svikram e_ddi_mark_retiring(dev_info_t *dip, void *arg)
75654845Svikram {
75664845Svikram 	char	**cons_array = (char **)arg;
75674845Svikram 	char	*path;
75684845Svikram 	int	constraint;
75694845Svikram 	int	i;
75704845Svikram 
75714845Svikram 	constraint = 0;
75724845Svikram 	if (cons_array) {
75734845Svikram 		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
75744845Svikram 		(void) ddi_pathname(dip, path);
75754845Svikram 		for (i = 0; cons_array[i] != NULL; i++) {
75764845Svikram 			if (strcmp(path, cons_array[i]) == 0) {
75774845Svikram 				constraint = 1;
75784845Svikram 				break;
75794845Svikram 			}
75804845Svikram 		}
75814845Svikram 		kmem_free(path, MAXPATHLEN);
75824845Svikram 	}
75834845Svikram 
75844845Svikram 	mutex_enter(&DEVI(dip)->devi_lock);
75854845Svikram 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
75864845Svikram 	DEVI(dip)->devi_flags |= DEVI_RETIRING;
75874845Svikram 	if (constraint)
75884845Svikram 		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
75894845Svikram 	mutex_exit(&DEVI(dip)->devi_lock);
75904845Svikram 
75914845Svikram 	RIO_VERBOSE((CE_NOTE, "marked dip as undergoing retire process dip=%p",
75924845Svikram 	    (void *)dip));
75934845Svikram 
75944845Svikram 	if (constraint)
75954845Svikram 		RIO_DEBUG((CE_NOTE, "marked dip as constrained, dip=%p",
75964845Svikram 		    (void *)dip));
75974845Svikram 
75984845Svikram 	if (MDI_PHCI(dip))
75994845Svikram 		mdi_phci_mark_retiring(dip, cons_array);
76004845Svikram 
76014845Svikram 	return (DDI_WALK_CONTINUE);
76024845Svikram }
76034845Svikram 
76044845Svikram static void
76054845Svikram free_array(char **cons_array)
76064845Svikram {
76074845Svikram 	int	i;
76084845Svikram 
76094845Svikram 	if (cons_array == NULL)
76104845Svikram 		return;
76114845Svikram 
76124845Svikram 	for (i = 0; cons_array[i] != NULL; i++) {
76134845Svikram 		kmem_free(cons_array[i], strlen(cons_array[i]) + 1);
76144845Svikram 	}
76154845Svikram 	kmem_free(cons_array, (i+1) * sizeof (char *));
76164845Svikram }
76174845Svikram 
76184845Svikram /*
76194845Svikram  * Walk *every* node in subtree and check if it blocks, allows or has no
76204845Svikram  * comment on a proposed retire.
76214845Svikram  */
76224845Svikram int
76234845Svikram e_ddi_retire_notify(dev_info_t *dip, void *arg)
76244845Svikram {
76254845Svikram 	int	*constraint = (int *)arg;
76264845Svikram 
76274845Svikram 	RIO_DEBUG((CE_NOTE, "retire notify: dip = %p", (void *)dip));
76284845Svikram 
76294845Svikram 	(void) e_ddi_offline_notify(dip);
76304845Svikram 
76314845Svikram 	mutex_enter(&(DEVI(dip)->devi_lock));
76324845Svikram 	if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) {
76334845Svikram 		RIO_DEBUG((CE_WARN, "retire notify: dip in retire "
76344845Svikram 		    "subtree is not marked: dip = %p", (void *)dip));
76354845Svikram 		*constraint = 0;
76364845Svikram 	} else if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) {
76374845Svikram 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
76384845Svikram 		RIO_DEBUG((CE_NOTE, "retire notify: BLOCKED: dip = %p",
76394845Svikram 		    (void *)dip));
76404845Svikram 		*constraint = 0;
76414845Svikram 	} else if (!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT)) {
76424845Svikram 		RIO_DEBUG((CE_NOTE, "retire notify: NO CONSTRAINT: "
76434845Svikram 		    "dip = %p", (void *)dip));
76444845Svikram 		*constraint = 0;
76454845Svikram 	} else {
76464845Svikram 		RIO_DEBUG((CE_NOTE, "retire notify: CONSTRAINT set: "
76474845Svikram 		    "dip = %p", (void *)dip));
76484845Svikram 	}
76494845Svikram 	mutex_exit(&DEVI(dip)->devi_lock);
76504845Svikram 
76514845Svikram 	if (MDI_PHCI(dip))
76524845Svikram 		mdi_phci_retire_notify(dip, constraint);
76534845Svikram 
76544845Svikram 	return (DDI_WALK_CONTINUE);
76554845Svikram }
76564845Svikram 
76574845Svikram int
76584845Svikram e_ddi_retire_finalize(dev_info_t *dip, void *arg)
76594845Svikram {
76604845Svikram 	int constraint = *(int *)arg;
76614845Svikram 	int finalize;
76624845Svikram 	int phci_only;
76634845Svikram 
76644845Svikram 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
76654845Svikram 
76664845Svikram 	mutex_enter(&DEVI(dip)->devi_lock);
76674845Svikram 	if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) {
76684845Svikram 		RIO_DEBUG((CE_WARN,
76694845Svikram 		    "retire: unmarked dip(%p) in retire subtree",
76704845Svikram 		    (void *)dip));
76714845Svikram 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRED));
76724845Svikram 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
76734845Svikram 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
76744845Svikram 		mutex_exit(&DEVI(dip)->devi_lock);
76754845Svikram 		return (DDI_WALK_CONTINUE);
76764845Svikram 	}
76774845Svikram 
76784845Svikram 	/*
76794845Svikram 	 * retire the device if constraints have been applied
76804845Svikram 	 * or if the device is not in use
76814845Svikram 	 */
76824845Svikram 	finalize = 0;
76834845Svikram 	if (constraint) {
76844845Svikram 		ASSERT(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT);
76854845Svikram 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
76864845Svikram 		DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
76874845Svikram 		DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
76884845Svikram 		DEVI(dip)->devi_flags |= DEVI_RETIRED;
76894845Svikram 		mutex_exit(&DEVI(dip)->devi_lock);
76904845Svikram 		(void) spec_fence_snode(dip, NULL);
76914845Svikram 		RIO_DEBUG((CE_NOTE, "Fenced off: dip = %p", (void *)dip));
76924845Svikram 		e_ddi_offline_finalize(dip, DDI_SUCCESS);
76934845Svikram 	} else {
76944845Svikram 		if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) {
76954845Svikram 			ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
76964845Svikram 			DEVI(dip)->devi_flags &= ~DEVI_R_BLOCKED;
76974845Svikram 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
76984845Svikram 			/* we have already finalized during notify */
76994845Svikram 		} else if (DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT) {
77004845Svikram 			DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
77014845Svikram 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
77024845Svikram 			finalize = 1;
77034845Svikram 		} else {
77044845Svikram 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
77054845Svikram 			/*
77064845Svikram 			 * even if no contracts, need to call finalize
77074845Svikram 			 * to clear the contract barrier on the dip
77084845Svikram 			 */
77094845Svikram 			finalize = 1;
77104845Svikram 		}
77114845Svikram 		mutex_exit(&DEVI(dip)->devi_lock);
77124845Svikram 		RIO_DEBUG((CE_NOTE, "finalize: NOT retired: dip = %p",
77134845Svikram 		    (void *)dip));
77144845Svikram 		if (finalize)
77154845Svikram 			e_ddi_offline_finalize(dip, DDI_FAILURE);
77164845Svikram 	}
77174845Svikram 
77184845Svikram 	/*
77194845Svikram 	 * phci_only variable indicates no client checking, just
77204845Svikram 	 * offline the PHCI. We set that to 0 to enable client
77214845Svikram 	 * checking
77224845Svikram 	 */
77234845Svikram 	phci_only = 0;
77244845Svikram 	if (MDI_PHCI(dip))
77254845Svikram 		mdi_phci_retire_finalize(dip, phci_only);
77264845Svikram 
77274845Svikram 	return (DDI_WALK_CONTINUE);
77284845Svikram }
77294845Svikram 
77304845Svikram /*
77314845Svikram  * Returns
77324845Svikram  * 	DDI_SUCCESS if constraints allow retire
77334845Svikram  *	DDI_FAILURE if constraints don't allow retire.
77344845Svikram  * cons_array is a NULL terminated array of node paths for
77354845Svikram  * which constraints have already been applied.
77364845Svikram  */
77374845Svikram int
77384845Svikram e_ddi_retire_device(char *path, char **cons_array)
77394845Svikram {
77404845Svikram 	dev_info_t	*dip;
77414845Svikram 	dev_info_t	*pdip;
77424845Svikram 	int		circ;
77434845Svikram 	int		circ2;
77444845Svikram 	int		constraint;
77454845Svikram 	char		*devnm;
77464845Svikram 
77474845Svikram 	/*
77484845Svikram 	 * First, lookup the device
77494845Svikram 	 */
77504845Svikram 	dip = e_ddi_hold_devi_by_path(path, 0);
77514845Svikram 	if (dip == NULL) {
77524845Svikram 		/*
77534845Svikram 		 * device does not exist. This device cannot be
77544845Svikram 		 * a critical device since it is not in use. Thus
77554845Svikram 		 * this device is always retireable. Return DDI_SUCCESS
77564845Svikram 		 * to indicate this. If this device is ever
77574845Svikram 		 * instantiated, I/O framework will consult the
77584845Svikram 		 * the persistent retire store, mark it as
77594845Svikram 		 * retired and fence it off.
77604845Svikram 		 */
77614845Svikram 		RIO_DEBUG((CE_NOTE, "Retire device: device doesn't exist."
77624845Svikram 		    " NOP. Just returning SUCCESS. path=%s", path));
77634845Svikram 		free_array(cons_array);
77644845Svikram 		return (DDI_SUCCESS);
77654845Svikram 	}
77664845Svikram 
77674845Svikram 	RIO_DEBUG((CE_NOTE, "Retire device: found dip = %p.", (void *)dip));
77684845Svikram 
77694845Svikram 	pdip = ddi_get_parent(dip);
77704845Svikram 	ndi_hold_devi(pdip);
77714845Svikram 
77724845Svikram 	/*
77734845Svikram 	 * Run devfs_clean() in case dip has no constraints and is
77744845Svikram 	 * not in use, so is retireable but there are dv_nodes holding
77754845Svikram 	 * ref-count on the dip. Note that devfs_clean() always returns
77764845Svikram 	 * success.
77774845Svikram 	 */
77784845Svikram 	devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
77794845Svikram 	(void) ddi_deviname(dip, devnm);
77804845Svikram 	(void) devfs_clean(pdip, devnm + 1, DV_CLEAN_FORCE);
77814845Svikram 	kmem_free(devnm, MAXNAMELEN + 1);
77824845Svikram 
77834845Svikram 	ndi_devi_enter(pdip, &circ);
77844845Svikram 
77854845Svikram 	/* release hold from e_ddi_hold_devi_by_path */
77864845Svikram 	ndi_rele_devi(dip);
77874845Svikram 
77884845Svikram 	/*
77894845Svikram 	 * If it cannot make a determination, is_leaf_node() assumes
77904845Svikram 	 * dip is a nexus.
77914845Svikram 	 */
77924845Svikram 	(void) e_ddi_mark_retiring(dip, cons_array);
77934845Svikram 	if (!is_leaf_node(dip)) {
77944845Svikram 		ndi_devi_enter(dip, &circ2);
77954845Svikram 		ddi_walk_devs(ddi_get_child(dip), e_ddi_mark_retiring,
77964845Svikram 		    cons_array);
77974845Svikram 		ndi_devi_exit(dip, circ2);
77984845Svikram 	}
77994845Svikram 	free_array(cons_array);
78004845Svikram 
78014845Svikram 	/*
78024845Svikram 	 * apply constraints
78034845Svikram 	 */
78044845Svikram 	RIO_DEBUG((CE_NOTE, "retire: subtree retire notify: path = %s", path));
78054845Svikram 
78064845Svikram 	constraint = 1;	/* assume constraints allow retire */
78074845Svikram 	(void) e_ddi_retire_notify(dip, &constraint);
78084845Svikram 	if (!is_leaf_node(dip)) {
78094845Svikram 		ndi_devi_enter(dip, &circ2);
78104845Svikram 		ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_notify,
78114845Svikram 		    &constraint);
78124845Svikram 		ndi_devi_exit(dip, circ2);
78134845Svikram 	}
78144845Svikram 
78154845Svikram 	/*
78164845Svikram 	 * Now finalize the retire
78174845Svikram 	 */
78184845Svikram 	(void) e_ddi_retire_finalize(dip, &constraint);
78194845Svikram 	if (!is_leaf_node(dip)) {
78204845Svikram 		ndi_devi_enter(dip, &circ2);
78214845Svikram 		ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_finalize,
78224845Svikram 		    &constraint);
78234845Svikram 		ndi_devi_exit(dip, circ2);
78244845Svikram 	}
78254845Svikram 
78264845Svikram 	if (!constraint) {
78274845Svikram 		RIO_DEBUG((CE_WARN, "retire failed: path = %s", path));
78284845Svikram 	} else {
78294845Svikram 		RIO_DEBUG((CE_NOTE, "retire succeeded: path = %s", path));
78304845Svikram 	}
78314845Svikram 
78324845Svikram 	ndi_devi_exit(pdip, circ);
78334845Svikram 	ndi_rele_devi(pdip);
78344845Svikram 	return (constraint ? DDI_SUCCESS : DDI_FAILURE);
78354845Svikram }
78364845Svikram 
78374845Svikram static int
78384845Svikram unmark_and_unfence(dev_info_t *dip, void *arg)
78394845Svikram {
78404845Svikram 	char	*path = (char *)arg;
78414845Svikram 
78424845Svikram 	ASSERT(path);
78434845Svikram 
78444845Svikram 	(void) ddi_pathname(dip, path);
78454845Svikram 
78464845Svikram 	mutex_enter(&DEVI(dip)->devi_lock);
78474845Svikram 	DEVI(dip)->devi_flags &= ~DEVI_RETIRED;
78484845Svikram 	DEVI_SET_DEVICE_ONLINE(dip);
78494845Svikram 	mutex_exit(&DEVI(dip)->devi_lock);
78504845Svikram 
78514845Svikram 	RIO_VERBOSE((CE_NOTE, "Cleared RETIRED flag: dip=%p, path=%s",
78524845Svikram 	    (void *)dip, path));
78534845Svikram 
78544845Svikram 	(void) spec_unfence_snode(dip);
78554845Svikram 	RIO_DEBUG((CE_NOTE, "Unfenced device: %s", path));
78564845Svikram 
78574845Svikram 	if (MDI_PHCI(dip))
78584845Svikram 		mdi_phci_unretire(dip);
78594845Svikram 
78604845Svikram 	return (DDI_WALK_CONTINUE);
78614845Svikram }
78624845Svikram 
78634845Svikram struct find_dip {
78644845Svikram 	char	*fd_buf;
78654845Svikram 	char	*fd_path;
78664845Svikram 	dev_info_t *fd_dip;
78674845Svikram };
78684845Svikram 
78694845Svikram static int
78704845Svikram find_dip_fcn(dev_info_t *dip, void *arg)
78714845Svikram {
78724845Svikram 	struct find_dip *findp = (struct find_dip *)arg;
78734845Svikram 
78744845Svikram 	(void) ddi_pathname(dip, findp->fd_buf);
78754845Svikram 
78764845Svikram 	if (strcmp(findp->fd_path, findp->fd_buf) != 0)
78774845Svikram 		return (DDI_WALK_CONTINUE);
78784845Svikram 
78794845Svikram 	ndi_hold_devi(dip);
78804845Svikram 	findp->fd_dip = dip;
78814845Svikram 
78824845Svikram 	return (DDI_WALK_TERMINATE);
78834845Svikram }
78844845Svikram 
78854845Svikram int
78864845Svikram e_ddi_unretire_device(char *path)
78874845Svikram {
78884845Svikram 	int		circ;
78897235Svikram 	int		circ2;
78904845Svikram 	char		*path2;
78914845Svikram 	dev_info_t	*pdip;
78924845Svikram 	dev_info_t	*dip;
78934845Svikram 	struct find_dip	 find_dip;
78944845Svikram 
78954845Svikram 	ASSERT(path);
78964845Svikram 	ASSERT(*path == '/');
78974845Svikram 
78984845Svikram 	if (strcmp(path, "/") == 0) {
78994845Svikram 		cmn_err(CE_WARN, "Root node cannot be retired. Skipping "
79004845Svikram 		    "device unretire: %s", path);
79014845Svikram 		return (0);
79024845Svikram 	}
79034845Svikram 
79044845Svikram 	/*
79054845Svikram 	 * We can't lookup the dip (corresponding to path) via
79064845Svikram 	 * e_ddi_hold_devi_by_path() because the dip may be offline
79074845Svikram 	 * and may not attach. Use ddi_walk_devs() instead;
79084845Svikram 	 */
79094845Svikram 	find_dip.fd_buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
79104845Svikram 	find_dip.fd_path = path;
79114845Svikram 	find_dip.fd_dip = NULL;
79124845Svikram 
79134845Svikram 	pdip = ddi_root_node();
79144845Svikram 
79154845Svikram 	ndi_devi_enter(pdip, &circ);
79164845Svikram 	ddi_walk_devs(ddi_get_child(pdip), find_dip_fcn, &find_dip);
79174845Svikram 	ndi_devi_exit(pdip, circ);
79184845Svikram 
79194845Svikram 	kmem_free(find_dip.fd_buf, MAXPATHLEN);
79204845Svikram 
79214845Svikram 	if (find_dip.fd_dip == NULL) {
79224845Svikram 		cmn_err(CE_WARN, "Device not found in device tree. Skipping "
79234845Svikram 		    "device unretire: %s", path);
79244845Svikram 		return (0);
79254845Svikram 	}
79264845Svikram 
79274845Svikram 	dip = find_dip.fd_dip;
79284845Svikram 
79294845Svikram 	pdip = ddi_get_parent(dip);
79304845Svikram 
79314845Svikram 	ndi_hold_devi(pdip);
79324845Svikram 
79334845Svikram 	ndi_devi_enter(pdip, &circ);
79344845Svikram 
79354845Svikram 	path2 = kmem_alloc(MAXPATHLEN, KM_SLEEP);
79364845Svikram 
79374845Svikram 	(void) unmark_and_unfence(dip, path2);
79384845Svikram 	if (!is_leaf_node(dip)) {
79397235Svikram 		ndi_devi_enter(dip, &circ2);
79404845Svikram 		ddi_walk_devs(ddi_get_child(dip), unmark_and_unfence, path2);
79417235Svikram 		ndi_devi_exit(dip, circ2);
79424845Svikram 	}
79434845Svikram 
79444845Svikram 	kmem_free(path2, MAXPATHLEN);
79454845Svikram 
79464845Svikram 	/* release hold from find_dip_fcn() */
79474845Svikram 	ndi_rele_devi(dip);
79484845Svikram 
79494845Svikram 	ndi_devi_exit(pdip, circ);
79504845Svikram 
79514845Svikram 	ndi_rele_devi(pdip);
79524845Svikram 
79534845Svikram 	return (0);
79544845Svikram }
79554845Svikram 
79564845Svikram /*
79574845Svikram  * Called before attach on a dip that has been retired.
79584845Svikram  */
79594845Svikram static int
79604845Svikram mark_and_fence(dev_info_t *dip, void *arg)
79614845Svikram {
79624845Svikram 	char    *fencepath = (char *)arg;
79634845Svikram 
79644845Svikram 	/*
79654845Svikram 	 * We have already decided to retire this device. The various
79664845Svikram 	 * constraint checking should not be set.
79674845Svikram 	 * NOTE that the retire flag may already be set due to
79684845Svikram 	 * fenced -> detach -> fenced transitions.
79694845Svikram 	 */
79704845Svikram 	mutex_enter(&DEVI(dip)->devi_lock);
79714845Svikram 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
79724845Svikram 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
79734845Svikram 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRING));
79744845Svikram 	DEVI(dip)->devi_flags |= DEVI_RETIRED;
79754845Svikram 	mutex_exit(&DEVI(dip)->devi_lock);
79764845Svikram 	RIO_VERBOSE((CE_NOTE, "marked as RETIRED dip=%p", (void *)dip));
79774845Svikram 
79784845Svikram 	if (fencepath) {
79794845Svikram 		(void) spec_fence_snode(dip, NULL);
79804845Svikram 		RIO_DEBUG((CE_NOTE, "Fenced: %s",
79814845Svikram 		    ddi_pathname(dip, fencepath)));
79824845Svikram 	}
79834845Svikram 
79844845Svikram 	return (DDI_WALK_CONTINUE);
79854845Svikram }
79864845Svikram 
79874845Svikram /*
79884845Svikram  * Checks the retire database and:
79894845Svikram  *
79904845Svikram  * - if device is present in the retire database, marks the device retired
79914845Svikram  *   and fences it off.
79924845Svikram  * - if device is not in retire database, allows the device to attach normally
79934845Svikram  *
79944845Svikram  * To be called only by framework attach code on first attach attempt.
79954845Svikram  *
79964845Svikram  */
79974845Svikram static void
79984845Svikram i_ddi_check_retire(dev_info_t *dip)
79994845Svikram {
80004845Svikram 	char		*path;
80014845Svikram 	dev_info_t	*pdip;
80024845Svikram 	int		circ;
80034845Svikram 	int		phci_only;
80044845Svikram 
80054845Svikram 	pdip = ddi_get_parent(dip);
80064845Svikram 
80074845Svikram 	/*
80084845Svikram 	 * Root dip is treated special and doesn't take this code path.
80094845Svikram 	 * Also root can never be retired.
80104845Svikram 	 */
80114845Svikram 	ASSERT(pdip);
80124845Svikram 	ASSERT(DEVI_BUSY_OWNED(pdip));
80134845Svikram 	ASSERT(i_ddi_node_state(dip) < DS_ATTACHED);
80144845Svikram 
80154845Svikram 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
80164845Svikram 
80174845Svikram 	(void) ddi_pathname(dip, path);
80184845Svikram 
80194845Svikram 	RIO_VERBOSE((CE_NOTE, "Checking if dip should attach: dip=%p, path=%s",
80204845Svikram 	    (void *)dip, path));
80214845Svikram 
80224845Svikram 	/*
80234845Svikram 	 * Check if this device is in the "retired" store i.e.  should
80244845Svikram 	 * be retired. If not, we have nothing to do.
80254845Svikram 	 */
80264845Svikram 	if (e_ddi_device_retired(path) == 0) {
80274845Svikram 		RIO_VERBOSE((CE_NOTE, "device is NOT retired: path=%s", path));
80284845Svikram 		kmem_free(path, MAXPATHLEN);
80294845Svikram 		return;
80304845Svikram 	}
80314845Svikram 
80324845Svikram 	RIO_DEBUG((CE_NOTE, "attach: device is retired: path=%s", path));
80334845Svikram 
80344845Svikram 	/*
80354845Svikram 	 * Mark dips and fence off snodes (if any)
80364845Svikram 	 */
80374845Svikram 	RIO_DEBUG((CE_NOTE, "attach: Mark and fence subtree: path=%s", path));
80384845Svikram 	(void) mark_and_fence(dip, path);
80394845Svikram 	if (!is_leaf_node(dip)) {
80404845Svikram 		ndi_devi_enter(dip, &circ);
80414845Svikram 		ddi_walk_devs(ddi_get_child(dip), mark_and_fence, path);
80424845Svikram 		ndi_devi_exit(dip, circ);
80434845Svikram 	}
80444845Svikram 
80454845Svikram 	kmem_free(path, MAXPATHLEN);
80464845Svikram 
80474845Svikram 	/*
80484845Svikram 	 * We don't want to check the client. We just want to
80494845Svikram 	 * offline the PHCI
80504845Svikram 	 */
80514845Svikram 	phci_only = 1;
80524845Svikram 	if (MDI_PHCI(dip))
80534845Svikram 		mdi_phci_retire_finalize(dip, phci_only);
80544845Svikram }
8055