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/bus.h> 35d9611800SBenno Rice #include <sys/conf.h> 36d9611800SBenno Rice #include <sys/kernel.h> 371ac37de6SPeter Grehan #include <sys/limits.h> 3891b928c9SDag-Erling Smørgrav #include <geom/geom_disk.h> 39d9611800SBenno Rice 40d9611800SBenno Rice #include <dev/ofw/openfirm.h> 41d9611800SBenno Rice 42d9611800SBenno Rice #include <machine/bus.h> 43d9611800SBenno Rice #include <machine/md_var.h> 44d9611800SBenno Rice #include <machine/nexusvar.h> 45d9611800SBenno Rice 46d9611800SBenno Rice #define OFWD_BLOCKSIZE 512 47d9611800SBenno Rice 48d9611800SBenno Rice struct ofwd_softc 49d9611800SBenno Rice { 50d9611800SBenno Rice device_t ofwd_dev; 51d9611800SBenno Rice struct disk ofwd_disk; 52d9611800SBenno Rice phandle_t ofwd_package; 53d9611800SBenno Rice ihandle_t ofwd_instance; 54d9611800SBenno Rice }; 55d9611800SBenno Rice 56d9611800SBenno Rice /* 57d9611800SBenno Rice * Disk device bus interface. 58d9611800SBenno Rice */ 591ac37de6SPeter Grehan static void ofwd_identify(driver_t *, device_t); 60d9611800SBenno Rice static int ofwd_probe(device_t); 61d9611800SBenno Rice static int ofwd_attach(device_t); 62d9611800SBenno Rice 63d9611800SBenno Rice static device_method_t ofwd_methods[] = { 641ac37de6SPeter Grehan DEVMETHOD(device_identify, ofwd_identify), 65d9611800SBenno Rice DEVMETHOD(device_probe, ofwd_probe), 66d9611800SBenno Rice DEVMETHOD(device_attach, ofwd_attach), 67d9611800SBenno Rice { 0, 0 } 68d9611800SBenno Rice }; 69d9611800SBenno Rice 70d9611800SBenno Rice static driver_t ofwd_driver = { 71d9611800SBenno Rice "ofwd", 72d9611800SBenno Rice ofwd_methods, 73d9611800SBenno Rice sizeof(struct ofwd_softc) 74d9611800SBenno Rice }; 75d9611800SBenno Rice 76d9611800SBenno Rice static devclass_t ofwd_devclass; 77d9611800SBenno Rice 78d9611800SBenno Rice DRIVER_MODULE(ofwd, nexus, ofwd_driver, ofwd_devclass, 0, 0); 79d9611800SBenno Rice 80d9611800SBenno Rice /* 81d9611800SBenno Rice * Disk device control interface. 82d9611800SBenno Rice */ 83e165e4d2SPoul-Henning Kamp static disk_strategy_t ofwd_strategy; 84d9611800SBenno Rice 85d9611800SBenno Rice /* 86d9611800SBenno Rice * Handle an I/O request. 87d9611800SBenno Rice */ 88d9611800SBenno Rice static void 89d9611800SBenno Rice ofwd_strategy(struct bio *bp) 90d9611800SBenno Rice { 91d9611800SBenno Rice struct ofwd_softc *sc; 92d9611800SBenno Rice long r; 93d9611800SBenno Rice 94e165e4d2SPoul-Henning Kamp sc = (struct ofwd_softc *)bp->bio_disk->d_drv1; 95d9611800SBenno Rice 96d9611800SBenno Rice if (sc == NULL) { 97d9611800SBenno Rice printf("ofwd: bio for invalid disk!\n"); 98e165e4d2SPoul-Henning Kamp biofinish(bp, NULL, EINVAL); 99d9611800SBenno Rice return; 100d9611800SBenno Rice } 101d9611800SBenno Rice 10224730dd7SPoul-Henning Kamp r = OF_seek(sc->ofwd_instance, bp->bio_offset); 103d9611800SBenno Rice if (r == -1) { 104d9611800SBenno Rice bp->bio_resid = bp->bio_bcount; 105d9611800SBenno Rice device_printf(sc->ofwd_dev, "seek failed\n"); 106e165e4d2SPoul-Henning Kamp biofinish(bp, NULL, EIO); 107d9611800SBenno Rice return; 108d9611800SBenno Rice } 109d9611800SBenno Rice 110d9611800SBenno Rice if (bp->bio_cmd == BIO_READ) { 111d9611800SBenno Rice r = OF_read(sc->ofwd_instance, (void *)bp->bio_data, 112d9611800SBenno Rice bp->bio_bcount); 113d9611800SBenno Rice } else { 114d9611800SBenno Rice r = OF_write(sc->ofwd_instance, (void *)bp->bio_data, 115d9611800SBenno Rice bp->bio_bcount); 116d9611800SBenno Rice } 117d9611800SBenno Rice 118e165e4d2SPoul-Henning Kamp if (r > bp->bio_bcount) 119e165e4d2SPoul-Henning Kamp panic("ofwd: more bytes read/written than requested"); 120e165e4d2SPoul-Henning Kamp if (r == -1) { 121e165e4d2SPoul-Henning Kamp device_printf(sc->ofwd_dev, "r (%ld) < bp->bio_bcount (%ld)\n", 122e165e4d2SPoul-Henning Kamp r, bp->bio_bcount); 123e165e4d2SPoul-Henning Kamp biofinish(bp, NULL, EIO); 124e165e4d2SPoul-Henning Kamp return; 125e165e4d2SPoul-Henning Kamp } 126e165e4d2SPoul-Henning Kamp 127e165e4d2SPoul-Henning Kamp bp->bio_resid -= r; 128e165e4d2SPoul-Henning Kamp 129d9611800SBenno Rice if (r < bp->bio_bcount) { 130d9611800SBenno Rice device_printf(sc->ofwd_dev, "r (%ld) < bp->bio_bcount (%ld)\n", 131d9611800SBenno Rice r, bp->bio_bcount); 132e165e4d2SPoul-Henning Kamp biofinish(bp, NULL, EIO); /* XXX: probably not an error */ 133e165e4d2SPoul-Henning Kamp return; 134e165e4d2SPoul-Henning Kamp } 13560794e04SPoul-Henning Kamp biodone(bp); 136d9611800SBenno Rice return; 137d9611800SBenno Rice } 138d9611800SBenno Rice 139d9611800SBenno Rice /* 1401ac37de6SPeter Grehan * Attach the OpenFirmware disk to nexus if present 1411ac37de6SPeter Grehan */ 1421ac37de6SPeter Grehan static void 1431ac37de6SPeter Grehan ofwd_identify(driver_t *driver, device_t parent) 1441ac37de6SPeter Grehan { 1451ac37de6SPeter Grehan device_t child; 1461ac37de6SPeter Grehan phandle_t ofd; 1471ac37de6SPeter Grehan static char type[8]; 1481ac37de6SPeter Grehan 1491ac37de6SPeter Grehan ofd = OF_finddevice("ofwdisk"); 1501ac37de6SPeter Grehan if (ofd == -1) 1511ac37de6SPeter Grehan return; 1521ac37de6SPeter Grehan 1531ac37de6SPeter Grehan OF_getprop(ofd, "device_type", type, sizeof(type)); 1541ac37de6SPeter Grehan 1551ac37de6SPeter Grehan child = BUS_ADD_CHILD(parent, INT_MAX, "ofwd", 0); 1561ac37de6SPeter Grehan if (child != NULL) { 1571ac37de6SPeter Grehan nexus_set_device_type(child, type); 1581ac37de6SPeter Grehan nexus_set_node(child, ofd); 1591ac37de6SPeter Grehan } 1601ac37de6SPeter Grehan } 1611ac37de6SPeter Grehan 1621ac37de6SPeter Grehan /* 163d9611800SBenno Rice * Probe for an OpenFirmware disk. 164d9611800SBenno Rice */ 165d9611800SBenno Rice static int 166d9611800SBenno Rice ofwd_probe(device_t dev) 167d9611800SBenno Rice { 168d9611800SBenno Rice char *type; 169ad1c13f4SPeter Grehan char fname[32]; 170ad1c13f4SPeter Grehan phandle_t node; 171d9611800SBenno Rice 172d9611800SBenno Rice type = nexus_get_device_type(dev); 173ad1c13f4SPeter Grehan node = nexus_get_node(dev); 174d9611800SBenno Rice 1752abd35bcSPeter Grehan if (type == NULL || 1762abd35bcSPeter Grehan (strcmp(type, "disk") != 0 && strcmp(type, "block") != 0)) 177d9611800SBenno Rice return (ENXIO); 178d9611800SBenno Rice 179ad1c13f4SPeter Grehan if (OF_getprop(node, "file", fname, sizeof(fname)) == -1) 180ad1c13f4SPeter Grehan return (ENXIO); 181ad1c13f4SPeter Grehan 182d9611800SBenno Rice device_set_desc(dev, "OpenFirmware disk"); 183d9611800SBenno Rice return (0); 184d9611800SBenno Rice } 185d9611800SBenno Rice 186d9611800SBenno Rice static int 187d9611800SBenno Rice ofwd_attach(device_t dev) 188d9611800SBenno Rice { 189d9611800SBenno Rice struct ofwd_softc *sc; 190d9611800SBenno Rice char path[128]; 191ad1c13f4SPeter Grehan char fname[32]; 192d9611800SBenno Rice 193d9611800SBenno Rice sc = device_get_softc(dev); 194d9611800SBenno Rice sc->ofwd_dev = dev; 195d9611800SBenno Rice 196d9611800SBenno Rice bzero(path, 128); 197d9611800SBenno Rice OF_package_to_path(nexus_get_node(dev), path, 128); 198ad1c13f4SPeter Grehan OF_getprop(nexus_get_node(dev), "file", fname, sizeof(fname)); 199ad1c13f4SPeter Grehan device_printf(dev, "located at %s, file %s\n", path, fname); 200d9611800SBenno Rice sc->ofwd_instance = OF_open(path); 201d9611800SBenno Rice if (sc->ofwd_instance == -1) { 202d9611800SBenno Rice device_printf(dev, "could not create instance\n"); 203d9611800SBenno Rice return (ENXIO); 204d9611800SBenno Rice } 205d9611800SBenno Rice 206e165e4d2SPoul-Henning Kamp sc->ofwd_disk.d_strategy = ofwd_strategy; 207e165e4d2SPoul-Henning Kamp sc->ofwd_disk.d_name = "ofwd"; 208e165e4d2SPoul-Henning Kamp sc->ofwd_disk.d_sectorsize = OFWD_BLOCKSIZE; 209e165e4d2SPoul-Henning Kamp sc->ofwd_disk.d_mediasize = (off_t)33554432 * OFWD_BLOCKSIZE; 210e165e4d2SPoul-Henning Kamp sc->ofwd_disk.d_fwsectors = 0; 211e165e4d2SPoul-Henning Kamp sc->ofwd_disk.d_fwheads = 0; 212e165e4d2SPoul-Henning Kamp sc->ofwd_disk.d_drv1 = sc; 213e165e4d2SPoul-Henning Kamp sc->ofwd_disk.d_maxsize = PAGE_SIZE; 214e165e4d2SPoul-Henning Kamp disk_create(device_get_unit(dev), &sc->ofwd_disk, 0, NULL, NULL); 215d9611800SBenno Rice 216d9611800SBenno Rice return (0); 217d9611800SBenno Rice } 218