1 // SPDX-License-Identifier: GPL-2.0-or-later
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>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/sched.h>
15 #include <linux/types.h>
16 #include <linux/backing-dev.h>
18 #include <linux/mtd/mtd.h>
19 #include <linux/mtd/concat.h>
21 #include <asm/div64.h>
24 * Our storage structure:
25 * Subdev points to an array of pointers to struct mtd_info objects
26 * which is allocated along with this structure
32 struct mtd_info **subdev;
36 * how to calculate the size required for the above structure,
37 * including the pointer array subdev points to:
39 #define SIZEOF_STRUCT_MTD_CONCAT(num_subdev) \
40 ((sizeof(struct mtd_concat) + (num_subdev) * sizeof(struct mtd_info *)))
43 * Given a pointer to the MTD object in the mtd_concat structure,
44 * we can retrieve the pointer to that structure with this macro.
46 #define CONCAT(x) ((struct mtd_concat *)(x))
49 * MTD methods which look up the relevant subdevice, translate the
50 * effective address and pass through to the subdevice.
54 concat_read(struct mtd_info *mtd, loff_t from, size_t len,
55 size_t * retlen, u_char * buf)
57 struct mtd_concat *concat = CONCAT(mtd);
61 for (i = 0; i < concat->num_subdev; i++) {
62 struct mtd_info *subdev = concat->subdev[i];
65 if (from >= subdev->size) {
66 /* Not destined for this subdev */
71 if (from + len > subdev->size)
72 /* First part goes into this subdev */
73 size = subdev->size - from;
75 /* Entire transaction goes into this subdev */
78 err = mtd_read(subdev, from, size, &retsize, buf);
80 /* Save information about bitflips! */
82 if (mtd_is_eccerr(err)) {
83 mtd->ecc_stats.failed++;
85 } else if (mtd_is_bitflip(err)) {
86 mtd->ecc_stats.corrected++;
87 /* Do not overwrite -EBADMSG !! */
106 concat_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
107 size_t * retlen, const u_char * buf)
109 struct mtd_concat *concat = CONCAT(mtd);
112 for (i = 0; i < concat->num_subdev; i++) {
113 struct mtd_info *subdev = concat->subdev[i];
114 size_t size, retsize;
116 if (to >= subdev->size) {
120 if (to + len > subdev->size)
121 size = subdev->size - to;
125 err = mtd_panic_write(subdev, to, size, &retsize, buf);
126 if (err == -EOPNOTSUPP) {
127 printk(KERN_ERR "mtdconcat: Cannot write from panic without panic_write\n");
147 concat_write(struct mtd_info *mtd, loff_t to, size_t len,
148 size_t * retlen, const u_char * buf)
150 struct mtd_concat *concat = CONCAT(mtd);
154 for (i = 0; i < concat->num_subdev; i++) {
155 struct mtd_info *subdev = concat->subdev[i];
156 size_t size, retsize;
158 if (to >= subdev->size) {
163 if (to + len > subdev->size)
164 size = subdev->size - to;
168 err = mtd_write(subdev, to, size, &retsize, buf);
185 concat_writev(struct mtd_info *mtd, const struct kvec *vecs,
186 unsigned long count, loff_t to, size_t * retlen)
188 struct mtd_concat *concat = CONCAT(mtd);
189 struct kvec *vecs_copy;
190 unsigned long entry_low, entry_high;
191 size_t total_len = 0;
195 /* Calculate total length of data */
196 for (i = 0; i < count; i++)
197 total_len += vecs[i].iov_len;
199 /* Check alignment */
200 if (mtd->writesize > 1) {
202 if (do_div(__to, mtd->writesize) || (total_len % mtd->writesize))
206 /* make a copy of vecs */
207 vecs_copy = kmemdup(vecs, sizeof(struct kvec) * count, GFP_KERNEL);
212 for (i = 0; i < concat->num_subdev; i++) {
213 struct mtd_info *subdev = concat->subdev[i];
214 size_t size, wsize, retsize, old_iov_len;
216 if (to >= subdev->size) {
221 size = min_t(uint64_t, total_len, subdev->size - to);
222 wsize = size; /* store for future use */
224 entry_high = entry_low;
225 while (entry_high < count) {
226 if (size <= vecs_copy[entry_high].iov_len)
228 size -= vecs_copy[entry_high++].iov_len;
231 old_iov_len = vecs_copy[entry_high].iov_len;
232 vecs_copy[entry_high].iov_len = size;
234 err = mtd_writev(subdev, &vecs_copy[entry_low],
235 entry_high - entry_low + 1, to, &retsize);
237 vecs_copy[entry_high].iov_len = old_iov_len - size;
238 vecs_copy[entry_high].iov_base += size;
240 entry_low = entry_high;
260 concat_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
262 struct mtd_concat *concat = CONCAT(mtd);
263 struct mtd_oob_ops devops = *ops;
266 ops->retlen = ops->oobretlen = 0;
268 for (i = 0; i < concat->num_subdev; i++) {
269 struct mtd_info *subdev = concat->subdev[i];
271 if (from >= subdev->size) {
272 from -= subdev->size;
277 if (from + devops.len > subdev->size)
278 devops.len = subdev->size - from;
280 err = mtd_read_oob(subdev, from, &devops);
281 ops->retlen += devops.retlen;
282 ops->oobretlen += devops.oobretlen;
284 /* Save information about bitflips! */
286 if (mtd_is_eccerr(err)) {
287 mtd->ecc_stats.failed++;
289 } else if (mtd_is_bitflip(err)) {
290 mtd->ecc_stats.corrected++;
291 /* Do not overwrite -EBADMSG !! */
299 devops.len = ops->len - ops->retlen;
302 devops.datbuf += devops.retlen;
305 devops.ooblen = ops->ooblen - ops->oobretlen;
308 devops.oobbuf += ops->oobretlen;
317 concat_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
319 struct mtd_concat *concat = CONCAT(mtd);
320 struct mtd_oob_ops devops = *ops;
323 if (!(mtd->flags & MTD_WRITEABLE))
326 ops->retlen = ops->oobretlen = 0;
328 for (i = 0; i < concat->num_subdev; i++) {
329 struct mtd_info *subdev = concat->subdev[i];
331 if (to >= subdev->size) {
336 /* partial write ? */
337 if (to + devops.len > subdev->size)
338 devops.len = subdev->size - to;
340 err = mtd_write_oob(subdev, to, &devops);
341 ops->retlen += devops.retlen;
342 ops->oobretlen += devops.oobretlen;
347 devops.len = ops->len - ops->retlen;
350 devops.datbuf += devops.retlen;
353 devops.ooblen = ops->ooblen - ops->oobretlen;
356 devops.oobbuf += devops.oobretlen;
363 static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
365 struct mtd_concat *concat = CONCAT(mtd);
366 struct mtd_info *subdev;
368 uint64_t length, offset = 0;
369 struct erase_info *erase;
372 * Check for proper erase block alignment of the to-be-erased area.
373 * It is easier to do this based on the super device's erase
374 * region info rather than looking at each particular sub-device
377 if (!concat->mtd.numeraseregions) {
378 /* the easy case: device has uniform erase block size */
379 if (instr->addr & (concat->mtd.erasesize - 1))
381 if (instr->len & (concat->mtd.erasesize - 1))
384 /* device has variable erase size */
385 struct mtd_erase_region_info *erase_regions =
386 concat->mtd.eraseregions;
389 * Find the erase region where the to-be-erased area begins:
391 for (i = 0; i < concat->mtd.numeraseregions &&
392 instr->addr >= erase_regions[i].offset; i++) ;
396 * Now erase_regions[i] is the region in which the
397 * to-be-erased area begins. Verify that the starting
398 * offset is aligned to this region's erase size:
400 if (i < 0 || instr->addr & (erase_regions[i].erasesize - 1))
404 * now find the erase region where the to-be-erased area ends:
406 for (; i < concat->mtd.numeraseregions &&
407 (instr->addr + instr->len) >= erase_regions[i].offset;
411 * check if the ending offset is aligned to this region's erase size
413 if (i < 0 || ((instr->addr + instr->len) &
414 (erase_regions[i].erasesize - 1)))
418 /* make a local copy of instr to avoid modifying the caller's struct */
419 erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
428 * find the subdevice where the to-be-erased area begins, adjust
429 * starting offset to be relative to the subdevice start
431 for (i = 0; i < concat->num_subdev; i++) {
432 subdev = concat->subdev[i];
433 if (subdev->size <= erase->addr) {
434 erase->addr -= subdev->size;
435 offset += subdev->size;
441 /* must never happen since size limit has been verified above */
442 BUG_ON(i >= concat->num_subdev);
444 /* now do the erase: */
446 for (; length > 0; i++) {
447 /* loop for all subdevices affected by this request */
448 subdev = concat->subdev[i]; /* get current subdevice */
450 /* limit length to subdevice's size: */
451 if (erase->addr + length > subdev->size)
452 erase->len = subdev->size - erase->addr;
456 length -= erase->len;
457 if ((err = mtd_erase(subdev, erase))) {
458 /* sanity check: should never happen since
459 * block alignment has been checked above */
460 BUG_ON(err == -EINVAL);
461 if (erase->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
462 instr->fail_addr = erase->fail_addr + offset;
466 * erase->addr specifies the offset of the area to be
467 * erased *within the current subdevice*. It can be
468 * non-zero only the first time through this loop, i.e.
469 * for the first subdevice where blocks need to be erased.
470 * All the following erases must begin at the start of the
471 * current subdevice, i.e. at offset zero.
474 offset += subdev->size;
481 static int concat_xxlock(struct mtd_info *mtd, loff_t ofs, uint64_t len,
484 struct mtd_concat *concat = CONCAT(mtd);
485 int i, err = -EINVAL;
487 for (i = 0; i < concat->num_subdev; i++) {
488 struct mtd_info *subdev = concat->subdev[i];
491 if (ofs >= subdev->size) {
496 if (ofs + len > subdev->size)
497 size = subdev->size - ofs;
502 err = mtd_lock(subdev, ofs, size);
504 err = mtd_unlock(subdev, ofs, size);
519 static int concat_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
521 return concat_xxlock(mtd, ofs, len, true);
524 static int concat_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
526 return concat_xxlock(mtd, ofs, len, false);
529 static int concat_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
531 struct mtd_concat *concat = CONCAT(mtd);
532 int i, err = -EINVAL;
534 for (i = 0; i < concat->num_subdev; i++) {
535 struct mtd_info *subdev = concat->subdev[i];
537 if (ofs >= subdev->size) {
542 if (ofs + len > subdev->size)
545 return mtd_is_locked(subdev, ofs, len);
551 static void concat_sync(struct mtd_info *mtd)
553 struct mtd_concat *concat = CONCAT(mtd);
556 for (i = 0; i < concat->num_subdev; i++) {
557 struct mtd_info *subdev = concat->subdev[i];
562 static int concat_suspend(struct mtd_info *mtd)
564 struct mtd_concat *concat = CONCAT(mtd);
567 for (i = 0; i < concat->num_subdev; i++) {
568 struct mtd_info *subdev = concat->subdev[i];
569 if ((rc = mtd_suspend(subdev)) < 0)
575 static void concat_resume(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];
586 static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
588 struct mtd_concat *concat = CONCAT(mtd);
591 if (!mtd_can_have_bb(concat->subdev[0]))
594 for (i = 0; i < concat->num_subdev; i++) {
595 struct mtd_info *subdev = concat->subdev[i];
597 if (ofs >= subdev->size) {
602 res = mtd_block_isbad(subdev, ofs);
609 static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
611 struct mtd_concat *concat = CONCAT(mtd);
612 int i, err = -EINVAL;
614 for (i = 0; i < concat->num_subdev; i++) {
615 struct mtd_info *subdev = concat->subdev[i];
617 if (ofs >= subdev->size) {
622 err = mtd_block_markbad(subdev, ofs);
624 mtd->ecc_stats.badblocks++;
632 * This function constructs a virtual MTD device by concatenating
633 * num_devs MTD devices. A pointer to the new device object is
634 * stored to *new_dev upon success. This function does _not_
635 * register any devices: this is the caller's responsibility.
637 struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */
638 int num_devs, /* number of subdevices */
640 { /* name for the new device */
643 struct mtd_concat *concat;
644 uint32_t max_erasesize, curr_erasesize;
645 int num_erase_region;
646 int max_writebufsize = 0;
648 printk(KERN_NOTICE "Concatenating MTD devices:\n");
649 for (i = 0; i < num_devs; i++)
650 printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
651 printk(KERN_NOTICE "into device \"%s\"\n", name);
653 /* allocate the device structure */
654 size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
655 concat = kzalloc(size, GFP_KERNEL);
658 ("memory allocation error while creating concatenated device \"%s\"\n",
662 concat->subdev = (struct mtd_info **) (concat + 1);
665 * Set up the new "super" device's MTD object structure, check for
666 * incompatibilities between the subdevices.
668 concat->mtd.type = subdev[0]->type;
669 concat->mtd.flags = subdev[0]->flags;
670 concat->mtd.size = subdev[0]->size;
671 concat->mtd.erasesize = subdev[0]->erasesize;
672 concat->mtd.writesize = subdev[0]->writesize;
674 for (i = 0; i < num_devs; i++)
675 if (max_writebufsize < subdev[i]->writebufsize)
676 max_writebufsize = subdev[i]->writebufsize;
677 concat->mtd.writebufsize = max_writebufsize;
679 concat->mtd.subpage_sft = subdev[0]->subpage_sft;
680 concat->mtd.oobsize = subdev[0]->oobsize;
681 concat->mtd.oobavail = subdev[0]->oobavail;
682 if (subdev[0]->_writev)
683 concat->mtd._writev = concat_writev;
684 if (subdev[0]->_read_oob)
685 concat->mtd._read_oob = concat_read_oob;
686 if (subdev[0]->_write_oob)
687 concat->mtd._write_oob = concat_write_oob;
688 if (subdev[0]->_block_isbad)
689 concat->mtd._block_isbad = concat_block_isbad;
690 if (subdev[0]->_block_markbad)
691 concat->mtd._block_markbad = concat_block_markbad;
692 if (subdev[0]->_panic_write)
693 concat->mtd._panic_write = concat_panic_write;
695 concat->mtd.ecc_stats.badblocks = subdev[0]->ecc_stats.badblocks;
697 concat->subdev[0] = subdev[0];
699 for (i = 1; i < num_devs; i++) {
700 if (concat->mtd.type != subdev[i]->type) {
702 printk("Incompatible device type on \"%s\"\n",
706 if (concat->mtd.flags != subdev[i]->flags) {
708 * Expect all flags except MTD_WRITEABLE to be
709 * equal on all subdevices.
711 if ((concat->mtd.flags ^ subdev[i]->
712 flags) & ~MTD_WRITEABLE) {
714 printk("Incompatible device flags on \"%s\"\n",
718 /* if writeable attribute differs,
719 make super device writeable */
721 subdev[i]->flags & MTD_WRITEABLE;
724 concat->mtd.size += subdev[i]->size;
725 concat->mtd.ecc_stats.badblocks +=
726 subdev[i]->ecc_stats.badblocks;
727 if (concat->mtd.writesize != subdev[i]->writesize ||
728 concat->mtd.subpage_sft != subdev[i]->subpage_sft ||
729 concat->mtd.oobsize != subdev[i]->oobsize ||
730 !concat->mtd._read_oob != !subdev[i]->_read_oob ||
731 !concat->mtd._write_oob != !subdev[i]->_write_oob) {
733 printk("Incompatible OOB or ECC data on \"%s\"\n",
737 concat->subdev[i] = subdev[i];
741 mtd_set_ooblayout(&concat->mtd, subdev[0]->ooblayout);
743 concat->num_subdev = num_devs;
744 concat->mtd.name = name;
746 concat->mtd._erase = concat_erase;
747 concat->mtd._read = concat_read;
748 concat->mtd._write = concat_write;
749 concat->mtd._sync = concat_sync;
750 concat->mtd._lock = concat_lock;
751 concat->mtd._unlock = concat_unlock;
752 concat->mtd._is_locked = concat_is_locked;
753 concat->mtd._suspend = concat_suspend;
754 concat->mtd._resume = concat_resume;
757 * Combine the erase block size info of the subdevices:
759 * first, walk the map of the new device and see how
760 * many changes in erase size we have
762 max_erasesize = curr_erasesize = subdev[0]->erasesize;
763 num_erase_region = 1;
764 for (i = 0; i < num_devs; i++) {
765 if (subdev[i]->numeraseregions == 0) {
766 /* current subdevice has uniform erase size */
767 if (subdev[i]->erasesize != curr_erasesize) {
768 /* if it differs from the last subdevice's erase size, count it */
770 curr_erasesize = subdev[i]->erasesize;
771 if (curr_erasesize > max_erasesize)
772 max_erasesize = curr_erasesize;
775 /* current subdevice has variable erase size */
777 for (j = 0; j < subdev[i]->numeraseregions; j++) {
779 /* walk the list of erase regions, count any changes */
780 if (subdev[i]->eraseregions[j].erasesize !=
784 subdev[i]->eraseregions[j].
786 if (curr_erasesize > max_erasesize)
787 max_erasesize = curr_erasesize;
793 if (num_erase_region == 1) {
795 * All subdevices have the same uniform erase size.
798 concat->mtd.erasesize = curr_erasesize;
799 concat->mtd.numeraseregions = 0;
804 * erase block size varies across the subdevices: allocate
805 * space to store the data describing the variable erase regions
807 struct mtd_erase_region_info *erase_region_p;
808 uint64_t begin, position;
810 concat->mtd.erasesize = max_erasesize;
811 concat->mtd.numeraseregions = num_erase_region;
812 concat->mtd.eraseregions = erase_region_p =
813 kmalloc_array(num_erase_region,
814 sizeof(struct mtd_erase_region_info),
816 if (!erase_region_p) {
819 ("memory allocation error while creating erase region list"
820 " for device \"%s\"\n", name);
825 * walk the map of the new device once more and fill in
826 * in erase region info:
828 curr_erasesize = subdev[0]->erasesize;
829 begin = position = 0;
830 for (i = 0; i < num_devs; i++) {
831 if (subdev[i]->numeraseregions == 0) {
832 /* current subdevice has uniform erase size */
833 if (subdev[i]->erasesize != curr_erasesize) {
835 * fill in an mtd_erase_region_info structure for the area
836 * we have walked so far:
838 erase_region_p->offset = begin;
839 erase_region_p->erasesize =
841 tmp64 = position - begin;
842 do_div(tmp64, curr_erasesize);
843 erase_region_p->numblocks = tmp64;
846 curr_erasesize = subdev[i]->erasesize;
849 position += subdev[i]->size;
851 /* current subdevice has variable erase size */
853 for (j = 0; j < subdev[i]->numeraseregions; j++) {
854 /* walk the list of erase regions, count any changes */
855 if (subdev[i]->eraseregions[j].
856 erasesize != curr_erasesize) {
857 erase_region_p->offset = begin;
858 erase_region_p->erasesize =
860 tmp64 = position - begin;
861 do_div(tmp64, curr_erasesize);
862 erase_region_p->numblocks = tmp64;
866 subdev[i]->eraseregions[j].
871 subdev[i]->eraseregions[j].
872 numblocks * (uint64_t)curr_erasesize;
876 /* Now write the final entry */
877 erase_region_p->offset = begin;
878 erase_region_p->erasesize = curr_erasesize;
879 tmp64 = position - begin;
880 do_div(tmp64, curr_erasesize);
881 erase_region_p->numblocks = tmp64;
887 /* Cleans the context obtained from mtd_concat_create() */
888 void mtd_concat_destroy(struct mtd_info *mtd)
890 struct mtd_concat *concat = CONCAT(mtd);
891 if (concat->mtd.numeraseregions)
892 kfree(concat->mtd.eraseregions);
896 EXPORT_SYMBOL(mtd_concat_create);
897 EXPORT_SYMBOL(mtd_concat_destroy);
899 MODULE_LICENSE("GPL");
900 MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
901 MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");