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