xref: /titanic_52/usr/src/uts/sun4v/io/mdesc.c (revision 1ae0874509b6811fdde1dfd46f0d93fd09867a3f)
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
5*1ae08745Sheppo  * Common Development and Distribution License (the "License").
6*1ae08745Sheppo  * 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 /*
22*1ae08745Sheppo  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * sun4v machine description driver
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/file.h>
347c478bd9Sstevel@tonic-gate #include <sys/errno.h>
357c478bd9Sstevel@tonic-gate #include <sys/open.h>
367c478bd9Sstevel@tonic-gate #include <sys/cred.h>
377c478bd9Sstevel@tonic-gate #include <sys/uio.h>
387c478bd9Sstevel@tonic-gate #include <sys/stat.h>
397c478bd9Sstevel@tonic-gate #include <sys/ksynch.h>
407c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
417c478bd9Sstevel@tonic-gate #include <sys/conf.h>
427c478bd9Sstevel@tonic-gate #include <sys/devops.h>
437c478bd9Sstevel@tonic-gate #include <sys/debug.h>
447c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
457c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
467c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include <sys/mdesc.h>
497c478bd9Sstevel@tonic-gate #include <sys/mach_descrip.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #define	MDESC_NAME	"mdesc"
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * Operational state flags
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate 
57*1ae08745Sheppo #define	MDESC_GOT_HANDLE	0x10		/* Got mdesc handle */
587c478bd9Sstevel@tonic-gate #define	MDESC_BUSY		0x20		/* Device is busy */
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate static void		*mdesc_state_head;
61*1ae08745Sheppo static vmem_t		*mdesc_minor;
62*1ae08745Sheppo static uint16_t 	mdesc_max_opens = 256;
63*1ae08745Sheppo static uint16_t		mdesc_opens = 0;
64*1ae08745Sheppo static int		mdesc_attached = 0;
65*1ae08745Sheppo static dev_info_t	*mdesc_devi;
66*1ae08745Sheppo static kmutex_t		mdesc_lock;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate struct mdesc_state {
697c478bd9Sstevel@tonic-gate 	int		instance;
70*1ae08745Sheppo 	dev_t		dev;
717c478bd9Sstevel@tonic-gate 	kmutex_t	lock;
727c478bd9Sstevel@tonic-gate 	kcondvar_t	cv;
737c478bd9Sstevel@tonic-gate 	size_t		mdesc_len;
74*1ae08745Sheppo 	md_t		*mdesc;
757c478bd9Sstevel@tonic-gate 	int		flags;
767c478bd9Sstevel@tonic-gate };
777c478bd9Sstevel@tonic-gate 
78*1ae08745Sheppo typedef struct mdesc_state mdesc_state_t;
79*1ae08745Sheppo 
807c478bd9Sstevel@tonic-gate static int mdesc_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
817c478bd9Sstevel@tonic-gate static int mdesc_attach(dev_info_t *, ddi_attach_cmd_t);
827c478bd9Sstevel@tonic-gate static int mdesc_detach(dev_info_t *, ddi_detach_cmd_t);
837c478bd9Sstevel@tonic-gate static int mdesc_open(dev_t *, int, int, cred_t *);
847c478bd9Sstevel@tonic-gate static int mdesc_close(dev_t, int, int, cred_t *);
857c478bd9Sstevel@tonic-gate static int mdesc_read(dev_t, struct uio *, cred_t *);
867c478bd9Sstevel@tonic-gate static int mdesc_write(dev_t, struct uio *, cred_t *);
877c478bd9Sstevel@tonic-gate static int mdesc_rw(dev_t, struct uio *, enum uio_rw);
887c478bd9Sstevel@tonic-gate static int mdesc_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate static struct cb_ops mdesc_cb_ops = {
917c478bd9Sstevel@tonic-gate 	mdesc_open,		/* cb_open */
927c478bd9Sstevel@tonic-gate 	mdesc_close,		/* cb_close */
937c478bd9Sstevel@tonic-gate 	nodev,			/* cb_strategy */
947c478bd9Sstevel@tonic-gate 	nodev,			/* cb_print */
957c478bd9Sstevel@tonic-gate 	nodev,			/* cb_dump */
967c478bd9Sstevel@tonic-gate 	mdesc_read,		/* cb_read */
977c478bd9Sstevel@tonic-gate 	nodev,			/* cb_write */
987c478bd9Sstevel@tonic-gate 	mdesc_ioctl,		/* cb_ioctl */
997c478bd9Sstevel@tonic-gate 	nodev,			/* cb_devmap */
1007c478bd9Sstevel@tonic-gate 	nodev,			/* cb_mmap */
1017c478bd9Sstevel@tonic-gate 	nodev,			/* cb_segmap */
1027c478bd9Sstevel@tonic-gate 	nochpoll,		/* cb_chpoll */
1037c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
1047c478bd9Sstevel@tonic-gate 	(struct streamtab *)NULL, /* cb_str */
1057c478bd9Sstevel@tonic-gate 	D_MP | D_64BIT,		/* cb_flag */
1067c478bd9Sstevel@tonic-gate 	CB_REV,			/* cb_rev */
1077c478bd9Sstevel@tonic-gate 	nodev,			/* cb_aread */
1087c478bd9Sstevel@tonic-gate 	nodev			/* cb_awrite */
1097c478bd9Sstevel@tonic-gate };
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate static struct dev_ops mdesc_dev_ops = {
1127c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
1137c478bd9Sstevel@tonic-gate 	0,			/* devo_refcnt */
1147c478bd9Sstevel@tonic-gate 	mdesc_getinfo,		/* devo_getinfo */
1157c478bd9Sstevel@tonic-gate 	nulldev,		/* devo_identify */
1167c478bd9Sstevel@tonic-gate 	nulldev,		/* devo_probe */
1177c478bd9Sstevel@tonic-gate 	mdesc_attach,		/* devo_attach */
1187c478bd9Sstevel@tonic-gate 	mdesc_detach,		/* devo_detach */
1197c478bd9Sstevel@tonic-gate 	nodev,			/* devo_reset */
1207c478bd9Sstevel@tonic-gate 	&mdesc_cb_ops,		/* devo_cb_ops */
1217c478bd9Sstevel@tonic-gate 	(struct bus_ops *)NULL,	/* devo_bus_ops */
1227c478bd9Sstevel@tonic-gate 	nulldev			/* devo_power */
1237c478bd9Sstevel@tonic-gate };
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
1267c478bd9Sstevel@tonic-gate 	&mod_driverops,
1277c478bd9Sstevel@tonic-gate 	"Machine Description Driver 1.0",
1287c478bd9Sstevel@tonic-gate 	&mdesc_dev_ops};
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1317c478bd9Sstevel@tonic-gate 	MODREV_1,
1327c478bd9Sstevel@tonic-gate 	(void *)&modldrv,
1337c478bd9Sstevel@tonic-gate 	NULL
1347c478bd9Sstevel@tonic-gate };
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate int
1387c478bd9Sstevel@tonic-gate _init(void)
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate 	int retval;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	if ((retval = ddi_soft_state_init(&mdesc_state_head,
143*1ae08745Sheppo 	    sizeof (struct mdesc_state), mdesc_max_opens)) != 0)
1447c478bd9Sstevel@tonic-gate 		return (retval);
1457c478bd9Sstevel@tonic-gate 	if ((retval = mod_install(&modlinkage)) != 0) {
1467c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&mdesc_state_head);
1477c478bd9Sstevel@tonic-gate 		return (retval);
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	return (retval);
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate int
1577c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate int
1667c478bd9Sstevel@tonic-gate _fini(void)
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate 	int retval;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	if ((retval = mod_remove(&modlinkage)) != 0)
1717c478bd9Sstevel@tonic-gate 		return (retval);
1727c478bd9Sstevel@tonic-gate 	ddi_soft_state_fini(&mdesc_state_head);
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	return (retval);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1817c478bd9Sstevel@tonic-gate static int
1827c478bd9Sstevel@tonic-gate mdesc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resultp)
1837c478bd9Sstevel@tonic-gate {
1847c478bd9Sstevel@tonic-gate 	struct mdesc_state *mdsp;
1857c478bd9Sstevel@tonic-gate 	int retval = DDI_FAILURE;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	ASSERT(resultp != NULL);
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	switch (cmd) {
1907c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
191*1ae08745Sheppo 		mdsp = ddi_get_soft_state(mdesc_state_head,
192*1ae08745Sheppo 		    getminor((dev_t)arg));
193*1ae08745Sheppo 		if (mdsp != NULL) {
194*1ae08745Sheppo 			*resultp = mdesc_devi;
1957c478bd9Sstevel@tonic-gate 			retval = DDI_SUCCESS;
1967c478bd9Sstevel@tonic-gate 		} else
1977c478bd9Sstevel@tonic-gate 			*resultp = NULL;
1987c478bd9Sstevel@tonic-gate 		break;
1997c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
2000bd5614cSiskreen 		*resultp = (void *)(uintptr_t)getminor((dev_t)arg);
2017c478bd9Sstevel@tonic-gate 		retval = DDI_SUCCESS;
2027c478bd9Sstevel@tonic-gate 		break;
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	return (retval);
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate static int
2127c478bd9Sstevel@tonic-gate mdesc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate 	int instance = ddi_get_instance(dip);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	switch (cmd) {
2177c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
218*1ae08745Sheppo 
2197c478bd9Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, MDESC_NAME, S_IFCHR, instance,
2207c478bd9Sstevel@tonic-gate 		    DDI_PSEUDO, 0) != DDI_SUCCESS) {
2217c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s@%d: Unable to create minor node",
2227c478bd9Sstevel@tonic-gate 			    MDESC_NAME, instance);
2237c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
2247c478bd9Sstevel@tonic-gate 		}
2257c478bd9Sstevel@tonic-gate 		ddi_report_dev(dip);
226*1ae08745Sheppo 		mdesc_devi = dip;
227*1ae08745Sheppo 		mdesc_minor = vmem_create("mdesc_minor", (void *) 1,
228*1ae08745Sheppo 		    mdesc_max_opens, 1, NULL, NULL, NULL, 0,
229*1ae08745Sheppo 		    VM_SLEEP | VMC_IDENTIFIER);
230*1ae08745Sheppo 		mutex_init(&mdesc_lock, NULL, MUTEX_DRIVER, NULL);
231*1ae08745Sheppo 		mdesc_attached = 1;
2327c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
2337c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
2347c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
2357c478bd9Sstevel@tonic-gate 	default:
2367c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate 
240*1ae08745Sheppo /*ARGSUSED*/
2417c478bd9Sstevel@tonic-gate static int
2427c478bd9Sstevel@tonic-gate mdesc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate 	switch (cmd) {
2457c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
246*1ae08745Sheppo 		mutex_destroy(&mdesc_lock);
247*1ae08745Sheppo 		vmem_destroy(mdesc_minor);
248*1ae08745Sheppo 		ddi_remove_minor_node(mdesc_devi, NULL);
249*1ae08745Sheppo 		mdesc_attached = 0;
2507c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
2537c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	default:
2567c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
260*1ae08745Sheppo static void
261*1ae08745Sheppo mdesc_destroy_state(mdesc_state_t *mdsp)
262*1ae08745Sheppo {
263*1ae08745Sheppo 	minor_t minor = getminor(mdsp->dev);
264*1ae08745Sheppo 
265*1ae08745Sheppo 	if (mdsp->flags & MDESC_GOT_HANDLE)
266*1ae08745Sheppo 		(void) md_fini_handle(mdsp->mdesc);
267*1ae08745Sheppo 
268*1ae08745Sheppo 	cv_destroy(&mdsp->cv);
269*1ae08745Sheppo 	mutex_destroy(&mdsp->lock);
270*1ae08745Sheppo 	ddi_soft_state_free(mdesc_state_head, minor);
271*1ae08745Sheppo 	vmem_free(mdesc_minor, (void *)(uintptr_t)minor, 1);
272*1ae08745Sheppo }
273*1ae08745Sheppo 
274*1ae08745Sheppo static mdesc_state_t *
275*1ae08745Sheppo mdesc_create_state(dev_t *devp)
276*1ae08745Sheppo {
277*1ae08745Sheppo 	major_t	major;
278*1ae08745Sheppo 	minor_t	minor;
279*1ae08745Sheppo 	mdesc_state_t *mdsp;
280*1ae08745Sheppo 
281*1ae08745Sheppo 	minor = (minor_t)(uintptr_t)vmem_alloc(mdesc_minor, 1,
282*1ae08745Sheppo 	    VM_BESTFIT | VM_SLEEP);
283*1ae08745Sheppo 
284*1ae08745Sheppo 	if (ddi_soft_state_zalloc(mdesc_state_head, minor) !=
285*1ae08745Sheppo 	    DDI_SUCCESS) {
286*1ae08745Sheppo 		cmn_err(CE_WARN, "%s@%d: Unable to allocate state",
287*1ae08745Sheppo 		    MDESC_NAME, minor);
288*1ae08745Sheppo 		vmem_free(mdesc_minor, (void *)(uintptr_t)minor, 1);
289*1ae08745Sheppo 		return (NULL);
290*1ae08745Sheppo 	}
291*1ae08745Sheppo 
292*1ae08745Sheppo 	mdsp = ddi_get_soft_state(mdesc_state_head, minor);
293*1ae08745Sheppo 
294*1ae08745Sheppo 	if (devp != NULL) {
295*1ae08745Sheppo 		major = getemajor(*devp);
296*1ae08745Sheppo 	} else {
297*1ae08745Sheppo 		major = ddi_driver_major(mdesc_devi);
298*1ae08745Sheppo 	}
299*1ae08745Sheppo 
300*1ae08745Sheppo 	mdsp->dev = makedevice(major, minor);
301*1ae08745Sheppo 
302*1ae08745Sheppo 	if (devp != NULL)
303*1ae08745Sheppo 		*devp = mdsp->dev;
304*1ae08745Sheppo 
305*1ae08745Sheppo 	mdsp->instance = minor;
306*1ae08745Sheppo 
307*1ae08745Sheppo 	mutex_init(&mdsp->lock, NULL, MUTEX_DRIVER, NULL);
308*1ae08745Sheppo 
309*1ae08745Sheppo 	cv_init(&mdsp->cv, NULL, CV_DRIVER, NULL);
310*1ae08745Sheppo 
311*1ae08745Sheppo 	mdsp->mdesc = md_get_handle();
312*1ae08745Sheppo 
313*1ae08745Sheppo 	if (mdsp->mdesc == NULL) {
314*1ae08745Sheppo 		mdesc_destroy_state(mdsp);
315*1ae08745Sheppo 		return (NULL);
316*1ae08745Sheppo 	}
317*1ae08745Sheppo 	mdsp->flags |= MDESC_GOT_HANDLE;
318*1ae08745Sheppo 
319*1ae08745Sheppo 	mdsp->mdesc_len = md_get_bin_size(mdsp->mdesc);
320*1ae08745Sheppo 
321*1ae08745Sheppo 	if (mdsp->mdesc_len == 0) {
322*1ae08745Sheppo 		mdesc_destroy_state(mdsp);
323*1ae08745Sheppo 		mdsp = NULL;
324*1ae08745Sheppo 	}
325*1ae08745Sheppo 
326*1ae08745Sheppo 	return (mdsp);
327*1ae08745Sheppo }
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3317c478bd9Sstevel@tonic-gate static int
3327c478bd9Sstevel@tonic-gate mdesc_open(dev_t *devp, int flag, int otyp, cred_t *credp)
3337c478bd9Sstevel@tonic-gate {
3347c478bd9Sstevel@tonic-gate 	struct mdesc_state *mdsp;
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
3377c478bd9Sstevel@tonic-gate 		return (EINVAL);
338*1ae08745Sheppo 	if (!mdesc_attached)
339*1ae08745Sheppo 		return (ENXIO);
340*1ae08745Sheppo 
341*1ae08745Sheppo 	mutex_enter(&mdesc_lock);
342*1ae08745Sheppo 
343*1ae08745Sheppo 	if (mdesc_opens >= mdesc_max_opens) {
344*1ae08745Sheppo 		mutex_exit(&mdesc_lock);
345*1ae08745Sheppo 		return (ENXIO);
346*1ae08745Sheppo 	}
347*1ae08745Sheppo 
348*1ae08745Sheppo 	mdsp = mdesc_create_state(devp);
349*1ae08745Sheppo 
350*1ae08745Sheppo 	if (mdsp == NULL) {
351*1ae08745Sheppo 		mutex_exit(&mdesc_lock);
352*1ae08745Sheppo 		return (ENXIO);
353*1ae08745Sheppo 	}
354*1ae08745Sheppo 
355*1ae08745Sheppo 	mdesc_opens++;
356*1ae08745Sheppo 
357*1ae08745Sheppo 	mutex_exit(&mdesc_lock);
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	return (0);
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3637c478bd9Sstevel@tonic-gate static int
3647c478bd9Sstevel@tonic-gate mdesc_close(dev_t dev, int flag, int otyp, cred_t *credp)
3657c478bd9Sstevel@tonic-gate {
3667c478bd9Sstevel@tonic-gate 	struct mdesc_state *mdsp;
3677c478bd9Sstevel@tonic-gate 	int instance = getminor(dev);
3687c478bd9Sstevel@tonic-gate 
369*1ae08745Sheppo 	if (otyp != OTYP_CHR)
370*1ae08745Sheppo 		return (EINVAL);
371*1ae08745Sheppo 
372*1ae08745Sheppo 	mutex_enter(&mdesc_lock);
373*1ae08745Sheppo 	if (mdesc_opens == 0) {
374*1ae08745Sheppo 		mutex_exit(&mdesc_lock);
375*1ae08745Sheppo 		return (0);
376*1ae08745Sheppo 	}
377*1ae08745Sheppo 	mutex_exit(&mdesc_lock);
378*1ae08745Sheppo 
3797c478bd9Sstevel@tonic-gate 	if ((mdsp = ddi_get_soft_state(mdesc_state_head, instance)) == NULL)
3807c478bd9Sstevel@tonic-gate 		return (ENXIO);
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	ASSERT(mdsp->instance == instance);
3837c478bd9Sstevel@tonic-gate 
384*1ae08745Sheppo 	mdesc_destroy_state(mdsp);
385*1ae08745Sheppo 	mutex_enter(&mdesc_lock);
386*1ae08745Sheppo 	mdesc_opens--;
387*1ae08745Sheppo 	mutex_exit(&mdesc_lock);
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	return (0);
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3967c478bd9Sstevel@tonic-gate static int
3977c478bd9Sstevel@tonic-gate mdesc_read(dev_t dev, struct uio *uiop, cred_t *credp)
3987c478bd9Sstevel@tonic-gate {
3997c478bd9Sstevel@tonic-gate 	return (mdesc_rw(dev, uiop, UIO_READ));
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4067c478bd9Sstevel@tonic-gate static int
4077c478bd9Sstevel@tonic-gate mdesc_write(dev_t dev, struct uio *uiop, cred_t *credp)
4087c478bd9Sstevel@tonic-gate {
4097c478bd9Sstevel@tonic-gate 	return (ENXIO);	/* This driver version does not allow updates */
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate static int
4167c478bd9Sstevel@tonic-gate mdesc_rw(dev_t dev, struct uio *uiop, enum uio_rw rw)
4177c478bd9Sstevel@tonic-gate {
4187c478bd9Sstevel@tonic-gate 	struct mdesc_state *mdsp;
4197c478bd9Sstevel@tonic-gate 	int instance = getminor(dev);
4207c478bd9Sstevel@tonic-gate 	size_t len;
4217c478bd9Sstevel@tonic-gate 	int retval;
422*1ae08745Sheppo 	caddr_t buf;
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	len = uiop->uio_resid;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	if ((mdsp = ddi_get_soft_state(mdesc_state_head, instance)) == NULL)
4277c478bd9Sstevel@tonic-gate 		return (ENXIO);
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	ASSERT(mdsp->instance == instance);
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	if (len == 0)
4327c478bd9Sstevel@tonic-gate 		return (0);
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	mutex_enter(&mdsp->lock);
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	while (mdsp->flags & MDESC_BUSY) {
4377c478bd9Sstevel@tonic-gate 		if (cv_wait_sig(&mdsp->cv, &mdsp->lock) == 0) {
4387c478bd9Sstevel@tonic-gate 			mutex_exit(&mdsp->lock);
4397c478bd9Sstevel@tonic-gate 			return (EINTR);
4407c478bd9Sstevel@tonic-gate 		}
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	if (uiop->uio_offset < 0 || uiop->uio_offset > mdsp->mdesc_len) {
4447c478bd9Sstevel@tonic-gate 		mutex_exit(&mdsp->lock);
4457c478bd9Sstevel@tonic-gate 		return (EINVAL);
4467c478bd9Sstevel@tonic-gate 	}
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	if (len > (mdsp->mdesc_len - uiop->uio_offset))
4497c478bd9Sstevel@tonic-gate 		len = mdsp->mdesc_len - uiop->uio_offset;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 		/* already checked that offset<mdesc_len above */
4527c478bd9Sstevel@tonic-gate 	if (len == 0) {
4537c478bd9Sstevel@tonic-gate 		mutex_exit(&mdsp->lock);
4547c478bd9Sstevel@tonic-gate 		return (rw == UIO_WRITE ? ENOSPC : 0);
4557c478bd9Sstevel@tonic-gate 	}
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	mdsp->flags |= MDESC_BUSY;
4587c478bd9Sstevel@tonic-gate 	mutex_exit(&mdsp->lock);
4597c478bd9Sstevel@tonic-gate 
460*1ae08745Sheppo 	buf = md_get_md_raw(mdsp->mdesc);
461*1ae08745Sheppo 	if (buf == NULL)
462*1ae08745Sheppo 		return (ENXIO);
463*1ae08745Sheppo 
464*1ae08745Sheppo 	retval = uiomove((void *)(buf + uiop->uio_offset),
4657c478bd9Sstevel@tonic-gate 		len, rw, uiop);
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	mutex_enter(&mdsp->lock);
4687c478bd9Sstevel@tonic-gate 	mdsp->flags &= ~MDESC_BUSY;
4697c478bd9Sstevel@tonic-gate 	cv_broadcast(&mdsp->cv);
4707c478bd9Sstevel@tonic-gate 	mutex_exit(&mdsp->lock);
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	return (retval);
4737c478bd9Sstevel@tonic-gate }
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4797c478bd9Sstevel@tonic-gate static int
4807c478bd9Sstevel@tonic-gate mdesc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
4817c478bd9Sstevel@tonic-gate     int *rvalp)
4827c478bd9Sstevel@tonic-gate {
4837c478bd9Sstevel@tonic-gate 	struct mdesc_state *mdsp;
4847c478bd9Sstevel@tonic-gate 	int instance = getminor(dev);
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	if ((mdsp = ddi_get_soft_state(mdesc_state_head, instance)) == NULL)
4877c478bd9Sstevel@tonic-gate 		return (ENXIO);
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	ASSERT(mdsp->instance == instance);
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	switch (cmd) {
4927c478bd9Sstevel@tonic-gate 	case MDESCIOCGSZ: {
4937c478bd9Sstevel@tonic-gate 		/*
4947c478bd9Sstevel@tonic-gate 		 * We are not guaranteed that ddi_copyout(9F) will read
4957c478bd9Sstevel@tonic-gate 		 * atomically anything larger than a byte.  Therefore we
4967c478bd9Sstevel@tonic-gate 		 * must duplicate the size before copying it out to the user.
4977c478bd9Sstevel@tonic-gate 		 */
4987c478bd9Sstevel@tonic-gate 		size_t sz = mdsp->mdesc_len;
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 		if (!(mode & FREAD))
5017c478bd9Sstevel@tonic-gate 			return (EACCES);
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
5047c478bd9Sstevel@tonic-gate 		switch (ddi_model_convert_from(mode & FMODELS)) {
5057c478bd9Sstevel@tonic-gate 		case DDI_MODEL_ILP32: {
5067c478bd9Sstevel@tonic-gate 			size32_t sz32 = (size32_t)sz;
5077c478bd9Sstevel@tonic-gate 			if (ddi_copyout(&sz32, (void *)arg, sizeof (size32_t),
5087c478bd9Sstevel@tonic-gate 			    mode) != 0)
5097c478bd9Sstevel@tonic-gate 				return (EFAULT);
5107c478bd9Sstevel@tonic-gate 			return (0);
5117c478bd9Sstevel@tonic-gate 		}
5127c478bd9Sstevel@tonic-gate 		case DDI_MODEL_NONE:
5137c478bd9Sstevel@tonic-gate 			if (ddi_copyout(&sz, (void *)arg, sizeof (size_t),
5147c478bd9Sstevel@tonic-gate 			    mode) != 0)
5157c478bd9Sstevel@tonic-gate 				return (EFAULT);
5167c478bd9Sstevel@tonic-gate 			return (0);
5177c478bd9Sstevel@tonic-gate 		default:
5187c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
5197c478bd9Sstevel@tonic-gate 			    "mdesc: Invalid data model %d in ioctl\n",
5207c478bd9Sstevel@tonic-gate 			    ddi_model_convert_from(mode & FMODELS));
5217c478bd9Sstevel@tonic-gate 			return (ENOTSUP);
5227c478bd9Sstevel@tonic-gate 		}
5237c478bd9Sstevel@tonic-gate #else /* ! _MULTI_DATAMODEL */
5247c478bd9Sstevel@tonic-gate 		if (ddi_copyout(&sz, (void *)arg, sizeof (size_t), mode) != 0)
5257c478bd9Sstevel@tonic-gate 			return (EFAULT);
5267c478bd9Sstevel@tonic-gate 		return (0);
5277c478bd9Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
5287c478bd9Sstevel@tonic-gate 	}
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	default:
5317c478bd9Sstevel@tonic-gate 		return (ENOTTY);
5327c478bd9Sstevel@tonic-gate 	}
5337c478bd9Sstevel@tonic-gate }
534