blob: e5d6e6d9532fa5f12961ae9aae72d0766e296370 [file] [log] [blame]
Artem Bityutskiy4db451a2008-12-08 13:38:34 +02001/*
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
Vikram Narayanan95637c32012-10-10 23:11:37 +053026#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
Artem Bityutskiy4db451a2008-12-08 13:38:34 +020028#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Artem Bityutskiy4db451a2008-12-08 13:38:34 +020034#include <linux/sched.h>
Akinobu Mita6cf78352013-08-03 18:52:15 +090035#include "mtd_test.h"
Artem Bityutskiy4db451a2008-12-08 13:38:34 +020036
Artem Bityutskiy4db451a2008-12-08 13:38:34 +020037#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
Wolfram Sang74060602011-10-30 00:11:53 +020051static int dev = -EINVAL;
Artem Bityutskiy4db451a2008-12-08 13:38:34 +020052module_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
96/*
Artem Bityutskiy4db451a2008-12-08 13:38:34 +020097 * 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;
Artem Bityutskiy30fa9842011-12-29 15:16:28 +0200103 size_t read;
Brian Norrisb9da8ba2015-02-28 02:02:26 -0800104 loff_t addr = (loff_t)ebnum * mtd->erasesize;
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200105 size_t len = mtd->erasesize;
106
107 if (pgcnt) {
Brian Norrisb9da8ba2015-02-28 02:02:26 -0800108 addr = (loff_t)(ebnum + 1) * mtd->erasesize - pgcnt * pgsize;
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200109 len = pgcnt * pgsize;
110 }
111
112retry:
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200113 err = mtd_read(mtd, addr, len, &read, check_buf);
Brian Norrisd57f40542011-09-20 18:34:25 -0700114 if (mtd_is_bitflip(err))
Vikram Narayanan95637c32012-10-10 23:11:37 +0530115 pr_err("single bit flip occurred at EB %d "
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200116 "MTD reported that it was fixed.\n", ebnum);
117 else if (err) {
Vikram Narayanan95637c32012-10-10 23:11:37 +0530118 pr_err("error %d while reading EB %d, "
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200119 "read %zd\n", err, ebnum, read);
120 return err;
121 }
122
123 if (read != len) {
Vikram Narayanan95637c32012-10-10 23:11:37 +0530124 pr_err("failed to read %zd bytes from EB %d, "
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200125 "read only %zd, but no error reported\n",
126 len, ebnum, read);
127 return -EIO;
128 }
129
130 if (memcmp(buf, check_buf, len)) {
Vikram Narayanan95637c32012-10-10 23:11:37 +0530131 pr_err("read wrong data from EB %d\n", ebnum);
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200132 report_corrupt(check_buf, buf);
133
134 if (retries++ < RETRIES) {
135 /* Try read again */
136 yield();
Vikram Narayanan95637c32012-10-10 23:11:37 +0530137 pr_info("re-try reading data from EB %d\n",
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200138 ebnum);
139 goto retry;
140 } else {
Vikram Narayanan95637c32012-10-10 23:11:37 +0530141 pr_info("retried %d times, still errors, "
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200142 "give-up\n", RETRIES);
143 return -EINVAL;
144 }
145 }
146
147 if (retries != 0)
Vikram Narayanan95637c32012-10-10 23:11:37 +0530148 pr_info("only attempt number %d was OK (!!!)\n",
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200149 retries);
150
151 return 0;
152}
153
154static inline int write_pattern(int ebnum, void *buf)
155{
156 int err;
Artem Bityutskiy30fa9842011-12-29 15:16:28 +0200157 size_t written;
Brian Norrisb9da8ba2015-02-28 02:02:26 -0800158 loff_t addr = (loff_t)ebnum * mtd->erasesize;
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200159 size_t len = mtd->erasesize;
160
161 if (pgcnt) {
Brian Norrisb9da8ba2015-02-28 02:02:26 -0800162 addr = (loff_t)(ebnum + 1) * mtd->erasesize - pgcnt * pgsize;
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200163 len = pgcnt * pgsize;
164 }
Artem Bityutskiyeda95cb2011-12-23 17:35:41 +0200165 err = mtd_write(mtd, addr, len, &written, buf);
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200166 if (err) {
Vikram Narayanan95637c32012-10-10 23:11:37 +0530167 pr_err("error %d while writing EB %d, written %zd"
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200168 " bytes\n", err, ebnum, written);
169 return err;
170 }
171 if (written != len) {
Vikram Narayanan95637c32012-10-10 23:11:37 +0530172 pr_info("written only %zd bytes of %zd, but no error"
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200173 " 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;
Akinobu Mita6cf78352013-08-03 18:52:15 +0900183 unsigned char *bad_ebs;
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200184
185 printk(KERN_INFO "\n");
186 printk(KERN_INFO "=================================================\n");
Vikram Narayanan95637c32012-10-10 23:11:37 +0530187 pr_info("Warning: this program is trying to wear out your "
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200188 "flash, stop it if this is not wanted.\n");
Wolfram Sang74060602011-10-30 00:11:53 +0200189
190 if (dev < 0) {
Masanari Iida064a7692012-11-09 23:20:58 +0900191 pr_info("Please specify a valid mtd-device via module parameter\n");
Vikram Narayanan95637c32012-10-10 23:11:37 +0530192 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
Wolfram Sang74060602011-10-30 00:11:53 +0200193 return -EINVAL;
194 }
195
Vikram Narayanan95637c32012-10-10 23:11:37 +0530196 pr_info("MTD device: %d\n", dev);
197 pr_info("torture %d eraseblocks (%d-%d) of mtd%d\n",
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200198 ebcnt, eb, eb + ebcnt - 1, dev);
199 if (pgcnt)
Vikram Narayanan95637c32012-10-10 23:11:37 +0530200 pr_info("torturing just %d pages per eraseblock\n",
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200201 pgcnt);
Vikram Narayanan95637c32012-10-10 23:11:37 +0530202 pr_info("write verify %s\n", check ? "enabled" : "disabled");
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200203
204 mtd = get_mtd_device(NULL, dev);
205 if (IS_ERR(mtd)) {
206 err = PTR_ERR(mtd);
Vikram Narayanan95637c32012-10-10 23:11:37 +0530207 pr_err("error: cannot get MTD device\n");
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200208 return err;
209 }
210
211 if (mtd->writesize == 1) {
Vikram Narayanan95637c32012-10-10 23:11:37 +0530212 pr_info("not NAND flash, assume page size is 512 "
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200213 "bytes.\n");
214 pgsize = 512;
215 } else
216 pgsize = mtd->writesize;
217
218 if (pgcnt && (pgcnt > mtd->erasesize / pgsize || pgcnt < 0)) {
Vikram Narayanan95637c32012-10-10 23:11:37 +0530219 pr_err("error: invalid pgcnt value %d\n", pgcnt);
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200220 goto out_mtd;
221 }
222
223 err = -ENOMEM;
224 patt_5A5 = kmalloc(mtd->erasesize, GFP_KERNEL);
Al Cooper221b1bd2013-02-05 09:08:10 -0500225 if (!patt_5A5)
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200226 goto out_mtd;
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200227
228 patt_A5A = kmalloc(mtd->erasesize, GFP_KERNEL);
Al Cooper221b1bd2013-02-05 09:08:10 -0500229 if (!patt_A5A)
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200230 goto out_patt_5A5;
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200231
232 patt_FF = kmalloc(mtd->erasesize, GFP_KERNEL);
Al Cooper221b1bd2013-02-05 09:08:10 -0500233 if (!patt_FF)
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200234 goto out_patt_A5A;
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200235
236 check_buf = kmalloc(mtd->erasesize, GFP_KERNEL);
Al Cooper221b1bd2013-02-05 09:08:10 -0500237 if (!check_buf)
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200238 goto out_patt_FF;
Al Cooper221b1bd2013-02-05 09:08:10 -0500239
Akinobu Mita6cf78352013-08-03 18:52:15 +0900240 bad_ebs = kzalloc(ebcnt, GFP_KERNEL);
Al Cooper221b1bd2013-02-05 09:08:10 -0500241 if (!bad_ebs)
242 goto out_check_buf;
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200243
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
Akinobu Mita6cf78352013-08-03 18:52:15 +0900258 err = mtdtest_scan_for_bad_eraseblocks(mtd, bad_ebs, eb, ebcnt);
259 if (err)
260 goto out;
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200261
262 start_timing();
263 while (1) {
264 int i;
265 void *patt;
266
Brian Norris68f29812014-11-21 10:24:29 -0800267 err = mtdtest_erase_good_eraseblocks(mtd, bad_ebs, eb, ebcnt);
268 if (err)
269 goto out;
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200270
271 /* Check if the eraseblocks contain only 0xFF bytes */
272 if (check) {
273 for (i = eb; i < eb + ebcnt; i++) {
274 if (bad_ebs[i - eb])
275 continue;
276 err = check_eraseblock(i, patt_FF);
277 if (err) {
Vikram Narayanan95637c32012-10-10 23:11:37 +0530278 pr_info("verify failed"
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200279 " for 0xFF... pattern\n");
280 goto out;
281 }
Richard Weinberger2a6a28e72015-03-29 21:52:06 +0200282
283 err = mtdtest_relax();
284 if (err)
285 goto out;
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200286 }
287 }
288
289 /* Write the pattern */
290 for (i = eb; i < eb + ebcnt; i++) {
291 if (bad_ebs[i - eb])
292 continue;
293 if ((eb + erase_cycles) & 1)
294 patt = patt_5A5;
295 else
296 patt = patt_A5A;
297 err = write_pattern(i, patt);
298 if (err)
299 goto out;
Richard Weinberger2a6a28e72015-03-29 21:52:06 +0200300
301 err = mtdtest_relax();
302 if (err)
303 goto out;
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200304 }
305
306 /* Verify what we wrote */
307 if (check) {
308 for (i = eb; i < eb + ebcnt; i++) {
309 if (bad_ebs[i - eb])
310 continue;
311 if ((eb + erase_cycles) & 1)
312 patt = patt_5A5;
313 else
314 patt = patt_A5A;
315 err = check_eraseblock(i, patt);
316 if (err) {
Vikram Narayanan95637c32012-10-10 23:11:37 +0530317 pr_info("verify failed for %s"
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200318 " pattern\n",
319 ((eb + erase_cycles) & 1) ?
320 "0x55AA55..." : "0xAA55AA...");
321 goto out;
322 }
Richard Weinberger2a6a28e72015-03-29 21:52:06 +0200323
324 err = mtdtest_relax();
325 if (err)
326 goto out;
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200327 }
328 }
329
330 erase_cycles += 1;
331
332 if (erase_cycles % gran == 0) {
333 long ms;
334
335 stop_timing();
336 ms = (finish.tv_sec - start.tv_sec) * 1000 +
337 (finish.tv_usec - start.tv_usec) / 1000;
Vikram Narayanan95637c32012-10-10 23:11:37 +0530338 pr_info("%08u erase cycles done, took %lu "
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200339 "milliseconds (%lu seconds)\n",
340 erase_cycles, ms, ms / 1000);
341 start_timing();
342 }
343
344 if (!infinite && --cycles_count == 0)
345 break;
346 }
347out:
348
Vikram Narayanan95637c32012-10-10 23:11:37 +0530349 pr_info("finished after %u erase cycles\n",
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200350 erase_cycles);
Al Cooper221b1bd2013-02-05 09:08:10 -0500351 kfree(bad_ebs);
352out_check_buf:
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200353 kfree(check_buf);
354out_patt_FF:
355 kfree(patt_FF);
356out_patt_A5A:
357 kfree(patt_A5A);
358out_patt_5A5:
359 kfree(patt_5A5);
360out_mtd:
361 put_mtd_device(mtd);
362 if (err)
Vikram Narayanan95637c32012-10-10 23:11:37 +0530363 pr_info("error %d occurred during torturing\n", err);
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200364 printk(KERN_INFO "=================================================\n");
365 return err;
366}
367module_init(tort_init);
368
369static void __exit tort_exit(void)
370{
371 return;
372}
373module_exit(tort_exit);
374
375static int countdiffs(unsigned char *buf, unsigned char *check_buf,
376 unsigned offset, unsigned len, unsigned *bytesp,
377 unsigned *bitsp);
378static void print_bufs(unsigned char *read, unsigned char *written, int start,
379 int len);
380
381/*
382 * Report the detailed information about how the read EB differs from what was
383 * written.
384 */
385static void report_corrupt(unsigned char *read, unsigned char *written)
386{
387 int i;
388 int bytes, bits, pages, first;
389 int offset, len;
390 size_t check_len = mtd->erasesize;
391
392 if (pgcnt)
393 check_len = pgcnt * pgsize;
394
395 bytes = bits = pages = 0;
396 for (i = 0; i < check_len; i += pgsize)
397 if (countdiffs(written, read, i, pgsize, &bytes,
398 &bits) >= 0)
399 pages++;
400
Vikram Narayanan95637c32012-10-10 23:11:37 +0530401 pr_info("verify fails on %d pages, %d bytes/%d bits\n",
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200402 pages, bytes, bits);
Vikram Narayanan95637c32012-10-10 23:11:37 +0530403 pr_info("The following is a list of all differences between"
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200404 " what was read from flash and what was expected\n");
405
406 for (i = 0; i < check_len; i += pgsize) {
407 cond_resched();
408 bytes = bits = 0;
409 first = countdiffs(written, read, i, pgsize, &bytes,
410 &bits);
411 if (first < 0)
412 continue;
413
414 printk("-------------------------------------------------------"
415 "----------------------------------\n");
416
Vikram Narayanan95637c32012-10-10 23:11:37 +0530417 pr_info("Page %zd has %d bytes/%d bits failing verify,"
Artem Bityutskiy4db451a2008-12-08 13:38:34 +0200418 " starting at offset 0x%x\n",
419 (mtd->erasesize - check_len + i) / pgsize,
420 bytes, bits, first);
421
422 offset = first & ~0x7;
423 len = ((first + bytes) | 0x7) + 1 - offset;
424
425 print_bufs(read, written, offset, len);
426 }
427}
428
429static void print_bufs(unsigned char *read, unsigned char *written, int start,
430 int len)
431{
432 int i = 0, j1, j2;
433 char *diff;
434
435 printk("Offset Read Written\n");
436 while (i < len) {
437 printk("0x%08x: ", start + i);
438 diff = " ";
439 for (j1 = 0; j1 < 8 && i + j1 < len; j1++) {
440 printk(" %02x", read[start + i + j1]);
441 if (read[start + i + j1] != written[start + i + j1])
442 diff = "***";
443 }
444
445 while (j1 < 8) {
446 printk(" ");
447 j1 += 1;
448 }
449
450 printk(" %s ", diff);
451
452 for (j2 = 0; j2 < 8 && i + j2 < len; j2++)
453 printk(" %02x", written[start + i + j2]);
454 printk("\n");
455 i += 8;
456 }
457}
458
459/*
460 * Count the number of differing bytes and bits and return the first differing
461 * offset.
462 */
463static int countdiffs(unsigned char *buf, unsigned char *check_buf,
464 unsigned offset, unsigned len, unsigned *bytesp,
465 unsigned *bitsp)
466{
467 unsigned i, bit;
468 int first = -1;
469
470 for (i = offset; i < offset + len; i++)
471 if (buf[i] != check_buf[i]) {
472 first = i;
473 break;
474 }
475
476 while (i < offset + len) {
477 if (buf[i] != check_buf[i]) {
478 (*bytesp)++;
479 bit = 1;
480 while (bit < 256) {
481 if ((buf[i] & bit) != (check_buf[i] & bit))
482 (*bitsp)++;
483 bit <<= 1;
484 }
485 }
486 i++;
487 }
488
489 return first;
490}
491
492MODULE_DESCRIPTION("Eraseblock torturing module");
493MODULE_AUTHOR("Artem Bityutskiy, Jarkko Lavinen, Adrian Hunter");
494MODULE_LICENSE("GPL");