2 * MTD device concatenation layer
4 * (C) 2002 Robert Kaiser <rkaiser@sysgo.de>
6 * NAND support by Christian Gan <cgan@iders.ca>
10 * $Id: mtdconcat.c,v 1.11 2005/11/07 11:14:20 gleixner Exp $
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>
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/concat.h>
22 #include <asm/div64.h>
25 * Our storage structure:
26 * Subdev points to an array of pointers to struct mtd_info objects
27 * which is allocated along with this structure
33 struct mtd_info **subdev;
37 * how to calculate the size required for the above structure,
38 * including the pointer array subdev points to:
40 #define SIZEOF_STRUCT_MTD_CONCAT(num_subdev) \
41 ((sizeof(struct mtd_concat) + (num_subdev) * sizeof(struct mtd_info *)))
44 * Given a pointer to the MTD object in the mtd_concat structure,
45 * we can retrieve the pointer to that structure with this macro.
47 #define CONCAT(x) ((struct mtd_concat *)(x))
50 * MTD methods which look up the relevant subdevice, translate the
51 * effective address and pass through to the subdevice.
55 concat_read(struct mtd_info *mtd, loff_t from, size_t len,
56 size_t * retlen, u_char * buf)
58 struct mtd_concat *concat = CONCAT(mtd);
64 for (i = 0; i < concat->num_subdev; i++) {
65 struct mtd_info *subdev = concat->subdev[i];
68 if (from >= subdev->size) {
69 /* Not destined for this subdev */
74 if (from + len > subdev->size)
75 /* First part goes into this subdev */
76 size = subdev->size - from;
78 /* Entire transaction goes into this subdev */
81 err = subdev->read(subdev, from, size, &retsize, buf);
83 /* Save information about bitflips! */
85 if (err == -EBADMSG) {
86 mtd->ecc_stats.failed++;
88 } else if (err == -EUCLEAN) {
89 mtd->ecc_stats.corrected++;
90 /* Do not overwrite -EBADMSG !! */
109 concat_write(struct mtd_info *mtd, loff_t to, size_t len,
110 size_t * retlen, const u_char * buf)
112 struct mtd_concat *concat = CONCAT(mtd);
116 if (!(mtd->flags & MTD_WRITEABLE))
121 for (i = 0; i < concat->num_subdev; i++) {
122 struct mtd_info *subdev = concat->subdev[i];
123 size_t size, retsize;
125 if (to >= subdev->size) {
130 if (to + len > subdev->size)
131 size = subdev->size - to;
135 if (!(subdev->flags & MTD_WRITEABLE))
138 err = subdev->write(subdev, to, size, &retsize, buf);
156 concat_writev(struct mtd_info *mtd, const struct kvec *vecs,
157 unsigned long count, loff_t to, size_t * retlen)
159 struct mtd_concat *concat = CONCAT(mtd);
160 struct kvec *vecs_copy;
161 unsigned long entry_low, entry_high;
162 size_t total_len = 0;
166 if (!(mtd->flags & MTD_WRITEABLE))
171 /* Calculate total length of data */
172 for (i = 0; i < count; i++)
173 total_len += vecs[i].iov_len;
175 /* Do not allow write past end of device */
176 if ((to + total_len) > mtd->size)
179 /* Check alignment */
180 if (mtd->writesize > 1) {
182 if (do_div(__to, mtd->writesize) || (total_len % mtd->writesize))
186 /* make a copy of vecs */
187 vecs_copy = kmalloc(sizeof(struct kvec) * count, GFP_KERNEL);
190 memcpy(vecs_copy, vecs, sizeof(struct kvec) * count);
193 for (i = 0; i < concat->num_subdev; i++) {
194 struct mtd_info *subdev = concat->subdev[i];
195 size_t size, wsize, retsize, old_iov_len;
197 if (to >= subdev->size) {
202 size = min(total_len, (size_t)(subdev->size - to));
203 wsize = size; /* store for future use */
205 entry_high = entry_low;
206 while (entry_high < count) {
207 if (size <= vecs_copy[entry_high].iov_len)
209 size -= vecs_copy[entry_high++].iov_len;
212 old_iov_len = vecs_copy[entry_high].iov_len;
213 vecs_copy[entry_high].iov_len = size;
215 if (!(subdev->flags & MTD_WRITEABLE))
218 err = subdev->writev(subdev, &vecs_copy[entry_low],
219 entry_high - entry_low + 1, to, &retsize);
221 vecs_copy[entry_high].iov_len = old_iov_len - size;
222 vecs_copy[entry_high].iov_base += size;
224 entry_low = entry_high;
244 concat_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
246 struct mtd_concat *concat = CONCAT(mtd);
247 struct mtd_oob_ops devops = *ops;
252 for (i = 0; i < concat->num_subdev; i++) {
253 struct mtd_info *subdev = concat->subdev[i];
255 if (from >= subdev->size) {
256 from -= subdev->size;
261 if (from + devops.len > subdev->size)
262 devops.len = subdev->size - from;
264 err = subdev->read_oob(subdev, from, &devops);
265 ops->retlen += devops.retlen;
267 /* Save information about bitflips! */
269 if (err == -EBADMSG) {
270 mtd->ecc_stats.failed++;
272 } else if (err == -EUCLEAN) {
273 mtd->ecc_stats.corrected++;
274 /* Do not overwrite -EBADMSG !! */
281 devops.len = ops->len - ops->retlen;
286 devops.datbuf += devops.retlen;
288 devops.oobbuf += devops.ooblen;
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))
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 = subdev->write_oob(subdev, to, &devops);
320 ops->retlen += devops.retlen;
324 devops.len = ops->len - ops->retlen;
329 devops.datbuf += devops.retlen;
331 devops.oobbuf += devops.ooblen;
337 static void concat_erase_callback(struct erase_info *instr)
339 wake_up((wait_queue_head_t *) instr->priv);
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->callback = concat_erase_callback;
355 erase->priv = (unsigned long) &waitq;
358 * FIXME: Allow INTERRUPTIBLE. Which means
359 * not having the wait_queue head on the stack.
361 err = mtd->erase(mtd, erase);
363 set_current_state(TASK_UNINTERRUPTIBLE);
364 add_wait_queue(&waitq, &wait);
365 if (erase->state != MTD_ERASE_DONE
366 && erase->state != MTD_ERASE_FAILED)
368 remove_wait_queue(&waitq, &wait);
369 set_current_state(TASK_RUNNING);
371 err = (erase->state == MTD_ERASE_FAILED) ? -EIO : 0;
376 static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
378 struct mtd_concat *concat = CONCAT(mtd);
379 struct mtd_info *subdev;
381 u_int32_t length, offset = 0;
382 struct erase_info *erase;
384 if (!(mtd->flags & MTD_WRITEABLE))
387 if (instr->addr > concat->mtd.size)
390 if (instr->len + instr->addr > concat->mtd.size)
394 * Check for proper erase block alignment of the to-be-erased area.
395 * It is easier to do this based on the super device's erase
396 * region info rather than looking at each particular sub-device
399 if (!concat->mtd.numeraseregions) {
400 /* the easy case: device has uniform erase block size */
401 if (instr->addr & (concat->mtd.erasesize - 1))
403 if (instr->len & (concat->mtd.erasesize - 1))
406 /* device has variable erase size */
407 struct mtd_erase_region_info *erase_regions =
408 concat->mtd.eraseregions;
411 * Find the erase region where the to-be-erased area begins:
413 for (i = 0; i < concat->mtd.numeraseregions &&
414 instr->addr >= erase_regions[i].offset; i++) ;
418 * Now erase_regions[i] is the region in which the
419 * to-be-erased area begins. Verify that the starting
420 * offset is aligned to this region's erase size:
422 if (instr->addr & (erase_regions[i].erasesize - 1))
426 * now find the erase region where the to-be-erased area ends:
428 for (; i < concat->mtd.numeraseregions &&
429 (instr->addr + instr->len) >= erase_regions[i].offset;
433 * check if the ending offset is aligned to this region's erase size
435 if ((instr->addr + instr->len) & (erase_regions[i].erasesize -
440 instr->fail_addr = 0xffffffff;
442 /* make a local copy of instr to avoid modifying the caller's struct */
443 erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
452 * find the subdevice where the to-be-erased area begins, adjust
453 * starting offset to be relative to the subdevice start
455 for (i = 0; i < concat->num_subdev; i++) {
456 subdev = concat->subdev[i];
457 if (subdev->size <= erase->addr) {
458 erase->addr -= subdev->size;
459 offset += subdev->size;
465 /* must never happen since size limit has been verified above */
466 BUG_ON(i >= concat->num_subdev);
468 /* now do the erase: */
470 for (; length > 0; i++) {
471 /* loop for all subdevices affected by this request */
472 subdev = concat->subdev[i]; /* get current subdevice */
474 /* limit length to subdevice's size: */
475 if (erase->addr + length > subdev->size)
476 erase->len = subdev->size - erase->addr;
480 if (!(subdev->flags & MTD_WRITEABLE)) {
484 length -= erase->len;
485 if ((err = concat_dev_erase(subdev, erase))) {
486 /* sanity check: should never happen since
487 * block alignment has been checked above */
488 BUG_ON(err == -EINVAL);
489 if (erase->fail_addr != 0xffffffff)
490 instr->fail_addr = erase->fail_addr + offset;
494 * erase->addr specifies the offset of the area to be
495 * erased *within the current subdevice*. It can be
496 * non-zero only the first time through this loop, i.e.
497 * for the first subdevice where blocks need to be erased.
498 * All the following erases must begin at the start of the
499 * current subdevice, i.e. at offset zero.
502 offset += subdev->size;
504 instr->state = erase->state;
510 instr->callback(instr);
514 static int concat_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
516 struct mtd_concat *concat = CONCAT(mtd);
517 int i, err = -EINVAL;
519 if ((len + ofs) > mtd->size)
522 for (i = 0; i < concat->num_subdev; i++) {
523 struct mtd_info *subdev = concat->subdev[i];
526 if (ofs >= subdev->size) {
531 if (ofs + len > subdev->size)
532 size = subdev->size - ofs;
536 err = subdev->lock(subdev, ofs, size);
552 static int concat_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
554 struct mtd_concat *concat = CONCAT(mtd);
557 if ((len + ofs) > mtd->size)
560 for (i = 0; i < concat->num_subdev; i++) {
561 struct mtd_info *subdev = concat->subdev[i];
564 if (ofs >= subdev->size) {
569 if (ofs + len > subdev->size)
570 size = subdev->size - ofs;
574 err = subdev->unlock(subdev, ofs, size);
590 static void concat_sync(struct mtd_info *mtd)
592 struct mtd_concat *concat = CONCAT(mtd);
595 for (i = 0; i < concat->num_subdev; i++) {
596 struct mtd_info *subdev = concat->subdev[i];
597 subdev->sync(subdev);
601 static int concat_suspend(struct mtd_info *mtd)
603 struct mtd_concat *concat = CONCAT(mtd);
606 for (i = 0; i < concat->num_subdev; i++) {
607 struct mtd_info *subdev = concat->subdev[i];
608 if ((rc = subdev->suspend(subdev)) < 0)
614 static void concat_resume(struct mtd_info *mtd)
616 struct mtd_concat *concat = CONCAT(mtd);
619 for (i = 0; i < concat->num_subdev; i++) {
620 struct mtd_info *subdev = concat->subdev[i];
621 subdev->resume(subdev);
625 static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
627 struct mtd_concat *concat = CONCAT(mtd);
630 if (!concat->subdev[0]->block_isbad)
636 for (i = 0; i < concat->num_subdev; i++) {
637 struct mtd_info *subdev = concat->subdev[i];
639 if (ofs >= subdev->size) {
644 res = subdev->block_isbad(subdev, ofs);
651 static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
653 struct mtd_concat *concat = CONCAT(mtd);
654 int i, err = -EINVAL;
656 if (!concat->subdev[0]->block_markbad)
662 for (i = 0; i < concat->num_subdev; i++) {
663 struct mtd_info *subdev = concat->subdev[i];
665 if (ofs >= subdev->size) {
670 err = subdev->block_markbad(subdev, ofs);
672 mtd->ecc_stats.badblocks++;
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 */
688 { /* name for the new device */
691 struct mtd_concat *concat;
692 u_int32_t max_erasesize, curr_erasesize;
693 int num_erase_region;
695 printk(KERN_NOTICE "Concatenating MTD devices:\n");
696 for (i = 0; i < num_devs; i++)
697 printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
698 printk(KERN_NOTICE "into device \"%s\"\n", name);
700 /* allocate the device structure */
701 size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
702 concat = kmalloc(size, GFP_KERNEL);
705 ("memory allocation error while creating concatenated device \"%s\"\n",
709 memset(concat, 0, size);
710 concat->subdev = (struct mtd_info **) (concat + 1);
713 * Set up the new "super" device's MTD object structure, check for
714 * incompatibilites between the subdevices.
716 concat->mtd.type = subdev[0]->type;
717 concat->mtd.flags = subdev[0]->flags;
718 concat->mtd.size = subdev[0]->size;
719 concat->mtd.erasesize = subdev[0]->erasesize;
720 concat->mtd.writesize = subdev[0]->writesize;
721 concat->mtd.oobsize = subdev[0]->oobsize;
722 concat->mtd.ecctype = subdev[0]->ecctype;
723 concat->mtd.eccsize = subdev[0]->eccsize;
724 if (subdev[0]->writev)
725 concat->mtd.writev = concat_writev;
726 if (subdev[0]->read_oob)
727 concat->mtd.read_oob = concat_read_oob;
728 if (subdev[0]->write_oob)
729 concat->mtd.write_oob = concat_write_oob;
730 if (subdev[0]->block_isbad)
731 concat->mtd.block_isbad = concat_block_isbad;
732 if (subdev[0]->block_markbad)
733 concat->mtd.block_markbad = concat_block_markbad;
735 concat->mtd.ecc_stats.badblocks = subdev[0]->ecc_stats.badblocks;
737 concat->subdev[0] = subdev[0];
739 for (i = 1; i < num_devs; i++) {
740 if (concat->mtd.type != subdev[i]->type) {
742 printk("Incompatible device type on \"%s\"\n",
746 if (concat->mtd.flags != subdev[i]->flags) {
748 * Expect all flags except MTD_WRITEABLE to be
749 * equal on all subdevices.
751 if ((concat->mtd.flags ^ subdev[i]->
752 flags) & ~MTD_WRITEABLE) {
754 printk("Incompatible device flags on \"%s\"\n",
758 /* if writeable attribute differs,
759 make super device writeable */
761 subdev[i]->flags & MTD_WRITEABLE;
763 concat->mtd.size += subdev[i]->size;
764 concat->mtd.ecc_stats.badblocks +=
765 subdev[i]->ecc_stats.badblocks;
766 if (concat->mtd.writesize != subdev[i]->writesize ||
767 concat->mtd.oobsize != subdev[i]->oobsize ||
768 concat->mtd.ecctype != subdev[i]->ecctype ||
769 concat->mtd.eccsize != subdev[i]->eccsize ||
770 !concat->mtd.read_oob != !subdev[i]->read_oob ||
771 !concat->mtd.write_oob != !subdev[i]->write_oob) {
773 printk("Incompatible OOB or ECC data on \"%s\"\n",
777 concat->subdev[i] = subdev[i];
781 concat->mtd.ecclayout = subdev[0]->ecclayout;
783 concat->num_subdev = num_devs;
784 concat->mtd.name = name;
786 concat->mtd.erase = concat_erase;
787 concat->mtd.read = concat_read;
788 concat->mtd.write = concat_write;
789 concat->mtd.sync = concat_sync;
790 concat->mtd.lock = concat_lock;
791 concat->mtd.unlock = concat_unlock;
792 concat->mtd.suspend = concat_suspend;
793 concat->mtd.resume = concat_resume;
796 * Combine the erase block size info of the subdevices:
798 * first, walk the map of the new device and see how
799 * many changes in erase size we have
801 max_erasesize = curr_erasesize = subdev[0]->erasesize;
802 num_erase_region = 1;
803 for (i = 0; i < num_devs; i++) {
804 if (subdev[i]->numeraseregions == 0) {
805 /* current subdevice has uniform erase size */
806 if (subdev[i]->erasesize != curr_erasesize) {
807 /* if it differs from the last subdevice's erase size, count it */
809 curr_erasesize = subdev[i]->erasesize;
810 if (curr_erasesize > max_erasesize)
811 max_erasesize = curr_erasesize;
814 /* current subdevice has variable erase size */
816 for (j = 0; j < subdev[i]->numeraseregions; j++) {
818 /* walk the list of erase regions, count any changes */
819 if (subdev[i]->eraseregions[j].erasesize !=
823 subdev[i]->eraseregions[j].
825 if (curr_erasesize > max_erasesize)
826 max_erasesize = curr_erasesize;
832 if (num_erase_region == 1) {
834 * All subdevices have the same uniform erase size.
837 concat->mtd.erasesize = curr_erasesize;
838 concat->mtd.numeraseregions = 0;
841 * erase block size varies across the subdevices: allocate
842 * space to store the data describing the variable erase regions
844 struct mtd_erase_region_info *erase_region_p;
845 u_int32_t begin, position;
847 concat->mtd.erasesize = max_erasesize;
848 concat->mtd.numeraseregions = num_erase_region;
849 concat->mtd.eraseregions = erase_region_p =
850 kmalloc(num_erase_region *
851 sizeof (struct mtd_erase_region_info), GFP_KERNEL);
852 if (!erase_region_p) {
855 ("memory allocation error while creating erase region list"
856 " for device \"%s\"\n", name);
861 * walk the map of the new device once more and fill in
862 * in erase region info:
864 curr_erasesize = subdev[0]->erasesize;
865 begin = position = 0;
866 for (i = 0; i < num_devs; i++) {
867 if (subdev[i]->numeraseregions == 0) {
868 /* current subdevice has uniform erase size */
869 if (subdev[i]->erasesize != curr_erasesize) {
871 * fill in an mtd_erase_region_info structure for the area
872 * we have walked so far:
874 erase_region_p->offset = begin;
875 erase_region_p->erasesize =
877 erase_region_p->numblocks =
878 (position - begin) / curr_erasesize;
881 curr_erasesize = subdev[i]->erasesize;
884 position += subdev[i]->size;
886 /* current subdevice has variable erase size */
888 for (j = 0; j < subdev[i]->numeraseregions; j++) {
889 /* walk the list of erase regions, count any changes */
890 if (subdev[i]->eraseregions[j].
891 erasesize != curr_erasesize) {
892 erase_region_p->offset = begin;
893 erase_region_p->erasesize =
895 erase_region_p->numblocks =
897 begin) / curr_erasesize;
901 subdev[i]->eraseregions[j].
906 subdev[i]->eraseregions[j].
907 numblocks * curr_erasesize;
911 /* Now write the final entry */
912 erase_region_p->offset = begin;
913 erase_region_p->erasesize = curr_erasesize;
914 erase_region_p->numblocks = (position - begin) / curr_erasesize;
921 * This function destroys an MTD object obtained from concat_mtd_devs()
924 void mtd_concat_destroy(struct mtd_info *mtd)
926 struct mtd_concat *concat = CONCAT(mtd);
927 if (concat->mtd.numeraseregions)
928 kfree(concat->mtd.eraseregions);
932 EXPORT_SYMBOL(mtd_concat_create);
933 EXPORT_SYMBOL(mtd_concat_destroy);
935 MODULE_LICENSE("GPL");
936 MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
937 MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");