xref: /onnv-gate/usr/src/uts/sun/io/sbusmem.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 1991-2002 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 #include <sys/types.h>
30*0Sstevel@tonic-gate #include <sys/conf.h>
31*0Sstevel@tonic-gate #include <sys/cmn_err.h>
32*0Sstevel@tonic-gate #include <sys/kmem.h>
33*0Sstevel@tonic-gate #include <sys/open.h>
34*0Sstevel@tonic-gate #include <sys/stat.h>
35*0Sstevel@tonic-gate #include <sys/modctl.h>
36*0Sstevel@tonic-gate #include <sys/errno.h>
37*0Sstevel@tonic-gate #include <sys/ddi.h>
38*0Sstevel@tonic-gate #include <sys/sunddi.h>
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate /* #define SBUSMEM_DEBUG */
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate #ifdef SBUSMEM_DEBUG
43*0Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate int sbusmem_debug_flag;
46*0Sstevel@tonic-gate #define	sbusmem_debug	if (sbusmem_debug_flag) printf
47*0Sstevel@tonic-gate #endif /* SBUSMEM_DEBUG */
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate static void *sbusmem_state_head;
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate struct sbusmem_unit {
52*0Sstevel@tonic-gate 	uint_t size;
53*0Sstevel@tonic-gate 	uint_t pagesize;
54*0Sstevel@tonic-gate 	dev_info_t *dip;
55*0Sstevel@tonic-gate };
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate static int sbmem_open(dev_t *, int, int, cred_t *);
58*0Sstevel@tonic-gate static int sbmem_close(dev_t, int, int, struct cred *);
59*0Sstevel@tonic-gate static int sbmem_read(dev_t, struct uio *, cred_t *);
60*0Sstevel@tonic-gate static int sbmem_write(dev_t, struct uio *, cred_t *);
61*0Sstevel@tonic-gate static int sbmem_devmap(dev_t, devmap_cookie_t, offset_t, size_t,
62*0Sstevel@tonic-gate 		size_t *, uint_t);
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate static struct cb_ops sbmem_cb_ops = {
65*0Sstevel@tonic-gate 	sbmem_open,		/* open */
66*0Sstevel@tonic-gate 	sbmem_close,		/* close */
67*0Sstevel@tonic-gate 	nodev,			/* strategy */
68*0Sstevel@tonic-gate 	nodev,			/* print */
69*0Sstevel@tonic-gate 	nodev,			/* dump */
70*0Sstevel@tonic-gate 	sbmem_read,		/* read */
71*0Sstevel@tonic-gate 	sbmem_write,		/* write */
72*0Sstevel@tonic-gate 	nodev,			/* ioctl */
73*0Sstevel@tonic-gate 	sbmem_devmap,		/* devmap */
74*0Sstevel@tonic-gate 	nodev,			/* mmap */
75*0Sstevel@tonic-gate 	ddi_devmap_segmap,	/* segmap */
76*0Sstevel@tonic-gate 	nochpoll,		/* poll */
77*0Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
78*0Sstevel@tonic-gate 	0,			/* streamtab  */
79*0Sstevel@tonic-gate 	D_NEW|D_MP|D_DEVMAP|D_HOTPLUG,	/* Driver compatibility flag */
80*0Sstevel@tonic-gate 	CB_REV,			/* rev */
81*0Sstevel@tonic-gate 	nodev,			/* int (*cb_aread)() */
82*0Sstevel@tonic-gate 	nodev			/* int (*cb_awrite)() */
83*0Sstevel@tonic-gate };
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate static int sbmem_attach(dev_info_t *, ddi_attach_cmd_t);
86*0Sstevel@tonic-gate static int sbmem_detach(dev_info_t *, ddi_detach_cmd_t);
87*0Sstevel@tonic-gate static int sbmem_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate static struct dev_ops sbmem_ops = {
90*0Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
91*0Sstevel@tonic-gate 	0,			/* refcnt  */
92*0Sstevel@tonic-gate 	sbmem_info,		/* get_dev_info */
93*0Sstevel@tonic-gate 	nulldev,		/* identify */
94*0Sstevel@tonic-gate 	nulldev,		/* probe */
95*0Sstevel@tonic-gate 	sbmem_attach,		/* attach */
96*0Sstevel@tonic-gate 	sbmem_detach,		/* detach */
97*0Sstevel@tonic-gate 	nodev,			/* reset */
98*0Sstevel@tonic-gate 	&sbmem_cb_ops,		/* driver operations */
99*0Sstevel@tonic-gate 	(struct bus_ops *)0,	/* bus operations */
100*0Sstevel@tonic-gate 	nulldev			/* power */
101*0Sstevel@tonic-gate };
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate static struct modldrv modldrv = {
104*0Sstevel@tonic-gate 	&mod_driverops,	/* Type of module.  This one is a driver */
105*0Sstevel@tonic-gate 	"SBus memory driver v%I%", /* Name of module. */
106*0Sstevel@tonic-gate 	&sbmem_ops,	/* driver ops */
107*0Sstevel@tonic-gate };
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate static struct modlinkage modlinkage = {
110*0Sstevel@tonic-gate 	MODREV_1, (void *)&modldrv, NULL
111*0Sstevel@tonic-gate };
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate static int sbmem_rw(dev_t, struct uio *, enum uio_rw, cred_t *);
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate #if !defined(lint)
116*0Sstevel@tonic-gate static char sbusmem_initmsg[] = "sbusmem _init: sbusmem.c\t%I%\t%E%\n";
117*0Sstevel@tonic-gate #endif
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate int
120*0Sstevel@tonic-gate _init(void)
121*0Sstevel@tonic-gate {
122*0Sstevel@tonic-gate 	int error;
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 	if ((error = ddi_soft_state_init(&sbusmem_state_head,
125*0Sstevel@tonic-gate 	    sizeof (struct sbusmem_unit), 1)) != 0) {
126*0Sstevel@tonic-gate 		return (error);
127*0Sstevel@tonic-gate 	}
128*0Sstevel@tonic-gate 	if ((error = mod_install(&modlinkage)) != 0) {
129*0Sstevel@tonic-gate 		ddi_soft_state_fini(&sbusmem_state_head);
130*0Sstevel@tonic-gate 	}
131*0Sstevel@tonic-gate 	return (error);
132*0Sstevel@tonic-gate }
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate int
135*0Sstevel@tonic-gate _fini(void)
136*0Sstevel@tonic-gate {
137*0Sstevel@tonic-gate 	int error;
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	if ((error = mod_remove(&modlinkage)) == 0) {
140*0Sstevel@tonic-gate 		ddi_soft_state_fini(&sbusmem_state_head);
141*0Sstevel@tonic-gate 	}
142*0Sstevel@tonic-gate 	return (error);
143*0Sstevel@tonic-gate }
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate int
146*0Sstevel@tonic-gate _info(struct modinfo *modinfop)
147*0Sstevel@tonic-gate {
148*0Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
149*0Sstevel@tonic-gate }
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate static int
152*0Sstevel@tonic-gate sbmem_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
153*0Sstevel@tonic-gate {
154*0Sstevel@tonic-gate 	struct sbusmem_unit *un;
155*0Sstevel@tonic-gate 	int error = DDI_FAILURE;
156*0Sstevel@tonic-gate 	int instance, ilen;
157*0Sstevel@tonic-gate 	uint_t size;
158*0Sstevel@tonic-gate 	char *ident;
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	switch (cmd) {
161*0Sstevel@tonic-gate 	case DDI_ATTACH:
162*0Sstevel@tonic-gate 		instance = ddi_get_instance(devi);
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 		size = ddi_getprop(DDI_DEV_T_NONE, devi,
165*0Sstevel@tonic-gate 		    DDI_PROP_DONTPASS | DDI_PROP_CANSLEEP, "size", -1);
166*0Sstevel@tonic-gate 		if (size == (uint_t)-1) {
167*0Sstevel@tonic-gate #ifdef SBUSMEM_DEBUG
168*0Sstevel@tonic-gate 			sbusmem_debug(
169*0Sstevel@tonic-gate 			    "sbmem_attach%d: No size property\n", instance);
170*0Sstevel@tonic-gate #endif /* SBUSMEM_DEBUG */
171*0Sstevel@tonic-gate 			break;
172*0Sstevel@tonic-gate 		}
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate #ifdef SBUSMEM_DEBUG
175*0Sstevel@tonic-gate 		{
176*0Sstevel@tonic-gate 			struct regspec *rp = ddi_rnumber_to_regspec(devi, 0);
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 			if (rp == NULL) {
179*0Sstevel@tonic-gate 				sbusmem_debug(
180*0Sstevel@tonic-gate 			    "sbmem_attach%d: No reg property\n", instance);
181*0Sstevel@tonic-gate 			} else {
182*0Sstevel@tonic-gate 				sbusmem_debug(
183*0Sstevel@tonic-gate 			    "sbmem_attach%d: slot 0x%x size 0x%x\n", instance,
184*0Sstevel@tonic-gate 				    rp->regspec_bustype, rp->regspec_size);
185*0Sstevel@tonic-gate 			}
186*0Sstevel@tonic-gate 		}
187*0Sstevel@tonic-gate #endif /* SBUSMEM_DEBUG */
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 		if (ddi_getlongprop(DDI_DEV_T_NONE, devi,
190*0Sstevel@tonic-gate 		    DDI_PROP_DONTPASS | DDI_PROP_CANSLEEP, "ident",
191*0Sstevel@tonic-gate 		    (caddr_t)&ident, &ilen) != DDI_PROP_SUCCESS) {
192*0Sstevel@tonic-gate #ifdef SBUSMEM_DEBUG
193*0Sstevel@tonic-gate 			sbusmem_debug(
194*0Sstevel@tonic-gate 			    "sbmem_attach%d: No ident property\n", instance);
195*0Sstevel@tonic-gate #endif /* SBUSMEM_DEBUG */
196*0Sstevel@tonic-gate 			break;
197*0Sstevel@tonic-gate 		}
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 		if (ddi_soft_state_zalloc(sbusmem_state_head,
200*0Sstevel@tonic-gate 		    instance) != DDI_SUCCESS)
201*0Sstevel@tonic-gate 			break;
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 		if ((un = ddi_get_soft_state(sbusmem_state_head,
204*0Sstevel@tonic-gate 		    instance)) == NULL) {
205*0Sstevel@tonic-gate 			ddi_soft_state_free(sbusmem_state_head, instance);
206*0Sstevel@tonic-gate 			break;
207*0Sstevel@tonic-gate 		}
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 		if (ddi_create_minor_node(devi, ident, S_IFCHR, instance,
210*0Sstevel@tonic-gate 		    DDI_PSEUDO, NULL) == DDI_FAILURE) {
211*0Sstevel@tonic-gate 			kmem_free(ident, ilen);
212*0Sstevel@tonic-gate 			ddi_remove_minor_node(devi, NULL);
213*0Sstevel@tonic-gate 			ddi_soft_state_free(sbusmem_state_head, instance);
214*0Sstevel@tonic-gate 			break;
215*0Sstevel@tonic-gate 		}
216*0Sstevel@tonic-gate 		kmem_free(ident, ilen);
217*0Sstevel@tonic-gate 		un->dip = devi;
218*0Sstevel@tonic-gate 		un->size = size;
219*0Sstevel@tonic-gate 		un->pagesize = ddi_ptob(devi, 1);
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate #ifdef SBUSMEM_DEBUG
222*0Sstevel@tonic-gate 		sbusmem_debug("sbmem_attach%d: dip 0x%p size 0x%x\n",
223*0Sstevel@tonic-gate 		    instance, devi, size);
224*0Sstevel@tonic-gate #endif /* SBUSMEM_DEBUG */
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate 		ddi_report_dev(devi);
227*0Sstevel@tonic-gate 		error = DDI_SUCCESS;
228*0Sstevel@tonic-gate 		break;
229*0Sstevel@tonic-gate 	case DDI_RESUME:
230*0Sstevel@tonic-gate 		error = DDI_SUCCESS;
231*0Sstevel@tonic-gate 		break;
232*0Sstevel@tonic-gate 	default:
233*0Sstevel@tonic-gate 		break;
234*0Sstevel@tonic-gate 	}
235*0Sstevel@tonic-gate 	return (error);
236*0Sstevel@tonic-gate }
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate static int
239*0Sstevel@tonic-gate sbmem_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
240*0Sstevel@tonic-gate {
241*0Sstevel@tonic-gate 	int instance;
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 	switch (cmd) {
244*0Sstevel@tonic-gate 	case DDI_DETACH:
245*0Sstevel@tonic-gate 		instance = ddi_get_instance(devi);
246*0Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
247*0Sstevel@tonic-gate 		ddi_soft_state_free(sbusmem_state_head, instance);
248*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 	case DDI_SUSPEND:
251*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
252*0Sstevel@tonic-gate 	}
253*0Sstevel@tonic-gate 	return (DDI_FAILURE);
254*0Sstevel@tonic-gate }
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate /*ARGSUSED1*/
257*0Sstevel@tonic-gate static int
258*0Sstevel@tonic-gate sbmem_open(dev_t *devp, int flag, int typ, cred_t *cred)
259*0Sstevel@tonic-gate {
260*0Sstevel@tonic-gate 	int instance;
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate 	if (typ != OTYP_CHR)
263*0Sstevel@tonic-gate 		return (EINVAL);
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate 	instance = getminor(*devp);
266*0Sstevel@tonic-gate 	if (ddi_get_soft_state(sbusmem_state_head, instance) == NULL) {
267*0Sstevel@tonic-gate 		return (ENXIO);
268*0Sstevel@tonic-gate 	}
269*0Sstevel@tonic-gate 	return (0);
270*0Sstevel@tonic-gate }
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate /*ARGSUSED*/
273*0Sstevel@tonic-gate static int
274*0Sstevel@tonic-gate sbmem_close(dev_t dev, int flag, int otyp, struct cred *cred)
275*0Sstevel@tonic-gate {
276*0Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
277*0Sstevel@tonic-gate 		return (EINVAL);
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate 	return (0);
280*0Sstevel@tonic-gate }
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate static int
283*0Sstevel@tonic-gate sbmem_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
284*0Sstevel@tonic-gate {
285*0Sstevel@tonic-gate 	int instance, error = DDI_FAILURE;
286*0Sstevel@tonic-gate 	struct sbusmem_unit *un;
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate #if defined(lint) || defined(__lint)
289*0Sstevel@tonic-gate 	dip = dip;
290*0Sstevel@tonic-gate #endif /* lint || __lint */
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 	switch (infocmd) {
293*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
294*0Sstevel@tonic-gate 		instance = getminor((dev_t)arg);
295*0Sstevel@tonic-gate 		if ((un = ddi_get_soft_state(sbusmem_state_head,
296*0Sstevel@tonic-gate 		    instance)) != NULL) {
297*0Sstevel@tonic-gate 			*result = (void *)un->dip;
298*0Sstevel@tonic-gate 			error = DDI_SUCCESS;
299*0Sstevel@tonic-gate #ifdef SBUSMEM_DEBUG
300*0Sstevel@tonic-gate 		sbusmem_debug(
301*0Sstevel@tonic-gate 		    "sbmem_info%d: returning dip 0x%p\n", instance, un->dip);
302*0Sstevel@tonic-gate #endif /* SBUSMEM_DEBUG */
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 		}
305*0Sstevel@tonic-gate 		break;
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
308*0Sstevel@tonic-gate 		instance = getminor((dev_t)arg);
309*0Sstevel@tonic-gate 		*result = (void *)instance;
310*0Sstevel@tonic-gate 		error = DDI_SUCCESS;
311*0Sstevel@tonic-gate 		break;
312*0Sstevel@tonic-gate 
313*0Sstevel@tonic-gate 	default:
314*0Sstevel@tonic-gate 		break;
315*0Sstevel@tonic-gate 	}
316*0Sstevel@tonic-gate 	return (error);
317*0Sstevel@tonic-gate }
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate static int
320*0Sstevel@tonic-gate sbmem_read(dev_t dev, struct uio *uio, cred_t *cred)
321*0Sstevel@tonic-gate {
322*0Sstevel@tonic-gate 	return (sbmem_rw(dev, uio, UIO_READ, cred));
323*0Sstevel@tonic-gate }
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate static int
326*0Sstevel@tonic-gate sbmem_write(dev_t dev, struct uio *uio, cred_t *cred)
327*0Sstevel@tonic-gate {
328*0Sstevel@tonic-gate 	return (sbmem_rw(dev, uio, UIO_WRITE, cred));
329*0Sstevel@tonic-gate }
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate static int
332*0Sstevel@tonic-gate sbmem_rw(dev_t dev, struct uio *uio, enum uio_rw rw, cred_t *cred)
333*0Sstevel@tonic-gate {
334*0Sstevel@tonic-gate 	uint_t c;
335*0Sstevel@tonic-gate 	struct iovec *iov;
336*0Sstevel@tonic-gate 	struct sbusmem_unit *un;
337*0Sstevel@tonic-gate 	uint_t pagesize, msize;
338*0Sstevel@tonic-gate 	int instance, error = 0;
339*0Sstevel@tonic-gate 	dev_info_t *dip;
340*0Sstevel@tonic-gate 	caddr_t reg;
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate #if defined(lint) || defined(__lint)
343*0Sstevel@tonic-gate 	cred = cred;
344*0Sstevel@tonic-gate #endif /* lint || __lint */
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate 	instance = getminor(dev);
347*0Sstevel@tonic-gate 	if ((un = ddi_get_soft_state(sbusmem_state_head, instance)) == NULL) {
348*0Sstevel@tonic-gate 		return (ENXIO);
349*0Sstevel@tonic-gate 	}
350*0Sstevel@tonic-gate 	dip = un->dip;
351*0Sstevel@tonic-gate 	pagesize = un->pagesize;
352*0Sstevel@tonic-gate 
353*0Sstevel@tonic-gate 	while (uio->uio_resid > 0 && error == 0) {
354*0Sstevel@tonic-gate 		iov = uio->uio_iov;
355*0Sstevel@tonic-gate 		if (iov->iov_len == 0) {
356*0Sstevel@tonic-gate 			uio->uio_iov++;
357*0Sstevel@tonic-gate 			uio->uio_iovcnt--;
358*0Sstevel@tonic-gate 			if (uio->uio_iovcnt < 0)
359*0Sstevel@tonic-gate 				cmn_err(CE_PANIC, "sbmem_rw");
360*0Sstevel@tonic-gate 			continue;
361*0Sstevel@tonic-gate 		}
362*0Sstevel@tonic-gate 
363*0Sstevel@tonic-gate 		if (uio->uio_offset > un->size) {
364*0Sstevel@tonic-gate 			return (EFAULT);
365*0Sstevel@tonic-gate 		}
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate 		if (uio->uio_offset == un->size) {
368*0Sstevel@tonic-gate 			return (0);		/* EOF */
369*0Sstevel@tonic-gate 		}
370*0Sstevel@tonic-gate 		msize = pagesize - (uio->uio_offset & (pagesize - 1));
371*0Sstevel@tonic-gate 		if (ddi_map_regs(dip, 0, &reg, uio->uio_offset,
372*0Sstevel@tonic-gate 		    (off_t)msize) != DDI_SUCCESS) {
373*0Sstevel@tonic-gate 			return (EFAULT);
374*0Sstevel@tonic-gate 		}
375*0Sstevel@tonic-gate 		c = min(msize, (uint_t)iov->iov_len);
376*0Sstevel@tonic-gate 		if (ddi_peekpokeio(dip, uio, rw, reg, (int)c,
377*0Sstevel@tonic-gate 		    sizeof (int)) != DDI_SUCCESS)
378*0Sstevel@tonic-gate 			error = EFAULT;
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate 		ddi_unmap_regs(dip, 0, &reg, uio->uio_offset, (off_t)msize);
381*0Sstevel@tonic-gate 	}
382*0Sstevel@tonic-gate 	return (error);
383*0Sstevel@tonic-gate }
384*0Sstevel@tonic-gate 
385*0Sstevel@tonic-gate static int
386*0Sstevel@tonic-gate sbmem_devmap(dev_t dev, devmap_cookie_t dhp, offset_t off, size_t len,
387*0Sstevel@tonic-gate 	size_t *maplen, uint_t model)
388*0Sstevel@tonic-gate {
389*0Sstevel@tonic-gate 	struct sbusmem_unit *un;
390*0Sstevel@tonic-gate 	int instance, error;
391*0Sstevel@tonic-gate 
392*0Sstevel@tonic-gate #if defined(lint) || defined(__lint)
393*0Sstevel@tonic-gate 	model = model;
394*0Sstevel@tonic-gate #endif /* lint || __lint */
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 	instance = getminor(dev);
397*0Sstevel@tonic-gate 	if ((un = ddi_get_soft_state(sbusmem_state_head, instance)) == NULL) {
398*0Sstevel@tonic-gate 		return (ENXIO);
399*0Sstevel@tonic-gate 	}
400*0Sstevel@tonic-gate 	if (off + len > un->size) {
401*0Sstevel@tonic-gate 		return (ENXIO);
402*0Sstevel@tonic-gate 	}
403*0Sstevel@tonic-gate 	if ((error = devmap_devmem_setup(dhp, un->dip, NULL, 0,
404*0Sstevel@tonic-gate 			off, len, PROT_ALL, DEVMAP_DEFAULTS, NULL)) < 0) {
405*0Sstevel@tonic-gate 		return (error);
406*0Sstevel@tonic-gate 	}
407*0Sstevel@tonic-gate 	*maplen = ptob(btopr(len));
408*0Sstevel@tonic-gate 	return (0);
409*0Sstevel@tonic-gate }
410