2 * Copyright (C) 2006-2008 Nokia Corporation
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.
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
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.
17 * Test random reads, writes and erases on MTD device.
19 * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/err.h>
28 #include <linux/mtd/mtd.h>
29 #include <linux/slab.h>
30 #include <linux/sched.h>
31 #include <linux/vmalloc.h>
32 #include <linux/random.h>
36 static int dev
= -EINVAL
;
37 module_param(dev
, int, S_IRUGO
);
38 MODULE_PARM_DESC(dev
, "MTD device number to use");
40 static int count
= 10000;
41 module_param(count
, int, S_IRUGO
);
42 MODULE_PARM_DESC(count
, "Number of operations to do (default is 10000)");
44 static struct mtd_info
*mtd
;
45 static unsigned char *writebuf
;
46 static unsigned char *readbuf
;
47 static unsigned char *bbt
;
55 static int rand_eb(void)
61 /* Read or write up 2 eraseblocks at a time - hence 'ebcnt - 1' */
68 static int rand_offs(void)
77 static int rand_len(int offs
)
82 len
%= (bufsize
- offs
);
86 static int do_read(void)
89 int offs
= rand_offs();
90 int len
= rand_len(offs
);
94 if (offs
>= mtd
->erasesize
)
95 offs
-= mtd
->erasesize
;
96 if (offs
+ len
> mtd
->erasesize
)
97 len
= mtd
->erasesize
- offs
;
99 addr
= (loff_t
)eb
* mtd
->erasesize
+ offs
;
100 return mtdtest_read(mtd
, addr
, len
, readbuf
);
103 static int do_write(void)
105 int eb
= rand_eb(), offs
, err
, len
;
109 if (offs
>= mtd
->erasesize
) {
110 err
= mtdtest_erase_eraseblock(mtd
, eb
);
113 offs
= offsets
[eb
] = 0;
115 len
= rand_len(offs
);
116 len
= ((len
+ pgsize
- 1) / pgsize
) * pgsize
;
117 if (offs
+ len
> mtd
->erasesize
) {
119 len
= mtd
->erasesize
- offs
;
121 err
= mtdtest_erase_eraseblock(mtd
, eb
+ 1);
127 addr
= (loff_t
)eb
* mtd
->erasesize
+ offs
;
128 err
= mtdtest_write(mtd
, addr
, len
, writebuf
);
132 while (offs
> mtd
->erasesize
) {
133 offsets
[eb
++] = mtd
->erasesize
;
134 offs
-= mtd
->erasesize
;
140 static int do_operation(void)
142 if (prandom_u32() & 1)
148 static int __init
mtd_stresstest_init(void)
154 printk(KERN_INFO
"\n");
155 printk(KERN_INFO
"=================================================\n");
158 pr_info("Please specify a valid mtd-device via module parameter\n");
159 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
163 pr_info("MTD device: %d\n", dev
);
165 mtd
= get_mtd_device(NULL
, dev
);
168 pr_err("error: cannot get MTD device\n");
172 if (mtd
->writesize
== 1) {
173 pr_info("not NAND flash, assume page size is 512 "
177 pgsize
= mtd
->writesize
;
180 do_div(tmp
, mtd
->erasesize
);
182 pgcnt
= mtd
->erasesize
/ pgsize
;
184 pr_info("MTD device size %llu, eraseblock size %u, "
185 "page size %u, count of eraseblocks %u, pages per "
186 "eraseblock %u, OOB size %u\n",
187 (unsigned long long)mtd
->size
, mtd
->erasesize
,
188 pgsize
, ebcnt
, pgcnt
, mtd
->oobsize
);
191 pr_err("error: need at least 2 eraseblocks\n");
196 /* Read or write up 2 eraseblocks at a time */
197 bufsize
= mtd
->erasesize
* 2;
200 readbuf
= vmalloc(bufsize
);
201 writebuf
= vmalloc(bufsize
);
202 offsets
= kmalloc(ebcnt
* sizeof(int), GFP_KERNEL
);
203 if (!readbuf
|| !writebuf
|| !offsets
)
205 for (i
= 0; i
< ebcnt
; i
++)
206 offsets
[i
] = mtd
->erasesize
;
207 prandom_bytes(writebuf
, bufsize
);
209 bbt
= kzalloc(ebcnt
, GFP_KERNEL
);
212 err
= mtdtest_scan_for_bad_eraseblocks(mtd
, bbt
, 0, ebcnt
);
217 pr_info("doing operations\n");
218 for (op
= 0; op
< count
; op
++) {
219 if ((op
& 1023) == 0)
220 pr_info("%d operations done\n", op
);
221 err
= do_operation();
225 err
= mtdtest_relax();
229 pr_info("finished, %d operations done\n", op
);
239 pr_info("error %d occurred\n", err
);
240 printk(KERN_INFO
"=================================================\n");
243 module_init(mtd_stresstest_init
);
245 static void __exit
mtd_stresstest_exit(void)
249 module_exit(mtd_stresstest_exit
);
251 MODULE_DESCRIPTION("Stress test module");
252 MODULE_AUTHOR("Adrian Hunter");
253 MODULE_LICENSE("GPL");