2 * MTD device concatenation layer
4 * (C) 2002 Robert Kaiser <rkaiser@sysgo.de>
6 * NAND support by Christian Gan <cgan@iders.ca>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/sched.h>
15 #include <linux/types.h>
16 #include <linux/backing-dev.h>
18 #include <linux/mtd/mtd.h>
19 #include <linux/mtd/concat.h>
21 #include <asm/div64.h>
24 * Our storage structure:
25 * Subdev points to an array of pointers to struct mtd_info objects
26 * which is allocated along with this structure
32 struct mtd_info **subdev;
36 * how to calculate the size required for the above structure,
37 * including the pointer array subdev points to:
39 #define SIZEOF_STRUCT_MTD_CONCAT(num_subdev) \
40 ((sizeof(struct mtd_concat) + (num_subdev) * sizeof(struct mtd_info *)))
43 * Given a pointer to the MTD object in the mtd_concat structure,
44 * we can retrieve the pointer to that structure with this macro.
46 #define CONCAT(x) ((struct mtd_concat *)(x))
49 * MTD methods which look up the relevant subdevice, translate the
50 * effective address and pass through to the subdevice.
54 concat_read(struct mtd_info *mtd, loff_t from, size_t len,
55 size_t * retlen, u_char * buf)
57 struct mtd_concat *concat = CONCAT(mtd);
63 for (i = 0; i < concat->num_subdev; i++) {
64 struct mtd_info *subdev = concat->subdev[i];
67 if (from >= subdev->size) {
68 /* Not destined for this subdev */
73 if (from + len > subdev->size)
74 /* First part goes into this subdev */
75 size = subdev->size - from;
77 /* Entire transaction goes into this subdev */
80 err = subdev->read(subdev, from, size, &retsize, buf);
82 /* Save information about bitflips! */
84 if (err == -EBADMSG) {
85 mtd->ecc_stats.failed++;
87 } else if (err == -EUCLEAN) {
88 mtd->ecc_stats.corrected++;
89 /* Do not overwrite -EBADMSG !! */
108 concat_write(struct mtd_info *mtd, loff_t to, size_t len,
109 size_t * retlen, const u_char * buf)
111 struct mtd_concat *concat = CONCAT(mtd);
115 if (!(mtd->flags & MTD_WRITEABLE))
120 for (i = 0; i < concat->num_subdev; i++) {
121 struct mtd_info *subdev = concat->subdev[i];
122 size_t size, retsize;
124 if (to >= subdev->size) {
129 if (to + len > subdev->size)
130 size = subdev->size - to;
134 if (!(subdev->flags & MTD_WRITEABLE))
137 err = subdev->write(subdev, to, size, &retsize, buf);
155 concat_writev(struct mtd_info *mtd, const struct kvec *vecs,
156 unsigned long count, loff_t to, size_t * retlen)
158 struct mtd_concat *concat = CONCAT(mtd);
159 struct kvec *vecs_copy;
160 unsigned long entry_low, entry_high;
161 size_t total_len = 0;
165 if (!(mtd->flags & MTD_WRITEABLE))
170 /* Calculate total length of data */
171 for (i = 0; i < count; i++)
172 total_len += vecs[i].iov_len;
174 /* Do not allow write past end of device */
175 if ((to + total_len) > mtd->size)
178 /* Check alignment */
179 if (mtd->writesize > 1) {
181 if (do_div(__to, mtd->writesize) || (total_len % mtd->writesize))
185 /* make a copy of vecs */
186 vecs_copy = kmemdup(vecs, sizeof(struct kvec) * count, GFP_KERNEL);
191 for (i = 0; i < concat->num_subdev; i++) {
192 struct mtd_info *subdev = concat->subdev[i];
193 size_t size, wsize, retsize, old_iov_len;
195 if (to >= subdev->size) {
200 size = min_t(uint64_t, total_len, subdev->size - to);
201 wsize = size; /* store for future use */
203 entry_high = entry_low;
204 while (entry_high < count) {
205 if (size <= vecs_copy[entry_high].iov_len)
207 size -= vecs_copy[entry_high++].iov_len;
210 old_iov_len = vecs_copy[entry_high].iov_len;
211 vecs_copy[entry_high].iov_len = size;
213 if (!(subdev->flags & MTD_WRITEABLE))
216 err = subdev->writev(subdev, &vecs_copy[entry_low],
217 entry_high - entry_low + 1, to, &retsize);
219 vecs_copy[entry_high].iov_len = old_iov_len - size;
220 vecs_copy[entry_high].iov_base += size;
222 entry_low = entry_high;
242 concat_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
244 struct mtd_concat *concat = CONCAT(mtd);
245 struct mtd_oob_ops devops = *ops;
248 ops->retlen = ops->oobretlen = 0;
250 for (i = 0; i < concat->num_subdev; i++) {
251 struct mtd_info *subdev = concat->subdev[i];
253 if (from >= subdev->size) {
254 from -= subdev->size;
259 if (from + devops.len > subdev->size)
260 devops.len = subdev->size - from;
262 err = subdev->read_oob(subdev, from, &devops);
263 ops->retlen += devops.retlen;
264 ops->oobretlen += devops.oobretlen;
266 /* Save information about bitflips! */
268 if (err == -EBADMSG) {
269 mtd->ecc_stats.failed++;
271 } else if (err == -EUCLEAN) {
272 mtd->ecc_stats.corrected++;
273 /* Do not overwrite -EBADMSG !! */
281 devops.len = ops->len - ops->retlen;
284 devops.datbuf += devops.retlen;
287 devops.ooblen = ops->ooblen - ops->oobretlen;
290 devops.oobbuf += ops->oobretlen;
299 concat_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
301 struct mtd_concat *concat = CONCAT(mtd);
302 struct mtd_oob_ops devops = *ops;
305 if (!(mtd->flags & MTD_WRITEABLE))
310 for (i = 0; i < concat->num_subdev; i++) {
311 struct mtd_info *subdev = concat->subdev[i];
313 if (to >= subdev->size) {
318 /* partial write ? */
319 if (to + devops.len > subdev->size)
320 devops.len = subdev->size - to;
322 err = subdev->write_oob(subdev, to, &devops);
323 ops->retlen += devops.retlen;
328 devops.len = ops->len - ops->retlen;
331 devops.datbuf += devops.retlen;
334 devops.ooblen = ops->ooblen - ops->oobretlen;
337 devops.oobbuf += devops.oobretlen;
344 static void concat_erase_callback(struct erase_info *instr)
346 wake_up((wait_queue_head_t *) instr->priv);
349 static int concat_dev_erase(struct mtd_info *mtd, struct erase_info *erase)
352 wait_queue_head_t waitq;
353 DECLARE_WAITQUEUE(wait, current);
356 * This code was stol^H^H^H^Hinspired by mtdchar.c
358 init_waitqueue_head(&waitq);
361 erase->callback = concat_erase_callback;
362 erase->priv = (unsigned long) &waitq;
365 * FIXME: Allow INTERRUPTIBLE. Which means
366 * not having the wait_queue head on the stack.
368 err = mtd->erase(mtd, erase);
370 set_current_state(TASK_UNINTERRUPTIBLE);
371 add_wait_queue(&waitq, &wait);
372 if (erase->state != MTD_ERASE_DONE
373 && erase->state != MTD_ERASE_FAILED)
375 remove_wait_queue(&waitq, &wait);
376 set_current_state(TASK_RUNNING);
378 err = (erase->state == MTD_ERASE_FAILED) ? -EIO : 0;
383 static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
385 struct mtd_concat *concat = CONCAT(mtd);
386 struct mtd_info *subdev;
388 uint64_t length, offset = 0;
389 struct erase_info *erase;
391 if (!(mtd->flags & MTD_WRITEABLE))
394 if (instr->addr > concat->mtd.size)
397 if (instr->len + instr->addr > concat->mtd.size)
401 * Check for proper erase block alignment of the to-be-erased area.
402 * It is easier to do this based on the super device's erase
403 * region info rather than looking at each particular sub-device
406 if (!concat->mtd.numeraseregions) {
407 /* the easy case: device has uniform erase block size */
408 if (instr->addr & (concat->mtd.erasesize - 1))
410 if (instr->len & (concat->mtd.erasesize - 1))
413 /* device has variable erase size */
414 struct mtd_erase_region_info *erase_regions =
415 concat->mtd.eraseregions;
418 * Find the erase region where the to-be-erased area begins:
420 for (i = 0; i < concat->mtd.numeraseregions &&
421 instr->addr >= erase_regions[i].offset; i++) ;
425 * Now erase_regions[i] is the region in which the
426 * to-be-erased area begins. Verify that the starting
427 * offset is aligned to this region's erase size:
429 if (i < 0 || instr->addr & (erase_regions[i].erasesize - 1))
433 * now find the erase region where the to-be-erased area ends:
435 for (; i < concat->mtd.numeraseregions &&
436 (instr->addr + instr->len) >= erase_regions[i].offset;
440 * check if the ending offset is aligned to this region's erase size
442 if (i < 0 || ((instr->addr + instr->len) &
443 (erase_regions[i].erasesize - 1)))
447 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
449 /* make a local copy of instr to avoid modifying the caller's struct */
450 erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
459 * find the subdevice where the to-be-erased area begins, adjust
460 * starting offset to be relative to the subdevice start
462 for (i = 0; i < concat->num_subdev; i++) {
463 subdev = concat->subdev[i];
464 if (subdev->size <= erase->addr) {
465 erase->addr -= subdev->size;
466 offset += subdev->size;
472 /* must never happen since size limit has been verified above */
473 BUG_ON(i >= concat->num_subdev);
475 /* now do the erase: */
477 for (; length > 0; i++) {
478 /* loop for all subdevices affected by this request */
479 subdev = concat->subdev[i]; /* get current subdevice */
481 /* limit length to subdevice's size: */
482 if (erase->addr + length > subdev->size)
483 erase->len = subdev->size - erase->addr;
487 if (!(subdev->flags & MTD_WRITEABLE)) {
491 length -= erase->len;
492 if ((err = concat_dev_erase(subdev, erase))) {
493 /* sanity check: should never happen since
494 * block alignment has been checked above */
495 BUG_ON(err == -EINVAL);
496 if (erase->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
497 instr->fail_addr = erase->fail_addr + offset;
501 * erase->addr specifies the offset of the area to be
502 * erased *within the current subdevice*. It can be
503 * non-zero only the first time through this loop, i.e.
504 * for the first subdevice where blocks need to be erased.
505 * All the following erases must begin at the start of the
506 * current subdevice, i.e. at offset zero.
509 offset += subdev->size;
511 instr->state = erase->state;
517 instr->callback(instr);
521 static int concat_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
523 struct mtd_concat *concat = CONCAT(mtd);
524 int i, err = -EINVAL;
526 if ((len + ofs) > mtd->size)
529 for (i = 0; i < concat->num_subdev; i++) {
530 struct mtd_info *subdev = concat->subdev[i];
533 if (ofs >= subdev->size) {
538 if (ofs + len > subdev->size)
539 size = subdev->size - ofs;
543 err = subdev->lock(subdev, ofs, size);
559 static int concat_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
561 struct mtd_concat *concat = CONCAT(mtd);
564 if ((len + ofs) > mtd->size)
567 for (i = 0; i < concat->num_subdev; i++) {
568 struct mtd_info *subdev = concat->subdev[i];
571 if (ofs >= subdev->size) {
576 if (ofs + len > subdev->size)
577 size = subdev->size - ofs;
581 err = subdev->unlock(subdev, ofs, size);
597 static void concat_sync(struct mtd_info *mtd)
599 struct mtd_concat *concat = CONCAT(mtd);
602 for (i = 0; i < concat->num_subdev; i++) {
603 struct mtd_info *subdev = concat->subdev[i];
604 subdev->sync(subdev);
608 static int concat_suspend(struct mtd_info *mtd)
610 struct mtd_concat *concat = CONCAT(mtd);
613 for (i = 0; i < concat->num_subdev; i++) {
614 struct mtd_info *subdev = concat->subdev[i];
615 if ((rc = subdev->suspend(subdev)) < 0)
621 static void concat_resume(struct mtd_info *mtd)
623 struct mtd_concat *concat = CONCAT(mtd);
626 for (i = 0; i < concat->num_subdev; i++) {
627 struct mtd_info *subdev = concat->subdev[i];
628 subdev->resume(subdev);
632 static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
634 struct mtd_concat *concat = CONCAT(mtd);
637 if (!concat->subdev[0]->block_isbad)
643 for (i = 0; i < concat->num_subdev; i++) {
644 struct mtd_info *subdev = concat->subdev[i];
646 if (ofs >= subdev->size) {
651 res = subdev->block_isbad(subdev, ofs);
658 static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
660 struct mtd_concat *concat = CONCAT(mtd);
661 int i, err = -EINVAL;
663 if (!concat->subdev[0]->block_markbad)
669 for (i = 0; i < concat->num_subdev; i++) {
670 struct mtd_info *subdev = concat->subdev[i];
672 if (ofs >= subdev->size) {
677 err = subdev->block_markbad(subdev, ofs);
679 mtd->ecc_stats.badblocks++;
687 * try to support NOMMU mmaps on concatenated devices
688 * - we don't support subdev spanning as we can't guarantee it'll work
690 static unsigned long concat_get_unmapped_area(struct mtd_info *mtd,
692 unsigned long offset,
695 struct mtd_concat *concat = CONCAT(mtd);
698 for (i = 0; i < concat->num_subdev; i++) {
699 struct mtd_info *subdev = concat->subdev[i];
701 if (offset >= subdev->size) {
702 offset -= subdev->size;
706 /* we've found the subdev over which the mapping will reside */
707 if (offset + len > subdev->size)
708 return (unsigned long) -EINVAL;
710 if (subdev->get_unmapped_area)
711 return subdev->get_unmapped_area(subdev, len, offset,
717 return (unsigned long) -ENOSYS;
721 * This function constructs a virtual MTD device by concatenating
722 * num_devs MTD devices. A pointer to the new device object is
723 * stored to *new_dev upon success. This function does _not_
724 * register any devices: this is the caller's responsibility.
726 struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */
727 int num_devs, /* number of subdevices */
729 { /* name for the new device */
732 struct mtd_concat *concat;
733 uint32_t max_erasesize, curr_erasesize;
734 int num_erase_region;
736 printk(KERN_NOTICE "Concatenating MTD devices:\n");
737 for (i = 0; i < num_devs; i++)
738 printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
739 printk(KERN_NOTICE "into device \"%s\"\n", name);
741 /* allocate the device structure */
742 size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
743 concat = kzalloc(size, GFP_KERNEL);
746 ("memory allocation error while creating concatenated device \"%s\"\n",
750 concat->subdev = (struct mtd_info **) (concat + 1);
753 * Set up the new "super" device's MTD object structure, check for
754 * incompatibilites between the subdevices.
756 concat->mtd.type = subdev[0]->type;
757 concat->mtd.flags = subdev[0]->flags;
758 concat->mtd.size = subdev[0]->size;
759 concat->mtd.erasesize = subdev[0]->erasesize;
760 concat->mtd.writesize = subdev[0]->writesize;
761 concat->mtd.subpage_sft = subdev[0]->subpage_sft;
762 concat->mtd.oobsize = subdev[0]->oobsize;
763 concat->mtd.oobavail = subdev[0]->oobavail;
764 if (subdev[0]->writev)
765 concat->mtd.writev = concat_writev;
766 if (subdev[0]->read_oob)
767 concat->mtd.read_oob = concat_read_oob;
768 if (subdev[0]->write_oob)
769 concat->mtd.write_oob = concat_write_oob;
770 if (subdev[0]->block_isbad)
771 concat->mtd.block_isbad = concat_block_isbad;
772 if (subdev[0]->block_markbad)
773 concat->mtd.block_markbad = concat_block_markbad;
775 concat->mtd.ecc_stats.badblocks = subdev[0]->ecc_stats.badblocks;
777 concat->mtd.backing_dev_info = subdev[0]->backing_dev_info;
779 concat->subdev[0] = subdev[0];
781 for (i = 1; i < num_devs; i++) {
782 if (concat->mtd.type != subdev[i]->type) {
784 printk("Incompatible device type on \"%s\"\n",
788 if (concat->mtd.flags != subdev[i]->flags) {
790 * Expect all flags except MTD_WRITEABLE to be
791 * equal on all subdevices.
793 if ((concat->mtd.flags ^ subdev[i]->
794 flags) & ~MTD_WRITEABLE) {
796 printk("Incompatible device flags on \"%s\"\n",
800 /* if writeable attribute differs,
801 make super device writeable */
803 subdev[i]->flags & MTD_WRITEABLE;
806 /* only permit direct mapping if the BDIs are all the same
807 * - copy-mapping is still permitted
809 if (concat->mtd.backing_dev_info !=
810 subdev[i]->backing_dev_info)
811 concat->mtd.backing_dev_info =
812 &default_backing_dev_info;
814 concat->mtd.size += subdev[i]->size;
815 concat->mtd.ecc_stats.badblocks +=
816 subdev[i]->ecc_stats.badblocks;
817 if (concat->mtd.writesize != subdev[i]->writesize ||
818 concat->mtd.subpage_sft != subdev[i]->subpage_sft ||
819 concat->mtd.oobsize != subdev[i]->oobsize ||
820 !concat->mtd.read_oob != !subdev[i]->read_oob ||
821 !concat->mtd.write_oob != !subdev[i]->write_oob) {
823 printk("Incompatible OOB or ECC data on \"%s\"\n",
827 concat->subdev[i] = subdev[i];
831 concat->mtd.ecclayout = subdev[0]->ecclayout;
833 concat->num_subdev = num_devs;
834 concat->mtd.name = name;
836 concat->mtd.erase = concat_erase;
837 concat->mtd.read = concat_read;
838 concat->mtd.write = concat_write;
839 concat->mtd.sync = concat_sync;
840 concat->mtd.lock = concat_lock;
841 concat->mtd.unlock = concat_unlock;
842 concat->mtd.suspend = concat_suspend;
843 concat->mtd.resume = concat_resume;
844 concat->mtd.get_unmapped_area = concat_get_unmapped_area;
847 * Combine the erase block size info of the subdevices:
849 * first, walk the map of the new device and see how
850 * many changes in erase size we have
852 max_erasesize = curr_erasesize = subdev[0]->erasesize;
853 num_erase_region = 1;
854 for (i = 0; i < num_devs; i++) {
855 if (subdev[i]->numeraseregions == 0) {
856 /* current subdevice has uniform erase size */
857 if (subdev[i]->erasesize != curr_erasesize) {
858 /* if it differs from the last subdevice's erase size, count it */
860 curr_erasesize = subdev[i]->erasesize;
861 if (curr_erasesize > max_erasesize)
862 max_erasesize = curr_erasesize;
865 /* current subdevice has variable erase size */
867 for (j = 0; j < subdev[i]->numeraseregions; j++) {
869 /* walk the list of erase regions, count any changes */
870 if (subdev[i]->eraseregions[j].erasesize !=
874 subdev[i]->eraseregions[j].
876 if (curr_erasesize > max_erasesize)
877 max_erasesize = curr_erasesize;
883 if (num_erase_region == 1) {
885 * All subdevices have the same uniform erase size.
888 concat->mtd.erasesize = curr_erasesize;
889 concat->mtd.numeraseregions = 0;
894 * erase block size varies across the subdevices: allocate
895 * space to store the data describing the variable erase regions
897 struct mtd_erase_region_info *erase_region_p;
898 uint64_t begin, position;
900 concat->mtd.erasesize = max_erasesize;
901 concat->mtd.numeraseregions = num_erase_region;
902 concat->mtd.eraseregions = erase_region_p =
903 kmalloc(num_erase_region *
904 sizeof (struct mtd_erase_region_info), GFP_KERNEL);
905 if (!erase_region_p) {
908 ("memory allocation error while creating erase region list"
909 " for device \"%s\"\n", name);
914 * walk the map of the new device once more and fill in
915 * in erase region info:
917 curr_erasesize = subdev[0]->erasesize;
918 begin = position = 0;
919 for (i = 0; i < num_devs; i++) {
920 if (subdev[i]->numeraseregions == 0) {
921 /* current subdevice has uniform erase size */
922 if (subdev[i]->erasesize != curr_erasesize) {
924 * fill in an mtd_erase_region_info structure for the area
925 * we have walked so far:
927 erase_region_p->offset = begin;
928 erase_region_p->erasesize =
930 tmp64 = position - begin;
931 do_div(tmp64, curr_erasesize);
932 erase_region_p->numblocks = tmp64;
935 curr_erasesize = subdev[i]->erasesize;
938 position += subdev[i]->size;
940 /* current subdevice has variable erase size */
942 for (j = 0; j < subdev[i]->numeraseregions; j++) {
943 /* walk the list of erase regions, count any changes */
944 if (subdev[i]->eraseregions[j].
945 erasesize != curr_erasesize) {
946 erase_region_p->offset = begin;
947 erase_region_p->erasesize =
949 tmp64 = position - begin;
950 do_div(tmp64, curr_erasesize);
951 erase_region_p->numblocks = tmp64;
955 subdev[i]->eraseregions[j].
960 subdev[i]->eraseregions[j].
961 numblocks * (uint64_t)curr_erasesize;
965 /* Now write the final entry */
966 erase_region_p->offset = begin;
967 erase_region_p->erasesize = curr_erasesize;
968 tmp64 = position - begin;
969 do_div(tmp64, curr_erasesize);
970 erase_region_p->numblocks = tmp64;
977 * This function destroys an MTD object obtained from concat_mtd_devs()
980 void mtd_concat_destroy(struct mtd_info *mtd)
982 struct mtd_concat *concat = CONCAT(mtd);
983 if (concat->mtd.numeraseregions)
984 kfree(concat->mtd.eraseregions);
988 EXPORT_SYMBOL(mtd_concat_create);
989 EXPORT_SYMBOL(mtd_concat_destroy);
991 MODULE_LICENSE("GPL");
992 MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
993 MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");