Merge branch 'linus' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[linux-2.6-block.git] / drivers / mtd / tests / speedtest.c
CommitLineData
4cd10358 1// SPDX-License-Identifier: GPL-2.0-only
72069be9
AB
2/*
3 * Copyright (C) 2007 Nokia Corporation
4 *
72069be9
AB
5 * Test read and write speed of a MTD device.
6 *
fc7fe769 7 * Author: Adrian Hunter <adrian.hunter@nokia.com>
72069be9
AB
8 */
9
2c70d292
VN
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
72069be9 12#include <linux/init.h>
af30c0a0 13#include <linux/ktime.h>
72069be9
AB
14#include <linux/module.h>
15#include <linux/moduleparam.h>
16#include <linux/err.h>
17#include <linux/mtd/mtd.h>
5a0e3ad6 18#include <linux/slab.h>
72069be9 19#include <linux/sched.h>
bfea1d4e 20#include <linux/random.h>
72069be9 21
59b0816d
AM
22#include "mtd_test.h"
23
7406060e 24static int dev = -EINVAL;
72069be9
AB
25module_param(dev, int, S_IRUGO);
26MODULE_PARM_DESC(dev, "MTD device number to use");
27
fc7fe769
AH
28static int count;
29module_param(count, int, S_IRUGO);
30MODULE_PARM_DESC(count, "Maximum number of eraseblocks to use "
31 "(0 means use all)");
32
72069be9
AB
33static struct mtd_info *mtd;
34static unsigned char *iobuf;
35static unsigned char *bbt;
36
37static int pgsize;
38static int ebcnt;
39static int pgcnt;
40static int goodebcnt;
af30c0a0 41static ktime_t start, finish;
72069be9 42
4085bcc6
RT
43static int multiblock_erase(int ebnum, int blocks)
44{
45 int err;
46 struct erase_info ei;
1001ff7a 47 loff_t addr = (loff_t)ebnum * mtd->erasesize;
4085bcc6
RT
48
49 memset(&ei, 0, sizeof(struct erase_info));
4085bcc6
RT
50 ei.addr = addr;
51 ei.len = mtd->erasesize * blocks;
52
7e1f0dc0 53 err = mtd_erase(mtd, &ei);
4085bcc6 54 if (err) {
2c70d292 55 pr_err("error %d while erasing EB %d, blocks %d\n",
4085bcc6
RT
56 err, ebnum, blocks);
57 return err;
58 }
59
4085bcc6
RT
60 return 0;
61}
62
72069be9
AB
63static int write_eraseblock(int ebnum)
64{
1001ff7a 65 loff_t addr = (loff_t)ebnum * mtd->erasesize;
72069be9 66
8a9f4aa3 67 return mtdtest_write(mtd, addr, mtd->erasesize, iobuf);
72069be9
AB
68}
69
70static int write_eraseblock_by_page(int ebnum)
71{
72069be9 72 int i, err = 0;
1001ff7a 73 loff_t addr = (loff_t)ebnum * mtd->erasesize;
72069be9
AB
74 void *buf = iobuf;
75
76 for (i = 0; i < pgcnt; i++) {
59b0816d 77 err = mtdtest_write(mtd, addr, pgsize, buf);
8a9f4aa3 78 if (err)
72069be9 79 break;
72069be9
AB
80 addr += pgsize;
81 buf += pgsize;
82 }
83
84 return err;
85}
86
87static int write_eraseblock_by_2pages(int ebnum)
88{
59b0816d 89 size_t sz = pgsize * 2;
72069be9 90 int i, n = pgcnt / 2, err = 0;
1001ff7a 91 loff_t addr = (loff_t)ebnum * mtd->erasesize;
72069be9
AB
92 void *buf = iobuf;
93
94 for (i = 0; i < n; i++) {
59b0816d 95 err = mtdtest_write(mtd, addr, sz, buf);
8a9f4aa3 96 if (err)
72069be9 97 return err;
72069be9
AB
98 addr += sz;
99 buf += sz;
100 }
8a9f4aa3 101 if (pgcnt % 2)
59b0816d 102 err = mtdtest_write(mtd, addr, pgsize, buf);
72069be9
AB
103
104 return err;
105}
106
107static int read_eraseblock(int ebnum)
108{
1001ff7a 109 loff_t addr = (loff_t)ebnum * mtd->erasesize;
72069be9 110
abc173ad 111 return mtdtest_read(mtd, addr, mtd->erasesize, iobuf);
72069be9
AB
112}
113
114static int read_eraseblock_by_page(int ebnum)
115{
72069be9 116 int i, err = 0;
1001ff7a 117 loff_t addr = (loff_t)ebnum * mtd->erasesize;
72069be9
AB
118 void *buf = iobuf;
119
120 for (i = 0; i < pgcnt; i++) {
59b0816d 121 err = mtdtest_read(mtd, addr, pgsize, buf);
abc173ad 122 if (err)
72069be9 123 break;
72069be9
AB
124 addr += pgsize;
125 buf += pgsize;
126 }
127
128 return err;
129}
130
131static int read_eraseblock_by_2pages(int ebnum)
132{
59b0816d 133 size_t sz = pgsize * 2;
72069be9 134 int i, n = pgcnt / 2, err = 0;
1001ff7a 135 loff_t addr = (loff_t)ebnum * mtd->erasesize;
72069be9
AB
136 void *buf = iobuf;
137
138 for (i = 0; i < n; i++) {
59b0816d 139 err = mtdtest_read(mtd, addr, sz, buf);
abc173ad 140 if (err)
72069be9 141 return err;
72069be9
AB
142 addr += sz;
143 buf += sz;
144 }
abc173ad 145 if (pgcnt % 2)
59b0816d 146 err = mtdtest_read(mtd, addr, pgsize, buf);
72069be9
AB
147
148 return err;
149}
150
72069be9
AB
151static inline void start_timing(void)
152{
af30c0a0 153 start = ktime_get();
72069be9
AB
154}
155
156static inline void stop_timing(void)
157{
af30c0a0 158 finish = ktime_get();
72069be9
AB
159}
160
161static long calc_speed(void)
162{
e70727e4
DL
163 uint64_t k;
164 long ms;
72069be9 165
af30c0a0 166 ms = ktime_ms_delta(finish, start);
e70727e4
DL
167 if (ms == 0)
168 return 0;
b9da8bae 169 k = (uint64_t)goodebcnt * (mtd->erasesize / 1024) * 1000;
e70727e4
DL
170 do_div(k, ms);
171 return k;
72069be9
AB
172}
173
72069be9
AB
174static int __init mtd_speedtest_init(void)
175{
4085bcc6 176 int err, i, blocks, j, k;
72069be9
AB
177 long speed;
178 uint64_t tmp;
179
180 printk(KERN_INFO "\n");
181 printk(KERN_INFO "=================================================\n");
7406060e
WS
182
183 if (dev < 0) {
064a7694 184 pr_info("Please specify a valid mtd-device via module parameter\n");
2c70d292 185 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
7406060e
WS
186 return -EINVAL;
187 }
188
fc7fe769 189 if (count)
2c70d292 190 pr_info("MTD device: %d count: %d\n", dev, count);
fc7fe769 191 else
2c70d292 192 pr_info("MTD device: %d\n", dev);
72069be9
AB
193
194 mtd = get_mtd_device(NULL, dev);
195 if (IS_ERR(mtd)) {
196 err = PTR_ERR(mtd);
2c70d292 197 pr_err("error: cannot get MTD device\n");
72069be9
AB
198 return err;
199 }
200
201 if (mtd->writesize == 1) {
2c70d292 202 pr_info("not NAND flash, assume page size is 512 "
72069be9
AB
203 "bytes.\n");
204 pgsize = 512;
205 } else
206 pgsize = mtd->writesize;
207
208 tmp = mtd->size;
209 do_div(tmp, mtd->erasesize);
210 ebcnt = tmp;
f5e2bae0 211 pgcnt = mtd->erasesize / pgsize;
72069be9 212
2c70d292 213 pr_info("MTD device size %llu, eraseblock size %u, "
72069be9
AB
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);
218
fc7fe769
AH
219 if (count > 0 && count < ebcnt)
220 ebcnt = count;
221
72069be9
AB
222 err = -ENOMEM;
223 iobuf = kmalloc(mtd->erasesize, GFP_KERNEL);
33777e66 224 if (!iobuf)
72069be9 225 goto out;
72069be9 226
99672f32 227 prandom_bytes(iobuf, mtd->erasesize);
72069be9 228
59b0816d
AM
229 bbt = kzalloc(ebcnt, GFP_KERNEL);
230 if (!bbt)
231 goto out;
232 err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
72069be9
AB
233 if (err)
234 goto out;
59b0816d
AM
235 for (i = 0; i < ebcnt; i++) {
236 if (!bbt[i])
237 goodebcnt++;
238 }
72069be9 239
59b0816d 240 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
72069be9
AB
241 if (err)
242 goto out;
243
244 /* Write all eraseblocks, 1 eraseblock at a time */
2c70d292 245 pr_info("testing eraseblock write speed\n");
72069be9
AB
246 start_timing();
247 for (i = 0; i < ebcnt; ++i) {
248 if (bbt[i])
249 continue;
250 err = write_eraseblock(i);
251 if (err)
252 goto out;
2a6a28e7
RW
253
254 err = mtdtest_relax();
255 if (err)
256 goto out;
72069be9
AB
257 }
258 stop_timing();
259 speed = calc_speed();
2c70d292 260 pr_info("eraseblock write speed is %ld KiB/s\n", speed);
72069be9
AB
261
262 /* Read all eraseblocks, 1 eraseblock at a time */
2c70d292 263 pr_info("testing eraseblock read speed\n");
72069be9
AB
264 start_timing();
265 for (i = 0; i < ebcnt; ++i) {
266 if (bbt[i])
267 continue;
268 err = read_eraseblock(i);
269 if (err)
270 goto out;
2a6a28e7
RW
271
272 err = mtdtest_relax();
273 if (err)
274 goto out;
72069be9
AB
275 }
276 stop_timing();
277 speed = calc_speed();
2c70d292 278 pr_info("eraseblock read speed is %ld KiB/s\n", speed);
72069be9 279
59b0816d 280 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
72069be9
AB
281 if (err)
282 goto out;
283
284 /* Write all eraseblocks, 1 page at a time */
2c70d292 285 pr_info("testing page write speed\n");
72069be9
AB
286 start_timing();
287 for (i = 0; i < ebcnt; ++i) {
288 if (bbt[i])
289 continue;
290 err = write_eraseblock_by_page(i);
291 if (err)
292 goto out;
2a6a28e7
RW
293
294 err = mtdtest_relax();
295 if (err)
296 goto out;
72069be9
AB
297 }
298 stop_timing();
299 speed = calc_speed();
2c70d292 300 pr_info("page write speed is %ld KiB/s\n", speed);
72069be9
AB
301
302 /* Read all eraseblocks, 1 page at a time */
2c70d292 303 pr_info("testing page read speed\n");
72069be9
AB
304 start_timing();
305 for (i = 0; i < ebcnt; ++i) {
306 if (bbt[i])
307 continue;
308 err = read_eraseblock_by_page(i);
309 if (err)
310 goto out;
2a6a28e7
RW
311
312 err = mtdtest_relax();
313 if (err)
314 goto out;
72069be9
AB
315 }
316 stop_timing();
317 speed = calc_speed();
2c70d292 318 pr_info("page read speed is %ld KiB/s\n", speed);
72069be9 319
59b0816d 320 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
72069be9
AB
321 if (err)
322 goto out;
323
324 /* Write all eraseblocks, 2 pages at a time */
2c70d292 325 pr_info("testing 2 page write speed\n");
72069be9
AB
326 start_timing();
327 for (i = 0; i < ebcnt; ++i) {
328 if (bbt[i])
329 continue;
330 err = write_eraseblock_by_2pages(i);
331 if (err)
332 goto out;
2a6a28e7
RW
333
334 err = mtdtest_relax();
335 if (err)
336 goto out;
72069be9
AB
337 }
338 stop_timing();
339 speed = calc_speed();
2c70d292 340 pr_info("2 page write speed is %ld KiB/s\n", speed);
72069be9
AB
341
342 /* Read all eraseblocks, 2 pages at a time */
2c70d292 343 pr_info("testing 2 page read speed\n");
72069be9
AB
344 start_timing();
345 for (i = 0; i < ebcnt; ++i) {
346 if (bbt[i])
347 continue;
348 err = read_eraseblock_by_2pages(i);
349 if (err)
350 goto out;
2a6a28e7
RW
351
352 err = mtdtest_relax();
353 if (err)
354 goto out;
72069be9
AB
355 }
356 stop_timing();
357 speed = calc_speed();
2c70d292 358 pr_info("2 page read speed is %ld KiB/s\n", speed);
72069be9
AB
359
360 /* Erase all eraseblocks */
2c70d292 361 pr_info("Testing erase speed\n");
72069be9 362 start_timing();
59b0816d
AM
363 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
364 if (err)
365 goto out;
72069be9
AB
366 stop_timing();
367 speed = calc_speed();
2c70d292 368 pr_info("erase speed is %ld KiB/s\n", speed);
72069be9 369
4085bcc6
RT
370 /* Multi-block erase all eraseblocks */
371 for (k = 1; k < 7; k++) {
372 blocks = 1 << k;
2c70d292 373 pr_info("Testing %dx multi-block erase speed\n",
4085bcc6
RT
374 blocks);
375 start_timing();
376 for (i = 0; i < ebcnt; ) {
377 for (j = 0; j < blocks && (i + j) < ebcnt; j++)
378 if (bbt[i + j])
379 break;
380 if (j < 1) {
381 i++;
382 continue;
383 }
384 err = multiblock_erase(i, j);
385 if (err)
386 goto out;
2a6a28e7
RW
387
388 err = mtdtest_relax();
389 if (err)
390 goto out;
391
4085bcc6
RT
392 i += j;
393 }
394 stop_timing();
395 speed = calc_speed();
2c70d292 396 pr_info("%dx multi-block erase speed is %ld KiB/s\n",
4085bcc6
RT
397 blocks, speed);
398 }
2c70d292 399 pr_info("finished\n");
72069be9
AB
400out:
401 kfree(iobuf);
402 kfree(bbt);
403 put_mtd_device(mtd);
404 if (err)
2c70d292 405 pr_info("error %d occurred\n", err);
72069be9
AB
406 printk(KERN_INFO "=================================================\n");
407 return err;
408}
409module_init(mtd_speedtest_init);
410
411static void __exit mtd_speedtest_exit(void)
412{
413 return;
414}
415module_exit(mtd_speedtest_exit);
416
417MODULE_DESCRIPTION("Speed test module");
418MODULE_AUTHOR("Adrian Hunter");
419MODULE_LICENSE("GPL");