xref: /onnv-gate/usr/src/uts/common/os/driver_lyr.c (revision 538:1636110b23c7)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*538Sedp  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Layered driver support.
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <sys/atomic.h>
340Sstevel@tonic-gate #include <sys/types.h>
350Sstevel@tonic-gate #include <sys/t_lock.h>
360Sstevel@tonic-gate #include <sys/param.h>
370Sstevel@tonic-gate #include <sys/conf.h>
380Sstevel@tonic-gate #include <sys/systm.h>
390Sstevel@tonic-gate #include <sys/sysmacros.h>
400Sstevel@tonic-gate #include <sys/buf.h>
410Sstevel@tonic-gate #include <sys/cred.h>
420Sstevel@tonic-gate #include <sys/uio.h>
430Sstevel@tonic-gate #include <sys/vnode.h>
440Sstevel@tonic-gate #include <sys/fs/snode.h>
450Sstevel@tonic-gate #include <sys/open.h>
460Sstevel@tonic-gate #include <sys/kmem.h>
470Sstevel@tonic-gate #include <sys/file.h>
480Sstevel@tonic-gate #include <sys/bootconf.h>
490Sstevel@tonic-gate #include <sys/pathname.h>
500Sstevel@tonic-gate #include <sys/bitmap.h>
510Sstevel@tonic-gate #include <sys/stat.h>
520Sstevel@tonic-gate #include <sys/dditypes.h>
530Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
540Sstevel@tonic-gate #include <sys/ddi.h>
550Sstevel@tonic-gate #include <sys/sunddi.h>
560Sstevel@tonic-gate #include <sys/sunndi.h>
570Sstevel@tonic-gate #include <sys/esunddi.h>
580Sstevel@tonic-gate #include <sys/autoconf.h>
590Sstevel@tonic-gate #include <sys/sunldi.h>
600Sstevel@tonic-gate #include <sys/sunldi_impl.h>
610Sstevel@tonic-gate #include <sys/errno.h>
620Sstevel@tonic-gate #include <sys/debug.h>
630Sstevel@tonic-gate #include <sys/modctl.h>
640Sstevel@tonic-gate #include <sys/var.h>
650Sstevel@tonic-gate #include <vm/seg_vn.h>
660Sstevel@tonic-gate 
670Sstevel@tonic-gate #include <sys/stropts.h>
680Sstevel@tonic-gate #include <sys/strsubr.h>
690Sstevel@tonic-gate #include <sys/socket.h>
700Sstevel@tonic-gate #include <sys/socketvar.h>
710Sstevel@tonic-gate #include <sys/kstr.h>
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate  * Define macros to manipulate snode, vnode, and open device flags
760Sstevel@tonic-gate  */
770Sstevel@tonic-gate #define	VTYP_VALID(i)	(((i) == VCHR) || ((i) == VBLK))
780Sstevel@tonic-gate #define	VTYP_TO_OTYP(i)	(((i) == VCHR) ? OTYP_CHR : OTYP_BLK)
790Sstevel@tonic-gate #define	VTYP_TO_STYP(i)	(((i) == VCHR) ? S_IFCHR : S_IFBLK)
800Sstevel@tonic-gate 
810Sstevel@tonic-gate #define	OTYP_VALID(i)	(((i) == OTYP_CHR) || ((i) == OTYP_BLK))
820Sstevel@tonic-gate #define	OTYP_TO_VTYP(i)	(((i) == OTYP_CHR) ? VCHR : VBLK)
830Sstevel@tonic-gate #define	OTYP_TO_STYP(i)	(((i) == OTYP_CHR) ? S_IFCHR : S_IFBLK)
840Sstevel@tonic-gate 
850Sstevel@tonic-gate #define	STYP_VALID(i)	(((i) == S_IFCHR) || ((i) == S_IFBLK))
860Sstevel@tonic-gate #define	STYP_TO_VTYP(i)	(((i) == S_IFCHR) ? VCHR : VBLK)
870Sstevel@tonic-gate 
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate  * Define macros for accessing layered driver hash structures
900Sstevel@tonic-gate  */
910Sstevel@tonic-gate #define	LH_HASH(vp)		(handle_hash_func(vp) % LH_HASH_SZ)
920Sstevel@tonic-gate #define	LI_HASH(mid, dip, dev)	(ident_hash_func(mid, dip, dev) % LI_HASH_SZ)
930Sstevel@tonic-gate 
940Sstevel@tonic-gate /*
950Sstevel@tonic-gate  * Define layered handle flags used in the lh_type field
960Sstevel@tonic-gate  */
970Sstevel@tonic-gate #define	LH_STREAM	(0x1)	/* handle to a streams device */
980Sstevel@tonic-gate #define	LH_CBDEV	(0x2)	/* handle to a char/block device */
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate /*
1010Sstevel@tonic-gate  * Define marco for devid property lookups
1020Sstevel@tonic-gate  */
1030Sstevel@tonic-gate #define	DEVID_PROP_FLAGS	(DDI_PROP_DONTPASS | \
1040Sstevel@tonic-gate 				DDI_PROP_TYPE_STRING|DDI_PROP_CANSLEEP)
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate /*
1080Sstevel@tonic-gate  * globals
1090Sstevel@tonic-gate  */
1100Sstevel@tonic-gate static kmutex_t			ldi_ident_hash_lock[LI_HASH_SZ];
1110Sstevel@tonic-gate static struct ldi_ident		*ldi_ident_hash[LI_HASH_SZ];
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate static kmutex_t			ldi_handle_hash_lock[LH_HASH_SZ];
1140Sstevel@tonic-gate static struct ldi_handle	*ldi_handle_hash[LH_HASH_SZ];
1150Sstevel@tonic-gate static size_t			ldi_handle_hash_count;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate void
1180Sstevel@tonic-gate ldi_init(void)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate 	int i;
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	ldi_handle_hash_count = 0;
1230Sstevel@tonic-gate 	for (i = 0; i < LH_HASH_SZ; i++) {
1240Sstevel@tonic-gate 		mutex_init(&ldi_handle_hash_lock[i], NULL, MUTEX_DEFAULT, NULL);
1250Sstevel@tonic-gate 		ldi_handle_hash[i] = NULL;
1260Sstevel@tonic-gate 	}
1270Sstevel@tonic-gate 	for (i = 0; i < LI_HASH_SZ; i++) {
1280Sstevel@tonic-gate 		mutex_init(&ldi_ident_hash_lock[i], NULL, MUTEX_DEFAULT, NULL);
1290Sstevel@tonic-gate 		ldi_ident_hash[i] = NULL;
1300Sstevel@tonic-gate 	}
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate /*
1340Sstevel@tonic-gate  * LDI ident manipulation functions
1350Sstevel@tonic-gate  */
1360Sstevel@tonic-gate static uint_t
1370Sstevel@tonic-gate ident_hash_func(modid_t modid, dev_info_t *dip, dev_t dev)
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate 	if (dip != NULL) {
1400Sstevel@tonic-gate 		uintptr_t k = (uintptr_t)dip;
1410Sstevel@tonic-gate 		k >>= (int)highbit(sizeof (struct dev_info));
1420Sstevel@tonic-gate 		return ((uint_t)k);
1430Sstevel@tonic-gate 	} else if (dev != DDI_DEV_T_NONE) {
1440Sstevel@tonic-gate 		return (modid + getminor(dev) + getmajor(dev));
1450Sstevel@tonic-gate 	} else {
1460Sstevel@tonic-gate 		return (modid);
1470Sstevel@tonic-gate 	}
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate static struct ldi_ident **
1510Sstevel@tonic-gate ident_find_ref_nolock(modid_t modid, dev_info_t *dip, dev_t dev, major_t major)
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate 	struct ldi_ident	**lipp = NULL;
1540Sstevel@tonic-gate 	uint_t			index = LI_HASH(modid, dip, dev);
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ldi_ident_hash_lock[index]));
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	for (lipp = &(ldi_ident_hash[index]);
1590Sstevel@tonic-gate 	    (*lipp != NULL);
1600Sstevel@tonic-gate 	    lipp = &((*lipp)->li_next)) {
1610Sstevel@tonic-gate 		if (((*lipp)->li_modid == modid) &&
1620Sstevel@tonic-gate 		    ((*lipp)->li_major == major) &&
1630Sstevel@tonic-gate 		    ((*lipp)->li_dip == dip) &&
1640Sstevel@tonic-gate 		    ((*lipp)->li_dev == dev))
1650Sstevel@tonic-gate 			break;
1660Sstevel@tonic-gate 	}
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	ASSERT(lipp != NULL);
1690Sstevel@tonic-gate 	return (lipp);
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate static struct ldi_ident *
1730Sstevel@tonic-gate ident_alloc(char *mod_name, dev_info_t *dip, dev_t dev, major_t major)
1740Sstevel@tonic-gate {
1750Sstevel@tonic-gate 	struct ldi_ident	*lip, **lipp;
1760Sstevel@tonic-gate 	modid_t			modid;
1770Sstevel@tonic-gate 	uint_t			index;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	ASSERT(mod_name != NULL);
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	/* get the module id */
1820Sstevel@tonic-gate 	modid = mod_name_to_modid(mod_name);
1830Sstevel@tonic-gate 	ASSERT(modid != -1);
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	/* allocate a new ident in case we need it */
1860Sstevel@tonic-gate 	lip = kmem_zalloc(sizeof (*lip), KM_SLEEP);
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	/* search the hash for a matching ident */
1890Sstevel@tonic-gate 	index = LI_HASH(modid, dip, dev);
1900Sstevel@tonic-gate 	mutex_enter(&ldi_ident_hash_lock[index]);
1910Sstevel@tonic-gate 	lipp = ident_find_ref_nolock(modid, dip, dev, major);
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	if (*lipp != NULL) {
1940Sstevel@tonic-gate 		/* we found an indent in the hash */
1950Sstevel@tonic-gate 		ASSERT(strcmp((*lipp)->li_modname, mod_name) == 0);
1960Sstevel@tonic-gate 		(*lipp)->li_ref++;
1970Sstevel@tonic-gate 		mutex_exit(&ldi_ident_hash_lock[index]);
1980Sstevel@tonic-gate 		kmem_free(lip, sizeof (struct ldi_ident));
1990Sstevel@tonic-gate 		return (*lipp);
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	/* initialize the new ident */
2030Sstevel@tonic-gate 	lip->li_next = NULL;
2040Sstevel@tonic-gate 	lip->li_ref = 1;
2050Sstevel@tonic-gate 	lip->li_modid = modid;
2060Sstevel@tonic-gate 	lip->li_major = major;
2070Sstevel@tonic-gate 	lip->li_dip = dip;
2080Sstevel@tonic-gate 	lip->li_dev = dev;
2090Sstevel@tonic-gate 	(void) strncpy(lip->li_modname, mod_name, sizeof (lip->li_modname) - 1);
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	/* add it to the ident hash */
2120Sstevel@tonic-gate 	lip->li_next = ldi_ident_hash[index];
2130Sstevel@tonic-gate 	ldi_ident_hash[index] = lip;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	mutex_exit(&ldi_ident_hash_lock[index]);
2160Sstevel@tonic-gate 	return (lip);
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate static void
2200Sstevel@tonic-gate ident_hold(struct ldi_ident *lip)
2210Sstevel@tonic-gate {
2220Sstevel@tonic-gate 	uint_t			index;
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	ASSERT(lip != NULL);
2250Sstevel@tonic-gate 	index = LI_HASH(lip->li_modid, lip->li_dip, lip->li_dev);
2260Sstevel@tonic-gate 	mutex_enter(&ldi_ident_hash_lock[index]);
2270Sstevel@tonic-gate 	ASSERT(lip->li_ref > 0);
2280Sstevel@tonic-gate 	lip->li_ref++;
2290Sstevel@tonic-gate 	mutex_exit(&ldi_ident_hash_lock[index]);
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate static void
2330Sstevel@tonic-gate ident_release(struct ldi_ident *lip)
2340Sstevel@tonic-gate {
2350Sstevel@tonic-gate 	struct ldi_ident	**lipp;
2360Sstevel@tonic-gate 	uint_t			index;
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	ASSERT(lip != NULL);
2390Sstevel@tonic-gate 	index = LI_HASH(lip->li_modid, lip->li_dip, lip->li_dev);
2400Sstevel@tonic-gate 	mutex_enter(&ldi_ident_hash_lock[index]);
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	ASSERT(lip->li_ref > 0);
2430Sstevel@tonic-gate 	if (--lip->li_ref > 0) {
2440Sstevel@tonic-gate 		/* there are more references to this ident */
2450Sstevel@tonic-gate 		mutex_exit(&ldi_ident_hash_lock[index]);
2460Sstevel@tonic-gate 		return;
2470Sstevel@tonic-gate 	}
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	/* this was the last reference/open for this ident.  free it. */
2500Sstevel@tonic-gate 	lipp = ident_find_ref_nolock(
2510Sstevel@tonic-gate 	    lip->li_modid, lip->li_dip, lip->li_dev, lip->li_major);
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	ASSERT((lipp != NULL) && (*lipp != NULL));
2540Sstevel@tonic-gate 	*lipp = lip->li_next;
2550Sstevel@tonic-gate 	mutex_exit(&ldi_ident_hash_lock[index]);
2560Sstevel@tonic-gate 	kmem_free(lip, sizeof (struct ldi_ident));
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate /*
2600Sstevel@tonic-gate  * LDI handle manipulation functions
2610Sstevel@tonic-gate  */
2620Sstevel@tonic-gate static uint_t
2630Sstevel@tonic-gate handle_hash_func(void *vp)
2640Sstevel@tonic-gate {
2650Sstevel@tonic-gate 	uintptr_t k = (uintptr_t)vp;
2660Sstevel@tonic-gate 	k >>= (int)highbit(sizeof (vnode_t));
2670Sstevel@tonic-gate 	return ((uint_t)k);
2680Sstevel@tonic-gate }
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate static struct ldi_handle **
2710Sstevel@tonic-gate handle_find_ref_nolock(vnode_t *vp, struct ldi_ident *ident)
2720Sstevel@tonic-gate {
2730Sstevel@tonic-gate 	struct ldi_handle	**lhpp = NULL;
2740Sstevel@tonic-gate 	uint_t			index = LH_HASH(vp);
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ldi_handle_hash_lock[index]));
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	for (lhpp = &(ldi_handle_hash[index]);
2790Sstevel@tonic-gate 	    (*lhpp != NULL);
2800Sstevel@tonic-gate 	    lhpp = &((*lhpp)->lh_next)) {
2810Sstevel@tonic-gate 		if (((*lhpp)->lh_ident == ident) &&
2820Sstevel@tonic-gate 		    ((*lhpp)->lh_vp == vp))
2830Sstevel@tonic-gate 			break;
2840Sstevel@tonic-gate 	}
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	ASSERT(lhpp != NULL);
2870Sstevel@tonic-gate 	return (lhpp);
2880Sstevel@tonic-gate }
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate static struct ldi_handle *
2910Sstevel@tonic-gate handle_find(vnode_t *vp, struct ldi_ident *ident)
2920Sstevel@tonic-gate {
2930Sstevel@tonic-gate 	struct ldi_handle	**lhpp;
2940Sstevel@tonic-gate 	int			index = LH_HASH(vp);
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	mutex_enter(&ldi_handle_hash_lock[index]);
2970Sstevel@tonic-gate 	lhpp = handle_find_ref_nolock(vp, ident);
2980Sstevel@tonic-gate 	mutex_exit(&ldi_handle_hash_lock[index]);
2990Sstevel@tonic-gate 	ASSERT(lhpp != NULL);
3000Sstevel@tonic-gate 	return (*lhpp);
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate static struct ldi_handle *
3040Sstevel@tonic-gate handle_alloc(vnode_t *vp, struct ldi_ident *ident)
3050Sstevel@tonic-gate {
3060Sstevel@tonic-gate 	struct ldi_handle	*lhp, **lhpp;
3070Sstevel@tonic-gate 	uint_t			index;
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	ASSERT((vp != NULL) && (ident != NULL));
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	/* allocate a new handle in case we need it */
3120Sstevel@tonic-gate 	lhp = kmem_zalloc(sizeof (*lhp), KM_SLEEP);
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	/* search the hash for a matching handle */
3150Sstevel@tonic-gate 	index = LH_HASH(vp);
3160Sstevel@tonic-gate 	mutex_enter(&ldi_handle_hash_lock[index]);
3170Sstevel@tonic-gate 	lhpp = handle_find_ref_nolock(vp, ident);
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	if (*lhpp != NULL) {
3200Sstevel@tonic-gate 		/* we found a handle in the hash */
3210Sstevel@tonic-gate 		(*lhpp)->lh_ref++;
3220Sstevel@tonic-gate 		mutex_exit(&ldi_handle_hash_lock[index]);
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 		LDI_ALLOCFREE((CE_WARN, "ldi handle alloc: dup "
3250Sstevel@tonic-gate 			"lh=0x%p, ident=0x%p, vp=0x%p, drv=%s, minor=0x%x",
3260Sstevel@tonic-gate 			(void *)*lhpp, (void *)ident, (void *)vp,
3270Sstevel@tonic-gate 			mod_major_to_name(getmajor(vp->v_rdev)),
3280Sstevel@tonic-gate 			getminor(vp->v_rdev)));
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 		kmem_free(lhp, sizeof (struct ldi_handle));
3310Sstevel@tonic-gate 		return (*lhpp);
3320Sstevel@tonic-gate 	}
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 	/* initialize the new handle */
3350Sstevel@tonic-gate 	lhp->lh_ref = 1;
3360Sstevel@tonic-gate 	lhp->lh_vp = vp;
3370Sstevel@tonic-gate 	lhp->lh_ident = ident;
3380Sstevel@tonic-gate 	mutex_init(lhp->lh_lock, NULL, MUTEX_DEFAULT, NULL);
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	/* set the device type for this handle */
3410Sstevel@tonic-gate 	lhp->lh_type = 0;
3420Sstevel@tonic-gate 	if (STREAMSTAB(getmajor(vp->v_rdev))) {
3430Sstevel@tonic-gate 		ASSERT(vp->v_type == VCHR);
3440Sstevel@tonic-gate 		lhp->lh_type |= LH_STREAM;
3450Sstevel@tonic-gate 	} else {
3460Sstevel@tonic-gate 		lhp->lh_type |= LH_CBDEV;
3470Sstevel@tonic-gate 	}
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	/* get holds on other objects */
3500Sstevel@tonic-gate 	ident_hold(ident);
3510Sstevel@tonic-gate 	ASSERT(vp->v_count >= 1);
3520Sstevel@tonic-gate 	VN_HOLD(vp);
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	/* add it to the handle hash */
3550Sstevel@tonic-gate 	lhp->lh_next = ldi_handle_hash[index];
3560Sstevel@tonic-gate 	ldi_handle_hash[index] = lhp;
3570Sstevel@tonic-gate 	atomic_add_long(&ldi_handle_hash_count, 1);
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	LDI_ALLOCFREE((CE_WARN, "ldi handle alloc: new "
3600Sstevel@tonic-gate 		"lh=0x%p, ident=0x%p, vp=0x%p, drv=%s, minor=0x%x",
3610Sstevel@tonic-gate 		(void *)lhp, (void *)ident, (void *)vp,
3620Sstevel@tonic-gate 		mod_major_to_name(getmajor(vp->v_rdev)),
3630Sstevel@tonic-gate 		getminor(vp->v_rdev)));
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	mutex_exit(&ldi_handle_hash_lock[index]);
3660Sstevel@tonic-gate 	return (lhp);
3670Sstevel@tonic-gate }
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate static void
3700Sstevel@tonic-gate handle_release(struct ldi_handle *lhp)
3710Sstevel@tonic-gate {
3720Sstevel@tonic-gate 	struct ldi_handle	**lhpp;
3730Sstevel@tonic-gate 	uint_t			index;
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	ASSERT(lhp != NULL);
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	index = LH_HASH(lhp->lh_vp);
3780Sstevel@tonic-gate 	mutex_enter(&ldi_handle_hash_lock[index]);
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	LDI_ALLOCFREE((CE_WARN, "ldi handle release: "
3810Sstevel@tonic-gate 		"lh=0x%p, ident=0x%p, vp=0x%p, drv=%s, minor=0x%x",
3820Sstevel@tonic-gate 		(void *)lhp, (void *)lhp->lh_ident, (void *)lhp->lh_vp,
3830Sstevel@tonic-gate 		mod_major_to_name(getmajor(lhp->lh_vp->v_rdev)),
3840Sstevel@tonic-gate 		getminor(lhp->lh_vp->v_rdev)));
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 	ASSERT(lhp->lh_ref > 0);
3870Sstevel@tonic-gate 	if (--lhp->lh_ref > 0) {
3880Sstevel@tonic-gate 		/* there are more references to this handle */
3890Sstevel@tonic-gate 		mutex_exit(&ldi_handle_hash_lock[index]);
3900Sstevel@tonic-gate 		return;
3910Sstevel@tonic-gate 	}
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	/* this was the last reference/open for this handle.  free it. */
3940Sstevel@tonic-gate 	lhpp = handle_find_ref_nolock(lhp->lh_vp, lhp->lh_ident);
3950Sstevel@tonic-gate 	ASSERT((lhpp != NULL) && (*lhpp != NULL));
3960Sstevel@tonic-gate 	*lhpp = lhp->lh_next;
3970Sstevel@tonic-gate 	atomic_add_long(&ldi_handle_hash_count, -1);
3980Sstevel@tonic-gate 	mutex_exit(&ldi_handle_hash_lock[index]);
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	VN_RELE(lhp->lh_vp);
4010Sstevel@tonic-gate 	ident_release(lhp->lh_ident);
4020Sstevel@tonic-gate 	mutex_destroy(lhp->lh_lock);
4030Sstevel@tonic-gate 	kmem_free(lhp, sizeof (struct ldi_handle));
4040Sstevel@tonic-gate }
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate /*
4070Sstevel@tonic-gate  * LDI event manipulation functions
4080Sstevel@tonic-gate  */
4090Sstevel@tonic-gate static void
4100Sstevel@tonic-gate handle_event_add(ldi_event_t *lep)
4110Sstevel@tonic-gate {
4120Sstevel@tonic-gate 	struct ldi_handle *lhp = lep->le_lhp;
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	ASSERT(lhp != NULL);
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	mutex_enter(lhp->lh_lock);
4170Sstevel@tonic-gate 	if (lhp->lh_events == NULL) {
4180Sstevel@tonic-gate 		lhp->lh_events = lep;
4190Sstevel@tonic-gate 		mutex_exit(lhp->lh_lock);
4200Sstevel@tonic-gate 		return;
4210Sstevel@tonic-gate 	}
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	lep->le_next = lhp->lh_events;
4240Sstevel@tonic-gate 	lhp->lh_events->le_prev = lep;
4250Sstevel@tonic-gate 	lhp->lh_events = lep;
4260Sstevel@tonic-gate 	mutex_exit(lhp->lh_lock);
4270Sstevel@tonic-gate }
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate static void
4300Sstevel@tonic-gate handle_event_remove(ldi_event_t *lep)
4310Sstevel@tonic-gate {
4320Sstevel@tonic-gate 	struct ldi_handle *lhp = lep->le_lhp;
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	ASSERT(lhp != NULL);
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	mutex_enter(lhp->lh_lock);
4370Sstevel@tonic-gate 	if (lep->le_prev)
4380Sstevel@tonic-gate 		lep->le_prev->le_next = lep->le_next;
4390Sstevel@tonic-gate 	if (lep->le_next)
4400Sstevel@tonic-gate 		lep->le_next->le_prev = lep->le_prev;
4410Sstevel@tonic-gate 	if (lhp->lh_events == lep)
4420Sstevel@tonic-gate 		lhp->lh_events = lep->le_next;
4430Sstevel@tonic-gate 	mutex_exit(lhp->lh_lock);
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate }
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate static void
4480Sstevel@tonic-gate i_ldi_callback(dev_info_t *dip, ddi_eventcookie_t event_cookie,
4490Sstevel@tonic-gate     void *arg, void *bus_impldata)
4500Sstevel@tonic-gate {
4510Sstevel@tonic-gate 	ldi_event_t *lep = (ldi_event_t *)arg;
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	ASSERT(lep != NULL);
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	LDI_EVENTCB((CE_NOTE, "%s: dip=0x%p, "
4560Sstevel@tonic-gate 	    "event_cookie=0x%p, ldi_eventp=0x%p", "i_ldi_callback",
4570Sstevel@tonic-gate 	    (void *)dip, (void *)event_cookie, (void *)lep));
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	lep->le_handler(lep->le_lhp, event_cookie, lep->le_arg, bus_impldata);
4600Sstevel@tonic-gate }
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate /*
4630Sstevel@tonic-gate  * LDI open helper functions
4640Sstevel@tonic-gate  */
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate /* get a vnode to a device by dev_t and otyp */
4670Sstevel@tonic-gate static int
4680Sstevel@tonic-gate ldi_vp_from_dev(dev_t dev, int otyp, vnode_t **vpp)
4690Sstevel@tonic-gate {
4700Sstevel@tonic-gate 	dev_info_t		*dip;
4710Sstevel@tonic-gate 	vnode_t			*vp;
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	/* sanity check required input parameters */
4740Sstevel@tonic-gate 	if ((dev == DDI_DEV_T_NONE) || (!OTYP_VALID(otyp)) || (vpp == NULL))
4750Sstevel@tonic-gate 		return (EINVAL);
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	if ((dip = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
4780Sstevel@tonic-gate 		return (ENODEV);
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	if (STREAMSTAB(getmajor(dev)) && (otyp != OTYP_CHR)) {
4810Sstevel@tonic-gate 		ddi_release_devi(dip);  /* from e_ddi_hold_devi_by_dev */
4820Sstevel@tonic-gate 		return (ENXIO);
4830Sstevel@tonic-gate 	}
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	vp = makespecvp(dev, OTYP_TO_VTYP(otyp));
4860Sstevel@tonic-gate 	spec_assoc_vp_with_devi(vp, dip);
4870Sstevel@tonic-gate 	ddi_release_devi(dip);  /* from e_ddi_hold_devi_by_dev */
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	*vpp = vp;
4900Sstevel@tonic-gate 	return (0);
4910Sstevel@tonic-gate }
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate /* get a vnode to a device by pathname */
4940Sstevel@tonic-gate static int
4950Sstevel@tonic-gate ldi_vp_from_name(char *path, vnode_t **vpp)
4960Sstevel@tonic-gate {
4970Sstevel@tonic-gate 	vnode_t			*vp = NULL;
4980Sstevel@tonic-gate 	int			ret;
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	/* sanity check required input parameters */
5010Sstevel@tonic-gate 	if ((path == NULL) || (vpp == NULL))
5020Sstevel@tonic-gate 		return (EINVAL);
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 	if (modrootloaded) {
5050Sstevel@tonic-gate 		cred_t *saved_cred = curthread->t_cred;
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 		/* we don't want lookupname to fail because of credentials */
5080Sstevel@tonic-gate 		curthread->t_cred = kcred;
509*538Sedp 
510*538Sedp 		/*
511*538Sedp 		 * all lookups should be done in the global zone.  but
512*538Sedp 		 * lookupnameat() won't actually do this if an absolute
513*538Sedp 		 * path is passed in.  since the ldi interfaces require an
514*538Sedp 		 * absolute path we pass lookupnameat() a pointer to
515*538Sedp 		 * the character after the leading '/' and tell it to
516*538Sedp 		 * start searching at the current system root directory.
517*538Sedp 		 */
518*538Sedp 		ASSERT(*path == '/');
519*538Sedp 		ret = lookupnameat(path + 1, UIO_SYSSPACE, FOLLOW, NULLVPP,
520*538Sedp 		    &vp, rootdir);
521*538Sedp 
522*538Sedp 		/* restore this threads credentials */
5230Sstevel@tonic-gate 		curthread->t_cred = saved_cred;
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 		if (ret == 0) {
5260Sstevel@tonic-gate 			if (!vn_matchops(vp, spec_getvnodeops()) ||
5270Sstevel@tonic-gate 			    !VTYP_VALID(vp->v_type)) {
5280Sstevel@tonic-gate 				VN_RELE(vp);
5290Sstevel@tonic-gate 				return (ENXIO);
5300Sstevel@tonic-gate 			}
5310Sstevel@tonic-gate 		}
5320Sstevel@tonic-gate 	}
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	if (vp == NULL) {
5350Sstevel@tonic-gate 		dev_info_t	*dip;
5360Sstevel@tonic-gate 		dev_t		dev;
5370Sstevel@tonic-gate 		int		spec_type;
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 		/*
5400Sstevel@tonic-gate 		 * Root is not mounted, the minor node is not specified,
5410Sstevel@tonic-gate 		 * or an OBP path has been specified.
5420Sstevel@tonic-gate 		 */
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 		/*
5450Sstevel@tonic-gate 		 * Determine if path can be pruned to produce an
5460Sstevel@tonic-gate 		 * OBP or devfs path for resolve_pathname.
5470Sstevel@tonic-gate 		 */
5480Sstevel@tonic-gate 		if (strncmp(path, "/devices/", 9) == 0)
5490Sstevel@tonic-gate 			path += strlen("/devices");
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 		/*
5520Sstevel@tonic-gate 		 * if no minor node was specified the DEFAULT minor node
5530Sstevel@tonic-gate 		 * will be returned.  if there is no DEFAULT minor node
5540Sstevel@tonic-gate 		 * one will be fabricated of type S_IFCHR with the minor
5550Sstevel@tonic-gate 		 * number equal to the instance number.
5560Sstevel@tonic-gate 		 */
5570Sstevel@tonic-gate 		ret = resolve_pathname(path, &dip, &dev, &spec_type);
5580Sstevel@tonic-gate 		if (ret != 0)
5590Sstevel@tonic-gate 			return (ENODEV);
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 		ASSERT(STYP_VALID(spec_type));
5620Sstevel@tonic-gate 		vp = makespecvp(dev, STYP_TO_VTYP(spec_type));
5630Sstevel@tonic-gate 		spec_assoc_vp_with_devi(vp, dip);
5640Sstevel@tonic-gate 		ddi_release_devi(dip);
5650Sstevel@tonic-gate 	}
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	*vpp = vp;
5680Sstevel@tonic-gate 	return (0);
5690Sstevel@tonic-gate }
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate static int
5720Sstevel@tonic-gate ldi_devid_match(ddi_devid_t devid, dev_info_t *dip, dev_t dev)
5730Sstevel@tonic-gate {
5740Sstevel@tonic-gate 	char		*devidstr;
5750Sstevel@tonic-gate 	ddi_prop_t	*propp;
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	/* convert devid as a string property */
5780Sstevel@tonic-gate 	if ((devidstr = ddi_devid_str_encode(devid, NULL)) == NULL)
5790Sstevel@tonic-gate 		return (0);
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	/*
5820Sstevel@tonic-gate 	 * Search for the devid.  For speed and ease in locking this
5830Sstevel@tonic-gate 	 * code directly uses the property implementation.  See
5840Sstevel@tonic-gate 	 * ddi_common_devid_to_devlist() for a comment as to why.
5850Sstevel@tonic-gate 	 */
5860Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	/* check if there is a DDI_DEV_T_NONE devid property */
5890Sstevel@tonic-gate 	propp = i_ddi_prop_search(DDI_DEV_T_NONE,
5900Sstevel@tonic-gate 	    DEVID_PROP_NAME, DEVID_PROP_FLAGS, &DEVI(dip)->devi_hw_prop_ptr);
5910Sstevel@tonic-gate 	if (propp != NULL) {
5920Sstevel@tonic-gate 		if (ddi_devid_str_compare(propp->prop_val, devidstr) == 0) {
5930Sstevel@tonic-gate 			/* a DDI_DEV_T_NONE devid exists and matchs */
5940Sstevel@tonic-gate 			mutex_exit(&(DEVI(dip)->devi_lock));
5950Sstevel@tonic-gate 			ddi_devid_str_free(devidstr);
5960Sstevel@tonic-gate 			return (1);
5970Sstevel@tonic-gate 		} else {
5980Sstevel@tonic-gate 			/* a DDI_DEV_T_NONE devid exists and doesn't match */
5990Sstevel@tonic-gate 			mutex_exit(&(DEVI(dip)->devi_lock));
6000Sstevel@tonic-gate 			ddi_devid_str_free(devidstr);
6010Sstevel@tonic-gate 			return (0);
6020Sstevel@tonic-gate 		}
6030Sstevel@tonic-gate 	}
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	/* check if there is a devt specific devid property */
6060Sstevel@tonic-gate 	propp = i_ddi_prop_search(dev,
6070Sstevel@tonic-gate 	    DEVID_PROP_NAME, DEVID_PROP_FLAGS, &(DEVI(dip)->devi_hw_prop_ptr));
6080Sstevel@tonic-gate 	if (propp != NULL) {
6090Sstevel@tonic-gate 		if (ddi_devid_str_compare(propp->prop_val, devidstr) == 0) {
6100Sstevel@tonic-gate 			/* a devt specific devid exists and matchs */
6110Sstevel@tonic-gate 			mutex_exit(&(DEVI(dip)->devi_lock));
6120Sstevel@tonic-gate 			ddi_devid_str_free(devidstr);
6130Sstevel@tonic-gate 			return (1);
6140Sstevel@tonic-gate 		} else {
6150Sstevel@tonic-gate 			/* a devt specific devid exists and doesn't match */
6160Sstevel@tonic-gate 			mutex_exit(&(DEVI(dip)->devi_lock));
6170Sstevel@tonic-gate 			ddi_devid_str_free(devidstr);
6180Sstevel@tonic-gate 			return (0);
6190Sstevel@tonic-gate 		}
6200Sstevel@tonic-gate 	}
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	/* we didn't find any devids associated with the device */
6230Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
6240Sstevel@tonic-gate 	ddi_devid_str_free(devidstr);
6250Sstevel@tonic-gate 	return (0);
6260Sstevel@tonic-gate }
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate /* get a handle to a device by devid and minor name */
6290Sstevel@tonic-gate static int
6300Sstevel@tonic-gate ldi_vp_from_devid(ddi_devid_t devid, char *minor_name, vnode_t **vpp)
6310Sstevel@tonic-gate {
6320Sstevel@tonic-gate 	dev_info_t		*dip;
6330Sstevel@tonic-gate 	vnode_t			*vp;
6340Sstevel@tonic-gate 	int			ret, i, ndevs, styp;
6350Sstevel@tonic-gate 	dev_t			dev, *devs;
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	/* sanity check required input parameters */
6380Sstevel@tonic-gate 	if ((devid == NULL) || (minor_name == NULL) || (vpp == NULL))
6390Sstevel@tonic-gate 		return (EINVAL);
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	ret = ddi_lyr_devid_to_devlist(devid, minor_name, &ndevs, &devs);
6420Sstevel@tonic-gate 	if ((ret != DDI_SUCCESS) || (ndevs <= 0))
6430Sstevel@tonic-gate 		return (ENODEV);
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	for (i = 0; i < ndevs; i++) {
6460Sstevel@tonic-gate 		dev = devs[i];
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 		if ((dip = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
6490Sstevel@tonic-gate 			continue;
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 		/*
6520Sstevel@tonic-gate 		 * now we have to verify that the devid of the disk
6530Sstevel@tonic-gate 		 * still matches what was requested.
6540Sstevel@tonic-gate 		 *
6550Sstevel@tonic-gate 		 * we have to do this because the devid could have
6560Sstevel@tonic-gate 		 * changed between the call to ddi_lyr_devid_to_devlist()
6570Sstevel@tonic-gate 		 * and e_ddi_hold_devi_by_dev().  this is because when
6580Sstevel@tonic-gate 		 * ddi_lyr_devid_to_devlist() returns a list of devts
6590Sstevel@tonic-gate 		 * there is no kind of hold on those devts so a device
6600Sstevel@tonic-gate 		 * could have been replaced out from under us in the
6610Sstevel@tonic-gate 		 * interim.
6620Sstevel@tonic-gate 		 */
6630Sstevel@tonic-gate 		if ((i_ddi_minorname_to_devtspectype(dip, minor_name,
6640Sstevel@tonic-gate 		    NULL, &styp) == DDI_SUCCESS) &&
6650Sstevel@tonic-gate 		    ldi_devid_match(devid, dip, dev))
6660Sstevel@tonic-gate 			break;
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 		ddi_release_devi(dip);	/* from e_ddi_hold_devi_by_dev() */
6690Sstevel@tonic-gate 	}
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	ddi_lyr_free_devlist(devs, ndevs);
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	if (i == ndevs)
6740Sstevel@tonic-gate 		return (ENODEV);
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	ASSERT(STYP_VALID(styp));
6770Sstevel@tonic-gate 	vp = makespecvp(dev, STYP_TO_VTYP(styp));
6780Sstevel@tonic-gate 	spec_assoc_vp_with_devi(vp, dip);
6790Sstevel@tonic-gate 	ddi_release_devi(dip);		/* from e_ddi_hold_devi_by_dev */
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	*vpp = vp;
6820Sstevel@tonic-gate 	return (0);
6830Sstevel@tonic-gate }
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate /* given a vnode, open a device */
6860Sstevel@tonic-gate static int
6870Sstevel@tonic-gate ldi_open_by_vp(vnode_t **vpp, int flag, cred_t *cr,
6880Sstevel@tonic-gate     ldi_handle_t *lhp, struct ldi_ident *li)
6890Sstevel@tonic-gate {
6900Sstevel@tonic-gate 	struct ldi_handle	*nlhp;
6910Sstevel@tonic-gate 	vnode_t			*vp;
6920Sstevel@tonic-gate 	int			err;
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 	ASSERT((vpp != NULL) && (*vpp != NULL));
6950Sstevel@tonic-gate 	ASSERT((lhp != NULL) && (li != NULL));
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 	vp = *vpp;
6980Sstevel@tonic-gate 	/* if the vnode passed in is not a device, then bail */
6990Sstevel@tonic-gate 	if (!vn_matchops(vp, spec_getvnodeops()) || !VTYP_VALID(vp->v_type))
7000Sstevel@tonic-gate 		return (ENXIO);
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	/*
7030Sstevel@tonic-gate 	 * the caller may have specified a node that
7040Sstevel@tonic-gate 	 * doesn't have cb_ops defined.  the ldi doesn't yet
7050Sstevel@tonic-gate 	 * support opening devices without a valid cb_ops.
7060Sstevel@tonic-gate 	 */
7070Sstevel@tonic-gate 	if (devopsp[getmajor(vp->v_rdev)]->devo_cb_ops == NULL)
7080Sstevel@tonic-gate 		return (ENXIO);
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	/* open the device */
7110Sstevel@tonic-gate 	if ((err = VOP_OPEN(&vp, flag | FKLYR, cr)) != 0)
7120Sstevel@tonic-gate 		return (err);
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	/* possible clone open, make sure that we still have a spec node */
7150Sstevel@tonic-gate 	ASSERT(vn_matchops(vp, spec_getvnodeops()));
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 	nlhp = handle_alloc(vp, li);
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	if (vp != *vpp) {
7200Sstevel@tonic-gate 		/*
7210Sstevel@tonic-gate 		 * allocating the layered handle took a new hold on the vnode
7220Sstevel@tonic-gate 		 * so we can release the hold that was returned by the clone
7230Sstevel@tonic-gate 		 * open
7240Sstevel@tonic-gate 		 */
7250Sstevel@tonic-gate 		LDI_OPENCLOSE((CE_WARN, "%s: lh=0x%p",
7260Sstevel@tonic-gate 			"ldi clone open", (void *)nlhp));
7270Sstevel@tonic-gate 	} else {
7280Sstevel@tonic-gate 		LDI_OPENCLOSE((CE_WARN, "%s: lh=0x%p",
7290Sstevel@tonic-gate 			"ldi open", (void *)nlhp));
7300Sstevel@tonic-gate 	}
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	/* Flush back any dirty pages associated with the device. */
7330Sstevel@tonic-gate 	if (nlhp->lh_type & LH_CBDEV) {
7340Sstevel@tonic-gate 		vnode_t	*cvp = common_specvp(nlhp->lh_vp);
7350Sstevel@tonic-gate 		dev_t	dev = cvp->v_rdev;
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 		(void) VOP_PUTPAGE(cvp, 0, 0, B_INVAL, kcred);
7380Sstevel@tonic-gate 		bflush(dev);
7390Sstevel@tonic-gate 	}
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 	*vpp = vp;
7420Sstevel@tonic-gate 	*lhp = (ldi_handle_t)nlhp;
7430Sstevel@tonic-gate 	return (0);
7440Sstevel@tonic-gate }
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate /* Call a drivers prop_op(9E) interface */
7470Sstevel@tonic-gate static int
7480Sstevel@tonic-gate i_ldi_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
7490Sstevel@tonic-gate     int flags, char *name, caddr_t valuep, int *lengthp)
7500Sstevel@tonic-gate {
7510Sstevel@tonic-gate 	struct dev_ops	*ops = NULL;
7520Sstevel@tonic-gate 	int		res;
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	ASSERT((dip != NULL) && (name != NULL));
7550Sstevel@tonic-gate 	ASSERT((prop_op == PROP_LEN) || (valuep != NULL));
7560Sstevel@tonic-gate 	ASSERT(lengthp != NULL);
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 	/*
7590Sstevel@tonic-gate 	 * we can only be invoked after a driver has been opened and
7600Sstevel@tonic-gate 	 * someone has a layered handle to it, so there had better be
7610Sstevel@tonic-gate 	 * a valid ops vector.
7620Sstevel@tonic-gate 	 */
7630Sstevel@tonic-gate 	ops = DEVI(dip)->devi_ops;
7640Sstevel@tonic-gate 	ASSERT(ops && ops->devo_cb_ops);
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	/*
7670Sstevel@tonic-gate 	 * Some nexus drivers incorrectly set cb_prop_op to nodev,
7680Sstevel@tonic-gate 	 * nulldev or even NULL.
7690Sstevel@tonic-gate 	 */
7700Sstevel@tonic-gate 	if ((ops->devo_cb_ops->cb_prop_op == nodev) ||
7710Sstevel@tonic-gate 	    (ops->devo_cb_ops->cb_prop_op == nulldev) ||
7720Sstevel@tonic-gate 	    (ops->devo_cb_ops->cb_prop_op == NULL)) {
7730Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
7740Sstevel@tonic-gate 	}
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	/* check if this is actually DDI_DEV_T_ANY query */
7770Sstevel@tonic-gate 	if (flags & LDI_DEV_T_ANY) {
7780Sstevel@tonic-gate 		flags &= ~LDI_DEV_T_ANY;
7790Sstevel@tonic-gate 		dev = DDI_DEV_T_ANY;
7800Sstevel@tonic-gate 	}
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 	res = cdev_prop_op(dev, dip, prop_op, flags, name, valuep, lengthp);
7830Sstevel@tonic-gate 	return (res);
7840Sstevel@tonic-gate }
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate static void
7870Sstevel@tonic-gate i_ldi_prop_op_free(struct prop_driver_data *pdd)
7880Sstevel@tonic-gate {
7890Sstevel@tonic-gate 	kmem_free(pdd, pdd->pdd_size);
7900Sstevel@tonic-gate }
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate static caddr_t
7930Sstevel@tonic-gate i_ldi_prop_op_alloc(int prop_len)
7940Sstevel@tonic-gate {
7950Sstevel@tonic-gate 	struct prop_driver_data	*pdd;
7960Sstevel@tonic-gate 	int			pdd_size;
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	pdd_size = sizeof (struct prop_driver_data) + prop_len;
7990Sstevel@tonic-gate 	pdd = kmem_alloc(pdd_size, KM_SLEEP);
8000Sstevel@tonic-gate 	pdd->pdd_size = pdd_size;
8010Sstevel@tonic-gate 	pdd->pdd_prop_free = i_ldi_prop_op_free;
8020Sstevel@tonic-gate 	return ((caddr_t)&pdd[1]);
8030Sstevel@tonic-gate }
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate /*
8060Sstevel@tonic-gate  * i_ldi_prop_op_typed() is a wrapper for i_ldi_prop_op that is used
8070Sstevel@tonic-gate  * by the typed ldi property lookup interfaces.
8080Sstevel@tonic-gate  */
8090Sstevel@tonic-gate static int
8100Sstevel@tonic-gate i_ldi_prop_op_typed(dev_t dev, dev_info_t *dip, int flags, char *name,
8110Sstevel@tonic-gate     caddr_t *datap, int *lengthp, int elem_size)
8120Sstevel@tonic-gate {
8130Sstevel@tonic-gate 	caddr_t	prop_val;
8140Sstevel@tonic-gate 	int	prop_len, res;
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 	ASSERT((dip != NULL) && (name != NULL));
8170Sstevel@tonic-gate 	ASSERT((datap != NULL) && (lengthp != NULL));
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	/*
8200Sstevel@tonic-gate 	 * first call the drivers prop_op() interface to allow it
8210Sstevel@tonic-gate 	 * it to override default property values.
8220Sstevel@tonic-gate 	 */
8230Sstevel@tonic-gate 	res = i_ldi_prop_op(dev, dip, PROP_LEN,
8240Sstevel@tonic-gate 	    flags | DDI_PROP_DYNAMIC, name, NULL, &prop_len);
8250Sstevel@tonic-gate 	if (res != DDI_PROP_SUCCESS)
8260Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	/* sanity check the property length */
8290Sstevel@tonic-gate 	if (prop_len == 0) {
8300Sstevel@tonic-gate 		/*
8310Sstevel@tonic-gate 		 * the ddi typed interfaces don't allow a drivers to
8320Sstevel@tonic-gate 		 * create properties with a length of 0.  so we should
8330Sstevel@tonic-gate 		 * prevent drivers from returning 0 length dynamic
8340Sstevel@tonic-gate 		 * properties for typed property lookups.
8350Sstevel@tonic-gate 		 */
8360Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
8370Sstevel@tonic-gate 	}
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 	/* sanity check the property length against the element size */
8400Sstevel@tonic-gate 	if (elem_size && ((prop_len % elem_size) != 0))
8410Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	/*
8440Sstevel@tonic-gate 	 * got it.  now allocate a prop_driver_data struct so that the
8450Sstevel@tonic-gate 	 * user can free the property via ddi_prop_free().
8460Sstevel@tonic-gate 	 */
8470Sstevel@tonic-gate 	prop_val = i_ldi_prop_op_alloc(prop_len);
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 	/* lookup the property again, this time get the value */
8500Sstevel@tonic-gate 	res = i_ldi_prop_op(dev, dip, PROP_LEN_AND_VAL_BUF,
8510Sstevel@tonic-gate 	    flags | DDI_PROP_DYNAMIC, name, prop_val, &prop_len);
8520Sstevel@tonic-gate 	if (res != DDI_PROP_SUCCESS) {
8530Sstevel@tonic-gate 		ddi_prop_free(prop_val);
8540Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
8550Sstevel@tonic-gate 	}
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	/* sanity check the property length */
8580Sstevel@tonic-gate 	if (prop_len == 0) {
8590Sstevel@tonic-gate 		ddi_prop_free(prop_val);
8600Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
8610Sstevel@tonic-gate 	}
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 	/* sanity check the property length against the element size */
8640Sstevel@tonic-gate 	if (elem_size && ((prop_len % elem_size) != 0)) {
8650Sstevel@tonic-gate 		ddi_prop_free(prop_val);
8660Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
8670Sstevel@tonic-gate 	}
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 	/*
8700Sstevel@tonic-gate 	 * return the prop_driver_data struct and, optionally, the length
8710Sstevel@tonic-gate 	 * of the data.
8720Sstevel@tonic-gate 	 */
8730Sstevel@tonic-gate 	*datap = prop_val;
8740Sstevel@tonic-gate 	*lengthp = prop_len;
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
8770Sstevel@tonic-gate }
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate /*
8800Sstevel@tonic-gate  * i_check_string looks at a string property and makes sure its
8810Sstevel@tonic-gate  * a valid null terminated string
8820Sstevel@tonic-gate  */
8830Sstevel@tonic-gate static int
8840Sstevel@tonic-gate i_check_string(char *str, int prop_len)
8850Sstevel@tonic-gate {
8860Sstevel@tonic-gate 	int i;
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	ASSERT(str != NULL);
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 	for (i = 0; i < prop_len; i++) {
8910Sstevel@tonic-gate 		if (str[i] == '\0')
8920Sstevel@tonic-gate 			return (0);
8930Sstevel@tonic-gate 	}
8940Sstevel@tonic-gate 	return (1);
8950Sstevel@tonic-gate }
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate /*
8980Sstevel@tonic-gate  * i_pack_string_array takes a a string array property that is represented
8990Sstevel@tonic-gate  * as a concatination of strings (with the NULL character included for
9000Sstevel@tonic-gate  * each string) and converts it into a format that can be returned by
9010Sstevel@tonic-gate  * ldi_prop_lookup_string_array.
9020Sstevel@tonic-gate  */
9030Sstevel@tonic-gate static int
9040Sstevel@tonic-gate i_pack_string_array(char *str_concat, int prop_len,
9050Sstevel@tonic-gate     char ***str_arrayp, int *nelemp)
9060Sstevel@tonic-gate {
9070Sstevel@tonic-gate 	int i, nelem, pack_size;
9080Sstevel@tonic-gate 	char **str_array, *strptr;
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	/*
9110Sstevel@tonic-gate 	 * first we need to sanity check the input string array.
9120Sstevel@tonic-gate 	 * in essence this can be done my making sure that the last
9130Sstevel@tonic-gate 	 * character of the array passed in is null.  (meaning the last
9140Sstevel@tonic-gate 	 * string in the array is NULL terminated.
9150Sstevel@tonic-gate 	 */
9160Sstevel@tonic-gate 	if (str_concat[prop_len - 1] != '\0')
9170Sstevel@tonic-gate 		return (1);
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 	/* now let's count the number of strings in the array */
9200Sstevel@tonic-gate 	for (nelem = i = 0; i < prop_len; i++)
9210Sstevel@tonic-gate 		if (str_concat[i] == '\0')
9220Sstevel@tonic-gate 			nelem++;
9230Sstevel@tonic-gate 	ASSERT(nelem >= 1);
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 	/* now let's allocate memory for the new packed property */
9260Sstevel@tonic-gate 	pack_size = (sizeof (char *) * (nelem + 1)) + prop_len;
9270Sstevel@tonic-gate 	str_array = (char **)i_ldi_prop_op_alloc(pack_size);
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	/* let's copy the actual string data into the new property */
9300Sstevel@tonic-gate 	strptr = (char *)&(str_array[nelem + 1]);
9310Sstevel@tonic-gate 	bcopy(str_concat, strptr, prop_len);
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 	/* now initialize the string array pointers */
9340Sstevel@tonic-gate 	for (i = 0; i < nelem; i++) {
9350Sstevel@tonic-gate 		str_array[i] = strptr;
9360Sstevel@tonic-gate 		strptr += strlen(strptr) + 1;
9370Sstevel@tonic-gate 	}
9380Sstevel@tonic-gate 	str_array[nelem] = NULL;
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate 	/* set the return values */
9410Sstevel@tonic-gate 	*str_arrayp = str_array;
9420Sstevel@tonic-gate 	*nelemp = nelem;
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	return (0);
9450Sstevel@tonic-gate }
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate /*
9490Sstevel@tonic-gate  * LDI Project private device usage interfaces
9500Sstevel@tonic-gate  */
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate /*
9530Sstevel@tonic-gate  * Get a count of how many devices are currentl open by different consumers
9540Sstevel@tonic-gate  */
9550Sstevel@tonic-gate int
9560Sstevel@tonic-gate ldi_usage_count()
9570Sstevel@tonic-gate {
9580Sstevel@tonic-gate 	return (ldi_handle_hash_count);
9590Sstevel@tonic-gate }
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate static void
9620Sstevel@tonic-gate ldi_usage_walker_tgt_helper(ldi_usage_t *ldi_usage, vnode_t *vp)
9630Sstevel@tonic-gate {
9640Sstevel@tonic-gate 	dev_info_t	*dip;
9650Sstevel@tonic-gate 	dev_t		dev;
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 	ASSERT(STYP_VALID(VTYP_TO_STYP(vp->v_type)));
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 	/* get the target devt */
9700Sstevel@tonic-gate 	dev = vp->v_rdev;
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 	/* try to get the target dip */
9730Sstevel@tonic-gate 	dip = VTOCS(vp)->s_dip;
9740Sstevel@tonic-gate 	if (dip != NULL) {
9750Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
9760Sstevel@tonic-gate 	} else if (dev != DDI_DEV_T_NONE) {
9770Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
9780Sstevel@tonic-gate 	}
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 	/* set the target information */
9810Sstevel@tonic-gate 	ldi_usage->tgt_name = mod_major_to_name(getmajor(dev));
9820Sstevel@tonic-gate 	ldi_usage->tgt_modid = mod_name_to_modid(ldi_usage->tgt_name);
9830Sstevel@tonic-gate 	ldi_usage->tgt_devt = dev;
9840Sstevel@tonic-gate 	ldi_usage->tgt_spec_type = VTYP_TO_STYP(vp->v_type);
9850Sstevel@tonic-gate 	ldi_usage->tgt_dip = dip;
9860Sstevel@tonic-gate }
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate static int
9900Sstevel@tonic-gate ldi_usage_walker_helper(struct ldi_ident *lip, vnode_t *vp,
9910Sstevel@tonic-gate     void *arg, int (*callback)(const ldi_usage_t *, void *))
9920Sstevel@tonic-gate {
9930Sstevel@tonic-gate 	ldi_usage_t	ldi_usage;
9940Sstevel@tonic-gate 	struct devnames	*dnp;
9950Sstevel@tonic-gate 	dev_info_t	*dip;
9960Sstevel@tonic-gate 	major_t		major;
9970Sstevel@tonic-gate 	dev_t		dev;
9980Sstevel@tonic-gate 	int		ret = LDI_USAGE_CONTINUE;
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 	/* set the target device information */
10010Sstevel@tonic-gate 	ldi_usage_walker_tgt_helper(&ldi_usage, vp);
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 	/* get the source devt */
10040Sstevel@tonic-gate 	dev = lip->li_dev;
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	/* try to get the source dip */
10070Sstevel@tonic-gate 	dip = lip->li_dip;
10080Sstevel@tonic-gate 	if (dip != NULL) {
10090Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
10100Sstevel@tonic-gate 	} else if (dev != DDI_DEV_T_NONE) {
10110Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
10120Sstevel@tonic-gate 	}
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 	/* set the valid source information */
10150Sstevel@tonic-gate 	ldi_usage.src_modid = lip->li_modid;
10160Sstevel@tonic-gate 	ldi_usage.src_name = lip->li_modname;
10170Sstevel@tonic-gate 	ldi_usage.src_devt = dev;
10180Sstevel@tonic-gate 	ldi_usage.src_dip = dip;
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 	/*
10210Sstevel@tonic-gate 	 * if the source ident represents either:
10220Sstevel@tonic-gate 	 *
10230Sstevel@tonic-gate 	 * - a kernel module (and not a device or device driver)
10240Sstevel@tonic-gate 	 * - a device node
10250Sstevel@tonic-gate 	 *
10260Sstevel@tonic-gate 	 * then we currently have all the info we need to report the
10270Sstevel@tonic-gate 	 * usage information so invoke the callback function.
10280Sstevel@tonic-gate 	 */
10290Sstevel@tonic-gate 	if (((lip->li_major == -1) && (dev == DDI_DEV_T_NONE)) ||
10300Sstevel@tonic-gate 	    (dip != NULL)) {
10310Sstevel@tonic-gate 		ret = callback(&ldi_usage, arg);
10320Sstevel@tonic-gate 		if (dip != NULL)
10330Sstevel@tonic-gate 			ddi_release_devi(dip);
10340Sstevel@tonic-gate 		if (ldi_usage.tgt_dip != NULL)
10350Sstevel@tonic-gate 			ddi_release_devi(ldi_usage.tgt_dip);
10360Sstevel@tonic-gate 		return (ret);
10370Sstevel@tonic-gate 	}
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 	/*
10400Sstevel@tonic-gate 	 * now this is kinda gross.
10410Sstevel@tonic-gate 	 *
10420Sstevel@tonic-gate 	 * what we do here is attempt to associate every device instance
10430Sstevel@tonic-gate 	 * of the source driver on the system with the open target driver.
10440Sstevel@tonic-gate 	 * we do this because we don't know which instance of the device
10450Sstevel@tonic-gate 	 * could potentially access the lower device so we assume that all
10460Sstevel@tonic-gate 	 * the instances could access it.
10470Sstevel@tonic-gate 	 *
10480Sstevel@tonic-gate 	 * there are two ways we could have gotten here:
10490Sstevel@tonic-gate 	 *
10500Sstevel@tonic-gate 	 * 1) this layered ident represents one created using only a
10510Sstevel@tonic-gate 	 *    major number or a driver module name.  this means that when
10520Sstevel@tonic-gate 	 *    it was created we could not associate it with a particular
10530Sstevel@tonic-gate 	 *    dev_t or device instance.
10540Sstevel@tonic-gate 	 *
10550Sstevel@tonic-gate 	 *    when could this possibly happen you ask?
10560Sstevel@tonic-gate 	 *
10570Sstevel@tonic-gate 	 *    a perfect example of this is streams persistent links.
10580Sstevel@tonic-gate 	 *    when a persistant streams link is formed we can't associate
10590Sstevel@tonic-gate 	 *    the lower device stream with any particular upper device
10600Sstevel@tonic-gate 	 *    stream or instance.  this is because any particular upper
10610Sstevel@tonic-gate 	 *    device stream could be closed, then another could be
10620Sstevel@tonic-gate 	 *    opened with a different dev_t and device instance, and it
10630Sstevel@tonic-gate 	 *    would still have access to the lower linked stream.
10640Sstevel@tonic-gate 	 *
10650Sstevel@tonic-gate 	 *    since any instance of the upper streams driver could
10660Sstevel@tonic-gate 	 *    potentially access the lower stream whenever it wants,
10670Sstevel@tonic-gate 	 *    we represent that here by associating the opened lower
10680Sstevel@tonic-gate 	 *    device with every existing device instance of the upper
10690Sstevel@tonic-gate 	 *    streams driver.
10700Sstevel@tonic-gate 	 *
10710Sstevel@tonic-gate 	 * 2) This case should really never happen but we'll include it
10720Sstevel@tonic-gate 	 *    for completeness.
10730Sstevel@tonic-gate 	 *
10740Sstevel@tonic-gate 	 *    it's possible that we could have gotten here because we
10750Sstevel@tonic-gate 	 *    have a dev_t for the upper device but we couldn't find a
10760Sstevel@tonic-gate 	 *    dip associated with that dev_t.
10770Sstevel@tonic-gate 	 *
10780Sstevel@tonic-gate 	 *    the only types of devices that have dev_t without an
10790Sstevel@tonic-gate 	 *    associated dip are unbound DLPIv2 network devices.  These
10800Sstevel@tonic-gate 	 *    types of devices exist to be able to attach a stream to any
10810Sstevel@tonic-gate 	 *    instance of a hardware network device.  since these types of
10820Sstevel@tonic-gate 	 *    devices are usually hardware devices they should never
10830Sstevel@tonic-gate 	 *    really have other devices open.
10840Sstevel@tonic-gate 	 */
10850Sstevel@tonic-gate 	if (dev != DDI_DEV_T_NONE)
10860Sstevel@tonic-gate 		major = getmajor(dev);
10870Sstevel@tonic-gate 	else
10880Sstevel@tonic-gate 		major = lip->li_major;
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 	ASSERT((major >= 0) && (major < devcnt));
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate 	dnp = &devnamesp[major];
10930Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
10940Sstevel@tonic-gate 	dip = dnp->dn_head;
10950Sstevel@tonic-gate 	while ((dip) && (ret == LDI_USAGE_CONTINUE)) {
10960Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
10970Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 		/* set the source dip */
11000Sstevel@tonic-gate 		ldi_usage.src_dip = dip;
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 		/* invoke the callback function */
11030Sstevel@tonic-gate 		ret = callback(&ldi_usage, arg);
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
11060Sstevel@tonic-gate 		ddi_release_devi(dip);
11070Sstevel@tonic-gate 		dip = ddi_get_next(dip);
11080Sstevel@tonic-gate 	}
11090Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 	/* if there was a target dip, release it */
11120Sstevel@tonic-gate 	if (ldi_usage.tgt_dip != NULL)
11130Sstevel@tonic-gate 		ddi_release_devi(ldi_usage.tgt_dip);
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 	return (ret);
11160Sstevel@tonic-gate }
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate /*
11190Sstevel@tonic-gate  * ldi_usage_walker() - this walker reports LDI kernel device usage
11200Sstevel@tonic-gate  * information via the callback() callback function.  the LDI keeps track
11210Sstevel@tonic-gate  * of what devices are being accessed in its own internal data structures.
11220Sstevel@tonic-gate  * this function walks those data structures to determine device usage.
11230Sstevel@tonic-gate  */
11240Sstevel@tonic-gate void
11250Sstevel@tonic-gate ldi_usage_walker(void *arg, int (*callback)(const ldi_usage_t *, void *))
11260Sstevel@tonic-gate {
11270Sstevel@tonic-gate 	struct ldi_handle	*lhp;
11280Sstevel@tonic-gate 	struct ldi_ident	*lip;
11290Sstevel@tonic-gate 	vnode_t			*vp;
11300Sstevel@tonic-gate 	int			i;
11310Sstevel@tonic-gate 	int			ret = LDI_USAGE_CONTINUE;
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 	for (i = 0; i < LH_HASH_SZ; i++) {
11340Sstevel@tonic-gate 		mutex_enter(&ldi_handle_hash_lock[i]);
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 		lhp = ldi_handle_hash[i];
11370Sstevel@tonic-gate 		while ((lhp != NULL) && (ret == LDI_USAGE_CONTINUE)) {
11380Sstevel@tonic-gate 			lip = lhp->lh_ident;
11390Sstevel@tonic-gate 			vp = lhp->lh_vp;
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 			/* invoke the devinfo callback function */
11420Sstevel@tonic-gate 			ret = ldi_usage_walker_helper(lip, vp, arg, callback);
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 			lhp = lhp->lh_next;
11450Sstevel@tonic-gate 		}
11460Sstevel@tonic-gate 		mutex_exit(&ldi_handle_hash_lock[i]);
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate 		if (ret != LDI_USAGE_CONTINUE)
11490Sstevel@tonic-gate 			break;
11500Sstevel@tonic-gate 	}
11510Sstevel@tonic-gate }
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate /*
11540Sstevel@tonic-gate  * LDI Project private interfaces (streams linking interfaces)
11550Sstevel@tonic-gate  *
11560Sstevel@tonic-gate  * Streams supports a type of built in device layering via linking.
11570Sstevel@tonic-gate  * Certain types of streams drivers can be streams multiplexors.
11580Sstevel@tonic-gate  * A streams multiplexor supports the I_LINK/I_PLINK operation.
11590Sstevel@tonic-gate  * These operations allows other streams devices to be linked under the
11600Sstevel@tonic-gate  * multiplexor.  By definition all streams multiplexors are devices
11610Sstevel@tonic-gate  * so this linking is a type of device layering where the multiplexor
11620Sstevel@tonic-gate  * device is layered on top of the device linked below it.
11630Sstevel@tonic-gate  */
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate /*
11660Sstevel@tonic-gate  * ldi_mlink_lh() is invoked when streams are linked using LDI handles.
11670Sstevel@tonic-gate  * It is not used for normal I_LINKs and I_PLINKs using file descriptors.
11680Sstevel@tonic-gate  *
11690Sstevel@tonic-gate  * The streams framework keeps track of links via the file_t of the lower
11700Sstevel@tonic-gate  * stream.  The LDI keeps track of devices using a vnode.  In the case
11710Sstevel@tonic-gate  * of a streams link created via an LDI handle, fnk_lh() allocates
11720Sstevel@tonic-gate  * a file_t that the streams framework can use to track the linkage.
11730Sstevel@tonic-gate  */
11740Sstevel@tonic-gate int
11750Sstevel@tonic-gate ldi_mlink_lh(vnode_t *vp, int cmd, intptr_t arg, cred_t *crp, int *rvalp)
11760Sstevel@tonic-gate {
11770Sstevel@tonic-gate 	struct ldi_handle	*lhp = (struct ldi_handle *)arg;
11780Sstevel@tonic-gate 	vnode_t			*vpdown;
11790Sstevel@tonic-gate 	file_t			*fpdown;
11800Sstevel@tonic-gate 	int			err;
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 	if (lhp == NULL)
11830Sstevel@tonic-gate 		return (EINVAL);
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate 	vpdown = lhp->lh_vp;
11860Sstevel@tonic-gate 	ASSERT(vn_matchops(vpdown, spec_getvnodeops()));
11870Sstevel@tonic-gate 	ASSERT(cmd == _I_PLINK_LH);
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate 	/*
11900Sstevel@tonic-gate 	 * create a new lower vnode and a file_t that points to it,
11910Sstevel@tonic-gate 	 * streams linking requires a file_t.  falloc() returns with
11920Sstevel@tonic-gate 	 * fpdown locked.
11930Sstevel@tonic-gate 	 */
11940Sstevel@tonic-gate 	VN_HOLD(vpdown);
11950Sstevel@tonic-gate 	(void) falloc(vpdown, FREAD|FWRITE, &fpdown, NULL);
11960Sstevel@tonic-gate 	mutex_exit(&fpdown->f_tlock);
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 	/* try to establish the link */
11990Sstevel@tonic-gate 	err = mlink_file(vp, I_PLINK, fpdown, crp, rvalp, 1);
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate 	if (err != 0) {
12020Sstevel@tonic-gate 		/* the link failed, free the file_t and release the vnode */
12030Sstevel@tonic-gate 		mutex_enter(&fpdown->f_tlock);
12040Sstevel@tonic-gate 		unfalloc(fpdown);
12050Sstevel@tonic-gate 		VN_RELE(vpdown);
12060Sstevel@tonic-gate 	}
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 	return (err);
12090Sstevel@tonic-gate }
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate /*
12120Sstevel@tonic-gate  * ldi_mlink_fp() is invoked for all successfull streams linkages created
12130Sstevel@tonic-gate  * via I_LINK and I_PLINK.  ldi_mlink_fp() records the linkage information
12140Sstevel@tonic-gate  * in its internal state so that the devinfo snapshot code has some
12150Sstevel@tonic-gate  * observability into streams device linkage information.
12160Sstevel@tonic-gate  */
12170Sstevel@tonic-gate void
12180Sstevel@tonic-gate ldi_mlink_fp(struct stdata *stp, file_t *fpdown, int lhlink, int type)
12190Sstevel@tonic-gate {
12200Sstevel@tonic-gate 	vnode_t			*vp = fpdown->f_vnode;
12210Sstevel@tonic-gate 	struct snode		*sp, *csp;
12220Sstevel@tonic-gate 	ldi_ident_t		li;
12230Sstevel@tonic-gate 	major_t			major;
12240Sstevel@tonic-gate 	int			ret;
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 	/* if the lower stream is not a device then return */
12270Sstevel@tonic-gate 	if (!vn_matchops(vp, spec_getvnodeops()))
12280Sstevel@tonic-gate 		return;
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 	LDI_STREAMS_LNK((CE_NOTE, "%s: linking streams "
12330Sstevel@tonic-gate 		"stp=0x%p, fpdown=0x%p", "ldi_mlink_fp",
12340Sstevel@tonic-gate 		(void *)stp, (void *)fpdown));
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 	sp = VTOS(vp);
12370Sstevel@tonic-gate 	csp = VTOS(sp->s_commonvp);
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate 	/* check if this was a plink via a layered handle */
12400Sstevel@tonic-gate 	if (lhlink) {
12410Sstevel@tonic-gate 		/*
12420Sstevel@tonic-gate 		 * increment the common snode s_count.
12430Sstevel@tonic-gate 		 *
12440Sstevel@tonic-gate 		 * this is done because after the link operation there
12450Sstevel@tonic-gate 		 * are two ways that s_count can be decremented.
12460Sstevel@tonic-gate 		 *
12470Sstevel@tonic-gate 		 * when the layered handle used to create the link is
12480Sstevel@tonic-gate 		 * closed, spec_close() is called and it will decrement
12490Sstevel@tonic-gate 		 * s_count in the common snode.  if we don't increment
12500Sstevel@tonic-gate 		 * s_count here then this could cause spec_close() to
12510Sstevel@tonic-gate 		 * actually close the device while it's still linked
12520Sstevel@tonic-gate 		 * under a multiplexer.
12530Sstevel@tonic-gate 		 *
12540Sstevel@tonic-gate 		 * also, when the lower stream is unlinked, closef() is
12550Sstevel@tonic-gate 		 * called for the file_t associated with this snode.
12560Sstevel@tonic-gate 		 * closef() will call spec_close(), which will decrement
12570Sstevel@tonic-gate 		 * s_count.  if we dont't increment s_count here then this
12580Sstevel@tonic-gate 		 * could cause spec_close() to actually close the device
12590Sstevel@tonic-gate 		 * while there may still be valid layered handles
12600Sstevel@tonic-gate 		 * pointing to it.
12610Sstevel@tonic-gate 		 */
12620Sstevel@tonic-gate 		mutex_enter(&csp->s_lock);
12630Sstevel@tonic-gate 		ASSERT(csp->s_count >= 1);
12640Sstevel@tonic-gate 		csp->s_count++;
12650Sstevel@tonic-gate 		mutex_exit(&csp->s_lock);
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 		/*
12680Sstevel@tonic-gate 		 * decrement the f_count.
12690Sstevel@tonic-gate 		 * this is done because the layered driver framework does
12700Sstevel@tonic-gate 		 * not actually cache a copy of the file_t allocated to
12710Sstevel@tonic-gate 		 * do the link.  this is done here instead of in ldi_mlink_lh()
12720Sstevel@tonic-gate 		 * because there is a window in ldi_mlink_lh() between where
12730Sstevel@tonic-gate 		 * milnk_file() returns and we would decrement the f_count
12740Sstevel@tonic-gate 		 * when the stream could be unlinked.
12750Sstevel@tonic-gate 		 */
12760Sstevel@tonic-gate 		mutex_enter(&fpdown->f_tlock);
12770Sstevel@tonic-gate 		fpdown->f_count--;
12780Sstevel@tonic-gate 		mutex_exit(&fpdown->f_tlock);
12790Sstevel@tonic-gate 	}
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 	/*
12820Sstevel@tonic-gate 	 * NOTE: here we rely on the streams subsystem not allowing
12830Sstevel@tonic-gate 	 * a stream to be multiplexed more than once.  if this
12840Sstevel@tonic-gate 	 * changes, we break.
12850Sstevel@tonic-gate 	 *
12860Sstevel@tonic-gate 	 * mark the snode/stream as multiplexed
12870Sstevel@tonic-gate 	 */
12880Sstevel@tonic-gate 	mutex_enter(&sp->s_lock);
12890Sstevel@tonic-gate 	ASSERT(!(sp->s_flag & SMUXED));
12900Sstevel@tonic-gate 	sp->s_flag |= SMUXED;
12910Sstevel@tonic-gate 	mutex_exit(&sp->s_lock);
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate 	/* get a layered ident for the upper stream */
12940Sstevel@tonic-gate 	if (type == LINKNORMAL) {
12950Sstevel@tonic-gate 		/*
12960Sstevel@tonic-gate 		 * if the link is not persistant then we can associate
12970Sstevel@tonic-gate 		 * the upper stream with a dev_t.  this is because the
12980Sstevel@tonic-gate 		 * upper stream is associated with a vnode, which is
12990Sstevel@tonic-gate 		 * associated with a dev_t and this binding can't change
13000Sstevel@tonic-gate 		 * during the life of the stream.  since the link isn't
13010Sstevel@tonic-gate 		 * persistant once the stream is destroyed the link is
13020Sstevel@tonic-gate 		 * destroyed.  so the dev_t will be valid for the life
13030Sstevel@tonic-gate 		 * of the link.
13040Sstevel@tonic-gate 		 */
13050Sstevel@tonic-gate 		ret = ldi_ident_from_stream(getendq(stp->sd_wrq), &li);
13060Sstevel@tonic-gate 	} else {
13070Sstevel@tonic-gate 		/*
13080Sstevel@tonic-gate 		 * if the link is persistant we can only associate the
13090Sstevel@tonic-gate 		 * link with a driver (and not a dev_t.)  this is
13100Sstevel@tonic-gate 		 * because subsequent opens of the upper device may result
13110Sstevel@tonic-gate 		 * in a different stream (and dev_t) having access to
13120Sstevel@tonic-gate 		 * the lower stream.
13130Sstevel@tonic-gate 		 *
13140Sstevel@tonic-gate 		 * for example, if the upper stream is closed after the
13150Sstevel@tonic-gate 		 * persistant link operation is compleated, a subsequent
13160Sstevel@tonic-gate 		 * open of the upper device will create a new stream which
13170Sstevel@tonic-gate 		 * may have a different dev_t and an unlink operation
13180Sstevel@tonic-gate 		 * can be performed using this new upper stream.
13190Sstevel@tonic-gate 		 */
13200Sstevel@tonic-gate 		ASSERT(type == LINKPERSIST);
13210Sstevel@tonic-gate 		major = getmajor(stp->sd_vnode->v_rdev);
13220Sstevel@tonic-gate 		ret = ldi_ident_from_major(major, &li);
13230Sstevel@tonic-gate 	}
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 	ASSERT(ret == 0);
13260Sstevel@tonic-gate 	(void) handle_alloc(vp, (struct ldi_ident *)li);
13270Sstevel@tonic-gate 	ldi_ident_release(li);
13280Sstevel@tonic-gate }
13290Sstevel@tonic-gate 
13300Sstevel@tonic-gate void
13310Sstevel@tonic-gate ldi_munlink_fp(struct stdata *stp, file_t *fpdown, int type)
13320Sstevel@tonic-gate {
13330Sstevel@tonic-gate 	struct ldi_handle	*lhp;
13340Sstevel@tonic-gate 	vnode_t			*vp = (vnode_t *)fpdown->f_vnode;
13350Sstevel@tonic-gate 	struct snode		*sp;
13360Sstevel@tonic-gate 	ldi_ident_t		li;
13370Sstevel@tonic-gate 	major_t			major;
13380Sstevel@tonic-gate 	int			ret;
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 	/* if the lower stream is not a device then return */
13410Sstevel@tonic-gate 	if (!vn_matchops(vp, spec_getvnodeops()))
13420Sstevel@tonic-gate 		return;
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
13450Sstevel@tonic-gate 	ASSERT((type == LINKNORMAL) || (type == LINKPERSIST));
13460Sstevel@tonic-gate 
13470Sstevel@tonic-gate 	LDI_STREAMS_LNK((CE_NOTE, "%s: unlinking streams "
13480Sstevel@tonic-gate 		"stp=0x%p, fpdown=0x%p", "ldi_munlink_fp",
13490Sstevel@tonic-gate 		(void *)stp, (void *)fpdown));
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 	/*
13520Sstevel@tonic-gate 	 * NOTE: here we rely on the streams subsystem not allowing
13530Sstevel@tonic-gate 	 * a stream to be multiplexed more than once.  if this
13540Sstevel@tonic-gate 	 * changes, we break.
13550Sstevel@tonic-gate 	 *
13560Sstevel@tonic-gate 	 * mark the snode/stream as not multiplexed
13570Sstevel@tonic-gate 	 */
13580Sstevel@tonic-gate 	sp = VTOS(vp);
13590Sstevel@tonic-gate 	mutex_enter(&sp->s_lock);
13600Sstevel@tonic-gate 	ASSERT(sp->s_flag & SMUXED);
13610Sstevel@tonic-gate 	sp->s_flag &= ~SMUXED;
13620Sstevel@tonic-gate 	mutex_exit(&sp->s_lock);
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate 	/*
13650Sstevel@tonic-gate 	 * clear the owner for this snode
13660Sstevel@tonic-gate 	 * see the comment in ldi_mlink_fp() for information about how
13670Sstevel@tonic-gate 	 * the ident is allocated
13680Sstevel@tonic-gate 	 */
13690Sstevel@tonic-gate 	if (type == LINKNORMAL) {
13700Sstevel@tonic-gate 		ret = ldi_ident_from_stream(getendq(stp->sd_wrq), &li);
13710Sstevel@tonic-gate 	} else {
13720Sstevel@tonic-gate 		ASSERT(type == LINKPERSIST);
13730Sstevel@tonic-gate 		major = getmajor(stp->sd_vnode->v_rdev);
13740Sstevel@tonic-gate 		ret = ldi_ident_from_major(major, &li);
13750Sstevel@tonic-gate 	}
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 	ASSERT(ret == 0);
13780Sstevel@tonic-gate 	lhp = handle_find(vp, (struct ldi_ident *)li);
13790Sstevel@tonic-gate 	handle_release(lhp);
13800Sstevel@tonic-gate 	ldi_ident_release(li);
13810Sstevel@tonic-gate }
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate /*
13840Sstevel@tonic-gate  * LDI Consolidation private interfaces
13850Sstevel@tonic-gate  */
13860Sstevel@tonic-gate int
13870Sstevel@tonic-gate ldi_ident_from_mod(struct modlinkage *modlp, ldi_ident_t *lip)
13880Sstevel@tonic-gate {
13890Sstevel@tonic-gate 	struct modctl		*modp;
13900Sstevel@tonic-gate 	major_t			major;
13910Sstevel@tonic-gate 	char			*name;
13920Sstevel@tonic-gate 
13930Sstevel@tonic-gate 	if ((modlp == NULL) || (lip == NULL))
13940Sstevel@tonic-gate 		return (EINVAL);
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate 	modp = mod_getctl(modlp);
13990Sstevel@tonic-gate 	if (modp == NULL)
14000Sstevel@tonic-gate 		return (EINVAL);
14010Sstevel@tonic-gate 	name = modp->mod_modname;
14020Sstevel@tonic-gate 	if (name == NULL)
14030Sstevel@tonic-gate 		return (EINVAL);
14040Sstevel@tonic-gate 	major = mod_name_to_major(name);
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate 	*lip = (ldi_ident_t)ident_alloc(name, NULL, DDI_DEV_T_NONE, major);
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate 	LDI_ALLOCFREE((CE_WARN, "%s: li=0x%p, mod=%s",
14090Sstevel@tonic-gate 		"ldi_ident_from_mod", (void *)*lip, name));
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 	return (0);
14120Sstevel@tonic-gate }
14130Sstevel@tonic-gate 
14140Sstevel@tonic-gate ldi_ident_t
14150Sstevel@tonic-gate ldi_ident_from_anon()
14160Sstevel@tonic-gate {
14170Sstevel@tonic-gate 	ldi_ident_t	lip;
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
14200Sstevel@tonic-gate 
14210Sstevel@tonic-gate 	lip = (ldi_ident_t)ident_alloc("genunix", NULL, DDI_DEV_T_NONE, -1);
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate 	LDI_ALLOCFREE((CE_WARN, "%s: li=0x%p, mod=%s",
14240Sstevel@tonic-gate 		"ldi_ident_from_anon", (void *)lip, "genunix"));
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate 	return (lip);
14270Sstevel@tonic-gate }
14280Sstevel@tonic-gate 
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate /*
14310Sstevel@tonic-gate  * LDI Public interfaces
14320Sstevel@tonic-gate  */
14330Sstevel@tonic-gate int
14340Sstevel@tonic-gate ldi_ident_from_stream(struct queue *sq, ldi_ident_t *lip)
14350Sstevel@tonic-gate {
14360Sstevel@tonic-gate 	struct stdata		*stp;
14370Sstevel@tonic-gate 	dev_t			dev;
14380Sstevel@tonic-gate 	char			*name;
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate 	if ((sq == NULL) || (lip == NULL))
14410Sstevel@tonic-gate 		return (EINVAL);
14420Sstevel@tonic-gate 
14430Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
14440Sstevel@tonic-gate 
14450Sstevel@tonic-gate 	stp = sq->q_stream;
14460Sstevel@tonic-gate 	if (!vn_matchops(stp->sd_vnode, spec_getvnodeops()))
14470Sstevel@tonic-gate 		return (EINVAL);
14480Sstevel@tonic-gate 
14490Sstevel@tonic-gate 	dev = stp->sd_vnode->v_rdev;
14500Sstevel@tonic-gate 	name = mod_major_to_name(getmajor(dev));
14510Sstevel@tonic-gate 	if (name == NULL)
14520Sstevel@tonic-gate 		return (EINVAL);
14530Sstevel@tonic-gate 	*lip = (ldi_ident_t)ident_alloc(name, NULL, dev, -1);
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate 	LDI_ALLOCFREE((CE_WARN,
14560Sstevel@tonic-gate 		"%s: li=0x%p, mod=%s, minor=0x%x, stp=0x%p",
14570Sstevel@tonic-gate 		"ldi_ident_from_stream", (void *)*lip, name, getminor(dev),
14580Sstevel@tonic-gate 		(void *)stp));
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate 	return (0);
14610Sstevel@tonic-gate }
14620Sstevel@tonic-gate 
14630Sstevel@tonic-gate int
14640Sstevel@tonic-gate ldi_ident_from_dev(dev_t dev, ldi_ident_t *lip)
14650Sstevel@tonic-gate {
14660Sstevel@tonic-gate 	char			*name;
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate 	if (lip == NULL)
14690Sstevel@tonic-gate 		return (EINVAL);
14700Sstevel@tonic-gate 
14710Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
14720Sstevel@tonic-gate 
14730Sstevel@tonic-gate 	name = mod_major_to_name(getmajor(dev));
14740Sstevel@tonic-gate 	if (name == NULL)
14750Sstevel@tonic-gate 		return (EINVAL);
14760Sstevel@tonic-gate 	*lip = (ldi_ident_t)ident_alloc(name, NULL, dev, -1);
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate 	LDI_ALLOCFREE((CE_WARN,
14790Sstevel@tonic-gate 		"%s: li=0x%p, mod=%s, minor=0x%x",
14800Sstevel@tonic-gate 		"ldi_ident_from_dev", (void *)*lip, name, getminor(dev)));
14810Sstevel@tonic-gate 
14820Sstevel@tonic-gate 	return (0);
14830Sstevel@tonic-gate }
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate int
14860Sstevel@tonic-gate ldi_ident_from_dip(dev_info_t *dip, ldi_ident_t *lip)
14870Sstevel@tonic-gate {
14880Sstevel@tonic-gate 	struct dev_info		*devi = (struct dev_info *)dip;
14890Sstevel@tonic-gate 	char			*name;
14900Sstevel@tonic-gate 
14910Sstevel@tonic-gate 	if ((dip == NULL) || (lip == NULL))
14920Sstevel@tonic-gate 		return (EINVAL);
14930Sstevel@tonic-gate 
14940Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate 	name = mod_major_to_name(devi->devi_major);
14970Sstevel@tonic-gate 	if (name == NULL)
14980Sstevel@tonic-gate 		return (EINVAL);
14990Sstevel@tonic-gate 	*lip = (ldi_ident_t)ident_alloc(name, dip, DDI_DEV_T_NONE, -1);
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate 	LDI_ALLOCFREE((CE_WARN,
15020Sstevel@tonic-gate 		"%s: li=0x%p, mod=%s, dip=0x%p",
15030Sstevel@tonic-gate 		"ldi_ident_from_dip", (void *)*lip, name, (void *)devi));
15040Sstevel@tonic-gate 
15050Sstevel@tonic-gate 	return (0);
15060Sstevel@tonic-gate }
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate int
15090Sstevel@tonic-gate ldi_ident_from_major(major_t major, ldi_ident_t *lip)
15100Sstevel@tonic-gate {
15110Sstevel@tonic-gate 	char			*name;
15120Sstevel@tonic-gate 
15130Sstevel@tonic-gate 	if (lip == NULL)
15140Sstevel@tonic-gate 		return (EINVAL);
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
15170Sstevel@tonic-gate 
15180Sstevel@tonic-gate 	name = mod_major_to_name(major);
15190Sstevel@tonic-gate 	if (name == NULL)
15200Sstevel@tonic-gate 		return (EINVAL);
15210Sstevel@tonic-gate 	*lip = (ldi_ident_t)ident_alloc(name, NULL, DDI_DEV_T_NONE, major);
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 	LDI_ALLOCFREE((CE_WARN,
15240Sstevel@tonic-gate 		"%s: li=0x%p, mod=%s",
15250Sstevel@tonic-gate 		"ldi_ident_from_major", (void *)*lip, name));
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate 	return (0);
15280Sstevel@tonic-gate }
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate void
15310Sstevel@tonic-gate ldi_ident_release(ldi_ident_t li)
15320Sstevel@tonic-gate {
15330Sstevel@tonic-gate 	struct ldi_ident	*ident = (struct ldi_ident *)li;
15340Sstevel@tonic-gate 	char			*name;
15350Sstevel@tonic-gate 
15360Sstevel@tonic-gate 	if (li == NULL)
15370Sstevel@tonic-gate 		return;
15380Sstevel@tonic-gate 
15390Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
15400Sstevel@tonic-gate 
15410Sstevel@tonic-gate 	name = ident->li_modname;
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate 	LDI_ALLOCFREE((CE_WARN,
15440Sstevel@tonic-gate 		"%s: li=0x%p, mod=%s",
15450Sstevel@tonic-gate 		"ldi_ident_release", (void *)li, name));
15460Sstevel@tonic-gate 
15470Sstevel@tonic-gate 	ident_release((struct ldi_ident *)li);
15480Sstevel@tonic-gate }
15490Sstevel@tonic-gate 
15500Sstevel@tonic-gate /* get a handle to a device by dev_t and otyp */
15510Sstevel@tonic-gate int
15520Sstevel@tonic-gate ldi_open_by_dev(dev_t *devp, int otyp, int flag, cred_t *cr,
15530Sstevel@tonic-gate     ldi_handle_t *lhp, ldi_ident_t li)
15540Sstevel@tonic-gate {
15550Sstevel@tonic-gate 	struct ldi_ident	*lip = (struct ldi_ident *)li;
15560Sstevel@tonic-gate 	int 			ret;
15570Sstevel@tonic-gate 	vnode_t			*vp;
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate 	/* sanity check required input parameters */
15600Sstevel@tonic-gate 	if ((devp == NULL) || (!OTYP_VALID(otyp)) || (cr == NULL) ||
15610Sstevel@tonic-gate 	    (lhp == NULL) || (lip == NULL))
15620Sstevel@tonic-gate 		return (EINVAL);
15630Sstevel@tonic-gate 
15640Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
15650Sstevel@tonic-gate 
15660Sstevel@tonic-gate 	if ((ret = ldi_vp_from_dev(*devp, otyp, &vp)) != 0)
15670Sstevel@tonic-gate 		return (ret);
15680Sstevel@tonic-gate 
15690Sstevel@tonic-gate 	if ((ret = ldi_open_by_vp(&vp, flag, cr, lhp, lip)) == 0) {
15700Sstevel@tonic-gate 		*devp = vp->v_rdev;
15710Sstevel@tonic-gate 	}
15720Sstevel@tonic-gate 	VN_RELE(vp);
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 	return (ret);
15750Sstevel@tonic-gate }
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate /* get a handle to a device by pathname */
15780Sstevel@tonic-gate int
15790Sstevel@tonic-gate ldi_open_by_name(char *pathname, int flag, cred_t *cr,
15800Sstevel@tonic-gate     ldi_handle_t *lhp, ldi_ident_t li)
15810Sstevel@tonic-gate {
15820Sstevel@tonic-gate 	struct ldi_ident	*lip = (struct ldi_ident *)li;
15830Sstevel@tonic-gate 	int 			ret;
15840Sstevel@tonic-gate 	vnode_t			*vp;
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate 	/* sanity check required input parameters */
15870Sstevel@tonic-gate 	if ((pathname == NULL) || (*pathname != '/') ||
15880Sstevel@tonic-gate 	    (cr == NULL) || (lhp == NULL) || (lip == NULL))
15890Sstevel@tonic-gate 		return (EINVAL);
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
15920Sstevel@tonic-gate 
15930Sstevel@tonic-gate 	if ((ret = ldi_vp_from_name(pathname, &vp)) != 0)
15940Sstevel@tonic-gate 		return (ret);
15950Sstevel@tonic-gate 
15960Sstevel@tonic-gate 	ret = ldi_open_by_vp(&vp, flag, cr, lhp, lip);
15970Sstevel@tonic-gate 	VN_RELE(vp);
15980Sstevel@tonic-gate 
15990Sstevel@tonic-gate 	return (ret);
16000Sstevel@tonic-gate }
16010Sstevel@tonic-gate 
16020Sstevel@tonic-gate /* get a handle to a device by devid and minor_name */
16030Sstevel@tonic-gate int
16040Sstevel@tonic-gate ldi_open_by_devid(ddi_devid_t devid, char *minor_name,
16050Sstevel@tonic-gate     int flag, cred_t *cr, ldi_handle_t *lhp, ldi_ident_t li)
16060Sstevel@tonic-gate {
16070Sstevel@tonic-gate 	struct ldi_ident	*lip = (struct ldi_ident *)li;
16080Sstevel@tonic-gate 	int			ret;
16090Sstevel@tonic-gate 	vnode_t			*vp;
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate 	/* sanity check required input parameters */
16120Sstevel@tonic-gate 	if ((minor_name == NULL) || (cr == NULL) ||
16130Sstevel@tonic-gate 	    (lhp == NULL) || (lip == NULL))
16140Sstevel@tonic-gate 		return (EINVAL);
16150Sstevel@tonic-gate 
16160Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
16170Sstevel@tonic-gate 
16180Sstevel@tonic-gate 	if ((ret = ldi_vp_from_devid(devid, minor_name, &vp)) != 0)
16190Sstevel@tonic-gate 		return (ret);
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate 	ret = ldi_open_by_vp(&vp, flag, cr, lhp, lip);
16220Sstevel@tonic-gate 	VN_RELE(vp);
16230Sstevel@tonic-gate 
16240Sstevel@tonic-gate 	return (ret);
16250Sstevel@tonic-gate }
16260Sstevel@tonic-gate 
16270Sstevel@tonic-gate int
16280Sstevel@tonic-gate ldi_close(ldi_handle_t lh, int flag, cred_t *cr)
16290Sstevel@tonic-gate {
16300Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
16310Sstevel@tonic-gate 	struct ldi_event	*lep;
16320Sstevel@tonic-gate 	int 			err = 0;
16330Sstevel@tonic-gate 
16340Sstevel@tonic-gate 	if (lh == NULL)
16350Sstevel@tonic-gate 		return (EINVAL);
16360Sstevel@tonic-gate 
16370Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
16380Sstevel@tonic-gate 
16390Sstevel@tonic-gate 	/* Flush back any dirty pages associated with the device. */
16400Sstevel@tonic-gate 	if (handlep->lh_type & LH_CBDEV) {
16410Sstevel@tonic-gate 		vnode_t	*cvp = common_specvp(handlep->lh_vp);
16420Sstevel@tonic-gate 		dev_t	dev = cvp->v_rdev;
16430Sstevel@tonic-gate 
16440Sstevel@tonic-gate 		(void) VOP_PUTPAGE(cvp, 0, 0, B_INVAL, kcred);
16450Sstevel@tonic-gate 		bflush(dev);
16460Sstevel@tonic-gate 	}
16470Sstevel@tonic-gate 
16480Sstevel@tonic-gate 	/*
16490Sstevel@tonic-gate 	 * Any event handlers should have been unregistered by the
16500Sstevel@tonic-gate 	 * time ldi_close() is called.  If they haven't then it's a
16510Sstevel@tonic-gate 	 * bug.
16520Sstevel@tonic-gate 	 *
16530Sstevel@tonic-gate 	 * In a debug kernel we'll panic to make the problem obvious.
16540Sstevel@tonic-gate 	 */
16550Sstevel@tonic-gate 	ASSERT(handlep->lh_events == NULL);
16560Sstevel@tonic-gate 
16570Sstevel@tonic-gate 	/*
16580Sstevel@tonic-gate 	 * On a production kernel we'll "do the right thing" (unregister
16590Sstevel@tonic-gate 	 * the event handlers) and then complain about having to do the
16600Sstevel@tonic-gate 	 * work ourselves.
16610Sstevel@tonic-gate 	 */
16620Sstevel@tonic-gate 	while ((lep = handlep->lh_events) != NULL) {
16630Sstevel@tonic-gate 		err = 1;
16640Sstevel@tonic-gate 		(void) ldi_remove_event_handler(lh, (ldi_callback_id_t)lep);
16650Sstevel@tonic-gate 	}
16660Sstevel@tonic-gate 	if (err) {
16670Sstevel@tonic-gate 		struct ldi_ident *lip = handlep->lh_ident;
16680Sstevel@tonic-gate 		ASSERT(lip != NULL);
16690Sstevel@tonic-gate 		cmn_err(CE_NOTE, "ldi err: %s "
16700Sstevel@tonic-gate 		    "failed to unregister layered event handlers before "
16710Sstevel@tonic-gate 		    "closing devices", lip->li_modname);
16720Sstevel@tonic-gate 	}
16730Sstevel@tonic-gate 
16740Sstevel@tonic-gate 	/* do a layered close on the device */
16750Sstevel@tonic-gate 	err = VOP_CLOSE(handlep->lh_vp, flag | FKLYR, 1, (offset_t)0, cr);
16760Sstevel@tonic-gate 
16770Sstevel@tonic-gate 	LDI_OPENCLOSE((CE_WARN, "%s: lh=0x%p", "ldi close", (void *)lh));
16780Sstevel@tonic-gate 
16790Sstevel@tonic-gate 	/*
16800Sstevel@tonic-gate 	 * Free the handle even if the device close failed.  why?
16810Sstevel@tonic-gate 	 *
16820Sstevel@tonic-gate 	 * If the device close failed we can't really make assumptions
16830Sstevel@tonic-gate 	 * about the devices state so we shouldn't allow access to the
16840Sstevel@tonic-gate 	 * device via this handle any more.  If the device consumer wants
16850Sstevel@tonic-gate 	 * to access the device again they should open it again.
16860Sstevel@tonic-gate 	 *
16870Sstevel@tonic-gate 	 * This is the same way file/device close failures are handled
16880Sstevel@tonic-gate 	 * in other places like spec_close() and closeandsetf().
16890Sstevel@tonic-gate 	 */
16900Sstevel@tonic-gate 	handle_release(handlep);
16910Sstevel@tonic-gate 	return (err);
16920Sstevel@tonic-gate }
16930Sstevel@tonic-gate 
16940Sstevel@tonic-gate int
16950Sstevel@tonic-gate ldi_read(ldi_handle_t lh, struct uio *uiop, cred_t *credp)
16960Sstevel@tonic-gate {
16970Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
16980Sstevel@tonic-gate 	vnode_t			*vp;
16990Sstevel@tonic-gate 	dev_t			dev;
17000Sstevel@tonic-gate 	int			ret;
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate 	if (lh == NULL)
17030Sstevel@tonic-gate 		return (EINVAL);
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate 	vp = handlep->lh_vp;
17060Sstevel@tonic-gate 	dev = vp->v_rdev;
17070Sstevel@tonic-gate 	if (handlep->lh_type & LH_CBDEV) {
17080Sstevel@tonic-gate 		ret = cdev_read(dev, uiop, credp);
17090Sstevel@tonic-gate 	} else if (handlep->lh_type & LH_STREAM) {
17100Sstevel@tonic-gate 		ret = strread(vp, uiop, credp);
17110Sstevel@tonic-gate 	} else {
17120Sstevel@tonic-gate 		return (ENOTSUP);
17130Sstevel@tonic-gate 	}
17140Sstevel@tonic-gate 	return (ret);
17150Sstevel@tonic-gate }
17160Sstevel@tonic-gate 
17170Sstevel@tonic-gate int
17180Sstevel@tonic-gate ldi_write(ldi_handle_t lh, struct uio *uiop, cred_t *credp)
17190Sstevel@tonic-gate {
17200Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
17210Sstevel@tonic-gate 	vnode_t			*vp;
17220Sstevel@tonic-gate 	dev_t			dev;
17230Sstevel@tonic-gate 	int			ret;
17240Sstevel@tonic-gate 
17250Sstevel@tonic-gate 	if (lh == NULL)
17260Sstevel@tonic-gate 		return (EINVAL);
17270Sstevel@tonic-gate 
17280Sstevel@tonic-gate 	vp = handlep->lh_vp;
17290Sstevel@tonic-gate 	dev = vp->v_rdev;
17300Sstevel@tonic-gate 	if (handlep->lh_type & LH_CBDEV) {
17310Sstevel@tonic-gate 		ret = cdev_write(dev, uiop, credp);
17320Sstevel@tonic-gate 	} else if (handlep->lh_type & LH_STREAM) {
17330Sstevel@tonic-gate 		ret = strwrite(vp, uiop, credp);
17340Sstevel@tonic-gate 	} else {
17350Sstevel@tonic-gate 		return (ENOTSUP);
17360Sstevel@tonic-gate 	}
17370Sstevel@tonic-gate 	return (ret);
17380Sstevel@tonic-gate }
17390Sstevel@tonic-gate 
17400Sstevel@tonic-gate int
17410Sstevel@tonic-gate ldi_get_size(ldi_handle_t lh, uint64_t *sizep)
17420Sstevel@tonic-gate {
17430Sstevel@tonic-gate 	int 			otyp;
17440Sstevel@tonic-gate 	uint_t			value;
17450Sstevel@tonic-gate 	int64_t			drv_prop64;
17460Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
17470Sstevel@tonic-gate 
17480Sstevel@tonic-gate 
17490Sstevel@tonic-gate 	if ((lh == NULL) || (sizep == NULL))
17500Sstevel@tonic-gate 		return (DDI_FAILURE);
17510Sstevel@tonic-gate 
17520Sstevel@tonic-gate 	if (handlep->lh_type & LH_STREAM)
17530Sstevel@tonic-gate 		return (DDI_FAILURE);
17540Sstevel@tonic-gate 
17550Sstevel@tonic-gate 	/*
17560Sstevel@tonic-gate 	 * Determine device type (char or block).
17570Sstevel@tonic-gate 	 * Character devices support Size/size
17580Sstevel@tonic-gate 	 * property value. Block devices may support
17590Sstevel@tonic-gate 	 * Nblocks/nblocks or Size/size property value.
17600Sstevel@tonic-gate 	 */
17610Sstevel@tonic-gate 	if ((ldi_get_otyp(lh, &otyp)) != 0)
17620Sstevel@tonic-gate 		return (DDI_FAILURE);
17630Sstevel@tonic-gate 
17640Sstevel@tonic-gate 	if (otyp == OTYP_BLK) {
17650Sstevel@tonic-gate 		if (ldi_prop_exists(lh,
17660Sstevel@tonic-gate 			DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "Nblocks")) {
17670Sstevel@tonic-gate 
17680Sstevel@tonic-gate 			drv_prop64 = ldi_prop_get_int64(lh,
17690Sstevel@tonic-gate 			    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
17700Sstevel@tonic-gate 			    "Nblocks", 0);
17710Sstevel@tonic-gate 			*sizep = (uint64_t)ldbtob((uint64_t)drv_prop64);
17720Sstevel@tonic-gate 			return (DDI_SUCCESS);
17730Sstevel@tonic-gate 		}
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate 		if (ldi_prop_exists(lh,
17760Sstevel@tonic-gate 			DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "nblocks")) {
17770Sstevel@tonic-gate 
17780Sstevel@tonic-gate 			value = ldi_prop_get_int(lh,
17790Sstevel@tonic-gate 			    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
17800Sstevel@tonic-gate 			    "nblocks", 0);
17810Sstevel@tonic-gate 			*sizep = (uint64_t)ldbtob(value);
17820Sstevel@tonic-gate 			return (DDI_SUCCESS);
17830Sstevel@tonic-gate 		}
17840Sstevel@tonic-gate 	}
17850Sstevel@tonic-gate 
17860Sstevel@tonic-gate 	if (ldi_prop_exists(lh,
17870Sstevel@tonic-gate 		DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "Size")) {
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate 		drv_prop64 = ldi_prop_get_int64(lh,
17900Sstevel@tonic-gate 		    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "Size", 0);
17910Sstevel@tonic-gate 		*sizep = (uint64_t)drv_prop64;
17920Sstevel@tonic-gate 		return (DDI_SUCCESS);
17930Sstevel@tonic-gate 	}
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate 	if (ldi_prop_exists(lh,
17960Sstevel@tonic-gate 		DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "size")) {
17970Sstevel@tonic-gate 
17980Sstevel@tonic-gate 		value = ldi_prop_get_int(lh,
17990Sstevel@tonic-gate 		    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "size", 0);
18000Sstevel@tonic-gate 		*sizep = (uint64_t)value;
18010Sstevel@tonic-gate 		return (DDI_SUCCESS);
18020Sstevel@tonic-gate 	}
18030Sstevel@tonic-gate 
18040Sstevel@tonic-gate 	/* unable to determine device size */
18050Sstevel@tonic-gate 	return (DDI_FAILURE);
18060Sstevel@tonic-gate }
18070Sstevel@tonic-gate 
18080Sstevel@tonic-gate int
18090Sstevel@tonic-gate ldi_ioctl(ldi_handle_t lh, int cmd, intptr_t arg, int mode,
18100Sstevel@tonic-gate 	cred_t *cr, int *rvalp)
18110Sstevel@tonic-gate {
18120Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
18130Sstevel@tonic-gate 	vnode_t			*vp;
18140Sstevel@tonic-gate 	dev_t			dev;
18150Sstevel@tonic-gate 	int			ret, copymode;
18160Sstevel@tonic-gate 
18170Sstevel@tonic-gate 	if (lh == NULL)
18180Sstevel@tonic-gate 		return (EINVAL);
18190Sstevel@tonic-gate 
18200Sstevel@tonic-gate 	/*
18210Sstevel@tonic-gate 	 * if the data pointed to by arg is located in the kernel then
18220Sstevel@tonic-gate 	 * make sure the FNATIVE flag is set.
18230Sstevel@tonic-gate 	 */
18240Sstevel@tonic-gate 	if (mode & FKIOCTL)
18250Sstevel@tonic-gate 		mode = (mode & ~FMODELS) | FNATIVE | FKIOCTL;
18260Sstevel@tonic-gate 
18270Sstevel@tonic-gate 	vp = handlep->lh_vp;
18280Sstevel@tonic-gate 	dev = vp->v_rdev;
18290Sstevel@tonic-gate 	if (handlep->lh_type & LH_CBDEV) {
18300Sstevel@tonic-gate 		ret = cdev_ioctl(dev, cmd, arg, mode, cr, rvalp);
18310Sstevel@tonic-gate 	} else if (handlep->lh_type & LH_STREAM) {
18320Sstevel@tonic-gate 		copymode = (mode & FKIOCTL) ? K_TO_K : U_TO_K;
18330Sstevel@tonic-gate 
18340Sstevel@tonic-gate 		/*
18350Sstevel@tonic-gate 		 * if we get an I_PLINK from within the kernel the
18360Sstevel@tonic-gate 		 * arg is a layered handle pointer instead of
18370Sstevel@tonic-gate 		 * a file descriptor, so we translate this ioctl
18380Sstevel@tonic-gate 		 * into a private one that can handle this.
18390Sstevel@tonic-gate 		 */
18400Sstevel@tonic-gate 		if ((mode & FKIOCTL) && (cmd == I_PLINK))
18410Sstevel@tonic-gate 			cmd = _I_PLINK_LH;
18420Sstevel@tonic-gate 
18430Sstevel@tonic-gate 		ret = strioctl(vp, cmd, arg, mode, copymode, cr, rvalp);
18440Sstevel@tonic-gate 	} else {
18450Sstevel@tonic-gate 		return (ENOTSUP);
18460Sstevel@tonic-gate 	}
18470Sstevel@tonic-gate 
18480Sstevel@tonic-gate 	return (ret);
18490Sstevel@tonic-gate }
18500Sstevel@tonic-gate 
18510Sstevel@tonic-gate int
18520Sstevel@tonic-gate ldi_poll(ldi_handle_t lh, short events, int anyyet, short *reventsp,
18530Sstevel@tonic-gate     struct pollhead **phpp)
18540Sstevel@tonic-gate {
18550Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
18560Sstevel@tonic-gate 	vnode_t			*vp;
18570Sstevel@tonic-gate 	dev_t			dev;
18580Sstevel@tonic-gate 	int			ret;
18590Sstevel@tonic-gate 
18600Sstevel@tonic-gate 	if (lh == NULL)
18610Sstevel@tonic-gate 		return (EINVAL);
18620Sstevel@tonic-gate 
18630Sstevel@tonic-gate 	vp = handlep->lh_vp;
18640Sstevel@tonic-gate 	dev = vp->v_rdev;
18650Sstevel@tonic-gate 	if (handlep->lh_type & LH_CBDEV) {
18660Sstevel@tonic-gate 		ret = cdev_poll(dev, events, anyyet, reventsp, phpp);
18670Sstevel@tonic-gate 	} else if (handlep->lh_type & LH_STREAM) {
18680Sstevel@tonic-gate 		ret = strpoll(vp->v_stream, events, anyyet, reventsp, phpp);
18690Sstevel@tonic-gate 	} else {
18700Sstevel@tonic-gate 		return (ENOTSUP);
18710Sstevel@tonic-gate 	}
18720Sstevel@tonic-gate 
18730Sstevel@tonic-gate 	return (ret);
18740Sstevel@tonic-gate }
18750Sstevel@tonic-gate 
18760Sstevel@tonic-gate int
18770Sstevel@tonic-gate ldi_prop_op(ldi_handle_t lh, ddi_prop_op_t prop_op,
18780Sstevel@tonic-gate 	int flags, char *name, caddr_t valuep, int *length)
18790Sstevel@tonic-gate {
18800Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
18810Sstevel@tonic-gate 	dev_t			dev;
18820Sstevel@tonic-gate 	dev_info_t		*dip;
18830Sstevel@tonic-gate 	int			ret;
18840Sstevel@tonic-gate 	struct snode		*csp;
18850Sstevel@tonic-gate 
18860Sstevel@tonic-gate 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
18870Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
18880Sstevel@tonic-gate 
18890Sstevel@tonic-gate 	if ((prop_op != PROP_LEN) && (valuep == NULL))
18900Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
18910Sstevel@tonic-gate 
18920Sstevel@tonic-gate 	if (length == NULL)
18930Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
18940Sstevel@tonic-gate 
18950Sstevel@tonic-gate 	/*
18960Sstevel@tonic-gate 	 * try to find the associated dip,
18970Sstevel@tonic-gate 	 * this places a hold on the driver
18980Sstevel@tonic-gate 	 */
18990Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
19000Sstevel@tonic-gate 
19010Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
19020Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
19030Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
19040Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
19050Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
19060Sstevel@tonic-gate 	if (dip == NULL)
19070Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate 	if (dip == NULL)
19100Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
19110Sstevel@tonic-gate 
19120Sstevel@tonic-gate 	ret = i_ldi_prop_op(dev, dip, prop_op, flags, name, valuep, length);
19130Sstevel@tonic-gate 	ddi_release_devi(dip);
19140Sstevel@tonic-gate 
19150Sstevel@tonic-gate 	return (ret);
19160Sstevel@tonic-gate }
19170Sstevel@tonic-gate 
19180Sstevel@tonic-gate int
19190Sstevel@tonic-gate ldi_strategy(ldi_handle_t lh, struct buf *bp)
19200Sstevel@tonic-gate {
19210Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
19220Sstevel@tonic-gate 	dev_t			dev;
19230Sstevel@tonic-gate 
19240Sstevel@tonic-gate 	if ((lh == NULL) || (bp == NULL))
19250Sstevel@tonic-gate 		return (EINVAL);
19260Sstevel@tonic-gate 
19270Sstevel@tonic-gate 	/* this entry point is only supported for cb devices */
19280Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
19290Sstevel@tonic-gate 	if (!(handlep->lh_type & LH_CBDEV))
19300Sstevel@tonic-gate 		return (ENOTSUP);
19310Sstevel@tonic-gate 
19320Sstevel@tonic-gate 	bp->b_edev = dev;
19330Sstevel@tonic-gate 	bp->b_dev = cmpdev(dev);
19340Sstevel@tonic-gate 	return (bdev_strategy(bp));
19350Sstevel@tonic-gate }
19360Sstevel@tonic-gate 
19370Sstevel@tonic-gate int
19380Sstevel@tonic-gate ldi_dump(ldi_handle_t lh, caddr_t addr, daddr_t blkno, int nblk)
19390Sstevel@tonic-gate {
19400Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
19410Sstevel@tonic-gate 	dev_t			dev;
19420Sstevel@tonic-gate 
19430Sstevel@tonic-gate 	if (lh == NULL)
19440Sstevel@tonic-gate 		return (EINVAL);
19450Sstevel@tonic-gate 
19460Sstevel@tonic-gate 	/* this entry point is only supported for cb devices */
19470Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
19480Sstevel@tonic-gate 	if (!(handlep->lh_type & LH_CBDEV))
19490Sstevel@tonic-gate 		return (ENOTSUP);
19500Sstevel@tonic-gate 
19510Sstevel@tonic-gate 	return (bdev_dump(dev, addr, blkno, nblk));
19520Sstevel@tonic-gate }
19530Sstevel@tonic-gate 
19540Sstevel@tonic-gate int
19550Sstevel@tonic-gate ldi_devmap(ldi_handle_t lh, devmap_cookie_t dhp, offset_t off,
19560Sstevel@tonic-gate     size_t len, size_t *maplen, uint_t model)
19570Sstevel@tonic-gate {
19580Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
19590Sstevel@tonic-gate 	dev_t			dev;
19600Sstevel@tonic-gate 
19610Sstevel@tonic-gate 	if (lh == NULL)
19620Sstevel@tonic-gate 		return (EINVAL);
19630Sstevel@tonic-gate 
19640Sstevel@tonic-gate 	/* this entry point is only supported for cb devices */
19650Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
19660Sstevel@tonic-gate 	if (!(handlep->lh_type & LH_CBDEV))
19670Sstevel@tonic-gate 		return (ENOTSUP);
19680Sstevel@tonic-gate 
19690Sstevel@tonic-gate 	return (cdev_devmap(dev, dhp, off, len, maplen, model));
19700Sstevel@tonic-gate }
19710Sstevel@tonic-gate 
19720Sstevel@tonic-gate int
19730Sstevel@tonic-gate ldi_aread(ldi_handle_t lh, struct aio_req *aio_reqp, cred_t *cr)
19740Sstevel@tonic-gate {
19750Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
19760Sstevel@tonic-gate 	dev_t			dev;
19770Sstevel@tonic-gate 	struct cb_ops		*cb;
19780Sstevel@tonic-gate 
19790Sstevel@tonic-gate 	if (lh == NULL)
19800Sstevel@tonic-gate 		return (EINVAL);
19810Sstevel@tonic-gate 
19820Sstevel@tonic-gate 	/* this entry point is only supported for cb devices */
19830Sstevel@tonic-gate 	if (!(handlep->lh_type & LH_CBDEV))
19840Sstevel@tonic-gate 		return (ENOTSUP);
19850Sstevel@tonic-gate 
19860Sstevel@tonic-gate 	/*
19870Sstevel@tonic-gate 	 * Kaio is only supported on block devices.
19880Sstevel@tonic-gate 	 */
19890Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
19900Sstevel@tonic-gate 	cb = devopsp[getmajor(dev)]->devo_cb_ops;
19910Sstevel@tonic-gate 	if (cb->cb_strategy == nodev || cb->cb_strategy == NULL)
19920Sstevel@tonic-gate 		return (ENOTSUP);
19930Sstevel@tonic-gate 
19940Sstevel@tonic-gate 	if (cb->cb_aread == NULL)
19950Sstevel@tonic-gate 		return (ENOTSUP);
19960Sstevel@tonic-gate 
19970Sstevel@tonic-gate 	return (cb->cb_aread(dev, aio_reqp, cr));
19980Sstevel@tonic-gate }
19990Sstevel@tonic-gate 
20000Sstevel@tonic-gate int
20010Sstevel@tonic-gate ldi_awrite(ldi_handle_t lh, struct aio_req *aio_reqp, cred_t *cr)
20020Sstevel@tonic-gate {
20030Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
20040Sstevel@tonic-gate 	struct cb_ops		*cb;
20050Sstevel@tonic-gate 	dev_t			dev;
20060Sstevel@tonic-gate 
20070Sstevel@tonic-gate 	if (lh == NULL)
20080Sstevel@tonic-gate 		return (EINVAL);
20090Sstevel@tonic-gate 
20100Sstevel@tonic-gate 	/* this entry point is only supported for cb devices */
20110Sstevel@tonic-gate 	if (!(handlep->lh_type & LH_CBDEV))
20120Sstevel@tonic-gate 		return (ENOTSUP);
20130Sstevel@tonic-gate 
20140Sstevel@tonic-gate 	/*
20150Sstevel@tonic-gate 	 * Kaio is only supported on block devices.
20160Sstevel@tonic-gate 	 */
20170Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
20180Sstevel@tonic-gate 	cb = devopsp[getmajor(dev)]->devo_cb_ops;
20190Sstevel@tonic-gate 	if (cb->cb_strategy == nodev || cb->cb_strategy == NULL)
20200Sstevel@tonic-gate 		return (ENOTSUP);
20210Sstevel@tonic-gate 
20220Sstevel@tonic-gate 	if (cb->cb_awrite == NULL)
20230Sstevel@tonic-gate 		return (ENOTSUP);
20240Sstevel@tonic-gate 
20250Sstevel@tonic-gate 	return (cb->cb_awrite(dev, aio_reqp, cr));
20260Sstevel@tonic-gate }
20270Sstevel@tonic-gate 
20280Sstevel@tonic-gate int
20290Sstevel@tonic-gate ldi_putmsg(ldi_handle_t lh, mblk_t *smp)
20300Sstevel@tonic-gate {
20310Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
20320Sstevel@tonic-gate 	int			ret;
20330Sstevel@tonic-gate 
20340Sstevel@tonic-gate 	if ((lh == NULL) || (smp == NULL))
20350Sstevel@tonic-gate 		return (EINVAL);
20360Sstevel@tonic-gate 
20370Sstevel@tonic-gate 	if (!(handlep->lh_type & LH_STREAM)) {
20380Sstevel@tonic-gate 		freemsg(smp);
20390Sstevel@tonic-gate 		return (ENOTSUP);
20400Sstevel@tonic-gate 	}
20410Sstevel@tonic-gate 
20420Sstevel@tonic-gate 	/* Send message while honoring flow control */
20430Sstevel@tonic-gate 	ret = kstrputmsg(handlep->lh_vp, smp, NULL, 0, 0,
20440Sstevel@tonic-gate 				MSG_BAND | MSG_HOLDSIG | MSG_IGNERROR, 0);
20450Sstevel@tonic-gate 
20460Sstevel@tonic-gate 	return (ret);
20470Sstevel@tonic-gate }
20480Sstevel@tonic-gate 
20490Sstevel@tonic-gate int
20500Sstevel@tonic-gate ldi_getmsg(ldi_handle_t lh, mblk_t **rmp, timestruc_t *timeo)
20510Sstevel@tonic-gate {
20520Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
20530Sstevel@tonic-gate 	clock_t			timout; /* milliseconds */
20540Sstevel@tonic-gate 	uchar_t			pri;
20550Sstevel@tonic-gate 	rval_t			rval;
20560Sstevel@tonic-gate 	int			ret, pflag;
20570Sstevel@tonic-gate 
20580Sstevel@tonic-gate 
20590Sstevel@tonic-gate 	if (lh == NULL)
20600Sstevel@tonic-gate 		return (EINVAL);
20610Sstevel@tonic-gate 
20620Sstevel@tonic-gate 	if (!(handlep->lh_type & LH_STREAM))
20630Sstevel@tonic-gate 		return (ENOTSUP);
20640Sstevel@tonic-gate 
20650Sstevel@tonic-gate 	/* Convert from nanoseconds to milliseconds */
20660Sstevel@tonic-gate 	if (timeo != NULL) {
20670Sstevel@tonic-gate 		timout = timeo->tv_sec * 1000 + timeo->tv_nsec / 1000000;
20680Sstevel@tonic-gate 		if (timout > INT_MAX)
20690Sstevel@tonic-gate 			return (EINVAL);
20700Sstevel@tonic-gate 	} else
20710Sstevel@tonic-gate 		timout = -1;
20720Sstevel@tonic-gate 
20730Sstevel@tonic-gate 	/* Wait for timeout millseconds for a message */
20740Sstevel@tonic-gate 	pflag = MSG_ANY;
20750Sstevel@tonic-gate 	pri = 0;
20760Sstevel@tonic-gate 	*rmp = NULL;
20770Sstevel@tonic-gate 	ret = kstrgetmsg(handlep->lh_vp,
20780Sstevel@tonic-gate 				rmp, NULL, &pri, &pflag, timout, &rval);
20790Sstevel@tonic-gate 	return (ret);
20800Sstevel@tonic-gate }
20810Sstevel@tonic-gate 
20820Sstevel@tonic-gate int
20830Sstevel@tonic-gate ldi_get_dev(ldi_handle_t lh, dev_t *devp)
20840Sstevel@tonic-gate {
20850Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
20860Sstevel@tonic-gate 
20870Sstevel@tonic-gate 	if ((lh == NULL) || (devp == NULL))
20880Sstevel@tonic-gate 		return (EINVAL);
20890Sstevel@tonic-gate 
20900Sstevel@tonic-gate 	*devp = handlep->lh_vp->v_rdev;
20910Sstevel@tonic-gate 	return (0);
20920Sstevel@tonic-gate }
20930Sstevel@tonic-gate 
20940Sstevel@tonic-gate int
20950Sstevel@tonic-gate ldi_get_otyp(ldi_handle_t lh, int *otyp)
20960Sstevel@tonic-gate {
20970Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
20980Sstevel@tonic-gate 
20990Sstevel@tonic-gate 	if ((lh == NULL) || (otyp == NULL))
21000Sstevel@tonic-gate 		return (EINVAL);
21010Sstevel@tonic-gate 
21020Sstevel@tonic-gate 	*otyp = VTYP_TO_OTYP(handlep->lh_vp->v_type);
21030Sstevel@tonic-gate 	return (0);
21040Sstevel@tonic-gate }
21050Sstevel@tonic-gate 
21060Sstevel@tonic-gate int
21070Sstevel@tonic-gate ldi_get_devid(ldi_handle_t lh, ddi_devid_t *devid)
21080Sstevel@tonic-gate {
21090Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
21100Sstevel@tonic-gate 	int			ret;
21110Sstevel@tonic-gate 	dev_t			dev;
21120Sstevel@tonic-gate 
21130Sstevel@tonic-gate 	if ((lh == NULL) || (devid == NULL))
21140Sstevel@tonic-gate 		return (EINVAL);
21150Sstevel@tonic-gate 
21160Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
21170Sstevel@tonic-gate 
21180Sstevel@tonic-gate 	ret = ddi_lyr_get_devid(dev, devid);
21190Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
21200Sstevel@tonic-gate 		return (ENOTSUP);
21210Sstevel@tonic-gate 
21220Sstevel@tonic-gate 	return (0);
21230Sstevel@tonic-gate }
21240Sstevel@tonic-gate 
21250Sstevel@tonic-gate int
21260Sstevel@tonic-gate ldi_get_minor_name(ldi_handle_t lh, char **minor_name)
21270Sstevel@tonic-gate {
21280Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
21290Sstevel@tonic-gate 	int			ret, otyp;
21300Sstevel@tonic-gate 	dev_t			dev;
21310Sstevel@tonic-gate 
21320Sstevel@tonic-gate 	if ((lh == NULL) || (minor_name == NULL))
21330Sstevel@tonic-gate 		return (EINVAL);
21340Sstevel@tonic-gate 
21350Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
21360Sstevel@tonic-gate 	otyp = VTYP_TO_OTYP(handlep->lh_vp->v_type);
21370Sstevel@tonic-gate 
21380Sstevel@tonic-gate 	ret = ddi_lyr_get_minor_name(dev, OTYP_TO_STYP(otyp), minor_name);
21390Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
21400Sstevel@tonic-gate 		return (ENOTSUP);
21410Sstevel@tonic-gate 
21420Sstevel@tonic-gate 	return (0);
21430Sstevel@tonic-gate }
21440Sstevel@tonic-gate 
21450Sstevel@tonic-gate int
21460Sstevel@tonic-gate ldi_prop_lookup_int_array(ldi_handle_t lh,
21470Sstevel@tonic-gate     uint_t flags, char *name, int **data, uint_t *nelements)
21480Sstevel@tonic-gate {
21490Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
21500Sstevel@tonic-gate 	dev_info_t		*dip;
21510Sstevel@tonic-gate 	dev_t			dev;
21520Sstevel@tonic-gate 	int			res;
21530Sstevel@tonic-gate 	struct snode		*csp;
21540Sstevel@tonic-gate 
21550Sstevel@tonic-gate 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
21560Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
21570Sstevel@tonic-gate 
21580Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
21590Sstevel@tonic-gate 
21600Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
21610Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
21620Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
21630Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
21640Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
21650Sstevel@tonic-gate 	if (dip == NULL)
21660Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
21670Sstevel@tonic-gate 
21680Sstevel@tonic-gate 	if (dip == NULL) {
21690Sstevel@tonic-gate 		flags |= DDI_UNBND_DLPI2;
21700Sstevel@tonic-gate 	} else if (flags & LDI_DEV_T_ANY) {
21710Sstevel@tonic-gate 		flags &= ~LDI_DEV_T_ANY;
21720Sstevel@tonic-gate 		dev = DDI_DEV_T_ANY;
21730Sstevel@tonic-gate 	}
21740Sstevel@tonic-gate 
21750Sstevel@tonic-gate 	if (dip != NULL) {
21760Sstevel@tonic-gate 		int *prop_val, prop_len;
21770Sstevel@tonic-gate 
21780Sstevel@tonic-gate 		res = i_ldi_prop_op_typed(dev, dip, flags, name,
21790Sstevel@tonic-gate 		    (caddr_t *)&prop_val, &prop_len, sizeof (int));
21800Sstevel@tonic-gate 
21810Sstevel@tonic-gate 		/* if we got it then return it */
21820Sstevel@tonic-gate 		if (res == DDI_PROP_SUCCESS) {
21830Sstevel@tonic-gate 			*nelements = prop_len / sizeof (int);
21840Sstevel@tonic-gate 			*data = prop_val;
21850Sstevel@tonic-gate 
21860Sstevel@tonic-gate 			ddi_release_devi(dip);
21870Sstevel@tonic-gate 			return (res);
21880Sstevel@tonic-gate 		}
21890Sstevel@tonic-gate 	}
21900Sstevel@tonic-gate 
21910Sstevel@tonic-gate 	/* call the normal property interfaces */
21920Sstevel@tonic-gate 	res = ddi_prop_lookup_int_array(dev, dip, flags,
21930Sstevel@tonic-gate 	    name, data, nelements);
21940Sstevel@tonic-gate 
21950Sstevel@tonic-gate 	if (dip != NULL)
21960Sstevel@tonic-gate 		ddi_release_devi(dip);
21970Sstevel@tonic-gate 
21980Sstevel@tonic-gate 	return (res);
21990Sstevel@tonic-gate }
22000Sstevel@tonic-gate 
22010Sstevel@tonic-gate int
22020Sstevel@tonic-gate ldi_prop_lookup_int64_array(ldi_handle_t lh,
22030Sstevel@tonic-gate     uint_t flags, char *name, int64_t **data, uint_t *nelements)
22040Sstevel@tonic-gate {
22050Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
22060Sstevel@tonic-gate 	dev_info_t		*dip;
22070Sstevel@tonic-gate 	dev_t			dev;
22080Sstevel@tonic-gate 	int			res;
22090Sstevel@tonic-gate 	struct snode		*csp;
22100Sstevel@tonic-gate 
22110Sstevel@tonic-gate 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
22120Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
22130Sstevel@tonic-gate 
22140Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
22150Sstevel@tonic-gate 
22160Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
22170Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
22180Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
22190Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
22200Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
22210Sstevel@tonic-gate 	if (dip == NULL)
22220Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
22230Sstevel@tonic-gate 
22240Sstevel@tonic-gate 	if (dip == NULL) {
22250Sstevel@tonic-gate 		flags |= DDI_UNBND_DLPI2;
22260Sstevel@tonic-gate 	} else if (flags & LDI_DEV_T_ANY) {
22270Sstevel@tonic-gate 		flags &= ~LDI_DEV_T_ANY;
22280Sstevel@tonic-gate 		dev = DDI_DEV_T_ANY;
22290Sstevel@tonic-gate 	}
22300Sstevel@tonic-gate 
22310Sstevel@tonic-gate 	if (dip != NULL) {
22320Sstevel@tonic-gate 		int64_t	*prop_val;
22330Sstevel@tonic-gate 		int	prop_len;
22340Sstevel@tonic-gate 
22350Sstevel@tonic-gate 		res = i_ldi_prop_op_typed(dev, dip, flags, name,
22360Sstevel@tonic-gate 		    (caddr_t *)&prop_val, &prop_len, sizeof (int64_t));
22370Sstevel@tonic-gate 
22380Sstevel@tonic-gate 		/* if we got it then return it */
22390Sstevel@tonic-gate 		if (res == DDI_PROP_SUCCESS) {
22400Sstevel@tonic-gate 			*nelements = prop_len / sizeof (int64_t);
22410Sstevel@tonic-gate 			*data = prop_val;
22420Sstevel@tonic-gate 
22430Sstevel@tonic-gate 			ddi_release_devi(dip);
22440Sstevel@tonic-gate 			return (res);
22450Sstevel@tonic-gate 		}
22460Sstevel@tonic-gate 	}
22470Sstevel@tonic-gate 
22480Sstevel@tonic-gate 	/* call the normal property interfaces */
22490Sstevel@tonic-gate 	res = ddi_prop_lookup_int64_array(dev, dip, flags,
22500Sstevel@tonic-gate 	    name, data, nelements);
22510Sstevel@tonic-gate 
22520Sstevel@tonic-gate 	if (dip != NULL)
22530Sstevel@tonic-gate 		ddi_release_devi(dip);
22540Sstevel@tonic-gate 
22550Sstevel@tonic-gate 	return (res);
22560Sstevel@tonic-gate }
22570Sstevel@tonic-gate 
22580Sstevel@tonic-gate int
22590Sstevel@tonic-gate ldi_prop_lookup_string_array(ldi_handle_t lh,
22600Sstevel@tonic-gate     uint_t flags, char *name, char ***data, uint_t *nelements)
22610Sstevel@tonic-gate {
22620Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
22630Sstevel@tonic-gate 	dev_info_t		*dip;
22640Sstevel@tonic-gate 	dev_t			dev;
22650Sstevel@tonic-gate 	int			res;
22660Sstevel@tonic-gate 	struct snode		*csp;
22670Sstevel@tonic-gate 
22680Sstevel@tonic-gate 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
22690Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
22700Sstevel@tonic-gate 
22710Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
22720Sstevel@tonic-gate 
22730Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
22740Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
22750Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
22760Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
22770Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
22780Sstevel@tonic-gate 	if (dip == NULL)
22790Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
22800Sstevel@tonic-gate 
22810Sstevel@tonic-gate 	if (dip == NULL) {
22820Sstevel@tonic-gate 		flags |= DDI_UNBND_DLPI2;
22830Sstevel@tonic-gate 	} else if (flags & LDI_DEV_T_ANY) {
22840Sstevel@tonic-gate 		flags &= ~LDI_DEV_T_ANY;
22850Sstevel@tonic-gate 		dev = DDI_DEV_T_ANY;
22860Sstevel@tonic-gate 	}
22870Sstevel@tonic-gate 
22880Sstevel@tonic-gate 	if (dip != NULL) {
22890Sstevel@tonic-gate 		char	*prop_val;
22900Sstevel@tonic-gate 		int	prop_len;
22910Sstevel@tonic-gate 
22920Sstevel@tonic-gate 		res = i_ldi_prop_op_typed(dev, dip, flags, name,
22930Sstevel@tonic-gate 		    (caddr_t *)&prop_val, &prop_len, 0);
22940Sstevel@tonic-gate 
22950Sstevel@tonic-gate 		/* if we got it then return it */
22960Sstevel@tonic-gate 		if (res == DDI_PROP_SUCCESS) {
22970Sstevel@tonic-gate 			char	**str_array;
22980Sstevel@tonic-gate 			int	nelem;
22990Sstevel@tonic-gate 
23000Sstevel@tonic-gate 			/*
23010Sstevel@tonic-gate 			 * pack the returned string array into the format
23020Sstevel@tonic-gate 			 * our callers expect
23030Sstevel@tonic-gate 			 */
23040Sstevel@tonic-gate 			if (i_pack_string_array(prop_val, prop_len,
23050Sstevel@tonic-gate 				&str_array, &nelem) == 0) {
23060Sstevel@tonic-gate 
23070Sstevel@tonic-gate 				*data = str_array;
23080Sstevel@tonic-gate 				*nelements = nelem;
23090Sstevel@tonic-gate 
23100Sstevel@tonic-gate 				ddi_prop_free(prop_val);
23110Sstevel@tonic-gate 				ddi_release_devi(dip);
23120Sstevel@tonic-gate 				return (res);
23130Sstevel@tonic-gate 			}
23140Sstevel@tonic-gate 
23150Sstevel@tonic-gate 			/*
23160Sstevel@tonic-gate 			 * the format of the returned property must have
23170Sstevel@tonic-gate 			 * been bad so throw it out
23180Sstevel@tonic-gate 			 */
23190Sstevel@tonic-gate 			ddi_prop_free(prop_val);
23200Sstevel@tonic-gate 		}
23210Sstevel@tonic-gate 	}
23220Sstevel@tonic-gate 
23230Sstevel@tonic-gate 	/* call the normal property interfaces */
23240Sstevel@tonic-gate 	res = ddi_prop_lookup_string_array(dev, dip, flags,
23250Sstevel@tonic-gate 	    name, data, nelements);
23260Sstevel@tonic-gate 
23270Sstevel@tonic-gate 	if (dip != NULL)
23280Sstevel@tonic-gate 		ddi_release_devi(dip);
23290Sstevel@tonic-gate 
23300Sstevel@tonic-gate 	return (res);
23310Sstevel@tonic-gate }
23320Sstevel@tonic-gate 
23330Sstevel@tonic-gate int
23340Sstevel@tonic-gate ldi_prop_lookup_string(ldi_handle_t lh,
23350Sstevel@tonic-gate     uint_t flags, char *name, char **data)
23360Sstevel@tonic-gate {
23370Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
23380Sstevel@tonic-gate 	dev_info_t		*dip;
23390Sstevel@tonic-gate 	dev_t			dev;
23400Sstevel@tonic-gate 	int			res;
23410Sstevel@tonic-gate 	struct snode		*csp;
23420Sstevel@tonic-gate 
23430Sstevel@tonic-gate 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
23440Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
23450Sstevel@tonic-gate 
23460Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
23470Sstevel@tonic-gate 
23480Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
23490Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
23500Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
23510Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
23520Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
23530Sstevel@tonic-gate 	if (dip == NULL)
23540Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
23550Sstevel@tonic-gate 
23560Sstevel@tonic-gate 	if (dip == NULL) {
23570Sstevel@tonic-gate 		flags |= DDI_UNBND_DLPI2;
23580Sstevel@tonic-gate 	} else if (flags & LDI_DEV_T_ANY) {
23590Sstevel@tonic-gate 		flags &= ~LDI_DEV_T_ANY;
23600Sstevel@tonic-gate 		dev = DDI_DEV_T_ANY;
23610Sstevel@tonic-gate 	}
23620Sstevel@tonic-gate 
23630Sstevel@tonic-gate 	if (dip != NULL) {
23640Sstevel@tonic-gate 		char	*prop_val;
23650Sstevel@tonic-gate 		int	prop_len;
23660Sstevel@tonic-gate 
23670Sstevel@tonic-gate 		res = i_ldi_prop_op_typed(dev, dip, flags, name,
23680Sstevel@tonic-gate 		    (caddr_t *)&prop_val, &prop_len, 0);
23690Sstevel@tonic-gate 
23700Sstevel@tonic-gate 		/* if we got it then return it */
23710Sstevel@tonic-gate 		if (res == DDI_PROP_SUCCESS) {
23720Sstevel@tonic-gate 			/*
23730Sstevel@tonic-gate 			 * sanity check the vaule returned.
23740Sstevel@tonic-gate 			 */
23750Sstevel@tonic-gate 			if (i_check_string(prop_val, prop_len)) {
23760Sstevel@tonic-gate 				ddi_prop_free(prop_val);
23770Sstevel@tonic-gate 			} else {
23780Sstevel@tonic-gate 				*data = prop_val;
23790Sstevel@tonic-gate 				ddi_release_devi(dip);
23800Sstevel@tonic-gate 				return (res);
23810Sstevel@tonic-gate 			}
23820Sstevel@tonic-gate 		}
23830Sstevel@tonic-gate 	}
23840Sstevel@tonic-gate 
23850Sstevel@tonic-gate 	/* call the normal property interfaces */
23860Sstevel@tonic-gate 	res = ddi_prop_lookup_string(dev, dip, flags, name, data);
23870Sstevel@tonic-gate 
23880Sstevel@tonic-gate 	if (dip != NULL)
23890Sstevel@tonic-gate 		ddi_release_devi(dip);
23900Sstevel@tonic-gate 
23910Sstevel@tonic-gate #ifdef DEBUG
23920Sstevel@tonic-gate 	if (res == DDI_PROP_SUCCESS) {
23930Sstevel@tonic-gate 		/*
23940Sstevel@tonic-gate 		 * keep ourselves honest
23950Sstevel@tonic-gate 		 * make sure the framework returns strings in the
23960Sstevel@tonic-gate 		 * same format as we're demanding from drivers.
23970Sstevel@tonic-gate 		 */
23980Sstevel@tonic-gate 		struct prop_driver_data	*pdd;
23990Sstevel@tonic-gate 		int			pdd_prop_size;
24000Sstevel@tonic-gate 
24010Sstevel@tonic-gate 		pdd = ((struct prop_driver_data *)(*data)) - 1;
24020Sstevel@tonic-gate 		pdd_prop_size = pdd->pdd_size -
24030Sstevel@tonic-gate 		    sizeof (struct prop_driver_data);
24040Sstevel@tonic-gate 		ASSERT(i_check_string(*data, pdd_prop_size) == 0);
24050Sstevel@tonic-gate 	}
24060Sstevel@tonic-gate #endif /* DEBUG */
24070Sstevel@tonic-gate 
24080Sstevel@tonic-gate 	return (res);
24090Sstevel@tonic-gate }
24100Sstevel@tonic-gate 
24110Sstevel@tonic-gate int
24120Sstevel@tonic-gate ldi_prop_lookup_byte_array(ldi_handle_t lh,
24130Sstevel@tonic-gate     uint_t flags, char *name, uchar_t **data, uint_t *nelements)
24140Sstevel@tonic-gate {
24150Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
24160Sstevel@tonic-gate 	dev_info_t		*dip;
24170Sstevel@tonic-gate 	dev_t			dev;
24180Sstevel@tonic-gate 	int			res;
24190Sstevel@tonic-gate 	struct snode		*csp;
24200Sstevel@tonic-gate 
24210Sstevel@tonic-gate 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
24220Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
24230Sstevel@tonic-gate 
24240Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
24250Sstevel@tonic-gate 
24260Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
24270Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
24280Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
24290Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
24300Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
24310Sstevel@tonic-gate 	if (dip == NULL)
24320Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
24330Sstevel@tonic-gate 
24340Sstevel@tonic-gate 	if (dip == NULL) {
24350Sstevel@tonic-gate 		flags |= DDI_UNBND_DLPI2;
24360Sstevel@tonic-gate 	} else if (flags & LDI_DEV_T_ANY) {
24370Sstevel@tonic-gate 		flags &= ~LDI_DEV_T_ANY;
24380Sstevel@tonic-gate 		dev = DDI_DEV_T_ANY;
24390Sstevel@tonic-gate 	}
24400Sstevel@tonic-gate 
24410Sstevel@tonic-gate 	if (dip != NULL) {
24420Sstevel@tonic-gate 		uchar_t	*prop_val;
24430Sstevel@tonic-gate 		int	prop_len;
24440Sstevel@tonic-gate 
24450Sstevel@tonic-gate 		res = i_ldi_prop_op_typed(dev, dip, flags, name,
24460Sstevel@tonic-gate 		    (caddr_t *)&prop_val, &prop_len, sizeof (uchar_t));
24470Sstevel@tonic-gate 
24480Sstevel@tonic-gate 		/* if we got it then return it */
24490Sstevel@tonic-gate 		if (res == DDI_PROP_SUCCESS) {
24500Sstevel@tonic-gate 			*nelements = prop_len / sizeof (uchar_t);
24510Sstevel@tonic-gate 			*data = prop_val;
24520Sstevel@tonic-gate 
24530Sstevel@tonic-gate 			ddi_release_devi(dip);
24540Sstevel@tonic-gate 			return (res);
24550Sstevel@tonic-gate 		}
24560Sstevel@tonic-gate 	}
24570Sstevel@tonic-gate 
24580Sstevel@tonic-gate 	/* call the normal property interfaces */
24590Sstevel@tonic-gate 	res = ddi_prop_lookup_byte_array(dev, dip, flags,
24600Sstevel@tonic-gate 	    name, data, nelements);
24610Sstevel@tonic-gate 
24620Sstevel@tonic-gate 	if (dip != NULL)
24630Sstevel@tonic-gate 		ddi_release_devi(dip);
24640Sstevel@tonic-gate 
24650Sstevel@tonic-gate 	return (res);
24660Sstevel@tonic-gate }
24670Sstevel@tonic-gate 
24680Sstevel@tonic-gate int
24690Sstevel@tonic-gate ldi_prop_get_int(ldi_handle_t lh,
24700Sstevel@tonic-gate     uint_t flags, char *name, int defvalue)
24710Sstevel@tonic-gate {
24720Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
24730Sstevel@tonic-gate 	dev_info_t		*dip;
24740Sstevel@tonic-gate 	dev_t			dev;
24750Sstevel@tonic-gate 	int			res;
24760Sstevel@tonic-gate 	struct snode		*csp;
24770Sstevel@tonic-gate 
24780Sstevel@tonic-gate 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
24790Sstevel@tonic-gate 		return (defvalue);
24800Sstevel@tonic-gate 
24810Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
24820Sstevel@tonic-gate 
24830Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
24840Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
24850Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
24860Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
24870Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
24880Sstevel@tonic-gate 	if (dip == NULL)
24890Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
24900Sstevel@tonic-gate 
24910Sstevel@tonic-gate 	if (dip == NULL) {
24920Sstevel@tonic-gate 		flags |= DDI_UNBND_DLPI2;
24930Sstevel@tonic-gate 	} else if (flags & LDI_DEV_T_ANY) {
24940Sstevel@tonic-gate 		flags &= ~LDI_DEV_T_ANY;
24950Sstevel@tonic-gate 		dev = DDI_DEV_T_ANY;
24960Sstevel@tonic-gate 	}
24970Sstevel@tonic-gate 
24980Sstevel@tonic-gate 	if (dip != NULL) {
24990Sstevel@tonic-gate 		int	prop_val;
25000Sstevel@tonic-gate 		int	prop_len;
25010Sstevel@tonic-gate 
25020Sstevel@tonic-gate 		/*
25030Sstevel@tonic-gate 		 * first call the drivers prop_op interface to allow it
25040Sstevel@tonic-gate 		 * it to override default property values.
25050Sstevel@tonic-gate 		 */
25060Sstevel@tonic-gate 		prop_len = sizeof (int);
25070Sstevel@tonic-gate 		res = i_ldi_prop_op(dev, dip, PROP_LEN_AND_VAL_BUF,
25080Sstevel@tonic-gate 		    flags | DDI_PROP_DYNAMIC, name,
25090Sstevel@tonic-gate 		    (caddr_t)&prop_val, &prop_len);
25100Sstevel@tonic-gate 
25110Sstevel@tonic-gate 		/* if we got it then return it */
25120Sstevel@tonic-gate 		if ((res == DDI_PROP_SUCCESS) &&
25130Sstevel@tonic-gate 		    (prop_len == sizeof (int))) {
25140Sstevel@tonic-gate 			res = prop_val;
25150Sstevel@tonic-gate 			ddi_release_devi(dip);
25160Sstevel@tonic-gate 			return (res);
25170Sstevel@tonic-gate 		}
25180Sstevel@tonic-gate 	}
25190Sstevel@tonic-gate 
25200Sstevel@tonic-gate 	/* call the normal property interfaces */
25210Sstevel@tonic-gate 	res = ddi_prop_get_int(dev, dip, flags, name, defvalue);
25220Sstevel@tonic-gate 
25230Sstevel@tonic-gate 	if (dip != NULL)
25240Sstevel@tonic-gate 		ddi_release_devi(dip);
25250Sstevel@tonic-gate 
25260Sstevel@tonic-gate 	return (res);
25270Sstevel@tonic-gate }
25280Sstevel@tonic-gate 
25290Sstevel@tonic-gate int64_t
25300Sstevel@tonic-gate ldi_prop_get_int64(ldi_handle_t lh,
25310Sstevel@tonic-gate     uint_t flags, char *name, int64_t defvalue)
25320Sstevel@tonic-gate {
25330Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
25340Sstevel@tonic-gate 	dev_info_t		*dip;
25350Sstevel@tonic-gate 	dev_t			dev;
25360Sstevel@tonic-gate 	int64_t			res;
25370Sstevel@tonic-gate 	struct snode		*csp;
25380Sstevel@tonic-gate 
25390Sstevel@tonic-gate 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
25400Sstevel@tonic-gate 		return (defvalue);
25410Sstevel@tonic-gate 
25420Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
25430Sstevel@tonic-gate 
25440Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
25450Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
25460Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
25470Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
25480Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
25490Sstevel@tonic-gate 	if (dip == NULL)
25500Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
25510Sstevel@tonic-gate 
25520Sstevel@tonic-gate 	if (dip == NULL) {
25530Sstevel@tonic-gate 		flags |= DDI_UNBND_DLPI2;
25540Sstevel@tonic-gate 	} else if (flags & LDI_DEV_T_ANY) {
25550Sstevel@tonic-gate 		flags &= ~LDI_DEV_T_ANY;
25560Sstevel@tonic-gate 		dev = DDI_DEV_T_ANY;
25570Sstevel@tonic-gate 	}
25580Sstevel@tonic-gate 
25590Sstevel@tonic-gate 	if (dip != NULL) {
25600Sstevel@tonic-gate 		int64_t	prop_val;
25610Sstevel@tonic-gate 		int	prop_len;
25620Sstevel@tonic-gate 
25630Sstevel@tonic-gate 		/*
25640Sstevel@tonic-gate 		 * first call the drivers prop_op interface to allow it
25650Sstevel@tonic-gate 		 * it to override default property values.
25660Sstevel@tonic-gate 		 */
25670Sstevel@tonic-gate 		prop_len = sizeof (int64_t);
25680Sstevel@tonic-gate 		res = i_ldi_prop_op(dev, dip, PROP_LEN_AND_VAL_BUF,
25690Sstevel@tonic-gate 		    flags | DDI_PROP_DYNAMIC, name,
25700Sstevel@tonic-gate 		    (caddr_t)&prop_val, &prop_len);
25710Sstevel@tonic-gate 
25720Sstevel@tonic-gate 		/* if we got it then return it */
25730Sstevel@tonic-gate 		if ((res == DDI_PROP_SUCCESS) &&
25740Sstevel@tonic-gate 		    (prop_len == sizeof (int64_t))) {
25750Sstevel@tonic-gate 			res = prop_val;
25760Sstevel@tonic-gate 			ddi_release_devi(dip);
25770Sstevel@tonic-gate 			return (res);
25780Sstevel@tonic-gate 		}
25790Sstevel@tonic-gate 	}
25800Sstevel@tonic-gate 
25810Sstevel@tonic-gate 	/* call the normal property interfaces */
25820Sstevel@tonic-gate 	res = ddi_prop_get_int64(dev, dip, flags, name, defvalue);
25830Sstevel@tonic-gate 
25840Sstevel@tonic-gate 	if (dip != NULL)
25850Sstevel@tonic-gate 		ddi_release_devi(dip);
25860Sstevel@tonic-gate 
25870Sstevel@tonic-gate 	return (res);
25880Sstevel@tonic-gate }
25890Sstevel@tonic-gate 
25900Sstevel@tonic-gate int
25910Sstevel@tonic-gate ldi_prop_exists(ldi_handle_t lh, uint_t flags, char *name)
25920Sstevel@tonic-gate {
25930Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
25940Sstevel@tonic-gate 	dev_info_t 		*dip;
25950Sstevel@tonic-gate 	dev_t 			dev;
25960Sstevel@tonic-gate 	int			res, prop_len;
25970Sstevel@tonic-gate 	struct snode		*csp;
25980Sstevel@tonic-gate 
25990Sstevel@tonic-gate 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
26000Sstevel@tonic-gate 		return (0);
26010Sstevel@tonic-gate 
26020Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
26030Sstevel@tonic-gate 
26040Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
26050Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
26060Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
26070Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
26080Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
26090Sstevel@tonic-gate 	if (dip == NULL)
26100Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
26110Sstevel@tonic-gate 
26120Sstevel@tonic-gate 	/* if NULL dip, prop does NOT exist */
26130Sstevel@tonic-gate 	if (dip == NULL)
26140Sstevel@tonic-gate 		return (0);
26150Sstevel@tonic-gate 
26160Sstevel@tonic-gate 	if (flags & LDI_DEV_T_ANY) {
26170Sstevel@tonic-gate 		flags &= ~LDI_DEV_T_ANY;
26180Sstevel@tonic-gate 		dev = DDI_DEV_T_ANY;
26190Sstevel@tonic-gate 	}
26200Sstevel@tonic-gate 
26210Sstevel@tonic-gate 	/*
26220Sstevel@tonic-gate 	 * first call the drivers prop_op interface to allow it
26230Sstevel@tonic-gate 	 * it to override default property values.
26240Sstevel@tonic-gate 	 */
26250Sstevel@tonic-gate 	res = i_ldi_prop_op(dev, dip, PROP_LEN,
26260Sstevel@tonic-gate 	    flags | DDI_PROP_DYNAMIC, name, NULL, &prop_len);
26270Sstevel@tonic-gate 
26280Sstevel@tonic-gate 	if (res == DDI_PROP_SUCCESS) {
26290Sstevel@tonic-gate 		ddi_release_devi(dip);
26300Sstevel@tonic-gate 		return (1);
26310Sstevel@tonic-gate 	}
26320Sstevel@tonic-gate 
26330Sstevel@tonic-gate 	/* call the normal property interfaces */
26340Sstevel@tonic-gate 	res = ddi_prop_exists(dev, dip, flags, name);
26350Sstevel@tonic-gate 
26360Sstevel@tonic-gate 	ddi_release_devi(dip);
26370Sstevel@tonic-gate 	return (res);
26380Sstevel@tonic-gate }
26390Sstevel@tonic-gate 
26400Sstevel@tonic-gate int
26410Sstevel@tonic-gate ldi_get_eventcookie(ldi_handle_t lh, char *name, ddi_eventcookie_t *ecp)
26420Sstevel@tonic-gate {
26430Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
26440Sstevel@tonic-gate 	dev_info_t		*dip;
26450Sstevel@tonic-gate 	dev_t			dev;
26460Sstevel@tonic-gate 	int			res;
26470Sstevel@tonic-gate 	struct snode		*csp;
26480Sstevel@tonic-gate 
26490Sstevel@tonic-gate 	if ((lh == NULL) || (name == NULL) ||
26500Sstevel@tonic-gate 	    (strlen(name) == 0) || (ecp == NULL)) {
26510Sstevel@tonic-gate 		return (DDI_FAILURE);
26520Sstevel@tonic-gate 	}
26530Sstevel@tonic-gate 
26540Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
26550Sstevel@tonic-gate 
26560Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
26570Sstevel@tonic-gate 
26580Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
26590Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
26600Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
26610Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
26620Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
26630Sstevel@tonic-gate 	if (dip == NULL)
26640Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
26650Sstevel@tonic-gate 
26660Sstevel@tonic-gate 	if (dip == NULL)
26670Sstevel@tonic-gate 		return (DDI_FAILURE);
26680Sstevel@tonic-gate 
26690Sstevel@tonic-gate 	LDI_EVENTCB((CE_NOTE, "%s: event_name=%s, "
26700Sstevel@tonic-gate 	    "dip=0x%p, event_cookiep=0x%p", "ldi_get_eventcookie",
26710Sstevel@tonic-gate 	    name, (void *)dip, (void *)ecp));
26720Sstevel@tonic-gate 
26730Sstevel@tonic-gate 	res = ddi_get_eventcookie(dip, name, ecp);
26740Sstevel@tonic-gate 
26750Sstevel@tonic-gate 	ddi_release_devi(dip);
26760Sstevel@tonic-gate 	return (res);
26770Sstevel@tonic-gate }
26780Sstevel@tonic-gate 
26790Sstevel@tonic-gate int
26800Sstevel@tonic-gate ldi_add_event_handler(ldi_handle_t lh, ddi_eventcookie_t ec,
26810Sstevel@tonic-gate     void (*handler)(ldi_handle_t, ddi_eventcookie_t, void *, void *),
26820Sstevel@tonic-gate     void *arg, ldi_callback_id_t *id)
26830Sstevel@tonic-gate {
26840Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
26850Sstevel@tonic-gate 	struct ldi_event	*lep;
26860Sstevel@tonic-gate 	dev_info_t		*dip;
26870Sstevel@tonic-gate 	dev_t			dev;
26880Sstevel@tonic-gate 	int			res;
26890Sstevel@tonic-gate 	struct snode		*csp;
26900Sstevel@tonic-gate 
26910Sstevel@tonic-gate 	if ((lh == NULL) || (ec == NULL) || (handler == NULL) || (id == NULL))
26920Sstevel@tonic-gate 		return (DDI_FAILURE);
26930Sstevel@tonic-gate 
26940Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
26950Sstevel@tonic-gate 
26960Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
26970Sstevel@tonic-gate 
26980Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
26990Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
27000Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
27010Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
27020Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
27030Sstevel@tonic-gate 	if (dip == NULL)
27040Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
27050Sstevel@tonic-gate 
27060Sstevel@tonic-gate 	if (dip == NULL)
27070Sstevel@tonic-gate 		return (DDI_FAILURE);
27080Sstevel@tonic-gate 
27090Sstevel@tonic-gate 	lep = kmem_zalloc(sizeof (struct ldi_event), KM_SLEEP);
27100Sstevel@tonic-gate 	lep->le_lhp = handlep;
27110Sstevel@tonic-gate 	lep->le_arg = arg;
27120Sstevel@tonic-gate 	lep->le_handler = handler;
27130Sstevel@tonic-gate 
27140Sstevel@tonic-gate 	if ((res = ddi_add_event_handler(dip, ec, i_ldi_callback,
27150Sstevel@tonic-gate 	    (void *)lep, &lep->le_id)) != DDI_SUCCESS) {
27160Sstevel@tonic-gate 		LDI_EVENTCB((CE_WARN, "%s: unable to add"
27170Sstevel@tonic-gate 		    "event callback", "ldi_add_event_handler"));
27180Sstevel@tonic-gate 		ddi_release_devi(dip);
27190Sstevel@tonic-gate 		kmem_free(lep, sizeof (struct ldi_event));
27200Sstevel@tonic-gate 		return (res);
27210Sstevel@tonic-gate 	}
27220Sstevel@tonic-gate 
27230Sstevel@tonic-gate 	*id = (ldi_callback_id_t)lep;
27240Sstevel@tonic-gate 
27250Sstevel@tonic-gate 	LDI_EVENTCB((CE_NOTE, "%s: dip=0x%p, event=0x%p, "
27260Sstevel@tonic-gate 	    "ldi_eventp=0x%p, cb_id=0x%p", "ldi_add_event_handler",
27270Sstevel@tonic-gate 	    (void *)dip, (void *)ec, (void *)lep, (void *)id));
27280Sstevel@tonic-gate 
27290Sstevel@tonic-gate 	handle_event_add(lep);
27300Sstevel@tonic-gate 	ddi_release_devi(dip);
27310Sstevel@tonic-gate 	return (res);
27320Sstevel@tonic-gate }
27330Sstevel@tonic-gate 
27340Sstevel@tonic-gate int
27350Sstevel@tonic-gate ldi_remove_event_handler(ldi_handle_t lh, ldi_callback_id_t id)
27360Sstevel@tonic-gate {
27370Sstevel@tonic-gate 	ldi_event_t		*lep = (ldi_event_t *)id;
27380Sstevel@tonic-gate 	int			res;
27390Sstevel@tonic-gate 
27400Sstevel@tonic-gate 	if ((lh == NULL) || (id == NULL))
27410Sstevel@tonic-gate 		return (DDI_FAILURE);
27420Sstevel@tonic-gate 
27430Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
27440Sstevel@tonic-gate 
27450Sstevel@tonic-gate 	if ((res = ddi_remove_event_handler(lep->le_id))
27460Sstevel@tonic-gate 	    != DDI_SUCCESS) {
27470Sstevel@tonic-gate 		LDI_EVENTCB((CE_WARN, "%s: unable to remove "
27480Sstevel@tonic-gate 		    "event callback", "ldi_remove_event_handler"));
27490Sstevel@tonic-gate 		return (res);
27500Sstevel@tonic-gate 	}
27510Sstevel@tonic-gate 
27520Sstevel@tonic-gate 	handle_event_remove(lep);
27530Sstevel@tonic-gate 	kmem_free(lep, sizeof (struct ldi_event));
27540Sstevel@tonic-gate 	return (res);
27550Sstevel@tonic-gate }
2756