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 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/err.h>
26 #include <linux/mtd/mtd.h>
27 #include <linux/slab.h>
28 #include <linux/sched.h>
29 #include <linux/vmalloc.h>
31 #define PRINT_PREF KERN_INFO "mtd_stresstest: "
33 static int dev
= -EINVAL
;
34 module_param(dev
, int, S_IRUGO
);
35 MODULE_PARM_DESC(dev
, "MTD device number to use");
37 static int count
= 10000;
38 module_param(count
, int, S_IRUGO
);
39 MODULE_PARM_DESC(count
, "Number of operations to do (default is 10000)");
41 static struct mtd_info
*mtd
;
42 static unsigned char *writebuf
;
43 static unsigned char *readbuf
;
44 static unsigned char *bbt
;
51 static unsigned long next
= 1;
53 static inline unsigned int simple_rand(void)
55 next
= next
* 1103515245 + 12345;
56 return (unsigned int)((next
/ 65536) % 32768);
59 static inline void simple_srand(unsigned long seed
)
64 static int rand_eb(void)
72 eb
= (simple_rand() << 15) | simple_rand();
73 /* Read or write up 2 eraseblocks at a time - hence 'ebcnt - 1' */
80 static int rand_offs(void)
87 offs
= (simple_rand() << 15) | simple_rand();
92 static int rand_len(int offs
)
99 len
= (simple_rand() << 15) | simple_rand();
100 len
%= (bufsize
- offs
);
104 static int erase_eraseblock(int ebnum
)
107 struct erase_info ei
;
108 loff_t addr
= ebnum
* mtd
->erasesize
;
110 memset(&ei
, 0, sizeof(struct erase_info
));
113 ei
.len
= mtd
->erasesize
;
115 err
= mtd_erase(mtd
, &ei
);
117 printk(PRINT_PREF
"error %d while erasing EB %d\n", err
, ebnum
);
121 if (unlikely(ei
.state
== MTD_ERASE_FAILED
)) {
122 printk(PRINT_PREF
"some erase error occurred at EB %d\n",
130 static int is_block_bad(int ebnum
)
132 loff_t addr
= ebnum
* mtd
->erasesize
;
135 ret
= mtd_block_isbad(mtd
, addr
);
137 printk(PRINT_PREF
"block %d is bad\n", ebnum
);
141 static int do_read(void)
145 int offs
= rand_offs();
146 int len
= rand_len(offs
), err
;
150 if (offs
>= mtd
->erasesize
)
151 offs
-= mtd
->erasesize
;
152 if (offs
+ len
> mtd
->erasesize
)
153 len
= mtd
->erasesize
- offs
;
155 addr
= eb
* mtd
->erasesize
+ offs
;
156 err
= mtd_read(mtd
, addr
, len
, &read
, readbuf
);
157 if (mtd_is_bitflip(err
))
159 if (unlikely(err
|| read
!= len
)) {
160 printk(PRINT_PREF
"error: read failed at 0x%llx\n",
169 static int do_write(void)
171 int eb
= rand_eb(), offs
, err
, len
;
176 if (offs
>= mtd
->erasesize
) {
177 err
= erase_eraseblock(eb
);
180 offs
= offsets
[eb
] = 0;
182 len
= rand_len(offs
);
183 len
= ((len
+ pgsize
- 1) / pgsize
) * pgsize
;
184 if (offs
+ len
> mtd
->erasesize
) {
186 len
= mtd
->erasesize
- offs
;
188 err
= erase_eraseblock(eb
+ 1);
194 addr
= eb
* mtd
->erasesize
+ offs
;
195 err
= mtd_write(mtd
, addr
, len
, &written
, writebuf
);
196 if (unlikely(err
|| written
!= len
)) {
197 printk(PRINT_PREF
"error: write failed at 0x%llx\n",
204 while (offs
> mtd
->erasesize
) {
205 offsets
[eb
++] = mtd
->erasesize
;
206 offs
-= mtd
->erasesize
;
212 static int do_operation(void)
214 if (simple_rand() & 1)
220 static int scan_for_bad_eraseblocks(void)
224 bbt
= kzalloc(ebcnt
, GFP_KERNEL
);
226 printk(PRINT_PREF
"error: cannot allocate memory\n");
230 if (!mtd_can_have_bb(mtd
))
233 printk(PRINT_PREF
"scanning for bad eraseblocks\n");
234 for (i
= 0; i
< ebcnt
; ++i
) {
235 bbt
[i
] = is_block_bad(i
) ? 1 : 0;
240 printk(PRINT_PREF
"scanned %d eraseblocks, %d are bad\n", i
, bad
);
244 static int __init
mtd_stresstest_init(void)
250 printk(KERN_INFO
"\n");
251 printk(KERN_INFO
"=================================================\n");
254 printk(PRINT_PREF
"Please specify a valid mtd-device via module paramter\n");
255 printk(KERN_CRIT
"CAREFUL: This test wipes all data on the specified MTD device!\n");
259 printk(PRINT_PREF
"MTD device: %d\n", dev
);
261 mtd
= get_mtd_device(NULL
, dev
);
264 printk(PRINT_PREF
"error: cannot get MTD device\n");
268 if (mtd
->writesize
== 1) {
269 printk(PRINT_PREF
"not NAND flash, assume page size is 512 "
273 pgsize
= mtd
->writesize
;
276 do_div(tmp
, mtd
->erasesize
);
278 pgcnt
= mtd
->erasesize
/ pgsize
;
280 printk(PRINT_PREF
"MTD device size %llu, eraseblock size %u, "
281 "page size %u, count of eraseblocks %u, pages per "
282 "eraseblock %u, OOB size %u\n",
283 (unsigned long long)mtd
->size
, mtd
->erasesize
,
284 pgsize
, ebcnt
, pgcnt
, mtd
->oobsize
);
287 printk(PRINT_PREF
"error: need at least 2 eraseblocks\n");
292 /* Read or write up 2 eraseblocks at a time */
293 bufsize
= mtd
->erasesize
* 2;
296 readbuf
= vmalloc(bufsize
);
297 writebuf
= vmalloc(bufsize
);
298 offsets
= kmalloc(ebcnt
* sizeof(int), GFP_KERNEL
);
299 if (!readbuf
|| !writebuf
|| !offsets
) {
300 printk(PRINT_PREF
"error: cannot allocate memory\n");
303 for (i
= 0; i
< ebcnt
; i
++)
304 offsets
[i
] = mtd
->erasesize
;
305 simple_srand(current
->pid
);
306 for (i
= 0; i
< bufsize
; i
++)
307 writebuf
[i
] = simple_rand();
309 err
= scan_for_bad_eraseblocks();
314 printk(PRINT_PREF
"doing operations\n");
315 for (op
= 0; op
< count
; op
++) {
316 if ((op
& 1023) == 0)
317 printk(PRINT_PREF
"%d operations done\n", op
);
318 err
= do_operation();
323 printk(PRINT_PREF
"finished, %d operations done\n", op
);
333 printk(PRINT_PREF
"error %d occurred\n", err
);
334 printk(KERN_INFO
"=================================================\n");
337 module_init(mtd_stresstest_init
);
339 static void __exit
mtd_stresstest_exit(void)
343 module_exit(mtd_stresstest_exit
);
345 MODULE_DESCRIPTION("Stress test module");
346 MODULE_AUTHOR("Adrian Hunter");
347 MODULE_LICENSE("GPL");