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