torturetest.c 12 KB

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