Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006-2008 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 page read and write on MTD device. |
| 18 | * |
| 19 | * Author: Adrian Hunter <ext-adrian.hunter@nokia.com> |
| 20 | */ |
| 21 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 22 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 23 | |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 24 | #include <asm/div64.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/moduleparam.h> |
| 28 | #include <linux/err.h> |
| 29 | #include <linux/mtd/mtd.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 31 | #include <linux/sched.h> |
Akinobu Mita | 825b8cc | 2013-02-27 17:05:35 -0800 | [diff] [blame] | 32 | #include <linux/random.h> |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 33 | |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 34 | #include "mtd_test.h" |
| 35 | |
Wolfram Sang | 7406060 | 2011-10-30 00:11:53 +0200 | [diff] [blame] | 36 | static int dev = -EINVAL; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 37 | module_param(dev, int, S_IRUGO); |
| 38 | MODULE_PARM_DESC(dev, "MTD device number to use"); |
| 39 | |
| 40 | static struct mtd_info *mtd; |
| 41 | static unsigned char *twopages; |
| 42 | static unsigned char *writebuf; |
| 43 | static unsigned char *boundary; |
| 44 | static unsigned char *bbt; |
| 45 | |
| 46 | static int pgsize; |
| 47 | static int bufsize; |
| 48 | static int ebcnt; |
| 49 | static int pgcnt; |
| 50 | static int errcnt; |
Akinobu Mita | 825b8cc | 2013-02-27 17:05:35 -0800 | [diff] [blame] | 51 | static struct rnd_state rnd_state; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 52 | |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 53 | static int write_eraseblock(int ebnum) |
| 54 | { |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 55 | int err; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 56 | loff_t addr = ebnum * mtd->erasesize; |
| 57 | |
Akinobu Mita | 825b8cc | 2013-02-27 17:05:35 -0800 | [diff] [blame] | 58 | prandom_bytes_state(&rnd_state, writebuf, mtd->erasesize); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 59 | cond_resched(); |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 60 | err = mtdtest_write(mtd, addr, mtd->erasesize, writebuf); |
| 61 | if (err) |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 62 | pr_err("error: write failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 63 | (long long)addr); |
| 64 | |
| 65 | return err; |
| 66 | } |
| 67 | |
| 68 | static int verify_eraseblock(int ebnum) |
| 69 | { |
| 70 | uint32_t j; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 71 | int err = 0, i; |
| 72 | loff_t addr0, addrn; |
| 73 | loff_t addr = ebnum * mtd->erasesize; |
| 74 | |
| 75 | addr0 = 0; |
Roel Kluin | c6f7e7b | 2009-07-31 16:21:01 +0200 | [diff] [blame] | 76 | for (i = 0; i < ebcnt && bbt[i]; ++i) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 77 | addr0 += mtd->erasesize; |
| 78 | |
| 79 | addrn = mtd->size; |
Roel Kluin | c6f7e7b | 2009-07-31 16:21:01 +0200 | [diff] [blame] | 80 | for (i = 0; i < ebcnt && bbt[ebcnt - i - 1]; ++i) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 81 | addrn -= mtd->erasesize; |
| 82 | |
Akinobu Mita | 825b8cc | 2013-02-27 17:05:35 -0800 | [diff] [blame] | 83 | prandom_bytes_state(&rnd_state, writebuf, mtd->erasesize); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 84 | for (j = 0; j < pgcnt - 1; ++j, addr += pgsize) { |
| 85 | /* Do a read to set the internal dataRAMs to different data */ |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 86 | err = mtdtest_read(mtd, addr0, bufsize, twopages); |
| 87 | if (err) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 88 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 89 | (long long)addr0); |
| 90 | return err; |
| 91 | } |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 92 | err = mtdtest_read(mtd, addrn - bufsize, bufsize, twopages); |
| 93 | if (err) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 94 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 95 | (long long)(addrn - bufsize)); |
| 96 | return err; |
| 97 | } |
| 98 | memset(twopages, 0, bufsize); |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 99 | err = mtdtest_read(mtd, addr, bufsize, twopages); |
| 100 | if (err) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 101 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 102 | (long long)addr); |
| 103 | break; |
| 104 | } |
| 105 | if (memcmp(twopages, writebuf + (j * pgsize), bufsize)) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 106 | pr_err("error: verify failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 107 | (long long)addr); |
| 108 | errcnt += 1; |
| 109 | } |
| 110 | } |
| 111 | /* Check boundary between eraseblocks */ |
| 112 | if (addr <= addrn - pgsize - pgsize && !bbt[ebnum + 1]) { |
Akinobu Mita | 825b8cc | 2013-02-27 17:05:35 -0800 | [diff] [blame] | 113 | struct rnd_state old_state = rnd_state; |
| 114 | |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 115 | /* Do a read to set the internal dataRAMs to different data */ |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 116 | err = mtdtest_read(mtd, addr0, bufsize, twopages); |
| 117 | if (err) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 118 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 119 | (long long)addr0); |
| 120 | return err; |
| 121 | } |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 122 | err = mtdtest_read(mtd, addrn - bufsize, bufsize, twopages); |
| 123 | if (err) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 124 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 125 | (long long)(addrn - bufsize)); |
| 126 | return err; |
| 127 | } |
| 128 | memset(twopages, 0, bufsize); |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 129 | err = mtdtest_read(mtd, addr, bufsize, twopages); |
| 130 | if (err) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 131 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 132 | (long long)addr); |
| 133 | return err; |
| 134 | } |
| 135 | memcpy(boundary, writebuf + mtd->erasesize - pgsize, pgsize); |
Akinobu Mita | 825b8cc | 2013-02-27 17:05:35 -0800 | [diff] [blame] | 136 | prandom_bytes_state(&rnd_state, boundary + pgsize, pgsize); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 137 | if (memcmp(twopages, boundary, bufsize)) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 138 | pr_err("error: verify failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 139 | (long long)addr); |
| 140 | errcnt += 1; |
| 141 | } |
Akinobu Mita | 825b8cc | 2013-02-27 17:05:35 -0800 | [diff] [blame] | 142 | rnd_state = old_state; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 143 | } |
| 144 | return err; |
| 145 | } |
| 146 | |
| 147 | static int crosstest(void) |
| 148 | { |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 149 | int err = 0, i; |
| 150 | loff_t addr, addr0, addrn; |
| 151 | unsigned char *pp1, *pp2, *pp3, *pp4; |
| 152 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 153 | pr_info("crosstest\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 154 | pp1 = kmalloc(pgsize * 4, GFP_KERNEL); |
Brian Norris | 33777e6 | 2013-05-02 14:18:51 -0700 | [diff] [blame] | 155 | if (!pp1) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 156 | return -ENOMEM; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 157 | pp2 = pp1 + pgsize; |
| 158 | pp3 = pp2 + pgsize; |
| 159 | pp4 = pp3 + pgsize; |
| 160 | memset(pp1, 0, pgsize * 4); |
| 161 | |
| 162 | addr0 = 0; |
Roel Kluin | c6f7e7b | 2009-07-31 16:21:01 +0200 | [diff] [blame] | 163 | for (i = 0; i < ebcnt && bbt[i]; ++i) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 164 | addr0 += mtd->erasesize; |
| 165 | |
| 166 | addrn = mtd->size; |
Roel Kluin | c6f7e7b | 2009-07-31 16:21:01 +0200 | [diff] [blame] | 167 | for (i = 0; i < ebcnt && bbt[ebcnt - i - 1]; ++i) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 168 | addrn -= mtd->erasesize; |
| 169 | |
| 170 | /* Read 2nd-to-last page to pp1 */ |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 171 | addr = addrn - pgsize - pgsize; |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 172 | err = mtdtest_read(mtd, addr, pgsize, pp1); |
| 173 | if (err) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 174 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 175 | (long long)addr); |
| 176 | kfree(pp1); |
| 177 | return err; |
| 178 | } |
| 179 | |
| 180 | /* Read 3rd-to-last page to pp1 */ |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 181 | addr = addrn - pgsize - pgsize - pgsize; |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 182 | err = mtdtest_read(mtd, addr, pgsize, pp1); |
| 183 | if (err) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 184 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 185 | (long long)addr); |
| 186 | kfree(pp1); |
| 187 | return err; |
| 188 | } |
| 189 | |
| 190 | /* Read first page to pp2 */ |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 191 | addr = addr0; |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 192 | pr_info("reading page at %#llx\n", (long long)addr); |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 193 | err = mtdtest_read(mtd, addr, pgsize, pp2); |
| 194 | if (err) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 195 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 196 | (long long)addr); |
| 197 | kfree(pp1); |
| 198 | return err; |
| 199 | } |
| 200 | |
| 201 | /* Read last page to pp3 */ |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 202 | addr = addrn - pgsize; |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 203 | pr_info("reading page at %#llx\n", (long long)addr); |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 204 | err = mtdtest_read(mtd, addr, pgsize, pp3); |
| 205 | if (err) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 206 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 207 | (long long)addr); |
| 208 | kfree(pp1); |
| 209 | return err; |
| 210 | } |
| 211 | |
| 212 | /* Read first page again to pp4 */ |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 213 | addr = addr0; |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 214 | pr_info("reading page at %#llx\n", (long long)addr); |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 215 | err = mtdtest_read(mtd, addr, pgsize, pp4); |
| 216 | if (err) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 217 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 218 | (long long)addr); |
| 219 | kfree(pp1); |
| 220 | return err; |
| 221 | } |
| 222 | |
| 223 | /* pp2 and pp4 should be the same */ |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 224 | pr_info("verifying pages read at %#llx match\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 225 | (long long)addr0); |
| 226 | if (memcmp(pp2, pp4, pgsize)) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 227 | pr_err("verify failed!\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 228 | errcnt += 1; |
| 229 | } else if (!err) |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 230 | pr_info("crosstest ok\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 231 | kfree(pp1); |
| 232 | return err; |
| 233 | } |
| 234 | |
| 235 | static int erasecrosstest(void) |
| 236 | { |
Roel Kluin | 7fc14bc | 2010-07-13 13:24:31 +0300 | [diff] [blame] | 237 | int err = 0, i, ebnum, ebnum2; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 238 | loff_t addr0; |
| 239 | char *readbuf = twopages; |
| 240 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 241 | pr_info("erasecrosstest\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 242 | |
| 243 | ebnum = 0; |
| 244 | addr0 = 0; |
Roel Kluin | c6f7e7b | 2009-07-31 16:21:01 +0200 | [diff] [blame] | 245 | for (i = 0; i < ebcnt && bbt[i]; ++i) { |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 246 | addr0 += mtd->erasesize; |
| 247 | ebnum += 1; |
| 248 | } |
| 249 | |
| 250 | ebnum2 = ebcnt - 1; |
| 251 | while (ebnum2 && bbt[ebnum2]) |
| 252 | ebnum2 -= 1; |
| 253 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 254 | pr_info("erasing block %d\n", ebnum); |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 255 | err = mtdtest_erase_eraseblock(mtd, ebnum); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 256 | if (err) |
| 257 | return err; |
| 258 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 259 | pr_info("writing 1st page of block %d\n", ebnum); |
Akinobu Mita | 825b8cc | 2013-02-27 17:05:35 -0800 | [diff] [blame] | 260 | prandom_bytes_state(&rnd_state, writebuf, pgsize); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 261 | strcpy(writebuf, "There is no data like this!"); |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 262 | err = mtdtest_write(mtd, addr0, pgsize, writebuf); |
| 263 | if (err) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 264 | pr_info("error: write failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 265 | (long long)addr0); |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 266 | return err; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 267 | } |
| 268 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 269 | pr_info("reading 1st page of block %d\n", ebnum); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 270 | memset(readbuf, 0, pgsize); |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 271 | err = mtdtest_read(mtd, addr0, pgsize, readbuf); |
| 272 | if (err) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 273 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 274 | (long long)addr0); |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 275 | return err; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 276 | } |
| 277 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 278 | pr_info("verifying 1st page of block %d\n", ebnum); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 279 | if (memcmp(writebuf, readbuf, pgsize)) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 280 | pr_err("verify failed!\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 281 | errcnt += 1; |
Roel Kluin | 7fc14bc | 2010-07-13 13:24:31 +0300 | [diff] [blame] | 282 | return -1; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 283 | } |
| 284 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 285 | pr_info("erasing block %d\n", ebnum); |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 286 | err = mtdtest_erase_eraseblock(mtd, ebnum); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 287 | if (err) |
| 288 | return err; |
| 289 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 290 | pr_info("writing 1st page of block %d\n", ebnum); |
Akinobu Mita | 825b8cc | 2013-02-27 17:05:35 -0800 | [diff] [blame] | 291 | prandom_bytes_state(&rnd_state, writebuf, pgsize); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 292 | strcpy(writebuf, "There is no data like this!"); |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 293 | err = mtdtest_write(mtd, addr0, pgsize, writebuf); |
| 294 | if (err) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 295 | pr_err("error: write failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 296 | (long long)addr0); |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 297 | return err; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 298 | } |
| 299 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 300 | pr_info("erasing block %d\n", ebnum2); |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 301 | err = mtdtest_erase_eraseblock(mtd, ebnum2); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 302 | if (err) |
| 303 | return err; |
| 304 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 305 | pr_info("reading 1st page of block %d\n", ebnum); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 306 | memset(readbuf, 0, pgsize); |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 307 | err = mtdtest_read(mtd, addr0, pgsize, readbuf); |
| 308 | if (err) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 309 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 310 | (long long)addr0); |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 311 | return err; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 312 | } |
| 313 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 314 | pr_info("verifying 1st page of block %d\n", ebnum); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 315 | if (memcmp(writebuf, readbuf, pgsize)) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 316 | pr_err("verify failed!\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 317 | errcnt += 1; |
Roel Kluin | 7fc14bc | 2010-07-13 13:24:31 +0300 | [diff] [blame] | 318 | return -1; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 319 | } |
| 320 | |
Roel Kluin | 7fc14bc | 2010-07-13 13:24:31 +0300 | [diff] [blame] | 321 | if (!err) |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 322 | pr_info("erasecrosstest ok\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 323 | return err; |
| 324 | } |
| 325 | |
| 326 | static int erasetest(void) |
| 327 | { |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 328 | int err = 0, i, ebnum, ok = 1; |
| 329 | loff_t addr0; |
| 330 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 331 | pr_info("erasetest\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 332 | |
| 333 | ebnum = 0; |
| 334 | addr0 = 0; |
Roel Kluin | c6f7e7b | 2009-07-31 16:21:01 +0200 | [diff] [blame] | 335 | for (i = 0; i < ebcnt && bbt[i]; ++i) { |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 336 | addr0 += mtd->erasesize; |
| 337 | ebnum += 1; |
| 338 | } |
| 339 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 340 | pr_info("erasing block %d\n", ebnum); |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 341 | err = mtdtest_erase_eraseblock(mtd, ebnum); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 342 | if (err) |
| 343 | return err; |
| 344 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 345 | pr_info("writing 1st page of block %d\n", ebnum); |
Akinobu Mita | 825b8cc | 2013-02-27 17:05:35 -0800 | [diff] [blame] | 346 | prandom_bytes_state(&rnd_state, writebuf, pgsize); |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 347 | err = mtdtest_write(mtd, addr0, pgsize, writebuf); |
| 348 | if (err) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 349 | pr_err("error: write failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 350 | (long long)addr0); |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 351 | return err; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 352 | } |
| 353 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 354 | pr_info("erasing block %d\n", ebnum); |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 355 | err = mtdtest_erase_eraseblock(mtd, ebnum); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 356 | if (err) |
| 357 | return err; |
| 358 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 359 | pr_info("reading 1st page of block %d\n", ebnum); |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 360 | err = mtdtest_read(mtd, addr0, pgsize, twopages); |
| 361 | if (err) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 362 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 363 | (long long)addr0); |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 364 | return err; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 365 | } |
| 366 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 367 | pr_info("verifying 1st page of block %d is all 0xff\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 368 | ebnum); |
| 369 | for (i = 0; i < pgsize; ++i) |
| 370 | if (twopages[i] != 0xff) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 371 | pr_err("verifying all 0xff failed at %d\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 372 | i); |
| 373 | errcnt += 1; |
| 374 | ok = 0; |
| 375 | break; |
| 376 | } |
| 377 | |
| 378 | if (ok && !err) |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 379 | pr_info("erasetest ok\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 380 | |
| 381 | return err; |
| 382 | } |
| 383 | |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 384 | static int __init mtd_pagetest_init(void) |
| 385 | { |
| 386 | int err = 0; |
| 387 | uint64_t tmp; |
| 388 | uint32_t i; |
| 389 | |
| 390 | printk(KERN_INFO "\n"); |
| 391 | printk(KERN_INFO "=================================================\n"); |
Wolfram Sang | 7406060 | 2011-10-30 00:11:53 +0200 | [diff] [blame] | 392 | |
| 393 | if (dev < 0) { |
Masanari Iida | 064a769 | 2012-11-09 23:20:58 +0900 | [diff] [blame] | 394 | pr_info("Please specify a valid mtd-device via module parameter\n"); |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 395 | 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] | 396 | return -EINVAL; |
| 397 | } |
| 398 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 399 | pr_info("MTD device: %d\n", dev); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 400 | |
| 401 | mtd = get_mtd_device(NULL, dev); |
| 402 | if (IS_ERR(mtd)) { |
| 403 | err = PTR_ERR(mtd); |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 404 | pr_err("error: cannot get MTD device\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 405 | return err; |
| 406 | } |
| 407 | |
| 408 | if (mtd->type != MTD_NANDFLASH) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 409 | pr_info("this test requires NAND flash\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 410 | goto out; |
| 411 | } |
| 412 | |
| 413 | tmp = mtd->size; |
| 414 | do_div(tmp, mtd->erasesize); |
| 415 | ebcnt = tmp; |
| 416 | pgcnt = mtd->erasesize / mtd->writesize; |
Artem Bityutskiy | 4c2b8a6 | 2009-11-27 16:58:08 +0200 | [diff] [blame] | 417 | pgsize = mtd->writesize; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 418 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 419 | pr_info("MTD device size %llu, eraseblock size %u, " |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 420 | "page size %u, count of eraseblocks %u, pages per " |
| 421 | "eraseblock %u, OOB size %u\n", |
| 422 | (unsigned long long)mtd->size, mtd->erasesize, |
| 423 | pgsize, ebcnt, pgcnt, mtd->oobsize); |
| 424 | |
| 425 | err = -ENOMEM; |
| 426 | bufsize = pgsize * 2; |
| 427 | writebuf = kmalloc(mtd->erasesize, GFP_KERNEL); |
Brian Norris | 33777e6 | 2013-05-02 14:18:51 -0700 | [diff] [blame] | 428 | if (!writebuf) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 429 | goto out; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 430 | twopages = kmalloc(bufsize, GFP_KERNEL); |
Brian Norris | 33777e6 | 2013-05-02 14:18:51 -0700 | [diff] [blame] | 431 | if (!twopages) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 432 | goto out; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 433 | boundary = kmalloc(bufsize, GFP_KERNEL); |
Brian Norris | 33777e6 | 2013-05-02 14:18:51 -0700 | [diff] [blame] | 434 | if (!boundary) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 435 | goto out; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 436 | |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 437 | bbt = kzalloc(ebcnt, GFP_KERNEL); |
| 438 | if (!bbt) |
| 439 | goto out; |
| 440 | err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 441 | if (err) |
| 442 | goto out; |
| 443 | |
| 444 | /* Erase all eraseblocks */ |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 445 | pr_info("erasing whole device\n"); |
Akinobu Mita | 66b2818 | 2013-08-03 18:52:10 +0900 | [diff] [blame] | 446 | err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt); |
| 447 | if (err) |
| 448 | goto out; |
| 449 | pr_info("erased %u eraseblocks\n", ebcnt); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 450 | |
| 451 | /* Write all eraseblocks */ |
Akinobu Mita | 825b8cc | 2013-02-27 17:05:35 -0800 | [diff] [blame] | 452 | prandom_seed_state(&rnd_state, 1); |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 453 | pr_info("writing whole device\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 454 | for (i = 0; i < ebcnt; ++i) { |
| 455 | if (bbt[i]) |
| 456 | continue; |
| 457 | err = write_eraseblock(i); |
| 458 | if (err) |
| 459 | goto out; |
| 460 | if (i % 256 == 0) |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 461 | pr_info("written up to eraseblock %u\n", i); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 462 | cond_resched(); |
| 463 | } |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 464 | pr_info("written %u eraseblocks\n", i); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 465 | |
| 466 | /* Check all eraseblocks */ |
Akinobu Mita | 825b8cc | 2013-02-27 17:05:35 -0800 | [diff] [blame] | 467 | prandom_seed_state(&rnd_state, 1); |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 468 | pr_info("verifying all eraseblocks\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 469 | for (i = 0; i < ebcnt; ++i) { |
| 470 | if (bbt[i]) |
| 471 | continue; |
| 472 | err = verify_eraseblock(i); |
| 473 | if (err) |
| 474 | goto out; |
| 475 | if (i % 256 == 0) |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 476 | pr_info("verified up to eraseblock %u\n", i); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 477 | cond_resched(); |
| 478 | } |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 479 | pr_info("verified %u eraseblocks\n", i); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 480 | |
| 481 | err = crosstest(); |
| 482 | if (err) |
| 483 | goto out; |
| 484 | |
| 485 | err = erasecrosstest(); |
| 486 | if (err) |
| 487 | goto out; |
| 488 | |
| 489 | err = erasetest(); |
| 490 | if (err) |
| 491 | goto out; |
| 492 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 493 | pr_info("finished with %d errors\n", errcnt); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 494 | out: |
| 495 | |
| 496 | kfree(bbt); |
| 497 | kfree(boundary); |
| 498 | kfree(twopages); |
| 499 | kfree(writebuf); |
| 500 | put_mtd_device(mtd); |
| 501 | if (err) |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 502 | pr_info("error %d occurred\n", err); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 503 | printk(KERN_INFO "=================================================\n"); |
| 504 | return err; |
| 505 | } |
| 506 | module_init(mtd_pagetest_init); |
| 507 | |
| 508 | static void __exit mtd_pagetest_exit(void) |
| 509 | { |
| 510 | return; |
| 511 | } |
| 512 | module_exit(mtd_pagetest_exit); |
| 513 | |
| 514 | MODULE_DESCRIPTION("NAND page test"); |
| 515 | MODULE_AUTHOR("Adrian Hunter"); |
| 516 | MODULE_LICENSE("GPL"); |