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