xref: /illumos-gate/usr/src/uts/common/os/driver.c (revision ade42b55)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5184cd04cScth  * Common Development and Distribution License (the "License").
6184cd04cScth  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22c9cc1492SJerry Gilliam  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
25*ade42b55SSebastien Roy /*
26*ade42b55SSebastien Roy  * Copyright (c) 2017 by Delphix. All rights reserved.
27*ade42b55SSebastien Roy  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/conf.h>
337c478bd9Sstevel@tonic-gate #include <sys/systm.h>
347c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
357c478bd9Sstevel@tonic-gate #include <sys/buf.h>
367c478bd9Sstevel@tonic-gate #include <sys/cred.h>
377c478bd9Sstevel@tonic-gate #include <sys/user.h>
387c478bd9Sstevel@tonic-gate #include <sys/stat.h>
397c478bd9Sstevel@tonic-gate #include <sys/uio.h>
407c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
417c478bd9Sstevel@tonic-gate #include <sys/fs/snode.h>
427c478bd9Sstevel@tonic-gate #include <sys/open.h>
437c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
447c478bd9Sstevel@tonic-gate #include <sys/file.h>
457c478bd9Sstevel@tonic-gate #include <sys/debug.h>
467c478bd9Sstevel@tonic-gate #include <sys/tnf_probe.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /* Don't #include <sys/ddi.h> - it #undef's getmajor() */
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
517c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
527c478bd9Sstevel@tonic-gate #include <sys/sunpm.h>
537c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
547c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
557c478bd9Sstevel@tonic-gate #include <sys/esunddi.h>
567c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
577c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
587c478bd9Sstevel@tonic-gate #include <sys/epm.h>
597c478bd9Sstevel@tonic-gate #include <sys/dacf.h>
607c478bd9Sstevel@tonic-gate #include <sys/sunmdi.h>
617c478bd9Sstevel@tonic-gate #include <sys/instance.h>
627c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate static void i_attach_ctlop(dev_info_t *, ddi_attach_cmd_t, ddi_pre_post_t, int);
657c478bd9Sstevel@tonic-gate static void i_detach_ctlop(dev_info_t *, ddi_detach_cmd_t, ddi_pre_post_t, int);
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /* decide what to do when a double dev_lclose is detected */
687c478bd9Sstevel@tonic-gate #ifdef	DEBUG
697c478bd9Sstevel@tonic-gate int		dev_lclose_ce = CE_PANIC;
707c478bd9Sstevel@tonic-gate #else	/* DEBUG */
717c478bd9Sstevel@tonic-gate int		dev_lclose_ce = CE_WARN;
727c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate  * Configuration-related entry points for nexus and leaf drivers
767c478bd9Sstevel@tonic-gate  */
777c478bd9Sstevel@tonic-gate int
devi_identify(dev_info_t * devi)787c478bd9Sstevel@tonic-gate devi_identify(dev_info_t *devi)
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate 	struct dev_ops *ops;
817c478bd9Sstevel@tonic-gate 	int (*fn)(dev_info_t *);
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	if ((ops = ddi_get_driver(devi)) == NULL ||
847c478bd9Sstevel@tonic-gate 	    (fn = ops->devo_identify) == NULL)
857c478bd9Sstevel@tonic-gate 		return (-1);
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	return ((*fn)(devi));
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate int
devi_probe(dev_info_t * devi)917c478bd9Sstevel@tonic-gate devi_probe(dev_info_t *devi)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	int rv, probe_failed;
947c478bd9Sstevel@tonic-gate 	pm_ppm_cookie_t ppm_cookie;
957c478bd9Sstevel@tonic-gate 	struct dev_ops *ops;
967c478bd9Sstevel@tonic-gate 	int (*fn)(dev_info_t *);
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	ops = ddi_get_driver(devi);
997c478bd9Sstevel@tonic-gate 	ASSERT(ops);
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	pm_pre_probe(devi, &ppm_cookie);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	/*
1047c478bd9Sstevel@tonic-gate 	 * probe(9E) in 2.0 implies that you can get
1057c478bd9Sstevel@tonic-gate 	 * away with not writing one of these .. so we
1067c478bd9Sstevel@tonic-gate 	 * pretend we're 'nulldev' if we don't find one (sigh).
1077c478bd9Sstevel@tonic-gate 	 */
10896c4a178SChris Horne 	if ((fn = ops->devo_probe) == NULL) {
10996c4a178SChris Horne 		if (ddi_dev_is_sid(devi) == DDI_SUCCESS)
11096c4a178SChris Horne 			rv = DDI_PROBE_DONTCARE;
11196c4a178SChris Horne 		else
11296c4a178SChris Horne 			rv = DDI_PROBE_FAILURE;
11396c4a178SChris Horne 	} else
1147c478bd9Sstevel@tonic-gate 		rv = (*fn)(devi);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	switch (rv) {
1177c478bd9Sstevel@tonic-gate 	case DDI_PROBE_DONTCARE:
1187c478bd9Sstevel@tonic-gate 	case DDI_PROBE_SUCCESS:
1197c478bd9Sstevel@tonic-gate 		probe_failed = 0;
1207c478bd9Sstevel@tonic-gate 		break;
1217c478bd9Sstevel@tonic-gate 	default:
1227c478bd9Sstevel@tonic-gate 		probe_failed = 1;
1237c478bd9Sstevel@tonic-gate 		break;
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 	pm_post_probe(&ppm_cookie, rv, probe_failed);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	return (rv);
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate /*
1327c478bd9Sstevel@tonic-gate  * devi_attach()
1337c478bd9Sstevel@tonic-gate  * 	attach a device instance to the system if the driver supplies an
1347c478bd9Sstevel@tonic-gate  * 	attach(9E) entrypoint.
1357c478bd9Sstevel@tonic-gate  */
1367c478bd9Sstevel@tonic-gate int
devi_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)1377c478bd9Sstevel@tonic-gate devi_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate 	struct dev_ops *ops;
1407c478bd9Sstevel@tonic-gate 	int error;
1417c478bd9Sstevel@tonic-gate 	int (*fn)(dev_info_t *, ddi_attach_cmd_t);
1427c478bd9Sstevel@tonic-gate 	pm_ppm_cookie_t pc;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	if ((error = mdi_pre_attach(devi, cmd)) != DDI_SUCCESS) {
1457c478bd9Sstevel@tonic-gate 		return (error);
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	pm_pre_attach(devi, &pc, cmd);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	if ((cmd == DDI_RESUME || cmd == DDI_PM_RESUME) &&
1517c478bd9Sstevel@tonic-gate 	    e_ddi_parental_suspend_resume(devi)) {
1527c478bd9Sstevel@tonic-gate 		error = e_ddi_resume(devi, cmd);
1537c478bd9Sstevel@tonic-gate 		goto done;
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 	ops = ddi_get_driver(devi);
1567c478bd9Sstevel@tonic-gate 	ASSERT(ops);
1577c478bd9Sstevel@tonic-gate 	if ((fn = ops->devo_attach) == NULL) {
1587c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
1597c478bd9Sstevel@tonic-gate 		goto done;
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	/*
1637c478bd9Sstevel@tonic-gate 	 * Call the driver's attach(9e) entrypoint
1647c478bd9Sstevel@tonic-gate 	 */
1657c478bd9Sstevel@tonic-gate 	i_attach_ctlop(devi, cmd, DDI_PRE, 0);
1667c478bd9Sstevel@tonic-gate 	error = (*fn)(devi, cmd);
1677c478bd9Sstevel@tonic-gate 	i_attach_ctlop(devi, cmd, DDI_POST, error);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate done:
1707c478bd9Sstevel@tonic-gate 	pm_post_attach(&pc, error);
1717c478bd9Sstevel@tonic-gate 	mdi_post_attach(devi, cmd, error);
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	return (error);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate  * devi_detach()
1787c478bd9Sstevel@tonic-gate  * 	detach a device instance from the system if the driver supplies a
1797c478bd9Sstevel@tonic-gate  * 	detach(9E) entrypoint.
1807c478bd9Sstevel@tonic-gate  */
1817c478bd9Sstevel@tonic-gate int
devi_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)1827c478bd9Sstevel@tonic-gate devi_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
1837c478bd9Sstevel@tonic-gate {
1847c478bd9Sstevel@tonic-gate 	struct dev_ops *ops;
1857c478bd9Sstevel@tonic-gate 	int error;
1867c478bd9Sstevel@tonic-gate 	int (*fn)(dev_info_t *, ddi_detach_cmd_t);
1877c478bd9Sstevel@tonic-gate 	pm_ppm_cookie_t pc;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	ASSERT(cmd == DDI_SUSPEND || cmd == DDI_PM_SUSPEND ||
1907c478bd9Sstevel@tonic-gate 	    cmd == DDI_DETACH);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	if ((cmd == DDI_SUSPEND || cmd == DDI_PM_SUSPEND) &&
1937c478bd9Sstevel@tonic-gate 	    e_ddi_parental_suspend_resume(devi)) {
1947c478bd9Sstevel@tonic-gate 		return (e_ddi_suspend(devi, cmd));
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 	ops = ddi_get_driver(devi);
1977c478bd9Sstevel@tonic-gate 	ASSERT(ops);
1987c478bd9Sstevel@tonic-gate 	if ((fn = ops->devo_detach) == NULL)
1997c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if ((error = mdi_pre_detach(devi, cmd)) != DDI_SUCCESS) {
2027c478bd9Sstevel@tonic-gate 		return (error);
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate 	i_detach_ctlop(devi, cmd, DDI_PRE, 0);
2057c478bd9Sstevel@tonic-gate 	pm_pre_detach(devi, cmd, &pc);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	/*
2087c478bd9Sstevel@tonic-gate 	 * Call the driver's detach routine
2097c478bd9Sstevel@tonic-gate 	 */
2107c478bd9Sstevel@tonic-gate 	error = (*fn)(devi, cmd);
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	pm_post_detach(&pc, error);
2137c478bd9Sstevel@tonic-gate 	i_detach_ctlop(devi, cmd, DDI_POST, error);
2147c478bd9Sstevel@tonic-gate 	mdi_post_detach(devi, cmd, error);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	return (error);
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate static void
i_attach_ctlop(dev_info_t * devi,ddi_attach_cmd_t cmd,ddi_pre_post_t w,int ret)2207c478bd9Sstevel@tonic-gate i_attach_ctlop(dev_info_t *devi, ddi_attach_cmd_t cmd, ddi_pre_post_t w,
2217c478bd9Sstevel@tonic-gate     int ret)
2227c478bd9Sstevel@tonic-gate {
2237c478bd9Sstevel@tonic-gate 	int error;
2247c478bd9Sstevel@tonic-gate 	struct attachspec as;
2257c478bd9Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(devi);
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	as.cmd = cmd;
2287c478bd9Sstevel@tonic-gate 	as.when = w;
2297c478bd9Sstevel@tonic-gate 	as.pdip = pdip;
2307c478bd9Sstevel@tonic-gate 	as.result = ret;
2317c478bd9Sstevel@tonic-gate 	(void) ddi_ctlops(devi, devi, DDI_CTLOPS_ATTACH, &as, &error);
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate static void
i_detach_ctlop(dev_info_t * devi,ddi_detach_cmd_t cmd,ddi_pre_post_t w,int ret)2357c478bd9Sstevel@tonic-gate i_detach_ctlop(dev_info_t *devi, ddi_detach_cmd_t cmd, ddi_pre_post_t w,
2367c478bd9Sstevel@tonic-gate     int ret)
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate 	int error;
2397c478bd9Sstevel@tonic-gate 	struct detachspec ds;
2407c478bd9Sstevel@tonic-gate 	dev_info_t *pdip = ddi_get_parent(devi);
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	ds.cmd = cmd;
2437c478bd9Sstevel@tonic-gate 	ds.when = w;
2447c478bd9Sstevel@tonic-gate 	ds.pdip = pdip;
2457c478bd9Sstevel@tonic-gate 	ds.result = ret;
2467c478bd9Sstevel@tonic-gate 	(void) ddi_ctlops(devi, devi, DDI_CTLOPS_DETACH, &ds, &error);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate /*
2507c478bd9Sstevel@tonic-gate  * This entry point not defined by Solaris 2.0 DDI/DKI, so
2517c478bd9Sstevel@tonic-gate  * its inclusion here is somewhat moot.
2527c478bd9Sstevel@tonic-gate  */
2537c478bd9Sstevel@tonic-gate int
devi_reset(dev_info_t * devi,ddi_reset_cmd_t cmd)2547c478bd9Sstevel@tonic-gate devi_reset(dev_info_t *devi, ddi_reset_cmd_t cmd)
2557c478bd9Sstevel@tonic-gate {
2567c478bd9Sstevel@tonic-gate 	struct dev_ops *ops;
2577c478bd9Sstevel@tonic-gate 	int (*fn)(dev_info_t *, ddi_reset_cmd_t);
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	if ((ops = ddi_get_driver(devi)) == NULL ||
2607c478bd9Sstevel@tonic-gate 	    (fn = ops->devo_reset) == NULL)
2617c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	return ((*fn)(devi, cmd));
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate 
26619397407SSherry Moore int
devi_quiesce(dev_info_t * devi)26719397407SSherry Moore devi_quiesce(dev_info_t *devi)
26819397407SSherry Moore {
26919397407SSherry Moore 	struct dev_ops *ops;
27019397407SSherry Moore 	int (*fn)(dev_info_t *);
27119397407SSherry Moore 
27219397407SSherry Moore 	if (((ops = ddi_get_driver(devi)) == NULL) ||
27319397407SSherry Moore 	    (ops->devo_rev < 4) || ((fn = ops->devo_quiesce) == NULL))
27419397407SSherry Moore 		return (DDI_FAILURE);
27519397407SSherry Moore 
27619397407SSherry Moore 	return ((*fn)(devi));
27719397407SSherry Moore }
27819397407SSherry Moore 
2797c478bd9Sstevel@tonic-gate /*
2807c478bd9Sstevel@tonic-gate  * Leaf driver entry points. The following [cb]dev_* functions are *not* part
2817c478bd9Sstevel@tonic-gate  * of the DDI, please use functions defined in <sys/sunldi.h> and driver_lyr.c.
2827c478bd9Sstevel@tonic-gate  */
2837c478bd9Sstevel@tonic-gate int
dev_open(dev_t * devp,int flag,int type,struct cred * cred)2847c478bd9Sstevel@tonic-gate dev_open(dev_t *devp, int flag, int type, struct cred *cred)
2857c478bd9Sstevel@tonic-gate {
2867c478bd9Sstevel@tonic-gate 	struct cb_ops   *cb;
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	cb = devopsp[getmajor(*devp)]->devo_cb_ops;
2897c478bd9Sstevel@tonic-gate 	return ((*cb->cb_open)(devp, flag, type, cred));
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate int
dev_close(dev_t dev,int flag,int type,struct cred * cred)2937c478bd9Sstevel@tonic-gate dev_close(dev_t dev, int flag, int type, struct cred *cred)
2947c478bd9Sstevel@tonic-gate {
2957c478bd9Sstevel@tonic-gate 	struct cb_ops   *cb;
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	cb = (devopsp[getmajor(dev)])->devo_cb_ops;
2987c478bd9Sstevel@tonic-gate 	return ((*cb->cb_close)(dev, flag, type, cred));
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate /*
3027c478bd9Sstevel@tonic-gate  * New Leaf driver open entry point.  We make a vnode and go through specfs
3037c478bd9Sstevel@tonic-gate  * in order to obtain open close exclusions guarantees.  Note that we drop
3047c478bd9Sstevel@tonic-gate  * OTYP_LYR if it was specified - we are going through specfs and it provides
3057c478bd9Sstevel@tonic-gate  * last close semantics (FKLYR is provided to open(9E)).  Also, since
3067c478bd9Sstevel@tonic-gate  * spec_open will drive attach via e_ddi_hold_devi_by_dev for a makespecvp
3077c478bd9Sstevel@tonic-gate  * vnode with no SDIP_SET on the common snode, the dev_lopen caller no longer
3087c478bd9Sstevel@tonic-gate  * needs to call ddi_hold_installed_driver.
3097c478bd9Sstevel@tonic-gate  */
3107c478bd9Sstevel@tonic-gate int
dev_lopen(dev_t * devp,int flag,int otype,struct cred * cred)3117c478bd9Sstevel@tonic-gate dev_lopen(dev_t *devp, int flag, int otype, struct cred *cred)
3127c478bd9Sstevel@tonic-gate {
3137c478bd9Sstevel@tonic-gate 	struct vnode	*vp;
3147c478bd9Sstevel@tonic-gate 	int		error;
3157c478bd9Sstevel@tonic-gate 	struct vnode	*cvp;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	vp = makespecvp(*devp, (otype == OTYP_BLK) ? VBLK : VCHR);
318da6c28aaSamw 	error = VOP_OPEN(&vp, flag | FKLYR, cred, NULL);
3197c478bd9Sstevel@tonic-gate 	if (error == 0) {
3207c478bd9Sstevel@tonic-gate 		/* Pick up the (possibly) new dev_t value. */
3217c478bd9Sstevel@tonic-gate 		*devp = vp->v_rdev;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 		/*
3247c478bd9Sstevel@tonic-gate 		 * Place extra hold on the common vnode, which contains the
3257c478bd9Sstevel@tonic-gate 		 * open count, so that it is not destroyed by the VN_RELE of
3267c478bd9Sstevel@tonic-gate 		 * the shadow makespecvp vnode below.
3277c478bd9Sstevel@tonic-gate 		 */
3287c478bd9Sstevel@tonic-gate 		cvp = STOV(VTOCS(vp));
3297c478bd9Sstevel@tonic-gate 		VN_HOLD(cvp);
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	/* release the shadow makespecvp vnode. */
3337c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
3347c478bd9Sstevel@tonic-gate 	return (error);
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate /*
3387c478bd9Sstevel@tonic-gate  * Leaf driver close entry point.  We make a vnode and go through specfs in
3397c478bd9Sstevel@tonic-gate  * order to obtain open close exclusions guarantees.  Note that we drop
3407c478bd9Sstevel@tonic-gate  * OTYP_LYR if it was specified - we are going through specfs and it provides
3417c478bd9Sstevel@tonic-gate  * last close semantics (FLKYR is provided to close(9E)).
3427c478bd9Sstevel@tonic-gate  */
3437c478bd9Sstevel@tonic-gate int
dev_lclose(dev_t dev,int flag,int otype,struct cred * cred)3447c478bd9Sstevel@tonic-gate dev_lclose(dev_t dev, int flag, int otype, struct cred *cred)
3457c478bd9Sstevel@tonic-gate {
3467c478bd9Sstevel@tonic-gate 	struct vnode	*vp;
3477c478bd9Sstevel@tonic-gate 	int		error;
3487c478bd9Sstevel@tonic-gate 	struct vnode	*cvp;
3497c478bd9Sstevel@tonic-gate 	char		*funcname;
3507c478bd9Sstevel@tonic-gate 	ulong_t		offset;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	vp = makespecvp(dev, (otype == OTYP_BLK) ? VBLK : VCHR);
353da6c28aaSamw 	error = VOP_CLOSE(vp, flag | FKLYR, 1, (offset_t)0, cred, NULL);
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	/*
3567c478bd9Sstevel@tonic-gate 	 * Release the extra dev_lopen hold on the common vnode. We inline a
3577c478bd9Sstevel@tonic-gate 	 * VN_RELE(cvp) call so that we can detect more dev_lclose calls than
3587c478bd9Sstevel@tonic-gate 	 * dev_lopen calls without panic. See vn_rele.  If our inline of
359da6c28aaSamw 	 * vn_rele called VOP_INACTIVE(cvp, CRED(), ...) we would panic on the
3607c478bd9Sstevel@tonic-gate 	 * "release the makespecvp vnode" VN_RELE(vp) that follows  - so
3617c478bd9Sstevel@tonic-gate 	 * instead we diagnose this situation.  Note that the driver has
3627c478bd9Sstevel@tonic-gate 	 * still seen a double close(9E), but that would have occurred with
3637c478bd9Sstevel@tonic-gate 	 * the old dev_close implementation too.
3647c478bd9Sstevel@tonic-gate 	 */
3657c478bd9Sstevel@tonic-gate 	cvp = STOV(VTOCS(vp));
3667c478bd9Sstevel@tonic-gate 	mutex_enter(&cvp->v_lock);
3677c478bd9Sstevel@tonic-gate 	switch (cvp->v_count) {
3687c478bd9Sstevel@tonic-gate 	default:
369*ade42b55SSebastien Roy 		VN_RELE_LOCKED(cvp);
3707c478bd9Sstevel@tonic-gate 		break;
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	case 0:
3737c478bd9Sstevel@tonic-gate 		VTOS(vp)->s_commonvp = NULL;	/* avoid panic */
3747c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
3757c478bd9Sstevel@tonic-gate 	case 1:
3767c478bd9Sstevel@tonic-gate 		/*
3777c478bd9Sstevel@tonic-gate 		 * The following message indicates a serious problem in the
3787c478bd9Sstevel@tonic-gate 		 * identified driver, the driver should be fixed. If obtaining
3797c478bd9Sstevel@tonic-gate 		 * a panic dump is needed to diagnose the driver problem then
3807c478bd9Sstevel@tonic-gate 		 * adding "set dev_lclose_ce=3" to /etc/system will cause a
3817c478bd9Sstevel@tonic-gate 		 * panic when this occurs.
3827c478bd9Sstevel@tonic-gate 		 */
3837c478bd9Sstevel@tonic-gate 		funcname = modgetsymname((uintptr_t)caller(), &offset);
3847c478bd9Sstevel@tonic-gate 		cmn_err(dev_lclose_ce, "dev_lclose: extra close of dev_t 0x%lx "
3857c478bd9Sstevel@tonic-gate 		    "from %s`%s()", dev, mod_containing_pc(caller()),
3867c478bd9Sstevel@tonic-gate 		    funcname ? funcname : "unknown...");
3877c478bd9Sstevel@tonic-gate 		break;
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 	mutex_exit(&cvp->v_lock);
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	/* release the makespecvp vnode. */
3927c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
3937c478bd9Sstevel@tonic-gate 	return (error);
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate /*
3977c478bd9Sstevel@tonic-gate  * Returns -1 or the instance number of the given dev_t as
3987c478bd9Sstevel@tonic-gate  * interpreted by the device driver.  The code may load the driver
3997c478bd9Sstevel@tonic-gate  * but it does not attach any instances.
4007c478bd9Sstevel@tonic-gate  *
4017c478bd9Sstevel@tonic-gate  * Instance is supposed to be a int but drivers have assumed that
4027c478bd9Sstevel@tonic-gate  * the pointer was a pointer to "void *" instead of a pointer to
4037c478bd9Sstevel@tonic-gate  * "int *" so we now explicitly pass a pointer to "void *" and then
4047c478bd9Sstevel@tonic-gate  * cast the result to an int when returning the value.
4057c478bd9Sstevel@tonic-gate  */
4067c478bd9Sstevel@tonic-gate int
dev_to_instance(dev_t dev)4077c478bd9Sstevel@tonic-gate dev_to_instance(dev_t dev)
4087c478bd9Sstevel@tonic-gate {
4097c478bd9Sstevel@tonic-gate 	major_t		major = getmajor(dev);
4107c478bd9Sstevel@tonic-gate 	struct dev_ops	*ops;
4117c478bd9Sstevel@tonic-gate 	void		*vinstance;
4127c478bd9Sstevel@tonic-gate 	int		error;
4137c478bd9Sstevel@tonic-gate 
414c9cc1492SJerry Gilliam 	/* verify that the driver is loaded */
415c9cc1492SJerry Gilliam 	if ((ops = mod_hold_dev_by_major(major)) == NULL)
4167c478bd9Sstevel@tonic-gate 		return (-1);
4177c478bd9Sstevel@tonic-gate 	ASSERT(CB_DRV_INSTALLED(ops));
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	/* verify that it supports the getinfo(9E) entry point */
4207c478bd9Sstevel@tonic-gate 	if (ops->devo_getinfo == NULL) {
4217c478bd9Sstevel@tonic-gate 		mod_rele_dev_by_major(major);
4227c478bd9Sstevel@tonic-gate 		return (-1);
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	/* ask the driver to extract the instance number from the devt */
4267c478bd9Sstevel@tonic-gate 	error = (*ops->devo_getinfo)(NULL, DDI_INFO_DEVT2INSTANCE,
4277c478bd9Sstevel@tonic-gate 	    (void *)dev, &vinstance);
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	/* release the driver */
4307c478bd9Sstevel@tonic-gate 	mod_rele_dev_by_major(major);
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	if (error != DDI_SUCCESS)
4337c478bd9Sstevel@tonic-gate 		return (-1);
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	return ((int)(uintptr_t)vinstance);
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate static void
bdev_strategy_tnf_probe(struct buf * bp)4397c478bd9Sstevel@tonic-gate bdev_strategy_tnf_probe(struct buf *bp)
4407c478bd9Sstevel@tonic-gate {
4417c478bd9Sstevel@tonic-gate 	/* Kernel probe */
4427c478bd9Sstevel@tonic-gate 	TNF_PROBE_5(strategy, "io blockio", /* CSTYLED */,
443184cd04cScth 	    tnf_device, device, bp->b_edev,
444184cd04cScth 	    tnf_diskaddr, block, bp->b_lblkno,
445184cd04cScth 	    tnf_size, size, bp->b_bcount,
446184cd04cScth 	    tnf_opaque, buf, bp,
447184cd04cScth 	    tnf_bioflags, flags, bp->b_flags);
4487c478bd9Sstevel@tonic-gate }
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate int
bdev_strategy(struct buf * bp)4517c478bd9Sstevel@tonic-gate bdev_strategy(struct buf *bp)
4527c478bd9Sstevel@tonic-gate {
4537c478bd9Sstevel@tonic-gate 	struct dev_ops *ops;
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	ops = devopsp[getmajor(bp->b_edev)];
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	/*
4587c478bd9Sstevel@tonic-gate 	 * Before we hit the io:::start probe, we need to fill in the b_dip
4597c478bd9Sstevel@tonic-gate 	 * field of the buf structure.  This should be -- for the most part --
4607c478bd9Sstevel@tonic-gate 	 * incredibly cheap.  If you're in this code looking to bum cycles,
4617c478bd9Sstevel@tonic-gate 	 * there is almost certainly bigger game further down the I/O path...
4627c478bd9Sstevel@tonic-gate 	 */
4637c478bd9Sstevel@tonic-gate 	(void) ops->devo_getinfo(NULL, DDI_INFO_DEVT2DEVINFO,
4647c478bd9Sstevel@tonic-gate 	    (void *)bp->b_edev, (void **)&bp->b_dip);
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	DTRACE_IO1(start, struct buf *, bp);
4677c478bd9Sstevel@tonic-gate 	bp->b_flags |= B_STARTED;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	/*
4707c478bd9Sstevel@tonic-gate 	 * Call the TNF probe here instead of the inline code
4717c478bd9Sstevel@tonic-gate 	 * to force our compiler to use the tail call optimization.
4727c478bd9Sstevel@tonic-gate 	 */
4737c478bd9Sstevel@tonic-gate 	bdev_strategy_tnf_probe(bp);
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	return (ops->devo_cb_ops->cb_strategy(bp));
4767c478bd9Sstevel@tonic-gate }
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate int
bdev_print(dev_t dev,caddr_t str)4797c478bd9Sstevel@tonic-gate bdev_print(dev_t dev, caddr_t str)
4807c478bd9Sstevel@tonic-gate {
4817c478bd9Sstevel@tonic-gate 	struct cb_ops	*cb;
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	cb = devopsp[getmajor(dev)]->devo_cb_ops;
4847c478bd9Sstevel@tonic-gate 	return ((*cb->cb_print)(dev, str));
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate 
487184cd04cScth /*
488184cd04cScth  * Return number of DEV_BSIZE byte blocks.
489184cd04cScth  */
4907c478bd9Sstevel@tonic-gate int
bdev_size(dev_t dev)4917c478bd9Sstevel@tonic-gate bdev_size(dev_t dev)
4927c478bd9Sstevel@tonic-gate {
493184cd04cScth 	uint_t		nblocks;
494184cd04cScth 	uint_t		blksize;
495184cd04cScth 
496184cd04cScth 	if ((nblocks = e_ddi_getprop(dev, VBLK, "nblocks",
497184cd04cScth 	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, -1)) == -1)
498184cd04cScth 		return (-1);
499184cd04cScth 
500184cd04cScth 	/* Get blksize, default to DEV_BSIZE */
501184cd04cScth 	if ((blksize = e_ddi_getprop(dev, VBLK, "blksize",
502184cd04cScth 	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, -1)) == -1)
503184cd04cScth 		blksize = e_ddi_getprop(DDI_DEV_T_ANY, VBLK, "device-blksize",
504184cd04cScth 		    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, DEV_BSIZE);
505184cd04cScth 
506184cd04cScth 	if (blksize >= DEV_BSIZE)
507184cd04cScth 		return (nblocks * (blksize / DEV_BSIZE));
508184cd04cScth 	else
509184cd04cScth 		return (nblocks / (DEV_BSIZE / blksize));
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate /*
5137c478bd9Sstevel@tonic-gate  * Same for 64-bit Nblocks property
5147c478bd9Sstevel@tonic-gate  */
5157c478bd9Sstevel@tonic-gate uint64_t
bdev_Size(dev_t dev)5167c478bd9Sstevel@tonic-gate bdev_Size(dev_t dev)
5177c478bd9Sstevel@tonic-gate {
518184cd04cScth 	uint64_t	nblocks;
519184cd04cScth 	uint_t		blksize;
520184cd04cScth 
521184cd04cScth 	if ((nblocks = e_ddi_getprop_int64(dev, VBLK, "Nblocks",
522184cd04cScth 	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, -1)) == -1)
523184cd04cScth 		return (-1);
524184cd04cScth 
525184cd04cScth 	/* Get blksize, default to DEV_BSIZE */
526184cd04cScth 	if ((blksize = e_ddi_getprop(dev, VBLK, "blksize",
527184cd04cScth 	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, -1)) == -1)
528184cd04cScth 		blksize = e_ddi_getprop(DDI_DEV_T_ANY, VBLK, "device-blksize",
529184cd04cScth 		    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, DEV_BSIZE);
530184cd04cScth 
531184cd04cScth 	if (blksize >= DEV_BSIZE)
532184cd04cScth 		return (nblocks * (blksize / DEV_BSIZE));
533184cd04cScth 	else
534184cd04cScth 		return (nblocks / (DEV_BSIZE / blksize));
5357c478bd9Sstevel@tonic-gate }
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate int
bdev_dump(dev_t dev,caddr_t addr,daddr_t blkno,int blkcnt)5387c478bd9Sstevel@tonic-gate bdev_dump(dev_t dev, caddr_t addr, daddr_t blkno, int blkcnt)
5397c478bd9Sstevel@tonic-gate {
5407c478bd9Sstevel@tonic-gate 	struct cb_ops	*cb;
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	cb = devopsp[getmajor(dev)]->devo_cb_ops;
5437c478bd9Sstevel@tonic-gate 	return ((*cb->cb_dump)(dev, addr, blkno, blkcnt));
5447c478bd9Sstevel@tonic-gate }
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate int
cdev_read(dev_t dev,struct uio * uiop,struct cred * cred)5477c478bd9Sstevel@tonic-gate cdev_read(dev_t dev, struct uio *uiop, struct cred *cred)
5487c478bd9Sstevel@tonic-gate {
5497c478bd9Sstevel@tonic-gate 	struct cb_ops	*cb;
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 	cb = devopsp[getmajor(dev)]->devo_cb_ops;
5527c478bd9Sstevel@tonic-gate 	return ((*cb->cb_read)(dev, uiop, cred));
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate int
cdev_write(dev_t dev,struct uio * uiop,struct cred * cred)5567c478bd9Sstevel@tonic-gate cdev_write(dev_t dev, struct uio *uiop, struct cred *cred)
5577c478bd9Sstevel@tonic-gate {
5587c478bd9Sstevel@tonic-gate 	struct cb_ops	*cb;
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	cb = devopsp[getmajor(dev)]->devo_cb_ops;
5617c478bd9Sstevel@tonic-gate 	return ((*cb->cb_write)(dev, uiop, cred));
5627c478bd9Sstevel@tonic-gate }
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate int
cdev_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,struct cred * cred,int * rvalp)5657c478bd9Sstevel@tonic-gate cdev_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, struct cred *cred,
5667c478bd9Sstevel@tonic-gate     int *rvalp)
5677c478bd9Sstevel@tonic-gate {
5687c478bd9Sstevel@tonic-gate 	struct cb_ops	*cb;
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	cb = devopsp[getmajor(dev)]->devo_cb_ops;
5717c478bd9Sstevel@tonic-gate 	return ((*cb->cb_ioctl)(dev, cmd, arg, mode, cred, rvalp));
5727c478bd9Sstevel@tonic-gate }
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate int
cdev_devmap(dev_t dev,devmap_cookie_t dhp,offset_t off,size_t len,size_t * maplen,uint_t mode)5757c478bd9Sstevel@tonic-gate cdev_devmap(dev_t dev, devmap_cookie_t dhp, offset_t off, size_t len,
576*ade42b55SSebastien Roy     size_t *maplen, uint_t mode)
5777c478bd9Sstevel@tonic-gate {
5787c478bd9Sstevel@tonic-gate 	struct cb_ops	*cb;
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	cb = devopsp[getmajor(dev)]->devo_cb_ops;
5817c478bd9Sstevel@tonic-gate 	return ((*cb->cb_devmap)(dev, dhp, off, len, maplen, mode));
5827c478bd9Sstevel@tonic-gate }
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate int
cdev_mmap(int (* mapfunc)(dev_t,off_t,int),dev_t dev,off_t off,int prot)5857c478bd9Sstevel@tonic-gate cdev_mmap(int (*mapfunc)(dev_t, off_t, int), dev_t dev, off_t off, int prot)
5867c478bd9Sstevel@tonic-gate {
5877c478bd9Sstevel@tonic-gate 	return ((*mapfunc)(dev, off, prot));
5887c478bd9Sstevel@tonic-gate }
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@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)5917c478bd9Sstevel@tonic-gate cdev_segmap(dev_t dev, off_t off, struct as *as, caddr_t *addrp, off_t len,
592*ade42b55SSebastien Roy     uint_t prot, uint_t maxprot, uint_t flags, cred_t *credp)
5937c478bd9Sstevel@tonic-gate {
5947c478bd9Sstevel@tonic-gate 	struct cb_ops	*cb;
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	cb = devopsp[getmajor(dev)]->devo_cb_ops;
5977c478bd9Sstevel@tonic-gate 	return ((*cb->cb_segmap)(dev, off, as, addrp,
5987c478bd9Sstevel@tonic-gate 	    len, prot, maxprot, flags, credp));
5997c478bd9Sstevel@tonic-gate }
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate int
cdev_poll(dev_t dev,short events,int anyyet,short * reventsp,struct pollhead ** pollhdrp)6027c478bd9Sstevel@tonic-gate cdev_poll(dev_t dev, short events, int anyyet, short *reventsp,
603*ade42b55SSebastien Roy     struct pollhead **pollhdrp)
6047c478bd9Sstevel@tonic-gate {
6057c478bd9Sstevel@tonic-gate 	struct cb_ops	*cb;
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	cb = devopsp[getmajor(dev)]->devo_cb_ops;
6087c478bd9Sstevel@tonic-gate 	return ((*cb->cb_chpoll)(dev, events, anyyet, reventsp, pollhdrp));
6097c478bd9Sstevel@tonic-gate }
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate /*
6127c478bd9Sstevel@tonic-gate  * A 'size' property can be provided by a VCHR device.
6137c478bd9Sstevel@tonic-gate  *
6147c478bd9Sstevel@tonic-gate  * Since it's defined as zero for STREAMS devices, so we avoid the
6157c478bd9Sstevel@tonic-gate  * overhead of looking it up.  Note also that we don't force an
6167c478bd9Sstevel@tonic-gate  * unused driver into memory simply to ask about it's size.  We also
6177c478bd9Sstevel@tonic-gate  * don't bother to ask it its size unless it's already been attached
6187c478bd9Sstevel@tonic-gate  * (the attach routine is the earliest place the property will be created)
6197c478bd9Sstevel@tonic-gate  *
6207c478bd9Sstevel@tonic-gate  * XXX	In an ideal world, we'd call this at VOP_GETATTR() time.
6217c478bd9Sstevel@tonic-gate  */
6227c478bd9Sstevel@tonic-gate int
cdev_size(dev_t dev)6237c478bd9Sstevel@tonic-gate cdev_size(dev_t dev)
6247c478bd9Sstevel@tonic-gate {
6257c478bd9Sstevel@tonic-gate 	major_t maj;
6267c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	if ((maj = getmajor(dev)) >= devcnt)
6297c478bd9Sstevel@tonic-gate 		return (0);
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	dnp = &(devnamesp[maj]);
6327c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
6337c478bd9Sstevel@tonic-gate 	if (devopsp[maj] && devopsp[maj]->devo_cb_ops &&
6347c478bd9Sstevel@tonic-gate 	    !devopsp[maj]->devo_cb_ops->cb_str) {
6357c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
6367c478bd9Sstevel@tonic-gate 		return (e_ddi_getprop(dev, VCHR, "size",
6377c478bd9Sstevel@tonic-gate 		    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, 0));
6387c478bd9Sstevel@tonic-gate 	}
6397c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
6407c478bd9Sstevel@tonic-gate 	return (0);
6417c478bd9Sstevel@tonic-gate }
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate /*
6447c478bd9Sstevel@tonic-gate  * same for 64-bit Size property
6457c478bd9Sstevel@tonic-gate  */
6467c478bd9Sstevel@tonic-gate uint64_t
cdev_Size(dev_t dev)6477c478bd9Sstevel@tonic-gate cdev_Size(dev_t dev)
6487c478bd9Sstevel@tonic-gate {
6497c478bd9Sstevel@tonic-gate 	major_t maj;
6507c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	if ((maj = getmajor(dev)) >= devcnt)
6537c478bd9Sstevel@tonic-gate 		return (0);
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	dnp = &(devnamesp[maj]);
6567c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
6577c478bd9Sstevel@tonic-gate 	if (devopsp[maj] && devopsp[maj]->devo_cb_ops &&
6587c478bd9Sstevel@tonic-gate 	    !devopsp[maj]->devo_cb_ops->cb_str) {
6597c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
6607c478bd9Sstevel@tonic-gate 		return (e_ddi_getprop_int64(dev, VCHR, "Size",
6617c478bd9Sstevel@tonic-gate 		    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, 0));
6627c478bd9Sstevel@tonic-gate 	}
6637c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
6647c478bd9Sstevel@tonic-gate 	return (0);
6657c478bd9Sstevel@tonic-gate }
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate /*
6687c478bd9Sstevel@tonic-gate  * XXX	This routine is poorly named, because block devices can and do
6697c478bd9Sstevel@tonic-gate  *	have properties (see bdev_size() above).
6707c478bd9Sstevel@tonic-gate  *
6717c478bd9Sstevel@tonic-gate  * XXX	fix the comment in devops.h that claims that cb_prop_op
6727c478bd9Sstevel@tonic-gate  *	is character-only.
6737c478bd9Sstevel@tonic-gate  */
6747c478bd9Sstevel@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)6757c478bd9Sstevel@tonic-gate cdev_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
6767c478bd9Sstevel@tonic-gate     char *name, caddr_t valuep, int *lengthp)
6777c478bd9Sstevel@tonic-gate {
6787c478bd9Sstevel@tonic-gate 	struct cb_ops	*cb;
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	if ((cb = devopsp[DEVI(dip)->devi_major]->devo_cb_ops) == NULL)
6817c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	return ((*cb->cb_prop_op)(dev, dip, prop_op, mod_flags,
6847c478bd9Sstevel@tonic-gate 	    name, valuep, lengthp));
6857c478bd9Sstevel@tonic-gate }
686