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