subpagetest.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * Copyright (C) 2006-2007 Nokia Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License version 2 as published by
  6. * the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; see the file COPYING. If not, write to the Free Software
  15. * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. *
  17. * Test sub-page read and write on MTD device.
  18. * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
  19. *
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/err.h>
  26. #include <linux/mtd/mtd.h>
  27. #include <linux/slab.h>
  28. #include <linux/sched.h>
  29. #include <linux/random.h>
  30. #include "mtd_test.h"
  31. static int dev = -EINVAL;
  32. module_param(dev, int, S_IRUGO);
  33. MODULE_PARM_DESC(dev, "MTD device number to use");
  34. static struct mtd_info *mtd;
  35. static unsigned char *writebuf;
  36. static unsigned char *readbuf;
  37. static unsigned char *bbt;
  38. static int subpgsize;
  39. static int bufsize;
  40. static int ebcnt;
  41. static int pgcnt;
  42. static int errcnt;
  43. static struct rnd_state rnd_state;
  44. static inline void clear_data(unsigned char *buf, size_t len)
  45. {
  46. memset(buf, 0, len);
  47. }
  48. static int write_eraseblock(int ebnum)
  49. {
  50. size_t written;
  51. int err = 0;
  52. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  53. prandom_bytes_state(&rnd_state, writebuf, subpgsize);
  54. err = mtd_write(mtd, addr, subpgsize, &written, writebuf);
  55. if (unlikely(err || written != subpgsize)) {
  56. pr_err("error: write failed at %#llx\n",
  57. (long long)addr);
  58. if (written != subpgsize) {
  59. pr_err(" write size: %#x\n", subpgsize);
  60. pr_err(" written: %#zx\n", written);
  61. }
  62. return err ? err : -1;
  63. }
  64. addr += subpgsize;
  65. prandom_bytes_state(&rnd_state, writebuf, subpgsize);
  66. err = mtd_write(mtd, addr, subpgsize, &written, writebuf);
  67. if (unlikely(err || written != subpgsize)) {
  68. pr_err("error: write failed at %#llx\n",
  69. (long long)addr);
  70. if (written != subpgsize) {
  71. pr_err(" write size: %#x\n", subpgsize);
  72. pr_err(" written: %#zx\n", written);
  73. }
  74. return err ? err : -1;
  75. }
  76. return err;
  77. }
  78. static int write_eraseblock2(int ebnum)
  79. {
  80. size_t written;
  81. int err = 0, k;
  82. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  83. for (k = 1; k < 33; ++k) {
  84. if (addr + (subpgsize * k) > (loff_t)(ebnum + 1) * mtd->erasesize)
  85. break;
  86. prandom_bytes_state(&rnd_state, writebuf, subpgsize * k);
  87. err = mtd_write(mtd, addr, subpgsize * k, &written, writebuf);
  88. if (unlikely(err || written != subpgsize * k)) {
  89. pr_err("error: write failed at %#llx\n",
  90. (long long)addr);
  91. if (written != subpgsize) {
  92. pr_err(" write size: %#x\n",
  93. subpgsize * k);
  94. pr_err(" written: %#08zx\n",
  95. written);
  96. }
  97. return err ? err : -1;
  98. }
  99. addr += subpgsize * k;
  100. }
  101. return err;
  102. }
  103. static void print_subpage(unsigned char *p)
  104. {
  105. int i, j;
  106. for (i = 0; i < subpgsize; ) {
  107. for (j = 0; i < subpgsize && j < 32; ++i, ++j)
  108. printk("%02x", *p++);
  109. printk("\n");
  110. }
  111. }
  112. static int verify_eraseblock(int ebnum)
  113. {
  114. size_t read;
  115. int err = 0;
  116. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  117. prandom_bytes_state(&rnd_state, writebuf, subpgsize);
  118. clear_data(readbuf, subpgsize);
  119. err = mtd_read(mtd, addr, subpgsize, &read, readbuf);
  120. if (unlikely(err || read != subpgsize)) {
  121. if (mtd_is_bitflip(err) && read == subpgsize) {
  122. pr_info("ECC correction at %#llx\n",
  123. (long long)addr);
  124. err = 0;
  125. } else {
  126. pr_err("error: read failed at %#llx\n",
  127. (long long)addr);
  128. return err ? err : -1;
  129. }
  130. }
  131. if (unlikely(memcmp(readbuf, writebuf, subpgsize))) {
  132. pr_err("error: verify failed at %#llx\n",
  133. (long long)addr);
  134. pr_info("------------- written----------------\n");
  135. print_subpage(writebuf);
  136. pr_info("------------- read ------------------\n");
  137. print_subpage(readbuf);
  138. pr_info("-------------------------------------\n");
  139. errcnt += 1;
  140. }
  141. addr += subpgsize;
  142. prandom_bytes_state(&rnd_state, writebuf, subpgsize);
  143. clear_data(readbuf, subpgsize);
  144. err = mtd_read(mtd, addr, subpgsize, &read, readbuf);
  145. if (unlikely(err || read != subpgsize)) {
  146. if (mtd_is_bitflip(err) && read == subpgsize) {
  147. pr_info("ECC correction at %#llx\n",
  148. (long long)addr);
  149. err = 0;
  150. } else {
  151. pr_err("error: read failed at %#llx\n",
  152. (long long)addr);
  153. return err ? err : -1;
  154. }
  155. }
  156. if (unlikely(memcmp(readbuf, writebuf, subpgsize))) {
  157. pr_info("error: verify failed at %#llx\n",
  158. (long long)addr);
  159. pr_info("------------- written----------------\n");
  160. print_subpage(writebuf);
  161. pr_info("------------- read ------------------\n");
  162. print_subpage(readbuf);
  163. pr_info("-------------------------------------\n");
  164. errcnt += 1;
  165. }
  166. return err;
  167. }
  168. static int verify_eraseblock2(int ebnum)
  169. {
  170. size_t read;
  171. int err = 0, k;
  172. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  173. for (k = 1; k < 33; ++k) {
  174. if (addr + (subpgsize * k) > (loff_t)(ebnum + 1) * mtd->erasesize)
  175. break;
  176. prandom_bytes_state(&rnd_state, writebuf, subpgsize * k);
  177. clear_data(readbuf, subpgsize * k);
  178. err = mtd_read(mtd, addr, subpgsize * k, &read, readbuf);
  179. if (unlikely(err || read != subpgsize * k)) {
  180. if (mtd_is_bitflip(err) && read == subpgsize * k) {
  181. pr_info("ECC correction at %#llx\n",
  182. (long long)addr);
  183. err = 0;
  184. } else {
  185. pr_err("error: read failed at "
  186. "%#llx\n", (long long)addr);
  187. return err ? err : -1;
  188. }
  189. }
  190. if (unlikely(memcmp(readbuf, writebuf, subpgsize * k))) {
  191. pr_err("error: verify failed at %#llx\n",
  192. (long long)addr);
  193. errcnt += 1;
  194. }
  195. addr += subpgsize * k;
  196. }
  197. return err;
  198. }
  199. static int verify_eraseblock_ff(int ebnum)
  200. {
  201. uint32_t j;
  202. size_t read;
  203. int err = 0;
  204. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  205. memset(writebuf, 0xff, subpgsize);
  206. for (j = 0; j < mtd->erasesize / subpgsize; ++j) {
  207. clear_data(readbuf, subpgsize);
  208. err = mtd_read(mtd, addr, subpgsize, &read, readbuf);
  209. if (unlikely(err || read != subpgsize)) {
  210. if (mtd_is_bitflip(err) && read == subpgsize) {
  211. pr_info("ECC correction at %#llx\n",
  212. (long long)addr);
  213. err = 0;
  214. } else {
  215. pr_err("error: read failed at "
  216. "%#llx\n", (long long)addr);
  217. return err ? err : -1;
  218. }
  219. }
  220. if (unlikely(memcmp(readbuf, writebuf, subpgsize))) {
  221. pr_err("error: verify 0xff failed at "
  222. "%#llx\n", (long long)addr);
  223. errcnt += 1;
  224. }
  225. addr += subpgsize;
  226. }
  227. return err;
  228. }
  229. static int verify_all_eraseblocks_ff(void)
  230. {
  231. int err;
  232. unsigned int i;
  233. pr_info("verifying all eraseblocks for 0xff\n");
  234. for (i = 0; i < ebcnt; ++i) {
  235. if (bbt[i])
  236. continue;
  237. err = verify_eraseblock_ff(i);
  238. if (err)
  239. return err;
  240. if (i % 256 == 0)
  241. pr_info("verified up to eraseblock %u\n", i);
  242. err = mtdtest_relax();
  243. if (err)
  244. return err;
  245. }
  246. pr_info("verified %u eraseblocks\n", i);
  247. return 0;
  248. }
  249. static int __init mtd_subpagetest_init(void)
  250. {
  251. int err = 0;
  252. uint32_t i;
  253. uint64_t tmp;
  254. printk(KERN_INFO "\n");
  255. printk(KERN_INFO "=================================================\n");
  256. if (dev < 0) {
  257. pr_info("Please specify a valid mtd-device via module parameter\n");
  258. pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
  259. return -EINVAL;
  260. }
  261. pr_info("MTD device: %d\n", dev);
  262. mtd = get_mtd_device(NULL, dev);
  263. if (IS_ERR(mtd)) {
  264. err = PTR_ERR(mtd);
  265. pr_err("error: cannot get MTD device\n");
  266. return err;
  267. }
  268. if (!mtd_type_is_nand(mtd)) {
  269. pr_info("this test requires NAND flash\n");
  270. goto out;
  271. }
  272. subpgsize = mtd->writesize >> mtd->subpage_sft;
  273. tmp = mtd->size;
  274. do_div(tmp, mtd->erasesize);
  275. ebcnt = tmp;
  276. pgcnt = mtd->erasesize / mtd->writesize;
  277. pr_info("MTD device size %llu, eraseblock size %u, "
  278. "page size %u, subpage size %u, count of eraseblocks %u, "
  279. "pages per eraseblock %u, OOB size %u\n",
  280. (unsigned long long)mtd->size, mtd->erasesize,
  281. mtd->writesize, subpgsize, ebcnt, pgcnt, mtd->oobsize);
  282. err = -ENOMEM;
  283. bufsize = subpgsize * 32;
  284. writebuf = kmalloc(bufsize, GFP_KERNEL);
  285. if (!writebuf)
  286. goto out;
  287. readbuf = kmalloc(bufsize, GFP_KERNEL);
  288. if (!readbuf)
  289. goto out;
  290. bbt = kzalloc(ebcnt, GFP_KERNEL);
  291. if (!bbt)
  292. goto out;
  293. err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
  294. if (err)
  295. goto out;
  296. err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
  297. if (err)
  298. goto out;
  299. pr_info("writing whole device\n");
  300. prandom_seed_state(&rnd_state, 1);
  301. for (i = 0; i < ebcnt; ++i) {
  302. if (bbt[i])
  303. continue;
  304. err = write_eraseblock(i);
  305. if (unlikely(err))
  306. goto out;
  307. if (i % 256 == 0)
  308. pr_info("written up to eraseblock %u\n", i);
  309. err = mtdtest_relax();
  310. if (err)
  311. goto out;
  312. }
  313. pr_info("written %u eraseblocks\n", i);
  314. prandom_seed_state(&rnd_state, 1);
  315. pr_info("verifying all eraseblocks\n");
  316. for (i = 0; i < ebcnt; ++i) {
  317. if (bbt[i])
  318. continue;
  319. err = verify_eraseblock(i);
  320. if (unlikely(err))
  321. goto out;
  322. if (i % 256 == 0)
  323. pr_info("verified up to eraseblock %u\n", i);
  324. err = mtdtest_relax();
  325. if (err)
  326. goto out;
  327. }
  328. pr_info("verified %u eraseblocks\n", i);
  329. err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
  330. if (err)
  331. goto out;
  332. err = verify_all_eraseblocks_ff();
  333. if (err)
  334. goto out;
  335. /* Write all eraseblocks */
  336. prandom_seed_state(&rnd_state, 3);
  337. pr_info("writing whole device\n");
  338. for (i = 0; i < ebcnt; ++i) {
  339. if (bbt[i])
  340. continue;
  341. err = write_eraseblock2(i);
  342. if (unlikely(err))
  343. goto out;
  344. if (i % 256 == 0)
  345. pr_info("written up to eraseblock %u\n", i);
  346. err = mtdtest_relax();
  347. if (err)
  348. goto out;
  349. }
  350. pr_info("written %u eraseblocks\n", i);
  351. /* Check all eraseblocks */
  352. prandom_seed_state(&rnd_state, 3);
  353. pr_info("verifying all eraseblocks\n");
  354. for (i = 0; i < ebcnt; ++i) {
  355. if (bbt[i])
  356. continue;
  357. err = verify_eraseblock2(i);
  358. if (unlikely(err))
  359. goto out;
  360. if (i % 256 == 0)
  361. pr_info("verified up to eraseblock %u\n", i);
  362. err = mtdtest_relax();
  363. if (err)
  364. goto out;
  365. }
  366. pr_info("verified %u eraseblocks\n", i);
  367. err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
  368. if (err)
  369. goto out;
  370. err = verify_all_eraseblocks_ff();
  371. if (err)
  372. goto out;
  373. pr_info("finished with %d errors\n", errcnt);
  374. out:
  375. kfree(bbt);
  376. kfree(readbuf);
  377. kfree(writebuf);
  378. put_mtd_device(mtd);
  379. if (err)
  380. pr_info("error %d occurred\n", err);
  381. printk(KERN_INFO "=================================================\n");
  382. return err;
  383. }
  384. module_init(mtd_subpagetest_init);
  385. static void __exit mtd_subpagetest_exit(void)
  386. {
  387. return;
  388. }
  389. module_exit(mtd_subpagetest_exit);
  390. MODULE_DESCRIPTION("Subpage test module");
  391. MODULE_AUTHOR("Adrian Hunter");
  392. MODULE_LICENSE("GPL");