Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006-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 sub-page read and write on MTD device. |
| 18 | * Author: Adrian Hunter <ext-adrian.hunter@nokia.com> |
| 19 | * |
| 20 | */ |
| 21 | |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 22 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 23 | |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 30 | #include <linux/sched.h> |
Akinobu Mita | a312b78 | 2013-02-27 17:05:39 -0800 | [diff] [blame] | 31 | #include <linux/random.h> |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 32 | |
Akinobu Mita | 725cd71 | 2013-08-03 18:52:14 +0900 | [diff] [blame] | 33 | #include "mtd_test.h" |
| 34 | |
Wolfram Sang | 7406060 | 2011-10-30 00:11:53 +0200 | [diff] [blame] | 35 | static int dev = -EINVAL; |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 36 | module_param(dev, int, S_IRUGO); |
| 37 | MODULE_PARM_DESC(dev, "MTD device number to use"); |
| 38 | |
| 39 | static struct mtd_info *mtd; |
| 40 | static unsigned char *writebuf; |
| 41 | static unsigned char *readbuf; |
| 42 | static unsigned char *bbt; |
| 43 | |
| 44 | static int subpgsize; |
| 45 | static int bufsize; |
| 46 | static int ebcnt; |
| 47 | static int pgcnt; |
| 48 | static int errcnt; |
Akinobu Mita | a312b78 | 2013-02-27 17:05:39 -0800 | [diff] [blame] | 49 | static struct rnd_state rnd_state; |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 50 | |
| 51 | static inline void clear_data(unsigned char *buf, size_t len) |
| 52 | { |
| 53 | memset(buf, 0, len); |
| 54 | } |
| 55 | |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 56 | static int write_eraseblock(int ebnum) |
| 57 | { |
Artem Bityutskiy | 30fa984 | 2011-12-29 15:16:28 +0200 | [diff] [blame] | 58 | size_t written; |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 59 | int err = 0; |
Brian Norris | 1001ff7 | 2014-07-21 19:07:12 -0700 | [diff] [blame] | 60 | loff_t addr = (loff_t)ebnum * mtd->erasesize; |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 61 | |
Akinobu Mita | a312b78 | 2013-02-27 17:05:39 -0800 | [diff] [blame] | 62 | prandom_bytes_state(&rnd_state, writebuf, subpgsize); |
Artem Bityutskiy | eda95cb | 2011-12-23 17:35:41 +0200 | [diff] [blame] | 63 | err = mtd_write(mtd, addr, subpgsize, &written, writebuf); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 64 | if (unlikely(err || written != subpgsize)) { |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 65 | pr_err("error: write failed at %#llx\n", |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 66 | (long long)addr); |
| 67 | if (written != subpgsize) { |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 68 | pr_err(" write size: %#x\n", subpgsize); |
| 69 | pr_err(" written: %#zx\n", written); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 70 | } |
| 71 | return err ? err : -1; |
| 72 | } |
| 73 | |
| 74 | addr += subpgsize; |
| 75 | |
Akinobu Mita | a312b78 | 2013-02-27 17:05:39 -0800 | [diff] [blame] | 76 | prandom_bytes_state(&rnd_state, writebuf, subpgsize); |
Artem Bityutskiy | eda95cb | 2011-12-23 17:35:41 +0200 | [diff] [blame] | 77 | err = mtd_write(mtd, addr, subpgsize, &written, writebuf); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 78 | if (unlikely(err || written != subpgsize)) { |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 79 | pr_err("error: write failed at %#llx\n", |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 80 | (long long)addr); |
| 81 | if (written != subpgsize) { |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 82 | pr_err(" write size: %#x\n", subpgsize); |
| 83 | pr_err(" written: %#zx\n", written); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 84 | } |
| 85 | return err ? err : -1; |
| 86 | } |
| 87 | |
| 88 | return err; |
| 89 | } |
| 90 | |
| 91 | static int write_eraseblock2(int ebnum) |
| 92 | { |
Artem Bityutskiy | 30fa984 | 2011-12-29 15:16:28 +0200 | [diff] [blame] | 93 | size_t written; |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 94 | int err = 0, k; |
Brian Norris | 1001ff7 | 2014-07-21 19:07:12 -0700 | [diff] [blame] | 95 | loff_t addr = (loff_t)ebnum * mtd->erasesize; |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 96 | |
| 97 | for (k = 1; k < 33; ++k) { |
| 98 | if (addr + (subpgsize * k) > (ebnum + 1) * mtd->erasesize) |
| 99 | break; |
Akinobu Mita | a312b78 | 2013-02-27 17:05:39 -0800 | [diff] [blame] | 100 | prandom_bytes_state(&rnd_state, writebuf, subpgsize * k); |
Artem Bityutskiy | eda95cb | 2011-12-23 17:35:41 +0200 | [diff] [blame] | 101 | err = mtd_write(mtd, addr, subpgsize * k, &written, writebuf); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 102 | if (unlikely(err || written != subpgsize * k)) { |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 103 | pr_err("error: write failed at %#llx\n", |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 104 | (long long)addr); |
| 105 | if (written != subpgsize) { |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 106 | pr_err(" write size: %#x\n", |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 107 | subpgsize * k); |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 108 | pr_err(" written: %#08zx\n", |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 109 | written); |
| 110 | } |
| 111 | return err ? err : -1; |
| 112 | } |
| 113 | addr += subpgsize * k; |
| 114 | } |
| 115 | |
| 116 | return err; |
| 117 | } |
| 118 | |
| 119 | static void print_subpage(unsigned char *p) |
| 120 | { |
| 121 | int i, j; |
| 122 | |
| 123 | for (i = 0; i < subpgsize; ) { |
| 124 | for (j = 0; i < subpgsize && j < 32; ++i, ++j) |
| 125 | printk("%02x", *p++); |
| 126 | printk("\n"); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | static int verify_eraseblock(int ebnum) |
| 131 | { |
Artem Bityutskiy | 30fa984 | 2011-12-29 15:16:28 +0200 | [diff] [blame] | 132 | size_t read; |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 133 | int err = 0; |
Brian Norris | 1001ff7 | 2014-07-21 19:07:12 -0700 | [diff] [blame] | 134 | loff_t addr = (loff_t)ebnum * mtd->erasesize; |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 135 | |
Akinobu Mita | a312b78 | 2013-02-27 17:05:39 -0800 | [diff] [blame] | 136 | prandom_bytes_state(&rnd_state, writebuf, subpgsize); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 137 | clear_data(readbuf, subpgsize); |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 138 | err = mtd_read(mtd, addr, subpgsize, &read, readbuf); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 139 | if (unlikely(err || read != subpgsize)) { |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 140 | if (mtd_is_bitflip(err) && read == subpgsize) { |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 141 | pr_info("ECC correction at %#llx\n", |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 142 | (long long)addr); |
| 143 | err = 0; |
| 144 | } else { |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 145 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 146 | (long long)addr); |
| 147 | return err ? err : -1; |
| 148 | } |
| 149 | } |
| 150 | if (unlikely(memcmp(readbuf, writebuf, subpgsize))) { |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 151 | pr_err("error: verify failed at %#llx\n", |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 152 | (long long)addr); |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 153 | pr_info("------------- written----------------\n"); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 154 | print_subpage(writebuf); |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 155 | pr_info("------------- read ------------------\n"); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 156 | print_subpage(readbuf); |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 157 | pr_info("-------------------------------------\n"); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 158 | errcnt += 1; |
| 159 | } |
| 160 | |
| 161 | addr += subpgsize; |
| 162 | |
Akinobu Mita | a312b78 | 2013-02-27 17:05:39 -0800 | [diff] [blame] | 163 | prandom_bytes_state(&rnd_state, writebuf, subpgsize); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 164 | clear_data(readbuf, subpgsize); |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 165 | err = mtd_read(mtd, addr, subpgsize, &read, readbuf); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 166 | if (unlikely(err || read != subpgsize)) { |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 167 | if (mtd_is_bitflip(err) && read == subpgsize) { |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 168 | pr_info("ECC correction at %#llx\n", |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 169 | (long long)addr); |
| 170 | err = 0; |
| 171 | } else { |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 172 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 173 | (long long)addr); |
| 174 | return err ? err : -1; |
| 175 | } |
| 176 | } |
| 177 | if (unlikely(memcmp(readbuf, writebuf, subpgsize))) { |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 178 | pr_info("error: verify failed at %#llx\n", |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 179 | (long long)addr); |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 180 | pr_info("------------- written----------------\n"); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 181 | print_subpage(writebuf); |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 182 | pr_info("------------- read ------------------\n"); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 183 | print_subpage(readbuf); |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 184 | pr_info("-------------------------------------\n"); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 185 | errcnt += 1; |
| 186 | } |
| 187 | |
| 188 | return err; |
| 189 | } |
| 190 | |
| 191 | static int verify_eraseblock2(int ebnum) |
| 192 | { |
Artem Bityutskiy | 30fa984 | 2011-12-29 15:16:28 +0200 | [diff] [blame] | 193 | size_t read; |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 194 | int err = 0, k; |
Brian Norris | 1001ff7 | 2014-07-21 19:07:12 -0700 | [diff] [blame] | 195 | loff_t addr = (loff_t)ebnum * mtd->erasesize; |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 196 | |
| 197 | for (k = 1; k < 33; ++k) { |
| 198 | if (addr + (subpgsize * k) > (ebnum + 1) * mtd->erasesize) |
| 199 | break; |
Akinobu Mita | a312b78 | 2013-02-27 17:05:39 -0800 | [diff] [blame] | 200 | prandom_bytes_state(&rnd_state, writebuf, subpgsize * k); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 201 | clear_data(readbuf, subpgsize * k); |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 202 | err = mtd_read(mtd, addr, subpgsize * k, &read, readbuf); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 203 | if (unlikely(err || read != subpgsize * k)) { |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 204 | if (mtd_is_bitflip(err) && read == subpgsize * k) { |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 205 | pr_info("ECC correction at %#llx\n", |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 206 | (long long)addr); |
| 207 | err = 0; |
| 208 | } else { |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 209 | pr_err("error: read failed at " |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 210 | "%#llx\n", (long long)addr); |
| 211 | return err ? err : -1; |
| 212 | } |
| 213 | } |
| 214 | if (unlikely(memcmp(readbuf, writebuf, subpgsize * k))) { |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 215 | pr_err("error: verify failed at %#llx\n", |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 216 | (long long)addr); |
| 217 | errcnt += 1; |
| 218 | } |
| 219 | addr += subpgsize * k; |
| 220 | } |
| 221 | |
| 222 | return err; |
| 223 | } |
| 224 | |
| 225 | static int verify_eraseblock_ff(int ebnum) |
| 226 | { |
| 227 | uint32_t j; |
Artem Bityutskiy | 30fa984 | 2011-12-29 15:16:28 +0200 | [diff] [blame] | 228 | size_t read; |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 229 | int err = 0; |
Brian Norris | 1001ff7 | 2014-07-21 19:07:12 -0700 | [diff] [blame] | 230 | loff_t addr = (loff_t)ebnum * mtd->erasesize; |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 231 | |
| 232 | memset(writebuf, 0xff, subpgsize); |
| 233 | for (j = 0; j < mtd->erasesize / subpgsize; ++j) { |
| 234 | clear_data(readbuf, subpgsize); |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 235 | err = mtd_read(mtd, addr, subpgsize, &read, readbuf); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 236 | if (unlikely(err || read != subpgsize)) { |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 237 | if (mtd_is_bitflip(err) && read == subpgsize) { |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 238 | pr_info("ECC correction at %#llx\n", |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 239 | (long long)addr); |
| 240 | err = 0; |
| 241 | } else { |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 242 | pr_err("error: read failed at " |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 243 | "%#llx\n", (long long)addr); |
| 244 | return err ? err : -1; |
| 245 | } |
| 246 | } |
| 247 | if (unlikely(memcmp(readbuf, writebuf, subpgsize))) { |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 248 | pr_err("error: verify 0xff failed at " |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 249 | "%#llx\n", (long long)addr); |
| 250 | errcnt += 1; |
| 251 | } |
| 252 | addr += subpgsize; |
| 253 | } |
| 254 | |
| 255 | return err; |
| 256 | } |
| 257 | |
| 258 | static int verify_all_eraseblocks_ff(void) |
| 259 | { |
| 260 | int err; |
| 261 | unsigned int i; |
| 262 | |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 263 | pr_info("verifying all eraseblocks for 0xff\n"); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 264 | for (i = 0; i < ebcnt; ++i) { |
| 265 | if (bbt[i]) |
| 266 | continue; |
| 267 | err = verify_eraseblock_ff(i); |
| 268 | if (err) |
| 269 | return err; |
| 270 | if (i % 256 == 0) |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 271 | pr_info("verified up to eraseblock %u\n", i); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 272 | cond_resched(); |
| 273 | } |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 274 | pr_info("verified %u eraseblocks\n", i); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 275 | return 0; |
| 276 | } |
| 277 | |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 278 | static int __init mtd_subpagetest_init(void) |
| 279 | { |
| 280 | int err = 0; |
| 281 | uint32_t i; |
| 282 | uint64_t tmp; |
| 283 | |
| 284 | printk(KERN_INFO "\n"); |
| 285 | printk(KERN_INFO "=================================================\n"); |
Wolfram Sang | 7406060 | 2011-10-30 00:11:53 +0200 | [diff] [blame] | 286 | |
| 287 | if (dev < 0) { |
Masanari Iida | 064a769 | 2012-11-09 23:20:58 +0900 | [diff] [blame] | 288 | pr_info("Please specify a valid mtd-device via module parameter\n"); |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 289 | 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] | 290 | return -EINVAL; |
| 291 | } |
| 292 | |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 293 | pr_info("MTD device: %d\n", dev); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 294 | |
| 295 | mtd = get_mtd_device(NULL, dev); |
| 296 | if (IS_ERR(mtd)) { |
| 297 | err = PTR_ERR(mtd); |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 298 | pr_err("error: cannot get MTD device\n"); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 299 | return err; |
| 300 | } |
| 301 | |
Huang Shijie | 818b973 | 2013-09-25 14:58:17 +0800 | [diff] [blame] | 302 | if (!mtd_type_is_nand(mtd)) { |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 303 | pr_info("this test requires NAND flash\n"); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 304 | goto out; |
| 305 | } |
| 306 | |
| 307 | subpgsize = mtd->writesize >> mtd->subpage_sft; |
Roman Tereshonkov | 7b7e905 | 2011-02-03 13:45:56 +0200 | [diff] [blame] | 308 | tmp = mtd->size; |
| 309 | do_div(tmp, mtd->erasesize); |
| 310 | ebcnt = tmp; |
| 311 | pgcnt = mtd->erasesize / mtd->writesize; |
| 312 | |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 313 | pr_info("MTD device size %llu, eraseblock size %u, " |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 314 | "page size %u, subpage size %u, count of eraseblocks %u, " |
| 315 | "pages per eraseblock %u, OOB size %u\n", |
| 316 | (unsigned long long)mtd->size, mtd->erasesize, |
| 317 | mtd->writesize, subpgsize, ebcnt, pgcnt, mtd->oobsize); |
| 318 | |
| 319 | err = -ENOMEM; |
| 320 | bufsize = subpgsize * 32; |
| 321 | writebuf = kmalloc(bufsize, GFP_KERNEL); |
Brian Norris | 33777e6 | 2013-05-02 14:18:51 -0700 | [diff] [blame] | 322 | if (!writebuf) |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 323 | goto out; |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 324 | readbuf = kmalloc(bufsize, GFP_KERNEL); |
Brian Norris | 33777e6 | 2013-05-02 14:18:51 -0700 | [diff] [blame] | 325 | if (!readbuf) |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 326 | goto out; |
Akinobu Mita | 725cd71 | 2013-08-03 18:52:14 +0900 | [diff] [blame] | 327 | bbt = kzalloc(ebcnt, GFP_KERNEL); |
| 328 | if (!bbt) |
| 329 | goto out; |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 330 | |
Akinobu Mita | 725cd71 | 2013-08-03 18:52:14 +0900 | [diff] [blame] | 331 | err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 332 | if (err) |
| 333 | goto out; |
| 334 | |
Akinobu Mita | 725cd71 | 2013-08-03 18:52:14 +0900 | [diff] [blame] | 335 | err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 336 | if (err) |
| 337 | goto out; |
| 338 | |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 339 | pr_info("writing whole device\n"); |
Akinobu Mita | a312b78 | 2013-02-27 17:05:39 -0800 | [diff] [blame] | 340 | prandom_seed_state(&rnd_state, 1); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 341 | for (i = 0; i < ebcnt; ++i) { |
| 342 | if (bbt[i]) |
| 343 | continue; |
| 344 | err = write_eraseblock(i); |
| 345 | if (unlikely(err)) |
| 346 | goto out; |
| 347 | if (i % 256 == 0) |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 348 | pr_info("written up to eraseblock %u\n", i); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 349 | cond_resched(); |
| 350 | } |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 351 | pr_info("written %u eraseblocks\n", i); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 352 | |
Akinobu Mita | a312b78 | 2013-02-27 17:05:39 -0800 | [diff] [blame] | 353 | prandom_seed_state(&rnd_state, 1); |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 354 | pr_info("verifying all eraseblocks\n"); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 355 | for (i = 0; i < ebcnt; ++i) { |
| 356 | if (bbt[i]) |
| 357 | continue; |
| 358 | err = verify_eraseblock(i); |
| 359 | if (unlikely(err)) |
| 360 | goto out; |
| 361 | if (i % 256 == 0) |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 362 | pr_info("verified up to eraseblock %u\n", i); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 363 | cond_resched(); |
| 364 | } |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 365 | pr_info("verified %u eraseblocks\n", i); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 366 | |
Akinobu Mita | 725cd71 | 2013-08-03 18:52:14 +0900 | [diff] [blame] | 367 | err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 368 | if (err) |
| 369 | goto out; |
| 370 | |
| 371 | err = verify_all_eraseblocks_ff(); |
| 372 | if (err) |
| 373 | goto out; |
| 374 | |
| 375 | /* Write all eraseblocks */ |
Akinobu Mita | a312b78 | 2013-02-27 17:05:39 -0800 | [diff] [blame] | 376 | prandom_seed_state(&rnd_state, 3); |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 377 | pr_info("writing whole device\n"); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 378 | for (i = 0; i < ebcnt; ++i) { |
| 379 | if (bbt[i]) |
| 380 | continue; |
| 381 | err = write_eraseblock2(i); |
| 382 | if (unlikely(err)) |
| 383 | goto out; |
| 384 | if (i % 256 == 0) |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 385 | pr_info("written up to eraseblock %u\n", i); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 386 | cond_resched(); |
| 387 | } |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 388 | pr_info("written %u eraseblocks\n", i); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 389 | |
| 390 | /* Check all eraseblocks */ |
Akinobu Mita | a312b78 | 2013-02-27 17:05:39 -0800 | [diff] [blame] | 391 | prandom_seed_state(&rnd_state, 3); |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 392 | pr_info("verifying all eraseblocks\n"); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 393 | for (i = 0; i < ebcnt; ++i) { |
| 394 | if (bbt[i]) |
| 395 | continue; |
| 396 | err = verify_eraseblock2(i); |
| 397 | if (unlikely(err)) |
| 398 | goto out; |
| 399 | if (i % 256 == 0) |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 400 | pr_info("verified up to eraseblock %u\n", i); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 401 | cond_resched(); |
| 402 | } |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 403 | pr_info("verified %u eraseblocks\n", i); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 404 | |
Akinobu Mita | 725cd71 | 2013-08-03 18:52:14 +0900 | [diff] [blame] | 405 | err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 406 | if (err) |
| 407 | goto out; |
| 408 | |
| 409 | err = verify_all_eraseblocks_ff(); |
| 410 | if (err) |
| 411 | goto out; |
| 412 | |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 413 | pr_info("finished with %d errors\n", errcnt); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 414 | |
| 415 | out: |
| 416 | kfree(bbt); |
| 417 | kfree(readbuf); |
| 418 | kfree(writebuf); |
| 419 | put_mtd_device(mtd); |
| 420 | if (err) |
Vikram Narayanan | cd66a2d | 2012-10-10 23:11:06 +0530 | [diff] [blame] | 421 | pr_info("error %d occurred\n", err); |
Artem Bityutskiy | bf60862 | 2008-12-08 13:37:48 +0200 | [diff] [blame] | 422 | printk(KERN_INFO "=================================================\n"); |
| 423 | return err; |
| 424 | } |
| 425 | module_init(mtd_subpagetest_init); |
| 426 | |
| 427 | static void __exit mtd_subpagetest_exit(void) |
| 428 | { |
| 429 | return; |
| 430 | } |
| 431 | module_exit(mtd_subpagetest_exit); |
| 432 | |
| 433 | MODULE_DESCRIPTION("Subpage test module"); |
| 434 | MODULE_AUTHOR("Adrian Hunter"); |
| 435 | MODULE_LICENSE("GPL"); |