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/slab.h>
32 #include <linux/sched.h>
34 #define PRINT_PREF KERN_INFO "mtd_torturetest: "
38 module_param(eb, int, S_IRUGO);
39 MODULE_PARM_DESC(eb, "eraseblock number within the selected MTD device");
41 static int ebcnt = 32;
42 module_param(ebcnt, int, S_IRUGO);
43 MODULE_PARM_DESC(ebcnt, "number of consecutive eraseblocks to torture");
46 module_param(pgcnt, int, S_IRUGO);
47 MODULE_PARM_DESC(pgcnt, "number of pages per eraseblock to torture (0 => all)");
49 static int dev = -EINVAL;
50 module_param(dev, int, S_IRUGO);
51 MODULE_PARM_DESC(dev, "MTD device number to use");
53 static int gran = 512;
54 module_param(gran, int, S_IRUGO);
55 MODULE_PARM_DESC(gran, "how often the status information should be printed");
58 module_param(check, int, S_IRUGO);
59 MODULE_PARM_DESC(check, "if the written data should be checked");
61 static unsigned int cycles_count;
62 module_param(cycles_count, uint, S_IRUGO);
63 MODULE_PARM_DESC(cycles_count, "how many erase cycles to do "
64 "(infinite by default)");
66 static struct mtd_info *mtd;
68 /* This buffer contains 0x555555...0xAAAAAA... pattern */
69 static unsigned char *patt_5A5;
70 /* This buffer contains 0xAAAAAA...0x555555... pattern */
71 static unsigned char *patt_A5A;
72 /* This buffer contains all 0xFF bytes */
73 static unsigned char *patt_FF;
74 /* This a temporary buffer is use when checking data */
75 static unsigned char *check_buf;
76 /* How many erase cycles were done */
77 static unsigned int erase_cycles;
80 static struct timeval start, finish;
82 static void report_corrupt(unsigned char *read, unsigned char *written);
84 static inline void start_timing(void)
86 do_gettimeofday(&start);
89 static inline void stop_timing(void)
91 do_gettimeofday(&finish);
95 * Erase eraseblock number @ebnum.
97 static inline int erase_eraseblock(int ebnum)
100 struct erase_info ei;
101 loff_t addr = ebnum * mtd->erasesize;
103 memset(&ei, 0, sizeof(struct erase_info));
106 ei.len = mtd->erasesize;
108 err = mtd->erase(mtd, &ei);
110 printk(PRINT_PREF "error %d while erasing EB %d\n", err, ebnum);
114 if (ei.state == MTD_ERASE_FAILED) {
115 printk(PRINT_PREF "some erase error occurred at EB %d\n",
124 * Check that the contents of eraseblock number @enbum is equivalent to the
127 static inline int check_eraseblock(int ebnum, unsigned char *buf)
129 int err, retries = 0;
131 loff_t addr = ebnum * mtd->erasesize;
132 size_t len = mtd->erasesize;
135 addr = (ebnum + 1) * mtd->erasesize - pgcnt * pgsize;
136 len = pgcnt * pgsize;
140 err = mtd->read(mtd, addr, len, &read, check_buf);
141 if (mtd_is_bitflip(err))
142 printk(PRINT_PREF "single bit flip occurred at EB %d "
143 "MTD reported that it was fixed.\n", ebnum);
145 printk(PRINT_PREF "error %d while reading EB %d, "
146 "read %zd\n", err, ebnum, read);
151 printk(PRINT_PREF "failed to read %zd bytes from EB %d, "
152 "read only %zd, but no error reported\n",
157 if (memcmp(buf, check_buf, len)) {
158 printk(PRINT_PREF "read wrong data from EB %d\n", ebnum);
159 report_corrupt(check_buf, buf);
161 if (retries++ < RETRIES) {
164 printk(PRINT_PREF "re-try reading data from EB %d\n",
168 printk(PRINT_PREF "retried %d times, still errors, "
169 "give-up\n", RETRIES);
175 printk(PRINT_PREF "only attempt number %d was OK (!!!)\n",
181 static inline int write_pattern(int ebnum, void *buf)
185 loff_t addr = ebnum * mtd->erasesize;
186 size_t len = mtd->erasesize;
189 addr = (ebnum + 1) * mtd->erasesize - pgcnt * pgsize;
190 len = pgcnt * pgsize;
192 err = mtd->write(mtd, addr, len, &written, buf);
194 printk(PRINT_PREF "error %d while writing EB %d, written %zd"
195 " bytes\n", err, ebnum, written);
198 if (written != len) {
199 printk(PRINT_PREF "written only %zd bytes of %zd, but no error"
200 " reported\n", written, len);
207 static int __init tort_init(void)
209 int err = 0, i, infinite = !cycles_count;
212 printk(KERN_INFO "\n");
213 printk(KERN_INFO "=================================================\n");
214 printk(PRINT_PREF "Warning: this program is trying to wear out your "
215 "flash, stop it if this is not wanted.\n");
218 printk(PRINT_PREF "Please specify a valid mtd-device via module paramter\n");
219 printk(KERN_CRIT "CAREFUL: This test wipes all data on the specified MTD device!\n");
223 printk(PRINT_PREF "MTD device: %d\n", dev);
224 printk(PRINT_PREF "torture %d eraseblocks (%d-%d) of mtd%d\n",
225 ebcnt, eb, eb + ebcnt - 1, dev);
227 printk(PRINT_PREF "torturing just %d pages per eraseblock\n",
229 printk(PRINT_PREF "write verify %s\n", check ? "enabled" : "disabled");
231 mtd = get_mtd_device(NULL, dev);
234 printk(PRINT_PREF "error: cannot get MTD device\n");
238 if (mtd->writesize == 1) {
239 printk(PRINT_PREF "not NAND flash, assume page size is 512 "
243 pgsize = mtd->writesize;
245 if (pgcnt && (pgcnt > mtd->erasesize / pgsize || pgcnt < 0)) {
246 printk(PRINT_PREF "error: invalid pgcnt value %d\n", pgcnt);
251 patt_5A5 = kmalloc(mtd->erasesize, GFP_KERNEL);
253 printk(PRINT_PREF "error: cannot allocate memory\n");
257 patt_A5A = kmalloc(mtd->erasesize, GFP_KERNEL);
259 printk(PRINT_PREF "error: cannot allocate memory\n");
263 patt_FF = kmalloc(mtd->erasesize, GFP_KERNEL);
265 printk(PRINT_PREF "error: cannot allocate memory\n");
269 check_buf = kmalloc(mtd->erasesize, GFP_KERNEL);
271 printk(PRINT_PREF "error: cannot allocate memory\n");
277 /* Initialize patterns */
278 memset(patt_FF, 0xFF, mtd->erasesize);
279 for (i = 0; i < mtd->erasesize / pgsize; i++) {
281 memset(patt_5A5 + i * pgsize, 0x55, pgsize);
282 memset(patt_A5A + i * pgsize, 0xAA, pgsize);
284 memset(patt_5A5 + i * pgsize, 0xAA, pgsize);
285 memset(patt_A5A + i * pgsize, 0x55, pgsize);
290 * Check if there is a bad eraseblock among those we are going to test.
292 memset(&bad_ebs[0], 0, sizeof(int) * ebcnt);
293 if (mtd->block_isbad) {
294 for (i = eb; i < eb + ebcnt; i++) {
295 err = mtd->block_isbad(mtd,
296 (loff_t)i * mtd->erasesize);
299 printk(PRINT_PREF "block_isbad() returned %d "
300 "for EB %d\n", err, i);
305 printk("EB %d is bad. Skip it.\n", i);
316 /* Erase all eraseblocks */
317 for (i = eb; i < eb + ebcnt; i++) {
320 err = erase_eraseblock(i);
326 /* Check if the eraseblocks contain only 0xFF bytes */
328 for (i = eb; i < eb + ebcnt; i++) {
331 err = check_eraseblock(i, patt_FF);
333 printk(PRINT_PREF "verify failed"
334 " for 0xFF... pattern\n");
341 /* Write the pattern */
342 for (i = eb; i < eb + ebcnt; i++) {
345 if ((eb + erase_cycles) & 1)
349 err = write_pattern(i, patt);
355 /* Verify what we wrote */
357 for (i = eb; i < eb + ebcnt; i++) {
360 if ((eb + erase_cycles) & 1)
364 err = check_eraseblock(i, patt);
366 printk(PRINT_PREF "verify failed for %s"
368 ((eb + erase_cycles) & 1) ?
369 "0x55AA55..." : "0xAA55AA...");
378 if (erase_cycles % gran == 0) {
382 ms = (finish.tv_sec - start.tv_sec) * 1000 +
383 (finish.tv_usec - start.tv_usec) / 1000;
384 printk(PRINT_PREF "%08u erase cycles done, took %lu "
385 "milliseconds (%lu seconds)\n",
386 erase_cycles, ms, ms / 1000);
390 if (!infinite && --cycles_count == 0)
395 printk(PRINT_PREF "finished after %u erase cycles\n",
407 printk(PRINT_PREF "error %d occurred during torturing\n", err);
408 printk(KERN_INFO "=================================================\n");
411 module_init(tort_init);
413 static void __exit tort_exit(void)
417 module_exit(tort_exit);
419 static int countdiffs(unsigned char *buf, unsigned char *check_buf,
420 unsigned offset, unsigned len, unsigned *bytesp,
422 static void print_bufs(unsigned char *read, unsigned char *written, int start,
426 * Report the detailed information about how the read EB differs from what was
429 static void report_corrupt(unsigned char *read, unsigned char *written)
432 int bytes, bits, pages, first;
434 size_t check_len = mtd->erasesize;
437 check_len = pgcnt * pgsize;
439 bytes = bits = pages = 0;
440 for (i = 0; i < check_len; i += pgsize)
441 if (countdiffs(written, read, i, pgsize, &bytes,
445 printk(PRINT_PREF "verify fails on %d pages, %d bytes/%d bits\n",
447 printk(PRINT_PREF "The following is a list of all differences between"
448 " what was read from flash and what was expected\n");
450 for (i = 0; i < check_len; i += pgsize) {
453 first = countdiffs(written, read, i, pgsize, &bytes,
458 printk("-------------------------------------------------------"
459 "----------------------------------\n");
461 printk(PRINT_PREF "Page %zd has %d bytes/%d bits failing verify,"
462 " starting at offset 0x%x\n",
463 (mtd->erasesize - check_len + i) / pgsize,
466 offset = first & ~0x7;
467 len = ((first + bytes) | 0x7) + 1 - offset;
469 print_bufs(read, written, offset, len);
473 static void print_bufs(unsigned char *read, unsigned char *written, int start,
479 printk("Offset Read Written\n");
481 printk("0x%08x: ", start + i);
483 for (j1 = 0; j1 < 8 && i + j1 < len; j1++) {
484 printk(" %02x", read[start + i + j1]);
485 if (read[start + i + j1] != written[start + i + j1])
494 printk(" %s ", diff);
496 for (j2 = 0; j2 < 8 && i + j2 < len; j2++)
497 printk(" %02x", written[start + i + j2]);
504 * Count the number of differing bytes and bits and return the first differing
507 static int countdiffs(unsigned char *buf, unsigned char *check_buf,
508 unsigned offset, unsigned len, unsigned *bytesp,
514 for (i = offset; i < offset + len; i++)
515 if (buf[i] != check_buf[i]) {
520 while (i < offset + len) {
521 if (buf[i] != check_buf[i]) {
525 if ((buf[i] & bit) != (check_buf[i] & bit))
536 MODULE_DESCRIPTION("Eraseblock torturing module");
537 MODULE_AUTHOR("Artem Bityutskiy, Jarkko Lavinen, Adrian Hunter");
538 MODULE_LICENSE("GPL");