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 | |
59b0816d AM |
33 | #include "mtd_test.h" |
34 | ||
7406060e | 35 | static int dev = -EINVAL; |
72069be9 AB |
36 | module_param(dev, int, S_IRUGO); |
37 | MODULE_PARM_DESC(dev, "MTD device number to use"); | |
38 | ||
fc7fe769 AH |
39 | static int count; |
40 | module_param(count, int, S_IRUGO); | |
41 | MODULE_PARM_DESC(count, "Maximum number of eraseblocks to use " | |
42 | "(0 means use all)"); | |
43 | ||
72069be9 AB |
44 | static struct mtd_info *mtd; |
45 | static unsigned char *iobuf; | |
46 | static unsigned char *bbt; | |
47 | ||
48 | static int pgsize; | |
49 | static int ebcnt; | |
50 | static int pgcnt; | |
51 | static int goodebcnt; | |
52 | static struct timeval start, finish; | |
72069be9 | 53 | |
4085bcc6 RT |
54 | static int multiblock_erase(int ebnum, int blocks) |
55 | { | |
56 | int err; | |
57 | struct erase_info ei; | |
58 | loff_t addr = ebnum * mtd->erasesize; | |
59 | ||
60 | memset(&ei, 0, sizeof(struct erase_info)); | |
61 | ei.mtd = mtd; | |
62 | ei.addr = addr; | |
63 | ei.len = mtd->erasesize * blocks; | |
64 | ||
7e1f0dc0 | 65 | err = mtd_erase(mtd, &ei); |
4085bcc6 | 66 | if (err) { |
2c70d292 | 67 | pr_err("error %d while erasing EB %d, blocks %d\n", |
4085bcc6 RT |
68 | err, ebnum, blocks); |
69 | return err; | |
70 | } | |
71 | ||
72 | if (ei.state == MTD_ERASE_FAILED) { | |
2c70d292 | 73 | pr_err("some erase error occurred at EB %d," |
4085bcc6 RT |
74 | "blocks %d\n", ebnum, blocks); |
75 | return -EIO; | |
76 | } | |
77 | ||
78 | return 0; | |
79 | } | |
80 | ||
72069be9 AB |
81 | static int write_eraseblock(int ebnum) |
82 | { | |
72069be9 AB |
83 | loff_t addr = ebnum * mtd->erasesize; |
84 | ||
8a9f4aa3 | 85 | return mtdtest_write(mtd, addr, mtd->erasesize, iobuf); |
72069be9 AB |
86 | } |
87 | ||
88 | static int write_eraseblock_by_page(int ebnum) | |
89 | { | |
72069be9 AB |
90 | int i, err = 0; |
91 | loff_t addr = ebnum * mtd->erasesize; | |
92 | void *buf = iobuf; | |
93 | ||
94 | for (i = 0; i < pgcnt; i++) { | |
59b0816d | 95 | err = mtdtest_write(mtd, addr, pgsize, buf); |
8a9f4aa3 | 96 | if (err) |
72069be9 | 97 | break; |
72069be9 AB |
98 | addr += pgsize; |
99 | buf += pgsize; | |
100 | } | |
101 | ||
102 | return err; | |
103 | } | |
104 | ||
105 | static int write_eraseblock_by_2pages(int ebnum) | |
106 | { | |
59b0816d | 107 | size_t sz = pgsize * 2; |
72069be9 AB |
108 | int i, n = pgcnt / 2, err = 0; |
109 | loff_t addr = ebnum * mtd->erasesize; | |
110 | void *buf = iobuf; | |
111 | ||
112 | for (i = 0; i < n; i++) { | |
59b0816d | 113 | err = mtdtest_write(mtd, addr, sz, buf); |
8a9f4aa3 | 114 | if (err) |
72069be9 | 115 | return err; |
72069be9 AB |
116 | addr += sz; |
117 | buf += sz; | |
118 | } | |
8a9f4aa3 | 119 | if (pgcnt % 2) |
59b0816d | 120 | err = mtdtest_write(mtd, addr, pgsize, buf); |
72069be9 AB |
121 | |
122 | return err; | |
123 | } | |
124 | ||
125 | static int read_eraseblock(int ebnum) | |
126 | { | |
72069be9 AB |
127 | loff_t addr = ebnum * mtd->erasesize; |
128 | ||
abc173ad | 129 | return mtdtest_read(mtd, addr, mtd->erasesize, iobuf); |
72069be9 AB |
130 | } |
131 | ||
132 | static int read_eraseblock_by_page(int ebnum) | |
133 | { | |
72069be9 AB |
134 | int i, err = 0; |
135 | loff_t addr = ebnum * mtd->erasesize; | |
136 | void *buf = iobuf; | |
137 | ||
138 | for (i = 0; i < pgcnt; i++) { | |
59b0816d | 139 | err = mtdtest_read(mtd, addr, pgsize, buf); |
abc173ad | 140 | if (err) |
72069be9 | 141 | break; |
72069be9 AB |
142 | addr += pgsize; |
143 | buf += pgsize; | |
144 | } | |
145 | ||
146 | return err; | |
147 | } | |
148 | ||
149 | static int read_eraseblock_by_2pages(int ebnum) | |
150 | { | |
59b0816d | 151 | size_t sz = pgsize * 2; |
72069be9 AB |
152 | int i, n = pgcnt / 2, err = 0; |
153 | loff_t addr = ebnum * mtd->erasesize; | |
154 | void *buf = iobuf; | |
155 | ||
156 | for (i = 0; i < n; i++) { | |
59b0816d | 157 | err = mtdtest_read(mtd, addr, sz, buf); |
abc173ad | 158 | if (err) |
72069be9 | 159 | return err; |
72069be9 AB |
160 | addr += sz; |
161 | buf += sz; | |
162 | } | |
abc173ad | 163 | if (pgcnt % 2) |
59b0816d | 164 | err = mtdtest_read(mtd, addr, pgsize, buf); |
72069be9 AB |
165 | |
166 | return err; | |
167 | } | |
168 | ||
72069be9 AB |
169 | static inline void start_timing(void) |
170 | { | |
171 | do_gettimeofday(&start); | |
172 | } | |
173 | ||
174 | static inline void stop_timing(void) | |
175 | { | |
176 | do_gettimeofday(&finish); | |
177 | } | |
178 | ||
179 | static long calc_speed(void) | |
180 | { | |
e70727e4 DL |
181 | uint64_t k; |
182 | long ms; | |
72069be9 AB |
183 | |
184 | ms = (finish.tv_sec - start.tv_sec) * 1000 + | |
185 | (finish.tv_usec - start.tv_usec) / 1000; | |
e70727e4 DL |
186 | if (ms == 0) |
187 | return 0; | |
188 | k = goodebcnt * (mtd->erasesize / 1024) * 1000; | |
189 | do_div(k, ms); | |
190 | return k; | |
72069be9 AB |
191 | } |
192 | ||
72069be9 AB |
193 | static 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; | |
272 | cond_resched(); | |
273 | } | |
274 | stop_timing(); | |
275 | speed = calc_speed(); | |
2c70d292 | 276 | pr_info("eraseblock write speed is %ld KiB/s\n", speed); |
72069be9 AB |
277 | |
278 | /* Read all eraseblocks, 1 eraseblock at a time */ | |
2c70d292 | 279 | pr_info("testing eraseblock read speed\n"); |
72069be9 AB |
280 | start_timing(); |
281 | for (i = 0; i < ebcnt; ++i) { | |
282 | if (bbt[i]) | |
283 | continue; | |
284 | err = read_eraseblock(i); | |
285 | if (err) | |
286 | goto out; | |
287 | cond_resched(); | |
288 | } | |
289 | stop_timing(); | |
290 | speed = calc_speed(); | |
2c70d292 | 291 | pr_info("eraseblock read speed is %ld KiB/s\n", speed); |
72069be9 | 292 | |
59b0816d | 293 | err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt); |
72069be9 AB |
294 | if (err) |
295 | goto out; | |
296 | ||
297 | /* Write all eraseblocks, 1 page at a time */ | |
2c70d292 | 298 | pr_info("testing page write speed\n"); |
72069be9 AB |
299 | start_timing(); |
300 | for (i = 0; i < ebcnt; ++i) { | |
301 | if (bbt[i]) | |
302 | continue; | |
303 | err = write_eraseblock_by_page(i); | |
304 | if (err) | |
305 | goto out; | |
306 | cond_resched(); | |
307 | } | |
308 | stop_timing(); | |
309 | speed = calc_speed(); | |
2c70d292 | 310 | pr_info("page write speed is %ld KiB/s\n", speed); |
72069be9 AB |
311 | |
312 | /* Read all eraseblocks, 1 page at a time */ | |
2c70d292 | 313 | pr_info("testing page read speed\n"); |
72069be9 AB |
314 | start_timing(); |
315 | for (i = 0; i < ebcnt; ++i) { | |
316 | if (bbt[i]) | |
317 | continue; | |
318 | err = read_eraseblock_by_page(i); | |
319 | if (err) | |
320 | goto out; | |
321 | cond_resched(); | |
322 | } | |
323 | stop_timing(); | |
324 | speed = calc_speed(); | |
2c70d292 | 325 | pr_info("page read speed is %ld KiB/s\n", speed); |
72069be9 | 326 | |
59b0816d | 327 | err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt); |
72069be9 AB |
328 | if (err) |
329 | goto out; | |
330 | ||
331 | /* Write all eraseblocks, 2 pages at a time */ | |
2c70d292 | 332 | pr_info("testing 2 page write speed\n"); |
72069be9 AB |
333 | start_timing(); |
334 | for (i = 0; i < ebcnt; ++i) { | |
335 | if (bbt[i]) | |
336 | continue; | |
337 | err = write_eraseblock_by_2pages(i); | |
338 | if (err) | |
339 | goto out; | |
340 | cond_resched(); | |
341 | } | |
342 | stop_timing(); | |
343 | speed = calc_speed(); | |
2c70d292 | 344 | pr_info("2 page write speed is %ld KiB/s\n", speed); |
72069be9 AB |
345 | |
346 | /* Read all eraseblocks, 2 pages at a time */ | |
2c70d292 | 347 | pr_info("testing 2 page read speed\n"); |
72069be9 AB |
348 | start_timing(); |
349 | for (i = 0; i < ebcnt; ++i) { | |
350 | if (bbt[i]) | |
351 | continue; | |
352 | err = read_eraseblock_by_2pages(i); | |
353 | if (err) | |
354 | goto out; | |
355 | cond_resched(); | |
356 | } | |
357 | stop_timing(); | |
358 | speed = calc_speed(); | |
2c70d292 | 359 | pr_info("2 page read speed is %ld KiB/s\n", speed); |
72069be9 AB |
360 | |
361 | /* Erase all eraseblocks */ | |
2c70d292 | 362 | pr_info("Testing erase speed\n"); |
72069be9 | 363 | start_timing(); |
59b0816d AM |
364 | err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt); |
365 | if (err) | |
366 | goto out; | |
72069be9 AB |
367 | stop_timing(); |
368 | speed = calc_speed(); | |
2c70d292 | 369 | pr_info("erase speed is %ld KiB/s\n", speed); |
72069be9 | 370 | |
4085bcc6 RT |
371 | /* Multi-block erase all eraseblocks */ |
372 | for (k = 1; k < 7; k++) { | |
373 | blocks = 1 << k; | |
2c70d292 | 374 | pr_info("Testing %dx multi-block erase speed\n", |
4085bcc6 RT |
375 | blocks); |
376 | start_timing(); | |
377 | for (i = 0; i < ebcnt; ) { | |
378 | for (j = 0; j < blocks && (i + j) < ebcnt; j++) | |
379 | if (bbt[i + j]) | |
380 | break; | |
381 | if (j < 1) { | |
382 | i++; | |
383 | continue; | |
384 | } | |
385 | err = multiblock_erase(i, j); | |
386 | if (err) | |
387 | goto out; | |
388 | cond_resched(); | |
389 | i += j; | |
390 | } | |
391 | stop_timing(); | |
392 | speed = calc_speed(); | |
2c70d292 | 393 | pr_info("%dx multi-block erase speed is %ld KiB/s\n", |
4085bcc6 RT |
394 | blocks, speed); |
395 | } | |
2c70d292 | 396 | pr_info("finished\n"); |
72069be9 AB |
397 | out: |
398 | kfree(iobuf); | |
399 | kfree(bbt); | |
400 | put_mtd_device(mtd); | |
401 | if (err) | |
2c70d292 | 402 | pr_info("error %d occurred\n", err); |
72069be9 AB |
403 | printk(KERN_INFO "=================================================\n"); |
404 | return err; | |
405 | } | |
406 | module_init(mtd_speedtest_init); | |
407 | ||
408 | static void __exit mtd_speedtest_exit(void) | |
409 | { | |
410 | return; | |
411 | } | |
412 | module_exit(mtd_speedtest_exit); | |
413 | ||
414 | MODULE_DESCRIPTION("Speed test module"); | |
415 | MODULE_AUTHOR("Adrian Hunter"); | |
416 | MODULE_LICENSE("GPL"); |