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);
99 concat_write(struct mtd_info *mtd, loff_t to, size_t len,
100 size_t * retlen, const u_char * buf)
102 struct mtd_concat *concat = CONCAT(mtd);
106 if (!(mtd->flags & MTD_WRITEABLE))
111 for (i = 0; i < concat->num_subdev; i++) {
112 struct mtd_info *subdev = concat->subdev[i];
113 size_t size, retsize;
115 if (to >= subdev->size) {
120 if (to + len > subdev->size)
121 size = subdev->size - to;
125 if (!(subdev->flags & MTD_WRITEABLE))
128 err = subdev->write(subdev, to, size, &retsize, buf);
146 concat_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
147 size_t * retlen, u_char * buf, u_char * eccbuf,
148 struct nand_oobinfo *oobsel)
150 struct mtd_concat *concat = CONCAT(mtd);
156 for (i = 0; i < concat->num_subdev; i++) {
157 struct mtd_info *subdev = concat->subdev[i];
158 size_t size, retsize;
160 if (from >= subdev->size) {
161 /* Not destined for this subdev */
163 from -= subdev->size;
167 if (from + len > subdev->size)
168 /* First part goes into this subdev */
169 size = subdev->size - from;
171 /* Entire transaction goes into this subdev */
174 if (subdev->read_ecc)
175 err = subdev->read_ecc(subdev, from, size,
176 &retsize, buf, eccbuf, oobsel);
191 eccbuf += subdev->oobsize;
192 /* in nand.c at least, eccbufs are
193 tagged with 2 (int)eccstatus'; we
194 must account for these */
195 eccbuf += 2 * (sizeof (int));
203 concat_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
204 size_t * retlen, const u_char * buf, u_char * eccbuf,
205 struct nand_oobinfo *oobsel)
207 struct mtd_concat *concat = CONCAT(mtd);
211 if (!(mtd->flags & MTD_WRITEABLE))
216 for (i = 0; i < concat->num_subdev; i++) {
217 struct mtd_info *subdev = concat->subdev[i];
218 size_t size, retsize;
220 if (to >= subdev->size) {
225 if (to + len > subdev->size)
226 size = subdev->size - to;
230 if (!(subdev->flags & MTD_WRITEABLE))
232 else if (subdev->write_ecc)
233 err = subdev->write_ecc(subdev, to, size,
234 &retsize, buf, eccbuf, oobsel);
249 eccbuf += subdev->oobsize;
256 concat_writev(struct mtd_info *mtd, const struct kvec *vecs,
257 unsigned long count, loff_t to, size_t * retlen)
259 struct mtd_concat *concat = CONCAT(mtd);
260 struct kvec *vecs_copy;
261 unsigned long entry_low, entry_high;
262 size_t total_len = 0;
266 if (!(mtd->flags & MTD_WRITEABLE))
271 /* Calculate total length of data */
272 for (i = 0; i < count; i++)
273 total_len += vecs[i].iov_len;
275 /* Do not allow write past end of device */
276 if ((to + total_len) > mtd->size)
279 /* Check alignment */
280 if (mtd->writesize > 1) {
282 if (do_div(__to, mtd->writesize) || (total_len % mtd->writesize))
286 /* make a copy of vecs */
287 vecs_copy = kmalloc(sizeof(struct kvec) * count, GFP_KERNEL);
290 memcpy(vecs_copy, vecs, sizeof(struct kvec) * count);
293 for (i = 0; i < concat->num_subdev; i++) {
294 struct mtd_info *subdev = concat->subdev[i];
295 size_t size, wsize, retsize, old_iov_len;
297 if (to >= subdev->size) {
302 size = min(total_len, (size_t)(subdev->size - to));
303 wsize = size; /* store for future use */
305 entry_high = entry_low;
306 while (entry_high < count) {
307 if (size <= vecs_copy[entry_high].iov_len)
309 size -= vecs_copy[entry_high++].iov_len;
312 old_iov_len = vecs_copy[entry_high].iov_len;
313 vecs_copy[entry_high].iov_len = size;
315 if (!(subdev->flags & MTD_WRITEABLE))
318 err = subdev->writev(subdev, &vecs_copy[entry_low],
319 entry_high - entry_low + 1, to, &retsize);
321 vecs_copy[entry_high].iov_len = old_iov_len - size;
322 vecs_copy[entry_high].iov_base += size;
324 entry_low = entry_high;
344 concat_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
345 size_t * retlen, u_char * buf)
347 struct mtd_concat *concat = CONCAT(mtd);
353 for (i = 0; i < concat->num_subdev; i++) {
354 struct mtd_info *subdev = concat->subdev[i];
355 size_t size, retsize;
357 if (from >= subdev->size) {
358 /* Not destined for this subdev */
360 from -= subdev->size;
363 if (from + len > subdev->size)
364 /* First part goes into this subdev */
365 size = subdev->size - from;
367 /* Entire transaction goes into this subdev */
370 if (subdev->read_oob)
371 err = subdev->read_oob(subdev, from, size,
392 concat_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
393 size_t * retlen, const u_char * buf)
395 struct mtd_concat *concat = CONCAT(mtd);
399 if (!(mtd->flags & MTD_WRITEABLE))
404 for (i = 0; i < concat->num_subdev; i++) {
405 struct mtd_info *subdev = concat->subdev[i];
406 size_t size, retsize;
408 if (to >= subdev->size) {
413 if (to + len > subdev->size)
414 size = subdev->size - to;
418 if (!(subdev->flags & MTD_WRITEABLE))
420 else if (subdev->write_oob)
421 err = subdev->write_oob(subdev, to, size, &retsize,
441 static void concat_erase_callback(struct erase_info *instr)
443 wake_up((wait_queue_head_t *) instr->priv);
446 static int concat_dev_erase(struct mtd_info *mtd, struct erase_info *erase)
449 wait_queue_head_t waitq;
450 DECLARE_WAITQUEUE(wait, current);
453 * This code was stol^H^H^H^Hinspired by mtdchar.c
455 init_waitqueue_head(&waitq);
458 erase->callback = concat_erase_callback;
459 erase->priv = (unsigned long) &waitq;
462 * FIXME: Allow INTERRUPTIBLE. Which means
463 * not having the wait_queue head on the stack.
465 err = mtd->erase(mtd, erase);
467 set_current_state(TASK_UNINTERRUPTIBLE);
468 add_wait_queue(&waitq, &wait);
469 if (erase->state != MTD_ERASE_DONE
470 && erase->state != MTD_ERASE_FAILED)
472 remove_wait_queue(&waitq, &wait);
473 set_current_state(TASK_RUNNING);
475 err = (erase->state == MTD_ERASE_FAILED) ? -EIO : 0;
480 static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
482 struct mtd_concat *concat = CONCAT(mtd);
483 struct mtd_info *subdev;
485 u_int32_t length, offset = 0;
486 struct erase_info *erase;
488 if (!(mtd->flags & MTD_WRITEABLE))
491 if (instr->addr > concat->mtd.size)
494 if (instr->len + instr->addr > concat->mtd.size)
498 * Check for proper erase block alignment of the to-be-erased area.
499 * It is easier to do this based on the super device's erase
500 * region info rather than looking at each particular sub-device
503 if (!concat->mtd.numeraseregions) {
504 /* the easy case: device has uniform erase block size */
505 if (instr->addr & (concat->mtd.erasesize - 1))
507 if (instr->len & (concat->mtd.erasesize - 1))
510 /* device has variable erase size */
511 struct mtd_erase_region_info *erase_regions =
512 concat->mtd.eraseregions;
515 * Find the erase region where the to-be-erased area begins:
517 for (i = 0; i < concat->mtd.numeraseregions &&
518 instr->addr >= erase_regions[i].offset; i++) ;
522 * Now erase_regions[i] is the region in which the
523 * to-be-erased area begins. Verify that the starting
524 * offset is aligned to this region's erase size:
526 if (instr->addr & (erase_regions[i].erasesize - 1))
530 * now find the erase region where the to-be-erased area ends:
532 for (; i < concat->mtd.numeraseregions &&
533 (instr->addr + instr->len) >= erase_regions[i].offset;
537 * check if the ending offset is aligned to this region's erase size
539 if ((instr->addr + instr->len) & (erase_regions[i].erasesize -
544 instr->fail_addr = 0xffffffff;
546 /* make a local copy of instr to avoid modifying the caller's struct */
547 erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
556 * find the subdevice where the to-be-erased area begins, adjust
557 * starting offset to be relative to the subdevice start
559 for (i = 0; i < concat->num_subdev; i++) {
560 subdev = concat->subdev[i];
561 if (subdev->size <= erase->addr) {
562 erase->addr -= subdev->size;
563 offset += subdev->size;
569 /* must never happen since size limit has been verified above */
570 BUG_ON(i >= concat->num_subdev);
572 /* now do the erase: */
574 for (; length > 0; i++) {
575 /* loop for all subdevices affected by this request */
576 subdev = concat->subdev[i]; /* get current subdevice */
578 /* limit length to subdevice's size: */
579 if (erase->addr + length > subdev->size)
580 erase->len = subdev->size - erase->addr;
584 if (!(subdev->flags & MTD_WRITEABLE)) {
588 length -= erase->len;
589 if ((err = concat_dev_erase(subdev, erase))) {
590 /* sanity check: should never happen since
591 * block alignment has been checked above */
592 BUG_ON(err == -EINVAL);
593 if (erase->fail_addr != 0xffffffff)
594 instr->fail_addr = erase->fail_addr + offset;
598 * erase->addr specifies the offset of the area to be
599 * erased *within the current subdevice*. It can be
600 * non-zero only the first time through this loop, i.e.
601 * for the first subdevice where blocks need to be erased.
602 * All the following erases must begin at the start of the
603 * current subdevice, i.e. at offset zero.
606 offset += subdev->size;
608 instr->state = erase->state;
614 instr->callback(instr);
618 static int concat_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
620 struct mtd_concat *concat = CONCAT(mtd);
621 int i, err = -EINVAL;
623 if ((len + ofs) > mtd->size)
626 for (i = 0; i < concat->num_subdev; i++) {
627 struct mtd_info *subdev = concat->subdev[i];
630 if (ofs >= subdev->size) {
635 if (ofs + len > subdev->size)
636 size = subdev->size - ofs;
640 err = subdev->lock(subdev, ofs, size);
656 static int concat_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
658 struct mtd_concat *concat = CONCAT(mtd);
661 if ((len + ofs) > mtd->size)
664 for (i = 0; i < concat->num_subdev; i++) {
665 struct mtd_info *subdev = concat->subdev[i];
668 if (ofs >= subdev->size) {
673 if (ofs + len > subdev->size)
674 size = subdev->size - ofs;
678 err = subdev->unlock(subdev, ofs, size);
694 static void concat_sync(struct mtd_info *mtd)
696 struct mtd_concat *concat = CONCAT(mtd);
699 for (i = 0; i < concat->num_subdev; i++) {
700 struct mtd_info *subdev = concat->subdev[i];
701 subdev->sync(subdev);
705 static int concat_suspend(struct mtd_info *mtd)
707 struct mtd_concat *concat = CONCAT(mtd);
710 for (i = 0; i < concat->num_subdev; i++) {
711 struct mtd_info *subdev = concat->subdev[i];
712 if ((rc = subdev->suspend(subdev)) < 0)
718 static void concat_resume(struct mtd_info *mtd)
720 struct mtd_concat *concat = CONCAT(mtd);
723 for (i = 0; i < concat->num_subdev; i++) {
724 struct mtd_info *subdev = concat->subdev[i];
725 subdev->resume(subdev);
729 static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
731 struct mtd_concat *concat = CONCAT(mtd);
734 if (!concat->subdev[0]->block_isbad)
740 for (i = 0; i < concat->num_subdev; i++) {
741 struct mtd_info *subdev = concat->subdev[i];
743 if (ofs >= subdev->size) {
748 res = subdev->block_isbad(subdev, ofs);
755 static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
757 struct mtd_concat *concat = CONCAT(mtd);
758 int i, err = -EINVAL;
760 if (!concat->subdev[0]->block_markbad)
766 for (i = 0; i < concat->num_subdev; i++) {
767 struct mtd_info *subdev = concat->subdev[i];
769 if (ofs >= subdev->size) {
774 err = subdev->block_markbad(subdev, ofs);
782 * This function constructs a virtual MTD device by concatenating
783 * num_devs MTD devices. A pointer to the new device object is
784 * stored to *new_dev upon success. This function does _not_
785 * register any devices: this is the caller's responsibility.
787 struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */
788 int num_devs, /* number of subdevices */
790 { /* name for the new device */
793 struct mtd_concat *concat;
794 u_int32_t max_erasesize, curr_erasesize;
795 int num_erase_region;
797 printk(KERN_NOTICE "Concatenating MTD devices:\n");
798 for (i = 0; i < num_devs; i++)
799 printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
800 printk(KERN_NOTICE "into device \"%s\"\n", name);
802 /* allocate the device structure */
803 size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
804 concat = kmalloc(size, GFP_KERNEL);
807 ("memory allocation error while creating concatenated device \"%s\"\n",
811 memset(concat, 0, size);
812 concat->subdev = (struct mtd_info **) (concat + 1);
815 * Set up the new "super" device's MTD object structure, check for
816 * incompatibilites between the subdevices.
818 concat->mtd.type = subdev[0]->type;
819 concat->mtd.flags = subdev[0]->flags;
820 concat->mtd.size = subdev[0]->size;
821 concat->mtd.erasesize = subdev[0]->erasesize;
822 concat->mtd.writesize = subdev[0]->writesize;
823 concat->mtd.oobsize = subdev[0]->oobsize;
824 concat->mtd.ecctype = subdev[0]->ecctype;
825 concat->mtd.eccsize = subdev[0]->eccsize;
826 if (subdev[0]->read_ecc)
827 concat->mtd.read_ecc = concat_read_ecc;
828 if (subdev[0]->write_ecc)
829 concat->mtd.write_ecc = concat_write_ecc;
830 if (subdev[0]->writev)
831 concat->mtd.writev = concat_writev;
832 if (subdev[0]->read_oob)
833 concat->mtd.read_oob = concat_read_oob;
834 if (subdev[0]->write_oob)
835 concat->mtd.write_oob = concat_write_oob;
836 if (subdev[0]->block_isbad)
837 concat->mtd.block_isbad = concat_block_isbad;
838 if (subdev[0]->block_markbad)
839 concat->mtd.block_markbad = concat_block_markbad;
841 concat->subdev[0] = subdev[0];
843 for (i = 1; i < num_devs; i++) {
844 if (concat->mtd.type != subdev[i]->type) {
846 printk("Incompatible device type on \"%s\"\n",
850 if (concat->mtd.flags != subdev[i]->flags) {
852 * Expect all flags except MTD_WRITEABLE to be
853 * equal on all subdevices.
855 if ((concat->mtd.flags ^ subdev[i]->
856 flags) & ~MTD_WRITEABLE) {
858 printk("Incompatible device flags on \"%s\"\n",
862 /* if writeable attribute differs,
863 make super device writeable */
865 subdev[i]->flags & MTD_WRITEABLE;
867 concat->mtd.size += subdev[i]->size;
868 if (concat->mtd.writesize != subdev[i]->writesize ||
869 concat->mtd.oobsize != subdev[i]->oobsize ||
870 concat->mtd.ecctype != subdev[i]->ecctype ||
871 concat->mtd.eccsize != subdev[i]->eccsize ||
872 !concat->mtd.read_ecc != !subdev[i]->read_ecc ||
873 !concat->mtd.write_ecc != !subdev[i]->write_ecc ||
874 !concat->mtd.read_oob != !subdev[i]->read_oob ||
875 !concat->mtd.write_oob != !subdev[i]->write_oob) {
877 printk("Incompatible OOB or ECC data on \"%s\"\n",
881 concat->subdev[i] = subdev[i];
885 if(concat->mtd.type == MTD_NANDFLASH)
886 memcpy(&concat->mtd.oobinfo, &subdev[0]->oobinfo,
887 sizeof(struct nand_oobinfo));
889 concat->num_subdev = num_devs;
890 concat->mtd.name = name;
892 concat->mtd.erase = concat_erase;
893 concat->mtd.read = concat_read;
894 concat->mtd.write = concat_write;
895 concat->mtd.sync = concat_sync;
896 concat->mtd.lock = concat_lock;
897 concat->mtd.unlock = concat_unlock;
898 concat->mtd.suspend = concat_suspend;
899 concat->mtd.resume = concat_resume;
902 * Combine the erase block size info of the subdevices:
904 * first, walk the map of the new device and see how
905 * many changes in erase size we have
907 max_erasesize = curr_erasesize = subdev[0]->erasesize;
908 num_erase_region = 1;
909 for (i = 0; i < num_devs; i++) {
910 if (subdev[i]->numeraseregions == 0) {
911 /* current subdevice has uniform erase size */
912 if (subdev[i]->erasesize != curr_erasesize) {
913 /* if it differs from the last subdevice's erase size, count it */
915 curr_erasesize = subdev[i]->erasesize;
916 if (curr_erasesize > max_erasesize)
917 max_erasesize = curr_erasesize;
920 /* current subdevice has variable erase size */
922 for (j = 0; j < subdev[i]->numeraseregions; j++) {
924 /* walk the list of erase regions, count any changes */
925 if (subdev[i]->eraseregions[j].erasesize !=
929 subdev[i]->eraseregions[j].
931 if (curr_erasesize > max_erasesize)
932 max_erasesize = curr_erasesize;
938 if (num_erase_region == 1) {
940 * All subdevices have the same uniform erase size.
943 concat->mtd.erasesize = curr_erasesize;
944 concat->mtd.numeraseregions = 0;
947 * erase block size varies across the subdevices: allocate
948 * space to store the data describing the variable erase regions
950 struct mtd_erase_region_info *erase_region_p;
951 u_int32_t begin, position;
953 concat->mtd.erasesize = max_erasesize;
954 concat->mtd.numeraseregions = num_erase_region;
955 concat->mtd.eraseregions = erase_region_p =
956 kmalloc(num_erase_region *
957 sizeof (struct mtd_erase_region_info), GFP_KERNEL);
958 if (!erase_region_p) {
961 ("memory allocation error while creating erase region list"
962 " for device \"%s\"\n", name);
967 * walk the map of the new device once more and fill in
968 * in erase region info:
970 curr_erasesize = subdev[0]->erasesize;
971 begin = position = 0;
972 for (i = 0; i < num_devs; i++) {
973 if (subdev[i]->numeraseregions == 0) {
974 /* current subdevice has uniform erase size */
975 if (subdev[i]->erasesize != curr_erasesize) {
977 * fill in an mtd_erase_region_info structure for the area
978 * we have walked so far:
980 erase_region_p->offset = begin;
981 erase_region_p->erasesize =
983 erase_region_p->numblocks =
984 (position - begin) / curr_erasesize;
987 curr_erasesize = subdev[i]->erasesize;
990 position += subdev[i]->size;
992 /* current subdevice has variable erase size */
994 for (j = 0; j < subdev[i]->numeraseregions; j++) {
995 /* walk the list of erase regions, count any changes */
996 if (subdev[i]->eraseregions[j].
997 erasesize != curr_erasesize) {
998 erase_region_p->offset = begin;
999 erase_region_p->erasesize =
1001 erase_region_p->numblocks =
1003 begin) / curr_erasesize;
1007 subdev[i]->eraseregions[j].
1012 subdev[i]->eraseregions[j].
1013 numblocks * curr_erasesize;
1017 /* Now write the final entry */
1018 erase_region_p->offset = begin;
1019 erase_region_p->erasesize = curr_erasesize;
1020 erase_region_p->numblocks = (position - begin) / curr_erasesize;
1023 return &concat->mtd;
1027 * This function destroys an MTD object obtained from concat_mtd_devs()
1030 void mtd_concat_destroy(struct mtd_info *mtd)
1032 struct mtd_concat *concat = CONCAT(mtd);
1033 if (concat->mtd.numeraseregions)
1034 kfree(concat->mtd.eraseregions);
1038 EXPORT_SYMBOL(mtd_concat_create);
1039 EXPORT_SYMBOL(mtd_concat_destroy);
1041 MODULE_LICENSE("GPL");
1042 MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
1043 MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");