xref: /onnv-gate/usr/src/uts/common/os/driver_lyr.c (revision 8778:b4169d2ab299)
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
54582Scth  * Common Development and Distribution License (the "License").
64582Scth  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*8778SErik.Nordmark@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * Layered driver support.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/atomic.h>
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/t_lock.h>
330Sstevel@tonic-gate #include <sys/param.h>
340Sstevel@tonic-gate #include <sys/conf.h>
350Sstevel@tonic-gate #include <sys/systm.h>
360Sstevel@tonic-gate #include <sys/sysmacros.h>
370Sstevel@tonic-gate #include <sys/buf.h>
380Sstevel@tonic-gate #include <sys/cred.h>
390Sstevel@tonic-gate #include <sys/uio.h>
400Sstevel@tonic-gate #include <sys/vnode.h>
410Sstevel@tonic-gate #include <sys/fs/snode.h>
420Sstevel@tonic-gate #include <sys/open.h>
430Sstevel@tonic-gate #include <sys/kmem.h>
440Sstevel@tonic-gate #include <sys/file.h>
450Sstevel@tonic-gate #include <sys/bootconf.h>
460Sstevel@tonic-gate #include <sys/pathname.h>
470Sstevel@tonic-gate #include <sys/bitmap.h>
480Sstevel@tonic-gate #include <sys/stat.h>
490Sstevel@tonic-gate #include <sys/dditypes.h>
500Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
510Sstevel@tonic-gate #include <sys/ddi.h>
520Sstevel@tonic-gate #include <sys/sunddi.h>
530Sstevel@tonic-gate #include <sys/sunndi.h>
540Sstevel@tonic-gate #include <sys/esunddi.h>
550Sstevel@tonic-gate #include <sys/autoconf.h>
560Sstevel@tonic-gate #include <sys/sunldi.h>
570Sstevel@tonic-gate #include <sys/sunldi_impl.h>
580Sstevel@tonic-gate #include <sys/errno.h>
590Sstevel@tonic-gate #include <sys/debug.h>
600Sstevel@tonic-gate #include <sys/modctl.h>
610Sstevel@tonic-gate #include <sys/var.h>
620Sstevel@tonic-gate #include <vm/seg_vn.h>
630Sstevel@tonic-gate 
640Sstevel@tonic-gate #include <sys/stropts.h>
650Sstevel@tonic-gate #include <sys/strsubr.h>
660Sstevel@tonic-gate #include <sys/socket.h>
670Sstevel@tonic-gate #include <sys/socketvar.h>
680Sstevel@tonic-gate #include <sys/kstr.h>
690Sstevel@tonic-gate 
704845Svikram /*
714845Svikram  * Device contract related
724845Svikram  */
734845Svikram #include <sys/contract_impl.h>
744845Svikram #include <sys/contract/device_impl.h>
750Sstevel@tonic-gate 
760Sstevel@tonic-gate /*
770Sstevel@tonic-gate  * Define macros to manipulate snode, vnode, and open device flags
780Sstevel@tonic-gate  */
790Sstevel@tonic-gate #define	VTYP_VALID(i)	(((i) == VCHR) || ((i) == VBLK))
800Sstevel@tonic-gate #define	VTYP_TO_OTYP(i)	(((i) == VCHR) ? OTYP_CHR : OTYP_BLK)
810Sstevel@tonic-gate #define	VTYP_TO_STYP(i)	(((i) == VCHR) ? S_IFCHR : S_IFBLK)
820Sstevel@tonic-gate 
830Sstevel@tonic-gate #define	OTYP_VALID(i)	(((i) == OTYP_CHR) || ((i) == OTYP_BLK))
840Sstevel@tonic-gate #define	OTYP_TO_VTYP(i)	(((i) == OTYP_CHR) ? VCHR : VBLK)
850Sstevel@tonic-gate #define	OTYP_TO_STYP(i)	(((i) == OTYP_CHR) ? S_IFCHR : S_IFBLK)
860Sstevel@tonic-gate 
870Sstevel@tonic-gate #define	STYP_VALID(i)	(((i) == S_IFCHR) || ((i) == S_IFBLK))
880Sstevel@tonic-gate #define	STYP_TO_VTYP(i)	(((i) == S_IFCHR) ? VCHR : VBLK)
890Sstevel@tonic-gate 
900Sstevel@tonic-gate /*
910Sstevel@tonic-gate  * Define macros for accessing layered driver hash structures
920Sstevel@tonic-gate  */
930Sstevel@tonic-gate #define	LH_HASH(vp)		(handle_hash_func(vp) % LH_HASH_SZ)
940Sstevel@tonic-gate #define	LI_HASH(mid, dip, dev)	(ident_hash_func(mid, dip, dev) % LI_HASH_SZ)
950Sstevel@tonic-gate 
960Sstevel@tonic-gate /*
970Sstevel@tonic-gate  * Define layered handle flags used in the lh_type field
980Sstevel@tonic-gate  */
990Sstevel@tonic-gate #define	LH_STREAM	(0x1)	/* handle to a streams device */
1000Sstevel@tonic-gate #define	LH_CBDEV	(0x2)	/* handle to a char/block device */
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate /*
1034845Svikram  * Define macro for devid property lookups
1040Sstevel@tonic-gate  */
1050Sstevel@tonic-gate #define	DEVID_PROP_FLAGS	(DDI_PROP_DONTPASS | \
1060Sstevel@tonic-gate 				DDI_PROP_TYPE_STRING|DDI_PROP_CANSLEEP)
1070Sstevel@tonic-gate 
1084845Svikram /*
1094845Svikram  * Dummy string for NDI events
1104845Svikram  */
1114845Svikram #define	NDI_EVENT_SERVICE	"NDI_EVENT_SERVICE"
1124845Svikram 
1134845Svikram static void ldi_ev_lock(void);
1144845Svikram static void ldi_ev_unlock(void);
1154845Svikram 
1164845Svikram #ifdef	LDI_OBSOLETE_EVENT
1174845Svikram int ldi_remove_event_handler(ldi_handle_t lh, ldi_callback_id_t id);
1184845Svikram #endif
1194845Svikram 
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate /*
1220Sstevel@tonic-gate  * globals
1230Sstevel@tonic-gate  */
1240Sstevel@tonic-gate static kmutex_t			ldi_ident_hash_lock[LI_HASH_SZ];
1250Sstevel@tonic-gate static struct ldi_ident		*ldi_ident_hash[LI_HASH_SZ];
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate static kmutex_t			ldi_handle_hash_lock[LH_HASH_SZ];
1280Sstevel@tonic-gate static struct ldi_handle	*ldi_handle_hash[LH_HASH_SZ];
1290Sstevel@tonic-gate static size_t			ldi_handle_hash_count;
1300Sstevel@tonic-gate 
1314845Svikram static struct ldi_ev_callback_list ldi_ev_callback_list;
1324845Svikram 
1334845Svikram static uint32_t ldi_ev_id_pool = 0;
1344845Svikram 
1354845Svikram struct ldi_ev_cookie {
1364845Svikram 	char *ck_evname;
1374845Svikram 	uint_t ck_sync;
1384845Svikram 	uint_t ck_ctype;
1394845Svikram };
1404845Svikram 
1414845Svikram static struct ldi_ev_cookie ldi_ev_cookies[] = {
1424845Svikram 	{ LDI_EV_OFFLINE, 1, CT_DEV_EV_OFFLINE},
1434845Svikram 	{ LDI_EV_DEGRADE, 0, CT_DEV_EV_DEGRADED},
1444845Svikram 	{ NULL}			/* must terminate list */
1454845Svikram };
1464845Svikram 
1470Sstevel@tonic-gate void
1480Sstevel@tonic-gate ldi_init(void)
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate 	int i;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	ldi_handle_hash_count = 0;
1530Sstevel@tonic-gate 	for (i = 0; i < LH_HASH_SZ; i++) {
1540Sstevel@tonic-gate 		mutex_init(&ldi_handle_hash_lock[i], NULL, MUTEX_DEFAULT, NULL);
1550Sstevel@tonic-gate 		ldi_handle_hash[i] = NULL;
1560Sstevel@tonic-gate 	}
1570Sstevel@tonic-gate 	for (i = 0; i < LI_HASH_SZ; i++) {
1580Sstevel@tonic-gate 		mutex_init(&ldi_ident_hash_lock[i], NULL, MUTEX_DEFAULT, NULL);
1590Sstevel@tonic-gate 		ldi_ident_hash[i] = NULL;
1600Sstevel@tonic-gate 	}
1614845Svikram 
1624845Svikram 	/*
1634845Svikram 	 * Initialize the LDI event subsystem
1644845Svikram 	 */
1654845Svikram 	mutex_init(&ldi_ev_callback_list.le_lock, NULL, MUTEX_DEFAULT, NULL);
1664845Svikram 	cv_init(&ldi_ev_callback_list.le_cv, NULL, CV_DEFAULT, NULL);
1674845Svikram 	ldi_ev_callback_list.le_busy = 0;
1684845Svikram 	ldi_ev_callback_list.le_thread = NULL;
1694845Svikram 	list_create(&ldi_ev_callback_list.le_head,
1704845Svikram 	    sizeof (ldi_ev_callback_impl_t),
1714845Svikram 	    offsetof(ldi_ev_callback_impl_t, lec_list));
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate /*
1750Sstevel@tonic-gate  * LDI ident manipulation functions
1760Sstevel@tonic-gate  */
1770Sstevel@tonic-gate static uint_t
1780Sstevel@tonic-gate ident_hash_func(modid_t modid, dev_info_t *dip, dev_t dev)
1790Sstevel@tonic-gate {
1800Sstevel@tonic-gate 	if (dip != NULL) {
1810Sstevel@tonic-gate 		uintptr_t k = (uintptr_t)dip;
1820Sstevel@tonic-gate 		k >>= (int)highbit(sizeof (struct dev_info));
1830Sstevel@tonic-gate 		return ((uint_t)k);
1840Sstevel@tonic-gate 	} else if (dev != DDI_DEV_T_NONE) {
1850Sstevel@tonic-gate 		return (modid + getminor(dev) + getmajor(dev));
1860Sstevel@tonic-gate 	} else {
1870Sstevel@tonic-gate 		return (modid);
1880Sstevel@tonic-gate 	}
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate static struct ldi_ident **
1920Sstevel@tonic-gate ident_find_ref_nolock(modid_t modid, dev_info_t *dip, dev_t dev, major_t major)
1930Sstevel@tonic-gate {
1940Sstevel@tonic-gate 	struct ldi_ident	**lipp = NULL;
1950Sstevel@tonic-gate 	uint_t			index = LI_HASH(modid, dip, dev);
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ldi_ident_hash_lock[index]));
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	for (lipp = &(ldi_ident_hash[index]);
2000Sstevel@tonic-gate 	    (*lipp != NULL);
2010Sstevel@tonic-gate 	    lipp = &((*lipp)->li_next)) {
2020Sstevel@tonic-gate 		if (((*lipp)->li_modid == modid) &&
2030Sstevel@tonic-gate 		    ((*lipp)->li_major == major) &&
2040Sstevel@tonic-gate 		    ((*lipp)->li_dip == dip) &&
2050Sstevel@tonic-gate 		    ((*lipp)->li_dev == dev))
2060Sstevel@tonic-gate 			break;
2070Sstevel@tonic-gate 	}
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	ASSERT(lipp != NULL);
2100Sstevel@tonic-gate 	return (lipp);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate static struct ldi_ident *
2140Sstevel@tonic-gate ident_alloc(char *mod_name, dev_info_t *dip, dev_t dev, major_t major)
2150Sstevel@tonic-gate {
2160Sstevel@tonic-gate 	struct ldi_ident	*lip, **lipp;
2170Sstevel@tonic-gate 	modid_t			modid;
2180Sstevel@tonic-gate 	uint_t			index;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	ASSERT(mod_name != NULL);
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	/* get the module id */
2230Sstevel@tonic-gate 	modid = mod_name_to_modid(mod_name);
2240Sstevel@tonic-gate 	ASSERT(modid != -1);
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	/* allocate a new ident in case we need it */
2270Sstevel@tonic-gate 	lip = kmem_zalloc(sizeof (*lip), KM_SLEEP);
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	/* search the hash for a matching ident */
2300Sstevel@tonic-gate 	index = LI_HASH(modid, dip, dev);
2310Sstevel@tonic-gate 	mutex_enter(&ldi_ident_hash_lock[index]);
2320Sstevel@tonic-gate 	lipp = ident_find_ref_nolock(modid, dip, dev, major);
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	if (*lipp != NULL) {
2350Sstevel@tonic-gate 		/* we found an indent in the hash */
2360Sstevel@tonic-gate 		ASSERT(strcmp((*lipp)->li_modname, mod_name) == 0);
2370Sstevel@tonic-gate 		(*lipp)->li_ref++;
2380Sstevel@tonic-gate 		mutex_exit(&ldi_ident_hash_lock[index]);
2390Sstevel@tonic-gate 		kmem_free(lip, sizeof (struct ldi_ident));
2400Sstevel@tonic-gate 		return (*lipp);
2410Sstevel@tonic-gate 	}
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	/* initialize the new ident */
2440Sstevel@tonic-gate 	lip->li_next = NULL;
2450Sstevel@tonic-gate 	lip->li_ref = 1;
2460Sstevel@tonic-gate 	lip->li_modid = modid;
2470Sstevel@tonic-gate 	lip->li_major = major;
2480Sstevel@tonic-gate 	lip->li_dip = dip;
2490Sstevel@tonic-gate 	lip->li_dev = dev;
2500Sstevel@tonic-gate 	(void) strncpy(lip->li_modname, mod_name, sizeof (lip->li_modname) - 1);
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	/* add it to the ident hash */
2530Sstevel@tonic-gate 	lip->li_next = ldi_ident_hash[index];
2540Sstevel@tonic-gate 	ldi_ident_hash[index] = lip;
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	mutex_exit(&ldi_ident_hash_lock[index]);
2570Sstevel@tonic-gate 	return (lip);
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate static void
2610Sstevel@tonic-gate ident_hold(struct ldi_ident *lip)
2620Sstevel@tonic-gate {
2630Sstevel@tonic-gate 	uint_t			index;
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	ASSERT(lip != NULL);
2660Sstevel@tonic-gate 	index = LI_HASH(lip->li_modid, lip->li_dip, lip->li_dev);
2670Sstevel@tonic-gate 	mutex_enter(&ldi_ident_hash_lock[index]);
2680Sstevel@tonic-gate 	ASSERT(lip->li_ref > 0);
2690Sstevel@tonic-gate 	lip->li_ref++;
2700Sstevel@tonic-gate 	mutex_exit(&ldi_ident_hash_lock[index]);
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate static void
2740Sstevel@tonic-gate ident_release(struct ldi_ident *lip)
2750Sstevel@tonic-gate {
2760Sstevel@tonic-gate 	struct ldi_ident	**lipp;
2770Sstevel@tonic-gate 	uint_t			index;
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	ASSERT(lip != NULL);
2800Sstevel@tonic-gate 	index = LI_HASH(lip->li_modid, lip->li_dip, lip->li_dev);
2810Sstevel@tonic-gate 	mutex_enter(&ldi_ident_hash_lock[index]);
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	ASSERT(lip->li_ref > 0);
2840Sstevel@tonic-gate 	if (--lip->li_ref > 0) {
2850Sstevel@tonic-gate 		/* there are more references to this ident */
2860Sstevel@tonic-gate 		mutex_exit(&ldi_ident_hash_lock[index]);
2870Sstevel@tonic-gate 		return;
2880Sstevel@tonic-gate 	}
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	/* this was the last reference/open for this ident.  free it. */
2910Sstevel@tonic-gate 	lipp = ident_find_ref_nolock(
2920Sstevel@tonic-gate 	    lip->li_modid, lip->li_dip, lip->li_dev, lip->li_major);
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	ASSERT((lipp != NULL) && (*lipp != NULL));
2950Sstevel@tonic-gate 	*lipp = lip->li_next;
2960Sstevel@tonic-gate 	mutex_exit(&ldi_ident_hash_lock[index]);
2970Sstevel@tonic-gate 	kmem_free(lip, sizeof (struct ldi_ident));
2980Sstevel@tonic-gate }
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate /*
3010Sstevel@tonic-gate  * LDI handle manipulation functions
3020Sstevel@tonic-gate  */
3030Sstevel@tonic-gate static uint_t
3040Sstevel@tonic-gate handle_hash_func(void *vp)
3050Sstevel@tonic-gate {
3060Sstevel@tonic-gate 	uintptr_t k = (uintptr_t)vp;
3070Sstevel@tonic-gate 	k >>= (int)highbit(sizeof (vnode_t));
3080Sstevel@tonic-gate 	return ((uint_t)k);
3090Sstevel@tonic-gate }
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate static struct ldi_handle **
3120Sstevel@tonic-gate handle_find_ref_nolock(vnode_t *vp, struct ldi_ident *ident)
3130Sstevel@tonic-gate {
3140Sstevel@tonic-gate 	struct ldi_handle	**lhpp = NULL;
3150Sstevel@tonic-gate 	uint_t			index = LH_HASH(vp);
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ldi_handle_hash_lock[index]));
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	for (lhpp = &(ldi_handle_hash[index]);
3200Sstevel@tonic-gate 	    (*lhpp != NULL);
3210Sstevel@tonic-gate 	    lhpp = &((*lhpp)->lh_next)) {
3220Sstevel@tonic-gate 		if (((*lhpp)->lh_ident == ident) &&
3230Sstevel@tonic-gate 		    ((*lhpp)->lh_vp == vp))
3240Sstevel@tonic-gate 			break;
3250Sstevel@tonic-gate 	}
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	ASSERT(lhpp != NULL);
3280Sstevel@tonic-gate 	return (lhpp);
3290Sstevel@tonic-gate }
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate static struct ldi_handle *
3320Sstevel@tonic-gate handle_find(vnode_t *vp, struct ldi_ident *ident)
3330Sstevel@tonic-gate {
3340Sstevel@tonic-gate 	struct ldi_handle	**lhpp;
3350Sstevel@tonic-gate 	int			index = LH_HASH(vp);
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	mutex_enter(&ldi_handle_hash_lock[index]);
3380Sstevel@tonic-gate 	lhpp = handle_find_ref_nolock(vp, ident);
3390Sstevel@tonic-gate 	mutex_exit(&ldi_handle_hash_lock[index]);
3400Sstevel@tonic-gate 	ASSERT(lhpp != NULL);
3410Sstevel@tonic-gate 	return (*lhpp);
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate static struct ldi_handle *
3450Sstevel@tonic-gate handle_alloc(vnode_t *vp, struct ldi_ident *ident)
3460Sstevel@tonic-gate {
3470Sstevel@tonic-gate 	struct ldi_handle	*lhp, **lhpp;
3480Sstevel@tonic-gate 	uint_t			index;
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	ASSERT((vp != NULL) && (ident != NULL));
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	/* allocate a new handle in case we need it */
3530Sstevel@tonic-gate 	lhp = kmem_zalloc(sizeof (*lhp), KM_SLEEP);
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	/* search the hash for a matching handle */
3560Sstevel@tonic-gate 	index = LH_HASH(vp);
3570Sstevel@tonic-gate 	mutex_enter(&ldi_handle_hash_lock[index]);
3580Sstevel@tonic-gate 	lhpp = handle_find_ref_nolock(vp, ident);
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	if (*lhpp != NULL) {
3610Sstevel@tonic-gate 		/* we found a handle in the hash */
3620Sstevel@tonic-gate 		(*lhpp)->lh_ref++;
3630Sstevel@tonic-gate 		mutex_exit(&ldi_handle_hash_lock[index]);
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 		LDI_ALLOCFREE((CE_WARN, "ldi handle alloc: dup "
3664582Scth 		    "lh=0x%p, ident=0x%p, vp=0x%p, drv=%s, minor=0x%x",
3674582Scth 		    (void *)*lhpp, (void *)ident, (void *)vp,
3684582Scth 		    mod_major_to_name(getmajor(vp->v_rdev)),
3694582Scth 		    getminor(vp->v_rdev)));
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 		kmem_free(lhp, sizeof (struct ldi_handle));
3720Sstevel@tonic-gate 		return (*lhpp);
3730Sstevel@tonic-gate 	}
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	/* initialize the new handle */
3760Sstevel@tonic-gate 	lhp->lh_ref = 1;
3770Sstevel@tonic-gate 	lhp->lh_vp = vp;
3780Sstevel@tonic-gate 	lhp->lh_ident = ident;
3794845Svikram #ifdef	LDI_OBSOLETE_EVENT
3800Sstevel@tonic-gate 	mutex_init(lhp->lh_lock, NULL, MUTEX_DEFAULT, NULL);
3814845Svikram #endif
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	/* set the device type for this handle */
3840Sstevel@tonic-gate 	lhp->lh_type = 0;
3850Sstevel@tonic-gate 	if (STREAMSTAB(getmajor(vp->v_rdev))) {
3860Sstevel@tonic-gate 		ASSERT(vp->v_type == VCHR);
3870Sstevel@tonic-gate 		lhp->lh_type |= LH_STREAM;
3880Sstevel@tonic-gate 	} else {
3890Sstevel@tonic-gate 		lhp->lh_type |= LH_CBDEV;
3900Sstevel@tonic-gate 	}
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	/* get holds on other objects */
3930Sstevel@tonic-gate 	ident_hold(ident);
3940Sstevel@tonic-gate 	ASSERT(vp->v_count >= 1);
3950Sstevel@tonic-gate 	VN_HOLD(vp);
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	/* add it to the handle hash */
3980Sstevel@tonic-gate 	lhp->lh_next = ldi_handle_hash[index];
3990Sstevel@tonic-gate 	ldi_handle_hash[index] = lhp;
4000Sstevel@tonic-gate 	atomic_add_long(&ldi_handle_hash_count, 1);
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	LDI_ALLOCFREE((CE_WARN, "ldi handle alloc: new "
4034582Scth 	    "lh=0x%p, ident=0x%p, vp=0x%p, drv=%s, minor=0x%x",
4044582Scth 	    (void *)lhp, (void *)ident, (void *)vp,
4054582Scth 	    mod_major_to_name(getmajor(vp->v_rdev)),
4064582Scth 	    getminor(vp->v_rdev)));
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	mutex_exit(&ldi_handle_hash_lock[index]);
4090Sstevel@tonic-gate 	return (lhp);
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate static void
4130Sstevel@tonic-gate handle_release(struct ldi_handle *lhp)
4140Sstevel@tonic-gate {
4150Sstevel@tonic-gate 	struct ldi_handle	**lhpp;
4160Sstevel@tonic-gate 	uint_t			index;
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	ASSERT(lhp != NULL);
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	index = LH_HASH(lhp->lh_vp);
4210Sstevel@tonic-gate 	mutex_enter(&ldi_handle_hash_lock[index]);
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	LDI_ALLOCFREE((CE_WARN, "ldi handle release: "
4244582Scth 	    "lh=0x%p, ident=0x%p, vp=0x%p, drv=%s, minor=0x%x",
4254582Scth 	    (void *)lhp, (void *)lhp->lh_ident, (void *)lhp->lh_vp,
4264582Scth 	    mod_major_to_name(getmajor(lhp->lh_vp->v_rdev)),
4274582Scth 	    getminor(lhp->lh_vp->v_rdev)));
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	ASSERT(lhp->lh_ref > 0);
4300Sstevel@tonic-gate 	if (--lhp->lh_ref > 0) {
4310Sstevel@tonic-gate 		/* there are more references to this handle */
4320Sstevel@tonic-gate 		mutex_exit(&ldi_handle_hash_lock[index]);
4330Sstevel@tonic-gate 		return;
4340Sstevel@tonic-gate 	}
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	/* this was the last reference/open for this handle.  free it. */
4370Sstevel@tonic-gate 	lhpp = handle_find_ref_nolock(lhp->lh_vp, lhp->lh_ident);
4380Sstevel@tonic-gate 	ASSERT((lhpp != NULL) && (*lhpp != NULL));
4390Sstevel@tonic-gate 	*lhpp = lhp->lh_next;
4400Sstevel@tonic-gate 	atomic_add_long(&ldi_handle_hash_count, -1);
4410Sstevel@tonic-gate 	mutex_exit(&ldi_handle_hash_lock[index]);
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	VN_RELE(lhp->lh_vp);
4440Sstevel@tonic-gate 	ident_release(lhp->lh_ident);
4454845Svikram #ifdef	LDI_OBSOLETE_EVENT
4460Sstevel@tonic-gate 	mutex_destroy(lhp->lh_lock);
4474845Svikram #endif
4480Sstevel@tonic-gate 	kmem_free(lhp, sizeof (struct ldi_handle));
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate 
4514845Svikram #ifdef	LDI_OBSOLETE_EVENT
4520Sstevel@tonic-gate /*
4530Sstevel@tonic-gate  * LDI event manipulation functions
4540Sstevel@tonic-gate  */
4550Sstevel@tonic-gate static void
4560Sstevel@tonic-gate handle_event_add(ldi_event_t *lep)
4570Sstevel@tonic-gate {
4580Sstevel@tonic-gate 	struct ldi_handle *lhp = lep->le_lhp;
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	ASSERT(lhp != NULL);
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	mutex_enter(lhp->lh_lock);
4630Sstevel@tonic-gate 	if (lhp->lh_events == NULL) {
4640Sstevel@tonic-gate 		lhp->lh_events = lep;
4650Sstevel@tonic-gate 		mutex_exit(lhp->lh_lock);
4660Sstevel@tonic-gate 		return;
4670Sstevel@tonic-gate 	}
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	lep->le_next = lhp->lh_events;
4700Sstevel@tonic-gate 	lhp->lh_events->le_prev = lep;
4710Sstevel@tonic-gate 	lhp->lh_events = lep;
4720Sstevel@tonic-gate 	mutex_exit(lhp->lh_lock);
4730Sstevel@tonic-gate }
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate static void
4760Sstevel@tonic-gate handle_event_remove(ldi_event_t *lep)
4770Sstevel@tonic-gate {
4780Sstevel@tonic-gate 	struct ldi_handle *lhp = lep->le_lhp;
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	ASSERT(lhp != NULL);
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	mutex_enter(lhp->lh_lock);
4830Sstevel@tonic-gate 	if (lep->le_prev)
4840Sstevel@tonic-gate 		lep->le_prev->le_next = lep->le_next;
4850Sstevel@tonic-gate 	if (lep->le_next)
4860Sstevel@tonic-gate 		lep->le_next->le_prev = lep->le_prev;
4870Sstevel@tonic-gate 	if (lhp->lh_events == lep)
4880Sstevel@tonic-gate 		lhp->lh_events = lep->le_next;
4890Sstevel@tonic-gate 	mutex_exit(lhp->lh_lock);
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate }
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate static void
4940Sstevel@tonic-gate i_ldi_callback(dev_info_t *dip, ddi_eventcookie_t event_cookie,
4950Sstevel@tonic-gate     void *arg, void *bus_impldata)
4960Sstevel@tonic-gate {
4970Sstevel@tonic-gate 	ldi_event_t *lep = (ldi_event_t *)arg;
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	ASSERT(lep != NULL);
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	LDI_EVENTCB((CE_NOTE, "%s: dip=0x%p, "
5020Sstevel@tonic-gate 	    "event_cookie=0x%p, ldi_eventp=0x%p", "i_ldi_callback",
5030Sstevel@tonic-gate 	    (void *)dip, (void *)event_cookie, (void *)lep));
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	lep->le_handler(lep->le_lhp, event_cookie, lep->le_arg, bus_impldata);
5060Sstevel@tonic-gate }
5074845Svikram #endif
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate /*
5100Sstevel@tonic-gate  * LDI open helper functions
5110Sstevel@tonic-gate  */
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate /* get a vnode to a device by dev_t and otyp */
5140Sstevel@tonic-gate static int
5150Sstevel@tonic-gate ldi_vp_from_dev(dev_t dev, int otyp, vnode_t **vpp)
5160Sstevel@tonic-gate {
5170Sstevel@tonic-gate 	dev_info_t		*dip;
5180Sstevel@tonic-gate 	vnode_t			*vp;
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	/* sanity check required input parameters */
5210Sstevel@tonic-gate 	if ((dev == DDI_DEV_T_NONE) || (!OTYP_VALID(otyp)) || (vpp == NULL))
5220Sstevel@tonic-gate 		return (EINVAL);
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 	if ((dip = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
5250Sstevel@tonic-gate 		return (ENODEV);
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	if (STREAMSTAB(getmajor(dev)) && (otyp != OTYP_CHR)) {
5280Sstevel@tonic-gate 		ddi_release_devi(dip);  /* from e_ddi_hold_devi_by_dev */
5290Sstevel@tonic-gate 		return (ENXIO);
5300Sstevel@tonic-gate 	}
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	vp = makespecvp(dev, OTYP_TO_VTYP(otyp));
5330Sstevel@tonic-gate 	spec_assoc_vp_with_devi(vp, dip);
5340Sstevel@tonic-gate 	ddi_release_devi(dip);  /* from e_ddi_hold_devi_by_dev */
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	*vpp = vp;
5370Sstevel@tonic-gate 	return (0);
5380Sstevel@tonic-gate }
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate /* get a vnode to a device by pathname */
5410Sstevel@tonic-gate static int
5420Sstevel@tonic-gate ldi_vp_from_name(char *path, vnode_t **vpp)
5430Sstevel@tonic-gate {
5440Sstevel@tonic-gate 	vnode_t			*vp = NULL;
5450Sstevel@tonic-gate 	int			ret;
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 	/* sanity check required input parameters */
5480Sstevel@tonic-gate 	if ((path == NULL) || (vpp == NULL))
5490Sstevel@tonic-gate 		return (EINVAL);
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	if (modrootloaded) {
5520Sstevel@tonic-gate 		cred_t *saved_cred = curthread->t_cred;
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 		/* we don't want lookupname to fail because of credentials */
5550Sstevel@tonic-gate 		curthread->t_cred = kcred;
556538Sedp 
557538Sedp 		/*
558538Sedp 		 * all lookups should be done in the global zone.  but
559538Sedp 		 * lookupnameat() won't actually do this if an absolute
560538Sedp 		 * path is passed in.  since the ldi interfaces require an
561538Sedp 		 * absolute path we pass lookupnameat() a pointer to
562538Sedp 		 * the character after the leading '/' and tell it to
563538Sedp 		 * start searching at the current system root directory.
564538Sedp 		 */
565538Sedp 		ASSERT(*path == '/');
566538Sedp 		ret = lookupnameat(path + 1, UIO_SYSSPACE, FOLLOW, NULLVPP,
567538Sedp 		    &vp, rootdir);
568538Sedp 
569538Sedp 		/* restore this threads credentials */
5700Sstevel@tonic-gate 		curthread->t_cred = saved_cred;
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 		if (ret == 0) {
5730Sstevel@tonic-gate 			if (!vn_matchops(vp, spec_getvnodeops()) ||
5740Sstevel@tonic-gate 			    !VTYP_VALID(vp->v_type)) {
5750Sstevel@tonic-gate 				VN_RELE(vp);
5760Sstevel@tonic-gate 				return (ENXIO);
5770Sstevel@tonic-gate 			}
5780Sstevel@tonic-gate 		}
5790Sstevel@tonic-gate 	}
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	if (vp == NULL) {
5820Sstevel@tonic-gate 		dev_info_t	*dip;
5830Sstevel@tonic-gate 		dev_t		dev;
5840Sstevel@tonic-gate 		int		spec_type;
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 		/*
5870Sstevel@tonic-gate 		 * Root is not mounted, the minor node is not specified,
5880Sstevel@tonic-gate 		 * or an OBP path has been specified.
5890Sstevel@tonic-gate 		 */
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 		/*
5920Sstevel@tonic-gate 		 * Determine if path can be pruned to produce an
5930Sstevel@tonic-gate 		 * OBP or devfs path for resolve_pathname.
5940Sstevel@tonic-gate 		 */
5950Sstevel@tonic-gate 		if (strncmp(path, "/devices/", 9) == 0)
5960Sstevel@tonic-gate 			path += strlen("/devices");
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 		/*
5990Sstevel@tonic-gate 		 * if no minor node was specified the DEFAULT minor node
6000Sstevel@tonic-gate 		 * will be returned.  if there is no DEFAULT minor node
6010Sstevel@tonic-gate 		 * one will be fabricated of type S_IFCHR with the minor
6020Sstevel@tonic-gate 		 * number equal to the instance number.
6030Sstevel@tonic-gate 		 */
6040Sstevel@tonic-gate 		ret = resolve_pathname(path, &dip, &dev, &spec_type);
6050Sstevel@tonic-gate 		if (ret != 0)
6060Sstevel@tonic-gate 			return (ENODEV);
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 		ASSERT(STYP_VALID(spec_type));
6090Sstevel@tonic-gate 		vp = makespecvp(dev, STYP_TO_VTYP(spec_type));
6100Sstevel@tonic-gate 		spec_assoc_vp_with_devi(vp, dip);
6110Sstevel@tonic-gate 		ddi_release_devi(dip);
6120Sstevel@tonic-gate 	}
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	*vpp = vp;
6150Sstevel@tonic-gate 	return (0);
6160Sstevel@tonic-gate }
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate static int
6190Sstevel@tonic-gate ldi_devid_match(ddi_devid_t devid, dev_info_t *dip, dev_t dev)
6200Sstevel@tonic-gate {
6210Sstevel@tonic-gate 	char		*devidstr;
6220Sstevel@tonic-gate 	ddi_prop_t	*propp;
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	/* convert devid as a string property */
6250Sstevel@tonic-gate 	if ((devidstr = ddi_devid_str_encode(devid, NULL)) == NULL)
6260Sstevel@tonic-gate 		return (0);
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	/*
6290Sstevel@tonic-gate 	 * Search for the devid.  For speed and ease in locking this
6300Sstevel@tonic-gate 	 * code directly uses the property implementation.  See
6310Sstevel@tonic-gate 	 * ddi_common_devid_to_devlist() for a comment as to why.
6320Sstevel@tonic-gate 	 */
6330Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	/* check if there is a DDI_DEV_T_NONE devid property */
6360Sstevel@tonic-gate 	propp = i_ddi_prop_search(DDI_DEV_T_NONE,
6370Sstevel@tonic-gate 	    DEVID_PROP_NAME, DEVID_PROP_FLAGS, &DEVI(dip)->devi_hw_prop_ptr);
6380Sstevel@tonic-gate 	if (propp != NULL) {
6390Sstevel@tonic-gate 		if (ddi_devid_str_compare(propp->prop_val, devidstr) == 0) {
6400Sstevel@tonic-gate 			/* a DDI_DEV_T_NONE devid exists and matchs */
6410Sstevel@tonic-gate 			mutex_exit(&(DEVI(dip)->devi_lock));
6420Sstevel@tonic-gate 			ddi_devid_str_free(devidstr);
6430Sstevel@tonic-gate 			return (1);
6440Sstevel@tonic-gate 		} else {
6450Sstevel@tonic-gate 			/* a DDI_DEV_T_NONE devid exists and doesn't match */
6460Sstevel@tonic-gate 			mutex_exit(&(DEVI(dip)->devi_lock));
6470Sstevel@tonic-gate 			ddi_devid_str_free(devidstr);
6480Sstevel@tonic-gate 			return (0);
6490Sstevel@tonic-gate 		}
6500Sstevel@tonic-gate 	}
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 	/* check if there is a devt specific devid property */
6530Sstevel@tonic-gate 	propp = i_ddi_prop_search(dev,
6540Sstevel@tonic-gate 	    DEVID_PROP_NAME, DEVID_PROP_FLAGS, &(DEVI(dip)->devi_hw_prop_ptr));
6550Sstevel@tonic-gate 	if (propp != NULL) {
6560Sstevel@tonic-gate 		if (ddi_devid_str_compare(propp->prop_val, devidstr) == 0) {
6570Sstevel@tonic-gate 			/* a devt specific devid exists and matchs */
6580Sstevel@tonic-gate 			mutex_exit(&(DEVI(dip)->devi_lock));
6590Sstevel@tonic-gate 			ddi_devid_str_free(devidstr);
6600Sstevel@tonic-gate 			return (1);
6610Sstevel@tonic-gate 		} else {
6620Sstevel@tonic-gate 			/* a devt specific devid exists and doesn't match */
6630Sstevel@tonic-gate 			mutex_exit(&(DEVI(dip)->devi_lock));
6640Sstevel@tonic-gate 			ddi_devid_str_free(devidstr);
6650Sstevel@tonic-gate 			return (0);
6660Sstevel@tonic-gate 		}
6670Sstevel@tonic-gate 	}
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	/* we didn't find any devids associated with the device */
6700Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
6710Sstevel@tonic-gate 	ddi_devid_str_free(devidstr);
6720Sstevel@tonic-gate 	return (0);
6730Sstevel@tonic-gate }
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate /* get a handle to a device by devid and minor name */
6760Sstevel@tonic-gate static int
6770Sstevel@tonic-gate ldi_vp_from_devid(ddi_devid_t devid, char *minor_name, vnode_t **vpp)
6780Sstevel@tonic-gate {
6790Sstevel@tonic-gate 	dev_info_t		*dip;
6800Sstevel@tonic-gate 	vnode_t			*vp;
6810Sstevel@tonic-gate 	int			ret, i, ndevs, styp;
6820Sstevel@tonic-gate 	dev_t			dev, *devs;
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	/* sanity check required input parameters */
6850Sstevel@tonic-gate 	if ((devid == NULL) || (minor_name == NULL) || (vpp == NULL))
6860Sstevel@tonic-gate 		return (EINVAL);
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	ret = ddi_lyr_devid_to_devlist(devid, minor_name, &ndevs, &devs);
6890Sstevel@tonic-gate 	if ((ret != DDI_SUCCESS) || (ndevs <= 0))
6900Sstevel@tonic-gate 		return (ENODEV);
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	for (i = 0; i < ndevs; i++) {
6930Sstevel@tonic-gate 		dev = devs[i];
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 		if ((dip = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
6960Sstevel@tonic-gate 			continue;
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 		/*
6990Sstevel@tonic-gate 		 * now we have to verify that the devid of the disk
7000Sstevel@tonic-gate 		 * still matches what was requested.
7010Sstevel@tonic-gate 		 *
7020Sstevel@tonic-gate 		 * we have to do this because the devid could have
7030Sstevel@tonic-gate 		 * changed between the call to ddi_lyr_devid_to_devlist()
7040Sstevel@tonic-gate 		 * and e_ddi_hold_devi_by_dev().  this is because when
7050Sstevel@tonic-gate 		 * ddi_lyr_devid_to_devlist() returns a list of devts
7060Sstevel@tonic-gate 		 * there is no kind of hold on those devts so a device
7070Sstevel@tonic-gate 		 * could have been replaced out from under us in the
7080Sstevel@tonic-gate 		 * interim.
7090Sstevel@tonic-gate 		 */
7100Sstevel@tonic-gate 		if ((i_ddi_minorname_to_devtspectype(dip, minor_name,
7110Sstevel@tonic-gate 		    NULL, &styp) == DDI_SUCCESS) &&
7120Sstevel@tonic-gate 		    ldi_devid_match(devid, dip, dev))
7130Sstevel@tonic-gate 			break;
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 		ddi_release_devi(dip);	/* from e_ddi_hold_devi_by_dev() */
7160Sstevel@tonic-gate 	}
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	ddi_lyr_free_devlist(devs, ndevs);
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	if (i == ndevs)
7210Sstevel@tonic-gate 		return (ENODEV);
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	ASSERT(STYP_VALID(styp));
7240Sstevel@tonic-gate 	vp = makespecvp(dev, STYP_TO_VTYP(styp));
7250Sstevel@tonic-gate 	spec_assoc_vp_with_devi(vp, dip);
7260Sstevel@tonic-gate 	ddi_release_devi(dip);		/* from e_ddi_hold_devi_by_dev */
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	*vpp = vp;
7290Sstevel@tonic-gate 	return (0);
7300Sstevel@tonic-gate }
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate /* given a vnode, open a device */
7330Sstevel@tonic-gate static int
7340Sstevel@tonic-gate ldi_open_by_vp(vnode_t **vpp, int flag, cred_t *cr,
7350Sstevel@tonic-gate     ldi_handle_t *lhp, struct ldi_ident *li)
7360Sstevel@tonic-gate {
7370Sstevel@tonic-gate 	struct ldi_handle	*nlhp;
7380Sstevel@tonic-gate 	vnode_t			*vp;
7390Sstevel@tonic-gate 	int			err;
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 	ASSERT((vpp != NULL) && (*vpp != NULL));
7420Sstevel@tonic-gate 	ASSERT((lhp != NULL) && (li != NULL));
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	vp = *vpp;
7450Sstevel@tonic-gate 	/* if the vnode passed in is not a device, then bail */
7460Sstevel@tonic-gate 	if (!vn_matchops(vp, spec_getvnodeops()) || !VTYP_VALID(vp->v_type))
7470Sstevel@tonic-gate 		return (ENXIO);
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	/*
7500Sstevel@tonic-gate 	 * the caller may have specified a node that
7510Sstevel@tonic-gate 	 * doesn't have cb_ops defined.  the ldi doesn't yet
7520Sstevel@tonic-gate 	 * support opening devices without a valid cb_ops.
7530Sstevel@tonic-gate 	 */
7540Sstevel@tonic-gate 	if (devopsp[getmajor(vp->v_rdev)]->devo_cb_ops == NULL)
7550Sstevel@tonic-gate 		return (ENXIO);
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 	/* open the device */
7585331Samw 	if ((err = VOP_OPEN(&vp, flag | FKLYR, cr, NULL)) != 0)
7590Sstevel@tonic-gate 		return (err);
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	/* possible clone open, make sure that we still have a spec node */
7620Sstevel@tonic-gate 	ASSERT(vn_matchops(vp, spec_getvnodeops()));
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	nlhp = handle_alloc(vp, li);
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	if (vp != *vpp) {
7670Sstevel@tonic-gate 		/*
7680Sstevel@tonic-gate 		 * allocating the layered handle took a new hold on the vnode
7690Sstevel@tonic-gate 		 * so we can release the hold that was returned by the clone
7700Sstevel@tonic-gate 		 * open
7710Sstevel@tonic-gate 		 */
7720Sstevel@tonic-gate 		LDI_OPENCLOSE((CE_WARN, "%s: lh=0x%p",
7734582Scth 		    "ldi clone open", (void *)nlhp));
7740Sstevel@tonic-gate 	} else {
7750Sstevel@tonic-gate 		LDI_OPENCLOSE((CE_WARN, "%s: lh=0x%p",
7764582Scth 		    "ldi open", (void *)nlhp));
7770Sstevel@tonic-gate 	}
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	/* Flush back any dirty pages associated with the device. */
7800Sstevel@tonic-gate 	if (nlhp->lh_type & LH_CBDEV) {
7810Sstevel@tonic-gate 		vnode_t	*cvp = common_specvp(nlhp->lh_vp);
7820Sstevel@tonic-gate 		dev_t	dev = cvp->v_rdev;
7830Sstevel@tonic-gate 
7845331Samw 		(void) VOP_PUTPAGE(cvp, 0, 0, B_INVAL, kcred, NULL);
7850Sstevel@tonic-gate 		bflush(dev);
7860Sstevel@tonic-gate 	}
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 	*vpp = vp;
7890Sstevel@tonic-gate 	*lhp = (ldi_handle_t)nlhp;
7900Sstevel@tonic-gate 	return (0);
7910Sstevel@tonic-gate }
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate /* Call a drivers prop_op(9E) interface */
7940Sstevel@tonic-gate static int
7950Sstevel@tonic-gate i_ldi_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
7960Sstevel@tonic-gate     int flags, char *name, caddr_t valuep, int *lengthp)
7970Sstevel@tonic-gate {
7980Sstevel@tonic-gate 	struct dev_ops	*ops = NULL;
7990Sstevel@tonic-gate 	int		res;
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	ASSERT((dip != NULL) && (name != NULL));
8020Sstevel@tonic-gate 	ASSERT((prop_op == PROP_LEN) || (valuep != NULL));
8030Sstevel@tonic-gate 	ASSERT(lengthp != NULL);
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	/*
8060Sstevel@tonic-gate 	 * we can only be invoked after a driver has been opened and
8070Sstevel@tonic-gate 	 * someone has a layered handle to it, so there had better be
8080Sstevel@tonic-gate 	 * a valid ops vector.
8090Sstevel@tonic-gate 	 */
8100Sstevel@tonic-gate 	ops = DEVI(dip)->devi_ops;
8110Sstevel@tonic-gate 	ASSERT(ops && ops->devo_cb_ops);
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 	/*
8140Sstevel@tonic-gate 	 * Some nexus drivers incorrectly set cb_prop_op to nodev,
8150Sstevel@tonic-gate 	 * nulldev or even NULL.
8160Sstevel@tonic-gate 	 */
8170Sstevel@tonic-gate 	if ((ops->devo_cb_ops->cb_prop_op == nodev) ||
8180Sstevel@tonic-gate 	    (ops->devo_cb_ops->cb_prop_op == nulldev) ||
8190Sstevel@tonic-gate 	    (ops->devo_cb_ops->cb_prop_op == NULL)) {
8200Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
8210Sstevel@tonic-gate 	}
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 	/* check if this is actually DDI_DEV_T_ANY query */
8240Sstevel@tonic-gate 	if (flags & LDI_DEV_T_ANY) {
8250Sstevel@tonic-gate 		flags &= ~LDI_DEV_T_ANY;
8260Sstevel@tonic-gate 		dev = DDI_DEV_T_ANY;
8270Sstevel@tonic-gate 	}
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	res = cdev_prop_op(dev, dip, prop_op, flags, name, valuep, lengthp);
8300Sstevel@tonic-gate 	return (res);
8310Sstevel@tonic-gate }
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate static void
8340Sstevel@tonic-gate i_ldi_prop_op_free(struct prop_driver_data *pdd)
8350Sstevel@tonic-gate {
8360Sstevel@tonic-gate 	kmem_free(pdd, pdd->pdd_size);
8370Sstevel@tonic-gate }
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate static caddr_t
8400Sstevel@tonic-gate i_ldi_prop_op_alloc(int prop_len)
8410Sstevel@tonic-gate {
8420Sstevel@tonic-gate 	struct prop_driver_data	*pdd;
8430Sstevel@tonic-gate 	int			pdd_size;
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 	pdd_size = sizeof (struct prop_driver_data) + prop_len;
8460Sstevel@tonic-gate 	pdd = kmem_alloc(pdd_size, KM_SLEEP);
8470Sstevel@tonic-gate 	pdd->pdd_size = pdd_size;
8480Sstevel@tonic-gate 	pdd->pdd_prop_free = i_ldi_prop_op_free;
8490Sstevel@tonic-gate 	return ((caddr_t)&pdd[1]);
8500Sstevel@tonic-gate }
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate /*
8530Sstevel@tonic-gate  * i_ldi_prop_op_typed() is a wrapper for i_ldi_prop_op that is used
8540Sstevel@tonic-gate  * by the typed ldi property lookup interfaces.
8550Sstevel@tonic-gate  */
8560Sstevel@tonic-gate static int
8570Sstevel@tonic-gate i_ldi_prop_op_typed(dev_t dev, dev_info_t *dip, int flags, char *name,
8580Sstevel@tonic-gate     caddr_t *datap, int *lengthp, int elem_size)
8590Sstevel@tonic-gate {
8600Sstevel@tonic-gate 	caddr_t	prop_val;
8610Sstevel@tonic-gate 	int	prop_len, res;
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 	ASSERT((dip != NULL) && (name != NULL));
8640Sstevel@tonic-gate 	ASSERT((datap != NULL) && (lengthp != NULL));
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 	/*
8670Sstevel@tonic-gate 	 * first call the drivers prop_op() interface to allow it
8680Sstevel@tonic-gate 	 * it to override default property values.
8690Sstevel@tonic-gate 	 */
8700Sstevel@tonic-gate 	res = i_ldi_prop_op(dev, dip, PROP_LEN,
8710Sstevel@tonic-gate 	    flags | DDI_PROP_DYNAMIC, name, NULL, &prop_len);
8720Sstevel@tonic-gate 	if (res != DDI_PROP_SUCCESS)
8730Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 	/* sanity check the property length */
8760Sstevel@tonic-gate 	if (prop_len == 0) {
8770Sstevel@tonic-gate 		/*
8780Sstevel@tonic-gate 		 * the ddi typed interfaces don't allow a drivers to
8790Sstevel@tonic-gate 		 * create properties with a length of 0.  so we should
8800Sstevel@tonic-gate 		 * prevent drivers from returning 0 length dynamic
8810Sstevel@tonic-gate 		 * properties for typed property lookups.
8820Sstevel@tonic-gate 		 */
8830Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
8840Sstevel@tonic-gate 	}
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 	/* sanity check the property length against the element size */
8870Sstevel@tonic-gate 	if (elem_size && ((prop_len % elem_size) != 0))
8880Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 	/*
8910Sstevel@tonic-gate 	 * got it.  now allocate a prop_driver_data struct so that the
8920Sstevel@tonic-gate 	 * user can free the property via ddi_prop_free().
8930Sstevel@tonic-gate 	 */
8940Sstevel@tonic-gate 	prop_val = i_ldi_prop_op_alloc(prop_len);
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate 	/* lookup the property again, this time get the value */
8970Sstevel@tonic-gate 	res = i_ldi_prop_op(dev, dip, PROP_LEN_AND_VAL_BUF,
8980Sstevel@tonic-gate 	    flags | DDI_PROP_DYNAMIC, name, prop_val, &prop_len);
8990Sstevel@tonic-gate 	if (res != DDI_PROP_SUCCESS) {
9000Sstevel@tonic-gate 		ddi_prop_free(prop_val);
9010Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
9020Sstevel@tonic-gate 	}
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	/* sanity check the property length */
9050Sstevel@tonic-gate 	if (prop_len == 0) {
9060Sstevel@tonic-gate 		ddi_prop_free(prop_val);
9070Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
9080Sstevel@tonic-gate 	}
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	/* sanity check the property length against the element size */
9110Sstevel@tonic-gate 	if (elem_size && ((prop_len % elem_size) != 0)) {
9120Sstevel@tonic-gate 		ddi_prop_free(prop_val);
9130Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
9140Sstevel@tonic-gate 	}
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 	/*
9170Sstevel@tonic-gate 	 * return the prop_driver_data struct and, optionally, the length
9180Sstevel@tonic-gate 	 * of the data.
9190Sstevel@tonic-gate 	 */
9200Sstevel@tonic-gate 	*datap = prop_val;
9210Sstevel@tonic-gate 	*lengthp = prop_len;
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
9240Sstevel@tonic-gate }
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate /*
9270Sstevel@tonic-gate  * i_check_string looks at a string property and makes sure its
9280Sstevel@tonic-gate  * a valid null terminated string
9290Sstevel@tonic-gate  */
9300Sstevel@tonic-gate static int
9310Sstevel@tonic-gate i_check_string(char *str, int prop_len)
9320Sstevel@tonic-gate {
9330Sstevel@tonic-gate 	int i;
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	ASSERT(str != NULL);
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 	for (i = 0; i < prop_len; i++) {
9380Sstevel@tonic-gate 		if (str[i] == '\0')
9390Sstevel@tonic-gate 			return (0);
9400Sstevel@tonic-gate 	}
9410Sstevel@tonic-gate 	return (1);
9420Sstevel@tonic-gate }
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate /*
9450Sstevel@tonic-gate  * i_pack_string_array takes a a string array property that is represented
9465331Samw  * as a concatenation of strings (with the NULL character included for
9470Sstevel@tonic-gate  * each string) and converts it into a format that can be returned by
9480Sstevel@tonic-gate  * ldi_prop_lookup_string_array.
9490Sstevel@tonic-gate  */
9500Sstevel@tonic-gate static int
9510Sstevel@tonic-gate i_pack_string_array(char *str_concat, int prop_len,
9520Sstevel@tonic-gate     char ***str_arrayp, int *nelemp)
9530Sstevel@tonic-gate {
9540Sstevel@tonic-gate 	int i, nelem, pack_size;
9550Sstevel@tonic-gate 	char **str_array, *strptr;
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 	/*
9580Sstevel@tonic-gate 	 * first we need to sanity check the input string array.
9590Sstevel@tonic-gate 	 * in essence this can be done my making sure that the last
9600Sstevel@tonic-gate 	 * character of the array passed in is null.  (meaning the last
9610Sstevel@tonic-gate 	 * string in the array is NULL terminated.
9620Sstevel@tonic-gate 	 */
9630Sstevel@tonic-gate 	if (str_concat[prop_len - 1] != '\0')
9640Sstevel@tonic-gate 		return (1);
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 	/* now let's count the number of strings in the array */
9670Sstevel@tonic-gate 	for (nelem = i = 0; i < prop_len; i++)
9680Sstevel@tonic-gate 		if (str_concat[i] == '\0')
9690Sstevel@tonic-gate 			nelem++;
9700Sstevel@tonic-gate 	ASSERT(nelem >= 1);
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 	/* now let's allocate memory for the new packed property */
9730Sstevel@tonic-gate 	pack_size = (sizeof (char *) * (nelem + 1)) + prop_len;
9740Sstevel@tonic-gate 	str_array = (char **)i_ldi_prop_op_alloc(pack_size);
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate 	/* let's copy the actual string data into the new property */
9770Sstevel@tonic-gate 	strptr = (char *)&(str_array[nelem + 1]);
9780Sstevel@tonic-gate 	bcopy(str_concat, strptr, prop_len);
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 	/* now initialize the string array pointers */
9810Sstevel@tonic-gate 	for (i = 0; i < nelem; i++) {
9820Sstevel@tonic-gate 		str_array[i] = strptr;
9830Sstevel@tonic-gate 		strptr += strlen(strptr) + 1;
9840Sstevel@tonic-gate 	}
9850Sstevel@tonic-gate 	str_array[nelem] = NULL;
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 	/* set the return values */
9880Sstevel@tonic-gate 	*str_arrayp = str_array;
9890Sstevel@tonic-gate 	*nelemp = nelem;
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 	return (0);
9920Sstevel@tonic-gate }
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate /*
9960Sstevel@tonic-gate  * LDI Project private device usage interfaces
9970Sstevel@tonic-gate  */
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate /*
10000Sstevel@tonic-gate  * Get a count of how many devices are currentl open by different consumers
10010Sstevel@tonic-gate  */
10020Sstevel@tonic-gate int
10030Sstevel@tonic-gate ldi_usage_count()
10040Sstevel@tonic-gate {
10050Sstevel@tonic-gate 	return (ldi_handle_hash_count);
10060Sstevel@tonic-gate }
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate static void
10090Sstevel@tonic-gate ldi_usage_walker_tgt_helper(ldi_usage_t *ldi_usage, vnode_t *vp)
10100Sstevel@tonic-gate {
10110Sstevel@tonic-gate 	dev_info_t	*dip;
10120Sstevel@tonic-gate 	dev_t		dev;
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 	ASSERT(STYP_VALID(VTYP_TO_STYP(vp->v_type)));
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 	/* get the target devt */
10170Sstevel@tonic-gate 	dev = vp->v_rdev;
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 	/* try to get the target dip */
10200Sstevel@tonic-gate 	dip = VTOCS(vp)->s_dip;
10210Sstevel@tonic-gate 	if (dip != NULL) {
10220Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
10230Sstevel@tonic-gate 	} else if (dev != DDI_DEV_T_NONE) {
10240Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
10250Sstevel@tonic-gate 	}
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate 	/* set the target information */
10280Sstevel@tonic-gate 	ldi_usage->tgt_name = mod_major_to_name(getmajor(dev));
10290Sstevel@tonic-gate 	ldi_usage->tgt_modid = mod_name_to_modid(ldi_usage->tgt_name);
10300Sstevel@tonic-gate 	ldi_usage->tgt_devt = dev;
10310Sstevel@tonic-gate 	ldi_usage->tgt_spec_type = VTYP_TO_STYP(vp->v_type);
10320Sstevel@tonic-gate 	ldi_usage->tgt_dip = dip;
10330Sstevel@tonic-gate }
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate static int
10370Sstevel@tonic-gate ldi_usage_walker_helper(struct ldi_ident *lip, vnode_t *vp,
10380Sstevel@tonic-gate     void *arg, int (*callback)(const ldi_usage_t *, void *))
10390Sstevel@tonic-gate {
10400Sstevel@tonic-gate 	ldi_usage_t	ldi_usage;
10410Sstevel@tonic-gate 	struct devnames	*dnp;
10420Sstevel@tonic-gate 	dev_info_t	*dip;
10430Sstevel@tonic-gate 	major_t		major;
10440Sstevel@tonic-gate 	dev_t		dev;
10450Sstevel@tonic-gate 	int		ret = LDI_USAGE_CONTINUE;
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 	/* set the target device information */
10480Sstevel@tonic-gate 	ldi_usage_walker_tgt_helper(&ldi_usage, vp);
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate 	/* get the source devt */
10510Sstevel@tonic-gate 	dev = lip->li_dev;
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 	/* try to get the source dip */
10540Sstevel@tonic-gate 	dip = lip->li_dip;
10550Sstevel@tonic-gate 	if (dip != NULL) {
10560Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
10570Sstevel@tonic-gate 	} else if (dev != DDI_DEV_T_NONE) {
10580Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
10590Sstevel@tonic-gate 	}
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 	/* set the valid source information */
10620Sstevel@tonic-gate 	ldi_usage.src_modid = lip->li_modid;
10630Sstevel@tonic-gate 	ldi_usage.src_name = lip->li_modname;
10640Sstevel@tonic-gate 	ldi_usage.src_devt = dev;
10650Sstevel@tonic-gate 	ldi_usage.src_dip = dip;
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 	/*
10680Sstevel@tonic-gate 	 * if the source ident represents either:
10690Sstevel@tonic-gate 	 *
10700Sstevel@tonic-gate 	 * - a kernel module (and not a device or device driver)
10710Sstevel@tonic-gate 	 * - a device node
10720Sstevel@tonic-gate 	 *
10730Sstevel@tonic-gate 	 * then we currently have all the info we need to report the
10740Sstevel@tonic-gate 	 * usage information so invoke the callback function.
10750Sstevel@tonic-gate 	 */
10760Sstevel@tonic-gate 	if (((lip->li_major == -1) && (dev == DDI_DEV_T_NONE)) ||
10770Sstevel@tonic-gate 	    (dip != NULL)) {
10780Sstevel@tonic-gate 		ret = callback(&ldi_usage, arg);
10790Sstevel@tonic-gate 		if (dip != NULL)
10800Sstevel@tonic-gate 			ddi_release_devi(dip);
10810Sstevel@tonic-gate 		if (ldi_usage.tgt_dip != NULL)
10820Sstevel@tonic-gate 			ddi_release_devi(ldi_usage.tgt_dip);
10830Sstevel@tonic-gate 		return (ret);
10840Sstevel@tonic-gate 	}
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	/*
10870Sstevel@tonic-gate 	 * now this is kinda gross.
10880Sstevel@tonic-gate 	 *
10890Sstevel@tonic-gate 	 * what we do here is attempt to associate every device instance
10900Sstevel@tonic-gate 	 * of the source driver on the system with the open target driver.
10910Sstevel@tonic-gate 	 * we do this because we don't know which instance of the device
10920Sstevel@tonic-gate 	 * could potentially access the lower device so we assume that all
10930Sstevel@tonic-gate 	 * the instances could access it.
10940Sstevel@tonic-gate 	 *
10950Sstevel@tonic-gate 	 * there are two ways we could have gotten here:
10960Sstevel@tonic-gate 	 *
10970Sstevel@tonic-gate 	 * 1) this layered ident represents one created using only a
10980Sstevel@tonic-gate 	 *    major number or a driver module name.  this means that when
10990Sstevel@tonic-gate 	 *    it was created we could not associate it with a particular
11000Sstevel@tonic-gate 	 *    dev_t or device instance.
11010Sstevel@tonic-gate 	 *
11020Sstevel@tonic-gate 	 *    when could this possibly happen you ask?
11030Sstevel@tonic-gate 	 *
11040Sstevel@tonic-gate 	 *    a perfect example of this is streams persistent links.
11050Sstevel@tonic-gate 	 *    when a persistant streams link is formed we can't associate
11060Sstevel@tonic-gate 	 *    the lower device stream with any particular upper device
11070Sstevel@tonic-gate 	 *    stream or instance.  this is because any particular upper
11080Sstevel@tonic-gate 	 *    device stream could be closed, then another could be
11090Sstevel@tonic-gate 	 *    opened with a different dev_t and device instance, and it
11100Sstevel@tonic-gate 	 *    would still have access to the lower linked stream.
11110Sstevel@tonic-gate 	 *
11120Sstevel@tonic-gate 	 *    since any instance of the upper streams driver could
11130Sstevel@tonic-gate 	 *    potentially access the lower stream whenever it wants,
11140Sstevel@tonic-gate 	 *    we represent that here by associating the opened lower
11150Sstevel@tonic-gate 	 *    device with every existing device instance of the upper
11160Sstevel@tonic-gate 	 *    streams driver.
11170Sstevel@tonic-gate 	 *
11180Sstevel@tonic-gate 	 * 2) This case should really never happen but we'll include it
11190Sstevel@tonic-gate 	 *    for completeness.
11200Sstevel@tonic-gate 	 *
11210Sstevel@tonic-gate 	 *    it's possible that we could have gotten here because we
11220Sstevel@tonic-gate 	 *    have a dev_t for the upper device but we couldn't find a
11230Sstevel@tonic-gate 	 *    dip associated with that dev_t.
11240Sstevel@tonic-gate 	 *
11250Sstevel@tonic-gate 	 *    the only types of devices that have dev_t without an
11260Sstevel@tonic-gate 	 *    associated dip are unbound DLPIv2 network devices.  These
11270Sstevel@tonic-gate 	 *    types of devices exist to be able to attach a stream to any
11280Sstevel@tonic-gate 	 *    instance of a hardware network device.  since these types of
11290Sstevel@tonic-gate 	 *    devices are usually hardware devices they should never
11300Sstevel@tonic-gate 	 *    really have other devices open.
11310Sstevel@tonic-gate 	 */
11320Sstevel@tonic-gate 	if (dev != DDI_DEV_T_NONE)
11330Sstevel@tonic-gate 		major = getmajor(dev);
11340Sstevel@tonic-gate 	else
11350Sstevel@tonic-gate 		major = lip->li_major;
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 	ASSERT((major >= 0) && (major < devcnt));
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 	dnp = &devnamesp[major];
11400Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
11410Sstevel@tonic-gate 	dip = dnp->dn_head;
11420Sstevel@tonic-gate 	while ((dip) && (ret == LDI_USAGE_CONTINUE)) {
11430Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
11440Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate 		/* set the source dip */
11470Sstevel@tonic-gate 		ldi_usage.src_dip = dip;
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate 		/* invoke the callback function */
11500Sstevel@tonic-gate 		ret = callback(&ldi_usage, arg);
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 		LOCK_DEV_OPS(&dnp->dn_lock);
11530Sstevel@tonic-gate 		ddi_release_devi(dip);
11540Sstevel@tonic-gate 		dip = ddi_get_next(dip);
11550Sstevel@tonic-gate 	}
11560Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	/* if there was a target dip, release it */
11590Sstevel@tonic-gate 	if (ldi_usage.tgt_dip != NULL)
11600Sstevel@tonic-gate 		ddi_release_devi(ldi_usage.tgt_dip);
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 	return (ret);
11630Sstevel@tonic-gate }
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate /*
11660Sstevel@tonic-gate  * ldi_usage_walker() - this walker reports LDI kernel device usage
11670Sstevel@tonic-gate  * information via the callback() callback function.  the LDI keeps track
11680Sstevel@tonic-gate  * of what devices are being accessed in its own internal data structures.
11690Sstevel@tonic-gate  * this function walks those data structures to determine device usage.
11700Sstevel@tonic-gate  */
11710Sstevel@tonic-gate void
11720Sstevel@tonic-gate ldi_usage_walker(void *arg, int (*callback)(const ldi_usage_t *, void *))
11730Sstevel@tonic-gate {
11740Sstevel@tonic-gate 	struct ldi_handle	*lhp;
11750Sstevel@tonic-gate 	struct ldi_ident	*lip;
11760Sstevel@tonic-gate 	vnode_t			*vp;
11770Sstevel@tonic-gate 	int			i;
11780Sstevel@tonic-gate 	int			ret = LDI_USAGE_CONTINUE;
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 	for (i = 0; i < LH_HASH_SZ; i++) {
11810Sstevel@tonic-gate 		mutex_enter(&ldi_handle_hash_lock[i]);
11820Sstevel@tonic-gate 
11830Sstevel@tonic-gate 		lhp = ldi_handle_hash[i];
11840Sstevel@tonic-gate 		while ((lhp != NULL) && (ret == LDI_USAGE_CONTINUE)) {
11850Sstevel@tonic-gate 			lip = lhp->lh_ident;
11860Sstevel@tonic-gate 			vp = lhp->lh_vp;
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate 			/* invoke the devinfo callback function */
11890Sstevel@tonic-gate 			ret = ldi_usage_walker_helper(lip, vp, arg, callback);
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 			lhp = lhp->lh_next;
11920Sstevel@tonic-gate 		}
11930Sstevel@tonic-gate 		mutex_exit(&ldi_handle_hash_lock[i]);
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 		if (ret != LDI_USAGE_CONTINUE)
11960Sstevel@tonic-gate 			break;
11970Sstevel@tonic-gate 	}
11980Sstevel@tonic-gate }
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate /*
12010Sstevel@tonic-gate  * LDI Project private interfaces (streams linking interfaces)
12020Sstevel@tonic-gate  *
12030Sstevel@tonic-gate  * Streams supports a type of built in device layering via linking.
12040Sstevel@tonic-gate  * Certain types of streams drivers can be streams multiplexors.
12050Sstevel@tonic-gate  * A streams multiplexor supports the I_LINK/I_PLINK operation.
12060Sstevel@tonic-gate  * These operations allows other streams devices to be linked under the
12070Sstevel@tonic-gate  * multiplexor.  By definition all streams multiplexors are devices
12080Sstevel@tonic-gate  * so this linking is a type of device layering where the multiplexor
12090Sstevel@tonic-gate  * device is layered on top of the device linked below it.
12100Sstevel@tonic-gate  */
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate /*
12130Sstevel@tonic-gate  * ldi_mlink_lh() is invoked when streams are linked using LDI handles.
12140Sstevel@tonic-gate  * It is not used for normal I_LINKs and I_PLINKs using file descriptors.
12150Sstevel@tonic-gate  *
12160Sstevel@tonic-gate  * The streams framework keeps track of links via the file_t of the lower
12170Sstevel@tonic-gate  * stream.  The LDI keeps track of devices using a vnode.  In the case
12180Sstevel@tonic-gate  * of a streams link created via an LDI handle, fnk_lh() allocates
12190Sstevel@tonic-gate  * a file_t that the streams framework can use to track the linkage.
12200Sstevel@tonic-gate  */
12210Sstevel@tonic-gate int
12220Sstevel@tonic-gate ldi_mlink_lh(vnode_t *vp, int cmd, intptr_t arg, cred_t *crp, int *rvalp)
12230Sstevel@tonic-gate {
12240Sstevel@tonic-gate 	struct ldi_handle	*lhp = (struct ldi_handle *)arg;
12250Sstevel@tonic-gate 	vnode_t			*vpdown;
12260Sstevel@tonic-gate 	file_t			*fpdown;
12270Sstevel@tonic-gate 	int			err;
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate 	if (lhp == NULL)
12300Sstevel@tonic-gate 		return (EINVAL);
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 	vpdown = lhp->lh_vp;
12330Sstevel@tonic-gate 	ASSERT(vn_matchops(vpdown, spec_getvnodeops()));
12340Sstevel@tonic-gate 	ASSERT(cmd == _I_PLINK_LH);
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 	/*
12370Sstevel@tonic-gate 	 * create a new lower vnode and a file_t that points to it,
12380Sstevel@tonic-gate 	 * streams linking requires a file_t.  falloc() returns with
12390Sstevel@tonic-gate 	 * fpdown locked.
12400Sstevel@tonic-gate 	 */
12410Sstevel@tonic-gate 	VN_HOLD(vpdown);
12420Sstevel@tonic-gate 	(void) falloc(vpdown, FREAD|FWRITE, &fpdown, NULL);
12430Sstevel@tonic-gate 	mutex_exit(&fpdown->f_tlock);
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate 	/* try to establish the link */
12460Sstevel@tonic-gate 	err = mlink_file(vp, I_PLINK, fpdown, crp, rvalp, 1);
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 	if (err != 0) {
12490Sstevel@tonic-gate 		/* the link failed, free the file_t and release the vnode */
12500Sstevel@tonic-gate 		mutex_enter(&fpdown->f_tlock);
12510Sstevel@tonic-gate 		unfalloc(fpdown);
12520Sstevel@tonic-gate 		VN_RELE(vpdown);
12530Sstevel@tonic-gate 	}
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate 	return (err);
12560Sstevel@tonic-gate }
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate /*
12595331Samw  * ldi_mlink_fp() is invoked for all successful streams linkages created
12600Sstevel@tonic-gate  * via I_LINK and I_PLINK.  ldi_mlink_fp() records the linkage information
12610Sstevel@tonic-gate  * in its internal state so that the devinfo snapshot code has some
12620Sstevel@tonic-gate  * observability into streams device linkage information.
12630Sstevel@tonic-gate  */
12640Sstevel@tonic-gate void
12650Sstevel@tonic-gate ldi_mlink_fp(struct stdata *stp, file_t *fpdown, int lhlink, int type)
12660Sstevel@tonic-gate {
12670Sstevel@tonic-gate 	vnode_t			*vp = fpdown->f_vnode;
12680Sstevel@tonic-gate 	struct snode		*sp, *csp;
12690Sstevel@tonic-gate 	ldi_ident_t		li;
12700Sstevel@tonic-gate 	major_t			major;
12710Sstevel@tonic-gate 	int			ret;
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 	/* if the lower stream is not a device then return */
12740Sstevel@tonic-gate 	if (!vn_matchops(vp, spec_getvnodeops()))
12750Sstevel@tonic-gate 		return;
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate 	LDI_STREAMS_LNK((CE_NOTE, "%s: linking streams "
12804582Scth 	    "stp=0x%p, fpdown=0x%p", "ldi_mlink_fp",
12814582Scth 	    (void *)stp, (void *)fpdown));
12820Sstevel@tonic-gate 
12830Sstevel@tonic-gate 	sp = VTOS(vp);
12840Sstevel@tonic-gate 	csp = VTOS(sp->s_commonvp);
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate 	/* check if this was a plink via a layered handle */
12870Sstevel@tonic-gate 	if (lhlink) {
12880Sstevel@tonic-gate 		/*
12890Sstevel@tonic-gate 		 * increment the common snode s_count.
12900Sstevel@tonic-gate 		 *
12910Sstevel@tonic-gate 		 * this is done because after the link operation there
12920Sstevel@tonic-gate 		 * are two ways that s_count can be decremented.
12930Sstevel@tonic-gate 		 *
12940Sstevel@tonic-gate 		 * when the layered handle used to create the link is
12950Sstevel@tonic-gate 		 * closed, spec_close() is called and it will decrement
12960Sstevel@tonic-gate 		 * s_count in the common snode.  if we don't increment
12970Sstevel@tonic-gate 		 * s_count here then this could cause spec_close() to
12980Sstevel@tonic-gate 		 * actually close the device while it's still linked
12990Sstevel@tonic-gate 		 * under a multiplexer.
13000Sstevel@tonic-gate 		 *
13010Sstevel@tonic-gate 		 * also, when the lower stream is unlinked, closef() is
13020Sstevel@tonic-gate 		 * called for the file_t associated with this snode.
13030Sstevel@tonic-gate 		 * closef() will call spec_close(), which will decrement
13040Sstevel@tonic-gate 		 * s_count.  if we dont't increment s_count here then this
13050Sstevel@tonic-gate 		 * could cause spec_close() to actually close the device
13060Sstevel@tonic-gate 		 * while there may still be valid layered handles
13070Sstevel@tonic-gate 		 * pointing to it.
13080Sstevel@tonic-gate 		 */
13090Sstevel@tonic-gate 		mutex_enter(&csp->s_lock);
13100Sstevel@tonic-gate 		ASSERT(csp->s_count >= 1);
13110Sstevel@tonic-gate 		csp->s_count++;
13120Sstevel@tonic-gate 		mutex_exit(&csp->s_lock);
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 		/*
13150Sstevel@tonic-gate 		 * decrement the f_count.
13160Sstevel@tonic-gate 		 * this is done because the layered driver framework does
13170Sstevel@tonic-gate 		 * not actually cache a copy of the file_t allocated to
13180Sstevel@tonic-gate 		 * do the link.  this is done here instead of in ldi_mlink_lh()
13190Sstevel@tonic-gate 		 * because there is a window in ldi_mlink_lh() between where
13200Sstevel@tonic-gate 		 * milnk_file() returns and we would decrement the f_count
13210Sstevel@tonic-gate 		 * when the stream could be unlinked.
13220Sstevel@tonic-gate 		 */
13230Sstevel@tonic-gate 		mutex_enter(&fpdown->f_tlock);
13240Sstevel@tonic-gate 		fpdown->f_count--;
13250Sstevel@tonic-gate 		mutex_exit(&fpdown->f_tlock);
13260Sstevel@tonic-gate 	}
13270Sstevel@tonic-gate 
13280Sstevel@tonic-gate 	/*
13290Sstevel@tonic-gate 	 * NOTE: here we rely on the streams subsystem not allowing
13300Sstevel@tonic-gate 	 * a stream to be multiplexed more than once.  if this
13310Sstevel@tonic-gate 	 * changes, we break.
13320Sstevel@tonic-gate 	 *
13330Sstevel@tonic-gate 	 * mark the snode/stream as multiplexed
13340Sstevel@tonic-gate 	 */
13350Sstevel@tonic-gate 	mutex_enter(&sp->s_lock);
13360Sstevel@tonic-gate 	ASSERT(!(sp->s_flag & SMUXED));
13370Sstevel@tonic-gate 	sp->s_flag |= SMUXED;
13380Sstevel@tonic-gate 	mutex_exit(&sp->s_lock);
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 	/* get a layered ident for the upper stream */
13410Sstevel@tonic-gate 	if (type == LINKNORMAL) {
13420Sstevel@tonic-gate 		/*
13430Sstevel@tonic-gate 		 * if the link is not persistant then we can associate
13440Sstevel@tonic-gate 		 * the upper stream with a dev_t.  this is because the
13450Sstevel@tonic-gate 		 * upper stream is associated with a vnode, which is
13460Sstevel@tonic-gate 		 * associated with a dev_t and this binding can't change
13470Sstevel@tonic-gate 		 * during the life of the stream.  since the link isn't
13480Sstevel@tonic-gate 		 * persistant once the stream is destroyed the link is
13490Sstevel@tonic-gate 		 * destroyed.  so the dev_t will be valid for the life
13500Sstevel@tonic-gate 		 * of the link.
13510Sstevel@tonic-gate 		 */
13520Sstevel@tonic-gate 		ret = ldi_ident_from_stream(getendq(stp->sd_wrq), &li);
13530Sstevel@tonic-gate 	} else {
13540Sstevel@tonic-gate 		/*
13550Sstevel@tonic-gate 		 * if the link is persistant we can only associate the
13560Sstevel@tonic-gate 		 * link with a driver (and not a dev_t.)  this is
13570Sstevel@tonic-gate 		 * because subsequent opens of the upper device may result
13580Sstevel@tonic-gate 		 * in a different stream (and dev_t) having access to
13590Sstevel@tonic-gate 		 * the lower stream.
13600Sstevel@tonic-gate 		 *
13610Sstevel@tonic-gate 		 * for example, if the upper stream is closed after the
13620Sstevel@tonic-gate 		 * persistant link operation is compleated, a subsequent
13630Sstevel@tonic-gate 		 * open of the upper device will create a new stream which
13640Sstevel@tonic-gate 		 * may have a different dev_t and an unlink operation
13650Sstevel@tonic-gate 		 * can be performed using this new upper stream.
13660Sstevel@tonic-gate 		 */
13670Sstevel@tonic-gate 		ASSERT(type == LINKPERSIST);
13680Sstevel@tonic-gate 		major = getmajor(stp->sd_vnode->v_rdev);
13690Sstevel@tonic-gate 		ret = ldi_ident_from_major(major, &li);
13700Sstevel@tonic-gate 	}
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 	ASSERT(ret == 0);
13730Sstevel@tonic-gate 	(void) handle_alloc(vp, (struct ldi_ident *)li);
13740Sstevel@tonic-gate 	ldi_ident_release(li);
13750Sstevel@tonic-gate }
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate void
13780Sstevel@tonic-gate ldi_munlink_fp(struct stdata *stp, file_t *fpdown, int type)
13790Sstevel@tonic-gate {
13800Sstevel@tonic-gate 	struct ldi_handle	*lhp;
13810Sstevel@tonic-gate 	vnode_t			*vp = (vnode_t *)fpdown->f_vnode;
13820Sstevel@tonic-gate 	struct snode		*sp;
13830Sstevel@tonic-gate 	ldi_ident_t		li;
13840Sstevel@tonic-gate 	major_t			major;
13850Sstevel@tonic-gate 	int			ret;
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate 	/* if the lower stream is not a device then return */
13880Sstevel@tonic-gate 	if (!vn_matchops(vp, spec_getvnodeops()))
13890Sstevel@tonic-gate 		return;
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
13920Sstevel@tonic-gate 	ASSERT((type == LINKNORMAL) || (type == LINKPERSIST));
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate 	LDI_STREAMS_LNK((CE_NOTE, "%s: unlinking streams "
13954582Scth 	    "stp=0x%p, fpdown=0x%p", "ldi_munlink_fp",
13964582Scth 	    (void *)stp, (void *)fpdown));
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate 	/*
13990Sstevel@tonic-gate 	 * NOTE: here we rely on the streams subsystem not allowing
14000Sstevel@tonic-gate 	 * a stream to be multiplexed more than once.  if this
14010Sstevel@tonic-gate 	 * changes, we break.
14020Sstevel@tonic-gate 	 *
14030Sstevel@tonic-gate 	 * mark the snode/stream as not multiplexed
14040Sstevel@tonic-gate 	 */
14050Sstevel@tonic-gate 	sp = VTOS(vp);
14060Sstevel@tonic-gate 	mutex_enter(&sp->s_lock);
14070Sstevel@tonic-gate 	ASSERT(sp->s_flag & SMUXED);
14080Sstevel@tonic-gate 	sp->s_flag &= ~SMUXED;
14090Sstevel@tonic-gate 	mutex_exit(&sp->s_lock);
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 	/*
14120Sstevel@tonic-gate 	 * clear the owner for this snode
14130Sstevel@tonic-gate 	 * see the comment in ldi_mlink_fp() for information about how
14140Sstevel@tonic-gate 	 * the ident is allocated
14150Sstevel@tonic-gate 	 */
14160Sstevel@tonic-gate 	if (type == LINKNORMAL) {
14170Sstevel@tonic-gate 		ret = ldi_ident_from_stream(getendq(stp->sd_wrq), &li);
14180Sstevel@tonic-gate 	} else {
14190Sstevel@tonic-gate 		ASSERT(type == LINKPERSIST);
14200Sstevel@tonic-gate 		major = getmajor(stp->sd_vnode->v_rdev);
14210Sstevel@tonic-gate 		ret = ldi_ident_from_major(major, &li);
14220Sstevel@tonic-gate 	}
14230Sstevel@tonic-gate 
14240Sstevel@tonic-gate 	ASSERT(ret == 0);
14250Sstevel@tonic-gate 	lhp = handle_find(vp, (struct ldi_ident *)li);
14260Sstevel@tonic-gate 	handle_release(lhp);
14270Sstevel@tonic-gate 	ldi_ident_release(li);
14280Sstevel@tonic-gate }
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate /*
14310Sstevel@tonic-gate  * LDI Consolidation private interfaces
14320Sstevel@tonic-gate  */
14330Sstevel@tonic-gate int
14340Sstevel@tonic-gate ldi_ident_from_mod(struct modlinkage *modlp, ldi_ident_t *lip)
14350Sstevel@tonic-gate {
14360Sstevel@tonic-gate 	struct modctl		*modp;
14370Sstevel@tonic-gate 	major_t			major;
14380Sstevel@tonic-gate 	char			*name;
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate 	if ((modlp == NULL) || (lip == NULL))
14410Sstevel@tonic-gate 		return (EINVAL);
14420Sstevel@tonic-gate 
14430Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
14440Sstevel@tonic-gate 
14450Sstevel@tonic-gate 	modp = mod_getctl(modlp);
14460Sstevel@tonic-gate 	if (modp == NULL)
14470Sstevel@tonic-gate 		return (EINVAL);
14480Sstevel@tonic-gate 	name = modp->mod_modname;
14490Sstevel@tonic-gate 	if (name == NULL)
14500Sstevel@tonic-gate 		return (EINVAL);
14510Sstevel@tonic-gate 	major = mod_name_to_major(name);
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate 	*lip = (ldi_ident_t)ident_alloc(name, NULL, DDI_DEV_T_NONE, major);
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate 	LDI_ALLOCFREE((CE_WARN, "%s: li=0x%p, mod=%s",
14564582Scth 	    "ldi_ident_from_mod", (void *)*lip, name));
14570Sstevel@tonic-gate 
14580Sstevel@tonic-gate 	return (0);
14590Sstevel@tonic-gate }
14600Sstevel@tonic-gate 
14610Sstevel@tonic-gate ldi_ident_t
14620Sstevel@tonic-gate ldi_ident_from_anon()
14630Sstevel@tonic-gate {
14640Sstevel@tonic-gate 	ldi_ident_t	lip;
14650Sstevel@tonic-gate 
14660Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate 	lip = (ldi_ident_t)ident_alloc("genunix", NULL, DDI_DEV_T_NONE, -1);
14690Sstevel@tonic-gate 
14700Sstevel@tonic-gate 	LDI_ALLOCFREE((CE_WARN, "%s: li=0x%p, mod=%s",
14714582Scth 	    "ldi_ident_from_anon", (void *)lip, "genunix"));
14720Sstevel@tonic-gate 
14730Sstevel@tonic-gate 	return (lip);
14740Sstevel@tonic-gate }
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate 
14770Sstevel@tonic-gate /*
14780Sstevel@tonic-gate  * LDI Public interfaces
14790Sstevel@tonic-gate  */
14800Sstevel@tonic-gate int
14810Sstevel@tonic-gate ldi_ident_from_stream(struct queue *sq, ldi_ident_t *lip)
14820Sstevel@tonic-gate {
14830Sstevel@tonic-gate 	struct stdata		*stp;
14840Sstevel@tonic-gate 	dev_t			dev;
14850Sstevel@tonic-gate 	char			*name;
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate 	if ((sq == NULL) || (lip == NULL))
14880Sstevel@tonic-gate 		return (EINVAL);
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
14910Sstevel@tonic-gate 
14920Sstevel@tonic-gate 	stp = sq->q_stream;
14930Sstevel@tonic-gate 	if (!vn_matchops(stp->sd_vnode, spec_getvnodeops()))
14940Sstevel@tonic-gate 		return (EINVAL);
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate 	dev = stp->sd_vnode->v_rdev;
14970Sstevel@tonic-gate 	name = mod_major_to_name(getmajor(dev));
14980Sstevel@tonic-gate 	if (name == NULL)
14990Sstevel@tonic-gate 		return (EINVAL);
15000Sstevel@tonic-gate 	*lip = (ldi_ident_t)ident_alloc(name, NULL, dev, -1);
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate 	LDI_ALLOCFREE((CE_WARN,
15034582Scth 	    "%s: li=0x%p, mod=%s, minor=0x%x, stp=0x%p",
15044582Scth 	    "ldi_ident_from_stream", (void *)*lip, name, getminor(dev),
15054582Scth 	    (void *)stp));
15060Sstevel@tonic-gate 
15070Sstevel@tonic-gate 	return (0);
15080Sstevel@tonic-gate }
15090Sstevel@tonic-gate 
15100Sstevel@tonic-gate int
15110Sstevel@tonic-gate ldi_ident_from_dev(dev_t dev, ldi_ident_t *lip)
15120Sstevel@tonic-gate {
15130Sstevel@tonic-gate 	char			*name;
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate 	if (lip == NULL)
15160Sstevel@tonic-gate 		return (EINVAL);
15170Sstevel@tonic-gate 
15180Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 	name = mod_major_to_name(getmajor(dev));
15210Sstevel@tonic-gate 	if (name == NULL)
15220Sstevel@tonic-gate 		return (EINVAL);
15230Sstevel@tonic-gate 	*lip = (ldi_ident_t)ident_alloc(name, NULL, dev, -1);
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate 	LDI_ALLOCFREE((CE_WARN,
15264582Scth 	    "%s: li=0x%p, mod=%s, minor=0x%x",
15274582Scth 	    "ldi_ident_from_dev", (void *)*lip, name, getminor(dev)));
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 	return (0);
15300Sstevel@tonic-gate }
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate int
15330Sstevel@tonic-gate ldi_ident_from_dip(dev_info_t *dip, ldi_ident_t *lip)
15340Sstevel@tonic-gate {
15350Sstevel@tonic-gate 	struct dev_info		*devi = (struct dev_info *)dip;
15360Sstevel@tonic-gate 	char			*name;
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 	if ((dip == NULL) || (lip == NULL))
15390Sstevel@tonic-gate 		return (EINVAL);
15400Sstevel@tonic-gate 
15410Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate 	name = mod_major_to_name(devi->devi_major);
15440Sstevel@tonic-gate 	if (name == NULL)
15450Sstevel@tonic-gate 		return (EINVAL);
15460Sstevel@tonic-gate 	*lip = (ldi_ident_t)ident_alloc(name, dip, DDI_DEV_T_NONE, -1);
15470Sstevel@tonic-gate 
15480Sstevel@tonic-gate 	LDI_ALLOCFREE((CE_WARN,
15494582Scth 	    "%s: li=0x%p, mod=%s, dip=0x%p",
15504582Scth 	    "ldi_ident_from_dip", (void *)*lip, name, (void *)devi));
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 	return (0);
15530Sstevel@tonic-gate }
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate int
15560Sstevel@tonic-gate ldi_ident_from_major(major_t major, ldi_ident_t *lip)
15570Sstevel@tonic-gate {
15580Sstevel@tonic-gate 	char			*name;
15590Sstevel@tonic-gate 
15600Sstevel@tonic-gate 	if (lip == NULL)
15610Sstevel@tonic-gate 		return (EINVAL);
15620Sstevel@tonic-gate 
15630Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate 	name = mod_major_to_name(major);
15660Sstevel@tonic-gate 	if (name == NULL)
15670Sstevel@tonic-gate 		return (EINVAL);
15680Sstevel@tonic-gate 	*lip = (ldi_ident_t)ident_alloc(name, NULL, DDI_DEV_T_NONE, major);
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate 	LDI_ALLOCFREE((CE_WARN,
15714582Scth 	    "%s: li=0x%p, mod=%s",
15724582Scth 	    "ldi_ident_from_major", (void *)*lip, name));
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 	return (0);
15750Sstevel@tonic-gate }
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate void
15780Sstevel@tonic-gate ldi_ident_release(ldi_ident_t li)
15790Sstevel@tonic-gate {
15800Sstevel@tonic-gate 	struct ldi_ident	*ident = (struct ldi_ident *)li;
15810Sstevel@tonic-gate 	char			*name;
15820Sstevel@tonic-gate 
15830Sstevel@tonic-gate 	if (li == NULL)
15840Sstevel@tonic-gate 		return;
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
15870Sstevel@tonic-gate 
15880Sstevel@tonic-gate 	name = ident->li_modname;
15890Sstevel@tonic-gate 
15900Sstevel@tonic-gate 	LDI_ALLOCFREE((CE_WARN,
15914582Scth 	    "%s: li=0x%p, mod=%s",
15924582Scth 	    "ldi_ident_release", (void *)li, name));
15930Sstevel@tonic-gate 
15940Sstevel@tonic-gate 	ident_release((struct ldi_ident *)li);
15950Sstevel@tonic-gate }
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate /* get a handle to a device by dev_t and otyp */
15980Sstevel@tonic-gate int
15990Sstevel@tonic-gate ldi_open_by_dev(dev_t *devp, int otyp, int flag, cred_t *cr,
16000Sstevel@tonic-gate     ldi_handle_t *lhp, ldi_ident_t li)
16010Sstevel@tonic-gate {
16020Sstevel@tonic-gate 	struct ldi_ident	*lip = (struct ldi_ident *)li;
16034582Scth 	int			ret;
16040Sstevel@tonic-gate 	vnode_t			*vp;
16050Sstevel@tonic-gate 
16060Sstevel@tonic-gate 	/* sanity check required input parameters */
16070Sstevel@tonic-gate 	if ((devp == NULL) || (!OTYP_VALID(otyp)) || (cr == NULL) ||
16080Sstevel@tonic-gate 	    (lhp == NULL) || (lip == NULL))
16090Sstevel@tonic-gate 		return (EINVAL);
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
16120Sstevel@tonic-gate 
16130Sstevel@tonic-gate 	if ((ret = ldi_vp_from_dev(*devp, otyp, &vp)) != 0)
16140Sstevel@tonic-gate 		return (ret);
16150Sstevel@tonic-gate 
16160Sstevel@tonic-gate 	if ((ret = ldi_open_by_vp(&vp, flag, cr, lhp, lip)) == 0) {
16170Sstevel@tonic-gate 		*devp = vp->v_rdev;
16180Sstevel@tonic-gate 	}
16190Sstevel@tonic-gate 	VN_RELE(vp);
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate 	return (ret);
16220Sstevel@tonic-gate }
16230Sstevel@tonic-gate 
16240Sstevel@tonic-gate /* get a handle to a device by pathname */
16250Sstevel@tonic-gate int
16260Sstevel@tonic-gate ldi_open_by_name(char *pathname, int flag, cred_t *cr,
16270Sstevel@tonic-gate     ldi_handle_t *lhp, ldi_ident_t li)
16280Sstevel@tonic-gate {
16290Sstevel@tonic-gate 	struct ldi_ident	*lip = (struct ldi_ident *)li;
16304582Scth 	int			ret;
16310Sstevel@tonic-gate 	vnode_t			*vp;
16320Sstevel@tonic-gate 
16330Sstevel@tonic-gate 	/* sanity check required input parameters */
16340Sstevel@tonic-gate 	if ((pathname == NULL) || (*pathname != '/') ||
16350Sstevel@tonic-gate 	    (cr == NULL) || (lhp == NULL) || (lip == NULL))
16360Sstevel@tonic-gate 		return (EINVAL);
16370Sstevel@tonic-gate 
16380Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
16390Sstevel@tonic-gate 
16400Sstevel@tonic-gate 	if ((ret = ldi_vp_from_name(pathname, &vp)) != 0)
16410Sstevel@tonic-gate 		return (ret);
16420Sstevel@tonic-gate 
16430Sstevel@tonic-gate 	ret = ldi_open_by_vp(&vp, flag, cr, lhp, lip);
16440Sstevel@tonic-gate 	VN_RELE(vp);
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate 	return (ret);
16470Sstevel@tonic-gate }
16480Sstevel@tonic-gate 
16490Sstevel@tonic-gate /* get a handle to a device by devid and minor_name */
16500Sstevel@tonic-gate int
16510Sstevel@tonic-gate ldi_open_by_devid(ddi_devid_t devid, char *minor_name,
16520Sstevel@tonic-gate     int flag, cred_t *cr, ldi_handle_t *lhp, ldi_ident_t li)
16530Sstevel@tonic-gate {
16540Sstevel@tonic-gate 	struct ldi_ident	*lip = (struct ldi_ident *)li;
16550Sstevel@tonic-gate 	int			ret;
16560Sstevel@tonic-gate 	vnode_t			*vp;
16570Sstevel@tonic-gate 
16580Sstevel@tonic-gate 	/* sanity check required input parameters */
16590Sstevel@tonic-gate 	if ((minor_name == NULL) || (cr == NULL) ||
16600Sstevel@tonic-gate 	    (lhp == NULL) || (lip == NULL))
16610Sstevel@tonic-gate 		return (EINVAL);
16620Sstevel@tonic-gate 
16630Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
16640Sstevel@tonic-gate 
16650Sstevel@tonic-gate 	if ((ret = ldi_vp_from_devid(devid, minor_name, &vp)) != 0)
16660Sstevel@tonic-gate 		return (ret);
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate 	ret = ldi_open_by_vp(&vp, flag, cr, lhp, lip);
16690Sstevel@tonic-gate 	VN_RELE(vp);
16700Sstevel@tonic-gate 
16710Sstevel@tonic-gate 	return (ret);
16720Sstevel@tonic-gate }
16730Sstevel@tonic-gate 
16740Sstevel@tonic-gate int
16750Sstevel@tonic-gate ldi_close(ldi_handle_t lh, int flag, cred_t *cr)
16760Sstevel@tonic-gate {
16770Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
16780Sstevel@tonic-gate 	struct ldi_event	*lep;
16794582Scth 	int			err = 0;
16804845Svikram 	int			notify = 0;
16814845Svikram 	list_t			*listp;
16824845Svikram 	ldi_ev_callback_impl_t	*lecp;
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate 	if (lh == NULL)
16850Sstevel@tonic-gate 		return (EINVAL);
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
16880Sstevel@tonic-gate 
16890Sstevel@tonic-gate 	/* Flush back any dirty pages associated with the device. */
16900Sstevel@tonic-gate 	if (handlep->lh_type & LH_CBDEV) {
16910Sstevel@tonic-gate 		vnode_t	*cvp = common_specvp(handlep->lh_vp);
16920Sstevel@tonic-gate 		dev_t	dev = cvp->v_rdev;
16930Sstevel@tonic-gate 
16945331Samw 		(void) VOP_PUTPAGE(cvp, 0, 0, B_INVAL, kcred, NULL);
16950Sstevel@tonic-gate 		bflush(dev);
16960Sstevel@tonic-gate 	}
16970Sstevel@tonic-gate 
16984845Svikram #ifdef	LDI_OBSOLETE_EVENT
16994845Svikram 
17000Sstevel@tonic-gate 	/*
17010Sstevel@tonic-gate 	 * Any event handlers should have been unregistered by the
17020Sstevel@tonic-gate 	 * time ldi_close() is called.  If they haven't then it's a
17030Sstevel@tonic-gate 	 * bug.
17040Sstevel@tonic-gate 	 *
17050Sstevel@tonic-gate 	 * In a debug kernel we'll panic to make the problem obvious.
17060Sstevel@tonic-gate 	 */
17070Sstevel@tonic-gate 	ASSERT(handlep->lh_events == NULL);
17080Sstevel@tonic-gate 
17090Sstevel@tonic-gate 	/*
17100Sstevel@tonic-gate 	 * On a production kernel we'll "do the right thing" (unregister
17110Sstevel@tonic-gate 	 * the event handlers) and then complain about having to do the
17120Sstevel@tonic-gate 	 * work ourselves.
17130Sstevel@tonic-gate 	 */
17140Sstevel@tonic-gate 	while ((lep = handlep->lh_events) != NULL) {
17150Sstevel@tonic-gate 		err = 1;
17160Sstevel@tonic-gate 		(void) ldi_remove_event_handler(lh, (ldi_callback_id_t)lep);
17170Sstevel@tonic-gate 	}
17180Sstevel@tonic-gate 	if (err) {
17190Sstevel@tonic-gate 		struct ldi_ident *lip = handlep->lh_ident;
17200Sstevel@tonic-gate 		ASSERT(lip != NULL);
17210Sstevel@tonic-gate 		cmn_err(CE_NOTE, "ldi err: %s "
17220Sstevel@tonic-gate 		    "failed to unregister layered event handlers before "
17230Sstevel@tonic-gate 		    "closing devices", lip->li_modname);
17240Sstevel@tonic-gate 	}
17254845Svikram #endif
17260Sstevel@tonic-gate 
17270Sstevel@tonic-gate 	/* do a layered close on the device */
17285331Samw 	err = VOP_CLOSE(handlep->lh_vp, flag | FKLYR, 1, (offset_t)0, cr, NULL);
17290Sstevel@tonic-gate 
17300Sstevel@tonic-gate 	LDI_OPENCLOSE((CE_WARN, "%s: lh=0x%p", "ldi close", (void *)lh));
17310Sstevel@tonic-gate 
17320Sstevel@tonic-gate 	/*
17334845Svikram 	 * Search the event callback list for callbacks with this
17344845Svikram 	 * handle. There are 2 cases
17354845Svikram 	 * 1. Called in the context of a notify. The handle consumer
17364845Svikram 	 *    is releasing its hold on the device to allow a reconfiguration
17374845Svikram 	 *    of the device. Simply NULL out the handle and the notify callback.
17384845Svikram 	 *    The finalize callback is still available so that the consumer
17394845Svikram 	 *    knows of the final disposition of the device.
17404845Svikram 	 * 2. Not called in the context of notify. NULL out the handle as well
17414845Svikram 	 *    as the notify and finalize callbacks. Since the consumer has
17424845Svikram 	 *    closed the handle, we assume it is not interested in the
17434845Svikram 	 *    notify and finalize callbacks.
17444845Svikram 	 */
17454845Svikram 	ldi_ev_lock();
17464845Svikram 
17474845Svikram 	if (handlep->lh_flags & LH_FLAGS_NOTIFY)
17484845Svikram 		notify = 1;
17494845Svikram 	listp = &ldi_ev_callback_list.le_head;
17504845Svikram 	for (lecp = list_head(listp); lecp; lecp = list_next(listp, lecp)) {
17514845Svikram 		if (lecp->lec_lhp != handlep)
17524845Svikram 			continue;
17534845Svikram 		lecp->lec_lhp = NULL;
17544845Svikram 		lecp->lec_notify = NULL;
17554845Svikram 		LDI_EVDBG((CE_NOTE, "ldi_close: NULLed lh and notify"));
17564845Svikram 		if (!notify) {
17574845Svikram 			LDI_EVDBG((CE_NOTE, "ldi_close: NULLed finalize"));
17584845Svikram 			lecp->lec_finalize = NULL;
17594845Svikram 		}
17604845Svikram 	}
17614845Svikram 
17624845Svikram 	if (notify)
17634845Svikram 		handlep->lh_flags &= ~LH_FLAGS_NOTIFY;
17644845Svikram 	ldi_ev_unlock();
17654845Svikram 
17664845Svikram 	/*
17670Sstevel@tonic-gate 	 * Free the handle even if the device close failed.  why?
17680Sstevel@tonic-gate 	 *
17690Sstevel@tonic-gate 	 * If the device close failed we can't really make assumptions
17700Sstevel@tonic-gate 	 * about the devices state so we shouldn't allow access to the
17710Sstevel@tonic-gate 	 * device via this handle any more.  If the device consumer wants
17720Sstevel@tonic-gate 	 * to access the device again they should open it again.
17730Sstevel@tonic-gate 	 *
17740Sstevel@tonic-gate 	 * This is the same way file/device close failures are handled
17750Sstevel@tonic-gate 	 * in other places like spec_close() and closeandsetf().
17760Sstevel@tonic-gate 	 */
17770Sstevel@tonic-gate 	handle_release(handlep);
17780Sstevel@tonic-gate 	return (err);
17790Sstevel@tonic-gate }
17800Sstevel@tonic-gate 
17810Sstevel@tonic-gate int
17820Sstevel@tonic-gate ldi_read(ldi_handle_t lh, struct uio *uiop, cred_t *credp)
17830Sstevel@tonic-gate {
17840Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
17850Sstevel@tonic-gate 	vnode_t			*vp;
17860Sstevel@tonic-gate 	dev_t			dev;
17870Sstevel@tonic-gate 	int			ret;
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate 	if (lh == NULL)
17900Sstevel@tonic-gate 		return (EINVAL);
17910Sstevel@tonic-gate 
17920Sstevel@tonic-gate 	vp = handlep->lh_vp;
17930Sstevel@tonic-gate 	dev = vp->v_rdev;
17940Sstevel@tonic-gate 	if (handlep->lh_type & LH_CBDEV) {
17950Sstevel@tonic-gate 		ret = cdev_read(dev, uiop, credp);
17960Sstevel@tonic-gate 	} else if (handlep->lh_type & LH_STREAM) {
17970Sstevel@tonic-gate 		ret = strread(vp, uiop, credp);
17980Sstevel@tonic-gate 	} else {
17990Sstevel@tonic-gate 		return (ENOTSUP);
18000Sstevel@tonic-gate 	}
18010Sstevel@tonic-gate 	return (ret);
18020Sstevel@tonic-gate }
18030Sstevel@tonic-gate 
18040Sstevel@tonic-gate int
18050Sstevel@tonic-gate ldi_write(ldi_handle_t lh, struct uio *uiop, cred_t *credp)
18060Sstevel@tonic-gate {
18070Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
18080Sstevel@tonic-gate 	vnode_t			*vp;
18090Sstevel@tonic-gate 	dev_t			dev;
18100Sstevel@tonic-gate 	int			ret;
18110Sstevel@tonic-gate 
18120Sstevel@tonic-gate 	if (lh == NULL)
18130Sstevel@tonic-gate 		return (EINVAL);
18140Sstevel@tonic-gate 
18150Sstevel@tonic-gate 	vp = handlep->lh_vp;
18160Sstevel@tonic-gate 	dev = vp->v_rdev;
18170Sstevel@tonic-gate 	if (handlep->lh_type & LH_CBDEV) {
18180Sstevel@tonic-gate 		ret = cdev_write(dev, uiop, credp);
18190Sstevel@tonic-gate 	} else if (handlep->lh_type & LH_STREAM) {
18200Sstevel@tonic-gate 		ret = strwrite(vp, uiop, credp);
18210Sstevel@tonic-gate 	} else {
18220Sstevel@tonic-gate 		return (ENOTSUP);
18230Sstevel@tonic-gate 	}
18240Sstevel@tonic-gate 	return (ret);
18250Sstevel@tonic-gate }
18260Sstevel@tonic-gate 
18270Sstevel@tonic-gate int
18280Sstevel@tonic-gate ldi_get_size(ldi_handle_t lh, uint64_t *sizep)
18290Sstevel@tonic-gate {
18304582Scth 	int			otyp;
18310Sstevel@tonic-gate 	uint_t			value;
18320Sstevel@tonic-gate 	int64_t			drv_prop64;
18330Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
18344582Scth 	uint_t			blksize;
18354582Scth 	int			blkshift;
18360Sstevel@tonic-gate 
18370Sstevel@tonic-gate 
18380Sstevel@tonic-gate 	if ((lh == NULL) || (sizep == NULL))
18390Sstevel@tonic-gate 		return (DDI_FAILURE);
18400Sstevel@tonic-gate 
18410Sstevel@tonic-gate 	if (handlep->lh_type & LH_STREAM)
18420Sstevel@tonic-gate 		return (DDI_FAILURE);
18430Sstevel@tonic-gate 
18440Sstevel@tonic-gate 	/*
18450Sstevel@tonic-gate 	 * Determine device type (char or block).
18460Sstevel@tonic-gate 	 * Character devices support Size/size
18470Sstevel@tonic-gate 	 * property value. Block devices may support
18480Sstevel@tonic-gate 	 * Nblocks/nblocks or Size/size property value.
18490Sstevel@tonic-gate 	 */
18500Sstevel@tonic-gate 	if ((ldi_get_otyp(lh, &otyp)) != 0)
18510Sstevel@tonic-gate 		return (DDI_FAILURE);
18520Sstevel@tonic-gate 
18530Sstevel@tonic-gate 	if (otyp == OTYP_BLK) {
18540Sstevel@tonic-gate 		if (ldi_prop_exists(lh,
18554582Scth 		    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "Nblocks")) {
18560Sstevel@tonic-gate 
18570Sstevel@tonic-gate 			drv_prop64 = ldi_prop_get_int64(lh,
18580Sstevel@tonic-gate 			    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
18590Sstevel@tonic-gate 			    "Nblocks", 0);
18604582Scth 			blksize = ldi_prop_get_int(lh,
18614582Scth 			    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
18624582Scth 			    "blksize", DEV_BSIZE);
18634582Scth 			if (blksize == DEV_BSIZE)
18644582Scth 				blksize = ldi_prop_get_int(lh, LDI_DEV_T_ANY |
18654582Scth 				    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
18664582Scth 				    "device-blksize", DEV_BSIZE);
18674582Scth 
18684582Scth 			/* blksize must be a power of two */
18694582Scth 			ASSERT(BIT_ONLYONESET(blksize));
18704582Scth 			blkshift = highbit(blksize) - 1;
18714582Scth 
18724582Scth 			/*
18734582Scth 			 * We don't support Nblocks values that don't have
18744582Scth 			 * an accurate uint64_t byte count representation.
18754582Scth 			 */
18764582Scth 			if ((uint64_t)drv_prop64 >= (UINT64_MAX >> blkshift))
18774582Scth 				return (DDI_FAILURE);
18784582Scth 
18794582Scth 			*sizep = (uint64_t)
18804582Scth 			    (((u_offset_t)drv_prop64) << blkshift);
18810Sstevel@tonic-gate 			return (DDI_SUCCESS);
18820Sstevel@tonic-gate 		}
18830Sstevel@tonic-gate 
18840Sstevel@tonic-gate 		if (ldi_prop_exists(lh,
18854582Scth 		    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "nblocks")) {
18860Sstevel@tonic-gate 
18870Sstevel@tonic-gate 			value = ldi_prop_get_int(lh,
18880Sstevel@tonic-gate 			    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
18890Sstevel@tonic-gate 			    "nblocks", 0);
18904582Scth 			blksize = ldi_prop_get_int(lh,
18914582Scth 			    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
18924582Scth 			    "blksize", DEV_BSIZE);
18934582Scth 			if (blksize == DEV_BSIZE)
18944582Scth 				blksize = ldi_prop_get_int(lh, LDI_DEV_T_ANY |
18954582Scth 				    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
18964582Scth 				    "device-blksize", DEV_BSIZE);
18974582Scth 
18984582Scth 			/* blksize must be a power of two */
18994582Scth 			ASSERT(BIT_ONLYONESET(blksize));
19004582Scth 			blkshift = highbit(blksize) - 1;
19014582Scth 
19024582Scth 			/*
19034582Scth 			 * We don't support nblocks values that don't have an
19044582Scth 			 * accurate uint64_t byte count representation.
19054582Scth 			 */
19064582Scth 			if ((uint64_t)value >= (UINT64_MAX >> blkshift))
19074582Scth 				return (DDI_FAILURE);
19084582Scth 
19094582Scth 			*sizep = (uint64_t)
19104582Scth 			    (((u_offset_t)value) << blkshift);
19110Sstevel@tonic-gate 			return (DDI_SUCCESS);
19120Sstevel@tonic-gate 		}
19130Sstevel@tonic-gate 	}
19140Sstevel@tonic-gate 
19150Sstevel@tonic-gate 	if (ldi_prop_exists(lh,
19164582Scth 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "Size")) {
19170Sstevel@tonic-gate 
19180Sstevel@tonic-gate 		drv_prop64 = ldi_prop_get_int64(lh,
19190Sstevel@tonic-gate 		    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "Size", 0);
19200Sstevel@tonic-gate 		*sizep = (uint64_t)drv_prop64;
19210Sstevel@tonic-gate 		return (DDI_SUCCESS);
19220Sstevel@tonic-gate 	}
19230Sstevel@tonic-gate 
19240Sstevel@tonic-gate 	if (ldi_prop_exists(lh,
19254582Scth 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "size")) {
19260Sstevel@tonic-gate 
19270Sstevel@tonic-gate 		value = ldi_prop_get_int(lh,
19280Sstevel@tonic-gate 		    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "size", 0);
19290Sstevel@tonic-gate 		*sizep = (uint64_t)value;
19300Sstevel@tonic-gate 		return (DDI_SUCCESS);
19310Sstevel@tonic-gate 	}
19320Sstevel@tonic-gate 
19330Sstevel@tonic-gate 	/* unable to determine device size */
19340Sstevel@tonic-gate 	return (DDI_FAILURE);
19350Sstevel@tonic-gate }
19360Sstevel@tonic-gate 
19370Sstevel@tonic-gate int
19380Sstevel@tonic-gate ldi_ioctl(ldi_handle_t lh, int cmd, intptr_t arg, int mode,
19390Sstevel@tonic-gate 	cred_t *cr, int *rvalp)
19400Sstevel@tonic-gate {
19410Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
19420Sstevel@tonic-gate 	vnode_t			*vp;
19430Sstevel@tonic-gate 	dev_t			dev;
19440Sstevel@tonic-gate 	int			ret, copymode;
19450Sstevel@tonic-gate 
19460Sstevel@tonic-gate 	if (lh == NULL)
19470Sstevel@tonic-gate 		return (EINVAL);
19480Sstevel@tonic-gate 
19490Sstevel@tonic-gate 	/*
19500Sstevel@tonic-gate 	 * if the data pointed to by arg is located in the kernel then
19510Sstevel@tonic-gate 	 * make sure the FNATIVE flag is set.
19520Sstevel@tonic-gate 	 */
19530Sstevel@tonic-gate 	if (mode & FKIOCTL)
19540Sstevel@tonic-gate 		mode = (mode & ~FMODELS) | FNATIVE | FKIOCTL;
19550Sstevel@tonic-gate 
19560Sstevel@tonic-gate 	vp = handlep->lh_vp;
19570Sstevel@tonic-gate 	dev = vp->v_rdev;
19580Sstevel@tonic-gate 	if (handlep->lh_type & LH_CBDEV) {
19590Sstevel@tonic-gate 		ret = cdev_ioctl(dev, cmd, arg, mode, cr, rvalp);
19600Sstevel@tonic-gate 	} else if (handlep->lh_type & LH_STREAM) {
19610Sstevel@tonic-gate 		copymode = (mode & FKIOCTL) ? K_TO_K : U_TO_K;
19620Sstevel@tonic-gate 
19630Sstevel@tonic-gate 		/*
19640Sstevel@tonic-gate 		 * if we get an I_PLINK from within the kernel the
19650Sstevel@tonic-gate 		 * arg is a layered handle pointer instead of
19660Sstevel@tonic-gate 		 * a file descriptor, so we translate this ioctl
19670Sstevel@tonic-gate 		 * into a private one that can handle this.
19680Sstevel@tonic-gate 		 */
19690Sstevel@tonic-gate 		if ((mode & FKIOCTL) && (cmd == I_PLINK))
19700Sstevel@tonic-gate 			cmd = _I_PLINK_LH;
19710Sstevel@tonic-gate 
19720Sstevel@tonic-gate 		ret = strioctl(vp, cmd, arg, mode, copymode, cr, rvalp);
19730Sstevel@tonic-gate 	} else {
19740Sstevel@tonic-gate 		return (ENOTSUP);
19750Sstevel@tonic-gate 	}
19760Sstevel@tonic-gate 
19770Sstevel@tonic-gate 	return (ret);
19780Sstevel@tonic-gate }
19790Sstevel@tonic-gate 
19800Sstevel@tonic-gate int
19810Sstevel@tonic-gate ldi_poll(ldi_handle_t lh, short events, int anyyet, short *reventsp,
19820Sstevel@tonic-gate     struct pollhead **phpp)
19830Sstevel@tonic-gate {
19840Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
19850Sstevel@tonic-gate 	vnode_t			*vp;
19860Sstevel@tonic-gate 	dev_t			dev;
19870Sstevel@tonic-gate 	int			ret;
19880Sstevel@tonic-gate 
19890Sstevel@tonic-gate 	if (lh == NULL)
19900Sstevel@tonic-gate 		return (EINVAL);
19910Sstevel@tonic-gate 
19920Sstevel@tonic-gate 	vp = handlep->lh_vp;
19930Sstevel@tonic-gate 	dev = vp->v_rdev;
19940Sstevel@tonic-gate 	if (handlep->lh_type & LH_CBDEV) {
19950Sstevel@tonic-gate 		ret = cdev_poll(dev, events, anyyet, reventsp, phpp);
19960Sstevel@tonic-gate 	} else if (handlep->lh_type & LH_STREAM) {
19970Sstevel@tonic-gate 		ret = strpoll(vp->v_stream, events, anyyet, reventsp, phpp);
19980Sstevel@tonic-gate 	} else {
19990Sstevel@tonic-gate 		return (ENOTSUP);
20000Sstevel@tonic-gate 	}
20010Sstevel@tonic-gate 
20020Sstevel@tonic-gate 	return (ret);
20030Sstevel@tonic-gate }
20040Sstevel@tonic-gate 
20050Sstevel@tonic-gate int
20060Sstevel@tonic-gate ldi_prop_op(ldi_handle_t lh, ddi_prop_op_t prop_op,
20070Sstevel@tonic-gate 	int flags, char *name, caddr_t valuep, int *length)
20080Sstevel@tonic-gate {
20090Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
20100Sstevel@tonic-gate 	dev_t			dev;
20110Sstevel@tonic-gate 	dev_info_t		*dip;
20120Sstevel@tonic-gate 	int			ret;
20130Sstevel@tonic-gate 	struct snode		*csp;
20140Sstevel@tonic-gate 
20150Sstevel@tonic-gate 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
20160Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
20170Sstevel@tonic-gate 
20180Sstevel@tonic-gate 	if ((prop_op != PROP_LEN) && (valuep == NULL))
20190Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
20200Sstevel@tonic-gate 
20210Sstevel@tonic-gate 	if (length == NULL)
20220Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
20230Sstevel@tonic-gate 
20240Sstevel@tonic-gate 	/*
20250Sstevel@tonic-gate 	 * try to find the associated dip,
20260Sstevel@tonic-gate 	 * this places a hold on the driver
20270Sstevel@tonic-gate 	 */
20280Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
20290Sstevel@tonic-gate 
20300Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
20310Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
20320Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
20330Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
20340Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
20350Sstevel@tonic-gate 	if (dip == NULL)
20360Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
20370Sstevel@tonic-gate 
20380Sstevel@tonic-gate 	if (dip == NULL)
20390Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
20400Sstevel@tonic-gate 
20410Sstevel@tonic-gate 	ret = i_ldi_prop_op(dev, dip, prop_op, flags, name, valuep, length);
20420Sstevel@tonic-gate 	ddi_release_devi(dip);
20430Sstevel@tonic-gate 
20440Sstevel@tonic-gate 	return (ret);
20450Sstevel@tonic-gate }
20460Sstevel@tonic-gate 
20470Sstevel@tonic-gate int
20480Sstevel@tonic-gate ldi_strategy(ldi_handle_t lh, struct buf *bp)
20490Sstevel@tonic-gate {
20500Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
20510Sstevel@tonic-gate 	dev_t			dev;
20520Sstevel@tonic-gate 
20530Sstevel@tonic-gate 	if ((lh == NULL) || (bp == NULL))
20540Sstevel@tonic-gate 		return (EINVAL);
20550Sstevel@tonic-gate 
20560Sstevel@tonic-gate 	/* this entry point is only supported for cb devices */
20570Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
20580Sstevel@tonic-gate 	if (!(handlep->lh_type & LH_CBDEV))
20590Sstevel@tonic-gate 		return (ENOTSUP);
20600Sstevel@tonic-gate 
20610Sstevel@tonic-gate 	bp->b_edev = dev;
20620Sstevel@tonic-gate 	bp->b_dev = cmpdev(dev);
20630Sstevel@tonic-gate 	return (bdev_strategy(bp));
20640Sstevel@tonic-gate }
20650Sstevel@tonic-gate 
20660Sstevel@tonic-gate int
20670Sstevel@tonic-gate ldi_dump(ldi_handle_t lh, caddr_t addr, daddr_t blkno, int nblk)
20680Sstevel@tonic-gate {
20690Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
20700Sstevel@tonic-gate 	dev_t			dev;
20710Sstevel@tonic-gate 
20720Sstevel@tonic-gate 	if (lh == NULL)
20730Sstevel@tonic-gate 		return (EINVAL);
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate 	/* this entry point is only supported for cb devices */
20760Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
20770Sstevel@tonic-gate 	if (!(handlep->lh_type & LH_CBDEV))
20780Sstevel@tonic-gate 		return (ENOTSUP);
20790Sstevel@tonic-gate 
20800Sstevel@tonic-gate 	return (bdev_dump(dev, addr, blkno, nblk));
20810Sstevel@tonic-gate }
20820Sstevel@tonic-gate 
20830Sstevel@tonic-gate int
20840Sstevel@tonic-gate ldi_devmap(ldi_handle_t lh, devmap_cookie_t dhp, offset_t off,
20850Sstevel@tonic-gate     size_t len, size_t *maplen, uint_t model)
20860Sstevel@tonic-gate {
20870Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
20880Sstevel@tonic-gate 	dev_t			dev;
20890Sstevel@tonic-gate 
20900Sstevel@tonic-gate 	if (lh == NULL)
20910Sstevel@tonic-gate 		return (EINVAL);
20920Sstevel@tonic-gate 
20930Sstevel@tonic-gate 	/* this entry point is only supported for cb devices */
20940Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
20950Sstevel@tonic-gate 	if (!(handlep->lh_type & LH_CBDEV))
20960Sstevel@tonic-gate 		return (ENOTSUP);
20970Sstevel@tonic-gate 
20980Sstevel@tonic-gate 	return (cdev_devmap(dev, dhp, off, len, maplen, model));
20990Sstevel@tonic-gate }
21000Sstevel@tonic-gate 
21010Sstevel@tonic-gate int
21020Sstevel@tonic-gate ldi_aread(ldi_handle_t lh, struct aio_req *aio_reqp, cred_t *cr)
21030Sstevel@tonic-gate {
21040Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
21050Sstevel@tonic-gate 	dev_t			dev;
21060Sstevel@tonic-gate 	struct cb_ops		*cb;
21070Sstevel@tonic-gate 
21080Sstevel@tonic-gate 	if (lh == NULL)
21090Sstevel@tonic-gate 		return (EINVAL);
21100Sstevel@tonic-gate 
21110Sstevel@tonic-gate 	/* this entry point is only supported for cb devices */
21120Sstevel@tonic-gate 	if (!(handlep->lh_type & LH_CBDEV))
21130Sstevel@tonic-gate 		return (ENOTSUP);
21140Sstevel@tonic-gate 
21150Sstevel@tonic-gate 	/*
21160Sstevel@tonic-gate 	 * Kaio is only supported on block devices.
21170Sstevel@tonic-gate 	 */
21180Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
21190Sstevel@tonic-gate 	cb = devopsp[getmajor(dev)]->devo_cb_ops;
21200Sstevel@tonic-gate 	if (cb->cb_strategy == nodev || cb->cb_strategy == NULL)
21210Sstevel@tonic-gate 		return (ENOTSUP);
21220Sstevel@tonic-gate 
21230Sstevel@tonic-gate 	if (cb->cb_aread == NULL)
21240Sstevel@tonic-gate 		return (ENOTSUP);
21250Sstevel@tonic-gate 
21260Sstevel@tonic-gate 	return (cb->cb_aread(dev, aio_reqp, cr));
21270Sstevel@tonic-gate }
21280Sstevel@tonic-gate 
21290Sstevel@tonic-gate int
21300Sstevel@tonic-gate ldi_awrite(ldi_handle_t lh, struct aio_req *aio_reqp, cred_t *cr)
21310Sstevel@tonic-gate {
21320Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
21330Sstevel@tonic-gate 	struct cb_ops		*cb;
21340Sstevel@tonic-gate 	dev_t			dev;
21350Sstevel@tonic-gate 
21360Sstevel@tonic-gate 	if (lh == NULL)
21370Sstevel@tonic-gate 		return (EINVAL);
21380Sstevel@tonic-gate 
21390Sstevel@tonic-gate 	/* this entry point is only supported for cb devices */
21400Sstevel@tonic-gate 	if (!(handlep->lh_type & LH_CBDEV))
21410Sstevel@tonic-gate 		return (ENOTSUP);
21420Sstevel@tonic-gate 
21430Sstevel@tonic-gate 	/*
21440Sstevel@tonic-gate 	 * Kaio is only supported on block devices.
21450Sstevel@tonic-gate 	 */
21460Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
21470Sstevel@tonic-gate 	cb = devopsp[getmajor(dev)]->devo_cb_ops;
21480Sstevel@tonic-gate 	if (cb->cb_strategy == nodev || cb->cb_strategy == NULL)
21490Sstevel@tonic-gate 		return (ENOTSUP);
21500Sstevel@tonic-gate 
21510Sstevel@tonic-gate 	if (cb->cb_awrite == NULL)
21520Sstevel@tonic-gate 		return (ENOTSUP);
21530Sstevel@tonic-gate 
21540Sstevel@tonic-gate 	return (cb->cb_awrite(dev, aio_reqp, cr));
21550Sstevel@tonic-gate }
21560Sstevel@tonic-gate 
21570Sstevel@tonic-gate int
21580Sstevel@tonic-gate ldi_putmsg(ldi_handle_t lh, mblk_t *smp)
21590Sstevel@tonic-gate {
21600Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
21610Sstevel@tonic-gate 	int			ret;
21620Sstevel@tonic-gate 
21630Sstevel@tonic-gate 	if ((lh == NULL) || (smp == NULL))
21640Sstevel@tonic-gate 		return (EINVAL);
21650Sstevel@tonic-gate 
21660Sstevel@tonic-gate 	if (!(handlep->lh_type & LH_STREAM)) {
21670Sstevel@tonic-gate 		freemsg(smp);
21680Sstevel@tonic-gate 		return (ENOTSUP);
21690Sstevel@tonic-gate 	}
21700Sstevel@tonic-gate 
2171*8778SErik.Nordmark@Sun.COM 	/*
2172*8778SErik.Nordmark@Sun.COM 	 * If we don't have db_credp, set it. Note that we can not be called
2173*8778SErik.Nordmark@Sun.COM 	 * from interrupt context.
2174*8778SErik.Nordmark@Sun.COM 	 */
2175*8778SErik.Nordmark@Sun.COM 	if (msg_getcred(smp, NULL) == NULL)
2176*8778SErik.Nordmark@Sun.COM 		mblk_setcred(smp, CRED(), curproc->p_pid);
2177*8778SErik.Nordmark@Sun.COM 
21780Sstevel@tonic-gate 	/* Send message while honoring flow control */
21790Sstevel@tonic-gate 	ret = kstrputmsg(handlep->lh_vp, smp, NULL, 0, 0,
21804582Scth 	    MSG_BAND | MSG_HOLDSIG | MSG_IGNERROR, 0);
21810Sstevel@tonic-gate 
21820Sstevel@tonic-gate 	return (ret);
21830Sstevel@tonic-gate }
21840Sstevel@tonic-gate 
21850Sstevel@tonic-gate int
21860Sstevel@tonic-gate ldi_getmsg(ldi_handle_t lh, mblk_t **rmp, timestruc_t *timeo)
21870Sstevel@tonic-gate {
21880Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
21890Sstevel@tonic-gate 	clock_t			timout; /* milliseconds */
21900Sstevel@tonic-gate 	uchar_t			pri;
21910Sstevel@tonic-gate 	rval_t			rval;
21920Sstevel@tonic-gate 	int			ret, pflag;
21930Sstevel@tonic-gate 
21940Sstevel@tonic-gate 
21950Sstevel@tonic-gate 	if (lh == NULL)
21960Sstevel@tonic-gate 		return (EINVAL);
21970Sstevel@tonic-gate 
21980Sstevel@tonic-gate 	if (!(handlep->lh_type & LH_STREAM))
21990Sstevel@tonic-gate 		return (ENOTSUP);
22000Sstevel@tonic-gate 
22010Sstevel@tonic-gate 	/* Convert from nanoseconds to milliseconds */
22020Sstevel@tonic-gate 	if (timeo != NULL) {
22030Sstevel@tonic-gate 		timout = timeo->tv_sec * 1000 + timeo->tv_nsec / 1000000;
22040Sstevel@tonic-gate 		if (timout > INT_MAX)
22050Sstevel@tonic-gate 			return (EINVAL);
22060Sstevel@tonic-gate 	} else
22070Sstevel@tonic-gate 		timout = -1;
22080Sstevel@tonic-gate 
22090Sstevel@tonic-gate 	/* Wait for timeout millseconds for a message */
22100Sstevel@tonic-gate 	pflag = MSG_ANY;
22110Sstevel@tonic-gate 	pri = 0;
22120Sstevel@tonic-gate 	*rmp = NULL;
22130Sstevel@tonic-gate 	ret = kstrgetmsg(handlep->lh_vp,
22144582Scth 	    rmp, NULL, &pri, &pflag, timout, &rval);
22150Sstevel@tonic-gate 	return (ret);
22160Sstevel@tonic-gate }
22170Sstevel@tonic-gate 
22180Sstevel@tonic-gate int
22190Sstevel@tonic-gate ldi_get_dev(ldi_handle_t lh, dev_t *devp)
22200Sstevel@tonic-gate {
22210Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
22220Sstevel@tonic-gate 
22230Sstevel@tonic-gate 	if ((lh == NULL) || (devp == NULL))
22240Sstevel@tonic-gate 		return (EINVAL);
22250Sstevel@tonic-gate 
22260Sstevel@tonic-gate 	*devp = handlep->lh_vp->v_rdev;
22270Sstevel@tonic-gate 	return (0);
22280Sstevel@tonic-gate }
22290Sstevel@tonic-gate 
22300Sstevel@tonic-gate int
22310Sstevel@tonic-gate ldi_get_otyp(ldi_handle_t lh, int *otyp)
22320Sstevel@tonic-gate {
22330Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
22340Sstevel@tonic-gate 
22350Sstevel@tonic-gate 	if ((lh == NULL) || (otyp == NULL))
22360Sstevel@tonic-gate 		return (EINVAL);
22370Sstevel@tonic-gate 
22380Sstevel@tonic-gate 	*otyp = VTYP_TO_OTYP(handlep->lh_vp->v_type);
22390Sstevel@tonic-gate 	return (0);
22400Sstevel@tonic-gate }
22410Sstevel@tonic-gate 
22420Sstevel@tonic-gate int
22430Sstevel@tonic-gate ldi_get_devid(ldi_handle_t lh, ddi_devid_t *devid)
22440Sstevel@tonic-gate {
22450Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
22460Sstevel@tonic-gate 	int			ret;
22470Sstevel@tonic-gate 	dev_t			dev;
22480Sstevel@tonic-gate 
22490Sstevel@tonic-gate 	if ((lh == NULL) || (devid == NULL))
22500Sstevel@tonic-gate 		return (EINVAL);
22510Sstevel@tonic-gate 
22520Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
22530Sstevel@tonic-gate 
22540Sstevel@tonic-gate 	ret = ddi_lyr_get_devid(dev, devid);
22550Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
22560Sstevel@tonic-gate 		return (ENOTSUP);
22570Sstevel@tonic-gate 
22580Sstevel@tonic-gate 	return (0);
22590Sstevel@tonic-gate }
22600Sstevel@tonic-gate 
22610Sstevel@tonic-gate int
22620Sstevel@tonic-gate ldi_get_minor_name(ldi_handle_t lh, char **minor_name)
22630Sstevel@tonic-gate {
22640Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
22650Sstevel@tonic-gate 	int			ret, otyp;
22660Sstevel@tonic-gate 	dev_t			dev;
22670Sstevel@tonic-gate 
22680Sstevel@tonic-gate 	if ((lh == NULL) || (minor_name == NULL))
22690Sstevel@tonic-gate 		return (EINVAL);
22700Sstevel@tonic-gate 
22710Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
22720Sstevel@tonic-gate 	otyp = VTYP_TO_OTYP(handlep->lh_vp->v_type);
22730Sstevel@tonic-gate 
22740Sstevel@tonic-gate 	ret = ddi_lyr_get_minor_name(dev, OTYP_TO_STYP(otyp), minor_name);
22750Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
22760Sstevel@tonic-gate 		return (ENOTSUP);
22770Sstevel@tonic-gate 
22780Sstevel@tonic-gate 	return (0);
22790Sstevel@tonic-gate }
22800Sstevel@tonic-gate 
22810Sstevel@tonic-gate int
22820Sstevel@tonic-gate ldi_prop_lookup_int_array(ldi_handle_t lh,
22830Sstevel@tonic-gate     uint_t flags, char *name, int **data, uint_t *nelements)
22840Sstevel@tonic-gate {
22850Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
22860Sstevel@tonic-gate 	dev_info_t		*dip;
22870Sstevel@tonic-gate 	dev_t			dev;
22880Sstevel@tonic-gate 	int			res;
22890Sstevel@tonic-gate 	struct snode		*csp;
22900Sstevel@tonic-gate 
22910Sstevel@tonic-gate 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
22920Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
22930Sstevel@tonic-gate 
22940Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
22950Sstevel@tonic-gate 
22960Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
22970Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
22980Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
22990Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
23000Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
23010Sstevel@tonic-gate 	if (dip == NULL)
23020Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
23030Sstevel@tonic-gate 
23040Sstevel@tonic-gate 	if (dip == NULL) {
23050Sstevel@tonic-gate 		flags |= DDI_UNBND_DLPI2;
23060Sstevel@tonic-gate 	} else if (flags & LDI_DEV_T_ANY) {
23070Sstevel@tonic-gate 		flags &= ~LDI_DEV_T_ANY;
23080Sstevel@tonic-gate 		dev = DDI_DEV_T_ANY;
23090Sstevel@tonic-gate 	}
23100Sstevel@tonic-gate 
23110Sstevel@tonic-gate 	if (dip != NULL) {
23120Sstevel@tonic-gate 		int *prop_val, prop_len;
23130Sstevel@tonic-gate 
23140Sstevel@tonic-gate 		res = i_ldi_prop_op_typed(dev, dip, flags, name,
23150Sstevel@tonic-gate 		    (caddr_t *)&prop_val, &prop_len, sizeof (int));
23160Sstevel@tonic-gate 
23170Sstevel@tonic-gate 		/* if we got it then return it */
23180Sstevel@tonic-gate 		if (res == DDI_PROP_SUCCESS) {
23190Sstevel@tonic-gate 			*nelements = prop_len / sizeof (int);
23200Sstevel@tonic-gate 			*data = prop_val;
23210Sstevel@tonic-gate 
23220Sstevel@tonic-gate 			ddi_release_devi(dip);
23230Sstevel@tonic-gate 			return (res);
23240Sstevel@tonic-gate 		}
23250Sstevel@tonic-gate 	}
23260Sstevel@tonic-gate 
23270Sstevel@tonic-gate 	/* call the normal property interfaces */
23280Sstevel@tonic-gate 	res = ddi_prop_lookup_int_array(dev, dip, flags,
23290Sstevel@tonic-gate 	    name, data, nelements);
23300Sstevel@tonic-gate 
23310Sstevel@tonic-gate 	if (dip != NULL)
23320Sstevel@tonic-gate 		ddi_release_devi(dip);
23330Sstevel@tonic-gate 
23340Sstevel@tonic-gate 	return (res);
23350Sstevel@tonic-gate }
23360Sstevel@tonic-gate 
23370Sstevel@tonic-gate int
23380Sstevel@tonic-gate ldi_prop_lookup_int64_array(ldi_handle_t lh,
23390Sstevel@tonic-gate     uint_t flags, char *name, int64_t **data, uint_t *nelements)
23400Sstevel@tonic-gate {
23410Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
23420Sstevel@tonic-gate 	dev_info_t		*dip;
23430Sstevel@tonic-gate 	dev_t			dev;
23440Sstevel@tonic-gate 	int			res;
23450Sstevel@tonic-gate 	struct snode		*csp;
23460Sstevel@tonic-gate 
23470Sstevel@tonic-gate 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
23480Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
23490Sstevel@tonic-gate 
23500Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
23510Sstevel@tonic-gate 
23520Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
23530Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
23540Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
23550Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
23560Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
23570Sstevel@tonic-gate 	if (dip == NULL)
23580Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
23590Sstevel@tonic-gate 
23600Sstevel@tonic-gate 	if (dip == NULL) {
23610Sstevel@tonic-gate 		flags |= DDI_UNBND_DLPI2;
23620Sstevel@tonic-gate 	} else if (flags & LDI_DEV_T_ANY) {
23630Sstevel@tonic-gate 		flags &= ~LDI_DEV_T_ANY;
23640Sstevel@tonic-gate 		dev = DDI_DEV_T_ANY;
23650Sstevel@tonic-gate 	}
23660Sstevel@tonic-gate 
23670Sstevel@tonic-gate 	if (dip != NULL) {
23680Sstevel@tonic-gate 		int64_t	*prop_val;
23690Sstevel@tonic-gate 		int	prop_len;
23700Sstevel@tonic-gate 
23710Sstevel@tonic-gate 		res = i_ldi_prop_op_typed(dev, dip, flags, name,
23720Sstevel@tonic-gate 		    (caddr_t *)&prop_val, &prop_len, sizeof (int64_t));
23730Sstevel@tonic-gate 
23740Sstevel@tonic-gate 		/* if we got it then return it */
23750Sstevel@tonic-gate 		if (res == DDI_PROP_SUCCESS) {
23760Sstevel@tonic-gate 			*nelements = prop_len / sizeof (int64_t);
23770Sstevel@tonic-gate 			*data = prop_val;
23780Sstevel@tonic-gate 
23790Sstevel@tonic-gate 			ddi_release_devi(dip);
23800Sstevel@tonic-gate 			return (res);
23810Sstevel@tonic-gate 		}
23820Sstevel@tonic-gate 	}
23830Sstevel@tonic-gate 
23840Sstevel@tonic-gate 	/* call the normal property interfaces */
23850Sstevel@tonic-gate 	res = ddi_prop_lookup_int64_array(dev, dip, flags,
23860Sstevel@tonic-gate 	    name, data, nelements);
23870Sstevel@tonic-gate 
23880Sstevel@tonic-gate 	if (dip != NULL)
23890Sstevel@tonic-gate 		ddi_release_devi(dip);
23900Sstevel@tonic-gate 
23910Sstevel@tonic-gate 	return (res);
23920Sstevel@tonic-gate }
23930Sstevel@tonic-gate 
23940Sstevel@tonic-gate int
23950Sstevel@tonic-gate ldi_prop_lookup_string_array(ldi_handle_t lh,
23960Sstevel@tonic-gate     uint_t flags, char *name, char ***data, uint_t *nelements)
23970Sstevel@tonic-gate {
23980Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
23990Sstevel@tonic-gate 	dev_info_t		*dip;
24000Sstevel@tonic-gate 	dev_t			dev;
24010Sstevel@tonic-gate 	int			res;
24020Sstevel@tonic-gate 	struct snode		*csp;
24030Sstevel@tonic-gate 
24040Sstevel@tonic-gate 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
24050Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
24060Sstevel@tonic-gate 
24070Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
24080Sstevel@tonic-gate 
24090Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
24100Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
24110Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
24120Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
24130Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
24140Sstevel@tonic-gate 	if (dip == NULL)
24150Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
24160Sstevel@tonic-gate 
24170Sstevel@tonic-gate 	if (dip == NULL) {
24180Sstevel@tonic-gate 		flags |= DDI_UNBND_DLPI2;
24190Sstevel@tonic-gate 	} else if (flags & LDI_DEV_T_ANY) {
24200Sstevel@tonic-gate 		flags &= ~LDI_DEV_T_ANY;
24210Sstevel@tonic-gate 		dev = DDI_DEV_T_ANY;
24220Sstevel@tonic-gate 	}
24230Sstevel@tonic-gate 
24240Sstevel@tonic-gate 	if (dip != NULL) {
24250Sstevel@tonic-gate 		char	*prop_val;
24260Sstevel@tonic-gate 		int	prop_len;
24270Sstevel@tonic-gate 
24280Sstevel@tonic-gate 		res = i_ldi_prop_op_typed(dev, dip, flags, name,
24290Sstevel@tonic-gate 		    (caddr_t *)&prop_val, &prop_len, 0);
24300Sstevel@tonic-gate 
24310Sstevel@tonic-gate 		/* if we got it then return it */
24320Sstevel@tonic-gate 		if (res == DDI_PROP_SUCCESS) {
24330Sstevel@tonic-gate 			char	**str_array;
24340Sstevel@tonic-gate 			int	nelem;
24350Sstevel@tonic-gate 
24360Sstevel@tonic-gate 			/*
24370Sstevel@tonic-gate 			 * pack the returned string array into the format
24380Sstevel@tonic-gate 			 * our callers expect
24390Sstevel@tonic-gate 			 */
24400Sstevel@tonic-gate 			if (i_pack_string_array(prop_val, prop_len,
24414582Scth 			    &str_array, &nelem) == 0) {
24420Sstevel@tonic-gate 
24430Sstevel@tonic-gate 				*data = str_array;
24440Sstevel@tonic-gate 				*nelements = nelem;
24450Sstevel@tonic-gate 
24460Sstevel@tonic-gate 				ddi_prop_free(prop_val);
24470Sstevel@tonic-gate 				ddi_release_devi(dip);
24480Sstevel@tonic-gate 				return (res);
24490Sstevel@tonic-gate 			}
24500Sstevel@tonic-gate 
24510Sstevel@tonic-gate 			/*
24520Sstevel@tonic-gate 			 * the format of the returned property must have
24530Sstevel@tonic-gate 			 * been bad so throw it out
24540Sstevel@tonic-gate 			 */
24550Sstevel@tonic-gate 			ddi_prop_free(prop_val);
24560Sstevel@tonic-gate 		}
24570Sstevel@tonic-gate 	}
24580Sstevel@tonic-gate 
24590Sstevel@tonic-gate 	/* call the normal property interfaces */
24600Sstevel@tonic-gate 	res = ddi_prop_lookup_string_array(dev, dip, flags,
24610Sstevel@tonic-gate 	    name, data, nelements);
24620Sstevel@tonic-gate 
24630Sstevel@tonic-gate 	if (dip != NULL)
24640Sstevel@tonic-gate 		ddi_release_devi(dip);
24650Sstevel@tonic-gate 
24660Sstevel@tonic-gate 	return (res);
24670Sstevel@tonic-gate }
24680Sstevel@tonic-gate 
24690Sstevel@tonic-gate int
24700Sstevel@tonic-gate ldi_prop_lookup_string(ldi_handle_t lh,
24710Sstevel@tonic-gate     uint_t flags, char *name, char **data)
24720Sstevel@tonic-gate {
24730Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
24740Sstevel@tonic-gate 	dev_info_t		*dip;
24750Sstevel@tonic-gate 	dev_t			dev;
24760Sstevel@tonic-gate 	int			res;
24770Sstevel@tonic-gate 	struct snode		*csp;
24780Sstevel@tonic-gate 
24790Sstevel@tonic-gate 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
24800Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
24810Sstevel@tonic-gate 
24820Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
24830Sstevel@tonic-gate 
24840Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
24850Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
24860Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
24870Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
24880Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
24890Sstevel@tonic-gate 	if (dip == NULL)
24900Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
24910Sstevel@tonic-gate 
24920Sstevel@tonic-gate 	if (dip == NULL) {
24930Sstevel@tonic-gate 		flags |= DDI_UNBND_DLPI2;
24940Sstevel@tonic-gate 	} else if (flags & LDI_DEV_T_ANY) {
24950Sstevel@tonic-gate 		flags &= ~LDI_DEV_T_ANY;
24960Sstevel@tonic-gate 		dev = DDI_DEV_T_ANY;
24970Sstevel@tonic-gate 	}
24980Sstevel@tonic-gate 
24990Sstevel@tonic-gate 	if (dip != NULL) {
25000Sstevel@tonic-gate 		char	*prop_val;
25010Sstevel@tonic-gate 		int	prop_len;
25020Sstevel@tonic-gate 
25030Sstevel@tonic-gate 		res = i_ldi_prop_op_typed(dev, dip, flags, name,
25040Sstevel@tonic-gate 		    (caddr_t *)&prop_val, &prop_len, 0);
25050Sstevel@tonic-gate 
25060Sstevel@tonic-gate 		/* if we got it then return it */
25070Sstevel@tonic-gate 		if (res == DDI_PROP_SUCCESS) {
25080Sstevel@tonic-gate 			/*
25090Sstevel@tonic-gate 			 * sanity check the vaule returned.
25100Sstevel@tonic-gate 			 */
25110Sstevel@tonic-gate 			if (i_check_string(prop_val, prop_len)) {
25120Sstevel@tonic-gate 				ddi_prop_free(prop_val);
25130Sstevel@tonic-gate 			} else {
25140Sstevel@tonic-gate 				*data = 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 
25210Sstevel@tonic-gate 	/* call the normal property interfaces */
25220Sstevel@tonic-gate 	res = ddi_prop_lookup_string(dev, dip, flags, name, data);
25230Sstevel@tonic-gate 
25240Sstevel@tonic-gate 	if (dip != NULL)
25250Sstevel@tonic-gate 		ddi_release_devi(dip);
25260Sstevel@tonic-gate 
25270Sstevel@tonic-gate #ifdef DEBUG
25280Sstevel@tonic-gate 	if (res == DDI_PROP_SUCCESS) {
25290Sstevel@tonic-gate 		/*
25300Sstevel@tonic-gate 		 * keep ourselves honest
25310Sstevel@tonic-gate 		 * make sure the framework returns strings in the
25320Sstevel@tonic-gate 		 * same format as we're demanding from drivers.
25330Sstevel@tonic-gate 		 */
25340Sstevel@tonic-gate 		struct prop_driver_data	*pdd;
25350Sstevel@tonic-gate 		int			pdd_prop_size;
25360Sstevel@tonic-gate 
25370Sstevel@tonic-gate 		pdd = ((struct prop_driver_data *)(*data)) - 1;
25380Sstevel@tonic-gate 		pdd_prop_size = pdd->pdd_size -
25390Sstevel@tonic-gate 		    sizeof (struct prop_driver_data);
25400Sstevel@tonic-gate 		ASSERT(i_check_string(*data, pdd_prop_size) == 0);
25410Sstevel@tonic-gate 	}
25420Sstevel@tonic-gate #endif /* DEBUG */
25430Sstevel@tonic-gate 
25440Sstevel@tonic-gate 	return (res);
25450Sstevel@tonic-gate }
25460Sstevel@tonic-gate 
25470Sstevel@tonic-gate int
25480Sstevel@tonic-gate ldi_prop_lookup_byte_array(ldi_handle_t lh,
25490Sstevel@tonic-gate     uint_t flags, char *name, uchar_t **data, uint_t *nelements)
25500Sstevel@tonic-gate {
25510Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
25520Sstevel@tonic-gate 	dev_info_t		*dip;
25530Sstevel@tonic-gate 	dev_t			dev;
25540Sstevel@tonic-gate 	int			res;
25550Sstevel@tonic-gate 	struct snode		*csp;
25560Sstevel@tonic-gate 
25570Sstevel@tonic-gate 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
25580Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
25590Sstevel@tonic-gate 
25600Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
25610Sstevel@tonic-gate 
25620Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
25630Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
25640Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
25650Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
25660Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
25670Sstevel@tonic-gate 	if (dip == NULL)
25680Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
25690Sstevel@tonic-gate 
25700Sstevel@tonic-gate 	if (dip == NULL) {
25710Sstevel@tonic-gate 		flags |= DDI_UNBND_DLPI2;
25720Sstevel@tonic-gate 	} else if (flags & LDI_DEV_T_ANY) {
25730Sstevel@tonic-gate 		flags &= ~LDI_DEV_T_ANY;
25740Sstevel@tonic-gate 		dev = DDI_DEV_T_ANY;
25750Sstevel@tonic-gate 	}
25760Sstevel@tonic-gate 
25770Sstevel@tonic-gate 	if (dip != NULL) {
25780Sstevel@tonic-gate 		uchar_t	*prop_val;
25790Sstevel@tonic-gate 		int	prop_len;
25800Sstevel@tonic-gate 
25810Sstevel@tonic-gate 		res = i_ldi_prop_op_typed(dev, dip, flags, name,
25820Sstevel@tonic-gate 		    (caddr_t *)&prop_val, &prop_len, sizeof (uchar_t));
25830Sstevel@tonic-gate 
25840Sstevel@tonic-gate 		/* if we got it then return it */
25850Sstevel@tonic-gate 		if (res == DDI_PROP_SUCCESS) {
25860Sstevel@tonic-gate 			*nelements = prop_len / sizeof (uchar_t);
25870Sstevel@tonic-gate 			*data = prop_val;
25880Sstevel@tonic-gate 
25890Sstevel@tonic-gate 			ddi_release_devi(dip);
25900Sstevel@tonic-gate 			return (res);
25910Sstevel@tonic-gate 		}
25920Sstevel@tonic-gate 	}
25930Sstevel@tonic-gate 
25940Sstevel@tonic-gate 	/* call the normal property interfaces */
25950Sstevel@tonic-gate 	res = ddi_prop_lookup_byte_array(dev, dip, flags,
25960Sstevel@tonic-gate 	    name, data, nelements);
25970Sstevel@tonic-gate 
25980Sstevel@tonic-gate 	if (dip != NULL)
25990Sstevel@tonic-gate 		ddi_release_devi(dip);
26000Sstevel@tonic-gate 
26010Sstevel@tonic-gate 	return (res);
26020Sstevel@tonic-gate }
26030Sstevel@tonic-gate 
26040Sstevel@tonic-gate int
26050Sstevel@tonic-gate ldi_prop_get_int(ldi_handle_t lh,
26060Sstevel@tonic-gate     uint_t flags, char *name, int defvalue)
26070Sstevel@tonic-gate {
26080Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
26090Sstevel@tonic-gate 	dev_info_t		*dip;
26100Sstevel@tonic-gate 	dev_t			dev;
26110Sstevel@tonic-gate 	int			res;
26120Sstevel@tonic-gate 	struct snode		*csp;
26130Sstevel@tonic-gate 
26140Sstevel@tonic-gate 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
26150Sstevel@tonic-gate 		return (defvalue);
26160Sstevel@tonic-gate 
26170Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
26180Sstevel@tonic-gate 
26190Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
26200Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
26210Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
26220Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
26230Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
26240Sstevel@tonic-gate 	if (dip == NULL)
26250Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
26260Sstevel@tonic-gate 
26270Sstevel@tonic-gate 	if (dip == NULL) {
26280Sstevel@tonic-gate 		flags |= DDI_UNBND_DLPI2;
26290Sstevel@tonic-gate 	} else if (flags & LDI_DEV_T_ANY) {
26300Sstevel@tonic-gate 		flags &= ~LDI_DEV_T_ANY;
26310Sstevel@tonic-gate 		dev = DDI_DEV_T_ANY;
26320Sstevel@tonic-gate 	}
26330Sstevel@tonic-gate 
26340Sstevel@tonic-gate 	if (dip != NULL) {
26350Sstevel@tonic-gate 		int	prop_val;
26360Sstevel@tonic-gate 		int	prop_len;
26370Sstevel@tonic-gate 
26380Sstevel@tonic-gate 		/*
26390Sstevel@tonic-gate 		 * first call the drivers prop_op interface to allow it
26400Sstevel@tonic-gate 		 * it to override default property values.
26410Sstevel@tonic-gate 		 */
26420Sstevel@tonic-gate 		prop_len = sizeof (int);
26430Sstevel@tonic-gate 		res = i_ldi_prop_op(dev, dip, PROP_LEN_AND_VAL_BUF,
26440Sstevel@tonic-gate 		    flags | DDI_PROP_DYNAMIC, name,
26450Sstevel@tonic-gate 		    (caddr_t)&prop_val, &prop_len);
26460Sstevel@tonic-gate 
26470Sstevel@tonic-gate 		/* if we got it then return it */
26480Sstevel@tonic-gate 		if ((res == DDI_PROP_SUCCESS) &&
26490Sstevel@tonic-gate 		    (prop_len == sizeof (int))) {
26500Sstevel@tonic-gate 			res = prop_val;
26510Sstevel@tonic-gate 			ddi_release_devi(dip);
26520Sstevel@tonic-gate 			return (res);
26530Sstevel@tonic-gate 		}
26540Sstevel@tonic-gate 	}
26550Sstevel@tonic-gate 
26560Sstevel@tonic-gate 	/* call the normal property interfaces */
26570Sstevel@tonic-gate 	res = ddi_prop_get_int(dev, dip, flags, name, defvalue);
26580Sstevel@tonic-gate 
26590Sstevel@tonic-gate 	if (dip != NULL)
26600Sstevel@tonic-gate 		ddi_release_devi(dip);
26610Sstevel@tonic-gate 
26620Sstevel@tonic-gate 	return (res);
26630Sstevel@tonic-gate }
26640Sstevel@tonic-gate 
26650Sstevel@tonic-gate int64_t
26660Sstevel@tonic-gate ldi_prop_get_int64(ldi_handle_t lh,
26670Sstevel@tonic-gate     uint_t flags, char *name, int64_t defvalue)
26680Sstevel@tonic-gate {
26690Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
26700Sstevel@tonic-gate 	dev_info_t		*dip;
26710Sstevel@tonic-gate 	dev_t			dev;
26720Sstevel@tonic-gate 	int64_t			res;
26730Sstevel@tonic-gate 	struct snode		*csp;
26740Sstevel@tonic-gate 
26750Sstevel@tonic-gate 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
26760Sstevel@tonic-gate 		return (defvalue);
26770Sstevel@tonic-gate 
26780Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
26790Sstevel@tonic-gate 
26800Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
26810Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
26820Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
26830Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
26840Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
26850Sstevel@tonic-gate 	if (dip == NULL)
26860Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
26870Sstevel@tonic-gate 
26880Sstevel@tonic-gate 	if (dip == NULL) {
26890Sstevel@tonic-gate 		flags |= DDI_UNBND_DLPI2;
26900Sstevel@tonic-gate 	} else if (flags & LDI_DEV_T_ANY) {
26910Sstevel@tonic-gate 		flags &= ~LDI_DEV_T_ANY;
26920Sstevel@tonic-gate 		dev = DDI_DEV_T_ANY;
26930Sstevel@tonic-gate 	}
26940Sstevel@tonic-gate 
26950Sstevel@tonic-gate 	if (dip != NULL) {
26960Sstevel@tonic-gate 		int64_t	prop_val;
26970Sstevel@tonic-gate 		int	prop_len;
26980Sstevel@tonic-gate 
26990Sstevel@tonic-gate 		/*
27000Sstevel@tonic-gate 		 * first call the drivers prop_op interface to allow it
27010Sstevel@tonic-gate 		 * it to override default property values.
27020Sstevel@tonic-gate 		 */
27030Sstevel@tonic-gate 		prop_len = sizeof (int64_t);
27040Sstevel@tonic-gate 		res = i_ldi_prop_op(dev, dip, PROP_LEN_AND_VAL_BUF,
27050Sstevel@tonic-gate 		    flags | DDI_PROP_DYNAMIC, name,
27060Sstevel@tonic-gate 		    (caddr_t)&prop_val, &prop_len);
27070Sstevel@tonic-gate 
27080Sstevel@tonic-gate 		/* if we got it then return it */
27090Sstevel@tonic-gate 		if ((res == DDI_PROP_SUCCESS) &&
27100Sstevel@tonic-gate 		    (prop_len == sizeof (int64_t))) {
27110Sstevel@tonic-gate 			res = prop_val;
27120Sstevel@tonic-gate 			ddi_release_devi(dip);
27130Sstevel@tonic-gate 			return (res);
27140Sstevel@tonic-gate 		}
27150Sstevel@tonic-gate 	}
27160Sstevel@tonic-gate 
27170Sstevel@tonic-gate 	/* call the normal property interfaces */
27180Sstevel@tonic-gate 	res = ddi_prop_get_int64(dev, dip, flags, name, defvalue);
27190Sstevel@tonic-gate 
27200Sstevel@tonic-gate 	if (dip != NULL)
27210Sstevel@tonic-gate 		ddi_release_devi(dip);
27220Sstevel@tonic-gate 
27230Sstevel@tonic-gate 	return (res);
27240Sstevel@tonic-gate }
27250Sstevel@tonic-gate 
27260Sstevel@tonic-gate int
27270Sstevel@tonic-gate ldi_prop_exists(ldi_handle_t lh, uint_t flags, char *name)
27280Sstevel@tonic-gate {
27290Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
27304582Scth 	dev_info_t		*dip;
27314582Scth 	dev_t			dev;
27320Sstevel@tonic-gate 	int			res, prop_len;
27330Sstevel@tonic-gate 	struct snode		*csp;
27340Sstevel@tonic-gate 
27350Sstevel@tonic-gate 	if ((lh == NULL) || (name == NULL) || (strlen(name) == 0))
27360Sstevel@tonic-gate 		return (0);
27370Sstevel@tonic-gate 
27380Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
27390Sstevel@tonic-gate 
27400Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
27410Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
27420Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
27430Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
27440Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
27450Sstevel@tonic-gate 	if (dip == NULL)
27460Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
27470Sstevel@tonic-gate 
27480Sstevel@tonic-gate 	/* if NULL dip, prop does NOT exist */
27490Sstevel@tonic-gate 	if (dip == NULL)
27500Sstevel@tonic-gate 		return (0);
27510Sstevel@tonic-gate 
27520Sstevel@tonic-gate 	if (flags & LDI_DEV_T_ANY) {
27530Sstevel@tonic-gate 		flags &= ~LDI_DEV_T_ANY;
27540Sstevel@tonic-gate 		dev = DDI_DEV_T_ANY;
27550Sstevel@tonic-gate 	}
27560Sstevel@tonic-gate 
27570Sstevel@tonic-gate 	/*
27580Sstevel@tonic-gate 	 * first call the drivers prop_op interface to allow it
27590Sstevel@tonic-gate 	 * it to override default property values.
27600Sstevel@tonic-gate 	 */
27610Sstevel@tonic-gate 	res = i_ldi_prop_op(dev, dip, PROP_LEN,
27620Sstevel@tonic-gate 	    flags | DDI_PROP_DYNAMIC, name, NULL, &prop_len);
27630Sstevel@tonic-gate 
27640Sstevel@tonic-gate 	if (res == DDI_PROP_SUCCESS) {
27650Sstevel@tonic-gate 		ddi_release_devi(dip);
27660Sstevel@tonic-gate 		return (1);
27670Sstevel@tonic-gate 	}
27680Sstevel@tonic-gate 
27690Sstevel@tonic-gate 	/* call the normal property interfaces */
27700Sstevel@tonic-gate 	res = ddi_prop_exists(dev, dip, flags, name);
27710Sstevel@tonic-gate 
27720Sstevel@tonic-gate 	ddi_release_devi(dip);
27730Sstevel@tonic-gate 	return (res);
27740Sstevel@tonic-gate }
27750Sstevel@tonic-gate 
27764845Svikram #ifdef	LDI_OBSOLETE_EVENT
27774845Svikram 
27780Sstevel@tonic-gate int
27790Sstevel@tonic-gate ldi_get_eventcookie(ldi_handle_t lh, char *name, ddi_eventcookie_t *ecp)
27800Sstevel@tonic-gate {
27810Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
27820Sstevel@tonic-gate 	dev_info_t		*dip;
27830Sstevel@tonic-gate 	dev_t			dev;
27840Sstevel@tonic-gate 	int			res;
27850Sstevel@tonic-gate 	struct snode		*csp;
27860Sstevel@tonic-gate 
27870Sstevel@tonic-gate 	if ((lh == NULL) || (name == NULL) ||
27880Sstevel@tonic-gate 	    (strlen(name) == 0) || (ecp == NULL)) {
27890Sstevel@tonic-gate 		return (DDI_FAILURE);
27900Sstevel@tonic-gate 	}
27910Sstevel@tonic-gate 
27920Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
27930Sstevel@tonic-gate 
27940Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
27950Sstevel@tonic-gate 
27960Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
27970Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
27980Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
27990Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
28000Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
28010Sstevel@tonic-gate 	if (dip == NULL)
28020Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
28030Sstevel@tonic-gate 
28040Sstevel@tonic-gate 	if (dip == NULL)
28050Sstevel@tonic-gate 		return (DDI_FAILURE);
28060Sstevel@tonic-gate 
28070Sstevel@tonic-gate 	LDI_EVENTCB((CE_NOTE, "%s: event_name=%s, "
28080Sstevel@tonic-gate 	    "dip=0x%p, event_cookiep=0x%p", "ldi_get_eventcookie",
28090Sstevel@tonic-gate 	    name, (void *)dip, (void *)ecp));
28100Sstevel@tonic-gate 
28110Sstevel@tonic-gate 	res = ddi_get_eventcookie(dip, name, ecp);
28120Sstevel@tonic-gate 
28130Sstevel@tonic-gate 	ddi_release_devi(dip);
28140Sstevel@tonic-gate 	return (res);
28150Sstevel@tonic-gate }
28160Sstevel@tonic-gate 
28170Sstevel@tonic-gate int
28180Sstevel@tonic-gate ldi_add_event_handler(ldi_handle_t lh, ddi_eventcookie_t ec,
28190Sstevel@tonic-gate     void (*handler)(ldi_handle_t, ddi_eventcookie_t, void *, void *),
28200Sstevel@tonic-gate     void *arg, ldi_callback_id_t *id)
28210Sstevel@tonic-gate {
28220Sstevel@tonic-gate 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
28230Sstevel@tonic-gate 	struct ldi_event	*lep;
28240Sstevel@tonic-gate 	dev_info_t		*dip;
28250Sstevel@tonic-gate 	dev_t			dev;
28260Sstevel@tonic-gate 	int			res;
28270Sstevel@tonic-gate 	struct snode		*csp;
28280Sstevel@tonic-gate 
28290Sstevel@tonic-gate 	if ((lh == NULL) || (ec == NULL) || (handler == NULL) || (id == NULL))
28300Sstevel@tonic-gate 		return (DDI_FAILURE);
28310Sstevel@tonic-gate 
28320Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
28330Sstevel@tonic-gate 
28340Sstevel@tonic-gate 	dev = handlep->lh_vp->v_rdev;
28350Sstevel@tonic-gate 
28360Sstevel@tonic-gate 	csp = VTOCS(handlep->lh_vp);
28370Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
28380Sstevel@tonic-gate 	if ((dip = csp->s_dip) != NULL)
28390Sstevel@tonic-gate 		e_ddi_hold_devi(dip);
28400Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
28410Sstevel@tonic-gate 	if (dip == NULL)
28420Sstevel@tonic-gate 		dip = e_ddi_hold_devi_by_dev(dev, 0);
28430Sstevel@tonic-gate 
28440Sstevel@tonic-gate 	if (dip == NULL)
28450Sstevel@tonic-gate 		return (DDI_FAILURE);
28460Sstevel@tonic-gate 
28470Sstevel@tonic-gate 	lep = kmem_zalloc(sizeof (struct ldi_event), KM_SLEEP);
28480Sstevel@tonic-gate 	lep->le_lhp = handlep;
28490Sstevel@tonic-gate 	lep->le_arg = arg;
28500Sstevel@tonic-gate 	lep->le_handler = handler;
28510Sstevel@tonic-gate 
28520Sstevel@tonic-gate 	if ((res = ddi_add_event_handler(dip, ec, i_ldi_callback,
28530Sstevel@tonic-gate 	    (void *)lep, &lep->le_id)) != DDI_SUCCESS) {
28540Sstevel@tonic-gate 		LDI_EVENTCB((CE_WARN, "%s: unable to add"
28550Sstevel@tonic-gate 		    "event callback", "ldi_add_event_handler"));
28560Sstevel@tonic-gate 		ddi_release_devi(dip);
28570Sstevel@tonic-gate 		kmem_free(lep, sizeof (struct ldi_event));
28580Sstevel@tonic-gate 		return (res);
28590Sstevel@tonic-gate 	}
28600Sstevel@tonic-gate 
28610Sstevel@tonic-gate 	*id = (ldi_callback_id_t)lep;
28620Sstevel@tonic-gate 
28630Sstevel@tonic-gate 	LDI_EVENTCB((CE_NOTE, "%s: dip=0x%p, event=0x%p, "
28640Sstevel@tonic-gate 	    "ldi_eventp=0x%p, cb_id=0x%p", "ldi_add_event_handler",
28650Sstevel@tonic-gate 	    (void *)dip, (void *)ec, (void *)lep, (void *)id));
28660Sstevel@tonic-gate 
28670Sstevel@tonic-gate 	handle_event_add(lep);
28680Sstevel@tonic-gate 	ddi_release_devi(dip);
28690Sstevel@tonic-gate 	return (res);
28700Sstevel@tonic-gate }
28710Sstevel@tonic-gate 
28720Sstevel@tonic-gate int
28730Sstevel@tonic-gate ldi_remove_event_handler(ldi_handle_t lh, ldi_callback_id_t id)
28740Sstevel@tonic-gate {
28750Sstevel@tonic-gate 	ldi_event_t		*lep = (ldi_event_t *)id;
28760Sstevel@tonic-gate 	int			res;
28770Sstevel@tonic-gate 
28780Sstevel@tonic-gate 	if ((lh == NULL) || (id == NULL))
28790Sstevel@tonic-gate 		return (DDI_FAILURE);
28800Sstevel@tonic-gate 
28810Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
28820Sstevel@tonic-gate 
28830Sstevel@tonic-gate 	if ((res = ddi_remove_event_handler(lep->le_id))
28840Sstevel@tonic-gate 	    != DDI_SUCCESS) {
28850Sstevel@tonic-gate 		LDI_EVENTCB((CE_WARN, "%s: unable to remove "
28860Sstevel@tonic-gate 		    "event callback", "ldi_remove_event_handler"));
28870Sstevel@tonic-gate 		return (res);
28880Sstevel@tonic-gate 	}
28890Sstevel@tonic-gate 
28900Sstevel@tonic-gate 	handle_event_remove(lep);
28910Sstevel@tonic-gate 	kmem_free(lep, sizeof (struct ldi_event));
28920Sstevel@tonic-gate 	return (res);
28930Sstevel@tonic-gate }
28944845Svikram 
28954845Svikram #endif
28964845Svikram 
28974845Svikram /*
28984845Svikram  * Here are some definitions of terms used in the following LDI events
28994845Svikram  * code:
29004845Svikram  *
29014845Svikram  * "LDI events" AKA "native events": These are events defined by the
29024845Svikram  * "new" LDI event framework. These events are serviced by the LDI event
29034845Svikram  * framework itself and thus are native to it.
29044845Svikram  *
29054845Svikram  * "LDI contract events": These are contract events that correspond to the
29064845Svikram  *  LDI events. This mapping of LDI events to contract events is defined by
29074845Svikram  * the ldi_ev_cookies[] array above.
29084845Svikram  *
29094845Svikram  * NDI events: These are events which are serviced by the NDI event subsystem.
29104845Svikram  * LDI subsystem just provides a thin wrapper around the NDI event interfaces
29115331Samw  * These events are therefore *not* native events.
29124845Svikram  */
29134845Svikram 
29144845Svikram static int
29154845Svikram ldi_native_event(const char *evname)
29164845Svikram {
29174845Svikram 	int i;
29184845Svikram 
29194845Svikram 	LDI_EVTRC((CE_NOTE, "ldi_native_event: entered: ev=%s", evname));
29204845Svikram 
29214845Svikram 	for (i = 0; ldi_ev_cookies[i].ck_evname != NULL; i++) {
29224845Svikram 		if (strcmp(ldi_ev_cookies[i].ck_evname, evname) == 0)
29234845Svikram 			return (1);
29244845Svikram 	}
29254845Svikram 
29264845Svikram 	return (0);
29274845Svikram }
29284845Svikram 
29294845Svikram static uint_t
29304845Svikram ldi_ev_sync_event(const char *evname)
29314845Svikram {
29324845Svikram 	int i;
29334845Svikram 
29344845Svikram 	ASSERT(ldi_native_event(evname));
29354845Svikram 
29364845Svikram 	LDI_EVTRC((CE_NOTE, "ldi_ev_sync_event: entered: %s", evname));
29374845Svikram 
29384845Svikram 	for (i = 0; ldi_ev_cookies[i].ck_evname != NULL; i++) {
29394845Svikram 		if (strcmp(ldi_ev_cookies[i].ck_evname, evname) == 0)
29404845Svikram 			return (ldi_ev_cookies[i].ck_sync);
29414845Svikram 	}
29424845Svikram 
29434845Svikram 	/*
29444845Svikram 	 * This should never happen until non-contract based
29454845Svikram 	 * LDI events are introduced. If that happens, we will
29464845Svikram 	 * use a "special" token to indicate that there are no
29474845Svikram 	 * contracts corresponding to this LDI event.
29484845Svikram 	 */
29494845Svikram 	cmn_err(CE_PANIC, "Unknown LDI event: %s", evname);
29504845Svikram 
29514845Svikram 	return (0);
29524845Svikram }
29534845Svikram 
29544845Svikram static uint_t
29554845Svikram ldi_contract_event(const char *evname)
29564845Svikram {
29574845Svikram 	int i;
29584845Svikram 
29594845Svikram 	ASSERT(ldi_native_event(evname));
29604845Svikram 
29614845Svikram 	LDI_EVTRC((CE_NOTE, "ldi_contract_event: entered: %s", evname));
29624845Svikram 
29634845Svikram 	for (i = 0; ldi_ev_cookies[i].ck_evname != NULL; i++) {
29644845Svikram 		if (strcmp(ldi_ev_cookies[i].ck_evname, evname) == 0)
29654845Svikram 			return (ldi_ev_cookies[i].ck_ctype);
29664845Svikram 	}
29674845Svikram 
29684845Svikram 	/*
29694845Svikram 	 * This should never happen until non-contract based
29704845Svikram 	 * LDI events are introduced. If that happens, we will
29714845Svikram 	 * use a "special" token to indicate that there are no
29724845Svikram 	 * contracts corresponding to this LDI event.
29734845Svikram 	 */
29744845Svikram 	cmn_err(CE_PANIC, "Unknown LDI event: %s", evname);
29754845Svikram 
29764845Svikram 	return (0);
29774845Svikram }
29784845Svikram 
29794845Svikram char *
29804845Svikram ldi_ev_get_type(ldi_ev_cookie_t cookie)
29814845Svikram {
29824845Svikram 	int i;
29834845Svikram 	struct ldi_ev_cookie *cookie_impl = (struct ldi_ev_cookie *)cookie;
29844845Svikram 
29854845Svikram 	for (i = 0; ldi_ev_cookies[i].ck_evname != NULL; i++) {
29864845Svikram 		if (&ldi_ev_cookies[i] == cookie_impl) {
29874845Svikram 			LDI_EVTRC((CE_NOTE, "ldi_ev_get_type: LDI: %s",
29884845Svikram 			    ldi_ev_cookies[i].ck_evname));
29894845Svikram 			return (ldi_ev_cookies[i].ck_evname);
29904845Svikram 		}
29914845Svikram 	}
29924845Svikram 
29934845Svikram 	/*
29944845Svikram 	 * Not an LDI native event. Must be NDI event service.
29954845Svikram 	 * Just return a generic string
29964845Svikram 	 */
29974845Svikram 	LDI_EVTRC((CE_NOTE, "ldi_ev_get_type: is NDI"));
29984845Svikram 	return (NDI_EVENT_SERVICE);
29994845Svikram }
30004845Svikram 
30014845Svikram static int
30024845Svikram ldi_native_cookie(ldi_ev_cookie_t cookie)
30034845Svikram {
30044845Svikram 	int i;
30054845Svikram 	struct ldi_ev_cookie *cookie_impl = (struct ldi_ev_cookie *)cookie;
30064845Svikram 
30074845Svikram 	for (i = 0; ldi_ev_cookies[i].ck_evname != NULL; i++) {
30084845Svikram 		if (&ldi_ev_cookies[i] == cookie_impl) {
30094845Svikram 			LDI_EVTRC((CE_NOTE, "ldi_native_cookie: native LDI"));
30104845Svikram 			return (1);
30114845Svikram 		}
30124845Svikram 	}
30134845Svikram 
30144845Svikram 	LDI_EVTRC((CE_NOTE, "ldi_native_cookie: is NDI"));
30154845Svikram 	return (0);
30164845Svikram }
30174845Svikram 
30184845Svikram static ldi_ev_cookie_t
30194845Svikram ldi_get_native_cookie(const char *evname)
30204845Svikram {
30214845Svikram 	int i;
30224845Svikram 
30234845Svikram 	for (i = 0; ldi_ev_cookies[i].ck_evname != NULL; i++) {
30244845Svikram 		if (strcmp(ldi_ev_cookies[i].ck_evname, evname) == 0) {
30254845Svikram 			LDI_EVTRC((CE_NOTE, "ldi_get_native_cookie: found"));
30264845Svikram 			return ((ldi_ev_cookie_t)&ldi_ev_cookies[i]);
30274845Svikram 		}
30284845Svikram 	}
30294845Svikram 
30304845Svikram 	LDI_EVTRC((CE_NOTE, "ldi_get_native_cookie: NOT found"));
30314845Svikram 	return (NULL);
30324845Svikram }
30334845Svikram 
30344845Svikram /*
30354845Svikram  * ldi_ev_lock() needs to be recursive, since layered drivers may call
30364845Svikram  * other LDI interfaces (such as ldi_close() from within the context of
30374845Svikram  * a notify callback. Since the notify callback is called with the
30384845Svikram  * ldi_ev_lock() held and ldi_close() also grabs ldi_ev_lock, the lock needs
30394845Svikram  * to be recursive.
30404845Svikram  */
30414845Svikram static void
30424845Svikram ldi_ev_lock(void)
30434845Svikram {
30444845Svikram 	LDI_EVTRC((CE_NOTE, "ldi_ev_lock: entered"));
30454845Svikram 
30464845Svikram 	mutex_enter(&ldi_ev_callback_list.le_lock);
30474845Svikram 	if (ldi_ev_callback_list.le_thread == curthread) {
30484845Svikram 		ASSERT(ldi_ev_callback_list.le_busy >= 1);
30494845Svikram 		ldi_ev_callback_list.le_busy++;
30504845Svikram 	} else {
30514845Svikram 		while (ldi_ev_callback_list.le_busy)
30524845Svikram 			cv_wait(&ldi_ev_callback_list.le_cv,
30534845Svikram 			    &ldi_ev_callback_list.le_lock);
30544845Svikram 		ASSERT(ldi_ev_callback_list.le_thread == NULL);
30554845Svikram 		ldi_ev_callback_list.le_busy = 1;
30564845Svikram 		ldi_ev_callback_list.le_thread = curthread;
30574845Svikram 	}
30584845Svikram 	mutex_exit(&ldi_ev_callback_list.le_lock);
30594845Svikram 
30604845Svikram 	LDI_EVTRC((CE_NOTE, "ldi_ev_lock: exit"));
30614845Svikram }
30624845Svikram 
30634845Svikram static void
30644845Svikram ldi_ev_unlock(void)
30654845Svikram {
30664845Svikram 	LDI_EVTRC((CE_NOTE, "ldi_ev_unlock: entered"));
30674845Svikram 	mutex_enter(&ldi_ev_callback_list.le_lock);
30684845Svikram 	ASSERT(ldi_ev_callback_list.le_thread == curthread);
30694845Svikram 	ASSERT(ldi_ev_callback_list.le_busy >= 1);
30704845Svikram 
30714845Svikram 	ldi_ev_callback_list.le_busy--;
30724845Svikram 	if (ldi_ev_callback_list.le_busy == 0) {
30734845Svikram 		ldi_ev_callback_list.le_thread = NULL;
30744845Svikram 		cv_signal(&ldi_ev_callback_list.le_cv);
30754845Svikram 	}
30764845Svikram 	mutex_exit(&ldi_ev_callback_list.le_lock);
30774845Svikram 	LDI_EVTRC((CE_NOTE, "ldi_ev_unlock: exit"));
30784845Svikram }
30794845Svikram 
30804845Svikram int
30814845Svikram ldi_ev_get_cookie(ldi_handle_t lh, char *evname, ldi_ev_cookie_t *cookiep)
30824845Svikram {
30834845Svikram 	struct ldi_handle	*handlep = (struct ldi_handle *)lh;
30844845Svikram 	dev_info_t		*dip;
30854845Svikram 	dev_t			dev;
30864845Svikram 	int			res;
30874845Svikram 	struct snode		*csp;
30884845Svikram 	ddi_eventcookie_t	ddi_cookie;
30894845Svikram 	ldi_ev_cookie_t		tcookie;
30904845Svikram 
30914845Svikram 	LDI_EVDBG((CE_NOTE, "ldi_ev_get_cookie: entered: evname=%s",
30924845Svikram 	    evname ? evname : "<NULL>"));
30934845Svikram 
30944845Svikram 	if (lh == NULL || evname == NULL ||
30954845Svikram 	    strlen(evname) == 0 || cookiep == NULL) {
30964845Svikram 		LDI_EVDBG((CE_NOTE, "ldi_ev_get_cookie: invalid args"));
30974845Svikram 		return (LDI_EV_FAILURE);
30984845Svikram 	}
30994845Svikram 
31004845Svikram 	*cookiep = NULL;
31014845Svikram 
31024845Svikram 	/*
31034845Svikram 	 * First check if it is a LDI native event
31044845Svikram 	 */
31054845Svikram 	tcookie = ldi_get_native_cookie(evname);
31064845Svikram 	if (tcookie) {
31074845Svikram 		LDI_EVDBG((CE_NOTE, "ldi_ev_get_cookie: got native cookie"));
31084845Svikram 		*cookiep = tcookie;
31094845Svikram 		return (LDI_EV_SUCCESS);
31104845Svikram 	}
31114845Svikram 
31124845Svikram 	/*
31134845Svikram 	 * Not a LDI native event. Try NDI event services
31144845Svikram 	 */
31154845Svikram 
31164845Svikram 	dev = handlep->lh_vp->v_rdev;
31174845Svikram 
31184845Svikram 	csp = VTOCS(handlep->lh_vp);
31194845Svikram 	mutex_enter(&csp->s_lock);
31204845Svikram 	if ((dip = csp->s_dip) != NULL)
31214845Svikram 		e_ddi_hold_devi(dip);
31224845Svikram 	mutex_exit(&csp->s_lock);
31234845Svikram 	if (dip == NULL)
31244845Svikram 		dip = e_ddi_hold_devi_by_dev(dev, 0);
31254845Svikram 
31264845Svikram 	if (dip == NULL) {
31274845Svikram 		cmn_err(CE_WARN, "ldi_ev_get_cookie: No devinfo node for LDI "
31284845Svikram 		    "handle: %p", (void *)handlep);
31294845Svikram 		return (LDI_EV_FAILURE);
31304845Svikram 	}
31314845Svikram 
31324845Svikram 	LDI_EVDBG((CE_NOTE, "Calling ddi_get_eventcookie: dip=%p, ev=%s",
31334845Svikram 	    (void *)dip, evname));
31344845Svikram 
31354845Svikram 	res = ddi_get_eventcookie(dip, evname, &ddi_cookie);
31364845Svikram 
31374845Svikram 	ddi_release_devi(dip);
31384845Svikram 
31394845Svikram 	if (res == DDI_SUCCESS) {
31404845Svikram 		LDI_EVDBG((CE_NOTE, "ldi_ev_get_cookie: NDI cookie found"));
31414845Svikram 		*cookiep = (ldi_ev_cookie_t)ddi_cookie;
31424845Svikram 		return (LDI_EV_SUCCESS);
31434845Svikram 	} else {
31444845Svikram 		LDI_EVDBG((CE_WARN, "ldi_ev_get_cookie: NDI cookie: failed"));
31454845Svikram 		return (LDI_EV_FAILURE);
31464845Svikram 	}
31474845Svikram }
31484845Svikram 
31494845Svikram /*ARGSUSED*/
31504845Svikram static void
31514845Svikram i_ldi_ev_callback(dev_info_t *dip, ddi_eventcookie_t event_cookie,
31524845Svikram     void *arg, void *ev_data)
31534845Svikram {
31544845Svikram 	ldi_ev_callback_impl_t *lecp = (ldi_ev_callback_impl_t *)arg;
31554845Svikram 
31564845Svikram 	ASSERT(lecp != NULL);
31574845Svikram 	ASSERT(!ldi_native_cookie(lecp->lec_cookie));
31584845Svikram 	ASSERT(lecp->lec_lhp);
31594845Svikram 	ASSERT(lecp->lec_notify == NULL);
31604845Svikram 	ASSERT(lecp->lec_finalize);
31614845Svikram 
31624845Svikram 	LDI_EVDBG((CE_NOTE, "i_ldi_ev_callback: ldh=%p, cookie=%p, arg=%p, "
31634845Svikram 	    "ev_data=%p", (void *)lecp->lec_lhp, (void *)event_cookie,
31644845Svikram 	    (void *)lecp->lec_arg, (void *)ev_data));
31654845Svikram 
31664845Svikram 	lecp->lec_finalize(lecp->lec_lhp, (ldi_ev_cookie_t)event_cookie,
31674845Svikram 	    lecp->lec_arg, ev_data);
31684845Svikram }
31694845Svikram 
31704845Svikram int
31714845Svikram ldi_ev_register_callbacks(ldi_handle_t lh, ldi_ev_cookie_t cookie,
31724845Svikram     ldi_ev_callback_t *callb, void *arg, ldi_callback_id_t *id)
31734845Svikram {
31744845Svikram 	struct ldi_handle	*lhp = (struct ldi_handle *)lh;
31754845Svikram 	ldi_ev_callback_impl_t	*lecp;
31764845Svikram 	dev_t			dev;
31774845Svikram 	struct snode		*csp;
31784845Svikram 	dev_info_t		*dip;
31794845Svikram 	int			ddi_event;
31804845Svikram 
31814845Svikram 	ASSERT(!servicing_interrupt());
31824845Svikram 
31834845Svikram 	if (lh == NULL || cookie == NULL || callb == NULL || id == NULL) {
31844845Svikram 		LDI_EVDBG((CE_NOTE, "ldi_ev_register_callbacks: Invalid args"));
31854845Svikram 		return (LDI_EV_FAILURE);
31864845Svikram 	}
31874845Svikram 
31884845Svikram 	if (callb->cb_vers != LDI_EV_CB_VERS) {
31894845Svikram 		LDI_EVDBG((CE_NOTE, "ldi_ev_register_callbacks: Invalid vers"));
31904845Svikram 		return (LDI_EV_FAILURE);
31914845Svikram 	}
31924845Svikram 
31934845Svikram 	if (callb->cb_notify == NULL && callb->cb_finalize == NULL) {
31944845Svikram 		LDI_EVDBG((CE_NOTE, "ldi_ev_register_callbacks: NULL callb"));
31954845Svikram 		return (LDI_EV_FAILURE);
31964845Svikram 	}
31974845Svikram 
31984845Svikram 	*id = 0;
31994845Svikram 
32004845Svikram 	dev = lhp->lh_vp->v_rdev;
32014845Svikram 	csp = VTOCS(lhp->lh_vp);
32024845Svikram 	mutex_enter(&csp->s_lock);
32034845Svikram 	if ((dip = csp->s_dip) != NULL)
32044845Svikram 		e_ddi_hold_devi(dip);
32054845Svikram 	mutex_exit(&csp->s_lock);
32064845Svikram 	if (dip == NULL)
32074845Svikram 		dip = e_ddi_hold_devi_by_dev(dev, 0);
32084845Svikram 
32094845Svikram 	if (dip == NULL) {
32104845Svikram 		cmn_err(CE_WARN, "ldi_ev_register: No devinfo node for "
32114845Svikram 		    "LDI handle: %p", (void *)lhp);
32124845Svikram 		return (LDI_EV_FAILURE);
32134845Svikram 	}
32144845Svikram 
32154845Svikram 	lecp = kmem_zalloc(sizeof (ldi_ev_callback_impl_t), KM_SLEEP);
32164845Svikram 
32174845Svikram 	ddi_event = 0;
32184845Svikram 	if (!ldi_native_cookie(cookie)) {
32194845Svikram 		if (callb->cb_notify || callb->cb_finalize == NULL) {
32204845Svikram 			/*
32214845Svikram 			 * NDI event services only accept finalize
32224845Svikram 			 */
32234845Svikram 			cmn_err(CE_WARN, "%s: module: %s: NDI event cookie. "
32244845Svikram 			    "Only finalize"
32254845Svikram 			    " callback supported with this cookie",
32264845Svikram 			    "ldi_ev_register_callbacks",
32274845Svikram 			    lhp->lh_ident->li_modname);
32284845Svikram 			kmem_free(lecp, sizeof (ldi_ev_callback_impl_t));
32294845Svikram 			ddi_release_devi(dip);
32304845Svikram 			return (LDI_EV_FAILURE);
32314845Svikram 		}
32324845Svikram 
32334845Svikram 		if (ddi_add_event_handler(dip, (ddi_eventcookie_t)cookie,
32344845Svikram 		    i_ldi_ev_callback, (void *)lecp,
32354845Svikram 		    (ddi_callback_id_t *)&lecp->lec_id)
32364845Svikram 		    != DDI_SUCCESS) {
32374845Svikram 			kmem_free(lecp, sizeof (ldi_ev_callback_impl_t));
32384845Svikram 			ddi_release_devi(dip);
32394845Svikram 			LDI_EVDBG((CE_NOTE, "ldi_ev_register_callbacks(): "
32404845Svikram 			    "ddi_add_event_handler failed"));
32414845Svikram 			return (LDI_EV_FAILURE);
32424845Svikram 		}
32434845Svikram 		ddi_event = 1;
32444845Svikram 		LDI_EVDBG((CE_NOTE, "ldi_ev_register_callbacks(): "
32454845Svikram 		    "ddi_add_event_handler success"));
32464845Svikram 	}
32474845Svikram 
32484845Svikram 
32494845Svikram 
32504845Svikram 	ldi_ev_lock();
32514845Svikram 
32524845Svikram 	/*
32534845Svikram 	 * Add the notify/finalize callback to the LDI's list of callbacks.
32544845Svikram 	 */
32554845Svikram 	lecp->lec_lhp = lhp;
32564845Svikram 	lecp->lec_dev = lhp->lh_vp->v_rdev;
32574845Svikram 	lecp->lec_spec = (lhp->lh_vp->v_type == VCHR) ?
32584845Svikram 	    S_IFCHR : S_IFBLK;
32594845Svikram 	lecp->lec_notify = callb->cb_notify;
32604845Svikram 	lecp->lec_finalize = callb->cb_finalize;
32614845Svikram 	lecp->lec_arg = arg;
32624845Svikram 	lecp->lec_cookie = cookie;
32634845Svikram 	if (!ddi_event)
32644845Svikram 		lecp->lec_id = (void *)(uintptr_t)(++ldi_ev_id_pool);
32654845Svikram 	else
32664845Svikram 		ASSERT(lecp->lec_id);
32674845Svikram 	lecp->lec_dip = dip;
32684845Svikram 	list_insert_tail(&ldi_ev_callback_list.le_head, lecp);
32694845Svikram 
32704845Svikram 	*id = (ldi_callback_id_t)lecp->lec_id;
32714845Svikram 
32724845Svikram 	ldi_ev_unlock();
32734845Svikram 
32744845Svikram 	ddi_release_devi(dip);
32754845Svikram 
32764845Svikram 	LDI_EVDBG((CE_NOTE, "ldi_ev_register_callbacks: registered "
32774845Svikram 	    "notify/finalize"));
32784845Svikram 
32794845Svikram 	return (LDI_EV_SUCCESS);
32804845Svikram }
32814845Svikram 
32824845Svikram static int
32834845Svikram ldi_ev_device_match(ldi_ev_callback_impl_t *lecp, dev_info_t *dip,
32844845Svikram     dev_t dev, int spec_type)
32854845Svikram {
32864845Svikram 	ASSERT(lecp);
32874845Svikram 	ASSERT(dip);
32884845Svikram 	ASSERT(dev != DDI_DEV_T_NONE);
32894845Svikram 	ASSERT(dev != NODEV);
32904845Svikram 	ASSERT((dev == DDI_DEV_T_ANY && spec_type == 0) ||
32914845Svikram 	    (spec_type == S_IFCHR || spec_type == S_IFBLK));
32924845Svikram 	ASSERT(lecp->lec_dip);
32934845Svikram 	ASSERT(lecp->lec_spec == S_IFCHR || lecp->lec_spec == S_IFBLK);
32944845Svikram 	ASSERT(lecp->lec_dev != DDI_DEV_T_ANY);
32954845Svikram 	ASSERT(lecp->lec_dev != DDI_DEV_T_NONE);
32964845Svikram 	ASSERT(lecp->lec_dev != NODEV);
32974845Svikram 
32984845Svikram 	if (dip != lecp->lec_dip)
32994845Svikram 		return (0);
33004845Svikram 
33014845Svikram 	if (dev != DDI_DEV_T_ANY) {
33024845Svikram 		if (dev != lecp->lec_dev || spec_type != lecp->lec_spec)
33034845Svikram 			return (0);
33044845Svikram 	}
33054845Svikram 
33064845Svikram 	LDI_EVTRC((CE_NOTE, "ldi_ev_device_match: MATCH dip=%p", (void *)dip));
33074845Svikram 
33084845Svikram 	return (1);
33094845Svikram }
33104845Svikram 
33114845Svikram /*
33124845Svikram  * LDI framework function to post a "notify" event to all layered drivers
33134845Svikram  * that have registered for that event
33144845Svikram  *
33154845Svikram  * Returns:
33164845Svikram  *		LDI_EV_SUCCESS - registered callbacks allow event
33174845Svikram  *		LDI_EV_FAILURE - registered callbacks block event
33184845Svikram  *		LDI_EV_NONE    - No matching LDI callbacks
33194845Svikram  *
33204845Svikram  * This function is *not* to be called by layered drivers. It is for I/O
33214845Svikram  * framework code in Solaris, such as the I/O retire code and DR code
33224845Svikram  * to call while servicing a device event such as offline or degraded.
33234845Svikram  */
33244845Svikram int
33254845Svikram ldi_invoke_notify(dev_info_t *dip, dev_t dev, int spec_type, char *event,
33264845Svikram     void *ev_data)
33274845Svikram {
33284845Svikram 	ldi_ev_callback_impl_t *lecp;
33294845Svikram 	list_t	*listp;
33304845Svikram 	int	ret;
33314845Svikram 	char	*lec_event;
33324845Svikram 
33334845Svikram 	ASSERT(dip);
33344845Svikram 	ASSERT(dev != DDI_DEV_T_NONE);
33354845Svikram 	ASSERT(dev != NODEV);
33364845Svikram 	ASSERT((dev == DDI_DEV_T_ANY && spec_type == 0) ||
33374845Svikram 	    (spec_type == S_IFCHR || spec_type == S_IFBLK));
33384845Svikram 	ASSERT(event);
33394845Svikram 	ASSERT(ldi_native_event(event));
33404845Svikram 	ASSERT(ldi_ev_sync_event(event));
33414845Svikram 
33424845Svikram 	LDI_EVDBG((CE_NOTE, "ldi_invoke_notify(): entered: dip=%p, ev=%s",
33434845Svikram 	    (void *)dip, event));
33444845Svikram 
33454845Svikram 	ret = LDI_EV_NONE;
33464845Svikram 	ldi_ev_lock();
33474845Svikram 	listp = &ldi_ev_callback_list.le_head;
33484845Svikram 	for (lecp = list_head(listp); lecp; lecp = list_next(listp, lecp)) {
33494845Svikram 
33504845Svikram 		/* Check if matching device */
33514845Svikram 		if (!ldi_ev_device_match(lecp, dip, dev, spec_type))
33524845Svikram 			continue;
33534845Svikram 
33544845Svikram 		if (lecp->lec_lhp == NULL) {
33554845Svikram 			/*
33564845Svikram 			 * Consumer has unregistered the handle and so
33574845Svikram 			 * is no longer interested in notify events.
33584845Svikram 			 */
33594845Svikram 			LDI_EVDBG((CE_NOTE, "ldi_invoke_notify(): No LDI "
33604845Svikram 			    "handle, skipping"));
33614845Svikram 			continue;
33624845Svikram 		}
33634845Svikram 
33644845Svikram 		if (lecp->lec_notify == NULL) {
33654845Svikram 			LDI_EVDBG((CE_NOTE, "ldi_invoke_notify(): No notify "
33664845Svikram 			    "callback. skipping"));
33674845Svikram 			continue;	/* not interested in notify */
33684845Svikram 		}
33694845Svikram 
33704845Svikram 		/*
33714845Svikram 		 * Check if matching event
33724845Svikram 		 */
33734845Svikram 		lec_event = ldi_ev_get_type(lecp->lec_cookie);
33744845Svikram 		if (strcmp(event, lec_event) != 0) {
33754845Svikram 			LDI_EVDBG((CE_NOTE, "ldi_invoke_notify(): Not matching"
33764845Svikram 			    " event {%s,%s}. skipping", event, lec_event));
33774845Svikram 			continue;
33784845Svikram 		}
33794845Svikram 
33804845Svikram 		lecp->lec_lhp->lh_flags |= LH_FLAGS_NOTIFY;
33814845Svikram 		if (lecp->lec_notify(lecp->lec_lhp, lecp->lec_cookie,
33824845Svikram 		    lecp->lec_arg, ev_data) != LDI_EV_SUCCESS) {
33834845Svikram 			ret = LDI_EV_FAILURE;
33844845Svikram 			LDI_EVDBG((CE_NOTE, "ldi_invoke_notify(): notify"
33854845Svikram 			    " FAILURE"));
33864845Svikram 			break;
33874845Svikram 		}
33884845Svikram 
33894845Svikram 		/* We have a matching callback that allows the event to occur */
33904845Svikram 		ret = LDI_EV_SUCCESS;
33914845Svikram 
33924845Svikram 		LDI_EVDBG((CE_NOTE, "ldi_invoke_notify(): 1 consumer success"));
33934845Svikram 	}
33944845Svikram 
33954845Svikram 	if (ret != LDI_EV_FAILURE)
33964845Svikram 		goto out;
33974845Svikram 
33984845Svikram 	LDI_EVDBG((CE_NOTE, "ldi_invoke_notify(): undoing notify"));
33994845Svikram 
34004845Svikram 	/*
34014845Svikram 	 * Undo notifies already sent
34024845Svikram 	 */
34034845Svikram 	lecp = list_prev(listp, lecp);
34044845Svikram 	for (; lecp; lecp = list_prev(listp, lecp)) {
34054845Svikram 
34064845Svikram 		/*
34074845Svikram 		 * Check if matching device
34084845Svikram 		 */
34094845Svikram 		if (!ldi_ev_device_match(lecp, dip, dev, spec_type))
34104845Svikram 			continue;
34114845Svikram 
34124845Svikram 
34134845Svikram 		if (lecp->lec_finalize == NULL) {
34144845Svikram 			LDI_EVDBG((CE_NOTE, "ldi_invoke_notify(): no finalize, "
34154845Svikram 			    "skipping"));
34164845Svikram 			continue;	/* not interested in finalize */
34174845Svikram 		}
34184845Svikram 
34194845Svikram 		/*
34204845Svikram 		 * it is possible that in response to a notify event a
34214845Svikram 		 * layered driver closed its LDI handle so it is ok
34224845Svikram 		 * to have a NULL LDI handle for finalize. The layered
34234845Svikram 		 * driver is expected to maintain state in its "arg"
34244845Svikram 		 * parameter to keep track of the closed device.
34254845Svikram 		 */
34264845Svikram 
34274845Svikram 		/* Check if matching event */
34284845Svikram 		lec_event = ldi_ev_get_type(lecp->lec_cookie);
34294845Svikram 		if (strcmp(event, lec_event) != 0) {
34304845Svikram 			LDI_EVDBG((CE_NOTE, "ldi_invoke_notify(): not matching "
34314845Svikram 			    "event: %s,%s, skipping", event, lec_event));
34324845Svikram 			continue;
34334845Svikram 		}
34344845Svikram 
34354845Svikram 		LDI_EVDBG((CE_NOTE, "ldi_invoke_notify(): calling finalize"));
34364845Svikram 
34374845Svikram 		lecp->lec_finalize(lecp->lec_lhp, lecp->lec_cookie,
34384845Svikram 		    LDI_EV_FAILURE, lecp->lec_arg, ev_data);
34394845Svikram 
34404845Svikram 		/*
34414845Svikram 		 * If LDI native event and LDI handle closed in context
34424845Svikram 		 * of notify, NULL out the finalize callback as we have
34434845Svikram 		 * already called the 1 finalize above allowed in this situation
34444845Svikram 		 */
34454845Svikram 		if (lecp->lec_lhp == NULL &&
34464845Svikram 		    ldi_native_cookie(lecp->lec_cookie)) {
34474845Svikram 			LDI_EVDBG((CE_NOTE,
34484845Svikram 			    "ldi_invoke_notify(): NULL-ing finalize after "
34494845Svikram 			    "calling 1 finalize following ldi_close"));
34504845Svikram 			lecp->lec_finalize = NULL;
34514845Svikram 		}
34524845Svikram 	}
34534845Svikram 
34544845Svikram out:
34554845Svikram 	ldi_ev_unlock();
34564845Svikram 
34574845Svikram 	if (ret == LDI_EV_NONE) {
34584845Svikram 		LDI_EVDBG((CE_NOTE, "ldi_invoke_notify(): no matching "
34594845Svikram 		    "LDI callbacks"));
34604845Svikram 	}
34614845Svikram 
34624845Svikram 	return (ret);
34634845Svikram }
34644845Svikram 
34654845Svikram /*
34664845Svikram  * Framework function to be called from a layered driver to propagate
34674845Svikram  * LDI "notify" events to exported minors.
34684845Svikram  *
34694845Svikram  * This function is a public interface exported by the LDI framework
34704845Svikram  * for use by layered drivers to propagate device events up the software
34714845Svikram  * stack.
34724845Svikram  */
34734845Svikram int
34744845Svikram ldi_ev_notify(dev_info_t *dip, minor_t minor, int spec_type,
34754845Svikram     ldi_ev_cookie_t cookie, void *ev_data)
34764845Svikram {
34774845Svikram 	char		*evname = ldi_ev_get_type(cookie);
34784845Svikram 	uint_t		ct_evtype;
34794845Svikram 	dev_t		dev;
34804845Svikram 	major_t		major;
34814845Svikram 	int		retc;
34824845Svikram 	int		retl;
34834845Svikram 
34844845Svikram 	ASSERT(spec_type == S_IFBLK || spec_type == S_IFCHR);
34854845Svikram 	ASSERT(dip);
34864845Svikram 	ASSERT(ldi_native_cookie(cookie));
34874845Svikram 
34884845Svikram 	LDI_EVDBG((CE_NOTE, "ldi_ev_notify(): entered: event=%s, dip=%p",
34894845Svikram 	    evname, (void *)dip));
34904845Svikram 
34914845Svikram 	if (!ldi_ev_sync_event(evname)) {
34924845Svikram 		cmn_err(CE_PANIC, "ldi_ev_notify(): %s not a "
34934845Svikram 		    "negotiatable event", evname);
34944845Svikram 		return (LDI_EV_SUCCESS);
34954845Svikram 	}
34964845Svikram 
34974845Svikram 	major = ddi_driver_major(dip);
34987009Scth 	if (major == DDI_MAJOR_T_NONE) {
34994845Svikram 		char *path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
35004845Svikram 		(void) ddi_pathname(dip, path);
35014845Svikram 		cmn_err(CE_WARN, "ldi_ev_notify: cannot derive major number "
35024845Svikram 		    "for device %s", path);
35034845Svikram 		kmem_free(path, MAXPATHLEN);
35044845Svikram 		return (LDI_EV_FAILURE);
35054845Svikram 	}
35064845Svikram 	dev = makedevice(major, minor);
35074845Svikram 
35084845Svikram 	/*
35094845Svikram 	 * Generate negotiation contract events on contracts (if any) associated
35104845Svikram 	 * with this minor.
35114845Svikram 	 */
35124845Svikram 	LDI_EVDBG((CE_NOTE, "ldi_ev_notify(): calling contract nego."));
35134845Svikram 	ct_evtype = ldi_contract_event(evname);
35144845Svikram 	retc = contract_device_negotiate(dip, dev, spec_type, ct_evtype);
35154845Svikram 	if (retc == CT_NACK) {
35164845Svikram 		LDI_EVDBG((CE_NOTE, "ldi_ev_notify(): contract neg. NACK"));
35174845Svikram 		return (LDI_EV_FAILURE);
35184845Svikram 	}
35194845Svikram 
35204845Svikram 	LDI_EVDBG((CE_NOTE, "ldi_ev_notify(): LDI invoke notify"));
35214845Svikram 	retl = ldi_invoke_notify(dip, dev, spec_type, evname, ev_data);
35224845Svikram 	if (retl == LDI_EV_FAILURE) {
35234845Svikram 		LDI_EVDBG((CE_NOTE, "ldi_ev_notify(): ldi_invoke_notify "
35244845Svikram 		    "returned FAILURE. Calling contract negend"));
35254845Svikram 		contract_device_negend(dip, dev, spec_type, CT_EV_FAILURE);
35264845Svikram 		return (LDI_EV_FAILURE);
35274845Svikram 	}
35284845Svikram 
35294845Svikram 	/*
35304845Svikram 	 * The very fact that we are here indicates that there is a
35314845Svikram 	 * LDI callback (and hence a constraint) for the retire of the
35324845Svikram 	 * HW device. So we just return success even if there are no
35334845Svikram 	 * contracts or LDI callbacks against the minors layered on top
35344845Svikram 	 * of the HW minors
35354845Svikram 	 */
35364845Svikram 	LDI_EVDBG((CE_NOTE, "ldi_ev_notify(): returning SUCCESS"));
35374845Svikram 	return (LDI_EV_SUCCESS);
35384845Svikram }
35394845Svikram 
35404845Svikram /*
35414845Svikram  * LDI framework function to invoke "finalize" callbacks for all layered
35424845Svikram  * drivers that have registered callbacks for that event.
35434845Svikram  *
35444845Svikram  * This function is *not* to be called by layered drivers. It is for I/O
35454845Svikram  * framework code in Solaris, such as the I/O retire code and DR code
35464845Svikram  * to call while servicing a device event such as offline or degraded.
35474845Svikram  */
35484845Svikram void
35494845Svikram ldi_invoke_finalize(dev_info_t *dip, dev_t dev, int spec_type, char *event,
35504845Svikram     int ldi_result, void *ev_data)
35514845Svikram {
35524845Svikram 	ldi_ev_callback_impl_t *lecp;
35534845Svikram 	list_t	*listp;
35544845Svikram 	char	*lec_event;
35554845Svikram 	int	found = 0;
35564845Svikram 
35574845Svikram 	ASSERT(dip);
35584845Svikram 	ASSERT(dev != DDI_DEV_T_NONE);
35594845Svikram 	ASSERT(dev != NODEV);
35604845Svikram 	ASSERT((dev == DDI_DEV_T_ANY && spec_type == 0) ||
35614845Svikram 	    (spec_type == S_IFCHR || spec_type == S_IFBLK));
35624845Svikram 	ASSERT(event);
35634845Svikram 	ASSERT(ldi_native_event(event));
35644845Svikram 	ASSERT(ldi_result == LDI_EV_SUCCESS || ldi_result == LDI_EV_FAILURE);
35654845Svikram 
35664845Svikram 	LDI_EVDBG((CE_NOTE, "ldi_invoke_finalize(): entered: dip=%p, result=%d"
35674845Svikram 	    " event=%s", (void *)dip, ldi_result, event));
35684845Svikram 
35694845Svikram 	ldi_ev_lock();
35704845Svikram 	listp = &ldi_ev_callback_list.le_head;
35714845Svikram 	for (lecp = list_head(listp); lecp; lecp = list_next(listp, lecp)) {
35724845Svikram 
35734845Svikram 		if (lecp->lec_finalize == NULL) {
35744845Svikram 			LDI_EVDBG((CE_NOTE, "ldi_invoke_finalize(): No "
35754845Svikram 			    "finalize. Skipping"));
35764845Svikram 			continue;	/* Not interested in finalize */
35774845Svikram 		}
35784845Svikram 
35794845Svikram 		/*
35804845Svikram 		 * Check if matching device
35814845Svikram 		 */
35824845Svikram 		if (!ldi_ev_device_match(lecp, dip, dev, spec_type))
35834845Svikram 			continue;
35844845Svikram 
35854845Svikram 		/*
35864845Svikram 		 * It is valid for the LDI handle to be NULL during finalize.
35874845Svikram 		 * The layered driver may have done an LDI close in the notify
35884845Svikram 		 * callback.
35894845Svikram 		 */
35904845Svikram 
35914845Svikram 		/*
35924845Svikram 		 * Check if matching event
35934845Svikram 		 */
35944845Svikram 		lec_event = ldi_ev_get_type(lecp->lec_cookie);
35954845Svikram 		if (strcmp(event, lec_event) != 0) {
35964845Svikram 			LDI_EVDBG((CE_NOTE, "ldi_invoke_finalize(): Not "
35974845Svikram 			    "matching event {%s,%s}. Skipping",
35984845Svikram 			    event, lec_event));
35994845Svikram 			continue;
36004845Svikram 		}
36014845Svikram 
36024845Svikram 		LDI_EVDBG((CE_NOTE, "ldi_invoke_finalize(): calling finalize"));
36034845Svikram 
36044845Svikram 		found = 1;
36054845Svikram 
36064845Svikram 		lecp->lec_finalize(lecp->lec_lhp, lecp->lec_cookie,
36074845Svikram 		    ldi_result, lecp->lec_arg, ev_data);
36084845Svikram 
36094845Svikram 		/*
36104845Svikram 		 * If LDI native event and LDI handle closed in context
36114845Svikram 		 * of notify, NULL out the finalize callback as we have
36124845Svikram 		 * already called the 1 finalize above allowed in this situation
36134845Svikram 		 */
36144845Svikram 		if (lecp->lec_lhp == NULL &&
36154845Svikram 		    ldi_native_cookie(lecp->lec_cookie)) {
36164845Svikram 			LDI_EVDBG((CE_NOTE,
36174845Svikram 			    "ldi_invoke_finalize(): NULLing finalize after "
36184845Svikram 			    "calling 1 finalize following ldi_close"));
36194845Svikram 			lecp->lec_finalize = NULL;
36204845Svikram 		}
36214845Svikram 	}
36224845Svikram 	ldi_ev_unlock();
36234845Svikram 
36244845Svikram 	if (found)
36254845Svikram 		return;
36264845Svikram 
36274845Svikram 	LDI_EVDBG((CE_NOTE, "ldi_invoke_finalize(): no matching callbacks"));
36284845Svikram }
36294845Svikram 
36304845Svikram /*
36314845Svikram  * Framework function to be called from a layered driver to propagate
36324845Svikram  * LDI "finalize" events to exported minors.
36334845Svikram  *
36344845Svikram  * This function is a public interface exported by the LDI framework
36354845Svikram  * for use by layered drivers to propagate device events up the software
36364845Svikram  * stack.
36374845Svikram  */
36384845Svikram void
36394845Svikram ldi_ev_finalize(dev_info_t *dip, minor_t minor, int spec_type, int ldi_result,
36404845Svikram     ldi_ev_cookie_t cookie, void *ev_data)
36414845Svikram {
36424845Svikram 	dev_t dev;
36434845Svikram 	major_t major;
36444845Svikram 	char *evname;
36454845Svikram 	int ct_result = (ldi_result == LDI_EV_SUCCESS) ?
36464845Svikram 	    CT_EV_SUCCESS : CT_EV_FAILURE;
36474845Svikram 	uint_t ct_evtype;
36484845Svikram 
36494845Svikram 	ASSERT(dip);
36504845Svikram 	ASSERT(spec_type == S_IFBLK || spec_type == S_IFCHR);
36514845Svikram 	ASSERT(ldi_result == LDI_EV_SUCCESS || ldi_result == LDI_EV_FAILURE);
36524845Svikram 	ASSERT(ldi_native_cookie(cookie));
36534845Svikram 
36544845Svikram 	LDI_EVDBG((CE_NOTE, "ldi_ev_finalize: entered: dip=%p", (void *)dip));
36554845Svikram 
36564845Svikram 	major = ddi_driver_major(dip);
36577009Scth 	if (major == DDI_MAJOR_T_NONE) {
36584845Svikram 		char *path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
36594845Svikram 		(void) ddi_pathname(dip, path);
36604845Svikram 		cmn_err(CE_WARN, "ldi_ev_finalize: cannot derive major number "
36614845Svikram 		    "for device %s", path);
36624845Svikram 		kmem_free(path, MAXPATHLEN);
36634845Svikram 		return;
36644845Svikram 	}
36654845Svikram 	dev = makedevice(major, minor);
36664845Svikram 
36674845Svikram 	evname = ldi_ev_get_type(cookie);
36684845Svikram 
36694845Svikram 	LDI_EVDBG((CE_NOTE, "ldi_ev_finalize: calling contracts"));
36704845Svikram 	ct_evtype = ldi_contract_event(evname);
36714845Svikram 	contract_device_finalize(dip, dev, spec_type, ct_evtype, ct_result);
36724845Svikram 
36734845Svikram 	LDI_EVDBG((CE_NOTE, "ldi_ev_finalize: calling ldi_invoke_finalize"));
36744845Svikram 	ldi_invoke_finalize(dip, dev, spec_type, evname, ldi_result, ev_data);
36754845Svikram }
36764845Svikram 
36774845Svikram int
36784845Svikram ldi_ev_remove_callbacks(ldi_callback_id_t id)
36794845Svikram {
36804845Svikram 	ldi_ev_callback_impl_t	*lecp;
36814845Svikram 	ldi_ev_callback_impl_t	*next;
36824845Svikram 	ldi_ev_callback_impl_t	*found;
36834845Svikram 	list_t			*listp;
36844845Svikram 
36854845Svikram 	ASSERT(!servicing_interrupt());
36864845Svikram 
36874845Svikram 	if (id == 0) {
36884845Svikram 		cmn_err(CE_WARN, "ldi_ev_remove_callbacks: Invalid ID 0");
36894845Svikram 		return (LDI_EV_FAILURE);
36904845Svikram 	}
36914845Svikram 
36924845Svikram 	LDI_EVDBG((CE_NOTE, "ldi_ev_remove_callbacks: entered: id=%p",
36934845Svikram 	    (void *)id));
36944845Svikram 
36954845Svikram 	ldi_ev_lock();
36964845Svikram 
36974845Svikram 	listp = &ldi_ev_callback_list.le_head;
36984845Svikram 	next = found = NULL;
36994845Svikram 	for (lecp = list_head(listp); lecp; lecp = next) {
37004845Svikram 		next = list_next(listp, lecp);
37014845Svikram 		if (lecp->lec_id == id) {
37024845Svikram 			ASSERT(found == NULL);
37034845Svikram 			list_remove(listp, lecp);
37044845Svikram 			found = lecp;
37054845Svikram 		}
37064845Svikram 	}
37074845Svikram 	ldi_ev_unlock();
37084845Svikram 
37094845Svikram 	if (found == NULL) {
37104845Svikram 		cmn_err(CE_WARN, "No LDI event handler for id (%p)",
37114845Svikram 		    (void *)id);
37124845Svikram 		return (LDI_EV_SUCCESS);
37134845Svikram 	}
37144845Svikram 
37154845Svikram 	if (!ldi_native_cookie(found->lec_cookie)) {
37164845Svikram 		ASSERT(found->lec_notify == NULL);
37174845Svikram 		if (ddi_remove_event_handler((ddi_callback_id_t)id)
37184845Svikram 		    != DDI_SUCCESS) {
37194845Svikram 			cmn_err(CE_WARN, "failed to remove NDI event handler "
37204845Svikram 			    "for id (%p)", (void *)id);
37214845Svikram 			ldi_ev_lock();
37224845Svikram 			list_insert_tail(listp, found);
37234845Svikram 			ldi_ev_unlock();
37244845Svikram 			return (LDI_EV_FAILURE);
37254845Svikram 		}
37264845Svikram 		LDI_EVDBG((CE_NOTE, "ldi_ev_remove_callbacks: NDI event "
37274845Svikram 		    "service removal succeeded"));
37284845Svikram 	} else {
37294845Svikram 		LDI_EVDBG((CE_NOTE, "ldi_ev_remove_callbacks: removed "
37304845Svikram 		    "LDI native callbacks"));
37314845Svikram 	}
37324845Svikram 	kmem_free(found, sizeof (ldi_ev_callback_impl_t));
37334845Svikram 
37344845Svikram 	return (LDI_EV_SUCCESS);
37354845Svikram }
3736