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 memcmpshow(loff_t addr
, const void *cs
, const void *ct
, size_t count
)
130 const unsigned char *su1
, *su2
;
135 for (su1
= cs
, su2
= ct
; 0 < count
; ++su1
, ++su2
, count
--, i
++) {
138 pr_info("error @addr[0x%lx:0x%zx] 0x%x -> 0x%x diff 0x%x\n",
139 (unsigned long)addr
, i
, *su1
, *su2
, res
);
140 bitflips
+= hweight8(res
);
148 * Compare with 0xff and show the address, offset and data bytes at
149 * comparison failure. Return number of bitflips encountered.
151 static size_t memffshow(loff_t addr
, loff_t offset
, const void *cs
,
154 const unsigned char *su1
;
159 for (su1
= cs
; 0 < count
; ++su1
, count
--, i
++) {
162 pr_info("error @addr[0x%lx:0x%lx] 0x%x -> 0xff diff 0x%x\n",
163 (unsigned long)addr
, (unsigned long)offset
+ i
,
165 bitflips
+= hweight8(res
);
172 static int verify_eraseblock(int ebnum
)
175 struct mtd_oob_ops ops
;
177 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
180 prandom_bytes_state(&rnd_state
, writebuf
, use_len_max
* pgcnt
);
181 for (i
= 0; i
< pgcnt
; ++i
, addr
+= mtd
->writesize
) {
182 ops
.mode
= MTD_OPS_AUTO_OOB
;
185 ops
.ooblen
= use_len
;
187 ops
.ooboffs
= use_offset
;
189 ops
.oobbuf
= readbuf
;
190 err
= mtd_read_oob(mtd
, addr
, &ops
);
191 if (err
|| ops
.oobretlen
!= use_len
) {
192 pr_err("error: readoob failed at %#llx\n",
195 return err
? err
: -1;
198 bitflips
= memcmpshow(addr
, readbuf
,
199 writebuf
+ (use_len_max
* i
) + use_offset
,
201 if (bitflips
> bitflip_limit
) {
202 pr_err("error: verify failed at %#llx\n",
206 pr_err("error: too many errors\n");
209 } else if (bitflips
) {
210 pr_info("ignoring error as within bitflip_limit\n");
213 if (use_offset
!= 0 || use_len
< mtd
->ecclayout
->oobavail
) {
216 ops
.mode
= MTD_OPS_AUTO_OOB
;
219 ops
.ooblen
= mtd
->ecclayout
->oobavail
;
223 ops
.oobbuf
= readbuf
;
224 err
= mtd_read_oob(mtd
, addr
, &ops
);
225 if (err
|| ops
.oobretlen
!= mtd
->ecclayout
->oobavail
) {
226 pr_err("error: readoob failed at %#llx\n",
229 return err
? err
: -1;
231 bitflips
= memcmpshow(addr
, readbuf
+ use_offset
,
232 writebuf
+ (use_len_max
* i
) + use_offset
,
235 /* verify pre-offset area for 0xff */
236 bitflips
+= memffshow(addr
, 0, readbuf
, use_offset
);
238 /* verify post-(use_offset + use_len) area for 0xff */
239 k
= use_offset
+ use_len
;
240 bitflips
+= memffshow(addr
, k
, readbuf
+ k
,
241 mtd
->ecclayout
->oobavail
- k
);
243 if (bitflips
> bitflip_limit
) {
244 pr_err("error: verify failed at %#llx\n",
248 pr_err("error: too many errors\n");
251 } else if (bitflips
) {
252 pr_info("ignoring errors as within bitflip limit\n");
261 static int verify_eraseblock_in_one_go(int ebnum
)
263 struct mtd_oob_ops ops
;
265 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
266 size_t len
= mtd
->ecclayout
->oobavail
* pgcnt
;
267 size_t oobavail
= mtd
->ecclayout
->oobavail
;
271 prandom_bytes_state(&rnd_state
, writebuf
, len
);
272 ops
.mode
= MTD_OPS_AUTO_OOB
;
279 ops
.oobbuf
= readbuf
;
281 /* read entire block's OOB at one go */
282 err
= mtd_read_oob(mtd
, addr
, &ops
);
283 if (err
|| ops
.oobretlen
!= len
) {
284 pr_err("error: readoob failed at %#llx\n",
287 return err
? err
: -1;
290 /* verify one page OOB at a time for bitflip per page limit check */
291 for (i
= 0; i
< pgcnt
; ++i
, addr
+= mtd
->writesize
) {
292 bitflips
= memcmpshow(addr
, readbuf
+ (i
* oobavail
),
293 writebuf
+ (i
* oobavail
), oobavail
);
294 if (bitflips
> bitflip_limit
) {
295 pr_err("error: verify failed at %#llx\n",
299 pr_err("error: too many errors\n");
302 } else if (bitflips
) {
303 pr_info("ignoring error as within bitflip_limit\n");
310 static int verify_all_eraseblocks(void)
315 pr_info("verifying all eraseblocks\n");
316 for (i
= 0; i
< ebcnt
; ++i
) {
319 err
= verify_eraseblock(i
);
323 pr_info("verified up to eraseblock %u\n", i
);
325 err
= mtdtest_relax();
329 pr_info("verified %u eraseblocks\n", i
);
333 static int __init
mtd_oobtest_init(void)
338 struct mtd_oob_ops ops
;
339 loff_t addr
= 0, addr0
;
341 printk(KERN_INFO
"\n");
342 printk(KERN_INFO
"=================================================\n");
345 pr_info("Please specify a valid mtd-device via module parameter\n");
346 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
350 pr_info("MTD device: %d\n", dev
);
352 mtd
= get_mtd_device(NULL
, dev
);
355 pr_err("error: cannot get MTD device\n");
359 if (!mtd_type_is_nand(mtd
)) {
360 pr_info("this test requires NAND flash\n");
365 do_div(tmp
, mtd
->erasesize
);
367 pgcnt
= mtd
->erasesize
/ mtd
->writesize
;
369 pr_info("MTD device size %llu, eraseblock size %u, "
370 "page size %u, count of eraseblocks %u, pages per "
371 "eraseblock %u, OOB size %u\n",
372 (unsigned long long)mtd
->size
, mtd
->erasesize
,
373 mtd
->writesize
, ebcnt
, pgcnt
, mtd
->oobsize
);
376 readbuf
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
379 writebuf
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
382 bbt
= kzalloc(ebcnt
, GFP_KERNEL
);
386 err
= mtdtest_scan_for_bad_eraseblocks(mtd
, bbt
, 0, ebcnt
);
391 use_len
= mtd
->ecclayout
->oobavail
;
392 use_len_max
= mtd
->ecclayout
->oobavail
;
395 /* First test: write all OOB, read it back and verify */
396 pr_info("test 1 of 5\n");
398 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
402 prandom_seed_state(&rnd_state
, 1);
403 err
= write_whole_device();
407 prandom_seed_state(&rnd_state
, 1);
408 err
= verify_all_eraseblocks();
413 * Second test: write all OOB, a block at a time, read it back and
416 pr_info("test 2 of 5\n");
418 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
422 prandom_seed_state(&rnd_state
, 3);
423 err
= write_whole_device();
427 /* Check all eraseblocks */
428 prandom_seed_state(&rnd_state
, 3);
429 pr_info("verifying all eraseblocks\n");
430 for (i
= 0; i
< ebcnt
; ++i
) {
433 err
= verify_eraseblock_in_one_go(i
);
437 pr_info("verified up to eraseblock %u\n", i
);
439 err
= mtdtest_relax();
443 pr_info("verified %u eraseblocks\n", i
);
446 * Third test: write OOB at varying offsets and lengths, read it back
449 pr_info("test 3 of 5\n");
451 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
455 /* Write all eraseblocks */
457 use_len
= mtd
->ecclayout
->oobavail
;
458 use_len_max
= mtd
->ecclayout
->oobavail
;
460 prandom_seed_state(&rnd_state
, 5);
462 err
= write_whole_device();
466 /* Check all eraseblocks */
468 use_len
= mtd
->ecclayout
->oobavail
;
469 use_len_max
= mtd
->ecclayout
->oobavail
;
471 prandom_seed_state(&rnd_state
, 5);
472 err
= verify_all_eraseblocks();
477 use_len
= mtd
->ecclayout
->oobavail
;
478 use_len_max
= mtd
->ecclayout
->oobavail
;
481 /* Fourth test: try to write off end of device */
482 pr_info("test 4 of 5\n");
484 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
489 for (i
= 0; i
< ebcnt
&& bbt
[i
]; ++i
)
490 addr0
+= mtd
->erasesize
;
492 /* Attempt to write off end of OOB */
493 ops
.mode
= MTD_OPS_AUTO_OOB
;
498 ops
.ooboffs
= mtd
->ecclayout
->oobavail
;
500 ops
.oobbuf
= writebuf
;
501 pr_info("attempting to start write past end of OOB\n");
502 pr_info("an error is expected...\n");
503 err
= mtd_write_oob(mtd
, addr0
, &ops
);
505 pr_info("error occurred as expected\n");
508 pr_err("error: can write past end of OOB\n");
512 /* Attempt to read off end of OOB */
513 ops
.mode
= MTD_OPS_AUTO_OOB
;
518 ops
.ooboffs
= mtd
->ecclayout
->oobavail
;
520 ops
.oobbuf
= readbuf
;
521 pr_info("attempting to start read past end of OOB\n");
522 pr_info("an error is expected...\n");
523 err
= mtd_read_oob(mtd
, addr0
, &ops
);
525 pr_info("error occurred as expected\n");
528 pr_err("error: can read past end of OOB\n");
533 pr_info("skipping end of device tests because last "
536 /* Attempt to write off end of device */
537 ops
.mode
= MTD_OPS_AUTO_OOB
;
540 ops
.ooblen
= mtd
->ecclayout
->oobavail
+ 1;
544 ops
.oobbuf
= writebuf
;
545 pr_info("attempting to write past end of device\n");
546 pr_info("an error is expected...\n");
547 err
= mtd_write_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
549 pr_info("error occurred as expected\n");
552 pr_err("error: wrote past end of device\n");
556 /* Attempt to read off end of device */
557 ops
.mode
= MTD_OPS_AUTO_OOB
;
560 ops
.ooblen
= mtd
->ecclayout
->oobavail
+ 1;
564 ops
.oobbuf
= readbuf
;
565 pr_info("attempting to read past end of device\n");
566 pr_info("an error is expected...\n");
567 err
= mtd_read_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
569 pr_info("error occurred as expected\n");
572 pr_err("error: read past end of device\n");
576 err
= mtdtest_erase_eraseblock(mtd
, ebcnt
- 1);
580 /* Attempt to write off end of device */
581 ops
.mode
= MTD_OPS_AUTO_OOB
;
584 ops
.ooblen
= mtd
->ecclayout
->oobavail
;
588 ops
.oobbuf
= writebuf
;
589 pr_info("attempting to write past end of device\n");
590 pr_info("an error is expected...\n");
591 err
= mtd_write_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
593 pr_info("error occurred as expected\n");
596 pr_err("error: wrote past end of device\n");
600 /* Attempt to read off end of device */
601 ops
.mode
= MTD_OPS_AUTO_OOB
;
604 ops
.ooblen
= mtd
->ecclayout
->oobavail
;
608 ops
.oobbuf
= readbuf
;
609 pr_info("attempting to read past end of device\n");
610 pr_info("an error is expected...\n");
611 err
= mtd_read_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
613 pr_info("error occurred as expected\n");
616 pr_err("error: read past end of device\n");
621 /* Fifth test: write / read across block boundaries */
622 pr_info("test 5 of 5\n");
624 /* Erase all eraseblocks */
625 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
629 /* Write all eraseblocks */
630 prandom_seed_state(&rnd_state
, 11);
631 pr_info("writing OOBs of whole device\n");
632 for (i
= 0; i
< ebcnt
- 1; ++i
) {
635 size_t sz
= mtd
->ecclayout
->oobavail
;
636 if (bbt
[i
] || bbt
[i
+ 1])
638 addr
= (loff_t
)(i
+ 1) * mtd
->erasesize
- mtd
->writesize
;
639 prandom_bytes_state(&rnd_state
, writebuf
, sz
* cnt
);
640 for (pg
= 0; pg
< cnt
; ++pg
) {
641 ops
.mode
= MTD_OPS_AUTO_OOB
;
648 ops
.oobbuf
= writebuf
+ pg
* sz
;
649 err
= mtd_write_oob(mtd
, addr
, &ops
);
653 pr_info("written up to eraseblock %u\n", i
);
655 err
= mtdtest_relax();
659 addr
+= mtd
->writesize
;
662 pr_info("written %u eraseblocks\n", i
);
664 /* Check all eraseblocks */
665 prandom_seed_state(&rnd_state
, 11);
666 pr_info("verifying all eraseblocks\n");
667 for (i
= 0; i
< ebcnt
- 1; ++i
) {
668 if (bbt
[i
] || bbt
[i
+ 1])
670 prandom_bytes_state(&rnd_state
, writebuf
,
671 mtd
->ecclayout
->oobavail
* 2);
672 addr
= (loff_t
)(i
+ 1) * mtd
->erasesize
- mtd
->writesize
;
673 ops
.mode
= MTD_OPS_AUTO_OOB
;
676 ops
.ooblen
= mtd
->ecclayout
->oobavail
* 2;
680 ops
.oobbuf
= readbuf
;
681 err
= mtd_read_oob(mtd
, addr
, &ops
);
684 if (memcmpshow(addr
, readbuf
, writebuf
,
685 mtd
->ecclayout
->oobavail
* 2)) {
686 pr_err("error: verify failed at %#llx\n",
690 pr_err("error: too many errors\n");
695 pr_info("verified up to eraseblock %u\n", i
);
697 err
= mtdtest_relax();
701 pr_info("verified %u eraseblocks\n", i
);
703 pr_info("finished with %d errors\n", errcnt
);
710 pr_info("error %d occurred\n", err
);
711 printk(KERN_INFO
"=================================================\n");
714 module_init(mtd_oobtest_init
);
716 static void __exit
mtd_oobtest_exit(void)
720 module_exit(mtd_oobtest_exit
);
722 MODULE_DESCRIPTION("Out-of-band test module");
723 MODULE_AUTHOR("Adrian Hunter");
724 MODULE_LICENSE("GPL");