xref: /titanic_52/usr/src/uts/common/os/devcfg.c (revision 245c82d97d674feb265d2afe1b4db18c9849f5b7)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*245c82d9Scth  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/note.h>
307c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
317c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
327c478bd9Sstevel@tonic-gate #include <sys/instance.h>
337c478bd9Sstevel@tonic-gate #include <sys/conf.h>
347c478bd9Sstevel@tonic-gate #include <sys/stat.h>
357c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
367c478bd9Sstevel@tonic-gate #include <sys/hwconf.h>
377c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
387c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
397c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
407c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
417c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
427c478bd9Sstevel@tonic-gate #include <sys/dacf.h>
437c478bd9Sstevel@tonic-gate #include <sys/promif.h>
447c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
457c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
467c478bd9Sstevel@tonic-gate #include <sys/taskq.h>
477c478bd9Sstevel@tonic-gate #include <sys/sysevent.h>
487c478bd9Sstevel@tonic-gate #include <sys/sunmdi.h>
497c478bd9Sstevel@tonic-gate #include <sys/stream.h>
507c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
517c478bd9Sstevel@tonic-gate #include <sys/fs/snode.h>
527c478bd9Sstevel@tonic-gate #include <sys/fs/dv_node.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #ifdef DEBUG
557c478bd9Sstevel@tonic-gate int ddidebug = DDI_AUDIT;
567c478bd9Sstevel@tonic-gate #else
577c478bd9Sstevel@tonic-gate int ddidebug = 0;
587c478bd9Sstevel@tonic-gate #endif
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #define	MT_CONFIG_OP	0
617c478bd9Sstevel@tonic-gate #define	MT_UNCONFIG_OP	1
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /* Multi-threaded configuration */
647c478bd9Sstevel@tonic-gate struct mt_config_handle {
657c478bd9Sstevel@tonic-gate 	kmutex_t mtc_lock;
667c478bd9Sstevel@tonic-gate 	kcondvar_t mtc_cv;
677c478bd9Sstevel@tonic-gate 	int mtc_thr_count;
687c478bd9Sstevel@tonic-gate 	dev_info_t *mtc_pdip;	/* parent dip for mt_config_children */
697c478bd9Sstevel@tonic-gate 	dev_info_t **mtc_fdip;	/* "a" dip where unconfigure failed */
707c478bd9Sstevel@tonic-gate 	major_t mtc_parmajor;	/* parent major for mt_config_driver */
717c478bd9Sstevel@tonic-gate 	major_t mtc_major;
727c478bd9Sstevel@tonic-gate 	int mtc_flags;
737c478bd9Sstevel@tonic-gate 	int mtc_op;		/* config or unconfig */
747c478bd9Sstevel@tonic-gate 	int mtc_error;		/* operation error */
757c478bd9Sstevel@tonic-gate 	struct brevq_node **mtc_brevqp;	/* outstanding branch events queue */
767c478bd9Sstevel@tonic-gate #ifdef DEBUG
777c478bd9Sstevel@tonic-gate 	int total_time;
787c478bd9Sstevel@tonic-gate 	timestruc_t start_time;
797c478bd9Sstevel@tonic-gate #endif /* DEBUG */
807c478bd9Sstevel@tonic-gate };
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate struct devi_nodeid {
83fa9e4066Sahrens 	pnode_t nodeid;
847c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
857c478bd9Sstevel@tonic-gate 	struct devi_nodeid *next;
867c478bd9Sstevel@tonic-gate };
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate struct devi_nodeid_list {
897c478bd9Sstevel@tonic-gate 	kmutex_t dno_lock;		/* Protects other fields */
907c478bd9Sstevel@tonic-gate 	struct devi_nodeid *dno_head;	/* list of devi nodeid elements */
917c478bd9Sstevel@tonic-gate 	struct devi_nodeid *dno_free;	/* Free list */
927c478bd9Sstevel@tonic-gate 	uint_t dno_list_length;		/* number of dips in list */
937c478bd9Sstevel@tonic-gate };
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /* used to keep track of branch remove events to be generated */
967c478bd9Sstevel@tonic-gate struct brevq_node {
97*245c82d9Scth 	char *brn_deviname;
98*245c82d9Scth 	struct brevq_node *brn_sibling;
99*245c82d9Scth 	struct brevq_node *brn_child;
1007c478bd9Sstevel@tonic-gate };
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate static struct devi_nodeid_list devi_nodeid_list;
1037c478bd9Sstevel@tonic-gate static struct devi_nodeid_list *devimap = &devi_nodeid_list;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * Well known nodes which are attached first at boot time.
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate dev_info_t *top_devinfo;		/* root of device tree */
1097c478bd9Sstevel@tonic-gate dev_info_t *options_dip;
1107c478bd9Sstevel@tonic-gate dev_info_t *pseudo_dip;
1117c478bd9Sstevel@tonic-gate dev_info_t *clone_dip;
1127c478bd9Sstevel@tonic-gate dev_info_t *scsi_vhci_dip;		/* MPXIO dip */
1137c478bd9Sstevel@tonic-gate major_t clone_major;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /* block all future dev_info state changes */
1167c478bd9Sstevel@tonic-gate static hrtime_t volatile devinfo_freeze = 0;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate /* number of dev_info attaches/detaches currently in progress */
1197c478bd9Sstevel@tonic-gate static ulong_t devinfo_attach_detach = 0;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate extern kmutex_t global_vhci_lock;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate  * The devinfo snapshot cache and related variables.
1257c478bd9Sstevel@tonic-gate  * The only field in the di_cache structure that needs initialization
1267c478bd9Sstevel@tonic-gate  * is the mutex (cache_lock). However, since this is an adaptive mutex
1277c478bd9Sstevel@tonic-gate  * (MUTEX_DEFAULT) - it is automatically initialized by being allocated
1287c478bd9Sstevel@tonic-gate  * in zeroed memory (static storage class). Therefore no explicit
1297c478bd9Sstevel@tonic-gate  * initialization of the di_cache structure is needed.
1307c478bd9Sstevel@tonic-gate  */
1317c478bd9Sstevel@tonic-gate struct di_cache	di_cache = {1};
1327c478bd9Sstevel@tonic-gate int		di_cache_debug = 0;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /* For ddvis, which needs pseudo children under PCI */
1357c478bd9Sstevel@tonic-gate int pci_allow_pseudo_children = 0;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /*
1387c478bd9Sstevel@tonic-gate  * The following switch is for service people, in case a
1397c478bd9Sstevel@tonic-gate  * 3rd party driver depends on identify(9e) being called.
1407c478bd9Sstevel@tonic-gate  */
1417c478bd9Sstevel@tonic-gate int identify_9e = 0;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate int mtc_off;					/* turn off mt config */
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate static kmem_cache_t *ddi_node_cache;		/* devinfo node cache */
1467c478bd9Sstevel@tonic-gate static devinfo_log_header_t *devinfo_audit_log;	/* devinfo log */
1477c478bd9Sstevel@tonic-gate static int devinfo_log_size;			/* size in pages */
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate static int lookup_compatible(dev_info_t *, uint_t);
1507c478bd9Sstevel@tonic-gate static char *encode_composite_string(char **, uint_t, size_t *, uint_t);
1517c478bd9Sstevel@tonic-gate static void link_to_driver_list(dev_info_t *);
1527c478bd9Sstevel@tonic-gate static void unlink_from_driver_list(dev_info_t *);
1537c478bd9Sstevel@tonic-gate static void add_to_dn_list(struct devnames *, dev_info_t *);
1547c478bd9Sstevel@tonic-gate static void remove_from_dn_list(struct devnames *, dev_info_t *);
1557c478bd9Sstevel@tonic-gate static dev_info_t *find_child_by_callback(dev_info_t *, char *, char *,
1567c478bd9Sstevel@tonic-gate     int (*)(dev_info_t *, char *, int));
1577c478bd9Sstevel@tonic-gate static dev_info_t *find_duplicate_child();
1587c478bd9Sstevel@tonic-gate static void add_global_props(dev_info_t *);
1597c478bd9Sstevel@tonic-gate static void remove_global_props(dev_info_t *);
1607c478bd9Sstevel@tonic-gate static int uninit_node(dev_info_t *);
1617c478bd9Sstevel@tonic-gate static void da_log_init(void);
1627c478bd9Sstevel@tonic-gate static void da_log_enter(dev_info_t *);
1637c478bd9Sstevel@tonic-gate static int walk_devs(dev_info_t *, int (*f)(dev_info_t *, void *), void *, int);
1647c478bd9Sstevel@tonic-gate static int reset_nexus_flags(dev_info_t *, void *);
1657c478bd9Sstevel@tonic-gate static void ddi_optimize_dtree(dev_info_t *);
1667c478bd9Sstevel@tonic-gate static int is_leaf_node(dev_info_t *);
1677c478bd9Sstevel@tonic-gate static struct mt_config_handle *mt_config_init(dev_info_t *, dev_info_t **,
1687c478bd9Sstevel@tonic-gate     int, major_t, int, struct brevq_node **);
1697c478bd9Sstevel@tonic-gate static void mt_config_children(struct mt_config_handle *);
1707c478bd9Sstevel@tonic-gate static void mt_config_driver(struct mt_config_handle *);
1717c478bd9Sstevel@tonic-gate static int mt_config_fini(struct mt_config_handle *);
1727c478bd9Sstevel@tonic-gate static int devi_unconfig_common(dev_info_t *, dev_info_t **, int, major_t,
1737c478bd9Sstevel@tonic-gate     struct brevq_node **);
1747c478bd9Sstevel@tonic-gate static int
1757c478bd9Sstevel@tonic-gate ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
1767c478bd9Sstevel@tonic-gate     dev_info_t **childp, int flags);
1779d3d2ed0Shiremath static void i_link_vhci_node(dev_info_t *);
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate /*
1807c478bd9Sstevel@tonic-gate  * dev_info cache and node management
1817c478bd9Sstevel@tonic-gate  */
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate /* initialize dev_info node cache */
1847c478bd9Sstevel@tonic-gate void
1857c478bd9Sstevel@tonic-gate i_ddi_node_cache_init()
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate 	ASSERT(ddi_node_cache == NULL);
1887c478bd9Sstevel@tonic-gate 	ddi_node_cache = kmem_cache_create("dev_info_node_cache",
1897c478bd9Sstevel@tonic-gate 	    sizeof (struct dev_info), 0, NULL, NULL, NULL, NULL, NULL, 0);
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	if (ddidebug & DDI_AUDIT)
1927c478bd9Sstevel@tonic-gate 		da_log_init();
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate /*
1967c478bd9Sstevel@tonic-gate  * Allocating a dev_info node, callable from interrupt context with KM_NOSLEEP
1977c478bd9Sstevel@tonic-gate  * The allocated node has a reference count of 0.
1987c478bd9Sstevel@tonic-gate  */
1997c478bd9Sstevel@tonic-gate dev_info_t *
200fa9e4066Sahrens i_ddi_alloc_node(dev_info_t *pdip, char *node_name, pnode_t nodeid,
2017c478bd9Sstevel@tonic-gate     int instance, ddi_prop_t *sys_prop, int flag)
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate 	struct dev_info *devi;
2047c478bd9Sstevel@tonic-gate 	struct devi_nodeid *elem;
2057c478bd9Sstevel@tonic-gate 	static char failed[] = "i_ddi_alloc_node: out of memory";
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	ASSERT(node_name != NULL);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	if ((devi = kmem_cache_alloc(ddi_node_cache, flag)) == NULL) {
2107c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, failed);
2117c478bd9Sstevel@tonic-gate 		return (NULL);
2127c478bd9Sstevel@tonic-gate 	}
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	bzero(devi, sizeof (struct dev_info));
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	if (devinfo_audit_log) {
2177c478bd9Sstevel@tonic-gate 		devi->devi_audit = kmem_zalloc(sizeof (devinfo_audit_t), flag);
2187c478bd9Sstevel@tonic-gate 		if (devi->devi_audit == NULL)
2197c478bd9Sstevel@tonic-gate 			goto fail;
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	if ((devi->devi_node_name = i_ddi_strdup(node_name, flag)) == NULL)
2237c478bd9Sstevel@tonic-gate 		goto fail;
2247c478bd9Sstevel@tonic-gate 	/* default binding name is node name */
2257c478bd9Sstevel@tonic-gate 	devi->devi_binding_name = devi->devi_node_name;
2267c478bd9Sstevel@tonic-gate 	devi->devi_major = (major_t)-1;	/* unbound by default */
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	/*
2297c478bd9Sstevel@tonic-gate 	 * Make a copy of system property
2307c478bd9Sstevel@tonic-gate 	 */
2317c478bd9Sstevel@tonic-gate 	if (sys_prop &&
2327c478bd9Sstevel@tonic-gate 	    (devi->devi_sys_prop_ptr = i_ddi_prop_list_dup(sys_prop, flag))
2337c478bd9Sstevel@tonic-gate 	    == NULL)
2347c478bd9Sstevel@tonic-gate 		goto fail;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	/*
2377c478bd9Sstevel@tonic-gate 	 * Assign devi_nodeid, devi_node_class, devi_node_attributes
2387c478bd9Sstevel@tonic-gate 	 * according to the following algorithm:
2397c478bd9Sstevel@tonic-gate 	 *
2407c478bd9Sstevel@tonic-gate 	 * nodeid arg			node class		node attributes
2417c478bd9Sstevel@tonic-gate 	 *
2427c478bd9Sstevel@tonic-gate 	 * DEVI_PSEUDO_NODEID		DDI_NC_PSEUDO		A
2437c478bd9Sstevel@tonic-gate 	 * DEVI_SID_NODEID		DDI_NC_PSEUDO		A,P
2447c478bd9Sstevel@tonic-gate 	 * other			DDI_NC_PROM		P
2457c478bd9Sstevel@tonic-gate 	 *
2467c478bd9Sstevel@tonic-gate 	 * Where A = DDI_AUTO_ASSIGNED_NODEID (auto-assign a nodeid)
2477c478bd9Sstevel@tonic-gate 	 * and	 P = DDI_PERSISTENT
2487c478bd9Sstevel@tonic-gate 	 *
2497c478bd9Sstevel@tonic-gate 	 * auto-assigned nodeids are also auto-freed.
2507c478bd9Sstevel@tonic-gate 	 */
2517c478bd9Sstevel@tonic-gate 	switch (nodeid) {
2527c478bd9Sstevel@tonic-gate 	case DEVI_SID_NODEID:
2537c478bd9Sstevel@tonic-gate 		devi->devi_node_attributes = DDI_PERSISTENT;
2547c478bd9Sstevel@tonic-gate 		if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL)
2557c478bd9Sstevel@tonic-gate 			goto fail;
2567c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
2577c478bd9Sstevel@tonic-gate 	case DEVI_PSEUDO_NODEID:
2587c478bd9Sstevel@tonic-gate 		devi->devi_node_attributes |= DDI_AUTO_ASSIGNED_NODEID;
2597c478bd9Sstevel@tonic-gate 		devi->devi_node_class = DDI_NC_PSEUDO;
2607c478bd9Sstevel@tonic-gate 		if (impl_ddi_alloc_nodeid(&devi->devi_nodeid)) {
2617c478bd9Sstevel@tonic-gate 			panic("i_ddi_alloc_node: out of nodeids");
2627c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
2637c478bd9Sstevel@tonic-gate 		}
2647c478bd9Sstevel@tonic-gate 		break;
2657c478bd9Sstevel@tonic-gate 	default:
2667c478bd9Sstevel@tonic-gate 		if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL)
2677c478bd9Sstevel@tonic-gate 			goto fail;
2687c478bd9Sstevel@tonic-gate 		/*
2697c478bd9Sstevel@tonic-gate 		 * the nodetype is 'prom', try to 'take' the nodeid now.
2707c478bd9Sstevel@tonic-gate 		 * This requires memory allocation, so check for failure.
2717c478bd9Sstevel@tonic-gate 		 */
2727c478bd9Sstevel@tonic-gate 		if (impl_ddi_take_nodeid(nodeid, flag) != 0) {
2737c478bd9Sstevel@tonic-gate 			kmem_free(elem, sizeof (*elem));
2747c478bd9Sstevel@tonic-gate 			goto fail;
2757c478bd9Sstevel@tonic-gate 		}
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 		devi->devi_nodeid = nodeid;
2787c478bd9Sstevel@tonic-gate 		devi->devi_node_class = DDI_NC_PROM;
2797c478bd9Sstevel@tonic-gate 		devi->devi_node_attributes = DDI_PERSISTENT;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	}
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node((dev_info_t *)devi)) {
2847c478bd9Sstevel@tonic-gate 		mutex_enter(&devimap->dno_lock);
2857c478bd9Sstevel@tonic-gate 		elem->next = devimap->dno_free;
2867c478bd9Sstevel@tonic-gate 		devimap->dno_free = elem;
2877c478bd9Sstevel@tonic-gate 		mutex_exit(&devimap->dno_lock);
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	/*
2917c478bd9Sstevel@tonic-gate 	 * Instance is normally initialized to -1. In a few special
2927c478bd9Sstevel@tonic-gate 	 * cases, the caller may specify an instance (e.g. CPU nodes).
2937c478bd9Sstevel@tonic-gate 	 */
2947c478bd9Sstevel@tonic-gate 	devi->devi_instance = instance;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	/*
2977c478bd9Sstevel@tonic-gate 	 * set parent and bus_ctl parent
2987c478bd9Sstevel@tonic-gate 	 */
2997c478bd9Sstevel@tonic-gate 	devi->devi_parent = DEVI(pdip);
3007c478bd9Sstevel@tonic-gate 	devi->devi_bus_ctl = DEVI(pdip);
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
3037c478bd9Sstevel@tonic-gate 	    "i_ddi_alloc_node: name=%s id=%d\n", node_name, devi->devi_nodeid));
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	cv_init(&(devi->devi_cv), NULL, CV_DEFAULT, NULL);
3067c478bd9Sstevel@tonic-gate 	mutex_init(&(devi->devi_lock), NULL, MUTEX_DEFAULT, NULL);
3077c478bd9Sstevel@tonic-gate 	mutex_init(&(devi->devi_pm_lock), NULL, MUTEX_DEFAULT, NULL);
3087c478bd9Sstevel@tonic-gate 	mutex_init(&(devi->devi_pm_busy_lock), NULL, MUTEX_DEFAULT, NULL);
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	i_ddi_set_node_state((dev_info_t *)devi, DS_PROTO);
3117c478bd9Sstevel@tonic-gate 	da_log_enter((dev_info_t *)devi);
3127c478bd9Sstevel@tonic-gate 	return ((dev_info_t *)devi);
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate fail:
3157c478bd9Sstevel@tonic-gate 	if (devi->devi_sys_prop_ptr)
3167c478bd9Sstevel@tonic-gate 		i_ddi_prop_list_delete(devi->devi_sys_prop_ptr);
3177c478bd9Sstevel@tonic-gate 	if (devi->devi_node_name)
3187c478bd9Sstevel@tonic-gate 		kmem_free(devi->devi_node_name, strlen(node_name) + 1);
3197c478bd9Sstevel@tonic-gate 	if (devi->devi_audit)
3207c478bd9Sstevel@tonic-gate 		kmem_free(devi->devi_audit, sizeof (devinfo_audit_t));
3217c478bd9Sstevel@tonic-gate 	kmem_cache_free(ddi_node_cache, devi);
3227c478bd9Sstevel@tonic-gate 	cmn_err(CE_NOTE, failed);
3237c478bd9Sstevel@tonic-gate 	return (NULL);
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate /*
3277c478bd9Sstevel@tonic-gate  * free a dev_info structure.
3287c478bd9Sstevel@tonic-gate  * NB. Not callable from interrupt since impl_ddi_free_nodeid may block.
3297c478bd9Sstevel@tonic-gate  */
3307c478bd9Sstevel@tonic-gate void
3317c478bd9Sstevel@tonic-gate i_ddi_free_node(dev_info_t *dip)
3327c478bd9Sstevel@tonic-gate {
3337c478bd9Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
3347c478bd9Sstevel@tonic-gate 	struct devi_nodeid *elem;
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	ASSERT(devi->devi_ref == 0);
3377c478bd9Sstevel@tonic-gate 	ASSERT(devi->devi_addr == NULL);
3387c478bd9Sstevel@tonic-gate 	ASSERT(devi->devi_node_state == DS_PROTO);
3397c478bd9Sstevel@tonic-gate 	ASSERT(devi->devi_child == NULL);
3407c478bd9Sstevel@tonic-gate 
341fe9fe9fbScth 	/* free devi_addr_buf allocated by ddi_set_name_addr() */
342fe9fe9fbScth 	if (devi->devi_addr_buf)
343fe9fe9fbScth 		kmem_free(devi->devi_addr_buf, 2 * MAXNAMELEN);
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	if (i_ndi_dev_is_auto_assigned_node(dip))
3467c478bd9Sstevel@tonic-gate 		impl_ddi_free_nodeid(DEVI(dip)->devi_nodeid);
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
3497c478bd9Sstevel@tonic-gate 		mutex_enter(&devimap->dno_lock);
3507c478bd9Sstevel@tonic-gate 		ASSERT(devimap->dno_free);
3517c478bd9Sstevel@tonic-gate 		elem = devimap->dno_free;
3527c478bd9Sstevel@tonic-gate 		devimap->dno_free = elem->next;
3537c478bd9Sstevel@tonic-gate 		mutex_exit(&devimap->dno_lock);
3547c478bd9Sstevel@tonic-gate 		kmem_free(elem, sizeof (*elem));
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_compat_names)
3587c478bd9Sstevel@tonic-gate 		kmem_free(DEVI(dip)->devi_compat_names,
3597c478bd9Sstevel@tonic-gate 		    DEVI(dip)->devi_compat_length);
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	ddi_prop_remove_all(dip);	/* remove driver properties */
3627c478bd9Sstevel@tonic-gate 	if (devi->devi_sys_prop_ptr)
3637c478bd9Sstevel@tonic-gate 		i_ddi_prop_list_delete(devi->devi_sys_prop_ptr);
3647c478bd9Sstevel@tonic-gate 	if (devi->devi_hw_prop_ptr)
3657c478bd9Sstevel@tonic-gate 		i_ddi_prop_list_delete(devi->devi_hw_prop_ptr);
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_INVAL);
3687c478bd9Sstevel@tonic-gate 	da_log_enter(dip);
3697c478bd9Sstevel@tonic-gate 	if (devi->devi_audit) {
3707c478bd9Sstevel@tonic-gate 		kmem_free(devi->devi_audit, sizeof (devinfo_audit_t));
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 	kmem_free(devi->devi_node_name, strlen(devi->devi_node_name) + 1);
3737c478bd9Sstevel@tonic-gate 	if (devi->devi_device_class)
3747c478bd9Sstevel@tonic-gate 		kmem_free(devi->devi_device_class,
3757c478bd9Sstevel@tonic-gate 		    strlen(devi->devi_device_class) + 1);
3767c478bd9Sstevel@tonic-gate 	cv_destroy(&(devi->devi_cv));
3777c478bd9Sstevel@tonic-gate 	mutex_destroy(&(devi->devi_lock));
3787c478bd9Sstevel@tonic-gate 	mutex_destroy(&(devi->devi_pm_lock));
3797c478bd9Sstevel@tonic-gate 	mutex_destroy(&(devi->devi_pm_busy_lock));
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	kmem_cache_free(ddi_node_cache, devi);
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate /*
3867c478bd9Sstevel@tonic-gate  * Node state transitions
3877c478bd9Sstevel@tonic-gate  */
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate /*
3907c478bd9Sstevel@tonic-gate  * Change the node name
3917c478bd9Sstevel@tonic-gate  */
3927c478bd9Sstevel@tonic-gate int
3937c478bd9Sstevel@tonic-gate ndi_devi_set_nodename(dev_info_t *dip, char *name, int flags)
3947c478bd9Sstevel@tonic-gate {
3957c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags))
3967c478bd9Sstevel@tonic-gate 	char *nname, *oname;
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	ASSERT(dip && name);
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	oname = DEVI(dip)->devi_node_name;
4017c478bd9Sstevel@tonic-gate 	if (strcmp(oname, name) == 0)
4027c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	/*
4057c478bd9Sstevel@tonic-gate 	 * pcicfg_fix_ethernet requires a name change after node
4067c478bd9Sstevel@tonic-gate 	 * is linked into the tree. When pcicfg is fixed, we
4077c478bd9Sstevel@tonic-gate 	 * should only allow name change in DS_PROTO state.
4087c478bd9Sstevel@tonic-gate 	 */
4097c478bd9Sstevel@tonic-gate 	if (i_ddi_node_state(dip) >= DS_BOUND) {
4107c478bd9Sstevel@tonic-gate 		/*
4117c478bd9Sstevel@tonic-gate 		 * Don't allow name change once node is bound
4127c478bd9Sstevel@tonic-gate 		 */
4137c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE,
4147c478bd9Sstevel@tonic-gate 		    "ndi_devi_set_nodename: node already bound dip = %p,"
4157c478bd9Sstevel@tonic-gate 		    " %s -> %s", (void *)dip, ddi_node_name(dip), name);
4167c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
4177c478bd9Sstevel@tonic-gate 	}
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	nname = i_ddi_strdup(name, KM_SLEEP);
4207c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_node_name = nname;
4217c478bd9Sstevel@tonic-gate 	i_ddi_set_binding_name(dip, nname);
4227c478bd9Sstevel@tonic-gate 	kmem_free(oname, strlen(oname) + 1);
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	da_log_enter(dip);
4257c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
4267c478bd9Sstevel@tonic-gate }
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate void
4297c478bd9Sstevel@tonic-gate i_ddi_add_devimap(dev_info_t *dip)
4307c478bd9Sstevel@tonic-gate {
4317c478bd9Sstevel@tonic-gate 	struct devi_nodeid *elem;
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	ASSERT(dip);
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	if (!ndi_dev_is_persistent_node(dip))
4367c478bd9Sstevel@tonic-gate 		return;
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	ASSERT(ddi_get_parent(dip) == NULL || (DEVI_VHCI_NODE(dip)) ||
4397c478bd9Sstevel@tonic-gate 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	mutex_enter(&devimap->dno_lock);
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	ASSERT(devimap->dno_free);
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	elem = devimap->dno_free;
4467c478bd9Sstevel@tonic-gate 	devimap->dno_free = elem->next;
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	elem->nodeid = ddi_get_nodeid(dip);
4497c478bd9Sstevel@tonic-gate 	elem->dip = dip;
4507c478bd9Sstevel@tonic-gate 	elem->next = devimap->dno_head;
4517c478bd9Sstevel@tonic-gate 	devimap->dno_head = elem;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	devimap->dno_list_length++;
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	mutex_exit(&devimap->dno_lock);
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate static int
4597c478bd9Sstevel@tonic-gate i_ddi_remove_devimap(dev_info_t *dip)
4607c478bd9Sstevel@tonic-gate {
4617c478bd9Sstevel@tonic-gate 	struct devi_nodeid *prev, *elem;
4627c478bd9Sstevel@tonic-gate 	static const char *fcn = "i_ddi_remove_devimap";
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	ASSERT(dip);
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	if (!ndi_dev_is_persistent_node(dip))
4677c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	mutex_enter(&devimap->dno_lock);
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	/*
4727c478bd9Sstevel@tonic-gate 	 * The following check is done with dno_lock held
4737c478bd9Sstevel@tonic-gate 	 * to prevent race between dip removal and
4747c478bd9Sstevel@tonic-gate 	 * e_ddi_prom_node_to_dip()
4757c478bd9Sstevel@tonic-gate 	 */
4767c478bd9Sstevel@tonic-gate 	if (e_ddi_devi_holdcnt(dip)) {
4777c478bd9Sstevel@tonic-gate 		mutex_exit(&devimap->dno_lock);
4787c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4797c478bd9Sstevel@tonic-gate 	}
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	ASSERT(devimap->dno_head);
4827c478bd9Sstevel@tonic-gate 	ASSERT(devimap->dno_list_length > 0);
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	prev = NULL;
4857c478bd9Sstevel@tonic-gate 	for (elem = devimap->dno_head; elem; elem = elem->next) {
4867c478bd9Sstevel@tonic-gate 		if (elem->dip == dip) {
4877c478bd9Sstevel@tonic-gate 			ASSERT(elem->nodeid == ddi_get_nodeid(dip));
4887c478bd9Sstevel@tonic-gate 			break;
4897c478bd9Sstevel@tonic-gate 		}
4907c478bd9Sstevel@tonic-gate 		prev = elem;
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	if (elem && prev)
4947c478bd9Sstevel@tonic-gate 		prev->next = elem->next;
4957c478bd9Sstevel@tonic-gate 	else if (elem)
4967c478bd9Sstevel@tonic-gate 		devimap->dno_head = elem->next;
4977c478bd9Sstevel@tonic-gate 	else
4987c478bd9Sstevel@tonic-gate 		panic("%s: devinfo node(%p) not found",
4997c478bd9Sstevel@tonic-gate 		    fcn, (void *)dip);
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	devimap->dno_list_length--;
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	elem->nodeid = 0;
5047c478bd9Sstevel@tonic-gate 	elem->dip = NULL;
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	elem->next = devimap->dno_free;
5077c478bd9Sstevel@tonic-gate 	devimap->dno_free = elem;
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	mutex_exit(&devimap->dno_lock);
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
5127c478bd9Sstevel@tonic-gate }
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate /*
5157c478bd9Sstevel@tonic-gate  * Link this node into the devinfo tree and add to orphan list
5167c478bd9Sstevel@tonic-gate  * Not callable from interrupt context
5177c478bd9Sstevel@tonic-gate  */
5187c478bd9Sstevel@tonic-gate static void
5197c478bd9Sstevel@tonic-gate link_node(dev_info_t *dip)
5207c478bd9Sstevel@tonic-gate {
5217c478bd9Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
5227c478bd9Sstevel@tonic-gate 	struct dev_info *parent = devi->devi_parent;
5237c478bd9Sstevel@tonic-gate 	dev_info_t **dipp;
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	ASSERT(parent);	/* never called for root node */
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "link_node: parent = %s child = %s\n",
5287c478bd9Sstevel@tonic-gate 	    parent->devi_node_name, devi->devi_node_name));
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	/*
5317c478bd9Sstevel@tonic-gate 	 * Hold the global_vhci_lock before linking any direct
5327c478bd9Sstevel@tonic-gate 	 * children of rootnex driver. This special lock protects
5337c478bd9Sstevel@tonic-gate 	 * linking and unlinking for rootnext direct children.
5347c478bd9Sstevel@tonic-gate 	 */
5357c478bd9Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
5367c478bd9Sstevel@tonic-gate 		mutex_enter(&global_vhci_lock);
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	/*
5397c478bd9Sstevel@tonic-gate 	 * attach the node to end of the list unless the node is already there
5407c478bd9Sstevel@tonic-gate 	 */
5417c478bd9Sstevel@tonic-gate 	dipp = (dev_info_t **)(&DEVI(parent)->devi_child);
5427c478bd9Sstevel@tonic-gate 	while (*dipp && (*dipp != dip)) {
5437c478bd9Sstevel@tonic-gate 		dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling);
5447c478bd9Sstevel@tonic-gate 	}
5457c478bd9Sstevel@tonic-gate 	ASSERT(*dipp == NULL);	/* node is not linked */
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	/*
5487c478bd9Sstevel@tonic-gate 	 * Now that we are in the tree, update the devi-nodeid map.
5497c478bd9Sstevel@tonic-gate 	 */
5507c478bd9Sstevel@tonic-gate 	i_ddi_add_devimap(dip);
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	/*
5537c478bd9Sstevel@tonic-gate 	 * This is a temporary workaround for Bug 4618861.
5547c478bd9Sstevel@tonic-gate 	 * We keep the scsi_vhci nexus node on the left side of the devinfo
5557c478bd9Sstevel@tonic-gate 	 * tree (under the root nexus driver), so that virtual nodes under
5567c478bd9Sstevel@tonic-gate 	 * scsi_vhci will be SUSPENDed first and RESUMEd last.  This ensures
5577c478bd9Sstevel@tonic-gate 	 * that the pHCI nodes are active during times when their clients
5587c478bd9Sstevel@tonic-gate 	 * may be depending on them.  This workaround embodies the knowledge
5597c478bd9Sstevel@tonic-gate 	 * that system PM and CPR both traverse the tree left-to-right during
5607c478bd9Sstevel@tonic-gate 	 * SUSPEND and right-to-left during RESUME.
5619d3d2ed0Shiremath 	 * Extending the workaround to IB Nexus/VHCI
5629d3d2ed0Shiremath 	 * driver also.
5637c478bd9Sstevel@tonic-gate 	 */
5647c478bd9Sstevel@tonic-gate 	if (strcmp(devi->devi_name, "scsi_vhci") == 0) {
5657c478bd9Sstevel@tonic-gate 		/* Add scsi_vhci to beginning of list */
5667c478bd9Sstevel@tonic-gate 		ASSERT((dev_info_t *)parent == top_devinfo);
5677c478bd9Sstevel@tonic-gate 		/* scsi_vhci under rootnex */
5687c478bd9Sstevel@tonic-gate 		devi->devi_sibling = parent->devi_child;
5697c478bd9Sstevel@tonic-gate 		parent->devi_child = devi;
5709d3d2ed0Shiremath 	} else if (strcmp(devi->devi_name, "ib") == 0) {
5719d3d2ed0Shiremath 		i_link_vhci_node(dip);
5727c478bd9Sstevel@tonic-gate 	} else {
5737c478bd9Sstevel@tonic-gate 		/* Add to end of list */
5747c478bd9Sstevel@tonic-gate 		*dipp = dip;
5757c478bd9Sstevel@tonic-gate 		DEVI(dip)->devi_sibling = NULL;
5767c478bd9Sstevel@tonic-gate 	}
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	/*
5797c478bd9Sstevel@tonic-gate 	 * Release the global_vhci_lock before linking any direct
5807c478bd9Sstevel@tonic-gate 	 * children of rootnex driver.
5817c478bd9Sstevel@tonic-gate 	 */
5827c478bd9Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
5837c478bd9Sstevel@tonic-gate 		mutex_exit(&global_vhci_lock);
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	/* persistent nodes go on orphan list */
5867c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip))
5877c478bd9Sstevel@tonic-gate 		add_to_dn_list(&orphanlist, dip);
5887c478bd9Sstevel@tonic-gate }
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate /*
5917c478bd9Sstevel@tonic-gate  * Unlink this node from the devinfo tree
5927c478bd9Sstevel@tonic-gate  */
5937c478bd9Sstevel@tonic-gate static int
5947c478bd9Sstevel@tonic-gate unlink_node(dev_info_t *dip)
5957c478bd9Sstevel@tonic-gate {
5967c478bd9Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
5977c478bd9Sstevel@tonic-gate 	struct dev_info *parent = devi->devi_parent;
5987c478bd9Sstevel@tonic-gate 	dev_info_t **dipp;
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	ASSERT(parent != NULL);
6017c478bd9Sstevel@tonic-gate 	ASSERT(devi->devi_node_state == DS_LINKED);
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "unlink_node: name = %s\n",
6047c478bd9Sstevel@tonic-gate 	    ddi_node_name(dip)));
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	/* check references */
6077c478bd9Sstevel@tonic-gate 	if (devi->devi_ref || i_ddi_remove_devimap(dip) != DDI_SUCCESS)
6087c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	/*
6117c478bd9Sstevel@tonic-gate 	 * Hold the global_vhci_lock before linking any direct
6127c478bd9Sstevel@tonic-gate 	 * children of rootnex driver.
6137c478bd9Sstevel@tonic-gate 	 */
6147c478bd9Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
6157c478bd9Sstevel@tonic-gate 		mutex_enter(&global_vhci_lock);
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	dipp = (dev_info_t **)(&DEVI(parent)->devi_child);
6187c478bd9Sstevel@tonic-gate 	while (*dipp && (*dipp != dip)) {
6197c478bd9Sstevel@tonic-gate 		dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling);
6207c478bd9Sstevel@tonic-gate 	}
6217c478bd9Sstevel@tonic-gate 	if (*dipp) {
6227c478bd9Sstevel@tonic-gate 		*dipp = (dev_info_t *)(devi->devi_sibling);
6237c478bd9Sstevel@tonic-gate 		devi->devi_sibling = NULL;
6247c478bd9Sstevel@tonic-gate 	} else {
6257c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE, "unlink_node: %s not linked",
6267c478bd9Sstevel@tonic-gate 		    devi->devi_node_name));
6277c478bd9Sstevel@tonic-gate 	}
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	/*
6307c478bd9Sstevel@tonic-gate 	 * Release the global_vhci_lock before linking any direct
6317c478bd9Sstevel@tonic-gate 	 * children of rootnex driver.
6327c478bd9Sstevel@tonic-gate 	 */
6337c478bd9Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
6347c478bd9Sstevel@tonic-gate 		mutex_exit(&global_vhci_lock);
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	/* Remove node from orphan list */
6377c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
6387c478bd9Sstevel@tonic-gate 		remove_from_dn_list(&orphanlist, dip);
6397c478bd9Sstevel@tonic-gate 	}
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
6427c478bd9Sstevel@tonic-gate }
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate /*
6457c478bd9Sstevel@tonic-gate  * Bind this devinfo node to a driver. If compat is NON-NULL, try that first.
6467c478bd9Sstevel@tonic-gate  * Else, use the node-name.
6477c478bd9Sstevel@tonic-gate  *
6487c478bd9Sstevel@tonic-gate  * NOTE: IEEE1275 specifies that nodename should be tried before compatible.
6497c478bd9Sstevel@tonic-gate  *	Solaris implementation binds nodename after compatible.
6507c478bd9Sstevel@tonic-gate  *
6517c478bd9Sstevel@tonic-gate  * If we find a binding,
6527c478bd9Sstevel@tonic-gate  * - set the binding name to the the string,
6537c478bd9Sstevel@tonic-gate  * - set major number to driver major
6547c478bd9Sstevel@tonic-gate  *
6557c478bd9Sstevel@tonic-gate  * If we don't find a binding,
6567c478bd9Sstevel@tonic-gate  * - return failure
6577c478bd9Sstevel@tonic-gate  */
6587c478bd9Sstevel@tonic-gate static int
6597c478bd9Sstevel@tonic-gate bind_node(dev_info_t *dip)
6607c478bd9Sstevel@tonic-gate {
6617c478bd9Sstevel@tonic-gate 	char *p = NULL;
6627c478bd9Sstevel@tonic-gate 	major_t major = (major_t)(major_t)-1;
6637c478bd9Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
6647c478bd9Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	ASSERT(devi->devi_node_state == DS_LINKED);
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "bind_node: 0x%p(name = %s)\n",
6697c478bd9Sstevel@tonic-gate 	    (void *)dip, ddi_node_name(dip)));
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
6727c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_flags & DEVI_NO_BIND) {
6737c478bd9Sstevel@tonic-gate 		mutex_exit(&DEVI(dip)->devi_lock);
6747c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6757c478bd9Sstevel@tonic-gate 	}
6767c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	/* find the driver with most specific binding using compatible */
6797c478bd9Sstevel@tonic-gate 	major = ddi_compatible_driver_major(dip, &p);
6807c478bd9Sstevel@tonic-gate 	if (major == (major_t)-1)
6817c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	devi->devi_major = major;
6847c478bd9Sstevel@tonic-gate 	if (p != NULL) {
6857c478bd9Sstevel@tonic-gate 		i_ddi_set_binding_name(dip, p);
6867c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "bind_node: %s bound to %s\n",
6877c478bd9Sstevel@tonic-gate 		    devi->devi_node_name, p));
6887c478bd9Sstevel@tonic-gate 	}
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	/* Link node to per-driver list */
6917c478bd9Sstevel@tonic-gate 	link_to_driver_list(dip);
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	/*
6947c478bd9Sstevel@tonic-gate 	 * reset parent flag so that nexus will merge .conf props
6957c478bd9Sstevel@tonic-gate 	 */
6967c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
6977c478bd9Sstevel@tonic-gate 		mutex_enter(&DEVI(parent)->devi_lock);
6987c478bd9Sstevel@tonic-gate 		DEVI(parent)->devi_flags &=
6997c478bd9Sstevel@tonic-gate 		    ~(DEVI_ATTACHED_CHILDREN|DEVI_MADE_CHILDREN);
7007c478bd9Sstevel@tonic-gate 		mutex_exit(&DEVI(parent)->devi_lock);
7017c478bd9Sstevel@tonic-gate 	}
7027c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
7037c478bd9Sstevel@tonic-gate }
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate /*
7067c478bd9Sstevel@tonic-gate  * Unbind this devinfo node
7077c478bd9Sstevel@tonic-gate  * Called before the node is destroyed or driver is removed from system
7087c478bd9Sstevel@tonic-gate  */
7097c478bd9Sstevel@tonic-gate static int
7107c478bd9Sstevel@tonic-gate unbind_node(dev_info_t *dip)
7117c478bd9Sstevel@tonic-gate {
7127c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_node_state == DS_BOUND);
7137c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_major != (major_t)-1);
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	/* check references */
7167c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_ref)
7177c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "unbind_node: 0x%p(name = %s)\n",
7207c478bd9Sstevel@tonic-gate 	    (void *)dip, ddi_node_name(dip)));
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	unlink_from_driver_list(dip);
7237c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_major = (major_t)-1;
7247c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
7257c478bd9Sstevel@tonic-gate }
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate /*
7287c478bd9Sstevel@tonic-gate  * Initialize a node: calls the parent nexus' bus_ctl ops to do the operation.
7297c478bd9Sstevel@tonic-gate  * Must hold parent and per-driver list while calling this function.
7307c478bd9Sstevel@tonic-gate  * A successful init_node() returns with an active ndi_hold_devi() hold on
7317c478bd9Sstevel@tonic-gate  * the parent.
7327c478bd9Sstevel@tonic-gate  */
7337c478bd9Sstevel@tonic-gate static int
7347c478bd9Sstevel@tonic-gate init_node(dev_info_t *dip)
7357c478bd9Sstevel@tonic-gate {
7367c478bd9Sstevel@tonic-gate 	int error;
7377c478bd9Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
7387c478bd9Sstevel@tonic-gate 	int (*f)(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *, void *);
7397c478bd9Sstevel@tonic-gate 	char *path;
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_BOUND);
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	/* should be DS_READY except for pcmcia ... */
7447c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(pdip) >= DS_PROBED);
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7477c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(dip, path);
7487c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "init_node: entry: path %s 0x%p\n",
7497c478bd9Sstevel@tonic-gate 	    path, (void *)dip));
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	/*
7527c478bd9Sstevel@tonic-gate 	 * The parent must have a bus_ctl operation.
7537c478bd9Sstevel@tonic-gate 	 */
7547c478bd9Sstevel@tonic-gate 	if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
7557c478bd9Sstevel@tonic-gate 	    (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_ctl) == NULL) {
7567c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
7577c478bd9Sstevel@tonic-gate 		goto out;
7587c478bd9Sstevel@tonic-gate 	}
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	add_global_props(dip);
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	/*
7637c478bd9Sstevel@tonic-gate 	 * Invoke the parent's bus_ctl operation with the DDI_CTLOPS_INITCHILD
7647c478bd9Sstevel@tonic-gate 	 * command to transform the child to canonical form 1. If there
7657c478bd9Sstevel@tonic-gate 	 * is an error, ddi_remove_child should be called, to clean up.
7667c478bd9Sstevel@tonic-gate 	 */
7677c478bd9Sstevel@tonic-gate 	error = (*f)(pdip, pdip, DDI_CTLOPS_INITCHILD, dip, NULL);
7687c478bd9Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
7697c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "init_node: %s 0x%p failed\n",
7707c478bd9Sstevel@tonic-gate 		    path, (void *)dip));
7717c478bd9Sstevel@tonic-gate 		remove_global_props(dip);
7727c478bd9Sstevel@tonic-gate 		/* in case nexus driver didn't clear this field */
7737c478bd9Sstevel@tonic-gate 		ddi_set_name_addr(dip, NULL);
7747c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
7757c478bd9Sstevel@tonic-gate 		goto out;
7767c478bd9Sstevel@tonic-gate 	}
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	ndi_hold_devi(pdip);
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	/* check for duplicate nodes */
7817c478bd9Sstevel@tonic-gate 	if (find_duplicate_child(pdip, dip) != NULL) {
7827c478bd9Sstevel@tonic-gate 		/* recompute path after initchild for @addr information */
7837c478bd9Sstevel@tonic-gate 		(void) ddi_pathname(dip, path);
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 		/*
7867c478bd9Sstevel@tonic-gate 		 * uninit_node() the duplicate - a successful uninit_node()
7877c478bd9Sstevel@tonic-gate 		 * does a ndi_rele_devi
7887c478bd9Sstevel@tonic-gate 		 */
7897c478bd9Sstevel@tonic-gate 		if ((error = uninit_node(dip)) != DDI_SUCCESS) {
7907c478bd9Sstevel@tonic-gate 			ndi_rele_devi(pdip);
7917c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "init_node: uninit of duplicate "
7927c478bd9Sstevel@tonic-gate 			    "node %s failed", path);
7937c478bd9Sstevel@tonic-gate 		}
7947c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "init_node: duplicate uninit "
7957c478bd9Sstevel@tonic-gate 		    "%s 0x%p%s\n", path, (void *)dip,
7967c478bd9Sstevel@tonic-gate 		    (error == DDI_SUCCESS) ? "" : " failed"));
7977c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
7987c478bd9Sstevel@tonic-gate 		goto out;
7997c478bd9Sstevel@tonic-gate 	}
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	/*
8027c478bd9Sstevel@tonic-gate 	 * Apply multi-parent/deep-nexus optimization to the new node
8037c478bd9Sstevel@tonic-gate 	 */
8047c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
8057c478bd9Sstevel@tonic-gate 	ddi_optimize_dtree(dip);
8067c478bd9Sstevel@tonic-gate 	error = DDI_SUCCESS;
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate out:	kmem_free(path, MAXPATHLEN);
8097c478bd9Sstevel@tonic-gate 	return (error);
8107c478bd9Sstevel@tonic-gate }
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate /*
8137c478bd9Sstevel@tonic-gate  * Uninitialize node
8147c478bd9Sstevel@tonic-gate  * The per-driver list must be held busy during the call.
8157c478bd9Sstevel@tonic-gate  * A successful uninit_node() releases the init_node() hold on
8167c478bd9Sstevel@tonic-gate  * the parent by calling ndi_rele_devi().
8177c478bd9Sstevel@tonic-gate  */
8187c478bd9Sstevel@tonic-gate static int
8197c478bd9Sstevel@tonic-gate uninit_node(dev_info_t *dip)
8207c478bd9Sstevel@tonic-gate {
8217c478bd9Sstevel@tonic-gate 	int node_state_entry;
8227c478bd9Sstevel@tonic-gate 	dev_info_t *pdip;
8237c478bd9Sstevel@tonic-gate 	struct dev_ops *ops;
8247c478bd9Sstevel@tonic-gate 	int (*f)();
8257c478bd9Sstevel@tonic-gate 	int error;
8267c478bd9Sstevel@tonic-gate 	char *addr;
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	/*
8297c478bd9Sstevel@tonic-gate 	 * Don't check for references here or else a ref-counted
8307c478bd9Sstevel@tonic-gate 	 * dip cannot be downgraded by the framework.
8317c478bd9Sstevel@tonic-gate 	 */
8327c478bd9Sstevel@tonic-gate 	node_state_entry = i_ddi_node_state(dip);
8337c478bd9Sstevel@tonic-gate 	ASSERT((node_state_entry == DS_BOUND) ||
8347c478bd9Sstevel@tonic-gate 		(node_state_entry == DS_INITIALIZED));
8357c478bd9Sstevel@tonic-gate 	pdip = ddi_get_parent(dip);
8367c478bd9Sstevel@tonic-gate 	ASSERT(pdip);
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "uninit_node: 0x%p(%s%d)\n",
8397c478bd9Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 	if (((ops = ddi_get_driver(pdip)) == NULL) ||
8427c478bd9Sstevel@tonic-gate 	    (ops->devo_bus_ops == NULL) ||
8437c478bd9Sstevel@tonic-gate 	    ((f = ops->devo_bus_ops->bus_ctl) == NULL)) {
8447c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
8457c478bd9Sstevel@tonic-gate 	}
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	/*
8487c478bd9Sstevel@tonic-gate 	 * save the @addr prior to DDI_CTLOPS_UNINITCHILD for use in
8497c478bd9Sstevel@tonic-gate 	 * freeing the instance if it succeeds.
8507c478bd9Sstevel@tonic-gate 	 */
8517c478bd9Sstevel@tonic-gate 	if (node_state_entry == DS_INITIALIZED) {
8527c478bd9Sstevel@tonic-gate 		addr = ddi_get_name_addr(dip);
8537c478bd9Sstevel@tonic-gate 		if (addr)
8547c478bd9Sstevel@tonic-gate 			addr = i_ddi_strdup(addr, KM_SLEEP);
8557c478bd9Sstevel@tonic-gate 	} else {
8567c478bd9Sstevel@tonic-gate 		addr = NULL;
8577c478bd9Sstevel@tonic-gate 	}
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	error = (*f)(pdip, pdip, DDI_CTLOPS_UNINITCHILD, dip, (void *)NULL);
8607c478bd9Sstevel@tonic-gate 	if (error == DDI_SUCCESS) {
8617c478bd9Sstevel@tonic-gate 		/* if uninitchild forgot to set devi_addr to NULL do it now */
8627c478bd9Sstevel@tonic-gate 		ddi_set_name_addr(dip, NULL);
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 		/*
8657c478bd9Sstevel@tonic-gate 		 * Free instance number. This is a no-op if instance has
8667c478bd9Sstevel@tonic-gate 		 * been kept by probe_node().  Avoid free when we are called
8677c478bd9Sstevel@tonic-gate 		 * from init_node (DS_BOUND) because the instance has not yet
8687c478bd9Sstevel@tonic-gate 		 * been assigned.
8697c478bd9Sstevel@tonic-gate 		 */
8707c478bd9Sstevel@tonic-gate 		if (node_state_entry == DS_INITIALIZED) {
8717c478bd9Sstevel@tonic-gate 			e_ddi_free_instance(dip, addr);
8727c478bd9Sstevel@tonic-gate 			DEVI(dip)->devi_instance = -1;
8737c478bd9Sstevel@tonic-gate 		}
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 		/* release the init_node hold */
8767c478bd9Sstevel@tonic-gate 		ndi_rele_devi(pdip);
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 		remove_global_props(dip);
8797c478bd9Sstevel@tonic-gate 		e_ddi_prop_remove_all(dip);
8807c478bd9Sstevel@tonic-gate 	} else {
8817c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "uninit_node failed: 0x%p(%s%d)\n",
8827c478bd9Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
8837c478bd9Sstevel@tonic-gate 	}
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 	if (addr)
8867c478bd9Sstevel@tonic-gate 		kmem_free(addr, strlen(addr) + 1);
8877c478bd9Sstevel@tonic-gate 	return (error);
8887c478bd9Sstevel@tonic-gate }
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate /*
8917c478bd9Sstevel@tonic-gate  * Invoke driver's probe entry point to probe for existence of hardware.
8927c478bd9Sstevel@tonic-gate  * Keep instance permanent for successful probe and leaf nodes.
8937c478bd9Sstevel@tonic-gate  *
8947c478bd9Sstevel@tonic-gate  * Per-driver list must be held busy while calling this function.
8957c478bd9Sstevel@tonic-gate  */
8967c478bd9Sstevel@tonic-gate static int
8977c478bd9Sstevel@tonic-gate probe_node(dev_info_t *dip)
8987c478bd9Sstevel@tonic-gate {
8997c478bd9Sstevel@tonic-gate 	int rv;
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_INITIALIZED);
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "probe_node: 0x%p(%s%d)\n",
9047c478bd9Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	/* temporarily hold the driver while we probe */
9077c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
9087c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_ops == NULL) {
9097c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
9107c478bd9Sstevel@tonic-gate 		    "probe_node: 0x%p(%s%d) cannot load driver\n",
9117c478bd9Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
9127c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
9137c478bd9Sstevel@tonic-gate 	}
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 	if (identify_9e != 0)
9167c478bd9Sstevel@tonic-gate 		(void) devi_identify(dip);
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	rv = devi_probe(dip);
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 	/* release the driver now that probe is complete */
9217c478bd9Sstevel@tonic-gate 	ndi_rele_driver(dip);
9227c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_ops = NULL;
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	switch (rv) {
9257c478bd9Sstevel@tonic-gate 	case DDI_PROBE_SUCCESS:			/* found */
9267c478bd9Sstevel@tonic-gate 	case DDI_PROBE_DONTCARE:		/* ddi_dev_is_sid */
9277c478bd9Sstevel@tonic-gate 		e_ddi_keep_instance(dip);	/* persist instance */
9287c478bd9Sstevel@tonic-gate 		rv = DDI_SUCCESS;
9297c478bd9Sstevel@tonic-gate 		break;
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	case DDI_PROBE_PARTIAL:			/* maybe later */
9327c478bd9Sstevel@tonic-gate 	case DDI_PROBE_FAILURE:			/* not found */
9337c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
9347c478bd9Sstevel@tonic-gate 		    "probe_node: 0x%p(%s%d) no hardware found%s\n",
9357c478bd9Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip),
9367c478bd9Sstevel@tonic-gate 		    (rv == DDI_PROBE_PARTIAL) ? " yet" : ""));
9377c478bd9Sstevel@tonic-gate 		rv = DDI_FAILURE;
9387c478bd9Sstevel@tonic-gate 		break;
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 	default:
9417c478bd9Sstevel@tonic-gate #ifdef	DEBUG
9427c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "probe_node: %s%d: illegal probe(9E) value",
9437c478bd9Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
9447c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
9457c478bd9Sstevel@tonic-gate 		rv = DDI_FAILURE;
9467c478bd9Sstevel@tonic-gate 		break;
9477c478bd9Sstevel@tonic-gate 	}
9487c478bd9Sstevel@tonic-gate 	return (rv);
9497c478bd9Sstevel@tonic-gate }
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate /*
9527c478bd9Sstevel@tonic-gate  * Unprobe a node. Simply reset the node state.
9537c478bd9Sstevel@tonic-gate  * Per-driver list must be held busy while calling this function.
9547c478bd9Sstevel@tonic-gate  */
9557c478bd9Sstevel@tonic-gate static int
9567c478bd9Sstevel@tonic-gate unprobe_node(dev_info_t *dip)
9577c478bd9Sstevel@tonic-gate {
9587c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_PROBED);
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	/*
9617c478bd9Sstevel@tonic-gate 	 * Don't check for references here or else a ref-counted
9627c478bd9Sstevel@tonic-gate 	 * dip cannot be downgraded by the framework.
9637c478bd9Sstevel@tonic-gate 	 */
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "unprobe_node: 0x%p(name = %s)\n",
9667c478bd9Sstevel@tonic-gate 	    (void *)dip, ddi_node_name(dip)));
9677c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
9687c478bd9Sstevel@tonic-gate }
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate /*
9717c478bd9Sstevel@tonic-gate  * Attach devinfo node.
9727c478bd9Sstevel@tonic-gate  * Per-driver list must be held busy.
9737c478bd9Sstevel@tonic-gate  */
9747c478bd9Sstevel@tonic-gate static int
9757c478bd9Sstevel@tonic-gate attach_node(dev_info_t *dip)
9767c478bd9Sstevel@tonic-gate {
9777c478bd9Sstevel@tonic-gate 	int rv;
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_PROBED);
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d)\n",
9827c478bd9Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 	/*
9857c478bd9Sstevel@tonic-gate 	 * Tell mpxio framework that a node is about to online.
9867c478bd9Sstevel@tonic-gate 	 */
9877c478bd9Sstevel@tonic-gate 	if ((rv = mdi_devi_online(dip, 0)) != NDI_SUCCESS) {
9887c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
9897c478bd9Sstevel@tonic-gate 	}
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 	/* no recursive attachment */
9927c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_ops == NULL);
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 	/*
9957c478bd9Sstevel@tonic-gate 	 * Hold driver the node is bound to.
9967c478bd9Sstevel@tonic-gate 	 */
9977c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
9987c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_ops == NULL) {
9997c478bd9Sstevel@tonic-gate 		/*
10007c478bd9Sstevel@tonic-gate 		 * We were able to load driver for probing, so we should
10017c478bd9Sstevel@tonic-gate 		 * not get here unless something really bad happened.
10027c478bd9Sstevel@tonic-gate 		 */
10037c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "attach_node: no driver for major %d",
10047c478bd9Sstevel@tonic-gate 		    DEVI(dip)->devi_major);
10057c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
10067c478bd9Sstevel@tonic-gate 	}
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	if (NEXUS_DRV(DEVI(dip)->devi_ops))
10097c478bd9Sstevel@tonic-gate 		DEVI(dip)->devi_taskq = ddi_taskq_create(dip,
10107c478bd9Sstevel@tonic-gate 		    "nexus_enum_tq", 1,
10117c478bd9Sstevel@tonic-gate 		    TASKQ_DEFAULTPRI, 0);
10127c478bd9Sstevel@tonic-gate 
101316747f41Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
10147c478bd9Sstevel@tonic-gate 	DEVI_SET_ATTACHING(dip);
10157c478bd9Sstevel@tonic-gate 	DEVI_SET_NEED_RESET(dip);
101616747f41Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
101716747f41Scth 
10187c478bd9Sstevel@tonic-gate 	rv = devi_attach(dip, DDI_ATTACH);
101916747f41Scth 
102016747f41Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
10217c478bd9Sstevel@tonic-gate 	if (rv != DDI_SUCCESS)
10227c478bd9Sstevel@tonic-gate 		DEVI_CLR_NEED_RESET(dip);
10237c478bd9Sstevel@tonic-gate 	DEVI_CLR_ATTACHING(dip);
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	if (rv != DDI_SUCCESS) {
1026225b11cdScth 		/* ensure that devids are unregistered */
10277c478bd9Sstevel@tonic-gate 		if (DEVI(dip)->devi_flags & DEVI_REGISTERED_DEVID) {
10287c478bd9Sstevel@tonic-gate 			DEVI(dip)->devi_flags &= ~DEVI_REGISTERED_DEVID;
1029225b11cdScth 			mutex_exit(&DEVI(dip)->devi_lock);
1030225b11cdScth 
1031225b11cdScth 			e_devid_cache_unregister(dip);
1032225b11cdScth 		} else
1033225b11cdScth 			mutex_exit(&DEVI(dip)->devi_lock);
1034225b11cdScth 
10357c478bd9Sstevel@tonic-gate 		/*
10367c478bd9Sstevel@tonic-gate 		 * Cleanup dacf reservations
10377c478bd9Sstevel@tonic-gate 		 */
10387c478bd9Sstevel@tonic-gate 		mutex_enter(&dacf_lock);
10397c478bd9Sstevel@tonic-gate 		dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH);
10407c478bd9Sstevel@tonic-gate 		dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH);
10417c478bd9Sstevel@tonic-gate 		mutex_exit(&dacf_lock);
10427c478bd9Sstevel@tonic-gate 		if (DEVI(dip)->devi_taskq)
10437c478bd9Sstevel@tonic-gate 			ddi_taskq_destroy(DEVI(dip)->devi_taskq);
10447c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 		/* release the driver if attach failed */
10477c478bd9Sstevel@tonic-gate 		ndi_rele_driver(dip);
10487c478bd9Sstevel@tonic-gate 		DEVI(dip)->devi_ops = NULL;
10497c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d) failed\n",
10507c478bd9Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
10517c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
105216747f41Scth 	} else
105316747f41Scth 		mutex_exit(&DEVI(dip)->devi_lock);
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 	/* successful attach, return with driver held */
105616747f41Scth 
10577c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
10587c478bd9Sstevel@tonic-gate }
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate /*
10617c478bd9Sstevel@tonic-gate  * Detach devinfo node.
10627c478bd9Sstevel@tonic-gate  * Per-driver list must be held busy.
10637c478bd9Sstevel@tonic-gate  */
10647c478bd9Sstevel@tonic-gate static int
10657c478bd9Sstevel@tonic-gate detach_node(dev_info_t *dip, uint_t flag)
10667c478bd9Sstevel@tonic-gate {
1067cfbaf6c3Scth 	struct devnames	*dnp;
10687c478bd9Sstevel@tonic-gate 	int		rv;
1069cfbaf6c3Scth 
10707c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_ATTACHED);
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 	/* check references */
10737c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_ref)
10747c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "detach_node: 0x%p(%s%d)\n",
10777c478bd9Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 	/* Offline the device node with the mpxio framework. */
10807c478bd9Sstevel@tonic-gate 	if (mdi_devi_offline(dip, flag) != NDI_SUCCESS) {
10817c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
10827c478bd9Sstevel@tonic-gate 	}
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 	/* drain the taskq */
10857c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_taskq)
10867c478bd9Sstevel@tonic-gate 		ddi_taskq_wait(DEVI(dip)->devi_taskq);
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 	rv = devi_detach(dip, DDI_DETACH);
108916747f41Scth 	if (rv == DDI_SUCCESS) {
109016747f41Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
10917c478bd9Sstevel@tonic-gate 		DEVI_CLR_NEED_RESET(dip);
109216747f41Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
109316747f41Scth 	}
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 	if (rv != DDI_SUCCESS) {
10967c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
10977c478bd9Sstevel@tonic-gate 		    "detach_node: 0x%p(%s%d) failed\n",
10987c478bd9Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
10997c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
11007c478bd9Sstevel@tonic-gate 	}
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 	/* destroy the taskq */
11037c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_taskq) {
11047c478bd9Sstevel@tonic-gate 		ddi_taskq_destroy(DEVI(dip)->devi_taskq);
11057c478bd9Sstevel@tonic-gate 		DEVI(dip)->devi_taskq = NULL;
11067c478bd9Sstevel@tonic-gate 	}
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	/* Cleanup dacf reservations */
11097c478bd9Sstevel@tonic-gate 	mutex_enter(&dacf_lock);
11107c478bd9Sstevel@tonic-gate 	dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH);
11117c478bd9Sstevel@tonic-gate 	dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH);
11127c478bd9Sstevel@tonic-gate 	mutex_exit(&dacf_lock);
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 	/* Remove properties and minor nodes in case driver forgots */
11157c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
11167c478bd9Sstevel@tonic-gate 	ddi_prop_remove_all(dip);
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 	/* a detached node can't have attached or .conf children */
11197c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
11207c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN|DEVI_ATTACHED_CHILDREN);
1121225b11cdScth 
1122225b11cdScth 	/* ensure that devids registered during attach are unregistered */
11237c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_flags & DEVI_REGISTERED_DEVID) {
11247c478bd9Sstevel@tonic-gate 		DEVI(dip)->devi_flags &= ~DEVI_REGISTERED_DEVID;
1125225b11cdScth 		mutex_exit(&DEVI(dip)->devi_lock);
1126225b11cdScth 
1127225b11cdScth 		e_devid_cache_unregister(dip);
1128225b11cdScth 	} else
11297c478bd9Sstevel@tonic-gate 		mutex_exit(&DEVI(dip)->devi_lock);
11307c478bd9Sstevel@tonic-gate 
1131cfbaf6c3Scth 	/*
1132cfbaf6c3Scth 	 * If the instance has successfully detached in detach_driver() context,
1133cfbaf6c3Scth 	 * clear DN_DRIVER_HELD for correct ddi_hold_installed_driver()
1134cfbaf6c3Scth 	 * behavior. Consumers like qassociate() depend on this (via clnopen()).
1135cfbaf6c3Scth 	 */
1136cfbaf6c3Scth 	if (flag & NDI_DETACH_DRIVER) {
1137cfbaf6c3Scth 		dnp = &(devnamesp[DEVI(dip)->devi_major]);
1138cfbaf6c3Scth 		LOCK_DEV_OPS(&dnp->dn_lock);
1139cfbaf6c3Scth 		dnp->dn_flags &= ~DN_DRIVER_HELD;
1140cfbaf6c3Scth 		UNLOCK_DEV_OPS(&dnp->dn_lock);
1141cfbaf6c3Scth 	}
1142cfbaf6c3Scth 
11437c478bd9Sstevel@tonic-gate 	/* successful detach, release the driver */
11447c478bd9Sstevel@tonic-gate 	ndi_rele_driver(dip);
11457c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_ops = NULL;
11467c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
11477c478bd9Sstevel@tonic-gate }
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate /*
11507c478bd9Sstevel@tonic-gate  * Run dacf post_attach routines
11517c478bd9Sstevel@tonic-gate  */
11527c478bd9Sstevel@tonic-gate static int
11537c478bd9Sstevel@tonic-gate postattach_node(dev_info_t *dip)
11547c478bd9Sstevel@tonic-gate {
11557c478bd9Sstevel@tonic-gate 	int rval;
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	/*
11587c478bd9Sstevel@tonic-gate 	 * For hotplug busses like USB, it's possible that devices
11597c478bd9Sstevel@tonic-gate 	 * are removed but dip is still around. We don't want to
11607c478bd9Sstevel@tonic-gate 	 * run dacf routines as part of detach failure recovery.
11617c478bd9Sstevel@tonic-gate 	 *
11627c478bd9Sstevel@tonic-gate 	 * Pretend success until we figure out how to prevent
11637c478bd9Sstevel@tonic-gate 	 * access to such devinfo nodes.
11647c478bd9Sstevel@tonic-gate 	 */
11657c478bd9Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_REMOVED(dip))
11667c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	/*
11697c478bd9Sstevel@tonic-gate 	 * if dacf_postattach failed, report it to the framework
11707c478bd9Sstevel@tonic-gate 	 * so that it can be retried later at the open time.
11717c478bd9Sstevel@tonic-gate 	 */
11727c478bd9Sstevel@tonic-gate 	mutex_enter(&dacf_lock);
11737c478bd9Sstevel@tonic-gate 	rval = dacfc_postattach(dip);
11747c478bd9Sstevel@tonic-gate 	mutex_exit(&dacf_lock);
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 	/*
11777c478bd9Sstevel@tonic-gate 	 * Plumbing during postattach may fail because of the
11787c478bd9Sstevel@tonic-gate 	 * underlying device is not ready. This will fail ndi_devi_config()
11797c478bd9Sstevel@tonic-gate 	 * in dv_filldir() and a warning message is issued. The message
11807c478bd9Sstevel@tonic-gate 	 * from here will explain what happened
11817c478bd9Sstevel@tonic-gate 	 */
11827c478bd9Sstevel@tonic-gate 	if (rval != DACF_SUCCESS) {
11837c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "Postattach failed for %s%d\n",
11847c478bd9Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
11857c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
11867c478bd9Sstevel@tonic-gate 	}
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
11897c478bd9Sstevel@tonic-gate }
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate /*
11927c478bd9Sstevel@tonic-gate  * Run dacf pre-detach routines
11937c478bd9Sstevel@tonic-gate  */
11947c478bd9Sstevel@tonic-gate static int
11957c478bd9Sstevel@tonic-gate predetach_node(dev_info_t *dip, uint_t flag)
11967c478bd9Sstevel@tonic-gate {
11977c478bd9Sstevel@tonic-gate 	int ret;
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 	/*
12007c478bd9Sstevel@tonic-gate 	 * Don't auto-detach if DDI_FORCEATTACH or DDI_NO_AUTODETACH
12017c478bd9Sstevel@tonic-gate 	 * properties are set.
12027c478bd9Sstevel@tonic-gate 	 */
12037c478bd9Sstevel@tonic-gate 	if (flag & NDI_AUTODETACH) {
12047c478bd9Sstevel@tonic-gate 		struct devnames *dnp;
12057c478bd9Sstevel@tonic-gate 		int pflag = DDI_PROP_NOTPROM | DDI_PROP_DONTPASS;
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 		if ((ddi_prop_get_int(DDI_DEV_T_ANY, dip,
12087c478bd9Sstevel@tonic-gate 			pflag, DDI_FORCEATTACH, 0) == 1) ||
12097c478bd9Sstevel@tonic-gate 		    (ddi_prop_get_int(DDI_DEV_T_ANY, dip,
12107c478bd9Sstevel@tonic-gate 			pflag, DDI_NO_AUTODETACH, 0) == 1))
12117c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 		/* check for driver global version of DDI_NO_AUTODETACH */
12147c478bd9Sstevel@tonic-gate 		dnp = &devnamesp[DEVI(dip)->devi_major];
12157c478bd9Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
12167c478bd9Sstevel@tonic-gate 		if (dnp->dn_flags & DN_NO_AUTODETACH) {
12177c478bd9Sstevel@tonic-gate 			UNLOCK_DEV_OPS(&dnp->dn_lock);
12187c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
12197c478bd9Sstevel@tonic-gate 		}
12207c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
12217c478bd9Sstevel@tonic-gate 	}
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 	mutex_enter(&dacf_lock);
12247c478bd9Sstevel@tonic-gate 	ret = dacfc_predetach(dip);
12257c478bd9Sstevel@tonic-gate 	mutex_exit(&dacf_lock);
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 	return (ret);
12287c478bd9Sstevel@tonic-gate }
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate /*
12317c478bd9Sstevel@tonic-gate  * Wrapper for making multiple state transitions
12327c478bd9Sstevel@tonic-gate  */
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate /*
12357c478bd9Sstevel@tonic-gate  * i_ndi_config_node: upgrade dev_info node into a specified state.
12367c478bd9Sstevel@tonic-gate  * It is a bit tricky because the locking protocol changes before and
12377c478bd9Sstevel@tonic-gate  * after a node is bound to a driver. All locks are held external to
12387c478bd9Sstevel@tonic-gate  * this function.
12397c478bd9Sstevel@tonic-gate  */
12407c478bd9Sstevel@tonic-gate int
12417c478bd9Sstevel@tonic-gate i_ndi_config_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag)
12427c478bd9Sstevel@tonic-gate {
12437c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flag))
12447c478bd9Sstevel@tonic-gate 	int rv = DDI_SUCCESS;
12457c478bd9Sstevel@tonic-gate 
12467c478bd9Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 	while ((i_ddi_node_state(dip) < state) && (rv == DDI_SUCCESS)) {
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 		/* don't allow any more changes to the device tree */
12517c478bd9Sstevel@tonic-gate 		if (devinfo_freeze) {
12527c478bd9Sstevel@tonic-gate 			rv = DDI_FAILURE;
12537c478bd9Sstevel@tonic-gate 			break;
12547c478bd9Sstevel@tonic-gate 		}
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate 		switch (i_ddi_node_state(dip)) {
12577c478bd9Sstevel@tonic-gate 		case DS_PROTO:
12587c478bd9Sstevel@tonic-gate 			/*
12597c478bd9Sstevel@tonic-gate 			 * only caller can reference this node, no external
12607c478bd9Sstevel@tonic-gate 			 * locking needed.
12617c478bd9Sstevel@tonic-gate 			 */
12627c478bd9Sstevel@tonic-gate 			link_node(dip);
12637c478bd9Sstevel@tonic-gate 			i_ddi_set_node_state(dip, DS_LINKED);
12647c478bd9Sstevel@tonic-gate 			break;
12657c478bd9Sstevel@tonic-gate 		case DS_LINKED:
12667c478bd9Sstevel@tonic-gate 			/*
12677c478bd9Sstevel@tonic-gate 			 * Three code path may attempt to bind a node:
12687c478bd9Sstevel@tonic-gate 			 * - boot code
12697c478bd9Sstevel@tonic-gate 			 * - add_drv
12707c478bd9Sstevel@tonic-gate 			 * - hotplug thread
12717c478bd9Sstevel@tonic-gate 			 * Boot code is single threaded, add_drv synchronize
12727c478bd9Sstevel@tonic-gate 			 * on a userland lock, and hotplug synchronize on
12737c478bd9Sstevel@tonic-gate 			 * hotplug_lk. There could be a race between add_drv
12747c478bd9Sstevel@tonic-gate 			 * and hotplug thread. We'll live with this until the
12757c478bd9Sstevel@tonic-gate 			 * conversion to top-down loading.
12767c478bd9Sstevel@tonic-gate 			 */
12777c478bd9Sstevel@tonic-gate 			if ((rv = bind_node(dip)) == DDI_SUCCESS)
12787c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_BOUND);
12797c478bd9Sstevel@tonic-gate 			break;
12807c478bd9Sstevel@tonic-gate 		case DS_BOUND:
12817c478bd9Sstevel@tonic-gate 			/*
12827c478bd9Sstevel@tonic-gate 			 * The following transitions synchronizes on the
12837c478bd9Sstevel@tonic-gate 			 * per-driver busy changing flag, since we already
12847c478bd9Sstevel@tonic-gate 			 * have a driver.
12857c478bd9Sstevel@tonic-gate 			 */
12867c478bd9Sstevel@tonic-gate 			if ((rv = init_node(dip)) == DDI_SUCCESS)
12877c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_INITIALIZED);
12887c478bd9Sstevel@tonic-gate 			break;
12897c478bd9Sstevel@tonic-gate 		case DS_INITIALIZED:
12907c478bd9Sstevel@tonic-gate 			if ((rv = probe_node(dip)) == DDI_SUCCESS)
12917c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_PROBED);
12927c478bd9Sstevel@tonic-gate 			break;
12937c478bd9Sstevel@tonic-gate 		case DS_PROBED:
12947c478bd9Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, 1);
12957c478bd9Sstevel@tonic-gate 			if ((rv = attach_node(dip)) == DDI_SUCCESS)
12967c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_ATTACHED);
12977c478bd9Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, -1);
12987c478bd9Sstevel@tonic-gate 			break;
12997c478bd9Sstevel@tonic-gate 		case DS_ATTACHED:
13007c478bd9Sstevel@tonic-gate 			if ((rv = postattach_node(dip)) == DDI_SUCCESS)
13017c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_READY);
13027c478bd9Sstevel@tonic-gate 			break;
13037c478bd9Sstevel@tonic-gate 		case DS_READY:
13047c478bd9Sstevel@tonic-gate 			break;
13057c478bd9Sstevel@tonic-gate 		default:
13067c478bd9Sstevel@tonic-gate 			/* should never reach here */
13077c478bd9Sstevel@tonic-gate 			ASSERT("unknown devinfo state");
13087c478bd9Sstevel@tonic-gate 		}
13097c478bd9Sstevel@tonic-gate 	}
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate 	if (ddidebug & DDI_AUDIT)
13127c478bd9Sstevel@tonic-gate 		da_log_enter(dip);
13137c478bd9Sstevel@tonic-gate 	return (rv);
13147c478bd9Sstevel@tonic-gate }
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate /*
13177c478bd9Sstevel@tonic-gate  * i_ndi_unconfig_node: downgrade dev_info node into a specified state.
13187c478bd9Sstevel@tonic-gate  */
13197c478bd9Sstevel@tonic-gate int
13207c478bd9Sstevel@tonic-gate i_ndi_unconfig_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag)
13217c478bd9Sstevel@tonic-gate {
13227c478bd9Sstevel@tonic-gate 	int rv = DDI_SUCCESS;
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 	while ((i_ddi_node_state(dip) > state) && (rv == DDI_SUCCESS)) {
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate 		/* don't allow any more changes to the device tree */
13297c478bd9Sstevel@tonic-gate 		if (devinfo_freeze) {
13307c478bd9Sstevel@tonic-gate 			rv = DDI_FAILURE;
13317c478bd9Sstevel@tonic-gate 			break;
13327c478bd9Sstevel@tonic-gate 		}
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate 		switch (i_ddi_node_state(dip)) {
13357c478bd9Sstevel@tonic-gate 		case DS_PROTO:
13367c478bd9Sstevel@tonic-gate 			break;
13377c478bd9Sstevel@tonic-gate 		case DS_LINKED:
13387c478bd9Sstevel@tonic-gate 			/*
13397c478bd9Sstevel@tonic-gate 			 * Persistent nodes are only removed by hotplug code
13407c478bd9Sstevel@tonic-gate 			 * .conf nodes synchronizes on per-driver list.
13417c478bd9Sstevel@tonic-gate 			 */
13427c478bd9Sstevel@tonic-gate 			if ((rv = unlink_node(dip)) == DDI_SUCCESS)
13437c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_PROTO);
13447c478bd9Sstevel@tonic-gate 			break;
13457c478bd9Sstevel@tonic-gate 		case DS_BOUND:
13467c478bd9Sstevel@tonic-gate 			/*
13477c478bd9Sstevel@tonic-gate 			 * The following transitions synchronizes on the
13487c478bd9Sstevel@tonic-gate 			 * per-driver busy changing flag, since we already
13497c478bd9Sstevel@tonic-gate 			 * have a driver.
13507c478bd9Sstevel@tonic-gate 			 */
13517c478bd9Sstevel@tonic-gate 			if ((rv = unbind_node(dip)) == DDI_SUCCESS)
13527c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_LINKED);
13537c478bd9Sstevel@tonic-gate 			break;
13547c478bd9Sstevel@tonic-gate 		case DS_INITIALIZED:
13557c478bd9Sstevel@tonic-gate 			if ((rv = uninit_node(dip)) == DDI_SUCCESS)
13567c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_BOUND);
13577c478bd9Sstevel@tonic-gate 			break;
13587c478bd9Sstevel@tonic-gate 		case DS_PROBED:
13597c478bd9Sstevel@tonic-gate 			if ((rv = unprobe_node(dip)) == DDI_SUCCESS)
13607c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_INITIALIZED);
13617c478bd9Sstevel@tonic-gate 			break;
13627c478bd9Sstevel@tonic-gate 		case DS_ATTACHED:
13637c478bd9Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, 1);
136416747f41Scth 
136516747f41Scth 			mutex_enter(&(DEVI(dip)->devi_lock));
13667c478bd9Sstevel@tonic-gate 			DEVI_SET_DETACHING(dip);
136716747f41Scth 			mutex_exit(&(DEVI(dip)->devi_lock));
136816747f41Scth 
13697c478bd9Sstevel@tonic-gate 			membar_enter();	/* ensure visibility for hold_devi */
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 			if ((rv = detach_node(dip, flag)) == DDI_SUCCESS)
13727c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_PROBED);
137316747f41Scth 
137416747f41Scth 			mutex_enter(&(DEVI(dip)->devi_lock));
13757c478bd9Sstevel@tonic-gate 			DEVI_CLR_DETACHING(dip);
137616747f41Scth 			mutex_exit(&(DEVI(dip)->devi_lock));
137716747f41Scth 
13787c478bd9Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, -1);
13797c478bd9Sstevel@tonic-gate 			break;
13807c478bd9Sstevel@tonic-gate 		case DS_READY:
13817c478bd9Sstevel@tonic-gate 			if ((rv = predetach_node(dip, flag)) == DDI_SUCCESS)
13827c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_ATTACHED);
13837c478bd9Sstevel@tonic-gate 			break;
13847c478bd9Sstevel@tonic-gate 		default:
13857c478bd9Sstevel@tonic-gate 			ASSERT("unknown devinfo state");
13867c478bd9Sstevel@tonic-gate 		}
13877c478bd9Sstevel@tonic-gate 	}
13887c478bd9Sstevel@tonic-gate 	da_log_enter(dip);
13897c478bd9Sstevel@tonic-gate 	return (rv);
13907c478bd9Sstevel@tonic-gate }
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate /*
13937c478bd9Sstevel@tonic-gate  * ddi_initchild: transform node to DS_INITIALIZED state
13947c478bd9Sstevel@tonic-gate  */
13957c478bd9Sstevel@tonic-gate int
13967c478bd9Sstevel@tonic-gate ddi_initchild(dev_info_t *parent, dev_info_t *proto)
13977c478bd9Sstevel@tonic-gate {
13987c478bd9Sstevel@tonic-gate 	int ret, circ;
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
14017c478bd9Sstevel@tonic-gate 	ret = i_ndi_config_node(proto, DS_INITIALIZED, 0);
14027c478bd9Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 	return (ret);
14057c478bd9Sstevel@tonic-gate }
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate /*
14087c478bd9Sstevel@tonic-gate  * ddi_uninitchild: transform node down to DS_BOUND state
14097c478bd9Sstevel@tonic-gate  */
14107c478bd9Sstevel@tonic-gate int
14117c478bd9Sstevel@tonic-gate ddi_uninitchild(dev_info_t *dip)
14127c478bd9Sstevel@tonic-gate {
14137c478bd9Sstevel@tonic-gate 	int ret, circ;
14147c478bd9Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
14157c478bd9Sstevel@tonic-gate 	ASSERT(parent);
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
14187c478bd9Sstevel@tonic-gate 	ret = i_ndi_unconfig_node(dip, DS_BOUND, 0);
14197c478bd9Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 	return (ret);
14227c478bd9Sstevel@tonic-gate }
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate /*
14257c478bd9Sstevel@tonic-gate  * i_ddi_attachchild: transform node to DS_READY state
14267c478bd9Sstevel@tonic-gate  */
14277c478bd9Sstevel@tonic-gate static int
14287c478bd9Sstevel@tonic-gate i_ddi_attachchild(dev_info_t *dip)
14297c478bd9Sstevel@tonic-gate {
14307c478bd9Sstevel@tonic-gate 	int ret, circ;
14317c478bd9Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
14327c478bd9Sstevel@tonic-gate 	ASSERT(parent);
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 	if ((i_ddi_node_state(dip) < DS_BOUND) || DEVI_IS_DEVICE_OFFLINE(dip))
14357c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
14387c478bd9Sstevel@tonic-gate 	ret = i_ndi_config_node(dip, DS_READY, 0);
14397c478bd9Sstevel@tonic-gate 	if (ret == NDI_SUCCESS) {
14407c478bd9Sstevel@tonic-gate 		ret = DDI_SUCCESS;
14417c478bd9Sstevel@tonic-gate 	} else {
14427c478bd9Sstevel@tonic-gate 		/*
14437c478bd9Sstevel@tonic-gate 		 * Take it down to DS_INITIALIZED so pm_pre_probe is run
14447c478bd9Sstevel@tonic-gate 		 * on the next attach
14457c478bd9Sstevel@tonic-gate 		 */
14467c478bd9Sstevel@tonic-gate 		(void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0);
14477c478bd9Sstevel@tonic-gate 		ret = DDI_FAILURE;
14487c478bd9Sstevel@tonic-gate 	}
14497c478bd9Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 	return (ret);
14527c478bd9Sstevel@tonic-gate }
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate /*
14557c478bd9Sstevel@tonic-gate  * i_ddi_detachchild: transform node down to DS_PROBED state
14567c478bd9Sstevel@tonic-gate  *	If it fails, put it back to DS_READY state.
14577c478bd9Sstevel@tonic-gate  * NOTE: A node that fails detach may be at DS_ATTACHED instead
14587c478bd9Sstevel@tonic-gate  * of DS_READY for a small amount of time.
14597c478bd9Sstevel@tonic-gate  */
14607c478bd9Sstevel@tonic-gate static int
14617c478bd9Sstevel@tonic-gate i_ddi_detachchild(dev_info_t *dip, uint_t flags)
14627c478bd9Sstevel@tonic-gate {
14637c478bd9Sstevel@tonic-gate 	int ret, circ;
14647c478bd9Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
14657c478bd9Sstevel@tonic-gate 	ASSERT(parent);
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
14687c478bd9Sstevel@tonic-gate 	ret = i_ndi_unconfig_node(dip, DS_PROBED, flags);
14697c478bd9Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
14707c478bd9Sstevel@tonic-gate 		(void) i_ndi_config_node(dip, DS_READY, 0);
14717c478bd9Sstevel@tonic-gate 	else
14727c478bd9Sstevel@tonic-gate 		/* allow pm_pre_probe to reestablish pm state */
14737c478bd9Sstevel@tonic-gate 		(void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0);
14747c478bd9Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 	return (ret);
14777c478bd9Sstevel@tonic-gate }
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate /*
14807c478bd9Sstevel@tonic-gate  * Add a child and bind to driver
14817c478bd9Sstevel@tonic-gate  */
14827c478bd9Sstevel@tonic-gate dev_info_t *
14837c478bd9Sstevel@tonic-gate ddi_add_child(dev_info_t *pdip, char *name, uint_t nodeid, uint_t unit)
14847c478bd9Sstevel@tonic-gate {
14857c478bd9Sstevel@tonic-gate 	int circ;
14867c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
14877c478bd9Sstevel@tonic-gate 
14887c478bd9Sstevel@tonic-gate 	/* allocate a new node */
14897c478bd9Sstevel@tonic-gate 	dip = i_ddi_alloc_node(pdip, name, nodeid, (int)unit, NULL, KM_SLEEP);
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
14927c478bd9Sstevel@tonic-gate 	(void) i_ndi_config_node(dip, DS_BOUND, 0);
14937c478bd9Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
14947c478bd9Sstevel@tonic-gate 	return (dip);
14957c478bd9Sstevel@tonic-gate }
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate /*
14987c478bd9Sstevel@tonic-gate  * ddi_remove_child: remove the dip. The parent must be attached and held
14997c478bd9Sstevel@tonic-gate  */
15007c478bd9Sstevel@tonic-gate int
15017c478bd9Sstevel@tonic-gate ddi_remove_child(dev_info_t *dip, int dummy)
15027c478bd9Sstevel@tonic-gate {
15037c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dummy))
15047c478bd9Sstevel@tonic-gate 	int circ, ret;
15057c478bd9Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
15067c478bd9Sstevel@tonic-gate 	ASSERT(parent);
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate 	/*
15117c478bd9Sstevel@tonic-gate 	 * If we still have children, for example SID nodes marked
15127c478bd9Sstevel@tonic-gate 	 * as persistent but not attached, attempt to remove them.
15137c478bd9Sstevel@tonic-gate 	 */
15147c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_child) {
15157c478bd9Sstevel@tonic-gate 		ret = ndi_devi_unconfig(dip, NDI_DEVI_REMOVE);
15167c478bd9Sstevel@tonic-gate 		if (ret != NDI_SUCCESS) {
15177c478bd9Sstevel@tonic-gate 			ndi_devi_exit(parent, circ);
15187c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
15197c478bd9Sstevel@tonic-gate 		}
15207c478bd9Sstevel@tonic-gate 		ASSERT(DEVI(dip)->devi_child == NULL);
15217c478bd9Sstevel@tonic-gate 	}
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 	ret = i_ndi_unconfig_node(dip, DS_PROTO, 0);
15247c478bd9Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
15277c478bd9Sstevel@tonic-gate 		return (ret);
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_PROTO);
15307c478bd9Sstevel@tonic-gate 	i_ddi_free_node(dip);
15317c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
15327c478bd9Sstevel@tonic-gate }
15337c478bd9Sstevel@tonic-gate 
15347c478bd9Sstevel@tonic-gate /*
15357c478bd9Sstevel@tonic-gate  * NDI wrappers for ref counting, node allocation, and transitions
15367c478bd9Sstevel@tonic-gate  */
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate /*
15397c478bd9Sstevel@tonic-gate  * Hold/release the devinfo node itself.
15407c478bd9Sstevel@tonic-gate  * Caller is assumed to prevent the devi from detaching during this call
15417c478bd9Sstevel@tonic-gate  */
15427c478bd9Sstevel@tonic-gate void
15437c478bd9Sstevel@tonic-gate ndi_hold_devi(dev_info_t *dip)
15447c478bd9Sstevel@tonic-gate {
15457c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
15467c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_ref >= 0);
15477c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_ref++;
15487c478bd9Sstevel@tonic-gate 	membar_enter();			/* make sure stores are flushed */
15497c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
15507c478bd9Sstevel@tonic-gate }
15517c478bd9Sstevel@tonic-gate 
15527c478bd9Sstevel@tonic-gate void
15537c478bd9Sstevel@tonic-gate ndi_rele_devi(dev_info_t *dip)
15547c478bd9Sstevel@tonic-gate {
15557c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_ref > 0);
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
15587c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_ref--;
15597c478bd9Sstevel@tonic-gate 	membar_enter();			/* make sure stores are flushed */
15607c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
15617c478bd9Sstevel@tonic-gate }
15627c478bd9Sstevel@tonic-gate 
15637c478bd9Sstevel@tonic-gate int
15647c478bd9Sstevel@tonic-gate e_ddi_devi_holdcnt(dev_info_t *dip)
15657c478bd9Sstevel@tonic-gate {
15667c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_ref);
15677c478bd9Sstevel@tonic-gate }
15687c478bd9Sstevel@tonic-gate 
15697c478bd9Sstevel@tonic-gate /*
15707c478bd9Sstevel@tonic-gate  * Hold/release the driver the devinfo node is bound to.
15717c478bd9Sstevel@tonic-gate  */
15727c478bd9Sstevel@tonic-gate struct dev_ops *
15737c478bd9Sstevel@tonic-gate ndi_hold_driver(dev_info_t *dip)
15747c478bd9Sstevel@tonic-gate {
15757c478bd9Sstevel@tonic-gate 	if (i_ddi_node_state(dip) < DS_BOUND)
15767c478bd9Sstevel@tonic-gate 		return (NULL);
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_major != -1);
15797c478bd9Sstevel@tonic-gate 	return (mod_hold_dev_by_major(DEVI(dip)->devi_major));
15807c478bd9Sstevel@tonic-gate }
15817c478bd9Sstevel@tonic-gate 
15827c478bd9Sstevel@tonic-gate void
15837c478bd9Sstevel@tonic-gate ndi_rele_driver(dev_info_t *dip)
15847c478bd9Sstevel@tonic-gate {
15857c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) >= DS_BOUND);
15867c478bd9Sstevel@tonic-gate 	mod_rele_dev_by_major(DEVI(dip)->devi_major);
15877c478bd9Sstevel@tonic-gate }
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate /*
15907c478bd9Sstevel@tonic-gate  * Single thread entry into devinfo node for modifying its children.
15917c478bd9Sstevel@tonic-gate  * To verify in ASSERTS use DEVI_BUSY_OWNED macro.
15927c478bd9Sstevel@tonic-gate  */
15937c478bd9Sstevel@tonic-gate void
15947c478bd9Sstevel@tonic-gate ndi_devi_enter(dev_info_t *dip, int *circular)
15957c478bd9Sstevel@tonic-gate {
15967c478bd9Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
15977c478bd9Sstevel@tonic-gate 	ASSERT(dip != NULL);
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate 	mutex_enter(&devi->devi_lock);
16007c478bd9Sstevel@tonic-gate 	if (devi->devi_busy_thread == curthread) {
16017c478bd9Sstevel@tonic-gate 		devi->devi_circular++;
16027c478bd9Sstevel@tonic-gate 	} else {
16037c478bd9Sstevel@tonic-gate 		while (DEVI_BUSY_CHANGING(devi) && !panicstr)
16047c478bd9Sstevel@tonic-gate 			cv_wait(&(devi->devi_cv), &(devi->devi_lock));
16057c478bd9Sstevel@tonic-gate 		if (panicstr) {
16067c478bd9Sstevel@tonic-gate 			mutex_exit(&devi->devi_lock);
16077c478bd9Sstevel@tonic-gate 			return;
16087c478bd9Sstevel@tonic-gate 		}
16097c478bd9Sstevel@tonic-gate 		devi->devi_flags |= DEVI_BUSY;
16107c478bd9Sstevel@tonic-gate 		devi->devi_busy_thread = curthread;
16117c478bd9Sstevel@tonic-gate 	}
16127c478bd9Sstevel@tonic-gate 	*circular = devi->devi_circular;
16137c478bd9Sstevel@tonic-gate 	mutex_exit(&devi->devi_lock);
16147c478bd9Sstevel@tonic-gate }
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate /*
16177c478bd9Sstevel@tonic-gate  * Release ndi_devi_enter or successful ndi_devi_tryenter.
16187c478bd9Sstevel@tonic-gate  */
16197c478bd9Sstevel@tonic-gate void
16207c478bd9Sstevel@tonic-gate ndi_devi_exit(dev_info_t *dip, int circular)
16217c478bd9Sstevel@tonic-gate {
16227c478bd9Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
16237c478bd9Sstevel@tonic-gate 	ASSERT(dip != NULL);
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate 	if (panicstr)
16267c478bd9Sstevel@tonic-gate 		return;
16277c478bd9Sstevel@tonic-gate 
16287c478bd9Sstevel@tonic-gate 	mutex_enter(&(devi->devi_lock));
16297c478bd9Sstevel@tonic-gate 	if (circular != 0) {
16307c478bd9Sstevel@tonic-gate 		devi->devi_circular--;
16317c478bd9Sstevel@tonic-gate 	} else {
16327c478bd9Sstevel@tonic-gate 		devi->devi_flags &= ~DEVI_BUSY;
16337c478bd9Sstevel@tonic-gate 		ASSERT(devi->devi_busy_thread == curthread);
16347c478bd9Sstevel@tonic-gate 		devi->devi_busy_thread = NULL;
16357c478bd9Sstevel@tonic-gate 		cv_broadcast(&(devi->devi_cv));
16367c478bd9Sstevel@tonic-gate 	}
16377c478bd9Sstevel@tonic-gate 	mutex_exit(&(devi->devi_lock));
16387c478bd9Sstevel@tonic-gate }
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate /*
16417c478bd9Sstevel@tonic-gate  * Attempt to single thread entry into devinfo node for modifying its children.
16427c478bd9Sstevel@tonic-gate  */
16437c478bd9Sstevel@tonic-gate int
16447c478bd9Sstevel@tonic-gate ndi_devi_tryenter(dev_info_t *dip, int *circular)
16457c478bd9Sstevel@tonic-gate {
16467c478bd9Sstevel@tonic-gate 	int rval = 1;		   /* assume we enter */
16477c478bd9Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
16487c478bd9Sstevel@tonic-gate 	ASSERT(dip != NULL);
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 	mutex_enter(&devi->devi_lock);
16517c478bd9Sstevel@tonic-gate 	if (devi->devi_busy_thread == (void *)curthread) {
16527c478bd9Sstevel@tonic-gate 		devi->devi_circular++;
16537c478bd9Sstevel@tonic-gate 	} else {
16547c478bd9Sstevel@tonic-gate 		if (!DEVI_BUSY_CHANGING(devi)) {
16557c478bd9Sstevel@tonic-gate 			devi->devi_flags |= DEVI_BUSY;
16567c478bd9Sstevel@tonic-gate 			devi->devi_busy_thread = (void *)curthread;
16577c478bd9Sstevel@tonic-gate 		} else {
16587c478bd9Sstevel@tonic-gate 			rval = 0;	/* devi is busy */
16597c478bd9Sstevel@tonic-gate 		}
16607c478bd9Sstevel@tonic-gate 	}
16617c478bd9Sstevel@tonic-gate 	*circular = devi->devi_circular;
16627c478bd9Sstevel@tonic-gate 	mutex_exit(&devi->devi_lock);
16637c478bd9Sstevel@tonic-gate 	return (rval);
16647c478bd9Sstevel@tonic-gate }
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate /*
16677c478bd9Sstevel@tonic-gate  * Allocate and initialize a new dev_info structure.
16687c478bd9Sstevel@tonic-gate  *
16697c478bd9Sstevel@tonic-gate  * This routine may be called at interrupt time by a nexus in
16707c478bd9Sstevel@tonic-gate  * response to a hotplug event, therefore memory allocations are
16717c478bd9Sstevel@tonic-gate  * not allowed to sleep.
16727c478bd9Sstevel@tonic-gate  */
16737c478bd9Sstevel@tonic-gate int
1674fa9e4066Sahrens ndi_devi_alloc(dev_info_t *parent, char *node_name, pnode_t nodeid,
16757c478bd9Sstevel@tonic-gate     dev_info_t **ret_dip)
16767c478bd9Sstevel@tonic-gate {
16777c478bd9Sstevel@tonic-gate 	ASSERT(node_name != NULL);
16787c478bd9Sstevel@tonic-gate 	ASSERT(ret_dip != NULL);
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate 	*ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL,
16817c478bd9Sstevel@tonic-gate 	    KM_NOSLEEP);
16827c478bd9Sstevel@tonic-gate 	if (*ret_dip == NULL) {
16837c478bd9Sstevel@tonic-gate 		return (NDI_NOMEM);
16847c478bd9Sstevel@tonic-gate 	}
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
16877c478bd9Sstevel@tonic-gate }
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate /*
16907c478bd9Sstevel@tonic-gate  * Allocate and initialize a new dev_info structure
16917c478bd9Sstevel@tonic-gate  * This routine may sleep and should not be called at interrupt time
16927c478bd9Sstevel@tonic-gate  */
16937c478bd9Sstevel@tonic-gate void
1694fa9e4066Sahrens ndi_devi_alloc_sleep(dev_info_t *parent, char *node_name, pnode_t nodeid,
16957c478bd9Sstevel@tonic-gate     dev_info_t **ret_dip)
16967c478bd9Sstevel@tonic-gate {
16977c478bd9Sstevel@tonic-gate 	ASSERT(node_name != NULL);
16987c478bd9Sstevel@tonic-gate 	ASSERT(ret_dip != NULL);
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate 	*ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL,
17017c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
17027c478bd9Sstevel@tonic-gate 	ASSERT(*ret_dip);
17037c478bd9Sstevel@tonic-gate }
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate /*
17067c478bd9Sstevel@tonic-gate  * Remove an initialized (but not yet attached) dev_info
17077c478bd9Sstevel@tonic-gate  * node from it's parent.
17087c478bd9Sstevel@tonic-gate  */
17097c478bd9Sstevel@tonic-gate int
17107c478bd9Sstevel@tonic-gate ndi_devi_free(dev_info_t *dip)
17117c478bd9Sstevel@tonic-gate {
17127c478bd9Sstevel@tonic-gate 	ASSERT(dip != NULL);
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 	if (i_ddi_node_state(dip) >= DS_INITIALIZED)
17157c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_free: %s%d (%p)\n",
17187c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 	(void) ddi_remove_child(dip, 0);
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
17237c478bd9Sstevel@tonic-gate }
17247c478bd9Sstevel@tonic-gate 
17257c478bd9Sstevel@tonic-gate /*
17267c478bd9Sstevel@tonic-gate  * ndi_devi_bind_driver() binds a driver to a given device. If it fails
17277c478bd9Sstevel@tonic-gate  * to bind the driver, it returns an appropriate error back. Some drivers
17287c478bd9Sstevel@tonic-gate  * may want to know if the actually failed to bind.
17297c478bd9Sstevel@tonic-gate  */
17307c478bd9Sstevel@tonic-gate int
17317c478bd9Sstevel@tonic-gate ndi_devi_bind_driver(dev_info_t *dip, uint_t flags)
17327c478bd9Sstevel@tonic-gate {
17337c478bd9Sstevel@tonic-gate 	int ret = NDI_FAILURE;
17347c478bd9Sstevel@tonic-gate 	int circ;
17357c478bd9Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
17367c478bd9Sstevel@tonic-gate 	ASSERT(pdip);
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
17397c478bd9Sstevel@tonic-gate 	    "ndi_devi_bind_driver: %s%d (%p) flags: %x\n",
17407c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
17437c478bd9Sstevel@tonic-gate 	if (i_ndi_config_node(dip, DS_BOUND, flags) == DDI_SUCCESS)
17447c478bd9Sstevel@tonic-gate 		ret = NDI_SUCCESS;
17457c478bd9Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
17467c478bd9Sstevel@tonic-gate 
17477c478bd9Sstevel@tonic-gate 	return (ret);
17487c478bd9Sstevel@tonic-gate }
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate /*
17517c478bd9Sstevel@tonic-gate  * ndi_devi_unbind_driver: unbind the dip
17527c478bd9Sstevel@tonic-gate  */
17537c478bd9Sstevel@tonic-gate static int
17547c478bd9Sstevel@tonic-gate ndi_devi_unbind_driver(dev_info_t *dip)
17557c478bd9Sstevel@tonic-gate {
17567c478bd9Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
17577c478bd9Sstevel@tonic-gate 
17587c478bd9Sstevel@tonic-gate 	return (i_ndi_unconfig_node(dip, DS_LINKED, 0));
17597c478bd9Sstevel@tonic-gate }
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate /*
17627c478bd9Sstevel@tonic-gate  * Misc. help routines called by framework only
17637c478bd9Sstevel@tonic-gate  */
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate /*
17667c478bd9Sstevel@tonic-gate  * Get the state of node
17677c478bd9Sstevel@tonic-gate  */
17687c478bd9Sstevel@tonic-gate ddi_node_state_t
17697c478bd9Sstevel@tonic-gate i_ddi_node_state(dev_info_t *dip)
17707c478bd9Sstevel@tonic-gate {
17717c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_node_state);
17727c478bd9Sstevel@tonic-gate }
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate /*
17757c478bd9Sstevel@tonic-gate  * Set the state of node
17767c478bd9Sstevel@tonic-gate  */
17777c478bd9Sstevel@tonic-gate void
17787c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dev_info_t *dip, ddi_node_state_t state)
17797c478bd9Sstevel@tonic-gate {
17807c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_node_state = state;
17817c478bd9Sstevel@tonic-gate 	membar_enter();			/* make sure stores are flushed */
17827c478bd9Sstevel@tonic-gate }
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate /*
17857c478bd9Sstevel@tonic-gate  * Common function for finding a node in a sibling list given name and addr.
17867c478bd9Sstevel@tonic-gate  *
17877c478bd9Sstevel@tonic-gate  * By default, name is matched with devi_node_name. The following
17887c478bd9Sstevel@tonic-gate  * alternative match strategies are supported:
17897c478bd9Sstevel@tonic-gate  *
17907c478bd9Sstevel@tonic-gate  *	FIND_NAME_BY_DRIVER: A match on driver name bound to node is conducted.
17917c478bd9Sstevel@tonic-gate  *		This support is used for support of OBP generic names and
17927c478bd9Sstevel@tonic-gate  *		for the conversion from driver names to generic names.  When
17937c478bd9Sstevel@tonic-gate  *		more consistency in the generic name environment is achieved
17947c478bd9Sstevel@tonic-gate  *		(and not needed for upgrade) this support can be removed.
17957c478bd9Sstevel@tonic-gate  *
17967c478bd9Sstevel@tonic-gate  * If a child is not named (dev_addr == NULL), there are three
17977c478bd9Sstevel@tonic-gate  * possible actions:
17987c478bd9Sstevel@tonic-gate  *
17997c478bd9Sstevel@tonic-gate  *	(1) skip it
18007c478bd9Sstevel@tonic-gate  *	(2) FIND_ADDR_BY_INIT: bring child to DS_INITIALIZED state
18017c478bd9Sstevel@tonic-gate  *	(3) FIND_ADDR_BY_CALLBACK: use a caller-supplied callback function
18027c478bd9Sstevel@tonic-gate  */
18037c478bd9Sstevel@tonic-gate #define	FIND_NAME_BY_DRIVER	0x01
18047c478bd9Sstevel@tonic-gate #define	FIND_ADDR_BY_INIT	0x10
18057c478bd9Sstevel@tonic-gate #define	FIND_ADDR_BY_CALLBACK	0x20
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate static dev_info_t *
18087c478bd9Sstevel@tonic-gate find_sibling(dev_info_t *head, char *cname, char *caddr, uint_t flag,
18097c478bd9Sstevel@tonic-gate     int (*callback)(dev_info_t *, char *, int))
18107c478bd9Sstevel@tonic-gate {
18117c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
18127c478bd9Sstevel@tonic-gate 	char		*addr, *buf;
18137c478bd9Sstevel@tonic-gate 	major_t		major;
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate 	/* only one way to name a node */
18167c478bd9Sstevel@tonic-gate 	ASSERT(((flag & FIND_ADDR_BY_INIT) == 0) ||
18177c478bd9Sstevel@tonic-gate 	    ((flag & FIND_ADDR_BY_CALLBACK) == 0));
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate 	if (flag & FIND_NAME_BY_DRIVER) {
18207c478bd9Sstevel@tonic-gate 		major = ddi_name_to_major(cname);
18217c478bd9Sstevel@tonic-gate 		if (major == (major_t)-1)
18227c478bd9Sstevel@tonic-gate 			return (NULL);
18237c478bd9Sstevel@tonic-gate 	}
18247c478bd9Sstevel@tonic-gate 
18257c478bd9Sstevel@tonic-gate 	/* preallocate buffer of naming node by callback */
18267c478bd9Sstevel@tonic-gate 	if (flag & FIND_ADDR_BY_CALLBACK)
18277c478bd9Sstevel@tonic-gate 		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate 	/*
18307c478bd9Sstevel@tonic-gate 	 * Walk the child list to find a match
18317c478bd9Sstevel@tonic-gate 	 */
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate 	for (dip = head; dip; dip = ddi_get_next_sibling(dip)) {
18347c478bd9Sstevel@tonic-gate 		if (flag & FIND_NAME_BY_DRIVER) {
18357c478bd9Sstevel@tonic-gate 			/* match driver major */
18367c478bd9Sstevel@tonic-gate 			if (DEVI(dip)->devi_major != major)
18377c478bd9Sstevel@tonic-gate 				continue;
18387c478bd9Sstevel@tonic-gate 		} else {
18397c478bd9Sstevel@tonic-gate 			/* match node name */
18407c478bd9Sstevel@tonic-gate 			if (strcmp(cname, DEVI(dip)->devi_node_name) != 0)
18417c478bd9Sstevel@tonic-gate 				continue;
18427c478bd9Sstevel@tonic-gate 		}
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate 		if ((addr = DEVI(dip)->devi_addr) == NULL) {
18457c478bd9Sstevel@tonic-gate 			/* name the child based on the flag */
18467c478bd9Sstevel@tonic-gate 			if (flag & FIND_ADDR_BY_INIT) {
18477c478bd9Sstevel@tonic-gate 				if (ddi_initchild(ddi_get_parent(dip), dip)
18487c478bd9Sstevel@tonic-gate 				    != DDI_SUCCESS)
18497c478bd9Sstevel@tonic-gate 					continue;
18507c478bd9Sstevel@tonic-gate 				addr = DEVI(dip)->devi_addr;
18517c478bd9Sstevel@tonic-gate 			} else if (flag & FIND_ADDR_BY_CALLBACK) {
18527c478bd9Sstevel@tonic-gate 				if ((callback == NULL) || (callback(
18537c478bd9Sstevel@tonic-gate 				    dip, buf, MAXNAMELEN) != DDI_SUCCESS))
18547c478bd9Sstevel@tonic-gate 					continue;
18557c478bd9Sstevel@tonic-gate 				addr = buf;
18567c478bd9Sstevel@tonic-gate 			} else {
18577c478bd9Sstevel@tonic-gate 				continue;	/* skip */
18587c478bd9Sstevel@tonic-gate 			}
18597c478bd9Sstevel@tonic-gate 		}
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate 		/* match addr */
18627c478bd9Sstevel@tonic-gate 		ASSERT(addr != NULL);
18637c478bd9Sstevel@tonic-gate 		if (strcmp(caddr, addr) == 0)
18647c478bd9Sstevel@tonic-gate 			break;	/* node found */
18657c478bd9Sstevel@tonic-gate 
18667c478bd9Sstevel@tonic-gate 	}
18677c478bd9Sstevel@tonic-gate 	if (flag & FIND_ADDR_BY_CALLBACK)
18687c478bd9Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
18697c478bd9Sstevel@tonic-gate 	return (dip);
18707c478bd9Sstevel@tonic-gate }
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate /*
18737c478bd9Sstevel@tonic-gate  * Find child of pdip with name: cname@caddr
18747c478bd9Sstevel@tonic-gate  * Called by init_node() to look for duplicate nodes
18757c478bd9Sstevel@tonic-gate  */
18767c478bd9Sstevel@tonic-gate static dev_info_t *
18777c478bd9Sstevel@tonic-gate find_duplicate_child(dev_info_t *pdip, dev_info_t *dip)
18787c478bd9Sstevel@tonic-gate {
18797c478bd9Sstevel@tonic-gate 	dev_info_t *dup;
18807c478bd9Sstevel@tonic-gate 	char *cname = DEVI(dip)->devi_node_name;
18817c478bd9Sstevel@tonic-gate 	char *caddr = DEVI(dip)->devi_addr;
18827c478bd9Sstevel@tonic-gate 
18837c478bd9Sstevel@tonic-gate 	/* search nodes before dip */
18847c478bd9Sstevel@tonic-gate 	dup = find_sibling(ddi_get_child(pdip), cname, caddr, 0, NULL);
18857c478bd9Sstevel@tonic-gate 	if (dup != dip)
18867c478bd9Sstevel@tonic-gate 		return (dup);
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 	/*
18897c478bd9Sstevel@tonic-gate 	 * search nodes after dip; normally this is not needed,
18907c478bd9Sstevel@tonic-gate 	 */
18917c478bd9Sstevel@tonic-gate 	return (find_sibling(ddi_get_next_sibling(dip), cname, caddr,
18927c478bd9Sstevel@tonic-gate 	    0, NULL));
18937c478bd9Sstevel@tonic-gate }
18947c478bd9Sstevel@tonic-gate 
18957c478bd9Sstevel@tonic-gate /*
18967c478bd9Sstevel@tonic-gate  * Find a child of a given name and address, using a callback to name
18977c478bd9Sstevel@tonic-gate  * unnamed children. cname is the binding name.
18987c478bd9Sstevel@tonic-gate  */
18997c478bd9Sstevel@tonic-gate static dev_info_t *
19007c478bd9Sstevel@tonic-gate find_child_by_callback(dev_info_t *pdip, char *cname, char *caddr,
19017c478bd9Sstevel@tonic-gate     int (*name_node)(dev_info_t *, char *, int))
19027c478bd9Sstevel@tonic-gate {
19037c478bd9Sstevel@tonic-gate 	return (find_sibling(ddi_get_child(pdip), cname, caddr,
19047c478bd9Sstevel@tonic-gate 	    FIND_NAME_BY_DRIVER|FIND_ADDR_BY_CALLBACK, name_node));
19057c478bd9Sstevel@tonic-gate }
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate /*
19087c478bd9Sstevel@tonic-gate  * Find a child of a given name and address, invoking initchild to name
19097c478bd9Sstevel@tonic-gate  * unnamed children. cname is the node name.
19107c478bd9Sstevel@tonic-gate  */
19117c478bd9Sstevel@tonic-gate static dev_info_t *
19127c478bd9Sstevel@tonic-gate find_child_by_name(dev_info_t *pdip, char *cname, char *caddr)
19137c478bd9Sstevel@tonic-gate {
19147c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
19157c478bd9Sstevel@tonic-gate 
19167c478bd9Sstevel@tonic-gate 	/* attempt search without changing state of preceeding siblings */
19177c478bd9Sstevel@tonic-gate 	dip = find_sibling(ddi_get_child(pdip), cname, caddr, 0, NULL);
19187c478bd9Sstevel@tonic-gate 	if (dip)
19197c478bd9Sstevel@tonic-gate 		return (dip);
19207c478bd9Sstevel@tonic-gate 
19217c478bd9Sstevel@tonic-gate 	return (find_sibling(ddi_get_child(pdip), cname, caddr,
19227c478bd9Sstevel@tonic-gate 	    FIND_ADDR_BY_INIT, NULL));
19237c478bd9Sstevel@tonic-gate }
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate /*
19267c478bd9Sstevel@tonic-gate  * Find a child of a given name and address, invoking initchild to name
19277c478bd9Sstevel@tonic-gate  * unnamed children. cname is the node name.
19287c478bd9Sstevel@tonic-gate  */
19297c478bd9Sstevel@tonic-gate static dev_info_t *
19307c478bd9Sstevel@tonic-gate find_child_by_driver(dev_info_t *pdip, char *cname, char *caddr)
19317c478bd9Sstevel@tonic-gate {
19327c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate 	/* attempt search without changing state of preceeding siblings */
19357c478bd9Sstevel@tonic-gate 	dip = find_sibling(ddi_get_child(pdip), cname, caddr,
19367c478bd9Sstevel@tonic-gate 	    FIND_NAME_BY_DRIVER, NULL);
19377c478bd9Sstevel@tonic-gate 	if (dip)
19387c478bd9Sstevel@tonic-gate 		return (dip);
19397c478bd9Sstevel@tonic-gate 
19407c478bd9Sstevel@tonic-gate 	return (find_sibling(ddi_get_child(pdip), cname, caddr,
19417c478bd9Sstevel@tonic-gate 	    FIND_NAME_BY_DRIVER|FIND_ADDR_BY_INIT, NULL));
19427c478bd9Sstevel@tonic-gate }
19437c478bd9Sstevel@tonic-gate 
19447c478bd9Sstevel@tonic-gate /*
19457c478bd9Sstevel@tonic-gate  * Deleting a property list. Take care, since some property structures
19467c478bd9Sstevel@tonic-gate  * may not be fully built.
19477c478bd9Sstevel@tonic-gate  */
19487c478bd9Sstevel@tonic-gate void
19497c478bd9Sstevel@tonic-gate i_ddi_prop_list_delete(ddi_prop_t *prop)
19507c478bd9Sstevel@tonic-gate {
19517c478bd9Sstevel@tonic-gate 	while (prop) {
19527c478bd9Sstevel@tonic-gate 		ddi_prop_t *next = prop->prop_next;
19537c478bd9Sstevel@tonic-gate 		if (prop->prop_name)
19547c478bd9Sstevel@tonic-gate 			kmem_free(prop->prop_name, strlen(prop->prop_name) + 1);
19557c478bd9Sstevel@tonic-gate 		if ((prop->prop_len != 0) && prop->prop_val)
19567c478bd9Sstevel@tonic-gate 			kmem_free(prop->prop_val, prop->prop_len);
19577c478bd9Sstevel@tonic-gate 		kmem_free(prop, sizeof (struct ddi_prop));
19587c478bd9Sstevel@tonic-gate 		prop = next;
19597c478bd9Sstevel@tonic-gate 	}
19607c478bd9Sstevel@tonic-gate }
19617c478bd9Sstevel@tonic-gate 
19627c478bd9Sstevel@tonic-gate /*
19637c478bd9Sstevel@tonic-gate  * Duplicate property list
19647c478bd9Sstevel@tonic-gate  */
19657c478bd9Sstevel@tonic-gate ddi_prop_t *
19667c478bd9Sstevel@tonic-gate i_ddi_prop_list_dup(ddi_prop_t *prop, uint_t flag)
19677c478bd9Sstevel@tonic-gate {
19687c478bd9Sstevel@tonic-gate 	ddi_prop_t *result, *prev, *copy;
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate 	if (prop == NULL)
19717c478bd9Sstevel@tonic-gate 		return (NULL);
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate 	result = prev = NULL;
19747c478bd9Sstevel@tonic-gate 	for (; prop != NULL; prop = prop->prop_next) {
19757c478bd9Sstevel@tonic-gate 		ASSERT(prop->prop_name != NULL);
19767c478bd9Sstevel@tonic-gate 		copy = kmem_zalloc(sizeof (struct ddi_prop), flag);
19777c478bd9Sstevel@tonic-gate 		if (copy == NULL)
19787c478bd9Sstevel@tonic-gate 			goto fail;
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate 		copy->prop_dev = prop->prop_dev;
19817c478bd9Sstevel@tonic-gate 		copy->prop_flags = prop->prop_flags;
19827c478bd9Sstevel@tonic-gate 		copy->prop_name = i_ddi_strdup(prop->prop_name, flag);
19837c478bd9Sstevel@tonic-gate 		if (copy->prop_name == NULL)
19847c478bd9Sstevel@tonic-gate 			goto fail;
19857c478bd9Sstevel@tonic-gate 
19867c478bd9Sstevel@tonic-gate 		if ((copy->prop_len = prop->prop_len) != 0) {
19877c478bd9Sstevel@tonic-gate 			copy->prop_val = kmem_zalloc(prop->prop_len, flag);
19887c478bd9Sstevel@tonic-gate 			if (copy->prop_val == NULL)
19897c478bd9Sstevel@tonic-gate 				goto fail;
19907c478bd9Sstevel@tonic-gate 
19917c478bd9Sstevel@tonic-gate 			bcopy(prop->prop_val, copy->prop_val, prop->prop_len);
19927c478bd9Sstevel@tonic-gate 		}
19937c478bd9Sstevel@tonic-gate 
19947c478bd9Sstevel@tonic-gate 		if (prev == NULL)
19957c478bd9Sstevel@tonic-gate 			result = prev = copy;
19967c478bd9Sstevel@tonic-gate 		else
19977c478bd9Sstevel@tonic-gate 			prev->prop_next = copy;
19987c478bd9Sstevel@tonic-gate 		prev = copy;
19997c478bd9Sstevel@tonic-gate 	}
20007c478bd9Sstevel@tonic-gate 	return (result);
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate fail:
20037c478bd9Sstevel@tonic-gate 	i_ddi_prop_list_delete(result);
20047c478bd9Sstevel@tonic-gate 	return (NULL);
20057c478bd9Sstevel@tonic-gate }
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate /*
20087c478bd9Sstevel@tonic-gate  * Create a reference property list, currently used only for
20097c478bd9Sstevel@tonic-gate  * driver global properties. Created with ref count of 1.
20107c478bd9Sstevel@tonic-gate  */
20117c478bd9Sstevel@tonic-gate ddi_prop_list_t *
20127c478bd9Sstevel@tonic-gate i_ddi_prop_list_create(ddi_prop_t *props)
20137c478bd9Sstevel@tonic-gate {
20147c478bd9Sstevel@tonic-gate 	ddi_prop_list_t *list = kmem_alloc(sizeof (*list), KM_SLEEP);
20157c478bd9Sstevel@tonic-gate 	list->prop_list = props;
20167c478bd9Sstevel@tonic-gate 	list->prop_ref = 1;
20177c478bd9Sstevel@tonic-gate 	return (list);
20187c478bd9Sstevel@tonic-gate }
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate /*
20217c478bd9Sstevel@tonic-gate  * Increment/decrement reference count. The reference is
20227c478bd9Sstevel@tonic-gate  * protected by dn_lock. The only interfaces modifying
20237c478bd9Sstevel@tonic-gate  * dn_global_prop_ptr is in impl_make[free]_parlist().
20247c478bd9Sstevel@tonic-gate  */
20257c478bd9Sstevel@tonic-gate void
20267c478bd9Sstevel@tonic-gate i_ddi_prop_list_hold(ddi_prop_list_t *prop_list, struct devnames *dnp)
20277c478bd9Sstevel@tonic-gate {
20287c478bd9Sstevel@tonic-gate 	ASSERT(prop_list->prop_ref >= 0);
20297c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&dnp->dn_lock));
20307c478bd9Sstevel@tonic-gate 	prop_list->prop_ref++;
20317c478bd9Sstevel@tonic-gate }
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate void
20347c478bd9Sstevel@tonic-gate i_ddi_prop_list_rele(ddi_prop_list_t *prop_list, struct devnames *dnp)
20357c478bd9Sstevel@tonic-gate {
20367c478bd9Sstevel@tonic-gate 	ASSERT(prop_list->prop_ref > 0);
20377c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&dnp->dn_lock));
20387c478bd9Sstevel@tonic-gate 	prop_list->prop_ref--;
20397c478bd9Sstevel@tonic-gate 
20407c478bd9Sstevel@tonic-gate 	if (prop_list->prop_ref == 0) {
20417c478bd9Sstevel@tonic-gate 		i_ddi_prop_list_delete(prop_list->prop_list);
20427c478bd9Sstevel@tonic-gate 		kmem_free(prop_list, sizeof (*prop_list));
20437c478bd9Sstevel@tonic-gate 	}
20447c478bd9Sstevel@tonic-gate }
20457c478bd9Sstevel@tonic-gate 
20467c478bd9Sstevel@tonic-gate /*
20477c478bd9Sstevel@tonic-gate  * Free table of classes by drivers
20487c478bd9Sstevel@tonic-gate  */
20497c478bd9Sstevel@tonic-gate void
20507c478bd9Sstevel@tonic-gate i_ddi_free_exported_classes(char **classes, int n)
20517c478bd9Sstevel@tonic-gate {
20527c478bd9Sstevel@tonic-gate 	if ((n == 0) || (classes == NULL))
20537c478bd9Sstevel@tonic-gate 		return;
20547c478bd9Sstevel@tonic-gate 
20557c478bd9Sstevel@tonic-gate 	kmem_free(classes, n * sizeof (char *));
20567c478bd9Sstevel@tonic-gate }
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate /*
20597c478bd9Sstevel@tonic-gate  * Get all classes exported by dip
20607c478bd9Sstevel@tonic-gate  */
20617c478bd9Sstevel@tonic-gate int
20627c478bd9Sstevel@tonic-gate i_ddi_get_exported_classes(dev_info_t *dip, char ***classes)
20637c478bd9Sstevel@tonic-gate {
20647c478bd9Sstevel@tonic-gate 	extern void lock_hw_class_list();
20657c478bd9Sstevel@tonic-gate 	extern void unlock_hw_class_list();
20667c478bd9Sstevel@tonic-gate 	extern int get_class(const char *, char **);
20677c478bd9Sstevel@tonic-gate 
20687c478bd9Sstevel@tonic-gate 	static char *rootclass = "root";
20697c478bd9Sstevel@tonic-gate 	int n = 0, nclass = 0;
20707c478bd9Sstevel@tonic-gate 	char **buf;
20717c478bd9Sstevel@tonic-gate 
20727c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) >= DS_BOUND);
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate 	if (dip == ddi_root_node())	/* rootnode exports class "root" */
20757c478bd9Sstevel@tonic-gate 		nclass = 1;
20767c478bd9Sstevel@tonic-gate 	lock_hw_class_list();
20777c478bd9Sstevel@tonic-gate 	nclass += get_class(ddi_driver_name(dip), NULL);
20787c478bd9Sstevel@tonic-gate 	if (nclass == 0) {
20797c478bd9Sstevel@tonic-gate 		unlock_hw_class_list();
20807c478bd9Sstevel@tonic-gate 		return (0);		/* no class exported */
20817c478bd9Sstevel@tonic-gate 	}
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate 	*classes = buf = kmem_alloc(nclass * sizeof (char *), KM_SLEEP);
20847c478bd9Sstevel@tonic-gate 	if (dip == ddi_root_node()) {
20857c478bd9Sstevel@tonic-gate 		*buf++ = rootclass;
20867c478bd9Sstevel@tonic-gate 		n = 1;
20877c478bd9Sstevel@tonic-gate 	}
20887c478bd9Sstevel@tonic-gate 	n += get_class(ddi_driver_name(dip), buf);
20897c478bd9Sstevel@tonic-gate 	unlock_hw_class_list();
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate 	ASSERT(n == nclass);    /* make sure buf wasn't overrun */
20927c478bd9Sstevel@tonic-gate 	return (nclass);
20937c478bd9Sstevel@tonic-gate }
20947c478bd9Sstevel@tonic-gate 
20957c478bd9Sstevel@tonic-gate /*
20967c478bd9Sstevel@tonic-gate  * Helper functions, returns NULL if no memory.
20977c478bd9Sstevel@tonic-gate  */
20987c478bd9Sstevel@tonic-gate char *
20997c478bd9Sstevel@tonic-gate i_ddi_strdup(char *str, uint_t flag)
21007c478bd9Sstevel@tonic-gate {
21017c478bd9Sstevel@tonic-gate 	char *copy;
21027c478bd9Sstevel@tonic-gate 
21037c478bd9Sstevel@tonic-gate 	if (str == NULL)
21047c478bd9Sstevel@tonic-gate 		return (NULL);
21057c478bd9Sstevel@tonic-gate 
21067c478bd9Sstevel@tonic-gate 	copy = kmem_alloc(strlen(str) + 1, flag);
21077c478bd9Sstevel@tonic-gate 	if (copy == NULL)
21087c478bd9Sstevel@tonic-gate 		return (NULL);
21097c478bd9Sstevel@tonic-gate 
21107c478bd9Sstevel@tonic-gate 	(void) strcpy(copy, str);
21117c478bd9Sstevel@tonic-gate 	return (copy);
21127c478bd9Sstevel@tonic-gate }
21137c478bd9Sstevel@tonic-gate 
21147c478bd9Sstevel@tonic-gate /*
21157c478bd9Sstevel@tonic-gate  * Load driver.conf file for major. Load all if major == -1.
21167c478bd9Sstevel@tonic-gate  *
21177c478bd9Sstevel@tonic-gate  * This is called
21187c478bd9Sstevel@tonic-gate  * - early in boot after devnames array is initialized
21197c478bd9Sstevel@tonic-gate  * - from vfs code when certain file systems are mounted
21207c478bd9Sstevel@tonic-gate  * - from add_drv when a new driver is added
21217c478bd9Sstevel@tonic-gate  */
21227c478bd9Sstevel@tonic-gate int
21237c478bd9Sstevel@tonic-gate i_ddi_load_drvconf(major_t major)
21247c478bd9Sstevel@tonic-gate {
21257c478bd9Sstevel@tonic-gate 	extern int modrootloaded;
21267c478bd9Sstevel@tonic-gate 
21277c478bd9Sstevel@tonic-gate 	major_t low, high, m;
21287c478bd9Sstevel@tonic-gate 
21297c478bd9Sstevel@tonic-gate 	if (major == (major_t)-1) {
21307c478bd9Sstevel@tonic-gate 		low = 0;
21317c478bd9Sstevel@tonic-gate 		high = devcnt - 1;
21327c478bd9Sstevel@tonic-gate 	} else {
21337c478bd9Sstevel@tonic-gate 		if (major >= devcnt)
21347c478bd9Sstevel@tonic-gate 			return (EINVAL);
21357c478bd9Sstevel@tonic-gate 		low = high = major;
21367c478bd9Sstevel@tonic-gate 	}
21377c478bd9Sstevel@tonic-gate 
21387c478bd9Sstevel@tonic-gate 	for (m = low; m <= high; m++) {
21397c478bd9Sstevel@tonic-gate 		struct devnames *dnp = &devnamesp[m];
21407c478bd9Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
21417c478bd9Sstevel@tonic-gate 		dnp->dn_flags &= ~DN_DRIVER_HELD;
21427c478bd9Sstevel@tonic-gate 		(void) impl_make_parlist(m);
21437c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
21447c478bd9Sstevel@tonic-gate 	}
21457c478bd9Sstevel@tonic-gate 
21467c478bd9Sstevel@tonic-gate 	if (modrootloaded) {
21477c478bd9Sstevel@tonic-gate 		ddi_walk_devs(ddi_root_node(), reset_nexus_flags,
21487c478bd9Sstevel@tonic-gate 		    (void *)(uintptr_t)major);
21497c478bd9Sstevel@tonic-gate 	}
21507c478bd9Sstevel@tonic-gate 
21517c478bd9Sstevel@tonic-gate 	/* build dn_list from old entries in path_to_inst */
21527c478bd9Sstevel@tonic-gate 	e_ddi_unorphan_instance_nos();
21537c478bd9Sstevel@tonic-gate 	return (0);
21547c478bd9Sstevel@tonic-gate }
21557c478bd9Sstevel@tonic-gate 
21567c478bd9Sstevel@tonic-gate /*
21577c478bd9Sstevel@tonic-gate  * Unload a specific driver.conf.
21587c478bd9Sstevel@tonic-gate  * Don't support unload all because it doesn't make any sense
21597c478bd9Sstevel@tonic-gate  */
21607c478bd9Sstevel@tonic-gate int
21617c478bd9Sstevel@tonic-gate i_ddi_unload_drvconf(major_t major)
21627c478bd9Sstevel@tonic-gate {
21637c478bd9Sstevel@tonic-gate 	int error;
21647c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
21657c478bd9Sstevel@tonic-gate 
21667c478bd9Sstevel@tonic-gate 	if (major >= devcnt)
21677c478bd9Sstevel@tonic-gate 		return (EINVAL);
21687c478bd9Sstevel@tonic-gate 
21697c478bd9Sstevel@tonic-gate 	/*
21707c478bd9Sstevel@tonic-gate 	 * Take the per-driver lock while unloading driver.conf
21717c478bd9Sstevel@tonic-gate 	 */
21727c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
21737c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
21747c478bd9Sstevel@tonic-gate 	error = impl_free_parlist(major);
21757c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
21767c478bd9Sstevel@tonic-gate 	return (error);
21777c478bd9Sstevel@tonic-gate }
21787c478bd9Sstevel@tonic-gate 
21797c478bd9Sstevel@tonic-gate /*
21807c478bd9Sstevel@tonic-gate  * Merge a .conf node. This is called by nexus drivers to augment
21817c478bd9Sstevel@tonic-gate  * hw node with properties specified in driver.conf file. This function
21827c478bd9Sstevel@tonic-gate  * takes a callback routine to name nexus children.
21837c478bd9Sstevel@tonic-gate  * The parent node must be held busy.
21847c478bd9Sstevel@tonic-gate  *
21857c478bd9Sstevel@tonic-gate  * It returns DDI_SUCCESS if the node is merged and DDI_FAILURE otherwise.
21867c478bd9Sstevel@tonic-gate  */
21877c478bd9Sstevel@tonic-gate int
21887c478bd9Sstevel@tonic-gate ndi_merge_node(dev_info_t *dip, int (*name_node)(dev_info_t *, char *, int))
21897c478bd9Sstevel@tonic-gate {
21907c478bd9Sstevel@tonic-gate 	dev_info_t *hwdip;
21917c478bd9Sstevel@tonic-gate 
21927c478bd9Sstevel@tonic-gate 	ASSERT(ndi_dev_is_persistent_node(dip) == 0);
21937c478bd9Sstevel@tonic-gate 	ASSERT(ddi_get_name_addr(dip) != NULL);
21947c478bd9Sstevel@tonic-gate 
21957c478bd9Sstevel@tonic-gate 	hwdip = find_child_by_callback(ddi_get_parent(dip),
21967c478bd9Sstevel@tonic-gate 	    ddi_binding_name(dip), ddi_get_name_addr(dip), name_node);
21977c478bd9Sstevel@tonic-gate 
21987c478bd9Sstevel@tonic-gate 	/*
21997c478bd9Sstevel@tonic-gate 	 * Look for the hardware node that is the target of the merge;
22007c478bd9Sstevel@tonic-gate 	 * return failure if not found.
22017c478bd9Sstevel@tonic-gate 	 */
22027c478bd9Sstevel@tonic-gate 	if ((hwdip == NULL) || (hwdip == dip)) {
22037c478bd9Sstevel@tonic-gate 		char *buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
22047c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_WARN, "No HW node to merge conf node %s",
22057c478bd9Sstevel@tonic-gate 		    ddi_deviname(dip, buf)));
22067c478bd9Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
22077c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
22087c478bd9Sstevel@tonic-gate 	}
22097c478bd9Sstevel@tonic-gate 
22107c478bd9Sstevel@tonic-gate 	/*
22117c478bd9Sstevel@tonic-gate 	 * Make sure the hardware node is uninitialized and has no property.
22127c478bd9Sstevel@tonic-gate 	 * This may not be the case if new .conf files are load after some
22137c478bd9Sstevel@tonic-gate 	 * hardware nodes have already been initialized and attached.
22147c478bd9Sstevel@tonic-gate 	 *
22157c478bd9Sstevel@tonic-gate 	 * N.B. We return success here because the node was *intended*
22167c478bd9Sstevel@tonic-gate 	 * 	to be a merge node because there is a hw node with the name.
22177c478bd9Sstevel@tonic-gate 	 */
22187c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(hwdip)->devi_lock);
22197c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(hwdip) == 0) {
22207c478bd9Sstevel@tonic-gate 		char *buf;
22217c478bd9Sstevel@tonic-gate 		mutex_exit(&DEVI(hwdip)->devi_lock);
22227c478bd9Sstevel@tonic-gate 
22237c478bd9Sstevel@tonic-gate 		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
22247c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE, "Duplicate .conf node %s",
22257c478bd9Sstevel@tonic-gate 		    ddi_deviname(dip, buf)));
22267c478bd9Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
22277c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
22287c478bd9Sstevel@tonic-gate 	}
22297c478bd9Sstevel@tonic-gate 
22307c478bd9Sstevel@tonic-gate 	/*
22317c478bd9Sstevel@tonic-gate 	 * If it is possible that the hardware has already been touched
22327c478bd9Sstevel@tonic-gate 	 * then don't merge.
22337c478bd9Sstevel@tonic-gate 	 */
22347c478bd9Sstevel@tonic-gate 	if (i_ddi_node_state(hwdip) >= DS_INITIALIZED ||
22357c478bd9Sstevel@tonic-gate 	    (DEVI(hwdip)->devi_sys_prop_ptr != NULL) ||
22367c478bd9Sstevel@tonic-gate 	    (DEVI(hwdip)->devi_drv_prop_ptr != NULL)) {
22377c478bd9Sstevel@tonic-gate 		char *buf;
22387c478bd9Sstevel@tonic-gate 		mutex_exit(&DEVI(hwdip)->devi_lock);
22397c478bd9Sstevel@tonic-gate 
22407c478bd9Sstevel@tonic-gate 		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
22417c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE,
22427c478bd9Sstevel@tonic-gate 		    "!Cannot merge .conf node %s with hw node %p "
22437c478bd9Sstevel@tonic-gate 		    "-- not in proper state",
22447c478bd9Sstevel@tonic-gate 		    ddi_deviname(dip, buf), (void *)hwdip));
22457c478bd9Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
22467c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
22477c478bd9Sstevel@tonic-gate 	}
22487c478bd9Sstevel@tonic-gate 
22497c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
22507c478bd9Sstevel@tonic-gate 	DEVI(hwdip)->devi_sys_prop_ptr = DEVI(dip)->devi_sys_prop_ptr;
22517c478bd9Sstevel@tonic-gate 	DEVI(hwdip)->devi_drv_prop_ptr = DEVI(dip)->devi_drv_prop_ptr;
22527c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_sys_prop_ptr = NULL;
22537c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_drv_prop_ptr = NULL;
22547c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
22557c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(hwdip)->devi_lock);
22567c478bd9Sstevel@tonic-gate 
22577c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
22587c478bd9Sstevel@tonic-gate }
22597c478bd9Sstevel@tonic-gate 
22607c478bd9Sstevel@tonic-gate /*
22617c478bd9Sstevel@tonic-gate  * Merge a "wildcard" .conf node. This is called by nexus drivers to
22627c478bd9Sstevel@tonic-gate  * augment a set of hw node with properties specified in driver.conf file.
22637c478bd9Sstevel@tonic-gate  * The parent node must be held busy.
22647c478bd9Sstevel@tonic-gate  *
22657c478bd9Sstevel@tonic-gate  * There is no failure mode, since the nexus may or may not have child
22667c478bd9Sstevel@tonic-gate  * node bound the driver specified by the wildcard node.
22677c478bd9Sstevel@tonic-gate  */
22687c478bd9Sstevel@tonic-gate void
22697c478bd9Sstevel@tonic-gate ndi_merge_wildcard_node(dev_info_t *dip)
22707c478bd9Sstevel@tonic-gate {
22717c478bd9Sstevel@tonic-gate 	dev_info_t *hwdip;
22727c478bd9Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
22737c478bd9Sstevel@tonic-gate 	major_t major = ddi_driver_major(dip);
22747c478bd9Sstevel@tonic-gate 
22757c478bd9Sstevel@tonic-gate 	/* never attempt to merge a hw node */
22767c478bd9Sstevel@tonic-gate 	ASSERT(ndi_dev_is_persistent_node(dip) == 0);
22777c478bd9Sstevel@tonic-gate 	/* must be bound to a driver major number */
22787c478bd9Sstevel@tonic-gate 	ASSERT(major != (major_t)-1);
22797c478bd9Sstevel@tonic-gate 
22807c478bd9Sstevel@tonic-gate 	/*
22817c478bd9Sstevel@tonic-gate 	 * Walk the child list to find all nodes bound to major
22827c478bd9Sstevel@tonic-gate 	 * and copy properties.
22837c478bd9Sstevel@tonic-gate 	 */
22847c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
22857c478bd9Sstevel@tonic-gate 	for (hwdip = ddi_get_child(pdip); hwdip;
22867c478bd9Sstevel@tonic-gate 	    hwdip = ddi_get_next_sibling(hwdip)) {
22877c478bd9Sstevel@tonic-gate 		/*
22887c478bd9Sstevel@tonic-gate 		 * Skip nodes not bound to same driver
22897c478bd9Sstevel@tonic-gate 		 */
22907c478bd9Sstevel@tonic-gate 		if (ddi_driver_major(hwdip) != major)
22917c478bd9Sstevel@tonic-gate 			continue;
22927c478bd9Sstevel@tonic-gate 
22937c478bd9Sstevel@tonic-gate 		/*
22947c478bd9Sstevel@tonic-gate 		 * Skip .conf nodes
22957c478bd9Sstevel@tonic-gate 		 */
22967c478bd9Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(hwdip) == 0)
22977c478bd9Sstevel@tonic-gate 			continue;
22987c478bd9Sstevel@tonic-gate 
22997c478bd9Sstevel@tonic-gate 		/*
23007c478bd9Sstevel@tonic-gate 		 * Make sure the node is uninitialized and has no property.
23017c478bd9Sstevel@tonic-gate 		 */
23027c478bd9Sstevel@tonic-gate 		mutex_enter(&DEVI(hwdip)->devi_lock);
23037c478bd9Sstevel@tonic-gate 		if (i_ddi_node_state(hwdip) >= DS_INITIALIZED ||
23047c478bd9Sstevel@tonic-gate 		    (DEVI(hwdip)->devi_sys_prop_ptr != NULL) ||
23057c478bd9Sstevel@tonic-gate 		    (DEVI(hwdip)->devi_drv_prop_ptr != NULL)) {
23067c478bd9Sstevel@tonic-gate 			mutex_exit(&DEVI(hwdip)->devi_lock);
23077c478bd9Sstevel@tonic-gate 			NDI_CONFIG_DEBUG((CE_NOTE, "HW node %p state not "
23087c478bd9Sstevel@tonic-gate 			    "suitable for merging wildcard conf node %s",
23097c478bd9Sstevel@tonic-gate 			    (void *)hwdip, ddi_node_name(dip)));
23107c478bd9Sstevel@tonic-gate 			continue;
23117c478bd9Sstevel@tonic-gate 		}
23127c478bd9Sstevel@tonic-gate 
23137c478bd9Sstevel@tonic-gate 		DEVI(hwdip)->devi_sys_prop_ptr =
23147c478bd9Sstevel@tonic-gate 		    i_ddi_prop_list_dup(DEVI(dip)->devi_sys_prop_ptr, KM_SLEEP);
23157c478bd9Sstevel@tonic-gate 		DEVI(hwdip)->devi_drv_prop_ptr =
23167c478bd9Sstevel@tonic-gate 		    i_ddi_prop_list_dup(DEVI(dip)->devi_drv_prop_ptr, KM_SLEEP);
23177c478bd9Sstevel@tonic-gate 		mutex_exit(&DEVI(hwdip)->devi_lock);
23187c478bd9Sstevel@tonic-gate 	}
23197c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
23207c478bd9Sstevel@tonic-gate }
23217c478bd9Sstevel@tonic-gate 
23227c478bd9Sstevel@tonic-gate /*
23237c478bd9Sstevel@tonic-gate  * Return the major number based on the compatible property. This interface
23247c478bd9Sstevel@tonic-gate  * may be used in situations where we are trying to detect if a better driver
23257c478bd9Sstevel@tonic-gate  * now exists for a device, so it must use the 'compatible' property.  If
23267c478bd9Sstevel@tonic-gate  * a non-NULL formp is specified and the binding was based on compatible then
23277c478bd9Sstevel@tonic-gate  * return the pointer to the form used in *formp.
23287c478bd9Sstevel@tonic-gate  */
23297c478bd9Sstevel@tonic-gate major_t
23307c478bd9Sstevel@tonic-gate ddi_compatible_driver_major(dev_info_t *dip, char **formp)
23317c478bd9Sstevel@tonic-gate {
23327c478bd9Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
23337c478bd9Sstevel@tonic-gate 	void		*compat;
23347c478bd9Sstevel@tonic-gate 	size_t		len;
23357c478bd9Sstevel@tonic-gate 	char		*p = NULL;
23367c478bd9Sstevel@tonic-gate 	major_t		major = (major_t)-1;
23377c478bd9Sstevel@tonic-gate 
23387c478bd9Sstevel@tonic-gate 	if (formp)
23397c478bd9Sstevel@tonic-gate 		*formp = NULL;
23407c478bd9Sstevel@tonic-gate 
23417c478bd9Sstevel@tonic-gate 	/* look up compatible property */
23427c478bd9Sstevel@tonic-gate 	(void) lookup_compatible(dip, KM_SLEEP);
23437c478bd9Sstevel@tonic-gate 	compat = (void *)(devi->devi_compat_names);
23447c478bd9Sstevel@tonic-gate 	len = devi->devi_compat_length;
23457c478bd9Sstevel@tonic-gate 
23467c478bd9Sstevel@tonic-gate 	/* find the highest precedence compatible form with a driver binding */
23477c478bd9Sstevel@tonic-gate 	while ((p = prom_decode_composite_string(compat, len, p)) != NULL) {
23487c478bd9Sstevel@tonic-gate 		major = ddi_name_to_major(p);
23497c478bd9Sstevel@tonic-gate 		if ((major != (major_t)-1) &&
23507c478bd9Sstevel@tonic-gate 		    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED)) {
23517c478bd9Sstevel@tonic-gate 			if (formp)
23527c478bd9Sstevel@tonic-gate 				*formp = p;
23537c478bd9Sstevel@tonic-gate 			return (major);
23547c478bd9Sstevel@tonic-gate 		}
23557c478bd9Sstevel@tonic-gate 	}
23567c478bd9Sstevel@tonic-gate 
23577c478bd9Sstevel@tonic-gate 	/*
23587c478bd9Sstevel@tonic-gate 	 * none of the compatible forms have a driver binding, see if
23597c478bd9Sstevel@tonic-gate 	 * the node name has a driver binding.
23607c478bd9Sstevel@tonic-gate 	 */
23617c478bd9Sstevel@tonic-gate 	major = ddi_name_to_major(ddi_node_name(dip));
23627c478bd9Sstevel@tonic-gate 	if ((major != (major_t)-1) &&
23637c478bd9Sstevel@tonic-gate 	    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED))
23647c478bd9Sstevel@tonic-gate 		return (major);
23657c478bd9Sstevel@tonic-gate 
23667c478bd9Sstevel@tonic-gate 	/* no driver */
23677c478bd9Sstevel@tonic-gate 	return ((major_t)-1);
23687c478bd9Sstevel@tonic-gate }
23697c478bd9Sstevel@tonic-gate 
23707c478bd9Sstevel@tonic-gate /*
23717c478bd9Sstevel@tonic-gate  * Static help functions
23727c478bd9Sstevel@tonic-gate  */
23737c478bd9Sstevel@tonic-gate 
23747c478bd9Sstevel@tonic-gate /*
23757c478bd9Sstevel@tonic-gate  * lookup the "compatible" property and cache it's contents in the
23767c478bd9Sstevel@tonic-gate  * device node.
23777c478bd9Sstevel@tonic-gate  */
23787c478bd9Sstevel@tonic-gate static int
23797c478bd9Sstevel@tonic-gate lookup_compatible(dev_info_t *dip, uint_t flag)
23807c478bd9Sstevel@tonic-gate {
23817c478bd9Sstevel@tonic-gate 	int rv;
23827c478bd9Sstevel@tonic-gate 	int prop_flags;
23837c478bd9Sstevel@tonic-gate 	uint_t ncompatstrs;
23847c478bd9Sstevel@tonic-gate 	char **compatstrpp;
23857c478bd9Sstevel@tonic-gate 	char *di_compat_strp;
23867c478bd9Sstevel@tonic-gate 	size_t di_compat_strlen;
23877c478bd9Sstevel@tonic-gate 
23887c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_compat_names) {
23897c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
23907c478bd9Sstevel@tonic-gate 	}
23917c478bd9Sstevel@tonic-gate 
23927c478bd9Sstevel@tonic-gate 	prop_flags = DDI_PROP_TYPE_STRING | DDI_PROP_DONTPASS;
23937c478bd9Sstevel@tonic-gate 
23947c478bd9Sstevel@tonic-gate 	if (flag & KM_NOSLEEP) {
23957c478bd9Sstevel@tonic-gate 		prop_flags |= DDI_PROP_DONTSLEEP;
23967c478bd9Sstevel@tonic-gate 	}
23977c478bd9Sstevel@tonic-gate 
23987c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_prom_node(dip) == 0) {
23997c478bd9Sstevel@tonic-gate 		prop_flags |= DDI_PROP_NOTPROM;
24007c478bd9Sstevel@tonic-gate 	}
24017c478bd9Sstevel@tonic-gate 
24027c478bd9Sstevel@tonic-gate 	rv = ddi_prop_lookup_common(DDI_DEV_T_ANY, dip, prop_flags,
24037c478bd9Sstevel@tonic-gate 	    "compatible", &compatstrpp, &ncompatstrs,
24047c478bd9Sstevel@tonic-gate 	    ddi_prop_fm_decode_strings);
24057c478bd9Sstevel@tonic-gate 
24067c478bd9Sstevel@tonic-gate 	if (rv == DDI_PROP_NOT_FOUND) {
24077c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
24087c478bd9Sstevel@tonic-gate 	}
24097c478bd9Sstevel@tonic-gate 
24107c478bd9Sstevel@tonic-gate 	if (rv != DDI_PROP_SUCCESS) {
24117c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
24127c478bd9Sstevel@tonic-gate 	}
24137c478bd9Sstevel@tonic-gate 
24147c478bd9Sstevel@tonic-gate 	/*
24157c478bd9Sstevel@tonic-gate 	 * encode the compatible property data in the dev_info node
24167c478bd9Sstevel@tonic-gate 	 */
24177c478bd9Sstevel@tonic-gate 	rv = DDI_SUCCESS;
24187c478bd9Sstevel@tonic-gate 	if (ncompatstrs != 0) {
24197c478bd9Sstevel@tonic-gate 		di_compat_strp = encode_composite_string(compatstrpp,
24207c478bd9Sstevel@tonic-gate 		    ncompatstrs, &di_compat_strlen, flag);
24217c478bd9Sstevel@tonic-gate 		if (di_compat_strp != NULL) {
24227c478bd9Sstevel@tonic-gate 			DEVI(dip)->devi_compat_names = di_compat_strp;
24237c478bd9Sstevel@tonic-gate 			DEVI(dip)->devi_compat_length = di_compat_strlen;
24247c478bd9Sstevel@tonic-gate 		} else {
24257c478bd9Sstevel@tonic-gate 			rv = DDI_FAILURE;
24267c478bd9Sstevel@tonic-gate 		}
24277c478bd9Sstevel@tonic-gate 	}
24287c478bd9Sstevel@tonic-gate 	ddi_prop_free(compatstrpp);
24297c478bd9Sstevel@tonic-gate 	return (rv);
24307c478bd9Sstevel@tonic-gate }
24317c478bd9Sstevel@tonic-gate 
24327c478bd9Sstevel@tonic-gate /*
24337c478bd9Sstevel@tonic-gate  * Create a composite string from a list of strings.
24347c478bd9Sstevel@tonic-gate  *
24357c478bd9Sstevel@tonic-gate  * A composite string consists of a single buffer containing one
24367c478bd9Sstevel@tonic-gate  * or more NULL terminated strings.
24377c478bd9Sstevel@tonic-gate  */
24387c478bd9Sstevel@tonic-gate static char *
24397c478bd9Sstevel@tonic-gate encode_composite_string(char **strings, uint_t nstrings, size_t *retsz,
24407c478bd9Sstevel@tonic-gate     uint_t flag)
24417c478bd9Sstevel@tonic-gate {
24427c478bd9Sstevel@tonic-gate 	uint_t index;
24437c478bd9Sstevel@tonic-gate 	char  **strpp;
24447c478bd9Sstevel@tonic-gate 	uint_t slen;
24457c478bd9Sstevel@tonic-gate 	size_t cbuf_sz = 0;
24467c478bd9Sstevel@tonic-gate 	char *cbuf_p;
24477c478bd9Sstevel@tonic-gate 	char *cbuf_ip;
24487c478bd9Sstevel@tonic-gate 
24497c478bd9Sstevel@tonic-gate 	if (strings == NULL || nstrings == 0 || retsz == NULL) {
24507c478bd9Sstevel@tonic-gate 		return (NULL);
24517c478bd9Sstevel@tonic-gate 	}
24527c478bd9Sstevel@tonic-gate 
24537c478bd9Sstevel@tonic-gate 	for (index = 0, strpp = strings; index < nstrings; index++)
24547c478bd9Sstevel@tonic-gate 		cbuf_sz += strlen(*(strpp++)) + 1;
24557c478bd9Sstevel@tonic-gate 
24567c478bd9Sstevel@tonic-gate 	if ((cbuf_p = kmem_alloc(cbuf_sz, flag)) == NULL) {
24577c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE,
24587c478bd9Sstevel@tonic-gate 		    "?failed to allocate device node compatstr");
24597c478bd9Sstevel@tonic-gate 		return (NULL);
24607c478bd9Sstevel@tonic-gate 	}
24617c478bd9Sstevel@tonic-gate 
24627c478bd9Sstevel@tonic-gate 	cbuf_ip = cbuf_p;
24637c478bd9Sstevel@tonic-gate 	for (index = 0, strpp = strings; index < nstrings; index++) {
24647c478bd9Sstevel@tonic-gate 		slen = strlen(*strpp);
24657c478bd9Sstevel@tonic-gate 		bcopy(*(strpp++), cbuf_ip, slen);
24667c478bd9Sstevel@tonic-gate 		cbuf_ip += slen;
24677c478bd9Sstevel@tonic-gate 		*(cbuf_ip++) = '\0';
24687c478bd9Sstevel@tonic-gate 	}
24697c478bd9Sstevel@tonic-gate 
24707c478bd9Sstevel@tonic-gate 	*retsz = cbuf_sz;
24717c478bd9Sstevel@tonic-gate 	return (cbuf_p);
24727c478bd9Sstevel@tonic-gate }
24737c478bd9Sstevel@tonic-gate 
24747c478bd9Sstevel@tonic-gate static void
24757c478bd9Sstevel@tonic-gate link_to_driver_list(dev_info_t *dip)
24767c478bd9Sstevel@tonic-gate {
24777c478bd9Sstevel@tonic-gate 	major_t major = DEVI(dip)->devi_major;
24787c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
24797c478bd9Sstevel@tonic-gate 
24807c478bd9Sstevel@tonic-gate 	ASSERT(major != (major_t)-1);
24817c478bd9Sstevel@tonic-gate 
24827c478bd9Sstevel@tonic-gate 	/*
24837c478bd9Sstevel@tonic-gate 	 * Remove from orphan list
24847c478bd9Sstevel@tonic-gate 	 */
24857c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
24867c478bd9Sstevel@tonic-gate 		dnp = &orphanlist;
24877c478bd9Sstevel@tonic-gate 		remove_from_dn_list(dnp, dip);
24887c478bd9Sstevel@tonic-gate 	}
24897c478bd9Sstevel@tonic-gate 
24907c478bd9Sstevel@tonic-gate 	/*
24917c478bd9Sstevel@tonic-gate 	 * Add to per driver list
24927c478bd9Sstevel@tonic-gate 	 */
24937c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
24947c478bd9Sstevel@tonic-gate 	add_to_dn_list(dnp, dip);
24957c478bd9Sstevel@tonic-gate }
24967c478bd9Sstevel@tonic-gate 
24977c478bd9Sstevel@tonic-gate static void
24987c478bd9Sstevel@tonic-gate unlink_from_driver_list(dev_info_t *dip)
24997c478bd9Sstevel@tonic-gate {
25007c478bd9Sstevel@tonic-gate 	major_t major = DEVI(dip)->devi_major;
25017c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
25027c478bd9Sstevel@tonic-gate 
25037c478bd9Sstevel@tonic-gate 	ASSERT(major != (major_t)-1);
25047c478bd9Sstevel@tonic-gate 
25057c478bd9Sstevel@tonic-gate 	/*
25067c478bd9Sstevel@tonic-gate 	 * Remove from per-driver list
25077c478bd9Sstevel@tonic-gate 	 */
25087c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
25097c478bd9Sstevel@tonic-gate 	remove_from_dn_list(dnp, dip);
25107c478bd9Sstevel@tonic-gate 
25117c478bd9Sstevel@tonic-gate 	/*
25127c478bd9Sstevel@tonic-gate 	 * Add to orphan list
25137c478bd9Sstevel@tonic-gate 	 */
25147c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
25157c478bd9Sstevel@tonic-gate 		dnp = &orphanlist;
25167c478bd9Sstevel@tonic-gate 		add_to_dn_list(dnp, dip);
25177c478bd9Sstevel@tonic-gate 	}
25187c478bd9Sstevel@tonic-gate }
25197c478bd9Sstevel@tonic-gate 
25207c478bd9Sstevel@tonic-gate /*
25217c478bd9Sstevel@tonic-gate  * scan the per-driver list looking for dev_info "dip"
25227c478bd9Sstevel@tonic-gate  */
25237c478bd9Sstevel@tonic-gate static dev_info_t *
25247c478bd9Sstevel@tonic-gate in_dn_list(struct devnames *dnp, dev_info_t *dip)
25257c478bd9Sstevel@tonic-gate {
25267c478bd9Sstevel@tonic-gate 	struct dev_info *idevi;
25277c478bd9Sstevel@tonic-gate 
25287c478bd9Sstevel@tonic-gate 	if ((idevi = DEVI(dnp->dn_head)) == NULL)
25297c478bd9Sstevel@tonic-gate 		return (NULL);
25307c478bd9Sstevel@tonic-gate 
25317c478bd9Sstevel@tonic-gate 	while (idevi) {
25327c478bd9Sstevel@tonic-gate 		if (idevi == DEVI(dip))
25337c478bd9Sstevel@tonic-gate 			return (dip);
25347c478bd9Sstevel@tonic-gate 		idevi = idevi->devi_next;
25357c478bd9Sstevel@tonic-gate 	}
25367c478bd9Sstevel@tonic-gate 	return (NULL);
25377c478bd9Sstevel@tonic-gate }
25387c478bd9Sstevel@tonic-gate 
25397c478bd9Sstevel@tonic-gate /*
25407c478bd9Sstevel@tonic-gate  * insert devinfo node 'dip' into the per-driver instance list
25417c478bd9Sstevel@tonic-gate  * headed by 'dnp'
25427c478bd9Sstevel@tonic-gate  *
25437c478bd9Sstevel@tonic-gate  * Nodes on the per-driver list are ordered: HW - SID - PSEUDO.  The order is
25447c478bd9Sstevel@tonic-gate  * required for merging of .conf file data to work properly.
25457c478bd9Sstevel@tonic-gate  */
25467c478bd9Sstevel@tonic-gate static void
25477c478bd9Sstevel@tonic-gate add_to_ordered_dn_list(struct devnames *dnp, dev_info_t *dip)
25487c478bd9Sstevel@tonic-gate {
25497c478bd9Sstevel@tonic-gate 	dev_info_t **dipp;
25507c478bd9Sstevel@tonic-gate 
25517c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&(dnp->dn_lock)));
25527c478bd9Sstevel@tonic-gate 
25537c478bd9Sstevel@tonic-gate 	dipp = &dnp->dn_head;
25547c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_prom_node(dip)) {
25557c478bd9Sstevel@tonic-gate 		/*
25567c478bd9Sstevel@tonic-gate 		 * Find the first non-prom node or end of list
25577c478bd9Sstevel@tonic-gate 		 */
25587c478bd9Sstevel@tonic-gate 		while (*dipp && (ndi_dev_is_prom_node(*dipp) != 0)) {
25597c478bd9Sstevel@tonic-gate 			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
25607c478bd9Sstevel@tonic-gate 		}
25617c478bd9Sstevel@tonic-gate 	} else if (ndi_dev_is_persistent_node(dip)) {
25627c478bd9Sstevel@tonic-gate 		/*
25637c478bd9Sstevel@tonic-gate 		 * Find the first non-persistent node
25647c478bd9Sstevel@tonic-gate 		 */
25657c478bd9Sstevel@tonic-gate 		while (*dipp && (ndi_dev_is_persistent_node(*dipp) != 0)) {
25667c478bd9Sstevel@tonic-gate 			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
25677c478bd9Sstevel@tonic-gate 		}
25687c478bd9Sstevel@tonic-gate 	} else {
25697c478bd9Sstevel@tonic-gate 		/*
25707c478bd9Sstevel@tonic-gate 		 * Find the end of the list
25717c478bd9Sstevel@tonic-gate 		 */
25727c478bd9Sstevel@tonic-gate 		while (*dipp) {
25737c478bd9Sstevel@tonic-gate 			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
25747c478bd9Sstevel@tonic-gate 		}
25757c478bd9Sstevel@tonic-gate 	}
25767c478bd9Sstevel@tonic-gate 
25777c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_next = DEVI(*dipp);
25787c478bd9Sstevel@tonic-gate 	*dipp = dip;
25797c478bd9Sstevel@tonic-gate }
25807c478bd9Sstevel@tonic-gate 
25817c478bd9Sstevel@tonic-gate /*
25827c478bd9Sstevel@tonic-gate  * add a list of device nodes to the device node list in the
25837c478bd9Sstevel@tonic-gate  * devnames structure
25847c478bd9Sstevel@tonic-gate  */
25857c478bd9Sstevel@tonic-gate static void
25867c478bd9Sstevel@tonic-gate add_to_dn_list(struct devnames *dnp, dev_info_t *dip)
25877c478bd9Sstevel@tonic-gate {
25887c478bd9Sstevel@tonic-gate 	/*
25897c478bd9Sstevel@tonic-gate 	 * Look to see if node already exists
25907c478bd9Sstevel@tonic-gate 	 */
25917c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&(dnp->dn_lock));
25927c478bd9Sstevel@tonic-gate 	if (in_dn_list(dnp, dip)) {
25937c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "add_to_dn_list: node %s already in list",
25947c478bd9Sstevel@tonic-gate 		    DEVI(dip)->devi_node_name);
25957c478bd9Sstevel@tonic-gate 	} else {
25967c478bd9Sstevel@tonic-gate 		add_to_ordered_dn_list(dnp, dip);
25977c478bd9Sstevel@tonic-gate 	}
25987c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
25997c478bd9Sstevel@tonic-gate }
26007c478bd9Sstevel@tonic-gate 
26017c478bd9Sstevel@tonic-gate static void
26027c478bd9Sstevel@tonic-gate remove_from_dn_list(struct devnames *dnp, dev_info_t *dip)
26037c478bd9Sstevel@tonic-gate {
26047c478bd9Sstevel@tonic-gate 	dev_info_t **plist;
26057c478bd9Sstevel@tonic-gate 
26067c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&(dnp->dn_lock));
26077c478bd9Sstevel@tonic-gate 
26087c478bd9Sstevel@tonic-gate 	plist = (dev_info_t **)&dnp->dn_head;
26097c478bd9Sstevel@tonic-gate 	while (*plist && (*plist != dip)) {
26107c478bd9Sstevel@tonic-gate 		plist = (dev_info_t **)&DEVI(*plist)->devi_next;
26117c478bd9Sstevel@tonic-gate 	}
26127c478bd9Sstevel@tonic-gate 
26137c478bd9Sstevel@tonic-gate 	if (*plist != NULL) {
26147c478bd9Sstevel@tonic-gate 		ASSERT(*plist == dip);
26157c478bd9Sstevel@tonic-gate 		*plist = (dev_info_t *)(DEVI(dip)->devi_next);
26167c478bd9Sstevel@tonic-gate 		DEVI(dip)->devi_next = NULL;
26177c478bd9Sstevel@tonic-gate 	} else {
26187c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE,
26197c478bd9Sstevel@tonic-gate 		    "remove_from_dn_list: node %s not found in list",
26207c478bd9Sstevel@tonic-gate 		    DEVI(dip)->devi_node_name));
26217c478bd9Sstevel@tonic-gate 	}
26227c478bd9Sstevel@tonic-gate 
26237c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
26247c478bd9Sstevel@tonic-gate }
26257c478bd9Sstevel@tonic-gate 
26267c478bd9Sstevel@tonic-gate /*
26277c478bd9Sstevel@tonic-gate  * Add and remove reference driver global property list
26287c478bd9Sstevel@tonic-gate  */
26297c478bd9Sstevel@tonic-gate static void
26307c478bd9Sstevel@tonic-gate add_global_props(dev_info_t *dip)
26317c478bd9Sstevel@tonic-gate {
26327c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
26337c478bd9Sstevel@tonic-gate 	ddi_prop_list_t *plist;
26347c478bd9Sstevel@tonic-gate 
26357c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_global_prop_list == NULL);
26367c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_major != (major_t)-1);
26377c478bd9Sstevel@tonic-gate 
26387c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[DEVI(dip)->devi_major];
26397c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
26407c478bd9Sstevel@tonic-gate 	plist = dnp->dn_global_prop_ptr;
26417c478bd9Sstevel@tonic-gate 	if (plist == NULL) {
26427c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
26437c478bd9Sstevel@tonic-gate 		return;
26447c478bd9Sstevel@tonic-gate 	}
26457c478bd9Sstevel@tonic-gate 	i_ddi_prop_list_hold(plist, dnp);
26467c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
26477c478bd9Sstevel@tonic-gate 
26487c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
26497c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_global_prop_list = plist;
26507c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
26517c478bd9Sstevel@tonic-gate }
26527c478bd9Sstevel@tonic-gate 
26537c478bd9Sstevel@tonic-gate static void
26547c478bd9Sstevel@tonic-gate remove_global_props(dev_info_t *dip)
26557c478bd9Sstevel@tonic-gate {
26567c478bd9Sstevel@tonic-gate 	ddi_prop_list_t *proplist;
26577c478bd9Sstevel@tonic-gate 
26587c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
26597c478bd9Sstevel@tonic-gate 	proplist = DEVI(dip)->devi_global_prop_list;
26607c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_global_prop_list = NULL;
26617c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
26627c478bd9Sstevel@tonic-gate 
26637c478bd9Sstevel@tonic-gate 	if (proplist) {
26647c478bd9Sstevel@tonic-gate 		major_t major;
26657c478bd9Sstevel@tonic-gate 		struct devnames *dnp;
26667c478bd9Sstevel@tonic-gate 
26677c478bd9Sstevel@tonic-gate 		major = ddi_driver_major(dip);
26687c478bd9Sstevel@tonic-gate 		ASSERT(major != (major_t)-1);
26697c478bd9Sstevel@tonic-gate 		dnp = &devnamesp[major];
26707c478bd9Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
26717c478bd9Sstevel@tonic-gate 		i_ddi_prop_list_rele(proplist, dnp);
26727c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
26737c478bd9Sstevel@tonic-gate 	}
26747c478bd9Sstevel@tonic-gate }
26757c478bd9Sstevel@tonic-gate 
26767c478bd9Sstevel@tonic-gate #ifdef DEBUG
26777c478bd9Sstevel@tonic-gate /*
26787c478bd9Sstevel@tonic-gate  * Set this variable to '0' to disable the optimization,
26797c478bd9Sstevel@tonic-gate  * and to 2 to print debug message.
26807c478bd9Sstevel@tonic-gate  */
26817c478bd9Sstevel@tonic-gate static int optimize_dtree = 1;
26827c478bd9Sstevel@tonic-gate 
26837c478bd9Sstevel@tonic-gate static void
26847c478bd9Sstevel@tonic-gate debug_dtree(dev_info_t *devi, struct dev_info *adevi, char *service)
26857c478bd9Sstevel@tonic-gate {
26867c478bd9Sstevel@tonic-gate 	char *adeviname, *buf;
26877c478bd9Sstevel@tonic-gate 
26887c478bd9Sstevel@tonic-gate 	/*
26897c478bd9Sstevel@tonic-gate 	 * Don't print unless optimize dtree is set to 2+
26907c478bd9Sstevel@tonic-gate 	 */
26917c478bd9Sstevel@tonic-gate 	if (optimize_dtree <= 1)
26927c478bd9Sstevel@tonic-gate 		return;
26937c478bd9Sstevel@tonic-gate 
26947c478bd9Sstevel@tonic-gate 	buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
26957c478bd9Sstevel@tonic-gate 	adeviname = ddi_deviname((dev_info_t *)adevi, buf);
26967c478bd9Sstevel@tonic-gate 	if (*adeviname == '\0')
26977c478bd9Sstevel@tonic-gate 		adeviname = "root";
26987c478bd9Sstevel@tonic-gate 
26997c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "%s %s -> %s\n",
27007c478bd9Sstevel@tonic-gate 	    ddi_deviname(devi, buf), service, adeviname);
27017c478bd9Sstevel@tonic-gate 
27027c478bd9Sstevel@tonic-gate 	kmem_free(buf, MAXNAMELEN);
27037c478bd9Sstevel@tonic-gate }
27047c478bd9Sstevel@tonic-gate #else /* DEBUG */
27057c478bd9Sstevel@tonic-gate #define	debug_dtree(a1, a2, a3)	 /* nothing */
27067c478bd9Sstevel@tonic-gate #endif  /* DEBUG */
27077c478bd9Sstevel@tonic-gate 
27087c478bd9Sstevel@tonic-gate static void
27097c478bd9Sstevel@tonic-gate ddi_optimize_dtree(dev_info_t *devi)
27107c478bd9Sstevel@tonic-gate {
27117c478bd9Sstevel@tonic-gate 	struct dev_info *pdevi;
27127c478bd9Sstevel@tonic-gate 	struct bus_ops *b;
27137c478bd9Sstevel@tonic-gate 
27147c478bd9Sstevel@tonic-gate 	pdevi = DEVI(devi)->devi_parent;
27157c478bd9Sstevel@tonic-gate 	ASSERT(pdevi);
27167c478bd9Sstevel@tonic-gate 
27177c478bd9Sstevel@tonic-gate 	/*
27187c478bd9Sstevel@tonic-gate 	 * Set the unoptimized values
27197c478bd9Sstevel@tonic-gate 	 */
27207c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_map_fault = pdevi;
27217c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_map = pdevi;
27227c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_allochdl = pdevi;
27237c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_freehdl = pdevi;
27247c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_bindhdl = pdevi;
27257c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_bindfunc =
27267c478bd9Sstevel@tonic-gate 	pdevi->devi_ops->devo_bus_ops->bus_dma_bindhdl;
27277c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_unbindhdl = pdevi;
27287c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_unbindfunc =
27297c478bd9Sstevel@tonic-gate 	    pdevi->devi_ops->devo_bus_ops->bus_dma_unbindhdl;
27307c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_flush = pdevi;
27317c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_win = pdevi;
27327c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_ctl = pdevi;
27337c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_ctl = pdevi;
27347c478bd9Sstevel@tonic-gate 
27357c478bd9Sstevel@tonic-gate #ifdef DEBUG
27367c478bd9Sstevel@tonic-gate 	if (optimize_dtree == 0)
27377c478bd9Sstevel@tonic-gate 		return;
27387c478bd9Sstevel@tonic-gate #endif /* DEBUG */
27397c478bd9Sstevel@tonic-gate 
27407c478bd9Sstevel@tonic-gate 	b = pdevi->devi_ops->devo_bus_ops;
27417c478bd9Sstevel@tonic-gate 
27427c478bd9Sstevel@tonic-gate 	if (i_ddi_map_fault == b->bus_map_fault) {
27437c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_map_fault = pdevi->devi_bus_map_fault;
27447c478bd9Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_map_fault,
27457c478bd9Sstevel@tonic-gate 		    "bus_map_fault");
27467c478bd9Sstevel@tonic-gate 	}
27477c478bd9Sstevel@tonic-gate 
27487c478bd9Sstevel@tonic-gate 	if (ddi_dma_map == b->bus_dma_map) {
27497c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_map = pdevi->devi_bus_dma_map;
27507c478bd9Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_map, "bus_dma_map");
27517c478bd9Sstevel@tonic-gate 	}
27527c478bd9Sstevel@tonic-gate 
27537c478bd9Sstevel@tonic-gate 	if (ddi_dma_allochdl == b->bus_dma_allochdl) {
27547c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_allochdl =
27557c478bd9Sstevel@tonic-gate 		    pdevi->devi_bus_dma_allochdl;
27567c478bd9Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_allochdl,
27577c478bd9Sstevel@tonic-gate 		    "bus_dma_allochdl");
27587c478bd9Sstevel@tonic-gate 	}
27597c478bd9Sstevel@tonic-gate 
27607c478bd9Sstevel@tonic-gate 	if (ddi_dma_freehdl == b->bus_dma_freehdl) {
27617c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_freehdl = pdevi->devi_bus_dma_freehdl;
27627c478bd9Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_freehdl,
27637c478bd9Sstevel@tonic-gate 		    "bus_dma_freehdl");
27647c478bd9Sstevel@tonic-gate 	}
27657c478bd9Sstevel@tonic-gate 
27667c478bd9Sstevel@tonic-gate 	if (ddi_dma_bindhdl == b->bus_dma_bindhdl) {
27677c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_bindhdl = pdevi->devi_bus_dma_bindhdl;
27687c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_bindfunc =
27697c478bd9Sstevel@tonic-gate 		    pdevi->devi_bus_dma_bindhdl->devi_ops->
27707c478bd9Sstevel@tonic-gate 		    devo_bus_ops->bus_dma_bindhdl;
27717c478bd9Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_bindhdl,
27727c478bd9Sstevel@tonic-gate 		    "bus_dma_bindhdl");
27737c478bd9Sstevel@tonic-gate 	}
27747c478bd9Sstevel@tonic-gate 
27757c478bd9Sstevel@tonic-gate 	if (ddi_dma_unbindhdl == b->bus_dma_unbindhdl) {
27767c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_unbindhdl =
27777c478bd9Sstevel@tonic-gate 		    pdevi->devi_bus_dma_unbindhdl;
27787c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_unbindfunc =
27797c478bd9Sstevel@tonic-gate 		    pdevi->devi_bus_dma_unbindhdl->devi_ops->
27807c478bd9Sstevel@tonic-gate 		    devo_bus_ops->bus_dma_unbindhdl;
27817c478bd9Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_unbindhdl,
27827c478bd9Sstevel@tonic-gate 		    "bus_dma_unbindhdl");
27837c478bd9Sstevel@tonic-gate 	}
27847c478bd9Sstevel@tonic-gate 
27857c478bd9Sstevel@tonic-gate 	if (ddi_dma_flush == b->bus_dma_flush) {
27867c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_flush = pdevi->devi_bus_dma_flush;
27877c478bd9Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_flush,
27887c478bd9Sstevel@tonic-gate 		    "bus_dma_flush");
27897c478bd9Sstevel@tonic-gate 	}
27907c478bd9Sstevel@tonic-gate 
27917c478bd9Sstevel@tonic-gate 	if (ddi_dma_win == b->bus_dma_win) {
27927c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_win = pdevi->devi_bus_dma_win;
27937c478bd9Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_win,
27947c478bd9Sstevel@tonic-gate 		    "bus_dma_win");
27957c478bd9Sstevel@tonic-gate 	}
27967c478bd9Sstevel@tonic-gate 
27977c478bd9Sstevel@tonic-gate 	if (ddi_dma_mctl == b->bus_dma_ctl) {
27987c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_ctl = pdevi->devi_bus_dma_ctl;
27997c478bd9Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_ctl, "bus_dma_ctl");
28007c478bd9Sstevel@tonic-gate 	}
28017c478bd9Sstevel@tonic-gate 
28027c478bd9Sstevel@tonic-gate 	if (ddi_ctlops == b->bus_ctl) {
28037c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_ctl = pdevi->devi_bus_ctl;
28047c478bd9Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_ctl, "bus_ctl");
28057c478bd9Sstevel@tonic-gate 	}
28067c478bd9Sstevel@tonic-gate }
28077c478bd9Sstevel@tonic-gate 
28087c478bd9Sstevel@tonic-gate #define	MIN_DEVINFO_LOG_SIZE	max_ncpus
28097c478bd9Sstevel@tonic-gate #define	MAX_DEVINFO_LOG_SIZE	max_ncpus * 10
28107c478bd9Sstevel@tonic-gate 
28117c478bd9Sstevel@tonic-gate static void
28127c478bd9Sstevel@tonic-gate da_log_init()
28137c478bd9Sstevel@tonic-gate {
28147c478bd9Sstevel@tonic-gate 	devinfo_log_header_t *dh;
28157c478bd9Sstevel@tonic-gate 	int logsize = devinfo_log_size;
28167c478bd9Sstevel@tonic-gate 
28177c478bd9Sstevel@tonic-gate 	if (logsize == 0)
28187c478bd9Sstevel@tonic-gate 		logsize = MIN_DEVINFO_LOG_SIZE;
28197c478bd9Sstevel@tonic-gate 	else if (logsize > MAX_DEVINFO_LOG_SIZE)
28207c478bd9Sstevel@tonic-gate 		logsize = MAX_DEVINFO_LOG_SIZE;
28217c478bd9Sstevel@tonic-gate 
28227c478bd9Sstevel@tonic-gate 	dh = kmem_alloc(logsize * PAGESIZE, KM_SLEEP);
28237c478bd9Sstevel@tonic-gate 	mutex_init(&dh->dh_lock, NULL, MUTEX_DEFAULT, NULL);
28247c478bd9Sstevel@tonic-gate 	dh->dh_max = ((logsize * PAGESIZE) - sizeof (*dh)) /
28257c478bd9Sstevel@tonic-gate 	    sizeof (devinfo_audit_t) + 1;
28267c478bd9Sstevel@tonic-gate 	dh->dh_curr = -1;
28277c478bd9Sstevel@tonic-gate 	dh->dh_hits = 0;
28287c478bd9Sstevel@tonic-gate 
28297c478bd9Sstevel@tonic-gate 	devinfo_audit_log = dh;
28307c478bd9Sstevel@tonic-gate }
28317c478bd9Sstevel@tonic-gate 
28327c478bd9Sstevel@tonic-gate /*
28337c478bd9Sstevel@tonic-gate  * Log the stack trace in per-devinfo audit structure and also enter
28347c478bd9Sstevel@tonic-gate  * it into a system wide log for recording the time history.
28357c478bd9Sstevel@tonic-gate  */
28367c478bd9Sstevel@tonic-gate static void
28377c478bd9Sstevel@tonic-gate da_log_enter(dev_info_t *dip)
28387c478bd9Sstevel@tonic-gate {
28397c478bd9Sstevel@tonic-gate 	devinfo_audit_t *da_log, *da = DEVI(dip)->devi_audit;
28407c478bd9Sstevel@tonic-gate 	devinfo_log_header_t *dh = devinfo_audit_log;
28417c478bd9Sstevel@tonic-gate 
28427c478bd9Sstevel@tonic-gate 	if (devinfo_audit_log == NULL)
28437c478bd9Sstevel@tonic-gate 		return;
28447c478bd9Sstevel@tonic-gate 
28457c478bd9Sstevel@tonic-gate 	ASSERT(da != NULL);
28467c478bd9Sstevel@tonic-gate 
28477c478bd9Sstevel@tonic-gate 	da->da_devinfo = dip;
28487c478bd9Sstevel@tonic-gate 	da->da_timestamp = gethrtime();
28497c478bd9Sstevel@tonic-gate 	da->da_thread = curthread;
28507c478bd9Sstevel@tonic-gate 	da->da_node_state = DEVI(dip)->devi_node_state;
28517c478bd9Sstevel@tonic-gate 	da->da_device_state = DEVI(dip)->devi_state;
28527c478bd9Sstevel@tonic-gate 	da->da_depth = getpcstack(da->da_stack, DDI_STACK_DEPTH);
28537c478bd9Sstevel@tonic-gate 
28547c478bd9Sstevel@tonic-gate 	/*
28557c478bd9Sstevel@tonic-gate 	 * Copy into common log and note the location for tracing history
28567c478bd9Sstevel@tonic-gate 	 */
28577c478bd9Sstevel@tonic-gate 	mutex_enter(&dh->dh_lock);
28587c478bd9Sstevel@tonic-gate 	dh->dh_hits++;
28597c478bd9Sstevel@tonic-gate 	dh->dh_curr++;
28607c478bd9Sstevel@tonic-gate 	if (dh->dh_curr >= dh->dh_max)
28617c478bd9Sstevel@tonic-gate 		dh->dh_curr -= dh->dh_max;
28627c478bd9Sstevel@tonic-gate 	da_log = &dh->dh_entry[dh->dh_curr];
28637c478bd9Sstevel@tonic-gate 	mutex_exit(&dh->dh_lock);
28647c478bd9Sstevel@tonic-gate 
28657c478bd9Sstevel@tonic-gate 	bcopy(da, da_log, sizeof (devinfo_audit_t));
28667c478bd9Sstevel@tonic-gate 	da->da_lastlog = da_log;
28677c478bd9Sstevel@tonic-gate }
28687c478bd9Sstevel@tonic-gate 
28697c478bd9Sstevel@tonic-gate static void
28707c478bd9Sstevel@tonic-gate attach_drivers()
28717c478bd9Sstevel@tonic-gate {
28727c478bd9Sstevel@tonic-gate 	int i;
28737c478bd9Sstevel@tonic-gate 	for (i = 0; i < devcnt; i++) {
28747c478bd9Sstevel@tonic-gate 		struct devnames *dnp = &devnamesp[i];
28757c478bd9Sstevel@tonic-gate 		if ((dnp->dn_flags & DN_FORCE_ATTACH) &&
28767c478bd9Sstevel@tonic-gate 		    (ddi_hold_installed_driver((major_t)i) != NULL))
28777c478bd9Sstevel@tonic-gate 			ddi_rele_driver((major_t)i);
28787c478bd9Sstevel@tonic-gate 	}
28797c478bd9Sstevel@tonic-gate }
28807c478bd9Sstevel@tonic-gate 
28817c478bd9Sstevel@tonic-gate /*
28827c478bd9Sstevel@tonic-gate  * Launch a thread to force attach drivers. This avoids penalty on boot time.
28837c478bd9Sstevel@tonic-gate  */
28847c478bd9Sstevel@tonic-gate void
28857c478bd9Sstevel@tonic-gate i_ddi_forceattach_drivers()
28867c478bd9Sstevel@tonic-gate {
28877c478bd9Sstevel@tonic-gate 	/*
28887c478bd9Sstevel@tonic-gate 	 * On i386, the USB drivers need to load and take over from the
28897c478bd9Sstevel@tonic-gate 	 * SMM BIOS drivers ASAP after consconfig(), so make sure they
28907c478bd9Sstevel@tonic-gate 	 * get loaded right here rather than letting the thread do it.
28917c478bd9Sstevel@tonic-gate 	 *
28927c478bd9Sstevel@tonic-gate 	 * The order here is important.  EHCI must be loaded first, as
28937c478bd9Sstevel@tonic-gate 	 * we have observed many systems on which hangs occur if the
28947c478bd9Sstevel@tonic-gate 	 * {U,O}HCI companion controllers take over control from the BIOS
28957c478bd9Sstevel@tonic-gate 	 * before EHCI does.  These hangs are also caused by BIOSes leaving
28967c478bd9Sstevel@tonic-gate 	 * interrupt-on-port-change enabled in the ehci controller, so that
28977c478bd9Sstevel@tonic-gate 	 * when uhci/ohci reset themselves, it induces a port change on
28987c478bd9Sstevel@tonic-gate 	 * the ehci companion controller.  Since there's no interrupt handler
28997c478bd9Sstevel@tonic-gate 	 * installed at the time, the moment that interrupt is unmasked, an
29007c478bd9Sstevel@tonic-gate 	 * interrupt storm will occur.  All this is averted when ehci is
29017c478bd9Sstevel@tonic-gate 	 * loaded first.  And now you know..... the REST of the story.
29027c478bd9Sstevel@tonic-gate 	 *
29037c478bd9Sstevel@tonic-gate 	 * Regardless of platform, ehci needs to initialize first to avoid
29047c478bd9Sstevel@tonic-gate 	 * unnecessary connects and disconnects on the companion controller
29057c478bd9Sstevel@tonic-gate 	 * when ehci sets up the routing.
29067c478bd9Sstevel@tonic-gate 	 */
29077c478bd9Sstevel@tonic-gate 	(void) ddi_hold_installed_driver(ddi_name_to_major("ehci"));
29087c478bd9Sstevel@tonic-gate 	(void) ddi_hold_installed_driver(ddi_name_to_major("uhci"));
29097c478bd9Sstevel@tonic-gate 	(void) ddi_hold_installed_driver(ddi_name_to_major("ohci"));
29107c478bd9Sstevel@tonic-gate 
29119d3d2ed0Shiremath 	/*
29129d3d2ed0Shiremath 	 * Attach IB VHCI driver before the force-attach thread attaches the
29139d3d2ed0Shiremath 	 * IB HCA driver. IB HCA driver will fail if IB Nexus has not yet
29149d3d2ed0Shiremath 	 * been attached.
29159d3d2ed0Shiremath 	 */
29169d3d2ed0Shiremath 	(void) ddi_hold_installed_driver(ddi_name_to_major("ib"));
29179d3d2ed0Shiremath 
29187c478bd9Sstevel@tonic-gate 	(void) thread_create(NULL, 0, (void (*)())attach_drivers, NULL, 0, &p0,
29197c478bd9Sstevel@tonic-gate 	    TS_RUN, minclsyspri);
29207c478bd9Sstevel@tonic-gate }
29217c478bd9Sstevel@tonic-gate 
29227c478bd9Sstevel@tonic-gate /*
29237c478bd9Sstevel@tonic-gate  * This is a private DDI interface for optimizing boot performance.
29247c478bd9Sstevel@tonic-gate  * I/O subsystem initialization is considered complete when devfsadm
29257c478bd9Sstevel@tonic-gate  * is executed.
29267c478bd9Sstevel@tonic-gate  *
29277c478bd9Sstevel@tonic-gate  * NOTE: The start of syseventd in S60devfsadm happen to be convenient
29287c478bd9Sstevel@tonic-gate  *	indicator for the completion of I/O initialization during boot.
29297c478bd9Sstevel@tonic-gate  *	The implementation should be replaced by something more robust.
29307c478bd9Sstevel@tonic-gate  */
29317c478bd9Sstevel@tonic-gate int
29327c478bd9Sstevel@tonic-gate i_ddi_io_initialized()
29337c478bd9Sstevel@tonic-gate {
29347c478bd9Sstevel@tonic-gate 	extern int sysevent_daemon_init;
29357c478bd9Sstevel@tonic-gate 	return (sysevent_daemon_init);
29367c478bd9Sstevel@tonic-gate }
29377c478bd9Sstevel@tonic-gate 
29387c478bd9Sstevel@tonic-gate 
29397c478bd9Sstevel@tonic-gate /*
29407c478bd9Sstevel@tonic-gate  * device tree walking
29417c478bd9Sstevel@tonic-gate  */
29427c478bd9Sstevel@tonic-gate 
29437c478bd9Sstevel@tonic-gate struct walk_elem {
29447c478bd9Sstevel@tonic-gate 	struct walk_elem *next;
29457c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
29467c478bd9Sstevel@tonic-gate };
29477c478bd9Sstevel@tonic-gate 
29487c478bd9Sstevel@tonic-gate static void
29497c478bd9Sstevel@tonic-gate free_list(struct walk_elem *list)
29507c478bd9Sstevel@tonic-gate {
29517c478bd9Sstevel@tonic-gate 	while (list) {
29527c478bd9Sstevel@tonic-gate 		struct walk_elem *next = list->next;
29537c478bd9Sstevel@tonic-gate 		kmem_free(list, sizeof (*list));
29547c478bd9Sstevel@tonic-gate 		list = next;
29557c478bd9Sstevel@tonic-gate 	}
29567c478bd9Sstevel@tonic-gate }
29577c478bd9Sstevel@tonic-gate 
29587c478bd9Sstevel@tonic-gate static void
29597c478bd9Sstevel@tonic-gate append_node(struct walk_elem **list, dev_info_t *dip)
29607c478bd9Sstevel@tonic-gate {
29617c478bd9Sstevel@tonic-gate 	struct walk_elem *tail;
29627c478bd9Sstevel@tonic-gate 	struct walk_elem *elem = kmem_alloc(sizeof (*elem), KM_SLEEP);
29637c478bd9Sstevel@tonic-gate 
29647c478bd9Sstevel@tonic-gate 	elem->next = NULL;
29657c478bd9Sstevel@tonic-gate 	elem->dip = dip;
29667c478bd9Sstevel@tonic-gate 
29677c478bd9Sstevel@tonic-gate 	if (*list == NULL) {
29687c478bd9Sstevel@tonic-gate 		*list = elem;
29697c478bd9Sstevel@tonic-gate 		return;
29707c478bd9Sstevel@tonic-gate 	}
29717c478bd9Sstevel@tonic-gate 
29727c478bd9Sstevel@tonic-gate 	tail = *list;
29737c478bd9Sstevel@tonic-gate 	while (tail->next)
29747c478bd9Sstevel@tonic-gate 		tail = tail->next;
29757c478bd9Sstevel@tonic-gate 
29767c478bd9Sstevel@tonic-gate 	tail->next = elem;
29777c478bd9Sstevel@tonic-gate }
29787c478bd9Sstevel@tonic-gate 
29797c478bd9Sstevel@tonic-gate /*
29807c478bd9Sstevel@tonic-gate  * The implementation of ddi_walk_devs().
29817c478bd9Sstevel@tonic-gate  */
29827c478bd9Sstevel@tonic-gate static int
29837c478bd9Sstevel@tonic-gate walk_devs(dev_info_t *dip, int (*f)(dev_info_t *, void *), void *arg,
29847c478bd9Sstevel@tonic-gate     int do_locking)
29857c478bd9Sstevel@tonic-gate {
29867c478bd9Sstevel@tonic-gate 	struct walk_elem *head = NULL;
29877c478bd9Sstevel@tonic-gate 
29887c478bd9Sstevel@tonic-gate 	/*
29897c478bd9Sstevel@tonic-gate 	 * Do it in two passes. First pass invoke callback on each
29907c478bd9Sstevel@tonic-gate 	 * dip on the sibling list. Second pass invoke callback on
29917c478bd9Sstevel@tonic-gate 	 * children of each dip.
29927c478bd9Sstevel@tonic-gate 	 */
29937c478bd9Sstevel@tonic-gate 	while (dip) {
29947c478bd9Sstevel@tonic-gate 		switch ((*f)(dip, arg)) {
29957c478bd9Sstevel@tonic-gate 		case DDI_WALK_TERMINATE:
29967c478bd9Sstevel@tonic-gate 			free_list(head);
29977c478bd9Sstevel@tonic-gate 			return (DDI_WALK_TERMINATE);
29987c478bd9Sstevel@tonic-gate 
29997c478bd9Sstevel@tonic-gate 		case DDI_WALK_PRUNESIB:
30007c478bd9Sstevel@tonic-gate 			/* ignore sibling by setting dip to NULL */
30017c478bd9Sstevel@tonic-gate 			append_node(&head, dip);
30027c478bd9Sstevel@tonic-gate 			dip = NULL;
30037c478bd9Sstevel@tonic-gate 			break;
30047c478bd9Sstevel@tonic-gate 
30057c478bd9Sstevel@tonic-gate 		case DDI_WALK_PRUNECHILD:
30067c478bd9Sstevel@tonic-gate 			/* don't worry about children */
30077c478bd9Sstevel@tonic-gate 			dip = ddi_get_next_sibling(dip);
30087c478bd9Sstevel@tonic-gate 			break;
30097c478bd9Sstevel@tonic-gate 
30107c478bd9Sstevel@tonic-gate 		case DDI_WALK_CONTINUE:
30117c478bd9Sstevel@tonic-gate 		default:
30127c478bd9Sstevel@tonic-gate 			append_node(&head, dip);
30137c478bd9Sstevel@tonic-gate 			dip = ddi_get_next_sibling(dip);
30147c478bd9Sstevel@tonic-gate 			break;
30157c478bd9Sstevel@tonic-gate 		}
30167c478bd9Sstevel@tonic-gate 
30177c478bd9Sstevel@tonic-gate 	}
30187c478bd9Sstevel@tonic-gate 
30197c478bd9Sstevel@tonic-gate 	/* second pass */
30207c478bd9Sstevel@tonic-gate 	while (head) {
30217c478bd9Sstevel@tonic-gate 		int circ;
30227c478bd9Sstevel@tonic-gate 		struct walk_elem *next = head->next;
30237c478bd9Sstevel@tonic-gate 
30247c478bd9Sstevel@tonic-gate 		if (do_locking)
30257c478bd9Sstevel@tonic-gate 			ndi_devi_enter(head->dip, &circ);
30267c478bd9Sstevel@tonic-gate 		if (walk_devs(ddi_get_child(head->dip), f, arg, do_locking) ==
30277c478bd9Sstevel@tonic-gate 		    DDI_WALK_TERMINATE) {
30287c478bd9Sstevel@tonic-gate 			if (do_locking)
30297c478bd9Sstevel@tonic-gate 				ndi_devi_exit(head->dip, circ);
30307c478bd9Sstevel@tonic-gate 			free_list(head);
30317c478bd9Sstevel@tonic-gate 			return (DDI_WALK_TERMINATE);
30327c478bd9Sstevel@tonic-gate 		}
30337c478bd9Sstevel@tonic-gate 		if (do_locking)
30347c478bd9Sstevel@tonic-gate 			ndi_devi_exit(head->dip, circ);
30357c478bd9Sstevel@tonic-gate 		kmem_free(head, sizeof (*head));
30367c478bd9Sstevel@tonic-gate 		head = next;
30377c478bd9Sstevel@tonic-gate 	}
30387c478bd9Sstevel@tonic-gate 
30397c478bd9Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
30407c478bd9Sstevel@tonic-gate }
30417c478bd9Sstevel@tonic-gate 
30427c478bd9Sstevel@tonic-gate /*
30437c478bd9Sstevel@tonic-gate  * This general-purpose routine traverses the tree of dev_info nodes,
30447c478bd9Sstevel@tonic-gate  * starting from the given node, and calls the given function for each
30457c478bd9Sstevel@tonic-gate  * node that it finds with the current node and the pointer arg (which
30467c478bd9Sstevel@tonic-gate  * can point to a structure of information that the function
30477c478bd9Sstevel@tonic-gate  * needs) as arguments.
30487c478bd9Sstevel@tonic-gate  *
30497c478bd9Sstevel@tonic-gate  * It does the walk a layer at a time, not depth-first. The given function
30507c478bd9Sstevel@tonic-gate  * must return one of the following values:
30517c478bd9Sstevel@tonic-gate  *	DDI_WALK_CONTINUE
30527c478bd9Sstevel@tonic-gate  *	DDI_WALK_PRUNESIB
30537c478bd9Sstevel@tonic-gate  *	DDI_WALK_PRUNECHILD
30547c478bd9Sstevel@tonic-gate  *	DDI_WALK_TERMINATE
30557c478bd9Sstevel@tonic-gate  *
30567c478bd9Sstevel@tonic-gate  * N.B. Since we walk the sibling list, the caller must ensure that
30577c478bd9Sstevel@tonic-gate  *	the parent of dip is held against changes, unless the parent
30587c478bd9Sstevel@tonic-gate  *	is rootnode.  ndi_devi_enter() on the parent is sufficient.
30597c478bd9Sstevel@tonic-gate  *
30607c478bd9Sstevel@tonic-gate  *	To avoid deadlock situations, caller must not attempt to
30617c478bd9Sstevel@tonic-gate  *	configure/unconfigure/remove device node in (*f)(), nor should
30627c478bd9Sstevel@tonic-gate  *	it attempt to recurse on other nodes in the system.
30637c478bd9Sstevel@tonic-gate  *
30647c478bd9Sstevel@tonic-gate  *	This is not callable from device autoconfiguration routines.
30657c478bd9Sstevel@tonic-gate  *	They include, but not limited to, _init(9e), _fini(9e), probe(9e),
30667c478bd9Sstevel@tonic-gate  *	attach(9e), and detach(9e).
30677c478bd9Sstevel@tonic-gate  */
30687c478bd9Sstevel@tonic-gate 
30697c478bd9Sstevel@tonic-gate void
30707c478bd9Sstevel@tonic-gate ddi_walk_devs(dev_info_t *dip, int (*f)(dev_info_t *, void *), void *arg)
30717c478bd9Sstevel@tonic-gate {
30727c478bd9Sstevel@tonic-gate 
30737c478bd9Sstevel@tonic-gate 	ASSERT(dip == NULL || ddi_get_parent(dip) == NULL ||
30747c478bd9Sstevel@tonic-gate 		DEVI_BUSY_OWNED(ddi_get_parent(dip)));
30757c478bd9Sstevel@tonic-gate 
30767c478bd9Sstevel@tonic-gate 	(void) walk_devs(dip, f, arg, 1);
30777c478bd9Sstevel@tonic-gate }
30787c478bd9Sstevel@tonic-gate 
30797c478bd9Sstevel@tonic-gate /*
30807c478bd9Sstevel@tonic-gate  * This is a general-purpose routine traverses the per-driver list
30817c478bd9Sstevel@tonic-gate  * and calls the given function for each node. must return one of
30827c478bd9Sstevel@tonic-gate  * the following values:
30837c478bd9Sstevel@tonic-gate  *	DDI_WALK_CONTINUE
30847c478bd9Sstevel@tonic-gate  *	DDI_WALK_TERMINATE
30857c478bd9Sstevel@tonic-gate  *
30867c478bd9Sstevel@tonic-gate  * N.B. The same restrictions from ddi_walk_devs() apply.
30877c478bd9Sstevel@tonic-gate  */
30887c478bd9Sstevel@tonic-gate 
30897c478bd9Sstevel@tonic-gate void
30907c478bd9Sstevel@tonic-gate e_ddi_walk_driver(char *drv, int (*f)(dev_info_t *, void *), void *arg)
30917c478bd9Sstevel@tonic-gate {
30927c478bd9Sstevel@tonic-gate 	major_t major;
30937c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
30947c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
30957c478bd9Sstevel@tonic-gate 
30967c478bd9Sstevel@tonic-gate 	major = ddi_name_to_major(drv);
30977c478bd9Sstevel@tonic-gate 	if (major == (major_t)-1)
30987c478bd9Sstevel@tonic-gate 		return;
30997c478bd9Sstevel@tonic-gate 
31007c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
31017c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
31027c478bd9Sstevel@tonic-gate 	dip = dnp->dn_head;
31037c478bd9Sstevel@tonic-gate 	while (dip) {
31047c478bd9Sstevel@tonic-gate 		ndi_hold_devi(dip);
31057c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
31067c478bd9Sstevel@tonic-gate 		if ((*f)(dip, arg) == DDI_WALK_TERMINATE) {
31077c478bd9Sstevel@tonic-gate 			ndi_rele_devi(dip);
31087c478bd9Sstevel@tonic-gate 			return;
31097c478bd9Sstevel@tonic-gate 		}
31107c478bd9Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
31117c478bd9Sstevel@tonic-gate 		ndi_rele_devi(dip);
31127c478bd9Sstevel@tonic-gate 		dip = ddi_get_next(dip);
31137c478bd9Sstevel@tonic-gate 	}
31147c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
31157c478bd9Sstevel@tonic-gate }
31167c478bd9Sstevel@tonic-gate 
31177c478bd9Sstevel@tonic-gate /*
31187c478bd9Sstevel@tonic-gate  * argument to i_find_devi, a devinfo node search callback function.
31197c478bd9Sstevel@tonic-gate  */
31207c478bd9Sstevel@tonic-gate struct match_info {
31217c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;		/* result */
31227c478bd9Sstevel@tonic-gate 	char		*nodename;	/* if non-null, nodename must match */
31237c478bd9Sstevel@tonic-gate 	int		instance;	/* if != -1, instance must match */
31247c478bd9Sstevel@tonic-gate 	int		attached;	/* if != 0, state >= DS_ATTACHED */
31257c478bd9Sstevel@tonic-gate };
31267c478bd9Sstevel@tonic-gate 
31277c478bd9Sstevel@tonic-gate static int
31287c478bd9Sstevel@tonic-gate i_find_devi(dev_info_t *dip, void *arg)
31297c478bd9Sstevel@tonic-gate {
31307c478bd9Sstevel@tonic-gate 	struct match_info *info = (struct match_info *)arg;
31317c478bd9Sstevel@tonic-gate 
31327c478bd9Sstevel@tonic-gate 	if (((info->nodename == NULL) ||
31337c478bd9Sstevel@tonic-gate 		(strcmp(ddi_node_name(dip), info->nodename) == 0)) &&
31347c478bd9Sstevel@tonic-gate 	    ((info->instance == -1) ||
31357c478bd9Sstevel@tonic-gate 		(ddi_get_instance(dip) == info->instance)) &&
31367c478bd9Sstevel@tonic-gate 	    ((info->attached == 0) ||
31377c478bd9Sstevel@tonic-gate 		(i_ddi_node_state(dip) >= DS_ATTACHED))) {
31387c478bd9Sstevel@tonic-gate 		info->dip = dip;
31397c478bd9Sstevel@tonic-gate 		ndi_hold_devi(dip);
31407c478bd9Sstevel@tonic-gate 		return (DDI_WALK_TERMINATE);
31417c478bd9Sstevel@tonic-gate 	}
31427c478bd9Sstevel@tonic-gate 
31437c478bd9Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
31447c478bd9Sstevel@tonic-gate }
31457c478bd9Sstevel@tonic-gate 
31467c478bd9Sstevel@tonic-gate /*
31477c478bd9Sstevel@tonic-gate  * Find dip with a known node name and instance and return with it held
31487c478bd9Sstevel@tonic-gate  */
31497c478bd9Sstevel@tonic-gate dev_info_t *
31507c478bd9Sstevel@tonic-gate ddi_find_devinfo(char *nodename, int instance, int attached)
31517c478bd9Sstevel@tonic-gate {
31527c478bd9Sstevel@tonic-gate 	struct match_info	info;
31537c478bd9Sstevel@tonic-gate 
31547c478bd9Sstevel@tonic-gate 	info.nodename = nodename;
31557c478bd9Sstevel@tonic-gate 	info.instance = instance;
31567c478bd9Sstevel@tonic-gate 	info.attached = attached;
31577c478bd9Sstevel@tonic-gate 	info.dip = NULL;
31587c478bd9Sstevel@tonic-gate 
31597c478bd9Sstevel@tonic-gate 	ddi_walk_devs(ddi_root_node(), i_find_devi, &info);
31607c478bd9Sstevel@tonic-gate 	return (info.dip);
31617c478bd9Sstevel@tonic-gate }
31627c478bd9Sstevel@tonic-gate 
31637c478bd9Sstevel@tonic-gate /*
31647c478bd9Sstevel@tonic-gate  * Parse for name, addr, and minor names. Some args may be NULL.
31657c478bd9Sstevel@tonic-gate  */
31667c478bd9Sstevel@tonic-gate void
31677c478bd9Sstevel@tonic-gate i_ddi_parse_name(char *name, char **nodename, char **addrname, char **minorname)
31687c478bd9Sstevel@tonic-gate {
31697c478bd9Sstevel@tonic-gate 	char *cp;
31707c478bd9Sstevel@tonic-gate 	static char nulladdrname[] = "";
31717c478bd9Sstevel@tonic-gate 
31727c478bd9Sstevel@tonic-gate 	/* default values */
31737c478bd9Sstevel@tonic-gate 	if (nodename)
31747c478bd9Sstevel@tonic-gate 		*nodename = name;
31757c478bd9Sstevel@tonic-gate 	if (addrname)
31767c478bd9Sstevel@tonic-gate 		*addrname = nulladdrname;
31777c478bd9Sstevel@tonic-gate 	if (minorname)
31787c478bd9Sstevel@tonic-gate 		*minorname = NULL;
31797c478bd9Sstevel@tonic-gate 
31807c478bd9Sstevel@tonic-gate 	cp = name;
31817c478bd9Sstevel@tonic-gate 	while (*cp != '\0') {
31827c478bd9Sstevel@tonic-gate 		if (addrname && *cp == '@') {
31837c478bd9Sstevel@tonic-gate 			*addrname = cp + 1;
31847c478bd9Sstevel@tonic-gate 			*cp = '\0';
31857c478bd9Sstevel@tonic-gate 		} else if (minorname && *cp == ':') {
31867c478bd9Sstevel@tonic-gate 			*minorname = cp + 1;
31877c478bd9Sstevel@tonic-gate 			*cp = '\0';
31887c478bd9Sstevel@tonic-gate 		}
31897c478bd9Sstevel@tonic-gate 		++cp;
31907c478bd9Sstevel@tonic-gate 	}
31917c478bd9Sstevel@tonic-gate }
31927c478bd9Sstevel@tonic-gate 
31937c478bd9Sstevel@tonic-gate static char *
31947c478bd9Sstevel@tonic-gate child_path_to_driver(dev_info_t *parent, char *child_name, char *unit_address)
31957c478bd9Sstevel@tonic-gate {
31967c478bd9Sstevel@tonic-gate 	char *p, *drvname = NULL;
31977c478bd9Sstevel@tonic-gate 	major_t maj;
31987c478bd9Sstevel@tonic-gate 
31997c478bd9Sstevel@tonic-gate 	/*
32007c478bd9Sstevel@tonic-gate 	 * Construct the pathname and ask the implementation
32017c478bd9Sstevel@tonic-gate 	 * if it can do a driver = f(pathname) for us, if not
32027c478bd9Sstevel@tonic-gate 	 * we'll just default to using the node-name that
32037c478bd9Sstevel@tonic-gate 	 * was given to us.  We want to do this first to
32047c478bd9Sstevel@tonic-gate 	 * allow the platform to use 'generic' names for
32057c478bd9Sstevel@tonic-gate 	 * legacy device drivers.
32067c478bd9Sstevel@tonic-gate 	 */
32077c478bd9Sstevel@tonic-gate 	p = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
32087c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(parent, p);
32097c478bd9Sstevel@tonic-gate 	(void) strcat(p, "/");
32107c478bd9Sstevel@tonic-gate 	(void) strcat(p, child_name);
32117c478bd9Sstevel@tonic-gate 	if (unit_address && *unit_address) {
32127c478bd9Sstevel@tonic-gate 		(void) strcat(p, "@");
32137c478bd9Sstevel@tonic-gate 		(void) strcat(p, unit_address);
32147c478bd9Sstevel@tonic-gate 	}
32157c478bd9Sstevel@tonic-gate 
32167c478bd9Sstevel@tonic-gate 	/*
32177c478bd9Sstevel@tonic-gate 	 * Get the binding. If there is none, return the child_name
32187c478bd9Sstevel@tonic-gate 	 * and let the caller deal with it.
32197c478bd9Sstevel@tonic-gate 	 */
32207c478bd9Sstevel@tonic-gate 	maj = path_to_major(p);
32217c478bd9Sstevel@tonic-gate 
32227c478bd9Sstevel@tonic-gate 	kmem_free(p, MAXPATHLEN);
32237c478bd9Sstevel@tonic-gate 
32247c478bd9Sstevel@tonic-gate 	if (maj != (major_t)-1)
32257c478bd9Sstevel@tonic-gate 		drvname = ddi_major_to_name(maj);
32267c478bd9Sstevel@tonic-gate 	if (drvname == NULL)
32277c478bd9Sstevel@tonic-gate 		drvname = child_name;
32287c478bd9Sstevel@tonic-gate 
32297c478bd9Sstevel@tonic-gate 	return (drvname);
32307c478bd9Sstevel@tonic-gate }
32317c478bd9Sstevel@tonic-gate 
32327c478bd9Sstevel@tonic-gate 
32337c478bd9Sstevel@tonic-gate /*
32347c478bd9Sstevel@tonic-gate  * Given the pathname of a device, fill in the dev_info_t value and/or the
32357c478bd9Sstevel@tonic-gate  * dev_t value and/or the spectype, depending on which parameters are non-NULL.
32367c478bd9Sstevel@tonic-gate  * If there is an error, this function returns -1.
32377c478bd9Sstevel@tonic-gate  *
32387c478bd9Sstevel@tonic-gate  * NOTE: If this function returns the dev_info_t structure, then it
32397c478bd9Sstevel@tonic-gate  * does so with a hold on the devi. Caller should ensure that they get
32407c478bd9Sstevel@tonic-gate  * decremented via ddi_release_devi() or ndi_rele_devi();
32417c478bd9Sstevel@tonic-gate  *
32427c478bd9Sstevel@tonic-gate  * This function can be invoked in the boot case for a pathname without
32437c478bd9Sstevel@tonic-gate  * device argument (:xxxx), traditionally treated as a minor name.
32447c478bd9Sstevel@tonic-gate  * In this case, we do the following
32457c478bd9Sstevel@tonic-gate  * (1) search the minor node of type DDM_DEFAULT.
32467c478bd9Sstevel@tonic-gate  * (2) if no DDM_DEFAULT minor exists, then the first non-alias minor is chosen.
32477c478bd9Sstevel@tonic-gate  * (3) if neither exists, a dev_t is faked with minor number = instance.
32487c478bd9Sstevel@tonic-gate  * As of S9 FCS, no instance of #1 exists. #2 is used by several platforms
32497c478bd9Sstevel@tonic-gate  * to default the boot partition to :a possibly by other OBP definitions.
32507c478bd9Sstevel@tonic-gate  * #3 is used for booting off network interfaces, most SPARC network
32517c478bd9Sstevel@tonic-gate  * drivers support Style-2 only, so only DDM_ALIAS minor exists.
32527c478bd9Sstevel@tonic-gate  *
32537c478bd9Sstevel@tonic-gate  * It is possible for OBP to present device args at the end of the path as
32547c478bd9Sstevel@tonic-gate  * well as in the middle. For example, with IB the following strings are
32557c478bd9Sstevel@tonic-gate  * valid boot paths.
32567c478bd9Sstevel@tonic-gate  *	a /pci@8,700000/ib@1,2:port=1,pkey=ff,dhcp,...
32577c478bd9Sstevel@tonic-gate  *	b /pci@8,700000/ib@1,1:port=1/ioc@xxxxxx,yyyyyyy:dhcp
32587c478bd9Sstevel@tonic-gate  * Case (a), we first look for minor node "port=1,pkey...".
32597c478bd9Sstevel@tonic-gate  * Failing that, we will pass "port=1,pkey..." to the bus_config
32607c478bd9Sstevel@tonic-gate  * entry point of ib (HCA) driver.
32617c478bd9Sstevel@tonic-gate  * Case (b), configure ib@1,1 as usual. Then invoke ib's bus_config
32627c478bd9Sstevel@tonic-gate  * with argument "ioc@xxxxxxx,yyyyyyy:port=1". After configuring
32637c478bd9Sstevel@tonic-gate  * the ioc, look for minor node dhcp. If not found, pass ":dhcp"
32647c478bd9Sstevel@tonic-gate  * to ioc's bus_config entry point.
32657c478bd9Sstevel@tonic-gate  */
32667c478bd9Sstevel@tonic-gate int
32677c478bd9Sstevel@tonic-gate resolve_pathname(char *pathname,
32687c478bd9Sstevel@tonic-gate 	dev_info_t **dipp, dev_t *devtp, int *spectypep)
32697c478bd9Sstevel@tonic-gate {
32707c478bd9Sstevel@tonic-gate 	int error;
32717c478bd9Sstevel@tonic-gate 	dev_info_t *parent, *child;
32727c478bd9Sstevel@tonic-gate 	struct pathname pn;
32737c478bd9Sstevel@tonic-gate 	char *component, *config_name;
32747c478bd9Sstevel@tonic-gate 	char *minorname = NULL;
32757c478bd9Sstevel@tonic-gate 	char *prev_minor = NULL;
32767c478bd9Sstevel@tonic-gate 	dev_t devt = NODEV;
32777c478bd9Sstevel@tonic-gate 	int spectype;
32787c478bd9Sstevel@tonic-gate 	struct ddi_minor_data *dmn;
32797c478bd9Sstevel@tonic-gate 
32807c478bd9Sstevel@tonic-gate 	if (*pathname != '/')
32817c478bd9Sstevel@tonic-gate 		return (EINVAL);
32827c478bd9Sstevel@tonic-gate 	parent = ddi_root_node();	/* Begin at the top of the tree */
32837c478bd9Sstevel@tonic-gate 
32847c478bd9Sstevel@tonic-gate 	if (error = pn_get(pathname, UIO_SYSSPACE, &pn))
32857c478bd9Sstevel@tonic-gate 		return (error);
32867c478bd9Sstevel@tonic-gate 	pn_skipslash(&pn);
32877c478bd9Sstevel@tonic-gate 
32887c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(parent) >= DS_ATTACHED);
32897c478bd9Sstevel@tonic-gate 	ndi_hold_devi(parent);
32907c478bd9Sstevel@tonic-gate 
32917c478bd9Sstevel@tonic-gate 	component = kmem_alloc(MAXNAMELEN, KM_SLEEP);
32927c478bd9Sstevel@tonic-gate 	config_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
32937c478bd9Sstevel@tonic-gate 
32947c478bd9Sstevel@tonic-gate 	while (pn_pathleft(&pn)) {
32957c478bd9Sstevel@tonic-gate 		/* remember prev minor (:xxx) in the middle of path */
32967c478bd9Sstevel@tonic-gate 		if (minorname)
32977c478bd9Sstevel@tonic-gate 			prev_minor = i_ddi_strdup(minorname, KM_SLEEP);
32987c478bd9Sstevel@tonic-gate 
32997c478bd9Sstevel@tonic-gate 		/* Get component and chop off minorname */
33007c478bd9Sstevel@tonic-gate 		(void) pn_getcomponent(&pn, component);
33017c478bd9Sstevel@tonic-gate 		i_ddi_parse_name(component, NULL, NULL, &minorname);
33027c478bd9Sstevel@tonic-gate 
33037c478bd9Sstevel@tonic-gate 		if (prev_minor == NULL) {
33047c478bd9Sstevel@tonic-gate 			(void) snprintf(config_name, MAXNAMELEN, "%s",
33057c478bd9Sstevel@tonic-gate 			    component);
33067c478bd9Sstevel@tonic-gate 		} else {
33077c478bd9Sstevel@tonic-gate 			(void) snprintf(config_name, MAXNAMELEN, "%s:%s",
33087c478bd9Sstevel@tonic-gate 			    component, prev_minor);
33097c478bd9Sstevel@tonic-gate 			kmem_free(prev_minor, strlen(prev_minor) + 1);
33107c478bd9Sstevel@tonic-gate 			prev_minor = NULL;
33117c478bd9Sstevel@tonic-gate 		}
33127c478bd9Sstevel@tonic-gate 
33137c478bd9Sstevel@tonic-gate 		/*
33147c478bd9Sstevel@tonic-gate 		 * Find and configure the child
33157c478bd9Sstevel@tonic-gate 		 */
33167c478bd9Sstevel@tonic-gate 		if (ndi_devi_config_one(parent, config_name, &child,
33177c478bd9Sstevel@tonic-gate 		    NDI_PROMNAME | NDI_NO_EVENT) != NDI_SUCCESS) {
33187c478bd9Sstevel@tonic-gate 			ndi_rele_devi(parent);
33197c478bd9Sstevel@tonic-gate 			pn_free(&pn);
33207c478bd9Sstevel@tonic-gate 			kmem_free(component, MAXNAMELEN);
33217c478bd9Sstevel@tonic-gate 			kmem_free(config_name, MAXNAMELEN);
33227c478bd9Sstevel@tonic-gate 			return (-1);
33237c478bd9Sstevel@tonic-gate 		}
33247c478bd9Sstevel@tonic-gate 
33257c478bd9Sstevel@tonic-gate 		ASSERT(i_ddi_node_state(child) >= DS_ATTACHED);
33267c478bd9Sstevel@tonic-gate 		ndi_rele_devi(parent);
33277c478bd9Sstevel@tonic-gate 		parent = child;
33287c478bd9Sstevel@tonic-gate 		pn_skipslash(&pn);
33297c478bd9Sstevel@tonic-gate 	}
33307c478bd9Sstevel@tonic-gate 
33317c478bd9Sstevel@tonic-gate 	/*
33327c478bd9Sstevel@tonic-gate 	 * First look for a minor node matching minorname.
33337c478bd9Sstevel@tonic-gate 	 * Failing that, try to pass minorname to bus_config().
33347c478bd9Sstevel@tonic-gate 	 */
33357c478bd9Sstevel@tonic-gate 	if (minorname && i_ddi_minorname_to_devtspectype(parent,
33367c478bd9Sstevel@tonic-gate 	    minorname, &devt, &spectype) == DDI_FAILURE) {
33377c478bd9Sstevel@tonic-gate 		(void) snprintf(config_name, MAXNAMELEN, "%s", minorname);
33387c478bd9Sstevel@tonic-gate 		if (ndi_devi_config_obp_args(parent,
33397c478bd9Sstevel@tonic-gate 		    config_name, &child, 0) != NDI_SUCCESS) {
33407c478bd9Sstevel@tonic-gate 			ndi_rele_devi(parent);
33417c478bd9Sstevel@tonic-gate 			pn_free(&pn);
33427c478bd9Sstevel@tonic-gate 			kmem_free(component, MAXNAMELEN);
33437c478bd9Sstevel@tonic-gate 			kmem_free(config_name, MAXNAMELEN);
33447c478bd9Sstevel@tonic-gate 			NDI_CONFIG_DEBUG((CE_NOTE,
33457c478bd9Sstevel@tonic-gate 			    "%s: minor node not found\n", pathname));
33467c478bd9Sstevel@tonic-gate 			return (-1);
33477c478bd9Sstevel@tonic-gate 		}
33487c478bd9Sstevel@tonic-gate 		minorname = NULL;	/* look for default minor */
33497c478bd9Sstevel@tonic-gate 		ASSERT(i_ddi_node_state(child) >= DS_ATTACHED);
33507c478bd9Sstevel@tonic-gate 		ndi_rele_devi(parent);
33517c478bd9Sstevel@tonic-gate 		parent = child;
33527c478bd9Sstevel@tonic-gate 	}
33537c478bd9Sstevel@tonic-gate 
33547c478bd9Sstevel@tonic-gate 	if (devtp || spectypep) {
33557c478bd9Sstevel@tonic-gate 		if (minorname == NULL) {
33567c478bd9Sstevel@tonic-gate 			/* search for a default entry */
33577c478bd9Sstevel@tonic-gate 			mutex_enter(&(DEVI(parent)->devi_lock));
33587c478bd9Sstevel@tonic-gate 			for (dmn = DEVI(parent)->devi_minor; dmn;
33597c478bd9Sstevel@tonic-gate 			    dmn = dmn->next) {
33607c478bd9Sstevel@tonic-gate 				if (dmn->type == DDM_DEFAULT) {
33617c478bd9Sstevel@tonic-gate 					devt = dmn->ddm_dev;
33627c478bd9Sstevel@tonic-gate 					spectype = dmn->ddm_spec_type;
33637c478bd9Sstevel@tonic-gate 					break;
33647c478bd9Sstevel@tonic-gate 				}
33657c478bd9Sstevel@tonic-gate 			}
33667c478bd9Sstevel@tonic-gate 
33677c478bd9Sstevel@tonic-gate 			if (devt == NODEV) {
33687c478bd9Sstevel@tonic-gate 				/*
33697c478bd9Sstevel@tonic-gate 				 * No default minor node, try the first one;
33707c478bd9Sstevel@tonic-gate 				 * else, assume 1-1 instance-minor mapping
33717c478bd9Sstevel@tonic-gate 				 */
33727c478bd9Sstevel@tonic-gate 				dmn = DEVI(parent)->devi_minor;
33737c478bd9Sstevel@tonic-gate 				if (dmn && ((dmn->type == DDM_MINOR) ||
33747c478bd9Sstevel@tonic-gate 				    (dmn->type == DDM_INTERNAL_PATH))) {
33757c478bd9Sstevel@tonic-gate 					devt = dmn->ddm_dev;
33767c478bd9Sstevel@tonic-gate 					spectype = dmn->ddm_spec_type;
33777c478bd9Sstevel@tonic-gate 				} else {
33787c478bd9Sstevel@tonic-gate 					devt = makedevice(
33797c478bd9Sstevel@tonic-gate 					    DEVI(parent)->devi_major,
33807c478bd9Sstevel@tonic-gate 					    ddi_get_instance(parent));
33817c478bd9Sstevel@tonic-gate 					spectype = S_IFCHR;
33827c478bd9Sstevel@tonic-gate 				}
33837c478bd9Sstevel@tonic-gate 			}
33847c478bd9Sstevel@tonic-gate 			mutex_exit(&(DEVI(parent)->devi_lock));
33857c478bd9Sstevel@tonic-gate 		}
33867c478bd9Sstevel@tonic-gate 		if (devtp)
33877c478bd9Sstevel@tonic-gate 			*devtp = devt;
33887c478bd9Sstevel@tonic-gate 		if (spectypep)
33897c478bd9Sstevel@tonic-gate 			*spectypep = spectype;
33907c478bd9Sstevel@tonic-gate 	}
33917c478bd9Sstevel@tonic-gate 
33927c478bd9Sstevel@tonic-gate 	pn_free(&pn);
33937c478bd9Sstevel@tonic-gate 	kmem_free(component, MAXNAMELEN);
33947c478bd9Sstevel@tonic-gate 	kmem_free(config_name, MAXNAMELEN);
33957c478bd9Sstevel@tonic-gate 
33967c478bd9Sstevel@tonic-gate 	/*
33977c478bd9Sstevel@tonic-gate 	 * If there is no error, return the appropriate parameters
33987c478bd9Sstevel@tonic-gate 	 */
33997c478bd9Sstevel@tonic-gate 	if (dipp != NULL)
34007c478bd9Sstevel@tonic-gate 		*dipp = parent;
34017c478bd9Sstevel@tonic-gate 	else {
34027c478bd9Sstevel@tonic-gate 		/*
34037c478bd9Sstevel@tonic-gate 		 * We should really keep the ref count to keep the node from
34047c478bd9Sstevel@tonic-gate 		 * detaching but ddi_pathname_to_dev_t() specifies a NULL dipp,
34057c478bd9Sstevel@tonic-gate 		 * so we have no way of passing back the held dip.  Not holding
34067c478bd9Sstevel@tonic-gate 		 * the dip allows detaches to occur - which can cause problems
34077c478bd9Sstevel@tonic-gate 		 * for subsystems which call ddi_pathname_to_dev_t (console).
34087c478bd9Sstevel@tonic-gate 		 *
34097c478bd9Sstevel@tonic-gate 		 * Instead of holding the dip, we place a ddi-no-autodetach
34107c478bd9Sstevel@tonic-gate 		 * property on the node to prevent auto detaching.
34117c478bd9Sstevel@tonic-gate 		 *
34127c478bd9Sstevel@tonic-gate 		 * The right fix is to remove ddi_pathname_to_dev_t and replace
34137c478bd9Sstevel@tonic-gate 		 * it, and all references, with a call that specifies a dipp.
34147c478bd9Sstevel@tonic-gate 		 * In addition, the callers of this new interfaces would then
34157c478bd9Sstevel@tonic-gate 		 * need to call ndi_rele_devi when the reference is complete.
34167c478bd9Sstevel@tonic-gate 		 */
34177c478bd9Sstevel@tonic-gate 		(void) ddi_prop_update_int(DDI_DEV_T_NONE, parent,
34187c478bd9Sstevel@tonic-gate 		    DDI_NO_AUTODETACH, 1);
34197c478bd9Sstevel@tonic-gate 		ndi_rele_devi(parent);
34207c478bd9Sstevel@tonic-gate 	}
34217c478bd9Sstevel@tonic-gate 
34227c478bd9Sstevel@tonic-gate 	return (0);
34237c478bd9Sstevel@tonic-gate }
34247c478bd9Sstevel@tonic-gate 
34257c478bd9Sstevel@tonic-gate /*
34267c478bd9Sstevel@tonic-gate  * Given the pathname of a device, return the dev_t of the corresponding
34277c478bd9Sstevel@tonic-gate  * device.  Returns NODEV on failure.
34287c478bd9Sstevel@tonic-gate  *
34297c478bd9Sstevel@tonic-gate  * Note that this call sets the DDI_NO_AUTODETACH property on the devinfo node.
34307c478bd9Sstevel@tonic-gate  */
34317c478bd9Sstevel@tonic-gate dev_t
34327c478bd9Sstevel@tonic-gate ddi_pathname_to_dev_t(char *pathname)
34337c478bd9Sstevel@tonic-gate {
34347c478bd9Sstevel@tonic-gate 	dev_t devt;
34357c478bd9Sstevel@tonic-gate 	int error;
34367c478bd9Sstevel@tonic-gate 
34377c478bd9Sstevel@tonic-gate 	error = resolve_pathname(pathname, NULL, &devt, NULL);
34387c478bd9Sstevel@tonic-gate 
34397c478bd9Sstevel@tonic-gate 	return (error ? NODEV : devt);
34407c478bd9Sstevel@tonic-gate }
34417c478bd9Sstevel@tonic-gate 
34427c478bd9Sstevel@tonic-gate /*
34437c478bd9Sstevel@tonic-gate  * Translate a prom pathname to kernel devfs pathname.
34447c478bd9Sstevel@tonic-gate  * Caller is assumed to allocate devfspath memory of
34457c478bd9Sstevel@tonic-gate  * size at least MAXPATHLEN
34467c478bd9Sstevel@tonic-gate  *
34477c478bd9Sstevel@tonic-gate  * The prom pathname may not include minor name, but
34487c478bd9Sstevel@tonic-gate  * devfs pathname has a minor name portion.
34497c478bd9Sstevel@tonic-gate  */
34507c478bd9Sstevel@tonic-gate int
34517c478bd9Sstevel@tonic-gate i_ddi_prompath_to_devfspath(char *prompath, char *devfspath)
34527c478bd9Sstevel@tonic-gate {
34537c478bd9Sstevel@tonic-gate 	dev_t		devt = (dev_t)NODEV;
34547c478bd9Sstevel@tonic-gate 	dev_info_t	*dip = NULL;
34557c478bd9Sstevel@tonic-gate 	char		*minor_name = NULL;
34567c478bd9Sstevel@tonic-gate 	int		spectype;
34577c478bd9Sstevel@tonic-gate 	int		error;
34587c478bd9Sstevel@tonic-gate 
34597c478bd9Sstevel@tonic-gate 	error = resolve_pathname(prompath, &dip, &devt, &spectype);
34607c478bd9Sstevel@tonic-gate 	if (error)
34617c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
34627c478bd9Sstevel@tonic-gate 	ASSERT(dip && devt != NODEV);
34637c478bd9Sstevel@tonic-gate 
34647c478bd9Sstevel@tonic-gate 	/*
34657c478bd9Sstevel@tonic-gate 	 * Get in-kernel devfs pathname
34667c478bd9Sstevel@tonic-gate 	 */
34677c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(dip, devfspath);
34687c478bd9Sstevel@tonic-gate 
34697c478bd9Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
34707c478bd9Sstevel@tonic-gate 	minor_name = i_ddi_devtspectype_to_minorname(dip, devt, spectype);
34717c478bd9Sstevel@tonic-gate 	if (minor_name) {
34727c478bd9Sstevel@tonic-gate 		(void) strcat(devfspath, ":");
34737c478bd9Sstevel@tonic-gate 		(void) strcat(devfspath, minor_name);
34747c478bd9Sstevel@tonic-gate 	} else {
34757c478bd9Sstevel@tonic-gate 		/*
34767c478bd9Sstevel@tonic-gate 		 * If minor_name is NULL, we have an alias minor node.
34777c478bd9Sstevel@tonic-gate 		 * So manufacture a path to the corresponding clone minor.
34787c478bd9Sstevel@tonic-gate 		 */
34797c478bd9Sstevel@tonic-gate 		(void) snprintf(devfspath, MAXPATHLEN, "%s:%s",
34807c478bd9Sstevel@tonic-gate 		    CLONE_PATH, ddi_driver_name(dip));
34817c478bd9Sstevel@tonic-gate 	}
34827c478bd9Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
34837c478bd9Sstevel@tonic-gate 
34847c478bd9Sstevel@tonic-gate 	/* release hold from resolve_pathname() */
34857c478bd9Sstevel@tonic-gate 	ndi_rele_devi(dip);
34867c478bd9Sstevel@tonic-gate 	return (0);
34877c478bd9Sstevel@tonic-gate }
34887c478bd9Sstevel@tonic-gate 
34897c478bd9Sstevel@tonic-gate /*
34907c478bd9Sstevel@tonic-gate  * Reset all the pure leaf drivers on the system at halt time
34917c478bd9Sstevel@tonic-gate  */
34927c478bd9Sstevel@tonic-gate static int
34937c478bd9Sstevel@tonic-gate reset_leaf_device(dev_info_t *dip, void *arg)
34947c478bd9Sstevel@tonic-gate {
34957c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(arg))
34967c478bd9Sstevel@tonic-gate 	struct dev_ops *ops;
34977c478bd9Sstevel@tonic-gate 
34987c478bd9Sstevel@tonic-gate 	/* if the device doesn't need to be reset then there's nothing to do */
34997c478bd9Sstevel@tonic-gate 	if (!DEVI_NEED_RESET(dip))
35007c478bd9Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
35017c478bd9Sstevel@tonic-gate 
35027c478bd9Sstevel@tonic-gate 	/*
35037c478bd9Sstevel@tonic-gate 	 * if the device isn't a char/block device or doesn't have a
35047c478bd9Sstevel@tonic-gate 	 * reset entry point then there's nothing to do.
35057c478bd9Sstevel@tonic-gate 	 */
35067c478bd9Sstevel@tonic-gate 	ops = ddi_get_driver(dip);
35077c478bd9Sstevel@tonic-gate 	if ((ops == NULL) || (ops->devo_cb_ops == NULL) ||
35087c478bd9Sstevel@tonic-gate 	    (ops->devo_reset == nodev) || (ops->devo_reset == nulldev) ||
35097c478bd9Sstevel@tonic-gate 	    (ops->devo_reset == NULL))
35107c478bd9Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
35117c478bd9Sstevel@tonic-gate 
35127c478bd9Sstevel@tonic-gate 	if (DEVI_IS_ATTACHING(dip) || DEVI_IS_DETACHING(dip)) {
35137c478bd9Sstevel@tonic-gate 		static char path[MAXPATHLEN];
35147c478bd9Sstevel@tonic-gate 
35157c478bd9Sstevel@tonic-gate 		/*
35167c478bd9Sstevel@tonic-gate 		 * bad news, this device has blocked in it's attach or
35177c478bd9Sstevel@tonic-gate 		 * detach routine, which means it not safe to call it's
35187c478bd9Sstevel@tonic-gate 		 * devo_reset() entry point.
35197c478bd9Sstevel@tonic-gate 		 */
35207c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "unable to reset device: %s",
35217c478bd9Sstevel@tonic-gate 		    ddi_pathname(dip, path));
35227c478bd9Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
35237c478bd9Sstevel@tonic-gate 	}
35247c478bd9Sstevel@tonic-gate 
35257c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_NOTE, "resetting %s%d\n",
35267c478bd9Sstevel@tonic-gate 		ddi_driver_name(dip), ddi_get_instance(dip)));
35277c478bd9Sstevel@tonic-gate 
35287c478bd9Sstevel@tonic-gate 	(void) devi_reset(dip, DDI_RESET_FORCE);
35297c478bd9Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
35307c478bd9Sstevel@tonic-gate }
35317c478bd9Sstevel@tonic-gate 
35327c478bd9Sstevel@tonic-gate void
35337c478bd9Sstevel@tonic-gate reset_leaves(void)
35347c478bd9Sstevel@tonic-gate {
35357c478bd9Sstevel@tonic-gate 	/*
35367c478bd9Sstevel@tonic-gate 	 * if we're reached here, the device tree better not be changing.
35377c478bd9Sstevel@tonic-gate 	 * so either devinfo_freeze better be set or we better be panicing.
35387c478bd9Sstevel@tonic-gate 	 */
35397c478bd9Sstevel@tonic-gate 	ASSERT(devinfo_freeze || panicstr);
35407c478bd9Sstevel@tonic-gate 
35417c478bd9Sstevel@tonic-gate 	(void) walk_devs(top_devinfo, reset_leaf_device, NULL, 0);
35427c478bd9Sstevel@tonic-gate }
35437c478bd9Sstevel@tonic-gate 
35447c478bd9Sstevel@tonic-gate /*
35457c478bd9Sstevel@tonic-gate  * devtree_freeze() must be called before reset_leaves() during a
35467c478bd9Sstevel@tonic-gate  * normal system shutdown.  It attempts to ensure that there are no
35477c478bd9Sstevel@tonic-gate  * outstanding attach or detach operations in progress when reset_leaves()
35487c478bd9Sstevel@tonic-gate  * is invoked.  It must be called before the system becomes single-threaded
35497c478bd9Sstevel@tonic-gate  * because device attach and detach are multi-threaded operations.  (note
35507c478bd9Sstevel@tonic-gate  * that during system shutdown the system doesn't actually become
35517c478bd9Sstevel@tonic-gate  * single-thread since other threads still exist, but the shutdown thread
35527c478bd9Sstevel@tonic-gate  * will disable preemption for itself, raise it's pil, and stop all the
35537c478bd9Sstevel@tonic-gate  * other cpus in the system there by effectively making the system
35547c478bd9Sstevel@tonic-gate  * single-threaded.)
35557c478bd9Sstevel@tonic-gate  */
35567c478bd9Sstevel@tonic-gate void
35577c478bd9Sstevel@tonic-gate devtree_freeze(void)
35587c478bd9Sstevel@tonic-gate {
35597c478bd9Sstevel@tonic-gate 	int delayed = 0;
35607c478bd9Sstevel@tonic-gate 
35617c478bd9Sstevel@tonic-gate 	/* if we're panicing then the device tree isn't going to be changing */
35627c478bd9Sstevel@tonic-gate 	if (panicstr)
35637c478bd9Sstevel@tonic-gate 		return;
35647c478bd9Sstevel@tonic-gate 
35657c478bd9Sstevel@tonic-gate 	/* stop all dev_info state changes in the device tree */
35667c478bd9Sstevel@tonic-gate 	devinfo_freeze = gethrtime();
35677c478bd9Sstevel@tonic-gate 
35687c478bd9Sstevel@tonic-gate 	/*
35697c478bd9Sstevel@tonic-gate 	 * if we're not panicing and there are on-going attach or detach
35707c478bd9Sstevel@tonic-gate 	 * operations, wait for up to 3 seconds for them to finish.  This
35717c478bd9Sstevel@tonic-gate 	 * is a randomly chosen interval but this should be ok because:
35727c478bd9Sstevel@tonic-gate 	 * - 3 seconds is very small relative to the deadman timer.
35737c478bd9Sstevel@tonic-gate 	 * - normal attach and detach operations should be very quick.
35747c478bd9Sstevel@tonic-gate 	 * - attach and detach operations are fairly rare.
35757c478bd9Sstevel@tonic-gate 	 */
35767c478bd9Sstevel@tonic-gate 	while (!panicstr && atomic_add_long_nv(&devinfo_attach_detach, 0) &&
35777c478bd9Sstevel@tonic-gate 	    (delayed < 3)) {
35787c478bd9Sstevel@tonic-gate 		delayed += 1;
35797c478bd9Sstevel@tonic-gate 
35807c478bd9Sstevel@tonic-gate 		/* do a sleeping wait for one second */
35817c478bd9Sstevel@tonic-gate 		ASSERT(!servicing_interrupt());
35827c478bd9Sstevel@tonic-gate 		delay(drv_usectohz(MICROSEC));
35837c478bd9Sstevel@tonic-gate 	}
35847c478bd9Sstevel@tonic-gate }
35857c478bd9Sstevel@tonic-gate 
35867c478bd9Sstevel@tonic-gate static int
35877c478bd9Sstevel@tonic-gate bind_dip(dev_info_t *dip, void *arg)
35887c478bd9Sstevel@tonic-gate {
35897c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(arg))
35907c478bd9Sstevel@tonic-gate 	if (i_ddi_node_state(dip) < DS_BOUND)
35917c478bd9Sstevel@tonic-gate 		(void) ndi_devi_bind_driver(dip, 0);
35927c478bd9Sstevel@tonic-gate 
35937c478bd9Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
35947c478bd9Sstevel@tonic-gate }
35957c478bd9Sstevel@tonic-gate 
35967c478bd9Sstevel@tonic-gate void
35977c478bd9Sstevel@tonic-gate i_ddi_bind_devs(void)
35987c478bd9Sstevel@tonic-gate {
35997c478bd9Sstevel@tonic-gate 	ddi_walk_devs(top_devinfo, bind_dip, (void *)NULL);
36007c478bd9Sstevel@tonic-gate }
36017c478bd9Sstevel@tonic-gate 
36027c478bd9Sstevel@tonic-gate static int
36037c478bd9Sstevel@tonic-gate unbind_children(dev_info_t *dip, void *arg)
36047c478bd9Sstevel@tonic-gate {
36057c478bd9Sstevel@tonic-gate 	int circ;
36067c478bd9Sstevel@tonic-gate 	dev_info_t *cdip;
36077c478bd9Sstevel@tonic-gate 	major_t major = (major_t)(uintptr_t)arg;
36087c478bd9Sstevel@tonic-gate 
36097c478bd9Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
36107c478bd9Sstevel@tonic-gate 	cdip = ddi_get_child(dip);
36117c478bd9Sstevel@tonic-gate 	/*
36127c478bd9Sstevel@tonic-gate 	 * We are called either from rem_drv or update_drv.
36137c478bd9Sstevel@tonic-gate 	 * In both cases, we unbind persistent nodes and destroy
36147c478bd9Sstevel@tonic-gate 	 * .conf nodes. In the case of rem_drv, this will be the
36157c478bd9Sstevel@tonic-gate 	 * final state. In the case of update_drv, i_ddi_bind_devs()
36167c478bd9Sstevel@tonic-gate 	 * will be invoked later to reenumerate (new) driver.conf
36177c478bd9Sstevel@tonic-gate 	 * rebind persistent nodes.
36187c478bd9Sstevel@tonic-gate 	 */
36197c478bd9Sstevel@tonic-gate 	while (cdip) {
36207c478bd9Sstevel@tonic-gate 		dev_info_t *next = ddi_get_next_sibling(cdip);
36217c478bd9Sstevel@tonic-gate 		if ((i_ddi_node_state(cdip) > DS_INITIALIZED) ||
36227c478bd9Sstevel@tonic-gate 		    (ddi_driver_major(cdip) != major)) {
36237c478bd9Sstevel@tonic-gate 			cdip = next;
36247c478bd9Sstevel@tonic-gate 			continue;
36257c478bd9Sstevel@tonic-gate 		}
36267c478bd9Sstevel@tonic-gate 		(void) ndi_devi_unbind_driver(cdip);
36277c478bd9Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(cdip) == 0)
36287c478bd9Sstevel@tonic-gate 			(void) ddi_remove_child(cdip, 0);
36297c478bd9Sstevel@tonic-gate 		cdip = next;
36307c478bd9Sstevel@tonic-gate 	}
36317c478bd9Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
36327c478bd9Sstevel@tonic-gate 
36337c478bd9Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
36347c478bd9Sstevel@tonic-gate }
36357c478bd9Sstevel@tonic-gate 
36367c478bd9Sstevel@tonic-gate void
36377c478bd9Sstevel@tonic-gate i_ddi_unbind_devs(major_t major)
36387c478bd9Sstevel@tonic-gate {
36397c478bd9Sstevel@tonic-gate 	ddi_walk_devs(top_devinfo, unbind_children, (void *)(uintptr_t)major);
36407c478bd9Sstevel@tonic-gate }
36417c478bd9Sstevel@tonic-gate 
36427c478bd9Sstevel@tonic-gate /*
36437c478bd9Sstevel@tonic-gate  * I/O Hotplug control
36447c478bd9Sstevel@tonic-gate  */
36457c478bd9Sstevel@tonic-gate 
36467c478bd9Sstevel@tonic-gate /*
36477c478bd9Sstevel@tonic-gate  * create and attach a dev_info node from a .conf file spec
36487c478bd9Sstevel@tonic-gate  */
36497c478bd9Sstevel@tonic-gate static void
36507c478bd9Sstevel@tonic-gate init_spec_child(dev_info_t *pdip, struct hwc_spec *specp, uint_t flags)
36517c478bd9Sstevel@tonic-gate {
36527c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags))
36537c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
36547c478bd9Sstevel@tonic-gate 	char *node_name;
36557c478bd9Sstevel@tonic-gate 
36567c478bd9Sstevel@tonic-gate 	if (((node_name = specp->hwc_devi_name) == NULL) ||
36577c478bd9Sstevel@tonic-gate 	    (ddi_name_to_major(node_name) == (major_t)-1)) {
36587c478bd9Sstevel@tonic-gate 		char *tmp = node_name;
36597c478bd9Sstevel@tonic-gate 		if (tmp == NULL)
36607c478bd9Sstevel@tonic-gate 			tmp = "<none>";
36617c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT,
36627c478bd9Sstevel@tonic-gate 		    "init_spec_child: parent=%s, bad spec (%s)\n",
36637c478bd9Sstevel@tonic-gate 		    ddi_node_name(pdip), tmp);
36647c478bd9Sstevel@tonic-gate 		return;
36657c478bd9Sstevel@tonic-gate 	}
36667c478bd9Sstevel@tonic-gate 
3667fa9e4066Sahrens 	dip = i_ddi_alloc_node(pdip, node_name, (pnode_t)DEVI_PSEUDO_NODEID,
36687c478bd9Sstevel@tonic-gate 	    -1, specp->hwc_devi_sys_prop_ptr, KM_SLEEP);
36697c478bd9Sstevel@tonic-gate 
36707c478bd9Sstevel@tonic-gate 	if (dip == NULL)
36717c478bd9Sstevel@tonic-gate 		return;
36727c478bd9Sstevel@tonic-gate 
36737c478bd9Sstevel@tonic-gate 	if (ddi_initchild(pdip, dip) != DDI_SUCCESS)
36747c478bd9Sstevel@tonic-gate 		(void) ddi_remove_child(dip, 0);
36757c478bd9Sstevel@tonic-gate }
36767c478bd9Sstevel@tonic-gate 
36777c478bd9Sstevel@tonic-gate /*
36787c478bd9Sstevel@tonic-gate  * Lookup hwc specs from hash tables and make children from the spec
36797c478bd9Sstevel@tonic-gate  * Because some .conf children are "merge" nodes, we also initialize
36807c478bd9Sstevel@tonic-gate  * .conf children to merge properties onto hardware nodes.
36817c478bd9Sstevel@tonic-gate  *
36827c478bd9Sstevel@tonic-gate  * The pdip must be held busy.
36837c478bd9Sstevel@tonic-gate  */
36847c478bd9Sstevel@tonic-gate int
36857c478bd9Sstevel@tonic-gate i_ndi_make_spec_children(dev_info_t *pdip, uint_t flags)
36867c478bd9Sstevel@tonic-gate {
36877c478bd9Sstevel@tonic-gate 	extern struct hwc_spec *hwc_get_child_spec(dev_info_t *, major_t);
36886a41d557Scth 	int			circ;
36897c478bd9Sstevel@tonic-gate 	struct hwc_spec		*list, *spec;
36907c478bd9Sstevel@tonic-gate 
36916a41d557Scth 	ndi_devi_enter(pdip, &circ);
36926a41d557Scth 	if (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN) {
36936a41d557Scth 		ndi_devi_exit(pdip, circ);
36947c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
36956a41d557Scth 	}
36967c478bd9Sstevel@tonic-gate 
36977c478bd9Sstevel@tonic-gate 	list = hwc_get_child_spec(pdip, (major_t)-1);
36987c478bd9Sstevel@tonic-gate 	for (spec = list; spec != NULL; spec = spec->hwc_next) {
36997c478bd9Sstevel@tonic-gate 		init_spec_child(pdip, spec, flags);
37007c478bd9Sstevel@tonic-gate 	}
37017c478bd9Sstevel@tonic-gate 	hwc_free_spec_list(list);
37027c478bd9Sstevel@tonic-gate 
37037c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(pdip)->devi_lock);
37047c478bd9Sstevel@tonic-gate 	DEVI(pdip)->devi_flags |= DEVI_MADE_CHILDREN;
37057c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(pdip)->devi_lock);
37066a41d557Scth 	ndi_devi_exit(pdip, circ);
37077c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
37087c478bd9Sstevel@tonic-gate }
37097c478bd9Sstevel@tonic-gate 
37107c478bd9Sstevel@tonic-gate /*
37117c478bd9Sstevel@tonic-gate  * Run initchild on all child nodes such that instance assignment
37127c478bd9Sstevel@tonic-gate  * for multiport network cards are contiguous.
37137c478bd9Sstevel@tonic-gate  *
37147c478bd9Sstevel@tonic-gate  * The pdip must be held busy.
37157c478bd9Sstevel@tonic-gate  */
37167c478bd9Sstevel@tonic-gate static void
37177c478bd9Sstevel@tonic-gate i_ndi_init_hw_children(dev_info_t *pdip, uint_t flags)
37187c478bd9Sstevel@tonic-gate {
37197c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
37207c478bd9Sstevel@tonic-gate 
37217c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
37227c478bd9Sstevel@tonic-gate 
37237c478bd9Sstevel@tonic-gate 	/* contiguous instance assignment */
37247c478bd9Sstevel@tonic-gate 	e_ddi_enter_instance();
37257c478bd9Sstevel@tonic-gate 	dip = ddi_get_child(pdip);
37267c478bd9Sstevel@tonic-gate 	while (dip) {
37277c478bd9Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(dip))
37287c478bd9Sstevel@tonic-gate 			(void) i_ndi_config_node(dip, DS_INITIALIZED, flags);
37297c478bd9Sstevel@tonic-gate 		dip = ddi_get_next_sibling(dip);
37307c478bd9Sstevel@tonic-gate 	}
37317c478bd9Sstevel@tonic-gate 	e_ddi_exit_instance();
37327c478bd9Sstevel@tonic-gate }
37337c478bd9Sstevel@tonic-gate 
37347c478bd9Sstevel@tonic-gate /*
37357c478bd9Sstevel@tonic-gate  * report device status
37367c478bd9Sstevel@tonic-gate  */
37377c478bd9Sstevel@tonic-gate static void
37387c478bd9Sstevel@tonic-gate i_ndi_devi_report_status_change(dev_info_t *dip, char *path)
37397c478bd9Sstevel@tonic-gate {
37407c478bd9Sstevel@tonic-gate 	char *status;
37417c478bd9Sstevel@tonic-gate 
37427c478bd9Sstevel@tonic-gate 	if (!DEVI_NEED_REPORT(dip) ||
37437c478bd9Sstevel@tonic-gate 	    (i_ddi_node_state(dip) < DS_INITIALIZED)) {
37447c478bd9Sstevel@tonic-gate 		return;
37457c478bd9Sstevel@tonic-gate 	}
37467c478bd9Sstevel@tonic-gate 
37477c478bd9Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
37487c478bd9Sstevel@tonic-gate 		status = "offline";
37497c478bd9Sstevel@tonic-gate 	} else if (DEVI_IS_DEVICE_DOWN(dip)) {
37507c478bd9Sstevel@tonic-gate 		status = "down";
37517c478bd9Sstevel@tonic-gate 	} else if (DEVI_IS_BUS_QUIESCED(dip)) {
37527c478bd9Sstevel@tonic-gate 		status = "quiesced";
37537c478bd9Sstevel@tonic-gate 	} else if (DEVI_IS_BUS_DOWN(dip)) {
37547c478bd9Sstevel@tonic-gate 		status = "down";
37557c478bd9Sstevel@tonic-gate 	} else if (i_ddi_node_state(dip) == DS_READY) {
37567c478bd9Sstevel@tonic-gate 		status = "online";
37577c478bd9Sstevel@tonic-gate 	} else {
37587c478bd9Sstevel@tonic-gate 		status = "unknown";
37597c478bd9Sstevel@tonic-gate 	}
37607c478bd9Sstevel@tonic-gate 
37617c478bd9Sstevel@tonic-gate 	if (path == NULL) {
37627c478bd9Sstevel@tonic-gate 		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
37637c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "?%s (%s%d) %s\n",
37647c478bd9Sstevel@tonic-gate 			ddi_pathname(dip, path), ddi_driver_name(dip),
37657c478bd9Sstevel@tonic-gate 			ddi_get_instance(dip), status);
37667c478bd9Sstevel@tonic-gate 		kmem_free(path, MAXPATHLEN);
37677c478bd9Sstevel@tonic-gate 	} else {
37687c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "?%s (%s%d) %s\n",
37697c478bd9Sstevel@tonic-gate 			path, ddi_driver_name(dip),
37707c478bd9Sstevel@tonic-gate 			ddi_get_instance(dip), status);
37717c478bd9Sstevel@tonic-gate 	}
37727c478bd9Sstevel@tonic-gate 
377316747f41Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
37747c478bd9Sstevel@tonic-gate 	DEVI_REPORT_DONE(dip);
377516747f41Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
37767c478bd9Sstevel@tonic-gate }
37777c478bd9Sstevel@tonic-gate 
37787c478bd9Sstevel@tonic-gate /*
37797c478bd9Sstevel@tonic-gate  * log a notification that a dev_info node has been configured.
37807c478bd9Sstevel@tonic-gate  */
37817c478bd9Sstevel@tonic-gate static int
37827c478bd9Sstevel@tonic-gate i_log_devfs_add_devinfo(dev_info_t *dip, uint_t flags)
37837c478bd9Sstevel@tonic-gate {
37847c478bd9Sstevel@tonic-gate 	int se_err;
37857c478bd9Sstevel@tonic-gate 	char *pathname;
37867c478bd9Sstevel@tonic-gate 	sysevent_t *ev;
37877c478bd9Sstevel@tonic-gate 	sysevent_id_t eid;
37887c478bd9Sstevel@tonic-gate 	sysevent_value_t se_val;
37897c478bd9Sstevel@tonic-gate 	sysevent_attr_list_t *ev_attr_list = NULL;
37907c478bd9Sstevel@tonic-gate 	char *class_name;
37917c478bd9Sstevel@tonic-gate 	int no_transport = 0;
37927c478bd9Sstevel@tonic-gate 
37937c478bd9Sstevel@tonic-gate 	ASSERT(dip);
37947c478bd9Sstevel@tonic-gate 
37957c478bd9Sstevel@tonic-gate 	/*
37967c478bd9Sstevel@tonic-gate 	 * Invalidate the devinfo snapshot cache
37977c478bd9Sstevel@tonic-gate 	 */
37987c478bd9Sstevel@tonic-gate 	i_ddi_di_cache_invalidate(KM_SLEEP);
37997c478bd9Sstevel@tonic-gate 
38007c478bd9Sstevel@tonic-gate 	/* do not generate ESC_DEVFS_DEVI_ADD event during boot */
38017c478bd9Sstevel@tonic-gate 	if (!i_ddi_io_initialized())
38027c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
38037c478bd9Sstevel@tonic-gate 
38047c478bd9Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_ADD, EP_DDI, SE_SLEEP);
38057c478bd9Sstevel@tonic-gate 
38067c478bd9Sstevel@tonic-gate 	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
38077c478bd9Sstevel@tonic-gate 
38087c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(dip, pathname);
38097c478bd9Sstevel@tonic-gate 	ASSERT(strlen(pathname));
38107c478bd9Sstevel@tonic-gate 
38117c478bd9Sstevel@tonic-gate 	se_val.value_type = SE_DATA_TYPE_STRING;
38127c478bd9Sstevel@tonic-gate 	se_val.value.sv_string = pathname;
38137c478bd9Sstevel@tonic-gate 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
38147c478bd9Sstevel@tonic-gate 	    &se_val, SE_SLEEP) != 0) {
38157c478bd9Sstevel@tonic-gate 		goto fail;
38167c478bd9Sstevel@tonic-gate 	}
38177c478bd9Sstevel@tonic-gate 
38187c478bd9Sstevel@tonic-gate 	/* add the device class attribute */
38197c478bd9Sstevel@tonic-gate 	if ((class_name = i_ddi_devi_class(dip)) != NULL) {
38207c478bd9Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_STRING;
38217c478bd9Sstevel@tonic-gate 		se_val.value.sv_string = class_name;
38227c478bd9Sstevel@tonic-gate 
38237c478bd9Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
38247c478bd9Sstevel@tonic-gate 		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
38257c478bd9Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
38267c478bd9Sstevel@tonic-gate 			goto fail;
38277c478bd9Sstevel@tonic-gate 		}
38287c478bd9Sstevel@tonic-gate 	}
38297c478bd9Sstevel@tonic-gate 
38307c478bd9Sstevel@tonic-gate 	/*
38317c478bd9Sstevel@tonic-gate 	 * must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
38327c478bd9Sstevel@tonic-gate 	 * in which case the branch event will be logged by the caller
38337c478bd9Sstevel@tonic-gate 	 * after the entire branch has been configured.
38347c478bd9Sstevel@tonic-gate 	 */
38357c478bd9Sstevel@tonic-gate 	if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
38367c478bd9Sstevel@tonic-gate 		/*
38377c478bd9Sstevel@tonic-gate 		 * Instead of logging a separate branch event just add
38387c478bd9Sstevel@tonic-gate 		 * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
38397c478bd9Sstevel@tonic-gate 		 * generate a EC_DEV_BRANCH event.
38407c478bd9Sstevel@tonic-gate 		 */
38417c478bd9Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_INT32;
38427c478bd9Sstevel@tonic-gate 		se_val.value.sv_int32 = 1;
38437c478bd9Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
38447c478bd9Sstevel@tonic-gate 		    DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
38457c478bd9Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
38467c478bd9Sstevel@tonic-gate 			goto fail;
38477c478bd9Sstevel@tonic-gate 		}
38487c478bd9Sstevel@tonic-gate 	}
38497c478bd9Sstevel@tonic-gate 
38507c478bd9Sstevel@tonic-gate 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
38517c478bd9Sstevel@tonic-gate 		sysevent_free_attr(ev_attr_list);
38527c478bd9Sstevel@tonic-gate 		goto fail;
38537c478bd9Sstevel@tonic-gate 	}
38547c478bd9Sstevel@tonic-gate 
38557c478bd9Sstevel@tonic-gate 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
38567c478bd9Sstevel@tonic-gate 		if (se_err == SE_NO_TRANSPORT)
38577c478bd9Sstevel@tonic-gate 			no_transport = 1;
38587c478bd9Sstevel@tonic-gate 		goto fail;
38597c478bd9Sstevel@tonic-gate 	}
38607c478bd9Sstevel@tonic-gate 
38617c478bd9Sstevel@tonic-gate 	sysevent_free(ev);
38627c478bd9Sstevel@tonic-gate 	kmem_free(pathname, MAXPATHLEN);
38637c478bd9Sstevel@tonic-gate 
38647c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
38657c478bd9Sstevel@tonic-gate 
38667c478bd9Sstevel@tonic-gate fail:
38677c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_ADD event for %s%s",
38687c478bd9Sstevel@tonic-gate 	    pathname, (no_transport) ? " (syseventd not responding)" : "");
38697c478bd9Sstevel@tonic-gate 
38707c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "/dev may not be current for driver %s. "
38717c478bd9Sstevel@tonic-gate 	    "Run devfsadm -i %s",
38727c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_driver_name(dip));
38737c478bd9Sstevel@tonic-gate 
38747c478bd9Sstevel@tonic-gate 	sysevent_free(ev);
38757c478bd9Sstevel@tonic-gate 	kmem_free(pathname, MAXPATHLEN);
38767c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
38777c478bd9Sstevel@tonic-gate }
38787c478bd9Sstevel@tonic-gate 
38797c478bd9Sstevel@tonic-gate /*
38807c478bd9Sstevel@tonic-gate  * log a notification that a dev_info node has been unconfigured.
38817c478bd9Sstevel@tonic-gate  */
38827c478bd9Sstevel@tonic-gate static int
38837c478bd9Sstevel@tonic-gate i_log_devfs_remove_devinfo(char *pathname, char *class_name, char *driver_name,
38847c478bd9Sstevel@tonic-gate     int instance, uint_t flags)
38857c478bd9Sstevel@tonic-gate {
38867c478bd9Sstevel@tonic-gate 	sysevent_t *ev;
38877c478bd9Sstevel@tonic-gate 	sysevent_id_t eid;
38887c478bd9Sstevel@tonic-gate 	sysevent_value_t se_val;
38897c478bd9Sstevel@tonic-gate 	sysevent_attr_list_t *ev_attr_list = NULL;
38907c478bd9Sstevel@tonic-gate 	int se_err;
38917c478bd9Sstevel@tonic-gate 	int no_transport = 0;
38927c478bd9Sstevel@tonic-gate 
38937c478bd9Sstevel@tonic-gate 	i_ddi_di_cache_invalidate(KM_SLEEP);
38947c478bd9Sstevel@tonic-gate 
38957c478bd9Sstevel@tonic-gate 	if (!i_ddi_io_initialized())
38967c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
38977c478bd9Sstevel@tonic-gate 
38987c478bd9Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_REMOVE, EP_DDI, SE_SLEEP);
38997c478bd9Sstevel@tonic-gate 
39007c478bd9Sstevel@tonic-gate 	se_val.value_type = SE_DATA_TYPE_STRING;
39017c478bd9Sstevel@tonic-gate 	se_val.value.sv_string = pathname;
39027c478bd9Sstevel@tonic-gate 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
39037c478bd9Sstevel@tonic-gate 	    &se_val, SE_SLEEP) != 0) {
39047c478bd9Sstevel@tonic-gate 		goto fail;
39057c478bd9Sstevel@tonic-gate 	}
39067c478bd9Sstevel@tonic-gate 
39077c478bd9Sstevel@tonic-gate 	if (class_name) {
39087c478bd9Sstevel@tonic-gate 		/* add the device class, driver name and instance attributes */
39097c478bd9Sstevel@tonic-gate 
39107c478bd9Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_STRING;
39117c478bd9Sstevel@tonic-gate 		se_val.value.sv_string = class_name;
39127c478bd9Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
39137c478bd9Sstevel@tonic-gate 		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
39147c478bd9Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
39157c478bd9Sstevel@tonic-gate 			goto fail;
39167c478bd9Sstevel@tonic-gate 		}
39177c478bd9Sstevel@tonic-gate 
39187c478bd9Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_STRING;
39197c478bd9Sstevel@tonic-gate 		se_val.value.sv_string = driver_name;
39207c478bd9Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
39217c478bd9Sstevel@tonic-gate 		    DEVFS_DRIVER_NAME, &se_val, SE_SLEEP) != 0) {
39227c478bd9Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
39237c478bd9Sstevel@tonic-gate 			goto fail;
39247c478bd9Sstevel@tonic-gate 		}
39257c478bd9Sstevel@tonic-gate 
39267c478bd9Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_INT32;
39277c478bd9Sstevel@tonic-gate 		se_val.value.sv_int32 = instance;
39287c478bd9Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
39297c478bd9Sstevel@tonic-gate 		    DEVFS_INSTANCE, &se_val, SE_SLEEP) != 0) {
39307c478bd9Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
39317c478bd9Sstevel@tonic-gate 			goto fail;
39327c478bd9Sstevel@tonic-gate 		}
39337c478bd9Sstevel@tonic-gate 	}
39347c478bd9Sstevel@tonic-gate 
39357c478bd9Sstevel@tonic-gate 	/*
39367c478bd9Sstevel@tonic-gate 	 * must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
39377c478bd9Sstevel@tonic-gate 	 * in which case the branch event will be logged by the caller
39387c478bd9Sstevel@tonic-gate 	 * after the entire branch has been unconfigured.
39397c478bd9Sstevel@tonic-gate 	 */
39407c478bd9Sstevel@tonic-gate 	if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
39417c478bd9Sstevel@tonic-gate 		/*
39427c478bd9Sstevel@tonic-gate 		 * Instead of logging a separate branch event just add
39437c478bd9Sstevel@tonic-gate 		 * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
39447c478bd9Sstevel@tonic-gate 		 * generate a EC_DEV_BRANCH event.
39457c478bd9Sstevel@tonic-gate 		 */
39467c478bd9Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_INT32;
39477c478bd9Sstevel@tonic-gate 		se_val.value.sv_int32 = 1;
39487c478bd9Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
39497c478bd9Sstevel@tonic-gate 		    DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
39507c478bd9Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
39517c478bd9Sstevel@tonic-gate 			goto fail;
39527c478bd9Sstevel@tonic-gate 		}
39537c478bd9Sstevel@tonic-gate 	}
39547c478bd9Sstevel@tonic-gate 
39557c478bd9Sstevel@tonic-gate 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
39567c478bd9Sstevel@tonic-gate 		sysevent_free_attr(ev_attr_list);
39577c478bd9Sstevel@tonic-gate 		goto fail;
39587c478bd9Sstevel@tonic-gate 	}
39597c478bd9Sstevel@tonic-gate 
39607c478bd9Sstevel@tonic-gate 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
39617c478bd9Sstevel@tonic-gate 		if (se_err == SE_NO_TRANSPORT)
39627c478bd9Sstevel@tonic-gate 			no_transport = 1;
39637c478bd9Sstevel@tonic-gate 		goto fail;
39647c478bd9Sstevel@tonic-gate 	}
39657c478bd9Sstevel@tonic-gate 
39667c478bd9Sstevel@tonic-gate 	sysevent_free(ev);
39677c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
39687c478bd9Sstevel@tonic-gate 
39697c478bd9Sstevel@tonic-gate fail:
39707c478bd9Sstevel@tonic-gate 	sysevent_free(ev);
39717c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_REMOVE event for %s%s",
39727c478bd9Sstevel@tonic-gate 	    pathname, (no_transport) ? " (syseventd not responding)" : "");
39737c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
39747c478bd9Sstevel@tonic-gate }
39757c478bd9Sstevel@tonic-gate 
39767c478bd9Sstevel@tonic-gate /*
39777c478bd9Sstevel@tonic-gate  * log an event that a dev_info branch has been configured or unconfigured.
39787c478bd9Sstevel@tonic-gate  */
39797c478bd9Sstevel@tonic-gate static int
39807c478bd9Sstevel@tonic-gate i_log_devfs_branch(char *node_path, char *subclass)
39817c478bd9Sstevel@tonic-gate {
39827c478bd9Sstevel@tonic-gate 	int se_err;
39837c478bd9Sstevel@tonic-gate 	sysevent_t *ev;
39847c478bd9Sstevel@tonic-gate 	sysevent_id_t eid;
39857c478bd9Sstevel@tonic-gate 	sysevent_value_t se_val;
39867c478bd9Sstevel@tonic-gate 	sysevent_attr_list_t *ev_attr_list = NULL;
39877c478bd9Sstevel@tonic-gate 	int no_transport = 0;
39887c478bd9Sstevel@tonic-gate 
39897c478bd9Sstevel@tonic-gate 	/* do not generate the event during boot */
39907c478bd9Sstevel@tonic-gate 	if (!i_ddi_io_initialized())
39917c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
39927c478bd9Sstevel@tonic-gate 
39937c478bd9Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, subclass, EP_DDI, SE_SLEEP);
39947c478bd9Sstevel@tonic-gate 
39957c478bd9Sstevel@tonic-gate 	se_val.value_type = SE_DATA_TYPE_STRING;
39967c478bd9Sstevel@tonic-gate 	se_val.value.sv_string = node_path;
39977c478bd9Sstevel@tonic-gate 
39987c478bd9Sstevel@tonic-gate 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
39997c478bd9Sstevel@tonic-gate 	    &se_val, SE_SLEEP) != 0) {
40007c478bd9Sstevel@tonic-gate 		goto fail;
40017c478bd9Sstevel@tonic-gate 	}
40027c478bd9Sstevel@tonic-gate 
40037c478bd9Sstevel@tonic-gate 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
40047c478bd9Sstevel@tonic-gate 		sysevent_free_attr(ev_attr_list);
40057c478bd9Sstevel@tonic-gate 		goto fail;
40067c478bd9Sstevel@tonic-gate 	}
40077c478bd9Sstevel@tonic-gate 
40087c478bd9Sstevel@tonic-gate 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
40097c478bd9Sstevel@tonic-gate 		if (se_err == SE_NO_TRANSPORT)
40107c478bd9Sstevel@tonic-gate 			no_transport = 1;
40117c478bd9Sstevel@tonic-gate 		goto fail;
40127c478bd9Sstevel@tonic-gate 	}
40137c478bd9Sstevel@tonic-gate 
40147c478bd9Sstevel@tonic-gate 	sysevent_free(ev);
40157c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
40167c478bd9Sstevel@tonic-gate 
40177c478bd9Sstevel@tonic-gate fail:
40187c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "failed to log %s branch event for %s%s",
40197c478bd9Sstevel@tonic-gate 	    subclass, node_path,
40207c478bd9Sstevel@tonic-gate 	    (no_transport) ? " (syseventd not responding)" : "");
40217c478bd9Sstevel@tonic-gate 
40227c478bd9Sstevel@tonic-gate 	sysevent_free(ev);
40237c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
40247c478bd9Sstevel@tonic-gate }
40257c478bd9Sstevel@tonic-gate 
40267c478bd9Sstevel@tonic-gate /*
40277c478bd9Sstevel@tonic-gate  * log an event that a dev_info tree branch has been configured.
40287c478bd9Sstevel@tonic-gate  */
40297c478bd9Sstevel@tonic-gate static int
40307c478bd9Sstevel@tonic-gate i_log_devfs_branch_add(dev_info_t *dip)
40317c478bd9Sstevel@tonic-gate {
40327c478bd9Sstevel@tonic-gate 	char *node_path;
40337c478bd9Sstevel@tonic-gate 	int rv;
40347c478bd9Sstevel@tonic-gate 
40357c478bd9Sstevel@tonic-gate 	node_path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
40367c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(dip, node_path);
40377c478bd9Sstevel@tonic-gate 	rv = i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_ADD);
40387c478bd9Sstevel@tonic-gate 	kmem_free(node_path, MAXPATHLEN);
40397c478bd9Sstevel@tonic-gate 
40407c478bd9Sstevel@tonic-gate 	return (rv);
40417c478bd9Sstevel@tonic-gate }
40427c478bd9Sstevel@tonic-gate 
40437c478bd9Sstevel@tonic-gate /*
40447c478bd9Sstevel@tonic-gate  * log an event that a dev_info tree branch has been unconfigured.
40457c478bd9Sstevel@tonic-gate  */
40467c478bd9Sstevel@tonic-gate static int
40477c478bd9Sstevel@tonic-gate i_log_devfs_branch_remove(char *node_path)
40487c478bd9Sstevel@tonic-gate {
40497c478bd9Sstevel@tonic-gate 	return (i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_REMOVE));
40507c478bd9Sstevel@tonic-gate }
40517c478bd9Sstevel@tonic-gate 
40527c478bd9Sstevel@tonic-gate /*
40537c478bd9Sstevel@tonic-gate  * enqueue the dip's deviname on the branch event queue.
40547c478bd9Sstevel@tonic-gate  */
40557c478bd9Sstevel@tonic-gate static struct brevq_node *
40567c478bd9Sstevel@tonic-gate brevq_enqueue(struct brevq_node **brevqp, dev_info_t *dip,
40577c478bd9Sstevel@tonic-gate     struct brevq_node *child)
40587c478bd9Sstevel@tonic-gate {
40597c478bd9Sstevel@tonic-gate 	struct brevq_node *brn;
40607c478bd9Sstevel@tonic-gate 	char *deviname;
40617c478bd9Sstevel@tonic-gate 
40627c478bd9Sstevel@tonic-gate 	deviname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
40637c478bd9Sstevel@tonic-gate 	(void) ddi_deviname(dip, deviname);
40647c478bd9Sstevel@tonic-gate 
40657c478bd9Sstevel@tonic-gate 	brn = kmem_zalloc(sizeof (*brn), KM_SLEEP);
4066*245c82d9Scth 	brn->brn_deviname = i_ddi_strdup(deviname, KM_SLEEP);
40677c478bd9Sstevel@tonic-gate 	kmem_free(deviname, MAXNAMELEN);
4068*245c82d9Scth 	brn->brn_child = child;
4069*245c82d9Scth 	brn->brn_sibling = *brevqp;
40707c478bd9Sstevel@tonic-gate 	*brevqp = brn;
40717c478bd9Sstevel@tonic-gate 
40727c478bd9Sstevel@tonic-gate 	return (brn);
40737c478bd9Sstevel@tonic-gate }
40747c478bd9Sstevel@tonic-gate 
40757c478bd9Sstevel@tonic-gate /*
40767c478bd9Sstevel@tonic-gate  * free the memory allocated for the elements on the branch event queue.
40777c478bd9Sstevel@tonic-gate  */
40787c478bd9Sstevel@tonic-gate static void
40797c478bd9Sstevel@tonic-gate free_brevq(struct brevq_node *brevq)
40807c478bd9Sstevel@tonic-gate {
40817c478bd9Sstevel@tonic-gate 	struct brevq_node *brn, *next_brn;
40827c478bd9Sstevel@tonic-gate 
40837c478bd9Sstevel@tonic-gate 	for (brn = brevq; brn != NULL; brn = next_brn) {
4084*245c82d9Scth 		next_brn = brn->brn_sibling;
4085*245c82d9Scth 		ASSERT(brn->brn_child == NULL);
4086*245c82d9Scth 		kmem_free(brn->brn_deviname, strlen(brn->brn_deviname) + 1);
40877c478bd9Sstevel@tonic-gate 		kmem_free(brn, sizeof (*brn));
40887c478bd9Sstevel@tonic-gate 	}
40897c478bd9Sstevel@tonic-gate }
40907c478bd9Sstevel@tonic-gate 
40917c478bd9Sstevel@tonic-gate /*
40927c478bd9Sstevel@tonic-gate  * log the events queued up on the branch event queue and free the
40937c478bd9Sstevel@tonic-gate  * associated memory.
40947c478bd9Sstevel@tonic-gate  *
40957c478bd9Sstevel@tonic-gate  * node_path must have been allocated with at least MAXPATHLEN bytes.
40967c478bd9Sstevel@tonic-gate  */
40977c478bd9Sstevel@tonic-gate static void
40987c478bd9Sstevel@tonic-gate log_and_free_brevq(char *node_path, struct brevq_node *brevq)
40997c478bd9Sstevel@tonic-gate {
41007c478bd9Sstevel@tonic-gate 	struct brevq_node *brn;
41017c478bd9Sstevel@tonic-gate 	char *p;
41027c478bd9Sstevel@tonic-gate 
41037c478bd9Sstevel@tonic-gate 	p = node_path + strlen(node_path);
4104*245c82d9Scth 	for (brn = brevq; brn != NULL; brn = brn->brn_sibling) {
4105*245c82d9Scth 		(void) strcpy(p, brn->brn_deviname);
41067c478bd9Sstevel@tonic-gate 		(void) i_log_devfs_branch_remove(node_path);
41077c478bd9Sstevel@tonic-gate 	}
41087c478bd9Sstevel@tonic-gate 	*p = '\0';
41097c478bd9Sstevel@tonic-gate 
41107c478bd9Sstevel@tonic-gate 	free_brevq(brevq);
41117c478bd9Sstevel@tonic-gate }
41127c478bd9Sstevel@tonic-gate 
41137c478bd9Sstevel@tonic-gate /*
41147c478bd9Sstevel@tonic-gate  * log the events queued up on the branch event queue and free the
41157c478bd9Sstevel@tonic-gate  * associated memory. Same as the previous function but operates on dip.
41167c478bd9Sstevel@tonic-gate  */
41177c478bd9Sstevel@tonic-gate static void
41187c478bd9Sstevel@tonic-gate log_and_free_brevq_dip(dev_info_t *dip, struct brevq_node *brevq)
41197c478bd9Sstevel@tonic-gate {
41207c478bd9Sstevel@tonic-gate 	char *path;
41217c478bd9Sstevel@tonic-gate 
41227c478bd9Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
41237c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(dip, path);
41247c478bd9Sstevel@tonic-gate 	log_and_free_brevq(path, brevq);
41257c478bd9Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
41267c478bd9Sstevel@tonic-gate }
41277c478bd9Sstevel@tonic-gate 
41287c478bd9Sstevel@tonic-gate /*
41297c478bd9Sstevel@tonic-gate  * log the outstanding branch remove events for the grand children of the dip
41307c478bd9Sstevel@tonic-gate  * and free the associated memory.
41317c478bd9Sstevel@tonic-gate  */
41327c478bd9Sstevel@tonic-gate static void
41337c478bd9Sstevel@tonic-gate log_and_free_br_events_on_grand_children(dev_info_t *dip,
41347c478bd9Sstevel@tonic-gate     struct brevq_node *brevq)
41357c478bd9Sstevel@tonic-gate {
41367c478bd9Sstevel@tonic-gate 	struct brevq_node *brn;
41377c478bd9Sstevel@tonic-gate 	char *path;
41387c478bd9Sstevel@tonic-gate 	char *p;
41397c478bd9Sstevel@tonic-gate 
41407c478bd9Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
41417c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(dip, path);
41427c478bd9Sstevel@tonic-gate 	p = path + strlen(path);
4143*245c82d9Scth 	for (brn = brevq; brn != NULL; brn = brn->brn_sibling) {
4144*245c82d9Scth 		if (brn->brn_child) {
4145*245c82d9Scth 			(void) strcpy(p, brn->brn_deviname);
41467c478bd9Sstevel@tonic-gate 			/* now path contains the node path to the dip's child */
4147*245c82d9Scth 			log_and_free_brevq(path, brn->brn_child);
4148*245c82d9Scth 			brn->brn_child = NULL;
41497c478bd9Sstevel@tonic-gate 		}
41507c478bd9Sstevel@tonic-gate 	}
41517c478bd9Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
41527c478bd9Sstevel@tonic-gate }
41537c478bd9Sstevel@tonic-gate 
41547c478bd9Sstevel@tonic-gate /*
41557c478bd9Sstevel@tonic-gate  * log and cleanup branch remove events for the grand children of the dip.
41567c478bd9Sstevel@tonic-gate  */
41577c478bd9Sstevel@tonic-gate static void
41587c478bd9Sstevel@tonic-gate cleanup_br_events_on_grand_children(dev_info_t *dip, struct brevq_node **brevqp)
41597c478bd9Sstevel@tonic-gate {
41607c478bd9Sstevel@tonic-gate 	dev_info_t *child;
41617c478bd9Sstevel@tonic-gate 	struct brevq_node *brevq, *brn, *prev_brn, *next_brn;
41627c478bd9Sstevel@tonic-gate 	char *path;
41637c478bd9Sstevel@tonic-gate 	int circ;
41647c478bd9Sstevel@tonic-gate 
41657c478bd9Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
41667c478bd9Sstevel@tonic-gate 	prev_brn = NULL;
41677c478bd9Sstevel@tonic-gate 	brevq = *brevqp;
41687c478bd9Sstevel@tonic-gate 
41697c478bd9Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
41707c478bd9Sstevel@tonic-gate 	for (brn = brevq; brn != NULL; brn = next_brn) {
4171*245c82d9Scth 		next_brn = brn->brn_sibling;
41727c478bd9Sstevel@tonic-gate 		for (child = ddi_get_child(dip); child != NULL;
41737c478bd9Sstevel@tonic-gate 		    child = ddi_get_next_sibling(child)) {
41747c478bd9Sstevel@tonic-gate 			if (i_ddi_node_state(child) >= DS_INITIALIZED) {
41757c478bd9Sstevel@tonic-gate 				(void) ddi_deviname(child, path);
4176*245c82d9Scth 				if (strcmp(path, brn->brn_deviname) == 0)
41777c478bd9Sstevel@tonic-gate 					break;
41787c478bd9Sstevel@tonic-gate 			}
41797c478bd9Sstevel@tonic-gate 		}
41807c478bd9Sstevel@tonic-gate 
41817c478bd9Sstevel@tonic-gate 		if (child != NULL && !(DEVI_EVREMOVE(child))) {
41827c478bd9Sstevel@tonic-gate 			/*
41837c478bd9Sstevel@tonic-gate 			 * Event state is not REMOVE. So branch remove event
4184*245c82d9Scth 			 * is not going be generated on brn->brn_child.
41857c478bd9Sstevel@tonic-gate 			 * If any branch remove events were queued up on
4186*245c82d9Scth 			 * brn->brn_child log them and remove the brn
41877c478bd9Sstevel@tonic-gate 			 * from the queue.
41887c478bd9Sstevel@tonic-gate 			 */
4189*245c82d9Scth 			if (brn->brn_child) {
41907c478bd9Sstevel@tonic-gate 				(void) ddi_pathname(dip, path);
4191*245c82d9Scth 				(void) strcat(path, brn->brn_deviname);
4192*245c82d9Scth 				log_and_free_brevq(path, brn->brn_child);
41937c478bd9Sstevel@tonic-gate 			}
41947c478bd9Sstevel@tonic-gate 
41957c478bd9Sstevel@tonic-gate 			if (prev_brn)
4196*245c82d9Scth 				prev_brn->brn_sibling = next_brn;
41977c478bd9Sstevel@tonic-gate 			else
41987c478bd9Sstevel@tonic-gate 				*brevqp = next_brn;
41997c478bd9Sstevel@tonic-gate 
4200*245c82d9Scth 			kmem_free(brn->brn_deviname,
4201*245c82d9Scth 			    strlen(brn->brn_deviname) + 1);
42027c478bd9Sstevel@tonic-gate 			kmem_free(brn, sizeof (*brn));
42037c478bd9Sstevel@tonic-gate 		} else {
42047c478bd9Sstevel@tonic-gate 			/*
42057c478bd9Sstevel@tonic-gate 			 * Free up the outstanding branch remove events
4206*245c82d9Scth 			 * queued on brn->brn_child since brn->brn_child
42077c478bd9Sstevel@tonic-gate 			 * itself is eligible for branch remove event.
42087c478bd9Sstevel@tonic-gate 			 */
4209*245c82d9Scth 			if (brn->brn_child) {
4210*245c82d9Scth 				free_brevq(brn->brn_child);
4211*245c82d9Scth 				brn->brn_child = NULL;
42127c478bd9Sstevel@tonic-gate 			}
42137c478bd9Sstevel@tonic-gate 			prev_brn = brn;
42147c478bd9Sstevel@tonic-gate 		}
42157c478bd9Sstevel@tonic-gate 	}
42167c478bd9Sstevel@tonic-gate 
42177c478bd9Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
42187c478bd9Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
42197c478bd9Sstevel@tonic-gate }
42207c478bd9Sstevel@tonic-gate 
42217c478bd9Sstevel@tonic-gate static int
42227c478bd9Sstevel@tonic-gate need_remove_event(dev_info_t *dip, int flags)
42237c478bd9Sstevel@tonic-gate {
42247c478bd9Sstevel@tonic-gate 	if ((flags & (NDI_NO_EVENT | NDI_AUTODETACH)) == 0 &&
42257c478bd9Sstevel@tonic-gate 	    (flags & (NDI_DEVI_OFFLINE | NDI_UNCONFIG | NDI_DEVI_REMOVE)) &&
42267c478bd9Sstevel@tonic-gate 	    !(DEVI_EVREMOVE(dip)))
42277c478bd9Sstevel@tonic-gate 		return (1);
42287c478bd9Sstevel@tonic-gate 	else
42297c478bd9Sstevel@tonic-gate 		return (0);
42307c478bd9Sstevel@tonic-gate }
42317c478bd9Sstevel@tonic-gate 
42327c478bd9Sstevel@tonic-gate /*
42337c478bd9Sstevel@tonic-gate  * Unconfigure children/descendants of the dip.
42347c478bd9Sstevel@tonic-gate  *
42357c478bd9Sstevel@tonic-gate  * If the operation involves a branch event NDI_BRANCH_EVENT_OP is set
42367c478bd9Sstevel@tonic-gate  * through out the unconfiguration. On successful return *brevqp is set to
42377c478bd9Sstevel@tonic-gate  * a queue of dip's child devinames for which branch remove events need
42387c478bd9Sstevel@tonic-gate  * to be generated.
42397c478bd9Sstevel@tonic-gate  */
42407c478bd9Sstevel@tonic-gate static int
42417c478bd9Sstevel@tonic-gate devi_unconfig_branch(dev_info_t *dip, dev_info_t **dipp, int flags,
42427c478bd9Sstevel@tonic-gate     struct brevq_node **brevqp)
42437c478bd9Sstevel@tonic-gate {
42447c478bd9Sstevel@tonic-gate 	int rval;
42457c478bd9Sstevel@tonic-gate 
42467c478bd9Sstevel@tonic-gate 	*brevqp = NULL;
42477c478bd9Sstevel@tonic-gate 
42487c478bd9Sstevel@tonic-gate 	if ((!(flags & NDI_BRANCH_EVENT_OP)) && need_remove_event(dip, flags))
42497c478bd9Sstevel@tonic-gate 		flags |= NDI_BRANCH_EVENT_OP;
42507c478bd9Sstevel@tonic-gate 
42517c478bd9Sstevel@tonic-gate 	if (flags & NDI_BRANCH_EVENT_OP) {
42527c478bd9Sstevel@tonic-gate 		rval = devi_unconfig_common(dip, dipp, flags, (major_t)-1,
42537c478bd9Sstevel@tonic-gate 		    brevqp);
42547c478bd9Sstevel@tonic-gate 
42557c478bd9Sstevel@tonic-gate 		if (rval != NDI_SUCCESS && (*brevqp)) {
42567c478bd9Sstevel@tonic-gate 			log_and_free_brevq_dip(dip, *brevqp);
42577c478bd9Sstevel@tonic-gate 			*brevqp = NULL;
42587c478bd9Sstevel@tonic-gate 		}
42597c478bd9Sstevel@tonic-gate 	} else
42607c478bd9Sstevel@tonic-gate 		rval = devi_unconfig_common(dip, dipp, flags, (major_t)-1,
42617c478bd9Sstevel@tonic-gate 		    NULL);
42627c478bd9Sstevel@tonic-gate 
42637c478bd9Sstevel@tonic-gate 	return (rval);
42647c478bd9Sstevel@tonic-gate }
42657c478bd9Sstevel@tonic-gate 
42667c478bd9Sstevel@tonic-gate /*
42677c478bd9Sstevel@tonic-gate  * If the dip is already bound to a driver transition to DS_INITIALIZED
42687c478bd9Sstevel@tonic-gate  * in order to generate an event in the case where the node was left in
42697c478bd9Sstevel@tonic-gate  * DS_BOUND state since boot (never got attached) and the node is now
42707c478bd9Sstevel@tonic-gate  * being offlined.
42717c478bd9Sstevel@tonic-gate  */
42727c478bd9Sstevel@tonic-gate static void
42737c478bd9Sstevel@tonic-gate init_bound_node_ev(dev_info_t *pdip, dev_info_t *dip, int flags)
42747c478bd9Sstevel@tonic-gate {
42757c478bd9Sstevel@tonic-gate 	if (need_remove_event(dip, flags) &&
42767c478bd9Sstevel@tonic-gate 	    i_ddi_node_state(dip) == DS_BOUND &&
42777c478bd9Sstevel@tonic-gate 	    i_ddi_node_state(pdip) >= DS_ATTACHED &&
42787c478bd9Sstevel@tonic-gate 	    !(DEVI_IS_DEVICE_OFFLINE(dip)))
42797c478bd9Sstevel@tonic-gate 		(void) ddi_initchild(pdip, dip);
42807c478bd9Sstevel@tonic-gate }
42817c478bd9Sstevel@tonic-gate 
42827c478bd9Sstevel@tonic-gate /*
42837c478bd9Sstevel@tonic-gate  * attach a node/branch with parent already held busy
42847c478bd9Sstevel@tonic-gate  */
42857c478bd9Sstevel@tonic-gate static int
42867c478bd9Sstevel@tonic-gate devi_attach_node(dev_info_t *dip, uint_t flags)
42877c478bd9Sstevel@tonic-gate {
428816747f41Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
42897c478bd9Sstevel@tonic-gate 	if (flags & NDI_DEVI_ONLINE) {
429016747f41Scth 		if (i_ddi_node_state(dip) != DS_READY)
429116747f41Scth 			DEVI_SET_REPORT(dip);
42927c478bd9Sstevel@tonic-gate 		DEVI_SET_DEVICE_ONLINE(dip);
42937c478bd9Sstevel@tonic-gate 	}
42947c478bd9Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
429516747f41Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
42967c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
42977c478bd9Sstevel@tonic-gate 	}
429816747f41Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
42997c478bd9Sstevel@tonic-gate 
43007c478bd9Sstevel@tonic-gate 	if (i_ddi_attachchild(dip) != DDI_SUCCESS) {
430116747f41Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
43027c478bd9Sstevel@tonic-gate 		DEVI_SET_EVUNINIT(dip);
430316747f41Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
430416747f41Scth 
43057c478bd9Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(dip))
43067c478bd9Sstevel@tonic-gate 			(void) ddi_uninitchild(dip);
43077c478bd9Sstevel@tonic-gate 		else {
43087c478bd9Sstevel@tonic-gate 			/*
43097c478bd9Sstevel@tonic-gate 			 * Delete .conf nodes and nodes that are not
43107c478bd9Sstevel@tonic-gate 			 * well formed.
43117c478bd9Sstevel@tonic-gate 			 */
43127c478bd9Sstevel@tonic-gate 			(void) ddi_remove_child(dip, 0);
43137c478bd9Sstevel@tonic-gate 		}
43147c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
43157c478bd9Sstevel@tonic-gate 	}
43167c478bd9Sstevel@tonic-gate 
43177c478bd9Sstevel@tonic-gate 	i_ndi_devi_report_status_change(dip, NULL);
43187c478bd9Sstevel@tonic-gate 
43197c478bd9Sstevel@tonic-gate 	/*
43207c478bd9Sstevel@tonic-gate 	 * log an event, but not during devfs lookups in which case
43217c478bd9Sstevel@tonic-gate 	 * NDI_NO_EVENT is set.
43227c478bd9Sstevel@tonic-gate 	 */
43237c478bd9Sstevel@tonic-gate 	if ((flags & NDI_NO_EVENT) == 0 && !(DEVI_EVADD(dip))) {
43247c478bd9Sstevel@tonic-gate 		(void) i_log_devfs_add_devinfo(dip, flags);
432516747f41Scth 
432616747f41Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
43277c478bd9Sstevel@tonic-gate 		DEVI_SET_EVADD(dip);
432816747f41Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
432916747f41Scth 	} else if (!(flags & NDI_NO_EVENT_STATE_CHNG)) {
433016747f41Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
43317c478bd9Sstevel@tonic-gate 		DEVI_SET_EVADD(dip);
433216747f41Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
433316747f41Scth 	}
43347c478bd9Sstevel@tonic-gate 
43357c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
43367c478bd9Sstevel@tonic-gate }
43377c478bd9Sstevel@tonic-gate 
43387c478bd9Sstevel@tonic-gate /*
43397c478bd9Sstevel@tonic-gate  * Configure all children of a nexus, assuming all spec children have
43407c478bd9Sstevel@tonic-gate  * been made.
43417c478bd9Sstevel@tonic-gate  */
43427c478bd9Sstevel@tonic-gate static int
43437c478bd9Sstevel@tonic-gate devi_attach_children(dev_info_t *pdip, uint_t flags, major_t major)
43447c478bd9Sstevel@tonic-gate {
43457c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
43467c478bd9Sstevel@tonic-gate 
43477c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
43487c478bd9Sstevel@tonic-gate 
43497c478bd9Sstevel@tonic-gate 	dip = ddi_get_child(pdip);
43507c478bd9Sstevel@tonic-gate 	while (dip) {
43517c478bd9Sstevel@tonic-gate 		/*
43527c478bd9Sstevel@tonic-gate 		 * NOTE: devi_attach_node() may remove the dip
43537c478bd9Sstevel@tonic-gate 		 */
43547c478bd9Sstevel@tonic-gate 		dev_info_t *next = ddi_get_next_sibling(dip);
43557c478bd9Sstevel@tonic-gate 
43567c478bd9Sstevel@tonic-gate 		/*
43577c478bd9Sstevel@tonic-gate 		 * Configure all nexus nodes or leaf nodes with
43587c478bd9Sstevel@tonic-gate 		 * matching driver major
43597c478bd9Sstevel@tonic-gate 		 */
43607c478bd9Sstevel@tonic-gate 		if ((major == (major_t)-1) ||
43617c478bd9Sstevel@tonic-gate 		    (major == ddi_driver_major(dip)) ||
43627c478bd9Sstevel@tonic-gate 		    ((flags & NDI_CONFIG) && (is_leaf_node(dip) == 0)))
43637c478bd9Sstevel@tonic-gate 			(void) devi_attach_node(dip, flags);
43647c478bd9Sstevel@tonic-gate 		dip = next;
43657c478bd9Sstevel@tonic-gate 	}
43667c478bd9Sstevel@tonic-gate 
43677c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
43687c478bd9Sstevel@tonic-gate }
43697c478bd9Sstevel@tonic-gate 
43707c478bd9Sstevel@tonic-gate /* internal function to config immediate children */
43717c478bd9Sstevel@tonic-gate static int
43727c478bd9Sstevel@tonic-gate config_immediate_children(dev_info_t *pdip, uint_t flags, major_t major)
43737c478bd9Sstevel@tonic-gate {
43747c478bd9Sstevel@tonic-gate 	int circ;
43757c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(pdip) >= DS_ATTACHED);
43767c478bd9Sstevel@tonic-gate 
43777c478bd9Sstevel@tonic-gate 	if (!NEXUS_DRV(ddi_get_driver(pdip)))
43787c478bd9Sstevel@tonic-gate 		return (NDI_SUCCESS);
43797c478bd9Sstevel@tonic-gate 
43807c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
43817c478bd9Sstevel@tonic-gate 	    "config_immediate_children: %s%d (%p), flags=%x\n",
43827c478bd9Sstevel@tonic-gate 	    ddi_driver_name(pdip), ddi_get_instance(pdip),
43837c478bd9Sstevel@tonic-gate 	    (void *)pdip, flags));
43847c478bd9Sstevel@tonic-gate 
43857c478bd9Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
43867c478bd9Sstevel@tonic-gate 
43877c478bd9Sstevel@tonic-gate 	if (flags & NDI_CONFIG_REPROBE) {
43887c478bd9Sstevel@tonic-gate 		mutex_enter(&DEVI(pdip)->devi_lock);
43897c478bd9Sstevel@tonic-gate 		DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
43907c478bd9Sstevel@tonic-gate 		mutex_exit(&DEVI(pdip)->devi_lock);
43917c478bd9Sstevel@tonic-gate 	}
43927c478bd9Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(pdip, flags);
43937c478bd9Sstevel@tonic-gate 	i_ndi_init_hw_children(pdip, flags);
43947c478bd9Sstevel@tonic-gate 	(void) devi_attach_children(pdip, flags, major);
43957c478bd9Sstevel@tonic-gate 
43967c478bd9Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
43977c478bd9Sstevel@tonic-gate 
43987c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
43997c478bd9Sstevel@tonic-gate }
44007c478bd9Sstevel@tonic-gate 
44017c478bd9Sstevel@tonic-gate /* internal function to config grand children */
44027c478bd9Sstevel@tonic-gate static int
44037c478bd9Sstevel@tonic-gate config_grand_children(dev_info_t *pdip, uint_t flags, major_t major)
44047c478bd9Sstevel@tonic-gate {
44057c478bd9Sstevel@tonic-gate 	struct mt_config_handle *hdl;
44067c478bd9Sstevel@tonic-gate 
44077c478bd9Sstevel@tonic-gate 	/* multi-threaded configuration of child nexus */
44087c478bd9Sstevel@tonic-gate 	hdl = mt_config_init(pdip, NULL, flags, major, MT_CONFIG_OP, NULL);
44097c478bd9Sstevel@tonic-gate 	mt_config_children(hdl);
44107c478bd9Sstevel@tonic-gate 
44117c478bd9Sstevel@tonic-gate 	return (mt_config_fini(hdl));	/* wait for threads to exit */
44127c478bd9Sstevel@tonic-gate }
44137c478bd9Sstevel@tonic-gate 
44147c478bd9Sstevel@tonic-gate /*
44157c478bd9Sstevel@tonic-gate  * Common function for device tree configuration,
44167c478bd9Sstevel@tonic-gate  * either BUS_CONFIG_ALL or BUS_CONFIG_DRIVER.
44177c478bd9Sstevel@tonic-gate  * The NDI_CONFIG flag causes recursive configuration of
44187c478bd9Sstevel@tonic-gate  * grandchildren, devfs usage should not recurse.
44197c478bd9Sstevel@tonic-gate  */
44207c478bd9Sstevel@tonic-gate static int
44217c478bd9Sstevel@tonic-gate devi_config_common(dev_info_t *dip, int flags, major_t major)
44227c478bd9Sstevel@tonic-gate {
44237c478bd9Sstevel@tonic-gate 	int error;
44247c478bd9Sstevel@tonic-gate 	int (*f)();
44257c478bd9Sstevel@tonic-gate 
44267c478bd9Sstevel@tonic-gate 	if (i_ddi_node_state(dip) <  DS_READY)
44277c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
44287c478bd9Sstevel@tonic-gate 
44297c478bd9Sstevel@tonic-gate 	if (pm_pre_config(dip, NULL) != DDI_SUCCESS)
44307c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
44317c478bd9Sstevel@tonic-gate 
44327c478bd9Sstevel@tonic-gate 	if ((DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
44337c478bd9Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
44347c478bd9Sstevel@tonic-gate 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
44357c478bd9Sstevel@tonic-gate 		error = config_immediate_children(dip, flags, major);
44367c478bd9Sstevel@tonic-gate 	} else {
44377c478bd9Sstevel@tonic-gate 		/* call bus_config entry point */
44387c478bd9Sstevel@tonic-gate 		ddi_bus_config_op_t bus_op = (major == (major_t)-1) ?
44397c478bd9Sstevel@tonic-gate 		    BUS_CONFIG_ALL : BUS_CONFIG_DRIVER;
44407c478bd9Sstevel@tonic-gate 		error = (*f)(dip,
44417c478bd9Sstevel@tonic-gate 		    flags, bus_op, (void *)(uintptr_t)major, NULL, 0);
44427c478bd9Sstevel@tonic-gate 	}
44437c478bd9Sstevel@tonic-gate 
44447c478bd9Sstevel@tonic-gate 	if (error) {
44457c478bd9Sstevel@tonic-gate 		pm_post_config(dip, NULL);
44467c478bd9Sstevel@tonic-gate 		return (error);
44477c478bd9Sstevel@tonic-gate 	}
44487c478bd9Sstevel@tonic-gate 
44497c478bd9Sstevel@tonic-gate 	/*
44507c478bd9Sstevel@tonic-gate 	 * Some callers, notably SCSI, need to mark the devfs cache
44517c478bd9Sstevel@tonic-gate 	 * to be rebuilt together with the config operation.
44527c478bd9Sstevel@tonic-gate 	 */
44537c478bd9Sstevel@tonic-gate 	if (flags & NDI_DEVFS_CLEAN)
44547c478bd9Sstevel@tonic-gate 		(void) devfs_clean(dip, NULL, 0);
44557c478bd9Sstevel@tonic-gate 
44567c478bd9Sstevel@tonic-gate 	if (flags & NDI_CONFIG)
44577c478bd9Sstevel@tonic-gate 		(void) config_grand_children(dip, flags, major);
44587c478bd9Sstevel@tonic-gate 
44597c478bd9Sstevel@tonic-gate 	pm_post_config(dip, NULL);
44607c478bd9Sstevel@tonic-gate 
44617c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
44627c478bd9Sstevel@tonic-gate }
44637c478bd9Sstevel@tonic-gate 
44647c478bd9Sstevel@tonic-gate /*
44657c478bd9Sstevel@tonic-gate  * Framework entry point for BUS_CONFIG_ALL
44667c478bd9Sstevel@tonic-gate  */
44677c478bd9Sstevel@tonic-gate int
44687c478bd9Sstevel@tonic-gate ndi_devi_config(dev_info_t *dip, int flags)
44697c478bd9Sstevel@tonic-gate {
44707c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
44717c478bd9Sstevel@tonic-gate 	    "ndi_devi_config: par = %s%d (%p), flags = 0x%x\n",
44727c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
44737c478bd9Sstevel@tonic-gate 
44747c478bd9Sstevel@tonic-gate 	return (devi_config_common(dip, flags, (major_t)-1));
44757c478bd9Sstevel@tonic-gate }
44767c478bd9Sstevel@tonic-gate 
44777c478bd9Sstevel@tonic-gate /*
44787c478bd9Sstevel@tonic-gate  * Framework entry point for BUS_CONFIG_DRIVER, bound to major
44797c478bd9Sstevel@tonic-gate  */
44807c478bd9Sstevel@tonic-gate int
44817c478bd9Sstevel@tonic-gate ndi_devi_config_driver(dev_info_t *dip, int flags, major_t major)
44827c478bd9Sstevel@tonic-gate {
44837c478bd9Sstevel@tonic-gate 	/* don't abuse this function */
44847c478bd9Sstevel@tonic-gate 	ASSERT(major != (major_t)-1);
44857c478bd9Sstevel@tonic-gate 
44867c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
44877c478bd9Sstevel@tonic-gate 	    "ndi_devi_config_driver: par = %s%d (%p), flags = 0x%x\n",
44887c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
44897c478bd9Sstevel@tonic-gate 
44907c478bd9Sstevel@tonic-gate 	return (devi_config_common(dip, flags, major));
44917c478bd9Sstevel@tonic-gate }
44927c478bd9Sstevel@tonic-gate 
44937c478bd9Sstevel@tonic-gate /*
44947c478bd9Sstevel@tonic-gate  * called by nexus drivers to configure/unconfigure its children
44957c478bd9Sstevel@tonic-gate  */
44967c478bd9Sstevel@tonic-gate static int
44977c478bd9Sstevel@tonic-gate devi_config_one(dev_info_t *pdip, char *devnm, dev_info_t **dipp,
44987c478bd9Sstevel@tonic-gate     uint_t flags, clock_t timeout)
44997c478bd9Sstevel@tonic-gate {
45007c478bd9Sstevel@tonic-gate 	int circ, probed, rv;
45017c478bd9Sstevel@tonic-gate 	dev_info_t *dip = NULL;
45027c478bd9Sstevel@tonic-gate 	char *name, *addr, *drivername = NULL;
45037c478bd9Sstevel@tonic-gate 	clock_t end_time;	/* 60 sec */
45047c478bd9Sstevel@tonic-gate 
45057c478bd9Sstevel@tonic-gate 	if (!NEXUS_DRV(ddi_get_driver(pdip)))
45067c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
45077c478bd9Sstevel@tonic-gate 
45087c478bd9Sstevel@tonic-gate 	if (MDI_PHCI(pdip)) {
45097c478bd9Sstevel@tonic-gate 		/* Call mdi_ to configure the child */
45107c478bd9Sstevel@tonic-gate 		rv = mdi_devi_config_one(pdip, devnm, dipp, flags, timeout);
45117c478bd9Sstevel@tonic-gate 		if (rv == MDI_SUCCESS)
45127c478bd9Sstevel@tonic-gate 			return (NDI_SUCCESS);
45137c478bd9Sstevel@tonic-gate 
45147c478bd9Sstevel@tonic-gate 		/*
45157c478bd9Sstevel@tonic-gate 		 * Normally, we should return failure here.
45167c478bd9Sstevel@tonic-gate 		 *
45177c478bd9Sstevel@tonic-gate 		 * Leadville implemented an unfortunate fallback mechanism.
45187c478bd9Sstevel@tonic-gate 		 * If a target is non-standard and scsi_vhci doesn't know
45197c478bd9Sstevel@tonic-gate 		 * how to do failover, then the node is enumerated under
45207c478bd9Sstevel@tonic-gate 		 * phci. Leadville specifies NDI_MDI_FALLBACK flag to
45217c478bd9Sstevel@tonic-gate 		 * maintain the old behavior.
45227c478bd9Sstevel@tonic-gate 		 */
45237c478bd9Sstevel@tonic-gate 		if ((flags & NDI_MDI_FALLBACK) == 0)
45247c478bd9Sstevel@tonic-gate 			return (NDI_FAILURE);
45257c478bd9Sstevel@tonic-gate 	}
45267c478bd9Sstevel@tonic-gate 
45277c478bd9Sstevel@tonic-gate 	/* split name into "name@addr" parts */
45287c478bd9Sstevel@tonic-gate 	i_ddi_parse_name(devnm, &name, &addr, NULL);
45297c478bd9Sstevel@tonic-gate 
45307c478bd9Sstevel@tonic-gate 	if (flags & NDI_PROMNAME) {
45317c478bd9Sstevel@tonic-gate 		/*
45327c478bd9Sstevel@tonic-gate 		 * We may have a genericname on a system that creates
45337c478bd9Sstevel@tonic-gate 		 * drivername nodes (from .conf files).  Find the drivername
45347c478bd9Sstevel@tonic-gate 		 * by nodeid. If we can't find a node with devnm as the
45357c478bd9Sstevel@tonic-gate 		 * node name then we search by drivername.  This allows an
45367c478bd9Sstevel@tonic-gate 		 * implementation to supply a genericly named boot path (disk)
45377c478bd9Sstevel@tonic-gate 		 * and locate drivename nodes (sd).
45387c478bd9Sstevel@tonic-gate 		 */
45397c478bd9Sstevel@tonic-gate 		drivername = child_path_to_driver(pdip, name, addr);
45407c478bd9Sstevel@tonic-gate 	}
45417c478bd9Sstevel@tonic-gate 
45427c478bd9Sstevel@tonic-gate 	if (timeout > 0) {
45437c478bd9Sstevel@tonic-gate 		end_time = ddi_get_lbolt() + timeout;
45447c478bd9Sstevel@tonic-gate 	}
45457c478bd9Sstevel@tonic-gate 
45467c478bd9Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
45477c478bd9Sstevel@tonic-gate 
45487c478bd9Sstevel@tonic-gate reprobe:
45497c478bd9Sstevel@tonic-gate 	probed = (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
45507c478bd9Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(pdip, flags);
45517c478bd9Sstevel@tonic-gate 	for (;;) {
45527c478bd9Sstevel@tonic-gate 		dip = find_child_by_name(pdip, name, addr);
45537c478bd9Sstevel@tonic-gate 		/*
45547c478bd9Sstevel@tonic-gate 		 * Search for a node bound to the drivername driver with
45557c478bd9Sstevel@tonic-gate 		 * the specified "@addr".
45567c478bd9Sstevel@tonic-gate 		 */
45577c478bd9Sstevel@tonic-gate 		if (dip == NULL && drivername)
45587c478bd9Sstevel@tonic-gate 			dip = find_child_by_driver(pdip, drivername, addr);
45597c478bd9Sstevel@tonic-gate 
45607c478bd9Sstevel@tonic-gate 		if (dip || timeout <= 0 || ddi_get_lbolt() >= end_time)
45617c478bd9Sstevel@tonic-gate 			break;
45627c478bd9Sstevel@tonic-gate 
45637c478bd9Sstevel@tonic-gate 		/*
45647c478bd9Sstevel@tonic-gate 		 * Wait up to end_time for asynchronous enumeration
45657c478bd9Sstevel@tonic-gate 		 */
45667c478bd9Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
45677c478bd9Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT,
45687c478bd9Sstevel@tonic-gate 		    "%s%d: waiting for child %s@%s, timeout %ld",
45697c478bd9Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
45707c478bd9Sstevel@tonic-gate 		    name, addr, timeout));
45717c478bd9Sstevel@tonic-gate 
45727c478bd9Sstevel@tonic-gate 		mutex_enter(&DEVI(pdip)->devi_lock);
45737c478bd9Sstevel@tonic-gate 		(void) cv_timedwait(&DEVI(pdip)->devi_cv,
45747c478bd9Sstevel@tonic-gate 		    &DEVI(pdip)->devi_lock, end_time);
45757c478bd9Sstevel@tonic-gate 		mutex_exit(&DEVI(pdip)->devi_lock);
45767c478bd9Sstevel@tonic-gate 		ndi_devi_enter(pdip, &circ);
45777c478bd9Sstevel@tonic-gate 		(void) i_ndi_make_spec_children(pdip, flags);
45787c478bd9Sstevel@tonic-gate 	}
45797c478bd9Sstevel@tonic-gate 
45807c478bd9Sstevel@tonic-gate 	if ((dip == NULL) && probed && (flags & NDI_CONFIG_REPROBE) &&
45817c478bd9Sstevel@tonic-gate 	    i_ddi_io_initialized()) {
45827c478bd9Sstevel@tonic-gate 		/*
45837c478bd9Sstevel@tonic-gate 		 * reenumerate .conf nodes and probe again
45847c478bd9Sstevel@tonic-gate 		 */
45857c478bd9Sstevel@tonic-gate 		mutex_enter(&DEVI(pdip)->devi_lock);
45867c478bd9Sstevel@tonic-gate 		DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
45877c478bd9Sstevel@tonic-gate 		mutex_exit(&DEVI(pdip)->devi_lock);
45887c478bd9Sstevel@tonic-gate 		goto reprobe;
45897c478bd9Sstevel@tonic-gate 	}
45907c478bd9Sstevel@tonic-gate 
45917c478bd9Sstevel@tonic-gate 	if (addr[0] != '\0')
45927c478bd9Sstevel@tonic-gate 		*(addr - 1) = '@';
45937c478bd9Sstevel@tonic-gate 
45947c478bd9Sstevel@tonic-gate 	if (dip == NULL || devi_attach_node(dip, flags) != NDI_SUCCESS) {
45957c478bd9Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
45967c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
45977c478bd9Sstevel@tonic-gate 	}
45987c478bd9Sstevel@tonic-gate 
45997c478bd9Sstevel@tonic-gate 	*dipp = dip;
46007c478bd9Sstevel@tonic-gate 	ndi_hold_devi(dip);
46017c478bd9Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
46027c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
46037c478bd9Sstevel@tonic-gate }
46047c478bd9Sstevel@tonic-gate 
46057c478bd9Sstevel@tonic-gate /*
46067c478bd9Sstevel@tonic-gate  * Enumerate and attach a child specified by name 'devnm'.
46077c478bd9Sstevel@tonic-gate  * Called by devfs lookup and DR to perform a BUS_CONFIG_ONE.
46087c478bd9Sstevel@tonic-gate  * Note: devfs does not make use of NDI_CONFIG to configure
46097c478bd9Sstevel@tonic-gate  * an entire branch.
46107c478bd9Sstevel@tonic-gate  */
46117c478bd9Sstevel@tonic-gate int
46127c478bd9Sstevel@tonic-gate ndi_devi_config_one(dev_info_t *dip, char *devnm, dev_info_t **dipp, int flags)
46137c478bd9Sstevel@tonic-gate {
46147c478bd9Sstevel@tonic-gate 	int error;
46157c478bd9Sstevel@tonic-gate 	int (*f)();
46167c478bd9Sstevel@tonic-gate 	int branch_event = 0;
46177c478bd9Sstevel@tonic-gate 
46187c478bd9Sstevel@tonic-gate 	ASSERT(dipp);
46197c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) >= DS_ATTACHED);
46207c478bd9Sstevel@tonic-gate 
46217c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
46227c478bd9Sstevel@tonic-gate 	    "ndi_devi_config_one: par = %s%d (%p), child = %s\n",
46237c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, devnm));
46247c478bd9Sstevel@tonic-gate 
46257c478bd9Sstevel@tonic-gate 	if (pm_pre_config(dip, devnm) != DDI_SUCCESS)
46267c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
46277c478bd9Sstevel@tonic-gate 
46287c478bd9Sstevel@tonic-gate 	if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
46297c478bd9Sstevel@tonic-gate 	    (flags & NDI_CONFIG)) {
46307c478bd9Sstevel@tonic-gate 		flags |= NDI_BRANCH_EVENT_OP;
46317c478bd9Sstevel@tonic-gate 		branch_event = 1;
46327c478bd9Sstevel@tonic-gate 	}
46337c478bd9Sstevel@tonic-gate 
46347c478bd9Sstevel@tonic-gate 	if ((DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
46357c478bd9Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
46367c478bd9Sstevel@tonic-gate 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
46377c478bd9Sstevel@tonic-gate 		error = devi_config_one(dip, devnm, dipp, flags, 0);
46387c478bd9Sstevel@tonic-gate 	} else {
46397c478bd9Sstevel@tonic-gate 		/* call bus_config entry point */
46407c478bd9Sstevel@tonic-gate 		error = (*f)(dip, flags, BUS_CONFIG_ONE, (void *)devnm, dipp);
46417c478bd9Sstevel@tonic-gate 	}
46427c478bd9Sstevel@tonic-gate 
46437c478bd9Sstevel@tonic-gate 	if (error || (flags & NDI_CONFIG) == 0) {
46447c478bd9Sstevel@tonic-gate 		pm_post_config(dip, devnm);
46457c478bd9Sstevel@tonic-gate 		return (error);
46467c478bd9Sstevel@tonic-gate 	}
46477c478bd9Sstevel@tonic-gate 
46487c478bd9Sstevel@tonic-gate 	/*
46497c478bd9Sstevel@tonic-gate 	 * DR usage ((i.e. call with NDI_CONFIG) recursively configures
46507c478bd9Sstevel@tonic-gate 	 * grandchildren, performing a BUS_CONFIG_ALL from the node attached
46517c478bd9Sstevel@tonic-gate 	 * by the BUS_CONFIG_ONE.
46527c478bd9Sstevel@tonic-gate 	 */
46537c478bd9Sstevel@tonic-gate 	ASSERT(*dipp);
46547c478bd9Sstevel@tonic-gate 
46557c478bd9Sstevel@tonic-gate 	error = devi_config_common(*dipp, flags, (major_t)-1);
46567c478bd9Sstevel@tonic-gate 
46577c478bd9Sstevel@tonic-gate 	pm_post_config(dip, devnm);
46587c478bd9Sstevel@tonic-gate 
46597c478bd9Sstevel@tonic-gate 	if (branch_event)
46607c478bd9Sstevel@tonic-gate 		(void) i_log_devfs_branch_add(*dipp);
46617c478bd9Sstevel@tonic-gate 
46627c478bd9Sstevel@tonic-gate 	return (error);
46637c478bd9Sstevel@tonic-gate }
46647c478bd9Sstevel@tonic-gate 
46657c478bd9Sstevel@tonic-gate 
46667c478bd9Sstevel@tonic-gate /*
46677c478bd9Sstevel@tonic-gate  * Enumerate and attach a child specified by name 'devnm'.
46687c478bd9Sstevel@tonic-gate  * Called during configure the OBP options. This configures
46697c478bd9Sstevel@tonic-gate  * only one node.
46707c478bd9Sstevel@tonic-gate  */
46717c478bd9Sstevel@tonic-gate static int
46727c478bd9Sstevel@tonic-gate ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
46737c478bd9Sstevel@tonic-gate     dev_info_t **childp, int flags)
46747c478bd9Sstevel@tonic-gate {
46757c478bd9Sstevel@tonic-gate 	int error;
46767c478bd9Sstevel@tonic-gate 	int (*f)();
46777c478bd9Sstevel@tonic-gate 
46787c478bd9Sstevel@tonic-gate 	ASSERT(childp);
46797c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(parent) >= DS_ATTACHED);
46807c478bd9Sstevel@tonic-gate 
46817c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_config_obp_args: "
46827c478bd9Sstevel@tonic-gate 	    "par = %s%d (%p), child = %s\n", ddi_driver_name(parent),
46837c478bd9Sstevel@tonic-gate 	    ddi_get_instance(parent), (void *)parent, devnm));
46847c478bd9Sstevel@tonic-gate 
46857c478bd9Sstevel@tonic-gate 	if ((DEVI(parent)->devi_ops->devo_bus_ops == NULL) ||
46867c478bd9Sstevel@tonic-gate 	    (DEVI(parent)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
46877c478bd9Sstevel@tonic-gate 	    (f = DEVI(parent)->devi_ops->devo_bus_ops->bus_config) == NULL) {
46887c478bd9Sstevel@tonic-gate 		error = NDI_FAILURE;
46897c478bd9Sstevel@tonic-gate 	} else {
46907c478bd9Sstevel@tonic-gate 		/* call bus_config entry point */
46917c478bd9Sstevel@tonic-gate 		error = (*f)(parent, flags,
46927c478bd9Sstevel@tonic-gate 		    BUS_CONFIG_OBP_ARGS, (void *)devnm, childp);
46937c478bd9Sstevel@tonic-gate 	}
46947c478bd9Sstevel@tonic-gate 	return (error);
46957c478bd9Sstevel@tonic-gate }
46967c478bd9Sstevel@tonic-gate 
46977c478bd9Sstevel@tonic-gate 
46987c478bd9Sstevel@tonic-gate /*
46997c478bd9Sstevel@tonic-gate  * detach a node with parent already held busy
47007c478bd9Sstevel@tonic-gate  */
47017c478bd9Sstevel@tonic-gate static int
47027c478bd9Sstevel@tonic-gate devi_detach_node(dev_info_t *dip, uint_t flags)
47037c478bd9Sstevel@tonic-gate {
47047c478bd9Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
47057c478bd9Sstevel@tonic-gate 	int ret = NDI_SUCCESS;
47067c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t cookie;
47077c478bd9Sstevel@tonic-gate 
47087c478bd9Sstevel@tonic-gate 	if (flags & NDI_POST_EVENT) {
47097c478bd9Sstevel@tonic-gate 		if (pdip && i_ddi_node_state(pdip) >= DS_ATTACHED) {
47107c478bd9Sstevel@tonic-gate 			if (ddi_get_eventcookie(dip, DDI_DEVI_REMOVE_EVENT,
47117c478bd9Sstevel@tonic-gate 			    &cookie) == NDI_SUCCESS)
47127c478bd9Sstevel@tonic-gate 				(void) ndi_post_event(dip, dip, cookie, NULL);
47137c478bd9Sstevel@tonic-gate 		}
47147c478bd9Sstevel@tonic-gate 	}
47157c478bd9Sstevel@tonic-gate 
47167c478bd9Sstevel@tonic-gate 	if (i_ddi_detachchild(dip, flags) != DDI_SUCCESS)
47177c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
47187c478bd9Sstevel@tonic-gate 
47197c478bd9Sstevel@tonic-gate 	if (flags & NDI_AUTODETACH)
47207c478bd9Sstevel@tonic-gate 		return (NDI_SUCCESS);
47217c478bd9Sstevel@tonic-gate 
47227c478bd9Sstevel@tonic-gate 	/*
47237c478bd9Sstevel@tonic-gate 	 * For DR, even bound nodes may need to have offline
47247c478bd9Sstevel@tonic-gate 	 * flag set.
47257c478bd9Sstevel@tonic-gate 	 */
47267c478bd9Sstevel@tonic-gate 	if (flags & NDI_DEVI_OFFLINE) {
472716747f41Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
47287c478bd9Sstevel@tonic-gate 		DEVI_SET_DEVICE_OFFLINE(dip);
472916747f41Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
47307c478bd9Sstevel@tonic-gate 	}
47317c478bd9Sstevel@tonic-gate 
47327c478bd9Sstevel@tonic-gate 	if (i_ddi_node_state(dip) == DS_INITIALIZED) {
47337c478bd9Sstevel@tonic-gate 		char *path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
47347c478bd9Sstevel@tonic-gate 		(void) ddi_pathname(dip, path);
47357c478bd9Sstevel@tonic-gate 		if (flags & NDI_DEVI_OFFLINE)
47367c478bd9Sstevel@tonic-gate 			i_ndi_devi_report_status_change(dip, path);
47377c478bd9Sstevel@tonic-gate 
47387c478bd9Sstevel@tonic-gate 		if (need_remove_event(dip, flags)) {
47397c478bd9Sstevel@tonic-gate 			(void) i_log_devfs_remove_devinfo(path,
47407c478bd9Sstevel@tonic-gate 			    i_ddi_devi_class(dip),
47417c478bd9Sstevel@tonic-gate 			    (char *)ddi_driver_name(dip),
47427c478bd9Sstevel@tonic-gate 			    ddi_get_instance(dip),
47437c478bd9Sstevel@tonic-gate 			    flags);
474416747f41Scth 			mutex_enter(&(DEVI(dip)->devi_lock));
47457c478bd9Sstevel@tonic-gate 			DEVI_SET_EVREMOVE(dip);
474616747f41Scth 			mutex_exit(&(DEVI(dip)->devi_lock));
47477c478bd9Sstevel@tonic-gate 		}
47487c478bd9Sstevel@tonic-gate 		kmem_free(path, MAXPATHLEN);
47497c478bd9Sstevel@tonic-gate 	}
47507c478bd9Sstevel@tonic-gate 
47517c478bd9Sstevel@tonic-gate 	if (flags & (NDI_UNCONFIG | NDI_DEVI_REMOVE)) {
47527c478bd9Sstevel@tonic-gate 		ret = ddi_uninitchild(dip);
47537c478bd9Sstevel@tonic-gate 		if (ret == NDI_SUCCESS) {
47547c478bd9Sstevel@tonic-gate 			/*
47557c478bd9Sstevel@tonic-gate 			 * Remove uninitialized pseudo nodes because
47567c478bd9Sstevel@tonic-gate 			 * system props are lost and the node cannot be
47577c478bd9Sstevel@tonic-gate 			 * reattached.
47587c478bd9Sstevel@tonic-gate 			 */
47597c478bd9Sstevel@tonic-gate 			if (!ndi_dev_is_persistent_node(dip))
47607c478bd9Sstevel@tonic-gate 				flags |= NDI_DEVI_REMOVE;
47617c478bd9Sstevel@tonic-gate 
47627c478bd9Sstevel@tonic-gate 			if (flags & NDI_DEVI_REMOVE)
47637c478bd9Sstevel@tonic-gate 				ret = ddi_remove_child(dip, 0);
47647c478bd9Sstevel@tonic-gate 		}
47657c478bd9Sstevel@tonic-gate 	}
47667c478bd9Sstevel@tonic-gate 
47677c478bd9Sstevel@tonic-gate 	return (ret);
47687c478bd9Sstevel@tonic-gate }
47697c478bd9Sstevel@tonic-gate 
47707c478bd9Sstevel@tonic-gate /*
47717c478bd9Sstevel@tonic-gate  * unconfigure immediate children of bus nexus device
47727c478bd9Sstevel@tonic-gate  */
47737c478bd9Sstevel@tonic-gate static int
47747c478bd9Sstevel@tonic-gate unconfig_immediate_children(
47757c478bd9Sstevel@tonic-gate 	dev_info_t *dip,
47767c478bd9Sstevel@tonic-gate 	dev_info_t **dipp,
47777c478bd9Sstevel@tonic-gate 	int flags,
47787c478bd9Sstevel@tonic-gate 	major_t major)
47797c478bd9Sstevel@tonic-gate {
47807c478bd9Sstevel@tonic-gate 	int rv = NDI_SUCCESS, circ;
47817c478bd9Sstevel@tonic-gate 	dev_info_t *child;
47827c478bd9Sstevel@tonic-gate 
47837c478bd9Sstevel@tonic-gate 	ASSERT(dipp == NULL || *dipp == NULL);
47847c478bd9Sstevel@tonic-gate 
47857c478bd9Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
47867c478bd9Sstevel@tonic-gate 	child = ddi_get_child(dip);
47877c478bd9Sstevel@tonic-gate 	while (child) {
47887c478bd9Sstevel@tonic-gate 		dev_info_t *next = ddi_get_next_sibling(child);
47897c478bd9Sstevel@tonic-gate 		if ((major != (major_t)-1) &&
47907c478bd9Sstevel@tonic-gate 		    (major != ddi_driver_major(child))) {
47917c478bd9Sstevel@tonic-gate 			child = next;
47927c478bd9Sstevel@tonic-gate 			continue;
47937c478bd9Sstevel@tonic-gate 		}
47947c478bd9Sstevel@tonic-gate 
47957c478bd9Sstevel@tonic-gate 		/* skip nexus nodes during autodetach */
47967c478bd9Sstevel@tonic-gate 		if ((flags & NDI_AUTODETACH) && !is_leaf_node(child)) {
47977c478bd9Sstevel@tonic-gate 			child = next;
47987c478bd9Sstevel@tonic-gate 			continue;
47997c478bd9Sstevel@tonic-gate 		}
48007c478bd9Sstevel@tonic-gate 
48017c478bd9Sstevel@tonic-gate 		if (devi_detach_node(child, flags) != NDI_SUCCESS) {
48027c478bd9Sstevel@tonic-gate 			if (dipp && *dipp == NULL) {
48037c478bd9Sstevel@tonic-gate 				ndi_hold_devi(child);
48047c478bd9Sstevel@tonic-gate 				*dipp = child;
48057c478bd9Sstevel@tonic-gate 			}
48067c478bd9Sstevel@tonic-gate 			rv = NDI_FAILURE;
48077c478bd9Sstevel@tonic-gate 		}
48087c478bd9Sstevel@tonic-gate 
48097c478bd9Sstevel@tonic-gate 		/*
48107c478bd9Sstevel@tonic-gate 		 * Continue upon failure--best effort algorithm
48117c478bd9Sstevel@tonic-gate 		 */
48127c478bd9Sstevel@tonic-gate 		child = next;
48137c478bd9Sstevel@tonic-gate 	}
48147c478bd9Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
48157c478bd9Sstevel@tonic-gate 	return (rv);
48167c478bd9Sstevel@tonic-gate }
48177c478bd9Sstevel@tonic-gate 
48187c478bd9Sstevel@tonic-gate /*
48197c478bd9Sstevel@tonic-gate  * unconfigure grand children of bus nexus device
48207c478bd9Sstevel@tonic-gate  */
48217c478bd9Sstevel@tonic-gate static int
48227c478bd9Sstevel@tonic-gate unconfig_grand_children(
48237c478bd9Sstevel@tonic-gate 	dev_info_t *dip,
48247c478bd9Sstevel@tonic-gate 	dev_info_t **dipp,
48257c478bd9Sstevel@tonic-gate 	int flags,
48267c478bd9Sstevel@tonic-gate 	major_t major,
48277c478bd9Sstevel@tonic-gate 	struct brevq_node **brevqp)
48287c478bd9Sstevel@tonic-gate {
48297c478bd9Sstevel@tonic-gate 	struct mt_config_handle *hdl;
48307c478bd9Sstevel@tonic-gate 
48317c478bd9Sstevel@tonic-gate 	if (brevqp)
48327c478bd9Sstevel@tonic-gate 		*brevqp = NULL;
48337c478bd9Sstevel@tonic-gate 
48347c478bd9Sstevel@tonic-gate 	/* multi-threaded configuration of child nexus */
48357c478bd9Sstevel@tonic-gate 	hdl = mt_config_init(dip, dipp, flags, major, MT_UNCONFIG_OP, brevqp);
48367c478bd9Sstevel@tonic-gate 	mt_config_children(hdl);
48377c478bd9Sstevel@tonic-gate 
48387c478bd9Sstevel@tonic-gate 	return (mt_config_fini(hdl));	/* wait for threads to exit */
48397c478bd9Sstevel@tonic-gate }
48407c478bd9Sstevel@tonic-gate 
48417c478bd9Sstevel@tonic-gate /*
48427c478bd9Sstevel@tonic-gate  * Unconfigure children/descendants of the dip.
48437c478bd9Sstevel@tonic-gate  *
48447c478bd9Sstevel@tonic-gate  * If brevqp is not NULL, on return *brevqp is set to a queue of dip's
48457c478bd9Sstevel@tonic-gate  * child devinames for which branch remove events need to be generated.
48467c478bd9Sstevel@tonic-gate  */
48477c478bd9Sstevel@tonic-gate static int
48487c478bd9Sstevel@tonic-gate devi_unconfig_common(
48497c478bd9Sstevel@tonic-gate 	dev_info_t *dip,
48507c478bd9Sstevel@tonic-gate 	dev_info_t **dipp,
48517c478bd9Sstevel@tonic-gate 	int flags,
48527c478bd9Sstevel@tonic-gate 	major_t major,
48537c478bd9Sstevel@tonic-gate 	struct brevq_node **brevqp)
48547c478bd9Sstevel@tonic-gate {
48557c478bd9Sstevel@tonic-gate 	int rv;
48567c478bd9Sstevel@tonic-gate 	int pm_cookie;
48577c478bd9Sstevel@tonic-gate 	int (*f)();
48587c478bd9Sstevel@tonic-gate 	ddi_bus_config_op_t bus_op;
48597c478bd9Sstevel@tonic-gate 
48607c478bd9Sstevel@tonic-gate 	if (dipp)
48617c478bd9Sstevel@tonic-gate 		*dipp = NULL;
48627c478bd9Sstevel@tonic-gate 	if (brevqp)
48637c478bd9Sstevel@tonic-gate 		*brevqp = NULL;
48647c478bd9Sstevel@tonic-gate 
48657c478bd9Sstevel@tonic-gate 	/*
48667c478bd9Sstevel@tonic-gate 	 * Power up the dip if it is powered off.  If the flag bit
48677c478bd9Sstevel@tonic-gate 	 * NDI_AUTODETACH is set and the dip is not at its full power,
48687c478bd9Sstevel@tonic-gate 	 * skip the rest of the branch.
48697c478bd9Sstevel@tonic-gate 	 */
48707c478bd9Sstevel@tonic-gate 	if (pm_pre_unconfig(dip, flags, &pm_cookie, NULL) != DDI_SUCCESS)
48717c478bd9Sstevel@tonic-gate 		return ((flags & NDI_AUTODETACH) ? NDI_SUCCESS :
48727c478bd9Sstevel@tonic-gate 		    NDI_FAILURE);
48737c478bd9Sstevel@tonic-gate 
48747c478bd9Sstevel@tonic-gate 	/*
48757c478bd9Sstevel@tonic-gate 	 * Some callers, notably SCSI, need to clear out the devfs
48767c478bd9Sstevel@tonic-gate 	 * cache together with the unconfig to prevent stale entries.
48777c478bd9Sstevel@tonic-gate 	 */
48787c478bd9Sstevel@tonic-gate 	if (flags & NDI_DEVFS_CLEAN)
48797c478bd9Sstevel@tonic-gate 		(void) devfs_clean(dip, NULL, 0);
48807c478bd9Sstevel@tonic-gate 
48817c478bd9Sstevel@tonic-gate 	rv = unconfig_grand_children(dip, dipp, flags, major, brevqp);
48827c478bd9Sstevel@tonic-gate 
48837c478bd9Sstevel@tonic-gate 	if ((rv != NDI_SUCCESS) && ((flags & NDI_AUTODETACH) == 0)) {
48847c478bd9Sstevel@tonic-gate 		if (brevqp && *brevqp) {
48857c478bd9Sstevel@tonic-gate 			log_and_free_br_events_on_grand_children(dip, *brevqp);
48867c478bd9Sstevel@tonic-gate 			free_brevq(*brevqp);
48877c478bd9Sstevel@tonic-gate 			*brevqp = NULL;
48887c478bd9Sstevel@tonic-gate 		}
48897c478bd9Sstevel@tonic-gate 		pm_post_unconfig(dip, pm_cookie, NULL);
48907c478bd9Sstevel@tonic-gate 		return (rv);
48917c478bd9Sstevel@tonic-gate 	}
48927c478bd9Sstevel@tonic-gate 
48937c478bd9Sstevel@tonic-gate 	if (dipp && *dipp) {
48947c478bd9Sstevel@tonic-gate 		ndi_rele_devi(*dipp);
48957c478bd9Sstevel@tonic-gate 		*dipp = NULL;
48967c478bd9Sstevel@tonic-gate 	}
48977c478bd9Sstevel@tonic-gate 
48987c478bd9Sstevel@tonic-gate 	/*
48997c478bd9Sstevel@tonic-gate 	 * It is possible to have a detached nexus with children
49007c478bd9Sstevel@tonic-gate 	 * and grandchildren (for example: a branch consisting
49017c478bd9Sstevel@tonic-gate 	 * entirely of bound nodes.) Since the nexus is detached
49027c478bd9Sstevel@tonic-gate 	 * the bus_unconfig entry point cannot be used to remove
49037c478bd9Sstevel@tonic-gate 	 * or unconfigure the descendants.
49047c478bd9Sstevel@tonic-gate 	 */
49057c478bd9Sstevel@tonic-gate 	if (i_ddi_node_state(dip) < DS_ATTACHED ||
49067c478bd9Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
49077c478bd9Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
49087c478bd9Sstevel@tonic-gate 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
49097c478bd9Sstevel@tonic-gate 		rv = unconfig_immediate_children(dip, dipp, flags, major);
49107c478bd9Sstevel@tonic-gate 	} else {
49117c478bd9Sstevel@tonic-gate 		/*
49127c478bd9Sstevel@tonic-gate 		 * call bus_unconfig entry point
49137c478bd9Sstevel@tonic-gate 		 * It should reset nexus flags if unconfigure succeeds.
49147c478bd9Sstevel@tonic-gate 		 */
49157c478bd9Sstevel@tonic-gate 		bus_op = (major == (major_t)-1) ?
49167c478bd9Sstevel@tonic-gate 		    BUS_UNCONFIG_ALL : BUS_UNCONFIG_DRIVER;
49177c478bd9Sstevel@tonic-gate 		rv = (*f)(dip, flags, bus_op, (void *)(uintptr_t)major);
49187c478bd9Sstevel@tonic-gate 	}
49197c478bd9Sstevel@tonic-gate 
49207c478bd9Sstevel@tonic-gate 	pm_post_unconfig(dip, pm_cookie, NULL);
49217c478bd9Sstevel@tonic-gate 
49227c478bd9Sstevel@tonic-gate 	if (brevqp && *brevqp)
49237c478bd9Sstevel@tonic-gate 		cleanup_br_events_on_grand_children(dip, brevqp);
49247c478bd9Sstevel@tonic-gate 
49257c478bd9Sstevel@tonic-gate 	return (rv);
49267c478bd9Sstevel@tonic-gate }
49277c478bd9Sstevel@tonic-gate 
49287c478bd9Sstevel@tonic-gate /*
49297c478bd9Sstevel@tonic-gate  * called by devfs/framework to unconfigure children bound to major
49307c478bd9Sstevel@tonic-gate  * If NDI_AUTODETACH is specified, this is invoked by either the
49317c478bd9Sstevel@tonic-gate  * moduninstall daemon or the modunload -i 0 command.
49327c478bd9Sstevel@tonic-gate  */
49337c478bd9Sstevel@tonic-gate int
49347c478bd9Sstevel@tonic-gate ndi_devi_unconfig_driver(dev_info_t *dip, int flags, major_t major)
49357c478bd9Sstevel@tonic-gate {
49367c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
49377c478bd9Sstevel@tonic-gate 	    "ndi_devi_unconfig_driver: par = %s%d (%p), flags = 0x%x\n",
49387c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
49397c478bd9Sstevel@tonic-gate 
49407c478bd9Sstevel@tonic-gate 	return (devi_unconfig_common(dip, NULL, flags, major, NULL));
49417c478bd9Sstevel@tonic-gate }
49427c478bd9Sstevel@tonic-gate 
49437c478bd9Sstevel@tonic-gate int
49447c478bd9Sstevel@tonic-gate ndi_devi_unconfig(dev_info_t *dip, int flags)
49457c478bd9Sstevel@tonic-gate {
49467c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
49477c478bd9Sstevel@tonic-gate 	    "ndi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
49487c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
49497c478bd9Sstevel@tonic-gate 
49507c478bd9Sstevel@tonic-gate 	return (devi_unconfig_common(dip, NULL, flags, (major_t)-1, NULL));
49517c478bd9Sstevel@tonic-gate }
49527c478bd9Sstevel@tonic-gate 
49537c478bd9Sstevel@tonic-gate int
49547c478bd9Sstevel@tonic-gate e_ddi_devi_unconfig(dev_info_t *dip, dev_info_t **dipp, int flags)
49557c478bd9Sstevel@tonic-gate {
49567c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
49577c478bd9Sstevel@tonic-gate 	    "e_ddi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
49587c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
49597c478bd9Sstevel@tonic-gate 
49607c478bd9Sstevel@tonic-gate 	return (devi_unconfig_common(dip, dipp, flags, (major_t)-1, NULL));
49617c478bd9Sstevel@tonic-gate }
49627c478bd9Sstevel@tonic-gate 
49637c478bd9Sstevel@tonic-gate /*
49647c478bd9Sstevel@tonic-gate  * Unconfigure child by name
49657c478bd9Sstevel@tonic-gate  */
49667c478bd9Sstevel@tonic-gate static int
49677c478bd9Sstevel@tonic-gate devi_unconfig_one(dev_info_t *pdip, char *devnm, int flags)
49687c478bd9Sstevel@tonic-gate {
49697c478bd9Sstevel@tonic-gate 	int rv, circ;
49707c478bd9Sstevel@tonic-gate 	dev_info_t *child;
49717c478bd9Sstevel@tonic-gate 
49727c478bd9Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
49737c478bd9Sstevel@tonic-gate 	child = ndi_devi_findchild(pdip, devnm);
49747c478bd9Sstevel@tonic-gate 	if (child == NULL) {
49757c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
49767c478bd9Sstevel@tonic-gate 		    "devi_unconfig_one: %s not found\n", devnm));
49777c478bd9Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
49787c478bd9Sstevel@tonic-gate 		return (NDI_SUCCESS);
49797c478bd9Sstevel@tonic-gate 	}
49807c478bd9Sstevel@tonic-gate 	rv = devi_detach_node(child, flags);
49817c478bd9Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
49827c478bd9Sstevel@tonic-gate 	return (rv);
49837c478bd9Sstevel@tonic-gate }
49847c478bd9Sstevel@tonic-gate 
49857c478bd9Sstevel@tonic-gate int
49867c478bd9Sstevel@tonic-gate ndi_devi_unconfig_one(
49877c478bd9Sstevel@tonic-gate 	dev_info_t *pdip,
49887c478bd9Sstevel@tonic-gate 	char *devnm,
49897c478bd9Sstevel@tonic-gate 	dev_info_t **dipp,
49907c478bd9Sstevel@tonic-gate 	int flags)
49917c478bd9Sstevel@tonic-gate {
49927c478bd9Sstevel@tonic-gate 	int (*f)();
49937c478bd9Sstevel@tonic-gate 	int circ, rv;
49947c478bd9Sstevel@tonic-gate 	int pm_cookie;
49957c478bd9Sstevel@tonic-gate 	dev_info_t *child;
49967c478bd9Sstevel@tonic-gate 	struct brevq_node *brevq = NULL;
49977c478bd9Sstevel@tonic-gate 
49987c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(pdip) >= DS_ATTACHED);
49997c478bd9Sstevel@tonic-gate 
50007c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
50017c478bd9Sstevel@tonic-gate 	    "ndi_devi_unconfig_one: par = %s%d (%p), child = %s\n",
50027c478bd9Sstevel@tonic-gate 	    ddi_driver_name(pdip), ddi_get_instance(pdip),
50037c478bd9Sstevel@tonic-gate 	    (void *)pdip, devnm));
50047c478bd9Sstevel@tonic-gate 
50057c478bd9Sstevel@tonic-gate 	if (pm_pre_unconfig(pdip, flags, &pm_cookie, devnm) != DDI_SUCCESS)
50067c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
50077c478bd9Sstevel@tonic-gate 
50087c478bd9Sstevel@tonic-gate 	if (dipp)
50097c478bd9Sstevel@tonic-gate 		*dipp = NULL;
50107c478bd9Sstevel@tonic-gate 
50117c478bd9Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
50127c478bd9Sstevel@tonic-gate 	child = ndi_devi_findchild(pdip, devnm);
50137c478bd9Sstevel@tonic-gate 	if (child == NULL) {
50147c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_unconfig_one: %s"
50157c478bd9Sstevel@tonic-gate 		    " not found\n", devnm));
50167c478bd9Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
50177c478bd9Sstevel@tonic-gate 		pm_post_unconfig(pdip, pm_cookie, devnm);
50187c478bd9Sstevel@tonic-gate 		return (NDI_SUCCESS);
50197c478bd9Sstevel@tonic-gate 	}
50207c478bd9Sstevel@tonic-gate 
50217c478bd9Sstevel@tonic-gate 	/*
50227c478bd9Sstevel@tonic-gate 	 * Unconfigure children/descendants of named child
50237c478bd9Sstevel@tonic-gate 	 */
50247c478bd9Sstevel@tonic-gate 	rv = devi_unconfig_branch(child, dipp, flags | NDI_UNCONFIG, &brevq);
50257c478bd9Sstevel@tonic-gate 	if (rv != NDI_SUCCESS)
50267c478bd9Sstevel@tonic-gate 		goto out;
50277c478bd9Sstevel@tonic-gate 
50287c478bd9Sstevel@tonic-gate 	init_bound_node_ev(pdip, child, flags);
50297c478bd9Sstevel@tonic-gate 
50307c478bd9Sstevel@tonic-gate 	if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
50317c478bd9Sstevel@tonic-gate 	    (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
50327c478bd9Sstevel@tonic-gate 	    (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
50337c478bd9Sstevel@tonic-gate 		rv = devi_detach_node(child, flags);
50347c478bd9Sstevel@tonic-gate 	} else {
50357c478bd9Sstevel@tonic-gate 		/* call bus_config entry point */
50367c478bd9Sstevel@tonic-gate 		rv = (*f)(pdip, flags, BUS_UNCONFIG_ONE, (void *)devnm);
50377c478bd9Sstevel@tonic-gate 	}
50387c478bd9Sstevel@tonic-gate 
50397c478bd9Sstevel@tonic-gate 	if (brevq) {
50407c478bd9Sstevel@tonic-gate 		if (rv != NDI_SUCCESS)
50417c478bd9Sstevel@tonic-gate 			log_and_free_brevq_dip(child, brevq);
50427c478bd9Sstevel@tonic-gate 		else
50437c478bd9Sstevel@tonic-gate 			free_brevq(brevq);
50447c478bd9Sstevel@tonic-gate 	}
50457c478bd9Sstevel@tonic-gate 
50467c478bd9Sstevel@tonic-gate 	if (dipp && rv != NDI_SUCCESS) {
50477c478bd9Sstevel@tonic-gate 		ndi_hold_devi(child);
50487c478bd9Sstevel@tonic-gate 		ASSERT(*dipp == NULL);
50497c478bd9Sstevel@tonic-gate 		*dipp = child;
50507c478bd9Sstevel@tonic-gate 	}
50517c478bd9Sstevel@tonic-gate 
50527c478bd9Sstevel@tonic-gate out:
50537c478bd9Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
50547c478bd9Sstevel@tonic-gate 	pm_post_unconfig(pdip, pm_cookie, devnm);
50557c478bd9Sstevel@tonic-gate 
50567c478bd9Sstevel@tonic-gate 	return (rv);
50577c478bd9Sstevel@tonic-gate }
50587c478bd9Sstevel@tonic-gate 
50597c478bd9Sstevel@tonic-gate struct async_arg {
50607c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
50617c478bd9Sstevel@tonic-gate 	uint_t flags;
50627c478bd9Sstevel@tonic-gate };
50637c478bd9Sstevel@tonic-gate 
50647c478bd9Sstevel@tonic-gate /*
50657c478bd9Sstevel@tonic-gate  * Common async handler for:
50667c478bd9Sstevel@tonic-gate  *	ndi_devi_bind_driver_async
50677c478bd9Sstevel@tonic-gate  *	ndi_devi_online_async
50687c478bd9Sstevel@tonic-gate  */
50697c478bd9Sstevel@tonic-gate static int
50707c478bd9Sstevel@tonic-gate i_ndi_devi_async_common(dev_info_t *dip, uint_t flags, void (*func)())
50717c478bd9Sstevel@tonic-gate {
50727c478bd9Sstevel@tonic-gate 	int tqflag;
50737c478bd9Sstevel@tonic-gate 	int kmflag;
50747c478bd9Sstevel@tonic-gate 	struct async_arg *arg;
50757c478bd9Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
50767c478bd9Sstevel@tonic-gate 
50777c478bd9Sstevel@tonic-gate 	ASSERT(pdip);
50787c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(pdip)->devi_taskq);
50797c478bd9Sstevel@tonic-gate 	ASSERT(ndi_dev_is_persistent_node(dip));
50807c478bd9Sstevel@tonic-gate 
50817c478bd9Sstevel@tonic-gate 	if (flags & NDI_NOSLEEP) {
50827c478bd9Sstevel@tonic-gate 		kmflag = KM_NOSLEEP;
50837c478bd9Sstevel@tonic-gate 		tqflag = TQ_NOSLEEP;
50847c478bd9Sstevel@tonic-gate 	} else {
50857c478bd9Sstevel@tonic-gate 		kmflag = KM_SLEEP;
50867c478bd9Sstevel@tonic-gate 		tqflag = TQ_SLEEP;
50877c478bd9Sstevel@tonic-gate 	}
50887c478bd9Sstevel@tonic-gate 
50897c478bd9Sstevel@tonic-gate 	arg = kmem_alloc(sizeof (*arg), kmflag);
50907c478bd9Sstevel@tonic-gate 	if (arg == NULL)
50917c478bd9Sstevel@tonic-gate 		goto fail;
50927c478bd9Sstevel@tonic-gate 
50937c478bd9Sstevel@tonic-gate 	arg->flags = flags;
50947c478bd9Sstevel@tonic-gate 	arg->dip = dip;
50957c478bd9Sstevel@tonic-gate 	if (ddi_taskq_dispatch(DEVI(pdip)->devi_taskq, func, arg, tqflag) ==
50967c478bd9Sstevel@tonic-gate 	    DDI_SUCCESS) {
50977c478bd9Sstevel@tonic-gate 		return (NDI_SUCCESS);
50987c478bd9Sstevel@tonic-gate 	}
50997c478bd9Sstevel@tonic-gate 
51007c478bd9Sstevel@tonic-gate fail:
51017c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "%s%d: ddi_taskq_dispatch failed",
51027c478bd9Sstevel@tonic-gate 	    ddi_driver_name(pdip), ddi_get_instance(pdip)));
51037c478bd9Sstevel@tonic-gate 
51047c478bd9Sstevel@tonic-gate 	if (arg)
51057c478bd9Sstevel@tonic-gate 		kmem_free(arg, sizeof (*arg));
51067c478bd9Sstevel@tonic-gate 	return (NDI_FAILURE);
51077c478bd9Sstevel@tonic-gate }
51087c478bd9Sstevel@tonic-gate 
51097c478bd9Sstevel@tonic-gate static void
51107c478bd9Sstevel@tonic-gate i_ndi_devi_bind_driver_cb(struct async_arg *arg)
51117c478bd9Sstevel@tonic-gate {
51127c478bd9Sstevel@tonic-gate 	(void) ndi_devi_bind_driver(arg->dip, arg->flags);
51137c478bd9Sstevel@tonic-gate 	kmem_free(arg, sizeof (*arg));
51147c478bd9Sstevel@tonic-gate }
51157c478bd9Sstevel@tonic-gate 
51167c478bd9Sstevel@tonic-gate int
51177c478bd9Sstevel@tonic-gate ndi_devi_bind_driver_async(dev_info_t *dip, uint_t flags)
51187c478bd9Sstevel@tonic-gate {
51197c478bd9Sstevel@tonic-gate 	return (i_ndi_devi_async_common(dip, flags,
51207c478bd9Sstevel@tonic-gate 	    (void (*)())i_ndi_devi_bind_driver_cb));
51217c478bd9Sstevel@tonic-gate }
51227c478bd9Sstevel@tonic-gate 
51237c478bd9Sstevel@tonic-gate /*
51247c478bd9Sstevel@tonic-gate  * place the devinfo in the ONLINE state.
51257c478bd9Sstevel@tonic-gate  */
51267c478bd9Sstevel@tonic-gate int
51277c478bd9Sstevel@tonic-gate ndi_devi_online(dev_info_t *dip, uint_t flags)
51287c478bd9Sstevel@tonic-gate {
51297c478bd9Sstevel@tonic-gate 	int circ, rv;
51307c478bd9Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
51317c478bd9Sstevel@tonic-gate 	int branch_event = 0;
51327c478bd9Sstevel@tonic-gate 
51337c478bd9Sstevel@tonic-gate 	ASSERT(pdip);
51347c478bd9Sstevel@tonic-gate 
51357c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_online: %s%d (%p)\n",
51367c478bd9Sstevel@tonic-gate 		ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
51377c478bd9Sstevel@tonic-gate 
51387c478bd9Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
51397c478bd9Sstevel@tonic-gate 	/* bind child before merging .conf nodes */
51407c478bd9Sstevel@tonic-gate 	rv = i_ndi_config_node(dip, DS_BOUND, flags);
51417c478bd9Sstevel@tonic-gate 	if (rv != NDI_SUCCESS) {
51427c478bd9Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
51437c478bd9Sstevel@tonic-gate 		return (rv);
51447c478bd9Sstevel@tonic-gate 	}
51457c478bd9Sstevel@tonic-gate 
51467c478bd9Sstevel@tonic-gate 	/* merge .conf properties */
51477c478bd9Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(pdip, flags);
51487c478bd9Sstevel@tonic-gate 
51497c478bd9Sstevel@tonic-gate 	flags |= (NDI_DEVI_ONLINE | NDI_CONFIG);
51507c478bd9Sstevel@tonic-gate 
51517c478bd9Sstevel@tonic-gate 	if (flags & NDI_NO_EVENT) {
51527c478bd9Sstevel@tonic-gate 		/*
51537c478bd9Sstevel@tonic-gate 		 * Caller is specifically asking for not to generate an event.
51547c478bd9Sstevel@tonic-gate 		 * Set the following flag so that devi_attach_node() don't
51557c478bd9Sstevel@tonic-gate 		 * change the event state.
51567c478bd9Sstevel@tonic-gate 		 */
51577c478bd9Sstevel@tonic-gate 		flags |= NDI_NO_EVENT_STATE_CHNG;
51587c478bd9Sstevel@tonic-gate 	}
51597c478bd9Sstevel@tonic-gate 
51607c478bd9Sstevel@tonic-gate 	if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
51617c478bd9Sstevel@tonic-gate 	    ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip))) {
51627c478bd9Sstevel@tonic-gate 		flags |= NDI_BRANCH_EVENT_OP;
51637c478bd9Sstevel@tonic-gate 		branch_event = 1;
51647c478bd9Sstevel@tonic-gate 	}
51657c478bd9Sstevel@tonic-gate 
51667c478bd9Sstevel@tonic-gate 	/*
51677c478bd9Sstevel@tonic-gate 	 * devi_attach_node() may remove dip on failure
51687c478bd9Sstevel@tonic-gate 	 */
51697c478bd9Sstevel@tonic-gate 	if ((rv = devi_attach_node(dip, flags)) == NDI_SUCCESS) {
51707c478bd9Sstevel@tonic-gate 		if ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip)) {
51717c478bd9Sstevel@tonic-gate 			(void) ndi_devi_config(dip, flags);
51727c478bd9Sstevel@tonic-gate 		}
51737c478bd9Sstevel@tonic-gate 
51747c478bd9Sstevel@tonic-gate 		if (branch_event)
51757c478bd9Sstevel@tonic-gate 			(void) i_log_devfs_branch_add(dip);
51767c478bd9Sstevel@tonic-gate 	}
51777c478bd9Sstevel@tonic-gate 
51787c478bd9Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
51797c478bd9Sstevel@tonic-gate 
51807c478bd9Sstevel@tonic-gate 	/*
51817c478bd9Sstevel@tonic-gate 	 * Notify devfs that we have a new node. Devfs needs to invalidate
51827c478bd9Sstevel@tonic-gate 	 * cached directory contents.
51837c478bd9Sstevel@tonic-gate 	 *
51847c478bd9Sstevel@tonic-gate 	 * For PCMCIA devices, it is possible the pdip is not fully
51857c478bd9Sstevel@tonic-gate 	 * attached. In this case, calling back into devfs will
51867c478bd9Sstevel@tonic-gate 	 * result in a loop or assertion error. Hence, the check
51877c478bd9Sstevel@tonic-gate 	 * on node state.
51887c478bd9Sstevel@tonic-gate 	 *
51897c478bd9Sstevel@tonic-gate 	 * If we own parent lock, this is part of a branch operation.
51907c478bd9Sstevel@tonic-gate 	 * We skip the devfs_clean() step because the cache invalidation
51917c478bd9Sstevel@tonic-gate 	 * is done higher up in the device tree.
51927c478bd9Sstevel@tonic-gate 	 */
51937c478bd9Sstevel@tonic-gate 	if (rv == NDI_SUCCESS && i_ddi_node_state(pdip) == DS_READY &&
51947c478bd9Sstevel@tonic-gate 	    !DEVI_BUSY_OWNED(pdip))
51957c478bd9Sstevel@tonic-gate 		(void) devfs_clean(pdip, NULL, 0);
51967c478bd9Sstevel@tonic-gate 	return (rv);
51977c478bd9Sstevel@tonic-gate }
51987c478bd9Sstevel@tonic-gate 
51997c478bd9Sstevel@tonic-gate static void
52007c478bd9Sstevel@tonic-gate i_ndi_devi_online_cb(struct async_arg *arg)
52017c478bd9Sstevel@tonic-gate {
52027c478bd9Sstevel@tonic-gate 	(void) ndi_devi_online(arg->dip, arg->flags);
52037c478bd9Sstevel@tonic-gate 	kmem_free(arg, sizeof (*arg));
52047c478bd9Sstevel@tonic-gate }
52057c478bd9Sstevel@tonic-gate 
52067c478bd9Sstevel@tonic-gate int
52077c478bd9Sstevel@tonic-gate ndi_devi_online_async(dev_info_t *dip, uint_t flags)
52087c478bd9Sstevel@tonic-gate {
52097c478bd9Sstevel@tonic-gate 	/* mark child as need config if requested. */
521016747f41Scth 	if (flags & NDI_CONFIG) {
521116747f41Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
52127c478bd9Sstevel@tonic-gate 		DEVI_SET_NDI_CONFIG(dip);
521316747f41Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
521416747f41Scth 	}
52157c478bd9Sstevel@tonic-gate 
52167c478bd9Sstevel@tonic-gate 	return (i_ndi_devi_async_common(dip, flags,
52177c478bd9Sstevel@tonic-gate 	    (void (*)())i_ndi_devi_online_cb));
52187c478bd9Sstevel@tonic-gate }
52197c478bd9Sstevel@tonic-gate 
52207c478bd9Sstevel@tonic-gate /*
52217c478bd9Sstevel@tonic-gate  * Take a device node Offline
52227c478bd9Sstevel@tonic-gate  * To take a device Offline means to detach the device instance from
52237c478bd9Sstevel@tonic-gate  * the driver and prevent devfs requests from re-attaching the device
52247c478bd9Sstevel@tonic-gate  * instance.
52257c478bd9Sstevel@tonic-gate  *
52267c478bd9Sstevel@tonic-gate  * The flag NDI_DEVI_REMOVE causes removes the device node from
52277c478bd9Sstevel@tonic-gate  * the driver list and the device tree. In this case, the device
52287c478bd9Sstevel@tonic-gate  * is assumed to be removed from the system.
52297c478bd9Sstevel@tonic-gate  */
52307c478bd9Sstevel@tonic-gate int
52317c478bd9Sstevel@tonic-gate ndi_devi_offline(dev_info_t *dip, uint_t flags)
52327c478bd9Sstevel@tonic-gate {
52337c478bd9Sstevel@tonic-gate 	int circ, rval = 0;
52347c478bd9Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
52357c478bd9Sstevel@tonic-gate 	struct brevq_node *brevq = NULL;
52367c478bd9Sstevel@tonic-gate 
52377c478bd9Sstevel@tonic-gate 	ASSERT(pdip);
52387c478bd9Sstevel@tonic-gate 
52397c478bd9Sstevel@tonic-gate 	flags |= NDI_DEVI_OFFLINE;
52407c478bd9Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
52417c478bd9Sstevel@tonic-gate 	if (i_ddi_node_state(dip) == DS_READY) {
52427c478bd9Sstevel@tonic-gate 		/*
52437c478bd9Sstevel@tonic-gate 		 * If dip is in DS_READY state, there may be cached dv_nodes
52447c478bd9Sstevel@tonic-gate 		 * referencing this dip, so we invoke devfs code path.
52457c478bd9Sstevel@tonic-gate 		 * Note that we must release busy changing on pdip to
52467c478bd9Sstevel@tonic-gate 		 * avoid deadlock against devfs.
52477c478bd9Sstevel@tonic-gate 		 */
52487c478bd9Sstevel@tonic-gate 		char *devname = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
52497c478bd9Sstevel@tonic-gate 		(void) ddi_deviname(dip, devname);
52507c478bd9Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
52517c478bd9Sstevel@tonic-gate 
52527c478bd9Sstevel@tonic-gate 		/*
52537c478bd9Sstevel@tonic-gate 		 * If we own parent lock, this is part of a branch
52547c478bd9Sstevel@tonic-gate 		 * operation. We skip the devfs_clean() step.
52557c478bd9Sstevel@tonic-gate 		 */
52567c478bd9Sstevel@tonic-gate 		if (!DEVI_BUSY_OWNED(pdip))
52577c478bd9Sstevel@tonic-gate 			rval = devfs_clean(pdip, devname + 1, DV_CLEAN_FORCE);
52587c478bd9Sstevel@tonic-gate 		kmem_free(devname, MAXNAMELEN + 1);
52597c478bd9Sstevel@tonic-gate 
52607c478bd9Sstevel@tonic-gate 		if (rval == 0)
52617c478bd9Sstevel@tonic-gate 			rval = devi_unconfig_branch(dip, NULL,
52627c478bd9Sstevel@tonic-gate 			    flags|NDI_UNCONFIG, &brevq);
52637c478bd9Sstevel@tonic-gate 		if (rval)
52647c478bd9Sstevel@tonic-gate 			return (NDI_FAILURE);
52657c478bd9Sstevel@tonic-gate 
52667c478bd9Sstevel@tonic-gate 		ndi_devi_enter(pdip, &circ);
52677c478bd9Sstevel@tonic-gate 	}
52687c478bd9Sstevel@tonic-gate 
52697c478bd9Sstevel@tonic-gate 	init_bound_node_ev(pdip, dip, flags);
52707c478bd9Sstevel@tonic-gate 
52717c478bd9Sstevel@tonic-gate 	rval = devi_detach_node(dip, flags);
52727c478bd9Sstevel@tonic-gate 	if (brevq) {
52737c478bd9Sstevel@tonic-gate 		if (rval != NDI_SUCCESS)
52747c478bd9Sstevel@tonic-gate 			log_and_free_brevq_dip(dip, brevq);
52757c478bd9Sstevel@tonic-gate 		else
52767c478bd9Sstevel@tonic-gate 			free_brevq(brevq);
52777c478bd9Sstevel@tonic-gate 	}
52787c478bd9Sstevel@tonic-gate 
52797c478bd9Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
52807c478bd9Sstevel@tonic-gate 
52817c478bd9Sstevel@tonic-gate 	return (rval);
52827c478bd9Sstevel@tonic-gate }
52837c478bd9Sstevel@tonic-gate 
52847c478bd9Sstevel@tonic-gate /*
52857c478bd9Sstevel@tonic-gate  * Find the child dev_info node of parent nexus 'p' whose name
52867c478bd9Sstevel@tonic-gate  * matches "cname@caddr".  Recommend use of ndi_devi_findchild() instead.
52877c478bd9Sstevel@tonic-gate  */
52887c478bd9Sstevel@tonic-gate dev_info_t *
52897c478bd9Sstevel@tonic-gate ndi_devi_find(dev_info_t *pdip, char *cname, char *caddr)
52907c478bd9Sstevel@tonic-gate {
52917c478bd9Sstevel@tonic-gate 	dev_info_t *child;
52927c478bd9Sstevel@tonic-gate 	int circ;
52937c478bd9Sstevel@tonic-gate 
52947c478bd9Sstevel@tonic-gate 	if (pdip == NULL || cname == NULL || caddr == NULL)
52957c478bd9Sstevel@tonic-gate 		return ((dev_info_t *)NULL);
52967c478bd9Sstevel@tonic-gate 
52977c478bd9Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
52987c478bd9Sstevel@tonic-gate 	child = find_sibling(ddi_get_child(pdip), cname, caddr, 0, NULL);
52997c478bd9Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
53007c478bd9Sstevel@tonic-gate 	return (child);
53017c478bd9Sstevel@tonic-gate }
53027c478bd9Sstevel@tonic-gate 
53037c478bd9Sstevel@tonic-gate /*
53047c478bd9Sstevel@tonic-gate  * Find the child dev_info node of parent nexus 'p' whose name
53057c478bd9Sstevel@tonic-gate  * matches devname "name@addr".  Permits caller to hold the parent.
53067c478bd9Sstevel@tonic-gate  */
53077c478bd9Sstevel@tonic-gate dev_info_t *
53087c478bd9Sstevel@tonic-gate ndi_devi_findchild(dev_info_t *pdip, char *devname)
53097c478bd9Sstevel@tonic-gate {
53107c478bd9Sstevel@tonic-gate 	dev_info_t *child;
53117c478bd9Sstevel@tonic-gate 	char	*cname, *caddr;
53127c478bd9Sstevel@tonic-gate 	char	*devstr;
53137c478bd9Sstevel@tonic-gate 
53147c478bd9Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(pdip));
53157c478bd9Sstevel@tonic-gate 
53167c478bd9Sstevel@tonic-gate 	devstr = i_ddi_strdup(devname, KM_SLEEP);
53177c478bd9Sstevel@tonic-gate 	i_ddi_parse_name(devstr, &cname, &caddr, NULL);
53187c478bd9Sstevel@tonic-gate 
53197c478bd9Sstevel@tonic-gate 	if (cname == NULL || caddr == NULL) {
53207c478bd9Sstevel@tonic-gate 		kmem_free(devstr, strlen(devname)+1);
53217c478bd9Sstevel@tonic-gate 		return ((dev_info_t *)NULL);
53227c478bd9Sstevel@tonic-gate 	}
53237c478bd9Sstevel@tonic-gate 
53247c478bd9Sstevel@tonic-gate 	child = find_sibling(ddi_get_child(pdip), cname, caddr, 0, NULL);
53257c478bd9Sstevel@tonic-gate 	kmem_free(devstr, strlen(devname)+1);
53267c478bd9Sstevel@tonic-gate 	return (child);
53277c478bd9Sstevel@tonic-gate }
53287c478bd9Sstevel@tonic-gate 
53297c478bd9Sstevel@tonic-gate /*
53307c478bd9Sstevel@tonic-gate  * Misc. routines called by framework only
53317c478bd9Sstevel@tonic-gate  */
53327c478bd9Sstevel@tonic-gate 
53337c478bd9Sstevel@tonic-gate /*
53347c478bd9Sstevel@tonic-gate  * Clear the DEVI_MADE_CHILDREN/DEVI_ATTACHED_CHILDREN flags
53357c478bd9Sstevel@tonic-gate  * if new child spec has been added.
53367c478bd9Sstevel@tonic-gate  */
53377c478bd9Sstevel@tonic-gate static int
53387c478bd9Sstevel@tonic-gate reset_nexus_flags(dev_info_t *dip, void *arg)
53397c478bd9Sstevel@tonic-gate {
53407c478bd9Sstevel@tonic-gate 	struct hwc_spec	*list;
53416a41d557Scth 	int		circ;
53427c478bd9Sstevel@tonic-gate 
53437c478bd9Sstevel@tonic-gate 	if (((DEVI(dip)->devi_flags & DEVI_MADE_CHILDREN) == 0) ||
53447c478bd9Sstevel@tonic-gate 	    ((list = hwc_get_child_spec(dip, (major_t)(uintptr_t)arg)) == NULL))
53457c478bd9Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
53467c478bd9Sstevel@tonic-gate 
53477c478bd9Sstevel@tonic-gate 	hwc_free_spec_list(list);
53486a41d557Scth 
53496a41d557Scth 	/* coordinate child state update */
53506a41d557Scth 	ndi_devi_enter(dip, &circ);
53517c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
53527c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN | DEVI_ATTACHED_CHILDREN);
53537c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
53546a41d557Scth 	ndi_devi_exit(dip, circ);
53557c478bd9Sstevel@tonic-gate 
53567c478bd9Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
53577c478bd9Sstevel@tonic-gate }
53587c478bd9Sstevel@tonic-gate 
53597c478bd9Sstevel@tonic-gate /*
53607c478bd9Sstevel@tonic-gate  * Helper functions, returns NULL if no memory.
53617c478bd9Sstevel@tonic-gate  */
53627c478bd9Sstevel@tonic-gate 
53637c478bd9Sstevel@tonic-gate /*
53647c478bd9Sstevel@tonic-gate  * path_to_major:
53657c478bd9Sstevel@tonic-gate  *
53667c478bd9Sstevel@tonic-gate  * Return an alternate driver name binding for the leaf device
53677c478bd9Sstevel@tonic-gate  * of the given pathname, if there is one. The purpose of this
53687c478bd9Sstevel@tonic-gate  * function is to deal with generic pathnames. The default action
53697c478bd9Sstevel@tonic-gate  * for platforms that can't do this (ie: x86 or any platform that
53707c478bd9Sstevel@tonic-gate  * does not have prom_finddevice functionality, which matches
53717c478bd9Sstevel@tonic-gate  * nodenames and unit-addresses without the drivers participation)
53727c478bd9Sstevel@tonic-gate  * is to return (major_t)-1.
53737c478bd9Sstevel@tonic-gate  *
53747c478bd9Sstevel@tonic-gate  * Used in loadrootmodules() in the swapgeneric module to
53757c478bd9Sstevel@tonic-gate  * associate a given pathname with a given leaf driver.
53767c478bd9Sstevel@tonic-gate  *
53777c478bd9Sstevel@tonic-gate  */
53787c478bd9Sstevel@tonic-gate major_t
53797c478bd9Sstevel@tonic-gate path_to_major(char *path)
53807c478bd9Sstevel@tonic-gate {
53817c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
53827c478bd9Sstevel@tonic-gate 	char *p, *q;
5383fa9e4066Sahrens 	pnode_t nodeid;
53847c478bd9Sstevel@tonic-gate 	major_t maj;
53857c478bd9Sstevel@tonic-gate 
53867c478bd9Sstevel@tonic-gate 	/*
53877c478bd9Sstevel@tonic-gate 	 * Get the nodeid of the given pathname, if such a mapping exists.
53887c478bd9Sstevel@tonic-gate 	 */
53897c478bd9Sstevel@tonic-gate 	dip = NULL;
53907c478bd9Sstevel@tonic-gate 	nodeid = prom_finddevice(path);
53917c478bd9Sstevel@tonic-gate 	if (nodeid != OBP_BADNODE) {
53927c478bd9Sstevel@tonic-gate 		/*
53937c478bd9Sstevel@tonic-gate 		 * Find the nodeid in our copy of the device tree and return
53947c478bd9Sstevel@tonic-gate 		 * whatever name we used to bind this node to a driver.
53957c478bd9Sstevel@tonic-gate 		 */
53967c478bd9Sstevel@tonic-gate 		dip = e_ddi_nodeid_to_dip(nodeid);
53977c478bd9Sstevel@tonic-gate 	}
53987c478bd9Sstevel@tonic-gate 
53997c478bd9Sstevel@tonic-gate 	if (dip == NULL) {
54007c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_WARN,
54017c478bd9Sstevel@tonic-gate 		    "path_to_major: can't bind <%s>\n", path));
54027c478bd9Sstevel@tonic-gate 		return ((major_t)-1);
54037c478bd9Sstevel@tonic-gate 	}
54047c478bd9Sstevel@tonic-gate 
54057c478bd9Sstevel@tonic-gate 	/*
54067c478bd9Sstevel@tonic-gate 	 * If we're bound to something other than the nodename,
54077c478bd9Sstevel@tonic-gate 	 * note that in the message buffer and system log.
54087c478bd9Sstevel@tonic-gate 	 */
54097c478bd9Sstevel@tonic-gate 	p = ddi_binding_name(dip);
54107c478bd9Sstevel@tonic-gate 	q = ddi_node_name(dip);
54117c478bd9Sstevel@tonic-gate 	if (p && q && (strcmp(p, q) != 0))
54127c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s bound to %s\n",
54137c478bd9Sstevel@tonic-gate 		    path, p));
54147c478bd9Sstevel@tonic-gate 
54157c478bd9Sstevel@tonic-gate 	maj = ddi_name_to_major(p);
54167c478bd9Sstevel@tonic-gate 
54177c478bd9Sstevel@tonic-gate 	ndi_rele_devi(dip);		/* release node held during walk */
54187c478bd9Sstevel@tonic-gate 
54197c478bd9Sstevel@tonic-gate 	return (maj);
54207c478bd9Sstevel@tonic-gate }
54217c478bd9Sstevel@tonic-gate 
54227c478bd9Sstevel@tonic-gate /*
54237c478bd9Sstevel@tonic-gate  * Return the held dip for the specified major and instance, attempting to do
54247c478bd9Sstevel@tonic-gate  * an attach if specified. Return NULL if the devi can't be found or put in
54257c478bd9Sstevel@tonic-gate  * the proper state. The caller must release the hold via ddi_release_devi if
54267c478bd9Sstevel@tonic-gate  * a non-NULL value is returned.
54277c478bd9Sstevel@tonic-gate  *
54287c478bd9Sstevel@tonic-gate  * Some callers expect to be able to perform a hold_devi() while in a context
54297c478bd9Sstevel@tonic-gate  * where using ndi_devi_enter() to ensure the hold might cause deadlock (see
54307c478bd9Sstevel@tonic-gate  * open-from-attach code in consconfig_dacf.c). Such special-case callers
54317c478bd9Sstevel@tonic-gate  * must ensure that an ndi_devi_enter(parent)/ndi_devi_hold() from a safe
54327c478bd9Sstevel@tonic-gate  * context is already active. The hold_devi() implementation must accommodate
54337c478bd9Sstevel@tonic-gate  * these callers.
54347c478bd9Sstevel@tonic-gate  */
54357c478bd9Sstevel@tonic-gate static dev_info_t *
54367c478bd9Sstevel@tonic-gate hold_devi(major_t major, int instance, int flags)
54377c478bd9Sstevel@tonic-gate {
54387c478bd9Sstevel@tonic-gate 	struct devnames	*dnp;
54397c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
54407c478bd9Sstevel@tonic-gate 	char		*path;
54417c478bd9Sstevel@tonic-gate 
54427c478bd9Sstevel@tonic-gate 	if ((major >= devcnt) || (instance == -1))
54437c478bd9Sstevel@tonic-gate 		return (NULL);
54447c478bd9Sstevel@tonic-gate 
54457c478bd9Sstevel@tonic-gate 	/* try to find the instance in the per driver list */
54467c478bd9Sstevel@tonic-gate 	dnp = &(devnamesp[major]);
54477c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&(dnp->dn_lock));
54487c478bd9Sstevel@tonic-gate 	for (dip = dnp->dn_head; dip;
54497c478bd9Sstevel@tonic-gate 	    dip = (dev_info_t *)DEVI(dip)->devi_next) {
54507c478bd9Sstevel@tonic-gate 		/* skip node if instance field is not valid */
54517c478bd9Sstevel@tonic-gate 		if (i_ddi_node_state(dip) < DS_INITIALIZED)
54527c478bd9Sstevel@tonic-gate 			continue;
54537c478bd9Sstevel@tonic-gate 
54547c478bd9Sstevel@tonic-gate 		/* look for instance match */
54557c478bd9Sstevel@tonic-gate 		if (DEVI(dip)->devi_instance == instance) {
54567c478bd9Sstevel@tonic-gate 			/*
54577c478bd9Sstevel@tonic-gate 			 * To accommodate callers that can't block in
54587c478bd9Sstevel@tonic-gate 			 * ndi_devi_enter() we do an ndi_devi_hold(), and
54597c478bd9Sstevel@tonic-gate 			 * afterwards check that the node is in a state where
54607c478bd9Sstevel@tonic-gate 			 * the hold prevents detach(). If we did not manage to
54617c478bd9Sstevel@tonic-gate 			 * prevent detach then we ndi_rele_devi() and perform
54627c478bd9Sstevel@tonic-gate 			 * the slow path below (which can result in a blocking
54637c478bd9Sstevel@tonic-gate 			 * ndi_devi_enter() while driving attach top-down).
54647c478bd9Sstevel@tonic-gate 			 * This code depends on the ordering of
54657c478bd9Sstevel@tonic-gate 			 * DEVI_SET_DETACHING and the devi_ref check in the
54667c478bd9Sstevel@tonic-gate 			 * detach_node() code path.
54677c478bd9Sstevel@tonic-gate 			 */
54687c478bd9Sstevel@tonic-gate 			ndi_hold_devi(dip);
54697c478bd9Sstevel@tonic-gate 			if ((i_ddi_node_state(dip) >= DS_ATTACHED) &&
54707c478bd9Sstevel@tonic-gate 			    !DEVI_IS_DETACHING(dip)) {
54717c478bd9Sstevel@tonic-gate 				UNLOCK_DEV_OPS(&(dnp->dn_lock));
54727c478bd9Sstevel@tonic-gate 				return (dip);	/* fast-path with devi held */
54737c478bd9Sstevel@tonic-gate 			}
54747c478bd9Sstevel@tonic-gate 			ndi_rele_devi(dip);
54757c478bd9Sstevel@tonic-gate 
54767c478bd9Sstevel@tonic-gate 			/* try slow-path */
54777c478bd9Sstevel@tonic-gate 			dip = NULL;
54787c478bd9Sstevel@tonic-gate 			break;
54797c478bd9Sstevel@tonic-gate 		}
54807c478bd9Sstevel@tonic-gate 	}
54817c478bd9Sstevel@tonic-gate 	ASSERT(dip == NULL);
54827c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
54837c478bd9Sstevel@tonic-gate 
54847c478bd9Sstevel@tonic-gate 	if (flags & E_DDI_HOLD_DEVI_NOATTACH)
54857c478bd9Sstevel@tonic-gate 		return (NULL);		/* told not to drive attach */
54867c478bd9Sstevel@tonic-gate 
54877c478bd9Sstevel@tonic-gate 	/* slow-path may block, so it should not occur from interrupt */
54887c478bd9Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
54897c478bd9Sstevel@tonic-gate 	if (servicing_interrupt())
54907c478bd9Sstevel@tonic-gate 		return (NULL);
54917c478bd9Sstevel@tonic-gate 
54927c478bd9Sstevel@tonic-gate 	/* reconstruct the path and drive attach by path through devfs. */
54937c478bd9Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
54947c478bd9Sstevel@tonic-gate 	if (e_ddi_majorinstance_to_path(major, instance, path) == 0)
54957c478bd9Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_path(path, flags);
54967c478bd9Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
54977c478bd9Sstevel@tonic-gate 	return (dip);			/* with devi held */
54987c478bd9Sstevel@tonic-gate }
54997c478bd9Sstevel@tonic-gate 
55007c478bd9Sstevel@tonic-gate /*
55017c478bd9Sstevel@tonic-gate  * The {e_}ddi_hold_devi{_by_{instance|dev|path}} hold the devinfo node
55027c478bd9Sstevel@tonic-gate  * associated with the specified arguments.  This hold should be released
55037c478bd9Sstevel@tonic-gate  * by calling ddi_release_devi.
55047c478bd9Sstevel@tonic-gate  *
55057c478bd9Sstevel@tonic-gate  * The E_DDI_HOLD_DEVI_NOATTACH flag argument allows the caller to to specify
55067c478bd9Sstevel@tonic-gate  * a failure return if the node is not already attached.
55077c478bd9Sstevel@tonic-gate  *
55087c478bd9Sstevel@tonic-gate  * NOTE: by the time we make e_ddi_hold_devi public, we should be able to reuse
55097c478bd9Sstevel@tonic-gate  * ddi_hold_devi again.
55107c478bd9Sstevel@tonic-gate  */
55117c478bd9Sstevel@tonic-gate dev_info_t *
55127c478bd9Sstevel@tonic-gate ddi_hold_devi_by_instance(major_t major, int instance, int flags)
55137c478bd9Sstevel@tonic-gate {
55147c478bd9Sstevel@tonic-gate 	return (hold_devi(major, instance, flags));
55157c478bd9Sstevel@tonic-gate }
55167c478bd9Sstevel@tonic-gate 
55177c478bd9Sstevel@tonic-gate dev_info_t *
55187c478bd9Sstevel@tonic-gate e_ddi_hold_devi_by_dev(dev_t dev, int flags)
55197c478bd9Sstevel@tonic-gate {
55207c478bd9Sstevel@tonic-gate 	major_t	major = getmajor(dev);
55217c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
55227c478bd9Sstevel@tonic-gate 	struct dev_ops	*ops;
55237c478bd9Sstevel@tonic-gate 	dev_info_t	*ddip = NULL;
55247c478bd9Sstevel@tonic-gate 
55257c478bd9Sstevel@tonic-gate 	dip = hold_devi(major, dev_to_instance(dev), flags);
55267c478bd9Sstevel@tonic-gate 
55277c478bd9Sstevel@tonic-gate 	/*
55287c478bd9Sstevel@tonic-gate 	 * The rest of this routine is legacy support for drivers that
55297c478bd9Sstevel@tonic-gate 	 * have broken DDI_INFO_DEVT2INSTANCE implementations but may have
55307c478bd9Sstevel@tonic-gate 	 * functional DDI_INFO_DEVT2DEVINFO implementations.  This code will
55317c478bd9Sstevel@tonic-gate 	 * diagnose inconsistency and, for maximum compatibility with legacy
55327c478bd9Sstevel@tonic-gate 	 * drivers, give preference to the drivers DDI_INFO_DEVT2DEVINFO
55337c478bd9Sstevel@tonic-gate 	 * implementation over the above derived dip based the driver's
55347c478bd9Sstevel@tonic-gate 	 * DDI_INFO_DEVT2INSTANCE implementation. This legacy support should
55357c478bd9Sstevel@tonic-gate 	 * be removed when DDI_INFO_DEVT2DEVINFO is deprecated.
55367c478bd9Sstevel@tonic-gate 	 *
55377c478bd9Sstevel@tonic-gate 	 * NOTE: The following code has a race condition. DEVT2DEVINFO
55387c478bd9Sstevel@tonic-gate 	 *	returns a dip which is not held. By the time we ref ddip,
55397c478bd9Sstevel@tonic-gate 	 *	it could have been freed. The saving grace is that for
55407c478bd9Sstevel@tonic-gate 	 *	most drivers, the dip returned from hold_devi() is the
55417c478bd9Sstevel@tonic-gate 	 *	same one as the one returned by DEVT2DEVINFO, so we are
55427c478bd9Sstevel@tonic-gate 	 *	safe for drivers with the correct getinfo(9e) impl.
55437c478bd9Sstevel@tonic-gate 	 */
55447c478bd9Sstevel@tonic-gate 	if (((ops = ddi_hold_driver(major)) != NULL) &&
55457c478bd9Sstevel@tonic-gate 	    CB_DRV_INSTALLED(ops) && ops->devo_getinfo)  {
55467c478bd9Sstevel@tonic-gate 		if ((*ops->devo_getinfo)(NULL, DDI_INFO_DEVT2DEVINFO,
55477c478bd9Sstevel@tonic-gate 		    (void *)dev, (void **)&ddip) != DDI_SUCCESS)
55487c478bd9Sstevel@tonic-gate 			ddip = NULL;
55497c478bd9Sstevel@tonic-gate 	}
55507c478bd9Sstevel@tonic-gate 
55517c478bd9Sstevel@tonic-gate 	/* give preference to the driver returned DEVT2DEVINFO dip */
55527c478bd9Sstevel@tonic-gate 	if (ddip && (dip != ddip)) {
55537c478bd9Sstevel@tonic-gate #ifdef	DEBUG
55547c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: inconsistent getinfo(9E) implementation",
55557c478bd9Sstevel@tonic-gate 		    ddi_driver_name(ddip));
55567c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
55577c478bd9Sstevel@tonic-gate 		ndi_hold_devi(ddip);
55587c478bd9Sstevel@tonic-gate 		if (dip)
55597c478bd9Sstevel@tonic-gate 			ndi_rele_devi(dip);
55607c478bd9Sstevel@tonic-gate 		dip = ddip;
55617c478bd9Sstevel@tonic-gate 	}
55627c478bd9Sstevel@tonic-gate 
55637c478bd9Sstevel@tonic-gate 	if (ops)
55647c478bd9Sstevel@tonic-gate 		ddi_rele_driver(major);
55657c478bd9Sstevel@tonic-gate 
55667c478bd9Sstevel@tonic-gate 	return (dip);
55677c478bd9Sstevel@tonic-gate }
55687c478bd9Sstevel@tonic-gate 
55697c478bd9Sstevel@tonic-gate /*
55707c478bd9Sstevel@tonic-gate  * For compatibility only. Do not call this function!
55717c478bd9Sstevel@tonic-gate  */
55727c478bd9Sstevel@tonic-gate dev_info_t *
55737c478bd9Sstevel@tonic-gate e_ddi_get_dev_info(dev_t dev, vtype_t type)
55747c478bd9Sstevel@tonic-gate {
55757c478bd9Sstevel@tonic-gate 	dev_info_t *dip = NULL;
55767c478bd9Sstevel@tonic-gate 	if (getmajor(dev) >= devcnt)
55777c478bd9Sstevel@tonic-gate 		return (NULL);
55787c478bd9Sstevel@tonic-gate 
55797c478bd9Sstevel@tonic-gate 	switch (type) {
55807c478bd9Sstevel@tonic-gate 	case VCHR:
55817c478bd9Sstevel@tonic-gate 	case VBLK:
55827c478bd9Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
55837c478bd9Sstevel@tonic-gate 	default:
55847c478bd9Sstevel@tonic-gate 		break;
55857c478bd9Sstevel@tonic-gate 	}
55867c478bd9Sstevel@tonic-gate 
55877c478bd9Sstevel@tonic-gate 	/*
55887c478bd9Sstevel@tonic-gate 	 * For compatibility reasons, we can only return the dip with
55897c478bd9Sstevel@tonic-gate 	 * the driver ref count held. This is not a safe thing to do.
55907c478bd9Sstevel@tonic-gate 	 * For certain broken third-party software, we are willing
55917c478bd9Sstevel@tonic-gate 	 * to venture into unknown territory.
55927c478bd9Sstevel@tonic-gate 	 */
55937c478bd9Sstevel@tonic-gate 	if (dip) {
55947c478bd9Sstevel@tonic-gate 		(void) ndi_hold_driver(dip);
55957c478bd9Sstevel@tonic-gate 		ndi_rele_devi(dip);
55967c478bd9Sstevel@tonic-gate 	}
55977c478bd9Sstevel@tonic-gate 	return (dip);
55987c478bd9Sstevel@tonic-gate }
55997c478bd9Sstevel@tonic-gate 
56007c478bd9Sstevel@tonic-gate dev_info_t *
56017c478bd9Sstevel@tonic-gate e_ddi_hold_devi_by_path(char *path, int flags)
56027c478bd9Sstevel@tonic-gate {
56037c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
56047c478bd9Sstevel@tonic-gate 
56057c478bd9Sstevel@tonic-gate 	/* can't specify NOATTACH by path */
56067c478bd9Sstevel@tonic-gate 	ASSERT(!(flags & E_DDI_HOLD_DEVI_NOATTACH));
56077c478bd9Sstevel@tonic-gate 
56087c478bd9Sstevel@tonic-gate 	return (resolve_pathname(path, &dip, NULL, NULL) ? NULL : dip);
56097c478bd9Sstevel@tonic-gate }
56107c478bd9Sstevel@tonic-gate 
56117c478bd9Sstevel@tonic-gate void
56127c478bd9Sstevel@tonic-gate e_ddi_hold_devi(dev_info_t *dip)
56137c478bd9Sstevel@tonic-gate {
56147c478bd9Sstevel@tonic-gate 	ndi_hold_devi(dip);
56157c478bd9Sstevel@tonic-gate }
56167c478bd9Sstevel@tonic-gate 
56177c478bd9Sstevel@tonic-gate void
56187c478bd9Sstevel@tonic-gate ddi_release_devi(dev_info_t *dip)
56197c478bd9Sstevel@tonic-gate {
56207c478bd9Sstevel@tonic-gate 	ndi_rele_devi(dip);
56217c478bd9Sstevel@tonic-gate }
56227c478bd9Sstevel@tonic-gate 
56237c478bd9Sstevel@tonic-gate /*
56247c478bd9Sstevel@tonic-gate  * Associate a streams queue with a devinfo node
56257c478bd9Sstevel@tonic-gate  * NOTE: This function is called by STREAM driver's put procedure.
56267c478bd9Sstevel@tonic-gate  *	It cannot block.
56277c478bd9Sstevel@tonic-gate  */
56287c478bd9Sstevel@tonic-gate void
56297c478bd9Sstevel@tonic-gate ddi_assoc_queue_with_devi(queue_t *q, dev_info_t *dip)
56307c478bd9Sstevel@tonic-gate {
56317c478bd9Sstevel@tonic-gate 	queue_t *rq = _RD(q);
56327c478bd9Sstevel@tonic-gate 	struct stdata *stp;
56337c478bd9Sstevel@tonic-gate 	vnode_t *vp;
56347c478bd9Sstevel@tonic-gate 
56357c478bd9Sstevel@tonic-gate 	/* set flag indicating that ddi_assoc_queue_with_devi was called */
56367c478bd9Sstevel@tonic-gate 	mutex_enter(QLOCK(rq));
56377c478bd9Sstevel@tonic-gate 	rq->q_flag |= _QASSOCIATED;
56387c478bd9Sstevel@tonic-gate 	mutex_exit(QLOCK(rq));
56397c478bd9Sstevel@tonic-gate 
56407c478bd9Sstevel@tonic-gate 	/* get the vnode associated with the queue */
56417c478bd9Sstevel@tonic-gate 	stp = STREAM(rq);
56427c478bd9Sstevel@tonic-gate 	vp = stp->sd_vnode;
56437c478bd9Sstevel@tonic-gate 	ASSERT(vp);
56447c478bd9Sstevel@tonic-gate 
56457c478bd9Sstevel@tonic-gate 	/* change the hardware association of the vnode */
56467c478bd9Sstevel@tonic-gate 	spec_assoc_vp_with_devi(vp, dip);
56477c478bd9Sstevel@tonic-gate }
56487c478bd9Sstevel@tonic-gate 
56497c478bd9Sstevel@tonic-gate /*
56507c478bd9Sstevel@tonic-gate  * ddi_install_driver(name)
56517c478bd9Sstevel@tonic-gate  *
56527c478bd9Sstevel@tonic-gate  * Driver installation is currently a byproduct of driver loading.  This
56537c478bd9Sstevel@tonic-gate  * may change.
56547c478bd9Sstevel@tonic-gate  */
56557c478bd9Sstevel@tonic-gate int
56567c478bd9Sstevel@tonic-gate ddi_install_driver(char *name)
56577c478bd9Sstevel@tonic-gate {
56587c478bd9Sstevel@tonic-gate 	major_t major = ddi_name_to_major(name);
56597c478bd9Sstevel@tonic-gate 
56607c478bd9Sstevel@tonic-gate 	if ((major == (major_t)-1) ||
56617c478bd9Sstevel@tonic-gate 	    (ddi_hold_installed_driver(major) == NULL)) {
56627c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
56637c478bd9Sstevel@tonic-gate 	}
56647c478bd9Sstevel@tonic-gate 	ddi_rele_driver(major);
56657c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
56667c478bd9Sstevel@tonic-gate }
56677c478bd9Sstevel@tonic-gate 
56687c478bd9Sstevel@tonic-gate struct dev_ops *
56697c478bd9Sstevel@tonic-gate ddi_hold_driver(major_t major)
56707c478bd9Sstevel@tonic-gate {
56717c478bd9Sstevel@tonic-gate 	return (mod_hold_dev_by_major(major));
56727c478bd9Sstevel@tonic-gate }
56737c478bd9Sstevel@tonic-gate 
56747c478bd9Sstevel@tonic-gate 
56757c478bd9Sstevel@tonic-gate void
56767c478bd9Sstevel@tonic-gate ddi_rele_driver(major_t major)
56777c478bd9Sstevel@tonic-gate {
56787c478bd9Sstevel@tonic-gate 	mod_rele_dev_by_major(major);
56797c478bd9Sstevel@tonic-gate }
56807c478bd9Sstevel@tonic-gate 
56817c478bd9Sstevel@tonic-gate 
56827c478bd9Sstevel@tonic-gate /*
56837c478bd9Sstevel@tonic-gate  * This is called during boot to force attachment order of special dips
56847c478bd9Sstevel@tonic-gate  * dip must be referenced via ndi_hold_devi()
56857c478bd9Sstevel@tonic-gate  */
56867c478bd9Sstevel@tonic-gate int
56877c478bd9Sstevel@tonic-gate i_ddi_attach_node_hierarchy(dev_info_t *dip)
56887c478bd9Sstevel@tonic-gate {
56897c478bd9Sstevel@tonic-gate 	dev_info_t *parent;
56907c478bd9Sstevel@tonic-gate 
56917c478bd9Sstevel@tonic-gate 	if (i_ddi_node_state(dip) == DS_READY)
56927c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
56937c478bd9Sstevel@tonic-gate 
56947c478bd9Sstevel@tonic-gate 	/*
56957c478bd9Sstevel@tonic-gate 	 * Attach parent dip
56967c478bd9Sstevel@tonic-gate 	 */
56977c478bd9Sstevel@tonic-gate 	parent = ddi_get_parent(dip);
56987c478bd9Sstevel@tonic-gate 	if (i_ddi_attach_node_hierarchy(parent) != DDI_SUCCESS)
56997c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
57007c478bd9Sstevel@tonic-gate 
57017c478bd9Sstevel@tonic-gate 	/*
57027c478bd9Sstevel@tonic-gate 	 * Expand .conf nodes under this parent
57037c478bd9Sstevel@tonic-gate 	 */
57047c478bd9Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(parent, 0);
57057c478bd9Sstevel@tonic-gate 	return (i_ddi_attachchild(dip));
57067c478bd9Sstevel@tonic-gate }
57077c478bd9Sstevel@tonic-gate 
57087c478bd9Sstevel@tonic-gate /* keep this function static */
57097c478bd9Sstevel@tonic-gate static int
57107c478bd9Sstevel@tonic-gate attach_driver_nodes(major_t major)
57117c478bd9Sstevel@tonic-gate {
57127c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
57137c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
57147c478bd9Sstevel@tonic-gate 	int error = DDI_FAILURE;
57157c478bd9Sstevel@tonic-gate 
57167c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
57177c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
57187c478bd9Sstevel@tonic-gate 	dip = dnp->dn_head;
57197c478bd9Sstevel@tonic-gate 	while (dip) {
57207c478bd9Sstevel@tonic-gate 		ndi_hold_devi(dip);
57217c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
57227c478bd9Sstevel@tonic-gate 		if (i_ddi_attach_node_hierarchy(dip) == DDI_SUCCESS)
57237c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
57247c478bd9Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
57257c478bd9Sstevel@tonic-gate 		ndi_rele_devi(dip);
57267c478bd9Sstevel@tonic-gate 		dip = ddi_get_next(dip);
57277c478bd9Sstevel@tonic-gate 	}
57287c478bd9Sstevel@tonic-gate 	if (error == DDI_SUCCESS)
57297c478bd9Sstevel@tonic-gate 		dnp->dn_flags |= DN_NO_AUTODETACH;
57307c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
57317c478bd9Sstevel@tonic-gate 
57327c478bd9Sstevel@tonic-gate 
57337c478bd9Sstevel@tonic-gate 	return (error);
57347c478bd9Sstevel@tonic-gate }
57357c478bd9Sstevel@tonic-gate 
57367c478bd9Sstevel@tonic-gate /*
57377c478bd9Sstevel@tonic-gate  * i_ddi_attach_hw_nodes configures and attaches all hw nodes
57387c478bd9Sstevel@tonic-gate  * bound to a specific driver. This function replaces calls to
57397c478bd9Sstevel@tonic-gate  * ddi_hold_installed_driver() for drivers with no .conf
57407c478bd9Sstevel@tonic-gate  * enumerated nodes.
57417c478bd9Sstevel@tonic-gate  *
57427c478bd9Sstevel@tonic-gate  * This facility is typically called at boot time to attach
57437c478bd9Sstevel@tonic-gate  * platform-specific hardware nodes, such as ppm nodes on xcal
57447c478bd9Sstevel@tonic-gate  * and grover and keyswitch nodes on cherrystone. It does not
57457c478bd9Sstevel@tonic-gate  * deal with .conf enumerated node. Calling it beyond the boot
57467c478bd9Sstevel@tonic-gate  * process is strongly discouraged.
57477c478bd9Sstevel@tonic-gate  */
57487c478bd9Sstevel@tonic-gate int
57497c478bd9Sstevel@tonic-gate i_ddi_attach_hw_nodes(char *driver)
57507c478bd9Sstevel@tonic-gate {
57517c478bd9Sstevel@tonic-gate 	major_t major;
57527c478bd9Sstevel@tonic-gate 
57537c478bd9Sstevel@tonic-gate 	major = ddi_name_to_major(driver);
57547c478bd9Sstevel@tonic-gate 	if (major == (major_t)-1)
57557c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
57567c478bd9Sstevel@tonic-gate 
57577c478bd9Sstevel@tonic-gate 	return (attach_driver_nodes(major));
57587c478bd9Sstevel@tonic-gate }
57597c478bd9Sstevel@tonic-gate 
57607c478bd9Sstevel@tonic-gate /*
57617c478bd9Sstevel@tonic-gate  * i_ddi_attach_pseudo_node configures pseudo drivers which
57627c478bd9Sstevel@tonic-gate  * has a single node. The .conf nodes must be enumerated
57637c478bd9Sstevel@tonic-gate  * before calling this interface. The dip is held attached
57647c478bd9Sstevel@tonic-gate  * upon returning.
57657c478bd9Sstevel@tonic-gate  *
57667c478bd9Sstevel@tonic-gate  * This facility should only be called only at boot time
57677c478bd9Sstevel@tonic-gate  * by the I/O framework.
57687c478bd9Sstevel@tonic-gate  */
57697c478bd9Sstevel@tonic-gate dev_info_t *
57707c478bd9Sstevel@tonic-gate i_ddi_attach_pseudo_node(char *driver)
57717c478bd9Sstevel@tonic-gate {
57727c478bd9Sstevel@tonic-gate 	major_t major;
57737c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
57747c478bd9Sstevel@tonic-gate 
57757c478bd9Sstevel@tonic-gate 	major = ddi_name_to_major(driver);
57767c478bd9Sstevel@tonic-gate 	if (major == (major_t)-1)
57777c478bd9Sstevel@tonic-gate 		return (NULL);
57787c478bd9Sstevel@tonic-gate 
57797c478bd9Sstevel@tonic-gate 	if (attach_driver_nodes(major) != DDI_SUCCESS)
57807c478bd9Sstevel@tonic-gate 		return (NULL);
57817c478bd9Sstevel@tonic-gate 
57827c478bd9Sstevel@tonic-gate 	dip = devnamesp[major].dn_head;
57837c478bd9Sstevel@tonic-gate 	ASSERT(dip && ddi_get_next(dip) == NULL);
57847c478bd9Sstevel@tonic-gate 	ndi_hold_devi(dip);
57857c478bd9Sstevel@tonic-gate 	return (dip);
57867c478bd9Sstevel@tonic-gate }
57877c478bd9Sstevel@tonic-gate 
57887c478bd9Sstevel@tonic-gate static void
57897c478bd9Sstevel@tonic-gate diplist_to_parent_major(dev_info_t *head, char parents[])
57907c478bd9Sstevel@tonic-gate {
57917c478bd9Sstevel@tonic-gate 	major_t major;
57927c478bd9Sstevel@tonic-gate 	dev_info_t *dip, *pdip;
57937c478bd9Sstevel@tonic-gate 
57947c478bd9Sstevel@tonic-gate 	for (dip = head; dip != NULL; dip = ddi_get_next(dip)) {
57957c478bd9Sstevel@tonic-gate 		pdip = ddi_get_parent(dip);
57967c478bd9Sstevel@tonic-gate 		ASSERT(pdip);	/* disallow rootnex.conf nodes */
57977c478bd9Sstevel@tonic-gate 		major = ddi_driver_major(pdip);
57987c478bd9Sstevel@tonic-gate 		if ((major != (major_t)-1) && parents[major] == 0)
57997c478bd9Sstevel@tonic-gate 			parents[major] = 1;
58007c478bd9Sstevel@tonic-gate 	}
58017c478bd9Sstevel@tonic-gate }
58027c478bd9Sstevel@tonic-gate 
58037c478bd9Sstevel@tonic-gate /*
58047c478bd9Sstevel@tonic-gate  * Call ddi_hold_installed_driver() on each parent major
58057c478bd9Sstevel@tonic-gate  * and invoke mt_config_driver() to attach child major.
58067c478bd9Sstevel@tonic-gate  * This is part of the implementation of ddi_hold_installed_driver.
58077c478bd9Sstevel@tonic-gate  */
58087c478bd9Sstevel@tonic-gate static int
58097c478bd9Sstevel@tonic-gate attach_driver_by_parent(major_t child_major, char parents[])
58107c478bd9Sstevel@tonic-gate {
58117c478bd9Sstevel@tonic-gate 	major_t par_major;
58127c478bd9Sstevel@tonic-gate 	struct mt_config_handle *hdl;
58137c478bd9Sstevel@tonic-gate 	int flags = NDI_DEVI_PERSIST | NDI_NO_EVENT;
58147c478bd9Sstevel@tonic-gate 
58157c478bd9Sstevel@tonic-gate 	hdl = mt_config_init(NULL, NULL, flags, child_major, MT_CONFIG_OP,
58167c478bd9Sstevel@tonic-gate 	    NULL);
58177c478bd9Sstevel@tonic-gate 	for (par_major = 0; par_major < devcnt; par_major++) {
58187c478bd9Sstevel@tonic-gate 		/* disallow recursion on the same driver */
58197c478bd9Sstevel@tonic-gate 		if (parents[par_major] == 0 || par_major == child_major)
58207c478bd9Sstevel@tonic-gate 			continue;
58217c478bd9Sstevel@tonic-gate 		if (ddi_hold_installed_driver(par_major) == NULL)
58227c478bd9Sstevel@tonic-gate 			continue;
58237c478bd9Sstevel@tonic-gate 		hdl->mtc_parmajor = par_major;
58247c478bd9Sstevel@tonic-gate 		mt_config_driver(hdl);
58257c478bd9Sstevel@tonic-gate 		ddi_rele_driver(par_major);
58267c478bd9Sstevel@tonic-gate 	}
58277c478bd9Sstevel@tonic-gate 	(void) mt_config_fini(hdl);
58287c478bd9Sstevel@tonic-gate 
58297c478bd9Sstevel@tonic-gate 	return (i_ddi_devs_attached(child_major));
58307c478bd9Sstevel@tonic-gate }
58317c478bd9Sstevel@tonic-gate 
58327c478bd9Sstevel@tonic-gate int
58337c478bd9Sstevel@tonic-gate i_ddi_devs_attached(major_t major)
58347c478bd9Sstevel@tonic-gate {
58357c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
58367c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
58377c478bd9Sstevel@tonic-gate 	int error = DDI_FAILURE;
58387c478bd9Sstevel@tonic-gate 
58397c478bd9Sstevel@tonic-gate 	/* check for attached instances */
58407c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
58417c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
58427c478bd9Sstevel@tonic-gate 	for (dip = dnp->dn_head; dip != NULL; dip = ddi_get_next(dip)) {
58437c478bd9Sstevel@tonic-gate 		if (i_ddi_node_state(dip) >= DS_ATTACHED) {
58447c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
58457c478bd9Sstevel@tonic-gate 			break;
58467c478bd9Sstevel@tonic-gate 		}
58477c478bd9Sstevel@tonic-gate 	}
58487c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
58497c478bd9Sstevel@tonic-gate 
58507c478bd9Sstevel@tonic-gate 	return (error);
58517c478bd9Sstevel@tonic-gate }
58527c478bd9Sstevel@tonic-gate 
58537c478bd9Sstevel@tonic-gate /*
58547c478bd9Sstevel@tonic-gate  * ddi_hold_installed_driver configures and attaches all
58557c478bd9Sstevel@tonic-gate  * instances of the specified driver. To accomplish this
58567c478bd9Sstevel@tonic-gate  * it configures and attaches all possible parents of
58577c478bd9Sstevel@tonic-gate  * the driver, enumerated both in h/w nodes and in the
58587c478bd9Sstevel@tonic-gate  * driver's .conf file.
58597c478bd9Sstevel@tonic-gate  *
58607c478bd9Sstevel@tonic-gate  * NOTE: This facility is for compatibility purposes only and will
58617c478bd9Sstevel@tonic-gate  *	eventually go away. Its usage is strongly discouraged.
58627c478bd9Sstevel@tonic-gate  */
58637c478bd9Sstevel@tonic-gate static void
58647c478bd9Sstevel@tonic-gate enter_driver(struct devnames *dnp)
58657c478bd9Sstevel@tonic-gate {
58667c478bd9Sstevel@tonic-gate 	mutex_enter(&dnp->dn_lock);
58677c478bd9Sstevel@tonic-gate 	ASSERT(dnp->dn_busy_thread != curthread);
58687c478bd9Sstevel@tonic-gate 	while (dnp->dn_flags & DN_DRIVER_BUSY)
58697c478bd9Sstevel@tonic-gate 		cv_wait(&dnp->dn_wait, &dnp->dn_lock);
58707c478bd9Sstevel@tonic-gate 	dnp->dn_flags |= DN_DRIVER_BUSY;
58717c478bd9Sstevel@tonic-gate 	dnp->dn_busy_thread = curthread;
58727c478bd9Sstevel@tonic-gate 	mutex_exit(&dnp->dn_lock);
58737c478bd9Sstevel@tonic-gate }
58747c478bd9Sstevel@tonic-gate 
58757c478bd9Sstevel@tonic-gate static void
58767c478bd9Sstevel@tonic-gate exit_driver(struct devnames *dnp)
58777c478bd9Sstevel@tonic-gate {
58787c478bd9Sstevel@tonic-gate 	mutex_enter(&dnp->dn_lock);
58797c478bd9Sstevel@tonic-gate 	ASSERT(dnp->dn_busy_thread == curthread);
58807c478bd9Sstevel@tonic-gate 	dnp->dn_flags &= ~DN_DRIVER_BUSY;
58817c478bd9Sstevel@tonic-gate 	dnp->dn_busy_thread = NULL;
58827c478bd9Sstevel@tonic-gate 	cv_broadcast(&dnp->dn_wait);
58837c478bd9Sstevel@tonic-gate 	mutex_exit(&dnp->dn_lock);
58847c478bd9Sstevel@tonic-gate }
58857c478bd9Sstevel@tonic-gate 
58867c478bd9Sstevel@tonic-gate struct dev_ops *
58877c478bd9Sstevel@tonic-gate ddi_hold_installed_driver(major_t major)
58887c478bd9Sstevel@tonic-gate {
58897c478bd9Sstevel@tonic-gate 	struct dev_ops *ops;
58907c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
58917c478bd9Sstevel@tonic-gate 	char *parents;
58927c478bd9Sstevel@tonic-gate 	int error;
58937c478bd9Sstevel@tonic-gate 
58947c478bd9Sstevel@tonic-gate 	ops = ddi_hold_driver(major);
58957c478bd9Sstevel@tonic-gate 	if (ops == NULL)
58967c478bd9Sstevel@tonic-gate 		return (NULL);
58977c478bd9Sstevel@tonic-gate 
58987c478bd9Sstevel@tonic-gate 	/*
58997c478bd9Sstevel@tonic-gate 	 * Return immediately if all the attach operations associated
59007c478bd9Sstevel@tonic-gate 	 * with a ddi_hold_installed_driver() call have already been done.
59017c478bd9Sstevel@tonic-gate 	 */
59027c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
59037c478bd9Sstevel@tonic-gate 	enter_driver(dnp);
59047c478bd9Sstevel@tonic-gate 	if (dnp->dn_flags & DN_DRIVER_HELD) {
59057c478bd9Sstevel@tonic-gate 		exit_driver(dnp);
59067c478bd9Sstevel@tonic-gate 		if (i_ddi_devs_attached(major) == DDI_SUCCESS)
59077c478bd9Sstevel@tonic-gate 			return (ops);
59087c478bd9Sstevel@tonic-gate 		ddi_rele_driver(major);
59097c478bd9Sstevel@tonic-gate 		return (NULL);
59107c478bd9Sstevel@tonic-gate 	}
59117c478bd9Sstevel@tonic-gate 
59127c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
59137c478bd9Sstevel@tonic-gate 	dnp->dn_flags |= (DN_DRIVER_HELD | DN_NO_AUTODETACH);
59147c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
59157c478bd9Sstevel@tonic-gate 
59167c478bd9Sstevel@tonic-gate 	DCOMPATPRINTF((CE_CONT,
59177c478bd9Sstevel@tonic-gate 	    "ddi_hold_installed_driver: %s\n", dnp->dn_name));
59187c478bd9Sstevel@tonic-gate 
59197c478bd9Sstevel@tonic-gate 	/*
59207c478bd9Sstevel@tonic-gate 	 * When the driver has no .conf children, it is sufficient
59217c478bd9Sstevel@tonic-gate 	 * to attach existing nodes in the device tree. Nodes not
59227c478bd9Sstevel@tonic-gate 	 * enumerated by the OBP are not attached.
59237c478bd9Sstevel@tonic-gate 	 */
59247c478bd9Sstevel@tonic-gate 	if (dnp->dn_pl == NULL) {
59257c478bd9Sstevel@tonic-gate 		if (attach_driver_nodes(major) == DDI_SUCCESS) {
59267c478bd9Sstevel@tonic-gate 			exit_driver(dnp);
59277c478bd9Sstevel@tonic-gate 			return (ops);
59287c478bd9Sstevel@tonic-gate 		}
59297c478bd9Sstevel@tonic-gate 		exit_driver(dnp);
59307c478bd9Sstevel@tonic-gate 		ddi_rele_driver(major);
59317c478bd9Sstevel@tonic-gate 		return (NULL);
59327c478bd9Sstevel@tonic-gate 	}
59337c478bd9Sstevel@tonic-gate 
59347c478bd9Sstevel@tonic-gate 	/*
59357c478bd9Sstevel@tonic-gate 	 * Driver has .conf nodes. We find all possible parents
59367c478bd9Sstevel@tonic-gate 	 * and recursively all ddi_hold_installed_driver on the
59377c478bd9Sstevel@tonic-gate 	 * parent driver; then we invoke ndi_config_driver()
59387c478bd9Sstevel@tonic-gate 	 * on all possible parent node in parallel to speed up
59397c478bd9Sstevel@tonic-gate 	 * performance.
59407c478bd9Sstevel@tonic-gate 	 */
59417c478bd9Sstevel@tonic-gate 	parents = kmem_zalloc(devcnt * sizeof (char), KM_SLEEP);
59427c478bd9Sstevel@tonic-gate 
59437c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
59447c478bd9Sstevel@tonic-gate 	/* find .conf parents */
59457c478bd9Sstevel@tonic-gate 	(void) impl_parlist_to_major(dnp->dn_pl, parents);
59467c478bd9Sstevel@tonic-gate 	/* find hw node parents */
59477c478bd9Sstevel@tonic-gate 	diplist_to_parent_major(dnp->dn_head, parents);
59487c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
59497c478bd9Sstevel@tonic-gate 
59507c478bd9Sstevel@tonic-gate 	error = attach_driver_by_parent(major, parents);
59517c478bd9Sstevel@tonic-gate 	kmem_free(parents, devcnt * sizeof (char));
59527c478bd9Sstevel@tonic-gate 	if (error == DDI_SUCCESS) {
59537c478bd9Sstevel@tonic-gate 		exit_driver(dnp);
59547c478bd9Sstevel@tonic-gate 		return (ops);
59557c478bd9Sstevel@tonic-gate 	}
59567c478bd9Sstevel@tonic-gate 
59577c478bd9Sstevel@tonic-gate 	exit_driver(dnp);
59587c478bd9Sstevel@tonic-gate 	ddi_rele_driver(major);
59597c478bd9Sstevel@tonic-gate 	return (NULL);
59607c478bd9Sstevel@tonic-gate }
59617c478bd9Sstevel@tonic-gate 
59627c478bd9Sstevel@tonic-gate /*
59637c478bd9Sstevel@tonic-gate  * Default bus_config entry point for nexus drivers
59647c478bd9Sstevel@tonic-gate  */
59657c478bd9Sstevel@tonic-gate int
59667c478bd9Sstevel@tonic-gate ndi_busop_bus_config(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
59677c478bd9Sstevel@tonic-gate     void *arg, dev_info_t **child, clock_t timeout)
59687c478bd9Sstevel@tonic-gate {
59697c478bd9Sstevel@tonic-gate 	major_t major;
59707c478bd9Sstevel@tonic-gate 
59717c478bd9Sstevel@tonic-gate 	/*
59727c478bd9Sstevel@tonic-gate 	 * A timeout of 30 minutes or more is probably a mistake
59737c478bd9Sstevel@tonic-gate 	 * This is intended to catch uses where timeout is in
59747c478bd9Sstevel@tonic-gate 	 * the wrong units.  timeout must be in units of ticks.
59757c478bd9Sstevel@tonic-gate 	 */
59767c478bd9Sstevel@tonic-gate 	ASSERT(timeout < SEC_TO_TICK(1800));
59777c478bd9Sstevel@tonic-gate 
59787c478bd9Sstevel@tonic-gate 	major = (major_t)-1;
59797c478bd9Sstevel@tonic-gate 	switch (op) {
59807c478bd9Sstevel@tonic-gate 	case BUS_CONFIG_ONE:
59817c478bd9Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config %s timeout=%ld\n",
59827c478bd9Sstevel@tonic-gate 			ddi_driver_name(pdip), ddi_get_instance(pdip),
59837c478bd9Sstevel@tonic-gate 			(char *)arg, timeout));
59847c478bd9Sstevel@tonic-gate 		return (devi_config_one(pdip, (char *)arg, child, flags,
59857c478bd9Sstevel@tonic-gate 		    timeout));
59867c478bd9Sstevel@tonic-gate 
59877c478bd9Sstevel@tonic-gate 	case BUS_CONFIG_DRIVER:
59887c478bd9Sstevel@tonic-gate 		major = (major_t)(uintptr_t)arg;
59897c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
59907c478bd9Sstevel@tonic-gate 	case BUS_CONFIG_ALL:
59917c478bd9Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config timeout=%ld\n",
59927c478bd9Sstevel@tonic-gate 			ddi_driver_name(pdip), ddi_get_instance(pdip),
59937c478bd9Sstevel@tonic-gate 			timeout));
59947c478bd9Sstevel@tonic-gate 		if (timeout > 0) {
59957c478bd9Sstevel@tonic-gate 			NDI_DEBUG(flags, (CE_CONT,
59967c478bd9Sstevel@tonic-gate 			    "%s%d: bus config all timeout=%ld\n",
59977c478bd9Sstevel@tonic-gate 			    ddi_driver_name(pdip), ddi_get_instance(pdip),
59987c478bd9Sstevel@tonic-gate 			    timeout));
59997c478bd9Sstevel@tonic-gate 			delay(timeout);
60007c478bd9Sstevel@tonic-gate 		}
60017c478bd9Sstevel@tonic-gate 		return (config_immediate_children(pdip, flags, major));
60027c478bd9Sstevel@tonic-gate 
60037c478bd9Sstevel@tonic-gate 	default:
60047c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
60057c478bd9Sstevel@tonic-gate 	}
60067c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
60077c478bd9Sstevel@tonic-gate }
60087c478bd9Sstevel@tonic-gate 
60097c478bd9Sstevel@tonic-gate /*
60107c478bd9Sstevel@tonic-gate  * Default busop bus_unconfig handler for nexus drivers
60117c478bd9Sstevel@tonic-gate  */
60127c478bd9Sstevel@tonic-gate int
60137c478bd9Sstevel@tonic-gate ndi_busop_bus_unconfig(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
60147c478bd9Sstevel@tonic-gate     void *arg)
60157c478bd9Sstevel@tonic-gate {
60167c478bd9Sstevel@tonic-gate 	major_t major;
60177c478bd9Sstevel@tonic-gate 
60187c478bd9Sstevel@tonic-gate 	major = (major_t)-1;
60197c478bd9Sstevel@tonic-gate 	switch (op) {
60207c478bd9Sstevel@tonic-gate 	case BUS_UNCONFIG_ONE:
60217c478bd9Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig %s\n",
60227c478bd9Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
60237c478bd9Sstevel@tonic-gate 		    (char *)arg));
60247c478bd9Sstevel@tonic-gate 		return (devi_unconfig_one(pdip, (char *)arg, flags));
60257c478bd9Sstevel@tonic-gate 
60267c478bd9Sstevel@tonic-gate 	case BUS_UNCONFIG_DRIVER:
60277c478bd9Sstevel@tonic-gate 		major = (major_t)(uintptr_t)arg;
60287c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
60297c478bd9Sstevel@tonic-gate 	case BUS_UNCONFIG_ALL:
60307c478bd9Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig all\n",
60317c478bd9Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip)));
60327c478bd9Sstevel@tonic-gate 		return (unconfig_immediate_children(pdip, NULL, flags, major));
60337c478bd9Sstevel@tonic-gate 
60347c478bd9Sstevel@tonic-gate 	default:
60357c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
60367c478bd9Sstevel@tonic-gate 	}
60377c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
60387c478bd9Sstevel@tonic-gate }
60397c478bd9Sstevel@tonic-gate 
60407c478bd9Sstevel@tonic-gate /*
60417c478bd9Sstevel@tonic-gate  * dummy functions to be removed
60427c478bd9Sstevel@tonic-gate  */
60437c478bd9Sstevel@tonic-gate void
60447c478bd9Sstevel@tonic-gate impl_rem_dev_props(dev_info_t *dip)
60457c478bd9Sstevel@tonic-gate {
60467c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip))
60477c478bd9Sstevel@tonic-gate 	/* do nothing */
60487c478bd9Sstevel@tonic-gate }
60497c478bd9Sstevel@tonic-gate 
60507c478bd9Sstevel@tonic-gate /*
60517c478bd9Sstevel@tonic-gate  * Determine if a node is a leaf node. If not sure, return false (0).
60527c478bd9Sstevel@tonic-gate  */
60537c478bd9Sstevel@tonic-gate static int
60547c478bd9Sstevel@tonic-gate is_leaf_node(dev_info_t *dip)
60557c478bd9Sstevel@tonic-gate {
60567c478bd9Sstevel@tonic-gate 	major_t major = ddi_driver_major(dip);
60577c478bd9Sstevel@tonic-gate 
60587c478bd9Sstevel@tonic-gate 	if (major == (major_t)-1)
60597c478bd9Sstevel@tonic-gate 		return (0);
60607c478bd9Sstevel@tonic-gate 
60617c478bd9Sstevel@tonic-gate 	return (devnamesp[major].dn_flags & DN_LEAF_DRIVER);
60627c478bd9Sstevel@tonic-gate }
60637c478bd9Sstevel@tonic-gate 
60647c478bd9Sstevel@tonic-gate /*
60657c478bd9Sstevel@tonic-gate  * Multithreaded [un]configuration
60667c478bd9Sstevel@tonic-gate  */
60677c478bd9Sstevel@tonic-gate static struct mt_config_handle *
60687c478bd9Sstevel@tonic-gate mt_config_init(dev_info_t *pdip, dev_info_t **dipp, int flags,
60697c478bd9Sstevel@tonic-gate     major_t major, int op, struct brevq_node **brevqp)
60707c478bd9Sstevel@tonic-gate {
60717c478bd9Sstevel@tonic-gate 	struct mt_config_handle	*hdl = kmem_alloc(sizeof (*hdl), KM_SLEEP);
60727c478bd9Sstevel@tonic-gate 
60737c478bd9Sstevel@tonic-gate 	mutex_init(&hdl->mtc_lock, NULL, MUTEX_DEFAULT, NULL);
60747c478bd9Sstevel@tonic-gate 	cv_init(&hdl->mtc_cv, NULL, CV_DEFAULT, NULL);
60757c478bd9Sstevel@tonic-gate 	hdl->mtc_pdip = pdip;
60767c478bd9Sstevel@tonic-gate 	hdl->mtc_fdip = dipp;
60777c478bd9Sstevel@tonic-gate 	hdl->mtc_parmajor = (major_t)-1;
60787c478bd9Sstevel@tonic-gate 	hdl->mtc_flags = flags;
60797c478bd9Sstevel@tonic-gate 	hdl->mtc_major = major;
60807c478bd9Sstevel@tonic-gate 	hdl->mtc_thr_count = 0;
60817c478bd9Sstevel@tonic-gate 	hdl->mtc_op = op;
60827c478bd9Sstevel@tonic-gate 	hdl->mtc_error = 0;
60837c478bd9Sstevel@tonic-gate 	hdl->mtc_brevqp = brevqp;
60847c478bd9Sstevel@tonic-gate 
60857c478bd9Sstevel@tonic-gate #ifdef DEBUG
60867c478bd9Sstevel@tonic-gate 	gethrestime(&hdl->start_time);
60877c478bd9Sstevel@tonic-gate 	hdl->total_time = 0;
60887c478bd9Sstevel@tonic-gate #endif /* DEBUG */
60897c478bd9Sstevel@tonic-gate 
60907c478bd9Sstevel@tonic-gate 	return (hdl);
60917c478bd9Sstevel@tonic-gate }
60927c478bd9Sstevel@tonic-gate 
60937c478bd9Sstevel@tonic-gate #ifdef DEBUG
60947c478bd9Sstevel@tonic-gate static int
60957c478bd9Sstevel@tonic-gate time_diff_in_msec(timestruc_t start, timestruc_t end)
60967c478bd9Sstevel@tonic-gate {
60977c478bd9Sstevel@tonic-gate 	int	nsec, sec;
60987c478bd9Sstevel@tonic-gate 
60997c478bd9Sstevel@tonic-gate 	sec = end.tv_sec - start.tv_sec;
61007c478bd9Sstevel@tonic-gate 	nsec = end.tv_nsec - start.tv_nsec;
61017c478bd9Sstevel@tonic-gate 	if (nsec < 0) {
61027c478bd9Sstevel@tonic-gate 		nsec += NANOSEC;
61037c478bd9Sstevel@tonic-gate 		sec -= 1;
61047c478bd9Sstevel@tonic-gate 	}
61057c478bd9Sstevel@tonic-gate 
61067c478bd9Sstevel@tonic-gate 	return (sec * (NANOSEC >> 20) + (nsec >> 20));
61077c478bd9Sstevel@tonic-gate }
61087c478bd9Sstevel@tonic-gate 
61097c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
61107c478bd9Sstevel@tonic-gate 
61117c478bd9Sstevel@tonic-gate static int
61127c478bd9Sstevel@tonic-gate mt_config_fini(struct mt_config_handle *hdl)
61137c478bd9Sstevel@tonic-gate {
61147c478bd9Sstevel@tonic-gate 	int		rv;
61157c478bd9Sstevel@tonic-gate #ifdef DEBUG
61167c478bd9Sstevel@tonic-gate 	int		real_time;
61177c478bd9Sstevel@tonic-gate 	timestruc_t	end_time;
61187c478bd9Sstevel@tonic-gate #endif /* DEBUG */
61197c478bd9Sstevel@tonic-gate 
61207c478bd9Sstevel@tonic-gate 	mutex_enter(&hdl->mtc_lock);
61217c478bd9Sstevel@tonic-gate 	while (hdl->mtc_thr_count > 0)
61227c478bd9Sstevel@tonic-gate 		cv_wait(&hdl->mtc_cv, &hdl->mtc_lock);
61237c478bd9Sstevel@tonic-gate 	rv = hdl->mtc_error;
61247c478bd9Sstevel@tonic-gate 	mutex_exit(&hdl->mtc_lock);
61257c478bd9Sstevel@tonic-gate 
61267c478bd9Sstevel@tonic-gate #ifdef DEBUG
61277c478bd9Sstevel@tonic-gate 	gethrestime(&end_time);
61287c478bd9Sstevel@tonic-gate 	real_time = time_diff_in_msec(hdl->start_time, end_time);
61297c478bd9Sstevel@tonic-gate 	if ((ddidebug & DDI_MTCONFIG) && hdl->mtc_pdip)
61307c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE,
61317c478bd9Sstevel@tonic-gate 		    "config %s%d: total time %d msec, real time %d msec",
61327c478bd9Sstevel@tonic-gate 			ddi_driver_name(hdl->mtc_pdip),
61337c478bd9Sstevel@tonic-gate 			ddi_get_instance(hdl->mtc_pdip),
61347c478bd9Sstevel@tonic-gate 			hdl->total_time, real_time);
61357c478bd9Sstevel@tonic-gate #endif /* DEBUG */
61367c478bd9Sstevel@tonic-gate 
61377c478bd9Sstevel@tonic-gate 	cv_destroy(&hdl->mtc_cv);
61387c478bd9Sstevel@tonic-gate 	mutex_destroy(&hdl->mtc_lock);
61397c478bd9Sstevel@tonic-gate 	kmem_free(hdl, sizeof (*hdl));
61407c478bd9Sstevel@tonic-gate 
61417c478bd9Sstevel@tonic-gate 	return (rv);
61427c478bd9Sstevel@tonic-gate }
61437c478bd9Sstevel@tonic-gate 
61447c478bd9Sstevel@tonic-gate struct mt_config_data {
61457c478bd9Sstevel@tonic-gate 	struct mt_config_handle	*mtc_hdl;
61467c478bd9Sstevel@tonic-gate 	dev_info_t		*mtc_dip;
61477c478bd9Sstevel@tonic-gate 	major_t			mtc_major;
61487c478bd9Sstevel@tonic-gate 	int			mtc_flags;
61497c478bd9Sstevel@tonic-gate 	struct brevq_node	*mtc_brn;
61507c478bd9Sstevel@tonic-gate 	struct mt_config_data	*mtc_next;
61517c478bd9Sstevel@tonic-gate };
61527c478bd9Sstevel@tonic-gate 
61537c478bd9Sstevel@tonic-gate static void
61547c478bd9Sstevel@tonic-gate mt_config_thread(void *arg)
61557c478bd9Sstevel@tonic-gate {
61567c478bd9Sstevel@tonic-gate 	struct mt_config_data	*mcd = (struct mt_config_data *)arg;
61577c478bd9Sstevel@tonic-gate 	struct mt_config_handle	*hdl = mcd->mtc_hdl;
61587c478bd9Sstevel@tonic-gate 	dev_info_t		*dip = mcd->mtc_dip;
61597c478bd9Sstevel@tonic-gate 	dev_info_t		*rdip, **dipp;
61607c478bd9Sstevel@tonic-gate 	major_t			major = mcd->mtc_major;
61617c478bd9Sstevel@tonic-gate 	int			flags = mcd->mtc_flags;
61627c478bd9Sstevel@tonic-gate 	int			rv = 0;
61637c478bd9Sstevel@tonic-gate 
61647c478bd9Sstevel@tonic-gate #ifdef DEBUG
61657c478bd9Sstevel@tonic-gate 	timestruc_t start_time, end_time;
61667c478bd9Sstevel@tonic-gate 	gethrestime(&start_time);
61677c478bd9Sstevel@tonic-gate #endif /* DEBUG */
61687c478bd9Sstevel@tonic-gate 
61697c478bd9Sstevel@tonic-gate 	rdip = NULL;
61707c478bd9Sstevel@tonic-gate 	dipp = hdl->mtc_fdip ? &rdip : NULL;
61717c478bd9Sstevel@tonic-gate 
61727c478bd9Sstevel@tonic-gate 	switch (hdl->mtc_op) {
61737c478bd9Sstevel@tonic-gate 	case MT_CONFIG_OP:
61747c478bd9Sstevel@tonic-gate 		rv = devi_config_common(dip, flags, major);
61757c478bd9Sstevel@tonic-gate 		break;
61767c478bd9Sstevel@tonic-gate 	case MT_UNCONFIG_OP:
61777c478bd9Sstevel@tonic-gate 		if (mcd->mtc_brn) {
61787c478bd9Sstevel@tonic-gate 			struct brevq_node *brevq = NULL;
61797c478bd9Sstevel@tonic-gate 			rv = devi_unconfig_common(dip, dipp, flags, major,
61807c478bd9Sstevel@tonic-gate 			    &brevq);
6181*245c82d9Scth 			mcd->mtc_brn->brn_child = brevq;
61827c478bd9Sstevel@tonic-gate 		} else
61837c478bd9Sstevel@tonic-gate 			rv = devi_unconfig_common(dip, dipp, flags, major,
61847c478bd9Sstevel@tonic-gate 			    NULL);
61857c478bd9Sstevel@tonic-gate 		break;
61867c478bd9Sstevel@tonic-gate 	}
61877c478bd9Sstevel@tonic-gate 
61887c478bd9Sstevel@tonic-gate 	mutex_enter(&hdl->mtc_lock);
61897c478bd9Sstevel@tonic-gate #ifdef DEBUG
61907c478bd9Sstevel@tonic-gate 	gethrestime(&end_time);
61917c478bd9Sstevel@tonic-gate 	hdl->total_time += time_diff_in_msec(start_time, end_time);
61927c478bd9Sstevel@tonic-gate #endif /* DEBUG */
61937c478bd9Sstevel@tonic-gate 	if (rv != NDI_SUCCESS)
61947c478bd9Sstevel@tonic-gate 		hdl->mtc_error = rv;
61957c478bd9Sstevel@tonic-gate 	if (hdl->mtc_fdip && *hdl->mtc_fdip == NULL) {
61967c478bd9Sstevel@tonic-gate 		*hdl->mtc_fdip = rdip;
61977c478bd9Sstevel@tonic-gate 		rdip = NULL;
61987c478bd9Sstevel@tonic-gate 	}
61997c478bd9Sstevel@tonic-gate 
62007c478bd9Sstevel@tonic-gate 	if (--hdl->mtc_thr_count == 0)
62017c478bd9Sstevel@tonic-gate 		cv_broadcast(&hdl->mtc_cv);
62027c478bd9Sstevel@tonic-gate 	mutex_exit(&hdl->mtc_lock);
62037c478bd9Sstevel@tonic-gate 
62047c478bd9Sstevel@tonic-gate 	if (rdip) {
62057c478bd9Sstevel@tonic-gate 		ASSERT(rv != NDI_SUCCESS);
62067c478bd9Sstevel@tonic-gate 		ndi_rele_devi(rdip);
62077c478bd9Sstevel@tonic-gate 	}
62087c478bd9Sstevel@tonic-gate 
62097c478bd9Sstevel@tonic-gate 	ndi_rele_devi(dip);
62107c478bd9Sstevel@tonic-gate 	kmem_free(mcd, sizeof (*mcd));
62117c478bd9Sstevel@tonic-gate }
62127c478bd9Sstevel@tonic-gate 
62137c478bd9Sstevel@tonic-gate /*
62147c478bd9Sstevel@tonic-gate  * Multi-threaded config/unconfig of child nexus
62157c478bd9Sstevel@tonic-gate  */
62167c478bd9Sstevel@tonic-gate static void
62177c478bd9Sstevel@tonic-gate mt_config_children(struct mt_config_handle *hdl)
62187c478bd9Sstevel@tonic-gate {
62197c478bd9Sstevel@tonic-gate 	dev_info_t		*pdip = hdl->mtc_pdip;
62207c478bd9Sstevel@tonic-gate 	major_t			major = hdl->mtc_major;
62217c478bd9Sstevel@tonic-gate 	dev_info_t		*dip;
62227c478bd9Sstevel@tonic-gate 	int			circ;
6223*245c82d9Scth 	struct brevq_node	*brn;
62247c478bd9Sstevel@tonic-gate 	struct mt_config_data	*mcd_head = NULL;
62257c478bd9Sstevel@tonic-gate 	struct mt_config_data	*mcd_tail = NULL;
62267c478bd9Sstevel@tonic-gate 	struct mt_config_data	*mcd;
62277c478bd9Sstevel@tonic-gate #ifdef DEBUG
62287c478bd9Sstevel@tonic-gate 	timestruc_t		end_time;
62297c478bd9Sstevel@tonic-gate 
62307c478bd9Sstevel@tonic-gate 	/* Update total_time in handle */
62317c478bd9Sstevel@tonic-gate 	gethrestime(&end_time);
62327c478bd9Sstevel@tonic-gate 	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
62337c478bd9Sstevel@tonic-gate #endif
62347c478bd9Sstevel@tonic-gate 
62357c478bd9Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
62367c478bd9Sstevel@tonic-gate 	dip = ddi_get_child(pdip);
62377c478bd9Sstevel@tonic-gate 	while (dip) {
62387c478bd9Sstevel@tonic-gate 		if (hdl->mtc_op == MT_UNCONFIG_OP && hdl->mtc_brevqp &&
62397c478bd9Sstevel@tonic-gate 		    !(DEVI_EVREMOVE(dip)) &&
62407c478bd9Sstevel@tonic-gate 		    i_ddi_node_state(dip) >= DS_INITIALIZED) {
62417c478bd9Sstevel@tonic-gate 			/*
62427c478bd9Sstevel@tonic-gate 			 * Enqueue this dip's deviname.
62437c478bd9Sstevel@tonic-gate 			 * No need to hold a lock while enqueuing since this
62447c478bd9Sstevel@tonic-gate 			 * is the only thread doing the enqueue and no one
62457c478bd9Sstevel@tonic-gate 			 * walks the queue while we are in multithreaded
62467c478bd9Sstevel@tonic-gate 			 * unconfiguration.
62477c478bd9Sstevel@tonic-gate 			 */
62487c478bd9Sstevel@tonic-gate 			brn = brevq_enqueue(hdl->mtc_brevqp, dip, NULL);
6249*245c82d9Scth 		} else
6250*245c82d9Scth 			brn = NULL;
62517c478bd9Sstevel@tonic-gate 
62527c478bd9Sstevel@tonic-gate 		/*
62537c478bd9Sstevel@tonic-gate 		 * Hold the child that we are processing so he does not get
62547c478bd9Sstevel@tonic-gate 		 * removed. The corrisponding ndi_rele_devi() for children
62557c478bd9Sstevel@tonic-gate 		 * that are not being skipped is done at the end of
62567c478bd9Sstevel@tonic-gate 		 * mt_config_thread().
62577c478bd9Sstevel@tonic-gate 		 */
62587c478bd9Sstevel@tonic-gate 		ndi_hold_devi(dip);
62597c478bd9Sstevel@tonic-gate 
62607c478bd9Sstevel@tonic-gate 		/*
62617c478bd9Sstevel@tonic-gate 		 * skip leaf nodes and (for configure) nodes not
62627c478bd9Sstevel@tonic-gate 		 * fully attached.
62637c478bd9Sstevel@tonic-gate 		 */
62647c478bd9Sstevel@tonic-gate 		if (is_leaf_node(dip) ||
62657c478bd9Sstevel@tonic-gate 		    (hdl->mtc_op == MT_CONFIG_OP &&
62667c478bd9Sstevel@tonic-gate 		    i_ddi_node_state(dip) < DS_READY)) {
62677c478bd9Sstevel@tonic-gate 			ndi_rele_devi(dip);
62687c478bd9Sstevel@tonic-gate 			dip = ddi_get_next_sibling(dip);
62697c478bd9Sstevel@tonic-gate 			continue;
62707c478bd9Sstevel@tonic-gate 		}
62717c478bd9Sstevel@tonic-gate 
62727c478bd9Sstevel@tonic-gate 		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
62737c478bd9Sstevel@tonic-gate 		mcd->mtc_dip = dip;
62747c478bd9Sstevel@tonic-gate 		mcd->mtc_hdl = hdl;
62757c478bd9Sstevel@tonic-gate 		mcd->mtc_brn = brn;
62767c478bd9Sstevel@tonic-gate 
62777c478bd9Sstevel@tonic-gate 		/*
62787c478bd9Sstevel@tonic-gate 		 * Switch a 'driver' operation to an 'all' operation below a
62797c478bd9Sstevel@tonic-gate 		 * node bound to the driver.
62807c478bd9Sstevel@tonic-gate 		 */
62818c1b6884Sszhou 		if ((major == (major_t)-1) || (major == ddi_driver_major(dip)))
62827c478bd9Sstevel@tonic-gate 			mcd->mtc_major = (major_t)-1;
62837c478bd9Sstevel@tonic-gate 		else
62847c478bd9Sstevel@tonic-gate 			mcd->mtc_major = major;
62857c478bd9Sstevel@tonic-gate 
62867c478bd9Sstevel@tonic-gate 		/*
62877c478bd9Sstevel@tonic-gate 		 * The unconfig-driver to unconfig-all conversion above
62887c478bd9Sstevel@tonic-gate 		 * constitutes an autodetach for NDI_DETACH_DRIVER calls,
62897c478bd9Sstevel@tonic-gate 		 * set NDI_AUTODETACH.
62907c478bd9Sstevel@tonic-gate 		 */
62917c478bd9Sstevel@tonic-gate 		mcd->mtc_flags = hdl->mtc_flags;
62927c478bd9Sstevel@tonic-gate 		if ((mcd->mtc_flags & NDI_DETACH_DRIVER) &&
62937c478bd9Sstevel@tonic-gate 		    (hdl->mtc_op == MT_UNCONFIG_OP) &&
62947c478bd9Sstevel@tonic-gate 		    (major == ddi_driver_major(pdip)))
62957c478bd9Sstevel@tonic-gate 			mcd->mtc_flags |= NDI_AUTODETACH;
62967c478bd9Sstevel@tonic-gate 
62977c478bd9Sstevel@tonic-gate 		mutex_enter(&hdl->mtc_lock);
62987c478bd9Sstevel@tonic-gate 		hdl->mtc_thr_count++;
62997c478bd9Sstevel@tonic-gate 		mutex_exit(&hdl->mtc_lock);
63007c478bd9Sstevel@tonic-gate 
63017c478bd9Sstevel@tonic-gate 		/*
63027c478bd9Sstevel@tonic-gate 		 * Add to end of list to process after ndi_devi_exit to avoid
63037c478bd9Sstevel@tonic-gate 		 * locking differences depending on value of mtc_off.
63047c478bd9Sstevel@tonic-gate 		 */
63057c478bd9Sstevel@tonic-gate 		mcd->mtc_next = NULL;
63067c478bd9Sstevel@tonic-gate 		if (mcd_head == NULL)
63077c478bd9Sstevel@tonic-gate 			mcd_head = mcd;
63087c478bd9Sstevel@tonic-gate 		else
63097c478bd9Sstevel@tonic-gate 			mcd_tail->mtc_next = mcd;
63107c478bd9Sstevel@tonic-gate 		mcd_tail = mcd;
63117c478bd9Sstevel@tonic-gate 
63127c478bd9Sstevel@tonic-gate 		dip = ddi_get_next_sibling(dip);
63137c478bd9Sstevel@tonic-gate 	}
63147c478bd9Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
63157c478bd9Sstevel@tonic-gate 
63167c478bd9Sstevel@tonic-gate 	/* go through the list of held children */
63177c478bd9Sstevel@tonic-gate 	for (mcd = mcd_head; mcd; mcd = mcd_head) {
63187c478bd9Sstevel@tonic-gate 		mcd_head = mcd->mtc_next;
63197c478bd9Sstevel@tonic-gate 		if (mtc_off)
63207c478bd9Sstevel@tonic-gate 			mt_config_thread(mcd);
63217c478bd9Sstevel@tonic-gate 		else
63227c478bd9Sstevel@tonic-gate 			(void) thread_create(NULL, 0, mt_config_thread, mcd,
63237c478bd9Sstevel@tonic-gate 			    0, &p0, TS_RUN, minclsyspri);
63247c478bd9Sstevel@tonic-gate 	}
63257c478bd9Sstevel@tonic-gate }
63267c478bd9Sstevel@tonic-gate 
63277c478bd9Sstevel@tonic-gate static void
63287c478bd9Sstevel@tonic-gate mt_config_driver(struct mt_config_handle *hdl)
63297c478bd9Sstevel@tonic-gate {
63307c478bd9Sstevel@tonic-gate 	major_t			par_major = hdl->mtc_parmajor;
63317c478bd9Sstevel@tonic-gate 	major_t			major = hdl->mtc_major;
63327c478bd9Sstevel@tonic-gate 	struct devnames		*dnp = &devnamesp[par_major];
63337c478bd9Sstevel@tonic-gate 	dev_info_t		*dip;
63347c478bd9Sstevel@tonic-gate 	struct mt_config_data	*mcd_head = NULL;
63357c478bd9Sstevel@tonic-gate 	struct mt_config_data	*mcd_tail = NULL;
63367c478bd9Sstevel@tonic-gate 	struct mt_config_data	*mcd;
63377c478bd9Sstevel@tonic-gate #ifdef DEBUG
63387c478bd9Sstevel@tonic-gate 	timestruc_t		end_time;
63397c478bd9Sstevel@tonic-gate 
63407c478bd9Sstevel@tonic-gate 	/* Update total_time in handle */
63417c478bd9Sstevel@tonic-gate 	gethrestime(&end_time);
63427c478bd9Sstevel@tonic-gate 	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
63437c478bd9Sstevel@tonic-gate #endif
63447c478bd9Sstevel@tonic-gate 	ASSERT(par_major != (major_t)-1);
63457c478bd9Sstevel@tonic-gate 	ASSERT(major != (major_t)-1);
63467c478bd9Sstevel@tonic-gate 
63477c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
63487c478bd9Sstevel@tonic-gate 	dip = devnamesp[par_major].dn_head;
63497c478bd9Sstevel@tonic-gate 	while (dip) {
63507c478bd9Sstevel@tonic-gate 		/*
63517c478bd9Sstevel@tonic-gate 		 * Hold the child that we are processing so he does not get
63527c478bd9Sstevel@tonic-gate 		 * removed. The corrisponding ndi_rele_devi() for children
63537c478bd9Sstevel@tonic-gate 		 * that are not being skipped is done at the end of
63547c478bd9Sstevel@tonic-gate 		 * mt_config_thread().
63557c478bd9Sstevel@tonic-gate 		 */
63567c478bd9Sstevel@tonic-gate 		ndi_hold_devi(dip);
63577c478bd9Sstevel@tonic-gate 
63587c478bd9Sstevel@tonic-gate 		/* skip leaf nodes and nodes not fully attached */
63597c478bd9Sstevel@tonic-gate 		if ((i_ddi_node_state(dip) < DS_READY) || is_leaf_node(dip)) {
63607c478bd9Sstevel@tonic-gate 			ndi_rele_devi(dip);
63617c478bd9Sstevel@tonic-gate 			dip = ddi_get_next(dip);
63627c478bd9Sstevel@tonic-gate 			continue;
63637c478bd9Sstevel@tonic-gate 		}
63647c478bd9Sstevel@tonic-gate 
63657c478bd9Sstevel@tonic-gate 		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
63667c478bd9Sstevel@tonic-gate 		mcd->mtc_dip = dip;
63677c478bd9Sstevel@tonic-gate 		mcd->mtc_hdl = hdl;
63687c478bd9Sstevel@tonic-gate 		mcd->mtc_major = major;
63697c478bd9Sstevel@tonic-gate 		mcd->mtc_flags = hdl->mtc_flags;
63707c478bd9Sstevel@tonic-gate 
63717c478bd9Sstevel@tonic-gate 		mutex_enter(&hdl->mtc_lock);
63727c478bd9Sstevel@tonic-gate 		hdl->mtc_thr_count++;
63737c478bd9Sstevel@tonic-gate 		mutex_exit(&hdl->mtc_lock);
63747c478bd9Sstevel@tonic-gate 
63757c478bd9Sstevel@tonic-gate 		/*
63767c478bd9Sstevel@tonic-gate 		 * Add to end of list to process after UNLOCK_DEV_OPS to avoid
63777c478bd9Sstevel@tonic-gate 		 * locking differences depending on value of mtc_off.
63787c478bd9Sstevel@tonic-gate 		 */
63797c478bd9Sstevel@tonic-gate 		mcd->mtc_next = NULL;
63807c478bd9Sstevel@tonic-gate 		if (mcd_head == NULL)
63817c478bd9Sstevel@tonic-gate 			mcd_head = mcd;
63827c478bd9Sstevel@tonic-gate 		else
63837c478bd9Sstevel@tonic-gate 			mcd_tail->mtc_next = mcd;
63847c478bd9Sstevel@tonic-gate 		mcd_tail = mcd;
63857c478bd9Sstevel@tonic-gate 
63867c478bd9Sstevel@tonic-gate 		dip = ddi_get_next(dip);
63877c478bd9Sstevel@tonic-gate 	}
63887c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
63897c478bd9Sstevel@tonic-gate 
63907c478bd9Sstevel@tonic-gate 	/* go through the list of held children */
63917c478bd9Sstevel@tonic-gate 	for (mcd = mcd_head; mcd; mcd = mcd_head) {
63927c478bd9Sstevel@tonic-gate 		mcd_head = mcd->mtc_next;
63937c478bd9Sstevel@tonic-gate 		if (mtc_off)
63947c478bd9Sstevel@tonic-gate 			mt_config_thread(mcd);
63957c478bd9Sstevel@tonic-gate 		else
63967c478bd9Sstevel@tonic-gate 			(void) thread_create(NULL, 0, mt_config_thread, mcd,
63977c478bd9Sstevel@tonic-gate 			    0, &p0, TS_RUN, minclsyspri);
63987c478bd9Sstevel@tonic-gate 	}
63997c478bd9Sstevel@tonic-gate }
64007c478bd9Sstevel@tonic-gate 
64017c478bd9Sstevel@tonic-gate /*
64027c478bd9Sstevel@tonic-gate  * Given the nodeid for a persistent (PROM or SID) node, return
64037c478bd9Sstevel@tonic-gate  * the corresponding devinfo node
64047c478bd9Sstevel@tonic-gate  * NOTE: This function will return NULL for .conf nodeids.
64057c478bd9Sstevel@tonic-gate  */
64067c478bd9Sstevel@tonic-gate dev_info_t *
6407fa9e4066Sahrens e_ddi_nodeid_to_dip(pnode_t nodeid)
64087c478bd9Sstevel@tonic-gate {
64097c478bd9Sstevel@tonic-gate 	dev_info_t		*dip = NULL;
64107c478bd9Sstevel@tonic-gate 	struct devi_nodeid	*prev, *elem;
64117c478bd9Sstevel@tonic-gate 
64127c478bd9Sstevel@tonic-gate 	mutex_enter(&devimap->dno_lock);
64137c478bd9Sstevel@tonic-gate 
64147c478bd9Sstevel@tonic-gate 	prev = NULL;
64157c478bd9Sstevel@tonic-gate 	for (elem = devimap->dno_head; elem; elem = elem->next) {
64167c478bd9Sstevel@tonic-gate 		if (elem->nodeid == nodeid) {
64177c478bd9Sstevel@tonic-gate 			ndi_hold_devi(elem->dip);
64187c478bd9Sstevel@tonic-gate 			dip = elem->dip;
64197c478bd9Sstevel@tonic-gate 			break;
64207c478bd9Sstevel@tonic-gate 		}
64217c478bd9Sstevel@tonic-gate 		prev = elem;
64227c478bd9Sstevel@tonic-gate 	}
64237c478bd9Sstevel@tonic-gate 
64247c478bd9Sstevel@tonic-gate 	/*
64257c478bd9Sstevel@tonic-gate 	 * Move to head for faster lookup next time
64267c478bd9Sstevel@tonic-gate 	 */
64277c478bd9Sstevel@tonic-gate 	if (elem && prev) {
64287c478bd9Sstevel@tonic-gate 		prev->next = elem->next;
64297c478bd9Sstevel@tonic-gate 		elem->next = devimap->dno_head;
64307c478bd9Sstevel@tonic-gate 		devimap->dno_head = elem;
64317c478bd9Sstevel@tonic-gate 	}
64327c478bd9Sstevel@tonic-gate 
64337c478bd9Sstevel@tonic-gate 	mutex_exit(&devimap->dno_lock);
64347c478bd9Sstevel@tonic-gate 	return (dip);
64357c478bd9Sstevel@tonic-gate }
64367c478bd9Sstevel@tonic-gate 
64377c478bd9Sstevel@tonic-gate static void
64387c478bd9Sstevel@tonic-gate free_cache_task(void *arg)
64397c478bd9Sstevel@tonic-gate {
64407c478bd9Sstevel@tonic-gate 	ASSERT(arg == NULL);
64417c478bd9Sstevel@tonic-gate 
64427c478bd9Sstevel@tonic-gate 	mutex_enter(&di_cache.cache_lock);
64437c478bd9Sstevel@tonic-gate 
64447c478bd9Sstevel@tonic-gate 	/*
64457c478bd9Sstevel@tonic-gate 	 * The cache can be invalidated without holding the lock
64467c478bd9Sstevel@tonic-gate 	 * but it can be made valid again only while the lock is held.
64477c478bd9Sstevel@tonic-gate 	 * So if the cache is invalid when the lock is held, it will
64487c478bd9Sstevel@tonic-gate 	 * stay invalid until lock is released.
64497c478bd9Sstevel@tonic-gate 	 */
64507c478bd9Sstevel@tonic-gate 	if (!di_cache.cache_valid)
64517c478bd9Sstevel@tonic-gate 		i_ddi_di_cache_free(&di_cache);
64527c478bd9Sstevel@tonic-gate 
64537c478bd9Sstevel@tonic-gate 	mutex_exit(&di_cache.cache_lock);
64547c478bd9Sstevel@tonic-gate 
64557c478bd9Sstevel@tonic-gate 	if (di_cache_debug)
64567c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "system_taskq: di_cache freed");
64577c478bd9Sstevel@tonic-gate }
64587c478bd9Sstevel@tonic-gate 
64597c478bd9Sstevel@tonic-gate extern int modrootloaded;
64607c478bd9Sstevel@tonic-gate 
64617c478bd9Sstevel@tonic-gate void
64627c478bd9Sstevel@tonic-gate i_ddi_di_cache_free(struct di_cache *cache)
64637c478bd9Sstevel@tonic-gate {
64647c478bd9Sstevel@tonic-gate 	int	error;
64657c478bd9Sstevel@tonic-gate 
64667c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&cache->cache_lock));
64677c478bd9Sstevel@tonic-gate 
64687c478bd9Sstevel@tonic-gate 	if (cache->cache_size) {
64697c478bd9Sstevel@tonic-gate 		ASSERT(cache->cache_size > 0);
64707c478bd9Sstevel@tonic-gate 		ASSERT(cache->cache_data);
64717c478bd9Sstevel@tonic-gate 
64727c478bd9Sstevel@tonic-gate 		kmem_free(cache->cache_data, cache->cache_size);
64737c478bd9Sstevel@tonic-gate 		cache->cache_data = NULL;
64747c478bd9Sstevel@tonic-gate 		cache->cache_size = 0;
64757c478bd9Sstevel@tonic-gate 
64767c478bd9Sstevel@tonic-gate 		if (di_cache_debug)
64777c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "i_ddi_di_cache_free: freed cachemem");
64787c478bd9Sstevel@tonic-gate 	} else {
64797c478bd9Sstevel@tonic-gate 		ASSERT(cache->cache_data == NULL);
64807c478bd9Sstevel@tonic-gate 		if (di_cache_debug)
64817c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "i_ddi_di_cache_free: NULL cache");
64827c478bd9Sstevel@tonic-gate 	}
64837c478bd9Sstevel@tonic-gate 
64847c478bd9Sstevel@tonic-gate 	if (!modrootloaded || rootvp == NULL || vn_is_readonly(rootvp)) {
64857c478bd9Sstevel@tonic-gate 		if (di_cache_debug) {
64867c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "/ not mounted/RDONLY. Skip unlink");
64877c478bd9Sstevel@tonic-gate 		}
64887c478bd9Sstevel@tonic-gate 		return;
64897c478bd9Sstevel@tonic-gate 	}
64907c478bd9Sstevel@tonic-gate 
64917c478bd9Sstevel@tonic-gate 	error = vn_remove(DI_CACHE_FILE, UIO_SYSSPACE, RMFILE);
64927c478bd9Sstevel@tonic-gate 	if (di_cache_debug && error && error != ENOENT) {
64937c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: unlink failed: %d", DI_CACHE_FILE, error);
64947c478bd9Sstevel@tonic-gate 	} else if (di_cache_debug && !error) {
64957c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "i_ddi_di_cache_free: unlinked cache file");
64967c478bd9Sstevel@tonic-gate 	}
64977c478bd9Sstevel@tonic-gate }
64987c478bd9Sstevel@tonic-gate 
64997c478bd9Sstevel@tonic-gate void
65007c478bd9Sstevel@tonic-gate i_ddi_di_cache_invalidate(int kmflag)
65017c478bd9Sstevel@tonic-gate {
65027c478bd9Sstevel@tonic-gate 	uint_t	flag;
65037c478bd9Sstevel@tonic-gate 
65047c478bd9Sstevel@tonic-gate 	if (!modrootloaded || !i_ddi_io_initialized()) {
65057c478bd9Sstevel@tonic-gate 		if (di_cache_debug)
65067c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "I/O not inited. Skipping invalidate");
65077c478bd9Sstevel@tonic-gate 		return;
65087c478bd9Sstevel@tonic-gate 	}
65097c478bd9Sstevel@tonic-gate 
65107c478bd9Sstevel@tonic-gate 	/*
65117c478bd9Sstevel@tonic-gate 	 * Invalidate the in-core cache
65127c478bd9Sstevel@tonic-gate 	 */
65137c478bd9Sstevel@tonic-gate 	atomic_and_32(&di_cache.cache_valid, 0);
65147c478bd9Sstevel@tonic-gate 
65157c478bd9Sstevel@tonic-gate 	flag = (kmflag == KM_SLEEP) ? TQ_SLEEP : TQ_NOSLEEP;
65167c478bd9Sstevel@tonic-gate 
65177c478bd9Sstevel@tonic-gate 	(void) taskq_dispatch(system_taskq, free_cache_task, NULL, flag);
65187c478bd9Sstevel@tonic-gate 
65197c478bd9Sstevel@tonic-gate 	if (di_cache_debug) {
65207c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "invalidation with km_flag: %s",
65217c478bd9Sstevel@tonic-gate 		    kmflag == KM_SLEEP ? "KM_SLEEP" : "KM_NOSLEEP");
65227c478bd9Sstevel@tonic-gate 	}
65237c478bd9Sstevel@tonic-gate }
65247c478bd9Sstevel@tonic-gate 
65257c478bd9Sstevel@tonic-gate 
65267c478bd9Sstevel@tonic-gate static void
65277c478bd9Sstevel@tonic-gate i_bind_vhci_node(dev_info_t *dip)
65287c478bd9Sstevel@tonic-gate {
65297c478bd9Sstevel@tonic-gate 	char	*node_name;
65307c478bd9Sstevel@tonic-gate 
65317c478bd9Sstevel@tonic-gate 	node_name = i_ddi_strdup(ddi_node_name(dip), KM_SLEEP);
65327c478bd9Sstevel@tonic-gate 	i_ddi_set_binding_name(dip, node_name);
65337c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_major = ddi_name_to_major(node_name);
65347c478bd9Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_BOUND);
65357c478bd9Sstevel@tonic-gate }
65367c478bd9Sstevel@tonic-gate 
65377c478bd9Sstevel@tonic-gate 
65387c478bd9Sstevel@tonic-gate static void
65397c478bd9Sstevel@tonic-gate i_free_vhci_bind_name(dev_info_t *dip)
65407c478bd9Sstevel@tonic-gate {
65417c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_binding_name) {
65427c478bd9Sstevel@tonic-gate 		kmem_free(DEVI(dip)->devi_binding_name,
65437c478bd9Sstevel@tonic-gate 		    sizeof (ddi_node_name(dip)));
65447c478bd9Sstevel@tonic-gate 	}
65457c478bd9Sstevel@tonic-gate }
65467c478bd9Sstevel@tonic-gate 
65477c478bd9Sstevel@tonic-gate 
65487c478bd9Sstevel@tonic-gate static char vhci_node_addr[2];
65497c478bd9Sstevel@tonic-gate 
65507c478bd9Sstevel@tonic-gate static int
65517c478bd9Sstevel@tonic-gate i_init_vhci_node(dev_info_t *dip)
65527c478bd9Sstevel@tonic-gate {
65537c478bd9Sstevel@tonic-gate 	add_global_props(dip);
65547c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
65557c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_ops == NULL)
65567c478bd9Sstevel@tonic-gate 		return (-1);
65577c478bd9Sstevel@tonic-gate 
65587c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
65597c478bd9Sstevel@tonic-gate 	e_ddi_keep_instance(dip);
65607c478bd9Sstevel@tonic-gate 	vhci_node_addr[0]	= '\0';
65617c478bd9Sstevel@tonic-gate 	ddi_set_name_addr(dip, vhci_node_addr);
65627c478bd9Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_INITIALIZED);
65637c478bd9Sstevel@tonic-gate 	return (0);
65647c478bd9Sstevel@tonic-gate }
65657c478bd9Sstevel@tonic-gate 
65667c478bd9Sstevel@tonic-gate static void
65677c478bd9Sstevel@tonic-gate i_link_vhci_node(dev_info_t *dip)
65687c478bd9Sstevel@tonic-gate {
65699d3d2ed0Shiremath 	ASSERT(MUTEX_HELD(&global_vhci_lock));
65709d3d2ed0Shiremath 
65717c478bd9Sstevel@tonic-gate 	/*
65727c478bd9Sstevel@tonic-gate 	 * scsi_vhci should be kept left most of the device tree.
65737c478bd9Sstevel@tonic-gate 	 */
65747c478bd9Sstevel@tonic-gate 	if (scsi_vhci_dip) {
65757c478bd9Sstevel@tonic-gate 		DEVI(dip)->devi_sibling = DEVI(scsi_vhci_dip)->devi_sibling;
65767c478bd9Sstevel@tonic-gate 		DEVI(scsi_vhci_dip)->devi_sibling = DEVI(dip);
65777c478bd9Sstevel@tonic-gate 	} else {
65787c478bd9Sstevel@tonic-gate 		DEVI(dip)->devi_sibling = DEVI(top_devinfo)->devi_child;
65797c478bd9Sstevel@tonic-gate 		DEVI(top_devinfo)->devi_child = DEVI(dip);
65807c478bd9Sstevel@tonic-gate 	}
65817c478bd9Sstevel@tonic-gate }
65827c478bd9Sstevel@tonic-gate 
65837c478bd9Sstevel@tonic-gate 
65847c478bd9Sstevel@tonic-gate /*
65857c478bd9Sstevel@tonic-gate  * This a special routine to enumerate vhci node (child of rootnex
65867c478bd9Sstevel@tonic-gate  * node) without holding the ndi_devi_enter() lock. The device node
65877c478bd9Sstevel@tonic-gate  * is allocated, initialized and brought into DS_READY state before
65887c478bd9Sstevel@tonic-gate  * inserting into the device tree. The VHCI node is handcrafted
65897c478bd9Sstevel@tonic-gate  * here to bring the node to DS_READY, similar to rootnex node.
65907c478bd9Sstevel@tonic-gate  *
65917c478bd9Sstevel@tonic-gate  * The global_vhci_lock protects linking the node into the device
65927c478bd9Sstevel@tonic-gate  * as same lock is held before linking/unlinking any direct child
65937c478bd9Sstevel@tonic-gate  * of rootnex children.
65947c478bd9Sstevel@tonic-gate  *
65957c478bd9Sstevel@tonic-gate  * This routine is a workaround to handle a possible deadlock
65967c478bd9Sstevel@tonic-gate  * that occurs while trying to enumerate node in a different sub-tree
65977c478bd9Sstevel@tonic-gate  * during _init/_attach entry points.
65987c478bd9Sstevel@tonic-gate  */
65997c478bd9Sstevel@tonic-gate /*ARGSUSED*/
66007c478bd9Sstevel@tonic-gate dev_info_t *
66017c478bd9Sstevel@tonic-gate ndi_devi_config_vhci(char *drvname, int flags)
66027c478bd9Sstevel@tonic-gate {
66037c478bd9Sstevel@tonic-gate 	struct devnames		*dnp;
66047c478bd9Sstevel@tonic-gate 	dev_info_t		*dip;
66057c478bd9Sstevel@tonic-gate 	major_t			major = ddi_name_to_major(drvname);
66067c478bd9Sstevel@tonic-gate 
66077c478bd9Sstevel@tonic-gate 	if (major == -1)
66087c478bd9Sstevel@tonic-gate 		return (NULL);
66097c478bd9Sstevel@tonic-gate 
66107c478bd9Sstevel@tonic-gate 	/* Make sure we create the VHCI node only once */
66117c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
66127c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
66137c478bd9Sstevel@tonic-gate 	if (dnp->dn_head) {
66147c478bd9Sstevel@tonic-gate 		dip = dnp->dn_head;
66157c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
66167c478bd9Sstevel@tonic-gate 		return (dip);
66177c478bd9Sstevel@tonic-gate 	}
66187c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
66197c478bd9Sstevel@tonic-gate 
66207c478bd9Sstevel@tonic-gate 	/* Allocate the VHCI node */
66217c478bd9Sstevel@tonic-gate 	ndi_devi_alloc_sleep(top_devinfo, drvname, DEVI_SID_NODEID, &dip);
66227c478bd9Sstevel@tonic-gate 	ndi_hold_devi(dip);
66237c478bd9Sstevel@tonic-gate 
66247c478bd9Sstevel@tonic-gate 	/* Mark the node as VHCI */
66257c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_node_attributes |= DDI_VHCI_NODE;
66267c478bd9Sstevel@tonic-gate 
66277c478bd9Sstevel@tonic-gate 	i_ddi_add_devimap(dip);
66287c478bd9Sstevel@tonic-gate 	i_bind_vhci_node(dip);
66297c478bd9Sstevel@tonic-gate 	if (i_init_vhci_node(dip) == -1) {
66307c478bd9Sstevel@tonic-gate 		i_free_vhci_bind_name(dip);
66317c478bd9Sstevel@tonic-gate 		ndi_rele_devi(dip);
66327c478bd9Sstevel@tonic-gate 		(void) ndi_devi_free(dip);
66337c478bd9Sstevel@tonic-gate 		return (NULL);
66347c478bd9Sstevel@tonic-gate 	}
66357c478bd9Sstevel@tonic-gate 
663616747f41Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
66377c478bd9Sstevel@tonic-gate 	DEVI_SET_ATTACHING(dip);
663816747f41Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
663916747f41Scth 
66407c478bd9Sstevel@tonic-gate 	if (devi_attach(dip, DDI_ATTACH) != DDI_SUCCESS) {
66417c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "Could not attach %s driver", drvname);
66427c478bd9Sstevel@tonic-gate 		e_ddi_free_instance(dip, vhci_node_addr);
66437c478bd9Sstevel@tonic-gate 		i_free_vhci_bind_name(dip);
66447c478bd9Sstevel@tonic-gate 		ndi_rele_devi(dip);
66457c478bd9Sstevel@tonic-gate 		(void) ndi_devi_free(dip);
66467c478bd9Sstevel@tonic-gate 		return (NULL);
66477c478bd9Sstevel@tonic-gate 	}
664816747f41Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
66497c478bd9Sstevel@tonic-gate 	DEVI_CLR_ATTACHING(dip);
665016747f41Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
66517c478bd9Sstevel@tonic-gate 
66529d3d2ed0Shiremath 	mutex_enter(&global_vhci_lock);
66537c478bd9Sstevel@tonic-gate 	i_link_vhci_node(dip);
66549d3d2ed0Shiremath 	mutex_exit(&global_vhci_lock);
66557c478bd9Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_READY);
66567c478bd9Sstevel@tonic-gate 
66577c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
66587c478bd9Sstevel@tonic-gate 	dnp->dn_flags |= DN_DRIVER_HELD;
66597c478bd9Sstevel@tonic-gate 	dnp->dn_head = dip;
66607c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
66617c478bd9Sstevel@tonic-gate 
66627c478bd9Sstevel@tonic-gate 	i_ndi_devi_report_status_change(dip, NULL);
66637c478bd9Sstevel@tonic-gate 
66647c478bd9Sstevel@tonic-gate 	return (dip);
66657c478bd9Sstevel@tonic-gate }
66667c478bd9Sstevel@tonic-gate 
66677c478bd9Sstevel@tonic-gate /*
66687c478bd9Sstevel@tonic-gate  * ibt_hw_is_present() returns 0 when there is no IB hardware actively
66697c478bd9Sstevel@tonic-gate  * running.  This is primarily useful for modules like rpcmod which
66707c478bd9Sstevel@tonic-gate  * needs a quick check to decide whether or not it should try to use
66717c478bd9Sstevel@tonic-gate  * InfiniBand
66727c478bd9Sstevel@tonic-gate  */
66737c478bd9Sstevel@tonic-gate int ib_hw_status = 0;
66747c478bd9Sstevel@tonic-gate int
66757c478bd9Sstevel@tonic-gate ibt_hw_is_present()
66767c478bd9Sstevel@tonic-gate {
66777c478bd9Sstevel@tonic-gate 	return (ib_hw_status);
66787c478bd9Sstevel@tonic-gate }
6679