2 * MTD device concatenation layer
4 * Copyright © 2002 Robert Kaiser <rkaiser@sysgo.de>
5 * Copyright © 2002-2010 David Woodhouse <dwmw2@infradead.org>
7 * NAND support by Christian Gan <cgan@iders.ca>
9 * SPDX-License-Identifier: GPL-2.0+
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/sched.h>
19 #include <linux/types.h>
20 #include <linux/backing-dev.h>
21 #include <asm/div64.h>
24 #include <linux/compat.h>
27 #include <linux/mtd/mtd.h>
28 #include <linux/mtd/concat.h>
30 #include <ubi_uboot.h>
33 * Our storage structure:
34 * Subdev points to an array of pointers to struct mtd_info objects
35 * which is allocated along with this structure
41 struct mtd_info **subdev;
45 * how to calculate the size required for the above structure,
46 * including the pointer array subdev points to:
48 #define SIZEOF_STRUCT_MTD_CONCAT(num_subdev) \
49 ((sizeof(struct mtd_concat) + (num_subdev) * sizeof(struct mtd_info *)))
52 * Given a pointer to the MTD object in the mtd_concat structure,
53 * we can retrieve the pointer to that structure with this macro.
55 #define CONCAT(x) ((struct mtd_concat *)(x))
58 * MTD methods which look up the relevant subdevice, translate the
59 * effective address and pass through to the subdevice.
63 concat_read(struct mtd_info *mtd, loff_t from, size_t len,
64 size_t * retlen, u_char * buf)
66 struct mtd_concat *concat = CONCAT(mtd);
74 for (i = 0; i < concat->num_subdev; i++) {
75 struct mtd_info *subdev = concat->subdev[i];
78 if (from >= subdev->size) {
79 /* Not destined for this subdev */
84 if (from + len > subdev->size)
85 /* First part goes into this subdev */
86 size = subdev->size - from;
88 /* Entire transaction goes into this subdev */
91 err = mtd_read(subdev, from, size, &retsize, buf);
93 /* Save information about bitflips! */
95 if (mtd_is_eccerr(err)) {
96 mtd->ecc_stats.failed++;
98 } else if (mtd_is_bitflip(err)) {
99 mtd->ecc_stats.corrected++;
100 /* Do not overwrite -EBADMSG !! */
119 concat_write(struct mtd_info *mtd, loff_t to, size_t len,
120 size_t * retlen, const u_char * buf)
122 struct mtd_concat *concat = CONCAT(mtd);
130 for (i = 0; i < concat->num_subdev; i++) {
131 struct mtd_info *subdev = concat->subdev[i];
132 size_t size, retsize;
134 if (to >= subdev->size) {
139 if (to + len > subdev->size)
140 size = subdev->size - to;
144 err = mtd_write(subdev, to, size, &retsize, buf);
162 concat_writev(struct mtd_info *mtd, const struct kvec *vecs,
163 unsigned long count, loff_t to, size_t * retlen)
165 struct mtd_concat *concat = CONCAT(mtd);
166 struct kvec *vecs_copy;
167 unsigned long entry_low, entry_high;
168 size_t total_len = 0;
172 /* Calculate total length of data */
173 for (i = 0; i < count; i++)
174 total_len += vecs[i].iov_len;
176 /* Check alignment */
177 if (mtd->writesize > 1) {
179 if (do_div(__to, mtd->writesize) || (total_len % mtd->writesize))
183 /* make a copy of vecs */
184 vecs_copy = kmemdup(vecs, sizeof(struct kvec) * count, GFP_KERNEL);
189 for (i = 0; i < concat->num_subdev; i++) {
190 struct mtd_info *subdev = concat->subdev[i];
191 size_t size, wsize, retsize, old_iov_len;
193 if (to >= subdev->size) {
198 size = min_t(uint64_t, total_len, subdev->size - to);
199 wsize = size; /* store for future use */
201 entry_high = entry_low;
202 while (entry_high < count) {
203 if (size <= vecs_copy[entry_high].iov_len)
205 size -= vecs_copy[entry_high++].iov_len;
208 old_iov_len = vecs_copy[entry_high].iov_len;
209 vecs_copy[entry_high].iov_len = size;
211 err = mtd_writev(subdev, &vecs_copy[entry_low],
212 entry_high - entry_low + 1, to, &retsize);
214 vecs_copy[entry_high].iov_len = old_iov_len - size;
215 vecs_copy[entry_high].iov_base += size;
217 entry_low = entry_high;
238 concat_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
240 struct mtd_concat *concat = CONCAT(mtd);
241 struct mtd_oob_ops devops = *ops;
244 ops->retlen = ops->oobretlen = 0;
246 for (i = 0; i < concat->num_subdev; i++) {
247 struct mtd_info *subdev = concat->subdev[i];
249 if (from >= subdev->size) {
250 from -= subdev->size;
255 if (from + devops.len > subdev->size)
256 devops.len = subdev->size - from;
258 err = mtd_read_oob(subdev, from, &devops);
259 ops->retlen += devops.retlen;
260 ops->oobretlen += devops.oobretlen;
262 /* Save information about bitflips! */
264 if (mtd_is_eccerr(err)) {
265 mtd->ecc_stats.failed++;
267 } else if (mtd_is_bitflip(err)) {
268 mtd->ecc_stats.corrected++;
269 /* Do not overwrite -EBADMSG !! */
277 devops.len = ops->len - ops->retlen;
280 devops.datbuf += devops.retlen;
283 devops.ooblen = ops->ooblen - ops->oobretlen;
286 devops.oobbuf += ops->oobretlen;
295 concat_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
297 struct mtd_concat *concat = CONCAT(mtd);
298 struct mtd_oob_ops devops = *ops;
301 if (!(mtd->flags & MTD_WRITEABLE))
304 ops->retlen = ops->oobretlen = 0;
306 for (i = 0; i < concat->num_subdev; i++) {
307 struct mtd_info *subdev = concat->subdev[i];
309 if (to >= subdev->size) {
314 /* partial write ? */
315 if (to + devops.len > subdev->size)
316 devops.len = subdev->size - to;
318 err = mtd_write_oob(subdev, to, &devops);
319 ops->retlen += devops.oobretlen;
324 devops.len = ops->len - ops->retlen;
327 devops.datbuf += devops.retlen;
330 devops.ooblen = ops->ooblen - ops->oobretlen;
333 devops.oobbuf += devops.oobretlen;
340 static void concat_erase_callback(struct erase_info *instr)
342 /* Nothing to do here in U-Boot */
344 wake_up((wait_queue_head_t *) instr->priv);
348 static int concat_dev_erase(struct mtd_info *mtd, struct erase_info *erase)
351 wait_queue_head_t waitq;
352 DECLARE_WAITQUEUE(wait, current);
355 * This code was stol^H^H^H^Hinspired by mtdchar.c
357 init_waitqueue_head(&waitq);
360 erase->callback = concat_erase_callback;
361 erase->priv = (unsigned long) &waitq;
364 * FIXME: Allow INTERRUPTIBLE. Which means
365 * not having the wait_queue head on the stack.
367 err = mtd_erase(mtd, erase);
369 set_current_state(TASK_UNINTERRUPTIBLE);
370 add_wait_queue(&waitq, &wait);
371 if (erase->state != MTD_ERASE_DONE
372 && erase->state != MTD_ERASE_FAILED)
374 remove_wait_queue(&waitq, &wait);
375 set_current_state(TASK_RUNNING);
377 err = (erase->state == MTD_ERASE_FAILED) ? -EIO : 0;
382 static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
384 struct mtd_concat *concat = CONCAT(mtd);
385 struct mtd_info *subdev;
387 uint64_t length, offset = 0;
388 struct erase_info *erase;
391 * Check for proper erase block alignment of the to-be-erased area.
392 * It is easier to do this based on the super device's erase
393 * region info rather than looking at each particular sub-device
396 if (!concat->mtd.numeraseregions) {
397 /* the easy case: device has uniform erase block size */
398 if (instr->addr & (concat->mtd.erasesize - 1))
400 if (instr->len & (concat->mtd.erasesize - 1))
403 /* device has variable erase size */
404 struct mtd_erase_region_info *erase_regions =
405 concat->mtd.eraseregions;
408 * Find the erase region where the to-be-erased area begins:
410 for (i = 0; i < concat->mtd.numeraseregions &&
411 instr->addr >= erase_regions[i].offset; i++) ;
415 * Now erase_regions[i] is the region in which the
416 * to-be-erased area begins. Verify that the starting
417 * offset is aligned to this region's erase size:
419 if (i < 0 || instr->addr & (erase_regions[i].erasesize - 1))
423 * now find the erase region where the to-be-erased area ends:
425 for (; i < concat->mtd.numeraseregions &&
426 (instr->addr + instr->len) >= erase_regions[i].offset;
430 * check if the ending offset is aligned to this region's erase size
432 if (i < 0 || ((instr->addr + instr->len) &
433 (erase_regions[i].erasesize - 1)))
437 /* make a local copy of instr to avoid modifying the caller's struct */
438 erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
447 * find the subdevice where the to-be-erased area begins, adjust
448 * starting offset to be relative to the subdevice start
450 for (i = 0; i < concat->num_subdev; i++) {
451 subdev = concat->subdev[i];
452 if (subdev->size <= erase->addr) {
453 erase->addr -= subdev->size;
454 offset += subdev->size;
460 /* must never happen since size limit has been verified above */
461 BUG_ON(i >= concat->num_subdev);
463 /* now do the erase: */
465 for (; length > 0; i++) {
466 /* loop for all subdevices affected by this request */
467 subdev = concat->subdev[i]; /* get current subdevice */
469 /* limit length to subdevice's size: */
470 if (erase->addr + length > subdev->size)
471 erase->len = subdev->size - erase->addr;
475 length -= erase->len;
476 if ((err = concat_dev_erase(subdev, erase))) {
477 /* sanity check: should never happen since
478 * block alignment has been checked above */
479 BUG_ON(err == -EINVAL);
480 if (erase->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
481 instr->fail_addr = erase->fail_addr + offset;
485 * erase->addr specifies the offset of the area to be
486 * erased *within the current subdevice*. It can be
487 * non-zero only the first time through this loop, i.e.
488 * for the first subdevice where blocks need to be erased.
489 * All the following erases must begin at the start of the
490 * current subdevice, i.e. at offset zero.
493 offset += subdev->size;
495 instr->state = erase->state;
501 instr->callback(instr);
505 static int concat_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
507 struct mtd_concat *concat = CONCAT(mtd);
508 int i, err = -EINVAL;
510 for (i = 0; i < concat->num_subdev; i++) {
511 struct mtd_info *subdev = concat->subdev[i];
514 if (ofs >= subdev->size) {
519 if (ofs + len > subdev->size)
520 size = subdev->size - ofs;
524 err = mtd_lock(subdev, ofs, size);
539 static int concat_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
541 struct mtd_concat *concat = CONCAT(mtd);
544 for (i = 0; i < concat->num_subdev; i++) {
545 struct mtd_info *subdev = concat->subdev[i];
548 if (ofs >= subdev->size) {
553 if (ofs + len > subdev->size)
554 size = subdev->size - ofs;
558 err = mtd_unlock(subdev, ofs, size);
573 static void concat_sync(struct mtd_info *mtd)
575 struct mtd_concat *concat = CONCAT(mtd);
578 for (i = 0; i < concat->num_subdev; i++) {
579 struct mtd_info *subdev = concat->subdev[i];
585 static int concat_suspend(struct mtd_info *mtd)
587 struct mtd_concat *concat = CONCAT(mtd);
590 for (i = 0; i < concat->num_subdev; i++) {
591 struct mtd_info *subdev = concat->subdev[i];
592 if ((rc = mtd_suspend(subdev)) < 0)
598 static void concat_resume(struct mtd_info *mtd)
600 struct mtd_concat *concat = CONCAT(mtd);
603 for (i = 0; i < concat->num_subdev; i++) {
604 struct mtd_info *subdev = concat->subdev[i];
610 static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
612 struct mtd_concat *concat = CONCAT(mtd);
615 if (!mtd_can_have_bb(concat->subdev[0]))
618 for (i = 0; i < concat->num_subdev; i++) {
619 struct mtd_info *subdev = concat->subdev[i];
621 if (ofs >= subdev->size) {
626 res = mtd_block_isbad(subdev, ofs);
633 static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
635 struct mtd_concat *concat = CONCAT(mtd);
636 int i, err = -EINVAL;
638 for (i = 0; i < concat->num_subdev; i++) {
639 struct mtd_info *subdev = concat->subdev[i];
641 if (ofs >= subdev->size) {
646 err = mtd_block_markbad(subdev, ofs);
648 mtd->ecc_stats.badblocks++;
656 * try to support NOMMU mmaps on concatenated devices
657 * - we don't support subdev spanning as we can't guarantee it'll work
659 static unsigned long concat_get_unmapped_area(struct mtd_info *mtd,
661 unsigned long offset,
664 struct mtd_concat *concat = CONCAT(mtd);
667 for (i = 0; i < concat->num_subdev; i++) {
668 struct mtd_info *subdev = concat->subdev[i];
670 if (offset >= subdev->size) {
671 offset -= subdev->size;
675 return mtd_get_unmapped_area(subdev, len, offset, flags);
678 return (unsigned long) -ENOSYS;
682 * This function constructs a virtual MTD device by concatenating
683 * num_devs MTD devices. A pointer to the new device object is
684 * stored to *new_dev upon success. This function does _not_
685 * register any devices: this is the caller's responsibility.
687 struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */
688 int num_devs, /* number of subdevices */
694 { /* name for the new device */
697 struct mtd_concat *concat;
698 uint32_t max_erasesize, curr_erasesize;
699 int num_erase_region;
700 int max_writebufsize = 0;
702 debug("Concatenating MTD devices:\n");
703 for (i = 0; i < num_devs; i++)
704 printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
705 debug("into device \"%s\"\n", name);
707 /* allocate the device structure */
708 size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
709 concat = kzalloc(size, GFP_KERNEL);
712 ("memory allocation error while creating concatenated device \"%s\"\n",
716 concat->subdev = (struct mtd_info **) (concat + 1);
719 * Set up the new "super" device's MTD object structure, check for
720 * incompatibilities between the subdevices.
722 concat->mtd.type = subdev[0]->type;
723 concat->mtd.flags = subdev[0]->flags;
724 concat->mtd.size = subdev[0]->size;
725 concat->mtd.erasesize = subdev[0]->erasesize;
726 concat->mtd.writesize = subdev[0]->writesize;
728 for (i = 0; i < num_devs; i++)
729 if (max_writebufsize < subdev[i]->writebufsize)
730 max_writebufsize = subdev[i]->writebufsize;
731 concat->mtd.writebufsize = max_writebufsize;
733 concat->mtd.subpage_sft = subdev[0]->subpage_sft;
734 concat->mtd.oobsize = subdev[0]->oobsize;
735 concat->mtd.oobavail = subdev[0]->oobavail;
737 if (subdev[0]->_writev)
738 concat->mtd._writev = concat_writev;
740 if (subdev[0]->_read_oob)
741 concat->mtd._read_oob = concat_read_oob;
742 if (subdev[0]->_write_oob)
743 concat->mtd._write_oob = concat_write_oob;
744 if (subdev[0]->_block_isbad)
745 concat->mtd._block_isbad = concat_block_isbad;
746 if (subdev[0]->_block_markbad)
747 concat->mtd._block_markbad = concat_block_markbad;
749 concat->mtd.ecc_stats.badblocks = subdev[0]->ecc_stats.badblocks;
752 concat->mtd.backing_dev_info = subdev[0]->backing_dev_info;
755 concat->subdev[0] = subdev[0];
757 for (i = 1; i < num_devs; i++) {
758 if (concat->mtd.type != subdev[i]->type) {
760 printk("Incompatible device type on \"%s\"\n",
764 if (concat->mtd.flags != subdev[i]->flags) {
766 * Expect all flags except MTD_WRITEABLE to be
767 * equal on all subdevices.
769 if ((concat->mtd.flags ^ subdev[i]->
770 flags) & ~MTD_WRITEABLE) {
772 printk("Incompatible device flags on \"%s\"\n",
776 /* if writeable attribute differs,
777 make super device writeable */
779 subdev[i]->flags & MTD_WRITEABLE;
783 /* only permit direct mapping if the BDIs are all the same
784 * - copy-mapping is still permitted
786 if (concat->mtd.backing_dev_info !=
787 subdev[i]->backing_dev_info)
788 concat->mtd.backing_dev_info =
789 &default_backing_dev_info;
792 concat->mtd.size += subdev[i]->size;
793 concat->mtd.ecc_stats.badblocks +=
794 subdev[i]->ecc_stats.badblocks;
795 if (concat->mtd.writesize != subdev[i]->writesize ||
796 concat->mtd.subpage_sft != subdev[i]->subpage_sft ||
797 concat->mtd.oobsize != subdev[i]->oobsize ||
798 !concat->mtd._read_oob != !subdev[i]->_read_oob ||
799 !concat->mtd._write_oob != !subdev[i]->_write_oob) {
801 printk("Incompatible OOB or ECC data on \"%s\"\n",
805 concat->subdev[i] = subdev[i];
809 concat->mtd.ecclayout = subdev[0]->ecclayout;
811 concat->num_subdev = num_devs;
812 concat->mtd.name = name;
814 concat->mtd._erase = concat_erase;
815 concat->mtd._read = concat_read;
816 concat->mtd._write = concat_write;
817 concat->mtd._sync = concat_sync;
818 concat->mtd._lock = concat_lock;
819 concat->mtd._unlock = concat_unlock;
821 concat->mtd._suspend = concat_suspend;
822 concat->mtd._resume = concat_resume;
824 concat->mtd._get_unmapped_area = concat_get_unmapped_area;
827 * Combine the erase block size info of the subdevices:
829 * first, walk the map of the new device and see how
830 * many changes in erase size we have
832 max_erasesize = curr_erasesize = subdev[0]->erasesize;
833 num_erase_region = 1;
834 for (i = 0; i < num_devs; i++) {
835 if (subdev[i]->numeraseregions == 0) {
836 /* current subdevice has uniform erase size */
837 if (subdev[i]->erasesize != curr_erasesize) {
838 /* if it differs from the last subdevice's erase size, count it */
840 curr_erasesize = subdev[i]->erasesize;
841 if (curr_erasesize > max_erasesize)
842 max_erasesize = curr_erasesize;
845 /* current subdevice has variable erase size */
847 for (j = 0; j < subdev[i]->numeraseregions; j++) {
849 /* walk the list of erase regions, count any changes */
850 if (subdev[i]->eraseregions[j].erasesize !=
854 subdev[i]->eraseregions[j].
856 if (curr_erasesize > max_erasesize)
857 max_erasesize = curr_erasesize;
863 if (num_erase_region == 1) {
865 * All subdevices have the same uniform erase size.
868 concat->mtd.erasesize = curr_erasesize;
869 concat->mtd.numeraseregions = 0;
874 * erase block size varies across the subdevices: allocate
875 * space to store the data describing the variable erase regions
877 struct mtd_erase_region_info *erase_region_p;
878 uint64_t begin, position;
880 concat->mtd.erasesize = max_erasesize;
881 concat->mtd.numeraseregions = num_erase_region;
882 concat->mtd.eraseregions = erase_region_p =
883 kmalloc(num_erase_region *
884 sizeof (struct mtd_erase_region_info), GFP_KERNEL);
885 if (!erase_region_p) {
888 ("memory allocation error while creating erase region list"
889 " for device \"%s\"\n", name);
894 * walk the map of the new device once more and fill in
895 * in erase region info:
897 curr_erasesize = subdev[0]->erasesize;
898 begin = position = 0;
899 for (i = 0; i < num_devs; i++) {
900 if (subdev[i]->numeraseregions == 0) {
901 /* current subdevice has uniform erase size */
902 if (subdev[i]->erasesize != curr_erasesize) {
904 * fill in an mtd_erase_region_info structure for the area
905 * we have walked so far:
907 erase_region_p->offset = begin;
908 erase_region_p->erasesize =
910 tmp64 = position - begin;
911 do_div(tmp64, curr_erasesize);
912 erase_region_p->numblocks = tmp64;
915 curr_erasesize = subdev[i]->erasesize;
918 position += subdev[i]->size;
920 /* current subdevice has variable erase size */
922 for (j = 0; j < subdev[i]->numeraseregions; j++) {
923 /* walk the list of erase regions, count any changes */
924 if (subdev[i]->eraseregions[j].
925 erasesize != curr_erasesize) {
926 erase_region_p->offset = begin;
927 erase_region_p->erasesize =
929 tmp64 = position - begin;
930 do_div(tmp64, curr_erasesize);
931 erase_region_p->numblocks = tmp64;
935 subdev[i]->eraseregions[j].
940 subdev[i]->eraseregions[j].
941 numblocks * (uint64_t)curr_erasesize;
945 /* Now write the final entry */
946 erase_region_p->offset = begin;
947 erase_region_p->erasesize = curr_erasesize;
948 tmp64 = position - begin;
949 do_div(tmp64, curr_erasesize);
950 erase_region_p->numblocks = tmp64;
957 * This function destroys an MTD object obtained from concat_mtd_devs()
960 void mtd_concat_destroy(struct mtd_info *mtd)
962 struct mtd_concat *concat = CONCAT(mtd);
963 if (concat->mtd.numeraseregions)
964 kfree(concat->mtd.eraseregions);
968 EXPORT_SYMBOL(mtd_concat_create);
969 EXPORT_SYMBOL(mtd_concat_destroy);
971 MODULE_LICENSE("GPL");
972 MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
973 MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");