1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2007 Nokia Corporation
5 * Test read and write speed of a MTD device.
7 * Author: Adrian Hunter <adrian.hunter@nokia.com>
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/init.h>
13 #include <linux/ktime.h>
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/err.h>
17 #include <linux/mtd/mtd.h>
18 #include <linux/slab.h>
19 #include <linux/sched.h>
20 #include <linux/random.h>
24 static int dev
= -EINVAL
;
25 module_param(dev
, int, S_IRUGO
);
26 MODULE_PARM_DESC(dev
, "MTD device number to use");
29 module_param(count
, int, S_IRUGO
);
30 MODULE_PARM_DESC(count
, "Maximum number of eraseblocks to use "
33 static struct mtd_info
*mtd
;
34 static unsigned char *iobuf
;
35 static unsigned char *bbt
;
41 static ktime_t start
, finish
;
43 static int multiblock_erase(int ebnum
, int blocks
)
47 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
49 memset(&ei
, 0, sizeof(struct erase_info
));
51 ei
.len
= mtd
->erasesize
* blocks
;
53 err
= mtd_erase(mtd
, &ei
);
55 pr_err("error %d while erasing EB %d, blocks %d\n",
63 static int write_eraseblock(int ebnum
)
65 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
67 return mtdtest_write(mtd
, addr
, mtd
->erasesize
, iobuf
);
70 static int write_eraseblock_by_page(int ebnum
)
73 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
76 for (i
= 0; i
< pgcnt
; i
++) {
77 err
= mtdtest_write(mtd
, addr
, pgsize
, buf
);
87 static int write_eraseblock_by_2pages(int ebnum
)
89 size_t sz
= pgsize
* 2;
90 int i
, n
= pgcnt
/ 2, err
= 0;
91 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
94 for (i
= 0; i
< n
; i
++) {
95 err
= mtdtest_write(mtd
, addr
, sz
, buf
);
102 err
= mtdtest_write(mtd
, addr
, pgsize
, buf
);
107 static int read_eraseblock(int ebnum
)
109 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
111 return mtdtest_read(mtd
, addr
, mtd
->erasesize
, iobuf
);
114 static int read_eraseblock_by_page(int ebnum
)
117 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
120 for (i
= 0; i
< pgcnt
; i
++) {
121 err
= mtdtest_read(mtd
, addr
, pgsize
, buf
);
131 static int read_eraseblock_by_2pages(int ebnum
)
133 size_t sz
= pgsize
* 2;
134 int i
, n
= pgcnt
/ 2, err
= 0;
135 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
138 for (i
= 0; i
< n
; i
++) {
139 err
= mtdtest_read(mtd
, addr
, sz
, buf
);
146 err
= mtdtest_read(mtd
, addr
, pgsize
, buf
);
151 static inline void start_timing(void)
156 static inline void stop_timing(void)
158 finish
= ktime_get();
161 static long calc_speed(void)
166 ms
= ktime_ms_delta(finish
, start
);
169 k
= (uint64_t)goodebcnt
* (mtd
->erasesize
/ 1024) * 1000;
174 static int __init
mtd_speedtest_init(void)
176 int err
, i
, blocks
, j
, k
;
180 printk(KERN_INFO
"\n");
181 printk(KERN_INFO
"=================================================\n");
184 pr_info("Please specify a valid mtd-device via module parameter\n");
185 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
190 pr_info("MTD device: %d count: %d\n", dev
, count
);
192 pr_info("MTD device: %d\n", dev
);
194 mtd
= get_mtd_device(NULL
, dev
);
197 pr_err("error: cannot get MTD device\n");
201 if (mtd
->writesize
== 1) {
202 pr_info("not NAND flash, assume page size is 512 "
206 pgsize
= mtd
->writesize
;
209 do_div(tmp
, mtd
->erasesize
);
211 pgcnt
= mtd
->erasesize
/ pgsize
;
213 pr_info("MTD device size %llu, eraseblock size %u, "
214 "page size %u, count of eraseblocks %u, pages per "
215 "eraseblock %u, OOB size %u\n",
216 (unsigned long long)mtd
->size
, mtd
->erasesize
,
217 pgsize
, ebcnt
, pgcnt
, mtd
->oobsize
);
219 if (count
> 0 && count
< ebcnt
)
223 iobuf
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
227 prandom_bytes(iobuf
, mtd
->erasesize
);
229 bbt
= kzalloc(ebcnt
, GFP_KERNEL
);
232 err
= mtdtest_scan_for_bad_eraseblocks(mtd
, bbt
, 0, ebcnt
);
235 for (i
= 0; i
< ebcnt
; i
++) {
240 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
244 /* Write all eraseblocks, 1 eraseblock at a time */
245 pr_info("testing eraseblock write speed\n");
247 for (i
= 0; i
< ebcnt
; ++i
) {
250 err
= write_eraseblock(i
);
254 err
= mtdtest_relax();
259 speed
= calc_speed();
260 pr_info("eraseblock write speed is %ld KiB/s\n", speed
);
262 /* Read all eraseblocks, 1 eraseblock at a time */
263 pr_info("testing eraseblock read speed\n");
265 for (i
= 0; i
< ebcnt
; ++i
) {
268 err
= read_eraseblock(i
);
272 err
= mtdtest_relax();
277 speed
= calc_speed();
278 pr_info("eraseblock read speed is %ld KiB/s\n", speed
);
280 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
284 /* Write all eraseblocks, 1 page at a time */
285 pr_info("testing page write speed\n");
287 for (i
= 0; i
< ebcnt
; ++i
) {
290 err
= write_eraseblock_by_page(i
);
294 err
= mtdtest_relax();
299 speed
= calc_speed();
300 pr_info("page write speed is %ld KiB/s\n", speed
);
302 /* Read all eraseblocks, 1 page at a time */
303 pr_info("testing page read speed\n");
305 for (i
= 0; i
< ebcnt
; ++i
) {
308 err
= read_eraseblock_by_page(i
);
312 err
= mtdtest_relax();
317 speed
= calc_speed();
318 pr_info("page read speed is %ld KiB/s\n", speed
);
320 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
324 /* Write all eraseblocks, 2 pages at a time */
325 pr_info("testing 2 page write speed\n");
327 for (i
= 0; i
< ebcnt
; ++i
) {
330 err
= write_eraseblock_by_2pages(i
);
334 err
= mtdtest_relax();
339 speed
= calc_speed();
340 pr_info("2 page write speed is %ld KiB/s\n", speed
);
342 /* Read all eraseblocks, 2 pages at a time */
343 pr_info("testing 2 page read speed\n");
345 for (i
= 0; i
< ebcnt
; ++i
) {
348 err
= read_eraseblock_by_2pages(i
);
352 err
= mtdtest_relax();
357 speed
= calc_speed();
358 pr_info("2 page read speed is %ld KiB/s\n", speed
);
360 /* Erase all eraseblocks */
361 pr_info("Testing erase speed\n");
363 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
367 speed
= calc_speed();
368 pr_info("erase speed is %ld KiB/s\n", speed
);
370 /* Multi-block erase all eraseblocks */
371 for (k
= 1; k
< 7; k
++) {
373 pr_info("Testing %dx multi-block erase speed\n",
376 for (i
= 0; i
< ebcnt
; ) {
377 for (j
= 0; j
< blocks
&& (i
+ j
) < ebcnt
; j
++)
384 err
= multiblock_erase(i
, j
);
388 err
= mtdtest_relax();
395 speed
= calc_speed();
396 pr_info("%dx multi-block erase speed is %ld KiB/s\n",
399 pr_info("finished\n");
405 pr_info("error %d occurred\n", err
);
406 printk(KERN_INFO
"=================================================\n");
409 module_init(mtd_speedtest_init
);
411 static void __exit
mtd_speedtest_exit(void)
415 module_exit(mtd_speedtest_exit
);
417 MODULE_DESCRIPTION("Speed test module");
418 MODULE_AUTHOR("Adrian Hunter");
419 MODULE_LICENSE("GPL");