mtd_subpagetest.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. static int dev = -EINVAL;
  31. module_param(dev, int, S_IRUGO);
  32. MODULE_PARM_DESC(dev, "MTD device number to use");
  33. static struct mtd_info *mtd;
  34. static unsigned char *writebuf;
  35. static unsigned char *readbuf;
  36. static unsigned char *bbt;
  37. static int subpgsize;
  38. static int bufsize;
  39. static int ebcnt;
  40. static int pgcnt;
  41. static int errcnt;
  42. static struct rnd_state rnd_state;
  43. static inline void clear_data(unsigned char *buf, size_t len)
  44. {
  45. memset(buf, 0, len);
  46. }
  47. static int erase_eraseblock(int ebnum)
  48. {
  49. int err;
  50. struct erase_info ei;
  51. loff_t addr = ebnum * mtd->erasesize;
  52. memset(&ei, 0, sizeof(struct erase_info));
  53. ei.mtd = mtd;
  54. ei.addr = addr;
  55. ei.len = mtd->erasesize;
  56. err = mtd_erase(mtd, &ei);
  57. if (err) {
  58. pr_err("error %d while erasing EB %d\n", err, ebnum);
  59. return err;
  60. }
  61. if (ei.state == MTD_ERASE_FAILED) {
  62. pr_err("some erase error occurred at EB %d\n",
  63. ebnum);
  64. return -EIO;
  65. }
  66. return 0;
  67. }
  68. static int erase_whole_device(void)
  69. {
  70. int err;
  71. unsigned int i;
  72. pr_info("erasing whole device\n");
  73. for (i = 0; i < ebcnt; ++i) {
  74. if (bbt[i])
  75. continue;
  76. err = erase_eraseblock(i);
  77. if (err)
  78. return err;
  79. cond_resched();
  80. }
  81. pr_info("erased %u eraseblocks\n", i);
  82. return 0;
  83. }
  84. static int write_eraseblock(int ebnum)
  85. {
  86. size_t written;
  87. int err = 0;
  88. loff_t addr = ebnum * mtd->erasesize;
  89. prandom_bytes_state(&rnd_state, writebuf, subpgsize);
  90. err = mtd_write(mtd, addr, subpgsize, &written, writebuf);
  91. if (unlikely(err || written != subpgsize)) {
  92. pr_err("error: write failed at %#llx\n",
  93. (long long)addr);
  94. if (written != subpgsize) {
  95. pr_err(" write size: %#x\n", subpgsize);
  96. pr_err(" written: %#zx\n", written);
  97. }
  98. return err ? err : -1;
  99. }
  100. addr += subpgsize;
  101. prandom_bytes_state(&rnd_state, writebuf, subpgsize);
  102. err = mtd_write(mtd, addr, subpgsize, &written, writebuf);
  103. if (unlikely(err || written != subpgsize)) {
  104. pr_err("error: write failed at %#llx\n",
  105. (long long)addr);
  106. if (written != subpgsize) {
  107. pr_err(" write size: %#x\n", subpgsize);
  108. pr_err(" written: %#zx\n", written);
  109. }
  110. return err ? err : -1;
  111. }
  112. return err;
  113. }
  114. static int write_eraseblock2(int ebnum)
  115. {
  116. size_t written;
  117. int err = 0, k;
  118. loff_t addr = ebnum * mtd->erasesize;
  119. for (k = 1; k < 33; ++k) {
  120. if (addr + (subpgsize * k) > (ebnum + 1) * mtd->erasesize)
  121. break;
  122. prandom_bytes_state(&rnd_state, writebuf, subpgsize * k);
  123. err = mtd_write(mtd, addr, subpgsize * k, &written, writebuf);
  124. if (unlikely(err || written != subpgsize * k)) {
  125. pr_err("error: write failed at %#llx\n",
  126. (long long)addr);
  127. if (written != subpgsize) {
  128. pr_err(" write size: %#x\n",
  129. subpgsize * k);
  130. pr_err(" written: %#08zx\n",
  131. written);
  132. }
  133. return err ? err : -1;
  134. }
  135. addr += subpgsize * k;
  136. }
  137. return err;
  138. }
  139. static void print_subpage(unsigned char *p)
  140. {
  141. int i, j;
  142. for (i = 0; i < subpgsize; ) {
  143. for (j = 0; i < subpgsize && j < 32; ++i, ++j)
  144. printk("%02x", *p++);
  145. printk("\n");
  146. }
  147. }
  148. static int verify_eraseblock(int ebnum)
  149. {
  150. size_t read;
  151. int err = 0;
  152. loff_t addr = ebnum * mtd->erasesize;
  153. prandom_bytes_state(&rnd_state, writebuf, subpgsize);
  154. clear_data(readbuf, subpgsize);
  155. err = mtd_read(mtd, addr, subpgsize, &read, readbuf);
  156. if (unlikely(err || read != subpgsize)) {
  157. if (mtd_is_bitflip(err) && read == subpgsize) {
  158. pr_info("ECC correction at %#llx\n",
  159. (long long)addr);
  160. err = 0;
  161. } else {
  162. pr_err("error: read failed at %#llx\n",
  163. (long long)addr);
  164. return err ? err : -1;
  165. }
  166. }
  167. if (unlikely(memcmp(readbuf, writebuf, subpgsize))) {
  168. pr_err("error: verify failed at %#llx\n",
  169. (long long)addr);
  170. pr_info("------------- written----------------\n");
  171. print_subpage(writebuf);
  172. pr_info("------------- read ------------------\n");
  173. print_subpage(readbuf);
  174. pr_info("-------------------------------------\n");
  175. errcnt += 1;
  176. }
  177. addr += subpgsize;
  178. prandom_bytes_state(&rnd_state, writebuf, subpgsize);
  179. clear_data(readbuf, subpgsize);
  180. err = mtd_read(mtd, addr, subpgsize, &read, readbuf);
  181. if (unlikely(err || read != subpgsize)) {
  182. if (mtd_is_bitflip(err) && read == subpgsize) {
  183. pr_info("ECC correction at %#llx\n",
  184. (long long)addr);
  185. err = 0;
  186. } else {
  187. pr_err("error: read failed at %#llx\n",
  188. (long long)addr);
  189. return err ? err : -1;
  190. }
  191. }
  192. if (unlikely(memcmp(readbuf, writebuf, subpgsize))) {
  193. pr_info("error: verify failed at %#llx\n",
  194. (long long)addr);
  195. pr_info("------------- written----------------\n");
  196. print_subpage(writebuf);
  197. pr_info("------------- read ------------------\n");
  198. print_subpage(readbuf);
  199. pr_info("-------------------------------------\n");
  200. errcnt += 1;
  201. }
  202. return err;
  203. }
  204. static int verify_eraseblock2(int ebnum)
  205. {
  206. size_t read;
  207. int err = 0, k;
  208. loff_t addr = ebnum * mtd->erasesize;
  209. for (k = 1; k < 33; ++k) {
  210. if (addr + (subpgsize * k) > (ebnum + 1) * mtd->erasesize)
  211. break;
  212. prandom_bytes_state(&rnd_state, writebuf, subpgsize * k);
  213. clear_data(readbuf, subpgsize * k);
  214. err = mtd_read(mtd, addr, subpgsize * k, &read, readbuf);
  215. if (unlikely(err || read != subpgsize * k)) {
  216. if (mtd_is_bitflip(err) && read == subpgsize * k) {
  217. pr_info("ECC correction at %#llx\n",
  218. (long long)addr);
  219. err = 0;
  220. } else {
  221. pr_err("error: read failed at "
  222. "%#llx\n", (long long)addr);
  223. return err ? err : -1;
  224. }
  225. }
  226. if (unlikely(memcmp(readbuf, writebuf, subpgsize * k))) {
  227. pr_err("error: verify failed at %#llx\n",
  228. (long long)addr);
  229. errcnt += 1;
  230. }
  231. addr += subpgsize * k;
  232. }
  233. return err;
  234. }
  235. static int verify_eraseblock_ff(int ebnum)
  236. {
  237. uint32_t j;
  238. size_t read;
  239. int err = 0;
  240. loff_t addr = ebnum * mtd->erasesize;
  241. memset(writebuf, 0xff, subpgsize);
  242. for (j = 0; j < mtd->erasesize / subpgsize; ++j) {
  243. clear_data(readbuf, subpgsize);
  244. err = mtd_read(mtd, addr, subpgsize, &read, readbuf);
  245. if (unlikely(err || read != subpgsize)) {
  246. if (mtd_is_bitflip(err) && read == subpgsize) {
  247. pr_info("ECC correction at %#llx\n",
  248. (long long)addr);
  249. err = 0;
  250. } else {
  251. pr_err("error: read failed at "
  252. "%#llx\n", (long long)addr);
  253. return err ? err : -1;
  254. }
  255. }
  256. if (unlikely(memcmp(readbuf, writebuf, subpgsize))) {
  257. pr_err("error: verify 0xff failed at "
  258. "%#llx\n", (long long)addr);
  259. errcnt += 1;
  260. }
  261. addr += subpgsize;
  262. }
  263. return err;
  264. }
  265. static int verify_all_eraseblocks_ff(void)
  266. {
  267. int err;
  268. unsigned int i;
  269. pr_info("verifying all eraseblocks for 0xff\n");
  270. for (i = 0; i < ebcnt; ++i) {
  271. if (bbt[i])
  272. continue;
  273. err = verify_eraseblock_ff(i);
  274. if (err)
  275. return err;
  276. if (i % 256 == 0)
  277. pr_info("verified up to eraseblock %u\n", i);
  278. cond_resched();
  279. }
  280. pr_info("verified %u eraseblocks\n", i);
  281. return 0;
  282. }
  283. static int is_block_bad(int ebnum)
  284. {
  285. loff_t addr = ebnum * mtd->erasesize;
  286. int ret;
  287. ret = mtd_block_isbad(mtd, addr);
  288. if (ret)
  289. pr_info("block %d is bad\n", ebnum);
  290. return ret;
  291. }
  292. static int scan_for_bad_eraseblocks(void)
  293. {
  294. int i, bad = 0;
  295. bbt = kzalloc(ebcnt, GFP_KERNEL);
  296. if (!bbt) {
  297. pr_err("error: cannot allocate memory\n");
  298. return -ENOMEM;
  299. }
  300. pr_info("scanning for bad eraseblocks\n");
  301. for (i = 0; i < ebcnt; ++i) {
  302. bbt[i] = is_block_bad(i) ? 1 : 0;
  303. if (bbt[i])
  304. bad += 1;
  305. cond_resched();
  306. }
  307. pr_info("scanned %d eraseblocks, %d are bad\n", i, bad);
  308. return 0;
  309. }
  310. static int __init mtd_subpagetest_init(void)
  311. {
  312. int err = 0;
  313. uint32_t i;
  314. uint64_t tmp;
  315. printk(KERN_INFO "\n");
  316. printk(KERN_INFO "=================================================\n");
  317. if (dev < 0) {
  318. pr_info("Please specify a valid mtd-device via module parameter\n");
  319. pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
  320. return -EINVAL;
  321. }
  322. pr_info("MTD device: %d\n", dev);
  323. mtd = get_mtd_device(NULL, dev);
  324. if (IS_ERR(mtd)) {
  325. err = PTR_ERR(mtd);
  326. pr_err("error: cannot get MTD device\n");
  327. return err;
  328. }
  329. if (mtd->type != MTD_NANDFLASH) {
  330. pr_info("this test requires NAND flash\n");
  331. goto out;
  332. }
  333. subpgsize = mtd->writesize >> mtd->subpage_sft;
  334. tmp = mtd->size;
  335. do_div(tmp, mtd->erasesize);
  336. ebcnt = tmp;
  337. pgcnt = mtd->erasesize / mtd->writesize;
  338. pr_info("MTD device size %llu, eraseblock size %u, "
  339. "page size %u, subpage size %u, count of eraseblocks %u, "
  340. "pages per eraseblock %u, OOB size %u\n",
  341. (unsigned long long)mtd->size, mtd->erasesize,
  342. mtd->writesize, subpgsize, ebcnt, pgcnt, mtd->oobsize);
  343. err = -ENOMEM;
  344. bufsize = subpgsize * 32;
  345. writebuf = kmalloc(bufsize, GFP_KERNEL);
  346. if (!writebuf) {
  347. pr_info("error: cannot allocate memory\n");
  348. goto out;
  349. }
  350. readbuf = kmalloc(bufsize, GFP_KERNEL);
  351. if (!readbuf) {
  352. pr_info("error: cannot allocate memory\n");
  353. goto out;
  354. }
  355. err = scan_for_bad_eraseblocks();
  356. if (err)
  357. goto out;
  358. err = erase_whole_device();
  359. if (err)
  360. goto out;
  361. pr_info("writing whole device\n");
  362. prandom_seed_state(&rnd_state, 1);
  363. for (i = 0; i < ebcnt; ++i) {
  364. if (bbt[i])
  365. continue;
  366. err = write_eraseblock(i);
  367. if (unlikely(err))
  368. goto out;
  369. if (i % 256 == 0)
  370. pr_info("written up to eraseblock %u\n", i);
  371. cond_resched();
  372. }
  373. pr_info("written %u eraseblocks\n", i);
  374. prandom_seed_state(&rnd_state, 1);
  375. pr_info("verifying all eraseblocks\n");
  376. for (i = 0; i < ebcnt; ++i) {
  377. if (bbt[i])
  378. continue;
  379. err = verify_eraseblock(i);
  380. if (unlikely(err))
  381. goto out;
  382. if (i % 256 == 0)
  383. pr_info("verified up to eraseblock %u\n", i);
  384. cond_resched();
  385. }
  386. pr_info("verified %u eraseblocks\n", i);
  387. err = erase_whole_device();
  388. if (err)
  389. goto out;
  390. err = verify_all_eraseblocks_ff();
  391. if (err)
  392. goto out;
  393. /* Write all eraseblocks */
  394. prandom_seed_state(&rnd_state, 3);
  395. pr_info("writing whole device\n");
  396. for (i = 0; i < ebcnt; ++i) {
  397. if (bbt[i])
  398. continue;
  399. err = write_eraseblock2(i);
  400. if (unlikely(err))
  401. goto out;
  402. if (i % 256 == 0)
  403. pr_info("written up to eraseblock %u\n", i);
  404. cond_resched();
  405. }
  406. pr_info("written %u eraseblocks\n", i);
  407. /* Check all eraseblocks */
  408. prandom_seed_state(&rnd_state, 3);
  409. pr_info("verifying all eraseblocks\n");
  410. for (i = 0; i < ebcnt; ++i) {
  411. if (bbt[i])
  412. continue;
  413. err = verify_eraseblock2(i);
  414. if (unlikely(err))
  415. goto out;
  416. if (i % 256 == 0)
  417. pr_info("verified up to eraseblock %u\n", i);
  418. cond_resched();
  419. }
  420. pr_info("verified %u eraseblocks\n", i);
  421. err = erase_whole_device();
  422. if (err)
  423. goto out;
  424. err = verify_all_eraseblocks_ff();
  425. if (err)
  426. goto out;
  427. pr_info("finished with %d errors\n", errcnt);
  428. out:
  429. kfree(bbt);
  430. kfree(readbuf);
  431. kfree(writebuf);
  432. put_mtd_device(mtd);
  433. if (err)
  434. pr_info("error %d occurred\n", err);
  435. printk(KERN_INFO "=================================================\n");
  436. return err;
  437. }
  438. module_init(mtd_subpagetest_init);
  439. static void __exit mtd_subpagetest_exit(void)
  440. {
  441. return;
  442. }
  443. module_exit(mtd_subpagetest_exit);
  444. MODULE_DESCRIPTION("Subpage test module");
  445. MODULE_AUTHOR("Adrian Hunter");
  446. MODULE_LICENSE("GPL");