xref: /onnv-gate/usr/src/uts/common/os/driver.c (revision 11052:f59c5298a2cc)
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 /*
2210842SJerry.Gilliam@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 #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/t_lock.h>
280Sstevel@tonic-gate #include <sys/param.h>
290Sstevel@tonic-gate #include <sys/conf.h>
300Sstevel@tonic-gate #include <sys/systm.h>
310Sstevel@tonic-gate #include <sys/sysmacros.h>
320Sstevel@tonic-gate #include <sys/buf.h>
330Sstevel@tonic-gate #include <sys/cred.h>
340Sstevel@tonic-gate #include <sys/user.h>
350Sstevel@tonic-gate #include <sys/stat.h>
360Sstevel@tonic-gate #include <sys/uio.h>
370Sstevel@tonic-gate #include <sys/vnode.h>
380Sstevel@tonic-gate #include <sys/fs/snode.h>
390Sstevel@tonic-gate #include <sys/open.h>
400Sstevel@tonic-gate #include <sys/kmem.h>
410Sstevel@tonic-gate #include <sys/file.h>
420Sstevel@tonic-gate #include <sys/debug.h>
430Sstevel@tonic-gate #include <sys/tnf_probe.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /* Don't #include <sys/ddi.h> - it #undef's getmajor() */
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #include <sys/sunddi.h>
480Sstevel@tonic-gate #include <sys/sunndi.h>
490Sstevel@tonic-gate #include <sys/sunpm.h>
500Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
510Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
520Sstevel@tonic-gate #include <sys/esunddi.h>
530Sstevel@tonic-gate #include <sys/autoconf.h>
540Sstevel@tonic-gate #include <sys/modctl.h>
550Sstevel@tonic-gate #include <sys/epm.h>
560Sstevel@tonic-gate #include <sys/dacf.h>
570Sstevel@tonic-gate #include <sys/sunmdi.h>
580Sstevel@tonic-gate #include <sys/instance.h>
590Sstevel@tonic-gate #include <sys/sdt.h>
600Sstevel@tonic-gate 
610Sstevel@tonic-gate static void i_attach_ctlop(dev_info_t *, ddi_attach_cmd_t, ddi_pre_post_t, int);
620Sstevel@tonic-gate static void i_detach_ctlop(dev_info_t *, ddi_detach_cmd_t, ddi_pre_post_t, int);
630Sstevel@tonic-gate 
640Sstevel@tonic-gate /* decide what to do when a double dev_lclose is detected */
650Sstevel@tonic-gate #ifdef	DEBUG
660Sstevel@tonic-gate int		dev_lclose_ce = CE_PANIC;
670Sstevel@tonic-gate #else	/* DEBUG */
680Sstevel@tonic-gate int		dev_lclose_ce = CE_WARN;
690Sstevel@tonic-gate #endif	/* DEBUG */
700Sstevel@tonic-gate 
710Sstevel@tonic-gate /*
720Sstevel@tonic-gate  * Configuration-related entry points for nexus and leaf drivers
730Sstevel@tonic-gate  */
740Sstevel@tonic-gate int
devi_identify(dev_info_t * devi)750Sstevel@tonic-gate devi_identify(dev_info_t *devi)
760Sstevel@tonic-gate {
770Sstevel@tonic-gate 	struct dev_ops *ops;
780Sstevel@tonic-gate 	int (*fn)(dev_info_t *);
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	if ((ops = ddi_get_driver(devi)) == NULL ||
810Sstevel@tonic-gate 	    (fn = ops->devo_identify) == NULL)
820Sstevel@tonic-gate 		return (-1);
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	return ((*fn)(devi));
850Sstevel@tonic-gate }
860Sstevel@tonic-gate 
870Sstevel@tonic-gate int
devi_probe(dev_info_t * devi)880Sstevel@tonic-gate devi_probe(dev_info_t *devi)
890Sstevel@tonic-gate {
900Sstevel@tonic-gate 	int rv, probe_failed;
910Sstevel@tonic-gate 	pm_ppm_cookie_t ppm_cookie;
920Sstevel@tonic-gate 	struct dev_ops *ops;
930Sstevel@tonic-gate 	int (*fn)(dev_info_t *);
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	ops = ddi_get_driver(devi);
960Sstevel@tonic-gate 	ASSERT(ops);
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	pm_pre_probe(devi, &ppm_cookie);
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	/*
1010Sstevel@tonic-gate 	 * probe(9E) in 2.0 implies that you can get
1020Sstevel@tonic-gate 	 * away with not writing one of these .. so we
1030Sstevel@tonic-gate 	 * pretend we're 'nulldev' if we don't find one (sigh).
1040Sstevel@tonic-gate 	 */
105*11052SChris.Horne@Sun.COM 	if ((fn = ops->devo_probe) == NULL) {
106*11052SChris.Horne@Sun.COM 		if (ddi_dev_is_sid(devi) == DDI_SUCCESS)
107*11052SChris.Horne@Sun.COM 			rv = DDI_PROBE_DONTCARE;
108*11052SChris.Horne@Sun.COM 		else
109*11052SChris.Horne@Sun.COM 			rv = DDI_PROBE_FAILURE;
110*11052SChris.Horne@Sun.COM 	} else
1110Sstevel@tonic-gate 		rv = (*fn)(devi);
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	switch (rv) {
1140Sstevel@tonic-gate 	case DDI_PROBE_DONTCARE:
1150Sstevel@tonic-gate 	case DDI_PROBE_SUCCESS:
1160Sstevel@tonic-gate 		probe_failed = 0;
1170Sstevel@tonic-gate 		break;
1180Sstevel@tonic-gate 	default:
1190Sstevel@tonic-gate 		probe_failed = 1;
1200Sstevel@tonic-gate 		break;
1210Sstevel@tonic-gate 	}
1220Sstevel@tonic-gate 	pm_post_probe(&ppm_cookie, rv, probe_failed);
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	return (rv);
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate /*
1290Sstevel@tonic-gate  * devi_attach()
1300Sstevel@tonic-gate  * 	attach a device instance to the system if the driver supplies an
1310Sstevel@tonic-gate  * 	attach(9E) entrypoint.
1320Sstevel@tonic-gate  */
1330Sstevel@tonic-gate int
devi_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)1340Sstevel@tonic-gate devi_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
1350Sstevel@tonic-gate {
1360Sstevel@tonic-gate 	struct dev_ops *ops;
1370Sstevel@tonic-gate 	int error;
1380Sstevel@tonic-gate 	int (*fn)(dev_info_t *, ddi_attach_cmd_t);
1390Sstevel@tonic-gate 	pm_ppm_cookie_t pc;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	if ((error = mdi_pre_attach(devi, cmd)) != DDI_SUCCESS) {
1420Sstevel@tonic-gate 		return (error);
1430Sstevel@tonic-gate 	}
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	pm_pre_attach(devi, &pc, cmd);
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	if ((cmd == DDI_RESUME || cmd == DDI_PM_RESUME) &&
1480Sstevel@tonic-gate 	    e_ddi_parental_suspend_resume(devi)) {
1490Sstevel@tonic-gate 		error = e_ddi_resume(devi, cmd);
1500Sstevel@tonic-gate 		goto done;
1510Sstevel@tonic-gate 	}
1520Sstevel@tonic-gate 	ops = ddi_get_driver(devi);
1530Sstevel@tonic-gate 	ASSERT(ops);
1540Sstevel@tonic-gate 	if ((fn = ops->devo_attach) == NULL) {
1550Sstevel@tonic-gate 		error = DDI_FAILURE;
1560Sstevel@tonic-gate 		goto done;
1570Sstevel@tonic-gate 	}
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	/*
1600Sstevel@tonic-gate 	 * Call the driver's attach(9e) entrypoint
1610Sstevel@tonic-gate 	 */
1620Sstevel@tonic-gate 	i_attach_ctlop(devi, cmd, DDI_PRE, 0);
1630Sstevel@tonic-gate 	error = (*fn)(devi, cmd);
1640Sstevel@tonic-gate 	i_attach_ctlop(devi, cmd, DDI_POST, error);
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate done:
1670Sstevel@tonic-gate 	pm_post_attach(&pc, error);
1680Sstevel@tonic-gate 	mdi_post_attach(devi, cmd, error);
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	return (error);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate /*
1740Sstevel@tonic-gate  * devi_detach()
1750Sstevel@tonic-gate  * 	detach a device instance from the system if the driver supplies a
1760Sstevel@tonic-gate  * 	detach(9E) entrypoint.
1770Sstevel@tonic-gate  */
1780Sstevel@tonic-gate int
devi_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)1790Sstevel@tonic-gate devi_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
1800Sstevel@tonic-gate {
1810Sstevel@tonic-gate 	struct dev_ops *ops;
1820Sstevel@tonic-gate 	int error;
1830Sstevel@tonic-gate 	int (*fn)(dev_info_t *, ddi_detach_cmd_t);
1840Sstevel@tonic-gate 	pm_ppm_cookie_t pc;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	ASSERT(cmd == DDI_SUSPEND || cmd == DDI_PM_SUSPEND ||
1870Sstevel@tonic-gate 	    cmd == DDI_DETACH);
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	if ((cmd == DDI_SUSPEND || cmd == DDI_PM_SUSPEND) &&
1900Sstevel@tonic-gate 	    e_ddi_parental_suspend_resume(devi)) {
1910Sstevel@tonic-gate 		return (e_ddi_suspend(devi, cmd));
1920Sstevel@tonic-gate 	}
1930Sstevel@tonic-gate 	ops = ddi_get_driver(devi);
1940Sstevel@tonic-gate 	ASSERT(ops);
1950Sstevel@tonic-gate 	if ((fn = ops->devo_detach) == NULL)
1960Sstevel@tonic-gate 		return (DDI_FAILURE);
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	if ((error = mdi_pre_detach(devi, cmd)) != DDI_SUCCESS) {
1990Sstevel@tonic-gate 		return (error);
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 	i_detach_ctlop(devi, cmd, DDI_PRE, 0);
2020Sstevel@tonic-gate 	pm_pre_detach(devi, cmd, &pc);
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	/*
2050Sstevel@tonic-gate 	 * Call the driver's detach routine
2060Sstevel@tonic-gate 	 */
2070Sstevel@tonic-gate 	error = (*fn)(devi, cmd);
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	pm_post_detach(&pc, error);
2100Sstevel@tonic-gate 	i_detach_ctlop(devi, cmd, DDI_POST, error);
2110Sstevel@tonic-gate 	mdi_post_detach(devi, cmd, error);
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	return (error);
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate static void
i_attach_ctlop(dev_info_t * devi,ddi_attach_cmd_t cmd,ddi_pre_post_t w,int ret)2170Sstevel@tonic-gate i_attach_ctlop(dev_info_t *devi, ddi_attach_cmd_t cmd, ddi_pre_post_t w,
2180Sstevel@tonic-gate     int ret)
2190Sstevel@tonic-gate {
2200Sstevel@tonic-gate 	int error;
2210Sstevel@tonic-gate 	struct attachspec as;
2220Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(devi);
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	as.cmd = cmd;
2250Sstevel@tonic-gate 	as.when = w;
2260Sstevel@tonic-gate 	as.pdip = pdip;
2270Sstevel@tonic-gate 	as.result = ret;
2280Sstevel@tonic-gate 	(void) ddi_ctlops(devi, devi, DDI_CTLOPS_ATTACH, &as, &error);
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate static void
i_detach_ctlop(dev_info_t * devi,ddi_detach_cmd_t cmd,ddi_pre_post_t w,int ret)2320Sstevel@tonic-gate i_detach_ctlop(dev_info_t *devi, ddi_detach_cmd_t cmd, ddi_pre_post_t w,
2330Sstevel@tonic-gate     int ret)
2340Sstevel@tonic-gate {
2350Sstevel@tonic-gate 	int error;
2360Sstevel@tonic-gate 	struct detachspec ds;
2370Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(devi);
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	ds.cmd = cmd;
2400Sstevel@tonic-gate 	ds.when = w;
2410Sstevel@tonic-gate 	ds.pdip = pdip;
2420Sstevel@tonic-gate 	ds.result = ret;
2430Sstevel@tonic-gate 	(void) ddi_ctlops(devi, devi, DDI_CTLOPS_DETACH, &ds, &error);
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate /*
2470Sstevel@tonic-gate  * This entry point not defined by Solaris 2.0 DDI/DKI, so
2480Sstevel@tonic-gate  * its inclusion here is somewhat moot.
2490Sstevel@tonic-gate  */
2500Sstevel@tonic-gate int
devi_reset(dev_info_t * devi,ddi_reset_cmd_t cmd)2510Sstevel@tonic-gate devi_reset(dev_info_t *devi, ddi_reset_cmd_t cmd)
2520Sstevel@tonic-gate {
2530Sstevel@tonic-gate 	struct dev_ops *ops;
2540Sstevel@tonic-gate 	int (*fn)(dev_info_t *, ddi_reset_cmd_t);
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	if ((ops = ddi_get_driver(devi)) == NULL ||
2570Sstevel@tonic-gate 	    (fn = ops->devo_reset) == NULL)
2580Sstevel@tonic-gate 		return (DDI_FAILURE);
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	return ((*fn)(devi, cmd));
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate 
2637656SSherry.Moore@Sun.COM int
devi_quiesce(dev_info_t * devi)2647656SSherry.Moore@Sun.COM devi_quiesce(dev_info_t *devi)
2657656SSherry.Moore@Sun.COM {
2667656SSherry.Moore@Sun.COM 	struct dev_ops *ops;
2677656SSherry.Moore@Sun.COM 	int (*fn)(dev_info_t *);
2687656SSherry.Moore@Sun.COM 
2697656SSherry.Moore@Sun.COM 	if (((ops = ddi_get_driver(devi)) == NULL) ||
2707656SSherry.Moore@Sun.COM 	    (ops->devo_rev < 4) || ((fn = ops->devo_quiesce) == NULL))
2717656SSherry.Moore@Sun.COM 		return (DDI_FAILURE);
2727656SSherry.Moore@Sun.COM 
2737656SSherry.Moore@Sun.COM 	return ((*fn)(devi));
2747656SSherry.Moore@Sun.COM }
2757656SSherry.Moore@Sun.COM 
2760Sstevel@tonic-gate /*
2770Sstevel@tonic-gate  * Leaf driver entry points. The following [cb]dev_* functions are *not* part
2780Sstevel@tonic-gate  * of the DDI, please use functions defined in <sys/sunldi.h> and driver_lyr.c.
2790Sstevel@tonic-gate  */
2800Sstevel@tonic-gate int
dev_open(dev_t * devp,int flag,int type,struct cred * cred)2810Sstevel@tonic-gate dev_open(dev_t *devp, int flag, int type, struct cred *cred)
2820Sstevel@tonic-gate {
2830Sstevel@tonic-gate 	struct cb_ops   *cb;
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	cb = devopsp[getmajor(*devp)]->devo_cb_ops;
2860Sstevel@tonic-gate 	return ((*cb->cb_open)(devp, flag, type, cred));
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate int
dev_close(dev_t dev,int flag,int type,struct cred * cred)2900Sstevel@tonic-gate dev_close(dev_t dev, int flag, int type, struct cred *cred)
2910Sstevel@tonic-gate {
2920Sstevel@tonic-gate 	struct cb_ops   *cb;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	cb = (devopsp[getmajor(dev)])->devo_cb_ops;
2950Sstevel@tonic-gate 	return ((*cb->cb_close)(dev, flag, type, cred));
2960Sstevel@tonic-gate }
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate /*
2990Sstevel@tonic-gate  * New Leaf driver open entry point.  We make a vnode and go through specfs
3000Sstevel@tonic-gate  * in order to obtain open close exclusions guarantees.  Note that we drop
3010Sstevel@tonic-gate  * OTYP_LYR if it was specified - we are going through specfs and it provides
3020Sstevel@tonic-gate  * last close semantics (FKLYR is provided to open(9E)).  Also, since
3030Sstevel@tonic-gate  * spec_open will drive attach via e_ddi_hold_devi_by_dev for a makespecvp
3040Sstevel@tonic-gate  * vnode with no SDIP_SET on the common snode, the dev_lopen caller no longer
3050Sstevel@tonic-gate  * needs to call ddi_hold_installed_driver.
3060Sstevel@tonic-gate  */
3070Sstevel@tonic-gate int
dev_lopen(dev_t * devp,int flag,int otype,struct cred * cred)3080Sstevel@tonic-gate dev_lopen(dev_t *devp, int flag, int otype, struct cred *cred)
3090Sstevel@tonic-gate {
3100Sstevel@tonic-gate 	struct vnode	*vp;
3110Sstevel@tonic-gate 	int		error;
3120Sstevel@tonic-gate 	struct vnode	*cvp;
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	vp = makespecvp(*devp, (otype == OTYP_BLK) ? VBLK : VCHR);
3155331Samw 	error = VOP_OPEN(&vp, flag | FKLYR, cred, NULL);
3160Sstevel@tonic-gate 	if (error == 0) {
3170Sstevel@tonic-gate 		/* Pick up the (possibly) new dev_t value. */
3180Sstevel@tonic-gate 		*devp = vp->v_rdev;
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 		/*
3210Sstevel@tonic-gate 		 * Place extra hold on the common vnode, which contains the
3220Sstevel@tonic-gate 		 * open count, so that it is not destroyed by the VN_RELE of
3230Sstevel@tonic-gate 		 * the shadow makespecvp vnode below.
3240Sstevel@tonic-gate 		 */
3250Sstevel@tonic-gate 		cvp = STOV(VTOCS(vp));
3260Sstevel@tonic-gate 		VN_HOLD(cvp);
3270Sstevel@tonic-gate 	}
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	/* release the shadow makespecvp vnode. */
3300Sstevel@tonic-gate 	VN_RELE(vp);
3310Sstevel@tonic-gate 	return (error);
3320Sstevel@tonic-gate }
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate /*
3350Sstevel@tonic-gate  * Leaf driver close entry point.  We make a vnode and go through specfs in
3360Sstevel@tonic-gate  * order to obtain open close exclusions guarantees.  Note that we drop
3370Sstevel@tonic-gate  * OTYP_LYR if it was specified - we are going through specfs and it provides
3380Sstevel@tonic-gate  * last close semantics (FLKYR is provided to close(9E)).
3390Sstevel@tonic-gate  */
3400Sstevel@tonic-gate int
dev_lclose(dev_t dev,int flag,int otype,struct cred * cred)3410Sstevel@tonic-gate dev_lclose(dev_t dev, int flag, int otype, struct cred *cred)
3420Sstevel@tonic-gate {
3430Sstevel@tonic-gate 	struct vnode	*vp;
3440Sstevel@tonic-gate 	int		error;
3450Sstevel@tonic-gate 	struct vnode	*cvp;
3460Sstevel@tonic-gate 	char		*funcname;
3470Sstevel@tonic-gate 	ulong_t		offset;
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	vp = makespecvp(dev, (otype == OTYP_BLK) ? VBLK : VCHR);
3505331Samw 	error = VOP_CLOSE(vp, flag | FKLYR, 1, (offset_t)0, cred, NULL);
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	/*
3530Sstevel@tonic-gate 	 * Release the extra dev_lopen hold on the common vnode. We inline a
3540Sstevel@tonic-gate 	 * VN_RELE(cvp) call so that we can detect more dev_lclose calls than
3550Sstevel@tonic-gate 	 * dev_lopen calls without panic. See vn_rele.  If our inline of
3565331Samw 	 * vn_rele called VOP_INACTIVE(cvp, CRED(), ...) we would panic on the
3570Sstevel@tonic-gate 	 * "release the makespecvp vnode" VN_RELE(vp) that follows  - so
3580Sstevel@tonic-gate 	 * instead we diagnose this situation.  Note that the driver has
3590Sstevel@tonic-gate 	 * still seen a double close(9E), but that would have occurred with
3600Sstevel@tonic-gate 	 * the old dev_close implementation too.
3610Sstevel@tonic-gate 	 */
3620Sstevel@tonic-gate 	cvp = STOV(VTOCS(vp));
3630Sstevel@tonic-gate 	mutex_enter(&cvp->v_lock);
3640Sstevel@tonic-gate 	switch (cvp->v_count) {
3650Sstevel@tonic-gate 	default:
3660Sstevel@tonic-gate 		cvp->v_count--;
3670Sstevel@tonic-gate 		break;
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	case 0:
3700Sstevel@tonic-gate 		VTOS(vp)->s_commonvp = NULL;	/* avoid panic */
3710Sstevel@tonic-gate 		/*FALLTHROUGH*/
3720Sstevel@tonic-gate 	case 1:
3730Sstevel@tonic-gate 		/*
3740Sstevel@tonic-gate 		 * The following message indicates a serious problem in the
3750Sstevel@tonic-gate 		 * identified driver, the driver should be fixed. If obtaining
3760Sstevel@tonic-gate 		 * a panic dump is needed to diagnose the driver problem then
3770Sstevel@tonic-gate 		 * adding "set dev_lclose_ce=3" to /etc/system will cause a
3780Sstevel@tonic-gate 		 * panic when this occurs.
3790Sstevel@tonic-gate 		 */
3800Sstevel@tonic-gate 		funcname = modgetsymname((uintptr_t)caller(), &offset);
3810Sstevel@tonic-gate 		cmn_err(dev_lclose_ce, "dev_lclose: extra close of dev_t 0x%lx "
3820Sstevel@tonic-gate 		    "from %s`%s()", dev, mod_containing_pc(caller()),
3830Sstevel@tonic-gate 		    funcname ? funcname : "unknown...");
3840Sstevel@tonic-gate 		break;
3850Sstevel@tonic-gate 	}
3860Sstevel@tonic-gate 	mutex_exit(&cvp->v_lock);
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	/* release the makespecvp vnode. */
3890Sstevel@tonic-gate 	VN_RELE(vp);
3900Sstevel@tonic-gate 	return (error);
3910Sstevel@tonic-gate }
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate /*
3940Sstevel@tonic-gate  * Returns -1 or the instance number of the given dev_t as
3950Sstevel@tonic-gate  * interpreted by the device driver.  The code may load the driver
3960Sstevel@tonic-gate  * but it does not attach any instances.
3970Sstevel@tonic-gate  *
3980Sstevel@tonic-gate  * Instance is supposed to be a int but drivers have assumed that
3990Sstevel@tonic-gate  * the pointer was a pointer to "void *" instead of a pointer to
4000Sstevel@tonic-gate  * "int *" so we now explicitly pass a pointer to "void *" and then
4010Sstevel@tonic-gate  * cast the result to an int when returning the value.
4020Sstevel@tonic-gate  */
4030Sstevel@tonic-gate int
dev_to_instance(dev_t dev)4040Sstevel@tonic-gate dev_to_instance(dev_t dev)
4050Sstevel@tonic-gate {
4060Sstevel@tonic-gate 	major_t		major = getmajor(dev);
4070Sstevel@tonic-gate 	struct dev_ops	*ops;
4080Sstevel@tonic-gate 	void		*vinstance;
4090Sstevel@tonic-gate 	int		error;
4100Sstevel@tonic-gate 
41110842SJerry.Gilliam@Sun.COM 	/* verify that the driver is loaded */
41210842SJerry.Gilliam@Sun.COM 	if ((ops = mod_hold_dev_by_major(major)) == NULL)
4130Sstevel@tonic-gate 		return (-1);
4140Sstevel@tonic-gate 	ASSERT(CB_DRV_INSTALLED(ops));
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	/* verify that it supports the getinfo(9E) entry point */
4170Sstevel@tonic-gate 	if (ops->devo_getinfo == NULL) {
4180Sstevel@tonic-gate 		mod_rele_dev_by_major(major);
4190Sstevel@tonic-gate 		return (-1);
4200Sstevel@tonic-gate 	}
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	/* ask the driver to extract the instance number from the devt */
4230Sstevel@tonic-gate 	error = (*ops->devo_getinfo)(NULL, DDI_INFO_DEVT2INSTANCE,
4240Sstevel@tonic-gate 	    (void *)dev, &vinstance);
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	/* release the driver */
4270Sstevel@tonic-gate 	mod_rele_dev_by_major(major);
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	if (error != DDI_SUCCESS)
4300Sstevel@tonic-gate 		return (-1);
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	return ((int)(uintptr_t)vinstance);
4330Sstevel@tonic-gate }
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate static void
bdev_strategy_tnf_probe(struct buf * bp)4360Sstevel@tonic-gate bdev_strategy_tnf_probe(struct buf *bp)
4370Sstevel@tonic-gate {
4380Sstevel@tonic-gate 	/* Kernel probe */
4390Sstevel@tonic-gate 	TNF_PROBE_5(strategy, "io blockio", /* CSTYLED */,
4404582Scth 	    tnf_device, device, bp->b_edev,
4414582Scth 	    tnf_diskaddr, block, bp->b_lblkno,
4424582Scth 	    tnf_size, size, bp->b_bcount,
4434582Scth 	    tnf_opaque, buf, bp,
4444582Scth 	    tnf_bioflags, flags, bp->b_flags);
4450Sstevel@tonic-gate }
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate int
bdev_strategy(struct buf * bp)4480Sstevel@tonic-gate bdev_strategy(struct buf *bp)
4490Sstevel@tonic-gate {
4500Sstevel@tonic-gate 	struct dev_ops *ops;
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	ops = devopsp[getmajor(bp->b_edev)];
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	/*
4550Sstevel@tonic-gate 	 * Before we hit the io:::start probe, we need to fill in the b_dip
4560Sstevel@tonic-gate 	 * field of the buf structure.  This should be -- for the most part --
4570Sstevel@tonic-gate 	 * incredibly cheap.  If you're in this code looking to bum cycles,
4580Sstevel@tonic-gate 	 * there is almost certainly bigger game further down the I/O path...
4590Sstevel@tonic-gate 	 */
4600Sstevel@tonic-gate 	(void) ops->devo_getinfo(NULL, DDI_INFO_DEVT2DEVINFO,
4610Sstevel@tonic-gate 	    (void *)bp->b_edev, (void **)&bp->b_dip);
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	DTRACE_IO1(start, struct buf *, bp);
4640Sstevel@tonic-gate 	bp->b_flags |= B_STARTED;
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	/*
4670Sstevel@tonic-gate 	 * Call the TNF probe here instead of the inline code
4680Sstevel@tonic-gate 	 * to force our compiler to use the tail call optimization.
4690Sstevel@tonic-gate 	 */
4700Sstevel@tonic-gate 	bdev_strategy_tnf_probe(bp);
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	return (ops->devo_cb_ops->cb_strategy(bp));
4730Sstevel@tonic-gate }
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate int
bdev_print(dev_t dev,caddr_t str)4760Sstevel@tonic-gate bdev_print(dev_t dev, caddr_t str)
4770Sstevel@tonic-gate {
4780Sstevel@tonic-gate 	struct cb_ops	*cb;
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	cb = devopsp[getmajor(dev)]->devo_cb_ops;
4810Sstevel@tonic-gate 	return ((*cb->cb_print)(dev, str));
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate 
4844582Scth /*
4854582Scth  * Return number of DEV_BSIZE byte blocks.
4864582Scth  */
4870Sstevel@tonic-gate int
bdev_size(dev_t dev)4880Sstevel@tonic-gate bdev_size(dev_t dev)
4890Sstevel@tonic-gate {
4904582Scth 	uint_t		nblocks;
4914582Scth 	uint_t		blksize;
4924582Scth 
4934582Scth 	if ((nblocks = e_ddi_getprop(dev, VBLK, "nblocks",
4944582Scth 	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, -1)) == -1)
4954582Scth 		return (-1);
4964582Scth 
4974582Scth 	/* Get blksize, default to DEV_BSIZE */
4984582Scth 	if ((blksize = e_ddi_getprop(dev, VBLK, "blksize",
4994582Scth 	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, -1)) == -1)
5004582Scth 		blksize = e_ddi_getprop(DDI_DEV_T_ANY, VBLK, "device-blksize",
5014582Scth 		    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, DEV_BSIZE);
5024582Scth 
5034582Scth 	if (blksize >= DEV_BSIZE)
5044582Scth 		return (nblocks * (blksize / DEV_BSIZE));
5054582Scth 	else
5064582Scth 		return (nblocks / (DEV_BSIZE / blksize));
5070Sstevel@tonic-gate }
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate /*
5100Sstevel@tonic-gate  * Same for 64-bit Nblocks property
5110Sstevel@tonic-gate  */
5120Sstevel@tonic-gate uint64_t
bdev_Size(dev_t dev)5130Sstevel@tonic-gate bdev_Size(dev_t dev)
5140Sstevel@tonic-gate {
5154582Scth 	uint64_t	nblocks;
5164582Scth 	uint_t		blksize;
5174582Scth 
5184582Scth 	if ((nblocks = e_ddi_getprop_int64(dev, VBLK, "Nblocks",
5194582Scth 	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, -1)) == -1)
5204582Scth 		return (-1);
5214582Scth 
5224582Scth 	/* Get blksize, default to DEV_BSIZE */
5234582Scth 	if ((blksize = e_ddi_getprop(dev, VBLK, "blksize",
5244582Scth 	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, -1)) == -1)
5254582Scth 		blksize = e_ddi_getprop(DDI_DEV_T_ANY, VBLK, "device-blksize",
5264582Scth 		    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, DEV_BSIZE);
5274582Scth 
5284582Scth 	if (blksize >= DEV_BSIZE)
5294582Scth 		return (nblocks * (blksize / DEV_BSIZE));
5304582Scth 	else
5314582Scth 		return (nblocks / (DEV_BSIZE / blksize));
5320Sstevel@tonic-gate }
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate int
bdev_dump(dev_t dev,caddr_t addr,daddr_t blkno,int blkcnt)5350Sstevel@tonic-gate bdev_dump(dev_t dev, caddr_t addr, daddr_t blkno, int blkcnt)
5360Sstevel@tonic-gate {
5370Sstevel@tonic-gate 	struct cb_ops	*cb;
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	cb = devopsp[getmajor(dev)]->devo_cb_ops;
5400Sstevel@tonic-gate 	return ((*cb->cb_dump)(dev, addr, blkno, blkcnt));
5410Sstevel@tonic-gate }
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate int
cdev_read(dev_t dev,struct uio * uiop,struct cred * cred)5440Sstevel@tonic-gate cdev_read(dev_t dev, struct uio *uiop, struct cred *cred)
5450Sstevel@tonic-gate {
5460Sstevel@tonic-gate 	struct cb_ops	*cb;
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	cb = devopsp[getmajor(dev)]->devo_cb_ops;
5490Sstevel@tonic-gate 	return ((*cb->cb_read)(dev, uiop, cred));
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate int
cdev_write(dev_t dev,struct uio * uiop,struct cred * cred)5530Sstevel@tonic-gate cdev_write(dev_t dev, struct uio *uiop, struct cred *cred)
5540Sstevel@tonic-gate {
5550Sstevel@tonic-gate 	struct cb_ops	*cb;
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	cb = devopsp[getmajor(dev)]->devo_cb_ops;
5580Sstevel@tonic-gate 	return ((*cb->cb_write)(dev, uiop, cred));
5590Sstevel@tonic-gate }
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate int
cdev_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,struct cred * cred,int * rvalp)5620Sstevel@tonic-gate cdev_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, struct cred *cred,
5630Sstevel@tonic-gate     int *rvalp)
5640Sstevel@tonic-gate {
5650Sstevel@tonic-gate 	struct cb_ops	*cb;
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	cb = devopsp[getmajor(dev)]->devo_cb_ops;
5680Sstevel@tonic-gate 	return ((*cb->cb_ioctl)(dev, cmd, arg, mode, cred, rvalp));
5690Sstevel@tonic-gate }
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate int
cdev_devmap(dev_t dev,devmap_cookie_t dhp,offset_t off,size_t len,size_t * maplen,uint_t mode)5720Sstevel@tonic-gate cdev_devmap(dev_t dev, devmap_cookie_t dhp, offset_t off, size_t len,
5730Sstevel@tonic-gate 	size_t *maplen, uint_t mode)
5740Sstevel@tonic-gate {
5750Sstevel@tonic-gate 	struct cb_ops	*cb;
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	cb = devopsp[getmajor(dev)]->devo_cb_ops;
5780Sstevel@tonic-gate 	return ((*cb->cb_devmap)(dev, dhp, off, len, maplen, mode));
5790Sstevel@tonic-gate }
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate int
cdev_mmap(int (* mapfunc)(dev_t,off_t,int),dev_t dev,off_t off,int prot)5820Sstevel@tonic-gate cdev_mmap(int (*mapfunc)(dev_t, off_t, int), dev_t dev, off_t off, int prot)
5830Sstevel@tonic-gate {
5840Sstevel@tonic-gate 	return ((*mapfunc)(dev, off, prot));
5850Sstevel@tonic-gate }
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate int
cdev_segmap(dev_t dev,off_t off,struct as * as,caddr_t * addrp,off_t len,uint_t prot,uint_t maxprot,uint_t flags,cred_t * credp)5880Sstevel@tonic-gate cdev_segmap(dev_t dev, off_t off, struct as *as, caddr_t *addrp, off_t len,
5890Sstevel@tonic-gate 	    uint_t prot, uint_t maxprot, uint_t flags, cred_t *credp)
5900Sstevel@tonic-gate {
5910Sstevel@tonic-gate 	struct cb_ops	*cb;
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	cb = devopsp[getmajor(dev)]->devo_cb_ops;
5940Sstevel@tonic-gate 	return ((*cb->cb_segmap)(dev, off, as, addrp,
5950Sstevel@tonic-gate 	    len, prot, maxprot, flags, credp));
5960Sstevel@tonic-gate }
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate int
cdev_poll(dev_t dev,short events,int anyyet,short * reventsp,struct pollhead ** pollhdrp)5990Sstevel@tonic-gate cdev_poll(dev_t dev, short events, int anyyet, short *reventsp,
6000Sstevel@tonic-gate 	struct pollhead **pollhdrp)
6010Sstevel@tonic-gate {
6020Sstevel@tonic-gate 	struct cb_ops	*cb;
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	cb = devopsp[getmajor(dev)]->devo_cb_ops;
6050Sstevel@tonic-gate 	return ((*cb->cb_chpoll)(dev, events, anyyet, reventsp, pollhdrp));
6060Sstevel@tonic-gate }
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate /*
6090Sstevel@tonic-gate  * A 'size' property can be provided by a VCHR device.
6100Sstevel@tonic-gate  *
6110Sstevel@tonic-gate  * Since it's defined as zero for STREAMS devices, so we avoid the
6120Sstevel@tonic-gate  * overhead of looking it up.  Note also that we don't force an
6130Sstevel@tonic-gate  * unused driver into memory simply to ask about it's size.  We also
6140Sstevel@tonic-gate  * don't bother to ask it its size unless it's already been attached
6150Sstevel@tonic-gate  * (the attach routine is the earliest place the property will be created)
6160Sstevel@tonic-gate  *
6170Sstevel@tonic-gate  * XXX	In an ideal world, we'd call this at VOP_GETATTR() time.
6180Sstevel@tonic-gate  */
6190Sstevel@tonic-gate int
cdev_size(dev_t dev)6200Sstevel@tonic-gate cdev_size(dev_t dev)
6210Sstevel@tonic-gate {
6220Sstevel@tonic-gate 	major_t maj;
6230Sstevel@tonic-gate 	struct devnames *dnp;
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	if ((maj = getmajor(dev)) >= devcnt)
6260Sstevel@tonic-gate 		return (0);
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	dnp = &(devnamesp[maj]);
6290Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
6300Sstevel@tonic-gate 	if (devopsp[maj] && devopsp[maj]->devo_cb_ops &&
6310Sstevel@tonic-gate 	    !devopsp[maj]->devo_cb_ops->cb_str) {
6320Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
6330Sstevel@tonic-gate 		return (e_ddi_getprop(dev, VCHR, "size",
6340Sstevel@tonic-gate 		    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, 0));
6350Sstevel@tonic-gate 	}
6360Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
6370Sstevel@tonic-gate 	return (0);
6380Sstevel@tonic-gate }
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate /*
6410Sstevel@tonic-gate  * same for 64-bit Size property
6420Sstevel@tonic-gate  */
6430Sstevel@tonic-gate uint64_t
cdev_Size(dev_t dev)6440Sstevel@tonic-gate cdev_Size(dev_t dev)
6450Sstevel@tonic-gate {
6460Sstevel@tonic-gate 	major_t maj;
6470Sstevel@tonic-gate 	struct devnames *dnp;
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	if ((maj = getmajor(dev)) >= devcnt)
6500Sstevel@tonic-gate 		return (0);
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 	dnp = &(devnamesp[maj]);
6530Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
6540Sstevel@tonic-gate 	if (devopsp[maj] && devopsp[maj]->devo_cb_ops &&
6550Sstevel@tonic-gate 	    !devopsp[maj]->devo_cb_ops->cb_str) {
6560Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
6570Sstevel@tonic-gate 		return (e_ddi_getprop_int64(dev, VCHR, "Size",
6580Sstevel@tonic-gate 		    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, 0));
6590Sstevel@tonic-gate 	}
6600Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
6610Sstevel@tonic-gate 	return (0);
6620Sstevel@tonic-gate }
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate /*
6650Sstevel@tonic-gate  * XXX	This routine is poorly named, because block devices can and do
6660Sstevel@tonic-gate  *	have properties (see bdev_size() above).
6670Sstevel@tonic-gate  *
6680Sstevel@tonic-gate  * XXX	fix the comment in devops.h that claims that cb_prop_op
6690Sstevel@tonic-gate  *	is character-only.
6700Sstevel@tonic-gate  */
6710Sstevel@tonic-gate int
cdev_prop_op(dev_t dev,dev_info_t * dip,ddi_prop_op_t prop_op,int mod_flags,char * name,caddr_t valuep,int * lengthp)6720Sstevel@tonic-gate cdev_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
6730Sstevel@tonic-gate     char *name, caddr_t valuep, int *lengthp)
6740Sstevel@tonic-gate {
6750Sstevel@tonic-gate 	struct cb_ops	*cb;
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	if ((cb = devopsp[DEVI(dip)->devi_major]->devo_cb_ops) == NULL)
6780Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	return ((*cb->cb_prop_op)(dev, dip, prop_op, mod_flags,
6810Sstevel@tonic-gate 	    name, valuep, lengthp));
6820Sstevel@tonic-gate }
683