blob: c6b35644a10f13162fae8a06aca77773cae33718 [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);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200115 cond_resched();
116 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530117 pr_info("written %u eraseblocks\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200118 return 0;
119}
120
Roger Quadrosafc0ea12014-10-21 16:53:28 +0300121/*
122 * Display the address, offset and data bytes at comparison failure.
123 * Return number of bitflips encountered.
124 */
125static size_t memcmpshow(loff_t addr, const void *cs, const void *ct, size_t count)
Roger Quadros5a660882014-10-21 16:53:27 +0300126{
127 const unsigned char *su1, *su2;
128 int res;
Roger Quadros5a660882014-10-21 16:53:27 +0300129 size_t i = 0;
Roger Quadrosafc0ea12014-10-21 16:53:28 +0300130 size_t bitflips = 0;
Roger Quadros5a660882014-10-21 16:53:27 +0300131
132 for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--, i++) {
133 res = *su1 ^ *su2;
134 if (res) {
Brian Norris806b6ef2014-11-20 01:26:51 -0800135 pr_info("error @addr[0x%lx:0x%zx] 0x%x -> 0x%x diff 0x%x\n",
Roger Quadros5a660882014-10-21 16:53:27 +0300136 (unsigned long)addr, i, *su1, *su2, res);
Roger Quadrosafc0ea12014-10-21 16:53:28 +0300137 bitflips += hweight8(res);
Roger Quadros5a660882014-10-21 16:53:27 +0300138 }
139 }
140
Roger Quadrosafc0ea12014-10-21 16:53:28 +0300141 return bitflips;
Roger Quadros5a660882014-10-21 16:53:27 +0300142}
143
Roger Quadrosd2b51c82014-12-05 17:18:39 +0200144/*
145 * Compare with 0xff and show the address, offset and data bytes at
146 * comparison failure. Return number of bitflips encountered.
147 */
148static size_t memffshow(loff_t addr, loff_t offset, const void *cs,
149 size_t count)
150{
151 const unsigned char *su1;
152 int res;
153 size_t i = 0;
154 size_t bitflips = 0;
155
156 for (su1 = cs; 0 < count; ++su1, count--, i++) {
157 res = *su1 ^ 0xff;
158 if (res) {
159 pr_info("error @addr[0x%lx:0x%lx] 0x%x -> 0xff diff 0x%x\n",
160 (unsigned long)addr, (unsigned long)offset + i,
161 *su1, res);
162 bitflips += hweight8(res);
163 }
164 }
165
166 return bitflips;
167}
168
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200169static int verify_eraseblock(int ebnum)
170{
171 int i;
172 struct mtd_oob_ops ops;
173 int err = 0;
Brian Norris1001ff72014-07-21 19:07:12 -0700174 loff_t addr = (loff_t)ebnum * mtd->erasesize;
Roger Quadrosafc0ea12014-10-21 16:53:28 +0300175 size_t bitflips;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200176
Akinobu Mitabe54f8f2014-03-08 00:24:10 +0900177 prandom_bytes_state(&rnd_state, writebuf, use_len_max * pgcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200178 for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
Brian Norris0612b9d2011-08-30 18:45:40 -0700179 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200180 ops.len = 0;
181 ops.retlen = 0;
182 ops.ooblen = use_len;
183 ops.oobretlen = 0;
184 ops.ooboffs = use_offset;
Hannes Eder23d42492009-03-05 20:15:01 +0100185 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200186 ops.oobbuf = readbuf;
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200187 err = mtd_read_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200188 if (err || ops.oobretlen != use_len) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530189 pr_err("error: readoob failed at %#llx\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200190 (long long)addr);
191 errcnt += 1;
192 return err ? err : -1;
193 }
Roger Quadrosafc0ea12014-10-21 16:53:28 +0300194
195 bitflips = memcmpshow(addr, readbuf,
196 writebuf + (use_len_max * i) + use_offset,
197 use_len);
198 if (bitflips > bitflip_limit) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530199 pr_err("error: verify failed at %#llx\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200200 (long long)addr);
201 errcnt += 1;
202 if (errcnt > 1000) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530203 pr_err("error: too many errors\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200204 return -1;
205 }
Roger Quadrosafc0ea12014-10-21 16:53:28 +0300206 } else if (bitflips) {
207 pr_info("ignoring error as within bitflip_limit\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200208 }
Roger Quadrosafc0ea12014-10-21 16:53:28 +0300209
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200210 if (use_offset != 0 || use_len < mtd->ecclayout->oobavail) {
211 int k;
212
Brian Norris0612b9d2011-08-30 18:45:40 -0700213 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200214 ops.len = 0;
215 ops.retlen = 0;
216 ops.ooblen = mtd->ecclayout->oobavail;
217 ops.oobretlen = 0;
218 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100219 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200220 ops.oobbuf = readbuf;
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200221 err = mtd_read_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200222 if (err || ops.oobretlen != mtd->ecclayout->oobavail) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530223 pr_err("error: readoob failed at %#llx\n",
224 (long long)addr);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200225 errcnt += 1;
226 return err ? err : -1;
227 }
Roger Quadrosafc0ea12014-10-21 16:53:28 +0300228 bitflips = memcmpshow(addr, readbuf + use_offset,
229 writebuf + (use_len_max * i) + use_offset,
230 use_len);
Roger Quadrosd2b51c82014-12-05 17:18:39 +0200231
232 /* verify pre-offset area for 0xff */
233 bitflips += memffshow(addr, 0, readbuf, use_offset);
234
235 /* verify post-(use_offset + use_len) area for 0xff */
236 k = use_offset + use_len;
237 bitflips += memffshow(addr, k, readbuf + k,
238 mtd->ecclayout->oobavail - k);
239
Roger Quadrosafc0ea12014-10-21 16:53:28 +0300240 if (bitflips > bitflip_limit) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530241 pr_err("error: verify failed at %#llx\n",
242 (long long)addr);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200243 errcnt += 1;
244 if (errcnt > 1000) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530245 pr_err("error: too many errors\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200246 return -1;
247 }
Roger Quadrosafc0ea12014-10-21 16:53:28 +0300248 } else if (bitflips) {
Roger Quadrosd2b51c82014-12-05 17:18:39 +0200249 pr_info("ignoring errors as within bitflip limit\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200250 }
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200251 }
252 if (vary_offset)
253 do_vary_offset();
254 }
255 return err;
256}
257
258static int verify_eraseblock_in_one_go(int ebnum)
259{
260 struct mtd_oob_ops ops;
261 int err = 0;
Brian Norris1001ff72014-07-21 19:07:12 -0700262 loff_t addr = (loff_t)ebnum * mtd->erasesize;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200263 size_t len = mtd->ecclayout->oobavail * pgcnt;
Roger Quadrosafc0ea12014-10-21 16:53:28 +0300264 size_t oobavail = mtd->ecclayout->oobavail;
265 size_t bitflips;
266 int i;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200267
Akinobu Mita8dad0492013-02-27 17:05:33 -0800268 prandom_bytes_state(&rnd_state, writebuf, len);
Brian Norris0612b9d2011-08-30 18:45:40 -0700269 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200270 ops.len = 0;
271 ops.retlen = 0;
272 ops.ooblen = len;
273 ops.oobretlen = 0;
274 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100275 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200276 ops.oobbuf = readbuf;
Roger Quadrosafc0ea12014-10-21 16:53:28 +0300277
278 /* read entire block's OOB at one go */
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200279 err = mtd_read_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200280 if (err || ops.oobretlen != len) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530281 pr_err("error: readoob failed at %#llx\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200282 (long long)addr);
283 errcnt += 1;
284 return err ? err : -1;
285 }
Roger Quadrosafc0ea12014-10-21 16:53:28 +0300286
287 /* verify one page OOB at a time for bitflip per page limit check */
288 for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
289 bitflips = memcmpshow(addr, readbuf + (i * oobavail),
290 writebuf + (i * oobavail), oobavail);
291 if (bitflips > bitflip_limit) {
292 pr_err("error: verify failed at %#llx\n",
293 (long long)addr);
294 errcnt += 1;
295 if (errcnt > 1000) {
296 pr_err("error: too many errors\n");
297 return -1;
298 }
299 } else if (bitflips) {
300 pr_info("ignoring error as within bitflip_limit\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200301 }
302 }
303
304 return err;
305}
306
307static int verify_all_eraseblocks(void)
308{
309 int err;
310 unsigned int i;
311
Vikram Narayanan04810272012-10-10 23:12:02 +0530312 pr_info("verifying all eraseblocks\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200313 for (i = 0; i < ebcnt; ++i) {
314 if (bbt[i])
315 continue;
316 err = verify_eraseblock(i);
317 if (err)
318 return err;
319 if (i % 256 == 0)
Vikram Narayanan04810272012-10-10 23:12:02 +0530320 pr_info("verified up to eraseblock %u\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200321 cond_resched();
322 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530323 pr_info("verified %u eraseblocks\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200324 return 0;
325}
326
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200327static int __init mtd_oobtest_init(void)
328{
329 int err = 0;
330 unsigned int i;
331 uint64_t tmp;
332 struct mtd_oob_ops ops;
333 loff_t addr = 0, addr0;
334
335 printk(KERN_INFO "\n");
336 printk(KERN_INFO "=================================================\n");
Wolfram Sang74060602011-10-30 00:11:53 +0200337
338 if (dev < 0) {
Masanari Iida064a7692012-11-09 23:20:58 +0900339 pr_info("Please specify a valid mtd-device via module parameter\n");
340 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
Wolfram Sang74060602011-10-30 00:11:53 +0200341 return -EINVAL;
342 }
343
Vikram Narayanan04810272012-10-10 23:12:02 +0530344 pr_info("MTD device: %d\n", dev);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200345
346 mtd = get_mtd_device(NULL, dev);
347 if (IS_ERR(mtd)) {
348 err = PTR_ERR(mtd);
Vikram Narayanan04810272012-10-10 23:12:02 +0530349 pr_err("error: cannot get MTD device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200350 return err;
351 }
352
Huang Shijie818b9732013-09-25 14:58:17 +0800353 if (!mtd_type_is_nand(mtd)) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530354 pr_info("this test requires NAND flash\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200355 goto out;
356 }
357
358 tmp = mtd->size;
359 do_div(tmp, mtd->erasesize);
360 ebcnt = tmp;
361 pgcnt = mtd->erasesize / mtd->writesize;
362
Vikram Narayanan04810272012-10-10 23:12:02 +0530363 pr_info("MTD device size %llu, eraseblock size %u, "
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200364 "page size %u, count of eraseblocks %u, pages per "
365 "eraseblock %u, OOB size %u\n",
366 (unsigned long long)mtd->size, mtd->erasesize,
367 mtd->writesize, ebcnt, pgcnt, mtd->oobsize);
368
369 err = -ENOMEM;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200370 readbuf = kmalloc(mtd->erasesize, GFP_KERNEL);
Brian Norris33777e62013-05-02 14:18:51 -0700371 if (!readbuf)
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200372 goto out;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200373 writebuf = kmalloc(mtd->erasesize, GFP_KERNEL);
Brian Norris33777e62013-05-02 14:18:51 -0700374 if (!writebuf)
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200375 goto out;
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900376 bbt = kzalloc(ebcnt, GFP_KERNEL);
377 if (!bbt)
378 goto out;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200379
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900380 err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200381 if (err)
382 goto out;
383
384 use_offset = 0;
385 use_len = mtd->ecclayout->oobavail;
386 use_len_max = mtd->ecclayout->oobavail;
387 vary_offset = 0;
388
389 /* First test: write all OOB, read it back and verify */
Vikram Narayanan04810272012-10-10 23:12:02 +0530390 pr_info("test 1 of 5\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200391
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900392 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200393 if (err)
394 goto out;
395
Akinobu Mita8dad0492013-02-27 17:05:33 -0800396 prandom_seed_state(&rnd_state, 1);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200397 err = write_whole_device();
398 if (err)
399 goto out;
400
Akinobu Mita8dad0492013-02-27 17:05:33 -0800401 prandom_seed_state(&rnd_state, 1);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200402 err = verify_all_eraseblocks();
403 if (err)
404 goto out;
405
406 /*
407 * Second test: write all OOB, a block at a time, read it back and
408 * verify.
409 */
Vikram Narayanan04810272012-10-10 23:12:02 +0530410 pr_info("test 2 of 5\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200411
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900412 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200413 if (err)
414 goto out;
415
Akinobu Mita8dad0492013-02-27 17:05:33 -0800416 prandom_seed_state(&rnd_state, 3);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200417 err = write_whole_device();
418 if (err)
419 goto out;
420
421 /* Check all eraseblocks */
Akinobu Mita8dad0492013-02-27 17:05:33 -0800422 prandom_seed_state(&rnd_state, 3);
Vikram Narayanan04810272012-10-10 23:12:02 +0530423 pr_info("verifying all eraseblocks\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200424 for (i = 0; i < ebcnt; ++i) {
425 if (bbt[i])
426 continue;
427 err = verify_eraseblock_in_one_go(i);
428 if (err)
429 goto out;
430 if (i % 256 == 0)
Vikram Narayanan04810272012-10-10 23:12:02 +0530431 pr_info("verified up to eraseblock %u\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200432 cond_resched();
433 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530434 pr_info("verified %u eraseblocks\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200435
436 /*
437 * Third test: write OOB at varying offsets and lengths, read it back
438 * and verify.
439 */
Vikram Narayanan04810272012-10-10 23:12:02 +0530440 pr_info("test 3 of 5\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200441
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900442 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200443 if (err)
444 goto out;
445
446 /* Write all eraseblocks */
447 use_offset = 0;
448 use_len = mtd->ecclayout->oobavail;
449 use_len_max = mtd->ecclayout->oobavail;
450 vary_offset = 1;
Akinobu Mita8dad0492013-02-27 17:05:33 -0800451 prandom_seed_state(&rnd_state, 5);
Akinobu Mitaf54d6332009-10-09 18:43:52 +0900452
453 err = write_whole_device();
454 if (err)
455 goto out;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200456
457 /* Check all eraseblocks */
458 use_offset = 0;
459 use_len = mtd->ecclayout->oobavail;
460 use_len_max = mtd->ecclayout->oobavail;
461 vary_offset = 1;
Akinobu Mita8dad0492013-02-27 17:05:33 -0800462 prandom_seed_state(&rnd_state, 5);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200463 err = verify_all_eraseblocks();
464 if (err)
465 goto out;
466
467 use_offset = 0;
468 use_len = mtd->ecclayout->oobavail;
469 use_len_max = mtd->ecclayout->oobavail;
470 vary_offset = 0;
471
472 /* Fourth test: try to write off end of device */
Vikram Narayanan04810272012-10-10 23:12:02 +0530473 pr_info("test 4 of 5\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200474
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900475 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200476 if (err)
477 goto out;
478
479 addr0 = 0;
Roel Kluinc6f7e7b2009-07-31 16:21:01 +0200480 for (i = 0; i < ebcnt && bbt[i]; ++i)
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200481 addr0 += mtd->erasesize;
482
483 /* Attempt to write off end of OOB */
Brian Norris0612b9d2011-08-30 18:45:40 -0700484 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200485 ops.len = 0;
486 ops.retlen = 0;
487 ops.ooblen = 1;
488 ops.oobretlen = 0;
489 ops.ooboffs = mtd->ecclayout->oobavail;
Hannes Eder23d42492009-03-05 20:15:01 +0100490 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200491 ops.oobbuf = writebuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530492 pr_info("attempting to start write past end of OOB\n");
493 pr_info("an error is expected...\n");
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200494 err = mtd_write_oob(mtd, addr0, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200495 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530496 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200497 err = 0;
498 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530499 pr_err("error: can write past end of OOB\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200500 errcnt += 1;
501 }
502
503 /* Attempt to read off end of OOB */
Brian Norris0612b9d2011-08-30 18:45:40 -0700504 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200505 ops.len = 0;
506 ops.retlen = 0;
507 ops.ooblen = 1;
508 ops.oobretlen = 0;
509 ops.ooboffs = mtd->ecclayout->oobavail;
Hannes Eder23d42492009-03-05 20:15:01 +0100510 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200511 ops.oobbuf = readbuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530512 pr_info("attempting to start read past end of OOB\n");
513 pr_info("an error is expected...\n");
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200514 err = mtd_read_oob(mtd, addr0, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200515 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530516 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200517 err = 0;
518 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530519 pr_err("error: can read past end of OOB\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200520 errcnt += 1;
521 }
522
523 if (bbt[ebcnt - 1])
Vikram Narayanan04810272012-10-10 23:12:02 +0530524 pr_info("skipping end of device tests because last "
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200525 "block is bad\n");
526 else {
527 /* Attempt to write off end of device */
Brian Norris0612b9d2011-08-30 18:45:40 -0700528 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200529 ops.len = 0;
530 ops.retlen = 0;
531 ops.ooblen = mtd->ecclayout->oobavail + 1;
532 ops.oobretlen = 0;
533 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100534 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200535 ops.oobbuf = writebuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530536 pr_info("attempting to write past end of device\n");
537 pr_info("an error is expected...\n");
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200538 err = mtd_write_oob(mtd, mtd->size - mtd->writesize, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200539 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530540 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200541 err = 0;
542 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530543 pr_err("error: wrote past end of device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200544 errcnt += 1;
545 }
546
547 /* Attempt to read off end of device */
Brian Norris0612b9d2011-08-30 18:45:40 -0700548 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200549 ops.len = 0;
550 ops.retlen = 0;
551 ops.ooblen = mtd->ecclayout->oobavail + 1;
552 ops.oobretlen = 0;
553 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100554 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200555 ops.oobbuf = readbuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530556 pr_info("attempting to read past end of device\n");
557 pr_info("an error is expected...\n");
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200558 err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200559 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530560 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200561 err = 0;
562 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530563 pr_err("error: read past end of device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200564 errcnt += 1;
565 }
566
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900567 err = mtdtest_erase_eraseblock(mtd, ebcnt - 1);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200568 if (err)
569 goto out;
570
571 /* Attempt to write off end of device */
Brian Norris0612b9d2011-08-30 18:45:40 -0700572 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200573 ops.len = 0;
574 ops.retlen = 0;
575 ops.ooblen = mtd->ecclayout->oobavail;
576 ops.oobretlen = 0;
577 ops.ooboffs = 1;
Hannes Eder23d42492009-03-05 20:15:01 +0100578 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200579 ops.oobbuf = writebuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530580 pr_info("attempting to write past end of device\n");
581 pr_info("an error is expected...\n");
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200582 err = mtd_write_oob(mtd, mtd->size - mtd->writesize, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200583 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530584 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200585 err = 0;
586 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530587 pr_err("error: wrote past end of device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200588 errcnt += 1;
589 }
590
591 /* Attempt to read off end of device */
Brian Norris0612b9d2011-08-30 18:45:40 -0700592 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200593 ops.len = 0;
594 ops.retlen = 0;
595 ops.ooblen = mtd->ecclayout->oobavail;
596 ops.oobretlen = 0;
597 ops.ooboffs = 1;
Hannes Eder23d42492009-03-05 20:15:01 +0100598 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200599 ops.oobbuf = readbuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530600 pr_info("attempting to read past end of device\n");
601 pr_info("an error is expected...\n");
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200602 err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200603 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530604 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200605 err = 0;
606 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530607 pr_err("error: read past end of device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200608 errcnt += 1;
609 }
610 }
611
612 /* Fifth test: write / read across block boundaries */
Vikram Narayanan04810272012-10-10 23:12:02 +0530613 pr_info("test 5 of 5\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200614
615 /* Erase all eraseblocks */
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900616 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200617 if (err)
618 goto out;
619
620 /* Write all eraseblocks */
Akinobu Mita8dad0492013-02-27 17:05:33 -0800621 prandom_seed_state(&rnd_state, 11);
Vikram Narayanan04810272012-10-10 23:12:02 +0530622 pr_info("writing OOBs of whole device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200623 for (i = 0; i < ebcnt - 1; ++i) {
624 int cnt = 2;
625 int pg;
626 size_t sz = mtd->ecclayout->oobavail;
627 if (bbt[i] || bbt[i + 1])
628 continue;
Brian Norris1001ff72014-07-21 19:07:12 -0700629 addr = (loff_t)(i + 1) * mtd->erasesize - mtd->writesize;
Akinobu Mitabe54f8f2014-03-08 00:24:10 +0900630 prandom_bytes_state(&rnd_state, writebuf, sz * cnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200631 for (pg = 0; pg < cnt; ++pg) {
Brian Norris0612b9d2011-08-30 18:45:40 -0700632 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200633 ops.len = 0;
634 ops.retlen = 0;
635 ops.ooblen = sz;
636 ops.oobretlen = 0;
637 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100638 ops.datbuf = NULL;
Akinobu Mitabe54f8f2014-03-08 00:24:10 +0900639 ops.oobbuf = writebuf + pg * sz;
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200640 err = mtd_write_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200641 if (err)
642 goto out;
643 if (i % 256 == 0)
Vikram Narayanan04810272012-10-10 23:12:02 +0530644 pr_info("written up to eraseblock %u\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200645 cond_resched();
646 addr += mtd->writesize;
647 }
648 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530649 pr_info("written %u eraseblocks\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200650
651 /* Check all eraseblocks */
Akinobu Mita8dad0492013-02-27 17:05:33 -0800652 prandom_seed_state(&rnd_state, 11);
Vikram Narayanan04810272012-10-10 23:12:02 +0530653 pr_info("verifying all eraseblocks\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200654 for (i = 0; i < ebcnt - 1; ++i) {
655 if (bbt[i] || bbt[i + 1])
656 continue;
Akinobu Mita8dad0492013-02-27 17:05:33 -0800657 prandom_bytes_state(&rnd_state, writebuf,
658 mtd->ecclayout->oobavail * 2);
Brian Norris1001ff72014-07-21 19:07:12 -0700659 addr = (loff_t)(i + 1) * mtd->erasesize - mtd->writesize;
Brian Norris0612b9d2011-08-30 18:45:40 -0700660 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200661 ops.len = 0;
662 ops.retlen = 0;
663 ops.ooblen = mtd->ecclayout->oobavail * 2;
664 ops.oobretlen = 0;
665 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100666 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200667 ops.oobbuf = readbuf;
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200668 err = mtd_read_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200669 if (err)
670 goto out;
Roger Quadros5a660882014-10-21 16:53:27 +0300671 if (memcmpshow(addr, readbuf, writebuf,
672 mtd->ecclayout->oobavail * 2)) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530673 pr_err("error: verify failed at %#llx\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200674 (long long)addr);
675 errcnt += 1;
676 if (errcnt > 1000) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530677 pr_err("error: too many errors\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200678 goto out;
679 }
680 }
681 if (i % 256 == 0)
Vikram Narayanan04810272012-10-10 23:12:02 +0530682 pr_info("verified up to eraseblock %u\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200683 cond_resched();
684 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530685 pr_info("verified %u eraseblocks\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200686
Vikram Narayanan04810272012-10-10 23:12:02 +0530687 pr_info("finished with %d errors\n", errcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200688out:
689 kfree(bbt);
690 kfree(writebuf);
691 kfree(readbuf);
692 put_mtd_device(mtd);
693 if (err)
Vikram Narayanan04810272012-10-10 23:12:02 +0530694 pr_info("error %d occurred\n", err);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200695 printk(KERN_INFO "=================================================\n");
696 return err;
697}
698module_init(mtd_oobtest_init);
699
700static void __exit mtd_oobtest_exit(void)
701{
702 return;
703}
704module_exit(mtd_oobtest_exit);
705
706MODULE_DESCRIPTION("Out-of-band test module");
707MODULE_AUTHOR("Adrian Hunter");
708MODULE_LICENSE("GPL");