[media] Fix typo in comments
[linux-2.6-block.git] / drivers / mtd / tests / torturetest.c
CommitLineData
4db451a7
AB
1/*
2 * Copyright (C) 2006-2008 Artem Bityutskiy
3 * Copyright (C) 2006-2008 Jarkko Lavinen
4 * Copyright (C) 2006-2008 Adrian Hunter
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; see the file COPYING. If not, write to the Free Software
17 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 * Authors: Artem Bityutskiy, Jarkko Lavinen, Adria Hunter
20 *
21 * WARNING: this test program may kill your flash and your device. Do not
22 * use it unless you know what you do. Authors are not responsible for any
23 * damage caused by this program.
24 */
25
95637c3a
VN
26#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
4db451a7
AB
28#include <linux/init.h>
29#include <linux/module.h>
30#include <linux/moduleparam.h>
31#include <linux/err.h>
32#include <linux/mtd/mtd.h>
5a0e3ad6 33#include <linux/slab.h>
4db451a7 34#include <linux/sched.h>
6cf78358 35#include "mtd_test.h"
4db451a7 36
4db451a7
AB
37#define RETRIES 3
38
39static int eb = 8;
40module_param(eb, int, S_IRUGO);
41MODULE_PARM_DESC(eb, "eraseblock number within the selected MTD device");
42
43static int ebcnt = 32;
44module_param(ebcnt, int, S_IRUGO);
45MODULE_PARM_DESC(ebcnt, "number of consecutive eraseblocks to torture");
46
47static int pgcnt;
48module_param(pgcnt, int, S_IRUGO);
49MODULE_PARM_DESC(pgcnt, "number of pages per eraseblock to torture (0 => all)");
50
7406060e 51static int dev = -EINVAL;
4db451a7
AB
52module_param(dev, int, S_IRUGO);
53MODULE_PARM_DESC(dev, "MTD device number to use");
54
55static int gran = 512;
56module_param(gran, int, S_IRUGO);
57MODULE_PARM_DESC(gran, "how often the status information should be printed");
58
59static int check = 1;
60module_param(check, int, S_IRUGO);
61MODULE_PARM_DESC(check, "if the written data should be checked");
62
63static unsigned int cycles_count;
64module_param(cycles_count, uint, S_IRUGO);
65MODULE_PARM_DESC(cycles_count, "how many erase cycles to do "
66 "(infinite by default)");
67
68static struct mtd_info *mtd;
69
70/* This buffer contains 0x555555...0xAAAAAA... pattern */
71static unsigned char *patt_5A5;
72/* This buffer contains 0xAAAAAA...0x555555... pattern */
73static unsigned char *patt_A5A;
74/* This buffer contains all 0xFF bytes */
75static unsigned char *patt_FF;
76/* This a temporary buffer is use when checking data */
77static unsigned char *check_buf;
78/* How many erase cycles were done */
79static unsigned int erase_cycles;
80
81static int pgsize;
82static struct timeval start, finish;
83
84static void report_corrupt(unsigned char *read, unsigned char *written);
85
86static inline void start_timing(void)
87{
88 do_gettimeofday(&start);
89}
90
91static inline void stop_timing(void)
92{
93 do_gettimeofday(&finish);
94}
95
4db451a7
AB
96/*
97 * Check that the contents of eraseblock number @enbum is equivalent to the
98 * @buf buffer.
99 */
100static inline int check_eraseblock(int ebnum, unsigned char *buf)
101{
102 int err, retries = 0;
30fa9848 103 size_t read;
4db451a7
AB
104 loff_t addr = ebnum * mtd->erasesize;
105 size_t len = mtd->erasesize;
106
107 if (pgcnt) {
108 addr = (ebnum + 1) * mtd->erasesize - pgcnt * pgsize;
109 len = pgcnt * pgsize;
110 }
111
112retry:
329ad399 113 err = mtd_read(mtd, addr, len, &read, check_buf);
d57f4054 114 if (mtd_is_bitflip(err))
95637c3a 115 pr_err("single bit flip occurred at EB %d "
4db451a7
AB
116 "MTD reported that it was fixed.\n", ebnum);
117 else if (err) {
95637c3a 118 pr_err("error %d while reading EB %d, "
4db451a7
AB
119 "read %zd\n", err, ebnum, read);
120 return err;
121 }
122
123 if (read != len) {
95637c3a 124 pr_err("failed to read %zd bytes from EB %d, "
4db451a7
AB
125 "read only %zd, but no error reported\n",
126 len, ebnum, read);
127 return -EIO;
128 }
129
130 if (memcmp(buf, check_buf, len)) {
95637c3a 131 pr_err("read wrong data from EB %d\n", ebnum);
4db451a7
AB
132 report_corrupt(check_buf, buf);
133
134 if (retries++ < RETRIES) {
135 /* Try read again */
136 yield();
95637c3a 137 pr_info("re-try reading data from EB %d\n",
4db451a7
AB
138 ebnum);
139 goto retry;
140 } else {
95637c3a 141 pr_info("retried %d times, still errors, "
4db451a7
AB
142 "give-up\n", RETRIES);
143 return -EINVAL;
144 }
145 }
146
147 if (retries != 0)
95637c3a 148 pr_info("only attempt number %d was OK (!!!)\n",
4db451a7
AB
149 retries);
150
151 return 0;
152}
153
154static inline int write_pattern(int ebnum, void *buf)
155{
156 int err;
30fa9848 157 size_t written;
4db451a7
AB
158 loff_t addr = ebnum * mtd->erasesize;
159 size_t len = mtd->erasesize;
160
161 if (pgcnt) {
162 addr = (ebnum + 1) * mtd->erasesize - pgcnt * pgsize;
163 len = pgcnt * pgsize;
164 }
eda95cbf 165 err = mtd_write(mtd, addr, len, &written, buf);
4db451a7 166 if (err) {
95637c3a 167 pr_err("error %d while writing EB %d, written %zd"
4db451a7
AB
168 " bytes\n", err, ebnum, written);
169 return err;
170 }
171 if (written != len) {
95637c3a 172 pr_info("written only %zd bytes of %zd, but no error"
4db451a7
AB
173 " reported\n", written, len);
174 return -EIO;
175 }
176
177 return 0;
178}
179
180static int __init tort_init(void)
181{
182 int err = 0, i, infinite = !cycles_count;
6cf78358 183 unsigned char *bad_ebs;
4db451a7
AB
184
185 printk(KERN_INFO "\n");
186 printk(KERN_INFO "=================================================\n");
95637c3a 187 pr_info("Warning: this program is trying to wear out your "
4db451a7 188 "flash, stop it if this is not wanted.\n");
7406060e
WS
189
190 if (dev < 0) {
064a7694 191 pr_info("Please specify a valid mtd-device via module parameter\n");
95637c3a 192 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
7406060e
WS
193 return -EINVAL;
194 }
195
95637c3a
VN
196 pr_info("MTD device: %d\n", dev);
197 pr_info("torture %d eraseblocks (%d-%d) of mtd%d\n",
4db451a7
AB
198 ebcnt, eb, eb + ebcnt - 1, dev);
199 if (pgcnt)
95637c3a 200 pr_info("torturing just %d pages per eraseblock\n",
4db451a7 201 pgcnt);
95637c3a 202 pr_info("write verify %s\n", check ? "enabled" : "disabled");
4db451a7
AB
203
204 mtd = get_mtd_device(NULL, dev);
205 if (IS_ERR(mtd)) {
206 err = PTR_ERR(mtd);
95637c3a 207 pr_err("error: cannot get MTD device\n");
4db451a7
AB
208 return err;
209 }
210
211 if (mtd->writesize == 1) {
95637c3a 212 pr_info("not NAND flash, assume page size is 512 "
4db451a7
AB
213 "bytes.\n");
214 pgsize = 512;
215 } else
216 pgsize = mtd->writesize;
217
218 if (pgcnt && (pgcnt > mtd->erasesize / pgsize || pgcnt < 0)) {
95637c3a 219 pr_err("error: invalid pgcnt value %d\n", pgcnt);
4db451a7
AB
220 goto out_mtd;
221 }
222
223 err = -ENOMEM;
224 patt_5A5 = kmalloc(mtd->erasesize, GFP_KERNEL);
221b1bd3 225 if (!patt_5A5)
4db451a7 226 goto out_mtd;
4db451a7
AB
227
228 patt_A5A = kmalloc(mtd->erasesize, GFP_KERNEL);
221b1bd3 229 if (!patt_A5A)
4db451a7 230 goto out_patt_5A5;
4db451a7
AB
231
232 patt_FF = kmalloc(mtd->erasesize, GFP_KERNEL);
221b1bd3 233 if (!patt_FF)
4db451a7 234 goto out_patt_A5A;
4db451a7
AB
235
236 check_buf = kmalloc(mtd->erasesize, GFP_KERNEL);
221b1bd3 237 if (!check_buf)
4db451a7 238 goto out_patt_FF;
221b1bd3 239
6cf78358 240 bad_ebs = kzalloc(ebcnt, GFP_KERNEL);
221b1bd3
AC
241 if (!bad_ebs)
242 goto out_check_buf;
4db451a7
AB
243
244 err = 0;
245
246 /* Initialize patterns */
247 memset(patt_FF, 0xFF, mtd->erasesize);
248 for (i = 0; i < mtd->erasesize / pgsize; i++) {
249 if (!(i & 1)) {
250 memset(patt_5A5 + i * pgsize, 0x55, pgsize);
251 memset(patt_A5A + i * pgsize, 0xAA, pgsize);
252 } else {
253 memset(patt_5A5 + i * pgsize, 0xAA, pgsize);
254 memset(patt_A5A + i * pgsize, 0x55, pgsize);
255 }
256 }
257
6cf78358
AM
258 err = mtdtest_scan_for_bad_eraseblocks(mtd, bad_ebs, eb, ebcnt);
259 if (err)
260 goto out;
4db451a7
AB
261
262 start_timing();
263 while (1) {
264 int i;
265 void *patt;
266
6cf78358 267 mtdtest_erase_good_eraseblocks(mtd, bad_ebs, eb, ebcnt);
4db451a7
AB
268
269 /* Check if the eraseblocks contain only 0xFF bytes */
270 if (check) {
271 for (i = eb; i < eb + ebcnt; i++) {
272 if (bad_ebs[i - eb])
273 continue;
274 err = check_eraseblock(i, patt_FF);
275 if (err) {
95637c3a 276 pr_info("verify failed"
4db451a7
AB
277 " for 0xFF... pattern\n");
278 goto out;
279 }
280 cond_resched();
281 }
282 }
283
284 /* Write the pattern */
285 for (i = eb; i < eb + ebcnt; i++) {
286 if (bad_ebs[i - eb])
287 continue;
288 if ((eb + erase_cycles) & 1)
289 patt = patt_5A5;
290 else
291 patt = patt_A5A;
292 err = write_pattern(i, patt);
293 if (err)
294 goto out;
295 cond_resched();
296 }
297
298 /* Verify what we wrote */
299 if (check) {
300 for (i = eb; i < eb + ebcnt; i++) {
301 if (bad_ebs[i - eb])
302 continue;
303 if ((eb + erase_cycles) & 1)
304 patt = patt_5A5;
305 else
306 patt = patt_A5A;
307 err = check_eraseblock(i, patt);
308 if (err) {
95637c3a 309 pr_info("verify failed for %s"
4db451a7
AB
310 " pattern\n",
311 ((eb + erase_cycles) & 1) ?
312 "0x55AA55..." : "0xAA55AA...");
313 goto out;
314 }
315 cond_resched();
316 }
317 }
318
319 erase_cycles += 1;
320
321 if (erase_cycles % gran == 0) {
322 long ms;
323
324 stop_timing();
325 ms = (finish.tv_sec - start.tv_sec) * 1000 +
326 (finish.tv_usec - start.tv_usec) / 1000;
95637c3a 327 pr_info("%08u erase cycles done, took %lu "
4db451a7
AB
328 "milliseconds (%lu seconds)\n",
329 erase_cycles, ms, ms / 1000);
330 start_timing();
331 }
332
333 if (!infinite && --cycles_count == 0)
334 break;
335 }
336out:
337
95637c3a 338 pr_info("finished after %u erase cycles\n",
4db451a7 339 erase_cycles);
221b1bd3
AC
340 kfree(bad_ebs);
341out_check_buf:
4db451a7
AB
342 kfree(check_buf);
343out_patt_FF:
344 kfree(patt_FF);
345out_patt_A5A:
346 kfree(patt_A5A);
347out_patt_5A5:
348 kfree(patt_5A5);
349out_mtd:
350 put_mtd_device(mtd);
351 if (err)
95637c3a 352 pr_info("error %d occurred during torturing\n", err);
4db451a7
AB
353 printk(KERN_INFO "=================================================\n");
354 return err;
355}
356module_init(tort_init);
357
358static void __exit tort_exit(void)
359{
360 return;
361}
362module_exit(tort_exit);
363
364static int countdiffs(unsigned char *buf, unsigned char *check_buf,
365 unsigned offset, unsigned len, unsigned *bytesp,
366 unsigned *bitsp);
367static void print_bufs(unsigned char *read, unsigned char *written, int start,
368 int len);
369
370/*
371 * Report the detailed information about how the read EB differs from what was
372 * written.
373 */
374static void report_corrupt(unsigned char *read, unsigned char *written)
375{
376 int i;
377 int bytes, bits, pages, first;
378 int offset, len;
379 size_t check_len = mtd->erasesize;
380
381 if (pgcnt)
382 check_len = pgcnt * pgsize;
383
384 bytes = bits = pages = 0;
385 for (i = 0; i < check_len; i += pgsize)
386 if (countdiffs(written, read, i, pgsize, &bytes,
387 &bits) >= 0)
388 pages++;
389
95637c3a 390 pr_info("verify fails on %d pages, %d bytes/%d bits\n",
4db451a7 391 pages, bytes, bits);
95637c3a 392 pr_info("The following is a list of all differences between"
4db451a7
AB
393 " what was read from flash and what was expected\n");
394
395 for (i = 0; i < check_len; i += pgsize) {
396 cond_resched();
397 bytes = bits = 0;
398 first = countdiffs(written, read, i, pgsize, &bytes,
399 &bits);
400 if (first < 0)
401 continue;
402
403 printk("-------------------------------------------------------"
404 "----------------------------------\n");
405
95637c3a 406 pr_info("Page %zd has %d bytes/%d bits failing verify,"
4db451a7
AB
407 " starting at offset 0x%x\n",
408 (mtd->erasesize - check_len + i) / pgsize,
409 bytes, bits, first);
410
411 offset = first & ~0x7;
412 len = ((first + bytes) | 0x7) + 1 - offset;
413
414 print_bufs(read, written, offset, len);
415 }
416}
417
418static void print_bufs(unsigned char *read, unsigned char *written, int start,
419 int len)
420{
421 int i = 0, j1, j2;
422 char *diff;
423
424 printk("Offset Read Written\n");
425 while (i < len) {
426 printk("0x%08x: ", start + i);
427 diff = " ";
428 for (j1 = 0; j1 < 8 && i + j1 < len; j1++) {
429 printk(" %02x", read[start + i + j1]);
430 if (read[start + i + j1] != written[start + i + j1])
431 diff = "***";
432 }
433
434 while (j1 < 8) {
435 printk(" ");
436 j1 += 1;
437 }
438
439 printk(" %s ", diff);
440
441 for (j2 = 0; j2 < 8 && i + j2 < len; j2++)
442 printk(" %02x", written[start + i + j2]);
443 printk("\n");
444 i += 8;
445 }
446}
447
448/*
449 * Count the number of differing bytes and bits and return the first differing
450 * offset.
451 */
452static int countdiffs(unsigned char *buf, unsigned char *check_buf,
453 unsigned offset, unsigned len, unsigned *bytesp,
454 unsigned *bitsp)
455{
456 unsigned i, bit;
457 int first = -1;
458
459 for (i = offset; i < offset + len; i++)
460 if (buf[i] != check_buf[i]) {
461 first = i;
462 break;
463 }
464
465 while (i < offset + len) {
466 if (buf[i] != check_buf[i]) {
467 (*bytesp)++;
468 bit = 1;
469 while (bit < 256) {
470 if ((buf[i] & bit) != (check_buf[i] & bit))
471 (*bitsp)++;
472 bit <<= 1;
473 }
474 }
475 i++;
476 }
477
478 return first;
479}
480
481MODULE_DESCRIPTION("Eraseblock torturing module");
482MODULE_AUTHOR("Artem Bityutskiy, Jarkko Lavinen, Adrian Hunter");
483MODULE_LICENSE("GPL");