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