1 // SPDX-License-Identifier: GPL-2.0+
3 * MTD device concatenation layer
5 * Copyright © 2002 Robert Kaiser <rkaiser@sysgo.de>
6 * Copyright © 2002-2010 David Woodhouse <dwmw2@infradead.org>
8 * NAND support by Christian Gan <cgan@iders.ca>
14 #include <dm/devres.h>
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/bug.h>
25 #include <linux/compat.h>
28 #include <linux/mtd/mtd.h>
29 #include <linux/mtd/concat.h>
31 #include <ubi_uboot.h>
34 * Our storage structure:
35 * Subdev points to an array of pointers to struct mtd_info objects
36 * which is allocated along with this structure
42 struct mtd_info **subdev;
46 * how to calculate the size required for the above structure,
47 * including the pointer array subdev points to:
49 #define SIZEOF_STRUCT_MTD_CONCAT(num_subdev) \
50 ((sizeof(struct mtd_concat) + (num_subdev) * sizeof(struct mtd_info *)))
53 * Given a pointer to the MTD object in the mtd_concat structure,
54 * we can retrieve the pointer to that structure with this macro.
56 #define CONCAT(x) ((struct mtd_concat *)(x))
59 * MTD methods which look up the relevant subdevice, translate the
60 * effective address and pass through to the subdevice.
64 concat_read(struct mtd_info *mtd, loff_t from, size_t len,
65 size_t * retlen, u_char * buf)
67 struct mtd_concat *concat = CONCAT(mtd);
75 for (i = 0; i < concat->num_subdev; i++) {
76 struct mtd_info *subdev = concat->subdev[i];
79 if (from >= subdev->size) {
80 /* Not destined for this subdev */
85 if (from + len > subdev->size)
86 /* First part goes into this subdev */
87 size = subdev->size - from;
89 /* Entire transaction goes into this subdev */
92 err = mtd_read(subdev, from, size, &retsize, buf);
94 /* Save information about bitflips! */
96 if (mtd_is_eccerr(err)) {
97 mtd->ecc_stats.failed++;
99 } else if (mtd_is_bitflip(err)) {
100 mtd->ecc_stats.corrected++;
101 /* Do not overwrite -EBADMSG !! */
120 concat_write(struct mtd_info *mtd, loff_t to, size_t len,
121 size_t * retlen, const u_char * buf)
123 struct mtd_concat *concat = CONCAT(mtd);
131 for (i = 0; i < concat->num_subdev; i++) {
132 struct mtd_info *subdev = concat->subdev[i];
133 size_t size, retsize;
135 if (to >= subdev->size) {
140 if (to + len > subdev->size)
141 size = subdev->size - to;
145 err = mtd_write(subdev, to, size, &retsize, buf);
163 concat_writev(struct mtd_info *mtd, const struct kvec *vecs,
164 unsigned long count, loff_t to, size_t * retlen)
166 struct mtd_concat *concat = CONCAT(mtd);
167 struct kvec *vecs_copy;
168 unsigned long entry_low, entry_high;
169 size_t total_len = 0;
173 /* Calculate total length of data */
174 for (i = 0; i < count; i++)
175 total_len += vecs[i].iov_len;
177 /* Check alignment */
178 if (mtd->writesize > 1) {
180 if (do_div(__to, mtd->writesize) || (total_len % mtd->writesize))
184 /* make a copy of vecs */
185 vecs_copy = kmemdup(vecs, sizeof(struct kvec) * count, GFP_KERNEL);
190 for (i = 0; i < concat->num_subdev; i++) {
191 struct mtd_info *subdev = concat->subdev[i];
192 size_t size, wsize, retsize, old_iov_len;
194 if (to >= subdev->size) {
199 size = min_t(uint64_t, total_len, subdev->size - to);
200 wsize = size; /* store for future use */
202 entry_high = entry_low;
203 while (entry_high < count) {
204 if (size <= vecs_copy[entry_high].iov_len)
206 size -= vecs_copy[entry_high++].iov_len;
209 old_iov_len = vecs_copy[entry_high].iov_len;
210 vecs_copy[entry_high].iov_len = size;
212 err = mtd_writev(subdev, &vecs_copy[entry_low],
213 entry_high - entry_low + 1, to, &retsize);
215 vecs_copy[entry_high].iov_len = old_iov_len - size;
216 vecs_copy[entry_high].iov_base += size;
218 entry_low = entry_high;
239 concat_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
241 struct mtd_concat *concat = CONCAT(mtd);
242 struct mtd_oob_ops devops = *ops;
245 ops->retlen = ops->oobretlen = 0;
247 for (i = 0; i < concat->num_subdev; i++) {
248 struct mtd_info *subdev = concat->subdev[i];
250 if (from >= subdev->size) {
251 from -= subdev->size;
256 if (from + devops.len > subdev->size)
257 devops.len = subdev->size - from;
259 err = mtd_read_oob(subdev, from, &devops);
260 ops->retlen += devops.retlen;
261 ops->oobretlen += devops.oobretlen;
263 /* Save information about bitflips! */
265 if (mtd_is_eccerr(err)) {
266 mtd->ecc_stats.failed++;
268 } else if (mtd_is_bitflip(err)) {
269 mtd->ecc_stats.corrected++;
270 /* Do not overwrite -EBADMSG !! */
278 devops.len = ops->len - ops->retlen;
281 devops.datbuf += devops.retlen;
284 devops.ooblen = ops->ooblen - ops->oobretlen;
287 devops.oobbuf += ops->oobretlen;
296 concat_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
298 struct mtd_concat *concat = CONCAT(mtd);
299 struct mtd_oob_ops devops = *ops;
302 if (!(mtd->flags & MTD_WRITEABLE))
305 ops->retlen = ops->oobretlen = 0;
307 for (i = 0; i < concat->num_subdev; i++) {
308 struct mtd_info *subdev = concat->subdev[i];
310 if (to >= subdev->size) {
315 /* partial write ? */
316 if (to + devops.len > subdev->size)
317 devops.len = subdev->size - to;
319 err = mtd_write_oob(subdev, to, &devops);
320 ops->retlen += devops.oobretlen;
325 devops.len = ops->len - ops->retlen;
328 devops.datbuf += devops.retlen;
331 devops.ooblen = ops->ooblen - ops->oobretlen;
334 devops.oobbuf += devops.oobretlen;
341 static int concat_dev_erase(struct mtd_info *mtd, struct erase_info *erase)
344 wait_queue_head_t waitq;
345 DECLARE_WAITQUEUE(wait, current);
348 * This code was stol^H^H^H^Hinspired by mtdchar.c
350 init_waitqueue_head(&waitq);
353 erase->priv = (unsigned long) &waitq;
356 * FIXME: Allow INTERRUPTIBLE. Which means
357 * not having the wait_queue head on the stack.
359 err = mtd_erase(mtd, erase);
361 set_current_state(TASK_UNINTERRUPTIBLE);
362 add_wait_queue(&waitq, &wait);
363 if (erase->state != MTD_ERASE_DONE
364 && erase->state != MTD_ERASE_FAILED)
366 remove_wait_queue(&waitq, &wait);
367 set_current_state(TASK_RUNNING);
369 err = (erase->state == MTD_ERASE_FAILED) ? -EIO : 0;
374 static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
376 struct mtd_concat *concat = CONCAT(mtd);
377 struct mtd_info *subdev;
379 uint64_t length, offset = 0;
380 struct erase_info *erase;
383 * Check for proper erase block alignment of the to-be-erased area.
384 * It is easier to do this based on the super device's erase
385 * region info rather than looking at each particular sub-device
388 if (!concat->mtd.numeraseregions) {
389 /* the easy case: device has uniform erase block size */
390 if (instr->addr & (concat->mtd.erasesize - 1))
392 if (instr->len & (concat->mtd.erasesize - 1))
395 /* device has variable erase size */
396 struct mtd_erase_region_info *erase_regions =
397 concat->mtd.eraseregions;
400 * Find the erase region where the to-be-erased area begins:
402 for (i = 0; i < concat->mtd.numeraseregions &&
403 instr->addr >= erase_regions[i].offset; i++) ;
407 * Now erase_regions[i] is the region in which the
408 * to-be-erased area begins. Verify that the starting
409 * offset is aligned to this region's erase size:
411 if (i < 0 || instr->addr & (erase_regions[i].erasesize - 1))
415 * now find the erase region where the to-be-erased area ends:
417 for (; i < concat->mtd.numeraseregions &&
418 (instr->addr + instr->len) >= erase_regions[i].offset;
422 * check if the ending offset is aligned to this region's erase size
424 if (i < 0 || ((instr->addr + instr->len) &
425 (erase_regions[i].erasesize - 1)))
429 /* make a local copy of instr to avoid modifying the caller's struct */
430 erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
439 * find the subdevice where the to-be-erased area begins, adjust
440 * starting offset to be relative to the subdevice start
442 for (i = 0; i < concat->num_subdev; i++) {
443 subdev = concat->subdev[i];
444 if (subdev->size <= erase->addr) {
445 erase->addr -= subdev->size;
446 offset += subdev->size;
452 /* must never happen since size limit has been verified above */
453 BUG_ON(i >= concat->num_subdev);
455 /* now do the erase: */
457 for (; length > 0; i++) {
458 /* loop for all subdevices affected by this request */
459 subdev = concat->subdev[i]; /* get current subdevice */
461 /* limit length to subdevice's size: */
462 if (erase->addr + length > subdev->size)
463 erase->len = subdev->size - erase->addr;
467 length -= erase->len;
468 if ((err = concat_dev_erase(subdev, erase))) {
469 /* sanity check: should never happen since
470 * block alignment has been checked above */
471 BUG_ON(err == -EINVAL);
472 if (erase->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
473 instr->fail_addr = erase->fail_addr + offset;
477 * erase->addr specifies the offset of the area to be
478 * erased *within the current subdevice*. It can be
479 * non-zero only the first time through this loop, i.e.
480 * for the first subdevice where blocks need to be erased.
481 * All the following erases must begin at the start of the
482 * current subdevice, i.e. at offset zero.
485 offset += subdev->size;
487 instr->state = erase->state;
495 static int concat_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
497 struct mtd_concat *concat = CONCAT(mtd);
498 int i, err = -EINVAL;
500 for (i = 0; i < concat->num_subdev; i++) {
501 struct mtd_info *subdev = concat->subdev[i];
504 if (ofs >= subdev->size) {
509 if (ofs + len > subdev->size)
510 size = subdev->size - ofs;
514 err = mtd_lock(subdev, ofs, size);
529 static int concat_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
531 struct mtd_concat *concat = CONCAT(mtd);
534 for (i = 0; i < concat->num_subdev; i++) {
535 struct mtd_info *subdev = concat->subdev[i];
538 if (ofs >= subdev->size) {
543 if (ofs + len > subdev->size)
544 size = subdev->size - ofs;
548 err = mtd_unlock(subdev, ofs, size);
563 static void concat_sync(struct mtd_info *mtd)
565 struct mtd_concat *concat = CONCAT(mtd);
568 for (i = 0; i < concat->num_subdev; i++) {
569 struct mtd_info *subdev = concat->subdev[i];
575 static int concat_suspend(struct mtd_info *mtd)
577 struct mtd_concat *concat = CONCAT(mtd);
580 for (i = 0; i < concat->num_subdev; i++) {
581 struct mtd_info *subdev = concat->subdev[i];
582 if ((rc = mtd_suspend(subdev)) < 0)
588 static void concat_resume(struct mtd_info *mtd)
590 struct mtd_concat *concat = CONCAT(mtd);
593 for (i = 0; i < concat->num_subdev; i++) {
594 struct mtd_info *subdev = concat->subdev[i];
600 static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
602 struct mtd_concat *concat = CONCAT(mtd);
605 if (!mtd_can_have_bb(concat->subdev[0]))
608 for (i = 0; i < concat->num_subdev; i++) {
609 struct mtd_info *subdev = concat->subdev[i];
611 if (ofs >= subdev->size) {
616 res = mtd_block_isbad(subdev, ofs);
623 static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
625 struct mtd_concat *concat = CONCAT(mtd);
626 int i, err = -EINVAL;
628 for (i = 0; i < concat->num_subdev; i++) {
629 struct mtd_info *subdev = concat->subdev[i];
631 if (ofs >= subdev->size) {
636 err = mtd_block_markbad(subdev, ofs);
638 mtd->ecc_stats.badblocks++;
646 * try to support NOMMU mmaps on concatenated devices
647 * - we don't support subdev spanning as we can't guarantee it'll work
649 static unsigned long concat_get_unmapped_area(struct mtd_info *mtd,
651 unsigned long offset,
654 struct mtd_concat *concat = CONCAT(mtd);
657 for (i = 0; i < concat->num_subdev; i++) {
658 struct mtd_info *subdev = concat->subdev[i];
660 if (offset >= subdev->size) {
661 offset -= subdev->size;
665 return mtd_get_unmapped_area(subdev, len, offset, flags);
668 return (unsigned long) -ENOSYS;
672 * This function constructs a virtual MTD device by concatenating
673 * num_devs MTD devices. A pointer to the new device object is
674 * stored to *new_dev upon success. This function does _not_
675 * register any devices: this is the caller's responsibility.
677 struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */
678 int num_devs, /* number of subdevices */
684 { /* name for the new device */
687 struct mtd_concat *concat;
688 uint32_t max_erasesize, curr_erasesize;
689 int num_erase_region;
690 int max_writebufsize = 0;
692 debug("Concatenating MTD devices:\n");
693 for (i = 0; i < num_devs; i++)
694 printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
695 debug("into device \"%s\"\n", name);
697 /* allocate the device structure */
698 size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
699 concat = kzalloc(size, GFP_KERNEL);
702 ("memory allocation error while creating concatenated device \"%s\"\n",
706 concat->subdev = (struct mtd_info **) (concat + 1);
709 * Set up the new "super" device's MTD object structure, check for
710 * incompatibilities between the subdevices.
712 concat->mtd.type = subdev[0]->type;
713 concat->mtd.flags = subdev[0]->flags;
714 concat->mtd.size = subdev[0]->size;
715 concat->mtd.erasesize = subdev[0]->erasesize;
716 concat->mtd.writesize = subdev[0]->writesize;
718 for (i = 0; i < num_devs; i++)
719 if (max_writebufsize < subdev[i]->writebufsize)
720 max_writebufsize = subdev[i]->writebufsize;
721 concat->mtd.writebufsize = max_writebufsize;
723 concat->mtd.subpage_sft = subdev[0]->subpage_sft;
724 concat->mtd.oobsize = subdev[0]->oobsize;
725 concat->mtd.oobavail = subdev[0]->oobavail;
727 if (subdev[0]->_writev)
728 concat->mtd._writev = concat_writev;
730 if (subdev[0]->_read_oob)
731 concat->mtd._read_oob = concat_read_oob;
732 if (subdev[0]->_write_oob)
733 concat->mtd._write_oob = concat_write_oob;
734 if (subdev[0]->_block_isbad)
735 concat->mtd._block_isbad = concat_block_isbad;
736 if (subdev[0]->_block_markbad)
737 concat->mtd._block_markbad = concat_block_markbad;
739 concat->mtd.ecc_stats.badblocks = subdev[0]->ecc_stats.badblocks;
742 concat->mtd.backing_dev_info = subdev[0]->backing_dev_info;
745 concat->subdev[0] = subdev[0];
747 for (i = 1; i < num_devs; i++) {
748 if (concat->mtd.type != subdev[i]->type) {
750 printk("Incompatible device type on \"%s\"\n",
754 if (concat->mtd.flags != subdev[i]->flags) {
756 * Expect all flags except MTD_WRITEABLE to be
757 * equal on all subdevices.
759 if ((concat->mtd.flags ^ subdev[i]->
760 flags) & ~MTD_WRITEABLE) {
762 printk("Incompatible device flags on \"%s\"\n",
766 /* if writeable attribute differs,
767 make super device writeable */
769 subdev[i]->flags & MTD_WRITEABLE;
773 /* only permit direct mapping if the BDIs are all the same
774 * - copy-mapping is still permitted
776 if (concat->mtd.backing_dev_info !=
777 subdev[i]->backing_dev_info)
778 concat->mtd.backing_dev_info =
779 &default_backing_dev_info;
782 concat->mtd.size += subdev[i]->size;
783 concat->mtd.ecc_stats.badblocks +=
784 subdev[i]->ecc_stats.badblocks;
785 if (concat->mtd.writesize != subdev[i]->writesize ||
786 concat->mtd.subpage_sft != subdev[i]->subpage_sft ||
787 concat->mtd.oobsize != subdev[i]->oobsize ||
788 !concat->mtd._read_oob != !subdev[i]->_read_oob ||
789 !concat->mtd._write_oob != !subdev[i]->_write_oob) {
791 printk("Incompatible OOB or ECC data on \"%s\"\n",
795 concat->subdev[i] = subdev[i];
799 concat->mtd.ecclayout = subdev[0]->ecclayout;
801 concat->num_subdev = num_devs;
802 concat->mtd.name = name;
804 concat->mtd._erase = concat_erase;
805 concat->mtd._read = concat_read;
806 concat->mtd._write = concat_write;
807 concat->mtd._sync = concat_sync;
808 concat->mtd._lock = concat_lock;
809 concat->mtd._unlock = concat_unlock;
811 concat->mtd._suspend = concat_suspend;
812 concat->mtd._resume = concat_resume;
814 concat->mtd._get_unmapped_area = concat_get_unmapped_area;
817 * Combine the erase block size info of the subdevices:
819 * first, walk the map of the new device and see how
820 * many changes in erase size we have
822 max_erasesize = curr_erasesize = subdev[0]->erasesize;
823 num_erase_region = 1;
824 for (i = 0; i < num_devs; i++) {
825 if (subdev[i]->numeraseregions == 0) {
826 /* current subdevice has uniform erase size */
827 if (subdev[i]->erasesize != curr_erasesize) {
828 /* if it differs from the last subdevice's erase size, count it */
830 curr_erasesize = subdev[i]->erasesize;
831 if (curr_erasesize > max_erasesize)
832 max_erasesize = curr_erasesize;
835 /* current subdevice has variable erase size */
837 for (j = 0; j < subdev[i]->numeraseregions; j++) {
839 /* walk the list of erase regions, count any changes */
840 if (subdev[i]->eraseregions[j].erasesize !=
844 subdev[i]->eraseregions[j].
846 if (curr_erasesize > max_erasesize)
847 max_erasesize = curr_erasesize;
853 if (num_erase_region == 1) {
855 * All subdevices have the same uniform erase size.
858 concat->mtd.erasesize = curr_erasesize;
859 concat->mtd.numeraseregions = 0;
864 * erase block size varies across the subdevices: allocate
865 * space to store the data describing the variable erase regions
867 struct mtd_erase_region_info *erase_region_p;
868 uint64_t begin, position;
870 concat->mtd.erasesize = max_erasesize;
871 concat->mtd.numeraseregions = num_erase_region;
872 concat->mtd.eraseregions = erase_region_p =
873 kmalloc(num_erase_region *
874 sizeof (struct mtd_erase_region_info), GFP_KERNEL);
875 if (!erase_region_p) {
878 ("memory allocation error while creating erase region list"
879 " for device \"%s\"\n", name);
884 * walk the map of the new device once more and fill in
885 * in erase region info:
887 curr_erasesize = subdev[0]->erasesize;
888 begin = position = 0;
889 for (i = 0; i < num_devs; i++) {
890 if (subdev[i]->numeraseregions == 0) {
891 /* current subdevice has uniform erase size */
892 if (subdev[i]->erasesize != curr_erasesize) {
894 * fill in an mtd_erase_region_info structure for the area
895 * we have walked so far:
897 erase_region_p->offset = begin;
898 erase_region_p->erasesize =
900 tmp64 = position - begin;
901 do_div(tmp64, curr_erasesize);
902 erase_region_p->numblocks = tmp64;
905 curr_erasesize = subdev[i]->erasesize;
908 position += subdev[i]->size;
910 /* current subdevice has variable erase size */
912 for (j = 0; j < subdev[i]->numeraseregions; j++) {
913 /* walk the list of erase regions, count any changes */
914 if (subdev[i]->eraseregions[j].
915 erasesize != curr_erasesize) {
916 erase_region_p->offset = begin;
917 erase_region_p->erasesize =
919 tmp64 = position - begin;
920 do_div(tmp64, curr_erasesize);
921 erase_region_p->numblocks = tmp64;
925 subdev[i]->eraseregions[j].
930 subdev[i]->eraseregions[j].
931 numblocks * (uint64_t)curr_erasesize;
935 /* Now write the final entry */
936 erase_region_p->offset = begin;
937 erase_region_p->erasesize = curr_erasesize;
938 tmp64 = position - begin;
939 do_div(tmp64, curr_erasesize);
940 erase_region_p->numblocks = tmp64;
947 * This function destroys an MTD object obtained from concat_mtd_devs()
950 void mtd_concat_destroy(struct mtd_info *mtd)
952 struct mtd_concat *concat = CONCAT(mtd);
953 if (concat->mtd.numeraseregions)
954 kfree(concat->mtd.eraseregions);
958 EXPORT_SYMBOL(mtd_concat_create);
959 EXPORT_SYMBOL(mtd_concat_destroy);
961 MODULE_LICENSE("GPL");
962 MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
963 MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");