2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008 Nathan Whitehorn. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/kthread.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <geom/geom_disk.h>
44 #include <powerpc/mambo/mambocall.h>
46 struct mambodisk_softc {
51 struct bio_queue_head bio_queue;
56 #define MAMBO_DISK_READ 116
57 #define MAMBO_DISK_WRITE 117
58 #define MAMBO_DISK_INFO 118
60 #define MAMBO_INFO_STATUS 1
61 #define MAMBO_INFO_BLKSZ 2
62 #define MAMBO_INFO_DEVSZ 3
64 /* bus entry points */
65 static void mambodisk_identify(driver_t *driver, device_t parent);
66 static int mambodisk_probe(device_t dev);
67 static int mambodisk_attach(device_t dev);
70 static int mambodisk_open(struct disk *dp);
71 static int mambodisk_close(struct disk *dp);
72 static void mambodisk_strategy(struct bio *bp);
73 static void mambodisk_task(void *arg);
75 #define MBODISK_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
76 #define MBODISK_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
77 #define MBODISK_LOCK_INIT(_sc) \
78 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
80 #define MBODISK_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
81 #define MBODISK_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
82 #define MBODISK_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
85 mambodisk_identify(driver_t *driver, device_t parent)
89 for (i = 0; mambocall(MAMBO_DISK_INFO,MAMBO_INFO_DEVSZ,i) > 0; i++)
90 BUS_ADD_CHILD(parent,0,"mambodisk",i);
94 mambodisk_probe(device_t dev)
96 device_set_desc(dev, "Mambo Simulated Block Device");
101 mambodisk_attach(device_t dev)
103 struct mambodisk_softc *sc;
108 sc = device_get_softc(dev);
110 MBODISK_LOCK_INIT(sc);
112 d = sc->disk = disk_alloc();
113 d->d_open = mambodisk_open;
114 d->d_close = mambodisk_close;
115 d->d_strategy = mambodisk_strategy;
116 d->d_name = "mambodisk";
118 d->d_maxsize = maxphys; /* Maybe ask bridge? */
120 d->d_sectorsize = 512;
121 sc->maxblocks = mambocall(MAMBO_DISK_INFO,MAMBO_INFO_BLKSZ,d->d_unit)
124 d->d_unit = device_get_unit(dev);
125 d->d_mediasize = mambocall(MAMBO_DISK_INFO,MAMBO_INFO_DEVSZ,d->d_unit)
126 * 1024ULL; /* Mambo gives size in KB */
128 mb = d->d_mediasize >> 20; /* 1MiB == 1 << 20 */
130 if (mb >= 10240) { /* 1GiB = 1024 MiB */
134 device_printf(dev, "%ju%cB, %d byte sectors\n", mb, unit,
136 disk_create(d, DISK_VERSION);
137 bioq_init(&sc->bio_queue);
140 kproc_create(&mambodisk_task, sc, &sc->p, 0, 0, "task: mambo hd");
146 mambodisk_detach(device_t dev)
148 struct mambodisk_softc *sc = device_get_softc(dev);
156 /* wait for thread to finish. XXX probably want timeout. -sorbo */
158 while (sc->running != -1)
159 msleep(sc, &sc->sc_mtx, PRIBIO, "detach", 0);
163 disk_destroy(sc->disk);
164 /* XXX destroy anything in queue */
166 MBODISK_LOCK_DESTROY(sc);
172 mambodisk_open(struct disk *dp)
178 mambodisk_close(struct disk *dp)
184 mambodisk_strategy(struct bio *bp)
186 struct mambodisk_softc *sc;
188 sc = (struct mambodisk_softc *)bp->bio_disk->d_drv1;
190 bioq_disksort(&sc->bio_queue, bp);
196 mambodisk_task(void *arg)
198 struct mambodisk_softc *sc = (struct mambodisk_softc*)arg;
207 unit = device_get_unit(dev);
209 while (sc->running) {
212 bp = bioq_first(&sc->bio_queue);
214 msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0);
215 } while (bp == NULL && sc->running);
217 bioq_remove(&sc->bio_queue, bp);
221 sz = sc->disk->d_sectorsize;
222 end = bp->bio_pblkno + (bp->bio_bcount / sz);
223 for (block = bp->bio_pblkno; block < end;) {
225 char *vaddr = bp->bio_data +
226 (block - bp->bio_pblkno) * sz;
228 numblocks = end - block;
229 if (numblocks > sc->maxblocks)
230 numblocks = sc->maxblocks;
232 if (bp->bio_cmd == BIO_READ) {
233 result = mambocall(MAMBO_DISK_READ, vaddr,
234 (u_long)block, (numblocks << 16) | unit);
235 } else if (bp->bio_cmd == BIO_WRITE) {
236 result = mambocall(MAMBO_DISK_WRITE, vaddr,
237 (u_long)block, (numblocks << 16) | unit);
249 bp->bio_resid = (end - block) * sz;
250 bp->bio_flags |= BIO_ERROR;
255 /* tell parent we're done */
264 static device_method_t mambodisk_methods[] = {
265 DEVMETHOD(device_identify, mambodisk_identify),
266 DEVMETHOD(device_probe, mambodisk_probe),
267 DEVMETHOD(device_attach, mambodisk_attach),
268 DEVMETHOD(device_detach, mambodisk_detach),
272 static driver_t mambodisk_driver = {
275 sizeof(struct mambodisk_softc),
277 static devclass_t mambodisk_devclass;
279 DRIVER_MODULE(mambodisk, mambo, mambodisk_driver, mambodisk_devclass, 0, 0);