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 void concat_erase_callback(struct erase_info *instr)
343 /* Nothing to do here in U-Boot */
345 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;
392 * Check for proper erase block alignment of the to-be-erased area.
393 * It is easier to do this based on the super device's erase
394 * region info rather than looking at each particular sub-device
397 if (!concat->mtd.numeraseregions) {
398 /* the easy case: device has uniform erase block size */
399 if (instr->addr & (concat->mtd.erasesize - 1))
401 if (instr->len & (concat->mtd.erasesize - 1))
404 /* device has variable erase size */
405 struct mtd_erase_region_info *erase_regions =
406 concat->mtd.eraseregions;
409 * Find the erase region where the to-be-erased area begins:
411 for (i = 0; i < concat->mtd.numeraseregions &&
412 instr->addr >= erase_regions[i].offset; i++) ;
416 * Now erase_regions[i] is the region in which the
417 * to-be-erased area begins. Verify that the starting
418 * offset is aligned to this region's erase size:
420 if (i < 0 || instr->addr & (erase_regions[i].erasesize - 1))
424 * now find the erase region where the to-be-erased area ends:
426 for (; i < concat->mtd.numeraseregions &&
427 (instr->addr + instr->len) >= erase_regions[i].offset;
431 * check if the ending offset is aligned to this region's erase size
433 if (i < 0 || ((instr->addr + instr->len) &
434 (erase_regions[i].erasesize - 1)))
438 /* make a local copy of instr to avoid modifying the caller's struct */
439 erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
448 * find the subdevice where the to-be-erased area begins, adjust
449 * starting offset to be relative to the subdevice start
451 for (i = 0; i < concat->num_subdev; i++) {
452 subdev = concat->subdev[i];
453 if (subdev->size <= erase->addr) {
454 erase->addr -= subdev->size;
455 offset += subdev->size;
461 /* must never happen since size limit has been verified above */
462 BUG_ON(i >= concat->num_subdev);
464 /* now do the erase: */
466 for (; length > 0; i++) {
467 /* loop for all subdevices affected by this request */
468 subdev = concat->subdev[i]; /* get current subdevice */
470 /* limit length to subdevice's size: */
471 if (erase->addr + length > subdev->size)
472 erase->len = subdev->size - erase->addr;
476 length -= erase->len;
477 if ((err = concat_dev_erase(subdev, erase))) {
478 /* sanity check: should never happen since
479 * block alignment has been checked above */
480 BUG_ON(err == -EINVAL);
481 if (erase->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
482 instr->fail_addr = erase->fail_addr + offset;
486 * erase->addr specifies the offset of the area to be
487 * erased *within the current subdevice*. It can be
488 * non-zero only the first time through this loop, i.e.
489 * for the first subdevice where blocks need to be erased.
490 * All the following erases must begin at the start of the
491 * current subdevice, i.e. at offset zero.
494 offset += subdev->size;
496 instr->state = erase->state;
502 instr->callback(instr);
506 static int concat_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
508 struct mtd_concat *concat = CONCAT(mtd);
509 int i, err = -EINVAL;
511 for (i = 0; i < concat->num_subdev; i++) {
512 struct mtd_info *subdev = concat->subdev[i];
515 if (ofs >= subdev->size) {
520 if (ofs + len > subdev->size)
521 size = subdev->size - ofs;
525 err = mtd_lock(subdev, ofs, size);
540 static int concat_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
542 struct mtd_concat *concat = CONCAT(mtd);
545 for (i = 0; i < concat->num_subdev; i++) {
546 struct mtd_info *subdev = concat->subdev[i];
549 if (ofs >= subdev->size) {
554 if (ofs + len > subdev->size)
555 size = subdev->size - ofs;
559 err = mtd_unlock(subdev, ofs, size);
574 static void concat_sync(struct mtd_info *mtd)
576 struct mtd_concat *concat = CONCAT(mtd);
579 for (i = 0; i < concat->num_subdev; i++) {
580 struct mtd_info *subdev = concat->subdev[i];
586 static int concat_suspend(struct mtd_info *mtd)
588 struct mtd_concat *concat = CONCAT(mtd);
591 for (i = 0; i < concat->num_subdev; i++) {
592 struct mtd_info *subdev = concat->subdev[i];
593 if ((rc = mtd_suspend(subdev)) < 0)
599 static void concat_resume(struct mtd_info *mtd)
601 struct mtd_concat *concat = CONCAT(mtd);
604 for (i = 0; i < concat->num_subdev; i++) {
605 struct mtd_info *subdev = concat->subdev[i];
611 static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
613 struct mtd_concat *concat = CONCAT(mtd);
616 if (!mtd_can_have_bb(concat->subdev[0]))
619 for (i = 0; i < concat->num_subdev; i++) {
620 struct mtd_info *subdev = concat->subdev[i];
622 if (ofs >= subdev->size) {
627 res = mtd_block_isbad(subdev, ofs);
634 static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
636 struct mtd_concat *concat = CONCAT(mtd);
637 int i, err = -EINVAL;
639 for (i = 0; i < concat->num_subdev; i++) {
640 struct mtd_info *subdev = concat->subdev[i];
642 if (ofs >= subdev->size) {
647 err = mtd_block_markbad(subdev, ofs);
649 mtd->ecc_stats.badblocks++;
657 * try to support NOMMU mmaps on concatenated devices
658 * - we don't support subdev spanning as we can't guarantee it'll work
660 static unsigned long concat_get_unmapped_area(struct mtd_info *mtd,
662 unsigned long offset,
665 struct mtd_concat *concat = CONCAT(mtd);
668 for (i = 0; i < concat->num_subdev; i++) {
669 struct mtd_info *subdev = concat->subdev[i];
671 if (offset >= subdev->size) {
672 offset -= subdev->size;
676 return mtd_get_unmapped_area(subdev, len, offset, flags);
679 return (unsigned long) -ENOSYS;
683 * This function constructs a virtual MTD device by concatenating
684 * num_devs MTD devices. A pointer to the new device object is
685 * stored to *new_dev upon success. This function does _not_
686 * register any devices: this is the caller's responsibility.
688 struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */
689 int num_devs, /* number of subdevices */
695 { /* name for the new device */
698 struct mtd_concat *concat;
699 uint32_t max_erasesize, curr_erasesize;
700 int num_erase_region;
701 int max_writebufsize = 0;
703 debug("Concatenating MTD devices:\n");
704 for (i = 0; i < num_devs; i++)
705 printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
706 debug("into device \"%s\"\n", name);
708 /* allocate the device structure */
709 size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
710 concat = kzalloc(size, GFP_KERNEL);
713 ("memory allocation error while creating concatenated device \"%s\"\n",
717 concat->subdev = (struct mtd_info **) (concat + 1);
720 * Set up the new "super" device's MTD object structure, check for
721 * incompatibilities between the subdevices.
723 concat->mtd.type = subdev[0]->type;
724 concat->mtd.flags = subdev[0]->flags;
725 concat->mtd.size = subdev[0]->size;
726 concat->mtd.erasesize = subdev[0]->erasesize;
727 concat->mtd.writesize = subdev[0]->writesize;
729 for (i = 0; i < num_devs; i++)
730 if (max_writebufsize < subdev[i]->writebufsize)
731 max_writebufsize = subdev[i]->writebufsize;
732 concat->mtd.writebufsize = max_writebufsize;
734 concat->mtd.subpage_sft = subdev[0]->subpage_sft;
735 concat->mtd.oobsize = subdev[0]->oobsize;
736 concat->mtd.oobavail = subdev[0]->oobavail;
738 if (subdev[0]->_writev)
739 concat->mtd._writev = concat_writev;
741 if (subdev[0]->_read_oob)
742 concat->mtd._read_oob = concat_read_oob;
743 if (subdev[0]->_write_oob)
744 concat->mtd._write_oob = concat_write_oob;
745 if (subdev[0]->_block_isbad)
746 concat->mtd._block_isbad = concat_block_isbad;
747 if (subdev[0]->_block_markbad)
748 concat->mtd._block_markbad = concat_block_markbad;
750 concat->mtd.ecc_stats.badblocks = subdev[0]->ecc_stats.badblocks;
753 concat->mtd.backing_dev_info = subdev[0]->backing_dev_info;
756 concat->subdev[0] = subdev[0];
758 for (i = 1; i < num_devs; i++) {
759 if (concat->mtd.type != subdev[i]->type) {
761 printk("Incompatible device type on \"%s\"\n",
765 if (concat->mtd.flags != subdev[i]->flags) {
767 * Expect all flags except MTD_WRITEABLE to be
768 * equal on all subdevices.
770 if ((concat->mtd.flags ^ subdev[i]->
771 flags) & ~MTD_WRITEABLE) {
773 printk("Incompatible device flags on \"%s\"\n",
777 /* if writeable attribute differs,
778 make super device writeable */
780 subdev[i]->flags & MTD_WRITEABLE;
784 /* only permit direct mapping if the BDIs are all the same
785 * - copy-mapping is still permitted
787 if (concat->mtd.backing_dev_info !=
788 subdev[i]->backing_dev_info)
789 concat->mtd.backing_dev_info =
790 &default_backing_dev_info;
793 concat->mtd.size += subdev[i]->size;
794 concat->mtd.ecc_stats.badblocks +=
795 subdev[i]->ecc_stats.badblocks;
796 if (concat->mtd.writesize != subdev[i]->writesize ||
797 concat->mtd.subpage_sft != subdev[i]->subpage_sft ||
798 concat->mtd.oobsize != subdev[i]->oobsize ||
799 !concat->mtd._read_oob != !subdev[i]->_read_oob ||
800 !concat->mtd._write_oob != !subdev[i]->_write_oob) {
802 printk("Incompatible OOB or ECC data on \"%s\"\n",
806 concat->subdev[i] = subdev[i];
810 concat->mtd.ecclayout = subdev[0]->ecclayout;
812 concat->num_subdev = num_devs;
813 concat->mtd.name = name;
815 concat->mtd._erase = concat_erase;
816 concat->mtd._read = concat_read;
817 concat->mtd._write = concat_write;
818 concat->mtd._sync = concat_sync;
819 concat->mtd._lock = concat_lock;
820 concat->mtd._unlock = concat_unlock;
822 concat->mtd._suspend = concat_suspend;
823 concat->mtd._resume = concat_resume;
825 concat->mtd._get_unmapped_area = concat_get_unmapped_area;
828 * Combine the erase block size info of the subdevices:
830 * first, walk the map of the new device and see how
831 * many changes in erase size we have
833 max_erasesize = curr_erasesize = subdev[0]->erasesize;
834 num_erase_region = 1;
835 for (i = 0; i < num_devs; i++) {
836 if (subdev[i]->numeraseregions == 0) {
837 /* current subdevice has uniform erase size */
838 if (subdev[i]->erasesize != curr_erasesize) {
839 /* if it differs from the last subdevice's erase size, count it */
841 curr_erasesize = subdev[i]->erasesize;
842 if (curr_erasesize > max_erasesize)
843 max_erasesize = curr_erasesize;
846 /* current subdevice has variable erase size */
848 for (j = 0; j < subdev[i]->numeraseregions; j++) {
850 /* walk the list of erase regions, count any changes */
851 if (subdev[i]->eraseregions[j].erasesize !=
855 subdev[i]->eraseregions[j].
857 if (curr_erasesize > max_erasesize)
858 max_erasesize = curr_erasesize;
864 if (num_erase_region == 1) {
866 * All subdevices have the same uniform erase size.
869 concat->mtd.erasesize = curr_erasesize;
870 concat->mtd.numeraseregions = 0;
875 * erase block size varies across the subdevices: allocate
876 * space to store the data describing the variable erase regions
878 struct mtd_erase_region_info *erase_region_p;
879 uint64_t begin, position;
881 concat->mtd.erasesize = max_erasesize;
882 concat->mtd.numeraseregions = num_erase_region;
883 concat->mtd.eraseregions = erase_region_p =
884 kmalloc(num_erase_region *
885 sizeof (struct mtd_erase_region_info), GFP_KERNEL);
886 if (!erase_region_p) {
889 ("memory allocation error while creating erase region list"
890 " for device \"%s\"\n", name);
895 * walk the map of the new device once more and fill in
896 * in erase region info:
898 curr_erasesize = subdev[0]->erasesize;
899 begin = position = 0;
900 for (i = 0; i < num_devs; i++) {
901 if (subdev[i]->numeraseregions == 0) {
902 /* current subdevice has uniform erase size */
903 if (subdev[i]->erasesize != curr_erasesize) {
905 * fill in an mtd_erase_region_info structure for the area
906 * we have walked so far:
908 erase_region_p->offset = begin;
909 erase_region_p->erasesize =
911 tmp64 = position - begin;
912 do_div(tmp64, curr_erasesize);
913 erase_region_p->numblocks = tmp64;
916 curr_erasesize = subdev[i]->erasesize;
919 position += subdev[i]->size;
921 /* current subdevice has variable erase size */
923 for (j = 0; j < subdev[i]->numeraseregions; j++) {
924 /* walk the list of erase regions, count any changes */
925 if (subdev[i]->eraseregions[j].
926 erasesize != curr_erasesize) {
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;
936 subdev[i]->eraseregions[j].
941 subdev[i]->eraseregions[j].
942 numblocks * (uint64_t)curr_erasesize;
946 /* Now write the final entry */
947 erase_region_p->offset = begin;
948 erase_region_p->erasesize = curr_erasesize;
949 tmp64 = position - begin;
950 do_div(tmp64, curr_erasesize);
951 erase_region_p->numblocks = tmp64;
958 * This function destroys an MTD object obtained from concat_mtd_devs()
961 void mtd_concat_destroy(struct mtd_info *mtd)
963 struct mtd_concat *concat = CONCAT(mtd);
964 if (concat->mtd.numeraseregions)
965 kfree(concat->mtd.eraseregions);
969 EXPORT_SYMBOL(mtd_concat_create);
970 EXPORT_SYMBOL(mtd_concat_destroy);
972 MODULE_LICENSE("GPL");
973 MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
974 MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");