2 * MTD device concatenation layer
4 * (C) 2002 Robert Kaiser <rkaiser@sysgo.de>
6 * NAND support by Christian Gan <cgan@iders.ca>
10 * $Id: mtdconcat.c,v 1.11 2005/11/07 11:14:20 gleixner Exp $
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/sched.h>
17 #include <linux/types.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/concat.h>
23 * Our storage structure:
24 * Subdev points to an array of pointers to struct mtd_info objects
25 * which is allocated along with this structure
31 struct mtd_info **subdev;
35 * how to calculate the size required for the above structure,
36 * including the pointer array subdev points to:
38 #define SIZEOF_STRUCT_MTD_CONCAT(num_subdev) \
39 ((sizeof(struct mtd_concat) + (num_subdev) * sizeof(struct mtd_info *)))
42 * Given a pointer to the MTD object in the mtd_concat structure,
43 * we can retrieve the pointer to that structure with this macro.
45 #define CONCAT(x) ((struct mtd_concat *)(x))
48 * MTD methods which look up the relevant subdevice, translate the
49 * effective address and pass through to the subdevice.
53 concat_read(struct mtd_info *mtd, loff_t from, size_t len,
54 size_t * retlen, u_char * buf)
56 struct mtd_concat *concat = CONCAT(mtd);
62 for (i = 0; i < concat->num_subdev; i++) {
63 struct mtd_info *subdev = concat->subdev[i];
66 if (from >= subdev->size) {
67 /* Not destined for this subdev */
72 if (from + len > subdev->size)
73 /* First part goes into this subdev */
74 size = subdev->size - from;
76 /* Entire transaction goes into this subdev */
79 err = subdev->read(subdev, from, size, &retsize, buf);
97 concat_write(struct mtd_info *mtd, loff_t to, size_t len,
98 size_t * retlen, const u_char * buf)
100 struct mtd_concat *concat = CONCAT(mtd);
104 if (!(mtd->flags & MTD_WRITEABLE))
109 for (i = 0; i < concat->num_subdev; i++) {
110 struct mtd_info *subdev = concat->subdev[i];
111 size_t size, retsize;
113 if (to >= subdev->size) {
118 if (to + len > subdev->size)
119 size = subdev->size - to;
123 if (!(subdev->flags & MTD_WRITEABLE))
126 err = subdev->write(subdev, to, size, &retsize, buf);
144 concat_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
145 size_t * retlen, u_char * buf, u_char * eccbuf,
146 struct nand_oobinfo *oobsel)
148 struct mtd_concat *concat = CONCAT(mtd);
154 for (i = 0; i < concat->num_subdev; i++) {
155 struct mtd_info *subdev = concat->subdev[i];
156 size_t size, retsize;
158 if (from >= subdev->size) {
159 /* Not destined for this subdev */
161 from -= subdev->size;
165 if (from + len > subdev->size)
166 /* First part goes into this subdev */
167 size = subdev->size - from;
169 /* Entire transaction goes into this subdev */
172 if (subdev->read_ecc)
173 err = subdev->read_ecc(subdev, from, size,
174 &retsize, buf, eccbuf, oobsel);
189 eccbuf += subdev->oobsize;
190 /* in nand.c at least, eccbufs are
191 tagged with 2 (int)eccstatus'; we
192 must account for these */
193 eccbuf += 2 * (sizeof (int));
201 concat_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
202 size_t * retlen, const u_char * buf, u_char * eccbuf,
203 struct nand_oobinfo *oobsel)
205 struct mtd_concat *concat = CONCAT(mtd);
209 if (!(mtd->flags & MTD_WRITEABLE))
214 for (i = 0; i < concat->num_subdev; i++) {
215 struct mtd_info *subdev = concat->subdev[i];
216 size_t size, retsize;
218 if (to >= subdev->size) {
223 if (to + len > subdev->size)
224 size = subdev->size - to;
228 if (!(subdev->flags & MTD_WRITEABLE))
230 else if (subdev->write_ecc)
231 err = subdev->write_ecc(subdev, to, size,
232 &retsize, buf, eccbuf, oobsel);
247 eccbuf += subdev->oobsize;
254 concat_writev_ecc(struct mtd_info *mtd, const struct kvec *vecs,
255 unsigned long count, loff_t to, size_t * retlen,
256 u_char *eccbuf, struct nand_oobinfo *oobsel)
258 struct mtd_concat *concat = CONCAT(mtd);
259 struct kvec *vecs_copy;
260 unsigned long entry_low, entry_high;
261 size_t total_len = 0;
265 if (!(mtd->flags & MTD_WRITEABLE))
270 /* Calculate total length of data */
271 for (i = 0; i < count; i++)
272 total_len += vecs[i].iov_len;
274 /* Do not allow write past end of device */
275 if ((to + total_len) > mtd->size)
278 /* Check alignment */
279 if (mtd->oobblock > 1)
280 if ((to % mtd->oobblock) || (total_len % mtd->oobblock))
283 /* make a copy of vecs */
284 vecs_copy = kmalloc(sizeof(struct kvec) * count, GFP_KERNEL);
287 memcpy(vecs_copy, vecs, sizeof(struct kvec) * count);
290 for (i = 0; i < concat->num_subdev; i++) {
291 struct mtd_info *subdev = concat->subdev[i];
292 size_t size, wsize, retsize, old_iov_len;
294 if (to >= subdev->size) {
299 size = min(total_len, (size_t)(subdev->size - to));
300 wsize = size; /* store for future use */
302 entry_high = entry_low;
303 while (entry_high < count) {
304 if (size <= vecs_copy[entry_high].iov_len)
306 size -= vecs_copy[entry_high++].iov_len;
309 old_iov_len = vecs_copy[entry_high].iov_len;
310 vecs_copy[entry_high].iov_len = size;
312 if (!(subdev->flags & MTD_WRITEABLE))
315 err = subdev->writev_ecc(subdev, &vecs_copy[entry_low],
316 entry_high - entry_low + 1, to, &retsize,
319 err = subdev->writev(subdev, &vecs_copy[entry_low],
320 entry_high - entry_low + 1, to, &retsize);
322 vecs_copy[entry_high].iov_len = old_iov_len - size;
323 vecs_copy[entry_high].iov_base += size;
325 entry_low = entry_high;
332 if (concat->mtd.type == MTD_NANDFLASH && eccbuf)
333 eccbuf += mtd->oobavail * (wsize / mtd->oobblock);
347 concat_writev(struct mtd_info *mtd, const struct kvec *vecs,
348 unsigned long count, loff_t to, size_t * retlen)
350 return concat_writev_ecc(mtd, vecs, count, to, retlen, NULL, NULL);
354 concat_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
355 size_t * retlen, u_char * buf)
357 struct mtd_concat *concat = CONCAT(mtd);
363 for (i = 0; i < concat->num_subdev; i++) {
364 struct mtd_info *subdev = concat->subdev[i];
365 size_t size, retsize;
367 if (from >= subdev->size) {
368 /* Not destined for this subdev */
370 from -= subdev->size;
373 if (from + len > subdev->size)
374 /* First part goes into this subdev */
375 size = subdev->size - from;
377 /* Entire transaction goes into this subdev */
380 if (subdev->read_oob)
381 err = subdev->read_oob(subdev, from, size,
402 concat_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
403 size_t * retlen, const u_char * buf)
405 struct mtd_concat *concat = CONCAT(mtd);
409 if (!(mtd->flags & MTD_WRITEABLE))
414 for (i = 0; i < concat->num_subdev; i++) {
415 struct mtd_info *subdev = concat->subdev[i];
416 size_t size, retsize;
418 if (to >= subdev->size) {
423 if (to + len > subdev->size)
424 size = subdev->size - to;
428 if (!(subdev->flags & MTD_WRITEABLE))
430 else if (subdev->write_oob)
431 err = subdev->write_oob(subdev, to, size, &retsize,
451 static void concat_erase_callback(struct erase_info *instr)
453 wake_up((wait_queue_head_t *) instr->priv);
456 static int concat_dev_erase(struct mtd_info *mtd, struct erase_info *erase)
459 wait_queue_head_t waitq;
460 DECLARE_WAITQUEUE(wait, current);
463 * This code was stol^H^H^H^Hinspired by mtdchar.c
465 init_waitqueue_head(&waitq);
468 erase->callback = concat_erase_callback;
469 erase->priv = (unsigned long) &waitq;
472 * FIXME: Allow INTERRUPTIBLE. Which means
473 * not having the wait_queue head on the stack.
475 err = mtd->erase(mtd, erase);
477 set_current_state(TASK_UNINTERRUPTIBLE);
478 add_wait_queue(&waitq, &wait);
479 if (erase->state != MTD_ERASE_DONE
480 && erase->state != MTD_ERASE_FAILED)
482 remove_wait_queue(&waitq, &wait);
483 set_current_state(TASK_RUNNING);
485 err = (erase->state == MTD_ERASE_FAILED) ? -EIO : 0;
490 static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
492 struct mtd_concat *concat = CONCAT(mtd);
493 struct mtd_info *subdev;
495 u_int32_t length, offset = 0;
496 struct erase_info *erase;
498 if (!(mtd->flags & MTD_WRITEABLE))
501 if (instr->addr > concat->mtd.size)
504 if (instr->len + instr->addr > concat->mtd.size)
508 * Check for proper erase block alignment of the to-be-erased area.
509 * It is easier to do this based on the super device's erase
510 * region info rather than looking at each particular sub-device
513 if (!concat->mtd.numeraseregions) {
514 /* the easy case: device has uniform erase block size */
515 if (instr->addr & (concat->mtd.erasesize - 1))
517 if (instr->len & (concat->mtd.erasesize - 1))
520 /* device has variable erase size */
521 struct mtd_erase_region_info *erase_regions =
522 concat->mtd.eraseregions;
525 * Find the erase region where the to-be-erased area begins:
527 for (i = 0; i < concat->mtd.numeraseregions &&
528 instr->addr >= erase_regions[i].offset; i++) ;
532 * Now erase_regions[i] is the region in which the
533 * to-be-erased area begins. Verify that the starting
534 * offset is aligned to this region's erase size:
536 if (instr->addr & (erase_regions[i].erasesize - 1))
540 * now find the erase region where the to-be-erased area ends:
542 for (; i < concat->mtd.numeraseregions &&
543 (instr->addr + instr->len) >= erase_regions[i].offset;
547 * check if the ending offset is aligned to this region's erase size
549 if ((instr->addr + instr->len) & (erase_regions[i].erasesize -
554 instr->fail_addr = 0xffffffff;
556 /* make a local copy of instr to avoid modifying the caller's struct */
557 erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
566 * find the subdevice where the to-be-erased area begins, adjust
567 * starting offset to be relative to the subdevice start
569 for (i = 0; i < concat->num_subdev; i++) {
570 subdev = concat->subdev[i];
571 if (subdev->size <= erase->addr) {
572 erase->addr -= subdev->size;
573 offset += subdev->size;
579 /* must never happen since size limit has been verified above */
580 BUG_ON(i >= concat->num_subdev);
582 /* now do the erase: */
584 for (; length > 0; i++) {
585 /* loop for all subdevices affected by this request */
586 subdev = concat->subdev[i]; /* get current subdevice */
588 /* limit length to subdevice's size: */
589 if (erase->addr + length > subdev->size)
590 erase->len = subdev->size - erase->addr;
594 if (!(subdev->flags & MTD_WRITEABLE)) {
598 length -= erase->len;
599 if ((err = concat_dev_erase(subdev, erase))) {
600 /* sanity check: should never happen since
601 * block alignment has been checked above */
602 BUG_ON(err == -EINVAL);
603 if (erase->fail_addr != 0xffffffff)
604 instr->fail_addr = erase->fail_addr + offset;
608 * erase->addr specifies the offset of the area to be
609 * erased *within the current subdevice*. It can be
610 * non-zero only the first time through this loop, i.e.
611 * for the first subdevice where blocks need to be erased.
612 * All the following erases must begin at the start of the
613 * current subdevice, i.e. at offset zero.
616 offset += subdev->size;
618 instr->state = erase->state;
624 instr->callback(instr);
628 static int concat_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
630 struct mtd_concat *concat = CONCAT(mtd);
631 int i, err = -EINVAL;
633 if ((len + ofs) > mtd->size)
636 for (i = 0; i < concat->num_subdev; i++) {
637 struct mtd_info *subdev = concat->subdev[i];
640 if (ofs >= subdev->size) {
645 if (ofs + len > subdev->size)
646 size = subdev->size - ofs;
650 err = subdev->lock(subdev, ofs, size);
666 static int concat_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
668 struct mtd_concat *concat = CONCAT(mtd);
671 if ((len + ofs) > mtd->size)
674 for (i = 0; i < concat->num_subdev; i++) {
675 struct mtd_info *subdev = concat->subdev[i];
678 if (ofs >= subdev->size) {
683 if (ofs + len > subdev->size)
684 size = subdev->size - ofs;
688 err = subdev->unlock(subdev, ofs, size);
704 static void concat_sync(struct mtd_info *mtd)
706 struct mtd_concat *concat = CONCAT(mtd);
709 for (i = 0; i < concat->num_subdev; i++) {
710 struct mtd_info *subdev = concat->subdev[i];
711 subdev->sync(subdev);
715 static int concat_suspend(struct mtd_info *mtd)
717 struct mtd_concat *concat = CONCAT(mtd);
720 for (i = 0; i < concat->num_subdev; i++) {
721 struct mtd_info *subdev = concat->subdev[i];
722 if ((rc = subdev->suspend(subdev)) < 0)
728 static void concat_resume(struct mtd_info *mtd)
730 struct mtd_concat *concat = CONCAT(mtd);
733 for (i = 0; i < concat->num_subdev; i++) {
734 struct mtd_info *subdev = concat->subdev[i];
735 subdev->resume(subdev);
739 static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
741 struct mtd_concat *concat = CONCAT(mtd);
744 if (!concat->subdev[0]->block_isbad)
750 for (i = 0; i < concat->num_subdev; i++) {
751 struct mtd_info *subdev = concat->subdev[i];
753 if (ofs >= subdev->size) {
758 res = subdev->block_isbad(subdev, ofs);
765 static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
767 struct mtd_concat *concat = CONCAT(mtd);
768 int i, err = -EINVAL;
770 if (!concat->subdev[0]->block_markbad)
776 for (i = 0; i < concat->num_subdev; i++) {
777 struct mtd_info *subdev = concat->subdev[i];
779 if (ofs >= subdev->size) {
784 err = subdev->block_markbad(subdev, ofs);
792 * This function constructs a virtual MTD device by concatenating
793 * num_devs MTD devices. A pointer to the new device object is
794 * stored to *new_dev upon success. This function does _not_
795 * register any devices: this is the caller's responsibility.
797 struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */
798 int num_devs, /* number of subdevices */
800 { /* name for the new device */
803 struct mtd_concat *concat;
804 u_int32_t max_erasesize, curr_erasesize;
805 int num_erase_region;
807 printk(KERN_NOTICE "Concatenating MTD devices:\n");
808 for (i = 0; i < num_devs; i++)
809 printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
810 printk(KERN_NOTICE "into device \"%s\"\n", name);
812 /* allocate the device structure */
813 size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
814 concat = kmalloc(size, GFP_KERNEL);
817 ("memory allocation error while creating concatenated device \"%s\"\n",
821 memset(concat, 0, size);
822 concat->subdev = (struct mtd_info **) (concat + 1);
825 * Set up the new "super" device's MTD object structure, check for
826 * incompatibilites between the subdevices.
828 concat->mtd.type = subdev[0]->type;
829 concat->mtd.flags = subdev[0]->flags;
830 concat->mtd.size = subdev[0]->size;
831 concat->mtd.erasesize = subdev[0]->erasesize;
832 concat->mtd.oobblock = subdev[0]->oobblock;
833 concat->mtd.oobsize = subdev[0]->oobsize;
834 concat->mtd.ecctype = subdev[0]->ecctype;
835 concat->mtd.eccsize = subdev[0]->eccsize;
836 if (subdev[0]->read_ecc)
837 concat->mtd.read_ecc = concat_read_ecc;
838 if (subdev[0]->write_ecc)
839 concat->mtd.write_ecc = concat_write_ecc;
840 if (subdev[0]->writev)
841 concat->mtd.writev = concat_writev;
842 if (subdev[0]->writev_ecc)
843 concat->mtd.writev_ecc = concat_writev_ecc;
844 if (subdev[0]->read_oob)
845 concat->mtd.read_oob = concat_read_oob;
846 if (subdev[0]->write_oob)
847 concat->mtd.write_oob = concat_write_oob;
848 if (subdev[0]->block_isbad)
849 concat->mtd.block_isbad = concat_block_isbad;
850 if (subdev[0]->block_markbad)
851 concat->mtd.block_markbad = concat_block_markbad;
853 concat->subdev[0] = subdev[0];
855 for (i = 1; i < num_devs; i++) {
856 if (concat->mtd.type != subdev[i]->type) {
858 printk("Incompatible device type on \"%s\"\n",
862 if (concat->mtd.flags != subdev[i]->flags) {
864 * Expect all flags except MTD_WRITEABLE to be
865 * equal on all subdevices.
867 if ((concat->mtd.flags ^ subdev[i]->
868 flags) & ~MTD_WRITEABLE) {
870 printk("Incompatible device flags on \"%s\"\n",
874 /* if writeable attribute differs,
875 make super device writeable */
877 subdev[i]->flags & MTD_WRITEABLE;
879 concat->mtd.size += subdev[i]->size;
880 if (concat->mtd.oobblock != subdev[i]->oobblock ||
881 concat->mtd.oobsize != subdev[i]->oobsize ||
882 concat->mtd.ecctype != subdev[i]->ecctype ||
883 concat->mtd.eccsize != subdev[i]->eccsize ||
884 !concat->mtd.read_ecc != !subdev[i]->read_ecc ||
885 !concat->mtd.write_ecc != !subdev[i]->write_ecc ||
886 !concat->mtd.read_oob != !subdev[i]->read_oob ||
887 !concat->mtd.write_oob != !subdev[i]->write_oob) {
889 printk("Incompatible OOB or ECC data on \"%s\"\n",
893 concat->subdev[i] = subdev[i];
897 if(concat->mtd.type == MTD_NANDFLASH)
898 memcpy(&concat->mtd.oobinfo, &subdev[0]->oobinfo,
899 sizeof(struct nand_oobinfo));
901 concat->num_subdev = num_devs;
902 concat->mtd.name = name;
904 concat->mtd.erase = concat_erase;
905 concat->mtd.read = concat_read;
906 concat->mtd.write = concat_write;
907 concat->mtd.sync = concat_sync;
908 concat->mtd.lock = concat_lock;
909 concat->mtd.unlock = concat_unlock;
910 concat->mtd.suspend = concat_suspend;
911 concat->mtd.resume = concat_resume;
914 * Combine the erase block size info of the subdevices:
916 * first, walk the map of the new device and see how
917 * many changes in erase size we have
919 max_erasesize = curr_erasesize = subdev[0]->erasesize;
920 num_erase_region = 1;
921 for (i = 0; i < num_devs; i++) {
922 if (subdev[i]->numeraseregions == 0) {
923 /* current subdevice has uniform erase size */
924 if (subdev[i]->erasesize != curr_erasesize) {
925 /* if it differs from the last subdevice's erase size, count it */
927 curr_erasesize = subdev[i]->erasesize;
928 if (curr_erasesize > max_erasesize)
929 max_erasesize = curr_erasesize;
932 /* current subdevice has variable erase size */
934 for (j = 0; j < subdev[i]->numeraseregions; j++) {
936 /* walk the list of erase regions, count any changes */
937 if (subdev[i]->eraseregions[j].erasesize !=
941 subdev[i]->eraseregions[j].
943 if (curr_erasesize > max_erasesize)
944 max_erasesize = curr_erasesize;
950 if (num_erase_region == 1) {
952 * All subdevices have the same uniform erase size.
955 concat->mtd.erasesize = curr_erasesize;
956 concat->mtd.numeraseregions = 0;
959 * erase block size varies across the subdevices: allocate
960 * space to store the data describing the variable erase regions
962 struct mtd_erase_region_info *erase_region_p;
963 u_int32_t begin, position;
965 concat->mtd.erasesize = max_erasesize;
966 concat->mtd.numeraseregions = num_erase_region;
967 concat->mtd.eraseregions = erase_region_p =
968 kmalloc(num_erase_region *
969 sizeof (struct mtd_erase_region_info), GFP_KERNEL);
970 if (!erase_region_p) {
973 ("memory allocation error while creating erase region list"
974 " for device \"%s\"\n", name);
979 * walk the map of the new device once more and fill in
980 * in erase region info:
982 curr_erasesize = subdev[0]->erasesize;
983 begin = position = 0;
984 for (i = 0; i < num_devs; i++) {
985 if (subdev[i]->numeraseregions == 0) {
986 /* current subdevice has uniform erase size */
987 if (subdev[i]->erasesize != curr_erasesize) {
989 * fill in an mtd_erase_region_info structure for the area
990 * we have walked so far:
992 erase_region_p->offset = begin;
993 erase_region_p->erasesize =
995 erase_region_p->numblocks =
996 (position - begin) / curr_erasesize;
999 curr_erasesize = subdev[i]->erasesize;
1002 position += subdev[i]->size;
1004 /* current subdevice has variable erase size */
1006 for (j = 0; j < subdev[i]->numeraseregions; j++) {
1007 /* walk the list of erase regions, count any changes */
1008 if (subdev[i]->eraseregions[j].
1009 erasesize != curr_erasesize) {
1010 erase_region_p->offset = begin;
1011 erase_region_p->erasesize =
1013 erase_region_p->numblocks =
1015 begin) / curr_erasesize;
1019 subdev[i]->eraseregions[j].
1024 subdev[i]->eraseregions[j].
1025 numblocks * curr_erasesize;
1029 /* Now write the final entry */
1030 erase_region_p->offset = begin;
1031 erase_region_p->erasesize = curr_erasesize;
1032 erase_region_p->numblocks = (position - begin) / curr_erasesize;
1035 return &concat->mtd;
1039 * This function destroys an MTD object obtained from concat_mtd_devs()
1042 void mtd_concat_destroy(struct mtd_info *mtd)
1044 struct mtd_concat *concat = CONCAT(mtd);
1045 if (concat->mtd.numeraseregions)
1046 kfree(concat->mtd.eraseregions);
1050 EXPORT_SYMBOL(mtd_concat_create);
1051 EXPORT_SYMBOL(mtd_concat_destroy);
1053 MODULE_LICENSE("GPL");
1054 MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
1055 MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");