1 /*- 2 * Copyright (c) 2019 Justin Hibbits 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 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. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26 #include <sys/cdefs.h> 27 __FBSDID("$FreeBSD$"); 28 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/bio.h> 32 #include <sys/bus.h> 33 #include <sys/kernel.h> 34 #include <sys/kthread.h> 35 #include <sys/lock.h> 36 #include <sys/module.h> 37 #include <sys/mutex.h> 38 #include <sys/proc.h> 39 #include <geom/geom_disk.h> 40 41 #include <vm/vm.h> 42 #include <vm/pmap.h> 43 44 #include <dev/ofw/ofw_bus.h> 45 #include <dev/ofw/ofw_bus_subr.h> 46 #include <dev/ofw/openfirm.h> 47 #include "opal.h" 48 49 /* 50 * OPAL System flash driver, using OPAL firmware calls to access the device. 51 * 52 * This just presents the base block interface. The fdt_slicer can be used on 53 * top to present the partitions listed in the fdt. 54 * 55 * There are three OPAL methods used: OPAL_FLASH_READ, OPAL_FLASH_WRITE, and 56 * OPAL_FLASH_ERASE. At the firmware layer, READ and WRITE can be on arbitrary 57 * boundaries, but ERASE is only at flash-block-size block alignments and sizes. 58 * To account for this, the following restrictions are in place: 59 * 60 * - Reads are on a 512-byte block boundary and size 61 * - Writes and Erases are aligned and sized on flash-block-size bytes. 62 * 63 * In order to support the fdt_slicer we present a type attribute of 64 * NAND::device. 65 */ 66 struct opalflash_softc { 67 device_t sc_dev; 68 struct mtx sc_mtx; 69 struct disk *sc_disk; 70 struct proc *sc_p; 71 struct bio_queue_head sc_bio_queue; 72 int sc_opal_id; 73 bool sc_erase; /* Erase is needed before write. */ 74 }; 75 76 #define OPALFLASH_LOCK(sc) mtx_lock(&(sc)->sc_mtx) 77 #define OPALFLASH_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx) 78 #define OPALFLASH_LOCK_INIT(sc) \ 79 mtx_init(&(sc)->sc_mtx, device_get_nameunit((sc)->sc_dev), \ 80 "opalflash", MTX_DEF) 81 82 #define FLASH_BLOCKSIZE 512 83 84 static int opalflash_probe(device_t); 85 static int opalflash_attach(device_t); 86 87 static device_method_t opalflash_methods[] = { 88 /* Device interface */ 89 DEVMETHOD(device_probe, opalflash_probe), 90 DEVMETHOD(device_attach, opalflash_attach), 91 92 DEVMETHOD_END 93 }; 94 95 static driver_t opalflash_driver = { 96 "opalflash", 97 opalflash_methods, 98 sizeof(struct opalflash_softc) 99 }; 100 101 static devclass_t opalflash_devclass; 102 103 DRIVER_MODULE(opalflash, opal, opalflash_driver, opalflash_devclass, 0, 0); 104 105 /* GEOM Disk interfaces. */ 106 static int 107 opalflash_open(struct disk *dp) 108 { 109 110 return (0); 111 } 112 113 static int 114 opalflash_close(struct disk *dp) 115 { 116 117 return (0); 118 } 119 120 static int 121 opalflash_ioctl(struct disk *dp, u_long cmd, void *data, int fflag, 122 struct thread *td) 123 { 124 125 return (EINVAL); 126 } 127 128 /* Handle the one attribute we need to play nice with geom_flashmap. */ 129 static int 130 opalflash_getattr(struct bio *bp) 131 { 132 struct opalflash_softc *sc; 133 device_t dev; 134 135 if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL) 136 return (ENXIO); 137 138 sc = bp->bio_disk->d_drv1; 139 dev = sc->sc_dev; 140 141 if (strcmp(bp->bio_attribute, "NAND::device") == 0) { 142 if (bp->bio_length != sizeof(dev)) 143 return (EFAULT); 144 bcopy(&dev, bp->bio_data, sizeof(dev)); 145 } else 146 return (-1); 147 return (0); 148 } 149 150 static void 151 opalflash_strategy(struct bio *bp) 152 { 153 struct opalflash_softc *sc; 154 155 sc = (struct opalflash_softc *)bp->bio_disk->d_drv1; 156 OPALFLASH_LOCK(sc); 157 bioq_disksort(&sc->sc_bio_queue, bp); 158 wakeup(sc); 159 OPALFLASH_UNLOCK(sc); 160 } 161 162 static int 163 opalflash_read(struct opalflash_softc *sc, off_t off, 164 caddr_t data, off_t count) 165 { 166 struct opal_msg msg; 167 int rv, size, token; 168 169 /* Ensure we write aligned to a full block size. */ 170 if (off % sc->sc_disk->d_sectorsize != 0 || 171 count % sc->sc_disk->d_sectorsize != 0) 172 return (EIO); 173 174 token = opal_alloc_async_token(); 175 176 /* 177 * Read one page at a time. It's not guaranteed that the buffer is 178 * physically contiguous. 179 */ 180 rv = 0; 181 while (count > 0) { 182 size = MIN(count, PAGE_SIZE); 183 size = MIN(size, PAGE_SIZE - ((u_long)data & PAGE_MASK)); 184 rv = opal_call(OPAL_FLASH_READ, sc->sc_opal_id, off, 185 vtophys(data), size, token); 186 if (rv == OPAL_ASYNC_COMPLETION) { 187 rv = opal_wait_completion(&msg, sizeof(msg), token); 188 if (rv == OPAL_SUCCESS) 189 rv = msg.params[1]; 190 } 191 if (rv != OPAL_SUCCESS) 192 break; 193 count -= size; 194 off += size; 195 data += size; 196 } 197 opal_free_async_token(token); 198 if (rv == OPAL_SUCCESS) 199 rv = 0; 200 else 201 rv = EIO; 202 203 return (rv); 204 } 205 206 static int 207 opalflash_erase(struct opalflash_softc *sc, off_t off, off_t count) 208 { 209 struct opal_msg msg; 210 int rv, token; 211 212 /* Ensure we write aligned to a full block size. */ 213 if (off % sc->sc_disk->d_stripesize != 0 || 214 count % sc->sc_disk->d_stripesize != 0) 215 return (EIO); 216 217 token = opal_alloc_async_token(); 218 219 rv = opal_call(OPAL_FLASH_ERASE, sc->sc_opal_id, off, count, token); 220 if (rv == OPAL_ASYNC_COMPLETION) { 221 rv = opal_wait_completion(&msg, sizeof(msg), token); 222 if (rv == OPAL_SUCCESS) 223 rv = msg.params[1]; 224 } 225 opal_free_async_token(token); 226 227 if (rv == OPAL_SUCCESS) 228 rv = 0; 229 else 230 rv = EIO; 231 232 return (rv); 233 } 234 235 static int 236 opalflash_write(struct opalflash_softc *sc, off_t off, 237 caddr_t data, off_t count) 238 { 239 struct opal_msg msg; 240 int rv, size, token; 241 242 /* Ensure we write aligned to a full block size. */ 243 if (off % sc->sc_disk->d_sectorsize != 0 || 244 count % sc->sc_disk->d_sectorsize != 0) 245 return (EIO); 246 247 if (sc->sc_erase) { 248 /* Erase the full block first, then write in page chunks. */ 249 rv = opalflash_erase(sc, off, count); 250 if (rv != 0) 251 return (rv); 252 } 253 254 token = opal_alloc_async_token(); 255 256 /* 257 * Write one page at a time. It's not guaranteed that the buffer is 258 * physically contiguous. 259 */ 260 while (count > 0) { 261 size = MIN(count, PAGE_SIZE); 262 size = MIN(size, PAGE_SIZE - ((u_long)data & PAGE_MASK)); 263 rv = opal_call(OPAL_FLASH_WRITE, sc->sc_opal_id, off, 264 vtophys(data), size, token); 265 if (rv == OPAL_ASYNC_COMPLETION) { 266 rv = opal_wait_completion(&msg, sizeof(msg), token); 267 if (rv == OPAL_SUCCESS) 268 rv = msg.params[1]; 269 } 270 if (rv != OPAL_SUCCESS) 271 break; 272 count -= size; 273 off += size; 274 data += size; 275 } 276 opal_free_async_token(token); 277 278 if (rv == OPAL_SUCCESS) 279 rv = 0; 280 else 281 rv = EIO; 282 283 return (rv); 284 } 285 286 /* Main flash handling task. */ 287 static void 288 opalflash_task(void *arg) 289 { 290 struct opalflash_softc *sc; 291 struct bio *bp; 292 device_t dev; 293 294 sc = arg; 295 296 for (;;) { 297 dev = sc->sc_dev; 298 OPALFLASH_LOCK(sc); 299 do { 300 bp = bioq_first(&sc->sc_bio_queue); 301 if (bp == NULL) 302 msleep(sc, &sc->sc_mtx, PRIBIO, "opalflash", 0); 303 } while (bp == NULL); 304 bioq_remove(&sc->sc_bio_queue, bp); 305 OPALFLASH_UNLOCK(sc); 306 307 switch (bp->bio_cmd) { 308 case BIO_DELETE: 309 bp->bio_error = opalflash_erase(sc, bp->bio_offset, 310 bp->bio_bcount); 311 break; 312 case BIO_READ: 313 bp->bio_error = opalflash_read(sc, bp->bio_offset, 314 bp->bio_data, bp->bio_bcount); 315 break; 316 case BIO_WRITE: 317 bp->bio_error = opalflash_write(sc, bp->bio_offset, 318 bp->bio_data, bp->bio_bcount); 319 break; 320 default: 321 bp->bio_error = EINVAL; 322 } 323 biodone(bp); 324 } 325 } 326 327 328 /* Device driver interfaces. */ 329 330 static int 331 opalflash_probe(device_t dev) 332 { 333 if (!ofw_bus_is_compatible(dev, "ibm,opal-flash")) 334 return (ENXIO); 335 336 device_set_desc(dev, "OPAL System Flash"); 337 338 return (BUS_PROBE_GENERIC); 339 } 340 341 static int 342 opalflash_attach(device_t dev) 343 { 344 struct opalflash_softc *sc; 345 phandle_t node; 346 cell_t flash_blocksize, opal_id; 347 uint32_t regs[2]; 348 349 sc = device_get_softc(dev); 350 sc->sc_dev = dev; 351 352 node = ofw_bus_get_node(dev); 353 OF_getencprop(node, "ibm,opal-id", &opal_id, sizeof(opal_id)); 354 sc->sc_opal_id = opal_id; 355 356 if (OF_getencprop(node, "ibm,flash-block-size", 357 &flash_blocksize, sizeof(flash_blocksize)) < 0) { 358 device_printf(dev, "Cannot determine flash block size.\n"); 359 return (ENXIO); 360 } 361 362 if (!OF_hasprop(node, "no-erase")) 363 sc->sc_erase = true; 364 365 OPALFLASH_LOCK_INIT(sc); 366 367 if (OF_getencprop(node, "reg", regs, sizeof(regs)) < 0) { 368 device_printf(dev, "Unable to get flash size.\n"); 369 return (ENXIO); 370 } 371 372 sc->sc_disk = disk_alloc(); 373 sc->sc_disk->d_name = "opalflash"; 374 sc->sc_disk->d_open = opalflash_open; 375 sc->sc_disk->d_close = opalflash_close; 376 sc->sc_disk->d_strategy = opalflash_strategy; 377 sc->sc_disk->d_ioctl = opalflash_ioctl; 378 sc->sc_disk->d_getattr = opalflash_getattr; 379 sc->sc_disk->d_drv1 = sc; 380 sc->sc_disk->d_maxsize = DFLTPHYS; 381 sc->sc_disk->d_mediasize = regs[1]; 382 sc->sc_disk->d_unit = device_get_unit(sc->sc_dev); 383 sc->sc_disk->d_sectorsize = FLASH_BLOCKSIZE; 384 sc->sc_disk->d_stripesize = flash_blocksize; 385 sc->sc_disk->d_dump = NULL; 386 387 disk_create(sc->sc_disk, DISK_VERSION); 388 bioq_init(&sc->sc_bio_queue); 389 390 kproc_create(&opalflash_task, sc, &sc->sc_p, 0, 0, "task: OPAL Flash"); 391 392 return (0); 393 } 394