2 * Copyright (c) 2008-2012 Juli Mallett <jmallett@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
43 #include <geom/geom.h>
45 #include <machine/cpuregs.h>
47 #include <dev/gxemul/disk/gxemul_diskreg.h>
49 struct gxemul_disk_softc {
52 struct g_geom *sc_geom;
53 struct g_provider *sc_provider;
56 static struct mtx gxemul_disk_controller_mutex;
58 static g_start_t gxemul_disk_start;
59 static g_access_t gxemul_disk_access;
61 struct g_class g_gxemul_disk_class = {
64 .start = gxemul_disk_start,
65 .access = gxemul_disk_access,
68 DECLARE_GEOM_CLASS(g_gxemul_disk_class, g_gxemul_disk);
70 static void gxemul_disk_identify(driver_t *, device_t);
71 static int gxemul_disk_probe(device_t);
72 static int gxemul_disk_attach(device_t);
73 static void gxemul_disk_attach_geom(void *, int);
75 static int gxemul_disk_read(unsigned, void *, off_t);
76 static int gxemul_disk_size(unsigned, uint64_t *);
77 static int gxemul_disk_write(unsigned, const void *, off_t);
80 gxemul_disk_start(struct bio *bp)
82 struct gxemul_disk_softc *sc;
88 sc = bp->bio_to->geom->softc;
89 diskid = device_get_unit(sc->sc_dev);
91 if ((bp->bio_length % GXEMUL_DISK_DEV_BLOCKSIZE) != 0) {
92 g_io_deliver(bp, EINVAL);
97 offset = bp->bio_offset;
98 bp->bio_resid = bp->bio_length;
99 while (bp->bio_resid != 0) {
100 switch (bp->bio_cmd) {
102 mtx_lock(&gxemul_disk_controller_mutex);
103 error = gxemul_disk_read(diskid, buf, offset);
104 mtx_unlock(&gxemul_disk_controller_mutex);
107 mtx_lock(&gxemul_disk_controller_mutex);
108 error = gxemul_disk_write(diskid, buf, offset);
109 mtx_unlock(&gxemul_disk_controller_mutex);
112 g_io_deliver(bp, EOPNOTSUPP);
116 g_io_deliver(bp, error);
120 buf += GXEMUL_DISK_DEV_BLOCKSIZE;
121 offset += GXEMUL_DISK_DEV_BLOCKSIZE;
122 bp->bio_completed += GXEMUL_DISK_DEV_BLOCKSIZE;
123 bp->bio_resid -= GXEMUL_DISK_DEV_BLOCKSIZE;
130 gxemul_disk_access(struct g_provider *pp, int r, int w, int e)
136 gxemul_disk_identify(driver_t *drv, device_t parent)
140 mtx_init(&gxemul_disk_controller_mutex, "GXemul disk controller", NULL, MTX_DEF);
142 mtx_lock(&gxemul_disk_controller_mutex);
143 for (diskid = 0; diskid < 0x100; diskid++) {
145 * If we can read at offset 0, this disk id must be
146 * present enough. If we get an error, stop looking.
147 * Disks in GXemul are allocated linearly from 0.
149 if (gxemul_disk_read(diskid, NULL, 0) != 0)
151 BUS_ADD_CHILD(parent, 0, "gxemul_disk", diskid);
153 mtx_unlock(&gxemul_disk_controller_mutex);
157 gxemul_disk_probe(device_t dev)
159 device_set_desc(dev, "GXemul test disk");
165 gxemul_disk_attach_geom(void *arg, int flag)
167 struct gxemul_disk_softc *sc;
171 sc->sc_geom = g_new_geomf(&g_gxemul_disk_class, "%s", device_get_nameunit(sc->sc_dev));
172 sc->sc_geom->softc = sc;
174 sc->sc_provider = g_new_providerf(sc->sc_geom, sc->sc_geom->name);
175 sc->sc_provider->sectorsize = GXEMUL_DISK_DEV_BLOCKSIZE;
176 sc->sc_provider->mediasize = sc->sc_size;
177 g_error_provider(sc->sc_provider, 0);
181 gxemul_disk_attach(device_t dev)
183 struct gxemul_disk_softc *sc;
187 diskid = device_get_unit(dev);
189 sc = device_get_softc(dev);
192 sc->sc_provider = NULL;
194 mtx_lock(&gxemul_disk_controller_mutex);
195 error = gxemul_disk_size(diskid, &sc->sc_size);
197 mtx_unlock(&gxemul_disk_controller_mutex);
200 mtx_unlock(&gxemul_disk_controller_mutex);
202 g_post_event(gxemul_disk_attach_geom, sc, M_WAITOK, NULL);
208 gxemul_disk_read(unsigned diskid, void *buf, off_t off)
210 const volatile void *src;
212 mtx_assert(&gxemul_disk_controller_mutex, MA_OWNED);
214 if (off < 0 || off % GXEMUL_DISK_DEV_BLOCKSIZE != 0)
218 GXEMUL_DISK_DEV_WRITE(GXEMUL_DISK_DEV_OFFSET, (uint64_t)off);
220 GXEMUL_DISK_DEV_WRITE(GXEMUL_DISK_DEV_OFFSET_LO,
221 (uint32_t)(off & 0xffffffff));
222 GXEMUL_DISK_DEV_WRITE(GXEMUL_DISK_DEV_OFFSET_HI,
223 (uint32_t)((off >> 32) & 0xffffffff));
225 GXEMUL_DISK_DEV_WRITE(GXEMUL_DISK_DEV_DISKID, diskid);
226 GXEMUL_DISK_DEV_WRITE(GXEMUL_DISK_DEV_START, GXEMUL_DISK_DEV_START_READ);
227 switch (GXEMUL_DISK_DEV_READ(GXEMUL_DISK_DEV_STATUS)) {
228 case GXEMUL_DISK_DEV_STATUS_FAILURE:
235 src = GXEMUL_DISK_DEV_FUNCTION(GXEMUL_DISK_DEV_BLOCK);
236 memcpy(buf, (const void *)(uintptr_t)src,
237 GXEMUL_DISK_DEV_BLOCKSIZE);
244 gxemul_disk_size(unsigned diskid, uint64_t *sizep)
246 uint64_t offset, ogood;
255 offset = (ogood * s) + (m * GXEMUL_DISK_DEV_BLOCKSIZE);
257 error = gxemul_disk_read(diskid, NULL, offset);
259 if (m == 1 && s == 1) {
260 *sizep = ogood + GXEMUL_DISK_DEV_BLOCKSIZE;
269 if (ogood == offset) {
281 gxemul_disk_write(unsigned diskid, const void *buf, off_t off)
285 mtx_assert(&gxemul_disk_controller_mutex, MA_OWNED);
287 if (off < 0 || off % GXEMUL_DISK_DEV_BLOCKSIZE != 0)
291 GXEMUL_DISK_DEV_WRITE(GXEMUL_DISK_DEV_OFFSET, (uint64_t)off);
293 GXEMUL_DISK_DEV_WRITE(GXEMUL_DISK_DEV_OFFSET_LO,
294 (uint32_t)(off & 0xffffffff));
295 GXEMUL_DISK_DEV_WRITE(GXEMUL_DISK_DEV_OFFSET_HI,
296 (uint32_t)((off >> 32) & 0xffffffff));
299 GXEMUL_DISK_DEV_WRITE(GXEMUL_DISK_DEV_DISKID, diskid);
301 dst = GXEMUL_DISK_DEV_FUNCTION(GXEMUL_DISK_DEV_BLOCK);
302 memcpy((void *)(uintptr_t)dst, buf, GXEMUL_DISK_DEV_BLOCKSIZE);
304 GXEMUL_DISK_DEV_WRITE(GXEMUL_DISK_DEV_START, GXEMUL_DISK_DEV_START_WRITE);
305 switch (GXEMUL_DISK_DEV_READ(GXEMUL_DISK_DEV_STATUS)) {
306 case GXEMUL_DISK_DEV_STATUS_FAILURE:
315 static device_method_t gxemul_disk_methods[] = {
316 DEVMETHOD(device_probe, gxemul_disk_probe),
317 DEVMETHOD(device_identify, gxemul_disk_identify),
318 DEVMETHOD(device_attach, gxemul_disk_attach),
323 static driver_t gxemul_disk_driver = {
326 sizeof (struct gxemul_disk_softc)
329 static devclass_t gxemul_disk_devclass;
330 DRIVER_MODULE(gxemul_disk, nexus, gxemul_disk_driver, gxemul_disk_devclass, 0, 0);