2 * Copyright (C) 2006-2008 Artem Bityutskiy
3 * Copyright (C) 2006-2008 Jarkko Lavinen
4 * Copyright (C) 2006-2008 Adrian Hunter
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; see the file COPYING. If not, write to the Free Software
17 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * Authors: Artem Bityutskiy, Jarkko Lavinen, Adria Hunter
21 * WARNING: this test program may kill your flash and your device. Do not
22 * use it unless you know what you do. Authors are not responsible for any
23 * damage caused by this program.
26 #include <linux/init.h>
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/err.h>
30 #include <linux/mtd/mtd.h>
31 #include <linux/sched.h>
33 #define PRINT_PREF KERN_INFO "mtd_torturetest: "
37 module_param(eb
, int, S_IRUGO
);
38 MODULE_PARM_DESC(eb
, "eraseblock number within the selected MTD device");
40 static int ebcnt
= 32;
41 module_param(ebcnt
, int, S_IRUGO
);
42 MODULE_PARM_DESC(ebcnt
, "number of consecutive eraseblocks to torture");
45 module_param(pgcnt
, int, S_IRUGO
);
46 MODULE_PARM_DESC(pgcnt
, "number of pages per eraseblock to torture (0 => all)");
49 module_param(dev
, int, S_IRUGO
);
50 MODULE_PARM_DESC(dev
, "MTD device number to use");
52 static int gran
= 512;
53 module_param(gran
, int, S_IRUGO
);
54 MODULE_PARM_DESC(gran
, "how often the status information should be printed");
57 module_param(check
, int, S_IRUGO
);
58 MODULE_PARM_DESC(check
, "if the written data should be checked");
60 static unsigned int cycles_count
;
61 module_param(cycles_count
, uint
, S_IRUGO
);
62 MODULE_PARM_DESC(cycles_count
, "how many erase cycles to do "
63 "(infinite by default)");
65 static struct mtd_info
*mtd
;
67 /* This buffer contains 0x555555...0xAAAAAA... pattern */
68 static unsigned char *patt_5A5
;
69 /* This buffer contains 0xAAAAAA...0x555555... pattern */
70 static unsigned char *patt_A5A
;
71 /* This buffer contains all 0xFF bytes */
72 static unsigned char *patt_FF
;
73 /* This a temporary buffer is use when checking data */
74 static unsigned char *check_buf
;
75 /* How many erase cycles were done */
76 static unsigned int erase_cycles
;
79 static struct timeval start
, finish
;
81 static void report_corrupt(unsigned char *read
, unsigned char *written
);
83 static inline void start_timing(void)
85 do_gettimeofday(&start
);
88 static inline void stop_timing(void)
90 do_gettimeofday(&finish
);
94 * Erase eraseblock number @ebnum.
96 static inline int erase_eraseblock(int ebnum
)
100 loff_t addr
= ebnum
* mtd
->erasesize
;
102 memset(&ei
, 0, sizeof(struct erase_info
));
105 ei
.len
= mtd
->erasesize
;
107 err
= mtd
->erase(mtd
, &ei
);
109 printk(PRINT_PREF
"error %d while erasing EB %d\n", err
, ebnum
);
113 if (ei
.state
== MTD_ERASE_FAILED
) {
114 printk(PRINT_PREF
"some erase error occurred at EB %d\n",
123 * Check that the contents of eraseblock number @enbum is equivalent to the
126 static inline int check_eraseblock(int ebnum
, unsigned char *buf
)
128 int err
, retries
= 0;
130 loff_t addr
= ebnum
* mtd
->erasesize
;
131 size_t len
= mtd
->erasesize
;
134 addr
= (ebnum
+ 1) * mtd
->erasesize
- pgcnt
* pgsize
;
135 len
= pgcnt
* pgsize
;
139 err
= mtd
->read(mtd
, addr
, len
, &read
, check_buf
);
141 printk(PRINT_PREF
"single bit flip occurred at EB %d "
142 "MTD reported that it was fixed.\n", ebnum
);
144 printk(PRINT_PREF
"error %d while reading EB %d, "
145 "read %zd\n", err
, ebnum
, read
);
150 printk(PRINT_PREF
"failed to read %zd bytes from EB %d, "
151 "read only %zd, but no error reported\n",
156 if (memcmp(buf
, check_buf
, len
)) {
157 printk(PRINT_PREF
"read wrong data from EB %d\n", ebnum
);
158 report_corrupt(check_buf
, buf
);
160 if (retries
++ < RETRIES
) {
163 printk(PRINT_PREF
"re-try reading data from EB %d\n",
167 printk(PRINT_PREF
"retried %d times, still errors, "
168 "give-up\n", RETRIES
);
174 printk(PRINT_PREF
"only attempt number %d was OK (!!!)\n",
180 static inline int write_pattern(int ebnum
, void *buf
)
184 loff_t addr
= ebnum
* mtd
->erasesize
;
185 size_t len
= mtd
->erasesize
;
188 addr
= (ebnum
+ 1) * mtd
->erasesize
- pgcnt
* pgsize
;
189 len
= pgcnt
* pgsize
;
191 err
= mtd
->write(mtd
, addr
, len
, &written
, buf
);
193 printk(PRINT_PREF
"error %d while writing EB %d, written %zd"
194 " bytes\n", err
, ebnum
, written
);
197 if (written
!= len
) {
198 printk(PRINT_PREF
"written only %zd bytes of %zd, but no error"
199 " reported\n", written
, len
);
206 static int __init
tort_init(void)
208 int err
= 0, i
, infinite
= !cycles_count
;
211 printk(KERN_INFO
"\n");
212 printk(KERN_INFO
"=================================================\n");
213 printk(PRINT_PREF
"Warning: this program is trying to wear out your "
214 "flash, stop it if this is not wanted.\n");
215 printk(PRINT_PREF
"MTD device: %d\n", dev
);
216 printk(PRINT_PREF
"torture %d eraseblocks (%d-%d) of mtd%d\n",
217 ebcnt
, eb
, eb
+ ebcnt
- 1, dev
);
219 printk(PRINT_PREF
"torturing just %d pages per eraseblock\n",
221 printk(PRINT_PREF
"write verify %s\n", check
? "enabled" : "disabled");
223 mtd
= get_mtd_device(NULL
, dev
);
226 printk(PRINT_PREF
"error: cannot get MTD device\n");
230 if (mtd
->writesize
== 1) {
231 printk(PRINT_PREF
"not NAND flash, assume page size is 512 "
235 pgsize
= mtd
->writesize
;
237 if (pgcnt
&& (pgcnt
> mtd
->erasesize
/ pgsize
|| pgcnt
< 0)) {
238 printk(PRINT_PREF
"error: invalid pgcnt value %d\n", pgcnt
);
243 patt_5A5
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
245 printk(PRINT_PREF
"error: cannot allocate memory\n");
249 patt_A5A
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
251 printk(PRINT_PREF
"error: cannot allocate memory\n");
255 patt_FF
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
257 printk(PRINT_PREF
"error: cannot allocate memory\n");
261 check_buf
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
263 printk(PRINT_PREF
"error: cannot allocate memory\n");
269 /* Initialize patterns */
270 memset(patt_FF
, 0xFF, mtd
->erasesize
);
271 for (i
= 0; i
< mtd
->erasesize
/ pgsize
; i
++) {
273 memset(patt_5A5
+ i
* pgsize
, 0x55, pgsize
);
274 memset(patt_A5A
+ i
* pgsize
, 0xAA, pgsize
);
276 memset(patt_5A5
+ i
* pgsize
, 0xAA, pgsize
);
277 memset(patt_A5A
+ i
* pgsize
, 0x55, pgsize
);
282 * Check if there is a bad eraseblock among those we are going to test.
284 memset(&bad_ebs
[0], 0, sizeof(int) * ebcnt
);
285 if (mtd
->block_isbad
) {
286 for (i
= eb
; i
< eb
+ ebcnt
; i
++) {
287 err
= mtd
->block_isbad(mtd
,
288 (loff_t
)i
* mtd
->erasesize
);
291 printk(PRINT_PREF
"block_isbad() returned %d "
292 "for EB %d\n", err
, i
);
297 printk("EB %d is bad. Skip it.\n", i
);
308 /* Erase all eraseblocks */
309 for (i
= eb
; i
< eb
+ ebcnt
; i
++) {
312 err
= erase_eraseblock(i
);
318 /* Check if the eraseblocks contain only 0xFF bytes */
320 for (i
= eb
; i
< eb
+ ebcnt
; i
++) {
323 err
= check_eraseblock(i
, patt_FF
);
325 printk(PRINT_PREF
"verify failed"
326 " for 0xFF... pattern\n");
333 /* Write the pattern */
334 for (i
= eb
; i
< eb
+ ebcnt
; i
++) {
337 if ((eb
+ erase_cycles
) & 1)
341 err
= write_pattern(i
, patt
);
347 /* Verify what we wrote */
349 for (i
= eb
; i
< eb
+ ebcnt
; i
++) {
352 if ((eb
+ erase_cycles
) & 1)
356 err
= check_eraseblock(i
, patt
);
358 printk(PRINT_PREF
"verify failed for %s"
360 ((eb
+ erase_cycles
) & 1) ?
361 "0x55AA55..." : "0xAA55AA...");
370 if (erase_cycles
% gran
== 0) {
374 ms
= (finish
.tv_sec
- start
.tv_sec
) * 1000 +
375 (finish
.tv_usec
- start
.tv_usec
) / 1000;
376 printk(PRINT_PREF
"%08u erase cycles done, took %lu "
377 "milliseconds (%lu seconds)\n",
378 erase_cycles
, ms
, ms
/ 1000);
382 if (!infinite
&& --cycles_count
== 0)
387 printk(PRINT_PREF
"finished after %u erase cycles\n",
399 printk(PRINT_PREF
"error %d occurred during torturing\n", err
);
400 printk(KERN_INFO
"=================================================\n");
403 module_init(tort_init
);
405 static void __exit
tort_exit(void)
409 module_exit(tort_exit
);
411 static int countdiffs(unsigned char *buf
, unsigned char *check_buf
,
412 unsigned offset
, unsigned len
, unsigned *bytesp
,
414 static void print_bufs(unsigned char *read
, unsigned char *written
, int start
,
418 * Report the detailed information about how the read EB differs from what was
421 static void report_corrupt(unsigned char *read
, unsigned char *written
)
424 int bytes
, bits
, pages
, first
;
426 size_t check_len
= mtd
->erasesize
;
429 check_len
= pgcnt
* pgsize
;
431 bytes
= bits
= pages
= 0;
432 for (i
= 0; i
< check_len
; i
+= pgsize
)
433 if (countdiffs(written
, read
, i
, pgsize
, &bytes
,
437 printk(PRINT_PREF
"verify fails on %d pages, %d bytes/%d bits\n",
439 printk(PRINT_PREF
"The following is a list of all differences between"
440 " what was read from flash and what was expected\n");
442 for (i
= 0; i
< check_len
; i
+= pgsize
) {
445 first
= countdiffs(written
, read
, i
, pgsize
, &bytes
,
450 printk("-------------------------------------------------------"
451 "----------------------------------\n");
453 printk(PRINT_PREF
"Page %zd has %d bytes/%d bits failing verify,"
454 " starting at offset 0x%x\n",
455 (mtd
->erasesize
- check_len
+ i
) / pgsize
,
458 offset
= first
& ~0x7;
459 len
= ((first
+ bytes
) | 0x7) + 1 - offset
;
461 print_bufs(read
, written
, offset
, len
);
465 static void print_bufs(unsigned char *read
, unsigned char *written
, int start
,
471 printk("Offset Read Written\n");
473 printk("0x%08x: ", start
+ i
);
475 for (j1
= 0; j1
< 8 && i
+ j1
< len
; j1
++) {
476 printk(" %02x", read
[start
+ i
+ j1
]);
477 if (read
[start
+ i
+ j1
] != written
[start
+ i
+ j1
])
486 printk(" %s ", diff
);
488 for (j2
= 0; j2
< 8 && i
+ j2
< len
; j2
++)
489 printk(" %02x", written
[start
+ i
+ j2
]);
496 * Count the number of differing bytes and bits and return the first differing
499 static int countdiffs(unsigned char *buf
, unsigned char *check_buf
,
500 unsigned offset
, unsigned len
, unsigned *bytesp
,
506 for (i
= offset
; i
< offset
+ len
; i
++)
507 if (buf
[i
] != check_buf
[i
]) {
512 while (i
< offset
+ len
) {
513 if (buf
[i
] != check_buf
[i
]) {
517 if ((buf
[i
] & bit
) != (check_buf
[i
] & bit
))
528 MODULE_DESCRIPTION("Eraseblock torturing module");
529 MODULE_AUTHOR("Artem Bityutskiy, Jarkko Lavinen, Adrian Hunter");
530 MODULE_LICENSE("GPL");