xref: /titanic_53/usr/src/uts/common/os/devcfg.c (revision 94c894bb831b66894f8e0b0bdcf8a9a450530820)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5144dfaa9Scth  * Common Development and Distribution License (the "License").
6144dfaa9Scth  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22bf002425SStephen Hanson  * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include <sys/note.h>
267c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
277c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
287c478bd9Sstevel@tonic-gate #include <sys/instance.h>
297c478bd9Sstevel@tonic-gate #include <sys/conf.h>
307c478bd9Sstevel@tonic-gate #include <sys/stat.h>
317c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
327c478bd9Sstevel@tonic-gate #include <sys/hwconf.h>
337c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
347c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
357c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
367c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
377c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
3825e8c5aaSvikram #include <sys/contract/device_impl.h>
397c478bd9Sstevel@tonic-gate #include <sys/dacf.h>
407c478bd9Sstevel@tonic-gate #include <sys/promif.h>
4119397407SSherry Moore #include <sys/pci.h>
427c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
437c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
447c478bd9Sstevel@tonic-gate #include <sys/taskq.h>
457c478bd9Sstevel@tonic-gate #include <sys/sysevent.h>
467c478bd9Sstevel@tonic-gate #include <sys/sunmdi.h>
477c478bd9Sstevel@tonic-gate #include <sys/stream.h>
487c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
497c478bd9Sstevel@tonic-gate #include <sys/fs/snode.h>
507c478bd9Sstevel@tonic-gate #include <sys/fs/dv_node.h>
51facf4a8dSllai1 #include <sys/reboot.h>
5225e8c5aaSvikram #include <sys/sysmacros.h>
5319397407SSherry Moore #include <sys/systm.h>
543c5e027bSEric Taylor #include <sys/fs/sdev_impl.h>
5525e8c5aaSvikram #include <sys/sunldi.h>
5625e8c5aaSvikram #include <sys/sunldi_impl.h>
57dedec472SJack Meng #include <sys/bootprops.h>
58*94c894bbSVikram Hegde #include <sys/varargs.h>
59*94c894bbSVikram Hegde #include <sys/modhash.h>
60*94c894bbSVikram Hegde #include <sys/instance.h>
61dedec472SJack Meng 
623a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
6309011d40SVikram Hegde #include <sys/iommulib.h>
6409011d40SVikram Hegde #endif
653a634bfcSVikram Hegde 
667c478bd9Sstevel@tonic-gate #ifdef DEBUG
677c478bd9Sstevel@tonic-gate int ddidebug = DDI_AUDIT;
687c478bd9Sstevel@tonic-gate #else
697c478bd9Sstevel@tonic-gate int ddidebug = 0;
707c478bd9Sstevel@tonic-gate #endif
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #define	MT_CONFIG_OP	0
737c478bd9Sstevel@tonic-gate #define	MT_UNCONFIG_OP	1
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /* Multi-threaded configuration */
767c478bd9Sstevel@tonic-gate struct mt_config_handle {
777c478bd9Sstevel@tonic-gate 	kmutex_t mtc_lock;
787c478bd9Sstevel@tonic-gate 	kcondvar_t mtc_cv;
797c478bd9Sstevel@tonic-gate 	int mtc_thr_count;
807c478bd9Sstevel@tonic-gate 	dev_info_t *mtc_pdip;	/* parent dip for mt_config_children */
817c478bd9Sstevel@tonic-gate 	dev_info_t **mtc_fdip;	/* "a" dip where unconfigure failed */
827c478bd9Sstevel@tonic-gate 	major_t mtc_parmajor;	/* parent major for mt_config_driver */
837c478bd9Sstevel@tonic-gate 	major_t mtc_major;
847c478bd9Sstevel@tonic-gate 	int mtc_flags;
857c478bd9Sstevel@tonic-gate 	int mtc_op;		/* config or unconfig */
867c478bd9Sstevel@tonic-gate 	int mtc_error;		/* operation error */
877c478bd9Sstevel@tonic-gate 	struct brevq_node **mtc_brevqp;	/* outstanding branch events queue */
887c478bd9Sstevel@tonic-gate #ifdef DEBUG
897c478bd9Sstevel@tonic-gate 	int total_time;
907c478bd9Sstevel@tonic-gate 	timestruc_t start_time;
917c478bd9Sstevel@tonic-gate #endif /* DEBUG */
927c478bd9Sstevel@tonic-gate };
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate struct devi_nodeid {
95fa9e4066Sahrens 	pnode_t nodeid;
967c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
977c478bd9Sstevel@tonic-gate 	struct devi_nodeid *next;
987c478bd9Sstevel@tonic-gate };
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate struct devi_nodeid_list {
1017c478bd9Sstevel@tonic-gate 	kmutex_t dno_lock;		/* Protects other fields */
1027c478bd9Sstevel@tonic-gate 	struct devi_nodeid *dno_head;	/* list of devi nodeid elements */
1037c478bd9Sstevel@tonic-gate 	struct devi_nodeid *dno_free;	/* Free list */
1047c478bd9Sstevel@tonic-gate 	uint_t dno_list_length;		/* number of dips in list */
1057c478bd9Sstevel@tonic-gate };
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /* used to keep track of branch remove events to be generated */
1087c478bd9Sstevel@tonic-gate struct brevq_node {
109245c82d9Scth 	char *brn_deviname;
110245c82d9Scth 	struct brevq_node *brn_sibling;
111245c82d9Scth 	struct brevq_node *brn_child;
1127c478bd9Sstevel@tonic-gate };
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate static struct devi_nodeid_list devi_nodeid_list;
1157c478bd9Sstevel@tonic-gate static struct devi_nodeid_list *devimap = &devi_nodeid_list;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate /*
1187c478bd9Sstevel@tonic-gate  * Well known nodes which are attached first at boot time.
1197c478bd9Sstevel@tonic-gate  */
1207c478bd9Sstevel@tonic-gate dev_info_t *top_devinfo;		/* root of device tree */
1217c478bd9Sstevel@tonic-gate dev_info_t *options_dip;
1227c478bd9Sstevel@tonic-gate dev_info_t *pseudo_dip;
1237c478bd9Sstevel@tonic-gate dev_info_t *clone_dip;
1247c478bd9Sstevel@tonic-gate dev_info_t *scsi_vhci_dip;		/* MPXIO dip */
1257c478bd9Sstevel@tonic-gate major_t clone_major;
1267c478bd9Sstevel@tonic-gate 
127facf4a8dSllai1 /*
128facf4a8dSllai1  * A non-global zone's /dev is derived from the device tree.
129facf4a8dSllai1  * This generation number serves to indicate when a zone's
130facf4a8dSllai1  * /dev may need to be updated.
131facf4a8dSllai1  */
132facf4a8dSllai1 volatile ulong_t devtree_gen;		/* generation number */
133facf4a8dSllai1 
1347c478bd9Sstevel@tonic-gate /* block all future dev_info state changes */
13519397407SSherry Moore hrtime_t volatile devinfo_freeze = 0;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /* number of dev_info attaches/detaches currently in progress */
1387c478bd9Sstevel@tonic-gate static ulong_t devinfo_attach_detach = 0;
1397c478bd9Sstevel@tonic-gate 
140c3b4ae18SJerry Gilliam extern int	sys_shutdown;
1417c478bd9Sstevel@tonic-gate extern kmutex_t global_vhci_lock;
1427c478bd9Sstevel@tonic-gate 
143facf4a8dSllai1 /* bitset of DS_SYSAVAIL & DS_RECONFIG - no races, no lock */
144facf4a8dSllai1 static int devname_state = 0;
145facf4a8dSllai1 
1467c478bd9Sstevel@tonic-gate /*
1477c478bd9Sstevel@tonic-gate  * The devinfo snapshot cache and related variables.
1487c478bd9Sstevel@tonic-gate  * The only field in the di_cache structure that needs initialization
1497c478bd9Sstevel@tonic-gate  * is the mutex (cache_lock). However, since this is an adaptive mutex
1507c478bd9Sstevel@tonic-gate  * (MUTEX_DEFAULT) - it is automatically initialized by being allocated
1517c478bd9Sstevel@tonic-gate  * in zeroed memory (static storage class). Therefore no explicit
1527c478bd9Sstevel@tonic-gate  * initialization of the di_cache structure is needed.
1537c478bd9Sstevel@tonic-gate  */
1547c478bd9Sstevel@tonic-gate struct di_cache	di_cache = {1};
1557c478bd9Sstevel@tonic-gate int		di_cache_debug = 0;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate /* For ddvis, which needs pseudo children under PCI */
1587c478bd9Sstevel@tonic-gate int pci_allow_pseudo_children = 0;
1597c478bd9Sstevel@tonic-gate 
160f4da9be0Scth /* Allow path-oriented alias driver binding on driver.conf enumerated nodes */
161f4da9be0Scth int driver_conf_allow_path_alias = 1;
162f4da9be0Scth 
1637c478bd9Sstevel@tonic-gate /*
1647c478bd9Sstevel@tonic-gate  * The following switch is for service people, in case a
1657c478bd9Sstevel@tonic-gate  * 3rd party driver depends on identify(9e) being called.
1667c478bd9Sstevel@tonic-gate  */
1677c478bd9Sstevel@tonic-gate int identify_9e = 0;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate int mtc_off;					/* turn off mt config */
1707c478bd9Sstevel@tonic-gate 
17119397407SSherry Moore int quiesce_debug = 0;
17219397407SSherry Moore 
173*94c894bbSVikram Hegde boolean_t ddi_aliases_present = B_FALSE;
174*94c894bbSVikram Hegde ddi_alias_t ddi_aliases;
175*94c894bbSVikram Hegde uint_t tsd_ddi_redirect;
176*94c894bbSVikram Hegde 
177*94c894bbSVikram Hegde #define	DDI_ALIAS_HASH_SIZE	(2700)
178*94c894bbSVikram Hegde 
1797c478bd9Sstevel@tonic-gate static kmem_cache_t *ddi_node_cache;		/* devinfo node cache */
1807c478bd9Sstevel@tonic-gate static devinfo_log_header_t *devinfo_audit_log;	/* devinfo log */
1817c478bd9Sstevel@tonic-gate static int devinfo_log_size;			/* size in pages */
1827c478bd9Sstevel@tonic-gate 
183*94c894bbSVikram Hegde boolean_t ddi_err_panic = B_FALSE;
184*94c894bbSVikram Hegde 
1857c478bd9Sstevel@tonic-gate static int lookup_compatible(dev_info_t *, uint_t);
1867c478bd9Sstevel@tonic-gate static char *encode_composite_string(char **, uint_t, size_t *, uint_t);
1877c478bd9Sstevel@tonic-gate static void link_to_driver_list(dev_info_t *);
1887c478bd9Sstevel@tonic-gate static void unlink_from_driver_list(dev_info_t *);
1897c478bd9Sstevel@tonic-gate static void add_to_dn_list(struct devnames *, dev_info_t *);
1907c478bd9Sstevel@tonic-gate static void remove_from_dn_list(struct devnames *, dev_info_t *);
1917c478bd9Sstevel@tonic-gate static dev_info_t *find_duplicate_child();
1927c478bd9Sstevel@tonic-gate static void add_global_props(dev_info_t *);
1937c478bd9Sstevel@tonic-gate static void remove_global_props(dev_info_t *);
1947c478bd9Sstevel@tonic-gate static int uninit_node(dev_info_t *);
1957c478bd9Sstevel@tonic-gate static void da_log_init(void);
1967c478bd9Sstevel@tonic-gate static void da_log_enter(dev_info_t *);
1977c478bd9Sstevel@tonic-gate static int walk_devs(dev_info_t *, int (*f)(dev_info_t *, void *), void *, int);
1987c478bd9Sstevel@tonic-gate static int reset_nexus_flags(dev_info_t *, void *);
1997c478bd9Sstevel@tonic-gate static void ddi_optimize_dtree(dev_info_t *);
2007c478bd9Sstevel@tonic-gate static int is_leaf_node(dev_info_t *);
2017c478bd9Sstevel@tonic-gate static struct mt_config_handle *mt_config_init(dev_info_t *, dev_info_t **,
2027c478bd9Sstevel@tonic-gate     int, major_t, int, struct brevq_node **);
2037c478bd9Sstevel@tonic-gate static void mt_config_children(struct mt_config_handle *);
2047c478bd9Sstevel@tonic-gate static void mt_config_driver(struct mt_config_handle *);
2057c478bd9Sstevel@tonic-gate static int mt_config_fini(struct mt_config_handle *);
2067c478bd9Sstevel@tonic-gate static int devi_unconfig_common(dev_info_t *, dev_info_t **, int, major_t,
2077c478bd9Sstevel@tonic-gate     struct brevq_node **);
2087c478bd9Sstevel@tonic-gate static int
2097c478bd9Sstevel@tonic-gate ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
2107c478bd9Sstevel@tonic-gate     dev_info_t **childp, int flags);
2119d3d2ed0Shiremath static void i_link_vhci_node(dev_info_t *);
2125e3986cbScth static void ndi_devi_exit_and_wait(dev_info_t *dip,
2135e3986cbScth     int circular, clock_t end_time);
214f4da9be0Scth static int ndi_devi_unbind_driver(dev_info_t *dip);
2157c478bd9Sstevel@tonic-gate 
21625e8c5aaSvikram static void i_ddi_check_retire(dev_info_t *dip);
21725e8c5aaSvikram 
21819397407SSherry Moore static void quiesce_one_device(dev_info_t *, void *);
21925e8c5aaSvikram 
220*94c894bbSVikram Hegde dev_info_t *ddi_alias_redirect(char *alias);
221*94c894bbSVikram Hegde char *ddi_curr_redirect(char *currpath);
222*94c894bbSVikram Hegde 
223*94c894bbSVikram Hegde 
2247c478bd9Sstevel@tonic-gate /*
2257c478bd9Sstevel@tonic-gate  * dev_info cache and node management
2267c478bd9Sstevel@tonic-gate  */
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate /* initialize dev_info node cache */
2297c478bd9Sstevel@tonic-gate void
2307c478bd9Sstevel@tonic-gate i_ddi_node_cache_init()
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate 	ASSERT(ddi_node_cache == NULL);
2337c478bd9Sstevel@tonic-gate 	ddi_node_cache = kmem_cache_create("dev_info_node_cache",
2347c478bd9Sstevel@tonic-gate 	    sizeof (struct dev_info), 0, NULL, NULL, NULL, NULL, NULL, 0);
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	if (ddidebug & DDI_AUDIT)
2377c478bd9Sstevel@tonic-gate 		da_log_init();
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate 
240fc256490SJason Beloro 
2417c478bd9Sstevel@tonic-gate /*
2427c478bd9Sstevel@tonic-gate  * Allocating a dev_info node, callable from interrupt context with KM_NOSLEEP
2437c478bd9Sstevel@tonic-gate  * The allocated node has a reference count of 0.
2447c478bd9Sstevel@tonic-gate  */
2457c478bd9Sstevel@tonic-gate dev_info_t *
246fa9e4066Sahrens i_ddi_alloc_node(dev_info_t *pdip, char *node_name, pnode_t nodeid,
2477c478bd9Sstevel@tonic-gate     int instance, ddi_prop_t *sys_prop, int flag)
2487c478bd9Sstevel@tonic-gate {
2497c478bd9Sstevel@tonic-gate 	struct dev_info *devi;
2507c478bd9Sstevel@tonic-gate 	struct devi_nodeid *elem;
2517c478bd9Sstevel@tonic-gate 	static char failed[] = "i_ddi_alloc_node: out of memory";
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	ASSERT(node_name != NULL);
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	if ((devi = kmem_cache_alloc(ddi_node_cache, flag)) == NULL) {
2567c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, failed);
2577c478bd9Sstevel@tonic-gate 		return (NULL);
2587c478bd9Sstevel@tonic-gate 	}
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	bzero(devi, sizeof (struct dev_info));
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	if (devinfo_audit_log) {
2637c478bd9Sstevel@tonic-gate 		devi->devi_audit = kmem_zalloc(sizeof (devinfo_audit_t), flag);
2647c478bd9Sstevel@tonic-gate 		if (devi->devi_audit == NULL)
2657c478bd9Sstevel@tonic-gate 			goto fail;
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	if ((devi->devi_node_name = i_ddi_strdup(node_name, flag)) == NULL)
2697c478bd9Sstevel@tonic-gate 		goto fail;
270f4da9be0Scth 
2717c478bd9Sstevel@tonic-gate 	/* default binding name is node name */
2727c478bd9Sstevel@tonic-gate 	devi->devi_binding_name = devi->devi_node_name;
273a204de77Scth 	devi->devi_major = DDI_MAJOR_T_NONE;	/* unbound by default */
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	/*
2767c478bd9Sstevel@tonic-gate 	 * Make a copy of system property
2777c478bd9Sstevel@tonic-gate 	 */
2787c478bd9Sstevel@tonic-gate 	if (sys_prop &&
2797c478bd9Sstevel@tonic-gate 	    (devi->devi_sys_prop_ptr = i_ddi_prop_list_dup(sys_prop, flag))
2807c478bd9Sstevel@tonic-gate 	    == NULL)
2817c478bd9Sstevel@tonic-gate 		goto fail;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	/*
2847c478bd9Sstevel@tonic-gate 	 * Assign devi_nodeid, devi_node_class, devi_node_attributes
2857c478bd9Sstevel@tonic-gate 	 * according to the following algorithm:
2867c478bd9Sstevel@tonic-gate 	 *
2877c478bd9Sstevel@tonic-gate 	 * nodeid arg			node class		node attributes
2887c478bd9Sstevel@tonic-gate 	 *
2897c478bd9Sstevel@tonic-gate 	 * DEVI_PSEUDO_NODEID		DDI_NC_PSEUDO		A
2907c478bd9Sstevel@tonic-gate 	 * DEVI_SID_NODEID		DDI_NC_PSEUDO		A,P
291027021c7SChris Horne 	 * DEVI_SID_HIDDEN_NODEID	DDI_NC_PSEUDO		A,P,H
2924c06356bSdh142964 	 * DEVI_SID_HP_NODEID		DDI_NC_PSEUDO		A,P,h
2934c06356bSdh142964 	 * DEVI_SID_HP_HIDDEN_NODEID	DDI_NC_PSEUDO		A,P,H,h
2947c478bd9Sstevel@tonic-gate 	 * other			DDI_NC_PROM		P
2957c478bd9Sstevel@tonic-gate 	 *
2967c478bd9Sstevel@tonic-gate 	 * Where A = DDI_AUTO_ASSIGNED_NODEID (auto-assign a nodeid)
2977c478bd9Sstevel@tonic-gate 	 * and	 P = DDI_PERSISTENT
298027021c7SChris Horne 	 * and	 H = DDI_HIDDEN_NODE
2994c06356bSdh142964 	 * and	 h = DDI_HOTPLUG_NODE
3007c478bd9Sstevel@tonic-gate 	 *
3017c478bd9Sstevel@tonic-gate 	 * auto-assigned nodeids are also auto-freed.
3027c478bd9Sstevel@tonic-gate 	 */
303027021c7SChris Horne 	devi->devi_node_attributes = 0;
3047c478bd9Sstevel@tonic-gate 	switch (nodeid) {
305027021c7SChris Horne 	case DEVI_SID_HIDDEN_NODEID:
306027021c7SChris Horne 		devi->devi_node_attributes |= DDI_HIDDEN_NODE;
3074c06356bSdh142964 		goto sid;
3084c06356bSdh142964 
3094c06356bSdh142964 	case DEVI_SID_HP_NODEID:
3104c06356bSdh142964 		devi->devi_node_attributes |= DDI_HOTPLUG_NODE;
3114c06356bSdh142964 		goto sid;
3124c06356bSdh142964 
3134c06356bSdh142964 	case DEVI_SID_HP_HIDDEN_NODEID:
3144c06356bSdh142964 		devi->devi_node_attributes |= DDI_HIDDEN_NODE;
3154c06356bSdh142964 		devi->devi_node_attributes |= DDI_HOTPLUG_NODE;
3164c06356bSdh142964 		goto sid;
3174c06356bSdh142964 
3187c478bd9Sstevel@tonic-gate 	case DEVI_SID_NODEID:
3194c06356bSdh142964 sid:		devi->devi_node_attributes |= DDI_PERSISTENT;
3207c478bd9Sstevel@tonic-gate 		if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL)
3217c478bd9Sstevel@tonic-gate 			goto fail;
3227c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
3234c06356bSdh142964 
3247c478bd9Sstevel@tonic-gate 	case DEVI_PSEUDO_NODEID:
3257c478bd9Sstevel@tonic-gate 		devi->devi_node_attributes |= DDI_AUTO_ASSIGNED_NODEID;
3267c478bd9Sstevel@tonic-gate 		devi->devi_node_class = DDI_NC_PSEUDO;
3277c478bd9Sstevel@tonic-gate 		if (impl_ddi_alloc_nodeid(&devi->devi_nodeid)) {
3287c478bd9Sstevel@tonic-gate 			panic("i_ddi_alloc_node: out of nodeids");
3297c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
3307c478bd9Sstevel@tonic-gate 		}
3317c478bd9Sstevel@tonic-gate 		break;
3324c06356bSdh142964 
3337c478bd9Sstevel@tonic-gate 	default:
3347c478bd9Sstevel@tonic-gate 		if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL)
3357c478bd9Sstevel@tonic-gate 			goto fail;
3364c06356bSdh142964 
3377c478bd9Sstevel@tonic-gate 		/*
3387c478bd9Sstevel@tonic-gate 		 * the nodetype is 'prom', try to 'take' the nodeid now.
3397c478bd9Sstevel@tonic-gate 		 * This requires memory allocation, so check for failure.
3407c478bd9Sstevel@tonic-gate 		 */
3417c478bd9Sstevel@tonic-gate 		if (impl_ddi_take_nodeid(nodeid, flag) != 0) {
3427c478bd9Sstevel@tonic-gate 			kmem_free(elem, sizeof (*elem));
3437c478bd9Sstevel@tonic-gate 			goto fail;
3447c478bd9Sstevel@tonic-gate 		}
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 		devi->devi_nodeid = nodeid;
3477c478bd9Sstevel@tonic-gate 		devi->devi_node_class = DDI_NC_PROM;
3487c478bd9Sstevel@tonic-gate 		devi->devi_node_attributes = DDI_PERSISTENT;
3494c06356bSdh142964 		break;
3507c478bd9Sstevel@tonic-gate 	}
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node((dev_info_t *)devi)) {
3537c478bd9Sstevel@tonic-gate 		mutex_enter(&devimap->dno_lock);
3547c478bd9Sstevel@tonic-gate 		elem->next = devimap->dno_free;
3557c478bd9Sstevel@tonic-gate 		devimap->dno_free = elem;
3567c478bd9Sstevel@tonic-gate 		mutex_exit(&devimap->dno_lock);
3577c478bd9Sstevel@tonic-gate 	}
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	/*
3607c478bd9Sstevel@tonic-gate 	 * Instance is normally initialized to -1. In a few special
3617c478bd9Sstevel@tonic-gate 	 * cases, the caller may specify an instance (e.g. CPU nodes).
3627c478bd9Sstevel@tonic-gate 	 */
3637c478bd9Sstevel@tonic-gate 	devi->devi_instance = instance;
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	/*
3667c478bd9Sstevel@tonic-gate 	 * set parent and bus_ctl parent
3677c478bd9Sstevel@tonic-gate 	 */
3687c478bd9Sstevel@tonic-gate 	devi->devi_parent = DEVI(pdip);
3697c478bd9Sstevel@tonic-gate 	devi->devi_bus_ctl = DEVI(pdip);
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
3727c478bd9Sstevel@tonic-gate 	    "i_ddi_alloc_node: name=%s id=%d\n", node_name, devi->devi_nodeid));
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	cv_init(&(devi->devi_cv), NULL, CV_DEFAULT, NULL);
3757c478bd9Sstevel@tonic-gate 	mutex_init(&(devi->devi_lock), NULL, MUTEX_DEFAULT, NULL);
3767c478bd9Sstevel@tonic-gate 	mutex_init(&(devi->devi_pm_lock), NULL, MUTEX_DEFAULT, NULL);
3777c478bd9Sstevel@tonic-gate 	mutex_init(&(devi->devi_pm_busy_lock), NULL, MUTEX_DEFAULT, NULL);
3787c478bd9Sstevel@tonic-gate 
37925e8c5aaSvikram 	RIO_TRACE((CE_NOTE, "i_ddi_alloc_node: Initing contract fields: "
38025e8c5aaSvikram 	    "dip=%p, name=%s", (void *)devi, node_name));
38125e8c5aaSvikram 
38225e8c5aaSvikram 	mutex_init(&(devi->devi_ct_lock), NULL, MUTEX_DEFAULT, NULL);
38325e8c5aaSvikram 	cv_init(&(devi->devi_ct_cv), NULL, CV_DEFAULT, NULL);
38425e8c5aaSvikram 	devi->devi_ct_count = -1;	/* counter not in use if -1 */
38525e8c5aaSvikram 	list_create(&(devi->devi_ct), sizeof (cont_device_t),
38625e8c5aaSvikram 	    offsetof(cont_device_t, cond_next));
38725e8c5aaSvikram 
3887c478bd9Sstevel@tonic-gate 	i_ddi_set_node_state((dev_info_t *)devi, DS_PROTO);
3897c478bd9Sstevel@tonic-gate 	da_log_enter((dev_info_t *)devi);
3907c478bd9Sstevel@tonic-gate 	return ((dev_info_t *)devi);
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate fail:
3937c478bd9Sstevel@tonic-gate 	if (devi->devi_sys_prop_ptr)
3947c478bd9Sstevel@tonic-gate 		i_ddi_prop_list_delete(devi->devi_sys_prop_ptr);
3957c478bd9Sstevel@tonic-gate 	if (devi->devi_node_name)
3967c478bd9Sstevel@tonic-gate 		kmem_free(devi->devi_node_name, strlen(node_name) + 1);
3977c478bd9Sstevel@tonic-gate 	if (devi->devi_audit)
3987c478bd9Sstevel@tonic-gate 		kmem_free(devi->devi_audit, sizeof (devinfo_audit_t));
3997c478bd9Sstevel@tonic-gate 	kmem_cache_free(ddi_node_cache, devi);
4007c478bd9Sstevel@tonic-gate 	cmn_err(CE_NOTE, failed);
4017c478bd9Sstevel@tonic-gate 	return (NULL);
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate /*
4057c478bd9Sstevel@tonic-gate  * free a dev_info structure.
4067c478bd9Sstevel@tonic-gate  * NB. Not callable from interrupt since impl_ddi_free_nodeid may block.
4077c478bd9Sstevel@tonic-gate  */
4087c478bd9Sstevel@tonic-gate void
4097c478bd9Sstevel@tonic-gate i_ddi_free_node(dev_info_t *dip)
4107c478bd9Sstevel@tonic-gate {
4117c478bd9Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
4127c478bd9Sstevel@tonic-gate 	struct devi_nodeid *elem;
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	ASSERT(devi->devi_ref == 0);
4157c478bd9Sstevel@tonic-gate 	ASSERT(devi->devi_addr == NULL);
4167c478bd9Sstevel@tonic-gate 	ASSERT(devi->devi_node_state == DS_PROTO);
4177c478bd9Sstevel@tonic-gate 	ASSERT(devi->devi_child == NULL);
41826947304SEvan Yan 	ASSERT(devi->devi_hp_hdlp == NULL);
4197c478bd9Sstevel@tonic-gate 
420fe9fe9fbScth 	/* free devi_addr_buf allocated by ddi_set_name_addr() */
421fe9fe9fbScth 	if (devi->devi_addr_buf)
422fe9fe9fbScth 		kmem_free(devi->devi_addr_buf, 2 * MAXNAMELEN);
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	if (i_ndi_dev_is_auto_assigned_node(dip))
4257c478bd9Sstevel@tonic-gate 		impl_ddi_free_nodeid(DEVI(dip)->devi_nodeid);
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
4287c478bd9Sstevel@tonic-gate 		mutex_enter(&devimap->dno_lock);
4297c478bd9Sstevel@tonic-gate 		ASSERT(devimap->dno_free);
4307c478bd9Sstevel@tonic-gate 		elem = devimap->dno_free;
4317c478bd9Sstevel@tonic-gate 		devimap->dno_free = elem->next;
4327c478bd9Sstevel@tonic-gate 		mutex_exit(&devimap->dno_lock);
4337c478bd9Sstevel@tonic-gate 		kmem_free(elem, sizeof (*elem));
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_compat_names)
4377c478bd9Sstevel@tonic-gate 		kmem_free(DEVI(dip)->devi_compat_names,
4387c478bd9Sstevel@tonic-gate 		    DEVI(dip)->devi_compat_length);
439f4da9be0Scth 	if (DEVI(dip)->devi_rebinding_name)
440f4da9be0Scth 		kmem_free(DEVI(dip)->devi_rebinding_name,
441f4da9be0Scth 		    strlen(DEVI(dip)->devi_rebinding_name) + 1);
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	ddi_prop_remove_all(dip);	/* remove driver properties */
4447c478bd9Sstevel@tonic-gate 	if (devi->devi_sys_prop_ptr)
4457c478bd9Sstevel@tonic-gate 		i_ddi_prop_list_delete(devi->devi_sys_prop_ptr);
4467c478bd9Sstevel@tonic-gate 	if (devi->devi_hw_prop_ptr)
4477c478bd9Sstevel@tonic-gate 		i_ddi_prop_list_delete(devi->devi_hw_prop_ptr);
4487c478bd9Sstevel@tonic-gate 
449602ca9eaScth 	if (DEVI(dip)->devi_devid_str)
450602ca9eaScth 		ddi_devid_str_free(DEVI(dip)->devi_devid_str);
451602ca9eaScth 
4527c478bd9Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_INVAL);
4537c478bd9Sstevel@tonic-gate 	da_log_enter(dip);
4547c478bd9Sstevel@tonic-gate 	if (devi->devi_audit) {
4557c478bd9Sstevel@tonic-gate 		kmem_free(devi->devi_audit, sizeof (devinfo_audit_t));
4567c478bd9Sstevel@tonic-gate 	}
4577c478bd9Sstevel@tonic-gate 	if (devi->devi_device_class)
4587c478bd9Sstevel@tonic-gate 		kmem_free(devi->devi_device_class,
4597c478bd9Sstevel@tonic-gate 		    strlen(devi->devi_device_class) + 1);
4607c478bd9Sstevel@tonic-gate 	cv_destroy(&(devi->devi_cv));
4617c478bd9Sstevel@tonic-gate 	mutex_destroy(&(devi->devi_lock));
4627c478bd9Sstevel@tonic-gate 	mutex_destroy(&(devi->devi_pm_lock));
4637c478bd9Sstevel@tonic-gate 	mutex_destroy(&(devi->devi_pm_busy_lock));
4647c478bd9Sstevel@tonic-gate 
46525e8c5aaSvikram 	RIO_TRACE((CE_NOTE, "i_ddi_free_node: destroying contract fields: "
46625e8c5aaSvikram 	    "dip=%p", (void *)dip));
46725e8c5aaSvikram 	contract_device_remove_dip(dip);
46825e8c5aaSvikram 	ASSERT(devi->devi_ct_count == -1);
46925e8c5aaSvikram 	ASSERT(list_is_empty(&(devi->devi_ct)));
47025e8c5aaSvikram 	cv_destroy(&(devi->devi_ct_cv));
47125e8c5aaSvikram 	list_destroy(&(devi->devi_ct));
47225e8c5aaSvikram 	/* free this last since contract_device_remove_dip() uses it */
47325e8c5aaSvikram 	mutex_destroy(&(devi->devi_ct_lock));
47425e8c5aaSvikram 	RIO_TRACE((CE_NOTE, "i_ddi_free_node: destroyed all contract fields: "
47525e8c5aaSvikram 	    "dip=%p, name=%s", (void *)dip, devi->devi_node_name));
47625e8c5aaSvikram 
47725e8c5aaSvikram 	kmem_free(devi->devi_node_name, strlen(devi->devi_node_name) + 1);
47825e8c5aaSvikram 
4797c478bd9Sstevel@tonic-gate 	kmem_cache_free(ddi_node_cache, devi);
4807c478bd9Sstevel@tonic-gate }
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate /*
4847c478bd9Sstevel@tonic-gate  * Node state transitions
4857c478bd9Sstevel@tonic-gate  */
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate /*
4887c478bd9Sstevel@tonic-gate  * Change the node name
4897c478bd9Sstevel@tonic-gate  */
4907c478bd9Sstevel@tonic-gate int
4917c478bd9Sstevel@tonic-gate ndi_devi_set_nodename(dev_info_t *dip, char *name, int flags)
4927c478bd9Sstevel@tonic-gate {
4937c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags))
4947c478bd9Sstevel@tonic-gate 	char *nname, *oname;
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	ASSERT(dip && name);
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	oname = DEVI(dip)->devi_node_name;
4997c478bd9Sstevel@tonic-gate 	if (strcmp(oname, name) == 0)
5007c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	/*
5037c478bd9Sstevel@tonic-gate 	 * pcicfg_fix_ethernet requires a name change after node
5047c478bd9Sstevel@tonic-gate 	 * is linked into the tree. When pcicfg is fixed, we
5057c478bd9Sstevel@tonic-gate 	 * should only allow name change in DS_PROTO state.
5067c478bd9Sstevel@tonic-gate 	 */
5077c478bd9Sstevel@tonic-gate 	if (i_ddi_node_state(dip) >= DS_BOUND) {
5087c478bd9Sstevel@tonic-gate 		/*
5097c478bd9Sstevel@tonic-gate 		 * Don't allow name change once node is bound
5107c478bd9Sstevel@tonic-gate 		 */
5117c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE,
5127c478bd9Sstevel@tonic-gate 		    "ndi_devi_set_nodename: node already bound dip = %p,"
5137c478bd9Sstevel@tonic-gate 		    " %s -> %s", (void *)dip, ddi_node_name(dip), name);
5147c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	nname = i_ddi_strdup(name, KM_SLEEP);
5187c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_node_name = nname;
5197c478bd9Sstevel@tonic-gate 	i_ddi_set_binding_name(dip, nname);
5207c478bd9Sstevel@tonic-gate 	kmem_free(oname, strlen(oname) + 1);
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	da_log_enter(dip);
5237c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate void
5277c478bd9Sstevel@tonic-gate i_ddi_add_devimap(dev_info_t *dip)
5287c478bd9Sstevel@tonic-gate {
5297c478bd9Sstevel@tonic-gate 	struct devi_nodeid *elem;
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	ASSERT(dip);
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	if (!ndi_dev_is_persistent_node(dip))
5347c478bd9Sstevel@tonic-gate 		return;
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	ASSERT(ddi_get_parent(dip) == NULL || (DEVI_VHCI_NODE(dip)) ||
5377c478bd9Sstevel@tonic-gate 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	mutex_enter(&devimap->dno_lock);
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	ASSERT(devimap->dno_free);
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	elem = devimap->dno_free;
5447c478bd9Sstevel@tonic-gate 	devimap->dno_free = elem->next;
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	elem->nodeid = ddi_get_nodeid(dip);
5477c478bd9Sstevel@tonic-gate 	elem->dip = dip;
5487c478bd9Sstevel@tonic-gate 	elem->next = devimap->dno_head;
5497c478bd9Sstevel@tonic-gate 	devimap->dno_head = elem;
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 	devimap->dno_list_length++;
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	mutex_exit(&devimap->dno_lock);
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate static int
5577c478bd9Sstevel@tonic-gate i_ddi_remove_devimap(dev_info_t *dip)
5587c478bd9Sstevel@tonic-gate {
5597c478bd9Sstevel@tonic-gate 	struct devi_nodeid *prev, *elem;
5607c478bd9Sstevel@tonic-gate 	static const char *fcn = "i_ddi_remove_devimap";
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	ASSERT(dip);
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	if (!ndi_dev_is_persistent_node(dip))
5657c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	mutex_enter(&devimap->dno_lock);
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	/*
5707c478bd9Sstevel@tonic-gate 	 * The following check is done with dno_lock held
5717c478bd9Sstevel@tonic-gate 	 * to prevent race between dip removal and
5727c478bd9Sstevel@tonic-gate 	 * e_ddi_prom_node_to_dip()
5737c478bd9Sstevel@tonic-gate 	 */
5747c478bd9Sstevel@tonic-gate 	if (e_ddi_devi_holdcnt(dip)) {
5757c478bd9Sstevel@tonic-gate 		mutex_exit(&devimap->dno_lock);
5767c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
5777c478bd9Sstevel@tonic-gate 	}
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	ASSERT(devimap->dno_head);
5807c478bd9Sstevel@tonic-gate 	ASSERT(devimap->dno_list_length > 0);
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	prev = NULL;
5837c478bd9Sstevel@tonic-gate 	for (elem = devimap->dno_head; elem; elem = elem->next) {
5847c478bd9Sstevel@tonic-gate 		if (elem->dip == dip) {
5857c478bd9Sstevel@tonic-gate 			ASSERT(elem->nodeid == ddi_get_nodeid(dip));
5867c478bd9Sstevel@tonic-gate 			break;
5877c478bd9Sstevel@tonic-gate 		}
5887c478bd9Sstevel@tonic-gate 		prev = elem;
5897c478bd9Sstevel@tonic-gate 	}
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	if (elem && prev)
5927c478bd9Sstevel@tonic-gate 		prev->next = elem->next;
5937c478bd9Sstevel@tonic-gate 	else if (elem)
5947c478bd9Sstevel@tonic-gate 		devimap->dno_head = elem->next;
5957c478bd9Sstevel@tonic-gate 	else
5967c478bd9Sstevel@tonic-gate 		panic("%s: devinfo node(%p) not found",
5977c478bd9Sstevel@tonic-gate 		    fcn, (void *)dip);
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	devimap->dno_list_length--;
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	elem->nodeid = 0;
6027c478bd9Sstevel@tonic-gate 	elem->dip = NULL;
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	elem->next = devimap->dno_free;
6057c478bd9Sstevel@tonic-gate 	devimap->dno_free = elem;
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	mutex_exit(&devimap->dno_lock);
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
6107c478bd9Sstevel@tonic-gate }
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate /*
6137c478bd9Sstevel@tonic-gate  * Link this node into the devinfo tree and add to orphan list
6147c478bd9Sstevel@tonic-gate  * Not callable from interrupt context
6157c478bd9Sstevel@tonic-gate  */
6167c478bd9Sstevel@tonic-gate static void
6177c478bd9Sstevel@tonic-gate link_node(dev_info_t *dip)
6187c478bd9Sstevel@tonic-gate {
6197c478bd9Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
6207c478bd9Sstevel@tonic-gate 	struct dev_info *parent = devi->devi_parent;
6217c478bd9Sstevel@tonic-gate 	dev_info_t **dipp;
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	ASSERT(parent);	/* never called for root node */
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "link_node: parent = %s child = %s\n",
6267c478bd9Sstevel@tonic-gate 	    parent->devi_node_name, devi->devi_node_name));
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	/*
6297c478bd9Sstevel@tonic-gate 	 * Hold the global_vhci_lock before linking any direct
6307c478bd9Sstevel@tonic-gate 	 * children of rootnex driver. This special lock protects
6317c478bd9Sstevel@tonic-gate 	 * linking and unlinking for rootnext direct children.
6327c478bd9Sstevel@tonic-gate 	 */
6337c478bd9Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
6347c478bd9Sstevel@tonic-gate 		mutex_enter(&global_vhci_lock);
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	/*
6377c478bd9Sstevel@tonic-gate 	 * attach the node to end of the list unless the node is already there
6387c478bd9Sstevel@tonic-gate 	 */
6397c478bd9Sstevel@tonic-gate 	dipp = (dev_info_t **)(&DEVI(parent)->devi_child);
6407c478bd9Sstevel@tonic-gate 	while (*dipp && (*dipp != dip)) {
6417c478bd9Sstevel@tonic-gate 		dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling);
6427c478bd9Sstevel@tonic-gate 	}
6437c478bd9Sstevel@tonic-gate 	ASSERT(*dipp == NULL);	/* node is not linked */
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 	/*
6467c478bd9Sstevel@tonic-gate 	 * Now that we are in the tree, update the devi-nodeid map.
6477c478bd9Sstevel@tonic-gate 	 */
6487c478bd9Sstevel@tonic-gate 	i_ddi_add_devimap(dip);
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	/*
6517c478bd9Sstevel@tonic-gate 	 * This is a temporary workaround for Bug 4618861.
6527c478bd9Sstevel@tonic-gate 	 * We keep the scsi_vhci nexus node on the left side of the devinfo
6537c478bd9Sstevel@tonic-gate 	 * tree (under the root nexus driver), so that virtual nodes under
6547c478bd9Sstevel@tonic-gate 	 * scsi_vhci will be SUSPENDed first and RESUMEd last.	This ensures
6557c478bd9Sstevel@tonic-gate 	 * that the pHCI nodes are active during times when their clients
6567c478bd9Sstevel@tonic-gate 	 * may be depending on them.  This workaround embodies the knowledge
6577c478bd9Sstevel@tonic-gate 	 * that system PM and CPR both traverse the tree left-to-right during
6587c478bd9Sstevel@tonic-gate 	 * SUSPEND and right-to-left during RESUME.
6599d3d2ed0Shiremath 	 * Extending the workaround to IB Nexus/VHCI
6609d3d2ed0Shiremath 	 * driver also.
6617c478bd9Sstevel@tonic-gate 	 */
662f4da9be0Scth 	if (strcmp(devi->devi_binding_name, "scsi_vhci") == 0) {
6637c478bd9Sstevel@tonic-gate 		/* Add scsi_vhci to beginning of list */
6647c478bd9Sstevel@tonic-gate 		ASSERT((dev_info_t *)parent == top_devinfo);
6657c478bd9Sstevel@tonic-gate 		/* scsi_vhci under rootnex */
6667c478bd9Sstevel@tonic-gate 		devi->devi_sibling = parent->devi_child;
6677c478bd9Sstevel@tonic-gate 		parent->devi_child = devi;
668f4da9be0Scth 	} else if (strcmp(devi->devi_binding_name, "ib") == 0) {
6699d3d2ed0Shiremath 		i_link_vhci_node(dip);
6707c478bd9Sstevel@tonic-gate 	} else {
6717c478bd9Sstevel@tonic-gate 		/* Add to end of list */
6727c478bd9Sstevel@tonic-gate 		*dipp = dip;
6737c478bd9Sstevel@tonic-gate 		DEVI(dip)->devi_sibling = NULL;
6747c478bd9Sstevel@tonic-gate 	}
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	/*
6777c478bd9Sstevel@tonic-gate 	 * Release the global_vhci_lock before linking any direct
6787c478bd9Sstevel@tonic-gate 	 * children of rootnex driver.
6797c478bd9Sstevel@tonic-gate 	 */
6807c478bd9Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
6817c478bd9Sstevel@tonic-gate 		mutex_exit(&global_vhci_lock);
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	/* persistent nodes go on orphan list */
6847c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip))
6857c478bd9Sstevel@tonic-gate 		add_to_dn_list(&orphanlist, dip);
6867c478bd9Sstevel@tonic-gate }
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate /*
6897c478bd9Sstevel@tonic-gate  * Unlink this node from the devinfo tree
6907c478bd9Sstevel@tonic-gate  */
6917c478bd9Sstevel@tonic-gate static int
6927c478bd9Sstevel@tonic-gate unlink_node(dev_info_t *dip)
6937c478bd9Sstevel@tonic-gate {
6947c478bd9Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
6957c478bd9Sstevel@tonic-gate 	struct dev_info *parent = devi->devi_parent;
6967c478bd9Sstevel@tonic-gate 	dev_info_t **dipp;
69726947304SEvan Yan 	ddi_hp_cn_handle_t *hdlp;
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	ASSERT(parent != NULL);
7007c478bd9Sstevel@tonic-gate 	ASSERT(devi->devi_node_state == DS_LINKED);
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "unlink_node: name = %s\n",
7037c478bd9Sstevel@tonic-gate 	    ddi_node_name(dip)));
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	/* check references */
7067c478bd9Sstevel@tonic-gate 	if (devi->devi_ref || i_ddi_remove_devimap(dip) != DDI_SUCCESS)
7077c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	/*
7107c478bd9Sstevel@tonic-gate 	 * Hold the global_vhci_lock before linking any direct
7117c478bd9Sstevel@tonic-gate 	 * children of rootnex driver.
7127c478bd9Sstevel@tonic-gate 	 */
7137c478bd9Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
7147c478bd9Sstevel@tonic-gate 		mutex_enter(&global_vhci_lock);
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	dipp = (dev_info_t **)(&DEVI(parent)->devi_child);
7177c478bd9Sstevel@tonic-gate 	while (*dipp && (*dipp != dip)) {
7187c478bd9Sstevel@tonic-gate 		dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling);
7197c478bd9Sstevel@tonic-gate 	}
7207c478bd9Sstevel@tonic-gate 	if (*dipp) {
7217c478bd9Sstevel@tonic-gate 		*dipp = (dev_info_t *)(devi->devi_sibling);
7227c478bd9Sstevel@tonic-gate 		devi->devi_sibling = NULL;
7237c478bd9Sstevel@tonic-gate 	} else {
7247c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE, "unlink_node: %s not linked",
7257c478bd9Sstevel@tonic-gate 		    devi->devi_node_name));
7267c478bd9Sstevel@tonic-gate 	}
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	/*
7297c478bd9Sstevel@tonic-gate 	 * Release the global_vhci_lock before linking any direct
7307c478bd9Sstevel@tonic-gate 	 * children of rootnex driver.
7317c478bd9Sstevel@tonic-gate 	 */
7327c478bd9Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
7337c478bd9Sstevel@tonic-gate 		mutex_exit(&global_vhci_lock);
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	/* Remove node from orphan list */
7367c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
7377c478bd9Sstevel@tonic-gate 		remove_from_dn_list(&orphanlist, dip);
7387c478bd9Sstevel@tonic-gate 	}
7397c478bd9Sstevel@tonic-gate 
74026947304SEvan Yan 	/* Update parent's hotplug handle list */
74126947304SEvan Yan 	for (hdlp = DEVI(parent)->devi_hp_hdlp; hdlp; hdlp = hdlp->next) {
74226947304SEvan Yan 		if (hdlp->cn_info.cn_child == dip)
74326947304SEvan Yan 			hdlp->cn_info.cn_child = NULL;
74426947304SEvan Yan 	}
7457c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
7467c478bd9Sstevel@tonic-gate }
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate /*
7497c478bd9Sstevel@tonic-gate  * Bind this devinfo node to a driver. If compat is NON-NULL, try that first.
7507c478bd9Sstevel@tonic-gate  * Else, use the node-name.
7517c478bd9Sstevel@tonic-gate  *
7527c478bd9Sstevel@tonic-gate  * NOTE: IEEE1275 specifies that nodename should be tried before compatible.
7537c478bd9Sstevel@tonic-gate  *	Solaris implementation binds nodename after compatible.
7547c478bd9Sstevel@tonic-gate  *
7557c478bd9Sstevel@tonic-gate  * If we find a binding,
756027021c7SChris Horne  * - set the binding name to the string,
7577c478bd9Sstevel@tonic-gate  * - set major number to driver major
7587c478bd9Sstevel@tonic-gate  *
7597c478bd9Sstevel@tonic-gate  * If we don't find a binding,
7607c478bd9Sstevel@tonic-gate  * - return failure
7617c478bd9Sstevel@tonic-gate  */
7627c478bd9Sstevel@tonic-gate static int
7637c478bd9Sstevel@tonic-gate bind_node(dev_info_t *dip)
7647c478bd9Sstevel@tonic-gate {
7657c478bd9Sstevel@tonic-gate 	char *p = NULL;
766a204de77Scth 	major_t major = DDI_MAJOR_T_NONE;
7677c478bd9Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
7687c478bd9Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	ASSERT(devi->devi_node_state == DS_LINKED);
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "bind_node: 0x%p(name = %s)\n",
7737c478bd9Sstevel@tonic-gate 	    (void *)dip, ddi_node_name(dip)));
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
7767c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_flags & DEVI_NO_BIND) {
7777c478bd9Sstevel@tonic-gate 		mutex_exit(&DEVI(dip)->devi_lock);
7787c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7797c478bd9Sstevel@tonic-gate 	}
7807c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	/* find the driver with most specific binding using compatible */
7837c478bd9Sstevel@tonic-gate 	major = ddi_compatible_driver_major(dip, &p);
784a204de77Scth 	if (major == DDI_MAJOR_T_NONE)
7857c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	devi->devi_major = major;
7887c478bd9Sstevel@tonic-gate 	if (p != NULL) {
7897c478bd9Sstevel@tonic-gate 		i_ddi_set_binding_name(dip, p);
7907c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "bind_node: %s bound to %s\n",
7917c478bd9Sstevel@tonic-gate 		    devi->devi_node_name, p));
7927c478bd9Sstevel@tonic-gate 	}
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	/* Link node to per-driver list */
7957c478bd9Sstevel@tonic-gate 	link_to_driver_list(dip);
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	/*
7987c478bd9Sstevel@tonic-gate 	 * reset parent flag so that nexus will merge .conf props
7997c478bd9Sstevel@tonic-gate 	 */
8007c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
8017c478bd9Sstevel@tonic-gate 		mutex_enter(&DEVI(parent)->devi_lock);
8027c478bd9Sstevel@tonic-gate 		DEVI(parent)->devi_flags &=
8037c478bd9Sstevel@tonic-gate 		    ~(DEVI_ATTACHED_CHILDREN|DEVI_MADE_CHILDREN);
8047c478bd9Sstevel@tonic-gate 		mutex_exit(&DEVI(parent)->devi_lock);
8057c478bd9Sstevel@tonic-gate 	}
8067c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
8077c478bd9Sstevel@tonic-gate }
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate /*
8107c478bd9Sstevel@tonic-gate  * Unbind this devinfo node
8117c478bd9Sstevel@tonic-gate  * Called before the node is destroyed or driver is removed from system
8127c478bd9Sstevel@tonic-gate  */
8137c478bd9Sstevel@tonic-gate static int
8147c478bd9Sstevel@tonic-gate unbind_node(dev_info_t *dip)
8157c478bd9Sstevel@tonic-gate {
8167c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_node_state == DS_BOUND);
817a204de77Scth 	ASSERT(DEVI(dip)->devi_major != DDI_MAJOR_T_NONE);
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	/* check references */
8207c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_ref)
8217c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "unbind_node: 0x%p(name = %s)\n",
8247c478bd9Sstevel@tonic-gate 	    (void *)dip, ddi_node_name(dip)));
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 	unlink_from_driver_list(dip);
827f4da9be0Scth 
828a204de77Scth 	DEVI(dip)->devi_major = DDI_MAJOR_T_NONE;
829f4da9be0Scth 	DEVI(dip)->devi_binding_name = DEVI(dip)->devi_node_name;
8307c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
8317c478bd9Sstevel@tonic-gate }
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate /*
8347c478bd9Sstevel@tonic-gate  * Initialize a node: calls the parent nexus' bus_ctl ops to do the operation.
8357c478bd9Sstevel@tonic-gate  * Must hold parent and per-driver list while calling this function.
8367c478bd9Sstevel@tonic-gate  * A successful init_node() returns with an active ndi_hold_devi() hold on
8377c478bd9Sstevel@tonic-gate  * the parent.
8387c478bd9Sstevel@tonic-gate  */
8397c478bd9Sstevel@tonic-gate static int
8407c478bd9Sstevel@tonic-gate init_node(dev_info_t *dip)
8417c478bd9Sstevel@tonic-gate {
8427c478bd9Sstevel@tonic-gate 	int error;
8437c478bd9Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
8447c478bd9Sstevel@tonic-gate 	int (*f)(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *, void *);
8457c478bd9Sstevel@tonic-gate 	char *path;
846f4da9be0Scth 	major_t	major;
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_BOUND);
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	/* should be DS_READY except for pcmcia ... */
8517c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(pdip) >= DS_PROBED);
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8547c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(dip, path);
8557c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "init_node: entry: path %s 0x%p\n",
8567c478bd9Sstevel@tonic-gate 	    path, (void *)dip));
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	/*
8597c478bd9Sstevel@tonic-gate 	 * The parent must have a bus_ctl operation.
8607c478bd9Sstevel@tonic-gate 	 */
8617c478bd9Sstevel@tonic-gate 	if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
8627c478bd9Sstevel@tonic-gate 	    (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_ctl) == NULL) {
8637c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
8647c478bd9Sstevel@tonic-gate 		goto out;
8657c478bd9Sstevel@tonic-gate 	}
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	add_global_props(dip);
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 	/*
8707c478bd9Sstevel@tonic-gate 	 * Invoke the parent's bus_ctl operation with the DDI_CTLOPS_INITCHILD
8717c478bd9Sstevel@tonic-gate 	 * command to transform the child to canonical form 1. If there
8727c478bd9Sstevel@tonic-gate 	 * is an error, ddi_remove_child should be called, to clean up.
8737c478bd9Sstevel@tonic-gate 	 */
8747c478bd9Sstevel@tonic-gate 	error = (*f)(pdip, pdip, DDI_CTLOPS_INITCHILD, dip, NULL);
8757c478bd9Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
8767c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "init_node: %s 0x%p failed\n",
8777c478bd9Sstevel@tonic-gate 		    path, (void *)dip));
8787c478bd9Sstevel@tonic-gate 		remove_global_props(dip);
8797c478bd9Sstevel@tonic-gate 		/* in case nexus driver didn't clear this field */
8807c478bd9Sstevel@tonic-gate 		ddi_set_name_addr(dip, NULL);
8817c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
8827c478bd9Sstevel@tonic-gate 		goto out;
8837c478bd9Sstevel@tonic-gate 	}
8847c478bd9Sstevel@tonic-gate 
885d6ae180bScth 	ndi_hold_devi(pdip);			/* initial hold of parent */
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	/* recompute path after initchild for @addr information */
8887c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(dip, path);
8897c478bd9Sstevel@tonic-gate 
890f4da9be0Scth 	/* Check for duplicate nodes */
891f4da9be0Scth 	if (find_duplicate_child(pdip, dip) != NULL) {
8927c478bd9Sstevel@tonic-gate 		/*
8937c478bd9Sstevel@tonic-gate 		 * uninit_node() the duplicate - a successful uninit_node()
894d6ae180bScth 		 * will release inital hold of parent using ndi_rele_devi().
8957c478bd9Sstevel@tonic-gate 		 */
8967c478bd9Sstevel@tonic-gate 		if ((error = uninit_node(dip)) != DDI_SUCCESS) {
897d6ae180bScth 			ndi_rele_devi(pdip);	/* release initial hold */
8987c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "init_node: uninit of duplicate "
8997c478bd9Sstevel@tonic-gate 			    "node %s failed", path);
9007c478bd9Sstevel@tonic-gate 		}
9017c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "init_node: duplicate uninit "
9027c478bd9Sstevel@tonic-gate 		    "%s 0x%p%s\n", path, (void *)dip,
9037c478bd9Sstevel@tonic-gate 		    (error == DDI_SUCCESS) ? "" : " failed"));
9047c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
9057c478bd9Sstevel@tonic-gate 		goto out;
9067c478bd9Sstevel@tonic-gate 	}
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	/*
909f4da9be0Scth 	 * Check to see if we have a path-oriented driver alias that overrides
910f4da9be0Scth 	 * the current driver binding. If so, we need to rebind. This check
911f4da9be0Scth 	 * needs to be delayed until after a successful DDI_CTLOPS_INITCHILD,
912f4da9be0Scth 	 * so the unit-address is established on the last component of the path.
913f4da9be0Scth 	 *
914f4da9be0Scth 	 * NOTE: Allowing a path-oriented alias to change the driver binding
915f4da9be0Scth 	 * of a driver.conf node results in non-intuitive property behavior.
916f4da9be0Scth 	 * We provide a tunable (driver_conf_allow_path_alias) to control
917f4da9be0Scth 	 * this behavior. See uninit_node() for more details.
918f4da9be0Scth 	 *
919f4da9be0Scth 	 * NOTE: If you are adding a path-oriented alias for the boot device,
920f4da9be0Scth 	 * and there is mismatch between OBP and the kernel in regard to
921f4da9be0Scth 	 * generic name use, like "disk" .vs. "ssd", then you will need
922f4da9be0Scth 	 * to add a path-oriented alias for both paths.
923f4da9be0Scth 	 */
924f4da9be0Scth 	major = ddi_name_to_major(path);
925c9cc1492SJerry Gilliam 	if (driver_installed(major) && (major != DEVI(dip)->devi_major) &&
926f4da9be0Scth 	    (ndi_dev_is_persistent_node(dip) || driver_conf_allow_path_alias)) {
927f4da9be0Scth 
928f4da9be0Scth 		/* Mark node for rebind processing. */
929f4da9be0Scth 		mutex_enter(&DEVI(dip)->devi_lock);
930f4da9be0Scth 		DEVI(dip)->devi_flags |= DEVI_REBIND;
931f4da9be0Scth 		mutex_exit(&DEVI(dip)->devi_lock);
932f4da9be0Scth 
933f4da9be0Scth 		/*
934d6ae180bScth 		 * Add an extra hold on the parent to prevent it from ever
935d6ae180bScth 		 * having a zero devi_ref during the child rebind process.
936d6ae180bScth 		 * This is necessary to ensure that the parent will never
937d6ae180bScth 		 * detach(9E) during the rebind.
938d6ae180bScth 		 */
939d6ae180bScth 		ndi_hold_devi(pdip);		/* extra hold of parent */
940d6ae180bScth 
941d6ae180bScth 		/*
942f4da9be0Scth 		 * uninit_node() current binding - a successful uninit_node()
943d6ae180bScth 		 * will release extra hold of parent using ndi_rele_devi().
944f4da9be0Scth 		 */
945f4da9be0Scth 		if ((error = uninit_node(dip)) != DDI_SUCCESS) {
946d6ae180bScth 			ndi_rele_devi(pdip);	/* release extra hold */
947d6ae180bScth 			ndi_rele_devi(pdip);	/* release initial hold */
948f4da9be0Scth 			cmn_err(CE_WARN, "init_node: uninit for rebind "
949f4da9be0Scth 			    "of node %s failed", path);
950f4da9be0Scth 			goto out;
951f4da9be0Scth 		}
952f4da9be0Scth 
953f4da9be0Scth 		/* Unbind: demote the node back to DS_LINKED.  */
954f4da9be0Scth 		if ((error = ndi_devi_unbind_driver(dip)) != DDI_SUCCESS) {
955027021c7SChris Horne 			ndi_rele_devi(pdip);	/* release initial hold */
956f4da9be0Scth 			cmn_err(CE_WARN, "init_node: unbind for rebind "
957f4da9be0Scth 			    "of node %s failed", path);
958f4da9be0Scth 			goto out;
959f4da9be0Scth 		}
960f4da9be0Scth 
961f4da9be0Scth 		/* establish rebinding name */
962f4da9be0Scth 		if (DEVI(dip)->devi_rebinding_name == NULL)
963f4da9be0Scth 			DEVI(dip)->devi_rebinding_name =
964f4da9be0Scth 			    i_ddi_strdup(path, KM_SLEEP);
965f4da9be0Scth 
966f4da9be0Scth 		/*
967f4da9be0Scth 		 * Now that we are demoted and marked for rebind, repromote.
968f4da9be0Scth 		 * We need to do this in steps, instead of just calling
969f4da9be0Scth 		 * ddi_initchild, so that we can redo the merge operation
970f4da9be0Scth 		 * after we are rebound to the path-bound driver.
971f4da9be0Scth 		 *
972f4da9be0Scth 		 * Start by rebinding node to the path-bound driver.
973f4da9be0Scth 		 */
974f4da9be0Scth 		if ((error = ndi_devi_bind_driver(dip, 0)) != DDI_SUCCESS) {
975027021c7SChris Horne 			ndi_rele_devi(pdip);	/* release initial hold */
976f4da9be0Scth 			cmn_err(CE_WARN, "init_node: rebind "
977f4da9be0Scth 			    "of node %s failed", path);
978f4da9be0Scth 			goto out;
979f4da9be0Scth 		}
980f4da9be0Scth 
981f4da9be0Scth 		/*
982f4da9be0Scth 		 * If the node is not a driver.conf node then merge
983f4da9be0Scth 		 * driver.conf properties from new path-bound driver.conf.
984f4da9be0Scth 		 */
985f4da9be0Scth 		if (ndi_dev_is_persistent_node(dip))
986f4da9be0Scth 			(void) i_ndi_make_spec_children(pdip, 0);
987f4da9be0Scth 
988f4da9be0Scth 		/*
989f4da9be0Scth 		 * Now that we have taken care of merge, repromote back
990f4da9be0Scth 		 * to DS_INITIALIZED.
991f4da9be0Scth 		 */
992f4da9be0Scth 		error = ddi_initchild(pdip, dip);
993f4da9be0Scth 		NDI_CONFIG_DEBUG((CE_CONT, "init_node: rebind "
994f4da9be0Scth 		    "%s 0x%p\n", path, (void *)dip));
995d6ae180bScth 
996d6ae180bScth 		/*
997d6ae180bScth 		 * Release our initial hold. If ddi_initchild() was
998b9ccdc5aScth 		 * successful then it will return with the active hold.
999d6ae180bScth 		 */
1000d6ae180bScth 		ndi_rele_devi(pdip);
1001f4da9be0Scth 		goto out;
1002f4da9be0Scth 	}
1003f4da9be0Scth 
1004f4da9be0Scth 	/*
10057c478bd9Sstevel@tonic-gate 	 * Apply multi-parent/deep-nexus optimization to the new node
10067c478bd9Sstevel@tonic-gate 	 */
10077c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
10087c478bd9Sstevel@tonic-gate 	ddi_optimize_dtree(dip);
1009d6ae180bScth 	error = DDI_SUCCESS;		/* return with active hold */
10107c478bd9Sstevel@tonic-gate 
1011f4da9be0Scth out:	if (error != DDI_SUCCESS) {
1012f4da9be0Scth 		/* On failure ensure that DEVI_REBIND is cleared */
1013f4da9be0Scth 		mutex_enter(&DEVI(dip)->devi_lock);
1014f4da9be0Scth 		DEVI(dip)->devi_flags &= ~DEVI_REBIND;
1015f4da9be0Scth 		mutex_exit(&DEVI(dip)->devi_lock);
1016f4da9be0Scth 	}
1017f4da9be0Scth 	kmem_free(path, MAXPATHLEN);
10187c478bd9Sstevel@tonic-gate 	return (error);
10197c478bd9Sstevel@tonic-gate }
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate /*
10227c478bd9Sstevel@tonic-gate  * Uninitialize node
10237c478bd9Sstevel@tonic-gate  * The per-driver list must be held busy during the call.
10247c478bd9Sstevel@tonic-gate  * A successful uninit_node() releases the init_node() hold on
10257c478bd9Sstevel@tonic-gate  * the parent by calling ndi_rele_devi().
10267c478bd9Sstevel@tonic-gate  */
10277c478bd9Sstevel@tonic-gate static int
10287c478bd9Sstevel@tonic-gate uninit_node(dev_info_t *dip)
10297c478bd9Sstevel@tonic-gate {
10307c478bd9Sstevel@tonic-gate 	int node_state_entry;
10317c478bd9Sstevel@tonic-gate 	dev_info_t *pdip;
10327c478bd9Sstevel@tonic-gate 	struct dev_ops *ops;
10337c478bd9Sstevel@tonic-gate 	int (*f)();
10347c478bd9Sstevel@tonic-gate 	int error;
10357c478bd9Sstevel@tonic-gate 	char *addr;
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 	/*
10387c478bd9Sstevel@tonic-gate 	 * Don't check for references here or else a ref-counted
10397c478bd9Sstevel@tonic-gate 	 * dip cannot be downgraded by the framework.
10407c478bd9Sstevel@tonic-gate 	 */
10417c478bd9Sstevel@tonic-gate 	node_state_entry = i_ddi_node_state(dip);
10427c478bd9Sstevel@tonic-gate 	ASSERT((node_state_entry == DS_BOUND) ||
10437c478bd9Sstevel@tonic-gate 	    (node_state_entry == DS_INITIALIZED));
10447c478bd9Sstevel@tonic-gate 	pdip = ddi_get_parent(dip);
10457c478bd9Sstevel@tonic-gate 	ASSERT(pdip);
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "uninit_node: 0x%p(%s%d)\n",
10487c478bd9Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 	if (((ops = ddi_get_driver(pdip)) == NULL) ||
10517c478bd9Sstevel@tonic-gate 	    (ops->devo_bus_ops == NULL) ||
10527c478bd9Sstevel@tonic-gate 	    ((f = ops->devo_bus_ops->bus_ctl) == NULL)) {
10537c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
10547c478bd9Sstevel@tonic-gate 	}
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 	/*
10577c478bd9Sstevel@tonic-gate 	 * save the @addr prior to DDI_CTLOPS_UNINITCHILD for use in
10587c478bd9Sstevel@tonic-gate 	 * freeing the instance if it succeeds.
10597c478bd9Sstevel@tonic-gate 	 */
10607c478bd9Sstevel@tonic-gate 	if (node_state_entry == DS_INITIALIZED) {
10617c478bd9Sstevel@tonic-gate 		addr = ddi_get_name_addr(dip);
10627c478bd9Sstevel@tonic-gate 		if (addr)
10637c478bd9Sstevel@tonic-gate 			addr = i_ddi_strdup(addr, KM_SLEEP);
10647c478bd9Sstevel@tonic-gate 	} else {
10657c478bd9Sstevel@tonic-gate 		addr = NULL;
10667c478bd9Sstevel@tonic-gate 	}
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	error = (*f)(pdip, pdip, DDI_CTLOPS_UNINITCHILD, dip, (void *)NULL);
10697c478bd9Sstevel@tonic-gate 	if (error == DDI_SUCCESS) {
1070470452aaSPrasad Singamsetty 		/* ensure that devids are unregistered */
1071470452aaSPrasad Singamsetty 		if (DEVI(dip)->devi_flags & DEVI_REGISTERED_DEVID) {
1072470452aaSPrasad Singamsetty 			DEVI(dip)->devi_flags &= ~DEVI_REGISTERED_DEVID;
1073470452aaSPrasad Singamsetty 			ddi_devid_unregister(dip);
1074470452aaSPrasad Singamsetty 		}
1075470452aaSPrasad Singamsetty 
10767c478bd9Sstevel@tonic-gate 		/* if uninitchild forgot to set devi_addr to NULL do it now */
10777c478bd9Sstevel@tonic-gate 		ddi_set_name_addr(dip, NULL);
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 		/*
10807c478bd9Sstevel@tonic-gate 		 * Free instance number. This is a no-op if instance has
10817c478bd9Sstevel@tonic-gate 		 * been kept by probe_node().  Avoid free when we are called
10827c478bd9Sstevel@tonic-gate 		 * from init_node (DS_BOUND) because the instance has not yet
10837c478bd9Sstevel@tonic-gate 		 * been assigned.
10847c478bd9Sstevel@tonic-gate 		 */
10857c478bd9Sstevel@tonic-gate 		if (node_state_entry == DS_INITIALIZED) {
10867c478bd9Sstevel@tonic-gate 			e_ddi_free_instance(dip, addr);
10877c478bd9Sstevel@tonic-gate 			DEVI(dip)->devi_instance = -1;
10887c478bd9Sstevel@tonic-gate 		}
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 		/* release the init_node hold */
10917c478bd9Sstevel@tonic-gate 		ndi_rele_devi(pdip);
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 		remove_global_props(dip);
1094f4da9be0Scth 
1095f4da9be0Scth 		/*
1096f4da9be0Scth 		 * NOTE: The decision on whether to allow a path-oriented
1097f4da9be0Scth 		 * rebind of a driver.conf enumerated node is made by
1098f4da9be0Scth 		 * init_node() based on driver_conf_allow_path_alias. The
1099f4da9be0Scth 		 * rebind code below prevents deletion of system properties
1100f4da9be0Scth 		 * on driver.conf nodes.
1101f4da9be0Scth 		 *
1102f4da9be0Scth 		 * When driver_conf_allow_path_alias is set, property behavior
1103f4da9be0Scth 		 * on rebound driver.conf file is non-intuitive. For a
1104f4da9be0Scth 		 * driver.conf node, the unit-address properties come from
1105f4da9be0Scth 		 * the driver.conf file as system properties. Removing system
1106f4da9be0Scth 		 * properties from a driver.conf node makes the node
1107f4da9be0Scth 		 * useless (we get node without unit-address properties) - so
1108f4da9be0Scth 		 * we leave system properties in place. The result is a node
1109f4da9be0Scth 		 * where system properties come from the node being rebound,
1110f4da9be0Scth 		 * and global properties come from the driver.conf file
1111f4da9be0Scth 		 * of the driver we are rebinding to.  If we could determine
1112f4da9be0Scth 		 * that the path-oriented alias driver.conf file defined a
1113f4da9be0Scth 		 * node at the same unit address, it would be best to use
1114f4da9be0Scth 		 * that node and avoid the non-intuitive property behavior.
1115f4da9be0Scth 		 * Unfortunately, the current "merge" code does not support
1116f4da9be0Scth 		 * this, so we live with the non-intuitive property behavior.
1117f4da9be0Scth 		 */
1118f4da9be0Scth 		if (!((ndi_dev_is_persistent_node(dip) == 0) &&
1119f4da9be0Scth 		    (DEVI(dip)->devi_flags & DEVI_REBIND)))
11207c478bd9Sstevel@tonic-gate 			e_ddi_prop_remove_all(dip);
11217c478bd9Sstevel@tonic-gate 	} else {
11227c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "uninit_node failed: 0x%p(%s%d)\n",
11237c478bd9Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
11247c478bd9Sstevel@tonic-gate 	}
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	if (addr)
11277c478bd9Sstevel@tonic-gate 		kmem_free(addr, strlen(addr) + 1);
11287c478bd9Sstevel@tonic-gate 	return (error);
11297c478bd9Sstevel@tonic-gate }
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate /*
11327c478bd9Sstevel@tonic-gate  * Invoke driver's probe entry point to probe for existence of hardware.
11337c478bd9Sstevel@tonic-gate  * Keep instance permanent for successful probe and leaf nodes.
11347c478bd9Sstevel@tonic-gate  *
11357c478bd9Sstevel@tonic-gate  * Per-driver list must be held busy while calling this function.
11367c478bd9Sstevel@tonic-gate  */
11377c478bd9Sstevel@tonic-gate static int
11387c478bd9Sstevel@tonic-gate probe_node(dev_info_t *dip)
11397c478bd9Sstevel@tonic-gate {
11407c478bd9Sstevel@tonic-gate 	int rv;
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_INITIALIZED);
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "probe_node: 0x%p(%s%d)\n",
11457c478bd9Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 	/* temporarily hold the driver while we probe */
11487c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
11497c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_ops == NULL) {
11507c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
11517c478bd9Sstevel@tonic-gate 		    "probe_node: 0x%p(%s%d) cannot load driver\n",
11527c478bd9Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
11537c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
11547c478bd9Sstevel@tonic-gate 	}
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 	if (identify_9e != 0)
11577c478bd9Sstevel@tonic-gate 		(void) devi_identify(dip);
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate 	rv = devi_probe(dip);
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 	/* release the driver now that probe is complete */
11627c478bd9Sstevel@tonic-gate 	ndi_rele_driver(dip);
11637c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_ops = NULL;
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate 	switch (rv) {
11667c478bd9Sstevel@tonic-gate 	case DDI_PROBE_SUCCESS:			/* found */
11677c478bd9Sstevel@tonic-gate 	case DDI_PROBE_DONTCARE:		/* ddi_dev_is_sid */
11687c478bd9Sstevel@tonic-gate 		e_ddi_keep_instance(dip);	/* persist instance */
11697c478bd9Sstevel@tonic-gate 		rv = DDI_SUCCESS;
11707c478bd9Sstevel@tonic-gate 		break;
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 	case DDI_PROBE_PARTIAL:			/* maybe later */
11737c478bd9Sstevel@tonic-gate 	case DDI_PROBE_FAILURE:			/* not found */
11747c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
11757c478bd9Sstevel@tonic-gate 		    "probe_node: 0x%p(%s%d) no hardware found%s\n",
11767c478bd9Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip),
11777c478bd9Sstevel@tonic-gate 		    (rv == DDI_PROBE_PARTIAL) ? " yet" : ""));
11787c478bd9Sstevel@tonic-gate 		rv = DDI_FAILURE;
11797c478bd9Sstevel@tonic-gate 		break;
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 	default:
11827c478bd9Sstevel@tonic-gate #ifdef	DEBUG
11837c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "probe_node: %s%d: illegal probe(9E) value",
11847c478bd9Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
11857c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
11867c478bd9Sstevel@tonic-gate 		rv = DDI_FAILURE;
11877c478bd9Sstevel@tonic-gate 		break;
11887c478bd9Sstevel@tonic-gate 	}
11897c478bd9Sstevel@tonic-gate 	return (rv);
11907c478bd9Sstevel@tonic-gate }
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate /*
11937c478bd9Sstevel@tonic-gate  * Unprobe a node. Simply reset the node state.
11947c478bd9Sstevel@tonic-gate  * Per-driver list must be held busy while calling this function.
11957c478bd9Sstevel@tonic-gate  */
11967c478bd9Sstevel@tonic-gate static int
11977c478bd9Sstevel@tonic-gate unprobe_node(dev_info_t *dip)
11987c478bd9Sstevel@tonic-gate {
11997c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_PROBED);
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate 	/*
12027c478bd9Sstevel@tonic-gate 	 * Don't check for references here or else a ref-counted
12037c478bd9Sstevel@tonic-gate 	 * dip cannot be downgraded by the framework.
12047c478bd9Sstevel@tonic-gate 	 */
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "unprobe_node: 0x%p(name = %s)\n",
12077c478bd9Sstevel@tonic-gate 	    (void *)dip, ddi_node_name(dip)));
12087c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
12097c478bd9Sstevel@tonic-gate }
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate /*
12127c478bd9Sstevel@tonic-gate  * Attach devinfo node.
12137c478bd9Sstevel@tonic-gate  * Per-driver list must be held busy.
12147c478bd9Sstevel@tonic-gate  */
12157c478bd9Sstevel@tonic-gate static int
12167c478bd9Sstevel@tonic-gate attach_node(dev_info_t *dip)
12177c478bd9Sstevel@tonic-gate {
12187c478bd9Sstevel@tonic-gate 	int rv;
12197c478bd9Sstevel@tonic-gate 
12205e3986cbScth 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
12217c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_PROBED);
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d)\n",
12247c478bd9Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate 	/*
12277c478bd9Sstevel@tonic-gate 	 * Tell mpxio framework that a node is about to online.
12287c478bd9Sstevel@tonic-gate 	 */
12297c478bd9Sstevel@tonic-gate 	if ((rv = mdi_devi_online(dip, 0)) != NDI_SUCCESS) {
12307c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
12317c478bd9Sstevel@tonic-gate 	}
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 	/* no recursive attachment */
12347c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_ops == NULL);
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	/*
12377c478bd9Sstevel@tonic-gate 	 * Hold driver the node is bound to.
12387c478bd9Sstevel@tonic-gate 	 */
12397c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
12407c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_ops == NULL) {
12417c478bd9Sstevel@tonic-gate 		/*
12427c478bd9Sstevel@tonic-gate 		 * We were able to load driver for probing, so we should
12437c478bd9Sstevel@tonic-gate 		 * not get here unless something really bad happened.
12447c478bd9Sstevel@tonic-gate 		 */
12457c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "attach_node: no driver for major %d",
12467c478bd9Sstevel@tonic-gate 		    DEVI(dip)->devi_major);
12477c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
12487c478bd9Sstevel@tonic-gate 	}
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 	if (NEXUS_DRV(DEVI(dip)->devi_ops))
12517c478bd9Sstevel@tonic-gate 		DEVI(dip)->devi_taskq = ddi_taskq_create(dip,
12527c478bd9Sstevel@tonic-gate 		    "nexus_enum_tq", 1,
12537c478bd9Sstevel@tonic-gate 		    TASKQ_DEFAULTPRI, 0);
12547c478bd9Sstevel@tonic-gate 
125516747f41Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
1256c73a93f2Sdm120769 	DEVI_SET_ATTACHING(dip);
12577c478bd9Sstevel@tonic-gate 	DEVI_SET_NEED_RESET(dip);
125816747f41Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
125916747f41Scth 
12607c478bd9Sstevel@tonic-gate 	rv = devi_attach(dip, DDI_ATTACH);
126116747f41Scth 
1262144dfaa9Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
1263c73a93f2Sdm120769 	DEVI_CLR_ATTACHING(dip);
1264144dfaa9Scth 
1265c73a93f2Sdm120769 	if (rv != DDI_SUCCESS) {
12665e3986cbScth 		DEVI_CLR_NEED_RESET(dip);
1267225b11cdScth 		mutex_exit(&DEVI(dip)->devi_lock);
1268225b11cdScth 
12697c478bd9Sstevel@tonic-gate 		/*
12707c478bd9Sstevel@tonic-gate 		 * Cleanup dacf reservations
12717c478bd9Sstevel@tonic-gate 		 */
12727c478bd9Sstevel@tonic-gate 		mutex_enter(&dacf_lock);
12737c478bd9Sstevel@tonic-gate 		dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH);
12747c478bd9Sstevel@tonic-gate 		dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH);
12757c478bd9Sstevel@tonic-gate 		mutex_exit(&dacf_lock);
12767c478bd9Sstevel@tonic-gate 		if (DEVI(dip)->devi_taskq)
12777c478bd9Sstevel@tonic-gate 			ddi_taskq_destroy(DEVI(dip)->devi_taskq);
12787c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 		/* release the driver if attach failed */
12817c478bd9Sstevel@tonic-gate 		ndi_rele_driver(dip);
12827c478bd9Sstevel@tonic-gate 		DEVI(dip)->devi_ops = NULL;
12837c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d) failed\n",
12847c478bd9Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
12857c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
1286c73a93f2Sdm120769 	} else
1287c73a93f2Sdm120769 		mutex_exit(&DEVI(dip)->devi_lock);
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 	/* successful attach, return with driver held */
129016747f41Scth 
12917c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
12927c478bd9Sstevel@tonic-gate }
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate /*
12957c478bd9Sstevel@tonic-gate  * Detach devinfo node.
12967c478bd9Sstevel@tonic-gate  * Per-driver list must be held busy.
12977c478bd9Sstevel@tonic-gate  */
12987c478bd9Sstevel@tonic-gate static int
12997c478bd9Sstevel@tonic-gate detach_node(dev_info_t *dip, uint_t flag)
13007c478bd9Sstevel@tonic-gate {
1301cfbaf6c3Scth 	struct devnames	*dnp;
13027c478bd9Sstevel@tonic-gate 	int		rv;
1303cfbaf6c3Scth 
13045e3986cbScth 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
13057c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_ATTACHED);
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate 	/* check references */
13087c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_ref)
13097c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "detach_node: 0x%p(%s%d)\n",
13127c478bd9Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
13137c478bd9Sstevel@tonic-gate 
13145e3986cbScth 	/*
13155e3986cbScth 	 * NOTE: If we are processing a pHCI node then the calling code
13165e3986cbScth 	 * must detect this and ndi_devi_enter() in (vHCI, parent(pHCI))
13175e3986cbScth 	 * order unless pHCI and vHCI are siblings.  Code paths leading
13185e3986cbScth 	 * here that must ensure this ordering include:
13195e3986cbScth 	 * unconfig_immediate_children(), devi_unconfig_one(),
13205e3986cbScth 	 * ndi_devi_unconfig_one(), ndi_devi_offline().
13215e3986cbScth 	 */
13225e3986cbScth 	ASSERT(!MDI_PHCI(dip) ||
13235e3986cbScth 	    (ddi_get_parent(mdi_devi_get_vdip(dip)) == ddi_get_parent(dip)) ||
13245e3986cbScth 	    DEVI_BUSY_OWNED(mdi_devi_get_vdip(dip)));
13255e3986cbScth 
13267c478bd9Sstevel@tonic-gate 	/* Offline the device node with the mpxio framework. */
13277c478bd9Sstevel@tonic-gate 	if (mdi_devi_offline(dip, flag) != NDI_SUCCESS) {
13287c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
13297c478bd9Sstevel@tonic-gate 	}
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 	/* drain the taskq */
13327c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_taskq)
13337c478bd9Sstevel@tonic-gate 		ddi_taskq_wait(DEVI(dip)->devi_taskq);
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate 	rv = devi_detach(dip, DDI_DETACH);
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 	if (rv != DDI_SUCCESS) {
13387c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
13397c478bd9Sstevel@tonic-gate 		    "detach_node: 0x%p(%s%d) failed\n",
13407c478bd9Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
13417c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
13427c478bd9Sstevel@tonic-gate 	}
13437c478bd9Sstevel@tonic-gate 
13445e3986cbScth 	mutex_enter(&(DEVI(dip)->devi_lock));
13455e3986cbScth 	DEVI_CLR_NEED_RESET(dip);
13465e3986cbScth 	mutex_exit(&(DEVI(dip)->devi_lock));
13475e3986cbScth 
13483a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
134909011d40SVikram Hegde 	/*
135009011d40SVikram Hegde 	 * Close any iommulib mediated linkage to an IOMMU
135109011d40SVikram Hegde 	 */
135209011d40SVikram Hegde 	iommulib_nex_close(dip);
135309011d40SVikram Hegde #endif
135409011d40SVikram Hegde 
13557c478bd9Sstevel@tonic-gate 	/* destroy the taskq */
13567c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_taskq) {
13577c478bd9Sstevel@tonic-gate 		ddi_taskq_destroy(DEVI(dip)->devi_taskq);
13587c478bd9Sstevel@tonic-gate 		DEVI(dip)->devi_taskq = NULL;
13597c478bd9Sstevel@tonic-gate 	}
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate 	/* Cleanup dacf reservations */
13627c478bd9Sstevel@tonic-gate 	mutex_enter(&dacf_lock);
13637c478bd9Sstevel@tonic-gate 	dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH);
13647c478bd9Sstevel@tonic-gate 	dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH);
13657c478bd9Sstevel@tonic-gate 	mutex_exit(&dacf_lock);
13667c478bd9Sstevel@tonic-gate 
1367255a2d50SMatthew Jacob 	/* remove any additional flavors that were added */
1368255a2d50SMatthew Jacob 	if (DEVI(dip)->devi_flavorv_n > 1 && DEVI(dip)->devi_flavorv != NULL) {
1369255a2d50SMatthew Jacob 		kmem_free(DEVI(dip)->devi_flavorv,
1370255a2d50SMatthew Jacob 		    (DEVI(dip)->devi_flavorv_n - 1) * sizeof (void *));
1371255a2d50SMatthew Jacob 		DEVI(dip)->devi_flavorv = NULL;
1372255a2d50SMatthew Jacob 	}
1373255a2d50SMatthew Jacob 
13747c478bd9Sstevel@tonic-gate 	/* Remove properties and minor nodes in case driver forgots */
13757c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
13767c478bd9Sstevel@tonic-gate 	ddi_prop_remove_all(dip);
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate 	/* a detached node can't have attached or .conf children */
13797c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
13807c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN|DEVI_ATTACHED_CHILDREN);
13817c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
13827c478bd9Sstevel@tonic-gate 
1383cfbaf6c3Scth 	/*
1384cfbaf6c3Scth 	 * If the instance has successfully detached in detach_driver() context,
1385cfbaf6c3Scth 	 * clear DN_DRIVER_HELD for correct ddi_hold_installed_driver()
1386cfbaf6c3Scth 	 * behavior. Consumers like qassociate() depend on this (via clnopen()).
1387cfbaf6c3Scth 	 */
1388cfbaf6c3Scth 	if (flag & NDI_DETACH_DRIVER) {
1389cfbaf6c3Scth 		dnp = &(devnamesp[DEVI(dip)->devi_major]);
1390cfbaf6c3Scth 		LOCK_DEV_OPS(&dnp->dn_lock);
1391cfbaf6c3Scth 		dnp->dn_flags &= ~DN_DRIVER_HELD;
1392cfbaf6c3Scth 		UNLOCK_DEV_OPS(&dnp->dn_lock);
1393cfbaf6c3Scth 	}
1394cfbaf6c3Scth 
13957c478bd9Sstevel@tonic-gate 	/* successful detach, release the driver */
13967c478bd9Sstevel@tonic-gate 	ndi_rele_driver(dip);
13977c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_ops = NULL;
13987c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
13997c478bd9Sstevel@tonic-gate }
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate /*
14027c478bd9Sstevel@tonic-gate  * Run dacf post_attach routines
14037c478bd9Sstevel@tonic-gate  */
14047c478bd9Sstevel@tonic-gate static int
14057c478bd9Sstevel@tonic-gate postattach_node(dev_info_t *dip)
14067c478bd9Sstevel@tonic-gate {
14077c478bd9Sstevel@tonic-gate 	int rval;
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 	/*
14107c478bd9Sstevel@tonic-gate 	 * For hotplug busses like USB, it's possible that devices
14117c478bd9Sstevel@tonic-gate 	 * are removed but dip is still around. We don't want to
14127c478bd9Sstevel@tonic-gate 	 * run dacf routines as part of detach failure recovery.
14137c478bd9Sstevel@tonic-gate 	 *
14147c478bd9Sstevel@tonic-gate 	 * Pretend success until we figure out how to prevent
14157c478bd9Sstevel@tonic-gate 	 * access to such devinfo nodes.
14167c478bd9Sstevel@tonic-gate 	 */
14177c478bd9Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_REMOVED(dip))
14187c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
14197c478bd9Sstevel@tonic-gate 
14207c478bd9Sstevel@tonic-gate 	/*
14217c478bd9Sstevel@tonic-gate 	 * if dacf_postattach failed, report it to the framework
14227c478bd9Sstevel@tonic-gate 	 * so that it can be retried later at the open time.
14237c478bd9Sstevel@tonic-gate 	 */
14247c478bd9Sstevel@tonic-gate 	mutex_enter(&dacf_lock);
14257c478bd9Sstevel@tonic-gate 	rval = dacfc_postattach(dip);
14267c478bd9Sstevel@tonic-gate 	mutex_exit(&dacf_lock);
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 	/*
14297c478bd9Sstevel@tonic-gate 	 * Plumbing during postattach may fail because of the
14307c478bd9Sstevel@tonic-gate 	 * underlying device is not ready. This will fail ndi_devi_config()
14317c478bd9Sstevel@tonic-gate 	 * in dv_filldir() and a warning message is issued. The message
14327c478bd9Sstevel@tonic-gate 	 * from here will explain what happened
14337c478bd9Sstevel@tonic-gate 	 */
14347c478bd9Sstevel@tonic-gate 	if (rval != DACF_SUCCESS) {
14357c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "Postattach failed for %s%d\n",
14367c478bd9Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
14377c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
14387c478bd9Sstevel@tonic-gate 	}
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
14417c478bd9Sstevel@tonic-gate }
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate /*
14447c478bd9Sstevel@tonic-gate  * Run dacf pre-detach routines
14457c478bd9Sstevel@tonic-gate  */
14467c478bd9Sstevel@tonic-gate static int
14477c478bd9Sstevel@tonic-gate predetach_node(dev_info_t *dip, uint_t flag)
14487c478bd9Sstevel@tonic-gate {
14497c478bd9Sstevel@tonic-gate 	int ret;
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 	/*
14527c478bd9Sstevel@tonic-gate 	 * Don't auto-detach if DDI_FORCEATTACH or DDI_NO_AUTODETACH
14537c478bd9Sstevel@tonic-gate 	 * properties are set.
14547c478bd9Sstevel@tonic-gate 	 */
14557c478bd9Sstevel@tonic-gate 	if (flag & NDI_AUTODETACH) {
14567c478bd9Sstevel@tonic-gate 		struct devnames *dnp;
14577c478bd9Sstevel@tonic-gate 		int pflag = DDI_PROP_NOTPROM | DDI_PROP_DONTPASS;
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate 		if ((ddi_prop_get_int(DDI_DEV_T_ANY, dip,
14607c478bd9Sstevel@tonic-gate 		    pflag, DDI_FORCEATTACH, 0) == 1) ||
14617c478bd9Sstevel@tonic-gate 		    (ddi_prop_get_int(DDI_DEV_T_ANY, dip,
14627c478bd9Sstevel@tonic-gate 		    pflag, DDI_NO_AUTODETACH, 0) == 1))
14637c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate 		/* check for driver global version of DDI_NO_AUTODETACH */
14667c478bd9Sstevel@tonic-gate 		dnp = &devnamesp[DEVI(dip)->devi_major];
14677c478bd9Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
14687c478bd9Sstevel@tonic-gate 		if (dnp->dn_flags & DN_NO_AUTODETACH) {
14697c478bd9Sstevel@tonic-gate 			UNLOCK_DEV_OPS(&dnp->dn_lock);
14707c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14717c478bd9Sstevel@tonic-gate 		}
14727c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
14737c478bd9Sstevel@tonic-gate 	}
14747c478bd9Sstevel@tonic-gate 
14757c478bd9Sstevel@tonic-gate 	mutex_enter(&dacf_lock);
14767c478bd9Sstevel@tonic-gate 	ret = dacfc_predetach(dip);
14777c478bd9Sstevel@tonic-gate 	mutex_exit(&dacf_lock);
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate 	return (ret);
14807c478bd9Sstevel@tonic-gate }
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate /*
14837c478bd9Sstevel@tonic-gate  * Wrapper for making multiple state transitions
14847c478bd9Sstevel@tonic-gate  */
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate /*
14877c478bd9Sstevel@tonic-gate  * i_ndi_config_node: upgrade dev_info node into a specified state.
14887c478bd9Sstevel@tonic-gate  * It is a bit tricky because the locking protocol changes before and
14897c478bd9Sstevel@tonic-gate  * after a node is bound to a driver. All locks are held external to
14907c478bd9Sstevel@tonic-gate  * this function.
14917c478bd9Sstevel@tonic-gate  */
14927c478bd9Sstevel@tonic-gate int
14937c478bd9Sstevel@tonic-gate i_ndi_config_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag)
14947c478bd9Sstevel@tonic-gate {
14957c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flag))
14967c478bd9Sstevel@tonic-gate 	int rv = DDI_SUCCESS;
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
14997c478bd9Sstevel@tonic-gate 
15007c478bd9Sstevel@tonic-gate 	while ((i_ddi_node_state(dip) < state) && (rv == DDI_SUCCESS)) {
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 		/* don't allow any more changes to the device tree */
15037c478bd9Sstevel@tonic-gate 		if (devinfo_freeze) {
15047c478bd9Sstevel@tonic-gate 			rv = DDI_FAILURE;
15057c478bd9Sstevel@tonic-gate 			break;
15067c478bd9Sstevel@tonic-gate 		}
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate 		switch (i_ddi_node_state(dip)) {
15097c478bd9Sstevel@tonic-gate 		case DS_PROTO:
15107c478bd9Sstevel@tonic-gate 			/*
15117c478bd9Sstevel@tonic-gate 			 * only caller can reference this node, no external
15127c478bd9Sstevel@tonic-gate 			 * locking needed.
15137c478bd9Sstevel@tonic-gate 			 */
15147c478bd9Sstevel@tonic-gate 			link_node(dip);
1515fc256490SJason Beloro 			translate_devid((dev_info_t *)dip);
15167c478bd9Sstevel@tonic-gate 			i_ddi_set_node_state(dip, DS_LINKED);
15177c478bd9Sstevel@tonic-gate 			break;
15187c478bd9Sstevel@tonic-gate 		case DS_LINKED:
15197c478bd9Sstevel@tonic-gate 			/*
15207c478bd9Sstevel@tonic-gate 			 * Three code path may attempt to bind a node:
15217c478bd9Sstevel@tonic-gate 			 * - boot code
15227c478bd9Sstevel@tonic-gate 			 * - add_drv
15237c478bd9Sstevel@tonic-gate 			 * - hotplug thread
15247c478bd9Sstevel@tonic-gate 			 * Boot code is single threaded, add_drv synchronize
15257c478bd9Sstevel@tonic-gate 			 * on a userland lock, and hotplug synchronize on
15267c478bd9Sstevel@tonic-gate 			 * hotplug_lk. There could be a race between add_drv
15277c478bd9Sstevel@tonic-gate 			 * and hotplug thread. We'll live with this until the
15287c478bd9Sstevel@tonic-gate 			 * conversion to top-down loading.
15297c478bd9Sstevel@tonic-gate 			 */
15307c478bd9Sstevel@tonic-gate 			if ((rv = bind_node(dip)) == DDI_SUCCESS)
15317c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_BOUND);
1532f4da9be0Scth 
15337c478bd9Sstevel@tonic-gate 			break;
15347c478bd9Sstevel@tonic-gate 		case DS_BOUND:
15357c478bd9Sstevel@tonic-gate 			/*
15367c478bd9Sstevel@tonic-gate 			 * The following transitions synchronizes on the
15377c478bd9Sstevel@tonic-gate 			 * per-driver busy changing flag, since we already
15387c478bd9Sstevel@tonic-gate 			 * have a driver.
15397c478bd9Sstevel@tonic-gate 			 */
15407c478bd9Sstevel@tonic-gate 			if ((rv = init_node(dip)) == DDI_SUCCESS)
15417c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_INITIALIZED);
15427c478bd9Sstevel@tonic-gate 			break;
15437c478bd9Sstevel@tonic-gate 		case DS_INITIALIZED:
15447c478bd9Sstevel@tonic-gate 			if ((rv = probe_node(dip)) == DDI_SUCCESS)
15457c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_PROBED);
15467c478bd9Sstevel@tonic-gate 			break;
15477c478bd9Sstevel@tonic-gate 		case DS_PROBED:
154825e8c5aaSvikram 			i_ddi_check_retire(dip);
15497c478bd9Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, 1);
15507c478bd9Sstevel@tonic-gate 			if ((rv = attach_node(dip)) == DDI_SUCCESS)
15517c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_ATTACHED);
15527c478bd9Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, -1);
15537c478bd9Sstevel@tonic-gate 			break;
15547c478bd9Sstevel@tonic-gate 		case DS_ATTACHED:
15557c478bd9Sstevel@tonic-gate 			if ((rv = postattach_node(dip)) == DDI_SUCCESS)
15567c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_READY);
15577c478bd9Sstevel@tonic-gate 			break;
15587c478bd9Sstevel@tonic-gate 		case DS_READY:
15597c478bd9Sstevel@tonic-gate 			break;
15607c478bd9Sstevel@tonic-gate 		default:
15617c478bd9Sstevel@tonic-gate 			/* should never reach here */
15627c478bd9Sstevel@tonic-gate 			ASSERT("unknown devinfo state");
15637c478bd9Sstevel@tonic-gate 		}
15647c478bd9Sstevel@tonic-gate 	}
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 	if (ddidebug & DDI_AUDIT)
15677c478bd9Sstevel@tonic-gate 		da_log_enter(dip);
15687c478bd9Sstevel@tonic-gate 	return (rv);
15697c478bd9Sstevel@tonic-gate }
15707c478bd9Sstevel@tonic-gate 
15717c478bd9Sstevel@tonic-gate /*
15727c478bd9Sstevel@tonic-gate  * i_ndi_unconfig_node: downgrade dev_info node into a specified state.
15737c478bd9Sstevel@tonic-gate  */
15747c478bd9Sstevel@tonic-gate int
15757c478bd9Sstevel@tonic-gate i_ndi_unconfig_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag)
15767c478bd9Sstevel@tonic-gate {
15777c478bd9Sstevel@tonic-gate 	int	rv = DDI_SUCCESS;
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate 	while ((i_ddi_node_state(dip) > state) && (rv == DDI_SUCCESS)) {
15827c478bd9Sstevel@tonic-gate 
15837c478bd9Sstevel@tonic-gate 		/* don't allow any more changes to the device tree */
15847c478bd9Sstevel@tonic-gate 		if (devinfo_freeze) {
15857c478bd9Sstevel@tonic-gate 			rv = DDI_FAILURE;
15867c478bd9Sstevel@tonic-gate 			break;
15877c478bd9Sstevel@tonic-gate 		}
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate 		switch (i_ddi_node_state(dip)) {
15907c478bd9Sstevel@tonic-gate 		case DS_PROTO:
15917c478bd9Sstevel@tonic-gate 			break;
15927c478bd9Sstevel@tonic-gate 		case DS_LINKED:
15937c478bd9Sstevel@tonic-gate 			/*
15947c478bd9Sstevel@tonic-gate 			 * Persistent nodes are only removed by hotplug code
15957c478bd9Sstevel@tonic-gate 			 * .conf nodes synchronizes on per-driver list.
15967c478bd9Sstevel@tonic-gate 			 */
15977c478bd9Sstevel@tonic-gate 			if ((rv = unlink_node(dip)) == DDI_SUCCESS)
15987c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_PROTO);
15997c478bd9Sstevel@tonic-gate 			break;
16007c478bd9Sstevel@tonic-gate 		case DS_BOUND:
16017c478bd9Sstevel@tonic-gate 			/*
16027c478bd9Sstevel@tonic-gate 			 * The following transitions synchronizes on the
16037c478bd9Sstevel@tonic-gate 			 * per-driver busy changing flag, since we already
16047c478bd9Sstevel@tonic-gate 			 * have a driver.
16057c478bd9Sstevel@tonic-gate 			 */
16067c478bd9Sstevel@tonic-gate 			if ((rv = unbind_node(dip)) == DDI_SUCCESS)
16077c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_LINKED);
16087c478bd9Sstevel@tonic-gate 			break;
16097c478bd9Sstevel@tonic-gate 		case DS_INITIALIZED:
16107c478bd9Sstevel@tonic-gate 			if ((rv = uninit_node(dip)) == DDI_SUCCESS)
16117c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_BOUND);
16127c478bd9Sstevel@tonic-gate 			break;
16137c478bd9Sstevel@tonic-gate 		case DS_PROBED:
16147c478bd9Sstevel@tonic-gate 			if ((rv = unprobe_node(dip)) == DDI_SUCCESS)
16157c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_INITIALIZED);
16167c478bd9Sstevel@tonic-gate 			break;
16177c478bd9Sstevel@tonic-gate 		case DS_ATTACHED:
16187c478bd9Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, 1);
161916747f41Scth 
162016747f41Scth 			mutex_enter(&(DEVI(dip)->devi_lock));
16217c478bd9Sstevel@tonic-gate 			DEVI_SET_DETACHING(dip);
162216747f41Scth 			mutex_exit(&(DEVI(dip)->devi_lock));
162316747f41Scth 
16247c478bd9Sstevel@tonic-gate 			membar_enter();	/* ensure visibility for hold_devi */
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate 			if ((rv = detach_node(dip, flag)) == DDI_SUCCESS)
16277c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_PROBED);
162816747f41Scth 
162916747f41Scth 			mutex_enter(&(DEVI(dip)->devi_lock));
16307c478bd9Sstevel@tonic-gate 			DEVI_CLR_DETACHING(dip);
163116747f41Scth 			mutex_exit(&(DEVI(dip)->devi_lock));
163216747f41Scth 
16337c478bd9Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, -1);
16347c478bd9Sstevel@tonic-gate 			break;
16357c478bd9Sstevel@tonic-gate 		case DS_READY:
16367c478bd9Sstevel@tonic-gate 			if ((rv = predetach_node(dip, flag)) == DDI_SUCCESS)
16377c478bd9Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_ATTACHED);
16387c478bd9Sstevel@tonic-gate 			break;
16397c478bd9Sstevel@tonic-gate 		default:
16407c478bd9Sstevel@tonic-gate 			ASSERT("unknown devinfo state");
16417c478bd9Sstevel@tonic-gate 		}
16427c478bd9Sstevel@tonic-gate 	}
16437c478bd9Sstevel@tonic-gate 	da_log_enter(dip);
16447c478bd9Sstevel@tonic-gate 	return (rv);
16457c478bd9Sstevel@tonic-gate }
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate /*
16487c478bd9Sstevel@tonic-gate  * ddi_initchild: transform node to DS_INITIALIZED state
16497c478bd9Sstevel@tonic-gate  */
16507c478bd9Sstevel@tonic-gate int
16517c478bd9Sstevel@tonic-gate ddi_initchild(dev_info_t *parent, dev_info_t *proto)
16527c478bd9Sstevel@tonic-gate {
16537c478bd9Sstevel@tonic-gate 	int ret, circ;
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
16567c478bd9Sstevel@tonic-gate 	ret = i_ndi_config_node(proto, DS_INITIALIZED, 0);
16577c478bd9Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate 	return (ret);
16607c478bd9Sstevel@tonic-gate }
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate /*
16637c478bd9Sstevel@tonic-gate  * ddi_uninitchild: transform node down to DS_BOUND state
16647c478bd9Sstevel@tonic-gate  */
16657c478bd9Sstevel@tonic-gate int
16667c478bd9Sstevel@tonic-gate ddi_uninitchild(dev_info_t *dip)
16677c478bd9Sstevel@tonic-gate {
16687c478bd9Sstevel@tonic-gate 	int ret, circ;
16697c478bd9Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
16707c478bd9Sstevel@tonic-gate 	ASSERT(parent);
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
16737c478bd9Sstevel@tonic-gate 	ret = i_ndi_unconfig_node(dip, DS_BOUND, 0);
16747c478bd9Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 	return (ret);
16777c478bd9Sstevel@tonic-gate }
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate /*
1680737d277aScth  * i_ddi_attachchild: transform node to DS_READY/i_ddi_devi_attached() state
16817c478bd9Sstevel@tonic-gate  */
16827c478bd9Sstevel@tonic-gate static int
16837c478bd9Sstevel@tonic-gate i_ddi_attachchild(dev_info_t *dip)
16847c478bd9Sstevel@tonic-gate {
16857c478bd9Sstevel@tonic-gate 	dev_info_t	*parent = ddi_get_parent(dip);
16865e3986cbScth 	int		ret;
16875e3986cbScth 
16885e3986cbScth 	ASSERT(parent && DEVI_BUSY_OWNED(parent));
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate 	if ((i_ddi_node_state(dip) < DS_BOUND) || DEVI_IS_DEVICE_OFFLINE(dip))
16917c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
16927c478bd9Sstevel@tonic-gate 
16937c478bd9Sstevel@tonic-gate 	ret = i_ndi_config_node(dip, DS_READY, 0);
16947c478bd9Sstevel@tonic-gate 	if (ret == NDI_SUCCESS) {
16957c478bd9Sstevel@tonic-gate 		ret = DDI_SUCCESS;
16967c478bd9Sstevel@tonic-gate 	} else {
16977c478bd9Sstevel@tonic-gate 		/*
16987c478bd9Sstevel@tonic-gate 		 * Take it down to DS_INITIALIZED so pm_pre_probe is run
16997c478bd9Sstevel@tonic-gate 		 * on the next attach
17007c478bd9Sstevel@tonic-gate 		 */
17017c478bd9Sstevel@tonic-gate 		(void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0);
17027c478bd9Sstevel@tonic-gate 		ret = DDI_FAILURE;
17037c478bd9Sstevel@tonic-gate 	}
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate 	return (ret);
17067c478bd9Sstevel@tonic-gate }
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate /*
17097c478bd9Sstevel@tonic-gate  * i_ddi_detachchild: transform node down to DS_PROBED state
17107c478bd9Sstevel@tonic-gate  *	If it fails, put it back to DS_READY state.
17117c478bd9Sstevel@tonic-gate  * NOTE: A node that fails detach may be at DS_ATTACHED instead
1712737d277aScth  * of DS_READY for a small amount of time - this is the source of
1713737d277aScth  * transient DS_READY->DS_ATTACHED->DS_READY state changes.
17147c478bd9Sstevel@tonic-gate  */
17157c478bd9Sstevel@tonic-gate static int
17167c478bd9Sstevel@tonic-gate i_ddi_detachchild(dev_info_t *dip, uint_t flags)
17177c478bd9Sstevel@tonic-gate {
17187c478bd9Sstevel@tonic-gate 	dev_info_t	*parent = ddi_get_parent(dip);
17195e3986cbScth 	int		ret;
17207c478bd9Sstevel@tonic-gate 
17215e3986cbScth 	ASSERT(parent && DEVI_BUSY_OWNED(parent));
17225e3986cbScth 
17237c478bd9Sstevel@tonic-gate 	ret = i_ndi_unconfig_node(dip, DS_PROBED, flags);
17247c478bd9Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
17257c478bd9Sstevel@tonic-gate 		(void) i_ndi_config_node(dip, DS_READY, 0);
17267c478bd9Sstevel@tonic-gate 	else
17277c478bd9Sstevel@tonic-gate 		/* allow pm_pre_probe to reestablish pm state */
17287c478bd9Sstevel@tonic-gate 		(void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0);
17297c478bd9Sstevel@tonic-gate 	return (ret);
17307c478bd9Sstevel@tonic-gate }
17317c478bd9Sstevel@tonic-gate 
17327c478bd9Sstevel@tonic-gate /*
17337c478bd9Sstevel@tonic-gate  * Add a child and bind to driver
17347c478bd9Sstevel@tonic-gate  */
17357c478bd9Sstevel@tonic-gate dev_info_t *
17367c478bd9Sstevel@tonic-gate ddi_add_child(dev_info_t *pdip, char *name, uint_t nodeid, uint_t unit)
17377c478bd9Sstevel@tonic-gate {
17387c478bd9Sstevel@tonic-gate 	int circ;
17397c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
17407c478bd9Sstevel@tonic-gate 
17417c478bd9Sstevel@tonic-gate 	/* allocate a new node */
17427c478bd9Sstevel@tonic-gate 	dip = i_ddi_alloc_node(pdip, name, nodeid, (int)unit, NULL, KM_SLEEP);
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
17457c478bd9Sstevel@tonic-gate 	(void) i_ndi_config_node(dip, DS_BOUND, 0);
17467c478bd9Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
17477c478bd9Sstevel@tonic-gate 	return (dip);
17487c478bd9Sstevel@tonic-gate }
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate /*
17517c478bd9Sstevel@tonic-gate  * ddi_remove_child: remove the dip. The parent must be attached and held
17527c478bd9Sstevel@tonic-gate  */
17537c478bd9Sstevel@tonic-gate int
17547c478bd9Sstevel@tonic-gate ddi_remove_child(dev_info_t *dip, int dummy)
17557c478bd9Sstevel@tonic-gate {
17567c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dummy))
17577c478bd9Sstevel@tonic-gate 	int circ, ret;
17587c478bd9Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
17597c478bd9Sstevel@tonic-gate 	ASSERT(parent);
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate 	/*
17647c478bd9Sstevel@tonic-gate 	 * If we still have children, for example SID nodes marked
17657c478bd9Sstevel@tonic-gate 	 * as persistent but not attached, attempt to remove them.
17667c478bd9Sstevel@tonic-gate 	 */
17677c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_child) {
17687c478bd9Sstevel@tonic-gate 		ret = ndi_devi_unconfig(dip, NDI_DEVI_REMOVE);
17697c478bd9Sstevel@tonic-gate 		if (ret != NDI_SUCCESS) {
17707c478bd9Sstevel@tonic-gate 			ndi_devi_exit(parent, circ);
17717c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
17727c478bd9Sstevel@tonic-gate 		}
17737c478bd9Sstevel@tonic-gate 		ASSERT(DEVI(dip)->devi_child == NULL);
17747c478bd9Sstevel@tonic-gate 	}
17757c478bd9Sstevel@tonic-gate 
17767c478bd9Sstevel@tonic-gate 	ret = i_ndi_unconfig_node(dip, DS_PROTO, 0);
17777c478bd9Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
17807c478bd9Sstevel@tonic-gate 		return (ret);
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_PROTO);
17837c478bd9Sstevel@tonic-gate 	i_ddi_free_node(dip);
17847c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
17857c478bd9Sstevel@tonic-gate }
17867c478bd9Sstevel@tonic-gate 
17877c478bd9Sstevel@tonic-gate /*
17887c478bd9Sstevel@tonic-gate  * NDI wrappers for ref counting, node allocation, and transitions
17897c478bd9Sstevel@tonic-gate  */
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate /*
17927c478bd9Sstevel@tonic-gate  * Hold/release the devinfo node itself.
17937c478bd9Sstevel@tonic-gate  * Caller is assumed to prevent the devi from detaching during this call
17947c478bd9Sstevel@tonic-gate  */
17957c478bd9Sstevel@tonic-gate void
17967c478bd9Sstevel@tonic-gate ndi_hold_devi(dev_info_t *dip)
17977c478bd9Sstevel@tonic-gate {
17987c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
17997c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_ref >= 0);
18007c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_ref++;
18017c478bd9Sstevel@tonic-gate 	membar_enter();			/* make sure stores are flushed */
18027c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
18037c478bd9Sstevel@tonic-gate }
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate void
18067c478bd9Sstevel@tonic-gate ndi_rele_devi(dev_info_t *dip)
18077c478bd9Sstevel@tonic-gate {
18087c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_ref > 0);
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
18117c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_ref--;
18127c478bd9Sstevel@tonic-gate 	membar_enter();			/* make sure stores are flushed */
18137c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
18147c478bd9Sstevel@tonic-gate }
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate int
18177c478bd9Sstevel@tonic-gate e_ddi_devi_holdcnt(dev_info_t *dip)
18187c478bd9Sstevel@tonic-gate {
18197c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_ref);
18207c478bd9Sstevel@tonic-gate }
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate /*
18237c478bd9Sstevel@tonic-gate  * Hold/release the driver the devinfo node is bound to.
18247c478bd9Sstevel@tonic-gate  */
18257c478bd9Sstevel@tonic-gate struct dev_ops *
18267c478bd9Sstevel@tonic-gate ndi_hold_driver(dev_info_t *dip)
18277c478bd9Sstevel@tonic-gate {
18287c478bd9Sstevel@tonic-gate 	if (i_ddi_node_state(dip) < DS_BOUND)
18297c478bd9Sstevel@tonic-gate 		return (NULL);
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_major != -1);
18327c478bd9Sstevel@tonic-gate 	return (mod_hold_dev_by_major(DEVI(dip)->devi_major));
18337c478bd9Sstevel@tonic-gate }
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate void
18367c478bd9Sstevel@tonic-gate ndi_rele_driver(dev_info_t *dip)
18377c478bd9Sstevel@tonic-gate {
18387c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) >= DS_BOUND);
18397c478bd9Sstevel@tonic-gate 	mod_rele_dev_by_major(DEVI(dip)->devi_major);
18407c478bd9Sstevel@tonic-gate }
18417c478bd9Sstevel@tonic-gate 
18427c478bd9Sstevel@tonic-gate /*
1843b9ccdc5aScth  * Single thread entry into devinfo node for modifying its children (devinfo,
1844b9ccdc5aScth  * pathinfo, and minor). To verify in ASSERTS use DEVI_BUSY_OWNED macro.
18457c478bd9Sstevel@tonic-gate  */
18467c478bd9Sstevel@tonic-gate void
18477c478bd9Sstevel@tonic-gate ndi_devi_enter(dev_info_t *dip, int *circular)
18487c478bd9Sstevel@tonic-gate {
18497c478bd9Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
18507c478bd9Sstevel@tonic-gate 	ASSERT(dip != NULL);
18517c478bd9Sstevel@tonic-gate 
18525e3986cbScth 	/* for vHCI, enforce (vHCI, pHCI) ndi_deve_enter() order */
18535e3986cbScth 	ASSERT(!MDI_VHCI(dip) || (mdi_devi_pdip_entered(dip) == 0) ||
18545e3986cbScth 	    DEVI_BUSY_OWNED(dip));
18555e3986cbScth 
18567c478bd9Sstevel@tonic-gate 	mutex_enter(&devi->devi_lock);
18577c478bd9Sstevel@tonic-gate 	if (devi->devi_busy_thread == curthread) {
18587c478bd9Sstevel@tonic-gate 		devi->devi_circular++;
18597c478bd9Sstevel@tonic-gate 	} else {
18607c478bd9Sstevel@tonic-gate 		while (DEVI_BUSY_CHANGING(devi) && !panicstr)
18617c478bd9Sstevel@tonic-gate 			cv_wait(&(devi->devi_cv), &(devi->devi_lock));
18627c478bd9Sstevel@tonic-gate 		if (panicstr) {
18637c478bd9Sstevel@tonic-gate 			mutex_exit(&devi->devi_lock);
18647c478bd9Sstevel@tonic-gate 			return;
18657c478bd9Sstevel@tonic-gate 		}
18667c478bd9Sstevel@tonic-gate 		devi->devi_flags |= DEVI_BUSY;
18677c478bd9Sstevel@tonic-gate 		devi->devi_busy_thread = curthread;
18687c478bd9Sstevel@tonic-gate 	}
18697c478bd9Sstevel@tonic-gate 	*circular = devi->devi_circular;
18707c478bd9Sstevel@tonic-gate 	mutex_exit(&devi->devi_lock);
18717c478bd9Sstevel@tonic-gate }
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate /*
18747c478bd9Sstevel@tonic-gate  * Release ndi_devi_enter or successful ndi_devi_tryenter.
18757c478bd9Sstevel@tonic-gate  */
18767c478bd9Sstevel@tonic-gate void
18777c478bd9Sstevel@tonic-gate ndi_devi_exit(dev_info_t *dip, int circular)
18787c478bd9Sstevel@tonic-gate {
18797c478bd9Sstevel@tonic-gate 	struct dev_info	*devi = DEVI(dip);
18805e3986cbScth 	struct dev_info	*vdevi;
18817c478bd9Sstevel@tonic-gate 	ASSERT(dip != NULL);
18827c478bd9Sstevel@tonic-gate 
18837c478bd9Sstevel@tonic-gate 	if (panicstr)
18847c478bd9Sstevel@tonic-gate 		return;
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate 	mutex_enter(&(devi->devi_lock));
18877c478bd9Sstevel@tonic-gate 	if (circular != 0) {
18887c478bd9Sstevel@tonic-gate 		devi->devi_circular--;
18897c478bd9Sstevel@tonic-gate 	} else {
18907c478bd9Sstevel@tonic-gate 		devi->devi_flags &= ~DEVI_BUSY;
18917c478bd9Sstevel@tonic-gate 		ASSERT(devi->devi_busy_thread == curthread);
18927c478bd9Sstevel@tonic-gate 		devi->devi_busy_thread = NULL;
18937c478bd9Sstevel@tonic-gate 		cv_broadcast(&(devi->devi_cv));
18947c478bd9Sstevel@tonic-gate 	}
18957c478bd9Sstevel@tonic-gate 	mutex_exit(&(devi->devi_lock));
18965e3986cbScth 
18975e3986cbScth 	/*
18985e3986cbScth 	 * For pHCI exit we issue a broadcast to vHCI for ndi_devi_config_one()
18995e3986cbScth 	 * doing cv_wait on vHCI.
19005e3986cbScth 	 */
19015e3986cbScth 	if (MDI_PHCI(dip)) {
19025e3986cbScth 		vdevi = DEVI(mdi_devi_get_vdip(dip));
19035e3986cbScth 		if (vdevi) {
19045e3986cbScth 			mutex_enter(&(vdevi->devi_lock));
19055e3986cbScth 			if (vdevi->devi_flags & DEVI_PHCI_SIGNALS_VHCI) {
19065e3986cbScth 				vdevi->devi_flags &= ~DEVI_PHCI_SIGNALS_VHCI;
19075e3986cbScth 				cv_broadcast(&(vdevi->devi_cv));
19085e3986cbScth 			}
19095e3986cbScth 			mutex_exit(&(vdevi->devi_lock));
19105e3986cbScth 		}
19115e3986cbScth 	}
19125e3986cbScth }
19135e3986cbScth 
19145e3986cbScth /*
19155e3986cbScth  * Release ndi_devi_enter and wait for possibility of new children, avoiding
19165e3986cbScth  * possibility of missing broadcast before getting to cv_timedwait().
19175e3986cbScth  */
19185e3986cbScth static void
19195e3986cbScth ndi_devi_exit_and_wait(dev_info_t *dip, int circular, clock_t end_time)
19205e3986cbScth {
19215e3986cbScth 	struct dev_info	*devi = DEVI(dip);
19225e3986cbScth 	ASSERT(dip != NULL);
19235e3986cbScth 
19245e3986cbScth 	if (panicstr)
19255e3986cbScth 		return;
19265e3986cbScth 
19275e3986cbScth 	/*
19285e3986cbScth 	 * We are called to wait for of a new child, and new child can
19295e3986cbScth 	 * only be added if circular is zero.
19305e3986cbScth 	 */
19315e3986cbScth 	ASSERT(circular == 0);
19325e3986cbScth 
19335e3986cbScth 	/* like ndi_devi_exit with circular of zero */
19345e3986cbScth 	mutex_enter(&(devi->devi_lock));
19355e3986cbScth 	devi->devi_flags &= ~DEVI_BUSY;
19365e3986cbScth 	ASSERT(devi->devi_busy_thread == curthread);
19375e3986cbScth 	devi->devi_busy_thread = NULL;
19385e3986cbScth 	cv_broadcast(&(devi->devi_cv));
19395e3986cbScth 
19405e3986cbScth 	/* now wait for new children while still holding devi_lock */
19415e3986cbScth 	(void) cv_timedwait(&devi->devi_cv, &(devi->devi_lock), end_time);
19425e3986cbScth 	mutex_exit(&(devi->devi_lock));
19437c478bd9Sstevel@tonic-gate }
19447c478bd9Sstevel@tonic-gate 
19457c478bd9Sstevel@tonic-gate /*
19467c478bd9Sstevel@tonic-gate  * Attempt to single thread entry into devinfo node for modifying its children.
19477c478bd9Sstevel@tonic-gate  */
19487c478bd9Sstevel@tonic-gate int
19497c478bd9Sstevel@tonic-gate ndi_devi_tryenter(dev_info_t *dip, int *circular)
19507c478bd9Sstevel@tonic-gate {
19517c478bd9Sstevel@tonic-gate 	int rval = 1;		   /* assume we enter */
19527c478bd9Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
19537c478bd9Sstevel@tonic-gate 	ASSERT(dip != NULL);
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate 	mutex_enter(&devi->devi_lock);
19567c478bd9Sstevel@tonic-gate 	if (devi->devi_busy_thread == (void *)curthread) {
19577c478bd9Sstevel@tonic-gate 		devi->devi_circular++;
19587c478bd9Sstevel@tonic-gate 	} else {
19597c478bd9Sstevel@tonic-gate 		if (!DEVI_BUSY_CHANGING(devi)) {
19607c478bd9Sstevel@tonic-gate 			devi->devi_flags |= DEVI_BUSY;
19617c478bd9Sstevel@tonic-gate 			devi->devi_busy_thread = (void *)curthread;
19627c478bd9Sstevel@tonic-gate 		} else {
19637c478bd9Sstevel@tonic-gate 			rval = 0;	/* devi is busy */
19647c478bd9Sstevel@tonic-gate 		}
19657c478bd9Sstevel@tonic-gate 	}
19667c478bd9Sstevel@tonic-gate 	*circular = devi->devi_circular;
19677c478bd9Sstevel@tonic-gate 	mutex_exit(&devi->devi_lock);
19687c478bd9Sstevel@tonic-gate 	return (rval);
19697c478bd9Sstevel@tonic-gate }
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate /*
19727c478bd9Sstevel@tonic-gate  * Allocate and initialize a new dev_info structure.
19737c478bd9Sstevel@tonic-gate  *
19747c478bd9Sstevel@tonic-gate  * This routine may be called at interrupt time by a nexus in
19757c478bd9Sstevel@tonic-gate  * response to a hotplug event, therefore memory allocations are
19767c478bd9Sstevel@tonic-gate  * not allowed to sleep.
19777c478bd9Sstevel@tonic-gate  */
19787c478bd9Sstevel@tonic-gate int
1979fa9e4066Sahrens ndi_devi_alloc(dev_info_t *parent, char *node_name, pnode_t nodeid,
19807c478bd9Sstevel@tonic-gate     dev_info_t **ret_dip)
19817c478bd9Sstevel@tonic-gate {
19827c478bd9Sstevel@tonic-gate 	ASSERT(node_name != NULL);
19837c478bd9Sstevel@tonic-gate 	ASSERT(ret_dip != NULL);
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 	*ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL,
19867c478bd9Sstevel@tonic-gate 	    KM_NOSLEEP);
19877c478bd9Sstevel@tonic-gate 	if (*ret_dip == NULL) {
19887c478bd9Sstevel@tonic-gate 		return (NDI_NOMEM);
19897c478bd9Sstevel@tonic-gate 	}
19907c478bd9Sstevel@tonic-gate 
19917c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
19927c478bd9Sstevel@tonic-gate }
19937c478bd9Sstevel@tonic-gate 
19947c478bd9Sstevel@tonic-gate /*
19957c478bd9Sstevel@tonic-gate  * Allocate and initialize a new dev_info structure
19967c478bd9Sstevel@tonic-gate  * This routine may sleep and should not be called at interrupt time
19977c478bd9Sstevel@tonic-gate  */
19987c478bd9Sstevel@tonic-gate void
1999fa9e4066Sahrens ndi_devi_alloc_sleep(dev_info_t *parent, char *node_name, pnode_t nodeid,
20007c478bd9Sstevel@tonic-gate     dev_info_t **ret_dip)
20017c478bd9Sstevel@tonic-gate {
20027c478bd9Sstevel@tonic-gate 	ASSERT(node_name != NULL);
20037c478bd9Sstevel@tonic-gate 	ASSERT(ret_dip != NULL);
20047c478bd9Sstevel@tonic-gate 
20057c478bd9Sstevel@tonic-gate 	*ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL,
20067c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
20077c478bd9Sstevel@tonic-gate 	ASSERT(*ret_dip);
20087c478bd9Sstevel@tonic-gate }
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate /*
20117c478bd9Sstevel@tonic-gate  * Remove an initialized (but not yet attached) dev_info
20127c478bd9Sstevel@tonic-gate  * node from it's parent.
20137c478bd9Sstevel@tonic-gate  */
20147c478bd9Sstevel@tonic-gate int
20157c478bd9Sstevel@tonic-gate ndi_devi_free(dev_info_t *dip)
20167c478bd9Sstevel@tonic-gate {
20177c478bd9Sstevel@tonic-gate 	ASSERT(dip != NULL);
20187c478bd9Sstevel@tonic-gate 
20197c478bd9Sstevel@tonic-gate 	if (i_ddi_node_state(dip) >= DS_INITIALIZED)
20207c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_free: %s%d (%p)\n",
20237c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
20247c478bd9Sstevel@tonic-gate 
20257c478bd9Sstevel@tonic-gate 	(void) ddi_remove_child(dip, 0);
20267c478bd9Sstevel@tonic-gate 
20277c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
20287c478bd9Sstevel@tonic-gate }
20297c478bd9Sstevel@tonic-gate 
20307c478bd9Sstevel@tonic-gate /*
20317c478bd9Sstevel@tonic-gate  * ndi_devi_bind_driver() binds a driver to a given device. If it fails
20327c478bd9Sstevel@tonic-gate  * to bind the driver, it returns an appropriate error back. Some drivers
20337c478bd9Sstevel@tonic-gate  * may want to know if the actually failed to bind.
20347c478bd9Sstevel@tonic-gate  */
20357c478bd9Sstevel@tonic-gate int
20367c478bd9Sstevel@tonic-gate ndi_devi_bind_driver(dev_info_t *dip, uint_t flags)
20377c478bd9Sstevel@tonic-gate {
20387c478bd9Sstevel@tonic-gate 	int ret = NDI_FAILURE;
20397c478bd9Sstevel@tonic-gate 	int circ;
20407c478bd9Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
20417c478bd9Sstevel@tonic-gate 	ASSERT(pdip);
20427c478bd9Sstevel@tonic-gate 
20437c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
20447c478bd9Sstevel@tonic-gate 	    "ndi_devi_bind_driver: %s%d (%p) flags: %x\n",
20457c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
20467c478bd9Sstevel@tonic-gate 
20477c478bd9Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
20487c478bd9Sstevel@tonic-gate 	if (i_ndi_config_node(dip, DS_BOUND, flags) == DDI_SUCCESS)
20497c478bd9Sstevel@tonic-gate 		ret = NDI_SUCCESS;
20507c478bd9Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate 	return (ret);
20537c478bd9Sstevel@tonic-gate }
20547c478bd9Sstevel@tonic-gate 
20557c478bd9Sstevel@tonic-gate /*
20567c478bd9Sstevel@tonic-gate  * ndi_devi_unbind_driver: unbind the dip
20577c478bd9Sstevel@tonic-gate  */
20587c478bd9Sstevel@tonic-gate static int
20597c478bd9Sstevel@tonic-gate ndi_devi_unbind_driver(dev_info_t *dip)
20607c478bd9Sstevel@tonic-gate {
20617c478bd9Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
20627c478bd9Sstevel@tonic-gate 
20637c478bd9Sstevel@tonic-gate 	return (i_ndi_unconfig_node(dip, DS_LINKED, 0));
20647c478bd9Sstevel@tonic-gate }
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate /*
20677c478bd9Sstevel@tonic-gate  * Misc. help routines called by framework only
20687c478bd9Sstevel@tonic-gate  */
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate /*
20717c478bd9Sstevel@tonic-gate  * Get the state of node
20727c478bd9Sstevel@tonic-gate  */
20737c478bd9Sstevel@tonic-gate ddi_node_state_t
20747c478bd9Sstevel@tonic-gate i_ddi_node_state(dev_info_t *dip)
20757c478bd9Sstevel@tonic-gate {
20767c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_node_state);
20777c478bd9Sstevel@tonic-gate }
20787c478bd9Sstevel@tonic-gate 
20797c478bd9Sstevel@tonic-gate /*
20807c478bd9Sstevel@tonic-gate  * Set the state of node
20817c478bd9Sstevel@tonic-gate  */
20827c478bd9Sstevel@tonic-gate void
20837c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dev_info_t *dip, ddi_node_state_t state)
20847c478bd9Sstevel@tonic-gate {
20857c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_node_state = state;
20867c478bd9Sstevel@tonic-gate 	membar_enter();			/* make sure stores are flushed */
20877c478bd9Sstevel@tonic-gate }
20887c478bd9Sstevel@tonic-gate 
20897c478bd9Sstevel@tonic-gate /*
2090737d277aScth  * Determine if node is attached. The implementation accommodates transient
2091737d277aScth  * DS_READY->DS_ATTACHED->DS_READY state changes.  Outside this file, this
2092737d277aScth  * function should be instead of i_ddi_node_state() DS_ATTACHED/DS_READY
2093737d277aScth  * state checks.
2094737d277aScth  */
2095737d277aScth int
2096737d277aScth i_ddi_devi_attached(dev_info_t *dip)
2097737d277aScth {
2098737d277aScth 	return (DEVI(dip)->devi_node_state >= DS_ATTACHED);
2099737d277aScth }
2100737d277aScth 
2101737d277aScth /*
21027c478bd9Sstevel@tonic-gate  * Common function for finding a node in a sibling list given name and addr.
21037c478bd9Sstevel@tonic-gate  *
21047c478bd9Sstevel@tonic-gate  * By default, name is matched with devi_node_name. The following
21057c478bd9Sstevel@tonic-gate  * alternative match strategies are supported:
21067c478bd9Sstevel@tonic-gate  *
2107f4da9be0Scth  *	FIND_NODE_BY_NODENAME: Match on node name - typical use.
2108027021c7SChris Horne  *
2109f4da9be0Scth  *	FIND_NODE_BY_DRIVER: A match on driver name bound to node is conducted.
21107c478bd9Sstevel@tonic-gate  *		This support is used for support of OBP generic names and
21117c478bd9Sstevel@tonic-gate  *		for the conversion from driver names to generic names. When
21127c478bd9Sstevel@tonic-gate  *		more consistency in the generic name environment is achieved
21137c478bd9Sstevel@tonic-gate  *		(and not needed for upgrade) this support can be removed.
2114027021c7SChris Horne  *
2115f4da9be0Scth  *	FIND_NODE_BY_ADDR: Match on just the addr.
2116f4da9be0Scth  *		This support is only used/needed during boot to match
2117f4da9be0Scth  *		a node bound via a path-based driver alias.
21187c478bd9Sstevel@tonic-gate  *
21197c478bd9Sstevel@tonic-gate  * If a child is not named (dev_addr == NULL), there are three
21207c478bd9Sstevel@tonic-gate  * possible actions:
21217c478bd9Sstevel@tonic-gate  *
21227c478bd9Sstevel@tonic-gate  *	(1) skip it
21237c478bd9Sstevel@tonic-gate  *	(2) FIND_ADDR_BY_INIT: bring child to DS_INITIALIZED state
21247c478bd9Sstevel@tonic-gate  *	(3) FIND_ADDR_BY_CALLBACK: use a caller-supplied callback function
21257c478bd9Sstevel@tonic-gate  */
2126f4da9be0Scth #define	FIND_NODE_BY_NODENAME	0x01
2127f4da9be0Scth #define	FIND_NODE_BY_DRIVER	0x02
2128f4da9be0Scth #define	FIND_NODE_BY_ADDR	0x04
21297c478bd9Sstevel@tonic-gate #define	FIND_ADDR_BY_INIT	0x10
21307c478bd9Sstevel@tonic-gate #define	FIND_ADDR_BY_CALLBACK	0x20
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate static dev_info_t *
21337c478bd9Sstevel@tonic-gate find_sibling(dev_info_t *head, char *cname, char *caddr, uint_t flag,
21347c478bd9Sstevel@tonic-gate     int (*callback)(dev_info_t *, char *, int))
21357c478bd9Sstevel@tonic-gate {
21367c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
21377c478bd9Sstevel@tonic-gate 	char		*addr, *buf;
21387c478bd9Sstevel@tonic-gate 	major_t		major;
2139f4da9be0Scth 	uint_t		by;
2140f4da9be0Scth 
2141f4da9be0Scth 	/* only one way to find a node */
2142f4da9be0Scth 	by = flag &
2143f4da9be0Scth 	    (FIND_NODE_BY_DRIVER | FIND_NODE_BY_NODENAME | FIND_NODE_BY_ADDR);
2144f4da9be0Scth 	ASSERT(by && BIT_ONLYONESET(by));
21457c478bd9Sstevel@tonic-gate 
21467c478bd9Sstevel@tonic-gate 	/* only one way to name a node */
21477c478bd9Sstevel@tonic-gate 	ASSERT(((flag & FIND_ADDR_BY_INIT) == 0) ||
21487c478bd9Sstevel@tonic-gate 	    ((flag & FIND_ADDR_BY_CALLBACK) == 0));
21497c478bd9Sstevel@tonic-gate 
2150f4da9be0Scth 	if (by == FIND_NODE_BY_DRIVER) {
21517c478bd9Sstevel@tonic-gate 		major = ddi_name_to_major(cname);
2152a204de77Scth 		if (major == DDI_MAJOR_T_NONE)
21537c478bd9Sstevel@tonic-gate 			return (NULL);
21547c478bd9Sstevel@tonic-gate 	}
21557c478bd9Sstevel@tonic-gate 
21567c478bd9Sstevel@tonic-gate 	/* preallocate buffer of naming node by callback */
21577c478bd9Sstevel@tonic-gate 	if (flag & FIND_ADDR_BY_CALLBACK)
21587c478bd9Sstevel@tonic-gate 		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate 	/*
21617c478bd9Sstevel@tonic-gate 	 * Walk the child list to find a match
21627c478bd9Sstevel@tonic-gate 	 */
21634c06356bSdh142964 	if (head == NULL)
21644c06356bSdh142964 		return (NULL);
21654c06356bSdh142964 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(head)));
21667c478bd9Sstevel@tonic-gate 	for (dip = head; dip; dip = ddi_get_next_sibling(dip)) {
2167f4da9be0Scth 		if (by == FIND_NODE_BY_NODENAME) {
21687c478bd9Sstevel@tonic-gate 			/* match node name */
21697c478bd9Sstevel@tonic-gate 			if (strcmp(cname, DEVI(dip)->devi_node_name) != 0)
21707c478bd9Sstevel@tonic-gate 				continue;
2171f4da9be0Scth 		} else if (by == FIND_NODE_BY_DRIVER) {
2172f4da9be0Scth 			/* match driver major */
2173f4da9be0Scth 			if (DEVI(dip)->devi_major != major)
2174f4da9be0Scth 				continue;
21757c478bd9Sstevel@tonic-gate 		}
21767c478bd9Sstevel@tonic-gate 
21777c478bd9Sstevel@tonic-gate 		if ((addr = DEVI(dip)->devi_addr) == NULL) {
21787c478bd9Sstevel@tonic-gate 			/* name the child based on the flag */
21797c478bd9Sstevel@tonic-gate 			if (flag & FIND_ADDR_BY_INIT) {
21807c478bd9Sstevel@tonic-gate 				if (ddi_initchild(ddi_get_parent(dip), dip)
21817c478bd9Sstevel@tonic-gate 				    != DDI_SUCCESS)
21827c478bd9Sstevel@tonic-gate 					continue;
21837c478bd9Sstevel@tonic-gate 				addr = DEVI(dip)->devi_addr;
21847c478bd9Sstevel@tonic-gate 			} else if (flag & FIND_ADDR_BY_CALLBACK) {
21857c478bd9Sstevel@tonic-gate 				if ((callback == NULL) || (callback(
21867c478bd9Sstevel@tonic-gate 				    dip, buf, MAXNAMELEN) != DDI_SUCCESS))
21877c478bd9Sstevel@tonic-gate 					continue;
21887c478bd9Sstevel@tonic-gate 				addr = buf;
21897c478bd9Sstevel@tonic-gate 			} else {
21907c478bd9Sstevel@tonic-gate 				continue;	/* skip */
21917c478bd9Sstevel@tonic-gate 			}
21927c478bd9Sstevel@tonic-gate 		}
21937c478bd9Sstevel@tonic-gate 
21947c478bd9Sstevel@tonic-gate 		/* match addr */
21957c478bd9Sstevel@tonic-gate 		ASSERT(addr != NULL);
21967c478bd9Sstevel@tonic-gate 		if (strcmp(caddr, addr) == 0)
21977c478bd9Sstevel@tonic-gate 			break;	/* node found */
21987c478bd9Sstevel@tonic-gate 
21997c478bd9Sstevel@tonic-gate 	}
22007c478bd9Sstevel@tonic-gate 	if (flag & FIND_ADDR_BY_CALLBACK)
22017c478bd9Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
22027c478bd9Sstevel@tonic-gate 	return (dip);
22037c478bd9Sstevel@tonic-gate }
22047c478bd9Sstevel@tonic-gate 
22057c478bd9Sstevel@tonic-gate /*
22067c478bd9Sstevel@tonic-gate  * Find child of pdip with name: cname@caddr
22077c478bd9Sstevel@tonic-gate  * Called by init_node() to look for duplicate nodes
22087c478bd9Sstevel@tonic-gate  */
22097c478bd9Sstevel@tonic-gate static dev_info_t *
22107c478bd9Sstevel@tonic-gate find_duplicate_child(dev_info_t *pdip, dev_info_t *dip)
22117c478bd9Sstevel@tonic-gate {
22127c478bd9Sstevel@tonic-gate 	dev_info_t *dup;
22137c478bd9Sstevel@tonic-gate 	char *cname = DEVI(dip)->devi_node_name;
22147c478bd9Sstevel@tonic-gate 	char *caddr = DEVI(dip)->devi_addr;
22157c478bd9Sstevel@tonic-gate 
22167c478bd9Sstevel@tonic-gate 	/* search nodes before dip */
2217f4da9be0Scth 	dup = find_sibling(ddi_get_child(pdip), cname, caddr,
2218f4da9be0Scth 	    FIND_NODE_BY_NODENAME, NULL);
22197c478bd9Sstevel@tonic-gate 	if (dup != dip)
22207c478bd9Sstevel@tonic-gate 		return (dup);
22217c478bd9Sstevel@tonic-gate 
22227c478bd9Sstevel@tonic-gate 	/*
22237c478bd9Sstevel@tonic-gate 	 * search nodes after dip; normally this is not needed,
22247c478bd9Sstevel@tonic-gate 	 */
22257c478bd9Sstevel@tonic-gate 	return (find_sibling(ddi_get_next_sibling(dip), cname, caddr,
2226f4da9be0Scth 	    FIND_NODE_BY_NODENAME, NULL));
22277c478bd9Sstevel@tonic-gate }
22287c478bd9Sstevel@tonic-gate 
22297c478bd9Sstevel@tonic-gate /*
22307c478bd9Sstevel@tonic-gate  * Find a child of a given name and address, using a callback to name
22317c478bd9Sstevel@tonic-gate  * unnamed children. cname is the binding name.
22327c478bd9Sstevel@tonic-gate  */
22334c06356bSdh142964 dev_info_t *
22344c06356bSdh142964 ndi_devi_findchild_by_callback(dev_info_t *pdip, char *dname, char *ua,
22354c06356bSdh142964     int (*make_ua)(dev_info_t *, char *, int))
22367c478bd9Sstevel@tonic-gate {
22374c06356bSdh142964 	int	by = FIND_ADDR_BY_CALLBACK;
22384c06356bSdh142964 
22394c06356bSdh142964 	ASSERT(DEVI_BUSY_OWNED(pdip));
22404c06356bSdh142964 	by |= dname ? FIND_NODE_BY_DRIVER : FIND_NODE_BY_ADDR;
22414c06356bSdh142964 	return (find_sibling(ddi_get_child(pdip), dname, ua, by, make_ua));
22427c478bd9Sstevel@tonic-gate }
22437c478bd9Sstevel@tonic-gate 
22447c478bd9Sstevel@tonic-gate /*
22457c478bd9Sstevel@tonic-gate  * Find a child of a given name and address, invoking initchild to name
22467c478bd9Sstevel@tonic-gate  * unnamed children. cname is the node name.
22477c478bd9Sstevel@tonic-gate  */
22487c478bd9Sstevel@tonic-gate static dev_info_t *
22497c478bd9Sstevel@tonic-gate find_child_by_name(dev_info_t *pdip, char *cname, char *caddr)
22507c478bd9Sstevel@tonic-gate {
22517c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
22527c478bd9Sstevel@tonic-gate 
2253f4da9be0Scth 	/* attempt search without changing state of preceding siblings */
2254f4da9be0Scth 	dip = find_sibling(ddi_get_child(pdip), cname, caddr,
2255f4da9be0Scth 	    FIND_NODE_BY_NODENAME, NULL);
22567c478bd9Sstevel@tonic-gate 	if (dip)
22577c478bd9Sstevel@tonic-gate 		return (dip);
22587c478bd9Sstevel@tonic-gate 
22597c478bd9Sstevel@tonic-gate 	return (find_sibling(ddi_get_child(pdip), cname, caddr,
2260f4da9be0Scth 	    FIND_NODE_BY_NODENAME|FIND_ADDR_BY_INIT, NULL));
22617c478bd9Sstevel@tonic-gate }
22627c478bd9Sstevel@tonic-gate 
22637c478bd9Sstevel@tonic-gate /*
22647c478bd9Sstevel@tonic-gate  * Find a child of a given name and address, invoking initchild to name
22657c478bd9Sstevel@tonic-gate  * unnamed children. cname is the node name.
22667c478bd9Sstevel@tonic-gate  */
22677c478bd9Sstevel@tonic-gate static dev_info_t *
22687c478bd9Sstevel@tonic-gate find_child_by_driver(dev_info_t *pdip, char *cname, char *caddr)
22697c478bd9Sstevel@tonic-gate {
22707c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
22717c478bd9Sstevel@tonic-gate 
2272f4da9be0Scth 	/* attempt search without changing state of preceding siblings */
22737c478bd9Sstevel@tonic-gate 	dip = find_sibling(ddi_get_child(pdip), cname, caddr,
2274f4da9be0Scth 	    FIND_NODE_BY_DRIVER, NULL);
22757c478bd9Sstevel@tonic-gate 	if (dip)
22767c478bd9Sstevel@tonic-gate 		return (dip);
22777c478bd9Sstevel@tonic-gate 
22787c478bd9Sstevel@tonic-gate 	return (find_sibling(ddi_get_child(pdip), cname, caddr,
2279f4da9be0Scth 	    FIND_NODE_BY_DRIVER|FIND_ADDR_BY_INIT, NULL));
2280f4da9be0Scth }
2281f4da9be0Scth 
2282f4da9be0Scth /*
2283f4da9be0Scth  * Find a child of a given address, invoking initchild to name
2284f4da9be0Scth  * unnamed children. cname is the node name.
2285f4da9be0Scth  *
2286f4da9be0Scth  * NOTE: This function is only used during boot. One would hope that
2287f4da9be0Scth  * unique sibling unit-addresses on hardware branches of the tree would
2288f4da9be0Scth  * be a requirement to avoid two drivers trying to control the same
2289f4da9be0Scth  * piece of hardware. Unfortunately there are some cases where this
2290f4da9be0Scth  * situation exists (/ssm@0,0/pci@1c,700000 /ssm@0,0/sghsc@1c,700000).
2291f4da9be0Scth  * Until unit-address uniqueness of siblings is guaranteed, use of this
2292f4da9be0Scth  * interface for purposes other than boot should be avoided.
2293f4da9be0Scth  */
2294f4da9be0Scth static dev_info_t *
2295f4da9be0Scth find_child_by_addr(dev_info_t *pdip, char *caddr)
2296f4da9be0Scth {
2297f4da9be0Scth 	dev_info_t	*dip;
2298f4da9be0Scth 
22992486dd1dScth 	/* return NULL if called without a unit-address */
23002486dd1dScth 	if ((caddr == NULL) || (*caddr == '\0'))
23012486dd1dScth 		return (NULL);
23022486dd1dScth 
2303f4da9be0Scth 	/* attempt search without changing state of preceding siblings */
2304f4da9be0Scth 	dip = find_sibling(ddi_get_child(pdip), NULL, caddr,
2305f4da9be0Scth 	    FIND_NODE_BY_ADDR, NULL);
2306f4da9be0Scth 	if (dip)
2307f4da9be0Scth 		return (dip);
2308f4da9be0Scth 
2309f4da9be0Scth 	return (find_sibling(ddi_get_child(pdip), NULL, caddr,
2310f4da9be0Scth 	    FIND_NODE_BY_ADDR|FIND_ADDR_BY_INIT, NULL));
23117c478bd9Sstevel@tonic-gate }
23127c478bd9Sstevel@tonic-gate 
23137c478bd9Sstevel@tonic-gate /*
23147c478bd9Sstevel@tonic-gate  * Deleting a property list. Take care, since some property structures
23157c478bd9Sstevel@tonic-gate  * may not be fully built.
23167c478bd9Sstevel@tonic-gate  */
23177c478bd9Sstevel@tonic-gate void
23187c478bd9Sstevel@tonic-gate i_ddi_prop_list_delete(ddi_prop_t *prop)
23197c478bd9Sstevel@tonic-gate {
23207c478bd9Sstevel@tonic-gate 	while (prop) {
23217c478bd9Sstevel@tonic-gate 		ddi_prop_t *next = prop->prop_next;
23227c478bd9Sstevel@tonic-gate 		if (prop->prop_name)
23237c478bd9Sstevel@tonic-gate 			kmem_free(prop->prop_name, strlen(prop->prop_name) + 1);
23247c478bd9Sstevel@tonic-gate 		if ((prop->prop_len != 0) && prop->prop_val)
23257c478bd9Sstevel@tonic-gate 			kmem_free(prop->prop_val, prop->prop_len);
23267c478bd9Sstevel@tonic-gate 		kmem_free(prop, sizeof (struct ddi_prop));
23277c478bd9Sstevel@tonic-gate 		prop = next;
23287c478bd9Sstevel@tonic-gate 	}
23297c478bd9Sstevel@tonic-gate }
23307c478bd9Sstevel@tonic-gate 
23317c478bd9Sstevel@tonic-gate /*
23327c478bd9Sstevel@tonic-gate  * Duplicate property list
23337c478bd9Sstevel@tonic-gate  */
23347c478bd9Sstevel@tonic-gate ddi_prop_t *
23357c478bd9Sstevel@tonic-gate i_ddi_prop_list_dup(ddi_prop_t *prop, uint_t flag)
23367c478bd9Sstevel@tonic-gate {
23377c478bd9Sstevel@tonic-gate 	ddi_prop_t *result, *prev, *copy;
23387c478bd9Sstevel@tonic-gate 
23397c478bd9Sstevel@tonic-gate 	if (prop == NULL)
23407c478bd9Sstevel@tonic-gate 		return (NULL);
23417c478bd9Sstevel@tonic-gate 
23427c478bd9Sstevel@tonic-gate 	result = prev = NULL;
23437c478bd9Sstevel@tonic-gate 	for (; prop != NULL; prop = prop->prop_next) {
23447c478bd9Sstevel@tonic-gate 		ASSERT(prop->prop_name != NULL);
23457c478bd9Sstevel@tonic-gate 		copy = kmem_zalloc(sizeof (struct ddi_prop), flag);
23467c478bd9Sstevel@tonic-gate 		if (copy == NULL)
23477c478bd9Sstevel@tonic-gate 			goto fail;
23487c478bd9Sstevel@tonic-gate 
23497c478bd9Sstevel@tonic-gate 		copy->prop_dev = prop->prop_dev;
23507c478bd9Sstevel@tonic-gate 		copy->prop_flags = prop->prop_flags;
23517c478bd9Sstevel@tonic-gate 		copy->prop_name = i_ddi_strdup(prop->prop_name, flag);
23527c478bd9Sstevel@tonic-gate 		if (copy->prop_name == NULL)
23537c478bd9Sstevel@tonic-gate 			goto fail;
23547c478bd9Sstevel@tonic-gate 
23557c478bd9Sstevel@tonic-gate 		if ((copy->prop_len = prop->prop_len) != 0) {
23567c478bd9Sstevel@tonic-gate 			copy->prop_val = kmem_zalloc(prop->prop_len, flag);
23577c478bd9Sstevel@tonic-gate 			if (copy->prop_val == NULL)
23587c478bd9Sstevel@tonic-gate 				goto fail;
23597c478bd9Sstevel@tonic-gate 
23607c478bd9Sstevel@tonic-gate 			bcopy(prop->prop_val, copy->prop_val, prop->prop_len);
23617c478bd9Sstevel@tonic-gate 		}
23627c478bd9Sstevel@tonic-gate 
23637c478bd9Sstevel@tonic-gate 		if (prev == NULL)
23647c478bd9Sstevel@tonic-gate 			result = prev = copy;
23657c478bd9Sstevel@tonic-gate 		else
23667c478bd9Sstevel@tonic-gate 			prev->prop_next = copy;
23677c478bd9Sstevel@tonic-gate 		prev = copy;
23687c478bd9Sstevel@tonic-gate 	}
23697c478bd9Sstevel@tonic-gate 	return (result);
23707c478bd9Sstevel@tonic-gate 
23717c478bd9Sstevel@tonic-gate fail:
23727c478bd9Sstevel@tonic-gate 	i_ddi_prop_list_delete(result);
23737c478bd9Sstevel@tonic-gate 	return (NULL);
23747c478bd9Sstevel@tonic-gate }
23757c478bd9Sstevel@tonic-gate 
23767c478bd9Sstevel@tonic-gate /*
23777c478bd9Sstevel@tonic-gate  * Create a reference property list, currently used only for
23787c478bd9Sstevel@tonic-gate  * driver global properties. Created with ref count of 1.
23797c478bd9Sstevel@tonic-gate  */
23807c478bd9Sstevel@tonic-gate ddi_prop_list_t *
23817c478bd9Sstevel@tonic-gate i_ddi_prop_list_create(ddi_prop_t *props)
23827c478bd9Sstevel@tonic-gate {
23837c478bd9Sstevel@tonic-gate 	ddi_prop_list_t *list = kmem_alloc(sizeof (*list), KM_SLEEP);
23847c478bd9Sstevel@tonic-gate 	list->prop_list = props;
23857c478bd9Sstevel@tonic-gate 	list->prop_ref = 1;
23867c478bd9Sstevel@tonic-gate 	return (list);
23877c478bd9Sstevel@tonic-gate }
23887c478bd9Sstevel@tonic-gate 
23897c478bd9Sstevel@tonic-gate /*
23907c478bd9Sstevel@tonic-gate  * Increment/decrement reference count. The reference is
23917c478bd9Sstevel@tonic-gate  * protected by dn_lock. The only interfaces modifying
23927c478bd9Sstevel@tonic-gate  * dn_global_prop_ptr is in impl_make[free]_parlist().
23937c478bd9Sstevel@tonic-gate  */
23947c478bd9Sstevel@tonic-gate void
23957c478bd9Sstevel@tonic-gate i_ddi_prop_list_hold(ddi_prop_list_t *prop_list, struct devnames *dnp)
23967c478bd9Sstevel@tonic-gate {
23977c478bd9Sstevel@tonic-gate 	ASSERT(prop_list->prop_ref >= 0);
23987c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&dnp->dn_lock));
23997c478bd9Sstevel@tonic-gate 	prop_list->prop_ref++;
24007c478bd9Sstevel@tonic-gate }
24017c478bd9Sstevel@tonic-gate 
24027c478bd9Sstevel@tonic-gate void
24037c478bd9Sstevel@tonic-gate i_ddi_prop_list_rele(ddi_prop_list_t *prop_list, struct devnames *dnp)
24047c478bd9Sstevel@tonic-gate {
24057c478bd9Sstevel@tonic-gate 	ASSERT(prop_list->prop_ref > 0);
24067c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&dnp->dn_lock));
24077c478bd9Sstevel@tonic-gate 	prop_list->prop_ref--;
24087c478bd9Sstevel@tonic-gate 
24097c478bd9Sstevel@tonic-gate 	if (prop_list->prop_ref == 0) {
24107c478bd9Sstevel@tonic-gate 		i_ddi_prop_list_delete(prop_list->prop_list);
24117c478bd9Sstevel@tonic-gate 		kmem_free(prop_list, sizeof (*prop_list));
24127c478bd9Sstevel@tonic-gate 	}
24137c478bd9Sstevel@tonic-gate }
24147c478bd9Sstevel@tonic-gate 
24157c478bd9Sstevel@tonic-gate /*
24167c478bd9Sstevel@tonic-gate  * Free table of classes by drivers
24177c478bd9Sstevel@tonic-gate  */
24187c478bd9Sstevel@tonic-gate void
24197c478bd9Sstevel@tonic-gate i_ddi_free_exported_classes(char **classes, int n)
24207c478bd9Sstevel@tonic-gate {
24217c478bd9Sstevel@tonic-gate 	if ((n == 0) || (classes == NULL))
24227c478bd9Sstevel@tonic-gate 		return;
24237c478bd9Sstevel@tonic-gate 
24247c478bd9Sstevel@tonic-gate 	kmem_free(classes, n * sizeof (char *));
24257c478bd9Sstevel@tonic-gate }
24267c478bd9Sstevel@tonic-gate 
24277c478bd9Sstevel@tonic-gate /*
24287c478bd9Sstevel@tonic-gate  * Get all classes exported by dip
24297c478bd9Sstevel@tonic-gate  */
24307c478bd9Sstevel@tonic-gate int
24317c478bd9Sstevel@tonic-gate i_ddi_get_exported_classes(dev_info_t *dip, char ***classes)
24327c478bd9Sstevel@tonic-gate {
24337c478bd9Sstevel@tonic-gate 	extern void lock_hw_class_list();
24347c478bd9Sstevel@tonic-gate 	extern void unlock_hw_class_list();
24357c478bd9Sstevel@tonic-gate 	extern int get_class(const char *, char **);
24367c478bd9Sstevel@tonic-gate 
24377c478bd9Sstevel@tonic-gate 	static char *rootclass = "root";
24387c478bd9Sstevel@tonic-gate 	int n = 0, nclass = 0;
24397c478bd9Sstevel@tonic-gate 	char **buf;
24407c478bd9Sstevel@tonic-gate 
24417c478bd9Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) >= DS_BOUND);
24427c478bd9Sstevel@tonic-gate 
24437c478bd9Sstevel@tonic-gate 	if (dip == ddi_root_node())	/* rootnode exports class "root" */
24447c478bd9Sstevel@tonic-gate 		nclass = 1;
24457c478bd9Sstevel@tonic-gate 	lock_hw_class_list();
24467c478bd9Sstevel@tonic-gate 	nclass += get_class(ddi_driver_name(dip), NULL);
24477c478bd9Sstevel@tonic-gate 	if (nclass == 0) {
24487c478bd9Sstevel@tonic-gate 		unlock_hw_class_list();
24497c478bd9Sstevel@tonic-gate 		return (0);		/* no class exported */
24507c478bd9Sstevel@tonic-gate 	}
24517c478bd9Sstevel@tonic-gate 
24527c478bd9Sstevel@tonic-gate 	*classes = buf = kmem_alloc(nclass * sizeof (char *), KM_SLEEP);
24537c478bd9Sstevel@tonic-gate 	if (dip == ddi_root_node()) {
24547c478bd9Sstevel@tonic-gate 		*buf++ = rootclass;
24557c478bd9Sstevel@tonic-gate 		n = 1;
24567c478bd9Sstevel@tonic-gate 	}
24577c478bd9Sstevel@tonic-gate 	n += get_class(ddi_driver_name(dip), buf);
24587c478bd9Sstevel@tonic-gate 	unlock_hw_class_list();
24597c478bd9Sstevel@tonic-gate 
24607c478bd9Sstevel@tonic-gate 	ASSERT(n == nclass);	/* make sure buf wasn't overrun */
24617c478bd9Sstevel@tonic-gate 	return (nclass);
24627c478bd9Sstevel@tonic-gate }
24637c478bd9Sstevel@tonic-gate 
24647c478bd9Sstevel@tonic-gate /*
24657c478bd9Sstevel@tonic-gate  * Helper functions, returns NULL if no memory.
24667c478bd9Sstevel@tonic-gate  */
24677c478bd9Sstevel@tonic-gate char *
24687c478bd9Sstevel@tonic-gate i_ddi_strdup(char *str, uint_t flag)
24697c478bd9Sstevel@tonic-gate {
24707c478bd9Sstevel@tonic-gate 	char *copy;
24717c478bd9Sstevel@tonic-gate 
24727c478bd9Sstevel@tonic-gate 	if (str == NULL)
24737c478bd9Sstevel@tonic-gate 		return (NULL);
24747c478bd9Sstevel@tonic-gate 
24757c478bd9Sstevel@tonic-gate 	copy = kmem_alloc(strlen(str) + 1, flag);
24767c478bd9Sstevel@tonic-gate 	if (copy == NULL)
24777c478bd9Sstevel@tonic-gate 		return (NULL);
24787c478bd9Sstevel@tonic-gate 
24797c478bd9Sstevel@tonic-gate 	(void) strcpy(copy, str);
24807c478bd9Sstevel@tonic-gate 	return (copy);
24817c478bd9Sstevel@tonic-gate }
24827c478bd9Sstevel@tonic-gate 
24837c478bd9Sstevel@tonic-gate /*
24847c478bd9Sstevel@tonic-gate  * Load driver.conf file for major. Load all if major == -1.
24857c478bd9Sstevel@tonic-gate  *
24867c478bd9Sstevel@tonic-gate  * This is called
24877c478bd9Sstevel@tonic-gate  * - early in boot after devnames array is initialized
24887c478bd9Sstevel@tonic-gate  * - from vfs code when certain file systems are mounted
24897c478bd9Sstevel@tonic-gate  * - from add_drv when a new driver is added
24907c478bd9Sstevel@tonic-gate  */
24917c478bd9Sstevel@tonic-gate int
24927c478bd9Sstevel@tonic-gate i_ddi_load_drvconf(major_t major)
24937c478bd9Sstevel@tonic-gate {
24947c478bd9Sstevel@tonic-gate 	extern int modrootloaded;
24957c478bd9Sstevel@tonic-gate 
24967c478bd9Sstevel@tonic-gate 	major_t low, high, m;
24977c478bd9Sstevel@tonic-gate 
2498a204de77Scth 	if (major == DDI_MAJOR_T_NONE) {
24997c478bd9Sstevel@tonic-gate 		low = 0;
25007c478bd9Sstevel@tonic-gate 		high = devcnt - 1;
25017c478bd9Sstevel@tonic-gate 	} else {
25027c478bd9Sstevel@tonic-gate 		if (major >= devcnt)
25037c478bd9Sstevel@tonic-gate 			return (EINVAL);
25047c478bd9Sstevel@tonic-gate 		low = high = major;
25057c478bd9Sstevel@tonic-gate 	}
25067c478bd9Sstevel@tonic-gate 
25077c478bd9Sstevel@tonic-gate 	for (m = low; m <= high; m++) {
25087c478bd9Sstevel@tonic-gate 		struct devnames *dnp = &devnamesp[m];
25097c478bd9Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
2510c9cc1492SJerry Gilliam 		dnp->dn_flags &= ~(DN_DRIVER_HELD|DN_DRIVER_INACTIVE);
25117c478bd9Sstevel@tonic-gate 		(void) impl_make_parlist(m);
25127c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
25137c478bd9Sstevel@tonic-gate 	}
25147c478bd9Sstevel@tonic-gate 
25157c478bd9Sstevel@tonic-gate 	if (modrootloaded) {
25167c478bd9Sstevel@tonic-gate 		ddi_walk_devs(ddi_root_node(), reset_nexus_flags,
25177c478bd9Sstevel@tonic-gate 		    (void *)(uintptr_t)major);
25187c478bd9Sstevel@tonic-gate 	}
25197c478bd9Sstevel@tonic-gate 
25207c478bd9Sstevel@tonic-gate 	/* build dn_list from old entries in path_to_inst */
25217c478bd9Sstevel@tonic-gate 	e_ddi_unorphan_instance_nos();
25227c478bd9Sstevel@tonic-gate 	return (0);
25237c478bd9Sstevel@tonic-gate }
25247c478bd9Sstevel@tonic-gate 
25257c478bd9Sstevel@tonic-gate /*
25267c478bd9Sstevel@tonic-gate  * Unload a specific driver.conf.
25277c478bd9Sstevel@tonic-gate  * Don't support unload all because it doesn't make any sense
25287c478bd9Sstevel@tonic-gate  */
25297c478bd9Sstevel@tonic-gate int
25307c478bd9Sstevel@tonic-gate i_ddi_unload_drvconf(major_t major)
25317c478bd9Sstevel@tonic-gate {
25327c478bd9Sstevel@tonic-gate 	int error;
25337c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
25347c478bd9Sstevel@tonic-gate 
25357c478bd9Sstevel@tonic-gate 	if (major >= devcnt)
25367c478bd9Sstevel@tonic-gate 		return (EINVAL);
25377c478bd9Sstevel@tonic-gate 
25387c478bd9Sstevel@tonic-gate 	/*
25397c478bd9Sstevel@tonic-gate 	 * Take the per-driver lock while unloading driver.conf
25407c478bd9Sstevel@tonic-gate 	 */
25417c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
25427c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
25437c478bd9Sstevel@tonic-gate 	error = impl_free_parlist(major);
25447c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
25457c478bd9Sstevel@tonic-gate 	return (error);
25467c478bd9Sstevel@tonic-gate }
25477c478bd9Sstevel@tonic-gate 
25487c478bd9Sstevel@tonic-gate /*
25497c478bd9Sstevel@tonic-gate  * Merge a .conf node. This is called by nexus drivers to augment
25507c478bd9Sstevel@tonic-gate  * hw node with properties specified in driver.conf file. This function
25517c478bd9Sstevel@tonic-gate  * takes a callback routine to name nexus children.
25527c478bd9Sstevel@tonic-gate  * The parent node must be held busy.
25537c478bd9Sstevel@tonic-gate  *
25547c478bd9Sstevel@tonic-gate  * It returns DDI_SUCCESS if the node is merged and DDI_FAILURE otherwise.
25557c478bd9Sstevel@tonic-gate  */
25567c478bd9Sstevel@tonic-gate int
25574c06356bSdh142964 ndi_merge_node(dev_info_t *dip, int (*make_ua)(dev_info_t *, char *, int))
25587c478bd9Sstevel@tonic-gate {
25597c478bd9Sstevel@tonic-gate 	dev_info_t *hwdip;
25607c478bd9Sstevel@tonic-gate 
25617c478bd9Sstevel@tonic-gate 	ASSERT(ndi_dev_is_persistent_node(dip) == 0);
25627c478bd9Sstevel@tonic-gate 	ASSERT(ddi_get_name_addr(dip) != NULL);
25637c478bd9Sstevel@tonic-gate 
25644c06356bSdh142964 	hwdip = ndi_devi_findchild_by_callback(ddi_get_parent(dip),
25654c06356bSdh142964 	    ddi_binding_name(dip), ddi_get_name_addr(dip), make_ua);
25667c478bd9Sstevel@tonic-gate 
25677c478bd9Sstevel@tonic-gate 	/*
25687c478bd9Sstevel@tonic-gate 	 * Look for the hardware node that is the target of the merge;
25697c478bd9Sstevel@tonic-gate 	 * return failure if not found.
25707c478bd9Sstevel@tonic-gate 	 */
25717c478bd9Sstevel@tonic-gate 	if ((hwdip == NULL) || (hwdip == dip)) {
25727c478bd9Sstevel@tonic-gate 		char *buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
25737c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_WARN, "No HW node to merge conf node %s",
25747c478bd9Sstevel@tonic-gate 		    ddi_deviname(dip, buf)));
25757c478bd9Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
25767c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
25777c478bd9Sstevel@tonic-gate 	}
25787c478bd9Sstevel@tonic-gate 
25797c478bd9Sstevel@tonic-gate 	/*
25807c478bd9Sstevel@tonic-gate 	 * Make sure the hardware node is uninitialized and has no property.
25817c478bd9Sstevel@tonic-gate 	 * This may not be the case if new .conf files are load after some
25827c478bd9Sstevel@tonic-gate 	 * hardware nodes have already been initialized and attached.
25837c478bd9Sstevel@tonic-gate 	 *
25847c478bd9Sstevel@tonic-gate 	 * N.B. We return success here because the node was *intended*
25857c478bd9Sstevel@tonic-gate 	 *	to be a merge node because there is a hw node with the name.
25867c478bd9Sstevel@tonic-gate 	 */
25877c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(hwdip)->devi_lock);
25887c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(hwdip) == 0) {
25897c478bd9Sstevel@tonic-gate 		char *buf;
25907c478bd9Sstevel@tonic-gate 		mutex_exit(&DEVI(hwdip)->devi_lock);
25917c478bd9Sstevel@tonic-gate 
25927c478bd9Sstevel@tonic-gate 		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
25937c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE, "Duplicate .conf node %s",
25947c478bd9Sstevel@tonic-gate 		    ddi_deviname(dip, buf)));
25957c478bd9Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
25967c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
25977c478bd9Sstevel@tonic-gate 	}
25987c478bd9Sstevel@tonic-gate 
25997c478bd9Sstevel@tonic-gate 	/*
26007c478bd9Sstevel@tonic-gate 	 * If it is possible that the hardware has already been touched
26017c478bd9Sstevel@tonic-gate 	 * then don't merge.
26027c478bd9Sstevel@tonic-gate 	 */
26037c478bd9Sstevel@tonic-gate 	if (i_ddi_node_state(hwdip) >= DS_INITIALIZED ||
26047c478bd9Sstevel@tonic-gate 	    (DEVI(hwdip)->devi_sys_prop_ptr != NULL) ||
26057c478bd9Sstevel@tonic-gate 	    (DEVI(hwdip)->devi_drv_prop_ptr != NULL)) {
26067c478bd9Sstevel@tonic-gate 		char *buf;
26077c478bd9Sstevel@tonic-gate 		mutex_exit(&DEVI(hwdip)->devi_lock);
26087c478bd9Sstevel@tonic-gate 
26097c478bd9Sstevel@tonic-gate 		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
26107c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE,
26117c478bd9Sstevel@tonic-gate 		    "!Cannot merge .conf node %s with hw node %p "
26127c478bd9Sstevel@tonic-gate 		    "-- not in proper state",
26137c478bd9Sstevel@tonic-gate 		    ddi_deviname(dip, buf), (void *)hwdip));
26147c478bd9Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
26157c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
26167c478bd9Sstevel@tonic-gate 	}
26177c478bd9Sstevel@tonic-gate 
26187c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
26197c478bd9Sstevel@tonic-gate 	DEVI(hwdip)->devi_sys_prop_ptr = DEVI(dip)->devi_sys_prop_ptr;
26207c478bd9Sstevel@tonic-gate 	DEVI(hwdip)->devi_drv_prop_ptr = DEVI(dip)->devi_drv_prop_ptr;
26217c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_sys_prop_ptr = NULL;
26227c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_drv_prop_ptr = NULL;
26237c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
26247c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(hwdip)->devi_lock);
26257c478bd9Sstevel@tonic-gate 
26267c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
26277c478bd9Sstevel@tonic-gate }
26287c478bd9Sstevel@tonic-gate 
26297c478bd9Sstevel@tonic-gate /*
26307c478bd9Sstevel@tonic-gate  * Merge a "wildcard" .conf node. This is called by nexus drivers to
26317c478bd9Sstevel@tonic-gate  * augment a set of hw node with properties specified in driver.conf file.
26327c478bd9Sstevel@tonic-gate  * The parent node must be held busy.
26337c478bd9Sstevel@tonic-gate  *
26347c478bd9Sstevel@tonic-gate  * There is no failure mode, since the nexus may or may not have child
26357c478bd9Sstevel@tonic-gate  * node bound the driver specified by the wildcard node.
26367c478bd9Sstevel@tonic-gate  */
26377c478bd9Sstevel@tonic-gate void
26387c478bd9Sstevel@tonic-gate ndi_merge_wildcard_node(dev_info_t *dip)
26397c478bd9Sstevel@tonic-gate {
26407c478bd9Sstevel@tonic-gate 	dev_info_t *hwdip;
26417c478bd9Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
26427c478bd9Sstevel@tonic-gate 	major_t major = ddi_driver_major(dip);
26437c478bd9Sstevel@tonic-gate 
26447c478bd9Sstevel@tonic-gate 	/* never attempt to merge a hw node */
26457c478bd9Sstevel@tonic-gate 	ASSERT(ndi_dev_is_persistent_node(dip) == 0);
26467c478bd9Sstevel@tonic-gate 	/* must be bound to a driver major number */
2647a204de77Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
26487c478bd9Sstevel@tonic-gate 
26497c478bd9Sstevel@tonic-gate 	/*
26507c478bd9Sstevel@tonic-gate 	 * Walk the child list to find all nodes bound to major
26517c478bd9Sstevel@tonic-gate 	 * and copy properties.
26527c478bd9Sstevel@tonic-gate 	 */
26537c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
2654b9ccdc5aScth 	ASSERT(DEVI_BUSY_OWNED(pdip));
26557c478bd9Sstevel@tonic-gate 	for (hwdip = ddi_get_child(pdip); hwdip;
26567c478bd9Sstevel@tonic-gate 	    hwdip = ddi_get_next_sibling(hwdip)) {
26577c478bd9Sstevel@tonic-gate 		/*
26587c478bd9Sstevel@tonic-gate 		 * Skip nodes not bound to same driver
26597c478bd9Sstevel@tonic-gate 		 */
26607c478bd9Sstevel@tonic-gate 		if (ddi_driver_major(hwdip) != major)
26617c478bd9Sstevel@tonic-gate 			continue;
26627c478bd9Sstevel@tonic-gate 
26637c478bd9Sstevel@tonic-gate 		/*
26647c478bd9Sstevel@tonic-gate 		 * Skip .conf nodes
26657c478bd9Sstevel@tonic-gate 		 */
26667c478bd9Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(hwdip) == 0)
26677c478bd9Sstevel@tonic-gate 			continue;
26687c478bd9Sstevel@tonic-gate 
26697c478bd9Sstevel@tonic-gate 		/*
26707c478bd9Sstevel@tonic-gate 		 * Make sure the node is uninitialized and has no property.
26717c478bd9Sstevel@tonic-gate 		 */
26727c478bd9Sstevel@tonic-gate 		mutex_enter(&DEVI(hwdip)->devi_lock);
26737c478bd9Sstevel@tonic-gate 		if (i_ddi_node_state(hwdip) >= DS_INITIALIZED ||
26747c478bd9Sstevel@tonic-gate 		    (DEVI(hwdip)->devi_sys_prop_ptr != NULL) ||
26757c478bd9Sstevel@tonic-gate 		    (DEVI(hwdip)->devi_drv_prop_ptr != NULL)) {
26767c478bd9Sstevel@tonic-gate 			mutex_exit(&DEVI(hwdip)->devi_lock);
26777c478bd9Sstevel@tonic-gate 			NDI_CONFIG_DEBUG((CE_NOTE, "HW node %p state not "
26787c478bd9Sstevel@tonic-gate 			    "suitable for merging wildcard conf node %s",
26797c478bd9Sstevel@tonic-gate 			    (void *)hwdip, ddi_node_name(dip)));
26807c478bd9Sstevel@tonic-gate 			continue;
26817c478bd9Sstevel@tonic-gate 		}
26827c478bd9Sstevel@tonic-gate 
26837c478bd9Sstevel@tonic-gate 		DEVI(hwdip)->devi_sys_prop_ptr =
26847c478bd9Sstevel@tonic-gate 		    i_ddi_prop_list_dup(DEVI(dip)->devi_sys_prop_ptr, KM_SLEEP);
26857c478bd9Sstevel@tonic-gate 		DEVI(hwdip)->devi_drv_prop_ptr =
26867c478bd9Sstevel@tonic-gate 		    i_ddi_prop_list_dup(DEVI(dip)->devi_drv_prop_ptr, KM_SLEEP);
26877c478bd9Sstevel@tonic-gate 		mutex_exit(&DEVI(hwdip)->devi_lock);
26887c478bd9Sstevel@tonic-gate 	}
26897c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
26907c478bd9Sstevel@tonic-gate }
26917c478bd9Sstevel@tonic-gate 
26927c478bd9Sstevel@tonic-gate /*
26937c478bd9Sstevel@tonic-gate  * Return the major number based on the compatible property. This interface
26947c478bd9Sstevel@tonic-gate  * may be used in situations where we are trying to detect if a better driver
26957c478bd9Sstevel@tonic-gate  * now exists for a device, so it must use the 'compatible' property.  If
26967c478bd9Sstevel@tonic-gate  * a non-NULL formp is specified and the binding was based on compatible then
26977c478bd9Sstevel@tonic-gate  * return the pointer to the form used in *formp.
26987c478bd9Sstevel@tonic-gate  */
26997c478bd9Sstevel@tonic-gate major_t
27007c478bd9Sstevel@tonic-gate ddi_compatible_driver_major(dev_info_t *dip, char **formp)
27017c478bd9Sstevel@tonic-gate {
27027c478bd9Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
27037c478bd9Sstevel@tonic-gate 	void		*compat;
27047c478bd9Sstevel@tonic-gate 	size_t		len;
27057c478bd9Sstevel@tonic-gate 	char		*p = NULL;
2706a204de77Scth 	major_t		major = DDI_MAJOR_T_NONE;
27077c478bd9Sstevel@tonic-gate 
27087c478bd9Sstevel@tonic-gate 	if (formp)
27097c478bd9Sstevel@tonic-gate 		*formp = NULL;
27107c478bd9Sstevel@tonic-gate 
2711fc256490SJason Beloro 	if (ddi_prop_exists(DDI_DEV_T_NONE, dip, DDI_PROP_DONTPASS,
2712fc256490SJason Beloro 	    "ddi-assigned")) {
2713fc256490SJason Beloro 		major = ddi_name_to_major("nulldriver");
2714fc256490SJason Beloro 		return (major);
2715fc256490SJason Beloro 	}
2716fc256490SJason Beloro 
2717f4da9be0Scth 	/*
2718f4da9be0Scth 	 * Highest precedence binding is a path-oriented alias. Since this
2719f4da9be0Scth 	 * requires a 'path', this type of binding occurs via more obtuse
2720f4da9be0Scth 	 * 'rebind'. The need for a path-oriented alias 'rebind' is detected
2721f4da9be0Scth 	 * after a successful DDI_CTLOPS_INITCHILD to another driver: this is
2722f4da9be0Scth 	 * is the first point at which the unit-address (or instance) of the
2723f4da9be0Scth 	 * last component of the path is available (even though the path is
2724f4da9be0Scth 	 * bound to the wrong driver at this point).
2725f4da9be0Scth 	 */
2726f4da9be0Scth 	if (devi->devi_flags & DEVI_REBIND) {
2727f4da9be0Scth 		p = devi->devi_rebinding_name;
2728f4da9be0Scth 		major = ddi_name_to_major(p);
2729c9cc1492SJerry Gilliam 		if (driver_installed(major)) {
2730f4da9be0Scth 			if (formp)
2731f4da9be0Scth 				*formp = p;
2732f4da9be0Scth 			return (major);
2733f4da9be0Scth 		}
2734f4da9be0Scth 
2735f4da9be0Scth 		/*
2736f4da9be0Scth 		 * If for some reason devi_rebinding_name no longer resolves
2737f4da9be0Scth 		 * to a proper driver then clear DEVI_REBIND.
2738f4da9be0Scth 		 */
2739f4da9be0Scth 		mutex_enter(&devi->devi_lock);
2740f4da9be0Scth 		devi->devi_flags &= ~DEVI_REBIND;
2741f4da9be0Scth 		mutex_exit(&devi->devi_lock);
2742f4da9be0Scth 	}
2743f4da9be0Scth 
27447c478bd9Sstevel@tonic-gate 	/* look up compatible property */
27457c478bd9Sstevel@tonic-gate 	(void) lookup_compatible(dip, KM_SLEEP);
27467c478bd9Sstevel@tonic-gate 	compat = (void *)(devi->devi_compat_names);
27477c478bd9Sstevel@tonic-gate 	len = devi->devi_compat_length;
27487c478bd9Sstevel@tonic-gate 
27497c478bd9Sstevel@tonic-gate 	/* find the highest precedence compatible form with a driver binding */
27507c478bd9Sstevel@tonic-gate 	while ((p = prom_decode_composite_string(compat, len, p)) != NULL) {
27517c478bd9Sstevel@tonic-gate 		major = ddi_name_to_major(p);
2752c9cc1492SJerry Gilliam 		if (driver_installed(major)) {
27537c478bd9Sstevel@tonic-gate 			if (formp)
27547c478bd9Sstevel@tonic-gate 				*formp = p;
27557c478bd9Sstevel@tonic-gate 			return (major);
27567c478bd9Sstevel@tonic-gate 		}
27577c478bd9Sstevel@tonic-gate 	}
27587c478bd9Sstevel@tonic-gate 
27597c478bd9Sstevel@tonic-gate 	/*
27607c478bd9Sstevel@tonic-gate 	 * none of the compatible forms have a driver binding, see if
27617c478bd9Sstevel@tonic-gate 	 * the node name has a driver binding.
27627c478bd9Sstevel@tonic-gate 	 */
27637c478bd9Sstevel@tonic-gate 	major = ddi_name_to_major(ddi_node_name(dip));
2764c9cc1492SJerry Gilliam 	if (driver_installed(major))
27657c478bd9Sstevel@tonic-gate 		return (major);
27667c478bd9Sstevel@tonic-gate 
27677c478bd9Sstevel@tonic-gate 	/* no driver */
2768a204de77Scth 	return (DDI_MAJOR_T_NONE);
27697c478bd9Sstevel@tonic-gate }
27707c478bd9Sstevel@tonic-gate 
27717c478bd9Sstevel@tonic-gate /*
27727c478bd9Sstevel@tonic-gate  * Static help functions
27737c478bd9Sstevel@tonic-gate  */
27747c478bd9Sstevel@tonic-gate 
27757c478bd9Sstevel@tonic-gate /*
27767c478bd9Sstevel@tonic-gate  * lookup the "compatible" property and cache it's contents in the
27777c478bd9Sstevel@tonic-gate  * device node.
27787c478bd9Sstevel@tonic-gate  */
27797c478bd9Sstevel@tonic-gate static int
27807c478bd9Sstevel@tonic-gate lookup_compatible(dev_info_t *dip, uint_t flag)
27817c478bd9Sstevel@tonic-gate {
27827c478bd9Sstevel@tonic-gate 	int rv;
27837c478bd9Sstevel@tonic-gate 	int prop_flags;
27847c478bd9Sstevel@tonic-gate 	uint_t ncompatstrs;
27857c478bd9Sstevel@tonic-gate 	char **compatstrpp;
27867c478bd9Sstevel@tonic-gate 	char *di_compat_strp;
27877c478bd9Sstevel@tonic-gate 	size_t di_compat_strlen;
27887c478bd9Sstevel@tonic-gate 
27897c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_compat_names) {
27907c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
27917c478bd9Sstevel@tonic-gate 	}
27927c478bd9Sstevel@tonic-gate 
27937c478bd9Sstevel@tonic-gate 	prop_flags = DDI_PROP_TYPE_STRING | DDI_PROP_DONTPASS;
27947c478bd9Sstevel@tonic-gate 
27957c478bd9Sstevel@tonic-gate 	if (flag & KM_NOSLEEP) {
27967c478bd9Sstevel@tonic-gate 		prop_flags |= DDI_PROP_DONTSLEEP;
27977c478bd9Sstevel@tonic-gate 	}
27987c478bd9Sstevel@tonic-gate 
27997c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_prom_node(dip) == 0) {
28007c478bd9Sstevel@tonic-gate 		prop_flags |= DDI_PROP_NOTPROM;
28017c478bd9Sstevel@tonic-gate 	}
28027c478bd9Sstevel@tonic-gate 
28037c478bd9Sstevel@tonic-gate 	rv = ddi_prop_lookup_common(DDI_DEV_T_ANY, dip, prop_flags,
28047c478bd9Sstevel@tonic-gate 	    "compatible", &compatstrpp, &ncompatstrs,
28057c478bd9Sstevel@tonic-gate 	    ddi_prop_fm_decode_strings);
28067c478bd9Sstevel@tonic-gate 
28077c478bd9Sstevel@tonic-gate 	if (rv == DDI_PROP_NOT_FOUND) {
28087c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
28097c478bd9Sstevel@tonic-gate 	}
28107c478bd9Sstevel@tonic-gate 
28117c478bd9Sstevel@tonic-gate 	if (rv != DDI_PROP_SUCCESS) {
28127c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
28137c478bd9Sstevel@tonic-gate 	}
28147c478bd9Sstevel@tonic-gate 
28157c478bd9Sstevel@tonic-gate 	/*
28167c478bd9Sstevel@tonic-gate 	 * encode the compatible property data in the dev_info node
28177c478bd9Sstevel@tonic-gate 	 */
28187c478bd9Sstevel@tonic-gate 	rv = DDI_SUCCESS;
28197c478bd9Sstevel@tonic-gate 	if (ncompatstrs != 0) {
28207c478bd9Sstevel@tonic-gate 		di_compat_strp = encode_composite_string(compatstrpp,
28217c478bd9Sstevel@tonic-gate 		    ncompatstrs, &di_compat_strlen, flag);
28227c478bd9Sstevel@tonic-gate 		if (di_compat_strp != NULL) {
28237c478bd9Sstevel@tonic-gate 			DEVI(dip)->devi_compat_names = di_compat_strp;
28247c478bd9Sstevel@tonic-gate 			DEVI(dip)->devi_compat_length = di_compat_strlen;
28257c478bd9Sstevel@tonic-gate 		} else {
28267c478bd9Sstevel@tonic-gate 			rv = DDI_FAILURE;
28277c478bd9Sstevel@tonic-gate 		}
28287c478bd9Sstevel@tonic-gate 	}
28297c478bd9Sstevel@tonic-gate 	ddi_prop_free(compatstrpp);
28307c478bd9Sstevel@tonic-gate 	return (rv);
28317c478bd9Sstevel@tonic-gate }
28327c478bd9Sstevel@tonic-gate 
28337c478bd9Sstevel@tonic-gate /*
28347c478bd9Sstevel@tonic-gate  * Create a composite string from a list of strings.
28357c478bd9Sstevel@tonic-gate  *
28367c478bd9Sstevel@tonic-gate  * A composite string consists of a single buffer containing one
28377c478bd9Sstevel@tonic-gate  * or more NULL terminated strings.
28387c478bd9Sstevel@tonic-gate  */
28397c478bd9Sstevel@tonic-gate static char *
28407c478bd9Sstevel@tonic-gate encode_composite_string(char **strings, uint_t nstrings, size_t *retsz,
28417c478bd9Sstevel@tonic-gate     uint_t flag)
28427c478bd9Sstevel@tonic-gate {
28437c478bd9Sstevel@tonic-gate 	uint_t index;
28447c478bd9Sstevel@tonic-gate 	char  **strpp;
28457c478bd9Sstevel@tonic-gate 	uint_t slen;
28467c478bd9Sstevel@tonic-gate 	size_t cbuf_sz = 0;
28477c478bd9Sstevel@tonic-gate 	char *cbuf_p;
28487c478bd9Sstevel@tonic-gate 	char *cbuf_ip;
28497c478bd9Sstevel@tonic-gate 
28507c478bd9Sstevel@tonic-gate 	if (strings == NULL || nstrings == 0 || retsz == NULL) {
28517c478bd9Sstevel@tonic-gate 		return (NULL);
28527c478bd9Sstevel@tonic-gate 	}
28537c478bd9Sstevel@tonic-gate 
28547c478bd9Sstevel@tonic-gate 	for (index = 0, strpp = strings; index < nstrings; index++)
28557c478bd9Sstevel@tonic-gate 		cbuf_sz += strlen(*(strpp++)) + 1;
28567c478bd9Sstevel@tonic-gate 
28577c478bd9Sstevel@tonic-gate 	if ((cbuf_p = kmem_alloc(cbuf_sz, flag)) == NULL) {
28587c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE,
28597c478bd9Sstevel@tonic-gate 		    "?failed to allocate device node compatstr");
28607c478bd9Sstevel@tonic-gate 		return (NULL);
28617c478bd9Sstevel@tonic-gate 	}
28627c478bd9Sstevel@tonic-gate 
28637c478bd9Sstevel@tonic-gate 	cbuf_ip = cbuf_p;
28647c478bd9Sstevel@tonic-gate 	for (index = 0, strpp = strings; index < nstrings; index++) {
28657c478bd9Sstevel@tonic-gate 		slen = strlen(*strpp);
28667c478bd9Sstevel@tonic-gate 		bcopy(*(strpp++), cbuf_ip, slen);
28677c478bd9Sstevel@tonic-gate 		cbuf_ip += slen;
28687c478bd9Sstevel@tonic-gate 		*(cbuf_ip++) = '\0';
28697c478bd9Sstevel@tonic-gate 	}
28707c478bd9Sstevel@tonic-gate 
28717c478bd9Sstevel@tonic-gate 	*retsz = cbuf_sz;
28727c478bd9Sstevel@tonic-gate 	return (cbuf_p);
28737c478bd9Sstevel@tonic-gate }
28747c478bd9Sstevel@tonic-gate 
28757c478bd9Sstevel@tonic-gate static void
28767c478bd9Sstevel@tonic-gate link_to_driver_list(dev_info_t *dip)
28777c478bd9Sstevel@tonic-gate {
28787c478bd9Sstevel@tonic-gate 	major_t major = DEVI(dip)->devi_major;
28797c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
28807c478bd9Sstevel@tonic-gate 
2881a204de77Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
28827c478bd9Sstevel@tonic-gate 
28837c478bd9Sstevel@tonic-gate 	/*
28847c478bd9Sstevel@tonic-gate 	 * Remove from orphan list
28857c478bd9Sstevel@tonic-gate 	 */
28867c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
28877c478bd9Sstevel@tonic-gate 		dnp = &orphanlist;
28887c478bd9Sstevel@tonic-gate 		remove_from_dn_list(dnp, dip);
28897c478bd9Sstevel@tonic-gate 	}
28907c478bd9Sstevel@tonic-gate 
28917c478bd9Sstevel@tonic-gate 	/*
28927c478bd9Sstevel@tonic-gate 	 * Add to per driver list
28937c478bd9Sstevel@tonic-gate 	 */
28947c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
28957c478bd9Sstevel@tonic-gate 	add_to_dn_list(dnp, dip);
28967c478bd9Sstevel@tonic-gate }
28977c478bd9Sstevel@tonic-gate 
28987c478bd9Sstevel@tonic-gate static void
28997c478bd9Sstevel@tonic-gate unlink_from_driver_list(dev_info_t *dip)
29007c478bd9Sstevel@tonic-gate {
29017c478bd9Sstevel@tonic-gate 	major_t major = DEVI(dip)->devi_major;
29027c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
29037c478bd9Sstevel@tonic-gate 
2904a204de77Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
29057c478bd9Sstevel@tonic-gate 
29067c478bd9Sstevel@tonic-gate 	/*
29077c478bd9Sstevel@tonic-gate 	 * Remove from per-driver list
29087c478bd9Sstevel@tonic-gate 	 */
29097c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
29107c478bd9Sstevel@tonic-gate 	remove_from_dn_list(dnp, dip);
29117c478bd9Sstevel@tonic-gate 
29127c478bd9Sstevel@tonic-gate 	/*
29137c478bd9Sstevel@tonic-gate 	 * Add to orphan list
29147c478bd9Sstevel@tonic-gate 	 */
29157c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
29167c478bd9Sstevel@tonic-gate 		dnp = &orphanlist;
29177c478bd9Sstevel@tonic-gate 		add_to_dn_list(dnp, dip);
29187c478bd9Sstevel@tonic-gate 	}
29197c478bd9Sstevel@tonic-gate }
29207c478bd9Sstevel@tonic-gate 
29217c478bd9Sstevel@tonic-gate /*
29227c478bd9Sstevel@tonic-gate  * scan the per-driver list looking for dev_info "dip"
29237c478bd9Sstevel@tonic-gate  */
29247c478bd9Sstevel@tonic-gate static dev_info_t *
29257c478bd9Sstevel@tonic-gate in_dn_list(struct devnames *dnp, dev_info_t *dip)
29267c478bd9Sstevel@tonic-gate {
29277c478bd9Sstevel@tonic-gate 	struct dev_info *idevi;
29287c478bd9Sstevel@tonic-gate 
29297c478bd9Sstevel@tonic-gate 	if ((idevi = DEVI(dnp->dn_head)) == NULL)
29307c478bd9Sstevel@tonic-gate 		return (NULL);
29317c478bd9Sstevel@tonic-gate 
29327c478bd9Sstevel@tonic-gate 	while (idevi) {
29337c478bd9Sstevel@tonic-gate 		if (idevi == DEVI(dip))
29347c478bd9Sstevel@tonic-gate 			return (dip);
29357c478bd9Sstevel@tonic-gate 		idevi = idevi->devi_next;
29367c478bd9Sstevel@tonic-gate 	}
29377c478bd9Sstevel@tonic-gate 	return (NULL);
29387c478bd9Sstevel@tonic-gate }
29397c478bd9Sstevel@tonic-gate 
29407c478bd9Sstevel@tonic-gate /*
29417c478bd9Sstevel@tonic-gate  * insert devinfo node 'dip' into the per-driver instance list
29427c478bd9Sstevel@tonic-gate  * headed by 'dnp'
29437c478bd9Sstevel@tonic-gate  *
29447c478bd9Sstevel@tonic-gate  * Nodes on the per-driver list are ordered: HW - SID - PSEUDO.  The order is
29457c478bd9Sstevel@tonic-gate  * required for merging of .conf file data to work properly.
29467c478bd9Sstevel@tonic-gate  */
29477c478bd9Sstevel@tonic-gate static void
29487c478bd9Sstevel@tonic-gate add_to_ordered_dn_list(struct devnames *dnp, dev_info_t *dip)
29497c478bd9Sstevel@tonic-gate {
29507c478bd9Sstevel@tonic-gate 	dev_info_t **dipp;
29517c478bd9Sstevel@tonic-gate 
29527c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&(dnp->dn_lock)));
29537c478bd9Sstevel@tonic-gate 
29547c478bd9Sstevel@tonic-gate 	dipp = &dnp->dn_head;
29557c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_prom_node(dip)) {
29567c478bd9Sstevel@tonic-gate 		/*
29577c478bd9Sstevel@tonic-gate 		 * Find the first non-prom node or end of list
29587c478bd9Sstevel@tonic-gate 		 */
29597c478bd9Sstevel@tonic-gate 		while (*dipp && (ndi_dev_is_prom_node(*dipp) != 0)) {
29607c478bd9Sstevel@tonic-gate 			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
29617c478bd9Sstevel@tonic-gate 		}
29627c478bd9Sstevel@tonic-gate 	} else if (ndi_dev_is_persistent_node(dip)) {
29637c478bd9Sstevel@tonic-gate 		/*
29647c478bd9Sstevel@tonic-gate 		 * Find the first non-persistent node
29657c478bd9Sstevel@tonic-gate 		 */
29667c478bd9Sstevel@tonic-gate 		while (*dipp && (ndi_dev_is_persistent_node(*dipp) != 0)) {
29677c478bd9Sstevel@tonic-gate 			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
29687c478bd9Sstevel@tonic-gate 		}
29697c478bd9Sstevel@tonic-gate 	} else {
29707c478bd9Sstevel@tonic-gate 		/*
29717c478bd9Sstevel@tonic-gate 		 * Find the end of the list
29727c478bd9Sstevel@tonic-gate 		 */
29737c478bd9Sstevel@tonic-gate 		while (*dipp) {
29747c478bd9Sstevel@tonic-gate 			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
29757c478bd9Sstevel@tonic-gate 		}
29767c478bd9Sstevel@tonic-gate 	}
29777c478bd9Sstevel@tonic-gate 
29787c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_next = DEVI(*dipp);
29797c478bd9Sstevel@tonic-gate 	*dipp = dip;
29807c478bd9Sstevel@tonic-gate }
29817c478bd9Sstevel@tonic-gate 
29827c478bd9Sstevel@tonic-gate /*
29837c478bd9Sstevel@tonic-gate  * add a list of device nodes to the device node list in the
29847c478bd9Sstevel@tonic-gate  * devnames structure
29857c478bd9Sstevel@tonic-gate  */
29867c478bd9Sstevel@tonic-gate static void
29877c478bd9Sstevel@tonic-gate add_to_dn_list(struct devnames *dnp, dev_info_t *dip)
29887c478bd9Sstevel@tonic-gate {
29897c478bd9Sstevel@tonic-gate 	/*
29907c478bd9Sstevel@tonic-gate 	 * Look to see if node already exists
29917c478bd9Sstevel@tonic-gate 	 */
29927c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&(dnp->dn_lock));
29937c478bd9Sstevel@tonic-gate 	if (in_dn_list(dnp, dip)) {
29947c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "add_to_dn_list: node %s already in list",
29957c478bd9Sstevel@tonic-gate 		    DEVI(dip)->devi_node_name);
29967c478bd9Sstevel@tonic-gate 	} else {
29977c478bd9Sstevel@tonic-gate 		add_to_ordered_dn_list(dnp, dip);
29987c478bd9Sstevel@tonic-gate 	}
29997c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
30007c478bd9Sstevel@tonic-gate }
30017c478bd9Sstevel@tonic-gate 
30027c478bd9Sstevel@tonic-gate static void
30037c478bd9Sstevel@tonic-gate remove_from_dn_list(struct devnames *dnp, dev_info_t *dip)
30047c478bd9Sstevel@tonic-gate {
30057c478bd9Sstevel@tonic-gate 	dev_info_t **plist;
30067c478bd9Sstevel@tonic-gate 
30077c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&(dnp->dn_lock));
30087c478bd9Sstevel@tonic-gate 
30097c478bd9Sstevel@tonic-gate 	plist = (dev_info_t **)&dnp->dn_head;
30107c478bd9Sstevel@tonic-gate 	while (*plist && (*plist != dip)) {
30117c478bd9Sstevel@tonic-gate 		plist = (dev_info_t **)&DEVI(*plist)->devi_next;
30127c478bd9Sstevel@tonic-gate 	}
30137c478bd9Sstevel@tonic-gate 
30147c478bd9Sstevel@tonic-gate 	if (*plist != NULL) {
30157c478bd9Sstevel@tonic-gate 		ASSERT(*plist == dip);
30167c478bd9Sstevel@tonic-gate 		*plist = (dev_info_t *)(DEVI(dip)->devi_next);
30177c478bd9Sstevel@tonic-gate 		DEVI(dip)->devi_next = NULL;
30187c478bd9Sstevel@tonic-gate 	} else {
30197c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE,
30207c478bd9Sstevel@tonic-gate 		    "remove_from_dn_list: node %s not found in list",
30217c478bd9Sstevel@tonic-gate 		    DEVI(dip)->devi_node_name));
30227c478bd9Sstevel@tonic-gate 	}
30237c478bd9Sstevel@tonic-gate 
30247c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
30257c478bd9Sstevel@tonic-gate }
30267c478bd9Sstevel@tonic-gate 
30277c478bd9Sstevel@tonic-gate /*
30287c478bd9Sstevel@tonic-gate  * Add and remove reference driver global property list
30297c478bd9Sstevel@tonic-gate  */
30307c478bd9Sstevel@tonic-gate static void
30317c478bd9Sstevel@tonic-gate add_global_props(dev_info_t *dip)
30327c478bd9Sstevel@tonic-gate {
30337c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
30347c478bd9Sstevel@tonic-gate 	ddi_prop_list_t *plist;
30357c478bd9Sstevel@tonic-gate 
30367c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_global_prop_list == NULL);
3037a204de77Scth 	ASSERT(DEVI(dip)->devi_major != DDI_MAJOR_T_NONE);
30387c478bd9Sstevel@tonic-gate 
30397c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[DEVI(dip)->devi_major];
30407c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
30417c478bd9Sstevel@tonic-gate 	plist = dnp->dn_global_prop_ptr;
30427c478bd9Sstevel@tonic-gate 	if (plist == NULL) {
30437c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
30447c478bd9Sstevel@tonic-gate 		return;
30457c478bd9Sstevel@tonic-gate 	}
30467c478bd9Sstevel@tonic-gate 	i_ddi_prop_list_hold(plist, dnp);
30477c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
30487c478bd9Sstevel@tonic-gate 
30497c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
30507c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_global_prop_list = plist;
30517c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
30527c478bd9Sstevel@tonic-gate }
30537c478bd9Sstevel@tonic-gate 
30547c478bd9Sstevel@tonic-gate static void
30557c478bd9Sstevel@tonic-gate remove_global_props(dev_info_t *dip)
30567c478bd9Sstevel@tonic-gate {
30577c478bd9Sstevel@tonic-gate 	ddi_prop_list_t *proplist;
30587c478bd9Sstevel@tonic-gate 
30597c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
30607c478bd9Sstevel@tonic-gate 	proplist = DEVI(dip)->devi_global_prop_list;
30617c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_global_prop_list = NULL;
30627c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
30637c478bd9Sstevel@tonic-gate 
30647c478bd9Sstevel@tonic-gate 	if (proplist) {
30657c478bd9Sstevel@tonic-gate 		major_t major;
30667c478bd9Sstevel@tonic-gate 		struct devnames *dnp;
30677c478bd9Sstevel@tonic-gate 
30687c478bd9Sstevel@tonic-gate 		major = ddi_driver_major(dip);
3069a204de77Scth 		ASSERT(major != DDI_MAJOR_T_NONE);
30707c478bd9Sstevel@tonic-gate 		dnp = &devnamesp[major];
30717c478bd9Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
30727c478bd9Sstevel@tonic-gate 		i_ddi_prop_list_rele(proplist, dnp);
30737c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
30747c478bd9Sstevel@tonic-gate 	}
30757c478bd9Sstevel@tonic-gate }
30767c478bd9Sstevel@tonic-gate 
30777c478bd9Sstevel@tonic-gate #ifdef DEBUG
30787c478bd9Sstevel@tonic-gate /*
30797c478bd9Sstevel@tonic-gate  * Set this variable to '0' to disable the optimization,
30807c478bd9Sstevel@tonic-gate  * and to 2 to print debug message.
30817c478bd9Sstevel@tonic-gate  */
30827c478bd9Sstevel@tonic-gate static int optimize_dtree = 1;
30837c478bd9Sstevel@tonic-gate 
30847c478bd9Sstevel@tonic-gate static void
30857c478bd9Sstevel@tonic-gate debug_dtree(dev_info_t *devi, struct dev_info *adevi, char *service)
30867c478bd9Sstevel@tonic-gate {
30877c478bd9Sstevel@tonic-gate 	char *adeviname, *buf;
30887c478bd9Sstevel@tonic-gate 
30897c478bd9Sstevel@tonic-gate 	/*
30907c478bd9Sstevel@tonic-gate 	 * Don't print unless optimize dtree is set to 2+
30917c478bd9Sstevel@tonic-gate 	 */
30927c478bd9Sstevel@tonic-gate 	if (optimize_dtree <= 1)
30937c478bd9Sstevel@tonic-gate 		return;
30947c478bd9Sstevel@tonic-gate 
30957c478bd9Sstevel@tonic-gate 	buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
30967c478bd9Sstevel@tonic-gate 	adeviname = ddi_deviname((dev_info_t *)adevi, buf);
30977c478bd9Sstevel@tonic-gate 	if (*adeviname == '\0')
30987c478bd9Sstevel@tonic-gate 		adeviname = "root";
30997c478bd9Sstevel@tonic-gate 
31007c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "%s %s -> %s\n",
31017c478bd9Sstevel@tonic-gate 	    ddi_deviname(devi, buf), service, adeviname);
31027c478bd9Sstevel@tonic-gate 
31037c478bd9Sstevel@tonic-gate 	kmem_free(buf, MAXNAMELEN);
31047c478bd9Sstevel@tonic-gate }
31057c478bd9Sstevel@tonic-gate #else /* DEBUG */
31067c478bd9Sstevel@tonic-gate #define	debug_dtree(a1, a2, a3)	 /* nothing */
31077c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
31087c478bd9Sstevel@tonic-gate 
31097c478bd9Sstevel@tonic-gate static void
31107c478bd9Sstevel@tonic-gate ddi_optimize_dtree(dev_info_t *devi)
31117c478bd9Sstevel@tonic-gate {
31127c478bd9Sstevel@tonic-gate 	struct dev_info *pdevi;
31137c478bd9Sstevel@tonic-gate 	struct bus_ops *b;
31147c478bd9Sstevel@tonic-gate 
31157c478bd9Sstevel@tonic-gate 	pdevi = DEVI(devi)->devi_parent;
31167c478bd9Sstevel@tonic-gate 	ASSERT(pdevi);
31177c478bd9Sstevel@tonic-gate 
31187c478bd9Sstevel@tonic-gate 	/*
31197c478bd9Sstevel@tonic-gate 	 * Set the unoptimized values
31207c478bd9Sstevel@tonic-gate 	 */
31217c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_map_fault = pdevi;
31227c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_map = pdevi;
31237c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_allochdl = pdevi;
31247c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_freehdl = pdevi;
31257c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_bindhdl = pdevi;
31267c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_bindfunc =
31277c478bd9Sstevel@tonic-gate 	    pdevi->devi_ops->devo_bus_ops->bus_dma_bindhdl;
31287c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_unbindhdl = pdevi;
31297c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_unbindfunc =
31307c478bd9Sstevel@tonic-gate 	    pdevi->devi_ops->devo_bus_ops->bus_dma_unbindhdl;
31317c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_flush = pdevi;
31327c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_win = pdevi;
31337c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_ctl = pdevi;
31347c478bd9Sstevel@tonic-gate 	DEVI(devi)->devi_bus_ctl = pdevi;
31357c478bd9Sstevel@tonic-gate 
31367c478bd9Sstevel@tonic-gate #ifdef DEBUG
31377c478bd9Sstevel@tonic-gate 	if (optimize_dtree == 0)
31387c478bd9Sstevel@tonic-gate 		return;
31397c478bd9Sstevel@tonic-gate #endif /* DEBUG */
31407c478bd9Sstevel@tonic-gate 
31417c478bd9Sstevel@tonic-gate 	b = pdevi->devi_ops->devo_bus_ops;
31427c478bd9Sstevel@tonic-gate 
31437c478bd9Sstevel@tonic-gate 	if (i_ddi_map_fault == b->bus_map_fault) {
31447c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_map_fault = pdevi->devi_bus_map_fault;
31457c478bd9Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_map_fault,
31467c478bd9Sstevel@tonic-gate 		    "bus_map_fault");
31477c478bd9Sstevel@tonic-gate 	}
31487c478bd9Sstevel@tonic-gate 
31497c478bd9Sstevel@tonic-gate 	if (ddi_dma_map == b->bus_dma_map) {
31507c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_map = pdevi->devi_bus_dma_map;
31517c478bd9Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_map, "bus_dma_map");
31527c478bd9Sstevel@tonic-gate 	}
31537c478bd9Sstevel@tonic-gate 
31547c478bd9Sstevel@tonic-gate 	if (ddi_dma_allochdl == b->bus_dma_allochdl) {
31557c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_allochdl =
31567c478bd9Sstevel@tonic-gate 		    pdevi->devi_bus_dma_allochdl;
31577c478bd9Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_allochdl,
31587c478bd9Sstevel@tonic-gate 		    "bus_dma_allochdl");
31597c478bd9Sstevel@tonic-gate 	}
31607c478bd9Sstevel@tonic-gate 
31617c478bd9Sstevel@tonic-gate 	if (ddi_dma_freehdl == b->bus_dma_freehdl) {
31627c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_freehdl = pdevi->devi_bus_dma_freehdl;
31637c478bd9Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_freehdl,
31647c478bd9Sstevel@tonic-gate 		    "bus_dma_freehdl");
31657c478bd9Sstevel@tonic-gate 	}
31667c478bd9Sstevel@tonic-gate 
31677c478bd9Sstevel@tonic-gate 	if (ddi_dma_bindhdl == b->bus_dma_bindhdl) {
31687c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_bindhdl = pdevi->devi_bus_dma_bindhdl;
31697c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_bindfunc =
31707c478bd9Sstevel@tonic-gate 		    pdevi->devi_bus_dma_bindhdl->devi_ops->
31717c478bd9Sstevel@tonic-gate 		    devo_bus_ops->bus_dma_bindhdl;
31727c478bd9Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_bindhdl,
31737c478bd9Sstevel@tonic-gate 		    "bus_dma_bindhdl");
31747c478bd9Sstevel@tonic-gate 	}
31757c478bd9Sstevel@tonic-gate 
31767c478bd9Sstevel@tonic-gate 	if (ddi_dma_unbindhdl == b->bus_dma_unbindhdl) {
31777c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_unbindhdl =
31787c478bd9Sstevel@tonic-gate 		    pdevi->devi_bus_dma_unbindhdl;
31797c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_unbindfunc =
31807c478bd9Sstevel@tonic-gate 		    pdevi->devi_bus_dma_unbindhdl->devi_ops->
31817c478bd9Sstevel@tonic-gate 		    devo_bus_ops->bus_dma_unbindhdl;
31827c478bd9Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_unbindhdl,
31837c478bd9Sstevel@tonic-gate 		    "bus_dma_unbindhdl");
31847c478bd9Sstevel@tonic-gate 	}
31857c478bd9Sstevel@tonic-gate 
31867c478bd9Sstevel@tonic-gate 	if (ddi_dma_flush == b->bus_dma_flush) {
31877c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_flush = pdevi->devi_bus_dma_flush;
31887c478bd9Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_flush,
31897c478bd9Sstevel@tonic-gate 		    "bus_dma_flush");
31907c478bd9Sstevel@tonic-gate 	}
31917c478bd9Sstevel@tonic-gate 
31927c478bd9Sstevel@tonic-gate 	if (ddi_dma_win == b->bus_dma_win) {
31937c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_win = pdevi->devi_bus_dma_win;
31947c478bd9Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_win,
31957c478bd9Sstevel@tonic-gate 		    "bus_dma_win");
31967c478bd9Sstevel@tonic-gate 	}
31977c478bd9Sstevel@tonic-gate 
31987c478bd9Sstevel@tonic-gate 	if (ddi_dma_mctl == b->bus_dma_ctl) {
31997c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_ctl = pdevi->devi_bus_dma_ctl;
32007c478bd9Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_ctl, "bus_dma_ctl");
32017c478bd9Sstevel@tonic-gate 	}
32027c478bd9Sstevel@tonic-gate 
32037c478bd9Sstevel@tonic-gate 	if (ddi_ctlops == b->bus_ctl) {
32047c478bd9Sstevel@tonic-gate 		DEVI(devi)->devi_bus_ctl = pdevi->devi_bus_ctl;
32057c478bd9Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_ctl, "bus_ctl");
32067c478bd9Sstevel@tonic-gate 	}
32077c478bd9Sstevel@tonic-gate }
32087c478bd9Sstevel@tonic-gate 
32097c478bd9Sstevel@tonic-gate #define	MIN_DEVINFO_LOG_SIZE	max_ncpus
32107c478bd9Sstevel@tonic-gate #define	MAX_DEVINFO_LOG_SIZE	max_ncpus * 10
32117c478bd9Sstevel@tonic-gate 
32127c478bd9Sstevel@tonic-gate static void
32137c478bd9Sstevel@tonic-gate da_log_init()
32147c478bd9Sstevel@tonic-gate {
32157c478bd9Sstevel@tonic-gate 	devinfo_log_header_t *dh;
32167c478bd9Sstevel@tonic-gate 	int logsize = devinfo_log_size;
32177c478bd9Sstevel@tonic-gate 
32187c478bd9Sstevel@tonic-gate 	if (logsize == 0)
32197c478bd9Sstevel@tonic-gate 		logsize = MIN_DEVINFO_LOG_SIZE;
32207c478bd9Sstevel@tonic-gate 	else if (logsize > MAX_DEVINFO_LOG_SIZE)
32217c478bd9Sstevel@tonic-gate 		logsize = MAX_DEVINFO_LOG_SIZE;
32227c478bd9Sstevel@tonic-gate 
32237c478bd9Sstevel@tonic-gate 	dh = kmem_alloc(logsize * PAGESIZE, KM_SLEEP);
32247c478bd9Sstevel@tonic-gate 	mutex_init(&dh->dh_lock, NULL, MUTEX_DEFAULT, NULL);
32257c478bd9Sstevel@tonic-gate 	dh->dh_max = ((logsize * PAGESIZE) - sizeof (*dh)) /
32267c478bd9Sstevel@tonic-gate 	    sizeof (devinfo_audit_t) + 1;
32277c478bd9Sstevel@tonic-gate 	dh->dh_curr = -1;
32287c478bd9Sstevel@tonic-gate 	dh->dh_hits = 0;
32297c478bd9Sstevel@tonic-gate 
32307c478bd9Sstevel@tonic-gate 	devinfo_audit_log = dh;
32317c478bd9Sstevel@tonic-gate }
32327c478bd9Sstevel@tonic-gate 
32337c478bd9Sstevel@tonic-gate /*
32347c478bd9Sstevel@tonic-gate  * Log the stack trace in per-devinfo audit structure and also enter
32357c478bd9Sstevel@tonic-gate  * it into a system wide log for recording the time history.
32367c478bd9Sstevel@tonic-gate  */
32377c478bd9Sstevel@tonic-gate static void
32387c478bd9Sstevel@tonic-gate da_log_enter(dev_info_t *dip)
32397c478bd9Sstevel@tonic-gate {
32407c478bd9Sstevel@tonic-gate 	devinfo_audit_t *da_log, *da = DEVI(dip)->devi_audit;
32417c478bd9Sstevel@tonic-gate 	devinfo_log_header_t *dh = devinfo_audit_log;
32427c478bd9Sstevel@tonic-gate 
32437c478bd9Sstevel@tonic-gate 	if (devinfo_audit_log == NULL)
32447c478bd9Sstevel@tonic-gate 		return;
32457c478bd9Sstevel@tonic-gate 
32467c478bd9Sstevel@tonic-gate 	ASSERT(da != NULL);
32477c478bd9Sstevel@tonic-gate 
32487c478bd9Sstevel@tonic-gate 	da->da_devinfo = dip;
32497c478bd9Sstevel@tonic-gate 	da->da_timestamp = gethrtime();
32507c478bd9Sstevel@tonic-gate 	da->da_thread = curthread;
32517c478bd9Sstevel@tonic-gate 	da->da_node_state = DEVI(dip)->devi_node_state;
32527c478bd9Sstevel@tonic-gate 	da->da_device_state = DEVI(dip)->devi_state;
32537c478bd9Sstevel@tonic-gate 	da->da_depth = getpcstack(da->da_stack, DDI_STACK_DEPTH);
32547c478bd9Sstevel@tonic-gate 
32557c478bd9Sstevel@tonic-gate 	/*
32567c478bd9Sstevel@tonic-gate 	 * Copy into common log and note the location for tracing history
32577c478bd9Sstevel@tonic-gate 	 */
32587c478bd9Sstevel@tonic-gate 	mutex_enter(&dh->dh_lock);
32597c478bd9Sstevel@tonic-gate 	dh->dh_hits++;
32607c478bd9Sstevel@tonic-gate 	dh->dh_curr++;
32617c478bd9Sstevel@tonic-gate 	if (dh->dh_curr >= dh->dh_max)
32627c478bd9Sstevel@tonic-gate 		dh->dh_curr -= dh->dh_max;
32637c478bd9Sstevel@tonic-gate 	da_log = &dh->dh_entry[dh->dh_curr];
32647c478bd9Sstevel@tonic-gate 	mutex_exit(&dh->dh_lock);
32657c478bd9Sstevel@tonic-gate 
32667c478bd9Sstevel@tonic-gate 	bcopy(da, da_log, sizeof (devinfo_audit_t));
32677c478bd9Sstevel@tonic-gate 	da->da_lastlog = da_log;
32687c478bd9Sstevel@tonic-gate }
32697c478bd9Sstevel@tonic-gate 
32707c478bd9Sstevel@tonic-gate static void
32717c478bd9Sstevel@tonic-gate attach_drivers()
32727c478bd9Sstevel@tonic-gate {
32737c478bd9Sstevel@tonic-gate 	int i;
32747c478bd9Sstevel@tonic-gate 	for (i = 0; i < devcnt; i++) {
32757c478bd9Sstevel@tonic-gate 		struct devnames *dnp = &devnamesp[i];
32767c478bd9Sstevel@tonic-gate 		if ((dnp->dn_flags & DN_FORCE_ATTACH) &&
32777c478bd9Sstevel@tonic-gate 		    (ddi_hold_installed_driver((major_t)i) != NULL))
32787c478bd9Sstevel@tonic-gate 			ddi_rele_driver((major_t)i);
32797c478bd9Sstevel@tonic-gate 	}
32807c478bd9Sstevel@tonic-gate }
32817c478bd9Sstevel@tonic-gate 
32827c478bd9Sstevel@tonic-gate /*
32837c478bd9Sstevel@tonic-gate  * Launch a thread to force attach drivers. This avoids penalty on boot time.
32847c478bd9Sstevel@tonic-gate  */
32857c478bd9Sstevel@tonic-gate void
32867c478bd9Sstevel@tonic-gate i_ddi_forceattach_drivers()
32877c478bd9Sstevel@tonic-gate {
32887c478bd9Sstevel@tonic-gate 
32899d3d2ed0Shiremath 	/*
32909d3d2ed0Shiremath 	 * Attach IB VHCI driver before the force-attach thread attaches the
32919d3d2ed0Shiremath 	 * IB HCA driver. IB HCA driver will fail if IB Nexus has not yet
32929d3d2ed0Shiremath 	 * been attached.
32939d3d2ed0Shiremath 	 */
32949d3d2ed0Shiremath 	(void) ddi_hold_installed_driver(ddi_name_to_major("ib"));
32959d3d2ed0Shiremath 
32967c478bd9Sstevel@tonic-gate 	(void) thread_create(NULL, 0, (void (*)())attach_drivers, NULL, 0, &p0,
32977c478bd9Sstevel@tonic-gate 	    TS_RUN, minclsyspri);
32987c478bd9Sstevel@tonic-gate }
32997c478bd9Sstevel@tonic-gate 
33007c478bd9Sstevel@tonic-gate /*
33017c478bd9Sstevel@tonic-gate  * This is a private DDI interface for optimizing boot performance.
33027c478bd9Sstevel@tonic-gate  * I/O subsystem initialization is considered complete when devfsadm
33037c478bd9Sstevel@tonic-gate  * is executed.
33047c478bd9Sstevel@tonic-gate  *
3305facf4a8dSllai1  * NOTE: The start of syseventd happens to be a convenient indicator
3306facf4a8dSllai1  *	of the completion of I/O initialization during boot.
33077c478bd9Sstevel@tonic-gate  *	The implementation should be replaced by something more robust.
33087c478bd9Sstevel@tonic-gate  */
33097c478bd9Sstevel@tonic-gate int
33107c478bd9Sstevel@tonic-gate i_ddi_io_initialized()
33117c478bd9Sstevel@tonic-gate {
33127c478bd9Sstevel@tonic-gate 	extern int sysevent_daemon_init;
33137c478bd9Sstevel@tonic-gate 	return (sysevent_daemon_init);
33147c478bd9Sstevel@tonic-gate }
33157c478bd9Sstevel@tonic-gate 
3316facf4a8dSllai1 /*
3317facf4a8dSllai1  * May be used to determine system boot state
3318facf4a8dSllai1  * "Available" means the system is for the most part up
3319facf4a8dSllai1  * and initialized, with all system services either up or
3320facf4a8dSllai1  * capable of being started.  This state is set by devfsadm
3321facf4a8dSllai1  * during the boot process.  The /dev filesystem infers
3322facf4a8dSllai1  * from this when implicit reconfig can be performed,
3323facf4a8dSllai1  * ie, devfsadm can be invoked.  Please avoid making
3324facf4a8dSllai1  * further use of this unless it's really necessary.
3325facf4a8dSllai1  */
3326facf4a8dSllai1 int
3327facf4a8dSllai1 i_ddi_sysavail()
3328facf4a8dSllai1 {
3329facf4a8dSllai1 	return (devname_state & DS_SYSAVAIL);
3330facf4a8dSllai1 }
3331facf4a8dSllai1 
3332facf4a8dSllai1 /*
3333facf4a8dSllai1  * May be used to determine if boot is a reconfigure boot.
3334facf4a8dSllai1  */
3335facf4a8dSllai1 int
3336facf4a8dSllai1 i_ddi_reconfig()
3337facf4a8dSllai1 {
3338facf4a8dSllai1 	return (devname_state & DS_RECONFIG);
3339facf4a8dSllai1 }
3340facf4a8dSllai1 
3341facf4a8dSllai1 /*
3342facf4a8dSllai1  * Note system services are up, inform /dev.
3343facf4a8dSllai1  */
3344facf4a8dSllai1 void
3345facf4a8dSllai1 i_ddi_set_sysavail()
3346facf4a8dSllai1 {
3347facf4a8dSllai1 	if ((devname_state & DS_SYSAVAIL) == 0) {
3348facf4a8dSllai1 		devname_state |= DS_SYSAVAIL;
3349facf4a8dSllai1 		sdev_devstate_change();
3350facf4a8dSllai1 	}
3351facf4a8dSllai1 }
3352facf4a8dSllai1 
3353facf4a8dSllai1 /*
3354facf4a8dSllai1  * Note reconfiguration boot, inform /dev.
3355facf4a8dSllai1  */
3356facf4a8dSllai1 void
3357facf4a8dSllai1 i_ddi_set_reconfig()
3358facf4a8dSllai1 {
3359facf4a8dSllai1 	if ((devname_state & DS_RECONFIG) == 0) {
3360facf4a8dSllai1 		devname_state |= DS_RECONFIG;
3361facf4a8dSllai1 		sdev_devstate_change();
3362facf4a8dSllai1 	}
3363facf4a8dSllai1 }
3364facf4a8dSllai1 
33657c478bd9Sstevel@tonic-gate 
33667c478bd9Sstevel@tonic-gate /*
33677c478bd9Sstevel@tonic-gate  * device tree walking
33687c478bd9Sstevel@tonic-gate  */
33697c478bd9Sstevel@tonic-gate 
33707c478bd9Sstevel@tonic-gate struct walk_elem {
33717c478bd9Sstevel@tonic-gate 	struct walk_elem *next;
33727c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
33737c478bd9Sstevel@tonic-gate };
33747c478bd9Sstevel@tonic-gate 
33757c478bd9Sstevel@tonic-gate static void
33767c478bd9Sstevel@tonic-gate free_list(struct walk_elem *list)
33777c478bd9Sstevel@tonic-gate {
33787c478bd9Sstevel@tonic-gate 	while (list) {
33797c478bd9Sstevel@tonic-gate 		struct walk_elem *next = list->next;
33807c478bd9Sstevel@tonic-gate 		kmem_free(list, sizeof (*list));
33817c478bd9Sstevel@tonic-gate 		list = next;
33827c478bd9Sstevel@tonic-gate 	}
33837c478bd9Sstevel@tonic-gate }
33847c478bd9Sstevel@tonic-gate 
33857c478bd9Sstevel@tonic-gate static void
33867c478bd9Sstevel@tonic-gate append_node(struct walk_elem **list, dev_info_t *dip)
33877c478bd9Sstevel@tonic-gate {
33887c478bd9Sstevel@tonic-gate 	struct walk_elem *tail;
33897c478bd9Sstevel@tonic-gate 	struct walk_elem *elem = kmem_alloc(sizeof (*elem), KM_SLEEP);
33907c478bd9Sstevel@tonic-gate 
33917c478bd9Sstevel@tonic-gate 	elem->next = NULL;
33927c478bd9Sstevel@tonic-gate 	elem->dip = dip;
33937c478bd9Sstevel@tonic-gate 
33947c478bd9Sstevel@tonic-gate 	if (*list == NULL) {
33957c478bd9Sstevel@tonic-gate 		*list = elem;
33967c478bd9Sstevel@tonic-gate 		return;
33977c478bd9Sstevel@tonic-gate 	}
33987c478bd9Sstevel@tonic-gate 
33997c478bd9Sstevel@tonic-gate 	tail = *list;
34007c478bd9Sstevel@tonic-gate 	while (tail->next)
34017c478bd9Sstevel@tonic-gate 		tail = tail->next;
34027c478bd9Sstevel@tonic-gate 
34037c478bd9Sstevel@tonic-gate 	tail->next = elem;
34047c478bd9Sstevel@tonic-gate }
34057c478bd9Sstevel@tonic-gate 
34067c478bd9Sstevel@tonic-gate /*
34077c478bd9Sstevel@tonic-gate  * The implementation of ddi_walk_devs().
34087c478bd9Sstevel@tonic-gate  */
34097c478bd9Sstevel@tonic-gate static int
34107c478bd9Sstevel@tonic-gate walk_devs(dev_info_t *dip, int (*f)(dev_info_t *, void *), void *arg,
34117c478bd9Sstevel@tonic-gate     int do_locking)
34127c478bd9Sstevel@tonic-gate {
34137c478bd9Sstevel@tonic-gate 	struct walk_elem *head = NULL;
34147c478bd9Sstevel@tonic-gate 
34157c478bd9Sstevel@tonic-gate 	/*
34167c478bd9Sstevel@tonic-gate 	 * Do it in two passes. First pass invoke callback on each
34177c478bd9Sstevel@tonic-gate 	 * dip on the sibling list. Second pass invoke callback on
34187c478bd9Sstevel@tonic-gate 	 * children of each dip.
34197c478bd9Sstevel@tonic-gate 	 */
34207c478bd9Sstevel@tonic-gate 	while (dip) {
34217c478bd9Sstevel@tonic-gate 		switch ((*f)(dip, arg)) {
34227c478bd9Sstevel@tonic-gate 		case DDI_WALK_TERMINATE:
34237c478bd9Sstevel@tonic-gate 			free_list(head);
34247c478bd9Sstevel@tonic-gate 			return (DDI_WALK_TERMINATE);
34257c478bd9Sstevel@tonic-gate 
34267c478bd9Sstevel@tonic-gate 		case DDI_WALK_PRUNESIB:
34277c478bd9Sstevel@tonic-gate 			/* ignore sibling by setting dip to NULL */
34287c478bd9Sstevel@tonic-gate 			append_node(&head, dip);
34297c478bd9Sstevel@tonic-gate 			dip = NULL;
34307c478bd9Sstevel@tonic-gate 			break;
34317c478bd9Sstevel@tonic-gate 
34327c478bd9Sstevel@tonic-gate 		case DDI_WALK_PRUNECHILD:
34337c478bd9Sstevel@tonic-gate 			/* don't worry about children */
34347c478bd9Sstevel@tonic-gate 			dip = ddi_get_next_sibling(dip);
34357c478bd9Sstevel@tonic-gate 			break;
34367c478bd9Sstevel@tonic-gate 
34377c478bd9Sstevel@tonic-gate 		case DDI_WALK_CONTINUE:
34387c478bd9Sstevel@tonic-gate 		default:
34397c478bd9Sstevel@tonic-gate 			append_node(&head, dip);
34407c478bd9Sstevel@tonic-gate 			dip = ddi_get_next_sibling(dip);
34417c478bd9Sstevel@tonic-gate 			break;
34427c478bd9Sstevel@tonic-gate 		}
34437c478bd9Sstevel@tonic-gate 
34447c478bd9Sstevel@tonic-gate 	}
34457c478bd9Sstevel@tonic-gate 
34467c478bd9Sstevel@tonic-gate 	/* second pass */
34477c478bd9Sstevel@tonic-gate 	while (head) {
34487c478bd9Sstevel@tonic-gate 		int circ;
34497c478bd9Sstevel@tonic-gate 		struct walk_elem *next = head->next;
34507c478bd9Sstevel@tonic-gate 
34517c478bd9Sstevel@tonic-gate 		if (do_locking)
34527c478bd9Sstevel@tonic-gate 			ndi_devi_enter(head->dip, &circ);
34537c478bd9Sstevel@tonic-gate 		if (walk_devs(ddi_get_child(head->dip), f, arg, do_locking) ==
34547c478bd9Sstevel@tonic-gate 		    DDI_WALK_TERMINATE) {
34557c478bd9Sstevel@tonic-gate 			if (do_locking)
34567c478bd9Sstevel@tonic-gate 				ndi_devi_exit(head->dip, circ);
34577c478bd9Sstevel@tonic-gate 			free_list(head);
34587c478bd9Sstevel@tonic-gate 			return (DDI_WALK_TERMINATE);
34597c478bd9Sstevel@tonic-gate 		}
34607c478bd9Sstevel@tonic-gate 		if (do_locking)
34617c478bd9Sstevel@tonic-gate 			ndi_devi_exit(head->dip, circ);
34627c478bd9Sstevel@tonic-gate 		kmem_free(head, sizeof (*head));
34637c478bd9Sstevel@tonic-gate 		head = next;
34647c478bd9Sstevel@tonic-gate 	}
34657c478bd9Sstevel@tonic-gate 
34667c478bd9Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
34677c478bd9Sstevel@tonic-gate }
34687c478bd9Sstevel@tonic-gate 
34697c478bd9Sstevel@tonic-gate /*
34707c478bd9Sstevel@tonic-gate  * This general-purpose routine traverses the tree of dev_info nodes,
34717c478bd9Sstevel@tonic-gate  * starting from the given node, and calls the given function for each
34727c478bd9Sstevel@tonic-gate  * node that it finds with the current node and the pointer arg (which
34737c478bd9Sstevel@tonic-gate  * can point to a structure of information that the function
34747c478bd9Sstevel@tonic-gate  * needs) as arguments.
34757c478bd9Sstevel@tonic-gate  *
34767c478bd9Sstevel@tonic-gate  * It does the walk a layer at a time, not depth-first. The given function
34777c478bd9Sstevel@tonic-gate  * must return one of the following values:
34787c478bd9Sstevel@tonic-gate  *	DDI_WALK_CONTINUE
34797c478bd9Sstevel@tonic-gate  *	DDI_WALK_PRUNESIB
34807c478bd9Sstevel@tonic-gate  *	DDI_WALK_PRUNECHILD
34817c478bd9Sstevel@tonic-gate  *	DDI_WALK_TERMINATE
34827c478bd9Sstevel@tonic-gate  *
34837c478bd9Sstevel@tonic-gate  * N.B. Since we walk the sibling list, the caller must ensure that
34847c478bd9Sstevel@tonic-gate  *	the parent of dip is held against changes, unless the parent
34857c478bd9Sstevel@tonic-gate  *	is rootnode.  ndi_devi_enter() on the parent is sufficient.
34867c478bd9Sstevel@tonic-gate  *
34877c478bd9Sstevel@tonic-gate  *	To avoid deadlock situations, caller must not attempt to
34887c478bd9Sstevel@tonic-gate  *	configure/unconfigure/remove device node in (*f)(), nor should
34895e3986cbScth  *	it attempt to recurse on other nodes in the system. Any
34905e3986cbScth  *	ndi_devi_enter() done by (*f)() must occur 'at-or-below' the
34915e3986cbScth  *	node entered prior to ddi_walk_devs(). Furthermore, if (*f)()
34925e3986cbScth  *	does any multi-threading (in framework *or* in driver) then the
34935e3986cbScth  *	ndi_devi_enter() calls done by dependent threads must be
34945e3986cbScth  *	'strictly-below'.
34957c478bd9Sstevel@tonic-gate  *
34967c478bd9Sstevel@tonic-gate  *	This is not callable from device autoconfiguration routines.
34977c478bd9Sstevel@tonic-gate  *	They include, but not limited to, _init(9e), _fini(9e), probe(9e),
34987c478bd9Sstevel@tonic-gate  *	attach(9e), and detach(9e).
34997c478bd9Sstevel@tonic-gate  */
35007c478bd9Sstevel@tonic-gate 
35017c478bd9Sstevel@tonic-gate void
35027c478bd9Sstevel@tonic-gate ddi_walk_devs(dev_info_t *dip, int (*f)(dev_info_t *, void *), void *arg)
35037c478bd9Sstevel@tonic-gate {
35047c478bd9Sstevel@tonic-gate 
35057c478bd9Sstevel@tonic-gate 	ASSERT(dip == NULL || ddi_get_parent(dip) == NULL ||
35067c478bd9Sstevel@tonic-gate 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
35077c478bd9Sstevel@tonic-gate 
35087c478bd9Sstevel@tonic-gate 	(void) walk_devs(dip, f, arg, 1);
35097c478bd9Sstevel@tonic-gate }
35107c478bd9Sstevel@tonic-gate 
35117c478bd9Sstevel@tonic-gate /*
35127c478bd9Sstevel@tonic-gate  * This is a general-purpose routine traverses the per-driver list
35137c478bd9Sstevel@tonic-gate  * and calls the given function for each node. must return one of
35147c478bd9Sstevel@tonic-gate  * the following values:
35157c478bd9Sstevel@tonic-gate  *	DDI_WALK_CONTINUE
35167c478bd9Sstevel@tonic-gate  *	DDI_WALK_TERMINATE
35177c478bd9Sstevel@tonic-gate  *
35187c478bd9Sstevel@tonic-gate  * N.B. The same restrictions from ddi_walk_devs() apply.
35197c478bd9Sstevel@tonic-gate  */
35207c478bd9Sstevel@tonic-gate 
35217c478bd9Sstevel@tonic-gate void
35227c478bd9Sstevel@tonic-gate e_ddi_walk_driver(char *drv, int (*f)(dev_info_t *, void *), void *arg)
35237c478bd9Sstevel@tonic-gate {
35247c478bd9Sstevel@tonic-gate 	major_t major;
35257c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
35267c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
35277c478bd9Sstevel@tonic-gate 
35287c478bd9Sstevel@tonic-gate 	major = ddi_name_to_major(drv);
3529a204de77Scth 	if (major == DDI_MAJOR_T_NONE)
35307c478bd9Sstevel@tonic-gate 		return;
35317c478bd9Sstevel@tonic-gate 
35327c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
35337c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
35347c478bd9Sstevel@tonic-gate 	dip = dnp->dn_head;
35357c478bd9Sstevel@tonic-gate 	while (dip) {
35367c478bd9Sstevel@tonic-gate 		ndi_hold_devi(dip);
35377c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
35387c478bd9Sstevel@tonic-gate 		if ((*f)(dip, arg) == DDI_WALK_TERMINATE) {
35397c478bd9Sstevel@tonic-gate 			ndi_rele_devi(dip);
35407c478bd9Sstevel@tonic-gate 			return;
35417c478bd9Sstevel@tonic-gate 		}
35427c478bd9Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
35437c478bd9Sstevel@tonic-gate 		ndi_rele_devi(dip);
35447c478bd9Sstevel@tonic-gate 		dip = ddi_get_next(dip);
35457c478bd9Sstevel@tonic-gate 	}
35467c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
35477c478bd9Sstevel@tonic-gate }
35487c478bd9Sstevel@tonic-gate 
35497c478bd9Sstevel@tonic-gate /*
35507c478bd9Sstevel@tonic-gate  * argument to i_find_devi, a devinfo node search callback function.
35517c478bd9Sstevel@tonic-gate  */
35527c478bd9Sstevel@tonic-gate struct match_info {
35537c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;		/* result */
35547c478bd9Sstevel@tonic-gate 	char		*nodename;	/* if non-null, nodename must match */
35557c478bd9Sstevel@tonic-gate 	int		instance;	/* if != -1, instance must match */
3556737d277aScth 	int		attached;	/* if != 0, i_ddi_devi_attached() */
35577c478bd9Sstevel@tonic-gate };
35587c478bd9Sstevel@tonic-gate 
35597c478bd9Sstevel@tonic-gate static int
35607c478bd9Sstevel@tonic-gate i_find_devi(dev_info_t *dip, void *arg)
35617c478bd9Sstevel@tonic-gate {
35627c478bd9Sstevel@tonic-gate 	struct match_info *info = (struct match_info *)arg;
35637c478bd9Sstevel@tonic-gate 
35647c478bd9Sstevel@tonic-gate 	if (((info->nodename == NULL) ||
35657c478bd9Sstevel@tonic-gate 	    (strcmp(ddi_node_name(dip), info->nodename) == 0)) &&
35667c478bd9Sstevel@tonic-gate 	    ((info->instance == -1) ||
35677c478bd9Sstevel@tonic-gate 	    (ddi_get_instance(dip) == info->instance)) &&
3568737d277aScth 	    ((info->attached == 0) || i_ddi_devi_attached(dip))) {
35697c478bd9Sstevel@tonic-gate 		info->dip = dip;
35707c478bd9Sstevel@tonic-gate 		ndi_hold_devi(dip);
35717c478bd9Sstevel@tonic-gate 		return (DDI_WALK_TERMINATE);
35727c478bd9Sstevel@tonic-gate 	}
35737c478bd9Sstevel@tonic-gate 
35747c478bd9Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
35757c478bd9Sstevel@tonic-gate }
35767c478bd9Sstevel@tonic-gate 
35777c478bd9Sstevel@tonic-gate /*
35787c478bd9Sstevel@tonic-gate  * Find dip with a known node name and instance and return with it held
35797c478bd9Sstevel@tonic-gate  */
35807c478bd9Sstevel@tonic-gate dev_info_t *
35817c478bd9Sstevel@tonic-gate ddi_find_devinfo(char *nodename, int instance, int attached)
35827c478bd9Sstevel@tonic-gate {
35837c478bd9Sstevel@tonic-gate 	struct match_info	info;
35847c478bd9Sstevel@tonic-gate 
35857c478bd9Sstevel@tonic-gate 	info.nodename = nodename;
35867c478bd9Sstevel@tonic-gate 	info.instance = instance;
35877c478bd9Sstevel@tonic-gate 	info.attached = attached;
35887c478bd9Sstevel@tonic-gate 	info.dip = NULL;
35897c478bd9Sstevel@tonic-gate 
35907c478bd9Sstevel@tonic-gate 	ddi_walk_devs(ddi_root_node(), i_find_devi, &info);
35917c478bd9Sstevel@tonic-gate 	return (info.dip);
35927c478bd9Sstevel@tonic-gate }
35937c478bd9Sstevel@tonic-gate 
3594dedec472SJack Meng extern ib_boot_prop_t *iscsiboot_prop;
3595dedec472SJack Meng static void
3596dedec472SJack Meng i_ddi_parse_iscsi_name(char *name, char **nodename, char **addrname,
3597dedec472SJack Meng     char **minorname)
3598dedec472SJack Meng {
3599dedec472SJack Meng 	char *cp, *colon;
3600dedec472SJack Meng 	static char nulladdrname[] = "";
3601dedec472SJack Meng 
3602dedec472SJack Meng 	/* default values */
3603dedec472SJack Meng 	if (nodename)
3604dedec472SJack Meng 		*nodename = name;
3605dedec472SJack Meng 	if (addrname)
3606dedec472SJack Meng 		*addrname = nulladdrname;
3607dedec472SJack Meng 	if (minorname)
3608dedec472SJack Meng 		*minorname = NULL;
3609dedec472SJack Meng 
3610dedec472SJack Meng 	cp = colon = name;
3611dedec472SJack Meng 	while (*cp != '\0') {
3612dedec472SJack Meng 		if (addrname && *cp == '@') {
3613dedec472SJack Meng 			*addrname = cp + 1;
3614dedec472SJack Meng 			*cp = '\0';
3615dedec472SJack Meng 		} else if (minorname && *cp == ':') {
3616dedec472SJack Meng 			*minorname = cp + 1;
3617dedec472SJack Meng 			colon = cp;
3618dedec472SJack Meng 		}
3619dedec472SJack Meng 		++cp;
3620dedec472SJack Meng 	}
3621dedec472SJack Meng 	if (colon != name) {
3622dedec472SJack Meng 		*colon = '\0';
3623dedec472SJack Meng 	}
3624dedec472SJack Meng }
3625dedec472SJack Meng 
36267c478bd9Sstevel@tonic-gate /*
36277c478bd9Sstevel@tonic-gate  * Parse for name, addr, and minor names. Some args may be NULL.
36287c478bd9Sstevel@tonic-gate  */
36297c478bd9Sstevel@tonic-gate void
36307c478bd9Sstevel@tonic-gate i_ddi_parse_name(char *name, char **nodename, char **addrname, char **minorname)
36317c478bd9Sstevel@tonic-gate {
36327c478bd9Sstevel@tonic-gate 	char *cp;
36337c478bd9Sstevel@tonic-gate 	static char nulladdrname[] = "";
36347c478bd9Sstevel@tonic-gate 
36357c478bd9Sstevel@tonic-gate 	/* default values */
36367c478bd9Sstevel@tonic-gate 	if (nodename)
36377c478bd9Sstevel@tonic-gate 		*nodename = name;
36387c478bd9Sstevel@tonic-gate 	if (addrname)
36397c478bd9Sstevel@tonic-gate 		*addrname = nulladdrname;
36407c478bd9Sstevel@tonic-gate 	if (minorname)
36417c478bd9Sstevel@tonic-gate 		*minorname = NULL;
36427c478bd9Sstevel@tonic-gate 
36437c478bd9Sstevel@tonic-gate 	cp = name;
36447c478bd9Sstevel@tonic-gate 	while (*cp != '\0') {
36457c478bd9Sstevel@tonic-gate 		if (addrname && *cp == '@') {
36467c478bd9Sstevel@tonic-gate 			*addrname = cp + 1;
36477c478bd9Sstevel@tonic-gate 			*cp = '\0';
36487c478bd9Sstevel@tonic-gate 		} else if (minorname && *cp == ':') {
36497c478bd9Sstevel@tonic-gate 			*minorname = cp + 1;
36507c478bd9Sstevel@tonic-gate 			*cp = '\0';
36517c478bd9Sstevel@tonic-gate 		}
36527c478bd9Sstevel@tonic-gate 		++cp;
36537c478bd9Sstevel@tonic-gate 	}
36547c478bd9Sstevel@tonic-gate }
36557c478bd9Sstevel@tonic-gate 
36567c478bd9Sstevel@tonic-gate static char *
36577c478bd9Sstevel@tonic-gate child_path_to_driver(dev_info_t *parent, char *child_name, char *unit_address)
36587c478bd9Sstevel@tonic-gate {
36597c478bd9Sstevel@tonic-gate 	char *p, *drvname = NULL;
36607c478bd9Sstevel@tonic-gate 	major_t maj;
36617c478bd9Sstevel@tonic-gate 
36627c478bd9Sstevel@tonic-gate 	/*
36637c478bd9Sstevel@tonic-gate 	 * Construct the pathname and ask the implementation
36647c478bd9Sstevel@tonic-gate 	 * if it can do a driver = f(pathname) for us, if not
36657c478bd9Sstevel@tonic-gate 	 * we'll just default to using the node-name that
36667c478bd9Sstevel@tonic-gate 	 * was given to us.  We want to do this first to
36677c478bd9Sstevel@tonic-gate 	 * allow the platform to use 'generic' names for
36687c478bd9Sstevel@tonic-gate 	 * legacy device drivers.
36697c478bd9Sstevel@tonic-gate 	 */
36707c478bd9Sstevel@tonic-gate 	p = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
36717c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(parent, p);
36727c478bd9Sstevel@tonic-gate 	(void) strcat(p, "/");
36737c478bd9Sstevel@tonic-gate 	(void) strcat(p, child_name);
36747c478bd9Sstevel@tonic-gate 	if (unit_address && *unit_address) {
36757c478bd9Sstevel@tonic-gate 		(void) strcat(p, "@");
36767c478bd9Sstevel@tonic-gate 		(void) strcat(p, unit_address);
36777c478bd9Sstevel@tonic-gate 	}
36787c478bd9Sstevel@tonic-gate 
36797c478bd9Sstevel@tonic-gate 	/*
36807c478bd9Sstevel@tonic-gate 	 * Get the binding. If there is none, return the child_name
36817c478bd9Sstevel@tonic-gate 	 * and let the caller deal with it.
36827c478bd9Sstevel@tonic-gate 	 */
36837c478bd9Sstevel@tonic-gate 	maj = path_to_major(p);
36847c478bd9Sstevel@tonic-gate 
36857c478bd9Sstevel@tonic-gate 	kmem_free(p, MAXPATHLEN);
36867c478bd9Sstevel@tonic-gate 
3687a204de77Scth 	if (maj != DDI_MAJOR_T_NONE)
36887c478bd9Sstevel@tonic-gate 		drvname = ddi_major_to_name(maj);
36897c478bd9Sstevel@tonic-gate 	if (drvname == NULL)
36907c478bd9Sstevel@tonic-gate 		drvname = child_name;
36917c478bd9Sstevel@tonic-gate 
36927c478bd9Sstevel@tonic-gate 	return (drvname);
36937c478bd9Sstevel@tonic-gate }
36947c478bd9Sstevel@tonic-gate 
36957c478bd9Sstevel@tonic-gate 
369620906b23SVikram Hegde #define	PCI_EX_CLASS	"pciexclass"
369720906b23SVikram Hegde #define	PCI_EX		"pciex"
369820906b23SVikram Hegde #define	PCI_CLASS	"pciclass"
369920906b23SVikram Hegde #define	PCI		"pci"
370020906b23SVikram Hegde 
370120906b23SVikram Hegde int
370220906b23SVikram Hegde ddi_is_pci_dip(dev_info_t *dip)
370320906b23SVikram Hegde {
370420906b23SVikram Hegde 	char	*prop = NULL;
370520906b23SVikram Hegde 
370620906b23SVikram Hegde 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
370720906b23SVikram Hegde 	    "compatible", &prop) == DDI_PROP_SUCCESS) {
370820906b23SVikram Hegde 		ASSERT(prop);
370920906b23SVikram Hegde 		if (strncmp(prop, PCI_EX_CLASS, sizeof (PCI_EX_CLASS) - 1)
371020906b23SVikram Hegde 		    == 0 ||
371120906b23SVikram Hegde 		    strncmp(prop, PCI_EX, sizeof (PCI_EX)- 1)
371220906b23SVikram Hegde 		    == 0 ||
371320906b23SVikram Hegde 		    strncmp(prop, PCI_CLASS, sizeof (PCI_CLASS) - 1)
371420906b23SVikram Hegde 		    == 0 ||
371520906b23SVikram Hegde 		    strncmp(prop, PCI, sizeof (PCI) - 1)
371620906b23SVikram Hegde 		    == 0) {
371720906b23SVikram Hegde 			ddi_prop_free(prop);
371820906b23SVikram Hegde 			return (1);
371920906b23SVikram Hegde 		}
372020906b23SVikram Hegde 	}
372120906b23SVikram Hegde 
372220906b23SVikram Hegde 	if (prop != NULL) {
372320906b23SVikram Hegde 		ddi_prop_free(prop);
372420906b23SVikram Hegde 	}
372520906b23SVikram Hegde 
372620906b23SVikram Hegde 	return (0);
372720906b23SVikram Hegde }
372820906b23SVikram Hegde 
37297c478bd9Sstevel@tonic-gate /*
37307c478bd9Sstevel@tonic-gate  * Given the pathname of a device, fill in the dev_info_t value and/or the
37317c478bd9Sstevel@tonic-gate  * dev_t value and/or the spectype, depending on which parameters are non-NULL.
37327c478bd9Sstevel@tonic-gate  * If there is an error, this function returns -1.
37337c478bd9Sstevel@tonic-gate  *
37347c478bd9Sstevel@tonic-gate  * NOTE: If this function returns the dev_info_t structure, then it
37357c478bd9Sstevel@tonic-gate  * does so with a hold on the devi. Caller should ensure that they get
37367c478bd9Sstevel@tonic-gate  * decremented via ddi_release_devi() or ndi_rele_devi();
37377c478bd9Sstevel@tonic-gate  *
37387c478bd9Sstevel@tonic-gate  * This function can be invoked in the boot case for a pathname without
37397c478bd9Sstevel@tonic-gate  * device argument (:xxxx), traditionally treated as a minor name.
37407c478bd9Sstevel@tonic-gate  * In this case, we do the following
37417c478bd9Sstevel@tonic-gate  * (1) search the minor node of type DDM_DEFAULT.
37427c478bd9Sstevel@tonic-gate  * (2) if no DDM_DEFAULT minor exists, then the first non-alias minor is chosen.
37437c478bd9Sstevel@tonic-gate  * (3) if neither exists, a dev_t is faked with minor number = instance.
37447c478bd9Sstevel@tonic-gate  * As of S9 FCS, no instance of #1 exists. #2 is used by several platforms
37457c478bd9Sstevel@tonic-gate  * to default the boot partition to :a possibly by other OBP definitions.
37467c478bd9Sstevel@tonic-gate  * #3 is used for booting off network interfaces, most SPARC network
37477c478bd9Sstevel@tonic-gate  * drivers support Style-2 only, so only DDM_ALIAS minor exists.
37487c478bd9Sstevel@tonic-gate  *
37497c478bd9Sstevel@tonic-gate  * It is possible for OBP to present device args at the end of the path as
37507c478bd9Sstevel@tonic-gate  * well as in the middle. For example, with IB the following strings are
37517c478bd9Sstevel@tonic-gate  * valid boot paths.
37527c478bd9Sstevel@tonic-gate  *	a /pci@8,700000/ib@1,2:port=1,pkey=ff,dhcp,...
37537c478bd9Sstevel@tonic-gate  *	b /pci@8,700000/ib@1,1:port=1/ioc@xxxxxx,yyyyyyy:dhcp
37547c478bd9Sstevel@tonic-gate  * Case (a), we first look for minor node "port=1,pkey...".
37557c478bd9Sstevel@tonic-gate  * Failing that, we will pass "port=1,pkey..." to the bus_config
37567c478bd9Sstevel@tonic-gate  * entry point of ib (HCA) driver.
37577c478bd9Sstevel@tonic-gate  * Case (b), configure ib@1,1 as usual. Then invoke ib's bus_config
37587c478bd9Sstevel@tonic-gate  * with argument "ioc@xxxxxxx,yyyyyyy:port=1". After configuring
37597c478bd9Sstevel@tonic-gate  * the ioc, look for minor node dhcp. If not found, pass ":dhcp"
37607c478bd9Sstevel@tonic-gate  * to ioc's bus_config entry point.
37617c478bd9Sstevel@tonic-gate  */
3762bde7aadfSVikram Hegde int
3763bde7aadfSVikram Hegde resolve_pathname(char *pathname,
3764bde7aadfSVikram Hegde 	dev_info_t **dipp, dev_t *devtp, int *spectypep)
37657c478bd9Sstevel@tonic-gate {
37667c478bd9Sstevel@tonic-gate 	int			error;
37677c478bd9Sstevel@tonic-gate 	dev_info_t		*parent, *child;
37687c478bd9Sstevel@tonic-gate 	struct pathname		pn;
37697c478bd9Sstevel@tonic-gate 	char			*component, *config_name;
37707c478bd9Sstevel@tonic-gate 	char			*minorname = NULL;
37717c478bd9Sstevel@tonic-gate 	char			*prev_minor = NULL;
37727c478bd9Sstevel@tonic-gate 	dev_t			devt = NODEV;
37737c478bd9Sstevel@tonic-gate 	int			spectype;
37747c478bd9Sstevel@tonic-gate 	struct ddi_minor_data	*dmn;
3775b9ccdc5aScth 	int			circ;
37767c478bd9Sstevel@tonic-gate 
37777c478bd9Sstevel@tonic-gate 	if (*pathname != '/')
37787c478bd9Sstevel@tonic-gate 		return (EINVAL);
37797c478bd9Sstevel@tonic-gate 	parent = ddi_root_node();	/* Begin at the top of the tree */
37807c478bd9Sstevel@tonic-gate 
37817c478bd9Sstevel@tonic-gate 	if (error = pn_get(pathname, UIO_SYSSPACE, &pn))
37827c478bd9Sstevel@tonic-gate 		return (error);
37837c478bd9Sstevel@tonic-gate 	pn_skipslash(&pn);
37847c478bd9Sstevel@tonic-gate 
3785737d277aScth 	ASSERT(i_ddi_devi_attached(parent));
37867c478bd9Sstevel@tonic-gate 	ndi_hold_devi(parent);
37877c478bd9Sstevel@tonic-gate 
37887c478bd9Sstevel@tonic-gate 	component = kmem_alloc(MAXNAMELEN, KM_SLEEP);
37897c478bd9Sstevel@tonic-gate 	config_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
37907c478bd9Sstevel@tonic-gate 
37917c478bd9Sstevel@tonic-gate 	while (pn_pathleft(&pn)) {
37927c478bd9Sstevel@tonic-gate 		/* remember prev minor (:xxx) in the middle of path */
37937c478bd9Sstevel@tonic-gate 		if (minorname)
37947c478bd9Sstevel@tonic-gate 			prev_minor = i_ddi_strdup(minorname, KM_SLEEP);
37957c478bd9Sstevel@tonic-gate 
37967c478bd9Sstevel@tonic-gate 		/* Get component and chop off minorname */
37977c478bd9Sstevel@tonic-gate 		(void) pn_getcomponent(&pn, component);
3798dedec472SJack Meng 		if ((iscsiboot_prop != NULL) &&
3799dedec472SJack Meng 		    (strcmp((DEVI(parent)->devi_node_name), "iscsi") == 0)) {
3800dedec472SJack Meng 			i_ddi_parse_iscsi_name(component, NULL, NULL,
3801dedec472SJack Meng 			    &minorname);
3802dedec472SJack Meng 		} else {
38037c478bd9Sstevel@tonic-gate 			i_ddi_parse_name(component, NULL, NULL, &minorname);
3804dedec472SJack Meng 		}
38057c478bd9Sstevel@tonic-gate 		if (prev_minor == NULL) {
38067c478bd9Sstevel@tonic-gate 			(void) snprintf(config_name, MAXNAMELEN, "%s",
38077c478bd9Sstevel@tonic-gate 			    component);
38087c478bd9Sstevel@tonic-gate 		} else {
38097c478bd9Sstevel@tonic-gate 			(void) snprintf(config_name, MAXNAMELEN, "%s:%s",
38107c478bd9Sstevel@tonic-gate 			    component, prev_minor);
38117c478bd9Sstevel@tonic-gate 			kmem_free(prev_minor, strlen(prev_minor) + 1);
38127c478bd9Sstevel@tonic-gate 			prev_minor = NULL;
38137c478bd9Sstevel@tonic-gate 		}
38147c478bd9Sstevel@tonic-gate 
38157c478bd9Sstevel@tonic-gate 		/*
38167c478bd9Sstevel@tonic-gate 		 * Find and configure the child
38177c478bd9Sstevel@tonic-gate 		 */
38187c478bd9Sstevel@tonic-gate 		if (ndi_devi_config_one(parent, config_name, &child,
38197c478bd9Sstevel@tonic-gate 		    NDI_PROMNAME | NDI_NO_EVENT) != NDI_SUCCESS) {
38207c478bd9Sstevel@tonic-gate 			ndi_rele_devi(parent);
38217c478bd9Sstevel@tonic-gate 			pn_free(&pn);
38227c478bd9Sstevel@tonic-gate 			kmem_free(component, MAXNAMELEN);
38237c478bd9Sstevel@tonic-gate 			kmem_free(config_name, MAXNAMELEN);
38247c478bd9Sstevel@tonic-gate 			return (-1);
38257c478bd9Sstevel@tonic-gate 		}
38267c478bd9Sstevel@tonic-gate 
3827737d277aScth 		ASSERT(i_ddi_devi_attached(child));
38287c478bd9Sstevel@tonic-gate 		ndi_rele_devi(parent);
38297c478bd9Sstevel@tonic-gate 		parent = child;
38307c478bd9Sstevel@tonic-gate 		pn_skipslash(&pn);
38317c478bd9Sstevel@tonic-gate 	}
38327c478bd9Sstevel@tonic-gate 
38337c478bd9Sstevel@tonic-gate 	/*
38347c478bd9Sstevel@tonic-gate 	 * First look for a minor node matching minorname.
38357c478bd9Sstevel@tonic-gate 	 * Failing that, try to pass minorname to bus_config().
38367c478bd9Sstevel@tonic-gate 	 */
38377c478bd9Sstevel@tonic-gate 	if (minorname && i_ddi_minorname_to_devtspectype(parent,
38387c478bd9Sstevel@tonic-gate 	    minorname, &devt, &spectype) == DDI_FAILURE) {
38397c478bd9Sstevel@tonic-gate 		(void) snprintf(config_name, MAXNAMELEN, "%s", minorname);
38407c478bd9Sstevel@tonic-gate 		if (ndi_devi_config_obp_args(parent,
38417c478bd9Sstevel@tonic-gate 		    config_name, &child, 0) != NDI_SUCCESS) {
38427c478bd9Sstevel@tonic-gate 			ndi_rele_devi(parent);
38437c478bd9Sstevel@tonic-gate 			pn_free(&pn);
38447c478bd9Sstevel@tonic-gate 			kmem_free(component, MAXNAMELEN);
38457c478bd9Sstevel@tonic-gate 			kmem_free(config_name, MAXNAMELEN);
38467c478bd9Sstevel@tonic-gate 			NDI_CONFIG_DEBUG((CE_NOTE,
38477c478bd9Sstevel@tonic-gate 			    "%s: minor node not found\n", pathname));
38487c478bd9Sstevel@tonic-gate 			return (-1);
38497c478bd9Sstevel@tonic-gate 		}
38507c478bd9Sstevel@tonic-gate 		minorname = NULL;	/* look for default minor */
3851737d277aScth 		ASSERT(i_ddi_devi_attached(child));
38527c478bd9Sstevel@tonic-gate 		ndi_rele_devi(parent);
38537c478bd9Sstevel@tonic-gate 		parent = child;
38547c478bd9Sstevel@tonic-gate 	}
38557c478bd9Sstevel@tonic-gate 
38567c478bd9Sstevel@tonic-gate 	if (devtp || spectypep) {
38577c478bd9Sstevel@tonic-gate 		if (minorname == NULL) {
3858b9ccdc5aScth 			/*
3859b9ccdc5aScth 			 * Search for a default entry with an active
3860b9ccdc5aScth 			 * ndi_devi_enter to protect the devi_minor list.
3861b9ccdc5aScth 			 */
3862b9ccdc5aScth 			ndi_devi_enter(parent, &circ);
38637c478bd9Sstevel@tonic-gate 			for (dmn = DEVI(parent)->devi_minor; dmn;
38647c478bd9Sstevel@tonic-gate 			    dmn = dmn->next) {
38657c478bd9Sstevel@tonic-gate 				if (dmn->type == DDM_DEFAULT) {
38667c478bd9Sstevel@tonic-gate 					devt = dmn->ddm_dev;
38677c478bd9Sstevel@tonic-gate 					spectype = dmn->ddm_spec_type;
38687c478bd9Sstevel@tonic-gate 					break;
38697c478bd9Sstevel@tonic-gate 				}
38707c478bd9Sstevel@tonic-gate 			}
38717c478bd9Sstevel@tonic-gate 
38727c478bd9Sstevel@tonic-gate 			if (devt == NODEV) {
38737c478bd9Sstevel@tonic-gate 				/*
38747c478bd9Sstevel@tonic-gate 				 * No default minor node, try the first one;
38757c478bd9Sstevel@tonic-gate 				 * else, assume 1-1 instance-minor mapping
38767c478bd9Sstevel@tonic-gate 				 */
38777c478bd9Sstevel@tonic-gate 				dmn = DEVI(parent)->devi_minor;
38787c478bd9Sstevel@tonic-gate 				if (dmn && ((dmn->type == DDM_MINOR) ||
38797c478bd9Sstevel@tonic-gate 				    (dmn->type == DDM_INTERNAL_PATH))) {
38807c478bd9Sstevel@tonic-gate 					devt = dmn->ddm_dev;
38817c478bd9Sstevel@tonic-gate 					spectype = dmn->ddm_spec_type;
38827c478bd9Sstevel@tonic-gate 				} else {
38837c478bd9Sstevel@tonic-gate 					devt = makedevice(
38847c478bd9Sstevel@tonic-gate 					    DEVI(parent)->devi_major,
38857c478bd9Sstevel@tonic-gate 					    ddi_get_instance(parent));
38867c478bd9Sstevel@tonic-gate 					spectype = S_IFCHR;
38877c478bd9Sstevel@tonic-gate 				}
38887c478bd9Sstevel@tonic-gate 			}
3889b9ccdc5aScth 			ndi_devi_exit(parent, circ);
38907c478bd9Sstevel@tonic-gate 		}
38917c478bd9Sstevel@tonic-gate 		if (devtp)
38927c478bd9Sstevel@tonic-gate 			*devtp = devt;
38937c478bd9Sstevel@tonic-gate 		if (spectypep)
38947c478bd9Sstevel@tonic-gate 			*spectypep = spectype;
38957c478bd9Sstevel@tonic-gate 	}
38967c478bd9Sstevel@tonic-gate 
38977c478bd9Sstevel@tonic-gate 	pn_free(&pn);
38987c478bd9Sstevel@tonic-gate 	kmem_free(component, MAXNAMELEN);
38997c478bd9Sstevel@tonic-gate 	kmem_free(config_name, MAXNAMELEN);
39007c478bd9Sstevel@tonic-gate 
39017c478bd9Sstevel@tonic-gate 	/*
39027c478bd9Sstevel@tonic-gate 	 * If there is no error, return the appropriate parameters
39037c478bd9Sstevel@tonic-gate 	 */
39047c478bd9Sstevel@tonic-gate 	if (dipp != NULL)
39057c478bd9Sstevel@tonic-gate 		*dipp = parent;
3906bde7aadfSVikram Hegde 	else {
39077c478bd9Sstevel@tonic-gate 		/*
39087c478bd9Sstevel@tonic-gate 		 * We should really keep the ref count to keep the node from
39097c478bd9Sstevel@tonic-gate 		 * detaching but ddi_pathname_to_dev_t() specifies a NULL dipp,
39107c478bd9Sstevel@tonic-gate 		 * so we have no way of passing back the held dip.  Not holding
39117c478bd9Sstevel@tonic-gate 		 * the dip allows detaches to occur - which can cause problems
39127c478bd9Sstevel@tonic-gate 		 * for subsystems which call ddi_pathname_to_dev_t (console).
39137c478bd9Sstevel@tonic-gate 		 *
39147c478bd9Sstevel@tonic-gate 		 * Instead of holding the dip, we place a ddi-no-autodetach
39157c478bd9Sstevel@tonic-gate 		 * property on the node to prevent auto detaching.
39167c478bd9Sstevel@tonic-gate 		 *
39177c478bd9Sstevel@tonic-gate 		 * The right fix is to remove ddi_pathname_to_dev_t and replace
39187c478bd9Sstevel@tonic-gate 		 * it, and all references, with a call that specifies a dipp.
39197c478bd9Sstevel@tonic-gate 		 * In addition, the callers of this new interfaces would then
39207c478bd9Sstevel@tonic-gate 		 * need to call ndi_rele_devi when the reference is complete.
392120906b23SVikram Hegde 		 *
39227c478bd9Sstevel@tonic-gate 		 */
39237c478bd9Sstevel@tonic-gate 		(void) ddi_prop_update_int(DDI_DEV_T_NONE, parent,
39247c478bd9Sstevel@tonic-gate 		    DDI_NO_AUTODETACH, 1);
39257c478bd9Sstevel@tonic-gate 		ndi_rele_devi(parent);
39267c478bd9Sstevel@tonic-gate 	}
39277c478bd9Sstevel@tonic-gate 
39287c478bd9Sstevel@tonic-gate 	return (0);
39297c478bd9Sstevel@tonic-gate }
39307c478bd9Sstevel@tonic-gate 
39317c478bd9Sstevel@tonic-gate /*
39327c478bd9Sstevel@tonic-gate  * Given the pathname of a device, return the dev_t of the corresponding
39337c478bd9Sstevel@tonic-gate  * device.  Returns NODEV on failure.
39347c478bd9Sstevel@tonic-gate  *
39357c478bd9Sstevel@tonic-gate  * Note that this call sets the DDI_NO_AUTODETACH property on the devinfo node.
39367c478bd9Sstevel@tonic-gate  */
39377c478bd9Sstevel@tonic-gate dev_t
39387c478bd9Sstevel@tonic-gate ddi_pathname_to_dev_t(char *pathname)
39397c478bd9Sstevel@tonic-gate {
39407c478bd9Sstevel@tonic-gate 	dev_t devt;
39417c478bd9Sstevel@tonic-gate 	int error;
39427c478bd9Sstevel@tonic-gate 
39437c478bd9Sstevel@tonic-gate 	error = resolve_pathname(pathname, NULL, &devt, NULL);
39447c478bd9Sstevel@tonic-gate 
39457c478bd9Sstevel@tonic-gate 	return (error ? NODEV : devt);
39467c478bd9Sstevel@tonic-gate }
39477c478bd9Sstevel@tonic-gate 
39487c478bd9Sstevel@tonic-gate /*
39497c478bd9Sstevel@tonic-gate  * Translate a prom pathname to kernel devfs pathname.
39507c478bd9Sstevel@tonic-gate  * Caller is assumed to allocate devfspath memory of
39517c478bd9Sstevel@tonic-gate  * size at least MAXPATHLEN
39527c478bd9Sstevel@tonic-gate  *
39537c478bd9Sstevel@tonic-gate  * The prom pathname may not include minor name, but
39547c478bd9Sstevel@tonic-gate  * devfs pathname has a minor name portion.
39557c478bd9Sstevel@tonic-gate  */
39567c478bd9Sstevel@tonic-gate int
39577c478bd9Sstevel@tonic-gate i_ddi_prompath_to_devfspath(char *prompath, char *devfspath)
39587c478bd9Sstevel@tonic-gate {
39597c478bd9Sstevel@tonic-gate 	dev_t		devt = (dev_t)NODEV;
39607c478bd9Sstevel@tonic-gate 	dev_info_t	*dip = NULL;
39617c478bd9Sstevel@tonic-gate 	char		*minor_name = NULL;
39627c478bd9Sstevel@tonic-gate 	int		spectype;
39637c478bd9Sstevel@tonic-gate 	int		error;
3964b9ccdc5aScth 	int		circ;
39657c478bd9Sstevel@tonic-gate 
39667c478bd9Sstevel@tonic-gate 	error = resolve_pathname(prompath, &dip, &devt, &spectype);
39677c478bd9Sstevel@tonic-gate 	if (error)
39687c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
39697c478bd9Sstevel@tonic-gate 	ASSERT(dip && devt != NODEV);
39707c478bd9Sstevel@tonic-gate 
39717c478bd9Sstevel@tonic-gate 	/*
39727c478bd9Sstevel@tonic-gate 	 * Get in-kernel devfs pathname
39737c478bd9Sstevel@tonic-gate 	 */
39747c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(dip, devfspath);
39757c478bd9Sstevel@tonic-gate 
3976b9ccdc5aScth 	ndi_devi_enter(dip, &circ);
39777c478bd9Sstevel@tonic-gate 	minor_name = i_ddi_devtspectype_to_minorname(dip, devt, spectype);
39787c478bd9Sstevel@tonic-gate 	if (minor_name) {
39797c478bd9Sstevel@tonic-gate 		(void) strcat(devfspath, ":");
39807c478bd9Sstevel@tonic-gate 		(void) strcat(devfspath, minor_name);
39817c478bd9Sstevel@tonic-gate 	} else {
39827c478bd9Sstevel@tonic-gate 		/*
39837c478bd9Sstevel@tonic-gate 		 * If minor_name is NULL, we have an alias minor node.
39847c478bd9Sstevel@tonic-gate 		 * So manufacture a path to the corresponding clone minor.
39857c478bd9Sstevel@tonic-gate 		 */
39867c478bd9Sstevel@tonic-gate 		(void) snprintf(devfspath, MAXPATHLEN, "%s:%s",
39877c478bd9Sstevel@tonic-gate 		    CLONE_PATH, ddi_driver_name(dip));
39887c478bd9Sstevel@tonic-gate 	}
3989b9ccdc5aScth 	ndi_devi_exit(dip, circ);
39907c478bd9Sstevel@tonic-gate 
39917c478bd9Sstevel@tonic-gate 	/* release hold from resolve_pathname() */
39927c478bd9Sstevel@tonic-gate 	ndi_rele_devi(dip);
39937c478bd9Sstevel@tonic-gate 	return (0);
39947c478bd9Sstevel@tonic-gate }
39957c478bd9Sstevel@tonic-gate 
39967c478bd9Sstevel@tonic-gate /*
399719397407SSherry Moore  * This function is intended to identify drivers that must quiesce for fast
399819397407SSherry Moore  * reboot to succeed.  It does not claim to have more knowledge about the device
399919397407SSherry Moore  * than its driver.  If a driver has implemented quiesce(), it will be invoked;
400019397407SSherry Moore  * if a so identified driver does not manage any device that needs to be
400119397407SSherry Moore  * quiesced, it must explicitly set its devo_quiesce dev_op to
400219397407SSherry Moore  * ddi_quiesce_not_needed.
400319397407SSherry Moore  */
400419397407SSherry Moore static int skip_pseudo = 1;	/* Skip pseudo devices */
400519397407SSherry Moore static int skip_non_hw = 1;	/* Skip devices with no hardware property */
400619397407SSherry Moore static int
400719397407SSherry Moore should_implement_quiesce(dev_info_t *dip)
400819397407SSherry Moore {
400919397407SSherry Moore 	struct dev_info *devi = DEVI(dip);
401019397407SSherry Moore 	dev_info_t *pdip;
401119397407SSherry Moore 
401219397407SSherry Moore 	/*
401319397407SSherry Moore 	 * If dip is pseudo and skip_pseudo is set, driver doesn't have to
401419397407SSherry Moore 	 * implement quiesce().
401519397407SSherry Moore 	 */
401619397407SSherry Moore 	if (skip_pseudo &&
401719397407SSherry Moore 	    strncmp(ddi_binding_name(dip), "pseudo", sizeof ("pseudo")) == 0)
401819397407SSherry Moore 		return (0);
401919397407SSherry Moore 
402019397407SSherry Moore 	/*
402119397407SSherry Moore 	 * If parent dip is pseudo and skip_pseudo is set, driver doesn't have
402219397407SSherry Moore 	 * to implement quiesce().
402319397407SSherry Moore 	 */
402419397407SSherry Moore 	if (skip_pseudo && (pdip = ddi_get_parent(dip)) != NULL &&
402519397407SSherry Moore 	    strncmp(ddi_binding_name(pdip), "pseudo", sizeof ("pseudo")) == 0)
402619397407SSherry Moore 		return (0);
402719397407SSherry Moore 
402819397407SSherry Moore 	/*
402919397407SSherry Moore 	 * If not attached, driver doesn't have to implement quiesce().
403019397407SSherry Moore 	 */
403119397407SSherry Moore 	if (!i_ddi_devi_attached(dip))
403219397407SSherry Moore 		return (0);
403319397407SSherry Moore 
403419397407SSherry Moore 	/*
403519397407SSherry Moore 	 * If dip has no hardware property and skip_non_hw is set,
403619397407SSherry Moore 	 * driver doesn't have to implement quiesce().
403719397407SSherry Moore 	 */
403819397407SSherry Moore 	if (skip_non_hw && devi->devi_hw_prop_ptr == NULL)
403919397407SSherry Moore 		return (0);
404019397407SSherry Moore 
404119397407SSherry Moore 	return (1);
404219397407SSherry Moore }
404319397407SSherry Moore 
404419397407SSherry Moore static int
404519397407SSherry Moore driver_has_quiesce(struct dev_ops *ops)
404619397407SSherry Moore {
404719397407SSherry Moore 	if ((ops->devo_rev >= 4) && (ops->devo_quiesce != nodev) &&
404819397407SSherry Moore 	    (ops->devo_quiesce != NULL) && (ops->devo_quiesce != nulldev) &&
404919397407SSherry Moore 	    (ops->devo_quiesce != ddi_quiesce_not_supported))
405019397407SSherry Moore 		return (1);
405119397407SSherry Moore 	else
405219397407SSherry Moore 		return (0);
405319397407SSherry Moore }
405419397407SSherry Moore 
405519397407SSherry Moore /*
405619397407SSherry Moore  * Check to see if a driver has implemented the quiesce() DDI function.
405719397407SSherry Moore  */
405819397407SSherry Moore int
405919397407SSherry Moore check_driver_quiesce(dev_info_t *dip, void *arg)
406019397407SSherry Moore {
406119397407SSherry Moore 	struct dev_ops *ops;
406219397407SSherry Moore 
406319397407SSherry Moore 	if (!should_implement_quiesce(dip))
406419397407SSherry Moore 		return (DDI_WALK_CONTINUE);
406519397407SSherry Moore 
406619397407SSherry Moore 	if ((ops = ddi_get_driver(dip)) == NULL)
406719397407SSherry Moore 		return (DDI_WALK_CONTINUE);
406819397407SSherry Moore 
406919397407SSherry Moore 	if (driver_has_quiesce(ops)) {
407019397407SSherry Moore 		if ((quiesce_debug & 0x2) == 0x2) {
407119397407SSherry Moore 			if (ops->devo_quiesce == ddi_quiesce_not_needed)
407219397407SSherry Moore 				cmn_err(CE_CONT, "%s does not need to be "
407319397407SSherry Moore 				    "quiesced", ddi_driver_name(dip));
407419397407SSherry Moore 			else
407519397407SSherry Moore 				cmn_err(CE_CONT, "%s has quiesce routine",
407619397407SSherry Moore 				    ddi_driver_name(dip));
407719397407SSherry Moore 		}
407819397407SSherry Moore 	} else {
407919397407SSherry Moore 		if (arg != NULL)
408019397407SSherry Moore 			*((int *)arg) = -1;
408119397407SSherry Moore 		cmn_err(CE_WARN, "%s has no quiesce()", ddi_driver_name(dip));
408219397407SSherry Moore 	}
408319397407SSherry Moore 
408419397407SSherry Moore 	return (DDI_WALK_CONTINUE);
408519397407SSherry Moore }
408619397407SSherry Moore 
408719397407SSherry Moore /*
408819397407SSherry Moore  * Quiesce device.
408919397407SSherry Moore  */
409019397407SSherry Moore static void
409119397407SSherry Moore quiesce_one_device(dev_info_t *dip, void *arg)
409219397407SSherry Moore {
409319397407SSherry Moore 	struct dev_ops *ops;
409419397407SSherry Moore 	int should_quiesce = 0;
409519397407SSherry Moore 
409619397407SSherry Moore 	/*
409719397407SSherry Moore 	 * If the device is not attached it doesn't need to be quiesced.
409819397407SSherry Moore 	 */
409919397407SSherry Moore 	if (!i_ddi_devi_attached(dip))
410019397407SSherry Moore 		return;
410119397407SSherry Moore 
410219397407SSherry Moore 	if ((ops = ddi_get_driver(dip)) == NULL)
410319397407SSherry Moore 		return;
410419397407SSherry Moore 
410519397407SSherry Moore 	should_quiesce = should_implement_quiesce(dip);
410619397407SSherry Moore 
410719397407SSherry Moore 	/*
410819397407SSherry Moore 	 * If there's an implementation of quiesce(), always call it even if
410919397407SSherry Moore 	 * some of the drivers don't have quiesce() or quiesce() have failed
411019397407SSherry Moore 	 * so we can do force fast reboot.  The implementation of quiesce()
411119397407SSherry Moore 	 * should not negatively affect a regular reboot.
411219397407SSherry Moore 	 */
411319397407SSherry Moore 	if (driver_has_quiesce(ops)) {
411419397407SSherry Moore 		int rc = DDI_SUCCESS;
411519397407SSherry Moore 
411619397407SSherry Moore 		if (ops->devo_quiesce == ddi_quiesce_not_needed)
411719397407SSherry Moore 			return;
411819397407SSherry Moore 
411919397407SSherry Moore 		rc = devi_quiesce(dip);
412019397407SSherry Moore 
412119397407SSherry Moore 		/* quiesce() should never fail */
412219397407SSherry Moore 		ASSERT(rc == DDI_SUCCESS);
412319397407SSherry Moore 
412419397407SSherry Moore 		if (rc != DDI_SUCCESS && should_quiesce) {
412519397407SSherry Moore 
412619397407SSherry Moore 			if (arg != NULL)
412719397407SSherry Moore 				*((int *)arg) = -1;
412819397407SSherry Moore 		}
412919397407SSherry Moore 	} else if (should_quiesce && arg != NULL) {
413019397407SSherry Moore 		*((int *)arg) = -1;
413119397407SSherry Moore 	}
413219397407SSherry Moore }
413319397407SSherry Moore 
413419397407SSherry Moore /*
413519397407SSherry Moore  * Traverse the dev info tree in a breadth-first manner so that we quiesce
413619397407SSherry Moore  * children first.  All subtrees under the parent of dip will be quiesced.
413719397407SSherry Moore  */
413819397407SSherry Moore void
413919397407SSherry Moore quiesce_devices(dev_info_t *dip, void *arg)
414019397407SSherry Moore {
414119397407SSherry Moore 	/*
414219397407SSherry Moore 	 * if we're reached here, the device tree better not be changing.
414319397407SSherry Moore 	 * so either devinfo_freeze better be set or we better be panicing.
414419397407SSherry Moore 	 */
414519397407SSherry Moore 	ASSERT(devinfo_freeze || panicstr);
414619397407SSherry Moore 
414719397407SSherry Moore 	for (; dip != NULL; dip = ddi_get_next_sibling(dip)) {
414819397407SSherry Moore 		quiesce_devices(ddi_get_child(dip), arg);
414919397407SSherry Moore 
415019397407SSherry Moore 		quiesce_one_device(dip, arg);
415119397407SSherry Moore 	}
415219397407SSherry Moore }
415319397407SSherry Moore 
415419397407SSherry Moore /*
41557c478bd9Sstevel@tonic-gate  * Reset all the pure leaf drivers on the system at halt time
41567c478bd9Sstevel@tonic-gate  */
41577c478bd9Sstevel@tonic-gate static int
41587c478bd9Sstevel@tonic-gate reset_leaf_device(dev_info_t *dip, void *arg)
41597c478bd9Sstevel@tonic-gate {
41607c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(arg))
41617c478bd9Sstevel@tonic-gate 	struct dev_ops *ops;
41627c478bd9Sstevel@tonic-gate 
41637c478bd9Sstevel@tonic-gate 	/* if the device doesn't need to be reset then there's nothing to do */
41647c478bd9Sstevel@tonic-gate 	if (!DEVI_NEED_RESET(dip))
41657c478bd9Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
41667c478bd9Sstevel@tonic-gate 
41677c478bd9Sstevel@tonic-gate 	/*
41687c478bd9Sstevel@tonic-gate 	 * if the device isn't a char/block device or doesn't have a
41697c478bd9Sstevel@tonic-gate 	 * reset entry point then there's nothing to do.
41707c478bd9Sstevel@tonic-gate 	 */
41717c478bd9Sstevel@tonic-gate 	ops = ddi_get_driver(dip);
41727c478bd9Sstevel@tonic-gate 	if ((ops == NULL) || (ops->devo_cb_ops == NULL) ||
41737c478bd9Sstevel@tonic-gate 	    (ops->devo_reset == nodev) || (ops->devo_reset == nulldev) ||
41747c478bd9Sstevel@tonic-gate 	    (ops->devo_reset == NULL))
41757c478bd9Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
41767c478bd9Sstevel@tonic-gate 
41777c478bd9Sstevel@tonic-gate 	if (DEVI_IS_ATTACHING(dip) || DEVI_IS_DETACHING(dip)) {
41787c478bd9Sstevel@tonic-gate 		static char path[MAXPATHLEN];
41797c478bd9Sstevel@tonic-gate 
41807c478bd9Sstevel@tonic-gate 		/*
41817c478bd9Sstevel@tonic-gate 		 * bad news, this device has blocked in it's attach or
41827c478bd9Sstevel@tonic-gate 		 * detach routine, which means it not safe to call it's
41837c478bd9Sstevel@tonic-gate 		 * devo_reset() entry point.
41847c478bd9Sstevel@tonic-gate 		 */
41857c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "unable to reset device: %s",
41867c478bd9Sstevel@tonic-gate 		    ddi_pathname(dip, path));
41877c478bd9Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
41887c478bd9Sstevel@tonic-gate 	}
41897c478bd9Sstevel@tonic-gate 
41907c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_NOTE, "resetting %s%d\n",
41917c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip)));
41927c478bd9Sstevel@tonic-gate 
41937c478bd9Sstevel@tonic-gate 	(void) devi_reset(dip, DDI_RESET_FORCE);
41947c478bd9Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
41957c478bd9Sstevel@tonic-gate }
41967c478bd9Sstevel@tonic-gate 
41977c478bd9Sstevel@tonic-gate void
41987c478bd9Sstevel@tonic-gate reset_leaves(void)
41997c478bd9Sstevel@tonic-gate {
42007c478bd9Sstevel@tonic-gate 	/*
42017c478bd9Sstevel@tonic-gate 	 * if we're reached here, the device tree better not be changing.
42027c478bd9Sstevel@tonic-gate 	 * so either devinfo_freeze better be set or we better be panicing.
42037c478bd9Sstevel@tonic-gate 	 */
42047c478bd9Sstevel@tonic-gate 	ASSERT(devinfo_freeze || panicstr);
42057c478bd9Sstevel@tonic-gate 
42067c478bd9Sstevel@tonic-gate 	(void) walk_devs(top_devinfo, reset_leaf_device, NULL, 0);
42077c478bd9Sstevel@tonic-gate }
42087c478bd9Sstevel@tonic-gate 
420919397407SSherry Moore 
42107c478bd9Sstevel@tonic-gate /*
421119397407SSherry Moore  * devtree_freeze() must be called before quiesce_devices() and reset_leaves()
421219397407SSherry Moore  * during a normal system shutdown.  It attempts to ensure that there are no
421319397407SSherry Moore  * outstanding attach or detach operations in progress when quiesce_devices() or
421419397407SSherry Moore  * reset_leaves()is invoked.  It must be called before the system becomes
421519397407SSherry Moore  * single-threaded because device attach and detach are multi-threaded
421619397407SSherry Moore  * operations.	(note that during system shutdown the system doesn't actually
421719397407SSherry Moore  * become single-thread since other threads still exist, but the shutdown thread
421819397407SSherry Moore  * will disable preemption for itself, raise it's pil, and stop all the other
421919397407SSherry Moore  * cpus in the system there by effectively making the system single-threaded.)
42207c478bd9Sstevel@tonic-gate  */
42217c478bd9Sstevel@tonic-gate void
42227c478bd9Sstevel@tonic-gate devtree_freeze(void)
42237c478bd9Sstevel@tonic-gate {
42247c478bd9Sstevel@tonic-gate 	int delayed = 0;
42257c478bd9Sstevel@tonic-gate 
42267c478bd9Sstevel@tonic-gate 	/* if we're panicing then the device tree isn't going to be changing */
42277c478bd9Sstevel@tonic-gate 	if (panicstr)
42287c478bd9Sstevel@tonic-gate 		return;
42297c478bd9Sstevel@tonic-gate 
42307c478bd9Sstevel@tonic-gate 	/* stop all dev_info state changes in the device tree */
42317c478bd9Sstevel@tonic-gate 	devinfo_freeze = gethrtime();
42327c478bd9Sstevel@tonic-gate 
42337c478bd9Sstevel@tonic-gate 	/*
42347c478bd9Sstevel@tonic-gate 	 * if we're not panicing and there are on-going attach or detach
42357c478bd9Sstevel@tonic-gate 	 * operations, wait for up to 3 seconds for them to finish.  This
42367c478bd9Sstevel@tonic-gate 	 * is a randomly chosen interval but this should be ok because:
42377c478bd9Sstevel@tonic-gate 	 * - 3 seconds is very small relative to the deadman timer.
42387c478bd9Sstevel@tonic-gate 	 * - normal attach and detach operations should be very quick.
42397c478bd9Sstevel@tonic-gate 	 * - attach and detach operations are fairly rare.
42407c478bd9Sstevel@tonic-gate 	 */
42417c478bd9Sstevel@tonic-gate 	while (!panicstr && atomic_add_long_nv(&devinfo_attach_detach, 0) &&
42427c478bd9Sstevel@tonic-gate 	    (delayed < 3)) {
42437c478bd9Sstevel@tonic-gate 		delayed += 1;
42447c478bd9Sstevel@tonic-gate 
42457c478bd9Sstevel@tonic-gate 		/* do a sleeping wait for one second */
42467c478bd9Sstevel@tonic-gate 		ASSERT(!servicing_interrupt());
42477c478bd9Sstevel@tonic-gate 		delay(drv_usectohz(MICROSEC));
42487c478bd9Sstevel@tonic-gate 	}
42497c478bd9Sstevel@tonic-gate }
42507c478bd9Sstevel@tonic-gate 
42517c478bd9Sstevel@tonic-gate static int
42527c478bd9Sstevel@tonic-gate bind_dip(dev_info_t *dip, void *arg)
42537c478bd9Sstevel@tonic-gate {
42547c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(arg))
4255f4da9be0Scth 	char	*path;
4256f4da9be0Scth 	major_t	major, pmajor;
4257f4da9be0Scth 
4258f4da9be0Scth 	/*
4259f4da9be0Scth 	 * If the node is currently bound to the wrong driver, try to unbind
4260f4da9be0Scth 	 * so that we can rebind to the correct driver.
4261f4da9be0Scth 	 */
4262f4da9be0Scth 	if (i_ddi_node_state(dip) >= DS_BOUND) {
4263f4da9be0Scth 		major = ddi_compatible_driver_major(dip, NULL);
4264f4da9be0Scth 		if ((DEVI(dip)->devi_major == major) &&
4265f4da9be0Scth 		    (i_ddi_node_state(dip) >= DS_INITIALIZED)) {
4266f4da9be0Scth 			/*
4267f4da9be0Scth 			 * Check for a path-oriented driver alias that
4268f4da9be0Scth 			 * takes precedence over current driver binding.
4269f4da9be0Scth 			 */
4270f4da9be0Scth 			path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4271f4da9be0Scth 			(void) ddi_pathname(dip, path);
4272f4da9be0Scth 			pmajor = ddi_name_to_major(path);
4273c9cc1492SJerry Gilliam 			if (driver_installed(pmajor))
4274f4da9be0Scth 				major = pmajor;
4275f4da9be0Scth 			kmem_free(path, MAXPATHLEN);
4276f4da9be0Scth 		}
4277f4da9be0Scth 
4278f4da9be0Scth 		/* attempt unbind if current driver is incorrect */
4279c9cc1492SJerry Gilliam 		if (driver_installed(major) &&
4280f4da9be0Scth 		    (major != DEVI(dip)->devi_major))
4281f4da9be0Scth 			(void) ndi_devi_unbind_driver(dip);
4282f4da9be0Scth 	}
4283f4da9be0Scth 
4284f4da9be0Scth 	/* If unbound, try to bind to a driver */
42857c478bd9Sstevel@tonic-gate 	if (i_ddi_node_state(dip) < DS_BOUND)
42867c478bd9Sstevel@tonic-gate 		(void) ndi_devi_bind_driver(dip, 0);
42877c478bd9Sstevel@tonic-gate 
42887c478bd9Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
42897c478bd9Sstevel@tonic-gate }
42907c478bd9Sstevel@tonic-gate 
42917c478bd9Sstevel@tonic-gate void
42927c478bd9Sstevel@tonic-gate i_ddi_bind_devs(void)
42937c478bd9Sstevel@tonic-gate {
4294f4da9be0Scth 	/* flush devfs so that ndi_devi_unbind_driver will work when possible */
4295f4da9be0Scth 	(void) devfs_clean(top_devinfo, NULL, 0);
4296f4da9be0Scth 
42977c478bd9Sstevel@tonic-gate 	ddi_walk_devs(top_devinfo, bind_dip, (void *)NULL);
42987c478bd9Sstevel@tonic-gate }
42997c478bd9Sstevel@tonic-gate 
43006532b960SJerry Gilliam /* callback data for unbind_children_by_alias() */
43016532b960SJerry Gilliam typedef struct unbind_data {
43026532b960SJerry Gilliam 	major_t	drv_major;
43036532b960SJerry Gilliam 	char	*drv_alias;
43046532b960SJerry Gilliam 	int	ndevs_bound;
43056532b960SJerry Gilliam 	int	unbind_errors;
43066532b960SJerry Gilliam } unbind_data_t;
43076532b960SJerry Gilliam 
43086532b960SJerry Gilliam /*
43096532b960SJerry Gilliam  * A utility function provided for testing and support convenience
43106532b960SJerry Gilliam  * Called for each device during an upgrade_drv -d bound to the alias
43116532b960SJerry Gilliam  * that cannot be unbound due to device in use.
43126532b960SJerry Gilliam  */
43136532b960SJerry Gilliam static void
43146532b960SJerry Gilliam unbind_alias_dev_in_use(dev_info_t *dip, char *alias)
43156532b960SJerry Gilliam {
43166532b960SJerry Gilliam 	if (moddebug & MODDEBUG_BINDING) {
43176532b960SJerry Gilliam 		cmn_err(CE_CONT, "%s%d: state %d: bound to %s\n",
43186532b960SJerry Gilliam 		    ddi_driver_name(dip), ddi_get_instance(dip),
43196532b960SJerry Gilliam 		    i_ddi_node_state(dip), alias);
43206532b960SJerry Gilliam 	}
43216532b960SJerry Gilliam }
43226532b960SJerry Gilliam 
43236532b960SJerry Gilliam /*
43246532b960SJerry Gilliam  * walkdevs callback for unbind devices bound to specific driver
43256532b960SJerry Gilliam  * and alias.  Invoked within the context of update_drv -d <alias>.
43266532b960SJerry Gilliam  */
43277c478bd9Sstevel@tonic-gate static int
43286532b960SJerry Gilliam unbind_children_by_alias(dev_info_t *dip, void *arg)
43297c478bd9Sstevel@tonic-gate {
43307c478bd9Sstevel@tonic-gate 	int		circ;
43317c478bd9Sstevel@tonic-gate 	dev_info_t	*cdip;
43326532b960SJerry Gilliam 	dev_info_t	*next;
43336532b960SJerry Gilliam 	unbind_data_t	*ub = (unbind_data_t *)(uintptr_t)arg;
43346532b960SJerry Gilliam 	int		rv;
43357c478bd9Sstevel@tonic-gate 
43367c478bd9Sstevel@tonic-gate 	/*
43376532b960SJerry Gilliam 	 * We are called from update_drv to try to unbind a specific
43386532b960SJerry Gilliam 	 * set of aliases for a driver.  Unbind what persistent nodes
43396532b960SJerry Gilliam 	 * we can, and return the number of nodes which cannot be unbound.
43406532b960SJerry Gilliam 	 * If not all nodes can be unbound, update_drv leaves the
43416532b960SJerry Gilliam 	 * state of the driver binding files unchanged, except in
43426532b960SJerry Gilliam 	 * the case of -f.
43437c478bd9Sstevel@tonic-gate 	 */
43446532b960SJerry Gilliam 	ndi_devi_enter(dip, &circ);
43456532b960SJerry Gilliam 	for (cdip = ddi_get_child(dip); cdip; cdip = next) {
43466532b960SJerry Gilliam 		next = ddi_get_next_sibling(cdip);
43476532b960SJerry Gilliam 		if ((ddi_driver_major(cdip) != ub->drv_major) ||
43486532b960SJerry Gilliam 		    (strcmp(DEVI(cdip)->devi_node_name, ub->drv_alias) != 0))
43496532b960SJerry Gilliam 			continue;
43506532b960SJerry Gilliam 		if (i_ddi_node_state(cdip) >= DS_BOUND) {
43516532b960SJerry Gilliam 			rv = ndi_devi_unbind_driver(cdip);
43526532b960SJerry Gilliam 			if (rv != DDI_SUCCESS ||
43536532b960SJerry Gilliam 			    (i_ddi_node_state(cdip) >= DS_BOUND)) {
43546532b960SJerry Gilliam 				unbind_alias_dev_in_use(cdip, ub->drv_alias);
43556532b960SJerry Gilliam 				ub->ndevs_bound++;
43567c478bd9Sstevel@tonic-gate 				continue;
43577c478bd9Sstevel@tonic-gate 			}
43587c478bd9Sstevel@tonic-gate 			if (ndi_dev_is_persistent_node(cdip) == 0)
43597c478bd9Sstevel@tonic-gate 				(void) ddi_remove_child(cdip, 0);
43606532b960SJerry Gilliam 		}
43617c478bd9Sstevel@tonic-gate 	}
43627c478bd9Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
43637c478bd9Sstevel@tonic-gate 
43647c478bd9Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
43657c478bd9Sstevel@tonic-gate }
43667c478bd9Sstevel@tonic-gate 
43676532b960SJerry Gilliam /*
43686532b960SJerry Gilliam  * Unbind devices by driver & alias
43696532b960SJerry Gilliam  * Context: update_drv [-f] -d -i <alias> <driver>
43706532b960SJerry Gilliam  */
43716532b960SJerry Gilliam int
43726532b960SJerry Gilliam i_ddi_unbind_devs_by_alias(major_t major, char *alias)
43736532b960SJerry Gilliam {
43746532b960SJerry Gilliam 	unbind_data_t	*ub;
43756532b960SJerry Gilliam 	int		rv;
43766532b960SJerry Gilliam 
43776532b960SJerry Gilliam 	ub = kmem_zalloc(sizeof (*ub), KM_SLEEP);
43786532b960SJerry Gilliam 	ub->drv_major = major;
43796532b960SJerry Gilliam 	ub->drv_alias = alias;
43806532b960SJerry Gilliam 	ub->ndevs_bound = 0;
43816532b960SJerry Gilliam 	ub->unbind_errors = 0;
43826532b960SJerry Gilliam 
43836532b960SJerry Gilliam 	/* flush devfs so that ndi_devi_unbind_driver will work when possible */
438400931d94SJerry Gilliam 	(void) devfs_clean(top_devinfo, NULL, 0);
43856532b960SJerry Gilliam 	ddi_walk_devs(top_devinfo, unbind_children_by_alias,
43866532b960SJerry Gilliam 	    (void *)(uintptr_t)ub);
43876532b960SJerry Gilliam 
43886532b960SJerry Gilliam 	/* return the number of devices remaining bound to the alias */
43896532b960SJerry Gilliam 	rv = ub->ndevs_bound + ub->unbind_errors;
43906532b960SJerry Gilliam 	kmem_free(ub, sizeof (*ub));
43916532b960SJerry Gilliam 	return (rv);
43926532b960SJerry Gilliam }
43936532b960SJerry Gilliam 
43946532b960SJerry Gilliam /*
43956532b960SJerry Gilliam  * walkdevs callback for unbind devices by driver
43966532b960SJerry Gilliam  */
43976532b960SJerry Gilliam static int
43986532b960SJerry Gilliam unbind_children_by_driver(dev_info_t *dip, void *arg)
43996532b960SJerry Gilliam {
44006532b960SJerry Gilliam 	int		circ;
44016532b960SJerry Gilliam 	dev_info_t	*cdip;
44026532b960SJerry Gilliam 	dev_info_t	*next;
44036532b960SJerry Gilliam 	major_t		major = (major_t)(uintptr_t)arg;
44046532b960SJerry Gilliam 	int		rv;
44056532b960SJerry Gilliam 
44066532b960SJerry Gilliam 	/*
44076532b960SJerry Gilliam 	 * We are called either from rem_drv or update_drv when reloading
44086532b960SJerry Gilliam 	 * a driver.conf file. In either case, we unbind persistent nodes
44096532b960SJerry Gilliam 	 * and destroy .conf nodes. In the case of rem_drv, this will be
44106532b960SJerry Gilliam 	 * the final state. In the case of update_drv,	i_ddi_bind_devs()
44116532b960SJerry Gilliam 	 * may be invoked later to re-enumerate (new) driver.conf rebind
44126532b960SJerry Gilliam 	 * persistent nodes.
44136532b960SJerry Gilliam 	 */
44146532b960SJerry Gilliam 	ndi_devi_enter(dip, &circ);
44156532b960SJerry Gilliam 	for (cdip = ddi_get_child(dip); cdip; cdip = next) {
44166532b960SJerry Gilliam 		next = ddi_get_next_sibling(cdip);
44176532b960SJerry Gilliam 		if (ddi_driver_major(cdip) != major)
44186532b960SJerry Gilliam 			continue;
44196532b960SJerry Gilliam 		if (i_ddi_node_state(cdip) >= DS_BOUND) {
44206532b960SJerry Gilliam 			rv = ndi_devi_unbind_driver(cdip);
44216532b960SJerry Gilliam 			if (rv == DDI_FAILURE ||
44226532b960SJerry Gilliam 			    (i_ddi_node_state(cdip) >= DS_BOUND))
44236532b960SJerry Gilliam 				continue;
44246532b960SJerry Gilliam 			if (ndi_dev_is_persistent_node(cdip) == 0)
44256532b960SJerry Gilliam 				(void) ddi_remove_child(cdip, 0);
44266532b960SJerry Gilliam 		}
44276532b960SJerry Gilliam 	}
44286532b960SJerry Gilliam 	ndi_devi_exit(dip, circ);
44296532b960SJerry Gilliam 
44306532b960SJerry Gilliam 	return (DDI_WALK_CONTINUE);
44316532b960SJerry Gilliam }
44326532b960SJerry Gilliam 
44336532b960SJerry Gilliam /*
44346532b960SJerry Gilliam  * Unbind devices by driver
44356532b960SJerry Gilliam  * Context: rem_drv or unload driver.conf
44366532b960SJerry Gilliam  */
44377c478bd9Sstevel@tonic-gate void
44387c478bd9Sstevel@tonic-gate i_ddi_unbind_devs(major_t major)
44397c478bd9Sstevel@tonic-gate {
44406532b960SJerry Gilliam 	/* flush devfs so that ndi_devi_unbind_driver will work when possible */
444100931d94SJerry Gilliam 	(void) devfs_clean(top_devinfo, NULL, 0);
44426532b960SJerry Gilliam 	ddi_walk_devs(top_devinfo, unbind_children_by_driver,
44436532b960SJerry Gilliam 	    (void *)(uintptr_t)major);
44447c478bd9Sstevel@tonic-gate }
44457c478bd9Sstevel@tonic-gate 
44467c478bd9Sstevel@tonic-gate /*
44477c478bd9Sstevel@tonic-gate  * I/O Hotplug control
44487c478bd9Sstevel@tonic-gate  */
44497c478bd9Sstevel@tonic-gate 
44507c478bd9Sstevel@tonic-gate /*
44517c478bd9Sstevel@tonic-gate  * create and attach a dev_info node from a .conf file spec
44527c478bd9Sstevel@tonic-gate  */
44537c478bd9Sstevel@tonic-gate static void
44547c478bd9Sstevel@tonic-gate init_spec_child(dev_info_t *pdip, struct hwc_spec *specp, uint_t flags)
44557c478bd9Sstevel@tonic-gate {
44567c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags))
44577c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
44587c478bd9Sstevel@tonic-gate 	char *node_name;
44597c478bd9Sstevel@tonic-gate 
44607c478bd9Sstevel@tonic-gate 	if (((node_name = specp->hwc_devi_name) == NULL) ||
4461a204de77Scth 	    (ddi_name_to_major(node_name) == DDI_MAJOR_T_NONE)) {
44627c478bd9Sstevel@tonic-gate 		char *tmp = node_name;
44637c478bd9Sstevel@tonic-gate 		if (tmp == NULL)
44647c478bd9Sstevel@tonic-gate 			tmp = "<none>";
44657c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT,
44667c478bd9Sstevel@tonic-gate 		    "init_spec_child: parent=%s, bad spec (%s)\n",
44677c478bd9Sstevel@tonic-gate 		    ddi_node_name(pdip), tmp);
44687c478bd9Sstevel@tonic-gate 		return;
44697c478bd9Sstevel@tonic-gate 	}
44707c478bd9Sstevel@tonic-gate 
4471fa9e4066Sahrens 	dip = i_ddi_alloc_node(pdip, node_name, (pnode_t)DEVI_PSEUDO_NODEID,
44727c478bd9Sstevel@tonic-gate 	    -1, specp->hwc_devi_sys_prop_ptr, KM_SLEEP);
44737c478bd9Sstevel@tonic-gate 
44747c478bd9Sstevel@tonic-gate 	if (dip == NULL)
44757c478bd9Sstevel@tonic-gate 		return;
44767c478bd9Sstevel@tonic-gate 
44777c478bd9Sstevel@tonic-gate 	if (ddi_initchild(pdip, dip) != DDI_SUCCESS)
44787c478bd9Sstevel@tonic-gate 		(void) ddi_remove_child(dip, 0);
44797c478bd9Sstevel@tonic-gate }
44807c478bd9Sstevel@tonic-gate 
44817c478bd9Sstevel@tonic-gate /*
44827c478bd9Sstevel@tonic-gate  * Lookup hwc specs from hash tables and make children from the spec
44837c478bd9Sstevel@tonic-gate  * Because some .conf children are "merge" nodes, we also initialize
44847c478bd9Sstevel@tonic-gate  * .conf children to merge properties onto hardware nodes.
44857c478bd9Sstevel@tonic-gate  *
44867c478bd9Sstevel@tonic-gate  * The pdip must be held busy.
44877c478bd9Sstevel@tonic-gate  */
44887c478bd9Sstevel@tonic-gate int
44897c478bd9Sstevel@tonic-gate i_ndi_make_spec_children(dev_info_t *pdip, uint_t flags)
44907c478bd9Sstevel@tonic-gate {
44917c478bd9Sstevel@tonic-gate 	extern struct hwc_spec *hwc_get_child_spec(dev_info_t *, major_t);
44926a41d557Scth 	int			circ;
44937c478bd9Sstevel@tonic-gate 	struct hwc_spec		*list, *spec;
44947c478bd9Sstevel@tonic-gate 
44956a41d557Scth 	ndi_devi_enter(pdip, &circ);
44966a41d557Scth 	if (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN) {
44976a41d557Scth 		ndi_devi_exit(pdip, circ);
44987c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
44996a41d557Scth 	}
45007c478bd9Sstevel@tonic-gate 
4501a204de77Scth 	list = hwc_get_child_spec(pdip, DDI_MAJOR_T_NONE);
45027c478bd9Sstevel@tonic-gate 	for (spec = list; spec != NULL; spec = spec->hwc_next) {
45037c478bd9Sstevel@tonic-gate 		init_spec_child(pdip, spec, flags);
45047c478bd9Sstevel@tonic-gate 	}
45057c478bd9Sstevel@tonic-gate 	hwc_free_spec_list(list);
45067c478bd9Sstevel@tonic-gate 
45077c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(pdip)->devi_lock);
45087c478bd9Sstevel@tonic-gate 	DEVI(pdip)->devi_flags |= DEVI_MADE_CHILDREN;
45097c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(pdip)->devi_lock);
45106a41d557Scth 	ndi_devi_exit(pdip, circ);
45117c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
45127c478bd9Sstevel@tonic-gate }
45137c478bd9Sstevel@tonic-gate 
45147c478bd9Sstevel@tonic-gate /*
45157c478bd9Sstevel@tonic-gate  * Run initchild on all child nodes such that instance assignment
45167c478bd9Sstevel@tonic-gate  * for multiport network cards are contiguous.
45177c478bd9Sstevel@tonic-gate  *
45187c478bd9Sstevel@tonic-gate  * The pdip must be held busy.
45197c478bd9Sstevel@tonic-gate  */
45207c478bd9Sstevel@tonic-gate static void
45217c478bd9Sstevel@tonic-gate i_ndi_init_hw_children(dev_info_t *pdip, uint_t flags)
45227c478bd9Sstevel@tonic-gate {
45237c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
45247c478bd9Sstevel@tonic-gate 
45257c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
45267c478bd9Sstevel@tonic-gate 
45277c478bd9Sstevel@tonic-gate 	/* contiguous instance assignment */
45287c478bd9Sstevel@tonic-gate 	e_ddi_enter_instance();
45297c478bd9Sstevel@tonic-gate 	dip = ddi_get_child(pdip);
45307c478bd9Sstevel@tonic-gate 	while (dip) {
45317c478bd9Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(dip))
45327c478bd9Sstevel@tonic-gate 			(void) i_ndi_config_node(dip, DS_INITIALIZED, flags);
45337c478bd9Sstevel@tonic-gate 		dip = ddi_get_next_sibling(dip);
45347c478bd9Sstevel@tonic-gate 	}
45357c478bd9Sstevel@tonic-gate 	e_ddi_exit_instance();
45367c478bd9Sstevel@tonic-gate }
45377c478bd9Sstevel@tonic-gate 
45387c478bd9Sstevel@tonic-gate /*
45397c478bd9Sstevel@tonic-gate  * report device status
45407c478bd9Sstevel@tonic-gate  */
45417c478bd9Sstevel@tonic-gate static void
45427c478bd9Sstevel@tonic-gate i_ndi_devi_report_status_change(dev_info_t *dip, char *path)
45437c478bd9Sstevel@tonic-gate {
45447c478bd9Sstevel@tonic-gate 	char *status;
45457c478bd9Sstevel@tonic-gate 
45467c478bd9Sstevel@tonic-gate 	if (!DEVI_NEED_REPORT(dip) ||
45474c06356bSdh142964 	    (i_ddi_node_state(dip) < DS_INITIALIZED) ||
45484c06356bSdh142964 	    ndi_dev_is_hidden_node(dip)) {
45497c478bd9Sstevel@tonic-gate 		return;
45507c478bd9Sstevel@tonic-gate 	}
45517c478bd9Sstevel@tonic-gate 
45524c06356bSdh142964 	/* Invalidate the devinfo snapshot cache */
45534c06356bSdh142964 	i_ddi_di_cache_invalidate();
45544c06356bSdh142964 
45554c06356bSdh142964 	if (DEVI_IS_DEVICE_REMOVED(dip)) {
45564c06356bSdh142964 		status = "removed";
45574c06356bSdh142964 	} else if (DEVI_IS_DEVICE_OFFLINE(dip)) {
45587c478bd9Sstevel@tonic-gate 		status = "offline";
45597c478bd9Sstevel@tonic-gate 	} else if (DEVI_IS_DEVICE_DOWN(dip)) {
45607c478bd9Sstevel@tonic-gate 		status = "down";
45617c478bd9Sstevel@tonic-gate 	} else if (DEVI_IS_BUS_QUIESCED(dip)) {
45627c478bd9Sstevel@tonic-gate 		status = "quiesced";
45637c478bd9Sstevel@tonic-gate 	} else if (DEVI_IS_BUS_DOWN(dip)) {
45647c478bd9Sstevel@tonic-gate 		status = "down";
4565737d277aScth 	} else if (i_ddi_devi_attached(dip)) {
45667c478bd9Sstevel@tonic-gate 		status = "online";
45677c478bd9Sstevel@tonic-gate 	} else {
45687c478bd9Sstevel@tonic-gate 		status = "unknown";
45697c478bd9Sstevel@tonic-gate 	}
45707c478bd9Sstevel@tonic-gate 
45717c478bd9Sstevel@tonic-gate 	if (path == NULL) {
45727c478bd9Sstevel@tonic-gate 		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
45737c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "?%s (%s%d) %s\n",
45747c478bd9Sstevel@tonic-gate 		    ddi_pathname(dip, path), ddi_driver_name(dip),
45757c478bd9Sstevel@tonic-gate 		    ddi_get_instance(dip), status);
45767c478bd9Sstevel@tonic-gate 		kmem_free(path, MAXPATHLEN);
45777c478bd9Sstevel@tonic-gate 	} else {
45787c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "?%s (%s%d) %s\n",
45797c478bd9Sstevel@tonic-gate 		    path, ddi_driver_name(dip),
45807c478bd9Sstevel@tonic-gate 		    ddi_get_instance(dip), status);
45817c478bd9Sstevel@tonic-gate 	}
45827c478bd9Sstevel@tonic-gate 
458316747f41Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
45847c478bd9Sstevel@tonic-gate 	DEVI_REPORT_DONE(dip);
458516747f41Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
45867c478bd9Sstevel@tonic-gate }
45877c478bd9Sstevel@tonic-gate 
45887c478bd9Sstevel@tonic-gate /*
45897c478bd9Sstevel@tonic-gate  * log a notification that a dev_info node has been configured.
45907c478bd9Sstevel@tonic-gate  */
45917c478bd9Sstevel@tonic-gate static int
45927c478bd9Sstevel@tonic-gate i_log_devfs_add_devinfo(dev_info_t *dip, uint_t flags)
45937c478bd9Sstevel@tonic-gate {
45947c478bd9Sstevel@tonic-gate 	int			se_err;
45957c478bd9Sstevel@tonic-gate 	char			*pathname;
45967c478bd9Sstevel@tonic-gate 	sysevent_t		*ev;
45977c478bd9Sstevel@tonic-gate 	sysevent_id_t		eid;
45987c478bd9Sstevel@tonic-gate 	sysevent_value_t	se_val;
45997c478bd9Sstevel@tonic-gate 	sysevent_attr_list_t	*ev_attr_list = NULL;
46007c478bd9Sstevel@tonic-gate 	char			*class_name;
46017c478bd9Sstevel@tonic-gate 	int			no_transport = 0;
46027c478bd9Sstevel@tonic-gate 
46034c06356bSdh142964 	ASSERT(dip && ddi_get_parent(dip) &&
46044c06356bSdh142964 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
46057c478bd9Sstevel@tonic-gate 
46067c478bd9Sstevel@tonic-gate 	/* do not generate ESC_DEVFS_DEVI_ADD event during boot */
46077c478bd9Sstevel@tonic-gate 	if (!i_ddi_io_initialized())
46087c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
46097c478bd9Sstevel@tonic-gate 
46104c06356bSdh142964 	/* Invalidate the devinfo snapshot cache */
46114c06356bSdh142964 	i_ddi_di_cache_invalidate();
46124c06356bSdh142964 
46137c478bd9Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_ADD, EP_DDI, SE_SLEEP);
46147c478bd9Sstevel@tonic-gate 
46157c478bd9Sstevel@tonic-gate 	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
46167c478bd9Sstevel@tonic-gate 
46177c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(dip, pathname);
46187c478bd9Sstevel@tonic-gate 	ASSERT(strlen(pathname));
46197c478bd9Sstevel@tonic-gate 
46207c478bd9Sstevel@tonic-gate 	se_val.value_type = SE_DATA_TYPE_STRING;
46217c478bd9Sstevel@tonic-gate 	se_val.value.sv_string = pathname;
46227c478bd9Sstevel@tonic-gate 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
46237c478bd9Sstevel@tonic-gate 	    &se_val, SE_SLEEP) != 0) {
46247c478bd9Sstevel@tonic-gate 		goto fail;
46257c478bd9Sstevel@tonic-gate 	}
46267c478bd9Sstevel@tonic-gate 
46277c478bd9Sstevel@tonic-gate 	/* add the device class attribute */
46287c478bd9Sstevel@tonic-gate 	if ((class_name = i_ddi_devi_class(dip)) != NULL) {
46297c478bd9Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_STRING;
46307c478bd9Sstevel@tonic-gate 		se_val.value.sv_string = class_name;
46317c478bd9Sstevel@tonic-gate 
46327c478bd9Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
46337c478bd9Sstevel@tonic-gate 		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
46347c478bd9Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
46357c478bd9Sstevel@tonic-gate 			goto fail;
46367c478bd9Sstevel@tonic-gate 		}
46377c478bd9Sstevel@tonic-gate 	}
46387c478bd9Sstevel@tonic-gate 
46397c478bd9Sstevel@tonic-gate 	/*
46407c478bd9Sstevel@tonic-gate 	 * must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
46417c478bd9Sstevel@tonic-gate 	 * in which case the branch event will be logged by the caller
46427c478bd9Sstevel@tonic-gate 	 * after the entire branch has been configured.
46437c478bd9Sstevel@tonic-gate 	 */
46447c478bd9Sstevel@tonic-gate 	if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
46457c478bd9Sstevel@tonic-gate 		/*
46467c478bd9Sstevel@tonic-gate 		 * Instead of logging a separate branch event just add
46477c478bd9Sstevel@tonic-gate 		 * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
46487c478bd9Sstevel@tonic-gate 		 * generate a EC_DEV_BRANCH event.
46497c478bd9Sstevel@tonic-gate 		 */
46507c478bd9Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_INT32;
46517c478bd9Sstevel@tonic-gate 		se_val.value.sv_int32 = 1;
46527c478bd9Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
46537c478bd9Sstevel@tonic-gate 		    DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
46547c478bd9Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
46557c478bd9Sstevel@tonic-gate 			goto fail;
46567c478bd9Sstevel@tonic-gate 		}
46577c478bd9Sstevel@tonic-gate 	}
46587c478bd9Sstevel@tonic-gate 
46597c478bd9Sstevel@tonic-gate 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
46607c478bd9Sstevel@tonic-gate 		sysevent_free_attr(ev_attr_list);
46617c478bd9Sstevel@tonic-gate 		goto fail;
46627c478bd9Sstevel@tonic-gate 	}
46637c478bd9Sstevel@tonic-gate 
46647c478bd9Sstevel@tonic-gate 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
46657c478bd9Sstevel@tonic-gate 		if (se_err == SE_NO_TRANSPORT)
46667c478bd9Sstevel@tonic-gate 			no_transport = 1;
46677c478bd9Sstevel@tonic-gate 		goto fail;
46687c478bd9Sstevel@tonic-gate 	}
46697c478bd9Sstevel@tonic-gate 
46707c478bd9Sstevel@tonic-gate 	sysevent_free(ev);
46717c478bd9Sstevel@tonic-gate 	kmem_free(pathname, MAXPATHLEN);
46727c478bd9Sstevel@tonic-gate 
46737c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
46747c478bd9Sstevel@tonic-gate 
46757c478bd9Sstevel@tonic-gate fail:
46767c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_ADD event for %s%s",
46777c478bd9Sstevel@tonic-gate 	    pathname, (no_transport) ? " (syseventd not responding)" : "");
46787c478bd9Sstevel@tonic-gate 
46797c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "/dev may not be current for driver %s. "
46807c478bd9Sstevel@tonic-gate 	    "Run devfsadm -i %s",
46817c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_driver_name(dip));
46827c478bd9Sstevel@tonic-gate 
46837c478bd9Sstevel@tonic-gate 	sysevent_free(ev);
46847c478bd9Sstevel@tonic-gate 	kmem_free(pathname, MAXPATHLEN);
46857c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
46867c478bd9Sstevel@tonic-gate }
46877c478bd9Sstevel@tonic-gate 
46887c478bd9Sstevel@tonic-gate /*
46897c478bd9Sstevel@tonic-gate  * log a notification that a dev_info node has been unconfigured.
46907c478bd9Sstevel@tonic-gate  */
46917c478bd9Sstevel@tonic-gate static int
46927c478bd9Sstevel@tonic-gate i_log_devfs_remove_devinfo(char *pathname, char *class_name, char *driver_name,
46937c478bd9Sstevel@tonic-gate     int instance, uint_t flags)
46947c478bd9Sstevel@tonic-gate {
46957c478bd9Sstevel@tonic-gate 	sysevent_t		*ev;
46967c478bd9Sstevel@tonic-gate 	sysevent_id_t		eid;
46977c478bd9Sstevel@tonic-gate 	sysevent_value_t	se_val;
46987c478bd9Sstevel@tonic-gate 	sysevent_attr_list_t	*ev_attr_list = NULL;
46997c478bd9Sstevel@tonic-gate 	int			se_err;
47007c478bd9Sstevel@tonic-gate 	int			no_transport = 0;
47017c478bd9Sstevel@tonic-gate 
47027c478bd9Sstevel@tonic-gate 	if (!i_ddi_io_initialized())
47037c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
47047c478bd9Sstevel@tonic-gate 
47054c06356bSdh142964 	/* Invalidate the devinfo snapshot cache */
47064c06356bSdh142964 	i_ddi_di_cache_invalidate();
47074c06356bSdh142964 
47087c478bd9Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_REMOVE, EP_DDI, SE_SLEEP);
47097c478bd9Sstevel@tonic-gate 
47107c478bd9Sstevel@tonic-gate 	se_val.value_type = SE_DATA_TYPE_STRING;
47117c478bd9Sstevel@tonic-gate 	se_val.value.sv_string = pathname;
47127c478bd9Sstevel@tonic-gate 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
47137c478bd9Sstevel@tonic-gate 	    &se_val, SE_SLEEP) != 0) {
47147c478bd9Sstevel@tonic-gate 		goto fail;
47157c478bd9Sstevel@tonic-gate 	}
47167c478bd9Sstevel@tonic-gate 
47177c478bd9Sstevel@tonic-gate 	if (class_name) {
47187c478bd9Sstevel@tonic-gate 		/* add the device class, driver name and instance attributes */
47197c478bd9Sstevel@tonic-gate 
47207c478bd9Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_STRING;
47217c478bd9Sstevel@tonic-gate 		se_val.value.sv_string = class_name;
47227c478bd9Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
47237c478bd9Sstevel@tonic-gate 		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
47247c478bd9Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
47257c478bd9Sstevel@tonic-gate 			goto fail;
47267c478bd9Sstevel@tonic-gate 		}
47277c478bd9Sstevel@tonic-gate 
47287c478bd9Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_STRING;
47297c478bd9Sstevel@tonic-gate 		se_val.value.sv_string = driver_name;
47307c478bd9Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
47317c478bd9Sstevel@tonic-gate 		    DEVFS_DRIVER_NAME, &se_val, SE_SLEEP) != 0) {
47327c478bd9Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
47337c478bd9Sstevel@tonic-gate 			goto fail;
47347c478bd9Sstevel@tonic-gate 		}
47357c478bd9Sstevel@tonic-gate 
47367c478bd9Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_INT32;
47377c478bd9Sstevel@tonic-gate 		se_val.value.sv_int32 = instance;
47387c478bd9Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
47397c478bd9Sstevel@tonic-gate 		    DEVFS_INSTANCE, &se_val, SE_SLEEP) != 0) {
47407c478bd9Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
47417c478bd9Sstevel@tonic-gate 			goto fail;
47427c478bd9Sstevel@tonic-gate 		}
47437c478bd9Sstevel@tonic-gate 	}
47447c478bd9Sstevel@tonic-gate 
47457c478bd9Sstevel@tonic-gate 	/*
47467c478bd9Sstevel@tonic-gate 	 * must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
47477c478bd9Sstevel@tonic-gate 	 * in which case the branch event will be logged by the caller
47487c478bd9Sstevel@tonic-gate 	 * after the entire branch has been unconfigured.
47497c478bd9Sstevel@tonic-gate 	 */
47507c478bd9Sstevel@tonic-gate 	if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
47517c478bd9Sstevel@tonic-gate 		/*
47527c478bd9Sstevel@tonic-gate 		 * Instead of logging a separate branch event just add
47537c478bd9Sstevel@tonic-gate 		 * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
47547c478bd9Sstevel@tonic-gate 		 * generate a EC_DEV_BRANCH event.
47557c478bd9Sstevel@tonic-gate 		 */
47567c478bd9Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_INT32;
47577c478bd9Sstevel@tonic-gate 		se_val.value.sv_int32 = 1;
47587c478bd9Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
47597c478bd9Sstevel@tonic-gate 		    DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
47607c478bd9Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
47617c478bd9Sstevel@tonic-gate 			goto fail;
47627c478bd9Sstevel@tonic-gate 		}
47637c478bd9Sstevel@tonic-gate 	}
47647c478bd9Sstevel@tonic-gate 
47657c478bd9Sstevel@tonic-gate 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
47667c478bd9Sstevel@tonic-gate 		sysevent_free_attr(ev_attr_list);
47677c478bd9Sstevel@tonic-gate 		goto fail;
47687c478bd9Sstevel@tonic-gate 	}
47697c478bd9Sstevel@tonic-gate 
47707c478bd9Sstevel@tonic-gate 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
47717c478bd9Sstevel@tonic-gate 		if (se_err == SE_NO_TRANSPORT)
47727c478bd9Sstevel@tonic-gate 			no_transport = 1;
47737c478bd9Sstevel@tonic-gate 		goto fail;
47747c478bd9Sstevel@tonic-gate 	}
47757c478bd9Sstevel@tonic-gate 
47767c478bd9Sstevel@tonic-gate 	sysevent_free(ev);
47777c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
47787c478bd9Sstevel@tonic-gate 
47797c478bd9Sstevel@tonic-gate fail:
47807c478bd9Sstevel@tonic-gate 	sysevent_free(ev);
47817c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_REMOVE event for %s%s",
47827c478bd9Sstevel@tonic-gate 	    pathname, (no_transport) ? " (syseventd not responding)" : "");
47837c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
47847c478bd9Sstevel@tonic-gate }
47857c478bd9Sstevel@tonic-gate 
47864c06356bSdh142964 static void
47874c06356bSdh142964 i_ddi_log_devfs_device_remove(dev_info_t *dip)
47884c06356bSdh142964 {
47894c06356bSdh142964 	char	*path;
47904c06356bSdh142964 
47914c06356bSdh142964 	ASSERT(dip && ddi_get_parent(dip) &&
47924c06356bSdh142964 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
47934c06356bSdh142964 	ASSERT(DEVI_IS_DEVICE_REMOVED(dip));
47944c06356bSdh142964 
47954c06356bSdh142964 	ASSERT(i_ddi_node_state(dip) >= DS_INITIALIZED);
47964c06356bSdh142964 	if (i_ddi_node_state(dip) < DS_INITIALIZED)
47974c06356bSdh142964 		return;
47984c06356bSdh142964 
47994c06356bSdh142964 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
48004c06356bSdh142964 	(void) i_log_devfs_remove_devinfo(ddi_pathname(dip, path),
48014c06356bSdh142964 	    i_ddi_devi_class(dip), (char *)ddi_driver_name(dip),
48024c06356bSdh142964 	    ddi_get_instance(dip), 0);
48034c06356bSdh142964 	kmem_free(path, MAXPATHLEN);
48044c06356bSdh142964 }
48054c06356bSdh142964 
48064c06356bSdh142964 static void
48074c06356bSdh142964 i_ddi_log_devfs_device_insert(dev_info_t *dip)
48084c06356bSdh142964 {
48094c06356bSdh142964 	ASSERT(dip && ddi_get_parent(dip) &&
48104c06356bSdh142964 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
48114c06356bSdh142964 	ASSERT(!DEVI_IS_DEVICE_REMOVED(dip));
48124c06356bSdh142964 
48134c06356bSdh142964 	(void) i_log_devfs_add_devinfo(dip, 0);
48144c06356bSdh142964 }
48154c06356bSdh142964 
48164c06356bSdh142964 
48177c478bd9Sstevel@tonic-gate /*
48187c478bd9Sstevel@tonic-gate  * log an event that a dev_info branch has been configured or unconfigured.
48197c478bd9Sstevel@tonic-gate  */
48207c478bd9Sstevel@tonic-gate static int
48217c478bd9Sstevel@tonic-gate i_log_devfs_branch(char *node_path, char *subclass)
48227c478bd9Sstevel@tonic-gate {
48237c478bd9Sstevel@tonic-gate 	int se_err;
48247c478bd9Sstevel@tonic-gate 	sysevent_t *ev;
48257c478bd9Sstevel@tonic-gate 	sysevent_id_t eid;
48267c478bd9Sstevel@tonic-gate 	sysevent_value_t se_val;
48277c478bd9Sstevel@tonic-gate 	sysevent_attr_list_t *ev_attr_list = NULL;
48287c478bd9Sstevel@tonic-gate 	int no_transport = 0;
48297c478bd9Sstevel@tonic-gate 
48307c478bd9Sstevel@tonic-gate 	/* do not generate the event during boot */
48317c478bd9Sstevel@tonic-gate 	if (!i_ddi_io_initialized())
48327c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
48337c478bd9Sstevel@tonic-gate 
48344c06356bSdh142964 	/* Invalidate the devinfo snapshot cache */
48354c06356bSdh142964 	i_ddi_di_cache_invalidate();
48364c06356bSdh142964 
48377c478bd9Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, subclass, EP_DDI, SE_SLEEP);
48387c478bd9Sstevel@tonic-gate 
48397c478bd9Sstevel@tonic-gate 	se_val.value_type = SE_DATA_TYPE_STRING;
48407c478bd9Sstevel@tonic-gate 	se_val.value.sv_string = node_path;
48417c478bd9Sstevel@tonic-gate 
48427c478bd9Sstevel@tonic-gate 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
48437c478bd9Sstevel@tonic-gate 	    &se_val, SE_SLEEP) != 0) {
48447c478bd9Sstevel@tonic-gate 		goto fail;
48457c478bd9Sstevel@tonic-gate 	}
48467c478bd9Sstevel@tonic-gate 
48477c478bd9Sstevel@tonic-gate 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
48487c478bd9Sstevel@tonic-gate 		sysevent_free_attr(ev_attr_list);
48497c478bd9Sstevel@tonic-gate 		goto fail;
48507c478bd9Sstevel@tonic-gate 	}
48517c478bd9Sstevel@tonic-gate 
48527c478bd9Sstevel@tonic-gate 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
48537c478bd9Sstevel@tonic-gate 		if (se_err == SE_NO_TRANSPORT)
48547c478bd9Sstevel@tonic-gate 			no_transport = 1;
48557c478bd9Sstevel@tonic-gate 		goto fail;
48567c478bd9Sstevel@tonic-gate 	}
48577c478bd9Sstevel@tonic-gate 
48587c478bd9Sstevel@tonic-gate 	sysevent_free(ev);
48597c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
48607c478bd9Sstevel@tonic-gate 
48617c478bd9Sstevel@tonic-gate fail:
48627c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "failed to log %s branch event for %s%s",
48637c478bd9Sstevel@tonic-gate 	    subclass, node_path,
48647c478bd9Sstevel@tonic-gate 	    (no_transport) ? " (syseventd not responding)" : "");
48657c478bd9Sstevel@tonic-gate 
48667c478bd9Sstevel@tonic-gate 	sysevent_free(ev);
48677c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
48687c478bd9Sstevel@tonic-gate }
48697c478bd9Sstevel@tonic-gate 
48707c478bd9Sstevel@tonic-gate /*
48717c478bd9Sstevel@tonic-gate  * log an event that a dev_info tree branch has been configured.
48727c478bd9Sstevel@tonic-gate  */
48737c478bd9Sstevel@tonic-gate static int
48747c478bd9Sstevel@tonic-gate i_log_devfs_branch_add(dev_info_t *dip)
48757c478bd9Sstevel@tonic-gate {
48767c478bd9Sstevel@tonic-gate 	char *node_path;
48777c478bd9Sstevel@tonic-gate 	int rv;
48787c478bd9Sstevel@tonic-gate 
48797c478bd9Sstevel@tonic-gate 	node_path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
48807c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(dip, node_path);
48817c478bd9Sstevel@tonic-gate 	rv = i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_ADD);
48827c478bd9Sstevel@tonic-gate 	kmem_free(node_path, MAXPATHLEN);
48837c478bd9Sstevel@tonic-gate 
48847c478bd9Sstevel@tonic-gate 	return (rv);
48857c478bd9Sstevel@tonic-gate }
48867c478bd9Sstevel@tonic-gate 
48877c478bd9Sstevel@tonic-gate /*
48887c478bd9Sstevel@tonic-gate  * log an event that a dev_info tree branch has been unconfigured.
48897c478bd9Sstevel@tonic-gate  */
48907c478bd9Sstevel@tonic-gate static int
48917c478bd9Sstevel@tonic-gate i_log_devfs_branch_remove(char *node_path)
48927c478bd9Sstevel@tonic-gate {
48937c478bd9Sstevel@tonic-gate 	return (i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_REMOVE));
48947c478bd9Sstevel@tonic-gate }
48957c478bd9Sstevel@tonic-gate 
48967c478bd9Sstevel@tonic-gate /*
48977c478bd9Sstevel@tonic-gate  * enqueue the dip's deviname on the branch event queue.
48987c478bd9Sstevel@tonic-gate  */
48997c478bd9Sstevel@tonic-gate static struct brevq_node *
49007c478bd9Sstevel@tonic-gate brevq_enqueue(struct brevq_node **brevqp, dev_info_t *dip,
49017c478bd9Sstevel@tonic-gate     struct brevq_node *child)
49027c478bd9Sstevel@tonic-gate {
49037c478bd9Sstevel@tonic-gate 	struct brevq_node *brn;
49047c478bd9Sstevel@tonic-gate 	char *deviname;
49057c478bd9Sstevel@tonic-gate 
49067c478bd9Sstevel@tonic-gate 	deviname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
49077c478bd9Sstevel@tonic-gate 	(void) ddi_deviname(dip, deviname);
49087c478bd9Sstevel@tonic-gate 
49097c478bd9Sstevel@tonic-gate 	brn = kmem_zalloc(sizeof (*brn), KM_SLEEP);
4910245c82d9Scth 	brn->brn_deviname = i_ddi_strdup(deviname, KM_SLEEP);
49117c478bd9Sstevel@tonic-gate 	kmem_free(deviname, MAXNAMELEN);
4912245c82d9Scth 	brn->brn_child = child;
4913245c82d9Scth 	brn->brn_sibling = *brevqp;
49147c478bd9Sstevel@tonic-gate 	*brevqp = brn;
49157c478bd9Sstevel@tonic-gate 
49167c478bd9Sstevel@tonic-gate 	return (brn);
49177c478bd9Sstevel@tonic-gate }
49187c478bd9Sstevel@tonic-gate 
49197c478bd9Sstevel@tonic-gate /*
49207c478bd9Sstevel@tonic-gate  * free the memory allocated for the elements on the branch event queue.
49217c478bd9Sstevel@tonic-gate  */
49227c478bd9Sstevel@tonic-gate static void
49237c478bd9Sstevel@tonic-gate free_brevq(struct brevq_node *brevq)
49247c478bd9Sstevel@tonic-gate {
49257c478bd9Sstevel@tonic-gate 	struct brevq_node *brn, *next_brn;
49267c478bd9Sstevel@tonic-gate 
49277c478bd9Sstevel@tonic-gate 	for (brn = brevq; brn != NULL; brn = next_brn) {
4928245c82d9Scth 		next_brn = brn->brn_sibling;
4929245c82d9Scth 		ASSERT(brn->brn_child == NULL);
4930245c82d9Scth 		kmem_free(brn->brn_deviname, strlen(brn->brn_deviname) + 1);
49317c478bd9Sstevel@tonic-gate 		kmem_free(brn, sizeof (*brn));
49327c478bd9Sstevel@tonic-gate 	}
49337c478bd9Sstevel@tonic-gate }
49347c478bd9Sstevel@tonic-gate 
49357c478bd9Sstevel@tonic-gate /*
49367c478bd9Sstevel@tonic-gate  * log the events queued up on the branch event queue and free the
49377c478bd9Sstevel@tonic-gate  * associated memory.
49387c478bd9Sstevel@tonic-gate  *
49397c478bd9Sstevel@tonic-gate  * node_path must have been allocated with at least MAXPATHLEN bytes.
49407c478bd9Sstevel@tonic-gate  */
49417c478bd9Sstevel@tonic-gate static void
49427c478bd9Sstevel@tonic-gate log_and_free_brevq(char *node_path, struct brevq_node *brevq)
49437c478bd9Sstevel@tonic-gate {
49447c478bd9Sstevel@tonic-gate 	struct brevq_node *brn;
49457c478bd9Sstevel@tonic-gate 	char *p;
49467c478bd9Sstevel@tonic-gate 
49477c478bd9Sstevel@tonic-gate 	p = node_path + strlen(node_path);
4948245c82d9Scth 	for (brn = brevq; brn != NULL; brn = brn->brn_sibling) {
4949245c82d9Scth 		(void) strcpy(p, brn->brn_deviname);
49507c478bd9Sstevel@tonic-gate 		(void) i_log_devfs_branch_remove(node_path);
49517c478bd9Sstevel@tonic-gate 	}
49527c478bd9Sstevel@tonic-gate 	*p = '\0';
49537c478bd9Sstevel@tonic-gate 
49547c478bd9Sstevel@tonic-gate 	free_brevq(brevq);
49557c478bd9Sstevel@tonic-gate }
49567c478bd9Sstevel@tonic-gate 
49577c478bd9Sstevel@tonic-gate /*
49587c478bd9Sstevel@tonic-gate  * log the events queued up on the branch event queue and free the
49597c478bd9Sstevel@tonic-gate  * associated memory. Same as the previous function but operates on dip.
49607c478bd9Sstevel@tonic-gate  */
49617c478bd9Sstevel@tonic-gate static void
49627c478bd9Sstevel@tonic-gate log_and_free_brevq_dip(dev_info_t *dip, struct brevq_node *brevq)
49637c478bd9Sstevel@tonic-gate {
49647c478bd9Sstevel@tonic-gate 	char *path;
49657c478bd9Sstevel@tonic-gate 
49667c478bd9Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
49677c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(dip, path);
49687c478bd9Sstevel@tonic-gate 	log_and_free_brevq(path, brevq);
49697c478bd9Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
49707c478bd9Sstevel@tonic-gate }
49717c478bd9Sstevel@tonic-gate 
49727c478bd9Sstevel@tonic-gate /*
49737c478bd9Sstevel@tonic-gate  * log the outstanding branch remove events for the grand children of the dip
49747c478bd9Sstevel@tonic-gate  * and free the associated memory.
49757c478bd9Sstevel@tonic-gate  */
49767c478bd9Sstevel@tonic-gate static void
49777c478bd9Sstevel@tonic-gate log_and_free_br_events_on_grand_children(dev_info_t *dip,
49787c478bd9Sstevel@tonic-gate     struct brevq_node *brevq)
49797c478bd9Sstevel@tonic-gate {
49807c478bd9Sstevel@tonic-gate 	struct brevq_node *brn;
49817c478bd9Sstevel@tonic-gate 	char *path;
49827c478bd9Sstevel@tonic-gate 	char *p;
49837c478bd9Sstevel@tonic-gate 
49847c478bd9Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
49857c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(dip, path);
49867c478bd9Sstevel@tonic-gate 	p = path + strlen(path);
4987245c82d9Scth 	for (brn = brevq; brn != NULL; brn = brn->brn_sibling) {
4988245c82d9Scth 		if (brn->brn_child) {
4989245c82d9Scth 			(void) strcpy(p, brn->brn_deviname);
49907c478bd9Sstevel@tonic-gate 			/* now path contains the node path to the dip's child */
4991245c82d9Scth 			log_and_free_brevq(path, brn->brn_child);
4992245c82d9Scth 			brn->brn_child = NULL;
49937c478bd9Sstevel@tonic-gate 		}
49947c478bd9Sstevel@tonic-gate 	}
49957c478bd9Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
49967c478bd9Sstevel@tonic-gate }
49977c478bd9Sstevel@tonic-gate 
49987c478bd9Sstevel@tonic-gate /*
49997c478bd9Sstevel@tonic-gate  * log and cleanup branch remove events for the grand children of the dip.
50007c478bd9Sstevel@tonic-gate  */
50017c478bd9Sstevel@tonic-gate static void
50027c478bd9Sstevel@tonic-gate cleanup_br_events_on_grand_children(dev_info_t *dip, struct brevq_node **brevqp)
50037c478bd9Sstevel@tonic-gate {
50047c478bd9Sstevel@tonic-gate 	dev_info_t *child;
50057c478bd9Sstevel@tonic-gate 	struct brevq_node *brevq, *brn, *prev_brn, *next_brn;
50067c478bd9Sstevel@tonic-gate 	char *path;
50077c478bd9Sstevel@tonic-gate 	int circ;
50087c478bd9Sstevel@tonic-gate 
50097c478bd9Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
50107c478bd9Sstevel@tonic-gate 	prev_brn = NULL;
50117c478bd9Sstevel@tonic-gate 	brevq = *brevqp;
50127c478bd9Sstevel@tonic-gate 
50137c478bd9Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
50147c478bd9Sstevel@tonic-gate 	for (brn = brevq; brn != NULL; brn = next_brn) {
5015245c82d9Scth 		next_brn = brn->brn_sibling;
50167c478bd9Sstevel@tonic-gate 		for (child = ddi_get_child(dip); child != NULL;
50177c478bd9Sstevel@tonic-gate 		    child = ddi_get_next_sibling(child)) {
50187c478bd9Sstevel@tonic-gate 			if (i_ddi_node_state(child) >= DS_INITIALIZED) {
50197c478bd9Sstevel@tonic-gate 				(void) ddi_deviname(child, path);
5020245c82d9Scth 				if (strcmp(path, brn->brn_deviname) == 0)
50217c478bd9Sstevel@tonic-gate 					break;
50227c478bd9Sstevel@tonic-gate 			}
50237c478bd9Sstevel@tonic-gate 		}
50247c478bd9Sstevel@tonic-gate 
50257c478bd9Sstevel@tonic-gate 		if (child != NULL && !(DEVI_EVREMOVE(child))) {
50267c478bd9Sstevel@tonic-gate 			/*
50277c478bd9Sstevel@tonic-gate 			 * Event state is not REMOVE. So branch remove event
5028245c82d9Scth 			 * is not going be generated on brn->brn_child.
50297c478bd9Sstevel@tonic-gate 			 * If any branch remove events were queued up on
5030245c82d9Scth 			 * brn->brn_child log them and remove the brn
50317c478bd9Sstevel@tonic-gate 			 * from the queue.
50327c478bd9Sstevel@tonic-gate 			 */
5033245c82d9Scth 			if (brn->brn_child) {
50347c478bd9Sstevel@tonic-gate 				(void) ddi_pathname(dip, path);
5035245c82d9Scth 				(void) strcat(path, brn->brn_deviname);
5036245c82d9Scth 				log_and_free_brevq(path, brn->brn_child);
50377c478bd9Sstevel@tonic-gate 			}
50387c478bd9Sstevel@tonic-gate 
50397c478bd9Sstevel@tonic-gate 			if (prev_brn)
5040245c82d9Scth 				prev_brn->brn_sibling = next_brn;
50417c478bd9Sstevel@tonic-gate 			else
50427c478bd9Sstevel@tonic-gate 				*brevqp = next_brn;
50437c478bd9Sstevel@tonic-gate 
5044245c82d9Scth 			kmem_free(brn->brn_deviname,
5045245c82d9Scth 			    strlen(brn->brn_deviname) + 1);
50467c478bd9Sstevel@tonic-gate 			kmem_free(brn, sizeof (*brn));
50477c478bd9Sstevel@tonic-gate 		} else {
50487c478bd9Sstevel@tonic-gate 			/*
50497c478bd9Sstevel@tonic-gate 			 * Free up the outstanding branch remove events
5050245c82d9Scth 			 * queued on brn->brn_child since brn->brn_child
50517c478bd9Sstevel@tonic-gate 			 * itself is eligible for branch remove event.
50527c478bd9Sstevel@tonic-gate 			 */
5053245c82d9Scth 			if (brn->brn_child) {
5054245c82d9Scth 				free_brevq(brn->brn_child);
5055245c82d9Scth 				brn->brn_child = NULL;
50567c478bd9Sstevel@tonic-gate 			}
50577c478bd9Sstevel@tonic-gate 			prev_brn = brn;
50587c478bd9Sstevel@tonic-gate 		}
50597c478bd9Sstevel@tonic-gate 	}
50607c478bd9Sstevel@tonic-gate 
50617c478bd9Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
50627c478bd9Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
50637c478bd9Sstevel@tonic-gate }
50647c478bd9Sstevel@tonic-gate 
50657c478bd9Sstevel@tonic-gate static int
50667c478bd9Sstevel@tonic-gate need_remove_event(dev_info_t *dip, int flags)
50677c478bd9Sstevel@tonic-gate {
50687c478bd9Sstevel@tonic-gate 	if ((flags & (NDI_NO_EVENT | NDI_AUTODETACH)) == 0 &&
50697c478bd9Sstevel@tonic-gate 	    (flags & (NDI_DEVI_OFFLINE | NDI_UNCONFIG | NDI_DEVI_REMOVE)) &&
50707c478bd9Sstevel@tonic-gate 	    !(DEVI_EVREMOVE(dip)))
50717c478bd9Sstevel@tonic-gate 		return (1);
50727c478bd9Sstevel@tonic-gate 	else
50737c478bd9Sstevel@tonic-gate 		return (0);
50747c478bd9Sstevel@tonic-gate }
50757c478bd9Sstevel@tonic-gate 
50767c478bd9Sstevel@tonic-gate /*
50777c478bd9Sstevel@tonic-gate  * Unconfigure children/descendants of the dip.
50787c478bd9Sstevel@tonic-gate  *
50797c478bd9Sstevel@tonic-gate  * If the operation involves a branch event NDI_BRANCH_EVENT_OP is set
50807c478bd9Sstevel@tonic-gate  * through out the unconfiguration. On successful return *brevqp is set to
50817c478bd9Sstevel@tonic-gate  * a queue of dip's child devinames for which branch remove events need
50827c478bd9Sstevel@tonic-gate  * to be generated.
50837c478bd9Sstevel@tonic-gate  */
50847c478bd9Sstevel@tonic-gate static int
50857c478bd9Sstevel@tonic-gate devi_unconfig_branch(dev_info_t *dip, dev_info_t **dipp, int flags,
50867c478bd9Sstevel@tonic-gate     struct brevq_node **brevqp)
50877c478bd9Sstevel@tonic-gate {
50887c478bd9Sstevel@tonic-gate 	int rval;
50897c478bd9Sstevel@tonic-gate 
50907c478bd9Sstevel@tonic-gate 	*brevqp = NULL;
50917c478bd9Sstevel@tonic-gate 
50927c478bd9Sstevel@tonic-gate 	if ((!(flags & NDI_BRANCH_EVENT_OP)) && need_remove_event(dip, flags))
50937c478bd9Sstevel@tonic-gate 		flags |= NDI_BRANCH_EVENT_OP;
50947c478bd9Sstevel@tonic-gate 
50957c478bd9Sstevel@tonic-gate 	if (flags & NDI_BRANCH_EVENT_OP) {
5096a204de77Scth 		rval = devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE,
50977c478bd9Sstevel@tonic-gate 		    brevqp);
50987c478bd9Sstevel@tonic-gate 
50997c478bd9Sstevel@tonic-gate 		if (rval != NDI_SUCCESS && (*brevqp)) {
51007c478bd9Sstevel@tonic-gate 			log_and_free_brevq_dip(dip, *brevqp);
51017c478bd9Sstevel@tonic-gate 			*brevqp = NULL;
51027c478bd9Sstevel@tonic-gate 		}
51037c478bd9Sstevel@tonic-gate 	} else
5104a204de77Scth 		rval = devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE,
51057c478bd9Sstevel@tonic-gate 		    NULL);
51067c478bd9Sstevel@tonic-gate 
51077c478bd9Sstevel@tonic-gate 	return (rval);
51087c478bd9Sstevel@tonic-gate }
51097c478bd9Sstevel@tonic-gate 
51107c478bd9Sstevel@tonic-gate /*
51117c478bd9Sstevel@tonic-gate  * If the dip is already bound to a driver transition to DS_INITIALIZED
51127c478bd9Sstevel@tonic-gate  * in order to generate an event in the case where the node was left in
51137c478bd9Sstevel@tonic-gate  * DS_BOUND state since boot (never got attached) and the node is now
51147c478bd9Sstevel@tonic-gate  * being offlined.
51157c478bd9Sstevel@tonic-gate  */
51167c478bd9Sstevel@tonic-gate static void
51177c478bd9Sstevel@tonic-gate init_bound_node_ev(dev_info_t *pdip, dev_info_t *dip, int flags)
51187c478bd9Sstevel@tonic-gate {
51197c478bd9Sstevel@tonic-gate 	if (need_remove_event(dip, flags) &&
51207c478bd9Sstevel@tonic-gate 	    i_ddi_node_state(dip) == DS_BOUND &&
5121737d277aScth 	    i_ddi_devi_attached(pdip) && !DEVI_IS_DEVICE_OFFLINE(dip))
51227c478bd9Sstevel@tonic-gate 		(void) ddi_initchild(pdip, dip);
51237c478bd9Sstevel@tonic-gate }
51247c478bd9Sstevel@tonic-gate 
51257c478bd9Sstevel@tonic-gate /*
51267c478bd9Sstevel@tonic-gate  * attach a node/branch with parent already held busy
51277c478bd9Sstevel@tonic-gate  */
51287c478bd9Sstevel@tonic-gate static int
51297c478bd9Sstevel@tonic-gate devi_attach_node(dev_info_t *dip, uint_t flags)
51307c478bd9Sstevel@tonic-gate {
51315e3986cbScth 	dev_info_t *pdip = ddi_get_parent(dip);
51325e3986cbScth 
51335e3986cbScth 	ASSERT(pdip && DEVI_BUSY_OWNED(pdip));
51345e3986cbScth 
513516747f41Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
51367c478bd9Sstevel@tonic-gate 	if (flags & NDI_DEVI_ONLINE) {
5137737d277aScth 		if (!i_ddi_devi_attached(dip))
513816747f41Scth 			DEVI_SET_REPORT(dip);
51397c478bd9Sstevel@tonic-gate 		DEVI_SET_DEVICE_ONLINE(dip);
51407c478bd9Sstevel@tonic-gate 	}
51417c478bd9Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
514216747f41Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
51437c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
51447c478bd9Sstevel@tonic-gate 	}
514516747f41Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
51467c478bd9Sstevel@tonic-gate 
51477c478bd9Sstevel@tonic-gate 	if (i_ddi_attachchild(dip) != DDI_SUCCESS) {
514816747f41Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
51497c478bd9Sstevel@tonic-gate 		DEVI_SET_EVUNINIT(dip);
515016747f41Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
515116747f41Scth 
51527c478bd9Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(dip))
51537c478bd9Sstevel@tonic-gate 			(void) ddi_uninitchild(dip);
51547c478bd9Sstevel@tonic-gate 		else {
51557c478bd9Sstevel@tonic-gate 			/*
51567c478bd9Sstevel@tonic-gate 			 * Delete .conf nodes and nodes that are not
51577c478bd9Sstevel@tonic-gate 			 * well formed.
51587c478bd9Sstevel@tonic-gate 			 */
51597c478bd9Sstevel@tonic-gate 			(void) ddi_remove_child(dip, 0);
51607c478bd9Sstevel@tonic-gate 		}
51617c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
51627c478bd9Sstevel@tonic-gate 	}
51637c478bd9Sstevel@tonic-gate 
51647c478bd9Sstevel@tonic-gate 	i_ndi_devi_report_status_change(dip, NULL);
51657c478bd9Sstevel@tonic-gate 
51667c478bd9Sstevel@tonic-gate 	/*
51677c478bd9Sstevel@tonic-gate 	 * log an event, but not during devfs lookups in which case
51687c478bd9Sstevel@tonic-gate 	 * NDI_NO_EVENT is set.
51697c478bd9Sstevel@tonic-gate 	 */
51707c478bd9Sstevel@tonic-gate 	if ((flags & NDI_NO_EVENT) == 0 && !(DEVI_EVADD(dip))) {
51717c478bd9Sstevel@tonic-gate 		(void) i_log_devfs_add_devinfo(dip, flags);
517216747f41Scth 
517316747f41Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
51747c478bd9Sstevel@tonic-gate 		DEVI_SET_EVADD(dip);
517516747f41Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
517616747f41Scth 	} else if (!(flags & NDI_NO_EVENT_STATE_CHNG)) {
517716747f41Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
51787c478bd9Sstevel@tonic-gate 		DEVI_SET_EVADD(dip);
517916747f41Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
518016747f41Scth 	}
51817c478bd9Sstevel@tonic-gate 
51827c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
51837c478bd9Sstevel@tonic-gate }
51847c478bd9Sstevel@tonic-gate 
51857c478bd9Sstevel@tonic-gate /* internal function to config immediate children */
51867c478bd9Sstevel@tonic-gate static int
51877c478bd9Sstevel@tonic-gate config_immediate_children(dev_info_t *pdip, uint_t flags, major_t major)
51887c478bd9Sstevel@tonic-gate {
51895e3986cbScth 	dev_info_t	*child, *next;
51907c478bd9Sstevel@tonic-gate 	int		circ;
51915e3986cbScth 
5192737d277aScth 	ASSERT(i_ddi_devi_attached(pdip));
51937c478bd9Sstevel@tonic-gate 
51947c478bd9Sstevel@tonic-gate 	if (!NEXUS_DRV(ddi_get_driver(pdip)))
51957c478bd9Sstevel@tonic-gate 		return (NDI_SUCCESS);
51967c478bd9Sstevel@tonic-gate 
51977c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
51987c478bd9Sstevel@tonic-gate 	    "config_immediate_children: %s%d (%p), flags=%x\n",
51997c478bd9Sstevel@tonic-gate 	    ddi_driver_name(pdip), ddi_get_instance(pdip),
52007c478bd9Sstevel@tonic-gate 	    (void *)pdip, flags));
52017c478bd9Sstevel@tonic-gate 
52027c478bd9Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
52037c478bd9Sstevel@tonic-gate 
52047c478bd9Sstevel@tonic-gate 	if (flags & NDI_CONFIG_REPROBE) {
52057c478bd9Sstevel@tonic-gate 		mutex_enter(&DEVI(pdip)->devi_lock);
52067c478bd9Sstevel@tonic-gate 		DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
52077c478bd9Sstevel@tonic-gate 		mutex_exit(&DEVI(pdip)->devi_lock);
52087c478bd9Sstevel@tonic-gate 	}
52097c478bd9Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(pdip, flags);
52107c478bd9Sstevel@tonic-gate 	i_ndi_init_hw_children(pdip, flags);
52115e3986cbScth 
52125e3986cbScth 	child = ddi_get_child(pdip);
52135e3986cbScth 	while (child) {
52145e3986cbScth 		/* NOTE: devi_attach_node() may remove the dip */
52155e3986cbScth 		next = ddi_get_next_sibling(child);
52165e3986cbScth 
52175e3986cbScth 		/*
52185e3986cbScth 		 * Configure all nexus nodes or leaf nodes with
52195e3986cbScth 		 * matching driver major
52205e3986cbScth 		 */
5221a204de77Scth 		if ((major == DDI_MAJOR_T_NONE) ||
52225e3986cbScth 		    (major == ddi_driver_major(child)) ||
52235e3986cbScth 		    ((flags & NDI_CONFIG) && (is_leaf_node(child) == 0)))
52245e3986cbScth 			(void) devi_attach_node(child, flags);
52255e3986cbScth 		child = next;
52265e3986cbScth 	}
52277c478bd9Sstevel@tonic-gate 
52287c478bd9Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
52297c478bd9Sstevel@tonic-gate 
52307c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
52317c478bd9Sstevel@tonic-gate }
52327c478bd9Sstevel@tonic-gate 
52337c478bd9Sstevel@tonic-gate /* internal function to config grand children */
52347c478bd9Sstevel@tonic-gate static int
52357c478bd9Sstevel@tonic-gate config_grand_children(dev_info_t *pdip, uint_t flags, major_t major)
52367c478bd9Sstevel@tonic-gate {
52377c478bd9Sstevel@tonic-gate 	struct mt_config_handle *hdl;
52387c478bd9Sstevel@tonic-gate 
52397c478bd9Sstevel@tonic-gate 	/* multi-threaded configuration of child nexus */
52407c478bd9Sstevel@tonic-gate 	hdl = mt_config_init(pdip, NULL, flags, major, MT_CONFIG_OP, NULL);
52417c478bd9Sstevel@tonic-gate 	mt_config_children(hdl);
52427c478bd9Sstevel@tonic-gate 
52437c478bd9Sstevel@tonic-gate 	return (mt_config_fini(hdl));	/* wait for threads to exit */
52447c478bd9Sstevel@tonic-gate }
52457c478bd9Sstevel@tonic-gate 
52467c478bd9Sstevel@tonic-gate /*
52477c478bd9Sstevel@tonic-gate  * Common function for device tree configuration,
52487c478bd9Sstevel@tonic-gate  * either BUS_CONFIG_ALL or BUS_CONFIG_DRIVER.
52497c478bd9Sstevel@tonic-gate  * The NDI_CONFIG flag causes recursive configuration of
52507c478bd9Sstevel@tonic-gate  * grandchildren, devfs usage should not recurse.
52517c478bd9Sstevel@tonic-gate  */
52527c478bd9Sstevel@tonic-gate static int
52537c478bd9Sstevel@tonic-gate devi_config_common(dev_info_t *dip, int flags, major_t major)
52547c478bd9Sstevel@tonic-gate {
52557c478bd9Sstevel@tonic-gate 	int error;
52567c478bd9Sstevel@tonic-gate 	int (*f)();
52577c478bd9Sstevel@tonic-gate 
5258737d277aScth 	if (!i_ddi_devi_attached(dip))
52597c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
52607c478bd9Sstevel@tonic-gate 
52617c478bd9Sstevel@tonic-gate 	if (pm_pre_config(dip, NULL) != DDI_SUCCESS)
52627c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
52637c478bd9Sstevel@tonic-gate 
52647c478bd9Sstevel@tonic-gate 	if ((DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
52657c478bd9Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
52667c478bd9Sstevel@tonic-gate 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
52677c478bd9Sstevel@tonic-gate 		error = config_immediate_children(dip, flags, major);
52687c478bd9Sstevel@tonic-gate 	} else {
52697c478bd9Sstevel@tonic-gate 		/* call bus_config entry point */
5270a204de77Scth 		ddi_bus_config_op_t bus_op = (major == DDI_MAJOR_T_NONE) ?
52717c478bd9Sstevel@tonic-gate 		    BUS_CONFIG_ALL : BUS_CONFIG_DRIVER;
52727c478bd9Sstevel@tonic-gate 		error = (*f)(dip,
52737c478bd9Sstevel@tonic-gate 		    flags, bus_op, (void *)(uintptr_t)major, NULL, 0);
52747c478bd9Sstevel@tonic-gate 	}
52757c478bd9Sstevel@tonic-gate 
52767c478bd9Sstevel@tonic-gate 	if (error) {
52777c478bd9Sstevel@tonic-gate 		pm_post_config(dip, NULL);
52787c478bd9Sstevel@tonic-gate 		return (error);
52797c478bd9Sstevel@tonic-gate 	}
52807c478bd9Sstevel@tonic-gate 
52817c478bd9Sstevel@tonic-gate 	/*
52827c478bd9Sstevel@tonic-gate 	 * Some callers, notably SCSI, need to mark the devfs cache
52837c478bd9Sstevel@tonic-gate 	 * to be rebuilt together with the config operation.
52847c478bd9Sstevel@tonic-gate 	 */
52857c478bd9Sstevel@tonic-gate 	if (flags & NDI_DEVFS_CLEAN)
52867c478bd9Sstevel@tonic-gate 		(void) devfs_clean(dip, NULL, 0);
52877c478bd9Sstevel@tonic-gate 
52887c478bd9Sstevel@tonic-gate 	if (flags & NDI_CONFIG)
52897c478bd9Sstevel@tonic-gate 		(void) config_grand_children(dip, flags, major);
52907c478bd9Sstevel@tonic-gate 
52917c478bd9Sstevel@tonic-gate 	pm_post_config(dip, NULL);
52927c478bd9Sstevel@tonic-gate 
52937c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
52947c478bd9Sstevel@tonic-gate }
52957c478bd9Sstevel@tonic-gate 
52967c478bd9Sstevel@tonic-gate /*
52977c478bd9Sstevel@tonic-gate  * Framework entry point for BUS_CONFIG_ALL
52987c478bd9Sstevel@tonic-gate  */
52997c478bd9Sstevel@tonic-gate int
53007c478bd9Sstevel@tonic-gate ndi_devi_config(dev_info_t *dip, int flags)
53017c478bd9Sstevel@tonic-gate {
53027c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
53037c478bd9Sstevel@tonic-gate 	    "ndi_devi_config: par = %s%d (%p), flags = 0x%x\n",
53047c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
53057c478bd9Sstevel@tonic-gate 
5306a204de77Scth 	return (devi_config_common(dip, flags, DDI_MAJOR_T_NONE));
53077c478bd9Sstevel@tonic-gate }
53087c478bd9Sstevel@tonic-gate 
53097c478bd9Sstevel@tonic-gate /*
53107c478bd9Sstevel@tonic-gate  * Framework entry point for BUS_CONFIG_DRIVER, bound to major
53117c478bd9Sstevel@tonic-gate  */
53127c478bd9Sstevel@tonic-gate int
53137c478bd9Sstevel@tonic-gate ndi_devi_config_driver(dev_info_t *dip, int flags, major_t major)
53147c478bd9Sstevel@tonic-gate {
53157c478bd9Sstevel@tonic-gate 	/* don't abuse this function */
5316a204de77Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
53177c478bd9Sstevel@tonic-gate 
53187c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
53197c478bd9Sstevel@tonic-gate 	    "ndi_devi_config_driver: par = %s%d (%p), flags = 0x%x\n",
53207c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
53217c478bd9Sstevel@tonic-gate 
53227c478bd9Sstevel@tonic-gate 	return (devi_config_common(dip, flags, major));
53237c478bd9Sstevel@tonic-gate }
53247c478bd9Sstevel@tonic-gate 
53257c478bd9Sstevel@tonic-gate /*
53265e3986cbScth  * Called by nexus drivers to configure its children.
53277c478bd9Sstevel@tonic-gate  */
53287c478bd9Sstevel@tonic-gate static int
53295e3986cbScth devi_config_one(dev_info_t *pdip, char *devnm, dev_info_t **cdipp,
53307c478bd9Sstevel@tonic-gate     uint_t flags, clock_t timeout)
53317c478bd9Sstevel@tonic-gate {
53325e3986cbScth 	dev_info_t	*vdip = NULL;
53335e3986cbScth 	char		*drivername = NULL;
5334f4da9be0Scth 	int		find_by_addr = 0;
53355e3986cbScth 	char		*name, *addr;
53365e3986cbScth 	int		v_circ, p_circ;
53377c478bd9Sstevel@tonic-gate 	clock_t		end_time;	/* 60 sec */
53385e3986cbScth 	int		probed;
53395e3986cbScth 	dev_info_t	*cdip;
53405e3986cbScth 	mdi_pathinfo_t	*cpip;
53415e3986cbScth 
53425e3986cbScth 	*cdipp = NULL;
53437c478bd9Sstevel@tonic-gate 
53447c478bd9Sstevel@tonic-gate 	if (!NEXUS_DRV(ddi_get_driver(pdip)))
53457c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
53467c478bd9Sstevel@tonic-gate 
53477c478bd9Sstevel@tonic-gate 	/* split name into "name@addr" parts */
53487c478bd9Sstevel@tonic-gate 	i_ddi_parse_name(devnm, &name, &addr, NULL);
53497c478bd9Sstevel@tonic-gate 
53507c478bd9Sstevel@tonic-gate 	/*
53515e3986cbScth 	 * If the nexus is a pHCI and we are not processing a pHCI from
53525e3986cbScth 	 * mdi bus_config code then we need to know the vHCI.
53537c478bd9Sstevel@tonic-gate 	 */
53545e3986cbScth 	if (MDI_PHCI(pdip))
53555e3986cbScth 		vdip = mdi_devi_get_vdip(pdip);
53565e3986cbScth 
53575e3986cbScth 	/*
53585e3986cbScth 	 * We may have a genericname on a system that creates drivername
53595e3986cbScth 	 * nodes (from .conf files).  Find the drivername by nodeid. If we
53605e3986cbScth 	 * can't find a node with devnm as the node name then we search by
53615e3986cbScth 	 * drivername.	This allows an implementation to supply a genericly
53624c96bfe1Scth 	 * named boot path (disk) and locate drivename nodes (sd).  The
53634c96bfe1Scth 	 * NDI_PROMNAME flag does not apply to /devices/pseudo paths.
53645e3986cbScth 	 */
53654c96bfe1Scth 	if ((flags & NDI_PROMNAME) && (pdip != pseudo_dip)) {
53667c478bd9Sstevel@tonic-gate 		drivername = child_path_to_driver(pdip, name, addr);
5367f4da9be0Scth 		find_by_addr = 1;
5368f4da9be0Scth 	}
53697c478bd9Sstevel@tonic-gate 
5370144dfaa9Scth 	/*
53715e3986cbScth 	 * Determine end_time: This routine should *not* be called with a
53725e3986cbScth 	 * constant non-zero timeout argument, the caller should be adjusting
53735e3986cbScth 	 * the timeout argument relative to when it *started* its asynchronous
53745e3986cbScth 	 * enumeration.
5375144dfaa9Scth 	 */
53765e3986cbScth 	if (timeout > 0)
53775e3986cbScth 		end_time = ddi_get_lbolt() + timeout;
5378c73a93f2Sdm120769 
53795e3986cbScth 	for (;;) {
53805e3986cbScth 		/*
53815e3986cbScth 		 * For pHCI, enter (vHCI, pHCI) and search for pathinfo/client
53825e3986cbScth 		 * child - break out of for(;;) loop if child found.
53835e3986cbScth 		 * NOTE: Lock order for ndi_devi_enter is (vHCI, pHCI).
53845e3986cbScth 		 */
53855e3986cbScth 		if (vdip) {
53865e3986cbScth 			/* use mdi_devi_enter ordering */
53875e3986cbScth 			ndi_devi_enter(vdip, &v_circ);
53885e3986cbScth 			ndi_devi_enter(pdip, &p_circ);
53895e3986cbScth 			cpip = mdi_pi_find(pdip, NULL, addr);
53905e3986cbScth 			cdip = mdi_pi_get_client(cpip);
53915e3986cbScth 			if (cdip)
53925e3986cbScth 				break;
53935e3986cbScth 		} else
53945e3986cbScth 			ndi_devi_enter(pdip, &p_circ);
53955e3986cbScth 
53965e3986cbScth 		/*
53975e3986cbScth 		 * When not a  vHCI or not all pHCI devices are required to
53985e3986cbScth 		 * enumerated under the vHCI (NDI_MDI_FALLBACK) search for
53995e3986cbScth 		 * devinfo child.
54005e3986cbScth 		 */
54015e3986cbScth 		if ((vdip == NULL) || (flags & NDI_MDI_FALLBACK)) {
54025e3986cbScth 			/* determine if .conf nodes already built */
54035e3986cbScth 			probed = (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
54045e3986cbScth 
54055e3986cbScth 			/*
54065e3986cbScth 			 * Search for child by name, if not found then search
54075e3986cbScth 			 * for a node bound to the drivername driver with the
54085e3986cbScth 			 * specified "@addr". Break out of for(;;) loop if
5409f4da9be0Scth 			 * child found.  To support path-oriented aliases
5410f4da9be0Scth 			 * binding on boot-device, we do a search_by_addr too.
54115e3986cbScth 			 */
54125e3986cbScth again:			(void) i_ndi_make_spec_children(pdip, flags);
54135e3986cbScth 			cdip = find_child_by_name(pdip, name, addr);
54145e3986cbScth 			if ((cdip == NULL) && drivername)
54155e3986cbScth 				cdip = find_child_by_driver(pdip,
54165e3986cbScth 				    drivername, addr);
5417f4da9be0Scth 			if ((cdip == NULL) && find_by_addr)
5418f4da9be0Scth 				cdip = find_child_by_addr(pdip, addr);
54195e3986cbScth 			if (cdip)
54207c478bd9Sstevel@tonic-gate 				break;
54217c478bd9Sstevel@tonic-gate 
54227c478bd9Sstevel@tonic-gate 			/*
54235e3986cbScth 			 * determine if we should reenumerate .conf nodes
54245e3986cbScth 			 * and look for child again.
54257c478bd9Sstevel@tonic-gate 			 */
54265e3986cbScth 			if (probed &&
54275e3986cbScth 			    i_ddi_io_initialized() &&
54285e3986cbScth 			    (flags & NDI_CONFIG_REPROBE) &&
54295e3986cbScth 			    ((timeout <= 0) || (ddi_get_lbolt() >= end_time))) {
54305e3986cbScth 				probed = 0;
54315e3986cbScth 				mutex_enter(&DEVI(pdip)->devi_lock);
54325e3986cbScth 				DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
54335e3986cbScth 				mutex_exit(&DEVI(pdip)->devi_lock);
54345e3986cbScth 				goto again;
54355e3986cbScth 			}
54365e3986cbScth 		}
54375e3986cbScth 
54385e3986cbScth 		/* break out of for(;;) if time expired */
54395e3986cbScth 		if ((timeout <= 0) || (ddi_get_lbolt() >= end_time))
54405e3986cbScth 			break;
54415e3986cbScth 
54425e3986cbScth 		/*
54435e3986cbScth 		 * Child not found, exit and wait for asynchronous enumeration
54445e3986cbScth 		 * to add child (or timeout). The addition of a new child (vhci
54455e3986cbScth 		 * or phci) requires the asynchronous enumeration thread to
54465e3986cbScth 		 * ndi_devi_enter/ndi_devi_exit. This exit will signal devi_cv
54475e3986cbScth 		 * and cause us to return from ndi_devi_exit_and_wait, after
54485e3986cbScth 		 * which we loop and search for the requested child again.
54495e3986cbScth 		 */
54507c478bd9Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT,
54517c478bd9Sstevel@tonic-gate 		    "%s%d: waiting for child %s@%s, timeout %ld",
54527c478bd9Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
54537c478bd9Sstevel@tonic-gate 		    name, addr, timeout));
54545e3986cbScth 		if (vdip) {
5455c73a93f2Sdm120769 			/*
54565e3986cbScth 			 * Mark vHCI for pHCI ndi_devi_exit broadcast.
5457c73a93f2Sdm120769 			 */
54585e3986cbScth 			mutex_enter(&DEVI(vdip)->devi_lock);
54595e3986cbScth 			DEVI(vdip)->devi_flags |=
54605e3986cbScth 			    DEVI_PHCI_SIGNALS_VHCI;
54615e3986cbScth 			mutex_exit(&DEVI(vdip)->devi_lock);
54625e3986cbScth 			ndi_devi_exit(pdip, p_circ);
54635e3986cbScth 
54645e3986cbScth 			/*
54655e3986cbScth 			 * NB: There is a small race window from above
54665e3986cbScth 			 * ndi_devi_exit() of pdip to cv_wait() in
54675e3986cbScth 			 * ndi_devi_exit_and_wait() which can result in
54685e3986cbScth 			 * not immediately finding a new pHCI child
54695e3986cbScth 			 * of a pHCI that uses NDI_MDI_FAILBACK.
54705e3986cbScth 			 */
54715e3986cbScth 			ndi_devi_exit_and_wait(vdip, v_circ, end_time);
54725e3986cbScth 		} else {
54735e3986cbScth 			ndi_devi_exit_and_wait(pdip, p_circ, end_time);
54745e3986cbScth 		}
5475c73a93f2Sdm120769 	}
5476c73a93f2Sdm120769 
54775e3986cbScth 	/* done with paddr, fixup i_ddi_parse_name '@'->'\0' change */
54785e3986cbScth 	if (addr && *addr != '\0')
54797c478bd9Sstevel@tonic-gate 		*(addr - 1) = '@';
54807c478bd9Sstevel@tonic-gate 
54815e3986cbScth 	/* attach and hold the child, returning pointer to child */
54825e3986cbScth 	if (cdip && (devi_attach_node(cdip, flags) == NDI_SUCCESS)) {
54835e3986cbScth 		ndi_hold_devi(cdip);
54845e3986cbScth 		*cdipp = cdip;
54857c478bd9Sstevel@tonic-gate 	}
54867c478bd9Sstevel@tonic-gate 
54875e3986cbScth 	ndi_devi_exit(pdip, p_circ);
54885e3986cbScth 	if (vdip)
54895e3986cbScth 		ndi_devi_exit(vdip, v_circ);
54905e3986cbScth 	return (*cdipp ? NDI_SUCCESS : NDI_FAILURE);
54917c478bd9Sstevel@tonic-gate }
54927c478bd9Sstevel@tonic-gate 
54937c478bd9Sstevel@tonic-gate /*
54947c478bd9Sstevel@tonic-gate  * Enumerate and attach a child specified by name 'devnm'.
54957c478bd9Sstevel@tonic-gate  * Called by devfs lookup and DR to perform a BUS_CONFIG_ONE.
54967c478bd9Sstevel@tonic-gate  * Note: devfs does not make use of NDI_CONFIG to configure
54977c478bd9Sstevel@tonic-gate  * an entire branch.
54987c478bd9Sstevel@tonic-gate  */
54997c478bd9Sstevel@tonic-gate int
5500*94c894bbSVikram Hegde ndi_devi_config_one(dev_info_t *pdip, char *devnm, dev_info_t **dipp, int flags)
55017c478bd9Sstevel@tonic-gate {
55027c478bd9Sstevel@tonic-gate 	int error;
55037c478bd9Sstevel@tonic-gate 	int (*f)();
5504*94c894bbSVikram Hegde 	char *nmdup;
5505*94c894bbSVikram Hegde 	int duplen;
55067c478bd9Sstevel@tonic-gate 	int branch_event = 0;
55077c478bd9Sstevel@tonic-gate 
5508*94c894bbSVikram Hegde 	ASSERT(pdip);
5509*94c894bbSVikram Hegde 	ASSERT(devnm);
55107c478bd9Sstevel@tonic-gate 	ASSERT(dipp);
5511*94c894bbSVikram Hegde 	ASSERT(i_ddi_devi_attached(pdip));
55127c478bd9Sstevel@tonic-gate 
55137c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
55147c478bd9Sstevel@tonic-gate 	    "ndi_devi_config_one: par = %s%d (%p), child = %s\n",
5515*94c894bbSVikram Hegde 	    ddi_driver_name(pdip), ddi_get_instance(pdip),
5516*94c894bbSVikram Hegde 	    (void *)pdip, devnm));
55177c478bd9Sstevel@tonic-gate 
5518*94c894bbSVikram Hegde 	*dipp = NULL;
5519*94c894bbSVikram Hegde 
5520*94c894bbSVikram Hegde 	if (pm_pre_config(pdip, devnm) != DDI_SUCCESS) {
5521*94c894bbSVikram Hegde 		cmn_err(CE_WARN, "preconfig failed: %s", devnm);
55227c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
5523*94c894bbSVikram Hegde 	}
55247c478bd9Sstevel@tonic-gate 
55257c478bd9Sstevel@tonic-gate 	if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
55267c478bd9Sstevel@tonic-gate 	    (flags & NDI_CONFIG)) {
55277c478bd9Sstevel@tonic-gate 		flags |= NDI_BRANCH_EVENT_OP;
55287c478bd9Sstevel@tonic-gate 		branch_event = 1;
55297c478bd9Sstevel@tonic-gate 	}
55307c478bd9Sstevel@tonic-gate 
5531*94c894bbSVikram Hegde 	nmdup = strdup(devnm);
5532*94c894bbSVikram Hegde 	duplen = strlen(devnm) + 1;
5533*94c894bbSVikram Hegde 
5534*94c894bbSVikram Hegde 	if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
5535*94c894bbSVikram Hegde 	    (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
5536*94c894bbSVikram Hegde 	    (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
5537*94c894bbSVikram Hegde 		error = devi_config_one(pdip, devnm, dipp, flags, 0);
55387c478bd9Sstevel@tonic-gate 	} else {
55397c478bd9Sstevel@tonic-gate 		/* call bus_config entry point */
5540*94c894bbSVikram Hegde 		error = (*f)(pdip, flags, BUS_CONFIG_ONE, (void *)devnm, dipp);
55417c478bd9Sstevel@tonic-gate 	}
55427c478bd9Sstevel@tonic-gate 
5543*94c894bbSVikram Hegde 	if (error) {
5544*94c894bbSVikram Hegde 		*dipp = NULL;
5545*94c894bbSVikram Hegde 	}
5546*94c894bbSVikram Hegde 
5547*94c894bbSVikram Hegde 	/*
5548*94c894bbSVikram Hegde 	 * if we fail to lookup and this could be an alias, lookup currdip
5549*94c894bbSVikram Hegde 	 * To prevent recursive lookups into the same hash table, only
5550*94c894bbSVikram Hegde 	 * do the currdip lookups once the hash table init is complete.
5551*94c894bbSVikram Hegde 	 * Use tsd so that redirection doesn't recurse
5552*94c894bbSVikram Hegde 	 */
5553*94c894bbSVikram Hegde 	if (error) {
5554*94c894bbSVikram Hegde 		char *alias = kmem_alloc(MAXPATHLEN, KM_NOSLEEP);
5555*94c894bbSVikram Hegde 		if (alias == NULL) {
5556*94c894bbSVikram Hegde 			ddi_err(DER_PANIC, pdip, "alias alloc failed: %s",
5557*94c894bbSVikram Hegde 			    nmdup);
5558*94c894bbSVikram Hegde 		}
5559*94c894bbSVikram Hegde 		(void) ddi_pathname(pdip, alias);
5560*94c894bbSVikram Hegde 		(void) strlcat(alias, "/", MAXPATHLEN);
5561*94c894bbSVikram Hegde 		(void) strlcat(alias, nmdup, MAXPATHLEN);
5562*94c894bbSVikram Hegde 
5563*94c894bbSVikram Hegde 		*dipp = ddi_alias_redirect(alias);
5564*94c894bbSVikram Hegde 		error = (*dipp ? NDI_SUCCESS : NDI_FAILURE);
5565*94c894bbSVikram Hegde 
5566*94c894bbSVikram Hegde 		kmem_free(alias, MAXPATHLEN);
5567*94c894bbSVikram Hegde 	}
5568*94c894bbSVikram Hegde 	kmem_free(nmdup, duplen);
5569*94c894bbSVikram Hegde 
5570*94c894bbSVikram Hegde 	if (error || !(flags & NDI_CONFIG)) {
5571*94c894bbSVikram Hegde 		pm_post_config(pdip, devnm);
55727c478bd9Sstevel@tonic-gate 		return (error);
55737c478bd9Sstevel@tonic-gate 	}
55747c478bd9Sstevel@tonic-gate 
55757c478bd9Sstevel@tonic-gate 	/*
5576f4da9be0Scth 	 * DR usage (i.e. call with NDI_CONFIG) recursively configures
55777c478bd9Sstevel@tonic-gate 	 * grandchildren, performing a BUS_CONFIG_ALL from the node attached
55787c478bd9Sstevel@tonic-gate 	 * by the BUS_CONFIG_ONE.
55797c478bd9Sstevel@tonic-gate 	 */
55807c478bd9Sstevel@tonic-gate 	ASSERT(*dipp);
5581a204de77Scth 	error = devi_config_common(*dipp, flags, DDI_MAJOR_T_NONE);
55827c478bd9Sstevel@tonic-gate 
5583*94c894bbSVikram Hegde 	pm_post_config(pdip, devnm);
55847c478bd9Sstevel@tonic-gate 
55857c478bd9Sstevel@tonic-gate 	if (branch_event)
55867c478bd9Sstevel@tonic-gate 		(void) i_log_devfs_branch_add(*dipp);
55877c478bd9Sstevel@tonic-gate 
55887c478bd9Sstevel@tonic-gate 	return (error);
55897c478bd9Sstevel@tonic-gate }
55907c478bd9Sstevel@tonic-gate 
55917c478bd9Sstevel@tonic-gate /*
55927c478bd9Sstevel@tonic-gate  * Enumerate and attach a child specified by name 'devnm'.
55937c478bd9Sstevel@tonic-gate  * Called during configure the OBP options. This configures
55947c478bd9Sstevel@tonic-gate  * only one node.
55957c478bd9Sstevel@tonic-gate  */
55967c478bd9Sstevel@tonic-gate static int
55977c478bd9Sstevel@tonic-gate ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
55987c478bd9Sstevel@tonic-gate     dev_info_t **childp, int flags)
55997c478bd9Sstevel@tonic-gate {
56007c478bd9Sstevel@tonic-gate 	int error;
56017c478bd9Sstevel@tonic-gate 	int (*f)();
56027c478bd9Sstevel@tonic-gate 
56037c478bd9Sstevel@tonic-gate 	ASSERT(childp);
5604737d277aScth 	ASSERT(i_ddi_devi_attached(parent));
56057c478bd9Sstevel@tonic-gate 
56067c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_config_obp_args: "
56077c478bd9Sstevel@tonic-gate 	    "par = %s%d (%p), child = %s\n", ddi_driver_name(parent),
56087c478bd9Sstevel@tonic-gate 	    ddi_get_instance(parent), (void *)parent, devnm));
56097c478bd9Sstevel@tonic-gate 
56107c478bd9Sstevel@tonic-gate 	if ((DEVI(parent)->devi_ops->devo_bus_ops == NULL) ||
56117c478bd9Sstevel@tonic-gate 	    (DEVI(parent)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
56127c478bd9Sstevel@tonic-gate 	    (f = DEVI(parent)->devi_ops->devo_bus_ops->bus_config) == NULL) {
56137c478bd9Sstevel@tonic-gate 		error = NDI_FAILURE;
56147c478bd9Sstevel@tonic-gate 	} else {
56157c478bd9Sstevel@tonic-gate 		/* call bus_config entry point */
56167c478bd9Sstevel@tonic-gate 		error = (*f)(parent, flags,
56177c478bd9Sstevel@tonic-gate 		    BUS_CONFIG_OBP_ARGS, (void *)devnm, childp);
56187c478bd9Sstevel@tonic-gate 	}
56197c478bd9Sstevel@tonic-gate 	return (error);
56207c478bd9Sstevel@tonic-gate }
56217c478bd9Sstevel@tonic-gate 
562225e8c5aaSvikram /*
562325e8c5aaSvikram  * Pay attention, the following is a bit tricky:
562425e8c5aaSvikram  * There are three possible cases when constraints are applied
562525e8c5aaSvikram  *
562625e8c5aaSvikram  *	- A constraint is applied and the offline is disallowed.
562725e8c5aaSvikram  *	  Simply return failure and block the offline
562825e8c5aaSvikram  *
562925e8c5aaSvikram  *	- A constraint is applied and the offline is allowed.
563025e8c5aaSvikram  *	  Mark the dip as having passed the constraint and allow
563125e8c5aaSvikram  *	  offline to proceed.
563225e8c5aaSvikram  *
563325e8c5aaSvikram  *	- A constraint is not applied. Allow the offline to proceed for now.
563425e8c5aaSvikram  *
563525e8c5aaSvikram  * In the latter two cases we allow the offline to proceed. If the
563625e8c5aaSvikram  * offline succeeds (no users) everything is fine. It is ok for an unused
563725e8c5aaSvikram  * device to be offlined even if no constraints were imposed on the offline.
563825e8c5aaSvikram  * If the offline fails because there are users, we look at the constraint
563925e8c5aaSvikram  * flag on the dip. If the constraint flag is set (implying that it passed
564025e8c5aaSvikram  * a constraint) we allow the dip to be retired. If not, we don't allow
564125e8c5aaSvikram  * the retire. This ensures that we don't allow unconstrained retire.
564225e8c5aaSvikram  */
564325e8c5aaSvikram int
564425e8c5aaSvikram e_ddi_offline_notify(dev_info_t *dip)
564525e8c5aaSvikram {
564625e8c5aaSvikram 	int retval;
564725e8c5aaSvikram 	int constraint;
564825e8c5aaSvikram 	int failure;
564925e8c5aaSvikram 
565025e8c5aaSvikram 	RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): entered: dip=%p",
565125e8c5aaSvikram 	    (void *) dip));
565225e8c5aaSvikram 
565325e8c5aaSvikram 	constraint = 0;
565425e8c5aaSvikram 	failure = 0;
565525e8c5aaSvikram 
565625e8c5aaSvikram 	/*
565725e8c5aaSvikram 	 * Start with userland constraints first - applied via device contracts
565825e8c5aaSvikram 	 */
565925e8c5aaSvikram 	retval = contract_device_offline(dip, DDI_DEV_T_ANY, 0);
566025e8c5aaSvikram 	switch (retval) {
566125e8c5aaSvikram 	case CT_NACK:
566225e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "Received NACK for dip=%p", (void *)dip));
566325e8c5aaSvikram 		failure = 1;
566425e8c5aaSvikram 		goto out;
566525e8c5aaSvikram 	case CT_ACK:
566625e8c5aaSvikram 		constraint = 1;
566725e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "Received ACK for dip=%p", (void *)dip));
566825e8c5aaSvikram 		break;
566925e8c5aaSvikram 	case CT_NONE:
567025e8c5aaSvikram 		/* no contracts */
567125e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "No contracts on dip=%p", (void *)dip));
567225e8c5aaSvikram 		break;
567325e8c5aaSvikram 	default:
567425e8c5aaSvikram 		ASSERT(retval == CT_NONE);
567525e8c5aaSvikram 	}
567625e8c5aaSvikram 
567725e8c5aaSvikram 	/*
567825e8c5aaSvikram 	 * Next, use LDI to impose kernel constraints
567925e8c5aaSvikram 	 */
568025e8c5aaSvikram 	retval = ldi_invoke_notify(dip, DDI_DEV_T_ANY, 0, LDI_EV_OFFLINE, NULL);
568125e8c5aaSvikram 	switch (retval) {
568225e8c5aaSvikram 	case LDI_EV_FAILURE:
568325e8c5aaSvikram 		contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_FAILURE);
568425e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "LDI callback failed on dip=%p",
568525e8c5aaSvikram 		    (void *)dip));
568625e8c5aaSvikram 		failure = 1;
568725e8c5aaSvikram 		goto out;
568825e8c5aaSvikram 	case LDI_EV_SUCCESS:
568925e8c5aaSvikram 		constraint = 1;
569025e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "LDI callback success on dip=%p",
569125e8c5aaSvikram 		    (void *)dip));
569225e8c5aaSvikram 		break;
569325e8c5aaSvikram 	case LDI_EV_NONE:
569425e8c5aaSvikram 		/* no matching LDI callbacks */
569525e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "No LDI callbacks for dip=%p",
569625e8c5aaSvikram 		    (void *)dip));
569725e8c5aaSvikram 		break;
569825e8c5aaSvikram 	default:
569925e8c5aaSvikram 		ASSERT(retval == LDI_EV_NONE);
570025e8c5aaSvikram 	}
570125e8c5aaSvikram 
570225e8c5aaSvikram out:
570325e8c5aaSvikram 	mutex_enter(&(DEVI(dip)->devi_lock));
570425e8c5aaSvikram 	if ((DEVI(dip)->devi_flags & DEVI_RETIRING) && failure) {
570525e8c5aaSvikram 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): setting "
570625e8c5aaSvikram 		    "BLOCKED flag. dip=%p", (void *)dip));
570725e8c5aaSvikram 		DEVI(dip)->devi_flags |= DEVI_R_BLOCKED;
570825e8c5aaSvikram 		if (DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT) {
570925e8c5aaSvikram 			RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): "
571025e8c5aaSvikram 			    "blocked. clearing RCM CONSTRAINT flag. dip=%p",
571125e8c5aaSvikram 			    (void *)dip));
571225e8c5aaSvikram 			DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
571325e8c5aaSvikram 		}
571425e8c5aaSvikram 	} else if ((DEVI(dip)->devi_flags & DEVI_RETIRING) && constraint) {
571525e8c5aaSvikram 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): setting "
571625e8c5aaSvikram 		    "CONSTRAINT flag. dip=%p", (void *)dip));
571725e8c5aaSvikram 		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
571825e8c5aaSvikram 	} else if ((DEVI(dip)->devi_flags & DEVI_RETIRING) &&
5719bf002425SStephen Hanson 	    ((DEVI(dip)->devi_ops != NULL &&
5720bf002425SStephen Hanson 	    DEVI(dip)->devi_ops->devo_bus_ops != NULL) ||
5721bf002425SStephen Hanson 	    DEVI(dip)->devi_ref == 0)) {
5722bf002425SStephen Hanson 		/* also allow retire if nexus or if device is not in use */
572325e8c5aaSvikram 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): device not in "
572425e8c5aaSvikram 		    "use. Setting CONSTRAINT flag. dip=%p", (void *)dip));
572525e8c5aaSvikram 		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
572625e8c5aaSvikram 	} else {
572725e8c5aaSvikram 		/*
572825e8c5aaSvikram 		 * Note: We cannot ASSERT here that DEVI_R_CONSTRAINT is
572925e8c5aaSvikram 		 * not set, since other sources (such as RCM) may have
573025e8c5aaSvikram 		 * set the flag.
573125e8c5aaSvikram 		 */
573225e8c5aaSvikram 		RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): not setting "
573325e8c5aaSvikram 		    "constraint flag. dip=%p", (void *)dip));
573425e8c5aaSvikram 	}
573525e8c5aaSvikram 	mutex_exit(&(DEVI(dip)->devi_lock));
573625e8c5aaSvikram 
573725e8c5aaSvikram 
573825e8c5aaSvikram 	RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): exit: dip=%p",
573925e8c5aaSvikram 	    (void *) dip));
574025e8c5aaSvikram 
574125e8c5aaSvikram 	return (failure ? DDI_FAILURE : DDI_SUCCESS);
574225e8c5aaSvikram }
574325e8c5aaSvikram 
574425e8c5aaSvikram void
574525e8c5aaSvikram e_ddi_offline_finalize(dev_info_t *dip, int result)
574625e8c5aaSvikram {
574725e8c5aaSvikram 	RIO_DEBUG((CE_NOTE, "e_ddi_offline_finalize(): entry: result=%s, "
574825e8c5aaSvikram 	    "dip=%p", result == DDI_SUCCESS ? "SUCCESS" : "FAILURE",
574925e8c5aaSvikram 	    (void *)dip));
575025e8c5aaSvikram 
575125e8c5aaSvikram 	contract_device_negend(dip, DDI_DEV_T_ANY, 0,  result == DDI_SUCCESS ?
575225e8c5aaSvikram 	    CT_EV_SUCCESS : CT_EV_FAILURE);
575325e8c5aaSvikram 
575425e8c5aaSvikram 	ldi_invoke_finalize(dip, DDI_DEV_T_ANY, 0,
575525e8c5aaSvikram 	    LDI_EV_OFFLINE, result == DDI_SUCCESS ?
575625e8c5aaSvikram 	    LDI_EV_SUCCESS : LDI_EV_FAILURE, NULL);
575725e8c5aaSvikram 
575825e8c5aaSvikram 	RIO_VERBOSE((CE_NOTE, "e_ddi_offline_finalize(): exit: dip=%p",
575925e8c5aaSvikram 	    (void *)dip));
576025e8c5aaSvikram }
576125e8c5aaSvikram 
576225e8c5aaSvikram void
576325e8c5aaSvikram e_ddi_degrade_finalize(dev_info_t *dip)
576425e8c5aaSvikram {
576525e8c5aaSvikram 	RIO_DEBUG((CE_NOTE, "e_ddi_degrade_finalize(): entry: "
576625e8c5aaSvikram 	    "result always = DDI_SUCCESS, dip=%p", (void *)dip));
576725e8c5aaSvikram 
576825e8c5aaSvikram 	contract_device_degrade(dip, DDI_DEV_T_ANY, 0);
576925e8c5aaSvikram 	contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_SUCCESS);
577025e8c5aaSvikram 
577125e8c5aaSvikram 	ldi_invoke_finalize(dip, DDI_DEV_T_ANY, 0, LDI_EV_DEGRADE,
577225e8c5aaSvikram 	    LDI_EV_SUCCESS, NULL);
577325e8c5aaSvikram 
577425e8c5aaSvikram 	RIO_VERBOSE((CE_NOTE, "e_ddi_degrade_finalize(): exit: dip=%p",
577525e8c5aaSvikram 	    (void *)dip));
577625e8c5aaSvikram }
577725e8c5aaSvikram 
577825e8c5aaSvikram void
577925e8c5aaSvikram e_ddi_undegrade_finalize(dev_info_t *dip)
578025e8c5aaSvikram {
578125e8c5aaSvikram 	RIO_DEBUG((CE_NOTE, "e_ddi_undegrade_finalize(): entry: "
578225e8c5aaSvikram 	    "result always = DDI_SUCCESS, dip=%p", (void *)dip));
578325e8c5aaSvikram 
578425e8c5aaSvikram 	contract_device_undegrade(dip, DDI_DEV_T_ANY, 0);
578525e8c5aaSvikram 	contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_SUCCESS);
578625e8c5aaSvikram 
578725e8c5aaSvikram 	RIO_VERBOSE((CE_NOTE, "e_ddi_undegrade_finalize(): exit: dip=%p",
578825e8c5aaSvikram 	    (void *)dip));
578925e8c5aaSvikram }
57907c478bd9Sstevel@tonic-gate 
57917c478bd9Sstevel@tonic-gate /*
57927c478bd9Sstevel@tonic-gate  * detach a node with parent already held busy
57937c478bd9Sstevel@tonic-gate  */
57947c478bd9Sstevel@tonic-gate static int
57957c478bd9Sstevel@tonic-gate devi_detach_node(dev_info_t *dip, uint_t flags)
57967c478bd9Sstevel@tonic-gate {
57977c478bd9Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
57987c478bd9Sstevel@tonic-gate 	int ret = NDI_SUCCESS;
57997c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t cookie;
58006c75e108SVikram Hegde 	char *path = NULL;
58016c75e108SVikram Hegde 	char *class = NULL;
58026c75e108SVikram Hegde 	char *driver = NULL;
58036c75e108SVikram Hegde 	int instance = -1;
58046c75e108SVikram Hegde 	int post_event = 0;
58057c478bd9Sstevel@tonic-gate 
58065e3986cbScth 	ASSERT(pdip && DEVI_BUSY_OWNED(pdip));
58075e3986cbScth 
580825e8c5aaSvikram 	/*
580925e8c5aaSvikram 	 * Invoke notify if offlining
581025e8c5aaSvikram 	 */
581125e8c5aaSvikram 	if (flags & NDI_DEVI_OFFLINE) {
581225e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "devi_detach_node: offlining dip=%p",
581325e8c5aaSvikram 		    (void *)dip));
581425e8c5aaSvikram 		if (e_ddi_offline_notify(dip) != DDI_SUCCESS) {
581525e8c5aaSvikram 			RIO_DEBUG((CE_NOTE, "devi_detach_node: offline NACKed"
581625e8c5aaSvikram 			    "dip=%p", (void *)dip));
581725e8c5aaSvikram 			return (NDI_FAILURE);
581825e8c5aaSvikram 		}
581925e8c5aaSvikram 	}
582025e8c5aaSvikram 
58217c478bd9Sstevel@tonic-gate 	if (flags & NDI_POST_EVENT) {
58225e3986cbScth 		if (i_ddi_devi_attached(pdip)) {
58237c478bd9Sstevel@tonic-gate 			if (ddi_get_eventcookie(dip, DDI_DEVI_REMOVE_EVENT,
58247c478bd9Sstevel@tonic-gate 			    &cookie) == NDI_SUCCESS)
58257c478bd9Sstevel@tonic-gate 				(void) ndi_post_event(dip, dip, cookie, NULL);
58267c478bd9Sstevel@tonic-gate 		}
58277c478bd9Sstevel@tonic-gate 	}
58287c478bd9Sstevel@tonic-gate 
582925e8c5aaSvikram 	if (i_ddi_detachchild(dip, flags) != DDI_SUCCESS) {
583025e8c5aaSvikram 		if (flags & NDI_DEVI_OFFLINE) {
583125e8c5aaSvikram 			RIO_DEBUG((CE_NOTE, "devi_detach_node: offline failed."
583225e8c5aaSvikram 			    " Calling e_ddi_offline_finalize with result=%d. "
583325e8c5aaSvikram 			    "dip=%p", DDI_FAILURE, (void *)dip));
583425e8c5aaSvikram 			e_ddi_offline_finalize(dip, DDI_FAILURE);
583525e8c5aaSvikram 		}
58367c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
583725e8c5aaSvikram 	}
583825e8c5aaSvikram 
583925e8c5aaSvikram 	if (flags & NDI_DEVI_OFFLINE) {
584025e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "devi_detach_node: offline succeeded."
584125e8c5aaSvikram 		    " Calling e_ddi_offline_finalize with result=%d, "
584225e8c5aaSvikram 		    "dip=%p", DDI_SUCCESS, (void *)dip));
584325e8c5aaSvikram 		e_ddi_offline_finalize(dip, DDI_SUCCESS);
584425e8c5aaSvikram 	}
58457c478bd9Sstevel@tonic-gate 
58467c478bd9Sstevel@tonic-gate 	if (flags & NDI_AUTODETACH)
58477c478bd9Sstevel@tonic-gate 		return (NDI_SUCCESS);
58487c478bd9Sstevel@tonic-gate 
58497c478bd9Sstevel@tonic-gate 	/*
58507c478bd9Sstevel@tonic-gate 	 * For DR, even bound nodes may need to have offline
58517c478bd9Sstevel@tonic-gate 	 * flag set.
58527c478bd9Sstevel@tonic-gate 	 */
58537c478bd9Sstevel@tonic-gate 	if (flags & NDI_DEVI_OFFLINE) {
585416747f41Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
58557c478bd9Sstevel@tonic-gate 		DEVI_SET_DEVICE_OFFLINE(dip);
585616747f41Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
58577c478bd9Sstevel@tonic-gate 	}
58587c478bd9Sstevel@tonic-gate 
58597c478bd9Sstevel@tonic-gate 	if (i_ddi_node_state(dip) == DS_INITIALIZED) {
58606c75e108SVikram Hegde 		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
58617c478bd9Sstevel@tonic-gate 		(void) ddi_pathname(dip, path);
58627c478bd9Sstevel@tonic-gate 		if (flags & NDI_DEVI_OFFLINE)
58637c478bd9Sstevel@tonic-gate 			i_ndi_devi_report_status_change(dip, path);
58647c478bd9Sstevel@tonic-gate 
58657c478bd9Sstevel@tonic-gate 		if (need_remove_event(dip, flags)) {
58666c75e108SVikram Hegde 			post_event = 1;
58676c75e108SVikram Hegde 			class = i_ddi_strdup(i_ddi_devi_class(dip), KM_SLEEP);
58686c75e108SVikram Hegde 			driver = i_ddi_strdup((char *)ddi_driver_name(dip),
58696c75e108SVikram Hegde 			    KM_SLEEP);
58706c75e108SVikram Hegde 			instance = ddi_get_instance(dip);
58716c75e108SVikram Hegde 
587216747f41Scth 			mutex_enter(&(DEVI(dip)->devi_lock));
58737c478bd9Sstevel@tonic-gate 			DEVI_SET_EVREMOVE(dip);
587416747f41Scth 			mutex_exit(&(DEVI(dip)->devi_lock));
58757c478bd9Sstevel@tonic-gate 		}
58767c478bd9Sstevel@tonic-gate 	}
58777c478bd9Sstevel@tonic-gate 
58787c478bd9Sstevel@tonic-gate 	if (flags & (NDI_UNCONFIG | NDI_DEVI_REMOVE)) {
58797c478bd9Sstevel@tonic-gate 		ret = ddi_uninitchild(dip);
58807c478bd9Sstevel@tonic-gate 		if (ret == NDI_SUCCESS) {
58817c478bd9Sstevel@tonic-gate 			/*
58827c478bd9Sstevel@tonic-gate 			 * Remove uninitialized pseudo nodes because
58837c478bd9Sstevel@tonic-gate 			 * system props are lost and the node cannot be
58847c478bd9Sstevel@tonic-gate 			 * reattached.
58857c478bd9Sstevel@tonic-gate 			 */
58867c478bd9Sstevel@tonic-gate 			if (!ndi_dev_is_persistent_node(dip))
58877c478bd9Sstevel@tonic-gate 				flags |= NDI_DEVI_REMOVE;
58887c478bd9Sstevel@tonic-gate 
58896c75e108SVikram Hegde 			if (flags & NDI_DEVI_REMOVE) {
58907c478bd9Sstevel@tonic-gate 				ret = ddi_remove_child(dip, 0);
58916c75e108SVikram Hegde 				if (post_event && ret == NDI_SUCCESS) {
58926c75e108SVikram Hegde 					(void) i_log_devfs_remove_devinfo(path,
58936c75e108SVikram Hegde 					    class, driver, instance, flags);
58947c478bd9Sstevel@tonic-gate 				}
58957c478bd9Sstevel@tonic-gate 			}
58967c478bd9Sstevel@tonic-gate 
58976c75e108SVikram Hegde 		}
58986c75e108SVikram Hegde 	}
58996c75e108SVikram Hegde 
59006c75e108SVikram Hegde 	if (path)
59016c75e108SVikram Hegde 		kmem_free(path, MAXPATHLEN);
59026c75e108SVikram Hegde 	if (class)
59036c75e108SVikram Hegde 		kmem_free(class, strlen(class) + 1);
59046c75e108SVikram Hegde 	if (driver)
59056c75e108SVikram Hegde 		kmem_free(driver, strlen(driver) + 1);
59066c75e108SVikram Hegde 
59077c478bd9Sstevel@tonic-gate 	return (ret);
59087c478bd9Sstevel@tonic-gate }
59097c478bd9Sstevel@tonic-gate 
59107c478bd9Sstevel@tonic-gate /*
59117c478bd9Sstevel@tonic-gate  * unconfigure immediate children of bus nexus device
59127c478bd9Sstevel@tonic-gate  */
59137c478bd9Sstevel@tonic-gate static int
59147c478bd9Sstevel@tonic-gate unconfig_immediate_children(
59157c478bd9Sstevel@tonic-gate 	dev_info_t *dip,
59167c478bd9Sstevel@tonic-gate 	dev_info_t **dipp,
59177c478bd9Sstevel@tonic-gate 	int flags,
59187c478bd9Sstevel@tonic-gate 	major_t major)
59197c478bd9Sstevel@tonic-gate {
59205e3986cbScth 	int rv = NDI_SUCCESS;
59215e3986cbScth 	int circ, vcirc;
59227c478bd9Sstevel@tonic-gate 	dev_info_t *child;
59235e3986cbScth 	dev_info_t *vdip = NULL;
59245e3986cbScth 	dev_info_t *next;
59257c478bd9Sstevel@tonic-gate 
59267c478bd9Sstevel@tonic-gate 	ASSERT(dipp == NULL || *dipp == NULL);
59277c478bd9Sstevel@tonic-gate 
59285e3986cbScth 	/*
59295e3986cbScth 	 * Scan forward to see if we will be processing a pHCI child. If we
59305e3986cbScth 	 * have a child that is a pHCI and vHCI and pHCI are not siblings then
59315e3986cbScth 	 * enter vHCI before parent(pHCI) to prevent deadlock with mpxio
59325e3986cbScth 	 * Client power management operations.
59335e3986cbScth 	 */
59347c478bd9Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
59355e3986cbScth 	for (child = ddi_get_child(dip); child;
59365e3986cbScth 	    child = ddi_get_next_sibling(child)) {
59375e3986cbScth 		/* skip same nodes we skip below */
5938a204de77Scth 		if (((major != DDI_MAJOR_T_NONE) &&
59395e3986cbScth 		    (major != ddi_driver_major(child))) ||
59405e3986cbScth 		    ((flags & NDI_AUTODETACH) && !is_leaf_node(child)))
59415e3986cbScth 			continue;
59425e3986cbScth 
59435e3986cbScth 		if (MDI_PHCI(child)) {
59445e3986cbScth 			vdip = mdi_devi_get_vdip(child);
59455e3986cbScth 			/*
59465e3986cbScth 			 * If vHCI and vHCI is not a sibling of pHCI
59475e3986cbScth 			 * then enter in (vHCI, parent(pHCI)) order.
59485e3986cbScth 			 */
59495e3986cbScth 			if (vdip && (ddi_get_parent(vdip) != dip)) {
59505e3986cbScth 				ndi_devi_exit(dip, circ);
59515e3986cbScth 
59525e3986cbScth 				/* use mdi_devi_enter ordering */
59535e3986cbScth 				ndi_devi_enter(vdip, &vcirc);
59545e3986cbScth 				ndi_devi_enter(dip, &circ);
59555e3986cbScth 				break;
59565e3986cbScth 			} else
59575e3986cbScth 				vdip = NULL;
59585e3986cbScth 		}
59595e3986cbScth 	}
59605e3986cbScth 
59617c478bd9Sstevel@tonic-gate 	child = ddi_get_child(dip);
59627c478bd9Sstevel@tonic-gate 	while (child) {
59635e3986cbScth 		next = ddi_get_next_sibling(child);
59645e3986cbScth 
5965a204de77Scth 		if ((major != DDI_MAJOR_T_NONE) &&
59667c478bd9Sstevel@tonic-gate 		    (major != ddi_driver_major(child))) {
59677c478bd9Sstevel@tonic-gate 			child = next;
59687c478bd9Sstevel@tonic-gate 			continue;
59697c478bd9Sstevel@tonic-gate 		}
59707c478bd9Sstevel@tonic-gate 
59717c478bd9Sstevel@tonic-gate 		/* skip nexus nodes during autodetach */
59727c478bd9Sstevel@tonic-gate 		if ((flags & NDI_AUTODETACH) && !is_leaf_node(child)) {
59737c478bd9Sstevel@tonic-gate 			child = next;
59747c478bd9Sstevel@tonic-gate 			continue;
59757c478bd9Sstevel@tonic-gate 		}
59767c478bd9Sstevel@tonic-gate 
59777c478bd9Sstevel@tonic-gate 		if (devi_detach_node(child, flags) != NDI_SUCCESS) {
59787c478bd9Sstevel@tonic-gate 			if (dipp && *dipp == NULL) {
59797c478bd9Sstevel@tonic-gate 				ndi_hold_devi(child);
59807c478bd9Sstevel@tonic-gate 				*dipp = child;
59817c478bd9Sstevel@tonic-gate 			}
59827c478bd9Sstevel@tonic-gate 			rv = NDI_FAILURE;
59837c478bd9Sstevel@tonic-gate 		}
59847c478bd9Sstevel@tonic-gate 
59857c478bd9Sstevel@tonic-gate 		/*
59867c478bd9Sstevel@tonic-gate 		 * Continue upon failure--best effort algorithm
59877c478bd9Sstevel@tonic-gate 		 */
59887c478bd9Sstevel@tonic-gate 		child = next;
59897c478bd9Sstevel@tonic-gate 	}
59905e3986cbScth 
59917c478bd9Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
59925e3986cbScth 	if (vdip)
59935e3986cbScth 		ndi_devi_exit(vdip, vcirc);
59945e3986cbScth 
59957c478bd9Sstevel@tonic-gate 	return (rv);
59967c478bd9Sstevel@tonic-gate }
59977c478bd9Sstevel@tonic-gate 
59987c478bd9Sstevel@tonic-gate /*
59997c478bd9Sstevel@tonic-gate  * unconfigure grand children of bus nexus device
60007c478bd9Sstevel@tonic-gate  */
60017c478bd9Sstevel@tonic-gate static int
60027c478bd9Sstevel@tonic-gate unconfig_grand_children(
60037c478bd9Sstevel@tonic-gate 	dev_info_t *dip,
60047c478bd9Sstevel@tonic-gate 	dev_info_t **dipp,
60057c478bd9Sstevel@tonic-gate 	int flags,
60067c478bd9Sstevel@tonic-gate 	major_t major,
60077c478bd9Sstevel@tonic-gate 	struct brevq_node **brevqp)
60087c478bd9Sstevel@tonic-gate {
60097c478bd9Sstevel@tonic-gate 	struct mt_config_handle *hdl;
60107c478bd9Sstevel@tonic-gate 
60117c478bd9Sstevel@tonic-gate 	if (brevqp)
60127c478bd9Sstevel@tonic-gate 		*brevqp = NULL;
60137c478bd9Sstevel@tonic-gate 
60147c478bd9Sstevel@tonic-gate 	/* multi-threaded configuration of child nexus */
60157c478bd9Sstevel@tonic-gate 	hdl = mt_config_init(dip, dipp, flags, major, MT_UNCONFIG_OP, brevqp);
60167c478bd9Sstevel@tonic-gate 	mt_config_children(hdl);
60177c478bd9Sstevel@tonic-gate 
60187c478bd9Sstevel@tonic-gate 	return (mt_config_fini(hdl));	/* wait for threads to exit */
60197c478bd9Sstevel@tonic-gate }
60207c478bd9Sstevel@tonic-gate 
60217c478bd9Sstevel@tonic-gate /*
60227c478bd9Sstevel@tonic-gate  * Unconfigure children/descendants of the dip.
60237c478bd9Sstevel@tonic-gate  *
60247c478bd9Sstevel@tonic-gate  * If brevqp is not NULL, on return *brevqp is set to a queue of dip's
60257c478bd9Sstevel@tonic-gate  * child devinames for which branch remove events need to be generated.
60267c478bd9Sstevel@tonic-gate  */
60277c478bd9Sstevel@tonic-gate static int
60287c478bd9Sstevel@tonic-gate devi_unconfig_common(
60297c478bd9Sstevel@tonic-gate 	dev_info_t *dip,
60307c478bd9Sstevel@tonic-gate 	dev_info_t **dipp,
60317c478bd9Sstevel@tonic-gate 	int flags,
60327c478bd9Sstevel@tonic-gate 	major_t major,
60337c478bd9Sstevel@tonic-gate 	struct brevq_node **brevqp)
60347c478bd9Sstevel@tonic-gate {
60357c478bd9Sstevel@tonic-gate 	int rv;
60367c478bd9Sstevel@tonic-gate 	int pm_cookie;
60377c478bd9Sstevel@tonic-gate 	int (*f)();
60387c478bd9Sstevel@tonic-gate 	ddi_bus_config_op_t bus_op;
60397c478bd9Sstevel@tonic-gate 
60407c478bd9Sstevel@tonic-gate 	if (dipp)
60417c478bd9Sstevel@tonic-gate 		*dipp = NULL;
60427c478bd9Sstevel@tonic-gate 	if (brevqp)
60437c478bd9Sstevel@tonic-gate 		*brevqp = NULL;
60447c478bd9Sstevel@tonic-gate 
60457c478bd9Sstevel@tonic-gate 	/*
60467c478bd9Sstevel@tonic-gate 	 * Power up the dip if it is powered off.  If the flag bit
60477c478bd9Sstevel@tonic-gate 	 * NDI_AUTODETACH is set and the dip is not at its full power,
60487c478bd9Sstevel@tonic-gate 	 * skip the rest of the branch.
60497c478bd9Sstevel@tonic-gate 	 */
60507c478bd9Sstevel@tonic-gate 	if (pm_pre_unconfig(dip, flags, &pm_cookie, NULL) != DDI_SUCCESS)
60517c478bd9Sstevel@tonic-gate 		return ((flags & NDI_AUTODETACH) ? NDI_SUCCESS :
60527c478bd9Sstevel@tonic-gate 		    NDI_FAILURE);
60537c478bd9Sstevel@tonic-gate 
60547c478bd9Sstevel@tonic-gate 	/*
60557c478bd9Sstevel@tonic-gate 	 * Some callers, notably SCSI, need to clear out the devfs
60567c478bd9Sstevel@tonic-gate 	 * cache together with the unconfig to prevent stale entries.
60577c478bd9Sstevel@tonic-gate 	 */
60587c478bd9Sstevel@tonic-gate 	if (flags & NDI_DEVFS_CLEAN)
60597c478bd9Sstevel@tonic-gate 		(void) devfs_clean(dip, NULL, 0);
60607c478bd9Sstevel@tonic-gate 
60617c478bd9Sstevel@tonic-gate 	rv = unconfig_grand_children(dip, dipp, flags, major, brevqp);
60627c478bd9Sstevel@tonic-gate 
60637c478bd9Sstevel@tonic-gate 	if ((rv != NDI_SUCCESS) && ((flags & NDI_AUTODETACH) == 0)) {
60647c478bd9Sstevel@tonic-gate 		if (brevqp && *brevqp) {
60657c478bd9Sstevel@tonic-gate 			log_and_free_br_events_on_grand_children(dip, *brevqp);
60667c478bd9Sstevel@tonic-gate 			free_brevq(*brevqp);
60677c478bd9Sstevel@tonic-gate 			*brevqp = NULL;
60687c478bd9Sstevel@tonic-gate 		}
60697c478bd9Sstevel@tonic-gate 		pm_post_unconfig(dip, pm_cookie, NULL);
60707c478bd9Sstevel@tonic-gate 		return (rv);
60717c478bd9Sstevel@tonic-gate 	}
60727c478bd9Sstevel@tonic-gate 
60737c478bd9Sstevel@tonic-gate 	if (dipp && *dipp) {
60747c478bd9Sstevel@tonic-gate 		ndi_rele_devi(*dipp);
60757c478bd9Sstevel@tonic-gate 		*dipp = NULL;
60767c478bd9Sstevel@tonic-gate 	}
60777c478bd9Sstevel@tonic-gate 
60787c478bd9Sstevel@tonic-gate 	/*
60797c478bd9Sstevel@tonic-gate 	 * It is possible to have a detached nexus with children
60807c478bd9Sstevel@tonic-gate 	 * and grandchildren (for example: a branch consisting
60817c478bd9Sstevel@tonic-gate 	 * entirely of bound nodes.) Since the nexus is detached
60827c478bd9Sstevel@tonic-gate 	 * the bus_unconfig entry point cannot be used to remove
60837c478bd9Sstevel@tonic-gate 	 * or unconfigure the descendants.
60847c478bd9Sstevel@tonic-gate 	 */
6085737d277aScth 	if (!i_ddi_devi_attached(dip) ||
60867c478bd9Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
60877c478bd9Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
60887c478bd9Sstevel@tonic-gate 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
60897c478bd9Sstevel@tonic-gate 		rv = unconfig_immediate_children(dip, dipp, flags, major);
60907c478bd9Sstevel@tonic-gate 	} else {
60917c478bd9Sstevel@tonic-gate 		/*
60927c478bd9Sstevel@tonic-gate 		 * call bus_unconfig entry point
60937c478bd9Sstevel@tonic-gate 		 * It should reset nexus flags if unconfigure succeeds.
60947c478bd9Sstevel@tonic-gate 		 */
6095a204de77Scth 		bus_op = (major == DDI_MAJOR_T_NONE) ?
60967c478bd9Sstevel@tonic-gate 		    BUS_UNCONFIG_ALL : BUS_UNCONFIG_DRIVER;
60977c478bd9Sstevel@tonic-gate 		rv = (*f)(dip, flags, bus_op, (void *)(uintptr_t)major);
60987c478bd9Sstevel@tonic-gate 	}
60997c478bd9Sstevel@tonic-gate 
61007c478bd9Sstevel@tonic-gate 	pm_post_unconfig(dip, pm_cookie, NULL);
61017c478bd9Sstevel@tonic-gate 
61027c478bd9Sstevel@tonic-gate 	if (brevqp && *brevqp)
61037c478bd9Sstevel@tonic-gate 		cleanup_br_events_on_grand_children(dip, brevqp);
61047c478bd9Sstevel@tonic-gate 
61057c478bd9Sstevel@tonic-gate 	return (rv);
61067c478bd9Sstevel@tonic-gate }
61077c478bd9Sstevel@tonic-gate 
61087c478bd9Sstevel@tonic-gate /*
61097c478bd9Sstevel@tonic-gate  * called by devfs/framework to unconfigure children bound to major
61107c478bd9Sstevel@tonic-gate  * If NDI_AUTODETACH is specified, this is invoked by either the
61117c478bd9Sstevel@tonic-gate  * moduninstall daemon or the modunload -i 0 command.
61127c478bd9Sstevel@tonic-gate  */
61137c478bd9Sstevel@tonic-gate int
61147c478bd9Sstevel@tonic-gate ndi_devi_unconfig_driver(dev_info_t *dip, int flags, major_t major)
61157c478bd9Sstevel@tonic-gate {
61167c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
61177c478bd9Sstevel@tonic-gate 	    "ndi_devi_unconfig_driver: par = %s%d (%p), flags = 0x%x\n",
61187c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
61197c478bd9Sstevel@tonic-gate 
61207c478bd9Sstevel@tonic-gate 	return (devi_unconfig_common(dip, NULL, flags, major, NULL));
61217c478bd9Sstevel@tonic-gate }
61227c478bd9Sstevel@tonic-gate 
61237c478bd9Sstevel@tonic-gate int
61247c478bd9Sstevel@tonic-gate ndi_devi_unconfig(dev_info_t *dip, int flags)
61257c478bd9Sstevel@tonic-gate {
61267c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
61277c478bd9Sstevel@tonic-gate 	    "ndi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
61287c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
61297c478bd9Sstevel@tonic-gate 
6130a204de77Scth 	return (devi_unconfig_common(dip, NULL, flags, DDI_MAJOR_T_NONE, NULL));
61317c478bd9Sstevel@tonic-gate }
61327c478bd9Sstevel@tonic-gate 
61337c478bd9Sstevel@tonic-gate int
61347c478bd9Sstevel@tonic-gate e_ddi_devi_unconfig(dev_info_t *dip, dev_info_t **dipp, int flags)
61357c478bd9Sstevel@tonic-gate {
61367c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
61377c478bd9Sstevel@tonic-gate 	    "e_ddi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
61387c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
61397c478bd9Sstevel@tonic-gate 
6140a204de77Scth 	return (devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE, NULL));
61417c478bd9Sstevel@tonic-gate }
61427c478bd9Sstevel@tonic-gate 
61437c478bd9Sstevel@tonic-gate /*
61447c478bd9Sstevel@tonic-gate  * Unconfigure child by name
61457c478bd9Sstevel@tonic-gate  */
61467c478bd9Sstevel@tonic-gate static int
61477c478bd9Sstevel@tonic-gate devi_unconfig_one(dev_info_t *pdip, char *devnm, int flags)
61487c478bd9Sstevel@tonic-gate {
61497c478bd9Sstevel@tonic-gate 	int		rv, circ;
61507c478bd9Sstevel@tonic-gate 	dev_info_t	*child;
61515e3986cbScth 	dev_info_t	*vdip = NULL;
61525e3986cbScth 	int		v_circ;
61537c478bd9Sstevel@tonic-gate 
61547c478bd9Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
61557c478bd9Sstevel@tonic-gate 	child = ndi_devi_findchild(pdip, devnm);
61565e3986cbScth 
61575e3986cbScth 	/*
61585e3986cbScth 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
61595e3986cbScth 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
61605e3986cbScth 	 * management operations.
61615e3986cbScth 	 */
61625e3986cbScth 	if (child && MDI_PHCI(child)) {
61635e3986cbScth 		vdip = mdi_devi_get_vdip(child);
61645e3986cbScth 		if (vdip && (ddi_get_parent(vdip) != pdip)) {
61655e3986cbScth 			ndi_devi_exit(pdip, circ);
61665e3986cbScth 
61675e3986cbScth 			/* use mdi_devi_enter ordering */
61685e3986cbScth 			ndi_devi_enter(vdip, &v_circ);
61695e3986cbScth 			ndi_devi_enter(pdip, &circ);
61705e3986cbScth 			child = ndi_devi_findchild(pdip, devnm);
61715e3986cbScth 		} else
61725e3986cbScth 			vdip = NULL;
61735e3986cbScth 	}
61745e3986cbScth 
61755e3986cbScth 	if (child) {
61765e3986cbScth 		rv = devi_detach_node(child, flags);
61775e3986cbScth 	} else {
61787c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
61797c478bd9Sstevel@tonic-gate 		    "devi_unconfig_one: %s not found\n", devnm));
61805e3986cbScth 		rv = NDI_SUCCESS;
6181c73a93f2Sdm120769 	}
61825e3986cbScth 
6183c73a93f2Sdm120769 	ndi_devi_exit(pdip, circ);
61845e3986cbScth 	if (vdip)
6185ffc89d77Svikram 		ndi_devi_exit(vdip, v_circ);
61865e3986cbScth 
61877c478bd9Sstevel@tonic-gate 	return (rv);
61887c478bd9Sstevel@tonic-gate }
61897c478bd9Sstevel@tonic-gate 
61907c478bd9Sstevel@tonic-gate int
61917c478bd9Sstevel@tonic-gate ndi_devi_unconfig_one(
61927c478bd9Sstevel@tonic-gate 	dev_info_t *pdip,
61937c478bd9Sstevel@tonic-gate 	char *devnm,
61947c478bd9Sstevel@tonic-gate 	dev_info_t **dipp,
61957c478bd9Sstevel@tonic-gate 	int flags)
61967c478bd9Sstevel@tonic-gate {
61977c478bd9Sstevel@tonic-gate 	int		(*f)();
61987c478bd9Sstevel@tonic-gate 	int		circ, rv;
61997c478bd9Sstevel@tonic-gate 	int		pm_cookie;
62007c478bd9Sstevel@tonic-gate 	dev_info_t	*child;
62015e3986cbScth 	dev_info_t	*vdip = NULL;
62025e3986cbScth 	int		v_circ;
62037c478bd9Sstevel@tonic-gate 	struct brevq_node *brevq = NULL;
62047c478bd9Sstevel@tonic-gate 
6205737d277aScth 	ASSERT(i_ddi_devi_attached(pdip));
62067c478bd9Sstevel@tonic-gate 
62077c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
62087c478bd9Sstevel@tonic-gate 	    "ndi_devi_unconfig_one: par = %s%d (%p), child = %s\n",
62097c478bd9Sstevel@tonic-gate 	    ddi_driver_name(pdip), ddi_get_instance(pdip),
62107c478bd9Sstevel@tonic-gate 	    (void *)pdip, devnm));
62117c478bd9Sstevel@tonic-gate 
62127c478bd9Sstevel@tonic-gate 	if (pm_pre_unconfig(pdip, flags, &pm_cookie, devnm) != DDI_SUCCESS)
62137c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
62147c478bd9Sstevel@tonic-gate 
62157c478bd9Sstevel@tonic-gate 	if (dipp)
62167c478bd9Sstevel@tonic-gate 		*dipp = NULL;
62177c478bd9Sstevel@tonic-gate 
62187c478bd9Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
62197c478bd9Sstevel@tonic-gate 	child = ndi_devi_findchild(pdip, devnm);
62205e3986cbScth 
62215e3986cbScth 	/*
62225e3986cbScth 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
62235e3986cbScth 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
62245e3986cbScth 	 * management operations.
62255e3986cbScth 	 */
62265e3986cbScth 	if (child && MDI_PHCI(child)) {
62275e3986cbScth 		vdip = mdi_devi_get_vdip(child);
62285e3986cbScth 		if (vdip && (ddi_get_parent(vdip) != pdip)) {
62295e3986cbScth 			ndi_devi_exit(pdip, circ);
62305e3986cbScth 
62315e3986cbScth 			/* use mdi_devi_enter ordering */
62325e3986cbScth 			ndi_devi_enter(vdip, &v_circ);
62335e3986cbScth 			ndi_devi_enter(pdip, &circ);
62345e3986cbScth 			child = ndi_devi_findchild(pdip, devnm);
62355e3986cbScth 		} else
62365e3986cbScth 			vdip = NULL;
62375e3986cbScth 	}
62385e3986cbScth 
62397c478bd9Sstevel@tonic-gate 	if (child == NULL) {
62407c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_unconfig_one: %s"
62417c478bd9Sstevel@tonic-gate 		    " not found\n", devnm));
62425e3986cbScth 		rv = NDI_SUCCESS;
62435e3986cbScth 		goto out;
62447c478bd9Sstevel@tonic-gate 	}
62457c478bd9Sstevel@tonic-gate 
62467c478bd9Sstevel@tonic-gate 	/*
62477c478bd9Sstevel@tonic-gate 	 * Unconfigure children/descendants of named child
62487c478bd9Sstevel@tonic-gate 	 */
62497c478bd9Sstevel@tonic-gate 	rv = devi_unconfig_branch(child, dipp, flags | NDI_UNCONFIG, &brevq);
62507c478bd9Sstevel@tonic-gate 	if (rv != NDI_SUCCESS)
62517c478bd9Sstevel@tonic-gate 		goto out;
62527c478bd9Sstevel@tonic-gate 
62537c478bd9Sstevel@tonic-gate 	init_bound_node_ev(pdip, child, flags);
62547c478bd9Sstevel@tonic-gate 
62557c478bd9Sstevel@tonic-gate 	if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
62567c478bd9Sstevel@tonic-gate 	    (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
62577c478bd9Sstevel@tonic-gate 	    (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
62587c478bd9Sstevel@tonic-gate 		rv = devi_detach_node(child, flags);
62597c478bd9Sstevel@tonic-gate 	} else {
62607c478bd9Sstevel@tonic-gate 		/* call bus_config entry point */
62617c478bd9Sstevel@tonic-gate 		rv = (*f)(pdip, flags, BUS_UNCONFIG_ONE, (void *)devnm);
62627c478bd9Sstevel@tonic-gate 	}
62637c478bd9Sstevel@tonic-gate 
62647c478bd9Sstevel@tonic-gate 	if (brevq) {
62657c478bd9Sstevel@tonic-gate 		if (rv != NDI_SUCCESS)
62667c478bd9Sstevel@tonic-gate 			log_and_free_brevq_dip(child, brevq);
62677c478bd9Sstevel@tonic-gate 		else
62687c478bd9Sstevel@tonic-gate 			free_brevq(brevq);
62697c478bd9Sstevel@tonic-gate 	}
62707c478bd9Sstevel@tonic-gate 
62717c478bd9Sstevel@tonic-gate 	if (dipp && rv != NDI_SUCCESS) {
62727c478bd9Sstevel@tonic-gate 		ndi_hold_devi(child);
62737c478bd9Sstevel@tonic-gate 		ASSERT(*dipp == NULL);
62747c478bd9Sstevel@tonic-gate 		*dipp = child;
62757c478bd9Sstevel@tonic-gate 	}
62767c478bd9Sstevel@tonic-gate 
62777c478bd9Sstevel@tonic-gate out:
62787c478bd9Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
62795e3986cbScth 	if (vdip)
6280ffc89d77Svikram 		ndi_devi_exit(vdip, v_circ);
62815e3986cbScth 
62827c478bd9Sstevel@tonic-gate 	pm_post_unconfig(pdip, pm_cookie, devnm);
62837c478bd9Sstevel@tonic-gate 
62847c478bd9Sstevel@tonic-gate 	return (rv);
62857c478bd9Sstevel@tonic-gate }
62867c478bd9Sstevel@tonic-gate 
62877c478bd9Sstevel@tonic-gate struct async_arg {
62887c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
62897c478bd9Sstevel@tonic-gate 	uint_t flags;
62907c478bd9Sstevel@tonic-gate };
62917c478bd9Sstevel@tonic-gate 
62927c478bd9Sstevel@tonic-gate /*
62937c478bd9Sstevel@tonic-gate  * Common async handler for:
62947c478bd9Sstevel@tonic-gate  *	ndi_devi_bind_driver_async
62957c478bd9Sstevel@tonic-gate  *	ndi_devi_online_async
62967c478bd9Sstevel@tonic-gate  */
62977c478bd9Sstevel@tonic-gate static int
62987c478bd9Sstevel@tonic-gate i_ndi_devi_async_common(dev_info_t *dip, uint_t flags, void (*func)())
62997c478bd9Sstevel@tonic-gate {
63007c478bd9Sstevel@tonic-gate 	int tqflag;
63017c478bd9Sstevel@tonic-gate 	int kmflag;
63027c478bd9Sstevel@tonic-gate 	struct async_arg *arg;
63037c478bd9Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
63047c478bd9Sstevel@tonic-gate 
63057c478bd9Sstevel@tonic-gate 	ASSERT(pdip);
63067c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(pdip)->devi_taskq);
63077c478bd9Sstevel@tonic-gate 	ASSERT(ndi_dev_is_persistent_node(dip));
63087c478bd9Sstevel@tonic-gate 
63097c478bd9Sstevel@tonic-gate 	if (flags & NDI_NOSLEEP) {
63107c478bd9Sstevel@tonic-gate 		kmflag = KM_NOSLEEP;
63117c478bd9Sstevel@tonic-gate 		tqflag = TQ_NOSLEEP;
63127c478bd9Sstevel@tonic-gate 	} else {
63137c478bd9Sstevel@tonic-gate 		kmflag = KM_SLEEP;
63147c478bd9Sstevel@tonic-gate 		tqflag = TQ_SLEEP;
63157c478bd9Sstevel@tonic-gate 	}
63167c478bd9Sstevel@tonic-gate 
63177c478bd9Sstevel@tonic-gate 	arg = kmem_alloc(sizeof (*arg), kmflag);
63187c478bd9Sstevel@tonic-gate 	if (arg == NULL)
63197c478bd9Sstevel@tonic-gate 		goto fail;
63207c478bd9Sstevel@tonic-gate 
63217c478bd9Sstevel@tonic-gate 	arg->flags = flags;
63227c478bd9Sstevel@tonic-gate 	arg->dip = dip;
63237c478bd9Sstevel@tonic-gate 	if (ddi_taskq_dispatch(DEVI(pdip)->devi_taskq, func, arg, tqflag) ==
63247c478bd9Sstevel@tonic-gate 	    DDI_SUCCESS) {
63257c478bd9Sstevel@tonic-gate 		return (NDI_SUCCESS);
63267c478bd9Sstevel@tonic-gate 	}
63277c478bd9Sstevel@tonic-gate 
63287c478bd9Sstevel@tonic-gate fail:
63297c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "%s%d: ddi_taskq_dispatch failed",
63307c478bd9Sstevel@tonic-gate 	    ddi_driver_name(pdip), ddi_get_instance(pdip)));
63317c478bd9Sstevel@tonic-gate 
63327c478bd9Sstevel@tonic-gate 	if (arg)
63337c478bd9Sstevel@tonic-gate 		kmem_free(arg, sizeof (*arg));
63347c478bd9Sstevel@tonic-gate 	return (NDI_FAILURE);
63357c478bd9Sstevel@tonic-gate }
63367c478bd9Sstevel@tonic-gate 
63377c478bd9Sstevel@tonic-gate static void
63387c478bd9Sstevel@tonic-gate i_ndi_devi_bind_driver_cb(struct async_arg *arg)
63397c478bd9Sstevel@tonic-gate {
63407c478bd9Sstevel@tonic-gate 	(void) ndi_devi_bind_driver(arg->dip, arg->flags);
63417c478bd9Sstevel@tonic-gate 	kmem_free(arg, sizeof (*arg));
63427c478bd9Sstevel@tonic-gate }
63437c478bd9Sstevel@tonic-gate 
63447c478bd9Sstevel@tonic-gate int
63457c478bd9Sstevel@tonic-gate ndi_devi_bind_driver_async(dev_info_t *dip, uint_t flags)
63467c478bd9Sstevel@tonic-gate {
63477c478bd9Sstevel@tonic-gate 	return (i_ndi_devi_async_common(dip, flags,
63487c478bd9Sstevel@tonic-gate 	    (void (*)())i_ndi_devi_bind_driver_cb));
63497c478bd9Sstevel@tonic-gate }
63507c478bd9Sstevel@tonic-gate 
63517c478bd9Sstevel@tonic-gate /*
63527c478bd9Sstevel@tonic-gate  * place the devinfo in the ONLINE state.
63537c478bd9Sstevel@tonic-gate  */
63547c478bd9Sstevel@tonic-gate int
63557c478bd9Sstevel@tonic-gate ndi_devi_online(dev_info_t *dip, uint_t flags)
63567c478bd9Sstevel@tonic-gate {
63577c478bd9Sstevel@tonic-gate 	int circ, rv;
63587c478bd9Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
63597c478bd9Sstevel@tonic-gate 	int branch_event = 0;
63607c478bd9Sstevel@tonic-gate 
63617c478bd9Sstevel@tonic-gate 	ASSERT(pdip);
63627c478bd9Sstevel@tonic-gate 
63637c478bd9Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_online: %s%d (%p)\n",
63647c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
63657c478bd9Sstevel@tonic-gate 
63667c478bd9Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
63677c478bd9Sstevel@tonic-gate 	/* bind child before merging .conf nodes */
63687c478bd9Sstevel@tonic-gate 	rv = i_ndi_config_node(dip, DS_BOUND, flags);
63697c478bd9Sstevel@tonic-gate 	if (rv != NDI_SUCCESS) {
63707c478bd9Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
63717c478bd9Sstevel@tonic-gate 		return (rv);
63727c478bd9Sstevel@tonic-gate 	}
63737c478bd9Sstevel@tonic-gate 
63747c478bd9Sstevel@tonic-gate 	/* merge .conf properties */
63757c478bd9Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(pdip, flags);
63767c478bd9Sstevel@tonic-gate 
6377c73a93f2Sdm120769 	flags |= (NDI_DEVI_ONLINE | NDI_CONFIG);
63787c478bd9Sstevel@tonic-gate 
63797c478bd9Sstevel@tonic-gate 	if (flags & NDI_NO_EVENT) {
63807c478bd9Sstevel@tonic-gate 		/*
63817c478bd9Sstevel@tonic-gate 		 * Caller is specifically asking for not to generate an event.
63827c478bd9Sstevel@tonic-gate 		 * Set the following flag so that devi_attach_node() don't
63837c478bd9Sstevel@tonic-gate 		 * change the event state.
63847c478bd9Sstevel@tonic-gate 		 */
63857c478bd9Sstevel@tonic-gate 		flags |= NDI_NO_EVENT_STATE_CHNG;
63867c478bd9Sstevel@tonic-gate 	}
63877c478bd9Sstevel@tonic-gate 
63887c478bd9Sstevel@tonic-gate 	if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
63897c478bd9Sstevel@tonic-gate 	    ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip))) {
63907c478bd9Sstevel@tonic-gate 		flags |= NDI_BRANCH_EVENT_OP;
63917c478bd9Sstevel@tonic-gate 		branch_event = 1;
63927c478bd9Sstevel@tonic-gate 	}
63937c478bd9Sstevel@tonic-gate 
63947c478bd9Sstevel@tonic-gate 	/*
63957c478bd9Sstevel@tonic-gate 	 * devi_attach_node() may remove dip on failure
63967c478bd9Sstevel@tonic-gate 	 */
63977c478bd9Sstevel@tonic-gate 	if ((rv = devi_attach_node(dip, flags)) == NDI_SUCCESS) {
63987c478bd9Sstevel@tonic-gate 		if ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip)) {
63997c478bd9Sstevel@tonic-gate 			(void) ndi_devi_config(dip, flags);
64007c478bd9Sstevel@tonic-gate 		}
64017c478bd9Sstevel@tonic-gate 
64027c478bd9Sstevel@tonic-gate 		if (branch_event)
64037c478bd9Sstevel@tonic-gate 			(void) i_log_devfs_branch_add(dip);
64047c478bd9Sstevel@tonic-gate 	}
64057c478bd9Sstevel@tonic-gate 
64067c478bd9Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
64077c478bd9Sstevel@tonic-gate 
64087c478bd9Sstevel@tonic-gate 	/*
64097c478bd9Sstevel@tonic-gate 	 * Notify devfs that we have a new node. Devfs needs to invalidate
64107c478bd9Sstevel@tonic-gate 	 * cached directory contents.
64117c478bd9Sstevel@tonic-gate 	 *
64127c478bd9Sstevel@tonic-gate 	 * For PCMCIA devices, it is possible the pdip is not fully
64137c478bd9Sstevel@tonic-gate 	 * attached. In this case, calling back into devfs will
64147c478bd9Sstevel@tonic-gate 	 * result in a loop or assertion error. Hence, the check
64157c478bd9Sstevel@tonic-gate 	 * on node state.
64167c478bd9Sstevel@tonic-gate 	 *
64177c478bd9Sstevel@tonic-gate 	 * If we own parent lock, this is part of a branch operation.
64187c478bd9Sstevel@tonic-gate 	 * We skip the devfs_clean() step because the cache invalidation
64197c478bd9Sstevel@tonic-gate 	 * is done higher up in the device tree.
64207c478bd9Sstevel@tonic-gate 	 */
6421737d277aScth 	if (rv == NDI_SUCCESS && i_ddi_devi_attached(pdip) &&
64227c478bd9Sstevel@tonic-gate 	    !DEVI_BUSY_OWNED(pdip))
64237c478bd9Sstevel@tonic-gate 		(void) devfs_clean(pdip, NULL, 0);
64247c478bd9Sstevel@tonic-gate 	return (rv);
64257c478bd9Sstevel@tonic-gate }
64267c478bd9Sstevel@tonic-gate 
64277c478bd9Sstevel@tonic-gate static void
64287c478bd9Sstevel@tonic-gate i_ndi_devi_online_cb(struct async_arg *arg)
64297c478bd9Sstevel@tonic-gate {
64307c478bd9Sstevel@tonic-gate 	(void) ndi_devi_online(arg->dip, arg->flags);
64317c478bd9Sstevel@tonic-gate 	kmem_free(arg, sizeof (*arg));
64327c478bd9Sstevel@tonic-gate }
64337c478bd9Sstevel@tonic-gate 
64347c478bd9Sstevel@tonic-gate int
64357c478bd9Sstevel@tonic-gate ndi_devi_online_async(dev_info_t *dip, uint_t flags)
64367c478bd9Sstevel@tonic-gate {
64377c478bd9Sstevel@tonic-gate 	/* mark child as need config if requested. */
643816747f41Scth 	if (flags & NDI_CONFIG) {
643916747f41Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
64407c478bd9Sstevel@tonic-gate 		DEVI_SET_NDI_CONFIG(dip);
644116747f41Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
644216747f41Scth 	}
64437c478bd9Sstevel@tonic-gate 
64447c478bd9Sstevel@tonic-gate 	return (i_ndi_devi_async_common(dip, flags,
64457c478bd9Sstevel@tonic-gate 	    (void (*)())i_ndi_devi_online_cb));
64467c478bd9Sstevel@tonic-gate }
64477c478bd9Sstevel@tonic-gate 
64487c478bd9Sstevel@tonic-gate /*
64497c478bd9Sstevel@tonic-gate  * Take a device node Offline
64507c478bd9Sstevel@tonic-gate  * To take a device Offline means to detach the device instance from
64517c478bd9Sstevel@tonic-gate  * the driver and prevent devfs requests from re-attaching the device
64527c478bd9Sstevel@tonic-gate  * instance.
64537c478bd9Sstevel@tonic-gate  *
64547c478bd9Sstevel@tonic-gate  * The flag NDI_DEVI_REMOVE causes removes the device node from
64557c478bd9Sstevel@tonic-gate  * the driver list and the device tree. In this case, the device
64567c478bd9Sstevel@tonic-gate  * is assumed to be removed from the system.
64577c478bd9Sstevel@tonic-gate  */
64587c478bd9Sstevel@tonic-gate int
64597c478bd9Sstevel@tonic-gate ndi_devi_offline(dev_info_t *dip, uint_t flags)
64607c478bd9Sstevel@tonic-gate {
64617c478bd9Sstevel@tonic-gate 	int		circ, rval = 0;
64627c478bd9Sstevel@tonic-gate 	dev_info_t	*pdip = ddi_get_parent(dip);
64635e3986cbScth 	dev_info_t	*vdip = NULL;
64645e3986cbScth 	int		v_circ;
64657c478bd9Sstevel@tonic-gate 	struct brevq_node *brevq = NULL;
64667c478bd9Sstevel@tonic-gate 
64677c478bd9Sstevel@tonic-gate 	ASSERT(pdip);
64687c478bd9Sstevel@tonic-gate 
64697c478bd9Sstevel@tonic-gate 	flags |= NDI_DEVI_OFFLINE;
64705e3986cbScth 
64715e3986cbScth 	/*
64725e3986cbScth 	 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
64735e3986cbScth 	 * before parent(pHCI) to avoid deadlock with mpxio Client power
64745e3986cbScth 	 * management operations.
64755e3986cbScth 	 */
64765e3986cbScth 	if (MDI_PHCI(dip)) {
64775e3986cbScth 		vdip = mdi_devi_get_vdip(dip);
64785e3986cbScth 		if (vdip && (ddi_get_parent(vdip) != pdip))
64795e3986cbScth 			ndi_devi_enter(vdip, &v_circ);
64805e3986cbScth 		else
64815e3986cbScth 			vdip = NULL;
64825e3986cbScth 	}
64837c478bd9Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
64845e3986cbScth 
64854c06356bSdh142964 	if (i_ddi_devi_attached(dip)) {
64867c478bd9Sstevel@tonic-gate 		/*
64877c478bd9Sstevel@tonic-gate 		 * If dip is in DS_READY state, there may be cached dv_nodes
64887c478bd9Sstevel@tonic-gate 		 * referencing this dip, so we invoke devfs code path.
64897c478bd9Sstevel@tonic-gate 		 * Note that we must release busy changing on pdip to
64907c478bd9Sstevel@tonic-gate 		 * avoid deadlock against devfs.
64917c478bd9Sstevel@tonic-gate 		 */
64927c478bd9Sstevel@tonic-gate 		char *devname = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
64937c478bd9Sstevel@tonic-gate 		(void) ddi_deviname(dip, devname);
64945e3986cbScth 
64957c478bd9Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
64965e3986cbScth 		if (vdip)
64975e3986cbScth 			ndi_devi_exit(vdip, v_circ);
64987c478bd9Sstevel@tonic-gate 
64997c478bd9Sstevel@tonic-gate 		/*
65004c06356bSdh142964 		 * If we are explictly told to clean, then clean. If we own the
65014c06356bSdh142964 		 * parent lock then this is part of a branch operation, and we
65024c06356bSdh142964 		 * skip the devfs_clean() step.
65034c06356bSdh142964 		 *
65044c06356bSdh142964 		 * NOTE: A thread performing a devfs file system lookup/
65054c06356bSdh142964 		 * bus_config can't call devfs_clean to unconfig without
65064c06356bSdh142964 		 * causing rwlock problems in devfs. For ndi_devi_offline, this
65074c06356bSdh142964 		 * means that the NDI_DEVFS_CLEAN flag is safe from ioctl code
65084c06356bSdh142964 		 * or from an async hotplug thread, but is not safe from a
65094c06356bSdh142964 		 * nexus driver's bus_config implementation.
65107c478bd9Sstevel@tonic-gate 		 */
65114c06356bSdh142964 		if ((flags & NDI_DEVFS_CLEAN) ||
65124c06356bSdh142964 		    (!DEVI_BUSY_OWNED(pdip)))
651319174f18Svikram 			(void) devfs_clean(pdip, devname + 1, DV_CLEAN_FORCE);
65144c06356bSdh142964 
65157c478bd9Sstevel@tonic-gate 		kmem_free(devname, MAXNAMELEN + 1);
65167c478bd9Sstevel@tonic-gate 
651719174f18Svikram 		rval = devi_unconfig_branch(dip, NULL, flags|NDI_UNCONFIG,
651819174f18Svikram 		    &brevq);
651919174f18Svikram 
65207c478bd9Sstevel@tonic-gate 		if (rval)
65217c478bd9Sstevel@tonic-gate 			return (NDI_FAILURE);
65227c478bd9Sstevel@tonic-gate 
65235e3986cbScth 		if (vdip)
65245e3986cbScth 			ndi_devi_enter(vdip, &v_circ);
65257c478bd9Sstevel@tonic-gate 		ndi_devi_enter(pdip, &circ);
65267c478bd9Sstevel@tonic-gate 	}
65277c478bd9Sstevel@tonic-gate 
65287c478bd9Sstevel@tonic-gate 	init_bound_node_ev(pdip, dip, flags);
65297c478bd9Sstevel@tonic-gate 
65307c478bd9Sstevel@tonic-gate 	rval = devi_detach_node(dip, flags);
65317c478bd9Sstevel@tonic-gate 	if (brevq) {
65327c478bd9Sstevel@tonic-gate 		if (rval != NDI_SUCCESS)
65337c478bd9Sstevel@tonic-gate 			log_and_free_brevq_dip(dip, brevq);
65347c478bd9Sstevel@tonic-gate 		else
65357c478bd9Sstevel@tonic-gate 			free_brevq(brevq);
65367c478bd9Sstevel@tonic-gate 	}
65377c478bd9Sstevel@tonic-gate 
65387c478bd9Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
65395e3986cbScth 	if (vdip)
65405e3986cbScth 		ndi_devi_exit(vdip, v_circ);
65417c478bd9Sstevel@tonic-gate 
65427c478bd9Sstevel@tonic-gate 	return (rval);
65437c478bd9Sstevel@tonic-gate }
65447c478bd9Sstevel@tonic-gate 
65457c478bd9Sstevel@tonic-gate /*
65464c06356bSdh142964  * Find the child dev_info node of parent nexus 'p' whose unit address
65477c478bd9Sstevel@tonic-gate  * matches "cname@caddr".  Recommend use of ndi_devi_findchild() instead.
65487c478bd9Sstevel@tonic-gate  */
65497c478bd9Sstevel@tonic-gate dev_info_t *
65507c478bd9Sstevel@tonic-gate ndi_devi_find(dev_info_t *pdip, char *cname, char *caddr)
65517c478bd9Sstevel@tonic-gate {
65527c478bd9Sstevel@tonic-gate 	dev_info_t *child;
65537c478bd9Sstevel@tonic-gate 	int circ;
65547c478bd9Sstevel@tonic-gate 
65557c478bd9Sstevel@tonic-gate 	if (pdip == NULL || cname == NULL || caddr == NULL)
65567c478bd9Sstevel@tonic-gate 		return ((dev_info_t *)NULL);
65577c478bd9Sstevel@tonic-gate 
65587c478bd9Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
6559f4da9be0Scth 	child = find_sibling(ddi_get_child(pdip), cname, caddr,
6560f4da9be0Scth 	    FIND_NODE_BY_NODENAME, NULL);
65617c478bd9Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
65627c478bd9Sstevel@tonic-gate 	return (child);
65637c478bd9Sstevel@tonic-gate }
65647c478bd9Sstevel@tonic-gate 
65657c478bd9Sstevel@tonic-gate /*
65664c06356bSdh142964  * Find the child dev_info node of parent nexus 'p' whose unit address
65677c478bd9Sstevel@tonic-gate  * matches devname "name@addr".  Permits caller to hold the parent.
65687c478bd9Sstevel@tonic-gate  */
65697c478bd9Sstevel@tonic-gate dev_info_t *
65707c478bd9Sstevel@tonic-gate ndi_devi_findchild(dev_info_t *pdip, char *devname)
65717c478bd9Sstevel@tonic-gate {
65727c478bd9Sstevel@tonic-gate 	dev_info_t *child;
65737c478bd9Sstevel@tonic-gate 	char	*cname, *caddr;
65747c478bd9Sstevel@tonic-gate 	char	*devstr;
65757c478bd9Sstevel@tonic-gate 
65767c478bd9Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(pdip));
65777c478bd9Sstevel@tonic-gate 
65787c478bd9Sstevel@tonic-gate 	devstr = i_ddi_strdup(devname, KM_SLEEP);
65797c478bd9Sstevel@tonic-gate 	i_ddi_parse_name(devstr, &cname, &caddr, NULL);
65807c478bd9Sstevel@tonic-gate 
65817c478bd9Sstevel@tonic-gate 	if (cname == NULL || caddr == NULL) {
65827c478bd9Sstevel@tonic-gate 		kmem_free(devstr, strlen(devname)+1);
65837c478bd9Sstevel@tonic-gate 		return ((dev_info_t *)NULL);
65847c478bd9Sstevel@tonic-gate 	}
65857c478bd9Sstevel@tonic-gate 
6586f4da9be0Scth 	child = find_sibling(ddi_get_child(pdip), cname, caddr,
6587f4da9be0Scth 	    FIND_NODE_BY_NODENAME, NULL);
65887c478bd9Sstevel@tonic-gate 	kmem_free(devstr, strlen(devname)+1);
65897c478bd9Sstevel@tonic-gate 	return (child);
65907c478bd9Sstevel@tonic-gate }
65917c478bd9Sstevel@tonic-gate 
65927c478bd9Sstevel@tonic-gate /*
65937c478bd9Sstevel@tonic-gate  * Misc. routines called by framework only
65947c478bd9Sstevel@tonic-gate  */
65957c478bd9Sstevel@tonic-gate 
65967c478bd9Sstevel@tonic-gate /*
65977c478bd9Sstevel@tonic-gate  * Clear the DEVI_MADE_CHILDREN/DEVI_ATTACHED_CHILDREN flags
65987c478bd9Sstevel@tonic-gate  * if new child spec has been added.
65997c478bd9Sstevel@tonic-gate  */
66007c478bd9Sstevel@tonic-gate static int
66017c478bd9Sstevel@tonic-gate reset_nexus_flags(dev_info_t *dip, void *arg)
66027c478bd9Sstevel@tonic-gate {
66037c478bd9Sstevel@tonic-gate 	struct hwc_spec	*list;
66046a41d557Scth 	int		circ;
66057c478bd9Sstevel@tonic-gate 
66067c478bd9Sstevel@tonic-gate 	if (((DEVI(dip)->devi_flags & DEVI_MADE_CHILDREN) == 0) ||
66077c478bd9Sstevel@tonic-gate 	    ((list = hwc_get_child_spec(dip, (major_t)(uintptr_t)arg)) == NULL))
66087c478bd9Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
66097c478bd9Sstevel@tonic-gate 
66107c478bd9Sstevel@tonic-gate 	hwc_free_spec_list(list);
66116a41d557Scth 
66126a41d557Scth 	/* coordinate child state update */
66136a41d557Scth 	ndi_devi_enter(dip, &circ);
66147c478bd9Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
66157c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN | DEVI_ATTACHED_CHILDREN);
66167c478bd9Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
66176a41d557Scth 	ndi_devi_exit(dip, circ);
66187c478bd9Sstevel@tonic-gate 
66197c478bd9Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
66207c478bd9Sstevel@tonic-gate }
66217c478bd9Sstevel@tonic-gate 
66227c478bd9Sstevel@tonic-gate /*
66237c478bd9Sstevel@tonic-gate  * Helper functions, returns NULL if no memory.
66247c478bd9Sstevel@tonic-gate  */
66257c478bd9Sstevel@tonic-gate 
66267c478bd9Sstevel@tonic-gate /*
66277c478bd9Sstevel@tonic-gate  * path_to_major:
66287c478bd9Sstevel@tonic-gate  *
66297c478bd9Sstevel@tonic-gate  * Return an alternate driver name binding for the leaf device
66307c478bd9Sstevel@tonic-gate  * of the given pathname, if there is one. The purpose of this
66317c478bd9Sstevel@tonic-gate  * function is to deal with generic pathnames. The default action
66327c478bd9Sstevel@tonic-gate  * for platforms that can't do this (ie: x86 or any platform that
66337c478bd9Sstevel@tonic-gate  * does not have prom_finddevice functionality, which matches
66347c478bd9Sstevel@tonic-gate  * nodenames and unit-addresses without the drivers participation)
6635a204de77Scth  * is to return DDI_MAJOR_T_NONE.
66367c478bd9Sstevel@tonic-gate  *
66377c478bd9Sstevel@tonic-gate  * Used in loadrootmodules() in the swapgeneric module to
66387c478bd9Sstevel@tonic-gate  * associate a given pathname with a given leaf driver.
66397c478bd9Sstevel@tonic-gate  *
66407c478bd9Sstevel@tonic-gate  */
66417c478bd9Sstevel@tonic-gate major_t
66427c478bd9Sstevel@tonic-gate path_to_major(char *path)
66437c478bd9Sstevel@tonic-gate {
66447c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
66457c478bd9Sstevel@tonic-gate 	char *p, *q;
6646fa9e4066Sahrens 	pnode_t nodeid;
6647f4da9be0Scth 	major_t major;
6648f4da9be0Scth 
6649f4da9be0Scth 	/* check for path-oriented alias */
6650f4da9be0Scth 	major = ddi_name_to_major(path);
6651c9cc1492SJerry Gilliam 	if (driver_installed(major)) {
6652f4da9be0Scth 		NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s path bound %s\n",
6653f4da9be0Scth 		    path, ddi_major_to_name(major)));
6654f4da9be0Scth 		return (major);
6655f4da9be0Scth 	}
66567c478bd9Sstevel@tonic-gate 
66577c478bd9Sstevel@tonic-gate 	/*
66587c478bd9Sstevel@tonic-gate 	 * Get the nodeid of the given pathname, if such a mapping exists.
66597c478bd9Sstevel@tonic-gate 	 */
66607c478bd9Sstevel@tonic-gate 	dip = NULL;
66617c478bd9Sstevel@tonic-gate 	nodeid = prom_finddevice(path);
66627c478bd9Sstevel@tonic-gate 	if (nodeid != OBP_BADNODE) {
66637c478bd9Sstevel@tonic-gate 		/*
66647c478bd9Sstevel@tonic-gate 		 * Find the nodeid in our copy of the device tree and return
66657c478bd9Sstevel@tonic-gate 		 * whatever name we used to bind this node to a driver.
66667c478bd9Sstevel@tonic-gate 		 */
66677c478bd9Sstevel@tonic-gate 		dip = e_ddi_nodeid_to_dip(nodeid);
66687c478bd9Sstevel@tonic-gate 	}
66697c478bd9Sstevel@tonic-gate 
66707c478bd9Sstevel@tonic-gate 	if (dip == NULL) {
66717c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_WARN,
66727c478bd9Sstevel@tonic-gate 		    "path_to_major: can't bind <%s>\n", path));
6673a204de77Scth 		return (DDI_MAJOR_T_NONE);
66747c478bd9Sstevel@tonic-gate 	}
66757c478bd9Sstevel@tonic-gate 
66767c478bd9Sstevel@tonic-gate 	/*
66777c478bd9Sstevel@tonic-gate 	 * If we're bound to something other than the nodename,
66787c478bd9Sstevel@tonic-gate 	 * note that in the message buffer and system log.
66797c478bd9Sstevel@tonic-gate 	 */
66807c478bd9Sstevel@tonic-gate 	p = ddi_binding_name(dip);
66817c478bd9Sstevel@tonic-gate 	q = ddi_node_name(dip);
66827c478bd9Sstevel@tonic-gate 	if (p && q && (strcmp(p, q) != 0))
66837c478bd9Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s bound to %s\n",
66847c478bd9Sstevel@tonic-gate 		    path, p));
66857c478bd9Sstevel@tonic-gate 
6686f4da9be0Scth 	major = ddi_name_to_major(p);
66877c478bd9Sstevel@tonic-gate 
6688f4da9be0Scth 	ndi_rele_devi(dip);		/* release e_ddi_nodeid_to_dip hold */
66897c478bd9Sstevel@tonic-gate 
6690f4da9be0Scth 	return (major);
66917c478bd9Sstevel@tonic-gate }
66927c478bd9Sstevel@tonic-gate 
66937c478bd9Sstevel@tonic-gate /*
66947c478bd9Sstevel@tonic-gate  * Return the held dip for the specified major and instance, attempting to do
66957c478bd9Sstevel@tonic-gate  * an attach if specified. Return NULL if the devi can't be found or put in
66967c478bd9Sstevel@tonic-gate  * the proper state. The caller must release the hold via ddi_release_devi if
66977c478bd9Sstevel@tonic-gate  * a non-NULL value is returned.
66987c478bd9Sstevel@tonic-gate  *
66997c478bd9Sstevel@tonic-gate  * Some callers expect to be able to perform a hold_devi() while in a context
67007c478bd9Sstevel@tonic-gate  * where using ndi_devi_enter() to ensure the hold might cause deadlock (see
67017c478bd9Sstevel@tonic-gate  * open-from-attach code in consconfig_dacf.c). Such special-case callers
67024c06356bSdh142964  * must ensure that an ndi_devi_enter(parent)/ndi_hold_devi() from a safe
67037c478bd9Sstevel@tonic-gate  * context is already active. The hold_devi() implementation must accommodate
67047c478bd9Sstevel@tonic-gate  * these callers.
67057c478bd9Sstevel@tonic-gate  */
67067c478bd9Sstevel@tonic-gate static dev_info_t *
67077c478bd9Sstevel@tonic-gate hold_devi(major_t major, int instance, int flags)
67087c478bd9Sstevel@tonic-gate {
67097c478bd9Sstevel@tonic-gate 	struct devnames	*dnp;
67107c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
67117c478bd9Sstevel@tonic-gate 	char		*path;
671210b58182SChris Horne 	char		*vpath;
67137c478bd9Sstevel@tonic-gate 
67147c478bd9Sstevel@tonic-gate 	if ((major >= devcnt) || (instance == -1))
67157c478bd9Sstevel@tonic-gate 		return (NULL);
67167c478bd9Sstevel@tonic-gate 
67177c478bd9Sstevel@tonic-gate 	/* try to find the instance in the per driver list */
67187c478bd9Sstevel@tonic-gate 	dnp = &(devnamesp[major]);
67197c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&(dnp->dn_lock));
67207c478bd9Sstevel@tonic-gate 	for (dip = dnp->dn_head; dip;
67217c478bd9Sstevel@tonic-gate 	    dip = (dev_info_t *)DEVI(dip)->devi_next) {
67227c478bd9Sstevel@tonic-gate 		/* skip node if instance field is not valid */
67237c478bd9Sstevel@tonic-gate 		if (i_ddi_node_state(dip) < DS_INITIALIZED)
67247c478bd9Sstevel@tonic-gate 			continue;
67257c478bd9Sstevel@tonic-gate 
67267c478bd9Sstevel@tonic-gate 		/* look for instance match */
67277c478bd9Sstevel@tonic-gate 		if (DEVI(dip)->devi_instance == instance) {
67287c478bd9Sstevel@tonic-gate 			/*
67297c478bd9Sstevel@tonic-gate 			 * To accommodate callers that can't block in
67304c06356bSdh142964 			 * ndi_devi_enter() we do an ndi_hold_devi(), and
67317c478bd9Sstevel@tonic-gate 			 * afterwards check that the node is in a state where
67327c478bd9Sstevel@tonic-gate 			 * the hold prevents detach(). If we did not manage to
67337c478bd9Sstevel@tonic-gate 			 * prevent detach then we ndi_rele_devi() and perform
67347c478bd9Sstevel@tonic-gate 			 * the slow path below (which can result in a blocking
67357c478bd9Sstevel@tonic-gate 			 * ndi_devi_enter() while driving attach top-down).
67367c478bd9Sstevel@tonic-gate 			 * This code depends on the ordering of
67377c478bd9Sstevel@tonic-gate 			 * DEVI_SET_DETACHING and the devi_ref check in the
67387c478bd9Sstevel@tonic-gate 			 * detach_node() code path.
67397c478bd9Sstevel@tonic-gate 			 */
67407c478bd9Sstevel@tonic-gate 			ndi_hold_devi(dip);
6741737d277aScth 			if (i_ddi_devi_attached(dip) &&
67427c478bd9Sstevel@tonic-gate 			    !DEVI_IS_DETACHING(dip)) {
67437c478bd9Sstevel@tonic-gate 				UNLOCK_DEV_OPS(&(dnp->dn_lock));
67447c478bd9Sstevel@tonic-gate 				return (dip);	/* fast-path with devi held */
67457c478bd9Sstevel@tonic-gate 			}
67467c478bd9Sstevel@tonic-gate 			ndi_rele_devi(dip);
67477c478bd9Sstevel@tonic-gate 
67487c478bd9Sstevel@tonic-gate 			/* try slow-path */
67497c478bd9Sstevel@tonic-gate 			dip = NULL;
67507c478bd9Sstevel@tonic-gate 			break;
67517c478bd9Sstevel@tonic-gate 		}
67527c478bd9Sstevel@tonic-gate 	}
67537c478bd9Sstevel@tonic-gate 	ASSERT(dip == NULL);
67547c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
67557c478bd9Sstevel@tonic-gate 
67567c478bd9Sstevel@tonic-gate 	if (flags & E_DDI_HOLD_DEVI_NOATTACH)
67577c478bd9Sstevel@tonic-gate 		return (NULL);		/* told not to drive attach */
67587c478bd9Sstevel@tonic-gate 
67597c478bd9Sstevel@tonic-gate 	/* slow-path may block, so it should not occur from interrupt */
67607c478bd9Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
67617c478bd9Sstevel@tonic-gate 	if (servicing_interrupt())
67627c478bd9Sstevel@tonic-gate 		return (NULL);
67637c478bd9Sstevel@tonic-gate 
67647c478bd9Sstevel@tonic-gate 	/* reconstruct the path and drive attach by path through devfs. */
67657c478bd9Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
676610b58182SChris Horne 	if (e_ddi_majorinstance_to_path(major, instance, path) == 0) {
67677c478bd9Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_path(path, flags);
676810b58182SChris Horne 
676910b58182SChris Horne 		/*
677010b58182SChris Horne 		 * Verify that we got the correct device - a path_to_inst file
677110b58182SChris Horne 		 * with a bogus/corrupt path (or a nexus that changes its
677210b58182SChris Horne 		 * unit-address format) could result in an incorrect answer
677310b58182SChris Horne 		 *
677410b58182SChris Horne 		 * Verify major, instance, and path.
677510b58182SChris Horne 		 */
677610b58182SChris Horne 		vpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
677710b58182SChris Horne 		if (dip &&
677810b58182SChris Horne 		    ((DEVI(dip)->devi_major != major) ||
677910b58182SChris Horne 		    ((DEVI(dip)->devi_instance != instance)) ||
678010b58182SChris Horne 		    (strcmp(path, ddi_pathname(dip, vpath)) != 0))) {
678110b58182SChris Horne 			ndi_rele_devi(dip);
678210b58182SChris Horne 			dip = NULL;	/* no answer better than wrong answer */
678310b58182SChris Horne 		}
678410b58182SChris Horne 		kmem_free(vpath, MAXPATHLEN);
678510b58182SChris Horne 	}
67867c478bd9Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
67877c478bd9Sstevel@tonic-gate 	return (dip);			/* with devi held */
67887c478bd9Sstevel@tonic-gate }
67897c478bd9Sstevel@tonic-gate 
67907c478bd9Sstevel@tonic-gate /*
67917c478bd9Sstevel@tonic-gate  * The {e_}ddi_hold_devi{_by_{instance|dev|path}} hold the devinfo node
67927c478bd9Sstevel@tonic-gate  * associated with the specified arguments.  This hold should be released
67937c478bd9Sstevel@tonic-gate  * by calling ddi_release_devi.
67947c478bd9Sstevel@tonic-gate  *
67957c478bd9Sstevel@tonic-gate  * The E_DDI_HOLD_DEVI_NOATTACH flag argument allows the caller to to specify
67967c478bd9Sstevel@tonic-gate  * a failure return if the node is not already attached.
67977c478bd9Sstevel@tonic-gate  *
67987c478bd9Sstevel@tonic-gate  * NOTE: by the time we make e_ddi_hold_devi public, we should be able to reuse
67997c478bd9Sstevel@tonic-gate  * ddi_hold_devi again.
68007c478bd9Sstevel@tonic-gate  */
68017c478bd9Sstevel@tonic-gate dev_info_t *
68027c478bd9Sstevel@tonic-gate ddi_hold_devi_by_instance(major_t major, int instance, int flags)
68037c478bd9Sstevel@tonic-gate {
68047c478bd9Sstevel@tonic-gate 	return (hold_devi(major, instance, flags));
68057c478bd9Sstevel@tonic-gate }
68067c478bd9Sstevel@tonic-gate 
68077c478bd9Sstevel@tonic-gate dev_info_t *
68087c478bd9Sstevel@tonic-gate e_ddi_hold_devi_by_dev(dev_t dev, int flags)
68097c478bd9Sstevel@tonic-gate {
68107c478bd9Sstevel@tonic-gate 	major_t	major = getmajor(dev);
68117c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
68127c478bd9Sstevel@tonic-gate 	struct dev_ops	*ops;
68137c478bd9Sstevel@tonic-gate 	dev_info_t	*ddip = NULL;
68147c478bd9Sstevel@tonic-gate 
68157c478bd9Sstevel@tonic-gate 	dip = hold_devi(major, dev_to_instance(dev), flags);
68167c478bd9Sstevel@tonic-gate 
68177c478bd9Sstevel@tonic-gate 	/*
68187c478bd9Sstevel@tonic-gate 	 * The rest of this routine is legacy support for drivers that
68197c478bd9Sstevel@tonic-gate 	 * have broken DDI_INFO_DEVT2INSTANCE implementations but may have
68207c478bd9Sstevel@tonic-gate 	 * functional DDI_INFO_DEVT2DEVINFO implementations.  This code will
68217c478bd9Sstevel@tonic-gate 	 * diagnose inconsistency and, for maximum compatibility with legacy
68227c478bd9Sstevel@tonic-gate 	 * drivers, give preference to the drivers DDI_INFO_DEVT2DEVINFO
68237c478bd9Sstevel@tonic-gate 	 * implementation over the above derived dip based the driver's
68247c478bd9Sstevel@tonic-gate 	 * DDI_INFO_DEVT2INSTANCE implementation. This legacy support should
68257c478bd9Sstevel@tonic-gate 	 * be removed when DDI_INFO_DEVT2DEVINFO is deprecated.
68267c478bd9Sstevel@tonic-gate 	 *
68277c478bd9Sstevel@tonic-gate 	 * NOTE: The following code has a race condition. DEVT2DEVINFO
68287c478bd9Sstevel@tonic-gate 	 *	returns a dip which is not held. By the time we ref ddip,
68297c478bd9Sstevel@tonic-gate 	 *	it could have been freed. The saving grace is that for
68307c478bd9Sstevel@tonic-gate 	 *	most drivers, the dip returned from hold_devi() is the
68317c478bd9Sstevel@tonic-gate 	 *	same one as the one returned by DEVT2DEVINFO, so we are
68327c478bd9Sstevel@tonic-gate 	 *	safe for drivers with the correct getinfo(9e) impl.
68337c478bd9Sstevel@tonic-gate 	 */
68347c478bd9Sstevel@tonic-gate 	if (((ops = ddi_hold_driver(major)) != NULL) &&
68357c478bd9Sstevel@tonic-gate 	    CB_DRV_INSTALLED(ops) && ops->devo_getinfo)  {
68367c478bd9Sstevel@tonic-gate 		if ((*ops->devo_getinfo)(NULL, DDI_INFO_DEVT2DEVINFO,
68377c478bd9Sstevel@tonic-gate 		    (void *)dev, (void **)&ddip) != DDI_SUCCESS)
68387c478bd9Sstevel@tonic-gate 			ddip = NULL;
68397c478bd9Sstevel@tonic-gate 	}
68407c478bd9Sstevel@tonic-gate 
68417c478bd9Sstevel@tonic-gate 	/* give preference to the driver returned DEVT2DEVINFO dip */
68427c478bd9Sstevel@tonic-gate 	if (ddip && (dip != ddip)) {
68437c478bd9Sstevel@tonic-gate #ifdef	DEBUG
68447c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: inconsistent getinfo(9E) implementation",
68457c478bd9Sstevel@tonic-gate 		    ddi_driver_name(ddip));
68467c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
68477c478bd9Sstevel@tonic-gate 		ndi_hold_devi(ddip);
68487c478bd9Sstevel@tonic-gate 		if (dip)
68497c478bd9Sstevel@tonic-gate 			ndi_rele_devi(dip);
68507c478bd9Sstevel@tonic-gate 		dip = ddip;
68517c478bd9Sstevel@tonic-gate 	}
68527c478bd9Sstevel@tonic-gate 
68537c478bd9Sstevel@tonic-gate 	if (ops)
68547c478bd9Sstevel@tonic-gate 		ddi_rele_driver(major);
68557c478bd9Sstevel@tonic-gate 
68567c478bd9Sstevel@tonic-gate 	return (dip);
68577c478bd9Sstevel@tonic-gate }
68587c478bd9Sstevel@tonic-gate 
68597c478bd9Sstevel@tonic-gate /*
68607c478bd9Sstevel@tonic-gate  * For compatibility only. Do not call this function!
68617c478bd9Sstevel@tonic-gate  */
68627c478bd9Sstevel@tonic-gate dev_info_t *
68637c478bd9Sstevel@tonic-gate e_ddi_get_dev_info(dev_t dev, vtype_t type)
68647c478bd9Sstevel@tonic-gate {
68657c478bd9Sstevel@tonic-gate 	dev_info_t *dip = NULL;
68667c478bd9Sstevel@tonic-gate 	if (getmajor(dev) >= devcnt)
68677c478bd9Sstevel@tonic-gate 		return (NULL);
68687c478bd9Sstevel@tonic-gate 
68697c478bd9Sstevel@tonic-gate 	switch (type) {
68707c478bd9Sstevel@tonic-gate 	case VCHR:
68717c478bd9Sstevel@tonic-gate 	case VBLK:
68727c478bd9Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
68737c478bd9Sstevel@tonic-gate 	default:
68747c478bd9Sstevel@tonic-gate 		break;
68757c478bd9Sstevel@tonic-gate 	}
68767c478bd9Sstevel@tonic-gate 
68777c478bd9Sstevel@tonic-gate 	/*
68787c478bd9Sstevel@tonic-gate 	 * For compatibility reasons, we can only return the dip with
68797c478bd9Sstevel@tonic-gate 	 * the driver ref count held. This is not a safe thing to do.
68807c478bd9Sstevel@tonic-gate 	 * For certain broken third-party software, we are willing
68817c478bd9Sstevel@tonic-gate 	 * to venture into unknown territory.
68827c478bd9Sstevel@tonic-gate 	 */
68837c478bd9Sstevel@tonic-gate 	if (dip) {
68847c478bd9Sstevel@tonic-gate 		(void) ndi_hold_driver(dip);
68857c478bd9Sstevel@tonic-gate 		ndi_rele_devi(dip);
68867c478bd9Sstevel@tonic-gate 	}
68877c478bd9Sstevel@tonic-gate 	return (dip);
68887c478bd9Sstevel@tonic-gate }
68897c478bd9Sstevel@tonic-gate 
68907c478bd9Sstevel@tonic-gate dev_info_t *
68917c478bd9Sstevel@tonic-gate e_ddi_hold_devi_by_path(char *path, int flags)
68927c478bd9Sstevel@tonic-gate {
68937c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
68947c478bd9Sstevel@tonic-gate 
68957c478bd9Sstevel@tonic-gate 	/* can't specify NOATTACH by path */
68967c478bd9Sstevel@tonic-gate 	ASSERT(!(flags & E_DDI_HOLD_DEVI_NOATTACH));
68977c478bd9Sstevel@tonic-gate 
68987c478bd9Sstevel@tonic-gate 	return (resolve_pathname(path, &dip, NULL, NULL) ? NULL : dip);
68997c478bd9Sstevel@tonic-gate }
69007c478bd9Sstevel@tonic-gate 
69017c478bd9Sstevel@tonic-gate void
69027c478bd9Sstevel@tonic-gate e_ddi_hold_devi(dev_info_t *dip)
69037c478bd9Sstevel@tonic-gate {
69047c478bd9Sstevel@tonic-gate 	ndi_hold_devi(dip);
69057c478bd9Sstevel@tonic-gate }
69067c478bd9Sstevel@tonic-gate 
69077c478bd9Sstevel@tonic-gate void
69087c478bd9Sstevel@tonic-gate ddi_release_devi(dev_info_t *dip)
69097c478bd9Sstevel@tonic-gate {
69107c478bd9Sstevel@tonic-gate 	ndi_rele_devi(dip);
69117c478bd9Sstevel@tonic-gate }
69127c478bd9Sstevel@tonic-gate 
69137c478bd9Sstevel@tonic-gate /*
69147c478bd9Sstevel@tonic-gate  * Associate a streams queue with a devinfo node
69157c478bd9Sstevel@tonic-gate  * NOTE: This function is called by STREAM driver's put procedure.
69167c478bd9Sstevel@tonic-gate  *	It cannot block.
69177c478bd9Sstevel@tonic-gate  */
69187c478bd9Sstevel@tonic-gate void
69197c478bd9Sstevel@tonic-gate ddi_assoc_queue_with_devi(queue_t *q, dev_info_t *dip)
69207c478bd9Sstevel@tonic-gate {
69217c478bd9Sstevel@tonic-gate 	queue_t *rq = _RD(q);
69227c478bd9Sstevel@tonic-gate 	struct stdata *stp;
69237c478bd9Sstevel@tonic-gate 	vnode_t *vp;
69247c478bd9Sstevel@tonic-gate 
69257c478bd9Sstevel@tonic-gate 	/* set flag indicating that ddi_assoc_queue_with_devi was called */
69267c478bd9Sstevel@tonic-gate 	mutex_enter(QLOCK(rq));
69277c478bd9Sstevel@tonic-gate 	rq->q_flag |= _QASSOCIATED;
69287c478bd9Sstevel@tonic-gate 	mutex_exit(QLOCK(rq));
69297c478bd9Sstevel@tonic-gate 
69307c478bd9Sstevel@tonic-gate 	/* get the vnode associated with the queue */
69317c478bd9Sstevel@tonic-gate 	stp = STREAM(rq);
69327c478bd9Sstevel@tonic-gate 	vp = stp->sd_vnode;
69337c478bd9Sstevel@tonic-gate 	ASSERT(vp);
69347c478bd9Sstevel@tonic-gate 
69357c478bd9Sstevel@tonic-gate 	/* change the hardware association of the vnode */
69367c478bd9Sstevel@tonic-gate 	spec_assoc_vp_with_devi(vp, dip);
69377c478bd9Sstevel@tonic-gate }
69387c478bd9Sstevel@tonic-gate 
69397c478bd9Sstevel@tonic-gate /*
69407c478bd9Sstevel@tonic-gate  * ddi_install_driver(name)
69417c478bd9Sstevel@tonic-gate  *
69427c478bd9Sstevel@tonic-gate  * Driver installation is currently a byproduct of driver loading.  This
69437c478bd9Sstevel@tonic-gate  * may change.
69447c478bd9Sstevel@tonic-gate  */
69457c478bd9Sstevel@tonic-gate int
69467c478bd9Sstevel@tonic-gate ddi_install_driver(char *name)
69477c478bd9Sstevel@tonic-gate {
69487c478bd9Sstevel@tonic-gate 	major_t major = ddi_name_to_major(name);
69497c478bd9Sstevel@tonic-gate 
6950a204de77Scth 	if ((major == DDI_MAJOR_T_NONE) ||
69517c478bd9Sstevel@tonic-gate 	    (ddi_hold_installed_driver(major) == NULL)) {
69527c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
69537c478bd9Sstevel@tonic-gate 	}
69547c478bd9Sstevel@tonic-gate 	ddi_rele_driver(major);
69557c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
69567c478bd9Sstevel@tonic-gate }
69577c478bd9Sstevel@tonic-gate 
69587c478bd9Sstevel@tonic-gate struct dev_ops *
69597c478bd9Sstevel@tonic-gate ddi_hold_driver(major_t major)
69607c478bd9Sstevel@tonic-gate {
69617c478bd9Sstevel@tonic-gate 	return (mod_hold_dev_by_major(major));
69627c478bd9Sstevel@tonic-gate }
69637c478bd9Sstevel@tonic-gate 
69647c478bd9Sstevel@tonic-gate 
69657c478bd9Sstevel@tonic-gate void
69667c478bd9Sstevel@tonic-gate ddi_rele_driver(major_t major)
69677c478bd9Sstevel@tonic-gate {
69687c478bd9Sstevel@tonic-gate 	mod_rele_dev_by_major(major);
69697c478bd9Sstevel@tonic-gate }
69707c478bd9Sstevel@tonic-gate 
69717c478bd9Sstevel@tonic-gate 
69727c478bd9Sstevel@tonic-gate /*
69737c478bd9Sstevel@tonic-gate  * This is called during boot to force attachment order of special dips
69747c478bd9Sstevel@tonic-gate  * dip must be referenced via ndi_hold_devi()
69757c478bd9Sstevel@tonic-gate  */
69767c478bd9Sstevel@tonic-gate int
69777c478bd9Sstevel@tonic-gate i_ddi_attach_node_hierarchy(dev_info_t *dip)
69787c478bd9Sstevel@tonic-gate {
69797c478bd9Sstevel@tonic-gate 	dev_info_t	*parent;
69805e3986cbScth 	int		ret, circ;
6981c73a93f2Sdm120769 
6982c73a93f2Sdm120769 	/*
69835e3986cbScth 	 * Recurse up until attached parent is found.
6984c73a93f2Sdm120769 	 */
69855e3986cbScth 	if (i_ddi_devi_attached(dip))
69865e3986cbScth 		return (DDI_SUCCESS);
69877c478bd9Sstevel@tonic-gate 	parent = ddi_get_parent(dip);
69887c478bd9Sstevel@tonic-gate 	if (i_ddi_attach_node_hierarchy(parent) != DDI_SUCCESS)
69897c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
69907c478bd9Sstevel@tonic-gate 
69917c478bd9Sstevel@tonic-gate 	/*
69925e3986cbScth 	 * Come top-down, expanding .conf nodes under this parent
69935e3986cbScth 	 * and driving attach.
69947c478bd9Sstevel@tonic-gate 	 */
69955e3986cbScth 	ndi_devi_enter(parent, &circ);
69967c478bd9Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(parent, 0);
69975e3986cbScth 	ret = i_ddi_attachchild(dip);
69985e3986cbScth 	ndi_devi_exit(parent, circ);
69995e3986cbScth 
70005e3986cbScth 	return (ret);
70017c478bd9Sstevel@tonic-gate }
70027c478bd9Sstevel@tonic-gate 
70037c478bd9Sstevel@tonic-gate /* keep this function static */
70047c478bd9Sstevel@tonic-gate static int
70057c478bd9Sstevel@tonic-gate attach_driver_nodes(major_t major)
70067c478bd9Sstevel@tonic-gate {
70077c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
70087c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
70097c478bd9Sstevel@tonic-gate 	int error = DDI_FAILURE;
7010fa0d09b9Sjiang wu - Sun Microsystems - Beijing China 	int circ;
70117c478bd9Sstevel@tonic-gate 
70127c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
70137c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
70147c478bd9Sstevel@tonic-gate 	dip = dnp->dn_head;
70157c478bd9Sstevel@tonic-gate 	while (dip) {
70167c478bd9Sstevel@tonic-gate 		ndi_hold_devi(dip);
70177c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
70187c478bd9Sstevel@tonic-gate 		if (i_ddi_attach_node_hierarchy(dip) == DDI_SUCCESS)
70197c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
7020fa0d09b9Sjiang wu - Sun Microsystems - Beijing China 		/*
7021fa0d09b9Sjiang wu - Sun Microsystems - Beijing China 		 * Set the 'ddi-config-driver-node' property on a nexus
7022fa0d09b9Sjiang wu - Sun Microsystems - Beijing China 		 * node to cause attach_driver_nodes() to configure all
7023fa0d09b9Sjiang wu - Sun Microsystems - Beijing China 		 * immediate children of the nexus. This property should
7024fa0d09b9Sjiang wu - Sun Microsystems - Beijing China 		 * be set on nodes with immediate children that bind to
7025fa0d09b9Sjiang wu - Sun Microsystems - Beijing China 		 * the same driver as parent.
7026fa0d09b9Sjiang wu - Sun Microsystems - Beijing China 		 */
7027fa0d09b9Sjiang wu - Sun Microsystems - Beijing China 		if ((error == DDI_SUCCESS) && (ddi_prop_exists(DDI_DEV_T_ANY,
7028fa0d09b9Sjiang wu - Sun Microsystems - Beijing China 		    dip, DDI_PROP_DONTPASS, "ddi-config-driver-node"))) {
7029fa0d09b9Sjiang wu - Sun Microsystems - Beijing China 			ndi_devi_enter(dip, &circ);
7030fa0d09b9Sjiang wu - Sun Microsystems - Beijing China 			(void) ndi_devi_config(dip, NDI_NO_EVENT);
7031fa0d09b9Sjiang wu - Sun Microsystems - Beijing China 			ndi_devi_exit(dip, circ);
7032fa0d09b9Sjiang wu - Sun Microsystems - Beijing China 		}
70337c478bd9Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
70347c478bd9Sstevel@tonic-gate 		ndi_rele_devi(dip);
70357c478bd9Sstevel@tonic-gate 		dip = ddi_get_next(dip);
70367c478bd9Sstevel@tonic-gate 	}
70377c478bd9Sstevel@tonic-gate 	if (error == DDI_SUCCESS)
70387c478bd9Sstevel@tonic-gate 		dnp->dn_flags |= DN_NO_AUTODETACH;
70397c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
70407c478bd9Sstevel@tonic-gate 
70417c478bd9Sstevel@tonic-gate 
70427c478bd9Sstevel@tonic-gate 	return (error);
70437c478bd9Sstevel@tonic-gate }
70447c478bd9Sstevel@tonic-gate 
70457c478bd9Sstevel@tonic-gate /*
70467c478bd9Sstevel@tonic-gate  * i_ddi_attach_hw_nodes configures and attaches all hw nodes
70477c478bd9Sstevel@tonic-gate  * bound to a specific driver. This function replaces calls to
70487c478bd9Sstevel@tonic-gate  * ddi_hold_installed_driver() for drivers with no .conf
70497c478bd9Sstevel@tonic-gate  * enumerated nodes.
70507c478bd9Sstevel@tonic-gate  *
70517c478bd9Sstevel@tonic-gate  * This facility is typically called at boot time to attach
70527c478bd9Sstevel@tonic-gate  * platform-specific hardware nodes, such as ppm nodes on xcal
70537c478bd9Sstevel@tonic-gate  * and grover and keyswitch nodes on cherrystone. It does not
70547c478bd9Sstevel@tonic-gate  * deal with .conf enumerated node. Calling it beyond the boot
70557c478bd9Sstevel@tonic-gate  * process is strongly discouraged.
70567c478bd9Sstevel@tonic-gate  */
70577c478bd9Sstevel@tonic-gate int
70587c478bd9Sstevel@tonic-gate i_ddi_attach_hw_nodes(char *driver)
70597c478bd9Sstevel@tonic-gate {
70607c478bd9Sstevel@tonic-gate 	major_t major;
70617c478bd9Sstevel@tonic-gate 
70627c478bd9Sstevel@tonic-gate 	major = ddi_name_to_major(driver);
7063a204de77Scth 	if (major == DDI_MAJOR_T_NONE)
70647c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
70657c478bd9Sstevel@tonic-gate 
70667c478bd9Sstevel@tonic-gate 	return (attach_driver_nodes(major));
70677c478bd9Sstevel@tonic-gate }
70687c478bd9Sstevel@tonic-gate 
70697c478bd9Sstevel@tonic-gate /*
70707c478bd9Sstevel@tonic-gate  * i_ddi_attach_pseudo_node configures pseudo drivers which
70717c478bd9Sstevel@tonic-gate  * has a single node. The .conf nodes must be enumerated
70727c478bd9Sstevel@tonic-gate  * before calling this interface. The dip is held attached
70737c478bd9Sstevel@tonic-gate  * upon returning.
70747c478bd9Sstevel@tonic-gate  *
70757c478bd9Sstevel@tonic-gate  * This facility should only be called only at boot time
70767c478bd9Sstevel@tonic-gate  * by the I/O framework.
70777c478bd9Sstevel@tonic-gate  */
70787c478bd9Sstevel@tonic-gate dev_info_t *
70797c478bd9Sstevel@tonic-gate i_ddi_attach_pseudo_node(char *driver)
70807c478bd9Sstevel@tonic-gate {
70817c478bd9Sstevel@tonic-gate 	major_t major;
70827c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
70837c478bd9Sstevel@tonic-gate 
70847c478bd9Sstevel@tonic-gate 	major = ddi_name_to_major(driver);
7085a204de77Scth 	if (major == DDI_MAJOR_T_NONE)
70867c478bd9Sstevel@tonic-gate 		return (NULL);
70877c478bd9Sstevel@tonic-gate 
70887c478bd9Sstevel@tonic-gate 	if (attach_driver_nodes(major) != DDI_SUCCESS)
70897c478bd9Sstevel@tonic-gate 		return (NULL);
70907c478bd9Sstevel@tonic-gate 
70917c478bd9Sstevel@tonic-gate 	dip = devnamesp[major].dn_head;
70927c478bd9Sstevel@tonic-gate 	ASSERT(dip && ddi_get_next(dip) == NULL);
70937c478bd9Sstevel@tonic-gate 	ndi_hold_devi(dip);
70947c478bd9Sstevel@tonic-gate 	return (dip);
70957c478bd9Sstevel@tonic-gate }
70967c478bd9Sstevel@tonic-gate 
70977c478bd9Sstevel@tonic-gate static void
70987c478bd9Sstevel@tonic-gate diplist_to_parent_major(dev_info_t *head, char parents[])
70997c478bd9Sstevel@tonic-gate {
71007c478bd9Sstevel@tonic-gate 	major_t major;
71017c478bd9Sstevel@tonic-gate 	dev_info_t *dip, *pdip;
71027c478bd9Sstevel@tonic-gate 
71037c478bd9Sstevel@tonic-gate 	for (dip = head; dip != NULL; dip = ddi_get_next(dip)) {
71047c478bd9Sstevel@tonic-gate 		pdip = ddi_get_parent(dip);
71057c478bd9Sstevel@tonic-gate 		ASSERT(pdip);	/* disallow rootnex.conf nodes */
71067c478bd9Sstevel@tonic-gate 		major = ddi_driver_major(pdip);
7107a204de77Scth 		if ((major != DDI_MAJOR_T_NONE) && parents[major] == 0)
71087c478bd9Sstevel@tonic-gate 			parents[major] = 1;
71097c478bd9Sstevel@tonic-gate 	}
71107c478bd9Sstevel@tonic-gate }
71117c478bd9Sstevel@tonic-gate 
71127c478bd9Sstevel@tonic-gate /*
71137c478bd9Sstevel@tonic-gate  * Call ddi_hold_installed_driver() on each parent major
71147c478bd9Sstevel@tonic-gate  * and invoke mt_config_driver() to attach child major.
71157c478bd9Sstevel@tonic-gate  * This is part of the implementation of ddi_hold_installed_driver.
71167c478bd9Sstevel@tonic-gate  */
71177c478bd9Sstevel@tonic-gate static int
71187c478bd9Sstevel@tonic-gate attach_driver_by_parent(major_t child_major, char parents[])
71197c478bd9Sstevel@tonic-gate {
71207c478bd9Sstevel@tonic-gate 	major_t par_major;
71217c478bd9Sstevel@tonic-gate 	struct mt_config_handle *hdl;
71227c478bd9Sstevel@tonic-gate 	int flags = NDI_DEVI_PERSIST | NDI_NO_EVENT;
71237c478bd9Sstevel@tonic-gate 
71247c478bd9Sstevel@tonic-gate 	hdl = mt_config_init(NULL, NULL, flags, child_major, MT_CONFIG_OP,
71257c478bd9Sstevel@tonic-gate 	    NULL);
71267c478bd9Sstevel@tonic-gate 	for (par_major = 0; par_major < devcnt; par_major++) {
71277c478bd9Sstevel@tonic-gate 		/* disallow recursion on the same driver */
71287c478bd9Sstevel@tonic-gate 		if (parents[par_major] == 0 || par_major == child_major)
71297c478bd9Sstevel@tonic-gate 			continue;
71307c478bd9Sstevel@tonic-gate 		if (ddi_hold_installed_driver(par_major) == NULL)
71317c478bd9Sstevel@tonic-gate 			continue;
71327c478bd9Sstevel@tonic-gate 		hdl->mtc_parmajor = par_major;
71337c478bd9Sstevel@tonic-gate 		mt_config_driver(hdl);
71347c478bd9Sstevel@tonic-gate 		ddi_rele_driver(par_major);
71357c478bd9Sstevel@tonic-gate 	}
71367c478bd9Sstevel@tonic-gate 	(void) mt_config_fini(hdl);
71377c478bd9Sstevel@tonic-gate 
71387c478bd9Sstevel@tonic-gate 	return (i_ddi_devs_attached(child_major));
71397c478bd9Sstevel@tonic-gate }
71407c478bd9Sstevel@tonic-gate 
71417c478bd9Sstevel@tonic-gate int
71427c478bd9Sstevel@tonic-gate i_ddi_devs_attached(major_t major)
71437c478bd9Sstevel@tonic-gate {
71447c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
71457c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
71467c478bd9Sstevel@tonic-gate 	int error = DDI_FAILURE;
71477c478bd9Sstevel@tonic-gate 
71487c478bd9Sstevel@tonic-gate 	/* check for attached instances */
71497c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
71507c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
71517c478bd9Sstevel@tonic-gate 	for (dip = dnp->dn_head; dip != NULL; dip = ddi_get_next(dip)) {
7152737d277aScth 		if (i_ddi_devi_attached(dip)) {
71537c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
71547c478bd9Sstevel@tonic-gate 			break;
71557c478bd9Sstevel@tonic-gate 		}
71567c478bd9Sstevel@tonic-gate 	}
71577c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
71587c478bd9Sstevel@tonic-gate 
71597c478bd9Sstevel@tonic-gate 	return (error);
71607c478bd9Sstevel@tonic-gate }
71617c478bd9Sstevel@tonic-gate 
7162d62bc4baSyz147064 int
7163d62bc4baSyz147064 i_ddi_minor_node_count(dev_info_t *ddip, const char *node_type)
7164d62bc4baSyz147064 {
7165b9ccdc5aScth 	int			circ;
7166d62bc4baSyz147064 	struct ddi_minor_data	*dp;
7167d62bc4baSyz147064 	int			count = 0;
7168d62bc4baSyz147064 
7169b9ccdc5aScth 	ndi_devi_enter(ddip, &circ);
7170b9ccdc5aScth 	for (dp = DEVI(ddip)->devi_minor; dp != NULL; dp = dp->next) {
7171d62bc4baSyz147064 		if (strcmp(dp->ddm_node_type, node_type) == 0)
7172d62bc4baSyz147064 			count++;
7173b9ccdc5aScth 	}
7174b9ccdc5aScth 	ndi_devi_exit(ddip, circ);
7175d62bc4baSyz147064 	return (count);
7176d62bc4baSyz147064 }
7177d62bc4baSyz147064 
71787c478bd9Sstevel@tonic-gate /*
71797c478bd9Sstevel@tonic-gate  * ddi_hold_installed_driver configures and attaches all
71807c478bd9Sstevel@tonic-gate  * instances of the specified driver. To accomplish this
71817c478bd9Sstevel@tonic-gate  * it configures and attaches all possible parents of
71827c478bd9Sstevel@tonic-gate  * the driver, enumerated both in h/w nodes and in the
71837c478bd9Sstevel@tonic-gate  * driver's .conf file.
71847c478bd9Sstevel@tonic-gate  *
71857c478bd9Sstevel@tonic-gate  * NOTE: This facility is for compatibility purposes only and will
71867c478bd9Sstevel@tonic-gate  *	eventually go away. Its usage is strongly discouraged.
71877c478bd9Sstevel@tonic-gate  */
71887c478bd9Sstevel@tonic-gate static void
71897c478bd9Sstevel@tonic-gate enter_driver(struct devnames *dnp)
71907c478bd9Sstevel@tonic-gate {
71917c478bd9Sstevel@tonic-gate 	mutex_enter(&dnp->dn_lock);
71927c478bd9Sstevel@tonic-gate 	ASSERT(dnp->dn_busy_thread != curthread);
71937c478bd9Sstevel@tonic-gate 	while (dnp->dn_flags & DN_DRIVER_BUSY)
71947c478bd9Sstevel@tonic-gate 		cv_wait(&dnp->dn_wait, &dnp->dn_lock);
71957c478bd9Sstevel@tonic-gate 	dnp->dn_flags |= DN_DRIVER_BUSY;
71967c478bd9Sstevel@tonic-gate 	dnp->dn_busy_thread = curthread;
71977c478bd9Sstevel@tonic-gate 	mutex_exit(&dnp->dn_lock);
71987c478bd9Sstevel@tonic-gate }
71997c478bd9Sstevel@tonic-gate 
72007c478bd9Sstevel@tonic-gate static void
72017c478bd9Sstevel@tonic-gate exit_driver(struct devnames *dnp)
72027c478bd9Sstevel@tonic-gate {
72037c478bd9Sstevel@tonic-gate 	mutex_enter(&dnp->dn_lock);
72047c478bd9Sstevel@tonic-gate 	ASSERT(dnp->dn_busy_thread == curthread);
72057c478bd9Sstevel@tonic-gate 	dnp->dn_flags &= ~DN_DRIVER_BUSY;
72067c478bd9Sstevel@tonic-gate 	dnp->dn_busy_thread = NULL;
72077c478bd9Sstevel@tonic-gate 	cv_broadcast(&dnp->dn_wait);
72087c478bd9Sstevel@tonic-gate 	mutex_exit(&dnp->dn_lock);
72097c478bd9Sstevel@tonic-gate }
72107c478bd9Sstevel@tonic-gate 
72117c478bd9Sstevel@tonic-gate struct dev_ops *
72127c478bd9Sstevel@tonic-gate ddi_hold_installed_driver(major_t major)
72137c478bd9Sstevel@tonic-gate {
72147c478bd9Sstevel@tonic-gate 	struct dev_ops *ops;
72157c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
72167c478bd9Sstevel@tonic-gate 	char *parents;
72177c478bd9Sstevel@tonic-gate 	int error;
72187c478bd9Sstevel@tonic-gate 
72197c478bd9Sstevel@tonic-gate 	ops = ddi_hold_driver(major);
72207c478bd9Sstevel@tonic-gate 	if (ops == NULL)
72217c478bd9Sstevel@tonic-gate 		return (NULL);
72227c478bd9Sstevel@tonic-gate 
72237c478bd9Sstevel@tonic-gate 	/*
72247c478bd9Sstevel@tonic-gate 	 * Return immediately if all the attach operations associated
72257c478bd9Sstevel@tonic-gate 	 * with a ddi_hold_installed_driver() call have already been done.
72267c478bd9Sstevel@tonic-gate 	 */
72277c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
72287c478bd9Sstevel@tonic-gate 	enter_driver(dnp);
7229c9cc1492SJerry Gilliam 	ASSERT(driver_installed(major));
7230c9cc1492SJerry Gilliam 
72317c478bd9Sstevel@tonic-gate 	if (dnp->dn_flags & DN_DRIVER_HELD) {
72327c478bd9Sstevel@tonic-gate 		exit_driver(dnp);
72337c478bd9Sstevel@tonic-gate 		if (i_ddi_devs_attached(major) == DDI_SUCCESS)
72347c478bd9Sstevel@tonic-gate 			return (ops);
72357c478bd9Sstevel@tonic-gate 		ddi_rele_driver(major);
72367c478bd9Sstevel@tonic-gate 		return (NULL);
72377c478bd9Sstevel@tonic-gate 	}
72387c478bd9Sstevel@tonic-gate 
72397c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
72407c478bd9Sstevel@tonic-gate 	dnp->dn_flags |= (DN_DRIVER_HELD | DN_NO_AUTODETACH);
72417c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
72427c478bd9Sstevel@tonic-gate 
72437c478bd9Sstevel@tonic-gate 	DCOMPATPRINTF((CE_CONT,
72447c478bd9Sstevel@tonic-gate 	    "ddi_hold_installed_driver: %s\n", dnp->dn_name));
72457c478bd9Sstevel@tonic-gate 
72467c478bd9Sstevel@tonic-gate 	/*
72477c478bd9Sstevel@tonic-gate 	 * When the driver has no .conf children, it is sufficient
72487c478bd9Sstevel@tonic-gate 	 * to attach existing nodes in the device tree. Nodes not
72497c478bd9Sstevel@tonic-gate 	 * enumerated by the OBP are not attached.
72507c478bd9Sstevel@tonic-gate 	 */
72517c478bd9Sstevel@tonic-gate 	if (dnp->dn_pl == NULL) {
72527c478bd9Sstevel@tonic-gate 		if (attach_driver_nodes(major) == DDI_SUCCESS) {
72537c478bd9Sstevel@tonic-gate 			exit_driver(dnp);
72547c478bd9Sstevel@tonic-gate 			return (ops);
72557c478bd9Sstevel@tonic-gate 		}
72567c478bd9Sstevel@tonic-gate 		exit_driver(dnp);
72577c478bd9Sstevel@tonic-gate 		ddi_rele_driver(major);
72587c478bd9Sstevel@tonic-gate 		return (NULL);
72597c478bd9Sstevel@tonic-gate 	}
72607c478bd9Sstevel@tonic-gate 
72617c478bd9Sstevel@tonic-gate 	/*
72627c478bd9Sstevel@tonic-gate 	 * Driver has .conf nodes. We find all possible parents
72637c478bd9Sstevel@tonic-gate 	 * and recursively all ddi_hold_installed_driver on the
72647c478bd9Sstevel@tonic-gate 	 * parent driver; then we invoke ndi_config_driver()
72657c478bd9Sstevel@tonic-gate 	 * on all possible parent node in parallel to speed up
72667c478bd9Sstevel@tonic-gate 	 * performance.
72677c478bd9Sstevel@tonic-gate 	 */
72687c478bd9Sstevel@tonic-gate 	parents = kmem_zalloc(devcnt * sizeof (char), KM_SLEEP);
72697c478bd9Sstevel@tonic-gate 
72707c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
72717c478bd9Sstevel@tonic-gate 	/* find .conf parents */
72727c478bd9Sstevel@tonic-gate 	(void) impl_parlist_to_major(dnp->dn_pl, parents);
72737c478bd9Sstevel@tonic-gate 	/* find hw node parents */
72747c478bd9Sstevel@tonic-gate 	diplist_to_parent_major(dnp->dn_head, parents);
72757c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
72767c478bd9Sstevel@tonic-gate 
72777c478bd9Sstevel@tonic-gate 	error = attach_driver_by_parent(major, parents);
72787c478bd9Sstevel@tonic-gate 	kmem_free(parents, devcnt * sizeof (char));
72797c478bd9Sstevel@tonic-gate 	if (error == DDI_SUCCESS) {
72807c478bd9Sstevel@tonic-gate 		exit_driver(dnp);
72817c478bd9Sstevel@tonic-gate 		return (ops);
72827c478bd9Sstevel@tonic-gate 	}
72837c478bd9Sstevel@tonic-gate 
72847c478bd9Sstevel@tonic-gate 	exit_driver(dnp);
72857c478bd9Sstevel@tonic-gate 	ddi_rele_driver(major);
72867c478bd9Sstevel@tonic-gate 	return (NULL);
72877c478bd9Sstevel@tonic-gate }
72887c478bd9Sstevel@tonic-gate 
72897c478bd9Sstevel@tonic-gate /*
72907c478bd9Sstevel@tonic-gate  * Default bus_config entry point for nexus drivers
72917c478bd9Sstevel@tonic-gate  */
72927c478bd9Sstevel@tonic-gate int
72937c478bd9Sstevel@tonic-gate ndi_busop_bus_config(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
72947c478bd9Sstevel@tonic-gate     void *arg, dev_info_t **child, clock_t timeout)
72957c478bd9Sstevel@tonic-gate {
72967c478bd9Sstevel@tonic-gate 	major_t major;
72977c478bd9Sstevel@tonic-gate 
72987c478bd9Sstevel@tonic-gate 	/*
72997c478bd9Sstevel@tonic-gate 	 * A timeout of 30 minutes or more is probably a mistake
73007c478bd9Sstevel@tonic-gate 	 * This is intended to catch uses where timeout is in
73017c478bd9Sstevel@tonic-gate 	 * the wrong units.  timeout must be in units of ticks.
73027c478bd9Sstevel@tonic-gate 	 */
73037c478bd9Sstevel@tonic-gate 	ASSERT(timeout < SEC_TO_TICK(1800));
73047c478bd9Sstevel@tonic-gate 
7305a204de77Scth 	major = DDI_MAJOR_T_NONE;
73067c478bd9Sstevel@tonic-gate 	switch (op) {
73077c478bd9Sstevel@tonic-gate 	case BUS_CONFIG_ONE:
73087c478bd9Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config %s timeout=%ld\n",
73097c478bd9Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
73107c478bd9Sstevel@tonic-gate 		    (char *)arg, timeout));
73117c478bd9Sstevel@tonic-gate 		return (devi_config_one(pdip, (char *)arg, child, flags,
73127c478bd9Sstevel@tonic-gate 		    timeout));
73137c478bd9Sstevel@tonic-gate 
73147c478bd9Sstevel@tonic-gate 	case BUS_CONFIG_DRIVER:
73157c478bd9Sstevel@tonic-gate 		major = (major_t)(uintptr_t)arg;
73167c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
73177c478bd9Sstevel@tonic-gate 	case BUS_CONFIG_ALL:
73187c478bd9Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config timeout=%ld\n",
73197c478bd9Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
73207c478bd9Sstevel@tonic-gate 		    timeout));
73217c478bd9Sstevel@tonic-gate 		if (timeout > 0) {
73227c478bd9Sstevel@tonic-gate 			NDI_DEBUG(flags, (CE_CONT,
73237c478bd9Sstevel@tonic-gate 			    "%s%d: bus config all timeout=%ld\n",
73247c478bd9Sstevel@tonic-gate 			    ddi_driver_name(pdip), ddi_get_instance(pdip),
73257c478bd9Sstevel@tonic-gate 			    timeout));
73267c478bd9Sstevel@tonic-gate 			delay(timeout);
73277c478bd9Sstevel@tonic-gate 		}
73287c478bd9Sstevel@tonic-gate 		return (config_immediate_children(pdip, flags, major));
73297c478bd9Sstevel@tonic-gate 
73307c478bd9Sstevel@tonic-gate 	default:
73317c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
73327c478bd9Sstevel@tonic-gate 	}
73337c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
73347c478bd9Sstevel@tonic-gate }
73357c478bd9Sstevel@tonic-gate 
73367c478bd9Sstevel@tonic-gate /*
73377c478bd9Sstevel@tonic-gate  * Default busop bus_unconfig handler for nexus drivers
73387c478bd9Sstevel@tonic-gate  */
73397c478bd9Sstevel@tonic-gate int
73407c478bd9Sstevel@tonic-gate ndi_busop_bus_unconfig(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
73417c478bd9Sstevel@tonic-gate     void *arg)
73427c478bd9Sstevel@tonic-gate {
73437c478bd9Sstevel@tonic-gate 	major_t major;
73447c478bd9Sstevel@tonic-gate 
7345a204de77Scth 	major = DDI_MAJOR_T_NONE;
73467c478bd9Sstevel@tonic-gate 	switch (op) {
73477c478bd9Sstevel@tonic-gate 	case BUS_UNCONFIG_ONE:
73487c478bd9Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig %s\n",
73497c478bd9Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
73507c478bd9Sstevel@tonic-gate 		    (char *)arg));
73517c478bd9Sstevel@tonic-gate 		return (devi_unconfig_one(pdip, (char *)arg, flags));
73527c478bd9Sstevel@tonic-gate 
73537c478bd9Sstevel@tonic-gate 	case BUS_UNCONFIG_DRIVER:
73547c478bd9Sstevel@tonic-gate 		major = (major_t)(uintptr_t)arg;
73557c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
73567c478bd9Sstevel@tonic-gate 	case BUS_UNCONFIG_ALL:
73577c478bd9Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig all\n",
73587c478bd9Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip)));
73597c478bd9Sstevel@tonic-gate 		return (unconfig_immediate_children(pdip, NULL, flags, major));
73607c478bd9Sstevel@tonic-gate 
73617c478bd9Sstevel@tonic-gate 	default:
73627c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
73637c478bd9Sstevel@tonic-gate 	}
73647c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
73657c478bd9Sstevel@tonic-gate }
73667c478bd9Sstevel@tonic-gate 
73677c478bd9Sstevel@tonic-gate /*
73687c478bd9Sstevel@tonic-gate  * dummy functions to be removed
73697c478bd9Sstevel@tonic-gate  */
73707c478bd9Sstevel@tonic-gate void
73717c478bd9Sstevel@tonic-gate impl_rem_dev_props(dev_info_t *dip)
73727c478bd9Sstevel@tonic-gate {
73737c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip))
73747c478bd9Sstevel@tonic-gate 	/* do nothing */
73757c478bd9Sstevel@tonic-gate }
73767c478bd9Sstevel@tonic-gate 
73777c478bd9Sstevel@tonic-gate /*
73787c478bd9Sstevel@tonic-gate  * Determine if a node is a leaf node. If not sure, return false (0).
73797c478bd9Sstevel@tonic-gate  */
73807c478bd9Sstevel@tonic-gate static int
73817c478bd9Sstevel@tonic-gate is_leaf_node(dev_info_t *dip)
73827c478bd9Sstevel@tonic-gate {
73837c478bd9Sstevel@tonic-gate 	major_t major = ddi_driver_major(dip);
73847c478bd9Sstevel@tonic-gate 
7385a204de77Scth 	if (major == DDI_MAJOR_T_NONE)
73867c478bd9Sstevel@tonic-gate 		return (0);
73877c478bd9Sstevel@tonic-gate 
73887c478bd9Sstevel@tonic-gate 	return (devnamesp[major].dn_flags & DN_LEAF_DRIVER);
73897c478bd9Sstevel@tonic-gate }
73907c478bd9Sstevel@tonic-gate 
73917c478bd9Sstevel@tonic-gate /*
73927c478bd9Sstevel@tonic-gate  * Multithreaded [un]configuration
73937c478bd9Sstevel@tonic-gate  */
73947c478bd9Sstevel@tonic-gate static struct mt_config_handle *
73957c478bd9Sstevel@tonic-gate mt_config_init(dev_info_t *pdip, dev_info_t **dipp, int flags,
73967c478bd9Sstevel@tonic-gate     major_t major, int op, struct brevq_node **brevqp)
73977c478bd9Sstevel@tonic-gate {
73987c478bd9Sstevel@tonic-gate 	struct mt_config_handle	*hdl = kmem_alloc(sizeof (*hdl), KM_SLEEP);
73997c478bd9Sstevel@tonic-gate 
74007c478bd9Sstevel@tonic-gate 	mutex_init(&hdl->mtc_lock, NULL, MUTEX_DEFAULT, NULL);
74017c478bd9Sstevel@tonic-gate 	cv_init(&hdl->mtc_cv, NULL, CV_DEFAULT, NULL);
74027c478bd9Sstevel@tonic-gate 	hdl->mtc_pdip = pdip;
74037c478bd9Sstevel@tonic-gate 	hdl->mtc_fdip = dipp;
7404a204de77Scth 	hdl->mtc_parmajor = DDI_MAJOR_T_NONE;
74057c478bd9Sstevel@tonic-gate 	hdl->mtc_flags = flags;
74067c478bd9Sstevel@tonic-gate 	hdl->mtc_major = major;
74077c478bd9Sstevel@tonic-gate 	hdl->mtc_thr_count = 0;
74087c478bd9Sstevel@tonic-gate 	hdl->mtc_op = op;
74097c478bd9Sstevel@tonic-gate 	hdl->mtc_error = 0;
74107c478bd9Sstevel@tonic-gate 	hdl->mtc_brevqp = brevqp;
74117c478bd9Sstevel@tonic-gate 
74127c478bd9Sstevel@tonic-gate #ifdef DEBUG
74137c478bd9Sstevel@tonic-gate 	gethrestime(&hdl->start_time);
74147c478bd9Sstevel@tonic-gate 	hdl->total_time = 0;
74157c478bd9Sstevel@tonic-gate #endif /* DEBUG */
74167c478bd9Sstevel@tonic-gate 
74177c478bd9Sstevel@tonic-gate 	return (hdl);
74187c478bd9Sstevel@tonic-gate }
74197c478bd9Sstevel@tonic-gate 
74207c478bd9Sstevel@tonic-gate #ifdef DEBUG
74217c478bd9Sstevel@tonic-gate static int
74227c478bd9Sstevel@tonic-gate time_diff_in_msec(timestruc_t start, timestruc_t end)
74237c478bd9Sstevel@tonic-gate {
74247c478bd9Sstevel@tonic-gate 	int	nsec, sec;
74257c478bd9Sstevel@tonic-gate 
74267c478bd9Sstevel@tonic-gate 	sec = end.tv_sec - start.tv_sec;
74277c478bd9Sstevel@tonic-gate 	nsec = end.tv_nsec - start.tv_nsec;
74287c478bd9Sstevel@tonic-gate 	if (nsec < 0) {
74297c478bd9Sstevel@tonic-gate 		nsec += NANOSEC;
74307c478bd9Sstevel@tonic-gate 		sec -= 1;
74317c478bd9Sstevel@tonic-gate 	}
74327c478bd9Sstevel@tonic-gate 
74337c478bd9Sstevel@tonic-gate 	return (sec * (NANOSEC >> 20) + (nsec >> 20));
74347c478bd9Sstevel@tonic-gate }
74357c478bd9Sstevel@tonic-gate 
74367c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
74377c478bd9Sstevel@tonic-gate 
74387c478bd9Sstevel@tonic-gate static int
74397c478bd9Sstevel@tonic-gate mt_config_fini(struct mt_config_handle *hdl)
74407c478bd9Sstevel@tonic-gate {
74417c478bd9Sstevel@tonic-gate 	int		rv;
74427c478bd9Sstevel@tonic-gate #ifdef DEBUG
74437c478bd9Sstevel@tonic-gate 	int		real_time;
74447c478bd9Sstevel@tonic-gate 	timestruc_t	end_time;
74457c478bd9Sstevel@tonic-gate #endif /* DEBUG */
74467c478bd9Sstevel@tonic-gate 
74477c478bd9Sstevel@tonic-gate 	mutex_enter(&hdl->mtc_lock);
74487c478bd9Sstevel@tonic-gate 	while (hdl->mtc_thr_count > 0)
74497c478bd9Sstevel@tonic-gate 		cv_wait(&hdl->mtc_cv, &hdl->mtc_lock);
74507c478bd9Sstevel@tonic-gate 	rv = hdl->mtc_error;
74517c478bd9Sstevel@tonic-gate 	mutex_exit(&hdl->mtc_lock);
74527c478bd9Sstevel@tonic-gate 
74537c478bd9Sstevel@tonic-gate #ifdef DEBUG
74547c478bd9Sstevel@tonic-gate 	gethrestime(&end_time);
74557c478bd9Sstevel@tonic-gate 	real_time = time_diff_in_msec(hdl->start_time, end_time);
74567c478bd9Sstevel@tonic-gate 	if ((ddidebug & DDI_MTCONFIG) && hdl->mtc_pdip)
74577c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE,
74587c478bd9Sstevel@tonic-gate 		    "config %s%d: total time %d msec, real time %d msec",
74597c478bd9Sstevel@tonic-gate 		    ddi_driver_name(hdl->mtc_pdip),
74607c478bd9Sstevel@tonic-gate 		    ddi_get_instance(hdl->mtc_pdip),
74617c478bd9Sstevel@tonic-gate 		    hdl->total_time, real_time);
74627c478bd9Sstevel@tonic-gate #endif /* DEBUG */
74637c478bd9Sstevel@tonic-gate 
74647c478bd9Sstevel@tonic-gate 	cv_destroy(&hdl->mtc_cv);
74657c478bd9Sstevel@tonic-gate 	mutex_destroy(&hdl->mtc_lock);
74667c478bd9Sstevel@tonic-gate 	kmem_free(hdl, sizeof (*hdl));
74677c478bd9Sstevel@tonic-gate 
74687c478bd9Sstevel@tonic-gate 	return (rv);
74697c478bd9Sstevel@tonic-gate }
74707c478bd9Sstevel@tonic-gate 
74717c478bd9Sstevel@tonic-gate struct mt_config_data {
74727c478bd9Sstevel@tonic-gate 	struct mt_config_handle	*mtc_hdl;
74737c478bd9Sstevel@tonic-gate 	dev_info_t		*mtc_dip;
74747c478bd9Sstevel@tonic-gate 	major_t			mtc_major;
74757c478bd9Sstevel@tonic-gate 	int			mtc_flags;
74767c478bd9Sstevel@tonic-gate 	struct brevq_node	*mtc_brn;
74777c478bd9Sstevel@tonic-gate 	struct mt_config_data	*mtc_next;
74787c478bd9Sstevel@tonic-gate };
74797c478bd9Sstevel@tonic-gate 
74807c478bd9Sstevel@tonic-gate static void
74817c478bd9Sstevel@tonic-gate mt_config_thread(void *arg)
74827c478bd9Sstevel@tonic-gate {
74837c478bd9Sstevel@tonic-gate 	struct mt_config_data	*mcd = (struct mt_config_data *)arg;
74847c478bd9Sstevel@tonic-gate 	struct mt_config_handle	*hdl = mcd->mtc_hdl;
74857c478bd9Sstevel@tonic-gate 	dev_info_t		*dip = mcd->mtc_dip;
74867c478bd9Sstevel@tonic-gate 	dev_info_t		*rdip, **dipp;
74877c478bd9Sstevel@tonic-gate 	major_t			major = mcd->mtc_major;
74887c478bd9Sstevel@tonic-gate 	int			flags = mcd->mtc_flags;
74897c478bd9Sstevel@tonic-gate 	int			rv = 0;
74907c478bd9Sstevel@tonic-gate 
74917c478bd9Sstevel@tonic-gate #ifdef DEBUG
74927c478bd9Sstevel@tonic-gate 	timestruc_t start_time, end_time;
74937c478bd9Sstevel@tonic-gate 	gethrestime(&start_time);
74947c478bd9Sstevel@tonic-gate #endif /* DEBUG */
74957c478bd9Sstevel@tonic-gate 
74967c478bd9Sstevel@tonic-gate 	rdip = NULL;
74977c478bd9Sstevel@tonic-gate 	dipp = hdl->mtc_fdip ? &rdip : NULL;
74987c478bd9Sstevel@tonic-gate 
74997c478bd9Sstevel@tonic-gate 	switch (hdl->mtc_op) {
75007c478bd9Sstevel@tonic-gate 	case MT_CONFIG_OP:
75017c478bd9Sstevel@tonic-gate 		rv = devi_config_common(dip, flags, major);
75027c478bd9Sstevel@tonic-gate 		break;
75037c478bd9Sstevel@tonic-gate 	case MT_UNCONFIG_OP:
75047c478bd9Sstevel@tonic-gate 		if (mcd->mtc_brn) {
75057c478bd9Sstevel@tonic-gate 			struct brevq_node *brevq = NULL;
75067c478bd9Sstevel@tonic-gate 			rv = devi_unconfig_common(dip, dipp, flags, major,
75077c478bd9Sstevel@tonic-gate 			    &brevq);
7508245c82d9Scth 			mcd->mtc_brn->brn_child = brevq;
75097c478bd9Sstevel@tonic-gate 		} else
75107c478bd9Sstevel@tonic-gate 			rv = devi_unconfig_common(dip, dipp, flags, major,
75117c478bd9Sstevel@tonic-gate 			    NULL);
75127c478bd9Sstevel@tonic-gate 		break;
75137c478bd9Sstevel@tonic-gate 	}
75147c478bd9Sstevel@tonic-gate 
75157c478bd9Sstevel@tonic-gate 	mutex_enter(&hdl->mtc_lock);
75167c478bd9Sstevel@tonic-gate #ifdef DEBUG
75177c478bd9Sstevel@tonic-gate 	gethrestime(&end_time);
75187c478bd9Sstevel@tonic-gate 	hdl->total_time += time_diff_in_msec(start_time, end_time);
75197c478bd9Sstevel@tonic-gate #endif /* DEBUG */
75205e3986cbScth 
75215e3986cbScth 	if ((rv != NDI_SUCCESS) && (hdl->mtc_error == 0)) {
75227c478bd9Sstevel@tonic-gate 		hdl->mtc_error = rv;
75235e3986cbScth #ifdef	DEBUG
7524a204de77Scth 		if ((ddidebug & DDI_DEBUG) && (major != DDI_MAJOR_T_NONE)) {
75255e3986cbScth 			char	*path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
75265e3986cbScth 
75275e3986cbScth 			(void) ddi_pathname(dip, path);
75285e3986cbScth 			cmn_err(CE_NOTE, "mt_config_thread: "
75295e3986cbScth 			    "op %d.%d.%x at %s failed %d",
75305e3986cbScth 			    hdl->mtc_op, major, flags, path, rv);
75315e3986cbScth 			kmem_free(path, MAXPATHLEN);
75325e3986cbScth 		}
75335e3986cbScth #endif	/* DEBUG */
75345e3986cbScth 	}
75355e3986cbScth 
75367c478bd9Sstevel@tonic-gate 	if (hdl->mtc_fdip && *hdl->mtc_fdip == NULL) {
75377c478bd9Sstevel@tonic-gate 		*hdl->mtc_fdip = rdip;
75387c478bd9Sstevel@tonic-gate 		rdip = NULL;
75397c478bd9Sstevel@tonic-gate 	}
75407c478bd9Sstevel@tonic-gate 
75417c478bd9Sstevel@tonic-gate 	if (rdip) {
75427c478bd9Sstevel@tonic-gate 		ASSERT(rv != NDI_SUCCESS);
75437c478bd9Sstevel@tonic-gate 		ndi_rele_devi(rdip);
75447c478bd9Sstevel@tonic-gate 	}
75457c478bd9Sstevel@tonic-gate 
75467c478bd9Sstevel@tonic-gate 	ndi_rele_devi(dip);
75473b3b7f33Sbm42561 
75483b3b7f33Sbm42561 	if (--hdl->mtc_thr_count == 0)
75493b3b7f33Sbm42561 		cv_broadcast(&hdl->mtc_cv);
75503b3b7f33Sbm42561 	mutex_exit(&hdl->mtc_lock);
75517c478bd9Sstevel@tonic-gate 	kmem_free(mcd, sizeof (*mcd));
75527c478bd9Sstevel@tonic-gate }
75537c478bd9Sstevel@tonic-gate 
75547c478bd9Sstevel@tonic-gate /*
75557c478bd9Sstevel@tonic-gate  * Multi-threaded config/unconfig of child nexus
75567c478bd9Sstevel@tonic-gate  */
75577c478bd9Sstevel@tonic-gate static void
75587c478bd9Sstevel@tonic-gate mt_config_children(struct mt_config_handle *hdl)
75597c478bd9Sstevel@tonic-gate {
75607c478bd9Sstevel@tonic-gate 	dev_info_t		*pdip = hdl->mtc_pdip;
75617c478bd9Sstevel@tonic-gate 	major_t			major = hdl->mtc_major;
75627c478bd9Sstevel@tonic-gate 	dev_info_t		*dip;
75637c478bd9Sstevel@tonic-gate 	int			circ;
7564245c82d9Scth 	struct brevq_node	*brn;
75657c478bd9Sstevel@tonic-gate 	struct mt_config_data	*mcd_head = NULL;
75667c478bd9Sstevel@tonic-gate 	struct mt_config_data	*mcd_tail = NULL;
75677c478bd9Sstevel@tonic-gate 	struct mt_config_data	*mcd;
75687c478bd9Sstevel@tonic-gate #ifdef DEBUG
75697c478bd9Sstevel@tonic-gate 	timestruc_t		end_time;
75707c478bd9Sstevel@tonic-gate 
75717c478bd9Sstevel@tonic-gate 	/* Update total_time in handle */
75727c478bd9Sstevel@tonic-gate 	gethrestime(&end_time);
75737c478bd9Sstevel@tonic-gate 	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
75747c478bd9Sstevel@tonic-gate #endif
75757c478bd9Sstevel@tonic-gate 
75767c478bd9Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
75777c478bd9Sstevel@tonic-gate 	dip = ddi_get_child(pdip);
75787c478bd9Sstevel@tonic-gate 	while (dip) {
75797c478bd9Sstevel@tonic-gate 		if (hdl->mtc_op == MT_UNCONFIG_OP && hdl->mtc_brevqp &&
75807c478bd9Sstevel@tonic-gate 		    !(DEVI_EVREMOVE(dip)) &&
75817c478bd9Sstevel@tonic-gate 		    i_ddi_node_state(dip) >= DS_INITIALIZED) {
75827c478bd9Sstevel@tonic-gate 			/*
75837c478bd9Sstevel@tonic-gate 			 * Enqueue this dip's deviname.
75847c478bd9Sstevel@tonic-gate 			 * No need to hold a lock while enqueuing since this
75857c478bd9Sstevel@tonic-gate 			 * is the only thread doing the enqueue and no one
75867c478bd9Sstevel@tonic-gate 			 * walks the queue while we are in multithreaded
75877c478bd9Sstevel@tonic-gate 			 * unconfiguration.
75887c478bd9Sstevel@tonic-gate 			 */
75897c478bd9Sstevel@tonic-gate 			brn = brevq_enqueue(hdl->mtc_brevqp, dip, NULL);
7590245c82d9Scth 		} else
7591245c82d9Scth 			brn = NULL;
75927c478bd9Sstevel@tonic-gate 
75937c478bd9Sstevel@tonic-gate 		/*
75947c478bd9Sstevel@tonic-gate 		 * Hold the child that we are processing so he does not get
75957c478bd9Sstevel@tonic-gate 		 * removed. The corrisponding ndi_rele_devi() for children
75967c478bd9Sstevel@tonic-gate 		 * that are not being skipped is done at the end of
75977c478bd9Sstevel@tonic-gate 		 * mt_config_thread().
75987c478bd9Sstevel@tonic-gate 		 */
75997c478bd9Sstevel@tonic-gate 		ndi_hold_devi(dip);
76007c478bd9Sstevel@tonic-gate 
76017c478bd9Sstevel@tonic-gate 		/*
76027c478bd9Sstevel@tonic-gate 		 * skip leaf nodes and (for configure) nodes not
76037c478bd9Sstevel@tonic-gate 		 * fully attached.
76047c478bd9Sstevel@tonic-gate 		 */
76057c478bd9Sstevel@tonic-gate 		if (is_leaf_node(dip) ||
76067c478bd9Sstevel@tonic-gate 		    (hdl->mtc_op == MT_CONFIG_OP &&
76077c478bd9Sstevel@tonic-gate 		    i_ddi_node_state(dip) < DS_READY)) {
76087c478bd9Sstevel@tonic-gate 			ndi_rele_devi(dip);
76097c478bd9Sstevel@tonic-gate 			dip = ddi_get_next_sibling(dip);
76107c478bd9Sstevel@tonic-gate 			continue;
76117c478bd9Sstevel@tonic-gate 		}
76127c478bd9Sstevel@tonic-gate 
76137c478bd9Sstevel@tonic-gate 		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
76147c478bd9Sstevel@tonic-gate 		mcd->mtc_dip = dip;
76157c478bd9Sstevel@tonic-gate 		mcd->mtc_hdl = hdl;
76167c478bd9Sstevel@tonic-gate 		mcd->mtc_brn = brn;
76177c478bd9Sstevel@tonic-gate 
76187c478bd9Sstevel@tonic-gate 		/*
76197c478bd9Sstevel@tonic-gate 		 * Switch a 'driver' operation to an 'all' operation below a
76207c478bd9Sstevel@tonic-gate 		 * node bound to the driver.
76217c478bd9Sstevel@tonic-gate 		 */
7622a204de77Scth 		if ((major == DDI_MAJOR_T_NONE) ||
7623a204de77Scth 		    (major == ddi_driver_major(dip)))
7624a204de77Scth 			mcd->mtc_major = DDI_MAJOR_T_NONE;
76257c478bd9Sstevel@tonic-gate 		else
76267c478bd9Sstevel@tonic-gate 			mcd->mtc_major = major;
76277c478bd9Sstevel@tonic-gate 
76287c478bd9Sstevel@tonic-gate 		/*
76297c478bd9Sstevel@tonic-gate 		 * The unconfig-driver to unconfig-all conversion above
76307c478bd9Sstevel@tonic-gate 		 * constitutes an autodetach for NDI_DETACH_DRIVER calls,
76317c478bd9Sstevel@tonic-gate 		 * set NDI_AUTODETACH.
76327c478bd9Sstevel@tonic-gate 		 */
76337c478bd9Sstevel@tonic-gate 		mcd->mtc_flags = hdl->mtc_flags;
76347c478bd9Sstevel@tonic-gate 		if ((mcd->mtc_flags & NDI_DETACH_DRIVER) &&
76357c478bd9Sstevel@tonic-gate 		    (hdl->mtc_op == MT_UNCONFIG_OP) &&
76367c478bd9Sstevel@tonic-gate 		    (major == ddi_driver_major(pdip)))
76377c478bd9Sstevel@tonic-gate 			mcd->mtc_flags |= NDI_AUTODETACH;
76387c478bd9Sstevel@tonic-gate 
76397c478bd9Sstevel@tonic-gate 		mutex_enter(&hdl->mtc_lock);
76407c478bd9Sstevel@tonic-gate 		hdl->mtc_thr_count++;
76417c478bd9Sstevel@tonic-gate 		mutex_exit(&hdl->mtc_lock);
76427c478bd9Sstevel@tonic-gate 
76437c478bd9Sstevel@tonic-gate 		/*
76447c478bd9Sstevel@tonic-gate 		 * Add to end of list to process after ndi_devi_exit to avoid
76457c478bd9Sstevel@tonic-gate 		 * locking differences depending on value of mtc_off.
76467c478bd9Sstevel@tonic-gate 		 */
76477c478bd9Sstevel@tonic-gate 		mcd->mtc_next = NULL;
76487c478bd9Sstevel@tonic-gate 		if (mcd_head == NULL)
76497c478bd9Sstevel@tonic-gate 			mcd_head = mcd;
76507c478bd9Sstevel@tonic-gate 		else
76517c478bd9Sstevel@tonic-gate 			mcd_tail->mtc_next = mcd;
76527c478bd9Sstevel@tonic-gate 		mcd_tail = mcd;
76537c478bd9Sstevel@tonic-gate 
76547c478bd9Sstevel@tonic-gate 		dip = ddi_get_next_sibling(dip);
76557c478bd9Sstevel@tonic-gate 	}
76567c478bd9Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
76577c478bd9Sstevel@tonic-gate 
76587c478bd9Sstevel@tonic-gate 	/* go through the list of held children */
76597c478bd9Sstevel@tonic-gate 	for (mcd = mcd_head; mcd; mcd = mcd_head) {
76607c478bd9Sstevel@tonic-gate 		mcd_head = mcd->mtc_next;
76615e3986cbScth 		if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
76627c478bd9Sstevel@tonic-gate 			mt_config_thread(mcd);
76637c478bd9Sstevel@tonic-gate 		else
76647c478bd9Sstevel@tonic-gate 			(void) thread_create(NULL, 0, mt_config_thread, mcd,
76657c478bd9Sstevel@tonic-gate 			    0, &p0, TS_RUN, minclsyspri);
76667c478bd9Sstevel@tonic-gate 	}
76677c478bd9Sstevel@tonic-gate }
76687c478bd9Sstevel@tonic-gate 
76697c478bd9Sstevel@tonic-gate static void
76707c478bd9Sstevel@tonic-gate mt_config_driver(struct mt_config_handle *hdl)
76717c478bd9Sstevel@tonic-gate {
76727c478bd9Sstevel@tonic-gate 	major_t			par_major = hdl->mtc_parmajor;
76737c478bd9Sstevel@tonic-gate 	major_t			major = hdl->mtc_major;
76747c478bd9Sstevel@tonic-gate 	struct devnames		*dnp = &devnamesp[par_major];
76757c478bd9Sstevel@tonic-gate 	dev_info_t		*dip;
76767c478bd9Sstevel@tonic-gate 	struct mt_config_data	*mcd_head = NULL;
76777c478bd9Sstevel@tonic-gate 	struct mt_config_data	*mcd_tail = NULL;
76787c478bd9Sstevel@tonic-gate 	struct mt_config_data	*mcd;
76797c478bd9Sstevel@tonic-gate #ifdef DEBUG
76807c478bd9Sstevel@tonic-gate 	timestruc_t		end_time;
76817c478bd9Sstevel@tonic-gate 
76827c478bd9Sstevel@tonic-gate 	/* Update total_time in handle */
76837c478bd9Sstevel@tonic-gate 	gethrestime(&end_time);
76847c478bd9Sstevel@tonic-gate 	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
76857c478bd9Sstevel@tonic-gate #endif
7686a204de77Scth 	ASSERT(par_major != DDI_MAJOR_T_NONE);
7687a204de77Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
76887c478bd9Sstevel@tonic-gate 
76897c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
76907c478bd9Sstevel@tonic-gate 	dip = devnamesp[par_major].dn_head;
76917c478bd9Sstevel@tonic-gate 	while (dip) {
76927c478bd9Sstevel@tonic-gate 		/*
76937c478bd9Sstevel@tonic-gate 		 * Hold the child that we are processing so he does not get
76947c478bd9Sstevel@tonic-gate 		 * removed. The corrisponding ndi_rele_devi() for children
76957c478bd9Sstevel@tonic-gate 		 * that are not being skipped is done at the end of
76967c478bd9Sstevel@tonic-gate 		 * mt_config_thread().
76977c478bd9Sstevel@tonic-gate 		 */
76987c478bd9Sstevel@tonic-gate 		ndi_hold_devi(dip);
76997c478bd9Sstevel@tonic-gate 
77007c478bd9Sstevel@tonic-gate 		/* skip leaf nodes and nodes not fully attached */
7701737d277aScth 		if (!i_ddi_devi_attached(dip) || is_leaf_node(dip)) {
77027c478bd9Sstevel@tonic-gate 			ndi_rele_devi(dip);
77037c478bd9Sstevel@tonic-gate 			dip = ddi_get_next(dip);
77047c478bd9Sstevel@tonic-gate 			continue;
77057c478bd9Sstevel@tonic-gate 		}
77067c478bd9Sstevel@tonic-gate 
77077c478bd9Sstevel@tonic-gate 		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
77087c478bd9Sstevel@tonic-gate 		mcd->mtc_dip = dip;
77097c478bd9Sstevel@tonic-gate 		mcd->mtc_hdl = hdl;
77107c478bd9Sstevel@tonic-gate 		mcd->mtc_major = major;
77117c478bd9Sstevel@tonic-gate 		mcd->mtc_flags = hdl->mtc_flags;
77127c478bd9Sstevel@tonic-gate 
77137c478bd9Sstevel@tonic-gate 		mutex_enter(&hdl->mtc_lock);
77147c478bd9Sstevel@tonic-gate 		hdl->mtc_thr_count++;
77157c478bd9Sstevel@tonic-gate 		mutex_exit(&hdl->mtc_lock);
77167c478bd9Sstevel@tonic-gate 
77177c478bd9Sstevel@tonic-gate 		/*
77187c478bd9Sstevel@tonic-gate 		 * Add to end of list to process after UNLOCK_DEV_OPS to avoid
77197c478bd9Sstevel@tonic-gate 		 * locking differences depending on value of mtc_off.
77207c478bd9Sstevel@tonic-gate 		 */
77217c478bd9Sstevel@tonic-gate 		mcd->mtc_next = NULL;
77227c478bd9Sstevel@tonic-gate 		if (mcd_head == NULL)
77237c478bd9Sstevel@tonic-gate 			mcd_head = mcd;
77247c478bd9Sstevel@tonic-gate 		else
77257c478bd9Sstevel@tonic-gate 			mcd_tail->mtc_next = mcd;
77267c478bd9Sstevel@tonic-gate 		mcd_tail = mcd;
77277c478bd9Sstevel@tonic-gate 
77287c478bd9Sstevel@tonic-gate 		dip = ddi_get_next(dip);
77297c478bd9Sstevel@tonic-gate 	}
77307c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
77317c478bd9Sstevel@tonic-gate 
77327c478bd9Sstevel@tonic-gate 	/* go through the list of held children */
77337c478bd9Sstevel@tonic-gate 	for (mcd = mcd_head; mcd; mcd = mcd_head) {
77347c478bd9Sstevel@tonic-gate 		mcd_head = mcd->mtc_next;
77355e3986cbScth 		if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
77367c478bd9Sstevel@tonic-gate 			mt_config_thread(mcd);
77377c478bd9Sstevel@tonic-gate 		else
77387c478bd9Sstevel@tonic-gate 			(void) thread_create(NULL, 0, mt_config_thread, mcd,
77397c478bd9Sstevel@tonic-gate 			    0, &p0, TS_RUN, minclsyspri);
77407c478bd9Sstevel@tonic-gate 	}
77417c478bd9Sstevel@tonic-gate }
77427c478bd9Sstevel@tonic-gate 
77437c478bd9Sstevel@tonic-gate /*
77447c478bd9Sstevel@tonic-gate  * Given the nodeid for a persistent (PROM or SID) node, return
77457c478bd9Sstevel@tonic-gate  * the corresponding devinfo node
77467c478bd9Sstevel@tonic-gate  * NOTE: This function will return NULL for .conf nodeids.
77477c478bd9Sstevel@tonic-gate  */
77487c478bd9Sstevel@tonic-gate dev_info_t *
7749fa9e4066Sahrens e_ddi_nodeid_to_dip(pnode_t nodeid)
77507c478bd9Sstevel@tonic-gate {
77517c478bd9Sstevel@tonic-gate 	dev_info_t		*dip = NULL;
77527c478bd9Sstevel@tonic-gate 	struct devi_nodeid	*prev, *elem;
77537c478bd9Sstevel@tonic-gate 
77547c478bd9Sstevel@tonic-gate 	mutex_enter(&devimap->dno_lock);
77557c478bd9Sstevel@tonic-gate 
77567c478bd9Sstevel@tonic-gate 	prev = NULL;
77577c478bd9Sstevel@tonic-gate 	for (elem = devimap->dno_head; elem; elem = elem->next) {
77587c478bd9Sstevel@tonic-gate 		if (elem->nodeid == nodeid) {
77597c478bd9Sstevel@tonic-gate 			ndi_hold_devi(elem->dip);
77607c478bd9Sstevel@tonic-gate 			dip = elem->dip;
77617c478bd9Sstevel@tonic-gate 			break;
77627c478bd9Sstevel@tonic-gate 		}
77637c478bd9Sstevel@tonic-gate 		prev = elem;
77647c478bd9Sstevel@tonic-gate 	}
77657c478bd9Sstevel@tonic-gate 
77667c478bd9Sstevel@tonic-gate 	/*
77677c478bd9Sstevel@tonic-gate 	 * Move to head for faster lookup next time
77687c478bd9Sstevel@tonic-gate 	 */
77697c478bd9Sstevel@tonic-gate 	if (elem && prev) {
77707c478bd9Sstevel@tonic-gate 		prev->next = elem->next;
77717c478bd9Sstevel@tonic-gate 		elem->next = devimap->dno_head;
77727c478bd9Sstevel@tonic-gate 		devimap->dno_head = elem;
77737c478bd9Sstevel@tonic-gate 	}
77747c478bd9Sstevel@tonic-gate 
77757c478bd9Sstevel@tonic-gate 	mutex_exit(&devimap->dno_lock);
77767c478bd9Sstevel@tonic-gate 	return (dip);
77777c478bd9Sstevel@tonic-gate }
77787c478bd9Sstevel@tonic-gate 
77797c478bd9Sstevel@tonic-gate static void
77807c478bd9Sstevel@tonic-gate free_cache_task(void *arg)
77817c478bd9Sstevel@tonic-gate {
77827c478bd9Sstevel@tonic-gate 	ASSERT(arg == NULL);
77837c478bd9Sstevel@tonic-gate 
77847c478bd9Sstevel@tonic-gate 	mutex_enter(&di_cache.cache_lock);
77857c478bd9Sstevel@tonic-gate 
77867c478bd9Sstevel@tonic-gate 	/*
77877c478bd9Sstevel@tonic-gate 	 * The cache can be invalidated without holding the lock
77887c478bd9Sstevel@tonic-gate 	 * but it can be made valid again only while the lock is held.
77897c478bd9Sstevel@tonic-gate 	 * So if the cache is invalid when the lock is held, it will
77907c478bd9Sstevel@tonic-gate 	 * stay invalid until lock is released.
77917c478bd9Sstevel@tonic-gate 	 */
77927c478bd9Sstevel@tonic-gate 	if (!di_cache.cache_valid)
77937c478bd9Sstevel@tonic-gate 		i_ddi_di_cache_free(&di_cache);
77947c478bd9Sstevel@tonic-gate 
77957c478bd9Sstevel@tonic-gate 	mutex_exit(&di_cache.cache_lock);
77967c478bd9Sstevel@tonic-gate 
77977c478bd9Sstevel@tonic-gate 	if (di_cache_debug)
77987c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "system_taskq: di_cache freed");
77997c478bd9Sstevel@tonic-gate }
78007c478bd9Sstevel@tonic-gate 
78017c478bd9Sstevel@tonic-gate extern int modrootloaded;
78027c478bd9Sstevel@tonic-gate 
78037c478bd9Sstevel@tonic-gate void
78047c478bd9Sstevel@tonic-gate i_ddi_di_cache_free(struct di_cache *cache)
78057c478bd9Sstevel@tonic-gate {
78067c478bd9Sstevel@tonic-gate 	int	error;
780719397407SSherry Moore 	extern int sys_shutdown;
78087c478bd9Sstevel@tonic-gate 
78097c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&cache->cache_lock));
78107c478bd9Sstevel@tonic-gate 
78117c478bd9Sstevel@tonic-gate 	if (cache->cache_size) {
78127c478bd9Sstevel@tonic-gate 		ASSERT(cache->cache_size > 0);
78137c478bd9Sstevel@tonic-gate 		ASSERT(cache->cache_data);
78147c478bd9Sstevel@tonic-gate 
78157c478bd9Sstevel@tonic-gate 		kmem_free(cache->cache_data, cache->cache_size);
78167c478bd9Sstevel@tonic-gate 		cache->cache_data = NULL;
78177c478bd9Sstevel@tonic-gate 		cache->cache_size = 0;
78187c478bd9Sstevel@tonic-gate 
78197c478bd9Sstevel@tonic-gate 		if (di_cache_debug)
78207c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "i_ddi_di_cache_free: freed cachemem");
78217c478bd9Sstevel@tonic-gate 	} else {
78227c478bd9Sstevel@tonic-gate 		ASSERT(cache->cache_data == NULL);
78237c478bd9Sstevel@tonic-gate 		if (di_cache_debug)
78247c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "i_ddi_di_cache_free: NULL cache");
78257c478bd9Sstevel@tonic-gate 	}
78267c478bd9Sstevel@tonic-gate 
7827c3b4ae18SJerry Gilliam 	if (!modrootloaded || rootvp == NULL ||
7828c3b4ae18SJerry Gilliam 	    vn_is_readonly(rootvp) || sys_shutdown) {
78297c478bd9Sstevel@tonic-gate 		if (di_cache_debug) {
78307c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "/ not mounted/RDONLY. Skip unlink");
78317c478bd9Sstevel@tonic-gate 		}
78327c478bd9Sstevel@tonic-gate 		return;
78337c478bd9Sstevel@tonic-gate 	}
78347c478bd9Sstevel@tonic-gate 
78357c478bd9Sstevel@tonic-gate 	error = vn_remove(DI_CACHE_FILE, UIO_SYSSPACE, RMFILE);
78367c478bd9Sstevel@tonic-gate 	if (di_cache_debug && error && error != ENOENT) {
78377c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: unlink failed: %d", DI_CACHE_FILE, error);
78387c478bd9Sstevel@tonic-gate 	} else if (di_cache_debug && !error) {
78397c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "i_ddi_di_cache_free: unlinked cache file");
78407c478bd9Sstevel@tonic-gate 	}
78417c478bd9Sstevel@tonic-gate }
78427c478bd9Sstevel@tonic-gate 
78437c478bd9Sstevel@tonic-gate void
78444c06356bSdh142964 i_ddi_di_cache_invalidate()
78457c478bd9Sstevel@tonic-gate {
7846e37c6c37Scth 	int	cache_valid;
78477c478bd9Sstevel@tonic-gate 
78487c478bd9Sstevel@tonic-gate 	if (!modrootloaded || !i_ddi_io_initialized()) {
78497c478bd9Sstevel@tonic-gate 		if (di_cache_debug)
78507c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "I/O not inited. Skipping invalidate");
78517c478bd9Sstevel@tonic-gate 		return;
78527c478bd9Sstevel@tonic-gate 	}
78537c478bd9Sstevel@tonic-gate 
7854e37c6c37Scth 	/* Increment devtree generation number. */
7855facf4a8dSllai1 	atomic_inc_ulong(&devtree_gen);
78567c478bd9Sstevel@tonic-gate 
7857e37c6c37Scth 	/* Invalidate the in-core cache and dispatch free on valid->invalid */
7858e37c6c37Scth 	cache_valid = atomic_swap_uint(&di_cache.cache_valid, 0);
7859e37c6c37Scth 	if (cache_valid) {
78604c06356bSdh142964 		/*
78614c06356bSdh142964 		 * This is an optimization to start cleaning up a cached
78624c06356bSdh142964 		 * snapshot early.  For this reason, it is OK for
78634c06356bSdh142964 		 * taskq_dispatach to fail (and it is OK to not track calling
78644c06356bSdh142964 		 * context relative to sleep, and assume NOSLEEP).
78654c06356bSdh142964 		 */
7866e37c6c37Scth 		(void) taskq_dispatch(system_taskq, free_cache_task, NULL,
78674c06356bSdh142964 		    TQ_NOSLEEP);
7868e37c6c37Scth 	}
78697c478bd9Sstevel@tonic-gate 
78707c478bd9Sstevel@tonic-gate 	if (di_cache_debug) {
78714c06356bSdh142964 		cmn_err(CE_NOTE, "invalidation");
78727c478bd9Sstevel@tonic-gate 	}
78737c478bd9Sstevel@tonic-gate }
78747c478bd9Sstevel@tonic-gate 
78757c478bd9Sstevel@tonic-gate 
78767c478bd9Sstevel@tonic-gate static void
78777c478bd9Sstevel@tonic-gate i_bind_vhci_node(dev_info_t *dip)
78787c478bd9Sstevel@tonic-gate {
7879f4da9be0Scth 	DEVI(dip)->devi_major = ddi_name_to_major(ddi_node_name(dip));
78807c478bd9Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_BOUND);
78817c478bd9Sstevel@tonic-gate }
78827c478bd9Sstevel@tonic-gate 
78837c478bd9Sstevel@tonic-gate static char vhci_node_addr[2];
78847c478bd9Sstevel@tonic-gate 
78857c478bd9Sstevel@tonic-gate static int
78867c478bd9Sstevel@tonic-gate i_init_vhci_node(dev_info_t *dip)
78877c478bd9Sstevel@tonic-gate {
78887c478bd9Sstevel@tonic-gate 	add_global_props(dip);
78897c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
78907c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_ops == NULL)
78917c478bd9Sstevel@tonic-gate 		return (-1);
78927c478bd9Sstevel@tonic-gate 
78937c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
78947c478bd9Sstevel@tonic-gate 	e_ddi_keep_instance(dip);
78957c478bd9Sstevel@tonic-gate 	vhci_node_addr[0]	= '\0';
78967c478bd9Sstevel@tonic-gate 	ddi_set_name_addr(dip, vhci_node_addr);
78977c478bd9Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_INITIALIZED);
78987c478bd9Sstevel@tonic-gate 	return (0);
78997c478bd9Sstevel@tonic-gate }
79007c478bd9Sstevel@tonic-gate 
79017c478bd9Sstevel@tonic-gate static void
79027c478bd9Sstevel@tonic-gate i_link_vhci_node(dev_info_t *dip)
79037c478bd9Sstevel@tonic-gate {
79049d3d2ed0Shiremath 	ASSERT(MUTEX_HELD(&global_vhci_lock));
79059d3d2ed0Shiremath 
79067c478bd9Sstevel@tonic-gate 	/*
79077c478bd9Sstevel@tonic-gate 	 * scsi_vhci should be kept left most of the device tree.
79087c478bd9Sstevel@tonic-gate 	 */
79097c478bd9Sstevel@tonic-gate 	if (scsi_vhci_dip) {
79107c478bd9Sstevel@tonic-gate 		DEVI(dip)->devi_sibling = DEVI(scsi_vhci_dip)->devi_sibling;
79117c478bd9Sstevel@tonic-gate 		DEVI(scsi_vhci_dip)->devi_sibling = DEVI(dip);
79127c478bd9Sstevel@tonic-gate 	} else {
79137c478bd9Sstevel@tonic-gate 		DEVI(dip)->devi_sibling = DEVI(top_devinfo)->devi_child;
79147c478bd9Sstevel@tonic-gate 		DEVI(top_devinfo)->devi_child = DEVI(dip);
79157c478bd9Sstevel@tonic-gate 	}
79167c478bd9Sstevel@tonic-gate }
79177c478bd9Sstevel@tonic-gate 
79187c478bd9Sstevel@tonic-gate 
79197c478bd9Sstevel@tonic-gate /*
79207c478bd9Sstevel@tonic-gate  * This a special routine to enumerate vhci node (child of rootnex
79217c478bd9Sstevel@tonic-gate  * node) without holding the ndi_devi_enter() lock. The device node
79227c478bd9Sstevel@tonic-gate  * is allocated, initialized and brought into DS_READY state before
79237c478bd9Sstevel@tonic-gate  * inserting into the device tree. The VHCI node is handcrafted
79247c478bd9Sstevel@tonic-gate  * here to bring the node to DS_READY, similar to rootnex node.
79257c478bd9Sstevel@tonic-gate  *
79267c478bd9Sstevel@tonic-gate  * The global_vhci_lock protects linking the node into the device
79277c478bd9Sstevel@tonic-gate  * as same lock is held before linking/unlinking any direct child
79287c478bd9Sstevel@tonic-gate  * of rootnex children.
79297c478bd9Sstevel@tonic-gate  *
79307c478bd9Sstevel@tonic-gate  * This routine is a workaround to handle a possible deadlock
79317c478bd9Sstevel@tonic-gate  * that occurs while trying to enumerate node in a different sub-tree
79327c478bd9Sstevel@tonic-gate  * during _init/_attach entry points.
79337c478bd9Sstevel@tonic-gate  */
79347c478bd9Sstevel@tonic-gate /*ARGSUSED*/
79357c478bd9Sstevel@tonic-gate dev_info_t *
79367c478bd9Sstevel@tonic-gate ndi_devi_config_vhci(char *drvname, int flags)
79377c478bd9Sstevel@tonic-gate {
79387c478bd9Sstevel@tonic-gate 	struct devnames		*dnp;
79397c478bd9Sstevel@tonic-gate 	dev_info_t		*dip;
79407c478bd9Sstevel@tonic-gate 	major_t			major = ddi_name_to_major(drvname);
79417c478bd9Sstevel@tonic-gate 
79427c478bd9Sstevel@tonic-gate 	if (major == -1)
79437c478bd9Sstevel@tonic-gate 		return (NULL);
79447c478bd9Sstevel@tonic-gate 
79457c478bd9Sstevel@tonic-gate 	/* Make sure we create the VHCI node only once */
79467c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
79477c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
79487c478bd9Sstevel@tonic-gate 	if (dnp->dn_head) {
79497c478bd9Sstevel@tonic-gate 		dip = dnp->dn_head;
79507c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
79517c478bd9Sstevel@tonic-gate 		return (dip);
79527c478bd9Sstevel@tonic-gate 	}
79537c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
79547c478bd9Sstevel@tonic-gate 
79557c478bd9Sstevel@tonic-gate 	/* Allocate the VHCI node */
79567c478bd9Sstevel@tonic-gate 	ndi_devi_alloc_sleep(top_devinfo, drvname, DEVI_SID_NODEID, &dip);
79577c478bd9Sstevel@tonic-gate 	ndi_hold_devi(dip);
79587c478bd9Sstevel@tonic-gate 
79597c478bd9Sstevel@tonic-gate 	/* Mark the node as VHCI */
79607c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_node_attributes |= DDI_VHCI_NODE;
79617c478bd9Sstevel@tonic-gate 
79627c478bd9Sstevel@tonic-gate 	i_ddi_add_devimap(dip);
79637c478bd9Sstevel@tonic-gate 	i_bind_vhci_node(dip);
79647c478bd9Sstevel@tonic-gate 	if (i_init_vhci_node(dip) == -1) {
79657c478bd9Sstevel@tonic-gate 		ndi_rele_devi(dip);
79667c478bd9Sstevel@tonic-gate 		(void) ndi_devi_free(dip);
79677c478bd9Sstevel@tonic-gate 		return (NULL);
79687c478bd9Sstevel@tonic-gate 	}
79697c478bd9Sstevel@tonic-gate 
797016747f41Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
79717c478bd9Sstevel@tonic-gate 	DEVI_SET_ATTACHING(dip);
797216747f41Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
797316747f41Scth 
79747c478bd9Sstevel@tonic-gate 	if (devi_attach(dip, DDI_ATTACH) != DDI_SUCCESS) {
79757c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "Could not attach %s driver", drvname);
79767c478bd9Sstevel@tonic-gate 		e_ddi_free_instance(dip, vhci_node_addr);
79777c478bd9Sstevel@tonic-gate 		ndi_rele_devi(dip);
79787c478bd9Sstevel@tonic-gate 		(void) ndi_devi_free(dip);
79797c478bd9Sstevel@tonic-gate 		return (NULL);
79807c478bd9Sstevel@tonic-gate 	}
798116747f41Scth 	mutex_enter(&(DEVI(dip)->devi_lock));
79827c478bd9Sstevel@tonic-gate 	DEVI_CLR_ATTACHING(dip);
798316747f41Scth 	mutex_exit(&(DEVI(dip)->devi_lock));
79847c478bd9Sstevel@tonic-gate 
79859d3d2ed0Shiremath 	mutex_enter(&global_vhci_lock);
79867c478bd9Sstevel@tonic-gate 	i_link_vhci_node(dip);
79879d3d2ed0Shiremath 	mutex_exit(&global_vhci_lock);
79887c478bd9Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_READY);
79897c478bd9Sstevel@tonic-gate 
79907c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
79917c478bd9Sstevel@tonic-gate 	dnp->dn_flags |= DN_DRIVER_HELD;
79927c478bd9Sstevel@tonic-gate 	dnp->dn_head = dip;
79937c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
79947c478bd9Sstevel@tonic-gate 
79957c478bd9Sstevel@tonic-gate 	i_ndi_devi_report_status_change(dip, NULL);
79967c478bd9Sstevel@tonic-gate 
79977c478bd9Sstevel@tonic-gate 	return (dip);
79987c478bd9Sstevel@tonic-gate }
79997c478bd9Sstevel@tonic-gate 
80007c478bd9Sstevel@tonic-gate /*
80014c06356bSdh142964  * Maintain DEVI_DEVICE_REMOVED hotplug devi_state for remove/reinsert hotplug
80024c06356bSdh142964  * of open devices. Currently, because of tight coupling between the devfs file
80034c06356bSdh142964  * system and the Solaris device tree, a driver can't always make the device
80044c06356bSdh142964  * tree state (esp devi_node_state) match device hardware hotplug state. Until
80054c06356bSdh142964  * resolved, to overcome this deficiency we use the following interfaces that
80064c06356bSdh142964  * maintain the DEVI_DEVICE_REMOVED devi_state status bit.  These interface
80074c06356bSdh142964  * report current state, and drive operation (like events and cache
80084c06356bSdh142964  * invalidation) when a driver changes remove/insert state of an open device.
80094c06356bSdh142964  *
80104c06356bSdh142964  * The ndi_devi_device_isremoved() returns 1 if the device is currently removed.
80114c06356bSdh142964  *
80124c06356bSdh142964  * The ndi_devi_device_remove() interface declares the device as removed, and
80134c06356bSdh142964  * returns 1 if there was a state change associated with this declaration.
80144c06356bSdh142964  *
80154c06356bSdh142964  * The ndi_devi_device_insert() declares the device as inserted, and returns 1
80164c06356bSdh142964  * if there was a state change associated with this declaration.
80174c06356bSdh142964  */
80184c06356bSdh142964 int
80194c06356bSdh142964 ndi_devi_device_isremoved(dev_info_t *dip)
80204c06356bSdh142964 {
80214c06356bSdh142964 	return (DEVI_IS_DEVICE_REMOVED(dip));
80224c06356bSdh142964 }
80234c06356bSdh142964 
80244c06356bSdh142964 int
80254c06356bSdh142964 ndi_devi_device_remove(dev_info_t *dip)
80264c06356bSdh142964 {
80274c06356bSdh142964 	ASSERT(dip && ddi_get_parent(dip) &&
80284c06356bSdh142964 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
80294c06356bSdh142964 
80304c06356bSdh142964 	/* Return if already marked removed. */
80314c06356bSdh142964 	if (ndi_devi_device_isremoved(dip))
80324c06356bSdh142964 		return (0);
80334c06356bSdh142964 
80344c06356bSdh142964 	/* Mark the device as having been physically removed. */
80354c06356bSdh142964 	mutex_enter(&(DEVI(dip)->devi_lock));
80364c06356bSdh142964 	ndi_devi_set_hidden(dip);	/* invisible: lookup/snapshot */
80374c06356bSdh142964 	DEVI_SET_DEVICE_REMOVED(dip);
80384c06356bSdh142964 	DEVI_SET_EVREMOVE(dip);		/* this clears EVADD too */
80394c06356bSdh142964 	mutex_exit(&(DEVI(dip)->devi_lock));
80404c06356bSdh142964 
80414c06356bSdh142964 	/* report remove (as 'removed') */
80424c06356bSdh142964 	i_ndi_devi_report_status_change(dip, NULL);
80434c06356bSdh142964 
80444c06356bSdh142964 	/*
80454c06356bSdh142964 	 * Invalidate the cache to ensure accurate
80464c06356bSdh142964 	 * (di_state() & DI_DEVICE_REMOVED).
80474c06356bSdh142964 	 */
80484c06356bSdh142964 	i_ddi_di_cache_invalidate();
80494c06356bSdh142964 
80504c06356bSdh142964 	/*
80514c06356bSdh142964 	 * Generate sysevent for those interested in removal (either directly
80524c06356bSdh142964 	 * via EC_DEVFS or indirectly via devfsadmd generated EC_DEV).
80534c06356bSdh142964 	 */
80544c06356bSdh142964 	i_ddi_log_devfs_device_remove(dip);
80554c06356bSdh142964 
80564c06356bSdh142964 	return (1);		/* DEVICE_REMOVED state changed */
80574c06356bSdh142964 }
80584c06356bSdh142964 
80594c06356bSdh142964 int
80604c06356bSdh142964 ndi_devi_device_insert(dev_info_t *dip)
80614c06356bSdh142964 {
80624c06356bSdh142964 	ASSERT(dip && ddi_get_parent(dip) &&
80634c06356bSdh142964 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
80644c06356bSdh142964 
80654c06356bSdh142964 	/* Return if not marked removed. */
80664c06356bSdh142964 	if (!ndi_devi_device_isremoved(dip))
80674c06356bSdh142964 		return (0);
80684c06356bSdh142964 
80694c06356bSdh142964 	/* Mark the device as having been physically reinserted. */
80704c06356bSdh142964 	mutex_enter(&(DEVI(dip)->devi_lock));
80714c06356bSdh142964 	ndi_devi_clr_hidden(dip);	/* visible: lookup/snapshot */
80724c06356bSdh142964 	DEVI_SET_DEVICE_REINSERTED(dip);
80734c06356bSdh142964 	DEVI_SET_EVADD(dip);		/* this clears EVREMOVE too */
80744c06356bSdh142964 	mutex_exit(&(DEVI(dip)->devi_lock));
80754c06356bSdh142964 
80764c06356bSdh142964 	/* report insert (as 'online') */
80774c06356bSdh142964 	i_ndi_devi_report_status_change(dip, NULL);
80784c06356bSdh142964 
80794c06356bSdh142964 	/*
80804c06356bSdh142964 	 * Invalidate the cache to ensure accurate
80814c06356bSdh142964 	 * (di_state() & DI_DEVICE_REMOVED).
80824c06356bSdh142964 	 */
80834c06356bSdh142964 	i_ddi_di_cache_invalidate();
80844c06356bSdh142964 
80854c06356bSdh142964 	/*
80864c06356bSdh142964 	 * Generate sysevent for those interested in removal (either directly
80874c06356bSdh142964 	 * via EC_DEVFS or indirectly via devfsadmd generated EC_DEV).
80884c06356bSdh142964 	 */
80894c06356bSdh142964 	i_ddi_log_devfs_device_insert(dip);
80904c06356bSdh142964 
80914c06356bSdh142964 	return (1);		/* DEVICE_REMOVED state changed */
80924c06356bSdh142964 }
80934c06356bSdh142964 
80944c06356bSdh142964 /*
80957c478bd9Sstevel@tonic-gate  * ibt_hw_is_present() returns 0 when there is no IB hardware actively
80967c478bd9Sstevel@tonic-gate  * running.  This is primarily useful for modules like rpcmod which
80977c478bd9Sstevel@tonic-gate  * needs a quick check to decide whether or not it should try to use
80987c478bd9Sstevel@tonic-gate  * InfiniBand
80997c478bd9Sstevel@tonic-gate  */
81007c478bd9Sstevel@tonic-gate int ib_hw_status = 0;
81017c478bd9Sstevel@tonic-gate int
81027c478bd9Sstevel@tonic-gate ibt_hw_is_present()
81037c478bd9Sstevel@tonic-gate {
81047c478bd9Sstevel@tonic-gate 	return (ib_hw_status);
81057c478bd9Sstevel@tonic-gate }
810625e8c5aaSvikram 
810725e8c5aaSvikram /*
810825e8c5aaSvikram  * ASSERT that constraint flag is not set and then set the "retire attempt"
810925e8c5aaSvikram  * flag.
811025e8c5aaSvikram  */
811125e8c5aaSvikram int
811225e8c5aaSvikram e_ddi_mark_retiring(dev_info_t *dip, void *arg)
811325e8c5aaSvikram {
811425e8c5aaSvikram 	char	**cons_array = (char **)arg;
811525e8c5aaSvikram 	char	*path;
811625e8c5aaSvikram 	int	constraint;
811725e8c5aaSvikram 	int	i;
811825e8c5aaSvikram 
811925e8c5aaSvikram 	constraint = 0;
812025e8c5aaSvikram 	if (cons_array) {
812125e8c5aaSvikram 		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
812225e8c5aaSvikram 		(void) ddi_pathname(dip, path);
812325e8c5aaSvikram 		for (i = 0; cons_array[i] != NULL; i++) {
812425e8c5aaSvikram 			if (strcmp(path, cons_array[i]) == 0) {
812525e8c5aaSvikram 				constraint = 1;
812625e8c5aaSvikram 				break;
812725e8c5aaSvikram 			}
812825e8c5aaSvikram 		}
812925e8c5aaSvikram 		kmem_free(path, MAXPATHLEN);
813025e8c5aaSvikram 	}
813125e8c5aaSvikram 
813225e8c5aaSvikram 	mutex_enter(&DEVI(dip)->devi_lock);
813325e8c5aaSvikram 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
813425e8c5aaSvikram 	DEVI(dip)->devi_flags |= DEVI_RETIRING;
813525e8c5aaSvikram 	if (constraint)
813625e8c5aaSvikram 		DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
813725e8c5aaSvikram 	mutex_exit(&DEVI(dip)->devi_lock);
813825e8c5aaSvikram 
813925e8c5aaSvikram 	RIO_VERBOSE((CE_NOTE, "marked dip as undergoing retire process dip=%p",
814025e8c5aaSvikram 	    (void *)dip));
814125e8c5aaSvikram 
814225e8c5aaSvikram 	if (constraint)
814325e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "marked dip as constrained, dip=%p",
814425e8c5aaSvikram 		    (void *)dip));
814525e8c5aaSvikram 
814625e8c5aaSvikram 	if (MDI_PHCI(dip))
814725e8c5aaSvikram 		mdi_phci_mark_retiring(dip, cons_array);
814825e8c5aaSvikram 
814925e8c5aaSvikram 	return (DDI_WALK_CONTINUE);
815025e8c5aaSvikram }
815125e8c5aaSvikram 
815225e8c5aaSvikram static void
815325e8c5aaSvikram free_array(char **cons_array)
815425e8c5aaSvikram {
815525e8c5aaSvikram 	int	i;
815625e8c5aaSvikram 
815725e8c5aaSvikram 	if (cons_array == NULL)
815825e8c5aaSvikram 		return;
815925e8c5aaSvikram 
816025e8c5aaSvikram 	for (i = 0; cons_array[i] != NULL; i++) {
816125e8c5aaSvikram 		kmem_free(cons_array[i], strlen(cons_array[i]) + 1);
816225e8c5aaSvikram 	}
816325e8c5aaSvikram 	kmem_free(cons_array, (i+1) * sizeof (char *));
816425e8c5aaSvikram }
816525e8c5aaSvikram 
816625e8c5aaSvikram /*
816725e8c5aaSvikram  * Walk *every* node in subtree and check if it blocks, allows or has no
816825e8c5aaSvikram  * comment on a proposed retire.
816925e8c5aaSvikram  */
817025e8c5aaSvikram int
817125e8c5aaSvikram e_ddi_retire_notify(dev_info_t *dip, void *arg)
817225e8c5aaSvikram {
817325e8c5aaSvikram 	int	*constraint = (int *)arg;
817425e8c5aaSvikram 
817525e8c5aaSvikram 	RIO_DEBUG((CE_NOTE, "retire notify: dip = %p", (void *)dip));
817625e8c5aaSvikram 
817725e8c5aaSvikram 	(void) e_ddi_offline_notify(dip);
817825e8c5aaSvikram 
817925e8c5aaSvikram 	mutex_enter(&(DEVI(dip)->devi_lock));
818025e8c5aaSvikram 	if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) {
818125e8c5aaSvikram 		RIO_DEBUG((CE_WARN, "retire notify: dip in retire "
818225e8c5aaSvikram 		    "subtree is not marked: dip = %p", (void *)dip));
818325e8c5aaSvikram 		*constraint = 0;
818425e8c5aaSvikram 	} else if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) {
818525e8c5aaSvikram 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
818625e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "retire notify: BLOCKED: dip = %p",
818725e8c5aaSvikram 		    (void *)dip));
818825e8c5aaSvikram 		*constraint = 0;
818925e8c5aaSvikram 	} else if (!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT)) {
819025e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "retire notify: NO CONSTRAINT: "
819125e8c5aaSvikram 		    "dip = %p", (void *)dip));
819225e8c5aaSvikram 		*constraint = 0;
819325e8c5aaSvikram 	} else {
819425e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "retire notify: CONSTRAINT set: "
819525e8c5aaSvikram 		    "dip = %p", (void *)dip));
819625e8c5aaSvikram 	}
819725e8c5aaSvikram 	mutex_exit(&DEVI(dip)->devi_lock);
819825e8c5aaSvikram 
819925e8c5aaSvikram 	if (MDI_PHCI(dip))
820025e8c5aaSvikram 		mdi_phci_retire_notify(dip, constraint);
820125e8c5aaSvikram 
820225e8c5aaSvikram 	return (DDI_WALK_CONTINUE);
820325e8c5aaSvikram }
820425e8c5aaSvikram 
820525e8c5aaSvikram int
820625e8c5aaSvikram e_ddi_retire_finalize(dev_info_t *dip, void *arg)
820725e8c5aaSvikram {
820825e8c5aaSvikram 	int constraint = *(int *)arg;
820925e8c5aaSvikram 	int finalize;
821025e8c5aaSvikram 	int phci_only;
821125e8c5aaSvikram 
821225e8c5aaSvikram 	mutex_enter(&DEVI(dip)->devi_lock);
821325e8c5aaSvikram 	if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) {
821425e8c5aaSvikram 		RIO_DEBUG((CE_WARN,
821525e8c5aaSvikram 		    "retire: unmarked dip(%p) in retire subtree",
821625e8c5aaSvikram 		    (void *)dip));
821725e8c5aaSvikram 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRED));
821825e8c5aaSvikram 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
821925e8c5aaSvikram 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
822025e8c5aaSvikram 		mutex_exit(&DEVI(dip)->devi_lock);
822125e8c5aaSvikram 		return (DDI_WALK_CONTINUE);
822225e8c5aaSvikram 	}
822325e8c5aaSvikram 
822425e8c5aaSvikram 	/*
822525e8c5aaSvikram 	 * retire the device if constraints have been applied
822625e8c5aaSvikram 	 * or if the device is not in use
822725e8c5aaSvikram 	 */
822825e8c5aaSvikram 	finalize = 0;
822925e8c5aaSvikram 	if (constraint) {
8230bf002425SStephen Hanson 		ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
8231bf002425SStephen Hanson 
823225e8c5aaSvikram 		ASSERT(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT);
823325e8c5aaSvikram 		ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
823425e8c5aaSvikram 		DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
823525e8c5aaSvikram 		DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
823625e8c5aaSvikram 		DEVI(dip)->devi_flags |= DEVI_RETIRED;
823725e8c5aaSvikram 		mutex_exit(&DEVI(dip)->devi_lock);
823825e8c5aaSvikram 		(void) spec_fence_snode(dip, NULL);
823925e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "Fenced off: dip = %p", (void *)dip));
824025e8c5aaSvikram 		e_ddi_offline_finalize(dip, DDI_SUCCESS);
824125e8c5aaSvikram 	} else {
824225e8c5aaSvikram 		if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) {
824325e8c5aaSvikram 			ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
824425e8c5aaSvikram 			DEVI(dip)->devi_flags &= ~DEVI_R_BLOCKED;
824525e8c5aaSvikram 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
824625e8c5aaSvikram 			/* we have already finalized during notify */
824725e8c5aaSvikram 		} else if (DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT) {
824825e8c5aaSvikram 			DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
824925e8c5aaSvikram 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
825025e8c5aaSvikram 			finalize = 1;
825125e8c5aaSvikram 		} else {
825225e8c5aaSvikram 			DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
825325e8c5aaSvikram 			/*
825425e8c5aaSvikram 			 * even if no contracts, need to call finalize
825525e8c5aaSvikram 			 * to clear the contract barrier on the dip
825625e8c5aaSvikram 			 */
825725e8c5aaSvikram 			finalize = 1;
825825e8c5aaSvikram 		}
825925e8c5aaSvikram 		mutex_exit(&DEVI(dip)->devi_lock);
826025e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "finalize: NOT retired: dip = %p",
826125e8c5aaSvikram 		    (void *)dip));
826225e8c5aaSvikram 		if (finalize)
826325e8c5aaSvikram 			e_ddi_offline_finalize(dip, DDI_FAILURE);
826425e8c5aaSvikram 	}
826525e8c5aaSvikram 
826625e8c5aaSvikram 	/*
826725e8c5aaSvikram 	 * phci_only variable indicates no client checking, just
826825e8c5aaSvikram 	 * offline the PHCI. We set that to 0 to enable client
826925e8c5aaSvikram 	 * checking
827025e8c5aaSvikram 	 */
827125e8c5aaSvikram 	phci_only = 0;
827225e8c5aaSvikram 	if (MDI_PHCI(dip))
8273bf002425SStephen Hanson 		mdi_phci_retire_finalize(dip, phci_only, arg);
827425e8c5aaSvikram 
827525e8c5aaSvikram 	return (DDI_WALK_CONTINUE);
827625e8c5aaSvikram }
827725e8c5aaSvikram 
827825e8c5aaSvikram /*
827925e8c5aaSvikram  * Returns
828025e8c5aaSvikram  *	DDI_SUCCESS if constraints allow retire
828125e8c5aaSvikram  *	DDI_FAILURE if constraints don't allow retire.
828225e8c5aaSvikram  * cons_array is a NULL terminated array of node paths for
828325e8c5aaSvikram  * which constraints have already been applied.
828425e8c5aaSvikram  */
828525e8c5aaSvikram int
828625e8c5aaSvikram e_ddi_retire_device(char *path, char **cons_array)
828725e8c5aaSvikram {
828825e8c5aaSvikram 	dev_info_t	*dip;
828925e8c5aaSvikram 	dev_info_t	*pdip;
829025e8c5aaSvikram 	int		circ;
829125e8c5aaSvikram 	int		circ2;
829225e8c5aaSvikram 	int		constraint;
829325e8c5aaSvikram 	char		*devnm;
829425e8c5aaSvikram 
829525e8c5aaSvikram 	/*
829625e8c5aaSvikram 	 * First, lookup the device
829725e8c5aaSvikram 	 */
829825e8c5aaSvikram 	dip = e_ddi_hold_devi_by_path(path, 0);
829925e8c5aaSvikram 	if (dip == NULL) {
830025e8c5aaSvikram 		/*
830125e8c5aaSvikram 		 * device does not exist. This device cannot be
830225e8c5aaSvikram 		 * a critical device since it is not in use. Thus
830325e8c5aaSvikram 		 * this device is always retireable. Return DDI_SUCCESS
830425e8c5aaSvikram 		 * to indicate this. If this device is ever
830525e8c5aaSvikram 		 * instantiated, I/O framework will consult the
830625e8c5aaSvikram 		 * the persistent retire store, mark it as
830725e8c5aaSvikram 		 * retired and fence it off.
830825e8c5aaSvikram 		 */
830925e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "Retire device: device doesn't exist."
831025e8c5aaSvikram 		    " NOP. Just returning SUCCESS. path=%s", path));
831125e8c5aaSvikram 		free_array(cons_array);
831225e8c5aaSvikram 		return (DDI_SUCCESS);
831325e8c5aaSvikram 	}
831425e8c5aaSvikram 
831525e8c5aaSvikram 	RIO_DEBUG((CE_NOTE, "Retire device: found dip = %p.", (void *)dip));
831625e8c5aaSvikram 
831725e8c5aaSvikram 	pdip = ddi_get_parent(dip);
831825e8c5aaSvikram 	ndi_hold_devi(pdip);
831925e8c5aaSvikram 
832025e8c5aaSvikram 	/*
832125e8c5aaSvikram 	 * Run devfs_clean() in case dip has no constraints and is
832225e8c5aaSvikram 	 * not in use, so is retireable but there are dv_nodes holding
832325e8c5aaSvikram 	 * ref-count on the dip. Note that devfs_clean() always returns
832425e8c5aaSvikram 	 * success.
832525e8c5aaSvikram 	 */
832625e8c5aaSvikram 	devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
832725e8c5aaSvikram 	(void) ddi_deviname(dip, devnm);
832825e8c5aaSvikram 	(void) devfs_clean(pdip, devnm + 1, DV_CLEAN_FORCE);
832925e8c5aaSvikram 	kmem_free(devnm, MAXNAMELEN + 1);
833025e8c5aaSvikram 
833125e8c5aaSvikram 	ndi_devi_enter(pdip, &circ);
833225e8c5aaSvikram 
833325e8c5aaSvikram 	/* release hold from e_ddi_hold_devi_by_path */
833425e8c5aaSvikram 	ndi_rele_devi(dip);
833525e8c5aaSvikram 
833625e8c5aaSvikram 	/*
833725e8c5aaSvikram 	 * If it cannot make a determination, is_leaf_node() assumes
833825e8c5aaSvikram 	 * dip is a nexus.
833925e8c5aaSvikram 	 */
834025e8c5aaSvikram 	(void) e_ddi_mark_retiring(dip, cons_array);
834125e8c5aaSvikram 	if (!is_leaf_node(dip)) {
834225e8c5aaSvikram 		ndi_devi_enter(dip, &circ2);
834325e8c5aaSvikram 		ddi_walk_devs(ddi_get_child(dip), e_ddi_mark_retiring,
834425e8c5aaSvikram 		    cons_array);
834525e8c5aaSvikram 		ndi_devi_exit(dip, circ2);
834625e8c5aaSvikram 	}
834725e8c5aaSvikram 	free_array(cons_array);
834825e8c5aaSvikram 
834925e8c5aaSvikram 	/*
835025e8c5aaSvikram 	 * apply constraints
835125e8c5aaSvikram 	 */
835225e8c5aaSvikram 	RIO_DEBUG((CE_NOTE, "retire: subtree retire notify: path = %s", path));
835325e8c5aaSvikram 
835425e8c5aaSvikram 	constraint = 1;	/* assume constraints allow retire */
835525e8c5aaSvikram 	(void) e_ddi_retire_notify(dip, &constraint);
835625e8c5aaSvikram 	if (!is_leaf_node(dip)) {
835725e8c5aaSvikram 		ndi_devi_enter(dip, &circ2);
835825e8c5aaSvikram 		ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_notify,
835925e8c5aaSvikram 		    &constraint);
836025e8c5aaSvikram 		ndi_devi_exit(dip, circ2);
836125e8c5aaSvikram 	}
836225e8c5aaSvikram 
836325e8c5aaSvikram 	/*
836425e8c5aaSvikram 	 * Now finalize the retire
836525e8c5aaSvikram 	 */
836625e8c5aaSvikram 	(void) e_ddi_retire_finalize(dip, &constraint);
836725e8c5aaSvikram 	if (!is_leaf_node(dip)) {
836825e8c5aaSvikram 		ndi_devi_enter(dip, &circ2);
836925e8c5aaSvikram 		ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_finalize,
837025e8c5aaSvikram 		    &constraint);
837125e8c5aaSvikram 		ndi_devi_exit(dip, circ2);
837225e8c5aaSvikram 	}
837325e8c5aaSvikram 
837425e8c5aaSvikram 	if (!constraint) {
837525e8c5aaSvikram 		RIO_DEBUG((CE_WARN, "retire failed: path = %s", path));
837625e8c5aaSvikram 	} else {
837725e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "retire succeeded: path = %s", path));
837825e8c5aaSvikram 	}
837925e8c5aaSvikram 
838025e8c5aaSvikram 	ndi_devi_exit(pdip, circ);
838125e8c5aaSvikram 	ndi_rele_devi(pdip);
838225e8c5aaSvikram 	return (constraint ? DDI_SUCCESS : DDI_FAILURE);
838325e8c5aaSvikram }
838425e8c5aaSvikram 
838525e8c5aaSvikram static int
838625e8c5aaSvikram unmark_and_unfence(dev_info_t *dip, void *arg)
838725e8c5aaSvikram {
838825e8c5aaSvikram 	char	*path = (char *)arg;
838925e8c5aaSvikram 
839025e8c5aaSvikram 	ASSERT(path);
839125e8c5aaSvikram 
839225e8c5aaSvikram 	(void) ddi_pathname(dip, path);
839325e8c5aaSvikram 
839425e8c5aaSvikram 	mutex_enter(&DEVI(dip)->devi_lock);
839525e8c5aaSvikram 	DEVI(dip)->devi_flags &= ~DEVI_RETIRED;
839625e8c5aaSvikram 	DEVI_SET_DEVICE_ONLINE(dip);
839725e8c5aaSvikram 	mutex_exit(&DEVI(dip)->devi_lock);
839825e8c5aaSvikram 
839925e8c5aaSvikram 	RIO_VERBOSE((CE_NOTE, "Cleared RETIRED flag: dip=%p, path=%s",
840025e8c5aaSvikram 	    (void *)dip, path));
840125e8c5aaSvikram 
840225e8c5aaSvikram 	(void) spec_unfence_snode(dip);
840325e8c5aaSvikram 	RIO_DEBUG((CE_NOTE, "Unfenced device: %s", path));
840425e8c5aaSvikram 
840525e8c5aaSvikram 	if (MDI_PHCI(dip))
840625e8c5aaSvikram 		mdi_phci_unretire(dip);
840725e8c5aaSvikram 
840825e8c5aaSvikram 	return (DDI_WALK_CONTINUE);
840925e8c5aaSvikram }
841025e8c5aaSvikram 
841125e8c5aaSvikram struct find_dip {
841225e8c5aaSvikram 	char	*fd_buf;
841325e8c5aaSvikram 	char	*fd_path;
841425e8c5aaSvikram 	dev_info_t *fd_dip;
841525e8c5aaSvikram };
841625e8c5aaSvikram 
841725e8c5aaSvikram static int
841825e8c5aaSvikram find_dip_fcn(dev_info_t *dip, void *arg)
841925e8c5aaSvikram {
842025e8c5aaSvikram 	struct find_dip *findp = (struct find_dip *)arg;
842125e8c5aaSvikram 
842225e8c5aaSvikram 	(void) ddi_pathname(dip, findp->fd_buf);
842325e8c5aaSvikram 
842425e8c5aaSvikram 	if (strcmp(findp->fd_path, findp->fd_buf) != 0)
842525e8c5aaSvikram 		return (DDI_WALK_CONTINUE);
842625e8c5aaSvikram 
842725e8c5aaSvikram 	ndi_hold_devi(dip);
842825e8c5aaSvikram 	findp->fd_dip = dip;
842925e8c5aaSvikram 
843025e8c5aaSvikram 	return (DDI_WALK_TERMINATE);
843125e8c5aaSvikram }
843225e8c5aaSvikram 
843325e8c5aaSvikram int
843425e8c5aaSvikram e_ddi_unretire_device(char *path)
843525e8c5aaSvikram {
843625e8c5aaSvikram 	int		circ;
8437ffc89d77Svikram 	int		circ2;
843825e8c5aaSvikram 	char		*path2;
843925e8c5aaSvikram 	dev_info_t	*pdip;
844025e8c5aaSvikram 	dev_info_t	*dip;
844125e8c5aaSvikram 	struct find_dip	 find_dip;
844225e8c5aaSvikram 
844325e8c5aaSvikram 	ASSERT(path);
844425e8c5aaSvikram 	ASSERT(*path == '/');
844525e8c5aaSvikram 
844625e8c5aaSvikram 	if (strcmp(path, "/") == 0) {
844725e8c5aaSvikram 		cmn_err(CE_WARN, "Root node cannot be retired. Skipping "
844825e8c5aaSvikram 		    "device unretire: %s", path);
844925e8c5aaSvikram 		return (0);
845025e8c5aaSvikram 	}
845125e8c5aaSvikram 
845225e8c5aaSvikram 	/*
845325e8c5aaSvikram 	 * We can't lookup the dip (corresponding to path) via
845425e8c5aaSvikram 	 * e_ddi_hold_devi_by_path() because the dip may be offline
845525e8c5aaSvikram 	 * and may not attach. Use ddi_walk_devs() instead;
845625e8c5aaSvikram 	 */
845725e8c5aaSvikram 	find_dip.fd_buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
845825e8c5aaSvikram 	find_dip.fd_path = path;
845925e8c5aaSvikram 	find_dip.fd_dip = NULL;
846025e8c5aaSvikram 
846125e8c5aaSvikram 	pdip = ddi_root_node();
846225e8c5aaSvikram 
846325e8c5aaSvikram 	ndi_devi_enter(pdip, &circ);
846425e8c5aaSvikram 	ddi_walk_devs(ddi_get_child(pdip), find_dip_fcn, &find_dip);
846525e8c5aaSvikram 	ndi_devi_exit(pdip, circ);
846625e8c5aaSvikram 
846725e8c5aaSvikram 	kmem_free(find_dip.fd_buf, MAXPATHLEN);
846825e8c5aaSvikram 
846925e8c5aaSvikram 	if (find_dip.fd_dip == NULL) {
847025e8c5aaSvikram 		cmn_err(CE_WARN, "Device not found in device tree. Skipping "
847125e8c5aaSvikram 		    "device unretire: %s", path);
847225e8c5aaSvikram 		return (0);
847325e8c5aaSvikram 	}
847425e8c5aaSvikram 
847525e8c5aaSvikram 	dip = find_dip.fd_dip;
847625e8c5aaSvikram 
847725e8c5aaSvikram 	pdip = ddi_get_parent(dip);
847825e8c5aaSvikram 
847925e8c5aaSvikram 	ndi_hold_devi(pdip);
848025e8c5aaSvikram 
848125e8c5aaSvikram 	ndi_devi_enter(pdip, &circ);
848225e8c5aaSvikram 
848325e8c5aaSvikram 	path2 = kmem_alloc(MAXPATHLEN, KM_SLEEP);
848425e8c5aaSvikram 
848525e8c5aaSvikram 	(void) unmark_and_unfence(dip, path2);
848625e8c5aaSvikram 	if (!is_leaf_node(dip)) {
8487ffc89d77Svikram 		ndi_devi_enter(dip, &circ2);
848825e8c5aaSvikram 		ddi_walk_devs(ddi_get_child(dip), unmark_and_unfence, path2);
8489ffc89d77Svikram 		ndi_devi_exit(dip, circ2);
849025e8c5aaSvikram 	}
849125e8c5aaSvikram 
849225e8c5aaSvikram 	kmem_free(path2, MAXPATHLEN);
849325e8c5aaSvikram 
849425e8c5aaSvikram 	/* release hold from find_dip_fcn() */
849525e8c5aaSvikram 	ndi_rele_devi(dip);
849625e8c5aaSvikram 
849725e8c5aaSvikram 	ndi_devi_exit(pdip, circ);
849825e8c5aaSvikram 
849925e8c5aaSvikram 	ndi_rele_devi(pdip);
850025e8c5aaSvikram 
850125e8c5aaSvikram 	return (0);
850225e8c5aaSvikram }
850325e8c5aaSvikram 
850425e8c5aaSvikram /*
850525e8c5aaSvikram  * Called before attach on a dip that has been retired.
850625e8c5aaSvikram  */
850725e8c5aaSvikram static int
850825e8c5aaSvikram mark_and_fence(dev_info_t *dip, void *arg)
850925e8c5aaSvikram {
851025e8c5aaSvikram 	char	*fencepath = (char *)arg;
851125e8c5aaSvikram 
851225e8c5aaSvikram 	/*
851325e8c5aaSvikram 	 * We have already decided to retire this device. The various
851425e8c5aaSvikram 	 * constraint checking should not be set.
851525e8c5aaSvikram 	 * NOTE that the retire flag may already be set due to
851625e8c5aaSvikram 	 * fenced -> detach -> fenced transitions.
851725e8c5aaSvikram 	 */
851825e8c5aaSvikram 	mutex_enter(&DEVI(dip)->devi_lock);
851925e8c5aaSvikram 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
852025e8c5aaSvikram 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
852125e8c5aaSvikram 	ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRING));
852225e8c5aaSvikram 	DEVI(dip)->devi_flags |= DEVI_RETIRED;
852325e8c5aaSvikram 	mutex_exit(&DEVI(dip)->devi_lock);
852425e8c5aaSvikram 	RIO_VERBOSE((CE_NOTE, "marked as RETIRED dip=%p", (void *)dip));
852525e8c5aaSvikram 
852625e8c5aaSvikram 	if (fencepath) {
852725e8c5aaSvikram 		(void) spec_fence_snode(dip, NULL);
852825e8c5aaSvikram 		RIO_DEBUG((CE_NOTE, "Fenced: %s",
852925e8c5aaSvikram 		    ddi_pathname(dip, fencepath)));
853025e8c5aaSvikram 	}
853125e8c5aaSvikram 
853225e8c5aaSvikram 	return (DDI_WALK_CONTINUE);
853325e8c5aaSvikram }
853425e8c5aaSvikram 
853525e8c5aaSvikram /*
853625e8c5aaSvikram  * Checks the retire database and:
853725e8c5aaSvikram  *
853825e8c5aaSvikram  * - if device is present in the retire database, marks the device retired
853925e8c5aaSvikram  *   and fences it off.
854025e8c5aaSvikram  * - if device is not in retire database, allows the device to attach normally
854125e8c5aaSvikram  *
854225e8c5aaSvikram  * To be called only by framework attach code on first attach attempt.
854325e8c5aaSvikram  *
854425e8c5aaSvikram  */
854525e8c5aaSvikram static void
854625e8c5aaSvikram i_ddi_check_retire(dev_info_t *dip)
854725e8c5aaSvikram {
854825e8c5aaSvikram 	char		*path;
854925e8c5aaSvikram 	dev_info_t	*pdip;
855025e8c5aaSvikram 	int		circ;
855125e8c5aaSvikram 	int		phci_only;
8552bf002425SStephen Hanson 	int		constraint;
855325e8c5aaSvikram 
855425e8c5aaSvikram 	pdip = ddi_get_parent(dip);
855525e8c5aaSvikram 
855625e8c5aaSvikram 	/*
855725e8c5aaSvikram 	 * Root dip is treated special and doesn't take this code path.
855825e8c5aaSvikram 	 * Also root can never be retired.
855925e8c5aaSvikram 	 */
856025e8c5aaSvikram 	ASSERT(pdip);
856125e8c5aaSvikram 	ASSERT(DEVI_BUSY_OWNED(pdip));
856225e8c5aaSvikram 	ASSERT(i_ddi_node_state(dip) < DS_ATTACHED);
856325e8c5aaSvikram 
856425e8c5aaSvikram 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
856525e8c5aaSvikram 
856625e8c5aaSvikram 	(void) ddi_pathname(dip, path);
856725e8c5aaSvikram 
856825e8c5aaSvikram 	RIO_VERBOSE((CE_NOTE, "Checking if dip should attach: dip=%p, path=%s",
856925e8c5aaSvikram 	    (void *)dip, path));
857025e8c5aaSvikram 
857125e8c5aaSvikram 	/*
857225e8c5aaSvikram 	 * Check if this device is in the "retired" store i.e.	should
857325e8c5aaSvikram 	 * be retired. If not, we have nothing to do.
857425e8c5aaSvikram 	 */
857525e8c5aaSvikram 	if (e_ddi_device_retired(path) == 0) {
857625e8c5aaSvikram 		RIO_VERBOSE((CE_NOTE, "device is NOT retired: path=%s", path));
857725e8c5aaSvikram 		kmem_free(path, MAXPATHLEN);
857825e8c5aaSvikram 		return;
857925e8c5aaSvikram 	}
858025e8c5aaSvikram 
858125e8c5aaSvikram 	RIO_DEBUG((CE_NOTE, "attach: device is retired: path=%s", path));
858225e8c5aaSvikram 
858325e8c5aaSvikram 	/*
858425e8c5aaSvikram 	 * Mark dips and fence off snodes (if any)
858525e8c5aaSvikram 	 */
858625e8c5aaSvikram 	RIO_DEBUG((CE_NOTE, "attach: Mark and fence subtree: path=%s", path));
858725e8c5aaSvikram 	(void) mark_and_fence(dip, path);
858825e8c5aaSvikram 	if (!is_leaf_node(dip)) {
858925e8c5aaSvikram 		ndi_devi_enter(dip, &circ);
859025e8c5aaSvikram 		ddi_walk_devs(ddi_get_child(dip), mark_and_fence, path);
859125e8c5aaSvikram 		ndi_devi_exit(dip, circ);
859225e8c5aaSvikram 	}
859325e8c5aaSvikram 
859425e8c5aaSvikram 	kmem_free(path, MAXPATHLEN);
859525e8c5aaSvikram 
859625e8c5aaSvikram 	/*
859725e8c5aaSvikram 	 * We don't want to check the client. We just want to
859825e8c5aaSvikram 	 * offline the PHCI
859925e8c5aaSvikram 	 */
860025e8c5aaSvikram 	phci_only = 1;
8601bf002425SStephen Hanson 	constraint = 1;
860225e8c5aaSvikram 	if (MDI_PHCI(dip))
8603bf002425SStephen Hanson 		mdi_phci_retire_finalize(dip, phci_only, &constraint);
860425e8c5aaSvikram }
86053a634bfcSVikram Hegde 
8606*94c894bbSVikram Hegde 
8607*94c894bbSVikram Hegde #define	VAL_ALIAS(array, x)	(strlen(array[x].pair_alias))
8608*94c894bbSVikram Hegde #define	VAL_CURR(array, x)	(strlen(array[x].pair_curr))
8609*94c894bbSVikram Hegde #define	SWAP(array, x, y)			\
8610*94c894bbSVikram Hegde {						\
8611*94c894bbSVikram Hegde 	alias_pair_t tmpair = array[x];		\
8612*94c894bbSVikram Hegde 	array[x] = array[y];			\
8613*94c894bbSVikram Hegde 	array[y] = tmpair;			\
8614*94c894bbSVikram Hegde }
8615*94c894bbSVikram Hegde 
8616*94c894bbSVikram Hegde static int
8617*94c894bbSVikram Hegde partition_curr(alias_pair_t *array, int start, int end)
8618*94c894bbSVikram Hegde {
8619*94c894bbSVikram Hegde 	int	i = start - 1;
8620*94c894bbSVikram Hegde 	int	j = end + 1;
8621*94c894bbSVikram Hegde 	int	pivot = start;
8622*94c894bbSVikram Hegde 
8623*94c894bbSVikram Hegde 	for (;;) {
8624*94c894bbSVikram Hegde 		do {
8625*94c894bbSVikram Hegde 			j--;
8626*94c894bbSVikram Hegde 		} while (VAL_CURR(array, j) > VAL_CURR(array, pivot));
8627*94c894bbSVikram Hegde 
8628*94c894bbSVikram Hegde 		do {
8629*94c894bbSVikram Hegde 			i++;
8630*94c894bbSVikram Hegde 		} while (VAL_CURR(array, i) < VAL_CURR(array, pivot));
8631*94c894bbSVikram Hegde 
8632*94c894bbSVikram Hegde 		if (i < j)
8633*94c894bbSVikram Hegde 			SWAP(array, i, j)
8634*94c894bbSVikram Hegde 		else
8635*94c894bbSVikram Hegde 			return (j);
8636*94c894bbSVikram Hegde 	}
8637*94c894bbSVikram Hegde }
8638*94c894bbSVikram Hegde 
8639*94c894bbSVikram Hegde static int
8640*94c894bbSVikram Hegde partition_aliases(alias_pair_t *array, int start, int end)
8641*94c894bbSVikram Hegde {
8642*94c894bbSVikram Hegde 	int	i = start - 1;
8643*94c894bbSVikram Hegde 	int	j = end + 1;
8644*94c894bbSVikram Hegde 	int	pivot = start;
8645*94c894bbSVikram Hegde 
8646*94c894bbSVikram Hegde 	for (;;) {
8647*94c894bbSVikram Hegde 		do {
8648*94c894bbSVikram Hegde 			j--;
8649*94c894bbSVikram Hegde 		} while (VAL_ALIAS(array, j) > VAL_ALIAS(array, pivot));
8650*94c894bbSVikram Hegde 
8651*94c894bbSVikram Hegde 		do {
8652*94c894bbSVikram Hegde 			i++;
8653*94c894bbSVikram Hegde 		} while (VAL_ALIAS(array, i) < VAL_ALIAS(array, pivot));
8654*94c894bbSVikram Hegde 
8655*94c894bbSVikram Hegde 		if (i < j)
8656*94c894bbSVikram Hegde 			SWAP(array, i, j)
8657*94c894bbSVikram Hegde 		else
8658*94c894bbSVikram Hegde 			return (j);
8659*94c894bbSVikram Hegde 	}
8660*94c894bbSVikram Hegde }
8661*94c894bbSVikram Hegde static void
8662*94c894bbSVikram Hegde sort_alias_pairs(alias_pair_t *array, int start, int end)
8663*94c894bbSVikram Hegde {
8664*94c894bbSVikram Hegde 	int mid;
8665*94c894bbSVikram Hegde 
8666*94c894bbSVikram Hegde 	if (start < end) {
8667*94c894bbSVikram Hegde 		mid = partition_aliases(array, start, end);
8668*94c894bbSVikram Hegde 		sort_alias_pairs(array, start, mid);
8669*94c894bbSVikram Hegde 		sort_alias_pairs(array, mid + 1, end);
8670*94c894bbSVikram Hegde 	}
8671*94c894bbSVikram Hegde }
8672*94c894bbSVikram Hegde 
8673*94c894bbSVikram Hegde static void
8674*94c894bbSVikram Hegde sort_curr_pairs(alias_pair_t *array, int start, int end)
8675*94c894bbSVikram Hegde {
8676*94c894bbSVikram Hegde 	int mid;
8677*94c894bbSVikram Hegde 
8678*94c894bbSVikram Hegde 	if (start < end) {
8679*94c894bbSVikram Hegde 		mid = partition_curr(array, start, end);
8680*94c894bbSVikram Hegde 		sort_curr_pairs(array, start, mid);
8681*94c894bbSVikram Hegde 		sort_curr_pairs(array, mid + 1, end);
8682*94c894bbSVikram Hegde 	}
8683*94c894bbSVikram Hegde }
8684*94c894bbSVikram Hegde 
8685*94c894bbSVikram Hegde static void
8686*94c894bbSVikram Hegde create_sorted_pairs(plat_alias_t *pali, int npali)
8687*94c894bbSVikram Hegde {
8688*94c894bbSVikram Hegde 	int		i;
8689*94c894bbSVikram Hegde 	int		j;
8690*94c894bbSVikram Hegde 	int		k;
8691*94c894bbSVikram Hegde 	int		count;
8692*94c894bbSVikram Hegde 
8693*94c894bbSVikram Hegde 	count = 0;
8694*94c894bbSVikram Hegde 	for (i = 0; i < npali; i++) {
8695*94c894bbSVikram Hegde 		count += pali[i].pali_naliases;
8696*94c894bbSVikram Hegde 	}
8697*94c894bbSVikram Hegde 
8698*94c894bbSVikram Hegde 	ddi_aliases.dali_alias_pairs = kmem_zalloc(
8699*94c894bbSVikram Hegde 	    (sizeof (alias_pair_t)) * count, KM_NOSLEEP);
8700*94c894bbSVikram Hegde 	if (ddi_aliases.dali_alias_pairs == NULL) {
8701*94c894bbSVikram Hegde 		cmn_err(CE_PANIC, "alias path-pair alloc failed");
8702*94c894bbSVikram Hegde 		/*NOTREACHED*/
8703*94c894bbSVikram Hegde 	}
8704*94c894bbSVikram Hegde 
8705*94c894bbSVikram Hegde 	ddi_aliases.dali_curr_pairs = kmem_zalloc(
8706*94c894bbSVikram Hegde 	    (sizeof (alias_pair_t)) * count, KM_NOSLEEP);
8707*94c894bbSVikram Hegde 	if (ddi_aliases.dali_curr_pairs == NULL) {
8708*94c894bbSVikram Hegde 		cmn_err(CE_PANIC, "curr path-pair alloc failed");
8709*94c894bbSVikram Hegde 		/*NOTREACHED*/
8710*94c894bbSVikram Hegde 	}
8711*94c894bbSVikram Hegde 
8712*94c894bbSVikram Hegde 	for (i = 0, k = 0; i < npali; i++) {
8713*94c894bbSVikram Hegde 		for (j = 0; j < pali[i].pali_naliases; j++, k++) {
8714*94c894bbSVikram Hegde 			ddi_aliases.dali_alias_pairs[k].pair_curr =
8715*94c894bbSVikram Hegde 			    ddi_aliases.dali_curr_pairs[k].pair_curr =
8716*94c894bbSVikram Hegde 			    pali[i].pali_current;
8717*94c894bbSVikram Hegde 			ddi_aliases.dali_alias_pairs[k].pair_alias =
8718*94c894bbSVikram Hegde 			    ddi_aliases.dali_curr_pairs[k].pair_alias =
8719*94c894bbSVikram Hegde 			    pali[i].pali_aliases[j];
8720*94c894bbSVikram Hegde 		}
8721*94c894bbSVikram Hegde 	}
8722*94c894bbSVikram Hegde 
8723*94c894bbSVikram Hegde 	ASSERT(k == count);
8724*94c894bbSVikram Hegde 
8725*94c894bbSVikram Hegde 	ddi_aliases.dali_num_pairs = count;
8726*94c894bbSVikram Hegde 
8727*94c894bbSVikram Hegde 	/* Now sort the array based on length of pair_alias */
8728*94c894bbSVikram Hegde 	sort_alias_pairs(ddi_aliases.dali_alias_pairs, 0, count - 1);
8729*94c894bbSVikram Hegde 	sort_curr_pairs(ddi_aliases.dali_curr_pairs, 0, count - 1);
8730*94c894bbSVikram Hegde }
8731*94c894bbSVikram Hegde 
8732*94c894bbSVikram Hegde void
8733*94c894bbSVikram Hegde ddi_register_aliases(plat_alias_t *pali, uint64_t npali)
8734*94c894bbSVikram Hegde {
8735*94c894bbSVikram Hegde 
8736*94c894bbSVikram Hegde 	ASSERT((pali == NULL) ^ (npali != 0));
8737*94c894bbSVikram Hegde 
8738*94c894bbSVikram Hegde 	if (npali == 0) {
8739*94c894bbSVikram Hegde 		ddi_err(DER_PANIC, NULL, "npali == 0");
8740*94c894bbSVikram Hegde 		/*NOTREACHED*/
8741*94c894bbSVikram Hegde 	}
8742*94c894bbSVikram Hegde 
8743*94c894bbSVikram Hegde 	if (ddi_aliases_present == B_TRUE) {
8744*94c894bbSVikram Hegde 		ddi_err(DER_PANIC, NULL, "multiple init");
8745*94c894bbSVikram Hegde 		/*NOTREACHED*/
8746*94c894bbSVikram Hegde 	}
8747*94c894bbSVikram Hegde 
8748*94c894bbSVikram Hegde 	ddi_aliases.dali_alias_TLB = mod_hash_create_strhash(
8749*94c894bbSVikram Hegde 	    "ddi-alias-tlb", DDI_ALIAS_HASH_SIZE, mod_hash_null_valdtor);
8750*94c894bbSVikram Hegde 	if (ddi_aliases.dali_alias_TLB == NULL) {
8751*94c894bbSVikram Hegde 		ddi_err(DER_PANIC, NULL, "alias TLB hash alloc failed");
8752*94c894bbSVikram Hegde 		/*NOTREACHED*/
8753*94c894bbSVikram Hegde 	}
8754*94c894bbSVikram Hegde 
8755*94c894bbSVikram Hegde 	ddi_aliases.dali_curr_TLB = mod_hash_create_strhash(
8756*94c894bbSVikram Hegde 	    "ddi-curr-tlb", DDI_ALIAS_HASH_SIZE, mod_hash_null_valdtor);
8757*94c894bbSVikram Hegde 	if (ddi_aliases.dali_curr_TLB == NULL) {
8758*94c894bbSVikram Hegde 		ddi_err(DER_PANIC, NULL, "curr TLB hash alloc failed");
8759*94c894bbSVikram Hegde 		/*NOTREACHED*/
8760*94c894bbSVikram Hegde 	}
8761*94c894bbSVikram Hegde 
8762*94c894bbSVikram Hegde 	create_sorted_pairs(pali, npali);
8763*94c894bbSVikram Hegde 
8764*94c894bbSVikram Hegde 	tsd_create(&tsd_ddi_redirect, NULL);
8765*94c894bbSVikram Hegde 
8766*94c894bbSVikram Hegde 	ddi_aliases_present = B_TRUE;
8767*94c894bbSVikram Hegde }
8768*94c894bbSVikram Hegde 
8769*94c894bbSVikram Hegde static dev_info_t *
8770*94c894bbSVikram Hegde path_to_dip(char *path)
8771*94c894bbSVikram Hegde {
8772*94c894bbSVikram Hegde 	dev_info_t	*currdip;
8773*94c894bbSVikram Hegde 	int		error;
8774*94c894bbSVikram Hegde 	char		*pdup;
8775*94c894bbSVikram Hegde 
8776*94c894bbSVikram Hegde 	pdup = ddi_strdup(path, KM_NOSLEEP);
8777*94c894bbSVikram Hegde 	if (pdup == NULL) {
8778*94c894bbSVikram Hegde 		cmn_err(CE_PANIC, "path strdup failed: %s", path);
8779*94c894bbSVikram Hegde 		/*NOTREACHED*/
8780*94c894bbSVikram Hegde 	}
8781*94c894bbSVikram Hegde 
8782*94c894bbSVikram Hegde 	error = resolve_pathname(pdup, &currdip, NULL, NULL);
8783*94c894bbSVikram Hegde 
8784*94c894bbSVikram Hegde 	kmem_free(pdup, strlen(path) + 1);
8785*94c894bbSVikram Hegde 
8786*94c894bbSVikram Hegde 	return (error ? NULL : currdip);
8787*94c894bbSVikram Hegde }
8788*94c894bbSVikram Hegde 
8789*94c894bbSVikram Hegde dev_info_t *
8790*94c894bbSVikram Hegde ddi_alias_to_currdip(char *alias, int i)
8791*94c894bbSVikram Hegde {
8792*94c894bbSVikram Hegde 	alias_pair_t *pair;
8793*94c894bbSVikram Hegde 	char *curr;
8794*94c894bbSVikram Hegde 	dev_info_t *currdip = NULL;
8795*94c894bbSVikram Hegde 	char *aliasdup;
8796*94c894bbSVikram Hegde 	int len;
8797*94c894bbSVikram Hegde 
8798*94c894bbSVikram Hegde 	pair = &(ddi_aliases.dali_alias_pairs[i]);
8799*94c894bbSVikram Hegde 	len = strlen(pair->pair_alias);
8800*94c894bbSVikram Hegde 
8801*94c894bbSVikram Hegde 	curr = NULL;
8802*94c894bbSVikram Hegde 	aliasdup = ddi_strdup(alias, KM_NOSLEEP);
8803*94c894bbSVikram Hegde 	if (aliasdup == NULL) {
8804*94c894bbSVikram Hegde 		cmn_err(CE_PANIC, "aliasdup alloc failed");
8805*94c894bbSVikram Hegde 		/*NOTREACHED*/
8806*94c894bbSVikram Hegde 	}
8807*94c894bbSVikram Hegde 
8808*94c894bbSVikram Hegde 	if (strncmp(alias, pair->pair_alias, len)  != 0)
8809*94c894bbSVikram Hegde 		goto out;
8810*94c894bbSVikram Hegde 
8811*94c894bbSVikram Hegde 	if (alias[len] != '/' && alias[len] != '\0')
8812*94c894bbSVikram Hegde 		goto out;
8813*94c894bbSVikram Hegde 
8814*94c894bbSVikram Hegde 
8815*94c894bbSVikram Hegde 	curr = kmem_alloc(MAXPATHLEN, KM_NOSLEEP);
8816*94c894bbSVikram Hegde 	if (curr == NULL) {
8817*94c894bbSVikram Hegde 		cmn_err(CE_PANIC, "curr alloc failed");
8818*94c894bbSVikram Hegde 		/*NOTREACHED*/
8819*94c894bbSVikram Hegde 	}
8820*94c894bbSVikram Hegde 	(void) strlcpy(curr, pair->pair_curr, MAXPATHLEN);
8821*94c894bbSVikram Hegde 	if (alias[len] == '/') {
8822*94c894bbSVikram Hegde 		(void) strlcat(curr, "/", MAXPATHLEN);
8823*94c894bbSVikram Hegde 		(void) strlcat(curr, &alias[len + 1], MAXPATHLEN);
8824*94c894bbSVikram Hegde 	}
8825*94c894bbSVikram Hegde 
8826*94c894bbSVikram Hegde 	currdip = path_to_dip(curr);
8827*94c894bbSVikram Hegde 
8828*94c894bbSVikram Hegde out:
8829*94c894bbSVikram Hegde 	if (currdip) {
8830*94c894bbSVikram Hegde 		(void) mod_hash_insert(ddi_aliases.dali_alias_TLB,
8831*94c894bbSVikram Hegde 		    (mod_hash_key_t)aliasdup, (mod_hash_val_t)curr);
8832*94c894bbSVikram Hegde 	} else {
8833*94c894bbSVikram Hegde 		(void) mod_hash_insert(ddi_aliases.dali_alias_TLB,
8834*94c894bbSVikram Hegde 		    (mod_hash_key_t)aliasdup, (mod_hash_val_t)NULL);
8835*94c894bbSVikram Hegde 		if (curr)
8836*94c894bbSVikram Hegde 			kmem_free(curr, MAXPATHLEN);
8837*94c894bbSVikram Hegde 	}
8838*94c894bbSVikram Hegde 
8839*94c894bbSVikram Hegde 	return (currdip);
8840*94c894bbSVikram Hegde }
8841*94c894bbSVikram Hegde 
8842*94c894bbSVikram Hegde char *
8843*94c894bbSVikram Hegde ddi_curr_to_alias(char *curr, int i)
8844*94c894bbSVikram Hegde {
8845*94c894bbSVikram Hegde 	alias_pair_t	*pair;
8846*94c894bbSVikram Hegde 	char		*alias;
8847*94c894bbSVikram Hegde 	char		*currdup;
8848*94c894bbSVikram Hegde 	int		len;
8849*94c894bbSVikram Hegde 
8850*94c894bbSVikram Hegde 	pair = &(ddi_aliases.dali_curr_pairs[i]);
8851*94c894bbSVikram Hegde 
8852*94c894bbSVikram Hegde 	len = strlen(pair->pair_curr);
8853*94c894bbSVikram Hegde 
8854*94c894bbSVikram Hegde 	alias = NULL;
8855*94c894bbSVikram Hegde 
8856*94c894bbSVikram Hegde 	currdup = ddi_strdup(curr, KM_NOSLEEP);
8857*94c894bbSVikram Hegde 	if (currdup == NULL) {
8858*94c894bbSVikram Hegde 		cmn_err(CE_PANIC, "currdup alloc failed");
8859*94c894bbSVikram Hegde 		/*NOTREACHED*/
8860*94c894bbSVikram Hegde 	}
8861*94c894bbSVikram Hegde 
8862*94c894bbSVikram Hegde 	if (strncmp(curr, pair->pair_curr, len) != 0)
8863*94c894bbSVikram Hegde 		goto out;
8864*94c894bbSVikram Hegde 
8865*94c894bbSVikram Hegde 	if (curr[len] != '/' && curr[len] != '\0')
8866*94c894bbSVikram Hegde 		goto out;
8867*94c894bbSVikram Hegde 
8868*94c894bbSVikram Hegde 	alias = kmem_alloc(MAXPATHLEN, KM_NOSLEEP);
8869*94c894bbSVikram Hegde 	if (alias == NULL) {
8870*94c894bbSVikram Hegde 		cmn_err(CE_PANIC, "alias alloc failed");
8871*94c894bbSVikram Hegde 		/*NOTREACHED*/
8872*94c894bbSVikram Hegde 	}
8873*94c894bbSVikram Hegde 
8874*94c894bbSVikram Hegde 	(void) strlcpy(alias, pair->pair_alias, MAXPATHLEN);
8875*94c894bbSVikram Hegde 	if (curr[len] == '/') {
8876*94c894bbSVikram Hegde 		(void) strlcat(alias, "/", MAXPATHLEN);
8877*94c894bbSVikram Hegde 		(void) strlcat(alias, &curr[len + 1], MAXPATHLEN);
8878*94c894bbSVikram Hegde 	}
8879*94c894bbSVikram Hegde 
8880*94c894bbSVikram Hegde 	if (e_ddi_path_to_instance(alias) == NULL) {
8881*94c894bbSVikram Hegde 		kmem_free(alias, MAXPATHLEN);
8882*94c894bbSVikram Hegde 		alias = NULL;
8883*94c894bbSVikram Hegde 	}
8884*94c894bbSVikram Hegde 
8885*94c894bbSVikram Hegde out:
8886*94c894bbSVikram Hegde 	(void) mod_hash_insert(ddi_aliases.dali_curr_TLB,
8887*94c894bbSVikram Hegde 	    (mod_hash_key_t)currdup, (mod_hash_val_t)alias);
8888*94c894bbSVikram Hegde 
8889*94c894bbSVikram Hegde 	return (alias);
8890*94c894bbSVikram Hegde }
8891*94c894bbSVikram Hegde 
8892*94c894bbSVikram Hegde dev_info_t *
8893*94c894bbSVikram Hegde ddi_alias_redirect(char *alias)
8894*94c894bbSVikram Hegde {
8895*94c894bbSVikram Hegde 	char		*curr;
8896*94c894bbSVikram Hegde 	char		*aliasdup;
8897*94c894bbSVikram Hegde 	dev_info_t	*currdip;
8898*94c894bbSVikram Hegde 	int		i;
8899*94c894bbSVikram Hegde 
8900*94c894bbSVikram Hegde 	if (ddi_aliases_present == B_FALSE)
8901*94c894bbSVikram Hegde 		return (NULL);
8902*94c894bbSVikram Hegde 
8903*94c894bbSVikram Hegde 	if (tsd_get(tsd_ddi_redirect))
8904*94c894bbSVikram Hegde 		return (NULL);
8905*94c894bbSVikram Hegde 
8906*94c894bbSVikram Hegde 	(void) tsd_set(tsd_ddi_redirect, (void *)1);
8907*94c894bbSVikram Hegde 
8908*94c894bbSVikram Hegde 	ASSERT(ddi_aliases.dali_alias_TLB);
8909*94c894bbSVikram Hegde 	ASSERT(ddi_aliases.dali_alias_pairs);
8910*94c894bbSVikram Hegde 
8911*94c894bbSVikram Hegde 	curr = NULL;
8912*94c894bbSVikram Hegde 	if (mod_hash_find(ddi_aliases.dali_alias_TLB,
8913*94c894bbSVikram Hegde 	    (mod_hash_key_t)alias, (mod_hash_val_t *)&curr) == 0) {
8914*94c894bbSVikram Hegde 		currdip = curr ? path_to_dip(curr) : NULL;
8915*94c894bbSVikram Hegde 		goto out;
8916*94c894bbSVikram Hegde 	}
8917*94c894bbSVikram Hegde 
8918*94c894bbSVikram Hegde 	aliasdup = ddi_strdup(alias, KM_NOSLEEP);
8919*94c894bbSVikram Hegde 	if (aliasdup == NULL) {
8920*94c894bbSVikram Hegde 		cmn_err(CE_PANIC, "aliasdup alloc failed");
8921*94c894bbSVikram Hegde 		/*NOTREACHED*/
8922*94c894bbSVikram Hegde 	}
8923*94c894bbSVikram Hegde 
8924*94c894bbSVikram Hegde 	/* The TLB has no translation, do it the hard way */
8925*94c894bbSVikram Hegde 	currdip = NULL;
8926*94c894bbSVikram Hegde 	for (i = ddi_aliases.dali_num_pairs - 1; i >= 0; i--) {
8927*94c894bbSVikram Hegde 		currdip = ddi_alias_to_currdip(alias, i);
8928*94c894bbSVikram Hegde 		if (currdip)
8929*94c894bbSVikram Hegde 			break;
8930*94c894bbSVikram Hegde 	}
8931*94c894bbSVikram Hegde out:
8932*94c894bbSVikram Hegde 	(void) tsd_set(tsd_ddi_redirect, NULL);
8933*94c894bbSVikram Hegde 
8934*94c894bbSVikram Hegde 	return (currdip);
8935*94c894bbSVikram Hegde }
8936*94c894bbSVikram Hegde 
8937*94c894bbSVikram Hegde char *
8938*94c894bbSVikram Hegde ddi_curr_redirect(char *curr)
8939*94c894bbSVikram Hegde {
8940*94c894bbSVikram Hegde 	char 	*alias;
8941*94c894bbSVikram Hegde 	int i;
8942*94c894bbSVikram Hegde 
8943*94c894bbSVikram Hegde 	if (ddi_aliases_present == B_FALSE)
8944*94c894bbSVikram Hegde 		return (NULL);
8945*94c894bbSVikram Hegde 
8946*94c894bbSVikram Hegde 	if (tsd_get(tsd_ddi_redirect))
8947*94c894bbSVikram Hegde 		return (NULL);
8948*94c894bbSVikram Hegde 
8949*94c894bbSVikram Hegde 	(void) tsd_set(tsd_ddi_redirect, (void *)1);
8950*94c894bbSVikram Hegde 
8951*94c894bbSVikram Hegde 	ASSERT(ddi_aliases.dali_curr_TLB);
8952*94c894bbSVikram Hegde 	ASSERT(ddi_aliases.dali_curr_pairs);
8953*94c894bbSVikram Hegde 
8954*94c894bbSVikram Hegde 	alias = NULL;
8955*94c894bbSVikram Hegde 	if (mod_hash_find(ddi_aliases.dali_curr_TLB,
8956*94c894bbSVikram Hegde 	    (mod_hash_key_t)curr, (mod_hash_val_t *)&alias) == 0) {
8957*94c894bbSVikram Hegde 		goto out;
8958*94c894bbSVikram Hegde 	}
8959*94c894bbSVikram Hegde 
8960*94c894bbSVikram Hegde 
8961*94c894bbSVikram Hegde 	/* The TLB has no translation, do it the slow way */
8962*94c894bbSVikram Hegde 	alias = NULL;
8963*94c894bbSVikram Hegde 	for (i = ddi_aliases.dali_num_pairs - 1; i >= 0; i--) {
8964*94c894bbSVikram Hegde 		alias = ddi_curr_to_alias(curr, i);
8965*94c894bbSVikram Hegde 		if (alias)
8966*94c894bbSVikram Hegde 			break;
8967*94c894bbSVikram Hegde 	}
8968*94c894bbSVikram Hegde 
8969*94c894bbSVikram Hegde out:
8970*94c894bbSVikram Hegde 	(void) tsd_set(tsd_ddi_redirect, NULL);
8971*94c894bbSVikram Hegde 
8972*94c894bbSVikram Hegde 	return (alias);
8973*94c894bbSVikram Hegde }
8974*94c894bbSVikram Hegde 
89753a634bfcSVikram Hegde void
89763a634bfcSVikram Hegde ddi_err(ddi_err_t ade, dev_info_t *rdip, const char *fmt, ...)
89773a634bfcSVikram Hegde {
89783a634bfcSVikram Hegde 	va_list ap;
89793a634bfcSVikram Hegde 	char strbuf[256];
89803a634bfcSVikram Hegde 	char *buf;
89813a634bfcSVikram Hegde 	size_t buflen, tlen;
89823a634bfcSVikram Hegde 	int ce;
89833a634bfcSVikram Hegde 	int de;
89843a634bfcSVikram Hegde 	const char *fmtbad = "Invalid arguments to ddi_err()";
89853a634bfcSVikram Hegde 
89863a634bfcSVikram Hegde 	de = DER_CONT;
89873a634bfcSVikram Hegde 	strbuf[1] = '\0';
89883a634bfcSVikram Hegde 
89893a634bfcSVikram Hegde 	switch (ade) {
89903a634bfcSVikram Hegde 	case DER_CONS:
89913a634bfcSVikram Hegde 		strbuf[0] = '^';
89923a634bfcSVikram Hegde 		break;
89933a634bfcSVikram Hegde 	case DER_LOG:
89943a634bfcSVikram Hegde 		strbuf[0] = '!';
89953a634bfcSVikram Hegde 		break;
89963a634bfcSVikram Hegde 	case DER_VERB:
89973a634bfcSVikram Hegde 		strbuf[0] = '?';
89983a634bfcSVikram Hegde 		break;
89993a634bfcSVikram Hegde 	default:
90003a634bfcSVikram Hegde 		strbuf[0] = '\0';
90013a634bfcSVikram Hegde 		de = ade;
90023a634bfcSVikram Hegde 		break;
90033a634bfcSVikram Hegde 	}
90043a634bfcSVikram Hegde 
90053a634bfcSVikram Hegde 	tlen = strlen(strbuf);
90063a634bfcSVikram Hegde 	buf = strbuf + tlen;
90073a634bfcSVikram Hegde 	buflen = sizeof (strbuf) - tlen;
90083a634bfcSVikram Hegde 
90093a634bfcSVikram Hegde 	if (rdip && ddi_get_instance(rdip) == -1) {
90103a634bfcSVikram Hegde 		(void) snprintf(buf, buflen, "%s: ",
90113a634bfcSVikram Hegde 		    ddi_driver_name(rdip));
90123a634bfcSVikram Hegde 	} else if (rdip) {
90133a634bfcSVikram Hegde 		(void) snprintf(buf, buflen, "%s%d: ",
90143a634bfcSVikram Hegde 		    ddi_driver_name(rdip), ddi_get_instance(rdip));
90153a634bfcSVikram Hegde 	}
90163a634bfcSVikram Hegde 
90173a634bfcSVikram Hegde 	tlen = strlen(strbuf);
90183a634bfcSVikram Hegde 	buf = strbuf + tlen;
90193a634bfcSVikram Hegde 	buflen = sizeof (strbuf) - tlen;
90203a634bfcSVikram Hegde 
90213a634bfcSVikram Hegde 	va_start(ap, fmt);
90223a634bfcSVikram Hegde 	switch (de) {
90233a634bfcSVikram Hegde 	case DER_CONT:
90243a634bfcSVikram Hegde 		(void) vsnprintf(buf, buflen, fmt, ap);
90253a634bfcSVikram Hegde 		if (ade != DER_CONT) {
90263a634bfcSVikram Hegde 			(void) strlcat(strbuf, "\n", sizeof (strbuf));
90273a634bfcSVikram Hegde 		}
90283a634bfcSVikram Hegde 		ce = CE_CONT;
90293a634bfcSVikram Hegde 		break;
90303a634bfcSVikram Hegde 	case DER_NOTE:
90313a634bfcSVikram Hegde 		(void) vsnprintf(buf, buflen, fmt, ap);
90323a634bfcSVikram Hegde 		ce = CE_NOTE;
90333a634bfcSVikram Hegde 		break;
90343a634bfcSVikram Hegde 	case DER_WARN:
90353a634bfcSVikram Hegde 		(void) vsnprintf(buf, buflen, fmt, ap);
90363a634bfcSVikram Hegde 		ce = CE_WARN;
90373a634bfcSVikram Hegde 		break;
90383a634bfcSVikram Hegde 	case DER_MODE:
90393a634bfcSVikram Hegde 		(void) vsnprintf(buf, buflen, fmt, ap);
90403a634bfcSVikram Hegde 		if (ddi_err_panic == B_TRUE) {
90413a634bfcSVikram Hegde 			ce = CE_PANIC;
90423a634bfcSVikram Hegde 		} else {
90433a634bfcSVikram Hegde 			ce = CE_WARN;
90443a634bfcSVikram Hegde 		}
90453a634bfcSVikram Hegde 		break;
90463a634bfcSVikram Hegde 	case DER_DEBUG:
90473a634bfcSVikram Hegde 		(void) snprintf(buf, buflen, "DEBUG: ");
90483a634bfcSVikram Hegde 		tlen = strlen("DEBUG: ");
90493a634bfcSVikram Hegde 		(void) vsnprintf(buf + tlen, buflen - tlen, fmt, ap);
90503a634bfcSVikram Hegde 		ce = CE_CONT;
90513a634bfcSVikram Hegde 		break;
90523a634bfcSVikram Hegde 	case DER_PANIC:
90533a634bfcSVikram Hegde 		(void) vsnprintf(buf, buflen, fmt, ap);
90543a634bfcSVikram Hegde 		ce = CE_PANIC;
90553a634bfcSVikram Hegde 		break;
90563a634bfcSVikram Hegde 	case DER_INVALID:
90573a634bfcSVikram Hegde 	default:
90583a634bfcSVikram Hegde 		(void) snprintf(buf, buflen, fmtbad);
90593a634bfcSVikram Hegde 		tlen = strlen(fmtbad);
90603a634bfcSVikram Hegde 		(void) vsnprintf(buf + tlen, buflen - tlen, fmt, ap);
90613a634bfcSVikram Hegde 		ce = CE_PANIC;
90623a634bfcSVikram Hegde 		break;
90633a634bfcSVikram Hegde 	}
90643a634bfcSVikram Hegde 	va_end(ap);
90653a634bfcSVikram Hegde 
90663a634bfcSVikram Hegde 	cmn_err(ce, strbuf);
90673a634bfcSVikram Hegde }
90683a634bfcSVikram Hegde 
90693a634bfcSVikram Hegde /*ARGSUSED*/
90703a634bfcSVikram Hegde void
90713a634bfcSVikram Hegde ddi_mem_update(uint64_t addr, uint64_t size)
90723a634bfcSVikram Hegde {
90733a634bfcSVikram Hegde #if defined(__x86) && !defined(__xpv)
90743a634bfcSVikram Hegde 	extern void immu_physmem_update(uint64_t addr, uint64_t size);
90753a634bfcSVikram Hegde 	immu_physmem_update(addr, size);
90763a634bfcSVikram Hegde #else
90773a634bfcSVikram Hegde 	/*LINTED*/
90783a634bfcSVikram Hegde 	;
90793a634bfcSVikram Hegde #endif
90803a634bfcSVikram Hegde }
9081