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