1 /* $NetBSD: flash.c,v 1.12 2014/07/25 08:10:36 dholland Exp $ */ 2 3 /*- 4 * Copyright (c) 2011 Department of Software Engineering, 5 * University of Szeged, Hungary 6 * Copyright (c) 2011 Adam Hoka <ahoka@NetBSD.org> 7 * Copyright (c) 2010 David Tengeri <dtengeri@inf.u-szeged.hu> 8 * All rights reserved. 9 * 10 * This code is derived from software contributed to The NetBSD Foundation 11 * by the Department of Software Engineering, University of Szeged, Hungary 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 /*- 36 * Framework for storage devices based on Flash technology 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: flash.c,v 1.12 2014/07/25 08:10:36 dholland Exp $"); 41 42 #include <sys/param.h> 43 #include <sys/types.h> 44 #include <sys/proc.h> 45 #include <sys/errno.h> 46 #include <sys/ioctl.h> 47 #include <sys/device.h> 48 #include <sys/conf.h> 49 #include <sys/kmem.h> 50 #include <sys/uio.h> 51 #include <sys/kernel.h> 52 53 #include <sys/atomic.h> 54 #include <sys/buf.h> 55 #include <sys/bufq.h> 56 #include <sys/disk.h> 57 #include <sys/disklabel.h> 58 #include <sys/malloc.h> 59 #include <sys/reboot.h> 60 61 #include <sys/flashio.h> 62 #include "flash.h" 63 64 #ifdef FLASH_DEBUG 65 int flashdebug = FLASH_DEBUG; 66 #endif 67 68 extern struct cfdriver flash_cd; 69 70 dev_type_open(flashopen); 71 dev_type_close(flashclose); 72 dev_type_read(flashread); 73 dev_type_write(flashwrite); 74 dev_type_ioctl(flashioctl); 75 dev_type_strategy(flashstrategy); 76 dev_type_dump(flashdump); 77 78 int flash_print(void *aux, const char *pnp); 79 80 bool flash_shutdown(device_t dev, int how); 81 int flash_nsectors(struct buf *bp); 82 int flash_sector(struct buf *bp); 83 84 int flash_match(device_t parent, cfdata_t match, void *aux); 85 void flash_attach(device_t parent, device_t self, void *aux); 86 int flash_detach(device_t device, int flags); 87 88 CFATTACH_DECL_NEW(flash, sizeof(struct flash_softc), 89 flash_match, flash_attach, flash_detach, NULL); 90 91 /** 92 * Block device's operation 93 */ 94 const struct bdevsw flash_bdevsw = { 95 .d_open = flashopen, 96 .d_close = flashclose, 97 .d_strategy = flashstrategy, 98 .d_ioctl = flashioctl, 99 .d_dump = flashdump, 100 .d_psize = nosize, 101 .d_discard = nodiscard, /* XXX this driver probably wants a discard */ 102 .d_flag = D_DISK | D_MPSAFE 103 }; 104 105 /** 106 * Character device's operations 107 */ 108 const struct cdevsw flash_cdevsw = { 109 .d_open = flashopen, 110 .d_close = flashclose, 111 .d_read = flashread, 112 .d_write = flashwrite, 113 .d_ioctl = flashioctl, 114 .d_stop = nostop, 115 .d_tty = notty, 116 .d_poll = nopoll, 117 .d_mmap = nommap, 118 .d_kqfilter = nokqfilter, 119 .d_discard = nodiscard, 120 .d_flag = D_DISK | D_MPSAFE 121 }; 122 123 /* ARGSUSED */ 124 int 125 flash_match(device_t parent, cfdata_t match, void *aux) 126 { 127 /* pseudo device, always attaches */ 128 return 1; 129 } 130 131 /* ARGSUSED */ 132 void 133 flash_attach(device_t parent, device_t self, void *aux) 134 { 135 struct flash_softc * const sc = device_private(self); 136 struct flash_attach_args * const faa = aux; 137 char pbuf[2][sizeof("9999 KB")]; 138 139 sc->sc_dev = self; 140 sc->sc_parent_dev = parent; 141 sc->flash_if = faa->flash_if; 142 sc->sc_partinfo = faa->partinfo; 143 sc->hw_softc = device_private(parent); 144 145 format_bytes(pbuf[0], sizeof(pbuf[0]), sc->sc_partinfo.part_size); 146 format_bytes(pbuf[1], sizeof(pbuf[1]), sc->flash_if->erasesize); 147 148 aprint_naive("\n"); 149 150 switch (sc->flash_if->type) { 151 case FLASH_TYPE_NOR: 152 aprint_normal(": NOR flash partition size %s, offset %#jx", 153 pbuf[0], (uintmax_t )sc->sc_partinfo.part_offset); 154 break; 155 156 case FLASH_TYPE_NAND: 157 aprint_normal(": NAND flash partition size %s, offset %#jx", 158 pbuf[0], (uintmax_t )sc->sc_partinfo.part_offset); 159 break; 160 161 default: 162 aprint_normal(": %s unknown flash", pbuf[0]); 163 } 164 165 if (sc->sc_partinfo.part_flags & FLASH_PART_READONLY) { 166 sc->sc_readonly = true; 167 aprint_normal(", read only"); 168 } else { 169 sc->sc_readonly = false; 170 } 171 172 aprint_normal("\n"); 173 174 if (sc->sc_partinfo.part_size == 0) { 175 aprint_error_dev(self, 176 "partition size must be larger than 0\n"); 177 return; 178 } 179 180 switch (sc->flash_if->type) { 181 case FLASH_TYPE_NOR: 182 aprint_normal_dev(sc->sc_dev, 183 "erase size %s bytes, write size %d bytes\n", 184 pbuf[1], sc->flash_if->writesize); 185 break; 186 187 case FLASH_TYPE_NAND: 188 default: 189 aprint_normal_dev(sc->sc_dev, 190 "erase size %s, page size %d bytes, write size %d bytes\n", 191 pbuf[1], sc->flash_if->page_size, 192 sc->flash_if->writesize); 193 break; 194 } 195 196 if (!pmf_device_register1(sc->sc_dev, NULL, NULL, flash_shutdown)) 197 aprint_error_dev(sc->sc_dev, 198 "couldn't establish power handler\n"); 199 } 200 201 int 202 flash_detach(device_t device, int flags) 203 { 204 struct flash_softc * const sc = device_private(device); 205 206 pmf_device_deregister(sc->sc_dev); 207 208 /* freeing flash_if is our responsibility */ 209 kmem_free(sc->flash_if, sizeof(*sc->flash_if)); 210 211 return 0; 212 } 213 214 int 215 flash_print(void *aux, const char *pnp) 216 { 217 struct flash_attach_args *arg; 218 const char *type; 219 220 if (pnp != NULL) { 221 arg = aux; 222 switch (arg->flash_if->type) { 223 case FLASH_TYPE_NOR: 224 type = "NOR"; 225 break; 226 case FLASH_TYPE_NAND: 227 type = "NAND"; 228 break; 229 default: 230 panic("flash_print: unknown type %d", 231 arg->flash_if->type); 232 } 233 aprint_normal("%s flash at %s", type, pnp); 234 } 235 return UNCONF; 236 } 237 238 device_t 239 flash_attach_mi(struct flash_interface * const flash_if, device_t device) 240 { 241 struct flash_attach_args arg; 242 243 #ifdef DIAGNOSTIC 244 if (flash_if == NULL) { 245 aprint_error("flash_attach_mi: NULL\n"); 246 return 0; 247 } 248 #endif 249 arg.flash_if = flash_if; 250 251 return config_found_ia(device, "flashbus", &arg, flash_print); 252 } 253 254 /** 255 * flash_open - open the character device 256 * Checks if there is a driver registered to the minor number of the open 257 * request. 258 */ 259 int 260 flashopen(dev_t dev, int flags, int fmt, lwp_t *l) 261 { 262 int unit = minor(dev); 263 struct flash_softc *sc; 264 265 FLDPRINTFN(1, ("flash: opening device unit %d\n", unit)); 266 267 if ((sc = device_lookup_private(&flash_cd, unit)) == NULL) 268 return ENXIO; 269 270 /* TODO return eperm if want to open for writing a read only dev */ 271 272 /* reset buffer length */ 273 // sc->sc_cache->fc_len = 0; 274 275 return 0; 276 } 277 278 /** 279 * flash_close - close device 280 * We don't have to release any resources, so just return 0. 281 */ 282 int 283 flashclose(dev_t dev, int flags, int fmt, lwp_t *l) 284 { 285 int unit = minor(dev); 286 struct flash_softc *sc; 287 int err; 288 289 FLDPRINTFN(1, ("flash: closing flash device unit %d\n", unit)); 290 291 if ((sc = device_lookup_private(&flash_cd, unit)) == NULL) 292 return ENXIO; 293 294 if (!sc->sc_readonly) { 295 err = flash_sync(sc->sc_dev); 296 if (err) 297 return err; 298 } 299 300 return 0; 301 } 302 303 /** 304 * flash_read - read from character device 305 * This function uses the registered driver's read function to read the 306 * requested length to * a buffer and then moves this buffer to userspace. 307 */ 308 int 309 flashread(dev_t dev, struct uio * const uio, int flag) 310 { 311 return physio(flashstrategy, NULL, dev, B_READ, minphys, uio); 312 } 313 314 /** 315 * flash_write - write to character device 316 * This function moves the data into a buffer from userspace to kernel space, 317 * then uses the registered driver's write function to write out the data to 318 * the media. 319 */ 320 int 321 flashwrite(dev_t dev, struct uio * const uio, int flag) 322 { 323 return physio(flashstrategy, NULL, dev, B_WRITE, minphys, uio); 324 } 325 326 void 327 flashstrategy(struct buf * const bp) 328 { 329 struct flash_softc *sc; 330 const struct flash_interface *flash_if; 331 const struct flash_partition *part; 332 int unit, device_blks; 333 334 unit = minor(bp->b_dev); 335 sc = device_lookup_private(&flash_cd, unit); 336 if (sc == NULL) { 337 bp->b_error = ENXIO; 338 goto done; 339 } 340 341 flash_if = sc->flash_if; 342 part = &sc->sc_partinfo; 343 344 /* divider */ 345 KASSERT(flash_if->writesize != 0); 346 347 aprint_debug_dev(sc->sc_dev, "flash_strategy()\n"); 348 349 if (!(bp->b_flags & B_READ) && sc->sc_readonly) { 350 bp->b_error = EACCES; 351 goto done; 352 } 353 354 /* check if length is not negative */ 355 if (bp->b_blkno < 0) { 356 bp->b_error = EINVAL; 357 goto done; 358 } 359 360 /* zero lenght i/o */ 361 if (bp->b_bcount == 0) { 362 goto done; 363 } 364 365 device_blks = sc->sc_partinfo.part_size / DEV_BSIZE; 366 KASSERT(part->part_offset % DEV_BSIZE == 0); 367 bp->b_rawblkno = bp->b_blkno + (part->part_offset / DEV_BSIZE); 368 369 if (bounds_check_with_mediasize(bp, DEV_BSIZE, device_blks) <= 0) { 370 goto done; 371 } 372 373 bp->b_resid = bp->b_bcount; 374 flash_if->submit(sc->sc_parent_dev, bp); 375 376 return; 377 done: 378 bp->b_resid = bp->b_bcount; 379 biodone(bp); 380 } 381 382 /* 383 * Handle the ioctl for the device 384 */ 385 int 386 flashioctl(dev_t dev, u_long command, void * const data, int flags, lwp_t *l) 387 { 388 struct flash_erase_params *ep; 389 struct flash_info_params *ip; 390 struct flash_dump_params *dp; 391 struct flash_badblock_params *bbp; 392 struct flash_erase_instruction ei; 393 struct flash_softc *sc; 394 int unit, err; 395 size_t retlen; 396 flash_off_t offset; 397 bool bad; 398 399 unit = minor(dev); 400 if ((sc = device_lookup_private(&flash_cd, unit)) == NULL) 401 return ENXIO; 402 403 err = 0; 404 switch (command) { 405 case FLASH_ERASE_BLOCK: 406 /** 407 * Set up an erase instruction then call the registered 408 * driver's erase operation. 409 */ 410 ep = data; 411 412 if (sc->sc_readonly) { 413 return EACCES; 414 } 415 416 ei.ei_addr = ep->ep_addr; 417 ei.ei_len = ep->ep_len; 418 ei.ei_callback = NULL; 419 420 err = flash_erase(sc->sc_dev, &ei); 421 if (err) { 422 return err; 423 } 424 425 break; 426 case FLASH_BLOCK_ISBAD: 427 /** 428 * Set up an erase instruction then call the registered 429 * driver's erase operation. 430 */ 431 bbp = data; 432 433 err = flash_block_isbad(sc->sc_dev, bbp->bbp_addr, &bad); 434 if (err) { 435 return err; 436 } 437 bbp->bbp_isbad = bad; 438 439 break; 440 case FLASH_BLOCK_MARKBAD: 441 bbp = data; 442 443 err = flash_block_markbad(sc->sc_dev, bbp->bbp_addr); 444 445 break; 446 case FLASH_DUMP: 447 dp = data; 448 offset = dp->dp_block * sc->flash_if->erasesize; 449 FLDPRINTF(("Reading from block: %jd len: %jd\n", 450 (intmax_t )dp->dp_block, (intmax_t )dp->dp_len)); 451 err = flash_read(sc->sc_parent_dev, offset, dp->dp_len, 452 &retlen, dp->dp_buf); 453 if (err) 454 return err; 455 if (retlen != dp->dp_len) { 456 dp->dp_len = -1; 457 dp->dp_buf = NULL; 458 } 459 460 break; 461 case FLASH_GET_INFO: 462 ip = data; 463 464 ip->ip_page_size = sc->flash_if->page_size; 465 ip->ip_erase_size = sc->flash_if->erasesize; 466 ip->ip_flash_type = sc->flash_if->type; 467 ip->ip_flash_size = sc->sc_partinfo.part_size; 468 break; 469 default: 470 err = ENODEV; 471 } 472 473 return err; 474 } 475 476 int 477 flashdump(dev_t dev, daddr_t blkno, void *va, size_t size) 478 { 479 return EACCES; 480 } 481 482 bool 483 flash_shutdown(device_t self, int how) 484 { 485 struct flash_softc * const sc = device_private(self); 486 487 if ((how & RB_NOSYNC) == 0 && !sc->sc_readonly) 488 flash_sync(self); 489 490 return true; 491 } 492 493 const struct flash_interface * 494 flash_get_interface(dev_t dev) 495 { 496 struct flash_softc *sc; 497 int unit; 498 499 unit = minor(dev); 500 if ((sc = device_lookup_private(&flash_cd, unit)) == NULL) 501 return NULL; 502 503 return sc->flash_if; 504 } 505 506 const struct flash_softc * 507 flash_get_softc(dev_t dev) 508 { 509 struct flash_softc *sc; 510 int unit; 511 512 unit = minor(dev); 513 sc = device_lookup_private(&flash_cd, unit); 514 515 return sc; 516 } 517 518 device_t 519 flash_get_device(dev_t dev) 520 { 521 struct flash_softc *sc; 522 int unit; 523 524 unit = minor(dev); 525 sc = device_lookup_private(&flash_cd, unit); 526 527 return sc->sc_dev; 528 } 529 530 flash_size_t 531 flash_get_size(dev_t dev) 532 { 533 const struct flash_softc *sc; 534 535 sc = flash_get_softc(dev); 536 537 return sc->sc_partinfo.part_size; 538 } 539 540 int 541 flash_erase(device_t self, struct flash_erase_instruction * const ei) 542 { 543 struct flash_softc * const sc = device_private(self); 544 KASSERT(ei != NULL); 545 struct flash_erase_instruction e = *ei; 546 547 if (sc->sc_readonly) 548 return EACCES; 549 550 /* adjust for flash partition */ 551 e.ei_addr += sc->sc_partinfo.part_offset; 552 553 /* bounds check for flash partition */ 554 if (e.ei_addr + e.ei_len > sc->sc_partinfo.part_size + 555 sc->sc_partinfo.part_offset) 556 return EINVAL; 557 558 return sc->flash_if->erase(device_parent(self), &e); 559 } 560 561 int 562 flash_read(device_t self, flash_off_t offset, size_t len, size_t * const retlen, 563 uint8_t * const buf) 564 { 565 struct flash_softc * const sc = device_private(self); 566 567 offset += sc->sc_partinfo.part_offset; 568 569 if (offset + len > sc->sc_partinfo.part_size + 570 sc->sc_partinfo.part_offset) 571 return EINVAL; 572 573 return sc->flash_if->read(device_parent(self), 574 offset, len, retlen, buf); 575 } 576 577 int 578 flash_write(device_t self, flash_off_t offset, size_t len, 579 size_t * const retlen, const uint8_t * const buf) 580 { 581 struct flash_softc * const sc = device_private(self); 582 583 if (sc->sc_readonly) 584 return EACCES; 585 586 offset += sc->sc_partinfo.part_offset; 587 588 if (offset + len > sc->sc_partinfo.part_size + 589 sc->sc_partinfo.part_offset) 590 return EINVAL; 591 592 return sc->flash_if->write(device_parent(self), 593 offset, len, retlen, buf); 594 } 595 596 int 597 flash_block_markbad(device_t self, flash_off_t offset) 598 { 599 struct flash_softc * const sc = device_private(self); 600 601 if (sc->sc_readonly) 602 return EACCES; 603 604 offset += sc->sc_partinfo.part_offset; 605 606 if (offset + sc->flash_if->erasesize >= 607 sc->sc_partinfo.part_size + 608 sc->sc_partinfo.part_offset) 609 return EINVAL; 610 611 return sc->flash_if->block_markbad(device_parent(self), offset); 612 } 613 614 int 615 flash_block_isbad(device_t self, flash_off_t offset, bool * const bad) 616 { 617 struct flash_softc * const sc = device_private(self); 618 619 offset += sc->sc_partinfo.part_offset; 620 621 if (offset + sc->flash_if->erasesize > 622 sc->sc_partinfo.part_size + 623 sc->sc_partinfo.part_offset) 624 return EINVAL; 625 626 return sc->flash_if->block_isbad(device_parent(self), offset, bad); 627 } 628 629 int 630 flash_sync(device_t self) 631 { 632 struct flash_softc * const sc = device_private(self); 633 634 if (sc->sc_readonly) 635 return EACCES; 636 637 /* noop now TODO: implement */ 638 return 0; 639 } 640 641 MODULE(MODULE_CLASS_DRIVER, flash, NULL); 642 643 #ifdef _MODULE 644 #include "ioconf.c" 645 #endif 646 647 static int 648 flash_modcmd(modcmd_t cmd, void *opaque) 649 { 650 int error = 0; 651 #ifdef _MODULE 652 int bmaj = -1, cmaj = -1; 653 #endif 654 655 switch (cmd) { 656 case MODULE_CMD_INIT: 657 #ifdef _MODULE 658 error = config_init_component(cfdriver_ioconf_flash, 659 cfattach_ioconf_flash, cfdata_ioconf_flash); 660 if (error) 661 return error; 662 error = devsw_attach("flash", &flash_bdevsw, &bmaj, 663 &flash_cdevsw, &cmaj); 664 if (error) 665 config_fini_component(cfdriver_ioconf_flash, 666 cfattach_ioconf_flash, cfdata_ioconf_flash); 667 #endif 668 return error; 669 case MODULE_CMD_FINI: 670 #ifdef _MODULE 671 devsw_detach(&flash_bdevsw, &flash_cdevsw); 672 error = config_fini_component(cfdriver_ioconf_flash, 673 cfattach_ioconf_flash, cfdata_ioconf_flash); 674 #endif 675 return error; 676 default: 677 return ENOTTY; 678 } 679 } 680