1cf824886SRobert Watson /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni *
4f00e79c8SRobert Watson * Copyright (c) 2012-2013, 2016 Robert N. M. Watson
5cf824886SRobert Watson * All rights reserved.
6cf824886SRobert Watson *
7cf824886SRobert Watson * This software was developed by SRI International and the University of
8cf824886SRobert Watson * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
9cf824886SRobert Watson * ("CTSRD"), as part of the DARPA CRASH research programme.
10cf824886SRobert Watson *
11cf824886SRobert Watson * Redistribution and use in source and binary forms, with or without
12cf824886SRobert Watson * modification, are permitted provided that the following conditions
13cf824886SRobert Watson * are met:
14cf824886SRobert Watson * 1. Redistributions of source code must retain the above copyright
15cf824886SRobert Watson * notice, this list of conditions and the following disclaimer.
16cf824886SRobert Watson * 2. Redistributions in binary form must reproduce the above copyright
17cf824886SRobert Watson * notice, this list of conditions and the following disclaimer in the
18cf824886SRobert Watson * documentation and/or other materials provided with the distribution.
19cf824886SRobert Watson *
20cf824886SRobert Watson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21cf824886SRobert Watson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22cf824886SRobert Watson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23cf824886SRobert Watson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24cf824886SRobert Watson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25cf824886SRobert Watson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26cf824886SRobert Watson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27cf824886SRobert Watson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28cf824886SRobert Watson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29cf824886SRobert Watson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30cf824886SRobert Watson * SUCH DAMAGE.
31cf824886SRobert Watson */
32cf824886SRobert Watson
33cf824886SRobert Watson #include <sys/param.h>
34f00e79c8SRobert Watson #include <sys/bio.h>
35cf824886SRobert Watson #include <sys/bus.h>
36cf824886SRobert Watson #include <sys/condvar.h>
37cf824886SRobert Watson #include <sys/conf.h>
38cf824886SRobert Watson #include <sys/kernel.h>
39cf824886SRobert Watson #include <sys/lock.h>
40cf824886SRobert Watson #include <sys/malloc.h>
41cf824886SRobert Watson #include <sys/module.h>
42cf824886SRobert Watson #include <sys/mutex.h>
43cf824886SRobert Watson #include <sys/rman.h>
44cf824886SRobert Watson #include <sys/stat.h>
45cf824886SRobert Watson #include <sys/systm.h>
46cf824886SRobert Watson #include <sys/uio.h>
47cf824886SRobert Watson
48f00e79c8SRobert Watson #include <geom/geom_disk.h>
49f00e79c8SRobert Watson
50cf824886SRobert Watson #include <machine/bus.h>
51cf824886SRobert Watson #include <machine/resource.h>
52cf824886SRobert Watson
53cf824886SRobert Watson #include <vm/vm.h>
54cf824886SRobert Watson
55cf824886SRobert Watson #include <dev/altera/avgen/altera_avgen.h>
56cf824886SRobert Watson
57cf824886SRobert Watson /*
58cf824886SRobert Watson * Generic device driver for allowing read(), write(), and mmap() on
59cf824886SRobert Watson * memory-mapped, Avalon-attached devices. There is no actual dependence on
60cf824886SRobert Watson * Avalon, so conceivably this should just be soc_dev or similar, since many
61cf824886SRobert Watson * system-on-chip bus environments would work fine with the same code.
62cf824886SRobert Watson */
63cf824886SRobert Watson
64cf824886SRobert Watson static d_mmap_t altera_avgen_mmap;
65cf824886SRobert Watson static d_read_t altera_avgen_read;
66cf824886SRobert Watson static d_write_t altera_avgen_write;
67cf824886SRobert Watson
68f00e79c8SRobert Watson #define ALTERA_AVGEN_DEVNAME "altera_avgen"
69f00e79c8SRobert Watson #define ALTERA_AVGEN_DEVNAME_FMT (ALTERA_AVGEN_DEVNAME "%d")
70f00e79c8SRobert Watson
71cf824886SRobert Watson static struct cdevsw avg_cdevsw = {
72cf824886SRobert Watson .d_version = D_VERSION,
73cf824886SRobert Watson .d_mmap = altera_avgen_mmap,
74cf824886SRobert Watson .d_read = altera_avgen_read,
75cf824886SRobert Watson .d_write = altera_avgen_write,
76f00e79c8SRobert Watson .d_name = ALTERA_AVGEN_DEVNAME,
77cf824886SRobert Watson };
78cf824886SRobert Watson
79f00e79c8SRobert Watson #define ALTERA_AVGEN_SECTORSIZE 512 /* Not configurable at this time. */
80f00e79c8SRobert Watson
81cf824886SRobert Watson static int
altera_avgen_read(struct cdev * dev,struct uio * uio,int flag)82cf824886SRobert Watson altera_avgen_read(struct cdev *dev, struct uio *uio, int flag)
83cf824886SRobert Watson {
84cf824886SRobert Watson struct altera_avgen_softc *sc;
85cf824886SRobert Watson u_long offset, size;
86cf824886SRobert Watson #ifdef NOTYET
87cf824886SRobert Watson uint64_t v8;
88cf824886SRobert Watson #endif
89cf824886SRobert Watson uint32_t v4;
90cf824886SRobert Watson uint16_t v2;
91cf824886SRobert Watson uint8_t v1;
92cf824886SRobert Watson u_int width;
93cf824886SRobert Watson int error;
94cf824886SRobert Watson
95cf824886SRobert Watson sc = dev->si_drv1;
96cf824886SRobert Watson if ((sc->avg_flags & ALTERA_AVALON_FLAG_READ) == 0)
97cf824886SRobert Watson return (EACCES);
98cf824886SRobert Watson width = sc->avg_width;
99cf824886SRobert Watson if (uio->uio_offset < 0 || uio->uio_offset % width != 0 ||
100cf824886SRobert Watson uio->uio_resid % width != 0)
101cf824886SRobert Watson return (ENODEV);
102cf824886SRobert Watson size = rman_get_size(sc->avg_res);
103cf824886SRobert Watson if ((uio->uio_offset + uio->uio_resid < 0) ||
104cf824886SRobert Watson (uio->uio_offset + uio->uio_resid > size))
105cf824886SRobert Watson return (ENODEV);
106cf824886SRobert Watson while (uio->uio_resid > 0) {
107cf824886SRobert Watson offset = uio->uio_offset;
108cf824886SRobert Watson if (offset + width > size)
109cf824886SRobert Watson return (ENODEV);
110cf824886SRobert Watson switch (width) {
111cf824886SRobert Watson case 1:
112cf824886SRobert Watson v1 = bus_read_1(sc->avg_res, offset);
113cf824886SRobert Watson error = uiomove(&v1, sizeof(v1), uio);
114cf824886SRobert Watson break;
115cf824886SRobert Watson
116cf824886SRobert Watson case 2:
117cf824886SRobert Watson v2 = bus_read_2(sc->avg_res, offset);
118cf824886SRobert Watson error = uiomove(&v2, sizeof(v2), uio);
119cf824886SRobert Watson break;
120cf824886SRobert Watson
121cf824886SRobert Watson case 4:
122cf824886SRobert Watson v4 = bus_read_4(sc->avg_res, offset);
123cf824886SRobert Watson error = uiomove(&v4, sizeof(v4), uio);
124cf824886SRobert Watson break;
125cf824886SRobert Watson
126cf824886SRobert Watson #ifdef NOTYET
127cf824886SRobert Watson case 8:
128cf824886SRobert Watson v8 = bus_read_8(sc->avg_res, offset);
129cf824886SRobert Watson error = uiomove(&v8, sizeof(v8), uio);
130cf824886SRobert Watson break;
131cf824886SRobert Watson
132cf824886SRobert Watson #endif
133cf824886SRobert Watson
134cf824886SRobert Watson default:
135cf824886SRobert Watson panic("%s: unexpected widthment %u", __func__, width);
136cf824886SRobert Watson }
137cf824886SRobert Watson if (error)
138cf824886SRobert Watson return (error);
139cf824886SRobert Watson }
140cf824886SRobert Watson return (0);
141cf824886SRobert Watson }
142cf824886SRobert Watson
143cf824886SRobert Watson static int
altera_avgen_write(struct cdev * dev,struct uio * uio,int flag)144cf824886SRobert Watson altera_avgen_write(struct cdev *dev, struct uio *uio, int flag)
145cf824886SRobert Watson {
146cf824886SRobert Watson struct altera_avgen_softc *sc;
147cf824886SRobert Watson u_long offset, size;
148cf824886SRobert Watson #ifdef NOTYET
149cf824886SRobert Watson uint64_t v8;
150cf824886SRobert Watson #endif
151cf824886SRobert Watson uint32_t v4;
152cf824886SRobert Watson uint16_t v2;
153cf824886SRobert Watson uint8_t v1;
154cf824886SRobert Watson u_int width;
155cf824886SRobert Watson int error;
156cf824886SRobert Watson
157cf824886SRobert Watson sc = dev->si_drv1;
158cf824886SRobert Watson if ((sc->avg_flags & ALTERA_AVALON_FLAG_WRITE) == 0)
159cf824886SRobert Watson return (EACCES);
160cf824886SRobert Watson width = sc->avg_width;
161cf824886SRobert Watson if (uio->uio_offset < 0 || uio->uio_offset % width != 0 ||
162cf824886SRobert Watson uio->uio_resid % width != 0)
163cf824886SRobert Watson return (ENODEV);
164cf824886SRobert Watson size = rman_get_size(sc->avg_res);
165cf824886SRobert Watson while (uio->uio_resid > 0) {
166cf824886SRobert Watson offset = uio->uio_offset;
167cf824886SRobert Watson if (offset + width > size)
168cf824886SRobert Watson return (ENODEV);
169cf824886SRobert Watson switch (width) {
170cf824886SRobert Watson case 1:
171cf824886SRobert Watson error = uiomove(&v1, sizeof(v1), uio);
172cf824886SRobert Watson if (error)
173cf824886SRobert Watson return (error);
174cf824886SRobert Watson bus_write_1(sc->avg_res, offset, v1);
175cf824886SRobert Watson break;
176cf824886SRobert Watson
177cf824886SRobert Watson case 2:
178cf824886SRobert Watson error = uiomove(&v2, sizeof(v2), uio);
179cf824886SRobert Watson if (error)
180cf824886SRobert Watson return (error);
181cf824886SRobert Watson bus_write_2(sc->avg_res, offset, v2);
182cf824886SRobert Watson break;
183cf824886SRobert Watson
184cf824886SRobert Watson case 4:
185cf824886SRobert Watson error = uiomove(&v4, sizeof(v4), uio);
186cf824886SRobert Watson if (error)
187cf824886SRobert Watson return (error);
188cf824886SRobert Watson bus_write_4(sc->avg_res, offset, v4);
189cf824886SRobert Watson break;
190cf824886SRobert Watson
191cf824886SRobert Watson #ifdef NOTYET
192cf824886SRobert Watson case 8:
193cf824886SRobert Watson error = uiomove(&v8, sizeof(v8), uio);
194cf824886SRobert Watson if (error)
195cf824886SRobert Watson return (error);
196cf824886SRobert Watson bus_write_8(sc->avg_res, offset, v8);
197cf824886SRobert Watson break;
198cf824886SRobert Watson #endif
199cf824886SRobert Watson
200cf824886SRobert Watson default:
201cf824886SRobert Watson panic("%s: unexpected width %u", __func__, width);
202cf824886SRobert Watson }
203cf824886SRobert Watson }
204cf824886SRobert Watson return (0);
205cf824886SRobert Watson }
206cf824886SRobert Watson
207cf824886SRobert Watson static int
altera_avgen_mmap(struct cdev * dev,vm_ooffset_t offset,vm_paddr_t * paddr,int nprot,vm_memattr_t * memattr)208cf824886SRobert Watson altera_avgen_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
209cf824886SRobert Watson int nprot, vm_memattr_t *memattr)
210cf824886SRobert Watson {
211cf824886SRobert Watson struct altera_avgen_softc *sc;
212cf824886SRobert Watson
213cf824886SRobert Watson sc = dev->si_drv1;
214cf824886SRobert Watson if (nprot & VM_PROT_READ) {
215cf824886SRobert Watson if ((sc->avg_flags & ALTERA_AVALON_FLAG_MMAP_READ) == 0)
216cf824886SRobert Watson return (EACCES);
217cf824886SRobert Watson }
218cf824886SRobert Watson if (nprot & VM_PROT_WRITE) {
219cf824886SRobert Watson if ((sc->avg_flags & ALTERA_AVALON_FLAG_MMAP_WRITE) == 0)
220cf824886SRobert Watson return (EACCES);
221cf824886SRobert Watson }
222cf824886SRobert Watson if (nprot & VM_PROT_EXECUTE) {
223cf824886SRobert Watson if ((sc->avg_flags & ALTERA_AVALON_FLAG_MMAP_EXEC) == 0)
224cf824886SRobert Watson return (EACCES);
225cf824886SRobert Watson }
226cf824886SRobert Watson if (trunc_page(offset) == offset &&
227b8c20c02SKonstantin Belousov offset + PAGE_SIZE > offset &&
228cf824886SRobert Watson rman_get_size(sc->avg_res) >= offset + PAGE_SIZE) {
229cf824886SRobert Watson *paddr = rman_get_start(sc->avg_res) + offset;
230cf824886SRobert Watson *memattr = VM_MEMATTR_UNCACHEABLE;
231cf824886SRobert Watson } else
232cf824886SRobert Watson return (ENODEV);
233cf824886SRobert Watson return (0);
234cf824886SRobert Watson }
235cf824886SRobert Watson
236f00e79c8SRobert Watson /*
237f00e79c8SRobert Watson * NB: We serialise block reads and writes in case the OS is generating
238f00e79c8SRobert Watson * concurrent I/O against the same block, in which case we want one I/O (or
239f00e79c8SRobert Watson * another) to win. This is not sufficient to provide atomicity for the
240f00e79c8SRobert Watson * sector in the presence of a fail stop -- however, we're just writing this
241f00e79c8SRobert Watson * to non-persistent DRAM .. right?
242f00e79c8SRobert Watson */
243f00e79c8SRobert Watson static void
altera_avgen_disk_strategy(struct bio * bp)244f00e79c8SRobert Watson altera_avgen_disk_strategy(struct bio *bp)
245f00e79c8SRobert Watson {
246f00e79c8SRobert Watson struct altera_avgen_softc *sc;
247f00e79c8SRobert Watson void *data;
248f00e79c8SRobert Watson long bcount;
249f00e79c8SRobert Watson daddr_t pblkno;
250d176b803SScott Long int error;
251f00e79c8SRobert Watson
252f00e79c8SRobert Watson sc = bp->bio_disk->d_drv1;
253f00e79c8SRobert Watson data = bp->bio_data;
254f00e79c8SRobert Watson bcount = bp->bio_bcount;
255f00e79c8SRobert Watson pblkno = bp->bio_pblkno;
256d176b803SScott Long error = 0;
257f00e79c8SRobert Watson
258f00e79c8SRobert Watson /*
259f00e79c8SRobert Watson * Serialize block reads / writes.
260f00e79c8SRobert Watson */
261f00e79c8SRobert Watson mtx_lock(&sc->avg_disk_mtx);
262f00e79c8SRobert Watson switch (bp->bio_cmd) {
263f00e79c8SRobert Watson case BIO_READ:
264f00e79c8SRobert Watson if (!(sc->avg_flags & ALTERA_AVALON_FLAG_GEOM_READ)) {
265d176b803SScott Long error = EROFS;
266f00e79c8SRobert Watson break;
267f00e79c8SRobert Watson }
268f00e79c8SRobert Watson switch (sc->avg_width) {
269f00e79c8SRobert Watson case 1:
270f00e79c8SRobert Watson bus_read_region_1(sc->avg_res,
271f00e79c8SRobert Watson bp->bio_pblkno * ALTERA_AVGEN_SECTORSIZE,
272f00e79c8SRobert Watson (uint8_t *)data, bcount);
273f00e79c8SRobert Watson break;
274f00e79c8SRobert Watson
275f00e79c8SRobert Watson case 2:
276f00e79c8SRobert Watson bus_read_region_2(sc->avg_res,
277f00e79c8SRobert Watson bp->bio_pblkno * ALTERA_AVGEN_SECTORSIZE,
278f00e79c8SRobert Watson (uint16_t *)data, bcount / 2);
279f00e79c8SRobert Watson break;
280f00e79c8SRobert Watson
281f00e79c8SRobert Watson case 4:
282f00e79c8SRobert Watson bus_read_region_4(sc->avg_res,
283f00e79c8SRobert Watson bp->bio_pblkno * ALTERA_AVGEN_SECTORSIZE,
284f00e79c8SRobert Watson (uint32_t *)data, bcount / 4);
285f00e79c8SRobert Watson break;
286f00e79c8SRobert Watson
287f00e79c8SRobert Watson default:
288f00e79c8SRobert Watson panic("%s: unexpected width %u", __func__,
289f00e79c8SRobert Watson sc->avg_width);
290f00e79c8SRobert Watson }
291f00e79c8SRobert Watson break;
292f00e79c8SRobert Watson
293f00e79c8SRobert Watson case BIO_WRITE:
294f00e79c8SRobert Watson if (!(sc->avg_flags & ALTERA_AVALON_FLAG_GEOM_WRITE)) {
295f00e79c8SRobert Watson biofinish(bp, NULL, EROFS);
296f00e79c8SRobert Watson break;
297f00e79c8SRobert Watson }
298f00e79c8SRobert Watson switch (sc->avg_width) {
299f00e79c8SRobert Watson case 1:
300f00e79c8SRobert Watson bus_write_region_1(sc->avg_res,
301f00e79c8SRobert Watson bp->bio_pblkno * ALTERA_AVGEN_SECTORSIZE,
302f00e79c8SRobert Watson (uint8_t *)data, bcount);
303f00e79c8SRobert Watson break;
304f00e79c8SRobert Watson
305f00e79c8SRobert Watson case 2:
306f00e79c8SRobert Watson bus_write_region_2(sc->avg_res,
307f00e79c8SRobert Watson bp->bio_pblkno * ALTERA_AVGEN_SECTORSIZE,
308f00e79c8SRobert Watson (uint16_t *)data, bcount / 2);
309f00e79c8SRobert Watson break;
310f00e79c8SRobert Watson
311f00e79c8SRobert Watson case 4:
312f00e79c8SRobert Watson bus_write_region_4(sc->avg_res,
313f00e79c8SRobert Watson bp->bio_pblkno * ALTERA_AVGEN_SECTORSIZE,
314f00e79c8SRobert Watson (uint32_t *)data, bcount / 4);
315f00e79c8SRobert Watson break;
316f00e79c8SRobert Watson
317f00e79c8SRobert Watson default:
318f00e79c8SRobert Watson panic("%s: unexpected width %u", __func__,
319f00e79c8SRobert Watson sc->avg_width);
320f00e79c8SRobert Watson }
321f00e79c8SRobert Watson break;
322f00e79c8SRobert Watson
323f00e79c8SRobert Watson default:
324d176b803SScott Long error = EOPNOTSUPP;
325d176b803SScott Long break;
326f00e79c8SRobert Watson }
327f00e79c8SRobert Watson mtx_unlock(&sc->avg_disk_mtx);
328d176b803SScott Long biofinish(bp, NULL, error);
329f00e79c8SRobert Watson }
330cf824886SRobert Watson
331cf824886SRobert Watson static int
altera_avgen_process_options(struct altera_avgen_softc * sc,const char * str_fileio,const char * str_geomio,const char * str_mmapio,const char * str_devname,int devunit)332cf824886SRobert Watson altera_avgen_process_options(struct altera_avgen_softc *sc,
333f00e79c8SRobert Watson const char *str_fileio, const char *str_geomio, const char *str_mmapio,
334f00e79c8SRobert Watson const char *str_devname, int devunit)
335cf824886SRobert Watson {
336cf824886SRobert Watson const char *cp;
337cf824886SRobert Watson device_t dev = sc->avg_dev;
338cf824886SRobert Watson
339cf824886SRobert Watson /*
340cf824886SRobert Watson * Check for valid combinations of options.
341cf824886SRobert Watson */
342f00e79c8SRobert Watson if (str_fileio == NULL && str_geomio == NULL && str_mmapio == NULL) {
343cf824886SRobert Watson device_printf(dev,
344f00e79c8SRobert Watson "at least one of %s, %s, or %s must be specified\n",
345f00e79c8SRobert Watson ALTERA_AVALON_STR_FILEIO, ALTERA_AVALON_STR_GEOMIO,
346f00e79c8SRobert Watson ALTERA_AVALON_STR_MMAPIO);
347cf824886SRobert Watson return (ENXIO);
348cf824886SRobert Watson }
349f00e79c8SRobert Watson
350f00e79c8SRobert Watson /*
351f00e79c8SRobert Watson * Validity check: a device can either be a GEOM device (in which case
352f00e79c8SRobert Watson * we use GEOM to register the device node), or a special device --
353f00e79c8SRobert Watson * but not both as that causes a collision in /dev.
354f00e79c8SRobert Watson */
355f00e79c8SRobert Watson if (str_geomio != NULL && (str_fileio != NULL || str_mmapio != NULL)) {
356f00e79c8SRobert Watson device_printf(dev,
357f00e79c8SRobert Watson "at most one of %s and (%s or %s) may be specified\n",
358f00e79c8SRobert Watson ALTERA_AVALON_STR_GEOMIO, ALTERA_AVALON_STR_FILEIO,
359f00e79c8SRobert Watson ALTERA_AVALON_STR_MMAPIO);
360f00e79c8SRobert Watson return (ENXIO);
361f00e79c8SRobert Watson }
362f00e79c8SRobert Watson
363f00e79c8SRobert Watson /*
364f00e79c8SRobert Watson * Ensure that a unit is specified if a name is also specified.
365f00e79c8SRobert Watson */
366cf824886SRobert Watson if (str_devname == NULL && devunit != -1) {
367cf824886SRobert Watson device_printf(dev, "%s requires %s be specified\n",
368cf824886SRobert Watson ALTERA_AVALON_STR_DEVUNIT, ALTERA_AVALON_STR_DEVNAME);
369cf824886SRobert Watson return (ENXIO);
370cf824886SRobert Watson }
371cf824886SRobert Watson
372cf824886SRobert Watson /*
373cf824886SRobert Watson * Extract, digest, and save values.
374cf824886SRobert Watson */
375cf824886SRobert Watson switch (sc->avg_width) {
376cf824886SRobert Watson case 1:
377cf824886SRobert Watson case 2:
378cf824886SRobert Watson case 4:
379cf824886SRobert Watson #ifdef NOTYET
380cf824886SRobert Watson case 8:
381cf824886SRobert Watson #endif
382cf824886SRobert Watson break;
383cf824886SRobert Watson
384cf824886SRobert Watson default:
385cf824886SRobert Watson device_printf(dev, "%s unsupported value %u\n",
386cf824886SRobert Watson ALTERA_AVALON_STR_WIDTH, sc->avg_width);
387cf824886SRobert Watson return (ENXIO);
388cf824886SRobert Watson }
389cf824886SRobert Watson sc->avg_flags = 0;
390cf824886SRobert Watson if (str_fileio != NULL) {
391cf824886SRobert Watson for (cp = str_fileio; *cp != '\0'; cp++) {
392cf824886SRobert Watson switch (*cp) {
393cf824886SRobert Watson case ALTERA_AVALON_CHAR_READ:
394cf824886SRobert Watson sc->avg_flags |= ALTERA_AVALON_FLAG_READ;
395cf824886SRobert Watson break;
396cf824886SRobert Watson
397cf824886SRobert Watson case ALTERA_AVALON_CHAR_WRITE:
398cf824886SRobert Watson sc->avg_flags |= ALTERA_AVALON_FLAG_WRITE;
399cf824886SRobert Watson break;
400cf824886SRobert Watson
401cf824886SRobert Watson default:
402cf824886SRobert Watson device_printf(dev,
403cf824886SRobert Watson "invalid %s character %c\n",
404cf824886SRobert Watson ALTERA_AVALON_STR_FILEIO, *cp);
405cf824886SRobert Watson return (ENXIO);
406cf824886SRobert Watson }
407cf824886SRobert Watson }
408cf824886SRobert Watson }
409f00e79c8SRobert Watson if (str_geomio != NULL) {
410f00e79c8SRobert Watson for (cp = str_geomio; *cp != '\0'; cp++){
411f00e79c8SRobert Watson switch (*cp) {
412f00e79c8SRobert Watson case ALTERA_AVALON_CHAR_READ:
413f00e79c8SRobert Watson sc->avg_flags |= ALTERA_AVALON_FLAG_GEOM_READ;
414f00e79c8SRobert Watson break;
415f00e79c8SRobert Watson
416f00e79c8SRobert Watson case ALTERA_AVALON_CHAR_WRITE:
417f00e79c8SRobert Watson sc->avg_flags |= ALTERA_AVALON_FLAG_GEOM_WRITE;
418f00e79c8SRobert Watson break;
419f00e79c8SRobert Watson
420f00e79c8SRobert Watson default:
421f00e79c8SRobert Watson device_printf(dev,
422f00e79c8SRobert Watson "invalid %s character %c\n",
423f00e79c8SRobert Watson ALTERA_AVALON_STR_GEOMIO, *cp);
424f00e79c8SRobert Watson return (ENXIO);
425f00e79c8SRobert Watson }
426f00e79c8SRobert Watson }
427f00e79c8SRobert Watson }
428cf824886SRobert Watson if (str_mmapio != NULL) {
429cf824886SRobert Watson for (cp = str_mmapio; *cp != '\0'; cp++) {
430cf824886SRobert Watson switch (*cp) {
431cf824886SRobert Watson case ALTERA_AVALON_CHAR_READ:
432cf824886SRobert Watson sc->avg_flags |= ALTERA_AVALON_FLAG_MMAP_READ;
433cf824886SRobert Watson break;
434cf824886SRobert Watson
435cf824886SRobert Watson case ALTERA_AVALON_CHAR_WRITE:
436cf824886SRobert Watson sc->avg_flags |=
437cf824886SRobert Watson ALTERA_AVALON_FLAG_MMAP_WRITE;
438cf824886SRobert Watson break;
439cf824886SRobert Watson
440cf824886SRobert Watson case ALTERA_AVALON_CHAR_EXEC:
441cf824886SRobert Watson sc->avg_flags |= ALTERA_AVALON_FLAG_MMAP_EXEC;
442cf824886SRobert Watson break;
443cf824886SRobert Watson
444cf824886SRobert Watson default:
445cf824886SRobert Watson device_printf(dev,
446cf824886SRobert Watson "invalid %s character %c\n",
447cf824886SRobert Watson ALTERA_AVALON_STR_MMAPIO, *cp);
448cf824886SRobert Watson return (ENXIO);
449cf824886SRobert Watson }
450cf824886SRobert Watson }
451cf824886SRobert Watson }
452cf824886SRobert Watson return (0);
453cf824886SRobert Watson }
454cf824886SRobert Watson
455b364a525SRobert Watson int
altera_avgen_attach(struct altera_avgen_softc * sc,const char * str_fileio,const char * str_geomio,const char * str_mmapio,const char * str_devname,int devunit)456b364a525SRobert Watson altera_avgen_attach(struct altera_avgen_softc *sc, const char *str_fileio,
457f00e79c8SRobert Watson const char *str_geomio, const char *str_mmapio, const char *str_devname,
458f00e79c8SRobert Watson int devunit)
459cf824886SRobert Watson {
460b364a525SRobert Watson device_t dev = sc->avg_dev;
461b364a525SRobert Watson int error;
462cf824886SRobert Watson
463f00e79c8SRobert Watson error = altera_avgen_process_options(sc, str_fileio, str_geomio,
464f00e79c8SRobert Watson str_mmapio, str_devname, devunit);
465cf824886SRobert Watson if (error)
466cf824886SRobert Watson return (error);
467cf824886SRobert Watson
468cf824886SRobert Watson if (rman_get_size(sc->avg_res) >= PAGE_SIZE || str_mmapio != NULL) {
469cf824886SRobert Watson if (rman_get_size(sc->avg_res) % PAGE_SIZE != 0) {
470cf824886SRobert Watson device_printf(dev,
471cf824886SRobert Watson "memory region not even multiple of page size\n");
472b364a525SRobert Watson return (ENXIO);
473cf824886SRobert Watson }
474cf824886SRobert Watson if (rman_get_start(sc->avg_res) % PAGE_SIZE != 0) {
475cf824886SRobert Watson device_printf(dev, "memory region not page-aligned\n");
476b364a525SRobert Watson return (ENXIO);
477cf824886SRobert Watson }
478cf824886SRobert Watson }
479cf824886SRobert Watson
480f00e79c8SRobert Watson /*
481f00e79c8SRobert Watson * If a GEOM permission is requested, then create the device via GEOM.
482f00e79c8SRobert Watson * Otherwise, create a special device. We checked during options
483f00e79c8SRobert Watson * processing that both weren't requested a once.
484f00e79c8SRobert Watson */
485f00e79c8SRobert Watson if (str_devname != NULL) {
486f00e79c8SRobert Watson sc->avg_name = strdup(str_devname, M_TEMP);
487f00e79c8SRobert Watson devunit = sc->avg_unit;
488f00e79c8SRobert Watson } else
489f00e79c8SRobert Watson sc->avg_name = strdup(ALTERA_AVGEN_DEVNAME, M_TEMP);
490f00e79c8SRobert Watson if (sc->avg_flags & (ALTERA_AVALON_FLAG_GEOM_READ |
491f00e79c8SRobert Watson ALTERA_AVALON_FLAG_GEOM_WRITE)) {
492f00e79c8SRobert Watson mtx_init(&sc->avg_disk_mtx, "altera_avgen_disk", NULL,
493f00e79c8SRobert Watson MTX_DEF);
494f00e79c8SRobert Watson sc->avg_disk = disk_alloc();
495f00e79c8SRobert Watson sc->avg_disk->d_drv1 = sc;
496f00e79c8SRobert Watson sc->avg_disk->d_strategy = altera_avgen_disk_strategy;
497f00e79c8SRobert Watson if (devunit == -1)
498f00e79c8SRobert Watson devunit = 0;
499f00e79c8SRobert Watson sc->avg_disk->d_name = sc->avg_name;
500f00e79c8SRobert Watson sc->avg_disk->d_unit = devunit;
501f00e79c8SRobert Watson
502f00e79c8SRobert Watson /*
503f00e79c8SRobert Watson * NB: As avg_res is a multiple of PAGE_SIZE, it is also a
504f00e79c8SRobert Watson * multiple of ALTERA_AVGEN_SECTORSIZE.
505f00e79c8SRobert Watson */
506f00e79c8SRobert Watson sc->avg_disk->d_sectorsize = ALTERA_AVGEN_SECTORSIZE;
507f00e79c8SRobert Watson sc->avg_disk->d_mediasize = rman_get_size(sc->avg_res);
508f00e79c8SRobert Watson sc->avg_disk->d_maxsize = ALTERA_AVGEN_SECTORSIZE;
509f00e79c8SRobert Watson disk_create(sc->avg_disk, DISK_VERSION);
510f00e79c8SRobert Watson } else {
511cf824886SRobert Watson /* Device node allocation. */
512cf824886SRobert Watson if (str_devname == NULL) {
513f00e79c8SRobert Watson str_devname = ALTERA_AVGEN_DEVNAME_FMT;
514cf824886SRobert Watson devunit = sc->avg_unit;
515cf824886SRobert Watson }
516cf824886SRobert Watson if (devunit != -1)
517f00e79c8SRobert Watson sc->avg_cdev = make_dev(&avg_cdevsw, sc->avg_unit,
518f00e79c8SRobert Watson UID_ROOT, GID_WHEEL, S_IRUSR | S_IWUSR, "%s%d",
519f00e79c8SRobert Watson str_devname, devunit);
520cf824886SRobert Watson else
521f00e79c8SRobert Watson sc->avg_cdev = make_dev(&avg_cdevsw, sc->avg_unit,
522f00e79c8SRobert Watson UID_ROOT, GID_WHEEL, S_IRUSR | S_IWUSR,
523f00e79c8SRobert Watson "%s", str_devname);
524cf824886SRobert Watson if (sc->avg_cdev == NULL) {
525f00e79c8SRobert Watson device_printf(sc->avg_dev, "%s: make_dev failed\n",
526f00e79c8SRobert Watson __func__);
527b364a525SRobert Watson return (ENXIO);
528cf824886SRobert Watson }
529f00e79c8SRobert Watson
530cf824886SRobert Watson /* XXXRW: Slight race between make_dev(9) and here. */
531cf824886SRobert Watson sc->avg_cdev->si_drv1 = sc;
532f00e79c8SRobert Watson }
533cf824886SRobert Watson return (0);
534cf824886SRobert Watson }
535cf824886SRobert Watson
536b364a525SRobert Watson void
altera_avgen_detach(struct altera_avgen_softc * sc)537b364a525SRobert Watson altera_avgen_detach(struct altera_avgen_softc *sc)
538cf824886SRobert Watson {
539cf824886SRobert Watson
540f00e79c8SRobert Watson KASSERT((sc->avg_disk != NULL) || (sc->avg_cdev != NULL),
541f00e79c8SRobert Watson ("%s: neither GEOM nor special device", __func__));
542f00e79c8SRobert Watson
543f00e79c8SRobert Watson if (sc->avg_disk != NULL) {
544f00e79c8SRobert Watson disk_gone(sc->avg_disk);
545f00e79c8SRobert Watson disk_destroy(sc->avg_disk);
546f00e79c8SRobert Watson free(sc->avg_name, M_TEMP);
547f00e79c8SRobert Watson mtx_destroy(&sc->avg_disk_mtx);
548f00e79c8SRobert Watson } else {
549cf824886SRobert Watson destroy_dev(sc->avg_cdev);
550cf824886SRobert Watson }
551f00e79c8SRobert Watson }
552