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>
34 static int dev
= -EINVAL
;
35 module_param(dev
, int, S_IRUGO
);
36 MODULE_PARM_DESC(dev
, "MTD device number to use");
38 static int count
= 10000;
39 module_param(count
, int, S_IRUGO
);
40 MODULE_PARM_DESC(count
, "Number of operations to do (default is 10000)");
42 static struct mtd_info
*mtd
;
43 static unsigned char *writebuf
;
44 static unsigned char *readbuf
;
45 static unsigned char *bbt
;
53 static int rand_eb(void)
59 /* Read or write up 2 eraseblocks at a time - hence 'ebcnt - 1' */
66 static int rand_offs(void)
75 static int rand_len(int offs
)
80 len
%= (bufsize
- offs
);
84 static int erase_eraseblock(int ebnum
)
88 loff_t addr
= ebnum
* mtd
->erasesize
;
90 memset(&ei
, 0, sizeof(struct erase_info
));
93 ei
.len
= mtd
->erasesize
;
95 err
= mtd_erase(mtd
, &ei
);
97 pr_err("error %d while erasing EB %d\n", err
, ebnum
);
101 if (unlikely(ei
.state
== MTD_ERASE_FAILED
)) {
102 pr_err("some erase error occurred at EB %d\n",
110 static int is_block_bad(int ebnum
)
112 loff_t addr
= ebnum
* mtd
->erasesize
;
115 ret
= mtd_block_isbad(mtd
, addr
);
117 pr_info("block %d is bad\n", ebnum
);
121 static int do_read(void)
125 int offs
= rand_offs();
126 int len
= rand_len(offs
), err
;
130 if (offs
>= mtd
->erasesize
)
131 offs
-= mtd
->erasesize
;
132 if (offs
+ len
> mtd
->erasesize
)
133 len
= mtd
->erasesize
- offs
;
135 addr
= eb
* mtd
->erasesize
+ offs
;
136 err
= mtd_read(mtd
, addr
, len
, &read
, readbuf
);
137 if (mtd_is_bitflip(err
))
139 if (unlikely(err
|| read
!= len
)) {
140 pr_err("error: read failed at 0x%llx\n",
149 static int do_write(void)
151 int eb
= rand_eb(), offs
, err
, len
;
156 if (offs
>= mtd
->erasesize
) {
157 err
= erase_eraseblock(eb
);
160 offs
= offsets
[eb
] = 0;
162 len
= rand_len(offs
);
163 len
= ((len
+ pgsize
- 1) / pgsize
) * pgsize
;
164 if (offs
+ len
> mtd
->erasesize
) {
166 len
= mtd
->erasesize
- offs
;
168 err
= erase_eraseblock(eb
+ 1);
174 addr
= eb
* mtd
->erasesize
+ offs
;
175 err
= mtd_write(mtd
, addr
, len
, &written
, writebuf
);
176 if (unlikely(err
|| written
!= len
)) {
177 pr_err("error: write failed at 0x%llx\n",
184 while (offs
> mtd
->erasesize
) {
185 offsets
[eb
++] = mtd
->erasesize
;
186 offs
-= mtd
->erasesize
;
192 static int do_operation(void)
200 static int scan_for_bad_eraseblocks(void)
204 bbt
= kzalloc(ebcnt
, GFP_KERNEL
);
206 pr_err("error: cannot allocate memory\n");
210 if (!mtd_can_have_bb(mtd
))
213 pr_info("scanning for bad eraseblocks\n");
214 for (i
= 0; i
< ebcnt
; ++i
) {
215 bbt
[i
] = is_block_bad(i
) ? 1 : 0;
220 pr_info("scanned %d eraseblocks, %d are bad\n", i
, bad
);
224 static int __init
mtd_stresstest_init(void)
230 printk(KERN_INFO
"\n");
231 printk(KERN_INFO
"=================================================\n");
234 pr_info("Please specify a valid mtd-device via module parameter\n");
235 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
239 pr_info("MTD device: %d\n", dev
);
241 mtd
= get_mtd_device(NULL
, dev
);
244 pr_err("error: cannot get MTD device\n");
248 if (mtd
->writesize
== 1) {
249 pr_info("not NAND flash, assume page size is 512 "
253 pgsize
= mtd
->writesize
;
256 do_div(tmp
, mtd
->erasesize
);
258 pgcnt
= mtd
->erasesize
/ pgsize
;
260 pr_info("MTD device size %llu, eraseblock size %u, "
261 "page size %u, count of eraseblocks %u, pages per "
262 "eraseblock %u, OOB size %u\n",
263 (unsigned long long)mtd
->size
, mtd
->erasesize
,
264 pgsize
, ebcnt
, pgcnt
, mtd
->oobsize
);
267 pr_err("error: need at least 2 eraseblocks\n");
272 /* Read or write up 2 eraseblocks at a time */
273 bufsize
= mtd
->erasesize
* 2;
276 readbuf
= vmalloc(bufsize
);
277 writebuf
= vmalloc(bufsize
);
278 offsets
= kmalloc(ebcnt
* sizeof(int), GFP_KERNEL
);
279 if (!readbuf
|| !writebuf
|| !offsets
) {
280 pr_err("error: cannot allocate memory\n");
283 for (i
= 0; i
< ebcnt
; i
++)
284 offsets
[i
] = mtd
->erasesize
;
285 for (i
= 0; i
< bufsize
; i
++)
286 writebuf
[i
] = random32();
288 err
= scan_for_bad_eraseblocks();
293 pr_info("doing operations\n");
294 for (op
= 0; op
< count
; op
++) {
295 if ((op
& 1023) == 0)
296 pr_info("%d operations done\n", op
);
297 err
= do_operation();
302 pr_info("finished, %d operations done\n", op
);
312 pr_info("error %d occurred\n", err
);
313 printk(KERN_INFO
"=================================================\n");
316 module_init(mtd_stresstest_init
);
318 static void __exit
mtd_stresstest_exit(void)
322 module_exit(mtd_stresstest_exit
);
324 MODULE_DESCRIPTION("Stress test module");
325 MODULE_AUTHOR("Adrian Hunter");
326 MODULE_LICENSE("GPL");