xref: /titanic_53/usr/src/uts/common/os/devcfg.c (revision 193974072f41a843678abf5f61979c748687e66b)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5144dfaa9Scth  * Common Development and Distribution License (the "License").
6144dfaa9Scth  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22d62bc4baSyz147064  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/note.h>
277c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
287c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
297c478bd9Sstevel@tonic-gate #include <sys/instance.h>
307c478bd9Sstevel@tonic-gate #include <sys/conf.h>
317c478bd9Sstevel@tonic-gate #include <sys/stat.h>
327c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
337c478bd9Sstevel@tonic-gate #include <sys/hwconf.h>
347c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
357c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
367c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
377c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
387c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
3925e8c5aaSvikram #include <sys/contract/device_impl.h>
407c478bd9Sstevel@tonic-gate #include <sys/dacf.h>
417c478bd9Sstevel@tonic-gate #include <sys/promif.h>
42*19397407SSherry Moore #include <sys/pci.h>
437c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
447c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
457c478bd9Sstevel@tonic-gate #include <sys/taskq.h>
467c478bd9Sstevel@tonic-gate #include <sys/sysevent.h>
477c478bd9Sstevel@tonic-gate #include <sys/sunmdi.h>
487c478bd9Sstevel@tonic-gate #include <sys/stream.h>
497c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
507c478bd9Sstevel@tonic-gate #include <sys/fs/snode.h>
517c478bd9Sstevel@tonic-gate #include <sys/fs/dv_node.h>
52facf4a8dSllai1 #include <sys/reboot.h>
5325e8c5aaSvikram #include <sys/sysmacros.h>
54*19397407SSherry Moore #include <sys/systm.h>
5525e8c5aaSvikram #include <sys/sunldi.h>
5625e8c5aaSvikram #include <sys/sunldi_impl.h>
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #ifdef DEBUG
597c478bd9Sstevel@tonic-gate int ddidebug = DDI_AUDIT;
607c478bd9Sstevel@tonic-gate #else
617c478bd9Sstevel@tonic-gate int ddidebug = 0;
627c478bd9Sstevel@tonic-gate #endif
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #define	MT_CONFIG_OP	0
657c478bd9Sstevel@tonic-gate #define	MT_UNCONFIG_OP	1
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /* Multi-threaded configuration */
687c478bd9Sstevel@tonic-gate struct mt_config_handle {
697c478bd9Sstevel@tonic-gate 	kmutex_t mtc_lock;
707c478bd9Sstevel@tonic-gate 	kcondvar_t mtc_cv;
717c478bd9Sstevel@tonic-gate 	int mtc_thr_count;
727c478bd9Sstevel@tonic-gate 	dev_info_t *mtc_pdip;	/* parent dip for mt_config_children */
737c478bd9Sstevel@tonic-gate 	dev_info_t **mtc_fdip;	/* "a" dip where unconfigure failed */
747c478bd9Sstevel@tonic-gate 	major_t mtc_parmajor;	/* parent major for mt_config_driver */
757c478bd9Sstevel@tonic-gate 	major_t mtc_major;
767c478bd9Sstevel@tonic-gate 	int mtc_flags;
777c478bd9Sstevel@tonic-gate 	int mtc_op;		/* config or unconfig */
787c478bd9Sstevel@tonic-gate 	int mtc_error;		/* operation error */
797c478bd9Sstevel@tonic-gate 	struct brevq_node **mtc_brevqp;	/* outstanding branch events queue */
807c478bd9Sstevel@tonic-gate #ifdef DEBUG
817c478bd9Sstevel@tonic-gate 	int total_time;
827c478bd9Sstevel@tonic-gate 	timestruc_t start_time;
837c478bd9Sstevel@tonic-gate #endif /* DEBUG */
847c478bd9Sstevel@tonic-gate };
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate struct devi_nodeid {
87fa9e4066Sahrens 	pnode_t nodeid;
887c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
897c478bd9Sstevel@tonic-gate 	struct devi_nodeid *next;
907c478bd9Sstevel@tonic-gate };
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate struct devi_nodeid_list {
937c478bd9Sstevel@tonic-gate 	kmutex_t dno_lock;		/* Protects other fields */
947c478bd9Sstevel@tonic-gate 	struct devi_nodeid *dno_head;	/* list of devi nodeid elements */
957c478bd9Sstevel@tonic-gate 	struct devi_nodeid *dno_free;	/* Free list */
967c478bd9Sstevel@tonic-gate 	uint_t dno_list_length;		/* number of dips in list */
977c478bd9Sstevel@tonic-gate };
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /* used to keep track of branch remove events to be generated */
1007c478bd9Sstevel@tonic-gate struct brevq_node {
101245c82d9Scth 	char *brn_deviname;
102245c82d9Scth 	struct brevq_node *brn_sibling;
103245c82d9Scth 	struct brevq_node *brn_child;
1047c478bd9Sstevel@tonic-gate };
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate static struct devi_nodeid_list devi_nodeid_list;
1077c478bd9Sstevel@tonic-gate static struct devi_nodeid_list *devimap = &devi_nodeid_list;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate  * Well known nodes which are attached first at boot time.
1117c478bd9Sstevel@tonic-gate  */
1127c478bd9Sstevel@tonic-gate dev_info_t *top_devinfo;		/* root of device tree */
1137c478bd9Sstevel@tonic-gate dev_info_t *options_dip;
1147c478bd9Sstevel@tonic-gate dev_info_t *pseudo_dip;
1157c478bd9Sstevel@tonic-gate dev_info_t *clone_dip;
1167c478bd9Sstevel@tonic-gate dev_info_t *scsi_vhci_dip;		/* MPXIO dip */
1177c478bd9Sstevel@tonic-gate major_t clone_major;
1187c478bd9Sstevel@tonic-gate 
119facf4a8dSllai1 /*
120facf4a8dSllai1  * A non-global zone's /dev is derived from the device tree.
121facf4a8dSllai1  * This generation number serves to indicate when a zone's
122facf4a8dSllai1  * /dev may need to be updated.
123facf4a8dSllai1  */
124facf4a8dSllai1 volatile ulong_t devtree_gen;		/* generation number */
125facf4a8dSllai1 
1267c478bd9Sstevel@tonic-gate /* block all future dev_info state changes */
127*19397407SSherry Moore hrtime_t volatile devinfo_freeze = 0;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate /* number of dev_info attaches/detaches currently in progress */
1307c478bd9Sstevel@tonic-gate static ulong_t devinfo_attach_detach = 0;
1317c478bd9Sstevel@tonic-gate 
132c3b4ae18SJerry Gilliam extern int	sys_shutdown;
1337c478bd9Sstevel@tonic-gate extern kmutex_t global_vhci_lock;
1347c478bd9Sstevel@tonic-gate 
135facf4a8dSllai1 /* bitset of DS_SYSAVAIL & DS_RECONFIG - no races, no lock */
136facf4a8dSllai1 static int devname_state = 0;
137facf4a8dSllai1 
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate  * The devinfo snapshot cache and related variables.
1407c478bd9Sstevel@tonic-gate  * The only field in the di_cache structure that needs initialization
1417c478bd9Sstevel@tonic-gate  * is the mutex (cache_lock). However, since this is an adaptive mutex
1427c478bd9Sstevel@tonic-gate  * (MUTEX_DEFAULT) - it is automatically initialized by being allocated
1437c478bd9Sstevel@tonic-gate  * in zeroed memory (static storage class). Therefore no explicit
1447c478bd9Sstevel@tonic-gate  * initialization of the di_cache structure is needed.
1457c478bd9Sstevel@tonic-gate  */
1467c478bd9Sstevel@tonic-gate struct di_cache	di_cache = {1};
1477c478bd9Sstevel@tonic-gate int		di_cache_debug = 0;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate /* For ddvis, which needs pseudo children under PCI */
1507c478bd9Sstevel@tonic-gate int pci_allow_pseudo_children = 0;
1517c478bd9Sstevel@tonic-gate 
152f4da9be0Scth /* Allow path-oriented alias driver binding on driver.conf enumerated nodes */
153f4da9be0Scth int driver_conf_allow_path_alias = 1;
154f4da9be0Scth 
1557c478bd9Sstevel@tonic-gate /*
1567c478bd9Sstevel@tonic-gate  * The following switch is for service people, in case a
1577c478bd9Sstevel@tonic-gate  * 3rd party driver depends on identify(9e) being called.
1587c478bd9Sstevel@tonic-gate  */
1597c478bd9Sstevel@tonic-gate int identify_9e = 0;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate int mtc_off;					/* turn off mt config */
1627c478bd9Sstevel@tonic-gate 
163*19397407SSherry Moore int quiesce_debug = 0;
164*19397407SSherry Moore 
1657c478bd9Sstevel@tonic-gate static kmem_cache_t *ddi_node_cache;		/* devinfo node cache */
1667c478bd9Sstevel@tonic-gate static devinfo_log_header_t *devinfo_audit_log;	/* devinfo log */
1677c478bd9Sstevel@tonic-gate static int devinfo_log_size;			/* size in pages */
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate static int lookup_compatible(dev_info_t *, uint_t);
1707c478bd9Sstevel@tonic-gate static char *encode_composite_string(char **, uint_t, size_t *, uint_t);
1717c478bd9Sstevel@tonic-gate static void link_to_driver_list(dev_info_t *);
1727c478bd9Sstevel@tonic-gate static void unlink_from_driver_list(dev_info_t *);
1737c478bd9Sstevel@tonic-gate static void add_to_dn_list(struct devnames *, dev_info_t *);
1747c478bd9Sstevel@tonic-gate static void remove_from_dn_list(struct devnames *, dev_info_t *);
1757c478bd9Sstevel@tonic-gate static dev_info_t *find_child_by_callback(dev_info_t *, char *, char *,
1767c478bd9Sstevel@tonic-gate     int (*)(dev_info_t *, char *, int));
1777c478bd9Sstevel@tonic-gate static dev_info_t *find_duplicate_child();
1787c478bd9Sstevel@tonic-gate static void add_global_props(dev_info_t *);
1797c478bd9Sstevel@tonic-gate static void remove_global_props(dev_info_t *);
1807c478bd9Sstevel@tonic-gate static int uninit_node(dev_info_t *);
1817c478bd9Sstevel@tonic-gate static void da_log_init(void);
1827c478bd9Sstevel@tonic-gate static void da_log_enter(dev_info_t *);
1837c478bd9Sstevel@tonic-gate static int walk_devs(dev_info_t *, int (*f)(dev_info_t *, void *), void *, int);
1847c478bd9Sstevel@tonic-gate static int reset_nexus_flags(dev_info_t *, void *);
1857c478bd9Sstevel@tonic-gate static void ddi_optimize_dtree(dev_info_t *);
1867c478bd9Sstevel@tonic-gate static int is_leaf_node(dev_info_t *);
1877c478bd9Sstevel@tonic-gate static struct mt_config_handle *mt_config_init(dev_info_t *, dev_info_t **,
1887c478bd9Sstevel@tonic-gate     int, major_t, int, struct brevq_node **);
1897c478bd9Sstevel@tonic-gate static void mt_config_children(struct mt_config_handle *);
1907c478bd9Sstevel@tonic-gate static void mt_config_driver(struct mt_config_handle *);
1917c478bd9Sstevel@tonic-gate static int mt_config_fini(struct mt_config_handle *);
1927c478bd9Sstevel@tonic-gate static int devi_unconfig_common(dev_info_t *, dev_info_t **, int, major_t,
1937c478bd9Sstevel@tonic-gate     struct brevq_node **);
1947c478bd9Sstevel@tonic-gate static int
1957c478bd9Sstevel@tonic-gate ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
1967c478bd9Sstevel@tonic-gate     dev_info_t **childp, int flags);
1979d3d2ed0Shiremath static void i_link_vhci_node(dev_info_t *);
1985e3986cbScth static void ndi_devi_exit_and_wait(dev_info_t *dip,
1995e3986cbScth     int circular, clock_t end_time);
200f4da9be0Scth static int ndi_devi_unbind_driver(dev_info_t *dip);
2017c478bd9Sstevel@tonic-gate 
20225e8c5aaSvikram static void i_ddi_check_retire(dev_info_t *dip);
20325e8c5aaSvikram 
204*19397407SSherry Moore static void quiesce_one_device(dev_info_t *, void *);
20525e8c5aaSvikram 
2067c478bd9Sstevel@tonic-gate /*
2077c478bd9Sstevel@tonic-gate  * dev_info cache and node management
2087c478bd9Sstevel@tonic-gate  */
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate /* initialize dev_info node cache */
2117c478bd9Sstevel@tonic-gate void
2127c478bd9Sstevel@tonic-gate i_ddi_node_cache_init()
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate 	ASSERT(ddi_node_cache == NULL);
2157c478bd9Sstevel@tonic-gate 	ddi_node_cache = kmem_cache_create("dev_info_node_cache",
2167c478bd9Sstevel@tonic-gate 	    sizeof (struct dev_info), 0, NULL, NULL, NULL, NULL, NULL, 0);
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	if (ddidebug & DDI_AUDIT)
2197c478bd9Sstevel@tonic-gate 		da_log_init();
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate /*
2237c478bd9Sstevel@tonic-gate  * Allocating a dev_info node, callable from interrupt context with KM_NOSLEEP
2247c478bd9Sstevel@tonic-gate  * The allocated node has a reference count of 0.
2257c478bd9Sstevel@tonic-gate  */
2267c478bd9Sstevel@tonic-gate dev_info_t *
227fa9e4066Sahrens i_ddi_alloc_node(dev_info_t *pdip, char *node_name, pnode_t nodeid,
2287c478bd9Sstevel@tonic-gate     int instance, ddi_prop_t *sys_prop, int flag)
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate 	struct dev_info *devi;
2317c478bd9Sstevel@tonic-gate 	struct devi_nodeid *elem;
2327c478bd9Sstevel@tonic-gate 	static char failed[] = "i_ddi_alloc_node: out of memory";
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	ASSERT(node_name != NULL);
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	if ((devi = kmem_cache_alloc(ddi_node_cache, flag)) == NULL) {
2377c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, failed);
2387c478bd9Sstevel@tonic-gate 		return (NULL);
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	bzero(devi, sizeof (struct dev_info));
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	if (devinfo_audit_log) {
2447c478bd9Sstevel@tonic-gate 		devi->devi_audit = kmem_zalloc(sizeof (devinfo_audit_t), flag);
2457c478bd9Sstevel@tonic-gate 		if (devi->devi_audit == NULL)
2467c478bd9Sstevel@tonic-gate 			goto fail;
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	if ((devi->devi_node_name = i_ddi_strdup(node_name, flag)) == NULL)
2507c478bd9Sstevel@tonic-gate 		goto fail;
251f4da9be0Scth 
2527c478bd9Sstevel@tonic-gate 	/* default binding name is node name */
2537c478bd9Sstevel@tonic-gate 	devi->devi_binding_name = devi->devi_node_name;
254a204de77Scth 	devi->devi_major = DDI_MAJOR_T_NONE;	/* unbound by default */
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	/*
2577c478bd9Sstevel@tonic-gate 	 * Make a copy of system property
2587c478bd9Sstevel@tonic-gate 	 */
2597c478bd9Sstevel@tonic-gate 	if (sys_prop &&
2607c478bd9Sstevel@tonic-gate 	    (devi->devi_sys_prop_ptr = i_ddi_prop_list_dup(sys_prop, flag))
2617c478bd9Sstevel@tonic-gate 	    == NULL)
2627c478bd9Sstevel@tonic-gate 		goto fail;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	/*
2657c478bd9Sstevel@tonic-gate 	 * Assign devi_nodeid, devi_node_class, devi_node_attributes
2667c478bd9Sstevel@tonic-gate 	 * according to the following algorithm:
2677c478bd9Sstevel@tonic-gate 	 *
2687c478bd9Sstevel@tonic-gate 	 * nodeid arg			node class		node attributes
2697c478bd9Sstevel@tonic-gate 	 *
2707c478bd9Sstevel@tonic-gate 	 * DEVI_PSEUDO_NODEID		DDI_NC_PSEUDO		A
2717c478bd9Sstevel@tonic-gate 	 * DEVI_SID_NODEID		DDI_NC_PSEUDO		A,P
2727c478bd9Sstevel@tonic-gate 	 * other			DDI_NC_PROM		P
2737c478bd9Sstevel@tonic-gate 	 *
2747c478bd9Sstevel@tonic-gate 	 * Where A = DDI_AUTO_ASSIGNED_NODEID (auto-assign a nodeid)
2757c478bd9Sstevel@tonic-gate 	 * and	 P = DDI_PERSISTENT
2767c478bd9Sstevel@tonic-gate 	 *
2777c478bd9Sstevel@tonic-gate 	 * auto-assigned nodeids are also auto-freed.
2787c478bd9Sstevel@tonic-gate 	 */
2797c478bd9Sstevel@tonic-gate 	switch (nodeid) {
2807c478bd9Sstevel@tonic-gate 	case DEVI_SID_NODEID:
2817c478bd9Sstevel@tonic-gate 		devi->devi_node_attributes = DDI_PERSISTENT;
2827c478bd9Sstevel@tonic-gate 		if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL)
2837c478bd9Sstevel@tonic-gate 			goto fail;
2847c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
2857c478bd9Sstevel@tonic-gate 	case DEVI_PSEUDO_NODEID:
2867c478bd9Sstevel@tonic-gate 		devi->devi_node_attributes |= DDI_AUTO_ASSIGNED_NODEID;
2877c478bd9Sstevel@tonic-gate 		devi->devi_node_class = DDI_NC_PSEUDO;
2887c478bd9Sstevel@tonic-gate 		if (impl_ddi_alloc_nodeid(&devi->devi_nodeid)) {
2897c478bd9Sstevel@tonic-gate 			panic("i_ddi_alloc_node: out of nodeids");
2907c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
2917c478bd9Sstevel@tonic-gate 		}
2927c478bd9Sstevel@tonic-gate 		break;
2937c478bd9Sstevel@tonic-gate 	default:
2947c478bd9Sstevel@tonic-gate 		if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL)
2957c478bd9Sstevel@tonic-gate 			goto fail;
2967c478bd9Sstevel@tonic-gate 		/*
2977c478bd9Sstevel@tonic-gate 		 * the nodetype is 'prom', try to 'take' the nodeid now.
2987c478bd9Sstevel@tonic-gate 		 * This requires memory allocation, so check for failure.
2997c478bd9Sstevel@tonic-gate 		 */
3007c478bd9Sstevel@tonic-gate 		if (impl_ddi_take_nodeid(nodeid, flag) != 0) {
3017c478bd9Sstevel@tonic-gate 			kmem_free(elem, sizeof (*elem));
3027c478bd9Sstevel@tonic-gate 			goto fail;
3037c478bd9Sstevel@tonic-gate 		}
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 		devi->devi_nodeid = nodeid;
3067c478bd9Sstevel@tonic-gate 		devi->devi_node_class = DDI_NC_PROM;
3077c478bd9Sstevel@tonic-gate 		devi->devi_node_attributes = DDI_PERSISTENT;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	}
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node((dev_info_t *)devi)) {
3127c478bd9Sstevel@tonic-gate 		mutex_enter(&devimap->dno_lock);
3137c478bd9Sstevel@tonic-gate 		elem->next = devimap->dno_free;
3147c478bd9Sstevel@tonic-gate 		devimap->dno_free = elem;
3157c478bd9Sstevel@tonic-gate 		mutex_exit(&devimap->dno_lock);
3167c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	/*
3197c478bd9Sstevel@tonic-gate 	 * Instance is normally initialized to -1. In a few special
3207c478bd9Sstevel@tonic-gate 	 * cases, the caller may specify an instance (e.g. CPU nodes).
3217c478bd9Sstevel@tonic-gate 	 */
3227c478bd9Sstevel@tonic-gate 	devi->devi_instance = instance;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	/*
3257c478bd9Sstevel@tonic-gate 	 * set parent and bus_ctl parent
3267c478bd9Sstevel@tonic-gate 	 */
3277c478bd9Sstevel@tonic-gate 	devi->devi_parent = DEVI(pdip);
3287c478bd9Sstevel@tonic-gate 	devi->devi_bus_ctl = DEVI(pdip);
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
3317c478bd9Sstevel@tonic-gate 	    "i_ddi_alloc_node: name=%s id=%d\n", node_name, devi->devi_nodeid));
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	cv_init(&(devi->devi_cv), NULL, CV_DEFAULT, NULL);
3347c478bd9Sstevel@tonic-gate 	mutex_init(&(devi->devi_lock), NULL, MUTEX_DEFAULT, NULL);
3357c478bd9Sstevel@tonic-gate 	mutex_init(&(devi->devi_pm_lock), NULL, MUTEX_DEFAULT, NULL);
3367c478bd9Sstevel@tonic-gate 	mutex_init(&(devi->devi_pm_busy_lock), NULL, MUTEX_DEFAULT, NULL);
3377c478bd9Sstevel@tonic-gate 
33825e8c5aaSvikram 	RIO_TRACE((CE_NOTE, "i_ddi_alloc_node: Initing contract fields: "
33925e8c5aaSvikram 	    "dip=%p, name=%s", (void *)devi, node_name));
34025e8c5aaSvikram 
34125e8c5aaSvikram 	mutex_init(&(devi->devi_ct_lock), NULL, MUTEX_DEFAULT, NULL);
34225e8c5aaSvikram 	cv_init(&(devi->devi_ct_cv), NULL, CV_DEFAULT, NULL);
34325e8c5aaSvikram 	devi->devi_ct_count = -1;	/* counter not in use if -1 */
34425e8c5aaSvikram 	list_create(&(devi->devi_ct), sizeof (cont_device_t),
34525e8c5aaSvikram 	    offsetof(cont_device_t, cond_next));
34625e8c5aaSvikram 
3477c478bd9Sstevel@tonic-gate 	i_ddi_set_node_state((dev_info_t *)devi, DS_PROTO);
3487c478bd9Sstevel@tonic-gate 	da_log_enter((dev_info_t *)devi);
3497c478bd9Sstevel@tonic-gate 	return ((dev_info_t *)devi);
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate fail:
3527c478bd9Sstevel@tonic-gate 	if (devi->devi_sys_prop_ptr)
3537c478bd9Sstevel@tonic-gate 		i_ddi_prop_list_delete(devi->devi_sys_prop_ptr);
3547c478bd9Sstevel@tonic-gate 	if (devi->devi_node_name)
3557c478bd9Sstevel@tonic-gate 		kmem_free(devi->devi_node_name, strlen(node_name) + 1);
3567c478bd9Sstevel@tonic-gate 	if (devi->devi_audit)
3577c478bd9Sstevel@tonic-gate 		kmem_free(devi->devi_audit, sizeof (devinfo_audit_t));
3587c478bd9Sstevel@tonic-gate 	kmem_cache_free(ddi_node_cache, devi);
3597c478bd9Sstevel@tonic-gate 	cmn_err(CE_NOTE, failed);
3607c478bd9Sstevel@tonic-gate 	return (NULL);
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate /*
3647c478bd9Sstevel@tonic-gate  * free a dev_info structure.
3657c478bd9Sstevel@tonic-gate  * NB. Not callable from interrupt since impl_ddi_free_nodeid may block.
3667c478bd9Sstevel@tonic-gate  */
3677c478bd9Sstevel@tonic-gate void
3687c478bd9Sstevel@tonic-gate i_ddi_free_node(dev_info_t *dip)
3697c478bd9Sstevel@tonic-gate {
3707c478bd9Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
3717c478bd9Sstevel@tonic-gate 	struct devi_nodeid *elem;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	ASSERT(devi->devi_ref == 0);
3747c478bd9Sstevel@tonic-gate 	ASSERT(devi->devi_addr == NULL);
3757c478bd9Sstevel@tonic-gate 	ASSERT(devi->devi_node_state == DS_PROTO);
3767c478bd9Sstevel@tonic-gate 	ASSERT(devi->devi_child == NULL);
3777c478bd9Sstevel@tonic-gate 
378fe9fe9fbScth 	/* free devi_addr_buf allocated by ddi_set_name_addr() */
379fe9fe9fbScth 	if (devi->devi_addr_buf)
380fe9fe9fbScth 		kmem_free(devi->devi_addr_buf, 2 * MAXNAMELEN);
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	if (i_ndi_dev_is_auto_assigned_node(dip))
3837c478bd9Sstevel@tonic-gate 		impl_ddi_free_nodeid(DEVI(dip)->devi_nodeid);
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
3867c478bd9Sstevel@tonic-gate 		mutex_enter(&devimap->dno_lock);
3877c478bd9Sstevel@tonic-gate 		ASSERT(devimap->dno_free);
3887c478bd9Sstevel@tonic-gate 		elem = devimap->dno_free;
3897c478bd9Sstevel@tonic-gate 		devimap->dno_free = elem->next;
3907c478bd9Sstevel@tonic-gate 		mutex_exit(&devimap->dno_lock);
3917c478bd9Sstevel@tonic-gate 		kmem_free(elem, sizeof (*elem));
3927c478bd9Sstevel@tonic-gate 	}
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_compat_names)
3957c478bd9Sstevel@tonic-gate 		kmem_free(DEVI(dip)->devi_compat_names,
3967c478bd9Sstevel@tonic-gate 		    DEVI(dip)->devi_compat_length);
397f4da9be0Scth 	if (DEVI(dip)->devi_rebinding_name)
398f4da9be0Scth 		kmem_free(DEVI(dip)->devi_rebinding_name,
399f4da9be0Scth 		    strlen(DEVI(dip)->devi_rebinding_name) + 1);
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	ddi_prop_remove_all(dip);	/* remove driver properties */
4027c478bd9Sstevel@tonic-gate 	if (devi->devi_sys_prop_ptr)
4037c478bd9Sstevel@tonic-gate 		i_ddi_prop_list_delete(devi->devi_sys_prop_ptr);
4047c478bd9Sstevel@tonic-gate 	if (devi->devi_hw_prop_ptr)
4057c478bd9Sstevel@tonic-gate 		i_ddi_prop_list_delete(devi->devi_hw_prop_ptr);
4067c478bd9Sstevel@tonic-gate 
407602ca9eaScth 	if (DEVI(dip)->devi_devid_str)
408602ca9eaScth 		ddi_devid_str_free(DEVI(dip)->devi_devid_str);
409602ca9eaScth 
4107c478bd9Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_INVAL);
4117c478bd9Sstevel@tonic-gate 	da_log_enter(dip);
4127c478bd9Sstevel@tonic-gate 	if (devi->devi_audit) {
4137c478bd9Sstevel@tonic-gate 		kmem_free(devi->devi_audit, sizeof (devinfo_audit_t));
4147c478bd9Sstevel@tonic-gate 	}
4157c478bd9Sstevel@tonic-gate 	if (devi->devi_device_class)
4167c478bd9Sstevel@tonic-gate 		kmem_free(devi->devi_device_class,
4177c478bd9Sstevel@tonic-gate 		    strlen(devi->devi_device_class) + 1);
4187c478bd9Sstevel@tonic-gate 	cv_destroy(&(devi->devi_cv));
4197c478bd9Sstevel@tonic-gate 	mutex_destroy(&(devi->devi_lock));
4207c478bd9Sstevel@tonic-gate 	mutex_destroy(&(devi->devi_pm_lock));
4217c478bd9Sstevel@tonic-gate 	mutex_destroy(&(devi->devi_pm_busy_lock));
4227c478bd9Sstevel@tonic-gate 
42325e8c5aaSvikram 	RIO_TRACE((CE_NOTE, "i_ddi_free_node: destroying contract fields: "
42425e8c5aaSvikram 	    "dip=%p", (void *)dip));
42525e8c5aaSvikram 	contract_device_remove_dip(dip);
42625e8c5aaSvikram 	ASSERT(devi->devi_ct_count == -1);
42725e8c5aaSvikram 	ASSERT(list_is_empty(&(devi->devi_ct)));
42825e8c5aaSvikram 	cv_destroy(&(devi->devi_ct_cv));
42925e8c5aaSvikram 	list_destroy(&(devi->devi_ct));
43025e8c5aaSvikram 	/* free this last since contract_device_remove_dip() uses it */
43125e8c5aaSvikram 	mutex_destroy(&(devi->devi_ct_lock));
43225e8c5aaSvikram 	RIO_TRACE((CE_NOTE, "i_ddi_free_node: destroyed all contract fields: "
43325e8c5aaSvikram 	    "dip=%p, name=%s", (void *)dip, devi->devi_node_name));
43425e8c5aaSvikram 
43525e8c5aaSvikram 	kmem_free(devi->devi_node_name, strlen(devi->devi_node_name) + 1);
43625e8c5aaSvikram 
4377c478bd9Sstevel@tonic-gate 	kmem_cache_free(ddi_node_cache, devi);
4387c478bd9Sstevel@tonic-gate }
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate /*
4427c478bd9Sstevel@tonic-gate  * Node state transitions
4437c478bd9Sstevel@tonic-gate  */
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate /*
4467c478bd9Sstevel@tonic-gate  * Change the node name
4477c478bd9Sstevel@tonic-gate  */
4487c478bd9Sstevel@tonic-gate int
4497c478bd9Sstevel@tonic-gate ndi_devi_set_nodename(dev_info_t *dip, char *name, int flags)
4507c478bd9Sstevel@tonic-gate {
4517c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags))
4527c478bd9Sstevel@tonic-gate 	char *nname, *oname;
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	ASSERT(dip && name);
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	oname = DEVI(dip)->devi_node_name;
4577c478bd9Sstevel@tonic-gate 	if (strcmp(oname, name) == 0)
4587c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	/*
4617c478bd9Sstevel@tonic-gate 	 * pcicfg_fix_ethernet requires a name change after node
4627c478bd9Sstevel@tonic-gate 	 * is linked into the tree. When pcicfg is fixed, we
4637c478bd9Sstevel@tonic-gate 	 * should only allow name change in DS_PROTO state.
4647c478bd9Sstevel@tonic-gate 	 */
4657c478bd9Sstevel@tonic-gate 	if (i_ddi_node_state(dip) >= DS_BOUND) {
4667c478bd9Sstevel@tonic-gate 		/*
4677c478bd9Sstevel@tonic-gate 		 * Don't allow name change once node is bound
4687c478bd9Sstevel@tonic-gate 		 */
4697c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE,
4707c478bd9Sstevel@tonic-gate 		    "ndi_devi_set_nodename: node already bound dip = %p,"
4717c478bd9Sstevel@tonic-gate 		    " %s -> %s", (void *)dip, ddi_node_name(dip), name);
4727c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
4737c478bd9Sstevel@tonic-gate 	}
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	nname = i_ddi_strdup(name, KM_SLEEP);
4767c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_node_name = nname;
4777c478bd9Sstevel@tonic-gate 	i_ddi_set_binding_name(dip, nname);
4787c478bd9Sstevel@tonic-gate 	kmem_free(oname, strlen(oname) + 1);
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	da_log_enter(dip);
4817c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
4827c478bd9Sstevel@tonic-gate }
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate void
4857c478bd9Sstevel@tonic-gate i_ddi_add_devimap(dev_info_t *dip)
4867c478bd9Sstevel@tonic-gate {
4877c478bd9Sstevel@tonic-gate 	struct devi_nodeid *elem;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	ASSERT(dip);
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	if (!ndi_dev_is_persistent_node(dip))
4927c478bd9Sstevel@tonic-gate 		return;
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	ASSERT(ddi_get_parent(dip) == NULL || (DEVI_VHCI_NODE(dip)) ||
4957c478bd9Sstevel@tonic-gate 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	mutex_enter(&devimap->dno_lock);
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	ASSERT(devimap->dno_free);
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	elem = devimap->dno_free;
5027c478bd9Sstevel@tonic-gate 	devimap->dno_free = elem->next;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	elem->nodeid = ddi_get_nodeid(dip);
5057c478bd9Sstevel@tonic-gate 	elem->dip = dip;
5067c478bd9Sstevel@tonic-gate 	elem->next = devimap->dno_head;
5077c478bd9Sstevel@tonic-gate 	devimap->dno_head = elem;
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	devimap->dno_list_length++;
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	mutex_exit(&devimap->dno_lock);
5127c478bd9Sstevel@tonic-gate }
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate static int
5157c478bd9Sstevel@tonic-gate i_ddi_remove_devimap(dev_info_t *dip)
5167c478bd9Sstevel@tonic-gate {
5177c478bd9Sstevel@tonic-gate 	struct devi_nodeid *prev, *elem;
5187c478bd9Sstevel@tonic-gate 	static const char *fcn = "i_ddi_remove_devimap";
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	ASSERT(dip);
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	if (!ndi_dev_is_persistent_node(dip))
5237c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	mutex_enter(&devimap->dno_lock);
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	/*
5287c478bd9Sstevel@tonic-gate 	 * The following check is done with dno_lock held
5297c478bd9Sstevel@tonic-gate 	 * to prevent race between dip removal and
5307c478bd9Sstevel@tonic-gate 	 * e_ddi_prom_node_to_dip()
5317c478bd9Sstevel@tonic-gate 	 */
5327c478bd9Sstevel@tonic-gate 	if (e_ddi_devi_holdcnt(dip)) {
5337c478bd9Sstevel@tonic-gate 		mutex_exit(&devimap->dno_lock);
5347c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
5357c478bd9Sstevel@tonic-gate 	}
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	ASSERT(devimap->dno_head);
5387c478bd9Sstevel@tonic-gate 	ASSERT(devimap->dno_list_length > 0);
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	prev = NULL;
5417c478bd9Sstevel@tonic-gate 	for (elem = devimap->dno_head; elem; elem = elem->next) {
5427c478bd9Sstevel@tonic-gate 		if (elem->dip == dip) {
5437c478bd9Sstevel@tonic-gate 			ASSERT(elem->nodeid == ddi_get_nodeid(dip));
5447c478bd9Sstevel@tonic-gate 			break;
5457c478bd9Sstevel@tonic-gate 		}
5467c478bd9Sstevel@tonic-gate 		prev = elem;
5477c478bd9Sstevel@tonic-gate 	}
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	if (elem && prev)
5507c478bd9Sstevel@tonic-gate 		prev->next = elem->next;
5517c478bd9Sstevel@tonic-gate 	else if (elem)
5527c478bd9Sstevel@tonic-gate 		devimap->dno_head = elem->next;
5537c478bd9Sstevel@tonic-gate 	else
5547c478bd9Sstevel@tonic-gate 		panic("%s: devinfo node(%p) not found",
5557c478bd9Sstevel@tonic-gate 		    fcn, (void *)dip);
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	devimap->dno_list_length--;
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	elem->nodeid = 0;
5607c478bd9Sstevel@tonic-gate 	elem->dip = NULL;
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	elem->next = devimap->dno_free;
5637c478bd9Sstevel@tonic-gate 	devimap->dno_free = elem;
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	mutex_exit(&devimap->dno_lock);
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
5687c478bd9Sstevel@tonic-gate }
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate /*
5717c478bd9Sstevel@tonic-gate  * Link this node into the devinfo tree and add to orphan list
5727c478bd9Sstevel@tonic-gate  * Not callable from interrupt context
5737c478bd9Sstevel@tonic-gate  */
5747c478bd9Sstevel@tonic-gate static void
5757c478bd9Sstevel@tonic-gate link_node(dev_info_t *dip)
5767c478bd9Sstevel@tonic-gate {
5777c478bd9Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
5787c478bd9Sstevel@tonic-gate 	struct dev_info *parent = devi->devi_parent;
5797c478bd9Sstevel@tonic-gate 	dev_info_t **dipp;
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	ASSERT(parent);	/* never called for root node */
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "link_node: parent = %s child = %s\n",
5847c478bd9Sstevel@tonic-gate 	    parent->devi_node_name, devi->devi_node_name));
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	/*
5877c478bd9Sstevel@tonic-gate 	 * Hold the global_vhci_lock before linking any direct
5887c478bd9Sstevel@tonic-gate 	 * children of rootnex driver. This special lock protects
5897c478bd9Sstevel@tonic-gate 	 * linking and unlinking for rootnext direct children.
5907c478bd9Sstevel@tonic-gate 	 */
5917c478bd9Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
5927c478bd9Sstevel@tonic-gate 		mutex_enter(&global_vhci_lock);
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	/*
5957c478bd9Sstevel@tonic-gate 	 * attach the node to end of the list unless the node is already there
5967c478bd9Sstevel@tonic-gate 	 */
5977c478bd9Sstevel@tonic-gate 	dipp = (dev_info_t **)(&DEVI(parent)->devi_child);
5987c478bd9Sstevel@tonic-gate 	while (*dipp && (*dipp != dip)) {
5997c478bd9Sstevel@tonic-gate 		dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling);
6007c478bd9Sstevel@tonic-gate 	}
6017c478bd9Sstevel@tonic-gate 	ASSERT(*dipp == NULL);	/* node is not linked */
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	/*
6047c478bd9Sstevel@tonic-gate 	 * Now that we are in the tree, update the devi-nodeid map.
6057c478bd9Sstevel@tonic-gate 	 */
6067c478bd9Sstevel@tonic-gate 	i_ddi_add_devimap(dip);
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	/*
6097c478bd9Sstevel@tonic-gate 	 * This is a temporary workaround for Bug 4618861.
6107c478bd9Sstevel@tonic-gate 	 * We keep the scsi_vhci nexus node on the left side of the devinfo
6117c478bd9Sstevel@tonic-gate 	 * tree (under the root nexus driver), so that virtual nodes under
6127c478bd9Sstevel@tonic-gate 	 * scsi_vhci will be SUSPENDed first and RESUMEd last.  This ensures
6137c478bd9Sstevel@tonic-gate 	 * that the pHCI nodes are active during times when their clients
6147c478bd9Sstevel@tonic-gate 	 * may be depending on them.  This workaround embodies the knowledge
6157c478bd9Sstevel@tonic-gate 	 * that system PM and CPR both traverse the tree left-to-right during
6167c478bd9Sstevel@tonic-gate 	 * SUSPEND and right-to-left during RESUME.
6179d3d2ed0Shiremath 	 * Extending the workaround to IB Nexus/VHCI
6189d3d2ed0Shiremath 	 * driver also.
6197c478bd9Sstevel@tonic-gate 	 */
620f4da9be0Scth 	if (strcmp(devi->devi_binding_name, "scsi_vhci") == 0) {
6217c478bd9Sstevel@tonic-gate 		/* Add scsi_vhci to beginning of list */
6227c478bd9Sstevel@tonic-gate 		ASSERT((dev_info_t *)parent == top_devinfo);
6237c478bd9Sstevel@tonic-gate 		/* scsi_vhci under rootnex */
6247c478bd9Sstevel@tonic-gate 		devi->devi_sibling = parent->devi_child;
6257c478bd9Sstevel@tonic-gate 		parent->devi_child = devi;
626f4da9be0Scth 	} else if (strcmp(devi->devi_binding_name, "ib") == 0) {
6279d3d2ed0Shiremath 		i_link_vhci_node(dip);
6287c478bd9Sstevel@tonic-gate 	} else {
6297c478bd9Sstevel@tonic-gate 		/* Add to end of list */
6307c478bd9Sstevel@tonic-gate 		*dipp = dip;
6317c478bd9Sstevel@tonic-gate 		DEVI(dip)->devi_sibling = NULL;
6327c478bd9Sstevel@tonic-gate 	}
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	/*
6357c478bd9Sstevel@tonic-gate 	 * Release the global_vhci_lock before linking any direct
6367c478bd9Sstevel@tonic-gate 	 * children of rootnex driver.
6377c478bd9Sstevel@tonic-gate 	 */
6387c478bd9Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
6397c478bd9Sstevel@tonic-gate 		mutex_exit(&global_vhci_lock);
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	/* persistent nodes go on orphan list */
6427c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip))
6437c478bd9Sstevel@tonic-gate 		add_to_dn_list(&orphanlist, dip);
6447c478bd9Sstevel@tonic-gate }
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate /*
6477c478bd9Sstevel@tonic-gate  * Unlink this node from the devinfo tree
6487c478bd9Sstevel@tonic-gate  */
6497c478bd9Sstevel@tonic-gate static int
6507c478bd9Sstevel@tonic-gate unlink_node(dev_info_t *dip)
6517c478bd9Sstevel@tonic-gate {
6527c478bd9Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
6537c478bd9Sstevel@tonic-gate 	struct dev_info *parent = devi->devi_parent;
6547c478bd9Sstevel@tonic-gate 	dev_info_t **dipp;
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	ASSERT(parent != NULL);
6577c478bd9Sstevel@tonic-gate 	ASSERT(devi->devi_node_state == DS_LINKED);
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "unlink_node: name = %s\n",
6607c478bd9Sstevel@tonic-gate 	    ddi_node_name(dip)));
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	/* check references */
6637c478bd9Sstevel@tonic-gate 	if (devi->devi_ref || i_ddi_remove_devimap(dip) != DDI_SUCCESS)
6647c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	/*
6677c478bd9Sstevel@tonic-gate 	 * Hold the global_vhci_lock before linking any direct
6687c478bd9Sstevel@tonic-gate 	 * children of rootnex driver.
6697c478bd9Sstevel@tonic-gate 	 */
6707c478bd9Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
6717c478bd9Sstevel@tonic-gate 		mutex_enter(&global_vhci_lock);
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	dipp = (dev_info_t **)(&DEVI(parent)->devi_child);
6747c478bd9Sstevel@tonic-gate 	while (*dipp && (*dipp != dip)) {
6757c478bd9Sstevel@tonic-gate 		dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling);
6767c478bd9Sstevel@tonic-gate 	}
6777c478bd9Sstevel@tonic-gate 	if (*dipp) {
6787c478bd9Sstevel@tonic-gate 		*dipp = (dev_info_t *)(devi->devi_sibling);
6797c478bd9Sstevel@tonic-gate 		devi->devi_sibling = NULL;
6807c478bd9Sstevel@tonic-gate 	} else {
6817c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE, "unlink_node: %s not linked",
6827c478bd9Sstevel@tonic-gate 		    devi->devi_node_name));
6837c478bd9Sstevel@tonic-gate 	}
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	/*
6867c478bd9Sstevel@tonic-gate 	 * Release the global_vhci_lock before linking any direct
6877c478bd9Sstevel@tonic-gate 	 * children of rootnex driver.
6887c478bd9Sstevel@tonic-gate 	 */
6897c478bd9Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
6907c478bd9Sstevel@tonic-gate 		mutex_exit(&global_vhci_lock);
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	/* Remove node from orphan list */
6937c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
6947c478bd9Sstevel@tonic-gate 		remove_from_dn_list(&orphanlist, dip);
6957c478bd9Sstevel@tonic-gate 	}
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
6987c478bd9Sstevel@tonic-gate }
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate /*
7017c478bd9Sstevel@tonic-gate  * Bind this devinfo node to a driver. If compat is NON-NULL, try that first.
7027c478bd9Sstevel@tonic-gate  * Else, use the node-name.
7037c478bd9Sstevel@tonic-gate  *
7047c478bd9Sstevel@tonic-gate  * NOTE: IEEE1275 specifies that nodename should be tried before compatible.
7057c478bd9Sstevel@tonic-gate  *	Solaris implementation binds nodename after compatible.
7067c478bd9Sstevel@tonic-gate  *
7077c478bd9Sstevel@tonic-gate  * If we find a binding,
7087c478bd9Sstevel@tonic-gate  * - set the binding name to the the string,
7097c478bd9Sstevel@tonic-gate  * - set major number to driver major
7107c478bd9Sstevel@tonic-gate  *
7117c478bd9Sstevel@tonic-gate  * If we don't find a binding,
7127c478bd9Sstevel@tonic-gate  * - return failure
7137c478bd9Sstevel@tonic-gate  */
7147c478bd9Sstevel@tonic-gate static int
7157c478bd9Sstevel@tonic-gate bind_node(dev_info_t *dip)
7167c478bd9Sstevel@tonic-gate {
7177c478bd9Sstevel@tonic-gate 	char *p = NULL;
718a204de77Scth 	major_t major = DDI_MAJOR_T_NONE;
7197c478bd9Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
7207c478bd9Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	ASSERT(devi->devi_node_state == DS_LINKED);
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "bind_node: 0x%p(name = %s)\n",
7257c478bd9Sstevel@tonic-gate 	    (void *)dip, ddi_node_name(dip)));
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
7287c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_flags & DEVI_NO_BIND) {
7297c478bd9Sstevel@tonic-gate 		mutex_exit(&DEVI(dip)->devi_lock);
7307c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7317c478bd9Sstevel@tonic-gate 	}
7327c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	/* find the driver with most specific binding using compatible */
7357c478bd9Sstevel@tonic-gate 	major = ddi_compatible_driver_major(dip, &p);
736a204de77Scth 	if (major == DDI_MAJOR_T_NONE)
7377c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	devi->devi_major = major;
7407c478bd9Sstevel@tonic-gate 	if (p != NULL) {
7417c478bd9Sstevel@tonic-gate 		i_ddi_set_binding_name(dip, p);
7427c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "bind_node: %s bound to %s\n",
7437c478bd9Sstevel@tonic-gate 		    devi->devi_node_name, p));
7447c478bd9Sstevel@tonic-gate 	}
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	/* Link node to per-driver list */
7477c478bd9Sstevel@tonic-gate 	link_to_driver_list(dip);
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	/*
7507c478bd9Sstevel@tonic-gate 	 * reset parent flag so that nexus will merge .conf props
7517c478bd9Sstevel@tonic-gate 	 */
7527c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
7537c478bd9Sstevel@tonic-gate 		mutex_enter(&DEVI(parent)->devi_lock);
7547c478bd9Sstevel@tonic-gate 		DEVI(parent)->devi_flags &=
7557c478bd9Sstevel@tonic-gate 		    ~(DEVI_ATTACHED_CHILDREN|DEVI_MADE_CHILDREN);
7567c478bd9Sstevel@tonic-gate 		mutex_exit(&DEVI(parent)->devi_lock);
7577c478bd9Sstevel@tonic-gate 	}
7587c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
7597c478bd9Sstevel@tonic-gate }
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate /*
7627c478bd9Sstevel@tonic-gate  * Unbind this devinfo node
7637c478bd9Sstevel@tonic-gate  * Called before the node is destroyed or driver is removed from system
7647c478bd9Sstevel@tonic-gate  */
7657c478bd9Sstevel@tonic-gate static int
7667c478bd9Sstevel@tonic-gate unbind_node(dev_info_t *dip)
7677c478bd9Sstevel@tonic-gate {
7687c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_node_state == DS_BOUND);
769a204de77Scth 	ASSERT(DEVI(dip)->devi_major != DDI_MAJOR_T_NONE);
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 	/* check references */
7727c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_ref)
7737c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "unbind_node: 0x%p(name = %s)\n",
7767c478bd9Sstevel@tonic-gate 	    (void *)dip, ddi_node_name(dip)));
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	unlink_from_driver_list(dip);
779f4da9be0Scth 
780a204de77Scth 	DEVI(dip)->devi_major = DDI_MAJOR_T_NONE;
781f4da9be0Scth 	DEVI(dip)->devi_binding_name = DEVI(dip)->devi_node_name;
7827c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
7837c478bd9Sstevel@tonic-gate }
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate /*
7867c478bd9Sstevel@tonic-gate  * Initialize a node: calls the parent nexus' bus_ctl ops to do the operation.
7877c478bd9Sstevel@tonic-gate  * Must hold parent and per-driver list while calling this function.
7887c478bd9Sstevel@tonic-gate  * A successful init_node() returns with an active ndi_hold_devi() hold on
7897c478bd9Sstevel@tonic-gate  * the parent.
7907c478bd9Sstevel@tonic-gate  */
7917c478bd9Sstevel@tonic-gate static int
7927c478bd9Sstevel@tonic-gate init_node(dev_info_t *dip)
7937c478bd9Sstevel@tonic-gate {
7947c478bd9Sstevel@tonic-gate 	int error;
7957c478bd9Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
7967c478bd9Sstevel@tonic-gate 	int (*f)(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *, void *);
7977c478bd9Sstevel@tonic-gate 	char *path;
798f4da9be0Scth 	major_t	major;
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_BOUND);
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	/* should be DS_READY except for pcmcia ... */
8037c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(pdip) >= DS_PROBED);
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8067c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(dip, path);
8077c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "init_node: entry: path %s 0x%p\n",
8087c478bd9Sstevel@tonic-gate 	    path, (void *)dip));
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 	/*
8117c478bd9Sstevel@tonic-gate 	 * The parent must have a bus_ctl operation.
8127c478bd9Sstevel@tonic-gate 	 */
8137c478bd9Sstevel@tonic-gate 	if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
8147c478bd9Sstevel@tonic-gate 	    (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_ctl) == NULL) {
8157c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
8167c478bd9Sstevel@tonic-gate 		goto out;
8177c478bd9Sstevel@tonic-gate 	}
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	add_global_props(dip);
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 	/*
8227c478bd9Sstevel@tonic-gate 	 * Invoke the parent's bus_ctl operation with the DDI_CTLOPS_INITCHILD
8237c478bd9Sstevel@tonic-gate 	 * command to transform the child to canonical form 1. If there
8247c478bd9Sstevel@tonic-gate 	 * is an error, ddi_remove_child should be called, to clean up.
8257c478bd9Sstevel@tonic-gate 	 */
8267c478bd9Sstevel@tonic-gate 	error = (*f)(pdip, pdip, DDI_CTLOPS_INITCHILD, dip, NULL);
8277c478bd9Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
8287c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "init_node: %s 0x%p failed\n",
8297c478bd9Sstevel@tonic-gate 		    path, (void *)dip));
8307c478bd9Sstevel@tonic-gate 		remove_global_props(dip);
8317c478bd9Sstevel@tonic-gate 		/* in case nexus driver didn't clear this field */
8327c478bd9Sstevel@tonic-gate 		ddi_set_name_addr(dip, NULL);
8337c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
8347c478bd9Sstevel@tonic-gate 		goto out;
8357c478bd9Sstevel@tonic-gate 	}
8367c478bd9Sstevel@tonic-gate 
837d6ae180bScth 	ndi_hold_devi(pdip);			/* initial hold of parent */
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 	/* recompute path after initchild for @addr information */
8407c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(dip, path);
8417c478bd9Sstevel@tonic-gate 
842f4da9be0Scth 	/* Check for duplicate nodes */
843f4da9be0Scth 	if (find_duplicate_child(pdip, dip) != NULL) {
8447c478bd9Sstevel@tonic-gate 		/*
8457c478bd9Sstevel@tonic-gate 		 * uninit_node() the duplicate - a successful uninit_node()
846d6ae180bScth 		 * will release inital hold of parent using ndi_rele_devi().
8477c478bd9Sstevel@tonic-gate 		 */
8487c478bd9Sstevel@tonic-gate 		if ((error = uninit_node(dip)) != DDI_SUCCESS) {
849d6ae180bScth 			ndi_rele_devi(pdip);	/* release initial hold */
8507c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "init_node: uninit of duplicate "
8517c478bd9Sstevel@tonic-gate 			    "node %s failed", path);
8527c478bd9Sstevel@tonic-gate 		}
8537c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "init_node: duplicate uninit "
8547c478bd9Sstevel@tonic-gate 		    "%s 0x%p%s\n", path, (void *)dip,
8557c478bd9Sstevel@tonic-gate 		    (error == DDI_SUCCESS) ? "" : " failed"));
8567c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
8577c478bd9Sstevel@tonic-gate 		goto out;
8587c478bd9Sstevel@tonic-gate 	}
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	/*
861f4da9be0Scth 	 * Check to see if we have a path-oriented driver alias that overrides
862f4da9be0Scth 	 * the current driver binding. If so, we need to rebind. This check
863f4da9be0Scth 	 * needs to be delayed until after a successful DDI_CTLOPS_INITCHILD,
864f4da9be0Scth 	 * so the unit-address is established on the last component of the path.
865f4da9be0Scth 	 *
866f4da9be0Scth 	 * NOTE: Allowing a path-oriented alias to change the driver binding
867f4da9be0Scth 	 * of a driver.conf node results in non-intuitive property behavior.
868f4da9be0Scth 	 * We provide a tunable (driver_conf_allow_path_alias) to control
869f4da9be0Scth 	 * this behavior. See uninit_node() for more details.
870f4da9be0Scth 	 *
871f4da9be0Scth 	 * NOTE: If you are adding a path-oriented alias for the boot device,
872f4da9be0Scth 	 * and there is mismatch between OBP and the kernel in regard to
873f4da9be0Scth 	 * generic name use, like "disk" .vs. "ssd", then you will need
874f4da9be0Scth 	 * to add a path-oriented alias for both paths.
875f4da9be0Scth 	 */
876f4da9be0Scth 	major = ddi_name_to_major(path);
877a204de77Scth 	if ((major != DDI_MAJOR_T_NONE) &&
878f4da9be0Scth 	    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED) &&
879f4da9be0Scth 	    (major != DEVI(dip)->devi_major) &&
880f4da9be0Scth 	    (ndi_dev_is_persistent_node(dip) || driver_conf_allow_path_alias)) {
881f4da9be0Scth 
882f4da9be0Scth 		/* Mark node for rebind processing. */
883f4da9be0Scth 		mutex_enter(&DEVI(dip)->devi_lock);
884f4da9be0Scth 		DEVI(dip)->devi_flags |= DEVI_REBIND;
885f4da9be0Scth 		mutex_exit(&DEVI(dip)->devi_lock);
886f4da9be0Scth 
887f4da9be0Scth 		/*
888d6ae180bScth 		 * Add an extra hold on the parent to prevent it from ever
889d6ae180bScth 		 * having a zero devi_ref during the child rebind process.
890d6ae180bScth 		 * This is necessary to ensure that the parent will never
891d6ae180bScth 		 * detach(9E) during the rebind.
892d6ae180bScth 		 */
893d6ae180bScth 		ndi_hold_devi(pdip);		/* extra hold of parent */
894d6ae180bScth 
895d6ae180bScth 		/*
896f4da9be0Scth 		 * uninit_node() current binding - a successful uninit_node()
897d6ae180bScth 		 * will release extra hold of parent using ndi_rele_devi().
898f4da9be0Scth 		 */
899f4da9be0Scth 		if ((error = uninit_node(dip)) != DDI_SUCCESS) {
900d6ae180bScth 			ndi_rele_devi(pdip);	/* release extra hold */
901d6ae180bScth 			ndi_rele_devi(pdip);	/* release initial hold */
902f4da9be0Scth 			cmn_err(CE_WARN, "init_node: uninit for rebind "
903f4da9be0Scth 			    "of node %s failed", path);
904f4da9be0Scth 			goto out;
905f4da9be0Scth 		}
906f4da9be0Scth 
907f4da9be0Scth 		/* Unbind: demote the node back to DS_LINKED.  */
908f4da9be0Scth 		if ((error = ndi_devi_unbind_driver(dip)) != DDI_SUCCESS) {
909d6ae180bScth 			ndi_rele_devi(pdip);	/* relrease initial hold */
910f4da9be0Scth 			cmn_err(CE_WARN, "init_node: unbind for rebind "
911f4da9be0Scth 			    "of node %s failed", path);
912f4da9be0Scth 			goto out;
913f4da9be0Scth 		}
914f4da9be0Scth 
915f4da9be0Scth 		/* establish rebinding name */
916f4da9be0Scth 		if (DEVI(dip)->devi_rebinding_name == NULL)
917f4da9be0Scth 			DEVI(dip)->devi_rebinding_name =
918f4da9be0Scth 			    i_ddi_strdup(path, KM_SLEEP);
919f4da9be0Scth 
920f4da9be0Scth 		/*
921f4da9be0Scth 		 * Now that we are demoted and marked for rebind, repromote.
922f4da9be0Scth 		 * We need to do this in steps, instead of just calling
923f4da9be0Scth 		 * ddi_initchild, so that we can redo the merge operation
924f4da9be0Scth 		 * after we are rebound to the path-bound driver.
925f4da9be0Scth 		 *
926f4da9be0Scth 		 * Start by rebinding node to the path-bound driver.
927f4da9be0Scth 		 */
928f4da9be0Scth 		if ((error = ndi_devi_bind_driver(dip, 0)) != DDI_SUCCESS) {
929d6ae180bScth 			ndi_rele_devi(pdip);	/* relrease initial hold */
930f4da9be0Scth 			cmn_err(CE_WARN, "init_node: rebind "
931f4da9be0Scth 			    "of node %s failed", path);
932f4da9be0Scth 			goto out;
933f4da9be0Scth 		}
934f4da9be0Scth 
935f4da9be0Scth 		/*
936f4da9be0Scth 		 * If the node is not a driver.conf node then merge
937f4da9be0Scth 		 * driver.conf properties from new path-bound driver.conf.
938f4da9be0Scth 		 */
939f4da9be0Scth 		if (ndi_dev_is_persistent_node(dip))
940f4da9be0Scth 			(void) i_ndi_make_spec_children(pdip, 0);
941f4da9be0Scth 
942f4da9be0Scth 		/*
943f4da9be0Scth 		 * Now that we have taken care of merge, repromote back
944f4da9be0Scth 		 * to DS_INITIALIZED.
945f4da9be0Scth 		 */
946f4da9be0Scth 		error = ddi_initchild(pdip, dip);
947f4da9be0Scth 		NDI_CONFIG_DEBUG((CE_CONT, "init_node: rebind "
948f4da9be0Scth 		    "%s 0x%p\n", path, (void *)dip));
949d6ae180bScth 
950d6ae180bScth 		/*
951d6ae180bScth 		 * Release our initial hold. If ddi_initchild() was
952b9ccdc5aScth 		 * successful then it will return with the active hold.
953d6ae180bScth 		 */
954d6ae180bScth 		ndi_rele_devi(pdip);
955f4da9be0Scth 		goto out;
956f4da9be0Scth 	}
957f4da9be0Scth 
958f4da9be0Scth 	/*
9597c478bd9Sstevel@tonic-gate 	 * Apply multi-parent/deep-nexus optimization to the new node
9607c478bd9Sstevel@tonic-gate 	 */
9617c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
9627c478bd9Sstevel@tonic-gate 	ddi_optimize_dtree(dip);
963d6ae180bScth 	error = DDI_SUCCESS;		/* return with active hold */
9647c478bd9Sstevel@tonic-gate 
965f4da9be0Scth out:	if (error != DDI_SUCCESS) {
966f4da9be0Scth 		/* On failure ensure that DEVI_REBIND is cleared */
967f4da9be0Scth 		mutex_enter(&DEVI(dip)->devi_lock);
968f4da9be0Scth 		DEVI(dip)->devi_flags &= ~DEVI_REBIND;
969f4da9be0Scth 		mutex_exit(&DEVI(dip)->devi_lock);
970f4da9be0Scth 	}
971f4da9be0Scth 	kmem_free(path, MAXPATHLEN);
9727c478bd9Sstevel@tonic-gate 	return (error);
9737c478bd9Sstevel@tonic-gate }
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate /*
9767c478bd9Sstevel@tonic-gate  * Uninitialize node
9777c478bd9Sstevel@tonic-gate  * The per-driver list must be held busy during the call.
9787c478bd9Sstevel@tonic-gate  * A successful uninit_node() releases the init_node() hold on
9797c478bd9Sstevel@tonic-gate  * the parent by calling ndi_rele_devi().
9807c478bd9Sstevel@tonic-gate  */
9817c478bd9Sstevel@tonic-gate static int
9827c478bd9Sstevel@tonic-gate uninit_node(dev_info_t *dip)
9837c478bd9Sstevel@tonic-gate {
9847c478bd9Sstevel@tonic-gate 	int node_state_entry;
9857c478bd9Sstevel@tonic-gate 	dev_info_t *pdip;
9867c478bd9Sstevel@tonic-gate 	struct dev_ops *ops;
9877c478bd9Sstevel@tonic-gate 	int (*f)();
9887c478bd9Sstevel@tonic-gate 	int error;
9897c478bd9Sstevel@tonic-gate 	char *addr;
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 	/*
9927c478bd9Sstevel@tonic-gate 	 * Don't check for references here or else a ref-counted
9937c478bd9Sstevel@tonic-gate 	 * dip cannot be downgraded by the framework.
9947c478bd9Sstevel@tonic-gate 	 */
9957c478bd9Sstevel@tonic-gate 	node_state_entry = i_ddi_node_state(dip);
9967c478bd9Sstevel@tonic-gate 	ASSERT((node_state_entry == DS_BOUND) ||
9977c478bd9Sstevel@tonic-gate 	    (node_state_entry == DS_INITIALIZED));
9987c478bd9Sstevel@tonic-gate 	pdip = ddi_get_parent(dip);
9997c478bd9Sstevel@tonic-gate 	ASSERT(pdip);
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "uninit_node: 0x%p(%s%d)\n",
10027c478bd9Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 	if (((ops = ddi_get_driver(pdip)) == NULL) ||
10057c478bd9Sstevel@tonic-gate 	    (ops->devo_bus_ops == NULL) ||
10067c478bd9Sstevel@tonic-gate 	    ((f = ops->devo_bus_ops->bus_ctl) == NULL)) {
10077c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
10087c478bd9Sstevel@tonic-gate 	}
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	/*
10117c478bd9Sstevel@tonic-gate 	 * save the @addr prior to DDI_CTLOPS_UNINITCHILD for use in
10127c478bd9Sstevel@tonic-gate 	 * freeing the instance if it succeeds.
10137c478bd9Sstevel@tonic-gate 	 */
10147c478bd9Sstevel@tonic-gate 	if (node_state_entry == DS_INITIALIZED) {
10157c478bd9Sstevel@tonic-gate 		addr = ddi_get_name_addr(dip);
10167c478bd9Sstevel@tonic-gate 		if (addr)
10177c478bd9Sstevel@tonic-gate 			addr = i_ddi_strdup(addr, KM_SLEEP);
10187c478bd9Sstevel@tonic-gate 	} else {
10197c478bd9Sstevel@tonic-gate 		addr = NULL;
10207c478bd9Sstevel@tonic-gate 	}
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	error = (*f)(pdip, pdip, DDI_CTLOPS_UNINITCHILD, dip, (void *)NULL);
10237c478bd9Sstevel@tonic-gate 	if (error == DDI_SUCCESS) {
10247c478bd9Sstevel@tonic-gate 		/* if uninitchild forgot to set devi_addr to NULL do it now */
10257c478bd9Sstevel@tonic-gate 		ddi_set_name_addr(dip, NULL);
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 		/*
10287c478bd9Sstevel@tonic-gate 		 * Free instance number. This is a no-op if instance has
10297c478bd9Sstevel@tonic-gate 		 * been kept by probe_node().  Avoid free when we are called
10307c478bd9Sstevel@tonic-gate 		 * from init_node (DS_BOUND) because the instance has not yet
10317c478bd9Sstevel@tonic-gate 		 * been assigned.
10327c478bd9Sstevel@tonic-gate 		 */
10337c478bd9Sstevel@tonic-gate 		if (node_state_entry == DS_INITIALIZED) {
10347c478bd9Sstevel@tonic-gate 			e_ddi_free_instance(dip, addr);
10357c478bd9Sstevel@tonic-gate 			DEVI(dip)->devi_instance = -1;
10367c478bd9Sstevel@tonic-gate 		}
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 		/* release the init_node hold */
10397c478bd9Sstevel@tonic-gate 		ndi_rele_devi(pdip);
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 		remove_global_props(dip);
1042f4da9be0Scth 
1043f4da9be0Scth 		/*
1044f4da9be0Scth 		 * NOTE: The decision on whether to allow a path-oriented
1045f4da9be0Scth 		 * rebind of a driver.conf enumerated node is made by
1046f4da9be0Scth 		 * init_node() based on driver_conf_allow_path_alias. The
1047f4da9be0Scth 		 * rebind code below prevents deletion of system properties
1048f4da9be0Scth 		 * on driver.conf nodes.
1049f4da9be0Scth 		 *
1050f4da9be0Scth 		 * When driver_conf_allow_path_alias is set, property behavior
1051f4da9be0Scth 		 * on rebound driver.conf file is non-intuitive. For a
1052f4da9be0Scth 		 * driver.conf node, the unit-address properties come from
1053f4da9be0Scth 		 * the driver.conf file as system properties. Removing system
1054f4da9be0Scth 		 * properties from a driver.conf node makes the node
1055f4da9be0Scth 		 * useless (we get node without unit-address properties) - so
1056f4da9be0Scth 		 * we leave system properties in place. The result is a node
1057f4da9be0Scth 		 * where system properties come from the node being rebound,
1058f4da9be0Scth 		 * and global properties come from the driver.conf file
1059f4da9be0Scth 		 * of the driver we are rebinding to.  If we could determine
1060f4da9be0Scth 		 * that the path-oriented alias driver.conf file defined a
1061f4da9be0Scth 		 * node at the same unit address, it would be best to use
1062f4da9be0Scth 		 * that node and avoid the non-intuitive property behavior.
1063f4da9be0Scth 		 * Unfortunately, the current "merge" code does not support
1064f4da9be0Scth 		 * this, so we live with the non-intuitive property behavior.
1065f4da9be0Scth 		 */
1066f4da9be0Scth 		if (!((ndi_dev_is_persistent_node(dip) == 0) &&
1067f4da9be0Scth 		    (DEVI(dip)->devi_flags & DEVI_REBIND)))
10687c478bd9Sstevel@tonic-gate 			e_ddi_prop_remove_all(dip);
10697c478bd9Sstevel@tonic-gate 	} else {
10707c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "uninit_node failed: 0x%p(%s%d)\n",
10717c478bd9Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
10727c478bd9Sstevel@tonic-gate 	}
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	if (addr)
10757c478bd9Sstevel@tonic-gate 		kmem_free(addr, strlen(addr) + 1);
10767c478bd9Sstevel@tonic-gate 	return (error);
10777c478bd9Sstevel@tonic-gate }
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate /*
10807c478bd9Sstevel@tonic-gate  * Invoke driver's probe entry point to probe for existence of hardware.
10817c478bd9Sstevel@tonic-gate  * Keep instance permanent for successful probe and leaf nodes.
10827c478bd9Sstevel@tonic-gate  *
10837c478bd9Sstevel@tonic-gate  * Per-driver list must be held busy while calling this function.
10847c478bd9Sstevel@tonic-gate  */
10857c478bd9Sstevel@tonic-gate static int
10867c478bd9Sstevel@tonic-gate probe_node(dev_info_t *dip)
10877c478bd9Sstevel@tonic-gate {
10887c478bd9Sstevel@tonic-gate 	int rv;
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_INITIALIZED);
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "probe_node: 0x%p(%s%d)\n",
10937c478bd9Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 	/* temporarily hold the driver while we probe */
10967c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
10977c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_ops == NULL) {
10987c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
10997c478bd9Sstevel@tonic-gate 		    "probe_node: 0x%p(%s%d) cannot load driver\n",
11007c478bd9Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
11017c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
11027c478bd9Sstevel@tonic-gate 	}
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 	if (identify_9e != 0)
11057c478bd9Sstevel@tonic-gate 		(void) devi_identify(dip);
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 	rv = devi_probe(dip);
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	/* release the driver now that probe is complete */
11107c478bd9Sstevel@tonic-gate 	ndi_rele_driver(dip);
11117c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_ops = NULL;
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 	switch (rv) {
11147c478bd9Sstevel@tonic-gate 	case DDI_PROBE_SUCCESS:			/* found */
11157c478bd9Sstevel@tonic-gate 	case DDI_PROBE_DONTCARE:		/* ddi_dev_is_sid */
11167c478bd9Sstevel@tonic-gate 		e_ddi_keep_instance(dip);	/* persist instance */
11177c478bd9Sstevel@tonic-gate 		rv = DDI_SUCCESS;
11187c478bd9Sstevel@tonic-gate 		break;
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	case DDI_PROBE_PARTIAL:			/* maybe later */
11217c478bd9Sstevel@tonic-gate 	case DDI_PROBE_FAILURE:			/* not found */
11227c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
11237c478bd9Sstevel@tonic-gate 		    "probe_node: 0x%p(%s%d) no hardware found%s\n",
11247c478bd9Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip),
11257c478bd9Sstevel@tonic-gate 		    (rv == DDI_PROBE_PARTIAL) ? " yet" : ""));
11267c478bd9Sstevel@tonic-gate 		rv = DDI_FAILURE;
11277c478bd9Sstevel@tonic-gate 		break;
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	default:
11307c478bd9Sstevel@tonic-gate #ifdef	DEBUG
11317c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "probe_node: %s%d: illegal probe(9E) value",
11327c478bd9Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
11337c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
11347c478bd9Sstevel@tonic-gate 		rv = DDI_FAILURE;
11357c478bd9Sstevel@tonic-gate 		break;
11367c478bd9Sstevel@tonic-gate 	}
11377c478bd9Sstevel@tonic-gate 	return (rv);
11387c478bd9Sstevel@tonic-gate }
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate /*
11417c478bd9Sstevel@tonic-gate  * Unprobe a node. Simply reset the node state.
11427c478bd9Sstevel@tonic-gate  * Per-driver list must be held busy while calling this function.
11437c478bd9Sstevel@tonic-gate  */
11447c478bd9Sstevel@tonic-gate static int
11457c478bd9Sstevel@tonic-gate unprobe_node(dev_info_t *dip)
11467c478bd9Sstevel@tonic-gate {
11477c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_PROBED);
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 	/*
11507c478bd9Sstevel@tonic-gate 	 * Don't check for references here or else a ref-counted
11517c478bd9Sstevel@tonic-gate 	 * dip cannot be downgraded by the framework.
11527c478bd9Sstevel@tonic-gate 	 */
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "unprobe_node: 0x%p(name = %s)\n",
11557c478bd9Sstevel@tonic-gate 	    (void *)dip, ddi_node_name(dip)));
11567c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
11577c478bd9Sstevel@tonic-gate }
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate /*
11607c478bd9Sstevel@tonic-gate  * Attach devinfo node.
11617c478bd9Sstevel@tonic-gate  * Per-driver list must be held busy.
11627c478bd9Sstevel@tonic-gate  */
11637c478bd9Sstevel@tonic-gate static int
11647c478bd9Sstevel@tonic-gate attach_node(dev_info_t *dip)
11657c478bd9Sstevel@tonic-gate {
11667c478bd9Sstevel@tonic-gate 	int rv;
11677c478bd9Sstevel@tonic-gate 
11685e3986cbScth 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
11697c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_PROBED);
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d)\n",
11727c478bd9Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 	/*
11757c478bd9Sstevel@tonic-gate 	 * Tell mpxio framework that a node is about to online.
11767c478bd9Sstevel@tonic-gate 	 */
11777c478bd9Sstevel@tonic-gate 	if ((rv = mdi_devi_online(dip, 0)) != NDI_SUCCESS) {
11787c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
11797c478bd9Sstevel@tonic-gate 	}
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 	/* no recursive attachment */
11827c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_ops == NULL);
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 	/*
11857c478bd9Sstevel@tonic-gate 	 * Hold driver the node is bound to.
11867c478bd9Sstevel@tonic-gate 	 */
11877c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
11887c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_ops == NULL) {
11897c478bd9Sstevel@tonic-gate 		/*
11907c478bd9Sstevel@tonic-gate 		 * We were able to load driver for probing, so we should
11917c478bd9Sstevel@tonic-gate 		 * not get here unless something really bad happened.
11927c478bd9Sstevel@tonic-gate 		 */
11937c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "attach_node: no driver for major %d",
11947c478bd9Sstevel@tonic-gate 		    DEVI(dip)->devi_major);
11957c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
11967c478bd9Sstevel@tonic-gate 	}
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 	if (NEXUS_DRV(DEVI(dip)->devi_ops))
11997c478bd9Sstevel@tonic-gate 		DEVI(dip)->devi_taskq = ddi_taskq_create(dip,
12007c478bd9Sstevel@tonic-gate 		    "nexus_enum_tq", 1,
12017c478bd9Sstevel@tonic-gate 		    TASKQ_DEFAULTPRI, 0);
12027c478bd9Sstevel@tonic-gate 
120316747f41Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
1204c73a93f2Sdm120769 	DEVI_SET_ATTACHING(dip);
12057c478bd9Sstevel@tonic-gate 	DEVI_SET_NEED_RESET(dip);
120616747f41Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
120716747f41Scth 
12087c478bd9Sstevel@tonic-gate 	rv = devi_attach(dip, DDI_ATTACH);
120916747f41Scth 
1210144dfaa9Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
1211c73a93f2Sdm120769 	DEVI_CLR_ATTACHING(dip);
1212144dfaa9Scth 
1213c73a93f2Sdm120769 	if (rv != DDI_SUCCESS) {
12145e3986cbScth 		DEVI_CLR_NEED_RESET(dip);
12155e3986cbScth 
1216225b11cdScth 		/* ensure that devids are unregistered */
12177c478bd9Sstevel@tonic-gate 		if (DEVI(dip)->devi_flags & DEVI_REGISTERED_DEVID) {
12187c478bd9Sstevel@tonic-gate 			DEVI(dip)->devi_flags &= ~DEVI_REGISTERED_DEVID;
1219225b11cdScth 			mutex_exit(&DEVI(dip)->devi_lock);
1220602ca9eaScth 			ddi_devid_unregister(dip);
1221225b11cdScth 		} else
1222225b11cdScth 			mutex_exit(&DEVI(dip)->devi_lock);
1223225b11cdScth 
12247c478bd9Sstevel@tonic-gate 		/*
12257c478bd9Sstevel@tonic-gate 		 * Cleanup dacf reservations
12267c478bd9Sstevel@tonic-gate 		 */
12277c478bd9Sstevel@tonic-gate 		mutex_enter(&dacf_lock);
12287c478bd9Sstevel@tonic-gate 		dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH);
12297c478bd9Sstevel@tonic-gate 		dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH);
12307c478bd9Sstevel@tonic-gate 		mutex_exit(&dacf_lock);
12317c478bd9Sstevel@tonic-gate 		if (DEVI(dip)->devi_taskq)
12327c478bd9Sstevel@tonic-gate 			ddi_taskq_destroy(DEVI(dip)->devi_taskq);
12337c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 		/* release the driver if attach failed */
12367c478bd9Sstevel@tonic-gate 		ndi_rele_driver(dip);
12377c478bd9Sstevel@tonic-gate 		DEVI(dip)->devi_ops = NULL;
12387c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d) failed\n",
12397c478bd9Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
12407c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
1241c73a93f2Sdm120769 	} else
1242c73a93f2Sdm120769 		mutex_exit(&DEVI(dip)->devi_lock);
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate 	/* successful attach, return with driver held */
124516747f41Scth 
12467c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
12477c478bd9Sstevel@tonic-gate }
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate /*
12507c478bd9Sstevel@tonic-gate  * Detach devinfo node.
12517c478bd9Sstevel@tonic-gate  * Per-driver list must be held busy.
12527c478bd9Sstevel@tonic-gate  */
12537c478bd9Sstevel@tonic-gate static int
12547c478bd9Sstevel@tonic-gate detach_node(dev_info_t *dip, uint_t flag)
12557c478bd9Sstevel@tonic-gate {
1256cfbaf6c3Scth 	struct devnames	*dnp;
12577c478bd9Sstevel@tonic-gate 	int		rv;
1258cfbaf6c3Scth 
12595e3986cbScth 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
12607c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_ATTACHED);
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 	/* check references */
12637c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_ref)
12647c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "detach_node: 0x%p(%s%d)\n",
12677c478bd9Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
12687c478bd9Sstevel@tonic-gate 
12695e3986cbScth 	/*
12705e3986cbScth 	 * NOTE: If we are processing a pHCI node then the calling code
12715e3986cbScth 	 * must detect this and ndi_devi_enter() in (vHCI, parent(pHCI))
12725e3986cbScth 	 * order unless pHCI and vHCI are siblings.  Code paths leading
12735e3986cbScth 	 * here that must ensure this ordering include:
12745e3986cbScth 	 * unconfig_immediate_children(), devi_unconfig_one(),
12755e3986cbScth 	 * ndi_devi_unconfig_one(), ndi_devi_offline().
12765e3986cbScth 	 */
12775e3986cbScth 	ASSERT(!MDI_PHCI(dip) ||
12785e3986cbScth 	    (ddi_get_parent(mdi_devi_get_vdip(dip)) == ddi_get_parent(dip)) ||
12795e3986cbScth 	    DEVI_BUSY_OWNED(mdi_devi_get_vdip(dip)));
12805e3986cbScth 
12817c478bd9Sstevel@tonic-gate 	/* Offline the device node with the mpxio framework. */
12827c478bd9Sstevel@tonic-gate 	if (mdi_devi_offline(dip, flag) != NDI_SUCCESS) {
12837c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
12847c478bd9Sstevel@tonic-gate 	}
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate 	/* drain the taskq */
12877c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_taskq)
12887c478bd9Sstevel@tonic-gate 		ddi_taskq_wait(DEVI(dip)->devi_taskq);
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 	rv = devi_detach(dip, DDI_DETACH);
12917c478bd9Sstevel@tonic-gate 
12927c478bd9Sstevel@tonic-gate 	if (rv != DDI_SUCCESS) {
12937c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
12947c478bd9Sstevel@tonic-gate 		    "detach_node: 0x%p(%s%d) failed\n",
12957c478bd9Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
12967c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
12977c478bd9Sstevel@tonic-gate 	}
12987c478bd9Sstevel@tonic-gate 
12995e3986cbScth 	mutex_enter(&(DEVI(dip)->devi_lock));
13005e3986cbScth 	DEVI_CLR_NEED_RESET(dip);
13015e3986cbScth 	mutex_exit(&(DEVI(dip)->devi_lock));
13025e3986cbScth 
13037c478bd9Sstevel@tonic-gate 	/* destroy the taskq */
13047c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_taskq) {
13057c478bd9Sstevel@tonic-gate 		ddi_taskq_destroy(DEVI(dip)->devi_taskq);
13067c478bd9Sstevel@tonic-gate 		DEVI(dip)->devi_taskq = NULL;
13077c478bd9Sstevel@tonic-gate 	}
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate 	/* Cleanup dacf reservations */
13107c478bd9Sstevel@tonic-gate 	mutex_enter(&dacf_lock);
13117c478bd9Sstevel@tonic-gate 	dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH);
13127c478bd9Sstevel@tonic-gate 	dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH);
13137c478bd9Sstevel@tonic-gate 	mutex_exit(&dacf_lock);
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 	/* Remove properties and minor nodes in case driver forgots */
13167c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
13177c478bd9Sstevel@tonic-gate 	ddi_prop_remove_all(dip);
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate 	/* a detached node can't have attached or .conf children */
13207c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
13217c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN|DEVI_ATTACHED_CHILDREN);
1322225b11cdScth 
1323225b11cdScth 	/* ensure that devids registered during attach are unregistered */
13247c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_flags & DEVI_REGISTERED_DEVID) {
13257c478bd9Sstevel@tonic-gate 		DEVI(dip)->devi_flags &= ~DEVI_REGISTERED_DEVID;
1326225b11cdScth 		mutex_exit(&DEVI(dip)->devi_lock);
1327602ca9eaScth 		ddi_devid_unregister(dip);
1328225b11cdScth 	} else
13297c478bd9Sstevel@tonic-gate 		mutex_exit(&DEVI(dip)->devi_lock);
13307c478bd9Sstevel@tonic-gate 
1331cfbaf6c3Scth 	/*
1332cfbaf6c3Scth 	 * If the instance has successfully detached in detach_driver() context,
1333cfbaf6c3Scth 	 * clear DN_DRIVER_HELD for correct ddi_hold_installed_driver()
1334cfbaf6c3Scth 	 * behavior. Consumers like qassociate() depend on this (via clnopen()).
1335cfbaf6c3Scth 	 */
1336cfbaf6c3Scth 	if (flag & NDI_DETACH_DRIVER) {
1337cfbaf6c3Scth 		dnp = &(devnamesp[DEVI(dip)->devi_major]);
1338cfbaf6c3Scth 		LOCK_DEV_OPS(&dnp->dn_lock);
1339cfbaf6c3Scth 		dnp->dn_flags &= ~DN_DRIVER_HELD;
1340cfbaf6c3Scth 		UNLOCK_DEV_OPS(&dnp->dn_lock);
1341cfbaf6c3Scth 	}
1342cfbaf6c3Scth 
13437c478bd9Sstevel@tonic-gate 	/* successful detach, release the driver */
13447c478bd9Sstevel@tonic-gate 	ndi_rele_driver(dip);
13457c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_ops = NULL;
13467c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
13477c478bd9Sstevel@tonic-gate }
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate /*
13507c478bd9Sstevel@tonic-gate  * Run dacf post_attach routines
13517c478bd9Sstevel@tonic-gate  */
13527c478bd9Sstevel@tonic-gate static int
13537c478bd9Sstevel@tonic-gate postattach_node(dev_info_t *dip)
13547c478bd9Sstevel@tonic-gate {
13557c478bd9Sstevel@tonic-gate 	int rval;
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate 	/*
13587c478bd9Sstevel@tonic-gate 	 * For hotplug busses like USB, it's possible that devices
13597c478bd9Sstevel@tonic-gate 	 * are removed but dip is still around. We don't want to
13607c478bd9Sstevel@tonic-gate 	 * run dacf routines as part of detach failure recovery.
13617c478bd9Sstevel@tonic-gate 	 *
13627c478bd9Sstevel@tonic-gate 	 * Pretend success until we figure out how to prevent
13637c478bd9Sstevel@tonic-gate 	 * access to such devinfo nodes.
13647c478bd9Sstevel@tonic-gate 	 */
13657c478bd9Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_REMOVED(dip))
13667c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 	/*
13697c478bd9Sstevel@tonic-gate 	 * if dacf_postattach failed, report it to the framework
13707c478bd9Sstevel@tonic-gate 	 * so that it can be retried later at the open time.
13717c478bd9Sstevel@tonic-gate 	 */
13727c478bd9Sstevel@tonic-gate 	mutex_enter(&dacf_lock);
13737c478bd9Sstevel@tonic-gate 	rval = dacfc_postattach(dip);
13747c478bd9Sstevel@tonic-gate 	mutex_exit(&dacf_lock);
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate 	/*
13777c478bd9Sstevel@tonic-gate 	 * Plumbing during postattach may fail because of the
13787c478bd9Sstevel@tonic-gate 	 * underlying device is not ready. This will fail ndi_devi_config()
13797c478bd9Sstevel@tonic-gate 	 * in dv_filldir() and a warning message is issued. The message
13807c478bd9Sstevel@tonic-gate 	 * from here will explain what happened
13817c478bd9Sstevel@tonic-gate 	 */
13827c478bd9Sstevel@tonic-gate 	if (rval != DACF_SUCCESS) {
13837c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "Postattach failed for %s%d\n",
13847c478bd9Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
13857c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
13867c478bd9Sstevel@tonic-gate 	}
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
13897c478bd9Sstevel@tonic-gate }
13907c478bd9Sstevel@tonic-gate 
13917c478bd9Sstevel@tonic-gate /*
13927c478bd9Sstevel@tonic-gate  * Run dacf pre-detach routines
13937c478bd9Sstevel@tonic-gate  */
13947c478bd9Sstevel@tonic-gate static int
13957c478bd9Sstevel@tonic-gate predetach_node(dev_info_t *dip, uint_t flag)
13967c478bd9Sstevel@tonic-gate {
13977c478bd9Sstevel@tonic-gate 	int ret;
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 	/*
14007c478bd9Sstevel@tonic-gate 	 * Don't auto-detach if DDI_FORCEATTACH or DDI_NO_AUTODETACH
14017c478bd9Sstevel@tonic-gate 	 * properties are set.
14027c478bd9Sstevel@tonic-gate 	 */
14037c478bd9Sstevel@tonic-gate 	if (flag & NDI_AUTODETACH) {
14047c478bd9Sstevel@tonic-gate 		struct devnames *dnp;
14057c478bd9Sstevel@tonic-gate 		int pflag = DDI_PROP_NOTPROM | DDI_PROP_DONTPASS;
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate 		if ((ddi_prop_get_int(DDI_DEV_T_ANY, dip,
14087c478bd9Sstevel@tonic-gate 		    pflag, DDI_FORCEATTACH, 0) == 1) ||
14097c478bd9Sstevel@tonic-gate 		    (ddi_prop_get_int(DDI_DEV_T_ANY, dip,
14107c478bd9Sstevel@tonic-gate 		    pflag, DDI_NO_AUTODETACH, 0) == 1))
14117c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 		/* check for driver global version of DDI_NO_AUTODETACH */
14147c478bd9Sstevel@tonic-gate 		dnp = &devnamesp[DEVI(dip)->devi_major];
14157c478bd9Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
14167c478bd9Sstevel@tonic-gate 		if (dnp->dn_flags & DN_NO_AUTODETACH) {
14177c478bd9Sstevel@tonic-gate 			UNLOCK_DEV_OPS(&dnp->dn_lock);
14187c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14197c478bd9Sstevel@tonic-gate 		}
14207c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
14217c478bd9Sstevel@tonic-gate 	}
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 	mutex_enter(&dacf_lock);
14247c478bd9Sstevel@tonic-gate 	ret = dacfc_predetach(dip);
14257c478bd9Sstevel@tonic-gate 	mutex_exit(&dacf_lock);
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate 	return (ret);
14287c478bd9Sstevel@tonic-gate }
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate /*
14317c478bd9Sstevel@tonic-gate  * Wrapper for making multiple state transitions
14327c478bd9Sstevel@tonic-gate  */
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate /*
14357c478bd9Sstevel@tonic-gate  * i_ndi_config_node: upgrade dev_info node into a specified state.
14367c478bd9Sstevel@tonic-gate  * It is a bit tricky because the locking protocol changes before and
14377c478bd9Sstevel@tonic-gate  * after a node is bound to a driver. All locks are held external to
14387c478bd9Sstevel@tonic-gate  * this function.
14397c478bd9Sstevel@tonic-gate  */
14407c478bd9Sstevel@tonic-gate int
14417c478bd9Sstevel@tonic-gate i_ndi_config_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag)
14427c478bd9Sstevel@tonic-gate {
14437c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flag))
14447c478bd9Sstevel@tonic-gate 	int rv = DDI_SUCCESS;
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 	while ((i_ddi_node_state(dip) < state) && (rv == DDI_SUCCESS)) {
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate 		/* don't allow any more changes to the device tree */
14517c478bd9Sstevel@tonic-gate 		if (devinfo_freeze) {
14527c478bd9Sstevel@tonic-gate 			rv = DDI_FAILURE;
14537c478bd9Sstevel@tonic-gate 			break;
14547c478bd9Sstevel@tonic-gate 		}
14557c478bd9Sstevel@tonic-gate 
14567c478bd9Sstevel@tonic-gate 		switch (i_ddi_node_state(dip)) {
14577c478bd9Sstevel@tonic-gate 		case DS_PROTO:
14587c478bd9Sstevel@tonic-gate 			/*
14597c478bd9Sstevel@tonic-gate 			 * only caller can reference this node, no external
14607c478bd9Sstevel@tonic-gate 			 * locking needed.
14617c478bd9Sstevel@tonic-gate 			 */
14627c478bd9Sstevel@tonic-gate 			link_node(dip);
14637c478bd9Sstevel@tonic-gate 			i_ddi_set_node_state(dip, DS_LINKED);
14647c478bd9Sstevel@tonic-gate 			break;
14657c478bd9Sstevel@tonic-gate 		case DS_LINKED:
14667c478bd9Sstevel@tonic-gate 			/*
14677c478bd9Sstevel@tonic-gate 			 * Three code path may attempt to bind a node:
14687c478bd9Sstevel@tonic-gate 			 * - boot code
14697c478bd9Sstevel@tonic-gate 			 * - add_drv
14707c478bd9Sstevel@tonic-gate 			 * - hotplug thread
14717c478bd9Sstevel@tonic-gate 			 * Boot code is single threaded, add_drv synchronize
14727c478bd9Sstevel@tonic-gate 			 * on a userland lock, and hotplug synchronize on
14737c478bd9Sstevel@tonic-gate 			 * hotplug_lk. There could be a race between add_drv
14747c478bd9Sstevel@tonic-gate 			 * and hotplug thread. We'll live with this until the
14757c478bd9Sstevel@tonic-gate 			 * conversion to top-down loading.
14767c478bd9Sstevel@tonic-gate 			 */
14777c478bd9Sstevel@tonic-gate 			if ((rv = bind_node(dip)) == DDI_SUCCESS)
14787c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_BOUND);
1479f4da9be0Scth 
14807c478bd9Sstevel@tonic-gate 			break;
14817c478bd9Sstevel@tonic-gate 		case DS_BOUND:
14827c478bd9Sstevel@tonic-gate 			/*
14837c478bd9Sstevel@tonic-gate 			 * The following transitions synchronizes on the
14847c478bd9Sstevel@tonic-gate 			 * per-driver busy changing flag, since we already
14857c478bd9Sstevel@tonic-gate 			 * have a driver.
14867c478bd9Sstevel@tonic-gate 			 */
14877c478bd9Sstevel@tonic-gate 			if ((rv = init_node(dip)) == DDI_SUCCESS)
14887c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_INITIALIZED);
14897c478bd9Sstevel@tonic-gate 			break;
14907c478bd9Sstevel@tonic-gate 		case DS_INITIALIZED:
14917c478bd9Sstevel@tonic-gate 			if ((rv = probe_node(dip)) == DDI_SUCCESS)
14927c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_PROBED);
14937c478bd9Sstevel@tonic-gate 			break;
14947c478bd9Sstevel@tonic-gate 		case DS_PROBED:
149525e8c5aaSvikram 			i_ddi_check_retire(dip);
14967c478bd9Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, 1);
14977c478bd9Sstevel@tonic-gate 			if ((rv = attach_node(dip)) == DDI_SUCCESS)
14987c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_ATTACHED);
14997c478bd9Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, -1);
15007c478bd9Sstevel@tonic-gate 			break;
15017c478bd9Sstevel@tonic-gate 		case DS_ATTACHED:
15027c478bd9Sstevel@tonic-gate 			if ((rv = postattach_node(dip)) == DDI_SUCCESS)
15037c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_READY);
15047c478bd9Sstevel@tonic-gate 			break;
15057c478bd9Sstevel@tonic-gate 		case DS_READY:
15067c478bd9Sstevel@tonic-gate 			break;
15077c478bd9Sstevel@tonic-gate 		default:
15087c478bd9Sstevel@tonic-gate 			/* should never reach here */
15097c478bd9Sstevel@tonic-gate 			ASSERT("unknown devinfo state");
15107c478bd9Sstevel@tonic-gate 		}
15117c478bd9Sstevel@tonic-gate 	}
15127c478bd9Sstevel@tonic-gate 
15137c478bd9Sstevel@tonic-gate 	if (ddidebug & DDI_AUDIT)
15147c478bd9Sstevel@tonic-gate 		da_log_enter(dip);
15157c478bd9Sstevel@tonic-gate 	return (rv);
15167c478bd9Sstevel@tonic-gate }
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate /*
15197c478bd9Sstevel@tonic-gate  * i_ndi_unconfig_node: downgrade dev_info node into a specified state.
15207c478bd9Sstevel@tonic-gate  */
15217c478bd9Sstevel@tonic-gate int
15227c478bd9Sstevel@tonic-gate i_ndi_unconfig_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag)
15237c478bd9Sstevel@tonic-gate {
15247c478bd9Sstevel@tonic-gate 	int	rv = DDI_SUCCESS;
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 	while ((i_ddi_node_state(dip) > state) && (rv == DDI_SUCCESS)) {
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate 		/* don't allow any more changes to the device tree */
15317c478bd9Sstevel@tonic-gate 		if (devinfo_freeze) {
15327c478bd9Sstevel@tonic-gate 			rv = DDI_FAILURE;
15337c478bd9Sstevel@tonic-gate 			break;
15347c478bd9Sstevel@tonic-gate 		}
15357c478bd9Sstevel@tonic-gate 
15367c478bd9Sstevel@tonic-gate 		switch (i_ddi_node_state(dip)) {
15377c478bd9Sstevel@tonic-gate 		case DS_PROTO:
15387c478bd9Sstevel@tonic-gate 			break;
15397c478bd9Sstevel@tonic-gate 		case DS_LINKED:
15407c478bd9Sstevel@tonic-gate 			/*
15417c478bd9Sstevel@tonic-gate 			 * Persistent nodes are only removed by hotplug code
15427c478bd9Sstevel@tonic-gate 			 * .conf nodes synchronizes on per-driver list.
15437c478bd9Sstevel@tonic-gate 			 */
15447c478bd9Sstevel@tonic-gate 			if ((rv = unlink_node(dip)) == DDI_SUCCESS)
15457c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_PROTO);
15467c478bd9Sstevel@tonic-gate 			break;
15477c478bd9Sstevel@tonic-gate 		case DS_BOUND:
15487c478bd9Sstevel@tonic-gate 			/*
15497c478bd9Sstevel@tonic-gate 			 * The following transitions synchronizes on the
15507c478bd9Sstevel@tonic-gate 			 * per-driver busy changing flag, since we already
15517c478bd9Sstevel@tonic-gate 			 * have a driver.
15527c478bd9Sstevel@tonic-gate 			 */
15537c478bd9Sstevel@tonic-gate 			if ((rv = unbind_node(dip)) == DDI_SUCCESS)
15547c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_LINKED);
15557c478bd9Sstevel@tonic-gate 			break;
15567c478bd9Sstevel@tonic-gate 		case DS_INITIALIZED:
15577c478bd9Sstevel@tonic-gate 			if ((rv = uninit_node(dip)) == DDI_SUCCESS)
15587c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_BOUND);
15597c478bd9Sstevel@tonic-gate 			break;
15607c478bd9Sstevel@tonic-gate 		case DS_PROBED:
15617c478bd9Sstevel@tonic-gate 			if ((rv = unprobe_node(dip)) == DDI_SUCCESS)
15627c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_INITIALIZED);
15637c478bd9Sstevel@tonic-gate 			break;
15647c478bd9Sstevel@tonic-gate 		case DS_ATTACHED:
15657c478bd9Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, 1);
156616747f41Scth 
156716747f41Scth 			mutex_enter(&(DEVI(dip)->devi_lock));
15687c478bd9Sstevel@tonic-gate 			DEVI_SET_DETACHING(dip);
156916747f41Scth 			mutex_exit(&(DEVI(dip)->devi_lock));
157016747f41Scth 
15717c478bd9Sstevel@tonic-gate 			membar_enter();	/* ensure visibility for hold_devi */
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 			if ((rv = detach_node(dip, flag)) == DDI_SUCCESS)
15747c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_PROBED);
157516747f41Scth 
157616747f41Scth 			mutex_enter(&(DEVI(dip)->devi_lock));
15777c478bd9Sstevel@tonic-gate 			DEVI_CLR_DETACHING(dip);
157816747f41Scth 			mutex_exit(&(DEVI(dip)->devi_lock));
157916747f41Scth 
15807c478bd9Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, -1);
15817c478bd9Sstevel@tonic-gate 			break;
15827c478bd9Sstevel@tonic-gate 		case DS_READY:
15837c478bd9Sstevel@tonic-gate 			if ((rv = predetach_node(dip, flag)) == DDI_SUCCESS)
15847c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_ATTACHED);
15857c478bd9Sstevel@tonic-gate 			break;
15867c478bd9Sstevel@tonic-gate 		default:
15877c478bd9Sstevel@tonic-gate 			ASSERT("unknown devinfo state");
15887c478bd9Sstevel@tonic-gate 		}
15897c478bd9Sstevel@tonic-gate 	}
15907c478bd9Sstevel@tonic-gate 	da_log_enter(dip);
15917c478bd9Sstevel@tonic-gate 	return (rv);
15927c478bd9Sstevel@tonic-gate }
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate /*
15957c478bd9Sstevel@tonic-gate  * ddi_initchild: transform node to DS_INITIALIZED state
15967c478bd9Sstevel@tonic-gate  */
15977c478bd9Sstevel@tonic-gate int
15987c478bd9Sstevel@tonic-gate ddi_initchild(dev_info_t *parent, dev_info_t *proto)
15997c478bd9Sstevel@tonic-gate {
16007c478bd9Sstevel@tonic-gate 	int ret, circ;
16017c478bd9Sstevel@tonic-gate 
16027c478bd9Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
16037c478bd9Sstevel@tonic-gate 	ret = i_ndi_config_node(proto, DS_INITIALIZED, 0);
16047c478bd9Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate 	return (ret);
16077c478bd9Sstevel@tonic-gate }
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate /*
16107c478bd9Sstevel@tonic-gate  * ddi_uninitchild: transform node down to DS_BOUND state
16117c478bd9Sstevel@tonic-gate  */
16127c478bd9Sstevel@tonic-gate int
16137c478bd9Sstevel@tonic-gate ddi_uninitchild(dev_info_t *dip)
16147c478bd9Sstevel@tonic-gate {
16157c478bd9Sstevel@tonic-gate 	int ret, circ;
16167c478bd9Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
16177c478bd9Sstevel@tonic-gate 	ASSERT(parent);
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
16207c478bd9Sstevel@tonic-gate 	ret = i_ndi_unconfig_node(dip, DS_BOUND, 0);
16217c478bd9Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate 	return (ret);
16247c478bd9Sstevel@tonic-gate }
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate /*
1627737d277aScth  * i_ddi_attachchild: transform node to DS_READY/i_ddi_devi_attached() state
16287c478bd9Sstevel@tonic-gate  */
16297c478bd9Sstevel@tonic-gate static int
16307c478bd9Sstevel@tonic-gate i_ddi_attachchild(dev_info_t *dip)
16317c478bd9Sstevel@tonic-gate {
16327c478bd9Sstevel@tonic-gate 	dev_info_t	*parent = ddi_get_parent(dip);
16335e3986cbScth 	int		ret;
16345e3986cbScth 
16355e3986cbScth 	ASSERT(parent && DEVI_BUSY_OWNED(parent));
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate 	if ((i_ddi_node_state(dip) < DS_BOUND) || DEVI_IS_DEVICE_OFFLINE(dip))
16387c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 	ret = i_ndi_config_node(dip, DS_READY, 0);
16417c478bd9Sstevel@tonic-gate 	if (ret == NDI_SUCCESS) {
16427c478bd9Sstevel@tonic-gate 		ret = DDI_SUCCESS;
16437c478bd9Sstevel@tonic-gate 	} else {
16447c478bd9Sstevel@tonic-gate 		/*
16457c478bd9Sstevel@tonic-gate 		 * Take it down to DS_INITIALIZED so pm_pre_probe is run
16467c478bd9Sstevel@tonic-gate 		 * on the next attach
16477c478bd9Sstevel@tonic-gate 		 */
16487c478bd9Sstevel@tonic-gate 		(void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0);
16497c478bd9Sstevel@tonic-gate 		ret = DDI_FAILURE;
16507c478bd9Sstevel@tonic-gate 	}
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate 	return (ret);
16537c478bd9Sstevel@tonic-gate }
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate /*
16567c478bd9Sstevel@tonic-gate  * i_ddi_detachchild: transform node down to DS_PROBED state
16577c478bd9Sstevel@tonic-gate  *	If it fails, put it back to DS_READY state.
16587c478bd9Sstevel@tonic-gate  * NOTE: A node that fails detach may be at DS_ATTACHED instead
1659737d277aScth  * of DS_READY for a small amount of time - this is the source of
1660737d277aScth  * transient DS_READY->DS_ATTACHED->DS_READY state changes.
16617c478bd9Sstevel@tonic-gate  */
16627c478bd9Sstevel@tonic-gate static int
16637c478bd9Sstevel@tonic-gate i_ddi_detachchild(dev_info_t *dip, uint_t flags)
16647c478bd9Sstevel@tonic-gate {
16657c478bd9Sstevel@tonic-gate 	dev_info_t	*parent = ddi_get_parent(dip);
16665e3986cbScth 	int		ret;
16677c478bd9Sstevel@tonic-gate 
16685e3986cbScth 	ASSERT(parent && DEVI_BUSY_OWNED(parent));
16695e3986cbScth 
16707c478bd9Sstevel@tonic-gate 	ret = i_ndi_unconfig_node(dip, DS_PROBED, flags);
16717c478bd9Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
16727c478bd9Sstevel@tonic-gate 		(void) i_ndi_config_node(dip, DS_READY, 0);
16737c478bd9Sstevel@tonic-gate 	else
16747c478bd9Sstevel@tonic-gate 		/* allow pm_pre_probe to reestablish pm state */
16757c478bd9Sstevel@tonic-gate 		(void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0);
16767c478bd9Sstevel@tonic-gate 	return (ret);
16777c478bd9Sstevel@tonic-gate }
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate /*
16807c478bd9Sstevel@tonic-gate  * Add a child and bind to driver
16817c478bd9Sstevel@tonic-gate  */
16827c478bd9Sstevel@tonic-gate dev_info_t *
16837c478bd9Sstevel@tonic-gate ddi_add_child(dev_info_t *pdip, char *name, uint_t nodeid, uint_t unit)
16847c478bd9Sstevel@tonic-gate {
16857c478bd9Sstevel@tonic-gate 	int circ;
16867c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate 	/* allocate a new node */
16897c478bd9Sstevel@tonic-gate 	dip = i_ddi_alloc_node(pdip, name, nodeid, (int)unit, NULL, KM_SLEEP);
16907c478bd9Sstevel@tonic-gate 
16917c478bd9Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
16927c478bd9Sstevel@tonic-gate 	(void) i_ndi_config_node(dip, DS_BOUND, 0);
16937c478bd9Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
16947c478bd9Sstevel@tonic-gate 	return (dip);
16957c478bd9Sstevel@tonic-gate }
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate /*
16987c478bd9Sstevel@tonic-gate  * ddi_remove_child: remove the dip. The parent must be attached and held
16997c478bd9Sstevel@tonic-gate  */
17007c478bd9Sstevel@tonic-gate int
17017c478bd9Sstevel@tonic-gate ddi_remove_child(dev_info_t *dip, int dummy)
17027c478bd9Sstevel@tonic-gate {
17037c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dummy))
17047c478bd9Sstevel@tonic-gate 	int circ, ret;
17057c478bd9Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
17067c478bd9Sstevel@tonic-gate 	ASSERT(parent);
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate 	/*
17117c478bd9Sstevel@tonic-gate 	 * If we still have children, for example SID nodes marked
17127c478bd9Sstevel@tonic-gate 	 * as persistent but not attached, attempt to remove them.
17137c478bd9Sstevel@tonic-gate 	 */
17147c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_child) {
17157c478bd9Sstevel@tonic-gate 		ret = ndi_devi_unconfig(dip, NDI_DEVI_REMOVE);
17167c478bd9Sstevel@tonic-gate 		if (ret != NDI_SUCCESS) {
17177c478bd9Sstevel@tonic-gate 			ndi_devi_exit(parent, circ);
17187c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
17197c478bd9Sstevel@tonic-gate 		}
17207c478bd9Sstevel@tonic-gate 		ASSERT(DEVI(dip)->devi_child == NULL);
17217c478bd9Sstevel@tonic-gate 	}
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 	ret = i_ndi_unconfig_node(dip, DS_PROTO, 0);
17247c478bd9Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
17257c478bd9Sstevel@tonic-gate 
17267c478bd9Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
17277c478bd9Sstevel@tonic-gate 		return (ret);
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_PROTO);
17307c478bd9Sstevel@tonic-gate 	i_ddi_free_node(dip);
17317c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
17327c478bd9Sstevel@tonic-gate }
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate /*
17357c478bd9Sstevel@tonic-gate  * NDI wrappers for ref counting, node allocation, and transitions
17367c478bd9Sstevel@tonic-gate  */
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate /*
17397c478bd9Sstevel@tonic-gate  * Hold/release the devinfo node itself.
17407c478bd9Sstevel@tonic-gate  * Caller is assumed to prevent the devi from detaching during this call
17417c478bd9Sstevel@tonic-gate  */
17427c478bd9Sstevel@tonic-gate void
17437c478bd9Sstevel@tonic-gate ndi_hold_devi(dev_info_t *dip)
17447c478bd9Sstevel@tonic-gate {
17457c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
17467c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_ref >= 0);
17477c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_ref++;
17487c478bd9Sstevel@tonic-gate 	membar_enter();			/* make sure stores are flushed */
17497c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
17507c478bd9Sstevel@tonic-gate }
17517c478bd9Sstevel@tonic-gate 
17527c478bd9Sstevel@tonic-gate void
17537c478bd9Sstevel@tonic-gate ndi_rele_devi(dev_info_t *dip)
17547c478bd9Sstevel@tonic-gate {
17557c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_ref > 0);
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
17587c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_ref--;
17597c478bd9Sstevel@tonic-gate 	membar_enter();			/* make sure stores are flushed */
17607c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
17617c478bd9Sstevel@tonic-gate }
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate int
17647c478bd9Sstevel@tonic-gate e_ddi_devi_holdcnt(dev_info_t *dip)
17657c478bd9Sstevel@tonic-gate {
17667c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_ref);
17677c478bd9Sstevel@tonic-gate }
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate /*
17707c478bd9Sstevel@tonic-gate  * Hold/release the driver the devinfo node is bound to.
17717c478bd9Sstevel@tonic-gate  */
17727c478bd9Sstevel@tonic-gate struct dev_ops *
17737c478bd9Sstevel@tonic-gate ndi_hold_driver(dev_info_t *dip)
17747c478bd9Sstevel@tonic-gate {
17757c478bd9Sstevel@tonic-gate 	if (i_ddi_node_state(dip) < DS_BOUND)
17767c478bd9Sstevel@tonic-gate 		return (NULL);
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_major != -1);
17797c478bd9Sstevel@tonic-gate 	return (mod_hold_dev_by_major(DEVI(dip)->devi_major));
17807c478bd9Sstevel@tonic-gate }
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate void
17837c478bd9Sstevel@tonic-gate ndi_rele_driver(dev_info_t *dip)
17847c478bd9Sstevel@tonic-gate {
17857c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) >= DS_BOUND);
17867c478bd9Sstevel@tonic-gate 	mod_rele_dev_by_major(DEVI(dip)->devi_major);
17877c478bd9Sstevel@tonic-gate }
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate /*
1790b9ccdc5aScth  * Single thread entry into devinfo node for modifying its children (devinfo,
1791b9ccdc5aScth  * pathinfo, and minor). To verify in ASSERTS use DEVI_BUSY_OWNED macro.
17927c478bd9Sstevel@tonic-gate  */
17937c478bd9Sstevel@tonic-gate void
17947c478bd9Sstevel@tonic-gate ndi_devi_enter(dev_info_t *dip, int *circular)
17957c478bd9Sstevel@tonic-gate {
17967c478bd9Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
17977c478bd9Sstevel@tonic-gate 	ASSERT(dip != NULL);
17987c478bd9Sstevel@tonic-gate 
17995e3986cbScth 	/* for vHCI, enforce (vHCI, pHCI) ndi_deve_enter() order */
18005e3986cbScth 	ASSERT(!MDI_VHCI(dip) || (mdi_devi_pdip_entered(dip) == 0) ||
18015e3986cbScth 	    DEVI_BUSY_OWNED(dip));
18025e3986cbScth 
18037c478bd9Sstevel@tonic-gate 	mutex_enter(&devi->devi_lock);
18047c478bd9Sstevel@tonic-gate 	if (devi->devi_busy_thread == curthread) {
18057c478bd9Sstevel@tonic-gate 		devi->devi_circular++;
18067c478bd9Sstevel@tonic-gate 	} else {
18077c478bd9Sstevel@tonic-gate 		while (DEVI_BUSY_CHANGING(devi) && !panicstr)
18087c478bd9Sstevel@tonic-gate 			cv_wait(&(devi->devi_cv), &(devi->devi_lock));
18097c478bd9Sstevel@tonic-gate 		if (panicstr) {
18107c478bd9Sstevel@tonic-gate 			mutex_exit(&devi->devi_lock);
18117c478bd9Sstevel@tonic-gate 			return;
18127c478bd9Sstevel@tonic-gate 		}
18137c478bd9Sstevel@tonic-gate 		devi->devi_flags |= DEVI_BUSY;
18147c478bd9Sstevel@tonic-gate 		devi->devi_busy_thread = curthread;
18157c478bd9Sstevel@tonic-gate 	}
18167c478bd9Sstevel@tonic-gate 	*circular = devi->devi_circular;
18177c478bd9Sstevel@tonic-gate 	mutex_exit(&devi->devi_lock);
18187c478bd9Sstevel@tonic-gate }
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate /*
18217c478bd9Sstevel@tonic-gate  * Release ndi_devi_enter or successful ndi_devi_tryenter.
18227c478bd9Sstevel@tonic-gate  */
18237c478bd9Sstevel@tonic-gate void
18247c478bd9Sstevel@tonic-gate ndi_devi_exit(dev_info_t *dip, int circular)
18257c478bd9Sstevel@tonic-gate {
18267c478bd9Sstevel@tonic-gate 	struct dev_info	*devi = DEVI(dip);
18275e3986cbScth 	struct dev_info	*vdevi;
18287c478bd9Sstevel@tonic-gate 	ASSERT(dip != NULL);
18297c478bd9Sstevel@tonic-gate 
18307c478bd9Sstevel@tonic-gate 	if (panicstr)
18317c478bd9Sstevel@tonic-gate 		return;
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate 	mutex_enter(&(devi->devi_lock));
18347c478bd9Sstevel@tonic-gate 	if (circular != 0) {
18357c478bd9Sstevel@tonic-gate 		devi->devi_circular--;
18367c478bd9Sstevel@tonic-gate 	} else {
18377c478bd9Sstevel@tonic-gate 		devi->devi_flags &= ~DEVI_BUSY;
18387c478bd9Sstevel@tonic-gate 		ASSERT(devi->devi_busy_thread == curthread);
18397c478bd9Sstevel@tonic-gate 		devi->devi_busy_thread = NULL;
18407c478bd9Sstevel@tonic-gate 		cv_broadcast(&(devi->devi_cv));
18417c478bd9Sstevel@tonic-gate 	}
18427c478bd9Sstevel@tonic-gate 	mutex_exit(&(devi->devi_lock));
18435e3986cbScth 
18445e3986cbScth 	/*
18455e3986cbScth 	 * For pHCI exit we issue a broadcast to vHCI for ndi_devi_config_one()
18465e3986cbScth 	 * doing cv_wait on vHCI.
18475e3986cbScth 	 */
18485e3986cbScth 	if (MDI_PHCI(dip)) {
18495e3986cbScth 		vdevi = DEVI(mdi_devi_get_vdip(dip));
18505e3986cbScth 		if (vdevi) {
18515e3986cbScth 			mutex_enter(&(vdevi->devi_lock));
18525e3986cbScth 			if (vdevi->devi_flags & DEVI_PHCI_SIGNALS_VHCI) {
18535e3986cbScth 				vdevi->devi_flags &= ~DEVI_PHCI_SIGNALS_VHCI;
18545e3986cbScth 				cv_broadcast(&(vdevi->devi_cv));
18555e3986cbScth 			}
18565e3986cbScth 			mutex_exit(&(vdevi->devi_lock));
18575e3986cbScth 		}
18585e3986cbScth 	}
18595e3986cbScth }
18605e3986cbScth 
18615e3986cbScth /*
18625e3986cbScth  * Release ndi_devi_enter and wait for possibility of new children, avoiding
18635e3986cbScth  * possibility of missing broadcast before getting to cv_timedwait().
18645e3986cbScth  */
18655e3986cbScth static void
18665e3986cbScth ndi_devi_exit_and_wait(dev_info_t *dip, int circular, clock_t end_time)
18675e3986cbScth {
18685e3986cbScth 	struct dev_info	*devi = DEVI(dip);
18695e3986cbScth 	ASSERT(dip != NULL);
18705e3986cbScth 
18715e3986cbScth 	if (panicstr)
18725e3986cbScth 		return;
18735e3986cbScth 
18745e3986cbScth 	/*
18755e3986cbScth 	 * We are called to wait for of a new child, and new child can
18765e3986cbScth 	 * only be added if circular is zero.
18775e3986cbScth 	 */
18785e3986cbScth 	ASSERT(circular == 0);
18795e3986cbScth 
18805e3986cbScth 	/* like ndi_devi_exit with circular of zero */
18815e3986cbScth 	mutex_enter(&(devi->devi_lock));
18825e3986cbScth 	devi->devi_flags &= ~DEVI_BUSY;
18835e3986cbScth 	ASSERT(devi->devi_busy_thread == curthread);
18845e3986cbScth 	devi->devi_busy_thread = NULL;
18855e3986cbScth 	cv_broadcast(&(devi->devi_cv));
18865e3986cbScth 
18875e3986cbScth 	/* now wait for new children while still holding devi_lock */
18885e3986cbScth 	(void) cv_timedwait(&devi->devi_cv, &(devi->devi_lock), end_time);
18895e3986cbScth 	mutex_exit(&(devi->devi_lock));
18907c478bd9Sstevel@tonic-gate }
18917c478bd9Sstevel@tonic-gate 
18927c478bd9Sstevel@tonic-gate /*
18937c478bd9Sstevel@tonic-gate  * Attempt to single thread entry into devinfo node for modifying its children.
18947c478bd9Sstevel@tonic-gate  */
18957c478bd9Sstevel@tonic-gate int
18967c478bd9Sstevel@tonic-gate ndi_devi_tryenter(dev_info_t *dip, int *circular)
18977c478bd9Sstevel@tonic-gate {
18987c478bd9Sstevel@tonic-gate 	int rval = 1;		   /* assume we enter */
18997c478bd9Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
19007c478bd9Sstevel@tonic-gate 	ASSERT(dip != NULL);
19017c478bd9Sstevel@tonic-gate 
19027c478bd9Sstevel@tonic-gate 	mutex_enter(&devi->devi_lock);
19037c478bd9Sstevel@tonic-gate 	if (devi->devi_busy_thread == (void *)curthread) {
19047c478bd9Sstevel@tonic-gate 		devi->devi_circular++;
19057c478bd9Sstevel@tonic-gate 	} else {
19067c478bd9Sstevel@tonic-gate 		if (!DEVI_BUSY_CHANGING(devi)) {
19077c478bd9Sstevel@tonic-gate 			devi->devi_flags |= DEVI_BUSY;
19087c478bd9Sstevel@tonic-gate 			devi->devi_busy_thread = (void *)curthread;
19097c478bd9Sstevel@tonic-gate 		} else {
19107c478bd9Sstevel@tonic-gate 			rval = 0;	/* devi is busy */
19117c478bd9Sstevel@tonic-gate 		}
19127c478bd9Sstevel@tonic-gate 	}
19137c478bd9Sstevel@tonic-gate 	*circular = devi->devi_circular;
19147c478bd9Sstevel@tonic-gate 	mutex_exit(&devi->devi_lock);
19157c478bd9Sstevel@tonic-gate 	return (rval);
19167c478bd9Sstevel@tonic-gate }
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate /*
19197c478bd9Sstevel@tonic-gate  * Allocate and initialize a new dev_info structure.
19207c478bd9Sstevel@tonic-gate  *
19217c478bd9Sstevel@tonic-gate  * This routine may be called at interrupt time by a nexus in
19227c478bd9Sstevel@tonic-gate  * response to a hotplug event, therefore memory allocations are
19237c478bd9Sstevel@tonic-gate  * not allowed to sleep.
19247c478bd9Sstevel@tonic-gate  */
19257c478bd9Sstevel@tonic-gate int
1926fa9e4066Sahrens ndi_devi_alloc(dev_info_t *parent, char *node_name, pnode_t nodeid,
19277c478bd9Sstevel@tonic-gate     dev_info_t **ret_dip)
19287c478bd9Sstevel@tonic-gate {
19297c478bd9Sstevel@tonic-gate 	ASSERT(node_name != NULL);
19307c478bd9Sstevel@tonic-gate 	ASSERT(ret_dip != NULL);
19317c478bd9Sstevel@tonic-gate 
19327c478bd9Sstevel@tonic-gate 	*ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL,
19337c478bd9Sstevel@tonic-gate 	    KM_NOSLEEP);
19347c478bd9Sstevel@tonic-gate 	if (*ret_dip == NULL) {
19357c478bd9Sstevel@tonic-gate 		return (NDI_NOMEM);
19367c478bd9Sstevel@tonic-gate 	}
19377c478bd9Sstevel@tonic-gate 
19387c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
19397c478bd9Sstevel@tonic-gate }
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate /*
19427c478bd9Sstevel@tonic-gate  * Allocate and initialize a new dev_info structure
19437c478bd9Sstevel@tonic-gate  * This routine may sleep and should not be called at interrupt time
19447c478bd9Sstevel@tonic-gate  */
19457c478bd9Sstevel@tonic-gate void
1946fa9e4066Sahrens ndi_devi_alloc_sleep(dev_info_t *parent, char *node_name, pnode_t nodeid,
19477c478bd9Sstevel@tonic-gate     dev_info_t **ret_dip)
19487c478bd9Sstevel@tonic-gate {
19497c478bd9Sstevel@tonic-gate 	ASSERT(node_name != NULL);
19507c478bd9Sstevel@tonic-gate 	ASSERT(ret_dip != NULL);
19517c478bd9Sstevel@tonic-gate 
19527c478bd9Sstevel@tonic-gate 	*ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL,
19537c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
19547c478bd9Sstevel@tonic-gate 	ASSERT(*ret_dip);
19557c478bd9Sstevel@tonic-gate }
19567c478bd9Sstevel@tonic-gate 
19577c478bd9Sstevel@tonic-gate /*
19587c478bd9Sstevel@tonic-gate  * Remove an initialized (but not yet attached) dev_info
19597c478bd9Sstevel@tonic-gate  * node from it's parent.
19607c478bd9Sstevel@tonic-gate  */
19617c478bd9Sstevel@tonic-gate int
19627c478bd9Sstevel@tonic-gate ndi_devi_free(dev_info_t *dip)
19637c478bd9Sstevel@tonic-gate {
19647c478bd9Sstevel@tonic-gate 	ASSERT(dip != NULL);
19657c478bd9Sstevel@tonic-gate 
19667c478bd9Sstevel@tonic-gate 	if (i_ddi_node_state(dip) >= DS_INITIALIZED)
19677c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
19687c478bd9Sstevel@tonic-gate 
19697c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_free: %s%d (%p)\n",
19707c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
19717c478bd9Sstevel@tonic-gate 
19727c478bd9Sstevel@tonic-gate 	(void) ddi_remove_child(dip, 0);
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
19757c478bd9Sstevel@tonic-gate }
19767c478bd9Sstevel@tonic-gate 
19777c478bd9Sstevel@tonic-gate /*
19787c478bd9Sstevel@tonic-gate  * ndi_devi_bind_driver() binds a driver to a given device. If it fails
19797c478bd9Sstevel@tonic-gate  * to bind the driver, it returns an appropriate error back. Some drivers
19807c478bd9Sstevel@tonic-gate  * may want to know if the actually failed to bind.
19817c478bd9Sstevel@tonic-gate  */
19827c478bd9Sstevel@tonic-gate int
19837c478bd9Sstevel@tonic-gate ndi_devi_bind_driver(dev_info_t *dip, uint_t flags)
19847c478bd9Sstevel@tonic-gate {
19857c478bd9Sstevel@tonic-gate 	int ret = NDI_FAILURE;
19867c478bd9Sstevel@tonic-gate 	int circ;
19877c478bd9Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
19887c478bd9Sstevel@tonic-gate 	ASSERT(pdip);
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
19917c478bd9Sstevel@tonic-gate 	    "ndi_devi_bind_driver: %s%d (%p) flags: %x\n",
19927c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
19937c478bd9Sstevel@tonic-gate 
19947c478bd9Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
19957c478bd9Sstevel@tonic-gate 	if (i_ndi_config_node(dip, DS_BOUND, flags) == DDI_SUCCESS)
19967c478bd9Sstevel@tonic-gate 		ret = NDI_SUCCESS;
19977c478bd9Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate 	return (ret);
20007c478bd9Sstevel@tonic-gate }
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate /*
20037c478bd9Sstevel@tonic-gate  * ndi_devi_unbind_driver: unbind the dip
20047c478bd9Sstevel@tonic-gate  */
20057c478bd9Sstevel@tonic-gate static int
20067c478bd9Sstevel@tonic-gate ndi_devi_unbind_driver(dev_info_t *dip)
20077c478bd9Sstevel@tonic-gate {
20087c478bd9Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate 	return (i_ndi_unconfig_node(dip, DS_LINKED, 0));
20117c478bd9Sstevel@tonic-gate }
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate /*
20147c478bd9Sstevel@tonic-gate  * Misc. help routines called by framework only
20157c478bd9Sstevel@tonic-gate  */
20167c478bd9Sstevel@tonic-gate 
20177c478bd9Sstevel@tonic-gate /*
20187c478bd9Sstevel@tonic-gate  * Get the state of node
20197c478bd9Sstevel@tonic-gate  */
20207c478bd9Sstevel@tonic-gate ddi_node_state_t
20217c478bd9Sstevel@tonic-gate i_ddi_node_state(dev_info_t *dip)
20227c478bd9Sstevel@tonic-gate {
20237c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_node_state);
20247c478bd9Sstevel@tonic-gate }
20257c478bd9Sstevel@tonic-gate 
20267c478bd9Sstevel@tonic-gate /*
20277c478bd9Sstevel@tonic-gate  * Set the state of node
20287c478bd9Sstevel@tonic-gate  */
20297c478bd9Sstevel@tonic-gate void
20307c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dev_info_t *dip, ddi_node_state_t state)
20317c478bd9Sstevel@tonic-gate {
20327c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_node_state = state;
20337c478bd9Sstevel@tonic-gate 	membar_enter();			/* make sure stores are flushed */
20347c478bd9Sstevel@tonic-gate }
20357c478bd9Sstevel@tonic-gate 
20367c478bd9Sstevel@tonic-gate /*
2037737d277aScth  * Determine if node is attached. The implementation accommodates transient
2038737d277aScth  * DS_READY->DS_ATTACHED->DS_READY state changes.  Outside this file, this
2039737d277aScth  * function should be instead of i_ddi_node_state() DS_ATTACHED/DS_READY
2040737d277aScth  * state checks.
2041737d277aScth  */
2042737d277aScth int
2043737d277aScth i_ddi_devi_attached(dev_info_t *dip)
2044737d277aScth {
2045737d277aScth 	return (DEVI(dip)->devi_node_state >= DS_ATTACHED);
2046737d277aScth }
2047737d277aScth 
2048737d277aScth /*
20497c478bd9Sstevel@tonic-gate  * Common function for finding a node in a sibling list given name and addr.
20507c478bd9Sstevel@tonic-gate  *
20517c478bd9Sstevel@tonic-gate  * By default, name is matched with devi_node_name. The following
20527c478bd9Sstevel@tonic-gate  * alternative match strategies are supported:
20537c478bd9Sstevel@tonic-gate  *
2054f4da9be0Scth  *	FIND_NODE_BY_NODENAME: Match on node name - typical use.
2055f4da9be0Scth  *	FIND_NODE_BY_DRIVER: A match on driver name bound to node is conducted.
20567c478bd9Sstevel@tonic-gate  *		This support is used for support of OBP generic names and
20577c478bd9Sstevel@tonic-gate  *		for the conversion from driver names to generic names. When
20587c478bd9Sstevel@tonic-gate  *		more consistency in the generic name environment is achieved
20597c478bd9Sstevel@tonic-gate  *		(and not needed for upgrade) this support can be removed.
2060f4da9be0Scth  *	FIND_NODE_BY_ADDR: Match on just the addr.
2061f4da9be0Scth  *		This support is only used/needed during boot to match
2062f4da9be0Scth  *		a node bound via a path-based driver alias.
20637c478bd9Sstevel@tonic-gate  *
20647c478bd9Sstevel@tonic-gate  * If a child is not named (dev_addr == NULL), there are three
20657c478bd9Sstevel@tonic-gate  * possible actions:
20667c478bd9Sstevel@tonic-gate  *
20677c478bd9Sstevel@tonic-gate  *	(1) skip it
20687c478bd9Sstevel@tonic-gate  *	(2) FIND_ADDR_BY_INIT: bring child to DS_INITIALIZED state
20697c478bd9Sstevel@tonic-gate  *	(3) FIND_ADDR_BY_CALLBACK: use a caller-supplied callback function
20707c478bd9Sstevel@tonic-gate  */
2071f4da9be0Scth #define	FIND_NODE_BY_NODENAME	0x01
2072f4da9be0Scth #define	FIND_NODE_BY_DRIVER	0x02
2073f4da9be0Scth #define	FIND_NODE_BY_ADDR	0x04
20747c478bd9Sstevel@tonic-gate #define	FIND_ADDR_BY_INIT	0x10
20757c478bd9Sstevel@tonic-gate #define	FIND_ADDR_BY_CALLBACK	0x20
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate static dev_info_t *
20787c478bd9Sstevel@tonic-gate find_sibling(dev_info_t *head, char *cname, char *caddr, uint_t flag,
20797c478bd9Sstevel@tonic-gate     int (*callback)(dev_info_t *, char *, int))
20807c478bd9Sstevel@tonic-gate {
20817c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
20827c478bd9Sstevel@tonic-gate 	char		*addr, *buf;
20837c478bd9Sstevel@tonic-gate 	major_t		major;
2084f4da9be0Scth 	uint_t		by;
2085f4da9be0Scth 
2086f4da9be0Scth 	/* only one way to find a node */
2087f4da9be0Scth 	by = flag &
2088f4da9be0Scth 	    (FIND_NODE_BY_DRIVER | FIND_NODE_BY_NODENAME | FIND_NODE_BY_ADDR);
2089f4da9be0Scth 	ASSERT(by && BIT_ONLYONESET(by));
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate 	/* only one way to name a node */
20927c478bd9Sstevel@tonic-gate 	ASSERT(((flag & FIND_ADDR_BY_INIT) == 0) ||
20937c478bd9Sstevel@tonic-gate 	    ((flag & FIND_ADDR_BY_CALLBACK) == 0));
20947c478bd9Sstevel@tonic-gate 
2095f4da9be0Scth 	if (by == FIND_NODE_BY_DRIVER) {
20967c478bd9Sstevel@tonic-gate 		major = ddi_name_to_major(cname);
2097a204de77Scth 		if (major == DDI_MAJOR_T_NONE)
20987c478bd9Sstevel@tonic-gate 			return (NULL);
20997c478bd9Sstevel@tonic-gate 	}
21007c478bd9Sstevel@tonic-gate 
21017c478bd9Sstevel@tonic-gate 	/* preallocate buffer of naming node by callback */
21027c478bd9Sstevel@tonic-gate 	if (flag & FIND_ADDR_BY_CALLBACK)
21037c478bd9Sstevel@tonic-gate 		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
21047c478bd9Sstevel@tonic-gate 
21057c478bd9Sstevel@tonic-gate 	/*
21067c478bd9Sstevel@tonic-gate 	 * Walk the child list to find a match
21077c478bd9Sstevel@tonic-gate 	 */
21087c478bd9Sstevel@tonic-gate 
21097c478bd9Sstevel@tonic-gate 	for (dip = head; dip; dip = ddi_get_next_sibling(dip)) {
2110f4da9be0Scth 		if (by == FIND_NODE_BY_NODENAME) {
21117c478bd9Sstevel@tonic-gate 			/* match node name */
21127c478bd9Sstevel@tonic-gate 			if (strcmp(cname, DEVI(dip)->devi_node_name) != 0)
21137c478bd9Sstevel@tonic-gate 				continue;
2114f4da9be0Scth 		} else if (by == FIND_NODE_BY_DRIVER) {
2115f4da9be0Scth 			/* match driver major */
2116f4da9be0Scth 			if (DEVI(dip)->devi_major != major)
2117f4da9be0Scth 				continue;
21187c478bd9Sstevel@tonic-gate 		}
21197c478bd9Sstevel@tonic-gate 
21207c478bd9Sstevel@tonic-gate 		if ((addr = DEVI(dip)->devi_addr) == NULL) {
21217c478bd9Sstevel@tonic-gate 			/* name the child based on the flag */
21227c478bd9Sstevel@tonic-gate 			if (flag & FIND_ADDR_BY_INIT) {
21237c478bd9Sstevel@tonic-gate 				if (ddi_initchild(ddi_get_parent(dip), dip)
21247c478bd9Sstevel@tonic-gate 				    != DDI_SUCCESS)
21257c478bd9Sstevel@tonic-gate 					continue;
21267c478bd9Sstevel@tonic-gate 				addr = DEVI(dip)->devi_addr;
21277c478bd9Sstevel@tonic-gate 			} else if (flag & FIND_ADDR_BY_CALLBACK) {
21287c478bd9Sstevel@tonic-gate 				if ((callback == NULL) || (callback(
21297c478bd9Sstevel@tonic-gate 				    dip, buf, MAXNAMELEN) != DDI_SUCCESS))
21307c478bd9Sstevel@tonic-gate 					continue;
21317c478bd9Sstevel@tonic-gate 				addr = buf;
21327c478bd9Sstevel@tonic-gate 			} else {
21337c478bd9Sstevel@tonic-gate 				continue;	/* skip */
21347c478bd9Sstevel@tonic-gate 			}
21357c478bd9Sstevel@tonic-gate 		}
21367c478bd9Sstevel@tonic-gate 
21377c478bd9Sstevel@tonic-gate 		/* match addr */
21387c478bd9Sstevel@tonic-gate 		ASSERT(addr != NULL);
21397c478bd9Sstevel@tonic-gate 		if (strcmp(caddr, addr) == 0)
21407c478bd9Sstevel@tonic-gate 			break;	/* node found */
21417c478bd9Sstevel@tonic-gate 
21427c478bd9Sstevel@tonic-gate 	}
21437c478bd9Sstevel@tonic-gate 	if (flag & FIND_ADDR_BY_CALLBACK)
21447c478bd9Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
21457c478bd9Sstevel@tonic-gate 	return (dip);
21467c478bd9Sstevel@tonic-gate }
21477c478bd9Sstevel@tonic-gate 
21487c478bd9Sstevel@tonic-gate /*
21497c478bd9Sstevel@tonic-gate  * Find child of pdip with name: cname@caddr
21507c478bd9Sstevel@tonic-gate  * Called by init_node() to look for duplicate nodes
21517c478bd9Sstevel@tonic-gate  */
21527c478bd9Sstevel@tonic-gate static dev_info_t *
21537c478bd9Sstevel@tonic-gate find_duplicate_child(dev_info_t *pdip, dev_info_t *dip)
21547c478bd9Sstevel@tonic-gate {
21557c478bd9Sstevel@tonic-gate 	dev_info_t *dup;
21567c478bd9Sstevel@tonic-gate 	char *cname = DEVI(dip)->devi_node_name;
21577c478bd9Sstevel@tonic-gate 	char *caddr = DEVI(dip)->devi_addr;
21587c478bd9Sstevel@tonic-gate 
21597c478bd9Sstevel@tonic-gate 	/* search nodes before dip */
2160f4da9be0Scth 	dup = find_sibling(ddi_get_child(pdip), cname, caddr,
2161f4da9be0Scth 	    FIND_NODE_BY_NODENAME, NULL);
21627c478bd9Sstevel@tonic-gate 	if (dup != dip)
21637c478bd9Sstevel@tonic-gate 		return (dup);
21647c478bd9Sstevel@tonic-gate 
21657c478bd9Sstevel@tonic-gate 	/*
21667c478bd9Sstevel@tonic-gate 	 * search nodes after dip; normally this is not needed,
21677c478bd9Sstevel@tonic-gate 	 */
21687c478bd9Sstevel@tonic-gate 	return (find_sibling(ddi_get_next_sibling(dip), cname, caddr,
2169f4da9be0Scth 	    FIND_NODE_BY_NODENAME, NULL));
21707c478bd9Sstevel@tonic-gate }
21717c478bd9Sstevel@tonic-gate 
21727c478bd9Sstevel@tonic-gate /*
21737c478bd9Sstevel@tonic-gate  * Find a child of a given name and address, using a callback to name
21747c478bd9Sstevel@tonic-gate  * unnamed children. cname is the binding name.
21757c478bd9Sstevel@tonic-gate  */
21767c478bd9Sstevel@tonic-gate static dev_info_t *
21777c478bd9Sstevel@tonic-gate find_child_by_callback(dev_info_t *pdip, char *cname, char *caddr,
21787c478bd9Sstevel@tonic-gate     int (*name_node)(dev_info_t *, char *, int))
21797c478bd9Sstevel@tonic-gate {
21807c478bd9Sstevel@tonic-gate 	return (find_sibling(ddi_get_child(pdip), cname, caddr,
2181f4da9be0Scth 	    FIND_NODE_BY_DRIVER|FIND_ADDR_BY_CALLBACK, name_node));
21827c478bd9Sstevel@tonic-gate }
21837c478bd9Sstevel@tonic-gate 
21847c478bd9Sstevel@tonic-gate /*
21857c478bd9Sstevel@tonic-gate  * Find a child of a given name and address, invoking initchild to name
21867c478bd9Sstevel@tonic-gate  * unnamed children. cname is the node name.
21877c478bd9Sstevel@tonic-gate  */
21887c478bd9Sstevel@tonic-gate static dev_info_t *
21897c478bd9Sstevel@tonic-gate find_child_by_name(dev_info_t *pdip, char *cname, char *caddr)
21907c478bd9Sstevel@tonic-gate {
21917c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
21927c478bd9Sstevel@tonic-gate 
2193f4da9be0Scth 	/* attempt search without changing state of preceding siblings */
2194f4da9be0Scth 	dip = find_sibling(ddi_get_child(pdip), cname, caddr,
2195f4da9be0Scth 	    FIND_NODE_BY_NODENAME, NULL);
21967c478bd9Sstevel@tonic-gate 	if (dip)
21977c478bd9Sstevel@tonic-gate 		return (dip);
21987c478bd9Sstevel@tonic-gate 
21997c478bd9Sstevel@tonic-gate 	return (find_sibling(ddi_get_child(pdip), cname, caddr,
2200f4da9be0Scth 	    FIND_NODE_BY_NODENAME|FIND_ADDR_BY_INIT, NULL));
22017c478bd9Sstevel@tonic-gate }
22027c478bd9Sstevel@tonic-gate 
22037c478bd9Sstevel@tonic-gate /*
22047c478bd9Sstevel@tonic-gate  * Find a child of a given name and address, invoking initchild to name
22057c478bd9Sstevel@tonic-gate  * unnamed children. cname is the node name.
22067c478bd9Sstevel@tonic-gate  */
22077c478bd9Sstevel@tonic-gate static dev_info_t *
22087c478bd9Sstevel@tonic-gate find_child_by_driver(dev_info_t *pdip, char *cname, char *caddr)
22097c478bd9Sstevel@tonic-gate {
22107c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
22117c478bd9Sstevel@tonic-gate 
2212f4da9be0Scth 	/* attempt search without changing state of preceding siblings */
22137c478bd9Sstevel@tonic-gate 	dip = find_sibling(ddi_get_child(pdip), cname, caddr,
2214f4da9be0Scth 	    FIND_NODE_BY_DRIVER, NULL);
22157c478bd9Sstevel@tonic-gate 	if (dip)
22167c478bd9Sstevel@tonic-gate 		return (dip);
22177c478bd9Sstevel@tonic-gate 
22187c478bd9Sstevel@tonic-gate 	return (find_sibling(ddi_get_child(pdip), cname, caddr,
2219f4da9be0Scth 	    FIND_NODE_BY_DRIVER|FIND_ADDR_BY_INIT, NULL));
2220f4da9be0Scth }
2221f4da9be0Scth 
2222f4da9be0Scth /*
2223f4da9be0Scth  * Find a child of a given address, invoking initchild to name
2224f4da9be0Scth  * unnamed children. cname is the node name.
2225f4da9be0Scth  *
2226f4da9be0Scth  * NOTE: This function is only used during boot. One would hope that
2227f4da9be0Scth  * unique sibling unit-addresses on hardware branches of the tree would
2228f4da9be0Scth  * be a requirement to avoid two drivers trying to control the same
2229f4da9be0Scth  * piece of hardware. Unfortunately there are some cases where this
2230f4da9be0Scth  * situation exists (/ssm@0,0/pci@1c,700000 /ssm@0,0/sghsc@1c,700000).
2231f4da9be0Scth  * Until unit-address uniqueness of siblings is guaranteed, use of this
2232f4da9be0Scth  * interface for purposes other than boot should be avoided.
2233f4da9be0Scth  */
2234f4da9be0Scth static dev_info_t *
2235f4da9be0Scth find_child_by_addr(dev_info_t *pdip, char *caddr)
2236f4da9be0Scth {
2237f4da9be0Scth 	dev_info_t	*dip;
2238f4da9be0Scth 
22392486dd1dScth 	/* return NULL if called without a unit-address */
22402486dd1dScth 	if ((caddr == NULL) || (*caddr == '\0'))
22412486dd1dScth 		return (NULL);
22422486dd1dScth 
2243f4da9be0Scth 	/* attempt search without changing state of preceding siblings */
2244f4da9be0Scth 	dip = find_sibling(ddi_get_child(pdip), NULL, caddr,
2245f4da9be0Scth 	    FIND_NODE_BY_ADDR, NULL);
2246f4da9be0Scth 	if (dip)
2247f4da9be0Scth 		return (dip);
2248f4da9be0Scth 
2249f4da9be0Scth 	return (find_sibling(ddi_get_child(pdip), NULL, caddr,
2250f4da9be0Scth 	    FIND_NODE_BY_ADDR|FIND_ADDR_BY_INIT, NULL));
22517c478bd9Sstevel@tonic-gate }
22527c478bd9Sstevel@tonic-gate 
22537c478bd9Sstevel@tonic-gate /*
22547c478bd9Sstevel@tonic-gate  * Deleting a property list. Take care, since some property structures
22557c478bd9Sstevel@tonic-gate  * may not be fully built.
22567c478bd9Sstevel@tonic-gate  */
22577c478bd9Sstevel@tonic-gate void
22587c478bd9Sstevel@tonic-gate i_ddi_prop_list_delete(ddi_prop_t *prop)
22597c478bd9Sstevel@tonic-gate {
22607c478bd9Sstevel@tonic-gate 	while (prop) {
22617c478bd9Sstevel@tonic-gate 		ddi_prop_t *next = prop->prop_next;
22627c478bd9Sstevel@tonic-gate 		if (prop->prop_name)
22637c478bd9Sstevel@tonic-gate 			kmem_free(prop->prop_name, strlen(prop->prop_name) + 1);
22647c478bd9Sstevel@tonic-gate 		if ((prop->prop_len != 0) && prop->prop_val)
22657c478bd9Sstevel@tonic-gate 			kmem_free(prop->prop_val, prop->prop_len);
22667c478bd9Sstevel@tonic-gate 		kmem_free(prop, sizeof (struct ddi_prop));
22677c478bd9Sstevel@tonic-gate 		prop = next;
22687c478bd9Sstevel@tonic-gate 	}
22697c478bd9Sstevel@tonic-gate }
22707c478bd9Sstevel@tonic-gate 
22717c478bd9Sstevel@tonic-gate /*
22727c478bd9Sstevel@tonic-gate  * Duplicate property list
22737c478bd9Sstevel@tonic-gate  */
22747c478bd9Sstevel@tonic-gate ddi_prop_t *
22757c478bd9Sstevel@tonic-gate i_ddi_prop_list_dup(ddi_prop_t *prop, uint_t flag)
22767c478bd9Sstevel@tonic-gate {
22777c478bd9Sstevel@tonic-gate 	ddi_prop_t *result, *prev, *copy;
22787c478bd9Sstevel@tonic-gate 
22797c478bd9Sstevel@tonic-gate 	if (prop == NULL)
22807c478bd9Sstevel@tonic-gate 		return (NULL);
22817c478bd9Sstevel@tonic-gate 
22827c478bd9Sstevel@tonic-gate 	result = prev = NULL;
22837c478bd9Sstevel@tonic-gate 	for (; prop != NULL; prop = prop->prop_next) {
22847c478bd9Sstevel@tonic-gate 		ASSERT(prop->prop_name != NULL);
22857c478bd9Sstevel@tonic-gate 		copy = kmem_zalloc(sizeof (struct ddi_prop), flag);
22867c478bd9Sstevel@tonic-gate 		if (copy == NULL)
22877c478bd9Sstevel@tonic-gate 			goto fail;
22887c478bd9Sstevel@tonic-gate 
22897c478bd9Sstevel@tonic-gate 		copy->prop_dev = prop->prop_dev;
22907c478bd9Sstevel@tonic-gate 		copy->prop_flags = prop->prop_flags;
22917c478bd9Sstevel@tonic-gate 		copy->prop_name = i_ddi_strdup(prop->prop_name, flag);
22927c478bd9Sstevel@tonic-gate 		if (copy->prop_name == NULL)
22937c478bd9Sstevel@tonic-gate 			goto fail;
22947c478bd9Sstevel@tonic-gate 
22957c478bd9Sstevel@tonic-gate 		if ((copy->prop_len = prop->prop_len) != 0) {
22967c478bd9Sstevel@tonic-gate 			copy->prop_val = kmem_zalloc(prop->prop_len, flag);
22977c478bd9Sstevel@tonic-gate 			if (copy->prop_val == NULL)
22987c478bd9Sstevel@tonic-gate 				goto fail;
22997c478bd9Sstevel@tonic-gate 
23007c478bd9Sstevel@tonic-gate 			bcopy(prop->prop_val, copy->prop_val, prop->prop_len);
23017c478bd9Sstevel@tonic-gate 		}
23027c478bd9Sstevel@tonic-gate 
23037c478bd9Sstevel@tonic-gate 		if (prev == NULL)
23047c478bd9Sstevel@tonic-gate 			result = prev = copy;
23057c478bd9Sstevel@tonic-gate 		else
23067c478bd9Sstevel@tonic-gate 			prev->prop_next = copy;
23077c478bd9Sstevel@tonic-gate 		prev = copy;
23087c478bd9Sstevel@tonic-gate 	}
23097c478bd9Sstevel@tonic-gate 	return (result);
23107c478bd9Sstevel@tonic-gate 
23117c478bd9Sstevel@tonic-gate fail:
23127c478bd9Sstevel@tonic-gate 	i_ddi_prop_list_delete(result);
23137c478bd9Sstevel@tonic-gate 	return (NULL);
23147c478bd9Sstevel@tonic-gate }
23157c478bd9Sstevel@tonic-gate 
23167c478bd9Sstevel@tonic-gate /*
23177c478bd9Sstevel@tonic-gate  * Create a reference property list, currently used only for
23187c478bd9Sstevel@tonic-gate  * driver global properties. Created with ref count of 1.
23197c478bd9Sstevel@tonic-gate  */
23207c478bd9Sstevel@tonic-gate ddi_prop_list_t *
23217c478bd9Sstevel@tonic-gate i_ddi_prop_list_create(ddi_prop_t *props)
23227c478bd9Sstevel@tonic-gate {
23237c478bd9Sstevel@tonic-gate 	ddi_prop_list_t *list = kmem_alloc(sizeof (*list), KM_SLEEP);
23247c478bd9Sstevel@tonic-gate 	list->prop_list = props;
23257c478bd9Sstevel@tonic-gate 	list->prop_ref = 1;
23267c478bd9Sstevel@tonic-gate 	return (list);
23277c478bd9Sstevel@tonic-gate }
23287c478bd9Sstevel@tonic-gate 
23297c478bd9Sstevel@tonic-gate /*
23307c478bd9Sstevel@tonic-gate  * Increment/decrement reference count. The reference is
23317c478bd9Sstevel@tonic-gate  * protected by dn_lock. The only interfaces modifying
23327c478bd9Sstevel@tonic-gate  * dn_global_prop_ptr is in impl_make[free]_parlist().
23337c478bd9Sstevel@tonic-gate  */
23347c478bd9Sstevel@tonic-gate void
23357c478bd9Sstevel@tonic-gate i_ddi_prop_list_hold(ddi_prop_list_t *prop_list, struct devnames *dnp)
23367c478bd9Sstevel@tonic-gate {
23377c478bd9Sstevel@tonic-gate 	ASSERT(prop_list->prop_ref >= 0);
23387c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&dnp->dn_lock));
23397c478bd9Sstevel@tonic-gate 	prop_list->prop_ref++;
23407c478bd9Sstevel@tonic-gate }
23417c478bd9Sstevel@tonic-gate 
23427c478bd9Sstevel@tonic-gate void
23437c478bd9Sstevel@tonic-gate i_ddi_prop_list_rele(ddi_prop_list_t *prop_list, struct devnames *dnp)
23447c478bd9Sstevel@tonic-gate {
23457c478bd9Sstevel@tonic-gate 	ASSERT(prop_list->prop_ref > 0);
23467c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&dnp->dn_lock));
23477c478bd9Sstevel@tonic-gate 	prop_list->prop_ref--;
23487c478bd9Sstevel@tonic-gate 
23497c478bd9Sstevel@tonic-gate 	if (prop_list->prop_ref == 0) {
23507c478bd9Sstevel@tonic-gate 		i_ddi_prop_list_delete(prop_list->prop_list);
23517c478bd9Sstevel@tonic-gate 		kmem_free(prop_list, sizeof (*prop_list));
23527c478bd9Sstevel@tonic-gate 	}
23537c478bd9Sstevel@tonic-gate }
23547c478bd9Sstevel@tonic-gate 
23557c478bd9Sstevel@tonic-gate /*
23567c478bd9Sstevel@tonic-gate  * Free table of classes by drivers
23577c478bd9Sstevel@tonic-gate  */
23587c478bd9Sstevel@tonic-gate void
23597c478bd9Sstevel@tonic-gate i_ddi_free_exported_classes(char **classes, int n)
23607c478bd9Sstevel@tonic-gate {
23617c478bd9Sstevel@tonic-gate 	if ((n == 0) || (classes == NULL))
23627c478bd9Sstevel@tonic-gate 		return;
23637c478bd9Sstevel@tonic-gate 
23647c478bd9Sstevel@tonic-gate 	kmem_free(classes, n * sizeof (char *));
23657c478bd9Sstevel@tonic-gate }
23667c478bd9Sstevel@tonic-gate 
23677c478bd9Sstevel@tonic-gate /*
23687c478bd9Sstevel@tonic-gate  * Get all classes exported by dip
23697c478bd9Sstevel@tonic-gate  */
23707c478bd9Sstevel@tonic-gate int
23717c478bd9Sstevel@tonic-gate i_ddi_get_exported_classes(dev_info_t *dip, char ***classes)
23727c478bd9Sstevel@tonic-gate {
23737c478bd9Sstevel@tonic-gate 	extern void lock_hw_class_list();
23747c478bd9Sstevel@tonic-gate 	extern void unlock_hw_class_list();
23757c478bd9Sstevel@tonic-gate 	extern int get_class(const char *, char **);
23767c478bd9Sstevel@tonic-gate 
23777c478bd9Sstevel@tonic-gate 	static char *rootclass = "root";
23787c478bd9Sstevel@tonic-gate 	int n = 0, nclass = 0;
23797c478bd9Sstevel@tonic-gate 	char **buf;
23807c478bd9Sstevel@tonic-gate 
23817c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) >= DS_BOUND);
23827c478bd9Sstevel@tonic-gate 
23837c478bd9Sstevel@tonic-gate 	if (dip == ddi_root_node())	/* rootnode exports class "root" */
23847c478bd9Sstevel@tonic-gate 		nclass = 1;
23857c478bd9Sstevel@tonic-gate 	lock_hw_class_list();
23867c478bd9Sstevel@tonic-gate 	nclass += get_class(ddi_driver_name(dip), NULL);
23877c478bd9Sstevel@tonic-gate 	if (nclass == 0) {
23887c478bd9Sstevel@tonic-gate 		unlock_hw_class_list();
23897c478bd9Sstevel@tonic-gate 		return (0);		/* no class exported */
23907c478bd9Sstevel@tonic-gate 	}
23917c478bd9Sstevel@tonic-gate 
23927c478bd9Sstevel@tonic-gate 	*classes = buf = kmem_alloc(nclass * sizeof (char *), KM_SLEEP);
23937c478bd9Sstevel@tonic-gate 	if (dip == ddi_root_node()) {
23947c478bd9Sstevel@tonic-gate 		*buf++ = rootclass;
23957c478bd9Sstevel@tonic-gate 		n = 1;
23967c478bd9Sstevel@tonic-gate 	}
23977c478bd9Sstevel@tonic-gate 	n += get_class(ddi_driver_name(dip), buf);
23987c478bd9Sstevel@tonic-gate 	unlock_hw_class_list();
23997c478bd9Sstevel@tonic-gate 
24007c478bd9Sstevel@tonic-gate 	ASSERT(n == nclass);    /* make sure buf wasn't overrun */
24017c478bd9Sstevel@tonic-gate 	return (nclass);
24027c478bd9Sstevel@tonic-gate }
24037c478bd9Sstevel@tonic-gate 
24047c478bd9Sstevel@tonic-gate /*
24057c478bd9Sstevel@tonic-gate  * Helper functions, returns NULL if no memory.
24067c478bd9Sstevel@tonic-gate  */
24077c478bd9Sstevel@tonic-gate char *
24087c478bd9Sstevel@tonic-gate i_ddi_strdup(char *str, uint_t flag)
24097c478bd9Sstevel@tonic-gate {
24107c478bd9Sstevel@tonic-gate 	char *copy;
24117c478bd9Sstevel@tonic-gate 
24127c478bd9Sstevel@tonic-gate 	if (str == NULL)
24137c478bd9Sstevel@tonic-gate 		return (NULL);
24147c478bd9Sstevel@tonic-gate 
24157c478bd9Sstevel@tonic-gate 	copy = kmem_alloc(strlen(str) + 1, flag);
24167c478bd9Sstevel@tonic-gate 	if (copy == NULL)
24177c478bd9Sstevel@tonic-gate 		return (NULL);
24187c478bd9Sstevel@tonic-gate 
24197c478bd9Sstevel@tonic-gate 	(void) strcpy(copy, str);
24207c478bd9Sstevel@tonic-gate 	return (copy);
24217c478bd9Sstevel@tonic-gate }
24227c478bd9Sstevel@tonic-gate 
24237c478bd9Sstevel@tonic-gate /*
24247c478bd9Sstevel@tonic-gate  * Load driver.conf file for major. Load all if major == -1.
24257c478bd9Sstevel@tonic-gate  *
24267c478bd9Sstevel@tonic-gate  * This is called
24277c478bd9Sstevel@tonic-gate  * - early in boot after devnames array is initialized
24287c478bd9Sstevel@tonic-gate  * - from vfs code when certain file systems are mounted
24297c478bd9Sstevel@tonic-gate  * - from add_drv when a new driver is added
24307c478bd9Sstevel@tonic-gate  */
24317c478bd9Sstevel@tonic-gate int
24327c478bd9Sstevel@tonic-gate i_ddi_load_drvconf(major_t major)
24337c478bd9Sstevel@tonic-gate {
24347c478bd9Sstevel@tonic-gate 	extern int modrootloaded;
24357c478bd9Sstevel@tonic-gate 
24367c478bd9Sstevel@tonic-gate 	major_t low, high, m;
24377c478bd9Sstevel@tonic-gate 
2438a204de77Scth 	if (major == DDI_MAJOR_T_NONE) {
24397c478bd9Sstevel@tonic-gate 		low = 0;
24407c478bd9Sstevel@tonic-gate 		high = devcnt - 1;
24417c478bd9Sstevel@tonic-gate 	} else {
24427c478bd9Sstevel@tonic-gate 		if (major >= devcnt)
24437c478bd9Sstevel@tonic-gate 			return (EINVAL);
24447c478bd9Sstevel@tonic-gate 		low = high = major;
24457c478bd9Sstevel@tonic-gate 	}
24467c478bd9Sstevel@tonic-gate 
24477c478bd9Sstevel@tonic-gate 	for (m = low; m <= high; m++) {
24487c478bd9Sstevel@tonic-gate 		struct devnames *dnp = &devnamesp[m];
24497c478bd9Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
24507c478bd9Sstevel@tonic-gate 		dnp->dn_flags &= ~DN_DRIVER_HELD;
24517c478bd9Sstevel@tonic-gate 		(void) impl_make_parlist(m);
24527c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
24537c478bd9Sstevel@tonic-gate 	}
24547c478bd9Sstevel@tonic-gate 
24557c478bd9Sstevel@tonic-gate 	if (modrootloaded) {
24567c478bd9Sstevel@tonic-gate 		ddi_walk_devs(ddi_root_node(), reset_nexus_flags,
24577c478bd9Sstevel@tonic-gate 		    (void *)(uintptr_t)major);
24587c478bd9Sstevel@tonic-gate 	}
24597c478bd9Sstevel@tonic-gate 
24607c478bd9Sstevel@tonic-gate 	/* build dn_list from old entries in path_to_inst */
24617c478bd9Sstevel@tonic-gate 	e_ddi_unorphan_instance_nos();
24627c478bd9Sstevel@tonic-gate 	return (0);
24637c478bd9Sstevel@tonic-gate }
24647c478bd9Sstevel@tonic-gate 
24657c478bd9Sstevel@tonic-gate /*
24667c478bd9Sstevel@tonic-gate  * Unload a specific driver.conf.
24677c478bd9Sstevel@tonic-gate  * Don't support unload all because it doesn't make any sense
24687c478bd9Sstevel@tonic-gate  */
24697c478bd9Sstevel@tonic-gate int
24707c478bd9Sstevel@tonic-gate i_ddi_unload_drvconf(major_t major)
24717c478bd9Sstevel@tonic-gate {
24727c478bd9Sstevel@tonic-gate 	int error;
24737c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
24747c478bd9Sstevel@tonic-gate 
24757c478bd9Sstevel@tonic-gate 	if (major >= devcnt)
24767c478bd9Sstevel@tonic-gate 		return (EINVAL);
24777c478bd9Sstevel@tonic-gate 
24787c478bd9Sstevel@tonic-gate 	/*
24797c478bd9Sstevel@tonic-gate 	 * Take the per-driver lock while unloading driver.conf
24807c478bd9Sstevel@tonic-gate 	 */
24817c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
24827c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
24837c478bd9Sstevel@tonic-gate 	error = impl_free_parlist(major);
24847c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
24857c478bd9Sstevel@tonic-gate 	return (error);
24867c478bd9Sstevel@tonic-gate }
24877c478bd9Sstevel@tonic-gate 
24887c478bd9Sstevel@tonic-gate /*
24897c478bd9Sstevel@tonic-gate  * Merge a .conf node. This is called by nexus drivers to augment
24907c478bd9Sstevel@tonic-gate  * hw node with properties specified in driver.conf file. This function
24917c478bd9Sstevel@tonic-gate  * takes a callback routine to name nexus children.
24927c478bd9Sstevel@tonic-gate  * The parent node must be held busy.
24937c478bd9Sstevel@tonic-gate  *
24947c478bd9Sstevel@tonic-gate  * It returns DDI_SUCCESS if the node is merged and DDI_FAILURE otherwise.
24957c478bd9Sstevel@tonic-gate  */
24967c478bd9Sstevel@tonic-gate int
24977c478bd9Sstevel@tonic-gate ndi_merge_node(dev_info_t *dip, int (*name_node)(dev_info_t *, char *, int))
24987c478bd9Sstevel@tonic-gate {
24997c478bd9Sstevel@tonic-gate 	dev_info_t *hwdip;
25007c478bd9Sstevel@tonic-gate 
25017c478bd9Sstevel@tonic-gate 	ASSERT(ndi_dev_is_persistent_node(dip) == 0);
25027c478bd9Sstevel@tonic-gate 	ASSERT(ddi_get_name_addr(dip) != NULL);
25037c478bd9Sstevel@tonic-gate 
25047c478bd9Sstevel@tonic-gate 	hwdip = find_child_by_callback(ddi_get_parent(dip),
25057c478bd9Sstevel@tonic-gate 	    ddi_binding_name(dip), ddi_get_name_addr(dip), name_node);
25067c478bd9Sstevel@tonic-gate 
25077c478bd9Sstevel@tonic-gate 	/*
25087c478bd9Sstevel@tonic-gate 	 * Look for the hardware node that is the target of the merge;
25097c478bd9Sstevel@tonic-gate 	 * return failure if not found.
25107c478bd9Sstevel@tonic-gate 	 */
25117c478bd9Sstevel@tonic-gate 	if ((hwdip == NULL) || (hwdip == dip)) {
25127c478bd9Sstevel@tonic-gate 		char *buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
25137c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_WARN, "No HW node to merge conf node %s",
25147c478bd9Sstevel@tonic-gate 		    ddi_deviname(dip, buf)));
25157c478bd9Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
25167c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
25177c478bd9Sstevel@tonic-gate 	}
25187c478bd9Sstevel@tonic-gate 
25197c478bd9Sstevel@tonic-gate 	/*
25207c478bd9Sstevel@tonic-gate 	 * Make sure the hardware node is uninitialized and has no property.
25217c478bd9Sstevel@tonic-gate 	 * This may not be the case if new .conf files are load after some
25227c478bd9Sstevel@tonic-gate 	 * hardware nodes have already been initialized and attached.
25237c478bd9Sstevel@tonic-gate 	 *
25247c478bd9Sstevel@tonic-gate 	 * N.B. We return success here because the node was *intended*
25257c478bd9Sstevel@tonic-gate 	 * 	to be a merge node because there is a hw node with the name.
25267c478bd9Sstevel@tonic-gate 	 */
25277c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(hwdip)->devi_lock);
25287c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(hwdip) == 0) {
25297c478bd9Sstevel@tonic-gate 		char *buf;
25307c478bd9Sstevel@tonic-gate 		mutex_exit(&DEVI(hwdip)->devi_lock);
25317c478bd9Sstevel@tonic-gate 
25327c478bd9Sstevel@tonic-gate 		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
25337c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE, "Duplicate .conf node %s",
25347c478bd9Sstevel@tonic-gate 		    ddi_deviname(dip, buf)));
25357c478bd9Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
25367c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
25377c478bd9Sstevel@tonic-gate 	}
25387c478bd9Sstevel@tonic-gate 
25397c478bd9Sstevel@tonic-gate 	/*
25407c478bd9Sstevel@tonic-gate 	 * If it is possible that the hardware has already been touched
25417c478bd9Sstevel@tonic-gate 	 * then don't merge.
25427c478bd9Sstevel@tonic-gate 	 */
25437c478bd9Sstevel@tonic-gate 	if (i_ddi_node_state(hwdip) >= DS_INITIALIZED ||
25447c478bd9Sstevel@tonic-gate 	    (DEVI(hwdip)->devi_sys_prop_ptr != NULL) ||
25457c478bd9Sstevel@tonic-gate 	    (DEVI(hwdip)->devi_drv_prop_ptr != NULL)) {
25467c478bd9Sstevel@tonic-gate 		char *buf;
25477c478bd9Sstevel@tonic-gate 		mutex_exit(&DEVI(hwdip)->devi_lock);
25487c478bd9Sstevel@tonic-gate 
25497c478bd9Sstevel@tonic-gate 		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
25507c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE,
25517c478bd9Sstevel@tonic-gate 		    "!Cannot merge .conf node %s with hw node %p "
25527c478bd9Sstevel@tonic-gate 		    "-- not in proper state",
25537c478bd9Sstevel@tonic-gate 		    ddi_deviname(dip, buf), (void *)hwdip));
25547c478bd9Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
25557c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
25567c478bd9Sstevel@tonic-gate 	}
25577c478bd9Sstevel@tonic-gate 
25587c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
25597c478bd9Sstevel@tonic-gate 	DEVI(hwdip)->devi_sys_prop_ptr = DEVI(dip)->devi_sys_prop_ptr;
25607c478bd9Sstevel@tonic-gate 	DEVI(hwdip)->devi_drv_prop_ptr = DEVI(dip)->devi_drv_prop_ptr;
25617c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_sys_prop_ptr = NULL;
25627c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_drv_prop_ptr = NULL;
25637c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
25647c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(hwdip)->devi_lock);
25657c478bd9Sstevel@tonic-gate 
25667c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
25677c478bd9Sstevel@tonic-gate }
25687c478bd9Sstevel@tonic-gate 
25697c478bd9Sstevel@tonic-gate /*
25707c478bd9Sstevel@tonic-gate  * Merge a "wildcard" .conf node. This is called by nexus drivers to
25717c478bd9Sstevel@tonic-gate  * augment a set of hw node with properties specified in driver.conf file.
25727c478bd9Sstevel@tonic-gate  * The parent node must be held busy.
25737c478bd9Sstevel@tonic-gate  *
25747c478bd9Sstevel@tonic-gate  * There is no failure mode, since the nexus may or may not have child
25757c478bd9Sstevel@tonic-gate  * node bound the driver specified by the wildcard node.
25767c478bd9Sstevel@tonic-gate  */
25777c478bd9Sstevel@tonic-gate void
25787c478bd9Sstevel@tonic-gate ndi_merge_wildcard_node(dev_info_t *dip)
25797c478bd9Sstevel@tonic-gate {
25807c478bd9Sstevel@tonic-gate 	dev_info_t *hwdip;
25817c478bd9Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
25827c478bd9Sstevel@tonic-gate 	major_t major = ddi_driver_major(dip);
25837c478bd9Sstevel@tonic-gate 
25847c478bd9Sstevel@tonic-gate 	/* never attempt to merge a hw node */
25857c478bd9Sstevel@tonic-gate 	ASSERT(ndi_dev_is_persistent_node(dip) == 0);
25867c478bd9Sstevel@tonic-gate 	/* must be bound to a driver major number */
2587a204de77Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
25887c478bd9Sstevel@tonic-gate 
25897c478bd9Sstevel@tonic-gate 	/*
25907c478bd9Sstevel@tonic-gate 	 * Walk the child list to find all nodes bound to major
25917c478bd9Sstevel@tonic-gate 	 * and copy properties.
25927c478bd9Sstevel@tonic-gate 	 */
25937c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
2594b9ccdc5aScth 	ASSERT(DEVI_BUSY_OWNED(pdip));
25957c478bd9Sstevel@tonic-gate 	for (hwdip = ddi_get_child(pdip); hwdip;
25967c478bd9Sstevel@tonic-gate 	    hwdip = ddi_get_next_sibling(hwdip)) {
25977c478bd9Sstevel@tonic-gate 		/*
25987c478bd9Sstevel@tonic-gate 		 * Skip nodes not bound to same driver
25997c478bd9Sstevel@tonic-gate 		 */
26007c478bd9Sstevel@tonic-gate 		if (ddi_driver_major(hwdip) != major)
26017c478bd9Sstevel@tonic-gate 			continue;
26027c478bd9Sstevel@tonic-gate 
26037c478bd9Sstevel@tonic-gate 		/*
26047c478bd9Sstevel@tonic-gate 		 * Skip .conf nodes
26057c478bd9Sstevel@tonic-gate 		 */
26067c478bd9Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(hwdip) == 0)
26077c478bd9Sstevel@tonic-gate 			continue;
26087c478bd9Sstevel@tonic-gate 
26097c478bd9Sstevel@tonic-gate 		/*
26107c478bd9Sstevel@tonic-gate 		 * Make sure the node is uninitialized and has no property.
26117c478bd9Sstevel@tonic-gate 		 */
26127c478bd9Sstevel@tonic-gate 		mutex_enter(&DEVI(hwdip)->devi_lock);
26137c478bd9Sstevel@tonic-gate 		if (i_ddi_node_state(hwdip) >= DS_INITIALIZED ||
26147c478bd9Sstevel@tonic-gate 		    (DEVI(hwdip)->devi_sys_prop_ptr != NULL) ||
26157c478bd9Sstevel@tonic-gate 		    (DEVI(hwdip)->devi_drv_prop_ptr != NULL)) {
26167c478bd9Sstevel@tonic-gate 			mutex_exit(&DEVI(hwdip)->devi_lock);
26177c478bd9Sstevel@tonic-gate 			NDI_CONFIG_DEBUG((CE_NOTE, "HW node %p state not "
26187c478bd9Sstevel@tonic-gate 			    "suitable for merging wildcard conf node %s",
26197c478bd9Sstevel@tonic-gate 			    (void *)hwdip, ddi_node_name(dip)));
26207c478bd9Sstevel@tonic-gate 			continue;
26217c478bd9Sstevel@tonic-gate 		}
26227c478bd9Sstevel@tonic-gate 
26237c478bd9Sstevel@tonic-gate 		DEVI(hwdip)->devi_sys_prop_ptr =
26247c478bd9Sstevel@tonic-gate 		    i_ddi_prop_list_dup(DEVI(dip)->devi_sys_prop_ptr, KM_SLEEP);
26257c478bd9Sstevel@tonic-gate 		DEVI(hwdip)->devi_drv_prop_ptr =
26267c478bd9Sstevel@tonic-gate 		    i_ddi_prop_list_dup(DEVI(dip)->devi_drv_prop_ptr, KM_SLEEP);
26277c478bd9Sstevel@tonic-gate 		mutex_exit(&DEVI(hwdip)->devi_lock);
26287c478bd9Sstevel@tonic-gate 	}
26297c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
26307c478bd9Sstevel@tonic-gate }
26317c478bd9Sstevel@tonic-gate 
26327c478bd9Sstevel@tonic-gate /*
26337c478bd9Sstevel@tonic-gate  * Return the major number based on the compatible property. This interface
26347c478bd9Sstevel@tonic-gate  * may be used in situations where we are trying to detect if a better driver
26357c478bd9Sstevel@tonic-gate  * now exists for a device, so it must use the 'compatible' property.  If
26367c478bd9Sstevel@tonic-gate  * a non-NULL formp is specified and the binding was based on compatible then
26377c478bd9Sstevel@tonic-gate  * return the pointer to the form used in *formp.
26387c478bd9Sstevel@tonic-gate  */
26397c478bd9Sstevel@tonic-gate major_t
26407c478bd9Sstevel@tonic-gate ddi_compatible_driver_major(dev_info_t *dip, char **formp)
26417c478bd9Sstevel@tonic-gate {
26427c478bd9Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
26437c478bd9Sstevel@tonic-gate 	void		*compat;
26447c478bd9Sstevel@tonic-gate 	size_t		len;
26457c478bd9Sstevel@tonic-gate 	char		*p = NULL;
2646a204de77Scth 	major_t		major = DDI_MAJOR_T_NONE;
26477c478bd9Sstevel@tonic-gate 
26487c478bd9Sstevel@tonic-gate 	if (formp)
26497c478bd9Sstevel@tonic-gate 		*formp = NULL;
26507c478bd9Sstevel@tonic-gate 
2651f4da9be0Scth 	/*
2652f4da9be0Scth 	 * Highest precedence binding is a path-oriented alias. Since this
2653f4da9be0Scth 	 * requires a 'path', this type of binding occurs via more obtuse
2654f4da9be0Scth 	 * 'rebind'. The need for a path-oriented alias 'rebind' is detected
2655f4da9be0Scth 	 * after a successful DDI_CTLOPS_INITCHILD to another driver: this is
2656f4da9be0Scth 	 * is the first point at which the unit-address (or instance) of the
2657f4da9be0Scth 	 * last component of the path is available (even though the path is
2658f4da9be0Scth 	 * bound to the wrong driver at this point).
2659f4da9be0Scth 	 */
2660f4da9be0Scth 	if (devi->devi_flags & DEVI_REBIND) {
2661f4da9be0Scth 		p = devi->devi_rebinding_name;
2662f4da9be0Scth 		major = ddi_name_to_major(p);
2663a204de77Scth 		if ((major != DDI_MAJOR_T_NONE) &&
2664f4da9be0Scth 		    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED)) {
2665f4da9be0Scth 			if (formp)
2666f4da9be0Scth 				*formp = p;
2667f4da9be0Scth 			return (major);
2668f4da9be0Scth 		}
2669f4da9be0Scth 
2670f4da9be0Scth 		/*
2671f4da9be0Scth 		 * If for some reason devi_rebinding_name no longer resolves
2672f4da9be0Scth 		 * to a proper driver then clear DEVI_REBIND.
2673f4da9be0Scth 		 */
2674f4da9be0Scth 		mutex_enter(&devi->devi_lock);
2675f4da9be0Scth 		devi->devi_flags &= ~DEVI_REBIND;
2676f4da9be0Scth 		mutex_exit(&devi->devi_lock);
2677f4da9be0Scth 	}
2678f4da9be0Scth 
26797c478bd9Sstevel@tonic-gate 	/* look up compatible property */
26807c478bd9Sstevel@tonic-gate 	(void) lookup_compatible(dip, KM_SLEEP);
26817c478bd9Sstevel@tonic-gate 	compat = (void *)(devi->devi_compat_names);
26827c478bd9Sstevel@tonic-gate 	len = devi->devi_compat_length;
26837c478bd9Sstevel@tonic-gate 
26847c478bd9Sstevel@tonic-gate 	/* find the highest precedence compatible form with a driver binding */
26857c478bd9Sstevel@tonic-gate 	while ((p = prom_decode_composite_string(compat, len, p)) != NULL) {
26867c478bd9Sstevel@tonic-gate 		major = ddi_name_to_major(p);
2687a204de77Scth 		if ((major != DDI_MAJOR_T_NONE) &&
26887c478bd9Sstevel@tonic-gate 		    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED)) {
26897c478bd9Sstevel@tonic-gate 			if (formp)
26907c478bd9Sstevel@tonic-gate 				*formp = p;
26917c478bd9Sstevel@tonic-gate 			return (major);
26927c478bd9Sstevel@tonic-gate 		}
26937c478bd9Sstevel@tonic-gate 	}
26947c478bd9Sstevel@tonic-gate 
26957c478bd9Sstevel@tonic-gate 	/*
26967c478bd9Sstevel@tonic-gate 	 * none of the compatible forms have a driver binding, see if
26977c478bd9Sstevel@tonic-gate 	 * the node name has a driver binding.
26987c478bd9Sstevel@tonic-gate 	 */
26997c478bd9Sstevel@tonic-gate 	major = ddi_name_to_major(ddi_node_name(dip));
2700a204de77Scth 	if ((major != DDI_MAJOR_T_NONE) &&
27017c478bd9Sstevel@tonic-gate 	    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED))
27027c478bd9Sstevel@tonic-gate 		return (major);
27037c478bd9Sstevel@tonic-gate 
27047c478bd9Sstevel@tonic-gate 	/* no driver */
2705a204de77Scth 	return (DDI_MAJOR_T_NONE);
27067c478bd9Sstevel@tonic-gate }
27077c478bd9Sstevel@tonic-gate 
27087c478bd9Sstevel@tonic-gate /*
27097c478bd9Sstevel@tonic-gate  * Static help functions
27107c478bd9Sstevel@tonic-gate  */
27117c478bd9Sstevel@tonic-gate 
27127c478bd9Sstevel@tonic-gate /*
27137c478bd9Sstevel@tonic-gate  * lookup the "compatible" property and cache it's contents in the
27147c478bd9Sstevel@tonic-gate  * device node.
27157c478bd9Sstevel@tonic-gate  */
27167c478bd9Sstevel@tonic-gate static int
27177c478bd9Sstevel@tonic-gate lookup_compatible(dev_info_t *dip, uint_t flag)
27187c478bd9Sstevel@tonic-gate {
27197c478bd9Sstevel@tonic-gate 	int rv;
27207c478bd9Sstevel@tonic-gate 	int prop_flags;
27217c478bd9Sstevel@tonic-gate 	uint_t ncompatstrs;
27227c478bd9Sstevel@tonic-gate 	char **compatstrpp;
27237c478bd9Sstevel@tonic-gate 	char *di_compat_strp;
27247c478bd9Sstevel@tonic-gate 	size_t di_compat_strlen;
27257c478bd9Sstevel@tonic-gate 
27267c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_compat_names) {
27277c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
27287c478bd9Sstevel@tonic-gate 	}
27297c478bd9Sstevel@tonic-gate 
27307c478bd9Sstevel@tonic-gate 	prop_flags = DDI_PROP_TYPE_STRING | DDI_PROP_DONTPASS;
27317c478bd9Sstevel@tonic-gate 
27327c478bd9Sstevel@tonic-gate 	if (flag & KM_NOSLEEP) {
27337c478bd9Sstevel@tonic-gate 		prop_flags |= DDI_PROP_DONTSLEEP;
27347c478bd9Sstevel@tonic-gate 	}
27357c478bd9Sstevel@tonic-gate 
27367c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_prom_node(dip) == 0) {
27377c478bd9Sstevel@tonic-gate 		prop_flags |= DDI_PROP_NOTPROM;
27387c478bd9Sstevel@tonic-gate 	}
27397c478bd9Sstevel@tonic-gate 
27407c478bd9Sstevel@tonic-gate 	rv = ddi_prop_lookup_common(DDI_DEV_T_ANY, dip, prop_flags,
27417c478bd9Sstevel@tonic-gate 	    "compatible", &compatstrpp, &ncompatstrs,
27427c478bd9Sstevel@tonic-gate 	    ddi_prop_fm_decode_strings);
27437c478bd9Sstevel@tonic-gate 
27447c478bd9Sstevel@tonic-gate 	if (rv == DDI_PROP_NOT_FOUND) {
27457c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
27467c478bd9Sstevel@tonic-gate 	}
27477c478bd9Sstevel@tonic-gate 
27487c478bd9Sstevel@tonic-gate 	if (rv != DDI_PROP_SUCCESS) {
27497c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
27507c478bd9Sstevel@tonic-gate 	}
27517c478bd9Sstevel@tonic-gate 
27527c478bd9Sstevel@tonic-gate 	/*
27537c478bd9Sstevel@tonic-gate 	 * encode the compatible property data in the dev_info node
27547c478bd9Sstevel@tonic-gate 	 */
27557c478bd9Sstevel@tonic-gate 	rv = DDI_SUCCESS;
27567c478bd9Sstevel@tonic-gate 	if (ncompatstrs != 0) {
27577c478bd9Sstevel@tonic-gate 		di_compat_strp = encode_composite_string(compatstrpp,
27587c478bd9Sstevel@tonic-gate 		    ncompatstrs, &di_compat_strlen, flag);
27597c478bd9Sstevel@tonic-gate 		if (di_compat_strp != NULL) {
27607c478bd9Sstevel@tonic-gate 			DEVI(dip)->devi_compat_names = di_compat_strp;
27617c478bd9Sstevel@tonic-gate 			DEVI(dip)->devi_compat_length = di_compat_strlen;
27627c478bd9Sstevel@tonic-gate 		} else {
27637c478bd9Sstevel@tonic-gate 			rv = DDI_FAILURE;
27647c478bd9Sstevel@tonic-gate 		}
27657c478bd9Sstevel@tonic-gate 	}
27667c478bd9Sstevel@tonic-gate 	ddi_prop_free(compatstrpp);
27677c478bd9Sstevel@tonic-gate 	return (rv);
27687c478bd9Sstevel@tonic-gate }
27697c478bd9Sstevel@tonic-gate 
27707c478bd9Sstevel@tonic-gate /*
27717c478bd9Sstevel@tonic-gate  * Create a composite string from a list of strings.
27727c478bd9Sstevel@tonic-gate  *
27737c478bd9Sstevel@tonic-gate  * A composite string consists of a single buffer containing one
27747c478bd9Sstevel@tonic-gate  * or more NULL terminated strings.
27757c478bd9Sstevel@tonic-gate  */
27767c478bd9Sstevel@tonic-gate static char *
27777c478bd9Sstevel@tonic-gate encode_composite_string(char **strings, uint_t nstrings, size_t *retsz,
27787c478bd9Sstevel@tonic-gate     uint_t flag)
27797c478bd9Sstevel@tonic-gate {
27807c478bd9Sstevel@tonic-gate 	uint_t index;
27817c478bd9Sstevel@tonic-gate 	char  **strpp;
27827c478bd9Sstevel@tonic-gate 	uint_t slen;
27837c478bd9Sstevel@tonic-gate 	size_t cbuf_sz = 0;
27847c478bd9Sstevel@tonic-gate 	char *cbuf_p;
27857c478bd9Sstevel@tonic-gate 	char *cbuf_ip;
27867c478bd9Sstevel@tonic-gate 
27877c478bd9Sstevel@tonic-gate 	if (strings == NULL || nstrings == 0 || retsz == NULL) {
27887c478bd9Sstevel@tonic-gate 		return (NULL);
27897c478bd9Sstevel@tonic-gate 	}
27907c478bd9Sstevel@tonic-gate 
27917c478bd9Sstevel@tonic-gate 	for (index = 0, strpp = strings; index < nstrings; index++)
27927c478bd9Sstevel@tonic-gate 		cbuf_sz += strlen(*(strpp++)) + 1;
27937c478bd9Sstevel@tonic-gate 
27947c478bd9Sstevel@tonic-gate 	if ((cbuf_p = kmem_alloc(cbuf_sz, flag)) == NULL) {
27957c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE,
27967c478bd9Sstevel@tonic-gate 		    "?failed to allocate device node compatstr");
27977c478bd9Sstevel@tonic-gate 		return (NULL);
27987c478bd9Sstevel@tonic-gate 	}
27997c478bd9Sstevel@tonic-gate 
28007c478bd9Sstevel@tonic-gate 	cbuf_ip = cbuf_p;
28017c478bd9Sstevel@tonic-gate 	for (index = 0, strpp = strings; index < nstrings; index++) {
28027c478bd9Sstevel@tonic-gate 		slen = strlen(*strpp);
28037c478bd9Sstevel@tonic-gate 		bcopy(*(strpp++), cbuf_ip, slen);
28047c478bd9Sstevel@tonic-gate 		cbuf_ip += slen;
28057c478bd9Sstevel@tonic-gate 		*(cbuf_ip++) = '\0';
28067c478bd9Sstevel@tonic-gate 	}
28077c478bd9Sstevel@tonic-gate 
28087c478bd9Sstevel@tonic-gate 	*retsz = cbuf_sz;
28097c478bd9Sstevel@tonic-gate 	return (cbuf_p);
28107c478bd9Sstevel@tonic-gate }
28117c478bd9Sstevel@tonic-gate 
28127c478bd9Sstevel@tonic-gate static void
28137c478bd9Sstevel@tonic-gate link_to_driver_list(dev_info_t *dip)
28147c478bd9Sstevel@tonic-gate {
28157c478bd9Sstevel@tonic-gate 	major_t major = DEVI(dip)->devi_major;
28167c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
28177c478bd9Sstevel@tonic-gate 
2818a204de77Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
28197c478bd9Sstevel@tonic-gate 
28207c478bd9Sstevel@tonic-gate 	/*
28217c478bd9Sstevel@tonic-gate 	 * Remove from orphan list
28227c478bd9Sstevel@tonic-gate 	 */
28237c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
28247c478bd9Sstevel@tonic-gate 		dnp = &orphanlist;
28257c478bd9Sstevel@tonic-gate 		remove_from_dn_list(dnp, dip);
28267c478bd9Sstevel@tonic-gate 	}
28277c478bd9Sstevel@tonic-gate 
28287c478bd9Sstevel@tonic-gate 	/*
28297c478bd9Sstevel@tonic-gate 	 * Add to per driver list
28307c478bd9Sstevel@tonic-gate 	 */
28317c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
28327c478bd9Sstevel@tonic-gate 	add_to_dn_list(dnp, dip);
28337c478bd9Sstevel@tonic-gate }
28347c478bd9Sstevel@tonic-gate 
28357c478bd9Sstevel@tonic-gate static void
28367c478bd9Sstevel@tonic-gate unlink_from_driver_list(dev_info_t *dip)
28377c478bd9Sstevel@tonic-gate {
28387c478bd9Sstevel@tonic-gate 	major_t major = DEVI(dip)->devi_major;
28397c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
28407c478bd9Sstevel@tonic-gate 
2841a204de77Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
28427c478bd9Sstevel@tonic-gate 
28437c478bd9Sstevel@tonic-gate 	/*
28447c478bd9Sstevel@tonic-gate 	 * Remove from per-driver list
28457c478bd9Sstevel@tonic-gate 	 */
28467c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
28477c478bd9Sstevel@tonic-gate 	remove_from_dn_list(dnp, dip);
28487c478bd9Sstevel@tonic-gate 
28497c478bd9Sstevel@tonic-gate 	/*
28507c478bd9Sstevel@tonic-gate 	 * Add to orphan list
28517c478bd9Sstevel@tonic-gate 	 */
28527c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
28537c478bd9Sstevel@tonic-gate 		dnp = &orphanlist;
28547c478bd9Sstevel@tonic-gate 		add_to_dn_list(dnp, dip);
28557c478bd9Sstevel@tonic-gate 	}
28567c478bd9Sstevel@tonic-gate }
28577c478bd9Sstevel@tonic-gate 
28587c478bd9Sstevel@tonic-gate /*
28597c478bd9Sstevel@tonic-gate  * scan the per-driver list looking for dev_info "dip"
28607c478bd9Sstevel@tonic-gate  */
28617c478bd9Sstevel@tonic-gate static dev_info_t *
28627c478bd9Sstevel@tonic-gate in_dn_list(struct devnames *dnp, dev_info_t *dip)
28637c478bd9Sstevel@tonic-gate {
28647c478bd9Sstevel@tonic-gate 	struct dev_info *idevi;
28657c478bd9Sstevel@tonic-gate 
28667c478bd9Sstevel@tonic-gate 	if ((idevi = DEVI(dnp->dn_head)) == NULL)
28677c478bd9Sstevel@tonic-gate 		return (NULL);
28687c478bd9Sstevel@tonic-gate 
28697c478bd9Sstevel@tonic-gate 	while (idevi) {
28707c478bd9Sstevel@tonic-gate 		if (idevi == DEVI(dip))
28717c478bd9Sstevel@tonic-gate 			return (dip);
28727c478bd9Sstevel@tonic-gate 		idevi = idevi->devi_next;
28737c478bd9Sstevel@tonic-gate 	}
28747c478bd9Sstevel@tonic-gate 	return (NULL);
28757c478bd9Sstevel@tonic-gate }
28767c478bd9Sstevel@tonic-gate 
28777c478bd9Sstevel@tonic-gate /*
28787c478bd9Sstevel@tonic-gate  * insert devinfo node 'dip' into the per-driver instance list
28797c478bd9Sstevel@tonic-gate  * headed by 'dnp'
28807c478bd9Sstevel@tonic-gate  *
28817c478bd9Sstevel@tonic-gate  * Nodes on the per-driver list are ordered: HW - SID - PSEUDO.  The order is
28827c478bd9Sstevel@tonic-gate  * required for merging of .conf file data to work properly.
28837c478bd9Sstevel@tonic-gate  */
28847c478bd9Sstevel@tonic-gate static void
28857c478bd9Sstevel@tonic-gate add_to_ordered_dn_list(struct devnames *dnp, dev_info_t *dip)
28867c478bd9Sstevel@tonic-gate {
28877c478bd9Sstevel@tonic-gate 	dev_info_t **dipp;
28887c478bd9Sstevel@tonic-gate 
28897c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&(dnp->dn_lock)));
28907c478bd9Sstevel@tonic-gate 
28917c478bd9Sstevel@tonic-gate 	dipp = &dnp->dn_head;
28927c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_prom_node(dip)) {
28937c478bd9Sstevel@tonic-gate 		/*
28947c478bd9Sstevel@tonic-gate 		 * Find the first non-prom node or end of list
28957c478bd9Sstevel@tonic-gate 		 */
28967c478bd9Sstevel@tonic-gate 		while (*dipp && (ndi_dev_is_prom_node(*dipp) != 0)) {
28977c478bd9Sstevel@tonic-gate 			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
28987c478bd9Sstevel@tonic-gate 		}
28997c478bd9Sstevel@tonic-gate 	} else if (ndi_dev_is_persistent_node(dip)) {
29007c478bd9Sstevel@tonic-gate 		/*
29017c478bd9Sstevel@tonic-gate 		 * Find the first non-persistent node
29027c478bd9Sstevel@tonic-gate 		 */
29037c478bd9Sstevel@tonic-gate 		while (*dipp && (ndi_dev_is_persistent_node(*dipp) != 0)) {
29047c478bd9Sstevel@tonic-gate 			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
29057c478bd9Sstevel@tonic-gate 		}
29067c478bd9Sstevel@tonic-gate 	} else {
29077c478bd9Sstevel@tonic-gate 		/*
29087c478bd9Sstevel@tonic-gate 		 * Find the end of the list
29097c478bd9Sstevel@tonic-gate 		 */
29107c478bd9Sstevel@tonic-gate 		while (*dipp) {
29117c478bd9Sstevel@tonic-gate 			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
29127c478bd9Sstevel@tonic-gate 		}
29137c478bd9Sstevel@tonic-gate 	}
29147c478bd9Sstevel@tonic-gate 
29157c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_next = DEVI(*dipp);
29167c478bd9Sstevel@tonic-gate 	*dipp = dip;
29177c478bd9Sstevel@tonic-gate }
29187c478bd9Sstevel@tonic-gate 
29197c478bd9Sstevel@tonic-gate /*
29207c478bd9Sstevel@tonic-gate  * add a list of device nodes to the device node list in the
29217c478bd9Sstevel@tonic-gate  * devnames structure
29227c478bd9Sstevel@tonic-gate  */
29237c478bd9Sstevel@tonic-gate static void
29247c478bd9Sstevel@tonic-gate add_to_dn_list(struct devnames *dnp, dev_info_t *dip)
29257c478bd9Sstevel@tonic-gate {
29267c478bd9Sstevel@tonic-gate 	/*
29277c478bd9Sstevel@tonic-gate 	 * Look to see if node already exists
29287c478bd9Sstevel@tonic-gate 	 */
29297c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&(dnp->dn_lock));
29307c478bd9Sstevel@tonic-gate 	if (in_dn_list(dnp, dip)) {
29317c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "add_to_dn_list: node %s already in list",
29327c478bd9Sstevel@tonic-gate 		    DEVI(dip)->devi_node_name);
29337c478bd9Sstevel@tonic-gate 	} else {
29347c478bd9Sstevel@tonic-gate 		add_to_ordered_dn_list(dnp, dip);
29357c478bd9Sstevel@tonic-gate 	}
29367c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
29377c478bd9Sstevel@tonic-gate }
29387c478bd9Sstevel@tonic-gate 
29397c478bd9Sstevel@tonic-gate static void
29407c478bd9Sstevel@tonic-gate remove_from_dn_list(struct devnames *dnp, dev_info_t *dip)
29417c478bd9Sstevel@tonic-gate {
29427c478bd9Sstevel@tonic-gate 	dev_info_t **plist;
29437c478bd9Sstevel@tonic-gate 
29447c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&(dnp->dn_lock));
29457c478bd9Sstevel@tonic-gate 
29467c478bd9Sstevel@tonic-gate 	plist = (dev_info_t **)&dnp->dn_head;
29477c478bd9Sstevel@tonic-gate 	while (*plist && (*plist != dip)) {
29487c478bd9Sstevel@tonic-gate 		plist = (dev_info_t **)&DEVI(*plist)->devi_next;
29497c478bd9Sstevel@tonic-gate 	}
29507c478bd9Sstevel@tonic-gate 
29517c478bd9Sstevel@tonic-gate 	if (*plist != NULL) {
29527c478bd9Sstevel@tonic-gate 		ASSERT(*plist == dip);
29537c478bd9Sstevel@tonic-gate 		*plist = (dev_info_t *)(DEVI(dip)->devi_next);
29547c478bd9Sstevel@tonic-gate 		DEVI(dip)->devi_next = NULL;
29557c478bd9Sstevel@tonic-gate 	} else {
29567c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE,
29577c478bd9Sstevel@tonic-gate 		    "remove_from_dn_list: node %s not found in list",
29587c478bd9Sstevel@tonic-gate 		    DEVI(dip)->devi_node_name));
29597c478bd9Sstevel@tonic-gate 	}
29607c478bd9Sstevel@tonic-gate 
29617c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
29627c478bd9Sstevel@tonic-gate }
29637c478bd9Sstevel@tonic-gate 
29647c478bd9Sstevel@tonic-gate /*
29657c478bd9Sstevel@tonic-gate  * Add and remove reference driver global property list
29667c478bd9Sstevel@tonic-gate  */
29677c478bd9Sstevel@tonic-gate static void
29687c478bd9Sstevel@tonic-gate add_global_props(dev_info_t *dip)
29697c478bd9Sstevel@tonic-gate {
29707c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
29717c478bd9Sstevel@tonic-gate 	ddi_prop_list_t *plist;
29727c478bd9Sstevel@tonic-gate 
29737c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_global_prop_list == NULL);
2974a204de77Scth 	ASSERT(DEVI(dip)->devi_major != DDI_MAJOR_T_NONE);
29757c478bd9Sstevel@tonic-gate 
29767c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[DEVI(dip)->devi_major];
29777c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
29787c478bd9Sstevel@tonic-gate 	plist = dnp->dn_global_prop_ptr;
29797c478bd9Sstevel@tonic-gate 	if (plist == NULL) {
29807c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
29817c478bd9Sstevel@tonic-gate 		return;
29827c478bd9Sstevel@tonic-gate 	}
29837c478bd9Sstevel@tonic-gate 	i_ddi_prop_list_hold(plist, dnp);
29847c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
29857c478bd9Sstevel@tonic-gate 
29867c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
29877c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_global_prop_list = plist;
29887c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
29897c478bd9Sstevel@tonic-gate }
29907c478bd9Sstevel@tonic-gate 
29917c478bd9Sstevel@tonic-gate static void
29927c478bd9Sstevel@tonic-gate remove_global_props(dev_info_t *dip)
29937c478bd9Sstevel@tonic-gate {
29947c478bd9Sstevel@tonic-gate 	ddi_prop_list_t *proplist;
29957c478bd9Sstevel@tonic-gate 
29967c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
29977c478bd9Sstevel@tonic-gate 	proplist = DEVI(dip)->devi_global_prop_list;
29987c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_global_prop_list = NULL;
29997c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
30007c478bd9Sstevel@tonic-gate 
30017c478bd9Sstevel@tonic-gate 	if (proplist) {
30027c478bd9Sstevel@tonic-gate 		major_t major;
30037c478bd9Sstevel@tonic-gate 		struct devnames *dnp;
30047c478bd9Sstevel@tonic-gate 
30057c478bd9Sstevel@tonic-gate 		major = ddi_driver_major(dip);
3006a204de77Scth 		ASSERT(major != DDI_MAJOR_T_NONE);
30077c478bd9Sstevel@tonic-gate 		dnp = &devnamesp[major];
30087c478bd9Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
30097c478bd9Sstevel@tonic-gate 		i_ddi_prop_list_rele(proplist, dnp);
30107c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
30117c478bd9Sstevel@tonic-gate 	}
30127c478bd9Sstevel@tonic-gate }
30137c478bd9Sstevel@tonic-gate 
30147c478bd9Sstevel@tonic-gate #ifdef DEBUG
30157c478bd9Sstevel@tonic-gate /*
30167c478bd9Sstevel@tonic-gate  * Set this variable to '0' to disable the optimization,
30177c478bd9Sstevel@tonic-gate  * and to 2 to print debug message.
30187c478bd9Sstevel@tonic-gate  */
30197c478bd9Sstevel@tonic-gate static int optimize_dtree = 1;
30207c478bd9Sstevel@tonic-gate 
30217c478bd9Sstevel@tonic-gate static void
30227c478bd9Sstevel@tonic-gate debug_dtree(dev_info_t *devi, struct dev_info *adevi, char *service)
30237c478bd9Sstevel@tonic-gate {
30247c478bd9Sstevel@tonic-gate 	char *adeviname, *buf;
30257c478bd9Sstevel@tonic-gate 
30267c478bd9Sstevel@tonic-gate 	/*
30277c478bd9Sstevel@tonic-gate 	 * Don't print unless optimize dtree is set to 2+
30287c478bd9Sstevel@tonic-gate 	 */
30297c478bd9Sstevel@tonic-gate 	if (optimize_dtree <= 1)
30307c478bd9Sstevel@tonic-gate 		return;
30317c478bd9Sstevel@tonic-gate 
30327c478bd9Sstevel@tonic-gate 	buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
30337c478bd9Sstevel@tonic-gate 	adeviname = ddi_deviname((dev_info_t *)adevi, buf);
30347c478bd9Sstevel@tonic-gate 	if (*adeviname == '\0')
30357c478bd9Sstevel@tonic-gate 		adeviname = "root";
30367c478bd9Sstevel@tonic-gate 
30377c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "%s %s -> %s\n",
30387c478bd9Sstevel@tonic-gate 	    ddi_deviname(devi, buf), service, adeviname);
30397c478bd9Sstevel@tonic-gate 
30407c478bd9Sstevel@tonic-gate 	kmem_free(buf, MAXNAMELEN);
30417c478bd9Sstevel@tonic-gate }
30427c478bd9Sstevel@tonic-gate #else /* DEBUG */
30437c478bd9Sstevel@tonic-gate #define	debug_dtree(a1, a2, a3)	 /* nothing */
30447c478bd9Sstevel@tonic-gate #endif  /* DEBUG */
30457c478bd9Sstevel@tonic-gate 
30467c478bd9Sstevel@tonic-gate static void
30477c478bd9Sstevel@tonic-gate ddi_optimize_dtree(dev_info_t *devi)
30487c478bd9Sstevel@tonic-gate {
30497c478bd9Sstevel@tonic-gate 	struct dev_info *pdevi;
30507c478bd9Sstevel@tonic-gate 	struct bus_ops *b;
30517c478bd9Sstevel@tonic-gate 
30527c478bd9Sstevel@tonic-gate 	pdevi = DEVI(devi)->devi_parent;
30537c478bd9Sstevel@tonic-gate 	ASSERT(pdevi);
30547c478bd9Sstevel@tonic-gate 
30557c478bd9Sstevel@tonic-gate 	/*
30567c478bd9Sstevel@tonic-gate 	 * Set the unoptimized values
30577c478bd9Sstevel@tonic-gate 	 */
30587c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_map_fault = pdevi;
30597c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_map = pdevi;
30607c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_allochdl = pdevi;
30617c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_freehdl = pdevi;
30627c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_bindhdl = pdevi;
30637c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_bindfunc =
30647c478bd9Sstevel@tonic-gate 	    pdevi->devi_ops->devo_bus_ops->bus_dma_bindhdl;
30657c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_unbindhdl = pdevi;
30667c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_unbindfunc =
30677c478bd9Sstevel@tonic-gate 	    pdevi->devi_ops->devo_bus_ops->bus_dma_unbindhdl;
30687c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_flush = pdevi;
30697c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_win = pdevi;
30707c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_ctl = pdevi;
30717c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_ctl = pdevi;
30727c478bd9Sstevel@tonic-gate 
30737c478bd9Sstevel@tonic-gate #ifdef DEBUG
30747c478bd9Sstevel@tonic-gate 	if (optimize_dtree == 0)
30757c478bd9Sstevel@tonic-gate 		return;
30767c478bd9Sstevel@tonic-gate #endif /* DEBUG */
30777c478bd9Sstevel@tonic-gate 
30787c478bd9Sstevel@tonic-gate 	b = pdevi->devi_ops->devo_bus_ops;
30797c478bd9Sstevel@tonic-gate 
30807c478bd9Sstevel@tonic-gate 	if (i_ddi_map_fault == b->bus_map_fault) {
30817c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_map_fault = pdevi->devi_bus_map_fault;
30827c478bd9Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_map_fault,
30837c478bd9Sstevel@tonic-gate 		    "bus_map_fault");
30847c478bd9Sstevel@tonic-gate 	}
30857c478bd9Sstevel@tonic-gate 
30867c478bd9Sstevel@tonic-gate 	if (ddi_dma_map == b->bus_dma_map) {
30877c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_map = pdevi->devi_bus_dma_map;
30887c478bd9Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_map, "bus_dma_map");
30897c478bd9Sstevel@tonic-gate 	}
30907c478bd9Sstevel@tonic-gate 
30917c478bd9Sstevel@tonic-gate 	if (ddi_dma_allochdl == b->bus_dma_allochdl) {
30927c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_allochdl =
30937c478bd9Sstevel@tonic-gate 		    pdevi->devi_bus_dma_allochdl;
30947c478bd9Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_allochdl,
30957c478bd9Sstevel@tonic-gate 		    "bus_dma_allochdl");
30967c478bd9Sstevel@tonic-gate 	}
30977c478bd9Sstevel@tonic-gate 
30987c478bd9Sstevel@tonic-gate 	if (ddi_dma_freehdl == b->bus_dma_freehdl) {
30997c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_freehdl = pdevi->devi_bus_dma_freehdl;
31007c478bd9Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_freehdl,
31017c478bd9Sstevel@tonic-gate 		    "bus_dma_freehdl");
31027c478bd9Sstevel@tonic-gate 	}
31037c478bd9Sstevel@tonic-gate 
31047c478bd9Sstevel@tonic-gate 	if (ddi_dma_bindhdl == b->bus_dma_bindhdl) {
31057c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_bindhdl = pdevi->devi_bus_dma_bindhdl;
31067c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_bindfunc =
31077c478bd9Sstevel@tonic-gate 		    pdevi->devi_bus_dma_bindhdl->devi_ops->
31087c478bd9Sstevel@tonic-gate 		    devo_bus_ops->bus_dma_bindhdl;
31097c478bd9Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_bindhdl,
31107c478bd9Sstevel@tonic-gate 		    "bus_dma_bindhdl");
31117c478bd9Sstevel@tonic-gate 	}
31127c478bd9Sstevel@tonic-gate 
31137c478bd9Sstevel@tonic-gate 	if (ddi_dma_unbindhdl == b->bus_dma_unbindhdl) {
31147c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_unbindhdl =
31157c478bd9Sstevel@tonic-gate 		    pdevi->devi_bus_dma_unbindhdl;
31167c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_unbindfunc =
31177c478bd9Sstevel@tonic-gate 		    pdevi->devi_bus_dma_unbindhdl->devi_ops->
31187c478bd9Sstevel@tonic-gate 		    devo_bus_ops->bus_dma_unbindhdl;
31197c478bd9Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_unbindhdl,
31207c478bd9Sstevel@tonic-gate 		    "bus_dma_unbindhdl");
31217c478bd9Sstevel@tonic-gate 	}
31227c478bd9Sstevel@tonic-gate 
31237c478bd9Sstevel@tonic-gate 	if (ddi_dma_flush == b->bus_dma_flush) {
31247c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_flush = pdevi->devi_bus_dma_flush;
31257c478bd9Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_flush,
31267c478bd9Sstevel@tonic-gate 		    "bus_dma_flush");
31277c478bd9Sstevel@tonic-gate 	}
31287c478bd9Sstevel@tonic-gate 
31297c478bd9Sstevel@tonic-gate 	if (ddi_dma_win == b->bus_dma_win) {
31307c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_win = pdevi->devi_bus_dma_win;
31317c478bd9Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_win,
31327c478bd9Sstevel@tonic-gate 		    "bus_dma_win");
31337c478bd9Sstevel@tonic-gate 	}
31347c478bd9Sstevel@tonic-gate 
31357c478bd9Sstevel@tonic-gate 	if (ddi_dma_mctl == b->bus_dma_ctl) {
31367c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_ctl = pdevi->devi_bus_dma_ctl;
31377c478bd9Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_ctl, "bus_dma_ctl");
31387c478bd9Sstevel@tonic-gate 	}
31397c478bd9Sstevel@tonic-gate 
31407c478bd9Sstevel@tonic-gate 	if (ddi_ctlops == b->bus_ctl) {
31417c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_ctl = pdevi->devi_bus_ctl;
31427c478bd9Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_ctl, "bus_ctl");
31437c478bd9Sstevel@tonic-gate 	}
31447c478bd9Sstevel@tonic-gate }
31457c478bd9Sstevel@tonic-gate 
31467c478bd9Sstevel@tonic-gate #define	MIN_DEVINFO_LOG_SIZE	max_ncpus
31477c478bd9Sstevel@tonic-gate #define	MAX_DEVINFO_LOG_SIZE	max_ncpus * 10
31487c478bd9Sstevel@tonic-gate 
31497c478bd9Sstevel@tonic-gate static void
31507c478bd9Sstevel@tonic-gate da_log_init()
31517c478bd9Sstevel@tonic-gate {
31527c478bd9Sstevel@tonic-gate 	devinfo_log_header_t *dh;
31537c478bd9Sstevel@tonic-gate 	int logsize = devinfo_log_size;
31547c478bd9Sstevel@tonic-gate 
31557c478bd9Sstevel@tonic-gate 	if (logsize == 0)
31567c478bd9Sstevel@tonic-gate 		logsize = MIN_DEVINFO_LOG_SIZE;
31577c478bd9Sstevel@tonic-gate 	else if (logsize > MAX_DEVINFO_LOG_SIZE)
31587c478bd9Sstevel@tonic-gate 		logsize = MAX_DEVINFO_LOG_SIZE;
31597c478bd9Sstevel@tonic-gate 
31607c478bd9Sstevel@tonic-gate 	dh = kmem_alloc(logsize * PAGESIZE, KM_SLEEP);
31617c478bd9Sstevel@tonic-gate 	mutex_init(&dh->dh_lock, NULL, MUTEX_DEFAULT, NULL);
31627c478bd9Sstevel@tonic-gate 	dh->dh_max = ((logsize * PAGESIZE) - sizeof (*dh)) /
31637c478bd9Sstevel@tonic-gate 	    sizeof (devinfo_audit_t) + 1;
31647c478bd9Sstevel@tonic-gate 	dh->dh_curr = -1;
31657c478bd9Sstevel@tonic-gate 	dh->dh_hits = 0;
31667c478bd9Sstevel@tonic-gate 
31677c478bd9Sstevel@tonic-gate 	devinfo_audit_log = dh;
31687c478bd9Sstevel@tonic-gate }
31697c478bd9Sstevel@tonic-gate 
31707c478bd9Sstevel@tonic-gate /*
31717c478bd9Sstevel@tonic-gate  * Log the stack trace in per-devinfo audit structure and also enter
31727c478bd9Sstevel@tonic-gate  * it into a system wide log for recording the time history.
31737c478bd9Sstevel@tonic-gate  */
31747c478bd9Sstevel@tonic-gate static void
31757c478bd9Sstevel@tonic-gate da_log_enter(dev_info_t *dip)
31767c478bd9Sstevel@tonic-gate {
31777c478bd9Sstevel@tonic-gate 	devinfo_audit_t *da_log, *da = DEVI(dip)->devi_audit;
31787c478bd9Sstevel@tonic-gate 	devinfo_log_header_t *dh = devinfo_audit_log;
31797c478bd9Sstevel@tonic-gate 
31807c478bd9Sstevel@tonic-gate 	if (devinfo_audit_log == NULL)
31817c478bd9Sstevel@tonic-gate 		return;
31827c478bd9Sstevel@tonic-gate 
31837c478bd9Sstevel@tonic-gate 	ASSERT(da != NULL);
31847c478bd9Sstevel@tonic-gate 
31857c478bd9Sstevel@tonic-gate 	da->da_devinfo = dip;
31867c478bd9Sstevel@tonic-gate 	da->da_timestamp = gethrtime();
31877c478bd9Sstevel@tonic-gate 	da->da_thread = curthread;
31887c478bd9Sstevel@tonic-gate 	da->da_node_state = DEVI(dip)->devi_node_state;
31897c478bd9Sstevel@tonic-gate 	da->da_device_state = DEVI(dip)->devi_state;
31907c478bd9Sstevel@tonic-gate 	da->da_depth = getpcstack(da->da_stack, DDI_STACK_DEPTH);
31917c478bd9Sstevel@tonic-gate 
31927c478bd9Sstevel@tonic-gate 	/*
31937c478bd9Sstevel@tonic-gate 	 * Copy into common log and note the location for tracing history
31947c478bd9Sstevel@tonic-gate 	 */
31957c478bd9Sstevel@tonic-gate 	mutex_enter(&dh->dh_lock);
31967c478bd9Sstevel@tonic-gate 	dh->dh_hits++;
31977c478bd9Sstevel@tonic-gate 	dh->dh_curr++;
31987c478bd9Sstevel@tonic-gate 	if (dh->dh_curr >= dh->dh_max)
31997c478bd9Sstevel@tonic-gate 		dh->dh_curr -= dh->dh_max;
32007c478bd9Sstevel@tonic-gate 	da_log = &dh->dh_entry[dh->dh_curr];
32017c478bd9Sstevel@tonic-gate 	mutex_exit(&dh->dh_lock);
32027c478bd9Sstevel@tonic-gate 
32037c478bd9Sstevel@tonic-gate 	bcopy(da, da_log, sizeof (devinfo_audit_t));
32047c478bd9Sstevel@tonic-gate 	da->da_lastlog = da_log;
32057c478bd9Sstevel@tonic-gate }
32067c478bd9Sstevel@tonic-gate 
32077c478bd9Sstevel@tonic-gate static void
32087c478bd9Sstevel@tonic-gate attach_drivers()
32097c478bd9Sstevel@tonic-gate {
32107c478bd9Sstevel@tonic-gate 	int i;
32117c478bd9Sstevel@tonic-gate 	for (i = 0; i < devcnt; i++) {
32127c478bd9Sstevel@tonic-gate 		struct devnames *dnp = &devnamesp[i];
32137c478bd9Sstevel@tonic-gate 		if ((dnp->dn_flags & DN_FORCE_ATTACH) &&
32147c478bd9Sstevel@tonic-gate 		    (ddi_hold_installed_driver((major_t)i) != NULL))
32157c478bd9Sstevel@tonic-gate 			ddi_rele_driver((major_t)i);
32167c478bd9Sstevel@tonic-gate 	}
32177c478bd9Sstevel@tonic-gate }
32187c478bd9Sstevel@tonic-gate 
32197c478bd9Sstevel@tonic-gate /*
32207c478bd9Sstevel@tonic-gate  * Launch a thread to force attach drivers. This avoids penalty on boot time.
32217c478bd9Sstevel@tonic-gate  */
32227c478bd9Sstevel@tonic-gate void
32237c478bd9Sstevel@tonic-gate i_ddi_forceattach_drivers()
32247c478bd9Sstevel@tonic-gate {
32257c478bd9Sstevel@tonic-gate 	/*
32267c478bd9Sstevel@tonic-gate 	 * On i386, the USB drivers need to load and take over from the
32277c478bd9Sstevel@tonic-gate 	 * SMM BIOS drivers ASAP after consconfig(), so make sure they
32287c478bd9Sstevel@tonic-gate 	 * get loaded right here rather than letting the thread do it.
32297c478bd9Sstevel@tonic-gate 	 *
32307c478bd9Sstevel@tonic-gate 	 * The order here is important.  EHCI must be loaded first, as
32317c478bd9Sstevel@tonic-gate 	 * we have observed many systems on which hangs occur if the
32327c478bd9Sstevel@tonic-gate 	 * {U,O}HCI companion controllers take over control from the BIOS
32337c478bd9Sstevel@tonic-gate 	 * before EHCI does.  These hangs are also caused by BIOSes leaving
32347c478bd9Sstevel@tonic-gate 	 * interrupt-on-port-change enabled in the ehci controller, so that
32357c478bd9Sstevel@tonic-gate 	 * when uhci/ohci reset themselves, it induces a port change on
32367c478bd9Sstevel@tonic-gate 	 * the ehci companion controller.  Since there's no interrupt handler
32377c478bd9Sstevel@tonic-gate 	 * installed at the time, the moment that interrupt is unmasked, an
32387c478bd9Sstevel@tonic-gate 	 * interrupt storm will occur.  All this is averted when ehci is
32397c478bd9Sstevel@tonic-gate 	 * loaded first.  And now you know..... the REST of the story.
32407c478bd9Sstevel@tonic-gate 	 *
32417c478bd9Sstevel@tonic-gate 	 * Regardless of platform, ehci needs to initialize first to avoid
32427c478bd9Sstevel@tonic-gate 	 * unnecessary connects and disconnects on the companion controller
32437c478bd9Sstevel@tonic-gate 	 * when ehci sets up the routing.
32447c478bd9Sstevel@tonic-gate 	 */
32457c478bd9Sstevel@tonic-gate 	(void) ddi_hold_installed_driver(ddi_name_to_major("ehci"));
32467c478bd9Sstevel@tonic-gate 	(void) ddi_hold_installed_driver(ddi_name_to_major("uhci"));
32477c478bd9Sstevel@tonic-gate 	(void) ddi_hold_installed_driver(ddi_name_to_major("ohci"));
32487c478bd9Sstevel@tonic-gate 
32499d3d2ed0Shiremath 	/*
32509d3d2ed0Shiremath 	 * Attach IB VHCI driver before the force-attach thread attaches the
32519d3d2ed0Shiremath 	 * IB HCA driver. IB HCA driver will fail if IB Nexus has not yet
32529d3d2ed0Shiremath 	 * been attached.
32539d3d2ed0Shiremath 	 */
32549d3d2ed0Shiremath 	(void) ddi_hold_installed_driver(ddi_name_to_major("ib"));
32559d3d2ed0Shiremath 
32567c478bd9Sstevel@tonic-gate 	(void) thread_create(NULL, 0, (void (*)())attach_drivers, NULL, 0, &p0,
32577c478bd9Sstevel@tonic-gate 	    TS_RUN, minclsyspri);
32587c478bd9Sstevel@tonic-gate }
32597c478bd9Sstevel@tonic-gate 
32607c478bd9Sstevel@tonic-gate /*
32617c478bd9Sstevel@tonic-gate  * This is a private DDI interface for optimizing boot performance.
32627c478bd9Sstevel@tonic-gate  * I/O subsystem initialization is considered complete when devfsadm
32637c478bd9Sstevel@tonic-gate  * is executed.
32647c478bd9Sstevel@tonic-gate  *
3265facf4a8dSllai1  * NOTE: The start of syseventd happens to be a convenient indicator
3266facf4a8dSllai1  *	of the completion of I/O initialization during boot.
32677c478bd9Sstevel@tonic-gate  *	The implementation should be replaced by something more robust.
32687c478bd9Sstevel@tonic-gate  */
32697c478bd9Sstevel@tonic-gate int
32707c478bd9Sstevel@tonic-gate i_ddi_io_initialized()
32717c478bd9Sstevel@tonic-gate {
32727c478bd9Sstevel@tonic-gate 	extern int sysevent_daemon_init;
32737c478bd9Sstevel@tonic-gate 	return (sysevent_daemon_init);
32747c478bd9Sstevel@tonic-gate }
32757c478bd9Sstevel@tonic-gate 
3276facf4a8dSllai1 /*
3277facf4a8dSllai1  * May be used to determine system boot state
3278facf4a8dSllai1  * "Available" means the system is for the most part up
3279facf4a8dSllai1  * and initialized, with all system services either up or
3280facf4a8dSllai1  * capable of being started.  This state is set by devfsadm
3281facf4a8dSllai1  * during the boot process.  The /dev filesystem infers
3282facf4a8dSllai1  * from this when implicit reconfig can be performed,
3283facf4a8dSllai1  * ie, devfsadm can be invoked.  Please avoid making
3284facf4a8dSllai1  * further use of this unless it's really necessary.
3285facf4a8dSllai1  */
3286facf4a8dSllai1 int
3287facf4a8dSllai1 i_ddi_sysavail()
3288facf4a8dSllai1 {
3289facf4a8dSllai1 	return (devname_state & DS_SYSAVAIL);
3290facf4a8dSllai1 }
3291facf4a8dSllai1 
3292facf4a8dSllai1 /*
3293facf4a8dSllai1  * May be used to determine if boot is a reconfigure boot.
3294facf4a8dSllai1  */
3295facf4a8dSllai1 int
3296facf4a8dSllai1 i_ddi_reconfig()
3297facf4a8dSllai1 {
3298facf4a8dSllai1 	return (devname_state & DS_RECONFIG);
3299facf4a8dSllai1 }
3300facf4a8dSllai1 
3301facf4a8dSllai1 /*
3302facf4a8dSllai1  * Note system services are up, inform /dev.
3303facf4a8dSllai1  */
3304facf4a8dSllai1 void
3305facf4a8dSllai1 i_ddi_set_sysavail()
3306facf4a8dSllai1 {
3307facf4a8dSllai1 	if ((devname_state & DS_SYSAVAIL) == 0) {
3308facf4a8dSllai1 		devname_state |= DS_SYSAVAIL;
3309facf4a8dSllai1 		sdev_devstate_change();
3310facf4a8dSllai1 	}
3311facf4a8dSllai1 }
3312facf4a8dSllai1 
3313facf4a8dSllai1 /*
3314facf4a8dSllai1  * Note reconfiguration boot, inform /dev.
3315facf4a8dSllai1  */
3316facf4a8dSllai1 void
3317facf4a8dSllai1 i_ddi_set_reconfig()
3318facf4a8dSllai1 {
3319facf4a8dSllai1 	if ((devname_state & DS_RECONFIG) == 0) {
3320facf4a8dSllai1 		devname_state |= DS_RECONFIG;
3321facf4a8dSllai1 		sdev_devstate_change();
3322facf4a8dSllai1 	}
3323facf4a8dSllai1 }
3324facf4a8dSllai1 
33257c478bd9Sstevel@tonic-gate 
33267c478bd9Sstevel@tonic-gate /*
33277c478bd9Sstevel@tonic-gate  * device tree walking
33287c478bd9Sstevel@tonic-gate  */
33297c478bd9Sstevel@tonic-gate 
33307c478bd9Sstevel@tonic-gate struct walk_elem {
33317c478bd9Sstevel@tonic-gate 	struct walk_elem *next;
33327c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
33337c478bd9Sstevel@tonic-gate };
33347c478bd9Sstevel@tonic-gate 
33357c478bd9Sstevel@tonic-gate static void
33367c478bd9Sstevel@tonic-gate free_list(struct walk_elem *list)
33377c478bd9Sstevel@tonic-gate {
33387c478bd9Sstevel@tonic-gate 	while (list) {
33397c478bd9Sstevel@tonic-gate 		struct walk_elem *next = list->next;
33407c478bd9Sstevel@tonic-gate 		kmem_free(list, sizeof (*list));
33417c478bd9Sstevel@tonic-gate 		list = next;
33427c478bd9Sstevel@tonic-gate 	}
33437c478bd9Sstevel@tonic-gate }
33447c478bd9Sstevel@tonic-gate 
33457c478bd9Sstevel@tonic-gate static void
33467c478bd9Sstevel@tonic-gate append_node(struct walk_elem **list, dev_info_t *dip)
33477c478bd9Sstevel@tonic-gate {
33487c478bd9Sstevel@tonic-gate 	struct walk_elem *tail;
33497c478bd9Sstevel@tonic-gate 	struct walk_elem *elem = kmem_alloc(sizeof (*elem), KM_SLEEP);
33507c478bd9Sstevel@tonic-gate 
33517c478bd9Sstevel@tonic-gate 	elem->next = NULL;
33527c478bd9Sstevel@tonic-gate 	elem->dip = dip;
33537c478bd9Sstevel@tonic-gate 
33547c478bd9Sstevel@tonic-gate 	if (*list == NULL) {
33557c478bd9Sstevel@tonic-gate 		*list = elem;
33567c478bd9Sstevel@tonic-gate 		return;
33577c478bd9Sstevel@tonic-gate 	}
33587c478bd9Sstevel@tonic-gate 
33597c478bd9Sstevel@tonic-gate 	tail = *list;
33607c478bd9Sstevel@tonic-gate 	while (tail->next)
33617c478bd9Sstevel@tonic-gate 		tail = tail->next;
33627c478bd9Sstevel@tonic-gate 
33637c478bd9Sstevel@tonic-gate 	tail->next = elem;
33647c478bd9Sstevel@tonic-gate }
33657c478bd9Sstevel@tonic-gate 
33667c478bd9Sstevel@tonic-gate /*
33677c478bd9Sstevel@tonic-gate  * The implementation of ddi_walk_devs().
33687c478bd9Sstevel@tonic-gate  */
33697c478bd9Sstevel@tonic-gate static int
33707c478bd9Sstevel@tonic-gate walk_devs(dev_info_t *dip, int (*f)(dev_info_t *, void *), void *arg,
33717c478bd9Sstevel@tonic-gate     int do_locking)
33727c478bd9Sstevel@tonic-gate {
33737c478bd9Sstevel@tonic-gate 	struct walk_elem *head = NULL;
33747c478bd9Sstevel@tonic-gate 
33757c478bd9Sstevel@tonic-gate 	/*
33767c478bd9Sstevel@tonic-gate 	 * Do it in two passes. First pass invoke callback on each
33777c478bd9Sstevel@tonic-gate 	 * dip on the sibling list. Second pass invoke callback on
33787c478bd9Sstevel@tonic-gate 	 * children of each dip.
33797c478bd9Sstevel@tonic-gate 	 */
33807c478bd9Sstevel@tonic-gate 	while (dip) {
33817c478bd9Sstevel@tonic-gate 		switch ((*f)(dip, arg)) {
33827c478bd9Sstevel@tonic-gate 		case DDI_WALK_TERMINATE:
33837c478bd9Sstevel@tonic-gate 			free_list(head);
33847c478bd9Sstevel@tonic-gate 			return (DDI_WALK_TERMINATE);
33857c478bd9Sstevel@tonic-gate 
33867c478bd9Sstevel@tonic-gate 		case DDI_WALK_PRUNESIB:
33877c478bd9Sstevel@tonic-gate 			/* ignore sibling by setting dip to NULL */
33887c478bd9Sstevel@tonic-gate 			append_node(&head, dip);
33897c478bd9Sstevel@tonic-gate 			dip = NULL;
33907c478bd9Sstevel@tonic-gate 			break;
33917c478bd9Sstevel@tonic-gate 
33927c478bd9Sstevel@tonic-gate 		case DDI_WALK_PRUNECHILD:
33937c478bd9Sstevel@tonic-gate 			/* don't worry about children */
33947c478bd9Sstevel@tonic-gate 			dip = ddi_get_next_sibling(dip);
33957c478bd9Sstevel@tonic-gate 			break;
33967c478bd9Sstevel@tonic-gate 
33977c478bd9Sstevel@tonic-gate 		case DDI_WALK_CONTINUE:
33987c478bd9Sstevel@tonic-gate 		default:
33997c478bd9Sstevel@tonic-gate 			append_node(&head, dip);
34007c478bd9Sstevel@tonic-gate 			dip = ddi_get_next_sibling(dip);
34017c478bd9Sstevel@tonic-gate 			break;
34027c478bd9Sstevel@tonic-gate 		}
34037c478bd9Sstevel@tonic-gate 
34047c478bd9Sstevel@tonic-gate 	}
34057c478bd9Sstevel@tonic-gate 
34067c478bd9Sstevel@tonic-gate 	/* second pass */
34077c478bd9Sstevel@tonic-gate 	while (head) {
34087c478bd9Sstevel@tonic-gate 		int circ;
34097c478bd9Sstevel@tonic-gate 		struct walk_elem *next = head->next;
34107c478bd9Sstevel@tonic-gate 
34117c478bd9Sstevel@tonic-gate 		if (do_locking)
34127c478bd9Sstevel@tonic-gate 			ndi_devi_enter(head->dip, &circ);
34137c478bd9Sstevel@tonic-gate 		if (walk_devs(ddi_get_child(head->dip), f, arg, do_locking) ==
34147c478bd9Sstevel@tonic-gate 		    DDI_WALK_TERMINATE) {
34157c478bd9Sstevel@tonic-gate 			if (do_locking)
34167c478bd9Sstevel@tonic-gate 				ndi_devi_exit(head->dip, circ);
34177c478bd9Sstevel@tonic-gate 			free_list(head);
34187c478bd9Sstevel@tonic-gate 			return (DDI_WALK_TERMINATE);
34197c478bd9Sstevel@tonic-gate 		}
34207c478bd9Sstevel@tonic-gate 		if (do_locking)
34217c478bd9Sstevel@tonic-gate 			ndi_devi_exit(head->dip, circ);
34227c478bd9Sstevel@tonic-gate 		kmem_free(head, sizeof (*head));
34237c478bd9Sstevel@tonic-gate 		head = next;
34247c478bd9Sstevel@tonic-gate 	}
34257c478bd9Sstevel@tonic-gate 
34267c478bd9Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
34277c478bd9Sstevel@tonic-gate }
34287c478bd9Sstevel@tonic-gate 
34297c478bd9Sstevel@tonic-gate /*
34307c478bd9Sstevel@tonic-gate  * This general-purpose routine traverses the tree of dev_info nodes,
34317c478bd9Sstevel@tonic-gate  * starting from the given node, and calls the given function for each
34327c478bd9Sstevel@tonic-gate  * node that it finds with the current node and the pointer arg (which
34337c478bd9Sstevel@tonic-gate  * can point to a structure of information that the function
34347c478bd9Sstevel@tonic-gate  * needs) as arguments.
34357c478bd9Sstevel@tonic-gate  *
34367c478bd9Sstevel@tonic-gate  * It does the walk a layer at a time, not depth-first. The given function
34377c478bd9Sstevel@tonic-gate  * must return one of the following values:
34387c478bd9Sstevel@tonic-gate  *	DDI_WALK_CONTINUE
34397c478bd9Sstevel@tonic-gate  *	DDI_WALK_PRUNESIB
34407c478bd9Sstevel@tonic-gate  *	DDI_WALK_PRUNECHILD
34417c478bd9Sstevel@tonic-gate  *	DDI_WALK_TERMINATE
34427c478bd9Sstevel@tonic-gate  *
34437c478bd9Sstevel@tonic-gate  * N.B. Since we walk the sibling list, the caller must ensure that
34447c478bd9Sstevel@tonic-gate  *	the parent of dip is held against changes, unless the parent
34457c478bd9Sstevel@tonic-gate  *	is rootnode.  ndi_devi_enter() on the parent is sufficient.
34467c478bd9Sstevel@tonic-gate  *
34477c478bd9Sstevel@tonic-gate  *	To avoid deadlock situations, caller must not attempt to
34487c478bd9Sstevel@tonic-gate  *	configure/unconfigure/remove device node in (*f)(), nor should
34495e3986cbScth  *	it attempt to recurse on other nodes in the system. Any
34505e3986cbScth  *	ndi_devi_enter() done by (*f)() must occur 'at-or-below' the
34515e3986cbScth  *	node entered prior to ddi_walk_devs(). Furthermore, if (*f)()
34525e3986cbScth  *	does any multi-threading (in framework *or* in driver) then the
34535e3986cbScth  *	ndi_devi_enter() calls done by dependent threads must be
34545e3986cbScth  *	'strictly-below'.
34557c478bd9Sstevel@tonic-gate  *
34567c478bd9Sstevel@tonic-gate  *	This is not callable from device autoconfiguration routines.
34577c478bd9Sstevel@tonic-gate  *	They include, but not limited to, _init(9e), _fini(9e), probe(9e),
34587c478bd9Sstevel@tonic-gate  *	attach(9e), and detach(9e).
34597c478bd9Sstevel@tonic-gate  */
34607c478bd9Sstevel@tonic-gate 
34617c478bd9Sstevel@tonic-gate void
34627c478bd9Sstevel@tonic-gate ddi_walk_devs(dev_info_t *dip, int (*f)(dev_info_t *, void *), void *arg)
34637c478bd9Sstevel@tonic-gate {
34647c478bd9Sstevel@tonic-gate 
34657c478bd9Sstevel@tonic-gate 	ASSERT(dip == NULL || ddi_get_parent(dip) == NULL ||
34667c478bd9Sstevel@tonic-gate 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
34677c478bd9Sstevel@tonic-gate 
34687c478bd9Sstevel@tonic-gate 	(void) walk_devs(dip, f, arg, 1);
34697c478bd9Sstevel@tonic-gate }
34707c478bd9Sstevel@tonic-gate 
34717c478bd9Sstevel@tonic-gate /*
34727c478bd9Sstevel@tonic-gate  * This is a general-purpose routine traverses the per-driver list
34737c478bd9Sstevel@tonic-gate  * and calls the given function for each node. must return one of
34747c478bd9Sstevel@tonic-gate  * the following values:
34757c478bd9Sstevel@tonic-gate  *	DDI_WALK_CONTINUE
34767c478bd9Sstevel@tonic-gate  *	DDI_WALK_TERMINATE
34777c478bd9Sstevel@tonic-gate  *
34787c478bd9Sstevel@tonic-gate  * N.B. The same restrictions from ddi_walk_devs() apply.
34797c478bd9Sstevel@tonic-gate  */
34807c478bd9Sstevel@tonic-gate 
34817c478bd9Sstevel@tonic-gate void
34827c478bd9Sstevel@tonic-gate e_ddi_walk_driver(char *drv, int (*f)(dev_info_t *, void *), void *arg)
34837c478bd9Sstevel@tonic-gate {
34847c478bd9Sstevel@tonic-gate 	major_t major;
34857c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
34867c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
34877c478bd9Sstevel@tonic-gate 
34887c478bd9Sstevel@tonic-gate 	major = ddi_name_to_major(drv);
3489a204de77Scth 	if (major == DDI_MAJOR_T_NONE)
34907c478bd9Sstevel@tonic-gate 		return;
34917c478bd9Sstevel@tonic-gate 
34927c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
34937c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
34947c478bd9Sstevel@tonic-gate 	dip = dnp->dn_head;
34957c478bd9Sstevel@tonic-gate 	while (dip) {
34967c478bd9Sstevel@tonic-gate 		ndi_hold_devi(dip);
34977c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
34987c478bd9Sstevel@tonic-gate 		if ((*f)(dip, arg) == DDI_WALK_TERMINATE) {
34997c478bd9Sstevel@tonic-gate 			ndi_rele_devi(dip);
35007c478bd9Sstevel@tonic-gate 			return;
35017c478bd9Sstevel@tonic-gate 		}
35027c478bd9Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
35037c478bd9Sstevel@tonic-gate 		ndi_rele_devi(dip);
35047c478bd9Sstevel@tonic-gate 		dip = ddi_get_next(dip);
35057c478bd9Sstevel@tonic-gate 	}
35067c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
35077c478bd9Sstevel@tonic-gate }
35087c478bd9Sstevel@tonic-gate 
35097c478bd9Sstevel@tonic-gate /*
35107c478bd9Sstevel@tonic-gate  * argument to i_find_devi, a devinfo node search callback function.
35117c478bd9Sstevel@tonic-gate  */
35127c478bd9Sstevel@tonic-gate struct match_info {
35137c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;		/* result */
35147c478bd9Sstevel@tonic-gate 	char		*nodename;	/* if non-null, nodename must match */
35157c478bd9Sstevel@tonic-gate 	int		instance;	/* if != -1, instance must match */
3516737d277aScth 	int		attached;	/* if != 0, i_ddi_devi_attached() */
35177c478bd9Sstevel@tonic-gate };
35187c478bd9Sstevel@tonic-gate 
35197c478bd9Sstevel@tonic-gate static int
35207c478bd9Sstevel@tonic-gate i_find_devi(dev_info_t *dip, void *arg)
35217c478bd9Sstevel@tonic-gate {
35227c478bd9Sstevel@tonic-gate 	struct match_info *info = (struct match_info *)arg;
35237c478bd9Sstevel@tonic-gate 
35247c478bd9Sstevel@tonic-gate 	if (((info->nodename == NULL) ||
35257c478bd9Sstevel@tonic-gate 	    (strcmp(ddi_node_name(dip), info->nodename) == 0)) &&
35267c478bd9Sstevel@tonic-gate 	    ((info->instance == -1) ||
35277c478bd9Sstevel@tonic-gate 	    (ddi_get_instance(dip) == info->instance)) &&
3528737d277aScth 	    ((info->attached == 0) || i_ddi_devi_attached(dip))) {
35297c478bd9Sstevel@tonic-gate 		info->dip = dip;
35307c478bd9Sstevel@tonic-gate 		ndi_hold_devi(dip);
35317c478bd9Sstevel@tonic-gate 		return (DDI_WALK_TERMINATE);
35327c478bd9Sstevel@tonic-gate 	}
35337c478bd9Sstevel@tonic-gate 
35347c478bd9Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
35357c478bd9Sstevel@tonic-gate }
35367c478bd9Sstevel@tonic-gate 
35377c478bd9Sstevel@tonic-gate /*
35387c478bd9Sstevel@tonic-gate  * Find dip with a known node name and instance and return with it held
35397c478bd9Sstevel@tonic-gate  */
35407c478bd9Sstevel@tonic-gate dev_info_t *
35417c478bd9Sstevel@tonic-gate ddi_find_devinfo(char *nodename, int instance, int attached)
35427c478bd9Sstevel@tonic-gate {
35437c478bd9Sstevel@tonic-gate 	struct match_info	info;
35447c478bd9Sstevel@tonic-gate 
35457c478bd9Sstevel@tonic-gate 	info.nodename = nodename;
35467c478bd9Sstevel@tonic-gate 	info.instance = instance;
35477c478bd9Sstevel@tonic-gate 	info.attached = attached;
35487c478bd9Sstevel@tonic-gate 	info.dip = NULL;
35497c478bd9Sstevel@tonic-gate 
35507c478bd9Sstevel@tonic-gate 	ddi_walk_devs(ddi_root_node(), i_find_devi, &info);
35517c478bd9Sstevel@tonic-gate 	return (info.dip);
35527c478bd9Sstevel@tonic-gate }
35537c478bd9Sstevel@tonic-gate 
35547c478bd9Sstevel@tonic-gate /*
35557c478bd9Sstevel@tonic-gate  * Parse for name, addr, and minor names. Some args may be NULL.
35567c478bd9Sstevel@tonic-gate  */
35577c478bd9Sstevel@tonic-gate void
35587c478bd9Sstevel@tonic-gate i_ddi_parse_name(char *name, char **nodename, char **addrname, char **minorname)
35597c478bd9Sstevel@tonic-gate {
35607c478bd9Sstevel@tonic-gate 	char *cp;
35617c478bd9Sstevel@tonic-gate 	static char nulladdrname[] = "";
35627c478bd9Sstevel@tonic-gate 
35637c478bd9Sstevel@tonic-gate 	/* default values */
35647c478bd9Sstevel@tonic-gate 	if (nodename)
35657c478bd9Sstevel@tonic-gate 		*nodename = name;
35667c478bd9Sstevel@tonic-gate 	if (addrname)
35677c478bd9Sstevel@tonic-gate 		*addrname = nulladdrname;
35687c478bd9Sstevel@tonic-gate 	if (minorname)
35697c478bd9Sstevel@tonic-gate 		*minorname = NULL;
35707c478bd9Sstevel@tonic-gate 
35717c478bd9Sstevel@tonic-gate 	cp = name;
35727c478bd9Sstevel@tonic-gate 	while (*cp != '\0') {
35737c478bd9Sstevel@tonic-gate 		if (addrname && *cp == '@') {
35747c478bd9Sstevel@tonic-gate 			*addrname = cp + 1;
35757c478bd9Sstevel@tonic-gate 			*cp = '\0';
35767c478bd9Sstevel@tonic-gate 		} else if (minorname && *cp == ':') {
35777c478bd9Sstevel@tonic-gate 			*minorname = cp + 1;
35787c478bd9Sstevel@tonic-gate 			*cp = '\0';
35797c478bd9Sstevel@tonic-gate 		}
35807c478bd9Sstevel@tonic-gate 		++cp;
35817c478bd9Sstevel@tonic-gate 	}
35827c478bd9Sstevel@tonic-gate }
35837c478bd9Sstevel@tonic-gate 
35847c478bd9Sstevel@tonic-gate static char *
35857c478bd9Sstevel@tonic-gate child_path_to_driver(dev_info_t *parent, char *child_name, char *unit_address)
35867c478bd9Sstevel@tonic-gate {
35877c478bd9Sstevel@tonic-gate 	char *p, *drvname = NULL;
35887c478bd9Sstevel@tonic-gate 	major_t maj;
35897c478bd9Sstevel@tonic-gate 
35907c478bd9Sstevel@tonic-gate 	/*
35917c478bd9Sstevel@tonic-gate 	 * Construct the pathname and ask the implementation
35927c478bd9Sstevel@tonic-gate 	 * if it can do a driver = f(pathname) for us, if not
35937c478bd9Sstevel@tonic-gate 	 * we'll just default to using the node-name that
35947c478bd9Sstevel@tonic-gate 	 * was given to us.  We want to do this first to
35957c478bd9Sstevel@tonic-gate 	 * allow the platform to use 'generic' names for
35967c478bd9Sstevel@tonic-gate 	 * legacy device drivers.
35977c478bd9Sstevel@tonic-gate 	 */
35987c478bd9Sstevel@tonic-gate 	p = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
35997c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(parent, p);
36007c478bd9Sstevel@tonic-gate 	(void) strcat(p, "/");
36017c478bd9Sstevel@tonic-gate 	(void) strcat(p, child_name);
36027c478bd9Sstevel@tonic-gate 	if (unit_address && *unit_address) {
36037c478bd9Sstevel@tonic-gate 		(void) strcat(p, "@");
36047c478bd9Sstevel@tonic-gate 		(void) strcat(p, unit_address);
36057c478bd9Sstevel@tonic-gate 	}
36067c478bd9Sstevel@tonic-gate 
36077c478bd9Sstevel@tonic-gate 	/*
36087c478bd9Sstevel@tonic-gate 	 * Get the binding. If there is none, return the child_name
36097c478bd9Sstevel@tonic-gate 	 * and let the caller deal with it.
36107c478bd9Sstevel@tonic-gate 	 */
36117c478bd9Sstevel@tonic-gate 	maj = path_to_major(p);
36127c478bd9Sstevel@tonic-gate 
36137c478bd9Sstevel@tonic-gate 	kmem_free(p, MAXPATHLEN);
36147c478bd9Sstevel@tonic-gate 
3615a204de77Scth 	if (maj != DDI_MAJOR_T_NONE)
36167c478bd9Sstevel@tonic-gate 		drvname = ddi_major_to_name(maj);
36177c478bd9Sstevel@tonic-gate 	if (drvname == NULL)
36187c478bd9Sstevel@tonic-gate 		drvname = child_name;
36197c478bd9Sstevel@tonic-gate 
36207c478bd9Sstevel@tonic-gate 	return (drvname);
36217c478bd9Sstevel@tonic-gate }
36227c478bd9Sstevel@tonic-gate 
36237c478bd9Sstevel@tonic-gate 
362420906b23SVikram Hegde #define	PCI_EX_CLASS	"pciexclass"
362520906b23SVikram Hegde #define	PCI_EX		"pciex"
362620906b23SVikram Hegde #define	PCI_CLASS	"pciclass"
362720906b23SVikram Hegde #define	PCI		"pci"
362820906b23SVikram Hegde 
362920906b23SVikram Hegde int
363020906b23SVikram Hegde ddi_is_pci_dip(dev_info_t *dip)
363120906b23SVikram Hegde {
363220906b23SVikram Hegde 	char	*prop = NULL;
363320906b23SVikram Hegde 
363420906b23SVikram Hegde 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
363520906b23SVikram Hegde 	    "compatible", &prop) == DDI_PROP_SUCCESS) {
363620906b23SVikram Hegde 		ASSERT(prop);
363720906b23SVikram Hegde 		if (strncmp(prop, PCI_EX_CLASS, sizeof (PCI_EX_CLASS) - 1)
363820906b23SVikram Hegde 		    == 0 ||
363920906b23SVikram Hegde 		    strncmp(prop, PCI_EX, sizeof (PCI_EX)- 1)
364020906b23SVikram Hegde 		    == 0 ||
364120906b23SVikram Hegde 		    strncmp(prop, PCI_CLASS, sizeof (PCI_CLASS) - 1)
364220906b23SVikram Hegde 		    == 0 ||
364320906b23SVikram Hegde 		    strncmp(prop, PCI, sizeof (PCI) - 1)
364420906b23SVikram Hegde 		    == 0) {
364520906b23SVikram Hegde 			ddi_prop_free(prop);
364620906b23SVikram Hegde 			return (1);
364720906b23SVikram Hegde 		}
364820906b23SVikram Hegde 	}
364920906b23SVikram Hegde 
365020906b23SVikram Hegde 	if (prop != NULL) {
365120906b23SVikram Hegde 		ddi_prop_free(prop);
365220906b23SVikram Hegde 	}
365320906b23SVikram Hegde 
365420906b23SVikram Hegde 	return (0);
365520906b23SVikram Hegde }
365620906b23SVikram Hegde 
36577c478bd9Sstevel@tonic-gate /*
36587c478bd9Sstevel@tonic-gate  * Given the pathname of a device, fill in the dev_info_t value and/or the
36597c478bd9Sstevel@tonic-gate  * dev_t value and/or the spectype, depending on which parameters are non-NULL.
36607c478bd9Sstevel@tonic-gate  * If there is an error, this function returns -1.
36617c478bd9Sstevel@tonic-gate  *
36627c478bd9Sstevel@tonic-gate  * NOTE: If this function returns the dev_info_t structure, then it
36637c478bd9Sstevel@tonic-gate  * does so with a hold on the devi. Caller should ensure that they get
36647c478bd9Sstevel@tonic-gate  * decremented via ddi_release_devi() or ndi_rele_devi();
36657c478bd9Sstevel@tonic-gate  *
36667c478bd9Sstevel@tonic-gate  * This function can be invoked in the boot case for a pathname without
36677c478bd9Sstevel@tonic-gate  * device argument (:xxxx), traditionally treated as a minor name.
36687c478bd9Sstevel@tonic-gate  * In this case, we do the following
36697c478bd9Sstevel@tonic-gate  * (1) search the minor node of type DDM_DEFAULT.
36707c478bd9Sstevel@tonic-gate  * (2) if no DDM_DEFAULT minor exists, then the first non-alias minor is chosen.
36717c478bd9Sstevel@tonic-gate  * (3) if neither exists, a dev_t is faked with minor number = instance.
36727c478bd9Sstevel@tonic-gate  * As of S9 FCS, no instance of #1 exists. #2 is used by several platforms
36737c478bd9Sstevel@tonic-gate  * to default the boot partition to :a possibly by other OBP definitions.
36747c478bd9Sstevel@tonic-gate  * #3 is used for booting off network interfaces, most SPARC network
36757c478bd9Sstevel@tonic-gate  * drivers support Style-2 only, so only DDM_ALIAS minor exists.
36767c478bd9Sstevel@tonic-gate  *
36777c478bd9Sstevel@tonic-gate  * It is possible for OBP to present device args at the end of the path as
36787c478bd9Sstevel@tonic-gate  * well as in the middle. For example, with IB the following strings are
36797c478bd9Sstevel@tonic-gate  * valid boot paths.
36807c478bd9Sstevel@tonic-gate  *	a /pci@8,700000/ib@1,2:port=1,pkey=ff,dhcp,...
36817c478bd9Sstevel@tonic-gate  *	b /pci@8,700000/ib@1,1:port=1/ioc@xxxxxx,yyyyyyy:dhcp
36827c478bd9Sstevel@tonic-gate  * Case (a), we first look for minor node "port=1,pkey...".
36837c478bd9Sstevel@tonic-gate  * Failing that, we will pass "port=1,pkey..." to the bus_config
36847c478bd9Sstevel@tonic-gate  * entry point of ib (HCA) driver.
36857c478bd9Sstevel@tonic-gate  * Case (b), configure ib@1,1 as usual. Then invoke ib's bus_config
36867c478bd9Sstevel@tonic-gate  * with argument "ioc@xxxxxxx,yyyyyyy:port=1". After configuring
36877c478bd9Sstevel@tonic-gate  * the ioc, look for minor node dhcp. If not found, pass ":dhcp"
36887c478bd9Sstevel@tonic-gate  * to ioc's bus_config entry point.
36897c478bd9Sstevel@tonic-gate  */
369020906b23SVikram Hegde static int
369120906b23SVikram Hegde parse_pathname(char *pathname,
369220906b23SVikram Hegde 	dev_info_t **dipp, dev_t *devtp, int *spectypep, dev_info_t **pci_dipp)
36937c478bd9Sstevel@tonic-gate {
36947c478bd9Sstevel@tonic-gate 	int			error;
36957c478bd9Sstevel@tonic-gate 	dev_info_t		*parent, *child;
36967c478bd9Sstevel@tonic-gate 	struct pathname		pn;
36977c478bd9Sstevel@tonic-gate 	char			*component, *config_name;
36987c478bd9Sstevel@tonic-gate 	char			*minorname = NULL;
36997c478bd9Sstevel@tonic-gate 	char			*prev_minor = NULL;
37007c478bd9Sstevel@tonic-gate 	dev_t			devt = NODEV;
37017c478bd9Sstevel@tonic-gate 	int			spectype;
37027c478bd9Sstevel@tonic-gate 	struct ddi_minor_data	*dmn;
3703b9ccdc5aScth 	int			circ;
37047c478bd9Sstevel@tonic-gate 
370520906b23SVikram Hegde 	if (pci_dipp)
370620906b23SVikram Hegde 		*pci_dipp = NULL;
370720906b23SVikram Hegde 
37087c478bd9Sstevel@tonic-gate 	if (*pathname != '/')
37097c478bd9Sstevel@tonic-gate 		return (EINVAL);
37107c478bd9Sstevel@tonic-gate 	parent = ddi_root_node();	/* Begin at the top of the tree */
37117c478bd9Sstevel@tonic-gate 
37127c478bd9Sstevel@tonic-gate 	if (error = pn_get(pathname, UIO_SYSSPACE, &pn))
37137c478bd9Sstevel@tonic-gate 		return (error);
37147c478bd9Sstevel@tonic-gate 	pn_skipslash(&pn);
37157c478bd9Sstevel@tonic-gate 
3716737d277aScth 	ASSERT(i_ddi_devi_attached(parent));
37177c478bd9Sstevel@tonic-gate 	ndi_hold_devi(parent);
37187c478bd9Sstevel@tonic-gate 
37197c478bd9Sstevel@tonic-gate 	component = kmem_alloc(MAXNAMELEN, KM_SLEEP);
37207c478bd9Sstevel@tonic-gate 	config_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
37217c478bd9Sstevel@tonic-gate 
37227c478bd9Sstevel@tonic-gate 	while (pn_pathleft(&pn)) {
37237c478bd9Sstevel@tonic-gate 		/* remember prev minor (:xxx) in the middle of path */
37247c478bd9Sstevel@tonic-gate 		if (minorname)
37257c478bd9Sstevel@tonic-gate 			prev_minor = i_ddi_strdup(minorname, KM_SLEEP);
37267c478bd9Sstevel@tonic-gate 
37277c478bd9Sstevel@tonic-gate 		/* Get component and chop off minorname */
37287c478bd9Sstevel@tonic-gate 		(void) pn_getcomponent(&pn, component);
37297c478bd9Sstevel@tonic-gate 		i_ddi_parse_name(component, NULL, NULL, &minorname);
37307c478bd9Sstevel@tonic-gate 
37317c478bd9Sstevel@tonic-gate 		if (prev_minor == NULL) {
37327c478bd9Sstevel@tonic-gate 			(void) snprintf(config_name, MAXNAMELEN, "%s",
37337c478bd9Sstevel@tonic-gate 			    component);
37347c478bd9Sstevel@tonic-gate 		} else {
37357c478bd9Sstevel@tonic-gate 			(void) snprintf(config_name, MAXNAMELEN, "%s:%s",
37367c478bd9Sstevel@tonic-gate 			    component, prev_minor);
37377c478bd9Sstevel@tonic-gate 			kmem_free(prev_minor, strlen(prev_minor) + 1);
37387c478bd9Sstevel@tonic-gate 			prev_minor = NULL;
37397c478bd9Sstevel@tonic-gate 		}
37407c478bd9Sstevel@tonic-gate 
37417c478bd9Sstevel@tonic-gate 		/*
37427c478bd9Sstevel@tonic-gate 		 * Find and configure the child
37437c478bd9Sstevel@tonic-gate 		 */
37447c478bd9Sstevel@tonic-gate 		if (ndi_devi_config_one(parent, config_name, &child,
37457c478bd9Sstevel@tonic-gate 		    NDI_PROMNAME | NDI_NO_EVENT) != NDI_SUCCESS) {
37467c478bd9Sstevel@tonic-gate 			ndi_rele_devi(parent);
37477c478bd9Sstevel@tonic-gate 			pn_free(&pn);
37487c478bd9Sstevel@tonic-gate 			kmem_free(component, MAXNAMELEN);
37497c478bd9Sstevel@tonic-gate 			kmem_free(config_name, MAXNAMELEN);
375020906b23SVikram Hegde 			if (pci_dipp && *pci_dipp) {
375120906b23SVikram Hegde 				ndi_rele_devi(*pci_dipp);
375220906b23SVikram Hegde 				*pci_dipp = NULL;
375320906b23SVikram Hegde 			}
37547c478bd9Sstevel@tonic-gate 			return (-1);
37557c478bd9Sstevel@tonic-gate 		}
37567c478bd9Sstevel@tonic-gate 
3757737d277aScth 		ASSERT(i_ddi_devi_attached(child));
37587c478bd9Sstevel@tonic-gate 		ndi_rele_devi(parent);
37597c478bd9Sstevel@tonic-gate 		parent = child;
37607c478bd9Sstevel@tonic-gate 		pn_skipslash(&pn);
376120906b23SVikram Hegde 		if (pci_dipp) {
376220906b23SVikram Hegde 			if (ddi_is_pci_dip(child)) {
376320906b23SVikram Hegde 				ndi_hold_devi(child);
376420906b23SVikram Hegde 				if (*pci_dipp != NULL) {
376520906b23SVikram Hegde 					ndi_rele_devi(*pci_dipp);
376620906b23SVikram Hegde 				}
376720906b23SVikram Hegde 				*pci_dipp = child;
376820906b23SVikram Hegde 			}
376920906b23SVikram Hegde 		}
37707c478bd9Sstevel@tonic-gate 	}
37717c478bd9Sstevel@tonic-gate 
37727c478bd9Sstevel@tonic-gate 	/*
37737c478bd9Sstevel@tonic-gate 	 * First look for a minor node matching minorname.
37747c478bd9Sstevel@tonic-gate 	 * Failing that, try to pass minorname to bus_config().
37757c478bd9Sstevel@tonic-gate 	 */
37767c478bd9Sstevel@tonic-gate 	if (minorname && i_ddi_minorname_to_devtspectype(parent,
37777c478bd9Sstevel@tonic-gate 	    minorname, &devt, &spectype) == DDI_FAILURE) {
37787c478bd9Sstevel@tonic-gate 		(void) snprintf(config_name, MAXNAMELEN, "%s", minorname);
37797c478bd9Sstevel@tonic-gate 		if (ndi_devi_config_obp_args(parent,
37807c478bd9Sstevel@tonic-gate 		    config_name, &child, 0) != NDI_SUCCESS) {
37817c478bd9Sstevel@tonic-gate 			ndi_rele_devi(parent);
37827c478bd9Sstevel@tonic-gate 			pn_free(&pn);
37837c478bd9Sstevel@tonic-gate 			kmem_free(component, MAXNAMELEN);
37847c478bd9Sstevel@tonic-gate 			kmem_free(config_name, MAXNAMELEN);
37857c478bd9Sstevel@tonic-gate 			NDI_CONFIG_DEBUG((CE_NOTE,
37867c478bd9Sstevel@tonic-gate 			    "%s: minor node not found\n", pathname));
378720906b23SVikram Hegde 			if (pci_dipp && *pci_dipp) {
378820906b23SVikram Hegde 				ndi_rele_devi(*pci_dipp);
378920906b23SVikram Hegde 				*pci_dipp = NULL;
379020906b23SVikram Hegde 			}
37917c478bd9Sstevel@tonic-gate 			return (-1);
37927c478bd9Sstevel@tonic-gate 		}
37937c478bd9Sstevel@tonic-gate 		minorname = NULL;	/* look for default minor */
3794737d277aScth 		ASSERT(i_ddi_devi_attached(child));
37957c478bd9Sstevel@tonic-gate 		ndi_rele_devi(parent);
37967c478bd9Sstevel@tonic-gate 		parent = child;
37977c478bd9Sstevel@tonic-gate 	}
37987c478bd9Sstevel@tonic-gate 
37997c478bd9Sstevel@tonic-gate 	if (devtp || spectypep) {
38007c478bd9Sstevel@tonic-gate 		if (minorname == NULL) {
3801b9ccdc5aScth 			/*
3802b9ccdc5aScth 			 * Search for a default entry with an active
3803b9ccdc5aScth 			 * ndi_devi_enter to protect the devi_minor list.
3804b9ccdc5aScth 			 */
3805b9ccdc5aScth 			ndi_devi_enter(parent, &circ);
38067c478bd9Sstevel@tonic-gate 			for (dmn = DEVI(parent)->devi_minor; dmn;
38077c478bd9Sstevel@tonic-gate 			    dmn = dmn->next) {
38087c478bd9Sstevel@tonic-gate 				if (dmn->type == DDM_DEFAULT) {
38097c478bd9Sstevel@tonic-gate 					devt = dmn->ddm_dev;
38107c478bd9Sstevel@tonic-gate 					spectype = dmn->ddm_spec_type;
38117c478bd9Sstevel@tonic-gate 					break;
38127c478bd9Sstevel@tonic-gate 				}
38137c478bd9Sstevel@tonic-gate 			}
38147c478bd9Sstevel@tonic-gate 
38157c478bd9Sstevel@tonic-gate 			if (devt == NODEV) {
38167c478bd9Sstevel@tonic-gate 				/*
38177c478bd9Sstevel@tonic-gate 				 * No default minor node, try the first one;
38187c478bd9Sstevel@tonic-gate 				 * else, assume 1-1 instance-minor mapping
38197c478bd9Sstevel@tonic-gate 				 */
38207c478bd9Sstevel@tonic-gate 				dmn = DEVI(parent)->devi_minor;
38217c478bd9Sstevel@tonic-gate 				if (dmn && ((dmn->type == DDM_MINOR) ||
38227c478bd9Sstevel@tonic-gate 				    (dmn->type == DDM_INTERNAL_PATH))) {
38237c478bd9Sstevel@tonic-gate 					devt = dmn->ddm_dev;
38247c478bd9Sstevel@tonic-gate 					spectype = dmn->ddm_spec_type;
38257c478bd9Sstevel@tonic-gate 				} else {
38267c478bd9Sstevel@tonic-gate 					devt = makedevice(
38277c478bd9Sstevel@tonic-gate 					    DEVI(parent)->devi_major,
38287c478bd9Sstevel@tonic-gate 					    ddi_get_instance(parent));
38297c478bd9Sstevel@tonic-gate 					spectype = S_IFCHR;
38307c478bd9Sstevel@tonic-gate 				}
38317c478bd9Sstevel@tonic-gate 			}
3832b9ccdc5aScth 			ndi_devi_exit(parent, circ);
38337c478bd9Sstevel@tonic-gate 		}
38347c478bd9Sstevel@tonic-gate 		if (devtp)
38357c478bd9Sstevel@tonic-gate 			*devtp = devt;
38367c478bd9Sstevel@tonic-gate 		if (spectypep)
38377c478bd9Sstevel@tonic-gate 			*spectypep = spectype;
38387c478bd9Sstevel@tonic-gate 	}
38397c478bd9Sstevel@tonic-gate 
38407c478bd9Sstevel@tonic-gate 	pn_free(&pn);
38417c478bd9Sstevel@tonic-gate 	kmem_free(component, MAXNAMELEN);
38427c478bd9Sstevel@tonic-gate 	kmem_free(config_name, MAXNAMELEN);
38437c478bd9Sstevel@tonic-gate 
38447c478bd9Sstevel@tonic-gate 	/*
38457c478bd9Sstevel@tonic-gate 	 * If there is no error, return the appropriate parameters
38467c478bd9Sstevel@tonic-gate 	 */
38477c478bd9Sstevel@tonic-gate 	if (dipp != NULL)
38487c478bd9Sstevel@tonic-gate 		*dipp = parent;
384920906b23SVikram Hegde 	else if (pci_dipp == NULL) {
38507c478bd9Sstevel@tonic-gate 		/*
38517c478bd9Sstevel@tonic-gate 		 * We should really keep the ref count to keep the node from
38527c478bd9Sstevel@tonic-gate 		 * detaching but ddi_pathname_to_dev_t() specifies a NULL dipp,
38537c478bd9Sstevel@tonic-gate 		 * so we have no way of passing back the held dip.  Not holding
38547c478bd9Sstevel@tonic-gate 		 * the dip allows detaches to occur - which can cause problems
38557c478bd9Sstevel@tonic-gate 		 * for subsystems which call ddi_pathname_to_dev_t (console).
38567c478bd9Sstevel@tonic-gate 		 *
38577c478bd9Sstevel@tonic-gate 		 * Instead of holding the dip, we place a ddi-no-autodetach
38587c478bd9Sstevel@tonic-gate 		 * property on the node to prevent auto detaching.
38597c478bd9Sstevel@tonic-gate 		 *
38607c478bd9Sstevel@tonic-gate 		 * The right fix is to remove ddi_pathname_to_dev_t and replace
38617c478bd9Sstevel@tonic-gate 		 * it, and all references, with a call that specifies a dipp.
38627c478bd9Sstevel@tonic-gate 		 * In addition, the callers of this new interfaces would then
38637c478bd9Sstevel@tonic-gate 		 * need to call ndi_rele_devi when the reference is complete.
386420906b23SVikram Hegde 		 *
386520906b23SVikram Hegde 		 * NOTE: If pci_dipp is non-NULL we are only interested
386620906b23SVikram Hegde 		 * in the PCI parent which is returned held. No need to hold
386720906b23SVikram Hegde 		 * the leaf dip.
38687c478bd9Sstevel@tonic-gate 		 */
38697c478bd9Sstevel@tonic-gate 		(void) ddi_prop_update_int(DDI_DEV_T_NONE, parent,
38707c478bd9Sstevel@tonic-gate 		    DDI_NO_AUTODETACH, 1);
38717c478bd9Sstevel@tonic-gate 		ndi_rele_devi(parent);
38727c478bd9Sstevel@tonic-gate 	}
38737c478bd9Sstevel@tonic-gate 
38747c478bd9Sstevel@tonic-gate 	return (0);
38757c478bd9Sstevel@tonic-gate }
38767c478bd9Sstevel@tonic-gate 
387720906b23SVikram Hegde int
387820906b23SVikram Hegde resolve_pathname(char *pathname,
387920906b23SVikram Hegde 	dev_info_t **dipp, dev_t *devtp, int *spectypep)
388020906b23SVikram Hegde {
388120906b23SVikram Hegde 	return (parse_pathname(pathname, dipp, devtp, spectypep, NULL));
388220906b23SVikram Hegde }
388320906b23SVikram Hegde 
388420906b23SVikram Hegde int
388520906b23SVikram Hegde ddi_find_pci_parent(char *pathname, dev_info_t **pci_dipp)
388620906b23SVikram Hegde {
388720906b23SVikram Hegde 	return (parse_pathname(pathname, NULL, NULL, NULL, pci_dipp));
388820906b23SVikram Hegde }
388920906b23SVikram Hegde 
38907c478bd9Sstevel@tonic-gate /*
38917c478bd9Sstevel@tonic-gate  * Given the pathname of a device, return the dev_t of the corresponding
38927c478bd9Sstevel@tonic-gate  * device.  Returns NODEV on failure.
38937c478bd9Sstevel@tonic-gate  *
38947c478bd9Sstevel@tonic-gate  * Note that this call sets the DDI_NO_AUTODETACH property on the devinfo node.
38957c478bd9Sstevel@tonic-gate  */
38967c478bd9Sstevel@tonic-gate dev_t
38977c478bd9Sstevel@tonic-gate ddi_pathname_to_dev_t(char *pathname)
38987c478bd9Sstevel@tonic-gate {
38997c478bd9Sstevel@tonic-gate 	dev_t devt;
39007c478bd9Sstevel@tonic-gate 	int error;
39017c478bd9Sstevel@tonic-gate 
39027c478bd9Sstevel@tonic-gate 	error = resolve_pathname(pathname, NULL, &devt, NULL);
39037c478bd9Sstevel@tonic-gate 
39047c478bd9Sstevel@tonic-gate 	return (error ? NODEV : devt);
39057c478bd9Sstevel@tonic-gate }
39067c478bd9Sstevel@tonic-gate 
39077c478bd9Sstevel@tonic-gate /*
39087c478bd9Sstevel@tonic-gate  * Translate a prom pathname to kernel devfs pathname.
39097c478bd9Sstevel@tonic-gate  * Caller is assumed to allocate devfspath memory of
39107c478bd9Sstevel@tonic-gate  * size at least MAXPATHLEN
39117c478bd9Sstevel@tonic-gate  *
39127c478bd9Sstevel@tonic-gate  * The prom pathname may not include minor name, but
39137c478bd9Sstevel@tonic-gate  * devfs pathname has a minor name portion.
39147c478bd9Sstevel@tonic-gate  */
39157c478bd9Sstevel@tonic-gate int
39167c478bd9Sstevel@tonic-gate i_ddi_prompath_to_devfspath(char *prompath, char *devfspath)
39177c478bd9Sstevel@tonic-gate {
39187c478bd9Sstevel@tonic-gate 	dev_t		devt = (dev_t)NODEV;
39197c478bd9Sstevel@tonic-gate 	dev_info_t	*dip = NULL;
39207c478bd9Sstevel@tonic-gate 	char		*minor_name = NULL;
39217c478bd9Sstevel@tonic-gate 	int		spectype;
39227c478bd9Sstevel@tonic-gate 	int		error;
3923b9ccdc5aScth 	int		circ;
39247c478bd9Sstevel@tonic-gate 
39257c478bd9Sstevel@tonic-gate 	error = resolve_pathname(prompath, &dip, &devt, &spectype);
39267c478bd9Sstevel@tonic-gate 	if (error)
39277c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
39287c478bd9Sstevel@tonic-gate 	ASSERT(dip && devt != NODEV);
39297c478bd9Sstevel@tonic-gate 
39307c478bd9Sstevel@tonic-gate 	/*
39317c478bd9Sstevel@tonic-gate 	 * Get in-kernel devfs pathname
39327c478bd9Sstevel@tonic-gate 	 */
39337c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(dip, devfspath);
39347c478bd9Sstevel@tonic-gate 
3935b9ccdc5aScth 	ndi_devi_enter(dip, &circ);
39367c478bd9Sstevel@tonic-gate 	minor_name = i_ddi_devtspectype_to_minorname(dip, devt, spectype);
39377c478bd9Sstevel@tonic-gate 	if (minor_name) {
39387c478bd9Sstevel@tonic-gate 		(void) strcat(devfspath, ":");
39397c478bd9Sstevel@tonic-gate 		(void) strcat(devfspath, minor_name);
39407c478bd9Sstevel@tonic-gate 	} else {
39417c478bd9Sstevel@tonic-gate 		/*
39427c478bd9Sstevel@tonic-gate 		 * If minor_name is NULL, we have an alias minor node.
39437c478bd9Sstevel@tonic-gate 		 * So manufacture a path to the corresponding clone minor.
39447c478bd9Sstevel@tonic-gate 		 */
39457c478bd9Sstevel@tonic-gate 		(void) snprintf(devfspath, MAXPATHLEN, "%s:%s",
39467c478bd9Sstevel@tonic-gate 		    CLONE_PATH, ddi_driver_name(dip));
39477c478bd9Sstevel@tonic-gate 	}
3948b9ccdc5aScth 	ndi_devi_exit(dip, circ);
39497c478bd9Sstevel@tonic-gate 
39507c478bd9Sstevel@tonic-gate 	/* release hold from resolve_pathname() */
39517c478bd9Sstevel@tonic-gate 	ndi_rele_devi(dip);
39527c478bd9Sstevel@tonic-gate 	return (0);
39537c478bd9Sstevel@tonic-gate }
39547c478bd9Sstevel@tonic-gate 
39557c478bd9Sstevel@tonic-gate /*
3956*19397407SSherry Moore  * This function is intended to identify drivers that must quiesce for fast
3957*19397407SSherry Moore  * reboot to succeed.  It does not claim to have more knowledge about the device
3958*19397407SSherry Moore  * than its driver.  If a driver has implemented quiesce(), it will be invoked;
3959*19397407SSherry Moore  * if a so identified driver does not manage any device that needs to be
3960*19397407SSherry Moore  * quiesced, it must explicitly set its devo_quiesce dev_op to
3961*19397407SSherry Moore  * ddi_quiesce_not_needed.
3962*19397407SSherry Moore  */
3963*19397407SSherry Moore static int skip_pseudo = 1;	/* Skip pseudo devices */
3964*19397407SSherry Moore static int skip_non_hw = 1;	/* Skip devices with no hardware property */
3965*19397407SSherry Moore static int
3966*19397407SSherry Moore should_implement_quiesce(dev_info_t *dip)
3967*19397407SSherry Moore {
3968*19397407SSherry Moore 	struct dev_info *devi = DEVI(dip);
3969*19397407SSherry Moore 	dev_info_t *pdip;
3970*19397407SSherry Moore 
3971*19397407SSherry Moore 	/*
3972*19397407SSherry Moore 	 * If dip is pseudo and skip_pseudo is set, driver doesn't have to
3973*19397407SSherry Moore 	 * implement quiesce().
3974*19397407SSherry Moore 	 */
3975*19397407SSherry Moore 	if (skip_pseudo &&
3976*19397407SSherry Moore 	    strncmp(ddi_binding_name(dip), "pseudo", sizeof ("pseudo")) == 0)
3977*19397407SSherry Moore 		return (0);
3978*19397407SSherry Moore 
3979*19397407SSherry Moore 	/*
3980*19397407SSherry Moore 	 * If parent dip is pseudo and skip_pseudo is set, driver doesn't have
3981*19397407SSherry Moore 	 * to implement quiesce().
3982*19397407SSherry Moore 	 */
3983*19397407SSherry Moore 	if (skip_pseudo && (pdip = ddi_get_parent(dip)) != NULL &&
3984*19397407SSherry Moore 	    strncmp(ddi_binding_name(pdip), "pseudo", sizeof ("pseudo")) == 0)
3985*19397407SSherry Moore 		return (0);
3986*19397407SSherry Moore 
3987*19397407SSherry Moore 	/*
3988*19397407SSherry Moore 	 * If not attached, driver doesn't have to implement quiesce().
3989*19397407SSherry Moore 	 */
3990*19397407SSherry Moore 	if (!i_ddi_devi_attached(dip))
3991*19397407SSherry Moore 		return (0);
3992*19397407SSherry Moore 
3993*19397407SSherry Moore 	/*
3994*19397407SSherry Moore 	 * If dip has no hardware property and skip_non_hw is set,
3995*19397407SSherry Moore 	 * driver doesn't have to implement quiesce().
3996*19397407SSherry Moore 	 */
3997*19397407SSherry Moore 	if (skip_non_hw && devi->devi_hw_prop_ptr == NULL)
3998*19397407SSherry Moore 		return (0);
3999*19397407SSherry Moore 
4000*19397407SSherry Moore 	return (1);
4001*19397407SSherry Moore }
4002*19397407SSherry Moore 
4003*19397407SSherry Moore static int
4004*19397407SSherry Moore driver_has_quiesce(struct dev_ops *ops)
4005*19397407SSherry Moore {
4006*19397407SSherry Moore 	if ((ops->devo_rev >= 4) && (ops->devo_quiesce != nodev) &&
4007*19397407SSherry Moore 	    (ops->devo_quiesce != NULL) && (ops->devo_quiesce != nulldev) &&
4008*19397407SSherry Moore 	    (ops->devo_quiesce != ddi_quiesce_not_supported))
4009*19397407SSherry Moore 		return (1);
4010*19397407SSherry Moore 	else
4011*19397407SSherry Moore 		return (0);
4012*19397407SSherry Moore }
4013*19397407SSherry Moore 
4014*19397407SSherry Moore /*
4015*19397407SSherry Moore  * Check to see if a driver has implemented the quiesce() DDI function.
4016*19397407SSherry Moore  */
4017*19397407SSherry Moore int
4018*19397407SSherry Moore check_driver_quiesce(dev_info_t *dip, void *arg)
4019*19397407SSherry Moore {
4020*19397407SSherry Moore 	struct dev_ops *ops;
4021*19397407SSherry Moore 
4022*19397407SSherry Moore 	if (!should_implement_quiesce(dip))
4023*19397407SSherry Moore 		return (DDI_WALK_CONTINUE);
4024*19397407SSherry Moore 
4025*19397407SSherry Moore 	if ((ops = ddi_get_driver(dip)) == NULL)
4026*19397407SSherry Moore 		return (DDI_WALK_CONTINUE);
4027*19397407SSherry Moore 
4028*19397407SSherry Moore 	if (driver_has_quiesce(ops)) {
4029*19397407SSherry Moore 		if ((quiesce_debug & 0x2) == 0x2) {
4030*19397407SSherry Moore 			if (ops->devo_quiesce == ddi_quiesce_not_needed)
4031*19397407SSherry Moore 				cmn_err(CE_CONT, "%s does not need to be "
4032*19397407SSherry Moore 				    "quiesced", ddi_driver_name(dip));
4033*19397407SSherry Moore 			else
4034*19397407SSherry Moore 				cmn_err(CE_CONT, "%s has quiesce routine",
4035*19397407SSherry Moore 				    ddi_driver_name(dip));
4036*19397407SSherry Moore 		}
4037*19397407SSherry Moore 	} else {
4038*19397407SSherry Moore 		if (arg != NULL)
4039*19397407SSherry Moore 			*((int *)arg) = -1;
4040*19397407SSherry Moore 		cmn_err(CE_WARN, "%s has no quiesce()", ddi_driver_name(dip));
4041*19397407SSherry Moore 	}
4042*19397407SSherry Moore 
4043*19397407SSherry Moore 	return (DDI_WALK_CONTINUE);
4044*19397407SSherry Moore }
4045*19397407SSherry Moore 
4046*19397407SSherry Moore /*
4047*19397407SSherry Moore  * Quiesce device.
4048*19397407SSherry Moore  */
4049*19397407SSherry Moore static void
4050*19397407SSherry Moore quiesce_one_device(dev_info_t *dip, void *arg)
4051*19397407SSherry Moore {
4052*19397407SSherry Moore 	struct dev_ops *ops;
4053*19397407SSherry Moore 	int should_quiesce = 0;
4054*19397407SSherry Moore 
4055*19397407SSherry Moore 	/*
4056*19397407SSherry Moore 	 * If the device is not attached it doesn't need to be quiesced.
4057*19397407SSherry Moore 	 */
4058*19397407SSherry Moore 	if (!i_ddi_devi_attached(dip))
4059*19397407SSherry Moore 		return;
4060*19397407SSherry Moore 
4061*19397407SSherry Moore 	if ((ops = ddi_get_driver(dip)) == NULL)
4062*19397407SSherry Moore 		return;
4063*19397407SSherry Moore 
4064*19397407SSherry Moore 	should_quiesce = should_implement_quiesce(dip);
4065*19397407SSherry Moore 
4066*19397407SSherry Moore 	/*
4067*19397407SSherry Moore 	 * If there's an implementation of quiesce(), always call it even if
4068*19397407SSherry Moore 	 * some of the drivers don't have quiesce() or quiesce() have failed
4069*19397407SSherry Moore 	 * so we can do force fast reboot.  The implementation of quiesce()
4070*19397407SSherry Moore 	 * should not negatively affect a regular reboot.
4071*19397407SSherry Moore 	 */
4072*19397407SSherry Moore 	if (driver_has_quiesce(ops)) {
4073*19397407SSherry Moore 		int rc = DDI_SUCCESS;
4074*19397407SSherry Moore 
4075*19397407SSherry Moore 		if (ops->devo_quiesce == ddi_quiesce_not_needed)
4076*19397407SSherry Moore 			return;
4077*19397407SSherry Moore 
4078*19397407SSherry Moore 		rc = devi_quiesce(dip);
4079*19397407SSherry Moore 
4080*19397407SSherry Moore 		/* quiesce() should never fail */
4081*19397407SSherry Moore 		ASSERT(rc == DDI_SUCCESS);
4082*19397407SSherry Moore 
4083*19397407SSherry Moore 		if (rc != DDI_SUCCESS && should_quiesce) {
4084*19397407SSherry Moore 
4085*19397407SSherry Moore 			if (arg != NULL)
4086*19397407SSherry Moore 				*((int *)arg) = -1;
4087*19397407SSherry Moore 		}
4088*19397407SSherry Moore 	} else if (should_quiesce && arg != NULL) {
4089*19397407SSherry Moore 		*((int *)arg) = -1;
4090*19397407SSherry Moore 	}
4091*19397407SSherry Moore }
4092*19397407SSherry Moore 
4093*19397407SSherry Moore /*
4094*19397407SSherry Moore  * Traverse the dev info tree in a breadth-first manner so that we quiesce
4095*19397407SSherry Moore  * children first.  All subtrees under the parent of dip will be quiesced.
4096*19397407SSherry Moore  */
4097*19397407SSherry Moore void
4098*19397407SSherry Moore quiesce_devices(dev_info_t *dip, void *arg)
4099*19397407SSherry Moore {
4100*19397407SSherry Moore 	/*
4101*19397407SSherry Moore 	 * if we're reached here, the device tree better not be changing.
4102*19397407SSherry Moore 	 * so either devinfo_freeze better be set or we better be panicing.
4103*19397407SSherry Moore 	 */
4104*19397407SSherry Moore 	ASSERT(devinfo_freeze || panicstr);
4105*19397407SSherry Moore 
4106*19397407SSherry Moore 	for (; dip != NULL; dip = ddi_get_next_sibling(dip)) {
4107*19397407SSherry Moore 		quiesce_devices(ddi_get_child(dip), arg);
4108*19397407SSherry Moore 
4109*19397407SSherry Moore 		quiesce_one_device(dip, arg);
4110*19397407SSherry Moore 	}
4111*19397407SSherry Moore }
4112*19397407SSherry Moore 
4113*19397407SSherry Moore /*
41147c478bd9Sstevel@tonic-gate  * Reset all the pure leaf drivers on the system at halt time
41157c478bd9Sstevel@tonic-gate  */
41167c478bd9Sstevel@tonic-gate static int
41177c478bd9Sstevel@tonic-gate reset_leaf_device(dev_info_t *dip, void *arg)
41187c478bd9Sstevel@tonic-gate {
41197c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(arg))
41207c478bd9Sstevel@tonic-gate 	struct dev_ops *ops;
41217c478bd9Sstevel@tonic-gate 
41227c478bd9Sstevel@tonic-gate 	/* if the device doesn't need to be reset then there's nothing to do */
41237c478bd9Sstevel@tonic-gate 	if (!DEVI_NEED_RESET(dip))
41247c478bd9Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
41257c478bd9Sstevel@tonic-gate 
41267c478bd9Sstevel@tonic-gate 	/*
41277c478bd9Sstevel@tonic-gate 	 * if the device isn't a char/block device or doesn't have a
41287c478bd9Sstevel@tonic-gate 	 * reset entry point then there's nothing to do.
41297c478bd9Sstevel@tonic-gate 	 */
41307c478bd9Sstevel@tonic-gate 	ops = ddi_get_driver(dip);
41317c478bd9Sstevel@tonic-gate 	if ((ops == NULL) || (ops->devo_cb_ops == NULL) ||
41327c478bd9Sstevel@tonic-gate 	    (ops->devo_reset == nodev) || (ops->devo_reset == nulldev) ||
41337c478bd9Sstevel@tonic-gate 	    (ops->devo_reset == NULL))
41347c478bd9Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
41357c478bd9Sstevel@tonic-gate 
41367c478bd9Sstevel@tonic-gate 	if (DEVI_IS_ATTACHING(dip) || DEVI_IS_DETACHING(dip)) {
41377c478bd9Sstevel@tonic-gate 		static char path[MAXPATHLEN];
41387c478bd9Sstevel@tonic-gate 
41397c478bd9Sstevel@tonic-gate 		/*
41407c478bd9Sstevel@tonic-gate 		 * bad news, this device has blocked in it's attach or
41417c478bd9Sstevel@tonic-gate 		 * detach routine, which means it not safe to call it's
41427c478bd9Sstevel@tonic-gate 		 * devo_reset() entry point.
41437c478bd9Sstevel@tonic-gate 		 */
41447c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "unable to reset device: %s",
41457c478bd9Sstevel@tonic-gate 		    ddi_pathname(dip, path));
41467c478bd9Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
41477c478bd9Sstevel@tonic-gate 	}
41487c478bd9Sstevel@tonic-gate 
41497c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_NOTE, "resetting %s%d\n",
41507c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip)));
41517c478bd9Sstevel@tonic-gate 
41527c478bd9Sstevel@tonic-gate 	(void) devi_reset(dip, DDI_RESET_FORCE);
41537c478bd9Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
41547c478bd9Sstevel@tonic-gate }
41557c478bd9Sstevel@tonic-gate 
41567c478bd9Sstevel@tonic-gate void
41577c478bd9Sstevel@tonic-gate reset_leaves(void)
41587c478bd9Sstevel@tonic-gate {
41597c478bd9Sstevel@tonic-gate 	/*
41607c478bd9Sstevel@tonic-gate 	 * if we're reached here, the device tree better not be changing.
41617c478bd9Sstevel@tonic-gate 	 * so either devinfo_freeze better be set or we better be panicing.
41627c478bd9Sstevel@tonic-gate 	 */
41637c478bd9Sstevel@tonic-gate 	ASSERT(devinfo_freeze || panicstr);
41647c478bd9Sstevel@tonic-gate 
41657c478bd9Sstevel@tonic-gate 	(void) walk_devs(top_devinfo, reset_leaf_device, NULL, 0);
41667c478bd9Sstevel@tonic-gate }
41677c478bd9Sstevel@tonic-gate 
4168*19397407SSherry Moore 
41697c478bd9Sstevel@tonic-gate /*
4170*19397407SSherry Moore  * devtree_freeze() must be called before quiesce_devices() and reset_leaves()
4171*19397407SSherry Moore  * during a normal system shutdown.  It attempts to ensure that there are no
4172*19397407SSherry Moore  * outstanding attach or detach operations in progress when quiesce_devices() or
4173*19397407SSherry Moore  * reset_leaves()is invoked.  It must be called before the system becomes
4174*19397407SSherry Moore  * single-threaded because device attach and detach are multi-threaded
4175*19397407SSherry Moore  * operations.  (note that during system shutdown the system doesn't actually
4176*19397407SSherry Moore  * become single-thread since other threads still exist, but the shutdown thread
4177*19397407SSherry Moore  * will disable preemption for itself, raise it's pil, and stop all the other
4178*19397407SSherry Moore  * cpus in the system there by effectively making the system single-threaded.)
41797c478bd9Sstevel@tonic-gate  */
41807c478bd9Sstevel@tonic-gate void
41817c478bd9Sstevel@tonic-gate devtree_freeze(void)
41827c478bd9Sstevel@tonic-gate {
41837c478bd9Sstevel@tonic-gate 	int delayed = 0;
41847c478bd9Sstevel@tonic-gate 
41857c478bd9Sstevel@tonic-gate 	/* if we're panicing then the device tree isn't going to be changing */
41867c478bd9Sstevel@tonic-gate 	if (panicstr)
41877c478bd9Sstevel@tonic-gate 		return;
41887c478bd9Sstevel@tonic-gate 
41897c478bd9Sstevel@tonic-gate 	/* stop all dev_info state changes in the device tree */
41907c478bd9Sstevel@tonic-gate 	devinfo_freeze = gethrtime();
41917c478bd9Sstevel@tonic-gate 
41927c478bd9Sstevel@tonic-gate 	/*
41937c478bd9Sstevel@tonic-gate 	 * if we're not panicing and there are on-going attach or detach
41947c478bd9Sstevel@tonic-gate 	 * operations, wait for up to 3 seconds for them to finish.  This
41957c478bd9Sstevel@tonic-gate 	 * is a randomly chosen interval but this should be ok because:
41967c478bd9Sstevel@tonic-gate 	 * - 3 seconds is very small relative to the deadman timer.
41977c478bd9Sstevel@tonic-gate 	 * - normal attach and detach operations should be very quick.
41987c478bd9Sstevel@tonic-gate 	 * - attach and detach operations are fairly rare.
41997c478bd9Sstevel@tonic-gate 	 */
42007c478bd9Sstevel@tonic-gate 	while (!panicstr && atomic_add_long_nv(&devinfo_attach_detach, 0) &&
42017c478bd9Sstevel@tonic-gate 	    (delayed < 3)) {
42027c478bd9Sstevel@tonic-gate 		delayed += 1;
42037c478bd9Sstevel@tonic-gate 
42047c478bd9Sstevel@tonic-gate 		/* do a sleeping wait for one second */
42057c478bd9Sstevel@tonic-gate 		ASSERT(!servicing_interrupt());
42067c478bd9Sstevel@tonic-gate 		delay(drv_usectohz(MICROSEC));
42077c478bd9Sstevel@tonic-gate 	}
42087c478bd9Sstevel@tonic-gate }
42097c478bd9Sstevel@tonic-gate 
42107c478bd9Sstevel@tonic-gate static int
42117c478bd9Sstevel@tonic-gate bind_dip(dev_info_t *dip, void *arg)
42127c478bd9Sstevel@tonic-gate {
42137c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(arg))
4214f4da9be0Scth 	char	*path;
4215f4da9be0Scth 	major_t	major, pmajor;
4216f4da9be0Scth 
4217f4da9be0Scth 	/*
4218f4da9be0Scth 	 * If the node is currently bound to the wrong driver, try to unbind
4219f4da9be0Scth 	 * so that we can rebind to the correct driver.
4220f4da9be0Scth 	 */
4221f4da9be0Scth 	if (i_ddi_node_state(dip) >= DS_BOUND) {
4222f4da9be0Scth 		major = ddi_compatible_driver_major(dip, NULL);
4223f4da9be0Scth 		if ((DEVI(dip)->devi_major == major) &&
4224f4da9be0Scth 		    (i_ddi_node_state(dip) >= DS_INITIALIZED)) {
4225f4da9be0Scth 			/*
4226f4da9be0Scth 			 * Check for a path-oriented driver alias that
4227f4da9be0Scth 			 * takes precedence over current driver binding.
4228f4da9be0Scth 			 */
4229f4da9be0Scth 			path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4230f4da9be0Scth 			(void) ddi_pathname(dip, path);
4231f4da9be0Scth 			pmajor = ddi_name_to_major(path);
4232a204de77Scth 			if ((pmajor != DDI_MAJOR_T_NONE) &&
4233f4da9be0Scth 			    !(devnamesp[pmajor].dn_flags & DN_DRIVER_REMOVED))
4234f4da9be0Scth 				major = pmajor;
4235f4da9be0Scth 			kmem_free(path, MAXPATHLEN);
4236f4da9be0Scth 		}
4237f4da9be0Scth 
4238f4da9be0Scth 		/* attempt unbind if current driver is incorrect */
4239a204de77Scth 		if ((major != DDI_MAJOR_T_NONE) &&
4240f4da9be0Scth 		    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED) &&
4241f4da9be0Scth 		    (major != DEVI(dip)->devi_major))
4242f4da9be0Scth 			(void) ndi_devi_unbind_driver(dip);
4243f4da9be0Scth 	}
4244f4da9be0Scth 
4245f4da9be0Scth 	/* If unbound, try to bind to a driver */
42467c478bd9Sstevel@tonic-gate 	if (i_ddi_node_state(dip) < DS_BOUND)
42477c478bd9Sstevel@tonic-gate 		(void) ndi_devi_bind_driver(dip, 0);
42487c478bd9Sstevel@tonic-gate 
42497c478bd9Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
42507c478bd9Sstevel@tonic-gate }
42517c478bd9Sstevel@tonic-gate 
42527c478bd9Sstevel@tonic-gate void
42537c478bd9Sstevel@tonic-gate i_ddi_bind_devs(void)
42547c478bd9Sstevel@tonic-gate {
4255f4da9be0Scth 	/* flush devfs so that ndi_devi_unbind_driver will work when possible */
4256f4da9be0Scth 	(void) devfs_clean(top_devinfo, NULL, 0);
4257f4da9be0Scth 
42587c478bd9Sstevel@tonic-gate 	ddi_walk_devs(top_devinfo, bind_dip, (void *)NULL);
42597c478bd9Sstevel@tonic-gate }
42607c478bd9Sstevel@tonic-gate 
42617c478bd9Sstevel@tonic-gate static int
42627c478bd9Sstevel@tonic-gate unbind_children(dev_info_t *dip, void *arg)
42637c478bd9Sstevel@tonic-gate {
42647c478bd9Sstevel@tonic-gate 	int circ;
42657c478bd9Sstevel@tonic-gate 	dev_info_t *cdip;
42667c478bd9Sstevel@tonic-gate 	major_t major = (major_t)(uintptr_t)arg;
42677c478bd9Sstevel@tonic-gate 
42687c478bd9Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
42697c478bd9Sstevel@tonic-gate 	cdip = ddi_get_child(dip);
42707c478bd9Sstevel@tonic-gate 	/*
42717c478bd9Sstevel@tonic-gate 	 * We are called either from rem_drv or update_drv.
42727c478bd9Sstevel@tonic-gate 	 * In both cases, we unbind persistent nodes and destroy
42737c478bd9Sstevel@tonic-gate 	 * .conf nodes. In the case of rem_drv, this will be the
42747c478bd9Sstevel@tonic-gate 	 * final state. In the case of update_drv, i_ddi_bind_devs()
42757c478bd9Sstevel@tonic-gate 	 * will be invoked later to reenumerate (new) driver.conf
42767c478bd9Sstevel@tonic-gate 	 * rebind persistent nodes.
42777c478bd9Sstevel@tonic-gate 	 */
42787c478bd9Sstevel@tonic-gate 	while (cdip) {
42797c478bd9Sstevel@tonic-gate 		dev_info_t *next = ddi_get_next_sibling(cdip);
42807c478bd9Sstevel@tonic-gate 		if ((i_ddi_node_state(cdip) > DS_INITIALIZED) ||
42817c478bd9Sstevel@tonic-gate 		    (ddi_driver_major(cdip) != major)) {
42827c478bd9Sstevel@tonic-gate 			cdip = next;
42837c478bd9Sstevel@tonic-gate 			continue;
42847c478bd9Sstevel@tonic-gate 		}
42857c478bd9Sstevel@tonic-gate 		(void) ndi_devi_unbind_driver(cdip);
42867c478bd9Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(cdip) == 0)
42877c478bd9Sstevel@tonic-gate 			(void) ddi_remove_child(cdip, 0);
42887c478bd9Sstevel@tonic-gate 		cdip = next;
42897c478bd9Sstevel@tonic-gate 	}
42907c478bd9Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
42917c478bd9Sstevel@tonic-gate 
42927c478bd9Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
42937c478bd9Sstevel@tonic-gate }
42947c478bd9Sstevel@tonic-gate 
42957c478bd9Sstevel@tonic-gate void
42967c478bd9Sstevel@tonic-gate i_ddi_unbind_devs(major_t major)
42977c478bd9Sstevel@tonic-gate {
42987c478bd9Sstevel@tonic-gate 	ddi_walk_devs(top_devinfo, unbind_children, (void *)(uintptr_t)major);
42997c478bd9Sstevel@tonic-gate }
43007c478bd9Sstevel@tonic-gate 
43017c478bd9Sstevel@tonic-gate /*
43027c478bd9Sstevel@tonic-gate  * I/O Hotplug control
43037c478bd9Sstevel@tonic-gate  */
43047c478bd9Sstevel@tonic-gate 
43057c478bd9Sstevel@tonic-gate /*
43067c478bd9Sstevel@tonic-gate  * create and attach a dev_info node from a .conf file spec
43077c478bd9Sstevel@tonic-gate  */
43087c478bd9Sstevel@tonic-gate static void
43097c478bd9Sstevel@tonic-gate init_spec_child(dev_info_t *pdip, struct hwc_spec *specp, uint_t flags)
43107c478bd9Sstevel@tonic-gate {
43117c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags))
43127c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
43137c478bd9Sstevel@tonic-gate 	char *node_name;
43147c478bd9Sstevel@tonic-gate 
43157c478bd9Sstevel@tonic-gate 	if (((node_name = specp->hwc_devi_name) == NULL) ||
4316a204de77Scth 	    (ddi_name_to_major(node_name) == DDI_MAJOR_T_NONE)) {
43177c478bd9Sstevel@tonic-gate 		char *tmp = node_name;
43187c478bd9Sstevel@tonic-gate 		if (tmp == NULL)
43197c478bd9Sstevel@tonic-gate 			tmp = "<none>";
43207c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT,
43217c478bd9Sstevel@tonic-gate 		    "init_spec_child: parent=%s, bad spec (%s)\n",
43227c478bd9Sstevel@tonic-gate 		    ddi_node_name(pdip), tmp);
43237c478bd9Sstevel@tonic-gate 		return;
43247c478bd9Sstevel@tonic-gate 	}
43257c478bd9Sstevel@tonic-gate 
4326fa9e4066Sahrens 	dip = i_ddi_alloc_node(pdip, node_name, (pnode_t)DEVI_PSEUDO_NODEID,
43277c478bd9Sstevel@tonic-gate 	    -1, specp->hwc_devi_sys_prop_ptr, KM_SLEEP);
43287c478bd9Sstevel@tonic-gate 
43297c478bd9Sstevel@tonic-gate 	if (dip == NULL)
43307c478bd9Sstevel@tonic-gate 		return;
43317c478bd9Sstevel@tonic-gate 
43327c478bd9Sstevel@tonic-gate 	if (ddi_initchild(pdip, dip) != DDI_SUCCESS)
43337c478bd9Sstevel@tonic-gate 		(void) ddi_remove_child(dip, 0);
43347c478bd9Sstevel@tonic-gate }
43357c478bd9Sstevel@tonic-gate 
43367c478bd9Sstevel@tonic-gate /*
43377c478bd9Sstevel@tonic-gate  * Lookup hwc specs from hash tables and make children from the spec
43387c478bd9Sstevel@tonic-gate  * Because some .conf children are "merge" nodes, we also initialize
43397c478bd9Sstevel@tonic-gate  * .conf children to merge properties onto hardware nodes.
43407c478bd9Sstevel@tonic-gate  *
43417c478bd9Sstevel@tonic-gate  * The pdip must be held busy.
43427c478bd9Sstevel@tonic-gate  */
43437c478bd9Sstevel@tonic-gate int
43447c478bd9Sstevel@tonic-gate i_ndi_make_spec_children(dev_info_t *pdip, uint_t flags)
43457c478bd9Sstevel@tonic-gate {
43467c478bd9Sstevel@tonic-gate 	extern struct hwc_spec *hwc_get_child_spec(dev_info_t *, major_t);
43476a41d557Scth 	int			circ;
43487c478bd9Sstevel@tonic-gate 	struct hwc_spec		*list, *spec;
43497c478bd9Sstevel@tonic-gate 
43506a41d557Scth 	ndi_devi_enter(pdip, &circ);
43516a41d557Scth 	if (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN) {
43526a41d557Scth 		ndi_devi_exit(pdip, circ);
43537c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
43546a41d557Scth 	}
43557c478bd9Sstevel@tonic-gate 
4356a204de77Scth 	list = hwc_get_child_spec(pdip, DDI_MAJOR_T_NONE);
43577c478bd9Sstevel@tonic-gate 	for (spec = list; spec != NULL; spec = spec->hwc_next) {
43587c478bd9Sstevel@tonic-gate 		init_spec_child(pdip, spec, flags);
43597c478bd9Sstevel@tonic-gate 	}
43607c478bd9Sstevel@tonic-gate 	hwc_free_spec_list(list);
43617c478bd9Sstevel@tonic-gate 
43627c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(pdip)->devi_lock);
43637c478bd9Sstevel@tonic-gate 	DEVI(pdip)->devi_flags |= DEVI_MADE_CHILDREN;
43647c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(pdip)->devi_lock);
43656a41d557Scth 	ndi_devi_exit(pdip, circ);
43667c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
43677c478bd9Sstevel@tonic-gate }
43687c478bd9Sstevel@tonic-gate 
43697c478bd9Sstevel@tonic-gate /*
43707c478bd9Sstevel@tonic-gate  * Run initchild on all child nodes such that instance assignment
43717c478bd9Sstevel@tonic-gate  * for multiport network cards are contiguous.
43727c478bd9Sstevel@tonic-gate  *
43737c478bd9Sstevel@tonic-gate  * The pdip must be held busy.
43747c478bd9Sstevel@tonic-gate  */
43757c478bd9Sstevel@tonic-gate static void
43767c478bd9Sstevel@tonic-gate i_ndi_init_hw_children(dev_info_t *pdip, uint_t flags)
43777c478bd9Sstevel@tonic-gate {
43787c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
43797c478bd9Sstevel@tonic-gate 
43807c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
43817c478bd9Sstevel@tonic-gate 
43827c478bd9Sstevel@tonic-gate 	/* contiguous instance assignment */
43837c478bd9Sstevel@tonic-gate 	e_ddi_enter_instance();
43847c478bd9Sstevel@tonic-gate 	dip = ddi_get_child(pdip);
43857c478bd9Sstevel@tonic-gate 	while (dip) {
43867c478bd9Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(dip))
43877c478bd9Sstevel@tonic-gate 			(void) i_ndi_config_node(dip, DS_INITIALIZED, flags);
43887c478bd9Sstevel@tonic-gate 		dip = ddi_get_next_sibling(dip);
43897c478bd9Sstevel@tonic-gate 	}
43907c478bd9Sstevel@tonic-gate 	e_ddi_exit_instance();
43917c478bd9Sstevel@tonic-gate }
43927c478bd9Sstevel@tonic-gate 
43937c478bd9Sstevel@tonic-gate /*
43947c478bd9Sstevel@tonic-gate  * report device status
43957c478bd9Sstevel@tonic-gate  */
43967c478bd9Sstevel@tonic-gate static void
43977c478bd9Sstevel@tonic-gate i_ndi_devi_report_status_change(dev_info_t *dip, char *path)
43987c478bd9Sstevel@tonic-gate {
43997c478bd9Sstevel@tonic-gate 	char *status;
44007c478bd9Sstevel@tonic-gate 
44017c478bd9Sstevel@tonic-gate 	if (!DEVI_NEED_REPORT(dip) ||
44027c478bd9Sstevel@tonic-gate 	    (i_ddi_node_state(dip) < DS_INITIALIZED)) {
44037c478bd9Sstevel@tonic-gate 		return;
44047c478bd9Sstevel@tonic-gate 	}
44057c478bd9Sstevel@tonic-gate 
44067c478bd9Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
44077c478bd9Sstevel@tonic-gate 		status = "offline";
44087c478bd9Sstevel@tonic-gate 	} else if (DEVI_IS_DEVICE_DOWN(dip)) {
44097c478bd9Sstevel@tonic-gate 		status = "down";
44107c478bd9Sstevel@tonic-gate 	} else if (DEVI_IS_BUS_QUIESCED(dip)) {
44117c478bd9Sstevel@tonic-gate 		status = "quiesced";
44127c478bd9Sstevel@tonic-gate 	} else if (DEVI_IS_BUS_DOWN(dip)) {
44137c478bd9Sstevel@tonic-gate 		status = "down";
4414737d277aScth 	} else if (i_ddi_devi_attached(dip)) {
44157c478bd9Sstevel@tonic-gate 		status = "online";
44167c478bd9Sstevel@tonic-gate 	} else {
44177c478bd9Sstevel@tonic-gate 		status = "unknown";
44187c478bd9Sstevel@tonic-gate 	}
44197c478bd9Sstevel@tonic-gate 
44207c478bd9Sstevel@tonic-gate 	if (path == NULL) {
44217c478bd9Sstevel@tonic-gate 		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
44227c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "?%s (%s%d) %s\n",
44237c478bd9Sstevel@tonic-gate 		    ddi_pathname(dip, path), ddi_driver_name(dip),
44247c478bd9Sstevel@tonic-gate 		    ddi_get_instance(dip), status);
44257c478bd9Sstevel@tonic-gate 		kmem_free(path, MAXPATHLEN);
44267c478bd9Sstevel@tonic-gate 	} else {
44277c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "?%s (%s%d) %s\n",
44287c478bd9Sstevel@tonic-gate 		    path, ddi_driver_name(dip),
44297c478bd9Sstevel@tonic-gate 		    ddi_get_instance(dip), status);
44307c478bd9Sstevel@tonic-gate 	}
44317c478bd9Sstevel@tonic-gate 
443216747f41Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
44337c478bd9Sstevel@tonic-gate 	DEVI_REPORT_DONE(dip);
443416747f41Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
44357c478bd9Sstevel@tonic-gate }
44367c478bd9Sstevel@tonic-gate 
44377c478bd9Sstevel@tonic-gate /*
44387c478bd9Sstevel@tonic-gate  * log a notification that a dev_info node has been configured.
44397c478bd9Sstevel@tonic-gate  */
44407c478bd9Sstevel@tonic-gate static int
44417c478bd9Sstevel@tonic-gate i_log_devfs_add_devinfo(dev_info_t *dip, uint_t flags)
44427c478bd9Sstevel@tonic-gate {
44437c478bd9Sstevel@tonic-gate 	int se_err;
44447c478bd9Sstevel@tonic-gate 	char *pathname;
44457c478bd9Sstevel@tonic-gate 	sysevent_t *ev;
44467c478bd9Sstevel@tonic-gate 	sysevent_id_t eid;
44477c478bd9Sstevel@tonic-gate 	sysevent_value_t se_val;
44487c478bd9Sstevel@tonic-gate 	sysevent_attr_list_t *ev_attr_list = NULL;
44497c478bd9Sstevel@tonic-gate 	char *class_name;
44507c478bd9Sstevel@tonic-gate 	int no_transport = 0;
44517c478bd9Sstevel@tonic-gate 
44527c478bd9Sstevel@tonic-gate 	ASSERT(dip);
44537c478bd9Sstevel@tonic-gate 
44547c478bd9Sstevel@tonic-gate 	/*
44557c478bd9Sstevel@tonic-gate 	 * Invalidate the devinfo snapshot cache
44567c478bd9Sstevel@tonic-gate 	 */
44577c478bd9Sstevel@tonic-gate 	i_ddi_di_cache_invalidate(KM_SLEEP);
44587c478bd9Sstevel@tonic-gate 
44597c478bd9Sstevel@tonic-gate 	/* do not generate ESC_DEVFS_DEVI_ADD event during boot */
44607c478bd9Sstevel@tonic-gate 	if (!i_ddi_io_initialized())
44617c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
44627c478bd9Sstevel@tonic-gate 
44637c478bd9Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_ADD, EP_DDI, SE_SLEEP);
44647c478bd9Sstevel@tonic-gate 
44657c478bd9Sstevel@tonic-gate 	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
44667c478bd9Sstevel@tonic-gate 
44677c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(dip, pathname);
44687c478bd9Sstevel@tonic-gate 	ASSERT(strlen(pathname));
44697c478bd9Sstevel@tonic-gate 
44707c478bd9Sstevel@tonic-gate 	se_val.value_type = SE_DATA_TYPE_STRING;
44717c478bd9Sstevel@tonic-gate 	se_val.value.sv_string = pathname;
44727c478bd9Sstevel@tonic-gate 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
44737c478bd9Sstevel@tonic-gate 	    &se_val, SE_SLEEP) != 0) {
44747c478bd9Sstevel@tonic-gate 		goto fail;
44757c478bd9Sstevel@tonic-gate 	}
44767c478bd9Sstevel@tonic-gate 
44777c478bd9Sstevel@tonic-gate 	/* add the device class attribute */
44787c478bd9Sstevel@tonic-gate 	if ((class_name = i_ddi_devi_class(dip)) != NULL) {
44797c478bd9Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_STRING;
44807c478bd9Sstevel@tonic-gate 		se_val.value.sv_string = class_name;
44817c478bd9Sstevel@tonic-gate 
44827c478bd9Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
44837c478bd9Sstevel@tonic-gate 		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
44847c478bd9Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
44857c478bd9Sstevel@tonic-gate 			goto fail;
44867c478bd9Sstevel@tonic-gate 		}
44877c478bd9Sstevel@tonic-gate 	}
44887c478bd9Sstevel@tonic-gate 
44897c478bd9Sstevel@tonic-gate 	/*
44907c478bd9Sstevel@tonic-gate 	 * must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
44917c478bd9Sstevel@tonic-gate 	 * in which case the branch event will be logged by the caller
44927c478bd9Sstevel@tonic-gate 	 * after the entire branch has been configured.
44937c478bd9Sstevel@tonic-gate 	 */
44947c478bd9Sstevel@tonic-gate 	if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
44957c478bd9Sstevel@tonic-gate 		/*
44967c478bd9Sstevel@tonic-gate 		 * Instead of logging a separate branch event just add
44977c478bd9Sstevel@tonic-gate 		 * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
44987c478bd9Sstevel@tonic-gate 		 * generate a EC_DEV_BRANCH event.
44997c478bd9Sstevel@tonic-gate 		 */
45007c478bd9Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_INT32;
45017c478bd9Sstevel@tonic-gate 		se_val.value.sv_int32 = 1;
45027c478bd9Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
45037c478bd9Sstevel@tonic-gate 		    DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
45047c478bd9Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
45057c478bd9Sstevel@tonic-gate 			goto fail;
45067c478bd9Sstevel@tonic-gate 		}
45077c478bd9Sstevel@tonic-gate 	}
45087c478bd9Sstevel@tonic-gate 
45097c478bd9Sstevel@tonic-gate 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
45107c478bd9Sstevel@tonic-gate 		sysevent_free_attr(ev_attr_list);
45117c478bd9Sstevel@tonic-gate 		goto fail;
45127c478bd9Sstevel@tonic-gate 	}
45137c478bd9Sstevel@tonic-gate 
45147c478bd9Sstevel@tonic-gate 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
45157c478bd9Sstevel@tonic-gate 		if (se_err == SE_NO_TRANSPORT)
45167c478bd9Sstevel@tonic-gate 			no_transport = 1;
45177c478bd9Sstevel@tonic-gate 		goto fail;
45187c478bd9Sstevel@tonic-gate 	}
45197c478bd9Sstevel@tonic-gate 
45207c478bd9Sstevel@tonic-gate 	sysevent_free(ev);
45217c478bd9Sstevel@tonic-gate 	kmem_free(pathname, MAXPATHLEN);
45227c478bd9Sstevel@tonic-gate 
45237c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
45247c478bd9Sstevel@tonic-gate 
45257c478bd9Sstevel@tonic-gate fail:
45267c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_ADD event for %s%s",
45277c478bd9Sstevel@tonic-gate 	    pathname, (no_transport) ? " (syseventd not responding)" : "");
45287c478bd9Sstevel@tonic-gate 
45297c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "/dev may not be current for driver %s. "
45307c478bd9Sstevel@tonic-gate 	    "Run devfsadm -i %s",
45317c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_driver_name(dip));
45327c478bd9Sstevel@tonic-gate 
45337c478bd9Sstevel@tonic-gate 	sysevent_free(ev);
45347c478bd9Sstevel@tonic-gate 	kmem_free(pathname, MAXPATHLEN);
45357c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
45367c478bd9Sstevel@tonic-gate }
45377c478bd9Sstevel@tonic-gate 
45387c478bd9Sstevel@tonic-gate /*
45397c478bd9Sstevel@tonic-gate  * log a notification that a dev_info node has been unconfigured.
45407c478bd9Sstevel@tonic-gate  */
45417c478bd9Sstevel@tonic-gate static int
45427c478bd9Sstevel@tonic-gate i_log_devfs_remove_devinfo(char *pathname, char *class_name, char *driver_name,
45437c478bd9Sstevel@tonic-gate     int instance, uint_t flags)
45447c478bd9Sstevel@tonic-gate {
45457c478bd9Sstevel@tonic-gate 	sysevent_t *ev;
45467c478bd9Sstevel@tonic-gate 	sysevent_id_t eid;
45477c478bd9Sstevel@tonic-gate 	sysevent_value_t se_val;
45487c478bd9Sstevel@tonic-gate 	sysevent_attr_list_t *ev_attr_list = NULL;
45497c478bd9Sstevel@tonic-gate 	int se_err;
45507c478bd9Sstevel@tonic-gate 	int no_transport = 0;
45517c478bd9Sstevel@tonic-gate 
45527c478bd9Sstevel@tonic-gate 	i_ddi_di_cache_invalidate(KM_SLEEP);
45537c478bd9Sstevel@tonic-gate 
45547c478bd9Sstevel@tonic-gate 	if (!i_ddi_io_initialized())
45557c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
45567c478bd9Sstevel@tonic-gate 
45577c478bd9Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_REMOVE, EP_DDI, SE_SLEEP);
45587c478bd9Sstevel@tonic-gate 
45597c478bd9Sstevel@tonic-gate 	se_val.value_type = SE_DATA_TYPE_STRING;
45607c478bd9Sstevel@tonic-gate 	se_val.value.sv_string = pathname;
45617c478bd9Sstevel@tonic-gate 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
45627c478bd9Sstevel@tonic-gate 	    &se_val, SE_SLEEP) != 0) {
45637c478bd9Sstevel@tonic-gate 		goto fail;
45647c478bd9Sstevel@tonic-gate 	}
45657c478bd9Sstevel@tonic-gate 
45667c478bd9Sstevel@tonic-gate 	if (class_name) {
45677c478bd9Sstevel@tonic-gate 		/* add the device class, driver name and instance attributes */
45687c478bd9Sstevel@tonic-gate 
45697c478bd9Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_STRING;
45707c478bd9Sstevel@tonic-gate 		se_val.value.sv_string = class_name;
45717c478bd9Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
45727c478bd9Sstevel@tonic-gate 		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
45737c478bd9Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
45747c478bd9Sstevel@tonic-gate 			goto fail;
45757c478bd9Sstevel@tonic-gate 		}
45767c478bd9Sstevel@tonic-gate 
45777c478bd9Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_STRING;
45787c478bd9Sstevel@tonic-gate 		se_val.value.sv_string = driver_name;
45797c478bd9Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
45807c478bd9Sstevel@tonic-gate 		    DEVFS_DRIVER_NAME, &se_val, SE_SLEEP) != 0) {
45817c478bd9Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
45827c478bd9Sstevel@tonic-gate 			goto fail;
45837c478bd9Sstevel@tonic-gate 		}
45847c478bd9Sstevel@tonic-gate 
45857c478bd9Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_INT32;
45867c478bd9Sstevel@tonic-gate 		se_val.value.sv_int32 = instance;
45877c478bd9Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
45887c478bd9Sstevel@tonic-gate 		    DEVFS_INSTANCE, &se_val, SE_SLEEP) != 0) {
45897c478bd9Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
45907c478bd9Sstevel@tonic-gate 			goto fail;
45917c478bd9Sstevel@tonic-gate 		}
45927c478bd9Sstevel@tonic-gate 	}
45937c478bd9Sstevel@tonic-gate 
45947c478bd9Sstevel@tonic-gate 	/*
45957c478bd9Sstevel@tonic-gate 	 * must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
45967c478bd9Sstevel@tonic-gate 	 * in which case the branch event will be logged by the caller
45977c478bd9Sstevel@tonic-gate 	 * after the entire branch has been unconfigured.
45987c478bd9Sstevel@tonic-gate 	 */
45997c478bd9Sstevel@tonic-gate 	if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
46007c478bd9Sstevel@tonic-gate 		/*
46017c478bd9Sstevel@tonic-gate 		 * Instead of logging a separate branch event just add
46027c478bd9Sstevel@tonic-gate 		 * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
46037c478bd9Sstevel@tonic-gate 		 * generate a EC_DEV_BRANCH event.
46047c478bd9Sstevel@tonic-gate 		 */
46057c478bd9Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_INT32;
46067c478bd9Sstevel@tonic-gate 		se_val.value.sv_int32 = 1;
46077c478bd9Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
46087c478bd9Sstevel@tonic-gate 		    DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
46097c478bd9Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
46107c478bd9Sstevel@tonic-gate 			goto fail;
46117c478bd9Sstevel@tonic-gate 		}
46127c478bd9Sstevel@tonic-gate 	}
46137c478bd9Sstevel@tonic-gate 
46147c478bd9Sstevel@tonic-gate 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
46157c478bd9Sstevel@tonic-gate 		sysevent_free_attr(ev_attr_list);
46167c478bd9Sstevel@tonic-gate 		goto fail;
46177c478bd9Sstevel@tonic-gate 	}
46187c478bd9Sstevel@tonic-gate 
46197c478bd9Sstevel@tonic-gate 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
46207c478bd9Sstevel@tonic-gate 		if (se_err == SE_NO_TRANSPORT)
46217c478bd9Sstevel@tonic-gate 			no_transport = 1;
46227c478bd9Sstevel@tonic-gate 		goto fail;
46237c478bd9Sstevel@tonic-gate 	}
46247c478bd9Sstevel@tonic-gate 
46257c478bd9Sstevel@tonic-gate 	sysevent_free(ev);
46267c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
46277c478bd9Sstevel@tonic-gate 
46287c478bd9Sstevel@tonic-gate fail:
46297c478bd9Sstevel@tonic-gate 	sysevent_free(ev);
46307c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_REMOVE event for %s%s",
46317c478bd9Sstevel@tonic-gate 	    pathname, (no_transport) ? " (syseventd not responding)" : "");
46327c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
46337c478bd9Sstevel@tonic-gate }
46347c478bd9Sstevel@tonic-gate 
46357c478bd9Sstevel@tonic-gate /*
46367c478bd9Sstevel@tonic-gate  * log an event that a dev_info branch has been configured or unconfigured.
46377c478bd9Sstevel@tonic-gate  */
46387c478bd9Sstevel@tonic-gate static int
46397c478bd9Sstevel@tonic-gate i_log_devfs_branch(char *node_path, char *subclass)
46407c478bd9Sstevel@tonic-gate {
46417c478bd9Sstevel@tonic-gate 	int se_err;
46427c478bd9Sstevel@tonic-gate 	sysevent_t *ev;
46437c478bd9Sstevel@tonic-gate 	sysevent_id_t eid;
46447c478bd9Sstevel@tonic-gate 	sysevent_value_t se_val;
46457c478bd9Sstevel@tonic-gate 	sysevent_attr_list_t *ev_attr_list = NULL;
46467c478bd9Sstevel@tonic-gate 	int no_transport = 0;
46477c478bd9Sstevel@tonic-gate 
46487c478bd9Sstevel@tonic-gate 	/* do not generate the event during boot */
46497c478bd9Sstevel@tonic-gate 	if (!i_ddi_io_initialized())
46507c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
46517c478bd9Sstevel@tonic-gate 
46527c478bd9Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, subclass, EP_DDI, SE_SLEEP);
46537c478bd9Sstevel@tonic-gate 
46547c478bd9Sstevel@tonic-gate 	se_val.value_type = SE_DATA_TYPE_STRING;
46557c478bd9Sstevel@tonic-gate 	se_val.value.sv_string = node_path;
46567c478bd9Sstevel@tonic-gate 
46577c478bd9Sstevel@tonic-gate 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
46587c478bd9Sstevel@tonic-gate 	    &se_val, SE_SLEEP) != 0) {
46597c478bd9Sstevel@tonic-gate 		goto fail;
46607c478bd9Sstevel@tonic-gate 	}
46617c478bd9Sstevel@tonic-gate 
46627c478bd9Sstevel@tonic-gate 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
46637c478bd9Sstevel@tonic-gate 		sysevent_free_attr(ev_attr_list);
46647c478bd9Sstevel@tonic-gate 		goto fail;
46657c478bd9Sstevel@tonic-gate 	}
46667c478bd9Sstevel@tonic-gate 
46677c478bd9Sstevel@tonic-gate 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
46687c478bd9Sstevel@tonic-gate 		if (se_err == SE_NO_TRANSPORT)
46697c478bd9Sstevel@tonic-gate 			no_transport = 1;
46707c478bd9Sstevel@tonic-gate 		goto fail;
46717c478bd9Sstevel@tonic-gate 	}
46727c478bd9Sstevel@tonic-gate 
46737c478bd9Sstevel@tonic-gate 	sysevent_free(ev);
46747c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
46757c478bd9Sstevel@tonic-gate 
46767c478bd9Sstevel@tonic-gate fail:
46777c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "failed to log %s branch event for %s%s",
46787c478bd9Sstevel@tonic-gate 	    subclass, node_path,
46797c478bd9Sstevel@tonic-gate 	    (no_transport) ? " (syseventd not responding)" : "");
46807c478bd9Sstevel@tonic-gate 
46817c478bd9Sstevel@tonic-gate 	sysevent_free(ev);
46827c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
46837c478bd9Sstevel@tonic-gate }
46847c478bd9Sstevel@tonic-gate 
46857c478bd9Sstevel@tonic-gate /*
46867c478bd9Sstevel@tonic-gate  * log an event that a dev_info tree branch has been configured.
46877c478bd9Sstevel@tonic-gate  */
46887c478bd9Sstevel@tonic-gate static int
46897c478bd9Sstevel@tonic-gate i_log_devfs_branch_add(dev_info_t *dip)
46907c478bd9Sstevel@tonic-gate {
46917c478bd9Sstevel@tonic-gate 	char *node_path;
46927c478bd9Sstevel@tonic-gate 	int rv;
46937c478bd9Sstevel@tonic-gate 
46947c478bd9Sstevel@tonic-gate 	node_path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
46957c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(dip, node_path);
46967c478bd9Sstevel@tonic-gate 	rv = i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_ADD);
46977c478bd9Sstevel@tonic-gate 	kmem_free(node_path, MAXPATHLEN);
46987c478bd9Sstevel@tonic-gate 
46997c478bd9Sstevel@tonic-gate 	return (rv);
47007c478bd9Sstevel@tonic-gate }
47017c478bd9Sstevel@tonic-gate 
47027c478bd9Sstevel@tonic-gate /*
47037c478bd9Sstevel@tonic-gate  * log an event that a dev_info tree branch has been unconfigured.
47047c478bd9Sstevel@tonic-gate  */
47057c478bd9Sstevel@tonic-gate static int
47067c478bd9Sstevel@tonic-gate i_log_devfs_branch_remove(char *node_path)
47077c478bd9Sstevel@tonic-gate {
47087c478bd9Sstevel@tonic-gate 	return (i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_REMOVE));
47097c478bd9Sstevel@tonic-gate }
47107c478bd9Sstevel@tonic-gate 
47117c478bd9Sstevel@tonic-gate /*
47127c478bd9Sstevel@tonic-gate  * enqueue the dip's deviname on the branch event queue.
47137c478bd9Sstevel@tonic-gate  */
47147c478bd9Sstevel@tonic-gate static struct brevq_node *
47157c478bd9Sstevel@tonic-gate brevq_enqueue(struct brevq_node **brevqp, dev_info_t *dip,
47167c478bd9Sstevel@tonic-gate     struct brevq_node *child)
47177c478bd9Sstevel@tonic-gate {
47187c478bd9Sstevel@tonic-gate 	struct brevq_node *brn;
47197c478bd9Sstevel@tonic-gate 	char *deviname;
47207c478bd9Sstevel@tonic-gate 
47217c478bd9Sstevel@tonic-gate 	deviname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
47227c478bd9Sstevel@tonic-gate 	(void) ddi_deviname(dip, deviname);
47237c478bd9Sstevel@tonic-gate 
47247c478bd9Sstevel@tonic-gate 	brn = kmem_zalloc(sizeof (*brn), KM_SLEEP);
4725245c82d9Scth 	brn->brn_deviname = i_ddi_strdup(deviname, KM_SLEEP);
47267c478bd9Sstevel@tonic-gate 	kmem_free(deviname, MAXNAMELEN);
4727245c82d9Scth 	brn->brn_child = child;
4728245c82d9Scth 	brn->brn_sibling = *brevqp;
47297c478bd9Sstevel@tonic-gate 	*brevqp = brn;
47307c478bd9Sstevel@tonic-gate 
47317c478bd9Sstevel@tonic-gate 	return (brn);
47327c478bd9Sstevel@tonic-gate }
47337c478bd9Sstevel@tonic-gate 
47347c478bd9Sstevel@tonic-gate /*
47357c478bd9Sstevel@tonic-gate  * free the memory allocated for the elements on the branch event queue.
47367c478bd9Sstevel@tonic-gate  */
47377c478bd9Sstevel@tonic-gate static void
47387c478bd9Sstevel@tonic-gate free_brevq(struct brevq_node *brevq)
47397c478bd9Sstevel@tonic-gate {
47407c478bd9Sstevel@tonic-gate 	struct brevq_node *brn, *next_brn;
47417c478bd9Sstevel@tonic-gate 
47427c478bd9Sstevel@tonic-gate 	for (brn = brevq; brn != NULL; brn = next_brn) {
4743245c82d9Scth 		next_brn = brn->brn_sibling;
4744245c82d9Scth 		ASSERT(brn->brn_child == NULL);
4745245c82d9Scth 		kmem_free(brn->brn_deviname, strlen(brn->brn_deviname) + 1);
47467c478bd9Sstevel@tonic-gate 		kmem_free(brn, sizeof (*brn));
47477c478bd9Sstevel@tonic-gate 	}
47487c478bd9Sstevel@tonic-gate }
47497c478bd9Sstevel@tonic-gate 
47507c478bd9Sstevel@tonic-gate /*
47517c478bd9Sstevel@tonic-gate  * log the events queued up on the branch event queue and free the
47527c478bd9Sstevel@tonic-gate  * associated memory.
47537c478bd9Sstevel@tonic-gate  *
47547c478bd9Sstevel@tonic-gate  * node_path must have been allocated with at least MAXPATHLEN bytes.
47557c478bd9Sstevel@tonic-gate  */
47567c478bd9Sstevel@tonic-gate static void
47577c478bd9Sstevel@tonic-gate log_and_free_brevq(char *node_path, struct brevq_node *brevq)
47587c478bd9Sstevel@tonic-gate {
47597c478bd9Sstevel@tonic-gate 	struct brevq_node *brn;
47607c478bd9Sstevel@tonic-gate 	char *p;
47617c478bd9Sstevel@tonic-gate 
47627c478bd9Sstevel@tonic-gate 	p = node_path + strlen(node_path);
4763245c82d9Scth 	for (brn = brevq; brn != NULL; brn = brn->brn_sibling) {
4764245c82d9Scth 		(void) strcpy(p, brn->brn_deviname);
47657c478bd9Sstevel@tonic-gate 		(void) i_log_devfs_branch_remove(node_path);
47667c478bd9Sstevel@tonic-gate 	}
47677c478bd9Sstevel@tonic-gate 	*p = '\0';
47687c478bd9Sstevel@tonic-gate 
47697c478bd9Sstevel@tonic-gate 	free_brevq(brevq);
47707c478bd9Sstevel@tonic-gate }
47717c478bd9Sstevel@tonic-gate 
47727c478bd9Sstevel@tonic-gate /*
47737c478bd9Sstevel@tonic-gate  * log the events queued up on the branch event queue and free the
47747c478bd9Sstevel@tonic-gate  * associated memory. Same as the previous function but operates on dip.
47757c478bd9Sstevel@tonic-gate  */
47767c478bd9Sstevel@tonic-gate static void
47777c478bd9Sstevel@tonic-gate log_and_free_brevq_dip(dev_info_t *dip, struct brevq_node *brevq)
47787c478bd9Sstevel@tonic-gate {
47797c478bd9Sstevel@tonic-gate 	char *path;
47807c478bd9Sstevel@tonic-gate 
47817c478bd9Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
47827c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(dip, path);
47837c478bd9Sstevel@tonic-gate 	log_and_free_brevq(path, brevq);
47847c478bd9Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
47857c478bd9Sstevel@tonic-gate }
47867c478bd9Sstevel@tonic-gate 
47877c478bd9Sstevel@tonic-gate /*
47887c478bd9Sstevel@tonic-gate  * log the outstanding branch remove events for the grand children of the dip
47897c478bd9Sstevel@tonic-gate  * and free the associated memory.
47907c478bd9Sstevel@tonic-gate  */
47917c478bd9Sstevel@tonic-gate static void
47927c478bd9Sstevel@tonic-gate log_and_free_br_events_on_grand_children(dev_info_t *dip,
47937c478bd9Sstevel@tonic-gate     struct brevq_node *brevq)
47947c478bd9Sstevel@tonic-gate {
47957c478bd9Sstevel@tonic-gate 	struct brevq_node *brn;
47967c478bd9Sstevel@tonic-gate 	char *path;
47977c478bd9Sstevel@tonic-gate 	char *p;
47987c478bd9Sstevel@tonic-gate 
47997c478bd9Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
48007c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(dip, path);
48017c478bd9Sstevel@tonic-gate 	p = path + strlen(path);
4802245c82d9Scth 	for (brn = brevq; brn != NULL; brn = brn->brn_sibling) {
4803245c82d9Scth 		if (brn->brn_child) {
4804245c82d9Scth 			(void) strcpy(p, brn->brn_deviname);
48057c478bd9Sstevel@tonic-gate 			/* now path contains the node path to the dip's child */
4806245c82d9Scth 			log_and_free_brevq(path, brn->brn_child);
4807245c82d9Scth 			brn->brn_child = NULL;
48087c478bd9Sstevel@tonic-gate 		}
48097c478bd9Sstevel@tonic-gate 	}
48107c478bd9Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
48117c478bd9Sstevel@tonic-gate }
48127c478bd9Sstevel@tonic-gate 
48137c478bd9Sstevel@tonic-gate /*
48147c478bd9Sstevel@tonic-gate  * log and cleanup branch remove events for the grand children of the dip.
48157c478bd9Sstevel@tonic-gate  */
48167c478bd9Sstevel@tonic-gate static void
48177c478bd9Sstevel@tonic-gate cleanup_br_events_on_grand_children(dev_info_t *dip, struct brevq_node **brevqp)
48187c478bd9Sstevel@tonic-gate {
48197c478bd9Sstevel@tonic-gate 	dev_info_t *child;
48207c478bd9Sstevel@tonic-gate 	struct brevq_node *brevq, *brn, *prev_brn, *next_brn;
48217c478bd9Sstevel@tonic-gate 	char *path;
48227c478bd9Sstevel@tonic-gate 	int circ;
48237c478bd9Sstevel@tonic-gate 
48247c478bd9Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
48257c478bd9Sstevel@tonic-gate 	prev_brn = NULL;
48267c478bd9Sstevel@tonic-gate 	brevq = *brevqp;
48277c478bd9Sstevel@tonic-gate 
48287c478bd9Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
48297c478bd9Sstevel@tonic-gate 	for (brn = brevq; brn != NULL; brn = next_brn) {
4830245c82d9Scth 		next_brn = brn->brn_sibling;
48317c478bd9Sstevel@tonic-gate 		for (child = ddi_get_child(dip); child != NULL;
48327c478bd9Sstevel@tonic-gate 		    child = ddi_get_next_sibling(child)) {
48337c478bd9Sstevel@tonic-gate 			if (i_ddi_node_state(child) >= DS_INITIALIZED) {
48347c478bd9Sstevel@tonic-gate 				(void) ddi_deviname(child, path);
4835245c82d9Scth 				if (strcmp(path, brn->brn_deviname) == 0)
48367c478bd9Sstevel@tonic-gate 					break;
48377c478bd9Sstevel@tonic-gate 			}
48387c478bd9Sstevel@tonic-gate 		}
48397c478bd9Sstevel@tonic-gate 
48407c478bd9Sstevel@tonic-gate 		if (child != NULL && !(DEVI_EVREMOVE(child))) {
48417c478bd9Sstevel@tonic-gate 			/*
48427c478bd9Sstevel@tonic-gate 			 * Event state is not REMOVE. So branch remove event
4843245c82d9Scth 			 * is not going be generated on brn->brn_child.
48447c478bd9Sstevel@tonic-gate 			 * If any branch remove events were queued up on
4845245c82d9Scth 			 * brn->brn_child log them and remove the brn
48467c478bd9Sstevel@tonic-gate 			 * from the queue.
48477c478bd9Sstevel@tonic-gate 			 */
4848245c82d9Scth 			if (brn->brn_child) {
48497c478bd9Sstevel@tonic-gate 				(void) ddi_pathname(dip, path);
4850245c82d9Scth 				(void) strcat(path, brn->brn_deviname);
4851245c82d9Scth 				log_and_free_brevq(path, brn->brn_child);
48527c478bd9Sstevel@tonic-gate 			}
48537c478bd9Sstevel@tonic-gate 
48547c478bd9Sstevel@tonic-gate 			if (prev_brn)
4855245c82d9Scth 				prev_brn->brn_sibling = next_brn;
48567c478bd9Sstevel@tonic-gate 			else
48577c478bd9Sstevel@tonic-gate 				*brevqp = next_brn;
48587c478bd9Sstevel@tonic-gate 
4859245c82d9Scth 			kmem_free(brn->brn_deviname,
4860245c82d9Scth 			    strlen(brn->brn_deviname) + 1);
48617c478bd9Sstevel@tonic-gate 			kmem_free(brn, sizeof (*brn));
48627c478bd9Sstevel@tonic-gate 		} else {
48637c478bd9Sstevel@tonic-gate 			/*
48647c478bd9Sstevel@tonic-gate 			 * Free up the outstanding branch remove events
4865245c82d9Scth 			 * queued on brn->brn_child since brn->brn_child
48667c478bd9Sstevel@tonic-gate 			 * itself is eligible for branch remove event.
48677c478bd9Sstevel@tonic-gate 			 */
4868245c82d9Scth 			if (brn->brn_child) {
4869245c82d9Scth 				free_brevq(brn->brn_child);
4870245c82d9Scth 				brn->brn_child = NULL;
48717c478bd9Sstevel@tonic-gate 			}
48727c478bd9Sstevel@tonic-gate 			prev_brn = brn;
48737c478bd9Sstevel@tonic-gate 		}
48747c478bd9Sstevel@tonic-gate 	}
48757c478bd9Sstevel@tonic-gate 
48767c478bd9Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
48777c478bd9Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
48787c478bd9Sstevel@tonic-gate }
48797c478bd9Sstevel@tonic-gate 
48807c478bd9Sstevel@tonic-gate static int
48817c478bd9Sstevel@tonic-gate need_remove_event(dev_info_t *dip, int flags)
48827c478bd9Sstevel@tonic-gate {
48837c478bd9Sstevel@tonic-gate 	if ((flags & (NDI_NO_EVENT | NDI_AUTODETACH)) == 0 &&
48847c478bd9Sstevel@tonic-gate 	    (flags & (NDI_DEVI_OFFLINE | NDI_UNCONFIG | NDI_DEVI_REMOVE)) &&
48857c478bd9Sstevel@tonic-gate 	    !(DEVI_EVREMOVE(dip)))
48867c478bd9Sstevel@tonic-gate 		return (1);
48877c478bd9Sstevel@tonic-gate 	else
48887c478bd9Sstevel@tonic-gate 		return (0);
48897c478bd9Sstevel@tonic-gate }
48907c478bd9Sstevel@tonic-gate 
48917c478bd9Sstevel@tonic-gate /*
48927c478bd9Sstevel@tonic-gate  * Unconfigure children/descendants of the dip.
48937c478bd9Sstevel@tonic-gate  *
48947c478bd9Sstevel@tonic-gate  * If the operation involves a branch event NDI_BRANCH_EVENT_OP is set
48957c478bd9Sstevel@tonic-gate  * through out the unconfiguration. On successful return *brevqp is set to
48967c478bd9Sstevel@tonic-gate  * a queue of dip's child devinames for which branch remove events need
48977c478bd9Sstevel@tonic-gate  * to be generated.
48987c478bd9Sstevel@tonic-gate  */
48997c478bd9Sstevel@tonic-gate static int
49007c478bd9Sstevel@tonic-gate devi_unconfig_branch(dev_info_t *dip, dev_info_t **dipp, int flags,
49017c478bd9Sstevel@tonic-gate     struct brevq_node **brevqp)
49027c478bd9Sstevel@tonic-gate {
49037c478bd9Sstevel@tonic-gate 	int rval;
49047c478bd9Sstevel@tonic-gate 
49057c478bd9Sstevel@tonic-gate 	*brevqp = NULL;
49067c478bd9Sstevel@tonic-gate 
49077c478bd9Sstevel@tonic-gate 	if ((!(flags & NDI_BRANCH_EVENT_OP)) && need_remove_event(dip, flags))
49087c478bd9Sstevel@tonic-gate 		flags |= NDI_BRANCH_EVENT_OP;
49097c478bd9Sstevel@tonic-gate 
49107c478bd9Sstevel@tonic-gate 	if (flags & NDI_BRANCH_EVENT_OP) {
4911a204de77Scth 		rval = devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE,
49127c478bd9Sstevel@tonic-gate 		    brevqp);
49137c478bd9Sstevel@tonic-gate 
49147c478bd9Sstevel@tonic-gate 		if (rval != NDI_SUCCESS && (*brevqp)) {
49157c478bd9Sstevel@tonic-gate 			log_and_free_brevq_dip(dip, *brevqp);
49167c478bd9Sstevel@tonic-gate 			*brevqp = NULL;
49177c478bd9Sstevel@tonic-gate 		}
49187c478bd9Sstevel@tonic-gate 	} else
4919a204de77Scth 		rval = devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE,
49207c478bd9Sstevel@tonic-gate 		    NULL);
49217c478bd9Sstevel@tonic-gate 
49227c478bd9Sstevel@tonic-gate 	return (rval);
49237c478bd9Sstevel@tonic-gate }
49247c478bd9Sstevel@tonic-gate 
49257c478bd9Sstevel@tonic-gate /*
49267c478bd9Sstevel@tonic-gate  * If the dip is already bound to a driver transition to DS_INITIALIZED
49277c478bd9Sstevel@tonic-gate  * in order to generate an event in the case where the node was left in
49287c478bd9Sstevel@tonic-gate  * DS_BOUND state since boot (never got attached) and the node is now
49297c478bd9Sstevel@tonic-gate  * being offlined.
49307c478bd9Sstevel@tonic-gate  */
49317c478bd9Sstevel@tonic-gate static void
49327c478bd9Sstevel@tonic-gate init_bound_node_ev(dev_info_t *pdip, dev_info_t *dip, int flags)
49337c478bd9Sstevel@tonic-gate {
49347c478bd9Sstevel@tonic-gate 	if (need_remove_event(dip, flags) &&
49357c478bd9Sstevel@tonic-gate 	    i_ddi_node_state(dip) == DS_BOUND &&
4936737d277aScth 	    i_ddi_devi_attached(pdip) && !DEVI_IS_DEVICE_OFFLINE(dip))
49377c478bd9Sstevel@tonic-gate 		(void) ddi_initchild(pdip, dip);
49387c478bd9Sstevel@tonic-gate }
49397c478bd9Sstevel@tonic-gate 
49407c478bd9Sstevel@tonic-gate /*
49417c478bd9Sstevel@tonic-gate  * attach a node/branch with parent already held busy
49427c478bd9Sstevel@tonic-gate  */
49437c478bd9Sstevel@tonic-gate static int
49447c478bd9Sstevel@tonic-gate devi_attach_node(dev_info_t *dip, uint_t flags)
49457c478bd9Sstevel@tonic-gate {
49465e3986cbScth 	dev_info_t *pdip = ddi_get_parent(dip);
49475e3986cbScth 
49485e3986cbScth 	ASSERT(pdip && DEVI_BUSY_OWNED(pdip));
49495e3986cbScth 
495016747f41Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
49517c478bd9Sstevel@tonic-gate 	if (flags & NDI_DEVI_ONLINE) {
4952737d277aScth 		if (!i_ddi_devi_attached(dip))
495316747f41Scth 			DEVI_SET_REPORT(dip);
49547c478bd9Sstevel@tonic-gate 		DEVI_SET_DEVICE_ONLINE(dip);
49557c478bd9Sstevel@tonic-gate 	}
49567c478bd9Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
495716747f41Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
49587c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
49597c478bd9Sstevel@tonic-gate 	}
496016747f41Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
49617c478bd9Sstevel@tonic-gate 
49627c478bd9Sstevel@tonic-gate 	if (i_ddi_attachchild(dip) != DDI_SUCCESS) {
496316747f41Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
49647c478bd9Sstevel@tonic-gate 		DEVI_SET_EVUNINIT(dip);
496516747f41Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
496616747f41Scth 
49677c478bd9Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(dip))
49687c478bd9Sstevel@tonic-gate 			(void) ddi_uninitchild(dip);
49697c478bd9Sstevel@tonic-gate 		else {
49707c478bd9Sstevel@tonic-gate 			/*
49717c478bd9Sstevel@tonic-gate 			 * Delete .conf nodes and nodes that are not
49727c478bd9Sstevel@tonic-gate 			 * well formed.
49737c478bd9Sstevel@tonic-gate 			 */
49747c478bd9Sstevel@tonic-gate 			(void) ddi_remove_child(dip, 0);
49757c478bd9Sstevel@tonic-gate 		}
49767c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
49777c478bd9Sstevel@tonic-gate 	}
49787c478bd9Sstevel@tonic-gate 
49797c478bd9Sstevel@tonic-gate 	i_ndi_devi_report_status_change(dip, NULL);
49807c478bd9Sstevel@tonic-gate 
49817c478bd9Sstevel@tonic-gate 	/*
49827c478bd9Sstevel@tonic-gate 	 * log an event, but not during devfs lookups in which case
49837c478bd9Sstevel@tonic-gate 	 * NDI_NO_EVENT is set.
49847c478bd9Sstevel@tonic-gate 	 */
49857c478bd9Sstevel@tonic-gate 	if ((flags & NDI_NO_EVENT) == 0 && !(DEVI_EVADD(dip))) {
49867c478bd9Sstevel@tonic-gate 		(void) i_log_devfs_add_devinfo(dip, flags);
498716747f41Scth 
498816747f41Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
49897c478bd9Sstevel@tonic-gate 		DEVI_SET_EVADD(dip);
499016747f41Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
499116747f41Scth 	} else if (!(flags & NDI_NO_EVENT_STATE_CHNG)) {
499216747f41Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
49937c478bd9Sstevel@tonic-gate 		DEVI_SET_EVADD(dip);
499416747f41Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
499516747f41Scth 	}
49967c478bd9Sstevel@tonic-gate 
49977c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
49987c478bd9Sstevel@tonic-gate }
49997c478bd9Sstevel@tonic-gate 
50007c478bd9Sstevel@tonic-gate /* internal function to config immediate children */
50017c478bd9Sstevel@tonic-gate static int
50027c478bd9Sstevel@tonic-gate config_immediate_children(dev_info_t *pdip, uint_t flags, major_t major)
50037c478bd9Sstevel@tonic-gate {
50045e3986cbScth 	dev_info_t	*child, *next;
50057c478bd9Sstevel@tonic-gate 	int		circ;
50065e3986cbScth 
5007737d277aScth 	ASSERT(i_ddi_devi_attached(pdip));
50087c478bd9Sstevel@tonic-gate 
50097c478bd9Sstevel@tonic-gate 	if (!NEXUS_DRV(ddi_get_driver(pdip)))
50107c478bd9Sstevel@tonic-gate 		return (NDI_SUCCESS);
50117c478bd9Sstevel@tonic-gate 
50127c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
50137c478bd9Sstevel@tonic-gate 	    "config_immediate_children: %s%d (%p), flags=%x\n",
50147c478bd9Sstevel@tonic-gate 	    ddi_driver_name(pdip), ddi_get_instance(pdip),
50157c478bd9Sstevel@tonic-gate 	    (void *)pdip, flags));
50167c478bd9Sstevel@tonic-gate 
50177c478bd9Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
50187c478bd9Sstevel@tonic-gate 
50197c478bd9Sstevel@tonic-gate 	if (flags & NDI_CONFIG_REPROBE) {
50207c478bd9Sstevel@tonic-gate 		mutex_enter(&DEVI(pdip)->devi_lock);
50217c478bd9Sstevel@tonic-gate 		DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
50227c478bd9Sstevel@tonic-gate 		mutex_exit(&DEVI(pdip)->devi_lock);
50237c478bd9Sstevel@tonic-gate 	}
50247c478bd9Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(pdip, flags);
50257c478bd9Sstevel@tonic-gate 	i_ndi_init_hw_children(pdip, flags);
50265e3986cbScth 
50275e3986cbScth 	child = ddi_get_child(pdip);
50285e3986cbScth 	while (child) {
50295e3986cbScth 		/* NOTE: devi_attach_node() may remove the dip */
50305e3986cbScth 		next = ddi_get_next_sibling(child);
50315e3986cbScth 
50325e3986cbScth 		/*
50335e3986cbScth 		 * Configure all nexus nodes or leaf nodes with
50345e3986cbScth 		 * matching driver major
50355e3986cbScth 		 */
5036a204de77Scth 		if ((major == DDI_MAJOR_T_NONE) ||
50375e3986cbScth 		    (major == ddi_driver_major(child)) ||
50385e3986cbScth 		    ((flags & NDI_CONFIG) && (is_leaf_node(child) == 0)))
50395e3986cbScth 			(void) devi_attach_node(child, flags);
50405e3986cbScth 		child = next;
50415e3986cbScth 	}
50427c478bd9Sstevel@tonic-gate 
50437c478bd9Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
50447c478bd9Sstevel@tonic-gate 
50457c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
50467c478bd9Sstevel@tonic-gate }
50477c478bd9Sstevel@tonic-gate 
50487c478bd9Sstevel@tonic-gate /* internal function to config grand children */
50497c478bd9Sstevel@tonic-gate static int
50507c478bd9Sstevel@tonic-gate config_grand_children(dev_info_t *pdip, uint_t flags, major_t major)
50517c478bd9Sstevel@tonic-gate {
50527c478bd9Sstevel@tonic-gate 	struct mt_config_handle *hdl;
50537c478bd9Sstevel@tonic-gate 
50547c478bd9Sstevel@tonic-gate 	/* multi-threaded configuration of child nexus */
50557c478bd9Sstevel@tonic-gate 	hdl = mt_config_init(pdip, NULL, flags, major, MT_CONFIG_OP, NULL);
50567c478bd9Sstevel@tonic-gate 	mt_config_children(hdl);
50577c478bd9Sstevel@tonic-gate 
50587c478bd9Sstevel@tonic-gate 	return (mt_config_fini(hdl));	/* wait for threads to exit */
50597c478bd9Sstevel@tonic-gate }
50607c478bd9Sstevel@tonic-gate 
50617c478bd9Sstevel@tonic-gate /*
50627c478bd9Sstevel@tonic-gate  * Common function for device tree configuration,
50637c478bd9Sstevel@tonic-gate  * either BUS_CONFIG_ALL or BUS_CONFIG_DRIVER.
50647c478bd9Sstevel@tonic-gate  * The NDI_CONFIG flag causes recursive configuration of
50657c478bd9Sstevel@tonic-gate  * grandchildren, devfs usage should not recurse.
50667c478bd9Sstevel@tonic-gate  */
50677c478bd9Sstevel@tonic-gate static int
50687c478bd9Sstevel@tonic-gate devi_config_common(dev_info_t *dip, int flags, major_t major)
50697c478bd9Sstevel@tonic-gate {
50707c478bd9Sstevel@tonic-gate 	int error;
50717c478bd9Sstevel@tonic-gate 	int (*f)();
50727c478bd9Sstevel@tonic-gate 
5073737d277aScth 	if (!i_ddi_devi_attached(dip))
50747c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
50757c478bd9Sstevel@tonic-gate 
50767c478bd9Sstevel@tonic-gate 	if (pm_pre_config(dip, NULL) != DDI_SUCCESS)
50777c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
50787c478bd9Sstevel@tonic-gate 
50797c478bd9Sstevel@tonic-gate 	if ((DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
50807c478bd9Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
50817c478bd9Sstevel@tonic-gate 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
50827c478bd9Sstevel@tonic-gate 		error = config_immediate_children(dip, flags, major);
50837c478bd9Sstevel@tonic-gate 	} else {
50847c478bd9Sstevel@tonic-gate 		/* call bus_config entry point */
5085a204de77Scth 		ddi_bus_config_op_t bus_op = (major == DDI_MAJOR_T_NONE) ?
50867c478bd9Sstevel@tonic-gate 		    BUS_CONFIG_ALL : BUS_CONFIG_DRIVER;
50877c478bd9Sstevel@tonic-gate 		error = (*f)(dip,
50887c478bd9Sstevel@tonic-gate 		    flags, bus_op, (void *)(uintptr_t)major, NULL, 0);
50897c478bd9Sstevel@tonic-gate 	}
50907c478bd9Sstevel@tonic-gate 
50917c478bd9Sstevel@tonic-gate 	if (error) {
50927c478bd9Sstevel@tonic-gate 		pm_post_config(dip, NULL);
50937c478bd9Sstevel@tonic-gate 		return (error);
50947c478bd9Sstevel@tonic-gate 	}
50957c478bd9Sstevel@tonic-gate 
50967c478bd9Sstevel@tonic-gate 	/*
50977c478bd9Sstevel@tonic-gate 	 * Some callers, notably SCSI, need to mark the devfs cache
50987c478bd9Sstevel@tonic-gate 	 * to be rebuilt together with the config operation.
50997c478bd9Sstevel@tonic-gate 	 */
51007c478bd9Sstevel@tonic-gate 	if (flags & NDI_DEVFS_CLEAN)
51017c478bd9Sstevel@tonic-gate 		(void) devfs_clean(dip, NULL, 0);
51027c478bd9Sstevel@tonic-gate 
51037c478bd9Sstevel@tonic-gate 	if (flags & NDI_CONFIG)
51047c478bd9Sstevel@tonic-gate 		(void) config_grand_children(dip, flags, major);
51057c478bd9Sstevel@tonic-gate 
51067c478bd9Sstevel@tonic-gate 	pm_post_config(dip, NULL);
51077c478bd9Sstevel@tonic-gate 
51087c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
51097c478bd9Sstevel@tonic-gate }
51107c478bd9Sstevel@tonic-gate 
51117c478bd9Sstevel@tonic-gate /*
51127c478bd9Sstevel@tonic-gate  * Framework entry point for BUS_CONFIG_ALL
51137c478bd9Sstevel@tonic-gate  */
51147c478bd9Sstevel@tonic-gate int
51157c478bd9Sstevel@tonic-gate ndi_devi_config(dev_info_t *dip, int flags)
51167c478bd9Sstevel@tonic-gate {
51177c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
51187c478bd9Sstevel@tonic-gate 	    "ndi_devi_config: par = %s%d (%p), flags = 0x%x\n",
51197c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
51207c478bd9Sstevel@tonic-gate 
5121a204de77Scth 	return (devi_config_common(dip, flags, DDI_MAJOR_T_NONE));
51227c478bd9Sstevel@tonic-gate }
51237c478bd9Sstevel@tonic-gate 
51247c478bd9Sstevel@tonic-gate /*
51257c478bd9Sstevel@tonic-gate  * Framework entry point for BUS_CONFIG_DRIVER, bound to major
51267c478bd9Sstevel@tonic-gate  */
51277c478bd9Sstevel@tonic-gate int
51287c478bd9Sstevel@tonic-gate ndi_devi_config_driver(dev_info_t *dip, int flags, major_t major)
51297c478bd9Sstevel@tonic-gate {
51307c478bd9Sstevel@tonic-gate 	/* don't abuse this function */
5131a204de77Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
51327c478bd9Sstevel@tonic-gate 
51337c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
51347c478bd9Sstevel@tonic-gate 	    "ndi_devi_config_driver: par = %s%d (%p), flags = 0x%x\n",
51357c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
51367c478bd9Sstevel@tonic-gate 
51377c478bd9Sstevel@tonic-gate 	return (devi_config_common(dip, flags, major));
51387c478bd9Sstevel@tonic-gate }
51397c478bd9Sstevel@tonic-gate 
51407c478bd9Sstevel@tonic-gate /*
51415e3986cbScth  * Called by nexus drivers to configure its children.
51427c478bd9Sstevel@tonic-gate  */
51437c478bd9Sstevel@tonic-gate static int
51445e3986cbScth devi_config_one(dev_info_t *pdip, char *devnm, dev_info_t **cdipp,
51457c478bd9Sstevel@tonic-gate     uint_t flags, clock_t timeout)
51467c478bd9Sstevel@tonic-gate {
51475e3986cbScth 	dev_info_t	*vdip = NULL;
51485e3986cbScth 	char		*drivername = NULL;
5149f4da9be0Scth 	int		find_by_addr = 0;
51505e3986cbScth 	char		*name, *addr;
51515e3986cbScth 	int		v_circ, p_circ;
51527c478bd9Sstevel@tonic-gate 	clock_t		end_time;	/* 60 sec */
51535e3986cbScth 	int		probed;
51545e3986cbScth 	dev_info_t	*cdip;
51555e3986cbScth 	mdi_pathinfo_t	*cpip;
51565e3986cbScth 
51575e3986cbScth 	*cdipp = NULL;
51587c478bd9Sstevel@tonic-gate 
51597c478bd9Sstevel@tonic-gate 	if (!NEXUS_DRV(ddi_get_driver(pdip)))
51607c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
51617c478bd9Sstevel@tonic-gate 
51627c478bd9Sstevel@tonic-gate 	/* split name into "name@addr" parts */
51637c478bd9Sstevel@tonic-gate 	i_ddi_parse_name(devnm, &name, &addr, NULL);
51647c478bd9Sstevel@tonic-gate 
51657c478bd9Sstevel@tonic-gate 	/*
51665e3986cbScth 	 * If the nexus is a pHCI and we are not processing a pHCI from
51675e3986cbScth 	 * mdi bus_config code then we need to know the vHCI.
51687c478bd9Sstevel@tonic-gate 	 */
51695e3986cbScth 	if (MDI_PHCI(pdip))
51705e3986cbScth 		vdip = mdi_devi_get_vdip(pdip);
51715e3986cbScth 
51725e3986cbScth 	/*
51735e3986cbScth 	 * We may have a genericname on a system that creates drivername
51745e3986cbScth 	 * nodes (from .conf files).  Find the drivername by nodeid. If we
51755e3986cbScth 	 * can't find a node with devnm as the node name then we search by
51765e3986cbScth 	 * drivername.  This allows an implementation to supply a genericly
51774c96bfe1Scth 	 * named boot path (disk) and locate drivename nodes (sd).  The
51784c96bfe1Scth 	 * NDI_PROMNAME flag does not apply to /devices/pseudo paths.
51795e3986cbScth 	 */
51804c96bfe1Scth 	if ((flags & NDI_PROMNAME) && (pdip != pseudo_dip)) {
51817c478bd9Sstevel@tonic-gate 		drivername = child_path_to_driver(pdip, name, addr);
5182f4da9be0Scth 		find_by_addr = 1;
5183f4da9be0Scth 	}
51847c478bd9Sstevel@tonic-gate 
5185144dfaa9Scth 	/*
51865e3986cbScth 	 * Determine end_time: This routine should *not* be called with a
51875e3986cbScth 	 * constant non-zero timeout argument, the caller should be adjusting
51885e3986cbScth 	 * the timeout argument relative to when it *started* its asynchronous
51895e3986cbScth 	 * enumeration.
5190144dfaa9Scth 	 */
51915e3986cbScth 	if (timeout > 0)
51925e3986cbScth 		end_time = ddi_get_lbolt() + timeout;
5193c73a93f2Sdm120769 
51945e3986cbScth 	for (;;) {
51955e3986cbScth 		/*
51965e3986cbScth 		 * For pHCI, enter (vHCI, pHCI) and search for pathinfo/client
51975e3986cbScth 		 * child - break out of for(;;) loop if child found.
51985e3986cbScth 		 * NOTE: Lock order for ndi_devi_enter is (vHCI, pHCI).
51995e3986cbScth 		 */
52005e3986cbScth 		if (vdip) {
52015e3986cbScth 			/* use mdi_devi_enter ordering */
52025e3986cbScth 			ndi_devi_enter(vdip, &v_circ);
52035e3986cbScth 			ndi_devi_enter(pdip, &p_circ);
52045e3986cbScth 			cpip = mdi_pi_find(pdip, NULL, addr);
52055e3986cbScth 			cdip = mdi_pi_get_client(cpip);
52065e3986cbScth 			if (cdip)
52075e3986cbScth 				break;
52085e3986cbScth 		} else
52095e3986cbScth 			ndi_devi_enter(pdip, &p_circ);
52105e3986cbScth 
52115e3986cbScth 		/*
52125e3986cbScth 		 * When not a  vHCI or not all pHCI devices are required to
52135e3986cbScth 		 * enumerated under the vHCI (NDI_MDI_FALLBACK) search for
52145e3986cbScth 		 * devinfo child.
52155e3986cbScth 		 */
52165e3986cbScth 		if ((vdip == NULL) || (flags & NDI_MDI_FALLBACK)) {
52175e3986cbScth 			/* determine if .conf nodes already built */
52185e3986cbScth 			probed = (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
52195e3986cbScth 
52205e3986cbScth 			/*
52215e3986cbScth 			 * Search for child by name, if not found then search
52225e3986cbScth 			 * for a node bound to the drivername driver with the
52235e3986cbScth 			 * specified "@addr". Break out of for(;;) loop if
5224f4da9be0Scth 			 * child found.  To support path-oriented aliases
5225f4da9be0Scth 			 * binding on boot-device, we do a search_by_addr too.
52265e3986cbScth 			 */
52275e3986cbScth again:			(void) i_ndi_make_spec_children(pdip, flags);
52285e3986cbScth 			cdip = find_child_by_name(pdip, name, addr);
52295e3986cbScth 			if ((cdip == NULL) && drivername)
52305e3986cbScth 				cdip = find_child_by_driver(pdip,
52315e3986cbScth 				    drivername, addr);
5232f4da9be0Scth 			if ((cdip == NULL) && find_by_addr)
5233f4da9be0Scth 				cdip = find_child_by_addr(pdip, addr);
52345e3986cbScth 			if (cdip)
52357c478bd9Sstevel@tonic-gate 				break;
52367c478bd9Sstevel@tonic-gate 
52377c478bd9Sstevel@tonic-gate 			/*
52385e3986cbScth 			 * determine if we should reenumerate .conf nodes
52395e3986cbScth 			 * and look for child again.
52407c478bd9Sstevel@tonic-gate 			 */
52415e3986cbScth 			if (probed &&
52425e3986cbScth 			    i_ddi_io_initialized() &&
52435e3986cbScth 			    (flags & NDI_CONFIG_REPROBE) &&
52445e3986cbScth 			    ((timeout <= 0) || (ddi_get_lbolt() >= end_time))) {
52455e3986cbScth 				probed = 0;
52465e3986cbScth 				mutex_enter(&DEVI(pdip)->devi_lock);
52475e3986cbScth 				DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
52485e3986cbScth 				mutex_exit(&DEVI(pdip)->devi_lock);
52495e3986cbScth 				goto again;
52505e3986cbScth 			}
52515e3986cbScth 		}
52525e3986cbScth 
52535e3986cbScth 		/* break out of for(;;) if time expired */
52545e3986cbScth 		if ((timeout <= 0) || (ddi_get_lbolt() >= end_time))
52555e3986cbScth 			break;
52565e3986cbScth 
52575e3986cbScth 		/*
52585e3986cbScth 		 * Child not found, exit and wait for asynchronous enumeration
52595e3986cbScth 		 * to add child (or timeout). The addition of a new child (vhci
52605e3986cbScth 		 * or phci) requires the asynchronous enumeration thread to
52615e3986cbScth 		 * ndi_devi_enter/ndi_devi_exit. This exit will signal devi_cv
52625e3986cbScth 		 * and cause us to return from ndi_devi_exit_and_wait, after
52635e3986cbScth 		 * which we loop and search for the requested child again.
52645e3986cbScth 		 */
52657c478bd9Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT,
52667c478bd9Sstevel@tonic-gate 		    "%s%d: waiting for child %s@%s, timeout %ld",
52677c478bd9Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
52687c478bd9Sstevel@tonic-gate 		    name, addr, timeout));
52695e3986cbScth 		if (vdip) {
5270c73a93f2Sdm120769 			/*
52715e3986cbScth 			 * Mark vHCI for pHCI ndi_devi_exit broadcast.
5272c73a93f2Sdm120769 			 */
52735e3986cbScth 			mutex_enter(&DEVI(vdip)->devi_lock);
52745e3986cbScth 			DEVI(vdip)->devi_flags |=
52755e3986cbScth 			    DEVI_PHCI_SIGNALS_VHCI;
52765e3986cbScth 			mutex_exit(&DEVI(vdip)->devi_lock);
52775e3986cbScth 			ndi_devi_exit(pdip, p_circ);
52785e3986cbScth 
52795e3986cbScth 			/*
52805e3986cbScth 			 * NB: There is a small race window from above
52815e3986cbScth 			 * ndi_devi_exit() of pdip to cv_wait() in
52825e3986cbScth 			 * ndi_devi_exit_and_wait() which can result in
52835e3986cbScth 			 * not immediately finding a new pHCI child
52845e3986cbScth 			 * of a pHCI that uses NDI_MDI_FAILBACK.
52855e3986cbScth 			 */
52865e3986cbScth 			ndi_devi_exit_and_wait(vdip, v_circ, end_time);
52875e3986cbScth 		} else {
52885e3986cbScth 			ndi_devi_exit_and_wait(pdip, p_circ, end_time);
52895e3986cbScth 		}
5290c73a93f2Sdm120769 	}
5291c73a93f2Sdm120769 
52925e3986cbScth 	/* done with paddr, fixup i_ddi_parse_name '@'->'\0' change */
52935e3986cbScth 	if (addr && *addr != '\0')
52947c478bd9Sstevel@tonic-gate 		*(addr - 1) = '@';
52957c478bd9Sstevel@tonic-gate 
52965e3986cbScth 	/* attach and hold the child, returning pointer to child */
52975e3986cbScth 	if (cdip && (devi_attach_node(cdip, flags) == NDI_SUCCESS)) {
52985e3986cbScth 		ndi_hold_devi(cdip);
52995e3986cbScth 		*cdipp = cdip;
53007c478bd9Sstevel@tonic-gate 	}
53017c478bd9Sstevel@tonic-gate 
53025e3986cbScth 	ndi_devi_exit(pdip, p_circ);
53035e3986cbScth 	if (vdip)
53045e3986cbScth 		ndi_devi_exit(vdip, v_circ);
53055e3986cbScth 	return (*cdipp ? NDI_SUCCESS : NDI_FAILURE);
53067c478bd9Sstevel@tonic-gate }
53077c478bd9Sstevel@tonic-gate 
53087c478bd9Sstevel@tonic-gate /*
53097c478bd9Sstevel@tonic-gate  * Enumerate and attach a child specified by name 'devnm'.
53107c478bd9Sstevel@tonic-gate  * Called by devfs lookup and DR to perform a BUS_CONFIG_ONE.
53117c478bd9Sstevel@tonic-gate  * Note: devfs does not make use of NDI_CONFIG to configure
53127c478bd9Sstevel@tonic-gate  * an entire branch.
53137c478bd9Sstevel@tonic-gate  */
53147c478bd9Sstevel@tonic-gate int
53157c478bd9Sstevel@tonic-gate ndi_devi_config_one(dev_info_t *dip, char *devnm, dev_info_t **dipp, int flags)
53167c478bd9Sstevel@tonic-gate {
53177c478bd9Sstevel@tonic-gate 	int error;
53187c478bd9Sstevel@tonic-gate 	int (*f)();
53197c478bd9Sstevel@tonic-gate 	int branch_event = 0;
53207c478bd9Sstevel@tonic-gate 
53217c478bd9Sstevel@tonic-gate 	ASSERT(dipp);
5322737d277aScth 	ASSERT(i_ddi_devi_attached(dip));
53237c478bd9Sstevel@tonic-gate 
53247c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
53257c478bd9Sstevel@tonic-gate 	    "ndi_devi_config_one: par = %s%d (%p), child = %s\n",
53267c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, devnm));
53277c478bd9Sstevel@tonic-gate 
53287c478bd9Sstevel@tonic-gate 	if (pm_pre_config(dip, devnm) != DDI_SUCCESS)
53297c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
53307c478bd9Sstevel@tonic-gate 
53317c478bd9Sstevel@tonic-gate 	if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
53327c478bd9Sstevel@tonic-gate 	    (flags & NDI_CONFIG)) {
53337c478bd9Sstevel@tonic-gate 		flags |= NDI_BRANCH_EVENT_OP;
53347c478bd9Sstevel@tonic-gate 		branch_event = 1;
53357c478bd9Sstevel@tonic-gate 	}
53367c478bd9Sstevel@tonic-gate 
53377c478bd9Sstevel@tonic-gate 	if ((DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
53387c478bd9Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
53397c478bd9Sstevel@tonic-gate 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
53407c478bd9Sstevel@tonic-gate 		error = devi_config_one(dip, devnm, dipp, flags, 0);
53417c478bd9Sstevel@tonic-gate 	} else {
53427c478bd9Sstevel@tonic-gate 		/* call bus_config entry point */
53437c478bd9Sstevel@tonic-gate 		error = (*f)(dip, flags, BUS_CONFIG_ONE, (void *)devnm, dipp);
53447c478bd9Sstevel@tonic-gate 	}
53457c478bd9Sstevel@tonic-gate 
53467c478bd9Sstevel@tonic-gate 	if (error || (flags & NDI_CONFIG) == 0) {
53477c478bd9Sstevel@tonic-gate 		pm_post_config(dip, devnm);
53487c478bd9Sstevel@tonic-gate 		return (error);
53497c478bd9Sstevel@tonic-gate 	}
53507c478bd9Sstevel@tonic-gate 
53517c478bd9Sstevel@tonic-gate 	/*
5352f4da9be0Scth 	 * DR usage (i.e. call with NDI_CONFIG) recursively configures
53537c478bd9Sstevel@tonic-gate 	 * grandchildren, performing a BUS_CONFIG_ALL from the node attached
53547c478bd9Sstevel@tonic-gate 	 * by the BUS_CONFIG_ONE.
53557c478bd9Sstevel@tonic-gate 	 */
53567c478bd9Sstevel@tonic-gate 	ASSERT(*dipp);
53577c478bd9Sstevel@tonic-gate 
5358a204de77Scth 	error = devi_config_common(*dipp, flags, DDI_MAJOR_T_NONE);
53597c478bd9Sstevel@tonic-gate 
53607c478bd9Sstevel@tonic-gate 	pm_post_config(dip, devnm);
53617c478bd9Sstevel@tonic-gate 
53627c478bd9Sstevel@tonic-gate 	if (branch_event)
53637c478bd9Sstevel@tonic-gate 		(void) i_log_devfs_branch_add(*dipp);
53647c478bd9Sstevel@tonic-gate 
53657c478bd9Sstevel@tonic-gate 	return (error);
53667c478bd9Sstevel@tonic-gate }
53677c478bd9Sstevel@tonic-gate 
53687c478bd9Sstevel@tonic-gate 
53697c478bd9Sstevel@tonic-gate /*
53707c478bd9Sstevel@tonic-gate  * Enumerate and attach a child specified by name 'devnm'.
53717c478bd9Sstevel@tonic-gate  * Called during configure the OBP options. This configures
53727c478bd9Sstevel@tonic-gate  * only one node.
53737c478bd9Sstevel@tonic-gate  */
53747c478bd9Sstevel@tonic-gate static int
53757c478bd9Sstevel@tonic-gate ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
53767c478bd9Sstevel@tonic-gate     dev_info_t **childp, int flags)
53777c478bd9Sstevel@tonic-gate {
53787c478bd9Sstevel@tonic-gate 	int error;
53797c478bd9Sstevel@tonic-gate 	int (*f)();
53807c478bd9Sstevel@tonic-gate 
53817c478bd9Sstevel@tonic-gate 	ASSERT(childp);
5382737d277aScth 	ASSERT(i_ddi_devi_attached(parent));
53837c478bd9Sstevel@tonic-gate 
53847c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_config_obp_args: "
53857c478bd9Sstevel@tonic-gate 	    "par = %s%d (%p), child = %s\n", ddi_driver_name(parent),
53867c478bd9Sstevel@tonic-gate 	    ddi_get_instance(parent), (void *)parent, devnm));
53877c478bd9Sstevel@tonic-gate 
53887c478bd9Sstevel@tonic-gate 	if ((DEVI(parent)->devi_ops->devo_bus_ops == NULL) ||
53897c478bd9Sstevel@tonic-gate 	    (DEVI(parent)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
53907c478bd9Sstevel@tonic-gate 	    (f = DEVI(parent)->devi_ops->devo_bus_ops->bus_config) == NULL) {
53917c478bd9Sstevel@tonic-gate 		error = NDI_FAILURE;
53927c478bd9Sstevel@tonic-gate 	} else {
53937c478bd9Sstevel@tonic-gate 		/* call bus_config entry point */
53947c478bd9Sstevel@tonic-gate 		error = (*f)(parent, flags,
53957c478bd9Sstevel@tonic-gate 		    BUS_CONFIG_OBP_ARGS, (void *)devnm, childp);
53967c478bd9Sstevel@tonic-gate 	}
53977c478bd9Sstevel@tonic-gate 	return (error);
53987c478bd9Sstevel@tonic-gate }
53997c478bd9Sstevel@tonic-gate 
540025e8c5aaSvikram /*
540125e8c5aaSvikram  * Pay attention, the following is a bit tricky:
540225e8c5aaSvikram  * There are three possible cases when constraints are applied
540325e8c5aaSvikram  *
540425e8c5aaSvikram  *	- A constraint is applied and the offline is disallowed.
540525e8c5aaSvikram  *	  Simply return failure and block the offline
540625e8c5aaSvikram  *
540725e8c5aaSvikram  *	- A constraint is applied and the offline is allowed.
540825e8c5aaSvikram  *	  Mark the dip as having passed the constraint and allow
540925e8c5aaSvikram  *	  offline to proceed.
541025e8c5aaSvikram  *
541125e8c5aaSvikram  *	- A constraint is not applied. Allow the offline to proceed for now.
541225e8c5aaSvikram  *
541325e8c5aaSvikram  * In the latter two cases we allow the offline to proceed. If the
541425e8c5aaSvikram  * offline succeeds (no users) everything is fine. It is ok for an unused
541525e8c5aaSvikram  * device to be offlined even if no constraints were imposed on the offline.
541625e8c5aaSvikram  * If the offline fails because there are users, we look at the constraint
541725e8c5aaSvikram  * flag on the dip. If the constraint flag is set (implying that it passed
541825e8c5aaSvikram  * a constraint) we allow the dip to be retired. If not, we don't allow
541925e8c5aaSvikram  * the retire. This ensures that we don't allow unconstrained retire.
542025e8c5aaSvikram  */
542125e8c5aaSvikram int
542225e8c5aaSvikram e_ddi_offline_notify(dev_info_t *dip)
542325e8c5aaSvikram {
542425e8c5aaSvikram 	int retval;
542525e8c5aaSvikram 	int constraint;
542625e8c5aaSvikram 	int failure;
542725e8c5aaSvikram 
542825e8c5aaSvikram 	RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): entered: dip=%p",
542925e8c5aaSvikram 	    (void *) dip));
543025e8c5aaSvikram 
543125e8c5aaSvikram 	constraint = 0;
543225e8c5aaSvikram 	failure = 0;
543325e8c5aaSvikram 
543425e8c5aaSvikram 	/*
543525e8c5aaSvikram 	 * Start with userland constraints first - applied via device contracts
543625e8c5aaSvikram 	 */
543725e8c5aaSvikram 	retval = contract_device_offline(dip, DDI_DEV_T_ANY, 0);
543825e8c5aaSvikram 	switch (retval) {
543925e8c5aaSvikram 	case CT_NACK:
544025e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "Received NACK for dip=%p", (void *)dip));
544125e8c5aaSvikram 		failure = 1;
544225e8c5aaSvikram 		goto out;
544325e8c5aaSvikram 	case CT_ACK:
544425e8c5aaSvikram 		constraint = 1;
544525e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "Received ACK for dip=%p", (void *)dip));
544625e8c5aaSvikram 		break;
544725e8c5aaSvikram 	case CT_NONE:
544825e8c5aaSvikram 		/* no contracts */
544925e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "No contracts on dip=%p", (void *)dip));
545025e8c5aaSvikram 		break;
545125e8c5aaSvikram 	default:
545225e8c5aaSvikram 		ASSERT(retval == CT_NONE);
545325e8c5aaSvikram 	}
545425e8c5aaSvikram 
545525e8c5aaSvikram 	/*
545625e8c5aaSvikram 	 * Next, use LDI to impose kernel constraints
545725e8c5aaSvikram 	 */
545825e8c5aaSvikram 	retval = ldi_invoke_notify(dip, DDI_DEV_T_ANY, 0, LDI_EV_OFFLINE, NULL);
545925e8c5aaSvikram 	switch (retval) {
546025e8c5aaSvikram 	case LDI_EV_FAILURE:
546125e8c5aaSvikram 		contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_FAILURE);
546225e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "LDI callback failed on dip=%p",
546325e8c5aaSvikram 		    (void *)dip));
546425e8c5aaSvikram 		failure = 1;
546525e8c5aaSvikram 		goto out;
546625e8c5aaSvikram 	case LDI_EV_SUCCESS:
546725e8c5aaSvikram 		constraint = 1;
546825e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "LDI callback success on dip=%p",
546925e8c5aaSvikram 		    (void *)dip));
547025e8c5aaSvikram 		break;
547125e8c5aaSvikram 	case LDI_EV_NONE:
547225e8c5aaSvikram 		/* no matching LDI callbacks */
547325e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "No LDI callbacks for dip=%p",
547425e8c5aaSvikram 		    (void *)dip));
547525e8c5aaSvikram 		break;
547625e8c5aaSvikram 	default:
547725e8c5aaSvikram 		ASSERT(retval == LDI_EV_NONE);
547825e8c5aaSvikram 	}
547925e8c5aaSvikram 
548025e8c5aaSvikram out:
548125e8c5aaSvikram 	mutex_enter(&(DEVI(dip)->devi_lock));
548225e8c5aaSvikram 	if ((DEVI(dip)->devi_flags & DEVI_RETIRING) && failure) {
548325e8c5aaSvikram 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): setting "
548425e8c5aaSvikram 		    "BLOCKED flag. dip=%p", (void *)dip));
548525e8c5aaSvikram 		DEVI(dip)->devi_flags |= DEVI_R_BLOCKED;
548625e8c5aaSvikram 		if (DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT) {
548725e8c5aaSvikram 			RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): "
548825e8c5aaSvikram 			    "blocked. clearing RCM CONSTRAINT flag. dip=%p",
548925e8c5aaSvikram 			    (void *)dip));
549025e8c5aaSvikram 			DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
549125e8c5aaSvikram 		}
549225e8c5aaSvikram 	} else if ((DEVI(dip)->devi_flags & DEVI_RETIRING) && constraint) {
549325e8c5aaSvikram 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): setting "
549425e8c5aaSvikram 		    "CONSTRAINT flag. dip=%p", (void *)dip));
549525e8c5aaSvikram 		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
549625e8c5aaSvikram 	} else if ((DEVI(dip)->devi_flags & DEVI_RETIRING) &&
549725e8c5aaSvikram 	    DEVI(dip)->devi_ref == 0) {
549825e8c5aaSvikram 		/* also allow retire if device is not in use */
549925e8c5aaSvikram 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): device not in "
550025e8c5aaSvikram 		    "use. Setting CONSTRAINT flag. dip=%p", (void *)dip));
550125e8c5aaSvikram 		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
550225e8c5aaSvikram 	} else {
550325e8c5aaSvikram 		/*
550425e8c5aaSvikram 		 * Note: We cannot ASSERT here that DEVI_R_CONSTRAINT is
550525e8c5aaSvikram 		 * not set, since other sources (such as RCM) may have
550625e8c5aaSvikram 		 * set the flag.
550725e8c5aaSvikram 		 */
550825e8c5aaSvikram 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): not setting "
550925e8c5aaSvikram 		    "constraint flag. dip=%p", (void *)dip));
551025e8c5aaSvikram 	}
551125e8c5aaSvikram 	mutex_exit(&(DEVI(dip)->devi_lock));
551225e8c5aaSvikram 
551325e8c5aaSvikram 
551425e8c5aaSvikram 	RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): exit: dip=%p",
551525e8c5aaSvikram 	    (void *) dip));
551625e8c5aaSvikram 
551725e8c5aaSvikram 	return (failure ? DDI_FAILURE : DDI_SUCCESS);
551825e8c5aaSvikram }
551925e8c5aaSvikram 
552025e8c5aaSvikram void
552125e8c5aaSvikram e_ddi_offline_finalize(dev_info_t *dip, int result)
552225e8c5aaSvikram {
552325e8c5aaSvikram 	RIO_DEBUG((CE_NOTE, "e_ddi_offline_finalize(): entry: result=%s, "
552425e8c5aaSvikram 	    "dip=%p", result == DDI_SUCCESS ? "SUCCESS" : "FAILURE",
552525e8c5aaSvikram 	    (void *)dip));
552625e8c5aaSvikram 
552725e8c5aaSvikram 	contract_device_negend(dip, DDI_DEV_T_ANY, 0,  result == DDI_SUCCESS ?
552825e8c5aaSvikram 	    CT_EV_SUCCESS : CT_EV_FAILURE);
552925e8c5aaSvikram 
553025e8c5aaSvikram 	ldi_invoke_finalize(dip, DDI_DEV_T_ANY, 0,
553125e8c5aaSvikram 	    LDI_EV_OFFLINE, result == DDI_SUCCESS ?
553225e8c5aaSvikram 	    LDI_EV_SUCCESS : LDI_EV_FAILURE, NULL);
553325e8c5aaSvikram 
553425e8c5aaSvikram 	RIO_VERBOSE((CE_NOTE, "e_ddi_offline_finalize(): exit: dip=%p",
553525e8c5aaSvikram 	    (void *)dip));
553625e8c5aaSvikram }
553725e8c5aaSvikram 
553825e8c5aaSvikram void
553925e8c5aaSvikram e_ddi_degrade_finalize(dev_info_t *dip)
554025e8c5aaSvikram {
554125e8c5aaSvikram 	RIO_DEBUG((CE_NOTE, "e_ddi_degrade_finalize(): entry: "
554225e8c5aaSvikram 	    "result always = DDI_SUCCESS, dip=%p", (void *)dip));
554325e8c5aaSvikram 
554425e8c5aaSvikram 	contract_device_degrade(dip, DDI_DEV_T_ANY, 0);
554525e8c5aaSvikram 	contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_SUCCESS);
554625e8c5aaSvikram 
554725e8c5aaSvikram 	ldi_invoke_finalize(dip, DDI_DEV_T_ANY, 0, LDI_EV_DEGRADE,
554825e8c5aaSvikram 	    LDI_EV_SUCCESS, NULL);
554925e8c5aaSvikram 
555025e8c5aaSvikram 	RIO_VERBOSE((CE_NOTE, "e_ddi_degrade_finalize(): exit: dip=%p",
555125e8c5aaSvikram 	    (void *)dip));
555225e8c5aaSvikram }
555325e8c5aaSvikram 
555425e8c5aaSvikram void
555525e8c5aaSvikram e_ddi_undegrade_finalize(dev_info_t *dip)
555625e8c5aaSvikram {
555725e8c5aaSvikram 	RIO_DEBUG((CE_NOTE, "e_ddi_undegrade_finalize(): entry: "
555825e8c5aaSvikram 	    "result always = DDI_SUCCESS, dip=%p", (void *)dip));
555925e8c5aaSvikram 
556025e8c5aaSvikram 	contract_device_undegrade(dip, DDI_DEV_T_ANY, 0);
556125e8c5aaSvikram 	contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_SUCCESS);
556225e8c5aaSvikram 
556325e8c5aaSvikram 	RIO_VERBOSE((CE_NOTE, "e_ddi_undegrade_finalize(): exit: dip=%p",
556425e8c5aaSvikram 	    (void *)dip));
556525e8c5aaSvikram }
55667c478bd9Sstevel@tonic-gate 
55677c478bd9Sstevel@tonic-gate /*
55687c478bd9Sstevel@tonic-gate  * detach a node with parent already held busy
55697c478bd9Sstevel@tonic-gate  */
55707c478bd9Sstevel@tonic-gate static int
55717c478bd9Sstevel@tonic-gate devi_detach_node(dev_info_t *dip, uint_t flags)
55727c478bd9Sstevel@tonic-gate {
55737c478bd9Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
55747c478bd9Sstevel@tonic-gate 	int ret = NDI_SUCCESS;
55757c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t cookie;
55767c478bd9Sstevel@tonic-gate 
55775e3986cbScth 	ASSERT(pdip && DEVI_BUSY_OWNED(pdip));
55785e3986cbScth 
557925e8c5aaSvikram 	/*
558025e8c5aaSvikram 	 * Invoke notify if offlining
558125e8c5aaSvikram 	 */
558225e8c5aaSvikram 	if (flags & NDI_DEVI_OFFLINE) {
558325e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "devi_detach_node: offlining dip=%p",
558425e8c5aaSvikram 		    (void *)dip));
558525e8c5aaSvikram 		if (e_ddi_offline_notify(dip) != DDI_SUCCESS) {
558625e8c5aaSvikram 			RIO_DEBUG((CE_NOTE, "devi_detach_node: offline NACKed"
558725e8c5aaSvikram 			    "dip=%p", (void *)dip));
558825e8c5aaSvikram 			return (NDI_FAILURE);
558925e8c5aaSvikram 		}
559025e8c5aaSvikram 	}
559125e8c5aaSvikram 
55927c478bd9Sstevel@tonic-gate 	if (flags & NDI_POST_EVENT) {
55935e3986cbScth 		if (i_ddi_devi_attached(pdip)) {
55947c478bd9Sstevel@tonic-gate 			if (ddi_get_eventcookie(dip, DDI_DEVI_REMOVE_EVENT,
55957c478bd9Sstevel@tonic-gate 			    &cookie) == NDI_SUCCESS)
55967c478bd9Sstevel@tonic-gate 				(void) ndi_post_event(dip, dip, cookie, NULL);
55977c478bd9Sstevel@tonic-gate 		}
55987c478bd9Sstevel@tonic-gate 	}
55997c478bd9Sstevel@tonic-gate 
560025e8c5aaSvikram 	if (i_ddi_detachchild(dip, flags) != DDI_SUCCESS) {
560125e8c5aaSvikram 		if (flags & NDI_DEVI_OFFLINE) {
560225e8c5aaSvikram 			RIO_DEBUG((CE_NOTE, "devi_detach_node: offline failed."
560325e8c5aaSvikram 			    " Calling e_ddi_offline_finalize with result=%d. "
560425e8c5aaSvikram 			    "dip=%p", DDI_FAILURE, (void *)dip));
560525e8c5aaSvikram 			e_ddi_offline_finalize(dip, DDI_FAILURE);
560625e8c5aaSvikram 		}
56077c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
560825e8c5aaSvikram 	}
560925e8c5aaSvikram 
561025e8c5aaSvikram 	if (flags & NDI_DEVI_OFFLINE) {
561125e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "devi_detach_node: offline succeeded."
561225e8c5aaSvikram 		    " Calling e_ddi_offline_finalize with result=%d, "
561325e8c5aaSvikram 		    "dip=%p", DDI_SUCCESS, (void *)dip));
561425e8c5aaSvikram 		e_ddi_offline_finalize(dip, DDI_SUCCESS);
561525e8c5aaSvikram 	}
56167c478bd9Sstevel@tonic-gate 
56177c478bd9Sstevel@tonic-gate 	if (flags & NDI_AUTODETACH)
56187c478bd9Sstevel@tonic-gate 		return (NDI_SUCCESS);
56197c478bd9Sstevel@tonic-gate 
56207c478bd9Sstevel@tonic-gate 	/*
56217c478bd9Sstevel@tonic-gate 	 * For DR, even bound nodes may need to have offline
56227c478bd9Sstevel@tonic-gate 	 * flag set.
56237c478bd9Sstevel@tonic-gate 	 */
56247c478bd9Sstevel@tonic-gate 	if (flags & NDI_DEVI_OFFLINE) {
562516747f41Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
56267c478bd9Sstevel@tonic-gate 		DEVI_SET_DEVICE_OFFLINE(dip);
562716747f41Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
56287c478bd9Sstevel@tonic-gate 	}
56297c478bd9Sstevel@tonic-gate 
56307c478bd9Sstevel@tonic-gate 	if (i_ddi_node_state(dip) == DS_INITIALIZED) {
56317c478bd9Sstevel@tonic-gate 		char *path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
56327c478bd9Sstevel@tonic-gate 		(void) ddi_pathname(dip, path);
56337c478bd9Sstevel@tonic-gate 		if (flags & NDI_DEVI_OFFLINE)
56347c478bd9Sstevel@tonic-gate 			i_ndi_devi_report_status_change(dip, path);
56357c478bd9Sstevel@tonic-gate 
56367c478bd9Sstevel@tonic-gate 		if (need_remove_event(dip, flags)) {
56377c478bd9Sstevel@tonic-gate 			(void) i_log_devfs_remove_devinfo(path,
56387c478bd9Sstevel@tonic-gate 			    i_ddi_devi_class(dip),
56397c478bd9Sstevel@tonic-gate 			    (char *)ddi_driver_name(dip),
56407c478bd9Sstevel@tonic-gate 			    ddi_get_instance(dip),
56417c478bd9Sstevel@tonic-gate 			    flags);
564216747f41Scth 			mutex_enter(&(DEVI(dip)->devi_lock));
56437c478bd9Sstevel@tonic-gate 			DEVI_SET_EVREMOVE(dip);
564416747f41Scth 			mutex_exit(&(DEVI(dip)->devi_lock));
56457c478bd9Sstevel@tonic-gate 		}
56467c478bd9Sstevel@tonic-gate 		kmem_free(path, MAXPATHLEN);
56477c478bd9Sstevel@tonic-gate 	}
56487c478bd9Sstevel@tonic-gate 
56497c478bd9Sstevel@tonic-gate 	if (flags & (NDI_UNCONFIG | NDI_DEVI_REMOVE)) {
56507c478bd9Sstevel@tonic-gate 		ret = ddi_uninitchild(dip);
56517c478bd9Sstevel@tonic-gate 		if (ret == NDI_SUCCESS) {
56527c478bd9Sstevel@tonic-gate 			/*
56537c478bd9Sstevel@tonic-gate 			 * Remove uninitialized pseudo nodes because
56547c478bd9Sstevel@tonic-gate 			 * system props are lost and the node cannot be
56557c478bd9Sstevel@tonic-gate 			 * reattached.
56567c478bd9Sstevel@tonic-gate 			 */
56577c478bd9Sstevel@tonic-gate 			if (!ndi_dev_is_persistent_node(dip))
56587c478bd9Sstevel@tonic-gate 				flags |= NDI_DEVI_REMOVE;
56597c478bd9Sstevel@tonic-gate 
56607c478bd9Sstevel@tonic-gate 			if (flags & NDI_DEVI_REMOVE)
56617c478bd9Sstevel@tonic-gate 				ret = ddi_remove_child(dip, 0);
56627c478bd9Sstevel@tonic-gate 		}
56637c478bd9Sstevel@tonic-gate 	}
56647c478bd9Sstevel@tonic-gate 
56657c478bd9Sstevel@tonic-gate 	return (ret);
56667c478bd9Sstevel@tonic-gate }
56677c478bd9Sstevel@tonic-gate 
56687c478bd9Sstevel@tonic-gate /*
56697c478bd9Sstevel@tonic-gate  * unconfigure immediate children of bus nexus device
56707c478bd9Sstevel@tonic-gate  */
56717c478bd9Sstevel@tonic-gate static int
56727c478bd9Sstevel@tonic-gate unconfig_immediate_children(
56737c478bd9Sstevel@tonic-gate 	dev_info_t *dip,
56747c478bd9Sstevel@tonic-gate 	dev_info_t **dipp,
56757c478bd9Sstevel@tonic-gate 	int flags,
56767c478bd9Sstevel@tonic-gate 	major_t major)
56777c478bd9Sstevel@tonic-gate {
56785e3986cbScth 	int rv = NDI_SUCCESS;
56795e3986cbScth 	int circ, vcirc;
56807c478bd9Sstevel@tonic-gate 	dev_info_t *child;
56815e3986cbScth 	dev_info_t *vdip = NULL;
56825e3986cbScth 	dev_info_t *next;
56837c478bd9Sstevel@tonic-gate 
56847c478bd9Sstevel@tonic-gate 	ASSERT(dipp == NULL || *dipp == NULL);
56857c478bd9Sstevel@tonic-gate 
56865e3986cbScth 	/*
56875e3986cbScth 	 * Scan forward to see if we will be processing a pHCI child. If we
56885e3986cbScth 	 * have a child that is a pHCI and vHCI and pHCI are not siblings then
56895e3986cbScth 	 * enter vHCI before parent(pHCI) to prevent deadlock with mpxio
56905e3986cbScth 	 * Client power management operations.
56915e3986cbScth 	 */
56927c478bd9Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
56935e3986cbScth 	for (child = ddi_get_child(dip); child;
56945e3986cbScth 	    child = ddi_get_next_sibling(child)) {
56955e3986cbScth 		/* skip same nodes we skip below */
5696a204de77Scth 		if (((major != DDI_MAJOR_T_NONE) &&
56975e3986cbScth 		    (major != ddi_driver_major(child))) ||
56985e3986cbScth 		    ((flags & NDI_AUTODETACH) && !is_leaf_node(child)))
56995e3986cbScth 			continue;
57005e3986cbScth 
57015e3986cbScth 		if (MDI_PHCI(child)) {
57025e3986cbScth 			vdip = mdi_devi_get_vdip(child);
57035e3986cbScth 			/*
57045e3986cbScth 			 * If vHCI and vHCI is not a sibling of pHCI
57055e3986cbScth 			 * then enter in (vHCI, parent(pHCI)) order.
57065e3986cbScth 			 */
57075e3986cbScth 			if (vdip && (ddi_get_parent(vdip) != dip)) {
57085e3986cbScth 				ndi_devi_exit(dip, circ);
57095e3986cbScth 
57105e3986cbScth 				/* use mdi_devi_enter ordering */
57115e3986cbScth 				ndi_devi_enter(vdip, &vcirc);
57125e3986cbScth 				ndi_devi_enter(dip, &circ);
57135e3986cbScth 				break;
57145e3986cbScth 			} else
57155e3986cbScth 				vdip = NULL;
57165e3986cbScth 		}
57175e3986cbScth 	}
57185e3986cbScth 
57197c478bd9Sstevel@tonic-gate 	child = ddi_get_child(dip);
57207c478bd9Sstevel@tonic-gate 	while (child) {
57215e3986cbScth 		next = ddi_get_next_sibling(child);
57225e3986cbScth 
5723a204de77Scth 		if ((major != DDI_MAJOR_T_NONE) &&
57247c478bd9Sstevel@tonic-gate 		    (major != ddi_driver_major(child))) {
57257c478bd9Sstevel@tonic-gate 			child = next;
57267c478bd9Sstevel@tonic-gate 			continue;
57277c478bd9Sstevel@tonic-gate 		}
57287c478bd9Sstevel@tonic-gate 
57297c478bd9Sstevel@tonic-gate 		/* skip nexus nodes during autodetach */
57307c478bd9Sstevel@tonic-gate 		if ((flags & NDI_AUTODETACH) && !is_leaf_node(child)) {
57317c478bd9Sstevel@tonic-gate 			child = next;
57327c478bd9Sstevel@tonic-gate 			continue;
57337c478bd9Sstevel@tonic-gate 		}
57347c478bd9Sstevel@tonic-gate 
57357c478bd9Sstevel@tonic-gate 		if (devi_detach_node(child, flags) != NDI_SUCCESS) {
57367c478bd9Sstevel@tonic-gate 			if (dipp && *dipp == NULL) {
57377c478bd9Sstevel@tonic-gate 				ndi_hold_devi(child);
57387c478bd9Sstevel@tonic-gate 				*dipp = child;
57397c478bd9Sstevel@tonic-gate 			}
57407c478bd9Sstevel@tonic-gate 			rv = NDI_FAILURE;
57417c478bd9Sstevel@tonic-gate 		}
57427c478bd9Sstevel@tonic-gate 
57437c478bd9Sstevel@tonic-gate 		/*
57447c478bd9Sstevel@tonic-gate 		 * Continue upon failure--best effort algorithm
57457c478bd9Sstevel@tonic-gate 		 */
57467c478bd9Sstevel@tonic-gate 		child = next;
57477c478bd9Sstevel@tonic-gate 	}
57485e3986cbScth 
57497c478bd9Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
57505e3986cbScth 	if (vdip)
57515e3986cbScth 		ndi_devi_exit(vdip, vcirc);
57525e3986cbScth 
57537c478bd9Sstevel@tonic-gate 	return (rv);
57547c478bd9Sstevel@tonic-gate }
57557c478bd9Sstevel@tonic-gate 
57567c478bd9Sstevel@tonic-gate /*
57577c478bd9Sstevel@tonic-gate  * unconfigure grand children of bus nexus device
57587c478bd9Sstevel@tonic-gate  */
57597c478bd9Sstevel@tonic-gate static int
57607c478bd9Sstevel@tonic-gate unconfig_grand_children(
57617c478bd9Sstevel@tonic-gate 	dev_info_t *dip,
57627c478bd9Sstevel@tonic-gate 	dev_info_t **dipp,
57637c478bd9Sstevel@tonic-gate 	int flags,
57647c478bd9Sstevel@tonic-gate 	major_t major,
57657c478bd9Sstevel@tonic-gate 	struct brevq_node **brevqp)
57667c478bd9Sstevel@tonic-gate {
57677c478bd9Sstevel@tonic-gate 	struct mt_config_handle *hdl;
57687c478bd9Sstevel@tonic-gate 
57697c478bd9Sstevel@tonic-gate 	if (brevqp)
57707c478bd9Sstevel@tonic-gate 		*brevqp = NULL;
57717c478bd9Sstevel@tonic-gate 
57727c478bd9Sstevel@tonic-gate 	/* multi-threaded configuration of child nexus */
57737c478bd9Sstevel@tonic-gate 	hdl = mt_config_init(dip, dipp, flags, major, MT_UNCONFIG_OP, brevqp);
57747c478bd9Sstevel@tonic-gate 	mt_config_children(hdl);
57757c478bd9Sstevel@tonic-gate 
57767c478bd9Sstevel@tonic-gate 	return (mt_config_fini(hdl));	/* wait for threads to exit */
57777c478bd9Sstevel@tonic-gate }
57787c478bd9Sstevel@tonic-gate 
57797c478bd9Sstevel@tonic-gate /*
57807c478bd9Sstevel@tonic-gate  * Unconfigure children/descendants of the dip.
57817c478bd9Sstevel@tonic-gate  *
57827c478bd9Sstevel@tonic-gate  * If brevqp is not NULL, on return *brevqp is set to a queue of dip's
57837c478bd9Sstevel@tonic-gate  * child devinames for which branch remove events need to be generated.
57847c478bd9Sstevel@tonic-gate  */
57857c478bd9Sstevel@tonic-gate static int
57867c478bd9Sstevel@tonic-gate devi_unconfig_common(
57877c478bd9Sstevel@tonic-gate 	dev_info_t *dip,
57887c478bd9Sstevel@tonic-gate 	dev_info_t **dipp,
57897c478bd9Sstevel@tonic-gate 	int flags,
57907c478bd9Sstevel@tonic-gate 	major_t major,
57917c478bd9Sstevel@tonic-gate 	struct brevq_node **brevqp)
57927c478bd9Sstevel@tonic-gate {
57937c478bd9Sstevel@tonic-gate 	int rv;
57947c478bd9Sstevel@tonic-gate 	int pm_cookie;
57957c478bd9Sstevel@tonic-gate 	int (*f)();
57967c478bd9Sstevel@tonic-gate 	ddi_bus_config_op_t bus_op;
57977c478bd9Sstevel@tonic-gate 
57987c478bd9Sstevel@tonic-gate 	if (dipp)
57997c478bd9Sstevel@tonic-gate 		*dipp = NULL;
58007c478bd9Sstevel@tonic-gate 	if (brevqp)
58017c478bd9Sstevel@tonic-gate 		*brevqp = NULL;
58027c478bd9Sstevel@tonic-gate 
58037c478bd9Sstevel@tonic-gate 	/*
58047c478bd9Sstevel@tonic-gate 	 * Power up the dip if it is powered off.  If the flag bit
58057c478bd9Sstevel@tonic-gate 	 * NDI_AUTODETACH is set and the dip is not at its full power,
58067c478bd9Sstevel@tonic-gate 	 * skip the rest of the branch.
58077c478bd9Sstevel@tonic-gate 	 */
58087c478bd9Sstevel@tonic-gate 	if (pm_pre_unconfig(dip, flags, &pm_cookie, NULL) != DDI_SUCCESS)
58097c478bd9Sstevel@tonic-gate 		return ((flags & NDI_AUTODETACH) ? NDI_SUCCESS :
58107c478bd9Sstevel@tonic-gate 		    NDI_FAILURE);
58117c478bd9Sstevel@tonic-gate 
58127c478bd9Sstevel@tonic-gate 	/*
58137c478bd9Sstevel@tonic-gate 	 * Some callers, notably SCSI, need to clear out the devfs
58147c478bd9Sstevel@tonic-gate 	 * cache together with the unconfig to prevent stale entries.
58157c478bd9Sstevel@tonic-gate 	 */
58167c478bd9Sstevel@tonic-gate 	if (flags & NDI_DEVFS_CLEAN)
58177c478bd9Sstevel@tonic-gate 		(void) devfs_clean(dip, NULL, 0);
58187c478bd9Sstevel@tonic-gate 
58197c478bd9Sstevel@tonic-gate 	rv = unconfig_grand_children(dip, dipp, flags, major, brevqp);
58207c478bd9Sstevel@tonic-gate 
58217c478bd9Sstevel@tonic-gate 	if ((rv != NDI_SUCCESS) && ((flags & NDI_AUTODETACH) == 0)) {
58227c478bd9Sstevel@tonic-gate 		if (brevqp && *brevqp) {
58237c478bd9Sstevel@tonic-gate 			log_and_free_br_events_on_grand_children(dip, *brevqp);
58247c478bd9Sstevel@tonic-gate 			free_brevq(*brevqp);
58257c478bd9Sstevel@tonic-gate 			*brevqp = NULL;
58267c478bd9Sstevel@tonic-gate 		}
58277c478bd9Sstevel@tonic-gate 		pm_post_unconfig(dip, pm_cookie, NULL);
58287c478bd9Sstevel@tonic-gate 		return (rv);
58297c478bd9Sstevel@tonic-gate 	}
58307c478bd9Sstevel@tonic-gate 
58317c478bd9Sstevel@tonic-gate 	if (dipp && *dipp) {
58327c478bd9Sstevel@tonic-gate 		ndi_rele_devi(*dipp);
58337c478bd9Sstevel@tonic-gate 		*dipp = NULL;
58347c478bd9Sstevel@tonic-gate 	}
58357c478bd9Sstevel@tonic-gate 
58367c478bd9Sstevel@tonic-gate 	/*
58377c478bd9Sstevel@tonic-gate 	 * It is possible to have a detached nexus with children
58387c478bd9Sstevel@tonic-gate 	 * and grandchildren (for example: a branch consisting
58397c478bd9Sstevel@tonic-gate 	 * entirely of bound nodes.) Since the nexus is detached
58407c478bd9Sstevel@tonic-gate 	 * the bus_unconfig entry point cannot be used to remove
58417c478bd9Sstevel@tonic-gate 	 * or unconfigure the descendants.
58427c478bd9Sstevel@tonic-gate 	 */
5843737d277aScth 	if (!i_ddi_devi_attached(dip) ||
58447c478bd9Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
58457c478bd9Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
58467c478bd9Sstevel@tonic-gate 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
58477c478bd9Sstevel@tonic-gate 		rv = unconfig_immediate_children(dip, dipp, flags, major);
58487c478bd9Sstevel@tonic-gate 	} else {
58497c478bd9Sstevel@tonic-gate 		/*
58507c478bd9Sstevel@tonic-gate 		 * call bus_unconfig entry point
58517c478bd9Sstevel@tonic-gate 		 * It should reset nexus flags if unconfigure succeeds.
58527c478bd9Sstevel@tonic-gate 		 */
5853a204de77Scth 		bus_op = (major == DDI_MAJOR_T_NONE) ?
58547c478bd9Sstevel@tonic-gate 		    BUS_UNCONFIG_ALL : BUS_UNCONFIG_DRIVER;
58557c478bd9Sstevel@tonic-gate 		rv = (*f)(dip, flags, bus_op, (void *)(uintptr_t)major);
58567c478bd9Sstevel@tonic-gate 	}
58577c478bd9Sstevel@tonic-gate 
58587c478bd9Sstevel@tonic-gate 	pm_post_unconfig(dip, pm_cookie, NULL);
58597c478bd9Sstevel@tonic-gate 
58607c478bd9Sstevel@tonic-gate 	if (brevqp && *brevqp)
58617c478bd9Sstevel@tonic-gate 		cleanup_br_events_on_grand_children(dip, brevqp);
58627c478bd9Sstevel@tonic-gate 
58637c478bd9Sstevel@tonic-gate 	return (rv);
58647c478bd9Sstevel@tonic-gate }
58657c478bd9Sstevel@tonic-gate 
58667c478bd9Sstevel@tonic-gate /*
58677c478bd9Sstevel@tonic-gate  * called by devfs/framework to unconfigure children bound to major
58687c478bd9Sstevel@tonic-gate  * If NDI_AUTODETACH is specified, this is invoked by either the
58697c478bd9Sstevel@tonic-gate  * moduninstall daemon or the modunload -i 0 command.
58707c478bd9Sstevel@tonic-gate  */
58717c478bd9Sstevel@tonic-gate int
58727c478bd9Sstevel@tonic-gate ndi_devi_unconfig_driver(dev_info_t *dip, int flags, major_t major)
58737c478bd9Sstevel@tonic-gate {
58747c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
58757c478bd9Sstevel@tonic-gate 	    "ndi_devi_unconfig_driver: par = %s%d (%p), flags = 0x%x\n",
58767c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
58777c478bd9Sstevel@tonic-gate 
58787c478bd9Sstevel@tonic-gate 	return (devi_unconfig_common(dip, NULL, flags, major, NULL));
58797c478bd9Sstevel@tonic-gate }
58807c478bd9Sstevel@tonic-gate 
58817c478bd9Sstevel@tonic-gate int
58827c478bd9Sstevel@tonic-gate ndi_devi_unconfig(dev_info_t *dip, int flags)
58837c478bd9Sstevel@tonic-gate {
58847c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
58857c478bd9Sstevel@tonic-gate 	    "ndi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
58867c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
58877c478bd9Sstevel@tonic-gate 
5888a204de77Scth 	return (devi_unconfig_common(dip, NULL, flags, DDI_MAJOR_T_NONE, NULL));
58897c478bd9Sstevel@tonic-gate }
58907c478bd9Sstevel@tonic-gate 
58917c478bd9Sstevel@tonic-gate int
58927c478bd9Sstevel@tonic-gate e_ddi_devi_unconfig(dev_info_t *dip, dev_info_t **dipp, int flags)
58937c478bd9Sstevel@tonic-gate {
58947c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
58957c478bd9Sstevel@tonic-gate 	    "e_ddi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
58967c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
58977c478bd9Sstevel@tonic-gate 
5898a204de77Scth 	return (devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE, NULL));
58997c478bd9Sstevel@tonic-gate }
59007c478bd9Sstevel@tonic-gate 
59017c478bd9Sstevel@tonic-gate /*
59027c478bd9Sstevel@tonic-gate  * Unconfigure child by name
59037c478bd9Sstevel@tonic-gate  */
59047c478bd9Sstevel@tonic-gate static int
59057c478bd9Sstevel@tonic-gate devi_unconfig_one(dev_info_t *pdip, char *devnm, int flags)
59067c478bd9Sstevel@tonic-gate {
59077c478bd9Sstevel@tonic-gate 	int		rv, circ;
59087c478bd9Sstevel@tonic-gate 	dev_info_t	*child;
59095e3986cbScth 	dev_info_t	*vdip = NULL;
59105e3986cbScth 	int		v_circ;
59117c478bd9Sstevel@tonic-gate 
59127c478bd9Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
59137c478bd9Sstevel@tonic-gate 	child = ndi_devi_findchild(pdip, devnm);
59145e3986cbScth 
59155e3986cbScth 	/*
59165e3986cbScth 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
59175e3986cbScth 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
59185e3986cbScth 	 * management operations.
59195e3986cbScth 	 */
59205e3986cbScth 	if (child && MDI_PHCI(child)) {
59215e3986cbScth 		vdip = mdi_devi_get_vdip(child);
59225e3986cbScth 		if (vdip && (ddi_get_parent(vdip) != pdip)) {
59235e3986cbScth 			ndi_devi_exit(pdip, circ);
59245e3986cbScth 
59255e3986cbScth 			/* use mdi_devi_enter ordering */
59265e3986cbScth 			ndi_devi_enter(vdip, &v_circ);
59275e3986cbScth 			ndi_devi_enter(pdip, &circ);
59285e3986cbScth 			child = ndi_devi_findchild(pdip, devnm);
59295e3986cbScth 		} else
59305e3986cbScth 			vdip = NULL;
59315e3986cbScth 	}
59325e3986cbScth 
59335e3986cbScth 	if (child) {
59345e3986cbScth 		rv = devi_detach_node(child, flags);
59355e3986cbScth 	} else {
59367c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
59377c478bd9Sstevel@tonic-gate 		    "devi_unconfig_one: %s not found\n", devnm));
59385e3986cbScth 		rv = NDI_SUCCESS;
5939c73a93f2Sdm120769 	}
59405e3986cbScth 
5941c73a93f2Sdm120769 	ndi_devi_exit(pdip, circ);
59425e3986cbScth 	if (vdip)
5943ffc89d77Svikram 		ndi_devi_exit(vdip, v_circ);
59445e3986cbScth 
59457c478bd9Sstevel@tonic-gate 	return (rv);
59467c478bd9Sstevel@tonic-gate }
59477c478bd9Sstevel@tonic-gate 
59487c478bd9Sstevel@tonic-gate int
59497c478bd9Sstevel@tonic-gate ndi_devi_unconfig_one(
59507c478bd9Sstevel@tonic-gate 	dev_info_t *pdip,
59517c478bd9Sstevel@tonic-gate 	char *devnm,
59527c478bd9Sstevel@tonic-gate 	dev_info_t **dipp,
59537c478bd9Sstevel@tonic-gate 	int flags)
59547c478bd9Sstevel@tonic-gate {
59557c478bd9Sstevel@tonic-gate 	int		(*f)();
59567c478bd9Sstevel@tonic-gate 	int		circ, rv;
59577c478bd9Sstevel@tonic-gate 	int		pm_cookie;
59587c478bd9Sstevel@tonic-gate 	dev_info_t	*child;
59595e3986cbScth 	dev_info_t	*vdip = NULL;
59605e3986cbScth 	int		v_circ;
59617c478bd9Sstevel@tonic-gate 	struct brevq_node *brevq = NULL;
59627c478bd9Sstevel@tonic-gate 
5963737d277aScth 	ASSERT(i_ddi_devi_attached(pdip));
59647c478bd9Sstevel@tonic-gate 
59657c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
59667c478bd9Sstevel@tonic-gate 	    "ndi_devi_unconfig_one: par = %s%d (%p), child = %s\n",
59677c478bd9Sstevel@tonic-gate 	    ddi_driver_name(pdip), ddi_get_instance(pdip),
59687c478bd9Sstevel@tonic-gate 	    (void *)pdip, devnm));
59697c478bd9Sstevel@tonic-gate 
59707c478bd9Sstevel@tonic-gate 	if (pm_pre_unconfig(pdip, flags, &pm_cookie, devnm) != DDI_SUCCESS)
59717c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
59727c478bd9Sstevel@tonic-gate 
59737c478bd9Sstevel@tonic-gate 	if (dipp)
59747c478bd9Sstevel@tonic-gate 		*dipp = NULL;
59757c478bd9Sstevel@tonic-gate 
59767c478bd9Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
59777c478bd9Sstevel@tonic-gate 	child = ndi_devi_findchild(pdip, devnm);
59785e3986cbScth 
59795e3986cbScth 	/*
59805e3986cbScth 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
59815e3986cbScth 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
59825e3986cbScth 	 * management operations.
59835e3986cbScth 	 */
59845e3986cbScth 	if (child && MDI_PHCI(child)) {
59855e3986cbScth 		vdip = mdi_devi_get_vdip(child);
59865e3986cbScth 		if (vdip && (ddi_get_parent(vdip) != pdip)) {
59875e3986cbScth 			ndi_devi_exit(pdip, circ);
59885e3986cbScth 
59895e3986cbScth 			/* use mdi_devi_enter ordering */
59905e3986cbScth 			ndi_devi_enter(vdip, &v_circ);
59915e3986cbScth 			ndi_devi_enter(pdip, &circ);
59925e3986cbScth 			child = ndi_devi_findchild(pdip, devnm);
59935e3986cbScth 		} else
59945e3986cbScth 			vdip = NULL;
59955e3986cbScth 	}
59965e3986cbScth 
59977c478bd9Sstevel@tonic-gate 	if (child == NULL) {
59987c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_unconfig_one: %s"
59997c478bd9Sstevel@tonic-gate 		    " not found\n", devnm));
60005e3986cbScth 		rv = NDI_SUCCESS;
60015e3986cbScth 		goto out;
60027c478bd9Sstevel@tonic-gate 	}
60037c478bd9Sstevel@tonic-gate 
60047c478bd9Sstevel@tonic-gate 	/*
60057c478bd9Sstevel@tonic-gate 	 * Unconfigure children/descendants of named child
60067c478bd9Sstevel@tonic-gate 	 */
60077c478bd9Sstevel@tonic-gate 	rv = devi_unconfig_branch(child, dipp, flags | NDI_UNCONFIG, &brevq);
60087c478bd9Sstevel@tonic-gate 	if (rv != NDI_SUCCESS)
60097c478bd9Sstevel@tonic-gate 		goto out;
60107c478bd9Sstevel@tonic-gate 
60117c478bd9Sstevel@tonic-gate 	init_bound_node_ev(pdip, child, flags);
60127c478bd9Sstevel@tonic-gate 
60137c478bd9Sstevel@tonic-gate 	if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
60147c478bd9Sstevel@tonic-gate 	    (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
60157c478bd9Sstevel@tonic-gate 	    (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
60167c478bd9Sstevel@tonic-gate 		rv = devi_detach_node(child, flags);
60177c478bd9Sstevel@tonic-gate 	} else {
60187c478bd9Sstevel@tonic-gate 		/* call bus_config entry point */
60197c478bd9Sstevel@tonic-gate 		rv = (*f)(pdip, flags, BUS_UNCONFIG_ONE, (void *)devnm);
60207c478bd9Sstevel@tonic-gate 	}
60217c478bd9Sstevel@tonic-gate 
60227c478bd9Sstevel@tonic-gate 	if (brevq) {
60237c478bd9Sstevel@tonic-gate 		if (rv != NDI_SUCCESS)
60247c478bd9Sstevel@tonic-gate 			log_and_free_brevq_dip(child, brevq);
60257c478bd9Sstevel@tonic-gate 		else
60267c478bd9Sstevel@tonic-gate 			free_brevq(brevq);
60277c478bd9Sstevel@tonic-gate 	}
60287c478bd9Sstevel@tonic-gate 
60297c478bd9Sstevel@tonic-gate 	if (dipp && rv != NDI_SUCCESS) {
60307c478bd9Sstevel@tonic-gate 		ndi_hold_devi(child);
60317c478bd9Sstevel@tonic-gate 		ASSERT(*dipp == NULL);
60327c478bd9Sstevel@tonic-gate 		*dipp = child;
60337c478bd9Sstevel@tonic-gate 	}
60347c478bd9Sstevel@tonic-gate 
60357c478bd9Sstevel@tonic-gate out:
60367c478bd9Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
60375e3986cbScth 	if (vdip)
6038ffc89d77Svikram 		ndi_devi_exit(vdip, v_circ);
60395e3986cbScth 
60407c478bd9Sstevel@tonic-gate 	pm_post_unconfig(pdip, pm_cookie, devnm);
60417c478bd9Sstevel@tonic-gate 
60427c478bd9Sstevel@tonic-gate 	return (rv);
60437c478bd9Sstevel@tonic-gate }
60447c478bd9Sstevel@tonic-gate 
60457c478bd9Sstevel@tonic-gate struct async_arg {
60467c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
60477c478bd9Sstevel@tonic-gate 	uint_t flags;
60487c478bd9Sstevel@tonic-gate };
60497c478bd9Sstevel@tonic-gate 
60507c478bd9Sstevel@tonic-gate /*
60517c478bd9Sstevel@tonic-gate  * Common async handler for:
60527c478bd9Sstevel@tonic-gate  *	ndi_devi_bind_driver_async
60537c478bd9Sstevel@tonic-gate  *	ndi_devi_online_async
60547c478bd9Sstevel@tonic-gate  */
60557c478bd9Sstevel@tonic-gate static int
60567c478bd9Sstevel@tonic-gate i_ndi_devi_async_common(dev_info_t *dip, uint_t flags, void (*func)())
60577c478bd9Sstevel@tonic-gate {
60587c478bd9Sstevel@tonic-gate 	int tqflag;
60597c478bd9Sstevel@tonic-gate 	int kmflag;
60607c478bd9Sstevel@tonic-gate 	struct async_arg *arg;
60617c478bd9Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
60627c478bd9Sstevel@tonic-gate 
60637c478bd9Sstevel@tonic-gate 	ASSERT(pdip);
60647c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(pdip)->devi_taskq);
60657c478bd9Sstevel@tonic-gate 	ASSERT(ndi_dev_is_persistent_node(dip));
60667c478bd9Sstevel@tonic-gate 
60677c478bd9Sstevel@tonic-gate 	if (flags & NDI_NOSLEEP) {
60687c478bd9Sstevel@tonic-gate 		kmflag = KM_NOSLEEP;
60697c478bd9Sstevel@tonic-gate 		tqflag = TQ_NOSLEEP;
60707c478bd9Sstevel@tonic-gate 	} else {
60717c478bd9Sstevel@tonic-gate 		kmflag = KM_SLEEP;
60727c478bd9Sstevel@tonic-gate 		tqflag = TQ_SLEEP;
60737c478bd9Sstevel@tonic-gate 	}
60747c478bd9Sstevel@tonic-gate 
60757c478bd9Sstevel@tonic-gate 	arg = kmem_alloc(sizeof (*arg), kmflag);
60767c478bd9Sstevel@tonic-gate 	if (arg == NULL)
60777c478bd9Sstevel@tonic-gate 		goto fail;
60787c478bd9Sstevel@tonic-gate 
60797c478bd9Sstevel@tonic-gate 	arg->flags = flags;
60807c478bd9Sstevel@tonic-gate 	arg->dip = dip;
60817c478bd9Sstevel@tonic-gate 	if (ddi_taskq_dispatch(DEVI(pdip)->devi_taskq, func, arg, tqflag) ==
60827c478bd9Sstevel@tonic-gate 	    DDI_SUCCESS) {
60837c478bd9Sstevel@tonic-gate 		return (NDI_SUCCESS);
60847c478bd9Sstevel@tonic-gate 	}
60857c478bd9Sstevel@tonic-gate 
60867c478bd9Sstevel@tonic-gate fail:
60877c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "%s%d: ddi_taskq_dispatch failed",
60887c478bd9Sstevel@tonic-gate 	    ddi_driver_name(pdip), ddi_get_instance(pdip)));
60897c478bd9Sstevel@tonic-gate 
60907c478bd9Sstevel@tonic-gate 	if (arg)
60917c478bd9Sstevel@tonic-gate 		kmem_free(arg, sizeof (*arg));
60927c478bd9Sstevel@tonic-gate 	return (NDI_FAILURE);
60937c478bd9Sstevel@tonic-gate }
60947c478bd9Sstevel@tonic-gate 
60957c478bd9Sstevel@tonic-gate static void
60967c478bd9Sstevel@tonic-gate i_ndi_devi_bind_driver_cb(struct async_arg *arg)
60977c478bd9Sstevel@tonic-gate {
60987c478bd9Sstevel@tonic-gate 	(void) ndi_devi_bind_driver(arg->dip, arg->flags);
60997c478bd9Sstevel@tonic-gate 	kmem_free(arg, sizeof (*arg));
61007c478bd9Sstevel@tonic-gate }
61017c478bd9Sstevel@tonic-gate 
61027c478bd9Sstevel@tonic-gate int
61037c478bd9Sstevel@tonic-gate ndi_devi_bind_driver_async(dev_info_t *dip, uint_t flags)
61047c478bd9Sstevel@tonic-gate {
61057c478bd9Sstevel@tonic-gate 	return (i_ndi_devi_async_common(dip, flags,
61067c478bd9Sstevel@tonic-gate 	    (void (*)())i_ndi_devi_bind_driver_cb));
61077c478bd9Sstevel@tonic-gate }
61087c478bd9Sstevel@tonic-gate 
61097c478bd9Sstevel@tonic-gate /*
61107c478bd9Sstevel@tonic-gate  * place the devinfo in the ONLINE state.
61117c478bd9Sstevel@tonic-gate  */
61127c478bd9Sstevel@tonic-gate int
61137c478bd9Sstevel@tonic-gate ndi_devi_online(dev_info_t *dip, uint_t flags)
61147c478bd9Sstevel@tonic-gate {
61157c478bd9Sstevel@tonic-gate 	int circ, rv;
61167c478bd9Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
61177c478bd9Sstevel@tonic-gate 	int branch_event = 0;
61187c478bd9Sstevel@tonic-gate 
61197c478bd9Sstevel@tonic-gate 	ASSERT(pdip);
61207c478bd9Sstevel@tonic-gate 
61217c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_online: %s%d (%p)\n",
61227c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
61237c478bd9Sstevel@tonic-gate 
61247c478bd9Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
61257c478bd9Sstevel@tonic-gate 	/* bind child before merging .conf nodes */
61267c478bd9Sstevel@tonic-gate 	rv = i_ndi_config_node(dip, DS_BOUND, flags);
61277c478bd9Sstevel@tonic-gate 	if (rv != NDI_SUCCESS) {
61287c478bd9Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
61297c478bd9Sstevel@tonic-gate 		return (rv);
61307c478bd9Sstevel@tonic-gate 	}
61317c478bd9Sstevel@tonic-gate 
61327c478bd9Sstevel@tonic-gate 	/* merge .conf properties */
61337c478bd9Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(pdip, flags);
61347c478bd9Sstevel@tonic-gate 
6135c73a93f2Sdm120769 	flags |= (NDI_DEVI_ONLINE | NDI_CONFIG);
61367c478bd9Sstevel@tonic-gate 
61377c478bd9Sstevel@tonic-gate 	if (flags & NDI_NO_EVENT) {
61387c478bd9Sstevel@tonic-gate 		/*
61397c478bd9Sstevel@tonic-gate 		 * Caller is specifically asking for not to generate an event.
61407c478bd9Sstevel@tonic-gate 		 * Set the following flag so that devi_attach_node() don't
61417c478bd9Sstevel@tonic-gate 		 * change the event state.
61427c478bd9Sstevel@tonic-gate 		 */
61437c478bd9Sstevel@tonic-gate 		flags |= NDI_NO_EVENT_STATE_CHNG;
61447c478bd9Sstevel@tonic-gate 	}
61457c478bd9Sstevel@tonic-gate 
61467c478bd9Sstevel@tonic-gate 	if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
61477c478bd9Sstevel@tonic-gate 	    ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip))) {
61487c478bd9Sstevel@tonic-gate 		flags |= NDI_BRANCH_EVENT_OP;
61497c478bd9Sstevel@tonic-gate 		branch_event = 1;
61507c478bd9Sstevel@tonic-gate 	}
61517c478bd9Sstevel@tonic-gate 
61527c478bd9Sstevel@tonic-gate 	/*
61537c478bd9Sstevel@tonic-gate 	 * devi_attach_node() may remove dip on failure
61547c478bd9Sstevel@tonic-gate 	 */
61557c478bd9Sstevel@tonic-gate 	if ((rv = devi_attach_node(dip, flags)) == NDI_SUCCESS) {
61567c478bd9Sstevel@tonic-gate 		if ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip)) {
61577c478bd9Sstevel@tonic-gate 			(void) ndi_devi_config(dip, flags);
61587c478bd9Sstevel@tonic-gate 		}
61597c478bd9Sstevel@tonic-gate 
61607c478bd9Sstevel@tonic-gate 		if (branch_event)
61617c478bd9Sstevel@tonic-gate 			(void) i_log_devfs_branch_add(dip);
61627c478bd9Sstevel@tonic-gate 	}
61637c478bd9Sstevel@tonic-gate 
61647c478bd9Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
61657c478bd9Sstevel@tonic-gate 
61667c478bd9Sstevel@tonic-gate 	/*
61677c478bd9Sstevel@tonic-gate 	 * Notify devfs that we have a new node. Devfs needs to invalidate
61687c478bd9Sstevel@tonic-gate 	 * cached directory contents.
61697c478bd9Sstevel@tonic-gate 	 *
61707c478bd9Sstevel@tonic-gate 	 * For PCMCIA devices, it is possible the pdip is not fully
61717c478bd9Sstevel@tonic-gate 	 * attached. In this case, calling back into devfs will
61727c478bd9Sstevel@tonic-gate 	 * result in a loop or assertion error. Hence, the check
61737c478bd9Sstevel@tonic-gate 	 * on node state.
61747c478bd9Sstevel@tonic-gate 	 *
61757c478bd9Sstevel@tonic-gate 	 * If we own parent lock, this is part of a branch operation.
61767c478bd9Sstevel@tonic-gate 	 * We skip the devfs_clean() step because the cache invalidation
61777c478bd9Sstevel@tonic-gate 	 * is done higher up in the device tree.
61787c478bd9Sstevel@tonic-gate 	 */
6179737d277aScth 	if (rv == NDI_SUCCESS && i_ddi_devi_attached(pdip) &&
61807c478bd9Sstevel@tonic-gate 	    !DEVI_BUSY_OWNED(pdip))
61817c478bd9Sstevel@tonic-gate 		(void) devfs_clean(pdip, NULL, 0);
61827c478bd9Sstevel@tonic-gate 	return (rv);
61837c478bd9Sstevel@tonic-gate }
61847c478bd9Sstevel@tonic-gate 
61857c478bd9Sstevel@tonic-gate static void
61867c478bd9Sstevel@tonic-gate i_ndi_devi_online_cb(struct async_arg *arg)
61877c478bd9Sstevel@tonic-gate {
61887c478bd9Sstevel@tonic-gate 	(void) ndi_devi_online(arg->dip, arg->flags);
61897c478bd9Sstevel@tonic-gate 	kmem_free(arg, sizeof (*arg));
61907c478bd9Sstevel@tonic-gate }
61917c478bd9Sstevel@tonic-gate 
61927c478bd9Sstevel@tonic-gate int
61937c478bd9Sstevel@tonic-gate ndi_devi_online_async(dev_info_t *dip, uint_t flags)
61947c478bd9Sstevel@tonic-gate {
61957c478bd9Sstevel@tonic-gate 	/* mark child as need config if requested. */
619616747f41Scth 	if (flags & NDI_CONFIG) {
619716747f41Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
61987c478bd9Sstevel@tonic-gate 		DEVI_SET_NDI_CONFIG(dip);
619916747f41Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
620016747f41Scth 	}
62017c478bd9Sstevel@tonic-gate 
62027c478bd9Sstevel@tonic-gate 	return (i_ndi_devi_async_common(dip, flags,
62037c478bd9Sstevel@tonic-gate 	    (void (*)())i_ndi_devi_online_cb));
62047c478bd9Sstevel@tonic-gate }
62057c478bd9Sstevel@tonic-gate 
62067c478bd9Sstevel@tonic-gate /*
62077c478bd9Sstevel@tonic-gate  * Take a device node Offline
62087c478bd9Sstevel@tonic-gate  * To take a device Offline means to detach the device instance from
62097c478bd9Sstevel@tonic-gate  * the driver and prevent devfs requests from re-attaching the device
62107c478bd9Sstevel@tonic-gate  * instance.
62117c478bd9Sstevel@tonic-gate  *
62127c478bd9Sstevel@tonic-gate  * The flag NDI_DEVI_REMOVE causes removes the device node from
62137c478bd9Sstevel@tonic-gate  * the driver list and the device tree. In this case, the device
62147c478bd9Sstevel@tonic-gate  * is assumed to be removed from the system.
62157c478bd9Sstevel@tonic-gate  */
62167c478bd9Sstevel@tonic-gate int
62177c478bd9Sstevel@tonic-gate ndi_devi_offline(dev_info_t *dip, uint_t flags)
62187c478bd9Sstevel@tonic-gate {
62197c478bd9Sstevel@tonic-gate 	int		circ, rval = 0;
62207c478bd9Sstevel@tonic-gate 	dev_info_t	*pdip = ddi_get_parent(dip);
62215e3986cbScth 	dev_info_t	*vdip = NULL;
62225e3986cbScth 	int		v_circ;
62237c478bd9Sstevel@tonic-gate 	struct brevq_node *brevq = NULL;
62247c478bd9Sstevel@tonic-gate 
62257c478bd9Sstevel@tonic-gate 	ASSERT(pdip);
62267c478bd9Sstevel@tonic-gate 
62277c478bd9Sstevel@tonic-gate 	flags |= NDI_DEVI_OFFLINE;
62285e3986cbScth 
62295e3986cbScth 	/*
62305e3986cbScth 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
62315e3986cbScth 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
62325e3986cbScth 	 * management operations.
62335e3986cbScth 	 */
62345e3986cbScth 	if (MDI_PHCI(dip)) {
62355e3986cbScth 		vdip = mdi_devi_get_vdip(dip);
62365e3986cbScth 		if (vdip && (ddi_get_parent(vdip) != pdip))
62375e3986cbScth 			ndi_devi_enter(vdip, &v_circ);
62385e3986cbScth 		else
62395e3986cbScth 			vdip = NULL;
62405e3986cbScth 	}
62417c478bd9Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
62425e3986cbScth 
62437c478bd9Sstevel@tonic-gate 	if (i_ddi_node_state(dip) == DS_READY) {
62447c478bd9Sstevel@tonic-gate 		/*
62457c478bd9Sstevel@tonic-gate 		 * If dip is in DS_READY state, there may be cached dv_nodes
62467c478bd9Sstevel@tonic-gate 		 * referencing this dip, so we invoke devfs code path.
62477c478bd9Sstevel@tonic-gate 		 * Note that we must release busy changing on pdip to
62487c478bd9Sstevel@tonic-gate 		 * avoid deadlock against devfs.
62497c478bd9Sstevel@tonic-gate 		 */
62507c478bd9Sstevel@tonic-gate 		char *devname = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
62517c478bd9Sstevel@tonic-gate 		(void) ddi_deviname(dip, devname);
62525e3986cbScth 
62537c478bd9Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
62545e3986cbScth 		if (vdip)
62555e3986cbScth 			ndi_devi_exit(vdip, v_circ);
62567c478bd9Sstevel@tonic-gate 
62577c478bd9Sstevel@tonic-gate 		/*
62587c478bd9Sstevel@tonic-gate 		 * If we own parent lock, this is part of a branch
62597c478bd9Sstevel@tonic-gate 		 * operation. We skip the devfs_clean() step.
62607c478bd9Sstevel@tonic-gate 		 */
62617c478bd9Sstevel@tonic-gate 		if (!DEVI_BUSY_OWNED(pdip))
626219174f18Svikram 			(void) devfs_clean(pdip, devname + 1, DV_CLEAN_FORCE);
62637c478bd9Sstevel@tonic-gate 		kmem_free(devname, MAXNAMELEN + 1);
62647c478bd9Sstevel@tonic-gate 
626519174f18Svikram 		rval = devi_unconfig_branch(dip, NULL, flags|NDI_UNCONFIG,
626619174f18Svikram 		    &brevq);
626719174f18Svikram 
62687c478bd9Sstevel@tonic-gate 		if (rval)
62697c478bd9Sstevel@tonic-gate 			return (NDI_FAILURE);
62707c478bd9Sstevel@tonic-gate 
62715e3986cbScth 		if (vdip)
62725e3986cbScth 			ndi_devi_enter(vdip, &v_circ);
62737c478bd9Sstevel@tonic-gate 		ndi_devi_enter(pdip, &circ);
62747c478bd9Sstevel@tonic-gate 	}
62757c478bd9Sstevel@tonic-gate 
62767c478bd9Sstevel@tonic-gate 	init_bound_node_ev(pdip, dip, flags);
62777c478bd9Sstevel@tonic-gate 
62787c478bd9Sstevel@tonic-gate 	rval = devi_detach_node(dip, flags);
62797c478bd9Sstevel@tonic-gate 	if (brevq) {
62807c478bd9Sstevel@tonic-gate 		if (rval != NDI_SUCCESS)
62817c478bd9Sstevel@tonic-gate 			log_and_free_brevq_dip(dip, brevq);
62827c478bd9Sstevel@tonic-gate 		else
62837c478bd9Sstevel@tonic-gate 			free_brevq(brevq);
62847c478bd9Sstevel@tonic-gate 	}
62857c478bd9Sstevel@tonic-gate 
62867c478bd9Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
62875e3986cbScth 	if (vdip)
62885e3986cbScth 		ndi_devi_exit(vdip, v_circ);
62897c478bd9Sstevel@tonic-gate 
62907c478bd9Sstevel@tonic-gate 	return (rval);
62917c478bd9Sstevel@tonic-gate }
62927c478bd9Sstevel@tonic-gate 
62937c478bd9Sstevel@tonic-gate /*
62947c478bd9Sstevel@tonic-gate  * Find the child dev_info node of parent nexus 'p' whose name
62957c478bd9Sstevel@tonic-gate  * matches "cname@caddr".  Recommend use of ndi_devi_findchild() instead.
62967c478bd9Sstevel@tonic-gate  */
62977c478bd9Sstevel@tonic-gate dev_info_t *
62987c478bd9Sstevel@tonic-gate ndi_devi_find(dev_info_t *pdip, char *cname, char *caddr)
62997c478bd9Sstevel@tonic-gate {
63007c478bd9Sstevel@tonic-gate 	dev_info_t *child;
63017c478bd9Sstevel@tonic-gate 	int circ;
63027c478bd9Sstevel@tonic-gate 
63037c478bd9Sstevel@tonic-gate 	if (pdip == NULL || cname == NULL || caddr == NULL)
63047c478bd9Sstevel@tonic-gate 		return ((dev_info_t *)NULL);
63057c478bd9Sstevel@tonic-gate 
63067c478bd9Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
6307f4da9be0Scth 	child = find_sibling(ddi_get_child(pdip), cname, caddr,
6308f4da9be0Scth 	    FIND_NODE_BY_NODENAME, NULL);
63097c478bd9Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
63107c478bd9Sstevel@tonic-gate 	return (child);
63117c478bd9Sstevel@tonic-gate }
63127c478bd9Sstevel@tonic-gate 
63137c478bd9Sstevel@tonic-gate /*
63147c478bd9Sstevel@tonic-gate  * Find the child dev_info node of parent nexus 'p' whose name
63157c478bd9Sstevel@tonic-gate  * matches devname "name@addr".  Permits caller to hold the parent.
63167c478bd9Sstevel@tonic-gate  */
63177c478bd9Sstevel@tonic-gate dev_info_t *
63187c478bd9Sstevel@tonic-gate ndi_devi_findchild(dev_info_t *pdip, char *devname)
63197c478bd9Sstevel@tonic-gate {
63207c478bd9Sstevel@tonic-gate 	dev_info_t *child;
63217c478bd9Sstevel@tonic-gate 	char	*cname, *caddr;
63227c478bd9Sstevel@tonic-gate 	char	*devstr;
63237c478bd9Sstevel@tonic-gate 
63247c478bd9Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(pdip));
63257c478bd9Sstevel@tonic-gate 
63267c478bd9Sstevel@tonic-gate 	devstr = i_ddi_strdup(devname, KM_SLEEP);
63277c478bd9Sstevel@tonic-gate 	i_ddi_parse_name(devstr, &cname, &caddr, NULL);
63287c478bd9Sstevel@tonic-gate 
63297c478bd9Sstevel@tonic-gate 	if (cname == NULL || caddr == NULL) {
63307c478bd9Sstevel@tonic-gate 		kmem_free(devstr, strlen(devname)+1);
63317c478bd9Sstevel@tonic-gate 		return ((dev_info_t *)NULL);
63327c478bd9Sstevel@tonic-gate 	}
63337c478bd9Sstevel@tonic-gate 
6334f4da9be0Scth 	child = find_sibling(ddi_get_child(pdip), cname, caddr,
6335f4da9be0Scth 	    FIND_NODE_BY_NODENAME, NULL);
63367c478bd9Sstevel@tonic-gate 	kmem_free(devstr, strlen(devname)+1);
63377c478bd9Sstevel@tonic-gate 	return (child);
63387c478bd9Sstevel@tonic-gate }
63397c478bd9Sstevel@tonic-gate 
63407c478bd9Sstevel@tonic-gate /*
63417c478bd9Sstevel@tonic-gate  * Misc. routines called by framework only
63427c478bd9Sstevel@tonic-gate  */
63437c478bd9Sstevel@tonic-gate 
63447c478bd9Sstevel@tonic-gate /*
63457c478bd9Sstevel@tonic-gate  * Clear the DEVI_MADE_CHILDREN/DEVI_ATTACHED_CHILDREN flags
63467c478bd9Sstevel@tonic-gate  * if new child spec has been added.
63477c478bd9Sstevel@tonic-gate  */
63487c478bd9Sstevel@tonic-gate static int
63497c478bd9Sstevel@tonic-gate reset_nexus_flags(dev_info_t *dip, void *arg)
63507c478bd9Sstevel@tonic-gate {
63517c478bd9Sstevel@tonic-gate 	struct hwc_spec	*list;
63526a41d557Scth 	int		circ;
63537c478bd9Sstevel@tonic-gate 
63547c478bd9Sstevel@tonic-gate 	if (((DEVI(dip)->devi_flags & DEVI_MADE_CHILDREN) == 0) ||
63557c478bd9Sstevel@tonic-gate 	    ((list = hwc_get_child_spec(dip, (major_t)(uintptr_t)arg)) == NULL))
63567c478bd9Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
63577c478bd9Sstevel@tonic-gate 
63587c478bd9Sstevel@tonic-gate 	hwc_free_spec_list(list);
63596a41d557Scth 
63606a41d557Scth 	/* coordinate child state update */
63616a41d557Scth 	ndi_devi_enter(dip, &circ);
63627c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
63637c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN | DEVI_ATTACHED_CHILDREN);
63647c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
63656a41d557Scth 	ndi_devi_exit(dip, circ);
63667c478bd9Sstevel@tonic-gate 
63677c478bd9Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
63687c478bd9Sstevel@tonic-gate }
63697c478bd9Sstevel@tonic-gate 
63707c478bd9Sstevel@tonic-gate /*
63717c478bd9Sstevel@tonic-gate  * Helper functions, returns NULL if no memory.
63727c478bd9Sstevel@tonic-gate  */
63737c478bd9Sstevel@tonic-gate 
63747c478bd9Sstevel@tonic-gate /*
63757c478bd9Sstevel@tonic-gate  * path_to_major:
63767c478bd9Sstevel@tonic-gate  *
63777c478bd9Sstevel@tonic-gate  * Return an alternate driver name binding for the leaf device
63787c478bd9Sstevel@tonic-gate  * of the given pathname, if there is one. The purpose of this
63797c478bd9Sstevel@tonic-gate  * function is to deal with generic pathnames. The default action
63807c478bd9Sstevel@tonic-gate  * for platforms that can't do this (ie: x86 or any platform that
63817c478bd9Sstevel@tonic-gate  * does not have prom_finddevice functionality, which matches
63827c478bd9Sstevel@tonic-gate  * nodenames and unit-addresses without the drivers participation)
6383a204de77Scth  * is to return DDI_MAJOR_T_NONE.
63847c478bd9Sstevel@tonic-gate  *
63857c478bd9Sstevel@tonic-gate  * Used in loadrootmodules() in the swapgeneric module to
63867c478bd9Sstevel@tonic-gate  * associate a given pathname with a given leaf driver.
63877c478bd9Sstevel@tonic-gate  *
63887c478bd9Sstevel@tonic-gate  */
63897c478bd9Sstevel@tonic-gate major_t
63907c478bd9Sstevel@tonic-gate path_to_major(char *path)
63917c478bd9Sstevel@tonic-gate {
63927c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
63937c478bd9Sstevel@tonic-gate 	char *p, *q;
6394fa9e4066Sahrens 	pnode_t nodeid;
6395f4da9be0Scth 	major_t major;
6396f4da9be0Scth 
6397f4da9be0Scth 	/* check for path-oriented alias */
6398f4da9be0Scth 	major = ddi_name_to_major(path);
6399a204de77Scth 	if ((major != DDI_MAJOR_T_NONE) &&
6400f4da9be0Scth 	    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED)) {
6401f4da9be0Scth 		NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s path bound %s\n",
6402f4da9be0Scth 		    path, ddi_major_to_name(major)));
6403f4da9be0Scth 		return (major);
6404f4da9be0Scth 	}
64057c478bd9Sstevel@tonic-gate 
64067c478bd9Sstevel@tonic-gate 	/*
64077c478bd9Sstevel@tonic-gate 	 * Get the nodeid of the given pathname, if such a mapping exists.
64087c478bd9Sstevel@tonic-gate 	 */
64097c478bd9Sstevel@tonic-gate 	dip = NULL;
64107c478bd9Sstevel@tonic-gate 	nodeid = prom_finddevice(path);
64117c478bd9Sstevel@tonic-gate 	if (nodeid != OBP_BADNODE) {
64127c478bd9Sstevel@tonic-gate 		/*
64137c478bd9Sstevel@tonic-gate 		 * Find the nodeid in our copy of the device tree and return
64147c478bd9Sstevel@tonic-gate 		 * whatever name we used to bind this node to a driver.
64157c478bd9Sstevel@tonic-gate 		 */
64167c478bd9Sstevel@tonic-gate 		dip = e_ddi_nodeid_to_dip(nodeid);
64177c478bd9Sstevel@tonic-gate 	}
64187c478bd9Sstevel@tonic-gate 
64197c478bd9Sstevel@tonic-gate 	if (dip == NULL) {
64207c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_WARN,
64217c478bd9Sstevel@tonic-gate 		    "path_to_major: can't bind <%s>\n", path));
6422a204de77Scth 		return (DDI_MAJOR_T_NONE);
64237c478bd9Sstevel@tonic-gate 	}
64247c478bd9Sstevel@tonic-gate 
64257c478bd9Sstevel@tonic-gate 	/*
64267c478bd9Sstevel@tonic-gate 	 * If we're bound to something other than the nodename,
64277c478bd9Sstevel@tonic-gate 	 * note that in the message buffer and system log.
64287c478bd9Sstevel@tonic-gate 	 */
64297c478bd9Sstevel@tonic-gate 	p = ddi_binding_name(dip);
64307c478bd9Sstevel@tonic-gate 	q = ddi_node_name(dip);
64317c478bd9Sstevel@tonic-gate 	if (p && q && (strcmp(p, q) != 0))
64327c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s bound to %s\n",
64337c478bd9Sstevel@tonic-gate 		    path, p));
64347c478bd9Sstevel@tonic-gate 
6435f4da9be0Scth 	major = ddi_name_to_major(p);
64367c478bd9Sstevel@tonic-gate 
6437f4da9be0Scth 	ndi_rele_devi(dip);		/* release e_ddi_nodeid_to_dip hold */
64387c478bd9Sstevel@tonic-gate 
6439f4da9be0Scth 	return (major);
64407c478bd9Sstevel@tonic-gate }
64417c478bd9Sstevel@tonic-gate 
64427c478bd9Sstevel@tonic-gate /*
64437c478bd9Sstevel@tonic-gate  * Return the held dip for the specified major and instance, attempting to do
64447c478bd9Sstevel@tonic-gate  * an attach if specified. Return NULL if the devi can't be found or put in
64457c478bd9Sstevel@tonic-gate  * the proper state. The caller must release the hold via ddi_release_devi if
64467c478bd9Sstevel@tonic-gate  * a non-NULL value is returned.
64477c478bd9Sstevel@tonic-gate  *
64487c478bd9Sstevel@tonic-gate  * Some callers expect to be able to perform a hold_devi() while in a context
64497c478bd9Sstevel@tonic-gate  * where using ndi_devi_enter() to ensure the hold might cause deadlock (see
64507c478bd9Sstevel@tonic-gate  * open-from-attach code in consconfig_dacf.c). Such special-case callers
64517c478bd9Sstevel@tonic-gate  * must ensure that an ndi_devi_enter(parent)/ndi_devi_hold() from a safe
64527c478bd9Sstevel@tonic-gate  * context is already active. The hold_devi() implementation must accommodate
64537c478bd9Sstevel@tonic-gate  * these callers.
64547c478bd9Sstevel@tonic-gate  */
64557c478bd9Sstevel@tonic-gate static dev_info_t *
64567c478bd9Sstevel@tonic-gate hold_devi(major_t major, int instance, int flags)
64577c478bd9Sstevel@tonic-gate {
64587c478bd9Sstevel@tonic-gate 	struct devnames	*dnp;
64597c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
64607c478bd9Sstevel@tonic-gate 	char		*path;
64617c478bd9Sstevel@tonic-gate 
64627c478bd9Sstevel@tonic-gate 	if ((major >= devcnt) || (instance == -1))
64637c478bd9Sstevel@tonic-gate 		return (NULL);
64647c478bd9Sstevel@tonic-gate 
64657c478bd9Sstevel@tonic-gate 	/* try to find the instance in the per driver list */
64667c478bd9Sstevel@tonic-gate 	dnp = &(devnamesp[major]);
64677c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&(dnp->dn_lock));
64687c478bd9Sstevel@tonic-gate 	for (dip = dnp->dn_head; dip;
64697c478bd9Sstevel@tonic-gate 	    dip = (dev_info_t *)DEVI(dip)->devi_next) {
64707c478bd9Sstevel@tonic-gate 		/* skip node if instance field is not valid */
64717c478bd9Sstevel@tonic-gate 		if (i_ddi_node_state(dip) < DS_INITIALIZED)
64727c478bd9Sstevel@tonic-gate 			continue;
64737c478bd9Sstevel@tonic-gate 
64747c478bd9Sstevel@tonic-gate 		/* look for instance match */
64757c478bd9Sstevel@tonic-gate 		if (DEVI(dip)->devi_instance == instance) {
64767c478bd9Sstevel@tonic-gate 			/*
64777c478bd9Sstevel@tonic-gate 			 * To accommodate callers that can't block in
64787c478bd9Sstevel@tonic-gate 			 * ndi_devi_enter() we do an ndi_devi_hold(), and
64797c478bd9Sstevel@tonic-gate 			 * afterwards check that the node is in a state where
64807c478bd9Sstevel@tonic-gate 			 * the hold prevents detach(). If we did not manage to
64817c478bd9Sstevel@tonic-gate 			 * prevent detach then we ndi_rele_devi() and perform
64827c478bd9Sstevel@tonic-gate 			 * the slow path below (which can result in a blocking
64837c478bd9Sstevel@tonic-gate 			 * ndi_devi_enter() while driving attach top-down).
64847c478bd9Sstevel@tonic-gate 			 * This code depends on the ordering of
64857c478bd9Sstevel@tonic-gate 			 * DEVI_SET_DETACHING and the devi_ref check in the
64867c478bd9Sstevel@tonic-gate 			 * detach_node() code path.
64877c478bd9Sstevel@tonic-gate 			 */
64887c478bd9Sstevel@tonic-gate 			ndi_hold_devi(dip);
6489737d277aScth 			if (i_ddi_devi_attached(dip) &&
64907c478bd9Sstevel@tonic-gate 			    !DEVI_IS_DETACHING(dip)) {
64917c478bd9Sstevel@tonic-gate 				UNLOCK_DEV_OPS(&(dnp->dn_lock));
64927c478bd9Sstevel@tonic-gate 				return (dip);	/* fast-path with devi held */
64937c478bd9Sstevel@tonic-gate 			}
64947c478bd9Sstevel@tonic-gate 			ndi_rele_devi(dip);
64957c478bd9Sstevel@tonic-gate 
64967c478bd9Sstevel@tonic-gate 			/* try slow-path */
64977c478bd9Sstevel@tonic-gate 			dip = NULL;
64987c478bd9Sstevel@tonic-gate 			break;
64997c478bd9Sstevel@tonic-gate 		}
65007c478bd9Sstevel@tonic-gate 	}
65017c478bd9Sstevel@tonic-gate 	ASSERT(dip == NULL);
65027c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
65037c478bd9Sstevel@tonic-gate 
65047c478bd9Sstevel@tonic-gate 	if (flags & E_DDI_HOLD_DEVI_NOATTACH)
65057c478bd9Sstevel@tonic-gate 		return (NULL);		/* told not to drive attach */
65067c478bd9Sstevel@tonic-gate 
65077c478bd9Sstevel@tonic-gate 	/* slow-path may block, so it should not occur from interrupt */
65087c478bd9Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
65097c478bd9Sstevel@tonic-gate 	if (servicing_interrupt())
65107c478bd9Sstevel@tonic-gate 		return (NULL);
65117c478bd9Sstevel@tonic-gate 
65127c478bd9Sstevel@tonic-gate 	/* reconstruct the path and drive attach by path through devfs. */
65137c478bd9Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
65147c478bd9Sstevel@tonic-gate 	if (e_ddi_majorinstance_to_path(major, instance, path) == 0)
65157c478bd9Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_path(path, flags);
65167c478bd9Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
65177c478bd9Sstevel@tonic-gate 	return (dip);			/* with devi held */
65187c478bd9Sstevel@tonic-gate }
65197c478bd9Sstevel@tonic-gate 
65207c478bd9Sstevel@tonic-gate /*
65217c478bd9Sstevel@tonic-gate  * The {e_}ddi_hold_devi{_by_{instance|dev|path}} hold the devinfo node
65227c478bd9Sstevel@tonic-gate  * associated with the specified arguments.  This hold should be released
65237c478bd9Sstevel@tonic-gate  * by calling ddi_release_devi.
65247c478bd9Sstevel@tonic-gate  *
65257c478bd9Sstevel@tonic-gate  * The E_DDI_HOLD_DEVI_NOATTACH flag argument allows the caller to to specify
65267c478bd9Sstevel@tonic-gate  * a failure return if the node is not already attached.
65277c478bd9Sstevel@tonic-gate  *
65287c478bd9Sstevel@tonic-gate  * NOTE: by the time we make e_ddi_hold_devi public, we should be able to reuse
65297c478bd9Sstevel@tonic-gate  * ddi_hold_devi again.
65307c478bd9Sstevel@tonic-gate  */
65317c478bd9Sstevel@tonic-gate dev_info_t *
65327c478bd9Sstevel@tonic-gate ddi_hold_devi_by_instance(major_t major, int instance, int flags)
65337c478bd9Sstevel@tonic-gate {
65347c478bd9Sstevel@tonic-gate 	return (hold_devi(major, instance, flags));
65357c478bd9Sstevel@tonic-gate }
65367c478bd9Sstevel@tonic-gate 
65377c478bd9Sstevel@tonic-gate dev_info_t *
65387c478bd9Sstevel@tonic-gate e_ddi_hold_devi_by_dev(dev_t dev, int flags)
65397c478bd9Sstevel@tonic-gate {
65407c478bd9Sstevel@tonic-gate 	major_t	major = getmajor(dev);
65417c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
65427c478bd9Sstevel@tonic-gate 	struct dev_ops	*ops;
65437c478bd9Sstevel@tonic-gate 	dev_info_t	*ddip = NULL;
65447c478bd9Sstevel@tonic-gate 
65457c478bd9Sstevel@tonic-gate 	dip = hold_devi(major, dev_to_instance(dev), flags);
65467c478bd9Sstevel@tonic-gate 
65477c478bd9Sstevel@tonic-gate 	/*
65487c478bd9Sstevel@tonic-gate 	 * The rest of this routine is legacy support for drivers that
65497c478bd9Sstevel@tonic-gate 	 * have broken DDI_INFO_DEVT2INSTANCE implementations but may have
65507c478bd9Sstevel@tonic-gate 	 * functional DDI_INFO_DEVT2DEVINFO implementations.  This code will
65517c478bd9Sstevel@tonic-gate 	 * diagnose inconsistency and, for maximum compatibility with legacy
65527c478bd9Sstevel@tonic-gate 	 * drivers, give preference to the drivers DDI_INFO_DEVT2DEVINFO
65537c478bd9Sstevel@tonic-gate 	 * implementation over the above derived dip based the driver's
65547c478bd9Sstevel@tonic-gate 	 * DDI_INFO_DEVT2INSTANCE implementation. This legacy support should
65557c478bd9Sstevel@tonic-gate 	 * be removed when DDI_INFO_DEVT2DEVINFO is deprecated.
65567c478bd9Sstevel@tonic-gate 	 *
65577c478bd9Sstevel@tonic-gate 	 * NOTE: The following code has a race condition. DEVT2DEVINFO
65587c478bd9Sstevel@tonic-gate 	 *	returns a dip which is not held. By the time we ref ddip,
65597c478bd9Sstevel@tonic-gate 	 *	it could have been freed. The saving grace is that for
65607c478bd9Sstevel@tonic-gate 	 *	most drivers, the dip returned from hold_devi() is the
65617c478bd9Sstevel@tonic-gate 	 *	same one as the one returned by DEVT2DEVINFO, so we are
65627c478bd9Sstevel@tonic-gate 	 *	safe for drivers with the correct getinfo(9e) impl.
65637c478bd9Sstevel@tonic-gate 	 */
65647c478bd9Sstevel@tonic-gate 	if (((ops = ddi_hold_driver(major)) != NULL) &&
65657c478bd9Sstevel@tonic-gate 	    CB_DRV_INSTALLED(ops) && ops->devo_getinfo)  {
65667c478bd9Sstevel@tonic-gate 		if ((*ops->devo_getinfo)(NULL, DDI_INFO_DEVT2DEVINFO,
65677c478bd9Sstevel@tonic-gate 		    (void *)dev, (void **)&ddip) != DDI_SUCCESS)
65687c478bd9Sstevel@tonic-gate 			ddip = NULL;
65697c478bd9Sstevel@tonic-gate 	}
65707c478bd9Sstevel@tonic-gate 
65717c478bd9Sstevel@tonic-gate 	/* give preference to the driver returned DEVT2DEVINFO dip */
65727c478bd9Sstevel@tonic-gate 	if (ddip && (dip != ddip)) {
65737c478bd9Sstevel@tonic-gate #ifdef	DEBUG
65747c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: inconsistent getinfo(9E) implementation",
65757c478bd9Sstevel@tonic-gate 		    ddi_driver_name(ddip));
65767c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
65777c478bd9Sstevel@tonic-gate 		ndi_hold_devi(ddip);
65787c478bd9Sstevel@tonic-gate 		if (dip)
65797c478bd9Sstevel@tonic-gate 			ndi_rele_devi(dip);
65807c478bd9Sstevel@tonic-gate 		dip = ddip;
65817c478bd9Sstevel@tonic-gate 	}
65827c478bd9Sstevel@tonic-gate 
65837c478bd9Sstevel@tonic-gate 	if (ops)
65847c478bd9Sstevel@tonic-gate 		ddi_rele_driver(major);
65857c478bd9Sstevel@tonic-gate 
65867c478bd9Sstevel@tonic-gate 	return (dip);
65877c478bd9Sstevel@tonic-gate }
65887c478bd9Sstevel@tonic-gate 
65897c478bd9Sstevel@tonic-gate /*
65907c478bd9Sstevel@tonic-gate  * For compatibility only. Do not call this function!
65917c478bd9Sstevel@tonic-gate  */
65927c478bd9Sstevel@tonic-gate dev_info_t *
65937c478bd9Sstevel@tonic-gate e_ddi_get_dev_info(dev_t dev, vtype_t type)
65947c478bd9Sstevel@tonic-gate {
65957c478bd9Sstevel@tonic-gate 	dev_info_t *dip = NULL;
65967c478bd9Sstevel@tonic-gate 	if (getmajor(dev) >= devcnt)
65977c478bd9Sstevel@tonic-gate 		return (NULL);
65987c478bd9Sstevel@tonic-gate 
65997c478bd9Sstevel@tonic-gate 	switch (type) {
66007c478bd9Sstevel@tonic-gate 	case VCHR:
66017c478bd9Sstevel@tonic-gate 	case VBLK:
66027c478bd9Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
66037c478bd9Sstevel@tonic-gate 	default:
66047c478bd9Sstevel@tonic-gate 		break;
66057c478bd9Sstevel@tonic-gate 	}
66067c478bd9Sstevel@tonic-gate 
66077c478bd9Sstevel@tonic-gate 	/*
66087c478bd9Sstevel@tonic-gate 	 * For compatibility reasons, we can only return the dip with
66097c478bd9Sstevel@tonic-gate 	 * the driver ref count held. This is not a safe thing to do.
66107c478bd9Sstevel@tonic-gate 	 * For certain broken third-party software, we are willing
66117c478bd9Sstevel@tonic-gate 	 * to venture into unknown territory.
66127c478bd9Sstevel@tonic-gate 	 */
66137c478bd9Sstevel@tonic-gate 	if (dip) {
66147c478bd9Sstevel@tonic-gate 		(void) ndi_hold_driver(dip);
66157c478bd9Sstevel@tonic-gate 		ndi_rele_devi(dip);
66167c478bd9Sstevel@tonic-gate 	}
66177c478bd9Sstevel@tonic-gate 	return (dip);
66187c478bd9Sstevel@tonic-gate }
66197c478bd9Sstevel@tonic-gate 
66207c478bd9Sstevel@tonic-gate dev_info_t *
66217c478bd9Sstevel@tonic-gate e_ddi_hold_devi_by_path(char *path, int flags)
66227c478bd9Sstevel@tonic-gate {
66237c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
66247c478bd9Sstevel@tonic-gate 
66257c478bd9Sstevel@tonic-gate 	/* can't specify NOATTACH by path */
66267c478bd9Sstevel@tonic-gate 	ASSERT(!(flags & E_DDI_HOLD_DEVI_NOATTACH));
66277c478bd9Sstevel@tonic-gate 
66287c478bd9Sstevel@tonic-gate 	return (resolve_pathname(path, &dip, NULL, NULL) ? NULL : dip);
66297c478bd9Sstevel@tonic-gate }
66307c478bd9Sstevel@tonic-gate 
66317c478bd9Sstevel@tonic-gate void
66327c478bd9Sstevel@tonic-gate e_ddi_hold_devi(dev_info_t *dip)
66337c478bd9Sstevel@tonic-gate {
66347c478bd9Sstevel@tonic-gate 	ndi_hold_devi(dip);
66357c478bd9Sstevel@tonic-gate }
66367c478bd9Sstevel@tonic-gate 
66377c478bd9Sstevel@tonic-gate void
66387c478bd9Sstevel@tonic-gate ddi_release_devi(dev_info_t *dip)
66397c478bd9Sstevel@tonic-gate {
66407c478bd9Sstevel@tonic-gate 	ndi_rele_devi(dip);
66417c478bd9Sstevel@tonic-gate }
66427c478bd9Sstevel@tonic-gate 
66437c478bd9Sstevel@tonic-gate /*
66447c478bd9Sstevel@tonic-gate  * Associate a streams queue with a devinfo node
66457c478bd9Sstevel@tonic-gate  * NOTE: This function is called by STREAM driver's put procedure.
66467c478bd9Sstevel@tonic-gate  *	It cannot block.
66477c478bd9Sstevel@tonic-gate  */
66487c478bd9Sstevel@tonic-gate void
66497c478bd9Sstevel@tonic-gate ddi_assoc_queue_with_devi(queue_t *q, dev_info_t *dip)
66507c478bd9Sstevel@tonic-gate {
66517c478bd9Sstevel@tonic-gate 	queue_t *rq = _RD(q);
66527c478bd9Sstevel@tonic-gate 	struct stdata *stp;
66537c478bd9Sstevel@tonic-gate 	vnode_t *vp;
66547c478bd9Sstevel@tonic-gate 
66557c478bd9Sstevel@tonic-gate 	/* set flag indicating that ddi_assoc_queue_with_devi was called */
66567c478bd9Sstevel@tonic-gate 	mutex_enter(QLOCK(rq));
66577c478bd9Sstevel@tonic-gate 	rq->q_flag |= _QASSOCIATED;
66587c478bd9Sstevel@tonic-gate 	mutex_exit(QLOCK(rq));
66597c478bd9Sstevel@tonic-gate 
66607c478bd9Sstevel@tonic-gate 	/* get the vnode associated with the queue */
66617c478bd9Sstevel@tonic-gate 	stp = STREAM(rq);
66627c478bd9Sstevel@tonic-gate 	vp = stp->sd_vnode;
66637c478bd9Sstevel@tonic-gate 	ASSERT(vp);
66647c478bd9Sstevel@tonic-gate 
66657c478bd9Sstevel@tonic-gate 	/* change the hardware association of the vnode */
66667c478bd9Sstevel@tonic-gate 	spec_assoc_vp_with_devi(vp, dip);
66677c478bd9Sstevel@tonic-gate }
66687c478bd9Sstevel@tonic-gate 
66697c478bd9Sstevel@tonic-gate /*
66707c478bd9Sstevel@tonic-gate  * ddi_install_driver(name)
66717c478bd9Sstevel@tonic-gate  *
66727c478bd9Sstevel@tonic-gate  * Driver installation is currently a byproduct of driver loading.  This
66737c478bd9Sstevel@tonic-gate  * may change.
66747c478bd9Sstevel@tonic-gate  */
66757c478bd9Sstevel@tonic-gate int
66767c478bd9Sstevel@tonic-gate ddi_install_driver(char *name)
66777c478bd9Sstevel@tonic-gate {
66787c478bd9Sstevel@tonic-gate 	major_t major = ddi_name_to_major(name);
66797c478bd9Sstevel@tonic-gate 
6680a204de77Scth 	if ((major == DDI_MAJOR_T_NONE) ||
66817c478bd9Sstevel@tonic-gate 	    (ddi_hold_installed_driver(major) == NULL)) {
66827c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
66837c478bd9Sstevel@tonic-gate 	}
66847c478bd9Sstevel@tonic-gate 	ddi_rele_driver(major);
66857c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
66867c478bd9Sstevel@tonic-gate }
66877c478bd9Sstevel@tonic-gate 
66887c478bd9Sstevel@tonic-gate struct dev_ops *
66897c478bd9Sstevel@tonic-gate ddi_hold_driver(major_t major)
66907c478bd9Sstevel@tonic-gate {
66917c478bd9Sstevel@tonic-gate 	return (mod_hold_dev_by_major(major));
66927c478bd9Sstevel@tonic-gate }
66937c478bd9Sstevel@tonic-gate 
66947c478bd9Sstevel@tonic-gate 
66957c478bd9Sstevel@tonic-gate void
66967c478bd9Sstevel@tonic-gate ddi_rele_driver(major_t major)
66977c478bd9Sstevel@tonic-gate {
66987c478bd9Sstevel@tonic-gate 	mod_rele_dev_by_major(major);
66997c478bd9Sstevel@tonic-gate }
67007c478bd9Sstevel@tonic-gate 
67017c478bd9Sstevel@tonic-gate 
67027c478bd9Sstevel@tonic-gate /*
67037c478bd9Sstevel@tonic-gate  * This is called during boot to force attachment order of special dips
67047c478bd9Sstevel@tonic-gate  * dip must be referenced via ndi_hold_devi()
67057c478bd9Sstevel@tonic-gate  */
67067c478bd9Sstevel@tonic-gate int
67077c478bd9Sstevel@tonic-gate i_ddi_attach_node_hierarchy(dev_info_t *dip)
67087c478bd9Sstevel@tonic-gate {
67097c478bd9Sstevel@tonic-gate 	dev_info_t	*parent;
67105e3986cbScth 	int		ret, circ;
6711c73a93f2Sdm120769 
6712c73a93f2Sdm120769 	/*
67135e3986cbScth 	 * Recurse up until attached parent is found.
6714c73a93f2Sdm120769 	 */
67155e3986cbScth 	if (i_ddi_devi_attached(dip))
67165e3986cbScth 		return (DDI_SUCCESS);
67177c478bd9Sstevel@tonic-gate 	parent = ddi_get_parent(dip);
67187c478bd9Sstevel@tonic-gate 	if (i_ddi_attach_node_hierarchy(parent) != DDI_SUCCESS)
67197c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
67207c478bd9Sstevel@tonic-gate 
67217c478bd9Sstevel@tonic-gate 	/*
67225e3986cbScth 	 * Come top-down, expanding .conf nodes under this parent
67235e3986cbScth 	 * and driving attach.
67247c478bd9Sstevel@tonic-gate 	 */
67255e3986cbScth 	ndi_devi_enter(parent, &circ);
67267c478bd9Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(parent, 0);
67275e3986cbScth 	ret = i_ddi_attachchild(dip);
67285e3986cbScth 	ndi_devi_exit(parent, circ);
67295e3986cbScth 
67305e3986cbScth 	return (ret);
67317c478bd9Sstevel@tonic-gate }
67327c478bd9Sstevel@tonic-gate 
67337c478bd9Sstevel@tonic-gate /* keep this function static */
67347c478bd9Sstevel@tonic-gate static int
67357c478bd9Sstevel@tonic-gate attach_driver_nodes(major_t major)
67367c478bd9Sstevel@tonic-gate {
67377c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
67387c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
67397c478bd9Sstevel@tonic-gate 	int error = DDI_FAILURE;
67407c478bd9Sstevel@tonic-gate 
67417c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
67427c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
67437c478bd9Sstevel@tonic-gate 	dip = dnp->dn_head;
67447c478bd9Sstevel@tonic-gate 	while (dip) {
67457c478bd9Sstevel@tonic-gate 		ndi_hold_devi(dip);
67467c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
67477c478bd9Sstevel@tonic-gate 		if (i_ddi_attach_node_hierarchy(dip) == DDI_SUCCESS)
67487c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
67497c478bd9Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
67507c478bd9Sstevel@tonic-gate 		ndi_rele_devi(dip);
67517c478bd9Sstevel@tonic-gate 		dip = ddi_get_next(dip);
67527c478bd9Sstevel@tonic-gate 	}
67537c478bd9Sstevel@tonic-gate 	if (error == DDI_SUCCESS)
67547c478bd9Sstevel@tonic-gate 		dnp->dn_flags |= DN_NO_AUTODETACH;
67557c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
67567c478bd9Sstevel@tonic-gate 
67577c478bd9Sstevel@tonic-gate 
67587c478bd9Sstevel@tonic-gate 	return (error);
67597c478bd9Sstevel@tonic-gate }
67607c478bd9Sstevel@tonic-gate 
67617c478bd9Sstevel@tonic-gate /*
67627c478bd9Sstevel@tonic-gate  * i_ddi_attach_hw_nodes configures and attaches all hw nodes
67637c478bd9Sstevel@tonic-gate  * bound to a specific driver. This function replaces calls to
67647c478bd9Sstevel@tonic-gate  * ddi_hold_installed_driver() for drivers with no .conf
67657c478bd9Sstevel@tonic-gate  * enumerated nodes.
67667c478bd9Sstevel@tonic-gate  *
67677c478bd9Sstevel@tonic-gate  * This facility is typically called at boot time to attach
67687c478bd9Sstevel@tonic-gate  * platform-specific hardware nodes, such as ppm nodes on xcal
67697c478bd9Sstevel@tonic-gate  * and grover and keyswitch nodes on cherrystone. It does not
67707c478bd9Sstevel@tonic-gate  * deal with .conf enumerated node. Calling it beyond the boot
67717c478bd9Sstevel@tonic-gate  * process is strongly discouraged.
67727c478bd9Sstevel@tonic-gate  */
67737c478bd9Sstevel@tonic-gate int
67747c478bd9Sstevel@tonic-gate i_ddi_attach_hw_nodes(char *driver)
67757c478bd9Sstevel@tonic-gate {
67767c478bd9Sstevel@tonic-gate 	major_t major;
67777c478bd9Sstevel@tonic-gate 
67787c478bd9Sstevel@tonic-gate 	major = ddi_name_to_major(driver);
6779a204de77Scth 	if (major == DDI_MAJOR_T_NONE)
67807c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
67817c478bd9Sstevel@tonic-gate 
67827c478bd9Sstevel@tonic-gate 	return (attach_driver_nodes(major));
67837c478bd9Sstevel@tonic-gate }
67847c478bd9Sstevel@tonic-gate 
67857c478bd9Sstevel@tonic-gate /*
67867c478bd9Sstevel@tonic-gate  * i_ddi_attach_pseudo_node configures pseudo drivers which
67877c478bd9Sstevel@tonic-gate  * has a single node. The .conf nodes must be enumerated
67887c478bd9Sstevel@tonic-gate  * before calling this interface. The dip is held attached
67897c478bd9Sstevel@tonic-gate  * upon returning.
67907c478bd9Sstevel@tonic-gate  *
67917c478bd9Sstevel@tonic-gate  * This facility should only be called only at boot time
67927c478bd9Sstevel@tonic-gate  * by the I/O framework.
67937c478bd9Sstevel@tonic-gate  */
67947c478bd9Sstevel@tonic-gate dev_info_t *
67957c478bd9Sstevel@tonic-gate i_ddi_attach_pseudo_node(char *driver)
67967c478bd9Sstevel@tonic-gate {
67977c478bd9Sstevel@tonic-gate 	major_t major;
67987c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
67997c478bd9Sstevel@tonic-gate 
68007c478bd9Sstevel@tonic-gate 	major = ddi_name_to_major(driver);
6801a204de77Scth 	if (major == DDI_MAJOR_T_NONE)
68027c478bd9Sstevel@tonic-gate 		return (NULL);
68037c478bd9Sstevel@tonic-gate 
68047c478bd9Sstevel@tonic-gate 	if (attach_driver_nodes(major) != DDI_SUCCESS)
68057c478bd9Sstevel@tonic-gate 		return (NULL);
68067c478bd9Sstevel@tonic-gate 
68077c478bd9Sstevel@tonic-gate 	dip = devnamesp[major].dn_head;
68087c478bd9Sstevel@tonic-gate 	ASSERT(dip && ddi_get_next(dip) == NULL);
68097c478bd9Sstevel@tonic-gate 	ndi_hold_devi(dip);
68107c478bd9Sstevel@tonic-gate 	return (dip);
68117c478bd9Sstevel@tonic-gate }
68127c478bd9Sstevel@tonic-gate 
68137c478bd9Sstevel@tonic-gate static void
68147c478bd9Sstevel@tonic-gate diplist_to_parent_major(dev_info_t *head, char parents[])
68157c478bd9Sstevel@tonic-gate {
68167c478bd9Sstevel@tonic-gate 	major_t major;
68177c478bd9Sstevel@tonic-gate 	dev_info_t *dip, *pdip;
68187c478bd9Sstevel@tonic-gate 
68197c478bd9Sstevel@tonic-gate 	for (dip = head; dip != NULL; dip = ddi_get_next(dip)) {
68207c478bd9Sstevel@tonic-gate 		pdip = ddi_get_parent(dip);
68217c478bd9Sstevel@tonic-gate 		ASSERT(pdip);	/* disallow rootnex.conf nodes */
68227c478bd9Sstevel@tonic-gate 		major = ddi_driver_major(pdip);
6823a204de77Scth 		if ((major != DDI_MAJOR_T_NONE) && parents[major] == 0)
68247c478bd9Sstevel@tonic-gate 			parents[major] = 1;
68257c478bd9Sstevel@tonic-gate 	}
68267c478bd9Sstevel@tonic-gate }
68277c478bd9Sstevel@tonic-gate 
68287c478bd9Sstevel@tonic-gate /*
68297c478bd9Sstevel@tonic-gate  * Call ddi_hold_installed_driver() on each parent major
68307c478bd9Sstevel@tonic-gate  * and invoke mt_config_driver() to attach child major.
68317c478bd9Sstevel@tonic-gate  * This is part of the implementation of ddi_hold_installed_driver.
68327c478bd9Sstevel@tonic-gate  */
68337c478bd9Sstevel@tonic-gate static int
68347c478bd9Sstevel@tonic-gate attach_driver_by_parent(major_t child_major, char parents[])
68357c478bd9Sstevel@tonic-gate {
68367c478bd9Sstevel@tonic-gate 	major_t par_major;
68377c478bd9Sstevel@tonic-gate 	struct mt_config_handle *hdl;
68387c478bd9Sstevel@tonic-gate 	int flags = NDI_DEVI_PERSIST | NDI_NO_EVENT;
68397c478bd9Sstevel@tonic-gate 
68407c478bd9Sstevel@tonic-gate 	hdl = mt_config_init(NULL, NULL, flags, child_major, MT_CONFIG_OP,
68417c478bd9Sstevel@tonic-gate 	    NULL);
68427c478bd9Sstevel@tonic-gate 	for (par_major = 0; par_major < devcnt; par_major++) {
68437c478bd9Sstevel@tonic-gate 		/* disallow recursion on the same driver */
68447c478bd9Sstevel@tonic-gate 		if (parents[par_major] == 0 || par_major == child_major)
68457c478bd9Sstevel@tonic-gate 			continue;
68467c478bd9Sstevel@tonic-gate 		if (ddi_hold_installed_driver(par_major) == NULL)
68477c478bd9Sstevel@tonic-gate 			continue;
68487c478bd9Sstevel@tonic-gate 		hdl->mtc_parmajor = par_major;
68497c478bd9Sstevel@tonic-gate 		mt_config_driver(hdl);
68507c478bd9Sstevel@tonic-gate 		ddi_rele_driver(par_major);
68517c478bd9Sstevel@tonic-gate 	}
68527c478bd9Sstevel@tonic-gate 	(void) mt_config_fini(hdl);
68537c478bd9Sstevel@tonic-gate 
68547c478bd9Sstevel@tonic-gate 	return (i_ddi_devs_attached(child_major));
68557c478bd9Sstevel@tonic-gate }
68567c478bd9Sstevel@tonic-gate 
68577c478bd9Sstevel@tonic-gate int
68587c478bd9Sstevel@tonic-gate i_ddi_devs_attached(major_t major)
68597c478bd9Sstevel@tonic-gate {
68607c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
68617c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
68627c478bd9Sstevel@tonic-gate 	int error = DDI_FAILURE;
68637c478bd9Sstevel@tonic-gate 
68647c478bd9Sstevel@tonic-gate 	/* check for attached instances */
68657c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
68667c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
68677c478bd9Sstevel@tonic-gate 	for (dip = dnp->dn_head; dip != NULL; dip = ddi_get_next(dip)) {
6868737d277aScth 		if (i_ddi_devi_attached(dip)) {
68697c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
68707c478bd9Sstevel@tonic-gate 			break;
68717c478bd9Sstevel@tonic-gate 		}
68727c478bd9Sstevel@tonic-gate 	}
68737c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
68747c478bd9Sstevel@tonic-gate 
68757c478bd9Sstevel@tonic-gate 	return (error);
68767c478bd9Sstevel@tonic-gate }
68777c478bd9Sstevel@tonic-gate 
6878d62bc4baSyz147064 int
6879d62bc4baSyz147064 i_ddi_minor_node_count(dev_info_t *ddip, const char *node_type)
6880d62bc4baSyz147064 {
6881b9ccdc5aScth 	int			circ;
6882d62bc4baSyz147064 	struct ddi_minor_data	*dp;
6883d62bc4baSyz147064 	int			count = 0;
6884d62bc4baSyz147064 
6885b9ccdc5aScth 	ndi_devi_enter(ddip, &circ);
6886b9ccdc5aScth 	for (dp = DEVI(ddip)->devi_minor; dp != NULL; dp = dp->next) {
6887d62bc4baSyz147064 		if (strcmp(dp->ddm_node_type, node_type) == 0)
6888d62bc4baSyz147064 			count++;
6889b9ccdc5aScth 	}
6890b9ccdc5aScth 	ndi_devi_exit(ddip, circ);
6891d62bc4baSyz147064 	return (count);
6892d62bc4baSyz147064 }
6893d62bc4baSyz147064 
68947c478bd9Sstevel@tonic-gate /*
68957c478bd9Sstevel@tonic-gate  * ddi_hold_installed_driver configures and attaches all
68967c478bd9Sstevel@tonic-gate  * instances of the specified driver. To accomplish this
68977c478bd9Sstevel@tonic-gate  * it configures and attaches all possible parents of
68987c478bd9Sstevel@tonic-gate  * the driver, enumerated both in h/w nodes and in the
68997c478bd9Sstevel@tonic-gate  * driver's .conf file.
69007c478bd9Sstevel@tonic-gate  *
69017c478bd9Sstevel@tonic-gate  * NOTE: This facility is for compatibility purposes only and will
69027c478bd9Sstevel@tonic-gate  *	eventually go away. Its usage is strongly discouraged.
69037c478bd9Sstevel@tonic-gate  */
69047c478bd9Sstevel@tonic-gate static void
69057c478bd9Sstevel@tonic-gate enter_driver(struct devnames *dnp)
69067c478bd9Sstevel@tonic-gate {
69077c478bd9Sstevel@tonic-gate 	mutex_enter(&dnp->dn_lock);
69087c478bd9Sstevel@tonic-gate 	ASSERT(dnp->dn_busy_thread != curthread);
69097c478bd9Sstevel@tonic-gate 	while (dnp->dn_flags & DN_DRIVER_BUSY)
69107c478bd9Sstevel@tonic-gate 		cv_wait(&dnp->dn_wait, &dnp->dn_lock);
69117c478bd9Sstevel@tonic-gate 	dnp->dn_flags |= DN_DRIVER_BUSY;
69127c478bd9Sstevel@tonic-gate 	dnp->dn_busy_thread = curthread;
69137c478bd9Sstevel@tonic-gate 	mutex_exit(&dnp->dn_lock);
69147c478bd9Sstevel@tonic-gate }
69157c478bd9Sstevel@tonic-gate 
69167c478bd9Sstevel@tonic-gate static void
69177c478bd9Sstevel@tonic-gate exit_driver(struct devnames *dnp)
69187c478bd9Sstevel@tonic-gate {
69197c478bd9Sstevel@tonic-gate 	mutex_enter(&dnp->dn_lock);
69207c478bd9Sstevel@tonic-gate 	ASSERT(dnp->dn_busy_thread == curthread);
69217c478bd9Sstevel@tonic-gate 	dnp->dn_flags &= ~DN_DRIVER_BUSY;
69227c478bd9Sstevel@tonic-gate 	dnp->dn_busy_thread = NULL;
69237c478bd9Sstevel@tonic-gate 	cv_broadcast(&dnp->dn_wait);
69247c478bd9Sstevel@tonic-gate 	mutex_exit(&dnp->dn_lock);
69257c478bd9Sstevel@tonic-gate }
69267c478bd9Sstevel@tonic-gate 
69277c478bd9Sstevel@tonic-gate struct dev_ops *
69287c478bd9Sstevel@tonic-gate ddi_hold_installed_driver(major_t major)
69297c478bd9Sstevel@tonic-gate {
69307c478bd9Sstevel@tonic-gate 	struct dev_ops *ops;
69317c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
69327c478bd9Sstevel@tonic-gate 	char *parents;
69337c478bd9Sstevel@tonic-gate 	int error;
69347c478bd9Sstevel@tonic-gate 
69357c478bd9Sstevel@tonic-gate 	ops = ddi_hold_driver(major);
69367c478bd9Sstevel@tonic-gate 	if (ops == NULL)
69377c478bd9Sstevel@tonic-gate 		return (NULL);
69387c478bd9Sstevel@tonic-gate 
69397c478bd9Sstevel@tonic-gate 	/*
69407c478bd9Sstevel@tonic-gate 	 * Return immediately if all the attach operations associated
69417c478bd9Sstevel@tonic-gate 	 * with a ddi_hold_installed_driver() call have already been done.
69427c478bd9Sstevel@tonic-gate 	 */
69437c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
69447c478bd9Sstevel@tonic-gate 	enter_driver(dnp);
69457c478bd9Sstevel@tonic-gate 	if (dnp->dn_flags & DN_DRIVER_HELD) {
69467c478bd9Sstevel@tonic-gate 		exit_driver(dnp);
69477c478bd9Sstevel@tonic-gate 		if (i_ddi_devs_attached(major) == DDI_SUCCESS)
69487c478bd9Sstevel@tonic-gate 			return (ops);
69497c478bd9Sstevel@tonic-gate 		ddi_rele_driver(major);
69507c478bd9Sstevel@tonic-gate 		return (NULL);
69517c478bd9Sstevel@tonic-gate 	}
69527c478bd9Sstevel@tonic-gate 
69537c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
69547c478bd9Sstevel@tonic-gate 	dnp->dn_flags |= (DN_DRIVER_HELD | DN_NO_AUTODETACH);
69557c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
69567c478bd9Sstevel@tonic-gate 
69577c478bd9Sstevel@tonic-gate 	DCOMPATPRINTF((CE_CONT,
69587c478bd9Sstevel@tonic-gate 	    "ddi_hold_installed_driver: %s\n", dnp->dn_name));
69597c478bd9Sstevel@tonic-gate 
69607c478bd9Sstevel@tonic-gate 	/*
69617c478bd9Sstevel@tonic-gate 	 * When the driver has no .conf children, it is sufficient
69627c478bd9Sstevel@tonic-gate 	 * to attach existing nodes in the device tree. Nodes not
69637c478bd9Sstevel@tonic-gate 	 * enumerated by the OBP are not attached.
69647c478bd9Sstevel@tonic-gate 	 */
69657c478bd9Sstevel@tonic-gate 	if (dnp->dn_pl == NULL) {
69667c478bd9Sstevel@tonic-gate 		if (attach_driver_nodes(major) == DDI_SUCCESS) {
69677c478bd9Sstevel@tonic-gate 			exit_driver(dnp);
69687c478bd9Sstevel@tonic-gate 			return (ops);
69697c478bd9Sstevel@tonic-gate 		}
69707c478bd9Sstevel@tonic-gate 		exit_driver(dnp);
69717c478bd9Sstevel@tonic-gate 		ddi_rele_driver(major);
69727c478bd9Sstevel@tonic-gate 		return (NULL);
69737c478bd9Sstevel@tonic-gate 	}
69747c478bd9Sstevel@tonic-gate 
69757c478bd9Sstevel@tonic-gate 	/*
69767c478bd9Sstevel@tonic-gate 	 * Driver has .conf nodes. We find all possible parents
69777c478bd9Sstevel@tonic-gate 	 * and recursively all ddi_hold_installed_driver on the
69787c478bd9Sstevel@tonic-gate 	 * parent driver; then we invoke ndi_config_driver()
69797c478bd9Sstevel@tonic-gate 	 * on all possible parent node in parallel to speed up
69807c478bd9Sstevel@tonic-gate 	 * performance.
69817c478bd9Sstevel@tonic-gate 	 */
69827c478bd9Sstevel@tonic-gate 	parents = kmem_zalloc(devcnt * sizeof (char), KM_SLEEP);
69837c478bd9Sstevel@tonic-gate 
69847c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
69857c478bd9Sstevel@tonic-gate 	/* find .conf parents */
69867c478bd9Sstevel@tonic-gate 	(void) impl_parlist_to_major(dnp->dn_pl, parents);
69877c478bd9Sstevel@tonic-gate 	/* find hw node parents */
69887c478bd9Sstevel@tonic-gate 	diplist_to_parent_major(dnp->dn_head, parents);
69897c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
69907c478bd9Sstevel@tonic-gate 
69917c478bd9Sstevel@tonic-gate 	error = attach_driver_by_parent(major, parents);
69927c478bd9Sstevel@tonic-gate 	kmem_free(parents, devcnt * sizeof (char));
69937c478bd9Sstevel@tonic-gate 	if (error == DDI_SUCCESS) {
69947c478bd9Sstevel@tonic-gate 		exit_driver(dnp);
69957c478bd9Sstevel@tonic-gate 		return (ops);
69967c478bd9Sstevel@tonic-gate 	}
69977c478bd9Sstevel@tonic-gate 
69987c478bd9Sstevel@tonic-gate 	exit_driver(dnp);
69997c478bd9Sstevel@tonic-gate 	ddi_rele_driver(major);
70007c478bd9Sstevel@tonic-gate 	return (NULL);
70017c478bd9Sstevel@tonic-gate }
70027c478bd9Sstevel@tonic-gate 
70037c478bd9Sstevel@tonic-gate /*
70047c478bd9Sstevel@tonic-gate  * Default bus_config entry point for nexus drivers
70057c478bd9Sstevel@tonic-gate  */
70067c478bd9Sstevel@tonic-gate int
70077c478bd9Sstevel@tonic-gate ndi_busop_bus_config(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
70087c478bd9Sstevel@tonic-gate     void *arg, dev_info_t **child, clock_t timeout)
70097c478bd9Sstevel@tonic-gate {
70107c478bd9Sstevel@tonic-gate 	major_t major;
70117c478bd9Sstevel@tonic-gate 
70127c478bd9Sstevel@tonic-gate 	/*
70137c478bd9Sstevel@tonic-gate 	 * A timeout of 30 minutes or more is probably a mistake
70147c478bd9Sstevel@tonic-gate 	 * This is intended to catch uses where timeout is in
70157c478bd9Sstevel@tonic-gate 	 * the wrong units.  timeout must be in units of ticks.
70167c478bd9Sstevel@tonic-gate 	 */
70177c478bd9Sstevel@tonic-gate 	ASSERT(timeout < SEC_TO_TICK(1800));
70187c478bd9Sstevel@tonic-gate 
7019a204de77Scth 	major = DDI_MAJOR_T_NONE;
70207c478bd9Sstevel@tonic-gate 	switch (op) {
70217c478bd9Sstevel@tonic-gate 	case BUS_CONFIG_ONE:
70227c478bd9Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config %s timeout=%ld\n",
70237c478bd9Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
70247c478bd9Sstevel@tonic-gate 		    (char *)arg, timeout));
70257c478bd9Sstevel@tonic-gate 		return (devi_config_one(pdip, (char *)arg, child, flags,
70267c478bd9Sstevel@tonic-gate 		    timeout));
70277c478bd9Sstevel@tonic-gate 
70287c478bd9Sstevel@tonic-gate 	case BUS_CONFIG_DRIVER:
70297c478bd9Sstevel@tonic-gate 		major = (major_t)(uintptr_t)arg;
70307c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
70317c478bd9Sstevel@tonic-gate 	case BUS_CONFIG_ALL:
70327c478bd9Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config timeout=%ld\n",
70337c478bd9Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
70347c478bd9Sstevel@tonic-gate 		    timeout));
70357c478bd9Sstevel@tonic-gate 		if (timeout > 0) {
70367c478bd9Sstevel@tonic-gate 			NDI_DEBUG(flags, (CE_CONT,
70377c478bd9Sstevel@tonic-gate 			    "%s%d: bus config all timeout=%ld\n",
70387c478bd9Sstevel@tonic-gate 			    ddi_driver_name(pdip), ddi_get_instance(pdip),
70397c478bd9Sstevel@tonic-gate 			    timeout));
70407c478bd9Sstevel@tonic-gate 			delay(timeout);
70417c478bd9Sstevel@tonic-gate 		}
70427c478bd9Sstevel@tonic-gate 		return (config_immediate_children(pdip, flags, major));
70437c478bd9Sstevel@tonic-gate 
70447c478bd9Sstevel@tonic-gate 	default:
70457c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
70467c478bd9Sstevel@tonic-gate 	}
70477c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
70487c478bd9Sstevel@tonic-gate }
70497c478bd9Sstevel@tonic-gate 
70507c478bd9Sstevel@tonic-gate /*
70517c478bd9Sstevel@tonic-gate  * Default busop bus_unconfig handler for nexus drivers
70527c478bd9Sstevel@tonic-gate  */
70537c478bd9Sstevel@tonic-gate int
70547c478bd9Sstevel@tonic-gate ndi_busop_bus_unconfig(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
70557c478bd9Sstevel@tonic-gate     void *arg)
70567c478bd9Sstevel@tonic-gate {
70577c478bd9Sstevel@tonic-gate 	major_t major;
70587c478bd9Sstevel@tonic-gate 
7059a204de77Scth 	major = DDI_MAJOR_T_NONE;
70607c478bd9Sstevel@tonic-gate 	switch (op) {
70617c478bd9Sstevel@tonic-gate 	case BUS_UNCONFIG_ONE:
70627c478bd9Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig %s\n",
70637c478bd9Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
70647c478bd9Sstevel@tonic-gate 		    (char *)arg));
70657c478bd9Sstevel@tonic-gate 		return (devi_unconfig_one(pdip, (char *)arg, flags));
70667c478bd9Sstevel@tonic-gate 
70677c478bd9Sstevel@tonic-gate 	case BUS_UNCONFIG_DRIVER:
70687c478bd9Sstevel@tonic-gate 		major = (major_t)(uintptr_t)arg;
70697c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
70707c478bd9Sstevel@tonic-gate 	case BUS_UNCONFIG_ALL:
70717c478bd9Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig all\n",
70727c478bd9Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip)));
70737c478bd9Sstevel@tonic-gate 		return (unconfig_immediate_children(pdip, NULL, flags, major));
70747c478bd9Sstevel@tonic-gate 
70757c478bd9Sstevel@tonic-gate 	default:
70767c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
70777c478bd9Sstevel@tonic-gate 	}
70787c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
70797c478bd9Sstevel@tonic-gate }
70807c478bd9Sstevel@tonic-gate 
70817c478bd9Sstevel@tonic-gate /*
70827c478bd9Sstevel@tonic-gate  * dummy functions to be removed
70837c478bd9Sstevel@tonic-gate  */
70847c478bd9Sstevel@tonic-gate void
70857c478bd9Sstevel@tonic-gate impl_rem_dev_props(dev_info_t *dip)
70867c478bd9Sstevel@tonic-gate {
70877c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip))
70887c478bd9Sstevel@tonic-gate 	/* do nothing */
70897c478bd9Sstevel@tonic-gate }
70907c478bd9Sstevel@tonic-gate 
70917c478bd9Sstevel@tonic-gate /*
70927c478bd9Sstevel@tonic-gate  * Determine if a node is a leaf node. If not sure, return false (0).
70937c478bd9Sstevel@tonic-gate  */
70947c478bd9Sstevel@tonic-gate static int
70957c478bd9Sstevel@tonic-gate is_leaf_node(dev_info_t *dip)
70967c478bd9Sstevel@tonic-gate {
70977c478bd9Sstevel@tonic-gate 	major_t major = ddi_driver_major(dip);
70987c478bd9Sstevel@tonic-gate 
7099a204de77Scth 	if (major == DDI_MAJOR_T_NONE)
71007c478bd9Sstevel@tonic-gate 		return (0);
71017c478bd9Sstevel@tonic-gate 
71027c478bd9Sstevel@tonic-gate 	return (devnamesp[major].dn_flags & DN_LEAF_DRIVER);
71037c478bd9Sstevel@tonic-gate }
71047c478bd9Sstevel@tonic-gate 
71057c478bd9Sstevel@tonic-gate /*
71067c478bd9Sstevel@tonic-gate  * Multithreaded [un]configuration
71077c478bd9Sstevel@tonic-gate  */
71087c478bd9Sstevel@tonic-gate static struct mt_config_handle *
71097c478bd9Sstevel@tonic-gate mt_config_init(dev_info_t *pdip, dev_info_t **dipp, int flags,
71107c478bd9Sstevel@tonic-gate     major_t major, int op, struct brevq_node **brevqp)
71117c478bd9Sstevel@tonic-gate {
71127c478bd9Sstevel@tonic-gate 	struct mt_config_handle	*hdl = kmem_alloc(sizeof (*hdl), KM_SLEEP);
71137c478bd9Sstevel@tonic-gate 
71147c478bd9Sstevel@tonic-gate 	mutex_init(&hdl->mtc_lock, NULL, MUTEX_DEFAULT, NULL);
71157c478bd9Sstevel@tonic-gate 	cv_init(&hdl->mtc_cv, NULL, CV_DEFAULT, NULL);
71167c478bd9Sstevel@tonic-gate 	hdl->mtc_pdip = pdip;
71177c478bd9Sstevel@tonic-gate 	hdl->mtc_fdip = dipp;
7118a204de77Scth 	hdl->mtc_parmajor = DDI_MAJOR_T_NONE;
71197c478bd9Sstevel@tonic-gate 	hdl->mtc_flags = flags;
71207c478bd9Sstevel@tonic-gate 	hdl->mtc_major = major;
71217c478bd9Sstevel@tonic-gate 	hdl->mtc_thr_count = 0;
71227c478bd9Sstevel@tonic-gate 	hdl->mtc_op = op;
71237c478bd9Sstevel@tonic-gate 	hdl->mtc_error = 0;
71247c478bd9Sstevel@tonic-gate 	hdl->mtc_brevqp = brevqp;
71257c478bd9Sstevel@tonic-gate 
71267c478bd9Sstevel@tonic-gate #ifdef DEBUG
71277c478bd9Sstevel@tonic-gate 	gethrestime(&hdl->start_time);
71287c478bd9Sstevel@tonic-gate 	hdl->total_time = 0;
71297c478bd9Sstevel@tonic-gate #endif /* DEBUG */
71307c478bd9Sstevel@tonic-gate 
71317c478bd9Sstevel@tonic-gate 	return (hdl);
71327c478bd9Sstevel@tonic-gate }
71337c478bd9Sstevel@tonic-gate 
71347c478bd9Sstevel@tonic-gate #ifdef DEBUG
71357c478bd9Sstevel@tonic-gate static int
71367c478bd9Sstevel@tonic-gate time_diff_in_msec(timestruc_t start, timestruc_t end)
71377c478bd9Sstevel@tonic-gate {
71387c478bd9Sstevel@tonic-gate 	int	nsec, sec;
71397c478bd9Sstevel@tonic-gate 
71407c478bd9Sstevel@tonic-gate 	sec = end.tv_sec - start.tv_sec;
71417c478bd9Sstevel@tonic-gate 	nsec = end.tv_nsec - start.tv_nsec;
71427c478bd9Sstevel@tonic-gate 	if (nsec < 0) {
71437c478bd9Sstevel@tonic-gate 		nsec += NANOSEC;
71447c478bd9Sstevel@tonic-gate 		sec -= 1;
71457c478bd9Sstevel@tonic-gate 	}
71467c478bd9Sstevel@tonic-gate 
71477c478bd9Sstevel@tonic-gate 	return (sec * (NANOSEC >> 20) + (nsec >> 20));
71487c478bd9Sstevel@tonic-gate }
71497c478bd9Sstevel@tonic-gate 
71507c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
71517c478bd9Sstevel@tonic-gate 
71527c478bd9Sstevel@tonic-gate static int
71537c478bd9Sstevel@tonic-gate mt_config_fini(struct mt_config_handle *hdl)
71547c478bd9Sstevel@tonic-gate {
71557c478bd9Sstevel@tonic-gate 	int		rv;
71567c478bd9Sstevel@tonic-gate #ifdef DEBUG
71577c478bd9Sstevel@tonic-gate 	int		real_time;
71587c478bd9Sstevel@tonic-gate 	timestruc_t	end_time;
71597c478bd9Sstevel@tonic-gate #endif /* DEBUG */
71607c478bd9Sstevel@tonic-gate 
71617c478bd9Sstevel@tonic-gate 	mutex_enter(&hdl->mtc_lock);
71627c478bd9Sstevel@tonic-gate 	while (hdl->mtc_thr_count > 0)
71637c478bd9Sstevel@tonic-gate 		cv_wait(&hdl->mtc_cv, &hdl->mtc_lock);
71647c478bd9Sstevel@tonic-gate 	rv = hdl->mtc_error;
71657c478bd9Sstevel@tonic-gate 	mutex_exit(&hdl->mtc_lock);
71667c478bd9Sstevel@tonic-gate 
71677c478bd9Sstevel@tonic-gate #ifdef DEBUG
71687c478bd9Sstevel@tonic-gate 	gethrestime(&end_time);
71697c478bd9Sstevel@tonic-gate 	real_time = time_diff_in_msec(hdl->start_time, end_time);
71707c478bd9Sstevel@tonic-gate 	if ((ddidebug & DDI_MTCONFIG) && hdl->mtc_pdip)
71717c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE,
71727c478bd9Sstevel@tonic-gate 		    "config %s%d: total time %d msec, real time %d msec",
71737c478bd9Sstevel@tonic-gate 		    ddi_driver_name(hdl->mtc_pdip),
71747c478bd9Sstevel@tonic-gate 		    ddi_get_instance(hdl->mtc_pdip),
71757c478bd9Sstevel@tonic-gate 		    hdl->total_time, real_time);
71767c478bd9Sstevel@tonic-gate #endif /* DEBUG */
71777c478bd9Sstevel@tonic-gate 
71787c478bd9Sstevel@tonic-gate 	cv_destroy(&hdl->mtc_cv);
71797c478bd9Sstevel@tonic-gate 	mutex_destroy(&hdl->mtc_lock);
71807c478bd9Sstevel@tonic-gate 	kmem_free(hdl, sizeof (*hdl));
71817c478bd9Sstevel@tonic-gate 
71827c478bd9Sstevel@tonic-gate 	return (rv);
71837c478bd9Sstevel@tonic-gate }
71847c478bd9Sstevel@tonic-gate 
71857c478bd9Sstevel@tonic-gate struct mt_config_data {
71867c478bd9Sstevel@tonic-gate 	struct mt_config_handle	*mtc_hdl;
71877c478bd9Sstevel@tonic-gate 	dev_info_t		*mtc_dip;
71887c478bd9Sstevel@tonic-gate 	major_t			mtc_major;
71897c478bd9Sstevel@tonic-gate 	int			mtc_flags;
71907c478bd9Sstevel@tonic-gate 	struct brevq_node	*mtc_brn;
71917c478bd9Sstevel@tonic-gate 	struct mt_config_data	*mtc_next;
71927c478bd9Sstevel@tonic-gate };
71937c478bd9Sstevel@tonic-gate 
71947c478bd9Sstevel@tonic-gate static void
71957c478bd9Sstevel@tonic-gate mt_config_thread(void *arg)
71967c478bd9Sstevel@tonic-gate {
71977c478bd9Sstevel@tonic-gate 	struct mt_config_data	*mcd = (struct mt_config_data *)arg;
71987c478bd9Sstevel@tonic-gate 	struct mt_config_handle	*hdl = mcd->mtc_hdl;
71997c478bd9Sstevel@tonic-gate 	dev_info_t		*dip = mcd->mtc_dip;
72007c478bd9Sstevel@tonic-gate 	dev_info_t		*rdip, **dipp;
72017c478bd9Sstevel@tonic-gate 	major_t			major = mcd->mtc_major;
72027c478bd9Sstevel@tonic-gate 	int			flags = mcd->mtc_flags;
72037c478bd9Sstevel@tonic-gate 	int			rv = 0;
72047c478bd9Sstevel@tonic-gate 
72057c478bd9Sstevel@tonic-gate #ifdef DEBUG
72067c478bd9Sstevel@tonic-gate 	timestruc_t start_time, end_time;
72077c478bd9Sstevel@tonic-gate 	gethrestime(&start_time);
72087c478bd9Sstevel@tonic-gate #endif /* DEBUG */
72097c478bd9Sstevel@tonic-gate 
72107c478bd9Sstevel@tonic-gate 	rdip = NULL;
72117c478bd9Sstevel@tonic-gate 	dipp = hdl->mtc_fdip ? &rdip : NULL;
72127c478bd9Sstevel@tonic-gate 
72137c478bd9Sstevel@tonic-gate 	switch (hdl->mtc_op) {
72147c478bd9Sstevel@tonic-gate 	case MT_CONFIG_OP:
72157c478bd9Sstevel@tonic-gate 		rv = devi_config_common(dip, flags, major);
72167c478bd9Sstevel@tonic-gate 		break;
72177c478bd9Sstevel@tonic-gate 	case MT_UNCONFIG_OP:
72187c478bd9Sstevel@tonic-gate 		if (mcd->mtc_brn) {
72197c478bd9Sstevel@tonic-gate 			struct brevq_node *brevq = NULL;
72207c478bd9Sstevel@tonic-gate 			rv = devi_unconfig_common(dip, dipp, flags, major,
72217c478bd9Sstevel@tonic-gate 			    &brevq);
7222245c82d9Scth 			mcd->mtc_brn->brn_child = brevq;
72237c478bd9Sstevel@tonic-gate 		} else
72247c478bd9Sstevel@tonic-gate 			rv = devi_unconfig_common(dip, dipp, flags, major,
72257c478bd9Sstevel@tonic-gate 			    NULL);
72267c478bd9Sstevel@tonic-gate 		break;
72277c478bd9Sstevel@tonic-gate 	}
72287c478bd9Sstevel@tonic-gate 
72297c478bd9Sstevel@tonic-gate 	mutex_enter(&hdl->mtc_lock);
72307c478bd9Sstevel@tonic-gate #ifdef DEBUG
72317c478bd9Sstevel@tonic-gate 	gethrestime(&end_time);
72327c478bd9Sstevel@tonic-gate 	hdl->total_time += time_diff_in_msec(start_time, end_time);
72337c478bd9Sstevel@tonic-gate #endif /* DEBUG */
72345e3986cbScth 
72355e3986cbScth 	if ((rv != NDI_SUCCESS) && (hdl->mtc_error == 0)) {
72367c478bd9Sstevel@tonic-gate 		hdl->mtc_error = rv;
72375e3986cbScth #ifdef	DEBUG
7238a204de77Scth 		if ((ddidebug & DDI_DEBUG) && (major != DDI_MAJOR_T_NONE)) {
72395e3986cbScth 			char	*path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
72405e3986cbScth 
72415e3986cbScth 			(void) ddi_pathname(dip, path);
72425e3986cbScth 			cmn_err(CE_NOTE, "mt_config_thread: "
72435e3986cbScth 			    "op %d.%d.%x at %s failed %d",
72445e3986cbScth 			    hdl->mtc_op, major, flags, path, rv);
72455e3986cbScth 			kmem_free(path, MAXPATHLEN);
72465e3986cbScth 		}
72475e3986cbScth #endif	/* DEBUG */
72485e3986cbScth 	}
72495e3986cbScth 
72507c478bd9Sstevel@tonic-gate 	if (hdl->mtc_fdip && *hdl->mtc_fdip == NULL) {
72517c478bd9Sstevel@tonic-gate 		*hdl->mtc_fdip = rdip;
72527c478bd9Sstevel@tonic-gate 		rdip = NULL;
72537c478bd9Sstevel@tonic-gate 	}
72547c478bd9Sstevel@tonic-gate 
72557c478bd9Sstevel@tonic-gate 	if (rdip) {
72567c478bd9Sstevel@tonic-gate 		ASSERT(rv != NDI_SUCCESS);
72577c478bd9Sstevel@tonic-gate 		ndi_rele_devi(rdip);
72587c478bd9Sstevel@tonic-gate 	}
72597c478bd9Sstevel@tonic-gate 
72607c478bd9Sstevel@tonic-gate 	ndi_rele_devi(dip);
72613b3b7f33Sbm42561 
72623b3b7f33Sbm42561 	if (--hdl->mtc_thr_count == 0)
72633b3b7f33Sbm42561 		cv_broadcast(&hdl->mtc_cv);
72643b3b7f33Sbm42561 	mutex_exit(&hdl->mtc_lock);
72657c478bd9Sstevel@tonic-gate 	kmem_free(mcd, sizeof (*mcd));
72667c478bd9Sstevel@tonic-gate }
72677c478bd9Sstevel@tonic-gate 
72687c478bd9Sstevel@tonic-gate /*
72697c478bd9Sstevel@tonic-gate  * Multi-threaded config/unconfig of child nexus
72707c478bd9Sstevel@tonic-gate  */
72717c478bd9Sstevel@tonic-gate static void
72727c478bd9Sstevel@tonic-gate mt_config_children(struct mt_config_handle *hdl)
72737c478bd9Sstevel@tonic-gate {
72747c478bd9Sstevel@tonic-gate 	dev_info_t		*pdip = hdl->mtc_pdip;
72757c478bd9Sstevel@tonic-gate 	major_t			major = hdl->mtc_major;
72767c478bd9Sstevel@tonic-gate 	dev_info_t		*dip;
72777c478bd9Sstevel@tonic-gate 	int			circ;
7278245c82d9Scth 	struct brevq_node	*brn;
72797c478bd9Sstevel@tonic-gate 	struct mt_config_data	*mcd_head = NULL;
72807c478bd9Sstevel@tonic-gate 	struct mt_config_data	*mcd_tail = NULL;
72817c478bd9Sstevel@tonic-gate 	struct mt_config_data	*mcd;
72827c478bd9Sstevel@tonic-gate #ifdef DEBUG
72837c478bd9Sstevel@tonic-gate 	timestruc_t		end_time;
72847c478bd9Sstevel@tonic-gate 
72857c478bd9Sstevel@tonic-gate 	/* Update total_time in handle */
72867c478bd9Sstevel@tonic-gate 	gethrestime(&end_time);
72877c478bd9Sstevel@tonic-gate 	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
72887c478bd9Sstevel@tonic-gate #endif
72897c478bd9Sstevel@tonic-gate 
72907c478bd9Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
72917c478bd9Sstevel@tonic-gate 	dip = ddi_get_child(pdip);
72927c478bd9Sstevel@tonic-gate 	while (dip) {
72937c478bd9Sstevel@tonic-gate 		if (hdl->mtc_op == MT_UNCONFIG_OP && hdl->mtc_brevqp &&
72947c478bd9Sstevel@tonic-gate 		    !(DEVI_EVREMOVE(dip)) &&
72957c478bd9Sstevel@tonic-gate 		    i_ddi_node_state(dip) >= DS_INITIALIZED) {
72967c478bd9Sstevel@tonic-gate 			/*
72977c478bd9Sstevel@tonic-gate 			 * Enqueue this dip's deviname.
72987c478bd9Sstevel@tonic-gate 			 * No need to hold a lock while enqueuing since this
72997c478bd9Sstevel@tonic-gate 			 * is the only thread doing the enqueue and no one
73007c478bd9Sstevel@tonic-gate 			 * walks the queue while we are in multithreaded
73017c478bd9Sstevel@tonic-gate 			 * unconfiguration.
73027c478bd9Sstevel@tonic-gate 			 */
73037c478bd9Sstevel@tonic-gate 			brn = brevq_enqueue(hdl->mtc_brevqp, dip, NULL);
7304245c82d9Scth 		} else
7305245c82d9Scth 			brn = NULL;
73067c478bd9Sstevel@tonic-gate 
73077c478bd9Sstevel@tonic-gate 		/*
73087c478bd9Sstevel@tonic-gate 		 * Hold the child that we are processing so he does not get
73097c478bd9Sstevel@tonic-gate 		 * removed. The corrisponding ndi_rele_devi() for children
73107c478bd9Sstevel@tonic-gate 		 * that are not being skipped is done at the end of
73117c478bd9Sstevel@tonic-gate 		 * mt_config_thread().
73127c478bd9Sstevel@tonic-gate 		 */
73137c478bd9Sstevel@tonic-gate 		ndi_hold_devi(dip);
73147c478bd9Sstevel@tonic-gate 
73157c478bd9Sstevel@tonic-gate 		/*
73167c478bd9Sstevel@tonic-gate 		 * skip leaf nodes and (for configure) nodes not
73177c478bd9Sstevel@tonic-gate 		 * fully attached.
73187c478bd9Sstevel@tonic-gate 		 */
73197c478bd9Sstevel@tonic-gate 		if (is_leaf_node(dip) ||
73207c478bd9Sstevel@tonic-gate 		    (hdl->mtc_op == MT_CONFIG_OP &&
73217c478bd9Sstevel@tonic-gate 		    i_ddi_node_state(dip) < DS_READY)) {
73227c478bd9Sstevel@tonic-gate 			ndi_rele_devi(dip);
73237c478bd9Sstevel@tonic-gate 			dip = ddi_get_next_sibling(dip);
73247c478bd9Sstevel@tonic-gate 			continue;
73257c478bd9Sstevel@tonic-gate 		}
73267c478bd9Sstevel@tonic-gate 
73277c478bd9Sstevel@tonic-gate 		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
73287c478bd9Sstevel@tonic-gate 		mcd->mtc_dip = dip;
73297c478bd9Sstevel@tonic-gate 		mcd->mtc_hdl = hdl;
73307c478bd9Sstevel@tonic-gate 		mcd->mtc_brn = brn;
73317c478bd9Sstevel@tonic-gate 
73327c478bd9Sstevel@tonic-gate 		/*
73337c478bd9Sstevel@tonic-gate 		 * Switch a 'driver' operation to an 'all' operation below a
73347c478bd9Sstevel@tonic-gate 		 * node bound to the driver.
73357c478bd9Sstevel@tonic-gate 		 */
7336a204de77Scth 		if ((major == DDI_MAJOR_T_NONE) ||
7337a204de77Scth 		    (major == ddi_driver_major(dip)))
7338a204de77Scth 			mcd->mtc_major = DDI_MAJOR_T_NONE;
73397c478bd9Sstevel@tonic-gate 		else
73407c478bd9Sstevel@tonic-gate 			mcd->mtc_major = major;
73417c478bd9Sstevel@tonic-gate 
73427c478bd9Sstevel@tonic-gate 		/*
73437c478bd9Sstevel@tonic-gate 		 * The unconfig-driver to unconfig-all conversion above
73447c478bd9Sstevel@tonic-gate 		 * constitutes an autodetach for NDI_DETACH_DRIVER calls,
73457c478bd9Sstevel@tonic-gate 		 * set NDI_AUTODETACH.
73467c478bd9Sstevel@tonic-gate 		 */
73477c478bd9Sstevel@tonic-gate 		mcd->mtc_flags = hdl->mtc_flags;
73487c478bd9Sstevel@tonic-gate 		if ((mcd->mtc_flags & NDI_DETACH_DRIVER) &&
73497c478bd9Sstevel@tonic-gate 		    (hdl->mtc_op == MT_UNCONFIG_OP) &&
73507c478bd9Sstevel@tonic-gate 		    (major == ddi_driver_major(pdip)))
73517c478bd9Sstevel@tonic-gate 			mcd->mtc_flags |= NDI_AUTODETACH;
73527c478bd9Sstevel@tonic-gate 
73537c478bd9Sstevel@tonic-gate 		mutex_enter(&hdl->mtc_lock);
73547c478bd9Sstevel@tonic-gate 		hdl->mtc_thr_count++;
73557c478bd9Sstevel@tonic-gate 		mutex_exit(&hdl->mtc_lock);
73567c478bd9Sstevel@tonic-gate 
73577c478bd9Sstevel@tonic-gate 		/*
73587c478bd9Sstevel@tonic-gate 		 * Add to end of list to process after ndi_devi_exit to avoid
73597c478bd9Sstevel@tonic-gate 		 * locking differences depending on value of mtc_off.
73607c478bd9Sstevel@tonic-gate 		 */
73617c478bd9Sstevel@tonic-gate 		mcd->mtc_next = NULL;
73627c478bd9Sstevel@tonic-gate 		if (mcd_head == NULL)
73637c478bd9Sstevel@tonic-gate 			mcd_head = mcd;
73647c478bd9Sstevel@tonic-gate 		else
73657c478bd9Sstevel@tonic-gate 			mcd_tail->mtc_next = mcd;
73667c478bd9Sstevel@tonic-gate 		mcd_tail = mcd;
73677c478bd9Sstevel@tonic-gate 
73687c478bd9Sstevel@tonic-gate 		dip = ddi_get_next_sibling(dip);
73697c478bd9Sstevel@tonic-gate 	}
73707c478bd9Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
73717c478bd9Sstevel@tonic-gate 
73727c478bd9Sstevel@tonic-gate 	/* go through the list of held children */
73737c478bd9Sstevel@tonic-gate 	for (mcd = mcd_head; mcd; mcd = mcd_head) {
73747c478bd9Sstevel@tonic-gate 		mcd_head = mcd->mtc_next;
73755e3986cbScth 		if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
73767c478bd9Sstevel@tonic-gate 			mt_config_thread(mcd);
73777c478bd9Sstevel@tonic-gate 		else
73787c478bd9Sstevel@tonic-gate 			(void) thread_create(NULL, 0, mt_config_thread, mcd,
73797c478bd9Sstevel@tonic-gate 			    0, &p0, TS_RUN, minclsyspri);
73807c478bd9Sstevel@tonic-gate 	}
73817c478bd9Sstevel@tonic-gate }
73827c478bd9Sstevel@tonic-gate 
73837c478bd9Sstevel@tonic-gate static void
73847c478bd9Sstevel@tonic-gate mt_config_driver(struct mt_config_handle *hdl)
73857c478bd9Sstevel@tonic-gate {
73867c478bd9Sstevel@tonic-gate 	major_t			par_major = hdl->mtc_parmajor;
73877c478bd9Sstevel@tonic-gate 	major_t			major = hdl->mtc_major;
73887c478bd9Sstevel@tonic-gate 	struct devnames		*dnp = &devnamesp[par_major];
73897c478bd9Sstevel@tonic-gate 	dev_info_t		*dip;
73907c478bd9Sstevel@tonic-gate 	struct mt_config_data	*mcd_head = NULL;
73917c478bd9Sstevel@tonic-gate 	struct mt_config_data	*mcd_tail = NULL;
73927c478bd9Sstevel@tonic-gate 	struct mt_config_data	*mcd;
73937c478bd9Sstevel@tonic-gate #ifdef DEBUG
73947c478bd9Sstevel@tonic-gate 	timestruc_t		end_time;
73957c478bd9Sstevel@tonic-gate 
73967c478bd9Sstevel@tonic-gate 	/* Update total_time in handle */
73977c478bd9Sstevel@tonic-gate 	gethrestime(&end_time);
73987c478bd9Sstevel@tonic-gate 	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
73997c478bd9Sstevel@tonic-gate #endif
7400a204de77Scth 	ASSERT(par_major != DDI_MAJOR_T_NONE);
7401a204de77Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
74027c478bd9Sstevel@tonic-gate 
74037c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
74047c478bd9Sstevel@tonic-gate 	dip = devnamesp[par_major].dn_head;
74057c478bd9Sstevel@tonic-gate 	while (dip) {
74067c478bd9Sstevel@tonic-gate 		/*
74077c478bd9Sstevel@tonic-gate 		 * Hold the child that we are processing so he does not get
74087c478bd9Sstevel@tonic-gate 		 * removed. The corrisponding ndi_rele_devi() for children
74097c478bd9Sstevel@tonic-gate 		 * that are not being skipped is done at the end of
74107c478bd9Sstevel@tonic-gate 		 * mt_config_thread().
74117c478bd9Sstevel@tonic-gate 		 */
74127c478bd9Sstevel@tonic-gate 		ndi_hold_devi(dip);
74137c478bd9Sstevel@tonic-gate 
74147c478bd9Sstevel@tonic-gate 		/* skip leaf nodes and nodes not fully attached */
7415737d277aScth 		if (!i_ddi_devi_attached(dip) || is_leaf_node(dip)) {
74167c478bd9Sstevel@tonic-gate 			ndi_rele_devi(dip);
74177c478bd9Sstevel@tonic-gate 			dip = ddi_get_next(dip);
74187c478bd9Sstevel@tonic-gate 			continue;
74197c478bd9Sstevel@tonic-gate 		}
74207c478bd9Sstevel@tonic-gate 
74217c478bd9Sstevel@tonic-gate 		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
74227c478bd9Sstevel@tonic-gate 		mcd->mtc_dip = dip;
74237c478bd9Sstevel@tonic-gate 		mcd->mtc_hdl = hdl;
74247c478bd9Sstevel@tonic-gate 		mcd->mtc_major = major;
74257c478bd9Sstevel@tonic-gate 		mcd->mtc_flags = hdl->mtc_flags;
74267c478bd9Sstevel@tonic-gate 
74277c478bd9Sstevel@tonic-gate 		mutex_enter(&hdl->mtc_lock);
74287c478bd9Sstevel@tonic-gate 		hdl->mtc_thr_count++;
74297c478bd9Sstevel@tonic-gate 		mutex_exit(&hdl->mtc_lock);
74307c478bd9Sstevel@tonic-gate 
74317c478bd9Sstevel@tonic-gate 		/*
74327c478bd9Sstevel@tonic-gate 		 * Add to end of list to process after UNLOCK_DEV_OPS to avoid
74337c478bd9Sstevel@tonic-gate 		 * locking differences depending on value of mtc_off.
74347c478bd9Sstevel@tonic-gate 		 */
74357c478bd9Sstevel@tonic-gate 		mcd->mtc_next = NULL;
74367c478bd9Sstevel@tonic-gate 		if (mcd_head == NULL)
74377c478bd9Sstevel@tonic-gate 			mcd_head = mcd;
74387c478bd9Sstevel@tonic-gate 		else
74397c478bd9Sstevel@tonic-gate 			mcd_tail->mtc_next = mcd;
74407c478bd9Sstevel@tonic-gate 		mcd_tail = mcd;
74417c478bd9Sstevel@tonic-gate 
74427c478bd9Sstevel@tonic-gate 		dip = ddi_get_next(dip);
74437c478bd9Sstevel@tonic-gate 	}
74447c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
74457c478bd9Sstevel@tonic-gate 
74467c478bd9Sstevel@tonic-gate 	/* go through the list of held children */
74477c478bd9Sstevel@tonic-gate 	for (mcd = mcd_head; mcd; mcd = mcd_head) {
74487c478bd9Sstevel@tonic-gate 		mcd_head = mcd->mtc_next;
74495e3986cbScth 		if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
74507c478bd9Sstevel@tonic-gate 			mt_config_thread(mcd);
74517c478bd9Sstevel@tonic-gate 		else
74527c478bd9Sstevel@tonic-gate 			(void) thread_create(NULL, 0, mt_config_thread, mcd,
74537c478bd9Sstevel@tonic-gate 			    0, &p0, TS_RUN, minclsyspri);
74547c478bd9Sstevel@tonic-gate 	}
74557c478bd9Sstevel@tonic-gate }
74567c478bd9Sstevel@tonic-gate 
74577c478bd9Sstevel@tonic-gate /*
74587c478bd9Sstevel@tonic-gate  * Given the nodeid for a persistent (PROM or SID) node, return
74597c478bd9Sstevel@tonic-gate  * the corresponding devinfo node
74607c478bd9Sstevel@tonic-gate  * NOTE: This function will return NULL for .conf nodeids.
74617c478bd9Sstevel@tonic-gate  */
74627c478bd9Sstevel@tonic-gate dev_info_t *
7463fa9e4066Sahrens e_ddi_nodeid_to_dip(pnode_t nodeid)
74647c478bd9Sstevel@tonic-gate {
74657c478bd9Sstevel@tonic-gate 	dev_info_t		*dip = NULL;
74667c478bd9Sstevel@tonic-gate 	struct devi_nodeid	*prev, *elem;
74677c478bd9Sstevel@tonic-gate 
74687c478bd9Sstevel@tonic-gate 	mutex_enter(&devimap->dno_lock);
74697c478bd9Sstevel@tonic-gate 
74707c478bd9Sstevel@tonic-gate 	prev = NULL;
74717c478bd9Sstevel@tonic-gate 	for (elem = devimap->dno_head; elem; elem = elem->next) {
74727c478bd9Sstevel@tonic-gate 		if (elem->nodeid == nodeid) {
74737c478bd9Sstevel@tonic-gate 			ndi_hold_devi(elem->dip);
74747c478bd9Sstevel@tonic-gate 			dip = elem->dip;
74757c478bd9Sstevel@tonic-gate 			break;
74767c478bd9Sstevel@tonic-gate 		}
74777c478bd9Sstevel@tonic-gate 		prev = elem;
74787c478bd9Sstevel@tonic-gate 	}
74797c478bd9Sstevel@tonic-gate 
74807c478bd9Sstevel@tonic-gate 	/*
74817c478bd9Sstevel@tonic-gate 	 * Move to head for faster lookup next time
74827c478bd9Sstevel@tonic-gate 	 */
74837c478bd9Sstevel@tonic-gate 	if (elem && prev) {
74847c478bd9Sstevel@tonic-gate 		prev->next = elem->next;
74857c478bd9Sstevel@tonic-gate 		elem->next = devimap->dno_head;
74867c478bd9Sstevel@tonic-gate 		devimap->dno_head = elem;
74877c478bd9Sstevel@tonic-gate 	}
74887c478bd9Sstevel@tonic-gate 
74897c478bd9Sstevel@tonic-gate 	mutex_exit(&devimap->dno_lock);
74907c478bd9Sstevel@tonic-gate 	return (dip);
74917c478bd9Sstevel@tonic-gate }
74927c478bd9Sstevel@tonic-gate 
74937c478bd9Sstevel@tonic-gate static void
74947c478bd9Sstevel@tonic-gate free_cache_task(void *arg)
74957c478bd9Sstevel@tonic-gate {
74967c478bd9Sstevel@tonic-gate 	ASSERT(arg == NULL);
74977c478bd9Sstevel@tonic-gate 
74987c478bd9Sstevel@tonic-gate 	mutex_enter(&di_cache.cache_lock);
74997c478bd9Sstevel@tonic-gate 
75007c478bd9Sstevel@tonic-gate 	/*
75017c478bd9Sstevel@tonic-gate 	 * The cache can be invalidated without holding the lock
75027c478bd9Sstevel@tonic-gate 	 * but it can be made valid again only while the lock is held.
75037c478bd9Sstevel@tonic-gate 	 * So if the cache is invalid when the lock is held, it will
75047c478bd9Sstevel@tonic-gate 	 * stay invalid until lock is released.
75057c478bd9Sstevel@tonic-gate 	 */
75067c478bd9Sstevel@tonic-gate 	if (!di_cache.cache_valid)
75077c478bd9Sstevel@tonic-gate 		i_ddi_di_cache_free(&di_cache);
75087c478bd9Sstevel@tonic-gate 
75097c478bd9Sstevel@tonic-gate 	mutex_exit(&di_cache.cache_lock);
75107c478bd9Sstevel@tonic-gate 
75117c478bd9Sstevel@tonic-gate 	if (di_cache_debug)
75127c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "system_taskq: di_cache freed");
75137c478bd9Sstevel@tonic-gate }
75147c478bd9Sstevel@tonic-gate 
75157c478bd9Sstevel@tonic-gate extern int modrootloaded;
75167c478bd9Sstevel@tonic-gate 
75177c478bd9Sstevel@tonic-gate void
75187c478bd9Sstevel@tonic-gate i_ddi_di_cache_free(struct di_cache *cache)
75197c478bd9Sstevel@tonic-gate {
75207c478bd9Sstevel@tonic-gate 	int	error;
7521*19397407SSherry Moore 	extern int sys_shutdown;
75227c478bd9Sstevel@tonic-gate 
75237c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&cache->cache_lock));
75247c478bd9Sstevel@tonic-gate 
75257c478bd9Sstevel@tonic-gate 	if (cache->cache_size) {
75267c478bd9Sstevel@tonic-gate 		ASSERT(cache->cache_size > 0);
75277c478bd9Sstevel@tonic-gate 		ASSERT(cache->cache_data);
75287c478bd9Sstevel@tonic-gate 
75297c478bd9Sstevel@tonic-gate 		kmem_free(cache->cache_data, cache->cache_size);
75307c478bd9Sstevel@tonic-gate 		cache->cache_data = NULL;
75317c478bd9Sstevel@tonic-gate 		cache->cache_size = 0;
75327c478bd9Sstevel@tonic-gate 
75337c478bd9Sstevel@tonic-gate 		if (di_cache_debug)
75347c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "i_ddi_di_cache_free: freed cachemem");
75357c478bd9Sstevel@tonic-gate 	} else {
75367c478bd9Sstevel@tonic-gate 		ASSERT(cache->cache_data == NULL);
75377c478bd9Sstevel@tonic-gate 		if (di_cache_debug)
75387c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "i_ddi_di_cache_free: NULL cache");
75397c478bd9Sstevel@tonic-gate 	}
75407c478bd9Sstevel@tonic-gate 
7541c3b4ae18SJerry Gilliam 	if (!modrootloaded || rootvp == NULL ||
7542c3b4ae18SJerry Gilliam 	    vn_is_readonly(rootvp) || sys_shutdown) {
75437c478bd9Sstevel@tonic-gate 		if (di_cache_debug) {
75447c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "/ not mounted/RDONLY. Skip unlink");
75457c478bd9Sstevel@tonic-gate 		}
75467c478bd9Sstevel@tonic-gate 		return;
75477c478bd9Sstevel@tonic-gate 	}
75487c478bd9Sstevel@tonic-gate 
75497c478bd9Sstevel@tonic-gate 	error = vn_remove(DI_CACHE_FILE, UIO_SYSSPACE, RMFILE);
75507c478bd9Sstevel@tonic-gate 	if (di_cache_debug && error && error != ENOENT) {
75517c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: unlink failed: %d", DI_CACHE_FILE, error);
75527c478bd9Sstevel@tonic-gate 	} else if (di_cache_debug && !error) {
75537c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "i_ddi_di_cache_free: unlinked cache file");
75547c478bd9Sstevel@tonic-gate 	}
75557c478bd9Sstevel@tonic-gate }
75567c478bd9Sstevel@tonic-gate 
75577c478bd9Sstevel@tonic-gate void
75587c478bd9Sstevel@tonic-gate i_ddi_di_cache_invalidate(int kmflag)
75597c478bd9Sstevel@tonic-gate {
7560e37c6c37Scth 	int	cache_valid;
75617c478bd9Sstevel@tonic-gate 
75627c478bd9Sstevel@tonic-gate 	if (!modrootloaded || !i_ddi_io_initialized()) {
75637c478bd9Sstevel@tonic-gate 		if (di_cache_debug)
75647c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "I/O not inited. Skipping invalidate");
75657c478bd9Sstevel@tonic-gate 		return;
75667c478bd9Sstevel@tonic-gate 	}
75677c478bd9Sstevel@tonic-gate 
7568e37c6c37Scth 	/* Increment devtree generation number. */
7569facf4a8dSllai1 	atomic_inc_ulong(&devtree_gen);
75707c478bd9Sstevel@tonic-gate 
7571e37c6c37Scth 	/* Invalidate the in-core cache and dispatch free on valid->invalid */
7572e37c6c37Scth 	cache_valid = atomic_swap_uint(&di_cache.cache_valid, 0);
7573e37c6c37Scth 	if (cache_valid) {
7574e37c6c37Scth 		(void) taskq_dispatch(system_taskq, free_cache_task, NULL,
7575e37c6c37Scth 		    (kmflag == KM_SLEEP) ? TQ_SLEEP : TQ_NOSLEEP);
7576e37c6c37Scth 	}
75777c478bd9Sstevel@tonic-gate 
75787c478bd9Sstevel@tonic-gate 	if (di_cache_debug) {
75797c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "invalidation with km_flag: %s",
75807c478bd9Sstevel@tonic-gate 		    kmflag == KM_SLEEP ? "KM_SLEEP" : "KM_NOSLEEP");
75817c478bd9Sstevel@tonic-gate 	}
75827c478bd9Sstevel@tonic-gate }
75837c478bd9Sstevel@tonic-gate 
75847c478bd9Sstevel@tonic-gate 
75857c478bd9Sstevel@tonic-gate static void
75867c478bd9Sstevel@tonic-gate i_bind_vhci_node(dev_info_t *dip)
75877c478bd9Sstevel@tonic-gate {
7588f4da9be0Scth 	DEVI(dip)->devi_major = ddi_name_to_major(ddi_node_name(dip));
75897c478bd9Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_BOUND);
75907c478bd9Sstevel@tonic-gate }
75917c478bd9Sstevel@tonic-gate 
75927c478bd9Sstevel@tonic-gate static char vhci_node_addr[2];
75937c478bd9Sstevel@tonic-gate 
75947c478bd9Sstevel@tonic-gate static int
75957c478bd9Sstevel@tonic-gate i_init_vhci_node(dev_info_t *dip)
75967c478bd9Sstevel@tonic-gate {
75977c478bd9Sstevel@tonic-gate 	add_global_props(dip);
75987c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
75997c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_ops == NULL)
76007c478bd9Sstevel@tonic-gate 		return (-1);
76017c478bd9Sstevel@tonic-gate 
76027c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
76037c478bd9Sstevel@tonic-gate 	e_ddi_keep_instance(dip);
76047c478bd9Sstevel@tonic-gate 	vhci_node_addr[0]	= '\0';
76057c478bd9Sstevel@tonic-gate 	ddi_set_name_addr(dip, vhci_node_addr);
76067c478bd9Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_INITIALIZED);
76077c478bd9Sstevel@tonic-gate 	return (0);
76087c478bd9Sstevel@tonic-gate }
76097c478bd9Sstevel@tonic-gate 
76107c478bd9Sstevel@tonic-gate static void
76117c478bd9Sstevel@tonic-gate i_link_vhci_node(dev_info_t *dip)
76127c478bd9Sstevel@tonic-gate {
76139d3d2ed0Shiremath 	ASSERT(MUTEX_HELD(&global_vhci_lock));
76149d3d2ed0Shiremath 
76157c478bd9Sstevel@tonic-gate 	/*
76167c478bd9Sstevel@tonic-gate 	 * scsi_vhci should be kept left most of the device tree.
76177c478bd9Sstevel@tonic-gate 	 */
76187c478bd9Sstevel@tonic-gate 	if (scsi_vhci_dip) {
76197c478bd9Sstevel@tonic-gate 		DEVI(dip)->devi_sibling = DEVI(scsi_vhci_dip)->devi_sibling;
76207c478bd9Sstevel@tonic-gate 		DEVI(scsi_vhci_dip)->devi_sibling = DEVI(dip);
76217c478bd9Sstevel@tonic-gate 	} else {
76227c478bd9Sstevel@tonic-gate 		DEVI(dip)->devi_sibling = DEVI(top_devinfo)->devi_child;
76237c478bd9Sstevel@tonic-gate 		DEVI(top_devinfo)->devi_child = DEVI(dip);
76247c478bd9Sstevel@tonic-gate 	}
76257c478bd9Sstevel@tonic-gate }
76267c478bd9Sstevel@tonic-gate 
76277c478bd9Sstevel@tonic-gate 
76287c478bd9Sstevel@tonic-gate /*
76297c478bd9Sstevel@tonic-gate  * This a special routine to enumerate vhci node (child of rootnex
76307c478bd9Sstevel@tonic-gate  * node) without holding the ndi_devi_enter() lock. The device node
76317c478bd9Sstevel@tonic-gate  * is allocated, initialized and brought into DS_READY state before
76327c478bd9Sstevel@tonic-gate  * inserting into the device tree. The VHCI node is handcrafted
76337c478bd9Sstevel@tonic-gate  * here to bring the node to DS_READY, similar to rootnex node.
76347c478bd9Sstevel@tonic-gate  *
76357c478bd9Sstevel@tonic-gate  * The global_vhci_lock protects linking the node into the device
76367c478bd9Sstevel@tonic-gate  * as same lock is held before linking/unlinking any direct child
76377c478bd9Sstevel@tonic-gate  * of rootnex children.
76387c478bd9Sstevel@tonic-gate  *
76397c478bd9Sstevel@tonic-gate  * This routine is a workaround to handle a possible deadlock
76407c478bd9Sstevel@tonic-gate  * that occurs while trying to enumerate node in a different sub-tree
76417c478bd9Sstevel@tonic-gate  * during _init/_attach entry points.
76427c478bd9Sstevel@tonic-gate  */
76437c478bd9Sstevel@tonic-gate /*ARGSUSED*/
76447c478bd9Sstevel@tonic-gate dev_info_t *
76457c478bd9Sstevel@tonic-gate ndi_devi_config_vhci(char *drvname, int flags)
76467c478bd9Sstevel@tonic-gate {
76477c478bd9Sstevel@tonic-gate 	struct devnames		*dnp;
76487c478bd9Sstevel@tonic-gate 	dev_info_t		*dip;
76497c478bd9Sstevel@tonic-gate 	major_t			major = ddi_name_to_major(drvname);
76507c478bd9Sstevel@tonic-gate 
76517c478bd9Sstevel@tonic-gate 	if (major == -1)
76527c478bd9Sstevel@tonic-gate 		return (NULL);
76537c478bd9Sstevel@tonic-gate 
76547c478bd9Sstevel@tonic-gate 	/* Make sure we create the VHCI node only once */
76557c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
76567c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
76577c478bd9Sstevel@tonic-gate 	if (dnp->dn_head) {
76587c478bd9Sstevel@tonic-gate 		dip = dnp->dn_head;
76597c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
76607c478bd9Sstevel@tonic-gate 		return (dip);
76617c478bd9Sstevel@tonic-gate 	}
76627c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
76637c478bd9Sstevel@tonic-gate 
76647c478bd9Sstevel@tonic-gate 	/* Allocate the VHCI node */
76657c478bd9Sstevel@tonic-gate 	ndi_devi_alloc_sleep(top_devinfo, drvname, DEVI_SID_NODEID, &dip);
76667c478bd9Sstevel@tonic-gate 	ndi_hold_devi(dip);
76677c478bd9Sstevel@tonic-gate 
76687c478bd9Sstevel@tonic-gate 	/* Mark the node as VHCI */
76697c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_node_attributes |= DDI_VHCI_NODE;
76707c478bd9Sstevel@tonic-gate 
76717c478bd9Sstevel@tonic-gate 	i_ddi_add_devimap(dip);
76727c478bd9Sstevel@tonic-gate 	i_bind_vhci_node(dip);
76737c478bd9Sstevel@tonic-gate 	if (i_init_vhci_node(dip) == -1) {
76747c478bd9Sstevel@tonic-gate 		ndi_rele_devi(dip);
76757c478bd9Sstevel@tonic-gate 		(void) ndi_devi_free(dip);
76767c478bd9Sstevel@tonic-gate 		return (NULL);
76777c478bd9Sstevel@tonic-gate 	}
76787c478bd9Sstevel@tonic-gate 
767916747f41Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
76807c478bd9Sstevel@tonic-gate 	DEVI_SET_ATTACHING(dip);
768116747f41Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
768216747f41Scth 
76837c478bd9Sstevel@tonic-gate 	if (devi_attach(dip, DDI_ATTACH) != DDI_SUCCESS) {
76847c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "Could not attach %s driver", drvname);
76857c478bd9Sstevel@tonic-gate 		e_ddi_free_instance(dip, vhci_node_addr);
76867c478bd9Sstevel@tonic-gate 		ndi_rele_devi(dip);
76877c478bd9Sstevel@tonic-gate 		(void) ndi_devi_free(dip);
76887c478bd9Sstevel@tonic-gate 		return (NULL);
76897c478bd9Sstevel@tonic-gate 	}
769016747f41Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
76917c478bd9Sstevel@tonic-gate 	DEVI_CLR_ATTACHING(dip);
769216747f41Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
76937c478bd9Sstevel@tonic-gate 
76949d3d2ed0Shiremath 	mutex_enter(&global_vhci_lock);
76957c478bd9Sstevel@tonic-gate 	i_link_vhci_node(dip);
76969d3d2ed0Shiremath 	mutex_exit(&global_vhci_lock);
76977c478bd9Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_READY);
76987c478bd9Sstevel@tonic-gate 
76997c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
77007c478bd9Sstevel@tonic-gate 	dnp->dn_flags |= DN_DRIVER_HELD;
77017c478bd9Sstevel@tonic-gate 	dnp->dn_head = dip;
77027c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
77037c478bd9Sstevel@tonic-gate 
77047c478bd9Sstevel@tonic-gate 	i_ndi_devi_report_status_change(dip, NULL);
77057c478bd9Sstevel@tonic-gate 
77067c478bd9Sstevel@tonic-gate 	return (dip);
77077c478bd9Sstevel@tonic-gate }
77087c478bd9Sstevel@tonic-gate 
77097c478bd9Sstevel@tonic-gate /*
77107c478bd9Sstevel@tonic-gate  * ibt_hw_is_present() returns 0 when there is no IB hardware actively
77117c478bd9Sstevel@tonic-gate  * running.  This is primarily useful for modules like rpcmod which
77127c478bd9Sstevel@tonic-gate  * needs a quick check to decide whether or not it should try to use
77137c478bd9Sstevel@tonic-gate  * InfiniBand
77147c478bd9Sstevel@tonic-gate  */
77157c478bd9Sstevel@tonic-gate int ib_hw_status = 0;
77167c478bd9Sstevel@tonic-gate int
77177c478bd9Sstevel@tonic-gate ibt_hw_is_present()
77187c478bd9Sstevel@tonic-gate {
77197c478bd9Sstevel@tonic-gate 	return (ib_hw_status);
77207c478bd9Sstevel@tonic-gate }
772125e8c5aaSvikram 
772225e8c5aaSvikram /*
772325e8c5aaSvikram  * ASSERT that constraint flag is not set and then set the "retire attempt"
772425e8c5aaSvikram  * flag.
772525e8c5aaSvikram  */
772625e8c5aaSvikram int
772725e8c5aaSvikram e_ddi_mark_retiring(dev_info_t *dip, void *arg)
772825e8c5aaSvikram {
772925e8c5aaSvikram 	char	**cons_array = (char **)arg;
773025e8c5aaSvikram 	char	*path;
773125e8c5aaSvikram 	int	constraint;
773225e8c5aaSvikram 	int	i;
773325e8c5aaSvikram 
773425e8c5aaSvikram 	constraint = 0;
773525e8c5aaSvikram 	if (cons_array) {
773625e8c5aaSvikram 		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
773725e8c5aaSvikram 		(void) ddi_pathname(dip, path);
773825e8c5aaSvikram 		for (i = 0; cons_array[i] != NULL; i++) {
773925e8c5aaSvikram 			if (strcmp(path, cons_array[i]) == 0) {
774025e8c5aaSvikram 				constraint = 1;
774125e8c5aaSvikram 				break;
774225e8c5aaSvikram 			}
774325e8c5aaSvikram 		}
774425e8c5aaSvikram 		kmem_free(path, MAXPATHLEN);
774525e8c5aaSvikram 	}
774625e8c5aaSvikram 
774725e8c5aaSvikram 	mutex_enter(&DEVI(dip)->devi_lock);
774825e8c5aaSvikram 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
774925e8c5aaSvikram 	DEVI(dip)->devi_flags |= DEVI_RETIRING;
775025e8c5aaSvikram 	if (constraint)
775125e8c5aaSvikram 		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
775225e8c5aaSvikram 	mutex_exit(&DEVI(dip)->devi_lock);
775325e8c5aaSvikram 
775425e8c5aaSvikram 	RIO_VERBOSE((CE_NOTE, "marked dip as undergoing retire process dip=%p",
775525e8c5aaSvikram 	    (void *)dip));
775625e8c5aaSvikram 
775725e8c5aaSvikram 	if (constraint)
775825e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "marked dip as constrained, dip=%p",
775925e8c5aaSvikram 		    (void *)dip));
776025e8c5aaSvikram 
776125e8c5aaSvikram 	if (MDI_PHCI(dip))
776225e8c5aaSvikram 		mdi_phci_mark_retiring(dip, cons_array);
776325e8c5aaSvikram 
776425e8c5aaSvikram 	return (DDI_WALK_CONTINUE);
776525e8c5aaSvikram }
776625e8c5aaSvikram 
776725e8c5aaSvikram static void
776825e8c5aaSvikram free_array(char **cons_array)
776925e8c5aaSvikram {
777025e8c5aaSvikram 	int	i;
777125e8c5aaSvikram 
777225e8c5aaSvikram 	if (cons_array == NULL)
777325e8c5aaSvikram 		return;
777425e8c5aaSvikram 
777525e8c5aaSvikram 	for (i = 0; cons_array[i] != NULL; i++) {
777625e8c5aaSvikram 		kmem_free(cons_array[i], strlen(cons_array[i]) + 1);
777725e8c5aaSvikram 	}
777825e8c5aaSvikram 	kmem_free(cons_array, (i+1) * sizeof (char *));
777925e8c5aaSvikram }
778025e8c5aaSvikram 
778125e8c5aaSvikram /*
778225e8c5aaSvikram  * Walk *every* node in subtree and check if it blocks, allows or has no
778325e8c5aaSvikram  * comment on a proposed retire.
778425e8c5aaSvikram  */
778525e8c5aaSvikram int
778625e8c5aaSvikram e_ddi_retire_notify(dev_info_t *dip, void *arg)
778725e8c5aaSvikram {
778825e8c5aaSvikram 	int	*constraint = (int *)arg;
778925e8c5aaSvikram 
779025e8c5aaSvikram 	RIO_DEBUG((CE_NOTE, "retire notify: dip = %p", (void *)dip));
779125e8c5aaSvikram 
779225e8c5aaSvikram 	(void) e_ddi_offline_notify(dip);
779325e8c5aaSvikram 
779425e8c5aaSvikram 	mutex_enter(&(DEVI(dip)->devi_lock));
779525e8c5aaSvikram 	if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) {
779625e8c5aaSvikram 		RIO_DEBUG((CE_WARN, "retire notify: dip in retire "
779725e8c5aaSvikram 		    "subtree is not marked: dip = %p", (void *)dip));
779825e8c5aaSvikram 		*constraint = 0;
779925e8c5aaSvikram 	} else if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) {
780025e8c5aaSvikram 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
780125e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "retire notify: BLOCKED: dip = %p",
780225e8c5aaSvikram 		    (void *)dip));
780325e8c5aaSvikram 		*constraint = 0;
780425e8c5aaSvikram 	} else if (!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT)) {
780525e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "retire notify: NO CONSTRAINT: "
780625e8c5aaSvikram 		    "dip = %p", (void *)dip));
780725e8c5aaSvikram 		*constraint = 0;
780825e8c5aaSvikram 	} else {
780925e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "retire notify: CONSTRAINT set: "
781025e8c5aaSvikram 		    "dip = %p", (void *)dip));
781125e8c5aaSvikram 	}
781225e8c5aaSvikram 	mutex_exit(&DEVI(dip)->devi_lock);
781325e8c5aaSvikram 
781425e8c5aaSvikram 	if (MDI_PHCI(dip))
781525e8c5aaSvikram 		mdi_phci_retire_notify(dip, constraint);
781625e8c5aaSvikram 
781725e8c5aaSvikram 	return (DDI_WALK_CONTINUE);
781825e8c5aaSvikram }
781925e8c5aaSvikram 
782025e8c5aaSvikram int
782125e8c5aaSvikram e_ddi_retire_finalize(dev_info_t *dip, void *arg)
782225e8c5aaSvikram {
782325e8c5aaSvikram 	int constraint = *(int *)arg;
782425e8c5aaSvikram 	int finalize;
782525e8c5aaSvikram 	int phci_only;
782625e8c5aaSvikram 
782725e8c5aaSvikram 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
782825e8c5aaSvikram 
782925e8c5aaSvikram 	mutex_enter(&DEVI(dip)->devi_lock);
783025e8c5aaSvikram 	if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) {
783125e8c5aaSvikram 		RIO_DEBUG((CE_WARN,
783225e8c5aaSvikram 		    "retire: unmarked dip(%p) in retire subtree",
783325e8c5aaSvikram 		    (void *)dip));
783425e8c5aaSvikram 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRED));
783525e8c5aaSvikram 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
783625e8c5aaSvikram 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
783725e8c5aaSvikram 		mutex_exit(&DEVI(dip)->devi_lock);
783825e8c5aaSvikram 		return (DDI_WALK_CONTINUE);
783925e8c5aaSvikram 	}
784025e8c5aaSvikram 
784125e8c5aaSvikram 	/*
784225e8c5aaSvikram 	 * retire the device if constraints have been applied
784325e8c5aaSvikram 	 * or if the device is not in use
784425e8c5aaSvikram 	 */
784525e8c5aaSvikram 	finalize = 0;
784625e8c5aaSvikram 	if (constraint) {
784725e8c5aaSvikram 		ASSERT(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT);
784825e8c5aaSvikram 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
784925e8c5aaSvikram 		DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
785025e8c5aaSvikram 		DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
785125e8c5aaSvikram 		DEVI(dip)->devi_flags |= DEVI_RETIRED;
785225e8c5aaSvikram 		mutex_exit(&DEVI(dip)->devi_lock);
785325e8c5aaSvikram 		(void) spec_fence_snode(dip, NULL);
785425e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "Fenced off: dip = %p", (void *)dip));
785525e8c5aaSvikram 		e_ddi_offline_finalize(dip, DDI_SUCCESS);
785625e8c5aaSvikram 	} else {
785725e8c5aaSvikram 		if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) {
785825e8c5aaSvikram 			ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
785925e8c5aaSvikram 			DEVI(dip)->devi_flags &= ~DEVI_R_BLOCKED;
786025e8c5aaSvikram 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
786125e8c5aaSvikram 			/* we have already finalized during notify */
786225e8c5aaSvikram 		} else if (DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT) {
786325e8c5aaSvikram 			DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
786425e8c5aaSvikram 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
786525e8c5aaSvikram 			finalize = 1;
786625e8c5aaSvikram 		} else {
786725e8c5aaSvikram 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
786825e8c5aaSvikram 			/*
786925e8c5aaSvikram 			 * even if no contracts, need to call finalize
787025e8c5aaSvikram 			 * to clear the contract barrier on the dip
787125e8c5aaSvikram 			 */
787225e8c5aaSvikram 			finalize = 1;
787325e8c5aaSvikram 		}
787425e8c5aaSvikram 		mutex_exit(&DEVI(dip)->devi_lock);
787525e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "finalize: NOT retired: dip = %p",
787625e8c5aaSvikram 		    (void *)dip));
787725e8c5aaSvikram 		if (finalize)
787825e8c5aaSvikram 			e_ddi_offline_finalize(dip, DDI_FAILURE);
787925e8c5aaSvikram 	}
788025e8c5aaSvikram 
788125e8c5aaSvikram 	/*
788225e8c5aaSvikram 	 * phci_only variable indicates no client checking, just
788325e8c5aaSvikram 	 * offline the PHCI. We set that to 0 to enable client
788425e8c5aaSvikram 	 * checking
788525e8c5aaSvikram 	 */
788625e8c5aaSvikram 	phci_only = 0;
788725e8c5aaSvikram 	if (MDI_PHCI(dip))
788825e8c5aaSvikram 		mdi_phci_retire_finalize(dip, phci_only);
788925e8c5aaSvikram 
789025e8c5aaSvikram 	return (DDI_WALK_CONTINUE);
789125e8c5aaSvikram }
789225e8c5aaSvikram 
789325e8c5aaSvikram /*
789425e8c5aaSvikram  * Returns
789525e8c5aaSvikram  * 	DDI_SUCCESS if constraints allow retire
789625e8c5aaSvikram  *	DDI_FAILURE if constraints don't allow retire.
789725e8c5aaSvikram  * cons_array is a NULL terminated array of node paths for
789825e8c5aaSvikram  * which constraints have already been applied.
789925e8c5aaSvikram  */
790025e8c5aaSvikram int
790125e8c5aaSvikram e_ddi_retire_device(char *path, char **cons_array)
790225e8c5aaSvikram {
790325e8c5aaSvikram 	dev_info_t	*dip;
790425e8c5aaSvikram 	dev_info_t	*pdip;
790525e8c5aaSvikram 	int		circ;
790625e8c5aaSvikram 	int		circ2;
790725e8c5aaSvikram 	int		constraint;
790825e8c5aaSvikram 	char		*devnm;
790925e8c5aaSvikram 
791025e8c5aaSvikram 	/*
791125e8c5aaSvikram 	 * First, lookup the device
791225e8c5aaSvikram 	 */
791325e8c5aaSvikram 	dip = e_ddi_hold_devi_by_path(path, 0);
791425e8c5aaSvikram 	if (dip == NULL) {
791525e8c5aaSvikram 		/*
791625e8c5aaSvikram 		 * device does not exist. This device cannot be
791725e8c5aaSvikram 		 * a critical device since it is not in use. Thus
791825e8c5aaSvikram 		 * this device is always retireable. Return DDI_SUCCESS
791925e8c5aaSvikram 		 * to indicate this. If this device is ever
792025e8c5aaSvikram 		 * instantiated, I/O framework will consult the
792125e8c5aaSvikram 		 * the persistent retire store, mark it as
792225e8c5aaSvikram 		 * retired and fence it off.
792325e8c5aaSvikram 		 */
792425e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "Retire device: device doesn't exist."
792525e8c5aaSvikram 		    " NOP. Just returning SUCCESS. path=%s", path));
792625e8c5aaSvikram 		free_array(cons_array);
792725e8c5aaSvikram 		return (DDI_SUCCESS);
792825e8c5aaSvikram 	}
792925e8c5aaSvikram 
793025e8c5aaSvikram 	RIO_DEBUG((CE_NOTE, "Retire device: found dip = %p.", (void *)dip));
793125e8c5aaSvikram 
793225e8c5aaSvikram 	pdip = ddi_get_parent(dip);
793325e8c5aaSvikram 	ndi_hold_devi(pdip);
793425e8c5aaSvikram 
793525e8c5aaSvikram 	/*
793625e8c5aaSvikram 	 * Run devfs_clean() in case dip has no constraints and is
793725e8c5aaSvikram 	 * not in use, so is retireable but there are dv_nodes holding
793825e8c5aaSvikram 	 * ref-count on the dip. Note that devfs_clean() always returns
793925e8c5aaSvikram 	 * success.
794025e8c5aaSvikram 	 */
794125e8c5aaSvikram 	devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
794225e8c5aaSvikram 	(void) ddi_deviname(dip, devnm);
794325e8c5aaSvikram 	(void) devfs_clean(pdip, devnm + 1, DV_CLEAN_FORCE);
794425e8c5aaSvikram 	kmem_free(devnm, MAXNAMELEN + 1);
794525e8c5aaSvikram 
794625e8c5aaSvikram 	ndi_devi_enter(pdip, &circ);
794725e8c5aaSvikram 
794825e8c5aaSvikram 	/* release hold from e_ddi_hold_devi_by_path */
794925e8c5aaSvikram 	ndi_rele_devi(dip);
795025e8c5aaSvikram 
795125e8c5aaSvikram 	/*
795225e8c5aaSvikram 	 * If it cannot make a determination, is_leaf_node() assumes
795325e8c5aaSvikram 	 * dip is a nexus.
795425e8c5aaSvikram 	 */
795525e8c5aaSvikram 	(void) e_ddi_mark_retiring(dip, cons_array);
795625e8c5aaSvikram 	if (!is_leaf_node(dip)) {
795725e8c5aaSvikram 		ndi_devi_enter(dip, &circ2);
795825e8c5aaSvikram 		ddi_walk_devs(ddi_get_child(dip), e_ddi_mark_retiring,
795925e8c5aaSvikram 		    cons_array);
796025e8c5aaSvikram 		ndi_devi_exit(dip, circ2);
796125e8c5aaSvikram 	}
796225e8c5aaSvikram 	free_array(cons_array);
796325e8c5aaSvikram 
796425e8c5aaSvikram 	/*
796525e8c5aaSvikram 	 * apply constraints
796625e8c5aaSvikram 	 */
796725e8c5aaSvikram 	RIO_DEBUG((CE_NOTE, "retire: subtree retire notify: path = %s", path));
796825e8c5aaSvikram 
796925e8c5aaSvikram 	constraint = 1;	/* assume constraints allow retire */
797025e8c5aaSvikram 	(void) e_ddi_retire_notify(dip, &constraint);
797125e8c5aaSvikram 	if (!is_leaf_node(dip)) {
797225e8c5aaSvikram 		ndi_devi_enter(dip, &circ2);
797325e8c5aaSvikram 		ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_notify,
797425e8c5aaSvikram 		    &constraint);
797525e8c5aaSvikram 		ndi_devi_exit(dip, circ2);
797625e8c5aaSvikram 	}
797725e8c5aaSvikram 
797825e8c5aaSvikram 	/*
797925e8c5aaSvikram 	 * Now finalize the retire
798025e8c5aaSvikram 	 */
798125e8c5aaSvikram 	(void) e_ddi_retire_finalize(dip, &constraint);
798225e8c5aaSvikram 	if (!is_leaf_node(dip)) {
798325e8c5aaSvikram 		ndi_devi_enter(dip, &circ2);
798425e8c5aaSvikram 		ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_finalize,
798525e8c5aaSvikram 		    &constraint);
798625e8c5aaSvikram 		ndi_devi_exit(dip, circ2);
798725e8c5aaSvikram 	}
798825e8c5aaSvikram 
798925e8c5aaSvikram 	if (!constraint) {
799025e8c5aaSvikram 		RIO_DEBUG((CE_WARN, "retire failed: path = %s", path));
799125e8c5aaSvikram 	} else {
799225e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "retire succeeded: path = %s", path));
799325e8c5aaSvikram 	}
799425e8c5aaSvikram 
799525e8c5aaSvikram 	ndi_devi_exit(pdip, circ);
799625e8c5aaSvikram 	ndi_rele_devi(pdip);
799725e8c5aaSvikram 	return (constraint ? DDI_SUCCESS : DDI_FAILURE);
799825e8c5aaSvikram }
799925e8c5aaSvikram 
800025e8c5aaSvikram static int
800125e8c5aaSvikram unmark_and_unfence(dev_info_t *dip, void *arg)
800225e8c5aaSvikram {
800325e8c5aaSvikram 	char	*path = (char *)arg;
800425e8c5aaSvikram 
800525e8c5aaSvikram 	ASSERT(path);
800625e8c5aaSvikram 
800725e8c5aaSvikram 	(void) ddi_pathname(dip, path);
800825e8c5aaSvikram 
800925e8c5aaSvikram 	mutex_enter(&DEVI(dip)->devi_lock);
801025e8c5aaSvikram 	DEVI(dip)->devi_flags &= ~DEVI_RETIRED;
801125e8c5aaSvikram 	DEVI_SET_DEVICE_ONLINE(dip);
801225e8c5aaSvikram 	mutex_exit(&DEVI(dip)->devi_lock);
801325e8c5aaSvikram 
801425e8c5aaSvikram 	RIO_VERBOSE((CE_NOTE, "Cleared RETIRED flag: dip=%p, path=%s",
801525e8c5aaSvikram 	    (void *)dip, path));
801625e8c5aaSvikram 
801725e8c5aaSvikram 	(void) spec_unfence_snode(dip);
801825e8c5aaSvikram 	RIO_DEBUG((CE_NOTE, "Unfenced device: %s", path));
801925e8c5aaSvikram 
802025e8c5aaSvikram 	if (MDI_PHCI(dip))
802125e8c5aaSvikram 		mdi_phci_unretire(dip);
802225e8c5aaSvikram 
802325e8c5aaSvikram 	return (DDI_WALK_CONTINUE);
802425e8c5aaSvikram }
802525e8c5aaSvikram 
802625e8c5aaSvikram struct find_dip {
802725e8c5aaSvikram 	char	*fd_buf;
802825e8c5aaSvikram 	char	*fd_path;
802925e8c5aaSvikram 	dev_info_t *fd_dip;
803025e8c5aaSvikram };
803125e8c5aaSvikram 
803225e8c5aaSvikram static int
803325e8c5aaSvikram find_dip_fcn(dev_info_t *dip, void *arg)
803425e8c5aaSvikram {
803525e8c5aaSvikram 	struct find_dip *findp = (struct find_dip *)arg;
803625e8c5aaSvikram 
803725e8c5aaSvikram 	(void) ddi_pathname(dip, findp->fd_buf);
803825e8c5aaSvikram 
803925e8c5aaSvikram 	if (strcmp(findp->fd_path, findp->fd_buf) != 0)
804025e8c5aaSvikram 		return (DDI_WALK_CONTINUE);
804125e8c5aaSvikram 
804225e8c5aaSvikram 	ndi_hold_devi(dip);
804325e8c5aaSvikram 	findp->fd_dip = dip;
804425e8c5aaSvikram 
804525e8c5aaSvikram 	return (DDI_WALK_TERMINATE);
804625e8c5aaSvikram }
804725e8c5aaSvikram 
804825e8c5aaSvikram int
804925e8c5aaSvikram e_ddi_unretire_device(char *path)
805025e8c5aaSvikram {
805125e8c5aaSvikram 	int		circ;
8052ffc89d77Svikram 	int		circ2;
805325e8c5aaSvikram 	char		*path2;
805425e8c5aaSvikram 	dev_info_t	*pdip;
805525e8c5aaSvikram 	dev_info_t	*dip;
805625e8c5aaSvikram 	struct find_dip	 find_dip;
805725e8c5aaSvikram 
805825e8c5aaSvikram 	ASSERT(path);
805925e8c5aaSvikram 	ASSERT(*path == '/');
806025e8c5aaSvikram 
806125e8c5aaSvikram 	if (strcmp(path, "/") == 0) {
806225e8c5aaSvikram 		cmn_err(CE_WARN, "Root node cannot be retired. Skipping "
806325e8c5aaSvikram 		    "device unretire: %s", path);
806425e8c5aaSvikram 		return (0);
806525e8c5aaSvikram 	}
806625e8c5aaSvikram 
806725e8c5aaSvikram 	/*
806825e8c5aaSvikram 	 * We can't lookup the dip (corresponding to path) via
806925e8c5aaSvikram 	 * e_ddi_hold_devi_by_path() because the dip may be offline
807025e8c5aaSvikram 	 * and may not attach. Use ddi_walk_devs() instead;
807125e8c5aaSvikram 	 */
807225e8c5aaSvikram 	find_dip.fd_buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
807325e8c5aaSvikram 	find_dip.fd_path = path;
807425e8c5aaSvikram 	find_dip.fd_dip = NULL;
807525e8c5aaSvikram 
807625e8c5aaSvikram 	pdip = ddi_root_node();
807725e8c5aaSvikram 
807825e8c5aaSvikram 	ndi_devi_enter(pdip, &circ);
807925e8c5aaSvikram 	ddi_walk_devs(ddi_get_child(pdip), find_dip_fcn, &find_dip);
808025e8c5aaSvikram 	ndi_devi_exit(pdip, circ);
808125e8c5aaSvikram 
808225e8c5aaSvikram 	kmem_free(find_dip.fd_buf, MAXPATHLEN);
808325e8c5aaSvikram 
808425e8c5aaSvikram 	if (find_dip.fd_dip == NULL) {
808525e8c5aaSvikram 		cmn_err(CE_WARN, "Device not found in device tree. Skipping "
808625e8c5aaSvikram 		    "device unretire: %s", path);
808725e8c5aaSvikram 		return (0);
808825e8c5aaSvikram 	}
808925e8c5aaSvikram 
809025e8c5aaSvikram 	dip = find_dip.fd_dip;
809125e8c5aaSvikram 
809225e8c5aaSvikram 	pdip = ddi_get_parent(dip);
809325e8c5aaSvikram 
809425e8c5aaSvikram 	ndi_hold_devi(pdip);
809525e8c5aaSvikram 
809625e8c5aaSvikram 	ndi_devi_enter(pdip, &circ);
809725e8c5aaSvikram 
809825e8c5aaSvikram 	path2 = kmem_alloc(MAXPATHLEN, KM_SLEEP);
809925e8c5aaSvikram 
810025e8c5aaSvikram 	(void) unmark_and_unfence(dip, path2);
810125e8c5aaSvikram 	if (!is_leaf_node(dip)) {
8102ffc89d77Svikram 		ndi_devi_enter(dip, &circ2);
810325e8c5aaSvikram 		ddi_walk_devs(ddi_get_child(dip), unmark_and_unfence, path2);
8104ffc89d77Svikram 		ndi_devi_exit(dip, circ2);
810525e8c5aaSvikram 	}
810625e8c5aaSvikram 
810725e8c5aaSvikram 	kmem_free(path2, MAXPATHLEN);
810825e8c5aaSvikram 
810925e8c5aaSvikram 	/* release hold from find_dip_fcn() */
811025e8c5aaSvikram 	ndi_rele_devi(dip);
811125e8c5aaSvikram 
811225e8c5aaSvikram 	ndi_devi_exit(pdip, circ);
811325e8c5aaSvikram 
811425e8c5aaSvikram 	ndi_rele_devi(pdip);
811525e8c5aaSvikram 
811625e8c5aaSvikram 	return (0);
811725e8c5aaSvikram }
811825e8c5aaSvikram 
811925e8c5aaSvikram /*
812025e8c5aaSvikram  * Called before attach on a dip that has been retired.
812125e8c5aaSvikram  */
812225e8c5aaSvikram static int
812325e8c5aaSvikram mark_and_fence(dev_info_t *dip, void *arg)
812425e8c5aaSvikram {
812525e8c5aaSvikram 	char    *fencepath = (char *)arg;
812625e8c5aaSvikram 
812725e8c5aaSvikram 	/*
812825e8c5aaSvikram 	 * We have already decided to retire this device. The various
812925e8c5aaSvikram 	 * constraint checking should not be set.
813025e8c5aaSvikram 	 * NOTE that the retire flag may already be set due to
813125e8c5aaSvikram 	 * fenced -> detach -> fenced transitions.
813225e8c5aaSvikram 	 */
813325e8c5aaSvikram 	mutex_enter(&DEVI(dip)->devi_lock);
813425e8c5aaSvikram 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
813525e8c5aaSvikram 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
813625e8c5aaSvikram 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRING));
813725e8c5aaSvikram 	DEVI(dip)->devi_flags |= DEVI_RETIRED;
813825e8c5aaSvikram 	mutex_exit(&DEVI(dip)->devi_lock);
813925e8c5aaSvikram 	RIO_VERBOSE((CE_NOTE, "marked as RETIRED dip=%p", (void *)dip));
814025e8c5aaSvikram 
814125e8c5aaSvikram 	if (fencepath) {
814225e8c5aaSvikram 		(void) spec_fence_snode(dip, NULL);
814325e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "Fenced: %s",
814425e8c5aaSvikram 		    ddi_pathname(dip, fencepath)));
814525e8c5aaSvikram 	}
814625e8c5aaSvikram 
814725e8c5aaSvikram 	return (DDI_WALK_CONTINUE);
814825e8c5aaSvikram }
814925e8c5aaSvikram 
815025e8c5aaSvikram /*
815125e8c5aaSvikram  * Checks the retire database and:
815225e8c5aaSvikram  *
815325e8c5aaSvikram  * - if device is present in the retire database, marks the device retired
815425e8c5aaSvikram  *   and fences it off.
815525e8c5aaSvikram  * - if device is not in retire database, allows the device to attach normally
815625e8c5aaSvikram  *
815725e8c5aaSvikram  * To be called only by framework attach code on first attach attempt.
815825e8c5aaSvikram  *
815925e8c5aaSvikram  */
816025e8c5aaSvikram static void
816125e8c5aaSvikram i_ddi_check_retire(dev_info_t *dip)
816225e8c5aaSvikram {
816325e8c5aaSvikram 	char		*path;
816425e8c5aaSvikram 	dev_info_t	*pdip;
816525e8c5aaSvikram 	int		circ;
816625e8c5aaSvikram 	int		phci_only;
816725e8c5aaSvikram 
816825e8c5aaSvikram 	pdip = ddi_get_parent(dip);
816925e8c5aaSvikram 
817025e8c5aaSvikram 	/*
817125e8c5aaSvikram 	 * Root dip is treated special and doesn't take this code path.
817225e8c5aaSvikram 	 * Also root can never be retired.
817325e8c5aaSvikram 	 */
817425e8c5aaSvikram 	ASSERT(pdip);
817525e8c5aaSvikram 	ASSERT(DEVI_BUSY_OWNED(pdip));
817625e8c5aaSvikram 	ASSERT(i_ddi_node_state(dip) < DS_ATTACHED);
817725e8c5aaSvikram 
817825e8c5aaSvikram 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
817925e8c5aaSvikram 
818025e8c5aaSvikram 	(void) ddi_pathname(dip, path);
818125e8c5aaSvikram 
818225e8c5aaSvikram 	RIO_VERBOSE((CE_NOTE, "Checking if dip should attach: dip=%p, path=%s",
818325e8c5aaSvikram 	    (void *)dip, path));
818425e8c5aaSvikram 
818525e8c5aaSvikram 	/*
818625e8c5aaSvikram 	 * Check if this device is in the "retired" store i.e.  should
818725e8c5aaSvikram 	 * be retired. If not, we have nothing to do.
818825e8c5aaSvikram 	 */
818925e8c5aaSvikram 	if (e_ddi_device_retired(path) == 0) {
819025e8c5aaSvikram 		RIO_VERBOSE((CE_NOTE, "device is NOT retired: path=%s", path));
819125e8c5aaSvikram 		kmem_free(path, MAXPATHLEN);
819225e8c5aaSvikram 		return;
819325e8c5aaSvikram 	}
819425e8c5aaSvikram 
819525e8c5aaSvikram 	RIO_DEBUG((CE_NOTE, "attach: device is retired: path=%s", path));
819625e8c5aaSvikram 
819725e8c5aaSvikram 	/*
819825e8c5aaSvikram 	 * Mark dips and fence off snodes (if any)
819925e8c5aaSvikram 	 */
820025e8c5aaSvikram 	RIO_DEBUG((CE_NOTE, "attach: Mark and fence subtree: path=%s", path));
820125e8c5aaSvikram 	(void) mark_and_fence(dip, path);
820225e8c5aaSvikram 	if (!is_leaf_node(dip)) {
820325e8c5aaSvikram 		ndi_devi_enter(dip, &circ);
820425e8c5aaSvikram 		ddi_walk_devs(ddi_get_child(dip), mark_and_fence, path);
820525e8c5aaSvikram 		ndi_devi_exit(dip, circ);
820625e8c5aaSvikram 	}
820725e8c5aaSvikram 
820825e8c5aaSvikram 	kmem_free(path, MAXPATHLEN);
820925e8c5aaSvikram 
821025e8c5aaSvikram 	/*
821125e8c5aaSvikram 	 * We don't want to check the client. We just want to
821225e8c5aaSvikram 	 * offline the PHCI
821325e8c5aaSvikram 	 */
821425e8c5aaSvikram 	phci_only = 1;
821525e8c5aaSvikram 	if (MDI_PHCI(dip))
821625e8c5aaSvikram 		mdi_phci_retire_finalize(dip, phci_only);
821725e8c5aaSvikram }
8218