mtd_torturetest.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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 = -EINVAL;
  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;
  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 (mtd_is_bitflip(err))
  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;
  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. if (dev < 0) {
  184. printk(PRINT_PREF "Please specify a valid mtd-device via module paramter\n");
  185. printk(KERN_CRIT "CAREFUL: This test wipes all data on the specified MTD device!\n");
  186. return -EINVAL;
  187. }
  188. printk(PRINT_PREF "MTD device: %d\n", dev);
  189. printk(PRINT_PREF "torture %d eraseblocks (%d-%d) of mtd%d\n",
  190. ebcnt, eb, eb + ebcnt - 1, dev);
  191. if (pgcnt)
  192. printk(PRINT_PREF "torturing just %d pages per eraseblock\n",
  193. pgcnt);
  194. printk(PRINT_PREF "write verify %s\n", check ? "enabled" : "disabled");
  195. mtd = get_mtd_device(NULL, dev);
  196. if (IS_ERR(mtd)) {
  197. err = PTR_ERR(mtd);
  198. printk(PRINT_PREF "error: cannot get MTD device\n");
  199. return err;
  200. }
  201. if (mtd->writesize == 1) {
  202. printk(PRINT_PREF "not NAND flash, assume page size is 512 "
  203. "bytes.\n");
  204. pgsize = 512;
  205. } else
  206. pgsize = mtd->writesize;
  207. if (pgcnt && (pgcnt > mtd->erasesize / pgsize || pgcnt < 0)) {
  208. printk(PRINT_PREF "error: invalid pgcnt value %d\n", pgcnt);
  209. goto out_mtd;
  210. }
  211. err = -ENOMEM;
  212. patt_5A5 = kmalloc(mtd->erasesize, GFP_KERNEL);
  213. if (!patt_5A5) {
  214. printk(PRINT_PREF "error: cannot allocate memory\n");
  215. goto out_mtd;
  216. }
  217. patt_A5A = kmalloc(mtd->erasesize, GFP_KERNEL);
  218. if (!patt_A5A) {
  219. printk(PRINT_PREF "error: cannot allocate memory\n");
  220. goto out_patt_5A5;
  221. }
  222. patt_FF = kmalloc(mtd->erasesize, GFP_KERNEL);
  223. if (!patt_FF) {
  224. printk(PRINT_PREF "error: cannot allocate memory\n");
  225. goto out_patt_A5A;
  226. }
  227. check_buf = kmalloc(mtd->erasesize, GFP_KERNEL);
  228. if (!check_buf) {
  229. printk(PRINT_PREF "error: cannot allocate memory\n");
  230. goto out_patt_FF;
  231. }
  232. err = 0;
  233. /* Initialize patterns */
  234. memset(patt_FF, 0xFF, mtd->erasesize);
  235. for (i = 0; i < mtd->erasesize / pgsize; i++) {
  236. if (!(i & 1)) {
  237. memset(patt_5A5 + i * pgsize, 0x55, pgsize);
  238. memset(patt_A5A + i * pgsize, 0xAA, pgsize);
  239. } else {
  240. memset(patt_5A5 + i * pgsize, 0xAA, pgsize);
  241. memset(patt_A5A + i * pgsize, 0x55, pgsize);
  242. }
  243. }
  244. /*
  245. * Check if there is a bad eraseblock among those we are going to test.
  246. */
  247. memset(&bad_ebs[0], 0, sizeof(int) * ebcnt);
  248. if (mtd_can_have_bb(mtd)) {
  249. for (i = eb; i < eb + ebcnt; i++) {
  250. err = mtd_block_isbad(mtd, (loff_t)i * mtd->erasesize);
  251. if (err < 0) {
  252. printk(PRINT_PREF "block_isbad() returned %d "
  253. "for EB %d\n", err, i);
  254. goto out;
  255. }
  256. if (err) {
  257. printk("EB %d is bad. Skip it.\n", i);
  258. bad_ebs[i - eb] = 1;
  259. }
  260. }
  261. }
  262. start_timing();
  263. while (1) {
  264. int i;
  265. void *patt;
  266. /* Erase all eraseblocks */
  267. for (i = eb; i < eb + ebcnt; i++) {
  268. if (bad_ebs[i - eb])
  269. continue;
  270. err = erase_eraseblock(i);
  271. if (err)
  272. goto out;
  273. cond_resched();
  274. }
  275. /* Check if the eraseblocks contain only 0xFF bytes */
  276. if (check) {
  277. for (i = eb; i < eb + ebcnt; i++) {
  278. if (bad_ebs[i - eb])
  279. continue;
  280. err = check_eraseblock(i, patt_FF);
  281. if (err) {
  282. printk(PRINT_PREF "verify failed"
  283. " for 0xFF... pattern\n");
  284. goto out;
  285. }
  286. cond_resched();
  287. }
  288. }
  289. /* Write the pattern */
  290. for (i = eb; i < eb + ebcnt; i++) {
  291. if (bad_ebs[i - eb])
  292. continue;
  293. if ((eb + erase_cycles) & 1)
  294. patt = patt_5A5;
  295. else
  296. patt = patt_A5A;
  297. err = write_pattern(i, patt);
  298. if (err)
  299. goto out;
  300. cond_resched();
  301. }
  302. /* Verify what we wrote */
  303. if (check) {
  304. for (i = eb; i < eb + ebcnt; i++) {
  305. if (bad_ebs[i - eb])
  306. continue;
  307. if ((eb + erase_cycles) & 1)
  308. patt = patt_5A5;
  309. else
  310. patt = patt_A5A;
  311. err = check_eraseblock(i, patt);
  312. if (err) {
  313. printk(PRINT_PREF "verify failed for %s"
  314. " pattern\n",
  315. ((eb + erase_cycles) & 1) ?
  316. "0x55AA55..." : "0xAA55AA...");
  317. goto out;
  318. }
  319. cond_resched();
  320. }
  321. }
  322. erase_cycles += 1;
  323. if (erase_cycles % gran == 0) {
  324. long ms;
  325. stop_timing();
  326. ms = (finish.tv_sec - start.tv_sec) * 1000 +
  327. (finish.tv_usec - start.tv_usec) / 1000;
  328. printk(PRINT_PREF "%08u erase cycles done, took %lu "
  329. "milliseconds (%lu seconds)\n",
  330. erase_cycles, ms, ms / 1000);
  331. start_timing();
  332. }
  333. if (!infinite && --cycles_count == 0)
  334. break;
  335. }
  336. out:
  337. printk(PRINT_PREF "finished after %u erase cycles\n",
  338. erase_cycles);
  339. kfree(check_buf);
  340. out_patt_FF:
  341. kfree(patt_FF);
  342. out_patt_A5A:
  343. kfree(patt_A5A);
  344. out_patt_5A5:
  345. kfree(patt_5A5);
  346. out_mtd:
  347. put_mtd_device(mtd);
  348. if (err)
  349. printk(PRINT_PREF "error %d occurred during torturing\n", err);
  350. printk(KERN_INFO "=================================================\n");
  351. return err;
  352. }
  353. module_init(tort_init);
  354. static void __exit tort_exit(void)
  355. {
  356. return;
  357. }
  358. module_exit(tort_exit);
  359. static int countdiffs(unsigned char *buf, unsigned char *check_buf,
  360. unsigned offset, unsigned len, unsigned *bytesp,
  361. unsigned *bitsp);
  362. static void print_bufs(unsigned char *read, unsigned char *written, int start,
  363. int len);
  364. /*
  365. * Report the detailed information about how the read EB differs from what was
  366. * written.
  367. */
  368. static void report_corrupt(unsigned char *read, unsigned char *written)
  369. {
  370. int i;
  371. int bytes, bits, pages, first;
  372. int offset, len;
  373. size_t check_len = mtd->erasesize;
  374. if (pgcnt)
  375. check_len = pgcnt * pgsize;
  376. bytes = bits = pages = 0;
  377. for (i = 0; i < check_len; i += pgsize)
  378. if (countdiffs(written, read, i, pgsize, &bytes,
  379. &bits) >= 0)
  380. pages++;
  381. printk(PRINT_PREF "verify fails on %d pages, %d bytes/%d bits\n",
  382. pages, bytes, bits);
  383. printk(PRINT_PREF "The following is a list of all differences between"
  384. " what was read from flash and what was expected\n");
  385. for (i = 0; i < check_len; i += pgsize) {
  386. cond_resched();
  387. bytes = bits = 0;
  388. first = countdiffs(written, read, i, pgsize, &bytes,
  389. &bits);
  390. if (first < 0)
  391. continue;
  392. printk("-------------------------------------------------------"
  393. "----------------------------------\n");
  394. printk(PRINT_PREF "Page %zd has %d bytes/%d bits failing verify,"
  395. " starting at offset 0x%x\n",
  396. (mtd->erasesize - check_len + i) / pgsize,
  397. bytes, bits, first);
  398. offset = first & ~0x7;
  399. len = ((first + bytes) | 0x7) + 1 - offset;
  400. print_bufs(read, written, offset, len);
  401. }
  402. }
  403. static void print_bufs(unsigned char *read, unsigned char *written, int start,
  404. int len)
  405. {
  406. int i = 0, j1, j2;
  407. char *diff;
  408. printk("Offset Read Written\n");
  409. while (i < len) {
  410. printk("0x%08x: ", start + i);
  411. diff = " ";
  412. for (j1 = 0; j1 < 8 && i + j1 < len; j1++) {
  413. printk(" %02x", read[start + i + j1]);
  414. if (read[start + i + j1] != written[start + i + j1])
  415. diff = "***";
  416. }
  417. while (j1 < 8) {
  418. printk(" ");
  419. j1 += 1;
  420. }
  421. printk(" %s ", diff);
  422. for (j2 = 0; j2 < 8 && i + j2 < len; j2++)
  423. printk(" %02x", written[start + i + j2]);
  424. printk("\n");
  425. i += 8;
  426. }
  427. }
  428. /*
  429. * Count the number of differing bytes and bits and return the first differing
  430. * offset.
  431. */
  432. static int countdiffs(unsigned char *buf, unsigned char *check_buf,
  433. unsigned offset, unsigned len, unsigned *bytesp,
  434. unsigned *bitsp)
  435. {
  436. unsigned i, bit;
  437. int first = -1;
  438. for (i = offset; i < offset + len; i++)
  439. if (buf[i] != check_buf[i]) {
  440. first = i;
  441. break;
  442. }
  443. while (i < offset + len) {
  444. if (buf[i] != check_buf[i]) {
  445. (*bytesp)++;
  446. bit = 1;
  447. while (bit < 256) {
  448. if ((buf[i] & bit) != (check_buf[i] & bit))
  449. (*bitsp)++;
  450. bit <<= 1;
  451. }
  452. }
  453. i++;
  454. }
  455. return first;
  456. }
  457. MODULE_DESCRIPTION("Eraseblock torturing module");
  458. MODULE_AUTHOR("Artem Bityutskiy, Jarkko Lavinen, Adrian Hunter");
  459. MODULE_LICENSE("GPL");