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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 #include <linux/init.h>
29 #include <linux/ktime.h>
30 #include <linux/module.h>
31 #include <linux/moduleparam.h>
32 #include <linux/err.h>
33 #include <linux/mtd/mtd.h>
34 #include <linux/slab.h>
35 #include <linux/sched.h>
41 module_param(eb, int, S_IRUGO);
42 MODULE_PARM_DESC(eb, "eraseblock number within the selected MTD device");
44 static int ebcnt = 32;
45 module_param(ebcnt, int, S_IRUGO);
46 MODULE_PARM_DESC(ebcnt, "number of consecutive eraseblocks to torture");
49 module_param(pgcnt, int, S_IRUGO);
50 MODULE_PARM_DESC(pgcnt, "number of pages per eraseblock to torture (0 => all)");
52 static int dev = -EINVAL;
53 module_param(dev, int, S_IRUGO);
54 MODULE_PARM_DESC(dev, "MTD device number to use");
56 static int gran = 512;
57 module_param(gran, int, S_IRUGO);
58 MODULE_PARM_DESC(gran, "how often the status information should be printed");
61 module_param(check, int, S_IRUGO);
62 MODULE_PARM_DESC(check, "if the written data should be checked");
64 static unsigned int cycles_count;
65 module_param(cycles_count, uint, S_IRUGO);
66 MODULE_PARM_DESC(cycles_count, "how many erase cycles to do "
67 "(infinite by default)");
69 static struct mtd_info *mtd;
71 /* This buffer contains 0x555555...0xAAAAAA... pattern */
72 static unsigned char *patt_5A5;
73 /* This buffer contains 0xAAAAAA...0x555555... pattern */
74 static unsigned char *patt_A5A;
75 /* This buffer contains all 0xFF bytes */
76 static unsigned char *patt_FF;
77 /* This a temporary buffer is use when checking data */
78 static unsigned char *check_buf;
79 /* How many erase cycles were done */
80 static unsigned int erase_cycles;
83 static ktime_t start, finish;
85 static void report_corrupt(unsigned char *read, unsigned char *written);
87 static inline void start_timing(void)
92 static inline void stop_timing(void)
98 * Check that the contents of eraseblock number @enbum is equivalent to the
101 static inline int check_eraseblock(int ebnum, unsigned char *buf)
103 int err, retries = 0;
105 loff_t addr = (loff_t)ebnum * mtd->erasesize;
106 size_t len = mtd->erasesize;
109 addr = (loff_t)(ebnum + 1) * mtd->erasesize - pgcnt * pgsize;
110 len = pgcnt * pgsize;
114 err = mtd_read(mtd, addr, len, &read, check_buf);
115 if (mtd_is_bitflip(err))
116 pr_err("single bit flip occurred at EB %d "
117 "MTD reported that it was fixed.\n", ebnum);
119 pr_err("error %d while reading EB %d, "
120 "read %zd\n", err, ebnum, read);
125 pr_err("failed to read %zd bytes from EB %d, "
126 "read only %zd, but no error reported\n",
131 if (memcmp(buf, check_buf, len)) {
132 pr_err("read wrong data from EB %d\n", ebnum);
133 report_corrupt(check_buf, buf);
135 if (retries++ < RETRIES) {
138 pr_info("re-try reading data from EB %d\n",
142 pr_info("retried %d times, still errors, "
143 "give-up\n", RETRIES);
149 pr_info("only attempt number %d was OK (!!!)\n",
155 static inline int write_pattern(int ebnum, void *buf)
159 loff_t addr = (loff_t)ebnum * mtd->erasesize;
160 size_t len = mtd->erasesize;
163 addr = (loff_t)(ebnum + 1) * mtd->erasesize - pgcnt * pgsize;
164 len = pgcnt * pgsize;
166 err = mtd_write(mtd, addr, len, &written, buf);
168 pr_err("error %d while writing EB %d, written %zd"
169 " bytes\n", err, ebnum, written);
172 if (written != len) {
173 pr_info("written only %zd bytes of %zd, but no error"
174 " reported\n", written, len);
181 static int __init tort_init(void)
183 int err = 0, i, infinite = !cycles_count;
184 unsigned char *bad_ebs;
186 printk(KERN_INFO "\n");
187 printk(KERN_INFO "=================================================\n");
188 pr_info("Warning: this program is trying to wear out your "
189 "flash, stop it if this is not wanted.\n");
192 pr_info("Please specify a valid mtd-device via module parameter\n");
193 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
197 pr_info("MTD device: %d\n", dev);
198 pr_info("torture %d eraseblocks (%d-%d) of mtd%d\n",
199 ebcnt, eb, eb + ebcnt - 1, dev);
201 pr_info("torturing just %d pages per eraseblock\n",
203 pr_info("write verify %s\n", check ? "enabled" : "disabled");
205 mtd = get_mtd_device(NULL, dev);
208 pr_err("error: cannot get MTD device\n");
212 if (mtd->writesize == 1) {
213 pr_info("not NAND flash, assume page size is 512 "
217 pgsize = mtd->writesize;
219 if (pgcnt && (pgcnt > mtd->erasesize / pgsize || pgcnt < 0)) {
220 pr_err("error: invalid pgcnt value %d\n", pgcnt);
225 patt_5A5 = kmalloc(mtd->erasesize, GFP_KERNEL);
229 patt_A5A = kmalloc(mtd->erasesize, GFP_KERNEL);
233 patt_FF = kmalloc(mtd->erasesize, GFP_KERNEL);
237 check_buf = kmalloc(mtd->erasesize, GFP_KERNEL);
241 bad_ebs = kzalloc(ebcnt, GFP_KERNEL);
247 /* Initialize patterns */
248 memset(patt_FF, 0xFF, mtd->erasesize);
249 for (i = 0; i < mtd->erasesize / pgsize; i++) {
251 memset(patt_5A5 + i * pgsize, 0x55, pgsize);
252 memset(patt_A5A + i * pgsize, 0xAA, pgsize);
254 memset(patt_5A5 + i * pgsize, 0xAA, pgsize);
255 memset(patt_A5A + i * pgsize, 0x55, pgsize);
259 err = mtdtest_scan_for_bad_eraseblocks(mtd, bad_ebs, eb, ebcnt);
268 err = mtdtest_erase_good_eraseblocks(mtd, bad_ebs, eb, ebcnt);
272 /* Check if the eraseblocks contain only 0xFF bytes */
274 for (i = eb; i < eb + ebcnt; i++) {
277 err = check_eraseblock(i, patt_FF);
279 pr_info("verify failed"
280 " for 0xFF... pattern\n");
284 err = mtdtest_relax();
290 /* Write the pattern */
291 for (i = eb; i < eb + ebcnt; i++) {
294 if ((eb + erase_cycles) & 1)
298 err = write_pattern(i, patt);
302 err = mtdtest_relax();
307 /* Verify what we wrote */
309 for (i = eb; i < eb + ebcnt; i++) {
312 if ((eb + erase_cycles) & 1)
316 err = check_eraseblock(i, patt);
318 pr_info("verify failed for %s"
320 ((eb + erase_cycles) & 1) ?
321 "0x55AA55..." : "0xAA55AA...");
325 err = mtdtest_relax();
333 if (erase_cycles % gran == 0) {
337 ms = ktime_ms_delta(finish, start);
338 pr_info("%08u erase cycles done, took %lu "
339 "milliseconds (%lu seconds)\n",
340 erase_cycles, ms, ms / 1000);
344 if (!infinite && --cycles_count == 0)
349 pr_info("finished after %u erase cycles\n",
363 pr_info("error %d occurred during torturing\n", err);
364 printk(KERN_INFO "=================================================\n");
367 module_init(tort_init);
369 static void __exit tort_exit(void)
373 module_exit(tort_exit);
375 static int countdiffs(unsigned char *buf, unsigned char *check_buf,
376 unsigned offset, unsigned len, unsigned *bytesp,
378 static void print_bufs(unsigned char *read, unsigned char *written, int start,
382 * Report the detailed information about how the read EB differs from what was
385 static void report_corrupt(unsigned char *read, unsigned char *written)
388 int bytes, bits, pages, first;
390 size_t check_len = mtd->erasesize;
393 check_len = pgcnt * pgsize;
395 bytes = bits = pages = 0;
396 for (i = 0; i < check_len; i += pgsize)
397 if (countdiffs(written, read, i, pgsize, &bytes,
401 pr_info("verify fails on %d pages, %d bytes/%d bits\n",
403 pr_info("The following is a list of all differences between"
404 " what was read from flash and what was expected\n");
406 for (i = 0; i < check_len; i += pgsize) {
409 first = countdiffs(written, read, i, pgsize, &bytes,
414 printk("-------------------------------------------------------"
415 "----------------------------------\n");
417 pr_info("Page %zd has %d bytes/%d bits failing verify,"
418 " starting at offset 0x%x\n",
419 (mtd->erasesize - check_len + i) / pgsize,
422 offset = first & ~0x7;
423 len = ((first + bytes) | 0x7) + 1 - offset;
425 print_bufs(read, written, offset, len);
429 static void print_bufs(unsigned char *read, unsigned char *written, int start,
435 printk("Offset Read Written\n");
437 printk("0x%08x: ", start + i);
439 for (j1 = 0; j1 < 8 && i + j1 < len; j1++) {
440 printk(" %02x", read[start + i + j1]);
441 if (read[start + i + j1] != written[start + i + j1])
450 printk(" %s ", diff);
452 for (j2 = 0; j2 < 8 && i + j2 < len; j2++)
453 printk(" %02x", written[start + i + j2]);
460 * Count the number of differing bytes and bits and return the first differing
463 static int countdiffs(unsigned char *buf, unsigned char *check_buf,
464 unsigned offset, unsigned len, unsigned *bytesp,
470 for (i = offset; i < offset + len; i++)
471 if (buf[i] != check_buf[i]) {
476 while (i < offset + len) {
477 if (buf[i] != check_buf[i]) {
481 if ((buf[i] & bit) != (check_buf[i] & bit))
492 MODULE_DESCRIPTION("Eraseblock torturing module");
493 MODULE_AUTHOR("Artem Bityutskiy, Jarkko Lavinen, Adrian Hunter");
494 MODULE_LICENSE("GPL");