1d9611800SBenno Rice /* 2d9611800SBenno Rice * Copyright (C) 2002 Benno Rice <benno@FreeBSD.org> 3d9611800SBenno Rice * All rights reserved. 4d9611800SBenno Rice * 5d9611800SBenno Rice * Redistribution and use in source and binary forms, with or without 6d9611800SBenno Rice * modification, are permitted provided that the following conditions 7d9611800SBenno Rice * are met: 8d9611800SBenno Rice * 1. Redistributions of source code must retain the above copyright 9d9611800SBenno Rice * notice, this list of conditions and the following disclaimer. 10d9611800SBenno Rice * 2. Redistributions in binary form must reproduce the above copyright 11d9611800SBenno Rice * notice, this list of conditions and the following disclaimer in the 12d9611800SBenno Rice * documentation and/or other materials provided with the distribution. 13d9611800SBenno Rice * 14d9611800SBenno Rice * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR 15d9611800SBenno Rice * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16d9611800SBenno Rice * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17d9611800SBenno Rice * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 18d9611800SBenno Rice * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19d9611800SBenno Rice * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 20d9611800SBenno Rice * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 21d9611800SBenno Rice * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 22d9611800SBenno Rice * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 23d9611800SBenno Rice * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24d9611800SBenno Rice * 25d9611800SBenno Rice * 26d9611800SBenno Rice */ 27d9611800SBenno Rice 28aad970f1SDavid E. O'Brien #include <sys/cdefs.h> 29aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 30aad970f1SDavid E. O'Brien 31d9611800SBenno Rice #include <sys/param.h> 32d9611800SBenno Rice #include <sys/systm.h> 33d9611800SBenno Rice #include <sys/bio.h> 34d9611800SBenno Rice #include <sys/kernel.h> 35d626a8a2SPeter Grehan #include <sys/kthread.h> 36d626a8a2SPeter Grehan #include <sys/linker.h> 37d626a8a2SPeter Grehan #include <sys/lock.h> 38d626a8a2SPeter Grehan #include <sys/malloc.h> 39d626a8a2SPeter Grehan #include <sys/mutex.h> 40d626a8a2SPeter Grehan #include <sys/proc.h> 41d626a8a2SPeter Grehan 42d626a8a2SPeter Grehan #include <geom/geom.h> 43d9611800SBenno Rice 44d9611800SBenno Rice #include <dev/ofw/openfirm.h> 45d9611800SBenno Rice 46d9611800SBenno Rice #define OFWD_BLOCKSIZE 512 47d9611800SBenno Rice 48d9611800SBenno Rice struct ofwd_softc 49d9611800SBenno Rice { 50d626a8a2SPeter Grehan struct bio_queue_head ofwd_bio_queue; 51d626a8a2SPeter Grehan struct mtx ofwd_queue_mtx; 52d9611800SBenno Rice ihandle_t ofwd_instance; 53d626a8a2SPeter Grehan off_t ofwd_mediasize; 54d626a8a2SPeter Grehan unsigned ofwd_sectorsize; 55d626a8a2SPeter Grehan unsigned ofwd_fwheads; 56d626a8a2SPeter Grehan unsigned ofwd_fwsectors; 57d626a8a2SPeter Grehan struct proc *ofwd_procp; 58d626a8a2SPeter Grehan struct g_geom *ofwd_gp; 59d626a8a2SPeter Grehan struct g_provider *ofwd_pp; 60d626a8a2SPeter Grehan } ofwd_softc; 61d626a8a2SPeter Grehan 62d626a8a2SPeter Grehan static g_init_t g_ofwd_init; 63d626a8a2SPeter Grehan static g_start_t g_ofwd_start; 64d626a8a2SPeter Grehan static g_access_t g_ofwd_access; 65d626a8a2SPeter Grehan 66d626a8a2SPeter Grehan struct g_class g_ofwd_class = { 67d626a8a2SPeter Grehan .name = "OFWD", 68d626a8a2SPeter Grehan .version = G_VERSION, 69d626a8a2SPeter Grehan .init = g_ofwd_init, 70d626a8a2SPeter Grehan .start = g_ofwd_start, 71d626a8a2SPeter Grehan .access = g_ofwd_access, 72d9611800SBenno Rice }; 73d9611800SBenno Rice 74d626a8a2SPeter Grehan DECLARE_GEOM_CLASS(g_ofwd_class, g_ofwd); 75d9611800SBenno Rice 76d626a8a2SPeter Grehan static int 77d626a8a2SPeter Grehan ofwd_startio(struct ofwd_softc *sc, struct bio *bp) 78d9611800SBenno Rice { 79d626a8a2SPeter Grehan u_int r; 80aa77148dSPeter Grehan 8124730dd7SPoul-Henning Kamp r = OF_seek(sc->ofwd_instance, bp->bio_offset); 82d626a8a2SPeter Grehan switch (bp->bio_cmd) { 83d626a8a2SPeter Grehan case BIO_READ: 84d9611800SBenno Rice r = OF_read(sc->ofwd_instance, (void *)bp->bio_data, 85d626a8a2SPeter Grehan bp->bio_length); 86d626a8a2SPeter Grehan break; 87d626a8a2SPeter Grehan case BIO_WRITE: 88d9611800SBenno Rice r = OF_write(sc->ofwd_instance, (void *)bp->bio_data, 89d626a8a2SPeter Grehan bp->bio_length); 90d626a8a2SPeter Grehan break; 91d626a8a2SPeter Grehan } 92d626a8a2SPeter Grehan if (r != bp->bio_length) 93d626a8a2SPeter Grehan panic("ofwd: incorrect i/o count"); 94d626a8a2SPeter Grehan 95d626a8a2SPeter Grehan bp->bio_resid = 0; 96d626a8a2SPeter Grehan return (0); 97d9611800SBenno Rice } 98d9611800SBenno Rice 991ac37de6SPeter Grehan static void 100d626a8a2SPeter Grehan ofwd_kthread(void *arg) 1011ac37de6SPeter Grehan { 102d626a8a2SPeter Grehan struct ofwd_softc *sc; 103d626a8a2SPeter Grehan struct bio *bp; 104d626a8a2SPeter Grehan int error; 105d626a8a2SPeter Grehan 106d626a8a2SPeter Grehan sc = arg; 107d626a8a2SPeter Grehan curthread->td_base_pri = PRIBIO; 108d626a8a2SPeter Grehan 109d626a8a2SPeter Grehan for (;;) { 110d626a8a2SPeter Grehan mtx_lock(&sc->ofwd_queue_mtx); 111d626a8a2SPeter Grehan bp = bioq_takefirst(&sc->ofwd_bio_queue); 112d626a8a2SPeter Grehan if (!bp) { 113d626a8a2SPeter Grehan msleep(sc, &sc->ofwd_queue_mtx, PRIBIO | PDROP, 114d626a8a2SPeter Grehan "ofwdwait", 0); 115d626a8a2SPeter Grehan continue; 116d626a8a2SPeter Grehan } 117d626a8a2SPeter Grehan mtx_unlock(&sc->ofwd_queue_mtx); 118d626a8a2SPeter Grehan if (bp->bio_cmd == BIO_GETATTR) { 119d626a8a2SPeter Grehan error = EOPNOTSUPP; 120d626a8a2SPeter Grehan } else 121d626a8a2SPeter Grehan error = ofwd_startio(sc, bp); 122d626a8a2SPeter Grehan 123d626a8a2SPeter Grehan if (error != -1) { 124d626a8a2SPeter Grehan bp->bio_completed = bp->bio_length; 125d626a8a2SPeter Grehan g_io_deliver(bp, error); 126d626a8a2SPeter Grehan } 127d626a8a2SPeter Grehan } 128d626a8a2SPeter Grehan } 129d626a8a2SPeter Grehan 130d626a8a2SPeter Grehan static void 131d626a8a2SPeter Grehan g_ofwd_init(struct g_class *mp __unused) 132d626a8a2SPeter Grehan { 133d626a8a2SPeter Grehan struct ofwd_softc *sc; 134d626a8a2SPeter Grehan struct g_geom *gp; 135d626a8a2SPeter Grehan struct g_provider *pp; 136d626a8a2SPeter Grehan char path[128]; 137d626a8a2SPeter Grehan char fname[32]; 1381ac37de6SPeter Grehan phandle_t ofd; 139d626a8a2SPeter Grehan ihandle_t ifd; 140d626a8a2SPeter Grehan int error; 1411ac37de6SPeter Grehan 1421ac37de6SPeter Grehan ofd = OF_finddevice("ofwdisk"); 1431ac37de6SPeter Grehan if (ofd == -1) 1441ac37de6SPeter Grehan return; 1451ac37de6SPeter Grehan 146d626a8a2SPeter Grehan bzero(path, 128); 147d626a8a2SPeter Grehan OF_package_to_path(ofd, path, 128); 148d626a8a2SPeter Grehan OF_getprop(ofd, "file", fname, sizeof(fname)); 149d626a8a2SPeter Grehan printf("ofw_disk located at %s, file %s\n", path, fname); 150d626a8a2SPeter Grehan ifd = OF_open(path); 151d626a8a2SPeter Grehan if (ifd == -1) { 152d626a8a2SPeter Grehan printf("ofw_disk: could not create instance\n"); 153d626a8a2SPeter Grehan return; 1541ac37de6SPeter Grehan } 1551ac37de6SPeter Grehan 156d626a8a2SPeter Grehan sc = (struct ofwd_softc *)malloc(sizeof *sc, M_DEVBUF, 157d626a8a2SPeter Grehan M_WAITOK|M_ZERO); 158d626a8a2SPeter Grehan bioq_init(&sc->ofwd_bio_queue); 159d626a8a2SPeter Grehan mtx_init(&sc->ofwd_queue_mtx, "ofwd bio queue", NULL, MTX_DEF); 160d626a8a2SPeter Grehan sc->ofwd_instance = ifd; 161d626a8a2SPeter Grehan sc->ofwd_mediasize = (off_t)2*33554432 * OFWD_BLOCKSIZE; 162d626a8a2SPeter Grehan sc->ofwd_sectorsize = OFWD_BLOCKSIZE; 163d626a8a2SPeter Grehan sc->ofwd_fwsectors = 0; 164d626a8a2SPeter Grehan sc->ofwd_fwheads = 0; 165d626a8a2SPeter Grehan error = kthread_create(ofwd_kthread, sc, &sc->ofwd_procp, 0, 0, 166d626a8a2SPeter Grehan "ofwd0"); 167d626a8a2SPeter Grehan if (error != 0) { 168d626a8a2SPeter Grehan free(sc, M_DEVBUF); 169d626a8a2SPeter Grehan return; 170d9611800SBenno Rice } 171d9611800SBenno Rice 172d626a8a2SPeter Grehan gp = g_new_geomf(&g_ofwd_class, "ofwd0"); 173d626a8a2SPeter Grehan gp->softc = sc; 174d626a8a2SPeter Grehan pp = g_new_providerf(gp, "ofwd0"); 175d626a8a2SPeter Grehan pp->mediasize = sc->ofwd_mediasize; 176d626a8a2SPeter Grehan pp->sectorsize = sc->ofwd_sectorsize; 177d626a8a2SPeter Grehan sc->ofwd_gp = gp; 178d626a8a2SPeter Grehan sc->ofwd_pp = pp; 179d626a8a2SPeter Grehan g_error_provider(pp, 0); 180d626a8a2SPeter Grehan } 181d626a8a2SPeter Grehan 182d626a8a2SPeter Grehan static void 183d626a8a2SPeter Grehan g_ofwd_start(struct bio *bp) 184d9611800SBenno Rice { 185d9611800SBenno Rice struct ofwd_softc *sc; 186d9611800SBenno Rice 187d626a8a2SPeter Grehan sc = bp->bio_to->geom->softc; 188d626a8a2SPeter Grehan mtx_lock(&sc->ofwd_queue_mtx); 189d626a8a2SPeter Grehan bioq_disksort(&sc->ofwd_bio_queue, bp); 190d626a8a2SPeter Grehan mtx_unlock(&sc->ofwd_queue_mtx); 191d626a8a2SPeter Grehan wakeup(sc); 192d9611800SBenno Rice } 193d9611800SBenno Rice 194d626a8a2SPeter Grehan static int 195d626a8a2SPeter Grehan g_ofwd_access(struct g_provider *pp, int r, int w, int e) 196d626a8a2SPeter Grehan { 197d9611800SBenno Rice 198d626a8a2SPeter Grehan if (pp->geom->softc == NULL) 199d626a8a2SPeter Grehan return (ENXIO); 200d9611800SBenno Rice return (0); 201d9611800SBenno Rice } 202