Home | History | Annotate | Line # | Download | only in flash
flash.c revision 1.7
      1 /*	$NetBSD: flash.c,v 1.7 2011/06/28 18:14:11 ahoka Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2011 Department of Software Engineering,
      5  *		      University of Szeged, Hungary
      6  * Copyright (c) 2011 Adam Hoka <ahoka (at) NetBSD.org>
      7  * Copyright (c) 2010 David Tengeri <dtengeri (at) 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.7 2011/06/28 18:14:11 ahoka 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 static inline flash_off_t flash_get_part_offset(struct flash_softc *fl,
     85     size_t poffset);
     86 
     87 int flash_match(device_t parent, cfdata_t match, void *aux);
     88 void flash_attach(device_t parent, device_t self, void *aux);
     89 int flash_detach(device_t device, int flags);
     90 
     91 CFATTACH_DECL_NEW(flash, sizeof(struct flash_softc),
     92     flash_match, flash_attach, flash_detach, NULL);
     93 
     94 /**
     95  * Block device's operation
     96  */
     97 const struct bdevsw flash_bdevsw = {
     98 	.d_open = flashopen,
     99 	.d_close = flashclose,
    100 	.d_strategy = flashstrategy,
    101 	.d_ioctl = flashioctl,
    102 	.d_dump = flashdump,
    103 	.d_psize = nosize,
    104 	.d_flag = D_DISK | D_MPSAFE
    105 };
    106 
    107 /**
    108  * Character device's operations
    109  */
    110 const struct cdevsw flash_cdevsw = {
    111 	.d_open = flashopen,
    112 	.d_close = flashclose,
    113 	.d_read = flashread,
    114 	.d_write = flashwrite,
    115 	.d_ioctl = flashioctl,
    116 	.d_stop = nostop,
    117 	.d_tty = notty,
    118 	.d_poll = nopoll,
    119 	.d_mmap = nommap,
    120 	.d_kqfilter = nokqfilter,
    121 	.d_flag = D_DISK | D_MPSAFE
    122 };
    123 
    124 /* ARGSUSED */
    125 int
    126 flash_match(device_t parent, cfdata_t match, void *aux)
    127 {
    128 	/* pseudo device, always attaches */
    129 	return 1;
    130 }
    131 
    132 /* ARGSUSED */
    133 void
    134 flash_attach(device_t parent, device_t self, void *aux)
    135 {
    136 	struct flash_softc *sc = device_private(self);
    137 	struct flash_attach_args *faa = aux;
    138 	char pbuf[2][sizeof("9999 KB")];
    139 
    140 	sc->sc_dev = self;
    141 	sc->sc_parent_dev = parent;
    142 	sc->flash_if = faa->flash_if;
    143 	sc->sc_partinfo = faa->partinfo;
    144 	sc->hw_softc = device_private(parent);
    145 
    146 	format_bytes(pbuf[0], sizeof(pbuf[0]), sc->sc_partinfo.part_size);
    147 	format_bytes(pbuf[1], sizeof(pbuf[1]), sc->flash_if->erasesize);
    148 
    149 	aprint_naive("\n");
    150 
    151 	switch (sc->flash_if->type) {
    152 	case FLASH_TYPE_NOR:
    153 		aprint_normal(": %s NOR flash\n", pbuf[0]);
    154 		break;
    155 
    156 	case FLASH_TYPE_NAND:
    157 		aprint_normal(": %s NAND flash\n", pbuf[0]);
    158 		break;
    159 
    160 	default:
    161 		aprint_normal(": %s unknown flash\n", pbuf[0]);
    162 	}
    163 
    164 	aprint_normal_dev(sc->sc_dev,
    165 	    "size: %#jx, offset: %#jx",
    166 	    (uintmax_t )sc->sc_partinfo.part_size,
    167 	    (uintmax_t )sc->sc_partinfo.part_offset);
    168 
    169 	if (sc->sc_partinfo.part_flags & FLASH_PART_READONLY) {
    170 		sc->sc_readonly = true;
    171 		aprint_normal(", read only");
    172 	} else {
    173 		sc->sc_readonly = false;
    174 	}
    175 
    176 	aprint_normal("\n");
    177 
    178 	if (sc->sc_partinfo.part_size == 0) {
    179 		aprint_error_dev(self,
    180 		    "partition size must be larger than 0\n");
    181 		return;
    182 	}
    183 
    184 	switch (sc->flash_if->type) {
    185 	case FLASH_TYPE_NOR:
    186 		aprint_normal_dev(sc->sc_dev,
    187 		    "erase size %s bytes, write size %d bytes\n",
    188 		    pbuf[1], sc->flash_if->writesize);
    189 		break;
    190 
    191 	case FLASH_TYPE_NAND:
    192 	default:
    193 		aprint_normal_dev(sc->sc_dev,
    194 		    "erase size %s, page size %d bytes, write size %d bytes\n",
    195 		    pbuf[1], sc->flash_if->page_size,
    196 		    sc->flash_if->writesize);
    197 		break;
    198 	}
    199 
    200 	if (!pmf_device_register1(sc->sc_dev, NULL, NULL, flash_shutdown))
    201 		aprint_error_dev(sc->sc_dev,
    202 		    "couldn't establish power handler\n");
    203 }
    204 
    205 int
    206 flash_detach(device_t device, int flags)
    207 {
    208 	struct flash_softc *sc = device_private(device);
    209 
    210 	pmf_device_deregister(sc->sc_dev);
    211 
    212 	/* freeing flash_if is our responsibility */
    213 	kmem_free(sc->flash_if, sizeof(*sc->flash_if));
    214 
    215 	return 0;
    216 }
    217 
    218 int
    219 flash_print(void *aux, const char *pnp)
    220 {
    221 	struct flash_attach_args *arg;
    222 	const char *type;
    223 
    224 	if (pnp != NULL) {
    225 		arg = aux;
    226 		switch (arg->flash_if->type) {
    227 		case FLASH_TYPE_NOR:
    228 			type = "NOR";
    229 			break;
    230 		case FLASH_TYPE_NAND:
    231 			type = "NAND";
    232 			break;
    233 		default:
    234 			panic("flash_print: unknown type %d",
    235 			    arg->flash_if->type);
    236 		}
    237 		aprint_normal("%s flash at %s", type, pnp);
    238 	}
    239 	return UNCONF;
    240 }
    241 
    242 device_t
    243 flash_attach_mi(struct flash_interface *flash_if, device_t device)
    244 {
    245 	struct flash_attach_args arg;
    246 
    247 #ifdef DIAGNOSTIC
    248 	if (flash_if == NULL) {
    249 		aprint_error("flash_attach_mi: NULL\n");
    250 		return 0;
    251 	}
    252 #endif
    253 	arg.flash_if = flash_if;
    254 
    255 	return config_found_ia(device, "flashbus", &arg, flash_print);
    256 }
    257 
    258 /**
    259  * flash_open - open the character device
    260  * Checks if there is a driver registered to the minor number of the open
    261  * request.
    262  */
    263 int
    264 flashopen(dev_t dev, int flags, int fmt, struct lwp *l)
    265 {
    266 	int unit = minor(dev);
    267 	struct flash_softc *sc;
    268 
    269 	FLDPRINTFN(1, ("flash: opening device unit %d\n", unit));
    270 
    271 	if ((sc = device_lookup_private(&flash_cd, unit)) == NULL)
    272 		return ENXIO;
    273 
    274 	/* TODO return eperm if want to open for writing a read only dev */
    275 
    276 	/* reset buffer length */
    277 //	sc->sc_cache->fc_len = 0;
    278 
    279 	return 0;
    280 }
    281 
    282 /**
    283  * flash_close - close device
    284  * We don't have to release any resources, so just return 0.
    285  */
    286 int
    287 flashclose(dev_t dev, int flags, int fmt, struct lwp *l)
    288 {
    289 	int unit = minor(dev);
    290 	struct flash_softc *sc;
    291 	int err;
    292 
    293 	FLDPRINTFN(1, ("flash: closing flash device unit %d\n", unit));
    294 
    295 	if ((sc = device_lookup_private(&flash_cd, unit)) == NULL)
    296 		return ENXIO;
    297 
    298 	if (!sc->sc_readonly) {
    299 		err = flash_sync(sc->sc_dev);
    300 		if (err)
    301 			return err;
    302 	}
    303 
    304 	return 0;
    305 }
    306 
    307 /**
    308  * flash_read - read from character device
    309  * This function uses the registered driver's read function to read the requested length to
    310  * a buffer and then moves this buffer to userspace.
    311  */
    312 int
    313 flashread(dev_t dev, struct uio *uio, int flag)
    314 {
    315 	return physio(flashstrategy, NULL, dev, B_READ, minphys, uio);
    316 }
    317 
    318 /**
    319  * flash_write - write to character device
    320  * This function moves the data into a buffer from userspace to kernel space,
    321  * then uses the registered driver's write function to write out the data to
    322  * the media.
    323  */
    324 int
    325 flashwrite(dev_t dev, struct uio *uio, int flag)
    326 {
    327 	return physio(flashstrategy, NULL, dev, B_WRITE, minphys, uio);
    328 }
    329 
    330 void
    331 flashstrategy(struct buf *bp)
    332 {
    333 	struct flash_softc *sc;
    334 	const struct flash_interface *flash_if;
    335 	const struct flash_partition *part;
    336 	int unit, device_blks;
    337 
    338 	unit = minor(bp->b_dev);
    339 	sc = device_lookup_private(&flash_cd, unit);
    340 	if (sc == NULL) {
    341 		bp->b_error = ENXIO;
    342 		goto done;
    343 	}
    344 
    345 	flash_if = sc->flash_if;
    346 	part = &sc->sc_partinfo;
    347 
    348 	/* divider */
    349 	KASSERT(flash_if->writesize != 0);
    350 
    351 	aprint_debug_dev(sc->sc_dev, "flash_strategy()\n");
    352 
    353 	if (!(bp->b_flags & B_READ) && sc->sc_readonly) {
    354 		bp->b_error = EACCES;
    355 		goto done;
    356 	}
    357 
    358 	/* check if length is not negative */
    359 	if (bp->b_blkno < 0) {
    360 		bp->b_error = EINVAL;
    361 		goto done;
    362 	}
    363 
    364 	/* zero lenght i/o */
    365 	if (bp->b_bcount == 0) {
    366 		goto done;
    367 	}
    368 
    369 	device_blks = sc->sc_partinfo.part_size / DEV_BSIZE;
    370 	KASSERT(part->part_offset % DEV_BSIZE == 0);
    371 	bp->b_rawblkno = bp->b_blkno + (part->part_offset / DEV_BSIZE);
    372 
    373 	if (bounds_check_with_mediasize(bp, DEV_BSIZE, device_blks) <= 0) {
    374 		goto done;
    375 	}
    376 
    377 	bp->b_resid = bp->b_bcount;
    378 	flash_if->submit(sc->sc_parent_dev, bp);
    379 
    380 	return;
    381 done:
    382 	bp->b_resid = bp->b_bcount;
    383 	biodone(bp);
    384 }
    385 
    386 /*
    387  * Handle the ioctl for the device
    388  */
    389 int
    390 flashioctl(dev_t dev, u_long command, void *data, int flags, struct lwp *l)
    391 {
    392 	struct flash_erase_params *ep;
    393 	struct flash_info_params *ip;
    394 	struct flash_dump_params *dp;
    395 	struct flash_badblock_params *bbp;
    396 	struct flash_erase_instruction ei;
    397 	struct flash_softc *sc;
    398 	int unit, err;
    399 	size_t retlen;
    400 	flash_off_t offset;
    401 	bool bad;
    402 
    403 	unit = minor(dev);
    404 	if ((sc = device_lookup_private(&flash_cd, unit)) == NULL)
    405 		return ENXIO;
    406 
    407 	err = 0;
    408 	switch (command) {
    409 	case FLASH_ERASE_BLOCK:
    410 		/**
    411 		 * Set up an erase instruction then call the registered
    412 		 * driver's erase operation.
    413 		 */
    414 		ep = data;
    415 
    416 		if (sc->sc_readonly) {
    417 			return EACCES;
    418 		}
    419 
    420 		ei.ei_addr = ep->ep_addr;
    421 		ei.ei_len = ep->ep_len;
    422 		ei.ei_callback = NULL;
    423 
    424 		err = flash_erase(sc->sc_dev, &ei);
    425 		if (err) {
    426 			return err;
    427 		}
    428 
    429 		break;
    430 	case FLASH_BLOCK_ISBAD:
    431 		/**
    432 		 * Set up an erase instruction then call the registered
    433 		 * driver's erase operation.
    434 		 */
    435 		bbp = data;
    436 
    437 		err = flash_block_isbad(sc->sc_dev, bbp->bbp_addr, &bad);
    438 		if (err) {
    439 			return err;
    440 		}
    441 		bbp->bbp_isbad = bad;
    442 
    443 		break;
    444 	case FLASH_BLOCK_MARKBAD:
    445 		bbp = data;
    446 
    447 		err = flash_block_markbad(sc->sc_dev, bbp->bbp_addr);
    448 
    449 		break;
    450 	case FLASH_DUMP:
    451 		dp = data;
    452 		offset = dp->dp_block * sc->flash_if->erasesize;
    453 		FLDPRINTF(("Reading from block: %jd len: %jd\n",
    454 			(intmax_t )dp->dp_block, (intmax_t )dp->dp_len));
    455 		err = flash_read(sc->sc_parent_dev, offset, dp->dp_len,
    456 		    &retlen, dp->dp_buf);
    457 		if (err)
    458 			return err;
    459 		if (retlen != dp->dp_len) {
    460 			dp->dp_len = -1;
    461 			dp->dp_buf = NULL;
    462 		}
    463 
    464 		break;
    465 	case FLASH_GET_INFO:
    466 		ip = data;
    467 
    468 		ip->ip_page_size = sc->flash_if->page_size;
    469 		ip->ip_erase_size = sc->flash_if->erasesize;
    470 		ip->ip_flash_type = sc->flash_if->type;
    471 		ip->ip_flash_size = sc->sc_partinfo.part_size;
    472 		break;
    473 	default:
    474 		err = ENODEV;
    475 	}
    476 
    477 	return err;
    478 }
    479 
    480 int
    481 flashdump(dev_t dev, daddr_t blkno, void *va, size_t size)
    482 {
    483     return EACCES;
    484 }
    485 
    486 bool
    487 flash_shutdown(device_t self, int how)
    488 {
    489 	struct flash_softc *sc = device_private(self);
    490 
    491 	if ((how & RB_NOSYNC) == 0 && !sc->sc_readonly)
    492 		flash_sync(self);
    493 
    494 	return true;
    495 }
    496 
    497 const struct flash_interface *
    498 flash_get_interface(dev_t dev)
    499 {
    500 	struct flash_softc *sc;
    501 	int unit;
    502 
    503 	unit = minor(dev);
    504 	if ((sc = device_lookup_private(&flash_cd, unit)) == NULL)
    505 		return NULL;
    506 
    507 	return sc->flash_if;
    508 }
    509 
    510 const struct flash_softc *
    511 flash_get_softc(dev_t dev)
    512 {
    513 	struct flash_softc *sc;
    514 	int unit;
    515 
    516 	unit = minor(dev);
    517 	sc = device_lookup_private(&flash_cd, unit);
    518 
    519 	return sc;
    520 }
    521 
    522 device_t
    523 flash_get_device(dev_t dev)
    524 {
    525 	struct flash_softc *sc;
    526 	int unit;
    527 
    528 	unit = minor(dev);
    529 	sc = device_lookup_private(&flash_cd, unit);
    530 
    531 	return sc->sc_dev;
    532 }
    533 
    534 static inline flash_off_t
    535 flash_get_part_offset(struct flash_softc *sc, size_t poffset)
    536 {
    537 	return sc->sc_partinfo.part_offset + poffset;
    538 }
    539 
    540 int
    541 flash_erase(device_t self, struct flash_erase_instruction *ei)
    542 {
    543 	struct flash_softc *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,
    563     flash_off_t offset, size_t len, size_t *retlen, uint8_t *buf)
    564 {
    565 	struct flash_softc *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,
    579     flash_off_t offset, size_t len, size_t *retlen, const uint8_t *buf)
    580 {
    581 	struct flash_softc *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 *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 *bad)
    616 {
    617 	struct flash_softc *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 *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