torturetest.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. /* Initialize patterns */
  194. memset(patt_FF, 0xFF, mtd->erasesize);
  195. for (i = 0; i < mtd->erasesize / pgsize; i++) {
  196. if (!(i & 1)) {
  197. memset(patt_5A5 + i * pgsize, 0x55, pgsize);
  198. memset(patt_A5A + i * pgsize, 0xAA, pgsize);
  199. } else {
  200. memset(patt_5A5 + i * pgsize, 0xAA, pgsize);
  201. memset(patt_A5A + i * pgsize, 0x55, pgsize);
  202. }
  203. }
  204. err = mtdtest_scan_for_bad_eraseblocks(mtd, bad_ebs, eb, ebcnt);
  205. if (err)
  206. goto out;
  207. start_timing();
  208. while (1) {
  209. int i;
  210. void *patt;
  211. err = mtdtest_erase_good_eraseblocks(mtd, bad_ebs, eb, ebcnt);
  212. if (err)
  213. goto out;
  214. /* Check if the eraseblocks contain only 0xFF bytes */
  215. if (check) {
  216. for (i = eb; i < eb + ebcnt; i++) {
  217. if (bad_ebs[i - eb])
  218. continue;
  219. err = check_eraseblock(i, patt_FF);
  220. if (err) {
  221. pr_info("verify failed"
  222. " for 0xFF... pattern\n");
  223. goto out;
  224. }
  225. err = mtdtest_relax();
  226. if (err)
  227. goto out;
  228. }
  229. }
  230. /* Write the pattern */
  231. for (i = eb; i < eb + ebcnt; i++) {
  232. if (bad_ebs[i - eb])
  233. continue;
  234. if ((eb + erase_cycles) & 1)
  235. patt = patt_5A5;
  236. else
  237. patt = patt_A5A;
  238. err = write_pattern(i, patt);
  239. if (err)
  240. goto out;
  241. err = mtdtest_relax();
  242. if (err)
  243. goto out;
  244. }
  245. /* Verify what we wrote */
  246. if (check) {
  247. for (i = eb; i < eb + ebcnt; i++) {
  248. if (bad_ebs[i - eb])
  249. continue;
  250. if ((eb + erase_cycles) & 1)
  251. patt = patt_5A5;
  252. else
  253. patt = patt_A5A;
  254. err = check_eraseblock(i, patt);
  255. if (err) {
  256. pr_info("verify failed for %s"
  257. " pattern\n",
  258. ((eb + erase_cycles) & 1) ?
  259. "0x55AA55..." : "0xAA55AA...");
  260. goto out;
  261. }
  262. err = mtdtest_relax();
  263. if (err)
  264. goto out;
  265. }
  266. }
  267. erase_cycles += 1;
  268. if (erase_cycles % gran == 0) {
  269. long ms;
  270. stop_timing();
  271. ms = ktime_ms_delta(finish, start);
  272. pr_info("%08u erase cycles done, took %lu "
  273. "milliseconds (%lu seconds)\n",
  274. erase_cycles, ms, ms / 1000);
  275. start_timing();
  276. }
  277. if (!infinite && --cycles_count == 0)
  278. break;
  279. }
  280. out:
  281. pr_info("finished after %u erase cycles\n",
  282. erase_cycles);
  283. kfree(bad_ebs);
  284. out_check_buf:
  285. kfree(check_buf);
  286. out_patt_FF:
  287. kfree(patt_FF);
  288. out_patt_A5A:
  289. kfree(patt_A5A);
  290. out_patt_5A5:
  291. kfree(patt_5A5);
  292. out_mtd:
  293. put_mtd_device(mtd);
  294. if (err)
  295. pr_info("error %d occurred during torturing\n", err);
  296. printk(KERN_INFO "=================================================\n");
  297. return err;
  298. }
  299. module_init(tort_init);
  300. static void __exit tort_exit(void)
  301. {
  302. return;
  303. }
  304. module_exit(tort_exit);
  305. static int countdiffs(unsigned char *buf, unsigned char *check_buf,
  306. unsigned offset, unsigned len, unsigned *bytesp,
  307. unsigned *bitsp);
  308. static void print_bufs(unsigned char *read, unsigned char *written, int start,
  309. int len);
  310. /*
  311. * Report the detailed information about how the read EB differs from what was
  312. * written.
  313. */
  314. static void report_corrupt(unsigned char *read, unsigned char *written)
  315. {
  316. int i;
  317. int bytes, bits, pages, first;
  318. int offset, len;
  319. size_t check_len = mtd->erasesize;
  320. if (pgcnt)
  321. check_len = pgcnt * pgsize;
  322. bytes = bits = pages = 0;
  323. for (i = 0; i < check_len; i += pgsize)
  324. if (countdiffs(written, read, i, pgsize, &bytes,
  325. &bits) >= 0)
  326. pages++;
  327. pr_info("verify fails on %d pages, %d bytes/%d bits\n",
  328. pages, bytes, bits);
  329. pr_info("The following is a list of all differences between"
  330. " what was read from flash and what was expected\n");
  331. for (i = 0; i < check_len; i += pgsize) {
  332. cond_resched();
  333. bytes = bits = 0;
  334. first = countdiffs(written, read, i, pgsize, &bytes,
  335. &bits);
  336. if (first < 0)
  337. continue;
  338. printk("-------------------------------------------------------"
  339. "----------------------------------\n");
  340. pr_info("Page %zd has %d bytes/%d bits failing verify,"
  341. " starting at offset 0x%x\n",
  342. (mtd->erasesize - check_len + i) / pgsize,
  343. bytes, bits, first);
  344. offset = first & ~0x7;
  345. len = ((first + bytes) | 0x7) + 1 - offset;
  346. print_bufs(read, written, offset, len);
  347. }
  348. }
  349. static void print_bufs(unsigned char *read, unsigned char *written, int start,
  350. int len)
  351. {
  352. int i = 0, j1, j2;
  353. char *diff;
  354. printk("Offset Read Written\n");
  355. while (i < len) {
  356. printk("0x%08x: ", start + i);
  357. diff = " ";
  358. for (j1 = 0; j1 < 8 && i + j1 < len; j1++) {
  359. printk(" %02x", read[start + i + j1]);
  360. if (read[start + i + j1] != written[start + i + j1])
  361. diff = "***";
  362. }
  363. while (j1 < 8) {
  364. printk(" ");
  365. j1 += 1;
  366. }
  367. printk(" %s ", diff);
  368. for (j2 = 0; j2 < 8 && i + j2 < len; j2++)
  369. printk(" %02x", written[start + i + j2]);
  370. printk("\n");
  371. i += 8;
  372. }
  373. }
  374. /*
  375. * Count the number of differing bytes and bits and return the first differing
  376. * offset.
  377. */
  378. static int countdiffs(unsigned char *buf, unsigned char *check_buf,
  379. unsigned offset, unsigned len, unsigned *bytesp,
  380. unsigned *bitsp)
  381. {
  382. unsigned i, bit;
  383. int first = -1;
  384. for (i = offset; i < offset + len; i++)
  385. if (buf[i] != check_buf[i]) {
  386. first = i;
  387. break;
  388. }
  389. while (i < offset + len) {
  390. if (buf[i] != check_buf[i]) {
  391. (*bytesp)++;
  392. bit = 1;
  393. while (bit < 256) {
  394. if ((buf[i] & bit) != (check_buf[i] & bit))
  395. (*bitsp)++;
  396. bit <<= 1;
  397. }
  398. }
  399. i++;
  400. }
  401. return first;
  402. }
  403. MODULE_DESCRIPTION("Eraseblock torturing module");
  404. MODULE_AUTHOR("Artem Bityutskiy, Jarkko Lavinen, Adrian Hunter");
  405. MODULE_LICENSE("GPL");