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