pagetest.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * Copyright (C) 2006-2008 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 page read and write on MTD device.
  18. *
  19. * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <asm/div64.h>
  23. #include <linux/init.h>
  24. #include <linux/module.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/err.h>
  27. #include <linux/mtd/mtd.h>
  28. #include <linux/slab.h>
  29. #include <linux/sched.h>
  30. #include <linux/random.h>
  31. #include "mtd_test.h"
  32. static int dev = -EINVAL;
  33. module_param(dev, int, S_IRUGO);
  34. MODULE_PARM_DESC(dev, "MTD device number to use");
  35. static struct mtd_info *mtd;
  36. static unsigned char *twopages;
  37. static unsigned char *writebuf;
  38. static unsigned char *boundary;
  39. static unsigned char *bbt;
  40. static int pgsize;
  41. static int bufsize;
  42. static int ebcnt;
  43. static int pgcnt;
  44. static int errcnt;
  45. static struct rnd_state rnd_state;
  46. static int write_eraseblock(int ebnum)
  47. {
  48. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  49. prandom_bytes_state(&rnd_state, writebuf, mtd->erasesize);
  50. cond_resched();
  51. return mtdtest_write(mtd, addr, mtd->erasesize, writebuf);
  52. }
  53. static int verify_eraseblock(int ebnum)
  54. {
  55. uint32_t j;
  56. int err = 0, i;
  57. loff_t addr0, addrn;
  58. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  59. addr0 = 0;
  60. for (i = 0; i < ebcnt && bbt[i]; ++i)
  61. addr0 += mtd->erasesize;
  62. addrn = mtd->size;
  63. for (i = 0; i < ebcnt && bbt[ebcnt - i - 1]; ++i)
  64. addrn -= mtd->erasesize;
  65. prandom_bytes_state(&rnd_state, writebuf, mtd->erasesize);
  66. for (j = 0; j < pgcnt - 1; ++j, addr += pgsize) {
  67. /* Do a read to set the internal dataRAMs to different data */
  68. err = mtdtest_read(mtd, addr0, bufsize, twopages);
  69. if (err)
  70. return err;
  71. err = mtdtest_read(mtd, addrn - bufsize, bufsize, twopages);
  72. if (err)
  73. return err;
  74. memset(twopages, 0, bufsize);
  75. err = mtdtest_read(mtd, addr, bufsize, twopages);
  76. if (err)
  77. break;
  78. if (memcmp(twopages, writebuf + (j * pgsize), bufsize)) {
  79. pr_err("error: verify failed at %#llx\n",
  80. (long long)addr);
  81. errcnt += 1;
  82. }
  83. }
  84. /* Check boundary between eraseblocks */
  85. if (addr <= addrn - pgsize - pgsize && !bbt[ebnum + 1]) {
  86. struct rnd_state old_state = rnd_state;
  87. /* Do a read to set the internal dataRAMs to different data */
  88. err = mtdtest_read(mtd, addr0, bufsize, twopages);
  89. if (err)
  90. return err;
  91. err = mtdtest_read(mtd, addrn - bufsize, bufsize, twopages);
  92. if (err)
  93. return err;
  94. memset(twopages, 0, bufsize);
  95. err = mtdtest_read(mtd, addr, bufsize, twopages);
  96. if (err)
  97. return err;
  98. memcpy(boundary, writebuf + mtd->erasesize - pgsize, pgsize);
  99. prandom_bytes_state(&rnd_state, boundary + pgsize, pgsize);
  100. if (memcmp(twopages, boundary, bufsize)) {
  101. pr_err("error: verify failed at %#llx\n",
  102. (long long)addr);
  103. errcnt += 1;
  104. }
  105. rnd_state = old_state;
  106. }
  107. return err;
  108. }
  109. static int crosstest(void)
  110. {
  111. int err = 0, i;
  112. loff_t addr, addr0, addrn;
  113. unsigned char *pp1, *pp2, *pp3, *pp4;
  114. pr_info("crosstest\n");
  115. pp1 = kzalloc(pgsize * 4, GFP_KERNEL);
  116. if (!pp1)
  117. return -ENOMEM;
  118. pp2 = pp1 + pgsize;
  119. pp3 = pp2 + pgsize;
  120. pp4 = pp3 + pgsize;
  121. addr0 = 0;
  122. for (i = 0; i < ebcnt && bbt[i]; ++i)
  123. addr0 += mtd->erasesize;
  124. addrn = mtd->size;
  125. for (i = 0; i < ebcnt && bbt[ebcnt - i - 1]; ++i)
  126. addrn -= mtd->erasesize;
  127. /* Read 2nd-to-last page to pp1 */
  128. addr = addrn - pgsize - pgsize;
  129. err = mtdtest_read(mtd, addr, pgsize, pp1);
  130. if (err) {
  131. kfree(pp1);
  132. return err;
  133. }
  134. /* Read 3rd-to-last page to pp1 */
  135. addr = addrn - pgsize - pgsize - pgsize;
  136. err = mtdtest_read(mtd, addr, pgsize, pp1);
  137. if (err) {
  138. kfree(pp1);
  139. return err;
  140. }
  141. /* Read first page to pp2 */
  142. addr = addr0;
  143. pr_info("reading page at %#llx\n", (long long)addr);
  144. err = mtdtest_read(mtd, addr, pgsize, pp2);
  145. if (err) {
  146. kfree(pp1);
  147. return err;
  148. }
  149. /* Read last page to pp3 */
  150. addr = addrn - pgsize;
  151. pr_info("reading page at %#llx\n", (long long)addr);
  152. err = mtdtest_read(mtd, addr, pgsize, pp3);
  153. if (err) {
  154. kfree(pp1);
  155. return err;
  156. }
  157. /* Read first page again to pp4 */
  158. addr = addr0;
  159. pr_info("reading page at %#llx\n", (long long)addr);
  160. err = mtdtest_read(mtd, addr, pgsize, pp4);
  161. if (err) {
  162. kfree(pp1);
  163. return err;
  164. }
  165. /* pp2 and pp4 should be the same */
  166. pr_info("verifying pages read at %#llx match\n",
  167. (long long)addr0);
  168. if (memcmp(pp2, pp4, pgsize)) {
  169. pr_err("verify failed!\n");
  170. errcnt += 1;
  171. } else if (!err)
  172. pr_info("crosstest ok\n");
  173. kfree(pp1);
  174. return err;
  175. }
  176. static int erasecrosstest(void)
  177. {
  178. int err = 0, i, ebnum, ebnum2;
  179. loff_t addr0;
  180. char *readbuf = twopages;
  181. pr_info("erasecrosstest\n");
  182. ebnum = 0;
  183. addr0 = 0;
  184. for (i = 0; i < ebcnt && bbt[i]; ++i) {
  185. addr0 += mtd->erasesize;
  186. ebnum += 1;
  187. }
  188. ebnum2 = ebcnt - 1;
  189. while (ebnum2 && bbt[ebnum2])
  190. ebnum2 -= 1;
  191. pr_info("erasing block %d\n", ebnum);
  192. err = mtdtest_erase_eraseblock(mtd, ebnum);
  193. if (err)
  194. return err;
  195. pr_info("writing 1st page of block %d\n", ebnum);
  196. prandom_bytes_state(&rnd_state, writebuf, pgsize);
  197. strcpy(writebuf, "There is no data like this!");
  198. err = mtdtest_write(mtd, addr0, pgsize, writebuf);
  199. if (err)
  200. return err;
  201. pr_info("reading 1st page of block %d\n", ebnum);
  202. memset(readbuf, 0, pgsize);
  203. err = mtdtest_read(mtd, addr0, pgsize, readbuf);
  204. if (err)
  205. return err;
  206. pr_info("verifying 1st page of block %d\n", ebnum);
  207. if (memcmp(writebuf, readbuf, pgsize)) {
  208. pr_err("verify failed!\n");
  209. errcnt += 1;
  210. return -1;
  211. }
  212. pr_info("erasing block %d\n", ebnum);
  213. err = mtdtest_erase_eraseblock(mtd, ebnum);
  214. if (err)
  215. return err;
  216. pr_info("writing 1st page of block %d\n", ebnum);
  217. prandom_bytes_state(&rnd_state, writebuf, pgsize);
  218. strcpy(writebuf, "There is no data like this!");
  219. err = mtdtest_write(mtd, addr0, pgsize, writebuf);
  220. if (err)
  221. return err;
  222. pr_info("erasing block %d\n", ebnum2);
  223. err = mtdtest_erase_eraseblock(mtd, ebnum2);
  224. if (err)
  225. return err;
  226. pr_info("reading 1st page of block %d\n", ebnum);
  227. memset(readbuf, 0, pgsize);
  228. err = mtdtest_read(mtd, addr0, pgsize, readbuf);
  229. if (err)
  230. return err;
  231. pr_info("verifying 1st page of block %d\n", ebnum);
  232. if (memcmp(writebuf, readbuf, pgsize)) {
  233. pr_err("verify failed!\n");
  234. errcnt += 1;
  235. return -1;
  236. }
  237. if (!err)
  238. pr_info("erasecrosstest ok\n");
  239. return err;
  240. }
  241. static int erasetest(void)
  242. {
  243. int err = 0, i, ebnum, ok = 1;
  244. loff_t addr0;
  245. pr_info("erasetest\n");
  246. ebnum = 0;
  247. addr0 = 0;
  248. for (i = 0; i < ebcnt && bbt[i]; ++i) {
  249. addr0 += mtd->erasesize;
  250. ebnum += 1;
  251. }
  252. pr_info("erasing block %d\n", ebnum);
  253. err = mtdtest_erase_eraseblock(mtd, ebnum);
  254. if (err)
  255. return err;
  256. pr_info("writing 1st page of block %d\n", ebnum);
  257. prandom_bytes_state(&rnd_state, writebuf, pgsize);
  258. err = mtdtest_write(mtd, addr0, pgsize, writebuf);
  259. if (err)
  260. return err;
  261. pr_info("erasing block %d\n", ebnum);
  262. err = mtdtest_erase_eraseblock(mtd, ebnum);
  263. if (err)
  264. return err;
  265. pr_info("reading 1st page of block %d\n", ebnum);
  266. err = mtdtest_read(mtd, addr0, pgsize, twopages);
  267. if (err)
  268. return err;
  269. pr_info("verifying 1st page of block %d is all 0xff\n",
  270. ebnum);
  271. for (i = 0; i < pgsize; ++i)
  272. if (twopages[i] != 0xff) {
  273. pr_err("verifying all 0xff failed at %d\n",
  274. i);
  275. errcnt += 1;
  276. ok = 0;
  277. break;
  278. }
  279. if (ok && !err)
  280. pr_info("erasetest ok\n");
  281. return err;
  282. }
  283. static int __init mtd_pagetest_init(void)
  284. {
  285. int err = 0;
  286. uint64_t tmp;
  287. uint32_t i;
  288. printk(KERN_INFO "\n");
  289. printk(KERN_INFO "=================================================\n");
  290. if (dev < 0) {
  291. pr_info("Please specify a valid mtd-device via module parameter\n");
  292. pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
  293. return -EINVAL;
  294. }
  295. pr_info("MTD device: %d\n", dev);
  296. mtd = get_mtd_device(NULL, dev);
  297. if (IS_ERR(mtd)) {
  298. err = PTR_ERR(mtd);
  299. pr_err("error: cannot get MTD device\n");
  300. return err;
  301. }
  302. if (!mtd_type_is_nand(mtd)) {
  303. pr_info("this test requires NAND flash\n");
  304. goto out;
  305. }
  306. tmp = mtd->size;
  307. do_div(tmp, mtd->erasesize);
  308. ebcnt = tmp;
  309. pgcnt = mtd->erasesize / mtd->writesize;
  310. pgsize = mtd->writesize;
  311. pr_info("MTD device size %llu, eraseblock size %u, "
  312. "page size %u, count of eraseblocks %u, pages per "
  313. "eraseblock %u, OOB size %u\n",
  314. (unsigned long long)mtd->size, mtd->erasesize,
  315. pgsize, ebcnt, pgcnt, mtd->oobsize);
  316. err = -ENOMEM;
  317. bufsize = pgsize * 2;
  318. writebuf = kmalloc(mtd->erasesize, GFP_KERNEL);
  319. if (!writebuf)
  320. goto out;
  321. twopages = kmalloc(bufsize, GFP_KERNEL);
  322. if (!twopages)
  323. goto out;
  324. boundary = kmalloc(bufsize, GFP_KERNEL);
  325. if (!boundary)
  326. goto out;
  327. bbt = kzalloc(ebcnt, GFP_KERNEL);
  328. if (!bbt)
  329. goto out;
  330. err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
  331. if (err)
  332. goto out;
  333. /* Erase all eraseblocks */
  334. pr_info("erasing whole device\n");
  335. err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
  336. if (err)
  337. goto out;
  338. pr_info("erased %u eraseblocks\n", ebcnt);
  339. /* Write all eraseblocks */
  340. prandom_seed_state(&rnd_state, 1);
  341. pr_info("writing whole device\n");
  342. for (i = 0; i < ebcnt; ++i) {
  343. if (bbt[i])
  344. continue;
  345. err = write_eraseblock(i);
  346. if (err)
  347. goto out;
  348. if (i % 256 == 0)
  349. pr_info("written up to eraseblock %u\n", i);
  350. err = mtdtest_relax();
  351. if (err)
  352. goto out;
  353. }
  354. pr_info("written %u eraseblocks\n", i);
  355. /* Check all eraseblocks */
  356. prandom_seed_state(&rnd_state, 1);
  357. pr_info("verifying all eraseblocks\n");
  358. for (i = 0; i < ebcnt; ++i) {
  359. if (bbt[i])
  360. continue;
  361. err = verify_eraseblock(i);
  362. if (err)
  363. goto out;
  364. if (i % 256 == 0)
  365. pr_info("verified up to eraseblock %u\n", i);
  366. err = mtdtest_relax();
  367. if (err)
  368. goto out;
  369. }
  370. pr_info("verified %u eraseblocks\n", i);
  371. err = crosstest();
  372. if (err)
  373. goto out;
  374. err = erasecrosstest();
  375. if (err)
  376. goto out;
  377. err = erasetest();
  378. if (err)
  379. goto out;
  380. pr_info("finished with %d errors\n", errcnt);
  381. out:
  382. kfree(bbt);
  383. kfree(boundary);
  384. kfree(twopages);
  385. kfree(writebuf);
  386. put_mtd_device(mtd);
  387. if (err)
  388. pr_info("error %d occurred\n", err);
  389. printk(KERN_INFO "=================================================\n");
  390. return err;
  391. }
  392. module_init(mtd_pagetest_init);
  393. static void __exit mtd_pagetest_exit(void)
  394. {
  395. return;
  396. }
  397. module_exit(mtd_pagetest_exit);
  398. MODULE_DESCRIPTION("NAND page test");
  399. MODULE_AUTHOR("Adrian Hunter");
  400. MODULE_LICENSE("GPL");