Commit | Line | Data |
---|---|---|
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 AB |
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> | |
5a0e3ad6 | 29 | #include <linux/slab.h> |
72069be9 | 30 | #include <linux/sched.h> |
bfea1d4e | 31 | #include <linux/random.h> |
72069be9 | 32 | |
7406060e | 33 | static int dev = -EINVAL; |
72069be9 AB |
34 | module_param(dev, int, S_IRUGO); |
35 | MODULE_PARM_DESC(dev, "MTD device number to use"); | |
36 | ||
fc7fe769 AH |
37 | static int count; |
38 | module_param(count, int, S_IRUGO); | |
39 | MODULE_PARM_DESC(count, "Maximum number of eraseblocks to use " | |
40 | "(0 means use all)"); | |
41 | ||
72069be9 AB |
42 | static struct mtd_info *mtd; |
43 | static unsigned char *iobuf; | |
44 | static unsigned char *bbt; | |
45 | ||
46 | static int pgsize; | |
47 | static int ebcnt; | |
48 | static int pgcnt; | |
49 | static int goodebcnt; | |
50 | static struct timeval start, finish; | |
72069be9 AB |
51 | |
52 | static void set_random_data(unsigned char *buf, size_t len) | |
53 | { | |
54 | size_t i; | |
55 | ||
56 | for (i = 0; i < len; ++i) | |
bfea1d4e | 57 | buf[i] = random32(); |
72069be9 AB |
58 | } |
59 | ||
60 | static int erase_eraseblock(int ebnum) | |
61 | { | |
62 | int err; | |
63 | struct erase_info ei; | |
64 | loff_t addr = ebnum * mtd->erasesize; | |
65 | ||
66 | memset(&ei, 0, sizeof(struct erase_info)); | |
67 | ei.mtd = mtd; | |
68 | ei.addr = addr; | |
69 | ei.len = mtd->erasesize; | |
70 | ||
7e1f0dc0 | 71 | err = mtd_erase(mtd, &ei); |
72069be9 | 72 | if (err) { |
2c70d292 | 73 | pr_err("error %d while erasing EB %d\n", err, ebnum); |
72069be9 AB |
74 | return err; |
75 | } | |
76 | ||
77 | if (ei.state == MTD_ERASE_FAILED) { | |
2c70d292 | 78 | pr_err("some erase error occurred at EB %d\n", |
72069be9 AB |
79 | ebnum); |
80 | return -EIO; | |
81 | } | |
82 | ||
83 | return 0; | |
84 | } | |
85 | ||
4085bcc6 RT |
86 | static int multiblock_erase(int ebnum, int blocks) |
87 | { | |
88 | int err; | |
89 | struct erase_info ei; | |
90 | loff_t addr = ebnum * mtd->erasesize; | |
91 | ||
92 | memset(&ei, 0, sizeof(struct erase_info)); | |
93 | ei.mtd = mtd; | |
94 | ei.addr = addr; | |
95 | ei.len = mtd->erasesize * blocks; | |
96 | ||
7e1f0dc0 | 97 | err = mtd_erase(mtd, &ei); |
4085bcc6 | 98 | if (err) { |
2c70d292 | 99 | pr_err("error %d while erasing EB %d, blocks %d\n", |
4085bcc6 RT |
100 | err, ebnum, blocks); |
101 | return err; | |
102 | } | |
103 | ||
104 | if (ei.state == MTD_ERASE_FAILED) { | |
2c70d292 | 105 | pr_err("some erase error occurred at EB %d," |
4085bcc6 RT |
106 | "blocks %d\n", ebnum, blocks); |
107 | return -EIO; | |
108 | } | |
109 | ||
110 | return 0; | |
111 | } | |
112 | ||
72069be9 AB |
113 | static int erase_whole_device(void) |
114 | { | |
115 | int err; | |
116 | unsigned int i; | |
117 | ||
118 | for (i = 0; i < ebcnt; ++i) { | |
119 | if (bbt[i]) | |
120 | continue; | |
121 | err = erase_eraseblock(i); | |
122 | if (err) | |
123 | return err; | |
124 | cond_resched(); | |
125 | } | |
126 | return 0; | |
127 | } | |
128 | ||
129 | static int write_eraseblock(int ebnum) | |
130 | { | |
30fa9848 | 131 | size_t written; |
72069be9 AB |
132 | int err = 0; |
133 | loff_t addr = ebnum * mtd->erasesize; | |
134 | ||
eda95cbf | 135 | err = mtd_write(mtd, addr, mtd->erasesize, &written, iobuf); |
72069be9 | 136 | if (err || written != mtd->erasesize) { |
2c70d292 | 137 | pr_err("error: write failed at %#llx\n", addr); |
72069be9 AB |
138 | if (!err) |
139 | err = -EINVAL; | |
140 | } | |
141 | ||
142 | return err; | |
143 | } | |
144 | ||
145 | static int write_eraseblock_by_page(int ebnum) | |
146 | { | |
30fa9848 | 147 | size_t written; |
72069be9 AB |
148 | int i, err = 0; |
149 | loff_t addr = ebnum * mtd->erasesize; | |
150 | void *buf = iobuf; | |
151 | ||
152 | for (i = 0; i < pgcnt; i++) { | |
eda95cbf | 153 | err = mtd_write(mtd, addr, pgsize, &written, buf); |
72069be9 | 154 | if (err || written != pgsize) { |
2c70d292 | 155 | pr_err("error: write failed at %#llx\n", |
72069be9 AB |
156 | addr); |
157 | if (!err) | |
158 | err = -EINVAL; | |
159 | break; | |
160 | } | |
161 | addr += pgsize; | |
162 | buf += pgsize; | |
163 | } | |
164 | ||
165 | return err; | |
166 | } | |
167 | ||
168 | static int write_eraseblock_by_2pages(int ebnum) | |
169 | { | |
30fa9848 | 170 | size_t written, sz = pgsize * 2; |
72069be9 AB |
171 | int i, n = pgcnt / 2, err = 0; |
172 | loff_t addr = ebnum * mtd->erasesize; | |
173 | void *buf = iobuf; | |
174 | ||
175 | for (i = 0; i < n; i++) { | |
eda95cbf | 176 | err = mtd_write(mtd, addr, sz, &written, buf); |
72069be9 | 177 | if (err || written != sz) { |
2c70d292 | 178 | pr_err("error: write failed at %#llx\n", |
72069be9 AB |
179 | addr); |
180 | if (!err) | |
181 | err = -EINVAL; | |
182 | return err; | |
183 | } | |
184 | addr += sz; | |
185 | buf += sz; | |
186 | } | |
187 | if (pgcnt % 2) { | |
eda95cbf | 188 | err = mtd_write(mtd, addr, pgsize, &written, buf); |
72069be9 | 189 | if (err || written != pgsize) { |
2c70d292 | 190 | pr_err("error: write failed at %#llx\n", |
72069be9 AB |
191 | addr); |
192 | if (!err) | |
193 | err = -EINVAL; | |
194 | } | |
195 | } | |
196 | ||
197 | return err; | |
198 | } | |
199 | ||
200 | static int read_eraseblock(int ebnum) | |
201 | { | |
30fa9848 | 202 | size_t read; |
72069be9 AB |
203 | int err = 0; |
204 | loff_t addr = ebnum * mtd->erasesize; | |
205 | ||
329ad399 | 206 | err = mtd_read(mtd, addr, mtd->erasesize, &read, iobuf); |
72069be9 | 207 | /* Ignore corrected ECC errors */ |
d57f4054 | 208 | if (mtd_is_bitflip(err)) |
72069be9 AB |
209 | err = 0; |
210 | if (err || read != mtd->erasesize) { | |
2c70d292 | 211 | pr_err("error: read failed at %#llx\n", addr); |
72069be9 AB |
212 | if (!err) |
213 | err = -EINVAL; | |
214 | } | |
215 | ||
216 | return err; | |
217 | } | |
218 | ||
219 | static int read_eraseblock_by_page(int ebnum) | |
220 | { | |
30fa9848 | 221 | size_t read; |
72069be9 AB |
222 | int i, err = 0; |
223 | loff_t addr = ebnum * mtd->erasesize; | |
224 | void *buf = iobuf; | |
225 | ||
226 | for (i = 0; i < pgcnt; i++) { | |
329ad399 | 227 | err = mtd_read(mtd, addr, pgsize, &read, buf); |
72069be9 | 228 | /* Ignore corrected ECC errors */ |
d57f4054 | 229 | if (mtd_is_bitflip(err)) |
72069be9 AB |
230 | err = 0; |
231 | if (err || read != pgsize) { | |
2c70d292 | 232 | pr_err("error: read failed at %#llx\n", |
72069be9 AB |
233 | addr); |
234 | if (!err) | |
235 | err = -EINVAL; | |
236 | break; | |
237 | } | |
238 | addr += pgsize; | |
239 | buf += pgsize; | |
240 | } | |
241 | ||
242 | return err; | |
243 | } | |
244 | ||
245 | static int read_eraseblock_by_2pages(int ebnum) | |
246 | { | |
30fa9848 | 247 | size_t read, sz = pgsize * 2; |
72069be9 AB |
248 | int i, n = pgcnt / 2, err = 0; |
249 | loff_t addr = ebnum * mtd->erasesize; | |
250 | void *buf = iobuf; | |
251 | ||
252 | for (i = 0; i < n; i++) { | |
329ad399 | 253 | err = mtd_read(mtd, addr, sz, &read, buf); |
72069be9 | 254 | /* Ignore corrected ECC errors */ |
d57f4054 | 255 | if (mtd_is_bitflip(err)) |
72069be9 AB |
256 | err = 0; |
257 | if (err || read != sz) { | |
2c70d292 | 258 | pr_err("error: read failed at %#llx\n", |
72069be9 AB |
259 | addr); |
260 | if (!err) | |
261 | err = -EINVAL; | |
262 | return err; | |
263 | } | |
264 | addr += sz; | |
265 | buf += sz; | |
266 | } | |
267 | if (pgcnt % 2) { | |
329ad399 | 268 | err = mtd_read(mtd, addr, pgsize, &read, buf); |
72069be9 | 269 | /* Ignore corrected ECC errors */ |
d57f4054 | 270 | if (mtd_is_bitflip(err)) |
72069be9 AB |
271 | err = 0; |
272 | if (err || read != pgsize) { | |
2c70d292 | 273 | pr_err("error: read failed at %#llx\n", |
72069be9 AB |
274 | addr); |
275 | if (!err) | |
276 | err = -EINVAL; | |
277 | } | |
278 | } | |
279 | ||
280 | return err; | |
281 | } | |
282 | ||
283 | static int is_block_bad(int ebnum) | |
284 | { | |
285 | loff_t addr = ebnum * mtd->erasesize; | |
286 | int ret; | |
287 | ||
7086c19d | 288 | ret = mtd_block_isbad(mtd, addr); |
72069be9 | 289 | if (ret) |
2c70d292 | 290 | pr_info("block %d is bad\n", ebnum); |
72069be9 AB |
291 | return ret; |
292 | } | |
293 | ||
294 | static inline void start_timing(void) | |
295 | { | |
296 | do_gettimeofday(&start); | |
297 | } | |
298 | ||
299 | static inline void stop_timing(void) | |
300 | { | |
301 | do_gettimeofday(&finish); | |
302 | } | |
303 | ||
304 | static long calc_speed(void) | |
305 | { | |
e70727e4 DL |
306 | uint64_t k; |
307 | long ms; | |
72069be9 AB |
308 | |
309 | ms = (finish.tv_sec - start.tv_sec) * 1000 + | |
310 | (finish.tv_usec - start.tv_usec) / 1000; | |
e70727e4 DL |
311 | if (ms == 0) |
312 | return 0; | |
313 | k = goodebcnt * (mtd->erasesize / 1024) * 1000; | |
314 | do_div(k, ms); | |
315 | return k; | |
72069be9 AB |
316 | } |
317 | ||
318 | static int scan_for_bad_eraseblocks(void) | |
319 | { | |
320 | int i, bad = 0; | |
321 | ||
2bfefa4c | 322 | bbt = kzalloc(ebcnt, GFP_KERNEL); |
72069be9 | 323 | if (!bbt) { |
2c70d292 | 324 | pr_err("error: cannot allocate memory\n"); |
72069be9 AB |
325 | return -ENOMEM; |
326 | } | |
72069be9 | 327 | |
8f461a73 | 328 | if (!mtd_can_have_bb(mtd)) |
f5e2bae0 MTS |
329 | goto out; |
330 | ||
2c70d292 | 331 | pr_info("scanning for bad eraseblocks\n"); |
72069be9 AB |
332 | for (i = 0; i < ebcnt; ++i) { |
333 | bbt[i] = is_block_bad(i) ? 1 : 0; | |
334 | if (bbt[i]) | |
335 | bad += 1; | |
336 | cond_resched(); | |
337 | } | |
2c70d292 | 338 | pr_info("scanned %d eraseblocks, %d are bad\n", i, bad); |
f5e2bae0 | 339 | out: |
72069be9 AB |
340 | goodebcnt = ebcnt - bad; |
341 | return 0; | |
342 | } | |
343 | ||
344 | static int __init mtd_speedtest_init(void) | |
345 | { | |
4085bcc6 | 346 | int err, i, blocks, j, k; |
72069be9 AB |
347 | long speed; |
348 | uint64_t tmp; | |
349 | ||
350 | printk(KERN_INFO "\n"); | |
351 | printk(KERN_INFO "=================================================\n"); | |
7406060e WS |
352 | |
353 | if (dev < 0) { | |
2c70d292 VN |
354 | pr_info("Please specify a valid mtd-device via module paramter\n"); |
355 | pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n"); | |
7406060e WS |
356 | return -EINVAL; |
357 | } | |
358 | ||
fc7fe769 | 359 | if (count) |
2c70d292 | 360 | pr_info("MTD device: %d count: %d\n", dev, count); |
fc7fe769 | 361 | else |
2c70d292 | 362 | pr_info("MTD device: %d\n", dev); |
72069be9 AB |
363 | |
364 | mtd = get_mtd_device(NULL, dev); | |
365 | if (IS_ERR(mtd)) { | |
366 | err = PTR_ERR(mtd); | |
2c70d292 | 367 | pr_err("error: cannot get MTD device\n"); |
72069be9 AB |
368 | return err; |
369 | } | |
370 | ||
371 | if (mtd->writesize == 1) { | |
2c70d292 | 372 | pr_info("not NAND flash, assume page size is 512 " |
72069be9 AB |
373 | "bytes.\n"); |
374 | pgsize = 512; | |
375 | } else | |
376 | pgsize = mtd->writesize; | |
377 | ||
378 | tmp = mtd->size; | |
379 | do_div(tmp, mtd->erasesize); | |
380 | ebcnt = tmp; | |
f5e2bae0 | 381 | pgcnt = mtd->erasesize / pgsize; |
72069be9 | 382 | |
2c70d292 | 383 | pr_info("MTD device size %llu, eraseblock size %u, " |
72069be9 AB |
384 | "page size %u, count of eraseblocks %u, pages per " |
385 | "eraseblock %u, OOB size %u\n", | |
386 | (unsigned long long)mtd->size, mtd->erasesize, | |
387 | pgsize, ebcnt, pgcnt, mtd->oobsize); | |
388 | ||
fc7fe769 AH |
389 | if (count > 0 && count < ebcnt) |
390 | ebcnt = count; | |
391 | ||
72069be9 AB |
392 | err = -ENOMEM; |
393 | iobuf = kmalloc(mtd->erasesize, GFP_KERNEL); | |
394 | if (!iobuf) { | |
2c70d292 | 395 | pr_err("error: cannot allocate memory\n"); |
72069be9 AB |
396 | goto out; |
397 | } | |
398 | ||
72069be9 AB |
399 | set_random_data(iobuf, mtd->erasesize); |
400 | ||
401 | err = scan_for_bad_eraseblocks(); | |
402 | if (err) | |
403 | goto out; | |
404 | ||
405 | err = erase_whole_device(); | |
406 | if (err) | |
407 | goto out; | |
408 | ||
409 | /* Write all eraseblocks, 1 eraseblock at a time */ | |
2c70d292 | 410 | pr_info("testing eraseblock write speed\n"); |
72069be9 AB |
411 | start_timing(); |
412 | for (i = 0; i < ebcnt; ++i) { | |
413 | if (bbt[i]) | |
414 | continue; | |
415 | err = write_eraseblock(i); | |
416 | if (err) | |
417 | goto out; | |
418 | cond_resched(); | |
419 | } | |
420 | stop_timing(); | |
421 | speed = calc_speed(); | |
2c70d292 | 422 | pr_info("eraseblock write speed is %ld KiB/s\n", speed); |
72069be9 AB |
423 | |
424 | /* Read all eraseblocks, 1 eraseblock at a time */ | |
2c70d292 | 425 | pr_info("testing eraseblock read speed\n"); |
72069be9 AB |
426 | start_timing(); |
427 | for (i = 0; i < ebcnt; ++i) { | |
428 | if (bbt[i]) | |
429 | continue; | |
430 | err = read_eraseblock(i); | |
431 | if (err) | |
432 | goto out; | |
433 | cond_resched(); | |
434 | } | |
435 | stop_timing(); | |
436 | speed = calc_speed(); | |
2c70d292 | 437 | pr_info("eraseblock read speed is %ld KiB/s\n", speed); |
72069be9 AB |
438 | |
439 | err = erase_whole_device(); | |
440 | if (err) | |
441 | goto out; | |
442 | ||
443 | /* Write all eraseblocks, 1 page at a time */ | |
2c70d292 | 444 | pr_info("testing page write speed\n"); |
72069be9 AB |
445 | start_timing(); |
446 | for (i = 0; i < ebcnt; ++i) { | |
447 | if (bbt[i]) | |
448 | continue; | |
449 | err = write_eraseblock_by_page(i); | |
450 | if (err) | |
451 | goto out; | |
452 | cond_resched(); | |
453 | } | |
454 | stop_timing(); | |
455 | speed = calc_speed(); | |
2c70d292 | 456 | pr_info("page write speed is %ld KiB/s\n", speed); |
72069be9 AB |
457 | |
458 | /* Read all eraseblocks, 1 page at a time */ | |
2c70d292 | 459 | pr_info("testing page read speed\n"); |
72069be9 AB |
460 | start_timing(); |
461 | for (i = 0; i < ebcnt; ++i) { | |
462 | if (bbt[i]) | |
463 | continue; | |
464 | err = read_eraseblock_by_page(i); | |
465 | if (err) | |
466 | goto out; | |
467 | cond_resched(); | |
468 | } | |
469 | stop_timing(); | |
470 | speed = calc_speed(); | |
2c70d292 | 471 | pr_info("page read speed is %ld KiB/s\n", speed); |
72069be9 AB |
472 | |
473 | err = erase_whole_device(); | |
474 | if (err) | |
475 | goto out; | |
476 | ||
477 | /* Write all eraseblocks, 2 pages at a time */ | |
2c70d292 | 478 | pr_info("testing 2 page write speed\n"); |
72069be9 AB |
479 | start_timing(); |
480 | for (i = 0; i < ebcnt; ++i) { | |
481 | if (bbt[i]) | |
482 | continue; | |
483 | err = write_eraseblock_by_2pages(i); | |
484 | if (err) | |
485 | goto out; | |
486 | cond_resched(); | |
487 | } | |
488 | stop_timing(); | |
489 | speed = calc_speed(); | |
2c70d292 | 490 | pr_info("2 page write speed is %ld KiB/s\n", speed); |
72069be9 AB |
491 | |
492 | /* Read all eraseblocks, 2 pages at a time */ | |
2c70d292 | 493 | pr_info("testing 2 page read speed\n"); |
72069be9 AB |
494 | start_timing(); |
495 | for (i = 0; i < ebcnt; ++i) { | |
496 | if (bbt[i]) | |
497 | continue; | |
498 | err = read_eraseblock_by_2pages(i); | |
499 | if (err) | |
500 | goto out; | |
501 | cond_resched(); | |
502 | } | |
503 | stop_timing(); | |
504 | speed = calc_speed(); | |
2c70d292 | 505 | pr_info("2 page read speed is %ld KiB/s\n", speed); |
72069be9 AB |
506 | |
507 | /* Erase all eraseblocks */ | |
2c70d292 | 508 | pr_info("Testing erase speed\n"); |
72069be9 AB |
509 | start_timing(); |
510 | for (i = 0; i < ebcnt; ++i) { | |
511 | if (bbt[i]) | |
512 | continue; | |
513 | err = erase_eraseblock(i); | |
514 | if (err) | |
515 | goto out; | |
516 | cond_resched(); | |
517 | } | |
518 | stop_timing(); | |
519 | speed = calc_speed(); | |
2c70d292 | 520 | pr_info("erase speed is %ld KiB/s\n", speed); |
72069be9 | 521 | |
4085bcc6 RT |
522 | /* Multi-block erase all eraseblocks */ |
523 | for (k = 1; k < 7; k++) { | |
524 | blocks = 1 << k; | |
2c70d292 | 525 | pr_info("Testing %dx multi-block erase speed\n", |
4085bcc6 RT |
526 | blocks); |
527 | start_timing(); | |
528 | for (i = 0; i < ebcnt; ) { | |
529 | for (j = 0; j < blocks && (i + j) < ebcnt; j++) | |
530 | if (bbt[i + j]) | |
531 | break; | |
532 | if (j < 1) { | |
533 | i++; | |
534 | continue; | |
535 | } | |
536 | err = multiblock_erase(i, j); | |
537 | if (err) | |
538 | goto out; | |
539 | cond_resched(); | |
540 | i += j; | |
541 | } | |
542 | stop_timing(); | |
543 | speed = calc_speed(); | |
2c70d292 | 544 | pr_info("%dx multi-block erase speed is %ld KiB/s\n", |
4085bcc6 RT |
545 | blocks, speed); |
546 | } | |
2c70d292 | 547 | pr_info("finished\n"); |
72069be9 AB |
548 | out: |
549 | kfree(iobuf); | |
550 | kfree(bbt); | |
551 | put_mtd_device(mtd); | |
552 | if (err) | |
2c70d292 | 553 | pr_info("error %d occurred\n", err); |
72069be9 AB |
554 | printk(KERN_INFO "=================================================\n"); |
555 | return err; | |
556 | } | |
557 | module_init(mtd_speedtest_init); | |
558 | ||
559 | static void __exit mtd_speedtest_exit(void) | |
560 | { | |
561 | return; | |
562 | } | |
563 | module_exit(mtd_speedtest_exit); | |
564 | ||
565 | MODULE_DESCRIPTION("Speed test module"); | |
566 | MODULE_AUTHOR("Adrian Hunter"); | |
567 | MODULE_LICENSE("GPL"); |