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_writev(struct mtd_info *mtd, const struct kvec *vecs,
147 unsigned long count, loff_t to, size_t * retlen)
149 struct mtd_concat *concat = CONCAT(mtd);
150 struct kvec *vecs_copy;
151 unsigned long entry_low, entry_high;
152 size_t total_len = 0;
156 if (!(mtd->flags & MTD_WRITEABLE))
161 /* Calculate total length of data */
162 for (i = 0; i < count; i++)
163 total_len += vecs[i].iov_len;
165 /* Do not allow write past end of device */
166 if ((to + total_len) > mtd->size)
169 /* Check alignment */
170 if (mtd->writesize > 1) {
172 if (do_div(__to, mtd->writesize) || (total_len % mtd->writesize))
176 /* make a copy of vecs */
177 vecs_copy = kmalloc(sizeof(struct kvec) * count, GFP_KERNEL);
180 memcpy(vecs_copy, vecs, sizeof(struct kvec) * count);
183 for (i = 0; i < concat->num_subdev; i++) {
184 struct mtd_info *subdev = concat->subdev[i];
185 size_t size, wsize, retsize, old_iov_len;
187 if (to >= subdev->size) {
192 size = min(total_len, (size_t)(subdev->size - to));
193 wsize = size; /* store for future use */
195 entry_high = entry_low;
196 while (entry_high < count) {
197 if (size <= vecs_copy[entry_high].iov_len)
199 size -= vecs_copy[entry_high++].iov_len;
202 old_iov_len = vecs_copy[entry_high].iov_len;
203 vecs_copy[entry_high].iov_len = size;
205 if (!(subdev->flags & MTD_WRITEABLE))
208 err = subdev->writev(subdev, &vecs_copy[entry_low],
209 entry_high - entry_low + 1, to, &retsize);
211 vecs_copy[entry_high].iov_len = old_iov_len - size;
212 vecs_copy[entry_high].iov_base += size;
214 entry_low = entry_high;
234 concat_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
236 struct mtd_concat *concat = CONCAT(mtd);
237 struct mtd_oob_ops devops = *ops;
242 for (i = 0; i < concat->num_subdev; i++) {
243 struct mtd_info *subdev = concat->subdev[i];
245 if (from >= subdev->size) {
246 from -= subdev->size;
251 if (from + devops.len > subdev->size)
252 devops.len = subdev->size - from;
254 err = subdev->read_oob(subdev, from, &devops);
255 ops->retlen += devops.retlen;
259 devops.len = ops->len - ops->retlen;
264 devops.datbuf += devops.retlen;
266 devops.oobbuf += devops.ooblen;
274 concat_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
276 struct mtd_concat *concat = CONCAT(mtd);
277 struct mtd_oob_ops devops = *ops;
280 if (!(mtd->flags & MTD_WRITEABLE))
285 for (i = 0; i < concat->num_subdev; i++) {
286 struct mtd_info *subdev = concat->subdev[i];
288 if (to >= subdev->size) {
293 /* partial write ? */
294 if (to + devops.len > subdev->size)
295 devops.len = subdev->size - to;
297 err = subdev->write_oob(subdev, to, &devops);
298 ops->retlen += devops.retlen;
302 devops.len = ops->len - ops->retlen;
307 devops.datbuf += devops.retlen;
309 devops.oobbuf += devops.ooblen;
315 static void concat_erase_callback(struct erase_info *instr)
317 wake_up((wait_queue_head_t *) instr->priv);
320 static int concat_dev_erase(struct mtd_info *mtd, struct erase_info *erase)
323 wait_queue_head_t waitq;
324 DECLARE_WAITQUEUE(wait, current);
327 * This code was stol^H^H^H^Hinspired by mtdchar.c
329 init_waitqueue_head(&waitq);
332 erase->callback = concat_erase_callback;
333 erase->priv = (unsigned long) &waitq;
336 * FIXME: Allow INTERRUPTIBLE. Which means
337 * not having the wait_queue head on the stack.
339 err = mtd->erase(mtd, erase);
341 set_current_state(TASK_UNINTERRUPTIBLE);
342 add_wait_queue(&waitq, &wait);
343 if (erase->state != MTD_ERASE_DONE
344 && erase->state != MTD_ERASE_FAILED)
346 remove_wait_queue(&waitq, &wait);
347 set_current_state(TASK_RUNNING);
349 err = (erase->state == MTD_ERASE_FAILED) ? -EIO : 0;
354 static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
356 struct mtd_concat *concat = CONCAT(mtd);
357 struct mtd_info *subdev;
359 u_int32_t length, offset = 0;
360 struct erase_info *erase;
362 if (!(mtd->flags & MTD_WRITEABLE))
365 if (instr->addr > concat->mtd.size)
368 if (instr->len + instr->addr > concat->mtd.size)
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 (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 ((instr->addr + instr->len) & (erase_regions[i].erasesize -
418 instr->fail_addr = 0xffffffff;
420 /* make a local copy of instr to avoid modifying the caller's struct */
421 erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
430 * find the subdevice where the to-be-erased area begins, adjust
431 * starting offset to be relative to the subdevice start
433 for (i = 0; i < concat->num_subdev; i++) {
434 subdev = concat->subdev[i];
435 if (subdev->size <= erase->addr) {
436 erase->addr -= subdev->size;
437 offset += subdev->size;
443 /* must never happen since size limit has been verified above */
444 BUG_ON(i >= concat->num_subdev);
446 /* now do the erase: */
448 for (; length > 0; i++) {
449 /* loop for all subdevices affected by this request */
450 subdev = concat->subdev[i]; /* get current subdevice */
452 /* limit length to subdevice's size: */
453 if (erase->addr + length > subdev->size)
454 erase->len = subdev->size - erase->addr;
458 if (!(subdev->flags & MTD_WRITEABLE)) {
462 length -= erase->len;
463 if ((err = concat_dev_erase(subdev, erase))) {
464 /* sanity check: should never happen since
465 * block alignment has been checked above */
466 BUG_ON(err == -EINVAL);
467 if (erase->fail_addr != 0xffffffff)
468 instr->fail_addr = erase->fail_addr + offset;
472 * erase->addr specifies the offset of the area to be
473 * erased *within the current subdevice*. It can be
474 * non-zero only the first time through this loop, i.e.
475 * for the first subdevice where blocks need to be erased.
476 * All the following erases must begin at the start of the
477 * current subdevice, i.e. at offset zero.
480 offset += subdev->size;
482 instr->state = erase->state;
488 instr->callback(instr);
492 static int concat_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
494 struct mtd_concat *concat = CONCAT(mtd);
495 int i, err = -EINVAL;
497 if ((len + ofs) > mtd->size)
500 for (i = 0; i < concat->num_subdev; i++) {
501 struct mtd_info *subdev = concat->subdev[i];
504 if (ofs >= subdev->size) {
509 if (ofs + len > subdev->size)
510 size = subdev->size - ofs;
514 err = subdev->lock(subdev, ofs, size);
530 static int concat_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
532 struct mtd_concat *concat = CONCAT(mtd);
535 if ((len + ofs) > mtd->size)
538 for (i = 0; i < concat->num_subdev; i++) {
539 struct mtd_info *subdev = concat->subdev[i];
542 if (ofs >= subdev->size) {
547 if (ofs + len > subdev->size)
548 size = subdev->size - ofs;
552 err = subdev->unlock(subdev, ofs, size);
568 static void concat_sync(struct mtd_info *mtd)
570 struct mtd_concat *concat = CONCAT(mtd);
573 for (i = 0; i < concat->num_subdev; i++) {
574 struct mtd_info *subdev = concat->subdev[i];
575 subdev->sync(subdev);
579 static int concat_suspend(struct mtd_info *mtd)
581 struct mtd_concat *concat = CONCAT(mtd);
584 for (i = 0; i < concat->num_subdev; i++) {
585 struct mtd_info *subdev = concat->subdev[i];
586 if ((rc = subdev->suspend(subdev)) < 0)
592 static void concat_resume(struct mtd_info *mtd)
594 struct mtd_concat *concat = CONCAT(mtd);
597 for (i = 0; i < concat->num_subdev; i++) {
598 struct mtd_info *subdev = concat->subdev[i];
599 subdev->resume(subdev);
603 static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
605 struct mtd_concat *concat = CONCAT(mtd);
608 if (!concat->subdev[0]->block_isbad)
614 for (i = 0; i < concat->num_subdev; i++) {
615 struct mtd_info *subdev = concat->subdev[i];
617 if (ofs >= subdev->size) {
622 res = subdev->block_isbad(subdev, ofs);
629 static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
631 struct mtd_concat *concat = CONCAT(mtd);
632 int i, err = -EINVAL;
634 if (!concat->subdev[0]->block_markbad)
640 for (i = 0; i < concat->num_subdev; i++) {
641 struct mtd_info *subdev = concat->subdev[i];
643 if (ofs >= subdev->size) {
648 err = subdev->block_markbad(subdev, ofs);
656 * This function constructs a virtual MTD device by concatenating
657 * num_devs MTD devices. A pointer to the new device object is
658 * stored to *new_dev upon success. This function does _not_
659 * register any devices: this is the caller's responsibility.
661 struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */
662 int num_devs, /* number of subdevices */
664 { /* name for the new device */
667 struct mtd_concat *concat;
668 u_int32_t max_erasesize, curr_erasesize;
669 int num_erase_region;
671 printk(KERN_NOTICE "Concatenating MTD devices:\n");
672 for (i = 0; i < num_devs; i++)
673 printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
674 printk(KERN_NOTICE "into device \"%s\"\n", name);
676 /* allocate the device structure */
677 size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
678 concat = kmalloc(size, GFP_KERNEL);
681 ("memory allocation error while creating concatenated device \"%s\"\n",
685 memset(concat, 0, size);
686 concat->subdev = (struct mtd_info **) (concat + 1);
689 * Set up the new "super" device's MTD object structure, check for
690 * incompatibilites between the subdevices.
692 concat->mtd.type = subdev[0]->type;
693 concat->mtd.flags = subdev[0]->flags;
694 concat->mtd.size = subdev[0]->size;
695 concat->mtd.erasesize = subdev[0]->erasesize;
696 concat->mtd.writesize = subdev[0]->writesize;
697 concat->mtd.oobsize = subdev[0]->oobsize;
698 concat->mtd.ecctype = subdev[0]->ecctype;
699 concat->mtd.eccsize = subdev[0]->eccsize;
700 if (subdev[0]->writev)
701 concat->mtd.writev = concat_writev;
702 if (subdev[0]->read_oob)
703 concat->mtd.read_oob = concat_read_oob;
704 if (subdev[0]->write_oob)
705 concat->mtd.write_oob = concat_write_oob;
706 if (subdev[0]->block_isbad)
707 concat->mtd.block_isbad = concat_block_isbad;
708 if (subdev[0]->block_markbad)
709 concat->mtd.block_markbad = concat_block_markbad;
711 concat->subdev[0] = subdev[0];
713 for (i = 1; i < num_devs; i++) {
714 if (concat->mtd.type != subdev[i]->type) {
716 printk("Incompatible device type on \"%s\"\n",
720 if (concat->mtd.flags != subdev[i]->flags) {
722 * Expect all flags except MTD_WRITEABLE to be
723 * equal on all subdevices.
725 if ((concat->mtd.flags ^ subdev[i]->
726 flags) & ~MTD_WRITEABLE) {
728 printk("Incompatible device flags on \"%s\"\n",
732 /* if writeable attribute differs,
733 make super device writeable */
735 subdev[i]->flags & MTD_WRITEABLE;
737 concat->mtd.size += subdev[i]->size;
738 if (concat->mtd.writesize != subdev[i]->writesize ||
739 concat->mtd.oobsize != subdev[i]->oobsize ||
740 concat->mtd.ecctype != subdev[i]->ecctype ||
741 concat->mtd.eccsize != subdev[i]->eccsize ||
742 !concat->mtd.read_oob != !subdev[i]->read_oob ||
743 !concat->mtd.write_oob != !subdev[i]->write_oob) {
745 printk("Incompatible OOB or ECC data on \"%s\"\n",
749 concat->subdev[i] = subdev[i];
753 concat->mtd.ecclayout = subdev[0]->ecclayout;
755 concat->num_subdev = num_devs;
756 concat->mtd.name = name;
758 concat->mtd.erase = concat_erase;
759 concat->mtd.read = concat_read;
760 concat->mtd.write = concat_write;
761 concat->mtd.sync = concat_sync;
762 concat->mtd.lock = concat_lock;
763 concat->mtd.unlock = concat_unlock;
764 concat->mtd.suspend = concat_suspend;
765 concat->mtd.resume = concat_resume;
768 * Combine the erase block size info of the subdevices:
770 * first, walk the map of the new device and see how
771 * many changes in erase size we have
773 max_erasesize = curr_erasesize = subdev[0]->erasesize;
774 num_erase_region = 1;
775 for (i = 0; i < num_devs; i++) {
776 if (subdev[i]->numeraseregions == 0) {
777 /* current subdevice has uniform erase size */
778 if (subdev[i]->erasesize != curr_erasesize) {
779 /* if it differs from the last subdevice's erase size, count it */
781 curr_erasesize = subdev[i]->erasesize;
782 if (curr_erasesize > max_erasesize)
783 max_erasesize = curr_erasesize;
786 /* current subdevice has variable erase size */
788 for (j = 0; j < subdev[i]->numeraseregions; j++) {
790 /* walk the list of erase regions, count any changes */
791 if (subdev[i]->eraseregions[j].erasesize !=
795 subdev[i]->eraseregions[j].
797 if (curr_erasesize > max_erasesize)
798 max_erasesize = curr_erasesize;
804 if (num_erase_region == 1) {
806 * All subdevices have the same uniform erase size.
809 concat->mtd.erasesize = curr_erasesize;
810 concat->mtd.numeraseregions = 0;
813 * erase block size varies across the subdevices: allocate
814 * space to store the data describing the variable erase regions
816 struct mtd_erase_region_info *erase_region_p;
817 u_int32_t begin, position;
819 concat->mtd.erasesize = max_erasesize;
820 concat->mtd.numeraseregions = num_erase_region;
821 concat->mtd.eraseregions = erase_region_p =
822 kmalloc(num_erase_region *
823 sizeof (struct mtd_erase_region_info), GFP_KERNEL);
824 if (!erase_region_p) {
827 ("memory allocation error while creating erase region list"
828 " for device \"%s\"\n", name);
833 * walk the map of the new device once more and fill in
834 * in erase region info:
836 curr_erasesize = subdev[0]->erasesize;
837 begin = position = 0;
838 for (i = 0; i < num_devs; i++) {
839 if (subdev[i]->numeraseregions == 0) {
840 /* current subdevice has uniform erase size */
841 if (subdev[i]->erasesize != curr_erasesize) {
843 * fill in an mtd_erase_region_info structure for the area
844 * we have walked so far:
846 erase_region_p->offset = begin;
847 erase_region_p->erasesize =
849 erase_region_p->numblocks =
850 (position - begin) / curr_erasesize;
853 curr_erasesize = subdev[i]->erasesize;
856 position += subdev[i]->size;
858 /* current subdevice has variable erase size */
860 for (j = 0; j < subdev[i]->numeraseregions; j++) {
861 /* walk the list of erase regions, count any changes */
862 if (subdev[i]->eraseregions[j].
863 erasesize != curr_erasesize) {
864 erase_region_p->offset = begin;
865 erase_region_p->erasesize =
867 erase_region_p->numblocks =
869 begin) / curr_erasesize;
873 subdev[i]->eraseregions[j].
878 subdev[i]->eraseregions[j].
879 numblocks * curr_erasesize;
883 /* Now write the final entry */
884 erase_region_p->offset = begin;
885 erase_region_p->erasesize = curr_erasesize;
886 erase_region_p->numblocks = (position - begin) / curr_erasesize;
893 * This function destroys an MTD object obtained from concat_mtd_devs()
896 void mtd_concat_destroy(struct mtd_info *mtd)
898 struct mtd_concat *concat = CONCAT(mtd);
899 if (concat->mtd.numeraseregions)
900 kfree(concat->mtd.eraseregions);
904 EXPORT_SYMBOL(mtd_concat_create);
905 EXPORT_SYMBOL(mtd_concat_destroy);
907 MODULE_LICENSE("GPL");
908 MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
909 MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");