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