mtd_torturetest.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /*
  2. * Copyright (C) 2006-2008 Artem Bityutskiy
  3. * Copyright (C) 2006-2008 Jarkko Lavinen
  4. * Copyright (C) 2006-2008 Adrian Hunter
  5. *
  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.
  9. *
  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
  13. * more details.
  14. *
  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.
  18. *
  19. * Authors: Artem Bityutskiy, Jarkko Lavinen, Adria Hunter
  20. *
  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.
  24. */
  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. #define PRINT_PREF KERN_INFO "mtd_torturetest: "
  33. #define RETRIES 3
  34. static int eb = 8;
  35. module_param(eb, int, S_IRUGO);
  36. MODULE_PARM_DESC(eb, "eraseblock number within the selected MTD device");
  37. static int ebcnt = 32;
  38. module_param(ebcnt, int, S_IRUGO);
  39. MODULE_PARM_DESC(ebcnt, "number of consecutive eraseblocks to torture");
  40. static int pgcnt;
  41. module_param(pgcnt, int, S_IRUGO);
  42. MODULE_PARM_DESC(pgcnt, "number of pages per eraseblock to torture (0 => all)");
  43. static int dev;
  44. module_param(dev, int, S_IRUGO);
  45. MODULE_PARM_DESC(dev, "MTD device number to use");
  46. static int gran = 512;
  47. module_param(gran, int, S_IRUGO);
  48. MODULE_PARM_DESC(gran, "how often the status information should be printed");
  49. static int check = 1;
  50. module_param(check, int, S_IRUGO);
  51. MODULE_PARM_DESC(check, "if the written data should be checked");
  52. static unsigned int cycles_count;
  53. module_param(cycles_count, uint, S_IRUGO);
  54. MODULE_PARM_DESC(cycles_count, "how many erase cycles to do "
  55. "(infinite by default)");
  56. static struct mtd_info *mtd;
  57. /* This buffer contains 0x555555...0xAAAAAA... pattern */
  58. static unsigned char *patt_5A5;
  59. /* This buffer contains 0xAAAAAA...0x555555... pattern */
  60. static unsigned char *patt_A5A;
  61. /* This buffer contains all 0xFF bytes */
  62. static unsigned char *patt_FF;
  63. /* This a temporary buffer is use when checking data */
  64. static unsigned char *check_buf;
  65. /* How many erase cycles were done */
  66. static unsigned int erase_cycles;
  67. static int pgsize;
  68. static struct timeval start, finish;
  69. static void report_corrupt(unsigned char *read, unsigned char *written);
  70. static inline void start_timing(void)
  71. {
  72. do_gettimeofday(&start);
  73. }
  74. static inline void stop_timing(void)
  75. {
  76. do_gettimeofday(&finish);
  77. }
  78. /*
  79. * Erase eraseblock number @ebnum.
  80. */
  81. static inline int erase_eraseblock(int ebnum)
  82. {
  83. int err;
  84. struct erase_info ei;
  85. loff_t addr = ebnum * mtd->erasesize;
  86. memset(&ei, 0, sizeof(struct erase_info));
  87. ei.mtd = mtd;
  88. ei.addr = addr;
  89. ei.len = mtd->erasesize;
  90. err = mtd->erase(mtd, &ei);
  91. if (err) {
  92. printk(PRINT_PREF "error %d while erasing EB %d\n", err, ebnum);
  93. return err;
  94. }
  95. if (ei.state == MTD_ERASE_FAILED) {
  96. printk(PRINT_PREF "some erase error occurred at EB %d\n",
  97. ebnum);
  98. return -EIO;
  99. }
  100. return 0;
  101. }
  102. /*
  103. * Check that the contents of eraseblock number @enbum is equivalent to the
  104. * @buf buffer.
  105. */
  106. static inline int check_eraseblock(int ebnum, unsigned char *buf)
  107. {
  108. int err, retries = 0;
  109. size_t read = 0;
  110. loff_t addr = ebnum * mtd->erasesize;
  111. size_t len = mtd->erasesize;
  112. if (pgcnt) {
  113. addr = (ebnum + 1) * mtd->erasesize - pgcnt * pgsize;
  114. len = pgcnt * pgsize;
  115. }
  116. retry:
  117. err = mtd->read(mtd, addr, len, &read, check_buf);
  118. if (err == -EUCLEAN)
  119. printk(PRINT_PREF "single bit flip occurred at EB %d "
  120. "MTD reported that it was fixed.\n", ebnum);
  121. else if (err) {
  122. printk(PRINT_PREF "error %d while reading EB %d, "
  123. "read %zd\n", err, ebnum, read);
  124. return err;
  125. }
  126. if (read != len) {
  127. printk(PRINT_PREF "failed to read %zd bytes from EB %d, "
  128. "read only %zd, but no error reported\n",
  129. len, ebnum, read);
  130. return -EIO;
  131. }
  132. if (memcmp(buf, check_buf, len)) {
  133. printk(PRINT_PREF "read wrong data from EB %d\n", ebnum);
  134. report_corrupt(check_buf, buf);
  135. if (retries++ < RETRIES) {
  136. /* Try read again */
  137. yield();
  138. printk(PRINT_PREF "re-try reading data from EB %d\n",
  139. ebnum);
  140. goto retry;
  141. } else {
  142. printk(PRINT_PREF "retried %d times, still errors, "
  143. "give-up\n", RETRIES);
  144. return -EINVAL;
  145. }
  146. }
  147. if (retries != 0)
  148. printk(PRINT_PREF "only attempt number %d was OK (!!!)\n",
  149. retries);
  150. return 0;
  151. }
  152. static inline int write_pattern(int ebnum, void *buf)
  153. {
  154. int err;
  155. size_t written = 0;
  156. loff_t addr = ebnum * mtd->erasesize;
  157. size_t len = mtd->erasesize;
  158. if (pgcnt) {
  159. addr = (ebnum + 1) * mtd->erasesize - pgcnt * pgsize;
  160. len = pgcnt * pgsize;
  161. }
  162. err = mtd->write(mtd, addr, len, &written, buf);
  163. if (err) {
  164. printk(PRINT_PREF "error %d while writing EB %d, written %zd"
  165. " bytes\n", err, ebnum, written);
  166. return err;
  167. }
  168. if (written != len) {
  169. printk(PRINT_PREF "written only %zd bytes of %zd, but no error"
  170. " reported\n", written, len);
  171. return -EIO;
  172. }
  173. return 0;
  174. }
  175. static int __init tort_init(void)
  176. {
  177. int err = 0, i, infinite = !cycles_count;
  178. int bad_ebs[ebcnt];
  179. printk(KERN_INFO "\n");
  180. printk(KERN_INFO "=================================================\n");
  181. printk(PRINT_PREF "Warning: this program is trying to wear out your "
  182. "flash, stop it if this is not wanted.\n");
  183. printk(PRINT_PREF "MTD device: %d\n", dev);
  184. printk(PRINT_PREF "torture %d eraseblocks (%d-%d) of mtd%d\n",
  185. ebcnt, eb, eb + ebcnt - 1, dev);
  186. if (pgcnt)
  187. printk(PRINT_PREF "torturing just %d pages per eraseblock\n",
  188. pgcnt);
  189. printk(PRINT_PREF "write verify %s\n", check ? "enabled" : "disabled");
  190. mtd = get_mtd_device(NULL, dev);
  191. if (IS_ERR(mtd)) {
  192. err = PTR_ERR(mtd);
  193. printk(PRINT_PREF "error: cannot get MTD device\n");
  194. return err;
  195. }
  196. if (mtd->writesize == 1) {
  197. printk(PRINT_PREF "not NAND flash, assume page size is 512 "
  198. "bytes.\n");
  199. pgsize = 512;
  200. } else
  201. pgsize = mtd->writesize;
  202. if (pgcnt && (pgcnt > mtd->erasesize / pgsize || pgcnt < 0)) {
  203. printk(PRINT_PREF "error: invalid pgcnt value %d\n", pgcnt);
  204. goto out_mtd;
  205. }
  206. err = -ENOMEM;
  207. patt_5A5 = kmalloc(mtd->erasesize, GFP_KERNEL);
  208. if (!patt_5A5) {
  209. printk(PRINT_PREF "error: cannot allocate memory\n");
  210. goto out_mtd;
  211. }
  212. patt_A5A = kmalloc(mtd->erasesize, GFP_KERNEL);
  213. if (!patt_A5A) {
  214. printk(PRINT_PREF "error: cannot allocate memory\n");
  215. goto out_patt_5A5;
  216. }
  217. patt_FF = kmalloc(mtd->erasesize, GFP_KERNEL);
  218. if (!patt_FF) {
  219. printk(PRINT_PREF "error: cannot allocate memory\n");
  220. goto out_patt_A5A;
  221. }
  222. check_buf = kmalloc(mtd->erasesize, GFP_KERNEL);
  223. if (!check_buf) {
  224. printk(PRINT_PREF "error: cannot allocate memory\n");
  225. goto out_patt_FF;
  226. }
  227. err = 0;
  228. /* Initialize patterns */
  229. memset(patt_FF, 0xFF, mtd->erasesize);
  230. for (i = 0; i < mtd->erasesize / pgsize; i++) {
  231. if (!(i & 1)) {
  232. memset(patt_5A5 + i * pgsize, 0x55, pgsize);
  233. memset(patt_A5A + i * pgsize, 0xAA, pgsize);
  234. } else {
  235. memset(patt_5A5 + i * pgsize, 0xAA, pgsize);
  236. memset(patt_A5A + i * pgsize, 0x55, pgsize);
  237. }
  238. }
  239. /*
  240. * Check if there is a bad eraseblock among those we are going to test.
  241. */
  242. memset(&bad_ebs[0], 0, sizeof(int) * ebcnt);
  243. if (mtd->block_isbad) {
  244. for (i = eb; i < eb + ebcnt; i++) {
  245. err = mtd->block_isbad(mtd,
  246. (loff_t)i * mtd->erasesize);
  247. if (err < 0) {
  248. printk(PRINT_PREF "block_isbad() returned %d "
  249. "for EB %d\n", err, i);
  250. goto out;
  251. }
  252. if (err) {
  253. printk("EB %d is bad. Skip it.\n", i);
  254. bad_ebs[i - eb] = 1;
  255. }
  256. }
  257. }
  258. start_timing();
  259. while (1) {
  260. int i;
  261. void *patt;
  262. /* Erase all eraseblocks */
  263. for (i = eb; i < eb + ebcnt; i++) {
  264. if (bad_ebs[i - eb])
  265. continue;
  266. err = erase_eraseblock(i);
  267. if (err)
  268. goto out;
  269. cond_resched();
  270. }
  271. /* Check if the eraseblocks contain only 0xFF bytes */
  272. if (check) {
  273. for (i = eb; i < eb + ebcnt; i++) {
  274. if (bad_ebs[i - eb])
  275. continue;
  276. err = check_eraseblock(i, patt_FF);
  277. if (err) {
  278. printk(PRINT_PREF "verify failed"
  279. " for 0xFF... pattern\n");
  280. goto out;
  281. }
  282. cond_resched();
  283. }
  284. }
  285. /* Write the pattern */
  286. for (i = eb; i < eb + ebcnt; i++) {
  287. if (bad_ebs[i - eb])
  288. continue;
  289. if ((eb + erase_cycles) & 1)
  290. patt = patt_5A5;
  291. else
  292. patt = patt_A5A;
  293. err = write_pattern(i, patt);
  294. if (err)
  295. goto out;
  296. cond_resched();
  297. }
  298. /* Verify what we wrote */
  299. if (check) {
  300. for (i = eb; i < eb + ebcnt; i++) {
  301. if (bad_ebs[i - eb])
  302. continue;
  303. if ((eb + erase_cycles) & 1)
  304. patt = patt_5A5;
  305. else
  306. patt = patt_A5A;
  307. err = check_eraseblock(i, patt);
  308. if (err) {
  309. printk(PRINT_PREF "verify failed for %s"
  310. " pattern\n",
  311. ((eb + erase_cycles) & 1) ?
  312. "0x55AA55..." : "0xAA55AA...");
  313. goto out;
  314. }
  315. cond_resched();
  316. }
  317. }
  318. erase_cycles += 1;
  319. if (erase_cycles % gran == 0) {
  320. long ms;
  321. stop_timing();
  322. ms = (finish.tv_sec - start.tv_sec) * 1000 +
  323. (finish.tv_usec - start.tv_usec) / 1000;
  324. printk(PRINT_PREF "%08u erase cycles done, took %lu "
  325. "milliseconds (%lu seconds)\n",
  326. erase_cycles, ms, ms / 1000);
  327. start_timing();
  328. }
  329. if (!infinite && --cycles_count == 0)
  330. break;
  331. }
  332. out:
  333. printk(PRINT_PREF "finished after %u erase cycles\n",
  334. erase_cycles);
  335. kfree(check_buf);
  336. out_patt_FF:
  337. kfree(patt_FF);
  338. out_patt_A5A:
  339. kfree(patt_A5A);
  340. out_patt_5A5:
  341. kfree(patt_5A5);
  342. out_mtd:
  343. put_mtd_device(mtd);
  344. if (err)
  345. printk(PRINT_PREF "error %d occurred during torturing\n", err);
  346. printk(KERN_INFO "=================================================\n");
  347. return err;
  348. }
  349. module_init(tort_init);
  350. static void __exit tort_exit(void)
  351. {
  352. return;
  353. }
  354. module_exit(tort_exit);
  355. static int countdiffs(unsigned char *buf, unsigned char *check_buf,
  356. unsigned offset, unsigned len, unsigned *bytesp,
  357. unsigned *bitsp);
  358. static void print_bufs(unsigned char *read, unsigned char *written, int start,
  359. int len);
  360. /*
  361. * Report the detailed information about how the read EB differs from what was
  362. * written.
  363. */
  364. static void report_corrupt(unsigned char *read, unsigned char *written)
  365. {
  366. int i;
  367. int bytes, bits, pages, first;
  368. int offset, len;
  369. size_t check_len = mtd->erasesize;
  370. if (pgcnt)
  371. check_len = pgcnt * pgsize;
  372. bytes = bits = pages = 0;
  373. for (i = 0; i < check_len; i += pgsize)
  374. if (countdiffs(written, read, i, pgsize, &bytes,
  375. &bits) >= 0)
  376. pages++;
  377. printk(PRINT_PREF "verify fails on %d pages, %d bytes/%d bits\n",
  378. pages, bytes, bits);
  379. printk(PRINT_PREF "The following is a list of all differences between"
  380. " what was read from flash and what was expected\n");
  381. for (i = 0; i < check_len; i += pgsize) {
  382. cond_resched();
  383. bytes = bits = 0;
  384. first = countdiffs(written, read, i, pgsize, &bytes,
  385. &bits);
  386. if (first < 0)
  387. continue;
  388. printk("-------------------------------------------------------"
  389. "----------------------------------\n");
  390. printk(PRINT_PREF "Page %zd has %d bytes/%d bits failing verify,"
  391. " starting at offset 0x%x\n",
  392. (mtd->erasesize - check_len + i) / pgsize,
  393. bytes, bits, first);
  394. offset = first & ~0x7;
  395. len = ((first + bytes) | 0x7) + 1 - offset;
  396. print_bufs(read, written, offset, len);
  397. }
  398. }
  399. static void print_bufs(unsigned char *read, unsigned char *written, int start,
  400. int len)
  401. {
  402. int i = 0, j1, j2;
  403. char *diff;
  404. printk("Offset Read Written\n");
  405. while (i < len) {
  406. printk("0x%08x: ", start + i);
  407. diff = " ";
  408. for (j1 = 0; j1 < 8 && i + j1 < len; j1++) {
  409. printk(" %02x", read[start + i + j1]);
  410. if (read[start + i + j1] != written[start + i + j1])
  411. diff = "***";
  412. }
  413. while (j1 < 8) {
  414. printk(" ");
  415. j1 += 1;
  416. }
  417. printk(" %s ", diff);
  418. for (j2 = 0; j2 < 8 && i + j2 < len; j2++)
  419. printk(" %02x", written[start + i + j2]);
  420. printk("\n");
  421. i += 8;
  422. }
  423. }
  424. /*
  425. * Count the number of differing bytes and bits and return the first differing
  426. * offset.
  427. */
  428. static int countdiffs(unsigned char *buf, unsigned char *check_buf,
  429. unsigned offset, unsigned len, unsigned *bytesp,
  430. unsigned *bitsp)
  431. {
  432. unsigned i, bit;
  433. int first = -1;
  434. for (i = offset; i < offset + len; i++)
  435. if (buf[i] != check_buf[i]) {
  436. first = i;
  437. break;
  438. }
  439. while (i < offset + len) {
  440. if (buf[i] != check_buf[i]) {
  441. (*bytesp)++;
  442. bit = 1;
  443. while (bit < 256) {
  444. if ((buf[i] & bit) != (check_buf[i] & bit))
  445. (*bitsp)++;
  446. bit <<= 1;
  447. }
  448. }
  449. i++;
  450. }
  451. return first;
  452. }
  453. MODULE_DESCRIPTION("Eraseblock torturing module");
  454. MODULE_AUTHOR("Artem Bityutskiy, Jarkko Lavinen, Adrian Hunter");
  455. MODULE_LICENSE("GPL");