2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008-2012 Juli Mallett <jmallett@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/mutex.h>
45 #include <geom/geom.h>
47 #include <machine/cpuregs.h>
49 #include <dev/gxemul/disk/gxemul_diskreg.h>
51 struct gxemul_disk_softc {
54 struct g_geom *sc_geom;
55 struct g_provider *sc_provider;
58 static struct mtx gxemul_disk_controller_mutex;
60 static g_start_t gxemul_disk_start;
61 static g_access_t gxemul_disk_access;
63 struct g_class g_gxemul_disk_class = {
66 .start = gxemul_disk_start,
67 .access = gxemul_disk_access,
70 DECLARE_GEOM_CLASS(g_gxemul_disk_class, g_gxemul_disk);
72 static void gxemul_disk_identify(driver_t *, device_t);
73 static int gxemul_disk_probe(device_t);
74 static int gxemul_disk_attach(device_t);
75 static void gxemul_disk_attach_geom(void *, int);
77 static int gxemul_disk_read(unsigned, void *, off_t);
78 static int gxemul_disk_size(unsigned, uint64_t *);
79 static int gxemul_disk_write(unsigned, const void *, off_t);
82 gxemul_disk_start(struct bio *bp)
84 struct gxemul_disk_softc *sc;
90 sc = bp->bio_to->geom->softc;
91 diskid = device_get_unit(sc->sc_dev);
93 if ((bp->bio_length % GXEMUL_DISK_DEV_BLOCKSIZE) != 0) {
94 g_io_deliver(bp, EINVAL);
99 offset = bp->bio_offset;
100 bp->bio_resid = bp->bio_length;
101 while (bp->bio_resid != 0) {
102 switch (bp->bio_cmd) {
104 mtx_lock(&gxemul_disk_controller_mutex);
105 error = gxemul_disk_read(diskid, buf, offset);
106 mtx_unlock(&gxemul_disk_controller_mutex);
109 mtx_lock(&gxemul_disk_controller_mutex);
110 error = gxemul_disk_write(diskid, buf, offset);
111 mtx_unlock(&gxemul_disk_controller_mutex);
114 g_io_deliver(bp, EOPNOTSUPP);
118 g_io_deliver(bp, error);
122 buf += GXEMUL_DISK_DEV_BLOCKSIZE;
123 offset += GXEMUL_DISK_DEV_BLOCKSIZE;
124 bp->bio_completed += GXEMUL_DISK_DEV_BLOCKSIZE;
125 bp->bio_resid -= GXEMUL_DISK_DEV_BLOCKSIZE;
132 gxemul_disk_access(struct g_provider *pp, int r, int w, int e)
138 gxemul_disk_identify(driver_t *drv, device_t parent)
142 mtx_init(&gxemul_disk_controller_mutex, "GXemul disk controller", NULL, MTX_DEF);
144 mtx_lock(&gxemul_disk_controller_mutex);
145 for (diskid = 0; diskid < 0x100; diskid++) {
147 * If we can read at offset 0, this disk id must be
148 * present enough. If we get an error, stop looking.
149 * Disks in GXemul are allocated linearly from 0.
151 if (gxemul_disk_read(diskid, NULL, 0) != 0)
153 BUS_ADD_CHILD(parent, 0, "gxemul_disk", diskid);
155 mtx_unlock(&gxemul_disk_controller_mutex);
159 gxemul_disk_probe(device_t dev)
161 device_set_desc(dev, "GXemul test disk");
163 return (BUS_PROBE_NOWILDCARD);
167 gxemul_disk_attach_geom(void *arg, int flag)
169 struct gxemul_disk_softc *sc;
173 sc->sc_geom = g_new_geomf(&g_gxemul_disk_class, "%s", device_get_nameunit(sc->sc_dev));
174 sc->sc_geom->softc = sc;
176 sc->sc_provider = g_new_providerf(sc->sc_geom, "%s", sc->sc_geom->name);
177 sc->sc_provider->sectorsize = GXEMUL_DISK_DEV_BLOCKSIZE;
178 sc->sc_provider->mediasize = sc->sc_size;
179 g_error_provider(sc->sc_provider, 0);
183 gxemul_disk_attach(device_t dev)
185 struct gxemul_disk_softc *sc;
189 diskid = device_get_unit(dev);
191 sc = device_get_softc(dev);
194 sc->sc_provider = NULL;
196 mtx_lock(&gxemul_disk_controller_mutex);
197 error = gxemul_disk_size(diskid, &sc->sc_size);
199 mtx_unlock(&gxemul_disk_controller_mutex);
202 mtx_unlock(&gxemul_disk_controller_mutex);
204 g_post_event(gxemul_disk_attach_geom, sc, M_WAITOK, NULL);
210 gxemul_disk_read(unsigned diskid, void *buf, off_t off)
212 const volatile void *src;
214 mtx_assert(&gxemul_disk_controller_mutex, MA_OWNED);
216 if (off < 0 || off % GXEMUL_DISK_DEV_BLOCKSIZE != 0)
220 GXEMUL_DISK_DEV_WRITE(GXEMUL_DISK_DEV_OFFSET, (uint64_t)off);
222 GXEMUL_DISK_DEV_WRITE(GXEMUL_DISK_DEV_OFFSET_LO,
223 (uint32_t)(off & 0xffffffff));
224 GXEMUL_DISK_DEV_WRITE(GXEMUL_DISK_DEV_OFFSET_HI,
225 (uint32_t)((off >> 32) & 0xffffffff));
227 GXEMUL_DISK_DEV_WRITE(GXEMUL_DISK_DEV_DISKID, diskid);
228 GXEMUL_DISK_DEV_WRITE(GXEMUL_DISK_DEV_START, GXEMUL_DISK_DEV_START_READ);
229 switch (GXEMUL_DISK_DEV_READ(GXEMUL_DISK_DEV_STATUS)) {
230 case GXEMUL_DISK_DEV_STATUS_FAILURE:
237 src = GXEMUL_DISK_DEV_FUNCTION(GXEMUL_DISK_DEV_BLOCK);
238 memcpy(buf, (const void *)(uintptr_t)src,
239 GXEMUL_DISK_DEV_BLOCKSIZE);
246 gxemul_disk_size(unsigned diskid, uint64_t *sizep)
248 uint64_t offset, ogood;
257 offset = (ogood * s) + (m * GXEMUL_DISK_DEV_BLOCKSIZE);
259 error = gxemul_disk_read(diskid, NULL, offset);
261 if (m == 1 && s == 1) {
262 *sizep = ogood + GXEMUL_DISK_DEV_BLOCKSIZE;
271 if (ogood == offset) {
283 gxemul_disk_write(unsigned diskid, const void *buf, off_t off)
287 mtx_assert(&gxemul_disk_controller_mutex, MA_OWNED);
289 if (off < 0 || off % GXEMUL_DISK_DEV_BLOCKSIZE != 0)
293 GXEMUL_DISK_DEV_WRITE(GXEMUL_DISK_DEV_OFFSET, (uint64_t)off);
295 GXEMUL_DISK_DEV_WRITE(GXEMUL_DISK_DEV_OFFSET_LO,
296 (uint32_t)(off & 0xffffffff));
297 GXEMUL_DISK_DEV_WRITE(GXEMUL_DISK_DEV_OFFSET_HI,
298 (uint32_t)((off >> 32) & 0xffffffff));
301 GXEMUL_DISK_DEV_WRITE(GXEMUL_DISK_DEV_DISKID, diskid);
303 dst = GXEMUL_DISK_DEV_FUNCTION(GXEMUL_DISK_DEV_BLOCK);
304 memcpy((void *)(uintptr_t)dst, buf, GXEMUL_DISK_DEV_BLOCKSIZE);
306 GXEMUL_DISK_DEV_WRITE(GXEMUL_DISK_DEV_START, GXEMUL_DISK_DEV_START_WRITE);
307 switch (GXEMUL_DISK_DEV_READ(GXEMUL_DISK_DEV_STATUS)) {
308 case GXEMUL_DISK_DEV_STATUS_FAILURE:
317 static device_method_t gxemul_disk_methods[] = {
318 DEVMETHOD(device_probe, gxemul_disk_probe),
319 DEVMETHOD(device_identify, gxemul_disk_identify),
320 DEVMETHOD(device_attach, gxemul_disk_attach),
325 static driver_t gxemul_disk_driver = {
328 sizeof (struct gxemul_disk_softc)
331 static devclass_t gxemul_disk_devclass;
332 DRIVER_MODULE(gxemul_disk, nexus, gxemul_disk_driver, gxemul_disk_devclass, 0, 0);