2 * Copyright (C) 2006-2008 Nokia Corporation
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published by
6 * the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; see the file COPYING. If not, write to the Free Software
15 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 * Test OOB read and write on MTD device.
19 * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <asm/div64.h>
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/err.h>
29 #include <linux/mtd/mtd.h>
30 #include <linux/slab.h>
31 #include <linux/sched.h>
32 #include <linux/random.h>
36 static int dev
= -EINVAL
;
37 static int bitflip_limit
;
38 module_param(dev
, int, S_IRUGO
);
39 MODULE_PARM_DESC(dev
, "MTD device number to use");
40 module_param(bitflip_limit
, int, S_IRUGO
);
41 MODULE_PARM_DESC(bitflip_limit
, "Max. allowed bitflips per page");
43 static struct mtd_info
*mtd
;
44 static unsigned char *readbuf
;
45 static unsigned char *writebuf
;
46 static unsigned char *bbt
;
51 static int use_offset
;
53 static int use_len_max
;
54 static int vary_offset
;
55 static struct rnd_state rnd_state
;
57 static void do_vary_offset(void)
62 if (use_offset
>= use_len_max
)
64 use_len
= use_len_max
- use_offset
;
68 static int write_eraseblock(int ebnum
)
71 struct mtd_oob_ops ops
;
73 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
75 prandom_bytes_state(&rnd_state
, writebuf
, use_len_max
* pgcnt
);
76 for (i
= 0; i
< pgcnt
; ++i
, addr
+= mtd
->writesize
) {
77 ops
.mode
= MTD_OPS_AUTO_OOB
;
82 ops
.ooboffs
= use_offset
;
84 ops
.oobbuf
= writebuf
+ (use_len_max
* i
) + use_offset
;
85 err
= mtd_write_oob(mtd
, addr
, &ops
);
86 if (err
|| ops
.oobretlen
!= use_len
) {
87 pr_err("error: writeoob failed at %#llx\n",
89 pr_err("error: use_len %d, use_offset %d\n",
92 return err
? err
: -1;
101 static int write_whole_device(void)
106 pr_info("writing OOBs of whole device\n");
107 for (i
= 0; i
< ebcnt
; ++i
) {
110 err
= write_eraseblock(i
);
114 pr_info("written up to eraseblock %u\n", i
);
116 err
= mtdtest_relax();
120 pr_info("written %u eraseblocks\n", i
);
125 * Display the address, offset and data bytes at comparison failure.
126 * Return number of bitflips encountered.
128 static size_t memcmpshowoffset(loff_t addr
, loff_t offset
, const void *cs
,
129 const void *ct
, size_t count
)
131 const unsigned char *su1
, *su2
;
136 for (su1
= cs
, su2
= ct
; 0 < count
; ++su1
, ++su2
, count
--, i
++) {
139 pr_info("error @addr[0x%lx:0x%lx] 0x%x -> 0x%x diff 0x%x\n",
140 (unsigned long)addr
, (unsigned long)offset
+ i
,
142 bitflips
+= hweight8(res
);
149 #define memcmpshow(addr, cs, ct, count) memcmpshowoffset((addr), 0, (cs), (ct),\
153 * Compare with 0xff and show the address, offset and data bytes at
154 * comparison failure. Return number of bitflips encountered.
156 static size_t memffshow(loff_t addr
, loff_t offset
, const void *cs
,
159 const unsigned char *su1
;
164 for (su1
= cs
; 0 < count
; ++su1
, count
--, i
++) {
167 pr_info("error @addr[0x%lx:0x%lx] 0x%x -> 0xff diff 0x%x\n",
168 (unsigned long)addr
, (unsigned long)offset
+ i
,
170 bitflips
+= hweight8(res
);
177 static int verify_eraseblock(int ebnum
)
180 struct mtd_oob_ops ops
;
182 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
185 prandom_bytes_state(&rnd_state
, writebuf
, use_len_max
* pgcnt
);
186 for (i
= 0; i
< pgcnt
; ++i
, addr
+= mtd
->writesize
) {
187 ops
.mode
= MTD_OPS_AUTO_OOB
;
190 ops
.ooblen
= use_len
;
192 ops
.ooboffs
= use_offset
;
194 ops
.oobbuf
= readbuf
;
195 err
= mtd_read_oob(mtd
, addr
, &ops
);
196 if (mtd_is_bitflip(err
))
199 if (err
|| ops
.oobretlen
!= use_len
) {
200 pr_err("error: readoob failed at %#llx\n",
203 return err
? err
: -1;
206 bitflips
= memcmpshow(addr
, readbuf
,
207 writebuf
+ (use_len_max
* i
) + use_offset
,
209 if (bitflips
> bitflip_limit
) {
210 pr_err("error: verify failed at %#llx\n",
214 pr_err("error: too many errors\n");
217 } else if (bitflips
) {
218 pr_info("ignoring error as within bitflip_limit\n");
221 if (use_offset
!= 0 || use_len
< mtd
->oobavail
) {
224 ops
.mode
= MTD_OPS_AUTO_OOB
;
227 ops
.ooblen
= mtd
->oobavail
;
231 ops
.oobbuf
= readbuf
;
232 err
= mtd_read_oob(mtd
, addr
, &ops
);
233 if (mtd_is_bitflip(err
))
236 if (err
|| ops
.oobretlen
!= mtd
->oobavail
) {
237 pr_err("error: readoob failed at %#llx\n",
240 return err
? err
: -1;
242 bitflips
= memcmpshowoffset(addr
, use_offset
,
243 readbuf
+ use_offset
,
244 writebuf
+ (use_len_max
* i
) + use_offset
,
247 /* verify pre-offset area for 0xff */
248 bitflips
+= memffshow(addr
, 0, readbuf
, use_offset
);
250 /* verify post-(use_offset + use_len) area for 0xff */
251 k
= use_offset
+ use_len
;
252 bitflips
+= memffshow(addr
, k
, readbuf
+ k
,
255 if (bitflips
> bitflip_limit
) {
256 pr_err("error: verify failed at %#llx\n",
260 pr_err("error: too many errors\n");
263 } else if (bitflips
) {
264 pr_info("ignoring errors as within bitflip limit\n");
273 static int verify_eraseblock_in_one_go(int ebnum
)
275 struct mtd_oob_ops ops
;
277 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
278 size_t len
= mtd
->oobavail
* pgcnt
;
279 size_t oobavail
= mtd
->oobavail
;
283 prandom_bytes_state(&rnd_state
, writebuf
, len
);
284 ops
.mode
= MTD_OPS_AUTO_OOB
;
291 ops
.oobbuf
= readbuf
;
293 /* read entire block's OOB at one go */
294 err
= mtd_read_oob(mtd
, addr
, &ops
);
295 if (mtd_is_bitflip(err
))
298 if (err
|| ops
.oobretlen
!= len
) {
299 pr_err("error: readoob failed at %#llx\n",
302 return err
? err
: -1;
305 /* verify one page OOB at a time for bitflip per page limit check */
306 for (i
= 0; i
< pgcnt
; ++i
, addr
+= mtd
->writesize
) {
307 bitflips
= memcmpshow(addr
, readbuf
+ (i
* oobavail
),
308 writebuf
+ (i
* oobavail
), oobavail
);
309 if (bitflips
> bitflip_limit
) {
310 pr_err("error: verify failed at %#llx\n",
314 pr_err("error: too many errors\n");
317 } else if (bitflips
) {
318 pr_info("ignoring error as within bitflip_limit\n");
325 static int verify_all_eraseblocks(void)
330 pr_info("verifying all eraseblocks\n");
331 for (i
= 0; i
< ebcnt
; ++i
) {
334 err
= verify_eraseblock(i
);
338 pr_info("verified up to eraseblock %u\n", i
);
340 err
= mtdtest_relax();
344 pr_info("verified %u eraseblocks\n", i
);
348 static int __init
mtd_oobtest_init(void)
353 struct mtd_oob_ops ops
;
354 loff_t addr
= 0, addr0
;
356 printk(KERN_INFO
"\n");
357 printk(KERN_INFO
"=================================================\n");
360 pr_info("Please specify a valid mtd-device via module parameter\n");
361 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
365 pr_info("MTD device: %d\n", dev
);
367 mtd
= get_mtd_device(NULL
, dev
);
370 pr_err("error: cannot get MTD device\n");
374 if (!mtd_type_is_nand(mtd
)) {
375 pr_info("this test requires NAND flash\n");
380 do_div(tmp
, mtd
->erasesize
);
382 pgcnt
= mtd
->erasesize
/ mtd
->writesize
;
384 pr_info("MTD device size %llu, eraseblock size %u, "
385 "page size %u, count of eraseblocks %u, pages per "
386 "eraseblock %u, OOB size %u\n",
387 (unsigned long long)mtd
->size
, mtd
->erasesize
,
388 mtd
->writesize
, ebcnt
, pgcnt
, mtd
->oobsize
);
391 readbuf
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
394 writebuf
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
397 bbt
= kzalloc(ebcnt
, GFP_KERNEL
);
401 err
= mtdtest_scan_for_bad_eraseblocks(mtd
, bbt
, 0, ebcnt
);
406 use_len
= mtd
->oobavail
;
407 use_len_max
= mtd
->oobavail
;
410 /* First test: write all OOB, read it back and verify */
411 pr_info("test 1 of 5\n");
413 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
417 prandom_seed_state(&rnd_state
, 1);
418 err
= write_whole_device();
422 prandom_seed_state(&rnd_state
, 1);
423 err
= verify_all_eraseblocks();
428 * Second test: write all OOB, a block at a time, read it back and
431 pr_info("test 2 of 5\n");
433 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
437 prandom_seed_state(&rnd_state
, 3);
438 err
= write_whole_device();
442 /* Check all eraseblocks */
443 prandom_seed_state(&rnd_state
, 3);
444 pr_info("verifying all eraseblocks\n");
445 for (i
= 0; i
< ebcnt
; ++i
) {
448 err
= verify_eraseblock_in_one_go(i
);
452 pr_info("verified up to eraseblock %u\n", i
);
454 err
= mtdtest_relax();
458 pr_info("verified %u eraseblocks\n", i
);
461 * Third test: write OOB at varying offsets and lengths, read it back
464 pr_info("test 3 of 5\n");
466 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
470 /* Write all eraseblocks */
472 use_len
= mtd
->oobavail
;
473 use_len_max
= mtd
->oobavail
;
475 prandom_seed_state(&rnd_state
, 5);
477 err
= write_whole_device();
481 /* Check all eraseblocks */
483 use_len
= mtd
->oobavail
;
484 use_len_max
= mtd
->oobavail
;
486 prandom_seed_state(&rnd_state
, 5);
487 err
= verify_all_eraseblocks();
492 use_len
= mtd
->oobavail
;
493 use_len_max
= mtd
->oobavail
;
496 /* Fourth test: try to write off end of device */
497 pr_info("test 4 of 5\n");
499 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
504 for (i
= 0; i
< ebcnt
&& bbt
[i
]; ++i
)
505 addr0
+= mtd
->erasesize
;
507 /* Attempt to write off end of OOB */
508 ops
.mode
= MTD_OPS_AUTO_OOB
;
513 ops
.ooboffs
= mtd
->oobavail
;
515 ops
.oobbuf
= writebuf
;
516 pr_info("attempting to start write past end of OOB\n");
517 pr_info("an error is expected...\n");
518 err
= mtd_write_oob(mtd
, addr0
, &ops
);
520 pr_info("error occurred as expected\n");
523 pr_err("error: can write past end of OOB\n");
527 /* Attempt to read off end of OOB */
528 ops
.mode
= MTD_OPS_AUTO_OOB
;
533 ops
.ooboffs
= mtd
->oobavail
;
535 ops
.oobbuf
= readbuf
;
536 pr_info("attempting to start read past end of OOB\n");
537 pr_info("an error is expected...\n");
538 err
= mtd_read_oob(mtd
, addr0
, &ops
);
539 if (mtd_is_bitflip(err
))
543 pr_info("error occurred as expected\n");
546 pr_err("error: can read past end of OOB\n");
551 pr_info("skipping end of device tests because last "
554 /* Attempt to write off end of device */
555 ops
.mode
= MTD_OPS_AUTO_OOB
;
558 ops
.ooblen
= mtd
->oobavail
+ 1;
562 ops
.oobbuf
= writebuf
;
563 pr_info("attempting to write past end of device\n");
564 pr_info("an error is expected...\n");
565 err
= mtd_write_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
567 pr_info("error occurred as expected\n");
570 pr_err("error: wrote past end of device\n");
574 /* Attempt to read off end of device */
575 ops
.mode
= MTD_OPS_AUTO_OOB
;
578 ops
.ooblen
= mtd
->oobavail
+ 1;
582 ops
.oobbuf
= readbuf
;
583 pr_info("attempting to read past end of device\n");
584 pr_info("an error is expected...\n");
585 err
= mtd_read_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
586 if (mtd_is_bitflip(err
))
590 pr_info("error occurred as expected\n");
593 pr_err("error: read past end of device\n");
597 err
= mtdtest_erase_eraseblock(mtd
, ebcnt
- 1);
601 /* Attempt to write off end of device */
602 ops
.mode
= MTD_OPS_AUTO_OOB
;
605 ops
.ooblen
= mtd
->oobavail
;
609 ops
.oobbuf
= writebuf
;
610 pr_info("attempting to write past end of device\n");
611 pr_info("an error is expected...\n");
612 err
= mtd_write_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
614 pr_info("error occurred as expected\n");
617 pr_err("error: wrote past end of device\n");
621 /* Attempt to read off end of device */
622 ops
.mode
= MTD_OPS_AUTO_OOB
;
625 ops
.ooblen
= mtd
->oobavail
;
629 ops
.oobbuf
= readbuf
;
630 pr_info("attempting to read past end of device\n");
631 pr_info("an error is expected...\n");
632 err
= mtd_read_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
633 if (mtd_is_bitflip(err
))
637 pr_info("error occurred as expected\n");
640 pr_err("error: read past end of device\n");
645 /* Fifth test: write / read across block boundaries */
646 pr_info("test 5 of 5\n");
648 /* Erase all eraseblocks */
649 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
653 /* Write all eraseblocks */
654 prandom_seed_state(&rnd_state
, 11);
655 pr_info("writing OOBs of whole device\n");
656 for (i
= 0; i
< ebcnt
- 1; ++i
) {
659 size_t sz
= mtd
->oobavail
;
660 if (bbt
[i
] || bbt
[i
+ 1])
662 addr
= (loff_t
)(i
+ 1) * mtd
->erasesize
- mtd
->writesize
;
663 prandom_bytes_state(&rnd_state
, writebuf
, sz
* cnt
);
664 for (pg
= 0; pg
< cnt
; ++pg
) {
665 ops
.mode
= MTD_OPS_AUTO_OOB
;
672 ops
.oobbuf
= writebuf
+ pg
* sz
;
673 err
= mtd_write_oob(mtd
, addr
, &ops
);
677 pr_info("written up to eraseblock %u\n", i
);
679 err
= mtdtest_relax();
683 addr
+= mtd
->writesize
;
686 pr_info("written %u eraseblocks\n", i
);
688 /* Check all eraseblocks */
689 prandom_seed_state(&rnd_state
, 11);
690 pr_info("verifying all eraseblocks\n");
691 for (i
= 0; i
< ebcnt
- 1; ++i
) {
692 if (bbt
[i
] || bbt
[i
+ 1])
694 prandom_bytes_state(&rnd_state
, writebuf
, mtd
->oobavail
* 2);
695 addr
= (loff_t
)(i
+ 1) * mtd
->erasesize
- mtd
->writesize
;
696 ops
.mode
= MTD_OPS_AUTO_OOB
;
699 ops
.ooblen
= mtd
->oobavail
* 2;
703 ops
.oobbuf
= readbuf
;
704 err
= mtd_read_oob(mtd
, addr
, &ops
);
705 if (mtd_is_bitflip(err
))
710 if (memcmpshow(addr
, readbuf
, writebuf
,
711 mtd
->oobavail
* 2)) {
712 pr_err("error: verify failed at %#llx\n",
716 pr_err("error: too many errors\n");
721 pr_info("verified up to eraseblock %u\n", i
);
723 err
= mtdtest_relax();
727 pr_info("verified %u eraseblocks\n", i
);
729 pr_info("finished with %d errors\n", errcnt
);
736 pr_info("error %d occurred\n", err
);
737 printk(KERN_INFO
"=================================================\n");
740 module_init(mtd_oobtest_init
);
742 static void __exit
mtd_oobtest_exit(void)
746 module_exit(mtd_oobtest_exit
);
748 MODULE_DESCRIPTION("Out-of-band test module");
749 MODULE_AUTHOR("Adrian Hunter");
750 MODULE_LICENSE("GPL");