nandbiterrs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * Copyright © 2012 NetCommWireless
  3. * Iwo Mergler <Iwo.Mergler@netcommwireless.com.au>
  4. *
  5. * Test for multi-bit error recovery on a NAND page This mostly tests the
  6. * ECC controller / driver.
  7. *
  8. * There are two test modes:
  9. *
  10. * 0 - artificially inserting bit errors until the ECC fails
  11. * This is the default method and fairly quick. It should
  12. * be independent of the quality of the FLASH.
  13. *
  14. * 1 - re-writing the same pattern repeatedly until the ECC fails.
  15. * This method relies on the physics of NAND FLASH to eventually
  16. * generate '0' bits if '1' has been written sufficient times.
  17. * Depending on the NAND, the first bit errors will appear after
  18. * 1000 or more writes and then will usually snowball, reaching the
  19. * limits of the ECC quickly.
  20. *
  21. * The test stops after 10000 cycles, should your FLASH be
  22. * exceptionally good and not generate bit errors before that. Try
  23. * a different page in that case.
  24. *
  25. * Please note that neither of these tests will significantly 'use up' any
  26. * FLASH endurance. Only a maximum of two erase operations will be performed.
  27. *
  28. *
  29. * This program is free software; you can redistribute it and/or modify it
  30. * under the terms of the GNU General Public License version 2 as published by
  31. * the Free Software Foundation.
  32. *
  33. * This program is distributed in the hope that it will be useful, but WITHOUT
  34. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  35. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  36. * more details.
  37. *
  38. * You should have received a copy of the GNU General Public License along with
  39. * this program; see the file COPYING. If not, write to the Free Software
  40. * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  41. */
  42. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  43. #include <linux/init.h>
  44. #include <linux/module.h>
  45. #include <linux/moduleparam.h>
  46. #include <linux/mtd/mtd.h>
  47. #include <linux/err.h>
  48. #include <linux/mtd/nand.h>
  49. #include <linux/slab.h>
  50. #include "mtd_test.h"
  51. static int dev;
  52. module_param(dev, int, S_IRUGO);
  53. MODULE_PARM_DESC(dev, "MTD device number to use");
  54. static unsigned page_offset;
  55. module_param(page_offset, uint, S_IRUGO);
  56. MODULE_PARM_DESC(page_offset, "Page number relative to dev start");
  57. static unsigned seed;
  58. module_param(seed, uint, S_IRUGO);
  59. MODULE_PARM_DESC(seed, "Random seed");
  60. static int mode;
  61. module_param(mode, int, S_IRUGO);
  62. MODULE_PARM_DESC(mode, "0=incremental errors, 1=overwrite test");
  63. static unsigned max_overwrite = 10000;
  64. static loff_t offset; /* Offset of the page we're using. */
  65. static unsigned eraseblock; /* Eraseblock number for our page. */
  66. /* We assume that the ECC can correct up to a certain number
  67. * of biterrors per subpage. */
  68. static unsigned subsize; /* Size of subpages */
  69. static unsigned subcount; /* Number of subpages per page */
  70. static struct mtd_info *mtd; /* MTD device */
  71. static uint8_t *wbuffer; /* One page write / compare buffer */
  72. static uint8_t *rbuffer; /* One page read buffer */
  73. /* 'random' bytes from known offsets */
  74. static uint8_t hash(unsigned offset)
  75. {
  76. unsigned v = offset;
  77. unsigned char c;
  78. v ^= 0x7f7edfd3;
  79. v = v ^ (v >> 3);
  80. v = v ^ (v >> 5);
  81. v = v ^ (v >> 13);
  82. c = v & 0xFF;
  83. /* Reverse bits of result. */
  84. c = (c & 0x0F) << 4 | (c & 0xF0) >> 4;
  85. c = (c & 0x33) << 2 | (c & 0xCC) >> 2;
  86. c = (c & 0x55) << 1 | (c & 0xAA) >> 1;
  87. return c;
  88. }
  89. /* Writes wbuffer to page */
  90. static int write_page(int log)
  91. {
  92. if (log)
  93. pr_info("write_page\n");
  94. return mtdtest_write(mtd, offset, mtd->writesize, wbuffer);
  95. }
  96. /* Re-writes the data area while leaving the OOB alone. */
  97. static int rewrite_page(int log)
  98. {
  99. int err = 0;
  100. struct mtd_oob_ops ops;
  101. if (log)
  102. pr_info("rewrite page\n");
  103. ops.mode = MTD_OPS_RAW; /* No ECC */
  104. ops.len = mtd->writesize;
  105. ops.retlen = 0;
  106. ops.ooblen = 0;
  107. ops.oobretlen = 0;
  108. ops.ooboffs = 0;
  109. ops.datbuf = wbuffer;
  110. ops.oobbuf = NULL;
  111. err = mtd_write_oob(mtd, offset, &ops);
  112. if (err || ops.retlen != mtd->writesize) {
  113. pr_err("error: write_oob failed (%d)\n", err);
  114. if (!err)
  115. err = -EIO;
  116. }
  117. return err;
  118. }
  119. /* Reads page into rbuffer. Returns number of corrected bit errors (>=0)
  120. * or error (<0) */
  121. static int read_page(int log)
  122. {
  123. int err = 0;
  124. size_t read;
  125. struct mtd_ecc_stats oldstats;
  126. if (log)
  127. pr_info("read_page\n");
  128. /* Saving last mtd stats */
  129. memcpy(&oldstats, &mtd->ecc_stats, sizeof(oldstats));
  130. err = mtd_read(mtd, offset, mtd->writesize, &read, rbuffer);
  131. if (err == -EUCLEAN)
  132. err = mtd->ecc_stats.corrected - oldstats.corrected;
  133. if (err < 0 || read != mtd->writesize) {
  134. pr_err("error: read failed at %#llx\n", (long long)offset);
  135. if (err >= 0)
  136. err = -EIO;
  137. }
  138. return err;
  139. }
  140. /* Verifies rbuffer against random sequence */
  141. static int verify_page(int log)
  142. {
  143. unsigned i, errs = 0;
  144. if (log)
  145. pr_info("verify_page\n");
  146. for (i = 0; i < mtd->writesize; i++) {
  147. if (rbuffer[i] != hash(i+seed)) {
  148. pr_err("Error: page offset %u, expected %02x, got %02x\n",
  149. i, hash(i+seed), rbuffer[i]);
  150. errs++;
  151. }
  152. }
  153. if (errs)
  154. return -EIO;
  155. else
  156. return 0;
  157. }
  158. #define CBIT(v, n) ((v) & (1 << (n)))
  159. #define BCLR(v, n) ((v) = (v) & ~(1 << (n)))
  160. /* Finds the first '1' bit in wbuffer starting at offset 'byte'
  161. * and sets it to '0'. */
  162. static int insert_biterror(unsigned byte)
  163. {
  164. int bit;
  165. while (byte < mtd->writesize) {
  166. for (bit = 7; bit >= 0; bit--) {
  167. if (CBIT(wbuffer[byte], bit)) {
  168. BCLR(wbuffer[byte], bit);
  169. pr_info("Inserted biterror @ %u/%u\n", byte, bit);
  170. return 0;
  171. }
  172. }
  173. byte++;
  174. }
  175. pr_err("biterror: Failed to find a '1' bit\n");
  176. return -EIO;
  177. }
  178. /* Writes 'random' data to page and then introduces deliberate bit
  179. * errors into the page, while verifying each step. */
  180. static int incremental_errors_test(void)
  181. {
  182. int err = 0;
  183. unsigned i;
  184. unsigned errs_per_subpage = 0;
  185. pr_info("incremental biterrors test\n");
  186. for (i = 0; i < mtd->writesize; i++)
  187. wbuffer[i] = hash(i+seed);
  188. err = write_page(1);
  189. if (err)
  190. goto exit;
  191. while (1) {
  192. err = rewrite_page(1);
  193. if (err)
  194. goto exit;
  195. err = read_page(1);
  196. if (err > 0)
  197. pr_info("Read reported %d corrected bit errors\n", err);
  198. if (err < 0) {
  199. pr_err("After %d biterrors per subpage, read reported error %d\n",
  200. errs_per_subpage, err);
  201. err = 0;
  202. goto exit;
  203. }
  204. err = verify_page(1);
  205. if (err) {
  206. pr_err("ECC failure, read data is incorrect despite read success\n");
  207. goto exit;
  208. }
  209. pr_info("Successfully corrected %d bit errors per subpage\n",
  210. errs_per_subpage);
  211. for (i = 0; i < subcount; i++) {
  212. err = insert_biterror(i * subsize);
  213. if (err < 0)
  214. goto exit;
  215. }
  216. errs_per_subpage++;
  217. }
  218. exit:
  219. return err;
  220. }
  221. /* Writes 'random' data to page and then re-writes that same data repeatedly.
  222. This eventually develops bit errors (bits written as '1' will slowly become
  223. '0'), which are corrected as far as the ECC is capable of. */
  224. static int overwrite_test(void)
  225. {
  226. int err = 0;
  227. unsigned i;
  228. unsigned max_corrected = 0;
  229. unsigned opno = 0;
  230. /* We don't expect more than this many correctable bit errors per
  231. * page. */
  232. #define MAXBITS 512
  233. static unsigned bitstats[MAXBITS]; /* bit error histogram. */
  234. memset(bitstats, 0, sizeof(bitstats));
  235. pr_info("overwrite biterrors test\n");
  236. for (i = 0; i < mtd->writesize; i++)
  237. wbuffer[i] = hash(i+seed);
  238. err = write_page(1);
  239. if (err)
  240. goto exit;
  241. while (opno < max_overwrite) {
  242. err = rewrite_page(0);
  243. if (err)
  244. break;
  245. err = read_page(0);
  246. if (err >= 0) {
  247. if (err >= MAXBITS) {
  248. pr_info("Implausible number of bit errors corrected\n");
  249. err = -EIO;
  250. break;
  251. }
  252. bitstats[err]++;
  253. if (err > max_corrected) {
  254. max_corrected = err;
  255. pr_info("Read reported %d corrected bit errors\n",
  256. err);
  257. }
  258. } else { /* err < 0 */
  259. pr_info("Read reported error %d\n", err);
  260. err = 0;
  261. break;
  262. }
  263. err = verify_page(0);
  264. if (err) {
  265. bitstats[max_corrected] = opno;
  266. pr_info("ECC failure, read data is incorrect despite read success\n");
  267. break;
  268. }
  269. err = mtdtest_relax();
  270. if (err)
  271. break;
  272. opno++;
  273. }
  274. /* At this point bitstats[0] contains the number of ops with no bit
  275. * errors, bitstats[1] the number of ops with 1 bit error, etc. */
  276. pr_info("Bit error histogram (%d operations total):\n", opno);
  277. for (i = 0; i < max_corrected; i++)
  278. pr_info("Page reads with %3d corrected bit errors: %d\n",
  279. i, bitstats[i]);
  280. exit:
  281. return err;
  282. }
  283. static int __init mtd_nandbiterrs_init(void)
  284. {
  285. int err = 0;
  286. printk("\n");
  287. printk(KERN_INFO "==================================================\n");
  288. pr_info("MTD device: %d\n", dev);
  289. mtd = get_mtd_device(NULL, dev);
  290. if (IS_ERR(mtd)) {
  291. err = PTR_ERR(mtd);
  292. pr_err("error: cannot get MTD device\n");
  293. goto exit_mtddev;
  294. }
  295. if (!mtd_type_is_nand(mtd)) {
  296. pr_info("this test requires NAND flash\n");
  297. err = -ENODEV;
  298. goto exit_nand;
  299. }
  300. pr_info("MTD device size %llu, eraseblock=%u, page=%u, oob=%u\n",
  301. (unsigned long long)mtd->size, mtd->erasesize,
  302. mtd->writesize, mtd->oobsize);
  303. subsize = mtd->writesize >> mtd->subpage_sft;
  304. subcount = mtd->writesize / subsize;
  305. pr_info("Device uses %d subpages of %d bytes\n", subcount, subsize);
  306. offset = (loff_t)page_offset * mtd->writesize;
  307. eraseblock = mtd_div_by_eb(offset, mtd);
  308. pr_info("Using page=%u, offset=%llu, eraseblock=%u\n",
  309. page_offset, offset, eraseblock);
  310. wbuffer = kmalloc(mtd->writesize, GFP_KERNEL);
  311. if (!wbuffer) {
  312. err = -ENOMEM;
  313. goto exit_wbuffer;
  314. }
  315. rbuffer = kmalloc(mtd->writesize, GFP_KERNEL);
  316. if (!rbuffer) {
  317. err = -ENOMEM;
  318. goto exit_rbuffer;
  319. }
  320. err = mtdtest_erase_eraseblock(mtd, eraseblock);
  321. if (err)
  322. goto exit_error;
  323. if (mode == 0)
  324. err = incremental_errors_test();
  325. else
  326. err = overwrite_test();
  327. if (err)
  328. goto exit_error;
  329. /* We leave the block un-erased in case of test failure. */
  330. err = mtdtest_erase_eraseblock(mtd, eraseblock);
  331. if (err)
  332. goto exit_error;
  333. err = -EIO;
  334. pr_info("finished successfully.\n");
  335. printk(KERN_INFO "==================================================\n");
  336. exit_error:
  337. kfree(rbuffer);
  338. exit_rbuffer:
  339. kfree(wbuffer);
  340. exit_wbuffer:
  341. /* Nothing */
  342. exit_nand:
  343. put_mtd_device(mtd);
  344. exit_mtddev:
  345. return err;
  346. }
  347. static void __exit mtd_nandbiterrs_exit(void)
  348. {
  349. return;
  350. }
  351. module_init(mtd_nandbiterrs_init);
  352. module_exit(mtd_nandbiterrs_exit);
  353. MODULE_DESCRIPTION("NAND bit error recovery test");
  354. MODULE_AUTHOR("Iwo Mergler");
  355. MODULE_LICENSE("GPL");