]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/mtd/mtdconcat.c
6d52137988faf3e5fe29e628294044a145d37a00
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
, size_t len
,
235 size_t * retlen
, u_char
* buf
)
237 struct mtd_concat
*concat
= CONCAT(mtd
);
243 for (i
= 0; i
< concat
->num_subdev
; i
++) {
244 struct mtd_info
*subdev
= concat
->subdev
[i
];
245 size_t size
, retsize
;
247 if (from
>= subdev
->size
) {
248 /* Not destined for this subdev */
250 from
-= subdev
->size
;
253 if (from
+ len
> subdev
->size
)
254 /* First part goes into this subdev */
255 size
= subdev
->size
- from
;
257 /* Entire transaction goes into this subdev */
260 if (subdev
->read_oob
)
261 err
= subdev
->read_oob(subdev
, from
, size
,
282 concat_write_oob(struct mtd_info
*mtd
, loff_t to
, size_t len
,
283 size_t * retlen
, const u_char
* buf
)
285 struct mtd_concat
*concat
= CONCAT(mtd
);
289 if (!(mtd
->flags
& MTD_WRITEABLE
))
294 for (i
= 0; i
< concat
->num_subdev
; i
++) {
295 struct mtd_info
*subdev
= concat
->subdev
[i
];
296 size_t size
, retsize
;
298 if (to
>= subdev
->size
) {
303 if (to
+ len
> subdev
->size
)
304 size
= subdev
->size
- to
;
308 if (!(subdev
->flags
& MTD_WRITEABLE
))
310 else if (subdev
->write_oob
)
311 err
= subdev
->write_oob(subdev
, to
, size
, &retsize
,
331 static void concat_erase_callback(struct erase_info
*instr
)
333 wake_up((wait_queue_head_t
*) instr
->priv
);
336 static int concat_dev_erase(struct mtd_info
*mtd
, struct erase_info
*erase
)
339 wait_queue_head_t waitq
;
340 DECLARE_WAITQUEUE(wait
, current
);
343 * This code was stol^H^H^H^Hinspired by mtdchar.c
345 init_waitqueue_head(&waitq
);
348 erase
->callback
= concat_erase_callback
;
349 erase
->priv
= (unsigned long) &waitq
;
352 * FIXME: Allow INTERRUPTIBLE. Which means
353 * not having the wait_queue head on the stack.
355 err
= mtd
->erase(mtd
, erase
);
357 set_current_state(TASK_UNINTERRUPTIBLE
);
358 add_wait_queue(&waitq
, &wait
);
359 if (erase
->state
!= MTD_ERASE_DONE
360 && erase
->state
!= MTD_ERASE_FAILED
)
362 remove_wait_queue(&waitq
, &wait
);
363 set_current_state(TASK_RUNNING
);
365 err
= (erase
->state
== MTD_ERASE_FAILED
) ? -EIO
: 0;
370 static int concat_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
372 struct mtd_concat
*concat
= CONCAT(mtd
);
373 struct mtd_info
*subdev
;
375 u_int32_t length
, offset
= 0;
376 struct erase_info
*erase
;
378 if (!(mtd
->flags
& MTD_WRITEABLE
))
381 if (instr
->addr
> concat
->mtd
.size
)
384 if (instr
->len
+ instr
->addr
> concat
->mtd
.size
)
388 * Check for proper erase block alignment of the to-be-erased area.
389 * It is easier to do this based on the super device's erase
390 * region info rather than looking at each particular sub-device
393 if (!concat
->mtd
.numeraseregions
) {
394 /* the easy case: device has uniform erase block size */
395 if (instr
->addr
& (concat
->mtd
.erasesize
- 1))
397 if (instr
->len
& (concat
->mtd
.erasesize
- 1))
400 /* device has variable erase size */
401 struct mtd_erase_region_info
*erase_regions
=
402 concat
->mtd
.eraseregions
;
405 * Find the erase region where the to-be-erased area begins:
407 for (i
= 0; i
< concat
->mtd
.numeraseregions
&&
408 instr
->addr
>= erase_regions
[i
].offset
; i
++) ;
412 * Now erase_regions[i] is the region in which the
413 * to-be-erased area begins. Verify that the starting
414 * offset is aligned to this region's erase size:
416 if (instr
->addr
& (erase_regions
[i
].erasesize
- 1))
420 * now find the erase region where the to-be-erased area ends:
422 for (; i
< concat
->mtd
.numeraseregions
&&
423 (instr
->addr
+ instr
->len
) >= erase_regions
[i
].offset
;
427 * check if the ending offset is aligned to this region's erase size
429 if ((instr
->addr
+ instr
->len
) & (erase_regions
[i
].erasesize
-
434 instr
->fail_addr
= 0xffffffff;
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 if (!(subdev
->flags
& MTD_WRITEABLE
)) {
478 length
-= erase
->len
;
479 if ((err
= concat_dev_erase(subdev
, erase
))) {
480 /* sanity check: should never happen since
481 * block alignment has been checked above */
482 BUG_ON(err
== -EINVAL
);
483 if (erase
->fail_addr
!= 0xffffffff)
484 instr
->fail_addr
= erase
->fail_addr
+ offset
;
488 * erase->addr specifies the offset of the area to be
489 * erased *within the current subdevice*. It can be
490 * non-zero only the first time through this loop, i.e.
491 * for the first subdevice where blocks need to be erased.
492 * All the following erases must begin at the start of the
493 * current subdevice, i.e. at offset zero.
496 offset
+= subdev
->size
;
498 instr
->state
= erase
->state
;
504 instr
->callback(instr
);
508 static int concat_lock(struct mtd_info
*mtd
, loff_t ofs
, size_t len
)
510 struct mtd_concat
*concat
= CONCAT(mtd
);
511 int i
, err
= -EINVAL
;
513 if ((len
+ ofs
) > mtd
->size
)
516 for (i
= 0; i
< concat
->num_subdev
; i
++) {
517 struct mtd_info
*subdev
= concat
->subdev
[i
];
520 if (ofs
>= subdev
->size
) {
525 if (ofs
+ len
> subdev
->size
)
526 size
= subdev
->size
- ofs
;
530 err
= subdev
->lock(subdev
, ofs
, size
);
546 static int concat_unlock(struct mtd_info
*mtd
, loff_t ofs
, size_t len
)
548 struct mtd_concat
*concat
= CONCAT(mtd
);
551 if ((len
+ ofs
) > mtd
->size
)
554 for (i
= 0; i
< concat
->num_subdev
; i
++) {
555 struct mtd_info
*subdev
= concat
->subdev
[i
];
558 if (ofs
>= subdev
->size
) {
563 if (ofs
+ len
> subdev
->size
)
564 size
= subdev
->size
- ofs
;
568 err
= subdev
->unlock(subdev
, ofs
, size
);
584 static void concat_sync(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 subdev
->sync(subdev
);
595 static int concat_suspend(struct mtd_info
*mtd
)
597 struct mtd_concat
*concat
= CONCAT(mtd
);
600 for (i
= 0; i
< concat
->num_subdev
; i
++) {
601 struct mtd_info
*subdev
= concat
->subdev
[i
];
602 if ((rc
= subdev
->suspend(subdev
)) < 0)
608 static void concat_resume(struct mtd_info
*mtd
)
610 struct mtd_concat
*concat
= CONCAT(mtd
);
613 for (i
= 0; i
< concat
->num_subdev
; i
++) {
614 struct mtd_info
*subdev
= concat
->subdev
[i
];
615 subdev
->resume(subdev
);
619 static int concat_block_isbad(struct mtd_info
*mtd
, loff_t ofs
)
621 struct mtd_concat
*concat
= CONCAT(mtd
);
624 if (!concat
->subdev
[0]->block_isbad
)
630 for (i
= 0; i
< concat
->num_subdev
; i
++) {
631 struct mtd_info
*subdev
= concat
->subdev
[i
];
633 if (ofs
>= subdev
->size
) {
638 res
= subdev
->block_isbad(subdev
, ofs
);
645 static int concat_block_markbad(struct mtd_info
*mtd
, loff_t ofs
)
647 struct mtd_concat
*concat
= CONCAT(mtd
);
648 int i
, err
= -EINVAL
;
650 if (!concat
->subdev
[0]->block_markbad
)
656 for (i
= 0; i
< concat
->num_subdev
; i
++) {
657 struct mtd_info
*subdev
= concat
->subdev
[i
];
659 if (ofs
>= subdev
->size
) {
664 err
= subdev
->block_markbad(subdev
, ofs
);
672 * This function constructs a virtual MTD device by concatenating
673 * num_devs MTD devices. A pointer to the new device object is
674 * stored to *new_dev upon success. This function does _not_
675 * register any devices: this is the caller's responsibility.
677 struct mtd_info
*mtd_concat_create(struct mtd_info
*subdev
[], /* subdevices to concatenate */
678 int num_devs
, /* number of subdevices */
680 { /* name for the new device */
683 struct mtd_concat
*concat
;
684 u_int32_t max_erasesize
, curr_erasesize
;
685 int num_erase_region
;
687 printk(KERN_NOTICE
"Concatenating MTD devices:\n");
688 for (i
= 0; i
< num_devs
; i
++)
689 printk(KERN_NOTICE
"(%d): \"%s\"\n", i
, subdev
[i
]->name
);
690 printk(KERN_NOTICE
"into device \"%s\"\n", name
);
692 /* allocate the device structure */
693 size
= SIZEOF_STRUCT_MTD_CONCAT(num_devs
);
694 concat
= kmalloc(size
, GFP_KERNEL
);
697 ("memory allocation error while creating concatenated device \"%s\"\n",
701 memset(concat
, 0, size
);
702 concat
->subdev
= (struct mtd_info
**) (concat
+ 1);
705 * Set up the new "super" device's MTD object structure, check for
706 * incompatibilites between the subdevices.
708 concat
->mtd
.type
= subdev
[0]->type
;
709 concat
->mtd
.flags
= subdev
[0]->flags
;
710 concat
->mtd
.size
= subdev
[0]->size
;
711 concat
->mtd
.erasesize
= subdev
[0]->erasesize
;
712 concat
->mtd
.writesize
= subdev
[0]->writesize
;
713 concat
->mtd
.oobsize
= subdev
[0]->oobsize
;
714 concat
->mtd
.ecctype
= subdev
[0]->ecctype
;
715 concat
->mtd
.eccsize
= subdev
[0]->eccsize
;
716 if (subdev
[0]->writev
)
717 concat
->mtd
.writev
= concat_writev
;
718 if (subdev
[0]->read_oob
)
719 concat
->mtd
.read_oob
= concat_read_oob
;
720 if (subdev
[0]->write_oob
)
721 concat
->mtd
.write_oob
= concat_write_oob
;
722 if (subdev
[0]->block_isbad
)
723 concat
->mtd
.block_isbad
= concat_block_isbad
;
724 if (subdev
[0]->block_markbad
)
725 concat
->mtd
.block_markbad
= concat_block_markbad
;
727 concat
->subdev
[0] = subdev
[0];
729 for (i
= 1; i
< num_devs
; i
++) {
730 if (concat
->mtd
.type
!= subdev
[i
]->type
) {
732 printk("Incompatible device type on \"%s\"\n",
736 if (concat
->mtd
.flags
!= subdev
[i
]->flags
) {
738 * Expect all flags except MTD_WRITEABLE to be
739 * equal on all subdevices.
741 if ((concat
->mtd
.flags
^ subdev
[i
]->
742 flags
) & ~MTD_WRITEABLE
) {
744 printk("Incompatible device flags on \"%s\"\n",
748 /* if writeable attribute differs,
749 make super device writeable */
751 subdev
[i
]->flags
& MTD_WRITEABLE
;
753 concat
->mtd
.size
+= subdev
[i
]->size
;
754 if (concat
->mtd
.writesize
!= subdev
[i
]->writesize
||
755 concat
->mtd
.oobsize
!= subdev
[i
]->oobsize
||
756 concat
->mtd
.ecctype
!= subdev
[i
]->ecctype
||
757 concat
->mtd
.eccsize
!= subdev
[i
]->eccsize
||
758 !concat
->mtd
.read_oob
!= !subdev
[i
]->read_oob
||
759 !concat
->mtd
.write_oob
!= !subdev
[i
]->write_oob
) {
761 printk("Incompatible OOB or ECC data on \"%s\"\n",
765 concat
->subdev
[i
] = subdev
[i
];
769 if(concat
->mtd
.type
== MTD_NANDFLASH
)
770 memcpy(&concat
->mtd
.oobinfo
, &subdev
[0]->oobinfo
,
771 sizeof(struct nand_oobinfo
));
773 concat
->num_subdev
= num_devs
;
774 concat
->mtd
.name
= name
;
776 concat
->mtd
.erase
= concat_erase
;
777 concat
->mtd
.read
= concat_read
;
778 concat
->mtd
.write
= concat_write
;
779 concat
->mtd
.sync
= concat_sync
;
780 concat
->mtd
.lock
= concat_lock
;
781 concat
->mtd
.unlock
= concat_unlock
;
782 concat
->mtd
.suspend
= concat_suspend
;
783 concat
->mtd
.resume
= concat_resume
;
786 * Combine the erase block size info of the subdevices:
788 * first, walk the map of the new device and see how
789 * many changes in erase size we have
791 max_erasesize
= curr_erasesize
= subdev
[0]->erasesize
;
792 num_erase_region
= 1;
793 for (i
= 0; i
< num_devs
; i
++) {
794 if (subdev
[i
]->numeraseregions
== 0) {
795 /* current subdevice has uniform erase size */
796 if (subdev
[i
]->erasesize
!= curr_erasesize
) {
797 /* if it differs from the last subdevice's erase size, count it */
799 curr_erasesize
= subdev
[i
]->erasesize
;
800 if (curr_erasesize
> max_erasesize
)
801 max_erasesize
= curr_erasesize
;
804 /* current subdevice has variable erase size */
806 for (j
= 0; j
< subdev
[i
]->numeraseregions
; j
++) {
808 /* walk the list of erase regions, count any changes */
809 if (subdev
[i
]->eraseregions
[j
].erasesize
!=
813 subdev
[i
]->eraseregions
[j
].
815 if (curr_erasesize
> max_erasesize
)
816 max_erasesize
= curr_erasesize
;
822 if (num_erase_region
== 1) {
824 * All subdevices have the same uniform erase size.
827 concat
->mtd
.erasesize
= curr_erasesize
;
828 concat
->mtd
.numeraseregions
= 0;
831 * erase block size varies across the subdevices: allocate
832 * space to store the data describing the variable erase regions
834 struct mtd_erase_region_info
*erase_region_p
;
835 u_int32_t begin
, position
;
837 concat
->mtd
.erasesize
= max_erasesize
;
838 concat
->mtd
.numeraseregions
= num_erase_region
;
839 concat
->mtd
.eraseregions
= erase_region_p
=
840 kmalloc(num_erase_region
*
841 sizeof (struct mtd_erase_region_info
), GFP_KERNEL
);
842 if (!erase_region_p
) {
845 ("memory allocation error while creating erase region list"
846 " for device \"%s\"\n", name
);
851 * walk the map of the new device once more and fill in
852 * in erase region info:
854 curr_erasesize
= subdev
[0]->erasesize
;
855 begin
= position
= 0;
856 for (i
= 0; i
< num_devs
; i
++) {
857 if (subdev
[i
]->numeraseregions
== 0) {
858 /* current subdevice has uniform erase size */
859 if (subdev
[i
]->erasesize
!= curr_erasesize
) {
861 * fill in an mtd_erase_region_info structure for the area
862 * we have walked so far:
864 erase_region_p
->offset
= begin
;
865 erase_region_p
->erasesize
=
867 erase_region_p
->numblocks
=
868 (position
- begin
) / curr_erasesize
;
871 curr_erasesize
= subdev
[i
]->erasesize
;
874 position
+= subdev
[i
]->size
;
876 /* current subdevice has variable erase size */
878 for (j
= 0; j
< subdev
[i
]->numeraseregions
; j
++) {
879 /* walk the list of erase regions, count any changes */
880 if (subdev
[i
]->eraseregions
[j
].
881 erasesize
!= curr_erasesize
) {
882 erase_region_p
->offset
= begin
;
883 erase_region_p
->erasesize
=
885 erase_region_p
->numblocks
=
887 begin
) / curr_erasesize
;
891 subdev
[i
]->eraseregions
[j
].
896 subdev
[i
]->eraseregions
[j
].
897 numblocks
* curr_erasesize
;
901 /* Now write the final entry */
902 erase_region_p
->offset
= begin
;
903 erase_region_p
->erasesize
= curr_erasesize
;
904 erase_region_p
->numblocks
= (position
- begin
) / curr_erasesize
;
911 * This function destroys an MTD object obtained from concat_mtd_devs()
914 void mtd_concat_destroy(struct mtd_info
*mtd
)
916 struct mtd_concat
*concat
= CONCAT(mtd
);
917 if (concat
->mtd
.numeraseregions
)
918 kfree(concat
->mtd
.eraseregions
);
922 EXPORT_SYMBOL(mtd_concat_create
);
923 EXPORT_SYMBOL(mtd_concat_destroy
);
925 MODULE_LICENSE("GPL");
926 MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
927 MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");