2 * MTD device concatenation layer
4 * Copyright © 2002 Robert Kaiser <rkaiser@sysgo.de>
5 * Copyright © 2002-2010 David Woodhouse <dwmw2@infradead.org>
7 * NAND support by Christian Gan <cgan@iders.ca>
9 * SPDX-License-Identifier: GPL-2.0+
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/sched.h>
18 #include <linux/types.h>
19 #include <linux/backing-dev.h>
20 #include <asm/div64.h>
23 #include <linux/compat.h>
26 #include <linux/mtd/mtd.h>
27 #include <linux/mtd/concat.h>
29 #include <ubi_uboot.h>
32 * Our storage structure:
33 * Subdev points to an array of pointers to struct mtd_info objects
34 * which is allocated along with this structure
40 struct mtd_info **subdev;
44 * how to calculate the size required for the above structure,
45 * including the pointer array subdev points to:
47 #define SIZEOF_STRUCT_MTD_CONCAT(num_subdev) \
48 ((sizeof(struct mtd_concat) + (num_subdev) * sizeof(struct mtd_info *)))
51 * Given a pointer to the MTD object in the mtd_concat structure,
52 * we can retrieve the pointer to that structure with this macro.
54 #define CONCAT(x) ((struct mtd_concat *)(x))
57 * MTD methods which look up the relevant subdevice, translate the
58 * effective address and pass through to the subdevice.
62 concat_read(struct mtd_info *mtd, loff_t from, size_t len,
63 size_t * retlen, u_char * buf)
65 struct mtd_concat *concat = CONCAT(mtd);
73 for (i = 0; i < concat->num_subdev; i++) {
74 struct mtd_info *subdev = concat->subdev[i];
77 if (from >= subdev->size) {
78 /* Not destined for this subdev */
83 if (from + len > subdev->size)
84 /* First part goes into this subdev */
85 size = subdev->size - from;
87 /* Entire transaction goes into this subdev */
90 err = mtd_read(subdev, from, size, &retsize, buf);
92 /* Save information about bitflips! */
94 if (mtd_is_eccerr(err)) {
95 mtd->ecc_stats.failed++;
97 } else if (mtd_is_bitflip(err)) {
98 mtd->ecc_stats.corrected++;
99 /* Do not overwrite -EBADMSG !! */
118 concat_write(struct mtd_info *mtd, loff_t to, size_t len,
119 size_t * retlen, const u_char * buf)
121 struct mtd_concat *concat = CONCAT(mtd);
129 for (i = 0; i < concat->num_subdev; i++) {
130 struct mtd_info *subdev = concat->subdev[i];
131 size_t size, retsize;
133 if (to >= subdev->size) {
138 if (to + len > subdev->size)
139 size = subdev->size - to;
143 err = mtd_write(subdev, to, size, &retsize, buf);
161 concat_writev(struct mtd_info *mtd, const struct kvec *vecs,
162 unsigned long count, loff_t to, size_t * retlen)
164 struct mtd_concat *concat = CONCAT(mtd);
165 struct kvec *vecs_copy;
166 unsigned long entry_low, entry_high;
167 size_t total_len = 0;
171 /* Calculate total length of data */
172 for (i = 0; i < count; i++)
173 total_len += vecs[i].iov_len;
175 /* Check alignment */
176 if (mtd->writesize > 1) {
178 if (do_div(__to, mtd->writesize) || (total_len % mtd->writesize))
182 /* make a copy of vecs */
183 vecs_copy = kmemdup(vecs, sizeof(struct kvec) * count, GFP_KERNEL);
188 for (i = 0; i < concat->num_subdev; i++) {
189 struct mtd_info *subdev = concat->subdev[i];
190 size_t size, wsize, retsize, old_iov_len;
192 if (to >= subdev->size) {
197 size = min_t(uint64_t, total_len, subdev->size - to);
198 wsize = size; /* store for future use */
200 entry_high = entry_low;
201 while (entry_high < count) {
202 if (size <= vecs_copy[entry_high].iov_len)
204 size -= vecs_copy[entry_high++].iov_len;
207 old_iov_len = vecs_copy[entry_high].iov_len;
208 vecs_copy[entry_high].iov_len = size;
210 err = mtd_writev(subdev, &vecs_copy[entry_low],
211 entry_high - entry_low + 1, to, &retsize);
213 vecs_copy[entry_high].iov_len = old_iov_len - size;
214 vecs_copy[entry_high].iov_base += size;
216 entry_low = entry_high;
237 concat_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
239 struct mtd_concat *concat = CONCAT(mtd);
240 struct mtd_oob_ops devops = *ops;
243 ops->retlen = ops->oobretlen = 0;
245 for (i = 0; i < concat->num_subdev; i++) {
246 struct mtd_info *subdev = concat->subdev[i];
248 if (from >= subdev->size) {
249 from -= subdev->size;
254 if (from + devops.len > subdev->size)
255 devops.len = subdev->size - from;
257 err = mtd_read_oob(subdev, from, &devops);
258 ops->retlen += devops.retlen;
259 ops->oobretlen += devops.oobretlen;
261 /* Save information about bitflips! */
263 if (mtd_is_eccerr(err)) {
264 mtd->ecc_stats.failed++;
266 } else if (mtd_is_bitflip(err)) {
267 mtd->ecc_stats.corrected++;
268 /* Do not overwrite -EBADMSG !! */
276 devops.len = ops->len - ops->retlen;
279 devops.datbuf += devops.retlen;
282 devops.ooblen = ops->ooblen - ops->oobretlen;
285 devops.oobbuf += ops->oobretlen;
294 concat_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
296 struct mtd_concat *concat = CONCAT(mtd);
297 struct mtd_oob_ops devops = *ops;
300 if (!(mtd->flags & MTD_WRITEABLE))
303 ops->retlen = ops->oobretlen = 0;
305 for (i = 0; i < concat->num_subdev; i++) {
306 struct mtd_info *subdev = concat->subdev[i];
308 if (to >= subdev->size) {
313 /* partial write ? */
314 if (to + devops.len > subdev->size)
315 devops.len = subdev->size - to;
317 err = mtd_write_oob(subdev, to, &devops);
318 ops->retlen += devops.oobretlen;
323 devops.len = ops->len - ops->retlen;
326 devops.datbuf += devops.retlen;
329 devops.ooblen = ops->ooblen - ops->oobretlen;
332 devops.oobbuf += devops.oobretlen;
339 static void concat_erase_callback(struct erase_info *instr)
341 /* Nothing to do here in U-Boot */
343 wake_up((wait_queue_head_t *) instr->priv);
347 static int concat_dev_erase(struct mtd_info *mtd, struct erase_info *erase)
350 wait_queue_head_t waitq;
351 DECLARE_WAITQUEUE(wait, current);
354 * This code was stol^H^H^H^Hinspired by mtdchar.c
356 init_waitqueue_head(&waitq);
359 erase->callback = concat_erase_callback;
360 erase->priv = (unsigned long) &waitq;
363 * FIXME: Allow INTERRUPTIBLE. Which means
364 * not having the wait_queue head on the stack.
366 err = mtd_erase(mtd, erase);
368 set_current_state(TASK_UNINTERRUPTIBLE);
369 add_wait_queue(&waitq, &wait);
370 if (erase->state != MTD_ERASE_DONE
371 && erase->state != MTD_ERASE_FAILED)
373 remove_wait_queue(&waitq, &wait);
374 set_current_state(TASK_RUNNING);
376 err = (erase->state == MTD_ERASE_FAILED) ? -EIO : 0;
381 static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
383 struct mtd_concat *concat = CONCAT(mtd);
384 struct mtd_info *subdev;
386 uint64_t length, offset = 0;
387 struct erase_info *erase;
390 * Check for proper erase block alignment of the to-be-erased area.
391 * It is easier to do this based on the super device's erase
392 * region info rather than looking at each particular sub-device
395 if (!concat->mtd.numeraseregions) {
396 /* the easy case: device has uniform erase block size */
397 if (instr->addr & (concat->mtd.erasesize - 1))
399 if (instr->len & (concat->mtd.erasesize - 1))
402 /* device has variable erase size */
403 struct mtd_erase_region_info *erase_regions =
404 concat->mtd.eraseregions;
407 * Find the erase region where the to-be-erased area begins:
409 for (i = 0; i < concat->mtd.numeraseregions &&
410 instr->addr >= erase_regions[i].offset; i++) ;
414 * Now erase_regions[i] is the region in which the
415 * to-be-erased area begins. Verify that the starting
416 * offset is aligned to this region's erase size:
418 if (i < 0 || instr->addr & (erase_regions[i].erasesize - 1))
422 * now find the erase region where the to-be-erased area ends:
424 for (; i < concat->mtd.numeraseregions &&
425 (instr->addr + instr->len) >= erase_regions[i].offset;
429 * check if the ending offset is aligned to this region's erase size
431 if (i < 0 || ((instr->addr + instr->len) &
432 (erase_regions[i].erasesize - 1)))
436 /* make a local copy of instr to avoid modifying the caller's struct */
437 erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
446 * find the subdevice where the to-be-erased area begins, adjust
447 * starting offset to be relative to the subdevice start
449 for (i = 0; i < concat->num_subdev; i++) {
450 subdev = concat->subdev[i];
451 if (subdev->size <= erase->addr) {
452 erase->addr -= subdev->size;
453 offset += subdev->size;
459 /* must never happen since size limit has been verified above */
460 BUG_ON(i >= concat->num_subdev);
462 /* now do the erase: */
464 for (; length > 0; i++) {
465 /* loop for all subdevices affected by this request */
466 subdev = concat->subdev[i]; /* get current subdevice */
468 /* limit length to subdevice's size: */
469 if (erase->addr + length > subdev->size)
470 erase->len = subdev->size - erase->addr;
474 length -= erase->len;
475 if ((err = concat_dev_erase(subdev, erase))) {
476 /* sanity check: should never happen since
477 * block alignment has been checked above */
478 BUG_ON(err == -EINVAL);
479 if (erase->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
480 instr->fail_addr = erase->fail_addr + offset;
484 * erase->addr specifies the offset of the area to be
485 * erased *within the current subdevice*. It can be
486 * non-zero only the first time through this loop, i.e.
487 * for the first subdevice where blocks need to be erased.
488 * All the following erases must begin at the start of the
489 * current subdevice, i.e. at offset zero.
492 offset += subdev->size;
494 instr->state = erase->state;
500 instr->callback(instr);
504 static int concat_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
506 struct mtd_concat *concat = CONCAT(mtd);
507 int i, err = -EINVAL;
509 for (i = 0; i < concat->num_subdev; i++) {
510 struct mtd_info *subdev = concat->subdev[i];
513 if (ofs >= subdev->size) {
518 if (ofs + len > subdev->size)
519 size = subdev->size - ofs;
523 err = mtd_lock(subdev, ofs, size);
538 static int concat_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
540 struct mtd_concat *concat = CONCAT(mtd);
543 for (i = 0; i < concat->num_subdev; i++) {
544 struct mtd_info *subdev = concat->subdev[i];
547 if (ofs >= subdev->size) {
552 if (ofs + len > subdev->size)
553 size = subdev->size - ofs;
557 err = mtd_unlock(subdev, ofs, size);
572 static void concat_sync(struct mtd_info *mtd)
574 struct mtd_concat *concat = CONCAT(mtd);
577 for (i = 0; i < concat->num_subdev; i++) {
578 struct mtd_info *subdev = concat->subdev[i];
584 static int concat_suspend(struct mtd_info *mtd)
586 struct mtd_concat *concat = CONCAT(mtd);
589 for (i = 0; i < concat->num_subdev; i++) {
590 struct mtd_info *subdev = concat->subdev[i];
591 if ((rc = mtd_suspend(subdev)) < 0)
597 static void concat_resume(struct mtd_info *mtd)
599 struct mtd_concat *concat = CONCAT(mtd);
602 for (i = 0; i < concat->num_subdev; i++) {
603 struct mtd_info *subdev = concat->subdev[i];
609 static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
611 struct mtd_concat *concat = CONCAT(mtd);
614 if (!mtd_can_have_bb(concat->subdev[0]))
617 for (i = 0; i < concat->num_subdev; i++) {
618 struct mtd_info *subdev = concat->subdev[i];
620 if (ofs >= subdev->size) {
625 res = mtd_block_isbad(subdev, ofs);
632 static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
634 struct mtd_concat *concat = CONCAT(mtd);
635 int i, err = -EINVAL;
637 for (i = 0; i < concat->num_subdev; i++) {
638 struct mtd_info *subdev = concat->subdev[i];
640 if (ofs >= subdev->size) {
645 err = mtd_block_markbad(subdev, ofs);
647 mtd->ecc_stats.badblocks++;
655 * try to support NOMMU mmaps on concatenated devices
656 * - we don't support subdev spanning as we can't guarantee it'll work
658 static unsigned long concat_get_unmapped_area(struct mtd_info *mtd,
660 unsigned long offset,
663 struct mtd_concat *concat = CONCAT(mtd);
666 for (i = 0; i < concat->num_subdev; i++) {
667 struct mtd_info *subdev = concat->subdev[i];
669 if (offset >= subdev->size) {
670 offset -= subdev->size;
674 return mtd_get_unmapped_area(subdev, len, offset, flags);
677 return (unsigned long) -ENOSYS;
681 * This function constructs a virtual MTD device by concatenating
682 * num_devs MTD devices. A pointer to the new device object is
683 * stored to *new_dev upon success. This function does _not_
684 * register any devices: this is the caller's responsibility.
686 struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */
687 int num_devs, /* number of subdevices */
693 { /* name for the new device */
696 struct mtd_concat *concat;
697 uint32_t max_erasesize, curr_erasesize;
698 int num_erase_region;
699 int max_writebufsize = 0;
701 debug("Concatenating MTD devices:\n");
702 for (i = 0; i < num_devs; i++)
703 printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
704 debug("into device \"%s\"\n", name);
706 /* allocate the device structure */
707 size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
708 concat = kzalloc(size, GFP_KERNEL);
711 ("memory allocation error while creating concatenated device \"%s\"\n",
715 concat->subdev = (struct mtd_info **) (concat + 1);
718 * Set up the new "super" device's MTD object structure, check for
719 * incompatibilities between the subdevices.
721 concat->mtd.type = subdev[0]->type;
722 concat->mtd.flags = subdev[0]->flags;
723 concat->mtd.size = subdev[0]->size;
724 concat->mtd.erasesize = subdev[0]->erasesize;
725 concat->mtd.writesize = subdev[0]->writesize;
727 for (i = 0; i < num_devs; i++)
728 if (max_writebufsize < subdev[i]->writebufsize)
729 max_writebufsize = subdev[i]->writebufsize;
730 concat->mtd.writebufsize = max_writebufsize;
732 concat->mtd.subpage_sft = subdev[0]->subpage_sft;
733 concat->mtd.oobsize = subdev[0]->oobsize;
734 concat->mtd.oobavail = subdev[0]->oobavail;
736 if (subdev[0]->_writev)
737 concat->mtd._writev = concat_writev;
739 if (subdev[0]->_read_oob)
740 concat->mtd._read_oob = concat_read_oob;
741 if (subdev[0]->_write_oob)
742 concat->mtd._write_oob = concat_write_oob;
743 if (subdev[0]->_block_isbad)
744 concat->mtd._block_isbad = concat_block_isbad;
745 if (subdev[0]->_block_markbad)
746 concat->mtd._block_markbad = concat_block_markbad;
748 concat->mtd.ecc_stats.badblocks = subdev[0]->ecc_stats.badblocks;
751 concat->mtd.backing_dev_info = subdev[0]->backing_dev_info;
754 concat->subdev[0] = subdev[0];
756 for (i = 1; i < num_devs; i++) {
757 if (concat->mtd.type != subdev[i]->type) {
759 printk("Incompatible device type on \"%s\"\n",
763 if (concat->mtd.flags != subdev[i]->flags) {
765 * Expect all flags except MTD_WRITEABLE to be
766 * equal on all subdevices.
768 if ((concat->mtd.flags ^ subdev[i]->
769 flags) & ~MTD_WRITEABLE) {
771 printk("Incompatible device flags on \"%s\"\n",
775 /* if writeable attribute differs,
776 make super device writeable */
778 subdev[i]->flags & MTD_WRITEABLE;
782 /* only permit direct mapping if the BDIs are all the same
783 * - copy-mapping is still permitted
785 if (concat->mtd.backing_dev_info !=
786 subdev[i]->backing_dev_info)
787 concat->mtd.backing_dev_info =
788 &default_backing_dev_info;
791 concat->mtd.size += subdev[i]->size;
792 concat->mtd.ecc_stats.badblocks +=
793 subdev[i]->ecc_stats.badblocks;
794 if (concat->mtd.writesize != subdev[i]->writesize ||
795 concat->mtd.subpage_sft != subdev[i]->subpage_sft ||
796 concat->mtd.oobsize != subdev[i]->oobsize ||
797 !concat->mtd._read_oob != !subdev[i]->_read_oob ||
798 !concat->mtd._write_oob != !subdev[i]->_write_oob) {
800 printk("Incompatible OOB or ECC data on \"%s\"\n",
804 concat->subdev[i] = subdev[i];
808 concat->mtd.ecclayout = subdev[0]->ecclayout;
810 concat->num_subdev = num_devs;
811 concat->mtd.name = name;
813 concat->mtd._erase = concat_erase;
814 concat->mtd._read = concat_read;
815 concat->mtd._write = concat_write;
816 concat->mtd._sync = concat_sync;
817 concat->mtd._lock = concat_lock;
818 concat->mtd._unlock = concat_unlock;
820 concat->mtd._suspend = concat_suspend;
821 concat->mtd._resume = concat_resume;
823 concat->mtd._get_unmapped_area = concat_get_unmapped_area;
826 * Combine the erase block size info of the subdevices:
828 * first, walk the map of the new device and see how
829 * many changes in erase size we have
831 max_erasesize = curr_erasesize = subdev[0]->erasesize;
832 num_erase_region = 1;
833 for (i = 0; i < num_devs; i++) {
834 if (subdev[i]->numeraseregions == 0) {
835 /* current subdevice has uniform erase size */
836 if (subdev[i]->erasesize != curr_erasesize) {
837 /* if it differs from the last subdevice's erase size, count it */
839 curr_erasesize = subdev[i]->erasesize;
840 if (curr_erasesize > max_erasesize)
841 max_erasesize = curr_erasesize;
844 /* current subdevice has variable erase size */
846 for (j = 0; j < subdev[i]->numeraseregions; j++) {
848 /* walk the list of erase regions, count any changes */
849 if (subdev[i]->eraseregions[j].erasesize !=
853 subdev[i]->eraseregions[j].
855 if (curr_erasesize > max_erasesize)
856 max_erasesize = curr_erasesize;
862 if (num_erase_region == 1) {
864 * All subdevices have the same uniform erase size.
867 concat->mtd.erasesize = curr_erasesize;
868 concat->mtd.numeraseregions = 0;
873 * erase block size varies across the subdevices: allocate
874 * space to store the data describing the variable erase regions
876 struct mtd_erase_region_info *erase_region_p;
877 uint64_t begin, position;
879 concat->mtd.erasesize = max_erasesize;
880 concat->mtd.numeraseregions = num_erase_region;
881 concat->mtd.eraseregions = erase_region_p =
882 kmalloc(num_erase_region *
883 sizeof (struct mtd_erase_region_info), GFP_KERNEL);
884 if (!erase_region_p) {
887 ("memory allocation error while creating erase region list"
888 " for device \"%s\"\n", name);
893 * walk the map of the new device once more and fill in
894 * in erase region info:
896 curr_erasesize = subdev[0]->erasesize;
897 begin = position = 0;
898 for (i = 0; i < num_devs; i++) {
899 if (subdev[i]->numeraseregions == 0) {
900 /* current subdevice has uniform erase size */
901 if (subdev[i]->erasesize != curr_erasesize) {
903 * fill in an mtd_erase_region_info structure for the area
904 * we have walked so far:
906 erase_region_p->offset = begin;
907 erase_region_p->erasesize =
909 tmp64 = position - begin;
910 do_div(tmp64, curr_erasesize);
911 erase_region_p->numblocks = tmp64;
914 curr_erasesize = subdev[i]->erasesize;
917 position += subdev[i]->size;
919 /* current subdevice has variable erase size */
921 for (j = 0; j < subdev[i]->numeraseregions; j++) {
922 /* walk the list of erase regions, count any changes */
923 if (subdev[i]->eraseregions[j].
924 erasesize != curr_erasesize) {
925 erase_region_p->offset = begin;
926 erase_region_p->erasesize =
928 tmp64 = position - begin;
929 do_div(tmp64, curr_erasesize);
930 erase_region_p->numblocks = tmp64;
934 subdev[i]->eraseregions[j].
939 subdev[i]->eraseregions[j].
940 numblocks * (uint64_t)curr_erasesize;
944 /* Now write the final entry */
945 erase_region_p->offset = begin;
946 erase_region_p->erasesize = curr_erasesize;
947 tmp64 = position - begin;
948 do_div(tmp64, curr_erasesize);
949 erase_region_p->numblocks = tmp64;
956 * This function destroys an MTD object obtained from concat_mtd_devs()
959 void mtd_concat_destroy(struct mtd_info *mtd)
961 struct mtd_concat *concat = CONCAT(mtd);
962 if (concat->mtd.numeraseregions)
963 kfree(concat->mtd.eraseregions);
967 EXPORT_SYMBOL(mtd_concat_create);
968 EXPORT_SYMBOL(mtd_concat_destroy);
970 MODULE_LICENSE("GPL");
971 MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
972 MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");