1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/note.h>
30*0Sstevel@tonic-gate #include <sys/t_lock.h>
31*0Sstevel@tonic-gate #include <sys/cmn_err.h>
32*0Sstevel@tonic-gate #include <sys/instance.h>
33*0Sstevel@tonic-gate #include <sys/conf.h>
34*0Sstevel@tonic-gate #include <sys/stat.h>
35*0Sstevel@tonic-gate #include <sys/ddi.h>
36*0Sstevel@tonic-gate #include <sys/hwconf.h>
37*0Sstevel@tonic-gate #include <sys/sunddi.h>
38*0Sstevel@tonic-gate #include <sys/sunndi.h>
39*0Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
40*0Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
41*0Sstevel@tonic-gate #include <sys/modctl.h>
42*0Sstevel@tonic-gate #include <sys/dacf.h>
43*0Sstevel@tonic-gate #include <sys/promif.h>
44*0Sstevel@tonic-gate #include <sys/cpuvar.h>
45*0Sstevel@tonic-gate #include <sys/pathname.h>
46*0Sstevel@tonic-gate #include <sys/taskq.h>
47*0Sstevel@tonic-gate #include <sys/sysevent.h>
48*0Sstevel@tonic-gate #include <sys/sunmdi.h>
49*0Sstevel@tonic-gate #include <sys/stream.h>
50*0Sstevel@tonic-gate #include <sys/strsubr.h>
51*0Sstevel@tonic-gate #include <sys/fs/snode.h>
52*0Sstevel@tonic-gate #include <sys/fs/dv_node.h>
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate #ifdef DEBUG
55*0Sstevel@tonic-gate int ddidebug = DDI_AUDIT;
56*0Sstevel@tonic-gate #else
57*0Sstevel@tonic-gate int ddidebug = 0;
58*0Sstevel@tonic-gate #endif
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate #define	MT_CONFIG_OP	0
61*0Sstevel@tonic-gate #define	MT_UNCONFIG_OP	1
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate /* Multi-threaded configuration */
64*0Sstevel@tonic-gate struct mt_config_handle {
65*0Sstevel@tonic-gate 	kmutex_t mtc_lock;
66*0Sstevel@tonic-gate 	kcondvar_t mtc_cv;
67*0Sstevel@tonic-gate 	int mtc_thr_count;
68*0Sstevel@tonic-gate 	dev_info_t *mtc_pdip;	/* parent dip for mt_config_children */
69*0Sstevel@tonic-gate 	dev_info_t **mtc_fdip;	/* "a" dip where unconfigure failed */
70*0Sstevel@tonic-gate 	major_t mtc_parmajor;	/* parent major for mt_config_driver */
71*0Sstevel@tonic-gate 	major_t mtc_major;
72*0Sstevel@tonic-gate 	int mtc_flags;
73*0Sstevel@tonic-gate 	int mtc_op;		/* config or unconfig */
74*0Sstevel@tonic-gate 	int mtc_error;		/* operation error */
75*0Sstevel@tonic-gate 	struct brevq_node **mtc_brevqp;	/* outstanding branch events queue */
76*0Sstevel@tonic-gate #ifdef DEBUG
77*0Sstevel@tonic-gate 	int total_time;
78*0Sstevel@tonic-gate 	timestruc_t start_time;
79*0Sstevel@tonic-gate #endif /* DEBUG */
80*0Sstevel@tonic-gate };
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate struct devi_nodeid {
83*0Sstevel@tonic-gate 	dnode_t nodeid;
84*0Sstevel@tonic-gate 	dev_info_t *dip;
85*0Sstevel@tonic-gate 	struct devi_nodeid *next;
86*0Sstevel@tonic-gate };
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate struct devi_nodeid_list {
89*0Sstevel@tonic-gate 	kmutex_t dno_lock;		/* Protects other fields */
90*0Sstevel@tonic-gate 	struct devi_nodeid *dno_head;	/* list of devi nodeid elements */
91*0Sstevel@tonic-gate 	struct devi_nodeid *dno_free;	/* Free list */
92*0Sstevel@tonic-gate 	uint_t dno_list_length;		/* number of dips in list */
93*0Sstevel@tonic-gate };
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate /* used to keep track of branch remove events to be generated */
96*0Sstevel@tonic-gate struct brevq_node {
97*0Sstevel@tonic-gate 	char *deviname;
98*0Sstevel@tonic-gate 	struct brevq_node *sibling;
99*0Sstevel@tonic-gate 	struct brevq_node *child;
100*0Sstevel@tonic-gate };
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate static struct devi_nodeid_list devi_nodeid_list;
103*0Sstevel@tonic-gate static struct devi_nodeid_list *devimap = &devi_nodeid_list;
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate /*
106*0Sstevel@tonic-gate  * Well known nodes which are attached first at boot time.
107*0Sstevel@tonic-gate  */
108*0Sstevel@tonic-gate dev_info_t *top_devinfo;		/* root of device tree */
109*0Sstevel@tonic-gate dev_info_t *options_dip;
110*0Sstevel@tonic-gate dev_info_t *pseudo_dip;
111*0Sstevel@tonic-gate dev_info_t *clone_dip;
112*0Sstevel@tonic-gate dev_info_t *scsi_vhci_dip;		/* MPXIO dip */
113*0Sstevel@tonic-gate major_t clone_major;
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate /* block all future dev_info state changes */
116*0Sstevel@tonic-gate static hrtime_t volatile devinfo_freeze = 0;
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate /* number of dev_info attaches/detaches currently in progress */
119*0Sstevel@tonic-gate static ulong_t devinfo_attach_detach = 0;
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate extern kmutex_t global_vhci_lock;
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate /*
124*0Sstevel@tonic-gate  * The devinfo snapshot cache and related variables.
125*0Sstevel@tonic-gate  * The only field in the di_cache structure that needs initialization
126*0Sstevel@tonic-gate  * is the mutex (cache_lock). However, since this is an adaptive mutex
127*0Sstevel@tonic-gate  * (MUTEX_DEFAULT) - it is automatically initialized by being allocated
128*0Sstevel@tonic-gate  * in zeroed memory (static storage class). Therefore no explicit
129*0Sstevel@tonic-gate  * initialization of the di_cache structure is needed.
130*0Sstevel@tonic-gate  */
131*0Sstevel@tonic-gate struct di_cache	di_cache = {1};
132*0Sstevel@tonic-gate int		di_cache_debug = 0;
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate /* For ddvis, which needs pseudo children under PCI */
135*0Sstevel@tonic-gate int pci_allow_pseudo_children = 0;
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate /*
138*0Sstevel@tonic-gate  * The following switch is for service people, in case a
139*0Sstevel@tonic-gate  * 3rd party driver depends on identify(9e) being called.
140*0Sstevel@tonic-gate  */
141*0Sstevel@tonic-gate int identify_9e = 0;
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate int mtc_off;					/* turn off mt config */
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate static kmem_cache_t *ddi_node_cache;		/* devinfo node cache */
146*0Sstevel@tonic-gate static devinfo_log_header_t *devinfo_audit_log;	/* devinfo log */
147*0Sstevel@tonic-gate static int devinfo_log_size;			/* size in pages */
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate static int lookup_compatible(dev_info_t *, uint_t);
150*0Sstevel@tonic-gate static char *encode_composite_string(char **, uint_t, size_t *, uint_t);
151*0Sstevel@tonic-gate static void link_to_driver_list(dev_info_t *);
152*0Sstevel@tonic-gate static void unlink_from_driver_list(dev_info_t *);
153*0Sstevel@tonic-gate static void add_to_dn_list(struct devnames *, dev_info_t *);
154*0Sstevel@tonic-gate static void remove_from_dn_list(struct devnames *, dev_info_t *);
155*0Sstevel@tonic-gate static dev_info_t *find_child_by_callback(dev_info_t *, char *, char *,
156*0Sstevel@tonic-gate     int (*)(dev_info_t *, char *, int));
157*0Sstevel@tonic-gate static dev_info_t *find_duplicate_child();
158*0Sstevel@tonic-gate static void add_global_props(dev_info_t *);
159*0Sstevel@tonic-gate static void remove_global_props(dev_info_t *);
160*0Sstevel@tonic-gate static int uninit_node(dev_info_t *);
161*0Sstevel@tonic-gate static void da_log_init(void);
162*0Sstevel@tonic-gate static void da_log_enter(dev_info_t *);
163*0Sstevel@tonic-gate static int walk_devs(dev_info_t *, int (*f)(dev_info_t *, void *), void *, int);
164*0Sstevel@tonic-gate static int reset_nexus_flags(dev_info_t *, void *);
165*0Sstevel@tonic-gate static void ddi_optimize_dtree(dev_info_t *);
166*0Sstevel@tonic-gate static int is_leaf_node(dev_info_t *);
167*0Sstevel@tonic-gate static struct mt_config_handle *mt_config_init(dev_info_t *, dev_info_t **,
168*0Sstevel@tonic-gate     int, major_t, int, struct brevq_node **);
169*0Sstevel@tonic-gate static void mt_config_children(struct mt_config_handle *);
170*0Sstevel@tonic-gate static void mt_config_driver(struct mt_config_handle *);
171*0Sstevel@tonic-gate static int mt_config_fini(struct mt_config_handle *);
172*0Sstevel@tonic-gate static int devi_unconfig_common(dev_info_t *, dev_info_t **, int, major_t,
173*0Sstevel@tonic-gate     struct brevq_node **);
174*0Sstevel@tonic-gate static int
175*0Sstevel@tonic-gate ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
176*0Sstevel@tonic-gate     dev_info_t **childp, int flags);
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate /*
179*0Sstevel@tonic-gate  * dev_info cache and node management
180*0Sstevel@tonic-gate  */
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate /* initialize dev_info node cache */
183*0Sstevel@tonic-gate void
184*0Sstevel@tonic-gate i_ddi_node_cache_init()
185*0Sstevel@tonic-gate {
186*0Sstevel@tonic-gate 	ASSERT(ddi_node_cache == NULL);
187*0Sstevel@tonic-gate 	ddi_node_cache = kmem_cache_create("dev_info_node_cache",
188*0Sstevel@tonic-gate 	    sizeof (struct dev_info), 0, NULL, NULL, NULL, NULL, NULL, 0);
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 	if (ddidebug & DDI_AUDIT)
191*0Sstevel@tonic-gate 		da_log_init();
192*0Sstevel@tonic-gate }
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate /*
195*0Sstevel@tonic-gate  * Allocating a dev_info node, callable from interrupt context with KM_NOSLEEP
196*0Sstevel@tonic-gate  * The allocated node has a reference count of 0.
197*0Sstevel@tonic-gate  */
198*0Sstevel@tonic-gate dev_info_t *
199*0Sstevel@tonic-gate i_ddi_alloc_node(dev_info_t *pdip, char *node_name, dnode_t nodeid,
200*0Sstevel@tonic-gate     int instance, ddi_prop_t *sys_prop, int flag)
201*0Sstevel@tonic-gate {
202*0Sstevel@tonic-gate 	struct dev_info *devi;
203*0Sstevel@tonic-gate 	struct devi_nodeid *elem;
204*0Sstevel@tonic-gate 	static char failed[] = "i_ddi_alloc_node: out of memory";
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 	ASSERT(node_name != NULL);
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 	if ((devi = kmem_cache_alloc(ddi_node_cache, flag)) == NULL) {
209*0Sstevel@tonic-gate 		cmn_err(CE_NOTE, failed);
210*0Sstevel@tonic-gate 		return (NULL);
211*0Sstevel@tonic-gate 	}
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate 	bzero(devi, sizeof (struct dev_info));
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate 	if (devinfo_audit_log) {
216*0Sstevel@tonic-gate 		devi->devi_audit = kmem_zalloc(sizeof (devinfo_audit_t), flag);
217*0Sstevel@tonic-gate 		if (devi->devi_audit == NULL)
218*0Sstevel@tonic-gate 			goto fail;
219*0Sstevel@tonic-gate 	}
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 	if ((devi->devi_node_name = i_ddi_strdup(node_name, flag)) == NULL)
222*0Sstevel@tonic-gate 		goto fail;
223*0Sstevel@tonic-gate 	/* default binding name is node name */
224*0Sstevel@tonic-gate 	devi->devi_binding_name = devi->devi_node_name;
225*0Sstevel@tonic-gate 	devi->devi_major = (major_t)-1;	/* unbound by default */
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 	/*
228*0Sstevel@tonic-gate 	 * Make a copy of system property
229*0Sstevel@tonic-gate 	 */
230*0Sstevel@tonic-gate 	if (sys_prop &&
231*0Sstevel@tonic-gate 	    (devi->devi_sys_prop_ptr = i_ddi_prop_list_dup(sys_prop, flag))
232*0Sstevel@tonic-gate 	    == NULL)
233*0Sstevel@tonic-gate 		goto fail;
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 	/*
236*0Sstevel@tonic-gate 	 * Assign devi_nodeid, devi_node_class, devi_node_attributes
237*0Sstevel@tonic-gate 	 * according to the following algorithm:
238*0Sstevel@tonic-gate 	 *
239*0Sstevel@tonic-gate 	 * nodeid arg			node class		node attributes
240*0Sstevel@tonic-gate 	 *
241*0Sstevel@tonic-gate 	 * DEVI_PSEUDO_NODEID		DDI_NC_PSEUDO		A
242*0Sstevel@tonic-gate 	 * DEVI_SID_NODEID		DDI_NC_PSEUDO		A,P
243*0Sstevel@tonic-gate 	 * other			DDI_NC_PROM		P
244*0Sstevel@tonic-gate 	 *
245*0Sstevel@tonic-gate 	 * Where A = DDI_AUTO_ASSIGNED_NODEID (auto-assign a nodeid)
246*0Sstevel@tonic-gate 	 * and	 P = DDI_PERSISTENT
247*0Sstevel@tonic-gate 	 *
248*0Sstevel@tonic-gate 	 * auto-assigned nodeids are also auto-freed.
249*0Sstevel@tonic-gate 	 */
250*0Sstevel@tonic-gate 	switch (nodeid) {
251*0Sstevel@tonic-gate 	case DEVI_SID_NODEID:
252*0Sstevel@tonic-gate 		devi->devi_node_attributes = DDI_PERSISTENT;
253*0Sstevel@tonic-gate 		if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL)
254*0Sstevel@tonic-gate 			goto fail;
255*0Sstevel@tonic-gate 		/*FALLTHROUGH*/
256*0Sstevel@tonic-gate 	case DEVI_PSEUDO_NODEID:
257*0Sstevel@tonic-gate 		devi->devi_node_attributes |= DDI_AUTO_ASSIGNED_NODEID;
258*0Sstevel@tonic-gate 		devi->devi_node_class = DDI_NC_PSEUDO;
259*0Sstevel@tonic-gate 		if (impl_ddi_alloc_nodeid(&devi->devi_nodeid)) {
260*0Sstevel@tonic-gate 			panic("i_ddi_alloc_node: out of nodeids");
261*0Sstevel@tonic-gate 			/*NOTREACHED*/
262*0Sstevel@tonic-gate 		}
263*0Sstevel@tonic-gate 		break;
264*0Sstevel@tonic-gate 	default:
265*0Sstevel@tonic-gate 		if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL)
266*0Sstevel@tonic-gate 			goto fail;
267*0Sstevel@tonic-gate 		/*
268*0Sstevel@tonic-gate 		 * the nodetype is 'prom', try to 'take' the nodeid now.
269*0Sstevel@tonic-gate 		 * This requires memory allocation, so check for failure.
270*0Sstevel@tonic-gate 		 */
271*0Sstevel@tonic-gate 		if (impl_ddi_take_nodeid(nodeid, flag) != 0) {
272*0Sstevel@tonic-gate 			kmem_free(elem, sizeof (*elem));
273*0Sstevel@tonic-gate 			goto fail;
274*0Sstevel@tonic-gate 		}
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate 		devi->devi_nodeid = nodeid;
277*0Sstevel@tonic-gate 		devi->devi_node_class = DDI_NC_PROM;
278*0Sstevel@tonic-gate 		devi->devi_node_attributes = DDI_PERSISTENT;
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate 	}
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node((dev_info_t *)devi)) {
283*0Sstevel@tonic-gate 		mutex_enter(&devimap->dno_lock);
284*0Sstevel@tonic-gate 		elem->next = devimap->dno_free;
285*0Sstevel@tonic-gate 		devimap->dno_free = elem;
286*0Sstevel@tonic-gate 		mutex_exit(&devimap->dno_lock);
287*0Sstevel@tonic-gate 	}
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate 	/*
290*0Sstevel@tonic-gate 	 * Instance is normally initialized to -1. In a few special
291*0Sstevel@tonic-gate 	 * cases, the caller may specify an instance (e.g. CPU nodes).
292*0Sstevel@tonic-gate 	 */
293*0Sstevel@tonic-gate 	devi->devi_instance = instance;
294*0Sstevel@tonic-gate 
295*0Sstevel@tonic-gate 	/*
296*0Sstevel@tonic-gate 	 * set parent and bus_ctl parent
297*0Sstevel@tonic-gate 	 */
298*0Sstevel@tonic-gate 	devi->devi_parent = DEVI(pdip);
299*0Sstevel@tonic-gate 	devi->devi_bus_ctl = DEVI(pdip);
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
302*0Sstevel@tonic-gate 	    "i_ddi_alloc_node: name=%s id=%d\n", node_name, devi->devi_nodeid));
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 	cv_init(&(devi->devi_cv), NULL, CV_DEFAULT, NULL);
305*0Sstevel@tonic-gate 	mutex_init(&(devi->devi_lock), NULL, MUTEX_DEFAULT, NULL);
306*0Sstevel@tonic-gate 	mutex_init(&(devi->devi_pm_lock), NULL, MUTEX_DEFAULT, NULL);
307*0Sstevel@tonic-gate 	mutex_init(&(devi->devi_pm_busy_lock), NULL, MUTEX_DEFAULT, NULL);
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate 	i_ddi_set_node_state((dev_info_t *)devi, DS_PROTO);
310*0Sstevel@tonic-gate 	da_log_enter((dev_info_t *)devi);
311*0Sstevel@tonic-gate 	return ((dev_info_t *)devi);
312*0Sstevel@tonic-gate 
313*0Sstevel@tonic-gate fail:
314*0Sstevel@tonic-gate 	if (devi->devi_sys_prop_ptr)
315*0Sstevel@tonic-gate 		i_ddi_prop_list_delete(devi->devi_sys_prop_ptr);
316*0Sstevel@tonic-gate 	if (devi->devi_node_name)
317*0Sstevel@tonic-gate 		kmem_free(devi->devi_node_name, strlen(node_name) + 1);
318*0Sstevel@tonic-gate 	if (devi->devi_audit)
319*0Sstevel@tonic-gate 		kmem_free(devi->devi_audit, sizeof (devinfo_audit_t));
320*0Sstevel@tonic-gate 	kmem_cache_free(ddi_node_cache, devi);
321*0Sstevel@tonic-gate 	cmn_err(CE_NOTE, failed);
322*0Sstevel@tonic-gate 	return (NULL);
323*0Sstevel@tonic-gate }
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate /*
326*0Sstevel@tonic-gate  * free a dev_info structure.
327*0Sstevel@tonic-gate  * NB. Not callable from interrupt since impl_ddi_free_nodeid may block.
328*0Sstevel@tonic-gate  */
329*0Sstevel@tonic-gate void
330*0Sstevel@tonic-gate i_ddi_free_node(dev_info_t *dip)
331*0Sstevel@tonic-gate {
332*0Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
333*0Sstevel@tonic-gate 	struct devi_nodeid *elem;
334*0Sstevel@tonic-gate 
335*0Sstevel@tonic-gate 	ASSERT(devi->devi_ref == 0);
336*0Sstevel@tonic-gate 	ASSERT(devi->devi_addr == NULL);
337*0Sstevel@tonic-gate 	ASSERT(devi->devi_node_state == DS_PROTO);
338*0Sstevel@tonic-gate 	ASSERT(devi->devi_child == NULL);
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate 	if (devi->devi_intr_p)
341*0Sstevel@tonic-gate 		i_ddi_intr_devi_fini((dev_info_t *)devi);
342*0Sstevel@tonic-gate 
343*0Sstevel@tonic-gate 	/* free devi_addr */
344*0Sstevel@tonic-gate 	ddi_set_name_addr(dip, NULL);
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate 	if (i_ndi_dev_is_auto_assigned_node(dip))
347*0Sstevel@tonic-gate 		impl_ddi_free_nodeid(DEVI(dip)->devi_nodeid);
348*0Sstevel@tonic-gate 
349*0Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
350*0Sstevel@tonic-gate 		mutex_enter(&devimap->dno_lock);
351*0Sstevel@tonic-gate 		ASSERT(devimap->dno_free);
352*0Sstevel@tonic-gate 		elem = devimap->dno_free;
353*0Sstevel@tonic-gate 		devimap->dno_free = elem->next;
354*0Sstevel@tonic-gate 		mutex_exit(&devimap->dno_lock);
355*0Sstevel@tonic-gate 		kmem_free(elem, sizeof (*elem));
356*0Sstevel@tonic-gate 	}
357*0Sstevel@tonic-gate 
358*0Sstevel@tonic-gate 	if (DEVI(dip)->devi_compat_names)
359*0Sstevel@tonic-gate 		kmem_free(DEVI(dip)->devi_compat_names,
360*0Sstevel@tonic-gate 		    DEVI(dip)->devi_compat_length);
361*0Sstevel@tonic-gate 
362*0Sstevel@tonic-gate 	ddi_prop_remove_all(dip);	/* remove driver properties */
363*0Sstevel@tonic-gate 	if (devi->devi_sys_prop_ptr)
364*0Sstevel@tonic-gate 		i_ddi_prop_list_delete(devi->devi_sys_prop_ptr);
365*0Sstevel@tonic-gate 	if (devi->devi_hw_prop_ptr)
366*0Sstevel@tonic-gate 		i_ddi_prop_list_delete(devi->devi_hw_prop_ptr);
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_INVAL);
369*0Sstevel@tonic-gate 	da_log_enter(dip);
370*0Sstevel@tonic-gate 	if (devi->devi_audit) {
371*0Sstevel@tonic-gate 		kmem_free(devi->devi_audit, sizeof (devinfo_audit_t));
372*0Sstevel@tonic-gate 	}
373*0Sstevel@tonic-gate 	kmem_free(devi->devi_node_name, strlen(devi->devi_node_name) + 1);
374*0Sstevel@tonic-gate 	if (devi->devi_device_class)
375*0Sstevel@tonic-gate 		kmem_free(devi->devi_device_class,
376*0Sstevel@tonic-gate 		    strlen(devi->devi_device_class) + 1);
377*0Sstevel@tonic-gate 	cv_destroy(&(devi->devi_cv));
378*0Sstevel@tonic-gate 	mutex_destroy(&(devi->devi_lock));
379*0Sstevel@tonic-gate 	mutex_destroy(&(devi->devi_pm_lock));
380*0Sstevel@tonic-gate 	mutex_destroy(&(devi->devi_pm_busy_lock));
381*0Sstevel@tonic-gate 
382*0Sstevel@tonic-gate 	kmem_cache_free(ddi_node_cache, devi);
383*0Sstevel@tonic-gate }
384*0Sstevel@tonic-gate 
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate /*
387*0Sstevel@tonic-gate  * Node state transitions
388*0Sstevel@tonic-gate  */
389*0Sstevel@tonic-gate 
390*0Sstevel@tonic-gate /*
391*0Sstevel@tonic-gate  * Change the node name
392*0Sstevel@tonic-gate  */
393*0Sstevel@tonic-gate int
394*0Sstevel@tonic-gate ndi_devi_set_nodename(dev_info_t *dip, char *name, int flags)
395*0Sstevel@tonic-gate {
396*0Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags))
397*0Sstevel@tonic-gate 	char *nname, *oname;
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate 	ASSERT(dip && name);
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 	oname = DEVI(dip)->devi_node_name;
402*0Sstevel@tonic-gate 	if (strcmp(oname, name) == 0)
403*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
404*0Sstevel@tonic-gate 
405*0Sstevel@tonic-gate 	/*
406*0Sstevel@tonic-gate 	 * pcicfg_fix_ethernet requires a name change after node
407*0Sstevel@tonic-gate 	 * is linked into the tree. When pcicfg is fixed, we
408*0Sstevel@tonic-gate 	 * should only allow name change in DS_PROTO state.
409*0Sstevel@tonic-gate 	 */
410*0Sstevel@tonic-gate 	if (i_ddi_node_state(dip) >= DS_BOUND) {
411*0Sstevel@tonic-gate 		/*
412*0Sstevel@tonic-gate 		 * Don't allow name change once node is bound
413*0Sstevel@tonic-gate 		 */
414*0Sstevel@tonic-gate 		cmn_err(CE_NOTE,
415*0Sstevel@tonic-gate 		    "ndi_devi_set_nodename: node already bound dip = %p,"
416*0Sstevel@tonic-gate 		    " %s -> %s", (void *)dip, ddi_node_name(dip), name);
417*0Sstevel@tonic-gate 		return (NDI_FAILURE);
418*0Sstevel@tonic-gate 	}
419*0Sstevel@tonic-gate 
420*0Sstevel@tonic-gate 	nname = i_ddi_strdup(name, KM_SLEEP);
421*0Sstevel@tonic-gate 	DEVI(dip)->devi_node_name = nname;
422*0Sstevel@tonic-gate 	i_ddi_set_binding_name(dip, nname);
423*0Sstevel@tonic-gate 	kmem_free(oname, strlen(oname) + 1);
424*0Sstevel@tonic-gate 
425*0Sstevel@tonic-gate 	da_log_enter(dip);
426*0Sstevel@tonic-gate 	return (NDI_SUCCESS);
427*0Sstevel@tonic-gate }
428*0Sstevel@tonic-gate 
429*0Sstevel@tonic-gate void
430*0Sstevel@tonic-gate i_ddi_add_devimap(dev_info_t *dip)
431*0Sstevel@tonic-gate {
432*0Sstevel@tonic-gate 	struct devi_nodeid *elem;
433*0Sstevel@tonic-gate 
434*0Sstevel@tonic-gate 	ASSERT(dip);
435*0Sstevel@tonic-gate 
436*0Sstevel@tonic-gate 	if (!ndi_dev_is_persistent_node(dip))
437*0Sstevel@tonic-gate 		return;
438*0Sstevel@tonic-gate 
439*0Sstevel@tonic-gate 	ASSERT(ddi_get_parent(dip) == NULL || (DEVI_VHCI_NODE(dip)) ||
440*0Sstevel@tonic-gate 	    DEVI_BUSY_OWNED(ddi_get_parent(dip)));
441*0Sstevel@tonic-gate 
442*0Sstevel@tonic-gate 	mutex_enter(&devimap->dno_lock);
443*0Sstevel@tonic-gate 
444*0Sstevel@tonic-gate 	ASSERT(devimap->dno_free);
445*0Sstevel@tonic-gate 
446*0Sstevel@tonic-gate 	elem = devimap->dno_free;
447*0Sstevel@tonic-gate 	devimap->dno_free = elem->next;
448*0Sstevel@tonic-gate 
449*0Sstevel@tonic-gate 	elem->nodeid = ddi_get_nodeid(dip);
450*0Sstevel@tonic-gate 	elem->dip = dip;
451*0Sstevel@tonic-gate 	elem->next = devimap->dno_head;
452*0Sstevel@tonic-gate 	devimap->dno_head = elem;
453*0Sstevel@tonic-gate 
454*0Sstevel@tonic-gate 	devimap->dno_list_length++;
455*0Sstevel@tonic-gate 
456*0Sstevel@tonic-gate 	mutex_exit(&devimap->dno_lock);
457*0Sstevel@tonic-gate }
458*0Sstevel@tonic-gate 
459*0Sstevel@tonic-gate static int
460*0Sstevel@tonic-gate i_ddi_remove_devimap(dev_info_t *dip)
461*0Sstevel@tonic-gate {
462*0Sstevel@tonic-gate 	struct devi_nodeid *prev, *elem;
463*0Sstevel@tonic-gate 	static const char *fcn = "i_ddi_remove_devimap";
464*0Sstevel@tonic-gate 
465*0Sstevel@tonic-gate 	ASSERT(dip);
466*0Sstevel@tonic-gate 
467*0Sstevel@tonic-gate 	if (!ndi_dev_is_persistent_node(dip))
468*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
469*0Sstevel@tonic-gate 
470*0Sstevel@tonic-gate 	mutex_enter(&devimap->dno_lock);
471*0Sstevel@tonic-gate 
472*0Sstevel@tonic-gate 	/*
473*0Sstevel@tonic-gate 	 * The following check is done with dno_lock held
474*0Sstevel@tonic-gate 	 * to prevent race between dip removal and
475*0Sstevel@tonic-gate 	 * e_ddi_prom_node_to_dip()
476*0Sstevel@tonic-gate 	 */
477*0Sstevel@tonic-gate 	if (e_ddi_devi_holdcnt(dip)) {
478*0Sstevel@tonic-gate 		mutex_exit(&devimap->dno_lock);
479*0Sstevel@tonic-gate 		return (DDI_FAILURE);
480*0Sstevel@tonic-gate 	}
481*0Sstevel@tonic-gate 
482*0Sstevel@tonic-gate 	ASSERT(devimap->dno_head);
483*0Sstevel@tonic-gate 	ASSERT(devimap->dno_list_length > 0);
484*0Sstevel@tonic-gate 
485*0Sstevel@tonic-gate 	prev = NULL;
486*0Sstevel@tonic-gate 	for (elem = devimap->dno_head; elem; elem = elem->next) {
487*0Sstevel@tonic-gate 		if (elem->dip == dip) {
488*0Sstevel@tonic-gate 			ASSERT(elem->nodeid == ddi_get_nodeid(dip));
489*0Sstevel@tonic-gate 			break;
490*0Sstevel@tonic-gate 		}
491*0Sstevel@tonic-gate 		prev = elem;
492*0Sstevel@tonic-gate 	}
493*0Sstevel@tonic-gate 
494*0Sstevel@tonic-gate 	if (elem && prev)
495*0Sstevel@tonic-gate 		prev->next = elem->next;
496*0Sstevel@tonic-gate 	else if (elem)
497*0Sstevel@tonic-gate 		devimap->dno_head = elem->next;
498*0Sstevel@tonic-gate 	else
499*0Sstevel@tonic-gate 		panic("%s: devinfo node(%p) not found",
500*0Sstevel@tonic-gate 		    fcn, (void *)dip);
501*0Sstevel@tonic-gate 
502*0Sstevel@tonic-gate 	devimap->dno_list_length--;
503*0Sstevel@tonic-gate 
504*0Sstevel@tonic-gate 	elem->nodeid = 0;
505*0Sstevel@tonic-gate 	elem->dip = NULL;
506*0Sstevel@tonic-gate 
507*0Sstevel@tonic-gate 	elem->next = devimap->dno_free;
508*0Sstevel@tonic-gate 	devimap->dno_free = elem;
509*0Sstevel@tonic-gate 
510*0Sstevel@tonic-gate 	mutex_exit(&devimap->dno_lock);
511*0Sstevel@tonic-gate 
512*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
513*0Sstevel@tonic-gate }
514*0Sstevel@tonic-gate 
515*0Sstevel@tonic-gate /*
516*0Sstevel@tonic-gate  * Link this node into the devinfo tree and add to orphan list
517*0Sstevel@tonic-gate  * Not callable from interrupt context
518*0Sstevel@tonic-gate  */
519*0Sstevel@tonic-gate static void
520*0Sstevel@tonic-gate link_node(dev_info_t *dip)
521*0Sstevel@tonic-gate {
522*0Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
523*0Sstevel@tonic-gate 	struct dev_info *parent = devi->devi_parent;
524*0Sstevel@tonic-gate 	dev_info_t **dipp;
525*0Sstevel@tonic-gate 
526*0Sstevel@tonic-gate 	ASSERT(parent);	/* never called for root node */
527*0Sstevel@tonic-gate 
528*0Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "link_node: parent = %s child = %s\n",
529*0Sstevel@tonic-gate 	    parent->devi_node_name, devi->devi_node_name));
530*0Sstevel@tonic-gate 
531*0Sstevel@tonic-gate 	/*
532*0Sstevel@tonic-gate 	 * Hold the global_vhci_lock before linking any direct
533*0Sstevel@tonic-gate 	 * children of rootnex driver. This special lock protects
534*0Sstevel@tonic-gate 	 * linking and unlinking for rootnext direct children.
535*0Sstevel@tonic-gate 	 */
536*0Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
537*0Sstevel@tonic-gate 		mutex_enter(&global_vhci_lock);
538*0Sstevel@tonic-gate 
539*0Sstevel@tonic-gate 	/*
540*0Sstevel@tonic-gate 	 * attach the node to end of the list unless the node is already there
541*0Sstevel@tonic-gate 	 */
542*0Sstevel@tonic-gate 	dipp = (dev_info_t **)(&DEVI(parent)->devi_child);
543*0Sstevel@tonic-gate 	while (*dipp && (*dipp != dip)) {
544*0Sstevel@tonic-gate 		dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling);
545*0Sstevel@tonic-gate 	}
546*0Sstevel@tonic-gate 	ASSERT(*dipp == NULL);	/* node is not linked */
547*0Sstevel@tonic-gate 
548*0Sstevel@tonic-gate 	/*
549*0Sstevel@tonic-gate 	 * Now that we are in the tree, update the devi-nodeid map.
550*0Sstevel@tonic-gate 	 */
551*0Sstevel@tonic-gate 	i_ddi_add_devimap(dip);
552*0Sstevel@tonic-gate 
553*0Sstevel@tonic-gate 	/*
554*0Sstevel@tonic-gate 	 * This is a temporary workaround for Bug 4618861.
555*0Sstevel@tonic-gate 	 * We keep the scsi_vhci nexus node on the left side of the devinfo
556*0Sstevel@tonic-gate 	 * tree (under the root nexus driver), so that virtual nodes under
557*0Sstevel@tonic-gate 	 * scsi_vhci will be SUSPENDed first and RESUMEd last.  This ensures
558*0Sstevel@tonic-gate 	 * that the pHCI nodes are active during times when their clients
559*0Sstevel@tonic-gate 	 * may be depending on them.  This workaround embodies the knowledge
560*0Sstevel@tonic-gate 	 * that system PM and CPR both traverse the tree left-to-right during
561*0Sstevel@tonic-gate 	 * SUSPEND and right-to-left during RESUME.
562*0Sstevel@tonic-gate 	 */
563*0Sstevel@tonic-gate 	if (strcmp(devi->devi_name, "scsi_vhci") == 0) {
564*0Sstevel@tonic-gate 		/* Add scsi_vhci to beginning of list */
565*0Sstevel@tonic-gate 		ASSERT((dev_info_t *)parent == top_devinfo);
566*0Sstevel@tonic-gate 		/* scsi_vhci under rootnex */
567*0Sstevel@tonic-gate 		devi->devi_sibling = parent->devi_child;
568*0Sstevel@tonic-gate 		parent->devi_child = devi;
569*0Sstevel@tonic-gate 	} else {
570*0Sstevel@tonic-gate 		/* Add to end of list */
571*0Sstevel@tonic-gate 		*dipp = dip;
572*0Sstevel@tonic-gate 		DEVI(dip)->devi_sibling = NULL;
573*0Sstevel@tonic-gate 	}
574*0Sstevel@tonic-gate 
575*0Sstevel@tonic-gate 	/*
576*0Sstevel@tonic-gate 	 * Release the global_vhci_lock before linking any direct
577*0Sstevel@tonic-gate 	 * children of rootnex driver.
578*0Sstevel@tonic-gate 	 */
579*0Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
580*0Sstevel@tonic-gate 		mutex_exit(&global_vhci_lock);
581*0Sstevel@tonic-gate 
582*0Sstevel@tonic-gate 	/* persistent nodes go on orphan list */
583*0Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip))
584*0Sstevel@tonic-gate 		add_to_dn_list(&orphanlist, dip);
585*0Sstevel@tonic-gate }
586*0Sstevel@tonic-gate 
587*0Sstevel@tonic-gate /*
588*0Sstevel@tonic-gate  * Unlink this node from the devinfo tree
589*0Sstevel@tonic-gate  */
590*0Sstevel@tonic-gate static int
591*0Sstevel@tonic-gate unlink_node(dev_info_t *dip)
592*0Sstevel@tonic-gate {
593*0Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
594*0Sstevel@tonic-gate 	struct dev_info *parent = devi->devi_parent;
595*0Sstevel@tonic-gate 	dev_info_t **dipp;
596*0Sstevel@tonic-gate 
597*0Sstevel@tonic-gate 	ASSERT(parent != NULL);
598*0Sstevel@tonic-gate 	ASSERT(devi->devi_node_state == DS_LINKED);
599*0Sstevel@tonic-gate 
600*0Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "unlink_node: name = %s\n",
601*0Sstevel@tonic-gate 	    ddi_node_name(dip)));
602*0Sstevel@tonic-gate 
603*0Sstevel@tonic-gate 	/* check references */
604*0Sstevel@tonic-gate 	if (devi->devi_ref || i_ddi_remove_devimap(dip) != DDI_SUCCESS)
605*0Sstevel@tonic-gate 		return (DDI_FAILURE);
606*0Sstevel@tonic-gate 
607*0Sstevel@tonic-gate 	/*
608*0Sstevel@tonic-gate 	 * Hold the global_vhci_lock before linking any direct
609*0Sstevel@tonic-gate 	 * children of rootnex driver.
610*0Sstevel@tonic-gate 	 */
611*0Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
612*0Sstevel@tonic-gate 		mutex_enter(&global_vhci_lock);
613*0Sstevel@tonic-gate 
614*0Sstevel@tonic-gate 	dipp = (dev_info_t **)(&DEVI(parent)->devi_child);
615*0Sstevel@tonic-gate 	while (*dipp && (*dipp != dip)) {
616*0Sstevel@tonic-gate 		dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling);
617*0Sstevel@tonic-gate 	}
618*0Sstevel@tonic-gate 	if (*dipp) {
619*0Sstevel@tonic-gate 		*dipp = (dev_info_t *)(devi->devi_sibling);
620*0Sstevel@tonic-gate 		devi->devi_sibling = NULL;
621*0Sstevel@tonic-gate 	} else {
622*0Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE, "unlink_node: %s not linked",
623*0Sstevel@tonic-gate 		    devi->devi_node_name));
624*0Sstevel@tonic-gate 	}
625*0Sstevel@tonic-gate 
626*0Sstevel@tonic-gate 	/*
627*0Sstevel@tonic-gate 	 * Release the global_vhci_lock before linking any direct
628*0Sstevel@tonic-gate 	 * children of rootnex driver.
629*0Sstevel@tonic-gate 	 */
630*0Sstevel@tonic-gate 	if ((dev_info_t *)parent == ddi_root_node())
631*0Sstevel@tonic-gate 		mutex_exit(&global_vhci_lock);
632*0Sstevel@tonic-gate 
633*0Sstevel@tonic-gate 	/* Remove node from orphan list */
634*0Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
635*0Sstevel@tonic-gate 		remove_from_dn_list(&orphanlist, dip);
636*0Sstevel@tonic-gate 	}
637*0Sstevel@tonic-gate 
638*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
639*0Sstevel@tonic-gate }
640*0Sstevel@tonic-gate 
641*0Sstevel@tonic-gate /*
642*0Sstevel@tonic-gate  * Bind this devinfo node to a driver. If compat is NON-NULL, try that first.
643*0Sstevel@tonic-gate  * Else, use the node-name.
644*0Sstevel@tonic-gate  *
645*0Sstevel@tonic-gate  * NOTE: IEEE1275 specifies that nodename should be tried before compatible.
646*0Sstevel@tonic-gate  *	Solaris implementation binds nodename after compatible.
647*0Sstevel@tonic-gate  *
648*0Sstevel@tonic-gate  * If we find a binding,
649*0Sstevel@tonic-gate  * - set the binding name to the the string,
650*0Sstevel@tonic-gate  * - set major number to driver major
651*0Sstevel@tonic-gate  *
652*0Sstevel@tonic-gate  * If we don't find a binding,
653*0Sstevel@tonic-gate  * - return failure
654*0Sstevel@tonic-gate  */
655*0Sstevel@tonic-gate static int
656*0Sstevel@tonic-gate bind_node(dev_info_t *dip)
657*0Sstevel@tonic-gate {
658*0Sstevel@tonic-gate 	char *p = NULL;
659*0Sstevel@tonic-gate 	major_t major = (major_t)(major_t)-1;
660*0Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
661*0Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
662*0Sstevel@tonic-gate 
663*0Sstevel@tonic-gate 	ASSERT(devi->devi_node_state == DS_LINKED);
664*0Sstevel@tonic-gate 
665*0Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "bind_node: 0x%p(name = %s)\n",
666*0Sstevel@tonic-gate 	    (void *)dip, ddi_node_name(dip)));
667*0Sstevel@tonic-gate 
668*0Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
669*0Sstevel@tonic-gate 	if (DEVI(dip)->devi_flags & DEVI_NO_BIND) {
670*0Sstevel@tonic-gate 		mutex_exit(&DEVI(dip)->devi_lock);
671*0Sstevel@tonic-gate 		return (DDI_FAILURE);
672*0Sstevel@tonic-gate 	}
673*0Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
674*0Sstevel@tonic-gate 
675*0Sstevel@tonic-gate 	/* find the driver with most specific binding using compatible */
676*0Sstevel@tonic-gate 	major = ddi_compatible_driver_major(dip, &p);
677*0Sstevel@tonic-gate 	if (major == (major_t)-1)
678*0Sstevel@tonic-gate 		return (DDI_FAILURE);
679*0Sstevel@tonic-gate 
680*0Sstevel@tonic-gate 	devi->devi_major = major;
681*0Sstevel@tonic-gate 	if (p != NULL) {
682*0Sstevel@tonic-gate 		i_ddi_set_binding_name(dip, p);
683*0Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "bind_node: %s bound to %s\n",
684*0Sstevel@tonic-gate 		    devi->devi_node_name, p));
685*0Sstevel@tonic-gate 	}
686*0Sstevel@tonic-gate 
687*0Sstevel@tonic-gate 	/* Link node to per-driver list */
688*0Sstevel@tonic-gate 	link_to_driver_list(dip);
689*0Sstevel@tonic-gate 
690*0Sstevel@tonic-gate 	/*
691*0Sstevel@tonic-gate 	 * reset parent flag so that nexus will merge .conf props
692*0Sstevel@tonic-gate 	 */
693*0Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
694*0Sstevel@tonic-gate 		mutex_enter(&DEVI(parent)->devi_lock);
695*0Sstevel@tonic-gate 		DEVI(parent)->devi_flags &=
696*0Sstevel@tonic-gate 		    ~(DEVI_ATTACHED_CHILDREN|DEVI_MADE_CHILDREN);
697*0Sstevel@tonic-gate 		mutex_exit(&DEVI(parent)->devi_lock);
698*0Sstevel@tonic-gate 	}
699*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
700*0Sstevel@tonic-gate }
701*0Sstevel@tonic-gate 
702*0Sstevel@tonic-gate /*
703*0Sstevel@tonic-gate  * Unbind this devinfo node
704*0Sstevel@tonic-gate  * Called before the node is destroyed or driver is removed from system
705*0Sstevel@tonic-gate  */
706*0Sstevel@tonic-gate static int
707*0Sstevel@tonic-gate unbind_node(dev_info_t *dip)
708*0Sstevel@tonic-gate {
709*0Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_node_state == DS_BOUND);
710*0Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_major != (major_t)-1);
711*0Sstevel@tonic-gate 
712*0Sstevel@tonic-gate 	/* check references */
713*0Sstevel@tonic-gate 	if (DEVI(dip)->devi_ref)
714*0Sstevel@tonic-gate 		return (DDI_FAILURE);
715*0Sstevel@tonic-gate 
716*0Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "unbind_node: 0x%p(name = %s)\n",
717*0Sstevel@tonic-gate 	    (void *)dip, ddi_node_name(dip)));
718*0Sstevel@tonic-gate 
719*0Sstevel@tonic-gate 	unlink_from_driver_list(dip);
720*0Sstevel@tonic-gate 	DEVI(dip)->devi_major = (major_t)-1;
721*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
722*0Sstevel@tonic-gate }
723*0Sstevel@tonic-gate 
724*0Sstevel@tonic-gate /*
725*0Sstevel@tonic-gate  * Initialize a node: calls the parent nexus' bus_ctl ops to do the operation.
726*0Sstevel@tonic-gate  * Must hold parent and per-driver list while calling this function.
727*0Sstevel@tonic-gate  * A successful init_node() returns with an active ndi_hold_devi() hold on
728*0Sstevel@tonic-gate  * the parent.
729*0Sstevel@tonic-gate  */
730*0Sstevel@tonic-gate static int
731*0Sstevel@tonic-gate init_node(dev_info_t *dip)
732*0Sstevel@tonic-gate {
733*0Sstevel@tonic-gate 	int error;
734*0Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
735*0Sstevel@tonic-gate 	int (*f)(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *, void *);
736*0Sstevel@tonic-gate 	char *path;
737*0Sstevel@tonic-gate 
738*0Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_BOUND);
739*0Sstevel@tonic-gate 
740*0Sstevel@tonic-gate 	/* should be DS_READY except for pcmcia ... */
741*0Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(pdip) >= DS_PROBED);
742*0Sstevel@tonic-gate 
743*0Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
744*0Sstevel@tonic-gate 	(void) ddi_pathname(dip, path);
745*0Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "init_node: entry: path %s 0x%p\n",
746*0Sstevel@tonic-gate 	    path, (void *)dip));
747*0Sstevel@tonic-gate 
748*0Sstevel@tonic-gate 	/*
749*0Sstevel@tonic-gate 	 * The parent must have a bus_ctl operation.
750*0Sstevel@tonic-gate 	 */
751*0Sstevel@tonic-gate 	if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
752*0Sstevel@tonic-gate 	    (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_ctl) == NULL) {
753*0Sstevel@tonic-gate 		error = DDI_FAILURE;
754*0Sstevel@tonic-gate 		goto out;
755*0Sstevel@tonic-gate 	}
756*0Sstevel@tonic-gate 
757*0Sstevel@tonic-gate 	add_global_props(dip);
758*0Sstevel@tonic-gate 
759*0Sstevel@tonic-gate 	/*
760*0Sstevel@tonic-gate 	 * Invoke the parent's bus_ctl operation with the DDI_CTLOPS_INITCHILD
761*0Sstevel@tonic-gate 	 * command to transform the child to canonical form 1. If there
762*0Sstevel@tonic-gate 	 * is an error, ddi_remove_child should be called, to clean up.
763*0Sstevel@tonic-gate 	 */
764*0Sstevel@tonic-gate 	error = (*f)(pdip, pdip, DDI_CTLOPS_INITCHILD, dip, NULL);
765*0Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
766*0Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "init_node: %s 0x%p failed\n",
767*0Sstevel@tonic-gate 		    path, (void *)dip));
768*0Sstevel@tonic-gate 		remove_global_props(dip);
769*0Sstevel@tonic-gate 		/* in case nexus driver didn't clear this field */
770*0Sstevel@tonic-gate 		ddi_set_name_addr(dip, NULL);
771*0Sstevel@tonic-gate 		error = DDI_FAILURE;
772*0Sstevel@tonic-gate 		goto out;
773*0Sstevel@tonic-gate 	}
774*0Sstevel@tonic-gate 
775*0Sstevel@tonic-gate 	ndi_hold_devi(pdip);
776*0Sstevel@tonic-gate 
777*0Sstevel@tonic-gate 	/* check for duplicate nodes */
778*0Sstevel@tonic-gate 	if (find_duplicate_child(pdip, dip) != NULL) {
779*0Sstevel@tonic-gate 		/* recompute path after initchild for @addr information */
780*0Sstevel@tonic-gate 		(void) ddi_pathname(dip, path);
781*0Sstevel@tonic-gate 
782*0Sstevel@tonic-gate 		/*
783*0Sstevel@tonic-gate 		 * uninit_node() the duplicate - a successful uninit_node()
784*0Sstevel@tonic-gate 		 * does a ndi_rele_devi
785*0Sstevel@tonic-gate 		 */
786*0Sstevel@tonic-gate 		if ((error = uninit_node(dip)) != DDI_SUCCESS) {
787*0Sstevel@tonic-gate 			ndi_rele_devi(pdip);
788*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "init_node: uninit of duplicate "
789*0Sstevel@tonic-gate 			    "node %s failed", path);
790*0Sstevel@tonic-gate 		}
791*0Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "init_node: duplicate uninit "
792*0Sstevel@tonic-gate 		    "%s 0x%p%s\n", path, (void *)dip,
793*0Sstevel@tonic-gate 		    (error == DDI_SUCCESS) ? "" : " failed"));
794*0Sstevel@tonic-gate 		error = DDI_FAILURE;
795*0Sstevel@tonic-gate 		goto out;
796*0Sstevel@tonic-gate 	}
797*0Sstevel@tonic-gate 
798*0Sstevel@tonic-gate 	/*
799*0Sstevel@tonic-gate 	 * Apply multi-parent/deep-nexus optimization to the new node
800*0Sstevel@tonic-gate 	 */
801*0Sstevel@tonic-gate 	DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
802*0Sstevel@tonic-gate 	ddi_optimize_dtree(dip);
803*0Sstevel@tonic-gate 	error = DDI_SUCCESS;
804*0Sstevel@tonic-gate 
805*0Sstevel@tonic-gate out:	kmem_free(path, MAXPATHLEN);
806*0Sstevel@tonic-gate 	return (error);
807*0Sstevel@tonic-gate }
808*0Sstevel@tonic-gate 
809*0Sstevel@tonic-gate /*
810*0Sstevel@tonic-gate  * Uninitialize node
811*0Sstevel@tonic-gate  * The per-driver list must be held busy during the call.
812*0Sstevel@tonic-gate  * A successful uninit_node() releases the init_node() hold on
813*0Sstevel@tonic-gate  * the parent by calling ndi_rele_devi().
814*0Sstevel@tonic-gate  */
815*0Sstevel@tonic-gate static int
816*0Sstevel@tonic-gate uninit_node(dev_info_t *dip)
817*0Sstevel@tonic-gate {
818*0Sstevel@tonic-gate 	int node_state_entry;
819*0Sstevel@tonic-gate 	dev_info_t *pdip;
820*0Sstevel@tonic-gate 	struct dev_ops *ops;
821*0Sstevel@tonic-gate 	int (*f)();
822*0Sstevel@tonic-gate 	int error;
823*0Sstevel@tonic-gate 	char *addr;
824*0Sstevel@tonic-gate 
825*0Sstevel@tonic-gate 	/*
826*0Sstevel@tonic-gate 	 * Don't check for references here or else a ref-counted
827*0Sstevel@tonic-gate 	 * dip cannot be downgraded by the framework.
828*0Sstevel@tonic-gate 	 */
829*0Sstevel@tonic-gate 	node_state_entry = i_ddi_node_state(dip);
830*0Sstevel@tonic-gate 	ASSERT((node_state_entry == DS_BOUND) ||
831*0Sstevel@tonic-gate 		(node_state_entry == DS_INITIALIZED));
832*0Sstevel@tonic-gate 	pdip = ddi_get_parent(dip);
833*0Sstevel@tonic-gate 	ASSERT(pdip);
834*0Sstevel@tonic-gate 
835*0Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "uninit_node: 0x%p(%s%d)\n",
836*0Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
837*0Sstevel@tonic-gate 
838*0Sstevel@tonic-gate 	if (((ops = ddi_get_driver(pdip)) == NULL) ||
839*0Sstevel@tonic-gate 	    (ops->devo_bus_ops == NULL) ||
840*0Sstevel@tonic-gate 	    ((f = ops->devo_bus_ops->bus_ctl) == NULL)) {
841*0Sstevel@tonic-gate 		return (DDI_FAILURE);
842*0Sstevel@tonic-gate 	}
843*0Sstevel@tonic-gate 
844*0Sstevel@tonic-gate 	/*
845*0Sstevel@tonic-gate 	 * save the @addr prior to DDI_CTLOPS_UNINITCHILD for use in
846*0Sstevel@tonic-gate 	 * freeing the instance if it succeeds.
847*0Sstevel@tonic-gate 	 */
848*0Sstevel@tonic-gate 	if (node_state_entry == DS_INITIALIZED) {
849*0Sstevel@tonic-gate 		addr = ddi_get_name_addr(dip);
850*0Sstevel@tonic-gate 		if (addr)
851*0Sstevel@tonic-gate 			addr = i_ddi_strdup(addr, KM_SLEEP);
852*0Sstevel@tonic-gate 	} else {
853*0Sstevel@tonic-gate 		addr = NULL;
854*0Sstevel@tonic-gate 	}
855*0Sstevel@tonic-gate 
856*0Sstevel@tonic-gate 	error = (*f)(pdip, pdip, DDI_CTLOPS_UNINITCHILD, dip, (void *)NULL);
857*0Sstevel@tonic-gate 	if (error == DDI_SUCCESS) {
858*0Sstevel@tonic-gate 		/* if uninitchild forgot to set devi_addr to NULL do it now */
859*0Sstevel@tonic-gate 		ddi_set_name_addr(dip, NULL);
860*0Sstevel@tonic-gate 
861*0Sstevel@tonic-gate 		/*
862*0Sstevel@tonic-gate 		 * Free instance number. This is a no-op if instance has
863*0Sstevel@tonic-gate 		 * been kept by probe_node().  Avoid free when we are called
864*0Sstevel@tonic-gate 		 * from init_node (DS_BOUND) because the instance has not yet
865*0Sstevel@tonic-gate 		 * been assigned.
866*0Sstevel@tonic-gate 		 */
867*0Sstevel@tonic-gate 		if (node_state_entry == DS_INITIALIZED) {
868*0Sstevel@tonic-gate 			e_ddi_free_instance(dip, addr);
869*0Sstevel@tonic-gate 			DEVI(dip)->devi_instance = -1;
870*0Sstevel@tonic-gate 		}
871*0Sstevel@tonic-gate 
872*0Sstevel@tonic-gate 		/* release the init_node hold */
873*0Sstevel@tonic-gate 		ndi_rele_devi(pdip);
874*0Sstevel@tonic-gate 
875*0Sstevel@tonic-gate 		remove_global_props(dip);
876*0Sstevel@tonic-gate 		e_ddi_prop_remove_all(dip);
877*0Sstevel@tonic-gate 	} else {
878*0Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "uninit_node failed: 0x%p(%s%d)\n",
879*0Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
880*0Sstevel@tonic-gate 	}
881*0Sstevel@tonic-gate 
882*0Sstevel@tonic-gate 	if (addr)
883*0Sstevel@tonic-gate 		kmem_free(addr, strlen(addr) + 1);
884*0Sstevel@tonic-gate 	return (error);
885*0Sstevel@tonic-gate }
886*0Sstevel@tonic-gate 
887*0Sstevel@tonic-gate /*
888*0Sstevel@tonic-gate  * Invoke driver's probe entry point to probe for existence of hardware.
889*0Sstevel@tonic-gate  * Keep instance permanent for successful probe and leaf nodes.
890*0Sstevel@tonic-gate  *
891*0Sstevel@tonic-gate  * Per-driver list must be held busy while calling this function.
892*0Sstevel@tonic-gate  */
893*0Sstevel@tonic-gate static int
894*0Sstevel@tonic-gate probe_node(dev_info_t *dip)
895*0Sstevel@tonic-gate {
896*0Sstevel@tonic-gate 	int rv;
897*0Sstevel@tonic-gate 
898*0Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_INITIALIZED);
899*0Sstevel@tonic-gate 
900*0Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "probe_node: 0x%p(%s%d)\n",
901*0Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
902*0Sstevel@tonic-gate 
903*0Sstevel@tonic-gate 	/* temporarily hold the driver while we probe */
904*0Sstevel@tonic-gate 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
905*0Sstevel@tonic-gate 	if (DEVI(dip)->devi_ops == NULL) {
906*0Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
907*0Sstevel@tonic-gate 		    "probe_node: 0x%p(%s%d) cannot load driver\n",
908*0Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
909*0Sstevel@tonic-gate 		return (DDI_FAILURE);
910*0Sstevel@tonic-gate 	}
911*0Sstevel@tonic-gate 
912*0Sstevel@tonic-gate 	if (identify_9e != 0)
913*0Sstevel@tonic-gate 		(void) devi_identify(dip);
914*0Sstevel@tonic-gate 
915*0Sstevel@tonic-gate 	rv = devi_probe(dip);
916*0Sstevel@tonic-gate 
917*0Sstevel@tonic-gate 	/* release the driver now that probe is complete */
918*0Sstevel@tonic-gate 	ndi_rele_driver(dip);
919*0Sstevel@tonic-gate 	DEVI(dip)->devi_ops = NULL;
920*0Sstevel@tonic-gate 
921*0Sstevel@tonic-gate 	switch (rv) {
922*0Sstevel@tonic-gate 	case DDI_PROBE_SUCCESS:			/* found */
923*0Sstevel@tonic-gate 	case DDI_PROBE_DONTCARE:		/* ddi_dev_is_sid */
924*0Sstevel@tonic-gate 		e_ddi_keep_instance(dip);	/* persist instance */
925*0Sstevel@tonic-gate 		rv = DDI_SUCCESS;
926*0Sstevel@tonic-gate 		break;
927*0Sstevel@tonic-gate 
928*0Sstevel@tonic-gate 	case DDI_PROBE_PARTIAL:			/* maybe later */
929*0Sstevel@tonic-gate 	case DDI_PROBE_FAILURE:			/* not found */
930*0Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
931*0Sstevel@tonic-gate 		    "probe_node: 0x%p(%s%d) no hardware found%s\n",
932*0Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip),
933*0Sstevel@tonic-gate 		    (rv == DDI_PROBE_PARTIAL) ? " yet" : ""));
934*0Sstevel@tonic-gate 		rv = DDI_FAILURE;
935*0Sstevel@tonic-gate 		break;
936*0Sstevel@tonic-gate 
937*0Sstevel@tonic-gate 	default:
938*0Sstevel@tonic-gate #ifdef	DEBUG
939*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "probe_node: %s%d: illegal probe(9E) value",
940*0Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
941*0Sstevel@tonic-gate #endif	/* DEBUG */
942*0Sstevel@tonic-gate 		rv = DDI_FAILURE;
943*0Sstevel@tonic-gate 		break;
944*0Sstevel@tonic-gate 	}
945*0Sstevel@tonic-gate 	return (rv);
946*0Sstevel@tonic-gate }
947*0Sstevel@tonic-gate 
948*0Sstevel@tonic-gate /*
949*0Sstevel@tonic-gate  * Unprobe a node. Simply reset the node state.
950*0Sstevel@tonic-gate  * Per-driver list must be held busy while calling this function.
951*0Sstevel@tonic-gate  */
952*0Sstevel@tonic-gate static int
953*0Sstevel@tonic-gate unprobe_node(dev_info_t *dip)
954*0Sstevel@tonic-gate {
955*0Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_PROBED);
956*0Sstevel@tonic-gate 
957*0Sstevel@tonic-gate 	/*
958*0Sstevel@tonic-gate 	 * Don't check for references here or else a ref-counted
959*0Sstevel@tonic-gate 	 * dip cannot be downgraded by the framework.
960*0Sstevel@tonic-gate 	 */
961*0Sstevel@tonic-gate 
962*0Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "unprobe_node: 0x%p(name = %s)\n",
963*0Sstevel@tonic-gate 	    (void *)dip, ddi_node_name(dip)));
964*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
965*0Sstevel@tonic-gate }
966*0Sstevel@tonic-gate 
967*0Sstevel@tonic-gate /*
968*0Sstevel@tonic-gate  * Attach devinfo node.
969*0Sstevel@tonic-gate  * Per-driver list must be held busy.
970*0Sstevel@tonic-gate  */
971*0Sstevel@tonic-gate static int
972*0Sstevel@tonic-gate attach_node(dev_info_t *dip)
973*0Sstevel@tonic-gate {
974*0Sstevel@tonic-gate 	int rv;
975*0Sstevel@tonic-gate 
976*0Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_PROBED);
977*0Sstevel@tonic-gate 
978*0Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d)\n",
979*0Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
980*0Sstevel@tonic-gate 
981*0Sstevel@tonic-gate 	/*
982*0Sstevel@tonic-gate 	 * Tell mpxio framework that a node is about to online.
983*0Sstevel@tonic-gate 	 */
984*0Sstevel@tonic-gate 	if ((rv = mdi_devi_online(dip, 0)) != NDI_SUCCESS) {
985*0Sstevel@tonic-gate 		return (DDI_FAILURE);
986*0Sstevel@tonic-gate 	}
987*0Sstevel@tonic-gate 
988*0Sstevel@tonic-gate 	/* no recursive attachment */
989*0Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_ops == NULL);
990*0Sstevel@tonic-gate 
991*0Sstevel@tonic-gate 	/*
992*0Sstevel@tonic-gate 	 * Hold driver the node is bound to.
993*0Sstevel@tonic-gate 	 */
994*0Sstevel@tonic-gate 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
995*0Sstevel@tonic-gate 	if (DEVI(dip)->devi_ops == NULL) {
996*0Sstevel@tonic-gate 		/*
997*0Sstevel@tonic-gate 		 * We were able to load driver for probing, so we should
998*0Sstevel@tonic-gate 		 * not get here unless something really bad happened.
999*0Sstevel@tonic-gate 		 */
1000*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "attach_node: no driver for major %d",
1001*0Sstevel@tonic-gate 		    DEVI(dip)->devi_major);
1002*0Sstevel@tonic-gate 		return (DDI_FAILURE);
1003*0Sstevel@tonic-gate 	}
1004*0Sstevel@tonic-gate 
1005*0Sstevel@tonic-gate 	if (NEXUS_DRV(DEVI(dip)->devi_ops))
1006*0Sstevel@tonic-gate 		DEVI(dip)->devi_taskq = ddi_taskq_create(dip,
1007*0Sstevel@tonic-gate 		    "nexus_enum_tq", 1,
1008*0Sstevel@tonic-gate 		    TASKQ_DEFAULTPRI, 0);
1009*0Sstevel@tonic-gate 
1010*0Sstevel@tonic-gate 	DEVI_SET_ATTACHING(dip);
1011*0Sstevel@tonic-gate 	DEVI_SET_NEED_RESET(dip);
1012*0Sstevel@tonic-gate 	rv = devi_attach(dip, DDI_ATTACH);
1013*0Sstevel@tonic-gate 	if (rv != DDI_SUCCESS)
1014*0Sstevel@tonic-gate 		DEVI_CLR_NEED_RESET(dip);
1015*0Sstevel@tonic-gate 	DEVI_CLR_ATTACHING(dip);
1016*0Sstevel@tonic-gate 
1017*0Sstevel@tonic-gate 	if (rv != DDI_SUCCESS) {
1018*0Sstevel@tonic-gate 		if (DEVI(dip)->devi_flags & DEVI_REGISTERED_DEVID) {
1019*0Sstevel@tonic-gate 			e_devid_cache_unregister(dip);
1020*0Sstevel@tonic-gate 			DEVI(dip)->devi_flags &= ~DEVI_REGISTERED_DEVID;
1021*0Sstevel@tonic-gate 		}
1022*0Sstevel@tonic-gate 		/*
1023*0Sstevel@tonic-gate 		 * Cleanup dacf reservations
1024*0Sstevel@tonic-gate 		 */
1025*0Sstevel@tonic-gate 		mutex_enter(&dacf_lock);
1026*0Sstevel@tonic-gate 		dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH);
1027*0Sstevel@tonic-gate 		dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH);
1028*0Sstevel@tonic-gate 		mutex_exit(&dacf_lock);
1029*0Sstevel@tonic-gate 		if (DEVI(dip)->devi_taskq)
1030*0Sstevel@tonic-gate 			ddi_taskq_destroy(DEVI(dip)->devi_taskq);
1031*0Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
1032*0Sstevel@tonic-gate 
1033*0Sstevel@tonic-gate 		/* release the driver if attach failed */
1034*0Sstevel@tonic-gate 		ndi_rele_driver(dip);
1035*0Sstevel@tonic-gate 		DEVI(dip)->devi_ops = NULL;
1036*0Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d) failed\n",
1037*0Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1038*0Sstevel@tonic-gate 		return (DDI_FAILURE);
1039*0Sstevel@tonic-gate 	}
1040*0Sstevel@tonic-gate 
1041*0Sstevel@tonic-gate 	/* successful attach, return with driver held */
1042*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
1043*0Sstevel@tonic-gate }
1044*0Sstevel@tonic-gate 
1045*0Sstevel@tonic-gate /*
1046*0Sstevel@tonic-gate  * Detach devinfo node.
1047*0Sstevel@tonic-gate  * Per-driver list must be held busy.
1048*0Sstevel@tonic-gate  */
1049*0Sstevel@tonic-gate static int
1050*0Sstevel@tonic-gate detach_node(dev_info_t *dip, uint_t flag)
1051*0Sstevel@tonic-gate {
1052*0Sstevel@tonic-gate 	int rv;
1053*0Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_ATTACHED);
1054*0Sstevel@tonic-gate 
1055*0Sstevel@tonic-gate 	/* check references */
1056*0Sstevel@tonic-gate 	if (DEVI(dip)->devi_ref)
1057*0Sstevel@tonic-gate 		return (DDI_FAILURE);
1058*0Sstevel@tonic-gate 
1059*0Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "detach_node: 0x%p(%s%d)\n",
1060*0Sstevel@tonic-gate 	    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1061*0Sstevel@tonic-gate 
1062*0Sstevel@tonic-gate 	/* Offline the device node with the mpxio framework. */
1063*0Sstevel@tonic-gate 	if (mdi_devi_offline(dip, flag) != NDI_SUCCESS) {
1064*0Sstevel@tonic-gate 		return (DDI_FAILURE);
1065*0Sstevel@tonic-gate 	}
1066*0Sstevel@tonic-gate 
1067*0Sstevel@tonic-gate 	/* drain the taskq */
1068*0Sstevel@tonic-gate 	if (DEVI(dip)->devi_taskq)
1069*0Sstevel@tonic-gate 		ddi_taskq_wait(DEVI(dip)->devi_taskq);
1070*0Sstevel@tonic-gate 
1071*0Sstevel@tonic-gate 	rv = devi_detach(dip, DDI_DETACH);
1072*0Sstevel@tonic-gate 	if (rv == DDI_SUCCESS)
1073*0Sstevel@tonic-gate 		DEVI_CLR_NEED_RESET(dip);
1074*0Sstevel@tonic-gate 
1075*0Sstevel@tonic-gate 	if (rv != DDI_SUCCESS) {
1076*0Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
1077*0Sstevel@tonic-gate 		    "detach_node: 0x%p(%s%d) failed\n",
1078*0Sstevel@tonic-gate 		    (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1079*0Sstevel@tonic-gate 		return (DDI_FAILURE);
1080*0Sstevel@tonic-gate 	}
1081*0Sstevel@tonic-gate 
1082*0Sstevel@tonic-gate 	/* destroy the taskq */
1083*0Sstevel@tonic-gate 	if (DEVI(dip)->devi_taskq) {
1084*0Sstevel@tonic-gate 		ddi_taskq_destroy(DEVI(dip)->devi_taskq);
1085*0Sstevel@tonic-gate 		DEVI(dip)->devi_taskq = NULL;
1086*0Sstevel@tonic-gate 	}
1087*0Sstevel@tonic-gate 
1088*0Sstevel@tonic-gate 	/* Cleanup dacf reservations */
1089*0Sstevel@tonic-gate 	mutex_enter(&dacf_lock);
1090*0Sstevel@tonic-gate 	dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH);
1091*0Sstevel@tonic-gate 	dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH);
1092*0Sstevel@tonic-gate 	mutex_exit(&dacf_lock);
1093*0Sstevel@tonic-gate 
1094*0Sstevel@tonic-gate 	/* Remove properties and minor nodes in case driver forgots */
1095*0Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
1096*0Sstevel@tonic-gate 	ddi_prop_remove_all(dip);
1097*0Sstevel@tonic-gate 
1098*0Sstevel@tonic-gate 	/* a detached node can't have attached or .conf children */
1099*0Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
1100*0Sstevel@tonic-gate 	DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN|DEVI_ATTACHED_CHILDREN);
1101*0Sstevel@tonic-gate 	if (DEVI(dip)->devi_flags & DEVI_REGISTERED_DEVID) {
1102*0Sstevel@tonic-gate 		e_devid_cache_unregister(dip);
1103*0Sstevel@tonic-gate 		DEVI(dip)->devi_flags &= ~DEVI_REGISTERED_DEVID;
1104*0Sstevel@tonic-gate 	}
1105*0Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
1106*0Sstevel@tonic-gate 
1107*0Sstevel@tonic-gate 	/* successful detach, release the driver */
1108*0Sstevel@tonic-gate 	ndi_rele_driver(dip);
1109*0Sstevel@tonic-gate 	DEVI(dip)->devi_ops = NULL;
1110*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
1111*0Sstevel@tonic-gate }
1112*0Sstevel@tonic-gate 
1113*0Sstevel@tonic-gate /*
1114*0Sstevel@tonic-gate  * Run dacf post_attach routines
1115*0Sstevel@tonic-gate  */
1116*0Sstevel@tonic-gate static int
1117*0Sstevel@tonic-gate postattach_node(dev_info_t *dip)
1118*0Sstevel@tonic-gate {
1119*0Sstevel@tonic-gate 	int rval;
1120*0Sstevel@tonic-gate 
1121*0Sstevel@tonic-gate 	/*
1122*0Sstevel@tonic-gate 	 * For hotplug busses like USB, it's possible that devices
1123*0Sstevel@tonic-gate 	 * are removed but dip is still around. We don't want to
1124*0Sstevel@tonic-gate 	 * run dacf routines as part of detach failure recovery.
1125*0Sstevel@tonic-gate 	 *
1126*0Sstevel@tonic-gate 	 * Pretend success until we figure out how to prevent
1127*0Sstevel@tonic-gate 	 * access to such devinfo nodes.
1128*0Sstevel@tonic-gate 	 */
1129*0Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_REMOVED(dip))
1130*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
1131*0Sstevel@tonic-gate 
1132*0Sstevel@tonic-gate 	/*
1133*0Sstevel@tonic-gate 	 * if dacf_postattach failed, report it to the framework
1134*0Sstevel@tonic-gate 	 * so that it can be retried later at the open time.
1135*0Sstevel@tonic-gate 	 */
1136*0Sstevel@tonic-gate 	mutex_enter(&dacf_lock);
1137*0Sstevel@tonic-gate 	rval = dacfc_postattach(dip);
1138*0Sstevel@tonic-gate 	mutex_exit(&dacf_lock);
1139*0Sstevel@tonic-gate 
1140*0Sstevel@tonic-gate 	/*
1141*0Sstevel@tonic-gate 	 * Plumbing during postattach may fail because of the
1142*0Sstevel@tonic-gate 	 * underlying device is not ready. This will fail ndi_devi_config()
1143*0Sstevel@tonic-gate 	 * in dv_filldir() and a warning message is issued. The message
1144*0Sstevel@tonic-gate 	 * from here will explain what happened
1145*0Sstevel@tonic-gate 	 */
1146*0Sstevel@tonic-gate 	if (rval != DACF_SUCCESS) {
1147*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "Postattach failed for %s%d\n",
1148*0Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
1149*0Sstevel@tonic-gate 		return (DDI_FAILURE);
1150*0Sstevel@tonic-gate 	}
1151*0Sstevel@tonic-gate 
1152*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
1153*0Sstevel@tonic-gate }
1154*0Sstevel@tonic-gate 
1155*0Sstevel@tonic-gate /*
1156*0Sstevel@tonic-gate  * Run dacf pre-detach routines
1157*0Sstevel@tonic-gate  */
1158*0Sstevel@tonic-gate static int
1159*0Sstevel@tonic-gate predetach_node(dev_info_t *dip, uint_t flag)
1160*0Sstevel@tonic-gate {
1161*0Sstevel@tonic-gate 	int ret;
1162*0Sstevel@tonic-gate 
1163*0Sstevel@tonic-gate 	/*
1164*0Sstevel@tonic-gate 	 * Don't auto-detach if DDI_FORCEATTACH or DDI_NO_AUTODETACH
1165*0Sstevel@tonic-gate 	 * properties are set.
1166*0Sstevel@tonic-gate 	 */
1167*0Sstevel@tonic-gate 	if (flag & NDI_AUTODETACH) {
1168*0Sstevel@tonic-gate 		struct devnames *dnp;
1169*0Sstevel@tonic-gate 		int pflag = DDI_PROP_NOTPROM | DDI_PROP_DONTPASS;
1170*0Sstevel@tonic-gate 
1171*0Sstevel@tonic-gate 		if ((ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1172*0Sstevel@tonic-gate 			pflag, DDI_FORCEATTACH, 0) == 1) ||
1173*0Sstevel@tonic-gate 		    (ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1174*0Sstevel@tonic-gate 			pflag, DDI_NO_AUTODETACH, 0) == 1))
1175*0Sstevel@tonic-gate 			return (DDI_FAILURE);
1176*0Sstevel@tonic-gate 
1177*0Sstevel@tonic-gate 		/* check for driver global version of DDI_NO_AUTODETACH */
1178*0Sstevel@tonic-gate 		dnp = &devnamesp[DEVI(dip)->devi_major];
1179*0Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
1180*0Sstevel@tonic-gate 		if (dnp->dn_flags & DN_NO_AUTODETACH) {
1181*0Sstevel@tonic-gate 			UNLOCK_DEV_OPS(&dnp->dn_lock);
1182*0Sstevel@tonic-gate 			return (DDI_FAILURE);
1183*0Sstevel@tonic-gate 		}
1184*0Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
1185*0Sstevel@tonic-gate 	}
1186*0Sstevel@tonic-gate 
1187*0Sstevel@tonic-gate 	mutex_enter(&dacf_lock);
1188*0Sstevel@tonic-gate 	ret = dacfc_predetach(dip);
1189*0Sstevel@tonic-gate 	mutex_exit(&dacf_lock);
1190*0Sstevel@tonic-gate 
1191*0Sstevel@tonic-gate 	return (ret);
1192*0Sstevel@tonic-gate }
1193*0Sstevel@tonic-gate 
1194*0Sstevel@tonic-gate /*
1195*0Sstevel@tonic-gate  * Wrapper for making multiple state transitions
1196*0Sstevel@tonic-gate  */
1197*0Sstevel@tonic-gate 
1198*0Sstevel@tonic-gate /*
1199*0Sstevel@tonic-gate  * i_ndi_config_node: upgrade dev_info node into a specified state.
1200*0Sstevel@tonic-gate  * It is a bit tricky because the locking protocol changes before and
1201*0Sstevel@tonic-gate  * after a node is bound to a driver. All locks are held external to
1202*0Sstevel@tonic-gate  * this function.
1203*0Sstevel@tonic-gate  */
1204*0Sstevel@tonic-gate int
1205*0Sstevel@tonic-gate i_ndi_config_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag)
1206*0Sstevel@tonic-gate {
1207*0Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flag))
1208*0Sstevel@tonic-gate 	int rv = DDI_SUCCESS;
1209*0Sstevel@tonic-gate 
1210*0Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
1211*0Sstevel@tonic-gate 
1212*0Sstevel@tonic-gate 	while ((i_ddi_node_state(dip) < state) && (rv == DDI_SUCCESS)) {
1213*0Sstevel@tonic-gate 
1214*0Sstevel@tonic-gate 		/* don't allow any more changes to the device tree */
1215*0Sstevel@tonic-gate 		if (devinfo_freeze) {
1216*0Sstevel@tonic-gate 			rv = DDI_FAILURE;
1217*0Sstevel@tonic-gate 			break;
1218*0Sstevel@tonic-gate 		}
1219*0Sstevel@tonic-gate 
1220*0Sstevel@tonic-gate 		switch (i_ddi_node_state(dip)) {
1221*0Sstevel@tonic-gate 		case DS_PROTO:
1222*0Sstevel@tonic-gate 			/*
1223*0Sstevel@tonic-gate 			 * only caller can reference this node, no external
1224*0Sstevel@tonic-gate 			 * locking needed.
1225*0Sstevel@tonic-gate 			 */
1226*0Sstevel@tonic-gate 			link_node(dip);
1227*0Sstevel@tonic-gate 			i_ddi_set_node_state(dip, DS_LINKED);
1228*0Sstevel@tonic-gate 			break;
1229*0Sstevel@tonic-gate 		case DS_LINKED:
1230*0Sstevel@tonic-gate 			/*
1231*0Sstevel@tonic-gate 			 * Three code path may attempt to bind a node:
1232*0Sstevel@tonic-gate 			 * - boot code
1233*0Sstevel@tonic-gate 			 * - add_drv
1234*0Sstevel@tonic-gate 			 * - hotplug thread
1235*0Sstevel@tonic-gate 			 * Boot code is single threaded, add_drv synchronize
1236*0Sstevel@tonic-gate 			 * on a userland lock, and hotplug synchronize on
1237*0Sstevel@tonic-gate 			 * hotplug_lk. There could be a race between add_drv
1238*0Sstevel@tonic-gate 			 * and hotplug thread. We'll live with this until the
1239*0Sstevel@tonic-gate 			 * conversion to top-down loading.
1240*0Sstevel@tonic-gate 			 */
1241*0Sstevel@tonic-gate 			if ((rv = bind_node(dip)) == DDI_SUCCESS)
1242*0Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_BOUND);
1243*0Sstevel@tonic-gate 			break;
1244*0Sstevel@tonic-gate 		case DS_BOUND:
1245*0Sstevel@tonic-gate 			/*
1246*0Sstevel@tonic-gate 			 * The following transitions synchronizes on the
1247*0Sstevel@tonic-gate 			 * per-driver busy changing flag, since we already
1248*0Sstevel@tonic-gate 			 * have a driver.
1249*0Sstevel@tonic-gate 			 */
1250*0Sstevel@tonic-gate 			if ((rv = init_node(dip)) == DDI_SUCCESS)
1251*0Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_INITIALIZED);
1252*0Sstevel@tonic-gate 			break;
1253*0Sstevel@tonic-gate 		case DS_INITIALIZED:
1254*0Sstevel@tonic-gate 			if ((rv = probe_node(dip)) == DDI_SUCCESS)
1255*0Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_PROBED);
1256*0Sstevel@tonic-gate 			break;
1257*0Sstevel@tonic-gate 		case DS_PROBED:
1258*0Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, 1);
1259*0Sstevel@tonic-gate 			if ((rv = attach_node(dip)) == DDI_SUCCESS)
1260*0Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_ATTACHED);
1261*0Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, -1);
1262*0Sstevel@tonic-gate 			break;
1263*0Sstevel@tonic-gate 		case DS_ATTACHED:
1264*0Sstevel@tonic-gate 			if ((rv = postattach_node(dip)) == DDI_SUCCESS)
1265*0Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_READY);
1266*0Sstevel@tonic-gate 			break;
1267*0Sstevel@tonic-gate 		case DS_READY:
1268*0Sstevel@tonic-gate 			break;
1269*0Sstevel@tonic-gate 		default:
1270*0Sstevel@tonic-gate 			/* should never reach here */
1271*0Sstevel@tonic-gate 			ASSERT("unknown devinfo state");
1272*0Sstevel@tonic-gate 		}
1273*0Sstevel@tonic-gate 	}
1274*0Sstevel@tonic-gate 
1275*0Sstevel@tonic-gate 	if (ddidebug & DDI_AUDIT)
1276*0Sstevel@tonic-gate 		da_log_enter(dip);
1277*0Sstevel@tonic-gate 	return (rv);
1278*0Sstevel@tonic-gate }
1279*0Sstevel@tonic-gate 
1280*0Sstevel@tonic-gate /*
1281*0Sstevel@tonic-gate  * i_ndi_unconfig_node: downgrade dev_info node into a specified state.
1282*0Sstevel@tonic-gate  */
1283*0Sstevel@tonic-gate int
1284*0Sstevel@tonic-gate i_ndi_unconfig_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag)
1285*0Sstevel@tonic-gate {
1286*0Sstevel@tonic-gate 	int rv = DDI_SUCCESS;
1287*0Sstevel@tonic-gate 
1288*0Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
1289*0Sstevel@tonic-gate 
1290*0Sstevel@tonic-gate 	while ((i_ddi_node_state(dip) > state) && (rv == DDI_SUCCESS)) {
1291*0Sstevel@tonic-gate 
1292*0Sstevel@tonic-gate 		/* don't allow any more changes to the device tree */
1293*0Sstevel@tonic-gate 		if (devinfo_freeze) {
1294*0Sstevel@tonic-gate 			rv = DDI_FAILURE;
1295*0Sstevel@tonic-gate 			break;
1296*0Sstevel@tonic-gate 		}
1297*0Sstevel@tonic-gate 
1298*0Sstevel@tonic-gate 		switch (i_ddi_node_state(dip)) {
1299*0Sstevel@tonic-gate 		case DS_PROTO:
1300*0Sstevel@tonic-gate 			break;
1301*0Sstevel@tonic-gate 		case DS_LINKED:
1302*0Sstevel@tonic-gate 			/*
1303*0Sstevel@tonic-gate 			 * Persistent nodes are only removed by hotplug code
1304*0Sstevel@tonic-gate 			 * .conf nodes synchronizes on per-driver list.
1305*0Sstevel@tonic-gate 			 */
1306*0Sstevel@tonic-gate 			if ((rv = unlink_node(dip)) == DDI_SUCCESS)
1307*0Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_PROTO);
1308*0Sstevel@tonic-gate 			break;
1309*0Sstevel@tonic-gate 		case DS_BOUND:
1310*0Sstevel@tonic-gate 			/*
1311*0Sstevel@tonic-gate 			 * The following transitions synchronizes on the
1312*0Sstevel@tonic-gate 			 * per-driver busy changing flag, since we already
1313*0Sstevel@tonic-gate 			 * have a driver.
1314*0Sstevel@tonic-gate 			 */
1315*0Sstevel@tonic-gate 			if ((rv = unbind_node(dip)) == DDI_SUCCESS)
1316*0Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_LINKED);
1317*0Sstevel@tonic-gate 			break;
1318*0Sstevel@tonic-gate 		case DS_INITIALIZED:
1319*0Sstevel@tonic-gate 			if ((rv = uninit_node(dip)) == DDI_SUCCESS)
1320*0Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_BOUND);
1321*0Sstevel@tonic-gate 			break;
1322*0Sstevel@tonic-gate 		case DS_PROBED:
1323*0Sstevel@tonic-gate 			if ((rv = unprobe_node(dip)) == DDI_SUCCESS)
1324*0Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_INITIALIZED);
1325*0Sstevel@tonic-gate 			break;
1326*0Sstevel@tonic-gate 		case DS_ATTACHED:
1327*0Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, 1);
1328*0Sstevel@tonic-gate 			DEVI_SET_DETACHING(dip);
1329*0Sstevel@tonic-gate 			membar_enter();	/* ensure visibility for hold_devi */
1330*0Sstevel@tonic-gate 
1331*0Sstevel@tonic-gate 			if ((rv = detach_node(dip, flag)) == DDI_SUCCESS)
1332*0Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_PROBED);
1333*0Sstevel@tonic-gate 			DEVI_CLR_DETACHING(dip);
1334*0Sstevel@tonic-gate 			atomic_add_long(&devinfo_attach_detach, -1);
1335*0Sstevel@tonic-gate 			break;
1336*0Sstevel@tonic-gate 		case DS_READY:
1337*0Sstevel@tonic-gate 			if ((rv = predetach_node(dip, flag)) == DDI_SUCCESS)
1338*0Sstevel@tonic-gate 				i_ddi_set_node_state(dip, DS_ATTACHED);
1339*0Sstevel@tonic-gate 			break;
1340*0Sstevel@tonic-gate 		default:
1341*0Sstevel@tonic-gate 			ASSERT("unknown devinfo state");
1342*0Sstevel@tonic-gate 		}
1343*0Sstevel@tonic-gate 	}
1344*0Sstevel@tonic-gate 	da_log_enter(dip);
1345*0Sstevel@tonic-gate 	return (rv);
1346*0Sstevel@tonic-gate }
1347*0Sstevel@tonic-gate 
1348*0Sstevel@tonic-gate /*
1349*0Sstevel@tonic-gate  * ddi_initchild: transform node to DS_INITIALIZED state
1350*0Sstevel@tonic-gate  */
1351*0Sstevel@tonic-gate int
1352*0Sstevel@tonic-gate ddi_initchild(dev_info_t *parent, dev_info_t *proto)
1353*0Sstevel@tonic-gate {
1354*0Sstevel@tonic-gate 	int ret, circ;
1355*0Sstevel@tonic-gate 
1356*0Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
1357*0Sstevel@tonic-gate 	ret = i_ndi_config_node(proto, DS_INITIALIZED, 0);
1358*0Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
1359*0Sstevel@tonic-gate 
1360*0Sstevel@tonic-gate 	return (ret);
1361*0Sstevel@tonic-gate }
1362*0Sstevel@tonic-gate 
1363*0Sstevel@tonic-gate /*
1364*0Sstevel@tonic-gate  * ddi_uninitchild: transform node down to DS_BOUND state
1365*0Sstevel@tonic-gate  */
1366*0Sstevel@tonic-gate int
1367*0Sstevel@tonic-gate ddi_uninitchild(dev_info_t *dip)
1368*0Sstevel@tonic-gate {
1369*0Sstevel@tonic-gate 	int ret, circ;
1370*0Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
1371*0Sstevel@tonic-gate 	ASSERT(parent);
1372*0Sstevel@tonic-gate 
1373*0Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
1374*0Sstevel@tonic-gate 	ret = i_ndi_unconfig_node(dip, DS_BOUND, 0);
1375*0Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
1376*0Sstevel@tonic-gate 
1377*0Sstevel@tonic-gate 	return (ret);
1378*0Sstevel@tonic-gate }
1379*0Sstevel@tonic-gate 
1380*0Sstevel@tonic-gate /*
1381*0Sstevel@tonic-gate  * i_ddi_attachchild: transform node to DS_READY state
1382*0Sstevel@tonic-gate  */
1383*0Sstevel@tonic-gate static int
1384*0Sstevel@tonic-gate i_ddi_attachchild(dev_info_t *dip)
1385*0Sstevel@tonic-gate {
1386*0Sstevel@tonic-gate 	int ret, circ;
1387*0Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
1388*0Sstevel@tonic-gate 	ASSERT(parent);
1389*0Sstevel@tonic-gate 
1390*0Sstevel@tonic-gate 	if ((i_ddi_node_state(dip) < DS_BOUND) || DEVI_IS_DEVICE_OFFLINE(dip))
1391*0Sstevel@tonic-gate 		return (DDI_FAILURE);
1392*0Sstevel@tonic-gate 
1393*0Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
1394*0Sstevel@tonic-gate 	ret = i_ndi_config_node(dip, DS_READY, 0);
1395*0Sstevel@tonic-gate 	if (ret == NDI_SUCCESS) {
1396*0Sstevel@tonic-gate 		ret = DDI_SUCCESS;
1397*0Sstevel@tonic-gate 	} else {
1398*0Sstevel@tonic-gate 		/*
1399*0Sstevel@tonic-gate 		 * Take it down to DS_INITIALIZED so pm_pre_probe is run
1400*0Sstevel@tonic-gate 		 * on the next attach
1401*0Sstevel@tonic-gate 		 */
1402*0Sstevel@tonic-gate 		(void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0);
1403*0Sstevel@tonic-gate 		ret = DDI_FAILURE;
1404*0Sstevel@tonic-gate 	}
1405*0Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
1406*0Sstevel@tonic-gate 
1407*0Sstevel@tonic-gate 	return (ret);
1408*0Sstevel@tonic-gate }
1409*0Sstevel@tonic-gate 
1410*0Sstevel@tonic-gate /*
1411*0Sstevel@tonic-gate  * i_ddi_detachchild: transform node down to DS_PROBED state
1412*0Sstevel@tonic-gate  *	If it fails, put it back to DS_READY state.
1413*0Sstevel@tonic-gate  * NOTE: A node that fails detach may be at DS_ATTACHED instead
1414*0Sstevel@tonic-gate  * of DS_READY for a small amount of time.
1415*0Sstevel@tonic-gate  */
1416*0Sstevel@tonic-gate static int
1417*0Sstevel@tonic-gate i_ddi_detachchild(dev_info_t *dip, uint_t flags)
1418*0Sstevel@tonic-gate {
1419*0Sstevel@tonic-gate 	int ret, circ;
1420*0Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
1421*0Sstevel@tonic-gate 	ASSERT(parent);
1422*0Sstevel@tonic-gate 
1423*0Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
1424*0Sstevel@tonic-gate 	ret = i_ndi_unconfig_node(dip, DS_PROBED, flags);
1425*0Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
1426*0Sstevel@tonic-gate 		(void) i_ndi_config_node(dip, DS_READY, 0);
1427*0Sstevel@tonic-gate 	else
1428*0Sstevel@tonic-gate 		/* allow pm_pre_probe to reestablish pm state */
1429*0Sstevel@tonic-gate 		(void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0);
1430*0Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
1431*0Sstevel@tonic-gate 
1432*0Sstevel@tonic-gate 	return (ret);
1433*0Sstevel@tonic-gate }
1434*0Sstevel@tonic-gate 
1435*0Sstevel@tonic-gate /*
1436*0Sstevel@tonic-gate  * Add a child and bind to driver
1437*0Sstevel@tonic-gate  */
1438*0Sstevel@tonic-gate dev_info_t *
1439*0Sstevel@tonic-gate ddi_add_child(dev_info_t *pdip, char *name, uint_t nodeid, uint_t unit)
1440*0Sstevel@tonic-gate {
1441*0Sstevel@tonic-gate 	int circ;
1442*0Sstevel@tonic-gate 	dev_info_t *dip;
1443*0Sstevel@tonic-gate 
1444*0Sstevel@tonic-gate 	/* allocate a new node */
1445*0Sstevel@tonic-gate 	dip = i_ddi_alloc_node(pdip, name, nodeid, (int)unit, NULL, KM_SLEEP);
1446*0Sstevel@tonic-gate 
1447*0Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
1448*0Sstevel@tonic-gate 	(void) i_ndi_config_node(dip, DS_BOUND, 0);
1449*0Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
1450*0Sstevel@tonic-gate 	return (dip);
1451*0Sstevel@tonic-gate }
1452*0Sstevel@tonic-gate 
1453*0Sstevel@tonic-gate /*
1454*0Sstevel@tonic-gate  * ddi_remove_child: remove the dip. The parent must be attached and held
1455*0Sstevel@tonic-gate  */
1456*0Sstevel@tonic-gate int
1457*0Sstevel@tonic-gate ddi_remove_child(dev_info_t *dip, int dummy)
1458*0Sstevel@tonic-gate {
1459*0Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dummy))
1460*0Sstevel@tonic-gate 	int circ, ret;
1461*0Sstevel@tonic-gate 	dev_info_t *parent = ddi_get_parent(dip);
1462*0Sstevel@tonic-gate 	ASSERT(parent);
1463*0Sstevel@tonic-gate 
1464*0Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
1465*0Sstevel@tonic-gate 
1466*0Sstevel@tonic-gate 	/*
1467*0Sstevel@tonic-gate 	 * If we still have children, for example SID nodes marked
1468*0Sstevel@tonic-gate 	 * as persistent but not attached, attempt to remove them.
1469*0Sstevel@tonic-gate 	 */
1470*0Sstevel@tonic-gate 	if (DEVI(dip)->devi_child) {
1471*0Sstevel@tonic-gate 		ret = ndi_devi_unconfig(dip, NDI_DEVI_REMOVE);
1472*0Sstevel@tonic-gate 		if (ret != NDI_SUCCESS) {
1473*0Sstevel@tonic-gate 			ndi_devi_exit(parent, circ);
1474*0Sstevel@tonic-gate 			return (DDI_FAILURE);
1475*0Sstevel@tonic-gate 		}
1476*0Sstevel@tonic-gate 		ASSERT(DEVI(dip)->devi_child == NULL);
1477*0Sstevel@tonic-gate 	}
1478*0Sstevel@tonic-gate 
1479*0Sstevel@tonic-gate 	ret = i_ndi_unconfig_node(dip, DS_PROTO, 0);
1480*0Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
1481*0Sstevel@tonic-gate 
1482*0Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
1483*0Sstevel@tonic-gate 		return (ret);
1484*0Sstevel@tonic-gate 
1485*0Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) == DS_PROTO);
1486*0Sstevel@tonic-gate 	i_ddi_free_node(dip);
1487*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
1488*0Sstevel@tonic-gate }
1489*0Sstevel@tonic-gate 
1490*0Sstevel@tonic-gate /*
1491*0Sstevel@tonic-gate  * NDI wrappers for ref counting, node allocation, and transitions
1492*0Sstevel@tonic-gate  */
1493*0Sstevel@tonic-gate 
1494*0Sstevel@tonic-gate /*
1495*0Sstevel@tonic-gate  * Hold/release the devinfo node itself.
1496*0Sstevel@tonic-gate  * Caller is assumed to prevent the devi from detaching during this call
1497*0Sstevel@tonic-gate  */
1498*0Sstevel@tonic-gate void
1499*0Sstevel@tonic-gate ndi_hold_devi(dev_info_t *dip)
1500*0Sstevel@tonic-gate {
1501*0Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
1502*0Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_ref >= 0);
1503*0Sstevel@tonic-gate 	DEVI(dip)->devi_ref++;
1504*0Sstevel@tonic-gate 	membar_enter();			/* make sure stores are flushed */
1505*0Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
1506*0Sstevel@tonic-gate }
1507*0Sstevel@tonic-gate 
1508*0Sstevel@tonic-gate void
1509*0Sstevel@tonic-gate ndi_rele_devi(dev_info_t *dip)
1510*0Sstevel@tonic-gate {
1511*0Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_ref > 0);
1512*0Sstevel@tonic-gate 
1513*0Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
1514*0Sstevel@tonic-gate 	DEVI(dip)->devi_ref--;
1515*0Sstevel@tonic-gate 	membar_enter();			/* make sure stores are flushed */
1516*0Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
1517*0Sstevel@tonic-gate }
1518*0Sstevel@tonic-gate 
1519*0Sstevel@tonic-gate int
1520*0Sstevel@tonic-gate e_ddi_devi_holdcnt(dev_info_t *dip)
1521*0Sstevel@tonic-gate {
1522*0Sstevel@tonic-gate 	return (DEVI(dip)->devi_ref);
1523*0Sstevel@tonic-gate }
1524*0Sstevel@tonic-gate 
1525*0Sstevel@tonic-gate /*
1526*0Sstevel@tonic-gate  * Hold/release the driver the devinfo node is bound to.
1527*0Sstevel@tonic-gate  */
1528*0Sstevel@tonic-gate struct dev_ops *
1529*0Sstevel@tonic-gate ndi_hold_driver(dev_info_t *dip)
1530*0Sstevel@tonic-gate {
1531*0Sstevel@tonic-gate 	if (i_ddi_node_state(dip) < DS_BOUND)
1532*0Sstevel@tonic-gate 		return (NULL);
1533*0Sstevel@tonic-gate 
1534*0Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_major != -1);
1535*0Sstevel@tonic-gate 	return (mod_hold_dev_by_major(DEVI(dip)->devi_major));
1536*0Sstevel@tonic-gate }
1537*0Sstevel@tonic-gate 
1538*0Sstevel@tonic-gate void
1539*0Sstevel@tonic-gate ndi_rele_driver(dev_info_t *dip)
1540*0Sstevel@tonic-gate {
1541*0Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) >= DS_BOUND);
1542*0Sstevel@tonic-gate 	mod_rele_dev_by_major(DEVI(dip)->devi_major);
1543*0Sstevel@tonic-gate }
1544*0Sstevel@tonic-gate 
1545*0Sstevel@tonic-gate /*
1546*0Sstevel@tonic-gate  * Single thread entry into devinfo node for modifying its children.
1547*0Sstevel@tonic-gate  * To verify in ASSERTS use DEVI_BUSY_OWNED macro.
1548*0Sstevel@tonic-gate  */
1549*0Sstevel@tonic-gate void
1550*0Sstevel@tonic-gate ndi_devi_enter(dev_info_t *dip, int *circular)
1551*0Sstevel@tonic-gate {
1552*0Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
1553*0Sstevel@tonic-gate 	ASSERT(dip != NULL);
1554*0Sstevel@tonic-gate 
1555*0Sstevel@tonic-gate 	mutex_enter(&devi->devi_lock);
1556*0Sstevel@tonic-gate 	if (devi->devi_busy_thread == curthread) {
1557*0Sstevel@tonic-gate 		devi->devi_circular++;
1558*0Sstevel@tonic-gate 	} else {
1559*0Sstevel@tonic-gate 		while (DEVI_BUSY_CHANGING(devi) && !panicstr)
1560*0Sstevel@tonic-gate 			cv_wait(&(devi->devi_cv), &(devi->devi_lock));
1561*0Sstevel@tonic-gate 		if (panicstr) {
1562*0Sstevel@tonic-gate 			mutex_exit(&devi->devi_lock);
1563*0Sstevel@tonic-gate 			return;
1564*0Sstevel@tonic-gate 		}
1565*0Sstevel@tonic-gate 		devi->devi_flags |= DEVI_BUSY;
1566*0Sstevel@tonic-gate 		devi->devi_busy_thread = curthread;
1567*0Sstevel@tonic-gate 	}
1568*0Sstevel@tonic-gate 	*circular = devi->devi_circular;
1569*0Sstevel@tonic-gate 	mutex_exit(&devi->devi_lock);
1570*0Sstevel@tonic-gate }
1571*0Sstevel@tonic-gate 
1572*0Sstevel@tonic-gate /*
1573*0Sstevel@tonic-gate  * Release ndi_devi_enter or successful ndi_devi_tryenter.
1574*0Sstevel@tonic-gate  */
1575*0Sstevel@tonic-gate void
1576*0Sstevel@tonic-gate ndi_devi_exit(dev_info_t *dip, int circular)
1577*0Sstevel@tonic-gate {
1578*0Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
1579*0Sstevel@tonic-gate 	ASSERT(dip != NULL);
1580*0Sstevel@tonic-gate 
1581*0Sstevel@tonic-gate 	if (panicstr)
1582*0Sstevel@tonic-gate 		return;
1583*0Sstevel@tonic-gate 
1584*0Sstevel@tonic-gate 	mutex_enter(&(devi->devi_lock));
1585*0Sstevel@tonic-gate 	if (circular != 0) {
1586*0Sstevel@tonic-gate 		devi->devi_circular--;
1587*0Sstevel@tonic-gate 	} else {
1588*0Sstevel@tonic-gate 		devi->devi_flags &= ~DEVI_BUSY;
1589*0Sstevel@tonic-gate 		ASSERT(devi->devi_busy_thread == curthread);
1590*0Sstevel@tonic-gate 		devi->devi_busy_thread = NULL;
1591*0Sstevel@tonic-gate 		cv_broadcast(&(devi->devi_cv));
1592*0Sstevel@tonic-gate 	}
1593*0Sstevel@tonic-gate 	mutex_exit(&(devi->devi_lock));
1594*0Sstevel@tonic-gate }
1595*0Sstevel@tonic-gate 
1596*0Sstevel@tonic-gate /*
1597*0Sstevel@tonic-gate  * Attempt to single thread entry into devinfo node for modifying its children.
1598*0Sstevel@tonic-gate  */
1599*0Sstevel@tonic-gate int
1600*0Sstevel@tonic-gate ndi_devi_tryenter(dev_info_t *dip, int *circular)
1601*0Sstevel@tonic-gate {
1602*0Sstevel@tonic-gate 	int rval = 1;		   /* assume we enter */
1603*0Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
1604*0Sstevel@tonic-gate 	ASSERT(dip != NULL);
1605*0Sstevel@tonic-gate 
1606*0Sstevel@tonic-gate 	mutex_enter(&devi->devi_lock);
1607*0Sstevel@tonic-gate 	if (devi->devi_busy_thread == (void *)curthread) {
1608*0Sstevel@tonic-gate 		devi->devi_circular++;
1609*0Sstevel@tonic-gate 	} else {
1610*0Sstevel@tonic-gate 		if (!DEVI_BUSY_CHANGING(devi)) {
1611*0Sstevel@tonic-gate 			devi->devi_flags |= DEVI_BUSY;
1612*0Sstevel@tonic-gate 			devi->devi_busy_thread = (void *)curthread;
1613*0Sstevel@tonic-gate 		} else {
1614*0Sstevel@tonic-gate 			rval = 0;	/* devi is busy */
1615*0Sstevel@tonic-gate 		}
1616*0Sstevel@tonic-gate 	}
1617*0Sstevel@tonic-gate 	*circular = devi->devi_circular;
1618*0Sstevel@tonic-gate 	mutex_exit(&devi->devi_lock);
1619*0Sstevel@tonic-gate 	return (rval);
1620*0Sstevel@tonic-gate }
1621*0Sstevel@tonic-gate 
1622*0Sstevel@tonic-gate /*
1623*0Sstevel@tonic-gate  * Allocate and initialize a new dev_info structure.
1624*0Sstevel@tonic-gate  *
1625*0Sstevel@tonic-gate  * This routine may be called at interrupt time by a nexus in
1626*0Sstevel@tonic-gate  * response to a hotplug event, therefore memory allocations are
1627*0Sstevel@tonic-gate  * not allowed to sleep.
1628*0Sstevel@tonic-gate  */
1629*0Sstevel@tonic-gate int
1630*0Sstevel@tonic-gate ndi_devi_alloc(dev_info_t *parent, char *node_name, dnode_t nodeid,
1631*0Sstevel@tonic-gate     dev_info_t **ret_dip)
1632*0Sstevel@tonic-gate {
1633*0Sstevel@tonic-gate 	ASSERT(node_name != NULL);
1634*0Sstevel@tonic-gate 	ASSERT(ret_dip != NULL);
1635*0Sstevel@tonic-gate 
1636*0Sstevel@tonic-gate 	*ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL,
1637*0Sstevel@tonic-gate 	    KM_NOSLEEP);
1638*0Sstevel@tonic-gate 	if (*ret_dip == NULL) {
1639*0Sstevel@tonic-gate 		return (NDI_NOMEM);
1640*0Sstevel@tonic-gate 	}
1641*0Sstevel@tonic-gate 
1642*0Sstevel@tonic-gate 	return (NDI_SUCCESS);
1643*0Sstevel@tonic-gate }
1644*0Sstevel@tonic-gate 
1645*0Sstevel@tonic-gate /*
1646*0Sstevel@tonic-gate  * Allocate and initialize a new dev_info structure
1647*0Sstevel@tonic-gate  * This routine may sleep and should not be called at interrupt time
1648*0Sstevel@tonic-gate  */
1649*0Sstevel@tonic-gate void
1650*0Sstevel@tonic-gate ndi_devi_alloc_sleep(dev_info_t *parent, char *node_name, dnode_t nodeid,
1651*0Sstevel@tonic-gate     dev_info_t **ret_dip)
1652*0Sstevel@tonic-gate {
1653*0Sstevel@tonic-gate 	ASSERT(node_name != NULL);
1654*0Sstevel@tonic-gate 	ASSERT(ret_dip != NULL);
1655*0Sstevel@tonic-gate 
1656*0Sstevel@tonic-gate 	*ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL,
1657*0Sstevel@tonic-gate 	    KM_SLEEP);
1658*0Sstevel@tonic-gate 	ASSERT(*ret_dip);
1659*0Sstevel@tonic-gate }
1660*0Sstevel@tonic-gate 
1661*0Sstevel@tonic-gate /*
1662*0Sstevel@tonic-gate  * Remove an initialized (but not yet attached) dev_info
1663*0Sstevel@tonic-gate  * node from it's parent.
1664*0Sstevel@tonic-gate  */
1665*0Sstevel@tonic-gate int
1666*0Sstevel@tonic-gate ndi_devi_free(dev_info_t *dip)
1667*0Sstevel@tonic-gate {
1668*0Sstevel@tonic-gate 	ASSERT(dip != NULL);
1669*0Sstevel@tonic-gate 
1670*0Sstevel@tonic-gate 	if (i_ddi_node_state(dip) >= DS_INITIALIZED)
1671*0Sstevel@tonic-gate 		return (DDI_FAILURE);
1672*0Sstevel@tonic-gate 
1673*0Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_free: %s%d (%p)\n",
1674*0Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
1675*0Sstevel@tonic-gate 
1676*0Sstevel@tonic-gate 	(void) ddi_remove_child(dip, 0);
1677*0Sstevel@tonic-gate 
1678*0Sstevel@tonic-gate 	return (NDI_SUCCESS);
1679*0Sstevel@tonic-gate }
1680*0Sstevel@tonic-gate 
1681*0Sstevel@tonic-gate /*
1682*0Sstevel@tonic-gate  * ndi_devi_bind_driver() binds a driver to a given device. If it fails
1683*0Sstevel@tonic-gate  * to bind the driver, it returns an appropriate error back. Some drivers
1684*0Sstevel@tonic-gate  * may want to know if the actually failed to bind.
1685*0Sstevel@tonic-gate  */
1686*0Sstevel@tonic-gate int
1687*0Sstevel@tonic-gate ndi_devi_bind_driver(dev_info_t *dip, uint_t flags)
1688*0Sstevel@tonic-gate {
1689*0Sstevel@tonic-gate 	int ret = NDI_FAILURE;
1690*0Sstevel@tonic-gate 	int circ;
1691*0Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
1692*0Sstevel@tonic-gate 	ASSERT(pdip);
1693*0Sstevel@tonic-gate 
1694*0Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
1695*0Sstevel@tonic-gate 	    "ndi_devi_bind_driver: %s%d (%p) flags: %x\n",
1696*0Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
1697*0Sstevel@tonic-gate 
1698*0Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
1699*0Sstevel@tonic-gate 	if (i_ndi_config_node(dip, DS_BOUND, flags) == DDI_SUCCESS)
1700*0Sstevel@tonic-gate 		ret = NDI_SUCCESS;
1701*0Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
1702*0Sstevel@tonic-gate 
1703*0Sstevel@tonic-gate 	return (ret);
1704*0Sstevel@tonic-gate }
1705*0Sstevel@tonic-gate 
1706*0Sstevel@tonic-gate /*
1707*0Sstevel@tonic-gate  * ndi_devi_unbind_driver: unbind the dip
1708*0Sstevel@tonic-gate  */
1709*0Sstevel@tonic-gate static int
1710*0Sstevel@tonic-gate ndi_devi_unbind_driver(dev_info_t *dip)
1711*0Sstevel@tonic-gate {
1712*0Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
1713*0Sstevel@tonic-gate 
1714*0Sstevel@tonic-gate 	return (i_ndi_unconfig_node(dip, DS_LINKED, 0));
1715*0Sstevel@tonic-gate }
1716*0Sstevel@tonic-gate 
1717*0Sstevel@tonic-gate /*
1718*0Sstevel@tonic-gate  * Misc. help routines called by framework only
1719*0Sstevel@tonic-gate  */
1720*0Sstevel@tonic-gate 
1721*0Sstevel@tonic-gate /*
1722*0Sstevel@tonic-gate  * Get the state of node
1723*0Sstevel@tonic-gate  */
1724*0Sstevel@tonic-gate ddi_node_state_t
1725*0Sstevel@tonic-gate i_ddi_node_state(dev_info_t *dip)
1726*0Sstevel@tonic-gate {
1727*0Sstevel@tonic-gate 	return (DEVI(dip)->devi_node_state);
1728*0Sstevel@tonic-gate }
1729*0Sstevel@tonic-gate 
1730*0Sstevel@tonic-gate /*
1731*0Sstevel@tonic-gate  * Set the state of node
1732*0Sstevel@tonic-gate  */
1733*0Sstevel@tonic-gate void
1734*0Sstevel@tonic-gate i_ddi_set_node_state(dev_info_t *dip, ddi_node_state_t state)
1735*0Sstevel@tonic-gate {
1736*0Sstevel@tonic-gate 	DEVI(dip)->devi_node_state = state;
1737*0Sstevel@tonic-gate 	membar_enter();			/* make sure stores are flushed */
1738*0Sstevel@tonic-gate }
1739*0Sstevel@tonic-gate 
1740*0Sstevel@tonic-gate /*
1741*0Sstevel@tonic-gate  * Common function for finding a node in a sibling list given name and addr.
1742*0Sstevel@tonic-gate  *
1743*0Sstevel@tonic-gate  * By default, name is matched with devi_node_name. The following
1744*0Sstevel@tonic-gate  * alternative match strategies are supported:
1745*0Sstevel@tonic-gate  *
1746*0Sstevel@tonic-gate  *	FIND_NAME_BY_DRIVER: A match on driver name bound to node is conducted.
1747*0Sstevel@tonic-gate  *		This support is used for support of OBP generic names and
1748*0Sstevel@tonic-gate  *		for the conversion from driver names to generic names.  When
1749*0Sstevel@tonic-gate  *		more consistency in the generic name environment is achieved
1750*0Sstevel@tonic-gate  *		(and not needed for upgrade) this support can be removed.
1751*0Sstevel@tonic-gate  *
1752*0Sstevel@tonic-gate  * If a child is not named (dev_addr == NULL), there are three
1753*0Sstevel@tonic-gate  * possible actions:
1754*0Sstevel@tonic-gate  *
1755*0Sstevel@tonic-gate  *	(1) skip it
1756*0Sstevel@tonic-gate  *	(2) FIND_ADDR_BY_INIT: bring child to DS_INITIALIZED state
1757*0Sstevel@tonic-gate  *	(3) FIND_ADDR_BY_CALLBACK: use a caller-supplied callback function
1758*0Sstevel@tonic-gate  */
1759*0Sstevel@tonic-gate #define	FIND_NAME_BY_DRIVER	0x01
1760*0Sstevel@tonic-gate #define	FIND_ADDR_BY_INIT	0x10
1761*0Sstevel@tonic-gate #define	FIND_ADDR_BY_CALLBACK	0x20
1762*0Sstevel@tonic-gate 
1763*0Sstevel@tonic-gate static dev_info_t *
1764*0Sstevel@tonic-gate find_sibling(dev_info_t *head, char *cname, char *caddr, uint_t flag,
1765*0Sstevel@tonic-gate     int (*callback)(dev_info_t *, char *, int))
1766*0Sstevel@tonic-gate {
1767*0Sstevel@tonic-gate 	dev_info_t	*dip;
1768*0Sstevel@tonic-gate 	char		*addr, *buf;
1769*0Sstevel@tonic-gate 	major_t		major;
1770*0Sstevel@tonic-gate 
1771*0Sstevel@tonic-gate 	/* only one way to name a node */
1772*0Sstevel@tonic-gate 	ASSERT(((flag & FIND_ADDR_BY_INIT) == 0) ||
1773*0Sstevel@tonic-gate 	    ((flag & FIND_ADDR_BY_CALLBACK) == 0));
1774*0Sstevel@tonic-gate 
1775*0Sstevel@tonic-gate 	if (flag & FIND_NAME_BY_DRIVER) {
1776*0Sstevel@tonic-gate 		major = ddi_name_to_major(cname);
1777*0Sstevel@tonic-gate 		if (major == (major_t)-1)
1778*0Sstevel@tonic-gate 			return (NULL);
1779*0Sstevel@tonic-gate 	}
1780*0Sstevel@tonic-gate 
1781*0Sstevel@tonic-gate 	/* preallocate buffer of naming node by callback */
1782*0Sstevel@tonic-gate 	if (flag & FIND_ADDR_BY_CALLBACK)
1783*0Sstevel@tonic-gate 		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1784*0Sstevel@tonic-gate 
1785*0Sstevel@tonic-gate 	/*
1786*0Sstevel@tonic-gate 	 * Walk the child list to find a match
1787*0Sstevel@tonic-gate 	 */
1788*0Sstevel@tonic-gate 
1789*0Sstevel@tonic-gate 	for (dip = head; dip; dip = ddi_get_next_sibling(dip)) {
1790*0Sstevel@tonic-gate 		if (flag & FIND_NAME_BY_DRIVER) {
1791*0Sstevel@tonic-gate 			/* match driver major */
1792*0Sstevel@tonic-gate 			if (DEVI(dip)->devi_major != major)
1793*0Sstevel@tonic-gate 				continue;
1794*0Sstevel@tonic-gate 		} else {
1795*0Sstevel@tonic-gate 			/* match node name */
1796*0Sstevel@tonic-gate 			if (strcmp(cname, DEVI(dip)->devi_node_name) != 0)
1797*0Sstevel@tonic-gate 				continue;
1798*0Sstevel@tonic-gate 		}
1799*0Sstevel@tonic-gate 
1800*0Sstevel@tonic-gate 		if ((addr = DEVI(dip)->devi_addr) == NULL) {
1801*0Sstevel@tonic-gate 			/* name the child based on the flag */
1802*0Sstevel@tonic-gate 			if (flag & FIND_ADDR_BY_INIT) {
1803*0Sstevel@tonic-gate 				if (ddi_initchild(ddi_get_parent(dip), dip)
1804*0Sstevel@tonic-gate 				    != DDI_SUCCESS)
1805*0Sstevel@tonic-gate 					continue;
1806*0Sstevel@tonic-gate 				addr = DEVI(dip)->devi_addr;
1807*0Sstevel@tonic-gate 			} else if (flag & FIND_ADDR_BY_CALLBACK) {
1808*0Sstevel@tonic-gate 				if ((callback == NULL) || (callback(
1809*0Sstevel@tonic-gate 				    dip, buf, MAXNAMELEN) != DDI_SUCCESS))
1810*0Sstevel@tonic-gate 					continue;
1811*0Sstevel@tonic-gate 				addr = buf;
1812*0Sstevel@tonic-gate 			} else {
1813*0Sstevel@tonic-gate 				continue;	/* skip */
1814*0Sstevel@tonic-gate 			}
1815*0Sstevel@tonic-gate 		}
1816*0Sstevel@tonic-gate 
1817*0Sstevel@tonic-gate 		/* match addr */
1818*0Sstevel@tonic-gate 		ASSERT(addr != NULL);
1819*0Sstevel@tonic-gate 		if (strcmp(caddr, addr) == 0)
1820*0Sstevel@tonic-gate 			break;	/* node found */
1821*0Sstevel@tonic-gate 
1822*0Sstevel@tonic-gate 	}
1823*0Sstevel@tonic-gate 	if (flag & FIND_ADDR_BY_CALLBACK)
1824*0Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
1825*0Sstevel@tonic-gate 	return (dip);
1826*0Sstevel@tonic-gate }
1827*0Sstevel@tonic-gate 
1828*0Sstevel@tonic-gate /*
1829*0Sstevel@tonic-gate  * Find child of pdip with name: cname@caddr
1830*0Sstevel@tonic-gate  * Called by init_node() to look for duplicate nodes
1831*0Sstevel@tonic-gate  */
1832*0Sstevel@tonic-gate static dev_info_t *
1833*0Sstevel@tonic-gate find_duplicate_child(dev_info_t *pdip, dev_info_t *dip)
1834*0Sstevel@tonic-gate {
1835*0Sstevel@tonic-gate 	dev_info_t *dup;
1836*0Sstevel@tonic-gate 	char *cname = DEVI(dip)->devi_node_name;
1837*0Sstevel@tonic-gate 	char *caddr = DEVI(dip)->devi_addr;
1838*0Sstevel@tonic-gate 
1839*0Sstevel@tonic-gate 	/* search nodes before dip */
1840*0Sstevel@tonic-gate 	dup = find_sibling(ddi_get_child(pdip), cname, caddr, 0, NULL);
1841*0Sstevel@tonic-gate 	if (dup != dip)
1842*0Sstevel@tonic-gate 		return (dup);
1843*0Sstevel@tonic-gate 
1844*0Sstevel@tonic-gate 	/*
1845*0Sstevel@tonic-gate 	 * search nodes after dip; normally this is not needed,
1846*0Sstevel@tonic-gate 	 */
1847*0Sstevel@tonic-gate 	return (find_sibling(ddi_get_next_sibling(dip), cname, caddr,
1848*0Sstevel@tonic-gate 	    0, NULL));
1849*0Sstevel@tonic-gate }
1850*0Sstevel@tonic-gate 
1851*0Sstevel@tonic-gate /*
1852*0Sstevel@tonic-gate  * Find a child of a given name and address, using a callback to name
1853*0Sstevel@tonic-gate  * unnamed children. cname is the binding name.
1854*0Sstevel@tonic-gate  */
1855*0Sstevel@tonic-gate static dev_info_t *
1856*0Sstevel@tonic-gate find_child_by_callback(dev_info_t *pdip, char *cname, char *caddr,
1857*0Sstevel@tonic-gate     int (*name_node)(dev_info_t *, char *, int))
1858*0Sstevel@tonic-gate {
1859*0Sstevel@tonic-gate 	return (find_sibling(ddi_get_child(pdip), cname, caddr,
1860*0Sstevel@tonic-gate 	    FIND_NAME_BY_DRIVER|FIND_ADDR_BY_CALLBACK, name_node));
1861*0Sstevel@tonic-gate }
1862*0Sstevel@tonic-gate 
1863*0Sstevel@tonic-gate /*
1864*0Sstevel@tonic-gate  * Find a child of a given name and address, invoking initchild to name
1865*0Sstevel@tonic-gate  * unnamed children. cname is the node name.
1866*0Sstevel@tonic-gate  */
1867*0Sstevel@tonic-gate static dev_info_t *
1868*0Sstevel@tonic-gate find_child_by_name(dev_info_t *pdip, char *cname, char *caddr)
1869*0Sstevel@tonic-gate {
1870*0Sstevel@tonic-gate 	dev_info_t	*dip;
1871*0Sstevel@tonic-gate 
1872*0Sstevel@tonic-gate 	/* attempt search without changing state of preceeding siblings */
1873*0Sstevel@tonic-gate 	dip = find_sibling(ddi_get_child(pdip), cname, caddr, 0, NULL);
1874*0Sstevel@tonic-gate 	if (dip)
1875*0Sstevel@tonic-gate 		return (dip);
1876*0Sstevel@tonic-gate 
1877*0Sstevel@tonic-gate 	return (find_sibling(ddi_get_child(pdip), cname, caddr,
1878*0Sstevel@tonic-gate 	    FIND_ADDR_BY_INIT, NULL));
1879*0Sstevel@tonic-gate }
1880*0Sstevel@tonic-gate 
1881*0Sstevel@tonic-gate /*
1882*0Sstevel@tonic-gate  * Find a child of a given name and address, invoking initchild to name
1883*0Sstevel@tonic-gate  * unnamed children. cname is the node name.
1884*0Sstevel@tonic-gate  */
1885*0Sstevel@tonic-gate static dev_info_t *
1886*0Sstevel@tonic-gate find_child_by_driver(dev_info_t *pdip, char *cname, char *caddr)
1887*0Sstevel@tonic-gate {
1888*0Sstevel@tonic-gate 	dev_info_t	*dip;
1889*0Sstevel@tonic-gate 
1890*0Sstevel@tonic-gate 	/* attempt search without changing state of preceeding siblings */
1891*0Sstevel@tonic-gate 	dip = find_sibling(ddi_get_child(pdip), cname, caddr,
1892*0Sstevel@tonic-gate 	    FIND_NAME_BY_DRIVER, NULL);
1893*0Sstevel@tonic-gate 	if (dip)
1894*0Sstevel@tonic-gate 		return (dip);
1895*0Sstevel@tonic-gate 
1896*0Sstevel@tonic-gate 	return (find_sibling(ddi_get_child(pdip), cname, caddr,
1897*0Sstevel@tonic-gate 	    FIND_NAME_BY_DRIVER|FIND_ADDR_BY_INIT, NULL));
1898*0Sstevel@tonic-gate }
1899*0Sstevel@tonic-gate 
1900*0Sstevel@tonic-gate /*
1901*0Sstevel@tonic-gate  * Deleting a property list. Take care, since some property structures
1902*0Sstevel@tonic-gate  * may not be fully built.
1903*0Sstevel@tonic-gate  */
1904*0Sstevel@tonic-gate void
1905*0Sstevel@tonic-gate i_ddi_prop_list_delete(ddi_prop_t *prop)
1906*0Sstevel@tonic-gate {
1907*0Sstevel@tonic-gate 	while (prop) {
1908*0Sstevel@tonic-gate 		ddi_prop_t *next = prop->prop_next;
1909*0Sstevel@tonic-gate 		if (prop->prop_name)
1910*0Sstevel@tonic-gate 			kmem_free(prop->prop_name, strlen(prop->prop_name) + 1);
1911*0Sstevel@tonic-gate 		if ((prop->prop_len != 0) && prop->prop_val)
1912*0Sstevel@tonic-gate 			kmem_free(prop->prop_val, prop->prop_len);
1913*0Sstevel@tonic-gate 		kmem_free(prop, sizeof (struct ddi_prop));
1914*0Sstevel@tonic-gate 		prop = next;
1915*0Sstevel@tonic-gate 	}
1916*0Sstevel@tonic-gate }
1917*0Sstevel@tonic-gate 
1918*0Sstevel@tonic-gate /*
1919*0Sstevel@tonic-gate  * Duplicate property list
1920*0Sstevel@tonic-gate  */
1921*0Sstevel@tonic-gate ddi_prop_t *
1922*0Sstevel@tonic-gate i_ddi_prop_list_dup(ddi_prop_t *prop, uint_t flag)
1923*0Sstevel@tonic-gate {
1924*0Sstevel@tonic-gate 	ddi_prop_t *result, *prev, *copy;
1925*0Sstevel@tonic-gate 
1926*0Sstevel@tonic-gate 	if (prop == NULL)
1927*0Sstevel@tonic-gate 		return (NULL);
1928*0Sstevel@tonic-gate 
1929*0Sstevel@tonic-gate 	result = prev = NULL;
1930*0Sstevel@tonic-gate 	for (; prop != NULL; prop = prop->prop_next) {
1931*0Sstevel@tonic-gate 		ASSERT(prop->prop_name != NULL);
1932*0Sstevel@tonic-gate 		copy = kmem_zalloc(sizeof (struct ddi_prop), flag);
1933*0Sstevel@tonic-gate 		if (copy == NULL)
1934*0Sstevel@tonic-gate 			goto fail;
1935*0Sstevel@tonic-gate 
1936*0Sstevel@tonic-gate 		copy->prop_dev = prop->prop_dev;
1937*0Sstevel@tonic-gate 		copy->prop_flags = prop->prop_flags;
1938*0Sstevel@tonic-gate 		copy->prop_name = i_ddi_strdup(prop->prop_name, flag);
1939*0Sstevel@tonic-gate 		if (copy->prop_name == NULL)
1940*0Sstevel@tonic-gate 			goto fail;
1941*0Sstevel@tonic-gate 
1942*0Sstevel@tonic-gate 		if ((copy->prop_len = prop->prop_len) != 0) {
1943*0Sstevel@tonic-gate 			copy->prop_val = kmem_zalloc(prop->prop_len, flag);
1944*0Sstevel@tonic-gate 			if (copy->prop_val == NULL)
1945*0Sstevel@tonic-gate 				goto fail;
1946*0Sstevel@tonic-gate 
1947*0Sstevel@tonic-gate 			bcopy(prop->prop_val, copy->prop_val, prop->prop_len);
1948*0Sstevel@tonic-gate 		}
1949*0Sstevel@tonic-gate 
1950*0Sstevel@tonic-gate 		if (prev == NULL)
1951*0Sstevel@tonic-gate 			result = prev = copy;
1952*0Sstevel@tonic-gate 		else
1953*0Sstevel@tonic-gate 			prev->prop_next = copy;
1954*0Sstevel@tonic-gate 		prev = copy;
1955*0Sstevel@tonic-gate 	}
1956*0Sstevel@tonic-gate 	return (result);
1957*0Sstevel@tonic-gate 
1958*0Sstevel@tonic-gate fail:
1959*0Sstevel@tonic-gate 	i_ddi_prop_list_delete(result);
1960*0Sstevel@tonic-gate 	return (NULL);
1961*0Sstevel@tonic-gate }
1962*0Sstevel@tonic-gate 
1963*0Sstevel@tonic-gate /*
1964*0Sstevel@tonic-gate  * Create a reference property list, currently used only for
1965*0Sstevel@tonic-gate  * driver global properties. Created with ref count of 1.
1966*0Sstevel@tonic-gate  */
1967*0Sstevel@tonic-gate ddi_prop_list_t *
1968*0Sstevel@tonic-gate i_ddi_prop_list_create(ddi_prop_t *props)
1969*0Sstevel@tonic-gate {
1970*0Sstevel@tonic-gate 	ddi_prop_list_t *list = kmem_alloc(sizeof (*list), KM_SLEEP);
1971*0Sstevel@tonic-gate 	list->prop_list = props;
1972*0Sstevel@tonic-gate 	list->prop_ref = 1;
1973*0Sstevel@tonic-gate 	return (list);
1974*0Sstevel@tonic-gate }
1975*0Sstevel@tonic-gate 
1976*0Sstevel@tonic-gate /*
1977*0Sstevel@tonic-gate  * Increment/decrement reference count. The reference is
1978*0Sstevel@tonic-gate  * protected by dn_lock. The only interfaces modifying
1979*0Sstevel@tonic-gate  * dn_global_prop_ptr is in impl_make[free]_parlist().
1980*0Sstevel@tonic-gate  */
1981*0Sstevel@tonic-gate void
1982*0Sstevel@tonic-gate i_ddi_prop_list_hold(ddi_prop_list_t *prop_list, struct devnames *dnp)
1983*0Sstevel@tonic-gate {
1984*0Sstevel@tonic-gate 	ASSERT(prop_list->prop_ref >= 0);
1985*0Sstevel@tonic-gate 	ASSERT(mutex_owned(&dnp->dn_lock));
1986*0Sstevel@tonic-gate 	prop_list->prop_ref++;
1987*0Sstevel@tonic-gate }
1988*0Sstevel@tonic-gate 
1989*0Sstevel@tonic-gate void
1990*0Sstevel@tonic-gate i_ddi_prop_list_rele(ddi_prop_list_t *prop_list, struct devnames *dnp)
1991*0Sstevel@tonic-gate {
1992*0Sstevel@tonic-gate 	ASSERT(prop_list->prop_ref > 0);
1993*0Sstevel@tonic-gate 	ASSERT(mutex_owned(&dnp->dn_lock));
1994*0Sstevel@tonic-gate 	prop_list->prop_ref--;
1995*0Sstevel@tonic-gate 
1996*0Sstevel@tonic-gate 	if (prop_list->prop_ref == 0) {
1997*0Sstevel@tonic-gate 		i_ddi_prop_list_delete(prop_list->prop_list);
1998*0Sstevel@tonic-gate 		kmem_free(prop_list, sizeof (*prop_list));
1999*0Sstevel@tonic-gate 	}
2000*0Sstevel@tonic-gate }
2001*0Sstevel@tonic-gate 
2002*0Sstevel@tonic-gate /*
2003*0Sstevel@tonic-gate  * Free table of classes by drivers
2004*0Sstevel@tonic-gate  */
2005*0Sstevel@tonic-gate void
2006*0Sstevel@tonic-gate i_ddi_free_exported_classes(char **classes, int n)
2007*0Sstevel@tonic-gate {
2008*0Sstevel@tonic-gate 	if ((n == 0) || (classes == NULL))
2009*0Sstevel@tonic-gate 		return;
2010*0Sstevel@tonic-gate 
2011*0Sstevel@tonic-gate 	kmem_free(classes, n * sizeof (char *));
2012*0Sstevel@tonic-gate }
2013*0Sstevel@tonic-gate 
2014*0Sstevel@tonic-gate /*
2015*0Sstevel@tonic-gate  * Get all classes exported by dip
2016*0Sstevel@tonic-gate  */
2017*0Sstevel@tonic-gate int
2018*0Sstevel@tonic-gate i_ddi_get_exported_classes(dev_info_t *dip, char ***classes)
2019*0Sstevel@tonic-gate {
2020*0Sstevel@tonic-gate 	extern void lock_hw_class_list();
2021*0Sstevel@tonic-gate 	extern void unlock_hw_class_list();
2022*0Sstevel@tonic-gate 	extern int get_class(const char *, char **);
2023*0Sstevel@tonic-gate 
2024*0Sstevel@tonic-gate 	static char *rootclass = "root";
2025*0Sstevel@tonic-gate 	int n = 0, nclass = 0;
2026*0Sstevel@tonic-gate 	char **buf;
2027*0Sstevel@tonic-gate 
2028*0Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) >= DS_BOUND);
2029*0Sstevel@tonic-gate 
2030*0Sstevel@tonic-gate 	if (dip == ddi_root_node())	/* rootnode exports class "root" */
2031*0Sstevel@tonic-gate 		nclass = 1;
2032*0Sstevel@tonic-gate 	lock_hw_class_list();
2033*0Sstevel@tonic-gate 	nclass += get_class(ddi_driver_name(dip), NULL);
2034*0Sstevel@tonic-gate 	if (nclass == 0) {
2035*0Sstevel@tonic-gate 		unlock_hw_class_list();
2036*0Sstevel@tonic-gate 		return (0);		/* no class exported */
2037*0Sstevel@tonic-gate 	}
2038*0Sstevel@tonic-gate 
2039*0Sstevel@tonic-gate 	*classes = buf = kmem_alloc(nclass * sizeof (char *), KM_SLEEP);
2040*0Sstevel@tonic-gate 	if (dip == ddi_root_node()) {
2041*0Sstevel@tonic-gate 		*buf++ = rootclass;
2042*0Sstevel@tonic-gate 		n = 1;
2043*0Sstevel@tonic-gate 	}
2044*0Sstevel@tonic-gate 	n += get_class(ddi_driver_name(dip), buf);
2045*0Sstevel@tonic-gate 	unlock_hw_class_list();
2046*0Sstevel@tonic-gate 
2047*0Sstevel@tonic-gate 	ASSERT(n == nclass);    /* make sure buf wasn't overrun */
2048*0Sstevel@tonic-gate 	return (nclass);
2049*0Sstevel@tonic-gate }
2050*0Sstevel@tonic-gate 
2051*0Sstevel@tonic-gate /*
2052*0Sstevel@tonic-gate  * Helper functions, returns NULL if no memory.
2053*0Sstevel@tonic-gate  */
2054*0Sstevel@tonic-gate char *
2055*0Sstevel@tonic-gate i_ddi_strdup(char *str, uint_t flag)
2056*0Sstevel@tonic-gate {
2057*0Sstevel@tonic-gate 	char *copy;
2058*0Sstevel@tonic-gate 
2059*0Sstevel@tonic-gate 	if (str == NULL)
2060*0Sstevel@tonic-gate 		return (NULL);
2061*0Sstevel@tonic-gate 
2062*0Sstevel@tonic-gate 	copy = kmem_alloc(strlen(str) + 1, flag);
2063*0Sstevel@tonic-gate 	if (copy == NULL)
2064*0Sstevel@tonic-gate 		return (NULL);
2065*0Sstevel@tonic-gate 
2066*0Sstevel@tonic-gate 	(void) strcpy(copy, str);
2067*0Sstevel@tonic-gate 	return (copy);
2068*0Sstevel@tonic-gate }
2069*0Sstevel@tonic-gate 
2070*0Sstevel@tonic-gate /*
2071*0Sstevel@tonic-gate  * Load driver.conf file for major. Load all if major == -1.
2072*0Sstevel@tonic-gate  *
2073*0Sstevel@tonic-gate  * This is called
2074*0Sstevel@tonic-gate  * - early in boot after devnames array is initialized
2075*0Sstevel@tonic-gate  * - from vfs code when certain file systems are mounted
2076*0Sstevel@tonic-gate  * - from add_drv when a new driver is added
2077*0Sstevel@tonic-gate  */
2078*0Sstevel@tonic-gate int
2079*0Sstevel@tonic-gate i_ddi_load_drvconf(major_t major)
2080*0Sstevel@tonic-gate {
2081*0Sstevel@tonic-gate 	extern int modrootloaded;
2082*0Sstevel@tonic-gate 
2083*0Sstevel@tonic-gate 	major_t low, high, m;
2084*0Sstevel@tonic-gate 
2085*0Sstevel@tonic-gate 	if (major == (major_t)-1) {
2086*0Sstevel@tonic-gate 		low = 0;
2087*0Sstevel@tonic-gate 		high = devcnt - 1;
2088*0Sstevel@tonic-gate 	} else {
2089*0Sstevel@tonic-gate 		if (major >= devcnt)
2090*0Sstevel@tonic-gate 			return (EINVAL);
2091*0Sstevel@tonic-gate 		low = high = major;
2092*0Sstevel@tonic-gate 	}
2093*0Sstevel@tonic-gate 
2094*0Sstevel@tonic-gate 	for (m = low; m <= high; m++) {
2095*0Sstevel@tonic-gate 		struct devnames *dnp = &devnamesp[m];
2096*0Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
2097*0Sstevel@tonic-gate 		dnp->dn_flags &= ~DN_DRIVER_HELD;
2098*0Sstevel@tonic-gate 		(void) impl_make_parlist(m);
2099*0Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
2100*0Sstevel@tonic-gate 	}
2101*0Sstevel@tonic-gate 
2102*0Sstevel@tonic-gate 	if (modrootloaded) {
2103*0Sstevel@tonic-gate 		ddi_walk_devs(ddi_root_node(), reset_nexus_flags,
2104*0Sstevel@tonic-gate 		    (void *)(uintptr_t)major);
2105*0Sstevel@tonic-gate 	}
2106*0Sstevel@tonic-gate 
2107*0Sstevel@tonic-gate 	/* build dn_list from old entries in path_to_inst */
2108*0Sstevel@tonic-gate 	e_ddi_unorphan_instance_nos();
2109*0Sstevel@tonic-gate 	return (0);
2110*0Sstevel@tonic-gate }
2111*0Sstevel@tonic-gate 
2112*0Sstevel@tonic-gate /*
2113*0Sstevel@tonic-gate  * Unload a specific driver.conf.
2114*0Sstevel@tonic-gate  * Don't support unload all because it doesn't make any sense
2115*0Sstevel@tonic-gate  */
2116*0Sstevel@tonic-gate int
2117*0Sstevel@tonic-gate i_ddi_unload_drvconf(major_t major)
2118*0Sstevel@tonic-gate {
2119*0Sstevel@tonic-gate 	int error;
2120*0Sstevel@tonic-gate 	struct devnames *dnp;
2121*0Sstevel@tonic-gate 
2122*0Sstevel@tonic-gate 	if (major >= devcnt)
2123*0Sstevel@tonic-gate 		return (EINVAL);
2124*0Sstevel@tonic-gate 
2125*0Sstevel@tonic-gate 	/*
2126*0Sstevel@tonic-gate 	 * Take the per-driver lock while unloading driver.conf
2127*0Sstevel@tonic-gate 	 */
2128*0Sstevel@tonic-gate 	dnp = &devnamesp[major];
2129*0Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
2130*0Sstevel@tonic-gate 	error = impl_free_parlist(major);
2131*0Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
2132*0Sstevel@tonic-gate 	return (error);
2133*0Sstevel@tonic-gate }
2134*0Sstevel@tonic-gate 
2135*0Sstevel@tonic-gate /*
2136*0Sstevel@tonic-gate  * Merge a .conf node. This is called by nexus drivers to augment
2137*0Sstevel@tonic-gate  * hw node with properties specified in driver.conf file. This function
2138*0Sstevel@tonic-gate  * takes a callback routine to name nexus children.
2139*0Sstevel@tonic-gate  * The parent node must be held busy.
2140*0Sstevel@tonic-gate  *
2141*0Sstevel@tonic-gate  * It returns DDI_SUCCESS if the node is merged and DDI_FAILURE otherwise.
2142*0Sstevel@tonic-gate  */
2143*0Sstevel@tonic-gate int
2144*0Sstevel@tonic-gate ndi_merge_node(dev_info_t *dip, int (*name_node)(dev_info_t *, char *, int))
2145*0Sstevel@tonic-gate {
2146*0Sstevel@tonic-gate 	dev_info_t *hwdip;
2147*0Sstevel@tonic-gate 
2148*0Sstevel@tonic-gate 	ASSERT(ndi_dev_is_persistent_node(dip) == 0);
2149*0Sstevel@tonic-gate 	ASSERT(ddi_get_name_addr(dip) != NULL);
2150*0Sstevel@tonic-gate 
2151*0Sstevel@tonic-gate 	hwdip = find_child_by_callback(ddi_get_parent(dip),
2152*0Sstevel@tonic-gate 	    ddi_binding_name(dip), ddi_get_name_addr(dip), name_node);
2153*0Sstevel@tonic-gate 
2154*0Sstevel@tonic-gate 	/*
2155*0Sstevel@tonic-gate 	 * Look for the hardware node that is the target of the merge;
2156*0Sstevel@tonic-gate 	 * return failure if not found.
2157*0Sstevel@tonic-gate 	 */
2158*0Sstevel@tonic-gate 	if ((hwdip == NULL) || (hwdip == dip)) {
2159*0Sstevel@tonic-gate 		char *buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
2160*0Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_WARN, "No HW node to merge conf node %s",
2161*0Sstevel@tonic-gate 		    ddi_deviname(dip, buf)));
2162*0Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
2163*0Sstevel@tonic-gate 		return (DDI_FAILURE);
2164*0Sstevel@tonic-gate 	}
2165*0Sstevel@tonic-gate 
2166*0Sstevel@tonic-gate 	/*
2167*0Sstevel@tonic-gate 	 * Make sure the hardware node is uninitialized and has no property.
2168*0Sstevel@tonic-gate 	 * This may not be the case if new .conf files are load after some
2169*0Sstevel@tonic-gate 	 * hardware nodes have already been initialized and attached.
2170*0Sstevel@tonic-gate 	 *
2171*0Sstevel@tonic-gate 	 * N.B. We return success here because the node was *intended*
2172*0Sstevel@tonic-gate 	 * 	to be a merge node because there is a hw node with the name.
2173*0Sstevel@tonic-gate 	 */
2174*0Sstevel@tonic-gate 	mutex_enter(&DEVI(hwdip)->devi_lock);
2175*0Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(hwdip) == 0) {
2176*0Sstevel@tonic-gate 		char *buf;
2177*0Sstevel@tonic-gate 		mutex_exit(&DEVI(hwdip)->devi_lock);
2178*0Sstevel@tonic-gate 
2179*0Sstevel@tonic-gate 		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
2180*0Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE, "Duplicate .conf node %s",
2181*0Sstevel@tonic-gate 		    ddi_deviname(dip, buf)));
2182*0Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
2183*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
2184*0Sstevel@tonic-gate 	}
2185*0Sstevel@tonic-gate 
2186*0Sstevel@tonic-gate 	/*
2187*0Sstevel@tonic-gate 	 * If it is possible that the hardware has already been touched
2188*0Sstevel@tonic-gate 	 * then don't merge.
2189*0Sstevel@tonic-gate 	 */
2190*0Sstevel@tonic-gate 	if (i_ddi_node_state(hwdip) >= DS_INITIALIZED ||
2191*0Sstevel@tonic-gate 	    (DEVI(hwdip)->devi_sys_prop_ptr != NULL) ||
2192*0Sstevel@tonic-gate 	    (DEVI(hwdip)->devi_drv_prop_ptr != NULL)) {
2193*0Sstevel@tonic-gate 		char *buf;
2194*0Sstevel@tonic-gate 		mutex_exit(&DEVI(hwdip)->devi_lock);
2195*0Sstevel@tonic-gate 
2196*0Sstevel@tonic-gate 		buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
2197*0Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE,
2198*0Sstevel@tonic-gate 		    "!Cannot merge .conf node %s with hw node %p "
2199*0Sstevel@tonic-gate 		    "-- not in proper state",
2200*0Sstevel@tonic-gate 		    ddi_deviname(dip, buf), (void *)hwdip));
2201*0Sstevel@tonic-gate 		kmem_free(buf, MAXNAMELEN);
2202*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
2203*0Sstevel@tonic-gate 	}
2204*0Sstevel@tonic-gate 
2205*0Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
2206*0Sstevel@tonic-gate 	DEVI(hwdip)->devi_sys_prop_ptr = DEVI(dip)->devi_sys_prop_ptr;
2207*0Sstevel@tonic-gate 	DEVI(hwdip)->devi_drv_prop_ptr = DEVI(dip)->devi_drv_prop_ptr;
2208*0Sstevel@tonic-gate 	DEVI(dip)->devi_sys_prop_ptr = NULL;
2209*0Sstevel@tonic-gate 	DEVI(dip)->devi_drv_prop_ptr = NULL;
2210*0Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
2211*0Sstevel@tonic-gate 	mutex_exit(&DEVI(hwdip)->devi_lock);
2212*0Sstevel@tonic-gate 
2213*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
2214*0Sstevel@tonic-gate }
2215*0Sstevel@tonic-gate 
2216*0Sstevel@tonic-gate /*
2217*0Sstevel@tonic-gate  * Merge a "wildcard" .conf node. This is called by nexus drivers to
2218*0Sstevel@tonic-gate  * augment a set of hw node with properties specified in driver.conf file.
2219*0Sstevel@tonic-gate  * The parent node must be held busy.
2220*0Sstevel@tonic-gate  *
2221*0Sstevel@tonic-gate  * There is no failure mode, since the nexus may or may not have child
2222*0Sstevel@tonic-gate  * node bound the driver specified by the wildcard node.
2223*0Sstevel@tonic-gate  */
2224*0Sstevel@tonic-gate void
2225*0Sstevel@tonic-gate ndi_merge_wildcard_node(dev_info_t *dip)
2226*0Sstevel@tonic-gate {
2227*0Sstevel@tonic-gate 	dev_info_t *hwdip;
2228*0Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
2229*0Sstevel@tonic-gate 	major_t major = ddi_driver_major(dip);
2230*0Sstevel@tonic-gate 
2231*0Sstevel@tonic-gate 	/* never attempt to merge a hw node */
2232*0Sstevel@tonic-gate 	ASSERT(ndi_dev_is_persistent_node(dip) == 0);
2233*0Sstevel@tonic-gate 	/* must be bound to a driver major number */
2234*0Sstevel@tonic-gate 	ASSERT(major != (major_t)-1);
2235*0Sstevel@tonic-gate 
2236*0Sstevel@tonic-gate 	/*
2237*0Sstevel@tonic-gate 	 * Walk the child list to find all nodes bound to major
2238*0Sstevel@tonic-gate 	 * and copy properties.
2239*0Sstevel@tonic-gate 	 */
2240*0Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
2241*0Sstevel@tonic-gate 	for (hwdip = ddi_get_child(pdip); hwdip;
2242*0Sstevel@tonic-gate 	    hwdip = ddi_get_next_sibling(hwdip)) {
2243*0Sstevel@tonic-gate 		/*
2244*0Sstevel@tonic-gate 		 * Skip nodes not bound to same driver
2245*0Sstevel@tonic-gate 		 */
2246*0Sstevel@tonic-gate 		if (ddi_driver_major(hwdip) != major)
2247*0Sstevel@tonic-gate 			continue;
2248*0Sstevel@tonic-gate 
2249*0Sstevel@tonic-gate 		/*
2250*0Sstevel@tonic-gate 		 * Skip .conf nodes
2251*0Sstevel@tonic-gate 		 */
2252*0Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(hwdip) == 0)
2253*0Sstevel@tonic-gate 			continue;
2254*0Sstevel@tonic-gate 
2255*0Sstevel@tonic-gate 		/*
2256*0Sstevel@tonic-gate 		 * Make sure the node is uninitialized and has no property.
2257*0Sstevel@tonic-gate 		 */
2258*0Sstevel@tonic-gate 		mutex_enter(&DEVI(hwdip)->devi_lock);
2259*0Sstevel@tonic-gate 		if (i_ddi_node_state(hwdip) >= DS_INITIALIZED ||
2260*0Sstevel@tonic-gate 		    (DEVI(hwdip)->devi_sys_prop_ptr != NULL) ||
2261*0Sstevel@tonic-gate 		    (DEVI(hwdip)->devi_drv_prop_ptr != NULL)) {
2262*0Sstevel@tonic-gate 			mutex_exit(&DEVI(hwdip)->devi_lock);
2263*0Sstevel@tonic-gate 			NDI_CONFIG_DEBUG((CE_NOTE, "HW node %p state not "
2264*0Sstevel@tonic-gate 			    "suitable for merging wildcard conf node %s",
2265*0Sstevel@tonic-gate 			    (void *)hwdip, ddi_node_name(dip)));
2266*0Sstevel@tonic-gate 			continue;
2267*0Sstevel@tonic-gate 		}
2268*0Sstevel@tonic-gate 
2269*0Sstevel@tonic-gate 		DEVI(hwdip)->devi_sys_prop_ptr =
2270*0Sstevel@tonic-gate 		    i_ddi_prop_list_dup(DEVI(dip)->devi_sys_prop_ptr, KM_SLEEP);
2271*0Sstevel@tonic-gate 		DEVI(hwdip)->devi_drv_prop_ptr =
2272*0Sstevel@tonic-gate 		    i_ddi_prop_list_dup(DEVI(dip)->devi_drv_prop_ptr, KM_SLEEP);
2273*0Sstevel@tonic-gate 		mutex_exit(&DEVI(hwdip)->devi_lock);
2274*0Sstevel@tonic-gate 	}
2275*0Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
2276*0Sstevel@tonic-gate }
2277*0Sstevel@tonic-gate 
2278*0Sstevel@tonic-gate /*
2279*0Sstevel@tonic-gate  * Return the major number based on the compatible property. This interface
2280*0Sstevel@tonic-gate  * may be used in situations where we are trying to detect if a better driver
2281*0Sstevel@tonic-gate  * now exists for a device, so it must use the 'compatible' property.  If
2282*0Sstevel@tonic-gate  * a non-NULL formp is specified and the binding was based on compatible then
2283*0Sstevel@tonic-gate  * return the pointer to the form used in *formp.
2284*0Sstevel@tonic-gate  */
2285*0Sstevel@tonic-gate major_t
2286*0Sstevel@tonic-gate ddi_compatible_driver_major(dev_info_t *dip, char **formp)
2287*0Sstevel@tonic-gate {
2288*0Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
2289*0Sstevel@tonic-gate 	void		*compat;
2290*0Sstevel@tonic-gate 	size_t		len;
2291*0Sstevel@tonic-gate 	char		*p = NULL;
2292*0Sstevel@tonic-gate 	major_t		major = (major_t)-1;
2293*0Sstevel@tonic-gate 
2294*0Sstevel@tonic-gate 	if (formp)
2295*0Sstevel@tonic-gate 		*formp = NULL;
2296*0Sstevel@tonic-gate 
2297*0Sstevel@tonic-gate 	/* look up compatible property */
2298*0Sstevel@tonic-gate 	(void) lookup_compatible(dip, KM_SLEEP);
2299*0Sstevel@tonic-gate 	compat = (void *)(devi->devi_compat_names);
2300*0Sstevel@tonic-gate 	len = devi->devi_compat_length;
2301*0Sstevel@tonic-gate 
2302*0Sstevel@tonic-gate 	/* find the highest precedence compatible form with a driver binding */
2303*0Sstevel@tonic-gate 	while ((p = prom_decode_composite_string(compat, len, p)) != NULL) {
2304*0Sstevel@tonic-gate 		major = ddi_name_to_major(p);
2305*0Sstevel@tonic-gate 		if ((major != (major_t)-1) &&
2306*0Sstevel@tonic-gate 		    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED)) {
2307*0Sstevel@tonic-gate 			if (formp)
2308*0Sstevel@tonic-gate 				*formp = p;
2309*0Sstevel@tonic-gate 			return (major);
2310*0Sstevel@tonic-gate 		}
2311*0Sstevel@tonic-gate 	}
2312*0Sstevel@tonic-gate 
2313*0Sstevel@tonic-gate 	/*
2314*0Sstevel@tonic-gate 	 * none of the compatible forms have a driver binding, see if
2315*0Sstevel@tonic-gate 	 * the node name has a driver binding.
2316*0Sstevel@tonic-gate 	 */
2317*0Sstevel@tonic-gate 	major = ddi_name_to_major(ddi_node_name(dip));
2318*0Sstevel@tonic-gate 	if ((major != (major_t)-1) &&
2319*0Sstevel@tonic-gate 	    !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED))
2320*0Sstevel@tonic-gate 		return (major);
2321*0Sstevel@tonic-gate 
2322*0Sstevel@tonic-gate 	/* no driver */
2323*0Sstevel@tonic-gate 	return ((major_t)-1);
2324*0Sstevel@tonic-gate }
2325*0Sstevel@tonic-gate 
2326*0Sstevel@tonic-gate /*
2327*0Sstevel@tonic-gate  * Static help functions
2328*0Sstevel@tonic-gate  */
2329*0Sstevel@tonic-gate 
2330*0Sstevel@tonic-gate /*
2331*0Sstevel@tonic-gate  * lookup the "compatible" property and cache it's contents in the
2332*0Sstevel@tonic-gate  * device node.
2333*0Sstevel@tonic-gate  */
2334*0Sstevel@tonic-gate static int
2335*0Sstevel@tonic-gate lookup_compatible(dev_info_t *dip, uint_t flag)
2336*0Sstevel@tonic-gate {
2337*0Sstevel@tonic-gate 	int rv;
2338*0Sstevel@tonic-gate 	int prop_flags;
2339*0Sstevel@tonic-gate 	uint_t ncompatstrs;
2340*0Sstevel@tonic-gate 	char **compatstrpp;
2341*0Sstevel@tonic-gate 	char *di_compat_strp;
2342*0Sstevel@tonic-gate 	size_t di_compat_strlen;
2343*0Sstevel@tonic-gate 
2344*0Sstevel@tonic-gate 	if (DEVI(dip)->devi_compat_names) {
2345*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
2346*0Sstevel@tonic-gate 	}
2347*0Sstevel@tonic-gate 
2348*0Sstevel@tonic-gate 	prop_flags = DDI_PROP_TYPE_STRING | DDI_PROP_DONTPASS;
2349*0Sstevel@tonic-gate 
2350*0Sstevel@tonic-gate 	if (flag & KM_NOSLEEP) {
2351*0Sstevel@tonic-gate 		prop_flags |= DDI_PROP_DONTSLEEP;
2352*0Sstevel@tonic-gate 	}
2353*0Sstevel@tonic-gate 
2354*0Sstevel@tonic-gate 	if (ndi_dev_is_prom_node(dip) == 0) {
2355*0Sstevel@tonic-gate 		prop_flags |= DDI_PROP_NOTPROM;
2356*0Sstevel@tonic-gate 	}
2357*0Sstevel@tonic-gate 
2358*0Sstevel@tonic-gate 	rv = ddi_prop_lookup_common(DDI_DEV_T_ANY, dip, prop_flags,
2359*0Sstevel@tonic-gate 	    "compatible", &compatstrpp, &ncompatstrs,
2360*0Sstevel@tonic-gate 	    ddi_prop_fm_decode_strings);
2361*0Sstevel@tonic-gate 
2362*0Sstevel@tonic-gate 	if (rv == DDI_PROP_NOT_FOUND) {
2363*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
2364*0Sstevel@tonic-gate 	}
2365*0Sstevel@tonic-gate 
2366*0Sstevel@tonic-gate 	if (rv != DDI_PROP_SUCCESS) {
2367*0Sstevel@tonic-gate 		return (DDI_FAILURE);
2368*0Sstevel@tonic-gate 	}
2369*0Sstevel@tonic-gate 
2370*0Sstevel@tonic-gate 	/*
2371*0Sstevel@tonic-gate 	 * encode the compatible property data in the dev_info node
2372*0Sstevel@tonic-gate 	 */
2373*0Sstevel@tonic-gate 	rv = DDI_SUCCESS;
2374*0Sstevel@tonic-gate 	if (ncompatstrs != 0) {
2375*0Sstevel@tonic-gate 		di_compat_strp = encode_composite_string(compatstrpp,
2376*0Sstevel@tonic-gate 		    ncompatstrs, &di_compat_strlen, flag);
2377*0Sstevel@tonic-gate 		if (di_compat_strp != NULL) {
2378*0Sstevel@tonic-gate 			DEVI(dip)->devi_compat_names = di_compat_strp;
2379*0Sstevel@tonic-gate 			DEVI(dip)->devi_compat_length = di_compat_strlen;
2380*0Sstevel@tonic-gate 		} else {
2381*0Sstevel@tonic-gate 			rv = DDI_FAILURE;
2382*0Sstevel@tonic-gate 		}
2383*0Sstevel@tonic-gate 	}
2384*0Sstevel@tonic-gate 	ddi_prop_free(compatstrpp);
2385*0Sstevel@tonic-gate 	return (rv);
2386*0Sstevel@tonic-gate }
2387*0Sstevel@tonic-gate 
2388*0Sstevel@tonic-gate /*
2389*0Sstevel@tonic-gate  * Create a composite string from a list of strings.
2390*0Sstevel@tonic-gate  *
2391*0Sstevel@tonic-gate  * A composite string consists of a single buffer containing one
2392*0Sstevel@tonic-gate  * or more NULL terminated strings.
2393*0Sstevel@tonic-gate  */
2394*0Sstevel@tonic-gate static char *
2395*0Sstevel@tonic-gate encode_composite_string(char **strings, uint_t nstrings, size_t *retsz,
2396*0Sstevel@tonic-gate     uint_t flag)
2397*0Sstevel@tonic-gate {
2398*0Sstevel@tonic-gate 	uint_t index;
2399*0Sstevel@tonic-gate 	char  **strpp;
2400*0Sstevel@tonic-gate 	uint_t slen;
2401*0Sstevel@tonic-gate 	size_t cbuf_sz = 0;
2402*0Sstevel@tonic-gate 	char *cbuf_p;
2403*0Sstevel@tonic-gate 	char *cbuf_ip;
2404*0Sstevel@tonic-gate 
2405*0Sstevel@tonic-gate 	if (strings == NULL || nstrings == 0 || retsz == NULL) {
2406*0Sstevel@tonic-gate 		return (NULL);
2407*0Sstevel@tonic-gate 	}
2408*0Sstevel@tonic-gate 
2409*0Sstevel@tonic-gate 	for (index = 0, strpp = strings; index < nstrings; index++)
2410*0Sstevel@tonic-gate 		cbuf_sz += strlen(*(strpp++)) + 1;
2411*0Sstevel@tonic-gate 
2412*0Sstevel@tonic-gate 	if ((cbuf_p = kmem_alloc(cbuf_sz, flag)) == NULL) {
2413*0Sstevel@tonic-gate 		cmn_err(CE_NOTE,
2414*0Sstevel@tonic-gate 		    "?failed to allocate device node compatstr");
2415*0Sstevel@tonic-gate 		return (NULL);
2416*0Sstevel@tonic-gate 	}
2417*0Sstevel@tonic-gate 
2418*0Sstevel@tonic-gate 	cbuf_ip = cbuf_p;
2419*0Sstevel@tonic-gate 	for (index = 0, strpp = strings; index < nstrings; index++) {
2420*0Sstevel@tonic-gate 		slen = strlen(*strpp);
2421*0Sstevel@tonic-gate 		bcopy(*(strpp++), cbuf_ip, slen);
2422*0Sstevel@tonic-gate 		cbuf_ip += slen;
2423*0Sstevel@tonic-gate 		*(cbuf_ip++) = '\0';
2424*0Sstevel@tonic-gate 	}
2425*0Sstevel@tonic-gate 
2426*0Sstevel@tonic-gate 	*retsz = cbuf_sz;
2427*0Sstevel@tonic-gate 	return (cbuf_p);
2428*0Sstevel@tonic-gate }
2429*0Sstevel@tonic-gate 
2430*0Sstevel@tonic-gate static void
2431*0Sstevel@tonic-gate link_to_driver_list(dev_info_t *dip)
2432*0Sstevel@tonic-gate {
2433*0Sstevel@tonic-gate 	major_t major = DEVI(dip)->devi_major;
2434*0Sstevel@tonic-gate 	struct devnames *dnp;
2435*0Sstevel@tonic-gate 
2436*0Sstevel@tonic-gate 	ASSERT(major != (major_t)-1);
2437*0Sstevel@tonic-gate 
2438*0Sstevel@tonic-gate 	/*
2439*0Sstevel@tonic-gate 	 * Remove from orphan list
2440*0Sstevel@tonic-gate 	 */
2441*0Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
2442*0Sstevel@tonic-gate 		dnp = &orphanlist;
2443*0Sstevel@tonic-gate 		remove_from_dn_list(dnp, dip);
2444*0Sstevel@tonic-gate 	}
2445*0Sstevel@tonic-gate 
2446*0Sstevel@tonic-gate 	/*
2447*0Sstevel@tonic-gate 	 * Add to per driver list
2448*0Sstevel@tonic-gate 	 */
2449*0Sstevel@tonic-gate 	dnp = &devnamesp[major];
2450*0Sstevel@tonic-gate 	add_to_dn_list(dnp, dip);
2451*0Sstevel@tonic-gate }
2452*0Sstevel@tonic-gate 
2453*0Sstevel@tonic-gate static void
2454*0Sstevel@tonic-gate unlink_from_driver_list(dev_info_t *dip)
2455*0Sstevel@tonic-gate {
2456*0Sstevel@tonic-gate 	major_t major = DEVI(dip)->devi_major;
2457*0Sstevel@tonic-gate 	struct devnames *dnp;
2458*0Sstevel@tonic-gate 
2459*0Sstevel@tonic-gate 	ASSERT(major != (major_t)-1);
2460*0Sstevel@tonic-gate 
2461*0Sstevel@tonic-gate 	/*
2462*0Sstevel@tonic-gate 	 * Remove from per-driver list
2463*0Sstevel@tonic-gate 	 */
2464*0Sstevel@tonic-gate 	dnp = &devnamesp[major];
2465*0Sstevel@tonic-gate 	remove_from_dn_list(dnp, dip);
2466*0Sstevel@tonic-gate 
2467*0Sstevel@tonic-gate 	/*
2468*0Sstevel@tonic-gate 	 * Add to orphan list
2469*0Sstevel@tonic-gate 	 */
2470*0Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(dip)) {
2471*0Sstevel@tonic-gate 		dnp = &orphanlist;
2472*0Sstevel@tonic-gate 		add_to_dn_list(dnp, dip);
2473*0Sstevel@tonic-gate 	}
2474*0Sstevel@tonic-gate }
2475*0Sstevel@tonic-gate 
2476*0Sstevel@tonic-gate /*
2477*0Sstevel@tonic-gate  * scan the per-driver list looking for dev_info "dip"
2478*0Sstevel@tonic-gate  */
2479*0Sstevel@tonic-gate static dev_info_t *
2480*0Sstevel@tonic-gate in_dn_list(struct devnames *dnp, dev_info_t *dip)
2481*0Sstevel@tonic-gate {
2482*0Sstevel@tonic-gate 	struct dev_info *idevi;
2483*0Sstevel@tonic-gate 
2484*0Sstevel@tonic-gate 	if ((idevi = DEVI(dnp->dn_head)) == NULL)
2485*0Sstevel@tonic-gate 		return (NULL);
2486*0Sstevel@tonic-gate 
2487*0Sstevel@tonic-gate 	while (idevi) {
2488*0Sstevel@tonic-gate 		if (idevi == DEVI(dip))
2489*0Sstevel@tonic-gate 			return (dip);
2490*0Sstevel@tonic-gate 		idevi = idevi->devi_next;
2491*0Sstevel@tonic-gate 	}
2492*0Sstevel@tonic-gate 	return (NULL);
2493*0Sstevel@tonic-gate }
2494*0Sstevel@tonic-gate 
2495*0Sstevel@tonic-gate /*
2496*0Sstevel@tonic-gate  * insert devinfo node 'dip' into the per-driver instance list
2497*0Sstevel@tonic-gate  * headed by 'dnp'
2498*0Sstevel@tonic-gate  *
2499*0Sstevel@tonic-gate  * Nodes on the per-driver list are ordered: HW - SID - PSEUDO.  The order is
2500*0Sstevel@tonic-gate  * required for merging of .conf file data to work properly.
2501*0Sstevel@tonic-gate  */
2502*0Sstevel@tonic-gate static void
2503*0Sstevel@tonic-gate add_to_ordered_dn_list(struct devnames *dnp, dev_info_t *dip)
2504*0Sstevel@tonic-gate {
2505*0Sstevel@tonic-gate 	dev_info_t **dipp;
2506*0Sstevel@tonic-gate 
2507*0Sstevel@tonic-gate 	ASSERT(mutex_owned(&(dnp->dn_lock)));
2508*0Sstevel@tonic-gate 
2509*0Sstevel@tonic-gate 	dipp = &dnp->dn_head;
2510*0Sstevel@tonic-gate 	if (ndi_dev_is_prom_node(dip)) {
2511*0Sstevel@tonic-gate 		/*
2512*0Sstevel@tonic-gate 		 * Find the first non-prom node or end of list
2513*0Sstevel@tonic-gate 		 */
2514*0Sstevel@tonic-gate 		while (*dipp && (ndi_dev_is_prom_node(*dipp) != 0)) {
2515*0Sstevel@tonic-gate 			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
2516*0Sstevel@tonic-gate 		}
2517*0Sstevel@tonic-gate 	} else if (ndi_dev_is_persistent_node(dip)) {
2518*0Sstevel@tonic-gate 		/*
2519*0Sstevel@tonic-gate 		 * Find the first non-persistent node
2520*0Sstevel@tonic-gate 		 */
2521*0Sstevel@tonic-gate 		while (*dipp && (ndi_dev_is_persistent_node(*dipp) != 0)) {
2522*0Sstevel@tonic-gate 			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
2523*0Sstevel@tonic-gate 		}
2524*0Sstevel@tonic-gate 	} else {
2525*0Sstevel@tonic-gate 		/*
2526*0Sstevel@tonic-gate 		 * Find the end of the list
2527*0Sstevel@tonic-gate 		 */
2528*0Sstevel@tonic-gate 		while (*dipp) {
2529*0Sstevel@tonic-gate 			dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
2530*0Sstevel@tonic-gate 		}
2531*0Sstevel@tonic-gate 	}
2532*0Sstevel@tonic-gate 
2533*0Sstevel@tonic-gate 	DEVI(dip)->devi_next = DEVI(*dipp);
2534*0Sstevel@tonic-gate 	*dipp = dip;
2535*0Sstevel@tonic-gate }
2536*0Sstevel@tonic-gate 
2537*0Sstevel@tonic-gate /*
2538*0Sstevel@tonic-gate  * add a list of device nodes to the device node list in the
2539*0Sstevel@tonic-gate  * devnames structure
2540*0Sstevel@tonic-gate  */
2541*0Sstevel@tonic-gate static void
2542*0Sstevel@tonic-gate add_to_dn_list(struct devnames *dnp, dev_info_t *dip)
2543*0Sstevel@tonic-gate {
2544*0Sstevel@tonic-gate 	/*
2545*0Sstevel@tonic-gate 	 * Look to see if node already exists
2546*0Sstevel@tonic-gate 	 */
2547*0Sstevel@tonic-gate 	LOCK_DEV_OPS(&(dnp->dn_lock));
2548*0Sstevel@tonic-gate 	if (in_dn_list(dnp, dip)) {
2549*0Sstevel@tonic-gate 		cmn_err(CE_NOTE, "add_to_dn_list: node %s already in list",
2550*0Sstevel@tonic-gate 		    DEVI(dip)->devi_node_name);
2551*0Sstevel@tonic-gate 	} else {
2552*0Sstevel@tonic-gate 		add_to_ordered_dn_list(dnp, dip);
2553*0Sstevel@tonic-gate 	}
2554*0Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
2555*0Sstevel@tonic-gate }
2556*0Sstevel@tonic-gate 
2557*0Sstevel@tonic-gate static void
2558*0Sstevel@tonic-gate remove_from_dn_list(struct devnames *dnp, dev_info_t *dip)
2559*0Sstevel@tonic-gate {
2560*0Sstevel@tonic-gate 	dev_info_t **plist;
2561*0Sstevel@tonic-gate 
2562*0Sstevel@tonic-gate 	LOCK_DEV_OPS(&(dnp->dn_lock));
2563*0Sstevel@tonic-gate 
2564*0Sstevel@tonic-gate 	plist = (dev_info_t **)&dnp->dn_head;
2565*0Sstevel@tonic-gate 	while (*plist && (*plist != dip)) {
2566*0Sstevel@tonic-gate 		plist = (dev_info_t **)&DEVI(*plist)->devi_next;
2567*0Sstevel@tonic-gate 	}
2568*0Sstevel@tonic-gate 
2569*0Sstevel@tonic-gate 	if (*plist != NULL) {
2570*0Sstevel@tonic-gate 		ASSERT(*plist == dip);
2571*0Sstevel@tonic-gate 		*plist = (dev_info_t *)(DEVI(dip)->devi_next);
2572*0Sstevel@tonic-gate 		DEVI(dip)->devi_next = NULL;
2573*0Sstevel@tonic-gate 	} else {
2574*0Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE,
2575*0Sstevel@tonic-gate 		    "remove_from_dn_list: node %s not found in list",
2576*0Sstevel@tonic-gate 		    DEVI(dip)->devi_node_name));
2577*0Sstevel@tonic-gate 	}
2578*0Sstevel@tonic-gate 
2579*0Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
2580*0Sstevel@tonic-gate }
2581*0Sstevel@tonic-gate 
2582*0Sstevel@tonic-gate /*
2583*0Sstevel@tonic-gate  * Add and remove reference driver global property list
2584*0Sstevel@tonic-gate  */
2585*0Sstevel@tonic-gate static void
2586*0Sstevel@tonic-gate add_global_props(dev_info_t *dip)
2587*0Sstevel@tonic-gate {
2588*0Sstevel@tonic-gate 	struct devnames *dnp;
2589*0Sstevel@tonic-gate 	ddi_prop_list_t *plist;
2590*0Sstevel@tonic-gate 
2591*0Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_global_prop_list == NULL);
2592*0Sstevel@tonic-gate 	ASSERT(DEVI(dip)->devi_major != (major_t)-1);
2593*0Sstevel@tonic-gate 
2594*0Sstevel@tonic-gate 	dnp = &devnamesp[DEVI(dip)->devi_major];
2595*0Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
2596*0Sstevel@tonic-gate 	plist = dnp->dn_global_prop_ptr;
2597*0Sstevel@tonic-gate 	if (plist == NULL) {
2598*0Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
2599*0Sstevel@tonic-gate 		return;
2600*0Sstevel@tonic-gate 	}
2601*0Sstevel@tonic-gate 	i_ddi_prop_list_hold(plist, dnp);
2602*0Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
2603*0Sstevel@tonic-gate 
2604*0Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
2605*0Sstevel@tonic-gate 	DEVI(dip)->devi_global_prop_list = plist;
2606*0Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
2607*0Sstevel@tonic-gate }
2608*0Sstevel@tonic-gate 
2609*0Sstevel@tonic-gate static void
2610*0Sstevel@tonic-gate remove_global_props(dev_info_t *dip)
2611*0Sstevel@tonic-gate {
2612*0Sstevel@tonic-gate 	ddi_prop_list_t *proplist;
2613*0Sstevel@tonic-gate 
2614*0Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
2615*0Sstevel@tonic-gate 	proplist = DEVI(dip)->devi_global_prop_list;
2616*0Sstevel@tonic-gate 	DEVI(dip)->devi_global_prop_list = NULL;
2617*0Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
2618*0Sstevel@tonic-gate 
2619*0Sstevel@tonic-gate 	if (proplist) {
2620*0Sstevel@tonic-gate 		major_t major;
2621*0Sstevel@tonic-gate 		struct devnames *dnp;
2622*0Sstevel@tonic-gate 
2623*0Sstevel@tonic-gate 		major = ddi_driver_major(dip);
2624*0Sstevel@tonic-gate 		ASSERT(major != (major_t)-1);
2625*0Sstevel@tonic-gate 		dnp = &devnamesp[major];
2626*0Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
2627*0Sstevel@tonic-gate 		i_ddi_prop_list_rele(proplist, dnp);
2628*0Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
2629*0Sstevel@tonic-gate 	}
2630*0Sstevel@tonic-gate }
2631*0Sstevel@tonic-gate 
2632*0Sstevel@tonic-gate #ifdef DEBUG
2633*0Sstevel@tonic-gate /*
2634*0Sstevel@tonic-gate  * Set this variable to '0' to disable the optimization,
2635*0Sstevel@tonic-gate  * and to 2 to print debug message.
2636*0Sstevel@tonic-gate  */
2637*0Sstevel@tonic-gate static int optimize_dtree = 1;
2638*0Sstevel@tonic-gate 
2639*0Sstevel@tonic-gate static void
2640*0Sstevel@tonic-gate debug_dtree(dev_info_t *devi, struct dev_info *adevi, char *service)
2641*0Sstevel@tonic-gate {
2642*0Sstevel@tonic-gate 	char *adeviname, *buf;
2643*0Sstevel@tonic-gate 
2644*0Sstevel@tonic-gate 	/*
2645*0Sstevel@tonic-gate 	 * Don't print unless optimize dtree is set to 2+
2646*0Sstevel@tonic-gate 	 */
2647*0Sstevel@tonic-gate 	if (optimize_dtree <= 1)
2648*0Sstevel@tonic-gate 		return;
2649*0Sstevel@tonic-gate 
2650*0Sstevel@tonic-gate 	buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
2651*0Sstevel@tonic-gate 	adeviname = ddi_deviname((dev_info_t *)adevi, buf);
2652*0Sstevel@tonic-gate 	if (*adeviname == '\0')
2653*0Sstevel@tonic-gate 		adeviname = "root";
2654*0Sstevel@tonic-gate 
2655*0Sstevel@tonic-gate 	cmn_err(CE_CONT, "%s %s -> %s\n",
2656*0Sstevel@tonic-gate 	    ddi_deviname(devi, buf), service, adeviname);
2657*0Sstevel@tonic-gate 
2658*0Sstevel@tonic-gate 	kmem_free(buf, MAXNAMELEN);
2659*0Sstevel@tonic-gate }
2660*0Sstevel@tonic-gate #else /* DEBUG */
2661*0Sstevel@tonic-gate #define	debug_dtree(a1, a2, a3)	 /* nothing */
2662*0Sstevel@tonic-gate #endif  /* DEBUG */
2663*0Sstevel@tonic-gate 
2664*0Sstevel@tonic-gate static void
2665*0Sstevel@tonic-gate ddi_optimize_dtree(dev_info_t *devi)
2666*0Sstevel@tonic-gate {
2667*0Sstevel@tonic-gate 	struct dev_info *pdevi;
2668*0Sstevel@tonic-gate 	struct bus_ops *b;
2669*0Sstevel@tonic-gate 
2670*0Sstevel@tonic-gate 	pdevi = DEVI(devi)->devi_parent;
2671*0Sstevel@tonic-gate 	ASSERT(pdevi);
2672*0Sstevel@tonic-gate 
2673*0Sstevel@tonic-gate 	/*
2674*0Sstevel@tonic-gate 	 * Set the unoptimized values
2675*0Sstevel@tonic-gate 	 */
2676*0Sstevel@tonic-gate 	DEVI(devi)->devi_bus_map_fault = pdevi;
2677*0Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_map = pdevi;
2678*0Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_allochdl = pdevi;
2679*0Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_freehdl = pdevi;
2680*0Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_bindhdl = pdevi;
2681*0Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_bindfunc =
2682*0Sstevel@tonic-gate 	pdevi->devi_ops->devo_bus_ops->bus_dma_bindhdl;
2683*0Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_unbindhdl = pdevi;
2684*0Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_unbindfunc =
2685*0Sstevel@tonic-gate 	    pdevi->devi_ops->devo_bus_ops->bus_dma_unbindhdl;
2686*0Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_flush = pdevi;
2687*0Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_win = pdevi;
2688*0Sstevel@tonic-gate 	DEVI(devi)->devi_bus_dma_ctl = pdevi;
2689*0Sstevel@tonic-gate 	DEVI(devi)->devi_bus_ctl = pdevi;
2690*0Sstevel@tonic-gate 
2691*0Sstevel@tonic-gate #ifdef DEBUG
2692*0Sstevel@tonic-gate 	if (optimize_dtree == 0)
2693*0Sstevel@tonic-gate 		return;
2694*0Sstevel@tonic-gate #endif /* DEBUG */
2695*0Sstevel@tonic-gate 
2696*0Sstevel@tonic-gate 	b = pdevi->devi_ops->devo_bus_ops;
2697*0Sstevel@tonic-gate 
2698*0Sstevel@tonic-gate 	if (i_ddi_map_fault == b->bus_map_fault) {
2699*0Sstevel@tonic-gate 		DEVI(devi)->devi_bus_map_fault = pdevi->devi_bus_map_fault;
2700*0Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_map_fault,
2701*0Sstevel@tonic-gate 		    "bus_map_fault");
2702*0Sstevel@tonic-gate 	}
2703*0Sstevel@tonic-gate 
2704*0Sstevel@tonic-gate 	if (ddi_dma_map == b->bus_dma_map) {
2705*0Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_map = pdevi->devi_bus_dma_map;
2706*0Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_map, "bus_dma_map");
2707*0Sstevel@tonic-gate 	}
2708*0Sstevel@tonic-gate 
2709*0Sstevel@tonic-gate 	if (ddi_dma_allochdl == b->bus_dma_allochdl) {
2710*0Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_allochdl =
2711*0Sstevel@tonic-gate 		    pdevi->devi_bus_dma_allochdl;
2712*0Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_allochdl,
2713*0Sstevel@tonic-gate 		    "bus_dma_allochdl");
2714*0Sstevel@tonic-gate 	}
2715*0Sstevel@tonic-gate 
2716*0Sstevel@tonic-gate 	if (ddi_dma_freehdl == b->bus_dma_freehdl) {
2717*0Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_freehdl = pdevi->devi_bus_dma_freehdl;
2718*0Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_freehdl,
2719*0Sstevel@tonic-gate 		    "bus_dma_freehdl");
2720*0Sstevel@tonic-gate 	}
2721*0Sstevel@tonic-gate 
2722*0Sstevel@tonic-gate 	if (ddi_dma_bindhdl == b->bus_dma_bindhdl) {
2723*0Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_bindhdl = pdevi->devi_bus_dma_bindhdl;
2724*0Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_bindfunc =
2725*0Sstevel@tonic-gate 		    pdevi->devi_bus_dma_bindhdl->devi_ops->
2726*0Sstevel@tonic-gate 		    devo_bus_ops->bus_dma_bindhdl;
2727*0Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_bindhdl,
2728*0Sstevel@tonic-gate 		    "bus_dma_bindhdl");
2729*0Sstevel@tonic-gate 	}
2730*0Sstevel@tonic-gate 
2731*0Sstevel@tonic-gate 	if (ddi_dma_unbindhdl == b->bus_dma_unbindhdl) {
2732*0Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_unbindhdl =
2733*0Sstevel@tonic-gate 		    pdevi->devi_bus_dma_unbindhdl;
2734*0Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_unbindfunc =
2735*0Sstevel@tonic-gate 		    pdevi->devi_bus_dma_unbindhdl->devi_ops->
2736*0Sstevel@tonic-gate 		    devo_bus_ops->bus_dma_unbindhdl;
2737*0Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_unbindhdl,
2738*0Sstevel@tonic-gate 		    "bus_dma_unbindhdl");
2739*0Sstevel@tonic-gate 	}
2740*0Sstevel@tonic-gate 
2741*0Sstevel@tonic-gate 	if (ddi_dma_flush == b->bus_dma_flush) {
2742*0Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_flush = pdevi->devi_bus_dma_flush;
2743*0Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_flush,
2744*0Sstevel@tonic-gate 		    "bus_dma_flush");
2745*0Sstevel@tonic-gate 	}
2746*0Sstevel@tonic-gate 
2747*0Sstevel@tonic-gate 	if (ddi_dma_win == b->bus_dma_win) {
2748*0Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_win = pdevi->devi_bus_dma_win;
2749*0Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_win,
2750*0Sstevel@tonic-gate 		    "bus_dma_win");
2751*0Sstevel@tonic-gate 	}
2752*0Sstevel@tonic-gate 
2753*0Sstevel@tonic-gate 	if (ddi_dma_mctl == b->bus_dma_ctl) {
2754*0Sstevel@tonic-gate 		DEVI(devi)->devi_bus_dma_ctl = pdevi->devi_bus_dma_ctl;
2755*0Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_dma_ctl, "bus_dma_ctl");
2756*0Sstevel@tonic-gate 	}
2757*0Sstevel@tonic-gate 
2758*0Sstevel@tonic-gate 	if (ddi_ctlops == b->bus_ctl) {
2759*0Sstevel@tonic-gate 		DEVI(devi)->devi_bus_ctl = pdevi->devi_bus_ctl;
2760*0Sstevel@tonic-gate 		debug_dtree(devi, DEVI(devi)->devi_bus_ctl, "bus_ctl");
2761*0Sstevel@tonic-gate 	}
2762*0Sstevel@tonic-gate }
2763*0Sstevel@tonic-gate 
2764*0Sstevel@tonic-gate #define	MIN_DEVINFO_LOG_SIZE	max_ncpus
2765*0Sstevel@tonic-gate #define	MAX_DEVINFO_LOG_SIZE	max_ncpus * 10
2766*0Sstevel@tonic-gate 
2767*0Sstevel@tonic-gate static void
2768*0Sstevel@tonic-gate da_log_init()
2769*0Sstevel@tonic-gate {
2770*0Sstevel@tonic-gate 	devinfo_log_header_t *dh;
2771*0Sstevel@tonic-gate 	int logsize = devinfo_log_size;
2772*0Sstevel@tonic-gate 
2773*0Sstevel@tonic-gate 	if (logsize == 0)
2774*0Sstevel@tonic-gate 		logsize = MIN_DEVINFO_LOG_SIZE;
2775*0Sstevel@tonic-gate 	else if (logsize > MAX_DEVINFO_LOG_SIZE)
2776*0Sstevel@tonic-gate 		logsize = MAX_DEVINFO_LOG_SIZE;
2777*0Sstevel@tonic-gate 
2778*0Sstevel@tonic-gate 	dh = kmem_alloc(logsize * PAGESIZE, KM_SLEEP);
2779*0Sstevel@tonic-gate 	mutex_init(&dh->dh_lock, NULL, MUTEX_DEFAULT, NULL);
2780*0Sstevel@tonic-gate 	dh->dh_max = ((logsize * PAGESIZE) - sizeof (*dh)) /
2781*0Sstevel@tonic-gate 	    sizeof (devinfo_audit_t) + 1;
2782*0Sstevel@tonic-gate 	dh->dh_curr = -1;
2783*0Sstevel@tonic-gate 	dh->dh_hits = 0;
2784*0Sstevel@tonic-gate 
2785*0Sstevel@tonic-gate 	devinfo_audit_log = dh;
2786*0Sstevel@tonic-gate }
2787*0Sstevel@tonic-gate 
2788*0Sstevel@tonic-gate /*
2789*0Sstevel@tonic-gate  * Log the stack trace in per-devinfo audit structure and also enter
2790*0Sstevel@tonic-gate  * it into a system wide log for recording the time history.
2791*0Sstevel@tonic-gate  */
2792*0Sstevel@tonic-gate static void
2793*0Sstevel@tonic-gate da_log_enter(dev_info_t *dip)
2794*0Sstevel@tonic-gate {
2795*0Sstevel@tonic-gate 	devinfo_audit_t *da_log, *da = DEVI(dip)->devi_audit;
2796*0Sstevel@tonic-gate 	devinfo_log_header_t *dh = devinfo_audit_log;
2797*0Sstevel@tonic-gate 
2798*0Sstevel@tonic-gate 	if (devinfo_audit_log == NULL)
2799*0Sstevel@tonic-gate 		return;
2800*0Sstevel@tonic-gate 
2801*0Sstevel@tonic-gate 	ASSERT(da != NULL);
2802*0Sstevel@tonic-gate 
2803*0Sstevel@tonic-gate 	da->da_devinfo = dip;
2804*0Sstevel@tonic-gate 	da->da_timestamp = gethrtime();
2805*0Sstevel@tonic-gate 	da->da_thread = curthread;
2806*0Sstevel@tonic-gate 	da->da_node_state = DEVI(dip)->devi_node_state;
2807*0Sstevel@tonic-gate 	da->da_device_state = DEVI(dip)->devi_state;
2808*0Sstevel@tonic-gate 	da->da_depth = getpcstack(da->da_stack, DDI_STACK_DEPTH);
2809*0Sstevel@tonic-gate 
2810*0Sstevel@tonic-gate 	/*
2811*0Sstevel@tonic-gate 	 * Copy into common log and note the location for tracing history
2812*0Sstevel@tonic-gate 	 */
2813*0Sstevel@tonic-gate 	mutex_enter(&dh->dh_lock);
2814*0Sstevel@tonic-gate 	dh->dh_hits++;
2815*0Sstevel@tonic-gate 	dh->dh_curr++;
2816*0Sstevel@tonic-gate 	if (dh->dh_curr >= dh->dh_max)
2817*0Sstevel@tonic-gate 		dh->dh_curr -= dh->dh_max;
2818*0Sstevel@tonic-gate 	da_log = &dh->dh_entry[dh->dh_curr];
2819*0Sstevel@tonic-gate 	mutex_exit(&dh->dh_lock);
2820*0Sstevel@tonic-gate 
2821*0Sstevel@tonic-gate 	bcopy(da, da_log, sizeof (devinfo_audit_t));
2822*0Sstevel@tonic-gate 	da->da_lastlog = da_log;
2823*0Sstevel@tonic-gate }
2824*0Sstevel@tonic-gate 
2825*0Sstevel@tonic-gate static void
2826*0Sstevel@tonic-gate attach_drivers()
2827*0Sstevel@tonic-gate {
2828*0Sstevel@tonic-gate 	int i;
2829*0Sstevel@tonic-gate 	for (i = 0; i < devcnt; i++) {
2830*0Sstevel@tonic-gate 		struct devnames *dnp = &devnamesp[i];
2831*0Sstevel@tonic-gate 		if ((dnp->dn_flags & DN_FORCE_ATTACH) &&
2832*0Sstevel@tonic-gate 		    (ddi_hold_installed_driver((major_t)i) != NULL))
2833*0Sstevel@tonic-gate 			ddi_rele_driver((major_t)i);
2834*0Sstevel@tonic-gate 	}
2835*0Sstevel@tonic-gate }
2836*0Sstevel@tonic-gate 
2837*0Sstevel@tonic-gate /*
2838*0Sstevel@tonic-gate  * Launch a thread to force attach drivers. This avoids penalty on boot time.
2839*0Sstevel@tonic-gate  */
2840*0Sstevel@tonic-gate void
2841*0Sstevel@tonic-gate i_ddi_forceattach_drivers()
2842*0Sstevel@tonic-gate {
2843*0Sstevel@tonic-gate 	/*
2844*0Sstevel@tonic-gate 	 * On i386, the USB drivers need to load and take over from the
2845*0Sstevel@tonic-gate 	 * SMM BIOS drivers ASAP after consconfig(), so make sure they
2846*0Sstevel@tonic-gate 	 * get loaded right here rather than letting the thread do it.
2847*0Sstevel@tonic-gate 	 *
2848*0Sstevel@tonic-gate 	 * The order here is important.  EHCI must be loaded first, as
2849*0Sstevel@tonic-gate 	 * we have observed many systems on which hangs occur if the
2850*0Sstevel@tonic-gate 	 * {U,O}HCI companion controllers take over control from the BIOS
2851*0Sstevel@tonic-gate 	 * before EHCI does.  These hangs are also caused by BIOSes leaving
2852*0Sstevel@tonic-gate 	 * interrupt-on-port-change enabled in the ehci controller, so that
2853*0Sstevel@tonic-gate 	 * when uhci/ohci reset themselves, it induces a port change on
2854*0Sstevel@tonic-gate 	 * the ehci companion controller.  Since there's no interrupt handler
2855*0Sstevel@tonic-gate 	 * installed at the time, the moment that interrupt is unmasked, an
2856*0Sstevel@tonic-gate 	 * interrupt storm will occur.  All this is averted when ehci is
2857*0Sstevel@tonic-gate 	 * loaded first.  And now you know..... the REST of the story.
2858*0Sstevel@tonic-gate 	 *
2859*0Sstevel@tonic-gate 	 * Regardless of platform, ehci needs to initialize first to avoid
2860*0Sstevel@tonic-gate 	 * unnecessary connects and disconnects on the companion controller
2861*0Sstevel@tonic-gate 	 * when ehci sets up the routing.
2862*0Sstevel@tonic-gate 	 */
2863*0Sstevel@tonic-gate 	(void) ddi_hold_installed_driver(ddi_name_to_major("ehci"));
2864*0Sstevel@tonic-gate 	(void) ddi_hold_installed_driver(ddi_name_to_major("uhci"));
2865*0Sstevel@tonic-gate 	(void) ddi_hold_installed_driver(ddi_name_to_major("ohci"));
2866*0Sstevel@tonic-gate 
2867*0Sstevel@tonic-gate 	(void) thread_create(NULL, 0, (void (*)())attach_drivers, NULL, 0, &p0,
2868*0Sstevel@tonic-gate 	    TS_RUN, minclsyspri);
2869*0Sstevel@tonic-gate }
2870*0Sstevel@tonic-gate 
2871*0Sstevel@tonic-gate /*
2872*0Sstevel@tonic-gate  * This is a private DDI interface for optimizing boot performance.
2873*0Sstevel@tonic-gate  * I/O subsystem initialization is considered complete when devfsadm
2874*0Sstevel@tonic-gate  * is executed.
2875*0Sstevel@tonic-gate  *
2876*0Sstevel@tonic-gate  * NOTE: The start of syseventd in S60devfsadm happen to be convenient
2877*0Sstevel@tonic-gate  *	indicator for the completion of I/O initialization during boot.
2878*0Sstevel@tonic-gate  *	The implementation should be replaced by something more robust.
2879*0Sstevel@tonic-gate  */
2880*0Sstevel@tonic-gate int
2881*0Sstevel@tonic-gate i_ddi_io_initialized()
2882*0Sstevel@tonic-gate {
2883*0Sstevel@tonic-gate 	extern int sysevent_daemon_init;
2884*0Sstevel@tonic-gate 	return (sysevent_daemon_init);
2885*0Sstevel@tonic-gate }
2886*0Sstevel@tonic-gate 
2887*0Sstevel@tonic-gate 
2888*0Sstevel@tonic-gate /*
2889*0Sstevel@tonic-gate  * device tree walking
2890*0Sstevel@tonic-gate  */
2891*0Sstevel@tonic-gate 
2892*0Sstevel@tonic-gate struct walk_elem {
2893*0Sstevel@tonic-gate 	struct walk_elem *next;
2894*0Sstevel@tonic-gate 	dev_info_t *dip;
2895*0Sstevel@tonic-gate };
2896*0Sstevel@tonic-gate 
2897*0Sstevel@tonic-gate static void
2898*0Sstevel@tonic-gate free_list(struct walk_elem *list)
2899*0Sstevel@tonic-gate {
2900*0Sstevel@tonic-gate 	while (list) {
2901*0Sstevel@tonic-gate 		struct walk_elem *next = list->next;
2902*0Sstevel@tonic-gate 		kmem_free(list, sizeof (*list));
2903*0Sstevel@tonic-gate 		list = next;
2904*0Sstevel@tonic-gate 	}
2905*0Sstevel@tonic-gate }
2906*0Sstevel@tonic-gate 
2907*0Sstevel@tonic-gate static void
2908*0Sstevel@tonic-gate append_node(struct walk_elem **list, dev_info_t *dip)
2909*0Sstevel@tonic-gate {
2910*0Sstevel@tonic-gate 	struct walk_elem *tail;
2911*0Sstevel@tonic-gate 	struct walk_elem *elem = kmem_alloc(sizeof (*elem), KM_SLEEP);
2912*0Sstevel@tonic-gate 
2913*0Sstevel@tonic-gate 	elem->next = NULL;
2914*0Sstevel@tonic-gate 	elem->dip = dip;
2915*0Sstevel@tonic-gate 
2916*0Sstevel@tonic-gate 	if (*list == NULL) {
2917*0Sstevel@tonic-gate 		*list = elem;
2918*0Sstevel@tonic-gate 		return;
2919*0Sstevel@tonic-gate 	}
2920*0Sstevel@tonic-gate 
2921*0Sstevel@tonic-gate 	tail = *list;
2922*0Sstevel@tonic-gate 	while (tail->next)
2923*0Sstevel@tonic-gate 		tail = tail->next;
2924*0Sstevel@tonic-gate 
2925*0Sstevel@tonic-gate 	tail->next = elem;
2926*0Sstevel@tonic-gate }
2927*0Sstevel@tonic-gate 
2928*0Sstevel@tonic-gate /*
2929*0Sstevel@tonic-gate  * The implementation of ddi_walk_devs().
2930*0Sstevel@tonic-gate  */
2931*0Sstevel@tonic-gate static int
2932*0Sstevel@tonic-gate walk_devs(dev_info_t *dip, int (*f)(dev_info_t *, void *), void *arg,
2933*0Sstevel@tonic-gate     int do_locking)
2934*0Sstevel@tonic-gate {
2935*0Sstevel@tonic-gate 	struct walk_elem *head = NULL;
2936*0Sstevel@tonic-gate 
2937*0Sstevel@tonic-gate 	/*
2938*0Sstevel@tonic-gate 	 * Do it in two passes. First pass invoke callback on each
2939*0Sstevel@tonic-gate 	 * dip on the sibling list. Second pass invoke callback on
2940*0Sstevel@tonic-gate 	 * children of each dip.
2941*0Sstevel@tonic-gate 	 */
2942*0Sstevel@tonic-gate 	while (dip) {
2943*0Sstevel@tonic-gate 		switch ((*f)(dip, arg)) {
2944*0Sstevel@tonic-gate 		case DDI_WALK_TERMINATE:
2945*0Sstevel@tonic-gate 			free_list(head);
2946*0Sstevel@tonic-gate 			return (DDI_WALK_TERMINATE);
2947*0Sstevel@tonic-gate 
2948*0Sstevel@tonic-gate 		case DDI_WALK_PRUNESIB:
2949*0Sstevel@tonic-gate 			/* ignore sibling by setting dip to NULL */
2950*0Sstevel@tonic-gate 			append_node(&head, dip);
2951*0Sstevel@tonic-gate 			dip = NULL;
2952*0Sstevel@tonic-gate 			break;
2953*0Sstevel@tonic-gate 
2954*0Sstevel@tonic-gate 		case DDI_WALK_PRUNECHILD:
2955*0Sstevel@tonic-gate 			/* don't worry about children */
2956*0Sstevel@tonic-gate 			dip = ddi_get_next_sibling(dip);
2957*0Sstevel@tonic-gate 			break;
2958*0Sstevel@tonic-gate 
2959*0Sstevel@tonic-gate 		case DDI_WALK_CONTINUE:
2960*0Sstevel@tonic-gate 		default:
2961*0Sstevel@tonic-gate 			append_node(&head, dip);
2962*0Sstevel@tonic-gate 			dip = ddi_get_next_sibling(dip);
2963*0Sstevel@tonic-gate 			break;
2964*0Sstevel@tonic-gate 		}
2965*0Sstevel@tonic-gate 
2966*0Sstevel@tonic-gate 	}
2967*0Sstevel@tonic-gate 
2968*0Sstevel@tonic-gate 	/* second pass */
2969*0Sstevel@tonic-gate 	while (head) {
2970*0Sstevel@tonic-gate 		int circ;
2971*0Sstevel@tonic-gate 		struct walk_elem *next = head->next;
2972*0Sstevel@tonic-gate 
2973*0Sstevel@tonic-gate 		if (do_locking)
2974*0Sstevel@tonic-gate 			ndi_devi_enter(head->dip, &circ);
2975*0Sstevel@tonic-gate 		if (walk_devs(ddi_get_child(head->dip), f, arg, do_locking) ==
2976*0Sstevel@tonic-gate 		    DDI_WALK_TERMINATE) {
2977*0Sstevel@tonic-gate 			if (do_locking)
2978*0Sstevel@tonic-gate 				ndi_devi_exit(head->dip, circ);
2979*0Sstevel@tonic-gate 			free_list(head);
2980*0Sstevel@tonic-gate 			return (DDI_WALK_TERMINATE);
2981*0Sstevel@tonic-gate 		}
2982*0Sstevel@tonic-gate 		if (do_locking)
2983*0Sstevel@tonic-gate 			ndi_devi_exit(head->dip, circ);
2984*0Sstevel@tonic-gate 		kmem_free(head, sizeof (*head));
2985*0Sstevel@tonic-gate 		head = next;
2986*0Sstevel@tonic-gate 	}
2987*0Sstevel@tonic-gate 
2988*0Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
2989*0Sstevel@tonic-gate }
2990*0Sstevel@tonic-gate 
2991*0Sstevel@tonic-gate /*
2992*0Sstevel@tonic-gate  * This general-purpose routine traverses the tree of dev_info nodes,
2993*0Sstevel@tonic-gate  * starting from the given node, and calls the given function for each
2994*0Sstevel@tonic-gate  * node that it finds with the current node and the pointer arg (which
2995*0Sstevel@tonic-gate  * can point to a structure of information that the function
2996*0Sstevel@tonic-gate  * needs) as arguments.
2997*0Sstevel@tonic-gate  *
2998*0Sstevel@tonic-gate  * It does the walk a layer at a time, not depth-first. The given function
2999*0Sstevel@tonic-gate  * must return one of the following values:
3000*0Sstevel@tonic-gate  *	DDI_WALK_CONTINUE
3001*0Sstevel@tonic-gate  *	DDI_WALK_PRUNESIB
3002*0Sstevel@tonic-gate  *	DDI_WALK_PRUNECHILD
3003*0Sstevel@tonic-gate  *	DDI_WALK_TERMINATE
3004*0Sstevel@tonic-gate  *
3005*0Sstevel@tonic-gate  * N.B. Since we walk the sibling list, the caller must ensure that
3006*0Sstevel@tonic-gate  *	the parent of dip is held against changes, unless the parent
3007*0Sstevel@tonic-gate  *	is rootnode.  ndi_devi_enter() on the parent is sufficient.
3008*0Sstevel@tonic-gate  *
3009*0Sstevel@tonic-gate  *	To avoid deadlock situations, caller must not attempt to
3010*0Sstevel@tonic-gate  *	configure/unconfigure/remove device node in (*f)(), nor should
3011*0Sstevel@tonic-gate  *	it attempt to recurse on other nodes in the system.
3012*0Sstevel@tonic-gate  *
3013*0Sstevel@tonic-gate  *	This is not callable from device autoconfiguration routines.
3014*0Sstevel@tonic-gate  *	They include, but not limited to, _init(9e), _fini(9e), probe(9e),
3015*0Sstevel@tonic-gate  *	attach(9e), and detach(9e).
3016*0Sstevel@tonic-gate  */
3017*0Sstevel@tonic-gate 
3018*0Sstevel@tonic-gate void
3019*0Sstevel@tonic-gate ddi_walk_devs(dev_info_t *dip, int (*f)(dev_info_t *, void *), void *arg)
3020*0Sstevel@tonic-gate {
3021*0Sstevel@tonic-gate 
3022*0Sstevel@tonic-gate 	ASSERT(dip == NULL || ddi_get_parent(dip) == NULL ||
3023*0Sstevel@tonic-gate 		DEVI_BUSY_OWNED(ddi_get_parent(dip)));
3024*0Sstevel@tonic-gate 
3025*0Sstevel@tonic-gate 	(void) walk_devs(dip, f, arg, 1);
3026*0Sstevel@tonic-gate }
3027*0Sstevel@tonic-gate 
3028*0Sstevel@tonic-gate /*
3029*0Sstevel@tonic-gate  * This is a general-purpose routine traverses the per-driver list
3030*0Sstevel@tonic-gate  * and calls the given function for each node. must return one of
3031*0Sstevel@tonic-gate  * the following values:
3032*0Sstevel@tonic-gate  *	DDI_WALK_CONTINUE
3033*0Sstevel@tonic-gate  *	DDI_WALK_TERMINATE
3034*0Sstevel@tonic-gate  *
3035*0Sstevel@tonic-gate  * N.B. The same restrictions from ddi_walk_devs() apply.
3036*0Sstevel@tonic-gate  */
3037*0Sstevel@tonic-gate 
3038*0Sstevel@tonic-gate void
3039*0Sstevel@tonic-gate e_ddi_walk_driver(char *drv, int (*f)(dev_info_t *, void *), void *arg)
3040*0Sstevel@tonic-gate {
3041*0Sstevel@tonic-gate 	major_t major;
3042*0Sstevel@tonic-gate 	struct devnames *dnp;
3043*0Sstevel@tonic-gate 	dev_info_t *dip;
3044*0Sstevel@tonic-gate 
3045*0Sstevel@tonic-gate 	major = ddi_name_to_major(drv);
3046*0Sstevel@tonic-gate 	if (major == (major_t)-1)
3047*0Sstevel@tonic-gate 		return;
3048*0Sstevel@tonic-gate 
3049*0Sstevel@tonic-gate 	dnp = &devnamesp[major];
3050*0Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
3051*0Sstevel@tonic-gate 	dip = dnp->dn_head;
3052*0Sstevel@tonic-gate 	while (dip) {
3053*0Sstevel@tonic-gate 		ndi_hold_devi(dip);
3054*0Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
3055*0Sstevel@tonic-gate 		if ((*f)(dip, arg) == DDI_WALK_TERMINATE) {
3056*0Sstevel@tonic-gate 			ndi_rele_devi(dip);
3057*0Sstevel@tonic-gate 			return;
3058*0Sstevel@tonic-gate 		}
3059*0Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
3060*0Sstevel@tonic-gate 		ndi_rele_devi(dip);
3061*0Sstevel@tonic-gate 		dip = ddi_get_next(dip);
3062*0Sstevel@tonic-gate 	}
3063*0Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
3064*0Sstevel@tonic-gate }
3065*0Sstevel@tonic-gate 
3066*0Sstevel@tonic-gate /*
3067*0Sstevel@tonic-gate  * argument to i_find_devi, a devinfo node search callback function.
3068*0Sstevel@tonic-gate  */
3069*0Sstevel@tonic-gate struct match_info {
3070*0Sstevel@tonic-gate 	dev_info_t	*dip;		/* result */
3071*0Sstevel@tonic-gate 	char		*nodename;	/* if non-null, nodename must match */
3072*0Sstevel@tonic-gate 	int		instance;	/* if != -1, instance must match */
3073*0Sstevel@tonic-gate 	int		attached;	/* if != 0, state >= DS_ATTACHED */
3074*0Sstevel@tonic-gate };
3075*0Sstevel@tonic-gate 
3076*0Sstevel@tonic-gate static int
3077*0Sstevel@tonic-gate i_find_devi(dev_info_t *dip, void *arg)
3078*0Sstevel@tonic-gate {
3079*0Sstevel@tonic-gate 	struct match_info *info = (struct match_info *)arg;
3080*0Sstevel@tonic-gate 
3081*0Sstevel@tonic-gate 	if (((info->nodename == NULL) ||
3082*0Sstevel@tonic-gate 		(strcmp(ddi_node_name(dip), info->nodename) == 0)) &&
3083*0Sstevel@tonic-gate 	    ((info->instance == -1) ||
3084*0Sstevel@tonic-gate 		(ddi_get_instance(dip) == info->instance)) &&
3085*0Sstevel@tonic-gate 	    ((info->attached == 0) ||
3086*0Sstevel@tonic-gate 		(i_ddi_node_state(dip) >= DS_ATTACHED))) {
3087*0Sstevel@tonic-gate 		info->dip = dip;
3088*0Sstevel@tonic-gate 		ndi_hold_devi(dip);
3089*0Sstevel@tonic-gate 		return (DDI_WALK_TERMINATE);
3090*0Sstevel@tonic-gate 	}
3091*0Sstevel@tonic-gate 
3092*0Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
3093*0Sstevel@tonic-gate }
3094*0Sstevel@tonic-gate 
3095*0Sstevel@tonic-gate /*
3096*0Sstevel@tonic-gate  * Find dip with a known node name and instance and return with it held
3097*0Sstevel@tonic-gate  */
3098*0Sstevel@tonic-gate dev_info_t *
3099*0Sstevel@tonic-gate ddi_find_devinfo(char *nodename, int instance, int attached)
3100*0Sstevel@tonic-gate {
3101*0Sstevel@tonic-gate 	struct match_info	info;
3102*0Sstevel@tonic-gate 
3103*0Sstevel@tonic-gate 	info.nodename = nodename;
3104*0Sstevel@tonic-gate 	info.instance = instance;
3105*0Sstevel@tonic-gate 	info.attached = attached;
3106*0Sstevel@tonic-gate 	info.dip = NULL;
3107*0Sstevel@tonic-gate 
3108*0Sstevel@tonic-gate 	ddi_walk_devs(ddi_root_node(), i_find_devi, &info);
3109*0Sstevel@tonic-gate 	return (info.dip);
3110*0Sstevel@tonic-gate }
3111*0Sstevel@tonic-gate 
3112*0Sstevel@tonic-gate /*
3113*0Sstevel@tonic-gate  * Parse for name, addr, and minor names. Some args may be NULL.
3114*0Sstevel@tonic-gate  */
3115*0Sstevel@tonic-gate void
3116*0Sstevel@tonic-gate i_ddi_parse_name(char *name, char **nodename, char **addrname, char **minorname)
3117*0Sstevel@tonic-gate {
3118*0Sstevel@tonic-gate 	char *cp;
3119*0Sstevel@tonic-gate 	static char nulladdrname[] = "";
3120*0Sstevel@tonic-gate 
3121*0Sstevel@tonic-gate 	/* default values */
3122*0Sstevel@tonic-gate 	if (nodename)
3123*0Sstevel@tonic-gate 		*nodename = name;
3124*0Sstevel@tonic-gate 	if (addrname)
3125*0Sstevel@tonic-gate 		*addrname = nulladdrname;
3126*0Sstevel@tonic-gate 	if (minorname)
3127*0Sstevel@tonic-gate 		*minorname = NULL;
3128*0Sstevel@tonic-gate 
3129*0Sstevel@tonic-gate 	cp = name;
3130*0Sstevel@tonic-gate 	while (*cp != '\0') {
3131*0Sstevel@tonic-gate 		if (addrname && *cp == '@') {
3132*0Sstevel@tonic-gate 			*addrname = cp + 1;
3133*0Sstevel@tonic-gate 			*cp = '\0';
3134*0Sstevel@tonic-gate 		} else if (minorname && *cp == ':') {
3135*0Sstevel@tonic-gate 			*minorname = cp + 1;
3136*0Sstevel@tonic-gate 			*cp = '\0';
3137*0Sstevel@tonic-gate 		}
3138*0Sstevel@tonic-gate 		++cp;
3139*0Sstevel@tonic-gate 	}
3140*0Sstevel@tonic-gate }
3141*0Sstevel@tonic-gate 
3142*0Sstevel@tonic-gate static char *
3143*0Sstevel@tonic-gate child_path_to_driver(dev_info_t *parent, char *child_name, char *unit_address)
3144*0Sstevel@tonic-gate {
3145*0Sstevel@tonic-gate 	char *p, *drvname = NULL;
3146*0Sstevel@tonic-gate 	major_t maj;
3147*0Sstevel@tonic-gate 
3148*0Sstevel@tonic-gate 	/*
3149*0Sstevel@tonic-gate 	 * Construct the pathname and ask the implementation
3150*0Sstevel@tonic-gate 	 * if it can do a driver = f(pathname) for us, if not
3151*0Sstevel@tonic-gate 	 * we'll just default to using the node-name that
3152*0Sstevel@tonic-gate 	 * was given to us.  We want to do this first to
3153*0Sstevel@tonic-gate 	 * allow the platform to use 'generic' names for
3154*0Sstevel@tonic-gate 	 * legacy device drivers.
3155*0Sstevel@tonic-gate 	 */
3156*0Sstevel@tonic-gate 	p = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
3157*0Sstevel@tonic-gate 	(void) ddi_pathname(parent, p);
3158*0Sstevel@tonic-gate 	(void) strcat(p, "/");
3159*0Sstevel@tonic-gate 	(void) strcat(p, child_name);
3160*0Sstevel@tonic-gate 	if (unit_address && *unit_address) {
3161*0Sstevel@tonic-gate 		(void) strcat(p, "@");
3162*0Sstevel@tonic-gate 		(void) strcat(p, unit_address);
3163*0Sstevel@tonic-gate 	}
3164*0Sstevel@tonic-gate 
3165*0Sstevel@tonic-gate 	/*
3166*0Sstevel@tonic-gate 	 * Get the binding. If there is none, return the child_name
3167*0Sstevel@tonic-gate 	 * and let the caller deal with it.
3168*0Sstevel@tonic-gate 	 */
3169*0Sstevel@tonic-gate 	maj = path_to_major(p);
3170*0Sstevel@tonic-gate 
3171*0Sstevel@tonic-gate 	kmem_free(p, MAXPATHLEN);
3172*0Sstevel@tonic-gate 
3173*0Sstevel@tonic-gate 	if (maj != (major_t)-1)
3174*0Sstevel@tonic-gate 		drvname = ddi_major_to_name(maj);
3175*0Sstevel@tonic-gate 	if (drvname == NULL)
3176*0Sstevel@tonic-gate 		drvname = child_name;
3177*0Sstevel@tonic-gate 
3178*0Sstevel@tonic-gate 	return (drvname);
3179*0Sstevel@tonic-gate }
3180*0Sstevel@tonic-gate 
3181*0Sstevel@tonic-gate 
3182*0Sstevel@tonic-gate /*
3183*0Sstevel@tonic-gate  * Given the pathname of a device, fill in the dev_info_t value and/or the
3184*0Sstevel@tonic-gate  * dev_t value and/or the spectype, depending on which parameters are non-NULL.
3185*0Sstevel@tonic-gate  * If there is an error, this function returns -1.
3186*0Sstevel@tonic-gate  *
3187*0Sstevel@tonic-gate  * NOTE: If this function returns the dev_info_t structure, then it
3188*0Sstevel@tonic-gate  * does so with a hold on the devi. Caller should ensure that they get
3189*0Sstevel@tonic-gate  * decremented via ddi_release_devi() or ndi_rele_devi();
3190*0Sstevel@tonic-gate  *
3191*0Sstevel@tonic-gate  * This function can be invoked in the boot case for a pathname without
3192*0Sstevel@tonic-gate  * device argument (:xxxx), traditionally treated as a minor name.
3193*0Sstevel@tonic-gate  * In this case, we do the following
3194*0Sstevel@tonic-gate  * (1) search the minor node of type DDM_DEFAULT.
3195*0Sstevel@tonic-gate  * (2) if no DDM_DEFAULT minor exists, then the first non-alias minor is chosen.
3196*0Sstevel@tonic-gate  * (3) if neither exists, a dev_t is faked with minor number = instance.
3197*0Sstevel@tonic-gate  * As of S9 FCS, no instance of #1 exists. #2 is used by several platforms
3198*0Sstevel@tonic-gate  * to default the boot partition to :a possibly by other OBP definitions.
3199*0Sstevel@tonic-gate  * #3 is used for booting off network interfaces, most SPARC network
3200*0Sstevel@tonic-gate  * drivers support Style-2 only, so only DDM_ALIAS minor exists.
3201*0Sstevel@tonic-gate  *
3202*0Sstevel@tonic-gate  * It is possible for OBP to present device args at the end of the path as
3203*0Sstevel@tonic-gate  * well as in the middle. For example, with IB the following strings are
3204*0Sstevel@tonic-gate  * valid boot paths.
3205*0Sstevel@tonic-gate  *	a /pci@8,700000/ib@1,2:port=1,pkey=ff,dhcp,...
3206*0Sstevel@tonic-gate  *	b /pci@8,700000/ib@1,1:port=1/ioc@xxxxxx,yyyyyyy:dhcp
3207*0Sstevel@tonic-gate  * Case (a), we first look for minor node "port=1,pkey...".
3208*0Sstevel@tonic-gate  * Failing that, we will pass "port=1,pkey..." to the bus_config
3209*0Sstevel@tonic-gate  * entry point of ib (HCA) driver.
3210*0Sstevel@tonic-gate  * Case (b), configure ib@1,1 as usual. Then invoke ib's bus_config
3211*0Sstevel@tonic-gate  * with argument "ioc@xxxxxxx,yyyyyyy:port=1". After configuring
3212*0Sstevel@tonic-gate  * the ioc, look for minor node dhcp. If not found, pass ":dhcp"
3213*0Sstevel@tonic-gate  * to ioc's bus_config entry point.
3214*0Sstevel@tonic-gate  */
3215*0Sstevel@tonic-gate int
3216*0Sstevel@tonic-gate resolve_pathname(char *pathname,
3217*0Sstevel@tonic-gate 	dev_info_t **dipp, dev_t *devtp, int *spectypep)
3218*0Sstevel@tonic-gate {
3219*0Sstevel@tonic-gate 	int error;
3220*0Sstevel@tonic-gate 	dev_info_t *parent, *child;
3221*0Sstevel@tonic-gate 	struct pathname pn;
3222*0Sstevel@tonic-gate 	char *component, *config_name;
3223*0Sstevel@tonic-gate 	char *minorname = NULL;
3224*0Sstevel@tonic-gate 	char *prev_minor = NULL;
3225*0Sstevel@tonic-gate 	dev_t devt = NODEV;
3226*0Sstevel@tonic-gate 	int spectype;
3227*0Sstevel@tonic-gate 	struct ddi_minor_data *dmn;
3228*0Sstevel@tonic-gate 
3229*0Sstevel@tonic-gate 	if (*pathname != '/')
3230*0Sstevel@tonic-gate 		return (EINVAL);
3231*0Sstevel@tonic-gate 	parent = ddi_root_node();	/* Begin at the top of the tree */
3232*0Sstevel@tonic-gate 
3233*0Sstevel@tonic-gate 	if (error = pn_get(pathname, UIO_SYSSPACE, &pn))
3234*0Sstevel@tonic-gate 		return (error);
3235*0Sstevel@tonic-gate 	pn_skipslash(&pn);
3236*0Sstevel@tonic-gate 
3237*0Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(parent) >= DS_ATTACHED);
3238*0Sstevel@tonic-gate 	ndi_hold_devi(parent);
3239*0Sstevel@tonic-gate 
3240*0Sstevel@tonic-gate 	component = kmem_alloc(MAXNAMELEN, KM_SLEEP);
3241*0Sstevel@tonic-gate 	config_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
3242*0Sstevel@tonic-gate 
3243*0Sstevel@tonic-gate 	while (pn_pathleft(&pn)) {
3244*0Sstevel@tonic-gate 		/* remember prev minor (:xxx) in the middle of path */
3245*0Sstevel@tonic-gate 		if (minorname)
3246*0Sstevel@tonic-gate 			prev_minor = i_ddi_strdup(minorname, KM_SLEEP);
3247*0Sstevel@tonic-gate 
3248*0Sstevel@tonic-gate 		/* Get component and chop off minorname */
3249*0Sstevel@tonic-gate 		(void) pn_getcomponent(&pn, component);
3250*0Sstevel@tonic-gate 		i_ddi_parse_name(component, NULL, NULL, &minorname);
3251*0Sstevel@tonic-gate 
3252*0Sstevel@tonic-gate 		if (prev_minor == NULL) {
3253*0Sstevel@tonic-gate 			(void) snprintf(config_name, MAXNAMELEN, "%s",
3254*0Sstevel@tonic-gate 			    component);
3255*0Sstevel@tonic-gate 		} else {
3256*0Sstevel@tonic-gate 			(void) snprintf(config_name, MAXNAMELEN, "%s:%s",
3257*0Sstevel@tonic-gate 			    component, prev_minor);
3258*0Sstevel@tonic-gate 			kmem_free(prev_minor, strlen(prev_minor) + 1);
3259*0Sstevel@tonic-gate 			prev_minor = NULL;
3260*0Sstevel@tonic-gate 		}
3261*0Sstevel@tonic-gate 
3262*0Sstevel@tonic-gate 		/*
3263*0Sstevel@tonic-gate 		 * Find and configure the child
3264*0Sstevel@tonic-gate 		 */
3265*0Sstevel@tonic-gate 		if (ndi_devi_config_one(parent, config_name, &child,
3266*0Sstevel@tonic-gate 		    NDI_PROMNAME | NDI_NO_EVENT) != NDI_SUCCESS) {
3267*0Sstevel@tonic-gate 			ndi_rele_devi(parent);
3268*0Sstevel@tonic-gate 			pn_free(&pn);
3269*0Sstevel@tonic-gate 			kmem_free(component, MAXNAMELEN);
3270*0Sstevel@tonic-gate 			kmem_free(config_name, MAXNAMELEN);
3271*0Sstevel@tonic-gate 			return (-1);
3272*0Sstevel@tonic-gate 		}
3273*0Sstevel@tonic-gate 
3274*0Sstevel@tonic-gate 		ASSERT(i_ddi_node_state(child) >= DS_ATTACHED);
3275*0Sstevel@tonic-gate 		ndi_rele_devi(parent);
3276*0Sstevel@tonic-gate 		parent = child;
3277*0Sstevel@tonic-gate 		pn_skipslash(&pn);
3278*0Sstevel@tonic-gate 	}
3279*0Sstevel@tonic-gate 
3280*0Sstevel@tonic-gate 	/*
3281*0Sstevel@tonic-gate 	 * First look for a minor node matching minorname.
3282*0Sstevel@tonic-gate 	 * Failing that, try to pass minorname to bus_config().
3283*0Sstevel@tonic-gate 	 */
3284*0Sstevel@tonic-gate 	if (minorname && i_ddi_minorname_to_devtspectype(parent,
3285*0Sstevel@tonic-gate 	    minorname, &devt, &spectype) == DDI_FAILURE) {
3286*0Sstevel@tonic-gate 		(void) snprintf(config_name, MAXNAMELEN, "%s", minorname);
3287*0Sstevel@tonic-gate 		if (ndi_devi_config_obp_args(parent,
3288*0Sstevel@tonic-gate 		    config_name, &child, 0) != NDI_SUCCESS) {
3289*0Sstevel@tonic-gate 			ndi_rele_devi(parent);
3290*0Sstevel@tonic-gate 			pn_free(&pn);
3291*0Sstevel@tonic-gate 			kmem_free(component, MAXNAMELEN);
3292*0Sstevel@tonic-gate 			kmem_free(config_name, MAXNAMELEN);
3293*0Sstevel@tonic-gate 			NDI_CONFIG_DEBUG((CE_NOTE,
3294*0Sstevel@tonic-gate 			    "%s: minor node not found\n", pathname));
3295*0Sstevel@tonic-gate 			return (-1);
3296*0Sstevel@tonic-gate 		}
3297*0Sstevel@tonic-gate 		minorname = NULL;	/* look for default minor */
3298*0Sstevel@tonic-gate 		ASSERT(i_ddi_node_state(child) >= DS_ATTACHED);
3299*0Sstevel@tonic-gate 		ndi_rele_devi(parent);
3300*0Sstevel@tonic-gate 		parent = child;
3301*0Sstevel@tonic-gate 	}
3302*0Sstevel@tonic-gate 
3303*0Sstevel@tonic-gate 	if (devtp || spectypep) {
3304*0Sstevel@tonic-gate 		if (minorname == NULL) {
3305*0Sstevel@tonic-gate 			/* search for a default entry */
3306*0Sstevel@tonic-gate 			mutex_enter(&(DEVI(parent)->devi_lock));
3307*0Sstevel@tonic-gate 			for (dmn = DEVI(parent)->devi_minor; dmn;
3308*0Sstevel@tonic-gate 			    dmn = dmn->next) {
3309*0Sstevel@tonic-gate 				if (dmn->type == DDM_DEFAULT) {
3310*0Sstevel@tonic-gate 					devt = dmn->ddm_dev;
3311*0Sstevel@tonic-gate 					spectype = dmn->ddm_spec_type;
3312*0Sstevel@tonic-gate 					break;
3313*0Sstevel@tonic-gate 				}
3314*0Sstevel@tonic-gate 			}
3315*0Sstevel@tonic-gate 
3316*0Sstevel@tonic-gate 			if (devt == NODEV) {
3317*0Sstevel@tonic-gate 				/*
3318*0Sstevel@tonic-gate 				 * No default minor node, try the first one;
3319*0Sstevel@tonic-gate 				 * else, assume 1-1 instance-minor mapping
3320*0Sstevel@tonic-gate 				 */
3321*0Sstevel@tonic-gate 				dmn = DEVI(parent)->devi_minor;
3322*0Sstevel@tonic-gate 				if (dmn && ((dmn->type == DDM_MINOR) ||
3323*0Sstevel@tonic-gate 				    (dmn->type == DDM_INTERNAL_PATH))) {
3324*0Sstevel@tonic-gate 					devt = dmn->ddm_dev;
3325*0Sstevel@tonic-gate 					spectype = dmn->ddm_spec_type;
3326*0Sstevel@tonic-gate 				} else {
3327*0Sstevel@tonic-gate 					devt = makedevice(
3328*0Sstevel@tonic-gate 					    DEVI(parent)->devi_major,
3329*0Sstevel@tonic-gate 					    ddi_get_instance(parent));
3330*0Sstevel@tonic-gate 					spectype = S_IFCHR;
3331*0Sstevel@tonic-gate 				}
3332*0Sstevel@tonic-gate 			}
3333*0Sstevel@tonic-gate 			mutex_exit(&(DEVI(parent)->devi_lock));
3334*0Sstevel@tonic-gate 		}
3335*0Sstevel@tonic-gate 		if (devtp)
3336*0Sstevel@tonic-gate 			*devtp = devt;
3337*0Sstevel@tonic-gate 		if (spectypep)
3338*0Sstevel@tonic-gate 			*spectypep = spectype;
3339*0Sstevel@tonic-gate 	}
3340*0Sstevel@tonic-gate 
3341*0Sstevel@tonic-gate 	pn_free(&pn);
3342*0Sstevel@tonic-gate 	kmem_free(component, MAXNAMELEN);
3343*0Sstevel@tonic-gate 	kmem_free(config_name, MAXNAMELEN);
3344*0Sstevel@tonic-gate 
3345*0Sstevel@tonic-gate 	/*
3346*0Sstevel@tonic-gate 	 * If there is no error, return the appropriate parameters
3347*0Sstevel@tonic-gate 	 */
3348*0Sstevel@tonic-gate 	if (dipp != NULL)
3349*0Sstevel@tonic-gate 		*dipp = parent;
3350*0Sstevel@tonic-gate 	else {
3351*0Sstevel@tonic-gate 		/*
3352*0Sstevel@tonic-gate 		 * We should really keep the ref count to keep the node from
3353*0Sstevel@tonic-gate 		 * detaching but ddi_pathname_to_dev_t() specifies a NULL dipp,
3354*0Sstevel@tonic-gate 		 * so we have no way of passing back the held dip.  Not holding
3355*0Sstevel@tonic-gate 		 * the dip allows detaches to occur - which can cause problems
3356*0Sstevel@tonic-gate 		 * for subsystems which call ddi_pathname_to_dev_t (console).
3357*0Sstevel@tonic-gate 		 *
3358*0Sstevel@tonic-gate 		 * Instead of holding the dip, we place a ddi-no-autodetach
3359*0Sstevel@tonic-gate 		 * property on the node to prevent auto detaching.
3360*0Sstevel@tonic-gate 		 *
3361*0Sstevel@tonic-gate 		 * The right fix is to remove ddi_pathname_to_dev_t and replace
3362*0Sstevel@tonic-gate 		 * it, and all references, with a call that specifies a dipp.
3363*0Sstevel@tonic-gate 		 * In addition, the callers of this new interfaces would then
3364*0Sstevel@tonic-gate 		 * need to call ndi_rele_devi when the reference is complete.
3365*0Sstevel@tonic-gate 		 */
3366*0Sstevel@tonic-gate 		(void) ddi_prop_update_int(DDI_DEV_T_NONE, parent,
3367*0Sstevel@tonic-gate 		    DDI_NO_AUTODETACH, 1);
3368*0Sstevel@tonic-gate 		ndi_rele_devi(parent);
3369*0Sstevel@tonic-gate 	}
3370*0Sstevel@tonic-gate 
3371*0Sstevel@tonic-gate 	return (0);
3372*0Sstevel@tonic-gate }
3373*0Sstevel@tonic-gate 
3374*0Sstevel@tonic-gate /*
3375*0Sstevel@tonic-gate  * Given the pathname of a device, return the dev_t of the corresponding
3376*0Sstevel@tonic-gate  * device.  Returns NODEV on failure.
3377*0Sstevel@tonic-gate  *
3378*0Sstevel@tonic-gate  * Note that this call sets the DDI_NO_AUTODETACH property on the devinfo node.
3379*0Sstevel@tonic-gate  */
3380*0Sstevel@tonic-gate dev_t
3381*0Sstevel@tonic-gate ddi_pathname_to_dev_t(char *pathname)
3382*0Sstevel@tonic-gate {
3383*0Sstevel@tonic-gate 	dev_t devt;
3384*0Sstevel@tonic-gate 	int error;
3385*0Sstevel@tonic-gate 
3386*0Sstevel@tonic-gate 	error = resolve_pathname(pathname, NULL, &devt, NULL);
3387*0Sstevel@tonic-gate 
3388*0Sstevel@tonic-gate 	return (error ? NODEV : devt);
3389*0Sstevel@tonic-gate }
3390*0Sstevel@tonic-gate 
3391*0Sstevel@tonic-gate /*
3392*0Sstevel@tonic-gate  * Translate a prom pathname to kernel devfs pathname.
3393*0Sstevel@tonic-gate  * Caller is assumed to allocate devfspath memory of
3394*0Sstevel@tonic-gate  * size at least MAXPATHLEN
3395*0Sstevel@tonic-gate  *
3396*0Sstevel@tonic-gate  * The prom pathname may not include minor name, but
3397*0Sstevel@tonic-gate  * devfs pathname has a minor name portion.
3398*0Sstevel@tonic-gate  */
3399*0Sstevel@tonic-gate int
3400*0Sstevel@tonic-gate i_ddi_prompath_to_devfspath(char *prompath, char *devfspath)
3401*0Sstevel@tonic-gate {
3402*0Sstevel@tonic-gate 	dev_t		devt = (dev_t)NODEV;
3403*0Sstevel@tonic-gate 	dev_info_t	*dip = NULL;
3404*0Sstevel@tonic-gate 	char		*minor_name = NULL;
3405*0Sstevel@tonic-gate 	int		spectype;
3406*0Sstevel@tonic-gate 	int		error;
3407*0Sstevel@tonic-gate 
3408*0Sstevel@tonic-gate 	error = resolve_pathname(prompath, &dip, &devt, &spectype);
3409*0Sstevel@tonic-gate 	if (error)
3410*0Sstevel@tonic-gate 		return (DDI_FAILURE);
3411*0Sstevel@tonic-gate 	ASSERT(dip && devt != NODEV);
3412*0Sstevel@tonic-gate 
3413*0Sstevel@tonic-gate 	/*
3414*0Sstevel@tonic-gate 	 * Get in-kernel devfs pathname
3415*0Sstevel@tonic-gate 	 */
3416*0Sstevel@tonic-gate 	(void) ddi_pathname(dip, devfspath);
3417*0Sstevel@tonic-gate 
3418*0Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
3419*0Sstevel@tonic-gate 	minor_name = i_ddi_devtspectype_to_minorname(dip, devt, spectype);
3420*0Sstevel@tonic-gate 	if (minor_name) {
3421*0Sstevel@tonic-gate 		(void) strcat(devfspath, ":");
3422*0Sstevel@tonic-gate 		(void) strcat(devfspath, minor_name);
3423*0Sstevel@tonic-gate 	} else {
3424*0Sstevel@tonic-gate 		/*
3425*0Sstevel@tonic-gate 		 * If minor_name is NULL, we have an alias minor node.
3426*0Sstevel@tonic-gate 		 * So manufacture a path to the corresponding clone minor.
3427*0Sstevel@tonic-gate 		 */
3428*0Sstevel@tonic-gate 		(void) snprintf(devfspath, MAXPATHLEN, "%s:%s",
3429*0Sstevel@tonic-gate 		    CLONE_PATH, ddi_driver_name(dip));
3430*0Sstevel@tonic-gate 	}
3431*0Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
3432*0Sstevel@tonic-gate 
3433*0Sstevel@tonic-gate 	/* release hold from resolve_pathname() */
3434*0Sstevel@tonic-gate 	ndi_rele_devi(dip);
3435*0Sstevel@tonic-gate 	return (0);
3436*0Sstevel@tonic-gate }
3437*0Sstevel@tonic-gate 
3438*0Sstevel@tonic-gate /*
3439*0Sstevel@tonic-gate  * Reset all the pure leaf drivers on the system at halt time
3440*0Sstevel@tonic-gate  */
3441*0Sstevel@tonic-gate static int
3442*0Sstevel@tonic-gate reset_leaf_device(dev_info_t *dip, void *arg)
3443*0Sstevel@tonic-gate {
3444*0Sstevel@tonic-gate 	_NOTE(ARGUNUSED(arg))
3445*0Sstevel@tonic-gate 	struct dev_ops *ops;
3446*0Sstevel@tonic-gate 
3447*0Sstevel@tonic-gate 	/* if the device doesn't need to be reset then there's nothing to do */
3448*0Sstevel@tonic-gate 	if (!DEVI_NEED_RESET(dip))
3449*0Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
3450*0Sstevel@tonic-gate 
3451*0Sstevel@tonic-gate 	/*
3452*0Sstevel@tonic-gate 	 * if the device isn't a char/block device or doesn't have a
3453*0Sstevel@tonic-gate 	 * reset entry point then there's nothing to do.
3454*0Sstevel@tonic-gate 	 */
3455*0Sstevel@tonic-gate 	ops = ddi_get_driver(dip);
3456*0Sstevel@tonic-gate 	if ((ops == NULL) || (ops->devo_cb_ops == NULL) ||
3457*0Sstevel@tonic-gate 	    (ops->devo_reset == nodev) || (ops->devo_reset == nulldev) ||
3458*0Sstevel@tonic-gate 	    (ops->devo_reset == NULL))
3459*0Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
3460*0Sstevel@tonic-gate 
3461*0Sstevel@tonic-gate 	if (DEVI_IS_ATTACHING(dip) || DEVI_IS_DETACHING(dip)) {
3462*0Sstevel@tonic-gate 		static char path[MAXPATHLEN];
3463*0Sstevel@tonic-gate 
3464*0Sstevel@tonic-gate 		/*
3465*0Sstevel@tonic-gate 		 * bad news, this device has blocked in it's attach or
3466*0Sstevel@tonic-gate 		 * detach routine, which means it not safe to call it's
3467*0Sstevel@tonic-gate 		 * devo_reset() entry point.
3468*0Sstevel@tonic-gate 		 */
3469*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "unable to reset device: %s",
3470*0Sstevel@tonic-gate 		    ddi_pathname(dip, path));
3471*0Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
3472*0Sstevel@tonic-gate 	}
3473*0Sstevel@tonic-gate 
3474*0Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_NOTE, "resetting %s%d\n",
3475*0Sstevel@tonic-gate 		ddi_driver_name(dip), ddi_get_instance(dip)));
3476*0Sstevel@tonic-gate 
3477*0Sstevel@tonic-gate 	(void) devi_reset(dip, DDI_RESET_FORCE);
3478*0Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
3479*0Sstevel@tonic-gate }
3480*0Sstevel@tonic-gate 
3481*0Sstevel@tonic-gate void
3482*0Sstevel@tonic-gate reset_leaves(void)
3483*0Sstevel@tonic-gate {
3484*0Sstevel@tonic-gate 	/*
3485*0Sstevel@tonic-gate 	 * if we're reached here, the device tree better not be changing.
3486*0Sstevel@tonic-gate 	 * so either devinfo_freeze better be set or we better be panicing.
3487*0Sstevel@tonic-gate 	 */
3488*0Sstevel@tonic-gate 	ASSERT(devinfo_freeze || panicstr);
3489*0Sstevel@tonic-gate 
3490*0Sstevel@tonic-gate 	(void) walk_devs(top_devinfo, reset_leaf_device, NULL, 0);
3491*0Sstevel@tonic-gate }
3492*0Sstevel@tonic-gate 
3493*0Sstevel@tonic-gate /*
3494*0Sstevel@tonic-gate  * devtree_freeze() must be called before reset_leaves() during a
3495*0Sstevel@tonic-gate  * normal system shutdown.  It attempts to ensure that there are no
3496*0Sstevel@tonic-gate  * outstanding attach or detach operations in progress when reset_leaves()
3497*0Sstevel@tonic-gate  * is invoked.  It must be called before the system becomes single-threaded
3498*0Sstevel@tonic-gate  * because device attach and detach are multi-threaded operations.  (note
3499*0Sstevel@tonic-gate  * that during system shutdown the system doesn't actually become
3500*0Sstevel@tonic-gate  * single-thread since other threads still exist, but the shutdown thread
3501*0Sstevel@tonic-gate  * will disable preemption for itself, raise it's pil, and stop all the
3502*0Sstevel@tonic-gate  * other cpus in the system there by effectively making the system
3503*0Sstevel@tonic-gate  * single-threaded.)
3504*0Sstevel@tonic-gate  */
3505*0Sstevel@tonic-gate void
3506*0Sstevel@tonic-gate devtree_freeze(void)
3507*0Sstevel@tonic-gate {
3508*0Sstevel@tonic-gate 	int delayed = 0;
3509*0Sstevel@tonic-gate 
3510*0Sstevel@tonic-gate 	/* if we're panicing then the device tree isn't going to be changing */
3511*0Sstevel@tonic-gate 	if (panicstr)
3512*0Sstevel@tonic-gate 		return;
3513*0Sstevel@tonic-gate 
3514*0Sstevel@tonic-gate 	/* stop all dev_info state changes in the device tree */
3515*0Sstevel@tonic-gate 	devinfo_freeze = gethrtime();
3516*0Sstevel@tonic-gate 
3517*0Sstevel@tonic-gate 	/*
3518*0Sstevel@tonic-gate 	 * if we're not panicing and there are on-going attach or detach
3519*0Sstevel@tonic-gate 	 * operations, wait for up to 3 seconds for them to finish.  This
3520*0Sstevel@tonic-gate 	 * is a randomly chosen interval but this should be ok because:
3521*0Sstevel@tonic-gate 	 * - 3 seconds is very small relative to the deadman timer.
3522*0Sstevel@tonic-gate 	 * - normal attach and detach operations should be very quick.
3523*0Sstevel@tonic-gate 	 * - attach and detach operations are fairly rare.
3524*0Sstevel@tonic-gate 	 */
3525*0Sstevel@tonic-gate 	while (!panicstr && atomic_add_long_nv(&devinfo_attach_detach, 0) &&
3526*0Sstevel@tonic-gate 	    (delayed < 3)) {
3527*0Sstevel@tonic-gate 		delayed += 1;
3528*0Sstevel@tonic-gate 
3529*0Sstevel@tonic-gate 		/* do a sleeping wait for one second */
3530*0Sstevel@tonic-gate 		ASSERT(!servicing_interrupt());
3531*0Sstevel@tonic-gate 		delay(drv_usectohz(MICROSEC));
3532*0Sstevel@tonic-gate 	}
3533*0Sstevel@tonic-gate }
3534*0Sstevel@tonic-gate 
3535*0Sstevel@tonic-gate static int
3536*0Sstevel@tonic-gate bind_dip(dev_info_t *dip, void *arg)
3537*0Sstevel@tonic-gate {
3538*0Sstevel@tonic-gate 	_NOTE(ARGUNUSED(arg))
3539*0Sstevel@tonic-gate 	if (i_ddi_node_state(dip) < DS_BOUND)
3540*0Sstevel@tonic-gate 		(void) ndi_devi_bind_driver(dip, 0);
3541*0Sstevel@tonic-gate 
3542*0Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
3543*0Sstevel@tonic-gate }
3544*0Sstevel@tonic-gate 
3545*0Sstevel@tonic-gate void
3546*0Sstevel@tonic-gate i_ddi_bind_devs(void)
3547*0Sstevel@tonic-gate {
3548*0Sstevel@tonic-gate 	ddi_walk_devs(top_devinfo, bind_dip, (void *)NULL);
3549*0Sstevel@tonic-gate }
3550*0Sstevel@tonic-gate 
3551*0Sstevel@tonic-gate static int
3552*0Sstevel@tonic-gate unbind_children(dev_info_t *dip, void *arg)
3553*0Sstevel@tonic-gate {
3554*0Sstevel@tonic-gate 	int circ;
3555*0Sstevel@tonic-gate 	dev_info_t *cdip;
3556*0Sstevel@tonic-gate 	major_t major = (major_t)(uintptr_t)arg;
3557*0Sstevel@tonic-gate 
3558*0Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
3559*0Sstevel@tonic-gate 	cdip = ddi_get_child(dip);
3560*0Sstevel@tonic-gate 	/*
3561*0Sstevel@tonic-gate 	 * We are called either from rem_drv or update_drv.
3562*0Sstevel@tonic-gate 	 * In both cases, we unbind persistent nodes and destroy
3563*0Sstevel@tonic-gate 	 * .conf nodes. In the case of rem_drv, this will be the
3564*0Sstevel@tonic-gate 	 * final state. In the case of update_drv, i_ddi_bind_devs()
3565*0Sstevel@tonic-gate 	 * will be invoked later to reenumerate (new) driver.conf
3566*0Sstevel@tonic-gate 	 * rebind persistent nodes.
3567*0Sstevel@tonic-gate 	 */
3568*0Sstevel@tonic-gate 	while (cdip) {
3569*0Sstevel@tonic-gate 		dev_info_t *next = ddi_get_next_sibling(cdip);
3570*0Sstevel@tonic-gate 		if ((i_ddi_node_state(cdip) > DS_INITIALIZED) ||
3571*0Sstevel@tonic-gate 		    (ddi_driver_major(cdip) != major)) {
3572*0Sstevel@tonic-gate 			cdip = next;
3573*0Sstevel@tonic-gate 			continue;
3574*0Sstevel@tonic-gate 		}
3575*0Sstevel@tonic-gate 		(void) ndi_devi_unbind_driver(cdip);
3576*0Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(cdip) == 0)
3577*0Sstevel@tonic-gate 			(void) ddi_remove_child(cdip, 0);
3578*0Sstevel@tonic-gate 		cdip = next;
3579*0Sstevel@tonic-gate 	}
3580*0Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
3581*0Sstevel@tonic-gate 
3582*0Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
3583*0Sstevel@tonic-gate }
3584*0Sstevel@tonic-gate 
3585*0Sstevel@tonic-gate void
3586*0Sstevel@tonic-gate i_ddi_unbind_devs(major_t major)
3587*0Sstevel@tonic-gate {
3588*0Sstevel@tonic-gate 	ddi_walk_devs(top_devinfo, unbind_children, (void *)(uintptr_t)major);
3589*0Sstevel@tonic-gate }
3590*0Sstevel@tonic-gate 
3591*0Sstevel@tonic-gate /*
3592*0Sstevel@tonic-gate  * I/O Hotplug control
3593*0Sstevel@tonic-gate  */
3594*0Sstevel@tonic-gate 
3595*0Sstevel@tonic-gate /*
3596*0Sstevel@tonic-gate  * create and attach a dev_info node from a .conf file spec
3597*0Sstevel@tonic-gate  */
3598*0Sstevel@tonic-gate static void
3599*0Sstevel@tonic-gate init_spec_child(dev_info_t *pdip, struct hwc_spec *specp, uint_t flags)
3600*0Sstevel@tonic-gate {
3601*0Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags))
3602*0Sstevel@tonic-gate 	dev_info_t *dip;
3603*0Sstevel@tonic-gate 	char *node_name;
3604*0Sstevel@tonic-gate 
3605*0Sstevel@tonic-gate 	if (((node_name = specp->hwc_devi_name) == NULL) ||
3606*0Sstevel@tonic-gate 	    (ddi_name_to_major(node_name) == (major_t)-1)) {
3607*0Sstevel@tonic-gate 		char *tmp = node_name;
3608*0Sstevel@tonic-gate 		if (tmp == NULL)
3609*0Sstevel@tonic-gate 			tmp = "<none>";
3610*0Sstevel@tonic-gate 		cmn_err(CE_CONT,
3611*0Sstevel@tonic-gate 		    "init_spec_child: parent=%s, bad spec (%s)\n",
3612*0Sstevel@tonic-gate 		    ddi_node_name(pdip), tmp);
3613*0Sstevel@tonic-gate 		return;
3614*0Sstevel@tonic-gate 	}
3615*0Sstevel@tonic-gate 
3616*0Sstevel@tonic-gate 	dip = i_ddi_alloc_node(pdip, node_name, (dnode_t)DEVI_PSEUDO_NODEID,
3617*0Sstevel@tonic-gate 	    -1, specp->hwc_devi_sys_prop_ptr, KM_SLEEP);
3618*0Sstevel@tonic-gate 
3619*0Sstevel@tonic-gate 	if (dip == NULL)
3620*0Sstevel@tonic-gate 		return;
3621*0Sstevel@tonic-gate 
3622*0Sstevel@tonic-gate 	if (ddi_initchild(pdip, dip) != DDI_SUCCESS)
3623*0Sstevel@tonic-gate 		(void) ddi_remove_child(dip, 0);
3624*0Sstevel@tonic-gate }
3625*0Sstevel@tonic-gate 
3626*0Sstevel@tonic-gate /*
3627*0Sstevel@tonic-gate  * Lookup hwc specs from hash tables and make children from the spec
3628*0Sstevel@tonic-gate  * Because some .conf children are "merge" nodes, we also initialize
3629*0Sstevel@tonic-gate  * .conf children to merge properties onto hardware nodes.
3630*0Sstevel@tonic-gate  *
3631*0Sstevel@tonic-gate  * The pdip must be held busy.
3632*0Sstevel@tonic-gate  */
3633*0Sstevel@tonic-gate int
3634*0Sstevel@tonic-gate i_ndi_make_spec_children(dev_info_t *pdip, uint_t flags)
3635*0Sstevel@tonic-gate {
3636*0Sstevel@tonic-gate 	extern struct hwc_spec *hwc_get_child_spec(dev_info_t *, major_t);
3637*0Sstevel@tonic-gate 
3638*0Sstevel@tonic-gate 	struct hwc_spec *list, *spec;
3639*0Sstevel@tonic-gate 
3640*0Sstevel@tonic-gate 	if (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN)
3641*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
3642*0Sstevel@tonic-gate 
3643*0Sstevel@tonic-gate 	list = hwc_get_child_spec(pdip, (major_t)-1);
3644*0Sstevel@tonic-gate 	for (spec = list; spec != NULL; spec = spec->hwc_next) {
3645*0Sstevel@tonic-gate 		init_spec_child(pdip, spec, flags);
3646*0Sstevel@tonic-gate 	}
3647*0Sstevel@tonic-gate 	hwc_free_spec_list(list);
3648*0Sstevel@tonic-gate 
3649*0Sstevel@tonic-gate 	mutex_enter(&DEVI(pdip)->devi_lock);
3650*0Sstevel@tonic-gate 	DEVI(pdip)->devi_flags |= DEVI_MADE_CHILDREN;
3651*0Sstevel@tonic-gate 	mutex_exit(&DEVI(pdip)->devi_lock);
3652*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
3653*0Sstevel@tonic-gate }
3654*0Sstevel@tonic-gate 
3655*0Sstevel@tonic-gate /*
3656*0Sstevel@tonic-gate  * Run initchild on all child nodes such that instance assignment
3657*0Sstevel@tonic-gate  * for multiport network cards are contiguous.
3658*0Sstevel@tonic-gate  *
3659*0Sstevel@tonic-gate  * The pdip must be held busy.
3660*0Sstevel@tonic-gate  */
3661*0Sstevel@tonic-gate static void
3662*0Sstevel@tonic-gate i_ndi_init_hw_children(dev_info_t *pdip, uint_t flags)
3663*0Sstevel@tonic-gate {
3664*0Sstevel@tonic-gate 	dev_info_t *dip;
3665*0Sstevel@tonic-gate 
3666*0Sstevel@tonic-gate 	ASSERT(DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
3667*0Sstevel@tonic-gate 
3668*0Sstevel@tonic-gate 	/* contiguous instance assignment */
3669*0Sstevel@tonic-gate 	e_ddi_enter_instance();
3670*0Sstevel@tonic-gate 	dip = ddi_get_child(pdip);
3671*0Sstevel@tonic-gate 	while (dip) {
3672*0Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(dip))
3673*0Sstevel@tonic-gate 			(void) i_ndi_config_node(dip, DS_INITIALIZED, flags);
3674*0Sstevel@tonic-gate 		dip = ddi_get_next_sibling(dip);
3675*0Sstevel@tonic-gate 	}
3676*0Sstevel@tonic-gate 	e_ddi_exit_instance();
3677*0Sstevel@tonic-gate }
3678*0Sstevel@tonic-gate 
3679*0Sstevel@tonic-gate /*
3680*0Sstevel@tonic-gate  * report device status
3681*0Sstevel@tonic-gate  */
3682*0Sstevel@tonic-gate static void
3683*0Sstevel@tonic-gate i_ndi_devi_report_status_change(dev_info_t *dip, char *path)
3684*0Sstevel@tonic-gate {
3685*0Sstevel@tonic-gate 	char *status;
3686*0Sstevel@tonic-gate 
3687*0Sstevel@tonic-gate 	if (!DEVI_NEED_REPORT(dip) ||
3688*0Sstevel@tonic-gate 	    (i_ddi_node_state(dip) < DS_INITIALIZED)) {
3689*0Sstevel@tonic-gate 		return;
3690*0Sstevel@tonic-gate 	}
3691*0Sstevel@tonic-gate 
3692*0Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
3693*0Sstevel@tonic-gate 		status = "offline";
3694*0Sstevel@tonic-gate 	} else if (DEVI_IS_DEVICE_DOWN(dip)) {
3695*0Sstevel@tonic-gate 		status = "down";
3696*0Sstevel@tonic-gate 	} else if (DEVI_IS_BUS_QUIESCED(dip)) {
3697*0Sstevel@tonic-gate 		status = "quiesced";
3698*0Sstevel@tonic-gate 	} else if (DEVI_IS_BUS_DOWN(dip)) {
3699*0Sstevel@tonic-gate 		status = "down";
3700*0Sstevel@tonic-gate 	} else if (i_ddi_node_state(dip) == DS_READY) {
3701*0Sstevel@tonic-gate 		status = "online";
3702*0Sstevel@tonic-gate 	} else {
3703*0Sstevel@tonic-gate 		status = "unknown";
3704*0Sstevel@tonic-gate 	}
3705*0Sstevel@tonic-gate 
3706*0Sstevel@tonic-gate 	if (path == NULL) {
3707*0Sstevel@tonic-gate 		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3708*0Sstevel@tonic-gate 		cmn_err(CE_CONT, "?%s (%s%d) %s\n",
3709*0Sstevel@tonic-gate 			ddi_pathname(dip, path), ddi_driver_name(dip),
3710*0Sstevel@tonic-gate 			ddi_get_instance(dip), status);
3711*0Sstevel@tonic-gate 		kmem_free(path, MAXPATHLEN);
3712*0Sstevel@tonic-gate 	} else {
3713*0Sstevel@tonic-gate 		cmn_err(CE_CONT, "?%s (%s%d) %s\n",
3714*0Sstevel@tonic-gate 			path, ddi_driver_name(dip),
3715*0Sstevel@tonic-gate 			ddi_get_instance(dip), status);
3716*0Sstevel@tonic-gate 	}
3717*0Sstevel@tonic-gate 
3718*0Sstevel@tonic-gate 	DEVI_REPORT_DONE(dip);
3719*0Sstevel@tonic-gate }
3720*0Sstevel@tonic-gate 
3721*0Sstevel@tonic-gate /*
3722*0Sstevel@tonic-gate  * log a notification that a dev_info node has been configured.
3723*0Sstevel@tonic-gate  */
3724*0Sstevel@tonic-gate static int
3725*0Sstevel@tonic-gate i_log_devfs_add_devinfo(dev_info_t *dip, uint_t flags)
3726*0Sstevel@tonic-gate {
3727*0Sstevel@tonic-gate 	int se_err;
3728*0Sstevel@tonic-gate 	char *pathname;
3729*0Sstevel@tonic-gate 	sysevent_t *ev;
3730*0Sstevel@tonic-gate 	sysevent_id_t eid;
3731*0Sstevel@tonic-gate 	sysevent_value_t se_val;
3732*0Sstevel@tonic-gate 	sysevent_attr_list_t *ev_attr_list = NULL;
3733*0Sstevel@tonic-gate 	char *class_name;
3734*0Sstevel@tonic-gate 	int no_transport = 0;
3735*0Sstevel@tonic-gate 
3736*0Sstevel@tonic-gate 	ASSERT(dip);
3737*0Sstevel@tonic-gate 
3738*0Sstevel@tonic-gate 	/*
3739*0Sstevel@tonic-gate 	 * Invalidate the devinfo snapshot cache
3740*0Sstevel@tonic-gate 	 */
3741*0Sstevel@tonic-gate 	i_ddi_di_cache_invalidate(KM_SLEEP);
3742*0Sstevel@tonic-gate 
3743*0Sstevel@tonic-gate 	/* do not generate ESC_DEVFS_DEVI_ADD event during boot */
3744*0Sstevel@tonic-gate 	if (!i_ddi_io_initialized())
3745*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
3746*0Sstevel@tonic-gate 
3747*0Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_ADD, EP_DDI, SE_SLEEP);
3748*0Sstevel@tonic-gate 
3749*0Sstevel@tonic-gate 	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3750*0Sstevel@tonic-gate 
3751*0Sstevel@tonic-gate 	(void) ddi_pathname(dip, pathname);
3752*0Sstevel@tonic-gate 	ASSERT(strlen(pathname));
3753*0Sstevel@tonic-gate 
3754*0Sstevel@tonic-gate 	se_val.value_type = SE_DATA_TYPE_STRING;
3755*0Sstevel@tonic-gate 	se_val.value.sv_string = pathname;
3756*0Sstevel@tonic-gate 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
3757*0Sstevel@tonic-gate 	    &se_val, SE_SLEEP) != 0) {
3758*0Sstevel@tonic-gate 		goto fail;
3759*0Sstevel@tonic-gate 	}
3760*0Sstevel@tonic-gate 
3761*0Sstevel@tonic-gate 	/* add the device class attribute */
3762*0Sstevel@tonic-gate 	if ((class_name = i_ddi_devi_class(dip)) != NULL) {
3763*0Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_STRING;
3764*0Sstevel@tonic-gate 		se_val.value.sv_string = class_name;
3765*0Sstevel@tonic-gate 
3766*0Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
3767*0Sstevel@tonic-gate 		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
3768*0Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
3769*0Sstevel@tonic-gate 			goto fail;
3770*0Sstevel@tonic-gate 		}
3771*0Sstevel@tonic-gate 	}
3772*0Sstevel@tonic-gate 
3773*0Sstevel@tonic-gate 	/*
3774*0Sstevel@tonic-gate 	 * must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
3775*0Sstevel@tonic-gate 	 * in which case the branch event will be logged by the caller
3776*0Sstevel@tonic-gate 	 * after the entire branch has been configured.
3777*0Sstevel@tonic-gate 	 */
3778*0Sstevel@tonic-gate 	if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
3779*0Sstevel@tonic-gate 		/*
3780*0Sstevel@tonic-gate 		 * Instead of logging a separate branch event just add
3781*0Sstevel@tonic-gate 		 * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
3782*0Sstevel@tonic-gate 		 * generate a EC_DEV_BRANCH event.
3783*0Sstevel@tonic-gate 		 */
3784*0Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_INT32;
3785*0Sstevel@tonic-gate 		se_val.value.sv_int32 = 1;
3786*0Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
3787*0Sstevel@tonic-gate 		    DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
3788*0Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
3789*0Sstevel@tonic-gate 			goto fail;
3790*0Sstevel@tonic-gate 		}
3791*0Sstevel@tonic-gate 	}
3792*0Sstevel@tonic-gate 
3793*0Sstevel@tonic-gate 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
3794*0Sstevel@tonic-gate 		sysevent_free_attr(ev_attr_list);
3795*0Sstevel@tonic-gate 		goto fail;
3796*0Sstevel@tonic-gate 	}
3797*0Sstevel@tonic-gate 
3798*0Sstevel@tonic-gate 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
3799*0Sstevel@tonic-gate 		if (se_err == SE_NO_TRANSPORT)
3800*0Sstevel@tonic-gate 			no_transport = 1;
3801*0Sstevel@tonic-gate 		goto fail;
3802*0Sstevel@tonic-gate 	}
3803*0Sstevel@tonic-gate 
3804*0Sstevel@tonic-gate 	sysevent_free(ev);
3805*0Sstevel@tonic-gate 	kmem_free(pathname, MAXPATHLEN);
3806*0Sstevel@tonic-gate 
3807*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
3808*0Sstevel@tonic-gate 
3809*0Sstevel@tonic-gate fail:
3810*0Sstevel@tonic-gate 	cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_ADD event for %s%s",
3811*0Sstevel@tonic-gate 	    pathname, (no_transport) ? " (syseventd not responding)" : "");
3812*0Sstevel@tonic-gate 
3813*0Sstevel@tonic-gate 	cmn_err(CE_WARN, "/dev may not be current for driver %s. "
3814*0Sstevel@tonic-gate 	    "Run devfsadm -i %s",
3815*0Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_driver_name(dip));
3816*0Sstevel@tonic-gate 
3817*0Sstevel@tonic-gate 	sysevent_free(ev);
3818*0Sstevel@tonic-gate 	kmem_free(pathname, MAXPATHLEN);
3819*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
3820*0Sstevel@tonic-gate }
3821*0Sstevel@tonic-gate 
3822*0Sstevel@tonic-gate /*
3823*0Sstevel@tonic-gate  * log a notification that a dev_info node has been unconfigured.
3824*0Sstevel@tonic-gate  */
3825*0Sstevel@tonic-gate static int
3826*0Sstevel@tonic-gate i_log_devfs_remove_devinfo(char *pathname, char *class_name, char *driver_name,
3827*0Sstevel@tonic-gate     int instance, uint_t flags)
3828*0Sstevel@tonic-gate {
3829*0Sstevel@tonic-gate 	sysevent_t *ev;
3830*0Sstevel@tonic-gate 	sysevent_id_t eid;
3831*0Sstevel@tonic-gate 	sysevent_value_t se_val;
3832*0Sstevel@tonic-gate 	sysevent_attr_list_t *ev_attr_list = NULL;
3833*0Sstevel@tonic-gate 	int se_err;
3834*0Sstevel@tonic-gate 	int no_transport = 0;
3835*0Sstevel@tonic-gate 
3836*0Sstevel@tonic-gate 	i_ddi_di_cache_invalidate(KM_SLEEP);
3837*0Sstevel@tonic-gate 
3838*0Sstevel@tonic-gate 	if (!i_ddi_io_initialized())
3839*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
3840*0Sstevel@tonic-gate 
3841*0Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_REMOVE, EP_DDI, SE_SLEEP);
3842*0Sstevel@tonic-gate 
3843*0Sstevel@tonic-gate 	se_val.value_type = SE_DATA_TYPE_STRING;
3844*0Sstevel@tonic-gate 	se_val.value.sv_string = pathname;
3845*0Sstevel@tonic-gate 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
3846*0Sstevel@tonic-gate 	    &se_val, SE_SLEEP) != 0) {
3847*0Sstevel@tonic-gate 		goto fail;
3848*0Sstevel@tonic-gate 	}
3849*0Sstevel@tonic-gate 
3850*0Sstevel@tonic-gate 	if (class_name) {
3851*0Sstevel@tonic-gate 		/* add the device class, driver name and instance attributes */
3852*0Sstevel@tonic-gate 
3853*0Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_STRING;
3854*0Sstevel@tonic-gate 		se_val.value.sv_string = class_name;
3855*0Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
3856*0Sstevel@tonic-gate 		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
3857*0Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
3858*0Sstevel@tonic-gate 			goto fail;
3859*0Sstevel@tonic-gate 		}
3860*0Sstevel@tonic-gate 
3861*0Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_STRING;
3862*0Sstevel@tonic-gate 		se_val.value.sv_string = driver_name;
3863*0Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
3864*0Sstevel@tonic-gate 		    DEVFS_DRIVER_NAME, &se_val, SE_SLEEP) != 0) {
3865*0Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
3866*0Sstevel@tonic-gate 			goto fail;
3867*0Sstevel@tonic-gate 		}
3868*0Sstevel@tonic-gate 
3869*0Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_INT32;
3870*0Sstevel@tonic-gate 		se_val.value.sv_int32 = instance;
3871*0Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
3872*0Sstevel@tonic-gate 		    DEVFS_INSTANCE, &se_val, SE_SLEEP) != 0) {
3873*0Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
3874*0Sstevel@tonic-gate 			goto fail;
3875*0Sstevel@tonic-gate 		}
3876*0Sstevel@tonic-gate 	}
3877*0Sstevel@tonic-gate 
3878*0Sstevel@tonic-gate 	/*
3879*0Sstevel@tonic-gate 	 * must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
3880*0Sstevel@tonic-gate 	 * in which case the branch event will be logged by the caller
3881*0Sstevel@tonic-gate 	 * after the entire branch has been unconfigured.
3882*0Sstevel@tonic-gate 	 */
3883*0Sstevel@tonic-gate 	if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
3884*0Sstevel@tonic-gate 		/*
3885*0Sstevel@tonic-gate 		 * Instead of logging a separate branch event just add
3886*0Sstevel@tonic-gate 		 * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
3887*0Sstevel@tonic-gate 		 * generate a EC_DEV_BRANCH event.
3888*0Sstevel@tonic-gate 		 */
3889*0Sstevel@tonic-gate 		se_val.value_type = SE_DATA_TYPE_INT32;
3890*0Sstevel@tonic-gate 		se_val.value.sv_int32 = 1;
3891*0Sstevel@tonic-gate 		if (sysevent_add_attr(&ev_attr_list,
3892*0Sstevel@tonic-gate 		    DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
3893*0Sstevel@tonic-gate 			sysevent_free_attr(ev_attr_list);
3894*0Sstevel@tonic-gate 			goto fail;
3895*0Sstevel@tonic-gate 		}
3896*0Sstevel@tonic-gate 	}
3897*0Sstevel@tonic-gate 
3898*0Sstevel@tonic-gate 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
3899*0Sstevel@tonic-gate 		sysevent_free_attr(ev_attr_list);
3900*0Sstevel@tonic-gate 		goto fail;
3901*0Sstevel@tonic-gate 	}
3902*0Sstevel@tonic-gate 
3903*0Sstevel@tonic-gate 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
3904*0Sstevel@tonic-gate 		if (se_err == SE_NO_TRANSPORT)
3905*0Sstevel@tonic-gate 			no_transport = 1;
3906*0Sstevel@tonic-gate 		goto fail;
3907*0Sstevel@tonic-gate 	}
3908*0Sstevel@tonic-gate 
3909*0Sstevel@tonic-gate 	sysevent_free(ev);
3910*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
3911*0Sstevel@tonic-gate 
3912*0Sstevel@tonic-gate fail:
3913*0Sstevel@tonic-gate 	sysevent_free(ev);
3914*0Sstevel@tonic-gate 	cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_REMOVE event for %s%s",
3915*0Sstevel@tonic-gate 	    pathname, (no_transport) ? " (syseventd not responding)" : "");
3916*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
3917*0Sstevel@tonic-gate }
3918*0Sstevel@tonic-gate 
3919*0Sstevel@tonic-gate /*
3920*0Sstevel@tonic-gate  * log an event that a dev_info branch has been configured or unconfigured.
3921*0Sstevel@tonic-gate  */
3922*0Sstevel@tonic-gate static int
3923*0Sstevel@tonic-gate i_log_devfs_branch(char *node_path, char *subclass)
3924*0Sstevel@tonic-gate {
3925*0Sstevel@tonic-gate 	int se_err;
3926*0Sstevel@tonic-gate 	sysevent_t *ev;
3927*0Sstevel@tonic-gate 	sysevent_id_t eid;
3928*0Sstevel@tonic-gate 	sysevent_value_t se_val;
3929*0Sstevel@tonic-gate 	sysevent_attr_list_t *ev_attr_list = NULL;
3930*0Sstevel@tonic-gate 	int no_transport = 0;
3931*0Sstevel@tonic-gate 
3932*0Sstevel@tonic-gate 	/* do not generate the event during boot */
3933*0Sstevel@tonic-gate 	if (!i_ddi_io_initialized())
3934*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
3935*0Sstevel@tonic-gate 
3936*0Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, subclass, EP_DDI, SE_SLEEP);
3937*0Sstevel@tonic-gate 
3938*0Sstevel@tonic-gate 	se_val.value_type = SE_DATA_TYPE_STRING;
3939*0Sstevel@tonic-gate 	se_val.value.sv_string = node_path;
3940*0Sstevel@tonic-gate 
3941*0Sstevel@tonic-gate 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
3942*0Sstevel@tonic-gate 	    &se_val, SE_SLEEP) != 0) {
3943*0Sstevel@tonic-gate 		goto fail;
3944*0Sstevel@tonic-gate 	}
3945*0Sstevel@tonic-gate 
3946*0Sstevel@tonic-gate 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
3947*0Sstevel@tonic-gate 		sysevent_free_attr(ev_attr_list);
3948*0Sstevel@tonic-gate 		goto fail;
3949*0Sstevel@tonic-gate 	}
3950*0Sstevel@tonic-gate 
3951*0Sstevel@tonic-gate 	if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
3952*0Sstevel@tonic-gate 		if (se_err == SE_NO_TRANSPORT)
3953*0Sstevel@tonic-gate 			no_transport = 1;
3954*0Sstevel@tonic-gate 		goto fail;
3955*0Sstevel@tonic-gate 	}
3956*0Sstevel@tonic-gate 
3957*0Sstevel@tonic-gate 	sysevent_free(ev);
3958*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
3959*0Sstevel@tonic-gate 
3960*0Sstevel@tonic-gate fail:
3961*0Sstevel@tonic-gate 	cmn_err(CE_WARN, "failed to log %s branch event for %s%s",
3962*0Sstevel@tonic-gate 	    subclass, node_path,
3963*0Sstevel@tonic-gate 	    (no_transport) ? " (syseventd not responding)" : "");
3964*0Sstevel@tonic-gate 
3965*0Sstevel@tonic-gate 	sysevent_free(ev);
3966*0Sstevel@tonic-gate 	return (DDI_FAILURE);
3967*0Sstevel@tonic-gate }
3968*0Sstevel@tonic-gate 
3969*0Sstevel@tonic-gate /*
3970*0Sstevel@tonic-gate  * log an event that a dev_info tree branch has been configured.
3971*0Sstevel@tonic-gate  */
3972*0Sstevel@tonic-gate static int
3973*0Sstevel@tonic-gate i_log_devfs_branch_add(dev_info_t *dip)
3974*0Sstevel@tonic-gate {
3975*0Sstevel@tonic-gate 	char *node_path;
3976*0Sstevel@tonic-gate 	int rv;
3977*0Sstevel@tonic-gate 
3978*0Sstevel@tonic-gate 	node_path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3979*0Sstevel@tonic-gate 	(void) ddi_pathname(dip, node_path);
3980*0Sstevel@tonic-gate 	rv = i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_ADD);
3981*0Sstevel@tonic-gate 	kmem_free(node_path, MAXPATHLEN);
3982*0Sstevel@tonic-gate 
3983*0Sstevel@tonic-gate 	return (rv);
3984*0Sstevel@tonic-gate }
3985*0Sstevel@tonic-gate 
3986*0Sstevel@tonic-gate /*
3987*0Sstevel@tonic-gate  * log an event that a dev_info tree branch has been unconfigured.
3988*0Sstevel@tonic-gate  */
3989*0Sstevel@tonic-gate static int
3990*0Sstevel@tonic-gate i_log_devfs_branch_remove(char *node_path)
3991*0Sstevel@tonic-gate {
3992*0Sstevel@tonic-gate 	return (i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_REMOVE));
3993*0Sstevel@tonic-gate }
3994*0Sstevel@tonic-gate 
3995*0Sstevel@tonic-gate /*
3996*0Sstevel@tonic-gate  * enqueue the dip's deviname on the branch event queue.
3997*0Sstevel@tonic-gate  */
3998*0Sstevel@tonic-gate static struct brevq_node *
3999*0Sstevel@tonic-gate brevq_enqueue(struct brevq_node **brevqp, dev_info_t *dip,
4000*0Sstevel@tonic-gate     struct brevq_node *child)
4001*0Sstevel@tonic-gate {
4002*0Sstevel@tonic-gate 	struct brevq_node *brn;
4003*0Sstevel@tonic-gate 	char *deviname;
4004*0Sstevel@tonic-gate 
4005*0Sstevel@tonic-gate 	deviname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
4006*0Sstevel@tonic-gate 	(void) ddi_deviname(dip, deviname);
4007*0Sstevel@tonic-gate 
4008*0Sstevel@tonic-gate 	brn = kmem_zalloc(sizeof (*brn), KM_SLEEP);
4009*0Sstevel@tonic-gate 	brn->deviname = i_ddi_strdup(deviname, KM_SLEEP);
4010*0Sstevel@tonic-gate 	kmem_free(deviname, MAXNAMELEN);
4011*0Sstevel@tonic-gate 	brn->child = child;
4012*0Sstevel@tonic-gate 	brn->sibling = *brevqp;
4013*0Sstevel@tonic-gate 	*brevqp = brn;
4014*0Sstevel@tonic-gate 
4015*0Sstevel@tonic-gate 	return (brn);
4016*0Sstevel@tonic-gate }
4017*0Sstevel@tonic-gate 
4018*0Sstevel@tonic-gate /*
4019*0Sstevel@tonic-gate  * free the memory allocated for the elements on the branch event queue.
4020*0Sstevel@tonic-gate  */
4021*0Sstevel@tonic-gate static void
4022*0Sstevel@tonic-gate free_brevq(struct brevq_node *brevq)
4023*0Sstevel@tonic-gate {
4024*0Sstevel@tonic-gate 	struct brevq_node *brn, *next_brn;
4025*0Sstevel@tonic-gate 
4026*0Sstevel@tonic-gate 	for (brn = brevq; brn != NULL; brn = next_brn) {
4027*0Sstevel@tonic-gate 		next_brn = brn->sibling;
4028*0Sstevel@tonic-gate 		ASSERT(brn->child == NULL);
4029*0Sstevel@tonic-gate 		kmem_free(brn->deviname, strlen(brn->deviname) + 1);
4030*0Sstevel@tonic-gate 		kmem_free(brn, sizeof (*brn));
4031*0Sstevel@tonic-gate 	}
4032*0Sstevel@tonic-gate }
4033*0Sstevel@tonic-gate 
4034*0Sstevel@tonic-gate /*
4035*0Sstevel@tonic-gate  * log the events queued up on the branch event queue and free the
4036*0Sstevel@tonic-gate  * associated memory.
4037*0Sstevel@tonic-gate  *
4038*0Sstevel@tonic-gate  * node_path must have been allocated with at least MAXPATHLEN bytes.
4039*0Sstevel@tonic-gate  */
4040*0Sstevel@tonic-gate static void
4041*0Sstevel@tonic-gate log_and_free_brevq(char *node_path, struct brevq_node *brevq)
4042*0Sstevel@tonic-gate {
4043*0Sstevel@tonic-gate 	struct brevq_node *brn;
4044*0Sstevel@tonic-gate 	char *p;
4045*0Sstevel@tonic-gate 
4046*0Sstevel@tonic-gate 	p = node_path + strlen(node_path);
4047*0Sstevel@tonic-gate 	for (brn = brevq; brn != NULL; brn = brn->sibling) {
4048*0Sstevel@tonic-gate 		(void) strcpy(p, brn->deviname);
4049*0Sstevel@tonic-gate 		(void) i_log_devfs_branch_remove(node_path);
4050*0Sstevel@tonic-gate 	}
4051*0Sstevel@tonic-gate 	*p = '\0';
4052*0Sstevel@tonic-gate 
4053*0Sstevel@tonic-gate 	free_brevq(brevq);
4054*0Sstevel@tonic-gate }
4055*0Sstevel@tonic-gate 
4056*0Sstevel@tonic-gate /*
4057*0Sstevel@tonic-gate  * log the events queued up on the branch event queue and free the
4058*0Sstevel@tonic-gate  * associated memory. Same as the previous function but operates on dip.
4059*0Sstevel@tonic-gate  */
4060*0Sstevel@tonic-gate static void
4061*0Sstevel@tonic-gate log_and_free_brevq_dip(dev_info_t *dip, struct brevq_node *brevq)
4062*0Sstevel@tonic-gate {
4063*0Sstevel@tonic-gate 	char *path;
4064*0Sstevel@tonic-gate 
4065*0Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4066*0Sstevel@tonic-gate 	(void) ddi_pathname(dip, path);
4067*0Sstevel@tonic-gate 	log_and_free_brevq(path, brevq);
4068*0Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
4069*0Sstevel@tonic-gate }
4070*0Sstevel@tonic-gate 
4071*0Sstevel@tonic-gate /*
4072*0Sstevel@tonic-gate  * log the outstanding branch remove events for the grand children of the dip
4073*0Sstevel@tonic-gate  * and free the associated memory.
4074*0Sstevel@tonic-gate  */
4075*0Sstevel@tonic-gate static void
4076*0Sstevel@tonic-gate log_and_free_br_events_on_grand_children(dev_info_t *dip,
4077*0Sstevel@tonic-gate     struct brevq_node *brevq)
4078*0Sstevel@tonic-gate {
4079*0Sstevel@tonic-gate 	struct brevq_node *brn;
4080*0Sstevel@tonic-gate 	char *path;
4081*0Sstevel@tonic-gate 	char *p;
4082*0Sstevel@tonic-gate 
4083*0Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4084*0Sstevel@tonic-gate 	(void) ddi_pathname(dip, path);
4085*0Sstevel@tonic-gate 	p = path + strlen(path);
4086*0Sstevel@tonic-gate 	for (brn = brevq; brn != NULL; brn = brn->sibling) {
4087*0Sstevel@tonic-gate 		if (brn->child) {
4088*0Sstevel@tonic-gate 			(void) strcpy(p, brn->deviname);
4089*0Sstevel@tonic-gate 			/* now path contains the node path to the dip's child */
4090*0Sstevel@tonic-gate 			log_and_free_brevq(path, brn->child);
4091*0Sstevel@tonic-gate 			brn->child = NULL;
4092*0Sstevel@tonic-gate 		}
4093*0Sstevel@tonic-gate 	}
4094*0Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
4095*0Sstevel@tonic-gate }
4096*0Sstevel@tonic-gate 
4097*0Sstevel@tonic-gate /*
4098*0Sstevel@tonic-gate  * log and cleanup branch remove events for the grand children of the dip.
4099*0Sstevel@tonic-gate  */
4100*0Sstevel@tonic-gate static void
4101*0Sstevel@tonic-gate cleanup_br_events_on_grand_children(dev_info_t *dip, struct brevq_node **brevqp)
4102*0Sstevel@tonic-gate {
4103*0Sstevel@tonic-gate 	dev_info_t *child;
4104*0Sstevel@tonic-gate 	struct brevq_node *brevq, *brn, *prev_brn, *next_brn;
4105*0Sstevel@tonic-gate 	char *path;
4106*0Sstevel@tonic-gate 	int circ;
4107*0Sstevel@tonic-gate 
4108*0Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4109*0Sstevel@tonic-gate 	prev_brn = NULL;
4110*0Sstevel@tonic-gate 	brevq = *brevqp;
4111*0Sstevel@tonic-gate 
4112*0Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
4113*0Sstevel@tonic-gate 	for (brn = brevq; brn != NULL; brn = next_brn) {
4114*0Sstevel@tonic-gate 		next_brn = brn->sibling;
4115*0Sstevel@tonic-gate 		for (child = ddi_get_child(dip); child != NULL;
4116*0Sstevel@tonic-gate 		    child = ddi_get_next_sibling(child)) {
4117*0Sstevel@tonic-gate 			if (i_ddi_node_state(child) >= DS_INITIALIZED) {
4118*0Sstevel@tonic-gate 				(void) ddi_deviname(child, path);
4119*0Sstevel@tonic-gate 				if (strcmp(path, brn->deviname) == 0)
4120*0Sstevel@tonic-gate 					break;
4121*0Sstevel@tonic-gate 			}
4122*0Sstevel@tonic-gate 		}
4123*0Sstevel@tonic-gate 
4124*0Sstevel@tonic-gate 		if (child != NULL && !(DEVI_EVREMOVE(child))) {
4125*0Sstevel@tonic-gate 			/*
4126*0Sstevel@tonic-gate 			 * Event state is not REMOVE. So branch remove event
4127*0Sstevel@tonic-gate 			 * is not going be generated on brn->child.
4128*0Sstevel@tonic-gate 			 * If any branch remove events were queued up on
4129*0Sstevel@tonic-gate 			 * brn->child log them and remove the brn
4130*0Sstevel@tonic-gate 			 * from the queue.
4131*0Sstevel@tonic-gate 			 */
4132*0Sstevel@tonic-gate 			if (brn->child) {
4133*0Sstevel@tonic-gate 				(void) ddi_pathname(dip, path);
4134*0Sstevel@tonic-gate 				(void) strcat(path, brn->deviname);
4135*0Sstevel@tonic-gate 				log_and_free_brevq(path, brn->child);
4136*0Sstevel@tonic-gate 			}
4137*0Sstevel@tonic-gate 
4138*0Sstevel@tonic-gate 			if (prev_brn)
4139*0Sstevel@tonic-gate 				prev_brn->sibling = next_brn;
4140*0Sstevel@tonic-gate 			else
4141*0Sstevel@tonic-gate 				*brevqp = next_brn;
4142*0Sstevel@tonic-gate 
4143*0Sstevel@tonic-gate 			kmem_free(brn->deviname, strlen(brn->deviname) + 1);
4144*0Sstevel@tonic-gate 			kmem_free(brn, sizeof (*brn));
4145*0Sstevel@tonic-gate 		} else {
4146*0Sstevel@tonic-gate 			/*
4147*0Sstevel@tonic-gate 			 * Free up the outstanding branch remove events
4148*0Sstevel@tonic-gate 			 * queued on brn->child since brn->child
4149*0Sstevel@tonic-gate 			 * itself is eligible for branch remove event.
4150*0Sstevel@tonic-gate 			 */
4151*0Sstevel@tonic-gate 			if (brn->child) {
4152*0Sstevel@tonic-gate 				free_brevq(brn->child);
4153*0Sstevel@tonic-gate 				brn->child = NULL;
4154*0Sstevel@tonic-gate 			}
4155*0Sstevel@tonic-gate 			prev_brn = brn;
4156*0Sstevel@tonic-gate 		}
4157*0Sstevel@tonic-gate 	}
4158*0Sstevel@tonic-gate 
4159*0Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
4160*0Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
4161*0Sstevel@tonic-gate }
4162*0Sstevel@tonic-gate 
4163*0Sstevel@tonic-gate static int
4164*0Sstevel@tonic-gate need_remove_event(dev_info_t *dip, int flags)
4165*0Sstevel@tonic-gate {
4166*0Sstevel@tonic-gate 	if ((flags & (NDI_NO_EVENT | NDI_AUTODETACH)) == 0 &&
4167*0Sstevel@tonic-gate 	    (flags & (NDI_DEVI_OFFLINE | NDI_UNCONFIG | NDI_DEVI_REMOVE)) &&
4168*0Sstevel@tonic-gate 	    !(DEVI_EVREMOVE(dip)))
4169*0Sstevel@tonic-gate 		return (1);
4170*0Sstevel@tonic-gate 	else
4171*0Sstevel@tonic-gate 		return (0);
4172*0Sstevel@tonic-gate }
4173*0Sstevel@tonic-gate 
4174*0Sstevel@tonic-gate /*
4175*0Sstevel@tonic-gate  * Unconfigure children/descendants of the dip.
4176*0Sstevel@tonic-gate  *
4177*0Sstevel@tonic-gate  * If the operation involves a branch event NDI_BRANCH_EVENT_OP is set
4178*0Sstevel@tonic-gate  * through out the unconfiguration. On successful return *brevqp is set to
4179*0Sstevel@tonic-gate  * a queue of dip's child devinames for which branch remove events need
4180*0Sstevel@tonic-gate  * to be generated.
4181*0Sstevel@tonic-gate  */
4182*0Sstevel@tonic-gate static int
4183*0Sstevel@tonic-gate devi_unconfig_branch(dev_info_t *dip, dev_info_t **dipp, int flags,
4184*0Sstevel@tonic-gate     struct brevq_node **brevqp)
4185*0Sstevel@tonic-gate {
4186*0Sstevel@tonic-gate 	int rval;
4187*0Sstevel@tonic-gate 
4188*0Sstevel@tonic-gate 	*brevqp = NULL;
4189*0Sstevel@tonic-gate 
4190*0Sstevel@tonic-gate 	if ((!(flags & NDI_BRANCH_EVENT_OP)) && need_remove_event(dip, flags))
4191*0Sstevel@tonic-gate 		flags |= NDI_BRANCH_EVENT_OP;
4192*0Sstevel@tonic-gate 
4193*0Sstevel@tonic-gate 	if (flags & NDI_BRANCH_EVENT_OP) {
4194*0Sstevel@tonic-gate 		rval = devi_unconfig_common(dip, dipp, flags, (major_t)-1,
4195*0Sstevel@tonic-gate 		    brevqp);
4196*0Sstevel@tonic-gate 
4197*0Sstevel@tonic-gate 		if (rval != NDI_SUCCESS && (*brevqp)) {
4198*0Sstevel@tonic-gate 			log_and_free_brevq_dip(dip, *brevqp);
4199*0Sstevel@tonic-gate 			*brevqp = NULL;
4200*0Sstevel@tonic-gate 		}
4201*0Sstevel@tonic-gate 	} else
4202*0Sstevel@tonic-gate 		rval = devi_unconfig_common(dip, dipp, flags, (major_t)-1,
4203*0Sstevel@tonic-gate 		    NULL);
4204*0Sstevel@tonic-gate 
4205*0Sstevel@tonic-gate 	return (rval);
4206*0Sstevel@tonic-gate }
4207*0Sstevel@tonic-gate 
4208*0Sstevel@tonic-gate /*
4209*0Sstevel@tonic-gate  * If the dip is already bound to a driver transition to DS_INITIALIZED
4210*0Sstevel@tonic-gate  * in order to generate an event in the case where the node was left in
4211*0Sstevel@tonic-gate  * DS_BOUND state since boot (never got attached) and the node is now
4212*0Sstevel@tonic-gate  * being offlined.
4213*0Sstevel@tonic-gate  */
4214*0Sstevel@tonic-gate static void
4215*0Sstevel@tonic-gate init_bound_node_ev(dev_info_t *pdip, dev_info_t *dip, int flags)
4216*0Sstevel@tonic-gate {
4217*0Sstevel@tonic-gate 	if (need_remove_event(dip, flags) &&
4218*0Sstevel@tonic-gate 	    i_ddi_node_state(dip) == DS_BOUND &&
4219*0Sstevel@tonic-gate 	    i_ddi_node_state(pdip) >= DS_ATTACHED &&
4220*0Sstevel@tonic-gate 	    !(DEVI_IS_DEVICE_OFFLINE(dip)))
4221*0Sstevel@tonic-gate 		(void) ddi_initchild(pdip, dip);
4222*0Sstevel@tonic-gate }
4223*0Sstevel@tonic-gate 
4224*0Sstevel@tonic-gate /*
4225*0Sstevel@tonic-gate  * attach a node/branch with parent already held busy
4226*0Sstevel@tonic-gate  */
4227*0Sstevel@tonic-gate static int
4228*0Sstevel@tonic-gate devi_attach_node(dev_info_t *dip, uint_t flags)
4229*0Sstevel@tonic-gate {
4230*0Sstevel@tonic-gate 	if (flags & NDI_DEVI_ONLINE) {
4231*0Sstevel@tonic-gate 		DEVI_SET_DEVICE_ONLINE(dip);
4232*0Sstevel@tonic-gate 	}
4233*0Sstevel@tonic-gate 
4234*0Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
4235*0Sstevel@tonic-gate 		return (NDI_FAILURE);
4236*0Sstevel@tonic-gate 	}
4237*0Sstevel@tonic-gate 
4238*0Sstevel@tonic-gate 	if (i_ddi_attachchild(dip) != DDI_SUCCESS) {
4239*0Sstevel@tonic-gate 		DEVI_SET_EVUNINIT(dip);
4240*0Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node(dip))
4241*0Sstevel@tonic-gate 			(void) ddi_uninitchild(dip);
4242*0Sstevel@tonic-gate 		else {
4243*0Sstevel@tonic-gate 			/*
4244*0Sstevel@tonic-gate 			 * Delete .conf nodes and nodes that are not
4245*0Sstevel@tonic-gate 			 * well formed.
4246*0Sstevel@tonic-gate 			 */
4247*0Sstevel@tonic-gate 			(void) ddi_remove_child(dip, 0);
4248*0Sstevel@tonic-gate 		}
4249*0Sstevel@tonic-gate 		return (NDI_FAILURE);
4250*0Sstevel@tonic-gate 	}
4251*0Sstevel@tonic-gate 
4252*0Sstevel@tonic-gate 	i_ndi_devi_report_status_change(dip, NULL);
4253*0Sstevel@tonic-gate 
4254*0Sstevel@tonic-gate 	/*
4255*0Sstevel@tonic-gate 	 * log an event, but not during devfs lookups in which case
4256*0Sstevel@tonic-gate 	 * NDI_NO_EVENT is set.
4257*0Sstevel@tonic-gate 	 */
4258*0Sstevel@tonic-gate 	if ((flags & NDI_NO_EVENT) == 0 && !(DEVI_EVADD(dip))) {
4259*0Sstevel@tonic-gate 		(void) i_log_devfs_add_devinfo(dip, flags);
4260*0Sstevel@tonic-gate 		DEVI_SET_EVADD(dip);
4261*0Sstevel@tonic-gate 	} else if (!(flags & NDI_NO_EVENT_STATE_CHNG))
4262*0Sstevel@tonic-gate 		DEVI_SET_EVADD(dip);
4263*0Sstevel@tonic-gate 
4264*0Sstevel@tonic-gate 	return (NDI_SUCCESS);
4265*0Sstevel@tonic-gate }
4266*0Sstevel@tonic-gate 
4267*0Sstevel@tonic-gate /*
4268*0Sstevel@tonic-gate  * Configure all children of a nexus, assuming all spec children have
4269*0Sstevel@tonic-gate  * been made.
4270*0Sstevel@tonic-gate  */
4271*0Sstevel@tonic-gate static int
4272*0Sstevel@tonic-gate devi_attach_children(dev_info_t *pdip, uint_t flags, major_t major)
4273*0Sstevel@tonic-gate {
4274*0Sstevel@tonic-gate 	dev_info_t *dip;
4275*0Sstevel@tonic-gate 
4276*0Sstevel@tonic-gate 	ASSERT(DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
4277*0Sstevel@tonic-gate 
4278*0Sstevel@tonic-gate 	dip = ddi_get_child(pdip);
4279*0Sstevel@tonic-gate 	while (dip) {
4280*0Sstevel@tonic-gate 		/*
4281*0Sstevel@tonic-gate 		 * NOTE: devi_attach_node() may remove the dip
4282*0Sstevel@tonic-gate 		 */
4283*0Sstevel@tonic-gate 		dev_info_t *next = ddi_get_next_sibling(dip);
4284*0Sstevel@tonic-gate 
4285*0Sstevel@tonic-gate 		/*
4286*0Sstevel@tonic-gate 		 * Configure all nexus nodes or leaf nodes with
4287*0Sstevel@tonic-gate 		 * matching driver major
4288*0Sstevel@tonic-gate 		 */
4289*0Sstevel@tonic-gate 		if ((major == (major_t)-1) ||
4290*0Sstevel@tonic-gate 		    (major == ddi_driver_major(dip)) ||
4291*0Sstevel@tonic-gate 		    ((flags & NDI_CONFIG) && (is_leaf_node(dip) == 0)))
4292*0Sstevel@tonic-gate 			(void) devi_attach_node(dip, flags);
4293*0Sstevel@tonic-gate 		dip = next;
4294*0Sstevel@tonic-gate 	}
4295*0Sstevel@tonic-gate 
4296*0Sstevel@tonic-gate 	return (NDI_SUCCESS);
4297*0Sstevel@tonic-gate }
4298*0Sstevel@tonic-gate 
4299*0Sstevel@tonic-gate /* internal function to config immediate children */
4300*0Sstevel@tonic-gate static int
4301*0Sstevel@tonic-gate config_immediate_children(dev_info_t *pdip, uint_t flags, major_t major)
4302*0Sstevel@tonic-gate {
4303*0Sstevel@tonic-gate 	int circ;
4304*0Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(pdip) >= DS_ATTACHED);
4305*0Sstevel@tonic-gate 
4306*0Sstevel@tonic-gate 	if (!NEXUS_DRV(ddi_get_driver(pdip)))
4307*0Sstevel@tonic-gate 		return (NDI_SUCCESS);
4308*0Sstevel@tonic-gate 
4309*0Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
4310*0Sstevel@tonic-gate 	    "config_immediate_children: %s%d (%p), flags=%x\n",
4311*0Sstevel@tonic-gate 	    ddi_driver_name(pdip), ddi_get_instance(pdip),
4312*0Sstevel@tonic-gate 	    (void *)pdip, flags));
4313*0Sstevel@tonic-gate 
4314*0Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
4315*0Sstevel@tonic-gate 
4316*0Sstevel@tonic-gate 	if (flags & NDI_CONFIG_REPROBE) {
4317*0Sstevel@tonic-gate 		mutex_enter(&DEVI(pdip)->devi_lock);
4318*0Sstevel@tonic-gate 		DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
4319*0Sstevel@tonic-gate 		mutex_exit(&DEVI(pdip)->devi_lock);
4320*0Sstevel@tonic-gate 	}
4321*0Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(pdip, flags);
4322*0Sstevel@tonic-gate 	i_ndi_init_hw_children(pdip, flags);
4323*0Sstevel@tonic-gate 	(void) devi_attach_children(pdip, flags, major);
4324*0Sstevel@tonic-gate 
4325*0Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
4326*0Sstevel@tonic-gate 
4327*0Sstevel@tonic-gate 	return (NDI_SUCCESS);
4328*0Sstevel@tonic-gate }
4329*0Sstevel@tonic-gate 
4330*0Sstevel@tonic-gate /* internal function to config grand children */
4331*0Sstevel@tonic-gate static int
4332*0Sstevel@tonic-gate config_grand_children(dev_info_t *pdip, uint_t flags, major_t major)
4333*0Sstevel@tonic-gate {
4334*0Sstevel@tonic-gate 	struct mt_config_handle *hdl;
4335*0Sstevel@tonic-gate 
4336*0Sstevel@tonic-gate 	/* multi-threaded configuration of child nexus */
4337*0Sstevel@tonic-gate 	hdl = mt_config_init(pdip, NULL, flags, major, MT_CONFIG_OP, NULL);
4338*0Sstevel@tonic-gate 	mt_config_children(hdl);
4339*0Sstevel@tonic-gate 
4340*0Sstevel@tonic-gate 	return (mt_config_fini(hdl));	/* wait for threads to exit */
4341*0Sstevel@tonic-gate }
4342*0Sstevel@tonic-gate 
4343*0Sstevel@tonic-gate /*
4344*0Sstevel@tonic-gate  * Common function for device tree configuration,
4345*0Sstevel@tonic-gate  * either BUS_CONFIG_ALL or BUS_CONFIG_DRIVER.
4346*0Sstevel@tonic-gate  * The NDI_CONFIG flag causes recursive configuration of
4347*0Sstevel@tonic-gate  * grandchildren, devfs usage should not recurse.
4348*0Sstevel@tonic-gate  */
4349*0Sstevel@tonic-gate static int
4350*0Sstevel@tonic-gate devi_config_common(dev_info_t *dip, int flags, major_t major)
4351*0Sstevel@tonic-gate {
4352*0Sstevel@tonic-gate 	int error;
4353*0Sstevel@tonic-gate 	int (*f)();
4354*0Sstevel@tonic-gate 
4355*0Sstevel@tonic-gate 	if (i_ddi_node_state(dip) <  DS_READY)
4356*0Sstevel@tonic-gate 		return (NDI_FAILURE);
4357*0Sstevel@tonic-gate 
4358*0Sstevel@tonic-gate 	if (pm_pre_config(dip, NULL) != DDI_SUCCESS)
4359*0Sstevel@tonic-gate 		return (NDI_FAILURE);
4360*0Sstevel@tonic-gate 
4361*0Sstevel@tonic-gate 	if ((DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
4362*0Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
4363*0Sstevel@tonic-gate 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
4364*0Sstevel@tonic-gate 		error = config_immediate_children(dip, flags, major);
4365*0Sstevel@tonic-gate 	} else {
4366*0Sstevel@tonic-gate 		/* call bus_config entry point */
4367*0Sstevel@tonic-gate 		ddi_bus_config_op_t bus_op = (major == (major_t)-1) ?
4368*0Sstevel@tonic-gate 		    BUS_CONFIG_ALL : BUS_CONFIG_DRIVER;
4369*0Sstevel@tonic-gate 		error = (*f)(dip,
4370*0Sstevel@tonic-gate 		    flags, bus_op, (void *)(uintptr_t)major, NULL, 0);
4371*0Sstevel@tonic-gate 	}
4372*0Sstevel@tonic-gate 
4373*0Sstevel@tonic-gate 	if (error) {
4374*0Sstevel@tonic-gate 		pm_post_config(dip, NULL);
4375*0Sstevel@tonic-gate 		return (error);
4376*0Sstevel@tonic-gate 	}
4377*0Sstevel@tonic-gate 
4378*0Sstevel@tonic-gate 	/*
4379*0Sstevel@tonic-gate 	 * Some callers, notably SCSI, need to mark the devfs cache
4380*0Sstevel@tonic-gate 	 * to be rebuilt together with the config operation.
4381*0Sstevel@tonic-gate 	 */
4382*0Sstevel@tonic-gate 	if (flags & NDI_DEVFS_CLEAN)
4383*0Sstevel@tonic-gate 		(void) devfs_clean(dip, NULL, 0);
4384*0Sstevel@tonic-gate 
4385*0Sstevel@tonic-gate 	if (flags & NDI_CONFIG)
4386*0Sstevel@tonic-gate 		(void) config_grand_children(dip, flags, major);
4387*0Sstevel@tonic-gate 
4388*0Sstevel@tonic-gate 	pm_post_config(dip, NULL);
4389*0Sstevel@tonic-gate 
4390*0Sstevel@tonic-gate 	return (NDI_SUCCESS);
4391*0Sstevel@tonic-gate }
4392*0Sstevel@tonic-gate 
4393*0Sstevel@tonic-gate /*
4394*0Sstevel@tonic-gate  * Framework entry point for BUS_CONFIG_ALL
4395*0Sstevel@tonic-gate  */
4396*0Sstevel@tonic-gate int
4397*0Sstevel@tonic-gate ndi_devi_config(dev_info_t *dip, int flags)
4398*0Sstevel@tonic-gate {
4399*0Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
4400*0Sstevel@tonic-gate 	    "ndi_devi_config: par = %s%d (%p), flags = 0x%x\n",
4401*0Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
4402*0Sstevel@tonic-gate 
4403*0Sstevel@tonic-gate 	return (devi_config_common(dip, flags, (major_t)-1));
4404*0Sstevel@tonic-gate }
4405*0Sstevel@tonic-gate 
4406*0Sstevel@tonic-gate /*
4407*0Sstevel@tonic-gate  * Framework entry point for BUS_CONFIG_DRIVER, bound to major
4408*0Sstevel@tonic-gate  */
4409*0Sstevel@tonic-gate int
4410*0Sstevel@tonic-gate ndi_devi_config_driver(dev_info_t *dip, int flags, major_t major)
4411*0Sstevel@tonic-gate {
4412*0Sstevel@tonic-gate 	/* don't abuse this function */
4413*0Sstevel@tonic-gate 	ASSERT(major != (major_t)-1);
4414*0Sstevel@tonic-gate 
4415*0Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
4416*0Sstevel@tonic-gate 	    "ndi_devi_config_driver: par = %s%d (%p), flags = 0x%x\n",
4417*0Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
4418*0Sstevel@tonic-gate 
4419*0Sstevel@tonic-gate 	return (devi_config_common(dip, flags, major));
4420*0Sstevel@tonic-gate }
4421*0Sstevel@tonic-gate 
4422*0Sstevel@tonic-gate /*
4423*0Sstevel@tonic-gate  * called by nexus drivers to configure/unconfigure its children
4424*0Sstevel@tonic-gate  */
4425*0Sstevel@tonic-gate static int
4426*0Sstevel@tonic-gate devi_config_one(dev_info_t *pdip, char *devnm, dev_info_t **dipp,
4427*0Sstevel@tonic-gate     uint_t flags, clock_t timeout)
4428*0Sstevel@tonic-gate {
4429*0Sstevel@tonic-gate 	int circ, probed, rv;
4430*0Sstevel@tonic-gate 	dev_info_t *dip = NULL;
4431*0Sstevel@tonic-gate 	char *name, *addr, *drivername = NULL;
4432*0Sstevel@tonic-gate 	clock_t end_time;	/* 60 sec */
4433*0Sstevel@tonic-gate 
4434*0Sstevel@tonic-gate 	if (!NEXUS_DRV(ddi_get_driver(pdip)))
4435*0Sstevel@tonic-gate 		return (NDI_FAILURE);
4436*0Sstevel@tonic-gate 
4437*0Sstevel@tonic-gate 	if (MDI_PHCI(pdip)) {
4438*0Sstevel@tonic-gate 		/* Call mdi_ to configure the child */
4439*0Sstevel@tonic-gate 		rv = mdi_devi_config_one(pdip, devnm, dipp, flags, timeout);
4440*0Sstevel@tonic-gate 		if (rv == MDI_SUCCESS)
4441*0Sstevel@tonic-gate 			return (NDI_SUCCESS);
4442*0Sstevel@tonic-gate 
4443*0Sstevel@tonic-gate 		/*
4444*0Sstevel@tonic-gate 		 * Normally, we should return failure here.
4445*0Sstevel@tonic-gate 		 *
4446*0Sstevel@tonic-gate 		 * Leadville implemented an unfortunate fallback mechanism.
4447*0Sstevel@tonic-gate 		 * If a target is non-standard and scsi_vhci doesn't know
4448*0Sstevel@tonic-gate 		 * how to do failover, then the node is enumerated under
4449*0Sstevel@tonic-gate 		 * phci. Leadville specifies NDI_MDI_FALLBACK flag to
4450*0Sstevel@tonic-gate 		 * maintain the old behavior.
4451*0Sstevel@tonic-gate 		 */
4452*0Sstevel@tonic-gate 		if ((flags & NDI_MDI_FALLBACK) == 0)
4453*0Sstevel@tonic-gate 			return (NDI_FAILURE);
4454*0Sstevel@tonic-gate 	}
4455*0Sstevel@tonic-gate 
4456*0Sstevel@tonic-gate 	/* split name into "name@addr" parts */
4457*0Sstevel@tonic-gate 	i_ddi_parse_name(devnm, &name, &addr, NULL);
4458*0Sstevel@tonic-gate 
4459*0Sstevel@tonic-gate 	if (flags & NDI_PROMNAME) {
4460*0Sstevel@tonic-gate 		/*
4461*0Sstevel@tonic-gate 		 * We may have a genericname on a system that creates
4462*0Sstevel@tonic-gate 		 * drivername nodes (from .conf files).  Find the drivername
4463*0Sstevel@tonic-gate 		 * by nodeid. If we can't find a node with devnm as the
4464*0Sstevel@tonic-gate 		 * node name then we search by drivername.  This allows an
4465*0Sstevel@tonic-gate 		 * implementation to supply a genericly named boot path (disk)
4466*0Sstevel@tonic-gate 		 * and locate drivename nodes (sd).
4467*0Sstevel@tonic-gate 		 */
4468*0Sstevel@tonic-gate 		drivername = child_path_to_driver(pdip, name, addr);
4469*0Sstevel@tonic-gate 	}
4470*0Sstevel@tonic-gate 
4471*0Sstevel@tonic-gate 	if (timeout > 0) {
4472*0Sstevel@tonic-gate 		end_time = ddi_get_lbolt() + timeout;
4473*0Sstevel@tonic-gate 	}
4474*0Sstevel@tonic-gate 
4475*0Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
4476*0Sstevel@tonic-gate 
4477*0Sstevel@tonic-gate reprobe:
4478*0Sstevel@tonic-gate 	probed = (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
4479*0Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(pdip, flags);
4480*0Sstevel@tonic-gate 	for (;;) {
4481*0Sstevel@tonic-gate 		dip = find_child_by_name(pdip, name, addr);
4482*0Sstevel@tonic-gate 		/*
4483*0Sstevel@tonic-gate 		 * Search for a node bound to the drivername driver with
4484*0Sstevel@tonic-gate 		 * the specified "@addr".
4485*0Sstevel@tonic-gate 		 */
4486*0Sstevel@tonic-gate 		if (dip == NULL && drivername)
4487*0Sstevel@tonic-gate 			dip = find_child_by_driver(pdip, drivername, addr);
4488*0Sstevel@tonic-gate 
4489*0Sstevel@tonic-gate 		if (dip || timeout <= 0 || ddi_get_lbolt() >= end_time)
4490*0Sstevel@tonic-gate 			break;
4491*0Sstevel@tonic-gate 
4492*0Sstevel@tonic-gate 		/*
4493*0Sstevel@tonic-gate 		 * Wait up to end_time for asynchronous enumeration
4494*0Sstevel@tonic-gate 		 */
4495*0Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
4496*0Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT,
4497*0Sstevel@tonic-gate 		    "%s%d: waiting for child %s@%s, timeout %ld",
4498*0Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
4499*0Sstevel@tonic-gate 		    name, addr, timeout));
4500*0Sstevel@tonic-gate 
4501*0Sstevel@tonic-gate 		mutex_enter(&DEVI(pdip)->devi_lock);
4502*0Sstevel@tonic-gate 		(void) cv_timedwait(&DEVI(pdip)->devi_cv,
4503*0Sstevel@tonic-gate 		    &DEVI(pdip)->devi_lock, end_time);
4504*0Sstevel@tonic-gate 		mutex_exit(&DEVI(pdip)->devi_lock);
4505*0Sstevel@tonic-gate 		ndi_devi_enter(pdip, &circ);
4506*0Sstevel@tonic-gate 		(void) i_ndi_make_spec_children(pdip, flags);
4507*0Sstevel@tonic-gate 	}
4508*0Sstevel@tonic-gate 
4509*0Sstevel@tonic-gate 	if ((dip == NULL) && probed && (flags & NDI_CONFIG_REPROBE) &&
4510*0Sstevel@tonic-gate 	    i_ddi_io_initialized()) {
4511*0Sstevel@tonic-gate 		/*
4512*0Sstevel@tonic-gate 		 * reenumerate .conf nodes and probe again
4513*0Sstevel@tonic-gate 		 */
4514*0Sstevel@tonic-gate 		mutex_enter(&DEVI(pdip)->devi_lock);
4515*0Sstevel@tonic-gate 		DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
4516*0Sstevel@tonic-gate 		mutex_exit(&DEVI(pdip)->devi_lock);
4517*0Sstevel@tonic-gate 		goto reprobe;
4518*0Sstevel@tonic-gate 	}
4519*0Sstevel@tonic-gate 
4520*0Sstevel@tonic-gate 	if (addr[0] != '\0')
4521*0Sstevel@tonic-gate 		*(addr - 1) = '@';
4522*0Sstevel@tonic-gate 
4523*0Sstevel@tonic-gate 	if (dip == NULL || devi_attach_node(dip, flags) != NDI_SUCCESS) {
4524*0Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
4525*0Sstevel@tonic-gate 		return (NDI_FAILURE);
4526*0Sstevel@tonic-gate 	}
4527*0Sstevel@tonic-gate 
4528*0Sstevel@tonic-gate 	*dipp = dip;
4529*0Sstevel@tonic-gate 	ndi_hold_devi(dip);
4530*0Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
4531*0Sstevel@tonic-gate 	return (NDI_SUCCESS);
4532*0Sstevel@tonic-gate }
4533*0Sstevel@tonic-gate 
4534*0Sstevel@tonic-gate /*
4535*0Sstevel@tonic-gate  * Enumerate and attach a child specified by name 'devnm'.
4536*0Sstevel@tonic-gate  * Called by devfs lookup and DR to perform a BUS_CONFIG_ONE.
4537*0Sstevel@tonic-gate  * Note: devfs does not make use of NDI_CONFIG to configure
4538*0Sstevel@tonic-gate  * an entire branch.
4539*0Sstevel@tonic-gate  */
4540*0Sstevel@tonic-gate int
4541*0Sstevel@tonic-gate ndi_devi_config_one(dev_info_t *dip, char *devnm, dev_info_t **dipp, int flags)
4542*0Sstevel@tonic-gate {
4543*0Sstevel@tonic-gate 	int error;
4544*0Sstevel@tonic-gate 	int (*f)();
4545*0Sstevel@tonic-gate 	int branch_event = 0;
4546*0Sstevel@tonic-gate 
4547*0Sstevel@tonic-gate 	ASSERT(dipp);
4548*0Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(dip) >= DS_ATTACHED);
4549*0Sstevel@tonic-gate 
4550*0Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
4551*0Sstevel@tonic-gate 	    "ndi_devi_config_one: par = %s%d (%p), child = %s\n",
4552*0Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, devnm));
4553*0Sstevel@tonic-gate 
4554*0Sstevel@tonic-gate 	if (pm_pre_config(dip, devnm) != DDI_SUCCESS)
4555*0Sstevel@tonic-gate 		return (NDI_FAILURE);
4556*0Sstevel@tonic-gate 
4557*0Sstevel@tonic-gate 	if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
4558*0Sstevel@tonic-gate 	    (flags & NDI_CONFIG)) {
4559*0Sstevel@tonic-gate 		flags |= NDI_BRANCH_EVENT_OP;
4560*0Sstevel@tonic-gate 		branch_event = 1;
4561*0Sstevel@tonic-gate 	}
4562*0Sstevel@tonic-gate 
4563*0Sstevel@tonic-gate 	if ((DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
4564*0Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
4565*0Sstevel@tonic-gate 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
4566*0Sstevel@tonic-gate 		error = devi_config_one(dip, devnm, dipp, flags, 0);
4567*0Sstevel@tonic-gate 	} else {
4568*0Sstevel@tonic-gate 		/* call bus_config entry point */
4569*0Sstevel@tonic-gate 		error = (*f)(dip, flags, BUS_CONFIG_ONE, (void *)devnm, dipp);
4570*0Sstevel@tonic-gate 	}
4571*0Sstevel@tonic-gate 
4572*0Sstevel@tonic-gate 	if (error || (flags & NDI_CONFIG) == 0) {
4573*0Sstevel@tonic-gate 		pm_post_config(dip, devnm);
4574*0Sstevel@tonic-gate 		return (error);
4575*0Sstevel@tonic-gate 	}
4576*0Sstevel@tonic-gate 
4577*0Sstevel@tonic-gate 	/*
4578*0Sstevel@tonic-gate 	 * DR usage ((i.e. call with NDI_CONFIG) recursively configures
4579*0Sstevel@tonic-gate 	 * grandchildren, performing a BUS_CONFIG_ALL from the node attached
4580*0Sstevel@tonic-gate 	 * by the BUS_CONFIG_ONE.
4581*0Sstevel@tonic-gate 	 */
4582*0Sstevel@tonic-gate 	ASSERT(*dipp);
4583*0Sstevel@tonic-gate 
4584*0Sstevel@tonic-gate 	error = devi_config_common(*dipp, flags, (major_t)-1);
4585*0Sstevel@tonic-gate 
4586*0Sstevel@tonic-gate 	pm_post_config(dip, devnm);
4587*0Sstevel@tonic-gate 
4588*0Sstevel@tonic-gate 	if (branch_event)
4589*0Sstevel@tonic-gate 		(void) i_log_devfs_branch_add(*dipp);
4590*0Sstevel@tonic-gate 
4591*0Sstevel@tonic-gate 	return (error);
4592*0Sstevel@tonic-gate }
4593*0Sstevel@tonic-gate 
4594*0Sstevel@tonic-gate 
4595*0Sstevel@tonic-gate /*
4596*0Sstevel@tonic-gate  * Enumerate and attach a child specified by name 'devnm'.
4597*0Sstevel@tonic-gate  * Called during configure the OBP options. This configures
4598*0Sstevel@tonic-gate  * only one node.
4599*0Sstevel@tonic-gate  */
4600*0Sstevel@tonic-gate static int
4601*0Sstevel@tonic-gate ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
4602*0Sstevel@tonic-gate     dev_info_t **childp, int flags)
4603*0Sstevel@tonic-gate {
4604*0Sstevel@tonic-gate 	int error;
4605*0Sstevel@tonic-gate 	int (*f)();
4606*0Sstevel@tonic-gate 
4607*0Sstevel@tonic-gate 	ASSERT(childp);
4608*0Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(parent) >= DS_ATTACHED);
4609*0Sstevel@tonic-gate 
4610*0Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_config_obp_args: "
4611*0Sstevel@tonic-gate 	    "par = %s%d (%p), child = %s\n", ddi_driver_name(parent),
4612*0Sstevel@tonic-gate 	    ddi_get_instance(parent), (void *)parent, devnm));
4613*0Sstevel@tonic-gate 
4614*0Sstevel@tonic-gate 	if ((DEVI(parent)->devi_ops->devo_bus_ops == NULL) ||
4615*0Sstevel@tonic-gate 	    (DEVI(parent)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
4616*0Sstevel@tonic-gate 	    (f = DEVI(parent)->devi_ops->devo_bus_ops->bus_config) == NULL) {
4617*0Sstevel@tonic-gate 		error = NDI_FAILURE;
4618*0Sstevel@tonic-gate 	} else {
4619*0Sstevel@tonic-gate 		/* call bus_config entry point */
4620*0Sstevel@tonic-gate 		error = (*f)(parent, flags,
4621*0Sstevel@tonic-gate 		    BUS_CONFIG_OBP_ARGS, (void *)devnm, childp);
4622*0Sstevel@tonic-gate 	}
4623*0Sstevel@tonic-gate 	return (error);
4624*0Sstevel@tonic-gate }
4625*0Sstevel@tonic-gate 
4626*0Sstevel@tonic-gate 
4627*0Sstevel@tonic-gate /*
4628*0Sstevel@tonic-gate  * detach a node with parent already held busy
4629*0Sstevel@tonic-gate  */
4630*0Sstevel@tonic-gate static int
4631*0Sstevel@tonic-gate devi_detach_node(dev_info_t *dip, uint_t flags)
4632*0Sstevel@tonic-gate {
4633*0Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
4634*0Sstevel@tonic-gate 	int ret = NDI_SUCCESS;
4635*0Sstevel@tonic-gate 	ddi_eventcookie_t cookie;
4636*0Sstevel@tonic-gate 
4637*0Sstevel@tonic-gate 	if (flags & NDI_POST_EVENT) {
4638*0Sstevel@tonic-gate 		if (pdip && i_ddi_node_state(pdip) >= DS_ATTACHED) {
4639*0Sstevel@tonic-gate 			if (ddi_get_eventcookie(dip, DDI_DEVI_REMOVE_EVENT,
4640*0Sstevel@tonic-gate 			    &cookie) == NDI_SUCCESS)
4641*0Sstevel@tonic-gate 				(void) ndi_post_event(dip, dip, cookie, NULL);
4642*0Sstevel@tonic-gate 		}
4643*0Sstevel@tonic-gate 	}
4644*0Sstevel@tonic-gate 
4645*0Sstevel@tonic-gate 	if (i_ddi_detachchild(dip, flags) != DDI_SUCCESS)
4646*0Sstevel@tonic-gate 		return (NDI_FAILURE);
4647*0Sstevel@tonic-gate 
4648*0Sstevel@tonic-gate 	if (flags & NDI_AUTODETACH)
4649*0Sstevel@tonic-gate 		return (NDI_SUCCESS);
4650*0Sstevel@tonic-gate 
4651*0Sstevel@tonic-gate 	/*
4652*0Sstevel@tonic-gate 	 * For DR, even bound nodes may need to have offline
4653*0Sstevel@tonic-gate 	 * flag set.
4654*0Sstevel@tonic-gate 	 */
4655*0Sstevel@tonic-gate 	if (flags & NDI_DEVI_OFFLINE) {
4656*0Sstevel@tonic-gate 		DEVI_SET_DEVICE_OFFLINE(dip);
4657*0Sstevel@tonic-gate 	}
4658*0Sstevel@tonic-gate 
4659*0Sstevel@tonic-gate 	if (i_ddi_node_state(dip) == DS_INITIALIZED) {
4660*0Sstevel@tonic-gate 		char *path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4661*0Sstevel@tonic-gate 		(void) ddi_pathname(dip, path);
4662*0Sstevel@tonic-gate 		if (flags & NDI_DEVI_OFFLINE)
4663*0Sstevel@tonic-gate 			i_ndi_devi_report_status_change(dip, path);
4664*0Sstevel@tonic-gate 
4665*0Sstevel@tonic-gate 		if (need_remove_event(dip, flags)) {
4666*0Sstevel@tonic-gate 			(void) i_log_devfs_remove_devinfo(path,
4667*0Sstevel@tonic-gate 			    i_ddi_devi_class(dip),
4668*0Sstevel@tonic-gate 			    (char *)ddi_driver_name(dip),
4669*0Sstevel@tonic-gate 			    ddi_get_instance(dip),
4670*0Sstevel@tonic-gate 			    flags);
4671*0Sstevel@tonic-gate 			DEVI_SET_EVREMOVE(dip);
4672*0Sstevel@tonic-gate 		}
4673*0Sstevel@tonic-gate 		kmem_free(path, MAXPATHLEN);
4674*0Sstevel@tonic-gate 	}
4675*0Sstevel@tonic-gate 
4676*0Sstevel@tonic-gate 	if (flags & (NDI_UNCONFIG | NDI_DEVI_REMOVE)) {
4677*0Sstevel@tonic-gate 		ret = ddi_uninitchild(dip);
4678*0Sstevel@tonic-gate 		if (ret == NDI_SUCCESS) {
4679*0Sstevel@tonic-gate 			/*
4680*0Sstevel@tonic-gate 			 * Remove uninitialized pseudo nodes because
4681*0Sstevel@tonic-gate 			 * system props are lost and the node cannot be
4682*0Sstevel@tonic-gate 			 * reattached.
4683*0Sstevel@tonic-gate 			 */
4684*0Sstevel@tonic-gate 			if (!ndi_dev_is_persistent_node(dip))
4685*0Sstevel@tonic-gate 				flags |= NDI_DEVI_REMOVE;
4686*0Sstevel@tonic-gate 
4687*0Sstevel@tonic-gate 			if (flags & NDI_DEVI_REMOVE)
4688*0Sstevel@tonic-gate 				ret = ddi_remove_child(dip, 0);
4689*0Sstevel@tonic-gate 		}
4690*0Sstevel@tonic-gate 	}
4691*0Sstevel@tonic-gate 
4692*0Sstevel@tonic-gate 	return (ret);
4693*0Sstevel@tonic-gate }
4694*0Sstevel@tonic-gate 
4695*0Sstevel@tonic-gate /*
4696*0Sstevel@tonic-gate  * unconfigure immediate children of bus nexus device
4697*0Sstevel@tonic-gate  */
4698*0Sstevel@tonic-gate static int
4699*0Sstevel@tonic-gate unconfig_immediate_children(
4700*0Sstevel@tonic-gate 	dev_info_t *dip,
4701*0Sstevel@tonic-gate 	dev_info_t **dipp,
4702*0Sstevel@tonic-gate 	int flags,
4703*0Sstevel@tonic-gate 	major_t major)
4704*0Sstevel@tonic-gate {
4705*0Sstevel@tonic-gate 	int rv = NDI_SUCCESS, circ;
4706*0Sstevel@tonic-gate 	dev_info_t *child;
4707*0Sstevel@tonic-gate 
4708*0Sstevel@tonic-gate 	ASSERT(dipp == NULL || *dipp == NULL);
4709*0Sstevel@tonic-gate 
4710*0Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
4711*0Sstevel@tonic-gate 	child = ddi_get_child(dip);
4712*0Sstevel@tonic-gate 	while (child) {
4713*0Sstevel@tonic-gate 		dev_info_t *next = ddi_get_next_sibling(child);
4714*0Sstevel@tonic-gate 		if ((major != (major_t)-1) &&
4715*0Sstevel@tonic-gate 		    (major != ddi_driver_major(child))) {
4716*0Sstevel@tonic-gate 			child = next;
4717*0Sstevel@tonic-gate 			continue;
4718*0Sstevel@tonic-gate 		}
4719*0Sstevel@tonic-gate 
4720*0Sstevel@tonic-gate 		/* skip nexus nodes during autodetach */
4721*0Sstevel@tonic-gate 		if ((flags & NDI_AUTODETACH) && !is_leaf_node(child)) {
4722*0Sstevel@tonic-gate 			child = next;
4723*0Sstevel@tonic-gate 			continue;
4724*0Sstevel@tonic-gate 		}
4725*0Sstevel@tonic-gate 
4726*0Sstevel@tonic-gate 		if (devi_detach_node(child, flags) != NDI_SUCCESS) {
4727*0Sstevel@tonic-gate 			if (dipp && *dipp == NULL) {
4728*0Sstevel@tonic-gate 				ndi_hold_devi(child);
4729*0Sstevel@tonic-gate 				*dipp = child;
4730*0Sstevel@tonic-gate 			}
4731*0Sstevel@tonic-gate 			rv = NDI_FAILURE;
4732*0Sstevel@tonic-gate 		}
4733*0Sstevel@tonic-gate 
4734*0Sstevel@tonic-gate 		/*
4735*0Sstevel@tonic-gate 		 * Continue upon failure--best effort algorithm
4736*0Sstevel@tonic-gate 		 */
4737*0Sstevel@tonic-gate 		child = next;
4738*0Sstevel@tonic-gate 	}
4739*0Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
4740*0Sstevel@tonic-gate 	return (rv);
4741*0Sstevel@tonic-gate }
4742*0Sstevel@tonic-gate 
4743*0Sstevel@tonic-gate /*
4744*0Sstevel@tonic-gate  * unconfigure grand children of bus nexus device
4745*0Sstevel@tonic-gate  */
4746*0Sstevel@tonic-gate static int
4747*0Sstevel@tonic-gate unconfig_grand_children(
4748*0Sstevel@tonic-gate 	dev_info_t *dip,
4749*0Sstevel@tonic-gate 	dev_info_t **dipp,
4750*0Sstevel@tonic-gate 	int flags,
4751*0Sstevel@tonic-gate 	major_t major,
4752*0Sstevel@tonic-gate 	struct brevq_node **brevqp)
4753*0Sstevel@tonic-gate {
4754*0Sstevel@tonic-gate 	struct mt_config_handle *hdl;
4755*0Sstevel@tonic-gate 
4756*0Sstevel@tonic-gate 	if (brevqp)
4757*0Sstevel@tonic-gate 		*brevqp = NULL;
4758*0Sstevel@tonic-gate 
4759*0Sstevel@tonic-gate 	/* multi-threaded configuration of child nexus */
4760*0Sstevel@tonic-gate 	hdl = mt_config_init(dip, dipp, flags, major, MT_UNCONFIG_OP, brevqp);
4761*0Sstevel@tonic-gate 	mt_config_children(hdl);
4762*0Sstevel@tonic-gate 
4763*0Sstevel@tonic-gate 	return (mt_config_fini(hdl));	/* wait for threads to exit */
4764*0Sstevel@tonic-gate }
4765*0Sstevel@tonic-gate 
4766*0Sstevel@tonic-gate /*
4767*0Sstevel@tonic-gate  * Unconfigure children/descendants of the dip.
4768*0Sstevel@tonic-gate  *
4769*0Sstevel@tonic-gate  * If brevqp is not NULL, on return *brevqp is set to a queue of dip's
4770*0Sstevel@tonic-gate  * child devinames for which branch remove events need to be generated.
4771*0Sstevel@tonic-gate  */
4772*0Sstevel@tonic-gate static int
4773*0Sstevel@tonic-gate devi_unconfig_common(
4774*0Sstevel@tonic-gate 	dev_info_t *dip,
4775*0Sstevel@tonic-gate 	dev_info_t **dipp,
4776*0Sstevel@tonic-gate 	int flags,
4777*0Sstevel@tonic-gate 	major_t major,
4778*0Sstevel@tonic-gate 	struct brevq_node **brevqp)
4779*0Sstevel@tonic-gate {
4780*0Sstevel@tonic-gate 	int rv;
4781*0Sstevel@tonic-gate 	int pm_cookie;
4782*0Sstevel@tonic-gate 	int (*f)();
4783*0Sstevel@tonic-gate 	ddi_bus_config_op_t bus_op;
4784*0Sstevel@tonic-gate 
4785*0Sstevel@tonic-gate 	if (dipp)
4786*0Sstevel@tonic-gate 		*dipp = NULL;
4787*0Sstevel@tonic-gate 	if (brevqp)
4788*0Sstevel@tonic-gate 		*brevqp = NULL;
4789*0Sstevel@tonic-gate 
4790*0Sstevel@tonic-gate 	/*
4791*0Sstevel@tonic-gate 	 * Power up the dip if it is powered off.  If the flag bit
4792*0Sstevel@tonic-gate 	 * NDI_AUTODETACH is set and the dip is not at its full power,
4793*0Sstevel@tonic-gate 	 * skip the rest of the branch.
4794*0Sstevel@tonic-gate 	 */
4795*0Sstevel@tonic-gate 	if (pm_pre_unconfig(dip, flags, &pm_cookie, NULL) != DDI_SUCCESS)
4796*0Sstevel@tonic-gate 		return ((flags & NDI_AUTODETACH) ? NDI_SUCCESS :
4797*0Sstevel@tonic-gate 		    NDI_FAILURE);
4798*0Sstevel@tonic-gate 
4799*0Sstevel@tonic-gate 	/*
4800*0Sstevel@tonic-gate 	 * Some callers, notably SCSI, need to clear out the devfs
4801*0Sstevel@tonic-gate 	 * cache together with the unconfig to prevent stale entries.
4802*0Sstevel@tonic-gate 	 */
4803*0Sstevel@tonic-gate 	if (flags & NDI_DEVFS_CLEAN)
4804*0Sstevel@tonic-gate 		(void) devfs_clean(dip, NULL, 0);
4805*0Sstevel@tonic-gate 
4806*0Sstevel@tonic-gate 	rv = unconfig_grand_children(dip, dipp, flags, major, brevqp);
4807*0Sstevel@tonic-gate 
4808*0Sstevel@tonic-gate 	if ((rv != NDI_SUCCESS) && ((flags & NDI_AUTODETACH) == 0)) {
4809*0Sstevel@tonic-gate 		if (brevqp && *brevqp) {
4810*0Sstevel@tonic-gate 			log_and_free_br_events_on_grand_children(dip, *brevqp);
4811*0Sstevel@tonic-gate 			free_brevq(*brevqp);
4812*0Sstevel@tonic-gate 			*brevqp = NULL;
4813*0Sstevel@tonic-gate 		}
4814*0Sstevel@tonic-gate 		pm_post_unconfig(dip, pm_cookie, NULL);
4815*0Sstevel@tonic-gate 		return (rv);
4816*0Sstevel@tonic-gate 	}
4817*0Sstevel@tonic-gate 
4818*0Sstevel@tonic-gate 	if (dipp && *dipp) {
4819*0Sstevel@tonic-gate 		ndi_rele_devi(*dipp);
4820*0Sstevel@tonic-gate 		*dipp = NULL;
4821*0Sstevel@tonic-gate 	}
4822*0Sstevel@tonic-gate 
4823*0Sstevel@tonic-gate 	/*
4824*0Sstevel@tonic-gate 	 * It is possible to have a detached nexus with children
4825*0Sstevel@tonic-gate 	 * and grandchildren (for example: a branch consisting
4826*0Sstevel@tonic-gate 	 * entirely of bound nodes.) Since the nexus is detached
4827*0Sstevel@tonic-gate 	 * the bus_unconfig entry point cannot be used to remove
4828*0Sstevel@tonic-gate 	 * or unconfigure the descendants.
4829*0Sstevel@tonic-gate 	 */
4830*0Sstevel@tonic-gate 	if (i_ddi_node_state(dip) < DS_ATTACHED ||
4831*0Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
4832*0Sstevel@tonic-gate 	    (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
4833*0Sstevel@tonic-gate 	    (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
4834*0Sstevel@tonic-gate 		rv = unconfig_immediate_children(dip, dipp, flags, major);
4835*0Sstevel@tonic-gate 	} else {
4836*0Sstevel@tonic-gate 		/*
4837*0Sstevel@tonic-gate 		 * call bus_unconfig entry point
4838*0Sstevel@tonic-gate 		 * It should reset nexus flags if unconfigure succeeds.
4839*0Sstevel@tonic-gate 		 */
4840*0Sstevel@tonic-gate 		bus_op = (major == (major_t)-1) ?
4841*0Sstevel@tonic-gate 		    BUS_UNCONFIG_ALL : BUS_UNCONFIG_DRIVER;
4842*0Sstevel@tonic-gate 		rv = (*f)(dip, flags, bus_op, (void *)(uintptr_t)major);
4843*0Sstevel@tonic-gate 	}
4844*0Sstevel@tonic-gate 
4845*0Sstevel@tonic-gate 	pm_post_unconfig(dip, pm_cookie, NULL);
4846*0Sstevel@tonic-gate 
4847*0Sstevel@tonic-gate 	if (brevqp && *brevqp)
4848*0Sstevel@tonic-gate 		cleanup_br_events_on_grand_children(dip, brevqp);
4849*0Sstevel@tonic-gate 
4850*0Sstevel@tonic-gate 	return (rv);
4851*0Sstevel@tonic-gate }
4852*0Sstevel@tonic-gate 
4853*0Sstevel@tonic-gate /*
4854*0Sstevel@tonic-gate  * called by devfs/framework to unconfigure children bound to major
4855*0Sstevel@tonic-gate  * If NDI_AUTODETACH is specified, this is invoked by either the
4856*0Sstevel@tonic-gate  * moduninstall daemon or the modunload -i 0 command.
4857*0Sstevel@tonic-gate  */
4858*0Sstevel@tonic-gate int
4859*0Sstevel@tonic-gate ndi_devi_unconfig_driver(dev_info_t *dip, int flags, major_t major)
4860*0Sstevel@tonic-gate {
4861*0Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
4862*0Sstevel@tonic-gate 	    "ndi_devi_unconfig_driver: par = %s%d (%p), flags = 0x%x\n",
4863*0Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
4864*0Sstevel@tonic-gate 
4865*0Sstevel@tonic-gate 	return (devi_unconfig_common(dip, NULL, flags, major, NULL));
4866*0Sstevel@tonic-gate }
4867*0Sstevel@tonic-gate 
4868*0Sstevel@tonic-gate int
4869*0Sstevel@tonic-gate ndi_devi_unconfig(dev_info_t *dip, int flags)
4870*0Sstevel@tonic-gate {
4871*0Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
4872*0Sstevel@tonic-gate 	    "ndi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
4873*0Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
4874*0Sstevel@tonic-gate 
4875*0Sstevel@tonic-gate 	return (devi_unconfig_common(dip, NULL, flags, (major_t)-1, NULL));
4876*0Sstevel@tonic-gate }
4877*0Sstevel@tonic-gate 
4878*0Sstevel@tonic-gate int
4879*0Sstevel@tonic-gate e_ddi_devi_unconfig(dev_info_t *dip, dev_info_t **dipp, int flags)
4880*0Sstevel@tonic-gate {
4881*0Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
4882*0Sstevel@tonic-gate 	    "e_ddi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
4883*0Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
4884*0Sstevel@tonic-gate 
4885*0Sstevel@tonic-gate 	return (devi_unconfig_common(dip, dipp, flags, (major_t)-1, NULL));
4886*0Sstevel@tonic-gate }
4887*0Sstevel@tonic-gate 
4888*0Sstevel@tonic-gate /*
4889*0Sstevel@tonic-gate  * Unconfigure child by name
4890*0Sstevel@tonic-gate  */
4891*0Sstevel@tonic-gate static int
4892*0Sstevel@tonic-gate devi_unconfig_one(dev_info_t *pdip, char *devnm, int flags)
4893*0Sstevel@tonic-gate {
4894*0Sstevel@tonic-gate 	int rv, circ;
4895*0Sstevel@tonic-gate 	dev_info_t *child;
4896*0Sstevel@tonic-gate 
4897*0Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
4898*0Sstevel@tonic-gate 	child = ndi_devi_findchild(pdip, devnm);
4899*0Sstevel@tonic-gate 	if (child == NULL) {
4900*0Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT,
4901*0Sstevel@tonic-gate 		    "devi_unconfig_one: %s not found\n", devnm));
4902*0Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
4903*0Sstevel@tonic-gate 		return (NDI_SUCCESS);
4904*0Sstevel@tonic-gate 	}
4905*0Sstevel@tonic-gate 	rv = devi_detach_node(child, flags);
4906*0Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
4907*0Sstevel@tonic-gate 	return (rv);
4908*0Sstevel@tonic-gate }
4909*0Sstevel@tonic-gate 
4910*0Sstevel@tonic-gate int
4911*0Sstevel@tonic-gate ndi_devi_unconfig_one(
4912*0Sstevel@tonic-gate 	dev_info_t *pdip,
4913*0Sstevel@tonic-gate 	char *devnm,
4914*0Sstevel@tonic-gate 	dev_info_t **dipp,
4915*0Sstevel@tonic-gate 	int flags)
4916*0Sstevel@tonic-gate {
4917*0Sstevel@tonic-gate 	int (*f)();
4918*0Sstevel@tonic-gate 	int circ, rv;
4919*0Sstevel@tonic-gate 	int pm_cookie;
4920*0Sstevel@tonic-gate 	dev_info_t *child;
4921*0Sstevel@tonic-gate 	struct brevq_node *brevq = NULL;
4922*0Sstevel@tonic-gate 
4923*0Sstevel@tonic-gate 	ASSERT(i_ddi_node_state(pdip) >= DS_ATTACHED);
4924*0Sstevel@tonic-gate 
4925*0Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT,
4926*0Sstevel@tonic-gate 	    "ndi_devi_unconfig_one: par = %s%d (%p), child = %s\n",
4927*0Sstevel@tonic-gate 	    ddi_driver_name(pdip), ddi_get_instance(pdip),
4928*0Sstevel@tonic-gate 	    (void *)pdip, devnm));
4929*0Sstevel@tonic-gate 
4930*0Sstevel@tonic-gate 	if (pm_pre_unconfig(pdip, flags, &pm_cookie, devnm) != DDI_SUCCESS)
4931*0Sstevel@tonic-gate 		return (NDI_FAILURE);
4932*0Sstevel@tonic-gate 
4933*0Sstevel@tonic-gate 	if (dipp)
4934*0Sstevel@tonic-gate 		*dipp = NULL;
4935*0Sstevel@tonic-gate 
4936*0Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
4937*0Sstevel@tonic-gate 	child = ndi_devi_findchild(pdip, devnm);
4938*0Sstevel@tonic-gate 	if (child == NULL) {
4939*0Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_unconfig_one: %s"
4940*0Sstevel@tonic-gate 		    " not found\n", devnm));
4941*0Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
4942*0Sstevel@tonic-gate 		pm_post_unconfig(pdip, pm_cookie, devnm);
4943*0Sstevel@tonic-gate 		return (NDI_SUCCESS);
4944*0Sstevel@tonic-gate 	}
4945*0Sstevel@tonic-gate 
4946*0Sstevel@tonic-gate 	/*
4947*0Sstevel@tonic-gate 	 * Unconfigure children/descendants of named child
4948*0Sstevel@tonic-gate 	 */
4949*0Sstevel@tonic-gate 	rv = devi_unconfig_branch(child, dipp, flags | NDI_UNCONFIG, &brevq);
4950*0Sstevel@tonic-gate 	if (rv != NDI_SUCCESS)
4951*0Sstevel@tonic-gate 		goto out;
4952*0Sstevel@tonic-gate 
4953*0Sstevel@tonic-gate 	init_bound_node_ev(pdip, child, flags);
4954*0Sstevel@tonic-gate 
4955*0Sstevel@tonic-gate 	if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
4956*0Sstevel@tonic-gate 	    (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
4957*0Sstevel@tonic-gate 	    (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
4958*0Sstevel@tonic-gate 		rv = devi_detach_node(child, flags);
4959*0Sstevel@tonic-gate 	} else {
4960*0Sstevel@tonic-gate 		/* call bus_config entry point */
4961*0Sstevel@tonic-gate 		rv = (*f)(pdip, flags, BUS_UNCONFIG_ONE, (void *)devnm);
4962*0Sstevel@tonic-gate 	}
4963*0Sstevel@tonic-gate 
4964*0Sstevel@tonic-gate 	if (brevq) {
4965*0Sstevel@tonic-gate 		if (rv != NDI_SUCCESS)
4966*0Sstevel@tonic-gate 			log_and_free_brevq_dip(child, brevq);
4967*0Sstevel@tonic-gate 		else
4968*0Sstevel@tonic-gate 			free_brevq(brevq);
4969*0Sstevel@tonic-gate 	}
4970*0Sstevel@tonic-gate 
4971*0Sstevel@tonic-gate 	if (dipp && rv != NDI_SUCCESS) {
4972*0Sstevel@tonic-gate 		ndi_hold_devi(child);
4973*0Sstevel@tonic-gate 		ASSERT(*dipp == NULL);
4974*0Sstevel@tonic-gate 		*dipp = child;
4975*0Sstevel@tonic-gate 	}
4976*0Sstevel@tonic-gate 
4977*0Sstevel@tonic-gate out:
4978*0Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
4979*0Sstevel@tonic-gate 	pm_post_unconfig(pdip, pm_cookie, devnm);
4980*0Sstevel@tonic-gate 
4981*0Sstevel@tonic-gate 	return (rv);
4982*0Sstevel@tonic-gate }
4983*0Sstevel@tonic-gate 
4984*0Sstevel@tonic-gate struct async_arg {
4985*0Sstevel@tonic-gate 	dev_info_t *dip;
4986*0Sstevel@tonic-gate 	uint_t flags;
4987*0Sstevel@tonic-gate };
4988*0Sstevel@tonic-gate 
4989*0Sstevel@tonic-gate /*
4990*0Sstevel@tonic-gate  * Common async handler for:
4991*0Sstevel@tonic-gate  *	ndi_devi_bind_driver_async
4992*0Sstevel@tonic-gate  *	ndi_devi_online_async
4993*0Sstevel@tonic-gate  */
4994*0Sstevel@tonic-gate static int
4995*0Sstevel@tonic-gate i_ndi_devi_async_common(dev_info_t *dip, uint_t flags, void (*func)())
4996*0Sstevel@tonic-gate {
4997*0Sstevel@tonic-gate 	int tqflag;
4998*0Sstevel@tonic-gate 	int kmflag;
4999*0Sstevel@tonic-gate 	struct async_arg *arg;
5000*0Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
5001*0Sstevel@tonic-gate 
5002*0Sstevel@tonic-gate 	ASSERT(pdip);
5003*0Sstevel@tonic-gate 	ASSERT(DEVI(pdip)->devi_taskq);
5004*0Sstevel@tonic-gate 	ASSERT(ndi_dev_is_persistent_node(dip));
5005*0Sstevel@tonic-gate 
5006*0Sstevel@tonic-gate 	if (flags & NDI_NOSLEEP) {
5007*0Sstevel@tonic-gate 		kmflag = KM_NOSLEEP;
5008*0Sstevel@tonic-gate 		tqflag = TQ_NOSLEEP;
5009*0Sstevel@tonic-gate 	} else {
5010*0Sstevel@tonic-gate 		kmflag = KM_SLEEP;
5011*0Sstevel@tonic-gate 		tqflag = TQ_SLEEP;
5012*0Sstevel@tonic-gate 	}
5013*0Sstevel@tonic-gate 
5014*0Sstevel@tonic-gate 	arg = kmem_alloc(sizeof (*arg), kmflag);
5015*0Sstevel@tonic-gate 	if (arg == NULL)
5016*0Sstevel@tonic-gate 		goto fail;
5017*0Sstevel@tonic-gate 
5018*0Sstevel@tonic-gate 	arg->flags = flags;
5019*0Sstevel@tonic-gate 	arg->dip = dip;
5020*0Sstevel@tonic-gate 	if (ddi_taskq_dispatch(DEVI(pdip)->devi_taskq, func, arg, tqflag) ==
5021*0Sstevel@tonic-gate 	    DDI_SUCCESS) {
5022*0Sstevel@tonic-gate 		return (NDI_SUCCESS);
5023*0Sstevel@tonic-gate 	}
5024*0Sstevel@tonic-gate 
5025*0Sstevel@tonic-gate fail:
5026*0Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "%s%d: ddi_taskq_dispatch failed",
5027*0Sstevel@tonic-gate 	    ddi_driver_name(pdip), ddi_get_instance(pdip)));
5028*0Sstevel@tonic-gate 
5029*0Sstevel@tonic-gate 	if (arg)
5030*0Sstevel@tonic-gate 		kmem_free(arg, sizeof (*arg));
5031*0Sstevel@tonic-gate 	return (NDI_FAILURE);
5032*0Sstevel@tonic-gate }
5033*0Sstevel@tonic-gate 
5034*0Sstevel@tonic-gate static void
5035*0Sstevel@tonic-gate i_ndi_devi_bind_driver_cb(struct async_arg *arg)
5036*0Sstevel@tonic-gate {
5037*0Sstevel@tonic-gate 	(void) ndi_devi_bind_driver(arg->dip, arg->flags);
5038*0Sstevel@tonic-gate 	kmem_free(arg, sizeof (*arg));
5039*0Sstevel@tonic-gate }
5040*0Sstevel@tonic-gate 
5041*0Sstevel@tonic-gate int
5042*0Sstevel@tonic-gate ndi_devi_bind_driver_async(dev_info_t *dip, uint_t flags)
5043*0Sstevel@tonic-gate {
5044*0Sstevel@tonic-gate 	return (i_ndi_devi_async_common(dip, flags,
5045*0Sstevel@tonic-gate 	    (void (*)())i_ndi_devi_bind_driver_cb));
5046*0Sstevel@tonic-gate }
5047*0Sstevel@tonic-gate 
5048*0Sstevel@tonic-gate /*
5049*0Sstevel@tonic-gate  * place the devinfo in the ONLINE state.
5050*0Sstevel@tonic-gate  */
5051*0Sstevel@tonic-gate int
5052*0Sstevel@tonic-gate ndi_devi_online(dev_info_t *dip, uint_t flags)
5053*0Sstevel@tonic-gate {
5054*0Sstevel@tonic-gate 	int circ, rv;
5055*0Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
5056*0Sstevel@tonic-gate 	int branch_event = 0;
5057*0Sstevel@tonic-gate 
5058*0Sstevel@tonic-gate 	ASSERT(pdip);
5059*0Sstevel@tonic-gate 
5060*0Sstevel@tonic-gate 	NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_online: %s%d (%p)\n",
5061*0Sstevel@tonic-gate 		ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
5062*0Sstevel@tonic-gate 
5063*0Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
5064*0Sstevel@tonic-gate 	/* bind child before merging .conf nodes */
5065*0Sstevel@tonic-gate 	rv = i_ndi_config_node(dip, DS_BOUND, flags);
5066*0Sstevel@tonic-gate 	if (rv != NDI_SUCCESS) {
5067*0Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
5068*0Sstevel@tonic-gate 		return (rv);
5069*0Sstevel@tonic-gate 	}
5070*0Sstevel@tonic-gate 
5071*0Sstevel@tonic-gate 	/* merge .conf properties */
5072*0Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(pdip, flags);
5073*0Sstevel@tonic-gate 
5074*0Sstevel@tonic-gate 	flags |= (NDI_DEVI_ONLINE | NDI_CONFIG);
5075*0Sstevel@tonic-gate 
5076*0Sstevel@tonic-gate 	if (flags & NDI_NO_EVENT) {
5077*0Sstevel@tonic-gate 		/*
5078*0Sstevel@tonic-gate 		 * Caller is specifically asking for not to generate an event.
5079*0Sstevel@tonic-gate 		 * Set the following flag so that devi_attach_node() don't
5080*0Sstevel@tonic-gate 		 * change the event state.
5081*0Sstevel@tonic-gate 		 */
5082*0Sstevel@tonic-gate 		flags |= NDI_NO_EVENT_STATE_CHNG;
5083*0Sstevel@tonic-gate 	}
5084*0Sstevel@tonic-gate 
5085*0Sstevel@tonic-gate 	if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
5086*0Sstevel@tonic-gate 	    ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip))) {
5087*0Sstevel@tonic-gate 		flags |= NDI_BRANCH_EVENT_OP;
5088*0Sstevel@tonic-gate 		branch_event = 1;
5089*0Sstevel@tonic-gate 	}
5090*0Sstevel@tonic-gate 
5091*0Sstevel@tonic-gate 	/*
5092*0Sstevel@tonic-gate 	 * devi_attach_node() may remove dip on failure
5093*0Sstevel@tonic-gate 	 */
5094*0Sstevel@tonic-gate 	if ((rv = devi_attach_node(dip, flags)) == NDI_SUCCESS) {
5095*0Sstevel@tonic-gate 		if ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip)) {
5096*0Sstevel@tonic-gate 			(void) ndi_devi_config(dip, flags);
5097*0Sstevel@tonic-gate 		}
5098*0Sstevel@tonic-gate 
5099*0Sstevel@tonic-gate 		if (branch_event)
5100*0Sstevel@tonic-gate 			(void) i_log_devfs_branch_add(dip);
5101*0Sstevel@tonic-gate 	}
5102*0Sstevel@tonic-gate 
5103*0Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
5104*0Sstevel@tonic-gate 
5105*0Sstevel@tonic-gate 	/*
5106*0Sstevel@tonic-gate 	 * Notify devfs that we have a new node. Devfs needs to invalidate
5107*0Sstevel@tonic-gate 	 * cached directory contents.
5108*0Sstevel@tonic-gate 	 *
5109*0Sstevel@tonic-gate 	 * For PCMCIA devices, it is possible the pdip is not fully
5110*0Sstevel@tonic-gate 	 * attached. In this case, calling back into devfs will
5111*0Sstevel@tonic-gate 	 * result in a loop or assertion error. Hence, the check
5112*0Sstevel@tonic-gate 	 * on node state.
5113*0Sstevel@tonic-gate 	 *
5114*0Sstevel@tonic-gate 	 * If we own parent lock, this is part of a branch operation.
5115*0Sstevel@tonic-gate 	 * We skip the devfs_clean() step because the cache invalidation
5116*0Sstevel@tonic-gate 	 * is done higher up in the device tree.
5117*0Sstevel@tonic-gate 	 */
5118*0Sstevel@tonic-gate 	if (rv == NDI_SUCCESS && i_ddi_node_state(pdip) == DS_READY &&
5119*0Sstevel@tonic-gate 	    !DEVI_BUSY_OWNED(pdip))
5120*0Sstevel@tonic-gate 		(void) devfs_clean(pdip, NULL, 0);
5121*0Sstevel@tonic-gate 	return (rv);
5122*0Sstevel@tonic-gate }
5123*0Sstevel@tonic-gate 
5124*0Sstevel@tonic-gate static void
5125*0Sstevel@tonic-gate i_ndi_devi_online_cb(struct async_arg *arg)
5126*0Sstevel@tonic-gate {
5127*0Sstevel@tonic-gate 	(void) ndi_devi_online(arg->dip, arg->flags);
5128*0Sstevel@tonic-gate 	kmem_free(arg, sizeof (*arg));
5129*0Sstevel@tonic-gate }
5130*0Sstevel@tonic-gate 
5131*0Sstevel@tonic-gate int
5132*0Sstevel@tonic-gate ndi_devi_online_async(dev_info_t *dip, uint_t flags)
5133*0Sstevel@tonic-gate {
5134*0Sstevel@tonic-gate 	/* mark child as need config if requested. */
5135*0Sstevel@tonic-gate 	if (flags & NDI_CONFIG)
5136*0Sstevel@tonic-gate 		DEVI_SET_NDI_CONFIG(dip);
5137*0Sstevel@tonic-gate 
5138*0Sstevel@tonic-gate 	return (i_ndi_devi_async_common(dip, flags,
5139*0Sstevel@tonic-gate 	    (void (*)())i_ndi_devi_online_cb));
5140*0Sstevel@tonic-gate }
5141*0Sstevel@tonic-gate 
5142*0Sstevel@tonic-gate /*
5143*0Sstevel@tonic-gate  * Take a device node Offline
5144*0Sstevel@tonic-gate  * To take a device Offline means to detach the device instance from
5145*0Sstevel@tonic-gate  * the driver and prevent devfs requests from re-attaching the device
5146*0Sstevel@tonic-gate  * instance.
5147*0Sstevel@tonic-gate  *
5148*0Sstevel@tonic-gate  * The flag NDI_DEVI_REMOVE causes removes the device node from
5149*0Sstevel@tonic-gate  * the driver list and the device tree. In this case, the device
5150*0Sstevel@tonic-gate  * is assumed to be removed from the system.
5151*0Sstevel@tonic-gate  */
5152*0Sstevel@tonic-gate int
5153*0Sstevel@tonic-gate ndi_devi_offline(dev_info_t *dip, uint_t flags)
5154*0Sstevel@tonic-gate {
5155*0Sstevel@tonic-gate 	int circ, rval = 0;
5156*0Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(dip);
5157*0Sstevel@tonic-gate 	struct brevq_node *brevq = NULL;
5158*0Sstevel@tonic-gate 
5159*0Sstevel@tonic-gate 	ASSERT(pdip);
5160*0Sstevel@tonic-gate 
5161*0Sstevel@tonic-gate 	flags |= NDI_DEVI_OFFLINE;
5162*0Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
5163*0Sstevel@tonic-gate 	if (i_ddi_node_state(dip) == DS_READY) {
5164*0Sstevel@tonic-gate 		/*
5165*0Sstevel@tonic-gate 		 * If dip is in DS_READY state, there may be cached dv_nodes
5166*0Sstevel@tonic-gate 		 * referencing this dip, so we invoke devfs code path.
5167*0Sstevel@tonic-gate 		 * Note that we must release busy changing on pdip to
5168*0Sstevel@tonic-gate 		 * avoid deadlock against devfs.
5169*0Sstevel@tonic-gate 		 */
5170*0Sstevel@tonic-gate 		char *devname = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
5171*0Sstevel@tonic-gate 		(void) ddi_deviname(dip, devname);
5172*0Sstevel@tonic-gate 		ndi_devi_exit(pdip, circ);
5173*0Sstevel@tonic-gate 
5174*0Sstevel@tonic-gate 		/*
5175*0Sstevel@tonic-gate 		 * If we own parent lock, this is part of a branch
5176*0Sstevel@tonic-gate 		 * operation. We skip the devfs_clean() step.
5177*0Sstevel@tonic-gate 		 */
5178*0Sstevel@tonic-gate 		if (!DEVI_BUSY_OWNED(pdip))
5179*0Sstevel@tonic-gate 			rval = devfs_clean(pdip, devname + 1, DV_CLEAN_FORCE);
5180*0Sstevel@tonic-gate 		kmem_free(devname, MAXNAMELEN + 1);
5181*0Sstevel@tonic-gate 
5182*0Sstevel@tonic-gate 		if (rval == 0)
5183*0Sstevel@tonic-gate 			rval = devi_unconfig_branch(dip, NULL,
5184*0Sstevel@tonic-gate 			    flags|NDI_UNCONFIG, &brevq);
5185*0Sstevel@tonic-gate 		if (rval)
5186*0Sstevel@tonic-gate 			return (NDI_FAILURE);
5187*0Sstevel@tonic-gate 
5188*0Sstevel@tonic-gate 		ndi_devi_enter(pdip, &circ);
5189*0Sstevel@tonic-gate 	}
5190*0Sstevel@tonic-gate 
5191*0Sstevel@tonic-gate 	init_bound_node_ev(pdip, dip, flags);
5192*0Sstevel@tonic-gate 
5193*0Sstevel@tonic-gate 	rval = devi_detach_node(dip, flags);
5194*0Sstevel@tonic-gate 	if (brevq) {
5195*0Sstevel@tonic-gate 		if (rval != NDI_SUCCESS)
5196*0Sstevel@tonic-gate 			log_and_free_brevq_dip(dip, brevq);
5197*0Sstevel@tonic-gate 		else
5198*0Sstevel@tonic-gate 			free_brevq(brevq);
5199*0Sstevel@tonic-gate 	}
5200*0Sstevel@tonic-gate 
5201*0Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
5202*0Sstevel@tonic-gate 
5203*0Sstevel@tonic-gate 	return (rval);
5204*0Sstevel@tonic-gate }
5205*0Sstevel@tonic-gate 
5206*0Sstevel@tonic-gate /*
5207*0Sstevel@tonic-gate  * Find the child dev_info node of parent nexus 'p' whose name
5208*0Sstevel@tonic-gate  * matches "cname@caddr".  Recommend use of ndi_devi_findchild() instead.
5209*0Sstevel@tonic-gate  */
5210*0Sstevel@tonic-gate dev_info_t *
5211*0Sstevel@tonic-gate ndi_devi_find(dev_info_t *pdip, char *cname, char *caddr)
5212*0Sstevel@tonic-gate {
5213*0Sstevel@tonic-gate 	dev_info_t *child;
5214*0Sstevel@tonic-gate 	int circ;
5215*0Sstevel@tonic-gate 
5216*0Sstevel@tonic-gate 	if (pdip == NULL || cname == NULL || caddr == NULL)
5217*0Sstevel@tonic-gate 		return ((dev_info_t *)NULL);
5218*0Sstevel@tonic-gate 
5219*0Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
5220*0Sstevel@tonic-gate 	child = find_sibling(ddi_get_child(pdip), cname, caddr, 0, NULL);
5221*0Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
5222*0Sstevel@tonic-gate 	return (child);
5223*0Sstevel@tonic-gate }
5224*0Sstevel@tonic-gate 
5225*0Sstevel@tonic-gate /*
5226*0Sstevel@tonic-gate  * Find the child dev_info node of parent nexus 'p' whose name
5227*0Sstevel@tonic-gate  * matches devname "name@addr".  Permits caller to hold the parent.
5228*0Sstevel@tonic-gate  */
5229*0Sstevel@tonic-gate dev_info_t *
5230*0Sstevel@tonic-gate ndi_devi_findchild(dev_info_t *pdip, char *devname)
5231*0Sstevel@tonic-gate {
5232*0Sstevel@tonic-gate 	dev_info_t *child;
5233*0Sstevel@tonic-gate 	char	*cname, *caddr;
5234*0Sstevel@tonic-gate 	char	*devstr;
5235*0Sstevel@tonic-gate 
5236*0Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(pdip));
5237*0Sstevel@tonic-gate 
5238*0Sstevel@tonic-gate 	devstr = i_ddi_strdup(devname, KM_SLEEP);
5239*0Sstevel@tonic-gate 	i_ddi_parse_name(devstr, &cname, &caddr, NULL);
5240*0Sstevel@tonic-gate 
5241*0Sstevel@tonic-gate 	if (cname == NULL || caddr == NULL) {
5242*0Sstevel@tonic-gate 		kmem_free(devstr, strlen(devname)+1);
5243*0Sstevel@tonic-gate 		return ((dev_info_t *)NULL);
5244*0Sstevel@tonic-gate 	}
5245*0Sstevel@tonic-gate 
5246*0Sstevel@tonic-gate 	child = find_sibling(ddi_get_child(pdip), cname, caddr, 0, NULL);
5247*0Sstevel@tonic-gate 	kmem_free(devstr, strlen(devname)+1);
5248*0Sstevel@tonic-gate 	return (child);
5249*0Sstevel@tonic-gate }
5250*0Sstevel@tonic-gate 
5251*0Sstevel@tonic-gate /*
5252*0Sstevel@tonic-gate  * Misc. routines called by framework only
5253*0Sstevel@tonic-gate  */
5254*0Sstevel@tonic-gate 
5255*0Sstevel@tonic-gate /*
5256*0Sstevel@tonic-gate  * Clear the DEVI_MADE_CHILDREN/DEVI_ATTACHED_CHILDREN flags
5257*0Sstevel@tonic-gate  * if new child spec has been added.
5258*0Sstevel@tonic-gate  */
5259*0Sstevel@tonic-gate static int
5260*0Sstevel@tonic-gate reset_nexus_flags(dev_info_t *dip, void *arg)
5261*0Sstevel@tonic-gate {
5262*0Sstevel@tonic-gate 	struct hwc_spec *list;
5263*0Sstevel@tonic-gate 
5264*0Sstevel@tonic-gate 	if (((DEVI(dip)->devi_flags & DEVI_MADE_CHILDREN) == 0) ||
5265*0Sstevel@tonic-gate 	    ((list = hwc_get_child_spec(dip, (major_t)(uintptr_t)arg)) == NULL))
5266*0Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
5267*0Sstevel@tonic-gate 
5268*0Sstevel@tonic-gate 	hwc_free_spec_list(list);
5269*0Sstevel@tonic-gate 	mutex_enter(&DEVI(dip)->devi_lock);
5270*0Sstevel@tonic-gate 	DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN | DEVI_ATTACHED_CHILDREN);
5271*0Sstevel@tonic-gate 	mutex_exit(&DEVI(dip)->devi_lock);
5272*0Sstevel@tonic-gate 
5273*0Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
5274*0Sstevel@tonic-gate }
5275*0Sstevel@tonic-gate 
5276*0Sstevel@tonic-gate /*
5277*0Sstevel@tonic-gate  * Helper functions, returns NULL if no memory.
5278*0Sstevel@tonic-gate  */
5279*0Sstevel@tonic-gate 
5280*0Sstevel@tonic-gate /*
5281*0Sstevel@tonic-gate  * path_to_major:
5282*0Sstevel@tonic-gate  *
5283*0Sstevel@tonic-gate  * Return an alternate driver name binding for the leaf device
5284*0Sstevel@tonic-gate  * of the given pathname, if there is one. The purpose of this
5285*0Sstevel@tonic-gate  * function is to deal with generic pathnames. The default action
5286*0Sstevel@tonic-gate  * for platforms that can't do this (ie: x86 or any platform that
5287*0Sstevel@tonic-gate  * does not have prom_finddevice functionality, which matches
5288*0Sstevel@tonic-gate  * nodenames and unit-addresses without the drivers participation)
5289*0Sstevel@tonic-gate  * is to return (major_t)-1.
5290*0Sstevel@tonic-gate  *
5291*0Sstevel@tonic-gate  * Used in loadrootmodules() in the swapgeneric module to
5292*0Sstevel@tonic-gate  * associate a given pathname with a given leaf driver.
5293*0Sstevel@tonic-gate  *
5294*0Sstevel@tonic-gate  */
5295*0Sstevel@tonic-gate major_t
5296*0Sstevel@tonic-gate path_to_major(char *path)
5297*0Sstevel@tonic-gate {
5298*0Sstevel@tonic-gate 	dev_info_t *dip;
5299*0Sstevel@tonic-gate 	char *p, *q;
5300*0Sstevel@tonic-gate 	dnode_t nodeid;
5301*0Sstevel@tonic-gate 	major_t maj;
5302*0Sstevel@tonic-gate 
5303*0Sstevel@tonic-gate 	/*
5304*0Sstevel@tonic-gate 	 * Get the nodeid of the given pathname, if such a mapping exists.
5305*0Sstevel@tonic-gate 	 */
5306*0Sstevel@tonic-gate 	dip = NULL;
5307*0Sstevel@tonic-gate 	nodeid = prom_finddevice(path);
5308*0Sstevel@tonic-gate 	if (nodeid != OBP_BADNODE) {
5309*0Sstevel@tonic-gate 		/*
5310*0Sstevel@tonic-gate 		 * Find the nodeid in our copy of the device tree and return
5311*0Sstevel@tonic-gate 		 * whatever name we used to bind this node to a driver.
5312*0Sstevel@tonic-gate 		 */
5313*0Sstevel@tonic-gate 		dip = e_ddi_nodeid_to_dip(nodeid);
5314*0Sstevel@tonic-gate 	}
5315*0Sstevel@tonic-gate 
5316*0Sstevel@tonic-gate 	if (dip == NULL) {
5317*0Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_WARN,
5318*0Sstevel@tonic-gate 		    "path_to_major: can't bind <%s>\n", path));
5319*0Sstevel@tonic-gate 		return ((major_t)-1);
5320*0Sstevel@tonic-gate 	}
5321*0Sstevel@tonic-gate 
5322*0Sstevel@tonic-gate 	/*
5323*0Sstevel@tonic-gate 	 * If we're bound to something other than the nodename,
5324*0Sstevel@tonic-gate 	 * note that in the message buffer and system log.
5325*0Sstevel@tonic-gate 	 */
5326*0Sstevel@tonic-gate 	p = ddi_binding_name(dip);
5327*0Sstevel@tonic-gate 	q = ddi_node_name(dip);
5328*0Sstevel@tonic-gate 	if (p && q && (strcmp(p, q) != 0))
5329*0Sstevel@tonic-gate 		NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s bound to %s\n",
5330*0Sstevel@tonic-gate 		    path, p));
5331*0Sstevel@tonic-gate 
5332*0Sstevel@tonic-gate 	maj = ddi_name_to_major(p);
5333*0Sstevel@tonic-gate 
5334*0Sstevel@tonic-gate 	ndi_rele_devi(dip);		/* release node held during walk */
5335*0Sstevel@tonic-gate 
5336*0Sstevel@tonic-gate 	return (maj);
5337*0Sstevel@tonic-gate }
5338*0Sstevel@tonic-gate 
5339*0Sstevel@tonic-gate /*
5340*0Sstevel@tonic-gate  * Return the held dip for the specified major and instance, attempting to do
5341*0Sstevel@tonic-gate  * an attach if specified. Return NULL if the devi can't be found or put in
5342*0Sstevel@tonic-gate  * the proper state. The caller must release the hold via ddi_release_devi if
5343*0Sstevel@tonic-gate  * a non-NULL value is returned.
5344*0Sstevel@tonic-gate  *
5345*0Sstevel@tonic-gate  * Some callers expect to be able to perform a hold_devi() while in a context
5346*0Sstevel@tonic-gate  * where using ndi_devi_enter() to ensure the hold might cause deadlock (see
5347*0Sstevel@tonic-gate  * open-from-attach code in consconfig_dacf.c). Such special-case callers
5348*0Sstevel@tonic-gate  * must ensure that an ndi_devi_enter(parent)/ndi_devi_hold() from a safe
5349*0Sstevel@tonic-gate  * context is already active. The hold_devi() implementation must accommodate
5350*0Sstevel@tonic-gate  * these callers.
5351*0Sstevel@tonic-gate  */
5352*0Sstevel@tonic-gate static dev_info_t *
5353*0Sstevel@tonic-gate hold_devi(major_t major, int instance, int flags)
5354*0Sstevel@tonic-gate {
5355*0Sstevel@tonic-gate 	struct devnames	*dnp;
5356*0Sstevel@tonic-gate 	dev_info_t	*dip;
5357*0Sstevel@tonic-gate 	char		*path;
5358*0Sstevel@tonic-gate 
5359*0Sstevel@tonic-gate 	if ((major >= devcnt) || (instance == -1))
5360*0Sstevel@tonic-gate 		return (NULL);
5361*0Sstevel@tonic-gate 
5362*0Sstevel@tonic-gate 	/* try to find the instance in the per driver list */
5363*0Sstevel@tonic-gate 	dnp = &(devnamesp[major]);
5364*0Sstevel@tonic-gate 	LOCK_DEV_OPS(&(dnp->dn_lock));
5365*0Sstevel@tonic-gate 	for (dip = dnp->dn_head; dip;
5366*0Sstevel@tonic-gate 	    dip = (dev_info_t *)DEVI(dip)->devi_next) {
5367*0Sstevel@tonic-gate 		/* skip node if instance field is not valid */
5368*0Sstevel@tonic-gate 		if (i_ddi_node_state(dip) < DS_INITIALIZED)
5369*0Sstevel@tonic-gate 			continue;
5370*0Sstevel@tonic-gate 
5371*0Sstevel@tonic-gate 		/* look for instance match */
5372*0Sstevel@tonic-gate 		if (DEVI(dip)->devi_instance == instance) {
5373*0Sstevel@tonic-gate 			/*
5374*0Sstevel@tonic-gate 			 * To accommodate callers that can't block in
5375*0Sstevel@tonic-gate 			 * ndi_devi_enter() we do an ndi_devi_hold(), and
5376*0Sstevel@tonic-gate 			 * afterwards check that the node is in a state where
5377*0Sstevel@tonic-gate 			 * the hold prevents detach(). If we did not manage to
5378*0Sstevel@tonic-gate 			 * prevent detach then we ndi_rele_devi() and perform
5379*0Sstevel@tonic-gate 			 * the slow path below (which can result in a blocking
5380*0Sstevel@tonic-gate 			 * ndi_devi_enter() while driving attach top-down).
5381*0Sstevel@tonic-gate 			 * This code depends on the ordering of
5382*0Sstevel@tonic-gate 			 * DEVI_SET_DETACHING and the devi_ref check in the
5383*0Sstevel@tonic-gate 			 * detach_node() code path.
5384*0Sstevel@tonic-gate 			 */
5385*0Sstevel@tonic-gate 			ndi_hold_devi(dip);
5386*0Sstevel@tonic-gate 			if ((i_ddi_node_state(dip) >= DS_ATTACHED) &&
5387*0Sstevel@tonic-gate 			    !DEVI_IS_DETACHING(dip)) {
5388*0Sstevel@tonic-gate 				UNLOCK_DEV_OPS(&(dnp->dn_lock));
5389*0Sstevel@tonic-gate 				return (dip);	/* fast-path with devi held */
5390*0Sstevel@tonic-gate 			}
5391*0Sstevel@tonic-gate 			ndi_rele_devi(dip);
5392*0Sstevel@tonic-gate 
5393*0Sstevel@tonic-gate 			/* try slow-path */
5394*0Sstevel@tonic-gate 			dip = NULL;
5395*0Sstevel@tonic-gate 			break;
5396*0Sstevel@tonic-gate 		}
5397*0Sstevel@tonic-gate 	}
5398*0Sstevel@tonic-gate 	ASSERT(dip == NULL);
5399*0Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
5400*0Sstevel@tonic-gate 
5401*0Sstevel@tonic-gate 	if (flags & E_DDI_HOLD_DEVI_NOATTACH)
5402*0Sstevel@tonic-gate 		return (NULL);		/* told not to drive attach */
5403*0Sstevel@tonic-gate 
5404*0Sstevel@tonic-gate 	/* slow-path may block, so it should not occur from interrupt */
5405*0Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
5406*0Sstevel@tonic-gate 	if (servicing_interrupt())
5407*0Sstevel@tonic-gate 		return (NULL);
5408*0Sstevel@tonic-gate 
5409*0Sstevel@tonic-gate 	/* reconstruct the path and drive attach by path through devfs. */
5410*0Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
5411*0Sstevel@tonic-gate 	if (e_ddi_majorinstance_to_path(major, instance, path) == 0)
5412*0Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_path(path, flags);
5413*0Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
5414*0Sstevel@tonic-gate 	return (dip);			/* with devi held */
5415*0Sstevel@tonic-gate }
5416*0Sstevel@tonic-gate 
5417*0Sstevel@tonic-gate /*
5418*0Sstevel@tonic-gate  * The {e_}ddi_hold_devi{_by_{instance|dev|path}} hold the devinfo node
5419*0Sstevel@tonic-gate  * associated with the specified arguments.  This hold should be released
5420*0Sstevel@tonic-gate  * by calling ddi_release_devi.
5421*0Sstevel@tonic-gate  *
5422*0Sstevel@tonic-gate  * The E_DDI_HOLD_DEVI_NOATTACH flag argument allows the caller to to specify
5423*0Sstevel@tonic-gate  * a failure return if the node is not already attached.
5424*0Sstevel@tonic-gate  *
5425*0Sstevel@tonic-gate  * NOTE: by the time we make e_ddi_hold_devi public, we should be able to reuse
5426*0Sstevel@tonic-gate  * ddi_hold_devi again.
5427*0Sstevel@tonic-gate  */
5428*0Sstevel@tonic-gate dev_info_t *
5429*0Sstevel@tonic-gate ddi_hold_devi_by_instance(major_t major, int instance, int flags)
5430*0Sstevel@tonic-gate {
5431*0Sstevel@tonic-gate 	return (hold_devi(major, instance, flags));
5432*0Sstevel@tonic-gate }
5433*0Sstevel@tonic-gate 
5434*0Sstevel@tonic-gate dev_info_t *
5435*0Sstevel@tonic-gate e_ddi_hold_devi_by_dev(dev_t dev, int flags)
5436*0Sstevel@tonic-gate {
5437*0Sstevel@tonic-gate 	major_t	major = getmajor(dev);
5438*0Sstevel@tonic-gate 	dev_info_t	*dip;
5439*0Sstevel@tonic-gate 	struct dev_ops	*ops;
5440*0Sstevel@tonic-gate 	dev_info_t	*ddip = NULL;
5441*0Sstevel@tonic-gate 
5442*0Sstevel@tonic-gate 	dip = hold_devi(major, dev_to_instance(dev), flags);
5443*0Sstevel@tonic-gate 
5444*0Sstevel@tonic-gate 	/*
5445*0Sstevel@tonic-gate 	 * The rest of this routine is legacy support for drivers that
5446*0Sstevel@tonic-gate 	 * have broken DDI_INFO_DEVT2INSTANCE implementations but may have
5447*0Sstevel@tonic-gate 	 * functional DDI_INFO_DEVT2DEVINFO implementations.  This code will
5448*0Sstevel@tonic-gate 	 * diagnose inconsistency and, for maximum compatibility with legacy
5449*0Sstevel@tonic-gate 	 * drivers, give preference to the drivers DDI_INFO_DEVT2DEVINFO
5450*0Sstevel@tonic-gate 	 * implementation over the above derived dip based the driver's
5451*0Sstevel@tonic-gate 	 * DDI_INFO_DEVT2INSTANCE implementation. This legacy support should
5452*0Sstevel@tonic-gate 	 * be removed when DDI_INFO_DEVT2DEVINFO is deprecated.
5453*0Sstevel@tonic-gate 	 *
5454*0Sstevel@tonic-gate 	 * NOTE: The following code has a race condition. DEVT2DEVINFO
5455*0Sstevel@tonic-gate 	 *	returns a dip which is not held. By the time we ref ddip,
5456*0Sstevel@tonic-gate 	 *	it could have been freed. The saving grace is that for
5457*0Sstevel@tonic-gate 	 *	most drivers, the dip returned from hold_devi() is the
5458*0Sstevel@tonic-gate 	 *	same one as the one returned by DEVT2DEVINFO, so we are
5459*0Sstevel@tonic-gate 	 *	safe for drivers with the correct getinfo(9e) impl.
5460*0Sstevel@tonic-gate 	 */
5461*0Sstevel@tonic-gate 	if (((ops = ddi_hold_driver(major)) != NULL) &&
5462*0Sstevel@tonic-gate 	    CB_DRV_INSTALLED(ops) && ops->devo_getinfo)  {
5463*0Sstevel@tonic-gate 		if ((*ops->devo_getinfo)(NULL, DDI_INFO_DEVT2DEVINFO,
5464*0Sstevel@tonic-gate 		    (void *)dev, (void **)&ddip) != DDI_SUCCESS)
5465*0Sstevel@tonic-gate 			ddip = NULL;
5466*0Sstevel@tonic-gate 	}
5467*0Sstevel@tonic-gate 
5468*0Sstevel@tonic-gate 	/* give preference to the driver returned DEVT2DEVINFO dip */
5469*0Sstevel@tonic-gate 	if (ddip && (dip != ddip)) {
5470*0Sstevel@tonic-gate #ifdef	DEBUG
5471*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: inconsistent getinfo(9E) implementation",
5472*0Sstevel@tonic-gate 		    ddi_driver_name(ddip));
5473*0Sstevel@tonic-gate #endif	/* DEBUG */
5474*0Sstevel@tonic-gate 		ndi_hold_devi(ddip);
5475*0Sstevel@tonic-gate 		if (dip)
5476*0Sstevel@tonic-gate 			ndi_rele_devi(dip);
5477*0Sstevel@tonic-gate 		dip = ddip;
5478*0Sstevel@tonic-gate 	}
5479*0Sstevel@tonic-gate 
5480*0Sstevel@tonic-gate 	if (ops)
5481*0Sstevel@tonic-gate 		ddi_rele_driver(major);
5482*0Sstevel@tonic-gate 
5483*0Sstevel@tonic-gate 	return (dip);
5484*0Sstevel@tonic-gate }
5485*0Sstevel@tonic-gate 
5486*0Sstevel@tonic-gate /*
5487*0Sstevel@tonic-gate  * For compatibility only. Do not call this function!
5488*0Sstevel@tonic-gate  */
5489*0Sstevel@tonic-gate dev_info_t *
5490*0Sstevel@tonic-gate e_ddi_get_dev_info(dev_t dev, vtype_t type)
5491*0Sstevel@tonic-gate {
5492*0Sstevel@tonic-gate 	dev_info_t *dip = NULL;
5493*0Sstevel@tonic-gate 	if (getmajor(dev) >= devcnt)
5494*0Sstevel@tonic-gate 		return (NULL);
5495*0Sstevel@tonic-gate 
5496*0Sstevel@tonic-gate 	switch (type) {
5497*0Sstevel@tonic-gate 	case VCHR:
5498*0Sstevel@tonic-gate 	case VBLK:
5499*0Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
5500*0Sstevel@tonic-gate 	default:
5501*0Sstevel@tonic-gate 		break;
5502*0Sstevel@tonic-gate 	}
5503*0Sstevel@tonic-gate 
5504*0Sstevel@tonic-gate 	/*
5505*0Sstevel@tonic-gate 	 * For compatibility reasons, we can only return the dip with
5506*0Sstevel@tonic-gate 	 * the driver ref count held. This is not a safe thing to do.
5507*0Sstevel@tonic-gate 	 * For certain broken third-party software, we are willing
5508*0Sstevel@tonic-gate 	 * to venture into unknown territory.
5509*0Sstevel@tonic-gate 	 */
5510*0Sstevel@tonic-gate 	if (dip) {
5511*0Sstevel@tonic-gate 		(void) ndi_hold_driver(dip);
5512*0Sstevel@tonic-gate 		ndi_rele_devi(dip);
5513*0Sstevel@tonic-gate 	}
5514*0Sstevel@tonic-gate 	return (dip);
5515*0Sstevel@tonic-gate }
5516*0Sstevel@tonic-gate 
5517*0Sstevel@tonic-gate dev_info_t *
5518*0Sstevel@tonic-gate e_ddi_hold_devi_by_path(char *path, int flags)
5519*0Sstevel@tonic-gate {
5520*0Sstevel@tonic-gate 	dev_info_t	*dip;
5521*0Sstevel@tonic-gate 
5522*0Sstevel@tonic-gate 	/* can't specify NOATTACH by path */
5523*0Sstevel@tonic-gate 	ASSERT(!(flags & E_DDI_HOLD_DEVI_NOATTACH));
5524*0Sstevel@tonic-gate 
5525*0Sstevel@tonic-gate 	return (resolve_pathname(path, &dip, NULL, NULL) ? NULL : dip);
5526*0Sstevel@tonic-gate }
5527*0Sstevel@tonic-gate 
5528*0Sstevel@tonic-gate void
5529*0Sstevel@tonic-gate e_ddi_hold_devi(dev_info_t *dip)
5530*0Sstevel@tonic-gate {
5531*0Sstevel@tonic-gate 	ndi_hold_devi(dip);
5532*0Sstevel@tonic-gate }
5533*0Sstevel@tonic-gate 
5534*0Sstevel@tonic-gate void
5535*0Sstevel@tonic-gate ddi_release_devi(dev_info_t *dip)
5536*0Sstevel@tonic-gate {
5537*0Sstevel@tonic-gate 	ndi_rele_devi(dip);
5538*0Sstevel@tonic-gate }
5539*0Sstevel@tonic-gate 
5540*0Sstevel@tonic-gate /*
5541*0Sstevel@tonic-gate  * Associate a streams queue with a devinfo node
5542*0Sstevel@tonic-gate  * NOTE: This function is called by STREAM driver's put procedure.
5543*0Sstevel@tonic-gate  *	It cannot block.
5544*0Sstevel@tonic-gate  */
5545*0Sstevel@tonic-gate void
5546*0Sstevel@tonic-gate ddi_assoc_queue_with_devi(queue_t *q, dev_info_t *dip)
5547*0Sstevel@tonic-gate {
5548*0Sstevel@tonic-gate 	queue_t *rq = _RD(q);
5549*0Sstevel@tonic-gate 	struct stdata *stp;
5550*0Sstevel@tonic-gate 	vnode_t *vp;
5551*0Sstevel@tonic-gate 
5552*0Sstevel@tonic-gate 	/* set flag indicating that ddi_assoc_queue_with_devi was called */
5553*0Sstevel@tonic-gate 	mutex_enter(QLOCK(rq));
5554*0Sstevel@tonic-gate 	rq->q_flag |= _QASSOCIATED;
5555*0Sstevel@tonic-gate 	mutex_exit(QLOCK(rq));
5556*0Sstevel@tonic-gate 
5557*0Sstevel@tonic-gate 	/* get the vnode associated with the queue */
5558*0Sstevel@tonic-gate 	stp = STREAM(rq);
5559*0Sstevel@tonic-gate 	vp = stp->sd_vnode;
5560*0Sstevel@tonic-gate 	ASSERT(vp);
5561*0Sstevel@tonic-gate 
5562*0Sstevel@tonic-gate 	/* change the hardware association of the vnode */
5563*0Sstevel@tonic-gate 	spec_assoc_vp_with_devi(vp, dip);
5564*0Sstevel@tonic-gate }
5565*0Sstevel@tonic-gate 
5566*0Sstevel@tonic-gate /*
5567*0Sstevel@tonic-gate  * ddi_install_driver(name)
5568*0Sstevel@tonic-gate  *
5569*0Sstevel@tonic-gate  * Driver installation is currently a byproduct of driver loading.  This
5570*0Sstevel@tonic-gate  * may change.
5571*0Sstevel@tonic-gate  */
5572*0Sstevel@tonic-gate int
5573*0Sstevel@tonic-gate ddi_install_driver(char *name)
5574*0Sstevel@tonic-gate {
5575*0Sstevel@tonic-gate 	major_t major = ddi_name_to_major(name);
5576*0Sstevel@tonic-gate 
5577*0Sstevel@tonic-gate 	if ((major == (major_t)-1) ||
5578*0Sstevel@tonic-gate 	    (ddi_hold_installed_driver(major) == NULL)) {
5579*0Sstevel@tonic-gate 		return (DDI_FAILURE);
5580*0Sstevel@tonic-gate 	}
5581*0Sstevel@tonic-gate 	ddi_rele_driver(major);
5582*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
5583*0Sstevel@tonic-gate }
5584*0Sstevel@tonic-gate 
5585*0Sstevel@tonic-gate struct dev_ops *
5586*0Sstevel@tonic-gate ddi_hold_driver(major_t major)
5587*0Sstevel@tonic-gate {
5588*0Sstevel@tonic-gate 	return (mod_hold_dev_by_major(major));
5589*0Sstevel@tonic-gate }
5590*0Sstevel@tonic-gate 
5591*0Sstevel@tonic-gate 
5592*0Sstevel@tonic-gate void
5593*0Sstevel@tonic-gate ddi_rele_driver(major_t major)
5594*0Sstevel@tonic-gate {
5595*0Sstevel@tonic-gate 	mod_rele_dev_by_major(major);
5596*0Sstevel@tonic-gate }
5597*0Sstevel@tonic-gate 
5598*0Sstevel@tonic-gate 
5599*0Sstevel@tonic-gate /*
5600*0Sstevel@tonic-gate  * This is called during boot to force attachment order of special dips
5601*0Sstevel@tonic-gate  * dip must be referenced via ndi_hold_devi()
5602*0Sstevel@tonic-gate  */
5603*0Sstevel@tonic-gate int
5604*0Sstevel@tonic-gate i_ddi_attach_node_hierarchy(dev_info_t *dip)
5605*0Sstevel@tonic-gate {
5606*0Sstevel@tonic-gate 	dev_info_t *parent;
5607*0Sstevel@tonic-gate 
5608*0Sstevel@tonic-gate 	if (i_ddi_node_state(dip) == DS_READY)
5609*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
5610*0Sstevel@tonic-gate 
5611*0Sstevel@tonic-gate 	/*
5612*0Sstevel@tonic-gate 	 * Attach parent dip
5613*0Sstevel@tonic-gate 	 */
5614*0Sstevel@tonic-gate 	parent = ddi_get_parent(dip);
5615*0Sstevel@tonic-gate 	if (i_ddi_attach_node_hierarchy(parent) != DDI_SUCCESS)
5616*0Sstevel@tonic-gate 		return (DDI_FAILURE);
5617*0Sstevel@tonic-gate 
5618*0Sstevel@tonic-gate 	/*
5619*0Sstevel@tonic-gate 	 * Expand .conf nodes under this parent
5620*0Sstevel@tonic-gate 	 */
5621*0Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(parent, 0);
5622*0Sstevel@tonic-gate 	return (i_ddi_attachchild(dip));
5623*0Sstevel@tonic-gate }
5624*0Sstevel@tonic-gate 
5625*0Sstevel@tonic-gate /* keep this function static */
5626*0Sstevel@tonic-gate static int
5627*0Sstevel@tonic-gate attach_driver_nodes(major_t major)
5628*0Sstevel@tonic-gate {
5629*0Sstevel@tonic-gate 	struct devnames *dnp;
5630*0Sstevel@tonic-gate 	dev_info_t *dip;
5631*0Sstevel@tonic-gate 	int error = DDI_FAILURE;
5632*0Sstevel@tonic-gate 
5633*0Sstevel@tonic-gate 	dnp = &devnamesp[major];
5634*0Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
5635*0Sstevel@tonic-gate 	dip = dnp->dn_head;
5636*0Sstevel@tonic-gate 	while (dip) {
5637*0Sstevel@tonic-gate 		ndi_hold_devi(dip);
5638*0Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
5639*0Sstevel@tonic-gate 		if (i_ddi_attach_node_hierarchy(dip) == DDI_SUCCESS)
5640*0Sstevel@tonic-gate 			error = DDI_SUCCESS;
5641*0Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
5642*0Sstevel@tonic-gate 		ndi_rele_devi(dip);
5643*0Sstevel@tonic-gate 		dip = ddi_get_next(dip);
5644*0Sstevel@tonic-gate 	}
5645*0Sstevel@tonic-gate 	if (error == DDI_SUCCESS)
5646*0Sstevel@tonic-gate 		dnp->dn_flags |= DN_NO_AUTODETACH;
5647*0Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
5648*0Sstevel@tonic-gate 
5649*0Sstevel@tonic-gate 
5650*0Sstevel@tonic-gate 	return (error);
5651*0Sstevel@tonic-gate }
5652*0Sstevel@tonic-gate 
5653*0Sstevel@tonic-gate /*
5654*0Sstevel@tonic-gate  * i_ddi_attach_hw_nodes configures and attaches all hw nodes
5655*0Sstevel@tonic-gate  * bound to a specific driver. This function replaces calls to
5656*0Sstevel@tonic-gate  * ddi_hold_installed_driver() for drivers with no .conf
5657*0Sstevel@tonic-gate  * enumerated nodes.
5658*0Sstevel@tonic-gate  *
5659*0Sstevel@tonic-gate  * This facility is typically called at boot time to attach
5660*0Sstevel@tonic-gate  * platform-specific hardware nodes, such as ppm nodes on xcal
5661*0Sstevel@tonic-gate  * and grover and keyswitch nodes on cherrystone. It does not
5662*0Sstevel@tonic-gate  * deal with .conf enumerated node. Calling it beyond the boot
5663*0Sstevel@tonic-gate  * process is strongly discouraged.
5664*0Sstevel@tonic-gate  */
5665*0Sstevel@tonic-gate int
5666*0Sstevel@tonic-gate i_ddi_attach_hw_nodes(char *driver)
5667*0Sstevel@tonic-gate {
5668*0Sstevel@tonic-gate 	major_t major;
5669*0Sstevel@tonic-gate 
5670*0Sstevel@tonic-gate 	major = ddi_name_to_major(driver);
5671*0Sstevel@tonic-gate 	if (major == (major_t)-1)
5672*0Sstevel@tonic-gate 		return (DDI_FAILURE);
5673*0Sstevel@tonic-gate 
5674*0Sstevel@tonic-gate 	return (attach_driver_nodes(major));
5675*0Sstevel@tonic-gate }
5676*0Sstevel@tonic-gate 
5677*0Sstevel@tonic-gate /*
5678*0Sstevel@tonic-gate  * i_ddi_attach_pseudo_node configures pseudo drivers which
5679*0Sstevel@tonic-gate  * has a single node. The .conf nodes must be enumerated
5680*0Sstevel@tonic-gate  * before calling this interface. The dip is held attached
5681*0Sstevel@tonic-gate  * upon returning.
5682*0Sstevel@tonic-gate  *
5683*0Sstevel@tonic-gate  * This facility should only be called only at boot time
5684*0Sstevel@tonic-gate  * by the I/O framework.
5685*0Sstevel@tonic-gate  */
5686*0Sstevel@tonic-gate dev_info_t *
5687*0Sstevel@tonic-gate i_ddi_attach_pseudo_node(char *driver)
5688*0Sstevel@tonic-gate {
5689*0Sstevel@tonic-gate 	major_t major;
5690*0Sstevel@tonic-gate 	dev_info_t *dip;
5691*0Sstevel@tonic-gate 
5692*0Sstevel@tonic-gate 	major = ddi_name_to_major(driver);
5693*0Sstevel@tonic-gate 	if (major == (major_t)-1)
5694*0Sstevel@tonic-gate 		return (NULL);
5695*0Sstevel@tonic-gate 
5696*0Sstevel@tonic-gate 	if (attach_driver_nodes(major) != DDI_SUCCESS)
5697*0Sstevel@tonic-gate 		return (NULL);
5698*0Sstevel@tonic-gate 
5699*0Sstevel@tonic-gate 	dip = devnamesp[major].dn_head;
5700*0Sstevel@tonic-gate 	ASSERT(dip && ddi_get_next(dip) == NULL);
5701*0Sstevel@tonic-gate 	ndi_hold_devi(dip);
5702*0Sstevel@tonic-gate 	return (dip);
5703*0Sstevel@tonic-gate }
5704*0Sstevel@tonic-gate 
5705*0Sstevel@tonic-gate static void
5706*0Sstevel@tonic-gate diplist_to_parent_major(dev_info_t *head, char parents[])
5707*0Sstevel@tonic-gate {
5708*0Sstevel@tonic-gate 	major_t major;
5709*0Sstevel@tonic-gate 	dev_info_t *dip, *pdip;
5710*0Sstevel@tonic-gate 
5711*0Sstevel@tonic-gate 	for (dip = head; dip != NULL; dip = ddi_get_next(dip)) {
5712*0Sstevel@tonic-gate 		pdip = ddi_get_parent(dip);
5713*0Sstevel@tonic-gate 		ASSERT(pdip);	/* disallow rootnex.conf nodes */
5714*0Sstevel@tonic-gate 		major = ddi_driver_major(pdip);
5715*0Sstevel@tonic-gate 		if ((major != (major_t)-1) && parents[major] == 0)
5716*0Sstevel@tonic-gate 			parents[major] = 1;
5717*0Sstevel@tonic-gate 	}
5718*0Sstevel@tonic-gate }
5719*0Sstevel@tonic-gate 
5720*0Sstevel@tonic-gate /*
5721*0Sstevel@tonic-gate  * Call ddi_hold_installed_driver() on each parent major
5722*0Sstevel@tonic-gate  * and invoke mt_config_driver() to attach child major.
5723*0Sstevel@tonic-gate  * This is part of the implementation of ddi_hold_installed_driver.
5724*0Sstevel@tonic-gate  */
5725*0Sstevel@tonic-gate static int
5726*0Sstevel@tonic-gate attach_driver_by_parent(major_t child_major, char parents[])
5727*0Sstevel@tonic-gate {
5728*0Sstevel@tonic-gate 	major_t par_major;
5729*0Sstevel@tonic-gate 	struct mt_config_handle *hdl;
5730*0Sstevel@tonic-gate 	int flags = NDI_DEVI_PERSIST | NDI_NO_EVENT;
5731*0Sstevel@tonic-gate 
5732*0Sstevel@tonic-gate 	hdl = mt_config_init(NULL, NULL, flags, child_major, MT_CONFIG_OP,
5733*0Sstevel@tonic-gate 	    NULL);
5734*0Sstevel@tonic-gate 	for (par_major = 0; par_major < devcnt; par_major++) {
5735*0Sstevel@tonic-gate 		/* disallow recursion on the same driver */
5736*0Sstevel@tonic-gate 		if (parents[par_major] == 0 || par_major == child_major)
5737*0Sstevel@tonic-gate 			continue;
5738*0Sstevel@tonic-gate 		if (ddi_hold_installed_driver(par_major) == NULL)
5739*0Sstevel@tonic-gate 			continue;
5740*0Sstevel@tonic-gate 		hdl->mtc_parmajor = par_major;
5741*0Sstevel@tonic-gate 		mt_config_driver(hdl);
5742*0Sstevel@tonic-gate 		ddi_rele_driver(par_major);
5743*0Sstevel@tonic-gate 	}
5744*0Sstevel@tonic-gate 	(void) mt_config_fini(hdl);
5745*0Sstevel@tonic-gate 
5746*0Sstevel@tonic-gate 	return (i_ddi_devs_attached(child_major));
5747*0Sstevel@tonic-gate }
5748*0Sstevel@tonic-gate 
5749*0Sstevel@tonic-gate int
5750*0Sstevel@tonic-gate i_ddi_devs_attached(major_t major)
5751*0Sstevel@tonic-gate {
5752*0Sstevel@tonic-gate 	dev_info_t *dip;
5753*0Sstevel@tonic-gate 	struct devnames *dnp;
5754*0Sstevel@tonic-gate 	int error = DDI_FAILURE;
5755*0Sstevel@tonic-gate 
5756*0Sstevel@tonic-gate 	/* check for attached instances */
5757*0Sstevel@tonic-gate 	dnp = &devnamesp[major];
5758*0Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
5759*0Sstevel@tonic-gate 	for (dip = dnp->dn_head; dip != NULL; dip = ddi_get_next(dip)) {
5760*0Sstevel@tonic-gate 		if (i_ddi_node_state(dip) >= DS_ATTACHED) {
5761*0Sstevel@tonic-gate 			error = DDI_SUCCESS;
5762*0Sstevel@tonic-gate 			break;
5763*0Sstevel@tonic-gate 		}
5764*0Sstevel@tonic-gate 	}
5765*0Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
5766*0Sstevel@tonic-gate 
5767*0Sstevel@tonic-gate 	return (error);
5768*0Sstevel@tonic-gate }
5769*0Sstevel@tonic-gate 
5770*0Sstevel@tonic-gate /*
5771*0Sstevel@tonic-gate  * ddi_hold_installed_driver configures and attaches all
5772*0Sstevel@tonic-gate  * instances of the specified driver. To accomplish this
5773*0Sstevel@tonic-gate  * it configures and attaches all possible parents of
5774*0Sstevel@tonic-gate  * the driver, enumerated both in h/w nodes and in the
5775*0Sstevel@tonic-gate  * driver's .conf file.
5776*0Sstevel@tonic-gate  *
5777*0Sstevel@tonic-gate  * NOTE: This facility is for compatibility purposes only and will
5778*0Sstevel@tonic-gate  *	eventually go away. Its usage is strongly discouraged.
5779*0Sstevel@tonic-gate  */
5780*0Sstevel@tonic-gate static void
5781*0Sstevel@tonic-gate enter_driver(struct devnames *dnp)
5782*0Sstevel@tonic-gate {
5783*0Sstevel@tonic-gate 	mutex_enter(&dnp->dn_lock);
5784*0Sstevel@tonic-gate 	ASSERT(dnp->dn_busy_thread != curthread);
5785*0Sstevel@tonic-gate 	while (dnp->dn_flags & DN_DRIVER_BUSY)
5786*0Sstevel@tonic-gate 		cv_wait(&dnp->dn_wait, &dnp->dn_lock);
5787*0Sstevel@tonic-gate 	dnp->dn_flags |= DN_DRIVER_BUSY;
5788*0Sstevel@tonic-gate 	dnp->dn_busy_thread = curthread;
5789*0Sstevel@tonic-gate 	mutex_exit(&dnp->dn_lock);
5790*0Sstevel@tonic-gate }
5791*0Sstevel@tonic-gate 
5792*0Sstevel@tonic-gate static void
5793*0Sstevel@tonic-gate exit_driver(struct devnames *dnp)
5794*0Sstevel@tonic-gate {
5795*0Sstevel@tonic-gate 	mutex_enter(&dnp->dn_lock);
5796*0Sstevel@tonic-gate 	ASSERT(dnp->dn_busy_thread == curthread);
5797*0Sstevel@tonic-gate 	dnp->dn_flags &= ~DN_DRIVER_BUSY;
5798*0Sstevel@tonic-gate 	dnp->dn_busy_thread = NULL;
5799*0Sstevel@tonic-gate 	cv_broadcast(&dnp->dn_wait);
5800*0Sstevel@tonic-gate 	mutex_exit(&dnp->dn_lock);
5801*0Sstevel@tonic-gate }
5802*0Sstevel@tonic-gate 
5803*0Sstevel@tonic-gate struct dev_ops *
5804*0Sstevel@tonic-gate ddi_hold_installed_driver(major_t major)
5805*0Sstevel@tonic-gate {
5806*0Sstevel@tonic-gate 	struct dev_ops *ops;
5807*0Sstevel@tonic-gate 	struct devnames *dnp;
5808*0Sstevel@tonic-gate 	char *parents;
5809*0Sstevel@tonic-gate 	int error;
5810*0Sstevel@tonic-gate 
5811*0Sstevel@tonic-gate 	ops = ddi_hold_driver(major);
5812*0Sstevel@tonic-gate 	if (ops == NULL)
5813*0Sstevel@tonic-gate 		return (NULL);
5814*0Sstevel@tonic-gate 
5815*0Sstevel@tonic-gate 	/*
5816*0Sstevel@tonic-gate 	 * Return immediately if all the attach operations associated
5817*0Sstevel@tonic-gate 	 * with a ddi_hold_installed_driver() call have already been done.
5818*0Sstevel@tonic-gate 	 */
5819*0Sstevel@tonic-gate 	dnp = &devnamesp[major];
5820*0Sstevel@tonic-gate 	enter_driver(dnp);
5821*0Sstevel@tonic-gate 	if (dnp->dn_flags & DN_DRIVER_HELD) {
5822*0Sstevel@tonic-gate 		exit_driver(dnp);
5823*0Sstevel@tonic-gate 		if (i_ddi_devs_attached(major) == DDI_SUCCESS)
5824*0Sstevel@tonic-gate 			return (ops);
5825*0Sstevel@tonic-gate 		ddi_rele_driver(major);
5826*0Sstevel@tonic-gate 		return (NULL);
5827*0Sstevel@tonic-gate 	}
5828*0Sstevel@tonic-gate 
5829*0Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
5830*0Sstevel@tonic-gate 	dnp->dn_flags |= (DN_DRIVER_HELD | DN_NO_AUTODETACH);
5831*0Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
5832*0Sstevel@tonic-gate 
5833*0Sstevel@tonic-gate 	DCOMPATPRINTF((CE_CONT,
5834*0Sstevel@tonic-gate 	    "ddi_hold_installed_driver: %s\n", dnp->dn_name));
5835*0Sstevel@tonic-gate 
5836*0Sstevel@tonic-gate 	/*
5837*0Sstevel@tonic-gate 	 * When the driver has no .conf children, it is sufficient
5838*0Sstevel@tonic-gate 	 * to attach existing nodes in the device tree. Nodes not
5839*0Sstevel@tonic-gate 	 * enumerated by the OBP are not attached.
5840*0Sstevel@tonic-gate 	 */
5841*0Sstevel@tonic-gate 	if (dnp->dn_pl == NULL) {
5842*0Sstevel@tonic-gate 		if (attach_driver_nodes(major) == DDI_SUCCESS) {
5843*0Sstevel@tonic-gate 			exit_driver(dnp);
5844*0Sstevel@tonic-gate 			return (ops);
5845*0Sstevel@tonic-gate 		}
5846*0Sstevel@tonic-gate 		exit_driver(dnp);
5847*0Sstevel@tonic-gate 		ddi_rele_driver(major);
5848*0Sstevel@tonic-gate 		return (NULL);
5849*0Sstevel@tonic-gate 	}
5850*0Sstevel@tonic-gate 
5851*0Sstevel@tonic-gate 	/*
5852*0Sstevel@tonic-gate 	 * Driver has .conf nodes. We find all possible parents
5853*0Sstevel@tonic-gate 	 * and recursively all ddi_hold_installed_driver on the
5854*0Sstevel@tonic-gate 	 * parent driver; then we invoke ndi_config_driver()
5855*0Sstevel@tonic-gate 	 * on all possible parent node in parallel to speed up
5856*0Sstevel@tonic-gate 	 * performance.
5857*0Sstevel@tonic-gate 	 */
5858*0Sstevel@tonic-gate 	parents = kmem_zalloc(devcnt * sizeof (char), KM_SLEEP);
5859*0Sstevel@tonic-gate 
5860*0Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
5861*0Sstevel@tonic-gate 	/* find .conf parents */
5862*0Sstevel@tonic-gate 	(void) impl_parlist_to_major(dnp->dn_pl, parents);
5863*0Sstevel@tonic-gate 	/* find hw node parents */
5864*0Sstevel@tonic-gate 	diplist_to_parent_major(dnp->dn_head, parents);
5865*0Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
5866*0Sstevel@tonic-gate 
5867*0Sstevel@tonic-gate 	error = attach_driver_by_parent(major, parents);
5868*0Sstevel@tonic-gate 	kmem_free(parents, devcnt * sizeof (char));
5869*0Sstevel@tonic-gate 	if (error == DDI_SUCCESS) {
5870*0Sstevel@tonic-gate 		exit_driver(dnp);
5871*0Sstevel@tonic-gate 		return (ops);
5872*0Sstevel@tonic-gate 	}
5873*0Sstevel@tonic-gate 
5874*0Sstevel@tonic-gate 	exit_driver(dnp);
5875*0Sstevel@tonic-gate 	ddi_rele_driver(major);
5876*0Sstevel@tonic-gate 	return (NULL);
5877*0Sstevel@tonic-gate }
5878*0Sstevel@tonic-gate 
5879*0Sstevel@tonic-gate /*
5880*0Sstevel@tonic-gate  * Default bus_config entry point for nexus drivers
5881*0Sstevel@tonic-gate  */
5882*0Sstevel@tonic-gate int
5883*0Sstevel@tonic-gate ndi_busop_bus_config(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
5884*0Sstevel@tonic-gate     void *arg, dev_info_t **child, clock_t timeout)
5885*0Sstevel@tonic-gate {
5886*0Sstevel@tonic-gate 	major_t major;
5887*0Sstevel@tonic-gate 
5888*0Sstevel@tonic-gate 	/*
5889*0Sstevel@tonic-gate 	 * A timeout of 30 minutes or more is probably a mistake
5890*0Sstevel@tonic-gate 	 * This is intended to catch uses where timeout is in
5891*0Sstevel@tonic-gate 	 * the wrong units.  timeout must be in units of ticks.
5892*0Sstevel@tonic-gate 	 */
5893*0Sstevel@tonic-gate 	ASSERT(timeout < SEC_TO_TICK(1800));
5894*0Sstevel@tonic-gate 
5895*0Sstevel@tonic-gate 	major = (major_t)-1;
5896*0Sstevel@tonic-gate 	switch (op) {
5897*0Sstevel@tonic-gate 	case BUS_CONFIG_ONE:
5898*0Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config %s timeout=%ld\n",
5899*0Sstevel@tonic-gate 			ddi_driver_name(pdip), ddi_get_instance(pdip),
5900*0Sstevel@tonic-gate 			(char *)arg, timeout));
5901*0Sstevel@tonic-gate 		return (devi_config_one(pdip, (char *)arg, child, flags,
5902*0Sstevel@tonic-gate 		    timeout));
5903*0Sstevel@tonic-gate 
5904*0Sstevel@tonic-gate 	case BUS_CONFIG_DRIVER:
5905*0Sstevel@tonic-gate 		major = (major_t)(uintptr_t)arg;
5906*0Sstevel@tonic-gate 		/*FALLTHROUGH*/
5907*0Sstevel@tonic-gate 	case BUS_CONFIG_ALL:
5908*0Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config timeout=%ld\n",
5909*0Sstevel@tonic-gate 			ddi_driver_name(pdip), ddi_get_instance(pdip),
5910*0Sstevel@tonic-gate 			timeout));
5911*0Sstevel@tonic-gate 		if (timeout > 0) {
5912*0Sstevel@tonic-gate 			NDI_DEBUG(flags, (CE_CONT,
5913*0Sstevel@tonic-gate 			    "%s%d: bus config all timeout=%ld\n",
5914*0Sstevel@tonic-gate 			    ddi_driver_name(pdip), ddi_get_instance(pdip),
5915*0Sstevel@tonic-gate 			    timeout));
5916*0Sstevel@tonic-gate 			delay(timeout);
5917*0Sstevel@tonic-gate 		}
5918*0Sstevel@tonic-gate 		return (config_immediate_children(pdip, flags, major));
5919*0Sstevel@tonic-gate 
5920*0Sstevel@tonic-gate 	default:
5921*0Sstevel@tonic-gate 		return (NDI_FAILURE);
5922*0Sstevel@tonic-gate 	}
5923*0Sstevel@tonic-gate 	/*NOTREACHED*/
5924*0Sstevel@tonic-gate }
5925*0Sstevel@tonic-gate 
5926*0Sstevel@tonic-gate /*
5927*0Sstevel@tonic-gate  * Default busop bus_unconfig handler for nexus drivers
5928*0Sstevel@tonic-gate  */
5929*0Sstevel@tonic-gate int
5930*0Sstevel@tonic-gate ndi_busop_bus_unconfig(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
5931*0Sstevel@tonic-gate     void *arg)
5932*0Sstevel@tonic-gate {
5933*0Sstevel@tonic-gate 	major_t major;
5934*0Sstevel@tonic-gate 
5935*0Sstevel@tonic-gate 	major = (major_t)-1;
5936*0Sstevel@tonic-gate 	switch (op) {
5937*0Sstevel@tonic-gate 	case BUS_UNCONFIG_ONE:
5938*0Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig %s\n",
5939*0Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
5940*0Sstevel@tonic-gate 		    (char *)arg));
5941*0Sstevel@tonic-gate 		return (devi_unconfig_one(pdip, (char *)arg, flags));
5942*0Sstevel@tonic-gate 
5943*0Sstevel@tonic-gate 	case BUS_UNCONFIG_DRIVER:
5944*0Sstevel@tonic-gate 		major = (major_t)(uintptr_t)arg;
5945*0Sstevel@tonic-gate 		/*FALLTHROUGH*/
5946*0Sstevel@tonic-gate 	case BUS_UNCONFIG_ALL:
5947*0Sstevel@tonic-gate 		NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig all\n",
5948*0Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip)));
5949*0Sstevel@tonic-gate 		return (unconfig_immediate_children(pdip, NULL, flags, major));
5950*0Sstevel@tonic-gate 
5951*0Sstevel@tonic-gate 	default:
5952*0Sstevel@tonic-gate 		return (NDI_FAILURE);
5953*0Sstevel@tonic-gate 	}
5954*0Sstevel@tonic-gate 	/*NOTREACHED*/
5955*0Sstevel@tonic-gate }
5956*0Sstevel@tonic-gate 
5957*0Sstevel@tonic-gate /*
5958*0Sstevel@tonic-gate  * dummy functions to be removed
5959*0Sstevel@tonic-gate  */
5960*0Sstevel@tonic-gate void
5961*0Sstevel@tonic-gate impl_rem_dev_props(dev_info_t *dip)
5962*0Sstevel@tonic-gate {
5963*0Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip))
5964*0Sstevel@tonic-gate 	/* do nothing */
5965*0Sstevel@tonic-gate }
5966*0Sstevel@tonic-gate 
5967*0Sstevel@tonic-gate /*
5968*0Sstevel@tonic-gate  * Determine if a node is a leaf node. If not sure, return false (0).
5969*0Sstevel@tonic-gate  */
5970*0Sstevel@tonic-gate static int
5971*0Sstevel@tonic-gate is_leaf_node(dev_info_t *dip)
5972*0Sstevel@tonic-gate {
5973*0Sstevel@tonic-gate 	major_t major = ddi_driver_major(dip);
5974*0Sstevel@tonic-gate 
5975*0Sstevel@tonic-gate 	if (major == (major_t)-1)
5976*0Sstevel@tonic-gate 		return (0);
5977*0Sstevel@tonic-gate 
5978*0Sstevel@tonic-gate 	return (devnamesp[major].dn_flags & DN_LEAF_DRIVER);
5979*0Sstevel@tonic-gate }
5980*0Sstevel@tonic-gate 
5981*0Sstevel@tonic-gate /*
5982*0Sstevel@tonic-gate  * Multithreaded [un]configuration
5983*0Sstevel@tonic-gate  */
5984*0Sstevel@tonic-gate static struct mt_config_handle *
5985*0Sstevel@tonic-gate mt_config_init(dev_info_t *pdip, dev_info_t **dipp, int flags,
5986*0Sstevel@tonic-gate     major_t major, int op, struct brevq_node **brevqp)
5987*0Sstevel@tonic-gate {
5988*0Sstevel@tonic-gate 	struct mt_config_handle	*hdl = kmem_alloc(sizeof (*hdl), KM_SLEEP);
5989*0Sstevel@tonic-gate 
5990*0Sstevel@tonic-gate 	mutex_init(&hdl->mtc_lock, NULL, MUTEX_DEFAULT, NULL);
5991*0Sstevel@tonic-gate 	cv_init(&hdl->mtc_cv, NULL, CV_DEFAULT, NULL);
5992*0Sstevel@tonic-gate 	hdl->mtc_pdip = pdip;
5993*0Sstevel@tonic-gate 	hdl->mtc_fdip = dipp;
5994*0Sstevel@tonic-gate 	hdl->mtc_parmajor = (major_t)-1;
5995*0Sstevel@tonic-gate 	hdl->mtc_flags = flags;
5996*0Sstevel@tonic-gate 	hdl->mtc_major = major;
5997*0Sstevel@tonic-gate 	hdl->mtc_thr_count = 0;
5998*0Sstevel@tonic-gate 	hdl->mtc_op = op;
5999*0Sstevel@tonic-gate 	hdl->mtc_error = 0;
6000*0Sstevel@tonic-gate 	hdl->mtc_brevqp = brevqp;
6001*0Sstevel@tonic-gate 
6002*0Sstevel@tonic-gate #ifdef DEBUG
6003*0Sstevel@tonic-gate 	gethrestime(&hdl->start_time);
6004*0Sstevel@tonic-gate 	hdl->total_time = 0;
6005*0Sstevel@tonic-gate #endif /* DEBUG */
6006*0Sstevel@tonic-gate 
6007*0Sstevel@tonic-gate 	return (hdl);
6008*0Sstevel@tonic-gate }
6009*0Sstevel@tonic-gate 
6010*0Sstevel@tonic-gate #ifdef DEBUG
6011*0Sstevel@tonic-gate static int
6012*0Sstevel@tonic-gate time_diff_in_msec(timestruc_t start, timestruc_t end)
6013*0Sstevel@tonic-gate {
6014*0Sstevel@tonic-gate 	int	nsec, sec;
6015*0Sstevel@tonic-gate 
6016*0Sstevel@tonic-gate 	sec = end.tv_sec - start.tv_sec;
6017*0Sstevel@tonic-gate 	nsec = end.tv_nsec - start.tv_nsec;
6018*0Sstevel@tonic-gate 	if (nsec < 0) {
6019*0Sstevel@tonic-gate 		nsec += NANOSEC;
6020*0Sstevel@tonic-gate 		sec -= 1;
6021*0Sstevel@tonic-gate 	}
6022*0Sstevel@tonic-gate 
6023*0Sstevel@tonic-gate 	return (sec * (NANOSEC >> 20) + (nsec >> 20));
6024*0Sstevel@tonic-gate }
6025*0Sstevel@tonic-gate 
6026*0Sstevel@tonic-gate #endif	/* DEBUG */
6027*0Sstevel@tonic-gate 
6028*0Sstevel@tonic-gate static int
6029*0Sstevel@tonic-gate mt_config_fini(struct mt_config_handle *hdl)
6030*0Sstevel@tonic-gate {
6031*0Sstevel@tonic-gate 	int		rv;
6032*0Sstevel@tonic-gate #ifdef DEBUG
6033*0Sstevel@tonic-gate 	int		real_time;
6034*0Sstevel@tonic-gate 	timestruc_t	end_time;
6035*0Sstevel@tonic-gate #endif /* DEBUG */
6036*0Sstevel@tonic-gate 
6037*0Sstevel@tonic-gate 	mutex_enter(&hdl->mtc_lock);
6038*0Sstevel@tonic-gate 	while (hdl->mtc_thr_count > 0)
6039*0Sstevel@tonic-gate 		cv_wait(&hdl->mtc_cv, &hdl->mtc_lock);
6040*0Sstevel@tonic-gate 	rv = hdl->mtc_error;
6041*0Sstevel@tonic-gate 	mutex_exit(&hdl->mtc_lock);
6042*0Sstevel@tonic-gate 
6043*0Sstevel@tonic-gate #ifdef DEBUG
6044*0Sstevel@tonic-gate 	gethrestime(&end_time);
6045*0Sstevel@tonic-gate 	real_time = time_diff_in_msec(hdl->start_time, end_time);
6046*0Sstevel@tonic-gate 	if ((ddidebug & DDI_MTCONFIG) && hdl->mtc_pdip)
6047*0Sstevel@tonic-gate 		cmn_err(CE_NOTE,
6048*0Sstevel@tonic-gate 		    "config %s%d: total time %d msec, real time %d msec",
6049*0Sstevel@tonic-gate 			ddi_driver_name(hdl->mtc_pdip),
6050*0Sstevel@tonic-gate 			ddi_get_instance(hdl->mtc_pdip),
6051*0Sstevel@tonic-gate 			hdl->total_time, real_time);
6052*0Sstevel@tonic-gate #endif /* DEBUG */
6053*0Sstevel@tonic-gate 
6054*0Sstevel@tonic-gate 	cv_destroy(&hdl->mtc_cv);
6055*0Sstevel@tonic-gate 	mutex_destroy(&hdl->mtc_lock);
6056*0Sstevel@tonic-gate 	kmem_free(hdl, sizeof (*hdl));
6057*0Sstevel@tonic-gate 
6058*0Sstevel@tonic-gate 	return (rv);
6059*0Sstevel@tonic-gate }
6060*0Sstevel@tonic-gate 
6061*0Sstevel@tonic-gate struct mt_config_data {
6062*0Sstevel@tonic-gate 	struct mt_config_handle	*mtc_hdl;
6063*0Sstevel@tonic-gate 	dev_info_t		*mtc_dip;
6064*0Sstevel@tonic-gate 	major_t			mtc_major;
6065*0Sstevel@tonic-gate 	int			mtc_flags;
6066*0Sstevel@tonic-gate 	struct brevq_node	*mtc_brn;
6067*0Sstevel@tonic-gate 	struct mt_config_data	*mtc_next;
6068*0Sstevel@tonic-gate };
6069*0Sstevel@tonic-gate 
6070*0Sstevel@tonic-gate static void
6071*0Sstevel@tonic-gate mt_config_thread(void *arg)
6072*0Sstevel@tonic-gate {
6073*0Sstevel@tonic-gate 	struct mt_config_data	*mcd = (struct mt_config_data *)arg;
6074*0Sstevel@tonic-gate 	struct mt_config_handle	*hdl = mcd->mtc_hdl;
6075*0Sstevel@tonic-gate 	dev_info_t		*dip = mcd->mtc_dip;
6076*0Sstevel@tonic-gate 	dev_info_t		*rdip, **dipp;
6077*0Sstevel@tonic-gate 	major_t			major = mcd->mtc_major;
6078*0Sstevel@tonic-gate 	int			flags = mcd->mtc_flags;
6079*0Sstevel@tonic-gate 	int			rv = 0;
6080*0Sstevel@tonic-gate 
6081*0Sstevel@tonic-gate #ifdef DEBUG
6082*0Sstevel@tonic-gate 	timestruc_t start_time, end_time;
6083*0Sstevel@tonic-gate 	gethrestime(&start_time);
6084*0Sstevel@tonic-gate #endif /* DEBUG */
6085*0Sstevel@tonic-gate 
6086*0Sstevel@tonic-gate 	rdip = NULL;
6087*0Sstevel@tonic-gate 	dipp = hdl->mtc_fdip ? &rdip : NULL;
6088*0Sstevel@tonic-gate 
6089*0Sstevel@tonic-gate 	switch (hdl->mtc_op) {
6090*0Sstevel@tonic-gate 	case MT_CONFIG_OP:
6091*0Sstevel@tonic-gate 		rv = devi_config_common(dip, flags, major);
6092*0Sstevel@tonic-gate 		break;
6093*0Sstevel@tonic-gate 	case MT_UNCONFIG_OP:
6094*0Sstevel@tonic-gate 		if (mcd->mtc_brn) {
6095*0Sstevel@tonic-gate 			struct brevq_node *brevq = NULL;
6096*0Sstevel@tonic-gate 			rv = devi_unconfig_common(dip, dipp, flags, major,
6097*0Sstevel@tonic-gate 			    &brevq);
6098*0Sstevel@tonic-gate 			mcd->mtc_brn->child = brevq;
6099*0Sstevel@tonic-gate 		} else
6100*0Sstevel@tonic-gate 			rv = devi_unconfig_common(dip, dipp, flags, major,
6101*0Sstevel@tonic-gate 			    NULL);
6102*0Sstevel@tonic-gate 		break;
6103*0Sstevel@tonic-gate 	}
6104*0Sstevel@tonic-gate 
6105*0Sstevel@tonic-gate 	mutex_enter(&hdl->mtc_lock);
6106*0Sstevel@tonic-gate #ifdef DEBUG
6107*0Sstevel@tonic-gate 	gethrestime(&end_time);
6108*0Sstevel@tonic-gate 	hdl->total_time += time_diff_in_msec(start_time, end_time);
6109*0Sstevel@tonic-gate #endif /* DEBUG */
6110*0Sstevel@tonic-gate 	if (rv != NDI_SUCCESS)
6111*0Sstevel@tonic-gate 		hdl->mtc_error = rv;
6112*0Sstevel@tonic-gate 	if (hdl->mtc_fdip && *hdl->mtc_fdip == NULL) {
6113*0Sstevel@tonic-gate 		*hdl->mtc_fdip = rdip;
6114*0Sstevel@tonic-gate 		rdip = NULL;
6115*0Sstevel@tonic-gate 	}
6116*0Sstevel@tonic-gate 
6117*0Sstevel@tonic-gate 	if (--hdl->mtc_thr_count == 0)
6118*0Sstevel@tonic-gate 		cv_broadcast(&hdl->mtc_cv);
6119*0Sstevel@tonic-gate 	mutex_exit(&hdl->mtc_lock);
6120*0Sstevel@tonic-gate 
6121*0Sstevel@tonic-gate 	if (rdip) {
6122*0Sstevel@tonic-gate 		ASSERT(rv != NDI_SUCCESS);
6123*0Sstevel@tonic-gate 		ndi_rele_devi(rdip);
6124*0Sstevel@tonic-gate 	}
6125*0Sstevel@tonic-gate 
6126*0Sstevel@tonic-gate 	ndi_rele_devi(dip);
6127*0Sstevel@tonic-gate 	kmem_free(mcd, sizeof (*mcd));
6128*0Sstevel@tonic-gate }
6129*0Sstevel@tonic-gate 
6130*0Sstevel@tonic-gate /*
6131*0Sstevel@tonic-gate  * Multi-threaded config/unconfig of child nexus
6132*0Sstevel@tonic-gate  */
6133*0Sstevel@tonic-gate static void
6134*0Sstevel@tonic-gate mt_config_children(struct mt_config_handle *hdl)
6135*0Sstevel@tonic-gate {
6136*0Sstevel@tonic-gate 	dev_info_t		*pdip = hdl->mtc_pdip;
6137*0Sstevel@tonic-gate 	major_t			major = hdl->mtc_major;
6138*0Sstevel@tonic-gate 	dev_info_t		*dip;
6139*0Sstevel@tonic-gate 	int			circ;
6140*0Sstevel@tonic-gate 	struct brevq_node	*brn = NULL;
6141*0Sstevel@tonic-gate 	struct mt_config_data	*mcd_head = NULL;
6142*0Sstevel@tonic-gate 	struct mt_config_data	*mcd_tail = NULL;
6143*0Sstevel@tonic-gate 	struct mt_config_data	*mcd;
6144*0Sstevel@tonic-gate #ifdef DEBUG
6145*0Sstevel@tonic-gate 	timestruc_t		end_time;
6146*0Sstevel@tonic-gate 
6147*0Sstevel@tonic-gate 	/* Update total_time in handle */
6148*0Sstevel@tonic-gate 	gethrestime(&end_time);
6149*0Sstevel@tonic-gate 	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
6150*0Sstevel@tonic-gate #endif
6151*0Sstevel@tonic-gate 
6152*0Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
6153*0Sstevel@tonic-gate 	dip = ddi_get_child(pdip);
6154*0Sstevel@tonic-gate 	while (dip) {
6155*0Sstevel@tonic-gate 		if (hdl->mtc_op == MT_UNCONFIG_OP && hdl->mtc_brevqp &&
6156*0Sstevel@tonic-gate 		    !(DEVI_EVREMOVE(dip)) &&
6157*0Sstevel@tonic-gate 		    i_ddi_node_state(dip) >= DS_INITIALIZED) {
6158*0Sstevel@tonic-gate 			/*
6159*0Sstevel@tonic-gate 			 * Enqueue this dip's deviname.
6160*0Sstevel@tonic-gate 			 * No need to hold a lock while enqueuing since this
6161*0Sstevel@tonic-gate 			 * is the only thread doing the enqueue and no one
6162*0Sstevel@tonic-gate 			 * walks the queue while we are in multithreaded
6163*0Sstevel@tonic-gate 			 * unconfiguration.
6164*0Sstevel@tonic-gate 			 */
6165*0Sstevel@tonic-gate 			brn = brevq_enqueue(hdl->mtc_brevqp, dip, NULL);
6166*0Sstevel@tonic-gate 		}
6167*0Sstevel@tonic-gate 
6168*0Sstevel@tonic-gate 		/*
6169*0Sstevel@tonic-gate 		 * Hold the child that we are processing so he does not get
6170*0Sstevel@tonic-gate 		 * removed. The corrisponding ndi_rele_devi() for children
6171*0Sstevel@tonic-gate 		 * that are not being skipped is done at the end of
6172*0Sstevel@tonic-gate 		 * mt_config_thread().
6173*0Sstevel@tonic-gate 		 */
6174*0Sstevel@tonic-gate 		ndi_hold_devi(dip);
6175*0Sstevel@tonic-gate 
6176*0Sstevel@tonic-gate 		/*
6177*0Sstevel@tonic-gate 		 * skip leaf nodes and (for configure) nodes not
6178*0Sstevel@tonic-gate 		 * fully attached.
6179*0Sstevel@tonic-gate 		 */
6180*0Sstevel@tonic-gate 		if (is_leaf_node(dip) ||
6181*0Sstevel@tonic-gate 		    (hdl->mtc_op == MT_CONFIG_OP &&
6182*0Sstevel@tonic-gate 		    i_ddi_node_state(dip) < DS_READY)) {
6183*0Sstevel@tonic-gate 			ndi_rele_devi(dip);
6184*0Sstevel@tonic-gate 			dip = ddi_get_next_sibling(dip);
6185*0Sstevel@tonic-gate 			continue;
6186*0Sstevel@tonic-gate 		}
6187*0Sstevel@tonic-gate 
6188*0Sstevel@tonic-gate 		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
6189*0Sstevel@tonic-gate 		mcd->mtc_dip = dip;
6190*0Sstevel@tonic-gate 		mcd->mtc_hdl = hdl;
6191*0Sstevel@tonic-gate 		mcd->mtc_brn = brn;
6192*0Sstevel@tonic-gate 
6193*0Sstevel@tonic-gate 		/*
6194*0Sstevel@tonic-gate 		 * Switch a 'driver' operation to an 'all' operation below a
6195*0Sstevel@tonic-gate 		 * node bound to the driver.
6196*0Sstevel@tonic-gate 		 */
6197*0Sstevel@tonic-gate 		if ((major == (major_t)-1) || (major == ddi_driver_major(pdip)))
6198*0Sstevel@tonic-gate 			mcd->mtc_major = (major_t)-1;
6199*0Sstevel@tonic-gate 		else
6200*0Sstevel@tonic-gate 			mcd->mtc_major = major;
6201*0Sstevel@tonic-gate 
6202*0Sstevel@tonic-gate 		/*
6203*0Sstevel@tonic-gate 		 * The unconfig-driver to unconfig-all conversion above
6204*0Sstevel@tonic-gate 		 * constitutes an autodetach for NDI_DETACH_DRIVER calls,
6205*0Sstevel@tonic-gate 		 * set NDI_AUTODETACH.
6206*0Sstevel@tonic-gate 		 */
6207*0Sstevel@tonic-gate 		mcd->mtc_flags = hdl->mtc_flags;
6208*0Sstevel@tonic-gate 		if ((mcd->mtc_flags & NDI_DETACH_DRIVER) &&
6209*0Sstevel@tonic-gate 		    (hdl->mtc_op == MT_UNCONFIG_OP) &&
6210*0Sstevel@tonic-gate 		    (major == ddi_driver_major(pdip)))
6211*0Sstevel@tonic-gate 			mcd->mtc_flags |= NDI_AUTODETACH;
6212*0Sstevel@tonic-gate 
6213*0Sstevel@tonic-gate 		mutex_enter(&hdl->mtc_lock);
6214*0Sstevel@tonic-gate 		hdl->mtc_thr_count++;
6215*0Sstevel@tonic-gate 		mutex_exit(&hdl->mtc_lock);
6216*0Sstevel@tonic-gate 
6217*0Sstevel@tonic-gate 		/*
6218*0Sstevel@tonic-gate 		 * Add to end of list to process after ndi_devi_exit to avoid
6219*0Sstevel@tonic-gate 		 * locking differences depending on value of mtc_off.
6220*0Sstevel@tonic-gate 		 */
6221*0Sstevel@tonic-gate 		mcd->mtc_next = NULL;
6222*0Sstevel@tonic-gate 		if (mcd_head == NULL)
6223*0Sstevel@tonic-gate 			mcd_head = mcd;
6224*0Sstevel@tonic-gate 		else
6225*0Sstevel@tonic-gate 			mcd_tail->mtc_next = mcd;
6226*0Sstevel@tonic-gate 		mcd_tail = mcd;
6227*0Sstevel@tonic-gate 
6228*0Sstevel@tonic-gate 		dip = ddi_get_next_sibling(dip);
6229*0Sstevel@tonic-gate 	}
6230*0Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
6231*0Sstevel@tonic-gate 
6232*0Sstevel@tonic-gate 	/* go through the list of held children */
6233*0Sstevel@tonic-gate 	for (mcd = mcd_head; mcd; mcd = mcd_head) {
6234*0Sstevel@tonic-gate 		mcd_head = mcd->mtc_next;
6235*0Sstevel@tonic-gate 		if (mtc_off)
6236*0Sstevel@tonic-gate 			mt_config_thread(mcd);
6237*0Sstevel@tonic-gate 		else
6238*0Sstevel@tonic-gate 			(void) thread_create(NULL, 0, mt_config_thread, mcd,
6239*0Sstevel@tonic-gate 			    0, &p0, TS_RUN, minclsyspri);
6240*0Sstevel@tonic-gate 	}
6241*0Sstevel@tonic-gate }
6242*0Sstevel@tonic-gate 
6243*0Sstevel@tonic-gate static void
6244*0Sstevel@tonic-gate mt_config_driver(struct mt_config_handle *hdl)
6245*0Sstevel@tonic-gate {
6246*0Sstevel@tonic-gate 	major_t			par_major = hdl->mtc_parmajor;
6247*0Sstevel@tonic-gate 	major_t			major = hdl->mtc_major;
6248*0Sstevel@tonic-gate 	struct devnames		*dnp = &devnamesp[par_major];
6249*0Sstevel@tonic-gate 	dev_info_t		*dip;
6250*0Sstevel@tonic-gate 	struct mt_config_data	*mcd_head = NULL;
6251*0Sstevel@tonic-gate 	struct mt_config_data	*mcd_tail = NULL;
6252*0Sstevel@tonic-gate 	struct mt_config_data	*mcd;
6253*0Sstevel@tonic-gate #ifdef DEBUG
6254*0Sstevel@tonic-gate 	timestruc_t		end_time;
6255*0Sstevel@tonic-gate 
6256*0Sstevel@tonic-gate 	/* Update total_time in handle */
6257*0Sstevel@tonic-gate 	gethrestime(&end_time);
6258*0Sstevel@tonic-gate 	hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
6259*0Sstevel@tonic-gate #endif
6260*0Sstevel@tonic-gate 	ASSERT(par_major != (major_t)-1);
6261*0Sstevel@tonic-gate 	ASSERT(major != (major_t)-1);
6262*0Sstevel@tonic-gate 
6263*0Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
6264*0Sstevel@tonic-gate 	dip = devnamesp[par_major].dn_head;
6265*0Sstevel@tonic-gate 	while (dip) {
6266*0Sstevel@tonic-gate 		/*
6267*0Sstevel@tonic-gate 		 * Hold the child that we are processing so he does not get
6268*0Sstevel@tonic-gate 		 * removed. The corrisponding ndi_rele_devi() for children
6269*0Sstevel@tonic-gate 		 * that are not being skipped is done at the end of
6270*0Sstevel@tonic-gate 		 * mt_config_thread().
6271*0Sstevel@tonic-gate 		 */
6272*0Sstevel@tonic-gate 		ndi_hold_devi(dip);
6273*0Sstevel@tonic-gate 
6274*0Sstevel@tonic-gate 		/* skip leaf nodes and nodes not fully attached */
6275*0Sstevel@tonic-gate 		if ((i_ddi_node_state(dip) < DS_READY) || is_leaf_node(dip)) {
6276*0Sstevel@tonic-gate 			ndi_rele_devi(dip);
6277*0Sstevel@tonic-gate 			dip = ddi_get_next(dip);
6278*0Sstevel@tonic-gate 			continue;
6279*0Sstevel@tonic-gate 		}
6280*0Sstevel@tonic-gate 
6281*0Sstevel@tonic-gate 		mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
6282*0Sstevel@tonic-gate 		mcd->mtc_dip = dip;
6283*0Sstevel@tonic-gate 		mcd->mtc_hdl = hdl;
6284*0Sstevel@tonic-gate 		mcd->mtc_major = major;
6285*0Sstevel@tonic-gate 		mcd->mtc_flags = hdl->mtc_flags;
6286*0Sstevel@tonic-gate 
6287*0Sstevel@tonic-gate 		mutex_enter(&hdl->mtc_lock);
6288*0Sstevel@tonic-gate 		hdl->mtc_thr_count++;
6289*0Sstevel@tonic-gate 		mutex_exit(&hdl->mtc_lock);
6290*0Sstevel@tonic-gate 
6291*0Sstevel@tonic-gate 		/*
6292*0Sstevel@tonic-gate 		 * Add to end of list to process after UNLOCK_DEV_OPS to avoid
6293*0Sstevel@tonic-gate 		 * locking differences depending on value of mtc_off.
6294*0Sstevel@tonic-gate 		 */
6295*0Sstevel@tonic-gate 		mcd->mtc_next = NULL;
6296*0Sstevel@tonic-gate 		if (mcd_head == NULL)
6297*0Sstevel@tonic-gate 			mcd_head = mcd;
6298*0Sstevel@tonic-gate 		else
6299*0Sstevel@tonic-gate 			mcd_tail->mtc_next = mcd;
6300*0Sstevel@tonic-gate 		mcd_tail = mcd;
6301*0Sstevel@tonic-gate 
6302*0Sstevel@tonic-gate 		dip = ddi_get_next(dip);
6303*0Sstevel@tonic-gate 	}
6304*0Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
6305*0Sstevel@tonic-gate 
6306*0Sstevel@tonic-gate 	/* go through the list of held children */
6307*0Sstevel@tonic-gate 	for (mcd = mcd_head; mcd; mcd = mcd_head) {
6308*0Sstevel@tonic-gate 		mcd_head = mcd->mtc_next;
6309*0Sstevel@tonic-gate 		if (mtc_off)
6310*0Sstevel@tonic-gate 			mt_config_thread(mcd);
6311*0Sstevel@tonic-gate 		else
6312*0Sstevel@tonic-gate 			(void) thread_create(NULL, 0, mt_config_thread, mcd,
6313*0Sstevel@tonic-gate 			    0, &p0, TS_RUN, minclsyspri);
6314*0Sstevel@tonic-gate 	}
6315*0Sstevel@tonic-gate }
6316*0Sstevel@tonic-gate 
6317*0Sstevel@tonic-gate /*
6318*0Sstevel@tonic-gate  * Given the nodeid for a persistent (PROM or SID) node, return
6319*0Sstevel@tonic-gate  * the corresponding devinfo node
6320*0Sstevel@tonic-gate  * NOTE: This function will return NULL for .conf nodeids.
6321*0Sstevel@tonic-gate  */
6322*0Sstevel@tonic-gate dev_info_t *
6323*0Sstevel@tonic-gate e_ddi_nodeid_to_dip(dnode_t nodeid)
6324*0Sstevel@tonic-gate {
6325*0Sstevel@tonic-gate 	dev_info_t		*dip = NULL;
6326*0Sstevel@tonic-gate 	struct devi_nodeid	*prev, *elem;
6327*0Sstevel@tonic-gate 
6328*0Sstevel@tonic-gate 	mutex_enter(&devimap->dno_lock);
6329*0Sstevel@tonic-gate 
6330*0Sstevel@tonic-gate 	prev = NULL;
6331*0Sstevel@tonic-gate 	for (elem = devimap->dno_head; elem; elem = elem->next) {
6332*0Sstevel@tonic-gate 		if (elem->nodeid == nodeid) {
6333*0Sstevel@tonic-gate 			ndi_hold_devi(elem->dip);
6334*0Sstevel@tonic-gate 			dip = elem->dip;
6335*0Sstevel@tonic-gate 			break;
6336*0Sstevel@tonic-gate 		}
6337*0Sstevel@tonic-gate 		prev = elem;
6338*0Sstevel@tonic-gate 	}
6339*0Sstevel@tonic-gate 
6340*0Sstevel@tonic-gate 	/*
6341*0Sstevel@tonic-gate 	 * Move to head for faster lookup next time
6342*0Sstevel@tonic-gate 	 */
6343*0Sstevel@tonic-gate 	if (elem && prev) {
6344*0Sstevel@tonic-gate 		prev->next = elem->next;
6345*0Sstevel@tonic-gate 		elem->next = devimap->dno_head;
6346*0Sstevel@tonic-gate 		devimap->dno_head = elem;
6347*0Sstevel@tonic-gate 	}
6348*0Sstevel@tonic-gate 
6349*0Sstevel@tonic-gate 	mutex_exit(&devimap->dno_lock);
6350*0Sstevel@tonic-gate 	return (dip);
6351*0Sstevel@tonic-gate }
6352*0Sstevel@tonic-gate 
6353*0Sstevel@tonic-gate static void
6354*0Sstevel@tonic-gate free_cache_task(void *arg)
6355*0Sstevel@tonic-gate {
6356*0Sstevel@tonic-gate 	ASSERT(arg == NULL);
6357*0Sstevel@tonic-gate 
6358*0Sstevel@tonic-gate 	mutex_enter(&di_cache.cache_lock);
6359*0Sstevel@tonic-gate 
6360*0Sstevel@tonic-gate 	/*
6361*0Sstevel@tonic-gate 	 * The cache can be invalidated without holding the lock
6362*0Sstevel@tonic-gate 	 * but it can be made valid again only while the lock is held.
6363*0Sstevel@tonic-gate 	 * So if the cache is invalid when the lock is held, it will
6364*0Sstevel@tonic-gate 	 * stay invalid until lock is released.
6365*0Sstevel@tonic-gate 	 */
6366*0Sstevel@tonic-gate 	if (!di_cache.cache_valid)
6367*0Sstevel@tonic-gate 		i_ddi_di_cache_free(&di_cache);
6368*0Sstevel@tonic-gate 
6369*0Sstevel@tonic-gate 	mutex_exit(&di_cache.cache_lock);
6370*0Sstevel@tonic-gate 
6371*0Sstevel@tonic-gate 	if (di_cache_debug)
6372*0Sstevel@tonic-gate 		cmn_err(CE_NOTE, "system_taskq: di_cache freed");
6373*0Sstevel@tonic-gate }
6374*0Sstevel@tonic-gate 
6375*0Sstevel@tonic-gate extern int modrootloaded;
6376*0Sstevel@tonic-gate 
6377*0Sstevel@tonic-gate void
6378*0Sstevel@tonic-gate i_ddi_di_cache_free(struct di_cache *cache)
6379*0Sstevel@tonic-gate {
6380*0Sstevel@tonic-gate 	int	error;
6381*0Sstevel@tonic-gate 
6382*0Sstevel@tonic-gate 	ASSERT(mutex_owned(&cache->cache_lock));
6383*0Sstevel@tonic-gate 
6384*0Sstevel@tonic-gate 	if (cache->cache_size) {
6385*0Sstevel@tonic-gate 		ASSERT(cache->cache_size > 0);
6386*0Sstevel@tonic-gate 		ASSERT(cache->cache_data);
6387*0Sstevel@tonic-gate 
6388*0Sstevel@tonic-gate 		kmem_free(cache->cache_data, cache->cache_size);
6389*0Sstevel@tonic-gate 		cache->cache_data = NULL;
6390*0Sstevel@tonic-gate 		cache->cache_size = 0;
6391*0Sstevel@tonic-gate 
6392*0Sstevel@tonic-gate 		if (di_cache_debug)
6393*0Sstevel@tonic-gate 			cmn_err(CE_NOTE, "i_ddi_di_cache_free: freed cachemem");
6394*0Sstevel@tonic-gate 	} else {
6395*0Sstevel@tonic-gate 		ASSERT(cache->cache_data == NULL);
6396*0Sstevel@tonic-gate 		if (di_cache_debug)
6397*0Sstevel@tonic-gate 			cmn_err(CE_NOTE, "i_ddi_di_cache_free: NULL cache");
6398*0Sstevel@tonic-gate 	}
6399*0Sstevel@tonic-gate 
6400*0Sstevel@tonic-gate 	if (!modrootloaded || rootvp == NULL || vn_is_readonly(rootvp)) {
6401*0Sstevel@tonic-gate 		if (di_cache_debug) {
6402*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "/ not mounted/RDONLY. Skip unlink");
6403*0Sstevel@tonic-gate 		}
6404*0Sstevel@tonic-gate 		return;
6405*0Sstevel@tonic-gate 	}
6406*0Sstevel@tonic-gate 
6407*0Sstevel@tonic-gate 	error = vn_remove(DI_CACHE_FILE, UIO_SYSSPACE, RMFILE);
6408*0Sstevel@tonic-gate 	if (di_cache_debug && error && error != ENOENT) {
6409*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: unlink failed: %d", DI_CACHE_FILE, error);
6410*0Sstevel@tonic-gate 	} else if (di_cache_debug && !error) {
6411*0Sstevel@tonic-gate 		cmn_err(CE_NOTE, "i_ddi_di_cache_free: unlinked cache file");
6412*0Sstevel@tonic-gate 	}
6413*0Sstevel@tonic-gate }
6414*0Sstevel@tonic-gate 
6415*0Sstevel@tonic-gate void
6416*0Sstevel@tonic-gate i_ddi_di_cache_invalidate(int kmflag)
6417*0Sstevel@tonic-gate {
6418*0Sstevel@tonic-gate 	uint_t	flag;
6419*0Sstevel@tonic-gate 
6420*0Sstevel@tonic-gate 	if (!modrootloaded || !i_ddi_io_initialized()) {
6421*0Sstevel@tonic-gate 		if (di_cache_debug)
6422*0Sstevel@tonic-gate 			cmn_err(CE_NOTE, "I/O not inited. Skipping invalidate");
6423*0Sstevel@tonic-gate 		return;
6424*0Sstevel@tonic-gate 	}
6425*0Sstevel@tonic-gate 
6426*0Sstevel@tonic-gate 	/*
6427*0Sstevel@tonic-gate 	 * Invalidate the in-core cache
6428*0Sstevel@tonic-gate 	 */
6429*0Sstevel@tonic-gate 	atomic_and_32(&di_cache.cache_valid, 0);
6430*0Sstevel@tonic-gate 
6431*0Sstevel@tonic-gate 	flag = (kmflag == KM_SLEEP) ? TQ_SLEEP : TQ_NOSLEEP;
6432*0Sstevel@tonic-gate 
6433*0Sstevel@tonic-gate 	(void) taskq_dispatch(system_taskq, free_cache_task, NULL, flag);
6434*0Sstevel@tonic-gate 
6435*0Sstevel@tonic-gate 	if (di_cache_debug) {
6436*0Sstevel@tonic-gate 		cmn_err(CE_NOTE, "invalidation with km_flag: %s",
6437*0Sstevel@tonic-gate 		    kmflag == KM_SLEEP ? "KM_SLEEP" : "KM_NOSLEEP");
6438*0Sstevel@tonic-gate 	}
6439*0Sstevel@tonic-gate }
6440*0Sstevel@tonic-gate 
6441*0Sstevel@tonic-gate 
6442*0Sstevel@tonic-gate static void
6443*0Sstevel@tonic-gate i_bind_vhci_node(dev_info_t *dip)
6444*0Sstevel@tonic-gate {
6445*0Sstevel@tonic-gate 	char	*node_name;
6446*0Sstevel@tonic-gate 
6447*0Sstevel@tonic-gate 	node_name = i_ddi_strdup(ddi_node_name(dip), KM_SLEEP);
6448*0Sstevel@tonic-gate 	i_ddi_set_binding_name(dip, node_name);
6449*0Sstevel@tonic-gate 	DEVI(dip)->devi_major = ddi_name_to_major(node_name);
6450*0Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_BOUND);
6451*0Sstevel@tonic-gate }
6452*0Sstevel@tonic-gate 
6453*0Sstevel@tonic-gate 
6454*0Sstevel@tonic-gate static void
6455*0Sstevel@tonic-gate i_free_vhci_bind_name(dev_info_t *dip)
6456*0Sstevel@tonic-gate {
6457*0Sstevel@tonic-gate 	if (DEVI(dip)->devi_binding_name) {
6458*0Sstevel@tonic-gate 		kmem_free(DEVI(dip)->devi_binding_name,
6459*0Sstevel@tonic-gate 		    sizeof (ddi_node_name(dip)));
6460*0Sstevel@tonic-gate 	}
6461*0Sstevel@tonic-gate }
6462*0Sstevel@tonic-gate 
6463*0Sstevel@tonic-gate 
6464*0Sstevel@tonic-gate static char vhci_node_addr[2];
6465*0Sstevel@tonic-gate 
6466*0Sstevel@tonic-gate static int
6467*0Sstevel@tonic-gate i_init_vhci_node(dev_info_t *dip)
6468*0Sstevel@tonic-gate {
6469*0Sstevel@tonic-gate 	add_global_props(dip);
6470*0Sstevel@tonic-gate 	DEVI(dip)->devi_ops = ndi_hold_driver(dip);
6471*0Sstevel@tonic-gate 	if (DEVI(dip)->devi_ops == NULL)
6472*0Sstevel@tonic-gate 		return (-1);
6473*0Sstevel@tonic-gate 
6474*0Sstevel@tonic-gate 	DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
6475*0Sstevel@tonic-gate 	e_ddi_keep_instance(dip);
6476*0Sstevel@tonic-gate 	vhci_node_addr[0]	= '\0';
6477*0Sstevel@tonic-gate 	ddi_set_name_addr(dip, vhci_node_addr);
6478*0Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_INITIALIZED);
6479*0Sstevel@tonic-gate 	return (0);
6480*0Sstevel@tonic-gate }
6481*0Sstevel@tonic-gate 
6482*0Sstevel@tonic-gate static void
6483*0Sstevel@tonic-gate i_link_vhci_node(dev_info_t *dip)
6484*0Sstevel@tonic-gate {
6485*0Sstevel@tonic-gate 	/*
6486*0Sstevel@tonic-gate 	 * scsi_vhci should be kept left most of the device tree.
6487*0Sstevel@tonic-gate 	 */
6488*0Sstevel@tonic-gate 	mutex_enter(&global_vhci_lock);
6489*0Sstevel@tonic-gate 	if (scsi_vhci_dip) {
6490*0Sstevel@tonic-gate 		DEVI(dip)->devi_sibling = DEVI(scsi_vhci_dip)->devi_sibling;
6491*0Sstevel@tonic-gate 		DEVI(scsi_vhci_dip)->devi_sibling = DEVI(dip);
6492*0Sstevel@tonic-gate 	} else {
6493*0Sstevel@tonic-gate 		DEVI(dip)->devi_sibling = DEVI(top_devinfo)->devi_child;
6494*0Sstevel@tonic-gate 		DEVI(top_devinfo)->devi_child = DEVI(dip);
6495*0Sstevel@tonic-gate 	}
6496*0Sstevel@tonic-gate 	mutex_exit(&global_vhci_lock);
6497*0Sstevel@tonic-gate }
6498*0Sstevel@tonic-gate 
6499*0Sstevel@tonic-gate 
6500*0Sstevel@tonic-gate /*
6501*0Sstevel@tonic-gate  * This a special routine to enumerate vhci node (child of rootnex
6502*0Sstevel@tonic-gate  * node) without holding the ndi_devi_enter() lock. The device node
6503*0Sstevel@tonic-gate  * is allocated, initialized and brought into DS_READY state before
6504*0Sstevel@tonic-gate  * inserting into the device tree. The VHCI node is handcrafted
6505*0Sstevel@tonic-gate  * here to bring the node to DS_READY, similar to rootnex node.
6506*0Sstevel@tonic-gate  *
6507*0Sstevel@tonic-gate  * The global_vhci_lock protects linking the node into the device
6508*0Sstevel@tonic-gate  * as same lock is held before linking/unlinking any direct child
6509*0Sstevel@tonic-gate  * of rootnex children.
6510*0Sstevel@tonic-gate  *
6511*0Sstevel@tonic-gate  * This routine is a workaround to handle a possible deadlock
6512*0Sstevel@tonic-gate  * that occurs while trying to enumerate node in a different sub-tree
6513*0Sstevel@tonic-gate  * during _init/_attach entry points.
6514*0Sstevel@tonic-gate  */
6515*0Sstevel@tonic-gate /*ARGSUSED*/
6516*0Sstevel@tonic-gate dev_info_t *
6517*0Sstevel@tonic-gate ndi_devi_config_vhci(char *drvname, int flags)
6518*0Sstevel@tonic-gate {
6519*0Sstevel@tonic-gate 	struct devnames		*dnp;
6520*0Sstevel@tonic-gate 	dev_info_t		*dip;
6521*0Sstevel@tonic-gate 	major_t			major = ddi_name_to_major(drvname);
6522*0Sstevel@tonic-gate 
6523*0Sstevel@tonic-gate 	if (major == -1)
6524*0Sstevel@tonic-gate 		return (NULL);
6525*0Sstevel@tonic-gate 
6526*0Sstevel@tonic-gate 	/* Make sure we create the VHCI node only once */
6527*0Sstevel@tonic-gate 	dnp = &devnamesp[major];
6528*0Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
6529*0Sstevel@tonic-gate 	if (dnp->dn_head) {
6530*0Sstevel@tonic-gate 		dip = dnp->dn_head;
6531*0Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
6532*0Sstevel@tonic-gate 		return (dip);
6533*0Sstevel@tonic-gate 	}
6534*0Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
6535*0Sstevel@tonic-gate 
6536*0Sstevel@tonic-gate 	/* Allocate the VHCI node */
6537*0Sstevel@tonic-gate 	ndi_devi_alloc_sleep(top_devinfo, drvname, DEVI_SID_NODEID, &dip);
6538*0Sstevel@tonic-gate 	ndi_hold_devi(dip);
6539*0Sstevel@tonic-gate 
6540*0Sstevel@tonic-gate 	/* Mark the node as VHCI */
6541*0Sstevel@tonic-gate 	DEVI(dip)->devi_node_attributes |= DDI_VHCI_NODE;
6542*0Sstevel@tonic-gate 
6543*0Sstevel@tonic-gate 	i_ddi_add_devimap(dip);
6544*0Sstevel@tonic-gate 	i_bind_vhci_node(dip);
6545*0Sstevel@tonic-gate 	if (i_init_vhci_node(dip) == -1) {
6546*0Sstevel@tonic-gate 		i_free_vhci_bind_name(dip);
6547*0Sstevel@tonic-gate 		ndi_rele_devi(dip);
6548*0Sstevel@tonic-gate 		(void) ndi_devi_free(dip);
6549*0Sstevel@tonic-gate 		return (NULL);
6550*0Sstevel@tonic-gate 	}
6551*0Sstevel@tonic-gate 
6552*0Sstevel@tonic-gate 	DEVI_SET_ATTACHING(dip);
6553*0Sstevel@tonic-gate 	if (devi_attach(dip, DDI_ATTACH) != DDI_SUCCESS) {
6554*0Sstevel@tonic-gate 		cmn_err(CE_CONT, "Could not attach %s driver", drvname);
6555*0Sstevel@tonic-gate 		e_ddi_free_instance(dip, vhci_node_addr);
6556*0Sstevel@tonic-gate 		i_free_vhci_bind_name(dip);
6557*0Sstevel@tonic-gate 		ndi_rele_devi(dip);
6558*0Sstevel@tonic-gate 		(void) ndi_devi_free(dip);
6559*0Sstevel@tonic-gate 		return (NULL);
6560*0Sstevel@tonic-gate 	}
6561*0Sstevel@tonic-gate 	DEVI_CLR_ATTACHING(dip);
6562*0Sstevel@tonic-gate 
6563*0Sstevel@tonic-gate 	i_link_vhci_node(dip);
6564*0Sstevel@tonic-gate 	i_ddi_set_node_state(dip, DS_READY);
6565*0Sstevel@tonic-gate 
6566*0Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
6567*0Sstevel@tonic-gate 	dnp->dn_flags |= DN_DRIVER_HELD;
6568*0Sstevel@tonic-gate 	dnp->dn_head = dip;
6569*0Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
6570*0Sstevel@tonic-gate 
6571*0Sstevel@tonic-gate 	i_ndi_devi_report_status_change(dip, NULL);
6572*0Sstevel@tonic-gate 
6573*0Sstevel@tonic-gate 	return (dip);
6574*0Sstevel@tonic-gate }
6575*0Sstevel@tonic-gate 
6576*0Sstevel@tonic-gate /*
6577*0Sstevel@tonic-gate  * ibt_hw_is_present() returns 0 when there is no IB hardware actively
6578*0Sstevel@tonic-gate  * running.  This is primarily useful for modules like rpcmod which
6579*0Sstevel@tonic-gate  * needs a quick check to decide whether or not it should try to use
6580*0Sstevel@tonic-gate  * InfiniBand
6581*0Sstevel@tonic-gate  */
6582*0Sstevel@tonic-gate int ib_hw_status = 0;
6583*0Sstevel@tonic-gate int
6584*0Sstevel@tonic-gate ibt_hw_is_present()
6585*0Sstevel@tonic-gate {
6586*0Sstevel@tonic-gate 	return (ib_hw_status);
6587*0Sstevel@tonic-gate }
6588