xref: /onnv-gate/usr/src/uts/sun4v/io/mdesc.c (revision 7656:2621e50fdf4a)
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
51991Sheppo  * Common Development and Distribution License (the "License").
61991Sheppo  * 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*7656SSherry.Moore@Sun.COM  * Copyright 2008 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 /*
280Sstevel@tonic-gate  * sun4v machine description driver
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/file.h>
330Sstevel@tonic-gate #include <sys/errno.h>
340Sstevel@tonic-gate #include <sys/open.h>
350Sstevel@tonic-gate #include <sys/cred.h>
360Sstevel@tonic-gate #include <sys/uio.h>
370Sstevel@tonic-gate #include <sys/stat.h>
380Sstevel@tonic-gate #include <sys/ksynch.h>
390Sstevel@tonic-gate #include <sys/modctl.h>
400Sstevel@tonic-gate #include <sys/conf.h>
410Sstevel@tonic-gate #include <sys/devops.h>
420Sstevel@tonic-gate #include <sys/debug.h>
430Sstevel@tonic-gate #include <sys/cmn_err.h>
440Sstevel@tonic-gate #include <sys/ddi.h>
450Sstevel@tonic-gate #include <sys/sunddi.h>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #include <sys/mdesc.h>
480Sstevel@tonic-gate #include <sys/mach_descrip.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #define	MDESC_NAME	"mdesc"
510Sstevel@tonic-gate 
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate  * Operational state flags
540Sstevel@tonic-gate  */
550Sstevel@tonic-gate 
561991Sheppo #define	MDESC_GOT_HANDLE	0x10		/* Got mdesc handle */
571991Sheppo #define	MDESC_BUSY		0x20		/* Device is busy */
580Sstevel@tonic-gate 
591991Sheppo static void		*mdesc_state_head;
601991Sheppo static vmem_t		*mdesc_minor;
611991Sheppo static uint16_t 	mdesc_max_opens = 256;
621991Sheppo static uint16_t		mdesc_opens = 0;
631991Sheppo static int		mdesc_attached = 0;
641991Sheppo static dev_info_t	*mdesc_devi;
651991Sheppo static kmutex_t		mdesc_lock;
660Sstevel@tonic-gate 
670Sstevel@tonic-gate struct mdesc_state {
680Sstevel@tonic-gate 	int		instance;
691991Sheppo 	dev_t		dev;
700Sstevel@tonic-gate 	kmutex_t	lock;
710Sstevel@tonic-gate 	kcondvar_t	cv;
720Sstevel@tonic-gate 	size_t		mdesc_len;
731991Sheppo 	md_t		*mdesc;
740Sstevel@tonic-gate 	int		flags;
750Sstevel@tonic-gate };
760Sstevel@tonic-gate 
771991Sheppo typedef struct mdesc_state mdesc_state_t;
781991Sheppo 
790Sstevel@tonic-gate static int mdesc_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
800Sstevel@tonic-gate static int mdesc_attach(dev_info_t *, ddi_attach_cmd_t);
810Sstevel@tonic-gate static int mdesc_detach(dev_info_t *, ddi_detach_cmd_t);
820Sstevel@tonic-gate static int mdesc_open(dev_t *, int, int, cred_t *);
830Sstevel@tonic-gate static int mdesc_close(dev_t, int, int, cred_t *);
840Sstevel@tonic-gate static int mdesc_read(dev_t, struct uio *, cred_t *);
850Sstevel@tonic-gate static int mdesc_write(dev_t, struct uio *, cred_t *);
860Sstevel@tonic-gate static int mdesc_rw(dev_t, struct uio *, enum uio_rw);
870Sstevel@tonic-gate static int mdesc_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
880Sstevel@tonic-gate 
890Sstevel@tonic-gate static struct cb_ops mdesc_cb_ops = {
900Sstevel@tonic-gate 	mdesc_open,		/* cb_open */
910Sstevel@tonic-gate 	mdesc_close,		/* cb_close */
920Sstevel@tonic-gate 	nodev,			/* cb_strategy */
930Sstevel@tonic-gate 	nodev,			/* cb_print */
940Sstevel@tonic-gate 	nodev,			/* cb_dump */
950Sstevel@tonic-gate 	mdesc_read,		/* cb_read */
960Sstevel@tonic-gate 	nodev,			/* cb_write */
970Sstevel@tonic-gate 	mdesc_ioctl,		/* cb_ioctl */
980Sstevel@tonic-gate 	nodev,			/* cb_devmap */
990Sstevel@tonic-gate 	nodev,			/* cb_mmap */
1000Sstevel@tonic-gate 	nodev,			/* cb_segmap */
1010Sstevel@tonic-gate 	nochpoll,		/* cb_chpoll */
1020Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
1030Sstevel@tonic-gate 	(struct streamtab *)NULL, /* cb_str */
1040Sstevel@tonic-gate 	D_MP | D_64BIT,		/* cb_flag */
1050Sstevel@tonic-gate 	CB_REV,			/* cb_rev */
1060Sstevel@tonic-gate 	nodev,			/* cb_aread */
1070Sstevel@tonic-gate 	nodev			/* cb_awrite */
1080Sstevel@tonic-gate };
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate static struct dev_ops mdesc_dev_ops = {
1110Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
1120Sstevel@tonic-gate 	0,			/* devo_refcnt */
1130Sstevel@tonic-gate 	mdesc_getinfo,		/* devo_getinfo */
1140Sstevel@tonic-gate 	nulldev,		/* devo_identify */
1150Sstevel@tonic-gate 	nulldev,		/* devo_probe */
1160Sstevel@tonic-gate 	mdesc_attach,		/* devo_attach */
1170Sstevel@tonic-gate 	mdesc_detach,		/* devo_detach */
1180Sstevel@tonic-gate 	nodev,			/* devo_reset */
1190Sstevel@tonic-gate 	&mdesc_cb_ops,		/* devo_cb_ops */
1200Sstevel@tonic-gate 	(struct bus_ops *)NULL,	/* devo_bus_ops */
121*7656SSherry.Moore@Sun.COM 	nulldev,		/* devo_power */
122*7656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* quiesce */
1230Sstevel@tonic-gate };
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate static struct modldrv modldrv = {
1260Sstevel@tonic-gate 	&mod_driverops,
127*7656SSherry.Moore@Sun.COM 	"Machine Description Driver",
1280Sstevel@tonic-gate 	&mdesc_dev_ops};
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate static struct modlinkage modlinkage = {
1310Sstevel@tonic-gate 	MODREV_1,
1320Sstevel@tonic-gate 	(void *)&modldrv,
1330Sstevel@tonic-gate 	NULL
1340Sstevel@tonic-gate };
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate int
_init(void)1380Sstevel@tonic-gate _init(void)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate 	int retval;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	if ((retval = ddi_soft_state_init(&mdesc_state_head,
1431991Sheppo 	    sizeof (struct mdesc_state), mdesc_max_opens)) != 0)
1440Sstevel@tonic-gate 		return (retval);
1450Sstevel@tonic-gate 	if ((retval = mod_install(&modlinkage)) != 0) {
1460Sstevel@tonic-gate 		ddi_soft_state_fini(&mdesc_state_head);
1470Sstevel@tonic-gate 		return (retval);
1480Sstevel@tonic-gate 	}
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	return (retval);
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1570Sstevel@tonic-gate _info(struct modinfo *modinfop)
1580Sstevel@tonic-gate {
1590Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate int
_fini(void)1660Sstevel@tonic-gate _fini(void)
1670Sstevel@tonic-gate {
1680Sstevel@tonic-gate 	int retval;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	if ((retval = mod_remove(&modlinkage)) != 0)
1710Sstevel@tonic-gate 		return (retval);
1720Sstevel@tonic-gate 	ddi_soft_state_fini(&mdesc_state_head);
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	return (retval);
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate /*ARGSUSED*/
1810Sstevel@tonic-gate static int
mdesc_getinfo(dev_info_t * dip,ddi_info_cmd_t cmd,void * arg,void ** resultp)1820Sstevel@tonic-gate mdesc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resultp)
1830Sstevel@tonic-gate {
1840Sstevel@tonic-gate 	struct mdesc_state *mdsp;
1850Sstevel@tonic-gate 	int retval = DDI_FAILURE;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	ASSERT(resultp != NULL);
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	switch (cmd) {
1900Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
1911991Sheppo 		mdsp = ddi_get_soft_state(mdesc_state_head,
1921991Sheppo 		    getminor((dev_t)arg));
1931991Sheppo 		if (mdsp != NULL) {
1941991Sheppo 			*resultp = mdesc_devi;
1950Sstevel@tonic-gate 			retval = DDI_SUCCESS;
1960Sstevel@tonic-gate 		} else
1970Sstevel@tonic-gate 			*resultp = NULL;
1980Sstevel@tonic-gate 		break;
1990Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
200911Siskreen 		*resultp = (void *)(uintptr_t)getminor((dev_t)arg);
2010Sstevel@tonic-gate 		retval = DDI_SUCCESS;
2020Sstevel@tonic-gate 		break;
2030Sstevel@tonic-gate 	}
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	return (retval);
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate static int
mdesc_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)2120Sstevel@tonic-gate mdesc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2130Sstevel@tonic-gate {
2140Sstevel@tonic-gate 	int instance = ddi_get_instance(dip);
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	switch (cmd) {
2170Sstevel@tonic-gate 	case DDI_ATTACH:
2181991Sheppo 
2190Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, MDESC_NAME, S_IFCHR, instance,
2200Sstevel@tonic-gate 		    DDI_PSEUDO, 0) != DDI_SUCCESS) {
2210Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s@%d: Unable to create minor node",
2220Sstevel@tonic-gate 			    MDESC_NAME, instance);
2230Sstevel@tonic-gate 			return (DDI_FAILURE);
2240Sstevel@tonic-gate 		}
2250Sstevel@tonic-gate 		ddi_report_dev(dip);
2261991Sheppo 		mdesc_devi = dip;
2271991Sheppo 		mdesc_minor = vmem_create("mdesc_minor", (void *) 1,
2281991Sheppo 		    mdesc_max_opens, 1, NULL, NULL, NULL, 0,
2291991Sheppo 		    VM_SLEEP | VMC_IDENTIFIER);
2301991Sheppo 		mutex_init(&mdesc_lock, NULL, MUTEX_DRIVER, NULL);
2311991Sheppo 		mdesc_attached = 1;
2320Sstevel@tonic-gate 		return (DDI_SUCCESS);
2330Sstevel@tonic-gate 	case DDI_RESUME:
2340Sstevel@tonic-gate 		return (DDI_SUCCESS);
2350Sstevel@tonic-gate 	default:
2360Sstevel@tonic-gate 		return (DDI_FAILURE);
2370Sstevel@tonic-gate 	}
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate 
2401991Sheppo /*ARGSUSED*/
2410Sstevel@tonic-gate static int
mdesc_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)2420Sstevel@tonic-gate mdesc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2430Sstevel@tonic-gate {
2440Sstevel@tonic-gate 	switch (cmd) {
2450Sstevel@tonic-gate 	case DDI_DETACH:
2461991Sheppo 		mutex_destroy(&mdesc_lock);
2471991Sheppo 		vmem_destroy(mdesc_minor);
2481991Sheppo 		ddi_remove_minor_node(mdesc_devi, NULL);
2491991Sheppo 		mdesc_attached = 0;
2500Sstevel@tonic-gate 		return (DDI_SUCCESS);
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	case DDI_SUSPEND:
2530Sstevel@tonic-gate 		return (DDI_SUCCESS);
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	default:
2560Sstevel@tonic-gate 		return (DDI_FAILURE);
2570Sstevel@tonic-gate 	}
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate 
2601991Sheppo static void
mdesc_destroy_state(mdesc_state_t * mdsp)2611991Sheppo mdesc_destroy_state(mdesc_state_t *mdsp)
2621991Sheppo {
2631991Sheppo 	minor_t minor = getminor(mdsp->dev);
2641991Sheppo 
2651991Sheppo 	if (mdsp->flags & MDESC_GOT_HANDLE)
2661991Sheppo 		(void) md_fini_handle(mdsp->mdesc);
2671991Sheppo 
2681991Sheppo 	cv_destroy(&mdsp->cv);
2691991Sheppo 	mutex_destroy(&mdsp->lock);
2701991Sheppo 	ddi_soft_state_free(mdesc_state_head, minor);
2711991Sheppo 	vmem_free(mdesc_minor, (void *)(uintptr_t)minor, 1);
2721991Sheppo }
2731991Sheppo 
2741991Sheppo static mdesc_state_t *
mdesc_create_state(dev_t * devp)2751991Sheppo mdesc_create_state(dev_t *devp)
2761991Sheppo {
2771991Sheppo 	major_t	major;
2781991Sheppo 	minor_t	minor;
2791991Sheppo 	mdesc_state_t *mdsp;
2801991Sheppo 
2811991Sheppo 	minor = (minor_t)(uintptr_t)vmem_alloc(mdesc_minor, 1,
2821991Sheppo 	    VM_BESTFIT | VM_SLEEP);
2831991Sheppo 
2841991Sheppo 	if (ddi_soft_state_zalloc(mdesc_state_head, minor) !=
2851991Sheppo 	    DDI_SUCCESS) {
2861991Sheppo 		cmn_err(CE_WARN, "%s@%d: Unable to allocate state",
2871991Sheppo 		    MDESC_NAME, minor);
2881991Sheppo 		vmem_free(mdesc_minor, (void *)(uintptr_t)minor, 1);
2891991Sheppo 		return (NULL);
2901991Sheppo 	}
2911991Sheppo 
2921991Sheppo 	mdsp = ddi_get_soft_state(mdesc_state_head, minor);
2931991Sheppo 
2941991Sheppo 	if (devp != NULL) {
2951991Sheppo 		major = getemajor(*devp);
2961991Sheppo 	} else {
2971991Sheppo 		major = ddi_driver_major(mdesc_devi);
2981991Sheppo 	}
2991991Sheppo 
3001991Sheppo 	mdsp->dev = makedevice(major, minor);
3011991Sheppo 
3021991Sheppo 	if (devp != NULL)
3031991Sheppo 		*devp = mdsp->dev;
3041991Sheppo 
3051991Sheppo 	mdsp->instance = minor;
3061991Sheppo 
3071991Sheppo 	mutex_init(&mdsp->lock, NULL, MUTEX_DRIVER, NULL);
3081991Sheppo 
3091991Sheppo 	cv_init(&mdsp->cv, NULL, CV_DRIVER, NULL);
3101991Sheppo 
3111991Sheppo 	mdsp->mdesc = md_get_handle();
3121991Sheppo 
3131991Sheppo 	if (mdsp->mdesc == NULL) {
3141991Sheppo 		mdesc_destroy_state(mdsp);
3151991Sheppo 		return (NULL);
3161991Sheppo 	}
3171991Sheppo 	mdsp->flags |= MDESC_GOT_HANDLE;
3181991Sheppo 
3191991Sheppo 	mdsp->mdesc_len = md_get_bin_size(mdsp->mdesc);
3201991Sheppo 
3211991Sheppo 	if (mdsp->mdesc_len == 0) {
3221991Sheppo 		mdesc_destroy_state(mdsp);
3231991Sheppo 		mdsp = NULL;
3241991Sheppo 	}
3251991Sheppo 
3261991Sheppo 	return (mdsp);
3271991Sheppo }
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate /*ARGSUSED*/
3310Sstevel@tonic-gate static int
mdesc_open(dev_t * devp,int flag,int otyp,cred_t * credp)3320Sstevel@tonic-gate mdesc_open(dev_t *devp, int flag, int otyp, cred_t *credp)
3330Sstevel@tonic-gate {
3340Sstevel@tonic-gate 	struct mdesc_state *mdsp;
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
3370Sstevel@tonic-gate 		return (EINVAL);
3381991Sheppo 	if (!mdesc_attached)
3391991Sheppo 		return (ENXIO);
3401991Sheppo 
3411991Sheppo 	mutex_enter(&mdesc_lock);
3421991Sheppo 
3431991Sheppo 	if (mdesc_opens >= mdesc_max_opens) {
3441991Sheppo 		mutex_exit(&mdesc_lock);
3451991Sheppo 		return (ENXIO);
3461991Sheppo 	}
3471991Sheppo 
3481991Sheppo 	mdsp = mdesc_create_state(devp);
3491991Sheppo 
3501991Sheppo 	if (mdsp == NULL) {
3511991Sheppo 		mutex_exit(&mdesc_lock);
3521991Sheppo 		return (ENXIO);
3531991Sheppo 	}
3541991Sheppo 
3551991Sheppo 	mdesc_opens++;
3561991Sheppo 
3571991Sheppo 	mutex_exit(&mdesc_lock);
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	return (0);
3600Sstevel@tonic-gate }
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate /*ARGSUSED*/
3630Sstevel@tonic-gate static int
mdesc_close(dev_t dev,int flag,int otyp,cred_t * credp)3640Sstevel@tonic-gate mdesc_close(dev_t dev, int flag, int otyp, cred_t *credp)
3650Sstevel@tonic-gate {
3660Sstevel@tonic-gate 	struct mdesc_state *mdsp;
3670Sstevel@tonic-gate 	int instance = getminor(dev);
3680Sstevel@tonic-gate 
3691991Sheppo 	if (otyp != OTYP_CHR)
3701991Sheppo 		return (EINVAL);
3711991Sheppo 
3721991Sheppo 	mutex_enter(&mdesc_lock);
3731991Sheppo 	if (mdesc_opens == 0) {
3741991Sheppo 		mutex_exit(&mdesc_lock);
3751991Sheppo 		return (0);
3761991Sheppo 	}
3771991Sheppo 	mutex_exit(&mdesc_lock);
3781991Sheppo 
3790Sstevel@tonic-gate 	if ((mdsp = ddi_get_soft_state(mdesc_state_head, instance)) == NULL)
3800Sstevel@tonic-gate 		return (ENXIO);
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	ASSERT(mdsp->instance == instance);
3830Sstevel@tonic-gate 
3841991Sheppo 	mdesc_destroy_state(mdsp);
3851991Sheppo 	mutex_enter(&mdesc_lock);
3861991Sheppo 	mdesc_opens--;
3871991Sheppo 	mutex_exit(&mdesc_lock);
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	return (0);
3900Sstevel@tonic-gate }
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate /*ARGSUSED*/
3960Sstevel@tonic-gate static int
mdesc_read(dev_t dev,struct uio * uiop,cred_t * credp)3970Sstevel@tonic-gate mdesc_read(dev_t dev, struct uio *uiop, cred_t *credp)
3980Sstevel@tonic-gate {
3990Sstevel@tonic-gate 	return (mdesc_rw(dev, uiop, UIO_READ));
4000Sstevel@tonic-gate }
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate /*ARGSUSED*/
4060Sstevel@tonic-gate static int
mdesc_write(dev_t dev,struct uio * uiop,cred_t * credp)4070Sstevel@tonic-gate mdesc_write(dev_t dev, struct uio *uiop, cred_t *credp)
4080Sstevel@tonic-gate {
4090Sstevel@tonic-gate 	return (ENXIO);	/* This driver version does not allow updates */
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate static int
mdesc_rw(dev_t dev,struct uio * uiop,enum uio_rw rw)4160Sstevel@tonic-gate mdesc_rw(dev_t dev, struct uio *uiop, enum uio_rw rw)
4170Sstevel@tonic-gate {
4180Sstevel@tonic-gate 	struct mdesc_state *mdsp;
4190Sstevel@tonic-gate 	int instance = getminor(dev);
4200Sstevel@tonic-gate 	size_t len;
4210Sstevel@tonic-gate 	int retval;
4221991Sheppo 	caddr_t buf;
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	len = uiop->uio_resid;
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	if ((mdsp = ddi_get_soft_state(mdesc_state_head, instance)) == NULL)
4270Sstevel@tonic-gate 		return (ENXIO);
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	ASSERT(mdsp->instance == instance);
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	if (len == 0)
4320Sstevel@tonic-gate 		return (0);
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	mutex_enter(&mdsp->lock);
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	while (mdsp->flags & MDESC_BUSY) {
4370Sstevel@tonic-gate 		if (cv_wait_sig(&mdsp->cv, &mdsp->lock) == 0) {
4380Sstevel@tonic-gate 			mutex_exit(&mdsp->lock);
4390Sstevel@tonic-gate 			return (EINTR);
4400Sstevel@tonic-gate 		}
4410Sstevel@tonic-gate 	}
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	if (uiop->uio_offset < 0 || uiop->uio_offset > mdsp->mdesc_len) {
4440Sstevel@tonic-gate 		mutex_exit(&mdsp->lock);
4450Sstevel@tonic-gate 		return (EINVAL);
4460Sstevel@tonic-gate 	}
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	if (len > (mdsp->mdesc_len - uiop->uio_offset))
4490Sstevel@tonic-gate 		len = mdsp->mdesc_len - uiop->uio_offset;
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 		/* already checked that offset<mdesc_len above */
4520Sstevel@tonic-gate 	if (len == 0) {
4530Sstevel@tonic-gate 		mutex_exit(&mdsp->lock);
4540Sstevel@tonic-gate 		return (rw == UIO_WRITE ? ENOSPC : 0);
4550Sstevel@tonic-gate 	}
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	mdsp->flags |= MDESC_BUSY;
4580Sstevel@tonic-gate 	mutex_exit(&mdsp->lock);
4590Sstevel@tonic-gate 
4601991Sheppo 	buf = md_get_md_raw(mdsp->mdesc);
4611991Sheppo 	if (buf == NULL)
4621991Sheppo 		return (ENXIO);
4631991Sheppo 
4641991Sheppo 	retval = uiomove((void *)(buf + uiop->uio_offset),
465*7656SSherry.Moore@Sun.COM 	    len, rw, uiop);
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	mutex_enter(&mdsp->lock);
4680Sstevel@tonic-gate 	mdsp->flags &= ~MDESC_BUSY;
4690Sstevel@tonic-gate 	cv_broadcast(&mdsp->cv);
4700Sstevel@tonic-gate 	mutex_exit(&mdsp->lock);
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	return (retval);
4730Sstevel@tonic-gate }
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate /*ARGSUSED*/
4790Sstevel@tonic-gate static int
mdesc_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)4800Sstevel@tonic-gate mdesc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
4810Sstevel@tonic-gate     int *rvalp)
4820Sstevel@tonic-gate {
4830Sstevel@tonic-gate 	struct mdesc_state *mdsp;
4840Sstevel@tonic-gate 	int instance = getminor(dev);
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	if ((mdsp = ddi_get_soft_state(mdesc_state_head, instance)) == NULL)
4870Sstevel@tonic-gate 		return (ENXIO);
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	ASSERT(mdsp->instance == instance);
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	switch (cmd) {
4920Sstevel@tonic-gate 	case MDESCIOCGSZ: {
4930Sstevel@tonic-gate 		/*
4940Sstevel@tonic-gate 		 * We are not guaranteed that ddi_copyout(9F) will read
4950Sstevel@tonic-gate 		 * atomically anything larger than a byte.  Therefore we
4960Sstevel@tonic-gate 		 * must duplicate the size before copying it out to the user.
4970Sstevel@tonic-gate 		 */
4980Sstevel@tonic-gate 		size_t sz = mdsp->mdesc_len;
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 		if (!(mode & FREAD))
5010Sstevel@tonic-gate 			return (EACCES);
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
5040Sstevel@tonic-gate 		switch (ddi_model_convert_from(mode & FMODELS)) {
5050Sstevel@tonic-gate 		case DDI_MODEL_ILP32: {
5060Sstevel@tonic-gate 			size32_t sz32 = (size32_t)sz;
5070Sstevel@tonic-gate 			if (ddi_copyout(&sz32, (void *)arg, sizeof (size32_t),
5080Sstevel@tonic-gate 			    mode) != 0)
5090Sstevel@tonic-gate 				return (EFAULT);
5100Sstevel@tonic-gate 			return (0);
5110Sstevel@tonic-gate 		}
5120Sstevel@tonic-gate 		case DDI_MODEL_NONE:
5130Sstevel@tonic-gate 			if (ddi_copyout(&sz, (void *)arg, sizeof (size_t),
5140Sstevel@tonic-gate 			    mode) != 0)
5150Sstevel@tonic-gate 				return (EFAULT);
5160Sstevel@tonic-gate 			return (0);
5170Sstevel@tonic-gate 		default:
5180Sstevel@tonic-gate 			cmn_err(CE_WARN,
5190Sstevel@tonic-gate 			    "mdesc: Invalid data model %d in ioctl\n",
5200Sstevel@tonic-gate 			    ddi_model_convert_from(mode & FMODELS));
5210Sstevel@tonic-gate 			return (ENOTSUP);
5220Sstevel@tonic-gate 		}
5230Sstevel@tonic-gate #else /* ! _MULTI_DATAMODEL */
5240Sstevel@tonic-gate 		if (ddi_copyout(&sz, (void *)arg, sizeof (size_t), mode) != 0)
5250Sstevel@tonic-gate 			return (EFAULT);
5260Sstevel@tonic-gate 		return (0);
5270Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
5280Sstevel@tonic-gate 	}
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	default:
5310Sstevel@tonic-gate 		return (ENOTTY);
5320Sstevel@tonic-gate 	}
5330Sstevel@tonic-gate }
534