xref: /onnv-gate/usr/src/uts/sun4v/io/mdesc.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * sun4v machine description driver
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <sys/types.h>
34*0Sstevel@tonic-gate #include <sys/file.h>
35*0Sstevel@tonic-gate #include <sys/errno.h>
36*0Sstevel@tonic-gate #include <sys/open.h>
37*0Sstevel@tonic-gate #include <sys/cred.h>
38*0Sstevel@tonic-gate #include <sys/uio.h>
39*0Sstevel@tonic-gate #include <sys/stat.h>
40*0Sstevel@tonic-gate #include <sys/ksynch.h>
41*0Sstevel@tonic-gate #include <sys/modctl.h>
42*0Sstevel@tonic-gate #include <sys/conf.h>
43*0Sstevel@tonic-gate #include <sys/devops.h>
44*0Sstevel@tonic-gate #include <sys/debug.h>
45*0Sstevel@tonic-gate #include <sys/cmn_err.h>
46*0Sstevel@tonic-gate #include <sys/ddi.h>
47*0Sstevel@tonic-gate #include <sys/sunddi.h>
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate #include <sys/mdesc.h>
50*0Sstevel@tonic-gate #include <sys/mach_descrip.h>
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate #define	MDESC_NAME	"mdesc"
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate /*
55*0Sstevel@tonic-gate  * Operational state flags
56*0Sstevel@tonic-gate  */
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate #define	MDESC_DIDMINOR	0x2		/* Created minors */
59*0Sstevel@tonic-gate #define	MDESC_DIDMUTEX	0x8		/* Created mutex */
60*0Sstevel@tonic-gate #define	MDESC_DIDCV	0x10		/* Created cv */
61*0Sstevel@tonic-gate #define	MDESC_BUSY	0x20		/* Device is busy */
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate static void *mdesc_state_head;
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate struct mdesc_state {
66*0Sstevel@tonic-gate 	int		instance;
67*0Sstevel@tonic-gate 	dev_info_t	*devi;
68*0Sstevel@tonic-gate 	kmutex_t	lock;
69*0Sstevel@tonic-gate 	kcondvar_t	cv;
70*0Sstevel@tonic-gate 	size_t		mdesc_len;
71*0Sstevel@tonic-gate 	uint8_t		*mdesc;
72*0Sstevel@tonic-gate 	int		flags;
73*0Sstevel@tonic-gate };
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate static int mdesc_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
76*0Sstevel@tonic-gate static int mdesc_attach(dev_info_t *, ddi_attach_cmd_t);
77*0Sstevel@tonic-gate static int mdesc_detach(dev_info_t *, ddi_detach_cmd_t);
78*0Sstevel@tonic-gate static int mdesc_open(dev_t *, int, int, cred_t *);
79*0Sstevel@tonic-gate static int mdesc_close(dev_t, int, int, cred_t *);
80*0Sstevel@tonic-gate static int mdesc_read(dev_t, struct uio *, cred_t *);
81*0Sstevel@tonic-gate static int mdesc_write(dev_t, struct uio *, cred_t *);
82*0Sstevel@tonic-gate static int mdesc_rw(dev_t, struct uio *, enum uio_rw);
83*0Sstevel@tonic-gate static int mdesc_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate static struct cb_ops mdesc_cb_ops = {
86*0Sstevel@tonic-gate 	mdesc_open,		/* cb_open */
87*0Sstevel@tonic-gate 	mdesc_close,		/* cb_close */
88*0Sstevel@tonic-gate 	nodev,			/* cb_strategy */
89*0Sstevel@tonic-gate 	nodev,			/* cb_print */
90*0Sstevel@tonic-gate 	nodev,			/* cb_dump */
91*0Sstevel@tonic-gate 	mdesc_read,		/* cb_read */
92*0Sstevel@tonic-gate 	nodev,			/* cb_write */
93*0Sstevel@tonic-gate 	mdesc_ioctl,		/* cb_ioctl */
94*0Sstevel@tonic-gate 	nodev,			/* cb_devmap */
95*0Sstevel@tonic-gate 	nodev,			/* cb_mmap */
96*0Sstevel@tonic-gate 	nodev,			/* cb_segmap */
97*0Sstevel@tonic-gate 	nochpoll,		/* cb_chpoll */
98*0Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
99*0Sstevel@tonic-gate 	(struct streamtab *)NULL, /* cb_str */
100*0Sstevel@tonic-gate 	D_MP | D_64BIT,		/* cb_flag */
101*0Sstevel@tonic-gate 	CB_REV,			/* cb_rev */
102*0Sstevel@tonic-gate 	nodev,			/* cb_aread */
103*0Sstevel@tonic-gate 	nodev			/* cb_awrite */
104*0Sstevel@tonic-gate };
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate static struct dev_ops mdesc_dev_ops = {
107*0Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
108*0Sstevel@tonic-gate 	0,			/* devo_refcnt */
109*0Sstevel@tonic-gate 	mdesc_getinfo,		/* devo_getinfo */
110*0Sstevel@tonic-gate 	nulldev,		/* devo_identify */
111*0Sstevel@tonic-gate 	nulldev,		/* devo_probe */
112*0Sstevel@tonic-gate 	mdesc_attach,		/* devo_attach */
113*0Sstevel@tonic-gate 	mdesc_detach,		/* devo_detach */
114*0Sstevel@tonic-gate 	nodev,			/* devo_reset */
115*0Sstevel@tonic-gate 	&mdesc_cb_ops,		/* devo_cb_ops */
116*0Sstevel@tonic-gate 	(struct bus_ops *)NULL,	/* devo_bus_ops */
117*0Sstevel@tonic-gate 	nulldev			/* devo_power */
118*0Sstevel@tonic-gate };
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate static struct modldrv modldrv = {
121*0Sstevel@tonic-gate 	&mod_driverops,
122*0Sstevel@tonic-gate 	"Machine Description Driver 1.0",
123*0Sstevel@tonic-gate 	&mdesc_dev_ops};
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate static struct modlinkage modlinkage = {
126*0Sstevel@tonic-gate 	MODREV_1,
127*0Sstevel@tonic-gate 	(void *)&modldrv,
128*0Sstevel@tonic-gate 	NULL
129*0Sstevel@tonic-gate };
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate int
139*0Sstevel@tonic-gate _init(void)
140*0Sstevel@tonic-gate {
141*0Sstevel@tonic-gate 	int retval;
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 	if ((retval = ddi_soft_state_init(&mdesc_state_head,
144*0Sstevel@tonic-gate 	    sizeof (struct mdesc_state), 1)) != 0)
145*0Sstevel@tonic-gate 		return (retval);
146*0Sstevel@tonic-gate 	if ((retval = mod_install(&modlinkage)) != 0) {
147*0Sstevel@tonic-gate 		ddi_soft_state_fini(&mdesc_state_head);
148*0Sstevel@tonic-gate 		return (retval);
149*0Sstevel@tonic-gate 	}
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	return (retval);
152*0Sstevel@tonic-gate }
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate int
158*0Sstevel@tonic-gate _info(struct modinfo *modinfop)
159*0Sstevel@tonic-gate {
160*0Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
161*0Sstevel@tonic-gate }
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate int
167*0Sstevel@tonic-gate _fini(void)
168*0Sstevel@tonic-gate {
169*0Sstevel@tonic-gate 	int retval;
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 	if ((retval = mod_remove(&modlinkage)) != 0)
172*0Sstevel@tonic-gate 		return (retval);
173*0Sstevel@tonic-gate 	ddi_soft_state_fini(&mdesc_state_head);
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 	return (retval);
176*0Sstevel@tonic-gate }
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate /*ARGSUSED*/
182*0Sstevel@tonic-gate static int
183*0Sstevel@tonic-gate mdesc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resultp)
184*0Sstevel@tonic-gate {
185*0Sstevel@tonic-gate 	struct mdesc_state *mdsp;
186*0Sstevel@tonic-gate 	int retval = DDI_FAILURE;
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 	ASSERT(resultp != NULL);
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 	switch (cmd) {
191*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
192*0Sstevel@tonic-gate 		if ((mdsp = ddi_get_soft_state(mdesc_state_head,
193*0Sstevel@tonic-gate 		    getminor((dev_t)arg))) != NULL) {
194*0Sstevel@tonic-gate 			*resultp = mdsp->devi;
195*0Sstevel@tonic-gate 			retval = DDI_SUCCESS;
196*0Sstevel@tonic-gate 		} else
197*0Sstevel@tonic-gate 			*resultp = NULL;
198*0Sstevel@tonic-gate 		break;
199*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
200*0Sstevel@tonic-gate 		*resultp = (void *)getminor((dev_t)arg);
201*0Sstevel@tonic-gate 		retval = DDI_SUCCESS;
202*0Sstevel@tonic-gate 		break;
203*0Sstevel@tonic-gate 	}
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate 	return (retval);
206*0Sstevel@tonic-gate }
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate static int
212*0Sstevel@tonic-gate mdesc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
213*0Sstevel@tonic-gate {
214*0Sstevel@tonic-gate 	int instance = ddi_get_instance(dip);
215*0Sstevel@tonic-gate 	struct mdesc_state *mdsp;
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 	switch (cmd) {
218*0Sstevel@tonic-gate 	case DDI_ATTACH:
219*0Sstevel@tonic-gate 		if (ddi_soft_state_zalloc(mdesc_state_head, instance) !=
220*0Sstevel@tonic-gate 		    DDI_SUCCESS) {
221*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s@%d: Unable to allocate state",
222*0Sstevel@tonic-gate 			    MDESC_NAME, instance);
223*0Sstevel@tonic-gate 			return (DDI_FAILURE);
224*0Sstevel@tonic-gate 		}
225*0Sstevel@tonic-gate 		if ((mdsp = ddi_get_soft_state(mdesc_state_head, instance)) ==
226*0Sstevel@tonic-gate 		    NULL) {
227*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s@%d: Unable to obtain state",
228*0Sstevel@tonic-gate 			    MDESC_NAME, instance);
229*0Sstevel@tonic-gate 			ddi_soft_state_free(dip, instance);
230*0Sstevel@tonic-gate 			return (DDI_FAILURE);
231*0Sstevel@tonic-gate 		}
232*0Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, MDESC_NAME, S_IFCHR, instance,
233*0Sstevel@tonic-gate 		    DDI_PSEUDO, 0) != DDI_SUCCESS) {
234*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s@%d: Unable to create minor node",
235*0Sstevel@tonic-gate 			    MDESC_NAME, instance);
236*0Sstevel@tonic-gate 			(void) mdesc_detach(dip, DDI_DETACH);
237*0Sstevel@tonic-gate 			return (DDI_FAILURE);
238*0Sstevel@tonic-gate 		}
239*0Sstevel@tonic-gate 		mdsp->flags |= MDESC_DIDMINOR;
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate 		mdsp->instance = instance;
242*0Sstevel@tonic-gate 		mdsp->devi = dip;
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate 		mutex_init(&mdsp->lock, NULL, MUTEX_DRIVER, NULL);
245*0Sstevel@tonic-gate 		mdsp->flags |= MDESC_DIDMUTEX;
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 		cv_init(&mdsp->cv, NULL, CV_DRIVER, NULL);
248*0Sstevel@tonic-gate 		mdsp->flags |= MDESC_DIDCV;
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 			/* point the driver at the kernel's copy of the data */
251*0Sstevel@tonic-gate 		mdsp->mdesc = (uint8_t *)machine_descrip.va;
252*0Sstevel@tonic-gate 		mdsp->mdesc_len = (machine_descrip.va != NULL) ?
253*0Sstevel@tonic-gate 		    machine_descrip.size : 0;
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 		ddi_report_dev(dip);
256*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
257*0Sstevel@tonic-gate 	case DDI_RESUME:
258*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
259*0Sstevel@tonic-gate 	default:
260*0Sstevel@tonic-gate 		return (DDI_FAILURE);
261*0Sstevel@tonic-gate 	}
262*0Sstevel@tonic-gate }
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate static int
267*0Sstevel@tonic-gate mdesc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
268*0Sstevel@tonic-gate {
269*0Sstevel@tonic-gate 	int instance = ddi_get_instance(dip);
270*0Sstevel@tonic-gate 	struct mdesc_state *mdsp;
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate 	switch (cmd) {
273*0Sstevel@tonic-gate 	case DDI_DETACH:
274*0Sstevel@tonic-gate 		mdsp = ddi_get_soft_state(mdesc_state_head, instance);
275*0Sstevel@tonic-gate 		if (mdsp != NULL) {
276*0Sstevel@tonic-gate 			ASSERT(!(mdsp->flags & MDESC_BUSY));
277*0Sstevel@tonic-gate 			if (mdsp->flags & MDESC_DIDCV)
278*0Sstevel@tonic-gate 				cv_destroy(&mdsp->cv);
279*0Sstevel@tonic-gate 			if (mdsp->flags & MDESC_DIDMUTEX)
280*0Sstevel@tonic-gate 				mutex_destroy(&mdsp->lock);
281*0Sstevel@tonic-gate 			if (mdsp->flags & MDESC_DIDMINOR)
282*0Sstevel@tonic-gate 				ddi_remove_minor_node(dip, NULL);
283*0Sstevel@tonic-gate 		}
284*0Sstevel@tonic-gate 		ddi_soft_state_free(mdesc_state_head, instance);
285*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate 	case DDI_SUSPEND:
288*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate 	default:
291*0Sstevel@tonic-gate 		return (DDI_FAILURE);
292*0Sstevel@tonic-gate 	}
293*0Sstevel@tonic-gate }
294*0Sstevel@tonic-gate 
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate /*ARGSUSED*/
298*0Sstevel@tonic-gate static int
299*0Sstevel@tonic-gate mdesc_open(dev_t *devp, int flag, int otyp, cred_t *credp)
300*0Sstevel@tonic-gate {
301*0Sstevel@tonic-gate 	int instance = getminor(*devp);
302*0Sstevel@tonic-gate 	struct mdesc_state *mdsp;
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 	if ((mdsp = ddi_get_soft_state(mdesc_state_head, instance)) == NULL)
305*0Sstevel@tonic-gate 		return (ENXIO);
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate 	ASSERT(mdsp->instance == instance);
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
310*0Sstevel@tonic-gate 		return (EINVAL);
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate 	return (0);
313*0Sstevel@tonic-gate }
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate 
318*0Sstevel@tonic-gate /*ARGSUSED*/
319*0Sstevel@tonic-gate static int
320*0Sstevel@tonic-gate mdesc_close(dev_t dev, int flag, int otyp, cred_t *credp)
321*0Sstevel@tonic-gate {
322*0Sstevel@tonic-gate 	struct mdesc_state *mdsp;
323*0Sstevel@tonic-gate 	int instance = getminor(dev);
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate 	if ((mdsp = ddi_get_soft_state(mdesc_state_head, instance)) == NULL)
326*0Sstevel@tonic-gate 		return (ENXIO);
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate 	ASSERT(mdsp->instance == instance);
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
331*0Sstevel@tonic-gate 		return (EINVAL);
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate 	return (0);
334*0Sstevel@tonic-gate }
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate 
339*0Sstevel@tonic-gate /*ARGSUSED*/
340*0Sstevel@tonic-gate static int
341*0Sstevel@tonic-gate mdesc_read(dev_t dev, struct uio *uiop, cred_t *credp)
342*0Sstevel@tonic-gate {
343*0Sstevel@tonic-gate 	return (mdesc_rw(dev, uiop, UIO_READ));
344*0Sstevel@tonic-gate }
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate 
348*0Sstevel@tonic-gate 
349*0Sstevel@tonic-gate /*ARGSUSED*/
350*0Sstevel@tonic-gate static int
351*0Sstevel@tonic-gate mdesc_write(dev_t dev, struct uio *uiop, cred_t *credp)
352*0Sstevel@tonic-gate {
353*0Sstevel@tonic-gate 	return (ENXIO);	/* This driver version does not allow updates */
354*0Sstevel@tonic-gate }
355*0Sstevel@tonic-gate 
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate 
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate static int
360*0Sstevel@tonic-gate mdesc_rw(dev_t dev, struct uio *uiop, enum uio_rw rw)
361*0Sstevel@tonic-gate {
362*0Sstevel@tonic-gate 	struct mdesc_state *mdsp;
363*0Sstevel@tonic-gate 	int instance = getminor(dev);
364*0Sstevel@tonic-gate 	size_t len;
365*0Sstevel@tonic-gate 	int retval;
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate 	len = uiop->uio_resid;
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate 	if ((mdsp = ddi_get_soft_state(mdesc_state_head, instance)) == NULL)
370*0Sstevel@tonic-gate 		return (ENXIO);
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate 	ASSERT(mdsp->instance == instance);
373*0Sstevel@tonic-gate 
374*0Sstevel@tonic-gate 	if (len == 0)
375*0Sstevel@tonic-gate 		return (0);
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 	mutex_enter(&mdsp->lock);
378*0Sstevel@tonic-gate 
379*0Sstevel@tonic-gate 	while (mdsp->flags & MDESC_BUSY) {
380*0Sstevel@tonic-gate 		if (cv_wait_sig(&mdsp->cv, &mdsp->lock) == 0) {
381*0Sstevel@tonic-gate 			mutex_exit(&mdsp->lock);
382*0Sstevel@tonic-gate 			return (EINTR);
383*0Sstevel@tonic-gate 		}
384*0Sstevel@tonic-gate 	}
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate 	if (uiop->uio_offset < 0 || uiop->uio_offset > mdsp->mdesc_len) {
387*0Sstevel@tonic-gate 		mutex_exit(&mdsp->lock);
388*0Sstevel@tonic-gate 		return (EINVAL);
389*0Sstevel@tonic-gate 	}
390*0Sstevel@tonic-gate 
391*0Sstevel@tonic-gate 	if (len > (mdsp->mdesc_len - uiop->uio_offset))
392*0Sstevel@tonic-gate 		len = mdsp->mdesc_len - uiop->uio_offset;
393*0Sstevel@tonic-gate 
394*0Sstevel@tonic-gate 		/* already checked that offset<mdesc_len above */
395*0Sstevel@tonic-gate 	if (len == 0) {
396*0Sstevel@tonic-gate 		mutex_exit(&mdsp->lock);
397*0Sstevel@tonic-gate 		return (rw == UIO_WRITE ? ENOSPC : 0);
398*0Sstevel@tonic-gate 	}
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate 	mdsp->flags |= MDESC_BUSY;
401*0Sstevel@tonic-gate 	mutex_exit(&mdsp->lock);
402*0Sstevel@tonic-gate 
403*0Sstevel@tonic-gate 	retval = uiomove((void *)(mdsp->mdesc + uiop->uio_offset),
404*0Sstevel@tonic-gate 		len, rw, uiop);
405*0Sstevel@tonic-gate 
406*0Sstevel@tonic-gate 	mutex_enter(&mdsp->lock);
407*0Sstevel@tonic-gate 	mdsp->flags &= ~MDESC_BUSY;
408*0Sstevel@tonic-gate 	cv_broadcast(&mdsp->cv);
409*0Sstevel@tonic-gate 	mutex_exit(&mdsp->lock);
410*0Sstevel@tonic-gate 
411*0Sstevel@tonic-gate 	return (retval);
412*0Sstevel@tonic-gate }
413*0Sstevel@tonic-gate 
414*0Sstevel@tonic-gate 
415*0Sstevel@tonic-gate 
416*0Sstevel@tonic-gate 
417*0Sstevel@tonic-gate /*ARGSUSED*/
418*0Sstevel@tonic-gate static int
419*0Sstevel@tonic-gate mdesc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
420*0Sstevel@tonic-gate     int *rvalp)
421*0Sstevel@tonic-gate {
422*0Sstevel@tonic-gate 	struct mdesc_state *mdsp;
423*0Sstevel@tonic-gate 	int instance = getminor(dev);
424*0Sstevel@tonic-gate 
425*0Sstevel@tonic-gate 	if ((mdsp = ddi_get_soft_state(mdesc_state_head, instance)) == NULL)
426*0Sstevel@tonic-gate 		return (ENXIO);
427*0Sstevel@tonic-gate 
428*0Sstevel@tonic-gate 	ASSERT(mdsp->instance == instance);
429*0Sstevel@tonic-gate 
430*0Sstevel@tonic-gate 	switch (cmd) {
431*0Sstevel@tonic-gate 	case MDESCIOCGSZ: {
432*0Sstevel@tonic-gate 		/*
433*0Sstevel@tonic-gate 		 * We are not guaranteed that ddi_copyout(9F) will read
434*0Sstevel@tonic-gate 		 * atomically anything larger than a byte.  Therefore we
435*0Sstevel@tonic-gate 		 * must duplicate the size before copying it out to the user.
436*0Sstevel@tonic-gate 		 */
437*0Sstevel@tonic-gate 		size_t sz = mdsp->mdesc_len;
438*0Sstevel@tonic-gate 
439*0Sstevel@tonic-gate 		if (!(mode & FREAD))
440*0Sstevel@tonic-gate 			return (EACCES);
441*0Sstevel@tonic-gate 
442*0Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
443*0Sstevel@tonic-gate 		switch (ddi_model_convert_from(mode & FMODELS)) {
444*0Sstevel@tonic-gate 		case DDI_MODEL_ILP32: {
445*0Sstevel@tonic-gate 			size32_t sz32 = (size32_t)sz;
446*0Sstevel@tonic-gate 			if (ddi_copyout(&sz32, (void *)arg, sizeof (size32_t),
447*0Sstevel@tonic-gate 			    mode) != 0)
448*0Sstevel@tonic-gate 				return (EFAULT);
449*0Sstevel@tonic-gate 			return (0);
450*0Sstevel@tonic-gate 		}
451*0Sstevel@tonic-gate 		case DDI_MODEL_NONE:
452*0Sstevel@tonic-gate 			if (ddi_copyout(&sz, (void *)arg, sizeof (size_t),
453*0Sstevel@tonic-gate 			    mode) != 0)
454*0Sstevel@tonic-gate 				return (EFAULT);
455*0Sstevel@tonic-gate 			return (0);
456*0Sstevel@tonic-gate 		default:
457*0Sstevel@tonic-gate 			cmn_err(CE_WARN,
458*0Sstevel@tonic-gate 			    "mdesc: Invalid data model %d in ioctl\n",
459*0Sstevel@tonic-gate 			    ddi_model_convert_from(mode & FMODELS));
460*0Sstevel@tonic-gate 			return (ENOTSUP);
461*0Sstevel@tonic-gate 		}
462*0Sstevel@tonic-gate #else /* ! _MULTI_DATAMODEL */
463*0Sstevel@tonic-gate 		if (ddi_copyout(&sz, (void *)arg, sizeof (size_t), mode) != 0)
464*0Sstevel@tonic-gate 			return (EFAULT);
465*0Sstevel@tonic-gate 		return (0);
466*0Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
467*0Sstevel@tonic-gate 	}
468*0Sstevel@tonic-gate 
469*0Sstevel@tonic-gate 	default:
470*0Sstevel@tonic-gate 		return (ENOTTY);
471*0Sstevel@tonic-gate 	}
472*0Sstevel@tonic-gate }
473