torturetest.c 11 KB

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