123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807 |
- /*
- * MTD device concatenation layer
- *
- * (C) 2002 Robert Kaiser <rkaiser@sysgo.de>
- *
- * NAND support by Christian Gan <cgan@iders.ca>
- *
- * This code is GPL
- */
- #include <linux/mtd/mtd.h>
- #include <linux/mtd/compat.h>
- #include <linux/mtd/concat.h>
- #include <ubi_uboot.h>
- /*
- * Our storage structure:
- * Subdev points to an array of pointers to struct mtd_info objects
- * which is allocated along with this structure
- *
- */
- struct mtd_concat {
- struct mtd_info mtd;
- int num_subdev;
- struct mtd_info **subdev;
- };
- /*
- * how to calculate the size required for the above structure,
- * including the pointer array subdev points to:
- */
- #define SIZEOF_STRUCT_MTD_CONCAT(num_subdev) \
- ((sizeof(struct mtd_concat) + (num_subdev) * sizeof(struct mtd_info *)))
- /*
- * Given a pointer to the MTD object in the mtd_concat structure,
- * we can retrieve the pointer to that structure with this macro.
- */
- #define CONCAT(x) ((struct mtd_concat *)(x))
- /*
- * MTD methods which look up the relevant subdevice, translate the
- * effective address and pass through to the subdevice.
- */
- static int
- concat_read(struct mtd_info *mtd, loff_t from, size_t len,
- size_t * retlen, u_char * buf)
- {
- struct mtd_concat *concat = CONCAT(mtd);
- int ret = 0, err;
- int i;
- *retlen = 0;
- for (i = 0; i < concat->num_subdev; i++) {
- struct mtd_info *subdev = concat->subdev[i];
- size_t size, retsize;
- if (from >= subdev->size) {
- /* Not destined for this subdev */
- size = 0;
- from -= subdev->size;
- continue;
- }
- if (from + len > subdev->size)
- /* First part goes into this subdev */
- size = subdev->size - from;
- else
- /* Entire transaction goes into this subdev */
- size = len;
- err = subdev->read(subdev, from, size, &retsize, buf);
- /* Save information about bitflips! */
- if (unlikely(err)) {
- if (err == -EBADMSG) {
- mtd->ecc_stats.failed++;
- ret = err;
- } else if (err == -EUCLEAN) {
- mtd->ecc_stats.corrected++;
- /* Do not overwrite -EBADMSG !! */
- if (!ret)
- ret = err;
- } else
- return err;
- }
- *retlen += retsize;
- len -= size;
- if (len == 0)
- return ret;
- buf += size;
- from = 0;
- }
- return -EINVAL;
- }
- static int
- concat_write(struct mtd_info *mtd, loff_t to, size_t len,
- size_t * retlen, const u_char * buf)
- {
- struct mtd_concat *concat = CONCAT(mtd);
- int err = -EINVAL;
- int i;
- if (!(mtd->flags & MTD_WRITEABLE))
- return -EROFS;
- *retlen = 0;
- for (i = 0; i < concat->num_subdev; i++) {
- struct mtd_info *subdev = concat->subdev[i];
- size_t size, retsize;
- if (to >= subdev->size) {
- size = 0;
- to -= subdev->size;
- continue;
- }
- if (to + len > subdev->size)
- size = subdev->size - to;
- else
- size = len;
- if (!(subdev->flags & MTD_WRITEABLE))
- err = -EROFS;
- else
- err = subdev->write(subdev, to, size, &retsize, buf);
- if (err)
- break;
- *retlen += retsize;
- len -= size;
- if (len == 0)
- break;
- err = -EINVAL;
- buf += size;
- to = 0;
- }
- return err;
- }
- static int
- concat_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
- {
- struct mtd_concat *concat = CONCAT(mtd);
- struct mtd_oob_ops devops = *ops;
- int i, err, ret = 0;
- ops->retlen = ops->oobretlen = 0;
- for (i = 0; i < concat->num_subdev; i++) {
- struct mtd_info *subdev = concat->subdev[i];
- if (from >= subdev->size) {
- from -= subdev->size;
- continue;
- }
- /* partial read ? */
- if (from + devops.len > subdev->size)
- devops.len = subdev->size - from;
- err = subdev->read_oob(subdev, from, &devops);
- ops->retlen += devops.retlen;
- ops->oobretlen += devops.oobretlen;
- /* Save information about bitflips! */
- if (unlikely(err)) {
- if (err == -EBADMSG) {
- mtd->ecc_stats.failed++;
- ret = err;
- } else if (err == -EUCLEAN) {
- mtd->ecc_stats.corrected++;
- /* Do not overwrite -EBADMSG !! */
- if (!ret)
- ret = err;
- } else
- return err;
- }
- if (devops.datbuf) {
- devops.len = ops->len - ops->retlen;
- if (!devops.len)
- return ret;
- devops.datbuf += devops.retlen;
- }
- if (devops.oobbuf) {
- devops.ooblen = ops->ooblen - ops->oobretlen;
- if (!devops.ooblen)
- return ret;
- devops.oobbuf += ops->oobretlen;
- }
- from = 0;
- }
- return -EINVAL;
- }
- static int
- concat_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
- {
- struct mtd_concat *concat = CONCAT(mtd);
- struct mtd_oob_ops devops = *ops;
- int i, err;
- if (!(mtd->flags & MTD_WRITEABLE))
- return -EROFS;
- ops->retlen = 0;
- for (i = 0; i < concat->num_subdev; i++) {
- struct mtd_info *subdev = concat->subdev[i];
- if (to >= subdev->size) {
- to -= subdev->size;
- continue;
- }
- /* partial write ? */
- if (to + devops.len > subdev->size)
- devops.len = subdev->size - to;
- err = subdev->write_oob(subdev, to, &devops);
- ops->retlen += devops.retlen;
- if (err)
- return err;
- if (devops.datbuf) {
- devops.len = ops->len - ops->retlen;
- if (!devops.len)
- return 0;
- devops.datbuf += devops.retlen;
- }
- if (devops.oobbuf) {
- devops.ooblen = ops->ooblen - ops->oobretlen;
- if (!devops.ooblen)
- return 0;
- devops.oobbuf += devops.oobretlen;
- }
- to = 0;
- }
- return -EINVAL;
- }
- static void concat_erase_callback(struct erase_info *instr)
- {
- /* Nothing to do here in U-Boot */
- }
- static int concat_dev_erase(struct mtd_info *mtd, struct erase_info *erase)
- {
- int err;
- wait_queue_head_t waitq;
- DECLARE_WAITQUEUE(wait, current);
- /*
- * This code was stol^H^H^H^Hinspired by mtdchar.c
- */
- init_waitqueue_head(&waitq);
- erase->mtd = mtd;
- erase->callback = concat_erase_callback;
- erase->priv = (unsigned long) &waitq;
- /*
- * FIXME: Allow INTERRUPTIBLE. Which means
- * not having the wait_queue head on the stack.
- */
- err = mtd->erase(mtd, erase);
- if (!err) {
- set_current_state(TASK_UNINTERRUPTIBLE);
- add_wait_queue(&waitq, &wait);
- if (erase->state != MTD_ERASE_DONE
- && erase->state != MTD_ERASE_FAILED)
- schedule();
- remove_wait_queue(&waitq, &wait);
- set_current_state(TASK_RUNNING);
- err = (erase->state == MTD_ERASE_FAILED) ? -EIO : 0;
- }
- return err;
- }
- static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
- {
- struct mtd_concat *concat = CONCAT(mtd);
- struct mtd_info *subdev;
- int i, err;
- uint64_t length, offset = 0;
- struct erase_info *erase;
- if (!(mtd->flags & MTD_WRITEABLE))
- return -EROFS;
- if (instr->addr > concat->mtd.size)
- return -EINVAL;
- if (instr->len + instr->addr > concat->mtd.size)
- return -EINVAL;
- /*
- * Check for proper erase block alignment of the to-be-erased area.
- * It is easier to do this based on the super device's erase
- * region info rather than looking at each particular sub-device
- * in turn.
- */
- if (!concat->mtd.numeraseregions) {
- /* the easy case: device has uniform erase block size */
- if (instr->addr & (concat->mtd.erasesize - 1))
- return -EINVAL;
- if (instr->len & (concat->mtd.erasesize - 1))
- return -EINVAL;
- } else {
- /* device has variable erase size */
- struct mtd_erase_region_info *erase_regions =
- concat->mtd.eraseregions;
- /*
- * Find the erase region where the to-be-erased area begins:
- */
- for (i = 0; i < concat->mtd.numeraseregions &&
- instr->addr >= erase_regions[i].offset; i++) ;
- --i;
- /*
- * Now erase_regions[i] is the region in which the
- * to-be-erased area begins. Verify that the starting
- * offset is aligned to this region's erase size:
- */
- if (instr->addr & (erase_regions[i].erasesize - 1))
- return -EINVAL;
- /*
- * now find the erase region where the to-be-erased area ends:
- */
- for (; i < concat->mtd.numeraseregions &&
- (instr->addr + instr->len) >= erase_regions[i].offset;
- ++i) ;
- --i;
- /*
- * check if the ending offset is aligned to this region's erase size
- */
- if ((instr->addr + instr->len) & (erase_regions[i].erasesize -
- 1))
- return -EINVAL;
- }
- instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
- /* make a local copy of instr to avoid modifying the caller's struct */
- erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
- if (!erase)
- return -ENOMEM;
- *erase = *instr;
- length = instr->len;
- /*
- * find the subdevice where the to-be-erased area begins, adjust
- * starting offset to be relative to the subdevice start
- */
- for (i = 0; i < concat->num_subdev; i++) {
- subdev = concat->subdev[i];
- if (subdev->size <= erase->addr) {
- erase->addr -= subdev->size;
- offset += subdev->size;
- } else {
- break;
- }
- }
- /* must never happen since size limit has been verified above */
- BUG_ON(i >= concat->num_subdev);
- /* now do the erase: */
- err = 0;
- for (; length > 0; i++) {
- /* loop for all subdevices affected by this request */
- subdev = concat->subdev[i]; /* get current subdevice */
- /* limit length to subdevice's size: */
- if (erase->addr + length > subdev->size)
- erase->len = subdev->size - erase->addr;
- else
- erase->len = length;
- if (!(subdev->flags & MTD_WRITEABLE)) {
- err = -EROFS;
- break;
- }
- length -= erase->len;
- if ((err = concat_dev_erase(subdev, erase))) {
- /* sanity check: should never happen since
- * block alignment has been checked above */
- BUG_ON(err == -EINVAL);
- if (erase->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
- instr->fail_addr = erase->fail_addr + offset;
- break;
- }
- /*
- * erase->addr specifies the offset of the area to be
- * erased *within the current subdevice*. It can be
- * non-zero only the first time through this loop, i.e.
- * for the first subdevice where blocks need to be erased.
- * All the following erases must begin at the start of the
- * current subdevice, i.e. at offset zero.
- */
- erase->addr = 0;
- offset += subdev->size;
- }
- instr->state = erase->state;
- kfree(erase);
- if (err)
- return err;
- if (instr->callback)
- instr->callback(instr);
- return 0;
- }
- static int concat_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
- {
- struct mtd_concat *concat = CONCAT(mtd);
- int i, err = -EINVAL;
- if ((len + ofs) > mtd->size)
- return -EINVAL;
- for (i = 0; i < concat->num_subdev; i++) {
- struct mtd_info *subdev = concat->subdev[i];
- uint64_t size;
- if (ofs >= subdev->size) {
- size = 0;
- ofs -= subdev->size;
- continue;
- }
- if (ofs + len > subdev->size)
- size = subdev->size - ofs;
- else
- size = len;
- err = subdev->lock(subdev, ofs, size);
- if (err)
- break;
- len -= size;
- if (len == 0)
- break;
- err = -EINVAL;
- ofs = 0;
- }
- return err;
- }
- static int concat_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
- {
- struct mtd_concat *concat = CONCAT(mtd);
- int i, err = 0;
- if ((len + ofs) > mtd->size)
- return -EINVAL;
- for (i = 0; i < concat->num_subdev; i++) {
- struct mtd_info *subdev = concat->subdev[i];
- uint64_t size;
- if (ofs >= subdev->size) {
- size = 0;
- ofs -= subdev->size;
- continue;
- }
- if (ofs + len > subdev->size)
- size = subdev->size - ofs;
- else
- size = len;
- err = subdev->unlock(subdev, ofs, size);
- if (err)
- break;
- len -= size;
- if (len == 0)
- break;
- err = -EINVAL;
- ofs = 0;
- }
- return err;
- }
- static void concat_sync(struct mtd_info *mtd)
- {
- struct mtd_concat *concat = CONCAT(mtd);
- int i;
- for (i = 0; i < concat->num_subdev; i++) {
- struct mtd_info *subdev = concat->subdev[i];
- subdev->sync(subdev);
- }
- }
- static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
- {
- struct mtd_concat *concat = CONCAT(mtd);
- int i, res = 0;
- if (!concat->subdev[0]->block_isbad)
- return res;
- if (ofs > mtd->size)
- return -EINVAL;
- for (i = 0; i < concat->num_subdev; i++) {
- struct mtd_info *subdev = concat->subdev[i];
- if (ofs >= subdev->size) {
- ofs -= subdev->size;
- continue;
- }
- res = subdev->block_isbad(subdev, ofs);
- break;
- }
- return res;
- }
- static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
- {
- struct mtd_concat *concat = CONCAT(mtd);
- int i, err = -EINVAL;
- if (!concat->subdev[0]->block_markbad)
- return 0;
- if (ofs > mtd->size)
- return -EINVAL;
- for (i = 0; i < concat->num_subdev; i++) {
- struct mtd_info *subdev = concat->subdev[i];
- if (ofs >= subdev->size) {
- ofs -= subdev->size;
- continue;
- }
- err = subdev->block_markbad(subdev, ofs);
- if (!err)
- mtd->ecc_stats.badblocks++;
- break;
- }
- return err;
- }
- /*
- * This function constructs a virtual MTD device by concatenating
- * num_devs MTD devices. A pointer to the new device object is
- * stored to *new_dev upon success. This function does _not_
- * register any devices: this is the caller's responsibility.
- */
- struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */
- int num_devs, /* number of subdevices */
- const char *name)
- { /* name for the new device */
- int i;
- size_t size;
- struct mtd_concat *concat;
- uint32_t max_erasesize, curr_erasesize;
- int num_erase_region;
- debug("Concatenating MTD devices:\n");
- for (i = 0; i < num_devs; i++)
- debug("(%d): \"%s\"\n", i, subdev[i]->name);
- debug("into device \"%s\"\n", name);
- /* allocate the device structure */
- size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
- concat = kzalloc(size, GFP_KERNEL);
- if (!concat) {
- printk
- ("memory allocation error while creating concatenated device \"%s\"\n",
- name);
- return NULL;
- }
- concat->subdev = (struct mtd_info **) (concat + 1);
- /*
- * Set up the new "super" device's MTD object structure, check for
- * incompatibilites between the subdevices.
- */
- concat->mtd.type = subdev[0]->type;
- concat->mtd.flags = subdev[0]->flags;
- concat->mtd.size = subdev[0]->size;
- concat->mtd.erasesize = subdev[0]->erasesize;
- concat->mtd.writesize = subdev[0]->writesize;
- concat->mtd.subpage_sft = subdev[0]->subpage_sft;
- concat->mtd.oobsize = subdev[0]->oobsize;
- concat->mtd.oobavail = subdev[0]->oobavail;
- if (subdev[0]->read_oob)
- concat->mtd.read_oob = concat_read_oob;
- if (subdev[0]->write_oob)
- concat->mtd.write_oob = concat_write_oob;
- if (subdev[0]->block_isbad)
- concat->mtd.block_isbad = concat_block_isbad;
- if (subdev[0]->block_markbad)
- concat->mtd.block_markbad = concat_block_markbad;
- concat->mtd.ecc_stats.badblocks = subdev[0]->ecc_stats.badblocks;
- concat->subdev[0] = subdev[0];
- for (i = 1; i < num_devs; i++) {
- if (concat->mtd.type != subdev[i]->type) {
- kfree(concat);
- printk("Incompatible device type on \"%s\"\n",
- subdev[i]->name);
- return NULL;
- }
- if (concat->mtd.flags != subdev[i]->flags) {
- /*
- * Expect all flags except MTD_WRITEABLE to be
- * equal on all subdevices.
- */
- if ((concat->mtd.flags ^ subdev[i]->
- flags) & ~MTD_WRITEABLE) {
- kfree(concat);
- printk("Incompatible device flags on \"%s\"\n",
- subdev[i]->name);
- return NULL;
- } else
- /* if writeable attribute differs,
- make super device writeable */
- concat->mtd.flags |=
- subdev[i]->flags & MTD_WRITEABLE;
- }
- concat->mtd.size += subdev[i]->size;
- concat->mtd.ecc_stats.badblocks +=
- subdev[i]->ecc_stats.badblocks;
- if (concat->mtd.writesize != subdev[i]->writesize ||
- concat->mtd.subpage_sft != subdev[i]->subpage_sft ||
- concat->mtd.oobsize != subdev[i]->oobsize ||
- !concat->mtd.read_oob != !subdev[i]->read_oob ||
- !concat->mtd.write_oob != !subdev[i]->write_oob) {
- kfree(concat);
- printk("Incompatible OOB or ECC data on \"%s\"\n",
- subdev[i]->name);
- return NULL;
- }
- concat->subdev[i] = subdev[i];
- }
- concat->mtd.ecclayout = subdev[0]->ecclayout;
- concat->num_subdev = num_devs;
- concat->mtd.name = name;
- concat->mtd.erase = concat_erase;
- concat->mtd.read = concat_read;
- concat->mtd.write = concat_write;
- concat->mtd.sync = concat_sync;
- concat->mtd.lock = concat_lock;
- concat->mtd.unlock = concat_unlock;
- /*
- * Combine the erase block size info of the subdevices:
- *
- * first, walk the map of the new device and see how
- * many changes in erase size we have
- */
- max_erasesize = curr_erasesize = subdev[0]->erasesize;
- num_erase_region = 1;
- for (i = 0; i < num_devs; i++) {
- if (subdev[i]->numeraseregions == 0) {
- /* current subdevice has uniform erase size */
- if (subdev[i]->erasesize != curr_erasesize) {
- /* if it differs from the last subdevice's erase size, count it */
- ++num_erase_region;
- curr_erasesize = subdev[i]->erasesize;
- if (curr_erasesize > max_erasesize)
- max_erasesize = curr_erasesize;
- }
- } else {
- /* current subdevice has variable erase size */
- int j;
- for (j = 0; j < subdev[i]->numeraseregions; j++) {
- /* walk the list of erase regions, count any changes */
- if (subdev[i]->eraseregions[j].erasesize !=
- curr_erasesize) {
- ++num_erase_region;
- curr_erasesize =
- subdev[i]->eraseregions[j].
- erasesize;
- if (curr_erasesize > max_erasesize)
- max_erasesize = curr_erasesize;
- }
- }
- }
- }
- if (num_erase_region == 1) {
- /*
- * All subdevices have the same uniform erase size.
- * This is easy:
- */
- concat->mtd.erasesize = curr_erasesize;
- concat->mtd.numeraseregions = 0;
- } else {
- uint64_t tmp64;
- /*
- * erase block size varies across the subdevices: allocate
- * space to store the data describing the variable erase regions
- */
- struct mtd_erase_region_info *erase_region_p;
- uint64_t begin, position;
- concat->mtd.erasesize = max_erasesize;
- concat->mtd.numeraseregions = num_erase_region;
- concat->mtd.eraseregions = erase_region_p =
- kmalloc(num_erase_region *
- sizeof (struct mtd_erase_region_info), GFP_KERNEL);
- if (!erase_region_p) {
- kfree(concat);
- printk
- ("memory allocation error while creating erase region list"
- " for device \"%s\"\n", name);
- return NULL;
- }
- /*
- * walk the map of the new device once more and fill in
- * in erase region info:
- */
- curr_erasesize = subdev[0]->erasesize;
- begin = position = 0;
- for (i = 0; i < num_devs; i++) {
- if (subdev[i]->numeraseregions == 0) {
- /* current subdevice has uniform erase size */
- if (subdev[i]->erasesize != curr_erasesize) {
- /*
- * fill in an mtd_erase_region_info structure for the area
- * we have walked so far:
- */
- erase_region_p->offset = begin;
- erase_region_p->erasesize =
- curr_erasesize;
- tmp64 = position - begin;
- do_div(tmp64, curr_erasesize);
- erase_region_p->numblocks = tmp64;
- begin = position;
- curr_erasesize = subdev[i]->erasesize;
- ++erase_region_p;
- }
- position += subdev[i]->size;
- } else {
- /* current subdevice has variable erase size */
- int j;
- for (j = 0; j < subdev[i]->numeraseregions; j++) {
- /* walk the list of erase regions, count any changes */
- if (subdev[i]->eraseregions[j].
- erasesize != curr_erasesize) {
- erase_region_p->offset = begin;
- erase_region_p->erasesize =
- curr_erasesize;
- tmp64 = position - begin;
- do_div(tmp64, curr_erasesize);
- erase_region_p->numblocks = tmp64;
- begin = position;
- curr_erasesize =
- subdev[i]->eraseregions[j].
- erasesize;
- ++erase_region_p;
- }
- position +=
- subdev[i]->eraseregions[j].
- numblocks * (uint64_t)curr_erasesize;
- }
- }
- }
- /* Now write the final entry */
- erase_region_p->offset = begin;
- erase_region_p->erasesize = curr_erasesize;
- tmp64 = position - begin;
- do_div(tmp64, curr_erasesize);
- erase_region_p->numblocks = tmp64;
- }
- return &concat->mtd;
- }
|