0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0015
0016 #include <linux/init.h>
0017 #include <linux/ktime.h>
0018 #include <linux/module.h>
0019 #include <linux/moduleparam.h>
0020 #include <linux/err.h>
0021 #include <linux/mtd/mtd.h>
0022 #include <linux/slab.h>
0023 #include <linux/sched.h>
0024 #include "mtd_test.h"
0025
0026 #define RETRIES 3
0027
0028 static int eb = 8;
0029 module_param(eb, int, S_IRUGO);
0030 MODULE_PARM_DESC(eb, "eraseblock number within the selected MTD device");
0031
0032 static int ebcnt = 32;
0033 module_param(ebcnt, int, S_IRUGO);
0034 MODULE_PARM_DESC(ebcnt, "number of consecutive eraseblocks to torture");
0035
0036 static int pgcnt;
0037 module_param(pgcnt, int, S_IRUGO);
0038 MODULE_PARM_DESC(pgcnt, "number of pages per eraseblock to torture (0 => all)");
0039
0040 static int dev = -EINVAL;
0041 module_param(dev, int, S_IRUGO);
0042 MODULE_PARM_DESC(dev, "MTD device number to use");
0043
0044 static int gran = 512;
0045 module_param(gran, int, S_IRUGO);
0046 MODULE_PARM_DESC(gran, "how often the status information should be printed");
0047
0048 static int check = 1;
0049 module_param(check, int, S_IRUGO);
0050 MODULE_PARM_DESC(check, "if the written data should be checked");
0051
0052 static unsigned int cycles_count;
0053 module_param(cycles_count, uint, S_IRUGO);
0054 MODULE_PARM_DESC(cycles_count, "how many erase cycles to do "
0055 "(infinite by default)");
0056
0057 static struct mtd_info *mtd;
0058
0059
0060 static unsigned char *patt_5A5;
0061
0062 static unsigned char *patt_A5A;
0063
0064 static unsigned char *patt_FF;
0065
0066 static unsigned char *check_buf;
0067
0068 static unsigned int erase_cycles;
0069
0070 static int pgsize;
0071 static ktime_t start, finish;
0072
0073 static void report_corrupt(unsigned char *read, unsigned char *written);
0074
0075 static inline void start_timing(void)
0076 {
0077 start = ktime_get();
0078 }
0079
0080 static inline void stop_timing(void)
0081 {
0082 finish = ktime_get();
0083 }
0084
0085
0086
0087
0088
0089 static inline int check_eraseblock(int ebnum, unsigned char *buf)
0090 {
0091 int err, retries = 0;
0092 size_t read;
0093 loff_t addr = (loff_t)ebnum * mtd->erasesize;
0094 size_t len = mtd->erasesize;
0095
0096 if (pgcnt) {
0097 addr = (loff_t)(ebnum + 1) * mtd->erasesize - pgcnt * pgsize;
0098 len = pgcnt * pgsize;
0099 }
0100
0101 retry:
0102 err = mtd_read(mtd, addr, len, &read, check_buf);
0103 if (mtd_is_bitflip(err))
0104 pr_err("single bit flip occurred at EB %d "
0105 "MTD reported that it was fixed.\n", ebnum);
0106 else if (err) {
0107 pr_err("error %d while reading EB %d, "
0108 "read %zd\n", err, ebnum, read);
0109 return err;
0110 }
0111
0112 if (read != len) {
0113 pr_err("failed to read %zd bytes from EB %d, "
0114 "read only %zd, but no error reported\n",
0115 len, ebnum, read);
0116 return -EIO;
0117 }
0118
0119 if (memcmp(buf, check_buf, len)) {
0120 pr_err("read wrong data from EB %d\n", ebnum);
0121 report_corrupt(check_buf, buf);
0122
0123 if (retries++ < RETRIES) {
0124
0125 yield();
0126 pr_info("re-try reading data from EB %d\n",
0127 ebnum);
0128 goto retry;
0129 } else {
0130 pr_info("retried %d times, still errors, "
0131 "give-up\n", RETRIES);
0132 return -EINVAL;
0133 }
0134 }
0135
0136 if (retries != 0)
0137 pr_info("only attempt number %d was OK (!!!)\n",
0138 retries);
0139
0140 return 0;
0141 }
0142
0143 static inline int write_pattern(int ebnum, void *buf)
0144 {
0145 int err;
0146 size_t written;
0147 loff_t addr = (loff_t)ebnum * mtd->erasesize;
0148 size_t len = mtd->erasesize;
0149
0150 if (pgcnt) {
0151 addr = (loff_t)(ebnum + 1) * mtd->erasesize - pgcnt * pgsize;
0152 len = pgcnt * pgsize;
0153 }
0154 err = mtd_write(mtd, addr, len, &written, buf);
0155 if (err) {
0156 pr_err("error %d while writing EB %d, written %zd"
0157 " bytes\n", err, ebnum, written);
0158 return err;
0159 }
0160 if (written != len) {
0161 pr_info("written only %zd bytes of %zd, but no error"
0162 " reported\n", written, len);
0163 return -EIO;
0164 }
0165
0166 return 0;
0167 }
0168
0169 static int __init tort_init(void)
0170 {
0171 int err = 0, i, infinite = !cycles_count;
0172 unsigned char *bad_ebs;
0173
0174 printk(KERN_INFO "\n");
0175 printk(KERN_INFO "=================================================\n");
0176 pr_info("Warning: this program is trying to wear out your "
0177 "flash, stop it if this is not wanted.\n");
0178
0179 if (dev < 0) {
0180 pr_info("Please specify a valid mtd-device via module parameter\n");
0181 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
0182 return -EINVAL;
0183 }
0184
0185 pr_info("MTD device: %d\n", dev);
0186 pr_info("torture %d eraseblocks (%d-%d) of mtd%d\n",
0187 ebcnt, eb, eb + ebcnt - 1, dev);
0188 if (pgcnt)
0189 pr_info("torturing just %d pages per eraseblock\n",
0190 pgcnt);
0191 pr_info("write verify %s\n", check ? "enabled" : "disabled");
0192
0193 mtd = get_mtd_device(NULL, dev);
0194 if (IS_ERR(mtd)) {
0195 err = PTR_ERR(mtd);
0196 pr_err("error: cannot get MTD device\n");
0197 return err;
0198 }
0199
0200 if (mtd->writesize == 1) {
0201 pr_info("not NAND flash, assume page size is 512 "
0202 "bytes.\n");
0203 pgsize = 512;
0204 } else
0205 pgsize = mtd->writesize;
0206
0207 if (pgcnt && (pgcnt > mtd->erasesize / pgsize || pgcnt < 0)) {
0208 pr_err("error: invalid pgcnt value %d\n", pgcnt);
0209 goto out_mtd;
0210 }
0211
0212 err = -ENOMEM;
0213 patt_5A5 = kmalloc(mtd->erasesize, GFP_KERNEL);
0214 if (!patt_5A5)
0215 goto out_mtd;
0216
0217 patt_A5A = kmalloc(mtd->erasesize, GFP_KERNEL);
0218 if (!patt_A5A)
0219 goto out_patt_5A5;
0220
0221 patt_FF = kmalloc(mtd->erasesize, GFP_KERNEL);
0222 if (!patt_FF)
0223 goto out_patt_A5A;
0224
0225 check_buf = kmalloc(mtd->erasesize, GFP_KERNEL);
0226 if (!check_buf)
0227 goto out_patt_FF;
0228
0229 bad_ebs = kzalloc(ebcnt, GFP_KERNEL);
0230 if (!bad_ebs)
0231 goto out_check_buf;
0232
0233
0234 memset(patt_FF, 0xFF, mtd->erasesize);
0235 for (i = 0; i < mtd->erasesize / pgsize; i++) {
0236 if (!(i & 1)) {
0237 memset(patt_5A5 + i * pgsize, 0x55, pgsize);
0238 memset(patt_A5A + i * pgsize, 0xAA, pgsize);
0239 } else {
0240 memset(patt_5A5 + i * pgsize, 0xAA, pgsize);
0241 memset(patt_A5A + i * pgsize, 0x55, pgsize);
0242 }
0243 }
0244
0245 err = mtdtest_scan_for_bad_eraseblocks(mtd, bad_ebs, eb, ebcnt);
0246 if (err)
0247 goto out;
0248
0249 start_timing();
0250 while (1) {
0251 int i;
0252 void *patt;
0253
0254 err = mtdtest_erase_good_eraseblocks(mtd, bad_ebs, eb, ebcnt);
0255 if (err)
0256 goto out;
0257
0258
0259 if (check) {
0260 for (i = eb; i < eb + ebcnt; i++) {
0261 if (bad_ebs[i - eb])
0262 continue;
0263 err = check_eraseblock(i, patt_FF);
0264 if (err) {
0265 pr_info("verify failed"
0266 " for 0xFF... pattern\n");
0267 goto out;
0268 }
0269
0270 err = mtdtest_relax();
0271 if (err)
0272 goto out;
0273 }
0274 }
0275
0276
0277 for (i = eb; i < eb + ebcnt; i++) {
0278 if (bad_ebs[i - eb])
0279 continue;
0280 if ((eb + erase_cycles) & 1)
0281 patt = patt_5A5;
0282 else
0283 patt = patt_A5A;
0284 err = write_pattern(i, patt);
0285 if (err)
0286 goto out;
0287
0288 err = mtdtest_relax();
0289 if (err)
0290 goto out;
0291 }
0292
0293
0294 if (check) {
0295 for (i = eb; i < eb + ebcnt; i++) {
0296 if (bad_ebs[i - eb])
0297 continue;
0298 if ((eb + erase_cycles) & 1)
0299 patt = patt_5A5;
0300 else
0301 patt = patt_A5A;
0302 err = check_eraseblock(i, patt);
0303 if (err) {
0304 pr_info("verify failed for %s"
0305 " pattern\n",
0306 ((eb + erase_cycles) & 1) ?
0307 "0x55AA55..." : "0xAA55AA...");
0308 goto out;
0309 }
0310
0311 err = mtdtest_relax();
0312 if (err)
0313 goto out;
0314 }
0315 }
0316
0317 erase_cycles += 1;
0318
0319 if (erase_cycles % gran == 0) {
0320 long ms;
0321
0322 stop_timing();
0323 ms = ktime_ms_delta(finish, start);
0324 pr_info("%08u erase cycles done, took %lu "
0325 "milliseconds (%lu seconds)\n",
0326 erase_cycles, ms, ms / 1000);
0327 start_timing();
0328 }
0329
0330 if (!infinite && --cycles_count == 0)
0331 break;
0332 }
0333 out:
0334
0335 pr_info("finished after %u erase cycles\n",
0336 erase_cycles);
0337 kfree(bad_ebs);
0338 out_check_buf:
0339 kfree(check_buf);
0340 out_patt_FF:
0341 kfree(patt_FF);
0342 out_patt_A5A:
0343 kfree(patt_A5A);
0344 out_patt_5A5:
0345 kfree(patt_5A5);
0346 out_mtd:
0347 put_mtd_device(mtd);
0348 if (err)
0349 pr_info("error %d occurred during torturing\n", err);
0350 printk(KERN_INFO "=================================================\n");
0351 return err;
0352 }
0353 module_init(tort_init);
0354
0355 static void __exit tort_exit(void)
0356 {
0357 return;
0358 }
0359 module_exit(tort_exit);
0360
0361 static int countdiffs(unsigned char *buf, unsigned char *check_buf,
0362 unsigned offset, unsigned len, unsigned *bytesp,
0363 unsigned *bitsp);
0364 static void print_bufs(unsigned char *read, unsigned char *written, int start,
0365 int len);
0366
0367
0368
0369
0370
0371 static void report_corrupt(unsigned char *read, unsigned char *written)
0372 {
0373 int i;
0374 int bytes, bits, pages, first;
0375 int offset, len;
0376 size_t check_len = mtd->erasesize;
0377
0378 if (pgcnt)
0379 check_len = pgcnt * pgsize;
0380
0381 bytes = bits = pages = 0;
0382 for (i = 0; i < check_len; i += pgsize)
0383 if (countdiffs(written, read, i, pgsize, &bytes,
0384 &bits) >= 0)
0385 pages++;
0386
0387 pr_info("verify fails on %d pages, %d bytes/%d bits\n",
0388 pages, bytes, bits);
0389 pr_info("The following is a list of all differences between"
0390 " what was read from flash and what was expected\n");
0391
0392 for (i = 0; i < check_len; i += pgsize) {
0393 cond_resched();
0394 bytes = bits = 0;
0395 first = countdiffs(written, read, i, pgsize, &bytes,
0396 &bits);
0397 if (first < 0)
0398 continue;
0399
0400 printk("-------------------------------------------------------"
0401 "----------------------------------\n");
0402
0403 pr_info("Page %zd has %d bytes/%d bits failing verify,"
0404 " starting at offset 0x%x\n",
0405 (mtd->erasesize - check_len + i) / pgsize,
0406 bytes, bits, first);
0407
0408 offset = first & ~0x7;
0409 len = ((first + bytes) | 0x7) + 1 - offset;
0410
0411 print_bufs(read, written, offset, len);
0412 }
0413 }
0414
0415 static void print_bufs(unsigned char *read, unsigned char *written, int start,
0416 int len)
0417 {
0418 int i = 0, j1, j2;
0419 char *diff;
0420
0421 printk("Offset Read Written\n");
0422 while (i < len) {
0423 printk("0x%08x: ", start + i);
0424 diff = " ";
0425 for (j1 = 0; j1 < 8 && i + j1 < len; j1++) {
0426 printk(" %02x", read[start + i + j1]);
0427 if (read[start + i + j1] != written[start + i + j1])
0428 diff = "***";
0429 }
0430
0431 while (j1 < 8) {
0432 printk(" ");
0433 j1 += 1;
0434 }
0435
0436 printk(" %s ", diff);
0437
0438 for (j2 = 0; j2 < 8 && i + j2 < len; j2++)
0439 printk(" %02x", written[start + i + j2]);
0440 printk("\n");
0441 i += 8;
0442 }
0443 }
0444
0445
0446
0447
0448
0449 static int countdiffs(unsigned char *buf, unsigned char *check_buf,
0450 unsigned offset, unsigned len, unsigned *bytesp,
0451 unsigned *bitsp)
0452 {
0453 unsigned i, bit;
0454 int first = -1;
0455
0456 for (i = offset; i < offset + len; i++)
0457 if (buf[i] != check_buf[i]) {
0458 first = i;
0459 break;
0460 }
0461
0462 while (i < offset + len) {
0463 if (buf[i] != check_buf[i]) {
0464 (*bytesp)++;
0465 bit = 1;
0466 while (bit < 256) {
0467 if ((buf[i] & bit) != (check_buf[i] & bit))
0468 (*bitsp)++;
0469 bit <<= 1;
0470 }
0471 }
0472 i++;
0473 }
0474
0475 return first;
0476 }
0477
0478 MODULE_DESCRIPTION("Eraseblock torturing module");
0479 MODULE_AUTHOR("Artem Bityutskiy, Jarkko Lavinen, Adrian Hunter");
0480 MODULE_LICENSE("GPL");