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