blob: 8e8525f0202f60b2349cc0fcff0357b038d6417e [file] [log] [blame]
Artem Bityutskiye3644da2008-12-08 13:33:29 +02001/*
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 OOB read and write on MTD device.
18 *
19 * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
20 */
21
Vikram Narayanan04810272012-10-10 23:12:02 +053022#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
Artem Bityutskiye3644da2008-12-08 13:33:29 +020024#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 Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Artem Bityutskiye3644da2008-12-08 13:33:29 +020031#include <linux/sched.h>
Akinobu Mita8dad0492013-02-27 17:05:33 -080032#include <linux/random.h>
Artem Bityutskiye3644da2008-12-08 13:33:29 +020033
Akinobu Mita4bf527a2013-08-03 18:52:09 +090034#include "mtd_test.h"
35
Wolfram Sang74060602011-10-30 00:11:53 +020036static int dev = -EINVAL;
Roger Quadrosafc0ea12014-10-21 16:53:28 +030037static int bitflip_limit;
Artem Bityutskiye3644da2008-12-08 13:33:29 +020038module_param(dev, int, S_IRUGO);
39MODULE_PARM_DESC(dev, "MTD device number to use");
Roger Quadrosafc0ea12014-10-21 16:53:28 +030040module_param(bitflip_limit, int, S_IRUGO);
41MODULE_PARM_DESC(bitflip_limit, "Max. allowed bitflips per page");
Artem Bityutskiye3644da2008-12-08 13:33:29 +020042
43static struct mtd_info *mtd;
44static unsigned char *readbuf;
45static unsigned char *writebuf;
46static unsigned char *bbt;
47
48static int ebcnt;
49static int pgcnt;
50static int errcnt;
51static int use_offset;
52static int use_len;
53static int use_len_max;
54static int vary_offset;
Akinobu Mita8dad0492013-02-27 17:05:33 -080055static struct rnd_state rnd_state;
Artem Bityutskiye3644da2008-12-08 13:33:29 +020056
Artem Bityutskiye3644da2008-12-08 13:33:29 +020057static void do_vary_offset(void)
58{
59 use_len -= 1;
60 if (use_len < 1) {
61 use_offset += 1;
62 if (use_offset >= use_len_max)
63 use_offset = 0;
64 use_len = use_len_max - use_offset;
65 }
66}
67
68static int write_eraseblock(int ebnum)
69{
70 int i;
71 struct mtd_oob_ops ops;
72 int err = 0;
Brian Norrisb9da8ba2015-02-28 02:02:26 -080073 loff_t addr = (loff_t)ebnum * mtd->erasesize;
Artem Bityutskiye3644da2008-12-08 13:33:29 +020074
Akinobu Mitabe54f8f2014-03-08 00:24:10 +090075 prandom_bytes_state(&rnd_state, writebuf, use_len_max * pgcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +020076 for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
Brian Norris0612b9d2011-08-30 18:45:40 -070077 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +020078 ops.len = 0;
79 ops.retlen = 0;
80 ops.ooblen = use_len;
81 ops.oobretlen = 0;
82 ops.ooboffs = use_offset;
Hannes Eder23d42492009-03-05 20:15:01 +010083 ops.datbuf = NULL;
Akinobu Mitabe54f8f2014-03-08 00:24:10 +090084 ops.oobbuf = writebuf + (use_len_max * i) + use_offset;
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +020085 err = mtd_write_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +020086 if (err || ops.oobretlen != use_len) {
Vikram Narayanan04810272012-10-10 23:12:02 +053087 pr_err("error: writeoob failed at %#llx\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +020088 (long long)addr);
Vikram Narayanan04810272012-10-10 23:12:02 +053089 pr_err("error: use_len %d, use_offset %d\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +020090 use_len, use_offset);
91 errcnt += 1;
92 return err ? err : -1;
93 }
94 if (vary_offset)
95 do_vary_offset();
96 }
97
98 return err;
99}
100
101static int write_whole_device(void)
102{
103 int err;
104 unsigned int i;
105
Vikram Narayanan04810272012-10-10 23:12:02 +0530106 pr_info("writing OOBs of whole device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200107 for (i = 0; i < ebcnt; ++i) {
108 if (bbt[i])
109 continue;
110 err = write_eraseblock(i);
111 if (err)
112 return err;
113 if (i % 256 == 0)
Vikram Narayanan04810272012-10-10 23:12:02 +0530114 pr_info("written up to eraseblock %u\n", i);
Richard Weinberger2a6a28e72015-03-29 21:52:06 +0200115
116 err = mtdtest_relax();
117 if (err)
118 return err;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200119 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530120 pr_info("written %u eraseblocks\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200121 return 0;
122}
123
Roger Quadrosafc0ea12014-10-21 16:53:28 +0300124/*
125 * Display the address, offset and data bytes at comparison failure.
126 * Return number of bitflips encountered.
127 */
128static size_t memcmpshow(loff_t addr, const void *cs, const void *ct, size_t count)
Roger Quadros5a660882014-10-21 16:53:27 +0300129{
130 const unsigned char *su1, *su2;
131 int res;
Roger Quadros5a660882014-10-21 16:53:27 +0300132 size_t i = 0;
Roger Quadrosafc0ea12014-10-21 16:53:28 +0300133 size_t bitflips = 0;
Roger Quadros5a660882014-10-21 16:53:27 +0300134
135 for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--, i++) {
136 res = *su1 ^ *su2;
137 if (res) {
Brian Norris806b6ef2014-11-20 01:26:51 -0800138 pr_info("error @addr[0x%lx:0x%zx] 0x%x -> 0x%x diff 0x%x\n",
Roger Quadros5a660882014-10-21 16:53:27 +0300139 (unsigned long)addr, i, *su1, *su2, res);
Roger Quadrosafc0ea12014-10-21 16:53:28 +0300140 bitflips += hweight8(res);
Roger Quadros5a660882014-10-21 16:53:27 +0300141 }
142 }
143
Roger Quadrosafc0ea12014-10-21 16:53:28 +0300144 return bitflips;
Roger Quadros5a660882014-10-21 16:53:27 +0300145}
146
Roger Quadrosd2b51c82014-12-05 17:18:39 +0200147/*
148 * Compare with 0xff and show the address, offset and data bytes at
149 * comparison failure. Return number of bitflips encountered.
150 */
151static size_t memffshow(loff_t addr, loff_t offset, const void *cs,
152 size_t count)
153{
154 const unsigned char *su1;
155 int res;
156 size_t i = 0;
157 size_t bitflips = 0;
158
159 for (su1 = cs; 0 < count; ++su1, count--, i++) {
160 res = *su1 ^ 0xff;
161 if (res) {
162 pr_info("error @addr[0x%lx:0x%lx] 0x%x -> 0xff diff 0x%x\n",
163 (unsigned long)addr, (unsigned long)offset + i,
164 *su1, res);
165 bitflips += hweight8(res);
166 }
167 }
168
169 return bitflips;
170}
171
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200172static int verify_eraseblock(int ebnum)
173{
174 int i;
175 struct mtd_oob_ops ops;
176 int err = 0;
Brian Norris1001ff7a2014-07-21 19:07:12 -0700177 loff_t addr = (loff_t)ebnum * mtd->erasesize;
Roger Quadrosafc0ea12014-10-21 16:53:28 +0300178 size_t bitflips;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200179
Akinobu Mitabe54f8f2014-03-08 00:24:10 +0900180 prandom_bytes_state(&rnd_state, writebuf, use_len_max * pgcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200181 for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
Brian Norris0612b9d2011-08-30 18:45:40 -0700182 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200183 ops.len = 0;
184 ops.retlen = 0;
185 ops.ooblen = use_len;
186 ops.oobretlen = 0;
187 ops.ooboffs = use_offset;
Hannes Eder23d42492009-03-05 20:15:01 +0100188 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200189 ops.oobbuf = readbuf;
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200190 err = mtd_read_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200191 if (err || ops.oobretlen != use_len) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530192 pr_err("error: readoob failed at %#llx\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200193 (long long)addr);
194 errcnt += 1;
195 return err ? err : -1;
196 }
Roger Quadrosafc0ea12014-10-21 16:53:28 +0300197
198 bitflips = memcmpshow(addr, readbuf,
199 writebuf + (use_len_max * i) + use_offset,
200 use_len);
201 if (bitflips > bitflip_limit) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530202 pr_err("error: verify failed at %#llx\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200203 (long long)addr);
204 errcnt += 1;
205 if (errcnt > 1000) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530206 pr_err("error: too many errors\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200207 return -1;
208 }
Roger Quadrosafc0ea12014-10-21 16:53:28 +0300209 } else if (bitflips) {
210 pr_info("ignoring error as within bitflip_limit\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200211 }
Roger Quadrosafc0ea12014-10-21 16:53:28 +0300212
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200213 if (use_offset != 0 || use_len < mtd->ecclayout->oobavail) {
214 int k;
215
Brian Norris0612b9d2011-08-30 18:45:40 -0700216 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200217 ops.len = 0;
218 ops.retlen = 0;
219 ops.ooblen = mtd->ecclayout->oobavail;
220 ops.oobretlen = 0;
221 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100222 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200223 ops.oobbuf = readbuf;
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200224 err = mtd_read_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200225 if (err || ops.oobretlen != mtd->ecclayout->oobavail) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530226 pr_err("error: readoob failed at %#llx\n",
227 (long long)addr);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200228 errcnt += 1;
229 return err ? err : -1;
230 }
Roger Quadrosafc0ea12014-10-21 16:53:28 +0300231 bitflips = memcmpshow(addr, readbuf + use_offset,
232 writebuf + (use_len_max * i) + use_offset,
233 use_len);
Roger Quadrosd2b51c82014-12-05 17:18:39 +0200234
235 /* verify pre-offset area for 0xff */
236 bitflips += memffshow(addr, 0, readbuf, use_offset);
237
238 /* verify post-(use_offset + use_len) area for 0xff */
239 k = use_offset + use_len;
240 bitflips += memffshow(addr, k, readbuf + k,
241 mtd->ecclayout->oobavail - k);
242
Roger Quadrosafc0ea12014-10-21 16:53:28 +0300243 if (bitflips > bitflip_limit) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530244 pr_err("error: verify failed at %#llx\n",
245 (long long)addr);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200246 errcnt += 1;
247 if (errcnt > 1000) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530248 pr_err("error: too many errors\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200249 return -1;
250 }
Roger Quadrosafc0ea12014-10-21 16:53:28 +0300251 } else if (bitflips) {
Roger Quadrosd2b51c82014-12-05 17:18:39 +0200252 pr_info("ignoring errors as within bitflip limit\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200253 }
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200254 }
255 if (vary_offset)
256 do_vary_offset();
257 }
258 return err;
259}
260
261static int verify_eraseblock_in_one_go(int ebnum)
262{
263 struct mtd_oob_ops ops;
264 int err = 0;
Brian Norris1001ff7a2014-07-21 19:07:12 -0700265 loff_t addr = (loff_t)ebnum * mtd->erasesize;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200266 size_t len = mtd->ecclayout->oobavail * pgcnt;
Roger Quadrosafc0ea12014-10-21 16:53:28 +0300267 size_t oobavail = mtd->ecclayout->oobavail;
268 size_t bitflips;
269 int i;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200270
Akinobu Mita8dad0492013-02-27 17:05:33 -0800271 prandom_bytes_state(&rnd_state, writebuf, len);
Brian Norris0612b9d2011-08-30 18:45:40 -0700272 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200273 ops.len = 0;
274 ops.retlen = 0;
275 ops.ooblen = len;
276 ops.oobretlen = 0;
277 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100278 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200279 ops.oobbuf = readbuf;
Roger Quadrosafc0ea12014-10-21 16:53:28 +0300280
281 /* read entire block's OOB at one go */
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200282 err = mtd_read_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200283 if (err || ops.oobretlen != len) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530284 pr_err("error: readoob failed at %#llx\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200285 (long long)addr);
286 errcnt += 1;
287 return err ? err : -1;
288 }
Roger Quadrosafc0ea12014-10-21 16:53:28 +0300289
290 /* verify one page OOB at a time for bitflip per page limit check */
291 for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
292 bitflips = memcmpshow(addr, readbuf + (i * oobavail),
293 writebuf + (i * oobavail), oobavail);
294 if (bitflips > bitflip_limit) {
295 pr_err("error: verify failed at %#llx\n",
296 (long long)addr);
297 errcnt += 1;
298 if (errcnt > 1000) {
299 pr_err("error: too many errors\n");
300 return -1;
301 }
302 } else if (bitflips) {
303 pr_info("ignoring error as within bitflip_limit\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200304 }
305 }
306
307 return err;
308}
309
310static int verify_all_eraseblocks(void)
311{
312 int err;
313 unsigned int i;
314
Vikram Narayanan04810272012-10-10 23:12:02 +0530315 pr_info("verifying all eraseblocks\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200316 for (i = 0; i < ebcnt; ++i) {
317 if (bbt[i])
318 continue;
319 err = verify_eraseblock(i);
320 if (err)
321 return err;
322 if (i % 256 == 0)
Vikram Narayanan04810272012-10-10 23:12:02 +0530323 pr_info("verified up to eraseblock %u\n", i);
Richard Weinberger2a6a28e72015-03-29 21:52:06 +0200324
325 err = mtdtest_relax();
326 if (err)
327 return err;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200328 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530329 pr_info("verified %u eraseblocks\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200330 return 0;
331}
332
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200333static int __init mtd_oobtest_init(void)
334{
335 int err = 0;
336 unsigned int i;
337 uint64_t tmp;
338 struct mtd_oob_ops ops;
339 loff_t addr = 0, addr0;
340
341 printk(KERN_INFO "\n");
342 printk(KERN_INFO "=================================================\n");
Wolfram Sang74060602011-10-30 00:11:53 +0200343
344 if (dev < 0) {
Masanari Iida064a7692012-11-09 23:20:58 +0900345 pr_info("Please specify a valid mtd-device via module parameter\n");
346 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
Wolfram Sang74060602011-10-30 00:11:53 +0200347 return -EINVAL;
348 }
349
Vikram Narayanan04810272012-10-10 23:12:02 +0530350 pr_info("MTD device: %d\n", dev);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200351
352 mtd = get_mtd_device(NULL, dev);
353 if (IS_ERR(mtd)) {
354 err = PTR_ERR(mtd);
Vikram Narayanan04810272012-10-10 23:12:02 +0530355 pr_err("error: cannot get MTD device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200356 return err;
357 }
358
Huang Shijie818b9732013-09-25 14:58:17 +0800359 if (!mtd_type_is_nand(mtd)) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530360 pr_info("this test requires NAND flash\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200361 goto out;
362 }
363
364 tmp = mtd->size;
365 do_div(tmp, mtd->erasesize);
366 ebcnt = tmp;
367 pgcnt = mtd->erasesize / mtd->writesize;
368
Vikram Narayanan04810272012-10-10 23:12:02 +0530369 pr_info("MTD device size %llu, eraseblock size %u, "
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200370 "page size %u, count of eraseblocks %u, pages per "
371 "eraseblock %u, OOB size %u\n",
372 (unsigned long long)mtd->size, mtd->erasesize,
373 mtd->writesize, ebcnt, pgcnt, mtd->oobsize);
374
375 err = -ENOMEM;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200376 readbuf = kmalloc(mtd->erasesize, GFP_KERNEL);
Brian Norris33777e62013-05-02 14:18:51 -0700377 if (!readbuf)
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200378 goto out;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200379 writebuf = kmalloc(mtd->erasesize, GFP_KERNEL);
Brian Norris33777e62013-05-02 14:18:51 -0700380 if (!writebuf)
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200381 goto out;
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900382 bbt = kzalloc(ebcnt, GFP_KERNEL);
383 if (!bbt)
384 goto out;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200385
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900386 err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200387 if (err)
388 goto out;
389
390 use_offset = 0;
391 use_len = mtd->ecclayout->oobavail;
392 use_len_max = mtd->ecclayout->oobavail;
393 vary_offset = 0;
394
395 /* First test: write all OOB, read it back and verify */
Vikram Narayanan04810272012-10-10 23:12:02 +0530396 pr_info("test 1 of 5\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200397
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900398 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200399 if (err)
400 goto out;
401
Akinobu Mita8dad0492013-02-27 17:05:33 -0800402 prandom_seed_state(&rnd_state, 1);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200403 err = write_whole_device();
404 if (err)
405 goto out;
406
Akinobu Mita8dad0492013-02-27 17:05:33 -0800407 prandom_seed_state(&rnd_state, 1);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200408 err = verify_all_eraseblocks();
409 if (err)
410 goto out;
411
412 /*
413 * Second test: write all OOB, a block at a time, read it back and
414 * verify.
415 */
Vikram Narayanan04810272012-10-10 23:12:02 +0530416 pr_info("test 2 of 5\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200417
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900418 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200419 if (err)
420 goto out;
421
Akinobu Mita8dad0492013-02-27 17:05:33 -0800422 prandom_seed_state(&rnd_state, 3);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200423 err = write_whole_device();
424 if (err)
425 goto out;
426
427 /* Check all eraseblocks */
Akinobu Mita8dad0492013-02-27 17:05:33 -0800428 prandom_seed_state(&rnd_state, 3);
Vikram Narayanan04810272012-10-10 23:12:02 +0530429 pr_info("verifying all eraseblocks\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200430 for (i = 0; i < ebcnt; ++i) {
431 if (bbt[i])
432 continue;
433 err = verify_eraseblock_in_one_go(i);
434 if (err)
435 goto out;
436 if (i % 256 == 0)
Vikram Narayanan04810272012-10-10 23:12:02 +0530437 pr_info("verified up to eraseblock %u\n", i);
Richard Weinberger2a6a28e72015-03-29 21:52:06 +0200438
439 err = mtdtest_relax();
440 if (err)
441 goto out;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200442 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530443 pr_info("verified %u eraseblocks\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200444
445 /*
446 * Third test: write OOB at varying offsets and lengths, read it back
447 * and verify.
448 */
Vikram Narayanan04810272012-10-10 23:12:02 +0530449 pr_info("test 3 of 5\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200450
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900451 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200452 if (err)
453 goto out;
454
455 /* Write all eraseblocks */
456 use_offset = 0;
457 use_len = mtd->ecclayout->oobavail;
458 use_len_max = mtd->ecclayout->oobavail;
459 vary_offset = 1;
Akinobu Mita8dad0492013-02-27 17:05:33 -0800460 prandom_seed_state(&rnd_state, 5);
Akinobu Mitaf54d6332009-10-09 18:43:52 +0900461
462 err = write_whole_device();
463 if (err)
464 goto out;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200465
466 /* Check all eraseblocks */
467 use_offset = 0;
468 use_len = mtd->ecclayout->oobavail;
469 use_len_max = mtd->ecclayout->oobavail;
470 vary_offset = 1;
Akinobu Mita8dad0492013-02-27 17:05:33 -0800471 prandom_seed_state(&rnd_state, 5);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200472 err = verify_all_eraseblocks();
473 if (err)
474 goto out;
475
476 use_offset = 0;
477 use_len = mtd->ecclayout->oobavail;
478 use_len_max = mtd->ecclayout->oobavail;
479 vary_offset = 0;
480
481 /* Fourth test: try to write off end of device */
Vikram Narayanan04810272012-10-10 23:12:02 +0530482 pr_info("test 4 of 5\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200483
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900484 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200485 if (err)
486 goto out;
487
488 addr0 = 0;
Roel Kluinc6f7e7b2009-07-31 16:21:01 +0200489 for (i = 0; i < ebcnt && bbt[i]; ++i)
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200490 addr0 += mtd->erasesize;
491
492 /* Attempt to write off end of OOB */
Brian Norris0612b9d2011-08-30 18:45:40 -0700493 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200494 ops.len = 0;
495 ops.retlen = 0;
496 ops.ooblen = 1;
497 ops.oobretlen = 0;
498 ops.ooboffs = mtd->ecclayout->oobavail;
Hannes Eder23d42492009-03-05 20:15:01 +0100499 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200500 ops.oobbuf = writebuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530501 pr_info("attempting to start write past end of OOB\n");
502 pr_info("an error is expected...\n");
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200503 err = mtd_write_oob(mtd, addr0, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200504 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530505 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200506 err = 0;
507 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530508 pr_err("error: can write past end of OOB\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200509 errcnt += 1;
510 }
511
512 /* Attempt to read off end of OOB */
Brian Norris0612b9d2011-08-30 18:45:40 -0700513 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200514 ops.len = 0;
515 ops.retlen = 0;
516 ops.ooblen = 1;
517 ops.oobretlen = 0;
518 ops.ooboffs = mtd->ecclayout->oobavail;
Hannes Eder23d42492009-03-05 20:15:01 +0100519 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200520 ops.oobbuf = readbuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530521 pr_info("attempting to start read past end of OOB\n");
522 pr_info("an error is expected...\n");
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200523 err = mtd_read_oob(mtd, addr0, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200524 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530525 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200526 err = 0;
527 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530528 pr_err("error: can read past end of OOB\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200529 errcnt += 1;
530 }
531
532 if (bbt[ebcnt - 1])
Vikram Narayanan04810272012-10-10 23:12:02 +0530533 pr_info("skipping end of device tests because last "
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200534 "block is bad\n");
535 else {
536 /* Attempt to write off end of device */
Brian Norris0612b9d2011-08-30 18:45:40 -0700537 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200538 ops.len = 0;
539 ops.retlen = 0;
540 ops.ooblen = mtd->ecclayout->oobavail + 1;
541 ops.oobretlen = 0;
542 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100543 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200544 ops.oobbuf = writebuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530545 pr_info("attempting to write past end of device\n");
546 pr_info("an error is expected...\n");
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200547 err = mtd_write_oob(mtd, mtd->size - mtd->writesize, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200548 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530549 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200550 err = 0;
551 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530552 pr_err("error: wrote past end of device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200553 errcnt += 1;
554 }
555
556 /* Attempt to read off end of device */
Brian Norris0612b9d2011-08-30 18:45:40 -0700557 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200558 ops.len = 0;
559 ops.retlen = 0;
560 ops.ooblen = mtd->ecclayout->oobavail + 1;
561 ops.oobretlen = 0;
562 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100563 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200564 ops.oobbuf = readbuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530565 pr_info("attempting to read past end of device\n");
566 pr_info("an error is expected...\n");
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200567 err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200568 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530569 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200570 err = 0;
571 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530572 pr_err("error: read past end of device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200573 errcnt += 1;
574 }
575
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900576 err = mtdtest_erase_eraseblock(mtd, ebcnt - 1);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200577 if (err)
578 goto out;
579
580 /* Attempt to write off end of device */
Brian Norris0612b9d2011-08-30 18:45:40 -0700581 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200582 ops.len = 0;
583 ops.retlen = 0;
584 ops.ooblen = mtd->ecclayout->oobavail;
585 ops.oobretlen = 0;
586 ops.ooboffs = 1;
Hannes Eder23d42492009-03-05 20:15:01 +0100587 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200588 ops.oobbuf = writebuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530589 pr_info("attempting to write past end of device\n");
590 pr_info("an error is expected...\n");
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200591 err = mtd_write_oob(mtd, mtd->size - mtd->writesize, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200592 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530593 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200594 err = 0;
595 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530596 pr_err("error: wrote past end of device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200597 errcnt += 1;
598 }
599
600 /* Attempt to read off end of device */
Brian Norris0612b9d2011-08-30 18:45:40 -0700601 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200602 ops.len = 0;
603 ops.retlen = 0;
604 ops.ooblen = mtd->ecclayout->oobavail;
605 ops.oobretlen = 0;
606 ops.ooboffs = 1;
Hannes Eder23d42492009-03-05 20:15:01 +0100607 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200608 ops.oobbuf = readbuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530609 pr_info("attempting to read past end of device\n");
610 pr_info("an error is expected...\n");
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200611 err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200612 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530613 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200614 err = 0;
615 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530616 pr_err("error: read past end of device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200617 errcnt += 1;
618 }
619 }
620
621 /* Fifth test: write / read across block boundaries */
Vikram Narayanan04810272012-10-10 23:12:02 +0530622 pr_info("test 5 of 5\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200623
624 /* Erase all eraseblocks */
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900625 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200626 if (err)
627 goto out;
628
629 /* Write all eraseblocks */
Akinobu Mita8dad0492013-02-27 17:05:33 -0800630 prandom_seed_state(&rnd_state, 11);
Vikram Narayanan04810272012-10-10 23:12:02 +0530631 pr_info("writing OOBs of whole device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200632 for (i = 0; i < ebcnt - 1; ++i) {
633 int cnt = 2;
634 int pg;
635 size_t sz = mtd->ecclayout->oobavail;
636 if (bbt[i] || bbt[i + 1])
637 continue;
Brian Norris1001ff7a2014-07-21 19:07:12 -0700638 addr = (loff_t)(i + 1) * mtd->erasesize - mtd->writesize;
Akinobu Mitabe54f8f2014-03-08 00:24:10 +0900639 prandom_bytes_state(&rnd_state, writebuf, sz * cnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200640 for (pg = 0; pg < cnt; ++pg) {
Brian Norris0612b9d2011-08-30 18:45:40 -0700641 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200642 ops.len = 0;
643 ops.retlen = 0;
644 ops.ooblen = sz;
645 ops.oobretlen = 0;
646 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100647 ops.datbuf = NULL;
Akinobu Mitabe54f8f2014-03-08 00:24:10 +0900648 ops.oobbuf = writebuf + pg * sz;
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200649 err = mtd_write_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200650 if (err)
651 goto out;
652 if (i % 256 == 0)
Vikram Narayanan04810272012-10-10 23:12:02 +0530653 pr_info("written up to eraseblock %u\n", i);
Richard Weinberger2a6a28e72015-03-29 21:52:06 +0200654
655 err = mtdtest_relax();
656 if (err)
657 goto out;
658
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200659 addr += mtd->writesize;
660 }
661 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530662 pr_info("written %u eraseblocks\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200663
664 /* Check all eraseblocks */
Akinobu Mita8dad0492013-02-27 17:05:33 -0800665 prandom_seed_state(&rnd_state, 11);
Vikram Narayanan04810272012-10-10 23:12:02 +0530666 pr_info("verifying all eraseblocks\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200667 for (i = 0; i < ebcnt - 1; ++i) {
668 if (bbt[i] || bbt[i + 1])
669 continue;
Akinobu Mita8dad0492013-02-27 17:05:33 -0800670 prandom_bytes_state(&rnd_state, writebuf,
671 mtd->ecclayout->oobavail * 2);
Brian Norris1001ff7a2014-07-21 19:07:12 -0700672 addr = (loff_t)(i + 1) * mtd->erasesize - mtd->writesize;
Brian Norris0612b9d2011-08-30 18:45:40 -0700673 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200674 ops.len = 0;
675 ops.retlen = 0;
676 ops.ooblen = mtd->ecclayout->oobavail * 2;
677 ops.oobretlen = 0;
678 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100679 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200680 ops.oobbuf = readbuf;
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200681 err = mtd_read_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200682 if (err)
683 goto out;
Roger Quadros5a660882014-10-21 16:53:27 +0300684 if (memcmpshow(addr, readbuf, writebuf,
685 mtd->ecclayout->oobavail * 2)) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530686 pr_err("error: verify failed at %#llx\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200687 (long long)addr);
688 errcnt += 1;
689 if (errcnt > 1000) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530690 pr_err("error: too many errors\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200691 goto out;
692 }
693 }
694 if (i % 256 == 0)
Vikram Narayanan04810272012-10-10 23:12:02 +0530695 pr_info("verified up to eraseblock %u\n", i);
Richard Weinberger2a6a28e72015-03-29 21:52:06 +0200696
697 err = mtdtest_relax();
698 if (err)
699 goto out;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200700 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530701 pr_info("verified %u eraseblocks\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200702
Vikram Narayanan04810272012-10-10 23:12:02 +0530703 pr_info("finished with %d errors\n", errcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200704out:
705 kfree(bbt);
706 kfree(writebuf);
707 kfree(readbuf);
708 put_mtd_device(mtd);
709 if (err)
Vikram Narayanan04810272012-10-10 23:12:02 +0530710 pr_info("error %d occurred\n", err);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200711 printk(KERN_INFO "=================================================\n");
712 return err;
713}
714module_init(mtd_oobtest_init);
715
716static void __exit mtd_oobtest_exit(void)
717{
718 return;
719}
720module_exit(mtd_oobtest_exit);
721
722MODULE_DESCRIPTION("Out-of-band test module");
723MODULE_AUTHOR("Adrian Hunter");
724MODULE_LICENSE("GPL");