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.8 2003/06/30 11:01:26 dwmw2 Exp $
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
18 #include <linux/mtd/mtd.h>
19 #include <linux/mtd/concat.h>
22 * Our storage structure:
23 * Subdev points to an array of pointers to struct mtd_info objects
24 * which is allocated along with this structure
30 struct mtd_info **subdev;
34 * how to calculate the size required for the above structure,
35 * including the pointer array subdev points to:
37 #define SIZEOF_STRUCT_MTD_CONCAT(num_subdev) \
38 ((sizeof(struct mtd_concat) + (num_subdev) * sizeof(struct mtd_info *)))
41 * Given a pointer to the MTD object in the mtd_concat structure,
42 * we can retrieve the pointer to that structure with this macro.
44 #define CONCAT(x) ((struct mtd_concat *)(x))
47 * MTD methods which look up the relevant subdevice, translate the
48 * effective address and pass through to the subdevice.
52 concat_read(struct mtd_info *mtd, loff_t from, size_t len,
53 size_t * retlen, u_char * buf)
55 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 = subdev->read(subdev, from, size, &retsize, buf);
96 concat_write(struct mtd_info *mtd, loff_t to, size_t len,
97 size_t * retlen, const u_char * buf)
99 struct mtd_concat *concat = CONCAT(mtd);
103 if (!(mtd->flags & MTD_WRITEABLE))
108 for (i = 0; i < concat->num_subdev; i++) {
109 struct mtd_info *subdev = concat->subdev[i];
110 size_t size, retsize;
112 if (to >= subdev->size) {
117 if (to + len > subdev->size)
118 size = subdev->size - to;
122 if (!(subdev->flags & MTD_WRITEABLE))
125 err = subdev->write(subdev, to, size, &retsize, buf);
143 concat_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
144 size_t * retlen, u_char * buf, u_char * eccbuf,
145 struct nand_oobinfo *oobsel)
147 struct mtd_concat *concat = CONCAT(mtd);
153 for (i = 0; i < concat->num_subdev; i++) {
154 struct mtd_info *subdev = concat->subdev[i];
155 size_t size, retsize;
157 if (from >= subdev->size) {
158 /* Not destined for this subdev */
160 from -= subdev->size;
164 if (from + len > subdev->size)
165 /* First part goes into this subdev */
166 size = subdev->size - from;
168 /* Entire transaction goes into this subdev */
171 if (subdev->read_ecc)
172 err = subdev->read_ecc(subdev, from, size,
173 &retsize, buf, eccbuf, oobsel);
188 eccbuf += subdev->oobsize;
189 /* in nand.c at least, eccbufs are
190 tagged with 2 (int)eccstatus'; we
191 must account for these */
192 eccbuf += 2 * (sizeof (int));
200 concat_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
201 size_t * retlen, const u_char * buf, u_char * eccbuf,
202 struct nand_oobinfo *oobsel)
204 struct mtd_concat *concat = CONCAT(mtd);
208 if (!(mtd->flags & MTD_WRITEABLE))
213 for (i = 0; i < concat->num_subdev; i++) {
214 struct mtd_info *subdev = concat->subdev[i];
215 size_t size, retsize;
217 if (to >= subdev->size) {
222 if (to + len > subdev->size)
223 size = subdev->size - to;
227 if (!(subdev->flags & MTD_WRITEABLE))
229 else if (subdev->write_ecc)
230 err = subdev->write_ecc(subdev, to, size,
231 &retsize, buf, eccbuf, oobsel);
246 eccbuf += subdev->oobsize;
253 concat_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
254 size_t * retlen, u_char * buf)
256 struct mtd_concat *concat = CONCAT(mtd);
262 for (i = 0; i < concat->num_subdev; i++) {
263 struct mtd_info *subdev = concat->subdev[i];
264 size_t size, retsize;
266 if (from >= subdev->size) {
267 /* Not destined for this subdev */
269 from -= subdev->size;
272 if (from + len > subdev->size)
273 /* First part goes into this subdev */
274 size = subdev->size - from;
276 /* Entire transaction goes into this subdev */
279 if (subdev->read_oob)
280 err = subdev->read_oob(subdev, from, size,
301 concat_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
302 size_t * retlen, const u_char * buf)
304 struct mtd_concat *concat = CONCAT(mtd);
308 if (!(mtd->flags & MTD_WRITEABLE))
313 for (i = 0; i < concat->num_subdev; i++) {
314 struct mtd_info *subdev = concat->subdev[i];
315 size_t size, retsize;
317 if (to >= subdev->size) {
322 if (to + len > subdev->size)
323 size = subdev->size - to;
327 if (!(subdev->flags & MTD_WRITEABLE))
329 else if (subdev->write_oob)
330 err = subdev->write_oob(subdev, to, size, &retsize,
350 static void concat_erase_callback(struct erase_info *instr)
352 wake_up((wait_queue_head_t *) instr->priv);
355 static int concat_dev_erase(struct mtd_info *mtd, struct erase_info *erase)
358 wait_queue_head_t waitq;
359 DECLARE_WAITQUEUE(wait, current);
362 * This code was stol^H^H^H^Hinspired by mtdchar.c
364 init_waitqueue_head(&waitq);
367 erase->callback = concat_erase_callback;
368 erase->priv = (unsigned long) &waitq;
371 * FIXME: Allow INTERRUPTIBLE. Which means
372 * not having the wait_queue head on the stack.
374 err = mtd->erase(mtd, erase);
376 set_current_state(TASK_UNINTERRUPTIBLE);
377 add_wait_queue(&waitq, &wait);
378 if (erase->state != MTD_ERASE_DONE
379 && erase->state != MTD_ERASE_FAILED)
381 remove_wait_queue(&waitq, &wait);
382 set_current_state(TASK_RUNNING);
384 err = (erase->state == MTD_ERASE_FAILED) ? -EIO : 0;
389 static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
391 struct mtd_concat *concat = CONCAT(mtd);
392 struct mtd_info *subdev;
395 struct erase_info *erase;
397 if (!(mtd->flags & MTD_WRITEABLE))
400 if (instr->addr > concat->mtd.size)
403 if (instr->len + instr->addr > concat->mtd.size)
407 * Check for proper erase block alignment of the to-be-erased area.
408 * It is easier to do this based on the super device's erase
409 * region info rather than looking at each particular sub-device
412 if (!concat->mtd.numeraseregions) {
413 /* the easy case: device has uniform erase block size */
414 if (instr->addr & (concat->mtd.erasesize - 1))
416 if (instr->len & (concat->mtd.erasesize - 1))
419 /* device has variable erase size */
420 struct mtd_erase_region_info *erase_regions =
421 concat->mtd.eraseregions;
424 * Find the erase region where the to-be-erased area begins:
426 for (i = 0; i < concat->mtd.numeraseregions &&
427 instr->addr >= erase_regions[i].offset; i++) ;
431 * Now erase_regions[i] is the region in which the
432 * to-be-erased area begins. Verify that the starting
433 * offset is aligned to this region's erase size:
435 if (instr->addr & (erase_regions[i].erasesize - 1))
439 * now find the erase region where the to-be-erased area ends:
441 for (; i < concat->mtd.numeraseregions &&
442 (instr->addr + instr->len) >= erase_regions[i].offset;
446 * check if the ending offset is aligned to this region's erase size
448 if ((instr->addr + instr->len) & (erase_regions[i].erasesize -
453 /* make a local copy of instr to avoid modifying the caller's struct */
454 erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
463 * find the subdevice where the to-be-erased area begins, adjust
464 * starting offset to be relative to the subdevice start
466 for (i = 0; i < concat->num_subdev; i++) {
467 subdev = concat->subdev[i];
468 if (subdev->size <= erase->addr)
469 erase->addr -= subdev->size;
474 /* must never happen since size limit has been verified above */
475 if (i >= concat->num_subdev)
478 /* now do the erase: */
480 for (; length > 0; i++) {
481 /* loop for all subdevices affected by this request */
482 subdev = concat->subdev[i]; /* get current subdevice */
484 /* limit length to subdevice's size: */
485 if (erase->addr + length > subdev->size)
486 erase->len = subdev->size - erase->addr;
490 if (!(subdev->flags & MTD_WRITEABLE)) {
494 length -= erase->len;
495 if ((err = concat_dev_erase(subdev, erase))) {
496 /* sanity check: should never happen since
497 * block alignment has been checked above */
503 * erase->addr specifies the offset of the area to be
504 * erased *within the current subdevice*. It can be
505 * non-zero only the first time through this loop, i.e.
506 * for the first subdevice where blocks need to be erased.
507 * All the following erases must begin at the start of the
508 * current subdevice, i.e. at offset zero.
516 instr->state = MTD_ERASE_DONE;
518 instr->callback(instr);
522 static int concat_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
524 struct mtd_concat *concat = CONCAT(mtd);
525 int i, err = -EINVAL;
527 if ((len + ofs) > mtd->size)
530 for (i = 0; i < concat->num_subdev; i++) {
531 struct mtd_info *subdev = concat->subdev[i];
534 if (ofs >= subdev->size) {
539 if (ofs + len > subdev->size)
540 size = subdev->size - ofs;
544 err = subdev->lock(subdev, ofs, size);
560 static int concat_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
562 struct mtd_concat *concat = CONCAT(mtd);
565 if ((len + ofs) > mtd->size)
568 for (i = 0; i < concat->num_subdev; i++) {
569 struct mtd_info *subdev = concat->subdev[i];
572 if (ofs >= subdev->size) {
577 if (ofs + len > subdev->size)
578 size = subdev->size - ofs;
582 err = subdev->unlock(subdev, ofs, size);
598 static void concat_sync(struct mtd_info *mtd)
600 struct mtd_concat *concat = CONCAT(mtd);
603 for (i = 0; i < concat->num_subdev; i++) {
604 struct mtd_info *subdev = concat->subdev[i];
605 subdev->sync(subdev);
609 static int concat_suspend(struct mtd_info *mtd)
611 struct mtd_concat *concat = CONCAT(mtd);
614 for (i = 0; i < concat->num_subdev; i++) {
615 struct mtd_info *subdev = concat->subdev[i];
616 if ((rc = subdev->suspend(subdev)) < 0)
622 static void concat_resume(struct mtd_info *mtd)
624 struct mtd_concat *concat = CONCAT(mtd);
627 for (i = 0; i < concat->num_subdev; i++) {
628 struct mtd_info *subdev = concat->subdev[i];
629 subdev->resume(subdev);
634 * This function constructs a virtual MTD device by concatenating
635 * num_devs MTD devices. A pointer to the new device object is
636 * stored to *new_dev upon success. This function does _not_
637 * register any devices: this is the caller's responsibility.
639 struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */
640 int num_devs, /* number of subdevices */
642 { /* name for the new device */
645 struct mtd_concat *concat;
646 u_int32_t max_erasesize, curr_erasesize;
647 int num_erase_region;
649 printk(KERN_NOTICE "Concatenating MTD devices:\n");
650 for (i = 0; i < num_devs; i++)
651 printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
652 printk(KERN_NOTICE "into device \"%s\"\n", name);
654 /* allocate the device structure */
655 size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
656 concat = kmalloc(size, GFP_KERNEL);
659 ("memory allocation error while creating concatenated device \"%s\"\n",
663 memset(concat, 0, size);
664 concat->subdev = (struct mtd_info **) (concat + 1);
667 * Set up the new "super" device's MTD object structure, check for
668 * incompatibilites between the subdevices.
670 concat->mtd.type = subdev[0]->type;
671 concat->mtd.flags = subdev[0]->flags;
672 concat->mtd.size = subdev[0]->size;
673 concat->mtd.erasesize = subdev[0]->erasesize;
674 concat->mtd.oobblock = subdev[0]->oobblock;
675 concat->mtd.oobsize = subdev[0]->oobsize;
676 concat->mtd.ecctype = subdev[0]->ecctype;
677 concat->mtd.eccsize = subdev[0]->eccsize;
678 if (subdev[0]->read_ecc)
679 concat->mtd.read_ecc = concat_read_ecc;
680 if (subdev[0]->write_ecc)
681 concat->mtd.write_ecc = concat_write_ecc;
682 if (subdev[0]->read_oob)
683 concat->mtd.read_oob = concat_read_oob;
684 if (subdev[0]->write_oob)
685 concat->mtd.write_oob = concat_write_oob;
687 concat->subdev[0] = subdev[0];
689 for (i = 1; i < num_devs; i++) {
690 if (concat->mtd.type != subdev[i]->type) {
692 printk("Incompatible device type on \"%s\"\n",
696 if (concat->mtd.flags != subdev[i]->flags) {
698 * Expect all flags except MTD_WRITEABLE to be
699 * equal on all subdevices.
701 if ((concat->mtd.flags ^ subdev[i]->
702 flags) & ~MTD_WRITEABLE) {
704 printk("Incompatible device flags on \"%s\"\n",
708 /* if writeable attribute differs,
709 make super device writeable */
711 subdev[i]->flags & MTD_WRITEABLE;
713 concat->mtd.size += subdev[i]->size;
714 if (concat->mtd.oobblock != subdev[i]->oobblock ||
715 concat->mtd.oobsize != subdev[i]->oobsize ||
716 concat->mtd.ecctype != subdev[i]->ecctype ||
717 concat->mtd.eccsize != subdev[i]->eccsize ||
718 !concat->mtd.read_ecc != !subdev[i]->read_ecc ||
719 !concat->mtd.write_ecc != !subdev[i]->write_ecc ||
720 !concat->mtd.read_oob != !subdev[i]->read_oob ||
721 !concat->mtd.write_oob != !subdev[i]->write_oob) {
723 printk("Incompatible OOB or ECC data on \"%s\"\n",
727 concat->subdev[i] = subdev[i];
731 concat->num_subdev = num_devs;
732 concat->mtd.name = name;
735 * NOTE: for now, we do not provide any readv()/writev() methods
736 * because they are messy to implement and they are not
737 * used to a great extent anyway.
739 concat->mtd.erase = concat_erase;
740 concat->mtd.read = concat_read;
741 concat->mtd.write = concat_write;
742 concat->mtd.sync = concat_sync;
743 concat->mtd.lock = concat_lock;
744 concat->mtd.unlock = concat_unlock;
745 concat->mtd.suspend = concat_suspend;
746 concat->mtd.resume = concat_resume;
749 * Combine the erase block size info of the subdevices:
751 * first, walk the map of the new device and see how
752 * many changes in erase size we have
754 max_erasesize = curr_erasesize = subdev[0]->erasesize;
755 num_erase_region = 1;
756 for (i = 0; i < num_devs; i++) {
757 if (subdev[i]->numeraseregions == 0) {
758 /* current subdevice has uniform erase size */
759 if (subdev[i]->erasesize != curr_erasesize) {
760 /* if it differs from the last subdevice's erase size, count it */
762 curr_erasesize = subdev[i]->erasesize;
763 if (curr_erasesize > max_erasesize)
764 max_erasesize = curr_erasesize;
767 /* current subdevice has variable erase size */
769 for (j = 0; j < subdev[i]->numeraseregions; j++) {
771 /* walk the list of erase regions, count any changes */
772 if (subdev[i]->eraseregions[j].erasesize !=
776 subdev[i]->eraseregions[j].
778 if (curr_erasesize > max_erasesize)
779 max_erasesize = curr_erasesize;
785 if (num_erase_region == 1) {
787 * All subdevices have the same uniform erase size.
790 concat->mtd.erasesize = curr_erasesize;
791 concat->mtd.numeraseregions = 0;
794 * erase block size varies across the subdevices: allocate
795 * space to store the data describing the variable erase regions
797 struct mtd_erase_region_info *erase_region_p;
798 u_int32_t begin, position;
800 concat->mtd.erasesize = max_erasesize;
801 concat->mtd.numeraseregions = num_erase_region;
802 concat->mtd.eraseregions = erase_region_p =
803 kmalloc(num_erase_region *
804 sizeof (struct mtd_erase_region_info), GFP_KERNEL);
805 if (!erase_region_p) {
808 ("memory allocation error while creating erase region list"
809 " for device \"%s\"\n", name);
814 * walk the map of the new device once more and fill in
815 * in erase region info:
817 curr_erasesize = subdev[0]->erasesize;
818 begin = position = 0;
819 for (i = 0; i < num_devs; i++) {
820 if (subdev[i]->numeraseregions == 0) {
821 /* current subdevice has uniform erase size */
822 if (subdev[i]->erasesize != curr_erasesize) {
824 * fill in an mtd_erase_region_info structure for the area
825 * we have walked so far:
827 erase_region_p->offset = begin;
828 erase_region_p->erasesize =
830 erase_region_p->numblocks =
831 (position - begin) / curr_erasesize;
834 curr_erasesize = subdev[i]->erasesize;
837 position += subdev[i]->size;
839 /* current subdevice has variable erase size */
841 for (j = 0; j < subdev[i]->numeraseregions; j++) {
842 /* walk the list of erase regions, count any changes */
843 if (subdev[i]->eraseregions[j].
844 erasesize != curr_erasesize) {
845 erase_region_p->offset = begin;
846 erase_region_p->erasesize =
848 erase_region_p->numblocks =
850 begin) / curr_erasesize;
854 subdev[i]->eraseregions[j].
859 subdev[i]->eraseregions[j].
860 numblocks * curr_erasesize;
864 /* Now write the final entry */
865 erase_region_p->offset = begin;
866 erase_region_p->erasesize = curr_erasesize;
867 erase_region_p->numblocks = (position - begin) / curr_erasesize;
874 * This function destroys an MTD object obtained from concat_mtd_devs()
877 void mtd_concat_destroy(struct mtd_info *mtd)
879 struct mtd_concat *concat = CONCAT(mtd);
880 if (concat->mtd.numeraseregions)
881 kfree(concat->mtd.eraseregions);
885 EXPORT_SYMBOL(mtd_concat_create);
886 EXPORT_SYMBOL(mtd_concat_destroy);
888 MODULE_LICENSE("GPL");
889 MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
890 MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");