mtd_stresstest.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 random reads, writes and erases on MTD device.
  18. *
  19. * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
  20. */
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/err.h>
  25. #include <linux/mtd/mtd.h>
  26. #include <linux/slab.h>
  27. #include <linux/sched.h>
  28. #include <linux/vmalloc.h>
  29. #define PRINT_PREF KERN_INFO "mtd_stresstest: "
  30. static int dev = -EINVAL;
  31. module_param(dev, int, S_IRUGO);
  32. MODULE_PARM_DESC(dev, "MTD device number to use");
  33. static int count = 10000;
  34. module_param(count, int, S_IRUGO);
  35. MODULE_PARM_DESC(count, "Number of operations to do (default is 10000)");
  36. static struct mtd_info *mtd;
  37. static unsigned char *writebuf;
  38. static unsigned char *readbuf;
  39. static unsigned char *bbt;
  40. static int *offsets;
  41. static int pgsize;
  42. static int bufsize;
  43. static int ebcnt;
  44. static int pgcnt;
  45. static unsigned long next = 1;
  46. static inline unsigned int simple_rand(void)
  47. {
  48. next = next * 1103515245 + 12345;
  49. return (unsigned int)((next / 65536) % 32768);
  50. }
  51. static inline void simple_srand(unsigned long seed)
  52. {
  53. next = seed;
  54. }
  55. static int rand_eb(void)
  56. {
  57. int eb;
  58. again:
  59. if (ebcnt < 32768)
  60. eb = simple_rand();
  61. else
  62. eb = (simple_rand() << 15) | simple_rand();
  63. /* Read or write up 2 eraseblocks at a time - hence 'ebcnt - 1' */
  64. eb %= (ebcnt - 1);
  65. if (bbt[eb])
  66. goto again;
  67. return eb;
  68. }
  69. static int rand_offs(void)
  70. {
  71. int offs;
  72. if (bufsize < 32768)
  73. offs = simple_rand();
  74. else
  75. offs = (simple_rand() << 15) | simple_rand();
  76. offs %= bufsize;
  77. return offs;
  78. }
  79. static int rand_len(int offs)
  80. {
  81. int len;
  82. if (bufsize < 32768)
  83. len = simple_rand();
  84. else
  85. len = (simple_rand() << 15) | simple_rand();
  86. len %= (bufsize - offs);
  87. return len;
  88. }
  89. static int erase_eraseblock(int ebnum)
  90. {
  91. int err;
  92. struct erase_info ei;
  93. loff_t addr = ebnum * mtd->erasesize;
  94. memset(&ei, 0, sizeof(struct erase_info));
  95. ei.mtd = mtd;
  96. ei.addr = addr;
  97. ei.len = mtd->erasesize;
  98. err = mtd_erase(mtd, &ei);
  99. if (unlikely(err)) {
  100. printk(PRINT_PREF "error %d while erasing EB %d\n", err, ebnum);
  101. return err;
  102. }
  103. if (unlikely(ei.state == MTD_ERASE_FAILED)) {
  104. printk(PRINT_PREF "some erase error occurred at EB %d\n",
  105. ebnum);
  106. return -EIO;
  107. }
  108. return 0;
  109. }
  110. static int is_block_bad(int ebnum)
  111. {
  112. loff_t addr = ebnum * mtd->erasesize;
  113. int ret;
  114. ret = mtd_block_isbad(mtd, addr);
  115. if (ret)
  116. printk(PRINT_PREF "block %d is bad\n", ebnum);
  117. return ret;
  118. }
  119. static int do_read(void)
  120. {
  121. size_t read;
  122. int eb = rand_eb();
  123. int offs = rand_offs();
  124. int len = rand_len(offs), err;
  125. loff_t addr;
  126. if (bbt[eb + 1]) {
  127. if (offs >= mtd->erasesize)
  128. offs -= mtd->erasesize;
  129. if (offs + len > mtd->erasesize)
  130. len = mtd->erasesize - offs;
  131. }
  132. addr = eb * mtd->erasesize + offs;
  133. err = mtd_read(mtd, addr, len, &read, readbuf);
  134. if (mtd_is_bitflip(err))
  135. err = 0;
  136. if (unlikely(err || read != len)) {
  137. printk(PRINT_PREF "error: read failed at 0x%llx\n",
  138. (long long)addr);
  139. if (!err)
  140. err = -EINVAL;
  141. return err;
  142. }
  143. return 0;
  144. }
  145. static int do_write(void)
  146. {
  147. int eb = rand_eb(), offs, err, len;
  148. size_t written;
  149. loff_t addr;
  150. offs = offsets[eb];
  151. if (offs >= mtd->erasesize) {
  152. err = erase_eraseblock(eb);
  153. if (err)
  154. return err;
  155. offs = offsets[eb] = 0;
  156. }
  157. len = rand_len(offs);
  158. len = ((len + pgsize - 1) / pgsize) * pgsize;
  159. if (offs + len > mtd->erasesize) {
  160. if (bbt[eb + 1])
  161. len = mtd->erasesize - offs;
  162. else {
  163. err = erase_eraseblock(eb + 1);
  164. if (err)
  165. return err;
  166. offsets[eb + 1] = 0;
  167. }
  168. }
  169. addr = eb * mtd->erasesize + offs;
  170. err = mtd_write(mtd, addr, len, &written, writebuf);
  171. if (unlikely(err || written != len)) {
  172. printk(PRINT_PREF "error: write failed at 0x%llx\n",
  173. (long long)addr);
  174. if (!err)
  175. err = -EINVAL;
  176. return err;
  177. }
  178. offs += len;
  179. while (offs > mtd->erasesize) {
  180. offsets[eb++] = mtd->erasesize;
  181. offs -= mtd->erasesize;
  182. }
  183. offsets[eb] = offs;
  184. return 0;
  185. }
  186. static int do_operation(void)
  187. {
  188. if (simple_rand() & 1)
  189. return do_read();
  190. else
  191. return do_write();
  192. }
  193. static int scan_for_bad_eraseblocks(void)
  194. {
  195. int i, bad = 0;
  196. bbt = kzalloc(ebcnt, GFP_KERNEL);
  197. if (!bbt) {
  198. printk(PRINT_PREF "error: cannot allocate memory\n");
  199. return -ENOMEM;
  200. }
  201. if (!mtd_can_have_bb(mtd))
  202. return 0;
  203. printk(PRINT_PREF "scanning for bad eraseblocks\n");
  204. for (i = 0; i < ebcnt; ++i) {
  205. bbt[i] = is_block_bad(i) ? 1 : 0;
  206. if (bbt[i])
  207. bad += 1;
  208. cond_resched();
  209. }
  210. printk(PRINT_PREF "scanned %d eraseblocks, %d are bad\n", i, bad);
  211. return 0;
  212. }
  213. static int __init mtd_stresstest_init(void)
  214. {
  215. int err;
  216. int i, op;
  217. uint64_t tmp;
  218. printk(KERN_INFO "\n");
  219. printk(KERN_INFO "=================================================\n");
  220. if (dev < 0) {
  221. printk(PRINT_PREF "Please specify a valid mtd-device via module paramter\n");
  222. printk(KERN_CRIT "CAREFUL: This test wipes all data on the specified MTD device!\n");
  223. return -EINVAL;
  224. }
  225. printk(PRINT_PREF "MTD device: %d\n", dev);
  226. mtd = get_mtd_device(NULL, dev);
  227. if (IS_ERR(mtd)) {
  228. err = PTR_ERR(mtd);
  229. printk(PRINT_PREF "error: cannot get MTD device\n");
  230. return err;
  231. }
  232. if (mtd->writesize == 1) {
  233. printk(PRINT_PREF "not NAND flash, assume page size is 512 "
  234. "bytes.\n");
  235. pgsize = 512;
  236. } else
  237. pgsize = mtd->writesize;
  238. tmp = mtd->size;
  239. do_div(tmp, mtd->erasesize);
  240. ebcnt = tmp;
  241. pgcnt = mtd->erasesize / pgsize;
  242. printk(PRINT_PREF "MTD device size %llu, eraseblock size %u, "
  243. "page size %u, count of eraseblocks %u, pages per "
  244. "eraseblock %u, OOB size %u\n",
  245. (unsigned long long)mtd->size, mtd->erasesize,
  246. pgsize, ebcnt, pgcnt, mtd->oobsize);
  247. if (ebcnt < 2) {
  248. printk(PRINT_PREF "error: need at least 2 eraseblocks\n");
  249. err = -ENOSPC;
  250. goto out_put_mtd;
  251. }
  252. /* Read or write up 2 eraseblocks at a time */
  253. bufsize = mtd->erasesize * 2;
  254. err = -ENOMEM;
  255. readbuf = vmalloc(bufsize);
  256. writebuf = vmalloc(bufsize);
  257. offsets = kmalloc(ebcnt * sizeof(int), GFP_KERNEL);
  258. if (!readbuf || !writebuf || !offsets) {
  259. printk(PRINT_PREF "error: cannot allocate memory\n");
  260. goto out;
  261. }
  262. for (i = 0; i < ebcnt; i++)
  263. offsets[i] = mtd->erasesize;
  264. simple_srand(current->pid);
  265. for (i = 0; i < bufsize; i++)
  266. writebuf[i] = simple_rand();
  267. err = scan_for_bad_eraseblocks();
  268. if (err)
  269. goto out;
  270. /* Do operations */
  271. printk(PRINT_PREF "doing operations\n");
  272. for (op = 0; op < count; op++) {
  273. if ((op & 1023) == 0)
  274. printk(PRINT_PREF "%d operations done\n", op);
  275. err = do_operation();
  276. if (err)
  277. goto out;
  278. cond_resched();
  279. }
  280. printk(PRINT_PREF "finished, %d operations done\n", op);
  281. out:
  282. kfree(offsets);
  283. kfree(bbt);
  284. vfree(writebuf);
  285. vfree(readbuf);
  286. out_put_mtd:
  287. put_mtd_device(mtd);
  288. if (err)
  289. printk(PRINT_PREF "error %d occurred\n", err);
  290. printk(KERN_INFO "=================================================\n");
  291. return err;
  292. }
  293. module_init(mtd_stresstest_init);
  294. static void __exit mtd_stresstest_exit(void)
  295. {
  296. return;
  297. }
  298. module_exit(mtd_stresstest_exit);
  299. MODULE_DESCRIPTION("Stress test module");
  300. MODULE_AUTHOR("Adrian Hunter");
  301. MODULE_LICENSE("GPL");