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 = 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 (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 = 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. 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->block_isbad) {
  249. for (i = eb; i < eb + ebcnt; i++) {
  250. err = mtd->block_isbad(mtd,
  251. (loff_t)i * mtd->erasesize);
  252. if (err < 0) {
  253. printk(PRINT_PREF "block_isbad() returned %d "
  254. "for EB %d\n", err, i);
  255. goto out;
  256. }
  257. if (err) {
  258. printk("EB %d is bad. Skip it.\n", i);
  259. bad_ebs[i - eb] = 1;
  260. }
  261. }
  262. }
  263. start_timing();
  264. while (1) {
  265. int i;
  266. void *patt;
  267. /* Erase all eraseblocks */
  268. for (i = eb; i < eb + ebcnt; i++) {
  269. if (bad_ebs[i - eb])
  270. continue;
  271. err = erase_eraseblock(i);
  272. if (err)
  273. goto out;
  274. cond_resched();
  275. }
  276. /* Check if the eraseblocks contain only 0xFF bytes */
  277. if (check) {
  278. for (i = eb; i < eb + ebcnt; i++) {
  279. if (bad_ebs[i - eb])
  280. continue;
  281. err = check_eraseblock(i, patt_FF);
  282. if (err) {
  283. printk(PRINT_PREF "verify failed"
  284. " for 0xFF... pattern\n");
  285. goto out;
  286. }
  287. cond_resched();
  288. }
  289. }
  290. /* Write the pattern */
  291. for (i = eb; i < eb + ebcnt; i++) {
  292. if (bad_ebs[i - eb])
  293. continue;
  294. if ((eb + erase_cycles) & 1)
  295. patt = patt_5A5;
  296. else
  297. patt = patt_A5A;
  298. err = write_pattern(i, patt);
  299. if (err)
  300. goto out;
  301. cond_resched();
  302. }
  303. /* Verify what we wrote */
  304. if (check) {
  305. for (i = eb; i < eb + ebcnt; i++) {
  306. if (bad_ebs[i - eb])
  307. continue;
  308. if ((eb + erase_cycles) & 1)
  309. patt = patt_5A5;
  310. else
  311. patt = patt_A5A;
  312. err = check_eraseblock(i, patt);
  313. if (err) {
  314. printk(PRINT_PREF "verify failed for %s"
  315. " pattern\n",
  316. ((eb + erase_cycles) & 1) ?
  317. "0x55AA55..." : "0xAA55AA...");
  318. goto out;
  319. }
  320. cond_resched();
  321. }
  322. }
  323. erase_cycles += 1;
  324. if (erase_cycles % gran == 0) {
  325. long ms;
  326. stop_timing();
  327. ms = (finish.tv_sec - start.tv_sec) * 1000 +
  328. (finish.tv_usec - start.tv_usec) / 1000;
  329. printk(PRINT_PREF "%08u erase cycles done, took %lu "
  330. "milliseconds (%lu seconds)\n",
  331. erase_cycles, ms, ms / 1000);
  332. start_timing();
  333. }
  334. if (!infinite && --cycles_count == 0)
  335. break;
  336. }
  337. out:
  338. printk(PRINT_PREF "finished after %u erase cycles\n",
  339. erase_cycles);
  340. kfree(check_buf);
  341. out_patt_FF:
  342. kfree(patt_FF);
  343. out_patt_A5A:
  344. kfree(patt_A5A);
  345. out_patt_5A5:
  346. kfree(patt_5A5);
  347. out_mtd:
  348. put_mtd_device(mtd);
  349. if (err)
  350. printk(PRINT_PREF "error %d occurred during torturing\n", err);
  351. printk(KERN_INFO "=================================================\n");
  352. return err;
  353. }
  354. module_init(tort_init);
  355. static void __exit tort_exit(void)
  356. {
  357. return;
  358. }
  359. module_exit(tort_exit);
  360. static int countdiffs(unsigned char *buf, unsigned char *check_buf,
  361. unsigned offset, unsigned len, unsigned *bytesp,
  362. unsigned *bitsp);
  363. static void print_bufs(unsigned char *read, unsigned char *written, int start,
  364. int len);
  365. /*
  366. * Report the detailed information about how the read EB differs from what was
  367. * written.
  368. */
  369. static void report_corrupt(unsigned char *read, unsigned char *written)
  370. {
  371. int i;
  372. int bytes, bits, pages, first;
  373. int offset, len;
  374. size_t check_len = mtd->erasesize;
  375. if (pgcnt)
  376. check_len = pgcnt * pgsize;
  377. bytes = bits = pages = 0;
  378. for (i = 0; i < check_len; i += pgsize)
  379. if (countdiffs(written, read, i, pgsize, &bytes,
  380. &bits) >= 0)
  381. pages++;
  382. printk(PRINT_PREF "verify fails on %d pages, %d bytes/%d bits\n",
  383. pages, bytes, bits);
  384. printk(PRINT_PREF "The following is a list of all differences between"
  385. " what was read from flash and what was expected\n");
  386. for (i = 0; i < check_len; i += pgsize) {
  387. cond_resched();
  388. bytes = bits = 0;
  389. first = countdiffs(written, read, i, pgsize, &bytes,
  390. &bits);
  391. if (first < 0)
  392. continue;
  393. printk("-------------------------------------------------------"
  394. "----------------------------------\n");
  395. printk(PRINT_PREF "Page %zd has %d bytes/%d bits failing verify,"
  396. " starting at offset 0x%x\n",
  397. (mtd->erasesize - check_len + i) / pgsize,
  398. bytes, bits, first);
  399. offset = first & ~0x7;
  400. len = ((first + bytes) | 0x7) + 1 - offset;
  401. print_bufs(read, written, offset, len);
  402. }
  403. }
  404. static void print_bufs(unsigned char *read, unsigned char *written, int start,
  405. int len)
  406. {
  407. int i = 0, j1, j2;
  408. char *diff;
  409. printk("Offset Read Written\n");
  410. while (i < len) {
  411. printk("0x%08x: ", start + i);
  412. diff = " ";
  413. for (j1 = 0; j1 < 8 && i + j1 < len; j1++) {
  414. printk(" %02x", read[start + i + j1]);
  415. if (read[start + i + j1] != written[start + i + j1])
  416. diff = "***";
  417. }
  418. while (j1 < 8) {
  419. printk(" ");
  420. j1 += 1;
  421. }
  422. printk(" %s ", diff);
  423. for (j2 = 0; j2 < 8 && i + j2 < len; j2++)
  424. printk(" %02x", written[start + i + j2]);
  425. printk("\n");
  426. i += 8;
  427. }
  428. }
  429. /*
  430. * Count the number of differing bytes and bits and return the first differing
  431. * offset.
  432. */
  433. static int countdiffs(unsigned char *buf, unsigned char *check_buf,
  434. unsigned offset, unsigned len, unsigned *bytesp,
  435. unsigned *bitsp)
  436. {
  437. unsigned i, bit;
  438. int first = -1;
  439. for (i = offset; i < offset + len; i++)
  440. if (buf[i] != check_buf[i]) {
  441. first = i;
  442. break;
  443. }
  444. while (i < offset + len) {
  445. if (buf[i] != check_buf[i]) {
  446. (*bytesp)++;
  447. bit = 1;
  448. while (bit < 256) {
  449. if ((buf[i] & bit) != (check_buf[i] & bit))
  450. (*bitsp)++;
  451. bit <<= 1;
  452. }
  453. }
  454. i++;
  455. }
  456. return first;
  457. }
  458. MODULE_DESCRIPTION("Eraseblock torturing module");
  459. MODULE_AUTHOR("Artem Bityutskiy, Jarkko Lavinen, Adrian Hunter");
  460. MODULE_LICENSE("GPL");