2 * Copyright (c) 2008 Nathan Whitehorn. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
30 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/kthread.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <geom/geom_disk.h>
42 #include <powerpc/mambo/mambocall.h>
44 struct mambodisk_softc {
49 struct bio_queue_head bio_queue;
54 #define MAMBO_DISK_READ 116
55 #define MAMBO_DISK_WRITE 117
56 #define MAMBO_DISK_INFO 118
58 #define MAMBO_INFO_STATUS 1
59 #define MAMBO_INFO_BLKSZ 2
60 #define MAMBO_INFO_DEVSZ 3
62 /* bus entry points */
63 static void mambodisk_identify(driver_t *driver, device_t parent);
64 static int mambodisk_probe(device_t dev);
65 static int mambodisk_attach(device_t dev);
68 static int mambodisk_open(struct disk *dp);
69 static int mambodisk_close(struct disk *dp);
70 static void mambodisk_strategy(struct bio *bp);
71 static void mambodisk_task(void *arg);
73 #define MBODISK_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
74 #define MBODISK_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
75 #define MBODISK_LOCK_INIT(_sc) \
76 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
78 #define MBODISK_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
79 #define MBODISK_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
80 #define MBODISK_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
83 mambodisk_identify(driver_t *driver, device_t parent)
87 for (i = 0; mambocall(MAMBO_DISK_INFO,MAMBO_INFO_DEVSZ,i) > 0; i++)
88 BUS_ADD_CHILD(parent,0,"mambodisk",i);
92 mambodisk_probe(device_t dev)
94 device_set_desc(dev, "Mambo Simulated Block Device");
99 mambodisk_attach(device_t dev)
101 struct mambodisk_softc *sc;
106 sc = device_get_softc(dev);
108 MBODISK_LOCK_INIT(sc);
110 d = sc->disk = disk_alloc();
111 d->d_open = mambodisk_open;
112 d->d_close = mambodisk_close;
113 d->d_strategy = mambodisk_strategy;
114 d->d_name = "mambodisk";
116 d->d_maxsize = MAXPHYS; /* Maybe ask bridge? */
118 d->d_sectorsize = 512;
119 sc->maxblocks = mambocall(MAMBO_DISK_INFO,MAMBO_INFO_BLKSZ,d->d_unit)
122 d->d_unit = device_get_unit(dev);
123 d->d_mediasize = mambocall(MAMBO_DISK_INFO,MAMBO_INFO_DEVSZ,d->d_unit)
124 * 1024ULL; /* Mambo gives size in KB */
126 mb = d->d_mediasize >> 20; /* 1MiB == 1 << 20 */
128 if (mb >= 10240) { /* 1GiB = 1024 MiB */
132 device_printf(dev, "%ju%cB, %d byte sectors\n", mb, unit,
134 disk_create(d, DISK_VERSION);
135 bioq_init(&sc->bio_queue);
138 kproc_create(&mambodisk_task, sc, &sc->p, 0, 0, "task: mambo hd");
144 mambodisk_detach(device_t dev)
146 struct mambodisk_softc *sc = device_get_softc(dev);
154 /* wait for thread to finish. XXX probably want timeout. -sorbo */
156 while (sc->running != -1)
157 msleep(sc, &sc->sc_mtx, PRIBIO, "detach", 0);
161 disk_destroy(sc->disk);
162 /* XXX destroy anything in queue */
164 MBODISK_LOCK_DESTROY(sc);
170 mambodisk_open(struct disk *dp)
176 mambodisk_close(struct disk *dp)
182 mambodisk_strategy(struct bio *bp)
184 struct mambodisk_softc *sc;
186 sc = (struct mambodisk_softc *)bp->bio_disk->d_drv1;
188 bioq_disksort(&sc->bio_queue, bp);
194 mambodisk_task(void *arg)
196 struct mambodisk_softc *sc = (struct mambodisk_softc*)arg;
205 unit = device_get_unit(dev);
207 while (sc->running) {
210 bp = bioq_first(&sc->bio_queue);
212 msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0);
213 } while (bp == NULL && sc->running);
215 bioq_remove(&sc->bio_queue, bp);
219 sz = sc->disk->d_sectorsize;
220 end = bp->bio_pblkno + (bp->bio_bcount / sz);
221 for (block = bp->bio_pblkno; block < end;) {
223 char *vaddr = bp->bio_data +
224 (block - bp->bio_pblkno) * sz;
226 numblocks = end - block;
227 if (numblocks > sc->maxblocks)
228 numblocks = sc->maxblocks;
230 if (bp->bio_cmd == BIO_READ) {
231 result = mambocall(MAMBO_DISK_READ, vaddr,
232 (u_long)block, (numblocks << 16) | unit);
233 } else if (bp->bio_cmd == BIO_WRITE) {
234 result = mambocall(MAMBO_DISK_WRITE, vaddr,
235 (u_long)block, (numblocks << 16) | unit);
247 bp->bio_resid = (end - block) * sz;
248 bp->bio_flags |= BIO_ERROR;
253 /* tell parent we're done */
262 static device_method_t mambodisk_methods[] = {
263 DEVMETHOD(device_identify, mambodisk_identify),
264 DEVMETHOD(device_probe, mambodisk_probe),
265 DEVMETHOD(device_attach, mambodisk_attach),
266 DEVMETHOD(device_detach, mambodisk_detach),
270 static driver_t mambodisk_driver = {
273 sizeof(struct mambodisk_softc),
275 static devclass_t mambodisk_devclass;
277 DRIVER_MODULE(mambodisk, mambo, mambodisk_driver, mambodisk_devclass, 0, 0);