mtd_torturetest.c 13 KB

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