torturetest.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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/ktime.h>
  28. #include <linux/module.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/err.h>
  31. #include <linux/mtd/mtd.h>
  32. #include <linux/slab.h>
  33. #include <linux/sched.h>
  34. #include "mtd_test.h"
  35. #define RETRIES 3
  36. static int eb = 8;
  37. module_param(eb, int, S_IRUGO);
  38. MODULE_PARM_DESC(eb, "eraseblock number within the selected MTD device");
  39. static int ebcnt = 32;
  40. module_param(ebcnt, int, S_IRUGO);
  41. MODULE_PARM_DESC(ebcnt, "number of consecutive eraseblocks to torture");
  42. static int pgcnt;
  43. module_param(pgcnt, int, S_IRUGO);
  44. MODULE_PARM_DESC(pgcnt, "number of pages per eraseblock to torture (0 => all)");
  45. static int dev = -EINVAL;
  46. module_param(dev, int, S_IRUGO);
  47. MODULE_PARM_DESC(dev, "MTD device number to use");
  48. static int gran = 512;
  49. module_param(gran, int, S_IRUGO);
  50. MODULE_PARM_DESC(gran, "how often the status information should be printed");
  51. static int check = 1;
  52. module_param(check, int, S_IRUGO);
  53. MODULE_PARM_DESC(check, "if the written data should be checked");
  54. static unsigned int cycles_count;
  55. module_param(cycles_count, uint, S_IRUGO);
  56. MODULE_PARM_DESC(cycles_count, "how many erase cycles to do "
  57. "(infinite by default)");
  58. static struct mtd_info *mtd;
  59. /* This buffer contains 0x555555...0xAAAAAA... pattern */
  60. static unsigned char *patt_5A5;
  61. /* This buffer contains 0xAAAAAA...0x555555... pattern */
  62. static unsigned char *patt_A5A;
  63. /* This buffer contains all 0xFF bytes */
  64. static unsigned char *patt_FF;
  65. /* This a temporary buffer is use when checking data */
  66. static unsigned char *check_buf;
  67. /* How many erase cycles were done */
  68. static unsigned int erase_cycles;
  69. static int pgsize;
  70. static ktime_t start, finish;
  71. static void report_corrupt(unsigned char *read, unsigned char *written);
  72. static inline void start_timing(void)
  73. {
  74. start = ktime_get();
  75. }
  76. static inline void stop_timing(void)
  77. {
  78. finish = ktime_get();
  79. }
  80. /*
  81. * Check that the contents of eraseblock number @enbum is equivalent to the
  82. * @buf buffer.
  83. */
  84. static inline int check_eraseblock(int ebnum, unsigned char *buf)
  85. {
  86. int err, retries = 0;
  87. size_t read;
  88. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  89. size_t len = mtd->erasesize;
  90. if (pgcnt) {
  91. addr = (loff_t)(ebnum + 1) * mtd->erasesize - pgcnt * pgsize;
  92. len = pgcnt * pgsize;
  93. }
  94. retry:
  95. err = mtd_read(mtd, addr, len, &read, check_buf);
  96. if (mtd_is_bitflip(err))
  97. pr_err("single bit flip occurred at EB %d "
  98. "MTD reported that it was fixed.\n", ebnum);
  99. else if (err) {
  100. pr_err("error %d while reading EB %d, "
  101. "read %zd\n", err, ebnum, read);
  102. return err;
  103. }
  104. if (read != len) {
  105. pr_err("failed to read %zd bytes from EB %d, "
  106. "read only %zd, but no error reported\n",
  107. len, ebnum, read);
  108. return -EIO;
  109. }
  110. if (memcmp(buf, check_buf, len)) {
  111. pr_err("read wrong data from EB %d\n", ebnum);
  112. report_corrupt(check_buf, buf);
  113. if (retries++ < RETRIES) {
  114. /* Try read again */
  115. yield();
  116. pr_info("re-try reading data from EB %d\n",
  117. ebnum);
  118. goto retry;
  119. } else {
  120. pr_info("retried %d times, still errors, "
  121. "give-up\n", RETRIES);
  122. return -EINVAL;
  123. }
  124. }
  125. if (retries != 0)
  126. pr_info("only attempt number %d was OK (!!!)\n",
  127. retries);
  128. return 0;
  129. }
  130. static inline int write_pattern(int ebnum, void *buf)
  131. {
  132. int err;
  133. size_t written;
  134. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  135. size_t len = mtd->erasesize;
  136. if (pgcnt) {
  137. addr = (loff_t)(ebnum + 1) * mtd->erasesize - pgcnt * pgsize;
  138. len = pgcnt * pgsize;
  139. }
  140. err = mtd_write(mtd, addr, len, &written, buf);
  141. if (err) {
  142. pr_err("error %d while writing EB %d, written %zd"
  143. " bytes\n", err, ebnum, written);
  144. return err;
  145. }
  146. if (written != len) {
  147. pr_info("written only %zd bytes of %zd, but no error"
  148. " reported\n", written, len);
  149. return -EIO;
  150. }
  151. return 0;
  152. }
  153. static int __init tort_init(void)
  154. {
  155. int err = 0, i, infinite = !cycles_count;
  156. unsigned char *bad_ebs;
  157. printk(KERN_INFO "\n");
  158. printk(KERN_INFO "=================================================\n");
  159. pr_info("Warning: this program is trying to wear out your "
  160. "flash, stop it if this is not wanted.\n");
  161. if (dev < 0) {
  162. pr_info("Please specify a valid mtd-device via module parameter\n");
  163. pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
  164. return -EINVAL;
  165. }
  166. pr_info("MTD device: %d\n", dev);
  167. pr_info("torture %d eraseblocks (%d-%d) of mtd%d\n",
  168. ebcnt, eb, eb + ebcnt - 1, dev);
  169. if (pgcnt)
  170. pr_info("torturing just %d pages per eraseblock\n",
  171. pgcnt);
  172. pr_info("write verify %s\n", check ? "enabled" : "disabled");
  173. mtd = get_mtd_device(NULL, dev);
  174. if (IS_ERR(mtd)) {
  175. err = PTR_ERR(mtd);
  176. pr_err("error: cannot get MTD device\n");
  177. return err;
  178. }
  179. if (mtd->writesize == 1) {
  180. pr_info("not NAND flash, assume page size is 512 "
  181. "bytes.\n");
  182. pgsize = 512;
  183. } else
  184. pgsize = mtd->writesize;
  185. if (pgcnt && (pgcnt > mtd->erasesize / pgsize || pgcnt < 0)) {
  186. pr_err("error: invalid pgcnt value %d\n", pgcnt);
  187. goto out_mtd;
  188. }
  189. err = -ENOMEM;
  190. patt_5A5 = kmalloc(mtd->erasesize, GFP_KERNEL);
  191. if (!patt_5A5)
  192. goto out_mtd;
  193. patt_A5A = kmalloc(mtd->erasesize, GFP_KERNEL);
  194. if (!patt_A5A)
  195. goto out_patt_5A5;
  196. patt_FF = kmalloc(mtd->erasesize, GFP_KERNEL);
  197. if (!patt_FF)
  198. goto out_patt_A5A;
  199. check_buf = kmalloc(mtd->erasesize, GFP_KERNEL);
  200. if (!check_buf)
  201. goto out_patt_FF;
  202. bad_ebs = kzalloc(ebcnt, GFP_KERNEL);
  203. if (!bad_ebs)
  204. goto out_check_buf;
  205. err = 0;
  206. /* Initialize patterns */
  207. memset(patt_FF, 0xFF, mtd->erasesize);
  208. for (i = 0; i < mtd->erasesize / pgsize; i++) {
  209. if (!(i & 1)) {
  210. memset(patt_5A5 + i * pgsize, 0x55, pgsize);
  211. memset(patt_A5A + i * pgsize, 0xAA, pgsize);
  212. } else {
  213. memset(patt_5A5 + i * pgsize, 0xAA, pgsize);
  214. memset(patt_A5A + i * pgsize, 0x55, pgsize);
  215. }
  216. }
  217. err = mtdtest_scan_for_bad_eraseblocks(mtd, bad_ebs, eb, ebcnt);
  218. if (err)
  219. goto out;
  220. start_timing();
  221. while (1) {
  222. int i;
  223. void *patt;
  224. err = mtdtest_erase_good_eraseblocks(mtd, bad_ebs, eb, ebcnt);
  225. if (err)
  226. goto out;
  227. /* Check if the eraseblocks contain only 0xFF bytes */
  228. if (check) {
  229. for (i = eb; i < eb + ebcnt; i++) {
  230. if (bad_ebs[i - eb])
  231. continue;
  232. err = check_eraseblock(i, patt_FF);
  233. if (err) {
  234. pr_info("verify failed"
  235. " for 0xFF... pattern\n");
  236. goto out;
  237. }
  238. err = mtdtest_relax();
  239. if (err)
  240. goto out;
  241. }
  242. }
  243. /* Write the pattern */
  244. for (i = eb; i < eb + ebcnt; i++) {
  245. if (bad_ebs[i - eb])
  246. continue;
  247. if ((eb + erase_cycles) & 1)
  248. patt = patt_5A5;
  249. else
  250. patt = patt_A5A;
  251. err = write_pattern(i, patt);
  252. if (err)
  253. goto out;
  254. err = mtdtest_relax();
  255. if (err)
  256. goto out;
  257. }
  258. /* Verify what we wrote */
  259. if (check) {
  260. for (i = eb; i < eb + ebcnt; i++) {
  261. if (bad_ebs[i - eb])
  262. continue;
  263. if ((eb + erase_cycles) & 1)
  264. patt = patt_5A5;
  265. else
  266. patt = patt_A5A;
  267. err = check_eraseblock(i, patt);
  268. if (err) {
  269. pr_info("verify failed for %s"
  270. " pattern\n",
  271. ((eb + erase_cycles) & 1) ?
  272. "0x55AA55..." : "0xAA55AA...");
  273. goto out;
  274. }
  275. err = mtdtest_relax();
  276. if (err)
  277. goto out;
  278. }
  279. }
  280. erase_cycles += 1;
  281. if (erase_cycles % gran == 0) {
  282. long ms;
  283. stop_timing();
  284. ms = ktime_ms_delta(finish, start);
  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");