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>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/sched.h>
17 #include <linux/types.h>
18 #include <linux/backing-dev.h>
19 #include <asm/div64.h>
22 #include <linux/compat.h>
25 #include <linux/mtd/mtd.h>
26 #include <linux/mtd/concat.h>
28 #include <ubi_uboot.h>
31 * Our storage structure:
32 * Subdev points to an array of pointers to struct mtd_info objects
33 * which is allocated along with this structure
39 struct mtd_info **subdev;
43 * how to calculate the size required for the above structure,
44 * including the pointer array subdev points to:
46 #define SIZEOF_STRUCT_MTD_CONCAT(num_subdev) \
47 ((sizeof(struct mtd_concat) + (num_subdev) * sizeof(struct mtd_info *)))
50 * Given a pointer to the MTD object in the mtd_concat structure,
51 * we can retrieve the pointer to that structure with this macro.
53 #define CONCAT(x) ((struct mtd_concat *)(x))
56 * MTD methods which look up the relevant subdevice, translate the
57 * effective address and pass through to the subdevice.
61 concat_read(struct mtd_info *mtd, loff_t from, size_t len,
62 size_t * retlen, u_char * buf)
64 struct mtd_concat *concat = CONCAT(mtd);
72 for (i = 0; i < concat->num_subdev; i++) {
73 struct mtd_info *subdev = concat->subdev[i];
76 if (from >= subdev->size) {
77 /* Not destined for this subdev */
82 if (from + len > subdev->size)
83 /* First part goes into this subdev */
84 size = subdev->size - from;
86 /* Entire transaction goes into this subdev */
89 err = mtd_read(subdev, from, size, &retsize, buf);
91 /* Save information about bitflips! */
93 if (mtd_is_eccerr(err)) {
94 mtd->ecc_stats.failed++;
96 } else if (mtd_is_bitflip(err)) {
97 mtd->ecc_stats.corrected++;
98 /* Do not overwrite -EBADMSG !! */
117 concat_write(struct mtd_info *mtd, loff_t to, size_t len,
118 size_t * retlen, const u_char * buf)
120 struct mtd_concat *concat = CONCAT(mtd);
128 for (i = 0; i < concat->num_subdev; i++) {
129 struct mtd_info *subdev = concat->subdev[i];
130 size_t size, retsize;
132 if (to >= subdev->size) {
137 if (to + len > subdev->size)
138 size = subdev->size - to;
142 err = mtd_write(subdev, to, size, &retsize, buf);
160 concat_writev(struct mtd_info *mtd, const struct kvec *vecs,
161 unsigned long count, loff_t to, size_t * retlen)
163 struct mtd_concat *concat = CONCAT(mtd);
164 struct kvec *vecs_copy;
165 unsigned long entry_low, entry_high;
166 size_t total_len = 0;
170 /* Calculate total length of data */
171 for (i = 0; i < count; i++)
172 total_len += vecs[i].iov_len;
174 /* Check alignment */
175 if (mtd->writesize > 1) {
177 if (do_div(__to, mtd->writesize) || (total_len % mtd->writesize))
181 /* make a copy of vecs */
182 vecs_copy = kmemdup(vecs, sizeof(struct kvec) * count, GFP_KERNEL);
187 for (i = 0; i < concat->num_subdev; i++) {
188 struct mtd_info *subdev = concat->subdev[i];
189 size_t size, wsize, retsize, old_iov_len;
191 if (to >= subdev->size) {
196 size = min_t(uint64_t, total_len, subdev->size - to);
197 wsize = size; /* store for future use */
199 entry_high = entry_low;
200 while (entry_high < count) {
201 if (size <= vecs_copy[entry_high].iov_len)
203 size -= vecs_copy[entry_high++].iov_len;
206 old_iov_len = vecs_copy[entry_high].iov_len;
207 vecs_copy[entry_high].iov_len = size;
209 err = mtd_writev(subdev, &vecs_copy[entry_low],
210 entry_high - entry_low + 1, to, &retsize);
212 vecs_copy[entry_high].iov_len = old_iov_len - size;
213 vecs_copy[entry_high].iov_base += size;
215 entry_low = entry_high;
236 concat_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
238 struct mtd_concat *concat = CONCAT(mtd);
239 struct mtd_oob_ops devops = *ops;
242 ops->retlen = ops->oobretlen = 0;
244 for (i = 0; i < concat->num_subdev; i++) {
245 struct mtd_info *subdev = concat->subdev[i];
247 if (from >= subdev->size) {
248 from -= subdev->size;
253 if (from + devops.len > subdev->size)
254 devops.len = subdev->size - from;
256 err = mtd_read_oob(subdev, from, &devops);
257 ops->retlen += devops.retlen;
258 ops->oobretlen += devops.oobretlen;
260 /* Save information about bitflips! */
262 if (mtd_is_eccerr(err)) {
263 mtd->ecc_stats.failed++;
265 } else if (mtd_is_bitflip(err)) {
266 mtd->ecc_stats.corrected++;
267 /* Do not overwrite -EBADMSG !! */
275 devops.len = ops->len - ops->retlen;
278 devops.datbuf += devops.retlen;
281 devops.ooblen = ops->ooblen - ops->oobretlen;
284 devops.oobbuf += ops->oobretlen;
293 concat_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
295 struct mtd_concat *concat = CONCAT(mtd);
296 struct mtd_oob_ops devops = *ops;
299 if (!(mtd->flags & MTD_WRITEABLE))
302 ops->retlen = ops->oobretlen = 0;
304 for (i = 0; i < concat->num_subdev; i++) {
305 struct mtd_info *subdev = concat->subdev[i];
307 if (to >= subdev->size) {
312 /* partial write ? */
313 if (to + devops.len > subdev->size)
314 devops.len = subdev->size - to;
316 err = mtd_write_oob(subdev, to, &devops);
317 ops->retlen += devops.oobretlen;
322 devops.len = ops->len - ops->retlen;
325 devops.datbuf += devops.retlen;
328 devops.ooblen = ops->ooblen - ops->oobretlen;
331 devops.oobbuf += devops.oobretlen;
338 static void concat_erase_callback(struct erase_info *instr)
340 /* Nothing to do here in U-Boot */
342 wake_up((wait_queue_head_t *) instr->priv);
346 static int concat_dev_erase(struct mtd_info *mtd, struct erase_info *erase)
349 wait_queue_head_t waitq;
350 DECLARE_WAITQUEUE(wait, current);
353 * This code was stol^H^H^H^Hinspired by mtdchar.c
355 init_waitqueue_head(&waitq);
358 erase->callback = concat_erase_callback;
359 erase->priv = (unsigned long) &waitq;
362 * FIXME: Allow INTERRUPTIBLE. Which means
363 * not having the wait_queue head on the stack.
365 err = mtd_erase(mtd, erase);
367 set_current_state(TASK_UNINTERRUPTIBLE);
368 add_wait_queue(&waitq, &wait);
369 if (erase->state != MTD_ERASE_DONE
370 && erase->state != MTD_ERASE_FAILED)
372 remove_wait_queue(&waitq, &wait);
373 set_current_state(TASK_RUNNING);
375 err = (erase->state == MTD_ERASE_FAILED) ? -EIO : 0;
380 static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
382 struct mtd_concat *concat = CONCAT(mtd);
383 struct mtd_info *subdev;
385 uint64_t length, offset = 0;
386 struct erase_info *erase;
389 * Check for proper erase block alignment of the to-be-erased area.
390 * It is easier to do this based on the super device's erase
391 * region info rather than looking at each particular sub-device
394 if (!concat->mtd.numeraseregions) {
395 /* the easy case: device has uniform erase block size */
396 if (instr->addr & (concat->mtd.erasesize - 1))
398 if (instr->len & (concat->mtd.erasesize - 1))
401 /* device has variable erase size */
402 struct mtd_erase_region_info *erase_regions =
403 concat->mtd.eraseregions;
406 * Find the erase region where the to-be-erased area begins:
408 for (i = 0; i < concat->mtd.numeraseregions &&
409 instr->addr >= erase_regions[i].offset; i++) ;
413 * Now erase_regions[i] is the region in which the
414 * to-be-erased area begins. Verify that the starting
415 * offset is aligned to this region's erase size:
417 if (i < 0 || instr->addr & (erase_regions[i].erasesize - 1))
421 * now find the erase region where the to-be-erased area ends:
423 for (; i < concat->mtd.numeraseregions &&
424 (instr->addr + instr->len) >= erase_regions[i].offset;
428 * check if the ending offset is aligned to this region's erase size
430 if (i < 0 || ((instr->addr + instr->len) &
431 (erase_regions[i].erasesize - 1)))
435 /* make a local copy of instr to avoid modifying the caller's struct */
436 erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
445 * find the subdevice where the to-be-erased area begins, adjust
446 * starting offset to be relative to the subdevice start
448 for (i = 0; i < concat->num_subdev; i++) {
449 subdev = concat->subdev[i];
450 if (subdev->size <= erase->addr) {
451 erase->addr -= subdev->size;
452 offset += subdev->size;
458 /* must never happen since size limit has been verified above */
459 BUG_ON(i >= concat->num_subdev);
461 /* now do the erase: */
463 for (; length > 0; i++) {
464 /* loop for all subdevices affected by this request */
465 subdev = concat->subdev[i]; /* get current subdevice */
467 /* limit length to subdevice's size: */
468 if (erase->addr + length > subdev->size)
469 erase->len = subdev->size - erase->addr;
473 length -= erase->len;
474 if ((err = concat_dev_erase(subdev, erase))) {
475 /* sanity check: should never happen since
476 * block alignment has been checked above */
477 BUG_ON(err == -EINVAL);
478 if (erase->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
479 instr->fail_addr = erase->fail_addr + offset;
483 * erase->addr specifies the offset of the area to be
484 * erased *within the current subdevice*. It can be
485 * non-zero only the first time through this loop, i.e.
486 * for the first subdevice where blocks need to be erased.
487 * All the following erases must begin at the start of the
488 * current subdevice, i.e. at offset zero.
491 offset += subdev->size;
493 instr->state = erase->state;
499 instr->callback(instr);
503 static int concat_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
505 struct mtd_concat *concat = CONCAT(mtd);
506 int i, err = -EINVAL;
508 for (i = 0; i < concat->num_subdev; i++) {
509 struct mtd_info *subdev = concat->subdev[i];
512 if (ofs >= subdev->size) {
517 if (ofs + len > subdev->size)
518 size = subdev->size - ofs;
522 err = mtd_lock(subdev, ofs, size);
537 static int concat_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
539 struct mtd_concat *concat = CONCAT(mtd);
542 for (i = 0; i < concat->num_subdev; i++) {
543 struct mtd_info *subdev = concat->subdev[i];
546 if (ofs >= subdev->size) {
551 if (ofs + len > subdev->size)
552 size = subdev->size - ofs;
556 err = mtd_unlock(subdev, ofs, size);
571 static void concat_sync(struct mtd_info *mtd)
573 struct mtd_concat *concat = CONCAT(mtd);
576 for (i = 0; i < concat->num_subdev; i++) {
577 struct mtd_info *subdev = concat->subdev[i];
583 static int concat_suspend(struct mtd_info *mtd)
585 struct mtd_concat *concat = CONCAT(mtd);
588 for (i = 0; i < concat->num_subdev; i++) {
589 struct mtd_info *subdev = concat->subdev[i];
590 if ((rc = mtd_suspend(subdev)) < 0)
596 static void concat_resume(struct mtd_info *mtd)
598 struct mtd_concat *concat = CONCAT(mtd);
601 for (i = 0; i < concat->num_subdev; i++) {
602 struct mtd_info *subdev = concat->subdev[i];
608 static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
610 struct mtd_concat *concat = CONCAT(mtd);
613 if (!mtd_can_have_bb(concat->subdev[0]))
616 for (i = 0; i < concat->num_subdev; i++) {
617 struct mtd_info *subdev = concat->subdev[i];
619 if (ofs >= subdev->size) {
624 res = mtd_block_isbad(subdev, ofs);
631 static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
633 struct mtd_concat *concat = CONCAT(mtd);
634 int i, err = -EINVAL;
636 for (i = 0; i < concat->num_subdev; i++) {
637 struct mtd_info *subdev = concat->subdev[i];
639 if (ofs >= subdev->size) {
644 err = mtd_block_markbad(subdev, ofs);
646 mtd->ecc_stats.badblocks++;
654 * try to support NOMMU mmaps on concatenated devices
655 * - we don't support subdev spanning as we can't guarantee it'll work
657 static unsigned long concat_get_unmapped_area(struct mtd_info *mtd,
659 unsigned long offset,
662 struct mtd_concat *concat = CONCAT(mtd);
665 for (i = 0; i < concat->num_subdev; i++) {
666 struct mtd_info *subdev = concat->subdev[i];
668 if (offset >= subdev->size) {
669 offset -= subdev->size;
673 return mtd_get_unmapped_area(subdev, len, offset, flags);
676 return (unsigned long) -ENOSYS;
680 * This function constructs a virtual MTD device by concatenating
681 * num_devs MTD devices. A pointer to the new device object is
682 * stored to *new_dev upon success. This function does _not_
683 * register any devices: this is the caller's responsibility.
685 struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */
686 int num_devs, /* number of subdevices */
692 { /* name for the new device */
695 struct mtd_concat *concat;
696 uint32_t max_erasesize, curr_erasesize;
697 int num_erase_region;
698 int max_writebufsize = 0;
700 debug("Concatenating MTD devices:\n");
701 for (i = 0; i < num_devs; i++)
702 printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
703 debug("into device \"%s\"\n", name);
705 /* allocate the device structure */
706 size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
707 concat = kzalloc(size, GFP_KERNEL);
710 ("memory allocation error while creating concatenated device \"%s\"\n",
714 concat->subdev = (struct mtd_info **) (concat + 1);
717 * Set up the new "super" device's MTD object structure, check for
718 * incompatibilities between the subdevices.
720 concat->mtd.type = subdev[0]->type;
721 concat->mtd.flags = subdev[0]->flags;
722 concat->mtd.size = subdev[0]->size;
723 concat->mtd.erasesize = subdev[0]->erasesize;
724 concat->mtd.writesize = subdev[0]->writesize;
726 for (i = 0; i < num_devs; i++)
727 if (max_writebufsize < subdev[i]->writebufsize)
728 max_writebufsize = subdev[i]->writebufsize;
729 concat->mtd.writebufsize = max_writebufsize;
731 concat->mtd.subpage_sft = subdev[0]->subpage_sft;
732 concat->mtd.oobsize = subdev[0]->oobsize;
733 concat->mtd.oobavail = subdev[0]->oobavail;
735 if (subdev[0]->_writev)
736 concat->mtd._writev = concat_writev;
738 if (subdev[0]->_read_oob)
739 concat->mtd._read_oob = concat_read_oob;
740 if (subdev[0]->_write_oob)
741 concat->mtd._write_oob = concat_write_oob;
742 if (subdev[0]->_block_isbad)
743 concat->mtd._block_isbad = concat_block_isbad;
744 if (subdev[0]->_block_markbad)
745 concat->mtd._block_markbad = concat_block_markbad;
747 concat->mtd.ecc_stats.badblocks = subdev[0]->ecc_stats.badblocks;
750 concat->mtd.backing_dev_info = subdev[0]->backing_dev_info;
753 concat->subdev[0] = subdev[0];
755 for (i = 1; i < num_devs; i++) {
756 if (concat->mtd.type != subdev[i]->type) {
758 printk("Incompatible device type on \"%s\"\n",
762 if (concat->mtd.flags != subdev[i]->flags) {
764 * Expect all flags except MTD_WRITEABLE to be
765 * equal on all subdevices.
767 if ((concat->mtd.flags ^ subdev[i]->
768 flags) & ~MTD_WRITEABLE) {
770 printk("Incompatible device flags on \"%s\"\n",
774 /* if writeable attribute differs,
775 make super device writeable */
777 subdev[i]->flags & MTD_WRITEABLE;
781 /* only permit direct mapping if the BDIs are all the same
782 * - copy-mapping is still permitted
784 if (concat->mtd.backing_dev_info !=
785 subdev[i]->backing_dev_info)
786 concat->mtd.backing_dev_info =
787 &default_backing_dev_info;
790 concat->mtd.size += subdev[i]->size;
791 concat->mtd.ecc_stats.badblocks +=
792 subdev[i]->ecc_stats.badblocks;
793 if (concat->mtd.writesize != subdev[i]->writesize ||
794 concat->mtd.subpage_sft != subdev[i]->subpage_sft ||
795 concat->mtd.oobsize != subdev[i]->oobsize ||
796 !concat->mtd._read_oob != !subdev[i]->_read_oob ||
797 !concat->mtd._write_oob != !subdev[i]->_write_oob) {
799 printk("Incompatible OOB or ECC data on \"%s\"\n",
803 concat->subdev[i] = subdev[i];
807 concat->mtd.ecclayout = subdev[0]->ecclayout;
809 concat->num_subdev = num_devs;
810 concat->mtd.name = name;
812 concat->mtd._erase = concat_erase;
813 concat->mtd._read = concat_read;
814 concat->mtd._write = concat_write;
815 concat->mtd._sync = concat_sync;
816 concat->mtd._lock = concat_lock;
817 concat->mtd._unlock = concat_unlock;
819 concat->mtd._suspend = concat_suspend;
820 concat->mtd._resume = concat_resume;
822 concat->mtd._get_unmapped_area = concat_get_unmapped_area;
825 * Combine the erase block size info of the subdevices:
827 * first, walk the map of the new device and see how
828 * many changes in erase size we have
830 max_erasesize = curr_erasesize = subdev[0]->erasesize;
831 num_erase_region = 1;
832 for (i = 0; i < num_devs; i++) {
833 if (subdev[i]->numeraseregions == 0) {
834 /* current subdevice has uniform erase size */
835 if (subdev[i]->erasesize != curr_erasesize) {
836 /* if it differs from the last subdevice's erase size, count it */
838 curr_erasesize = subdev[i]->erasesize;
839 if (curr_erasesize > max_erasesize)
840 max_erasesize = curr_erasesize;
843 /* current subdevice has variable erase size */
845 for (j = 0; j < subdev[i]->numeraseregions; j++) {
847 /* walk the list of erase regions, count any changes */
848 if (subdev[i]->eraseregions[j].erasesize !=
852 subdev[i]->eraseregions[j].
854 if (curr_erasesize > max_erasesize)
855 max_erasesize = curr_erasesize;
861 if (num_erase_region == 1) {
863 * All subdevices have the same uniform erase size.
866 concat->mtd.erasesize = curr_erasesize;
867 concat->mtd.numeraseregions = 0;
872 * erase block size varies across the subdevices: allocate
873 * space to store the data describing the variable erase regions
875 struct mtd_erase_region_info *erase_region_p;
876 uint64_t begin, position;
878 concat->mtd.erasesize = max_erasesize;
879 concat->mtd.numeraseregions = num_erase_region;
880 concat->mtd.eraseregions = erase_region_p =
881 kmalloc(num_erase_region *
882 sizeof (struct mtd_erase_region_info), GFP_KERNEL);
883 if (!erase_region_p) {
886 ("memory allocation error while creating erase region list"
887 " for device \"%s\"\n", name);
892 * walk the map of the new device once more and fill in
893 * in erase region info:
895 curr_erasesize = subdev[0]->erasesize;
896 begin = position = 0;
897 for (i = 0; i < num_devs; i++) {
898 if (subdev[i]->numeraseregions == 0) {
899 /* current subdevice has uniform erase size */
900 if (subdev[i]->erasesize != curr_erasesize) {
902 * fill in an mtd_erase_region_info structure for the area
903 * we have walked so far:
905 erase_region_p->offset = begin;
906 erase_region_p->erasesize =
908 tmp64 = position - begin;
909 do_div(tmp64, curr_erasesize);
910 erase_region_p->numblocks = tmp64;
913 curr_erasesize = subdev[i]->erasesize;
916 position += subdev[i]->size;
918 /* current subdevice has variable erase size */
920 for (j = 0; j < subdev[i]->numeraseregions; j++) {
921 /* walk the list of erase regions, count any changes */
922 if (subdev[i]->eraseregions[j].
923 erasesize != curr_erasesize) {
924 erase_region_p->offset = begin;
925 erase_region_p->erasesize =
927 tmp64 = position - begin;
928 do_div(tmp64, curr_erasesize);
929 erase_region_p->numblocks = tmp64;
933 subdev[i]->eraseregions[j].
938 subdev[i]->eraseregions[j].
939 numblocks * (uint64_t)curr_erasesize;
943 /* Now write the final entry */
944 erase_region_p->offset = begin;
945 erase_region_p->erasesize = curr_erasesize;
946 tmp64 = position - begin;
947 do_div(tmp64, curr_erasesize);
948 erase_region_p->numblocks = tmp64;
955 * This function destroys an MTD object obtained from concat_mtd_devs()
958 void mtd_concat_destroy(struct mtd_info *mtd)
960 struct mtd_concat *concat = CONCAT(mtd);
961 if (concat->mtd.numeraseregions)
962 kfree(concat->mtd.eraseregions);
966 EXPORT_SYMBOL(mtd_concat_create);
967 EXPORT_SYMBOL(mtd_concat_destroy);
969 MODULE_LICENSE("GPL");
970 MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
971 MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");