subpagetest.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 = 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 = ebnum * mtd->erasesize;
  83. for (k = 1; k < 33; ++k) {
  84. if (addr + (subpgsize * k) > (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 = 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 = ebnum * mtd->erasesize;
  173. for (k = 1; k < 33; ++k) {
  174. if (addr + (subpgsize * k) > (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 = 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. cond_resched();
  243. }
  244. pr_info("verified %u eraseblocks\n", i);
  245. return 0;
  246. }
  247. static int __init mtd_subpagetest_init(void)
  248. {
  249. int err = 0;
  250. uint32_t i;
  251. uint64_t tmp;
  252. printk(KERN_INFO "\n");
  253. printk(KERN_INFO "=================================================\n");
  254. if (dev < 0) {
  255. pr_info("Please specify a valid mtd-device via module parameter\n");
  256. pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
  257. return -EINVAL;
  258. }
  259. pr_info("MTD device: %d\n", dev);
  260. mtd = get_mtd_device(NULL, dev);
  261. if (IS_ERR(mtd)) {
  262. err = PTR_ERR(mtd);
  263. pr_err("error: cannot get MTD device\n");
  264. return err;
  265. }
  266. if (mtd->type != MTD_NANDFLASH) {
  267. pr_info("this test requires NAND flash\n");
  268. goto out;
  269. }
  270. subpgsize = mtd->writesize >> mtd->subpage_sft;
  271. tmp = mtd->size;
  272. do_div(tmp, mtd->erasesize);
  273. ebcnt = tmp;
  274. pgcnt = mtd->erasesize / mtd->writesize;
  275. pr_info("MTD device size %llu, eraseblock size %u, "
  276. "page size %u, subpage size %u, count of eraseblocks %u, "
  277. "pages per eraseblock %u, OOB size %u\n",
  278. (unsigned long long)mtd->size, mtd->erasesize,
  279. mtd->writesize, subpgsize, ebcnt, pgcnt, mtd->oobsize);
  280. err = -ENOMEM;
  281. bufsize = subpgsize * 32;
  282. writebuf = kmalloc(bufsize, GFP_KERNEL);
  283. if (!writebuf)
  284. goto out;
  285. readbuf = kmalloc(bufsize, GFP_KERNEL);
  286. if (!readbuf)
  287. goto out;
  288. bbt = kzalloc(ebcnt, GFP_KERNEL);
  289. if (!bbt)
  290. goto out;
  291. err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
  292. if (err)
  293. goto out;
  294. err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
  295. if (err)
  296. goto out;
  297. pr_info("writing whole device\n");
  298. prandom_seed_state(&rnd_state, 1);
  299. for (i = 0; i < ebcnt; ++i) {
  300. if (bbt[i])
  301. continue;
  302. err = write_eraseblock(i);
  303. if (unlikely(err))
  304. goto out;
  305. if (i % 256 == 0)
  306. pr_info("written up to eraseblock %u\n", i);
  307. cond_resched();
  308. }
  309. pr_info("written %u eraseblocks\n", i);
  310. prandom_seed_state(&rnd_state, 1);
  311. pr_info("verifying all eraseblocks\n");
  312. for (i = 0; i < ebcnt; ++i) {
  313. if (bbt[i])
  314. continue;
  315. err = verify_eraseblock(i);
  316. if (unlikely(err))
  317. goto out;
  318. if (i % 256 == 0)
  319. pr_info("verified up to eraseblock %u\n", i);
  320. cond_resched();
  321. }
  322. pr_info("verified %u eraseblocks\n", i);
  323. err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
  324. if (err)
  325. goto out;
  326. err = verify_all_eraseblocks_ff();
  327. if (err)
  328. goto out;
  329. /* Write all eraseblocks */
  330. prandom_seed_state(&rnd_state, 3);
  331. pr_info("writing whole device\n");
  332. for (i = 0; i < ebcnt; ++i) {
  333. if (bbt[i])
  334. continue;
  335. err = write_eraseblock2(i);
  336. if (unlikely(err))
  337. goto out;
  338. if (i % 256 == 0)
  339. pr_info("written up to eraseblock %u\n", i);
  340. cond_resched();
  341. }
  342. pr_info("written %u eraseblocks\n", i);
  343. /* Check all eraseblocks */
  344. prandom_seed_state(&rnd_state, 3);
  345. pr_info("verifying all eraseblocks\n");
  346. for (i = 0; i < ebcnt; ++i) {
  347. if (bbt[i])
  348. continue;
  349. err = verify_eraseblock2(i);
  350. if (unlikely(err))
  351. goto out;
  352. if (i % 256 == 0)
  353. pr_info("verified up to eraseblock %u\n", i);
  354. cond_resched();
  355. }
  356. pr_info("verified %u eraseblocks\n", i);
  357. err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
  358. if (err)
  359. goto out;
  360. err = verify_all_eraseblocks_ff();
  361. if (err)
  362. goto out;
  363. pr_info("finished with %d errors\n", errcnt);
  364. out:
  365. kfree(bbt);
  366. kfree(readbuf);
  367. kfree(writebuf);
  368. put_mtd_device(mtd);
  369. if (err)
  370. pr_info("error %d occurred\n", err);
  371. printk(KERN_INFO "=================================================\n");
  372. return err;
  373. }
  374. module_init(mtd_subpagetest_init);
  375. static void __exit mtd_subpagetest_exit(void)
  376. {
  377. return;
  378. }
  379. module_exit(mtd_subpagetest_exit);
  380. MODULE_DESCRIPTION("Subpage test module");
  381. MODULE_AUTHOR("Adrian Hunter");
  382. MODULE_LICENSE("GPL");