Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 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 | |
Vikram Narayanan | 95637c3 | 2012-10-10 23:11:37 +0530 | [diff] [blame] | 26 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 27 | |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 28 | #include <linux/init.h> |
Shraddha Barke | af30c0a | 2015-10-22 20:29:54 +0530 | [diff] [blame] | 29 | #include <linux/ktime.h> |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 30 | #include <linux/module.h> |
| 31 | #include <linux/moduleparam.h> |
| 32 | #include <linux/err.h> |
| 33 | #include <linux/mtd/mtd.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/slab.h> |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 35 | #include <linux/sched.h> |
Akinobu Mita | 6cf7835 | 2013-08-03 18:52:15 +0900 | [diff] [blame] | 36 | #include "mtd_test.h" |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 37 | |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 38 | #define RETRIES 3 |
| 39 | |
| 40 | static int eb = 8; |
| 41 | module_param(eb, int, S_IRUGO); |
| 42 | MODULE_PARM_DESC(eb, "eraseblock number within the selected MTD device"); |
| 43 | |
| 44 | static int ebcnt = 32; |
| 45 | module_param(ebcnt, int, S_IRUGO); |
| 46 | MODULE_PARM_DESC(ebcnt, "number of consecutive eraseblocks to torture"); |
| 47 | |
| 48 | static int pgcnt; |
| 49 | module_param(pgcnt, int, S_IRUGO); |
| 50 | MODULE_PARM_DESC(pgcnt, "number of pages per eraseblock to torture (0 => all)"); |
| 51 | |
Wolfram Sang | 7406060 | 2011-10-30 00:11:53 +0200 | [diff] [blame] | 52 | static int dev = -EINVAL; |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 53 | module_param(dev, int, S_IRUGO); |
| 54 | MODULE_PARM_DESC(dev, "MTD device number to use"); |
| 55 | |
| 56 | static int gran = 512; |
| 57 | module_param(gran, int, S_IRUGO); |
| 58 | MODULE_PARM_DESC(gran, "how often the status information should be printed"); |
| 59 | |
| 60 | static int check = 1; |
| 61 | module_param(check, int, S_IRUGO); |
| 62 | MODULE_PARM_DESC(check, "if the written data should be checked"); |
| 63 | |
| 64 | static unsigned int cycles_count; |
| 65 | module_param(cycles_count, uint, S_IRUGO); |
| 66 | MODULE_PARM_DESC(cycles_count, "how many erase cycles to do " |
| 67 | "(infinite by default)"); |
| 68 | |
| 69 | static struct mtd_info *mtd; |
| 70 | |
| 71 | /* This buffer contains 0x555555...0xAAAAAA... pattern */ |
| 72 | static unsigned char *patt_5A5; |
| 73 | /* This buffer contains 0xAAAAAA...0x555555... pattern */ |
| 74 | static unsigned char *patt_A5A; |
| 75 | /* This buffer contains all 0xFF bytes */ |
| 76 | static unsigned char *patt_FF; |
| 77 | /* This a temporary buffer is use when checking data */ |
| 78 | static unsigned char *check_buf; |
| 79 | /* How many erase cycles were done */ |
| 80 | static unsigned int erase_cycles; |
| 81 | |
| 82 | static int pgsize; |
Shraddha Barke | af30c0a | 2015-10-22 20:29:54 +0530 | [diff] [blame] | 83 | static ktime_t start, finish; |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 84 | |
| 85 | static void report_corrupt(unsigned char *read, unsigned char *written); |
| 86 | |
| 87 | static inline void start_timing(void) |
| 88 | { |
Shraddha Barke | af30c0a | 2015-10-22 20:29:54 +0530 | [diff] [blame] | 89 | start = ktime_get(); |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static inline void stop_timing(void) |
| 93 | { |
Shraddha Barke | af30c0a | 2015-10-22 20:29:54 +0530 | [diff] [blame] | 94 | finish = ktime_get(); |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /* |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 98 | * Check that the contents of eraseblock number @enbum is equivalent to the |
| 99 | * @buf buffer. |
| 100 | */ |
| 101 | static inline int check_eraseblock(int ebnum, unsigned char *buf) |
| 102 | { |
| 103 | int err, retries = 0; |
Artem Bityutskiy | 30fa984 | 2011-12-29 15:16:28 +0200 | [diff] [blame] | 104 | size_t read; |
Brian Norris | b9da8ba | 2015-02-28 02:02:26 -0800 | [diff] [blame] | 105 | loff_t addr = (loff_t)ebnum * mtd->erasesize; |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 106 | size_t len = mtd->erasesize; |
| 107 | |
| 108 | if (pgcnt) { |
Brian Norris | b9da8ba | 2015-02-28 02:02:26 -0800 | [diff] [blame] | 109 | addr = (loff_t)(ebnum + 1) * mtd->erasesize - pgcnt * pgsize; |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 110 | len = pgcnt * pgsize; |
| 111 | } |
| 112 | |
| 113 | retry: |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 114 | err = mtd_read(mtd, addr, len, &read, check_buf); |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 115 | if (mtd_is_bitflip(err)) |
Vikram Narayanan | 95637c3 | 2012-10-10 23:11:37 +0530 | [diff] [blame] | 116 | pr_err("single bit flip occurred at EB %d " |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 117 | "MTD reported that it was fixed.\n", ebnum); |
| 118 | else if (err) { |
Vikram Narayanan | 95637c3 | 2012-10-10 23:11:37 +0530 | [diff] [blame] | 119 | pr_err("error %d while reading EB %d, " |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 120 | "read %zd\n", err, ebnum, read); |
| 121 | return err; |
| 122 | } |
| 123 | |
| 124 | if (read != len) { |
Vikram Narayanan | 95637c3 | 2012-10-10 23:11:37 +0530 | [diff] [blame] | 125 | pr_err("failed to read %zd bytes from EB %d, " |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 126 | "read only %zd, but no error reported\n", |
| 127 | len, ebnum, read); |
| 128 | return -EIO; |
| 129 | } |
| 130 | |
| 131 | if (memcmp(buf, check_buf, len)) { |
Vikram Narayanan | 95637c3 | 2012-10-10 23:11:37 +0530 | [diff] [blame] | 132 | pr_err("read wrong data from EB %d\n", ebnum); |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 133 | report_corrupt(check_buf, buf); |
| 134 | |
| 135 | if (retries++ < RETRIES) { |
| 136 | /* Try read again */ |
| 137 | yield(); |
Vikram Narayanan | 95637c3 | 2012-10-10 23:11:37 +0530 | [diff] [blame] | 138 | pr_info("re-try reading data from EB %d\n", |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 139 | ebnum); |
| 140 | goto retry; |
| 141 | } else { |
Vikram Narayanan | 95637c3 | 2012-10-10 23:11:37 +0530 | [diff] [blame] | 142 | pr_info("retried %d times, still errors, " |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 143 | "give-up\n", RETRIES); |
| 144 | return -EINVAL; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | if (retries != 0) |
Vikram Narayanan | 95637c3 | 2012-10-10 23:11:37 +0530 | [diff] [blame] | 149 | pr_info("only attempt number %d was OK (!!!)\n", |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 150 | retries); |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static inline int write_pattern(int ebnum, void *buf) |
| 156 | { |
| 157 | int err; |
Artem Bityutskiy | 30fa984 | 2011-12-29 15:16:28 +0200 | [diff] [blame] | 158 | size_t written; |
Brian Norris | b9da8ba | 2015-02-28 02:02:26 -0800 | [diff] [blame] | 159 | loff_t addr = (loff_t)ebnum * mtd->erasesize; |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 160 | size_t len = mtd->erasesize; |
| 161 | |
| 162 | if (pgcnt) { |
Brian Norris | b9da8ba | 2015-02-28 02:02:26 -0800 | [diff] [blame] | 163 | addr = (loff_t)(ebnum + 1) * mtd->erasesize - pgcnt * pgsize; |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 164 | len = pgcnt * pgsize; |
| 165 | } |
Artem Bityutskiy | eda95cb | 2011-12-23 17:35:41 +0200 | [diff] [blame] | 166 | err = mtd_write(mtd, addr, len, &written, buf); |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 167 | if (err) { |
Vikram Narayanan | 95637c3 | 2012-10-10 23:11:37 +0530 | [diff] [blame] | 168 | pr_err("error %d while writing EB %d, written %zd" |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 169 | " bytes\n", err, ebnum, written); |
| 170 | return err; |
| 171 | } |
| 172 | if (written != len) { |
Vikram Narayanan | 95637c3 | 2012-10-10 23:11:37 +0530 | [diff] [blame] | 173 | pr_info("written only %zd bytes of %zd, but no error" |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 174 | " reported\n", written, len); |
| 175 | return -EIO; |
| 176 | } |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | static int __init tort_init(void) |
| 182 | { |
| 183 | int err = 0, i, infinite = !cycles_count; |
Akinobu Mita | 6cf7835 | 2013-08-03 18:52:15 +0900 | [diff] [blame] | 184 | unsigned char *bad_ebs; |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 185 | |
| 186 | printk(KERN_INFO "\n"); |
| 187 | printk(KERN_INFO "=================================================\n"); |
Vikram Narayanan | 95637c3 | 2012-10-10 23:11:37 +0530 | [diff] [blame] | 188 | pr_info("Warning: this program is trying to wear out your " |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 189 | "flash, stop it if this is not wanted.\n"); |
Wolfram Sang | 7406060 | 2011-10-30 00:11:53 +0200 | [diff] [blame] | 190 | |
| 191 | if (dev < 0) { |
Masanari Iida | 064a769 | 2012-11-09 23:20:58 +0900 | [diff] [blame] | 192 | pr_info("Please specify a valid mtd-device via module parameter\n"); |
Vikram Narayanan | 95637c3 | 2012-10-10 23:11:37 +0530 | [diff] [blame] | 193 | pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n"); |
Wolfram Sang | 7406060 | 2011-10-30 00:11:53 +0200 | [diff] [blame] | 194 | return -EINVAL; |
| 195 | } |
| 196 | |
Vikram Narayanan | 95637c3 | 2012-10-10 23:11:37 +0530 | [diff] [blame] | 197 | pr_info("MTD device: %d\n", dev); |
| 198 | pr_info("torture %d eraseblocks (%d-%d) of mtd%d\n", |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 199 | ebcnt, eb, eb + ebcnt - 1, dev); |
| 200 | if (pgcnt) |
Vikram Narayanan | 95637c3 | 2012-10-10 23:11:37 +0530 | [diff] [blame] | 201 | pr_info("torturing just %d pages per eraseblock\n", |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 202 | pgcnt); |
Vikram Narayanan | 95637c3 | 2012-10-10 23:11:37 +0530 | [diff] [blame] | 203 | pr_info("write verify %s\n", check ? "enabled" : "disabled"); |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 204 | |
| 205 | mtd = get_mtd_device(NULL, dev); |
| 206 | if (IS_ERR(mtd)) { |
| 207 | err = PTR_ERR(mtd); |
Vikram Narayanan | 95637c3 | 2012-10-10 23:11:37 +0530 | [diff] [blame] | 208 | pr_err("error: cannot get MTD device\n"); |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 209 | return err; |
| 210 | } |
| 211 | |
| 212 | if (mtd->writesize == 1) { |
Vikram Narayanan | 95637c3 | 2012-10-10 23:11:37 +0530 | [diff] [blame] | 213 | pr_info("not NAND flash, assume page size is 512 " |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 214 | "bytes.\n"); |
| 215 | pgsize = 512; |
| 216 | } else |
| 217 | pgsize = mtd->writesize; |
| 218 | |
| 219 | if (pgcnt && (pgcnt > mtd->erasesize / pgsize || pgcnt < 0)) { |
Vikram Narayanan | 95637c3 | 2012-10-10 23:11:37 +0530 | [diff] [blame] | 220 | pr_err("error: invalid pgcnt value %d\n", pgcnt); |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 221 | goto out_mtd; |
| 222 | } |
| 223 | |
| 224 | err = -ENOMEM; |
| 225 | patt_5A5 = kmalloc(mtd->erasesize, GFP_KERNEL); |
Al Cooper | 221b1bd | 2013-02-05 09:08:10 -0500 | [diff] [blame] | 226 | if (!patt_5A5) |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 227 | goto out_mtd; |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 228 | |
| 229 | patt_A5A = kmalloc(mtd->erasesize, GFP_KERNEL); |
Al Cooper | 221b1bd | 2013-02-05 09:08:10 -0500 | [diff] [blame] | 230 | if (!patt_A5A) |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 231 | goto out_patt_5A5; |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 232 | |
| 233 | patt_FF = kmalloc(mtd->erasesize, GFP_KERNEL); |
Al Cooper | 221b1bd | 2013-02-05 09:08:10 -0500 | [diff] [blame] | 234 | if (!patt_FF) |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 235 | goto out_patt_A5A; |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 236 | |
| 237 | check_buf = kmalloc(mtd->erasesize, GFP_KERNEL); |
Al Cooper | 221b1bd | 2013-02-05 09:08:10 -0500 | [diff] [blame] | 238 | if (!check_buf) |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 239 | goto out_patt_FF; |
Al Cooper | 221b1bd | 2013-02-05 09:08:10 -0500 | [diff] [blame] | 240 | |
Akinobu Mita | 6cf7835 | 2013-08-03 18:52:15 +0900 | [diff] [blame] | 241 | bad_ebs = kzalloc(ebcnt, GFP_KERNEL); |
Al Cooper | 221b1bd | 2013-02-05 09:08:10 -0500 | [diff] [blame] | 242 | if (!bad_ebs) |
| 243 | goto out_check_buf; |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 244 | |
| 245 | err = 0; |
| 246 | |
| 247 | /* Initialize patterns */ |
| 248 | memset(patt_FF, 0xFF, mtd->erasesize); |
| 249 | for (i = 0; i < mtd->erasesize / pgsize; i++) { |
| 250 | if (!(i & 1)) { |
| 251 | memset(patt_5A5 + i * pgsize, 0x55, pgsize); |
| 252 | memset(patt_A5A + i * pgsize, 0xAA, pgsize); |
| 253 | } else { |
| 254 | memset(patt_5A5 + i * pgsize, 0xAA, pgsize); |
| 255 | memset(patt_A5A + i * pgsize, 0x55, pgsize); |
| 256 | } |
| 257 | } |
| 258 | |
Akinobu Mita | 6cf7835 | 2013-08-03 18:52:15 +0900 | [diff] [blame] | 259 | err = mtdtest_scan_for_bad_eraseblocks(mtd, bad_ebs, eb, ebcnt); |
| 260 | if (err) |
| 261 | goto out; |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 262 | |
| 263 | start_timing(); |
| 264 | while (1) { |
| 265 | int i; |
| 266 | void *patt; |
| 267 | |
Brian Norris | 68f2981 | 2014-11-21 10:24:29 -0800 | [diff] [blame] | 268 | err = mtdtest_erase_good_eraseblocks(mtd, bad_ebs, eb, ebcnt); |
| 269 | if (err) |
| 270 | goto out; |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 271 | |
| 272 | /* Check if the eraseblocks contain only 0xFF bytes */ |
| 273 | if (check) { |
| 274 | for (i = eb; i < eb + ebcnt; i++) { |
| 275 | if (bad_ebs[i - eb]) |
| 276 | continue; |
| 277 | err = check_eraseblock(i, patt_FF); |
| 278 | if (err) { |
Vikram Narayanan | 95637c3 | 2012-10-10 23:11:37 +0530 | [diff] [blame] | 279 | pr_info("verify failed" |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 280 | " for 0xFF... pattern\n"); |
| 281 | goto out; |
| 282 | } |
Richard Weinberger | 2a6a28e7 | 2015-03-29 21:52:06 +0200 | [diff] [blame] | 283 | |
| 284 | err = mtdtest_relax(); |
| 285 | if (err) |
| 286 | goto out; |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
| 290 | /* Write the pattern */ |
| 291 | for (i = eb; i < eb + ebcnt; i++) { |
| 292 | if (bad_ebs[i - eb]) |
| 293 | continue; |
| 294 | if ((eb + erase_cycles) & 1) |
| 295 | patt = patt_5A5; |
| 296 | else |
| 297 | patt = patt_A5A; |
| 298 | err = write_pattern(i, patt); |
| 299 | if (err) |
| 300 | goto out; |
Richard Weinberger | 2a6a28e7 | 2015-03-29 21:52:06 +0200 | [diff] [blame] | 301 | |
| 302 | err = mtdtest_relax(); |
| 303 | if (err) |
| 304 | goto out; |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | /* Verify what we wrote */ |
| 308 | if (check) { |
| 309 | for (i = eb; i < eb + ebcnt; i++) { |
| 310 | if (bad_ebs[i - eb]) |
| 311 | continue; |
| 312 | if ((eb + erase_cycles) & 1) |
| 313 | patt = patt_5A5; |
| 314 | else |
| 315 | patt = patt_A5A; |
| 316 | err = check_eraseblock(i, patt); |
| 317 | if (err) { |
Vikram Narayanan | 95637c3 | 2012-10-10 23:11:37 +0530 | [diff] [blame] | 318 | pr_info("verify failed for %s" |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 319 | " pattern\n", |
| 320 | ((eb + erase_cycles) & 1) ? |
| 321 | "0x55AA55..." : "0xAA55AA..."); |
| 322 | goto out; |
| 323 | } |
Richard Weinberger | 2a6a28e7 | 2015-03-29 21:52:06 +0200 | [diff] [blame] | 324 | |
| 325 | err = mtdtest_relax(); |
| 326 | if (err) |
| 327 | goto out; |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
| 331 | erase_cycles += 1; |
| 332 | |
| 333 | if (erase_cycles % gran == 0) { |
| 334 | long ms; |
| 335 | |
| 336 | stop_timing(); |
Shraddha Barke | af30c0a | 2015-10-22 20:29:54 +0530 | [diff] [blame] | 337 | ms = ktime_ms_delta(finish, start); |
Vikram Narayanan | 95637c3 | 2012-10-10 23:11:37 +0530 | [diff] [blame] | 338 | pr_info("%08u erase cycles done, took %lu " |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 339 | "milliseconds (%lu seconds)\n", |
| 340 | erase_cycles, ms, ms / 1000); |
| 341 | start_timing(); |
| 342 | } |
| 343 | |
| 344 | if (!infinite && --cycles_count == 0) |
| 345 | break; |
| 346 | } |
| 347 | out: |
| 348 | |
Vikram Narayanan | 95637c3 | 2012-10-10 23:11:37 +0530 | [diff] [blame] | 349 | pr_info("finished after %u erase cycles\n", |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 350 | erase_cycles); |
Al Cooper | 221b1bd | 2013-02-05 09:08:10 -0500 | [diff] [blame] | 351 | kfree(bad_ebs); |
| 352 | out_check_buf: |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 353 | kfree(check_buf); |
| 354 | out_patt_FF: |
| 355 | kfree(patt_FF); |
| 356 | out_patt_A5A: |
| 357 | kfree(patt_A5A); |
| 358 | out_patt_5A5: |
| 359 | kfree(patt_5A5); |
| 360 | out_mtd: |
| 361 | put_mtd_device(mtd); |
| 362 | if (err) |
Vikram Narayanan | 95637c3 | 2012-10-10 23:11:37 +0530 | [diff] [blame] | 363 | pr_info("error %d occurred during torturing\n", err); |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 364 | printk(KERN_INFO "=================================================\n"); |
| 365 | return err; |
| 366 | } |
| 367 | module_init(tort_init); |
| 368 | |
| 369 | static void __exit tort_exit(void) |
| 370 | { |
| 371 | return; |
| 372 | } |
| 373 | module_exit(tort_exit); |
| 374 | |
| 375 | static int countdiffs(unsigned char *buf, unsigned char *check_buf, |
| 376 | unsigned offset, unsigned len, unsigned *bytesp, |
| 377 | unsigned *bitsp); |
| 378 | static 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 | */ |
| 385 | static 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 Narayanan | 95637c3 | 2012-10-10 23:11:37 +0530 | [diff] [blame] | 401 | pr_info("verify fails on %d pages, %d bytes/%d bits\n", |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 402 | pages, bytes, bits); |
Vikram Narayanan | 95637c3 | 2012-10-10 23:11:37 +0530 | [diff] [blame] | 403 | pr_info("The following is a list of all differences between" |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 404 | " 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 Narayanan | 95637c3 | 2012-10-10 23:11:37 +0530 | [diff] [blame] | 417 | pr_info("Page %zd has %d bytes/%d bits failing verify," |
Artem Bityutskiy | 4db451a | 2008-12-08 13:38:34 +0200 | [diff] [blame] | 418 | " 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 | |
| 429 | static 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 | */ |
| 463 | static 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 | |
| 492 | MODULE_DESCRIPTION("Eraseblock torturing module"); |
| 493 | MODULE_AUTHOR("Artem Bityutskiy, Jarkko Lavinen, Adrian Hunter"); |
| 494 | MODULE_LICENSE("GPL"); |