1 /* 2 * Copyright (C) 2002 Benno Rice <benno@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 23 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * 25 * 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/bio.h> 34 #include <sys/bus.h> 35 #include <sys/conf.h> 36 #include <sys/kernel.h> 37 #include <geom/geom_disk.h> 38 39 #include <dev/ofw/openfirm.h> 40 41 #include <machine/bus.h> 42 #include <machine/md_var.h> 43 #include <machine/nexusvar.h> 44 45 #define OFWD_BLOCKSIZE 512 46 47 struct ofwd_softc 48 { 49 device_t ofwd_dev; 50 struct disk ofwd_disk; 51 phandle_t ofwd_package; 52 ihandle_t ofwd_instance; 53 }; 54 55 /* 56 * Disk device bus interface. 57 */ 58 static void ofwd_identify(driver_t *, device_t); 59 static int ofwd_probe(device_t); 60 static int ofwd_attach(device_t); 61 62 static device_method_t ofwd_methods[] = { 63 DEVMETHOD(device_identify, ofwd_identify), 64 DEVMETHOD(device_probe, ofwd_probe), 65 DEVMETHOD(device_attach, ofwd_attach), 66 { 0, 0 } 67 }; 68 69 static driver_t ofwd_driver = { 70 "ofwd", 71 ofwd_methods, 72 sizeof(struct ofwd_softc) 73 }; 74 75 static devclass_t ofwd_devclass; 76 77 DRIVER_MODULE(ofwd, nexus, ofwd_driver, ofwd_devclass, 0, 0); 78 79 /* 80 * Disk device control interface. 81 */ 82 static disk_strategy_t ofwd_strategy; 83 84 /* 85 * Handle an I/O request. 86 */ 87 static void 88 ofwd_strategy(struct bio *bp) 89 { 90 struct ofwd_softc *sc; 91 long r; 92 93 sc = (struct ofwd_softc *)bp->bio_disk->d_drv1; 94 95 if (sc == NULL) { 96 printf("ofwd: bio for invalid disk!\n"); 97 biofinish(bp, NULL, EINVAL); 98 return; 99 } 100 101 r = OF_seek(sc->ofwd_instance, bp->bio_offset); 102 if (r == -1) { 103 bp->bio_resid = bp->bio_bcount; 104 device_printf(sc->ofwd_dev, "seek failed\n"); 105 biofinish(bp, NULL, EIO); 106 return; 107 } 108 109 if (bp->bio_cmd == BIO_READ) { 110 r = OF_read(sc->ofwd_instance, (void *)bp->bio_data, 111 bp->bio_bcount); 112 } else { 113 r = OF_write(sc->ofwd_instance, (void *)bp->bio_data, 114 bp->bio_bcount); 115 } 116 117 if (r > bp->bio_bcount) 118 panic("ofwd: more bytes read/written than requested"); 119 if (r == -1) { 120 device_printf(sc->ofwd_dev, "r (%ld) < bp->bio_bcount (%ld)\n", 121 r, bp->bio_bcount); 122 biofinish(bp, NULL, EIO); 123 return; 124 } 125 126 bp->bio_resid -= r; 127 128 if (r < bp->bio_bcount) { 129 device_printf(sc->ofwd_dev, "r (%ld) < bp->bio_bcount (%ld)\n", 130 r, bp->bio_bcount); 131 biofinish(bp, NULL, EIO); /* XXX: probably not an error */ 132 return; 133 } 134 biodone(bp); 135 return; 136 } 137 138 /* 139 * Probe for an OpenFirmware disk. 140 */ 141 static int 142 ofwd_probe(device_t dev) 143 { 144 char *type; 145 146 type = nexus_get_device_type(dev); 147 148 if (type == NULL || strcmp(type, "disk") != 0) 149 return (ENXIO); 150 151 device_set_desc(dev, "OpenFirmware disk"); 152 return (0); 153 } 154 155 static int 156 ofwd_attach(device_t dev) 157 { 158 struct ofwd_softc *sc; 159 char path[128]; 160 dev_t dsk; 161 162 sc = device_get_softc(dev); 163 sc->ofwd_dev = dev; 164 165 bzero(path, 128); 166 OF_package_to_path(nexus_get_node(dev), path, 128); 167 device_printf(dev, "located at %s\n", path); 168 sc->ofwd_instance = OF_open(path); 169 if (sc->ofwd_instance == -1) { 170 device_printf(dev, "could not create instance\n"); 171 return (ENXIO); 172 } 173 174 sc->ofwd_disk.d_strategy = ofwd_strategy; 175 sc->ofwd_disk.d_name = "ofwd"; 176 sc->ofwd_disk.d_sectorsize = OFWD_BLOCKSIZE; 177 sc->ofwd_disk.d_mediasize = (off_t)33554432 * OFWD_BLOCKSIZE; 178 sc->ofwd_disk.d_fwsectors = 0; 179 sc->ofwd_disk.d_fwheads = 0; 180 sc->ofwd_disk.d_drv1 = sc; 181 sc->ofwd_disk.d_maxsize = PAGE_SIZE; 182 disk_create(device_get_unit(dev), &sc->ofwd_disk, 0, NULL, NULL); 183 184 return (0); 185 } 186 187 static void 188 ofwd_identify(driver_t *driver, device_t parent) 189 { 190 } 191