xref: /onnv-gate/usr/src/uts/common/os/devcfg.c (revision 13050:515b1e9bea30)
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 /*
2212107SStephen.Hanson@Sun.COM  * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate #include <sys/note.h>
260Sstevel@tonic-gate #include <sys/t_lock.h>
270Sstevel@tonic-gate #include <sys/cmn_err.h>
280Sstevel@tonic-gate #include <sys/instance.h>
290Sstevel@tonic-gate #include <sys/conf.h>
300Sstevel@tonic-gate #include <sys/stat.h>
310Sstevel@tonic-gate #include <sys/ddi.h>
320Sstevel@tonic-gate #include <sys/hwconf.h>
330Sstevel@tonic-gate #include <sys/sunddi.h>
340Sstevel@tonic-gate #include <sys/sunndi.h>
350Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
360Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
370Sstevel@tonic-gate #include <sys/modctl.h>
384845Svikram #include <sys/contract/device_impl.h>
390Sstevel@tonic-gate #include <sys/dacf.h>
400Sstevel@tonic-gate #include <sys/promif.h>
417656SSherry.Moore@Sun.COM #include <sys/pci.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>
537656SSherry.Moore@Sun.COM #include <sys/systm.h>
5410097SEric.Taylor@Sun.COM #include <sys/fs/sdev_impl.h>
554845Svikram #include <sys/sunldi.h>
564845Svikram #include <sys/sunldi_impl.h>
5710822SJack.Meng@Sun.COM #include <sys/bootprops.h>
5812116SVikram.Hegde@Sun.COM #include <sys/varargs.h>
5912116SVikram.Hegde@Sun.COM #include <sys/modhash.h>
6012116SVikram.Hegde@Sun.COM #include <sys/instance.h>
6110822SJack.Meng@Sun.COM 
6211600SVikram.Hegde@Sun.COM #if defined(__amd64) && !defined(__xpv)
638249SVikram.Hegde@Sun.COM #include <sys/iommulib.h>
648249SVikram.Hegde@Sun.COM #endif
6511600SVikram.Hegde@Sun.COM 
660Sstevel@tonic-gate #ifdef DEBUG
670Sstevel@tonic-gate int ddidebug = DDI_AUDIT;
680Sstevel@tonic-gate #else
690Sstevel@tonic-gate int ddidebug = 0;
700Sstevel@tonic-gate #endif
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #define	MT_CONFIG_OP	0
730Sstevel@tonic-gate #define	MT_UNCONFIG_OP	1
740Sstevel@tonic-gate 
750Sstevel@tonic-gate /* Multi-threaded configuration */
760Sstevel@tonic-gate struct mt_config_handle {
770Sstevel@tonic-gate 	kmutex_t mtc_lock;
780Sstevel@tonic-gate 	kcondvar_t mtc_cv;
790Sstevel@tonic-gate 	int mtc_thr_count;
800Sstevel@tonic-gate 	dev_info_t *mtc_pdip;	/* parent dip for mt_config_children */
810Sstevel@tonic-gate 	dev_info_t **mtc_fdip;	/* "a" dip where unconfigure failed */
820Sstevel@tonic-gate 	major_t mtc_parmajor;	/* parent major for mt_config_driver */
830Sstevel@tonic-gate 	major_t mtc_major;
840Sstevel@tonic-gate 	int mtc_flags;
850Sstevel@tonic-gate 	int mtc_op;		/* config or unconfig */
860Sstevel@tonic-gate 	int mtc_error;		/* operation error */
870Sstevel@tonic-gate 	struct brevq_node **mtc_brevqp;	/* outstanding branch events queue */
880Sstevel@tonic-gate #ifdef DEBUG
890Sstevel@tonic-gate 	int total_time;
900Sstevel@tonic-gate 	timestruc_t start_time;
910Sstevel@tonic-gate #endif /* DEBUG */
920Sstevel@tonic-gate };
930Sstevel@tonic-gate 
940Sstevel@tonic-gate struct devi_nodeid {
95789Sahrens 	pnode_t nodeid;
960Sstevel@tonic-gate 	dev_info_t *dip;
970Sstevel@tonic-gate 	struct devi_nodeid *next;
980Sstevel@tonic-gate };
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate struct devi_nodeid_list {
1010Sstevel@tonic-gate 	kmutex_t dno_lock;		/* Protects other fields */
1020Sstevel@tonic-gate 	struct devi_nodeid *dno_head;	/* list of devi nodeid elements */
1030Sstevel@tonic-gate 	struct devi_nodeid *dno_free;	/* Free list */
1040Sstevel@tonic-gate 	uint_t dno_list_length;		/* number of dips in list */
1050Sstevel@tonic-gate };
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate /* used to keep track of branch remove events to be generated */
1080Sstevel@tonic-gate struct brevq_node {
1091317Scth 	char *brn_deviname;
1101317Scth 	struct brevq_node *brn_sibling;
1111317Scth 	struct brevq_node *brn_child;
1120Sstevel@tonic-gate };
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate static struct devi_nodeid_list devi_nodeid_list;
1150Sstevel@tonic-gate static struct devi_nodeid_list *devimap = &devi_nodeid_list;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate /*
1180Sstevel@tonic-gate  * Well known nodes which are attached first at boot time.
1190Sstevel@tonic-gate  */
1200Sstevel@tonic-gate dev_info_t *top_devinfo;		/* root of device tree */
1210Sstevel@tonic-gate dev_info_t *options_dip;
1220Sstevel@tonic-gate dev_info_t *pseudo_dip;
1230Sstevel@tonic-gate dev_info_t *clone_dip;
1240Sstevel@tonic-gate dev_info_t *scsi_vhci_dip;		/* MPXIO dip */
1250Sstevel@tonic-gate major_t clone_major;
1260Sstevel@tonic-gate 
1272621Sllai1 /*
1282621Sllai1  * A non-global zone's /dev is derived from the device tree.
1292621Sllai1  * This generation number serves to indicate when a zone's
1302621Sllai1  * /dev may need to be updated.
1312621Sllai1  */
1322621Sllai1 volatile ulong_t devtree_gen;		/* generation number */
1332621Sllai1 
1340Sstevel@tonic-gate /* block all future dev_info state changes */
1357656SSherry.Moore@Sun.COM hrtime_t volatile devinfo_freeze = 0;
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate /* number of dev_info attaches/detaches currently in progress */
1380Sstevel@tonic-gate static ulong_t devinfo_attach_detach = 0;
1390Sstevel@tonic-gate 
1407576SJerry.Gilliam@Sun.COM extern int	sys_shutdown;
1410Sstevel@tonic-gate extern kmutex_t global_vhci_lock;
1420Sstevel@tonic-gate 
1432621Sllai1 /* bitset of DS_SYSAVAIL & DS_RECONFIG - no races, no lock */
1442621Sllai1 static int devname_state = 0;
1452621Sllai1 
1460Sstevel@tonic-gate /*
1470Sstevel@tonic-gate  * The devinfo snapshot cache and related variables.
1480Sstevel@tonic-gate  * The only field in the di_cache structure that needs initialization
1490Sstevel@tonic-gate  * is the mutex (cache_lock). However, since this is an adaptive mutex
1500Sstevel@tonic-gate  * (MUTEX_DEFAULT) - it is automatically initialized by being allocated
1510Sstevel@tonic-gate  * in zeroed memory (static storage class). Therefore no explicit
1520Sstevel@tonic-gate  * initialization of the di_cache structure is needed.
1530Sstevel@tonic-gate  */
1540Sstevel@tonic-gate struct di_cache	di_cache = {1};
1550Sstevel@tonic-gate int		di_cache_debug = 0;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate /* For ddvis, which needs pseudo children under PCI */
1580Sstevel@tonic-gate int pci_allow_pseudo_children = 0;
1590Sstevel@tonic-gate 
1604145Scth /* Allow path-oriented alias driver binding on driver.conf enumerated nodes */
1614145Scth int driver_conf_allow_path_alias = 1;
1624145Scth 
1630Sstevel@tonic-gate /*
1640Sstevel@tonic-gate  * The following switch is for service people, in case a
1650Sstevel@tonic-gate  * 3rd party driver depends on identify(9e) being called.
1660Sstevel@tonic-gate  */
1670Sstevel@tonic-gate int identify_9e = 0;
1680Sstevel@tonic-gate 
16912618SStephen.Hanson@Sun.COM /*
17012618SStephen.Hanson@Sun.COM  * Add flag so behaviour of preventing attach for retired persistant nodes
17112618SStephen.Hanson@Sun.COM  * can be disabled.
17212618SStephen.Hanson@Sun.COM  */
17312618SStephen.Hanson@Sun.COM int retire_prevents_attach = 1;
17412618SStephen.Hanson@Sun.COM 
1750Sstevel@tonic-gate int mtc_off;					/* turn off mt config */
1760Sstevel@tonic-gate 
1777656SSherry.Moore@Sun.COM int quiesce_debug = 0;
1787656SSherry.Moore@Sun.COM 
17912116SVikram.Hegde@Sun.COM boolean_t ddi_aliases_present = B_FALSE;
18012116SVikram.Hegde@Sun.COM ddi_alias_t ddi_aliases;
18112116SVikram.Hegde@Sun.COM uint_t tsd_ddi_redirect;
18212116SVikram.Hegde@Sun.COM 
18312116SVikram.Hegde@Sun.COM #define	DDI_ALIAS_HASH_SIZE	(2700)
18412116SVikram.Hegde@Sun.COM 
1850Sstevel@tonic-gate static kmem_cache_t *ddi_node_cache;		/* devinfo node cache */
1860Sstevel@tonic-gate static devinfo_log_header_t *devinfo_audit_log;	/* devinfo log */
1870Sstevel@tonic-gate static int devinfo_log_size;			/* size in pages */
1880Sstevel@tonic-gate 
18912116SVikram.Hegde@Sun.COM boolean_t ddi_err_panic = B_FALSE;
19012116SVikram.Hegde@Sun.COM 
1910Sstevel@tonic-gate static int lookup_compatible(dev_info_t *, uint_t);
1920Sstevel@tonic-gate static char *encode_composite_string(char **, uint_t, size_t *, uint_t);
1930Sstevel@tonic-gate static void link_to_driver_list(dev_info_t *);
1940Sstevel@tonic-gate static void unlink_from_driver_list(dev_info_t *);
1950Sstevel@tonic-gate static void add_to_dn_list(struct devnames *, dev_info_t *);
1960Sstevel@tonic-gate static void remove_from_dn_list(struct devnames *, dev_info_t *);
1970Sstevel@tonic-gate static dev_info_t *find_duplicate_child();
1980Sstevel@tonic-gate static void add_global_props(dev_info_t *);
1990Sstevel@tonic-gate static void remove_global_props(dev_info_t *);
2000Sstevel@tonic-gate static int uninit_node(dev_info_t *);
2010Sstevel@tonic-gate static void da_log_init(void);
2020Sstevel@tonic-gate static void da_log_enter(dev_info_t *);
2030Sstevel@tonic-gate static int walk_devs(dev_info_t *, int (*f)(dev_info_t *, void *), void *, int);
2040Sstevel@tonic-gate static int reset_nexus_flags(dev_info_t *, void *);
2050Sstevel@tonic-gate static void ddi_optimize_dtree(dev_info_t *);
2060Sstevel@tonic-gate static int is_leaf_node(dev_info_t *);
2070Sstevel@tonic-gate static struct mt_config_handle *mt_config_init(dev_info_t *, dev_info_t **,
2080Sstevel@tonic-gate     int, major_t, int, struct brevq_node **);
2090Sstevel@tonic-gate static void mt_config_children(struct mt_config_handle *);
2100Sstevel@tonic-gate static void mt_config_driver(struct mt_config_handle *);
2110Sstevel@tonic-gate static int mt_config_fini(struct mt_config_handle *);
2120Sstevel@tonic-gate static int devi_unconfig_common(dev_info_t *, dev_info_t **, int, major_t,
2130Sstevel@tonic-gate     struct brevq_node **);
2140Sstevel@tonic-gate static int
2150Sstevel@tonic-gate ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
2160Sstevel@tonic-gate     dev_info_t **childp, int flags);
2171093Shiremath static void i_link_vhci_node(dev_info_t *);
2182155Scth static void ndi_devi_exit_and_wait(dev_info_t *dip,
2192155Scth     int circular, clock_t end_time);
2204145Scth static int ndi_devi_unbind_driver(dev_info_t *dip);
2210Sstevel@tonic-gate 
22212618SStephen.Hanson@Sun.COM static int i_ddi_check_retire(dev_info_t *dip);
2234845Svikram 
2247656SSherry.Moore@Sun.COM static void quiesce_one_device(dev_info_t *, void *);
2254845Svikram 
22612116SVikram.Hegde@Sun.COM dev_info_t *ddi_alias_redirect(char *alias);
22712116SVikram.Hegde@Sun.COM char *ddi_curr_redirect(char *currpath);
22812116SVikram.Hegde@Sun.COM 
22912116SVikram.Hegde@Sun.COM 
2300Sstevel@tonic-gate /*
2310Sstevel@tonic-gate  * dev_info cache and node management
2320Sstevel@tonic-gate  */
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate /* initialize dev_info node cache */
2350Sstevel@tonic-gate void
i_ddi_node_cache_init()2360Sstevel@tonic-gate i_ddi_node_cache_init()
2370Sstevel@tonic-gate {
2380Sstevel@tonic-gate 	ASSERT(ddi_node_cache == NULL);
2390Sstevel@tonic-gate 	ddi_node_cache = kmem_cache_create("dev_info_node_cache",
2400Sstevel@tonic-gate 	    sizeof (struct dev_info), 0, NULL, NULL, NULL, NULL, NULL, 0);
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	if (ddidebug & DDI_AUDIT)
2430Sstevel@tonic-gate 		da_log_init();
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate 
24611596SJason.Beloro@Sun.COM 
2470Sstevel@tonic-gate /*
2480Sstevel@tonic-gate  * Allocating a dev_info node, callable from interrupt context with KM_NOSLEEP
2490Sstevel@tonic-gate  * The allocated node has a reference count of 0.
2500Sstevel@tonic-gate  */
2510Sstevel@tonic-gate dev_info_t *
i_ddi_alloc_node(dev_info_t * pdip,char * node_name,pnode_t nodeid,int instance,ddi_prop_t * sys_prop,int flag)252789Sahrens i_ddi_alloc_node(dev_info_t *pdip, char *node_name, pnode_t nodeid,
2530Sstevel@tonic-gate     int instance, ddi_prop_t *sys_prop, int flag)
2540Sstevel@tonic-gate {
2550Sstevel@tonic-gate 	struct dev_info *devi;
2560Sstevel@tonic-gate 	struct devi_nodeid *elem;
2570Sstevel@tonic-gate 	static char failed[] = "i_ddi_alloc_node: out of memory";
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	ASSERT(node_name != NULL);
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	if ((devi = kmem_cache_alloc(ddi_node_cache, flag)) == NULL) {
2620Sstevel@tonic-gate 		cmn_err(CE_NOTE, failed);
2630Sstevel@tonic-gate 		return (NULL);
2640Sstevel@tonic-gate 	}
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	bzero(devi, sizeof (struct dev_info));
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	if (devinfo_audit_log) {
2690Sstevel@tonic-gate 		devi->devi_audit = kmem_zalloc(sizeof (devinfo_audit_t), flag);
2700Sstevel@tonic-gate 		if (devi->devi_audit == NULL)
2710Sstevel@tonic-gate 			goto fail;
2720Sstevel@tonic-gate 	}
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	if ((devi->devi_node_name = i_ddi_strdup(node_name, flag)) == NULL)
2750Sstevel@tonic-gate 		goto fail;
2764145Scth 
2770Sstevel@tonic-gate 	/* default binding name is node name */
2780Sstevel@tonic-gate 	devi->devi_binding_name = devi->devi_node_name;
2797009Scth 	devi->devi_major = DDI_MAJOR_T_NONE;	/* unbound by default */
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	/*
2820Sstevel@tonic-gate 	 * Make a copy of system property
2830Sstevel@tonic-gate 	 */
2840Sstevel@tonic-gate 	if (sys_prop &&
2850Sstevel@tonic-gate 	    (devi->devi_sys_prop_ptr = i_ddi_prop_list_dup(sys_prop, flag))
2860Sstevel@tonic-gate 	    == NULL)
2870Sstevel@tonic-gate 		goto fail;
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	/*
2900Sstevel@tonic-gate 	 * Assign devi_nodeid, devi_node_class, devi_node_attributes
2910Sstevel@tonic-gate 	 * according to the following algorithm:
2920Sstevel@tonic-gate 	 *
2930Sstevel@tonic-gate 	 * nodeid arg			node class		node attributes
2940Sstevel@tonic-gate 	 *
2950Sstevel@tonic-gate 	 * DEVI_PSEUDO_NODEID		DDI_NC_PSEUDO		A
2960Sstevel@tonic-gate 	 * DEVI_SID_NODEID		DDI_NC_PSEUDO		A,P
2978912SChris.Horne@Sun.COM 	 * DEVI_SID_HIDDEN_NODEID	DDI_NC_PSEUDO		A,P,H
29810696SDavid.Hollister@Sun.COM 	 * DEVI_SID_HP_NODEID		DDI_NC_PSEUDO		A,P,h
29910696SDavid.Hollister@Sun.COM 	 * DEVI_SID_HP_HIDDEN_NODEID	DDI_NC_PSEUDO		A,P,H,h
3000Sstevel@tonic-gate 	 * other			DDI_NC_PROM		P
3010Sstevel@tonic-gate 	 *
3020Sstevel@tonic-gate 	 * Where A = DDI_AUTO_ASSIGNED_NODEID (auto-assign a nodeid)
3030Sstevel@tonic-gate 	 * and	 P = DDI_PERSISTENT
3048912SChris.Horne@Sun.COM 	 * and	 H = DDI_HIDDEN_NODE
30510696SDavid.Hollister@Sun.COM 	 * and	 h = DDI_HOTPLUG_NODE
3060Sstevel@tonic-gate 	 *
3070Sstevel@tonic-gate 	 * auto-assigned nodeids are also auto-freed.
3080Sstevel@tonic-gate 	 */
3098912SChris.Horne@Sun.COM 	devi->devi_node_attributes = 0;
3100Sstevel@tonic-gate 	switch (nodeid) {
3118912SChris.Horne@Sun.COM 	case DEVI_SID_HIDDEN_NODEID:
3128912SChris.Horne@Sun.COM 		devi->devi_node_attributes |= DDI_HIDDEN_NODE;
31310696SDavid.Hollister@Sun.COM 		goto sid;
31410696SDavid.Hollister@Sun.COM 
31510696SDavid.Hollister@Sun.COM 	case DEVI_SID_HP_NODEID:
31610696SDavid.Hollister@Sun.COM 		devi->devi_node_attributes |= DDI_HOTPLUG_NODE;
31710696SDavid.Hollister@Sun.COM 		goto sid;
31810696SDavid.Hollister@Sun.COM 
31910696SDavid.Hollister@Sun.COM 	case DEVI_SID_HP_HIDDEN_NODEID:
32010696SDavid.Hollister@Sun.COM 		devi->devi_node_attributes |= DDI_HIDDEN_NODE;
32110696SDavid.Hollister@Sun.COM 		devi->devi_node_attributes |= DDI_HOTPLUG_NODE;
32210696SDavid.Hollister@Sun.COM 		goto sid;
32310696SDavid.Hollister@Sun.COM 
3240Sstevel@tonic-gate 	case DEVI_SID_NODEID:
32510696SDavid.Hollister@Sun.COM sid:		devi->devi_node_attributes |= DDI_PERSISTENT;
3260Sstevel@tonic-gate 		if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL)
3270Sstevel@tonic-gate 			goto fail;
3280Sstevel@tonic-gate 		/*FALLTHROUGH*/
32910696SDavid.Hollister@Sun.COM 
3300Sstevel@tonic-gate 	case DEVI_PSEUDO_NODEID:
3310Sstevel@tonic-gate 		devi->devi_node_attributes |= DDI_AUTO_ASSIGNED_NODEID;
3320Sstevel@tonic-gate 		devi->devi_node_class = DDI_NC_PSEUDO;
3330Sstevel@tonic-gate 		if (impl_ddi_alloc_nodeid(&devi->devi_nodeid)) {
3340Sstevel@tonic-gate 			panic("i_ddi_alloc_node: out of nodeids");
3350Sstevel@tonic-gate 			/*NOTREACHED*/
3360Sstevel@tonic-gate 		}
3370Sstevel@tonic-gate 		break;
33810696SDavid.Hollister@Sun.COM 
3390Sstevel@tonic-gate 	default:
3400Sstevel@tonic-gate 		if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL)
3410Sstevel@tonic-gate 			goto fail;
34210696SDavid.Hollister@Sun.COM 
3430Sstevel@tonic-gate 		/*
3440Sstevel@tonic-gate 		 * the nodetype is 'prom', try to 'take' the nodeid now.
3450Sstevel@tonic-gate 		 * This requires memory allocation, so check for failure.
3460Sstevel@tonic-gate 		 */
3470Sstevel@tonic-gate 		if (impl_ddi_take_nodeid(nodeid, flag) != 0) {
3480Sstevel@tonic-gate 			kmem_free(elem, sizeof (*elem));
3490Sstevel@tonic-gate 			goto fail;
3500Sstevel@tonic-gate 		}
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 		devi->devi_nodeid = nodeid;
3530Sstevel@tonic-gate 		devi->devi_node_class = DDI_NC_PROM;
3540Sstevel@tonic-gate 		devi->devi_node_attributes = DDI_PERSISTENT;
35510696SDavid.Hollister@Sun.COM 		break;
3560Sstevel@tonic-gate 	}
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node((dev_info_t *)devi)) {
3590Sstevel@tonic-gate 		mutex_enter(&devimap->dno_lock);
3600Sstevel@tonic-gate 		elem->next = devimap->dno_free;
3610Sstevel@tonic-gate 		devimap->dno_free = elem;
3620Sstevel@tonic-gate 		mutex_exit(&devimap->dno_lock);
3630Sstevel@tonic-gate 	}
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	/*
3660Sstevel@tonic-gate 	 * Instance is normally initialized to -1. In a few special
3670Sstevel@tonic-gate 	 * cases, the caller may specify an instance (e.g. CPU nodes).
3680Sstevel@tonic-gate 	 */
3690Sstevel@tonic-gate 	devi->devi_instance = instance;
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	/*
3720Sstevel@tonic-gate 	 * set parent and bus_ctl parent
3730Sstevel@tonic-gate 	 */
3740Sstevel@tonic-gate 	devi->devi_parent = DEVI(pdip);
3750Sstevel@tonic-gate 	devi->devi_bus_ctl = DEVI(pdip);
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
3780Sstevel@tonic-gate 	    "i_ddi_alloc_node: name=%s id=%d\n", node_name, devi->devi_nodeid));
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	cv_init(&(devi->devi_cv), NULL, CV_DEFAULT, NULL);
3810Sstevel@tonic-gate 	mutex_init(&(devi->devi_lock), NULL, MUTEX_DEFAULT, NULL);
3820Sstevel@tonic-gate 	mutex_init(&(devi->devi_pm_lock), NULL, MUTEX_DEFAULT, NULL);
3830Sstevel@tonic-gate 	mutex_init(&(devi->devi_pm_busy_lock), NULL, MUTEX_DEFAULT, NULL);
3840Sstevel@tonic-gate 
3854845Svikram 	RIO_TRACE((CE_NOTE, "i_ddi_alloc_node: Initing contract fields: "
3864845Svikram 	    "dip=%p, name=%s", (void *)devi, node_name));
3874845Svikram 
3884845Svikram 	mutex_init(&(devi->devi_ct_lock), NULL, MUTEX_DEFAULT, NULL);
3894845Svikram 	cv_init(&(devi->devi_ct_cv), NULL, CV_DEFAULT, NULL);
3904845Svikram 	devi->devi_ct_count = -1;	/* counter not in use if -1 */
3914845Svikram 	list_create(&(devi->devi_ct), sizeof (cont_device_t),
3924845Svikram 	    offsetof(cont_device_t, cond_next));
3934845Svikram 
3940Sstevel@tonic-gate 	i_ddi_set_node_state((dev_info_t *)devi, DS_PROTO);
3950Sstevel@tonic-gate 	da_log_enter((dev_info_t *)devi);
3960Sstevel@tonic-gate 	return ((dev_info_t *)devi);
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate fail:
3990Sstevel@tonic-gate 	if (devi->devi_sys_prop_ptr)
4000Sstevel@tonic-gate 		i_ddi_prop_list_delete(devi->devi_sys_prop_ptr);
4010Sstevel@tonic-gate 	if (devi->devi_node_name)
4020Sstevel@tonic-gate 		kmem_free(devi->devi_node_name, strlen(node_name) + 1);
4030Sstevel@tonic-gate 	if (devi->devi_audit)
4040Sstevel@tonic-gate 		kmem_free(devi->devi_audit, sizeof (devinfo_audit_t));
4050Sstevel@tonic-gate 	kmem_cache_free(ddi_node_cache, devi);
4060Sstevel@tonic-gate 	cmn_err(CE_NOTE, failed);
4070Sstevel@tonic-gate 	return (NULL);
4080Sstevel@tonic-gate }
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate /*
4110Sstevel@tonic-gate  * free a dev_info structure.
4120Sstevel@tonic-gate  * NB. Not callable from interrupt since impl_ddi_free_nodeid may block.
4130Sstevel@tonic-gate  */
4140Sstevel@tonic-gate void
i_ddi_free_node(dev_info_t * dip)4150Sstevel@tonic-gate i_ddi_free_node(dev_info_t *dip)
4160Sstevel@tonic-gate {
4170Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
4180Sstevel@tonic-gate 	struct devi_nodeid *elem;
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	ASSERT(devi->devi_ref == 0);
4210Sstevel@tonic-gate 	ASSERT(devi->devi_addr == NULL);
4220Sstevel@tonic-gate 	ASSERT(devi->devi_node_state == DS_PROTO);
4230Sstevel@tonic-gate 	ASSERT(devi->devi_child == NULL);
42410923SEvan.Yan@Sun.COM 	ASSERT(devi->devi_hp_hdlp == NULL);
4250Sstevel@tonic-gate 
426439Scth 	/* free devi_addr_buf allocated by ddi_set_name_addr() */
427439Scth 	if (devi->devi_addr_buf)
428439Scth 		kmem_free(devi->devi_addr_buf, 2 * MAXNAMELEN);
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	if (i_ndi_dev_is_auto_assigned_node(dip))
4310Sstevel@tonic-gate 		impl_ddi_free_nodeid(DEVI(dip)->devi_nodeid);
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
4340Sstevel@tonic-gate 		mutex_enter(&devimap->dno_lock);
4350Sstevel@tonic-gate 		ASSERT(devimap->dno_free);
4360Sstevel@tonic-gate 		elem = devimap->dno_free;
4370Sstevel@tonic-gate 		devimap->dno_free = elem->next;
4380Sstevel@tonic-gate 		mutex_exit(&devimap->dno_lock);
4390Sstevel@tonic-gate 		kmem_free(elem, sizeof (*elem));
4400Sstevel@tonic-gate 	}
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	if (DEVI(dip)->devi_compat_names)
4430Sstevel@tonic-gate 		kmem_free(DEVI(dip)->devi_compat_names,
4440Sstevel@tonic-gate 		    DEVI(dip)->devi_compat_length);
4454145Scth 	if (DEVI(dip)->devi_rebinding_name)
4464145Scth 		kmem_free(DEVI(dip)->devi_rebinding_name,
4474145Scth 		    strlen(DEVI(dip)->devi_rebinding_name) + 1);
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	ddi_prop_remove_all(dip);	/* remove driver properties */
4500Sstevel@tonic-gate 	if (devi->devi_sys_prop_ptr)
4510Sstevel@tonic-gate 		i_ddi_prop_list_delete(devi->devi_sys_prop_ptr);
4520Sstevel@tonic-gate 	if (devi->devi_hw_prop_ptr)
4530Sstevel@tonic-gate 		i_ddi_prop_list_delete(devi->devi_hw_prop_ptr);
4540Sstevel@tonic-gate 
4556640Scth 	if (DEVI(dip)->devi_devid_str)
4566640Scth 		ddi_devid_str_free(DEVI(dip)->devi_devid_str);
4576640Scth 
4580Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_INVAL);
4590Sstevel@tonic-gate 	da_log_enter(dip);
4600Sstevel@tonic-gate 	if (devi->devi_audit) {
4610Sstevel@tonic-gate 		kmem_free(devi->devi_audit, sizeof (devinfo_audit_t));
4620Sstevel@tonic-gate 	}
4630Sstevel@tonic-gate 	if (devi->devi_device_class)
4640Sstevel@tonic-gate 		kmem_free(devi->devi_device_class,
4650Sstevel@tonic-gate 		    strlen(devi->devi_device_class) + 1);
4660Sstevel@tonic-gate 	cv_destroy(&(devi->devi_cv));
4670Sstevel@tonic-gate 	mutex_destroy(&(devi->devi_lock));
4680Sstevel@tonic-gate 	mutex_destroy(&(devi->devi_pm_lock));
4690Sstevel@tonic-gate 	mutex_destroy(&(devi->devi_pm_busy_lock));
4700Sstevel@tonic-gate 
4714845Svikram 	RIO_TRACE((CE_NOTE, "i_ddi_free_node: destroying contract fields: "
4724845Svikram 	    "dip=%p", (void *)dip));
4734845Svikram 	contract_device_remove_dip(dip);
4744845Svikram 	ASSERT(devi->devi_ct_count == -1);
4754845Svikram 	ASSERT(list_is_empty(&(devi->devi_ct)));
4764845Svikram 	cv_destroy(&(devi->devi_ct_cv));
4774845Svikram 	list_destroy(&(devi->devi_ct));
4784845Svikram 	/* free this last since contract_device_remove_dip() uses it */
4794845Svikram 	mutex_destroy(&(devi->devi_ct_lock));
4804845Svikram 	RIO_TRACE((CE_NOTE, "i_ddi_free_node: destroyed all contract fields: "
4814845Svikram 	    "dip=%p, name=%s", (void *)dip, devi->devi_node_name));
4824845Svikram 
4834845Svikram 	kmem_free(devi->devi_node_name, strlen(devi->devi_node_name) + 1);
4844845Svikram 
48512707SMichael.Bergknoff@Oracle.COM 	/* free event data */
48612707SMichael.Bergknoff@Oracle.COM 	if (devi->devi_ev_path)
48712707SMichael.Bergknoff@Oracle.COM 		kmem_free(devi->devi_ev_path, MAXPATHLEN);
48812707SMichael.Bergknoff@Oracle.COM 
4890Sstevel@tonic-gate 	kmem_cache_free(ddi_node_cache, devi);
4900Sstevel@tonic-gate }
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate /*
4940Sstevel@tonic-gate  * Node state transitions
4950Sstevel@tonic-gate  */
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate /*
4980Sstevel@tonic-gate  * Change the node name
4990Sstevel@tonic-gate  */
5000Sstevel@tonic-gate int
ndi_devi_set_nodename(dev_info_t * dip,char * name,int flags)5010Sstevel@tonic-gate ndi_devi_set_nodename(dev_info_t *dip, char *name, int flags)
5020Sstevel@tonic-gate {
5030Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags))
5040Sstevel@tonic-gate 	char *nname, *oname;
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	ASSERT(dip && name);
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	oname = DEVI(dip)->devi_node_name;
5090Sstevel@tonic-gate 	if (strcmp(oname, name) == 0)
5100Sstevel@tonic-gate 		return (DDI_SUCCESS);
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	/*
5130Sstevel@tonic-gate 	 * pcicfg_fix_ethernet requires a name change after node
5140Sstevel@tonic-gate 	 * is linked into the tree. When pcicfg is fixed, we
5150Sstevel@tonic-gate 	 * should only allow name change in DS_PROTO state.
5160Sstevel@tonic-gate 	 */
5170Sstevel@tonic-gate 	if (i_ddi_node_state(dip) >= DS_BOUND) {
5180Sstevel@tonic-gate 		/*
5190Sstevel@tonic-gate 		 * Don't allow name change once node is bound
5200Sstevel@tonic-gate 		 */
5210Sstevel@tonic-gate 		cmn_err(CE_NOTE,
5220Sstevel@tonic-gate 		    "ndi_devi_set_nodename: node already bound dip = %p,"
5230Sstevel@tonic-gate 		    " %s -> %s", (void *)dip, ddi_node_name(dip), name);
5240Sstevel@tonic-gate 		return (NDI_FAILURE);
5250Sstevel@tonic-gate 	}
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	nname = i_ddi_strdup(name, KM_SLEEP);
5280Sstevel@tonic-gate 	DEVI(dip)->devi_node_name = nname;
5290Sstevel@tonic-gate 	i_ddi_set_binding_name(dip, nname);
5300Sstevel@tonic-gate 	kmem_free(oname, strlen(oname) + 1);
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	da_log_enter(dip);
5330Sstevel@tonic-gate 	return (NDI_SUCCESS);
5340Sstevel@tonic-gate }
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate void
i_ddi_add_devimap(dev_info_t * dip)5370Sstevel@tonic-gate i_ddi_add_devimap(dev_info_t *dip)
5380Sstevel@tonic-gate {
5390Sstevel@tonic-gate 	struct devi_nodeid *elem;
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	ASSERT(dip);
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	if (!ndi_dev_is_persistent_node(dip))
5440Sstevel@tonic-gate 		return;
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	ASSERT(ddi_get_parent(dip) == NULL || (DEVI_VHCI_NODE(dip)) ||
5470Sstevel@tonic-gate 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	mutex_enter(&devimap->dno_lock);
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	ASSERT(devimap->dno_free);
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	elem = devimap->dno_free;
5540Sstevel@tonic-gate 	devimap->dno_free = elem->next;
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	elem->nodeid = ddi_get_nodeid(dip);
5570Sstevel@tonic-gate 	elem->dip = dip;
5580Sstevel@tonic-gate 	elem->next = devimap->dno_head;
5590Sstevel@tonic-gate 	devimap->dno_head = elem;
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	devimap->dno_list_length++;
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	mutex_exit(&devimap->dno_lock);
5640Sstevel@tonic-gate }
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate static int
i_ddi_remove_devimap(dev_info_t * dip)5670Sstevel@tonic-gate i_ddi_remove_devimap(dev_info_t *dip)
5680Sstevel@tonic-gate {
5690Sstevel@tonic-gate 	struct devi_nodeid *prev, *elem;
5700Sstevel@tonic-gate 	static const char *fcn = "i_ddi_remove_devimap";
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	ASSERT(dip);
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 	if (!ndi_dev_is_persistent_node(dip))
5750Sstevel@tonic-gate 		return (DDI_SUCCESS);
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	mutex_enter(&devimap->dno_lock);
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	/*
5800Sstevel@tonic-gate 	 * The following check is done with dno_lock held
5810Sstevel@tonic-gate 	 * to prevent race between dip removal and
5820Sstevel@tonic-gate 	 * e_ddi_prom_node_to_dip()
5830Sstevel@tonic-gate 	 */
5840Sstevel@tonic-gate 	if (e_ddi_devi_holdcnt(dip)) {
5850Sstevel@tonic-gate 		mutex_exit(&devimap->dno_lock);
5860Sstevel@tonic-gate 		return (DDI_FAILURE);
5870Sstevel@tonic-gate 	}
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 	ASSERT(devimap->dno_head);
5900Sstevel@tonic-gate 	ASSERT(devimap->dno_list_length > 0);
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	prev = NULL;
5930Sstevel@tonic-gate 	for (elem = devimap->dno_head; elem; elem = elem->next) {
5940Sstevel@tonic-gate 		if (elem->dip == dip) {
5950Sstevel@tonic-gate 			ASSERT(elem->nodeid == ddi_get_nodeid(dip));
5960Sstevel@tonic-gate 			break;
5970Sstevel@tonic-gate 		}
5980Sstevel@tonic-gate 		prev = elem;
5990Sstevel@tonic-gate 	}
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	if (elem && prev)
6020Sstevel@tonic-gate 		prev->next = elem->next;
6030Sstevel@tonic-gate 	else if (elem)
6040Sstevel@tonic-gate 		devimap->dno_head = elem->next;
6050Sstevel@tonic-gate 	else
6060Sstevel@tonic-gate 		panic("%s: devinfo node(%p) not found",
6070Sstevel@tonic-gate 		    fcn, (void *)dip);
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	devimap->dno_list_length--;
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	elem->nodeid = 0;
6120Sstevel@tonic-gate 	elem->dip = NULL;
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	elem->next = devimap->dno_free;
6150Sstevel@tonic-gate 	devimap->dno_free = elem;
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	mutex_exit(&devimap->dno_lock);
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	return (DDI_SUCCESS);
6200Sstevel@tonic-gate }
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate /*
6230Sstevel@tonic-gate  * Link this node into the devinfo tree and add to orphan list
6240Sstevel@tonic-gate  * Not callable from interrupt context
6250Sstevel@tonic-gate  */
6260Sstevel@tonic-gate static void
link_node(dev_info_t * dip)6270Sstevel@tonic-gate link_node(dev_info_t *dip)
6280Sstevel@tonic-gate {
6290Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
6300Sstevel@tonic-gate 	struct dev_info *parent = devi->devi_parent;
6310Sstevel@tonic-gate 	dev_info_t **dipp;
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	ASSERT(parent);	/* never called for root node */
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "link_node: parent = %s child = %s\n",
6360Sstevel@tonic-gate 	    parent->devi_node_name, devi->devi_node_name));
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	/*
6390Sstevel@tonic-gate 	 * Hold the global_vhci_lock before linking any direct
6400Sstevel@tonic-gate 	 * children of rootnex driver. This special lock protects
6410Sstevel@tonic-gate 	 * linking and unlinking for rootnext direct children.
6420Sstevel@tonic-gate 	 */
6430Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
6440Sstevel@tonic-gate 		mutex_enter(&global_vhci_lock);
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	/*
6470Sstevel@tonic-gate 	 * attach the node to end of the list unless the node is already there
6480Sstevel@tonic-gate 	 */
6490Sstevel@tonic-gate 	dipp = (dev_info_t **)(&DEVI(parent)->devi_child);
6500Sstevel@tonic-gate 	while (*dipp && (*dipp != dip)) {
6510Sstevel@tonic-gate 		dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling);
6520Sstevel@tonic-gate 	}
6530Sstevel@tonic-gate 	ASSERT(*dipp == NULL);	/* node is not linked */
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	/*
6560Sstevel@tonic-gate 	 * Now that we are in the tree, update the devi-nodeid map.
6570Sstevel@tonic-gate 	 */
6580Sstevel@tonic-gate 	i_ddi_add_devimap(dip);
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	/*
6610Sstevel@tonic-gate 	 * This is a temporary workaround for Bug 4618861.
6620Sstevel@tonic-gate 	 * We keep the scsi_vhci nexus node on the left side of the devinfo
6630Sstevel@tonic-gate 	 * tree (under the root nexus driver), so that virtual nodes under
66410923SEvan.Yan@Sun.COM 	 * scsi_vhci will be SUSPENDed first and RESUMEd last.	This ensures
6650Sstevel@tonic-gate 	 * that the pHCI nodes are active during times when their clients
6660Sstevel@tonic-gate 	 * may be depending on them.  This workaround embodies the knowledge
6670Sstevel@tonic-gate 	 * that system PM and CPR both traverse the tree left-to-right during
6680Sstevel@tonic-gate 	 * SUSPEND and right-to-left during RESUME.
6691093Shiremath 	 * Extending the workaround to IB Nexus/VHCI
6701093Shiremath 	 * driver also.
6710Sstevel@tonic-gate 	 */
6724145Scth 	if (strcmp(devi->devi_binding_name, "scsi_vhci") == 0) {
6730Sstevel@tonic-gate 		/* Add scsi_vhci to beginning of list */
6740Sstevel@tonic-gate 		ASSERT((dev_info_t *)parent == top_devinfo);
6750Sstevel@tonic-gate 		/* scsi_vhci under rootnex */
6760Sstevel@tonic-gate 		devi->devi_sibling = parent->devi_child;
6770Sstevel@tonic-gate 		parent->devi_child = devi;
6784145Scth 	} else if (strcmp(devi->devi_binding_name, "ib") == 0) {
6791093Shiremath 		i_link_vhci_node(dip);
6800Sstevel@tonic-gate 	} else {
6810Sstevel@tonic-gate 		/* Add to end of list */
6820Sstevel@tonic-gate 		*dipp = dip;
6830Sstevel@tonic-gate 		DEVI(dip)->devi_sibling = NULL;
6840Sstevel@tonic-gate 	}
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 	/*
6870Sstevel@tonic-gate 	 * Release the global_vhci_lock before linking any direct
6880Sstevel@tonic-gate 	 * children of rootnex driver.
6890Sstevel@tonic-gate 	 */
6900Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
6910Sstevel@tonic-gate 		mutex_exit(&global_vhci_lock);
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	/* persistent nodes go on orphan list */
6940Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip))
6950Sstevel@tonic-gate 		add_to_dn_list(&orphanlist, dip);
6960Sstevel@tonic-gate }
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate /*
6990Sstevel@tonic-gate  * Unlink this node from the devinfo tree
7000Sstevel@tonic-gate  */
7010Sstevel@tonic-gate static int
unlink_node(dev_info_t * dip)7020Sstevel@tonic-gate unlink_node(dev_info_t *dip)
7030Sstevel@tonic-gate {
7040Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
7050Sstevel@tonic-gate 	struct dev_info *parent = devi->devi_parent;
7060Sstevel@tonic-gate 	dev_info_t **dipp;
70710923SEvan.Yan@Sun.COM 	ddi_hp_cn_handle_t *hdlp;
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 	ASSERT(parent != NULL);
7100Sstevel@tonic-gate 	ASSERT(devi->devi_node_state == DS_LINKED);
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "unlink_node: name = %s\n",
7130Sstevel@tonic-gate 	    ddi_node_name(dip)));
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	/* check references */
7160Sstevel@tonic-gate 	if (devi->devi_ref || i_ddi_remove_devimap(dip) != DDI_SUCCESS)
7170Sstevel@tonic-gate 		return (DDI_FAILURE);
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	/*
7200Sstevel@tonic-gate 	 * Hold the global_vhci_lock before linking any direct
7210Sstevel@tonic-gate 	 * children of rootnex driver.
7220Sstevel@tonic-gate 	 */
7230Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
7240Sstevel@tonic-gate 		mutex_enter(&global_vhci_lock);
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	dipp = (dev_info_t **)(&DEVI(parent)->devi_child);
7270Sstevel@tonic-gate 	while (*dipp && (*dipp != dip)) {
7280Sstevel@tonic-gate 		dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling);
7290Sstevel@tonic-gate 	}
7300Sstevel@tonic-gate 	if (*dipp) {
7310Sstevel@tonic-gate 		*dipp = (dev_info_t *)(devi->devi_sibling);
7320Sstevel@tonic-gate 		devi->devi_sibling = NULL;
7330Sstevel@tonic-gate 	} else {
7340Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE, "unlink_node: %s not linked",
7350Sstevel@tonic-gate 		    devi->devi_node_name));
7360Sstevel@tonic-gate 	}
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 	/*
7390Sstevel@tonic-gate 	 * Release the global_vhci_lock before linking any direct
7400Sstevel@tonic-gate 	 * children of rootnex driver.
7410Sstevel@tonic-gate 	 */
7420Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
7430Sstevel@tonic-gate 		mutex_exit(&global_vhci_lock);
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 	/* Remove node from orphan list */
7460Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
7470Sstevel@tonic-gate 		remove_from_dn_list(&orphanlist, dip);
7480Sstevel@tonic-gate 	}
7490Sstevel@tonic-gate 
75010923SEvan.Yan@Sun.COM 	/* Update parent's hotplug handle list */
75110923SEvan.Yan@Sun.COM 	for (hdlp = DEVI(parent)->devi_hp_hdlp; hdlp; hdlp = hdlp->next) {
75210923SEvan.Yan@Sun.COM 		if (hdlp->cn_info.cn_child == dip)
75310923SEvan.Yan@Sun.COM 			hdlp->cn_info.cn_child = NULL;
75410923SEvan.Yan@Sun.COM 	}
7550Sstevel@tonic-gate 	return (DDI_SUCCESS);
7560Sstevel@tonic-gate }
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate /*
7590Sstevel@tonic-gate  * Bind this devinfo node to a driver. If compat is NON-NULL, try that first.
7600Sstevel@tonic-gate  * Else, use the node-name.
7610Sstevel@tonic-gate  *
7620Sstevel@tonic-gate  * NOTE: IEEE1275 specifies that nodename should be tried before compatible.
7630Sstevel@tonic-gate  *	Solaris implementation binds nodename after compatible.
7640Sstevel@tonic-gate  *
7650Sstevel@tonic-gate  * If we find a binding,
7668912SChris.Horne@Sun.COM  * - set the binding name to the string,
7670Sstevel@tonic-gate  * - set major number to driver major
7680Sstevel@tonic-gate  *
7690Sstevel@tonic-gate  * If we don't find a binding,
7700Sstevel@tonic-gate  * - return failure
7710Sstevel@tonic-gate  */
7720Sstevel@tonic-gate static int
bind_node(dev_info_t * dip)7730Sstevel@tonic-gate bind_node(dev_info_t *dip)
7740Sstevel@tonic-gate {
7750Sstevel@tonic-gate 	char *p = NULL;
7767009Scth 	major_t major = DDI_MAJOR_T_NONE;
7770Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
7780Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	ASSERT(devi->devi_node_state == DS_LINKED);
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "bind_node: 0x%p(name = %s)\n",
7830Sstevel@tonic-gate 	    (void *)dip, ddi_node_name(dip)));
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
7860Sstevel@tonic-gate 	if (DEVI(dip)->devi_flags & DEVI_NO_BIND) {
7870Sstevel@tonic-gate 		mutex_exit(&DEVI(dip)->devi_lock);
7880Sstevel@tonic-gate 		return (DDI_FAILURE);
7890Sstevel@tonic-gate 	}
7900Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	/* find the driver with most specific binding using compatible */
7930Sstevel@tonic-gate 	major = ddi_compatible_driver_major(dip, &p);
7947009Scth 	if (major == DDI_MAJOR_T_NONE)
7950Sstevel@tonic-gate 		return (DDI_FAILURE);
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 	devi->devi_major = major;
7980Sstevel@tonic-gate 	if (p != NULL) {
7990Sstevel@tonic-gate 		i_ddi_set_binding_name(dip, p);
8000Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "bind_node: %s bound to %s\n",
8010Sstevel@tonic-gate 		    devi->devi_node_name, p));
8020Sstevel@tonic-gate 	}
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 	/* Link node to per-driver list */
8050Sstevel@tonic-gate 	link_to_driver_list(dip);
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 	/*
8080Sstevel@tonic-gate 	 * reset parent flag so that nexus will merge .conf props
8090Sstevel@tonic-gate 	 */
8100Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
8110Sstevel@tonic-gate 		mutex_enter(&DEVI(parent)->devi_lock);
8120Sstevel@tonic-gate 		DEVI(parent)->devi_flags &=
8130Sstevel@tonic-gate 		    ~(DEVI_ATTACHED_CHILDREN|DEVI_MADE_CHILDREN);
8140Sstevel@tonic-gate 		mutex_exit(&DEVI(parent)->devi_lock);
8150Sstevel@tonic-gate 	}
8160Sstevel@tonic-gate 	return (DDI_SUCCESS);
8170Sstevel@tonic-gate }
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate /*
8200Sstevel@tonic-gate  * Unbind this devinfo node
8210Sstevel@tonic-gate  * Called before the node is destroyed or driver is removed from system
8220Sstevel@tonic-gate  */
8230Sstevel@tonic-gate static int
unbind_node(dev_info_t * dip)8240Sstevel@tonic-gate unbind_node(dev_info_t *dip)
8250Sstevel@tonic-gate {
8260Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_node_state == DS_BOUND);
8277009Scth 	ASSERT(DEVI(dip)->devi_major != DDI_MAJOR_T_NONE);
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	/* check references */
8300Sstevel@tonic-gate 	if (DEVI(dip)->devi_ref)
8310Sstevel@tonic-gate 		return (DDI_FAILURE);
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "unbind_node: 0x%p(name = %s)\n",
8340Sstevel@tonic-gate 	    (void *)dip, ddi_node_name(dip)));
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	unlink_from_driver_list(dip);
8374145Scth 
8387009Scth 	DEVI(dip)->devi_major = DDI_MAJOR_T_NONE;
8394145Scth 	DEVI(dip)->devi_binding_name = DEVI(dip)->devi_node_name;
8400Sstevel@tonic-gate 	return (DDI_SUCCESS);
8410Sstevel@tonic-gate }
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate /*
8440Sstevel@tonic-gate  * Initialize a node: calls the parent nexus' bus_ctl ops to do the operation.
8450Sstevel@tonic-gate  * Must hold parent and per-driver list while calling this function.
8460Sstevel@tonic-gate  * A successful init_node() returns with an active ndi_hold_devi() hold on
8470Sstevel@tonic-gate  * the parent.
8480Sstevel@tonic-gate  */
8490Sstevel@tonic-gate static int
init_node(dev_info_t * dip)8500Sstevel@tonic-gate init_node(dev_info_t *dip)
8510Sstevel@tonic-gate {
8520Sstevel@tonic-gate 	int error;
8530Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
8540Sstevel@tonic-gate 	int (*f)(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *, void *);
8550Sstevel@tonic-gate 	char *path;
8564145Scth 	major_t	major;
85712121SReed.Liu@Sun.COM 	ddi_devid_t devid = NULL;
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_BOUND);
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	/* should be DS_READY except for pcmcia ... */
8620Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(pdip) >= DS_PROBED);
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8650Sstevel@tonic-gate 	(void) ddi_pathname(dip, path);
8660Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "init_node: entry: path %s 0x%p\n",
8670Sstevel@tonic-gate 	    path, (void *)dip));
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 	/*
8700Sstevel@tonic-gate 	 * The parent must have a bus_ctl operation.
8710Sstevel@tonic-gate 	 */
8720Sstevel@tonic-gate 	if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
8730Sstevel@tonic-gate 	    (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_ctl) == NULL) {
8740Sstevel@tonic-gate 		error = DDI_FAILURE;
8750Sstevel@tonic-gate 		goto out;
8760Sstevel@tonic-gate 	}
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 	add_global_props(dip);
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	/*
8810Sstevel@tonic-gate 	 * Invoke the parent's bus_ctl operation with the DDI_CTLOPS_INITCHILD
8820Sstevel@tonic-gate 	 * command to transform the child to canonical form 1. If there
8830Sstevel@tonic-gate 	 * is an error, ddi_remove_child should be called, to clean up.
8840Sstevel@tonic-gate 	 */
8850Sstevel@tonic-gate 	error = (*f)(pdip, pdip, DDI_CTLOPS_INITCHILD, dip, NULL);
8860Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
8870Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "init_node: %s 0x%p failed\n",
8880Sstevel@tonic-gate 		    path, (void *)dip));
8890Sstevel@tonic-gate 		remove_global_props(dip);
89013015Sgavin.maltby@oracle.com 
89113015Sgavin.maltby@oracle.com 		/*
89213015Sgavin.maltby@oracle.com 		 * If a nexus INITCHILD implementation calls ddi_devid_regster()
89313015Sgavin.maltby@oracle.com 		 * prior to setting devi_addr, the devid is not recorded in
89413015Sgavin.maltby@oracle.com 		 * the devid cache (i.e. DEVI_CACHED_DEVID is not set).
89513015Sgavin.maltby@oracle.com 		 * With mpxio, while the vhci client path may be missing
89613015Sgavin.maltby@oracle.com 		 * from the cache, phci pathinfo paths may have already be
89713015Sgavin.maltby@oracle.com 		 * added to the cache, against the client dip, by use of
89813015Sgavin.maltby@oracle.com 		 * e_devid_cache_pathinfo().  Because of this, when INITCHILD
89913015Sgavin.maltby@oracle.com 		 * of the client fails, we need to purge the client dip from
90013015Sgavin.maltby@oracle.com 		 * the cache even if DEVI_CACHED_DEVID is not set - if only
90113015Sgavin.maltby@oracle.com 		 * devi_devid_str is set.
90213015Sgavin.maltby@oracle.com 		 */
90313015Sgavin.maltby@oracle.com 		mutex_enter(&DEVI(dip)->devi_lock);
90413015Sgavin.maltby@oracle.com 		if ((DEVI(dip)->devi_flags & DEVI_CACHED_DEVID) ||
90513015Sgavin.maltby@oracle.com 		    DEVI(dip)->devi_devid_str) {
90613015Sgavin.maltby@oracle.com 			DEVI(dip)->devi_flags &= ~DEVI_CACHED_DEVID;
90713015Sgavin.maltby@oracle.com 			mutex_exit(&DEVI(dip)->devi_lock);
90813015Sgavin.maltby@oracle.com 			ddi_devid_unregister(dip);
90913015Sgavin.maltby@oracle.com 		} else
91013015Sgavin.maltby@oracle.com 			mutex_exit(&DEVI(dip)->devi_lock);
91113015Sgavin.maltby@oracle.com 
9120Sstevel@tonic-gate 		/* in case nexus driver didn't clear this field */
9130Sstevel@tonic-gate 		ddi_set_name_addr(dip, NULL);
9140Sstevel@tonic-gate 		error = DDI_FAILURE;
9150Sstevel@tonic-gate 		goto out;
9160Sstevel@tonic-gate 	}
9170Sstevel@tonic-gate 
9184950Scth 	ndi_hold_devi(pdip);			/* initial hold of parent */
9190Sstevel@tonic-gate 
9204145Scth 	/* recompute path after initchild for @addr information */
9214145Scth 	(void) ddi_pathname(dip, path);
9224145Scth 
9234145Scth 	/* Check for duplicate nodes */
9240Sstevel@tonic-gate 	if (find_duplicate_child(pdip, dip) != NULL) {
9250Sstevel@tonic-gate 		/*
9260Sstevel@tonic-gate 		 * uninit_node() the duplicate - a successful uninit_node()
9274950Scth 		 * will release inital hold of parent using ndi_rele_devi().
9280Sstevel@tonic-gate 		 */
9290Sstevel@tonic-gate 		if ((error = uninit_node(dip)) != DDI_SUCCESS) {
9304950Scth 			ndi_rele_devi(pdip);	/* release initial hold */
9310Sstevel@tonic-gate 			cmn_err(CE_WARN, "init_node: uninit of duplicate "
9320Sstevel@tonic-gate 			    "node %s failed", path);
9330Sstevel@tonic-gate 		}
9340Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "init_node: duplicate uninit "
9350Sstevel@tonic-gate 		    "%s 0x%p%s\n", path, (void *)dip,
9360Sstevel@tonic-gate 		    (error == DDI_SUCCESS) ? "" : " failed"));
9370Sstevel@tonic-gate 		error = DDI_FAILURE;
9380Sstevel@tonic-gate 		goto out;
9390Sstevel@tonic-gate 	}
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 	/*
94212121SReed.Liu@Sun.COM 	 * If a devid was registered for a DS_BOUND node then the devid_cache
94312121SReed.Liu@Sun.COM 	 * may not have captured the path. Detect this situation and ensure that
94412121SReed.Liu@Sun.COM 	 * the path enters the cache now that devi_addr is established.
94512121SReed.Liu@Sun.COM 	 */
94613015Sgavin.maltby@oracle.com 	if (!(DEVI(dip)->devi_flags & DEVI_CACHED_DEVID) &&
94712121SReed.Liu@Sun.COM 	    (ddi_devid_get(dip, &devid) == DDI_SUCCESS)) {
94812121SReed.Liu@Sun.COM 		if (e_devid_cache_register(dip, devid) == DDI_SUCCESS) {
94913015Sgavin.maltby@oracle.com 			mutex_enter(&DEVI(dip)->devi_lock);
95013015Sgavin.maltby@oracle.com 			DEVI(dip)->devi_flags |= DEVI_CACHED_DEVID;
95113015Sgavin.maltby@oracle.com 			mutex_exit(&DEVI(dip)->devi_lock);
95212121SReed.Liu@Sun.COM 		}
95312121SReed.Liu@Sun.COM 
95412121SReed.Liu@Sun.COM 		ddi_devid_free(devid);
95512121SReed.Liu@Sun.COM 	}
95612121SReed.Liu@Sun.COM 
95712121SReed.Liu@Sun.COM 	/*
9584145Scth 	 * Check to see if we have a path-oriented driver alias that overrides
9594145Scth 	 * the current driver binding. If so, we need to rebind. This check
9604145Scth 	 * needs to be delayed until after a successful DDI_CTLOPS_INITCHILD,
9614145Scth 	 * so the unit-address is established on the last component of the path.
9624145Scth 	 *
9634145Scth 	 * NOTE: Allowing a path-oriented alias to change the driver binding
9644145Scth 	 * of a driver.conf node results in non-intuitive property behavior.
9654145Scth 	 * We provide a tunable (driver_conf_allow_path_alias) to control
9664145Scth 	 * this behavior. See uninit_node() for more details.
9674145Scth 	 *
9684145Scth 	 * NOTE: If you are adding a path-oriented alias for the boot device,
9694145Scth 	 * and there is mismatch between OBP and the kernel in regard to
9704145Scth 	 * generic name use, like "disk" .vs. "ssd", then you will need
9714145Scth 	 * to add a path-oriented alias for both paths.
9724145Scth 	 */
9734145Scth 	major = ddi_name_to_major(path);
97412588SJerry.Gilliam@Sun.COM 	if (driver_active(major) && (major != DEVI(dip)->devi_major) &&
9754145Scth 	    (ndi_dev_is_persistent_node(dip) || driver_conf_allow_path_alias)) {
9764145Scth 
9774145Scth 		/* Mark node for rebind processing. */
9784145Scth 		mutex_enter(&DEVI(dip)->devi_lock);
9794145Scth 		DEVI(dip)->devi_flags |= DEVI_REBIND;
9804145Scth 		mutex_exit(&DEVI(dip)->devi_lock);
9814145Scth 
9824145Scth 		/*
9834950Scth 		 * Add an extra hold on the parent to prevent it from ever
9844950Scth 		 * having a zero devi_ref during the child rebind process.
9854950Scth 		 * This is necessary to ensure that the parent will never
9864950Scth 		 * detach(9E) during the rebind.
9874950Scth 		 */
9884950Scth 		ndi_hold_devi(pdip);		/* extra hold of parent */
9894950Scth 
9904950Scth 		/*
9914145Scth 		 * uninit_node() current binding - a successful uninit_node()
9924950Scth 		 * will release extra hold of parent using ndi_rele_devi().
9934145Scth 		 */
9944145Scth 		if ((error = uninit_node(dip)) != DDI_SUCCESS) {
9954950Scth 			ndi_rele_devi(pdip);	/* release extra hold */
9964950Scth 			ndi_rele_devi(pdip);	/* release initial hold */
9974145Scth 			cmn_err(CE_WARN, "init_node: uninit for rebind "
9984145Scth 			    "of node %s failed", path);
9994145Scth 			goto out;
10004145Scth 		}
10014145Scth 
10024145Scth 		/* Unbind: demote the node back to DS_LINKED.  */
10034145Scth 		if ((error = ndi_devi_unbind_driver(dip)) != DDI_SUCCESS) {
10048912SChris.Horne@Sun.COM 			ndi_rele_devi(pdip);	/* release initial hold */
10054145Scth 			cmn_err(CE_WARN, "init_node: unbind for rebind "
10064145Scth 			    "of node %s failed", path);
10074145Scth 			goto out;
10084145Scth 		}
10094145Scth 
10104145Scth 		/* establish rebinding name */
10114145Scth 		if (DEVI(dip)->devi_rebinding_name == NULL)
10124145Scth 			DEVI(dip)->devi_rebinding_name =
10134145Scth 			    i_ddi_strdup(path, KM_SLEEP);
10144145Scth 
10154145Scth 		/*
10164145Scth 		 * Now that we are demoted and marked for rebind, repromote.
10174145Scth 		 * We need to do this in steps, instead of just calling
10184145Scth 		 * ddi_initchild, so that we can redo the merge operation
10194145Scth 		 * after we are rebound to the path-bound driver.
10204145Scth 		 *
10214145Scth 		 * Start by rebinding node to the path-bound driver.
10224145Scth 		 */
10234145Scth 		if ((error = ndi_devi_bind_driver(dip, 0)) != DDI_SUCCESS) {
10248912SChris.Horne@Sun.COM 			ndi_rele_devi(pdip);	/* release initial hold */
10254145Scth 			cmn_err(CE_WARN, "init_node: rebind "
10264145Scth 			    "of node %s failed", path);
10274145Scth 			goto out;
10284145Scth 		}
10294145Scth 
10304145Scth 		/*
10314145Scth 		 * If the node is not a driver.conf node then merge
10324145Scth 		 * driver.conf properties from new path-bound driver.conf.
10334145Scth 		 */
10344145Scth 		if (ndi_dev_is_persistent_node(dip))
10354145Scth 			(void) i_ndi_make_spec_children(pdip, 0);
10364145Scth 
10374145Scth 		/*
10384145Scth 		 * Now that we have taken care of merge, repromote back
10394145Scth 		 * to DS_INITIALIZED.
10404145Scth 		 */
10414145Scth 		error = ddi_initchild(pdip, dip);
10424145Scth 		NDI_CONFIG_DEBUG((CE_CONT, "init_node: rebind "
10434145Scth 		    "%s 0x%p\n", path, (void *)dip));
10444950Scth 
10454950Scth 		/*
10464950Scth 		 * Release our initial hold. If ddi_initchild() was
10477224Scth 		 * successful then it will return with the active hold.
10484950Scth 		 */
10494950Scth 		ndi_rele_devi(pdip);
10504145Scth 		goto out;
10514145Scth 	}
10524145Scth 
10534145Scth 	/*
10540Sstevel@tonic-gate 	 * Apply multi-parent/deep-nexus optimization to the new node
10550Sstevel@tonic-gate 	 */
10560Sstevel@tonic-gate 	DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
10570Sstevel@tonic-gate 	ddi_optimize_dtree(dip);
10584950Scth 	error = DDI_SUCCESS;		/* return with active hold */
10590Sstevel@tonic-gate 
10604145Scth out:	if (error != DDI_SUCCESS) {
10614145Scth 		/* On failure ensure that DEVI_REBIND is cleared */
10624145Scth 		mutex_enter(&DEVI(dip)->devi_lock);
10634145Scth 		DEVI(dip)->devi_flags &= ~DEVI_REBIND;
10644145Scth 		mutex_exit(&DEVI(dip)->devi_lock);
10654145Scth 	}
10664145Scth 	kmem_free(path, MAXPATHLEN);
10670Sstevel@tonic-gate 	return (error);
10680Sstevel@tonic-gate }
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate /*
10710Sstevel@tonic-gate  * Uninitialize node
10720Sstevel@tonic-gate  * The per-driver list must be held busy during the call.
10730Sstevel@tonic-gate  * A successful uninit_node() releases the init_node() hold on
10740Sstevel@tonic-gate  * the parent by calling ndi_rele_devi().
10750Sstevel@tonic-gate  */
10760Sstevel@tonic-gate static int
uninit_node(dev_info_t * dip)10770Sstevel@tonic-gate uninit_node(dev_info_t *dip)
10780Sstevel@tonic-gate {
10790Sstevel@tonic-gate 	int node_state_entry;
10800Sstevel@tonic-gate 	dev_info_t *pdip;
10810Sstevel@tonic-gate 	struct dev_ops *ops;
10820Sstevel@tonic-gate 	int (*f)();
10830Sstevel@tonic-gate 	int error;
10840Sstevel@tonic-gate 	char *addr;
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	/*
10870Sstevel@tonic-gate 	 * Don't check for references here or else a ref-counted
10880Sstevel@tonic-gate 	 * dip cannot be downgraded by the framework.
10890Sstevel@tonic-gate 	 */
10900Sstevel@tonic-gate 	node_state_entry = i_ddi_node_state(dip);
10910Sstevel@tonic-gate 	ASSERT((node_state_entry == DS_BOUND) ||
10924950Scth 	    (node_state_entry == DS_INITIALIZED));
10930Sstevel@tonic-gate 	pdip = ddi_get_parent(dip);
10940Sstevel@tonic-gate 	ASSERT(pdip);
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "uninit_node: 0x%p(%s%d)\n",
10970Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 	if (((ops = ddi_get_driver(pdip)) == NULL) ||
11000Sstevel@tonic-gate 	    (ops->devo_bus_ops == NULL) ||
11010Sstevel@tonic-gate 	    ((f = ops->devo_bus_ops->bus_ctl) == NULL)) {
11020Sstevel@tonic-gate 		return (DDI_FAILURE);
11030Sstevel@tonic-gate 	}
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate 	/*
11060Sstevel@tonic-gate 	 * save the @addr prior to DDI_CTLOPS_UNINITCHILD for use in
11070Sstevel@tonic-gate 	 * freeing the instance if it succeeds.
11080Sstevel@tonic-gate 	 */
11090Sstevel@tonic-gate 	if (node_state_entry == DS_INITIALIZED) {
11100Sstevel@tonic-gate 		addr = ddi_get_name_addr(dip);
11110Sstevel@tonic-gate 		if (addr)
11120Sstevel@tonic-gate 			addr = i_ddi_strdup(addr, KM_SLEEP);
11130Sstevel@tonic-gate 	} else {
11140Sstevel@tonic-gate 		addr = NULL;
11150Sstevel@tonic-gate 	}
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 	error = (*f)(pdip, pdip, DDI_CTLOPS_UNINITCHILD, dip, (void *)NULL);
11180Sstevel@tonic-gate 	if (error == DDI_SUCCESS) {
11199066SPrasad.Singamsetty@Sun.COM 		/* ensure that devids are unregistered */
112013015Sgavin.maltby@oracle.com 		mutex_enter(&DEVI(dip)->devi_lock);
112113015Sgavin.maltby@oracle.com 		if ((DEVI(dip)->devi_flags & DEVI_CACHED_DEVID)) {
112213015Sgavin.maltby@oracle.com 			DEVI(dip)->devi_flags &= ~DEVI_CACHED_DEVID;
112313015Sgavin.maltby@oracle.com 			mutex_exit(&DEVI(dip)->devi_lock);
11249066SPrasad.Singamsetty@Sun.COM 			ddi_devid_unregister(dip);
112513015Sgavin.maltby@oracle.com 		} else
112613015Sgavin.maltby@oracle.com 			mutex_exit(&DEVI(dip)->devi_lock);
11279066SPrasad.Singamsetty@Sun.COM 
11280Sstevel@tonic-gate 		/* if uninitchild forgot to set devi_addr to NULL do it now */
11290Sstevel@tonic-gate 		ddi_set_name_addr(dip, NULL);
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 		/*
11320Sstevel@tonic-gate 		 * Free instance number. This is a no-op if instance has
11330Sstevel@tonic-gate 		 * been kept by probe_node().  Avoid free when we are called
11340Sstevel@tonic-gate 		 * from init_node (DS_BOUND) because the instance has not yet
11350Sstevel@tonic-gate 		 * been assigned.
11360Sstevel@tonic-gate 		 */
11370Sstevel@tonic-gate 		if (node_state_entry == DS_INITIALIZED) {
11380Sstevel@tonic-gate 			e_ddi_free_instance(dip, addr);
11390Sstevel@tonic-gate 			DEVI(dip)->devi_instance = -1;
11400Sstevel@tonic-gate 		}
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 		/* release the init_node hold */
11430Sstevel@tonic-gate 		ndi_rele_devi(pdip);
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 		remove_global_props(dip);
11464145Scth 
11474145Scth 		/*
11484145Scth 		 * NOTE: The decision on whether to allow a path-oriented
11494145Scth 		 * rebind of a driver.conf enumerated node is made by
11504145Scth 		 * init_node() based on driver_conf_allow_path_alias. The
11514145Scth 		 * rebind code below prevents deletion of system properties
11524145Scth 		 * on driver.conf nodes.
11534145Scth 		 *
11544145Scth 		 * When driver_conf_allow_path_alias is set, property behavior
11554145Scth 		 * on rebound driver.conf file is non-intuitive. For a
11564145Scth 		 * driver.conf node, the unit-address properties come from
11574145Scth 		 * the driver.conf file as system properties. Removing system
11584145Scth 		 * properties from a driver.conf node makes the node
11594145Scth 		 * useless (we get node without unit-address properties) - so
11604145Scth 		 * we leave system properties in place. The result is a node
11614145Scth 		 * where system properties come from the node being rebound,
11624145Scth 		 * and global properties come from the driver.conf file
11634145Scth 		 * of the driver we are rebinding to.  If we could determine
11644145Scth 		 * that the path-oriented alias driver.conf file defined a
11654145Scth 		 * node at the same unit address, it would be best to use
11664145Scth 		 * that node and avoid the non-intuitive property behavior.
11674145Scth 		 * Unfortunately, the current "merge" code does not support
11684145Scth 		 * this, so we live with the non-intuitive property behavior.
11694145Scth 		 */
11704145Scth 		if (!((ndi_dev_is_persistent_node(dip) == 0) &&
11714145Scth 		    (DEVI(dip)->devi_flags & DEVI_REBIND)))
11724145Scth 			e_ddi_prop_remove_all(dip);
11730Sstevel@tonic-gate 	} else {
11740Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "uninit_node failed: 0x%p(%s%d)\n",
11750Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
11760Sstevel@tonic-gate 	}
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 	if (addr)
11790Sstevel@tonic-gate 		kmem_free(addr, strlen(addr) + 1);
11800Sstevel@tonic-gate 	return (error);
11810Sstevel@tonic-gate }
11820Sstevel@tonic-gate 
11830Sstevel@tonic-gate /*
11840Sstevel@tonic-gate  * Invoke driver's probe entry point to probe for existence of hardware.
11850Sstevel@tonic-gate  * Keep instance permanent for successful probe and leaf nodes.
11860Sstevel@tonic-gate  *
11870Sstevel@tonic-gate  * Per-driver list must be held busy while calling this function.
11880Sstevel@tonic-gate  */
11890Sstevel@tonic-gate static int
probe_node(dev_info_t * dip)11900Sstevel@tonic-gate probe_node(dev_info_t *dip)
11910Sstevel@tonic-gate {
11920Sstevel@tonic-gate 	int rv;
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_INITIALIZED);
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "probe_node: 0x%p(%s%d)\n",
11970Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 	/* temporarily hold the driver while we probe */
12000Sstevel@tonic-gate 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
12010Sstevel@tonic-gate 	if (DEVI(dip)->devi_ops == NULL) {
12020Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
12030Sstevel@tonic-gate 		    "probe_node: 0x%p(%s%d) cannot load driver\n",
12040Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
12050Sstevel@tonic-gate 		return (DDI_FAILURE);
12060Sstevel@tonic-gate 	}
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 	if (identify_9e != 0)
12090Sstevel@tonic-gate 		(void) devi_identify(dip);
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 	rv = devi_probe(dip);
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 	/* release the driver now that probe is complete */
12140Sstevel@tonic-gate 	ndi_rele_driver(dip);
12150Sstevel@tonic-gate 	DEVI(dip)->devi_ops = NULL;
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate 	switch (rv) {
12180Sstevel@tonic-gate 	case DDI_PROBE_SUCCESS:			/* found */
12190Sstevel@tonic-gate 	case DDI_PROBE_DONTCARE:		/* ddi_dev_is_sid */
12200Sstevel@tonic-gate 		e_ddi_keep_instance(dip);	/* persist instance */
12210Sstevel@tonic-gate 		rv = DDI_SUCCESS;
12220Sstevel@tonic-gate 		break;
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate 	case DDI_PROBE_PARTIAL:			/* maybe later */
12250Sstevel@tonic-gate 	case DDI_PROBE_FAILURE:			/* not found */
12260Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
12270Sstevel@tonic-gate 		    "probe_node: 0x%p(%s%d) no hardware found%s\n",
12280Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip),
12290Sstevel@tonic-gate 		    (rv == DDI_PROBE_PARTIAL) ? " yet" : ""));
12300Sstevel@tonic-gate 		rv = DDI_FAILURE;
12310Sstevel@tonic-gate 		break;
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 	default:
12340Sstevel@tonic-gate #ifdef	DEBUG
12350Sstevel@tonic-gate 		cmn_err(CE_WARN, "probe_node: %s%d: illegal probe(9E) value",
12360Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
12370Sstevel@tonic-gate #endif	/* DEBUG */
12380Sstevel@tonic-gate 		rv = DDI_FAILURE;
12390Sstevel@tonic-gate 		break;
12400Sstevel@tonic-gate 	}
12410Sstevel@tonic-gate 	return (rv);
12420Sstevel@tonic-gate }
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate /*
12450Sstevel@tonic-gate  * Unprobe a node. Simply reset the node state.
12460Sstevel@tonic-gate  * Per-driver list must be held busy while calling this function.
12470Sstevel@tonic-gate  */
12480Sstevel@tonic-gate static int
unprobe_node(dev_info_t * dip)12490Sstevel@tonic-gate unprobe_node(dev_info_t *dip)
12500Sstevel@tonic-gate {
12510Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_PROBED);
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate 	/*
12540Sstevel@tonic-gate 	 * Don't check for references here or else a ref-counted
12550Sstevel@tonic-gate 	 * dip cannot be downgraded by the framework.
12560Sstevel@tonic-gate 	 */
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "unprobe_node: 0x%p(name = %s)\n",
12590Sstevel@tonic-gate 	    (void *)dip, ddi_node_name(dip)));
12600Sstevel@tonic-gate 	return (DDI_SUCCESS);
12610Sstevel@tonic-gate }
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate /*
12640Sstevel@tonic-gate  * Attach devinfo node.
12650Sstevel@tonic-gate  * Per-driver list must be held busy.
12660Sstevel@tonic-gate  */
12670Sstevel@tonic-gate static int
attach_node(dev_info_t * dip)12680Sstevel@tonic-gate attach_node(dev_info_t *dip)
12690Sstevel@tonic-gate {
12700Sstevel@tonic-gate 	int rv;
12710Sstevel@tonic-gate 
12722155Scth 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
12730Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_PROBED);
12740Sstevel@tonic-gate 
12750Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d)\n",
12760Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 	/*
12790Sstevel@tonic-gate 	 * Tell mpxio framework that a node is about to online.
12800Sstevel@tonic-gate 	 */
12810Sstevel@tonic-gate 	if ((rv = mdi_devi_online(dip, 0)) != NDI_SUCCESS) {
12820Sstevel@tonic-gate 		return (DDI_FAILURE);
12830Sstevel@tonic-gate 	}
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 	/* no recursive attachment */
12860Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_ops == NULL);
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 	/*
12890Sstevel@tonic-gate 	 * Hold driver the node is bound to.
12900Sstevel@tonic-gate 	 */
12910Sstevel@tonic-gate 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
12920Sstevel@tonic-gate 	if (DEVI(dip)->devi_ops == NULL) {
12930Sstevel@tonic-gate 		/*
12940Sstevel@tonic-gate 		 * We were able to load driver for probing, so we should
12950Sstevel@tonic-gate 		 * not get here unless something really bad happened.
12960Sstevel@tonic-gate 		 */
12970Sstevel@tonic-gate 		cmn_err(CE_WARN, "attach_node: no driver for major %d",
12980Sstevel@tonic-gate 		    DEVI(dip)->devi_major);
12990Sstevel@tonic-gate 		return (DDI_FAILURE);
13000Sstevel@tonic-gate 	}
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 	if (NEXUS_DRV(DEVI(dip)->devi_ops))
13030Sstevel@tonic-gate 		DEVI(dip)->devi_taskq = ddi_taskq_create(dip,
13040Sstevel@tonic-gate 		    "nexus_enum_tq", 1,
13050Sstevel@tonic-gate 		    TASKQ_DEFAULTPRI, 0);
13060Sstevel@tonic-gate 
1307495Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
13082009Sdm120769 	DEVI_SET_ATTACHING(dip);
13090Sstevel@tonic-gate 	DEVI_SET_NEED_RESET(dip);
1310495Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
1311495Scth 
13120Sstevel@tonic-gate 	rv = devi_attach(dip, DDI_ATTACH);
1313495Scth 
13142009Sdm120769 	mutex_enter(&(DEVI(dip)->devi_lock));
13152009Sdm120769 	DEVI_CLR_ATTACHING(dip);
13162009Sdm120769 
13171961Scth 	if (rv != DDI_SUCCESS) {
13182155Scth 		DEVI_CLR_NEED_RESET(dip);
13199066SPrasad.Singamsetty@Sun.COM 		mutex_exit(&DEVI(dip)->devi_lock);
1320438Scth 
13210Sstevel@tonic-gate 		/*
13220Sstevel@tonic-gate 		 * Cleanup dacf reservations
13230Sstevel@tonic-gate 		 */
13240Sstevel@tonic-gate 		mutex_enter(&dacf_lock);
13250Sstevel@tonic-gate 		dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH);
13260Sstevel@tonic-gate 		dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH);
13270Sstevel@tonic-gate 		mutex_exit(&dacf_lock);
13280Sstevel@tonic-gate 		if (DEVI(dip)->devi_taskq)
13290Sstevel@tonic-gate 			ddi_taskq_destroy(DEVI(dip)->devi_taskq);
13300Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate 		/* release the driver if attach failed */
13330Sstevel@tonic-gate 		ndi_rele_driver(dip);
13340Sstevel@tonic-gate 		DEVI(dip)->devi_ops = NULL;
13350Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d) failed\n",
13360Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
13370Sstevel@tonic-gate 		return (DDI_FAILURE);
13382009Sdm120769 	} else
13392009Sdm120769 		mutex_exit(&DEVI(dip)->devi_lock);
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate 	/* successful attach, return with driver held */
1342495Scth 
13430Sstevel@tonic-gate 	return (DDI_SUCCESS);
13440Sstevel@tonic-gate }
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate /*
13470Sstevel@tonic-gate  * Detach devinfo node.
13480Sstevel@tonic-gate  * Per-driver list must be held busy.
13490Sstevel@tonic-gate  */
13500Sstevel@tonic-gate static int
detach_node(dev_info_t * dip,uint_t flag)13510Sstevel@tonic-gate detach_node(dev_info_t *dip, uint_t flag)
13520Sstevel@tonic-gate {
135353Scth 	struct devnames	*dnp;
135453Scth 	int		rv;
135553Scth 
13562155Scth 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
13570Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_ATTACHED);
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate 	/* check references */
13600Sstevel@tonic-gate 	if (DEVI(dip)->devi_ref)
13610Sstevel@tonic-gate 		return (DDI_FAILURE);
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "detach_node: 0x%p(%s%d)\n",
13640Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
13650Sstevel@tonic-gate 
13662155Scth 	/*
13672155Scth 	 * NOTE: If we are processing a pHCI node then the calling code
13682155Scth 	 * must detect this and ndi_devi_enter() in (vHCI, parent(pHCI))
13692155Scth 	 * order unless pHCI and vHCI are siblings.  Code paths leading
13702155Scth 	 * here that must ensure this ordering include:
13712155Scth 	 * unconfig_immediate_children(), devi_unconfig_one(),
13722155Scth 	 * ndi_devi_unconfig_one(), ndi_devi_offline().
13732155Scth 	 */
13742155Scth 	ASSERT(!MDI_PHCI(dip) ||
13752155Scth 	    (ddi_get_parent(mdi_devi_get_vdip(dip)) == ddi_get_parent(dip)) ||
13762155Scth 	    DEVI_BUSY_OWNED(mdi_devi_get_vdip(dip)));
13772155Scth 
13780Sstevel@tonic-gate 	/* Offline the device node with the mpxio framework. */
13790Sstevel@tonic-gate 	if (mdi_devi_offline(dip, flag) != NDI_SUCCESS) {
13800Sstevel@tonic-gate 		return (DDI_FAILURE);
13810Sstevel@tonic-gate 	}
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate 	/* drain the taskq */
13840Sstevel@tonic-gate 	if (DEVI(dip)->devi_taskq)
13850Sstevel@tonic-gate 		ddi_taskq_wait(DEVI(dip)->devi_taskq);
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate 	rv = devi_detach(dip, DDI_DETACH);
13880Sstevel@tonic-gate 
13890Sstevel@tonic-gate 	if (rv != DDI_SUCCESS) {
13900Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
13910Sstevel@tonic-gate 		    "detach_node: 0x%p(%s%d) failed\n",
13920Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
13930Sstevel@tonic-gate 		return (DDI_FAILURE);
13940Sstevel@tonic-gate 	}
13950Sstevel@tonic-gate 
13962155Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
13972155Scth 	DEVI_CLR_NEED_RESET(dip);
13982155Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
13992155Scth 
140011600SVikram.Hegde@Sun.COM #if defined(__amd64) && !defined(__xpv)
14018249SVikram.Hegde@Sun.COM 	/*
14028249SVikram.Hegde@Sun.COM 	 * Close any iommulib mediated linkage to an IOMMU
14038249SVikram.Hegde@Sun.COM 	 */
1404*13050Sfrank.van.der.linden@oracle.com 	if (IOMMU_USED(dip))
1405*13050Sfrank.van.der.linden@oracle.com 		iommulib_nex_close(dip);
14068249SVikram.Hegde@Sun.COM #endif
14078249SVikram.Hegde@Sun.COM 
14080Sstevel@tonic-gate 	/* destroy the taskq */
14090Sstevel@tonic-gate 	if (DEVI(dip)->devi_taskq) {
14100Sstevel@tonic-gate 		ddi_taskq_destroy(DEVI(dip)->devi_taskq);
14110Sstevel@tonic-gate 		DEVI(dip)->devi_taskq = NULL;
14120Sstevel@tonic-gate 	}
14130Sstevel@tonic-gate 
14140Sstevel@tonic-gate 	/* Cleanup dacf reservations */
14150Sstevel@tonic-gate 	mutex_enter(&dacf_lock);
14160Sstevel@tonic-gate 	dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH);
14170Sstevel@tonic-gate 	dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH);
14180Sstevel@tonic-gate 	mutex_exit(&dacf_lock);
14190Sstevel@tonic-gate 
14208860SMatthew.Jacob@Sun.COM 	/* remove any additional flavors that were added */
14218860SMatthew.Jacob@Sun.COM 	if (DEVI(dip)->devi_flavorv_n > 1 && DEVI(dip)->devi_flavorv != NULL) {
14228860SMatthew.Jacob@Sun.COM 		kmem_free(DEVI(dip)->devi_flavorv,
14238860SMatthew.Jacob@Sun.COM 		    (DEVI(dip)->devi_flavorv_n - 1) * sizeof (void *));
14248860SMatthew.Jacob@Sun.COM 		DEVI(dip)->devi_flavorv = NULL;
14258860SMatthew.Jacob@Sun.COM 	}
14268860SMatthew.Jacob@Sun.COM 
14270Sstevel@tonic-gate 	/* Remove properties and minor nodes in case driver forgots */
14280Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
14290Sstevel@tonic-gate 	ddi_prop_remove_all(dip);
14300Sstevel@tonic-gate 
14310Sstevel@tonic-gate 	/* a detached node can't have attached or .conf children */
14320Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
14330Sstevel@tonic-gate 	DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN|DEVI_ATTACHED_CHILDREN);
14349066SPrasad.Singamsetty@Sun.COM 	mutex_exit(&DEVI(dip)->devi_lock);
14350Sstevel@tonic-gate 
143653Scth 	/*
143753Scth 	 * If the instance has successfully detached in detach_driver() context,
143853Scth 	 * clear DN_DRIVER_HELD for correct ddi_hold_installed_driver()
143953Scth 	 * behavior. Consumers like qassociate() depend on this (via clnopen()).
144053Scth 	 */
144153Scth 	if (flag & NDI_DETACH_DRIVER) {
144253Scth 		dnp = &(devnamesp[DEVI(dip)->devi_major]);
144353Scth 		LOCK_DEV_OPS(&dnp->dn_lock);
144453Scth 		dnp->dn_flags &= ~DN_DRIVER_HELD;
144553Scth 		UNLOCK_DEV_OPS(&dnp->dn_lock);
144653Scth 	}
144753Scth 
14480Sstevel@tonic-gate 	/* successful detach, release the driver */
14490Sstevel@tonic-gate 	ndi_rele_driver(dip);
14500Sstevel@tonic-gate 	DEVI(dip)->devi_ops = NULL;
14510Sstevel@tonic-gate 	return (DDI_SUCCESS);
14520Sstevel@tonic-gate }
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate /*
14550Sstevel@tonic-gate  * Run dacf post_attach routines
14560Sstevel@tonic-gate  */
14570Sstevel@tonic-gate static int
postattach_node(dev_info_t * dip)14580Sstevel@tonic-gate postattach_node(dev_info_t *dip)
14590Sstevel@tonic-gate {
14600Sstevel@tonic-gate 	int rval;
14610Sstevel@tonic-gate 
14620Sstevel@tonic-gate 	/*
14630Sstevel@tonic-gate 	 * For hotplug busses like USB, it's possible that devices
14640Sstevel@tonic-gate 	 * are removed but dip is still around. We don't want to
14650Sstevel@tonic-gate 	 * run dacf routines as part of detach failure recovery.
14660Sstevel@tonic-gate 	 *
14670Sstevel@tonic-gate 	 * Pretend success until we figure out how to prevent
14680Sstevel@tonic-gate 	 * access to such devinfo nodes.
14690Sstevel@tonic-gate 	 */
14700Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_REMOVED(dip))
14710Sstevel@tonic-gate 		return (DDI_SUCCESS);
14720Sstevel@tonic-gate 
14730Sstevel@tonic-gate 	/*
14740Sstevel@tonic-gate 	 * if dacf_postattach failed, report it to the framework
14750Sstevel@tonic-gate 	 * so that it can be retried later at the open time.
14760Sstevel@tonic-gate 	 */
14770Sstevel@tonic-gate 	mutex_enter(&dacf_lock);
14780Sstevel@tonic-gate 	rval = dacfc_postattach(dip);
14790Sstevel@tonic-gate 	mutex_exit(&dacf_lock);
14800Sstevel@tonic-gate 
14810Sstevel@tonic-gate 	/*
14820Sstevel@tonic-gate 	 * Plumbing during postattach may fail because of the
14830Sstevel@tonic-gate 	 * underlying device is not ready. This will fail ndi_devi_config()
14840Sstevel@tonic-gate 	 * in dv_filldir() and a warning message is issued. The message
14850Sstevel@tonic-gate 	 * from here will explain what happened
14860Sstevel@tonic-gate 	 */
14870Sstevel@tonic-gate 	if (rval != DACF_SUCCESS) {
14880Sstevel@tonic-gate 		cmn_err(CE_WARN, "Postattach failed for %s%d\n",
14890Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
14900Sstevel@tonic-gate 		return (DDI_FAILURE);
14910Sstevel@tonic-gate 	}
14920Sstevel@tonic-gate 
14930Sstevel@tonic-gate 	return (DDI_SUCCESS);
14940Sstevel@tonic-gate }
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate /*
14970Sstevel@tonic-gate  * Run dacf pre-detach routines
14980Sstevel@tonic-gate  */
14990Sstevel@tonic-gate static int
predetach_node(dev_info_t * dip,uint_t flag)15000Sstevel@tonic-gate predetach_node(dev_info_t *dip, uint_t flag)
15010Sstevel@tonic-gate {
15020Sstevel@tonic-gate 	int ret;
15030Sstevel@tonic-gate 
15040Sstevel@tonic-gate 	/*
15050Sstevel@tonic-gate 	 * Don't auto-detach if DDI_FORCEATTACH or DDI_NO_AUTODETACH
15060Sstevel@tonic-gate 	 * properties are set.
15070Sstevel@tonic-gate 	 */
15080Sstevel@tonic-gate 	if (flag & NDI_AUTODETACH) {
15090Sstevel@tonic-gate 		struct devnames *dnp;
15100Sstevel@tonic-gate 		int pflag = DDI_PROP_NOTPROM | DDI_PROP_DONTPASS;
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate 		if ((ddi_prop_get_int(DDI_DEV_T_ANY, dip,
15134950Scth 		    pflag, DDI_FORCEATTACH, 0) == 1) ||
15140Sstevel@tonic-gate 		    (ddi_prop_get_int(DDI_DEV_T_ANY, dip,
15154950Scth 		    pflag, DDI_NO_AUTODETACH, 0) == 1))
15160Sstevel@tonic-gate 			return (DDI_FAILURE);
15170Sstevel@tonic-gate 
15180Sstevel@tonic-gate 		/* check for driver global version of DDI_NO_AUTODETACH */
15190Sstevel@tonic-gate 		dnp = &devnamesp[DEVI(dip)->devi_major];
15200Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
15210Sstevel@tonic-gate 		if (dnp->dn_flags & DN_NO_AUTODETACH) {
15220Sstevel@tonic-gate 			UNLOCK_DEV_OPS(&dnp->dn_lock);
15230Sstevel@tonic-gate 			return (DDI_FAILURE);
15240Sstevel@tonic-gate 		}
15250Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
15260Sstevel@tonic-gate 	}
15270Sstevel@tonic-gate 
15280Sstevel@tonic-gate 	mutex_enter(&dacf_lock);
15290Sstevel@tonic-gate 	ret = dacfc_predetach(dip);
15300Sstevel@tonic-gate 	mutex_exit(&dacf_lock);
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 	return (ret);
15330Sstevel@tonic-gate }
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate /*
15360Sstevel@tonic-gate  * Wrapper for making multiple state transitions
15370Sstevel@tonic-gate  */
15380Sstevel@tonic-gate 
15390Sstevel@tonic-gate /*
15400Sstevel@tonic-gate  * i_ndi_config_node: upgrade dev_info node into a specified state.
15410Sstevel@tonic-gate  * It is a bit tricky because the locking protocol changes before and
15420Sstevel@tonic-gate  * after a node is bound to a driver. All locks are held external to
15430Sstevel@tonic-gate  * this function.
15440Sstevel@tonic-gate  */
15450Sstevel@tonic-gate int
i_ndi_config_node(dev_info_t * dip,ddi_node_state_t state,uint_t flag)15460Sstevel@tonic-gate i_ndi_config_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag)
15470Sstevel@tonic-gate {
15480Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flag))
15490Sstevel@tonic-gate 	int rv = DDI_SUCCESS;
15500Sstevel@tonic-gate 
15510Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate 	while ((i_ddi_node_state(dip) < state) && (rv == DDI_SUCCESS)) {
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate 		/* don't allow any more changes to the device tree */
15560Sstevel@tonic-gate 		if (devinfo_freeze) {
15570Sstevel@tonic-gate 			rv = DDI_FAILURE;
15580Sstevel@tonic-gate 			break;
15590Sstevel@tonic-gate 		}
15600Sstevel@tonic-gate 
15610Sstevel@tonic-gate 		switch (i_ddi_node_state(dip)) {
15620Sstevel@tonic-gate 		case DS_PROTO:
15630Sstevel@tonic-gate 			/*
15640Sstevel@tonic-gate 			 * only caller can reference this node, no external
15650Sstevel@tonic-gate 			 * locking needed.
15660Sstevel@tonic-gate 			 */
15670Sstevel@tonic-gate 			link_node(dip);
156811596SJason.Beloro@Sun.COM 			translate_devid((dev_info_t *)dip);
15690Sstevel@tonic-gate 			i_ddi_set_node_state(dip, DS_LINKED);
15700Sstevel@tonic-gate 			break;
15710Sstevel@tonic-gate 		case DS_LINKED:
15720Sstevel@tonic-gate 			/*
15730Sstevel@tonic-gate 			 * Three code path may attempt to bind a node:
15740Sstevel@tonic-gate 			 * - boot code
15750Sstevel@tonic-gate 			 * - add_drv
15760Sstevel@tonic-gate 			 * - hotplug thread
15770Sstevel@tonic-gate 			 * Boot code is single threaded, add_drv synchronize
15780Sstevel@tonic-gate 			 * on a userland lock, and hotplug synchronize on
15790Sstevel@tonic-gate 			 * hotplug_lk. There could be a race between add_drv
15800Sstevel@tonic-gate 			 * and hotplug thread. We'll live with this until the
15810Sstevel@tonic-gate 			 * conversion to top-down loading.
15820Sstevel@tonic-gate 			 */
15830Sstevel@tonic-gate 			if ((rv = bind_node(dip)) == DDI_SUCCESS)
15840Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_BOUND);
15854145Scth 
15860Sstevel@tonic-gate 			break;
15870Sstevel@tonic-gate 		case DS_BOUND:
15880Sstevel@tonic-gate 			/*
15890Sstevel@tonic-gate 			 * The following transitions synchronizes on the
15900Sstevel@tonic-gate 			 * per-driver busy changing flag, since we already
15910Sstevel@tonic-gate 			 * have a driver.
15920Sstevel@tonic-gate 			 */
15930Sstevel@tonic-gate 			if ((rv = init_node(dip)) == DDI_SUCCESS)
15940Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_INITIALIZED);
15950Sstevel@tonic-gate 			break;
15960Sstevel@tonic-gate 		case DS_INITIALIZED:
15970Sstevel@tonic-gate 			if ((rv = probe_node(dip)) == DDI_SUCCESS)
15980Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_PROBED);
15990Sstevel@tonic-gate 			break;
16000Sstevel@tonic-gate 		case DS_PROBED:
160112618SStephen.Hanson@Sun.COM 			/*
160212618SStephen.Hanson@Sun.COM 			 * If node is retired and persistent, then prevent
160312618SStephen.Hanson@Sun.COM 			 * attach. We can't do this for non-persistent nodes
160412618SStephen.Hanson@Sun.COM 			 * as we would lose evidence that the node existed.
160512618SStephen.Hanson@Sun.COM 			 */
160612618SStephen.Hanson@Sun.COM 			if (i_ddi_check_retire(dip) == 1 &&
160712618SStephen.Hanson@Sun.COM 			    ndi_dev_is_persistent_node(dip) &&
160812618SStephen.Hanson@Sun.COM 			    retire_prevents_attach == 1) {
160912618SStephen.Hanson@Sun.COM 				rv = DDI_FAILURE;
161012618SStephen.Hanson@Sun.COM 				break;
161112618SStephen.Hanson@Sun.COM 			}
16120Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, 1);
16130Sstevel@tonic-gate 			if ((rv = attach_node(dip)) == DDI_SUCCESS)
16140Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_ATTACHED);
16150Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, -1);
16160Sstevel@tonic-gate 			break;
16170Sstevel@tonic-gate 		case DS_ATTACHED:
16180Sstevel@tonic-gate 			if ((rv = postattach_node(dip)) == DDI_SUCCESS)
16190Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_READY);
16200Sstevel@tonic-gate 			break;
16210Sstevel@tonic-gate 		case DS_READY:
16220Sstevel@tonic-gate 			break;
16230Sstevel@tonic-gate 		default:
16240Sstevel@tonic-gate 			/* should never reach here */
16250Sstevel@tonic-gate 			ASSERT("unknown devinfo state");
16260Sstevel@tonic-gate 		}
16270Sstevel@tonic-gate 	}
16280Sstevel@tonic-gate 
16290Sstevel@tonic-gate 	if (ddidebug & DDI_AUDIT)
16300Sstevel@tonic-gate 		da_log_enter(dip);
16310Sstevel@tonic-gate 	return (rv);
16320Sstevel@tonic-gate }
16330Sstevel@tonic-gate 
16340Sstevel@tonic-gate /*
16350Sstevel@tonic-gate  * i_ndi_unconfig_node: downgrade dev_info node into a specified state.
16360Sstevel@tonic-gate  */
16370Sstevel@tonic-gate int
i_ndi_unconfig_node(dev_info_t * dip,ddi_node_state_t state,uint_t flag)16380Sstevel@tonic-gate i_ndi_unconfig_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag)
16390Sstevel@tonic-gate {
16404950Scth 	int	rv = DDI_SUCCESS;
16410Sstevel@tonic-gate 
16420Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
16430Sstevel@tonic-gate 
16440Sstevel@tonic-gate 	while ((i_ddi_node_state(dip) > state) && (rv == DDI_SUCCESS)) {
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate 		/* don't allow any more changes to the device tree */
16470Sstevel@tonic-gate 		if (devinfo_freeze) {
16480Sstevel@tonic-gate 			rv = DDI_FAILURE;
16490Sstevel@tonic-gate 			break;
16500Sstevel@tonic-gate 		}
16510Sstevel@tonic-gate 
16520Sstevel@tonic-gate 		switch (i_ddi_node_state(dip)) {
16530Sstevel@tonic-gate 		case DS_PROTO:
16540Sstevel@tonic-gate 			break;
16550Sstevel@tonic-gate 		case DS_LINKED:
16560Sstevel@tonic-gate 			/*
16570Sstevel@tonic-gate 			 * Persistent nodes are only removed by hotplug code
16580Sstevel@tonic-gate 			 * .conf nodes synchronizes on per-driver list.
16590Sstevel@tonic-gate 			 */
16600Sstevel@tonic-gate 			if ((rv = unlink_node(dip)) == DDI_SUCCESS)
16610Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_PROTO);
16620Sstevel@tonic-gate 			break;
16630Sstevel@tonic-gate 		case DS_BOUND:
16640Sstevel@tonic-gate 			/*
16650Sstevel@tonic-gate 			 * The following transitions synchronizes on the
16660Sstevel@tonic-gate 			 * per-driver busy changing flag, since we already
16670Sstevel@tonic-gate 			 * have a driver.
16680Sstevel@tonic-gate 			 */
16690Sstevel@tonic-gate 			if ((rv = unbind_node(dip)) == DDI_SUCCESS)
16700Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_LINKED);
16710Sstevel@tonic-gate 			break;
16720Sstevel@tonic-gate 		case DS_INITIALIZED:
16730Sstevel@tonic-gate 			if ((rv = uninit_node(dip)) == DDI_SUCCESS)
16740Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_BOUND);
16750Sstevel@tonic-gate 			break;
16760Sstevel@tonic-gate 		case DS_PROBED:
16770Sstevel@tonic-gate 			if ((rv = unprobe_node(dip)) == DDI_SUCCESS)
16780Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_INITIALIZED);
16790Sstevel@tonic-gate 			break;
16800Sstevel@tonic-gate 		case DS_ATTACHED:
16810Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, 1);
1682495Scth 
1683495Scth 			mutex_enter(&(DEVI(dip)->devi_lock));
16840Sstevel@tonic-gate 			DEVI_SET_DETACHING(dip);
1685495Scth 			mutex_exit(&(DEVI(dip)->devi_lock));
1686495Scth 
16870Sstevel@tonic-gate 			membar_enter();	/* ensure visibility for hold_devi */
16880Sstevel@tonic-gate 
16890Sstevel@tonic-gate 			if ((rv = detach_node(dip, flag)) == DDI_SUCCESS)
16900Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_PROBED);
1691495Scth 
1692495Scth 			mutex_enter(&(DEVI(dip)->devi_lock));
16930Sstevel@tonic-gate 			DEVI_CLR_DETACHING(dip);
1694495Scth 			mutex_exit(&(DEVI(dip)->devi_lock));
1695495Scth 
16960Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, -1);
16970Sstevel@tonic-gate 			break;
16980Sstevel@tonic-gate 		case DS_READY:
16990Sstevel@tonic-gate 			if ((rv = predetach_node(dip, flag)) == DDI_SUCCESS)
17000Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_ATTACHED);
17010Sstevel@tonic-gate 			break;
17020Sstevel@tonic-gate 		default:
17030Sstevel@tonic-gate 			ASSERT("unknown devinfo state");
17040Sstevel@tonic-gate 		}
17050Sstevel@tonic-gate 	}
17060Sstevel@tonic-gate 	da_log_enter(dip);
17070Sstevel@tonic-gate 	return (rv);
17080Sstevel@tonic-gate }
17090Sstevel@tonic-gate 
17100Sstevel@tonic-gate /*
17110Sstevel@tonic-gate  * ddi_initchild: transform node to DS_INITIALIZED state
17120Sstevel@tonic-gate  */
17130Sstevel@tonic-gate int
ddi_initchild(dev_info_t * parent,dev_info_t * proto)17140Sstevel@tonic-gate ddi_initchild(dev_info_t *parent, dev_info_t *proto)
17150Sstevel@tonic-gate {
17160Sstevel@tonic-gate 	int ret, circ;
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
17190Sstevel@tonic-gate 	ret = i_ndi_config_node(proto, DS_INITIALIZED, 0);
17200Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
17210Sstevel@tonic-gate 
17220Sstevel@tonic-gate 	return (ret);
17230Sstevel@tonic-gate }
17240Sstevel@tonic-gate 
17250Sstevel@tonic-gate /*
17260Sstevel@tonic-gate  * ddi_uninitchild: transform node down to DS_BOUND state
17270Sstevel@tonic-gate  */
17280Sstevel@tonic-gate int
ddi_uninitchild(dev_info_t * dip)17290Sstevel@tonic-gate ddi_uninitchild(dev_info_t *dip)
17300Sstevel@tonic-gate {
17310Sstevel@tonic-gate 	int ret, circ;
17320Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
17330Sstevel@tonic-gate 	ASSERT(parent);
17340Sstevel@tonic-gate 
17350Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
17360Sstevel@tonic-gate 	ret = i_ndi_unconfig_node(dip, DS_BOUND, 0);
17370Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
17380Sstevel@tonic-gate 
17390Sstevel@tonic-gate 	return (ret);
17400Sstevel@tonic-gate }
17410Sstevel@tonic-gate 
17420Sstevel@tonic-gate /*
17431333Scth  * i_ddi_attachchild: transform node to DS_READY/i_ddi_devi_attached() state
17440Sstevel@tonic-gate  */
17450Sstevel@tonic-gate static int
i_ddi_attachchild(dev_info_t * dip)17460Sstevel@tonic-gate i_ddi_attachchild(dev_info_t *dip)
17470Sstevel@tonic-gate {
17482155Scth 	dev_info_t	*parent = ddi_get_parent(dip);
17492155Scth 	int		ret;
17502155Scth 
17512155Scth 	ASSERT(parent && DEVI_BUSY_OWNED(parent));
17520Sstevel@tonic-gate 
17530Sstevel@tonic-gate 	if ((i_ddi_node_state(dip) < DS_BOUND) || DEVI_IS_DEVICE_OFFLINE(dip))
17540Sstevel@tonic-gate 		return (DDI_FAILURE);
17550Sstevel@tonic-gate 
17560Sstevel@tonic-gate 	ret = i_ndi_config_node(dip, DS_READY, 0);
17570Sstevel@tonic-gate 	if (ret == NDI_SUCCESS) {
17580Sstevel@tonic-gate 		ret = DDI_SUCCESS;
17590Sstevel@tonic-gate 	} else {
17600Sstevel@tonic-gate 		/*
17610Sstevel@tonic-gate 		 * Take it down to DS_INITIALIZED so pm_pre_probe is run
17620Sstevel@tonic-gate 		 * on the next attach
17630Sstevel@tonic-gate 		 */
17640Sstevel@tonic-gate 		(void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0);
17650Sstevel@tonic-gate 		ret = DDI_FAILURE;
17660Sstevel@tonic-gate 	}
17670Sstevel@tonic-gate 
17680Sstevel@tonic-gate 	return (ret);
17690Sstevel@tonic-gate }
17700Sstevel@tonic-gate 
17710Sstevel@tonic-gate /*
17720Sstevel@tonic-gate  * i_ddi_detachchild: transform node down to DS_PROBED state
17730Sstevel@tonic-gate  *	If it fails, put it back to DS_READY state.
17740Sstevel@tonic-gate  * NOTE: A node that fails detach may be at DS_ATTACHED instead
17751333Scth  * of DS_READY for a small amount of time - this is the source of
17761333Scth  * transient DS_READY->DS_ATTACHED->DS_READY state changes.
17770Sstevel@tonic-gate  */
17780Sstevel@tonic-gate static int
i_ddi_detachchild(dev_info_t * dip,uint_t flags)17790Sstevel@tonic-gate i_ddi_detachchild(dev_info_t *dip, uint_t flags)
17800Sstevel@tonic-gate {
17812155Scth 	dev_info_t	*parent = ddi_get_parent(dip);
17822155Scth 	int		ret;
17832155Scth 
17842155Scth 	ASSERT(parent && DEVI_BUSY_OWNED(parent));
17852155Scth 
17860Sstevel@tonic-gate 	ret = i_ndi_unconfig_node(dip, DS_PROBED, flags);
17870Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
17880Sstevel@tonic-gate 		(void) i_ndi_config_node(dip, DS_READY, 0);
17890Sstevel@tonic-gate 	else
17900Sstevel@tonic-gate 		/* allow pm_pre_probe to reestablish pm state */
17910Sstevel@tonic-gate 		(void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0);
17920Sstevel@tonic-gate 	return (ret);
17930Sstevel@tonic-gate }
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate /*
17960Sstevel@tonic-gate  * Add a child and bind to driver
17970Sstevel@tonic-gate  */
17980Sstevel@tonic-gate dev_info_t *
ddi_add_child(dev_info_t * pdip,char * name,uint_t nodeid,uint_t unit)17990Sstevel@tonic-gate ddi_add_child(dev_info_t *pdip, char *name, uint_t nodeid, uint_t unit)
18000Sstevel@tonic-gate {
18010Sstevel@tonic-gate 	int circ;
18020Sstevel@tonic-gate 	dev_info_t *dip;
18030Sstevel@tonic-gate 
18040Sstevel@tonic-gate 	/* allocate a new node */
18050Sstevel@tonic-gate 	dip = i_ddi_alloc_node(pdip, name, nodeid, (int)unit, NULL, KM_SLEEP);
18060Sstevel@tonic-gate 
18070Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
18080Sstevel@tonic-gate 	(void) i_ndi_config_node(dip, DS_BOUND, 0);
18090Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
18100Sstevel@tonic-gate 	return (dip);
18110Sstevel@tonic-gate }
18120Sstevel@tonic-gate 
18130Sstevel@tonic-gate /*
18140Sstevel@tonic-gate  * ddi_remove_child: remove the dip. The parent must be attached and held
18150Sstevel@tonic-gate  */
18160Sstevel@tonic-gate int
ddi_remove_child(dev_info_t * dip,int dummy)18170Sstevel@tonic-gate ddi_remove_child(dev_info_t *dip, int dummy)
18180Sstevel@tonic-gate {
18190Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dummy))
18200Sstevel@tonic-gate 	int circ, ret;
18210Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
18220Sstevel@tonic-gate 	ASSERT(parent);
18230Sstevel@tonic-gate 
18240Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
18250Sstevel@tonic-gate 
18260Sstevel@tonic-gate 	/*
18270Sstevel@tonic-gate 	 * If we still have children, for example SID nodes marked
18280Sstevel@tonic-gate 	 * as persistent but not attached, attempt to remove them.
18290Sstevel@tonic-gate 	 */
18300Sstevel@tonic-gate 	if (DEVI(dip)->devi_child) {
18310Sstevel@tonic-gate 		ret = ndi_devi_unconfig(dip, NDI_DEVI_REMOVE);
18320Sstevel@tonic-gate 		if (ret != NDI_SUCCESS) {
18330Sstevel@tonic-gate 			ndi_devi_exit(parent, circ);
18340Sstevel@tonic-gate 			return (DDI_FAILURE);
18350Sstevel@tonic-gate 		}
18360Sstevel@tonic-gate 		ASSERT(DEVI(dip)->devi_child == NULL);
18370Sstevel@tonic-gate 	}
18380Sstevel@tonic-gate 
18390Sstevel@tonic-gate 	ret = i_ndi_unconfig_node(dip, DS_PROTO, 0);
18400Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
18410Sstevel@tonic-gate 
18420Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
18430Sstevel@tonic-gate 		return (ret);
18440Sstevel@tonic-gate 
18450Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_PROTO);
18460Sstevel@tonic-gate 	i_ddi_free_node(dip);
18470Sstevel@tonic-gate 	return (DDI_SUCCESS);
18480Sstevel@tonic-gate }
18490Sstevel@tonic-gate 
18500Sstevel@tonic-gate /*
18510Sstevel@tonic-gate  * NDI wrappers for ref counting, node allocation, and transitions
18520Sstevel@tonic-gate  */
18530Sstevel@tonic-gate 
18540Sstevel@tonic-gate /*
18550Sstevel@tonic-gate  * Hold/release the devinfo node itself.
18560Sstevel@tonic-gate  * Caller is assumed to prevent the devi from detaching during this call
18570Sstevel@tonic-gate  */
18580Sstevel@tonic-gate void
ndi_hold_devi(dev_info_t * dip)18590Sstevel@tonic-gate ndi_hold_devi(dev_info_t *dip)
18600Sstevel@tonic-gate {
18610Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
18620Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_ref >= 0);
18630Sstevel@tonic-gate 	DEVI(dip)->devi_ref++;
18640Sstevel@tonic-gate 	membar_enter();			/* make sure stores are flushed */
18650Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
18660Sstevel@tonic-gate }
18670Sstevel@tonic-gate 
18680Sstevel@tonic-gate void
ndi_rele_devi(dev_info_t * dip)18690Sstevel@tonic-gate ndi_rele_devi(dev_info_t *dip)
18700Sstevel@tonic-gate {
18710Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_ref > 0);
18720Sstevel@tonic-gate 
18730Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
18740Sstevel@tonic-gate 	DEVI(dip)->devi_ref--;
18750Sstevel@tonic-gate 	membar_enter();			/* make sure stores are flushed */
18760Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
18770Sstevel@tonic-gate }
18780Sstevel@tonic-gate 
18790Sstevel@tonic-gate int
e_ddi_devi_holdcnt(dev_info_t * dip)18800Sstevel@tonic-gate e_ddi_devi_holdcnt(dev_info_t *dip)
18810Sstevel@tonic-gate {
18820Sstevel@tonic-gate 	return (DEVI(dip)->devi_ref);
18830Sstevel@tonic-gate }
18840Sstevel@tonic-gate 
18850Sstevel@tonic-gate /*
18860Sstevel@tonic-gate  * Hold/release the driver the devinfo node is bound to.
18870Sstevel@tonic-gate  */
18880Sstevel@tonic-gate struct dev_ops *
ndi_hold_driver(dev_info_t * dip)18890Sstevel@tonic-gate ndi_hold_driver(dev_info_t *dip)
18900Sstevel@tonic-gate {
18910Sstevel@tonic-gate 	if (i_ddi_node_state(dip) < DS_BOUND)
18920Sstevel@tonic-gate 		return (NULL);
18930Sstevel@tonic-gate 
18940Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_major != -1);
18950Sstevel@tonic-gate 	return (mod_hold_dev_by_major(DEVI(dip)->devi_major));
18960Sstevel@tonic-gate }
18970Sstevel@tonic-gate 
18980Sstevel@tonic-gate void
ndi_rele_driver(dev_info_t * dip)18990Sstevel@tonic-gate ndi_rele_driver(dev_info_t *dip)
19000Sstevel@tonic-gate {
19010Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) >= DS_BOUND);
19020Sstevel@tonic-gate 	mod_rele_dev_by_major(DEVI(dip)->devi_major);
19030Sstevel@tonic-gate }
19040Sstevel@tonic-gate 
19050Sstevel@tonic-gate /*
19067224Scth  * Single thread entry into devinfo node for modifying its children (devinfo,
19077224Scth  * pathinfo, and minor). To verify in ASSERTS use DEVI_BUSY_OWNED macro.
19080Sstevel@tonic-gate  */
19090Sstevel@tonic-gate void
ndi_devi_enter(dev_info_t * dip,int * circular)19100Sstevel@tonic-gate ndi_devi_enter(dev_info_t *dip, int *circular)
19110Sstevel@tonic-gate {
19120Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
19130Sstevel@tonic-gate 	ASSERT(dip != NULL);
19140Sstevel@tonic-gate 
19152155Scth 	/* for vHCI, enforce (vHCI, pHCI) ndi_deve_enter() order */
19162155Scth 	ASSERT(!MDI_VHCI(dip) || (mdi_devi_pdip_entered(dip) == 0) ||
19172155Scth 	    DEVI_BUSY_OWNED(dip));
19182155Scth 
19190Sstevel@tonic-gate 	mutex_enter(&devi->devi_lock);
19200Sstevel@tonic-gate 	if (devi->devi_busy_thread == curthread) {
19210Sstevel@tonic-gate 		devi->devi_circular++;
19220Sstevel@tonic-gate 	} else {
19230Sstevel@tonic-gate 		while (DEVI_BUSY_CHANGING(devi) && !panicstr)
19240Sstevel@tonic-gate 			cv_wait(&(devi->devi_cv), &(devi->devi_lock));
19250Sstevel@tonic-gate 		if (panicstr) {
19260Sstevel@tonic-gate 			mutex_exit(&devi->devi_lock);
19270Sstevel@tonic-gate 			return;
19280Sstevel@tonic-gate 		}
19290Sstevel@tonic-gate 		devi->devi_flags |= DEVI_BUSY;
19300Sstevel@tonic-gate 		devi->devi_busy_thread = curthread;
19310Sstevel@tonic-gate 	}
19320Sstevel@tonic-gate 	*circular = devi->devi_circular;
19330Sstevel@tonic-gate 	mutex_exit(&devi->devi_lock);
19340Sstevel@tonic-gate }
19350Sstevel@tonic-gate 
19360Sstevel@tonic-gate /*
19370Sstevel@tonic-gate  * Release ndi_devi_enter or successful ndi_devi_tryenter.
19380Sstevel@tonic-gate  */
19390Sstevel@tonic-gate void
ndi_devi_exit(dev_info_t * dip,int circular)19400Sstevel@tonic-gate ndi_devi_exit(dev_info_t *dip, int circular)
19410Sstevel@tonic-gate {
19422155Scth 	struct dev_info	*devi = DEVI(dip);
19432155Scth 	struct dev_info	*vdevi;
19440Sstevel@tonic-gate 	ASSERT(dip != NULL);
19450Sstevel@tonic-gate 
19460Sstevel@tonic-gate 	if (panicstr)
19470Sstevel@tonic-gate 		return;
19480Sstevel@tonic-gate 
19490Sstevel@tonic-gate 	mutex_enter(&(devi->devi_lock));
19500Sstevel@tonic-gate 	if (circular != 0) {
19510Sstevel@tonic-gate 		devi->devi_circular--;
19520Sstevel@tonic-gate 	} else {
19530Sstevel@tonic-gate 		devi->devi_flags &= ~DEVI_BUSY;
19540Sstevel@tonic-gate 		ASSERT(devi->devi_busy_thread == curthread);
19550Sstevel@tonic-gate 		devi->devi_busy_thread = NULL;
19560Sstevel@tonic-gate 		cv_broadcast(&(devi->devi_cv));
19570Sstevel@tonic-gate 	}
19580Sstevel@tonic-gate 	mutex_exit(&(devi->devi_lock));
19592155Scth 
19602155Scth 	/*
19612155Scth 	 * For pHCI exit we issue a broadcast to vHCI for ndi_devi_config_one()
19622155Scth 	 * doing cv_wait on vHCI.
19632155Scth 	 */
19642155Scth 	if (MDI_PHCI(dip)) {
19652155Scth 		vdevi = DEVI(mdi_devi_get_vdip(dip));
19662155Scth 		if (vdevi) {
19672155Scth 			mutex_enter(&(vdevi->devi_lock));
19682155Scth 			if (vdevi->devi_flags & DEVI_PHCI_SIGNALS_VHCI) {
19692155Scth 				vdevi->devi_flags &= ~DEVI_PHCI_SIGNALS_VHCI;
19702155Scth 				cv_broadcast(&(vdevi->devi_cv));
19712155Scth 			}
19722155Scth 			mutex_exit(&(vdevi->devi_lock));
19732155Scth 		}
19742155Scth 	}
19752155Scth }
19762155Scth 
19772155Scth /*
19782155Scth  * Release ndi_devi_enter and wait for possibility of new children, avoiding
19792155Scth  * possibility of missing broadcast before getting to cv_timedwait().
19802155Scth  */
19812155Scth static void
ndi_devi_exit_and_wait(dev_info_t * dip,int circular,clock_t end_time)19822155Scth ndi_devi_exit_and_wait(dev_info_t *dip, int circular, clock_t end_time)
19832155Scth {
19842155Scth 	struct dev_info	*devi = DEVI(dip);
19852155Scth 	ASSERT(dip != NULL);
19862155Scth 
19872155Scth 	if (panicstr)
19882155Scth 		return;
19892155Scth 
19902155Scth 	/*
19912155Scth 	 * We are called to wait for of a new child, and new child can
19922155Scth 	 * only be added if circular is zero.
19932155Scth 	 */
19942155Scth 	ASSERT(circular == 0);
19952155Scth 
19962155Scth 	/* like ndi_devi_exit with circular of zero */
19972155Scth 	mutex_enter(&(devi->devi_lock));
19982155Scth 	devi->devi_flags &= ~DEVI_BUSY;
19992155Scth 	ASSERT(devi->devi_busy_thread == curthread);
20002155Scth 	devi->devi_busy_thread = NULL;
20012155Scth 	cv_broadcast(&(devi->devi_cv));
20022155Scth 
20032155Scth 	/* now wait for new children while still holding devi_lock */
20042155Scth 	(void) cv_timedwait(&devi->devi_cv, &(devi->devi_lock), end_time);
20052155Scth 	mutex_exit(&(devi->devi_lock));
20060Sstevel@tonic-gate }
20070Sstevel@tonic-gate 
20080Sstevel@tonic-gate /*
20090Sstevel@tonic-gate  * Attempt to single thread entry into devinfo node for modifying its children.
20100Sstevel@tonic-gate  */
20110Sstevel@tonic-gate int
ndi_devi_tryenter(dev_info_t * dip,int * circular)20120Sstevel@tonic-gate ndi_devi_tryenter(dev_info_t *dip, int *circular)
20130Sstevel@tonic-gate {
20140Sstevel@tonic-gate 	int rval = 1;		   /* assume we enter */
20150Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
20160Sstevel@tonic-gate 	ASSERT(dip != NULL);
20170Sstevel@tonic-gate 
20180Sstevel@tonic-gate 	mutex_enter(&devi->devi_lock);
20190Sstevel@tonic-gate 	if (devi->devi_busy_thread == (void *)curthread) {
20200Sstevel@tonic-gate 		devi->devi_circular++;
20210Sstevel@tonic-gate 	} else {
20220Sstevel@tonic-gate 		if (!DEVI_BUSY_CHANGING(devi)) {
20230Sstevel@tonic-gate 			devi->devi_flags |= DEVI_BUSY;
20240Sstevel@tonic-gate 			devi->devi_busy_thread = (void *)curthread;
20250Sstevel@tonic-gate 		} else {
20260Sstevel@tonic-gate 			rval = 0;	/* devi is busy */
20270Sstevel@tonic-gate 		}
20280Sstevel@tonic-gate 	}
20290Sstevel@tonic-gate 	*circular = devi->devi_circular;
20300Sstevel@tonic-gate 	mutex_exit(&devi->devi_lock);
20310Sstevel@tonic-gate 	return (rval);
20320Sstevel@tonic-gate }
20330Sstevel@tonic-gate 
20340Sstevel@tonic-gate /*
20350Sstevel@tonic-gate  * Allocate and initialize a new dev_info structure.
20360Sstevel@tonic-gate  *
20370Sstevel@tonic-gate  * This routine may be called at interrupt time by a nexus in
20380Sstevel@tonic-gate  * response to a hotplug event, therefore memory allocations are
20390Sstevel@tonic-gate  * not allowed to sleep.
20400Sstevel@tonic-gate  */
20410Sstevel@tonic-gate int
ndi_devi_alloc(dev_info_t * parent,char * node_name,pnode_t nodeid,dev_info_t ** ret_dip)2042789Sahrens ndi_devi_alloc(dev_info_t *parent, char *node_name, pnode_t nodeid,
20430Sstevel@tonic-gate     dev_info_t **ret_dip)
20440Sstevel@tonic-gate {
20450Sstevel@tonic-gate 	ASSERT(node_name != NULL);
20460Sstevel@tonic-gate 	ASSERT(ret_dip != NULL);
20470Sstevel@tonic-gate 
20480Sstevel@tonic-gate 	*ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL,
20490Sstevel@tonic-gate 	    KM_NOSLEEP);
20500Sstevel@tonic-gate 	if (*ret_dip == NULL) {
20510Sstevel@tonic-gate 		return (NDI_NOMEM);
20520Sstevel@tonic-gate 	}
20530Sstevel@tonic-gate 
20540Sstevel@tonic-gate 	return (NDI_SUCCESS);
20550Sstevel@tonic-gate }
20560Sstevel@tonic-gate 
20570Sstevel@tonic-gate /*
20580Sstevel@tonic-gate  * Allocate and initialize a new dev_info structure
20590Sstevel@tonic-gate  * This routine may sleep and should not be called at interrupt time
20600Sstevel@tonic-gate  */
20610Sstevel@tonic-gate void
ndi_devi_alloc_sleep(dev_info_t * parent,char * node_name,pnode_t nodeid,dev_info_t ** ret_dip)2062789Sahrens ndi_devi_alloc_sleep(dev_info_t *parent, char *node_name, pnode_t nodeid,
20630Sstevel@tonic-gate     dev_info_t **ret_dip)
20640Sstevel@tonic-gate {
20650Sstevel@tonic-gate 	ASSERT(node_name != NULL);
20660Sstevel@tonic-gate 	ASSERT(ret_dip != NULL);
20670Sstevel@tonic-gate 
20680Sstevel@tonic-gate 	*ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL,
20690Sstevel@tonic-gate 	    KM_SLEEP);
20700Sstevel@tonic-gate 	ASSERT(*ret_dip);
20710Sstevel@tonic-gate }
20720Sstevel@tonic-gate 
20730Sstevel@tonic-gate /*
20740Sstevel@tonic-gate  * Remove an initialized (but not yet attached) dev_info
20750Sstevel@tonic-gate  * node from it's parent.
20760Sstevel@tonic-gate  */
20770Sstevel@tonic-gate int
ndi_devi_free(dev_info_t * dip)20780Sstevel@tonic-gate ndi_devi_free(dev_info_t *dip)
20790Sstevel@tonic-gate {
20800Sstevel@tonic-gate 	ASSERT(dip != NULL);
20810Sstevel@tonic-gate 
20820Sstevel@tonic-gate 	if (i_ddi_node_state(dip) >= DS_INITIALIZED)
20830Sstevel@tonic-gate 		return (DDI_FAILURE);
20840Sstevel@tonic-gate 
20850Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_free: %s%d (%p)\n",
20860Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
20870Sstevel@tonic-gate 
20880Sstevel@tonic-gate 	(void) ddi_remove_child(dip, 0);
20890Sstevel@tonic-gate 
20900Sstevel@tonic-gate 	return (NDI_SUCCESS);
20910Sstevel@tonic-gate }
20920Sstevel@tonic-gate 
20930Sstevel@tonic-gate /*
20940Sstevel@tonic-gate  * ndi_devi_bind_driver() binds a driver to a given device. If it fails
20950Sstevel@tonic-gate  * to bind the driver, it returns an appropriate error back. Some drivers
20960Sstevel@tonic-gate  * may want to know if the actually failed to bind.
20970Sstevel@tonic-gate  */
20980Sstevel@tonic-gate int
ndi_devi_bind_driver(dev_info_t * dip,uint_t flags)20990Sstevel@tonic-gate ndi_devi_bind_driver(dev_info_t *dip, uint_t flags)
21000Sstevel@tonic-gate {
21010Sstevel@tonic-gate 	int ret = NDI_FAILURE;
21020Sstevel@tonic-gate 	int circ;
21030Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
21040Sstevel@tonic-gate 	ASSERT(pdip);
21050Sstevel@tonic-gate 
21060Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
21070Sstevel@tonic-gate 	    "ndi_devi_bind_driver: %s%d (%p) flags: %x\n",
21080Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
21090Sstevel@tonic-gate 
21100Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
21110Sstevel@tonic-gate 	if (i_ndi_config_node(dip, DS_BOUND, flags) == DDI_SUCCESS)
21120Sstevel@tonic-gate 		ret = NDI_SUCCESS;
21130Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
21140Sstevel@tonic-gate 
21150Sstevel@tonic-gate 	return (ret);
21160Sstevel@tonic-gate }
21170Sstevel@tonic-gate 
21180Sstevel@tonic-gate /*
21190Sstevel@tonic-gate  * ndi_devi_unbind_driver: unbind the dip
21200Sstevel@tonic-gate  */
21210Sstevel@tonic-gate static int
ndi_devi_unbind_driver(dev_info_t * dip)21220Sstevel@tonic-gate ndi_devi_unbind_driver(dev_info_t *dip)
21230Sstevel@tonic-gate {
21240Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
21250Sstevel@tonic-gate 
21260Sstevel@tonic-gate 	return (i_ndi_unconfig_node(dip, DS_LINKED, 0));
21270Sstevel@tonic-gate }
21280Sstevel@tonic-gate 
21290Sstevel@tonic-gate /*
21300Sstevel@tonic-gate  * Misc. help routines called by framework only
21310Sstevel@tonic-gate  */
21320Sstevel@tonic-gate 
21330Sstevel@tonic-gate /*
21340Sstevel@tonic-gate  * Get the state of node
21350Sstevel@tonic-gate  */
21360Sstevel@tonic-gate ddi_node_state_t
i_ddi_node_state(dev_info_t * dip)21370Sstevel@tonic-gate i_ddi_node_state(dev_info_t *dip)
21380Sstevel@tonic-gate {
21390Sstevel@tonic-gate 	return (DEVI(dip)->devi_node_state);
21400Sstevel@tonic-gate }
21410Sstevel@tonic-gate 
21420Sstevel@tonic-gate /*
21430Sstevel@tonic-gate  * Set the state of node
21440Sstevel@tonic-gate  */
21450Sstevel@tonic-gate void
i_ddi_set_node_state(dev_info_t * dip,ddi_node_state_t state)21460Sstevel@tonic-gate i_ddi_set_node_state(dev_info_t *dip, ddi_node_state_t state)
21470Sstevel@tonic-gate {
21480Sstevel@tonic-gate 	DEVI(dip)->devi_node_state = state;
21490Sstevel@tonic-gate 	membar_enter();			/* make sure stores are flushed */
21500Sstevel@tonic-gate }
21510Sstevel@tonic-gate 
21520Sstevel@tonic-gate /*
21531333Scth  * Determine if node is attached. The implementation accommodates transient
21541333Scth  * DS_READY->DS_ATTACHED->DS_READY state changes.  Outside this file, this
21551333Scth  * function should be instead of i_ddi_node_state() DS_ATTACHED/DS_READY
21561333Scth  * state checks.
21571333Scth  */
21581333Scth int
i_ddi_devi_attached(dev_info_t * dip)21591333Scth i_ddi_devi_attached(dev_info_t *dip)
21601333Scth {
21611333Scth 	return (DEVI(dip)->devi_node_state >= DS_ATTACHED);
21621333Scth }
21631333Scth 
21641333Scth /*
21650Sstevel@tonic-gate  * Common function for finding a node in a sibling list given name and addr.
21660Sstevel@tonic-gate  *
21670Sstevel@tonic-gate  * By default, name is matched with devi_node_name. The following
21680Sstevel@tonic-gate  * alternative match strategies are supported:
21690Sstevel@tonic-gate  *
21704145Scth  *	FIND_NODE_BY_NODENAME: Match on node name - typical use.
21718912SChris.Horne@Sun.COM  *
21724145Scth  *	FIND_NODE_BY_DRIVER: A match on driver name bound to node is conducted.
21730Sstevel@tonic-gate  *		This support is used for support of OBP generic names and
21744145Scth  *		for the conversion from driver names to generic names. When
21750Sstevel@tonic-gate  *		more consistency in the generic name environment is achieved
21760Sstevel@tonic-gate  *		(and not needed for upgrade) this support can be removed.
21778912SChris.Horne@Sun.COM  *
21784145Scth  *	FIND_NODE_BY_ADDR: Match on just the addr.
21794145Scth  *		This support is only used/needed during boot to match
21804145Scth  *		a node bound via a path-based driver alias.
21810Sstevel@tonic-gate  *
21820Sstevel@tonic-gate  * If a child is not named (dev_addr == NULL), there are three
21830Sstevel@tonic-gate  * possible actions:
21840Sstevel@tonic-gate  *
21850Sstevel@tonic-gate  *	(1) skip it
21860Sstevel@tonic-gate  *	(2) FIND_ADDR_BY_INIT: bring child to DS_INITIALIZED state
21870Sstevel@tonic-gate  *	(3) FIND_ADDR_BY_CALLBACK: use a caller-supplied callback function
21880Sstevel@tonic-gate  */
21894145Scth #define	FIND_NODE_BY_NODENAME	0x01
21904145Scth #define	FIND_NODE_BY_DRIVER	0x02
21914145Scth #define	FIND_NODE_BY_ADDR	0x04
21920Sstevel@tonic-gate #define	FIND_ADDR_BY_INIT	0x10
21930Sstevel@tonic-gate #define	FIND_ADDR_BY_CALLBACK	0x20
21940Sstevel@tonic-gate 
21950Sstevel@tonic-gate static dev_info_t *
find_sibling(dev_info_t * head,char * cname,char * caddr,uint_t flag,int (* callback)(dev_info_t *,char *,int))21960Sstevel@tonic-gate find_sibling(dev_info_t *head, char *cname, char *caddr, uint_t flag,
21970Sstevel@tonic-gate     int (*callback)(dev_info_t *, char *, int))
21980Sstevel@tonic-gate {
21990Sstevel@tonic-gate 	dev_info_t	*dip;
22000Sstevel@tonic-gate 	char		*addr, *buf;
22010Sstevel@tonic-gate 	major_t		major;
22024145Scth 	uint_t		by;
22034145Scth 
22044145Scth 	/* only one way to find a node */
22054145Scth 	by = flag &
22064145Scth 	    (FIND_NODE_BY_DRIVER | FIND_NODE_BY_NODENAME | FIND_NODE_BY_ADDR);
22074145Scth 	ASSERT(by && BIT_ONLYONESET(by));
22080Sstevel@tonic-gate 
22090Sstevel@tonic-gate 	/* only one way to name a node */
22100Sstevel@tonic-gate 	ASSERT(((flag & FIND_ADDR_BY_INIT) == 0) ||
22110Sstevel@tonic-gate 	    ((flag & FIND_ADDR_BY_CALLBACK) == 0));
22120Sstevel@tonic-gate 
22134145Scth 	if (by == FIND_NODE_BY_DRIVER) {
22140Sstevel@tonic-gate 		major = ddi_name_to_major(cname);
22157009Scth 		if (major == DDI_MAJOR_T_NONE)
22160Sstevel@tonic-gate 			return (NULL);
22170Sstevel@tonic-gate 	}
22180Sstevel@tonic-gate 
22190Sstevel@tonic-gate 	/* preallocate buffer of naming node by callback */
22200Sstevel@tonic-gate 	if (flag & FIND_ADDR_BY_CALLBACK)
22210Sstevel@tonic-gate 		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
22220Sstevel@tonic-gate 
22230Sstevel@tonic-gate 	/*
22240Sstevel@tonic-gate 	 * Walk the child list to find a match
22250Sstevel@tonic-gate 	 */
222610696SDavid.Hollister@Sun.COM 	if (head == NULL)
222710696SDavid.Hollister@Sun.COM 		return (NULL);
222810696SDavid.Hollister@Sun.COM 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(head)));
22290Sstevel@tonic-gate 	for (dip = head; dip; dip = ddi_get_next_sibling(dip)) {
22304145Scth 		if (by == FIND_NODE_BY_NODENAME) {
22314145Scth 			/* match node name */
22324145Scth 			if (strcmp(cname, DEVI(dip)->devi_node_name) != 0)
22334145Scth 				continue;
22344145Scth 		} else if (by == FIND_NODE_BY_DRIVER) {
22350Sstevel@tonic-gate 			/* match driver major */
22360Sstevel@tonic-gate 			if (DEVI(dip)->devi_major != major)
22370Sstevel@tonic-gate 				continue;
22380Sstevel@tonic-gate 		}
22390Sstevel@tonic-gate 
22400Sstevel@tonic-gate 		if ((addr = DEVI(dip)->devi_addr) == NULL) {
22410Sstevel@tonic-gate 			/* name the child based on the flag */
22420Sstevel@tonic-gate 			if (flag & FIND_ADDR_BY_INIT) {
22430Sstevel@tonic-gate 				if (ddi_initchild(ddi_get_parent(dip), dip)
22440Sstevel@tonic-gate 				    != DDI_SUCCESS)
22450Sstevel@tonic-gate 					continue;
22460Sstevel@tonic-gate 				addr = DEVI(dip)->devi_addr;
22470Sstevel@tonic-gate 			} else if (flag & FIND_ADDR_BY_CALLBACK) {
22480Sstevel@tonic-gate 				if ((callback == NULL) || (callback(
22490Sstevel@tonic-gate 				    dip, buf, MAXNAMELEN) != DDI_SUCCESS))
22500Sstevel@tonic-gate 					continue;
22510Sstevel@tonic-gate 				addr = buf;
22520Sstevel@tonic-gate 			} else {
22530Sstevel@tonic-gate 				continue;	/* skip */
22540Sstevel@tonic-gate 			}
22550Sstevel@tonic-gate 		}
22560Sstevel@tonic-gate 
22570Sstevel@tonic-gate 		/* match addr */
22580Sstevel@tonic-gate 		ASSERT(addr != NULL);
22590Sstevel@tonic-gate 		if (strcmp(caddr, addr) == 0)
22600Sstevel@tonic-gate 			break;	/* node found */
22610Sstevel@tonic-gate 
22620Sstevel@tonic-gate 	}
22630Sstevel@tonic-gate 	if (flag & FIND_ADDR_BY_CALLBACK)
22640Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
22650Sstevel@tonic-gate 	return (dip);
22660Sstevel@tonic-gate }
22670Sstevel@tonic-gate 
22680Sstevel@tonic-gate /*
22690Sstevel@tonic-gate  * Find child of pdip with name: cname@caddr
22700Sstevel@tonic-gate  * Called by init_node() to look for duplicate nodes
22710Sstevel@tonic-gate  */
22720Sstevel@tonic-gate static dev_info_t *
find_duplicate_child(dev_info_t * pdip,dev_info_t * dip)22730Sstevel@tonic-gate find_duplicate_child(dev_info_t *pdip, dev_info_t *dip)
22740Sstevel@tonic-gate {
22750Sstevel@tonic-gate 	dev_info_t *dup;
22760Sstevel@tonic-gate 	char *cname = DEVI(dip)->devi_node_name;
22770Sstevel@tonic-gate 	char *caddr = DEVI(dip)->devi_addr;
22780Sstevel@tonic-gate 
22790Sstevel@tonic-gate 	/* search nodes before dip */
22804145Scth 	dup = find_sibling(ddi_get_child(pdip), cname, caddr,
22814145Scth 	    FIND_NODE_BY_NODENAME, NULL);
22820Sstevel@tonic-gate 	if (dup != dip)
22830Sstevel@tonic-gate 		return (dup);
22840Sstevel@tonic-gate 
22850Sstevel@tonic-gate 	/*
22860Sstevel@tonic-gate 	 * search nodes after dip; normally this is not needed,
22870Sstevel@tonic-gate 	 */
22880Sstevel@tonic-gate 	return (find_sibling(ddi_get_next_sibling(dip), cname, caddr,
22894145Scth 	    FIND_NODE_BY_NODENAME, NULL));
22900Sstevel@tonic-gate }
22910Sstevel@tonic-gate 
22920Sstevel@tonic-gate /*
22930Sstevel@tonic-gate  * Find a child of a given name and address, using a callback to name
22940Sstevel@tonic-gate  * unnamed children. cname is the binding name.
22950Sstevel@tonic-gate  */
229610696SDavid.Hollister@Sun.COM dev_info_t *
ndi_devi_findchild_by_callback(dev_info_t * pdip,char * dname,char * ua,int (* make_ua)(dev_info_t *,char *,int))229710696SDavid.Hollister@Sun.COM ndi_devi_findchild_by_callback(dev_info_t *pdip, char *dname, char *ua,
229810696SDavid.Hollister@Sun.COM     int (*make_ua)(dev_info_t *, char *, int))
229910696SDavid.Hollister@Sun.COM {
230010696SDavid.Hollister@Sun.COM 	int	by = FIND_ADDR_BY_CALLBACK;
230110696SDavid.Hollister@Sun.COM 
230210696SDavid.Hollister@Sun.COM 	ASSERT(DEVI_BUSY_OWNED(pdip));
230310696SDavid.Hollister@Sun.COM 	by |= dname ? FIND_NODE_BY_DRIVER : FIND_NODE_BY_ADDR;
230410696SDavid.Hollister@Sun.COM 	return (find_sibling(ddi_get_child(pdip), dname, ua, by, make_ua));
23050Sstevel@tonic-gate }
23060Sstevel@tonic-gate 
23070Sstevel@tonic-gate /*
23080Sstevel@tonic-gate  * Find a child of a given name and address, invoking initchild to name
23090Sstevel@tonic-gate  * unnamed children. cname is the node name.
23100Sstevel@tonic-gate  */
23110Sstevel@tonic-gate static dev_info_t *
find_child_by_name(dev_info_t * pdip,char * cname,char * caddr)23120Sstevel@tonic-gate find_child_by_name(dev_info_t *pdip, char *cname, char *caddr)
23130Sstevel@tonic-gate {
23140Sstevel@tonic-gate 	dev_info_t	*dip;
23150Sstevel@tonic-gate 
23164145Scth 	/* attempt search without changing state of preceding siblings */
23174145Scth 	dip = find_sibling(ddi_get_child(pdip), cname, caddr,
23184145Scth 	    FIND_NODE_BY_NODENAME, NULL);
23190Sstevel@tonic-gate 	if (dip)
23200Sstevel@tonic-gate 		return (dip);
23210Sstevel@tonic-gate 
23220Sstevel@tonic-gate 	return (find_sibling(ddi_get_child(pdip), cname, caddr,
23234145Scth 	    FIND_NODE_BY_NODENAME|FIND_ADDR_BY_INIT, NULL));
23240Sstevel@tonic-gate }
23250Sstevel@tonic-gate 
23260Sstevel@tonic-gate /*
23270Sstevel@tonic-gate  * Find a child of a given name and address, invoking initchild to name
23280Sstevel@tonic-gate  * unnamed children. cname is the node name.
23290Sstevel@tonic-gate  */
23300Sstevel@tonic-gate static dev_info_t *
find_child_by_driver(dev_info_t * pdip,char * cname,char * caddr)23310Sstevel@tonic-gate find_child_by_driver(dev_info_t *pdip, char *cname, char *caddr)
23320Sstevel@tonic-gate {
23330Sstevel@tonic-gate 	dev_info_t	*dip;
23340Sstevel@tonic-gate 
23354145Scth 	/* attempt search without changing state of preceding siblings */
23360Sstevel@tonic-gate 	dip = find_sibling(ddi_get_child(pdip), cname, caddr,
23374145Scth 	    FIND_NODE_BY_DRIVER, NULL);
23380Sstevel@tonic-gate 	if (dip)
23390Sstevel@tonic-gate 		return (dip);
23400Sstevel@tonic-gate 
23410Sstevel@tonic-gate 	return (find_sibling(ddi_get_child(pdip), cname, caddr,
23424145Scth 	    FIND_NODE_BY_DRIVER|FIND_ADDR_BY_INIT, NULL));
23434145Scth }
23444145Scth 
23454145Scth /*
23464145Scth  * Find a child of a given address, invoking initchild to name
23474145Scth  * unnamed children. cname is the node name.
23484145Scth  *
23494145Scth  * NOTE: This function is only used during boot. One would hope that
23504145Scth  * unique sibling unit-addresses on hardware branches of the tree would
23514145Scth  * be a requirement to avoid two drivers trying to control the same
23524145Scth  * piece of hardware. Unfortunately there are some cases where this
23534145Scth  * situation exists (/ssm@0,0/pci@1c,700000 /ssm@0,0/sghsc@1c,700000).
23544145Scth  * Until unit-address uniqueness of siblings is guaranteed, use of this
23554145Scth  * interface for purposes other than boot should be avoided.
23564145Scth  */
23574145Scth static dev_info_t *
find_child_by_addr(dev_info_t * pdip,char * caddr)23584145Scth find_child_by_addr(dev_info_t *pdip, char *caddr)
23594145Scth {
23604145Scth 	dev_info_t	*dip;
23614145Scth 
23624540Scth 	/* return NULL if called without a unit-address */
23634540Scth 	if ((caddr == NULL) || (*caddr == '\0'))
23644540Scth 		return (NULL);
23654540Scth 
23664145Scth 	/* attempt search without changing state of preceding siblings */
23674145Scth 	dip = find_sibling(ddi_get_child(pdip), NULL, caddr,
23684145Scth 	    FIND_NODE_BY_ADDR, NULL);
23694145Scth 	if (dip)
23704145Scth 		return (dip);
23714145Scth 
23724145Scth 	return (find_sibling(ddi_get_child(pdip), NULL, caddr,
23734145Scth 	    FIND_NODE_BY_ADDR|FIND_ADDR_BY_INIT, NULL));
23740Sstevel@tonic-gate }
23750Sstevel@tonic-gate 
23760Sstevel@tonic-gate /*
23770Sstevel@tonic-gate  * Deleting a property list. Take care, since some property structures
23780Sstevel@tonic-gate  * may not be fully built.
23790Sstevel@tonic-gate  */
23800Sstevel@tonic-gate void
i_ddi_prop_list_delete(ddi_prop_t * prop)23810Sstevel@tonic-gate i_ddi_prop_list_delete(ddi_prop_t *prop)
23820Sstevel@tonic-gate {
23830Sstevel@tonic-gate 	while (prop) {
23840Sstevel@tonic-gate 		ddi_prop_t *next = prop->prop_next;
23850Sstevel@tonic-gate 		if (prop->prop_name)
23860Sstevel@tonic-gate 			kmem_free(prop->prop_name, strlen(prop->prop_name) + 1);
23870Sstevel@tonic-gate 		if ((prop->prop_len != 0) && prop->prop_val)
23880Sstevel@tonic-gate 			kmem_free(prop->prop_val, prop->prop_len);
23890Sstevel@tonic-gate 		kmem_free(prop, sizeof (struct ddi_prop));
23900Sstevel@tonic-gate 		prop = next;
23910Sstevel@tonic-gate 	}
23920Sstevel@tonic-gate }
23930Sstevel@tonic-gate 
23940Sstevel@tonic-gate /*
23950Sstevel@tonic-gate  * Duplicate property list
23960Sstevel@tonic-gate  */
23970Sstevel@tonic-gate ddi_prop_t *
i_ddi_prop_list_dup(ddi_prop_t * prop,uint_t flag)23980Sstevel@tonic-gate i_ddi_prop_list_dup(ddi_prop_t *prop, uint_t flag)
23990Sstevel@tonic-gate {
24000Sstevel@tonic-gate 	ddi_prop_t *result, *prev, *copy;
24010Sstevel@tonic-gate 
24020Sstevel@tonic-gate 	if (prop == NULL)
24030Sstevel@tonic-gate 		return (NULL);
24040Sstevel@tonic-gate 
24050Sstevel@tonic-gate 	result = prev = NULL;
24060Sstevel@tonic-gate 	for (; prop != NULL; prop = prop->prop_next) {
24070Sstevel@tonic-gate 		ASSERT(prop->prop_name != NULL);
24080Sstevel@tonic-gate 		copy = kmem_zalloc(sizeof (struct ddi_prop), flag);
24090Sstevel@tonic-gate 		if (copy == NULL)
24100Sstevel@tonic-gate 			goto fail;
24110Sstevel@tonic-gate 
24120Sstevel@tonic-gate 		copy->prop_dev = prop->prop_dev;
24130Sstevel@tonic-gate 		copy->prop_flags = prop->prop_flags;
24140Sstevel@tonic-gate 		copy->prop_name = i_ddi_strdup(prop->prop_name, flag);
24150Sstevel@tonic-gate 		if (copy->prop_name == NULL)
24160Sstevel@tonic-gate 			goto fail;
24170Sstevel@tonic-gate 
24180Sstevel@tonic-gate 		if ((copy->prop_len = prop->prop_len) != 0) {
24190Sstevel@tonic-gate 			copy->prop_val = kmem_zalloc(prop->prop_len, flag);
24200Sstevel@tonic-gate 			if (copy->prop_val == NULL)
24210Sstevel@tonic-gate 				goto fail;
24220Sstevel@tonic-gate 
24230Sstevel@tonic-gate 			bcopy(prop->prop_val, copy->prop_val, prop->prop_len);
24240Sstevel@tonic-gate 		}
24250Sstevel@tonic-gate 
24260Sstevel@tonic-gate 		if (prev == NULL)
24270Sstevel@tonic-gate 			result = prev = copy;
24280Sstevel@tonic-gate 		else
24290Sstevel@tonic-gate 			prev->prop_next = copy;
24300Sstevel@tonic-gate 		prev = copy;
24310Sstevel@tonic-gate 	}
24320Sstevel@tonic-gate 	return (result);
24330Sstevel@tonic-gate 
24340Sstevel@tonic-gate fail:
24350Sstevel@tonic-gate 	i_ddi_prop_list_delete(result);
24360Sstevel@tonic-gate 	return (NULL);
24370Sstevel@tonic-gate }
24380Sstevel@tonic-gate 
24390Sstevel@tonic-gate /*
24400Sstevel@tonic-gate  * Create a reference property list, currently used only for
24410Sstevel@tonic-gate  * driver global properties. Created with ref count of 1.
24420Sstevel@tonic-gate  */
24430Sstevel@tonic-gate ddi_prop_list_t *
i_ddi_prop_list_create(ddi_prop_t * props)24440Sstevel@tonic-gate i_ddi_prop_list_create(ddi_prop_t *props)
24450Sstevel@tonic-gate {
24460Sstevel@tonic-gate 	ddi_prop_list_t *list = kmem_alloc(sizeof (*list), KM_SLEEP);
24470Sstevel@tonic-gate 	list->prop_list = props;
24480Sstevel@tonic-gate 	list->prop_ref = 1;
24490Sstevel@tonic-gate 	return (list);
24500Sstevel@tonic-gate }
24510Sstevel@tonic-gate 
24520Sstevel@tonic-gate /*
24530Sstevel@tonic-gate  * Increment/decrement reference count. The reference is
24540Sstevel@tonic-gate  * protected by dn_lock. The only interfaces modifying
24550Sstevel@tonic-gate  * dn_global_prop_ptr is in impl_make[free]_parlist().
24560Sstevel@tonic-gate  */
24570Sstevel@tonic-gate void
i_ddi_prop_list_hold(ddi_prop_list_t * prop_list,struct devnames * dnp)24580Sstevel@tonic-gate i_ddi_prop_list_hold(ddi_prop_list_t *prop_list, struct devnames *dnp)
24590Sstevel@tonic-gate {
24600Sstevel@tonic-gate 	ASSERT(prop_list->prop_ref >= 0);
24610Sstevel@tonic-gate 	ASSERT(mutex_owned(&dnp->dn_lock));
24620Sstevel@tonic-gate 	prop_list->prop_ref++;
24630Sstevel@tonic-gate }
24640Sstevel@tonic-gate 
24650Sstevel@tonic-gate void
i_ddi_prop_list_rele(ddi_prop_list_t * prop_list,struct devnames * dnp)24660Sstevel@tonic-gate i_ddi_prop_list_rele(ddi_prop_list_t *prop_list, struct devnames *dnp)
24670Sstevel@tonic-gate {
24680Sstevel@tonic-gate 	ASSERT(prop_list->prop_ref > 0);
24690Sstevel@tonic-gate 	ASSERT(mutex_owned(&dnp->dn_lock));
24700Sstevel@tonic-gate 	prop_list->prop_ref--;
24710Sstevel@tonic-gate 
24720Sstevel@tonic-gate 	if (prop_list->prop_ref == 0) {
24730Sstevel@tonic-gate 		i_ddi_prop_list_delete(prop_list->prop_list);
24740Sstevel@tonic-gate 		kmem_free(prop_list, sizeof (*prop_list));
24750Sstevel@tonic-gate 	}
24760Sstevel@tonic-gate }
24770Sstevel@tonic-gate 
24780Sstevel@tonic-gate /*
24790Sstevel@tonic-gate  * Free table of classes by drivers
24800Sstevel@tonic-gate  */
24810Sstevel@tonic-gate void
i_ddi_free_exported_classes(char ** classes,int n)24820Sstevel@tonic-gate i_ddi_free_exported_classes(char **classes, int n)
24830Sstevel@tonic-gate {
24840Sstevel@tonic-gate 	if ((n == 0) || (classes == NULL))
24850Sstevel@tonic-gate 		return;
24860Sstevel@tonic-gate 
24870Sstevel@tonic-gate 	kmem_free(classes, n * sizeof (char *));
24880Sstevel@tonic-gate }
24890Sstevel@tonic-gate 
24900Sstevel@tonic-gate /*
24910Sstevel@tonic-gate  * Get all classes exported by dip
24920Sstevel@tonic-gate  */
24930Sstevel@tonic-gate int
i_ddi_get_exported_classes(dev_info_t * dip,char *** classes)24940Sstevel@tonic-gate i_ddi_get_exported_classes(dev_info_t *dip, char ***classes)
24950Sstevel@tonic-gate {
24960Sstevel@tonic-gate 	extern void lock_hw_class_list();
24970Sstevel@tonic-gate 	extern void unlock_hw_class_list();
24980Sstevel@tonic-gate 	extern int get_class(const char *, char **);
24990Sstevel@tonic-gate 
25000Sstevel@tonic-gate 	static char *rootclass = "root";
25010Sstevel@tonic-gate 	int n = 0, nclass = 0;
25020Sstevel@tonic-gate 	char **buf;
25030Sstevel@tonic-gate 
25040Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) >= DS_BOUND);
25050Sstevel@tonic-gate 
25060Sstevel@tonic-gate 	if (dip == ddi_root_node())	/* rootnode exports class "root" */
25070Sstevel@tonic-gate 		nclass = 1;
25080Sstevel@tonic-gate 	lock_hw_class_list();
25090Sstevel@tonic-gate 	nclass += get_class(ddi_driver_name(dip), NULL);
25100Sstevel@tonic-gate 	if (nclass == 0) {
25110Sstevel@tonic-gate 		unlock_hw_class_list();
25120Sstevel@tonic-gate 		return (0);		/* no class exported */
25130Sstevel@tonic-gate 	}
25140Sstevel@tonic-gate 
25150Sstevel@tonic-gate 	*classes = buf = kmem_alloc(nclass * sizeof (char *), KM_SLEEP);
25160Sstevel@tonic-gate 	if (dip == ddi_root_node()) {
25170Sstevel@tonic-gate 		*buf++ = rootclass;
25180Sstevel@tonic-gate 		n = 1;
25190Sstevel@tonic-gate 	}
25200Sstevel@tonic-gate 	n += get_class(ddi_driver_name(dip), buf);
25210Sstevel@tonic-gate 	unlock_hw_class_list();
25220Sstevel@tonic-gate 
252310923SEvan.Yan@Sun.COM 	ASSERT(n == nclass);	/* make sure buf wasn't overrun */
25240Sstevel@tonic-gate 	return (nclass);
25250Sstevel@tonic-gate }
25260Sstevel@tonic-gate 
25270Sstevel@tonic-gate /*
25280Sstevel@tonic-gate  * Helper functions, returns NULL if no memory.
25290Sstevel@tonic-gate  */
25300Sstevel@tonic-gate char *
i_ddi_strdup(char * str,uint_t flag)25310Sstevel@tonic-gate i_ddi_strdup(char *str, uint_t flag)
25320Sstevel@tonic-gate {
25330Sstevel@tonic-gate 	char *copy;
25340Sstevel@tonic-gate 
25350Sstevel@tonic-gate 	if (str == NULL)
25360Sstevel@tonic-gate 		return (NULL);
25370Sstevel@tonic-gate 
25380Sstevel@tonic-gate 	copy = kmem_alloc(strlen(str) + 1, flag);
25390Sstevel@tonic-gate 	if (copy == NULL)
25400Sstevel@tonic-gate 		return (NULL);
25410Sstevel@tonic-gate 
25420Sstevel@tonic-gate 	(void) strcpy(copy, str);
25430Sstevel@tonic-gate 	return (copy);
25440Sstevel@tonic-gate }
25450Sstevel@tonic-gate 
25460Sstevel@tonic-gate /*
25470Sstevel@tonic-gate  * Load driver.conf file for major. Load all if major == -1.
25480Sstevel@tonic-gate  *
25490Sstevel@tonic-gate  * This is called
25500Sstevel@tonic-gate  * - early in boot after devnames array is initialized
25510Sstevel@tonic-gate  * - from vfs code when certain file systems are mounted
25520Sstevel@tonic-gate  * - from add_drv when a new driver is added
25530Sstevel@tonic-gate  */
25540Sstevel@tonic-gate int
i_ddi_load_drvconf(major_t major)25550Sstevel@tonic-gate i_ddi_load_drvconf(major_t major)
25560Sstevel@tonic-gate {
25570Sstevel@tonic-gate 	extern int modrootloaded;
25580Sstevel@tonic-gate 
25590Sstevel@tonic-gate 	major_t low, high, m;
25600Sstevel@tonic-gate 
25617009Scth 	if (major == DDI_MAJOR_T_NONE) {
25620Sstevel@tonic-gate 		low = 0;
25630Sstevel@tonic-gate 		high = devcnt - 1;
25640Sstevel@tonic-gate 	} else {
25650Sstevel@tonic-gate 		if (major >= devcnt)
25660Sstevel@tonic-gate 			return (EINVAL);
25670Sstevel@tonic-gate 		low = high = major;
25680Sstevel@tonic-gate 	}
25690Sstevel@tonic-gate 
25700Sstevel@tonic-gate 	for (m = low; m <= high; m++) {
25710Sstevel@tonic-gate 		struct devnames *dnp = &devnamesp[m];
25720Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
257310842SJerry.Gilliam@Sun.COM 		dnp->dn_flags &= ~(DN_DRIVER_HELD|DN_DRIVER_INACTIVE);
25740Sstevel@tonic-gate 		(void) impl_make_parlist(m);
25750Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
25760Sstevel@tonic-gate 	}
25770Sstevel@tonic-gate 
25780Sstevel@tonic-gate 	if (modrootloaded) {
25790Sstevel@tonic-gate 		ddi_walk_devs(ddi_root_node(), reset_nexus_flags,
25800Sstevel@tonic-gate 		    (void *)(uintptr_t)major);
25810Sstevel@tonic-gate 	}
25820Sstevel@tonic-gate 
25830Sstevel@tonic-gate 	/* build dn_list from old entries in path_to_inst */
25840Sstevel@tonic-gate 	e_ddi_unorphan_instance_nos();
25850Sstevel@tonic-gate 	return (0);
25860Sstevel@tonic-gate }
25870Sstevel@tonic-gate 
25880Sstevel@tonic-gate /*
25890Sstevel@tonic-gate  * Unload a specific driver.conf.
25900Sstevel@tonic-gate  * Don't support unload all because it doesn't make any sense
25910Sstevel@tonic-gate  */
25920Sstevel@tonic-gate int
i_ddi_unload_drvconf(major_t major)25930Sstevel@tonic-gate i_ddi_unload_drvconf(major_t major)
25940Sstevel@tonic-gate {
25950Sstevel@tonic-gate 	int error;
25960Sstevel@tonic-gate 	struct devnames *dnp;
25970Sstevel@tonic-gate 
25980Sstevel@tonic-gate 	if (major >= devcnt)
25990Sstevel@tonic-gate 		return (EINVAL);
26000Sstevel@tonic-gate 
26010Sstevel@tonic-gate 	/*
26020Sstevel@tonic-gate 	 * Take the per-driver lock while unloading driver.conf
26030Sstevel@tonic-gate 	 */
26040Sstevel@tonic-gate 	dnp = &devnamesp[major];
26050Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
26060Sstevel@tonic-gate 	error = impl_free_parlist(major);
26070Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
26080Sstevel@tonic-gate 	return (error);
26090Sstevel@tonic-gate }
26100Sstevel@tonic-gate 
26110Sstevel@tonic-gate /*
26120Sstevel@tonic-gate  * Merge a .conf node. This is called by nexus drivers to augment
26130Sstevel@tonic-gate  * hw node with properties specified in driver.conf file. This function
26140Sstevel@tonic-gate  * takes a callback routine to name nexus children.
26150Sstevel@tonic-gate  * The parent node must be held busy.
26160Sstevel@tonic-gate  *
26170Sstevel@tonic-gate  * It returns DDI_SUCCESS if the node is merged and DDI_FAILURE otherwise.
26180Sstevel@tonic-gate  */
26190Sstevel@tonic-gate int
ndi_merge_node(dev_info_t * dip,int (* make_ua)(dev_info_t *,char *,int))262010696SDavid.Hollister@Sun.COM ndi_merge_node(dev_info_t *dip, int (*make_ua)(dev_info_t *, char *, int))
26210Sstevel@tonic-gate {
26220Sstevel@tonic-gate 	dev_info_t *hwdip;
26230Sstevel@tonic-gate 
26240Sstevel@tonic-gate 	ASSERT(ndi_dev_is_persistent_node(dip) == 0);
26250Sstevel@tonic-gate 	ASSERT(ddi_get_name_addr(dip) != NULL);
26260Sstevel@tonic-gate 
262710696SDavid.Hollister@Sun.COM 	hwdip = ndi_devi_findchild_by_callback(ddi_get_parent(dip),
262810696SDavid.Hollister@Sun.COM 	    ddi_binding_name(dip), ddi_get_name_addr(dip), make_ua);
26290Sstevel@tonic-gate 
26300Sstevel@tonic-gate 	/*
26310Sstevel@tonic-gate 	 * Look for the hardware node that is the target of the merge;
26320Sstevel@tonic-gate 	 * return failure if not found.
26330Sstevel@tonic-gate 	 */
26340Sstevel@tonic-gate 	if ((hwdip == NULL) || (hwdip == dip)) {
26350Sstevel@tonic-gate 		char *buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
26360Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_WARN, "No HW node to merge conf node %s",
26370Sstevel@tonic-gate 		    ddi_deviname(dip, buf)));
26380Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
26390Sstevel@tonic-gate 		return (DDI_FAILURE);
26400Sstevel@tonic-gate 	}
26410Sstevel@tonic-gate 
26420Sstevel@tonic-gate 	/*
26430Sstevel@tonic-gate 	 * Make sure the hardware node is uninitialized and has no property.
26440Sstevel@tonic-gate 	 * This may not be the case if new .conf files are load after some
26450Sstevel@tonic-gate 	 * hardware nodes have already been initialized and attached.
26460Sstevel@tonic-gate 	 *
26470Sstevel@tonic-gate 	 * N.B. We return success here because the node was *intended*
26488912SChris.Horne@Sun.COM 	 *	to be a merge node because there is a hw node with the name.
26490Sstevel@tonic-gate 	 */
26500Sstevel@tonic-gate 	mutex_enter(&DEVI(hwdip)->devi_lock);
26510Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(hwdip) == 0) {
26520Sstevel@tonic-gate 		char *buf;
26530Sstevel@tonic-gate 		mutex_exit(&DEVI(hwdip)->devi_lock);
26540Sstevel@tonic-gate 
26550Sstevel@tonic-gate 		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
26560Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE, "Duplicate .conf node %s",
26570Sstevel@tonic-gate 		    ddi_deviname(dip, buf)));
26580Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
26590Sstevel@tonic-gate 		return (DDI_SUCCESS);
26600Sstevel@tonic-gate 	}
26610Sstevel@tonic-gate 
26620Sstevel@tonic-gate 	/*
26630Sstevel@tonic-gate 	 * If it is possible that the hardware has already been touched
26640Sstevel@tonic-gate 	 * then don't merge.
26650Sstevel@tonic-gate 	 */
26660Sstevel@tonic-gate 	if (i_ddi_node_state(hwdip) >= DS_INITIALIZED ||
26670Sstevel@tonic-gate 	    (DEVI(hwdip)->devi_sys_prop_ptr != NULL) ||
26680Sstevel@tonic-gate 	    (DEVI(hwdip)->devi_drv_prop_ptr != NULL)) {
26690Sstevel@tonic-gate 		char *buf;
26700Sstevel@tonic-gate 		mutex_exit(&DEVI(hwdip)->devi_lock);
26710Sstevel@tonic-gate 
26720Sstevel@tonic-gate 		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
26730Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE,
26740Sstevel@tonic-gate 		    "!Cannot merge .conf node %s with hw node %p "
26750Sstevel@tonic-gate 		    "-- not in proper state",
26760Sstevel@tonic-gate 		    ddi_deviname(dip, buf), (void *)hwdip));
26770Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
26780Sstevel@tonic-gate 		return (DDI_SUCCESS);
26790Sstevel@tonic-gate 	}
26800Sstevel@tonic-gate 
26810Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
26820Sstevel@tonic-gate 	DEVI(hwdip)->devi_sys_prop_ptr = DEVI(dip)->devi_sys_prop_ptr;
26830Sstevel@tonic-gate 	DEVI(hwdip)->devi_drv_prop_ptr = DEVI(dip)->devi_drv_prop_ptr;
26840Sstevel@tonic-gate 	DEVI(dip)->devi_sys_prop_ptr = NULL;
26850Sstevel@tonic-gate 	DEVI(dip)->devi_drv_prop_ptr = NULL;
26860Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
26870Sstevel@tonic-gate 	mutex_exit(&DEVI(hwdip)->devi_lock);
26880Sstevel@tonic-gate 
26890Sstevel@tonic-gate 	return (DDI_SUCCESS);
26900Sstevel@tonic-gate }
26910Sstevel@tonic-gate 
26920Sstevel@tonic-gate /*
26930Sstevel@tonic-gate  * Merge a "wildcard" .conf node. This is called by nexus drivers to
26940Sstevel@tonic-gate  * augment a set of hw node with properties specified in driver.conf file.
26950Sstevel@tonic-gate  * The parent node must be held busy.
26960Sstevel@tonic-gate  *
26970Sstevel@tonic-gate  * There is no failure mode, since the nexus may or may not have child
26980Sstevel@tonic-gate  * node bound the driver specified by the wildcard node.
26990Sstevel@tonic-gate  */
27000Sstevel@tonic-gate void
ndi_merge_wildcard_node(dev_info_t * dip)27010Sstevel@tonic-gate ndi_merge_wildcard_node(dev_info_t *dip)
27020Sstevel@tonic-gate {
27030Sstevel@tonic-gate 	dev_info_t *hwdip;
27040Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
27050Sstevel@tonic-gate 	major_t major = ddi_driver_major(dip);
27060Sstevel@tonic-gate 
27070Sstevel@tonic-gate 	/* never attempt to merge a hw node */
27080Sstevel@tonic-gate 	ASSERT(ndi_dev_is_persistent_node(dip) == 0);
27090Sstevel@tonic-gate 	/* must be bound to a driver major number */
27107009Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
27110Sstevel@tonic-gate 
27120Sstevel@tonic-gate 	/*
27130Sstevel@tonic-gate 	 * Walk the child list to find all nodes bound to major
27140Sstevel@tonic-gate 	 * and copy properties.
27150Sstevel@tonic-gate 	 */
27160Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
27177224Scth 	ASSERT(DEVI_BUSY_OWNED(pdip));
27180Sstevel@tonic-gate 	for (hwdip = ddi_get_child(pdip); hwdip;
27190Sstevel@tonic-gate 	    hwdip = ddi_get_next_sibling(hwdip)) {
27200Sstevel@tonic-gate 		/*
27210Sstevel@tonic-gate 		 * Skip nodes not bound to same driver
27220Sstevel@tonic-gate 		 */
27230Sstevel@tonic-gate 		if (ddi_driver_major(hwdip) != major)
27240Sstevel@tonic-gate 			continue;
27250Sstevel@tonic-gate 
27260Sstevel@tonic-gate 		/*
27270Sstevel@tonic-gate 		 * Skip .conf nodes
27280Sstevel@tonic-gate 		 */
27290Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(hwdip) == 0)
27300Sstevel@tonic-gate 			continue;
27310Sstevel@tonic-gate 
27320Sstevel@tonic-gate 		/*
27330Sstevel@tonic-gate 		 * Make sure the node is uninitialized and has no property.
27340Sstevel@tonic-gate 		 */
27350Sstevel@tonic-gate 		mutex_enter(&DEVI(hwdip)->devi_lock);
27360Sstevel@tonic-gate 		if (i_ddi_node_state(hwdip) >= DS_INITIALIZED ||
27370Sstevel@tonic-gate 		    (DEVI(hwdip)->devi_sys_prop_ptr != NULL) ||
27380Sstevel@tonic-gate 		    (DEVI(hwdip)->devi_drv_prop_ptr != NULL)) {
27390Sstevel@tonic-gate 			mutex_exit(&DEVI(hwdip)->devi_lock);
27400Sstevel@tonic-gate 			NDI_CONFIG_DEBUG((CE_NOTE, "HW node %p state not "
27410Sstevel@tonic-gate 			    "suitable for merging wildcard conf node %s",
27420Sstevel@tonic-gate 			    (void *)hwdip, ddi_node_name(dip)));
27430Sstevel@tonic-gate 			continue;
27440Sstevel@tonic-gate 		}
27450Sstevel@tonic-gate 
27460Sstevel@tonic-gate 		DEVI(hwdip)->devi_sys_prop_ptr =
27470Sstevel@tonic-gate 		    i_ddi_prop_list_dup(DEVI(dip)->devi_sys_prop_ptr, KM_SLEEP);
27480Sstevel@tonic-gate 		DEVI(hwdip)->devi_drv_prop_ptr =
27490Sstevel@tonic-gate 		    i_ddi_prop_list_dup(DEVI(dip)->devi_drv_prop_ptr, KM_SLEEP);
27500Sstevel@tonic-gate 		mutex_exit(&DEVI(hwdip)->devi_lock);
27510Sstevel@tonic-gate 	}
27520Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
27530Sstevel@tonic-gate }
27540Sstevel@tonic-gate 
27550Sstevel@tonic-gate /*
27560Sstevel@tonic-gate  * Return the major number based on the compatible property. This interface
27570Sstevel@tonic-gate  * may be used in situations where we are trying to detect if a better driver
27580Sstevel@tonic-gate  * now exists for a device, so it must use the 'compatible' property.  If
27590Sstevel@tonic-gate  * a non-NULL formp is specified and the binding was based on compatible then
27600Sstevel@tonic-gate  * return the pointer to the form used in *formp.
27610Sstevel@tonic-gate  */
27620Sstevel@tonic-gate major_t
ddi_compatible_driver_major(dev_info_t * dip,char ** formp)27630Sstevel@tonic-gate ddi_compatible_driver_major(dev_info_t *dip, char **formp)
27640Sstevel@tonic-gate {
27650Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
27660Sstevel@tonic-gate 	void		*compat;
27670Sstevel@tonic-gate 	size_t		len;
27680Sstevel@tonic-gate 	char		*p = NULL;
27697009Scth 	major_t		major = DDI_MAJOR_T_NONE;
27700Sstevel@tonic-gate 
27710Sstevel@tonic-gate 	if (formp)
27720Sstevel@tonic-gate 		*formp = NULL;
27730Sstevel@tonic-gate 
277411596SJason.Beloro@Sun.COM 	if (ddi_prop_exists(DDI_DEV_T_NONE, dip, DDI_PROP_DONTPASS,
277511596SJason.Beloro@Sun.COM 	    "ddi-assigned")) {
277611596SJason.Beloro@Sun.COM 		major = ddi_name_to_major("nulldriver");
277711596SJason.Beloro@Sun.COM 		return (major);
277811596SJason.Beloro@Sun.COM 	}
277911596SJason.Beloro@Sun.COM 
27804145Scth 	/*
27814145Scth 	 * Highest precedence binding is a path-oriented alias. Since this
27824145Scth 	 * requires a 'path', this type of binding occurs via more obtuse
27834145Scth 	 * 'rebind'. The need for a path-oriented alias 'rebind' is detected
27844145Scth 	 * after a successful DDI_CTLOPS_INITCHILD to another driver: this is
27854145Scth 	 * is the first point at which the unit-address (or instance) of the
27864145Scth 	 * last component of the path is available (even though the path is
27874145Scth 	 * bound to the wrong driver at this point).
27884145Scth 	 */
27894145Scth 	if (devi->devi_flags & DEVI_REBIND) {
27904145Scth 		p = devi->devi_rebinding_name;
27914145Scth 		major = ddi_name_to_major(p);
279212588SJerry.Gilliam@Sun.COM 		if (driver_active(major)) {
27934145Scth 			if (formp)
27944145Scth 				*formp = p;
27954145Scth 			return (major);
27964145Scth 		}
27974145Scth 
27984145Scth 		/*
27994145Scth 		 * If for some reason devi_rebinding_name no longer resolves
28004145Scth 		 * to a proper driver then clear DEVI_REBIND.
28014145Scth 		 */
28024145Scth 		mutex_enter(&devi->devi_lock);
28034145Scth 		devi->devi_flags &= ~DEVI_REBIND;
28044145Scth 		mutex_exit(&devi->devi_lock);
28054145Scth 	}
28064145Scth 
28070Sstevel@tonic-gate 	/* look up compatible property */
28080Sstevel@tonic-gate 	(void) lookup_compatible(dip, KM_SLEEP);
28090Sstevel@tonic-gate 	compat = (void *)(devi->devi_compat_names);
28100Sstevel@tonic-gate 	len = devi->devi_compat_length;
28110Sstevel@tonic-gate 
28120Sstevel@tonic-gate 	/* find the highest precedence compatible form with a driver binding */
28130Sstevel@tonic-gate 	while ((p = prom_decode_composite_string(compat, len, p)) != NULL) {
28140Sstevel@tonic-gate 		major = ddi_name_to_major(p);
281512588SJerry.Gilliam@Sun.COM 		if (driver_active(major)) {
28160Sstevel@tonic-gate 			if (formp)
28170Sstevel@tonic-gate 				*formp = p;
28180Sstevel@tonic-gate 			return (major);
28190Sstevel@tonic-gate 		}
28200Sstevel@tonic-gate 	}
28210Sstevel@tonic-gate 
28220Sstevel@tonic-gate 	/*
28230Sstevel@tonic-gate 	 * none of the compatible forms have a driver binding, see if
28240Sstevel@tonic-gate 	 * the node name has a driver binding.
28250Sstevel@tonic-gate 	 */
28260Sstevel@tonic-gate 	major = ddi_name_to_major(ddi_node_name(dip));
282712588SJerry.Gilliam@Sun.COM 	if (driver_active(major))
28280Sstevel@tonic-gate 		return (major);
28290Sstevel@tonic-gate 
28300Sstevel@tonic-gate 	/* no driver */
28317009Scth 	return (DDI_MAJOR_T_NONE);
28320Sstevel@tonic-gate }
28330Sstevel@tonic-gate 
28340Sstevel@tonic-gate /*
28350Sstevel@tonic-gate  * Static help functions
28360Sstevel@tonic-gate  */
28370Sstevel@tonic-gate 
28380Sstevel@tonic-gate /*
28390Sstevel@tonic-gate  * lookup the "compatible" property and cache it's contents in the
28400Sstevel@tonic-gate  * device node.
28410Sstevel@tonic-gate  */
28420Sstevel@tonic-gate static int
lookup_compatible(dev_info_t * dip,uint_t flag)28430Sstevel@tonic-gate lookup_compatible(dev_info_t *dip, uint_t flag)
28440Sstevel@tonic-gate {
28450Sstevel@tonic-gate 	int rv;
28460Sstevel@tonic-gate 	int prop_flags;
28470Sstevel@tonic-gate 	uint_t ncompatstrs;
28480Sstevel@tonic-gate 	char **compatstrpp;
28490Sstevel@tonic-gate 	char *di_compat_strp;
28500Sstevel@tonic-gate 	size_t di_compat_strlen;
28510Sstevel@tonic-gate 
28520Sstevel@tonic-gate 	if (DEVI(dip)->devi_compat_names) {
28530Sstevel@tonic-gate 		return (DDI_SUCCESS);
28540Sstevel@tonic-gate 	}
28550Sstevel@tonic-gate 
28560Sstevel@tonic-gate 	prop_flags = DDI_PROP_TYPE_STRING | DDI_PROP_DONTPASS;
28570Sstevel@tonic-gate 
28580Sstevel@tonic-gate 	if (flag & KM_NOSLEEP) {
28590Sstevel@tonic-gate 		prop_flags |= DDI_PROP_DONTSLEEP;
28600Sstevel@tonic-gate 	}
28610Sstevel@tonic-gate 
28620Sstevel@tonic-gate 	if (ndi_dev_is_prom_node(dip) == 0) {
28630Sstevel@tonic-gate 		prop_flags |= DDI_PROP_NOTPROM;
28640Sstevel@tonic-gate 	}
28650Sstevel@tonic-gate 
28660Sstevel@tonic-gate 	rv = ddi_prop_lookup_common(DDI_DEV_T_ANY, dip, prop_flags,
28670Sstevel@tonic-gate 	    "compatible", &compatstrpp, &ncompatstrs,
28680Sstevel@tonic-gate 	    ddi_prop_fm_decode_strings);
28690Sstevel@tonic-gate 
28700Sstevel@tonic-gate 	if (rv == DDI_PROP_NOT_FOUND) {
28710Sstevel@tonic-gate 		return (DDI_SUCCESS);
28720Sstevel@tonic-gate 	}
28730Sstevel@tonic-gate 
28740Sstevel@tonic-gate 	if (rv != DDI_PROP_SUCCESS) {
28750Sstevel@tonic-gate 		return (DDI_FAILURE);
28760Sstevel@tonic-gate 	}
28770Sstevel@tonic-gate 
28780Sstevel@tonic-gate 	/*
28790Sstevel@tonic-gate 	 * encode the compatible property data in the dev_info node
28800Sstevel@tonic-gate 	 */
28810Sstevel@tonic-gate 	rv = DDI_SUCCESS;
28820Sstevel@tonic-gate 	if (ncompatstrs != 0) {
28830Sstevel@tonic-gate 		di_compat_strp = encode_composite_string(compatstrpp,
28840Sstevel@tonic-gate 		    ncompatstrs, &di_compat_strlen, flag);
28850Sstevel@tonic-gate 		if (di_compat_strp != NULL) {
28860Sstevel@tonic-gate 			DEVI(dip)->devi_compat_names = di_compat_strp;
28870Sstevel@tonic-gate 			DEVI(dip)->devi_compat_length = di_compat_strlen;
28880Sstevel@tonic-gate 		} else {
28890Sstevel@tonic-gate 			rv = DDI_FAILURE;
28900Sstevel@tonic-gate 		}
28910Sstevel@tonic-gate 	}
28920Sstevel@tonic-gate 	ddi_prop_free(compatstrpp);
28930Sstevel@tonic-gate 	return (rv);
28940Sstevel@tonic-gate }
28950Sstevel@tonic-gate 
28960Sstevel@tonic-gate /*
28970Sstevel@tonic-gate  * Create a composite string from a list of strings.
28980Sstevel@tonic-gate  *
28990Sstevel@tonic-gate  * A composite string consists of a single buffer containing one
29000Sstevel@tonic-gate  * or more NULL terminated strings.
29010Sstevel@tonic-gate  */
29020Sstevel@tonic-gate static char *
encode_composite_string(char ** strings,uint_t nstrings,size_t * retsz,uint_t flag)29030Sstevel@tonic-gate encode_composite_string(char **strings, uint_t nstrings, size_t *retsz,
29040Sstevel@tonic-gate     uint_t flag)
29050Sstevel@tonic-gate {
29060Sstevel@tonic-gate 	uint_t index;
29070Sstevel@tonic-gate 	char  **strpp;
29080Sstevel@tonic-gate 	uint_t slen;
29090Sstevel@tonic-gate 	size_t cbuf_sz = 0;
29100Sstevel@tonic-gate 	char *cbuf_p;
29110Sstevel@tonic-gate 	char *cbuf_ip;
29120Sstevel@tonic-gate 
29130Sstevel@tonic-gate 	if (strings == NULL || nstrings == 0 || retsz == NULL) {
29140Sstevel@tonic-gate 		return (NULL);
29150Sstevel@tonic-gate 	}
29160Sstevel@tonic-gate 
29170Sstevel@tonic-gate 	for (index = 0, strpp = strings; index < nstrings; index++)
29180Sstevel@tonic-gate 		cbuf_sz += strlen(*(strpp++)) + 1;
29190Sstevel@tonic-gate 
29200Sstevel@tonic-gate 	if ((cbuf_p = kmem_alloc(cbuf_sz, flag)) == NULL) {
29210Sstevel@tonic-gate 		cmn_err(CE_NOTE,
29220Sstevel@tonic-gate 		    "?failed to allocate device node compatstr");
29230Sstevel@tonic-gate 		return (NULL);
29240Sstevel@tonic-gate 	}
29250Sstevel@tonic-gate 
29260Sstevel@tonic-gate 	cbuf_ip = cbuf_p;
29270Sstevel@tonic-gate 	for (index = 0, strpp = strings; index < nstrings; index++) {
29280Sstevel@tonic-gate 		slen = strlen(*strpp);
29290Sstevel@tonic-gate 		bcopy(*(strpp++), cbuf_ip, slen);
29300Sstevel@tonic-gate 		cbuf_ip += slen;
29310Sstevel@tonic-gate 		*(cbuf_ip++) = '\0';
29320Sstevel@tonic-gate 	}
29330Sstevel@tonic-gate 
29340Sstevel@tonic-gate 	*retsz = cbuf_sz;
29350Sstevel@tonic-gate 	return (cbuf_p);
29360Sstevel@tonic-gate }
29370Sstevel@tonic-gate 
29380Sstevel@tonic-gate static void
link_to_driver_list(dev_info_t * dip)29390Sstevel@tonic-gate link_to_driver_list(dev_info_t *dip)
29400Sstevel@tonic-gate {
29410Sstevel@tonic-gate 	major_t major = DEVI(dip)->devi_major;
29420Sstevel@tonic-gate 	struct devnames *dnp;
29430Sstevel@tonic-gate 
29447009Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
29450Sstevel@tonic-gate 
29460Sstevel@tonic-gate 	/*
29470Sstevel@tonic-gate 	 * Remove from orphan list
29480Sstevel@tonic-gate 	 */
29490Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
29500Sstevel@tonic-gate 		dnp = &orphanlist;
29510Sstevel@tonic-gate 		remove_from_dn_list(dnp, dip);
29520Sstevel@tonic-gate 	}
29530Sstevel@tonic-gate 
29540Sstevel@tonic-gate 	/*
29550Sstevel@tonic-gate 	 * Add to per driver list
29560Sstevel@tonic-gate 	 */
29570Sstevel@tonic-gate 	dnp = &devnamesp[major];
29580Sstevel@tonic-gate 	add_to_dn_list(dnp, dip);
29590Sstevel@tonic-gate }
29600Sstevel@tonic-gate 
29610Sstevel@tonic-gate static void
unlink_from_driver_list(dev_info_t * dip)29620Sstevel@tonic-gate unlink_from_driver_list(dev_info_t *dip)
29630Sstevel@tonic-gate {
29640Sstevel@tonic-gate 	major_t major = DEVI(dip)->devi_major;
29650Sstevel@tonic-gate 	struct devnames *dnp;
29660Sstevel@tonic-gate 
29677009Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
29680Sstevel@tonic-gate 
29690Sstevel@tonic-gate 	/*
29700Sstevel@tonic-gate 	 * Remove from per-driver list
29710Sstevel@tonic-gate 	 */
29720Sstevel@tonic-gate 	dnp = &devnamesp[major];
29730Sstevel@tonic-gate 	remove_from_dn_list(dnp, dip);
29740Sstevel@tonic-gate 
29750Sstevel@tonic-gate 	/*
29760Sstevel@tonic-gate 	 * Add to orphan list
29770Sstevel@tonic-gate 	 */
29780Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
29790Sstevel@tonic-gate 		dnp = &orphanlist;
29800Sstevel@tonic-gate 		add_to_dn_list(dnp, dip);
29810Sstevel@tonic-gate 	}
29820Sstevel@tonic-gate }
29830Sstevel@tonic-gate 
29840Sstevel@tonic-gate /*
29850Sstevel@tonic-gate  * scan the per-driver list looking for dev_info "dip"
29860Sstevel@tonic-gate  */
29870Sstevel@tonic-gate static dev_info_t *
in_dn_list(struct devnames * dnp,dev_info_t * dip)29880Sstevel@tonic-gate in_dn_list(struct devnames *dnp, dev_info_t *dip)
29890Sstevel@tonic-gate {
29900Sstevel@tonic-gate 	struct dev_info *idevi;
29910Sstevel@tonic-gate 
29920Sstevel@tonic-gate 	if ((idevi = DEVI(dnp->dn_head)) == NULL)
29930Sstevel@tonic-gate 		return (NULL);
29940Sstevel@tonic-gate 
29950Sstevel@tonic-gate 	while (idevi) {
29960Sstevel@tonic-gate 		if (idevi == DEVI(dip))
29970Sstevel@tonic-gate 			return (dip);
29980Sstevel@tonic-gate 		idevi = idevi->devi_next;
29990Sstevel@tonic-gate 	}
30000Sstevel@tonic-gate 	return (NULL);
30010Sstevel@tonic-gate }
30020Sstevel@tonic-gate 
30030Sstevel@tonic-gate /*
30040Sstevel@tonic-gate  * insert devinfo node 'dip' into the per-driver instance list
30050Sstevel@tonic-gate  * headed by 'dnp'
30060Sstevel@tonic-gate  *
30070Sstevel@tonic-gate  * Nodes on the per-driver list are ordered: HW - SID - PSEUDO.  The order is
30080Sstevel@tonic-gate  * required for merging of .conf file data to work properly.
30090Sstevel@tonic-gate  */
30100Sstevel@tonic-gate static void
add_to_ordered_dn_list(struct devnames * dnp,dev_info_t * dip)30110Sstevel@tonic-gate add_to_ordered_dn_list(struct devnames *dnp, dev_info_t *dip)
30120Sstevel@tonic-gate {
30130Sstevel@tonic-gate 	dev_info_t **dipp;
30140Sstevel@tonic-gate 
30150Sstevel@tonic-gate 	ASSERT(mutex_owned(&(dnp->dn_lock)));
30160Sstevel@tonic-gate 
30170Sstevel@tonic-gate 	dipp = &dnp->dn_head;
30180Sstevel@tonic-gate 	if (ndi_dev_is_prom_node(dip)) {
30190Sstevel@tonic-gate 		/*
30200Sstevel@tonic-gate 		 * Find the first non-prom node or end of list
30210Sstevel@tonic-gate 		 */
30220Sstevel@tonic-gate 		while (*dipp && (ndi_dev_is_prom_node(*dipp) != 0)) {
30230Sstevel@tonic-gate 			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
30240Sstevel@tonic-gate 		}
30250Sstevel@tonic-gate 	} else if (ndi_dev_is_persistent_node(dip)) {
30260Sstevel@tonic-gate 		/*
30270Sstevel@tonic-gate 		 * Find the first non-persistent node
30280Sstevel@tonic-gate 		 */
30290Sstevel@tonic-gate 		while (*dipp && (ndi_dev_is_persistent_node(*dipp) != 0)) {
30300Sstevel@tonic-gate 			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
30310Sstevel@tonic-gate 		}
30320Sstevel@tonic-gate 	} else {
30330Sstevel@tonic-gate 		/*
30340Sstevel@tonic-gate 		 * Find the end of the list
30350Sstevel@tonic-gate 		 */
30360Sstevel@tonic-gate 		while (*dipp) {
30370Sstevel@tonic-gate 			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
30380Sstevel@tonic-gate 		}
30390Sstevel@tonic-gate 	}
30400Sstevel@tonic-gate 
30410Sstevel@tonic-gate 	DEVI(dip)->devi_next = DEVI(*dipp);
30420Sstevel@tonic-gate 	*dipp = dip;
30430Sstevel@tonic-gate }
30440Sstevel@tonic-gate 
30450Sstevel@tonic-gate /*
30460Sstevel@tonic-gate  * add a list of device nodes to the device node list in the
30470Sstevel@tonic-gate  * devnames structure
30480Sstevel@tonic-gate  */
30490Sstevel@tonic-gate static void
add_to_dn_list(struct devnames * dnp,dev_info_t * dip)30500Sstevel@tonic-gate add_to_dn_list(struct devnames *dnp, dev_info_t *dip)
30510Sstevel@tonic-gate {
30520Sstevel@tonic-gate 	/*
30530Sstevel@tonic-gate 	 * Look to see if node already exists
30540Sstevel@tonic-gate 	 */
30550Sstevel@tonic-gate 	LOCK_DEV_OPS(&(dnp->dn_lock));
30560Sstevel@tonic-gate 	if (in_dn_list(dnp, dip)) {
30570Sstevel@tonic-gate 		cmn_err(CE_NOTE, "add_to_dn_list: node %s already in list",
30580Sstevel@tonic-gate 		    DEVI(dip)->devi_node_name);
30590Sstevel@tonic-gate 	} else {
30600Sstevel@tonic-gate 		add_to_ordered_dn_list(dnp, dip);
30610Sstevel@tonic-gate 	}
30620Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
30630Sstevel@tonic-gate }
30640Sstevel@tonic-gate 
30650Sstevel@tonic-gate static void
remove_from_dn_list(struct devnames * dnp,dev_info_t * dip)30660Sstevel@tonic-gate remove_from_dn_list(struct devnames *dnp, dev_info_t *dip)
30670Sstevel@tonic-gate {
30680Sstevel@tonic-gate 	dev_info_t **plist;
30690Sstevel@tonic-gate 
30700Sstevel@tonic-gate 	LOCK_DEV_OPS(&(dnp->dn_lock));
30710Sstevel@tonic-gate 
30720Sstevel@tonic-gate 	plist = (dev_info_t **)&dnp->dn_head;
30730Sstevel@tonic-gate 	while (*plist && (*plist != dip)) {
30740Sstevel@tonic-gate 		plist = (dev_info_t **)&DEVI(*plist)->devi_next;
30750Sstevel@tonic-gate 	}
30760Sstevel@tonic-gate 
30770Sstevel@tonic-gate 	if (*plist != NULL) {
30780Sstevel@tonic-gate 		ASSERT(*plist == dip);
30790Sstevel@tonic-gate 		*plist = (dev_info_t *)(DEVI(dip)->devi_next);
30800Sstevel@tonic-gate 		DEVI(dip)->devi_next = NULL;
30810Sstevel@tonic-gate 	} else {
30820Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE,
30830Sstevel@tonic-gate 		    "remove_from_dn_list: node %s not found in list",
30840Sstevel@tonic-gate 		    DEVI(dip)->devi_node_name));
30850Sstevel@tonic-gate 	}
30860Sstevel@tonic-gate 
30870Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
30880Sstevel@tonic-gate }
30890Sstevel@tonic-gate 
30900Sstevel@tonic-gate /*
30910Sstevel@tonic-gate  * Add and remove reference driver global property list
30920Sstevel@tonic-gate  */
30930Sstevel@tonic-gate static void
add_global_props(dev_info_t * dip)30940Sstevel@tonic-gate add_global_props(dev_info_t *dip)
30950Sstevel@tonic-gate {
30960Sstevel@tonic-gate 	struct devnames *dnp;
30970Sstevel@tonic-gate 	ddi_prop_list_t *plist;
30980Sstevel@tonic-gate 
30990Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_global_prop_list == NULL);
31007009Scth 	ASSERT(DEVI(dip)->devi_major != DDI_MAJOR_T_NONE);
31010Sstevel@tonic-gate 
31020Sstevel@tonic-gate 	dnp = &devnamesp[DEVI(dip)->devi_major];
31030Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
31040Sstevel@tonic-gate 	plist = dnp->dn_global_prop_ptr;
31050Sstevel@tonic-gate 	if (plist == NULL) {
31060Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
31070Sstevel@tonic-gate 		return;
31080Sstevel@tonic-gate 	}
31090Sstevel@tonic-gate 	i_ddi_prop_list_hold(plist, dnp);
31100Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
31110Sstevel@tonic-gate 
31120Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
31130Sstevel@tonic-gate 	DEVI(dip)->devi_global_prop_list = plist;
31140Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
31150Sstevel@tonic-gate }
31160Sstevel@tonic-gate 
31170Sstevel@tonic-gate static void
remove_global_props(dev_info_t * dip)31180Sstevel@tonic-gate remove_global_props(dev_info_t *dip)
31190Sstevel@tonic-gate {
31200Sstevel@tonic-gate 	ddi_prop_list_t *proplist;
31210Sstevel@tonic-gate 
31220Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
31230Sstevel@tonic-gate 	proplist = DEVI(dip)->devi_global_prop_list;
31240Sstevel@tonic-gate 	DEVI(dip)->devi_global_prop_list = NULL;
31250Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
31260Sstevel@tonic-gate 
31270Sstevel@tonic-gate 	if (proplist) {
31280Sstevel@tonic-gate 		major_t major;
31290Sstevel@tonic-gate 		struct devnames *dnp;
31300Sstevel@tonic-gate 
31310Sstevel@tonic-gate 		major = ddi_driver_major(dip);
31327009Scth 		ASSERT(major != DDI_MAJOR_T_NONE);
31330Sstevel@tonic-gate 		dnp = &devnamesp[major];
31340Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
31350Sstevel@tonic-gate 		i_ddi_prop_list_rele(proplist, dnp);
31360Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
31370Sstevel@tonic-gate 	}
31380Sstevel@tonic-gate }
31390Sstevel@tonic-gate 
31400Sstevel@tonic-gate #ifdef DEBUG
31410Sstevel@tonic-gate /*
31420Sstevel@tonic-gate  * Set this variable to '0' to disable the optimization,
31430Sstevel@tonic-gate  * and to 2 to print debug message.
31440Sstevel@tonic-gate  */
31450Sstevel@tonic-gate static int optimize_dtree = 1;
31460Sstevel@tonic-gate 
31470Sstevel@tonic-gate static void
debug_dtree(dev_info_t * devi,struct dev_info * adevi,char * service)31480Sstevel@tonic-gate debug_dtree(dev_info_t *devi, struct dev_info *adevi, char *service)
31490Sstevel@tonic-gate {
31500Sstevel@tonic-gate 	char *adeviname, *buf;
31510Sstevel@tonic-gate 
31520Sstevel@tonic-gate 	/*
31530Sstevel@tonic-gate 	 * Don't print unless optimize dtree is set to 2+
31540Sstevel@tonic-gate 	 */
31550Sstevel@tonic-gate 	if (optimize_dtree <= 1)
31560Sstevel@tonic-gate 		return;
31570Sstevel@tonic-gate 
31580Sstevel@tonic-gate 	buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
31590Sstevel@tonic-gate 	adeviname = ddi_deviname((dev_info_t *)adevi, buf);
31600Sstevel@tonic-gate 	if (*adeviname == '\0')
31610Sstevel@tonic-gate 		adeviname = "root";
31620Sstevel@tonic-gate 
31630Sstevel@tonic-gate 	cmn_err(CE_CONT, "%s %s -> %s\n",
31640Sstevel@tonic-gate 	    ddi_deviname(devi, buf), service, adeviname);
31650Sstevel@tonic-gate 
31660Sstevel@tonic-gate 	kmem_free(buf, MAXNAMELEN);
31670Sstevel@tonic-gate }
31680Sstevel@tonic-gate #else /* DEBUG */
31690Sstevel@tonic-gate #define	debug_dtree(a1, a2, a3)	 /* nothing */
317010923SEvan.Yan@Sun.COM #endif	/* DEBUG */
31710Sstevel@tonic-gate 
31720Sstevel@tonic-gate static void
ddi_optimize_dtree(dev_info_t * devi)31730Sstevel@tonic-gate ddi_optimize_dtree(dev_info_t *devi)
31740Sstevel@tonic-gate {
31750Sstevel@tonic-gate 	struct dev_info *pdevi;
31760Sstevel@tonic-gate 	struct bus_ops *b;
31770Sstevel@tonic-gate 
31780Sstevel@tonic-gate 	pdevi = DEVI(devi)->devi_parent;
31790Sstevel@tonic-gate 	ASSERT(pdevi);
31800Sstevel@tonic-gate 
31810Sstevel@tonic-gate 	/*
31820Sstevel@tonic-gate 	 * Set the unoptimized values
31830Sstevel@tonic-gate 	 */
31840Sstevel@tonic-gate 	DEVI(devi)->devi_bus_map_fault = pdevi;
31850Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_map = pdevi;
31860Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_allochdl = pdevi;
31870Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_freehdl = pdevi;
31880Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_bindhdl = pdevi;
31890Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_bindfunc =
31904950Scth 	    pdevi->devi_ops->devo_bus_ops->bus_dma_bindhdl;
31910Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_unbindhdl = pdevi;
31920Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_unbindfunc =
31930Sstevel@tonic-gate 	    pdevi->devi_ops->devo_bus_ops->bus_dma_unbindhdl;
31940Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_flush = pdevi;
31950Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_win = pdevi;
31960Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_ctl = pdevi;
31970Sstevel@tonic-gate 	DEVI(devi)->devi_bus_ctl = pdevi;
31980Sstevel@tonic-gate 
31990Sstevel@tonic-gate #ifdef DEBUG
32000Sstevel@tonic-gate 	if (optimize_dtree == 0)
32010Sstevel@tonic-gate 		return;
32020Sstevel@tonic-gate #endif /* DEBUG */
32030Sstevel@tonic-gate 
32040Sstevel@tonic-gate 	b = pdevi->devi_ops->devo_bus_ops;
32050Sstevel@tonic-gate 
32060Sstevel@tonic-gate 	if (i_ddi_map_fault == b->bus_map_fault) {
32070Sstevel@tonic-gate 		DEVI(devi)->devi_bus_map_fault = pdevi->devi_bus_map_fault;
32080Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_map_fault,
32090Sstevel@tonic-gate 		    "bus_map_fault");
32100Sstevel@tonic-gate 	}
32110Sstevel@tonic-gate 
32120Sstevel@tonic-gate 	if (ddi_dma_map == b->bus_dma_map) {
32130Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_map = pdevi->devi_bus_dma_map;
32140Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_map, "bus_dma_map");
32150Sstevel@tonic-gate 	}
32160Sstevel@tonic-gate 
32170Sstevel@tonic-gate 	if (ddi_dma_allochdl == b->bus_dma_allochdl) {
32180Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_allochdl =
32190Sstevel@tonic-gate 		    pdevi->devi_bus_dma_allochdl;
32200Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_allochdl,
32210Sstevel@tonic-gate 		    "bus_dma_allochdl");
32220Sstevel@tonic-gate 	}
32230Sstevel@tonic-gate 
32240Sstevel@tonic-gate 	if (ddi_dma_freehdl == b->bus_dma_freehdl) {
32250Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_freehdl = pdevi->devi_bus_dma_freehdl;
32260Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_freehdl,
32270Sstevel@tonic-gate 		    "bus_dma_freehdl");
32280Sstevel@tonic-gate 	}
32290Sstevel@tonic-gate 
32300Sstevel@tonic-gate 	if (ddi_dma_bindhdl == b->bus_dma_bindhdl) {
32310Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_bindhdl = pdevi->devi_bus_dma_bindhdl;
32320Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_bindfunc =
32330Sstevel@tonic-gate 		    pdevi->devi_bus_dma_bindhdl->devi_ops->
32340Sstevel@tonic-gate 		    devo_bus_ops->bus_dma_bindhdl;
32350Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_bindhdl,
32360Sstevel@tonic-gate 		    "bus_dma_bindhdl");
32370Sstevel@tonic-gate 	}
32380Sstevel@tonic-gate 
32390Sstevel@tonic-gate 	if (ddi_dma_unbindhdl == b->bus_dma_unbindhdl) {
32400Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_unbindhdl =
32410Sstevel@tonic-gate 		    pdevi->devi_bus_dma_unbindhdl;
32420Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_unbindfunc =
32430Sstevel@tonic-gate 		    pdevi->devi_bus_dma_unbindhdl->devi_ops->
32440Sstevel@tonic-gate 		    devo_bus_ops->bus_dma_unbindhdl;
32450Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_unbindhdl,
32460Sstevel@tonic-gate 		    "bus_dma_unbindhdl");
32470Sstevel@tonic-gate 	}
32480Sstevel@tonic-gate 
32490Sstevel@tonic-gate 	if (ddi_dma_flush == b->bus_dma_flush) {
32500Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_flush = pdevi->devi_bus_dma_flush;
32510Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_flush,
32520Sstevel@tonic-gate 		    "bus_dma_flush");
32530Sstevel@tonic-gate 	}
32540Sstevel@tonic-gate 
32550Sstevel@tonic-gate 	if (ddi_dma_win == b->bus_dma_win) {
32560Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_win = pdevi->devi_bus_dma_win;
32570Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_win,
32580Sstevel@tonic-gate 		    "bus_dma_win");
32590Sstevel@tonic-gate 	}
32600Sstevel@tonic-gate 
32610Sstevel@tonic-gate 	if (ddi_dma_mctl == b->bus_dma_ctl) {
32620Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_ctl = pdevi->devi_bus_dma_ctl;
32630Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_ctl, "bus_dma_ctl");
32640Sstevel@tonic-gate 	}
32650Sstevel@tonic-gate 
32660Sstevel@tonic-gate 	if (ddi_ctlops == b->bus_ctl) {
32670Sstevel@tonic-gate 		DEVI(devi)->devi_bus_ctl = pdevi->devi_bus_ctl;
32680Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_ctl, "bus_ctl");
32690Sstevel@tonic-gate 	}
32700Sstevel@tonic-gate }
32710Sstevel@tonic-gate 
32720Sstevel@tonic-gate #define	MIN_DEVINFO_LOG_SIZE	max_ncpus
32730Sstevel@tonic-gate #define	MAX_DEVINFO_LOG_SIZE	max_ncpus * 10
32740Sstevel@tonic-gate 
32750Sstevel@tonic-gate static void
da_log_init()32760Sstevel@tonic-gate da_log_init()
32770Sstevel@tonic-gate {
32780Sstevel@tonic-gate 	devinfo_log_header_t *dh;
32790Sstevel@tonic-gate 	int logsize = devinfo_log_size;
32800Sstevel@tonic-gate 
32810Sstevel@tonic-gate 	if (logsize == 0)
32820Sstevel@tonic-gate 		logsize = MIN_DEVINFO_LOG_SIZE;
32830Sstevel@tonic-gate 	else if (logsize > MAX_DEVINFO_LOG_SIZE)
32840Sstevel@tonic-gate 		logsize = MAX_DEVINFO_LOG_SIZE;
32850Sstevel@tonic-gate 
32860Sstevel@tonic-gate 	dh = kmem_alloc(logsize * PAGESIZE, KM_SLEEP);
32870Sstevel@tonic-gate 	mutex_init(&dh->dh_lock, NULL, MUTEX_DEFAULT, NULL);
32880Sstevel@tonic-gate 	dh->dh_max = ((logsize * PAGESIZE) - sizeof (*dh)) /
32890Sstevel@tonic-gate 	    sizeof (devinfo_audit_t) + 1;
32900Sstevel@tonic-gate 	dh->dh_curr = -1;
32910Sstevel@tonic-gate 	dh->dh_hits = 0;
32920Sstevel@tonic-gate 
32930Sstevel@tonic-gate 	devinfo_audit_log = dh;
32940Sstevel@tonic-gate }
32950Sstevel@tonic-gate 
32960Sstevel@tonic-gate /*
32970Sstevel@tonic-gate  * Log the stack trace in per-devinfo audit structure and also enter
32980Sstevel@tonic-gate  * it into a system wide log for recording the time history.
32990Sstevel@tonic-gate  */
33000Sstevel@tonic-gate static void
da_log_enter(dev_info_t * dip)33010Sstevel@tonic-gate da_log_enter(dev_info_t *dip)
33020Sstevel@tonic-gate {
33030Sstevel@tonic-gate 	devinfo_audit_t *da_log, *da = DEVI(dip)->devi_audit;
33040Sstevel@tonic-gate 	devinfo_log_header_t *dh = devinfo_audit_log;
33050Sstevel@tonic-gate 
33060Sstevel@tonic-gate 	if (devinfo_audit_log == NULL)
33070Sstevel@tonic-gate 		return;
33080Sstevel@tonic-gate 
33090Sstevel@tonic-gate 	ASSERT(da != NULL);
33100Sstevel@tonic-gate 
33110Sstevel@tonic-gate 	da->da_devinfo = dip;
33120Sstevel@tonic-gate 	da->da_timestamp = gethrtime();
33130Sstevel@tonic-gate 	da->da_thread = curthread;
33140Sstevel@tonic-gate 	da->da_node_state = DEVI(dip)->devi_node_state;
33150Sstevel@tonic-gate 	da->da_device_state = DEVI(dip)->devi_state;
33160Sstevel@tonic-gate 	da->da_depth = getpcstack(da->da_stack, DDI_STACK_DEPTH);
33170Sstevel@tonic-gate 
33180Sstevel@tonic-gate 	/*
33190Sstevel@tonic-gate 	 * Copy into common log and note the location for tracing history
33200Sstevel@tonic-gate 	 */
33210Sstevel@tonic-gate 	mutex_enter(&dh->dh_lock);
33220Sstevel@tonic-gate 	dh->dh_hits++;
33230Sstevel@tonic-gate 	dh->dh_curr++;
33240Sstevel@tonic-gate 	if (dh->dh_curr >= dh->dh_max)
33250Sstevel@tonic-gate 		dh->dh_curr -= dh->dh_max;
33260Sstevel@tonic-gate 	da_log = &dh->dh_entry[dh->dh_curr];
33270Sstevel@tonic-gate 	mutex_exit(&dh->dh_lock);
33280Sstevel@tonic-gate 
33290Sstevel@tonic-gate 	bcopy(da, da_log, sizeof (devinfo_audit_t));
33300Sstevel@tonic-gate 	da->da_lastlog = da_log;
33310Sstevel@tonic-gate }
33320Sstevel@tonic-gate 
33330Sstevel@tonic-gate static void
attach_drivers()33340Sstevel@tonic-gate attach_drivers()
33350Sstevel@tonic-gate {
33360Sstevel@tonic-gate 	int i;
33370Sstevel@tonic-gate 	for (i = 0; i < devcnt; i++) {
33380Sstevel@tonic-gate 		struct devnames *dnp = &devnamesp[i];
33390Sstevel@tonic-gate 		if ((dnp->dn_flags & DN_FORCE_ATTACH) &&
33400Sstevel@tonic-gate 		    (ddi_hold_installed_driver((major_t)i) != NULL))
33410Sstevel@tonic-gate 			ddi_rele_driver((major_t)i);
33420Sstevel@tonic-gate 	}
33430Sstevel@tonic-gate }
33440Sstevel@tonic-gate 
33450Sstevel@tonic-gate /*
33460Sstevel@tonic-gate  * Launch a thread to force attach drivers. This avoids penalty on boot time.
33470Sstevel@tonic-gate  */
33480Sstevel@tonic-gate void
i_ddi_forceattach_drivers()33490Sstevel@tonic-gate i_ddi_forceattach_drivers()
33500Sstevel@tonic-gate {
33510Sstevel@tonic-gate 
33521093Shiremath 	/*
33531093Shiremath 	 * Attach IB VHCI driver before the force-attach thread attaches the
33541093Shiremath 	 * IB HCA driver. IB HCA driver will fail if IB Nexus has not yet
33551093Shiremath 	 * been attached.
33561093Shiremath 	 */
33571093Shiremath 	(void) ddi_hold_installed_driver(ddi_name_to_major("ib"));
33581093Shiremath 
33590Sstevel@tonic-gate 	(void) thread_create(NULL, 0, (void (*)())attach_drivers, NULL, 0, &p0,
33600Sstevel@tonic-gate 	    TS_RUN, minclsyspri);
33610Sstevel@tonic-gate }
33620Sstevel@tonic-gate 
33630Sstevel@tonic-gate /*
33640Sstevel@tonic-gate  * This is a private DDI interface for optimizing boot performance.
33650Sstevel@tonic-gate  * I/O subsystem initialization is considered complete when devfsadm
33660Sstevel@tonic-gate  * is executed.
33670Sstevel@tonic-gate  *
33682621Sllai1  * NOTE: The start of syseventd happens to be a convenient indicator
33692621Sllai1  *	of the completion of I/O initialization during boot.
33700Sstevel@tonic-gate  *	The implementation should be replaced by something more robust.
33710Sstevel@tonic-gate  */
33720Sstevel@tonic-gate int
i_ddi_io_initialized()33730Sstevel@tonic-gate i_ddi_io_initialized()
33740Sstevel@tonic-gate {
33750Sstevel@tonic-gate 	extern int sysevent_daemon_init;
33760Sstevel@tonic-gate 	return (sysevent_daemon_init);
33770Sstevel@tonic-gate }
33780Sstevel@tonic-gate 
33792621Sllai1 /*
33802621Sllai1  * May be used to determine system boot state
33812621Sllai1  * "Available" means the system is for the most part up
33822621Sllai1  * and initialized, with all system services either up or
33832621Sllai1  * capable of being started.  This state is set by devfsadm
33842621Sllai1  * during the boot process.  The /dev filesystem infers
33852621Sllai1  * from this when implicit reconfig can be performed,
33862621Sllai1  * ie, devfsadm can be invoked.  Please avoid making
33872621Sllai1  * further use of this unless it's really necessary.
33882621Sllai1  */
33892621Sllai1 int
i_ddi_sysavail()33902621Sllai1 i_ddi_sysavail()
33912621Sllai1 {
33922621Sllai1 	return (devname_state & DS_SYSAVAIL);
33932621Sllai1 }
33942621Sllai1 
33952621Sllai1 /*
33962621Sllai1  * May be used to determine if boot is a reconfigure boot.
33972621Sllai1  */
33982621Sllai1 int
i_ddi_reconfig()33992621Sllai1 i_ddi_reconfig()
34002621Sllai1 {
34012621Sllai1 	return (devname_state & DS_RECONFIG);
34022621Sllai1 }
34032621Sllai1 
34042621Sllai1 /*
34052621Sllai1  * Note system services are up, inform /dev.
34062621Sllai1  */
34072621Sllai1 void
i_ddi_set_sysavail()34082621Sllai1 i_ddi_set_sysavail()
34092621Sllai1 {
34102621Sllai1 	if ((devname_state & DS_SYSAVAIL) == 0) {
34112621Sllai1 		devname_state |= DS_SYSAVAIL;
34122621Sllai1 		sdev_devstate_change();
34132621Sllai1 	}
34142621Sllai1 }
34152621Sllai1 
34162621Sllai1 /*
34172621Sllai1  * Note reconfiguration boot, inform /dev.
34182621Sllai1  */
34192621Sllai1 void
i_ddi_set_reconfig()34202621Sllai1 i_ddi_set_reconfig()
34212621Sllai1 {
34222621Sllai1 	if ((devname_state & DS_RECONFIG) == 0) {
34232621Sllai1 		devname_state |= DS_RECONFIG;
34242621Sllai1 		sdev_devstate_change();
34252621Sllai1 	}
34262621Sllai1 }
34272621Sllai1 
34280Sstevel@tonic-gate 
34290Sstevel@tonic-gate /*
34300Sstevel@tonic-gate  * device tree walking
34310Sstevel@tonic-gate  */
34320Sstevel@tonic-gate 
34330Sstevel@tonic-gate struct walk_elem {
34340Sstevel@tonic-gate 	struct walk_elem *next;
34350Sstevel@tonic-gate 	dev_info_t *dip;
34360Sstevel@tonic-gate };
34370Sstevel@tonic-gate 
34380Sstevel@tonic-gate static void
free_list(struct walk_elem * list)34390Sstevel@tonic-gate free_list(struct walk_elem *list)
34400Sstevel@tonic-gate {
34410Sstevel@tonic-gate 	while (list) {
34420Sstevel@tonic-gate 		struct walk_elem *next = list->next;
34430Sstevel@tonic-gate 		kmem_free(list, sizeof (*list));
34440Sstevel@tonic-gate 		list = next;
34450Sstevel@tonic-gate 	}
34460Sstevel@tonic-gate }
34470Sstevel@tonic-gate 
34480Sstevel@tonic-gate static void
append_node(struct walk_elem ** list,dev_info_t * dip)34490Sstevel@tonic-gate append_node(struct walk_elem **list, dev_info_t *dip)
34500Sstevel@tonic-gate {
34510Sstevel@tonic-gate 	struct walk_elem *tail;
34520Sstevel@tonic-gate 	struct walk_elem *elem = kmem_alloc(sizeof (*elem), KM_SLEEP);
34530Sstevel@tonic-gate 
34540Sstevel@tonic-gate 	elem->next = NULL;
34550Sstevel@tonic-gate 	elem->dip = dip;
34560Sstevel@tonic-gate 
34570Sstevel@tonic-gate 	if (*list == NULL) {
34580Sstevel@tonic-gate 		*list = elem;
34590Sstevel@tonic-gate 		return;
34600Sstevel@tonic-gate 	}
34610Sstevel@tonic-gate 
34620Sstevel@tonic-gate 	tail = *list;
34630Sstevel@tonic-gate 	while (tail->next)
34640Sstevel@tonic-gate 		tail = tail->next;
34650Sstevel@tonic-gate 
34660Sstevel@tonic-gate 	tail->next = elem;
34670Sstevel@tonic-gate }
34680Sstevel@tonic-gate 
34690Sstevel@tonic-gate /*
34700Sstevel@tonic-gate  * The implementation of ddi_walk_devs().
34710Sstevel@tonic-gate  */
34720Sstevel@tonic-gate static int
walk_devs(dev_info_t * dip,int (* f)(dev_info_t *,void *),void * arg,int do_locking)34730Sstevel@tonic-gate walk_devs(dev_info_t *dip, int (*f)(dev_info_t *, void *), void *arg,
34740Sstevel@tonic-gate     int do_locking)
34750Sstevel@tonic-gate {
34760Sstevel@tonic-gate 	struct walk_elem *head = NULL;
34770Sstevel@tonic-gate 
34780Sstevel@tonic-gate 	/*
34790Sstevel@tonic-gate 	 * Do it in two passes. First pass invoke callback on each
34800Sstevel@tonic-gate 	 * dip on the sibling list. Second pass invoke callback on
34810Sstevel@tonic-gate 	 * children of each dip.
34820Sstevel@tonic-gate 	 */
34830Sstevel@tonic-gate 	while (dip) {
34840Sstevel@tonic-gate 		switch ((*f)(dip, arg)) {
34850Sstevel@tonic-gate 		case DDI_WALK_TERMINATE:
34860Sstevel@tonic-gate 			free_list(head);
34870Sstevel@tonic-gate 			return (DDI_WALK_TERMINATE);
34880Sstevel@tonic-gate 
34890Sstevel@tonic-gate 		case DDI_WALK_PRUNESIB:
34900Sstevel@tonic-gate 			/* ignore sibling by setting dip to NULL */
34910Sstevel@tonic-gate 			append_node(&head, dip);
34920Sstevel@tonic-gate 			dip = NULL;
34930Sstevel@tonic-gate 			break;
34940Sstevel@tonic-gate 
34950Sstevel@tonic-gate 		case DDI_WALK_PRUNECHILD:
34960Sstevel@tonic-gate 			/* don't worry about children */
34970Sstevel@tonic-gate 			dip = ddi_get_next_sibling(dip);
34980Sstevel@tonic-gate 			break;
34990Sstevel@tonic-gate 
35000Sstevel@tonic-gate 		case DDI_WALK_CONTINUE:
35010Sstevel@tonic-gate 		default:
35020Sstevel@tonic-gate 			append_node(&head, dip);
35030Sstevel@tonic-gate 			dip = ddi_get_next_sibling(dip);
35040Sstevel@tonic-gate 			break;
35050Sstevel@tonic-gate 		}
35060Sstevel@tonic-gate 
35070Sstevel@tonic-gate 	}
35080Sstevel@tonic-gate 
35090Sstevel@tonic-gate 	/* second pass */
35100Sstevel@tonic-gate 	while (head) {
35110Sstevel@tonic-gate 		int circ;
35120Sstevel@tonic-gate 		struct walk_elem *next = head->next;
35130Sstevel@tonic-gate 
35140Sstevel@tonic-gate 		if (do_locking)
35150Sstevel@tonic-gate 			ndi_devi_enter(head->dip, &circ);
35160Sstevel@tonic-gate 		if (walk_devs(ddi_get_child(head->dip), f, arg, do_locking) ==
35170Sstevel@tonic-gate 		    DDI_WALK_TERMINATE) {
35180Sstevel@tonic-gate 			if (do_locking)
35190Sstevel@tonic-gate 				ndi_devi_exit(head->dip, circ);
35200Sstevel@tonic-gate 			free_list(head);
35210Sstevel@tonic-gate 			return (DDI_WALK_TERMINATE);
35220Sstevel@tonic-gate 		}
35230Sstevel@tonic-gate 		if (do_locking)
35240Sstevel@tonic-gate 			ndi_devi_exit(head->dip, circ);
35250Sstevel@tonic-gate 		kmem_free(head, sizeof (*head));
35260Sstevel@tonic-gate 		head = next;
35270Sstevel@tonic-gate 	}
35280Sstevel@tonic-gate 
35290Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
35300Sstevel@tonic-gate }
35310Sstevel@tonic-gate 
35320Sstevel@tonic-gate /*
35330Sstevel@tonic-gate  * This general-purpose routine traverses the tree of dev_info nodes,
35340Sstevel@tonic-gate  * starting from the given node, and calls the given function for each
35350Sstevel@tonic-gate  * node that it finds with the current node and the pointer arg (which
35360Sstevel@tonic-gate  * can point to a structure of information that the function
35370Sstevel@tonic-gate  * needs) as arguments.
35380Sstevel@tonic-gate  *
35390Sstevel@tonic-gate  * It does the walk a layer at a time, not depth-first. The given function
35400Sstevel@tonic-gate  * must return one of the following values:
35410Sstevel@tonic-gate  *	DDI_WALK_CONTINUE
35420Sstevel@tonic-gate  *	DDI_WALK_PRUNESIB
35430Sstevel@tonic-gate  *	DDI_WALK_PRUNECHILD
35440Sstevel@tonic-gate  *	DDI_WALK_TERMINATE
35450Sstevel@tonic-gate  *
35460Sstevel@tonic-gate  * N.B. Since we walk the sibling list, the caller must ensure that
35470Sstevel@tonic-gate  *	the parent of dip is held against changes, unless the parent
35480Sstevel@tonic-gate  *	is rootnode.  ndi_devi_enter() on the parent is sufficient.
35490Sstevel@tonic-gate  *
35500Sstevel@tonic-gate  *	To avoid deadlock situations, caller must not attempt to
35510Sstevel@tonic-gate  *	configure/unconfigure/remove device node in (*f)(), nor should
35522155Scth  *	it attempt to recurse on other nodes in the system. Any
35532155Scth  *	ndi_devi_enter() done by (*f)() must occur 'at-or-below' the
35542155Scth  *	node entered prior to ddi_walk_devs(). Furthermore, if (*f)()
35552155Scth  *	does any multi-threading (in framework *or* in driver) then the
35562155Scth  *	ndi_devi_enter() calls done by dependent threads must be
35572155Scth  *	'strictly-below'.
35580Sstevel@tonic-gate  *
35590Sstevel@tonic-gate  *	This is not callable from device autoconfiguration routines.
35600Sstevel@tonic-gate  *	They include, but not limited to, _init(9e), _fini(9e), probe(9e),
35610Sstevel@tonic-gate  *	attach(9e), and detach(9e).
35620Sstevel@tonic-gate  */
35630Sstevel@tonic-gate 
35640Sstevel@tonic-gate void
ddi_walk_devs(dev_info_t * dip,int (* f)(dev_info_t *,void *),void * arg)35650Sstevel@tonic-gate ddi_walk_devs(dev_info_t *dip, int (*f)(dev_info_t *, void *), void *arg)
35660Sstevel@tonic-gate {
35670Sstevel@tonic-gate 
35680Sstevel@tonic-gate 	ASSERT(dip == NULL || ddi_get_parent(dip) == NULL ||
35694950Scth 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
35700Sstevel@tonic-gate 
35710Sstevel@tonic-gate 	(void) walk_devs(dip, f, arg, 1);
35720Sstevel@tonic-gate }
35730Sstevel@tonic-gate 
35740Sstevel@tonic-gate /*
35750Sstevel@tonic-gate  * This is a general-purpose routine traverses the per-driver list
35760Sstevel@tonic-gate  * and calls the given function for each node. must return one of
35770Sstevel@tonic-gate  * the following values:
35780Sstevel@tonic-gate  *	DDI_WALK_CONTINUE
35790Sstevel@tonic-gate  *	DDI_WALK_TERMINATE
35800Sstevel@tonic-gate  *
35810Sstevel@tonic-gate  * N.B. The same restrictions from ddi_walk_devs() apply.
35820Sstevel@tonic-gate  */
35830Sstevel@tonic-gate 
35840Sstevel@tonic-gate void
e_ddi_walk_driver(char * drv,int (* f)(dev_info_t *,void *),void * arg)35850Sstevel@tonic-gate e_ddi_walk_driver(char *drv, int (*f)(dev_info_t *, void *), void *arg)
35860Sstevel@tonic-gate {
35870Sstevel@tonic-gate 	major_t major;
35880Sstevel@tonic-gate 	struct devnames *dnp;
35890Sstevel@tonic-gate 	dev_info_t *dip;
35900Sstevel@tonic-gate 
35910Sstevel@tonic-gate 	major = ddi_name_to_major(drv);
35927009Scth 	if (major == DDI_MAJOR_T_NONE)
35930Sstevel@tonic-gate 		return;
35940Sstevel@tonic-gate 
35950Sstevel@tonic-gate 	dnp = &devnamesp[major];
35960Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
35970Sstevel@tonic-gate 	dip = dnp->dn_head;
35980Sstevel@tonic-gate 	while (dip) {
35990Sstevel@tonic-gate 		ndi_hold_devi(dip);
36000Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
36010Sstevel@tonic-gate 		if ((*f)(dip, arg) == DDI_WALK_TERMINATE) {
36020Sstevel@tonic-gate 			ndi_rele_devi(dip);
36030Sstevel@tonic-gate 			return;
36040Sstevel@tonic-gate 		}
36050Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
36060Sstevel@tonic-gate 		ndi_rele_devi(dip);
36070Sstevel@tonic-gate 		dip = ddi_get_next(dip);
36080Sstevel@tonic-gate 	}
36090Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
36100Sstevel@tonic-gate }
36110Sstevel@tonic-gate 
36120Sstevel@tonic-gate /*
36130Sstevel@tonic-gate  * argument to i_find_devi, a devinfo node search callback function.
36140Sstevel@tonic-gate  */
36150Sstevel@tonic-gate struct match_info {
36160Sstevel@tonic-gate 	dev_info_t	*dip;		/* result */
36170Sstevel@tonic-gate 	char		*nodename;	/* if non-null, nodename must match */
36180Sstevel@tonic-gate 	int		instance;	/* if != -1, instance must match */
36191333Scth 	int		attached;	/* if != 0, i_ddi_devi_attached() */
36200Sstevel@tonic-gate };
36210Sstevel@tonic-gate 
36220Sstevel@tonic-gate static int
i_find_devi(dev_info_t * dip,void * arg)36230Sstevel@tonic-gate i_find_devi(dev_info_t *dip, void *arg)
36240Sstevel@tonic-gate {
36250Sstevel@tonic-gate 	struct match_info *info = (struct match_info *)arg;
36260Sstevel@tonic-gate 
36270Sstevel@tonic-gate 	if (((info->nodename == NULL) ||
36284950Scth 	    (strcmp(ddi_node_name(dip), info->nodename) == 0)) &&
36290Sstevel@tonic-gate 	    ((info->instance == -1) ||
36304950Scth 	    (ddi_get_instance(dip) == info->instance)) &&
36311333Scth 	    ((info->attached == 0) || i_ddi_devi_attached(dip))) {
36320Sstevel@tonic-gate 		info->dip = dip;
36330Sstevel@tonic-gate 		ndi_hold_devi(dip);
36340Sstevel@tonic-gate 		return (DDI_WALK_TERMINATE);
36350Sstevel@tonic-gate 	}
36360Sstevel@tonic-gate 
36370Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
36380Sstevel@tonic-gate }
36390Sstevel@tonic-gate 
36400Sstevel@tonic-gate /*
36410Sstevel@tonic-gate  * Find dip with a known node name and instance and return with it held
36420Sstevel@tonic-gate  */
36430Sstevel@tonic-gate dev_info_t *
ddi_find_devinfo(char * nodename,int instance,int attached)36440Sstevel@tonic-gate ddi_find_devinfo(char *nodename, int instance, int attached)
36450Sstevel@tonic-gate {
36460Sstevel@tonic-gate 	struct match_info	info;
36470Sstevel@tonic-gate 
36480Sstevel@tonic-gate 	info.nodename = nodename;
36490Sstevel@tonic-gate 	info.instance = instance;
36500Sstevel@tonic-gate 	info.attached = attached;
36510Sstevel@tonic-gate 	info.dip = NULL;
36520Sstevel@tonic-gate 
36530Sstevel@tonic-gate 	ddi_walk_devs(ddi_root_node(), i_find_devi, &info);
36540Sstevel@tonic-gate 	return (info.dip);
36550Sstevel@tonic-gate }
36560Sstevel@tonic-gate 
365710822SJack.Meng@Sun.COM extern ib_boot_prop_t *iscsiboot_prop;
365810822SJack.Meng@Sun.COM static void
i_ddi_parse_iscsi_name(char * name,char ** nodename,char ** addrname,char ** minorname)365910822SJack.Meng@Sun.COM i_ddi_parse_iscsi_name(char *name, char **nodename, char **addrname,
366010822SJack.Meng@Sun.COM     char **minorname)
366110822SJack.Meng@Sun.COM {
366210822SJack.Meng@Sun.COM 	char *cp, *colon;
366310822SJack.Meng@Sun.COM 	static char nulladdrname[] = "";
366410822SJack.Meng@Sun.COM 
366510822SJack.Meng@Sun.COM 	/* default values */
366610822SJack.Meng@Sun.COM 	if (nodename)
366710822SJack.Meng@Sun.COM 		*nodename = name;
366810822SJack.Meng@Sun.COM 	if (addrname)
366910822SJack.Meng@Sun.COM 		*addrname = nulladdrname;
367010822SJack.Meng@Sun.COM 	if (minorname)
367110822SJack.Meng@Sun.COM 		*minorname = NULL;
367210822SJack.Meng@Sun.COM 
367310822SJack.Meng@Sun.COM 	cp = colon = name;
367410822SJack.Meng@Sun.COM 	while (*cp != '\0') {
367510822SJack.Meng@Sun.COM 		if (addrname && *cp == '@') {
367610822SJack.Meng@Sun.COM 			*addrname = cp + 1;
367710822SJack.Meng@Sun.COM 			*cp = '\0';
367810822SJack.Meng@Sun.COM 		} else if (minorname && *cp == ':') {
367910822SJack.Meng@Sun.COM 			*minorname = cp + 1;
368010822SJack.Meng@Sun.COM 			colon = cp;
368110822SJack.Meng@Sun.COM 		}
368210822SJack.Meng@Sun.COM 		++cp;
368310822SJack.Meng@Sun.COM 	}
368410822SJack.Meng@Sun.COM 	if (colon != name) {
368510822SJack.Meng@Sun.COM 		*colon = '\0';
368610822SJack.Meng@Sun.COM 	}
368710822SJack.Meng@Sun.COM }
368810822SJack.Meng@Sun.COM 
36890Sstevel@tonic-gate /*
36900Sstevel@tonic-gate  * Parse for name, addr, and minor names. Some args may be NULL.
36910Sstevel@tonic-gate  */
36920Sstevel@tonic-gate void
i_ddi_parse_name(char * name,char ** nodename,char ** addrname,char ** minorname)36930Sstevel@tonic-gate i_ddi_parse_name(char *name, char **nodename, char **addrname, char **minorname)
36940Sstevel@tonic-gate {
36950Sstevel@tonic-gate 	char *cp;
36960Sstevel@tonic-gate 	static char nulladdrname[] = "";
36970Sstevel@tonic-gate 
36980Sstevel@tonic-gate 	/* default values */
36990Sstevel@tonic-gate 	if (nodename)
37000Sstevel@tonic-gate 		*nodename = name;
37010Sstevel@tonic-gate 	if (addrname)
37020Sstevel@tonic-gate 		*addrname = nulladdrname;
37030Sstevel@tonic-gate 	if (minorname)
37040Sstevel@tonic-gate 		*minorname = NULL;
37050Sstevel@tonic-gate 
37060Sstevel@tonic-gate 	cp = name;
37070Sstevel@tonic-gate 	while (*cp != '\0') {
37080Sstevel@tonic-gate 		if (addrname && *cp == '@') {
37090Sstevel@tonic-gate 			*addrname = cp + 1;
37100Sstevel@tonic-gate 			*cp = '\0';
37110Sstevel@tonic-gate 		} else if (minorname && *cp == ':') {
37120Sstevel@tonic-gate 			*minorname = cp + 1;
37130Sstevel@tonic-gate 			*cp = '\0';
37140Sstevel@tonic-gate 		}
37150Sstevel@tonic-gate 		++cp;
37160Sstevel@tonic-gate 	}
37170Sstevel@tonic-gate }
37180Sstevel@tonic-gate 
37190Sstevel@tonic-gate static char *
child_path_to_driver(dev_info_t * parent,char * child_name,char * unit_address)37200Sstevel@tonic-gate child_path_to_driver(dev_info_t *parent, char *child_name, char *unit_address)
37210Sstevel@tonic-gate {
37220Sstevel@tonic-gate 	char *p, *drvname = NULL;
37230Sstevel@tonic-gate 	major_t maj;
37240Sstevel@tonic-gate 
37250Sstevel@tonic-gate 	/*
37260Sstevel@tonic-gate 	 * Construct the pathname and ask the implementation
37270Sstevel@tonic-gate 	 * if it can do a driver = f(pathname) for us, if not
37280Sstevel@tonic-gate 	 * we'll just default to using the node-name that
37290Sstevel@tonic-gate 	 * was given to us.  We want to do this first to
37300Sstevel@tonic-gate 	 * allow the platform to use 'generic' names for
37310Sstevel@tonic-gate 	 * legacy device drivers.
37320Sstevel@tonic-gate 	 */
37330Sstevel@tonic-gate 	p = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
37340Sstevel@tonic-gate 	(void) ddi_pathname(parent, p);
37350Sstevel@tonic-gate 	(void) strcat(p, "/");
37360Sstevel@tonic-gate 	(void) strcat(p, child_name);
37370Sstevel@tonic-gate 	if (unit_address && *unit_address) {
37380Sstevel@tonic-gate 		(void) strcat(p, "@");
37390Sstevel@tonic-gate 		(void) strcat(p, unit_address);
37400Sstevel@tonic-gate 	}
37410Sstevel@tonic-gate 
37420Sstevel@tonic-gate 	/*
37430Sstevel@tonic-gate 	 * Get the binding. If there is none, return the child_name
37440Sstevel@tonic-gate 	 * and let the caller deal with it.
37450Sstevel@tonic-gate 	 */
37460Sstevel@tonic-gate 	maj = path_to_major(p);
37470Sstevel@tonic-gate 
37480Sstevel@tonic-gate 	kmem_free(p, MAXPATHLEN);
37490Sstevel@tonic-gate 
37507009Scth 	if (maj != DDI_MAJOR_T_NONE)
37510Sstevel@tonic-gate 		drvname = ddi_major_to_name(maj);
37520Sstevel@tonic-gate 	if (drvname == NULL)
37530Sstevel@tonic-gate 		drvname = child_name;
37540Sstevel@tonic-gate 
37550Sstevel@tonic-gate 	return (drvname);
37560Sstevel@tonic-gate }
37570Sstevel@tonic-gate 
37580Sstevel@tonic-gate 
37597613SVikram.Hegde@Sun.COM #define	PCI_EX_CLASS	"pciexclass"
37607613SVikram.Hegde@Sun.COM #define	PCI_EX		"pciex"
37617613SVikram.Hegde@Sun.COM #define	PCI_CLASS	"pciclass"
37627613SVikram.Hegde@Sun.COM #define	PCI		"pci"
37637613SVikram.Hegde@Sun.COM 
37647613SVikram.Hegde@Sun.COM int
ddi_is_pci_dip(dev_info_t * dip)37657613SVikram.Hegde@Sun.COM ddi_is_pci_dip(dev_info_t *dip)
37667613SVikram.Hegde@Sun.COM {
37677613SVikram.Hegde@Sun.COM 	char	*prop = NULL;
37687613SVikram.Hegde@Sun.COM 
37697613SVikram.Hegde@Sun.COM 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
37707613SVikram.Hegde@Sun.COM 	    "compatible", &prop) == DDI_PROP_SUCCESS) {
37717613SVikram.Hegde@Sun.COM 		ASSERT(prop);
37727613SVikram.Hegde@Sun.COM 		if (strncmp(prop, PCI_EX_CLASS, sizeof (PCI_EX_CLASS) - 1)
37737613SVikram.Hegde@Sun.COM 		    == 0 ||
37747613SVikram.Hegde@Sun.COM 		    strncmp(prop, PCI_EX, sizeof (PCI_EX)- 1)
37757613SVikram.Hegde@Sun.COM 		    == 0 ||
37767613SVikram.Hegde@Sun.COM 		    strncmp(prop, PCI_CLASS, sizeof (PCI_CLASS) - 1)
37777613SVikram.Hegde@Sun.COM 		    == 0 ||
37787613SVikram.Hegde@Sun.COM 		    strncmp(prop, PCI, sizeof (PCI) - 1)
37797613SVikram.Hegde@Sun.COM 		    == 0) {
37807613SVikram.Hegde@Sun.COM 			ddi_prop_free(prop);
37817613SVikram.Hegde@Sun.COM 			return (1);
37827613SVikram.Hegde@Sun.COM 		}
37837613SVikram.Hegde@Sun.COM 	}
37847613SVikram.Hegde@Sun.COM 
37857613SVikram.Hegde@Sun.COM 	if (prop != NULL) {
37867613SVikram.Hegde@Sun.COM 		ddi_prop_free(prop);
37877613SVikram.Hegde@Sun.COM 	}
37887613SVikram.Hegde@Sun.COM 
37897613SVikram.Hegde@Sun.COM 	return (0);
37907613SVikram.Hegde@Sun.COM }
37917613SVikram.Hegde@Sun.COM 
37920Sstevel@tonic-gate /*
37930Sstevel@tonic-gate  * Given the pathname of a device, fill in the dev_info_t value and/or the
37940Sstevel@tonic-gate  * dev_t value and/or the spectype, depending on which parameters are non-NULL.
37950Sstevel@tonic-gate  * If there is an error, this function returns -1.
37960Sstevel@tonic-gate  *
37970Sstevel@tonic-gate  * NOTE: If this function returns the dev_info_t structure, then it
37980Sstevel@tonic-gate  * does so with a hold on the devi. Caller should ensure that they get
37990Sstevel@tonic-gate  * decremented via ddi_release_devi() or ndi_rele_devi();
38000Sstevel@tonic-gate  *
38010Sstevel@tonic-gate  * This function can be invoked in the boot case for a pathname without
38020Sstevel@tonic-gate  * device argument (:xxxx), traditionally treated as a minor name.
38030Sstevel@tonic-gate  * In this case, we do the following
38040Sstevel@tonic-gate  * (1) search the minor node of type DDM_DEFAULT.
38050Sstevel@tonic-gate  * (2) if no DDM_DEFAULT minor exists, then the first non-alias minor is chosen.
38060Sstevel@tonic-gate  * (3) if neither exists, a dev_t is faked with minor number = instance.
38070Sstevel@tonic-gate  * As of S9 FCS, no instance of #1 exists. #2 is used by several platforms
38080Sstevel@tonic-gate  * to default the boot partition to :a possibly by other OBP definitions.
38090Sstevel@tonic-gate  * #3 is used for booting off network interfaces, most SPARC network
38100Sstevel@tonic-gate  * drivers support Style-2 only, so only DDM_ALIAS minor exists.
38110Sstevel@tonic-gate  *
38120Sstevel@tonic-gate  * It is possible for OBP to present device args at the end of the path as
38130Sstevel@tonic-gate  * well as in the middle. For example, with IB the following strings are
38140Sstevel@tonic-gate  * valid boot paths.
38150Sstevel@tonic-gate  *	a /pci@8,700000/ib@1,2:port=1,pkey=ff,dhcp,...
38160Sstevel@tonic-gate  *	b /pci@8,700000/ib@1,1:port=1/ioc@xxxxxx,yyyyyyy:dhcp
38170Sstevel@tonic-gate  * Case (a), we first look for minor node "port=1,pkey...".
38180Sstevel@tonic-gate  * Failing that, we will pass "port=1,pkey..." to the bus_config
38190Sstevel@tonic-gate  * entry point of ib (HCA) driver.
38200Sstevel@tonic-gate  * Case (b), configure ib@1,1 as usual. Then invoke ib's bus_config
38210Sstevel@tonic-gate  * with argument "ioc@xxxxxxx,yyyyyyy:port=1". After configuring
38220Sstevel@tonic-gate  * the ioc, look for minor node dhcp. If not found, pass ":dhcp"
38230Sstevel@tonic-gate  * to ioc's bus_config entry point.
38240Sstevel@tonic-gate  */
38259140SVikram.Hegde@Sun.COM int
resolve_pathname(char * pathname,dev_info_t ** dipp,dev_t * devtp,int * spectypep)38269140SVikram.Hegde@Sun.COM resolve_pathname(char *pathname,
38279140SVikram.Hegde@Sun.COM 	dev_info_t **dipp, dev_t *devtp, int *spectypep)
38280Sstevel@tonic-gate {
38297224Scth 	int			error;
38307224Scth 	dev_info_t		*parent, *child;
38317224Scth 	struct pathname		pn;
38327224Scth 	char			*component, *config_name;
38337224Scth 	char			*minorname = NULL;
38347224Scth 	char			*prev_minor = NULL;
38357224Scth 	dev_t			devt = NODEV;
38367224Scth 	int			spectype;
38377224Scth 	struct ddi_minor_data	*dmn;
38387224Scth 	int			circ;
38390Sstevel@tonic-gate 
38400Sstevel@tonic-gate 	if (*pathname != '/')
38410Sstevel@tonic-gate 		return (EINVAL);
38420Sstevel@tonic-gate 	parent = ddi_root_node();	/* Begin at the top of the tree */
38430Sstevel@tonic-gate 
38440Sstevel@tonic-gate 	if (error = pn_get(pathname, UIO_SYSSPACE, &pn))
38450Sstevel@tonic-gate 		return (error);
38460Sstevel@tonic-gate 	pn_skipslash(&pn);
38470Sstevel@tonic-gate 
38481333Scth 	ASSERT(i_ddi_devi_attached(parent));
38490Sstevel@tonic-gate 	ndi_hold_devi(parent);
38500Sstevel@tonic-gate 
38510Sstevel@tonic-gate 	component = kmem_alloc(MAXNAMELEN, KM_SLEEP);
38520Sstevel@tonic-gate 	config_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
38530Sstevel@tonic-gate 
38540Sstevel@tonic-gate 	while (pn_pathleft(&pn)) {
38550Sstevel@tonic-gate 		/* remember prev minor (:xxx) in the middle of path */
38560Sstevel@tonic-gate 		if (minorname)
38570Sstevel@tonic-gate 			prev_minor = i_ddi_strdup(minorname, KM_SLEEP);
38580Sstevel@tonic-gate 
38590Sstevel@tonic-gate 		/* Get component and chop off minorname */
38600Sstevel@tonic-gate 		(void) pn_getcomponent(&pn, component);
386110822SJack.Meng@Sun.COM 		if ((iscsiboot_prop != NULL) &&
386210822SJack.Meng@Sun.COM 		    (strcmp((DEVI(parent)->devi_node_name), "iscsi") == 0)) {
386310822SJack.Meng@Sun.COM 			i_ddi_parse_iscsi_name(component, NULL, NULL,
386410822SJack.Meng@Sun.COM 			    &minorname);
386510822SJack.Meng@Sun.COM 		} else {
386610822SJack.Meng@Sun.COM 			i_ddi_parse_name(component, NULL, NULL, &minorname);
386710822SJack.Meng@Sun.COM 		}
38680Sstevel@tonic-gate 		if (prev_minor == NULL) {
38690Sstevel@tonic-gate 			(void) snprintf(config_name, MAXNAMELEN, "%s",
38700Sstevel@tonic-gate 			    component);
38710Sstevel@tonic-gate 		} else {
38720Sstevel@tonic-gate 			(void) snprintf(config_name, MAXNAMELEN, "%s:%s",
38730Sstevel@tonic-gate 			    component, prev_minor);
38740Sstevel@tonic-gate 			kmem_free(prev_minor, strlen(prev_minor) + 1);
38750Sstevel@tonic-gate 			prev_minor = NULL;
38760Sstevel@tonic-gate 		}
38770Sstevel@tonic-gate 
38780Sstevel@tonic-gate 		/*
38790Sstevel@tonic-gate 		 * Find and configure the child
38800Sstevel@tonic-gate 		 */
38810Sstevel@tonic-gate 		if (ndi_devi_config_one(parent, config_name, &child,
38820Sstevel@tonic-gate 		    NDI_PROMNAME | NDI_NO_EVENT) != NDI_SUCCESS) {
38830Sstevel@tonic-gate 			ndi_rele_devi(parent);
38840Sstevel@tonic-gate 			pn_free(&pn);
38850Sstevel@tonic-gate 			kmem_free(component, MAXNAMELEN);
38860Sstevel@tonic-gate 			kmem_free(config_name, MAXNAMELEN);
38870Sstevel@tonic-gate 			return (-1);
38880Sstevel@tonic-gate 		}
38890Sstevel@tonic-gate 
38901333Scth 		ASSERT(i_ddi_devi_attached(child));
38910Sstevel@tonic-gate 		ndi_rele_devi(parent);
38920Sstevel@tonic-gate 		parent = child;
38930Sstevel@tonic-gate 		pn_skipslash(&pn);
38940Sstevel@tonic-gate 	}
38950Sstevel@tonic-gate 
38960Sstevel@tonic-gate 	/*
38970Sstevel@tonic-gate 	 * First look for a minor node matching minorname.
38980Sstevel@tonic-gate 	 * Failing that, try to pass minorname to bus_config().
38990Sstevel@tonic-gate 	 */
39000Sstevel@tonic-gate 	if (minorname && i_ddi_minorname_to_devtspectype(parent,
39010Sstevel@tonic-gate 	    minorname, &devt, &spectype) == DDI_FAILURE) {
39020Sstevel@tonic-gate 		(void) snprintf(config_name, MAXNAMELEN, "%s", minorname);
39030Sstevel@tonic-gate 		if (ndi_devi_config_obp_args(parent,
39040Sstevel@tonic-gate 		    config_name, &child, 0) != NDI_SUCCESS) {
39050Sstevel@tonic-gate 			ndi_rele_devi(parent);
39060Sstevel@tonic-gate 			pn_free(&pn);
39070Sstevel@tonic-gate 			kmem_free(component, MAXNAMELEN);
39080Sstevel@tonic-gate 			kmem_free(config_name, MAXNAMELEN);
39090Sstevel@tonic-gate 			NDI_CONFIG_DEBUG((CE_NOTE,
39100Sstevel@tonic-gate 			    "%s: minor node not found\n", pathname));
39110Sstevel@tonic-gate 			return (-1);
39120Sstevel@tonic-gate 		}
39130Sstevel@tonic-gate 		minorname = NULL;	/* look for default minor */
39141333Scth 		ASSERT(i_ddi_devi_attached(child));
39150Sstevel@tonic-gate 		ndi_rele_devi(parent);
39160Sstevel@tonic-gate 		parent = child;
39170Sstevel@tonic-gate 	}
39180Sstevel@tonic-gate 
39190Sstevel@tonic-gate 	if (devtp || spectypep) {
39200Sstevel@tonic-gate 		if (minorname == NULL) {
39217224Scth 			/*
39227224Scth 			 * Search for a default entry with an active
39237224Scth 			 * ndi_devi_enter to protect the devi_minor list.
39247224Scth 			 */
39257224Scth 			ndi_devi_enter(parent, &circ);
39260Sstevel@tonic-gate 			for (dmn = DEVI(parent)->devi_minor; dmn;
39270Sstevel@tonic-gate 			    dmn = dmn->next) {
39280Sstevel@tonic-gate 				if (dmn->type == DDM_DEFAULT) {
39290Sstevel@tonic-gate 					devt = dmn->ddm_dev;
39300Sstevel@tonic-gate 					spectype = dmn->ddm_spec_type;
39310Sstevel@tonic-gate 					break;
39320Sstevel@tonic-gate 				}
39330Sstevel@tonic-gate 			}
39340Sstevel@tonic-gate 
39350Sstevel@tonic-gate 			if (devt == NODEV) {
39360Sstevel@tonic-gate 				/*
39370Sstevel@tonic-gate 				 * No default minor node, try the first one;
39380Sstevel@tonic-gate 				 * else, assume 1-1 instance-minor mapping
39390Sstevel@tonic-gate 				 */
39400Sstevel@tonic-gate 				dmn = DEVI(parent)->devi_minor;
39410Sstevel@tonic-gate 				if (dmn && ((dmn->type == DDM_MINOR) ||
39420Sstevel@tonic-gate 				    (dmn->type == DDM_INTERNAL_PATH))) {
39430Sstevel@tonic-gate 					devt = dmn->ddm_dev;
39440Sstevel@tonic-gate 					spectype = dmn->ddm_spec_type;
39450Sstevel@tonic-gate 				} else {
39460Sstevel@tonic-gate 					devt = makedevice(
39470Sstevel@tonic-gate 					    DEVI(parent)->devi_major,
39480Sstevel@tonic-gate 					    ddi_get_instance(parent));
39490Sstevel@tonic-gate 					spectype = S_IFCHR;
39500Sstevel@tonic-gate 				}
39510Sstevel@tonic-gate 			}
39527224Scth 			ndi_devi_exit(parent, circ);
39530Sstevel@tonic-gate 		}
39540Sstevel@tonic-gate 		if (devtp)
39550Sstevel@tonic-gate 			*devtp = devt;
39560Sstevel@tonic-gate 		if (spectypep)
39570Sstevel@tonic-gate 			*spectypep = spectype;
39580Sstevel@tonic-gate 	}
39590Sstevel@tonic-gate 
39600Sstevel@tonic-gate 	pn_free(&pn);
39610Sstevel@tonic-gate 	kmem_free(component, MAXNAMELEN);
39620Sstevel@tonic-gate 	kmem_free(config_name, MAXNAMELEN);
39630Sstevel@tonic-gate 
39640Sstevel@tonic-gate 	/*
39650Sstevel@tonic-gate 	 * If there is no error, return the appropriate parameters
39660Sstevel@tonic-gate 	 */
39670Sstevel@tonic-gate 	if (dipp != NULL)
39680Sstevel@tonic-gate 		*dipp = parent;
39699140SVikram.Hegde@Sun.COM 	else {
39700Sstevel@tonic-gate 		/*
39710Sstevel@tonic-gate 		 * We should really keep the ref count to keep the node from
39720Sstevel@tonic-gate 		 * detaching but ddi_pathname_to_dev_t() specifies a NULL dipp,
39730Sstevel@tonic-gate 		 * so we have no way of passing back the held dip.  Not holding
39740Sstevel@tonic-gate 		 * the dip allows detaches to occur - which can cause problems
39750Sstevel@tonic-gate 		 * for subsystems which call ddi_pathname_to_dev_t (console).
39760Sstevel@tonic-gate 		 *
39770Sstevel@tonic-gate 		 * Instead of holding the dip, we place a ddi-no-autodetach
39780Sstevel@tonic-gate 		 * property on the node to prevent auto detaching.
39790Sstevel@tonic-gate 		 *
39800Sstevel@tonic-gate 		 * The right fix is to remove ddi_pathname_to_dev_t and replace
39810Sstevel@tonic-gate 		 * it, and all references, with a call that specifies a dipp.
39820Sstevel@tonic-gate 		 * In addition, the callers of this new interfaces would then
39830Sstevel@tonic-gate 		 * need to call ndi_rele_devi when the reference is complete.
39847613SVikram.Hegde@Sun.COM 		 *
39850Sstevel@tonic-gate 		 */
39860Sstevel@tonic-gate 		(void) ddi_prop_update_int(DDI_DEV_T_NONE, parent,
39870Sstevel@tonic-gate 		    DDI_NO_AUTODETACH, 1);
39880Sstevel@tonic-gate 		ndi_rele_devi(parent);
39890Sstevel@tonic-gate 	}
39900Sstevel@tonic-gate 
39910Sstevel@tonic-gate 	return (0);
39920Sstevel@tonic-gate }
39930Sstevel@tonic-gate 
39940Sstevel@tonic-gate /*
39950Sstevel@tonic-gate  * Given the pathname of a device, return the dev_t of the corresponding
39960Sstevel@tonic-gate  * device.  Returns NODEV on failure.
39970Sstevel@tonic-gate  *
39980Sstevel@tonic-gate  * Note that this call sets the DDI_NO_AUTODETACH property on the devinfo node.
39990Sstevel@tonic-gate  */
40000Sstevel@tonic-gate dev_t
ddi_pathname_to_dev_t(char * pathname)40010Sstevel@tonic-gate ddi_pathname_to_dev_t(char *pathname)
40020Sstevel@tonic-gate {
40030Sstevel@tonic-gate 	dev_t devt;
40040Sstevel@tonic-gate 	int error;
40050Sstevel@tonic-gate 
40060Sstevel@tonic-gate 	error = resolve_pathname(pathname, NULL, &devt, NULL);
40070Sstevel@tonic-gate 
40080Sstevel@tonic-gate 	return (error ? NODEV : devt);
40090Sstevel@tonic-gate }
40100Sstevel@tonic-gate 
40110Sstevel@tonic-gate /*
40120Sstevel@tonic-gate  * Translate a prom pathname to kernel devfs pathname.
40130Sstevel@tonic-gate  * Caller is assumed to allocate devfspath memory of
40140Sstevel@tonic-gate  * size at least MAXPATHLEN
40150Sstevel@tonic-gate  *
40160Sstevel@tonic-gate  * The prom pathname may not include minor name, but
40170Sstevel@tonic-gate  * devfs pathname has a minor name portion.
40180Sstevel@tonic-gate  */
40190Sstevel@tonic-gate int
i_ddi_prompath_to_devfspath(char * prompath,char * devfspath)40200Sstevel@tonic-gate i_ddi_prompath_to_devfspath(char *prompath, char *devfspath)
40210Sstevel@tonic-gate {
40220Sstevel@tonic-gate 	dev_t		devt = (dev_t)NODEV;
40230Sstevel@tonic-gate 	dev_info_t	*dip = NULL;
40240Sstevel@tonic-gate 	char		*minor_name = NULL;
40250Sstevel@tonic-gate 	int		spectype;
40260Sstevel@tonic-gate 	int		error;
40277224Scth 	int		circ;
40280Sstevel@tonic-gate 
40290Sstevel@tonic-gate 	error = resolve_pathname(prompath, &dip, &devt, &spectype);
40300Sstevel@tonic-gate 	if (error)
40310Sstevel@tonic-gate 		return (DDI_FAILURE);
40320Sstevel@tonic-gate 	ASSERT(dip && devt != NODEV);
40330Sstevel@tonic-gate 
40340Sstevel@tonic-gate 	/*
40350Sstevel@tonic-gate 	 * Get in-kernel devfs pathname
40360Sstevel@tonic-gate 	 */
40370Sstevel@tonic-gate 	(void) ddi_pathname(dip, devfspath);
40380Sstevel@tonic-gate 
40397224Scth 	ndi_devi_enter(dip, &circ);
40400Sstevel@tonic-gate 	minor_name = i_ddi_devtspectype_to_minorname(dip, devt, spectype);
40410Sstevel@tonic-gate 	if (minor_name) {
40420Sstevel@tonic-gate 		(void) strcat(devfspath, ":");
40430Sstevel@tonic-gate 		(void) strcat(devfspath, minor_name);
40440Sstevel@tonic-gate 	} else {
40450Sstevel@tonic-gate 		/*
40460Sstevel@tonic-gate 		 * If minor_name is NULL, we have an alias minor node.
40470Sstevel@tonic-gate 		 * So manufacture a path to the corresponding clone minor.
40480Sstevel@tonic-gate 		 */
40490Sstevel@tonic-gate 		(void) snprintf(devfspath, MAXPATHLEN, "%s:%s",
40500Sstevel@tonic-gate 		    CLONE_PATH, ddi_driver_name(dip));
40510Sstevel@tonic-gate 	}
40527224Scth 	ndi_devi_exit(dip, circ);
40530Sstevel@tonic-gate 
40540Sstevel@tonic-gate 	/* release hold from resolve_pathname() */
40550Sstevel@tonic-gate 	ndi_rele_devi(dip);
40560Sstevel@tonic-gate 	return (0);
40570Sstevel@tonic-gate }
40580Sstevel@tonic-gate 
40590Sstevel@tonic-gate /*
40607656SSherry.Moore@Sun.COM  * This function is intended to identify drivers that must quiesce for fast
40617656SSherry.Moore@Sun.COM  * reboot to succeed.  It does not claim to have more knowledge about the device
40627656SSherry.Moore@Sun.COM  * than its driver.  If a driver has implemented quiesce(), it will be invoked;
40637656SSherry.Moore@Sun.COM  * if a so identified driver does not manage any device that needs to be
40647656SSherry.Moore@Sun.COM  * quiesced, it must explicitly set its devo_quiesce dev_op to
40657656SSherry.Moore@Sun.COM  * ddi_quiesce_not_needed.
40667656SSherry.Moore@Sun.COM  */
40677656SSherry.Moore@Sun.COM static int skip_pseudo = 1;	/* Skip pseudo devices */
40687656SSherry.Moore@Sun.COM static int skip_non_hw = 1;	/* Skip devices with no hardware property */
40697656SSherry.Moore@Sun.COM static int
should_implement_quiesce(dev_info_t * dip)40707656SSherry.Moore@Sun.COM should_implement_quiesce(dev_info_t *dip)
40717656SSherry.Moore@Sun.COM {
40727656SSherry.Moore@Sun.COM 	struct dev_info *devi = DEVI(dip);
40737656SSherry.Moore@Sun.COM 	dev_info_t *pdip;
40747656SSherry.Moore@Sun.COM 
40757656SSherry.Moore@Sun.COM 	/*
40767656SSherry.Moore@Sun.COM 	 * If dip is pseudo and skip_pseudo is set, driver doesn't have to
40777656SSherry.Moore@Sun.COM 	 * implement quiesce().
40787656SSherry.Moore@Sun.COM 	 */
40797656SSherry.Moore@Sun.COM 	if (skip_pseudo &&
40807656SSherry.Moore@Sun.COM 	    strncmp(ddi_binding_name(dip), "pseudo", sizeof ("pseudo")) == 0)
40817656SSherry.Moore@Sun.COM 		return (0);
40827656SSherry.Moore@Sun.COM 
40837656SSherry.Moore@Sun.COM 	/*
40847656SSherry.Moore@Sun.COM 	 * If parent dip is pseudo and skip_pseudo is set, driver doesn't have
40857656SSherry.Moore@Sun.COM 	 * to implement quiesce().
40867656SSherry.Moore@Sun.COM 	 */
40877656SSherry.Moore@Sun.COM 	if (skip_pseudo && (pdip = ddi_get_parent(dip)) != NULL &&
40887656SSherry.Moore@Sun.COM 	    strncmp(ddi_binding_name(pdip), "pseudo", sizeof ("pseudo")) == 0)
40897656SSherry.Moore@Sun.COM 		return (0);
40907656SSherry.Moore@Sun.COM 
40917656SSherry.Moore@Sun.COM 	/*
40927656SSherry.Moore@Sun.COM 	 * If not attached, driver doesn't have to implement quiesce().
40937656SSherry.Moore@Sun.COM 	 */
40947656SSherry.Moore@Sun.COM 	if (!i_ddi_devi_attached(dip))
40957656SSherry.Moore@Sun.COM 		return (0);
40967656SSherry.Moore@Sun.COM 
40977656SSherry.Moore@Sun.COM 	/*
40987656SSherry.Moore@Sun.COM 	 * If dip has no hardware property and skip_non_hw is set,
40997656SSherry.Moore@Sun.COM 	 * driver doesn't have to implement quiesce().
41007656SSherry.Moore@Sun.COM 	 */
41017656SSherry.Moore@Sun.COM 	if (skip_non_hw && devi->devi_hw_prop_ptr == NULL)
41027656SSherry.Moore@Sun.COM 		return (0);
41037656SSherry.Moore@Sun.COM 
41047656SSherry.Moore@Sun.COM 	return (1);
41057656SSherry.Moore@Sun.COM }
41067656SSherry.Moore@Sun.COM 
41077656SSherry.Moore@Sun.COM static int
driver_has_quiesce(struct dev_ops * ops)41087656SSherry.Moore@Sun.COM driver_has_quiesce(struct dev_ops *ops)
41097656SSherry.Moore@Sun.COM {
41107656SSherry.Moore@Sun.COM 	if ((ops->devo_rev >= 4) && (ops->devo_quiesce != nodev) &&
41117656SSherry.Moore@Sun.COM 	    (ops->devo_quiesce != NULL) && (ops->devo_quiesce != nulldev) &&
41127656SSherry.Moore@Sun.COM 	    (ops->devo_quiesce != ddi_quiesce_not_supported))
41137656SSherry.Moore@Sun.COM 		return (1);
41147656SSherry.Moore@Sun.COM 	else
41157656SSherry.Moore@Sun.COM 		return (0);
41167656SSherry.Moore@Sun.COM }
41177656SSherry.Moore@Sun.COM 
41187656SSherry.Moore@Sun.COM /*
41197656SSherry.Moore@Sun.COM  * Check to see if a driver has implemented the quiesce() DDI function.
41207656SSherry.Moore@Sun.COM  */
41217656SSherry.Moore@Sun.COM int
check_driver_quiesce(dev_info_t * dip,void * arg)41227656SSherry.Moore@Sun.COM check_driver_quiesce(dev_info_t *dip, void *arg)
41237656SSherry.Moore@Sun.COM {
41247656SSherry.Moore@Sun.COM 	struct dev_ops *ops;
41257656SSherry.Moore@Sun.COM 
41267656SSherry.Moore@Sun.COM 	if (!should_implement_quiesce(dip))
41277656SSherry.Moore@Sun.COM 		return (DDI_WALK_CONTINUE);
41287656SSherry.Moore@Sun.COM 
41297656SSherry.Moore@Sun.COM 	if ((ops = ddi_get_driver(dip)) == NULL)
41307656SSherry.Moore@Sun.COM 		return (DDI_WALK_CONTINUE);
41317656SSherry.Moore@Sun.COM 
41327656SSherry.Moore@Sun.COM 	if (driver_has_quiesce(ops)) {
41337656SSherry.Moore@Sun.COM 		if ((quiesce_debug & 0x2) == 0x2) {
41347656SSherry.Moore@Sun.COM 			if (ops->devo_quiesce == ddi_quiesce_not_needed)
41357656SSherry.Moore@Sun.COM 				cmn_err(CE_CONT, "%s does not need to be "
41367656SSherry.Moore@Sun.COM 				    "quiesced", ddi_driver_name(dip));
41377656SSherry.Moore@Sun.COM 			else
41387656SSherry.Moore@Sun.COM 				cmn_err(CE_CONT, "%s has quiesce routine",
41397656SSherry.Moore@Sun.COM 				    ddi_driver_name(dip));
41407656SSherry.Moore@Sun.COM 		}
41417656SSherry.Moore@Sun.COM 	} else {
41427656SSherry.Moore@Sun.COM 		if (arg != NULL)
41437656SSherry.Moore@Sun.COM 			*((int *)arg) = -1;
41447656SSherry.Moore@Sun.COM 		cmn_err(CE_WARN, "%s has no quiesce()", ddi_driver_name(dip));
41457656SSherry.Moore@Sun.COM 	}
41467656SSherry.Moore@Sun.COM 
41477656SSherry.Moore@Sun.COM 	return (DDI_WALK_CONTINUE);
41487656SSherry.Moore@Sun.COM }
41497656SSherry.Moore@Sun.COM 
41507656SSherry.Moore@Sun.COM /*
41517656SSherry.Moore@Sun.COM  * Quiesce device.
41527656SSherry.Moore@Sun.COM  */
41537656SSherry.Moore@Sun.COM static void
quiesce_one_device(dev_info_t * dip,void * arg)41547656SSherry.Moore@Sun.COM quiesce_one_device(dev_info_t *dip, void *arg)
41557656SSherry.Moore@Sun.COM {
41567656SSherry.Moore@Sun.COM 	struct dev_ops *ops;
41577656SSherry.Moore@Sun.COM 	int should_quiesce = 0;
41587656SSherry.Moore@Sun.COM 
41597656SSherry.Moore@Sun.COM 	/*
41607656SSherry.Moore@Sun.COM 	 * If the device is not attached it doesn't need to be quiesced.
41617656SSherry.Moore@Sun.COM 	 */
41627656SSherry.Moore@Sun.COM 	if (!i_ddi_devi_attached(dip))
41637656SSherry.Moore@Sun.COM 		return;
41647656SSherry.Moore@Sun.COM 
41657656SSherry.Moore@Sun.COM 	if ((ops = ddi_get_driver(dip)) == NULL)
41667656SSherry.Moore@Sun.COM 		return;
41677656SSherry.Moore@Sun.COM 
41687656SSherry.Moore@Sun.COM 	should_quiesce = should_implement_quiesce(dip);
41697656SSherry.Moore@Sun.COM 
41707656SSherry.Moore@Sun.COM 	/*
41717656SSherry.Moore@Sun.COM 	 * If there's an implementation of quiesce(), always call it even if
41727656SSherry.Moore@Sun.COM 	 * some of the drivers don't have quiesce() or quiesce() have failed
41737656SSherry.Moore@Sun.COM 	 * so we can do force fast reboot.  The implementation of quiesce()
41747656SSherry.Moore@Sun.COM 	 * should not negatively affect a regular reboot.
41757656SSherry.Moore@Sun.COM 	 */
41767656SSherry.Moore@Sun.COM 	if (driver_has_quiesce(ops)) {
41777656SSherry.Moore@Sun.COM 		int rc = DDI_SUCCESS;
41787656SSherry.Moore@Sun.COM 
41797656SSherry.Moore@Sun.COM 		if (ops->devo_quiesce == ddi_quiesce_not_needed)
41807656SSherry.Moore@Sun.COM 			return;
41817656SSherry.Moore@Sun.COM 
41827656SSherry.Moore@Sun.COM 		rc = devi_quiesce(dip);
41837656SSherry.Moore@Sun.COM 
41847656SSherry.Moore@Sun.COM 		/* quiesce() should never fail */
41857656SSherry.Moore@Sun.COM 		ASSERT(rc == DDI_SUCCESS);
41867656SSherry.Moore@Sun.COM 
41877656SSherry.Moore@Sun.COM 		if (rc != DDI_SUCCESS && should_quiesce) {
41887656SSherry.Moore@Sun.COM 
41897656SSherry.Moore@Sun.COM 			if (arg != NULL)
41907656SSherry.Moore@Sun.COM 				*((int *)arg) = -1;
41917656SSherry.Moore@Sun.COM 		}
41927656SSherry.Moore@Sun.COM 	} else if (should_quiesce && arg != NULL) {
41937656SSherry.Moore@Sun.COM 		*((int *)arg) = -1;
41947656SSherry.Moore@Sun.COM 	}
41957656SSherry.Moore@Sun.COM }
41967656SSherry.Moore@Sun.COM 
41977656SSherry.Moore@Sun.COM /*
41987656SSherry.Moore@Sun.COM  * Traverse the dev info tree in a breadth-first manner so that we quiesce
41997656SSherry.Moore@Sun.COM  * children first.  All subtrees under the parent of dip will be quiesced.
42007656SSherry.Moore@Sun.COM  */
42017656SSherry.Moore@Sun.COM void
quiesce_devices(dev_info_t * dip,void * arg)42027656SSherry.Moore@Sun.COM quiesce_devices(dev_info_t *dip, void *arg)
42037656SSherry.Moore@Sun.COM {
42047656SSherry.Moore@Sun.COM 	/*
42057656SSherry.Moore@Sun.COM 	 * if we're reached here, the device tree better not be changing.
42067656SSherry.Moore@Sun.COM 	 * so either devinfo_freeze better be set or we better be panicing.
42077656SSherry.Moore@Sun.COM 	 */
42087656SSherry.Moore@Sun.COM 	ASSERT(devinfo_freeze || panicstr);
42097656SSherry.Moore@Sun.COM 
42107656SSherry.Moore@Sun.COM 	for (; dip != NULL; dip = ddi_get_next_sibling(dip)) {
42117656SSherry.Moore@Sun.COM 		quiesce_devices(ddi_get_child(dip), arg);
42127656SSherry.Moore@Sun.COM 
42137656SSherry.Moore@Sun.COM 		quiesce_one_device(dip, arg);
42147656SSherry.Moore@Sun.COM 	}
42157656SSherry.Moore@Sun.COM }
42167656SSherry.Moore@Sun.COM 
42177656SSherry.Moore@Sun.COM /*
42180Sstevel@tonic-gate  * Reset all the pure leaf drivers on the system at halt time
42190Sstevel@tonic-gate  */
42200Sstevel@tonic-gate static int
reset_leaf_device(dev_info_t * dip,void * arg)42210Sstevel@tonic-gate reset_leaf_device(dev_info_t *dip, void *arg)
42220Sstevel@tonic-gate {
42230Sstevel@tonic-gate 	_NOTE(ARGUNUSED(arg))
42240Sstevel@tonic-gate 	struct dev_ops *ops;
42250Sstevel@tonic-gate 
42260Sstevel@tonic-gate 	/* if the device doesn't need to be reset then there's nothing to do */
42270Sstevel@tonic-gate 	if (!DEVI_NEED_RESET(dip))
42280Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
42290Sstevel@tonic-gate 
42300Sstevel@tonic-gate 	/*
42310Sstevel@tonic-gate 	 * if the device isn't a char/block device or doesn't have a
42320Sstevel@tonic-gate 	 * reset entry point then there's nothing to do.
42330Sstevel@tonic-gate 	 */
42340Sstevel@tonic-gate 	ops = ddi_get_driver(dip);
42350Sstevel@tonic-gate 	if ((ops == NULL) || (ops->devo_cb_ops == NULL) ||
42360Sstevel@tonic-gate 	    (ops->devo_reset == nodev) || (ops->devo_reset == nulldev) ||
42370Sstevel@tonic-gate 	    (ops->devo_reset == NULL))
42380Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
42390Sstevel@tonic-gate 
42400Sstevel@tonic-gate 	if (DEVI_IS_ATTACHING(dip) || DEVI_IS_DETACHING(dip)) {
42410Sstevel@tonic-gate 		static char path[MAXPATHLEN];
42420Sstevel@tonic-gate 
42430Sstevel@tonic-gate 		/*
42440Sstevel@tonic-gate 		 * bad news, this device has blocked in it's attach or
42450Sstevel@tonic-gate 		 * detach routine, which means it not safe to call it's
42460Sstevel@tonic-gate 		 * devo_reset() entry point.
42470Sstevel@tonic-gate 		 */
42480Sstevel@tonic-gate 		cmn_err(CE_WARN, "unable to reset device: %s",
42490Sstevel@tonic-gate 		    ddi_pathname(dip, path));
42500Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
42510Sstevel@tonic-gate 	}
42520Sstevel@tonic-gate 
42530Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_NOTE, "resetting %s%d\n",
42544950Scth 	    ddi_driver_name(dip), ddi_get_instance(dip)));
42550Sstevel@tonic-gate 
42560Sstevel@tonic-gate 	(void) devi_reset(dip, DDI_RESET_FORCE);
42570Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
42580Sstevel@tonic-gate }
42590Sstevel@tonic-gate 
42600Sstevel@tonic-gate void
reset_leaves(void)42610Sstevel@tonic-gate reset_leaves(void)
42620Sstevel@tonic-gate {
42630Sstevel@tonic-gate 	/*
42640Sstevel@tonic-gate 	 * if we're reached here, the device tree better not be changing.
42650Sstevel@tonic-gate 	 * so either devinfo_freeze better be set or we better be panicing.
42660Sstevel@tonic-gate 	 */
42670Sstevel@tonic-gate 	ASSERT(devinfo_freeze || panicstr);
42680Sstevel@tonic-gate 
42690Sstevel@tonic-gate 	(void) walk_devs(top_devinfo, reset_leaf_device, NULL, 0);
42700Sstevel@tonic-gate }
42710Sstevel@tonic-gate 
42727656SSherry.Moore@Sun.COM 
42737656SSherry.Moore@Sun.COM /*
42747656SSherry.Moore@Sun.COM  * devtree_freeze() must be called before quiesce_devices() and reset_leaves()
42757656SSherry.Moore@Sun.COM  * during a normal system shutdown.  It attempts to ensure that there are no
42767656SSherry.Moore@Sun.COM  * outstanding attach or detach operations in progress when quiesce_devices() or
42777656SSherry.Moore@Sun.COM  * reset_leaves()is invoked.  It must be called before the system becomes
42787656SSherry.Moore@Sun.COM  * single-threaded because device attach and detach are multi-threaded
427910923SEvan.Yan@Sun.COM  * operations.	(note that during system shutdown the system doesn't actually
42807656SSherry.Moore@Sun.COM  * become single-thread since other threads still exist, but the shutdown thread
42817656SSherry.Moore@Sun.COM  * will disable preemption for itself, raise it's pil, and stop all the other
42827656SSherry.Moore@Sun.COM  * cpus in the system there by effectively making the system single-threaded.)
42830Sstevel@tonic-gate  */
42840Sstevel@tonic-gate void
devtree_freeze(void)42850Sstevel@tonic-gate devtree_freeze(void)
42860Sstevel@tonic-gate {
42870Sstevel@tonic-gate 	int delayed = 0;
42880Sstevel@tonic-gate 
42890Sstevel@tonic-gate 	/* if we're panicing then the device tree isn't going to be changing */
42900Sstevel@tonic-gate 	if (panicstr)
42910Sstevel@tonic-gate 		return;
42920Sstevel@tonic-gate 
42930Sstevel@tonic-gate 	/* stop all dev_info state changes in the device tree */
42940Sstevel@tonic-gate 	devinfo_freeze = gethrtime();
42950Sstevel@tonic-gate 
42960Sstevel@tonic-gate 	/*
42970Sstevel@tonic-gate 	 * if we're not panicing and there are on-going attach or detach
42980Sstevel@tonic-gate 	 * operations, wait for up to 3 seconds for them to finish.  This
42990Sstevel@tonic-gate 	 * is a randomly chosen interval but this should be ok because:
43000Sstevel@tonic-gate 	 * - 3 seconds is very small relative to the deadman timer.
43010Sstevel@tonic-gate 	 * - normal attach and detach operations should be very quick.
43020Sstevel@tonic-gate 	 * - attach and detach operations are fairly rare.
43030Sstevel@tonic-gate 	 */
43040Sstevel@tonic-gate 	while (!panicstr && atomic_add_long_nv(&devinfo_attach_detach, 0) &&
43050Sstevel@tonic-gate 	    (delayed < 3)) {
43060Sstevel@tonic-gate 		delayed += 1;
43070Sstevel@tonic-gate 
43080Sstevel@tonic-gate 		/* do a sleeping wait for one second */
43090Sstevel@tonic-gate 		ASSERT(!servicing_interrupt());
43100Sstevel@tonic-gate 		delay(drv_usectohz(MICROSEC));
43110Sstevel@tonic-gate 	}
43120Sstevel@tonic-gate }
43130Sstevel@tonic-gate 
43140Sstevel@tonic-gate static int
bind_dip(dev_info_t * dip,void * arg)43150Sstevel@tonic-gate bind_dip(dev_info_t *dip, void *arg)
43160Sstevel@tonic-gate {
43170Sstevel@tonic-gate 	_NOTE(ARGUNUSED(arg))
43184145Scth 	char	*path;
43194145Scth 	major_t	major, pmajor;
43204145Scth 
43214145Scth 	/*
43224145Scth 	 * If the node is currently bound to the wrong driver, try to unbind
43234145Scth 	 * so that we can rebind to the correct driver.
43244145Scth 	 */
43254145Scth 	if (i_ddi_node_state(dip) >= DS_BOUND) {
43264145Scth 		major = ddi_compatible_driver_major(dip, NULL);
43274145Scth 		if ((DEVI(dip)->devi_major == major) &&
43284145Scth 		    (i_ddi_node_state(dip) >= DS_INITIALIZED)) {
43294145Scth 			/*
43304145Scth 			 * Check for a path-oriented driver alias that
43314145Scth 			 * takes precedence over current driver binding.
43324145Scth 			 */
43334145Scth 			path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
43344145Scth 			(void) ddi_pathname(dip, path);
43354145Scth 			pmajor = ddi_name_to_major(path);
433612588SJerry.Gilliam@Sun.COM 			if (driver_active(pmajor))
43374145Scth 				major = pmajor;
43384145Scth 			kmem_free(path, MAXPATHLEN);
43394145Scth 		}
43404145Scth 
43414145Scth 		/* attempt unbind if current driver is incorrect */
434212588SJerry.Gilliam@Sun.COM 		if (driver_active(major) &&
43434145Scth 		    (major != DEVI(dip)->devi_major))
43444145Scth 			(void) ndi_devi_unbind_driver(dip);
43454145Scth 	}
43464145Scth 
43474145Scth 	/* If unbound, try to bind to a driver */
43480Sstevel@tonic-gate 	if (i_ddi_node_state(dip) < DS_BOUND)
43490Sstevel@tonic-gate 		(void) ndi_devi_bind_driver(dip, 0);
43500Sstevel@tonic-gate 
43510Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
43520Sstevel@tonic-gate }
43530Sstevel@tonic-gate 
43540Sstevel@tonic-gate void
i_ddi_bind_devs(void)43550Sstevel@tonic-gate i_ddi_bind_devs(void)
43560Sstevel@tonic-gate {
43574145Scth 	/* flush devfs so that ndi_devi_unbind_driver will work when possible */
43584145Scth 	(void) devfs_clean(top_devinfo, NULL, 0);
43594145Scth 
43600Sstevel@tonic-gate 	ddi_walk_devs(top_devinfo, bind_dip, (void *)NULL);
43610Sstevel@tonic-gate }
43620Sstevel@tonic-gate 
43638831SJerry.Gilliam@Sun.COM /* callback data for unbind_children_by_alias() */
43648831SJerry.Gilliam@Sun.COM typedef struct unbind_data {
43658831SJerry.Gilliam@Sun.COM 	major_t	drv_major;
43668831SJerry.Gilliam@Sun.COM 	char	*drv_alias;
43678831SJerry.Gilliam@Sun.COM 	int	ndevs_bound;
43688831SJerry.Gilliam@Sun.COM 	int	unbind_errors;
43698831SJerry.Gilliam@Sun.COM } unbind_data_t;
43708831SJerry.Gilliam@Sun.COM 
43718831SJerry.Gilliam@Sun.COM /*
43728831SJerry.Gilliam@Sun.COM  * A utility function provided for testing and support convenience
43738831SJerry.Gilliam@Sun.COM  * Called for each device during an upgrade_drv -d bound to the alias
43748831SJerry.Gilliam@Sun.COM  * that cannot be unbound due to device in use.
43758831SJerry.Gilliam@Sun.COM  */
43768831SJerry.Gilliam@Sun.COM static void
unbind_alias_dev_in_use(dev_info_t * dip,char * alias)43778831SJerry.Gilliam@Sun.COM unbind_alias_dev_in_use(dev_info_t *dip, char *alias)
43788831SJerry.Gilliam@Sun.COM {
43798831SJerry.Gilliam@Sun.COM 	if (moddebug & MODDEBUG_BINDING) {
43808831SJerry.Gilliam@Sun.COM 		cmn_err(CE_CONT, "%s%d: state %d: bound to %s\n",
43818831SJerry.Gilliam@Sun.COM 		    ddi_driver_name(dip), ddi_get_instance(dip),
43828831SJerry.Gilliam@Sun.COM 		    i_ddi_node_state(dip), alias);
43838831SJerry.Gilliam@Sun.COM 	}
43848831SJerry.Gilliam@Sun.COM }
43858831SJerry.Gilliam@Sun.COM 
43868831SJerry.Gilliam@Sun.COM /*
43878831SJerry.Gilliam@Sun.COM  * walkdevs callback for unbind devices bound to specific driver
43888831SJerry.Gilliam@Sun.COM  * and alias.  Invoked within the context of update_drv -d <alias>.
43898831SJerry.Gilliam@Sun.COM  */
43900Sstevel@tonic-gate static int
unbind_children_by_alias(dev_info_t * dip,void * arg)43918831SJerry.Gilliam@Sun.COM unbind_children_by_alias(dev_info_t *dip, void *arg)
43928831SJerry.Gilliam@Sun.COM {
43938831SJerry.Gilliam@Sun.COM 	int		circ;
43948831SJerry.Gilliam@Sun.COM 	dev_info_t	*cdip;
43958831SJerry.Gilliam@Sun.COM 	dev_info_t	*next;
43968831SJerry.Gilliam@Sun.COM 	unbind_data_t	*ub = (unbind_data_t *)(uintptr_t)arg;
43978831SJerry.Gilliam@Sun.COM 	int		rv;
43988831SJerry.Gilliam@Sun.COM 
43998831SJerry.Gilliam@Sun.COM 	/*
44008831SJerry.Gilliam@Sun.COM 	 * We are called from update_drv to try to unbind a specific
44018831SJerry.Gilliam@Sun.COM 	 * set of aliases for a driver.  Unbind what persistent nodes
44028831SJerry.Gilliam@Sun.COM 	 * we can, and return the number of nodes which cannot be unbound.
44038831SJerry.Gilliam@Sun.COM 	 * If not all nodes can be unbound, update_drv leaves the
44048831SJerry.Gilliam@Sun.COM 	 * state of the driver binding files unchanged, except in
44058831SJerry.Gilliam@Sun.COM 	 * the case of -f.
44068831SJerry.Gilliam@Sun.COM 	 */
44070Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
44088831SJerry.Gilliam@Sun.COM 	for (cdip = ddi_get_child(dip); cdip; cdip = next) {
44098831SJerry.Gilliam@Sun.COM 		next = ddi_get_next_sibling(cdip);
44108831SJerry.Gilliam@Sun.COM 		if ((ddi_driver_major(cdip) != ub->drv_major) ||
44118831SJerry.Gilliam@Sun.COM 		    (strcmp(DEVI(cdip)->devi_node_name, ub->drv_alias) != 0))
44120Sstevel@tonic-gate 			continue;
44138831SJerry.Gilliam@Sun.COM 		if (i_ddi_node_state(cdip) >= DS_BOUND) {
44148831SJerry.Gilliam@Sun.COM 			rv = ndi_devi_unbind_driver(cdip);
44158831SJerry.Gilliam@Sun.COM 			if (rv != DDI_SUCCESS ||
44168831SJerry.Gilliam@Sun.COM 			    (i_ddi_node_state(cdip) >= DS_BOUND)) {
44178831SJerry.Gilliam@Sun.COM 				unbind_alias_dev_in_use(cdip, ub->drv_alias);
44188831SJerry.Gilliam@Sun.COM 				ub->ndevs_bound++;
44198831SJerry.Gilliam@Sun.COM 				continue;
44208831SJerry.Gilliam@Sun.COM 			}
44218831SJerry.Gilliam@Sun.COM 			if (ndi_dev_is_persistent_node(cdip) == 0)
44228831SJerry.Gilliam@Sun.COM 				(void) ddi_remove_child(cdip, 0);
44230Sstevel@tonic-gate 		}
44240Sstevel@tonic-gate 	}
44250Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
44260Sstevel@tonic-gate 
44270Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
44280Sstevel@tonic-gate }
44290Sstevel@tonic-gate 
44308831SJerry.Gilliam@Sun.COM /*
44318831SJerry.Gilliam@Sun.COM  * Unbind devices by driver & alias
44328831SJerry.Gilliam@Sun.COM  * Context: update_drv [-f] -d -i <alias> <driver>
44338831SJerry.Gilliam@Sun.COM  */
44348831SJerry.Gilliam@Sun.COM int
i_ddi_unbind_devs_by_alias(major_t major,char * alias)44358831SJerry.Gilliam@Sun.COM i_ddi_unbind_devs_by_alias(major_t major, char *alias)
44368831SJerry.Gilliam@Sun.COM {
44378831SJerry.Gilliam@Sun.COM 	unbind_data_t	*ub;
44388831SJerry.Gilliam@Sun.COM 	int		rv;
44398831SJerry.Gilliam@Sun.COM 
44408831SJerry.Gilliam@Sun.COM 	ub = kmem_zalloc(sizeof (*ub), KM_SLEEP);
44418831SJerry.Gilliam@Sun.COM 	ub->drv_major = major;
44428831SJerry.Gilliam@Sun.COM 	ub->drv_alias = alias;
44438831SJerry.Gilliam@Sun.COM 	ub->ndevs_bound = 0;
44448831SJerry.Gilliam@Sun.COM 	ub->unbind_errors = 0;
44458831SJerry.Gilliam@Sun.COM 
44468831SJerry.Gilliam@Sun.COM 	/* flush devfs so that ndi_devi_unbind_driver will work when possible */
444711085SJerry.Gilliam@Sun.COM 	(void) devfs_clean(top_devinfo, NULL, 0);
44488831SJerry.Gilliam@Sun.COM 	ddi_walk_devs(top_devinfo, unbind_children_by_alias,
44498831SJerry.Gilliam@Sun.COM 	    (void *)(uintptr_t)ub);
44508831SJerry.Gilliam@Sun.COM 
44518831SJerry.Gilliam@Sun.COM 	/* return the number of devices remaining bound to the alias */
44528831SJerry.Gilliam@Sun.COM 	rv = ub->ndevs_bound + ub->unbind_errors;
44538831SJerry.Gilliam@Sun.COM 	kmem_free(ub, sizeof (*ub));
44548831SJerry.Gilliam@Sun.COM 	return (rv);
44558831SJerry.Gilliam@Sun.COM }
44568831SJerry.Gilliam@Sun.COM 
44578831SJerry.Gilliam@Sun.COM /*
44588831SJerry.Gilliam@Sun.COM  * walkdevs callback for unbind devices by driver
44598831SJerry.Gilliam@Sun.COM  */
44608831SJerry.Gilliam@Sun.COM static int
unbind_children_by_driver(dev_info_t * dip,void * arg)44618831SJerry.Gilliam@Sun.COM unbind_children_by_driver(dev_info_t *dip, void *arg)
44628831SJerry.Gilliam@Sun.COM {
44638831SJerry.Gilliam@Sun.COM 	int		circ;
44648831SJerry.Gilliam@Sun.COM 	dev_info_t	*cdip;
44658831SJerry.Gilliam@Sun.COM 	dev_info_t	*next;
44668831SJerry.Gilliam@Sun.COM 	major_t		major = (major_t)(uintptr_t)arg;
44678831SJerry.Gilliam@Sun.COM 	int		rv;
44688831SJerry.Gilliam@Sun.COM 
44698831SJerry.Gilliam@Sun.COM 	/*
44708831SJerry.Gilliam@Sun.COM 	 * We are called either from rem_drv or update_drv when reloading
44718831SJerry.Gilliam@Sun.COM 	 * a driver.conf file. In either case, we unbind persistent nodes
44728831SJerry.Gilliam@Sun.COM 	 * and destroy .conf nodes. In the case of rem_drv, this will be
447310923SEvan.Yan@Sun.COM 	 * the final state. In the case of update_drv,	i_ddi_bind_devs()
44748831SJerry.Gilliam@Sun.COM 	 * may be invoked later to re-enumerate (new) driver.conf rebind
44758831SJerry.Gilliam@Sun.COM 	 * persistent nodes.
44768831SJerry.Gilliam@Sun.COM 	 */
44778831SJerry.Gilliam@Sun.COM 	ndi_devi_enter(dip, &circ);
44788831SJerry.Gilliam@Sun.COM 	for (cdip = ddi_get_child(dip); cdip; cdip = next) {
44798831SJerry.Gilliam@Sun.COM 		next = ddi_get_next_sibling(cdip);
44808831SJerry.Gilliam@Sun.COM 		if (ddi_driver_major(cdip) != major)
44818831SJerry.Gilliam@Sun.COM 			continue;
44828831SJerry.Gilliam@Sun.COM 		if (i_ddi_node_state(cdip) >= DS_BOUND) {
44838831SJerry.Gilliam@Sun.COM 			rv = ndi_devi_unbind_driver(cdip);
44848831SJerry.Gilliam@Sun.COM 			if (rv == DDI_FAILURE ||
44858831SJerry.Gilliam@Sun.COM 			    (i_ddi_node_state(cdip) >= DS_BOUND))
44868831SJerry.Gilliam@Sun.COM 				continue;
44878831SJerry.Gilliam@Sun.COM 			if (ndi_dev_is_persistent_node(cdip) == 0)
44888831SJerry.Gilliam@Sun.COM 				(void) ddi_remove_child(cdip, 0);
44898831SJerry.Gilliam@Sun.COM 		}
44908831SJerry.Gilliam@Sun.COM 	}
44918831SJerry.Gilliam@Sun.COM 	ndi_devi_exit(dip, circ);
44928831SJerry.Gilliam@Sun.COM 
44938831SJerry.Gilliam@Sun.COM 	return (DDI_WALK_CONTINUE);
44948831SJerry.Gilliam@Sun.COM }
44958831SJerry.Gilliam@Sun.COM 
44968831SJerry.Gilliam@Sun.COM /*
44978831SJerry.Gilliam@Sun.COM  * Unbind devices by driver
44988831SJerry.Gilliam@Sun.COM  * Context: rem_drv or unload driver.conf
44998831SJerry.Gilliam@Sun.COM  */
45000Sstevel@tonic-gate void
i_ddi_unbind_devs(major_t major)45010Sstevel@tonic-gate i_ddi_unbind_devs(major_t major)
45020Sstevel@tonic-gate {
45038831SJerry.Gilliam@Sun.COM 	/* flush devfs so that ndi_devi_unbind_driver will work when possible */
450411085SJerry.Gilliam@Sun.COM 	(void) devfs_clean(top_devinfo, NULL, 0);
45058831SJerry.Gilliam@Sun.COM 	ddi_walk_devs(top_devinfo, unbind_children_by_driver,
45068831SJerry.Gilliam@Sun.COM 	    (void *)(uintptr_t)major);
45070Sstevel@tonic-gate }
45080Sstevel@tonic-gate 
45090Sstevel@tonic-gate /*
45100Sstevel@tonic-gate  * I/O Hotplug control
45110Sstevel@tonic-gate  */
45120Sstevel@tonic-gate 
45130Sstevel@tonic-gate /*
45140Sstevel@tonic-gate  * create and attach a dev_info node from a .conf file spec
45150Sstevel@tonic-gate  */
45160Sstevel@tonic-gate static void
init_spec_child(dev_info_t * pdip,struct hwc_spec * specp,uint_t flags)45170Sstevel@tonic-gate init_spec_child(dev_info_t *pdip, struct hwc_spec *specp, uint_t flags)
45180Sstevel@tonic-gate {
45190Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags))
45200Sstevel@tonic-gate 	dev_info_t *dip;
45210Sstevel@tonic-gate 	char *node_name;
45220Sstevel@tonic-gate 
45230Sstevel@tonic-gate 	if (((node_name = specp->hwc_devi_name) == NULL) ||
45247009Scth 	    (ddi_name_to_major(node_name) == DDI_MAJOR_T_NONE)) {
45250Sstevel@tonic-gate 		char *tmp = node_name;
45260Sstevel@tonic-gate 		if (tmp == NULL)
45270Sstevel@tonic-gate 			tmp = "<none>";
45280Sstevel@tonic-gate 		cmn_err(CE_CONT,
45290Sstevel@tonic-gate 		    "init_spec_child: parent=%s, bad spec (%s)\n",
45300Sstevel@tonic-gate 		    ddi_node_name(pdip), tmp);
45310Sstevel@tonic-gate 		return;
45320Sstevel@tonic-gate 	}
45330Sstevel@tonic-gate 
4534789Sahrens 	dip = i_ddi_alloc_node(pdip, node_name, (pnode_t)DEVI_PSEUDO_NODEID,
45350Sstevel@tonic-gate 	    -1, specp->hwc_devi_sys_prop_ptr, KM_SLEEP);
45360Sstevel@tonic-gate 
45370Sstevel@tonic-gate 	if (dip == NULL)
45380Sstevel@tonic-gate 		return;
45390Sstevel@tonic-gate 
45400Sstevel@tonic-gate 	if (ddi_initchild(pdip, dip) != DDI_SUCCESS)
45410Sstevel@tonic-gate 		(void) ddi_remove_child(dip, 0);
45420Sstevel@tonic-gate }
45430Sstevel@tonic-gate 
45440Sstevel@tonic-gate /*
45450Sstevel@tonic-gate  * Lookup hwc specs from hash tables and make children from the spec
45460Sstevel@tonic-gate  * Because some .conf children are "merge" nodes, we also initialize
45470Sstevel@tonic-gate  * .conf children to merge properties onto hardware nodes.
45480Sstevel@tonic-gate  *
45490Sstevel@tonic-gate  * The pdip must be held busy.
45500Sstevel@tonic-gate  */
45510Sstevel@tonic-gate int
i_ndi_make_spec_children(dev_info_t * pdip,uint_t flags)45520Sstevel@tonic-gate i_ndi_make_spec_children(dev_info_t *pdip, uint_t flags)
45530Sstevel@tonic-gate {
45540Sstevel@tonic-gate 	extern struct hwc_spec *hwc_get_child_spec(dev_info_t *, major_t);
4555298Scth 	int			circ;
4556298Scth 	struct hwc_spec		*list, *spec;
4557298Scth 
4558298Scth 	ndi_devi_enter(pdip, &circ);
4559298Scth 	if (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN) {
4560298Scth 		ndi_devi_exit(pdip, circ);
45610Sstevel@tonic-gate 		return (DDI_SUCCESS);
4562298Scth 	}
45630Sstevel@tonic-gate 
45647009Scth 	list = hwc_get_child_spec(pdip, DDI_MAJOR_T_NONE);
45650Sstevel@tonic-gate 	for (spec = list; spec != NULL; spec = spec->hwc_next) {
45660Sstevel@tonic-gate 		init_spec_child(pdip, spec, flags);
45670Sstevel@tonic-gate 	}
45680Sstevel@tonic-gate 	hwc_free_spec_list(list);
45690Sstevel@tonic-gate 
45700Sstevel@tonic-gate 	mutex_enter(&DEVI(pdip)->devi_lock);
45710Sstevel@tonic-gate 	DEVI(pdip)->devi_flags |= DEVI_MADE_CHILDREN;
45720Sstevel@tonic-gate 	mutex_exit(&DEVI(pdip)->devi_lock);
4573298Scth 	ndi_devi_exit(pdip, circ);
45740Sstevel@tonic-gate 	return (DDI_SUCCESS);
45750Sstevel@tonic-gate }
45760Sstevel@tonic-gate 
45770Sstevel@tonic-gate /*
45780Sstevel@tonic-gate  * Run initchild on all child nodes such that instance assignment
45790Sstevel@tonic-gate  * for multiport network cards are contiguous.
45800Sstevel@tonic-gate  *
45810Sstevel@tonic-gate  * The pdip must be held busy.
45820Sstevel@tonic-gate  */
45830Sstevel@tonic-gate static void
i_ndi_init_hw_children(dev_info_t * pdip,uint_t flags)45840Sstevel@tonic-gate i_ndi_init_hw_children(dev_info_t *pdip, uint_t flags)
45850Sstevel@tonic-gate {
45860Sstevel@tonic-gate 	dev_info_t *dip;
45870Sstevel@tonic-gate 
45880Sstevel@tonic-gate 	ASSERT(DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
45890Sstevel@tonic-gate 
45900Sstevel@tonic-gate 	/* contiguous instance assignment */
45910Sstevel@tonic-gate 	e_ddi_enter_instance();
45920Sstevel@tonic-gate 	dip = ddi_get_child(pdip);
45930Sstevel@tonic-gate 	while (dip) {
45940Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(dip))
45950Sstevel@tonic-gate 			(void) i_ndi_config_node(dip, DS_INITIALIZED, flags);
45960Sstevel@tonic-gate 		dip = ddi_get_next_sibling(dip);
45970Sstevel@tonic-gate 	}
45980Sstevel@tonic-gate 	e_ddi_exit_instance();
45990Sstevel@tonic-gate }
46000Sstevel@tonic-gate 
46010Sstevel@tonic-gate /*
46020Sstevel@tonic-gate  * report device status
46030Sstevel@tonic-gate  */
46040Sstevel@tonic-gate static void
i_ndi_devi_report_status_change(dev_info_t * dip,char * path)46050Sstevel@tonic-gate i_ndi_devi_report_status_change(dev_info_t *dip, char *path)
46060Sstevel@tonic-gate {
46070Sstevel@tonic-gate 	char *status;
46080Sstevel@tonic-gate 
46090Sstevel@tonic-gate 	if (!DEVI_NEED_REPORT(dip) ||
461010696SDavid.Hollister@Sun.COM 	    (i_ddi_node_state(dip) < DS_INITIALIZED) ||
461110696SDavid.Hollister@Sun.COM 	    ndi_dev_is_hidden_node(dip)) {
46120Sstevel@tonic-gate 		return;
46130Sstevel@tonic-gate 	}
46140Sstevel@tonic-gate 
461510696SDavid.Hollister@Sun.COM 	/* Invalidate the devinfo snapshot cache */
461610696SDavid.Hollister@Sun.COM 	i_ddi_di_cache_invalidate();
461710696SDavid.Hollister@Sun.COM 
461810696SDavid.Hollister@Sun.COM 	if (DEVI_IS_DEVICE_REMOVED(dip)) {
461910696SDavid.Hollister@Sun.COM 		status = "removed";
462010696SDavid.Hollister@Sun.COM 	} else if (DEVI_IS_DEVICE_OFFLINE(dip)) {
46210Sstevel@tonic-gate 		status = "offline";
46220Sstevel@tonic-gate 	} else if (DEVI_IS_DEVICE_DOWN(dip)) {
46230Sstevel@tonic-gate 		status = "down";
46240Sstevel@tonic-gate 	} else if (DEVI_IS_BUS_QUIESCED(dip)) {
46250Sstevel@tonic-gate 		status = "quiesced";
46260Sstevel@tonic-gate 	} else if (DEVI_IS_BUS_DOWN(dip)) {
46270Sstevel@tonic-gate 		status = "down";
46281333Scth 	} else if (i_ddi_devi_attached(dip)) {
46290Sstevel@tonic-gate 		status = "online";
46300Sstevel@tonic-gate 	} else {
46310Sstevel@tonic-gate 		status = "unknown";
46320Sstevel@tonic-gate 	}
46330Sstevel@tonic-gate 
46340Sstevel@tonic-gate 	if (path == NULL) {
46350Sstevel@tonic-gate 		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
46360Sstevel@tonic-gate 		cmn_err(CE_CONT, "?%s (%s%d) %s\n",
46374950Scth 		    ddi_pathname(dip, path), ddi_driver_name(dip),
46384950Scth 		    ddi_get_instance(dip), status);
46390Sstevel@tonic-gate 		kmem_free(path, MAXPATHLEN);
46400Sstevel@tonic-gate 	} else {
46410Sstevel@tonic-gate 		cmn_err(CE_CONT, "?%s (%s%d) %s\n",
46424950Scth 		    path, ddi_driver_name(dip),
46434950Scth 		    ddi_get_instance(dip), status);
46440Sstevel@tonic-gate 	}
46450Sstevel@tonic-gate 
4646495Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
46470Sstevel@tonic-gate 	DEVI_REPORT_DONE(dip);
4648495Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
46490Sstevel@tonic-gate }
46500Sstevel@tonic-gate 
46510Sstevel@tonic-gate /*
46520Sstevel@tonic-gate  * log a notification that a dev_info node has been configured.
46530Sstevel@tonic-gate  */
46540Sstevel@tonic-gate static int
i_log_devfs_add_devinfo(dev_info_t * dip,uint_t flags)46550Sstevel@tonic-gate i_log_devfs_add_devinfo(dev_info_t *dip, uint_t flags)
46560Sstevel@tonic-gate {
465710696SDavid.Hollister@Sun.COM 	int			se_err;
465810696SDavid.Hollister@Sun.COM 	char			*pathname;
465910696SDavid.Hollister@Sun.COM 	sysevent_t		*ev;
466010696SDavid.Hollister@Sun.COM 	sysevent_id_t		eid;
466110696SDavid.Hollister@Sun.COM 	sysevent_value_t	se_val;
466210696SDavid.Hollister@Sun.COM 	sysevent_attr_list_t	*ev_attr_list = NULL;
466310696SDavid.Hollister@Sun.COM 	char			*class_name;
466410696SDavid.Hollister@Sun.COM 	int			no_transport = 0;
466510696SDavid.Hollister@Sun.COM 
466610696SDavid.Hollister@Sun.COM 	ASSERT(dip && ddi_get_parent(dip) &&
466710696SDavid.Hollister@Sun.COM 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
46680Sstevel@tonic-gate 
46690Sstevel@tonic-gate 	/* do not generate ESC_DEVFS_DEVI_ADD event during boot */
46700Sstevel@tonic-gate 	if (!i_ddi_io_initialized())
46710Sstevel@tonic-gate 		return (DDI_SUCCESS);
46720Sstevel@tonic-gate 
467310696SDavid.Hollister@Sun.COM 	/* Invalidate the devinfo snapshot cache */
467410696SDavid.Hollister@Sun.COM 	i_ddi_di_cache_invalidate();
467510696SDavid.Hollister@Sun.COM 
46760Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_ADD, EP_DDI, SE_SLEEP);
46770Sstevel@tonic-gate 
46780Sstevel@tonic-gate 	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
46790Sstevel@tonic-gate 
46800Sstevel@tonic-gate 	(void) ddi_pathname(dip, pathname);
46810Sstevel@tonic-gate 	ASSERT(strlen(pathname));
46820Sstevel@tonic-gate 
46830Sstevel@tonic-gate 	se_val.value_type = SE_DATA_TYPE_STRING;
46840Sstevel@tonic-gate 	se_val.value.sv_string = pathname;
46850Sstevel@tonic-gate 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
46860Sstevel@tonic-gate 	    &se_val, SE_SLEEP) != 0) {
46870Sstevel@tonic-gate 		goto fail;
46880Sstevel@tonic-gate 	}
46890Sstevel@tonic-gate 
46900Sstevel@tonic-gate 	/* add the device class attribute */
46910Sstevel@tonic-gate 	if ((class_name = i_ddi_devi_class(dip)) != NULL) {
46920Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_STRING;
46930Sstevel@tonic-gate 		se_val.value.sv_string = class_name;
46940Sstevel@tonic-gate 
46950Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
46960Sstevel@tonic-gate 		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
46970Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
46980Sstevel@tonic-gate 			goto fail;
46990Sstevel@tonic-gate 		}
47000Sstevel@tonic-gate 	}
47010Sstevel@tonic-gate 
47020Sstevel@tonic-gate 	/*
47030Sstevel@tonic-gate 	 * must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
47040Sstevel@tonic-gate 	 * in which case the branch event will be logged by the caller
47050Sstevel@tonic-gate 	 * after the entire branch has been configured.
47060Sstevel@tonic-gate 	 */
47070Sstevel@tonic-gate 	if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
47080Sstevel@tonic-gate 		/*
47090Sstevel@tonic-gate 		 * Instead of logging a separate branch event just add
47100Sstevel@tonic-gate 		 * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
47110Sstevel@tonic-gate 		 * generate a EC_DEV_BRANCH event.
47120Sstevel@tonic-gate 		 */
47130Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_INT32;
47140Sstevel@tonic-gate 		se_val.value.sv_int32 = 1;
47150Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
47160Sstevel@tonic-gate 		    DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
47170Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
47180Sstevel@tonic-gate 			goto fail;
47190Sstevel@tonic-gate 		}
47200Sstevel@tonic-gate 	}
47210Sstevel@tonic-gate 
47220Sstevel@tonic-gate 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
47230Sstevel@tonic-gate 		sysevent_free_attr(ev_attr_list);
47240Sstevel@tonic-gate 		goto fail;
47250Sstevel@tonic-gate 	}
47260Sstevel@tonic-gate 
47270Sstevel@tonic-gate 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
47280Sstevel@tonic-gate 		if (se_err == SE_NO_TRANSPORT)
47290Sstevel@tonic-gate 			no_transport = 1;
47300Sstevel@tonic-gate 		goto fail;
47310Sstevel@tonic-gate 	}
47320Sstevel@tonic-gate 
47330Sstevel@tonic-gate 	sysevent_free(ev);
47340Sstevel@tonic-gate 	kmem_free(pathname, MAXPATHLEN);
47350Sstevel@tonic-gate 
47360Sstevel@tonic-gate 	return (DDI_SUCCESS);
47370Sstevel@tonic-gate 
47380Sstevel@tonic-gate fail:
47390Sstevel@tonic-gate 	cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_ADD event for %s%s",
47400Sstevel@tonic-gate 	    pathname, (no_transport) ? " (syseventd not responding)" : "");
47410Sstevel@tonic-gate 
47420Sstevel@tonic-gate 	cmn_err(CE_WARN, "/dev may not be current for driver %s. "
47430Sstevel@tonic-gate 	    "Run devfsadm -i %s",
47440Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_driver_name(dip));
47450Sstevel@tonic-gate 
47460Sstevel@tonic-gate 	sysevent_free(ev);
47470Sstevel@tonic-gate 	kmem_free(pathname, MAXPATHLEN);
47480Sstevel@tonic-gate 	return (DDI_SUCCESS);
47490Sstevel@tonic-gate }
47500Sstevel@tonic-gate 
47510Sstevel@tonic-gate /*
47520Sstevel@tonic-gate  * log a notification that a dev_info node has been unconfigured.
47530Sstevel@tonic-gate  */
47540Sstevel@tonic-gate static int
i_log_devfs_remove_devinfo(char * pathname,char * class_name,char * driver_name,int instance,uint_t flags)47550Sstevel@tonic-gate i_log_devfs_remove_devinfo(char *pathname, char *class_name, char *driver_name,
47560Sstevel@tonic-gate     int instance, uint_t flags)
47570Sstevel@tonic-gate {
475810696SDavid.Hollister@Sun.COM 	sysevent_t		*ev;
475910696SDavid.Hollister@Sun.COM 	sysevent_id_t		eid;
476010696SDavid.Hollister@Sun.COM 	sysevent_value_t	se_val;
476110696SDavid.Hollister@Sun.COM 	sysevent_attr_list_t	*ev_attr_list = NULL;
476210696SDavid.Hollister@Sun.COM 	int			se_err;
476310696SDavid.Hollister@Sun.COM 	int			no_transport = 0;
47640Sstevel@tonic-gate 
47650Sstevel@tonic-gate 	if (!i_ddi_io_initialized())
47660Sstevel@tonic-gate 		return (DDI_SUCCESS);
47670Sstevel@tonic-gate 
476810696SDavid.Hollister@Sun.COM 	/* Invalidate the devinfo snapshot cache */
476910696SDavid.Hollister@Sun.COM 	i_ddi_di_cache_invalidate();
477010696SDavid.Hollister@Sun.COM 
47710Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_REMOVE, EP_DDI, SE_SLEEP);
47720Sstevel@tonic-gate 
47730Sstevel@tonic-gate 	se_val.value_type = SE_DATA_TYPE_STRING;
47740Sstevel@tonic-gate 	se_val.value.sv_string = pathname;
47750Sstevel@tonic-gate 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
47760Sstevel@tonic-gate 	    &se_val, SE_SLEEP) != 0) {
47770Sstevel@tonic-gate 		goto fail;
47780Sstevel@tonic-gate 	}
47790Sstevel@tonic-gate 
47800Sstevel@tonic-gate 	if (class_name) {
47810Sstevel@tonic-gate 		/* add the device class, driver name and instance attributes */
47820Sstevel@tonic-gate 
47830Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_STRING;
47840Sstevel@tonic-gate 		se_val.value.sv_string = class_name;
47850Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
47860Sstevel@tonic-gate 		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
47870Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
47880Sstevel@tonic-gate 			goto fail;
47890Sstevel@tonic-gate 		}
47900Sstevel@tonic-gate 
47910Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_STRING;
47920Sstevel@tonic-gate 		se_val.value.sv_string = driver_name;
47930Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
47940Sstevel@tonic-gate 		    DEVFS_DRIVER_NAME, &se_val, SE_SLEEP) != 0) {
47950Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
47960Sstevel@tonic-gate 			goto fail;
47970Sstevel@tonic-gate 		}
47980Sstevel@tonic-gate 
47990Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_INT32;
48000Sstevel@tonic-gate 		se_val.value.sv_int32 = instance;
48010Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
48020Sstevel@tonic-gate 		    DEVFS_INSTANCE, &se_val, SE_SLEEP) != 0) {
48030Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
48040Sstevel@tonic-gate 			goto fail;
48050Sstevel@tonic-gate 		}
48060Sstevel@tonic-gate 	}
48070Sstevel@tonic-gate 
48080Sstevel@tonic-gate 	/*
48090Sstevel@tonic-gate 	 * must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
48100Sstevel@tonic-gate 	 * in which case the branch event will be logged by the caller
48110Sstevel@tonic-gate 	 * after the entire branch has been unconfigured.
48120Sstevel@tonic-gate 	 */
48130Sstevel@tonic-gate 	if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
48140Sstevel@tonic-gate 		/*
48150Sstevel@tonic-gate 		 * Instead of logging a separate branch event just add
48160Sstevel@tonic-gate 		 * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
48170Sstevel@tonic-gate 		 * generate a EC_DEV_BRANCH event.
48180Sstevel@tonic-gate 		 */
48190Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_INT32;
48200Sstevel@tonic-gate 		se_val.value.sv_int32 = 1;
48210Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
48220Sstevel@tonic-gate 		    DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
48230Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
48240Sstevel@tonic-gate 			goto fail;
48250Sstevel@tonic-gate 		}
48260Sstevel@tonic-gate 	}
48270Sstevel@tonic-gate 
48280Sstevel@tonic-gate 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
48290Sstevel@tonic-gate 		sysevent_free_attr(ev_attr_list);
48300Sstevel@tonic-gate 		goto fail;
48310Sstevel@tonic-gate 	}
48320Sstevel@tonic-gate 
48330Sstevel@tonic-gate 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
48340Sstevel@tonic-gate 		if (se_err == SE_NO_TRANSPORT)
48350Sstevel@tonic-gate 			no_transport = 1;
48360Sstevel@tonic-gate 		goto fail;
48370Sstevel@tonic-gate 	}
48380Sstevel@tonic-gate 
48390Sstevel@tonic-gate 	sysevent_free(ev);
48400Sstevel@tonic-gate 	return (DDI_SUCCESS);
48410Sstevel@tonic-gate 
48420Sstevel@tonic-gate fail:
48430Sstevel@tonic-gate 	sysevent_free(ev);
48440Sstevel@tonic-gate 	cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_REMOVE event for %s%s",
48450Sstevel@tonic-gate 	    pathname, (no_transport) ? " (syseventd not responding)" : "");
48460Sstevel@tonic-gate 	return (DDI_SUCCESS);
48470Sstevel@tonic-gate }
48480Sstevel@tonic-gate 
484910696SDavid.Hollister@Sun.COM static void
i_ddi_log_devfs_device_remove(dev_info_t * dip)485010696SDavid.Hollister@Sun.COM i_ddi_log_devfs_device_remove(dev_info_t *dip)
485110696SDavid.Hollister@Sun.COM {
485210696SDavid.Hollister@Sun.COM 	char	*path;
485310696SDavid.Hollister@Sun.COM 
485410696SDavid.Hollister@Sun.COM 	ASSERT(dip && ddi_get_parent(dip) &&
485510696SDavid.Hollister@Sun.COM 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
485610696SDavid.Hollister@Sun.COM 	ASSERT(DEVI_IS_DEVICE_REMOVED(dip));
485710696SDavid.Hollister@Sun.COM 
485810696SDavid.Hollister@Sun.COM 	ASSERT(i_ddi_node_state(dip) >= DS_INITIALIZED);
485910696SDavid.Hollister@Sun.COM 	if (i_ddi_node_state(dip) < DS_INITIALIZED)
486010696SDavid.Hollister@Sun.COM 		return;
486110696SDavid.Hollister@Sun.COM 
486212288SChris.Horne@Sun.COM 	/* Inform LDI_EV_DEVICE_REMOVE callbacks. */
486312288SChris.Horne@Sun.COM 	ldi_invoke_finalize(dip, DDI_DEV_T_ANY, 0, LDI_EV_DEVICE_REMOVE,
486412288SChris.Horne@Sun.COM 	    LDI_EV_SUCCESS, NULL);
486512288SChris.Horne@Sun.COM 
486612288SChris.Horne@Sun.COM 	/* Generate EC_DEVFS_DEVI_REMOVE sysevent. */
486710696SDavid.Hollister@Sun.COM 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
486810696SDavid.Hollister@Sun.COM 	(void) i_log_devfs_remove_devinfo(ddi_pathname(dip, path),
486910696SDavid.Hollister@Sun.COM 	    i_ddi_devi_class(dip), (char *)ddi_driver_name(dip),
487010696SDavid.Hollister@Sun.COM 	    ddi_get_instance(dip), 0);
487110696SDavid.Hollister@Sun.COM 	kmem_free(path, MAXPATHLEN);
487210696SDavid.Hollister@Sun.COM }
487310696SDavid.Hollister@Sun.COM 
487410696SDavid.Hollister@Sun.COM static void
i_ddi_log_devfs_device_insert(dev_info_t * dip)487510696SDavid.Hollister@Sun.COM i_ddi_log_devfs_device_insert(dev_info_t *dip)
487610696SDavid.Hollister@Sun.COM {
487710696SDavid.Hollister@Sun.COM 	ASSERT(dip && ddi_get_parent(dip) &&
487810696SDavid.Hollister@Sun.COM 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
487910696SDavid.Hollister@Sun.COM 	ASSERT(!DEVI_IS_DEVICE_REMOVED(dip));
488010696SDavid.Hollister@Sun.COM 
488110696SDavid.Hollister@Sun.COM 	(void) i_log_devfs_add_devinfo(dip, 0);
488210696SDavid.Hollister@Sun.COM }
488310696SDavid.Hollister@Sun.COM 
488410696SDavid.Hollister@Sun.COM 
48850Sstevel@tonic-gate /*
48860Sstevel@tonic-gate  * log an event that a dev_info branch has been configured or unconfigured.
48870Sstevel@tonic-gate  */
48880Sstevel@tonic-gate static int
i_log_devfs_branch(char * node_path,char * subclass)48890Sstevel@tonic-gate i_log_devfs_branch(char *node_path, char *subclass)
48900Sstevel@tonic-gate {
48910Sstevel@tonic-gate 	int se_err;
48920Sstevel@tonic-gate 	sysevent_t *ev;
48930Sstevel@tonic-gate 	sysevent_id_t eid;
48940Sstevel@tonic-gate 	sysevent_value_t se_val;
48950Sstevel@tonic-gate 	sysevent_attr_list_t *ev_attr_list = NULL;
48960Sstevel@tonic-gate 	int no_transport = 0;
48970Sstevel@tonic-gate 
48980Sstevel@tonic-gate 	/* do not generate the event during boot */
48990Sstevel@tonic-gate 	if (!i_ddi_io_initialized())
49000Sstevel@tonic-gate 		return (DDI_SUCCESS);
49010Sstevel@tonic-gate 
490210696SDavid.Hollister@Sun.COM 	/* Invalidate the devinfo snapshot cache */
490310696SDavid.Hollister@Sun.COM 	i_ddi_di_cache_invalidate();
490410696SDavid.Hollister@Sun.COM 
49050Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, subclass, EP_DDI, SE_SLEEP);
49060Sstevel@tonic-gate 
49070Sstevel@tonic-gate 	se_val.value_type = SE_DATA_TYPE_STRING;
49080Sstevel@tonic-gate 	se_val.value.sv_string = node_path;
49090Sstevel@tonic-gate 
49100Sstevel@tonic-gate 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
49110Sstevel@tonic-gate 	    &se_val, SE_SLEEP) != 0) {
49120Sstevel@tonic-gate 		goto fail;
49130Sstevel@tonic-gate 	}
49140Sstevel@tonic-gate 
49150Sstevel@tonic-gate 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
49160Sstevel@tonic-gate 		sysevent_free_attr(ev_attr_list);
49170Sstevel@tonic-gate 		goto fail;
49180Sstevel@tonic-gate 	}
49190Sstevel@tonic-gate 
49200Sstevel@tonic-gate 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
49210Sstevel@tonic-gate 		if (se_err == SE_NO_TRANSPORT)
49220Sstevel@tonic-gate 			no_transport = 1;
49230Sstevel@tonic-gate 		goto fail;
49240Sstevel@tonic-gate 	}
49250Sstevel@tonic-gate 
49260Sstevel@tonic-gate 	sysevent_free(ev);
49270Sstevel@tonic-gate 	return (DDI_SUCCESS);
49280Sstevel@tonic-gate 
49290Sstevel@tonic-gate fail:
49300Sstevel@tonic-gate 	cmn_err(CE_WARN, "failed to log %s branch event for %s%s",
49310Sstevel@tonic-gate 	    subclass, node_path,
49320Sstevel@tonic-gate 	    (no_transport) ? " (syseventd not responding)" : "");
49330Sstevel@tonic-gate 
49340Sstevel@tonic-gate 	sysevent_free(ev);
49350Sstevel@tonic-gate 	return (DDI_FAILURE);
49360Sstevel@tonic-gate }
49370Sstevel@tonic-gate 
49380Sstevel@tonic-gate /*
49390Sstevel@tonic-gate  * log an event that a dev_info tree branch has been configured.
49400Sstevel@tonic-gate  */
49410Sstevel@tonic-gate static int
i_log_devfs_branch_add(dev_info_t * dip)49420Sstevel@tonic-gate i_log_devfs_branch_add(dev_info_t *dip)
49430Sstevel@tonic-gate {
49440Sstevel@tonic-gate 	char *node_path;
49450Sstevel@tonic-gate 	int rv;
49460Sstevel@tonic-gate 
49470Sstevel@tonic-gate 	node_path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
49480Sstevel@tonic-gate 	(void) ddi_pathname(dip, node_path);
49490Sstevel@tonic-gate 	rv = i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_ADD);
49500Sstevel@tonic-gate 	kmem_free(node_path, MAXPATHLEN);
49510Sstevel@tonic-gate 
49520Sstevel@tonic-gate 	return (rv);
49530Sstevel@tonic-gate }
49540Sstevel@tonic-gate 
49550Sstevel@tonic-gate /*
49560Sstevel@tonic-gate  * log an event that a dev_info tree branch has been unconfigured.
49570Sstevel@tonic-gate  */
49580Sstevel@tonic-gate static int
i_log_devfs_branch_remove(char * node_path)49590Sstevel@tonic-gate i_log_devfs_branch_remove(char *node_path)
49600Sstevel@tonic-gate {
49610Sstevel@tonic-gate 	return (i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_REMOVE));
49620Sstevel@tonic-gate }
49630Sstevel@tonic-gate 
49640Sstevel@tonic-gate /*
49650Sstevel@tonic-gate  * enqueue the dip's deviname on the branch event queue.
49660Sstevel@tonic-gate  */
49670Sstevel@tonic-gate static struct brevq_node *
brevq_enqueue(struct brevq_node ** brevqp,dev_info_t * dip,struct brevq_node * child)49680Sstevel@tonic-gate brevq_enqueue(struct brevq_node **brevqp, dev_info_t *dip,
49690Sstevel@tonic-gate     struct brevq_node *child)
49700Sstevel@tonic-gate {
49710Sstevel@tonic-gate 	struct brevq_node *brn;
49720Sstevel@tonic-gate 	char *deviname;
49730Sstevel@tonic-gate 
49740Sstevel@tonic-gate 	deviname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
49750Sstevel@tonic-gate 	(void) ddi_deviname(dip, deviname);
49760Sstevel@tonic-gate 
49770Sstevel@tonic-gate 	brn = kmem_zalloc(sizeof (*brn), KM_SLEEP);
49781317Scth 	brn->brn_deviname = i_ddi_strdup(deviname, KM_SLEEP);
49790Sstevel@tonic-gate 	kmem_free(deviname, MAXNAMELEN);
49801317Scth 	brn->brn_child = child;
49811317Scth 	brn->brn_sibling = *brevqp;
49820Sstevel@tonic-gate 	*brevqp = brn;
49830Sstevel@tonic-gate 
49840Sstevel@tonic-gate 	return (brn);
49850Sstevel@tonic-gate }
49860Sstevel@tonic-gate 
49870Sstevel@tonic-gate /*
49880Sstevel@tonic-gate  * free the memory allocated for the elements on the branch event queue.
49890Sstevel@tonic-gate  */
49900Sstevel@tonic-gate static void
free_brevq(struct brevq_node * brevq)49910Sstevel@tonic-gate free_brevq(struct brevq_node *brevq)
49920Sstevel@tonic-gate {
49930Sstevel@tonic-gate 	struct brevq_node *brn, *next_brn;
49940Sstevel@tonic-gate 
49950Sstevel@tonic-gate 	for (brn = brevq; brn != NULL; brn = next_brn) {
49961317Scth 		next_brn = brn->brn_sibling;
49971317Scth 		ASSERT(brn->brn_child == NULL);
49981317Scth 		kmem_free(brn->brn_deviname, strlen(brn->brn_deviname) + 1);
49990Sstevel@tonic-gate 		kmem_free(brn, sizeof (*brn));
50000Sstevel@tonic-gate 	}
50010Sstevel@tonic-gate }
50020Sstevel@tonic-gate 
50030Sstevel@tonic-gate /*
50040Sstevel@tonic-gate  * log the events queued up on the branch event queue and free the
50050Sstevel@tonic-gate  * associated memory.
50060Sstevel@tonic-gate  *
50070Sstevel@tonic-gate  * node_path must have been allocated with at least MAXPATHLEN bytes.
50080Sstevel@tonic-gate  */
50090Sstevel@tonic-gate static void
log_and_free_brevq(char * node_path,struct brevq_node * brevq)50100Sstevel@tonic-gate log_and_free_brevq(char *node_path, struct brevq_node *brevq)
50110Sstevel@tonic-gate {
50120Sstevel@tonic-gate 	struct brevq_node *brn;
50130Sstevel@tonic-gate 	char *p;
50140Sstevel@tonic-gate 
50150Sstevel@tonic-gate 	p = node_path + strlen(node_path);
50161317Scth 	for (brn = brevq; brn != NULL; brn = brn->brn_sibling) {
50171317Scth 		(void) strcpy(p, brn->brn_deviname);
50180Sstevel@tonic-gate 		(void) i_log_devfs_branch_remove(node_path);
50190Sstevel@tonic-gate 	}
50200Sstevel@tonic-gate 	*p = '\0';
50210Sstevel@tonic-gate 
50220Sstevel@tonic-gate 	free_brevq(brevq);
50230Sstevel@tonic-gate }
50240Sstevel@tonic-gate 
50250Sstevel@tonic-gate /*
50260Sstevel@tonic-gate  * log the events queued up on the branch event queue and free the
50270Sstevel@tonic-gate  * associated memory. Same as the previous function but operates on dip.
50280Sstevel@tonic-gate  */
50290Sstevel@tonic-gate static void
log_and_free_brevq_dip(dev_info_t * dip,struct brevq_node * brevq)50300Sstevel@tonic-gate log_and_free_brevq_dip(dev_info_t *dip, struct brevq_node *brevq)
50310Sstevel@tonic-gate {
50320Sstevel@tonic-gate 	char *path;
50330Sstevel@tonic-gate 
50340Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
50350Sstevel@tonic-gate 	(void) ddi_pathname(dip, path);
50360Sstevel@tonic-gate 	log_and_free_brevq(path, brevq);
50370Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
50380Sstevel@tonic-gate }
50390Sstevel@tonic-gate 
50400Sstevel@tonic-gate /*
50410Sstevel@tonic-gate  * log the outstanding branch remove events for the grand children of the dip
50420Sstevel@tonic-gate  * and free the associated memory.
50430Sstevel@tonic-gate  */
50440Sstevel@tonic-gate static void
log_and_free_br_events_on_grand_children(dev_info_t * dip,struct brevq_node * brevq)50450Sstevel@tonic-gate log_and_free_br_events_on_grand_children(dev_info_t *dip,
50460Sstevel@tonic-gate     struct brevq_node *brevq)
50470Sstevel@tonic-gate {
50480Sstevel@tonic-gate 	struct brevq_node *brn;
50490Sstevel@tonic-gate 	char *path;
50500Sstevel@tonic-gate 	char *p;
50510Sstevel@tonic-gate 
50520Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
50530Sstevel@tonic-gate 	(void) ddi_pathname(dip, path);
50540Sstevel@tonic-gate 	p = path + strlen(path);
50551317Scth 	for (brn = brevq; brn != NULL; brn = brn->brn_sibling) {
50561317Scth 		if (brn->brn_child) {
50571317Scth 			(void) strcpy(p, brn->brn_deviname);
50580Sstevel@tonic-gate 			/* now path contains the node path to the dip's child */
50591317Scth 			log_and_free_brevq(path, brn->brn_child);
50601317Scth 			brn->brn_child = NULL;
50610Sstevel@tonic-gate 		}
50620Sstevel@tonic-gate 	}
50630Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
50640Sstevel@tonic-gate }
50650Sstevel@tonic-gate 
50660Sstevel@tonic-gate /*
50670Sstevel@tonic-gate  * log and cleanup branch remove events for the grand children of the dip.
50680Sstevel@tonic-gate  */
50690Sstevel@tonic-gate static void
cleanup_br_events_on_grand_children(dev_info_t * dip,struct brevq_node ** brevqp)50700Sstevel@tonic-gate cleanup_br_events_on_grand_children(dev_info_t *dip, struct brevq_node **brevqp)
50710Sstevel@tonic-gate {
50720Sstevel@tonic-gate 	dev_info_t *child;
50730Sstevel@tonic-gate 	struct brevq_node *brevq, *brn, *prev_brn, *next_brn;
50740Sstevel@tonic-gate 	char *path;
50750Sstevel@tonic-gate 	int circ;
50760Sstevel@tonic-gate 
50770Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
50780Sstevel@tonic-gate 	prev_brn = NULL;
50790Sstevel@tonic-gate 	brevq = *brevqp;
50800Sstevel@tonic-gate 
50810Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
50820Sstevel@tonic-gate 	for (brn = brevq; brn != NULL; brn = next_brn) {
50831317Scth 		next_brn = brn->brn_sibling;
50840Sstevel@tonic-gate 		for (child = ddi_get_child(dip); child != NULL;
50850Sstevel@tonic-gate 		    child = ddi_get_next_sibling(child)) {
50860Sstevel@tonic-gate 			if (i_ddi_node_state(child) >= DS_INITIALIZED) {
50870Sstevel@tonic-gate 				(void) ddi_deviname(child, path);
50881317Scth 				if (strcmp(path, brn->brn_deviname) == 0)
50890Sstevel@tonic-gate 					break;
50900Sstevel@tonic-gate 			}
50910Sstevel@tonic-gate 		}
50920Sstevel@tonic-gate 
50930Sstevel@tonic-gate 		if (child != NULL && !(DEVI_EVREMOVE(child))) {
50940Sstevel@tonic-gate 			/*
50950Sstevel@tonic-gate 			 * Event state is not REMOVE. So branch remove event
50961317Scth 			 * is not going be generated on brn->brn_child.
50970Sstevel@tonic-gate 			 * If any branch remove events were queued up on
50981317Scth 			 * brn->brn_child log them and remove the brn
50990Sstevel@tonic-gate 			 * from the queue.
51000Sstevel@tonic-gate 			 */
51011317Scth 			if (brn->brn_child) {
51020Sstevel@tonic-gate 				(void) ddi_pathname(dip, path);
51031317Scth 				(void) strcat(path, brn->brn_deviname);
51041317Scth 				log_and_free_brevq(path, brn->brn_child);
51050Sstevel@tonic-gate 			}
51060Sstevel@tonic-gate 
51070Sstevel@tonic-gate 			if (prev_brn)
51081317Scth 				prev_brn->brn_sibling = next_brn;
51090Sstevel@tonic-gate 			else
51100Sstevel@tonic-gate 				*brevqp = next_brn;
51110Sstevel@tonic-gate 
51121317Scth 			kmem_free(brn->brn_deviname,
51131317Scth 			    strlen(brn->brn_deviname) + 1);
51140Sstevel@tonic-gate 			kmem_free(brn, sizeof (*brn));
51150Sstevel@tonic-gate 		} else {
51160Sstevel@tonic-gate 			/*
51170Sstevel@tonic-gate 			 * Free up the outstanding branch remove events
51181317Scth 			 * queued on brn->brn_child since brn->brn_child
51190Sstevel@tonic-gate 			 * itself is eligible for branch remove event.
51200Sstevel@tonic-gate 			 */
51211317Scth 			if (brn->brn_child) {
51221317Scth 				free_brevq(brn->brn_child);
51231317Scth 				brn->brn_child = NULL;
51240Sstevel@tonic-gate 			}
51250Sstevel@tonic-gate 			prev_brn = brn;
51260Sstevel@tonic-gate 		}
51270Sstevel@tonic-gate 	}
51280Sstevel@tonic-gate 
51290Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
51300Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
51310Sstevel@tonic-gate }
51320Sstevel@tonic-gate 
51330Sstevel@tonic-gate static int
need_remove_event(dev_info_t * dip,int flags)51340Sstevel@tonic-gate need_remove_event(dev_info_t *dip, int flags)
51350Sstevel@tonic-gate {
51360Sstevel@tonic-gate 	if ((flags & (NDI_NO_EVENT | NDI_AUTODETACH)) == 0 &&
51370Sstevel@tonic-gate 	    (flags & (NDI_DEVI_OFFLINE | NDI_UNCONFIG | NDI_DEVI_REMOVE)) &&
51380Sstevel@tonic-gate 	    !(DEVI_EVREMOVE(dip)))
51390Sstevel@tonic-gate 		return (1);
51400Sstevel@tonic-gate 	else
51410Sstevel@tonic-gate 		return (0);
51420Sstevel@tonic-gate }
51430Sstevel@tonic-gate 
51440Sstevel@tonic-gate /*
51450Sstevel@tonic-gate  * Unconfigure children/descendants of the dip.
51460Sstevel@tonic-gate  *
51470Sstevel@tonic-gate  * If the operation involves a branch event NDI_BRANCH_EVENT_OP is set
51480Sstevel@tonic-gate  * through out the unconfiguration. On successful return *brevqp is set to
51490Sstevel@tonic-gate  * a queue of dip's child devinames for which branch remove events need
51500Sstevel@tonic-gate  * to be generated.
51510Sstevel@tonic-gate  */
51520Sstevel@tonic-gate static int
devi_unconfig_branch(dev_info_t * dip,dev_info_t ** dipp,int flags,struct brevq_node ** brevqp)51530Sstevel@tonic-gate devi_unconfig_branch(dev_info_t *dip, dev_info_t **dipp, int flags,
51540Sstevel@tonic-gate     struct brevq_node **brevqp)
51550Sstevel@tonic-gate {
51560Sstevel@tonic-gate 	int rval;
51570Sstevel@tonic-gate 
51580Sstevel@tonic-gate 	*brevqp = NULL;
51590Sstevel@tonic-gate 
51600Sstevel@tonic-gate 	if ((!(flags & NDI_BRANCH_EVENT_OP)) && need_remove_event(dip, flags))
51610Sstevel@tonic-gate 		flags |= NDI_BRANCH_EVENT_OP;
51620Sstevel@tonic-gate 
51630Sstevel@tonic-gate 	if (flags & NDI_BRANCH_EVENT_OP) {
51647009Scth 		rval = devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE,
51650Sstevel@tonic-gate 		    brevqp);
51660Sstevel@tonic-gate 
51670Sstevel@tonic-gate 		if (rval != NDI_SUCCESS && (*brevqp)) {
51680Sstevel@tonic-gate 			log_and_free_brevq_dip(dip, *brevqp);
51690Sstevel@tonic-gate 			*brevqp = NULL;
51700Sstevel@tonic-gate 		}
51710Sstevel@tonic-gate 	} else
51727009Scth 		rval = devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE,
51730Sstevel@tonic-gate 		    NULL);
51740Sstevel@tonic-gate 
51750Sstevel@tonic-gate 	return (rval);
51760Sstevel@tonic-gate }
51770Sstevel@tonic-gate 
51780Sstevel@tonic-gate /*
51790Sstevel@tonic-gate  * If the dip is already bound to a driver transition to DS_INITIALIZED
51800Sstevel@tonic-gate  * in order to generate an event in the case where the node was left in
51810Sstevel@tonic-gate  * DS_BOUND state since boot (never got attached) and the node is now
51820Sstevel@tonic-gate  * being offlined.
51830Sstevel@tonic-gate  */
51840Sstevel@tonic-gate static void
init_bound_node_ev(dev_info_t * pdip,dev_info_t * dip,int flags)51850Sstevel@tonic-gate init_bound_node_ev(dev_info_t *pdip, dev_info_t *dip, int flags)
51860Sstevel@tonic-gate {
51870Sstevel@tonic-gate 	if (need_remove_event(dip, flags) &&
51880Sstevel@tonic-gate 	    i_ddi_node_state(dip) == DS_BOUND &&
51891333Scth 	    i_ddi_devi_attached(pdip) && !DEVI_IS_DEVICE_OFFLINE(dip))
51900Sstevel@tonic-gate 		(void) ddi_initchild(pdip, dip);
51910Sstevel@tonic-gate }
51920Sstevel@tonic-gate 
51930Sstevel@tonic-gate /*
51940Sstevel@tonic-gate  * attach a node/branch with parent already held busy
51950Sstevel@tonic-gate  */
51960Sstevel@tonic-gate static int
devi_attach_node(dev_info_t * dip,uint_t flags)51970Sstevel@tonic-gate devi_attach_node(dev_info_t *dip, uint_t flags)
51980Sstevel@tonic-gate {
51992155Scth 	dev_info_t *pdip = ddi_get_parent(dip);
52002155Scth 
52012155Scth 	ASSERT(pdip && DEVI_BUSY_OWNED(pdip));
52022155Scth 
5203495Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
52040Sstevel@tonic-gate 	if (flags & NDI_DEVI_ONLINE) {
52051333Scth 		if (!i_ddi_devi_attached(dip))
5206495Scth 			DEVI_SET_REPORT(dip);
52070Sstevel@tonic-gate 		DEVI_SET_DEVICE_ONLINE(dip);
52080Sstevel@tonic-gate 	}
52090Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
5210495Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
52110Sstevel@tonic-gate 		return (NDI_FAILURE);
52120Sstevel@tonic-gate 	}
5213495Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
52140Sstevel@tonic-gate 
52150Sstevel@tonic-gate 	if (i_ddi_attachchild(dip) != DDI_SUCCESS) {
5216495Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
52170Sstevel@tonic-gate 		DEVI_SET_EVUNINIT(dip);
5218495Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
5219495Scth 
52200Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(dip))
52210Sstevel@tonic-gate 			(void) ddi_uninitchild(dip);
52220Sstevel@tonic-gate 		else {
52230Sstevel@tonic-gate 			/*
52240Sstevel@tonic-gate 			 * Delete .conf nodes and nodes that are not
52250Sstevel@tonic-gate 			 * well formed.
52260Sstevel@tonic-gate 			 */
52270Sstevel@tonic-gate 			(void) ddi_remove_child(dip, 0);
52280Sstevel@tonic-gate 		}
52290Sstevel@tonic-gate 		return (NDI_FAILURE);
52300Sstevel@tonic-gate 	}
52310Sstevel@tonic-gate 
52320Sstevel@tonic-gate 	i_ndi_devi_report_status_change(dip, NULL);
52330Sstevel@tonic-gate 
52340Sstevel@tonic-gate 	/*
52350Sstevel@tonic-gate 	 * log an event, but not during devfs lookups in which case
52360Sstevel@tonic-gate 	 * NDI_NO_EVENT is set.
52370Sstevel@tonic-gate 	 */
52380Sstevel@tonic-gate 	if ((flags & NDI_NO_EVENT) == 0 && !(DEVI_EVADD(dip))) {
52390Sstevel@tonic-gate 		(void) i_log_devfs_add_devinfo(dip, flags);
5240495Scth 
5241495Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
52420Sstevel@tonic-gate 		DEVI_SET_EVADD(dip);
5243495Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
5244495Scth 	} else if (!(flags & NDI_NO_EVENT_STATE_CHNG)) {
5245495Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
52460Sstevel@tonic-gate 		DEVI_SET_EVADD(dip);
5247495Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
5248495Scth 	}
52490Sstevel@tonic-gate 
52500Sstevel@tonic-gate 	return (NDI_SUCCESS);
52510Sstevel@tonic-gate }
52520Sstevel@tonic-gate 
52530Sstevel@tonic-gate /* internal function to config immediate children */
52540Sstevel@tonic-gate static int
config_immediate_children(dev_info_t * pdip,uint_t flags,major_t major)52550Sstevel@tonic-gate config_immediate_children(dev_info_t *pdip, uint_t flags, major_t major)
52560Sstevel@tonic-gate {
52572155Scth 	dev_info_t	*child, *next;
52582155Scth 	int		circ;
52592155Scth 
52601333Scth 	ASSERT(i_ddi_devi_attached(pdip));
52610Sstevel@tonic-gate 
52620Sstevel@tonic-gate 	if (!NEXUS_DRV(ddi_get_driver(pdip)))
52630Sstevel@tonic-gate 		return (NDI_SUCCESS);
52640Sstevel@tonic-gate 
52650Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
52660Sstevel@tonic-gate 	    "config_immediate_children: %s%d (%p), flags=%x\n",
52670Sstevel@tonic-gate 	    ddi_driver_name(pdip), ddi_get_instance(pdip),
52680Sstevel@tonic-gate 	    (void *)pdip, flags));
52690Sstevel@tonic-gate 
52700Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
52710Sstevel@tonic-gate 
52720Sstevel@tonic-gate 	if (flags & NDI_CONFIG_REPROBE) {
52730Sstevel@tonic-gate 		mutex_enter(&DEVI(pdip)->devi_lock);
52740Sstevel@tonic-gate 		DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
52750Sstevel@tonic-gate 		mutex_exit(&DEVI(pdip)->devi_lock);
52760Sstevel@tonic-gate 	}
52770Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(pdip, flags);
52780Sstevel@tonic-gate 	i_ndi_init_hw_children(pdip, flags);
52792155Scth 
52802155Scth 	child = ddi_get_child(pdip);
52812155Scth 	while (child) {
52822155Scth 		/* NOTE: devi_attach_node() may remove the dip */
52832155Scth 		next = ddi_get_next_sibling(child);
52842155Scth 
52852155Scth 		/*
52862155Scth 		 * Configure all nexus nodes or leaf nodes with
52872155Scth 		 * matching driver major
52882155Scth 		 */
52897009Scth 		if ((major == DDI_MAJOR_T_NONE) ||
52902155Scth 		    (major == ddi_driver_major(child)) ||
52912155Scth 		    ((flags & NDI_CONFIG) && (is_leaf_node(child) == 0)))
52922155Scth 			(void) devi_attach_node(child, flags);
52932155Scth 		child = next;
52942155Scth 	}
52950Sstevel@tonic-gate 
52960Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
52970Sstevel@tonic-gate 
52980Sstevel@tonic-gate 	return (NDI_SUCCESS);
52990Sstevel@tonic-gate }
53000Sstevel@tonic-gate 
53010Sstevel@tonic-gate /* internal function to config grand children */
53020Sstevel@tonic-gate static int
config_grand_children(dev_info_t * pdip,uint_t flags,major_t major)53030Sstevel@tonic-gate config_grand_children(dev_info_t *pdip, uint_t flags, major_t major)
53040Sstevel@tonic-gate {
53050Sstevel@tonic-gate 	struct mt_config_handle *hdl;
53060Sstevel@tonic-gate 
53070Sstevel@tonic-gate 	/* multi-threaded configuration of child nexus */
53080Sstevel@tonic-gate 	hdl = mt_config_init(pdip, NULL, flags, major, MT_CONFIG_OP, NULL);
53090Sstevel@tonic-gate 	mt_config_children(hdl);
53100Sstevel@tonic-gate 
53110Sstevel@tonic-gate 	return (mt_config_fini(hdl));	/* wait for threads to exit */
53120Sstevel@tonic-gate }
53130Sstevel@tonic-gate 
53140Sstevel@tonic-gate /*
53150Sstevel@tonic-gate  * Common function for device tree configuration,
53160Sstevel@tonic-gate  * either BUS_CONFIG_ALL or BUS_CONFIG_DRIVER.
53170Sstevel@tonic-gate  * The NDI_CONFIG flag causes recursive configuration of
53180Sstevel@tonic-gate  * grandchildren, devfs usage should not recurse.
53190Sstevel@tonic-gate  */
53200Sstevel@tonic-gate static int
devi_config_common(dev_info_t * dip,int flags,major_t major)53210Sstevel@tonic-gate devi_config_common(dev_info_t *dip, int flags, major_t major)
53220Sstevel@tonic-gate {
53230Sstevel@tonic-gate 	int error;
53240Sstevel@tonic-gate 	int (*f)();
53250Sstevel@tonic-gate 
53261333Scth 	if (!i_ddi_devi_attached(dip))
53270Sstevel@tonic-gate 		return (NDI_FAILURE);
53280Sstevel@tonic-gate 
53290Sstevel@tonic-gate 	if (pm_pre_config(dip, NULL) != DDI_SUCCESS)
53300Sstevel@tonic-gate 		return (NDI_FAILURE);
53310Sstevel@tonic-gate 
53320Sstevel@tonic-gate 	if ((DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
53330Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
53340Sstevel@tonic-gate 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
53350Sstevel@tonic-gate 		error = config_immediate_children(dip, flags, major);
53360Sstevel@tonic-gate 	} else {
53370Sstevel@tonic-gate 		/* call bus_config entry point */
53387009Scth 		ddi_bus_config_op_t bus_op = (major == DDI_MAJOR_T_NONE) ?
53390Sstevel@tonic-gate 		    BUS_CONFIG_ALL : BUS_CONFIG_DRIVER;
53400Sstevel@tonic-gate 		error = (*f)(dip,
53410Sstevel@tonic-gate 		    flags, bus_op, (void *)(uintptr_t)major, NULL, 0);
53420Sstevel@tonic-gate 	}
53430Sstevel@tonic-gate 
53440Sstevel@tonic-gate 	if (error) {
53450Sstevel@tonic-gate 		pm_post_config(dip, NULL);
53460Sstevel@tonic-gate 		return (error);
53470Sstevel@tonic-gate 	}
53480Sstevel@tonic-gate 
53490Sstevel@tonic-gate 	/*
53500Sstevel@tonic-gate 	 * Some callers, notably SCSI, need to mark the devfs cache
53510Sstevel@tonic-gate 	 * to be rebuilt together with the config operation.
53520Sstevel@tonic-gate 	 */
53530Sstevel@tonic-gate 	if (flags & NDI_DEVFS_CLEAN)
53540Sstevel@tonic-gate 		(void) devfs_clean(dip, NULL, 0);
53550Sstevel@tonic-gate 
53560Sstevel@tonic-gate 	if (flags & NDI_CONFIG)
53570Sstevel@tonic-gate 		(void) config_grand_children(dip, flags, major);
53580Sstevel@tonic-gate 
53590Sstevel@tonic-gate 	pm_post_config(dip, NULL);
53600Sstevel@tonic-gate 
53610Sstevel@tonic-gate 	return (NDI_SUCCESS);
53620Sstevel@tonic-gate }
53630Sstevel@tonic-gate 
53640Sstevel@tonic-gate /*
53650Sstevel@tonic-gate  * Framework entry point for BUS_CONFIG_ALL
53660Sstevel@tonic-gate  */
53670Sstevel@tonic-gate int
ndi_devi_config(dev_info_t * dip,int flags)53680Sstevel@tonic-gate ndi_devi_config(dev_info_t *dip, int flags)
53690Sstevel@tonic-gate {
53700Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
53710Sstevel@tonic-gate 	    "ndi_devi_config: par = %s%d (%p), flags = 0x%x\n",
53720Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
53730Sstevel@tonic-gate 
53747009Scth 	return (devi_config_common(dip, flags, DDI_MAJOR_T_NONE));
53750Sstevel@tonic-gate }
53760Sstevel@tonic-gate 
53770Sstevel@tonic-gate /*
53780Sstevel@tonic-gate  * Framework entry point for BUS_CONFIG_DRIVER, bound to major
53790Sstevel@tonic-gate  */
53800Sstevel@tonic-gate int
ndi_devi_config_driver(dev_info_t * dip,int flags,major_t major)53810Sstevel@tonic-gate ndi_devi_config_driver(dev_info_t *dip, int flags, major_t major)
53820Sstevel@tonic-gate {
53830Sstevel@tonic-gate 	/* don't abuse this function */
53847009Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
53850Sstevel@tonic-gate 
53860Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
53870Sstevel@tonic-gate 	    "ndi_devi_config_driver: par = %s%d (%p), flags = 0x%x\n",
53880Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
53890Sstevel@tonic-gate 
53900Sstevel@tonic-gate 	return (devi_config_common(dip, flags, major));
53910Sstevel@tonic-gate }
53920Sstevel@tonic-gate 
53930Sstevel@tonic-gate /*
53942155Scth  * Called by nexus drivers to configure its children.
53950Sstevel@tonic-gate  */
53960Sstevel@tonic-gate static int
devi_config_one(dev_info_t * pdip,char * devnm,dev_info_t ** cdipp,uint_t flags,clock_t timeout)53972155Scth devi_config_one(dev_info_t *pdip, char *devnm, dev_info_t **cdipp,
53980Sstevel@tonic-gate     uint_t flags, clock_t timeout)
53990Sstevel@tonic-gate {
54002155Scth 	dev_info_t	*vdip = NULL;
54012155Scth 	char		*drivername = NULL;
54024145Scth 	int		find_by_addr = 0;
54032155Scth 	char		*name, *addr;
54042155Scth 	int		v_circ, p_circ;
54052155Scth 	clock_t		end_time;	/* 60 sec */
54062155Scth 	int		probed;
54072155Scth 	dev_info_t	*cdip;
54082155Scth 	mdi_pathinfo_t	*cpip;
54092155Scth 
54102155Scth 	*cdipp = NULL;
54110Sstevel@tonic-gate 
54120Sstevel@tonic-gate 	if (!NEXUS_DRV(ddi_get_driver(pdip)))
54130Sstevel@tonic-gate 		return (NDI_FAILURE);
54140Sstevel@tonic-gate 
54150Sstevel@tonic-gate 	/* split name into "name@addr" parts */
54160Sstevel@tonic-gate 	i_ddi_parse_name(devnm, &name, &addr, NULL);
54170Sstevel@tonic-gate 
54182155Scth 	/*
54192155Scth 	 * If the nexus is a pHCI and we are not processing a pHCI from
54202155Scth 	 * mdi bus_config code then we need to know the vHCI.
54212155Scth 	 */
54222155Scth 	if (MDI_PHCI(pdip))
54232155Scth 		vdip = mdi_devi_get_vdip(pdip);
54242155Scth 
54252155Scth 	/*
54262155Scth 	 * We may have a genericname on a system that creates drivername
54272155Scth 	 * nodes (from .conf files).  Find the drivername by nodeid. If we
54282155Scth 	 * can't find a node with devnm as the node name then we search by
542910923SEvan.Yan@Sun.COM 	 * drivername.	This allows an implementation to supply a genericly
54305742Scth 	 * named boot path (disk) and locate drivename nodes (sd).  The
54315742Scth 	 * NDI_PROMNAME flag does not apply to /devices/pseudo paths.
54322155Scth 	 */
54335742Scth 	if ((flags & NDI_PROMNAME) && (pdip != pseudo_dip)) {
54342009Sdm120769 		drivername = child_path_to_driver(pdip, name, addr);
54354145Scth 		find_by_addr = 1;
54364145Scth 	}
54372155Scth 
54382155Scth 	/*
54392155Scth 	 * Determine end_time: This routine should *not* be called with a
54402155Scth 	 * constant non-zero timeout argument, the caller should be adjusting
54412155Scth 	 * the timeout argument relative to when it *started* its asynchronous
54422155Scth 	 * enumeration.
54432155Scth 	 */
54442155Scth 	if (timeout > 0)
54452009Sdm120769 		end_time = ddi_get_lbolt() + timeout;
54462155Scth 
54472009Sdm120769 	for (;;) {
54481961Scth 		/*
54492155Scth 		 * For pHCI, enter (vHCI, pHCI) and search for pathinfo/client
54502155Scth 		 * child - break out of for(;;) loop if child found.
54512155Scth 		 * NOTE: Lock order for ndi_devi_enter is (vHCI, pHCI).
54522155Scth 		 */
54532155Scth 		if (vdip) {
54542155Scth 			/* use mdi_devi_enter ordering */
54552155Scth 			ndi_devi_enter(vdip, &v_circ);
54562155Scth 			ndi_devi_enter(pdip, &p_circ);
54572155Scth 			cpip = mdi_pi_find(pdip, NULL, addr);
54582155Scth 			cdip = mdi_pi_get_client(cpip);
54592155Scth 			if (cdip)
54602155Scth 				break;
54612155Scth 		} else
54622155Scth 			ndi_devi_enter(pdip, &p_circ);
54632155Scth 
54642155Scth 		/*
54652155Scth 		 * When not a  vHCI or not all pHCI devices are required to
54662155Scth 		 * enumerated under the vHCI (NDI_MDI_FALLBACK) search for
54672155Scth 		 * devinfo child.
54680Sstevel@tonic-gate 		 */
54692155Scth 		if ((vdip == NULL) || (flags & NDI_MDI_FALLBACK)) {
54702155Scth 			/* determine if .conf nodes already built */
54712155Scth 			probed = (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
54722155Scth 
54732155Scth 			/*
54742155Scth 			 * Search for child by name, if not found then search
54752155Scth 			 * for a node bound to the drivername driver with the
54762155Scth 			 * specified "@addr". Break out of for(;;) loop if
54774145Scth 			 * child found.  To support path-oriented aliases
54784145Scth 			 * binding on boot-device, we do a search_by_addr too.
54792155Scth 			 */
54802155Scth again:			(void) i_ndi_make_spec_children(pdip, flags);
54812155Scth 			cdip = find_child_by_name(pdip, name, addr);
54822155Scth 			if ((cdip == NULL) && drivername)
54832155Scth 				cdip = find_child_by_driver(pdip,
54842155Scth 				    drivername, addr);
54854145Scth 			if ((cdip == NULL) && find_by_addr)
54864145Scth 				cdip = find_child_by_addr(pdip, addr);
54872155Scth 			if (cdip)
54882155Scth 				break;
54892155Scth 
54902155Scth 			/*
54912155Scth 			 * determine if we should reenumerate .conf nodes
54922155Scth 			 * and look for child again.
54932155Scth 			 */
54942155Scth 			if (probed &&
54952155Scth 			    i_ddi_io_initialized() &&
54962155Scth 			    (flags & NDI_CONFIG_REPROBE) &&
54972155Scth 			    ((timeout <= 0) || (ddi_get_lbolt() >= end_time))) {
54982155Scth 				probed = 0;
54992155Scth 				mutex_enter(&DEVI(pdip)->devi_lock);
55002155Scth 				DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
55012155Scth 				mutex_exit(&DEVI(pdip)->devi_lock);
55022155Scth 				goto again;
55032155Scth 			}
55042155Scth 		}
55052155Scth 
55062155Scth 		/* break out of for(;;) if time expired */
55072155Scth 		if ((timeout <= 0) || (ddi_get_lbolt() >= end_time))
55080Sstevel@tonic-gate 			break;
55090Sstevel@tonic-gate 
55100Sstevel@tonic-gate 		/*
55112155Scth 		 * Child not found, exit and wait for asynchronous enumeration
55122155Scth 		 * to add child (or timeout). The addition of a new child (vhci
55132155Scth 		 * or phci) requires the asynchronous enumeration thread to
55142155Scth 		 * ndi_devi_enter/ndi_devi_exit. This exit will signal devi_cv
55152155Scth 		 * and cause us to return from ndi_devi_exit_and_wait, after
55162155Scth 		 * which we loop and search for the requested child again.
55170Sstevel@tonic-gate 		 */
55180Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT,
55190Sstevel@tonic-gate 		    "%s%d: waiting for child %s@%s, timeout %ld",
55200Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
55210Sstevel@tonic-gate 		    name, addr, timeout));
55222155Scth 		if (vdip) {
55232155Scth 			/*
55242155Scth 			 * Mark vHCI for pHCI ndi_devi_exit broadcast.
55252155Scth 			 */
55262155Scth 			mutex_enter(&DEVI(vdip)->devi_lock);
55272155Scth 			DEVI(vdip)->devi_flags |=
55282155Scth 			    DEVI_PHCI_SIGNALS_VHCI;
55292155Scth 			mutex_exit(&DEVI(vdip)->devi_lock);
55302155Scth 			ndi_devi_exit(pdip, p_circ);
55312155Scth 
55322155Scth 			/*
55332155Scth 			 * NB: There is a small race window from above
55342155Scth 			 * ndi_devi_exit() of pdip to cv_wait() in
55352155Scth 			 * ndi_devi_exit_and_wait() which can result in
55362155Scth 			 * not immediately finding a new pHCI child
55372155Scth 			 * of a pHCI that uses NDI_MDI_FAILBACK.
55382155Scth 			 */
55392155Scth 			ndi_devi_exit_and_wait(vdip, v_circ, end_time);
55402155Scth 		} else {
55412155Scth 			ndi_devi_exit_and_wait(pdip, p_circ, end_time);
55422155Scth 		}
55432155Scth 	}
55442155Scth 
55452155Scth 	/* done with paddr, fixup i_ddi_parse_name '@'->'\0' change */
55462155Scth 	if (addr && *addr != '\0')
55470Sstevel@tonic-gate 		*(addr - 1) = '@';
55480Sstevel@tonic-gate 
55492155Scth 	/* attach and hold the child, returning pointer to child */
55502155Scth 	if (cdip && (devi_attach_node(cdip, flags) == NDI_SUCCESS)) {
55512155Scth 		ndi_hold_devi(cdip);
55522155Scth 		*cdipp = cdip;
55532155Scth 	}
55542155Scth 
55552155Scth 	ndi_devi_exit(pdip, p_circ);
55562155Scth 	if (vdip)
55572155Scth 		ndi_devi_exit(vdip, v_circ);
55582155Scth 	return (*cdipp ? NDI_SUCCESS : NDI_FAILURE);
55590Sstevel@tonic-gate }
55600Sstevel@tonic-gate 
55610Sstevel@tonic-gate /*
55620Sstevel@tonic-gate  * Enumerate and attach a child specified by name 'devnm'.
55630Sstevel@tonic-gate  * Called by devfs lookup and DR to perform a BUS_CONFIG_ONE.
55640Sstevel@tonic-gate  * Note: devfs does not make use of NDI_CONFIG to configure
55650Sstevel@tonic-gate  * an entire branch.
55660Sstevel@tonic-gate  */
55670Sstevel@tonic-gate int
ndi_devi_config_one(dev_info_t * pdip,char * devnm,dev_info_t ** dipp,int flags)556812116SVikram.Hegde@Sun.COM ndi_devi_config_one(dev_info_t *pdip, char *devnm, dev_info_t **dipp, int flags)
55690Sstevel@tonic-gate {
55700Sstevel@tonic-gate 	int error;
55710Sstevel@tonic-gate 	int (*f)();
557212116SVikram.Hegde@Sun.COM 	char *nmdup;
557312116SVikram.Hegde@Sun.COM 	int duplen;
55740Sstevel@tonic-gate 	int branch_event = 0;
55750Sstevel@tonic-gate 
557612116SVikram.Hegde@Sun.COM 	ASSERT(pdip);
557712116SVikram.Hegde@Sun.COM 	ASSERT(devnm);
55780Sstevel@tonic-gate 	ASSERT(dipp);
557912116SVikram.Hegde@Sun.COM 	ASSERT(i_ddi_devi_attached(pdip));
55800Sstevel@tonic-gate 
55810Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
55820Sstevel@tonic-gate 	    "ndi_devi_config_one: par = %s%d (%p), child = %s\n",
558312116SVikram.Hegde@Sun.COM 	    ddi_driver_name(pdip), ddi_get_instance(pdip),
558412116SVikram.Hegde@Sun.COM 	    (void *)pdip, devnm));
558512116SVikram.Hegde@Sun.COM 
558612116SVikram.Hegde@Sun.COM 	*dipp = NULL;
558712116SVikram.Hegde@Sun.COM 
558812116SVikram.Hegde@Sun.COM 	if (pm_pre_config(pdip, devnm) != DDI_SUCCESS) {
558912116SVikram.Hegde@Sun.COM 		cmn_err(CE_WARN, "preconfig failed: %s", devnm);
55900Sstevel@tonic-gate 		return (NDI_FAILURE);
559112116SVikram.Hegde@Sun.COM 	}
55920Sstevel@tonic-gate 
55930Sstevel@tonic-gate 	if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
55940Sstevel@tonic-gate 	    (flags & NDI_CONFIG)) {
55950Sstevel@tonic-gate 		flags |= NDI_BRANCH_EVENT_OP;
55960Sstevel@tonic-gate 		branch_event = 1;
55970Sstevel@tonic-gate 	}
55980Sstevel@tonic-gate 
559912116SVikram.Hegde@Sun.COM 	nmdup = strdup(devnm);
560012116SVikram.Hegde@Sun.COM 	duplen = strlen(devnm) + 1;
560112116SVikram.Hegde@Sun.COM 
560212116SVikram.Hegde@Sun.COM 	if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
560312116SVikram.Hegde@Sun.COM 	    (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
560412116SVikram.Hegde@Sun.COM 	    (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
560512116SVikram.Hegde@Sun.COM 		error = devi_config_one(pdip, devnm, dipp, flags, 0);
56060Sstevel@tonic-gate 	} else {
56070Sstevel@tonic-gate 		/* call bus_config entry point */
560812116SVikram.Hegde@Sun.COM 		error = (*f)(pdip, flags, BUS_CONFIG_ONE, (void *)devnm, dipp);
560912116SVikram.Hegde@Sun.COM 	}
561012116SVikram.Hegde@Sun.COM 
561112116SVikram.Hegde@Sun.COM 	if (error) {
561212116SVikram.Hegde@Sun.COM 		*dipp = NULL;
561312116SVikram.Hegde@Sun.COM 	}
561412116SVikram.Hegde@Sun.COM 
561512116SVikram.Hegde@Sun.COM 	/*
561612116SVikram.Hegde@Sun.COM 	 * if we fail to lookup and this could be an alias, lookup currdip
561712116SVikram.Hegde@Sun.COM 	 * To prevent recursive lookups into the same hash table, only
561812116SVikram.Hegde@Sun.COM 	 * do the currdip lookups once the hash table init is complete.
561912116SVikram.Hegde@Sun.COM 	 * Use tsd so that redirection doesn't recurse
562012116SVikram.Hegde@Sun.COM 	 */
562112116SVikram.Hegde@Sun.COM 	if (error) {
562212116SVikram.Hegde@Sun.COM 		char *alias = kmem_alloc(MAXPATHLEN, KM_NOSLEEP);
562312116SVikram.Hegde@Sun.COM 		if (alias == NULL) {
562412116SVikram.Hegde@Sun.COM 			ddi_err(DER_PANIC, pdip, "alias alloc failed: %s",
562512116SVikram.Hegde@Sun.COM 			    nmdup);
562612116SVikram.Hegde@Sun.COM 		}
562712116SVikram.Hegde@Sun.COM 		(void) ddi_pathname(pdip, alias);
562812116SVikram.Hegde@Sun.COM 		(void) strlcat(alias, "/", MAXPATHLEN);
562912116SVikram.Hegde@Sun.COM 		(void) strlcat(alias, nmdup, MAXPATHLEN);
563012116SVikram.Hegde@Sun.COM 
563112116SVikram.Hegde@Sun.COM 		*dipp = ddi_alias_redirect(alias);
563212116SVikram.Hegde@Sun.COM 		error = (*dipp ? NDI_SUCCESS : NDI_FAILURE);
563312116SVikram.Hegde@Sun.COM 
563412116SVikram.Hegde@Sun.COM 		kmem_free(alias, MAXPATHLEN);
563512116SVikram.Hegde@Sun.COM 	}
563612116SVikram.Hegde@Sun.COM 	kmem_free(nmdup, duplen);
563712116SVikram.Hegde@Sun.COM 
563812116SVikram.Hegde@Sun.COM 	if (error || !(flags & NDI_CONFIG)) {
563912116SVikram.Hegde@Sun.COM 		pm_post_config(pdip, devnm);
56400Sstevel@tonic-gate 		return (error);
56410Sstevel@tonic-gate 	}
56420Sstevel@tonic-gate 
56430Sstevel@tonic-gate 	/*
56444145Scth 	 * DR usage (i.e. call with NDI_CONFIG) recursively configures
56450Sstevel@tonic-gate 	 * grandchildren, performing a BUS_CONFIG_ALL from the node attached
56460Sstevel@tonic-gate 	 * by the BUS_CONFIG_ONE.
56470Sstevel@tonic-gate 	 */
56480Sstevel@tonic-gate 	ASSERT(*dipp);
56497009Scth 	error = devi_config_common(*dipp, flags, DDI_MAJOR_T_NONE);
56500Sstevel@tonic-gate 
565112116SVikram.Hegde@Sun.COM 	pm_post_config(pdip, devnm);
56520Sstevel@tonic-gate 
56530Sstevel@tonic-gate 	if (branch_event)
56540Sstevel@tonic-gate 		(void) i_log_devfs_branch_add(*dipp);
56550Sstevel@tonic-gate 
56560Sstevel@tonic-gate 	return (error);
56570Sstevel@tonic-gate }
56580Sstevel@tonic-gate 
56590Sstevel@tonic-gate /*
56600Sstevel@tonic-gate  * Enumerate and attach a child specified by name 'devnm'.
56610Sstevel@tonic-gate  * Called during configure the OBP options. This configures
56620Sstevel@tonic-gate  * only one node.
56630Sstevel@tonic-gate  */
56640Sstevel@tonic-gate static int
ndi_devi_config_obp_args(dev_info_t * parent,char * devnm,dev_info_t ** childp,int flags)56650Sstevel@tonic-gate ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
56660Sstevel@tonic-gate     dev_info_t **childp, int flags)
56670Sstevel@tonic-gate {
56680Sstevel@tonic-gate 	int error;
56690Sstevel@tonic-gate 	int (*f)();
56700Sstevel@tonic-gate 
56710Sstevel@tonic-gate 	ASSERT(childp);
56721333Scth 	ASSERT(i_ddi_devi_attached(parent));
56730Sstevel@tonic-gate 
56740Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_config_obp_args: "
56750Sstevel@tonic-gate 	    "par = %s%d (%p), child = %s\n", ddi_driver_name(parent),
56760Sstevel@tonic-gate 	    ddi_get_instance(parent), (void *)parent, devnm));
56770Sstevel@tonic-gate 
56780Sstevel@tonic-gate 	if ((DEVI(parent)->devi_ops->devo_bus_ops == NULL) ||
56790Sstevel@tonic-gate 	    (DEVI(parent)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
56800Sstevel@tonic-gate 	    (f = DEVI(parent)->devi_ops->devo_bus_ops->bus_config) == NULL) {
56810Sstevel@tonic-gate 		error = NDI_FAILURE;
56820Sstevel@tonic-gate 	} else {
56830Sstevel@tonic-gate 		/* call bus_config entry point */
56840Sstevel@tonic-gate 		error = (*f)(parent, flags,
56850Sstevel@tonic-gate 		    BUS_CONFIG_OBP_ARGS, (void *)devnm, childp);
56860Sstevel@tonic-gate 	}
56870Sstevel@tonic-gate 	return (error);
56880Sstevel@tonic-gate }
56890Sstevel@tonic-gate 
56904845Svikram /*
56914845Svikram  * Pay attention, the following is a bit tricky:
56924845Svikram  * There are three possible cases when constraints are applied
56934845Svikram  *
56944845Svikram  *	- A constraint is applied and the offline is disallowed.
56954845Svikram  *	  Simply return failure and block the offline
56964845Svikram  *
56974845Svikram  *	- A constraint is applied and the offline is allowed.
56984845Svikram  *	  Mark the dip as having passed the constraint and allow
56994845Svikram  *	  offline to proceed.
57004845Svikram  *
57014845Svikram  *	- A constraint is not applied. Allow the offline to proceed for now.
57024845Svikram  *
57034845Svikram  * In the latter two cases we allow the offline to proceed. If the
57044845Svikram  * offline succeeds (no users) everything is fine. It is ok for an unused
57054845Svikram  * device to be offlined even if no constraints were imposed on the offline.
57064845Svikram  * If the offline fails because there are users, we look at the constraint
57074845Svikram  * flag on the dip. If the constraint flag is set (implying that it passed
57084845Svikram  * a constraint) we allow the dip to be retired. If not, we don't allow
57094845Svikram  * the retire. This ensures that we don't allow unconstrained retire.
57104845Svikram  */
57114845Svikram int
e_ddi_offline_notify(dev_info_t * dip)57124845Svikram e_ddi_offline_notify(dev_info_t *dip)
57134845Svikram {
57144845Svikram 	int retval;
57154845Svikram 	int constraint;
57164845Svikram 	int failure;
57174845Svikram 
57184845Svikram 	RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): entered: dip=%p",
57194845Svikram 	    (void *) dip));
57204845Svikram 
57214845Svikram 	constraint = 0;
57224845Svikram 	failure = 0;
57234845Svikram 
57244845Svikram 	/*
57254845Svikram 	 * Start with userland constraints first - applied via device contracts
57264845Svikram 	 */
57274845Svikram 	retval = contract_device_offline(dip, DDI_DEV_T_ANY, 0);
57284845Svikram 	switch (retval) {
57294845Svikram 	case CT_NACK:
57304845Svikram 		RIO_DEBUG((CE_NOTE, "Received NACK for dip=%p", (void *)dip));
57314845Svikram 		failure = 1;
57324845Svikram 		goto out;
57334845Svikram 	case CT_ACK:
57344845Svikram 		constraint = 1;
57354845Svikram 		RIO_DEBUG((CE_NOTE, "Received ACK for dip=%p", (void *)dip));
57364845Svikram 		break;
57374845Svikram 	case CT_NONE:
57384845Svikram 		/* no contracts */
57394845Svikram 		RIO_DEBUG((CE_NOTE, "No contracts on dip=%p", (void *)dip));
57404845Svikram 		break;
57414845Svikram 	default:
57424845Svikram 		ASSERT(retval == CT_NONE);
57434845Svikram 	}
57444845Svikram 
57454845Svikram 	/*
57464845Svikram 	 * Next, use LDI to impose kernel constraints
57474845Svikram 	 */
57484845Svikram 	retval = ldi_invoke_notify(dip, DDI_DEV_T_ANY, 0, LDI_EV_OFFLINE, NULL);
57494845Svikram 	switch (retval) {
57504845Svikram 	case LDI_EV_FAILURE:
57514845Svikram 		contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_FAILURE);
57524845Svikram 		RIO_DEBUG((CE_NOTE, "LDI callback failed on dip=%p",
57534845Svikram 		    (void *)dip));
57544845Svikram 		failure = 1;
57554845Svikram 		goto out;
57564845Svikram 	case LDI_EV_SUCCESS:
57574845Svikram 		constraint = 1;
57584845Svikram 		RIO_DEBUG((CE_NOTE, "LDI callback success on dip=%p",
57594845Svikram 		    (void *)dip));
57604845Svikram 		break;
57614845Svikram 	case LDI_EV_NONE:
57624845Svikram 		/* no matching LDI callbacks */
57634845Svikram 		RIO_DEBUG((CE_NOTE, "No LDI callbacks for dip=%p",
57644845Svikram 		    (void *)dip));
57654845Svikram 		break;
57664845Svikram 	default:
57674845Svikram 		ASSERT(retval == LDI_EV_NONE);
57684845Svikram 	}
57694845Svikram 
57704845Svikram out:
57714845Svikram 	mutex_enter(&(DEVI(dip)->devi_lock));
57724845Svikram 	if ((DEVI(dip)->devi_flags & DEVI_RETIRING) && failure) {
57734845Svikram 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): setting "
57744845Svikram 		    "BLOCKED flag. dip=%p", (void *)dip));
57754845Svikram 		DEVI(dip)->devi_flags |= DEVI_R_BLOCKED;
57764845Svikram 		if (DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT) {
57774845Svikram 			RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): "
57784845Svikram 			    "blocked. clearing RCM CONSTRAINT flag. dip=%p",
57794845Svikram 			    (void *)dip));
57804845Svikram 			DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
57814845Svikram 		}
57824845Svikram 	} else if ((DEVI(dip)->devi_flags & DEVI_RETIRING) && constraint) {
57834845Svikram 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): setting "
57844845Svikram 		    "CONSTRAINT flag. dip=%p", (void *)dip));
57854845Svikram 		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
57864845Svikram 	} else if ((DEVI(dip)->devi_flags & DEVI_RETIRING) &&
578712107SStephen.Hanson@Sun.COM 	    ((DEVI(dip)->devi_ops != NULL &&
578812107SStephen.Hanson@Sun.COM 	    DEVI(dip)->devi_ops->devo_bus_ops != NULL) ||
578912107SStephen.Hanson@Sun.COM 	    DEVI(dip)->devi_ref == 0)) {
579012107SStephen.Hanson@Sun.COM 		/* also allow retire if nexus or if device is not in use */
57914845Svikram 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): device not in "
57924845Svikram 		    "use. Setting CONSTRAINT flag. dip=%p", (void *)dip));
57934845Svikram 		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
57944845Svikram 	} else {
57954845Svikram 		/*
57964845Svikram 		 * Note: We cannot ASSERT here that DEVI_R_CONSTRAINT is
57974845Svikram 		 * not set, since other sources (such as RCM) may have
57984845Svikram 		 * set the flag.
57994845Svikram 		 */
58004845Svikram 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): not setting "
58014845Svikram 		    "constraint flag. dip=%p", (void *)dip));
58024845Svikram 	}
58034845Svikram 	mutex_exit(&(DEVI(dip)->devi_lock));
58044845Svikram 
58054845Svikram 
58064845Svikram 	RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): exit: dip=%p",
58074845Svikram 	    (void *) dip));
58084845Svikram 
58094845Svikram 	return (failure ? DDI_FAILURE : DDI_SUCCESS);
58104845Svikram }
58114845Svikram 
58124845Svikram void
e_ddi_offline_finalize(dev_info_t * dip,int result)58134845Svikram e_ddi_offline_finalize(dev_info_t *dip, int result)
58144845Svikram {
58154845Svikram 	RIO_DEBUG((CE_NOTE, "e_ddi_offline_finalize(): entry: result=%s, "
58164845Svikram 	    "dip=%p", result == DDI_SUCCESS ? "SUCCESS" : "FAILURE",
58174845Svikram 	    (void *)dip));
58184845Svikram 
58194845Svikram 	contract_device_negend(dip, DDI_DEV_T_ANY, 0,  result == DDI_SUCCESS ?
58204845Svikram 	    CT_EV_SUCCESS : CT_EV_FAILURE);
58214845Svikram 
58224845Svikram 	ldi_invoke_finalize(dip, DDI_DEV_T_ANY, 0,
58234845Svikram 	    LDI_EV_OFFLINE, result == DDI_SUCCESS ?
58244845Svikram 	    LDI_EV_SUCCESS : LDI_EV_FAILURE, NULL);
58254845Svikram 
58264845Svikram 	RIO_VERBOSE((CE_NOTE, "e_ddi_offline_finalize(): exit: dip=%p",
58274845Svikram 	    (void *)dip));
58284845Svikram }
58294845Svikram 
58304845Svikram void
e_ddi_degrade_finalize(dev_info_t * dip)58314845Svikram e_ddi_degrade_finalize(dev_info_t *dip)
58324845Svikram {
58334845Svikram 	RIO_DEBUG((CE_NOTE, "e_ddi_degrade_finalize(): entry: "
58344845Svikram 	    "result always = DDI_SUCCESS, dip=%p", (void *)dip));
58354845Svikram 
58364845Svikram 	contract_device_degrade(dip, DDI_DEV_T_ANY, 0);
58374845Svikram 	contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_SUCCESS);
58384845Svikram 
58394845Svikram 	ldi_invoke_finalize(dip, DDI_DEV_T_ANY, 0, LDI_EV_DEGRADE,
58404845Svikram 	    LDI_EV_SUCCESS, NULL);
58414845Svikram 
58424845Svikram 	RIO_VERBOSE((CE_NOTE, "e_ddi_degrade_finalize(): exit: dip=%p",
58434845Svikram 	    (void *)dip));
58444845Svikram }
58454845Svikram 
58464845Svikram void
e_ddi_undegrade_finalize(dev_info_t * dip)58474845Svikram e_ddi_undegrade_finalize(dev_info_t *dip)
58484845Svikram {
58494845Svikram 	RIO_DEBUG((CE_NOTE, "e_ddi_undegrade_finalize(): entry: "
58504845Svikram 	    "result always = DDI_SUCCESS, dip=%p", (void *)dip));
58514845Svikram 
58524845Svikram 	contract_device_undegrade(dip, DDI_DEV_T_ANY, 0);
58534845Svikram 	contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_SUCCESS);
58544845Svikram 
58554845Svikram 	RIO_VERBOSE((CE_NOTE, "e_ddi_undegrade_finalize(): exit: dip=%p",
58564845Svikram 	    (void *)dip));
58574845Svikram }
58580Sstevel@tonic-gate 
58590Sstevel@tonic-gate /*
58600Sstevel@tonic-gate  * detach a node with parent already held busy
58610Sstevel@tonic-gate  */
58620Sstevel@tonic-gate static int
devi_detach_node(dev_info_t * dip,uint_t flags)58630Sstevel@tonic-gate devi_detach_node(dev_info_t *dip, uint_t flags)
58640Sstevel@tonic-gate {
58650Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
58660Sstevel@tonic-gate 	int ret = NDI_SUCCESS;
58670Sstevel@tonic-gate 	ddi_eventcookie_t cookie;
58688640SVikram.Hegde@Sun.COM 	char *path = NULL;
58698640SVikram.Hegde@Sun.COM 	char *class = NULL;
58708640SVikram.Hegde@Sun.COM 	char *driver = NULL;
58718640SVikram.Hegde@Sun.COM 	int instance = -1;
58728640SVikram.Hegde@Sun.COM 	int post_event = 0;
58730Sstevel@tonic-gate 
58742155Scth 	ASSERT(pdip && DEVI_BUSY_OWNED(pdip));
58752155Scth 
58764845Svikram 	/*
58774845Svikram 	 * Invoke notify if offlining
58784845Svikram 	 */
58794845Svikram 	if (flags & NDI_DEVI_OFFLINE) {
58804845Svikram 		RIO_DEBUG((CE_NOTE, "devi_detach_node: offlining dip=%p",
58814845Svikram 		    (void *)dip));
58824845Svikram 		if (e_ddi_offline_notify(dip) != DDI_SUCCESS) {
58834845Svikram 			RIO_DEBUG((CE_NOTE, "devi_detach_node: offline NACKed"
58844845Svikram 			    "dip=%p", (void *)dip));
58854845Svikram 			return (NDI_FAILURE);
58864845Svikram 		}
58874845Svikram 	}
58884845Svikram 
58890Sstevel@tonic-gate 	if (flags & NDI_POST_EVENT) {
58902155Scth 		if (i_ddi_devi_attached(pdip)) {
58910Sstevel@tonic-gate 			if (ddi_get_eventcookie(dip, DDI_DEVI_REMOVE_EVENT,
58920Sstevel@tonic-gate 			    &cookie) == NDI_SUCCESS)
58930Sstevel@tonic-gate 				(void) ndi_post_event(dip, dip, cookie, NULL);
58940Sstevel@tonic-gate 		}
58950Sstevel@tonic-gate 	}
58960Sstevel@tonic-gate 
58974845Svikram 	if (i_ddi_detachchild(dip, flags) != DDI_SUCCESS) {
58984845Svikram 		if (flags & NDI_DEVI_OFFLINE) {
58994845Svikram 			RIO_DEBUG((CE_NOTE, "devi_detach_node: offline failed."
59004845Svikram 			    " Calling e_ddi_offline_finalize with result=%d. "
59014845Svikram 			    "dip=%p", DDI_FAILURE, (void *)dip));
59024845Svikram 			e_ddi_offline_finalize(dip, DDI_FAILURE);
59034845Svikram 		}
59040Sstevel@tonic-gate 		return (NDI_FAILURE);
59054845Svikram 	}
59064845Svikram 
59074845Svikram 	if (flags & NDI_DEVI_OFFLINE) {
59084845Svikram 		RIO_DEBUG((CE_NOTE, "devi_detach_node: offline succeeded."
59094845Svikram 		    " Calling e_ddi_offline_finalize with result=%d, "
59104845Svikram 		    "dip=%p", DDI_SUCCESS, (void *)dip));
59114845Svikram 		e_ddi_offline_finalize(dip, DDI_SUCCESS);
59124845Svikram 	}
59130Sstevel@tonic-gate 
59140Sstevel@tonic-gate 	if (flags & NDI_AUTODETACH)
59150Sstevel@tonic-gate 		return (NDI_SUCCESS);
59160Sstevel@tonic-gate 
59170Sstevel@tonic-gate 	/*
59180Sstevel@tonic-gate 	 * For DR, even bound nodes may need to have offline
59190Sstevel@tonic-gate 	 * flag set.
59200Sstevel@tonic-gate 	 */
59210Sstevel@tonic-gate 	if (flags & NDI_DEVI_OFFLINE) {
5922495Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
59230Sstevel@tonic-gate 		DEVI_SET_DEVICE_OFFLINE(dip);
5924495Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
59250Sstevel@tonic-gate 	}
59260Sstevel@tonic-gate 
59270Sstevel@tonic-gate 	if (i_ddi_node_state(dip) == DS_INITIALIZED) {
592812707SMichael.Bergknoff@Oracle.COM 		struct dev_info *devi = DEVI(dip);
592912707SMichael.Bergknoff@Oracle.COM 
593012707SMichael.Bergknoff@Oracle.COM 		if (devi->devi_ev_path == NULL) {
593112707SMichael.Bergknoff@Oracle.COM 			devi->devi_ev_path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
593212707SMichael.Bergknoff@Oracle.COM 			(void) ddi_pathname(dip, devi->devi_ev_path);
593312707SMichael.Bergknoff@Oracle.COM 		}
59340Sstevel@tonic-gate 		if (flags & NDI_DEVI_OFFLINE)
593512707SMichael.Bergknoff@Oracle.COM 			i_ndi_devi_report_status_change(dip,
593612707SMichael.Bergknoff@Oracle.COM 			    devi->devi_ev_path);
59370Sstevel@tonic-gate 
59380Sstevel@tonic-gate 		if (need_remove_event(dip, flags)) {
593912707SMichael.Bergknoff@Oracle.COM 			/*
594012707SMichael.Bergknoff@Oracle.COM 			 * instance and path data are lost in call to
594112707SMichael.Bergknoff@Oracle.COM 			 * ddi_uninitchild
594212707SMichael.Bergknoff@Oracle.COM 			 */
594312707SMichael.Bergknoff@Oracle.COM 			devi->devi_ev_instance = ddi_get_instance(dip);
59448640SVikram.Hegde@Sun.COM 
5945495Scth 			mutex_enter(&(DEVI(dip)->devi_lock));
59460Sstevel@tonic-gate 			DEVI_SET_EVREMOVE(dip);
5947495Scth 			mutex_exit(&(DEVI(dip)->devi_lock));
59480Sstevel@tonic-gate 		}
59490Sstevel@tonic-gate 	}
59500Sstevel@tonic-gate 
59510Sstevel@tonic-gate 	if (flags & (NDI_UNCONFIG | NDI_DEVI_REMOVE)) {
59520Sstevel@tonic-gate 		ret = ddi_uninitchild(dip);
59530Sstevel@tonic-gate 		if (ret == NDI_SUCCESS) {
59540Sstevel@tonic-gate 			/*
59550Sstevel@tonic-gate 			 * Remove uninitialized pseudo nodes because
59560Sstevel@tonic-gate 			 * system props are lost and the node cannot be
59570Sstevel@tonic-gate 			 * reattached.
59580Sstevel@tonic-gate 			 */
59590Sstevel@tonic-gate 			if (!ndi_dev_is_persistent_node(dip))
59600Sstevel@tonic-gate 				flags |= NDI_DEVI_REMOVE;
59610Sstevel@tonic-gate 
59628640SVikram.Hegde@Sun.COM 			if (flags & NDI_DEVI_REMOVE) {
596312288SChris.Horne@Sun.COM 				/*
596412288SChris.Horne@Sun.COM 				 * NOTE: If there is a consumer of LDI events,
596512288SChris.Horne@Sun.COM 				 * ddi_uninitchild above would have failed
596612288SChris.Horne@Sun.COM 				 * because of active devi_ref from ldi_open().
596712288SChris.Horne@Sun.COM 				 */
596812288SChris.Horne@Sun.COM 
596912707SMichael.Bergknoff@Oracle.COM 				if (DEVI_EVREMOVE(dip)) {
597012707SMichael.Bergknoff@Oracle.COM 					path = i_ddi_strdup(
597112707SMichael.Bergknoff@Oracle.COM 					    DEVI(dip)->devi_ev_path,
597212707SMichael.Bergknoff@Oracle.COM 					    KM_SLEEP);
597312707SMichael.Bergknoff@Oracle.COM 					class =
597412707SMichael.Bergknoff@Oracle.COM 					    i_ddi_strdup(i_ddi_devi_class(dip),
597512707SMichael.Bergknoff@Oracle.COM 					    KM_SLEEP);
597612707SMichael.Bergknoff@Oracle.COM 					driver =
597712707SMichael.Bergknoff@Oracle.COM 					    i_ddi_strdup(
597812707SMichael.Bergknoff@Oracle.COM 					    (char *)ddi_driver_name(dip),
597912707SMichael.Bergknoff@Oracle.COM 					    KM_SLEEP);
598012707SMichael.Bergknoff@Oracle.COM 					instance = DEVI(dip)->devi_ev_instance;
598112707SMichael.Bergknoff@Oracle.COM 					post_event = 1;
598212707SMichael.Bergknoff@Oracle.COM 				}
598312707SMichael.Bergknoff@Oracle.COM 
59840Sstevel@tonic-gate 				ret = ddi_remove_child(dip, 0);
59858640SVikram.Hegde@Sun.COM 				if (post_event && ret == NDI_SUCCESS) {
598612288SChris.Horne@Sun.COM 					/* Generate EC_DEVFS_DEVI_REMOVE */
59878640SVikram.Hegde@Sun.COM 					(void) i_log_devfs_remove_devinfo(path,
59888640SVikram.Hegde@Sun.COM 					    class, driver, instance, flags);
59898640SVikram.Hegde@Sun.COM 				}
59908640SVikram.Hegde@Sun.COM 			}
59918640SVikram.Hegde@Sun.COM 
59920Sstevel@tonic-gate 		}
59930Sstevel@tonic-gate 	}
59940Sstevel@tonic-gate 
59958640SVikram.Hegde@Sun.COM 	if (path)
599612707SMichael.Bergknoff@Oracle.COM 		strfree(path);
59978640SVikram.Hegde@Sun.COM 	if (class)
599812707SMichael.Bergknoff@Oracle.COM 		strfree(class);
59998640SVikram.Hegde@Sun.COM 	if (driver)
600012707SMichael.Bergknoff@Oracle.COM 		strfree(driver);
60018640SVikram.Hegde@Sun.COM 
60020Sstevel@tonic-gate 	return (ret);
60030Sstevel@tonic-gate }
60040Sstevel@tonic-gate 
60050Sstevel@tonic-gate /*
60060Sstevel@tonic-gate  * unconfigure immediate children of bus nexus device
60070Sstevel@tonic-gate  */
60080Sstevel@tonic-gate static int
unconfig_immediate_children(dev_info_t * dip,dev_info_t ** dipp,int flags,major_t major)60090Sstevel@tonic-gate unconfig_immediate_children(
60100Sstevel@tonic-gate 	dev_info_t *dip,
60110Sstevel@tonic-gate 	dev_info_t **dipp,
60120Sstevel@tonic-gate 	int flags,
60130Sstevel@tonic-gate 	major_t major)
60140Sstevel@tonic-gate {
60152155Scth 	int rv = NDI_SUCCESS;
60162155Scth 	int circ, vcirc;
60170Sstevel@tonic-gate 	dev_info_t *child;
60182155Scth 	dev_info_t *vdip = NULL;
60192155Scth 	dev_info_t *next;
60200Sstevel@tonic-gate 
60210Sstevel@tonic-gate 	ASSERT(dipp == NULL || *dipp == NULL);
60220Sstevel@tonic-gate 
60232155Scth 	/*
60242155Scth 	 * Scan forward to see if we will be processing a pHCI child. If we
60252155Scth 	 * have a child that is a pHCI and vHCI and pHCI are not siblings then
60262155Scth 	 * enter vHCI before parent(pHCI) to prevent deadlock with mpxio
60272155Scth 	 * Client power management operations.
60282155Scth 	 */
60290Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
60302155Scth 	for (child = ddi_get_child(dip); child;
60312155Scth 	    child = ddi_get_next_sibling(child)) {
60322155Scth 		/* skip same nodes we skip below */
60337009Scth 		if (((major != DDI_MAJOR_T_NONE) &&
60342155Scth 		    (major != ddi_driver_major(child))) ||
60352155Scth 		    ((flags & NDI_AUTODETACH) && !is_leaf_node(child)))
60362155Scth 			continue;
60372155Scth 
60382155Scth 		if (MDI_PHCI(child)) {
60392155Scth 			vdip = mdi_devi_get_vdip(child);
60402155Scth 			/*
60412155Scth 			 * If vHCI and vHCI is not a sibling of pHCI
60422155Scth 			 * then enter in (vHCI, parent(pHCI)) order.
60432155Scth 			 */
60442155Scth 			if (vdip && (ddi_get_parent(vdip) != dip)) {
60452155Scth 				ndi_devi_exit(dip, circ);
60462155Scth 
60472155Scth 				/* use mdi_devi_enter ordering */
60482155Scth 				ndi_devi_enter(vdip, &vcirc);
60492155Scth 				ndi_devi_enter(dip, &circ);
60502155Scth 				break;
60512155Scth 			} else
60522155Scth 				vdip = NULL;
60532155Scth 		}
60542155Scth 	}
60552155Scth 
60560Sstevel@tonic-gate 	child = ddi_get_child(dip);
60570Sstevel@tonic-gate 	while (child) {
60582155Scth 		next = ddi_get_next_sibling(child);
60592155Scth 
60607009Scth 		if ((major != DDI_MAJOR_T_NONE) &&
60610Sstevel@tonic-gate 		    (major != ddi_driver_major(child))) {
60620Sstevel@tonic-gate 			child = next;
60630Sstevel@tonic-gate 			continue;
60640Sstevel@tonic-gate 		}
60650Sstevel@tonic-gate 
60660Sstevel@tonic-gate 		/* skip nexus nodes during autodetach */
60670Sstevel@tonic-gate 		if ((flags & NDI_AUTODETACH) && !is_leaf_node(child)) {
60680Sstevel@tonic-gate 			child = next;
60690Sstevel@tonic-gate 			continue;
60700Sstevel@tonic-gate 		}
60710Sstevel@tonic-gate 
60720Sstevel@tonic-gate 		if (devi_detach_node(child, flags) != NDI_SUCCESS) {
60730Sstevel@tonic-gate 			if (dipp && *dipp == NULL) {
60740Sstevel@tonic-gate 				ndi_hold_devi(child);
60750Sstevel@tonic-gate 				*dipp = child;
60760Sstevel@tonic-gate 			}
60770Sstevel@tonic-gate 			rv = NDI_FAILURE;
60780Sstevel@tonic-gate 		}
60790Sstevel@tonic-gate 
60800Sstevel@tonic-gate 		/*
60810Sstevel@tonic-gate 		 * Continue upon failure--best effort algorithm
60820Sstevel@tonic-gate 		 */
60830Sstevel@tonic-gate 		child = next;
60840Sstevel@tonic-gate 	}
60852155Scth 
60860Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
60872155Scth 	if (vdip)
60882155Scth 		ndi_devi_exit(vdip, vcirc);
60892155Scth 
60900Sstevel@tonic-gate 	return (rv);
60910Sstevel@tonic-gate }
60920Sstevel@tonic-gate 
60930Sstevel@tonic-gate /*
60940Sstevel@tonic-gate  * unconfigure grand children of bus nexus device
60950Sstevel@tonic-gate  */
60960Sstevel@tonic-gate static int
unconfig_grand_children(dev_info_t * dip,dev_info_t ** dipp,int flags,major_t major,struct brevq_node ** brevqp)60970Sstevel@tonic-gate unconfig_grand_children(
60980Sstevel@tonic-gate 	dev_info_t *dip,
60990Sstevel@tonic-gate 	dev_info_t **dipp,
61000Sstevel@tonic-gate 	int flags,
61010Sstevel@tonic-gate 	major_t major,
61020Sstevel@tonic-gate 	struct brevq_node **brevqp)
61030Sstevel@tonic-gate {
61040Sstevel@tonic-gate 	struct mt_config_handle *hdl;
61050Sstevel@tonic-gate 
61060Sstevel@tonic-gate 	if (brevqp)
61070Sstevel@tonic-gate 		*brevqp = NULL;
61080Sstevel@tonic-gate 
61090Sstevel@tonic-gate 	/* multi-threaded configuration of child nexus */
61100Sstevel@tonic-gate 	hdl = mt_config_init(dip, dipp, flags, major, MT_UNCONFIG_OP, brevqp);
61110Sstevel@tonic-gate 	mt_config_children(hdl);
61120Sstevel@tonic-gate 
61130Sstevel@tonic-gate 	return (mt_config_fini(hdl));	/* wait for threads to exit */
61140Sstevel@tonic-gate }
61150Sstevel@tonic-gate 
61160Sstevel@tonic-gate /*
61170Sstevel@tonic-gate  * Unconfigure children/descendants of the dip.
61180Sstevel@tonic-gate  *
61190Sstevel@tonic-gate  * If brevqp is not NULL, on return *brevqp is set to a queue of dip's
61200Sstevel@tonic-gate  * child devinames for which branch remove events need to be generated.
61210Sstevel@tonic-gate  */
61220Sstevel@tonic-gate static int
devi_unconfig_common(dev_info_t * dip,dev_info_t ** dipp,int flags,major_t major,struct brevq_node ** brevqp)61230Sstevel@tonic-gate devi_unconfig_common(
61240Sstevel@tonic-gate 	dev_info_t *dip,
61250Sstevel@tonic-gate 	dev_info_t **dipp,
61260Sstevel@tonic-gate 	int flags,
61270Sstevel@tonic-gate 	major_t major,
61280Sstevel@tonic-gate 	struct brevq_node **brevqp)
61290Sstevel@tonic-gate {
61300Sstevel@tonic-gate 	int rv;
61310Sstevel@tonic-gate 	int pm_cookie;
61320Sstevel@tonic-gate 	int (*f)();
61330Sstevel@tonic-gate 	ddi_bus_config_op_t bus_op;
61340Sstevel@tonic-gate 
61350Sstevel@tonic-gate 	if (dipp)
61360Sstevel@tonic-gate 		*dipp = NULL;
61370Sstevel@tonic-gate 	if (brevqp)
61380Sstevel@tonic-gate 		*brevqp = NULL;
61390Sstevel@tonic-gate 
61400Sstevel@tonic-gate 	/*
61410Sstevel@tonic-gate 	 * Power up the dip if it is powered off.  If the flag bit
61420Sstevel@tonic-gate 	 * NDI_AUTODETACH is set and the dip is not at its full power,
61430Sstevel@tonic-gate 	 * skip the rest of the branch.
61440Sstevel@tonic-gate 	 */
61450Sstevel@tonic-gate 	if (pm_pre_unconfig(dip, flags, &pm_cookie, NULL) != DDI_SUCCESS)
61460Sstevel@tonic-gate 		return ((flags & NDI_AUTODETACH) ? NDI_SUCCESS :
61470Sstevel@tonic-gate 		    NDI_FAILURE);
61480Sstevel@tonic-gate 
61490Sstevel@tonic-gate 	/*
61500Sstevel@tonic-gate 	 * Some callers, notably SCSI, need to clear out the devfs
61510Sstevel@tonic-gate 	 * cache together with the unconfig to prevent stale entries.
61520Sstevel@tonic-gate 	 */
61530Sstevel@tonic-gate 	if (flags & NDI_DEVFS_CLEAN)
61540Sstevel@tonic-gate 		(void) devfs_clean(dip, NULL, 0);
61550Sstevel@tonic-gate 
61560Sstevel@tonic-gate 	rv = unconfig_grand_children(dip, dipp, flags, major, brevqp);
61570Sstevel@tonic-gate 
61580Sstevel@tonic-gate 	if ((rv != NDI_SUCCESS) && ((flags & NDI_AUTODETACH) == 0)) {
61590Sstevel@tonic-gate 		if (brevqp && *brevqp) {
61600Sstevel@tonic-gate 			log_and_free_br_events_on_grand_children(dip, *brevqp);
61610Sstevel@tonic-gate 			free_brevq(*brevqp);
61620Sstevel@tonic-gate 			*brevqp = NULL;
61630Sstevel@tonic-gate 		}
61640Sstevel@tonic-gate 		pm_post_unconfig(dip, pm_cookie, NULL);
61650Sstevel@tonic-gate 		return (rv);
61660Sstevel@tonic-gate 	}
61670Sstevel@tonic-gate 
61680Sstevel@tonic-gate 	if (dipp && *dipp) {
61690Sstevel@tonic-gate 		ndi_rele_devi(*dipp);
61700Sstevel@tonic-gate 		*dipp = NULL;
61710Sstevel@tonic-gate 	}
61720Sstevel@tonic-gate 
61730Sstevel@tonic-gate 	/*
61740Sstevel@tonic-gate 	 * It is possible to have a detached nexus with children
61750Sstevel@tonic-gate 	 * and grandchildren (for example: a branch consisting
61760Sstevel@tonic-gate 	 * entirely of bound nodes.) Since the nexus is detached
61770Sstevel@tonic-gate 	 * the bus_unconfig entry point cannot be used to remove
61780Sstevel@tonic-gate 	 * or unconfigure the descendants.
61790Sstevel@tonic-gate 	 */
61801333Scth 	if (!i_ddi_devi_attached(dip) ||
61810Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
61820Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
61830Sstevel@tonic-gate 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
61840Sstevel@tonic-gate 		rv = unconfig_immediate_children(dip, dipp, flags, major);
61850Sstevel@tonic-gate 	} else {
61860Sstevel@tonic-gate 		/*
61870Sstevel@tonic-gate 		 * call bus_unconfig entry point
61880Sstevel@tonic-gate 		 * It should reset nexus flags if unconfigure succeeds.
61890Sstevel@tonic-gate 		 */
61907009Scth 		bus_op = (major == DDI_MAJOR_T_NONE) ?
61910Sstevel@tonic-gate 		    BUS_UNCONFIG_ALL : BUS_UNCONFIG_DRIVER;
61920Sstevel@tonic-gate 		rv = (*f)(dip, flags, bus_op, (void *)(uintptr_t)major);
61930Sstevel@tonic-gate 	}
61940Sstevel@tonic-gate 
61950Sstevel@tonic-gate 	pm_post_unconfig(dip, pm_cookie, NULL);
61960Sstevel@tonic-gate 
61970Sstevel@tonic-gate 	if (brevqp && *brevqp)
61980Sstevel@tonic-gate 		cleanup_br_events_on_grand_children(dip, brevqp);
61990Sstevel@tonic-gate 
62000Sstevel@tonic-gate 	return (rv);
62010Sstevel@tonic-gate }
62020Sstevel@tonic-gate 
62030Sstevel@tonic-gate /*
62040Sstevel@tonic-gate  * called by devfs/framework to unconfigure children bound to major
62050Sstevel@tonic-gate  * If NDI_AUTODETACH is specified, this is invoked by either the
62060Sstevel@tonic-gate  * moduninstall daemon or the modunload -i 0 command.
62070Sstevel@tonic-gate  */
62080Sstevel@tonic-gate int
ndi_devi_unconfig_driver(dev_info_t * dip,int flags,major_t major)62090Sstevel@tonic-gate ndi_devi_unconfig_driver(dev_info_t *dip, int flags, major_t major)
62100Sstevel@tonic-gate {
62110Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
62120Sstevel@tonic-gate 	    "ndi_devi_unconfig_driver: par = %s%d (%p), flags = 0x%x\n",
62130Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
62140Sstevel@tonic-gate 
62150Sstevel@tonic-gate 	return (devi_unconfig_common(dip, NULL, flags, major, NULL));
62160Sstevel@tonic-gate }
62170Sstevel@tonic-gate 
62180Sstevel@tonic-gate int
ndi_devi_unconfig(dev_info_t * dip,int flags)62190Sstevel@tonic-gate ndi_devi_unconfig(dev_info_t *dip, int flags)
62200Sstevel@tonic-gate {
62210Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
62220Sstevel@tonic-gate 	    "ndi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
62230Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
62240Sstevel@tonic-gate 
62257009Scth 	return (devi_unconfig_common(dip, NULL, flags, DDI_MAJOR_T_NONE, NULL));
62260Sstevel@tonic-gate }
62270Sstevel@tonic-gate 
62280Sstevel@tonic-gate int
e_ddi_devi_unconfig(dev_info_t * dip,dev_info_t ** dipp,int flags)62290Sstevel@tonic-gate e_ddi_devi_unconfig(dev_info_t *dip, dev_info_t **dipp, int flags)
62300Sstevel@tonic-gate {
62310Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
62320Sstevel@tonic-gate 	    "e_ddi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
62330Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
62340Sstevel@tonic-gate 
62357009Scth 	return (devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE, NULL));
62360Sstevel@tonic-gate }
62370Sstevel@tonic-gate 
62380Sstevel@tonic-gate /*
62390Sstevel@tonic-gate  * Unconfigure child by name
62400Sstevel@tonic-gate  */
62410Sstevel@tonic-gate static int
devi_unconfig_one(dev_info_t * pdip,char * devnm,int flags)62420Sstevel@tonic-gate devi_unconfig_one(dev_info_t *pdip, char *devnm, int flags)
62430Sstevel@tonic-gate {
62442155Scth 	int		rv, circ;
62452155Scth 	dev_info_t	*child;
62462155Scth 	dev_info_t	*vdip = NULL;
62472155Scth 	int		v_circ;
62480Sstevel@tonic-gate 
62490Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
62500Sstevel@tonic-gate 	child = ndi_devi_findchild(pdip, devnm);
62512155Scth 
62522155Scth 	/*
62532155Scth 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
62542155Scth 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
62552155Scth 	 * management operations.
62562155Scth 	 */
62572155Scth 	if (child && MDI_PHCI(child)) {
62582155Scth 		vdip = mdi_devi_get_vdip(child);
62592155Scth 		if (vdip && (ddi_get_parent(vdip) != pdip)) {
62602155Scth 			ndi_devi_exit(pdip, circ);
62612155Scth 
62622155Scth 			/* use mdi_devi_enter ordering */
62632155Scth 			ndi_devi_enter(vdip, &v_circ);
62642155Scth 			ndi_devi_enter(pdip, &circ);
62652155Scth 			child = ndi_devi_findchild(pdip, devnm);
62662155Scth 		} else
62672155Scth 			vdip = NULL;
62682155Scth 	}
62692155Scth 
62702155Scth 	if (child) {
62712155Scth 		rv = devi_detach_node(child, flags);
62722155Scth 	} else {
62730Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
62740Sstevel@tonic-gate 		    "devi_unconfig_one: %s not found\n", devnm));
62752155Scth 		rv = NDI_SUCCESS;
62762155Scth 	}
62772155Scth 
62780Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
62792155Scth 	if (vdip)
62807235Svikram 		ndi_devi_exit(vdip, v_circ);
62812155Scth 
62820Sstevel@tonic-gate 	return (rv);
62830Sstevel@tonic-gate }
62840Sstevel@tonic-gate 
62850Sstevel@tonic-gate int
ndi_devi_unconfig_one(dev_info_t * pdip,char * devnm,dev_info_t ** dipp,int flags)62860Sstevel@tonic-gate ndi_devi_unconfig_one(
62870Sstevel@tonic-gate 	dev_info_t *pdip,
62880Sstevel@tonic-gate 	char *devnm,
62890Sstevel@tonic-gate 	dev_info_t **dipp,
62900Sstevel@tonic-gate 	int flags)
62910Sstevel@tonic-gate {
62922155Scth 	int		(*f)();
62932155Scth 	int		circ, rv;
62942155Scth 	int		pm_cookie;
62952155Scth 	dev_info_t	*child;
62962155Scth 	dev_info_t	*vdip = NULL;
62972155Scth 	int		v_circ;
62980Sstevel@tonic-gate 	struct brevq_node *brevq = NULL;
62990Sstevel@tonic-gate 
63001333Scth 	ASSERT(i_ddi_devi_attached(pdip));
63010Sstevel@tonic-gate 
63020Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
63030Sstevel@tonic-gate 	    "ndi_devi_unconfig_one: par = %s%d (%p), child = %s\n",
63040Sstevel@tonic-gate 	    ddi_driver_name(pdip), ddi_get_instance(pdip),
63050Sstevel@tonic-gate 	    (void *)pdip, devnm));
63060Sstevel@tonic-gate 
63070Sstevel@tonic-gate 	if (pm_pre_unconfig(pdip, flags, &pm_cookie, devnm) != DDI_SUCCESS)
63080Sstevel@tonic-gate 		return (NDI_FAILURE);
63090Sstevel@tonic-gate 
63100Sstevel@tonic-gate 	if (dipp)
63110Sstevel@tonic-gate 		*dipp = NULL;
63120Sstevel@tonic-gate 
63130Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
63140Sstevel@tonic-gate 	child = ndi_devi_findchild(pdip, devnm);
63152155Scth 
63162155Scth 	/*
63172155Scth 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
63182155Scth 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
63192155Scth 	 * management operations.
63202155Scth 	 */
63212155Scth 	if (child && MDI_PHCI(child)) {
63222155Scth 		vdip = mdi_devi_get_vdip(child);
63232155Scth 		if (vdip && (ddi_get_parent(vdip) != pdip)) {
63242155Scth 			ndi_devi_exit(pdip, circ);
63252155Scth 
63262155Scth 			/* use mdi_devi_enter ordering */
63272155Scth 			ndi_devi_enter(vdip, &v_circ);
63282155Scth 			ndi_devi_enter(pdip, &circ);
63292155Scth 			child = ndi_devi_findchild(pdip, devnm);
63302155Scth 		} else
63312155Scth 			vdip = NULL;
63322155Scth 	}
63332155Scth 
63340Sstevel@tonic-gate 	if (child == NULL) {
63350Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_unconfig_one: %s"
63360Sstevel@tonic-gate 		    " not found\n", devnm));
63372155Scth 		rv = NDI_SUCCESS;
63382155Scth 		goto out;
63390Sstevel@tonic-gate 	}
63400Sstevel@tonic-gate 
63410Sstevel@tonic-gate 	/*
63420Sstevel@tonic-gate 	 * Unconfigure children/descendants of named child
63430Sstevel@tonic-gate 	 */
63440Sstevel@tonic-gate 	rv = devi_unconfig_branch(child, dipp, flags | NDI_UNCONFIG, &brevq);
63450Sstevel@tonic-gate 	if (rv != NDI_SUCCESS)
63460Sstevel@tonic-gate 		goto out;
63470Sstevel@tonic-gate 
63480Sstevel@tonic-gate 	init_bound_node_ev(pdip, child, flags);
63490Sstevel@tonic-gate 
63500Sstevel@tonic-gate 	if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
63510Sstevel@tonic-gate 	    (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
63520Sstevel@tonic-gate 	    (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
63530Sstevel@tonic-gate 		rv = devi_detach_node(child, flags);
63540Sstevel@tonic-gate 	} else {
63550Sstevel@tonic-gate 		/* call bus_config entry point */
63560Sstevel@tonic-gate 		rv = (*f)(pdip, flags, BUS_UNCONFIG_ONE, (void *)devnm);
63570Sstevel@tonic-gate 	}
63580Sstevel@tonic-gate 
63590Sstevel@tonic-gate 	if (brevq) {
63600Sstevel@tonic-gate 		if (rv != NDI_SUCCESS)
63610Sstevel@tonic-gate 			log_and_free_brevq_dip(child, brevq);
63620Sstevel@tonic-gate 		else
63630Sstevel@tonic-gate 			free_brevq(brevq);
63640Sstevel@tonic-gate 	}
63650Sstevel@tonic-gate 
63660Sstevel@tonic-gate 	if (dipp && rv != NDI_SUCCESS) {
63670Sstevel@tonic-gate 		ndi_hold_devi(child);
63680Sstevel@tonic-gate 		ASSERT(*dipp == NULL);
63690Sstevel@tonic-gate 		*dipp = child;
63700Sstevel@tonic-gate 	}
63710Sstevel@tonic-gate 
63720Sstevel@tonic-gate out:
63730Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
63742155Scth 	if (vdip)
63757235Svikram 		ndi_devi_exit(vdip, v_circ);
63762155Scth 
63770Sstevel@tonic-gate 	pm_post_unconfig(pdip, pm_cookie, devnm);
63780Sstevel@tonic-gate 
63790Sstevel@tonic-gate 	return (rv);
63800Sstevel@tonic-gate }
63810Sstevel@tonic-gate 
63820Sstevel@tonic-gate struct async_arg {
63830Sstevel@tonic-gate 	dev_info_t *dip;
63840Sstevel@tonic-gate 	uint_t flags;
63850Sstevel@tonic-gate };
63860Sstevel@tonic-gate 
63870Sstevel@tonic-gate /*
63880Sstevel@tonic-gate  * Common async handler for:
63890Sstevel@tonic-gate  *	ndi_devi_bind_driver_async
63900Sstevel@tonic-gate  *	ndi_devi_online_async
63910Sstevel@tonic-gate  */
63920Sstevel@tonic-gate static int
i_ndi_devi_async_common(dev_info_t * dip,uint_t flags,void (* func)())63930Sstevel@tonic-gate i_ndi_devi_async_common(dev_info_t *dip, uint_t flags, void (*func)())
63940Sstevel@tonic-gate {
63950Sstevel@tonic-gate 	int tqflag;
63960Sstevel@tonic-gate 	int kmflag;
63970Sstevel@tonic-gate 	struct async_arg *arg;
63980Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
63990Sstevel@tonic-gate 
64000Sstevel@tonic-gate 	ASSERT(pdip);
64010Sstevel@tonic-gate 	ASSERT(DEVI(pdip)->devi_taskq);
64020Sstevel@tonic-gate 	ASSERT(ndi_dev_is_persistent_node(dip));
64030Sstevel@tonic-gate 
64040Sstevel@tonic-gate 	if (flags & NDI_NOSLEEP) {
64050Sstevel@tonic-gate 		kmflag = KM_NOSLEEP;
64060Sstevel@tonic-gate 		tqflag = TQ_NOSLEEP;
64070Sstevel@tonic-gate 	} else {
64080Sstevel@tonic-gate 		kmflag = KM_SLEEP;
64090Sstevel@tonic-gate 		tqflag = TQ_SLEEP;
64100Sstevel@tonic-gate 	}
64110Sstevel@tonic-gate 
64120Sstevel@tonic-gate 	arg = kmem_alloc(sizeof (*arg), kmflag);
64130Sstevel@tonic-gate 	if (arg == NULL)
64140Sstevel@tonic-gate 		goto fail;
64150Sstevel@tonic-gate 
64160Sstevel@tonic-gate 	arg->flags = flags;
64170Sstevel@tonic-gate 	arg->dip = dip;
64180Sstevel@tonic-gate 	if (ddi_taskq_dispatch(DEVI(pdip)->devi_taskq, func, arg, tqflag) ==
64190Sstevel@tonic-gate 	    DDI_SUCCESS) {
64200Sstevel@tonic-gate 		return (NDI_SUCCESS);
64210Sstevel@tonic-gate 	}
64220Sstevel@tonic-gate 
64230Sstevel@tonic-gate fail:
64240Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "%s%d: ddi_taskq_dispatch failed",
64250Sstevel@tonic-gate 	    ddi_driver_name(pdip), ddi_get_instance(pdip)));
64260Sstevel@tonic-gate 
64270Sstevel@tonic-gate 	if (arg)
64280Sstevel@tonic-gate 		kmem_free(arg, sizeof (*arg));
64290Sstevel@tonic-gate 	return (NDI_FAILURE);
64300Sstevel@tonic-gate }
64310Sstevel@tonic-gate 
64320Sstevel@tonic-gate static void
i_ndi_devi_bind_driver_cb(struct async_arg * arg)64330Sstevel@tonic-gate i_ndi_devi_bind_driver_cb(struct async_arg *arg)
64340Sstevel@tonic-gate {
64350Sstevel@tonic-gate 	(void) ndi_devi_bind_driver(arg->dip, arg->flags);
64360Sstevel@tonic-gate 	kmem_free(arg, sizeof (*arg));
64370Sstevel@tonic-gate }
64380Sstevel@tonic-gate 
64390Sstevel@tonic-gate int
ndi_devi_bind_driver_async(dev_info_t * dip,uint_t flags)64400Sstevel@tonic-gate ndi_devi_bind_driver_async(dev_info_t *dip, uint_t flags)
64410Sstevel@tonic-gate {
64420Sstevel@tonic-gate 	return (i_ndi_devi_async_common(dip, flags,
64430Sstevel@tonic-gate 	    (void (*)())i_ndi_devi_bind_driver_cb));
64440Sstevel@tonic-gate }
64450Sstevel@tonic-gate 
64460Sstevel@tonic-gate /*
64470Sstevel@tonic-gate  * place the devinfo in the ONLINE state.
64480Sstevel@tonic-gate  */
64490Sstevel@tonic-gate int
ndi_devi_online(dev_info_t * dip,uint_t flags)64500Sstevel@tonic-gate ndi_devi_online(dev_info_t *dip, uint_t flags)
64510Sstevel@tonic-gate {
64520Sstevel@tonic-gate 	int circ, rv;
64530Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
64540Sstevel@tonic-gate 	int branch_event = 0;
64550Sstevel@tonic-gate 
64560Sstevel@tonic-gate 	ASSERT(pdip);
64570Sstevel@tonic-gate 
64580Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_online: %s%d (%p)\n",
64594950Scth 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
64600Sstevel@tonic-gate 
64610Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
64620Sstevel@tonic-gate 	/* bind child before merging .conf nodes */
64630Sstevel@tonic-gate 	rv = i_ndi_config_node(dip, DS_BOUND, flags);
64640Sstevel@tonic-gate 	if (rv != NDI_SUCCESS) {
64650Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
64660Sstevel@tonic-gate 		return (rv);
64670Sstevel@tonic-gate 	}
64680Sstevel@tonic-gate 
64690Sstevel@tonic-gate 	/* merge .conf properties */
64700Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(pdip, flags);
64710Sstevel@tonic-gate 
64722009Sdm120769 	flags |= (NDI_DEVI_ONLINE | NDI_CONFIG);
64730Sstevel@tonic-gate 
64740Sstevel@tonic-gate 	if (flags & NDI_NO_EVENT) {
64750Sstevel@tonic-gate 		/*
64760Sstevel@tonic-gate 		 * Caller is specifically asking for not to generate an event.
64770Sstevel@tonic-gate 		 * Set the following flag so that devi_attach_node() don't
64780Sstevel@tonic-gate 		 * change the event state.
64790Sstevel@tonic-gate 		 */
64800Sstevel@tonic-gate 		flags |= NDI_NO_EVENT_STATE_CHNG;
64810Sstevel@tonic-gate 	}
64820Sstevel@tonic-gate 
64830Sstevel@tonic-gate 	if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
64840Sstevel@tonic-gate 	    ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip))) {
64850Sstevel@tonic-gate 		flags |= NDI_BRANCH_EVENT_OP;
64860Sstevel@tonic-gate 		branch_event = 1;
64870Sstevel@tonic-gate 	}
64880Sstevel@tonic-gate 
64890Sstevel@tonic-gate 	/*
64900Sstevel@tonic-gate 	 * devi_attach_node() may remove dip on failure
64910Sstevel@tonic-gate 	 */
64920Sstevel@tonic-gate 	if ((rv = devi_attach_node(dip, flags)) == NDI_SUCCESS) {
64930Sstevel@tonic-gate 		if ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip)) {
649412138SReed.Liu@Sun.COM 			/*
649512138SReed.Liu@Sun.COM 			 * Hold the attached dip, and exit the parent while
649612138SReed.Liu@Sun.COM 			 * we drive configuration of children below the
649712138SReed.Liu@Sun.COM 			 * attached dip.
649812138SReed.Liu@Sun.COM 			 */
649912138SReed.Liu@Sun.COM 			ndi_hold_devi(dip);
650012138SReed.Liu@Sun.COM 			ndi_devi_exit(pdip, circ);
650112138SReed.Liu@Sun.COM 
65020Sstevel@tonic-gate 			(void) ndi_devi_config(dip, flags);
650312138SReed.Liu@Sun.COM 
650412138SReed.Liu@Sun.COM 			ndi_devi_enter(pdip, &circ);
650512138SReed.Liu@Sun.COM 			ndi_rele_devi(dip);
65060Sstevel@tonic-gate 		}
65070Sstevel@tonic-gate 
65080Sstevel@tonic-gate 		if (branch_event)
65090Sstevel@tonic-gate 			(void) i_log_devfs_branch_add(dip);
65100Sstevel@tonic-gate 	}
65110Sstevel@tonic-gate 
65120Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
65130Sstevel@tonic-gate 
65140Sstevel@tonic-gate 	/*
65150Sstevel@tonic-gate 	 * Notify devfs that we have a new node. Devfs needs to invalidate
65160Sstevel@tonic-gate 	 * cached directory contents.
65170Sstevel@tonic-gate 	 *
65180Sstevel@tonic-gate 	 * For PCMCIA devices, it is possible the pdip is not fully
65190Sstevel@tonic-gate 	 * attached. In this case, calling back into devfs will
65200Sstevel@tonic-gate 	 * result in a loop or assertion error. Hence, the check
65210Sstevel@tonic-gate 	 * on node state.
65220Sstevel@tonic-gate 	 *
65230Sstevel@tonic-gate 	 * If we own parent lock, this is part of a branch operation.
65240Sstevel@tonic-gate 	 * We skip the devfs_clean() step because the cache invalidation
65250Sstevel@tonic-gate 	 * is done higher up in the device tree.
65260Sstevel@tonic-gate 	 */
65271333Scth 	if (rv == NDI_SUCCESS && i_ddi_devi_attached(pdip) &&
65280Sstevel@tonic-gate 	    !DEVI_BUSY_OWNED(pdip))
65290Sstevel@tonic-gate 		(void) devfs_clean(pdip, NULL, 0);
65300Sstevel@tonic-gate 	return (rv);
65310Sstevel@tonic-gate }
65320Sstevel@tonic-gate 
65330Sstevel@tonic-gate static void
i_ndi_devi_online_cb(struct async_arg * arg)65340Sstevel@tonic-gate i_ndi_devi_online_cb(struct async_arg *arg)
65350Sstevel@tonic-gate {
65360Sstevel@tonic-gate 	(void) ndi_devi_online(arg->dip, arg->flags);
65370Sstevel@tonic-gate 	kmem_free(arg, sizeof (*arg));
65380Sstevel@tonic-gate }
65390Sstevel@tonic-gate 
65400Sstevel@tonic-gate int
ndi_devi_online_async(dev_info_t * dip,uint_t flags)65410Sstevel@tonic-gate ndi_devi_online_async(dev_info_t *dip, uint_t flags)
65420Sstevel@tonic-gate {
65430Sstevel@tonic-gate 	/* mark child as need config if requested. */
6544495Scth 	if (flags & NDI_CONFIG) {
6545495Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
65460Sstevel@tonic-gate 		DEVI_SET_NDI_CONFIG(dip);
6547495Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
6548495Scth 	}
65490Sstevel@tonic-gate 
65500Sstevel@tonic-gate 	return (i_ndi_devi_async_common(dip, flags,
65510Sstevel@tonic-gate 	    (void (*)())i_ndi_devi_online_cb));
65520Sstevel@tonic-gate }
65530Sstevel@tonic-gate 
65540Sstevel@tonic-gate /*
65550Sstevel@tonic-gate  * Take a device node Offline
65560Sstevel@tonic-gate  * To take a device Offline means to detach the device instance from
65570Sstevel@tonic-gate  * the driver and prevent devfs requests from re-attaching the device
65580Sstevel@tonic-gate  * instance.
65590Sstevel@tonic-gate  *
65600Sstevel@tonic-gate  * The flag NDI_DEVI_REMOVE causes removes the device node from
65610Sstevel@tonic-gate  * the driver list and the device tree. In this case, the device
65620Sstevel@tonic-gate  * is assumed to be removed from the system.
65630Sstevel@tonic-gate  */
65640Sstevel@tonic-gate int
ndi_devi_offline(dev_info_t * dip,uint_t flags)65650Sstevel@tonic-gate ndi_devi_offline(dev_info_t *dip, uint_t flags)
65660Sstevel@tonic-gate {
65672155Scth 	int		circ, rval = 0;
65682155Scth 	dev_info_t	*pdip = ddi_get_parent(dip);
65692155Scth 	dev_info_t	*vdip = NULL;
65702155Scth 	int		v_circ;
65710Sstevel@tonic-gate 	struct brevq_node *brevq = NULL;
65720Sstevel@tonic-gate 
65730Sstevel@tonic-gate 	ASSERT(pdip);
65740Sstevel@tonic-gate 
65750Sstevel@tonic-gate 	flags |= NDI_DEVI_OFFLINE;
65762155Scth 
65772155Scth 	/*
65782155Scth 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
65792155Scth 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
65802155Scth 	 * management operations.
65812155Scth 	 */
65822155Scth 	if (MDI_PHCI(dip)) {
65832155Scth 		vdip = mdi_devi_get_vdip(dip);
65842155Scth 		if (vdip && (ddi_get_parent(vdip) != pdip))
65852155Scth 			ndi_devi_enter(vdip, &v_circ);
65862155Scth 		else
65872155Scth 			vdip = NULL;
65882155Scth 	}
65890Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
65902155Scth 
659110696SDavid.Hollister@Sun.COM 	if (i_ddi_devi_attached(dip)) {
65920Sstevel@tonic-gate 		/*
65930Sstevel@tonic-gate 		 * If dip is in DS_READY state, there may be cached dv_nodes
65940Sstevel@tonic-gate 		 * referencing this dip, so we invoke devfs code path.
65950Sstevel@tonic-gate 		 * Note that we must release busy changing on pdip to
65960Sstevel@tonic-gate 		 * avoid deadlock against devfs.
65970Sstevel@tonic-gate 		 */
65980Sstevel@tonic-gate 		char *devname = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
65990Sstevel@tonic-gate 		(void) ddi_deviname(dip, devname);
66002155Scth 
66010Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
66022155Scth 		if (vdip)
66032155Scth 			ndi_devi_exit(vdip, v_circ);
66040Sstevel@tonic-gate 
66050Sstevel@tonic-gate 		/*
660610696SDavid.Hollister@Sun.COM 		 * If we are explictly told to clean, then clean. If we own the
660710696SDavid.Hollister@Sun.COM 		 * parent lock then this is part of a branch operation, and we
660810696SDavid.Hollister@Sun.COM 		 * skip the devfs_clean() step.
660910696SDavid.Hollister@Sun.COM 		 *
661010696SDavid.Hollister@Sun.COM 		 * NOTE: A thread performing a devfs file system lookup/
661110696SDavid.Hollister@Sun.COM 		 * bus_config can't call devfs_clean to unconfig without
661210696SDavid.Hollister@Sun.COM 		 * causing rwlock problems in devfs. For ndi_devi_offline, this
661310696SDavid.Hollister@Sun.COM 		 * means that the NDI_DEVFS_CLEAN flag is safe from ioctl code
661410696SDavid.Hollister@Sun.COM 		 * or from an async hotplug thread, but is not safe from a
661510696SDavid.Hollister@Sun.COM 		 * nexus driver's bus_config implementation.
66160Sstevel@tonic-gate 		 */
661710696SDavid.Hollister@Sun.COM 		if ((flags & NDI_DEVFS_CLEAN) ||
661810696SDavid.Hollister@Sun.COM 		    (!DEVI_BUSY_OWNED(pdip)))
66194411Svikram 			(void) devfs_clean(pdip, devname + 1, DV_CLEAN_FORCE);
662010696SDavid.Hollister@Sun.COM 
66210Sstevel@tonic-gate 		kmem_free(devname, MAXNAMELEN + 1);
66220Sstevel@tonic-gate 
66234411Svikram 		rval = devi_unconfig_branch(dip, NULL, flags|NDI_UNCONFIG,
66244411Svikram 		    &brevq);
66254411Svikram 
66260Sstevel@tonic-gate 		if (rval)
66270Sstevel@tonic-gate 			return (NDI_FAILURE);
66280Sstevel@tonic-gate 
66292155Scth 		if (vdip)
66302155Scth 			ndi_devi_enter(vdip, &v_circ);
66310Sstevel@tonic-gate 		ndi_devi_enter(pdip, &circ);
66320Sstevel@tonic-gate 	}
66330Sstevel@tonic-gate 
66340Sstevel@tonic-gate 	init_bound_node_ev(pdip, dip, flags);
66350Sstevel@tonic-gate 
66360Sstevel@tonic-gate 	rval = devi_detach_node(dip, flags);
66370Sstevel@tonic-gate 	if (brevq) {
66380Sstevel@tonic-gate 		if (rval != NDI_SUCCESS)
66390Sstevel@tonic-gate 			log_and_free_brevq_dip(dip, brevq);
66400Sstevel@tonic-gate 		else
66410Sstevel@tonic-gate 			free_brevq(brevq);
66420Sstevel@tonic-gate 	}
66430Sstevel@tonic-gate 
66440Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
66452155Scth 	if (vdip)
66462155Scth 		ndi_devi_exit(vdip, v_circ);
66470Sstevel@tonic-gate 
66480Sstevel@tonic-gate 	return (rval);
66490Sstevel@tonic-gate }
66500Sstevel@tonic-gate 
66510Sstevel@tonic-gate /*
665210696SDavid.Hollister@Sun.COM  * Find the child dev_info node of parent nexus 'p' whose unit address
66530Sstevel@tonic-gate  * matches "cname@caddr".  Recommend use of ndi_devi_findchild() instead.
66540Sstevel@tonic-gate  */
66550Sstevel@tonic-gate dev_info_t *
ndi_devi_find(dev_info_t * pdip,char * cname,char * caddr)66560Sstevel@tonic-gate ndi_devi_find(dev_info_t *pdip, char *cname, char *caddr)
66570Sstevel@tonic-gate {
66580Sstevel@tonic-gate 	dev_info_t *child;
66590Sstevel@tonic-gate 	int circ;
66600Sstevel@tonic-gate 
66610Sstevel@tonic-gate 	if (pdip == NULL || cname == NULL || caddr == NULL)
66620Sstevel@tonic-gate 		return ((dev_info_t *)NULL);
66630Sstevel@tonic-gate 
66640Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
66654145Scth 	child = find_sibling(ddi_get_child(pdip), cname, caddr,
66664145Scth 	    FIND_NODE_BY_NODENAME, NULL);
66670Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
66680Sstevel@tonic-gate 	return (child);
66690Sstevel@tonic-gate }
66700Sstevel@tonic-gate 
66710Sstevel@tonic-gate /*
667210696SDavid.Hollister@Sun.COM  * Find the child dev_info node of parent nexus 'p' whose unit address
66730Sstevel@tonic-gate  * matches devname "name@addr".  Permits caller to hold the parent.
66740Sstevel@tonic-gate  */
66750Sstevel@tonic-gate dev_info_t *
ndi_devi_findchild(dev_info_t * pdip,char * devname)66760Sstevel@tonic-gate ndi_devi_findchild(dev_info_t *pdip, char *devname)
66770Sstevel@tonic-gate {
66780Sstevel@tonic-gate 	dev_info_t *child;
66790Sstevel@tonic-gate 	char	*cname, *caddr;
66800Sstevel@tonic-gate 	char	*devstr;
66810Sstevel@tonic-gate 
66820Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(pdip));
66830Sstevel@tonic-gate 
66840Sstevel@tonic-gate 	devstr = i_ddi_strdup(devname, KM_SLEEP);
66850Sstevel@tonic-gate 	i_ddi_parse_name(devstr, &cname, &caddr, NULL);
66860Sstevel@tonic-gate 
66870Sstevel@tonic-gate 	if (cname == NULL || caddr == NULL) {
66880Sstevel@tonic-gate 		kmem_free(devstr, strlen(devname)+1);
66890Sstevel@tonic-gate 		return ((dev_info_t *)NULL);
66900Sstevel@tonic-gate 	}
66910Sstevel@tonic-gate 
66924145Scth 	child = find_sibling(ddi_get_child(pdip), cname, caddr,
66934145Scth 	    FIND_NODE_BY_NODENAME, NULL);
66940Sstevel@tonic-gate 	kmem_free(devstr, strlen(devname)+1);
66950Sstevel@tonic-gate 	return (child);
66960Sstevel@tonic-gate }
66970Sstevel@tonic-gate 
66980Sstevel@tonic-gate /*
66990Sstevel@tonic-gate  * Misc. routines called by framework only
67000Sstevel@tonic-gate  */
67010Sstevel@tonic-gate 
67020Sstevel@tonic-gate /*
67030Sstevel@tonic-gate  * Clear the DEVI_MADE_CHILDREN/DEVI_ATTACHED_CHILDREN flags
67040Sstevel@tonic-gate  * if new child spec has been added.
67050Sstevel@tonic-gate  */
67060Sstevel@tonic-gate static int
reset_nexus_flags(dev_info_t * dip,void * arg)67070Sstevel@tonic-gate reset_nexus_flags(dev_info_t *dip, void *arg)
67080Sstevel@tonic-gate {
6709298Scth 	struct hwc_spec	*list;
6710298Scth 	int		circ;
67110Sstevel@tonic-gate 
67120Sstevel@tonic-gate 	if (((DEVI(dip)->devi_flags & DEVI_MADE_CHILDREN) == 0) ||
67130Sstevel@tonic-gate 	    ((list = hwc_get_child_spec(dip, (major_t)(uintptr_t)arg)) == NULL))
67140Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
67150Sstevel@tonic-gate 
67160Sstevel@tonic-gate 	hwc_free_spec_list(list);
6717298Scth 
6718298Scth 	/* coordinate child state update */
6719298Scth 	ndi_devi_enter(dip, &circ);
67200Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
67210Sstevel@tonic-gate 	DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN | DEVI_ATTACHED_CHILDREN);
67220Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
6723298Scth 	ndi_devi_exit(dip, circ);
67240Sstevel@tonic-gate 
67250Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
67260Sstevel@tonic-gate }
67270Sstevel@tonic-gate 
67280Sstevel@tonic-gate /*
67290Sstevel@tonic-gate  * Helper functions, returns NULL if no memory.
67300Sstevel@tonic-gate  */
67310Sstevel@tonic-gate 
67320Sstevel@tonic-gate /*
67330Sstevel@tonic-gate  * path_to_major:
67340Sstevel@tonic-gate  *
67350Sstevel@tonic-gate  * Return an alternate driver name binding for the leaf device
67360Sstevel@tonic-gate  * of the given pathname, if there is one. The purpose of this
67370Sstevel@tonic-gate  * function is to deal with generic pathnames. The default action
67380Sstevel@tonic-gate  * for platforms that can't do this (ie: x86 or any platform that
67390Sstevel@tonic-gate  * does not have prom_finddevice functionality, which matches
67400Sstevel@tonic-gate  * nodenames and unit-addresses without the drivers participation)
67417009Scth  * is to return DDI_MAJOR_T_NONE.
67420Sstevel@tonic-gate  *
67430Sstevel@tonic-gate  * Used in loadrootmodules() in the swapgeneric module to
67440Sstevel@tonic-gate  * associate a given pathname with a given leaf driver.
67450Sstevel@tonic-gate  *
67460Sstevel@tonic-gate  */
67470Sstevel@tonic-gate major_t
path_to_major(char * path)67480Sstevel@tonic-gate path_to_major(char *path)
67490Sstevel@tonic-gate {
67500Sstevel@tonic-gate 	dev_info_t *dip;
67510Sstevel@tonic-gate 	char *p, *q;
6752789Sahrens 	pnode_t nodeid;
67534145Scth 	major_t major;
67544145Scth 
67554145Scth 	/* check for path-oriented alias */
67564145Scth 	major = ddi_name_to_major(path);
675712588SJerry.Gilliam@Sun.COM 	if (driver_active(major)) {
67584145Scth 		NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s path bound %s\n",
67594145Scth 		    path, ddi_major_to_name(major)));
67604145Scth 		return (major);
67614145Scth 	}
67620Sstevel@tonic-gate 
67630Sstevel@tonic-gate 	/*
67640Sstevel@tonic-gate 	 * Get the nodeid of the given pathname, if such a mapping exists.
67650Sstevel@tonic-gate 	 */
67660Sstevel@tonic-gate 	dip = NULL;
67670Sstevel@tonic-gate 	nodeid = prom_finddevice(path);
67680Sstevel@tonic-gate 	if (nodeid != OBP_BADNODE) {
67690Sstevel@tonic-gate 		/*
67700Sstevel@tonic-gate 		 * Find the nodeid in our copy of the device tree and return
67710Sstevel@tonic-gate 		 * whatever name we used to bind this node to a driver.
67720Sstevel@tonic-gate 		 */
67730Sstevel@tonic-gate 		dip = e_ddi_nodeid_to_dip(nodeid);
67740Sstevel@tonic-gate 	}
67750Sstevel@tonic-gate 
67760Sstevel@tonic-gate 	if (dip == NULL) {
67770Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_WARN,
67780Sstevel@tonic-gate 		    "path_to_major: can't bind <%s>\n", path));
67797009Scth 		return (DDI_MAJOR_T_NONE);
67800Sstevel@tonic-gate 	}
67810Sstevel@tonic-gate 
67820Sstevel@tonic-gate 	/*
67830Sstevel@tonic-gate 	 * If we're bound to something other than the nodename,
67840Sstevel@tonic-gate 	 * note that in the message buffer and system log.
67850Sstevel@tonic-gate 	 */
67860Sstevel@tonic-gate 	p = ddi_binding_name(dip);
67870Sstevel@tonic-gate 	q = ddi_node_name(dip);
67880Sstevel@tonic-gate 	if (p && q && (strcmp(p, q) != 0))
67890Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s bound to %s\n",
67900Sstevel@tonic-gate 		    path, p));
67910Sstevel@tonic-gate 
67924145Scth 	major = ddi_name_to_major(p);
67934145Scth 
67944145Scth 	ndi_rele_devi(dip);		/* release e_ddi_nodeid_to_dip hold */
67954145Scth 
67964145Scth 	return (major);
67970Sstevel@tonic-gate }
67980Sstevel@tonic-gate 
67990Sstevel@tonic-gate /*
68000Sstevel@tonic-gate  * Return the held dip for the specified major and instance, attempting to do
68010Sstevel@tonic-gate  * an attach if specified. Return NULL if the devi can't be found or put in
68020Sstevel@tonic-gate  * the proper state. The caller must release the hold via ddi_release_devi if
68030Sstevel@tonic-gate  * a non-NULL value is returned.
68040Sstevel@tonic-gate  *
68050Sstevel@tonic-gate  * Some callers expect to be able to perform a hold_devi() while in a context
68060Sstevel@tonic-gate  * where using ndi_devi_enter() to ensure the hold might cause deadlock (see
68070Sstevel@tonic-gate  * open-from-attach code in consconfig_dacf.c). Such special-case callers
680810696SDavid.Hollister@Sun.COM  * must ensure that an ndi_devi_enter(parent)/ndi_hold_devi() from a safe
68090Sstevel@tonic-gate  * context is already active. The hold_devi() implementation must accommodate
68100Sstevel@tonic-gate  * these callers.
68110Sstevel@tonic-gate  */
68120Sstevel@tonic-gate static dev_info_t *
hold_devi(major_t major,int instance,int flags)68130Sstevel@tonic-gate hold_devi(major_t major, int instance, int flags)
68140Sstevel@tonic-gate {
68150Sstevel@tonic-gate 	struct devnames	*dnp;
68160Sstevel@tonic-gate 	dev_info_t	*dip;
68170Sstevel@tonic-gate 	char		*path;
68189065SChris.Horne@Sun.COM 	char		*vpath;
68190Sstevel@tonic-gate 
68200Sstevel@tonic-gate 	if ((major >= devcnt) || (instance == -1))
68210Sstevel@tonic-gate 		return (NULL);
68220Sstevel@tonic-gate 
68230Sstevel@tonic-gate 	/* try to find the instance in the per driver list */
68240Sstevel@tonic-gate 	dnp = &(devnamesp[major]);
68250Sstevel@tonic-gate 	LOCK_DEV_OPS(&(dnp->dn_lock));
68260Sstevel@tonic-gate 	for (dip = dnp->dn_head; dip;
68270Sstevel@tonic-gate 	    dip = (dev_info_t *)DEVI(dip)->devi_next) {
68280Sstevel@tonic-gate 		/* skip node if instance field is not valid */
68290Sstevel@tonic-gate 		if (i_ddi_node_state(dip) < DS_INITIALIZED)
68300Sstevel@tonic-gate 			continue;
68310Sstevel@tonic-gate 
68320Sstevel@tonic-gate 		/* look for instance match */
68330Sstevel@tonic-gate 		if (DEVI(dip)->devi_instance == instance) {
68340Sstevel@tonic-gate 			/*
68350Sstevel@tonic-gate 			 * To accommodate callers that can't block in
683610696SDavid.Hollister@Sun.COM 			 * ndi_devi_enter() we do an ndi_hold_devi(), and
68370Sstevel@tonic-gate 			 * afterwards check that the node is in a state where
68380Sstevel@tonic-gate 			 * the hold prevents detach(). If we did not manage to
68390Sstevel@tonic-gate 			 * prevent detach then we ndi_rele_devi() and perform
68400Sstevel@tonic-gate 			 * the slow path below (which can result in a blocking
68410Sstevel@tonic-gate 			 * ndi_devi_enter() while driving attach top-down).
68420Sstevel@tonic-gate 			 * This code depends on the ordering of
68430Sstevel@tonic-gate 			 * DEVI_SET_DETACHING and the devi_ref check in the
68440Sstevel@tonic-gate 			 * detach_node() code path.
68450Sstevel@tonic-gate 			 */
68460Sstevel@tonic-gate 			ndi_hold_devi(dip);
68471333Scth 			if (i_ddi_devi_attached(dip) &&
68480Sstevel@tonic-gate 			    !DEVI_IS_DETACHING(dip)) {
68490Sstevel@tonic-gate 				UNLOCK_DEV_OPS(&(dnp->dn_lock));
68500Sstevel@tonic-gate 				return (dip);	/* fast-path with devi held */
68510Sstevel@tonic-gate 			}
68520Sstevel@tonic-gate 			ndi_rele_devi(dip);
68530Sstevel@tonic-gate 
68540Sstevel@tonic-gate 			/* try slow-path */
68550Sstevel@tonic-gate 			dip = NULL;
68560Sstevel@tonic-gate 			break;
68570Sstevel@tonic-gate 		}
68580Sstevel@tonic-gate 	}
68590Sstevel@tonic-gate 	ASSERT(dip == NULL);
68600Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
68610Sstevel@tonic-gate 
68620Sstevel@tonic-gate 	if (flags & E_DDI_HOLD_DEVI_NOATTACH)
68630Sstevel@tonic-gate 		return (NULL);		/* told not to drive attach */
68640Sstevel@tonic-gate 
68650Sstevel@tonic-gate 	/* slow-path may block, so it should not occur from interrupt */
68660Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
68670Sstevel@tonic-gate 	if (servicing_interrupt())
68680Sstevel@tonic-gate 		return (NULL);
68690Sstevel@tonic-gate 
68700Sstevel@tonic-gate 	/* reconstruct the path and drive attach by path through devfs. */
68710Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
68729065SChris.Horne@Sun.COM 	if (e_ddi_majorinstance_to_path(major, instance, path) == 0) {
68730Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_path(path, flags);
68749065SChris.Horne@Sun.COM 
68759065SChris.Horne@Sun.COM 		/*
68769065SChris.Horne@Sun.COM 		 * Verify that we got the correct device - a path_to_inst file
68779065SChris.Horne@Sun.COM 		 * with a bogus/corrupt path (or a nexus that changes its
68789065SChris.Horne@Sun.COM 		 * unit-address format) could result in an incorrect answer
68799065SChris.Horne@Sun.COM 		 *
68809065SChris.Horne@Sun.COM 		 * Verify major, instance, and path.
68819065SChris.Horne@Sun.COM 		 */
68829065SChris.Horne@Sun.COM 		vpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
68839065SChris.Horne@Sun.COM 		if (dip &&
68849065SChris.Horne@Sun.COM 		    ((DEVI(dip)->devi_major != major) ||
68859065SChris.Horne@Sun.COM 		    ((DEVI(dip)->devi_instance != instance)) ||
68869065SChris.Horne@Sun.COM 		    (strcmp(path, ddi_pathname(dip, vpath)) != 0))) {
68879065SChris.Horne@Sun.COM 			ndi_rele_devi(dip);
68889065SChris.Horne@Sun.COM 			dip = NULL;	/* no answer better than wrong answer */
68899065SChris.Horne@Sun.COM 		}
68909065SChris.Horne@Sun.COM 		kmem_free(vpath, MAXPATHLEN);
68919065SChris.Horne@Sun.COM 	}
68920Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
68930Sstevel@tonic-gate 	return (dip);			/* with devi held */
68940Sstevel@tonic-gate }
68950Sstevel@tonic-gate 
68960Sstevel@tonic-gate /*
68970Sstevel@tonic-gate  * The {e_}ddi_hold_devi{_by_{instance|dev|path}} hold the devinfo node
68980Sstevel@tonic-gate  * associated with the specified arguments.  This hold should be released
68990Sstevel@tonic-gate  * by calling ddi_release_devi.
69000Sstevel@tonic-gate  *
69010Sstevel@tonic-gate  * The E_DDI_HOLD_DEVI_NOATTACH flag argument allows the caller to to specify
69020Sstevel@tonic-gate  * a failure return if the node is not already attached.
69030Sstevel@tonic-gate  *
69040Sstevel@tonic-gate  * NOTE: by the time we make e_ddi_hold_devi public, we should be able to reuse
69050Sstevel@tonic-gate  * ddi_hold_devi again.
69060Sstevel@tonic-gate  */
69070Sstevel@tonic-gate dev_info_t *
ddi_hold_devi_by_instance(major_t major,int instance,int flags)69080Sstevel@tonic-gate ddi_hold_devi_by_instance(major_t major, int instance, int flags)
69090Sstevel@tonic-gate {
69100Sstevel@tonic-gate 	return (hold_devi(major, instance, flags));
69110Sstevel@tonic-gate }
69120Sstevel@tonic-gate 
69130Sstevel@tonic-gate dev_info_t *
e_ddi_hold_devi_by_dev(dev_t dev,int flags)69140Sstevel@tonic-gate e_ddi_hold_devi_by_dev(dev_t dev, int flags)
69150Sstevel@tonic-gate {
69160Sstevel@tonic-gate 	major_t	major = getmajor(dev);
69170Sstevel@tonic-gate 	dev_info_t	*dip;
69180Sstevel@tonic-gate 	struct dev_ops	*ops;
69190Sstevel@tonic-gate 	dev_info_t	*ddip = NULL;
69200Sstevel@tonic-gate 
69210Sstevel@tonic-gate 	dip = hold_devi(major, dev_to_instance(dev), flags);
69220Sstevel@tonic-gate 
69230Sstevel@tonic-gate 	/*
69240Sstevel@tonic-gate 	 * The rest of this routine is legacy support for drivers that
69250Sstevel@tonic-gate 	 * have broken DDI_INFO_DEVT2INSTANCE implementations but may have
69260Sstevel@tonic-gate 	 * functional DDI_INFO_DEVT2DEVINFO implementations.  This code will
69270Sstevel@tonic-gate 	 * diagnose inconsistency and, for maximum compatibility with legacy
69280Sstevel@tonic-gate 	 * drivers, give preference to the drivers DDI_INFO_DEVT2DEVINFO
69290Sstevel@tonic-gate 	 * implementation over the above derived dip based the driver's
69300Sstevel@tonic-gate 	 * DDI_INFO_DEVT2INSTANCE implementation. This legacy support should
69310Sstevel@tonic-gate 	 * be removed when DDI_INFO_DEVT2DEVINFO is deprecated.
69320Sstevel@tonic-gate 	 *
69330Sstevel@tonic-gate 	 * NOTE: The following code has a race condition. DEVT2DEVINFO
69340Sstevel@tonic-gate 	 *	returns a dip which is not held. By the time we ref ddip,
69350Sstevel@tonic-gate 	 *	it could have been freed. The saving grace is that for
69360Sstevel@tonic-gate 	 *	most drivers, the dip returned from hold_devi() is the
69370Sstevel@tonic-gate 	 *	same one as the one returned by DEVT2DEVINFO, so we are
69380Sstevel@tonic-gate 	 *	safe for drivers with the correct getinfo(9e) impl.
69390Sstevel@tonic-gate 	 */
69400Sstevel@tonic-gate 	if (((ops = ddi_hold_driver(major)) != NULL) &&
69410Sstevel@tonic-gate 	    CB_DRV_INSTALLED(ops) && ops->devo_getinfo)  {
69420Sstevel@tonic-gate 		if ((*ops->devo_getinfo)(NULL, DDI_INFO_DEVT2DEVINFO,
69430Sstevel@tonic-gate 		    (void *)dev, (void **)&ddip) != DDI_SUCCESS)
69440Sstevel@tonic-gate 			ddip = NULL;
69450Sstevel@tonic-gate 	}
69460Sstevel@tonic-gate 
69470Sstevel@tonic-gate 	/* give preference to the driver returned DEVT2DEVINFO dip */
69480Sstevel@tonic-gate 	if (ddip && (dip != ddip)) {
69490Sstevel@tonic-gate #ifdef	DEBUG
69500Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: inconsistent getinfo(9E) implementation",
69510Sstevel@tonic-gate 		    ddi_driver_name(ddip));
69520Sstevel@tonic-gate #endif	/* DEBUG */
69530Sstevel@tonic-gate 		ndi_hold_devi(ddip);
69540Sstevel@tonic-gate 		if (dip)
69550Sstevel@tonic-gate 			ndi_rele_devi(dip);
69560Sstevel@tonic-gate 		dip = ddip;
69570Sstevel@tonic-gate 	}
69580Sstevel@tonic-gate 
69590Sstevel@tonic-gate 	if (ops)
69600Sstevel@tonic-gate 		ddi_rele_driver(major);
69610Sstevel@tonic-gate 
69620Sstevel@tonic-gate 	return (dip);
69630Sstevel@tonic-gate }
69640Sstevel@tonic-gate 
69650Sstevel@tonic-gate /*
69660Sstevel@tonic-gate  * For compatibility only. Do not call this function!
69670Sstevel@tonic-gate  */
69680Sstevel@tonic-gate dev_info_t *
e_ddi_get_dev_info(dev_t dev,vtype_t type)69690Sstevel@tonic-gate e_ddi_get_dev_info(dev_t dev, vtype_t type)
69700Sstevel@tonic-gate {
69710Sstevel@tonic-gate 	dev_info_t *dip = NULL;
69720Sstevel@tonic-gate 	if (getmajor(dev) >= devcnt)
69730Sstevel@tonic-gate 		return (NULL);
69740Sstevel@tonic-gate 
69750Sstevel@tonic-gate 	switch (type) {
69760Sstevel@tonic-gate 	case VCHR:
69770Sstevel@tonic-gate 	case VBLK:
69780Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
69790Sstevel@tonic-gate 	default:
69800Sstevel@tonic-gate 		break;
69810Sstevel@tonic-gate 	}
69820Sstevel@tonic-gate 
69830Sstevel@tonic-gate 	/*
69840Sstevel@tonic-gate 	 * For compatibility reasons, we can only return the dip with
69850Sstevel@tonic-gate 	 * the driver ref count held. This is not a safe thing to do.
69860Sstevel@tonic-gate 	 * For certain broken third-party software, we are willing
69870Sstevel@tonic-gate 	 * to venture into unknown territory.
69880Sstevel@tonic-gate 	 */
69890Sstevel@tonic-gate 	if (dip) {
69900Sstevel@tonic-gate 		(void) ndi_hold_driver(dip);
69910Sstevel@tonic-gate 		ndi_rele_devi(dip);
69920Sstevel@tonic-gate 	}
69930Sstevel@tonic-gate 	return (dip);
69940Sstevel@tonic-gate }
69950Sstevel@tonic-gate 
69960Sstevel@tonic-gate dev_info_t *
e_ddi_hold_devi_by_path(char * path,int flags)69970Sstevel@tonic-gate e_ddi_hold_devi_by_path(char *path, int flags)
69980Sstevel@tonic-gate {
69990Sstevel@tonic-gate 	dev_info_t	*dip;
70000Sstevel@tonic-gate 
70010Sstevel@tonic-gate 	/* can't specify NOATTACH by path */
70020Sstevel@tonic-gate 	ASSERT(!(flags & E_DDI_HOLD_DEVI_NOATTACH));
70030Sstevel@tonic-gate 
70040Sstevel@tonic-gate 	return (resolve_pathname(path, &dip, NULL, NULL) ? NULL : dip);
70050Sstevel@tonic-gate }
70060Sstevel@tonic-gate 
70070Sstevel@tonic-gate void
e_ddi_hold_devi(dev_info_t * dip)70080Sstevel@tonic-gate e_ddi_hold_devi(dev_info_t *dip)
70090Sstevel@tonic-gate {
70100Sstevel@tonic-gate 	ndi_hold_devi(dip);
70110Sstevel@tonic-gate }
70120Sstevel@tonic-gate 
70130Sstevel@tonic-gate void
ddi_release_devi(dev_info_t * dip)70140Sstevel@tonic-gate ddi_release_devi(dev_info_t *dip)
70150Sstevel@tonic-gate {
70160Sstevel@tonic-gate 	ndi_rele_devi(dip);
70170Sstevel@tonic-gate }
70180Sstevel@tonic-gate 
70190Sstevel@tonic-gate /*
70200Sstevel@tonic-gate  * Associate a streams queue with a devinfo node
70210Sstevel@tonic-gate  * NOTE: This function is called by STREAM driver's put procedure.
70220Sstevel@tonic-gate  *	It cannot block.
70230Sstevel@tonic-gate  */
70240Sstevel@tonic-gate void
ddi_assoc_queue_with_devi(queue_t * q,dev_info_t * dip)70250Sstevel@tonic-gate ddi_assoc_queue_with_devi(queue_t *q, dev_info_t *dip)
70260Sstevel@tonic-gate {
70270Sstevel@tonic-gate 	queue_t *rq = _RD(q);
70280Sstevel@tonic-gate 	struct stdata *stp;
70290Sstevel@tonic-gate 	vnode_t *vp;
70300Sstevel@tonic-gate 
70310Sstevel@tonic-gate 	/* set flag indicating that ddi_assoc_queue_with_devi was called */
70320Sstevel@tonic-gate 	mutex_enter(QLOCK(rq));
70330Sstevel@tonic-gate 	rq->q_flag |= _QASSOCIATED;
70340Sstevel@tonic-gate 	mutex_exit(QLOCK(rq));
70350Sstevel@tonic-gate 
70360Sstevel@tonic-gate 	/* get the vnode associated with the queue */
70370Sstevel@tonic-gate 	stp = STREAM(rq);
70380Sstevel@tonic-gate 	vp = stp->sd_vnode;
70390Sstevel@tonic-gate 	ASSERT(vp);
70400Sstevel@tonic-gate 
70410Sstevel@tonic-gate 	/* change the hardware association of the vnode */
70420Sstevel@tonic-gate 	spec_assoc_vp_with_devi(vp, dip);
70430Sstevel@tonic-gate }
70440Sstevel@tonic-gate 
70450Sstevel@tonic-gate /*
70460Sstevel@tonic-gate  * ddi_install_driver(name)
70470Sstevel@tonic-gate  *
70480Sstevel@tonic-gate  * Driver installation is currently a byproduct of driver loading.  This
70490Sstevel@tonic-gate  * may change.
70500Sstevel@tonic-gate  */
70510Sstevel@tonic-gate int
ddi_install_driver(char * name)70520Sstevel@tonic-gate ddi_install_driver(char *name)
70530Sstevel@tonic-gate {
70540Sstevel@tonic-gate 	major_t major = ddi_name_to_major(name);
70550Sstevel@tonic-gate 
70567009Scth 	if ((major == DDI_MAJOR_T_NONE) ||
70570Sstevel@tonic-gate 	    (ddi_hold_installed_driver(major) == NULL)) {
70580Sstevel@tonic-gate 		return (DDI_FAILURE);
70590Sstevel@tonic-gate 	}
70600Sstevel@tonic-gate 	ddi_rele_driver(major);
70610Sstevel@tonic-gate 	return (DDI_SUCCESS);
70620Sstevel@tonic-gate }
70630Sstevel@tonic-gate 
70640Sstevel@tonic-gate struct dev_ops *
ddi_hold_driver(major_t major)70650Sstevel@tonic-gate ddi_hold_driver(major_t major)
70660Sstevel@tonic-gate {
70670Sstevel@tonic-gate 	return (mod_hold_dev_by_major(major));
70680Sstevel@tonic-gate }
70690Sstevel@tonic-gate 
70700Sstevel@tonic-gate 
70710Sstevel@tonic-gate void
ddi_rele_driver(major_t major)70720Sstevel@tonic-gate ddi_rele_driver(major_t major)
70730Sstevel@tonic-gate {
70740Sstevel@tonic-gate 	mod_rele_dev_by_major(major);
70750Sstevel@tonic-gate }
70760Sstevel@tonic-gate 
70770Sstevel@tonic-gate 
70780Sstevel@tonic-gate /*
70790Sstevel@tonic-gate  * This is called during boot to force attachment order of special dips
70800Sstevel@tonic-gate  * dip must be referenced via ndi_hold_devi()
70810Sstevel@tonic-gate  */
70820Sstevel@tonic-gate int
i_ddi_attach_node_hierarchy(dev_info_t * dip)70830Sstevel@tonic-gate i_ddi_attach_node_hierarchy(dev_info_t *dip)
70840Sstevel@tonic-gate {
70852155Scth 	dev_info_t	*parent;
70862155Scth 	int		ret, circ;
70872155Scth 
70882155Scth 	/*
70892155Scth 	 * Recurse up until attached parent is found.
70902155Scth 	 */
70912009Sdm120769 	if (i_ddi_devi_attached(dip))
70922009Sdm120769 		return (DDI_SUCCESS);
70930Sstevel@tonic-gate 	parent = ddi_get_parent(dip);
70940Sstevel@tonic-gate 	if (i_ddi_attach_node_hierarchy(parent) != DDI_SUCCESS)
70950Sstevel@tonic-gate 		return (DDI_FAILURE);
70960Sstevel@tonic-gate 
70970Sstevel@tonic-gate 	/*
70982155Scth 	 * Come top-down, expanding .conf nodes under this parent
70992155Scth 	 * and driving attach.
71000Sstevel@tonic-gate 	 */
71012155Scth 	ndi_devi_enter(parent, &circ);
71020Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(parent, 0);
71032155Scth 	ret = i_ddi_attachchild(dip);
71042155Scth 	ndi_devi_exit(parent, circ);
71052155Scth 
71062155Scth 	return (ret);
71070Sstevel@tonic-gate }
71080Sstevel@tonic-gate 
71090Sstevel@tonic-gate /* keep this function static */
71100Sstevel@tonic-gate static int
attach_driver_nodes(major_t major)71110Sstevel@tonic-gate attach_driver_nodes(major_t major)
71120Sstevel@tonic-gate {
71130Sstevel@tonic-gate 	struct devnames *dnp;
71140Sstevel@tonic-gate 	dev_info_t *dip;
71150Sstevel@tonic-gate 	int error = DDI_FAILURE;
71160Sstevel@tonic-gate 
71170Sstevel@tonic-gate 	dnp = &devnamesp[major];
71180Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
71190Sstevel@tonic-gate 	dip = dnp->dn_head;
71200Sstevel@tonic-gate 	while (dip) {
71210Sstevel@tonic-gate 		ndi_hold_devi(dip);
71220Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
71230Sstevel@tonic-gate 		if (i_ddi_attach_node_hierarchy(dip) == DDI_SUCCESS)
71240Sstevel@tonic-gate 			error = DDI_SUCCESS;
71259621SJaven.Wu@Sun.COM 		/*
71269621SJaven.Wu@Sun.COM 		 * Set the 'ddi-config-driver-node' property on a nexus
71279621SJaven.Wu@Sun.COM 		 * node to cause attach_driver_nodes() to configure all
71289621SJaven.Wu@Sun.COM 		 * immediate children of the nexus. This property should
71299621SJaven.Wu@Sun.COM 		 * be set on nodes with immediate children that bind to
71309621SJaven.Wu@Sun.COM 		 * the same driver as parent.
71319621SJaven.Wu@Sun.COM 		 */
71329621SJaven.Wu@Sun.COM 		if ((error == DDI_SUCCESS) && (ddi_prop_exists(DDI_DEV_T_ANY,
71339621SJaven.Wu@Sun.COM 		    dip, DDI_PROP_DONTPASS, "ddi-config-driver-node"))) {
71349621SJaven.Wu@Sun.COM 			(void) ndi_devi_config(dip, NDI_NO_EVENT);
71359621SJaven.Wu@Sun.COM 		}
71360Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
71370Sstevel@tonic-gate 		ndi_rele_devi(dip);
71380Sstevel@tonic-gate 		dip = ddi_get_next(dip);
71390Sstevel@tonic-gate 	}
71400Sstevel@tonic-gate 	if (error == DDI_SUCCESS)
71410Sstevel@tonic-gate 		dnp->dn_flags |= DN_NO_AUTODETACH;
71420Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
71430Sstevel@tonic-gate 
71440Sstevel@tonic-gate 
71450Sstevel@tonic-gate 	return (error);
71460Sstevel@tonic-gate }
71470Sstevel@tonic-gate 
71480Sstevel@tonic-gate /*
71490Sstevel@tonic-gate  * i_ddi_attach_hw_nodes configures and attaches all hw nodes
71500Sstevel@tonic-gate  * bound to a specific driver. This function replaces calls to
71510Sstevel@tonic-gate  * ddi_hold_installed_driver() for drivers with no .conf
71520Sstevel@tonic-gate  * enumerated nodes.
71530Sstevel@tonic-gate  *
71540Sstevel@tonic-gate  * This facility is typically called at boot time to attach
71550Sstevel@tonic-gate  * platform-specific hardware nodes, such as ppm nodes on xcal
71560Sstevel@tonic-gate  * and grover and keyswitch nodes on cherrystone. It does not
71570Sstevel@tonic-gate  * deal with .conf enumerated node. Calling it beyond the boot
71580Sstevel@tonic-gate  * process is strongly discouraged.
71590Sstevel@tonic-gate  */
71600Sstevel@tonic-gate int
i_ddi_attach_hw_nodes(char * driver)71610Sstevel@tonic-gate i_ddi_attach_hw_nodes(char *driver)
71620Sstevel@tonic-gate {
71630Sstevel@tonic-gate 	major_t major;
71640Sstevel@tonic-gate 
71650Sstevel@tonic-gate 	major = ddi_name_to_major(driver);
71667009Scth 	if (major == DDI_MAJOR_T_NONE)
71670Sstevel@tonic-gate 		return (DDI_FAILURE);
71680Sstevel@tonic-gate 
71690Sstevel@tonic-gate 	return (attach_driver_nodes(major));
71700Sstevel@tonic-gate }
71710Sstevel@tonic-gate 
71720Sstevel@tonic-gate /*
71730Sstevel@tonic-gate  * i_ddi_attach_pseudo_node configures pseudo drivers which
71740Sstevel@tonic-gate  * has a single node. The .conf nodes must be enumerated
71750Sstevel@tonic-gate  * before calling this interface. The dip is held attached
71760Sstevel@tonic-gate  * upon returning.
71770Sstevel@tonic-gate  *
71780Sstevel@tonic-gate  * This facility should only be called only at boot time
71790Sstevel@tonic-gate  * by the I/O framework.
71800Sstevel@tonic-gate  */
71810Sstevel@tonic-gate dev_info_t *
i_ddi_attach_pseudo_node(char * driver)71820Sstevel@tonic-gate i_ddi_attach_pseudo_node(char *driver)
71830Sstevel@tonic-gate {
71840Sstevel@tonic-gate 	major_t major;
71850Sstevel@tonic-gate 	dev_info_t *dip;
71860Sstevel@tonic-gate 
71870Sstevel@tonic-gate 	major = ddi_name_to_major(driver);
71887009Scth 	if (major == DDI_MAJOR_T_NONE)
71890Sstevel@tonic-gate 		return (NULL);
71900Sstevel@tonic-gate 
71910Sstevel@tonic-gate 	if (attach_driver_nodes(major) != DDI_SUCCESS)
71920Sstevel@tonic-gate 		return (NULL);
71930Sstevel@tonic-gate 
71940Sstevel@tonic-gate 	dip = devnamesp[major].dn_head;
71950Sstevel@tonic-gate 	ASSERT(dip && ddi_get_next(dip) == NULL);
71960Sstevel@tonic-gate 	ndi_hold_devi(dip);
71970Sstevel@tonic-gate 	return (dip);
71980Sstevel@tonic-gate }
71990Sstevel@tonic-gate 
72000Sstevel@tonic-gate static void
diplist_to_parent_major(dev_info_t * head,char parents[])72010Sstevel@tonic-gate diplist_to_parent_major(dev_info_t *head, char parents[])
72020Sstevel@tonic-gate {
72030Sstevel@tonic-gate 	major_t major;
72040Sstevel@tonic-gate 	dev_info_t *dip, *pdip;
72050Sstevel@tonic-gate 
72060Sstevel@tonic-gate 	for (dip = head; dip != NULL; dip = ddi_get_next(dip)) {
72070Sstevel@tonic-gate 		pdip = ddi_get_parent(dip);
72080Sstevel@tonic-gate 		ASSERT(pdip);	/* disallow rootnex.conf nodes */
72090Sstevel@tonic-gate 		major = ddi_driver_major(pdip);
72107009Scth 		if ((major != DDI_MAJOR_T_NONE) && parents[major] == 0)
72110Sstevel@tonic-gate 			parents[major] = 1;
72120Sstevel@tonic-gate 	}
72130Sstevel@tonic-gate }
72140Sstevel@tonic-gate 
72150Sstevel@tonic-gate /*
72160Sstevel@tonic-gate  * Call ddi_hold_installed_driver() on each parent major
72170Sstevel@tonic-gate  * and invoke mt_config_driver() to attach child major.
72180Sstevel@tonic-gate  * This is part of the implementation of ddi_hold_installed_driver.
72190Sstevel@tonic-gate  */
72200Sstevel@tonic-gate static int
attach_driver_by_parent(major_t child_major,char parents[])72210Sstevel@tonic-gate attach_driver_by_parent(major_t child_major, char parents[])
72220Sstevel@tonic-gate {
72230Sstevel@tonic-gate 	major_t par_major;
72240Sstevel@tonic-gate 	struct mt_config_handle *hdl;
72250Sstevel@tonic-gate 	int flags = NDI_DEVI_PERSIST | NDI_NO_EVENT;
72260Sstevel@tonic-gate 
72270Sstevel@tonic-gate 	hdl = mt_config_init(NULL, NULL, flags, child_major, MT_CONFIG_OP,
72280Sstevel@tonic-gate 	    NULL);
72290Sstevel@tonic-gate 	for (par_major = 0; par_major < devcnt; par_major++) {
72300Sstevel@tonic-gate 		/* disallow recursion on the same driver */
72310Sstevel@tonic-gate 		if (parents[par_major] == 0 || par_major == child_major)
72320Sstevel@tonic-gate 			continue;
72330Sstevel@tonic-gate 		if (ddi_hold_installed_driver(par_major) == NULL)
72340Sstevel@tonic-gate 			continue;
72350Sstevel@tonic-gate 		hdl->mtc_parmajor = par_major;
72360Sstevel@tonic-gate 		mt_config_driver(hdl);
72370Sstevel@tonic-gate 		ddi_rele_driver(par_major);
72380Sstevel@tonic-gate 	}
72390Sstevel@tonic-gate 	(void) mt_config_fini(hdl);
72400Sstevel@tonic-gate 
72410Sstevel@tonic-gate 	return (i_ddi_devs_attached(child_major));
72420Sstevel@tonic-gate }
72430Sstevel@tonic-gate 
72440Sstevel@tonic-gate int
i_ddi_devs_attached(major_t major)72450Sstevel@tonic-gate i_ddi_devs_attached(major_t major)
72460Sstevel@tonic-gate {
72470Sstevel@tonic-gate 	dev_info_t *dip;
72480Sstevel@tonic-gate 	struct devnames *dnp;
72490Sstevel@tonic-gate 	int error = DDI_FAILURE;
72500Sstevel@tonic-gate 
72510Sstevel@tonic-gate 	/* check for attached instances */
72520Sstevel@tonic-gate 	dnp = &devnamesp[major];
72530Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
72540Sstevel@tonic-gate 	for (dip = dnp->dn_head; dip != NULL; dip = ddi_get_next(dip)) {
72551333Scth 		if (i_ddi_devi_attached(dip)) {
72560Sstevel@tonic-gate 			error = DDI_SUCCESS;
72570Sstevel@tonic-gate 			break;
72580Sstevel@tonic-gate 		}
72590Sstevel@tonic-gate 	}
72600Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
72610Sstevel@tonic-gate 
72620Sstevel@tonic-gate 	return (error);
72630Sstevel@tonic-gate }
72640Sstevel@tonic-gate 
72655895Syz147064 int
i_ddi_minor_node_count(dev_info_t * ddip,const char * node_type)72665895Syz147064 i_ddi_minor_node_count(dev_info_t *ddip, const char *node_type)
72675895Syz147064 {
72687224Scth 	int			circ;
72697224Scth 	struct ddi_minor_data	*dp;
72707224Scth 	int			count = 0;
72717224Scth 
72727224Scth 	ndi_devi_enter(ddip, &circ);
72737224Scth 	for (dp = DEVI(ddip)->devi_minor; dp != NULL; dp = dp->next) {
72745895Syz147064 		if (strcmp(dp->ddm_node_type, node_type) == 0)
72755895Syz147064 			count++;
72767224Scth 	}
72777224Scth 	ndi_devi_exit(ddip, circ);
72785895Syz147064 	return (count);
72795895Syz147064 }
72805895Syz147064 
72810Sstevel@tonic-gate /*
72820Sstevel@tonic-gate  * ddi_hold_installed_driver configures and attaches all
72830Sstevel@tonic-gate  * instances of the specified driver. To accomplish this
72840Sstevel@tonic-gate  * it configures and attaches all possible parents of
72850Sstevel@tonic-gate  * the driver, enumerated both in h/w nodes and in the
72860Sstevel@tonic-gate  * driver's .conf file.
72870Sstevel@tonic-gate  *
72880Sstevel@tonic-gate  * NOTE: This facility is for compatibility purposes only and will
72890Sstevel@tonic-gate  *	eventually go away. Its usage is strongly discouraged.
72900Sstevel@tonic-gate  */
72910Sstevel@tonic-gate static void
enter_driver(struct devnames * dnp)72920Sstevel@tonic-gate enter_driver(struct devnames *dnp)
72930Sstevel@tonic-gate {
72940Sstevel@tonic-gate 	mutex_enter(&dnp->dn_lock);
72950Sstevel@tonic-gate 	ASSERT(dnp->dn_busy_thread != curthread);
72960Sstevel@tonic-gate 	while (dnp->dn_flags & DN_DRIVER_BUSY)
72970Sstevel@tonic-gate 		cv_wait(&dnp->dn_wait, &dnp->dn_lock);
72980Sstevel@tonic-gate 	dnp->dn_flags |= DN_DRIVER_BUSY;
72990Sstevel@tonic-gate 	dnp->dn_busy_thread = curthread;
73000Sstevel@tonic-gate 	mutex_exit(&dnp->dn_lock);
73010Sstevel@tonic-gate }
73020Sstevel@tonic-gate 
73030Sstevel@tonic-gate static void
exit_driver(struct devnames * dnp)73040Sstevel@tonic-gate exit_driver(struct devnames *dnp)
73050Sstevel@tonic-gate {
73060Sstevel@tonic-gate 	mutex_enter(&dnp->dn_lock);
73070Sstevel@tonic-gate 	ASSERT(dnp->dn_busy_thread == curthread);
73080Sstevel@tonic-gate 	dnp->dn_flags &= ~DN_DRIVER_BUSY;
73090Sstevel@tonic-gate 	dnp->dn_busy_thread = NULL;
73100Sstevel@tonic-gate 	cv_broadcast(&dnp->dn_wait);
73110Sstevel@tonic-gate 	mutex_exit(&dnp->dn_lock);
73120Sstevel@tonic-gate }
73130Sstevel@tonic-gate 
73140Sstevel@tonic-gate struct dev_ops *
ddi_hold_installed_driver(major_t major)73150Sstevel@tonic-gate ddi_hold_installed_driver(major_t major)
73160Sstevel@tonic-gate {
73170Sstevel@tonic-gate 	struct dev_ops *ops;
73180Sstevel@tonic-gate 	struct devnames *dnp;
73190Sstevel@tonic-gate 	char *parents;
73200Sstevel@tonic-gate 	int error;
73210Sstevel@tonic-gate 
73220Sstevel@tonic-gate 	ops = ddi_hold_driver(major);
73230Sstevel@tonic-gate 	if (ops == NULL)
73240Sstevel@tonic-gate 		return (NULL);
73250Sstevel@tonic-gate 
73260Sstevel@tonic-gate 	/*
73270Sstevel@tonic-gate 	 * Return immediately if all the attach operations associated
73280Sstevel@tonic-gate 	 * with a ddi_hold_installed_driver() call have already been done.
73290Sstevel@tonic-gate 	 */
73300Sstevel@tonic-gate 	dnp = &devnamesp[major];
73310Sstevel@tonic-gate 	enter_driver(dnp);
733212588SJerry.Gilliam@Sun.COM 	ASSERT(driver_active(major));
733310842SJerry.Gilliam@Sun.COM 
73340Sstevel@tonic-gate 	if (dnp->dn_flags & DN_DRIVER_HELD) {
73350Sstevel@tonic-gate 		exit_driver(dnp);
73360Sstevel@tonic-gate 		if (i_ddi_devs_attached(major) == DDI_SUCCESS)
73370Sstevel@tonic-gate 			return (ops);
73380Sstevel@tonic-gate 		ddi_rele_driver(major);
73390Sstevel@tonic-gate 		return (NULL);
73400Sstevel@tonic-gate 	}
73410Sstevel@tonic-gate 
73420Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
73430Sstevel@tonic-gate 	dnp->dn_flags |= (DN_DRIVER_HELD | DN_NO_AUTODETACH);
73440Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
73450Sstevel@tonic-gate 
73460Sstevel@tonic-gate 	DCOMPATPRINTF((CE_CONT,
73470Sstevel@tonic-gate 	    "ddi_hold_installed_driver: %s\n", dnp->dn_name));
73480Sstevel@tonic-gate 
73490Sstevel@tonic-gate 	/*
73500Sstevel@tonic-gate 	 * When the driver has no .conf children, it is sufficient
73510Sstevel@tonic-gate 	 * to attach existing nodes in the device tree. Nodes not
73520Sstevel@tonic-gate 	 * enumerated by the OBP are not attached.
73530Sstevel@tonic-gate 	 */
73540Sstevel@tonic-gate 	if (dnp->dn_pl == NULL) {
73550Sstevel@tonic-gate 		if (attach_driver_nodes(major) == DDI_SUCCESS) {
73560Sstevel@tonic-gate 			exit_driver(dnp);
73570Sstevel@tonic-gate 			return (ops);
73580Sstevel@tonic-gate 		}
73590Sstevel@tonic-gate 		exit_driver(dnp);
73600Sstevel@tonic-gate 		ddi_rele_driver(major);
73610Sstevel@tonic-gate 		return (NULL);
73620Sstevel@tonic-gate 	}
73630Sstevel@tonic-gate 
73640Sstevel@tonic-gate 	/*
73650Sstevel@tonic-gate 	 * Driver has .conf nodes. We find all possible parents
73660Sstevel@tonic-gate 	 * and recursively all ddi_hold_installed_driver on the
73670Sstevel@tonic-gate 	 * parent driver; then we invoke ndi_config_driver()
73680Sstevel@tonic-gate 	 * on all possible parent node in parallel to speed up
73690Sstevel@tonic-gate 	 * performance.
73700Sstevel@tonic-gate 	 */
73710Sstevel@tonic-gate 	parents = kmem_zalloc(devcnt * sizeof (char), KM_SLEEP);
73720Sstevel@tonic-gate 
73730Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
73740Sstevel@tonic-gate 	/* find .conf parents */
73750Sstevel@tonic-gate 	(void) impl_parlist_to_major(dnp->dn_pl, parents);
73760Sstevel@tonic-gate 	/* find hw node parents */
73770Sstevel@tonic-gate 	diplist_to_parent_major(dnp->dn_head, parents);
73780Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
73790Sstevel@tonic-gate 
73800Sstevel@tonic-gate 	error = attach_driver_by_parent(major, parents);
73810Sstevel@tonic-gate 	kmem_free(parents, devcnt * sizeof (char));
73820Sstevel@tonic-gate 	if (error == DDI_SUCCESS) {
73830Sstevel@tonic-gate 		exit_driver(dnp);
73840Sstevel@tonic-gate 		return (ops);
73850Sstevel@tonic-gate 	}
73860Sstevel@tonic-gate 
73870Sstevel@tonic-gate 	exit_driver(dnp);
73880Sstevel@tonic-gate 	ddi_rele_driver(major);
73890Sstevel@tonic-gate 	return (NULL);
73900Sstevel@tonic-gate }
73910Sstevel@tonic-gate 
73920Sstevel@tonic-gate /*
73930Sstevel@tonic-gate  * Default bus_config entry point for nexus drivers
73940Sstevel@tonic-gate  */
73950Sstevel@tonic-gate int
ndi_busop_bus_config(dev_info_t * pdip,uint_t flags,ddi_bus_config_op_t op,void * arg,dev_info_t ** child,clock_t timeout)73960Sstevel@tonic-gate ndi_busop_bus_config(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
73970Sstevel@tonic-gate     void *arg, dev_info_t **child, clock_t timeout)
73980Sstevel@tonic-gate {
73990Sstevel@tonic-gate 	major_t major;
74000Sstevel@tonic-gate 
74010Sstevel@tonic-gate 	/*
74020Sstevel@tonic-gate 	 * A timeout of 30 minutes or more is probably a mistake
74030Sstevel@tonic-gate 	 * This is intended to catch uses where timeout is in
74040Sstevel@tonic-gate 	 * the wrong units.  timeout must be in units of ticks.
74050Sstevel@tonic-gate 	 */
74060Sstevel@tonic-gate 	ASSERT(timeout < SEC_TO_TICK(1800));
74070Sstevel@tonic-gate 
74087009Scth 	major = DDI_MAJOR_T_NONE;
74090Sstevel@tonic-gate 	switch (op) {
74100Sstevel@tonic-gate 	case BUS_CONFIG_ONE:
74110Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config %s timeout=%ld\n",
74124950Scth 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
74134950Scth 		    (char *)arg, timeout));
74140Sstevel@tonic-gate 		return (devi_config_one(pdip, (char *)arg, child, flags,
74150Sstevel@tonic-gate 		    timeout));
74160Sstevel@tonic-gate 
74170Sstevel@tonic-gate 	case BUS_CONFIG_DRIVER:
74180Sstevel@tonic-gate 		major = (major_t)(uintptr_t)arg;
74190Sstevel@tonic-gate 		/*FALLTHROUGH*/
74200Sstevel@tonic-gate 	case BUS_CONFIG_ALL:
74210Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config timeout=%ld\n",
74224950Scth 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
74234950Scth 		    timeout));
74240Sstevel@tonic-gate 		if (timeout > 0) {
74250Sstevel@tonic-gate 			NDI_DEBUG(flags, (CE_CONT,
74260Sstevel@tonic-gate 			    "%s%d: bus config all timeout=%ld\n",
74270Sstevel@tonic-gate 			    ddi_driver_name(pdip), ddi_get_instance(pdip),
74280Sstevel@tonic-gate 			    timeout));
74290Sstevel@tonic-gate 			delay(timeout);
74300Sstevel@tonic-gate 		}
74310Sstevel@tonic-gate 		return (config_immediate_children(pdip, flags, major));
74320Sstevel@tonic-gate 
74330Sstevel@tonic-gate 	default:
74340Sstevel@tonic-gate 		return (NDI_FAILURE);
74350Sstevel@tonic-gate 	}
74360Sstevel@tonic-gate 	/*NOTREACHED*/
74370Sstevel@tonic-gate }
74380Sstevel@tonic-gate 
74390Sstevel@tonic-gate /*
74400Sstevel@tonic-gate  * Default busop bus_unconfig handler for nexus drivers
74410Sstevel@tonic-gate  */
74420Sstevel@tonic-gate int
ndi_busop_bus_unconfig(dev_info_t * pdip,uint_t flags,ddi_bus_config_op_t op,void * arg)74430Sstevel@tonic-gate ndi_busop_bus_unconfig(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
74440Sstevel@tonic-gate     void *arg)
74450Sstevel@tonic-gate {
74460Sstevel@tonic-gate 	major_t major;
74470Sstevel@tonic-gate 
74487009Scth 	major = DDI_MAJOR_T_NONE;
74490Sstevel@tonic-gate 	switch (op) {
74500Sstevel@tonic-gate 	case BUS_UNCONFIG_ONE:
74510Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig %s\n",
74520Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
74530Sstevel@tonic-gate 		    (char *)arg));
74540Sstevel@tonic-gate 		return (devi_unconfig_one(pdip, (char *)arg, flags));
74550Sstevel@tonic-gate 
74560Sstevel@tonic-gate 	case BUS_UNCONFIG_DRIVER:
74570Sstevel@tonic-gate 		major = (major_t)(uintptr_t)arg;
74580Sstevel@tonic-gate 		/*FALLTHROUGH*/
74590Sstevel@tonic-gate 	case BUS_UNCONFIG_ALL:
74600Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig all\n",
74610Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip)));
74620Sstevel@tonic-gate 		return (unconfig_immediate_children(pdip, NULL, flags, major));
74630Sstevel@tonic-gate 
74640Sstevel@tonic-gate 	default:
74650Sstevel@tonic-gate 		return (NDI_FAILURE);
74660Sstevel@tonic-gate 	}
74670Sstevel@tonic-gate 	/*NOTREACHED*/
74680Sstevel@tonic-gate }
74690Sstevel@tonic-gate 
74700Sstevel@tonic-gate /*
74710Sstevel@tonic-gate  * dummy functions to be removed
74720Sstevel@tonic-gate  */
74730Sstevel@tonic-gate void
impl_rem_dev_props(dev_info_t * dip)74740Sstevel@tonic-gate impl_rem_dev_props(dev_info_t *dip)
74750Sstevel@tonic-gate {
74760Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip))
74770Sstevel@tonic-gate 	/* do nothing */
74780Sstevel@tonic-gate }
74790Sstevel@tonic-gate 
74800Sstevel@tonic-gate /*
74810Sstevel@tonic-gate  * Determine if a node is a leaf node. If not sure, return false (0).
74820Sstevel@tonic-gate  */
74830Sstevel@tonic-gate static int
is_leaf_node(dev_info_t * dip)74840Sstevel@tonic-gate is_leaf_node(dev_info_t *dip)
74850Sstevel@tonic-gate {
74860Sstevel@tonic-gate 	major_t major = ddi_driver_major(dip);
74870Sstevel@tonic-gate 
74887009Scth 	if (major == DDI_MAJOR_T_NONE)
74890Sstevel@tonic-gate 		return (0);
74900Sstevel@tonic-gate 
74910Sstevel@tonic-gate 	return (devnamesp[major].dn_flags & DN_LEAF_DRIVER);
74920Sstevel@tonic-gate }
74930Sstevel@tonic-gate 
74940Sstevel@tonic-gate /*
74950Sstevel@tonic-gate  * Multithreaded [un]configuration
74960Sstevel@tonic-gate  */
74970Sstevel@tonic-gate static struct mt_config_handle *
mt_config_init(dev_info_t * pdip,dev_info_t ** dipp,int flags,major_t major,int op,struct brevq_node ** brevqp)74980Sstevel@tonic-gate mt_config_init(dev_info_t *pdip, dev_info_t **dipp, int flags,
74990Sstevel@tonic-gate     major_t major, int op, struct brevq_node **brevqp)
75000Sstevel@tonic-gate {
75010Sstevel@tonic-gate 	struct mt_config_handle	*hdl = kmem_alloc(sizeof (*hdl), KM_SLEEP);
75020Sstevel@tonic-gate 
75030Sstevel@tonic-gate 	mutex_init(&hdl->mtc_lock, NULL, MUTEX_DEFAULT, NULL);
75040Sstevel@tonic-gate 	cv_init(&hdl->mtc_cv, NULL, CV_DEFAULT, NULL);
75050Sstevel@tonic-gate 	hdl->mtc_pdip = pdip;
75060Sstevel@tonic-gate 	hdl->mtc_fdip = dipp;
75077009Scth 	hdl->mtc_parmajor = DDI_MAJOR_T_NONE;
75080Sstevel@tonic-gate 	hdl->mtc_flags = flags;
75090Sstevel@tonic-gate 	hdl->mtc_major = major;
75100Sstevel@tonic-gate 	hdl->mtc_thr_count = 0;
75110Sstevel@tonic-gate 	hdl->mtc_op = op;
75120Sstevel@tonic-gate 	hdl->mtc_error = 0;
75130Sstevel@tonic-gate 	hdl->mtc_brevqp = brevqp;
75140Sstevel@tonic-gate 
75150Sstevel@tonic-gate #ifdef DEBUG
75160Sstevel@tonic-gate 	gethrestime(&hdl->start_time);
75170Sstevel@tonic-gate 	hdl->total_time = 0;
75180Sstevel@tonic-gate #endif /* DEBUG */
75190Sstevel@tonic-gate 
75200Sstevel@tonic-gate 	return (hdl);
75210Sstevel@tonic-gate }
75220Sstevel@tonic-gate 
75230Sstevel@tonic-gate #ifdef DEBUG
75240Sstevel@tonic-gate static int
time_diff_in_msec(timestruc_t start,timestruc_t end)75250Sstevel@tonic-gate time_diff_in_msec(timestruc_t start, timestruc_t end)
75260Sstevel@tonic-gate {
75270Sstevel@tonic-gate 	int	nsec, sec;
75280Sstevel@tonic-gate 
75290Sstevel@tonic-gate 	sec = end.tv_sec - start.tv_sec;
75300Sstevel@tonic-gate 	nsec = end.tv_nsec - start.tv_nsec;
75310Sstevel@tonic-gate 	if (nsec < 0) {
75320Sstevel@tonic-gate 		nsec += NANOSEC;
75330Sstevel@tonic-gate 		sec -= 1;
75340Sstevel@tonic-gate 	}
75350Sstevel@tonic-gate 
75360Sstevel@tonic-gate 	return (sec * (NANOSEC >> 20) + (nsec >> 20));
75370Sstevel@tonic-gate }
75380Sstevel@tonic-gate 
75390Sstevel@tonic-gate #endif	/* DEBUG */
75400Sstevel@tonic-gate 
75410Sstevel@tonic-gate static int
mt_config_fini(struct mt_config_handle * hdl)75420Sstevel@tonic-gate mt_config_fini(struct mt_config_handle *hdl)
75430Sstevel@tonic-gate {
75440Sstevel@tonic-gate 	int		rv;
75450Sstevel@tonic-gate #ifdef DEBUG
75460Sstevel@tonic-gate 	int		real_time;
75470Sstevel@tonic-gate 	timestruc_t	end_time;
75480Sstevel@tonic-gate #endif /* DEBUG */
75490Sstevel@tonic-gate 
75500Sstevel@tonic-gate 	mutex_enter(&hdl->mtc_lock);
75510Sstevel@tonic-gate 	while (hdl->mtc_thr_count > 0)
75520Sstevel@tonic-gate 		cv_wait(&hdl->mtc_cv, &hdl->mtc_lock);
75530Sstevel@tonic-gate 	rv = hdl->mtc_error;
75540Sstevel@tonic-gate 	mutex_exit(&hdl->mtc_lock);
75550Sstevel@tonic-gate 
75560Sstevel@tonic-gate #ifdef DEBUG
75570Sstevel@tonic-gate 	gethrestime(&end_time);
75580Sstevel@tonic-gate 	real_time = time_diff_in_msec(hdl->start_time, end_time);
75590Sstevel@tonic-gate 	if ((ddidebug & DDI_MTCONFIG) && hdl->mtc_pdip)
75600Sstevel@tonic-gate 		cmn_err(CE_NOTE,
75610Sstevel@tonic-gate 		    "config %s%d: total time %d msec, real time %d msec",
75624950Scth 		    ddi_driver_name(hdl->mtc_pdip),
75634950Scth 		    ddi_get_instance(hdl->mtc_pdip),
75644950Scth 		    hdl->total_time, real_time);
75650Sstevel@tonic-gate #endif /* DEBUG */
75660Sstevel@tonic-gate 
75670Sstevel@tonic-gate 	cv_destroy(&hdl->mtc_cv);
75680Sstevel@tonic-gate 	mutex_destroy(&hdl->mtc_lock);
75690Sstevel@tonic-gate 	kmem_free(hdl, sizeof (*hdl));
75700Sstevel@tonic-gate 
75710Sstevel@tonic-gate 	return (rv);
75720Sstevel@tonic-gate }
75730Sstevel@tonic-gate 
75740Sstevel@tonic-gate struct mt_config_data {
75750Sstevel@tonic-gate 	struct mt_config_handle	*mtc_hdl;
75760Sstevel@tonic-gate 	dev_info_t		*mtc_dip;
75770Sstevel@tonic-gate 	major_t			mtc_major;
75780Sstevel@tonic-gate 	int			mtc_flags;
75790Sstevel@tonic-gate 	struct brevq_node	*mtc_brn;
75800Sstevel@tonic-gate 	struct mt_config_data	*mtc_next;
75810Sstevel@tonic-gate };
75820Sstevel@tonic-gate 
75830Sstevel@tonic-gate static void
mt_config_thread(void * arg)75840Sstevel@tonic-gate mt_config_thread(void *arg)
75850Sstevel@tonic-gate {
75860Sstevel@tonic-gate 	struct mt_config_data	*mcd = (struct mt_config_data *)arg;
75870Sstevel@tonic-gate 	struct mt_config_handle	*hdl = mcd->mtc_hdl;
75880Sstevel@tonic-gate 	dev_info_t		*dip = mcd->mtc_dip;
75890Sstevel@tonic-gate 	dev_info_t		*rdip, **dipp;
75900Sstevel@tonic-gate 	major_t			major = mcd->mtc_major;
75910Sstevel@tonic-gate 	int			flags = mcd->mtc_flags;
75920Sstevel@tonic-gate 	int			rv = 0;
75930Sstevel@tonic-gate 
75940Sstevel@tonic-gate #ifdef DEBUG
75950Sstevel@tonic-gate 	timestruc_t start_time, end_time;
75960Sstevel@tonic-gate 	gethrestime(&start_time);
75970Sstevel@tonic-gate #endif /* DEBUG */
75980Sstevel@tonic-gate 
75990Sstevel@tonic-gate 	rdip = NULL;
76000Sstevel@tonic-gate 	dipp = hdl->mtc_fdip ? &rdip : NULL;
76010Sstevel@tonic-gate 
76020Sstevel@tonic-gate 	switch (hdl->mtc_op) {
76030Sstevel@tonic-gate 	case MT_CONFIG_OP:
76040Sstevel@tonic-gate 		rv = devi_config_common(dip, flags, major);
76050Sstevel@tonic-gate 		break;
76060Sstevel@tonic-gate 	case MT_UNCONFIG_OP:
76070Sstevel@tonic-gate 		if (mcd->mtc_brn) {
76080Sstevel@tonic-gate 			struct brevq_node *brevq = NULL;
76090Sstevel@tonic-gate 			rv = devi_unconfig_common(dip, dipp, flags, major,
76100Sstevel@tonic-gate 			    &brevq);
76111317Scth 			mcd->mtc_brn->brn_child = brevq;
76120Sstevel@tonic-gate 		} else
76130Sstevel@tonic-gate 			rv = devi_unconfig_common(dip, dipp, flags, major,
76140Sstevel@tonic-gate 			    NULL);
76150Sstevel@tonic-gate 		break;
76160Sstevel@tonic-gate 	}
76170Sstevel@tonic-gate 
76180Sstevel@tonic-gate 	mutex_enter(&hdl->mtc_lock);
76190Sstevel@tonic-gate #ifdef DEBUG
76200Sstevel@tonic-gate 	gethrestime(&end_time);
76210Sstevel@tonic-gate 	hdl->total_time += time_diff_in_msec(start_time, end_time);
76220Sstevel@tonic-gate #endif /* DEBUG */
76232155Scth 
76242155Scth 	if ((rv != NDI_SUCCESS) && (hdl->mtc_error == 0)) {
76250Sstevel@tonic-gate 		hdl->mtc_error = rv;
76262155Scth #ifdef	DEBUG
76277009Scth 		if ((ddidebug & DDI_DEBUG) && (major != DDI_MAJOR_T_NONE)) {
76282155Scth 			char	*path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
76292155Scth 
76302155Scth 			(void) ddi_pathname(dip, path);
76312155Scth 			cmn_err(CE_NOTE, "mt_config_thread: "
76322155Scth 			    "op %d.%d.%x at %s failed %d",
76332155Scth 			    hdl->mtc_op, major, flags, path, rv);
76342155Scth 			kmem_free(path, MAXPATHLEN);
76352155Scth 		}
76362155Scth #endif	/* DEBUG */
76372155Scth 	}
76382155Scth 
76390Sstevel@tonic-gate 	if (hdl->mtc_fdip && *hdl->mtc_fdip == NULL) {
76400Sstevel@tonic-gate 		*hdl->mtc_fdip = rdip;
76410Sstevel@tonic-gate 		rdip = NULL;
76420Sstevel@tonic-gate 	}
76430Sstevel@tonic-gate 
76440Sstevel@tonic-gate 	if (rdip) {
76450Sstevel@tonic-gate 		ASSERT(rv != NDI_SUCCESS);
76460Sstevel@tonic-gate 		ndi_rele_devi(rdip);
76470Sstevel@tonic-gate 	}
76480Sstevel@tonic-gate 
76490Sstevel@tonic-gate 	ndi_rele_devi(dip);
76501826Sbm42561 
76511826Sbm42561 	if (--hdl->mtc_thr_count == 0)
76521826Sbm42561 		cv_broadcast(&hdl->mtc_cv);
76531826Sbm42561 	mutex_exit(&hdl->mtc_lock);
76540Sstevel@tonic-gate 	kmem_free(mcd, sizeof (*mcd));
76550Sstevel@tonic-gate }
76560Sstevel@tonic-gate 
76570Sstevel@tonic-gate /*
76580Sstevel@tonic-gate  * Multi-threaded config/unconfig of child nexus
76590Sstevel@tonic-gate  */
76600Sstevel@tonic-gate static void
mt_config_children(struct mt_config_handle * hdl)76610Sstevel@tonic-gate mt_config_children(struct mt_config_handle *hdl)
76620Sstevel@tonic-gate {
76630Sstevel@tonic-gate 	dev_info_t		*pdip = hdl->mtc_pdip;
76640Sstevel@tonic-gate 	major_t			major = hdl->mtc_major;
76650Sstevel@tonic-gate 	dev_info_t		*dip;
76660Sstevel@tonic-gate 	int			circ;
76671317Scth 	struct brevq_node	*brn;
76680Sstevel@tonic-gate 	struct mt_config_data	*mcd_head = NULL;
76690Sstevel@tonic-gate 	struct mt_config_data	*mcd_tail = NULL;
76700Sstevel@tonic-gate 	struct mt_config_data	*mcd;
76710Sstevel@tonic-gate #ifdef DEBUG
76720Sstevel@tonic-gate 	timestruc_t		end_time;
76730Sstevel@tonic-gate 
76740Sstevel@tonic-gate 	/* Update total_time in handle */
76750Sstevel@tonic-gate 	gethrestime(&end_time);
76760Sstevel@tonic-gate 	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
76770Sstevel@tonic-gate #endif
76780Sstevel@tonic-gate 
76790Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
76800Sstevel@tonic-gate 	dip = ddi_get_child(pdip);
76810Sstevel@tonic-gate 	while (dip) {
76820Sstevel@tonic-gate 		if (hdl->mtc_op == MT_UNCONFIG_OP && hdl->mtc_brevqp &&
76830Sstevel@tonic-gate 		    !(DEVI_EVREMOVE(dip)) &&
76840Sstevel@tonic-gate 		    i_ddi_node_state(dip) >= DS_INITIALIZED) {
76850Sstevel@tonic-gate 			/*
76860Sstevel@tonic-gate 			 * Enqueue this dip's deviname.
76870Sstevel@tonic-gate 			 * No need to hold a lock while enqueuing since this
76880Sstevel@tonic-gate 			 * is the only thread doing the enqueue and no one
76890Sstevel@tonic-gate 			 * walks the queue while we are in multithreaded
76900Sstevel@tonic-gate 			 * unconfiguration.
76910Sstevel@tonic-gate 			 */
76920Sstevel@tonic-gate 			brn = brevq_enqueue(hdl->mtc_brevqp, dip, NULL);
76931317Scth 		} else
76941317Scth 			brn = NULL;
76950Sstevel@tonic-gate 
76960Sstevel@tonic-gate 		/*
76970Sstevel@tonic-gate 		 * Hold the child that we are processing so he does not get
76980Sstevel@tonic-gate 		 * removed. The corrisponding ndi_rele_devi() for children
76990Sstevel@tonic-gate 		 * that are not being skipped is done at the end of
77000Sstevel@tonic-gate 		 * mt_config_thread().
77010Sstevel@tonic-gate 		 */
77020Sstevel@tonic-gate 		ndi_hold_devi(dip);
77030Sstevel@tonic-gate 
77040Sstevel@tonic-gate 		/*
77050Sstevel@tonic-gate 		 * skip leaf nodes and (for configure) nodes not
77060Sstevel@tonic-gate 		 * fully attached.
77070Sstevel@tonic-gate 		 */
77080Sstevel@tonic-gate 		if (is_leaf_node(dip) ||
77090Sstevel@tonic-gate 		    (hdl->mtc_op == MT_CONFIG_OP &&
77100Sstevel@tonic-gate 		    i_ddi_node_state(dip) < DS_READY)) {
77110Sstevel@tonic-gate 			ndi_rele_devi(dip);
77120Sstevel@tonic-gate 			dip = ddi_get_next_sibling(dip);
77130Sstevel@tonic-gate 			continue;
77140Sstevel@tonic-gate 		}
77150Sstevel@tonic-gate 
77160Sstevel@tonic-gate 		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
77170Sstevel@tonic-gate 		mcd->mtc_dip = dip;
77180Sstevel@tonic-gate 		mcd->mtc_hdl = hdl;
77190Sstevel@tonic-gate 		mcd->mtc_brn = brn;
77200Sstevel@tonic-gate 
77210Sstevel@tonic-gate 		/*
77220Sstevel@tonic-gate 		 * Switch a 'driver' operation to an 'all' operation below a
77230Sstevel@tonic-gate 		 * node bound to the driver.
77240Sstevel@tonic-gate 		 */
77257009Scth 		if ((major == DDI_MAJOR_T_NONE) ||
77267009Scth 		    (major == ddi_driver_major(dip)))
77277009Scth 			mcd->mtc_major = DDI_MAJOR_T_NONE;
77280Sstevel@tonic-gate 		else
77290Sstevel@tonic-gate 			mcd->mtc_major = major;
77300Sstevel@tonic-gate 
77310Sstevel@tonic-gate 		/*
77320Sstevel@tonic-gate 		 * The unconfig-driver to unconfig-all conversion above
77330Sstevel@tonic-gate 		 * constitutes an autodetach for NDI_DETACH_DRIVER calls,
77340Sstevel@tonic-gate 		 * set NDI_AUTODETACH.
77350Sstevel@tonic-gate 		 */
77360Sstevel@tonic-gate 		mcd->mtc_flags = hdl->mtc_flags;
77370Sstevel@tonic-gate 		if ((mcd->mtc_flags & NDI_DETACH_DRIVER) &&
77380Sstevel@tonic-gate 		    (hdl->mtc_op == MT_UNCONFIG_OP) &&
77390Sstevel@tonic-gate 		    (major == ddi_driver_major(pdip)))
77400Sstevel@tonic-gate 			mcd->mtc_flags |= NDI_AUTODETACH;
77410Sstevel@tonic-gate 
77420Sstevel@tonic-gate 		mutex_enter(&hdl->mtc_lock);
77430Sstevel@tonic-gate 		hdl->mtc_thr_count++;
77440Sstevel@tonic-gate 		mutex_exit(&hdl->mtc_lock);
77450Sstevel@tonic-gate 
77460Sstevel@tonic-gate 		/*
77470Sstevel@tonic-gate 		 * Add to end of list to process after ndi_devi_exit to avoid
77480Sstevel@tonic-gate 		 * locking differences depending on value of mtc_off.
77490Sstevel@tonic-gate 		 */
77500Sstevel@tonic-gate 		mcd->mtc_next = NULL;
77510Sstevel@tonic-gate 		if (mcd_head == NULL)
77520Sstevel@tonic-gate 			mcd_head = mcd;
77530Sstevel@tonic-gate 		else
77540Sstevel@tonic-gate 			mcd_tail->mtc_next = mcd;
77550Sstevel@tonic-gate 		mcd_tail = mcd;
77560Sstevel@tonic-gate 
77570Sstevel@tonic-gate 		dip = ddi_get_next_sibling(dip);
77580Sstevel@tonic-gate 	}
77590Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
77600Sstevel@tonic-gate 
77610Sstevel@tonic-gate 	/* go through the list of held children */
77620Sstevel@tonic-gate 	for (mcd = mcd_head; mcd; mcd = mcd_head) {
77630Sstevel@tonic-gate 		mcd_head = mcd->mtc_next;
77642155Scth 		if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
77650Sstevel@tonic-gate 			mt_config_thread(mcd);
77660Sstevel@tonic-gate 		else
77670Sstevel@tonic-gate 			(void) thread_create(NULL, 0, mt_config_thread, mcd,
77680Sstevel@tonic-gate 			    0, &p0, TS_RUN, minclsyspri);
77690Sstevel@tonic-gate 	}
77700Sstevel@tonic-gate }
77710Sstevel@tonic-gate 
77720Sstevel@tonic-gate static void
mt_config_driver(struct mt_config_handle * hdl)77730Sstevel@tonic-gate mt_config_driver(struct mt_config_handle *hdl)
77740Sstevel@tonic-gate {
77750Sstevel@tonic-gate 	major_t			par_major = hdl->mtc_parmajor;
77760Sstevel@tonic-gate 	major_t			major = hdl->mtc_major;
77770Sstevel@tonic-gate 	struct devnames		*dnp = &devnamesp[par_major];
77780Sstevel@tonic-gate 	dev_info_t		*dip;
77790Sstevel@tonic-gate 	struct mt_config_data	*mcd_head = NULL;
77800Sstevel@tonic-gate 	struct mt_config_data	*mcd_tail = NULL;
77810Sstevel@tonic-gate 	struct mt_config_data	*mcd;
77820Sstevel@tonic-gate #ifdef DEBUG
77830Sstevel@tonic-gate 	timestruc_t		end_time;
77840Sstevel@tonic-gate 
77850Sstevel@tonic-gate 	/* Update total_time in handle */
77860Sstevel@tonic-gate 	gethrestime(&end_time);
77870Sstevel@tonic-gate 	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
77880Sstevel@tonic-gate #endif
77897009Scth 	ASSERT(par_major != DDI_MAJOR_T_NONE);
77907009Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
77910Sstevel@tonic-gate 
77920Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
77930Sstevel@tonic-gate 	dip = devnamesp[par_major].dn_head;
77940Sstevel@tonic-gate 	while (dip) {
77950Sstevel@tonic-gate 		/*
77960Sstevel@tonic-gate 		 * Hold the child that we are processing so he does not get
77970Sstevel@tonic-gate 		 * removed. The corrisponding ndi_rele_devi() for children
77980Sstevel@tonic-gate 		 * that are not being skipped is done at the end of
77990Sstevel@tonic-gate 		 * mt_config_thread().
78000Sstevel@tonic-gate 		 */
78010Sstevel@tonic-gate 		ndi_hold_devi(dip);
78020Sstevel@tonic-gate 
78030Sstevel@tonic-gate 		/* skip leaf nodes and nodes not fully attached */
78041333Scth 		if (!i_ddi_devi_attached(dip) || is_leaf_node(dip)) {
78050Sstevel@tonic-gate 			ndi_rele_devi(dip);
78060Sstevel@tonic-gate 			dip = ddi_get_next(dip);
78070Sstevel@tonic-gate 			continue;
78080Sstevel@tonic-gate 		}
78090Sstevel@tonic-gate 
78100Sstevel@tonic-gate 		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
78110Sstevel@tonic-gate 		mcd->mtc_dip = dip;
78120Sstevel@tonic-gate 		mcd->mtc_hdl = hdl;
78130Sstevel@tonic-gate 		mcd->mtc_major = major;
78140Sstevel@tonic-gate 		mcd->mtc_flags = hdl->mtc_flags;
78150Sstevel@tonic-gate 
78160Sstevel@tonic-gate 		mutex_enter(&hdl->mtc_lock);
78170Sstevel@tonic-gate 		hdl->mtc_thr_count++;
78180Sstevel@tonic-gate 		mutex_exit(&hdl->mtc_lock);
78190Sstevel@tonic-gate 
78200Sstevel@tonic-gate 		/*
78210Sstevel@tonic-gate 		 * Add to end of list to process after UNLOCK_DEV_OPS to avoid
78220Sstevel@tonic-gate 		 * locking differences depending on value of mtc_off.
78230Sstevel@tonic-gate 		 */
78240Sstevel@tonic-gate 		mcd->mtc_next = NULL;
78250Sstevel@tonic-gate 		if (mcd_head == NULL)
78260Sstevel@tonic-gate 			mcd_head = mcd;
78270Sstevel@tonic-gate 		else
78280Sstevel@tonic-gate 			mcd_tail->mtc_next = mcd;
78290Sstevel@tonic-gate 		mcd_tail = mcd;
78300Sstevel@tonic-gate 
78310Sstevel@tonic-gate 		dip = ddi_get_next(dip);
78320Sstevel@tonic-gate 	}
78330Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
78340Sstevel@tonic-gate 
78350Sstevel@tonic-gate 	/* go through the list of held children */
78360Sstevel@tonic-gate 	for (mcd = mcd_head; mcd; mcd = mcd_head) {
78370Sstevel@tonic-gate 		mcd_head = mcd->mtc_next;
78382155Scth 		if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
78390Sstevel@tonic-gate 			mt_config_thread(mcd);
78400Sstevel@tonic-gate 		else
78410Sstevel@tonic-gate 			(void) thread_create(NULL, 0, mt_config_thread, mcd,
78420Sstevel@tonic-gate 			    0, &p0, TS_RUN, minclsyspri);
78430Sstevel@tonic-gate 	}
78440Sstevel@tonic-gate }
78450Sstevel@tonic-gate 
78460Sstevel@tonic-gate /*
78470Sstevel@tonic-gate  * Given the nodeid for a persistent (PROM or SID) node, return
78480Sstevel@tonic-gate  * the corresponding devinfo node
78490Sstevel@tonic-gate  * NOTE: This function will return NULL for .conf nodeids.
78500Sstevel@tonic-gate  */
78510Sstevel@tonic-gate dev_info_t *
e_ddi_nodeid_to_dip(pnode_t nodeid)7852789Sahrens e_ddi_nodeid_to_dip(pnode_t nodeid)
78530Sstevel@tonic-gate {
78540Sstevel@tonic-gate 	dev_info_t		*dip = NULL;
78550Sstevel@tonic-gate 	struct devi_nodeid	*prev, *elem;
78560Sstevel@tonic-gate 
78570Sstevel@tonic-gate 	mutex_enter(&devimap->dno_lock);
78580Sstevel@tonic-gate 
78590Sstevel@tonic-gate 	prev = NULL;
78600Sstevel@tonic-gate 	for (elem = devimap->dno_head; elem; elem = elem->next) {
78610Sstevel@tonic-gate 		if (elem->nodeid == nodeid) {
78620Sstevel@tonic-gate 			ndi_hold_devi(elem->dip);
78630Sstevel@tonic-gate 			dip = elem->dip;
78640Sstevel@tonic-gate 			break;
78650Sstevel@tonic-gate 		}
78660Sstevel@tonic-gate 		prev = elem;
78670Sstevel@tonic-gate 	}
78680Sstevel@tonic-gate 
78690Sstevel@tonic-gate 	/*
78700Sstevel@tonic-gate 	 * Move to head for faster lookup next time
78710Sstevel@tonic-gate 	 */
78720Sstevel@tonic-gate 	if (elem && prev) {
78730Sstevel@tonic-gate 		prev->next = elem->next;
78740Sstevel@tonic-gate 		elem->next = devimap->dno_head;
78750Sstevel@tonic-gate 		devimap->dno_head = elem;
78760Sstevel@tonic-gate 	}
78770Sstevel@tonic-gate 
78780Sstevel@tonic-gate 	mutex_exit(&devimap->dno_lock);
78790Sstevel@tonic-gate 	return (dip);
78800Sstevel@tonic-gate }
78810Sstevel@tonic-gate 
78820Sstevel@tonic-gate static void
free_cache_task(void * arg)78830Sstevel@tonic-gate free_cache_task(void *arg)
78840Sstevel@tonic-gate {
78850Sstevel@tonic-gate 	ASSERT(arg == NULL);
78860Sstevel@tonic-gate 
78870Sstevel@tonic-gate 	mutex_enter(&di_cache.cache_lock);
78880Sstevel@tonic-gate 
78890Sstevel@tonic-gate 	/*
78900Sstevel@tonic-gate 	 * The cache can be invalidated without holding the lock
78910Sstevel@tonic-gate 	 * but it can be made valid again only while the lock is held.
78920Sstevel@tonic-gate 	 * So if the cache is invalid when the lock is held, it will
78930Sstevel@tonic-gate 	 * stay invalid until lock is released.
78940Sstevel@tonic-gate 	 */
78950Sstevel@tonic-gate 	if (!di_cache.cache_valid)
78960Sstevel@tonic-gate 		i_ddi_di_cache_free(&di_cache);
78970Sstevel@tonic-gate 
78980Sstevel@tonic-gate 	mutex_exit(&di_cache.cache_lock);
78990Sstevel@tonic-gate 
79000Sstevel@tonic-gate 	if (di_cache_debug)
79010Sstevel@tonic-gate 		cmn_err(CE_NOTE, "system_taskq: di_cache freed");
79020Sstevel@tonic-gate }
79030Sstevel@tonic-gate 
79040Sstevel@tonic-gate extern int modrootloaded;
79050Sstevel@tonic-gate 
79060Sstevel@tonic-gate void
i_ddi_di_cache_free(struct di_cache * cache)79070Sstevel@tonic-gate i_ddi_di_cache_free(struct di_cache *cache)
79080Sstevel@tonic-gate {
79090Sstevel@tonic-gate 	int	error;
79107656SSherry.Moore@Sun.COM 	extern int sys_shutdown;
79110Sstevel@tonic-gate 
79120Sstevel@tonic-gate 	ASSERT(mutex_owned(&cache->cache_lock));
79130Sstevel@tonic-gate 
79140Sstevel@tonic-gate 	if (cache->cache_size) {
79150Sstevel@tonic-gate 		ASSERT(cache->cache_size > 0);
79160Sstevel@tonic-gate 		ASSERT(cache->cache_data);
79170Sstevel@tonic-gate 
79180Sstevel@tonic-gate 		kmem_free(cache->cache_data, cache->cache_size);
79190Sstevel@tonic-gate 		cache->cache_data = NULL;
79200Sstevel@tonic-gate 		cache->cache_size = 0;
79210Sstevel@tonic-gate 
79220Sstevel@tonic-gate 		if (di_cache_debug)
79230Sstevel@tonic-gate 			cmn_err(CE_NOTE, "i_ddi_di_cache_free: freed cachemem");
79240Sstevel@tonic-gate 	} else {
79250Sstevel@tonic-gate 		ASSERT(cache->cache_data == NULL);
79260Sstevel@tonic-gate 		if (di_cache_debug)
79270Sstevel@tonic-gate 			cmn_err(CE_NOTE, "i_ddi_di_cache_free: NULL cache");
79280Sstevel@tonic-gate 	}
79290Sstevel@tonic-gate 
79307576SJerry.Gilliam@Sun.COM 	if (!modrootloaded || rootvp == NULL ||
79317576SJerry.Gilliam@Sun.COM 	    vn_is_readonly(rootvp) || sys_shutdown) {
79320Sstevel@tonic-gate 		if (di_cache_debug) {
79330Sstevel@tonic-gate 			cmn_err(CE_WARN, "/ not mounted/RDONLY. Skip unlink");
79340Sstevel@tonic-gate 		}
79350Sstevel@tonic-gate 		return;
79360Sstevel@tonic-gate 	}
79370Sstevel@tonic-gate 
79380Sstevel@tonic-gate 	error = vn_remove(DI_CACHE_FILE, UIO_SYSSPACE, RMFILE);
79390Sstevel@tonic-gate 	if (di_cache_debug && error && error != ENOENT) {
79400Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: unlink failed: %d", DI_CACHE_FILE, error);
79410Sstevel@tonic-gate 	} else if (di_cache_debug && !error) {
79420Sstevel@tonic-gate 		cmn_err(CE_NOTE, "i_ddi_di_cache_free: unlinked cache file");
79430Sstevel@tonic-gate 	}
79440Sstevel@tonic-gate }
79450Sstevel@tonic-gate 
79460Sstevel@tonic-gate void
i_ddi_di_cache_invalidate()794710696SDavid.Hollister@Sun.COM i_ddi_di_cache_invalidate()
79480Sstevel@tonic-gate {
79496065Scth 	int	cache_valid;
79500Sstevel@tonic-gate 
79510Sstevel@tonic-gate 	if (!modrootloaded || !i_ddi_io_initialized()) {
79520Sstevel@tonic-gate 		if (di_cache_debug)
79530Sstevel@tonic-gate 			cmn_err(CE_NOTE, "I/O not inited. Skipping invalidate");
79540Sstevel@tonic-gate 		return;
79550Sstevel@tonic-gate 	}
79560Sstevel@tonic-gate 
79576065Scth 	/* Increment devtree generation number. */
79582621Sllai1 	atomic_inc_ulong(&devtree_gen);
79590Sstevel@tonic-gate 
79606065Scth 	/* Invalidate the in-core cache and dispatch free on valid->invalid */
79616065Scth 	cache_valid = atomic_swap_uint(&di_cache.cache_valid, 0);
79626065Scth 	if (cache_valid) {
796310696SDavid.Hollister@Sun.COM 		/*
796410696SDavid.Hollister@Sun.COM 		 * This is an optimization to start cleaning up a cached
796510696SDavid.Hollister@Sun.COM 		 * snapshot early.  For this reason, it is OK for
796610696SDavid.Hollister@Sun.COM 		 * taskq_dispatach to fail (and it is OK to not track calling
796710696SDavid.Hollister@Sun.COM 		 * context relative to sleep, and assume NOSLEEP).
796810696SDavid.Hollister@Sun.COM 		 */
79696065Scth 		(void) taskq_dispatch(system_taskq, free_cache_task, NULL,
797010696SDavid.Hollister@Sun.COM 		    TQ_NOSLEEP);
79716065Scth 	}
79720Sstevel@tonic-gate 
79730Sstevel@tonic-gate 	if (di_cache_debug) {
797410696SDavid.Hollister@Sun.COM 		cmn_err(CE_NOTE, "invalidation");
79750Sstevel@tonic-gate 	}
79760Sstevel@tonic-gate }
79770Sstevel@tonic-gate 
79780Sstevel@tonic-gate 
79790Sstevel@tonic-gate static void
i_bind_vhci_node(dev_info_t * dip)79800Sstevel@tonic-gate i_bind_vhci_node(dev_info_t *dip)
79810Sstevel@tonic-gate {
79824145Scth 	DEVI(dip)->devi_major = ddi_name_to_major(ddi_node_name(dip));
79830Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_BOUND);
79840Sstevel@tonic-gate }
79850Sstevel@tonic-gate 
79860Sstevel@tonic-gate static char vhci_node_addr[2];
79870Sstevel@tonic-gate 
79880Sstevel@tonic-gate static int
i_init_vhci_node(dev_info_t * dip)79890Sstevel@tonic-gate i_init_vhci_node(dev_info_t *dip)
79900Sstevel@tonic-gate {
79910Sstevel@tonic-gate 	add_global_props(dip);
79920Sstevel@tonic-gate 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
79930Sstevel@tonic-gate 	if (DEVI(dip)->devi_ops == NULL)
79940Sstevel@tonic-gate 		return (-1);
79950Sstevel@tonic-gate 
79960Sstevel@tonic-gate 	DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
79970Sstevel@tonic-gate 	e_ddi_keep_instance(dip);
79980Sstevel@tonic-gate 	vhci_node_addr[0]	= '\0';
79990Sstevel@tonic-gate 	ddi_set_name_addr(dip, vhci_node_addr);
80000Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_INITIALIZED);
80010Sstevel@tonic-gate 	return (0);
80020Sstevel@tonic-gate }
80030Sstevel@tonic-gate 
80040Sstevel@tonic-gate static void
i_link_vhci_node(dev_info_t * dip)80050Sstevel@tonic-gate i_link_vhci_node(dev_info_t *dip)
80060Sstevel@tonic-gate {
80071093Shiremath 	ASSERT(MUTEX_HELD(&global_vhci_lock));
80081093Shiremath 
80090Sstevel@tonic-gate 	/*
80100Sstevel@tonic-gate 	 * scsi_vhci should be kept left most of the device tree.
80110Sstevel@tonic-gate 	 */
80120Sstevel@tonic-gate 	if (scsi_vhci_dip) {
80130Sstevel@tonic-gate 		DEVI(dip)->devi_sibling = DEVI(scsi_vhci_dip)->devi_sibling;
80140Sstevel@tonic-gate 		DEVI(scsi_vhci_dip)->devi_sibling = DEVI(dip);
80150Sstevel@tonic-gate 	} else {
80160Sstevel@tonic-gate 		DEVI(dip)->devi_sibling = DEVI(top_devinfo)->devi_child;
80170Sstevel@tonic-gate 		DEVI(top_devinfo)->devi_child = DEVI(dip);
80180Sstevel@tonic-gate 	}
80190Sstevel@tonic-gate }
80200Sstevel@tonic-gate 
80210Sstevel@tonic-gate 
80220Sstevel@tonic-gate /*
80230Sstevel@tonic-gate  * This a special routine to enumerate vhci node (child of rootnex
80240Sstevel@tonic-gate  * node) without holding the ndi_devi_enter() lock. The device node
80250Sstevel@tonic-gate  * is allocated, initialized and brought into DS_READY state before
80260Sstevel@tonic-gate  * inserting into the device tree. The VHCI node is handcrafted
80270Sstevel@tonic-gate  * here to bring the node to DS_READY, similar to rootnex node.
80280Sstevel@tonic-gate  *
80290Sstevel@tonic-gate  * The global_vhci_lock protects linking the node into the device
80300Sstevel@tonic-gate  * as same lock is held before linking/unlinking any direct child
80310Sstevel@tonic-gate  * of rootnex children.
80320Sstevel@tonic-gate  *
80330Sstevel@tonic-gate  * This routine is a workaround to handle a possible deadlock
80340Sstevel@tonic-gate  * that occurs while trying to enumerate node in a different sub-tree
80350Sstevel@tonic-gate  * during _init/_attach entry points.
80360Sstevel@tonic-gate  */
80370Sstevel@tonic-gate /*ARGSUSED*/
80380Sstevel@tonic-gate dev_info_t *
ndi_devi_config_vhci(char * drvname,int flags)80390Sstevel@tonic-gate ndi_devi_config_vhci(char *drvname, int flags)
80400Sstevel@tonic-gate {
80410Sstevel@tonic-gate 	struct devnames		*dnp;
80420Sstevel@tonic-gate 	dev_info_t		*dip;
80430Sstevel@tonic-gate 	major_t			major = ddi_name_to_major(drvname);
80440Sstevel@tonic-gate 
80450Sstevel@tonic-gate 	if (major == -1)
80460Sstevel@tonic-gate 		return (NULL);
80470Sstevel@tonic-gate 
80480Sstevel@tonic-gate 	/* Make sure we create the VHCI node only once */
80490Sstevel@tonic-gate 	dnp = &devnamesp[major];
80500Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
80510Sstevel@tonic-gate 	if (dnp->dn_head) {
80520Sstevel@tonic-gate 		dip = dnp->dn_head;
80530Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
80540Sstevel@tonic-gate 		return (dip);
80550Sstevel@tonic-gate 	}
80560Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
80570Sstevel@tonic-gate 
80580Sstevel@tonic-gate 	/* Allocate the VHCI node */
80590Sstevel@tonic-gate 	ndi_devi_alloc_sleep(top_devinfo, drvname, DEVI_SID_NODEID, &dip);
80600Sstevel@tonic-gate 	ndi_hold_devi(dip);
80610Sstevel@tonic-gate 
80620Sstevel@tonic-gate 	/* Mark the node as VHCI */
80630Sstevel@tonic-gate 	DEVI(dip)->devi_node_attributes |= DDI_VHCI_NODE;
80640Sstevel@tonic-gate 
80650Sstevel@tonic-gate 	i_ddi_add_devimap(dip);
80660Sstevel@tonic-gate 	i_bind_vhci_node(dip);
80670Sstevel@tonic-gate 	if (i_init_vhci_node(dip) == -1) {
80680Sstevel@tonic-gate 		ndi_rele_devi(dip);
80690Sstevel@tonic-gate 		(void) ndi_devi_free(dip);
80700Sstevel@tonic-gate 		return (NULL);
80710Sstevel@tonic-gate 	}
80720Sstevel@tonic-gate 
8073495Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
80740Sstevel@tonic-gate 	DEVI_SET_ATTACHING(dip);
8075495Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
8076495Scth 
80770Sstevel@tonic-gate 	if (devi_attach(dip, DDI_ATTACH) != DDI_SUCCESS) {
80780Sstevel@tonic-gate 		cmn_err(CE_CONT, "Could not attach %s driver", drvname);
80790Sstevel@tonic-gate 		e_ddi_free_instance(dip, vhci_node_addr);
80800Sstevel@tonic-gate 		ndi_rele_devi(dip);
80810Sstevel@tonic-gate 		(void) ndi_devi_free(dip);
80820Sstevel@tonic-gate 		return (NULL);
80830Sstevel@tonic-gate 	}
8084495Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
80850Sstevel@tonic-gate 	DEVI_CLR_ATTACHING(dip);
8086495Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
80870Sstevel@tonic-gate 
80881093Shiremath 	mutex_enter(&global_vhci_lock);
80890Sstevel@tonic-gate 	i_link_vhci_node(dip);
80901093Shiremath 	mutex_exit(&global_vhci_lock);
80910Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_READY);
80920Sstevel@tonic-gate 
80930Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
80940Sstevel@tonic-gate 	dnp->dn_flags |= DN_DRIVER_HELD;
80950Sstevel@tonic-gate 	dnp->dn_head = dip;
80960Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
80970Sstevel@tonic-gate 
80980Sstevel@tonic-gate 	i_ndi_devi_report_status_change(dip, NULL);
80990Sstevel@tonic-gate 
81000Sstevel@tonic-gate 	return (dip);
81010Sstevel@tonic-gate }
81020Sstevel@tonic-gate 
81030Sstevel@tonic-gate /*
810410696SDavid.Hollister@Sun.COM  * Maintain DEVI_DEVICE_REMOVED hotplug devi_state for remove/reinsert hotplug
810510696SDavid.Hollister@Sun.COM  * of open devices. Currently, because of tight coupling between the devfs file
810610696SDavid.Hollister@Sun.COM  * system and the Solaris device tree, a driver can't always make the device
810710696SDavid.Hollister@Sun.COM  * tree state (esp devi_node_state) match device hardware hotplug state. Until
810810696SDavid.Hollister@Sun.COM  * resolved, to overcome this deficiency we use the following interfaces that
810910696SDavid.Hollister@Sun.COM  * maintain the DEVI_DEVICE_REMOVED devi_state status bit.  These interface
811010696SDavid.Hollister@Sun.COM  * report current state, and drive operation (like events and cache
811110696SDavid.Hollister@Sun.COM  * invalidation) when a driver changes remove/insert state of an open device.
811210696SDavid.Hollister@Sun.COM  *
811310696SDavid.Hollister@Sun.COM  * The ndi_devi_device_isremoved() returns 1 if the device is currently removed.
811410696SDavid.Hollister@Sun.COM  *
811510696SDavid.Hollister@Sun.COM  * The ndi_devi_device_remove() interface declares the device as removed, and
811610696SDavid.Hollister@Sun.COM  * returns 1 if there was a state change associated with this declaration.
811710696SDavid.Hollister@Sun.COM  *
811810696SDavid.Hollister@Sun.COM  * The ndi_devi_device_insert() declares the device as inserted, and returns 1
811910696SDavid.Hollister@Sun.COM  * if there was a state change associated with this declaration.
812010696SDavid.Hollister@Sun.COM  */
812110696SDavid.Hollister@Sun.COM int
ndi_devi_device_isremoved(dev_info_t * dip)812210696SDavid.Hollister@Sun.COM ndi_devi_device_isremoved(dev_info_t *dip)
812310696SDavid.Hollister@Sun.COM {
812410696SDavid.Hollister@Sun.COM 	return (DEVI_IS_DEVICE_REMOVED(dip));
812510696SDavid.Hollister@Sun.COM }
812610696SDavid.Hollister@Sun.COM 
812710696SDavid.Hollister@Sun.COM int
ndi_devi_device_remove(dev_info_t * dip)812810696SDavid.Hollister@Sun.COM ndi_devi_device_remove(dev_info_t *dip)
812910696SDavid.Hollister@Sun.COM {
813010696SDavid.Hollister@Sun.COM 	ASSERT(dip && ddi_get_parent(dip) &&
813110696SDavid.Hollister@Sun.COM 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
813210696SDavid.Hollister@Sun.COM 
813310696SDavid.Hollister@Sun.COM 	/* Return if already marked removed. */
813410696SDavid.Hollister@Sun.COM 	if (ndi_devi_device_isremoved(dip))
813510696SDavid.Hollister@Sun.COM 		return (0);
813610696SDavid.Hollister@Sun.COM 
813710696SDavid.Hollister@Sun.COM 	/* Mark the device as having been physically removed. */
813810696SDavid.Hollister@Sun.COM 	mutex_enter(&(DEVI(dip)->devi_lock));
813910696SDavid.Hollister@Sun.COM 	ndi_devi_set_hidden(dip);	/* invisible: lookup/snapshot */
814010696SDavid.Hollister@Sun.COM 	DEVI_SET_DEVICE_REMOVED(dip);
814110696SDavid.Hollister@Sun.COM 	DEVI_SET_EVREMOVE(dip);		/* this clears EVADD too */
814210696SDavid.Hollister@Sun.COM 	mutex_exit(&(DEVI(dip)->devi_lock));
814310696SDavid.Hollister@Sun.COM 
814410696SDavid.Hollister@Sun.COM 	/* report remove (as 'removed') */
814510696SDavid.Hollister@Sun.COM 	i_ndi_devi_report_status_change(dip, NULL);
814610696SDavid.Hollister@Sun.COM 
814710696SDavid.Hollister@Sun.COM 	/*
814810696SDavid.Hollister@Sun.COM 	 * Invalidate the cache to ensure accurate
814910696SDavid.Hollister@Sun.COM 	 * (di_state() & DI_DEVICE_REMOVED).
815010696SDavid.Hollister@Sun.COM 	 */
815110696SDavid.Hollister@Sun.COM 	i_ddi_di_cache_invalidate();
815210696SDavid.Hollister@Sun.COM 
815310696SDavid.Hollister@Sun.COM 	/*
815412288SChris.Horne@Sun.COM 	 * Generate sysevent for those interested in removal (either
815512288SChris.Horne@Sun.COM 	 * directly via private EC_DEVFS or indirectly via devfsadmd
815612288SChris.Horne@Sun.COM 	 * generated EC_DEV). This will generate LDI DEVICE_REMOVE
815712288SChris.Horne@Sun.COM 	 * event too.
815810696SDavid.Hollister@Sun.COM 	 */
815910696SDavid.Hollister@Sun.COM 	i_ddi_log_devfs_device_remove(dip);
816010696SDavid.Hollister@Sun.COM 
816110696SDavid.Hollister@Sun.COM 	return (1);		/* DEVICE_REMOVED state changed */
816210696SDavid.Hollister@Sun.COM }
816310696SDavid.Hollister@Sun.COM 
816410696SDavid.Hollister@Sun.COM int
ndi_devi_device_insert(dev_info_t * dip)816510696SDavid.Hollister@Sun.COM ndi_devi_device_insert(dev_info_t *dip)
816610696SDavid.Hollister@Sun.COM {
816710696SDavid.Hollister@Sun.COM 	ASSERT(dip && ddi_get_parent(dip) &&
816810696SDavid.Hollister@Sun.COM 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
816910696SDavid.Hollister@Sun.COM 
817010696SDavid.Hollister@Sun.COM 	/* Return if not marked removed. */
817110696SDavid.Hollister@Sun.COM 	if (!ndi_devi_device_isremoved(dip))
817210696SDavid.Hollister@Sun.COM 		return (0);
817310696SDavid.Hollister@Sun.COM 
817410696SDavid.Hollister@Sun.COM 	/* Mark the device as having been physically reinserted. */
817510696SDavid.Hollister@Sun.COM 	mutex_enter(&(DEVI(dip)->devi_lock));
817610696SDavid.Hollister@Sun.COM 	ndi_devi_clr_hidden(dip);	/* visible: lookup/snapshot */
817710696SDavid.Hollister@Sun.COM 	DEVI_SET_DEVICE_REINSERTED(dip);
817810696SDavid.Hollister@Sun.COM 	DEVI_SET_EVADD(dip);		/* this clears EVREMOVE too */
817910696SDavid.Hollister@Sun.COM 	mutex_exit(&(DEVI(dip)->devi_lock));
818010696SDavid.Hollister@Sun.COM 
818110696SDavid.Hollister@Sun.COM 	/* report insert (as 'online') */
818210696SDavid.Hollister@Sun.COM 	i_ndi_devi_report_status_change(dip, NULL);
818310696SDavid.Hollister@Sun.COM 
818410696SDavid.Hollister@Sun.COM 	/*
818510696SDavid.Hollister@Sun.COM 	 * Invalidate the cache to ensure accurate
818610696SDavid.Hollister@Sun.COM 	 * (di_state() & DI_DEVICE_REMOVED).
818710696SDavid.Hollister@Sun.COM 	 */
818810696SDavid.Hollister@Sun.COM 	i_ddi_di_cache_invalidate();
818910696SDavid.Hollister@Sun.COM 
819010696SDavid.Hollister@Sun.COM 	/*
819110696SDavid.Hollister@Sun.COM 	 * Generate sysevent for those interested in removal (either directly
819210696SDavid.Hollister@Sun.COM 	 * via EC_DEVFS or indirectly via devfsadmd generated EC_DEV).
819310696SDavid.Hollister@Sun.COM 	 */
819410696SDavid.Hollister@Sun.COM 	i_ddi_log_devfs_device_insert(dip);
819510696SDavid.Hollister@Sun.COM 
819610696SDavid.Hollister@Sun.COM 	return (1);		/* DEVICE_REMOVED state changed */
819710696SDavid.Hollister@Sun.COM }
819810696SDavid.Hollister@Sun.COM 
819910696SDavid.Hollister@Sun.COM /*
82000Sstevel@tonic-gate  * ibt_hw_is_present() returns 0 when there is no IB hardware actively
82010Sstevel@tonic-gate  * running.  This is primarily useful for modules like rpcmod which
82020Sstevel@tonic-gate  * needs a quick check to decide whether or not it should try to use
82030Sstevel@tonic-gate  * InfiniBand
82040Sstevel@tonic-gate  */
82050Sstevel@tonic-gate int ib_hw_status = 0;
82060Sstevel@tonic-gate int
ibt_hw_is_present()82070Sstevel@tonic-gate ibt_hw_is_present()
82080Sstevel@tonic-gate {
82090Sstevel@tonic-gate 	return (ib_hw_status);
82100Sstevel@tonic-gate }
82114845Svikram 
82124845Svikram /*
82134845Svikram  * ASSERT that constraint flag is not set and then set the "retire attempt"
82144845Svikram  * flag.
82154845Svikram  */
82164845Svikram int
e_ddi_mark_retiring(dev_info_t * dip,void * arg)82174845Svikram e_ddi_mark_retiring(dev_info_t *dip, void *arg)
82184845Svikram {
82194845Svikram 	char	**cons_array = (char **)arg;
82204845Svikram 	char	*path;
82214845Svikram 	int	constraint;
82224845Svikram 	int	i;
82234845Svikram 
82244845Svikram 	constraint = 0;
82254845Svikram 	if (cons_array) {
82264845Svikram 		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
82274845Svikram 		(void) ddi_pathname(dip, path);
82284845Svikram 		for (i = 0; cons_array[i] != NULL; i++) {
82294845Svikram 			if (strcmp(path, cons_array[i]) == 0) {
82304845Svikram 				constraint = 1;
82314845Svikram 				break;
82324845Svikram 			}
82334845Svikram 		}
82344845Svikram 		kmem_free(path, MAXPATHLEN);
82354845Svikram 	}
82364845Svikram 
82374845Svikram 	mutex_enter(&DEVI(dip)->devi_lock);
82384845Svikram 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
82394845Svikram 	DEVI(dip)->devi_flags |= DEVI_RETIRING;
82404845Svikram 	if (constraint)
82414845Svikram 		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
82424845Svikram 	mutex_exit(&DEVI(dip)->devi_lock);
82434845Svikram 
82444845Svikram 	RIO_VERBOSE((CE_NOTE, "marked dip as undergoing retire process dip=%p",
82454845Svikram 	    (void *)dip));
82464845Svikram 
82474845Svikram 	if (constraint)
82484845Svikram 		RIO_DEBUG((CE_NOTE, "marked dip as constrained, dip=%p",
82494845Svikram 		    (void *)dip));
82504845Svikram 
82514845Svikram 	if (MDI_PHCI(dip))
82524845Svikram 		mdi_phci_mark_retiring(dip, cons_array);
82534845Svikram 
82544845Svikram 	return (DDI_WALK_CONTINUE);
82554845Svikram }
82564845Svikram 
82574845Svikram static void
free_array(char ** cons_array)82584845Svikram free_array(char **cons_array)
82594845Svikram {
82604845Svikram 	int	i;
82614845Svikram 
82624845Svikram 	if (cons_array == NULL)
82634845Svikram 		return;
82644845Svikram 
82654845Svikram 	for (i = 0; cons_array[i] != NULL; i++) {
82664845Svikram 		kmem_free(cons_array[i], strlen(cons_array[i]) + 1);
82674845Svikram 	}
82684845Svikram 	kmem_free(cons_array, (i+1) * sizeof (char *));
82694845Svikram }
82704845Svikram 
82714845Svikram /*
82724845Svikram  * Walk *every* node in subtree and check if it blocks, allows or has no
82734845Svikram  * comment on a proposed retire.
82744845Svikram  */
82754845Svikram int
e_ddi_retire_notify(dev_info_t * dip,void * arg)82764845Svikram e_ddi_retire_notify(dev_info_t *dip, void *arg)
82774845Svikram {
82784845Svikram 	int	*constraint = (int *)arg;
82794845Svikram 
82804845Svikram 	RIO_DEBUG((CE_NOTE, "retire notify: dip = %p", (void *)dip));
82814845Svikram 
82824845Svikram 	(void) e_ddi_offline_notify(dip);
82834845Svikram 
82844845Svikram 	mutex_enter(&(DEVI(dip)->devi_lock));
82854845Svikram 	if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) {
82864845Svikram 		RIO_DEBUG((CE_WARN, "retire notify: dip in retire "
82874845Svikram 		    "subtree is not marked: dip = %p", (void *)dip));
82884845Svikram 		*constraint = 0;
82894845Svikram 	} else if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) {
82904845Svikram 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
82914845Svikram 		RIO_DEBUG((CE_NOTE, "retire notify: BLOCKED: dip = %p",
82924845Svikram 		    (void *)dip));
82934845Svikram 		*constraint = 0;
82944845Svikram 	} else if (!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT)) {
82954845Svikram 		RIO_DEBUG((CE_NOTE, "retire notify: NO CONSTRAINT: "
82964845Svikram 		    "dip = %p", (void *)dip));
82974845Svikram 		*constraint = 0;
82984845Svikram 	} else {
82994845Svikram 		RIO_DEBUG((CE_NOTE, "retire notify: CONSTRAINT set: "
83004845Svikram 		    "dip = %p", (void *)dip));
83014845Svikram 	}
83024845Svikram 	mutex_exit(&DEVI(dip)->devi_lock);
83034845Svikram 
83044845Svikram 	if (MDI_PHCI(dip))
83054845Svikram 		mdi_phci_retire_notify(dip, constraint);
83064845Svikram 
83074845Svikram 	return (DDI_WALK_CONTINUE);
83084845Svikram }
83094845Svikram 
83104845Svikram int
e_ddi_retire_finalize(dev_info_t * dip,void * arg)83114845Svikram e_ddi_retire_finalize(dev_info_t *dip, void *arg)
83124845Svikram {
83134845Svikram 	int constraint = *(int *)arg;
83144845Svikram 	int finalize;
83154845Svikram 	int phci_only;
83164845Svikram 
83174845Svikram 	mutex_enter(&DEVI(dip)->devi_lock);
83184845Svikram 	if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) {
83194845Svikram 		RIO_DEBUG((CE_WARN,
83204845Svikram 		    "retire: unmarked dip(%p) in retire subtree",
83214845Svikram 		    (void *)dip));
83224845Svikram 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRED));
83234845Svikram 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
83244845Svikram 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
83254845Svikram 		mutex_exit(&DEVI(dip)->devi_lock);
83264845Svikram 		return (DDI_WALK_CONTINUE);
83274845Svikram 	}
83284845Svikram 
83294845Svikram 	/*
83304845Svikram 	 * retire the device if constraints have been applied
83314845Svikram 	 * or if the device is not in use
83324845Svikram 	 */
83334845Svikram 	finalize = 0;
83344845Svikram 	if (constraint) {
833512107SStephen.Hanson@Sun.COM 		ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
833612107SStephen.Hanson@Sun.COM 
83374845Svikram 		ASSERT(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT);
83384845Svikram 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
83394845Svikram 		DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
83404845Svikram 		DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
83414845Svikram 		DEVI(dip)->devi_flags |= DEVI_RETIRED;
83424845Svikram 		mutex_exit(&DEVI(dip)->devi_lock);
83434845Svikram 		(void) spec_fence_snode(dip, NULL);
83444845Svikram 		RIO_DEBUG((CE_NOTE, "Fenced off: dip = %p", (void *)dip));
83454845Svikram 		e_ddi_offline_finalize(dip, DDI_SUCCESS);
83464845Svikram 	} else {
83474845Svikram 		if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) {
83484845Svikram 			ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
83494845Svikram 			DEVI(dip)->devi_flags &= ~DEVI_R_BLOCKED;
83504845Svikram 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
83514845Svikram 			/* we have already finalized during notify */
83524845Svikram 		} else if (DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT) {
83534845Svikram 			DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
83544845Svikram 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
83554845Svikram 			finalize = 1;
83564845Svikram 		} else {
83574845Svikram 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
83584845Svikram 			/*
83594845Svikram 			 * even if no contracts, need to call finalize
83604845Svikram 			 * to clear the contract barrier on the dip
83614845Svikram 			 */
83624845Svikram 			finalize = 1;
83634845Svikram 		}
83644845Svikram 		mutex_exit(&DEVI(dip)->devi_lock);
83654845Svikram 		RIO_DEBUG((CE_NOTE, "finalize: NOT retired: dip = %p",
83664845Svikram 		    (void *)dip));
83674845Svikram 		if (finalize)
83684845Svikram 			e_ddi_offline_finalize(dip, DDI_FAILURE);
83694845Svikram 	}
83704845Svikram 
83714845Svikram 	/*
83724845Svikram 	 * phci_only variable indicates no client checking, just
83734845Svikram 	 * offline the PHCI. We set that to 0 to enable client
83744845Svikram 	 * checking
83754845Svikram 	 */
83764845Svikram 	phci_only = 0;
83774845Svikram 	if (MDI_PHCI(dip))
837812107SStephen.Hanson@Sun.COM 		mdi_phci_retire_finalize(dip, phci_only, arg);
83794845Svikram 
83804845Svikram 	return (DDI_WALK_CONTINUE);
83814845Svikram }
83824845Svikram 
83834845Svikram /*
83844845Svikram  * Returns
83858912SChris.Horne@Sun.COM  *	DDI_SUCCESS if constraints allow retire
83864845Svikram  *	DDI_FAILURE if constraints don't allow retire.
83874845Svikram  * cons_array is a NULL terminated array of node paths for
83884845Svikram  * which constraints have already been applied.
83894845Svikram  */
83904845Svikram int
e_ddi_retire_device(char * path,char ** cons_array)83914845Svikram e_ddi_retire_device(char *path, char **cons_array)
83924845Svikram {
83934845Svikram 	dev_info_t	*dip;
83944845Svikram 	dev_info_t	*pdip;
83954845Svikram 	int		circ;
83964845Svikram 	int		circ2;
83974845Svikram 	int		constraint;
83984845Svikram 	char		*devnm;
83994845Svikram 
84004845Svikram 	/*
84014845Svikram 	 * First, lookup the device
84024845Svikram 	 */
84034845Svikram 	dip = e_ddi_hold_devi_by_path(path, 0);
84044845Svikram 	if (dip == NULL) {
84054845Svikram 		/*
84064845Svikram 		 * device does not exist. This device cannot be
84074845Svikram 		 * a critical device since it is not in use. Thus
84084845Svikram 		 * this device is always retireable. Return DDI_SUCCESS
84094845Svikram 		 * to indicate this. If this device is ever
84104845Svikram 		 * instantiated, I/O framework will consult the
84114845Svikram 		 * the persistent retire store, mark it as
84124845Svikram 		 * retired and fence it off.
84134845Svikram 		 */
84144845Svikram 		RIO_DEBUG((CE_NOTE, "Retire device: device doesn't exist."
84154845Svikram 		    " NOP. Just returning SUCCESS. path=%s", path));
84164845Svikram 		free_array(cons_array);
84174845Svikram 		return (DDI_SUCCESS);
84184845Svikram 	}
84194845Svikram 
84204845Svikram 	RIO_DEBUG((CE_NOTE, "Retire device: found dip = %p.", (void *)dip));
84214845Svikram 
84224845Svikram 	pdip = ddi_get_parent(dip);
84234845Svikram 	ndi_hold_devi(pdip);
84244845Svikram 
84254845Svikram 	/*
84264845Svikram 	 * Run devfs_clean() in case dip has no constraints and is
84274845Svikram 	 * not in use, so is retireable but there are dv_nodes holding
84284845Svikram 	 * ref-count on the dip. Note that devfs_clean() always returns
84294845Svikram 	 * success.
84304845Svikram 	 */
84314845Svikram 	devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
84324845Svikram 	(void) ddi_deviname(dip, devnm);
84334845Svikram 	(void) devfs_clean(pdip, devnm + 1, DV_CLEAN_FORCE);
84344845Svikram 	kmem_free(devnm, MAXNAMELEN + 1);
84354845Svikram 
84364845Svikram 	ndi_devi_enter(pdip, &circ);
84374845Svikram 
84384845Svikram 	/* release hold from e_ddi_hold_devi_by_path */
84394845Svikram 	ndi_rele_devi(dip);
84404845Svikram 
84414845Svikram 	/*
84424845Svikram 	 * If it cannot make a determination, is_leaf_node() assumes
84434845Svikram 	 * dip is a nexus.
84444845Svikram 	 */
84454845Svikram 	(void) e_ddi_mark_retiring(dip, cons_array);
84464845Svikram 	if (!is_leaf_node(dip)) {
84474845Svikram 		ndi_devi_enter(dip, &circ2);
84484845Svikram 		ddi_walk_devs(ddi_get_child(dip), e_ddi_mark_retiring,
84494845Svikram 		    cons_array);
84504845Svikram 		ndi_devi_exit(dip, circ2);
84514845Svikram 	}
84524845Svikram 	free_array(cons_array);
84534845Svikram 
84544845Svikram 	/*
84554845Svikram 	 * apply constraints
84564845Svikram 	 */
84574845Svikram 	RIO_DEBUG((CE_NOTE, "retire: subtree retire notify: path = %s", path));
84584845Svikram 
84594845Svikram 	constraint = 1;	/* assume constraints allow retire */
84604845Svikram 	(void) e_ddi_retire_notify(dip, &constraint);
84614845Svikram 	if (!is_leaf_node(dip)) {
84624845Svikram 		ndi_devi_enter(dip, &circ2);
84634845Svikram 		ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_notify,
84644845Svikram 		    &constraint);
84654845Svikram 		ndi_devi_exit(dip, circ2);
84664845Svikram 	}
84674845Svikram 
84684845Svikram 	/*
84694845Svikram 	 * Now finalize the retire
84704845Svikram 	 */
84714845Svikram 	(void) e_ddi_retire_finalize(dip, &constraint);
84724845Svikram 	if (!is_leaf_node(dip)) {
84734845Svikram 		ndi_devi_enter(dip, &circ2);
84744845Svikram 		ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_finalize,
84754845Svikram 		    &constraint);
84764845Svikram 		ndi_devi_exit(dip, circ2);
84774845Svikram 	}
84784845Svikram 
84794845Svikram 	if (!constraint) {
84804845Svikram 		RIO_DEBUG((CE_WARN, "retire failed: path = %s", path));
84814845Svikram 	} else {
84824845Svikram 		RIO_DEBUG((CE_NOTE, "retire succeeded: path = %s", path));
84834845Svikram 	}
84844845Svikram 
84854845Svikram 	ndi_devi_exit(pdip, circ);
84864845Svikram 	ndi_rele_devi(pdip);
84874845Svikram 	return (constraint ? DDI_SUCCESS : DDI_FAILURE);
84884845Svikram }
84894845Svikram 
84904845Svikram static int
unmark_and_unfence(dev_info_t * dip,void * arg)84914845Svikram unmark_and_unfence(dev_info_t *dip, void *arg)
84924845Svikram {
84934845Svikram 	char	*path = (char *)arg;
84944845Svikram 
84954845Svikram 	ASSERT(path);
84964845Svikram 
84974845Svikram 	(void) ddi_pathname(dip, path);
84984845Svikram 
84994845Svikram 	mutex_enter(&DEVI(dip)->devi_lock);
85004845Svikram 	DEVI(dip)->devi_flags &= ~DEVI_RETIRED;
85014845Svikram 	DEVI_SET_DEVICE_ONLINE(dip);
85024845Svikram 	mutex_exit(&DEVI(dip)->devi_lock);
85034845Svikram 
85044845Svikram 	RIO_VERBOSE((CE_NOTE, "Cleared RETIRED flag: dip=%p, path=%s",
85054845Svikram 	    (void *)dip, path));
85064845Svikram 
85074845Svikram 	(void) spec_unfence_snode(dip);
85084845Svikram 	RIO_DEBUG((CE_NOTE, "Unfenced device: %s", path));
85094845Svikram 
85104845Svikram 	if (MDI_PHCI(dip))
85114845Svikram 		mdi_phci_unretire(dip);
85124845Svikram 
85134845Svikram 	return (DDI_WALK_CONTINUE);
85144845Svikram }
85154845Svikram 
85164845Svikram struct find_dip {
85174845Svikram 	char	*fd_buf;
85184845Svikram 	char	*fd_path;
85194845Svikram 	dev_info_t *fd_dip;
85204845Svikram };
85214845Svikram 
85224845Svikram static int
find_dip_fcn(dev_info_t * dip,void * arg)85234845Svikram find_dip_fcn(dev_info_t *dip, void *arg)
85244845Svikram {
85254845Svikram 	struct find_dip *findp = (struct find_dip *)arg;
85264845Svikram 
85274845Svikram 	(void) ddi_pathname(dip, findp->fd_buf);
85284845Svikram 
85294845Svikram 	if (strcmp(findp->fd_path, findp->fd_buf) != 0)
85304845Svikram 		return (DDI_WALK_CONTINUE);
85314845Svikram 
85324845Svikram 	ndi_hold_devi(dip);
85334845Svikram 	findp->fd_dip = dip;
85344845Svikram 
85354845Svikram 	return (DDI_WALK_TERMINATE);
85364845Svikram }
85374845Svikram 
85384845Svikram int
e_ddi_unretire_device(char * path)85394845Svikram e_ddi_unretire_device(char *path)
85404845Svikram {
85414845Svikram 	int		circ;
85427235Svikram 	int		circ2;
85434845Svikram 	char		*path2;
85444845Svikram 	dev_info_t	*pdip;
85454845Svikram 	dev_info_t	*dip;
85464845Svikram 	struct find_dip	 find_dip;
85474845Svikram 
85484845Svikram 	ASSERT(path);
85494845Svikram 	ASSERT(*path == '/');
85504845Svikram 
85514845Svikram 	if (strcmp(path, "/") == 0) {
85524845Svikram 		cmn_err(CE_WARN, "Root node cannot be retired. Skipping "
85534845Svikram 		    "device unretire: %s", path);
85544845Svikram 		return (0);
85554845Svikram 	}
85564845Svikram 
85574845Svikram 	/*
85584845Svikram 	 * We can't lookup the dip (corresponding to path) via
85594845Svikram 	 * e_ddi_hold_devi_by_path() because the dip may be offline
85604845Svikram 	 * and may not attach. Use ddi_walk_devs() instead;
85614845Svikram 	 */
85624845Svikram 	find_dip.fd_buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
85634845Svikram 	find_dip.fd_path = path;
85644845Svikram 	find_dip.fd_dip = NULL;
85654845Svikram 
85664845Svikram 	pdip = ddi_root_node();
85674845Svikram 
85684845Svikram 	ndi_devi_enter(pdip, &circ);
85694845Svikram 	ddi_walk_devs(ddi_get_child(pdip), find_dip_fcn, &find_dip);
85704845Svikram 	ndi_devi_exit(pdip, circ);
85714845Svikram 
85724845Svikram 	kmem_free(find_dip.fd_buf, MAXPATHLEN);
85734845Svikram 
85744845Svikram 	if (find_dip.fd_dip == NULL) {
85754845Svikram 		cmn_err(CE_WARN, "Device not found in device tree. Skipping "
85764845Svikram 		    "device unretire: %s", path);
85774845Svikram 		return (0);
85784845Svikram 	}
85794845Svikram 
85804845Svikram 	dip = find_dip.fd_dip;
85814845Svikram 
85824845Svikram 	pdip = ddi_get_parent(dip);
85834845Svikram 
85844845Svikram 	ndi_hold_devi(pdip);
85854845Svikram 
85864845Svikram 	ndi_devi_enter(pdip, &circ);
85874845Svikram 
85884845Svikram 	path2 = kmem_alloc(MAXPATHLEN, KM_SLEEP);
85894845Svikram 
85904845Svikram 	(void) unmark_and_unfence(dip, path2);
85914845Svikram 	if (!is_leaf_node(dip)) {
85927235Svikram 		ndi_devi_enter(dip, &circ2);
85934845Svikram 		ddi_walk_devs(ddi_get_child(dip), unmark_and_unfence, path2);
85947235Svikram 		ndi_devi_exit(dip, circ2);
85954845Svikram 	}
85964845Svikram 
85974845Svikram 	kmem_free(path2, MAXPATHLEN);
85984845Svikram 
85994845Svikram 	/* release hold from find_dip_fcn() */
86004845Svikram 	ndi_rele_devi(dip);
86014845Svikram 
86024845Svikram 	ndi_devi_exit(pdip, circ);
86034845Svikram 
86044845Svikram 	ndi_rele_devi(pdip);
86054845Svikram 
86064845Svikram 	return (0);
86074845Svikram }
86084845Svikram 
86094845Svikram /*
86104845Svikram  * Called before attach on a dip that has been retired.
86114845Svikram  */
86124845Svikram static int
mark_and_fence(dev_info_t * dip,void * arg)86134845Svikram mark_and_fence(dev_info_t *dip, void *arg)
86144845Svikram {
861510923SEvan.Yan@Sun.COM 	char	*fencepath = (char *)arg;
86164845Svikram 
86174845Svikram 	/*
86184845Svikram 	 * We have already decided to retire this device. The various
86194845Svikram 	 * constraint checking should not be set.
86204845Svikram 	 * NOTE that the retire flag may already be set due to
86214845Svikram 	 * fenced -> detach -> fenced transitions.
86224845Svikram 	 */
86234845Svikram 	mutex_enter(&DEVI(dip)->devi_lock);
86244845Svikram 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
86254845Svikram 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
86264845Svikram 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRING));
86274845Svikram 	DEVI(dip)->devi_flags |= DEVI_RETIRED;
86284845Svikram 	mutex_exit(&DEVI(dip)->devi_lock);
86294845Svikram 	RIO_VERBOSE((CE_NOTE, "marked as RETIRED dip=%p", (void *)dip));
86304845Svikram 
86314845Svikram 	if (fencepath) {
86324845Svikram 		(void) spec_fence_snode(dip, NULL);
86334845Svikram 		RIO_DEBUG((CE_NOTE, "Fenced: %s",
86344845Svikram 		    ddi_pathname(dip, fencepath)));
86354845Svikram 	}
86364845Svikram 
86374845Svikram 	return (DDI_WALK_CONTINUE);
86384845Svikram }
86394845Svikram 
86404845Svikram /*
86414845Svikram  * Checks the retire database and:
86424845Svikram  *
86434845Svikram  * - if device is present in the retire database, marks the device retired
86444845Svikram  *   and fences it off.
86454845Svikram  * - if device is not in retire database, allows the device to attach normally
86464845Svikram  *
86474845Svikram  * To be called only by framework attach code on first attach attempt.
86484845Svikram  *
86494845Svikram  */
865012618SStephen.Hanson@Sun.COM static int
i_ddi_check_retire(dev_info_t * dip)86514845Svikram i_ddi_check_retire(dev_info_t *dip)
86524845Svikram {
86534845Svikram 	char		*path;
86544845Svikram 	dev_info_t	*pdip;
86554845Svikram 	int		circ;
86564845Svikram 	int		phci_only;
865712107SStephen.Hanson@Sun.COM 	int		constraint;
86584845Svikram 
86594845Svikram 	pdip = ddi_get_parent(dip);
86604845Svikram 
86614845Svikram 	/*
86624845Svikram 	 * Root dip is treated special and doesn't take this code path.
86634845Svikram 	 * Also root can never be retired.
86644845Svikram 	 */
86654845Svikram 	ASSERT(pdip);
86664845Svikram 	ASSERT(DEVI_BUSY_OWNED(pdip));
86674845Svikram 	ASSERT(i_ddi_node_state(dip) < DS_ATTACHED);
86684845Svikram 
86694845Svikram 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
86704845Svikram 
86714845Svikram 	(void) ddi_pathname(dip, path);
86724845Svikram 
86734845Svikram 	RIO_VERBOSE((CE_NOTE, "Checking if dip should attach: dip=%p, path=%s",
86744845Svikram 	    (void *)dip, path));
86754845Svikram 
86764845Svikram 	/*
867710923SEvan.Yan@Sun.COM 	 * Check if this device is in the "retired" store i.e.	should
86784845Svikram 	 * be retired. If not, we have nothing to do.
86794845Svikram 	 */
86804845Svikram 	if (e_ddi_device_retired(path) == 0) {
86814845Svikram 		RIO_VERBOSE((CE_NOTE, "device is NOT retired: path=%s", path));
868212618SStephen.Hanson@Sun.COM 		if (DEVI(dip)->devi_flags & DEVI_RETIRED)
868312618SStephen.Hanson@Sun.COM 			(void) e_ddi_unretire_device(path);
86844845Svikram 		kmem_free(path, MAXPATHLEN);
868512618SStephen.Hanson@Sun.COM 		return (0);
86864845Svikram 	}
86874845Svikram 
86884845Svikram 	RIO_DEBUG((CE_NOTE, "attach: device is retired: path=%s", path));
86894845Svikram 
86904845Svikram 	/*
86914845Svikram 	 * Mark dips and fence off snodes (if any)
86924845Svikram 	 */
86934845Svikram 	RIO_DEBUG((CE_NOTE, "attach: Mark and fence subtree: path=%s", path));
86944845Svikram 	(void) mark_and_fence(dip, path);
86954845Svikram 	if (!is_leaf_node(dip)) {
86964845Svikram 		ndi_devi_enter(dip, &circ);
86974845Svikram 		ddi_walk_devs(ddi_get_child(dip), mark_and_fence, path);
86984845Svikram 		ndi_devi_exit(dip, circ);
86994845Svikram 	}
87004845Svikram 
87014845Svikram 	kmem_free(path, MAXPATHLEN);
87024845Svikram 
87034845Svikram 	/*
87044845Svikram 	 * We don't want to check the client. We just want to
87054845Svikram 	 * offline the PHCI
87064845Svikram 	 */
87074845Svikram 	phci_only = 1;
870812107SStephen.Hanson@Sun.COM 	constraint = 1;
87094845Svikram 	if (MDI_PHCI(dip))
871012107SStephen.Hanson@Sun.COM 		mdi_phci_retire_finalize(dip, phci_only, &constraint);
871112618SStephen.Hanson@Sun.COM 	return (1);
87124845Svikram }
871311600SVikram.Hegde@Sun.COM 
871412116SVikram.Hegde@Sun.COM 
871512116SVikram.Hegde@Sun.COM #define	VAL_ALIAS(array, x)	(strlen(array[x].pair_alias))
871612116SVikram.Hegde@Sun.COM #define	VAL_CURR(array, x)	(strlen(array[x].pair_curr))
871712116SVikram.Hegde@Sun.COM #define	SWAP(array, x, y)			\
871812116SVikram.Hegde@Sun.COM {						\
871912116SVikram.Hegde@Sun.COM 	alias_pair_t tmpair = array[x];		\
872012116SVikram.Hegde@Sun.COM 	array[x] = array[y];			\
872112116SVikram.Hegde@Sun.COM 	array[y] = tmpair;			\
872212116SVikram.Hegde@Sun.COM }
872312116SVikram.Hegde@Sun.COM 
872412116SVikram.Hegde@Sun.COM static int
partition_curr(alias_pair_t * array,int start,int end)872512116SVikram.Hegde@Sun.COM partition_curr(alias_pair_t *array, int start, int end)
872612116SVikram.Hegde@Sun.COM {
872712116SVikram.Hegde@Sun.COM 	int	i = start - 1;
872812116SVikram.Hegde@Sun.COM 	int	j = end + 1;
872912116SVikram.Hegde@Sun.COM 	int	pivot = start;
873012116SVikram.Hegde@Sun.COM 
873112116SVikram.Hegde@Sun.COM 	for (;;) {
873212116SVikram.Hegde@Sun.COM 		do {
873312116SVikram.Hegde@Sun.COM 			j--;
873412116SVikram.Hegde@Sun.COM 		} while (VAL_CURR(array, j) > VAL_CURR(array, pivot));
873512116SVikram.Hegde@Sun.COM 
873612116SVikram.Hegde@Sun.COM 		do {
873712116SVikram.Hegde@Sun.COM 			i++;
873812116SVikram.Hegde@Sun.COM 		} while (VAL_CURR(array, i) < VAL_CURR(array, pivot));
873912116SVikram.Hegde@Sun.COM 
874012116SVikram.Hegde@Sun.COM 		if (i < j)
874112116SVikram.Hegde@Sun.COM 			SWAP(array, i, j)
874212116SVikram.Hegde@Sun.COM 		else
874312116SVikram.Hegde@Sun.COM 			return (j);
874412116SVikram.Hegde@Sun.COM 	}
874512116SVikram.Hegde@Sun.COM }
874612116SVikram.Hegde@Sun.COM 
874712116SVikram.Hegde@Sun.COM static int
partition_aliases(alias_pair_t * array,int start,int end)874812116SVikram.Hegde@Sun.COM partition_aliases(alias_pair_t *array, int start, int end)
874912116SVikram.Hegde@Sun.COM {
875012116SVikram.Hegde@Sun.COM 	int	i = start - 1;
875112116SVikram.Hegde@Sun.COM 	int	j = end + 1;
875212116SVikram.Hegde@Sun.COM 	int	pivot = start;
875312116SVikram.Hegde@Sun.COM 
875412116SVikram.Hegde@Sun.COM 	for (;;) {
875512116SVikram.Hegde@Sun.COM 		do {
875612116SVikram.Hegde@Sun.COM 			j--;
875712116SVikram.Hegde@Sun.COM 		} while (VAL_ALIAS(array, j) > VAL_ALIAS(array, pivot));
875812116SVikram.Hegde@Sun.COM 
875912116SVikram.Hegde@Sun.COM 		do {
876012116SVikram.Hegde@Sun.COM 			i++;
876112116SVikram.Hegde@Sun.COM 		} while (VAL_ALIAS(array, i) < VAL_ALIAS(array, pivot));
876212116SVikram.Hegde@Sun.COM 
876312116SVikram.Hegde@Sun.COM 		if (i < j)
876412116SVikram.Hegde@Sun.COM 			SWAP(array, i, j)
876512116SVikram.Hegde@Sun.COM 		else
876612116SVikram.Hegde@Sun.COM 			return (j);
876712116SVikram.Hegde@Sun.COM 	}
876812116SVikram.Hegde@Sun.COM }
876912116SVikram.Hegde@Sun.COM static void
sort_alias_pairs(alias_pair_t * array,int start,int end)877012116SVikram.Hegde@Sun.COM sort_alias_pairs(alias_pair_t *array, int start, int end)
877112116SVikram.Hegde@Sun.COM {
877212116SVikram.Hegde@Sun.COM 	int mid;
877312116SVikram.Hegde@Sun.COM 
877412116SVikram.Hegde@Sun.COM 	if (start < end) {
877512116SVikram.Hegde@Sun.COM 		mid = partition_aliases(array, start, end);
877612116SVikram.Hegde@Sun.COM 		sort_alias_pairs(array, start, mid);
877712116SVikram.Hegde@Sun.COM 		sort_alias_pairs(array, mid + 1, end);
877812116SVikram.Hegde@Sun.COM 	}
877912116SVikram.Hegde@Sun.COM }
878012116SVikram.Hegde@Sun.COM 
878112116SVikram.Hegde@Sun.COM static void
sort_curr_pairs(alias_pair_t * array,int start,int end)878212116SVikram.Hegde@Sun.COM sort_curr_pairs(alias_pair_t *array, int start, int end)
878312116SVikram.Hegde@Sun.COM {
878412116SVikram.Hegde@Sun.COM 	int mid;
878512116SVikram.Hegde@Sun.COM 
878612116SVikram.Hegde@Sun.COM 	if (start < end) {
878712116SVikram.Hegde@Sun.COM 		mid = partition_curr(array, start, end);
878812116SVikram.Hegde@Sun.COM 		sort_curr_pairs(array, start, mid);
878912116SVikram.Hegde@Sun.COM 		sort_curr_pairs(array, mid + 1, end);
879012116SVikram.Hegde@Sun.COM 	}
879112116SVikram.Hegde@Sun.COM }
879212116SVikram.Hegde@Sun.COM 
879312116SVikram.Hegde@Sun.COM static void
create_sorted_pairs(plat_alias_t * pali,int npali)879412116SVikram.Hegde@Sun.COM create_sorted_pairs(plat_alias_t *pali, int npali)
879512116SVikram.Hegde@Sun.COM {
879612116SVikram.Hegde@Sun.COM 	int		i;
879712116SVikram.Hegde@Sun.COM 	int		j;
879812116SVikram.Hegde@Sun.COM 	int		k;
879912116SVikram.Hegde@Sun.COM 	int		count;
880012116SVikram.Hegde@Sun.COM 
880112116SVikram.Hegde@Sun.COM 	count = 0;
880212116SVikram.Hegde@Sun.COM 	for (i = 0; i < npali; i++) {
880312116SVikram.Hegde@Sun.COM 		count += pali[i].pali_naliases;
880412116SVikram.Hegde@Sun.COM 	}
880512116SVikram.Hegde@Sun.COM 
880612116SVikram.Hegde@Sun.COM 	ddi_aliases.dali_alias_pairs = kmem_zalloc(
880712116SVikram.Hegde@Sun.COM 	    (sizeof (alias_pair_t)) * count, KM_NOSLEEP);
880812116SVikram.Hegde@Sun.COM 	if (ddi_aliases.dali_alias_pairs == NULL) {
880912116SVikram.Hegde@Sun.COM 		cmn_err(CE_PANIC, "alias path-pair alloc failed");
881012116SVikram.Hegde@Sun.COM 		/*NOTREACHED*/
881112116SVikram.Hegde@Sun.COM 	}
881212116SVikram.Hegde@Sun.COM 
881312116SVikram.Hegde@Sun.COM 	ddi_aliases.dali_curr_pairs = kmem_zalloc(
881412116SVikram.Hegde@Sun.COM 	    (sizeof (alias_pair_t)) * count, KM_NOSLEEP);
881512116SVikram.Hegde@Sun.COM 	if (ddi_aliases.dali_curr_pairs == NULL) {
881612116SVikram.Hegde@Sun.COM 		cmn_err(CE_PANIC, "curr path-pair alloc failed");
881712116SVikram.Hegde@Sun.COM 		/*NOTREACHED*/
881812116SVikram.Hegde@Sun.COM 	}
881912116SVikram.Hegde@Sun.COM 
882012116SVikram.Hegde@Sun.COM 	for (i = 0, k = 0; i < npali; i++) {
882112116SVikram.Hegde@Sun.COM 		for (j = 0; j < pali[i].pali_naliases; j++, k++) {
882212116SVikram.Hegde@Sun.COM 			ddi_aliases.dali_alias_pairs[k].pair_curr =
882312116SVikram.Hegde@Sun.COM 			    ddi_aliases.dali_curr_pairs[k].pair_curr =
882412116SVikram.Hegde@Sun.COM 			    pali[i].pali_current;
882512116SVikram.Hegde@Sun.COM 			ddi_aliases.dali_alias_pairs[k].pair_alias =
882612116SVikram.Hegde@Sun.COM 			    ddi_aliases.dali_curr_pairs[k].pair_alias =
882712116SVikram.Hegde@Sun.COM 			    pali[i].pali_aliases[j];
882812116SVikram.Hegde@Sun.COM 		}
882912116SVikram.Hegde@Sun.COM 	}
883012116SVikram.Hegde@Sun.COM 
883112116SVikram.Hegde@Sun.COM 	ASSERT(k == count);
883212116SVikram.Hegde@Sun.COM 
883312116SVikram.Hegde@Sun.COM 	ddi_aliases.dali_num_pairs = count;
883412116SVikram.Hegde@Sun.COM 
883512116SVikram.Hegde@Sun.COM 	/* Now sort the array based on length of pair_alias */
883612116SVikram.Hegde@Sun.COM 	sort_alias_pairs(ddi_aliases.dali_alias_pairs, 0, count - 1);
883712116SVikram.Hegde@Sun.COM 	sort_curr_pairs(ddi_aliases.dali_curr_pairs, 0, count - 1);
883812116SVikram.Hegde@Sun.COM }
883912116SVikram.Hegde@Sun.COM 
884012116SVikram.Hegde@Sun.COM void
ddi_register_aliases(plat_alias_t * pali,uint64_t npali)884112116SVikram.Hegde@Sun.COM ddi_register_aliases(plat_alias_t *pali, uint64_t npali)
884212116SVikram.Hegde@Sun.COM {
884312116SVikram.Hegde@Sun.COM 
884412116SVikram.Hegde@Sun.COM 	ASSERT((pali == NULL) ^ (npali != 0));
884512116SVikram.Hegde@Sun.COM 
884612116SVikram.Hegde@Sun.COM 	if (npali == 0) {
884712116SVikram.Hegde@Sun.COM 		ddi_err(DER_PANIC, NULL, "npali == 0");
884812116SVikram.Hegde@Sun.COM 		/*NOTREACHED*/
884912116SVikram.Hegde@Sun.COM 	}
885012116SVikram.Hegde@Sun.COM 
885112116SVikram.Hegde@Sun.COM 	if (ddi_aliases_present == B_TRUE) {
885212116SVikram.Hegde@Sun.COM 		ddi_err(DER_PANIC, NULL, "multiple init");
885312116SVikram.Hegde@Sun.COM 		/*NOTREACHED*/
885412116SVikram.Hegde@Sun.COM 	}
885512116SVikram.Hegde@Sun.COM 
885612116SVikram.Hegde@Sun.COM 	ddi_aliases.dali_alias_TLB = mod_hash_create_strhash(
885712116SVikram.Hegde@Sun.COM 	    "ddi-alias-tlb", DDI_ALIAS_HASH_SIZE, mod_hash_null_valdtor);
885812116SVikram.Hegde@Sun.COM 	if (ddi_aliases.dali_alias_TLB == NULL) {
885912116SVikram.Hegde@Sun.COM 		ddi_err(DER_PANIC, NULL, "alias TLB hash alloc failed");
886012116SVikram.Hegde@Sun.COM 		/*NOTREACHED*/
886112116SVikram.Hegde@Sun.COM 	}
886212116SVikram.Hegde@Sun.COM 
886312116SVikram.Hegde@Sun.COM 	ddi_aliases.dali_curr_TLB = mod_hash_create_strhash(
886412116SVikram.Hegde@Sun.COM 	    "ddi-curr-tlb", DDI_ALIAS_HASH_SIZE, mod_hash_null_valdtor);
886512116SVikram.Hegde@Sun.COM 	if (ddi_aliases.dali_curr_TLB == NULL) {
886612116SVikram.Hegde@Sun.COM 		ddi_err(DER_PANIC, NULL, "curr TLB hash alloc failed");
886712116SVikram.Hegde@Sun.COM 		/*NOTREACHED*/
886812116SVikram.Hegde@Sun.COM 	}
886912116SVikram.Hegde@Sun.COM 
887012116SVikram.Hegde@Sun.COM 	create_sorted_pairs(pali, npali);
887112116SVikram.Hegde@Sun.COM 
887212116SVikram.Hegde@Sun.COM 	tsd_create(&tsd_ddi_redirect, NULL);
887312116SVikram.Hegde@Sun.COM 
887412116SVikram.Hegde@Sun.COM 	ddi_aliases_present = B_TRUE;
887512116SVikram.Hegde@Sun.COM }
887612116SVikram.Hegde@Sun.COM 
887712116SVikram.Hegde@Sun.COM static dev_info_t *
path_to_dip(char * path)887812116SVikram.Hegde@Sun.COM path_to_dip(char *path)
887912116SVikram.Hegde@Sun.COM {
888012116SVikram.Hegde@Sun.COM 	dev_info_t	*currdip;
888112116SVikram.Hegde@Sun.COM 	int		error;
888212116SVikram.Hegde@Sun.COM 	char		*pdup;
888312116SVikram.Hegde@Sun.COM 
888412116SVikram.Hegde@Sun.COM 	pdup = ddi_strdup(path, KM_NOSLEEP);
888512116SVikram.Hegde@Sun.COM 	if (pdup == NULL) {
888612116SVikram.Hegde@Sun.COM 		cmn_err(CE_PANIC, "path strdup failed: %s", path);
888712116SVikram.Hegde@Sun.COM 		/*NOTREACHED*/
888812116SVikram.Hegde@Sun.COM 	}
888912116SVikram.Hegde@Sun.COM 
889012116SVikram.Hegde@Sun.COM 	error = resolve_pathname(pdup, &currdip, NULL, NULL);
889112116SVikram.Hegde@Sun.COM 
889212116SVikram.Hegde@Sun.COM 	kmem_free(pdup, strlen(path) + 1);
889312116SVikram.Hegde@Sun.COM 
889412116SVikram.Hegde@Sun.COM 	return (error ? NULL : currdip);
889512116SVikram.Hegde@Sun.COM }
889612116SVikram.Hegde@Sun.COM 
889712116SVikram.Hegde@Sun.COM dev_info_t *
ddi_alias_to_currdip(char * alias,int i)889812116SVikram.Hegde@Sun.COM ddi_alias_to_currdip(char *alias, int i)
889912116SVikram.Hegde@Sun.COM {
890012116SVikram.Hegde@Sun.COM 	alias_pair_t *pair;
890112116SVikram.Hegde@Sun.COM 	char *curr;
890212116SVikram.Hegde@Sun.COM 	dev_info_t *currdip = NULL;
890312116SVikram.Hegde@Sun.COM 	char *aliasdup;
890412329SJerry.Gilliam@Sun.COM 	int rv, len;
890512116SVikram.Hegde@Sun.COM 
890612116SVikram.Hegde@Sun.COM 	pair = &(ddi_aliases.dali_alias_pairs[i]);
890712116SVikram.Hegde@Sun.COM 	len = strlen(pair->pair_alias);
890812116SVikram.Hegde@Sun.COM 
890912116SVikram.Hegde@Sun.COM 	curr = NULL;
891012116SVikram.Hegde@Sun.COM 	aliasdup = ddi_strdup(alias, KM_NOSLEEP);
891112116SVikram.Hegde@Sun.COM 	if (aliasdup == NULL) {
891212116SVikram.Hegde@Sun.COM 		cmn_err(CE_PANIC, "aliasdup alloc failed");
891312116SVikram.Hegde@Sun.COM 		/*NOTREACHED*/
891412116SVikram.Hegde@Sun.COM 	}
891512116SVikram.Hegde@Sun.COM 
891612116SVikram.Hegde@Sun.COM 	if (strncmp(alias, pair->pair_alias, len)  != 0)
891712116SVikram.Hegde@Sun.COM 		goto out;
891812116SVikram.Hegde@Sun.COM 
891912116SVikram.Hegde@Sun.COM 	if (alias[len] != '/' && alias[len] != '\0')
892012116SVikram.Hegde@Sun.COM 		goto out;
892112116SVikram.Hegde@Sun.COM 
892212116SVikram.Hegde@Sun.COM 	curr = kmem_alloc(MAXPATHLEN, KM_NOSLEEP);
892312116SVikram.Hegde@Sun.COM 	if (curr == NULL) {
892412116SVikram.Hegde@Sun.COM 		cmn_err(CE_PANIC, "curr alloc failed");
892512116SVikram.Hegde@Sun.COM 		/*NOTREACHED*/
892612116SVikram.Hegde@Sun.COM 	}
892712116SVikram.Hegde@Sun.COM 	(void) strlcpy(curr, pair->pair_curr, MAXPATHLEN);
892812116SVikram.Hegde@Sun.COM 	if (alias[len] == '/') {
892912116SVikram.Hegde@Sun.COM 		(void) strlcat(curr, "/", MAXPATHLEN);
893012116SVikram.Hegde@Sun.COM 		(void) strlcat(curr, &alias[len + 1], MAXPATHLEN);
893112116SVikram.Hegde@Sun.COM 	}
893212116SVikram.Hegde@Sun.COM 
893312116SVikram.Hegde@Sun.COM 	currdip = path_to_dip(curr);
893412116SVikram.Hegde@Sun.COM 
893512116SVikram.Hegde@Sun.COM out:
893612116SVikram.Hegde@Sun.COM 	if (currdip) {
893712329SJerry.Gilliam@Sun.COM 		rv = mod_hash_insert(ddi_aliases.dali_alias_TLB,
893812116SVikram.Hegde@Sun.COM 		    (mod_hash_key_t)aliasdup, (mod_hash_val_t)curr);
893912329SJerry.Gilliam@Sun.COM 		if (rv != 0) {
894012329SJerry.Gilliam@Sun.COM 			kmem_free(curr, MAXPATHLEN);
894112329SJerry.Gilliam@Sun.COM 			strfree(aliasdup);
894212329SJerry.Gilliam@Sun.COM 		}
894312116SVikram.Hegde@Sun.COM 	} else {
894412329SJerry.Gilliam@Sun.COM 		rv = mod_hash_insert(ddi_aliases.dali_alias_TLB,
894512116SVikram.Hegde@Sun.COM 		    (mod_hash_key_t)aliasdup, (mod_hash_val_t)NULL);
894612329SJerry.Gilliam@Sun.COM 		if (rv != 0) {
894712329SJerry.Gilliam@Sun.COM 			strfree(aliasdup);
894812329SJerry.Gilliam@Sun.COM 		}
894912116SVikram.Hegde@Sun.COM 		if (curr)
895012116SVikram.Hegde@Sun.COM 			kmem_free(curr, MAXPATHLEN);
895112116SVikram.Hegde@Sun.COM 	}
895212116SVikram.Hegde@Sun.COM 
895312116SVikram.Hegde@Sun.COM 	return (currdip);
895412116SVikram.Hegde@Sun.COM }
895512116SVikram.Hegde@Sun.COM 
895612116SVikram.Hegde@Sun.COM char *
ddi_curr_to_alias(char * curr,int i)895712116SVikram.Hegde@Sun.COM ddi_curr_to_alias(char *curr, int i)
895812116SVikram.Hegde@Sun.COM {
895912116SVikram.Hegde@Sun.COM 	alias_pair_t	*pair;
896012116SVikram.Hegde@Sun.COM 	char		*alias;
896112116SVikram.Hegde@Sun.COM 	char		*currdup;
896212116SVikram.Hegde@Sun.COM 	int		len;
896312329SJerry.Gilliam@Sun.COM 	int		rv;
896412116SVikram.Hegde@Sun.COM 
896512116SVikram.Hegde@Sun.COM 	pair = &(ddi_aliases.dali_curr_pairs[i]);
896612116SVikram.Hegde@Sun.COM 
896712116SVikram.Hegde@Sun.COM 	len = strlen(pair->pair_curr);
896812116SVikram.Hegde@Sun.COM 
896912116SVikram.Hegde@Sun.COM 	alias = NULL;
897012116SVikram.Hegde@Sun.COM 
897112116SVikram.Hegde@Sun.COM 	currdup = ddi_strdup(curr, KM_NOSLEEP);
897212116SVikram.Hegde@Sun.COM 	if (currdup == NULL) {
897312116SVikram.Hegde@Sun.COM 		cmn_err(CE_PANIC, "currdup alloc failed");
897412116SVikram.Hegde@Sun.COM 		/*NOTREACHED*/
897512116SVikram.Hegde@Sun.COM 	}
897612116SVikram.Hegde@Sun.COM 
897712116SVikram.Hegde@Sun.COM 	if (strncmp(curr, pair->pair_curr, len) != 0)
897812116SVikram.Hegde@Sun.COM 		goto out;
897912116SVikram.Hegde@Sun.COM 
898012116SVikram.Hegde@Sun.COM 	if (curr[len] != '/' && curr[len] != '\0')
898112116SVikram.Hegde@Sun.COM 		goto out;
898212116SVikram.Hegde@Sun.COM 
898312116SVikram.Hegde@Sun.COM 	alias = kmem_alloc(MAXPATHLEN, KM_NOSLEEP);
898412116SVikram.Hegde@Sun.COM 	if (alias == NULL) {
898512116SVikram.Hegde@Sun.COM 		cmn_err(CE_PANIC, "alias alloc failed");
898612116SVikram.Hegde@Sun.COM 		/*NOTREACHED*/
898712116SVikram.Hegde@Sun.COM 	}
898812116SVikram.Hegde@Sun.COM 
898912116SVikram.Hegde@Sun.COM 	(void) strlcpy(alias, pair->pair_alias, MAXPATHLEN);
899012116SVikram.Hegde@Sun.COM 	if (curr[len] == '/') {
899112116SVikram.Hegde@Sun.COM 		(void) strlcat(alias, "/", MAXPATHLEN);
899212116SVikram.Hegde@Sun.COM 		(void) strlcat(alias, &curr[len + 1], MAXPATHLEN);
899312116SVikram.Hegde@Sun.COM 	}
899412116SVikram.Hegde@Sun.COM 
899512116SVikram.Hegde@Sun.COM 	if (e_ddi_path_to_instance(alias) == NULL) {
899612116SVikram.Hegde@Sun.COM 		kmem_free(alias, MAXPATHLEN);
899712116SVikram.Hegde@Sun.COM 		alias = NULL;
899812116SVikram.Hegde@Sun.COM 	}
899912116SVikram.Hegde@Sun.COM 
900012116SVikram.Hegde@Sun.COM out:
900112329SJerry.Gilliam@Sun.COM 	rv = mod_hash_insert(ddi_aliases.dali_curr_TLB,
900212116SVikram.Hegde@Sun.COM 	    (mod_hash_key_t)currdup, (mod_hash_val_t)alias);
900312329SJerry.Gilliam@Sun.COM 	if (rv != 0) {
900412329SJerry.Gilliam@Sun.COM 		strfree(currdup);
900512329SJerry.Gilliam@Sun.COM 	}
900612116SVikram.Hegde@Sun.COM 
900712116SVikram.Hegde@Sun.COM 	return (alias);
900812116SVikram.Hegde@Sun.COM }
900912116SVikram.Hegde@Sun.COM 
901012116SVikram.Hegde@Sun.COM dev_info_t *
ddi_alias_redirect(char * alias)901112116SVikram.Hegde@Sun.COM ddi_alias_redirect(char *alias)
901212116SVikram.Hegde@Sun.COM {
901312116SVikram.Hegde@Sun.COM 	char		*curr;
901412116SVikram.Hegde@Sun.COM 	dev_info_t	*currdip;
901512116SVikram.Hegde@Sun.COM 	int		i;
901612116SVikram.Hegde@Sun.COM 
901712116SVikram.Hegde@Sun.COM 	if (ddi_aliases_present == B_FALSE)
901812116SVikram.Hegde@Sun.COM 		return (NULL);
901912116SVikram.Hegde@Sun.COM 
902012116SVikram.Hegde@Sun.COM 	if (tsd_get(tsd_ddi_redirect))
902112116SVikram.Hegde@Sun.COM 		return (NULL);
902212116SVikram.Hegde@Sun.COM 
902312116SVikram.Hegde@Sun.COM 	(void) tsd_set(tsd_ddi_redirect, (void *)1);
902412116SVikram.Hegde@Sun.COM 
902512116SVikram.Hegde@Sun.COM 	ASSERT(ddi_aliases.dali_alias_TLB);
902612116SVikram.Hegde@Sun.COM 	ASSERT(ddi_aliases.dali_alias_pairs);
902712116SVikram.Hegde@Sun.COM 
902812116SVikram.Hegde@Sun.COM 	curr = NULL;
902912116SVikram.Hegde@Sun.COM 	if (mod_hash_find(ddi_aliases.dali_alias_TLB,
903012116SVikram.Hegde@Sun.COM 	    (mod_hash_key_t)alias, (mod_hash_val_t *)&curr) == 0) {
903112116SVikram.Hegde@Sun.COM 		currdip = curr ? path_to_dip(curr) : NULL;
903212116SVikram.Hegde@Sun.COM 		goto out;
903312116SVikram.Hegde@Sun.COM 	}
903412116SVikram.Hegde@Sun.COM 
903512116SVikram.Hegde@Sun.COM 	/* The TLB has no translation, do it the hard way */
903612116SVikram.Hegde@Sun.COM 	currdip = NULL;
903712116SVikram.Hegde@Sun.COM 	for (i = ddi_aliases.dali_num_pairs - 1; i >= 0; i--) {
903812116SVikram.Hegde@Sun.COM 		currdip = ddi_alias_to_currdip(alias, i);
903912116SVikram.Hegde@Sun.COM 		if (currdip)
904012116SVikram.Hegde@Sun.COM 			break;
904112116SVikram.Hegde@Sun.COM 	}
904212116SVikram.Hegde@Sun.COM out:
904312116SVikram.Hegde@Sun.COM 	(void) tsd_set(tsd_ddi_redirect, NULL);
904412116SVikram.Hegde@Sun.COM 
904512116SVikram.Hegde@Sun.COM 	return (currdip);
904612116SVikram.Hegde@Sun.COM }
904712116SVikram.Hegde@Sun.COM 
904812116SVikram.Hegde@Sun.COM char *
ddi_curr_redirect(char * curr)904912116SVikram.Hegde@Sun.COM ddi_curr_redirect(char *curr)
905012116SVikram.Hegde@Sun.COM {
905112116SVikram.Hegde@Sun.COM 	char 	*alias;
905212116SVikram.Hegde@Sun.COM 	int i;
905312116SVikram.Hegde@Sun.COM 
905412116SVikram.Hegde@Sun.COM 	if (ddi_aliases_present == B_FALSE)
905512116SVikram.Hegde@Sun.COM 		return (NULL);
905612116SVikram.Hegde@Sun.COM 
905712116SVikram.Hegde@Sun.COM 	if (tsd_get(tsd_ddi_redirect))
905812116SVikram.Hegde@Sun.COM 		return (NULL);
905912116SVikram.Hegde@Sun.COM 
906012116SVikram.Hegde@Sun.COM 	(void) tsd_set(tsd_ddi_redirect, (void *)1);
906112116SVikram.Hegde@Sun.COM 
906212116SVikram.Hegde@Sun.COM 	ASSERT(ddi_aliases.dali_curr_TLB);
906312116SVikram.Hegde@Sun.COM 	ASSERT(ddi_aliases.dali_curr_pairs);
906412116SVikram.Hegde@Sun.COM 
906512116SVikram.Hegde@Sun.COM 	alias = NULL;
906612116SVikram.Hegde@Sun.COM 	if (mod_hash_find(ddi_aliases.dali_curr_TLB,
906712116SVikram.Hegde@Sun.COM 	    (mod_hash_key_t)curr, (mod_hash_val_t *)&alias) == 0) {
906812116SVikram.Hegde@Sun.COM 		goto out;
906912116SVikram.Hegde@Sun.COM 	}
907012116SVikram.Hegde@Sun.COM 
907112116SVikram.Hegde@Sun.COM 
907212116SVikram.Hegde@Sun.COM 	/* The TLB has no translation, do it the slow way */
907312116SVikram.Hegde@Sun.COM 	alias = NULL;
907412116SVikram.Hegde@Sun.COM 	for (i = ddi_aliases.dali_num_pairs - 1; i >= 0; i--) {
907512116SVikram.Hegde@Sun.COM 		alias = ddi_curr_to_alias(curr, i);
907612116SVikram.Hegde@Sun.COM 		if (alias)
907712116SVikram.Hegde@Sun.COM 			break;
907812116SVikram.Hegde@Sun.COM 	}
907912116SVikram.Hegde@Sun.COM 
908012116SVikram.Hegde@Sun.COM out:
908112116SVikram.Hegde@Sun.COM 	(void) tsd_set(tsd_ddi_redirect, NULL);
908212116SVikram.Hegde@Sun.COM 
908312116SVikram.Hegde@Sun.COM 	return (alias);
908412116SVikram.Hegde@Sun.COM }
908512116SVikram.Hegde@Sun.COM 
908611600SVikram.Hegde@Sun.COM void
ddi_err(ddi_err_t ade,dev_info_t * rdip,const char * fmt,...)908711600SVikram.Hegde@Sun.COM ddi_err(ddi_err_t ade, dev_info_t *rdip, const char *fmt, ...)
908811600SVikram.Hegde@Sun.COM {
908911600SVikram.Hegde@Sun.COM 	va_list ap;
909011600SVikram.Hegde@Sun.COM 	char strbuf[256];
909111600SVikram.Hegde@Sun.COM 	char *buf;
909211600SVikram.Hegde@Sun.COM 	size_t buflen, tlen;
909311600SVikram.Hegde@Sun.COM 	int ce;
909411600SVikram.Hegde@Sun.COM 	int de;
909511600SVikram.Hegde@Sun.COM 	const char *fmtbad = "Invalid arguments to ddi_err()";
909611600SVikram.Hegde@Sun.COM 
909711600SVikram.Hegde@Sun.COM 	de = DER_CONT;
909811600SVikram.Hegde@Sun.COM 	strbuf[1] = '\0';
909911600SVikram.Hegde@Sun.COM 
910011600SVikram.Hegde@Sun.COM 	switch (ade) {
910111600SVikram.Hegde@Sun.COM 	case DER_CONS:
910211600SVikram.Hegde@Sun.COM 		strbuf[0] = '^';
910311600SVikram.Hegde@Sun.COM 		break;
910411600SVikram.Hegde@Sun.COM 	case DER_LOG:
910511600SVikram.Hegde@Sun.COM 		strbuf[0] = '!';
910611600SVikram.Hegde@Sun.COM 		break;
910711600SVikram.Hegde@Sun.COM 	case DER_VERB:
910811600SVikram.Hegde@Sun.COM 		strbuf[0] = '?';
910911600SVikram.Hegde@Sun.COM 		break;
911011600SVikram.Hegde@Sun.COM 	default:
911111600SVikram.Hegde@Sun.COM 		strbuf[0] = '\0';
911211600SVikram.Hegde@Sun.COM 		de = ade;
911311600SVikram.Hegde@Sun.COM 		break;
911411600SVikram.Hegde@Sun.COM 	}
911511600SVikram.Hegde@Sun.COM 
911611600SVikram.Hegde@Sun.COM 	tlen = strlen(strbuf);
911711600SVikram.Hegde@Sun.COM 	buf = strbuf + tlen;
911811600SVikram.Hegde@Sun.COM 	buflen = sizeof (strbuf) - tlen;
911911600SVikram.Hegde@Sun.COM 
912011600SVikram.Hegde@Sun.COM 	if (rdip && ddi_get_instance(rdip) == -1) {
912111600SVikram.Hegde@Sun.COM 		(void) snprintf(buf, buflen, "%s: ",
912211600SVikram.Hegde@Sun.COM 		    ddi_driver_name(rdip));
912311600SVikram.Hegde@Sun.COM 	} else if (rdip) {
912411600SVikram.Hegde@Sun.COM 		(void) snprintf(buf, buflen, "%s%d: ",
912511600SVikram.Hegde@Sun.COM 		    ddi_driver_name(rdip), ddi_get_instance(rdip));
912611600SVikram.Hegde@Sun.COM 	}
912711600SVikram.Hegde@Sun.COM 
912811600SVikram.Hegde@Sun.COM 	tlen = strlen(strbuf);
912911600SVikram.Hegde@Sun.COM 	buf = strbuf + tlen;
913011600SVikram.Hegde@Sun.COM 	buflen = sizeof (strbuf) - tlen;
913111600SVikram.Hegde@Sun.COM 
913211600SVikram.Hegde@Sun.COM 	va_start(ap, fmt);
913311600SVikram.Hegde@Sun.COM 	switch (de) {
913411600SVikram.Hegde@Sun.COM 	case DER_CONT:
913511600SVikram.Hegde@Sun.COM 		(void) vsnprintf(buf, buflen, fmt, ap);
913611600SVikram.Hegde@Sun.COM 		if (ade != DER_CONT) {
913711600SVikram.Hegde@Sun.COM 			(void) strlcat(strbuf, "\n", sizeof (strbuf));
913811600SVikram.Hegde@Sun.COM 		}
913911600SVikram.Hegde@Sun.COM 		ce = CE_CONT;
914011600SVikram.Hegde@Sun.COM 		break;
914111600SVikram.Hegde@Sun.COM 	case DER_NOTE:
914211600SVikram.Hegde@Sun.COM 		(void) vsnprintf(buf, buflen, fmt, ap);
914311600SVikram.Hegde@Sun.COM 		ce = CE_NOTE;
914411600SVikram.Hegde@Sun.COM 		break;
914511600SVikram.Hegde@Sun.COM 	case DER_WARN:
914611600SVikram.Hegde@Sun.COM 		(void) vsnprintf(buf, buflen, fmt, ap);
914711600SVikram.Hegde@Sun.COM 		ce = CE_WARN;
914811600SVikram.Hegde@Sun.COM 		break;
914911600SVikram.Hegde@Sun.COM 	case DER_MODE:
915011600SVikram.Hegde@Sun.COM 		(void) vsnprintf(buf, buflen, fmt, ap);
915111600SVikram.Hegde@Sun.COM 		if (ddi_err_panic == B_TRUE) {
915211600SVikram.Hegde@Sun.COM 			ce = CE_PANIC;
915311600SVikram.Hegde@Sun.COM 		} else {
915411600SVikram.Hegde@Sun.COM 			ce = CE_WARN;
915511600SVikram.Hegde@Sun.COM 		}
915611600SVikram.Hegde@Sun.COM 		break;
915711600SVikram.Hegde@Sun.COM 	case DER_DEBUG:
915811600SVikram.Hegde@Sun.COM 		(void) snprintf(buf, buflen, "DEBUG: ");
915911600SVikram.Hegde@Sun.COM 		tlen = strlen("DEBUG: ");
916011600SVikram.Hegde@Sun.COM 		(void) vsnprintf(buf + tlen, buflen - tlen, fmt, ap);
916111600SVikram.Hegde@Sun.COM 		ce = CE_CONT;
916211600SVikram.Hegde@Sun.COM 		break;
916311600SVikram.Hegde@Sun.COM 	case DER_PANIC:
916411600SVikram.Hegde@Sun.COM 		(void) vsnprintf(buf, buflen, fmt, ap);
916511600SVikram.Hegde@Sun.COM 		ce = CE_PANIC;
916611600SVikram.Hegde@Sun.COM 		break;
916711600SVikram.Hegde@Sun.COM 	case DER_INVALID:
916811600SVikram.Hegde@Sun.COM 	default:
916911600SVikram.Hegde@Sun.COM 		(void) snprintf(buf, buflen, fmtbad);
917011600SVikram.Hegde@Sun.COM 		tlen = strlen(fmtbad);
917111600SVikram.Hegde@Sun.COM 		(void) vsnprintf(buf + tlen, buflen - tlen, fmt, ap);
917211600SVikram.Hegde@Sun.COM 		ce = CE_PANIC;
917311600SVikram.Hegde@Sun.COM 		break;
917411600SVikram.Hegde@Sun.COM 	}
917511600SVikram.Hegde@Sun.COM 	va_end(ap);
917611600SVikram.Hegde@Sun.COM 
917711600SVikram.Hegde@Sun.COM 	cmn_err(ce, strbuf);
917811600SVikram.Hegde@Sun.COM }
917911600SVikram.Hegde@Sun.COM 
918011600SVikram.Hegde@Sun.COM /*ARGSUSED*/
918111600SVikram.Hegde@Sun.COM void
ddi_mem_update(uint64_t addr,uint64_t size)918211600SVikram.Hegde@Sun.COM ddi_mem_update(uint64_t addr, uint64_t size)
918311600SVikram.Hegde@Sun.COM {
918411600SVikram.Hegde@Sun.COM #if defined(__x86) && !defined(__xpv)
918511600SVikram.Hegde@Sun.COM 	extern void immu_physmem_update(uint64_t addr, uint64_t size);
918611600SVikram.Hegde@Sun.COM 	immu_physmem_update(addr, size);
918711600SVikram.Hegde@Sun.COM #else
918811600SVikram.Hegde@Sun.COM 	/*LINTED*/
918911600SVikram.Hegde@Sun.COM 	;
919011600SVikram.Hegde@Sun.COM #endif
919111600SVikram.Hegde@Sun.COM }
9192