xref: /onnv-gate/usr/src/uts/common/os/driver_lyr.c (revision 0:68f95e015346)
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 2004 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 /*
30*0Sstevel@tonic-gate  * Layered driver support.
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <sys/atomic.h>
34*0Sstevel@tonic-gate #include <sys/types.h>
35*0Sstevel@tonic-gate #include <sys/t_lock.h>
36*0Sstevel@tonic-gate #include <sys/param.h>
37*0Sstevel@tonic-gate #include <sys/conf.h>
38*0Sstevel@tonic-gate #include <sys/systm.h>
39*0Sstevel@tonic-gate #include <sys/sysmacros.h>
40*0Sstevel@tonic-gate #include <sys/buf.h>
41*0Sstevel@tonic-gate #include <sys/cred.h>
42*0Sstevel@tonic-gate #include <sys/uio.h>
43*0Sstevel@tonic-gate #include <sys/vnode.h>
44*0Sstevel@tonic-gate #include <sys/fs/snode.h>
45*0Sstevel@tonic-gate #include <sys/open.h>
46*0Sstevel@tonic-gate #include <sys/kmem.h>
47*0Sstevel@tonic-gate #include <sys/file.h>
48*0Sstevel@tonic-gate #include <sys/bootconf.h>
49*0Sstevel@tonic-gate #include <sys/pathname.h>
50*0Sstevel@tonic-gate #include <sys/bitmap.h>
51*0Sstevel@tonic-gate #include <sys/stat.h>
52*0Sstevel@tonic-gate #include <sys/dditypes.h>
53*0Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
54*0Sstevel@tonic-gate #include <sys/ddi.h>
55*0Sstevel@tonic-gate #include <sys/sunddi.h>
56*0Sstevel@tonic-gate #include <sys/sunndi.h>
57*0Sstevel@tonic-gate #include <sys/esunddi.h>
58*0Sstevel@tonic-gate #include <sys/autoconf.h>
59*0Sstevel@tonic-gate #include <sys/sunldi.h>
60*0Sstevel@tonic-gate #include <sys/sunldi_impl.h>
61*0Sstevel@tonic-gate #include <sys/errno.h>
62*0Sstevel@tonic-gate #include <sys/debug.h>
63*0Sstevel@tonic-gate #include <sys/modctl.h>
64*0Sstevel@tonic-gate #include <sys/var.h>
65*0Sstevel@tonic-gate #include <vm/seg_vn.h>
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate #include <sys/stropts.h>
68*0Sstevel@tonic-gate #include <sys/strsubr.h>
69*0Sstevel@tonic-gate #include <sys/socket.h>
70*0Sstevel@tonic-gate #include <sys/socketvar.h>
71*0Sstevel@tonic-gate #include <sys/kstr.h>
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate /*
75*0Sstevel@tonic-gate  * Define macros to manipulate snode, vnode, and open device flags
76*0Sstevel@tonic-gate  */
77*0Sstevel@tonic-gate #define	VTYP_VALID(i)	(((i) == VCHR) || ((i) == VBLK))
78*0Sstevel@tonic-gate #define	VTYP_TO_OTYP(i)	(((i) == VCHR) ? OTYP_CHR : OTYP_BLK)
79*0Sstevel@tonic-gate #define	VTYP_TO_STYP(i)	(((i) == VCHR) ? S_IFCHR : S_IFBLK)
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate #define	OTYP_VALID(i)	(((i) == OTYP_CHR) || ((i) == OTYP_BLK))
82*0Sstevel@tonic-gate #define	OTYP_TO_VTYP(i)	(((i) == OTYP_CHR) ? VCHR : VBLK)
83*0Sstevel@tonic-gate #define	OTYP_TO_STYP(i)	(((i) == OTYP_CHR) ? S_IFCHR : S_IFBLK)
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate #define	STYP_VALID(i)	(((i) == S_IFCHR) || ((i) == S_IFBLK))
86*0Sstevel@tonic-gate #define	STYP_TO_VTYP(i)	(((i) == S_IFCHR) ? VCHR : VBLK)
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate /*
89*0Sstevel@tonic-gate  * Define macros for accessing layered driver hash structures
90*0Sstevel@tonic-gate  */
91*0Sstevel@tonic-gate #define	LH_HASH(vp)		(handle_hash_func(vp) % LH_HASH_SZ)
92*0Sstevel@tonic-gate #define	LI_HASH(mid, dip, dev)	(ident_hash_func(mid, dip, dev) % LI_HASH_SZ)
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate /*
95*0Sstevel@tonic-gate  * Define layered handle flags used in the lh_type field
96*0Sstevel@tonic-gate  */
97*0Sstevel@tonic-gate #define	LH_STREAM	(0x1)	/* handle to a streams device */
98*0Sstevel@tonic-gate #define	LH_CBDEV	(0x2)	/* handle to a char/block device */
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate /*
101*0Sstevel@tonic-gate  * Define marco for devid property lookups
102*0Sstevel@tonic-gate  */
103*0Sstevel@tonic-gate #define	DEVID_PROP_FLAGS	(DDI_PROP_DONTPASS | \
104*0Sstevel@tonic-gate 				DDI_PROP_TYPE_STRING|DDI_PROP_CANSLEEP)
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate /*
108*0Sstevel@tonic-gate  * globals
109*0Sstevel@tonic-gate  */
110*0Sstevel@tonic-gate static kmutex_t			ldi_ident_hash_lock[LI_HASH_SZ];
111*0Sstevel@tonic-gate static struct ldi_ident		*ldi_ident_hash[LI_HASH_SZ];
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate static kmutex_t			ldi_handle_hash_lock[LH_HASH_SZ];
114*0Sstevel@tonic-gate static struct ldi_handle	*ldi_handle_hash[LH_HASH_SZ];
115*0Sstevel@tonic-gate static size_t			ldi_handle_hash_count;
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate void
118*0Sstevel@tonic-gate ldi_init(void)
119*0Sstevel@tonic-gate {
120*0Sstevel@tonic-gate 	int i;
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	ldi_handle_hash_count = 0;
123*0Sstevel@tonic-gate 	for (i = 0; i < LH_HASH_SZ; i++) {
124*0Sstevel@tonic-gate 		mutex_init(&ldi_handle_hash_lock[i], NULL, MUTEX_DEFAULT, NULL);
125*0Sstevel@tonic-gate 		ldi_handle_hash[i] = NULL;
126*0Sstevel@tonic-gate 	}
127*0Sstevel@tonic-gate 	for (i = 0; i < LI_HASH_SZ; i++) {
128*0Sstevel@tonic-gate 		mutex_init(&ldi_ident_hash_lock[i], NULL, MUTEX_DEFAULT, NULL);
129*0Sstevel@tonic-gate 		ldi_ident_hash[i] = NULL;
130*0Sstevel@tonic-gate 	}
131*0Sstevel@tonic-gate }
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate /*
134*0Sstevel@tonic-gate  * LDI ident manipulation functions
135*0Sstevel@tonic-gate  */
136*0Sstevel@tonic-gate static uint_t
137*0Sstevel@tonic-gate ident_hash_func(modid_t modid, dev_info_t *dip, dev_t dev)
138*0Sstevel@tonic-gate {
139*0Sstevel@tonic-gate 	if (dip != NULL) {
140*0Sstevel@tonic-gate 		uintptr_t k = (uintptr_t)dip;
141*0Sstevel@tonic-gate 		k >>= (int)highbit(sizeof (struct dev_info));
142*0Sstevel@tonic-gate 		return ((uint_t)k);
143*0Sstevel@tonic-gate 	} else if (dev != DDI_DEV_T_NONE) {
144*0Sstevel@tonic-gate 		return (modid + getminor(dev) + getmajor(dev));
145*0Sstevel@tonic-gate 	} else {
146*0Sstevel@tonic-gate 		return (modid);
147*0Sstevel@tonic-gate 	}
148*0Sstevel@tonic-gate }
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate static struct ldi_ident **
151*0Sstevel@tonic-gate ident_find_ref_nolock(modid_t modid, dev_info_t *dip, dev_t dev, major_t major)
152*0Sstevel@tonic-gate {
153*0Sstevel@tonic-gate 	struct ldi_ident	**lipp = NULL;
154*0Sstevel@tonic-gate 	uint_t			index = LI_HASH(modid, dip, dev);
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ldi_ident_hash_lock[index]));
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 	for (lipp = &(ldi_ident_hash[index]);
159*0Sstevel@tonic-gate 	    (*lipp != NULL);
160*0Sstevel@tonic-gate 	    lipp = &((*lipp)->li_next)) {
161*0Sstevel@tonic-gate 		if (((*lipp)->li_modid == modid) &&
162*0Sstevel@tonic-gate 		    ((*lipp)->li_major == major) &&
163*0Sstevel@tonic-gate 		    ((*lipp)->li_dip == dip) &&
164*0Sstevel@tonic-gate 		    ((*lipp)->li_dev == dev))
165*0Sstevel@tonic-gate 			break;
166*0Sstevel@tonic-gate 	}
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 	ASSERT(lipp != NULL);
169*0Sstevel@tonic-gate 	return (lipp);
170*0Sstevel@tonic-gate }
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate static struct ldi_ident *
173*0Sstevel@tonic-gate ident_alloc(char *mod_name, dev_info_t *dip, dev_t dev, major_t major)
174*0Sstevel@tonic-gate {
175*0Sstevel@tonic-gate 	struct ldi_ident	*lip, **lipp;
176*0Sstevel@tonic-gate 	modid_t			modid;
177*0Sstevel@tonic-gate 	uint_t			index;
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate 	ASSERT(mod_name != NULL);
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	/* get the module id */
182*0Sstevel@tonic-gate 	modid = mod_name_to_modid(mod_name);
183*0Sstevel@tonic-gate 	ASSERT(modid != -1);
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 	/* allocate a new ident in case we need it */
186*0Sstevel@tonic-gate 	lip = kmem_zalloc(sizeof (*lip), KM_SLEEP);
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 	/* search the hash for a matching ident */
189*0Sstevel@tonic-gate 	index = LI_HASH(modid, dip, dev);
190*0Sstevel@tonic-gate 	mutex_enter(&ldi_ident_hash_lock[index]);
191*0Sstevel@tonic-gate 	lipp = ident_find_ref_nolock(modid, dip, dev, major);
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 	if (*lipp != NULL) {
194*0Sstevel@tonic-gate 		/* we found an indent in the hash */
195*0Sstevel@tonic-gate 		ASSERT(strcmp((*lipp)->li_modname, mod_name) == 0);
196*0Sstevel@tonic-gate 		(*lipp)->li_ref++;
197*0Sstevel@tonic-gate 		mutex_exit(&ldi_ident_hash_lock[index]);
198*0Sstevel@tonic-gate 		kmem_free(lip, sizeof (struct ldi_ident));
199*0Sstevel@tonic-gate 		return (*lipp);
200*0Sstevel@tonic-gate 	}
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 	/* initialize the new ident */
203*0Sstevel@tonic-gate 	lip->li_next = NULL;
204*0Sstevel@tonic-gate 	lip->li_ref = 1;
205*0Sstevel@tonic-gate 	lip->li_modid = modid;
206*0Sstevel@tonic-gate 	lip->li_major = major;
207*0Sstevel@tonic-gate 	lip->li_dip = dip;
208*0Sstevel@tonic-gate 	lip->li_dev = dev;
209*0Sstevel@tonic-gate 	(void) strncpy(lip->li_modname, mod_name, sizeof (lip->li_modname) - 1);
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 	/* add it to the ident hash */
212*0Sstevel@tonic-gate 	lip->li_next = ldi_ident_hash[index];
213*0Sstevel@tonic-gate 	ldi_ident_hash[index] = lip;
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate 	mutex_exit(&ldi_ident_hash_lock[index]);
216*0Sstevel@tonic-gate 	return (lip);
217*0Sstevel@tonic-gate }
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate static void
220*0Sstevel@tonic-gate ident_hold(struct ldi_ident *lip)
221*0Sstevel@tonic-gate {
222*0Sstevel@tonic-gate 	uint_t			index;
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 	ASSERT(lip != NULL);
225*0Sstevel@tonic-gate 	index = LI_HASH(lip->li_modid, lip->li_dip, lip->li_dev);
226*0Sstevel@tonic-gate 	mutex_enter(&ldi_ident_hash_lock[index]);
227*0Sstevel@tonic-gate 	ASSERT(lip->li_ref > 0);
228*0Sstevel@tonic-gate 	lip->li_ref++;
229*0Sstevel@tonic-gate 	mutex_exit(&ldi_ident_hash_lock[index]);
230*0Sstevel@tonic-gate }
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate static void
233*0Sstevel@tonic-gate ident_release(struct ldi_ident *lip)
234*0Sstevel@tonic-gate {
235*0Sstevel@tonic-gate 	struct ldi_ident	**lipp;
236*0Sstevel@tonic-gate 	uint_t			index;
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 	ASSERT(lip != NULL);
239*0Sstevel@tonic-gate 	index = LI_HASH(lip->li_modid, lip->li_dip, lip->li_dev);
240*0Sstevel@tonic-gate 	mutex_enter(&ldi_ident_hash_lock[index]);
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate 	ASSERT(lip->li_ref > 0);
243*0Sstevel@tonic-gate 	if (--lip->li_ref > 0) {
244*0Sstevel@tonic-gate 		/* there are more references to this ident */
245*0Sstevel@tonic-gate 		mutex_exit(&ldi_ident_hash_lock[index]);
246*0Sstevel@tonic-gate 		return;
247*0Sstevel@tonic-gate 	}
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate 	/* this was the last reference/open for this ident.  free it. */
250*0Sstevel@tonic-gate 	lipp = ident_find_ref_nolock(
251*0Sstevel@tonic-gate 	    lip->li_modid, lip->li_dip, lip->li_dev, lip->li_major);
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate 	ASSERT((lipp != NULL) && (*lipp != NULL));
254*0Sstevel@tonic-gate 	*lipp = lip->li_next;
255*0Sstevel@tonic-gate 	mutex_exit(&ldi_ident_hash_lock[index]);
256*0Sstevel@tonic-gate 	kmem_free(lip, sizeof (struct ldi_ident));
257*0Sstevel@tonic-gate }
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate /*
260*0Sstevel@tonic-gate  * LDI handle manipulation functions
261*0Sstevel@tonic-gate  */
262*0Sstevel@tonic-gate static uint_t
263*0Sstevel@tonic-gate handle_hash_func(void *vp)
264*0Sstevel@tonic-gate {
265*0Sstevel@tonic-gate 	uintptr_t k = (uintptr_t)vp;
266*0Sstevel@tonic-gate 	k >>= (int)highbit(sizeof (vnode_t));
267*0Sstevel@tonic-gate 	return ((uint_t)k);
268*0Sstevel@tonic-gate }
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate static struct ldi_handle **
271*0Sstevel@tonic-gate handle_find_ref_nolock(vnode_t *vp, struct ldi_ident *ident)
272*0Sstevel@tonic-gate {
273*0Sstevel@tonic-gate 	struct ldi_handle	**lhpp = NULL;
274*0Sstevel@tonic-gate 	uint_t			index = LH_HASH(vp);
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ldi_handle_hash_lock[index]));
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate 	for (lhpp = &(ldi_handle_hash[index]);
279*0Sstevel@tonic-gate 	    (*lhpp != NULL);
280*0Sstevel@tonic-gate 	    lhpp = &((*lhpp)->lh_next)) {
281*0Sstevel@tonic-gate 		if (((*lhpp)->lh_ident == ident) &&
282*0Sstevel@tonic-gate 		    ((*lhpp)->lh_vp == vp))
283*0Sstevel@tonic-gate 			break;
284*0Sstevel@tonic-gate 	}
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate 	ASSERT(lhpp != NULL);
287*0Sstevel@tonic-gate 	return (lhpp);
288*0Sstevel@tonic-gate }
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate static struct ldi_handle *
291*0Sstevel@tonic-gate handle_find(vnode_t *vp, struct ldi_ident *ident)
292*0Sstevel@tonic-gate {
293*0Sstevel@tonic-gate 	struct ldi_handle	**lhpp;
294*0Sstevel@tonic-gate 	int			index = LH_HASH(vp);
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate 	mutex_enter(&ldi_handle_hash_lock[index]);
297*0Sstevel@tonic-gate 	lhpp = handle_find_ref_nolock(vp, ident);
298*0Sstevel@tonic-gate 	mutex_exit(&ldi_handle_hash_lock[index]);
299*0Sstevel@tonic-gate 	ASSERT(lhpp != NULL);
300*0Sstevel@tonic-gate 	return (*lhpp);
301*0Sstevel@tonic-gate }
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate static struct ldi_handle *
304*0Sstevel@tonic-gate handle_alloc(vnode_t *vp, struct ldi_ident *ident)
305*0Sstevel@tonic-gate {
306*0Sstevel@tonic-gate 	struct ldi_handle	*lhp, **lhpp;
307*0Sstevel@tonic-gate 	uint_t			index;
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate 	ASSERT((vp != NULL) && (ident != NULL));
310*0Sstevel@tonic-gate 
311*0Sstevel@tonic-gate 	/* allocate a new handle in case we need it */
312*0Sstevel@tonic-gate 	lhp = kmem_zalloc(sizeof (*lhp), KM_SLEEP);
313*0Sstevel@tonic-gate 
314*0Sstevel@tonic-gate 	/* search the hash for a matching handle */
315*0Sstevel@tonic-gate 	index = LH_HASH(vp);
316*0Sstevel@tonic-gate 	mutex_enter(&ldi_handle_hash_lock[index]);
317*0Sstevel@tonic-gate 	lhpp = handle_find_ref_nolock(vp, ident);
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 	if (*lhpp != NULL) {
320*0Sstevel@tonic-gate 		/* we found a handle in the hash */
321*0Sstevel@tonic-gate 		(*lhpp)->lh_ref++;
322*0Sstevel@tonic-gate 		mutex_exit(&ldi_handle_hash_lock[index]);
323*0Sstevel@tonic-gate 
324*0Sstevel@tonic-gate 		LDI_ALLOCFREE((CE_WARN, "ldi handle alloc: dup "
325*0Sstevel@tonic-gate 			"lh=0x%p, ident=0x%p, vp=0x%p, drv=%s, minor=0x%x",
326*0Sstevel@tonic-gate 			(void *)*lhpp, (void *)ident, (void *)vp,
327*0Sstevel@tonic-gate 			mod_major_to_name(getmajor(vp->v_rdev)),
328*0Sstevel@tonic-gate 			getminor(vp->v_rdev)));
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 		kmem_free(lhp, sizeof (struct ldi_handle));
331*0Sstevel@tonic-gate 		return (*lhpp);
332*0Sstevel@tonic-gate 	}
333*0Sstevel@tonic-gate 
334*0Sstevel@tonic-gate 	/* initialize the new handle */
335*0Sstevel@tonic-gate 	lhp->lh_ref = 1;
336*0Sstevel@tonic-gate 	lhp->lh_vp = vp;
337*0Sstevel@tonic-gate 	lhp->lh_ident = ident;
338*0Sstevel@tonic-gate 	mutex_init(lhp->lh_lock, NULL, MUTEX_DEFAULT, NULL);
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate 	/* set the device type for this handle */
341*0Sstevel@tonic-gate 	lhp->lh_type = 0;
342*0Sstevel@tonic-gate 	if (STREAMSTAB(getmajor(vp->v_rdev))) {
343*0Sstevel@tonic-gate 		ASSERT(vp->v_type == VCHR);
344*0Sstevel@tonic-gate 		lhp->lh_type |= LH_STREAM;
345*0Sstevel@tonic-gate 	} else {
346*0Sstevel@tonic-gate 		lhp->lh_type |= LH_CBDEV;
347*0Sstevel@tonic-gate 	}
348*0Sstevel@tonic-gate 
349*0Sstevel@tonic-gate 	/* get holds on other objects */
350*0Sstevel@tonic-gate 	ident_hold(ident);
351*0Sstevel@tonic-gate 	ASSERT(vp->v_count >= 1);
352*0Sstevel@tonic-gate 	VN_HOLD(vp);
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate 	/* add it to the handle hash */
355*0Sstevel@tonic-gate 	lhp->lh_next = ldi_handle_hash[index];
356*0Sstevel@tonic-gate 	ldi_handle_hash[index] = lhp;
357*0Sstevel@tonic-gate 	atomic_add_long(&ldi_handle_hash_count, 1);
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate 	LDI_ALLOCFREE((CE_WARN, "ldi handle alloc: new "
360*0Sstevel@tonic-gate 		"lh=0x%p, ident=0x%p, vp=0x%p, drv=%s, minor=0x%x",
361*0Sstevel@tonic-gate 		(void *)lhp, (void *)ident, (void *)vp,
362*0Sstevel@tonic-gate 		mod_major_to_name(getmajor(vp->v_rdev)),
363*0Sstevel@tonic-gate 		getminor(vp->v_rdev)));
364*0Sstevel@tonic-gate 
365*0Sstevel@tonic-gate 	mutex_exit(&ldi_handle_hash_lock[index]);
366*0Sstevel@tonic-gate 	return (lhp);
367*0Sstevel@tonic-gate }
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate static void
370*0Sstevel@tonic-gate handle_release(struct ldi_handle *lhp)
371*0Sstevel@tonic-gate {
372*0Sstevel@tonic-gate 	struct ldi_handle	**lhpp;
373*0Sstevel@tonic-gate 	uint_t			index;
374*0Sstevel@tonic-gate 
375*0Sstevel@tonic-gate 	ASSERT(lhp != NULL);
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 	index = LH_HASH(lhp->lh_vp);
378*0Sstevel@tonic-gate 	mutex_enter(&ldi_handle_hash_lock[index]);
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate 	LDI_ALLOCFREE((CE_WARN, "ldi handle release: "
381*0Sstevel@tonic-gate 		"lh=0x%p, ident=0x%p, vp=0x%p, drv=%s, minor=0x%x",
382*0Sstevel@tonic-gate 		(void *)lhp, (void *)lhp->lh_ident, (void *)lhp->lh_vp,
383*0Sstevel@tonic-gate 		mod_major_to_name(getmajor(lhp->lh_vp->v_rdev)),
384*0Sstevel@tonic-gate 		getminor(lhp->lh_vp->v_rdev)));
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate 	ASSERT(lhp->lh_ref > 0);
387*0Sstevel@tonic-gate 	if (--lhp->lh_ref > 0) {
388*0Sstevel@tonic-gate 		/* there are more references to this handle */
389*0Sstevel@tonic-gate 		mutex_exit(&ldi_handle_hash_lock[index]);
390*0Sstevel@tonic-gate 		return;
391*0Sstevel@tonic-gate 	}
392*0Sstevel@tonic-gate 
393*0Sstevel@tonic-gate 	/* this was the last reference/open for this handle.  free it. */
394*0Sstevel@tonic-gate 	lhpp = handle_find_ref_nolock(lhp->lh_vp, lhp->lh_ident);
395*0Sstevel@tonic-gate 	ASSERT((lhpp != NULL) && (*lhpp != NULL));
396*0Sstevel@tonic-gate 	*lhpp = lhp->lh_next;
397*0Sstevel@tonic-gate 	atomic_add_long(&ldi_handle_hash_count, -1);
398*0Sstevel@tonic-gate 	mutex_exit(&ldi_handle_hash_lock[index]);
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate 	VN_RELE(lhp->lh_vp);
401*0Sstevel@tonic-gate 	ident_release(lhp->lh_ident);
402*0Sstevel@tonic-gate 	mutex_destroy(lhp->lh_lock);
403*0Sstevel@tonic-gate 	kmem_free(lhp, sizeof (struct ldi_handle));
404*0Sstevel@tonic-gate }
405*0Sstevel@tonic-gate 
406*0Sstevel@tonic-gate /*
407*0Sstevel@tonic-gate  * LDI event manipulation functions
408*0Sstevel@tonic-gate  */
409*0Sstevel@tonic-gate static void
410*0Sstevel@tonic-gate handle_event_add(ldi_event_t *lep)
411*0Sstevel@tonic-gate {
412*0Sstevel@tonic-gate 	struct ldi_handle *lhp = lep->le_lhp;
413*0Sstevel@tonic-gate 
414*0Sstevel@tonic-gate 	ASSERT(lhp != NULL);
415*0Sstevel@tonic-gate 
416*0Sstevel@tonic-gate 	mutex_enter(lhp->lh_lock);
417*0Sstevel@tonic-gate 	if (lhp->lh_events == NULL) {
418*0Sstevel@tonic-gate 		lhp->lh_events = lep;
419*0Sstevel@tonic-gate 		mutex_exit(lhp->lh_lock);
420*0Sstevel@tonic-gate 		return;
421*0Sstevel@tonic-gate 	}
422*0Sstevel@tonic-gate 
423*0Sstevel@tonic-gate 	lep->le_next = lhp->lh_events;
424*0Sstevel@tonic-gate 	lhp->lh_events->le_prev = lep;
425*0Sstevel@tonic-gate 	lhp->lh_events = lep;
426*0Sstevel@tonic-gate 	mutex_exit(lhp->lh_lock);
427*0Sstevel@tonic-gate }
428*0Sstevel@tonic-gate 
429*0Sstevel@tonic-gate static void
430*0Sstevel@tonic-gate handle_event_remove(ldi_event_t *lep)
431*0Sstevel@tonic-gate {
432*0Sstevel@tonic-gate 	struct ldi_handle *lhp = lep->le_lhp;
433*0Sstevel@tonic-gate 
434*0Sstevel@tonic-gate 	ASSERT(lhp != NULL);
435*0Sstevel@tonic-gate 
436*0Sstevel@tonic-gate 	mutex_enter(lhp->lh_lock);
437*0Sstevel@tonic-gate 	if (lep->le_prev)
438*0Sstevel@tonic-gate 		lep->le_prev->le_next = lep->le_next;
439*0Sstevel@tonic-gate 	if (lep->le_next)
440*0Sstevel@tonic-gate 		lep->le_next->le_prev = lep->le_prev;
441*0Sstevel@tonic-gate 	if (lhp->lh_events == lep)
442*0Sstevel@tonic-gate 		lhp->lh_events = lep->le_next;
443*0Sstevel@tonic-gate 	mutex_exit(lhp->lh_lock);
444*0Sstevel@tonic-gate 
445*0Sstevel@tonic-gate }
446*0Sstevel@tonic-gate 
447*0Sstevel@tonic-gate static void
448*0Sstevel@tonic-gate i_ldi_callback(dev_info_t *dip, ddi_eventcookie_t event_cookie,
449*0Sstevel@tonic-gate     void *arg, void *bus_impldata)
450*0Sstevel@tonic-gate {
451*0Sstevel@tonic-gate 	ldi_event_t *lep = (ldi_event_t *)arg;
452*0Sstevel@tonic-gate 
453*0Sstevel@tonic-gate 	ASSERT(lep != NULL);
454*0Sstevel@tonic-gate 
455*0Sstevel@tonic-gate 	LDI_EVENTCB((CE_NOTE, "%s: dip=0x%p, "
456*0Sstevel@tonic-gate 	    "event_cookie=0x%p, ldi_eventp=0x%p", "i_ldi_callback",
457*0Sstevel@tonic-gate 	    (void *)dip, (void *)event_cookie, (void *)lep));
458*0Sstevel@tonic-gate 
459*0Sstevel@tonic-gate 	lep->le_handler(lep->le_lhp, event_cookie, lep->le_arg, bus_impldata);
460*0Sstevel@tonic-gate }
461*0Sstevel@tonic-gate 
462*0Sstevel@tonic-gate /*
463*0Sstevel@tonic-gate  * LDI open helper functions
464*0Sstevel@tonic-gate  */
465*0Sstevel@tonic-gate 
466*0Sstevel@tonic-gate /* get a vnode to a device by dev_t and otyp */
467*0Sstevel@tonic-gate static int
468*0Sstevel@tonic-gate ldi_vp_from_dev(dev_t dev, int otyp, vnode_t **vpp)
469*0Sstevel@tonic-gate {
470*0Sstevel@tonic-gate 	dev_info_t		*dip;
471*0Sstevel@tonic-gate 	vnode_t			*vp;
472*0Sstevel@tonic-gate 
473*0Sstevel@tonic-gate 	/* sanity check required input parameters */
474*0Sstevel@tonic-gate 	if ((dev == DDI_DEV_T_NONE) || (!OTYP_VALID(otyp)) || (vpp == NULL))
475*0Sstevel@tonic-gate 		return (EINVAL);
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate 	if ((dip = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
478*0Sstevel@tonic-gate 		return (ENODEV);
479*0Sstevel@tonic-gate 
480*0Sstevel@tonic-gate 	if (STREAMSTAB(getmajor(dev)) && (otyp != OTYP_CHR)) {
481*0Sstevel@tonic-gate 		ddi_release_devi(dip);  /* from e_ddi_hold_devi_by_dev */
482*0Sstevel@tonic-gate 		return (ENXIO);
483*0Sstevel@tonic-gate 	}
484*0Sstevel@tonic-gate 
485*0Sstevel@tonic-gate 	vp = makespecvp(dev, OTYP_TO_VTYP(otyp));
486*0Sstevel@tonic-gate 	spec_assoc_vp_with_devi(vp, dip);
487*0Sstevel@tonic-gate 	ddi_release_devi(dip);  /* from e_ddi_hold_devi_by_dev */
488*0Sstevel@tonic-gate 
489*0Sstevel@tonic-gate 	*vpp = vp;
490*0Sstevel@tonic-gate 	return (0);
491*0Sstevel@tonic-gate }
492*0Sstevel@tonic-gate 
493*0Sstevel@tonic-gate /* get a vnode to a device by pathname */
494*0Sstevel@tonic-gate static int
495*0Sstevel@tonic-gate ldi_vp_from_name(char *path, vnode_t **vpp)
496*0Sstevel@tonic-gate {
497*0Sstevel@tonic-gate 	vnode_t			*vp = NULL;
498*0Sstevel@tonic-gate 	int			ret;
499*0Sstevel@tonic-gate 
500*0Sstevel@tonic-gate 	/* sanity check required input parameters */
501*0Sstevel@tonic-gate 	if ((path == NULL) || (vpp == NULL))
502*0Sstevel@tonic-gate 		return (EINVAL);
503*0Sstevel@tonic-gate 
504*0Sstevel@tonic-gate 	if (modrootloaded) {
505*0Sstevel@tonic-gate 		cred_t *saved_cred = curthread->t_cred;
506*0Sstevel@tonic-gate 
507*0Sstevel@tonic-gate 		/* we don't want lookupname to fail because of credentials */
508*0Sstevel@tonic-gate 		curthread->t_cred = kcred;
509*0Sstevel@tonic-gate 		ret = lookupname(path, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
510*0Sstevel@tonic-gate 		curthread->t_cred = saved_cred;
511*0Sstevel@tonic-gate 
512*0Sstevel@tonic-gate 		if (ret == 0) {
513*0Sstevel@tonic-gate 			if (!vn_matchops(vp, spec_getvnodeops()) ||
514*0Sstevel@tonic-gate 			    !VTYP_VALID(vp->v_type)) {
515*0Sstevel@tonic-gate 				VN_RELE(vp);
516*0Sstevel@tonic-gate 				return (ENXIO);
517*0Sstevel@tonic-gate 			}
518*0Sstevel@tonic-gate 		}
519*0Sstevel@tonic-gate 	}
520*0Sstevel@tonic-gate 
521*0Sstevel@tonic-gate 	if (vp == NULL) {
522*0Sstevel@tonic-gate 		dev_info_t	*dip;
523*0Sstevel@tonic-gate 		dev_t		dev;
524*0Sstevel@tonic-gate 		int		spec_type;
525*0Sstevel@tonic-gate 
526*0Sstevel@tonic-gate 		/*
527*0Sstevel@tonic-gate 		 * Root is not mounted, the minor node is not specified,
528*0Sstevel@tonic-gate 		 * or an OBP path has been specified.
529*0Sstevel@tonic-gate 		 */
530*0Sstevel@tonic-gate 
531*0Sstevel@tonic-gate 		/*
532*0Sstevel@tonic-gate 		 * Determine if path can be pruned to produce an
533*0Sstevel@tonic-gate 		 * OBP or devfs path for resolve_pathname.
534*0Sstevel@tonic-gate 		 */
535*0Sstevel@tonic-gate 		if (strncmp(path, "/devices/", 9) == 0)
536*0Sstevel@tonic-gate 			path += strlen("/devices");
537*0Sstevel@tonic-gate 
538*0Sstevel@tonic-gate 		/*
539*0Sstevel@tonic-gate 		 * if no minor node was specified the DEFAULT minor node
540*0Sstevel@tonic-gate 		 * will be returned.  if there is no DEFAULT minor node
541*0Sstevel@tonic-gate 		 * one will be fabricated of type S_IFCHR with the minor
542*0Sstevel@tonic-gate 		 * number equal to the instance number.
543*0Sstevel@tonic-gate 		 */
544*0Sstevel@tonic-gate 		ret = resolve_pathname(path, &dip, &dev, &spec_type);
545*0Sstevel@tonic-gate 		if (ret != 0)
546*0Sstevel@tonic-gate 			return (ENODEV);
547*0Sstevel@tonic-gate 
548*0Sstevel@tonic-gate 		ASSERT(STYP_VALID(spec_type));
549*0Sstevel@tonic-gate 		vp = makespecvp(dev, STYP_TO_VTYP(spec_type));
550*0Sstevel@tonic-gate 		spec_assoc_vp_with_devi(vp, dip);
551*0Sstevel@tonic-gate 		ddi_release_devi(dip);
552*0Sstevel@tonic-gate 	}
553*0Sstevel@tonic-gate 
554*0Sstevel@tonic-gate 	*vpp = vp;
555*0Sstevel@tonic-gate 	return (0);
556*0Sstevel@tonic-gate }
557*0Sstevel@tonic-gate 
558*0Sstevel@tonic-gate static int
559*0Sstevel@tonic-gate ldi_devid_match(ddi_devid_t devid, dev_info_t *dip, dev_t dev)
560*0Sstevel@tonic-gate {
561*0Sstevel@tonic-gate 	char		*devidstr;
562*0Sstevel@tonic-gate 	ddi_prop_t	*propp;
563*0Sstevel@tonic-gate 
564*0Sstevel@tonic-gate 	/* convert devid as a string property */
565*0Sstevel@tonic-gate 	if ((devidstr = ddi_devid_str_encode(devid, NULL)) == NULL)
566*0Sstevel@tonic-gate 		return (0);
567*0Sstevel@tonic-gate 
568*0Sstevel@tonic-gate 	/*
569*0Sstevel@tonic-gate 	 * Search for the devid.  For speed and ease in locking this
570*0Sstevel@tonic-gate 	 * code directly uses the property implementation.  See
571*0Sstevel@tonic-gate 	 * ddi_common_devid_to_devlist() for a comment as to why.
572*0Sstevel@tonic-gate 	 */
573*0Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
574*0Sstevel@tonic-gate 
575*0Sstevel@tonic-gate 	/* check if there is a DDI_DEV_T_NONE devid property */
576*0Sstevel@tonic-gate 	propp = i_ddi_prop_search(DDI_DEV_T_NONE,
577*0Sstevel@tonic-gate 	    DEVID_PROP_NAME, DEVID_PROP_FLAGS, &DEVI(dip)->devi_hw_prop_ptr);
578*0Sstevel@tonic-gate 	if (propp != NULL) {
579*0Sstevel@tonic-gate 		if (ddi_devid_str_compare(propp->prop_val, devidstr) == 0) {
580*0Sstevel@tonic-gate 			/* a DDI_DEV_T_NONE devid exists and matchs */
581*0Sstevel@tonic-gate 			mutex_exit(&(DEVI(dip)->devi_lock));
582*0Sstevel@tonic-gate 			ddi_devid_str_free(devidstr);
583*0Sstevel@tonic-gate 			return (1);
584*0Sstevel@tonic-gate 		} else {
585*0Sstevel@tonic-gate 			/* a DDI_DEV_T_NONE devid exists and doesn't match */
586*0Sstevel@tonic-gate 			mutex_exit(&(DEVI(dip)->devi_lock));
587*0Sstevel@tonic-gate 			ddi_devid_str_free(devidstr);
588*0Sstevel@tonic-gate 			return (0);
589*0Sstevel@tonic-gate 		}
590*0Sstevel@tonic-gate 	}
591*0Sstevel@tonic-gate 
592*0Sstevel@tonic-gate 	/* check if there is a devt specific devid property */
593*0Sstevel@tonic-gate 	propp = i_ddi_prop_search(dev,
594*0Sstevel@tonic-gate 	    DEVID_PROP_NAME, DEVID_PROP_FLAGS, &(DEVI(dip)->devi_hw_prop_ptr));
595*0Sstevel@tonic-gate 	if (propp != NULL) {
596*0Sstevel@tonic-gate 		if (ddi_devid_str_compare(propp->prop_val, devidstr) == 0) {
597*0Sstevel@tonic-gate 			/* a devt specific devid exists and matchs */
598*0Sstevel@tonic-gate 			mutex_exit(&(DEVI(dip)->devi_lock));
599*0Sstevel@tonic-gate 			ddi_devid_str_free(devidstr);
600*0Sstevel@tonic-gate 			return (1);
601*0Sstevel@tonic-gate 		} else {
602*0Sstevel@tonic-gate 			/* a devt specific devid exists and doesn't match */
603*0Sstevel@tonic-gate 			mutex_exit(&(DEVI(dip)->devi_lock));
604*0Sstevel@tonic-gate 			ddi_devid_str_free(devidstr);
605*0Sstevel@tonic-gate 			return (0);
606*0Sstevel@tonic-gate 		}
607*0Sstevel@tonic-gate 	}
608*0Sstevel@tonic-gate 
609*0Sstevel@tonic-gate 	/* we didn't find any devids associated with the device */
610*0Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
611*0Sstevel@tonic-gate 	ddi_devid_str_free(devidstr);
612*0Sstevel@tonic-gate 	return (0);
613*0Sstevel@tonic-gate }
614*0Sstevel@tonic-gate 
615*0Sstevel@tonic-gate /* get a handle to a device by devid and minor name */
616*0Sstevel@tonic-gate static int
617*0Sstevel@tonic-gate ldi_vp_from_devid(ddi_devid_t devid, char *minor_name, vnode_t **vpp)
618*0Sstevel@tonic-gate {
619*0Sstevel@tonic-gate 	dev_info_t		*dip;
620*0Sstevel@tonic-gate 	vnode_t			*vp;
621*0Sstevel@tonic-gate 	int			ret, i, ndevs, styp;
622*0Sstevel@tonic-gate 	dev_t			dev, *devs;
623*0Sstevel@tonic-gate 
624*0Sstevel@tonic-gate 	/* sanity check required input parameters */
625*0Sstevel@tonic-gate 	if ((devid == NULL) || (minor_name == NULL) || (vpp == NULL))
626*0Sstevel@tonic-gate 		return (EINVAL);
627*0Sstevel@tonic-gate 
628*0Sstevel@tonic-gate 	ret = ddi_lyr_devid_to_devlist(devid, minor_name, &ndevs, &devs);
629*0Sstevel@tonic-gate 	if ((ret != DDI_SUCCESS) || (ndevs <= 0))
630*0Sstevel@tonic-gate 		return (ENODEV);
631*0Sstevel@tonic-gate 
632*0Sstevel@tonic-gate 	for (i = 0; i < ndevs; i++) {
633*0Sstevel@tonic-gate 		dev = devs[i];
634*0Sstevel@tonic-gate 
635*0Sstevel@tonic-gate 		if ((dip = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
636*0Sstevel@tonic-gate 			continue;
637*0Sstevel@tonic-gate 
638*0Sstevel@tonic-gate 		/*
639*0Sstevel@tonic-gate 		 * now we have to verify that the devid of the disk
640*0Sstevel@tonic-gate 		 * still matches what was requested.
641*0Sstevel@tonic-gate 		 *
642*0Sstevel@tonic-gate 		 * we have to do this because the devid could have
643*0Sstevel@tonic-gate 		 * changed between the call to ddi_lyr_devid_to_devlist()
644*0Sstevel@tonic-gate 		 * and e_ddi_hold_devi_by_dev().  this is because when
645*0Sstevel@tonic-gate 		 * ddi_lyr_devid_to_devlist() returns a list of devts
646*0Sstevel@tonic-gate 		 * there is no kind of hold on those devts so a device
647*0Sstevel@tonic-gate 		 * could have been replaced out from under us in the
648*0Sstevel@tonic-gate 		 * interim.
649*0Sstevel@tonic-gate 		 */
650*0Sstevel@tonic-gate 		if ((i_ddi_minorname_to_devtspectype(dip, minor_name,
651*0Sstevel@tonic-gate 		    NULL, &styp) == DDI_SUCCESS) &&
652*0Sstevel@tonic-gate 		    ldi_devid_match(devid, dip, dev))
653*0Sstevel@tonic-gate 			break;
654*0Sstevel@tonic-gate 
655*0Sstevel@tonic-gate 		ddi_release_devi(dip);	/* from e_ddi_hold_devi_by_dev() */
656*0Sstevel@tonic-gate 	}
657*0Sstevel@tonic-gate 
658*0Sstevel@tonic-gate 	ddi_lyr_free_devlist(devs, ndevs);
659*0Sstevel@tonic-gate 
660*0Sstevel@tonic-gate 	if (i == ndevs)
661*0Sstevel@tonic-gate 		return (ENODEV);
662*0Sstevel@tonic-gate 
663*0Sstevel@tonic-gate 	ASSERT(STYP_VALID(styp));
664*0Sstevel@tonic-gate 	vp = makespecvp(dev, STYP_TO_VTYP(styp));
665*0Sstevel@tonic-gate 	spec_assoc_vp_with_devi(vp, dip);
666*0Sstevel@tonic-gate 	ddi_release_devi(dip);		/* from e_ddi_hold_devi_by_dev */
667*0Sstevel@tonic-gate 
668*0Sstevel@tonic-gate 	*vpp = vp;
669*0Sstevel@tonic-gate 	return (0);
670*0Sstevel@tonic-gate }
671*0Sstevel@tonic-gate 
672*0Sstevel@tonic-gate /* given a vnode, open a device */
673*0Sstevel@tonic-gate static int
674*0Sstevel@tonic-gate ldi_open_by_vp(vnode_t **vpp, int flag, cred_t *cr,
675*0Sstevel@tonic-gate     ldi_handle_t *lhp, struct ldi_ident *li)
676*0Sstevel@tonic-gate {
677*0Sstevel@tonic-gate 	struct ldi_handle	*nlhp;
678*0Sstevel@tonic-gate 	vnode_t			*vp;
679*0Sstevel@tonic-gate 	int			err;
680*0Sstevel@tonic-gate 
681*0Sstevel@tonic-gate 	ASSERT((vpp != NULL) && (*vpp != NULL));
682*0Sstevel@tonic-gate 	ASSERT((lhp != NULL) && (li != NULL));
683*0Sstevel@tonic-gate 
684*0Sstevel@tonic-gate 	vp = *vpp;
685*0Sstevel@tonic-gate 	/* if the vnode passed in is not a device, then bail */
686*0Sstevel@tonic-gate 	if (!vn_matchops(vp, spec_getvnodeops()) || !VTYP_VALID(vp->v_type))
687*0Sstevel@tonic-gate 		return (ENXIO);
688*0Sstevel@tonic-gate 
689*0Sstevel@tonic-gate 	/*
690*0Sstevel@tonic-gate 	 * the caller may have specified a node that
691*0Sstevel@tonic-gate 	 * doesn't have cb_ops defined.  the ldi doesn't yet
692*0Sstevel@tonic-gate 	 * support opening devices without a valid cb_ops.
693*0Sstevel@tonic-gate 	 */
694*0Sstevel@tonic-gate 	if (devopsp[getmajor(vp->v_rdev)]->devo_cb_ops == NULL)
695*0Sstevel@tonic-gate 		return (ENXIO);
696*0Sstevel@tonic-gate 
697*0Sstevel@tonic-gate 	/* open the device */
698*0Sstevel@tonic-gate 	if ((err = VOP_OPEN(&vp, flag | FKLYR, cr)) != 0)
699*0Sstevel@tonic-gate 		return (err);
700*0Sstevel@tonic-gate 
701*0Sstevel@tonic-gate 	/* possible clone open, make sure that we still have a spec node */
702*0Sstevel@tonic-gate 	ASSERT(vn_matchops(vp, spec_getvnodeops()));
703*0Sstevel@tonic-gate 
704*0Sstevel@tonic-gate 	nlhp = handle_alloc(vp, li);
705*0Sstevel@tonic-gate 
706*0Sstevel@tonic-gate 	if (vp != *vpp) {
707*0Sstevel@tonic-gate 		/*
708*0Sstevel@tonic-gate 		 * allocating the layered handle took a new hold on the vnode
709*0Sstevel@tonic-gate 		 * so we can release the hold that was returned by the clone
710*0Sstevel@tonic-gate 		 * open
711*0Sstevel@tonic-gate 		 */
712*0Sstevel@tonic-gate 		LDI_OPENCLOSE((CE_WARN, "%s: lh=0x%p",
713*0Sstevel@tonic-gate 			"ldi clone open", (void *)nlhp));
714*0Sstevel@tonic-gate 	} else {
715*0Sstevel@tonic-gate 		LDI_OPENCLOSE((CE_WARN, "%s: lh=0x%p",
716*0Sstevel@tonic-gate 			"ldi open", (void *)nlhp));
717*0Sstevel@tonic-gate 	}
718*0Sstevel@tonic-gate 
719*0Sstevel@tonic-gate 	/* Flush back any dirty pages associated with the device. */
720*0Sstevel@tonic-gate 	if (nlhp->lh_type & LH_CBDEV) {
721*0Sstevel@tonic-gate 		vnode_t	*cvp = common_specvp(nlhp->lh_vp);
722*0Sstevel@tonic-gate 		dev_t	dev = cvp->v_rdev;
723*0Sstevel@tonic-gate 
724*0Sstevel@tonic-gate 		(void) VOP_PUTPAGE(cvp, 0, 0, B_INVAL, kcred);
725*0Sstevel@tonic-gate 		bflush(dev);
726*0Sstevel@tonic-gate 	}
727*0Sstevel@tonic-gate 
728*0Sstevel@tonic-gate 	*vpp = vp;
729*0Sstevel@tonic-gate 	*lhp = (ldi_handle_t)nlhp;
730*0Sstevel@tonic-gate 	return (0);
731*0Sstevel@tonic-gate }
732*0Sstevel@tonic-gate 
733*0Sstevel@tonic-gate /* Call a drivers prop_op(9E) interface */
734*0Sstevel@tonic-gate static int
735*0Sstevel@tonic-gate i_ldi_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
736*0Sstevel@tonic-gate     int flags, char *name, caddr_t valuep, int *lengthp)
737*0Sstevel@tonic-gate {
738*0Sstevel@tonic-gate 	struct dev_ops	*ops = NULL;
739*0Sstevel@tonic-gate 	int		res;
740*0Sstevel@tonic-gate 
741*0Sstevel@tonic-gate 	ASSERT((dip != NULL) && (name != NULL));
742*0Sstevel@tonic-gate 	ASSERT((prop_op == PROP_LEN) || (valuep != NULL));
743*0Sstevel@tonic-gate 	ASSERT(lengthp != NULL);
744*0Sstevel@tonic-gate 
745*0Sstevel@tonic-gate 	/*
746*0Sstevel@tonic-gate 	 * we can only be invoked after a driver has been opened and
747*0Sstevel@tonic-gate 	 * someone has a layered handle to it, so there had better be
748*0Sstevel@tonic-gate 	 * a valid ops vector.
749*0Sstevel@tonic-gate 	 */
750*0Sstevel@tonic-gate 	ops = DEVI(dip)->devi_ops;
751*0Sstevel@tonic-gate 	ASSERT(ops && ops->devo_cb_ops);
752*0Sstevel@tonic-gate 
753*0Sstevel@tonic-gate 	/*
754*0Sstevel@tonic-gate 	 * Some nexus drivers incorrectly set cb_prop_op to nodev,
755*0Sstevel@tonic-gate 	 * nulldev or even NULL.
756*0Sstevel@tonic-gate 	 */
757*0Sstevel@tonic-gate 	if ((ops->devo_cb_ops->cb_prop_op == nodev) ||
758*0Sstevel@tonic-gate 	    (ops->devo_cb_ops->cb_prop_op == nulldev) ||
759*0Sstevel@tonic-gate 	    (ops->devo_cb_ops->cb_prop_op == NULL)) {
760*0Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
761*0Sstevel@tonic-gate 	}
762*0Sstevel@tonic-gate 
763*0Sstevel@tonic-gate 	/* check if this is actually DDI_DEV_T_ANY query */
764*0Sstevel@tonic-gate 	if (flags & LDI_DEV_T_ANY) {
765*0Sstevel@tonic-gate 		flags &= ~LDI_DEV_T_ANY;
766*0Sstevel@tonic-gate 		dev = DDI_DEV_T_ANY;
767*0Sstevel@tonic-gate 	}
768*0Sstevel@tonic-gate 
769*0Sstevel@tonic-gate 	res = cdev_prop_op(dev, dip, prop_op, flags, name, valuep, lengthp);
770*0Sstevel@tonic-gate 	return (res);
771*0Sstevel@tonic-gate }
772*0Sstevel@tonic-gate 
773*0Sstevel@tonic-gate static void
774*0Sstevel@tonic-gate i_ldi_prop_op_free(struct prop_driver_data *pdd)
775*0Sstevel@tonic-gate {
776*0Sstevel@tonic-gate 	kmem_free(pdd, pdd->pdd_size);
777*0Sstevel@tonic-gate }
778*0Sstevel@tonic-gate 
779*0Sstevel@tonic-gate static caddr_t
780*0Sstevel@tonic-gate i_ldi_prop_op_alloc(int prop_len)
781*0Sstevel@tonic-gate {
782*0Sstevel@tonic-gate 	struct prop_driver_data	*pdd;
783*0Sstevel@tonic-gate 	int			pdd_size;
784*0Sstevel@tonic-gate 
785*0Sstevel@tonic-gate 	pdd_size = sizeof (struct prop_driver_data) + prop_len;
786*0Sstevel@tonic-gate 	pdd = kmem_alloc(pdd_size, KM_SLEEP);
787*0Sstevel@tonic-gate 	pdd->pdd_size = pdd_size;
788*0Sstevel@tonic-gate 	pdd->pdd_prop_free = i_ldi_prop_op_free;
789*0Sstevel@tonic-gate 	return ((caddr_t)&pdd[1]);
790*0Sstevel@tonic-gate }
791*0Sstevel@tonic-gate 
792*0Sstevel@tonic-gate /*
793*0Sstevel@tonic-gate  * i_ldi_prop_op_typed() is a wrapper for i_ldi_prop_op that is used
794*0Sstevel@tonic-gate  * by the typed ldi property lookup interfaces.
795*0Sstevel@tonic-gate  */
796*0Sstevel@tonic-gate static int
797*0Sstevel@tonic-gate i_ldi_prop_op_typed(dev_t dev, dev_info_t *dip, int flags, char *name,
798*0Sstevel@tonic-gate     caddr_t *datap, int *lengthp, int elem_size)
799*0Sstevel@tonic-gate {
800*0Sstevel@tonic-gate 	caddr_t	prop_val;
801*0Sstevel@tonic-gate 	int	prop_len, res;
802*0Sstevel@tonic-gate 
803*0Sstevel@tonic-gate 	ASSERT((dip != NULL) && (name != NULL));
804*0Sstevel@tonic-gate 	ASSERT((datap != NULL) && (lengthp != NULL));
805*0Sstevel@tonic-gate 
806*0Sstevel@tonic-gate 	/*
807*0Sstevel@tonic-gate 	 * first call the drivers prop_op() interface to allow it
808*0Sstevel@tonic-gate 	 * it to override default property values.
809*0Sstevel@tonic-gate 	 */
810*0Sstevel@tonic-gate 	res = i_ldi_prop_op(dev, dip, PROP_LEN,
811*0Sstevel@tonic-gate 	    flags | DDI_PROP_DYNAMIC, name, NULL, &prop_len);
812*0Sstevel@tonic-gate 	if (res != DDI_PROP_SUCCESS)
813*0Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
814*0Sstevel@tonic-gate 
815*0Sstevel@tonic-gate 	/* sanity check the property length */
816*0Sstevel@tonic-gate 	if (prop_len == 0) {
817*0Sstevel@tonic-gate 		/*
818*0Sstevel@tonic-gate 		 * the ddi typed interfaces don't allow a drivers to
819*0Sstevel@tonic-gate 		 * create properties with a length of 0.  so we should
820*0Sstevel@tonic-gate 		 * prevent drivers from returning 0 length dynamic
821*0Sstevel@tonic-gate 		 * properties for typed property lookups.
822*0Sstevel@tonic-gate 		 */
823*0Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
824*0Sstevel@tonic-gate 	}
825*0Sstevel@tonic-gate 
826*0Sstevel@tonic-gate 	/* sanity check the property length against the element size */
827*0Sstevel@tonic-gate 	if (elem_size && ((prop_len % elem_size) != 0))
828*0Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
829*0Sstevel@tonic-gate 
830*0Sstevel@tonic-gate 	/*
831*0Sstevel@tonic-gate 	 * got it.  now allocate a prop_driver_data struct so that the
832*0Sstevel@tonic-gate 	 * user can free the property via ddi_prop_free().
833*0Sstevel@tonic-gate 	 */
834*0Sstevel@tonic-gate 	prop_val = i_ldi_prop_op_alloc(prop_len);
835*0Sstevel@tonic-gate 
836*0Sstevel@tonic-gate 	/* lookup the property again, this time get the value */
837*0Sstevel@tonic-gate 	res = i_ldi_prop_op(dev, dip, PROP_LEN_AND_VAL_BUF,
838*0Sstevel@tonic-gate 	    flags | DDI_PROP_DYNAMIC, name, prop_val, &prop_len);
839*0Sstevel@tonic-gate 	if (res != DDI_PROP_SUCCESS) {
840*0Sstevel@tonic-gate 		ddi_prop_free(prop_val);
841*0Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
842*0Sstevel@tonic-gate 	}
843*0Sstevel@tonic-gate 
844*0Sstevel@tonic-gate 	/* sanity check the property length */
845*0Sstevel@tonic-gate 	if (prop_len == 0) {
846*0Sstevel@tonic-gate 		ddi_prop_free(prop_val);
847*0Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
848*0Sstevel@tonic-gate 	}
849*0Sstevel@tonic-gate 
850*0Sstevel@tonic-gate 	/* sanity check the property length against the element size */
851*0Sstevel@tonic-gate 	if (elem_size && ((prop_len % elem_size) != 0)) {
852*0Sstevel@tonic-gate 		ddi_prop_free(prop_val);
853*0Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
854*0Sstevel@tonic-gate 	}
855*0Sstevel@tonic-gate 
856*0Sstevel@tonic-gate 	/*
857*0Sstevel@tonic-gate 	 * return the prop_driver_data struct and, optionally, the length
858*0Sstevel@tonic-gate 	 * of the data.
859*0Sstevel@tonic-gate 	 */
860*0Sstevel@tonic-gate 	*datap = prop_val;
861*0Sstevel@tonic-gate 	*lengthp = prop_len;
862*0Sstevel@tonic-gate 
863*0Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
864*0Sstevel@tonic-gate }
865*0Sstevel@tonic-gate 
866*0Sstevel@tonic-gate /*
867*0Sstevel@tonic-gate  * i_check_string looks at a string property and makes sure its
868*0Sstevel@tonic-gate  * a valid null terminated string
869*0Sstevel@tonic-gate  */
870*0Sstevel@tonic-gate static int
871*0Sstevel@tonic-gate i_check_string(char *str, int prop_len)
872*0Sstevel@tonic-gate {
873*0Sstevel@tonic-gate 	int i;
874*0Sstevel@tonic-gate 
875*0Sstevel@tonic-gate 	ASSERT(str != NULL);
876*0Sstevel@tonic-gate 
877*0Sstevel@tonic-gate 	for (i = 0; i < prop_len; i++) {
878*0Sstevel@tonic-gate 		if (str[i] == '\0')
879*0Sstevel@tonic-gate 			return (0);
880*0Sstevel@tonic-gate 	}
881*0Sstevel@tonic-gate 	return (1);
882*0Sstevel@tonic-gate }
883*0Sstevel@tonic-gate 
884*0Sstevel@tonic-gate /*
885*0Sstevel@tonic-gate  * i_pack_string_array takes a a string array property that is represented
886*0Sstevel@tonic-gate  * as a concatination of strings (with the NULL character included for
887*0Sstevel@tonic-gate  * each string) and converts it into a format that can be returned by
888*0Sstevel@tonic-gate  * ldi_prop_lookup_string_array.
889*0Sstevel@tonic-gate  */
890*0Sstevel@tonic-gate static int
891*0Sstevel@tonic-gate i_pack_string_array(char *str_concat, int prop_len,
892*0Sstevel@tonic-gate     char ***str_arrayp, int *nelemp)
893*0Sstevel@tonic-gate {
894*0Sstevel@tonic-gate 	int i, nelem, pack_size;
895*0Sstevel@tonic-gate 	char **str_array, *strptr;
896*0Sstevel@tonic-gate 
897*0Sstevel@tonic-gate 	/*
898*0Sstevel@tonic-gate 	 * first we need to sanity check the input string array.
899*0Sstevel@tonic-gate 	 * in essence this can be done my making sure that the last
900*0Sstevel@tonic-gate 	 * character of the array passed in is null.  (meaning the last
901*0Sstevel@tonic-gate 	 * string in the array is NULL terminated.
902*0Sstevel@tonic-gate 	 */
903*0Sstevel@tonic-gate 	if (str_concat[prop_len - 1] != '\0')
904*0Sstevel@tonic-gate 		return (1);
905*0Sstevel@tonic-gate 
906*0Sstevel@tonic-gate 	/* now let's count the number of strings in the array */
907*0Sstevel@tonic-gate 	for (nelem = i = 0; i < prop_len; i++)
908*0Sstevel@tonic-gate 		if (str_concat[i] == '\0')
909*0Sstevel@tonic-gate 			nelem++;
910*0Sstevel@tonic-gate 	ASSERT(nelem >= 1);
911*0Sstevel@tonic-gate 
912*0Sstevel@tonic-gate 	/* now let's allocate memory for the new packed property */
913*0Sstevel@tonic-gate 	pack_size = (sizeof (char *) * (nelem + 1)) + prop_len;
914*0Sstevel@tonic-gate 	str_array = (char **)i_ldi_prop_op_alloc(pack_size);
915*0Sstevel@tonic-gate 
916*0Sstevel@tonic-gate 	/* let's copy the actual string data into the new property */
917*0Sstevel@tonic-gate 	strptr = (char *)&(str_array[nelem + 1]);
918*0Sstevel@tonic-gate 	bcopy(str_concat, strptr, prop_len);
919*0Sstevel@tonic-gate 
920*0Sstevel@tonic-gate 	/* now initialize the string array pointers */
921*0Sstevel@tonic-gate 	for (i = 0; i < nelem; i++) {
922*0Sstevel@tonic-gate 		str_array[i] = strptr;
923*0Sstevel@tonic-gate 		strptr += strlen(strptr) + 1;
924*0Sstevel@tonic-gate 	}
925*0Sstevel@tonic-gate 	str_array[nelem] = NULL;
926*0Sstevel@tonic-gate 
927*0Sstevel@tonic-gate 	/* set the return values */
928*0Sstevel@tonic-gate 	*str_arrayp = str_array;
929*0Sstevel@tonic-gate 	*nelemp = nelem;
930*0Sstevel@tonic-gate 
931*0Sstevel@tonic-gate 	return (0);
932*0Sstevel@tonic-gate }
933*0Sstevel@tonic-gate 
934*0Sstevel@tonic-gate 
935*0Sstevel@tonic-gate /*
936*0Sstevel@tonic-gate  * LDI Project private device usage interfaces
937*0Sstevel@tonic-gate  */
938*0Sstevel@tonic-gate 
939*0Sstevel@tonic-gate /*
940*0Sstevel@tonic-gate  * Get a count of how many devices are currentl open by different consumers
941*0Sstevel@tonic-gate  */
942*0Sstevel@tonic-gate int
943*0Sstevel@tonic-gate ldi_usage_count()
944*0Sstevel@tonic-gate {
945*0Sstevel@tonic-gate 	return (ldi_handle_hash_count);
946*0Sstevel@tonic-gate }
947*0Sstevel@tonic-gate 
948*0Sstevel@tonic-gate static void
949*0Sstevel@tonic-gate ldi_usage_walker_tgt_helper(ldi_usage_t *ldi_usage, vnode_t *vp)
950*0Sstevel@tonic-gate {
951*0Sstevel@tonic-gate 	dev_info_t	*dip;
952*0Sstevel@tonic-gate 	dev_t		dev;
953*0Sstevel@tonic-gate 
954*0Sstevel@tonic-gate 	ASSERT(STYP_VALID(VTYP_TO_STYP(vp->v_type)));
955*0Sstevel@tonic-gate 
956*0Sstevel@tonic-gate 	/* get the target devt */
957*0Sstevel@tonic-gate 	dev = vp->v_rdev;
958*0Sstevel@tonic-gate 
959*0Sstevel@tonic-gate 	/* try to get the target dip */
960*0Sstevel@tonic-gate 	dip = VTOCS(vp)->s_dip;
961*0Sstevel@tonic-gate 	if (dip != NULL) {
962*0Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
963*0Sstevel@tonic-gate 	} else if (dev != DDI_DEV_T_NONE) {
964*0Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
965*0Sstevel@tonic-gate 	}
966*0Sstevel@tonic-gate 
967*0Sstevel@tonic-gate 	/* set the target information */
968*0Sstevel@tonic-gate 	ldi_usage->tgt_name = mod_major_to_name(getmajor(dev));
969*0Sstevel@tonic-gate 	ldi_usage->tgt_modid = mod_name_to_modid(ldi_usage->tgt_name);
970*0Sstevel@tonic-gate 	ldi_usage->tgt_devt = dev;
971*0Sstevel@tonic-gate 	ldi_usage->tgt_spec_type = VTYP_TO_STYP(vp->v_type);
972*0Sstevel@tonic-gate 	ldi_usage->tgt_dip = dip;
973*0Sstevel@tonic-gate }
974*0Sstevel@tonic-gate 
975*0Sstevel@tonic-gate 
976*0Sstevel@tonic-gate static int
977*0Sstevel@tonic-gate ldi_usage_walker_helper(struct ldi_ident *lip, vnode_t *vp,
978*0Sstevel@tonic-gate     void *arg, int (*callback)(const ldi_usage_t *, void *))
979*0Sstevel@tonic-gate {
980*0Sstevel@tonic-gate 	ldi_usage_t	ldi_usage;
981*0Sstevel@tonic-gate 	struct devnames	*dnp;
982*0Sstevel@tonic-gate 	dev_info_t	*dip;
983*0Sstevel@tonic-gate 	major_t		major;
984*0Sstevel@tonic-gate 	dev_t		dev;
985*0Sstevel@tonic-gate 	int		ret = LDI_USAGE_CONTINUE;
986*0Sstevel@tonic-gate 
987*0Sstevel@tonic-gate 	/* set the target device information */
988*0Sstevel@tonic-gate 	ldi_usage_walker_tgt_helper(&ldi_usage, vp);
989*0Sstevel@tonic-gate 
990*0Sstevel@tonic-gate 	/* get the source devt */
991*0Sstevel@tonic-gate 	dev = lip->li_dev;
992*0Sstevel@tonic-gate 
993*0Sstevel@tonic-gate 	/* try to get the source dip */
994*0Sstevel@tonic-gate 	dip = lip->li_dip;
995*0Sstevel@tonic-gate 	if (dip != NULL) {
996*0Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
997*0Sstevel@tonic-gate 	} else if (dev != DDI_DEV_T_NONE) {
998*0Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
999*0Sstevel@tonic-gate 	}
1000*0Sstevel@tonic-gate 
1001*0Sstevel@tonic-gate 	/* set the valid source information */
1002*0Sstevel@tonic-gate 	ldi_usage.src_modid = lip->li_modid;
1003*0Sstevel@tonic-gate 	ldi_usage.src_name = lip->li_modname;
1004*0Sstevel@tonic-gate 	ldi_usage.src_devt = dev;
1005*0Sstevel@tonic-gate 	ldi_usage.src_dip = dip;
1006*0Sstevel@tonic-gate 
1007*0Sstevel@tonic-gate 	/*
1008*0Sstevel@tonic-gate 	 * if the source ident represents either:
1009*0Sstevel@tonic-gate 	 *
1010*0Sstevel@tonic-gate 	 * - a kernel module (and not a device or device driver)
1011*0Sstevel@tonic-gate 	 * - a device node
1012*0Sstevel@tonic-gate 	 *
1013*0Sstevel@tonic-gate 	 * then we currently have all the info we need to report the
1014*0Sstevel@tonic-gate 	 * usage information so invoke the callback function.
1015*0Sstevel@tonic-gate 	 */
1016*0Sstevel@tonic-gate 	if (((lip->li_major == -1) && (dev == DDI_DEV_T_NONE)) ||
1017*0Sstevel@tonic-gate 	    (dip != NULL)) {
1018*0Sstevel@tonic-gate 		ret = callback(&ldi_usage, arg);
1019*0Sstevel@tonic-gate 		if (dip != NULL)
1020*0Sstevel@tonic-gate 			ddi_release_devi(dip);
1021*0Sstevel@tonic-gate 		if (ldi_usage.tgt_dip != NULL)
1022*0Sstevel@tonic-gate 			ddi_release_devi(ldi_usage.tgt_dip);
1023*0Sstevel@tonic-gate 		return (ret);
1024*0Sstevel@tonic-gate 	}
1025*0Sstevel@tonic-gate 
1026*0Sstevel@tonic-gate 	/*
1027*0Sstevel@tonic-gate 	 * now this is kinda gross.
1028*0Sstevel@tonic-gate 	 *
1029*0Sstevel@tonic-gate 	 * what we do here is attempt to associate every device instance
1030*0Sstevel@tonic-gate 	 * of the source driver on the system with the open target driver.
1031*0Sstevel@tonic-gate 	 * we do this because we don't know which instance of the device
1032*0Sstevel@tonic-gate 	 * could potentially access the lower device so we assume that all
1033*0Sstevel@tonic-gate 	 * the instances could access it.
1034*0Sstevel@tonic-gate 	 *
1035*0Sstevel@tonic-gate 	 * there are two ways we could have gotten here:
1036*0Sstevel@tonic-gate 	 *
1037*0Sstevel@tonic-gate 	 * 1) this layered ident represents one created using only a
1038*0Sstevel@tonic-gate 	 *    major number or a driver module name.  this means that when
1039*0Sstevel@tonic-gate 	 *    it was created we could not associate it with a particular
1040*0Sstevel@tonic-gate 	 *    dev_t or device instance.
1041*0Sstevel@tonic-gate 	 *
1042*0Sstevel@tonic-gate 	 *    when could this possibly happen you ask?
1043*0Sstevel@tonic-gate 	 *
1044*0Sstevel@tonic-gate 	 *    a perfect example of this is streams persistent links.
1045*0Sstevel@tonic-gate 	 *    when a persistant streams link is formed we can't associate
1046*0Sstevel@tonic-gate 	 *    the lower device stream with any particular upper device
1047*0Sstevel@tonic-gate 	 *    stream or instance.  this is because any particular upper
1048*0Sstevel@tonic-gate 	 *    device stream could be closed, then another could be
1049*0Sstevel@tonic-gate 	 *    opened with a different dev_t and device instance, and it
1050*0Sstevel@tonic-gate 	 *    would still have access to the lower linked stream.
1051*0Sstevel@tonic-gate 	 *
1052*0Sstevel@tonic-gate 	 *    since any instance of the upper streams driver could
1053*0Sstevel@tonic-gate 	 *    potentially access the lower stream whenever it wants,
1054*0Sstevel@tonic-gate 	 *    we represent that here by associating the opened lower
1055*0Sstevel@tonic-gate 	 *    device with every existing device instance of the upper
1056*0Sstevel@tonic-gate 	 *    streams driver.
1057*0Sstevel@tonic-gate 	 *
1058*0Sstevel@tonic-gate 	 * 2) This case should really never happen but we'll include it
1059*0Sstevel@tonic-gate 	 *    for completeness.
1060*0Sstevel@tonic-gate 	 *
1061*0Sstevel@tonic-gate 	 *    it's possible that we could have gotten here because we
1062*0Sstevel@tonic-gate 	 *    have a dev_t for the upper device but we couldn't find a
1063*0Sstevel@tonic-gate 	 *    dip associated with that dev_t.
1064*0Sstevel@tonic-gate 	 *
1065*0Sstevel@tonic-gate 	 *    the only types of devices that have dev_t without an
1066*0Sstevel@tonic-gate 	 *    associated dip are unbound DLPIv2 network devices.  These
1067*0Sstevel@tonic-gate 	 *    types of devices exist to be able to attach a stream to any
1068*0Sstevel@tonic-gate 	 *    instance of a hardware network device.  since these types of
1069*0Sstevel@tonic-gate 	 *    devices are usually hardware devices they should never
1070*0Sstevel@tonic-gate 	 *    really have other devices open.
1071*0Sstevel@tonic-gate 	 */
1072*0Sstevel@tonic-gate 	if (dev != DDI_DEV_T_NONE)
1073*0Sstevel@tonic-gate 		major = getmajor(dev);
1074*0Sstevel@tonic-gate 	else
1075*0Sstevel@tonic-gate 		major = lip->li_major;
1076*0Sstevel@tonic-gate 
1077*0Sstevel@tonic-gate 	ASSERT((major >= 0) && (major < devcnt));
1078*0Sstevel@tonic-gate 
1079*0Sstevel@tonic-gate 	dnp = &devnamesp[major];
1080*0Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
1081*0Sstevel@tonic-gate 	dip = dnp->dn_head;
1082*0Sstevel@tonic-gate 	while ((dip) && (ret == LDI_USAGE_CONTINUE)) {
1083*0Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
1084*0Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
1085*0Sstevel@tonic-gate 
1086*0Sstevel@tonic-gate 		/* set the source dip */
1087*0Sstevel@tonic-gate 		ldi_usage.src_dip = dip;
1088*0Sstevel@tonic-gate 
1089*0Sstevel@tonic-gate 		/* invoke the callback function */
1090*0Sstevel@tonic-gate 		ret = callback(&ldi_usage, arg);
1091*0Sstevel@tonic-gate 
1092*0Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
1093*0Sstevel@tonic-gate 		ddi_release_devi(dip);
1094*0Sstevel@tonic-gate 		dip = ddi_get_next(dip);
1095*0Sstevel@tonic-gate 	}
1096*0Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
1097*0Sstevel@tonic-gate 
1098*0Sstevel@tonic-gate 	/* if there was a target dip, release it */
1099*0Sstevel@tonic-gate 	if (ldi_usage.tgt_dip != NULL)
1100*0Sstevel@tonic-gate 		ddi_release_devi(ldi_usage.tgt_dip);
1101*0Sstevel@tonic-gate 
1102*0Sstevel@tonic-gate 	return (ret);
1103*0Sstevel@tonic-gate }
1104*0Sstevel@tonic-gate 
1105*0Sstevel@tonic-gate /*
1106*0Sstevel@tonic-gate  * ldi_usage_walker() - this walker reports LDI kernel device usage
1107*0Sstevel@tonic-gate  * information via the callback() callback function.  the LDI keeps track
1108*0Sstevel@tonic-gate  * of what devices are being accessed in its own internal data structures.
1109*0Sstevel@tonic-gate  * this function walks those data structures to determine device usage.
1110*0Sstevel@tonic-gate  */
1111*0Sstevel@tonic-gate void
1112*0Sstevel@tonic-gate ldi_usage_walker(void *arg, int (*callback)(const ldi_usage_t *, void *))
1113*0Sstevel@tonic-gate {
1114*0Sstevel@tonic-gate 	struct ldi_handle	*lhp;
1115*0Sstevel@tonic-gate 	struct ldi_ident	*lip;
1116*0Sstevel@tonic-gate 	vnode_t			*vp;
1117*0Sstevel@tonic-gate 	int			i;
1118*0Sstevel@tonic-gate 	int			ret = LDI_USAGE_CONTINUE;
1119*0Sstevel@tonic-gate 
1120*0Sstevel@tonic-gate 	for (i = 0; i < LH_HASH_SZ; i++) {
1121*0Sstevel@tonic-gate 		mutex_enter(&ldi_handle_hash_lock[i]);
1122*0Sstevel@tonic-gate 
1123*0Sstevel@tonic-gate 		lhp = ldi_handle_hash[i];
1124*0Sstevel@tonic-gate 		while ((lhp != NULL) && (ret == LDI_USAGE_CONTINUE)) {
1125*0Sstevel@tonic-gate 			lip = lhp->lh_ident;
1126*0Sstevel@tonic-gate 			vp = lhp->lh_vp;
1127*0Sstevel@tonic-gate 
1128*0Sstevel@tonic-gate 			/* invoke the devinfo callback function */
1129*0Sstevel@tonic-gate 			ret = ldi_usage_walker_helper(lip, vp, arg, callback);
1130*0Sstevel@tonic-gate 
1131*0Sstevel@tonic-gate 			lhp = lhp->lh_next;
1132*0Sstevel@tonic-gate 		}
1133*0Sstevel@tonic-gate 		mutex_exit(&ldi_handle_hash_lock[i]);
1134*0Sstevel@tonic-gate 
1135*0Sstevel@tonic-gate 		if (ret != LDI_USAGE_CONTINUE)
1136*0Sstevel@tonic-gate 			break;
1137*0Sstevel@tonic-gate 	}
1138*0Sstevel@tonic-gate }
1139*0Sstevel@tonic-gate 
1140*0Sstevel@tonic-gate /*
1141*0Sstevel@tonic-gate  * LDI Project private interfaces (streams linking interfaces)
1142*0Sstevel@tonic-gate  *
1143*0Sstevel@tonic-gate  * Streams supports a type of built in device layering via linking.
1144*0Sstevel@tonic-gate  * Certain types of streams drivers can be streams multiplexors.
1145*0Sstevel@tonic-gate  * A streams multiplexor supports the I_LINK/I_PLINK operation.
1146*0Sstevel@tonic-gate  * These operations allows other streams devices to be linked under the
1147*0Sstevel@tonic-gate  * multiplexor.  By definition all streams multiplexors are devices
1148*0Sstevel@tonic-gate  * so this linking is a type of device layering where the multiplexor
1149*0Sstevel@tonic-gate  * device is layered on top of the device linked below it.
1150*0Sstevel@tonic-gate  */
1151*0Sstevel@tonic-gate 
1152*0Sstevel@tonic-gate /*
1153*0Sstevel@tonic-gate  * ldi_mlink_lh() is invoked when streams are linked using LDI handles.
1154*0Sstevel@tonic-gate  * It is not used for normal I_LINKs and I_PLINKs using file descriptors.
1155*0Sstevel@tonic-gate  *
1156*0Sstevel@tonic-gate  * The streams framework keeps track of links via the file_t of the lower
1157*0Sstevel@tonic-gate  * stream.  The LDI keeps track of devices using a vnode.  In the case
1158*0Sstevel@tonic-gate  * of a streams link created via an LDI handle, fnk_lh() allocates
1159*0Sstevel@tonic-gate  * a file_t that the streams framework can use to track the linkage.
1160*0Sstevel@tonic-gate  */
1161*0Sstevel@tonic-gate int
1162*0Sstevel@tonic-gate ldi_mlink_lh(vnode_t *vp, int cmd, intptr_t arg, cred_t *crp, int *rvalp)
1163*0Sstevel@tonic-gate {
1164*0Sstevel@tonic-gate 	struct ldi_handle	*lhp = (struct ldi_handle *)arg;
1165*0Sstevel@tonic-gate 	vnode_t			*vpdown;
1166*0Sstevel@tonic-gate 	file_t			*fpdown;
1167*0Sstevel@tonic-gate 	int			err;
1168*0Sstevel@tonic-gate 
1169*0Sstevel@tonic-gate 	if (lhp == NULL)
1170*0Sstevel@tonic-gate 		return (EINVAL);
1171*0Sstevel@tonic-gate 
1172*0Sstevel@tonic-gate 	vpdown = lhp->lh_vp;
1173*0Sstevel@tonic-gate 	ASSERT(vn_matchops(vpdown, spec_getvnodeops()));
1174*0Sstevel@tonic-gate 	ASSERT(cmd == _I_PLINK_LH);
1175*0Sstevel@tonic-gate 
1176*0Sstevel@tonic-gate 	/*
1177*0Sstevel@tonic-gate 	 * create a new lower vnode and a file_t that points to it,
1178*0Sstevel@tonic-gate 	 * streams linking requires a file_t.  falloc() returns with
1179*0Sstevel@tonic-gate 	 * fpdown locked.
1180*0Sstevel@tonic-gate 	 */
1181*0Sstevel@tonic-gate 	VN_HOLD(vpdown);
1182*0Sstevel@tonic-gate 	(void) falloc(vpdown, FREAD|FWRITE, &fpdown, NULL);
1183*0Sstevel@tonic-gate 	mutex_exit(&fpdown->f_tlock);
1184*0Sstevel@tonic-gate 
1185*0Sstevel@tonic-gate 	/* try to establish the link */
1186*0Sstevel@tonic-gate 	err = mlink_file(vp, I_PLINK, fpdown, crp, rvalp, 1);
1187*0Sstevel@tonic-gate 
1188*0Sstevel@tonic-gate 	if (err != 0) {
1189*0Sstevel@tonic-gate 		/* the link failed, free the file_t and release the vnode */
1190*0Sstevel@tonic-gate 		mutex_enter(&fpdown->f_tlock);
1191*0Sstevel@tonic-gate 		unfalloc(fpdown);
1192*0Sstevel@tonic-gate 		VN_RELE(vpdown);
1193*0Sstevel@tonic-gate 	}
1194*0Sstevel@tonic-gate 
1195*0Sstevel@tonic-gate 	return (err);
1196*0Sstevel@tonic-gate }
1197*0Sstevel@tonic-gate 
1198*0Sstevel@tonic-gate /*
1199*0Sstevel@tonic-gate  * ldi_mlink_fp() is invoked for all successfull streams linkages created
1200*0Sstevel@tonic-gate  * via I_LINK and I_PLINK.  ldi_mlink_fp() records the linkage information
1201*0Sstevel@tonic-gate  * in its internal state so that the devinfo snapshot code has some
1202*0Sstevel@tonic-gate  * observability into streams device linkage information.
1203*0Sstevel@tonic-gate  */
1204*0Sstevel@tonic-gate void
1205*0Sstevel@tonic-gate ldi_mlink_fp(struct stdata *stp, file_t *fpdown, int lhlink, int type)
1206*0Sstevel@tonic-gate {
1207*0Sstevel@tonic-gate 	vnode_t			*vp = fpdown->f_vnode;
1208*0Sstevel@tonic-gate 	struct snode		*sp, *csp;
1209*0Sstevel@tonic-gate 	ldi_ident_t		li;
1210*0Sstevel@tonic-gate 	major_t			major;
1211*0Sstevel@tonic-gate 	int			ret;
1212*0Sstevel@tonic-gate 
1213*0Sstevel@tonic-gate 	/* if the lower stream is not a device then return */
1214*0Sstevel@tonic-gate 	if (!vn_matchops(vp, spec_getvnodeops()))
1215*0Sstevel@tonic-gate 		return;
1216*0Sstevel@tonic-gate 
1217*0Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
1218*0Sstevel@tonic-gate 
1219*0Sstevel@tonic-gate 	LDI_STREAMS_LNK((CE_NOTE, "%s: linking streams "
1220*0Sstevel@tonic-gate 		"stp=0x%p, fpdown=0x%p", "ldi_mlink_fp",
1221*0Sstevel@tonic-gate 		(void *)stp, (void *)fpdown));
1222*0Sstevel@tonic-gate 
1223*0Sstevel@tonic-gate 	sp = VTOS(vp);
1224*0Sstevel@tonic-gate 	csp = VTOS(sp->s_commonvp);
1225*0Sstevel@tonic-gate 
1226*0Sstevel@tonic-gate 	/* check if this was a plink via a layered handle */
1227*0Sstevel@tonic-gate 	if (lhlink) {
1228*0Sstevel@tonic-gate 		/*
1229*0Sstevel@tonic-gate 		 * increment the common snode s_count.
1230*0Sstevel@tonic-gate 		 *
1231*0Sstevel@tonic-gate 		 * this is done because after the link operation there
1232*0Sstevel@tonic-gate 		 * are two ways that s_count can be decremented.
1233*0Sstevel@tonic-gate 		 *
1234*0Sstevel@tonic-gate 		 * when the layered handle used to create the link is
1235*0Sstevel@tonic-gate 		 * closed, spec_close() is called and it will decrement
1236*0Sstevel@tonic-gate 		 * s_count in the common snode.  if we don't increment
1237*0Sstevel@tonic-gate 		 * s_count here then this could cause spec_close() to
1238*0Sstevel@tonic-gate 		 * actually close the device while it's still linked
1239*0Sstevel@tonic-gate 		 * under a multiplexer.
1240*0Sstevel@tonic-gate 		 *
1241*0Sstevel@tonic-gate 		 * also, when the lower stream is unlinked, closef() is
1242*0Sstevel@tonic-gate 		 * called for the file_t associated with this snode.
1243*0Sstevel@tonic-gate 		 * closef() will call spec_close(), which will decrement
1244*0Sstevel@tonic-gate 		 * s_count.  if we dont't increment s_count here then this
1245*0Sstevel@tonic-gate 		 * could cause spec_close() to actually close the device
1246*0Sstevel@tonic-gate 		 * while there may still be valid layered handles
1247*0Sstevel@tonic-gate 		 * pointing to it.
1248*0Sstevel@tonic-gate 		 */
1249*0Sstevel@tonic-gate 		mutex_enter(&csp->s_lock);
1250*0Sstevel@tonic-gate 		ASSERT(csp->s_count >= 1);
1251*0Sstevel@tonic-gate 		csp->s_count++;
1252*0Sstevel@tonic-gate 		mutex_exit(&csp->s_lock);
1253*0Sstevel@tonic-gate 
1254*0Sstevel@tonic-gate 		/*
1255*0Sstevel@tonic-gate 		 * decrement the f_count.
1256*0Sstevel@tonic-gate 		 * this is done because the layered driver framework does
1257*0Sstevel@tonic-gate 		 * not actually cache a copy of the file_t allocated to
1258*0Sstevel@tonic-gate 		 * do the link.  this is done here instead of in ldi_mlink_lh()
1259*0Sstevel@tonic-gate 		 * because there is a window in ldi_mlink_lh() between where
1260*0Sstevel@tonic-gate 		 * milnk_file() returns and we would decrement the f_count
1261*0Sstevel@tonic-gate 		 * when the stream could be unlinked.
1262*0Sstevel@tonic-gate 		 */
1263*0Sstevel@tonic-gate 		mutex_enter(&fpdown->f_tlock);
1264*0Sstevel@tonic-gate 		fpdown->f_count--;
1265*0Sstevel@tonic-gate 		mutex_exit(&fpdown->f_tlock);
1266*0Sstevel@tonic-gate 	}
1267*0Sstevel@tonic-gate 
1268*0Sstevel@tonic-gate 	/*
1269*0Sstevel@tonic-gate 	 * NOTE: here we rely on the streams subsystem not allowing
1270*0Sstevel@tonic-gate 	 * a stream to be multiplexed more than once.  if this
1271*0Sstevel@tonic-gate 	 * changes, we break.
1272*0Sstevel@tonic-gate 	 *
1273*0Sstevel@tonic-gate 	 * mark the snode/stream as multiplexed
1274*0Sstevel@tonic-gate 	 */
1275*0Sstevel@tonic-gate 	mutex_enter(&sp->s_lock);
1276*0Sstevel@tonic-gate 	ASSERT(!(sp->s_flag & SMUXED));
1277*0Sstevel@tonic-gate 	sp->s_flag |= SMUXED;
1278*0Sstevel@tonic-gate 	mutex_exit(&sp->s_lock);
1279*0Sstevel@tonic-gate 
1280*0Sstevel@tonic-gate 	/* get a layered ident for the upper stream */
1281*0Sstevel@tonic-gate 	if (type == LINKNORMAL) {
1282*0Sstevel@tonic-gate 		/*
1283*0Sstevel@tonic-gate 		 * if the link is not persistant then we can associate
1284*0Sstevel@tonic-gate 		 * the upper stream with a dev_t.  this is because the
1285*0Sstevel@tonic-gate 		 * upper stream is associated with a vnode, which is
1286*0Sstevel@tonic-gate 		 * associated with a dev_t and this binding can't change
1287*0Sstevel@tonic-gate 		 * during the life of the stream.  since the link isn't
1288*0Sstevel@tonic-gate 		 * persistant once the stream is destroyed the link is
1289*0Sstevel@tonic-gate 		 * destroyed.  so the dev_t will be valid for the life
1290*0Sstevel@tonic-gate 		 * of the link.
1291*0Sstevel@tonic-gate 		 */
1292*0Sstevel@tonic-gate 		ret = ldi_ident_from_stream(getendq(stp->sd_wrq), &li);
1293*0Sstevel@tonic-gate 	} else {
1294*0Sstevel@tonic-gate 		/*
1295*0Sstevel@tonic-gate 		 * if the link is persistant we can only associate the
1296*0Sstevel@tonic-gate 		 * link with a driver (and not a dev_t.)  this is
1297*0Sstevel@tonic-gate 		 * because subsequent opens of the upper device may result
1298*0Sstevel@tonic-gate 		 * in a different stream (and dev_t) having access to
1299*0Sstevel@tonic-gate 		 * the lower stream.
1300*0Sstevel@tonic-gate 		 *
1301*0Sstevel@tonic-gate 		 * for example, if the upper stream is closed after the
1302*0Sstevel@tonic-gate 		 * persistant link operation is compleated, a subsequent
1303*0Sstevel@tonic-gate 		 * open of the upper device will create a new stream which
1304*0Sstevel@tonic-gate 		 * may have a different dev_t and an unlink operation
1305*0Sstevel@tonic-gate 		 * can be performed using this new upper stream.
1306*0Sstevel@tonic-gate 		 */
1307*0Sstevel@tonic-gate 		ASSERT(type == LINKPERSIST);
1308*0Sstevel@tonic-gate 		major = getmajor(stp->sd_vnode->v_rdev);
1309*0Sstevel@tonic-gate 		ret = ldi_ident_from_major(major, &li);
1310*0Sstevel@tonic-gate 	}
1311*0Sstevel@tonic-gate 
1312*0Sstevel@tonic-gate 	ASSERT(ret == 0);
1313*0Sstevel@tonic-gate 	(void) handle_alloc(vp, (struct ldi_ident *)li);
1314*0Sstevel@tonic-gate 	ldi_ident_release(li);
1315*0Sstevel@tonic-gate }
1316*0Sstevel@tonic-gate 
1317*0Sstevel@tonic-gate void
1318*0Sstevel@tonic-gate ldi_munlink_fp(struct stdata *stp, file_t *fpdown, int type)
1319*0Sstevel@tonic-gate {
1320*0Sstevel@tonic-gate 	struct ldi_handle	*lhp;
1321*0Sstevel@tonic-gate 	vnode_t			*vp = (vnode_t *)fpdown->f_vnode;
1322*0Sstevel@tonic-gate 	struct snode		*sp;
1323*0Sstevel@tonic-gate 	ldi_ident_t		li;
1324*0Sstevel@tonic-gate 	major_t			major;
1325*0Sstevel@tonic-gate 	int			ret;
1326*0Sstevel@tonic-gate 
1327*0Sstevel@tonic-gate 	/* if the lower stream is not a device then return */
1328*0Sstevel@tonic-gate 	if (!vn_matchops(vp, spec_getvnodeops()))
1329*0Sstevel@tonic-gate 		return;
1330*0Sstevel@tonic-gate 
1331*0Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
1332*0Sstevel@tonic-gate 	ASSERT((type == LINKNORMAL) || (type == LINKPERSIST));
1333*0Sstevel@tonic-gate 
1334*0Sstevel@tonic-gate 	LDI_STREAMS_LNK((CE_NOTE, "%s: unlinking streams "
1335*0Sstevel@tonic-gate 		"stp=0x%p, fpdown=0x%p", "ldi_munlink_fp",
1336*0Sstevel@tonic-gate 		(void *)stp, (void *)fpdown));
1337*0Sstevel@tonic-gate 
1338*0Sstevel@tonic-gate 	/*
1339*0Sstevel@tonic-gate 	 * NOTE: here we rely on the streams subsystem not allowing
1340*0Sstevel@tonic-gate 	 * a stream to be multiplexed more than once.  if this
1341*0Sstevel@tonic-gate 	 * changes, we break.
1342*0Sstevel@tonic-gate 	 *
1343*0Sstevel@tonic-gate 	 * mark the snode/stream as not multiplexed
1344*0Sstevel@tonic-gate 	 */
1345*0Sstevel@tonic-gate 	sp = VTOS(vp);
1346*0Sstevel@tonic-gate 	mutex_enter(&sp->s_lock);
1347*0Sstevel@tonic-gate 	ASSERT(sp->s_flag & SMUXED);
1348*0Sstevel@tonic-gate 	sp->s_flag &= ~SMUXED;
1349*0Sstevel@tonic-gate 	mutex_exit(&sp->s_lock);
1350*0Sstevel@tonic-gate 
1351*0Sstevel@tonic-gate 	/*
1352*0Sstevel@tonic-gate 	 * clear the owner for this snode
1353*0Sstevel@tonic-gate 	 * see the comment in ldi_mlink_fp() for information about how
1354*0Sstevel@tonic-gate 	 * the ident is allocated
1355*0Sstevel@tonic-gate 	 */
1356*0Sstevel@tonic-gate 	if (type == LINKNORMAL) {
1357*0Sstevel@tonic-gate 		ret = ldi_ident_from_stream(getendq(stp->sd_wrq), &li);
1358*0Sstevel@tonic-gate 	} else {
1359*0Sstevel@tonic-gate 		ASSERT(type == LINKPERSIST);
1360*0Sstevel@tonic-gate 		major = getmajor(stp->sd_vnode->v_rdev);
1361*0Sstevel@tonic-gate 		ret = ldi_ident_from_major(major, &li);
1362*0Sstevel@tonic-gate 	}
1363*0Sstevel@tonic-gate 
1364*0Sstevel@tonic-gate 	ASSERT(ret == 0);
1365*0Sstevel@tonic-gate 	lhp = handle_find(vp, (struct ldi_ident *)li);
1366*0Sstevel@tonic-gate 	handle_release(lhp);
1367*0Sstevel@tonic-gate 	ldi_ident_release(li);
1368*0Sstevel@tonic-gate }
1369*0Sstevel@tonic-gate 
1370*0Sstevel@tonic-gate /*
1371*0Sstevel@tonic-gate  * LDI Consolidation private interfaces
1372*0Sstevel@tonic-gate  */
1373*0Sstevel@tonic-gate int
1374*0Sstevel@tonic-gate ldi_ident_from_mod(struct modlinkage *modlp, ldi_ident_t *lip)
1375*0Sstevel@tonic-gate {
1376*0Sstevel@tonic-gate 	struct modctl		*modp;
1377*0Sstevel@tonic-gate 	major_t			major;
1378*0Sstevel@tonic-gate 	char			*name;
1379*0Sstevel@tonic-gate 
1380*0Sstevel@tonic-gate 	if ((modlp == NULL) || (lip == NULL))
1381*0Sstevel@tonic-gate 		return (EINVAL);
1382*0Sstevel@tonic-gate 
1383*0Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
1384*0Sstevel@tonic-gate 
1385*0Sstevel@tonic-gate 	modp = mod_getctl(modlp);
1386*0Sstevel@tonic-gate 	if (modp == NULL)
1387*0Sstevel@tonic-gate 		return (EINVAL);
1388*0Sstevel@tonic-gate 	name = modp->mod_modname;
1389*0Sstevel@tonic-gate 	if (name == NULL)
1390*0Sstevel@tonic-gate 		return (EINVAL);
1391*0Sstevel@tonic-gate 	major = mod_name_to_major(name);
1392*0Sstevel@tonic-gate 
1393*0Sstevel@tonic-gate 	*lip = (ldi_ident_t)ident_alloc(name, NULL, DDI_DEV_T_NONE, major);
1394*0Sstevel@tonic-gate 
1395*0Sstevel@tonic-gate 	LDI_ALLOCFREE((CE_WARN, "%s: li=0x%p, mod=%s",
1396*0Sstevel@tonic-gate 		"ldi_ident_from_mod", (void *)*lip, name));
1397*0Sstevel@tonic-gate 
1398*0Sstevel@tonic-gate 	return (0);
1399*0Sstevel@tonic-gate }
1400*0Sstevel@tonic-gate 
1401*0Sstevel@tonic-gate ldi_ident_t
1402*0Sstevel@tonic-gate ldi_ident_from_anon()
1403*0Sstevel@tonic-gate {
1404*0Sstevel@tonic-gate 	ldi_ident_t	lip;
1405*0Sstevel@tonic-gate 
1406*0Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
1407*0Sstevel@tonic-gate 
1408*0Sstevel@tonic-gate 	lip = (ldi_ident_t)ident_alloc("genunix", NULL, DDI_DEV_T_NONE, -1);
1409*0Sstevel@tonic-gate 
1410*0Sstevel@tonic-gate 	LDI_ALLOCFREE((CE_WARN, "%s: li=0x%p, mod=%s",
1411*0Sstevel@tonic-gate 		"ldi_ident_from_anon", (void *)lip, "genunix"));
1412*0Sstevel@tonic-gate 
1413*0Sstevel@tonic-gate 	return (lip);
1414*0Sstevel@tonic-gate }
1415*0Sstevel@tonic-gate 
1416*0Sstevel@tonic-gate 
1417*0Sstevel@tonic-gate /*
1418*0Sstevel@tonic-gate  * LDI Public interfaces
1419*0Sstevel@tonic-gate  */
1420*0Sstevel@tonic-gate int
1421*0Sstevel@tonic-gate ldi_ident_from_stream(struct queue *sq, ldi_ident_t *lip)
1422*0Sstevel@tonic-gate {
1423*0Sstevel@tonic-gate 	struct stdata		*stp;
1424*0Sstevel@tonic-gate 	dev_t			dev;
1425*0Sstevel@tonic-gate 	char			*name;
1426*0Sstevel@tonic-gate 
1427*0Sstevel@tonic-gate 	if ((sq == NULL) || (lip == NULL))
1428*0Sstevel@tonic-gate 		return (EINVAL);
1429*0Sstevel@tonic-gate 
1430*0Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
1431*0Sstevel@tonic-gate 
1432*0Sstevel@tonic-gate 	stp = sq->q_stream;
1433*0Sstevel@tonic-gate 	if (!vn_matchops(stp->sd_vnode, spec_getvnodeops()))
1434*0Sstevel@tonic-gate 		return (EINVAL);
1435*0Sstevel@tonic-gate 
1436*0Sstevel@tonic-gate 	dev = stp->sd_vnode->v_rdev;
1437*0Sstevel@tonic-gate 	name = mod_major_to_name(getmajor(dev));
1438*0Sstevel@tonic-gate 	if (name == NULL)
1439*0Sstevel@tonic-gate 		return (EINVAL);
1440*0Sstevel@tonic-gate 	*lip = (ldi_ident_t)ident_alloc(name, NULL, dev, -1);
1441*0Sstevel@tonic-gate 
1442*0Sstevel@tonic-gate 	LDI_ALLOCFREE((CE_WARN,
1443*0Sstevel@tonic-gate 		"%s: li=0x%p, mod=%s, minor=0x%x, stp=0x%p",
1444*0Sstevel@tonic-gate 		"ldi_ident_from_stream", (void *)*lip, name, getminor(dev),
1445*0Sstevel@tonic-gate 		(void *)stp));
1446*0Sstevel@tonic-gate 
1447*0Sstevel@tonic-gate 	return (0);
1448*0Sstevel@tonic-gate }
1449*0Sstevel@tonic-gate 
1450*0Sstevel@tonic-gate int
1451*0Sstevel@tonic-gate ldi_ident_from_dev(dev_t dev, ldi_ident_t *lip)
1452*0Sstevel@tonic-gate {
1453*0Sstevel@tonic-gate 	char			*name;
1454*0Sstevel@tonic-gate 
1455*0Sstevel@tonic-gate 	if (lip == NULL)
1456*0Sstevel@tonic-gate 		return (EINVAL);
1457*0Sstevel@tonic-gate 
1458*0Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
1459*0Sstevel@tonic-gate 
1460*0Sstevel@tonic-gate 	name = mod_major_to_name(getmajor(dev));
1461*0Sstevel@tonic-gate 	if (name == NULL)
1462*0Sstevel@tonic-gate 		return (EINVAL);
1463*0Sstevel@tonic-gate 	*lip = (ldi_ident_t)ident_alloc(name, NULL, dev, -1);
1464*0Sstevel@tonic-gate 
1465*0Sstevel@tonic-gate 	LDI_ALLOCFREE((CE_WARN,
1466*0Sstevel@tonic-gate 		"%s: li=0x%p, mod=%s, minor=0x%x",
1467*0Sstevel@tonic-gate 		"ldi_ident_from_dev", (void *)*lip, name, getminor(dev)));
1468*0Sstevel@tonic-gate 
1469*0Sstevel@tonic-gate 	return (0);
1470*0Sstevel@tonic-gate }
1471*0Sstevel@tonic-gate 
1472*0Sstevel@tonic-gate int
1473*0Sstevel@tonic-gate ldi_ident_from_dip(dev_info_t *dip, ldi_ident_t *lip)
1474*0Sstevel@tonic-gate {
1475*0Sstevel@tonic-gate 	struct dev_info		*devi = (struct dev_info *)dip;
1476*0Sstevel@tonic-gate 	char			*name;
1477*0Sstevel@tonic-gate 
1478*0Sstevel@tonic-gate 	if ((dip == NULL) || (lip == NULL))
1479*0Sstevel@tonic-gate 		return (EINVAL);
1480*0Sstevel@tonic-gate 
1481*0Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
1482*0Sstevel@tonic-gate 
1483*0Sstevel@tonic-gate 	name = mod_major_to_name(devi->devi_major);
1484*0Sstevel@tonic-gate 	if (name == NULL)
1485*0Sstevel@tonic-gate 		return (EINVAL);
1486*0Sstevel@tonic-gate 	*lip = (ldi_ident_t)ident_alloc(name, dip, DDI_DEV_T_NONE, -1);
1487*0Sstevel@tonic-gate 
1488*0Sstevel@tonic-gate 	LDI_ALLOCFREE((CE_WARN,
1489*0Sstevel@tonic-gate 		"%s: li=0x%p, mod=%s, dip=0x%p",
1490*0Sstevel@tonic-gate 		"ldi_ident_from_dip", (void *)*lip, name, (void *)devi));
1491*0Sstevel@tonic-gate 
1492*0Sstevel@tonic-gate 	return (0);
1493*0Sstevel@tonic-gate }
1494*0Sstevel@tonic-gate 
1495*0Sstevel@tonic-gate int
1496*0Sstevel@tonic-gate ldi_ident_from_major(major_t major, ldi_ident_t *lip)
1497*0Sstevel@tonic-gate {
1498*0Sstevel@tonic-gate 	char			*name;
1499*0Sstevel@tonic-gate 
1500*0Sstevel@tonic-gate 	if (lip == NULL)
1501*0Sstevel@tonic-gate 		return (EINVAL);
1502*0Sstevel@tonic-gate 
1503*0Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
1504*0Sstevel@tonic-gate 
1505*0Sstevel@tonic-gate 	name = mod_major_to_name(major);
1506*0Sstevel@tonic-gate 	if (name == NULL)
1507*0Sstevel@tonic-gate 		return (EINVAL);
1508*0Sstevel@tonic-gate 	*lip = (ldi_ident_t)ident_alloc(name, NULL, DDI_DEV_T_NONE, major);
1509*0Sstevel@tonic-gate 
1510*0Sstevel@tonic-gate 	LDI_ALLOCFREE((CE_WARN,
1511*0Sstevel@tonic-gate 		"%s: li=0x%p, mod=%s",
1512*0Sstevel@tonic-gate 		"ldi_ident_from_major", (void *)*lip, name));
1513*0Sstevel@tonic-gate 
1514*0Sstevel@tonic-gate 	return (0);
1515*0Sstevel@tonic-gate }
1516*0Sstevel@tonic-gate 
1517*0Sstevel@tonic-gate void
1518*0Sstevel@tonic-gate ldi_ident_release(ldi_ident_t li)
1519*0Sstevel@tonic-gate {
1520*0Sstevel@tonic-gate 	struct ldi_ident	*ident = (struct ldi_ident *)li;
1521*0Sstevel@tonic-gate 	char			*name;
1522*0Sstevel@tonic-gate 
1523*0Sstevel@tonic-gate 	if (li == NULL)
1524*0Sstevel@tonic-gate 		return;
1525*0Sstevel@tonic-gate 
1526*0Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
1527*0Sstevel@tonic-gate 
1528*0Sstevel@tonic-gate 	name = ident->li_modname;
1529*0Sstevel@tonic-gate 
1530*0Sstevel@tonic-gate 	LDI_ALLOCFREE((CE_WARN,
1531*0Sstevel@tonic-gate 		"%s: li=0x%p, mod=%s",
1532*0Sstevel@tonic-gate 		"ldi_ident_release", (void *)li, name));
1533*0Sstevel@tonic-gate 
1534*0Sstevel@tonic-gate 	ident_release((struct ldi_ident *)li);
1535*0Sstevel@tonic-gate }
1536*0Sstevel@tonic-gate 
1537*0Sstevel@tonic-gate /* get a handle to a device by dev_t and otyp */
1538*0Sstevel@tonic-gate int
1539*0Sstevel@tonic-gate ldi_open_by_dev(dev_t *devp, int otyp, int flag, cred_t *cr,
1540*0Sstevel@tonic-gate     ldi_handle_t *lhp, ldi_ident_t li)
1541*0Sstevel@tonic-gate {
1542*0Sstevel@tonic-gate 	struct ldi_ident	*lip = (struct ldi_ident *)li;
1543*0Sstevel@tonic-gate 	int 			ret;
1544*0Sstevel@tonic-gate 	vnode_t			*vp;
1545*0Sstevel@tonic-gate 
1546*0Sstevel@tonic-gate 	/* sanity check required input parameters */
1547*0Sstevel@tonic-gate 	if ((devp == NULL) || (!OTYP_VALID(otyp)) || (cr == NULL) ||
1548*0Sstevel@tonic-gate 	    (lhp == NULL) || (lip == NULL))
1549*0Sstevel@tonic-gate 		return (EINVAL);
1550*0Sstevel@tonic-gate 
1551*0Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
1552*0Sstevel@tonic-gate 
1553*0Sstevel@tonic-gate 	if ((ret = ldi_vp_from_dev(*devp, otyp, &vp)) != 0)
1554*0Sstevel@tonic-gate 		return (ret);
1555*0Sstevel@tonic-gate 
1556*0Sstevel@tonic-gate 	if ((ret = ldi_open_by_vp(&vp, flag, cr, lhp, lip)) == 0) {
1557*0Sstevel@tonic-gate 		*devp = vp->v_rdev;
1558*0Sstevel@tonic-gate 	}
1559*0Sstevel@tonic-gate 	VN_RELE(vp);
1560*0Sstevel@tonic-gate 
1561*0Sstevel@tonic-gate 	return (ret);
1562*0Sstevel@tonic-gate }
1563*0Sstevel@tonic-gate 
1564*0Sstevel@tonic-gate /* get a handle to a device by pathname */
1565*0Sstevel@tonic-gate int
1566*0Sstevel@tonic-gate ldi_open_by_name(char *pathname, int flag, cred_t *cr,
1567*0Sstevel@tonic-gate     ldi_handle_t *lhp, ldi_ident_t li)
1568*0Sstevel@tonic-gate {
1569*0Sstevel@tonic-gate 	struct ldi_ident	*lip = (struct ldi_ident *)li;
1570*0Sstevel@tonic-gate 	int 			ret;
1571*0Sstevel@tonic-gate 	vnode_t			*vp;
1572*0Sstevel@tonic-gate 
1573*0Sstevel@tonic-gate 	/* sanity check required input parameters */
1574*0Sstevel@tonic-gate 	if ((pathname == NULL) || (*pathname != '/') ||
1575*0Sstevel@tonic-gate 	    (cr == NULL) || (lhp == NULL) || (lip == NULL))
1576*0Sstevel@tonic-gate 		return (EINVAL);
1577*0Sstevel@tonic-gate 
1578*0Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
1579*0Sstevel@tonic-gate 
1580*0Sstevel@tonic-gate 	if ((ret = ldi_vp_from_name(pathname, &vp)) != 0)
1581*0Sstevel@tonic-gate 		return (ret);
1582*0Sstevel@tonic-gate 
1583*0Sstevel@tonic-gate 	ret = ldi_open_by_vp(&vp, flag, cr, lhp, lip);
1584*0Sstevel@tonic-gate 	VN_RELE(vp);
1585*0Sstevel@tonic-gate 
1586*0Sstevel@tonic-gate 	return (ret);
1587*0Sstevel@tonic-gate }
1588*0Sstevel@tonic-gate 
1589*0Sstevel@tonic-gate /* get a handle to a device by devid and minor_name */
1590*0Sstevel@tonic-gate int
1591*0Sstevel@tonic-gate ldi_open_by_devid(ddi_devid_t devid, char *minor_name,
1592*0Sstevel@tonic-gate     int flag, cred_t *cr, ldi_handle_t *lhp, ldi_ident_t li)
1593*0Sstevel@tonic-gate {
1594*0Sstevel@tonic-gate 	struct ldi_ident	*lip = (struct ldi_ident *)li;
1595*0Sstevel@tonic-gate 	int			ret;
1596*0Sstevel@tonic-gate 	vnode_t			*vp;
1597*0Sstevel@tonic-gate 
1598*0Sstevel@tonic-gate 	/* sanity check required input parameters */
1599*0Sstevel@tonic-gate 	if ((minor_name == NULL) || (cr == NULL) ||
1600*0Sstevel@tonic-gate 	    (lhp == NULL) || (lip == NULL))
1601*0Sstevel@tonic-gate 		return (EINVAL);
1602*0Sstevel@tonic-gate 
1603*0Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
1604*0Sstevel@tonic-gate 
1605*0Sstevel@tonic-gate 	if ((ret = ldi_vp_from_devid(devid, minor_name, &vp)) != 0)
1606*0Sstevel@tonic-gate 		return (ret);
1607*0Sstevel@tonic-gate 
1608*0Sstevel@tonic-gate 	ret = ldi_open_by_vp(&vp, flag, cr, lhp, lip);
1609*0Sstevel@tonic-gate 	VN_RELE(vp);
1610*0Sstevel@tonic-gate 
1611*0Sstevel@tonic-gate 	return (ret);
1612*0Sstevel@tonic-gate }
1613*0Sstevel@tonic-gate 
1614*0Sstevel@tonic-gate int
1615*0Sstevel@tonic-gate ldi_close(ldi_handle_t lh, int flag, cred_t *cr)
1616*0Sstevel@tonic-gate {
1617*0Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
1618*0Sstevel@tonic-gate 	struct ldi_event	*lep;
1619*0Sstevel@tonic-gate 	int 			err = 0;
1620*0Sstevel@tonic-gate 
1621*0Sstevel@tonic-gate 	if (lh == NULL)
1622*0Sstevel@tonic-gate 		return (EINVAL);
1623*0Sstevel@tonic-gate 
1624*0Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
1625*0Sstevel@tonic-gate 
1626*0Sstevel@tonic-gate 	/* Flush back any dirty pages associated with the device. */
1627*0Sstevel@tonic-gate 	if (handlep->lh_type & LH_CBDEV) {
1628*0Sstevel@tonic-gate 		vnode_t	*cvp = common_specvp(handlep->lh_vp);
1629*0Sstevel@tonic-gate 		dev_t	dev = cvp->v_rdev;
1630*0Sstevel@tonic-gate 
1631*0Sstevel@tonic-gate 		(void) VOP_PUTPAGE(cvp, 0, 0, B_INVAL, kcred);
1632*0Sstevel@tonic-gate 		bflush(dev);
1633*0Sstevel@tonic-gate 	}
1634*0Sstevel@tonic-gate 
1635*0Sstevel@tonic-gate 	/*
1636*0Sstevel@tonic-gate 	 * Any event handlers should have been unregistered by the
1637*0Sstevel@tonic-gate 	 * time ldi_close() is called.  If they haven't then it's a
1638*0Sstevel@tonic-gate 	 * bug.
1639*0Sstevel@tonic-gate 	 *
1640*0Sstevel@tonic-gate 	 * In a debug kernel we'll panic to make the problem obvious.
1641*0Sstevel@tonic-gate 	 */
1642*0Sstevel@tonic-gate 	ASSERT(handlep->lh_events == NULL);
1643*0Sstevel@tonic-gate 
1644*0Sstevel@tonic-gate 	/*
1645*0Sstevel@tonic-gate 	 * On a production kernel we'll "do the right thing" (unregister
1646*0Sstevel@tonic-gate 	 * the event handlers) and then complain about having to do the
1647*0Sstevel@tonic-gate 	 * work ourselves.
1648*0Sstevel@tonic-gate 	 */
1649*0Sstevel@tonic-gate 	while ((lep = handlep->lh_events) != NULL) {
1650*0Sstevel@tonic-gate 		err = 1;
1651*0Sstevel@tonic-gate 		(void) ldi_remove_event_handler(lh, (ldi_callback_id_t)lep);
1652*0Sstevel@tonic-gate 	}
1653*0Sstevel@tonic-gate 	if (err) {
1654*0Sstevel@tonic-gate 		struct ldi_ident *lip = handlep->lh_ident;
1655*0Sstevel@tonic-gate 		ASSERT(lip != NULL);
1656*0Sstevel@tonic-gate 		cmn_err(CE_NOTE, "ldi err: %s "
1657*0Sstevel@tonic-gate 		    "failed to unregister layered event handlers before "
1658*0Sstevel@tonic-gate 		    "closing devices", lip->li_modname);
1659*0Sstevel@tonic-gate 	}
1660*0Sstevel@tonic-gate 
1661*0Sstevel@tonic-gate 	/* do a layered close on the device */
1662*0Sstevel@tonic-gate 	err = VOP_CLOSE(handlep->lh_vp, flag | FKLYR, 1, (offset_t)0, cr);
1663*0Sstevel@tonic-gate 
1664*0Sstevel@tonic-gate 	LDI_OPENCLOSE((CE_WARN, "%s: lh=0x%p", "ldi close", (void *)lh));
1665*0Sstevel@tonic-gate 
1666*0Sstevel@tonic-gate 	/*
1667*0Sstevel@tonic-gate 	 * Free the handle even if the device close failed.  why?
1668*0Sstevel@tonic-gate 	 *
1669*0Sstevel@tonic-gate 	 * If the device close failed we can't really make assumptions
1670*0Sstevel@tonic-gate 	 * about the devices state so we shouldn't allow access to the
1671*0Sstevel@tonic-gate 	 * device via this handle any more.  If the device consumer wants
1672*0Sstevel@tonic-gate 	 * to access the device again they should open it again.
1673*0Sstevel@tonic-gate 	 *
1674*0Sstevel@tonic-gate 	 * This is the same way file/device close failures are handled
1675*0Sstevel@tonic-gate 	 * in other places like spec_close() and closeandsetf().
1676*0Sstevel@tonic-gate 	 */
1677*0Sstevel@tonic-gate 	handle_release(handlep);
1678*0Sstevel@tonic-gate 	return (err);
1679*0Sstevel@tonic-gate }
1680*0Sstevel@tonic-gate 
1681*0Sstevel@tonic-gate int
1682*0Sstevel@tonic-gate ldi_read(ldi_handle_t lh, struct uio *uiop, cred_t *credp)
1683*0Sstevel@tonic-gate {
1684*0Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
1685*0Sstevel@tonic-gate 	vnode_t			*vp;
1686*0Sstevel@tonic-gate 	dev_t			dev;
1687*0Sstevel@tonic-gate 	int			ret;
1688*0Sstevel@tonic-gate 
1689*0Sstevel@tonic-gate 	if (lh == NULL)
1690*0Sstevel@tonic-gate 		return (EINVAL);
1691*0Sstevel@tonic-gate 
1692*0Sstevel@tonic-gate 	vp = handlep->lh_vp;
1693*0Sstevel@tonic-gate 	dev = vp->v_rdev;
1694*0Sstevel@tonic-gate 	if (handlep->lh_type & LH_CBDEV) {
1695*0Sstevel@tonic-gate 		ret = cdev_read(dev, uiop, credp);
1696*0Sstevel@tonic-gate 	} else if (handlep->lh_type & LH_STREAM) {
1697*0Sstevel@tonic-gate 		ret = strread(vp, uiop, credp);
1698*0Sstevel@tonic-gate 	} else {
1699*0Sstevel@tonic-gate 		return (ENOTSUP);
1700*0Sstevel@tonic-gate 	}
1701*0Sstevel@tonic-gate 	return (ret);
1702*0Sstevel@tonic-gate }
1703*0Sstevel@tonic-gate 
1704*0Sstevel@tonic-gate int
1705*0Sstevel@tonic-gate ldi_write(ldi_handle_t lh, struct uio *uiop, cred_t *credp)
1706*0Sstevel@tonic-gate {
1707*0Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
1708*0Sstevel@tonic-gate 	vnode_t			*vp;
1709*0Sstevel@tonic-gate 	dev_t			dev;
1710*0Sstevel@tonic-gate 	int			ret;
1711*0Sstevel@tonic-gate 
1712*0Sstevel@tonic-gate 	if (lh == NULL)
1713*0Sstevel@tonic-gate 		return (EINVAL);
1714*0Sstevel@tonic-gate 
1715*0Sstevel@tonic-gate 	vp = handlep->lh_vp;
1716*0Sstevel@tonic-gate 	dev = vp->v_rdev;
1717*0Sstevel@tonic-gate 	if (handlep->lh_type & LH_CBDEV) {
1718*0Sstevel@tonic-gate 		ret = cdev_write(dev, uiop, credp);
1719*0Sstevel@tonic-gate 	} else if (handlep->lh_type & LH_STREAM) {
1720*0Sstevel@tonic-gate 		ret = strwrite(vp, uiop, credp);
1721*0Sstevel@tonic-gate 	} else {
1722*0Sstevel@tonic-gate 		return (ENOTSUP);
1723*0Sstevel@tonic-gate 	}
1724*0Sstevel@tonic-gate 	return (ret);
1725*0Sstevel@tonic-gate }
1726*0Sstevel@tonic-gate 
1727*0Sstevel@tonic-gate int
1728*0Sstevel@tonic-gate ldi_get_size(ldi_handle_t lh, uint64_t *sizep)
1729*0Sstevel@tonic-gate {
1730*0Sstevel@tonic-gate 	int 			otyp;
1731*0Sstevel@tonic-gate 	uint_t			value;
1732*0Sstevel@tonic-gate 	int64_t			drv_prop64;
1733*0Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
1734*0Sstevel@tonic-gate 
1735*0Sstevel@tonic-gate 
1736*0Sstevel@tonic-gate 	if ((lh == NULL) || (sizep == NULL))
1737*0Sstevel@tonic-gate 		return (DDI_FAILURE);
1738*0Sstevel@tonic-gate 
1739*0Sstevel@tonic-gate 	if (handlep->lh_type & LH_STREAM)
1740*0Sstevel@tonic-gate 		return (DDI_FAILURE);
1741*0Sstevel@tonic-gate 
1742*0Sstevel@tonic-gate 	/*
1743*0Sstevel@tonic-gate 	 * Determine device type (char or block).
1744*0Sstevel@tonic-gate 	 * Character devices support Size/size
1745*0Sstevel@tonic-gate 	 * property value. Block devices may support
1746*0Sstevel@tonic-gate 	 * Nblocks/nblocks or Size/size property value.
1747*0Sstevel@tonic-gate 	 */
1748*0Sstevel@tonic-gate 	if ((ldi_get_otyp(lh, &otyp)) != 0)
1749*0Sstevel@tonic-gate 		return (DDI_FAILURE);
1750*0Sstevel@tonic-gate 
1751*0Sstevel@tonic-gate 	if (otyp == OTYP_BLK) {
1752*0Sstevel@tonic-gate 		if (ldi_prop_exists(lh,
1753*0Sstevel@tonic-gate 			DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "Nblocks")) {
1754*0Sstevel@tonic-gate 
1755*0Sstevel@tonic-gate 			drv_prop64 = ldi_prop_get_int64(lh,
1756*0Sstevel@tonic-gate 			    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1757*0Sstevel@tonic-gate 			    "Nblocks", 0);
1758*0Sstevel@tonic-gate 			*sizep = (uint64_t)ldbtob((uint64_t)drv_prop64);
1759*0Sstevel@tonic-gate 			return (DDI_SUCCESS);
1760*0Sstevel@tonic-gate 		}
1761*0Sstevel@tonic-gate 
1762*0Sstevel@tonic-gate 		if (ldi_prop_exists(lh,
1763*0Sstevel@tonic-gate 			DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "nblocks")) {
1764*0Sstevel@tonic-gate 
1765*0Sstevel@tonic-gate 			value = ldi_prop_get_int(lh,
1766*0Sstevel@tonic-gate 			    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1767*0Sstevel@tonic-gate 			    "nblocks", 0);
1768*0Sstevel@tonic-gate 			*sizep = (uint64_t)ldbtob(value);
1769*0Sstevel@tonic-gate 			return (DDI_SUCCESS);
1770*0Sstevel@tonic-gate 		}
1771*0Sstevel@tonic-gate 	}
1772*0Sstevel@tonic-gate 
1773*0Sstevel@tonic-gate 	if (ldi_prop_exists(lh,
1774*0Sstevel@tonic-gate 		DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "Size")) {
1775*0Sstevel@tonic-gate 
1776*0Sstevel@tonic-gate 		drv_prop64 = ldi_prop_get_int64(lh,
1777*0Sstevel@tonic-gate 		    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "Size", 0);
1778*0Sstevel@tonic-gate 		*sizep = (uint64_t)drv_prop64;
1779*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
1780*0Sstevel@tonic-gate 	}
1781*0Sstevel@tonic-gate 
1782*0Sstevel@tonic-gate 	if (ldi_prop_exists(lh,
1783*0Sstevel@tonic-gate 		DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "size")) {
1784*0Sstevel@tonic-gate 
1785*0Sstevel@tonic-gate 		value = ldi_prop_get_int(lh,
1786*0Sstevel@tonic-gate 		    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "size", 0);
1787*0Sstevel@tonic-gate 		*sizep = (uint64_t)value;
1788*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
1789*0Sstevel@tonic-gate 	}
1790*0Sstevel@tonic-gate 
1791*0Sstevel@tonic-gate 	/* unable to determine device size */
1792*0Sstevel@tonic-gate 	return (DDI_FAILURE);
1793*0Sstevel@tonic-gate }
1794*0Sstevel@tonic-gate 
1795*0Sstevel@tonic-gate int
1796*0Sstevel@tonic-gate ldi_ioctl(ldi_handle_t lh, int cmd, intptr_t arg, int mode,
1797*0Sstevel@tonic-gate 	cred_t *cr, int *rvalp)
1798*0Sstevel@tonic-gate {
1799*0Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
1800*0Sstevel@tonic-gate 	vnode_t			*vp;
1801*0Sstevel@tonic-gate 	dev_t			dev;
1802*0Sstevel@tonic-gate 	int			ret, copymode;
1803*0Sstevel@tonic-gate 
1804*0Sstevel@tonic-gate 	if (lh == NULL)
1805*0Sstevel@tonic-gate 		return (EINVAL);
1806*0Sstevel@tonic-gate 
1807*0Sstevel@tonic-gate 	/*
1808*0Sstevel@tonic-gate 	 * if the data pointed to by arg is located in the kernel then
1809*0Sstevel@tonic-gate 	 * make sure the FNATIVE flag is set.
1810*0Sstevel@tonic-gate 	 */
1811*0Sstevel@tonic-gate 	if (mode & FKIOCTL)
1812*0Sstevel@tonic-gate 		mode = (mode & ~FMODELS) | FNATIVE | FKIOCTL;
1813*0Sstevel@tonic-gate 
1814*0Sstevel@tonic-gate 	vp = handlep->lh_vp;
1815*0Sstevel@tonic-gate 	dev = vp->v_rdev;
1816*0Sstevel@tonic-gate 	if (handlep->lh_type & LH_CBDEV) {
1817*0Sstevel@tonic-gate 		ret = cdev_ioctl(dev, cmd, arg, mode, cr, rvalp);
1818*0Sstevel@tonic-gate 	} else if (handlep->lh_type & LH_STREAM) {
1819*0Sstevel@tonic-gate 		copymode = (mode & FKIOCTL) ? K_TO_K : U_TO_K;
1820*0Sstevel@tonic-gate 
1821*0Sstevel@tonic-gate 		/*
1822*0Sstevel@tonic-gate 		 * if we get an I_PLINK from within the kernel the
1823*0Sstevel@tonic-gate 		 * arg is a layered handle pointer instead of
1824*0Sstevel@tonic-gate 		 * a file descriptor, so we translate this ioctl
1825*0Sstevel@tonic-gate 		 * into a private one that can handle this.
1826*0Sstevel@tonic-gate 		 */
1827*0Sstevel@tonic-gate 		if ((mode & FKIOCTL) && (cmd == I_PLINK))
1828*0Sstevel@tonic-gate 			cmd = _I_PLINK_LH;
1829*0Sstevel@tonic-gate 
1830*0Sstevel@tonic-gate 		ret = strioctl(vp, cmd, arg, mode, copymode, cr, rvalp);
1831*0Sstevel@tonic-gate 	} else {
1832*0Sstevel@tonic-gate 		return (ENOTSUP);
1833*0Sstevel@tonic-gate 	}
1834*0Sstevel@tonic-gate 
1835*0Sstevel@tonic-gate 	return (ret);
1836*0Sstevel@tonic-gate }
1837*0Sstevel@tonic-gate 
1838*0Sstevel@tonic-gate int
1839*0Sstevel@tonic-gate ldi_poll(ldi_handle_t lh, short events, int anyyet, short *reventsp,
1840*0Sstevel@tonic-gate     struct pollhead **phpp)
1841*0Sstevel@tonic-gate {
1842*0Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
1843*0Sstevel@tonic-gate 	vnode_t			*vp;
1844*0Sstevel@tonic-gate 	dev_t			dev;
1845*0Sstevel@tonic-gate 	int			ret;
1846*0Sstevel@tonic-gate 
1847*0Sstevel@tonic-gate 	if (lh == NULL)
1848*0Sstevel@tonic-gate 		return (EINVAL);
1849*0Sstevel@tonic-gate 
1850*0Sstevel@tonic-gate 	vp = handlep->lh_vp;
1851*0Sstevel@tonic-gate 	dev = vp->v_rdev;
1852*0Sstevel@tonic-gate 	if (handlep->lh_type & LH_CBDEV) {
1853*0Sstevel@tonic-gate 		ret = cdev_poll(dev, events, anyyet, reventsp, phpp);
1854*0Sstevel@tonic-gate 	} else if (handlep->lh_type & LH_STREAM) {
1855*0Sstevel@tonic-gate 		ret = strpoll(vp->v_stream, events, anyyet, reventsp, phpp);
1856*0Sstevel@tonic-gate 	} else {
1857*0Sstevel@tonic-gate 		return (ENOTSUP);
1858*0Sstevel@tonic-gate 	}
1859*0Sstevel@tonic-gate 
1860*0Sstevel@tonic-gate 	return (ret);
1861*0Sstevel@tonic-gate }
1862*0Sstevel@tonic-gate 
1863*0Sstevel@tonic-gate int
1864*0Sstevel@tonic-gate ldi_prop_op(ldi_handle_t lh, ddi_prop_op_t prop_op,
1865*0Sstevel@tonic-gate 	int flags, char *name, caddr_t valuep, int *length)
1866*0Sstevel@tonic-gate {
1867*0Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
1868*0Sstevel@tonic-gate 	dev_t			dev;
1869*0Sstevel@tonic-gate 	dev_info_t		*dip;
1870*0Sstevel@tonic-gate 	int			ret;
1871*0Sstevel@tonic-gate 	struct snode		*csp;
1872*0Sstevel@tonic-gate 
1873*0Sstevel@tonic-gate 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
1874*0Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
1875*0Sstevel@tonic-gate 
1876*0Sstevel@tonic-gate 	if ((prop_op != PROP_LEN) && (valuep == NULL))
1877*0Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
1878*0Sstevel@tonic-gate 
1879*0Sstevel@tonic-gate 	if (length == NULL)
1880*0Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
1881*0Sstevel@tonic-gate 
1882*0Sstevel@tonic-gate 	/*
1883*0Sstevel@tonic-gate 	 * try to find the associated dip,
1884*0Sstevel@tonic-gate 	 * this places a hold on the driver
1885*0Sstevel@tonic-gate 	 */
1886*0Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
1887*0Sstevel@tonic-gate 
1888*0Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
1889*0Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
1890*0Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
1891*0Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
1892*0Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
1893*0Sstevel@tonic-gate 	if (dip == NULL)
1894*0Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
1895*0Sstevel@tonic-gate 
1896*0Sstevel@tonic-gate 	if (dip == NULL)
1897*0Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
1898*0Sstevel@tonic-gate 
1899*0Sstevel@tonic-gate 	ret = i_ldi_prop_op(dev, dip, prop_op, flags, name, valuep, length);
1900*0Sstevel@tonic-gate 	ddi_release_devi(dip);
1901*0Sstevel@tonic-gate 
1902*0Sstevel@tonic-gate 	return (ret);
1903*0Sstevel@tonic-gate }
1904*0Sstevel@tonic-gate 
1905*0Sstevel@tonic-gate int
1906*0Sstevel@tonic-gate ldi_strategy(ldi_handle_t lh, struct buf *bp)
1907*0Sstevel@tonic-gate {
1908*0Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
1909*0Sstevel@tonic-gate 	dev_t			dev;
1910*0Sstevel@tonic-gate 
1911*0Sstevel@tonic-gate 	if ((lh == NULL) || (bp == NULL))
1912*0Sstevel@tonic-gate 		return (EINVAL);
1913*0Sstevel@tonic-gate 
1914*0Sstevel@tonic-gate 	/* this entry point is only supported for cb devices */
1915*0Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
1916*0Sstevel@tonic-gate 	if (!(handlep->lh_type & LH_CBDEV))
1917*0Sstevel@tonic-gate 		return (ENOTSUP);
1918*0Sstevel@tonic-gate 
1919*0Sstevel@tonic-gate 	bp->b_edev = dev;
1920*0Sstevel@tonic-gate 	bp->b_dev = cmpdev(dev);
1921*0Sstevel@tonic-gate 	return (bdev_strategy(bp));
1922*0Sstevel@tonic-gate }
1923*0Sstevel@tonic-gate 
1924*0Sstevel@tonic-gate int
1925*0Sstevel@tonic-gate ldi_dump(ldi_handle_t lh, caddr_t addr, daddr_t blkno, int nblk)
1926*0Sstevel@tonic-gate {
1927*0Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
1928*0Sstevel@tonic-gate 	dev_t			dev;
1929*0Sstevel@tonic-gate 
1930*0Sstevel@tonic-gate 	if (lh == NULL)
1931*0Sstevel@tonic-gate 		return (EINVAL);
1932*0Sstevel@tonic-gate 
1933*0Sstevel@tonic-gate 	/* this entry point is only supported for cb devices */
1934*0Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
1935*0Sstevel@tonic-gate 	if (!(handlep->lh_type & LH_CBDEV))
1936*0Sstevel@tonic-gate 		return (ENOTSUP);
1937*0Sstevel@tonic-gate 
1938*0Sstevel@tonic-gate 	return (bdev_dump(dev, addr, blkno, nblk));
1939*0Sstevel@tonic-gate }
1940*0Sstevel@tonic-gate 
1941*0Sstevel@tonic-gate int
1942*0Sstevel@tonic-gate ldi_devmap(ldi_handle_t lh, devmap_cookie_t dhp, offset_t off,
1943*0Sstevel@tonic-gate     size_t len, size_t *maplen, uint_t model)
1944*0Sstevel@tonic-gate {
1945*0Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
1946*0Sstevel@tonic-gate 	dev_t			dev;
1947*0Sstevel@tonic-gate 
1948*0Sstevel@tonic-gate 	if (lh == NULL)
1949*0Sstevel@tonic-gate 		return (EINVAL);
1950*0Sstevel@tonic-gate 
1951*0Sstevel@tonic-gate 	/* this entry point is only supported for cb devices */
1952*0Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
1953*0Sstevel@tonic-gate 	if (!(handlep->lh_type & LH_CBDEV))
1954*0Sstevel@tonic-gate 		return (ENOTSUP);
1955*0Sstevel@tonic-gate 
1956*0Sstevel@tonic-gate 	return (cdev_devmap(dev, dhp, off, len, maplen, model));
1957*0Sstevel@tonic-gate }
1958*0Sstevel@tonic-gate 
1959*0Sstevel@tonic-gate int
1960*0Sstevel@tonic-gate ldi_aread(ldi_handle_t lh, struct aio_req *aio_reqp, cred_t *cr)
1961*0Sstevel@tonic-gate {
1962*0Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
1963*0Sstevel@tonic-gate 	dev_t			dev;
1964*0Sstevel@tonic-gate 	struct cb_ops		*cb;
1965*0Sstevel@tonic-gate 
1966*0Sstevel@tonic-gate 	if (lh == NULL)
1967*0Sstevel@tonic-gate 		return (EINVAL);
1968*0Sstevel@tonic-gate 
1969*0Sstevel@tonic-gate 	/* this entry point is only supported for cb devices */
1970*0Sstevel@tonic-gate 	if (!(handlep->lh_type & LH_CBDEV))
1971*0Sstevel@tonic-gate 		return (ENOTSUP);
1972*0Sstevel@tonic-gate 
1973*0Sstevel@tonic-gate 	/*
1974*0Sstevel@tonic-gate 	 * Kaio is only supported on block devices.
1975*0Sstevel@tonic-gate 	 */
1976*0Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
1977*0Sstevel@tonic-gate 	cb = devopsp[getmajor(dev)]->devo_cb_ops;
1978*0Sstevel@tonic-gate 	if (cb->cb_strategy == nodev || cb->cb_strategy == NULL)
1979*0Sstevel@tonic-gate 		return (ENOTSUP);
1980*0Sstevel@tonic-gate 
1981*0Sstevel@tonic-gate 	if (cb->cb_aread == NULL)
1982*0Sstevel@tonic-gate 		return (ENOTSUP);
1983*0Sstevel@tonic-gate 
1984*0Sstevel@tonic-gate 	return (cb->cb_aread(dev, aio_reqp, cr));
1985*0Sstevel@tonic-gate }
1986*0Sstevel@tonic-gate 
1987*0Sstevel@tonic-gate int
1988*0Sstevel@tonic-gate ldi_awrite(ldi_handle_t lh, struct aio_req *aio_reqp, cred_t *cr)
1989*0Sstevel@tonic-gate {
1990*0Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
1991*0Sstevel@tonic-gate 	struct cb_ops		*cb;
1992*0Sstevel@tonic-gate 	dev_t			dev;
1993*0Sstevel@tonic-gate 
1994*0Sstevel@tonic-gate 	if (lh == NULL)
1995*0Sstevel@tonic-gate 		return (EINVAL);
1996*0Sstevel@tonic-gate 
1997*0Sstevel@tonic-gate 	/* this entry point is only supported for cb devices */
1998*0Sstevel@tonic-gate 	if (!(handlep->lh_type & LH_CBDEV))
1999*0Sstevel@tonic-gate 		return (ENOTSUP);
2000*0Sstevel@tonic-gate 
2001*0Sstevel@tonic-gate 	/*
2002*0Sstevel@tonic-gate 	 * Kaio is only supported on block devices.
2003*0Sstevel@tonic-gate 	 */
2004*0Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
2005*0Sstevel@tonic-gate 	cb = devopsp[getmajor(dev)]->devo_cb_ops;
2006*0Sstevel@tonic-gate 	if (cb->cb_strategy == nodev || cb->cb_strategy == NULL)
2007*0Sstevel@tonic-gate 		return (ENOTSUP);
2008*0Sstevel@tonic-gate 
2009*0Sstevel@tonic-gate 	if (cb->cb_awrite == NULL)
2010*0Sstevel@tonic-gate 		return (ENOTSUP);
2011*0Sstevel@tonic-gate 
2012*0Sstevel@tonic-gate 	return (cb->cb_awrite(dev, aio_reqp, cr));
2013*0Sstevel@tonic-gate }
2014*0Sstevel@tonic-gate 
2015*0Sstevel@tonic-gate int
2016*0Sstevel@tonic-gate ldi_putmsg(ldi_handle_t lh, mblk_t *smp)
2017*0Sstevel@tonic-gate {
2018*0Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2019*0Sstevel@tonic-gate 	int			ret;
2020*0Sstevel@tonic-gate 
2021*0Sstevel@tonic-gate 	if ((lh == NULL) || (smp == NULL))
2022*0Sstevel@tonic-gate 		return (EINVAL);
2023*0Sstevel@tonic-gate 
2024*0Sstevel@tonic-gate 	if (!(handlep->lh_type & LH_STREAM)) {
2025*0Sstevel@tonic-gate 		freemsg(smp);
2026*0Sstevel@tonic-gate 		return (ENOTSUP);
2027*0Sstevel@tonic-gate 	}
2028*0Sstevel@tonic-gate 
2029*0Sstevel@tonic-gate 	/* Send message while honoring flow control */
2030*0Sstevel@tonic-gate 	ret = kstrputmsg(handlep->lh_vp, smp, NULL, 0, 0,
2031*0Sstevel@tonic-gate 				MSG_BAND | MSG_HOLDSIG | MSG_IGNERROR, 0);
2032*0Sstevel@tonic-gate 
2033*0Sstevel@tonic-gate 	return (ret);
2034*0Sstevel@tonic-gate }
2035*0Sstevel@tonic-gate 
2036*0Sstevel@tonic-gate int
2037*0Sstevel@tonic-gate ldi_getmsg(ldi_handle_t lh, mblk_t **rmp, timestruc_t *timeo)
2038*0Sstevel@tonic-gate {
2039*0Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2040*0Sstevel@tonic-gate 	clock_t			timout; /* milliseconds */
2041*0Sstevel@tonic-gate 	uchar_t			pri;
2042*0Sstevel@tonic-gate 	rval_t			rval;
2043*0Sstevel@tonic-gate 	int			ret, pflag;
2044*0Sstevel@tonic-gate 
2045*0Sstevel@tonic-gate 
2046*0Sstevel@tonic-gate 	if (lh == NULL)
2047*0Sstevel@tonic-gate 		return (EINVAL);
2048*0Sstevel@tonic-gate 
2049*0Sstevel@tonic-gate 	if (!(handlep->lh_type & LH_STREAM))
2050*0Sstevel@tonic-gate 		return (ENOTSUP);
2051*0Sstevel@tonic-gate 
2052*0Sstevel@tonic-gate 	/* Convert from nanoseconds to milliseconds */
2053*0Sstevel@tonic-gate 	if (timeo != NULL) {
2054*0Sstevel@tonic-gate 		timout = timeo->tv_sec * 1000 + timeo->tv_nsec / 1000000;
2055*0Sstevel@tonic-gate 		if (timout > INT_MAX)
2056*0Sstevel@tonic-gate 			return (EINVAL);
2057*0Sstevel@tonic-gate 	} else
2058*0Sstevel@tonic-gate 		timout = -1;
2059*0Sstevel@tonic-gate 
2060*0Sstevel@tonic-gate 	/* Wait for timeout millseconds for a message */
2061*0Sstevel@tonic-gate 	pflag = MSG_ANY;
2062*0Sstevel@tonic-gate 	pri = 0;
2063*0Sstevel@tonic-gate 	*rmp = NULL;
2064*0Sstevel@tonic-gate 	ret = kstrgetmsg(handlep->lh_vp,
2065*0Sstevel@tonic-gate 				rmp, NULL, &pri, &pflag, timout, &rval);
2066*0Sstevel@tonic-gate 	return (ret);
2067*0Sstevel@tonic-gate }
2068*0Sstevel@tonic-gate 
2069*0Sstevel@tonic-gate int
2070*0Sstevel@tonic-gate ldi_get_dev(ldi_handle_t lh, dev_t *devp)
2071*0Sstevel@tonic-gate {
2072*0Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2073*0Sstevel@tonic-gate 
2074*0Sstevel@tonic-gate 	if ((lh == NULL) || (devp == NULL))
2075*0Sstevel@tonic-gate 		return (EINVAL);
2076*0Sstevel@tonic-gate 
2077*0Sstevel@tonic-gate 	*devp = handlep->lh_vp->v_rdev;
2078*0Sstevel@tonic-gate 	return (0);
2079*0Sstevel@tonic-gate }
2080*0Sstevel@tonic-gate 
2081*0Sstevel@tonic-gate int
2082*0Sstevel@tonic-gate ldi_get_otyp(ldi_handle_t lh, int *otyp)
2083*0Sstevel@tonic-gate {
2084*0Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2085*0Sstevel@tonic-gate 
2086*0Sstevel@tonic-gate 	if ((lh == NULL) || (otyp == NULL))
2087*0Sstevel@tonic-gate 		return (EINVAL);
2088*0Sstevel@tonic-gate 
2089*0Sstevel@tonic-gate 	*otyp = VTYP_TO_OTYP(handlep->lh_vp->v_type);
2090*0Sstevel@tonic-gate 	return (0);
2091*0Sstevel@tonic-gate }
2092*0Sstevel@tonic-gate 
2093*0Sstevel@tonic-gate int
2094*0Sstevel@tonic-gate ldi_get_devid(ldi_handle_t lh, ddi_devid_t *devid)
2095*0Sstevel@tonic-gate {
2096*0Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2097*0Sstevel@tonic-gate 	int			ret;
2098*0Sstevel@tonic-gate 	dev_t			dev;
2099*0Sstevel@tonic-gate 
2100*0Sstevel@tonic-gate 	if ((lh == NULL) || (devid == NULL))
2101*0Sstevel@tonic-gate 		return (EINVAL);
2102*0Sstevel@tonic-gate 
2103*0Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
2104*0Sstevel@tonic-gate 
2105*0Sstevel@tonic-gate 	ret = ddi_lyr_get_devid(dev, devid);
2106*0Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
2107*0Sstevel@tonic-gate 		return (ENOTSUP);
2108*0Sstevel@tonic-gate 
2109*0Sstevel@tonic-gate 	return (0);
2110*0Sstevel@tonic-gate }
2111*0Sstevel@tonic-gate 
2112*0Sstevel@tonic-gate int
2113*0Sstevel@tonic-gate ldi_get_minor_name(ldi_handle_t lh, char **minor_name)
2114*0Sstevel@tonic-gate {
2115*0Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2116*0Sstevel@tonic-gate 	int			ret, otyp;
2117*0Sstevel@tonic-gate 	dev_t			dev;
2118*0Sstevel@tonic-gate 
2119*0Sstevel@tonic-gate 	if ((lh == NULL) || (minor_name == NULL))
2120*0Sstevel@tonic-gate 		return (EINVAL);
2121*0Sstevel@tonic-gate 
2122*0Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
2123*0Sstevel@tonic-gate 	otyp = VTYP_TO_OTYP(handlep->lh_vp->v_type);
2124*0Sstevel@tonic-gate 
2125*0Sstevel@tonic-gate 	ret = ddi_lyr_get_minor_name(dev, OTYP_TO_STYP(otyp), minor_name);
2126*0Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
2127*0Sstevel@tonic-gate 		return (ENOTSUP);
2128*0Sstevel@tonic-gate 
2129*0Sstevel@tonic-gate 	return (0);
2130*0Sstevel@tonic-gate }
2131*0Sstevel@tonic-gate 
2132*0Sstevel@tonic-gate int
2133*0Sstevel@tonic-gate ldi_prop_lookup_int_array(ldi_handle_t lh,
2134*0Sstevel@tonic-gate     uint_t flags, char *name, int **data, uint_t *nelements)
2135*0Sstevel@tonic-gate {
2136*0Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2137*0Sstevel@tonic-gate 	dev_info_t		*dip;
2138*0Sstevel@tonic-gate 	dev_t			dev;
2139*0Sstevel@tonic-gate 	int			res;
2140*0Sstevel@tonic-gate 	struct snode		*csp;
2141*0Sstevel@tonic-gate 
2142*0Sstevel@tonic-gate 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
2143*0Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
2144*0Sstevel@tonic-gate 
2145*0Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
2146*0Sstevel@tonic-gate 
2147*0Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
2148*0Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
2149*0Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
2150*0Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
2151*0Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
2152*0Sstevel@tonic-gate 	if (dip == NULL)
2153*0Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
2154*0Sstevel@tonic-gate 
2155*0Sstevel@tonic-gate 	if (dip == NULL) {
2156*0Sstevel@tonic-gate 		flags |= DDI_UNBND_DLPI2;
2157*0Sstevel@tonic-gate 	} else if (flags & LDI_DEV_T_ANY) {
2158*0Sstevel@tonic-gate 		flags &= ~LDI_DEV_T_ANY;
2159*0Sstevel@tonic-gate 		dev = DDI_DEV_T_ANY;
2160*0Sstevel@tonic-gate 	}
2161*0Sstevel@tonic-gate 
2162*0Sstevel@tonic-gate 	if (dip != NULL) {
2163*0Sstevel@tonic-gate 		int *prop_val, prop_len;
2164*0Sstevel@tonic-gate 
2165*0Sstevel@tonic-gate 		res = i_ldi_prop_op_typed(dev, dip, flags, name,
2166*0Sstevel@tonic-gate 		    (caddr_t *)&prop_val, &prop_len, sizeof (int));
2167*0Sstevel@tonic-gate 
2168*0Sstevel@tonic-gate 		/* if we got it then return it */
2169*0Sstevel@tonic-gate 		if (res == DDI_PROP_SUCCESS) {
2170*0Sstevel@tonic-gate 			*nelements = prop_len / sizeof (int);
2171*0Sstevel@tonic-gate 			*data = prop_val;
2172*0Sstevel@tonic-gate 
2173*0Sstevel@tonic-gate 			ddi_release_devi(dip);
2174*0Sstevel@tonic-gate 			return (res);
2175*0Sstevel@tonic-gate 		}
2176*0Sstevel@tonic-gate 	}
2177*0Sstevel@tonic-gate 
2178*0Sstevel@tonic-gate 	/* call the normal property interfaces */
2179*0Sstevel@tonic-gate 	res = ddi_prop_lookup_int_array(dev, dip, flags,
2180*0Sstevel@tonic-gate 	    name, data, nelements);
2181*0Sstevel@tonic-gate 
2182*0Sstevel@tonic-gate 	if (dip != NULL)
2183*0Sstevel@tonic-gate 		ddi_release_devi(dip);
2184*0Sstevel@tonic-gate 
2185*0Sstevel@tonic-gate 	return (res);
2186*0Sstevel@tonic-gate }
2187*0Sstevel@tonic-gate 
2188*0Sstevel@tonic-gate int
2189*0Sstevel@tonic-gate ldi_prop_lookup_int64_array(ldi_handle_t lh,
2190*0Sstevel@tonic-gate     uint_t flags, char *name, int64_t **data, uint_t *nelements)
2191*0Sstevel@tonic-gate {
2192*0Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2193*0Sstevel@tonic-gate 	dev_info_t		*dip;
2194*0Sstevel@tonic-gate 	dev_t			dev;
2195*0Sstevel@tonic-gate 	int			res;
2196*0Sstevel@tonic-gate 	struct snode		*csp;
2197*0Sstevel@tonic-gate 
2198*0Sstevel@tonic-gate 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
2199*0Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
2200*0Sstevel@tonic-gate 
2201*0Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
2202*0Sstevel@tonic-gate 
2203*0Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
2204*0Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
2205*0Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
2206*0Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
2207*0Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
2208*0Sstevel@tonic-gate 	if (dip == NULL)
2209*0Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
2210*0Sstevel@tonic-gate 
2211*0Sstevel@tonic-gate 	if (dip == NULL) {
2212*0Sstevel@tonic-gate 		flags |= DDI_UNBND_DLPI2;
2213*0Sstevel@tonic-gate 	} else if (flags & LDI_DEV_T_ANY) {
2214*0Sstevel@tonic-gate 		flags &= ~LDI_DEV_T_ANY;
2215*0Sstevel@tonic-gate 		dev = DDI_DEV_T_ANY;
2216*0Sstevel@tonic-gate 	}
2217*0Sstevel@tonic-gate 
2218*0Sstevel@tonic-gate 	if (dip != NULL) {
2219*0Sstevel@tonic-gate 		int64_t	*prop_val;
2220*0Sstevel@tonic-gate 		int	prop_len;
2221*0Sstevel@tonic-gate 
2222*0Sstevel@tonic-gate 		res = i_ldi_prop_op_typed(dev, dip, flags, name,
2223*0Sstevel@tonic-gate 		    (caddr_t *)&prop_val, &prop_len, sizeof (int64_t));
2224*0Sstevel@tonic-gate 
2225*0Sstevel@tonic-gate 		/* if we got it then return it */
2226*0Sstevel@tonic-gate 		if (res == DDI_PROP_SUCCESS) {
2227*0Sstevel@tonic-gate 			*nelements = prop_len / sizeof (int64_t);
2228*0Sstevel@tonic-gate 			*data = prop_val;
2229*0Sstevel@tonic-gate 
2230*0Sstevel@tonic-gate 			ddi_release_devi(dip);
2231*0Sstevel@tonic-gate 			return (res);
2232*0Sstevel@tonic-gate 		}
2233*0Sstevel@tonic-gate 	}
2234*0Sstevel@tonic-gate 
2235*0Sstevel@tonic-gate 	/* call the normal property interfaces */
2236*0Sstevel@tonic-gate 	res = ddi_prop_lookup_int64_array(dev, dip, flags,
2237*0Sstevel@tonic-gate 	    name, data, nelements);
2238*0Sstevel@tonic-gate 
2239*0Sstevel@tonic-gate 	if (dip != NULL)
2240*0Sstevel@tonic-gate 		ddi_release_devi(dip);
2241*0Sstevel@tonic-gate 
2242*0Sstevel@tonic-gate 	return (res);
2243*0Sstevel@tonic-gate }
2244*0Sstevel@tonic-gate 
2245*0Sstevel@tonic-gate int
2246*0Sstevel@tonic-gate ldi_prop_lookup_string_array(ldi_handle_t lh,
2247*0Sstevel@tonic-gate     uint_t flags, char *name, char ***data, uint_t *nelements)
2248*0Sstevel@tonic-gate {
2249*0Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2250*0Sstevel@tonic-gate 	dev_info_t		*dip;
2251*0Sstevel@tonic-gate 	dev_t			dev;
2252*0Sstevel@tonic-gate 	int			res;
2253*0Sstevel@tonic-gate 	struct snode		*csp;
2254*0Sstevel@tonic-gate 
2255*0Sstevel@tonic-gate 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
2256*0Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
2257*0Sstevel@tonic-gate 
2258*0Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
2259*0Sstevel@tonic-gate 
2260*0Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
2261*0Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
2262*0Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
2263*0Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
2264*0Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
2265*0Sstevel@tonic-gate 	if (dip == NULL)
2266*0Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
2267*0Sstevel@tonic-gate 
2268*0Sstevel@tonic-gate 	if (dip == NULL) {
2269*0Sstevel@tonic-gate 		flags |= DDI_UNBND_DLPI2;
2270*0Sstevel@tonic-gate 	} else if (flags & LDI_DEV_T_ANY) {
2271*0Sstevel@tonic-gate 		flags &= ~LDI_DEV_T_ANY;
2272*0Sstevel@tonic-gate 		dev = DDI_DEV_T_ANY;
2273*0Sstevel@tonic-gate 	}
2274*0Sstevel@tonic-gate 
2275*0Sstevel@tonic-gate 	if (dip != NULL) {
2276*0Sstevel@tonic-gate 		char	*prop_val;
2277*0Sstevel@tonic-gate 		int	prop_len;
2278*0Sstevel@tonic-gate 
2279*0Sstevel@tonic-gate 		res = i_ldi_prop_op_typed(dev, dip, flags, name,
2280*0Sstevel@tonic-gate 		    (caddr_t *)&prop_val, &prop_len, 0);
2281*0Sstevel@tonic-gate 
2282*0Sstevel@tonic-gate 		/* if we got it then return it */
2283*0Sstevel@tonic-gate 		if (res == DDI_PROP_SUCCESS) {
2284*0Sstevel@tonic-gate 			char	**str_array;
2285*0Sstevel@tonic-gate 			int	nelem;
2286*0Sstevel@tonic-gate 
2287*0Sstevel@tonic-gate 			/*
2288*0Sstevel@tonic-gate 			 * pack the returned string array into the format
2289*0Sstevel@tonic-gate 			 * our callers expect
2290*0Sstevel@tonic-gate 			 */
2291*0Sstevel@tonic-gate 			if (i_pack_string_array(prop_val, prop_len,
2292*0Sstevel@tonic-gate 				&str_array, &nelem) == 0) {
2293*0Sstevel@tonic-gate 
2294*0Sstevel@tonic-gate 				*data = str_array;
2295*0Sstevel@tonic-gate 				*nelements = nelem;
2296*0Sstevel@tonic-gate 
2297*0Sstevel@tonic-gate 				ddi_prop_free(prop_val);
2298*0Sstevel@tonic-gate 				ddi_release_devi(dip);
2299*0Sstevel@tonic-gate 				return (res);
2300*0Sstevel@tonic-gate 			}
2301*0Sstevel@tonic-gate 
2302*0Sstevel@tonic-gate 			/*
2303*0Sstevel@tonic-gate 			 * the format of the returned property must have
2304*0Sstevel@tonic-gate 			 * been bad so throw it out
2305*0Sstevel@tonic-gate 			 */
2306*0Sstevel@tonic-gate 			ddi_prop_free(prop_val);
2307*0Sstevel@tonic-gate 		}
2308*0Sstevel@tonic-gate 	}
2309*0Sstevel@tonic-gate 
2310*0Sstevel@tonic-gate 	/* call the normal property interfaces */
2311*0Sstevel@tonic-gate 	res = ddi_prop_lookup_string_array(dev, dip, flags,
2312*0Sstevel@tonic-gate 	    name, data, nelements);
2313*0Sstevel@tonic-gate 
2314*0Sstevel@tonic-gate 	if (dip != NULL)
2315*0Sstevel@tonic-gate 		ddi_release_devi(dip);
2316*0Sstevel@tonic-gate 
2317*0Sstevel@tonic-gate 	return (res);
2318*0Sstevel@tonic-gate }
2319*0Sstevel@tonic-gate 
2320*0Sstevel@tonic-gate int
2321*0Sstevel@tonic-gate ldi_prop_lookup_string(ldi_handle_t lh,
2322*0Sstevel@tonic-gate     uint_t flags, char *name, char **data)
2323*0Sstevel@tonic-gate {
2324*0Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2325*0Sstevel@tonic-gate 	dev_info_t		*dip;
2326*0Sstevel@tonic-gate 	dev_t			dev;
2327*0Sstevel@tonic-gate 	int			res;
2328*0Sstevel@tonic-gate 	struct snode		*csp;
2329*0Sstevel@tonic-gate 
2330*0Sstevel@tonic-gate 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
2331*0Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
2332*0Sstevel@tonic-gate 
2333*0Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
2334*0Sstevel@tonic-gate 
2335*0Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
2336*0Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
2337*0Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
2338*0Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
2339*0Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
2340*0Sstevel@tonic-gate 	if (dip == NULL)
2341*0Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
2342*0Sstevel@tonic-gate 
2343*0Sstevel@tonic-gate 	if (dip == NULL) {
2344*0Sstevel@tonic-gate 		flags |= DDI_UNBND_DLPI2;
2345*0Sstevel@tonic-gate 	} else if (flags & LDI_DEV_T_ANY) {
2346*0Sstevel@tonic-gate 		flags &= ~LDI_DEV_T_ANY;
2347*0Sstevel@tonic-gate 		dev = DDI_DEV_T_ANY;
2348*0Sstevel@tonic-gate 	}
2349*0Sstevel@tonic-gate 
2350*0Sstevel@tonic-gate 	if (dip != NULL) {
2351*0Sstevel@tonic-gate 		char	*prop_val;
2352*0Sstevel@tonic-gate 		int	prop_len;
2353*0Sstevel@tonic-gate 
2354*0Sstevel@tonic-gate 		res = i_ldi_prop_op_typed(dev, dip, flags, name,
2355*0Sstevel@tonic-gate 		    (caddr_t *)&prop_val, &prop_len, 0);
2356*0Sstevel@tonic-gate 
2357*0Sstevel@tonic-gate 		/* if we got it then return it */
2358*0Sstevel@tonic-gate 		if (res == DDI_PROP_SUCCESS) {
2359*0Sstevel@tonic-gate 			/*
2360*0Sstevel@tonic-gate 			 * sanity check the vaule returned.
2361*0Sstevel@tonic-gate 			 */
2362*0Sstevel@tonic-gate 			if (i_check_string(prop_val, prop_len)) {
2363*0Sstevel@tonic-gate 				ddi_prop_free(prop_val);
2364*0Sstevel@tonic-gate 			} else {
2365*0Sstevel@tonic-gate 				*data = prop_val;
2366*0Sstevel@tonic-gate 				ddi_release_devi(dip);
2367*0Sstevel@tonic-gate 				return (res);
2368*0Sstevel@tonic-gate 			}
2369*0Sstevel@tonic-gate 		}
2370*0Sstevel@tonic-gate 	}
2371*0Sstevel@tonic-gate 
2372*0Sstevel@tonic-gate 	/* call the normal property interfaces */
2373*0Sstevel@tonic-gate 	res = ddi_prop_lookup_string(dev, dip, flags, name, data);
2374*0Sstevel@tonic-gate 
2375*0Sstevel@tonic-gate 	if (dip != NULL)
2376*0Sstevel@tonic-gate 		ddi_release_devi(dip);
2377*0Sstevel@tonic-gate 
2378*0Sstevel@tonic-gate #ifdef DEBUG
2379*0Sstevel@tonic-gate 	if (res == DDI_PROP_SUCCESS) {
2380*0Sstevel@tonic-gate 		/*
2381*0Sstevel@tonic-gate 		 * keep ourselves honest
2382*0Sstevel@tonic-gate 		 * make sure the framework returns strings in the
2383*0Sstevel@tonic-gate 		 * same format as we're demanding from drivers.
2384*0Sstevel@tonic-gate 		 */
2385*0Sstevel@tonic-gate 		struct prop_driver_data	*pdd;
2386*0Sstevel@tonic-gate 		int			pdd_prop_size;
2387*0Sstevel@tonic-gate 
2388*0Sstevel@tonic-gate 		pdd = ((struct prop_driver_data *)(*data)) - 1;
2389*0Sstevel@tonic-gate 		pdd_prop_size = pdd->pdd_size -
2390*0Sstevel@tonic-gate 		    sizeof (struct prop_driver_data);
2391*0Sstevel@tonic-gate 		ASSERT(i_check_string(*data, pdd_prop_size) == 0);
2392*0Sstevel@tonic-gate 	}
2393*0Sstevel@tonic-gate #endif /* DEBUG */
2394*0Sstevel@tonic-gate 
2395*0Sstevel@tonic-gate 	return (res);
2396*0Sstevel@tonic-gate }
2397*0Sstevel@tonic-gate 
2398*0Sstevel@tonic-gate int
2399*0Sstevel@tonic-gate ldi_prop_lookup_byte_array(ldi_handle_t lh,
2400*0Sstevel@tonic-gate     uint_t flags, char *name, uchar_t **data, uint_t *nelements)
2401*0Sstevel@tonic-gate {
2402*0Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2403*0Sstevel@tonic-gate 	dev_info_t		*dip;
2404*0Sstevel@tonic-gate 	dev_t			dev;
2405*0Sstevel@tonic-gate 	int			res;
2406*0Sstevel@tonic-gate 	struct snode		*csp;
2407*0Sstevel@tonic-gate 
2408*0Sstevel@tonic-gate 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
2409*0Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
2410*0Sstevel@tonic-gate 
2411*0Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
2412*0Sstevel@tonic-gate 
2413*0Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
2414*0Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
2415*0Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
2416*0Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
2417*0Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
2418*0Sstevel@tonic-gate 	if (dip == NULL)
2419*0Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
2420*0Sstevel@tonic-gate 
2421*0Sstevel@tonic-gate 	if (dip == NULL) {
2422*0Sstevel@tonic-gate 		flags |= DDI_UNBND_DLPI2;
2423*0Sstevel@tonic-gate 	} else if (flags & LDI_DEV_T_ANY) {
2424*0Sstevel@tonic-gate 		flags &= ~LDI_DEV_T_ANY;
2425*0Sstevel@tonic-gate 		dev = DDI_DEV_T_ANY;
2426*0Sstevel@tonic-gate 	}
2427*0Sstevel@tonic-gate 
2428*0Sstevel@tonic-gate 	if (dip != NULL) {
2429*0Sstevel@tonic-gate 		uchar_t	*prop_val;
2430*0Sstevel@tonic-gate 		int	prop_len;
2431*0Sstevel@tonic-gate 
2432*0Sstevel@tonic-gate 		res = i_ldi_prop_op_typed(dev, dip, flags, name,
2433*0Sstevel@tonic-gate 		    (caddr_t *)&prop_val, &prop_len, sizeof (uchar_t));
2434*0Sstevel@tonic-gate 
2435*0Sstevel@tonic-gate 		/* if we got it then return it */
2436*0Sstevel@tonic-gate 		if (res == DDI_PROP_SUCCESS) {
2437*0Sstevel@tonic-gate 			*nelements = prop_len / sizeof (uchar_t);
2438*0Sstevel@tonic-gate 			*data = prop_val;
2439*0Sstevel@tonic-gate 
2440*0Sstevel@tonic-gate 			ddi_release_devi(dip);
2441*0Sstevel@tonic-gate 			return (res);
2442*0Sstevel@tonic-gate 		}
2443*0Sstevel@tonic-gate 	}
2444*0Sstevel@tonic-gate 
2445*0Sstevel@tonic-gate 	/* call the normal property interfaces */
2446*0Sstevel@tonic-gate 	res = ddi_prop_lookup_byte_array(dev, dip, flags,
2447*0Sstevel@tonic-gate 	    name, data, nelements);
2448*0Sstevel@tonic-gate 
2449*0Sstevel@tonic-gate 	if (dip != NULL)
2450*0Sstevel@tonic-gate 		ddi_release_devi(dip);
2451*0Sstevel@tonic-gate 
2452*0Sstevel@tonic-gate 	return (res);
2453*0Sstevel@tonic-gate }
2454*0Sstevel@tonic-gate 
2455*0Sstevel@tonic-gate int
2456*0Sstevel@tonic-gate ldi_prop_get_int(ldi_handle_t lh,
2457*0Sstevel@tonic-gate     uint_t flags, char *name, int defvalue)
2458*0Sstevel@tonic-gate {
2459*0Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2460*0Sstevel@tonic-gate 	dev_info_t		*dip;
2461*0Sstevel@tonic-gate 	dev_t			dev;
2462*0Sstevel@tonic-gate 	int			res;
2463*0Sstevel@tonic-gate 	struct snode		*csp;
2464*0Sstevel@tonic-gate 
2465*0Sstevel@tonic-gate 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
2466*0Sstevel@tonic-gate 		return (defvalue);
2467*0Sstevel@tonic-gate 
2468*0Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
2469*0Sstevel@tonic-gate 
2470*0Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
2471*0Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
2472*0Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
2473*0Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
2474*0Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
2475*0Sstevel@tonic-gate 	if (dip == NULL)
2476*0Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
2477*0Sstevel@tonic-gate 
2478*0Sstevel@tonic-gate 	if (dip == NULL) {
2479*0Sstevel@tonic-gate 		flags |= DDI_UNBND_DLPI2;
2480*0Sstevel@tonic-gate 	} else if (flags & LDI_DEV_T_ANY) {
2481*0Sstevel@tonic-gate 		flags &= ~LDI_DEV_T_ANY;
2482*0Sstevel@tonic-gate 		dev = DDI_DEV_T_ANY;
2483*0Sstevel@tonic-gate 	}
2484*0Sstevel@tonic-gate 
2485*0Sstevel@tonic-gate 	if (dip != NULL) {
2486*0Sstevel@tonic-gate 		int	prop_val;
2487*0Sstevel@tonic-gate 		int	prop_len;
2488*0Sstevel@tonic-gate 
2489*0Sstevel@tonic-gate 		/*
2490*0Sstevel@tonic-gate 		 * first call the drivers prop_op interface to allow it
2491*0Sstevel@tonic-gate 		 * it to override default property values.
2492*0Sstevel@tonic-gate 		 */
2493*0Sstevel@tonic-gate 		prop_len = sizeof (int);
2494*0Sstevel@tonic-gate 		res = i_ldi_prop_op(dev, dip, PROP_LEN_AND_VAL_BUF,
2495*0Sstevel@tonic-gate 		    flags | DDI_PROP_DYNAMIC, name,
2496*0Sstevel@tonic-gate 		    (caddr_t)&prop_val, &prop_len);
2497*0Sstevel@tonic-gate 
2498*0Sstevel@tonic-gate 		/* if we got it then return it */
2499*0Sstevel@tonic-gate 		if ((res == DDI_PROP_SUCCESS) &&
2500*0Sstevel@tonic-gate 		    (prop_len == sizeof (int))) {
2501*0Sstevel@tonic-gate 			res = prop_val;
2502*0Sstevel@tonic-gate 			ddi_release_devi(dip);
2503*0Sstevel@tonic-gate 			return (res);
2504*0Sstevel@tonic-gate 		}
2505*0Sstevel@tonic-gate 	}
2506*0Sstevel@tonic-gate 
2507*0Sstevel@tonic-gate 	/* call the normal property interfaces */
2508*0Sstevel@tonic-gate 	res = ddi_prop_get_int(dev, dip, flags, name, defvalue);
2509*0Sstevel@tonic-gate 
2510*0Sstevel@tonic-gate 	if (dip != NULL)
2511*0Sstevel@tonic-gate 		ddi_release_devi(dip);
2512*0Sstevel@tonic-gate 
2513*0Sstevel@tonic-gate 	return (res);
2514*0Sstevel@tonic-gate }
2515*0Sstevel@tonic-gate 
2516*0Sstevel@tonic-gate int64_t
2517*0Sstevel@tonic-gate ldi_prop_get_int64(ldi_handle_t lh,
2518*0Sstevel@tonic-gate     uint_t flags, char *name, int64_t defvalue)
2519*0Sstevel@tonic-gate {
2520*0Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2521*0Sstevel@tonic-gate 	dev_info_t		*dip;
2522*0Sstevel@tonic-gate 	dev_t			dev;
2523*0Sstevel@tonic-gate 	int64_t			res;
2524*0Sstevel@tonic-gate 	struct snode		*csp;
2525*0Sstevel@tonic-gate 
2526*0Sstevel@tonic-gate 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
2527*0Sstevel@tonic-gate 		return (defvalue);
2528*0Sstevel@tonic-gate 
2529*0Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
2530*0Sstevel@tonic-gate 
2531*0Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
2532*0Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
2533*0Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
2534*0Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
2535*0Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
2536*0Sstevel@tonic-gate 	if (dip == NULL)
2537*0Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
2538*0Sstevel@tonic-gate 
2539*0Sstevel@tonic-gate 	if (dip == NULL) {
2540*0Sstevel@tonic-gate 		flags |= DDI_UNBND_DLPI2;
2541*0Sstevel@tonic-gate 	} else if (flags & LDI_DEV_T_ANY) {
2542*0Sstevel@tonic-gate 		flags &= ~LDI_DEV_T_ANY;
2543*0Sstevel@tonic-gate 		dev = DDI_DEV_T_ANY;
2544*0Sstevel@tonic-gate 	}
2545*0Sstevel@tonic-gate 
2546*0Sstevel@tonic-gate 	if (dip != NULL) {
2547*0Sstevel@tonic-gate 		int64_t	prop_val;
2548*0Sstevel@tonic-gate 		int	prop_len;
2549*0Sstevel@tonic-gate 
2550*0Sstevel@tonic-gate 		/*
2551*0Sstevel@tonic-gate 		 * first call the drivers prop_op interface to allow it
2552*0Sstevel@tonic-gate 		 * it to override default property values.
2553*0Sstevel@tonic-gate 		 */
2554*0Sstevel@tonic-gate 		prop_len = sizeof (int64_t);
2555*0Sstevel@tonic-gate 		res = i_ldi_prop_op(dev, dip, PROP_LEN_AND_VAL_BUF,
2556*0Sstevel@tonic-gate 		    flags | DDI_PROP_DYNAMIC, name,
2557*0Sstevel@tonic-gate 		    (caddr_t)&prop_val, &prop_len);
2558*0Sstevel@tonic-gate 
2559*0Sstevel@tonic-gate 		/* if we got it then return it */
2560*0Sstevel@tonic-gate 		if ((res == DDI_PROP_SUCCESS) &&
2561*0Sstevel@tonic-gate 		    (prop_len == sizeof (int64_t))) {
2562*0Sstevel@tonic-gate 			res = prop_val;
2563*0Sstevel@tonic-gate 			ddi_release_devi(dip);
2564*0Sstevel@tonic-gate 			return (res);
2565*0Sstevel@tonic-gate 		}
2566*0Sstevel@tonic-gate 	}
2567*0Sstevel@tonic-gate 
2568*0Sstevel@tonic-gate 	/* call the normal property interfaces */
2569*0Sstevel@tonic-gate 	res = ddi_prop_get_int64(dev, dip, flags, name, defvalue);
2570*0Sstevel@tonic-gate 
2571*0Sstevel@tonic-gate 	if (dip != NULL)
2572*0Sstevel@tonic-gate 		ddi_release_devi(dip);
2573*0Sstevel@tonic-gate 
2574*0Sstevel@tonic-gate 	return (res);
2575*0Sstevel@tonic-gate }
2576*0Sstevel@tonic-gate 
2577*0Sstevel@tonic-gate int
2578*0Sstevel@tonic-gate ldi_prop_exists(ldi_handle_t lh, uint_t flags, char *name)
2579*0Sstevel@tonic-gate {
2580*0Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2581*0Sstevel@tonic-gate 	dev_info_t 		*dip;
2582*0Sstevel@tonic-gate 	dev_t 			dev;
2583*0Sstevel@tonic-gate 	int			res, prop_len;
2584*0Sstevel@tonic-gate 	struct snode		*csp;
2585*0Sstevel@tonic-gate 
2586*0Sstevel@tonic-gate 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
2587*0Sstevel@tonic-gate 		return (0);
2588*0Sstevel@tonic-gate 
2589*0Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
2590*0Sstevel@tonic-gate 
2591*0Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
2592*0Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
2593*0Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
2594*0Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
2595*0Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
2596*0Sstevel@tonic-gate 	if (dip == NULL)
2597*0Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
2598*0Sstevel@tonic-gate 
2599*0Sstevel@tonic-gate 	/* if NULL dip, prop does NOT exist */
2600*0Sstevel@tonic-gate 	if (dip == NULL)
2601*0Sstevel@tonic-gate 		return (0);
2602*0Sstevel@tonic-gate 
2603*0Sstevel@tonic-gate 	if (flags & LDI_DEV_T_ANY) {
2604*0Sstevel@tonic-gate 		flags &= ~LDI_DEV_T_ANY;
2605*0Sstevel@tonic-gate 		dev = DDI_DEV_T_ANY;
2606*0Sstevel@tonic-gate 	}
2607*0Sstevel@tonic-gate 
2608*0Sstevel@tonic-gate 	/*
2609*0Sstevel@tonic-gate 	 * first call the drivers prop_op interface to allow it
2610*0Sstevel@tonic-gate 	 * it to override default property values.
2611*0Sstevel@tonic-gate 	 */
2612*0Sstevel@tonic-gate 	res = i_ldi_prop_op(dev, dip, PROP_LEN,
2613*0Sstevel@tonic-gate 	    flags | DDI_PROP_DYNAMIC, name, NULL, &prop_len);
2614*0Sstevel@tonic-gate 
2615*0Sstevel@tonic-gate 	if (res == DDI_PROP_SUCCESS) {
2616*0Sstevel@tonic-gate 		ddi_release_devi(dip);
2617*0Sstevel@tonic-gate 		return (1);
2618*0Sstevel@tonic-gate 	}
2619*0Sstevel@tonic-gate 
2620*0Sstevel@tonic-gate 	/* call the normal property interfaces */
2621*0Sstevel@tonic-gate 	res = ddi_prop_exists(dev, dip, flags, name);
2622*0Sstevel@tonic-gate 
2623*0Sstevel@tonic-gate 	ddi_release_devi(dip);
2624*0Sstevel@tonic-gate 	return (res);
2625*0Sstevel@tonic-gate }
2626*0Sstevel@tonic-gate 
2627*0Sstevel@tonic-gate int
2628*0Sstevel@tonic-gate ldi_get_eventcookie(ldi_handle_t lh, char *name, ddi_eventcookie_t *ecp)
2629*0Sstevel@tonic-gate {
2630*0Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2631*0Sstevel@tonic-gate 	dev_info_t		*dip;
2632*0Sstevel@tonic-gate 	dev_t			dev;
2633*0Sstevel@tonic-gate 	int			res;
2634*0Sstevel@tonic-gate 	struct snode		*csp;
2635*0Sstevel@tonic-gate 
2636*0Sstevel@tonic-gate 	if ((lh == NULL) || (name == NULL) ||
2637*0Sstevel@tonic-gate 	    (strlen(name) == 0) || (ecp == NULL)) {
2638*0Sstevel@tonic-gate 		return (DDI_FAILURE);
2639*0Sstevel@tonic-gate 	}
2640*0Sstevel@tonic-gate 
2641*0Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
2642*0Sstevel@tonic-gate 
2643*0Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
2644*0Sstevel@tonic-gate 
2645*0Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
2646*0Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
2647*0Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
2648*0Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
2649*0Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
2650*0Sstevel@tonic-gate 	if (dip == NULL)
2651*0Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
2652*0Sstevel@tonic-gate 
2653*0Sstevel@tonic-gate 	if (dip == NULL)
2654*0Sstevel@tonic-gate 		return (DDI_FAILURE);
2655*0Sstevel@tonic-gate 
2656*0Sstevel@tonic-gate 	LDI_EVENTCB((CE_NOTE, "%s: event_name=%s, "
2657*0Sstevel@tonic-gate 	    "dip=0x%p, event_cookiep=0x%p", "ldi_get_eventcookie",
2658*0Sstevel@tonic-gate 	    name, (void *)dip, (void *)ecp));
2659*0Sstevel@tonic-gate 
2660*0Sstevel@tonic-gate 	res = ddi_get_eventcookie(dip, name, ecp);
2661*0Sstevel@tonic-gate 
2662*0Sstevel@tonic-gate 	ddi_release_devi(dip);
2663*0Sstevel@tonic-gate 	return (res);
2664*0Sstevel@tonic-gate }
2665*0Sstevel@tonic-gate 
2666*0Sstevel@tonic-gate int
2667*0Sstevel@tonic-gate ldi_add_event_handler(ldi_handle_t lh, ddi_eventcookie_t ec,
2668*0Sstevel@tonic-gate     void (*handler)(ldi_handle_t, ddi_eventcookie_t, void *, void *),
2669*0Sstevel@tonic-gate     void *arg, ldi_callback_id_t *id)
2670*0Sstevel@tonic-gate {
2671*0Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
2672*0Sstevel@tonic-gate 	struct ldi_event	*lep;
2673*0Sstevel@tonic-gate 	dev_info_t		*dip;
2674*0Sstevel@tonic-gate 	dev_t			dev;
2675*0Sstevel@tonic-gate 	int			res;
2676*0Sstevel@tonic-gate 	struct snode		*csp;
2677*0Sstevel@tonic-gate 
2678*0Sstevel@tonic-gate 	if ((lh == NULL) || (ec == NULL) || (handler == NULL) || (id == NULL))
2679*0Sstevel@tonic-gate 		return (DDI_FAILURE);
2680*0Sstevel@tonic-gate 
2681*0Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
2682*0Sstevel@tonic-gate 
2683*0Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
2684*0Sstevel@tonic-gate 
2685*0Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
2686*0Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
2687*0Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
2688*0Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
2689*0Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
2690*0Sstevel@tonic-gate 	if (dip == NULL)
2691*0Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
2692*0Sstevel@tonic-gate 
2693*0Sstevel@tonic-gate 	if (dip == NULL)
2694*0Sstevel@tonic-gate 		return (DDI_FAILURE);
2695*0Sstevel@tonic-gate 
2696*0Sstevel@tonic-gate 	lep = kmem_zalloc(sizeof (struct ldi_event), KM_SLEEP);
2697*0Sstevel@tonic-gate 	lep->le_lhp = handlep;
2698*0Sstevel@tonic-gate 	lep->le_arg = arg;
2699*0Sstevel@tonic-gate 	lep->le_handler = handler;
2700*0Sstevel@tonic-gate 
2701*0Sstevel@tonic-gate 	if ((res = ddi_add_event_handler(dip, ec, i_ldi_callback,
2702*0Sstevel@tonic-gate 	    (void *)lep, &lep->le_id)) != DDI_SUCCESS) {
2703*0Sstevel@tonic-gate 		LDI_EVENTCB((CE_WARN, "%s: unable to add"
2704*0Sstevel@tonic-gate 		    "event callback", "ldi_add_event_handler"));
2705*0Sstevel@tonic-gate 		ddi_release_devi(dip);
2706*0Sstevel@tonic-gate 		kmem_free(lep, sizeof (struct ldi_event));
2707*0Sstevel@tonic-gate 		return (res);
2708*0Sstevel@tonic-gate 	}
2709*0Sstevel@tonic-gate 
2710*0Sstevel@tonic-gate 	*id = (ldi_callback_id_t)lep;
2711*0Sstevel@tonic-gate 
2712*0Sstevel@tonic-gate 	LDI_EVENTCB((CE_NOTE, "%s: dip=0x%p, event=0x%p, "
2713*0Sstevel@tonic-gate 	    "ldi_eventp=0x%p, cb_id=0x%p", "ldi_add_event_handler",
2714*0Sstevel@tonic-gate 	    (void *)dip, (void *)ec, (void *)lep, (void *)id));
2715*0Sstevel@tonic-gate 
2716*0Sstevel@tonic-gate 	handle_event_add(lep);
2717*0Sstevel@tonic-gate 	ddi_release_devi(dip);
2718*0Sstevel@tonic-gate 	return (res);
2719*0Sstevel@tonic-gate }
2720*0Sstevel@tonic-gate 
2721*0Sstevel@tonic-gate int
2722*0Sstevel@tonic-gate ldi_remove_event_handler(ldi_handle_t lh, ldi_callback_id_t id)
2723*0Sstevel@tonic-gate {
2724*0Sstevel@tonic-gate 	ldi_event_t		*lep = (ldi_event_t *)id;
2725*0Sstevel@tonic-gate 	int			res;
2726*0Sstevel@tonic-gate 
2727*0Sstevel@tonic-gate 	if ((lh == NULL) || (id == NULL))
2728*0Sstevel@tonic-gate 		return (DDI_FAILURE);
2729*0Sstevel@tonic-gate 
2730*0Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
2731*0Sstevel@tonic-gate 
2732*0Sstevel@tonic-gate 	if ((res = ddi_remove_event_handler(lep->le_id))
2733*0Sstevel@tonic-gate 	    != DDI_SUCCESS) {
2734*0Sstevel@tonic-gate 		LDI_EVENTCB((CE_WARN, "%s: unable to remove "
2735*0Sstevel@tonic-gate 		    "event callback", "ldi_remove_event_handler"));
2736*0Sstevel@tonic-gate 		return (res);
2737*0Sstevel@tonic-gate 	}
2738*0Sstevel@tonic-gate 
2739*0Sstevel@tonic-gate 	handle_event_remove(lep);
2740*0Sstevel@tonic-gate 	kmem_free(lep, sizeof (struct ldi_event));
2741*0Sstevel@tonic-gate 	return (res);
2742*0Sstevel@tonic-gate }
2743