1 // SPDX-License-Identifier: GPL-2.0+
3 * MTD device concatenation layer
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>
26 #include <linux/printk.h>
29 #include <linux/mtd/mtd.h>
30 #include <linux/mtd/concat.h>
32 #include <ubi_uboot.h>
35 * Our storage structure:
36 * Subdev points to an array of pointers to struct mtd_info objects
37 * which is allocated along with this structure
43 struct mtd_info **subdev;
47 * how to calculate the size required for the above structure,
48 * including the pointer array subdev points to:
50 #define SIZEOF_STRUCT_MTD_CONCAT(num_subdev) \
51 ((sizeof(struct mtd_concat) + (num_subdev) * sizeof(struct mtd_info *)))
54 * Given a pointer to the MTD object in the mtd_concat structure,
55 * we can retrieve the pointer to that structure with this macro.
57 #define CONCAT(x) ((struct mtd_concat *)(x))
60 * MTD methods which look up the relevant subdevice, translate the
61 * effective address and pass through to the subdevice.
65 concat_read(struct mtd_info *mtd, loff_t from, size_t len,
66 size_t * retlen, u_char * buf)
68 struct mtd_concat *concat = CONCAT(mtd);
76 for (i = 0; i < concat->num_subdev; i++) {
77 struct mtd_info *subdev = concat->subdev[i];
80 if (from >= subdev->size) {
81 /* Not destined for this subdev */
86 if (from + len > subdev->size)
87 /* First part goes into this subdev */
88 size = subdev->size - from;
90 /* Entire transaction goes into this subdev */
93 err = mtd_read(subdev, from, size, &retsize, buf);
95 /* Save information about bitflips! */
97 if (mtd_is_eccerr(err)) {
98 mtd->ecc_stats.failed++;
100 } else if (mtd_is_bitflip(err)) {
101 mtd->ecc_stats.corrected++;
102 /* Do not overwrite -EBADMSG !! */
121 concat_write(struct mtd_info *mtd, loff_t to, size_t len,
122 size_t * retlen, const u_char * buf)
124 struct mtd_concat *concat = CONCAT(mtd);
132 for (i = 0; i < concat->num_subdev; i++) {
133 struct mtd_info *subdev = concat->subdev[i];
134 size_t size, retsize;
136 if (to >= subdev->size) {
141 if (to + len > subdev->size)
142 size = subdev->size - to;
146 err = mtd_write(subdev, to, size, &retsize, buf);
164 concat_writev(struct mtd_info *mtd, const struct kvec *vecs,
165 unsigned long count, loff_t to, size_t * retlen)
167 struct mtd_concat *concat = CONCAT(mtd);
168 struct kvec *vecs_copy;
169 unsigned long entry_low, entry_high;
170 size_t total_len = 0;
174 /* Calculate total length of data */
175 for (i = 0; i < count; i++)
176 total_len += vecs[i].iov_len;
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 err = mtd_writev(subdev, &vecs_copy[entry_low],
214 entry_high - entry_low + 1, to, &retsize);
216 vecs_copy[entry_high].iov_len = old_iov_len - size;
217 vecs_copy[entry_high].iov_base += size;
219 entry_low = entry_high;
240 concat_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
242 struct mtd_concat *concat = CONCAT(mtd);
243 struct mtd_oob_ops devops = *ops;
246 ops->retlen = ops->oobretlen = 0;
248 for (i = 0; i < concat->num_subdev; i++) {
249 struct mtd_info *subdev = concat->subdev[i];
251 if (from >= subdev->size) {
252 from -= subdev->size;
257 if (from + devops.len > subdev->size)
258 devops.len = subdev->size - from;
260 err = mtd_read_oob(subdev, from, &devops);
261 ops->retlen += devops.retlen;
262 ops->oobretlen += devops.oobretlen;
264 /* Save information about bitflips! */
266 if (mtd_is_eccerr(err)) {
267 mtd->ecc_stats.failed++;
269 } else if (mtd_is_bitflip(err)) {
270 mtd->ecc_stats.corrected++;
271 /* Do not overwrite -EBADMSG !! */
279 devops.len = ops->len - ops->retlen;
282 devops.datbuf += devops.retlen;
285 devops.ooblen = ops->ooblen - ops->oobretlen;
288 devops.oobbuf += ops->oobretlen;
297 concat_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
299 struct mtd_concat *concat = CONCAT(mtd);
300 struct mtd_oob_ops devops = *ops;
303 if (!(mtd->flags & MTD_WRITEABLE))
306 ops->retlen = ops->oobretlen = 0;
308 for (i = 0; i < concat->num_subdev; i++) {
309 struct mtd_info *subdev = concat->subdev[i];
311 if (to >= subdev->size) {
316 /* partial write ? */
317 if (to + devops.len > subdev->size)
318 devops.len = subdev->size - to;
320 err = mtd_write_oob(subdev, to, &devops);
321 ops->retlen += devops.oobretlen;
326 devops.len = ops->len - ops->retlen;
329 devops.datbuf += devops.retlen;
332 devops.ooblen = ops->ooblen - ops->oobretlen;
335 devops.oobbuf += devops.oobretlen;
342 static int concat_dev_erase(struct mtd_info *mtd, struct erase_info *erase)
345 wait_queue_head_t waitq;
346 DECLARE_WAITQUEUE(wait, current);
349 * This code was stol^H^H^H^Hinspired by mtdchar.c
351 init_waitqueue_head(&waitq);
354 erase->priv = (unsigned long) &waitq;
357 * FIXME: Allow INTERRUPTIBLE. Which means
358 * not having the wait_queue head on the stack.
360 err = mtd_erase(mtd, erase);
362 set_current_state(TASK_UNINTERRUPTIBLE);
363 add_wait_queue(&waitq, &wait);
364 if (erase->state != MTD_ERASE_DONE
365 && erase->state != MTD_ERASE_FAILED)
367 remove_wait_queue(&waitq, &wait);
368 set_current_state(TASK_RUNNING);
370 err = (erase->state == MTD_ERASE_FAILED) ? -EIO : 0;
375 static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
377 struct mtd_concat *concat = CONCAT(mtd);
378 struct mtd_info *subdev;
380 uint64_t length, offset = 0;
381 struct erase_info *erase;
384 * Check for proper erase block alignment of the to-be-erased area.
385 * It is easier to do this based on the super device's erase
386 * region info rather than looking at each particular sub-device
389 if (!concat->mtd.numeraseregions) {
390 /* the easy case: device has uniform erase block size */
391 if (instr->addr & (concat->mtd.erasesize - 1))
393 if (instr->len & (concat->mtd.erasesize - 1))
396 /* device has variable erase size */
397 struct mtd_erase_region_info *erase_regions =
398 concat->mtd.eraseregions;
401 * Find the erase region where the to-be-erased area begins:
403 for (i = 0; i < concat->mtd.numeraseregions &&
404 instr->addr >= erase_regions[i].offset; i++) ;
408 * Now erase_regions[i] is the region in which the
409 * to-be-erased area begins. Verify that the starting
410 * offset is aligned to this region's erase size:
412 if (i < 0 || instr->addr & (erase_regions[i].erasesize - 1))
416 * now find the erase region where the to-be-erased area ends:
418 for (; i < concat->mtd.numeraseregions &&
419 (instr->addr + instr->len) >= erase_regions[i].offset;
423 * check if the ending offset is aligned to this region's erase size
425 if (i < 0 || ((instr->addr + instr->len) &
426 (erase_regions[i].erasesize - 1)))
430 /* make a local copy of instr to avoid modifying the caller's struct */
431 erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
440 * find the subdevice where the to-be-erased area begins, adjust
441 * starting offset to be relative to the subdevice start
443 for (i = 0; i < concat->num_subdev; i++) {
444 subdev = concat->subdev[i];
445 if (subdev->size <= erase->addr) {
446 erase->addr -= subdev->size;
447 offset += subdev->size;
453 /* must never happen since size limit has been verified above */
454 BUG_ON(i >= concat->num_subdev);
456 /* now do the erase: */
458 for (; length > 0; i++) {
459 /* loop for all subdevices affected by this request */
460 subdev = concat->subdev[i]; /* get current subdevice */
462 /* limit length to subdevice's size: */
463 if (erase->addr + length > subdev->size)
464 erase->len = subdev->size - erase->addr;
468 length -= erase->len;
469 if ((err = concat_dev_erase(subdev, erase))) {
470 /* sanity check: should never happen since
471 * block alignment has been checked above */
472 BUG_ON(err == -EINVAL);
473 if (erase->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
474 instr->fail_addr = erase->fail_addr + offset;
478 * erase->addr specifies the offset of the area to be
479 * erased *within the current subdevice*. It can be
480 * non-zero only the first time through this loop, i.e.
481 * for the first subdevice where blocks need to be erased.
482 * All the following erases must begin at the start of the
483 * current subdevice, i.e. at offset zero.
486 offset += subdev->size;
488 instr->state = erase->state;
496 static int concat_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
498 struct mtd_concat *concat = CONCAT(mtd);
499 int i, err = -EINVAL;
501 for (i = 0; i < concat->num_subdev; i++) {
502 struct mtd_info *subdev = concat->subdev[i];
505 if (ofs >= subdev->size) {
510 if (ofs + len > subdev->size)
511 size = subdev->size - ofs;
515 err = mtd_lock(subdev, ofs, size);
530 static int concat_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
532 struct mtd_concat *concat = CONCAT(mtd);
535 for (i = 0; i < concat->num_subdev; i++) {
536 struct mtd_info *subdev = concat->subdev[i];
539 if (ofs >= subdev->size) {
544 if (ofs + len > subdev->size)
545 size = subdev->size - ofs;
549 err = mtd_unlock(subdev, ofs, size);
564 static void concat_sync(struct mtd_info *mtd)
566 struct mtd_concat *concat = CONCAT(mtd);
569 for (i = 0; i < concat->num_subdev; i++) {
570 struct mtd_info *subdev = concat->subdev[i];
576 static int concat_suspend(struct mtd_info *mtd)
578 struct mtd_concat *concat = CONCAT(mtd);
581 for (i = 0; i < concat->num_subdev; i++) {
582 struct mtd_info *subdev = concat->subdev[i];
583 if ((rc = mtd_suspend(subdev)) < 0)
589 static void concat_resume(struct mtd_info *mtd)
591 struct mtd_concat *concat = CONCAT(mtd);
594 for (i = 0; i < concat->num_subdev; i++) {
595 struct mtd_info *subdev = concat->subdev[i];
601 static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
603 struct mtd_concat *concat = CONCAT(mtd);
606 if (!mtd_can_have_bb(concat->subdev[0]))
609 for (i = 0; i < concat->num_subdev; i++) {
610 struct mtd_info *subdev = concat->subdev[i];
612 if (ofs >= subdev->size) {
617 res = mtd_block_isbad(subdev, ofs);
624 static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
626 struct mtd_concat *concat = CONCAT(mtd);
627 int i, err = -EINVAL;
629 for (i = 0; i < concat->num_subdev; i++) {
630 struct mtd_info *subdev = concat->subdev[i];
632 if (ofs >= subdev->size) {
637 err = mtd_block_markbad(subdev, ofs);
639 mtd->ecc_stats.badblocks++;
647 * try to support NOMMU mmaps on concatenated devices
648 * - we don't support subdev spanning as we can't guarantee it'll work
650 static unsigned long concat_get_unmapped_area(struct mtd_info *mtd,
652 unsigned long offset,
655 struct mtd_concat *concat = CONCAT(mtd);
658 for (i = 0; i < concat->num_subdev; i++) {
659 struct mtd_info *subdev = concat->subdev[i];
661 if (offset >= subdev->size) {
662 offset -= subdev->size;
666 return mtd_get_unmapped_area(subdev, len, offset, flags);
669 return (unsigned long) -ENOSYS;
673 * This function constructs a virtual MTD device by concatenating
674 * num_devs MTD devices. A pointer to the new device object is
675 * stored to *new_dev upon success. This function does _not_
676 * register any devices: this is the caller's responsibility.
678 struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */
679 int num_devs, /* number of subdevices */
685 { /* name for the new device */
688 struct mtd_concat *concat;
689 uint32_t max_erasesize, curr_erasesize;
690 int num_erase_region;
691 int max_writebufsize = 0;
693 debug("Concatenating MTD devices:\n");
694 for (i = 0; i < num_devs; i++)
695 printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
696 debug("into device \"%s\"\n", name);
698 /* allocate the device structure */
699 size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
700 concat = kzalloc(size, GFP_KERNEL);
703 ("memory allocation error while creating concatenated device \"%s\"\n",
707 concat->subdev = (struct mtd_info **) (concat + 1);
710 * Set up the new "super" device's MTD object structure, check for
711 * incompatibilities between the subdevices.
713 concat->mtd.type = subdev[0]->type;
714 concat->mtd.flags = subdev[0]->flags;
715 concat->mtd.size = subdev[0]->size;
716 concat->mtd.erasesize = subdev[0]->erasesize;
717 concat->mtd.writesize = subdev[0]->writesize;
719 for (i = 0; i < num_devs; i++)
720 if (max_writebufsize < subdev[i]->writebufsize)
721 max_writebufsize = subdev[i]->writebufsize;
722 concat->mtd.writebufsize = max_writebufsize;
724 concat->mtd.subpage_sft = subdev[0]->subpage_sft;
725 concat->mtd.oobsize = subdev[0]->oobsize;
726 concat->mtd.oobavail = subdev[0]->oobavail;
728 if (subdev[0]->_writev)
729 concat->mtd._writev = concat_writev;
731 if (subdev[0]->_read_oob)
732 concat->mtd._read_oob = concat_read_oob;
733 if (subdev[0]->_write_oob)
734 concat->mtd._write_oob = concat_write_oob;
735 if (subdev[0]->_block_isbad)
736 concat->mtd._block_isbad = concat_block_isbad;
737 if (subdev[0]->_block_markbad)
738 concat->mtd._block_markbad = concat_block_markbad;
740 concat->mtd.ecc_stats.badblocks = subdev[0]->ecc_stats.badblocks;
743 concat->mtd.backing_dev_info = subdev[0]->backing_dev_info;
746 concat->subdev[0] = subdev[0];
748 for (i = 1; i < num_devs; i++) {
749 if (concat->mtd.type != subdev[i]->type) {
751 printk("Incompatible device type on \"%s\"\n",
755 if (concat->mtd.flags != subdev[i]->flags) {
757 * Expect all flags except MTD_WRITEABLE to be
758 * equal on all subdevices.
760 if ((concat->mtd.flags ^ subdev[i]->
761 flags) & ~MTD_WRITEABLE) {
763 printk("Incompatible device flags on \"%s\"\n",
767 /* if writeable attribute differs,
768 make super device writeable */
770 subdev[i]->flags & MTD_WRITEABLE;
774 /* only permit direct mapping if the BDIs are all the same
775 * - copy-mapping is still permitted
777 if (concat->mtd.backing_dev_info !=
778 subdev[i]->backing_dev_info)
779 concat->mtd.backing_dev_info =
780 &default_backing_dev_info;
783 concat->mtd.size += subdev[i]->size;
784 concat->mtd.ecc_stats.badblocks +=
785 subdev[i]->ecc_stats.badblocks;
786 if (concat->mtd.writesize != subdev[i]->writesize ||
787 concat->mtd.subpage_sft != subdev[i]->subpage_sft ||
788 concat->mtd.oobsize != subdev[i]->oobsize ||
789 !concat->mtd._read_oob != !subdev[i]->_read_oob ||
790 !concat->mtd._write_oob != !subdev[i]->_write_oob) {
792 printk("Incompatible OOB or ECC data on \"%s\"\n",
796 concat->subdev[i] = subdev[i];
800 concat->mtd.ecclayout = subdev[0]->ecclayout;
802 concat->num_subdev = num_devs;
803 concat->mtd.name = name;
805 concat->mtd._erase = concat_erase;
806 concat->mtd._read = concat_read;
807 concat->mtd._write = concat_write;
808 concat->mtd._sync = concat_sync;
809 concat->mtd._lock = concat_lock;
810 concat->mtd._unlock = concat_unlock;
812 concat->mtd._suspend = concat_suspend;
813 concat->mtd._resume = concat_resume;
815 concat->mtd._get_unmapped_area = concat_get_unmapped_area;
818 * Combine the erase block size info of the subdevices:
820 * first, walk the map of the new device and see how
821 * many changes in erase size we have
823 max_erasesize = curr_erasesize = subdev[0]->erasesize;
824 num_erase_region = 1;
825 for (i = 0; i < num_devs; i++) {
826 if (subdev[i]->numeraseregions == 0) {
827 /* current subdevice has uniform erase size */
828 if (subdev[i]->erasesize != curr_erasesize) {
829 /* if it differs from the last subdevice's erase size, count it */
831 curr_erasesize = subdev[i]->erasesize;
832 if (curr_erasesize > max_erasesize)
833 max_erasesize = curr_erasesize;
836 /* current subdevice has variable erase size */
838 for (j = 0; j < subdev[i]->numeraseregions; j++) {
840 /* walk the list of erase regions, count any changes */
841 if (subdev[i]->eraseregions[j].erasesize !=
845 subdev[i]->eraseregions[j].
847 if (curr_erasesize > max_erasesize)
848 max_erasesize = curr_erasesize;
854 if (num_erase_region == 1) {
856 * All subdevices have the same uniform erase size.
859 concat->mtd.erasesize = curr_erasesize;
860 concat->mtd.numeraseregions = 0;
865 * erase block size varies across the subdevices: allocate
866 * space to store the data describing the variable erase regions
868 struct mtd_erase_region_info *erase_region_p;
869 uint64_t begin, position;
871 concat->mtd.erasesize = max_erasesize;
872 concat->mtd.numeraseregions = num_erase_region;
873 concat->mtd.eraseregions = erase_region_p =
874 kmalloc(num_erase_region *
875 sizeof (struct mtd_erase_region_info), GFP_KERNEL);
876 if (!erase_region_p) {
879 ("memory allocation error while creating erase region list"
880 " for device \"%s\"\n", name);
885 * walk the map of the new device once more and fill in
886 * in erase region info:
888 curr_erasesize = subdev[0]->erasesize;
889 begin = position = 0;
890 for (i = 0; i < num_devs; i++) {
891 if (subdev[i]->numeraseregions == 0) {
892 /* current subdevice has uniform erase size */
893 if (subdev[i]->erasesize != curr_erasesize) {
895 * fill in an mtd_erase_region_info structure for the area
896 * we have walked so far:
898 erase_region_p->offset = begin;
899 erase_region_p->erasesize =
901 tmp64 = position - begin;
902 do_div(tmp64, curr_erasesize);
903 erase_region_p->numblocks = tmp64;
906 curr_erasesize = subdev[i]->erasesize;
909 position += subdev[i]->size;
911 /* current subdevice has variable erase size */
913 for (j = 0; j < subdev[i]->numeraseregions; j++) {
914 /* walk the list of erase regions, count any changes */
915 if (subdev[i]->eraseregions[j].
916 erasesize != curr_erasesize) {
917 erase_region_p->offset = begin;
918 erase_region_p->erasesize =
920 tmp64 = position - begin;
921 do_div(tmp64, curr_erasesize);
922 erase_region_p->numblocks = tmp64;
926 subdev[i]->eraseregions[j].
931 subdev[i]->eraseregions[j].
932 numblocks * (uint64_t)curr_erasesize;
936 /* Now write the final entry */
937 erase_region_p->offset = begin;
938 erase_region_p->erasesize = curr_erasesize;
939 tmp64 = position - begin;
940 do_div(tmp64, curr_erasesize);
941 erase_region_p->numblocks = tmp64;
948 * This function destroys an MTD object obtained from concat_mtd_devs()
951 void mtd_concat_destroy(struct mtd_info *mtd)
953 struct mtd_concat *concat = CONCAT(mtd);
954 if (concat->mtd.numeraseregions)
955 kfree(concat->mtd.eraseregions);
959 EXPORT_SYMBOL(mtd_concat_create);
960 EXPORT_SYMBOL(mtd_concat_destroy);
962 MODULE_LICENSE("GPL");
964 MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");