mtd_stresstest.c 7.2 KB

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