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