blob: e86bb29419a21fe343eda838bc79c74cb660f73f [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>
32
Wolfram Sang74060602011-10-30 00:11:53 +020033static int dev = -EINVAL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +020034module_param(dev, int, S_IRUGO);
35MODULE_PARM_DESC(dev, "MTD device number to use");
36
37static struct mtd_info *mtd;
38static unsigned char *readbuf;
39static unsigned char *writebuf;
40static unsigned char *bbt;
41
42static int ebcnt;
43static int pgcnt;
44static int errcnt;
45static int use_offset;
46static int use_len;
47static int use_len_max;
48static int vary_offset;
49static unsigned long next = 1;
50
51static inline unsigned int simple_rand(void)
52{
53 next = next * 1103515245 + 12345;
54 return (unsigned int)((next / 65536) % 32768);
55}
56
57static inline void simple_srand(unsigned long seed)
58{
59 next = seed;
60}
61
62static void set_random_data(unsigned char *buf, size_t len)
63{
64 size_t i;
65
66 for (i = 0; i < len; ++i)
67 buf[i] = simple_rand();
68}
69
70static int erase_eraseblock(int ebnum)
71{
72 int err;
73 struct erase_info ei;
74 loff_t addr = ebnum * mtd->erasesize;
75
76 memset(&ei, 0, sizeof(struct erase_info));
77 ei.mtd = mtd;
78 ei.addr = addr;
79 ei.len = mtd->erasesize;
80
Artem Bityutskiy7e1f0dc2011-12-23 15:25:39 +020081 err = mtd_erase(mtd, &ei);
Artem Bityutskiye3644da2008-12-08 13:33:29 +020082 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +053083 pr_err("error %d while erasing EB %d\n", err, ebnum);
Artem Bityutskiye3644da2008-12-08 13:33:29 +020084 return err;
85 }
86
87 if (ei.state == MTD_ERASE_FAILED) {
Vikram Narayanan04810272012-10-10 23:12:02 +053088 pr_err("some erase error occurred at EB %d\n", ebnum);
Artem Bityutskiye3644da2008-12-08 13:33:29 +020089 return -EIO;
90 }
91
92 return 0;
93}
94
95static int erase_whole_device(void)
96{
97 int err;
98 unsigned int i;
99
Vikram Narayanan04810272012-10-10 23:12:02 +0530100 pr_info("erasing whole device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200101 for (i = 0; i < ebcnt; ++i) {
102 if (bbt[i])
103 continue;
104 err = erase_eraseblock(i);
105 if (err)
106 return err;
107 cond_resched();
108 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530109 pr_info("erased %u eraseblocks\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200110 return 0;
111}
112
113static void do_vary_offset(void)
114{
115 use_len -= 1;
116 if (use_len < 1) {
117 use_offset += 1;
118 if (use_offset >= use_len_max)
119 use_offset = 0;
120 use_len = use_len_max - use_offset;
121 }
122}
123
124static int write_eraseblock(int ebnum)
125{
126 int i;
127 struct mtd_oob_ops ops;
128 int err = 0;
129 loff_t addr = ebnum * mtd->erasesize;
130
131 for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
132 set_random_data(writebuf, use_len);
Brian Norris0612b9d2011-08-30 18:45:40 -0700133 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200134 ops.len = 0;
135 ops.retlen = 0;
136 ops.ooblen = use_len;
137 ops.oobretlen = 0;
138 ops.ooboffs = use_offset;
Hannes Eder23d42492009-03-05 20:15:01 +0100139 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200140 ops.oobbuf = writebuf;
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200141 err = mtd_write_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200142 if (err || ops.oobretlen != use_len) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530143 pr_err("error: writeoob failed at %#llx\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200144 (long long)addr);
Vikram Narayanan04810272012-10-10 23:12:02 +0530145 pr_err("error: use_len %d, use_offset %d\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200146 use_len, use_offset);
147 errcnt += 1;
148 return err ? err : -1;
149 }
150 if (vary_offset)
151 do_vary_offset();
152 }
153
154 return err;
155}
156
157static int write_whole_device(void)
158{
159 int err;
160 unsigned int i;
161
Vikram Narayanan04810272012-10-10 23:12:02 +0530162 pr_info("writing OOBs of whole device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200163 for (i = 0; i < ebcnt; ++i) {
164 if (bbt[i])
165 continue;
166 err = write_eraseblock(i);
167 if (err)
168 return err;
169 if (i % 256 == 0)
Vikram Narayanan04810272012-10-10 23:12:02 +0530170 pr_info("written up to eraseblock %u\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200171 cond_resched();
172 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530173 pr_info("written %u eraseblocks\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200174 return 0;
175}
176
177static int verify_eraseblock(int ebnum)
178{
179 int i;
180 struct mtd_oob_ops ops;
181 int err = 0;
182 loff_t addr = ebnum * mtd->erasesize;
183
184 for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
185 set_random_data(writebuf, use_len);
Brian Norris0612b9d2011-08-30 18:45:40 -0700186 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200187 ops.len = 0;
188 ops.retlen = 0;
189 ops.ooblen = use_len;
190 ops.oobretlen = 0;
191 ops.ooboffs = use_offset;
Hannes Eder23d42492009-03-05 20:15:01 +0100192 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200193 ops.oobbuf = readbuf;
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200194 err = mtd_read_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200195 if (err || ops.oobretlen != use_len) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530196 pr_err("error: readoob failed at %#llx\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200197 (long long)addr);
198 errcnt += 1;
199 return err ? err : -1;
200 }
201 if (memcmp(readbuf, writebuf, use_len)) {
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 }
209 }
210 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 }
228 if (memcmp(readbuf + use_offset, writebuf, use_len)) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530229 pr_err("error: verify failed at %#llx\n",
230 (long long)addr);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200231 errcnt += 1;
232 if (errcnt > 1000) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530233 pr_err("error: too many errors\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200234 return -1;
235 }
236 }
237 for (k = 0; k < use_offset; ++k)
238 if (readbuf[k] != 0xff) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530239 pr_err("error: verify 0xff "
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200240 "failed at %#llx\n",
241 (long long)addr);
242 errcnt += 1;
243 if (errcnt > 1000) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530244 pr_err("error: too "
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200245 "many errors\n");
246 return -1;
247 }
248 }
249 for (k = use_offset + use_len;
250 k < mtd->ecclayout->oobavail; ++k)
251 if (readbuf[k] != 0xff) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530252 pr_err("error: verify 0xff "
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200253 "failed at %#llx\n",
254 (long long)addr);
255 errcnt += 1;
256 if (errcnt > 1000) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530257 pr_err("error: too "
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200258 "many errors\n");
259 return -1;
260 }
261 }
262 }
263 if (vary_offset)
264 do_vary_offset();
265 }
266 return err;
267}
268
269static int verify_eraseblock_in_one_go(int ebnum)
270{
271 struct mtd_oob_ops ops;
272 int err = 0;
273 loff_t addr = ebnum * mtd->erasesize;
274 size_t len = mtd->ecclayout->oobavail * pgcnt;
275
276 set_random_data(writebuf, len);
Brian Norris0612b9d2011-08-30 18:45:40 -0700277 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200278 ops.len = 0;
279 ops.retlen = 0;
280 ops.ooblen = len;
281 ops.oobretlen = 0;
282 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100283 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200284 ops.oobbuf = readbuf;
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200285 err = mtd_read_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200286 if (err || ops.oobretlen != len) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530287 pr_err("error: readoob failed at %#llx\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200288 (long long)addr);
289 errcnt += 1;
290 return err ? err : -1;
291 }
292 if (memcmp(readbuf, writebuf, len)) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530293 pr_err("error: verify failed at %#llx\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200294 (long long)addr);
295 errcnt += 1;
296 if (errcnt > 1000) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530297 pr_err("error: too many errors\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200298 return -1;
299 }
300 }
301
302 return err;
303}
304
305static int verify_all_eraseblocks(void)
306{
307 int err;
308 unsigned int i;
309
Vikram Narayanan04810272012-10-10 23:12:02 +0530310 pr_info("verifying all eraseblocks\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200311 for (i = 0; i < ebcnt; ++i) {
312 if (bbt[i])
313 continue;
314 err = verify_eraseblock(i);
315 if (err)
316 return err;
317 if (i % 256 == 0)
Vikram Narayanan04810272012-10-10 23:12:02 +0530318 pr_info("verified up to eraseblock %u\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200319 cond_resched();
320 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530321 pr_info("verified %u eraseblocks\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200322 return 0;
323}
324
325static int is_block_bad(int ebnum)
326{
327 int ret;
328 loff_t addr = ebnum * mtd->erasesize;
329
Artem Bityutskiy7086c192011-12-23 19:35:30 +0200330 ret = mtd_block_isbad(mtd, addr);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200331 if (ret)
Vikram Narayanan04810272012-10-10 23:12:02 +0530332 pr_info("block %d is bad\n", ebnum);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200333 return ret;
334}
335
336static int scan_for_bad_eraseblocks(void)
337{
338 int i, bad = 0;
339
340 bbt = kmalloc(ebcnt, GFP_KERNEL);
341 if (!bbt) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530342 pr_err("error: cannot allocate memory\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200343 return -ENOMEM;
344 }
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200345
Vikram Narayanan04810272012-10-10 23:12:02 +0530346 pr_info("scanning for bad eraseblocks\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200347 for (i = 0; i < ebcnt; ++i) {
348 bbt[i] = is_block_bad(i) ? 1 : 0;
349 if (bbt[i])
350 bad += 1;
351 cond_resched();
352 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530353 pr_info("scanned %d eraseblocks, %d are bad\n", i, bad);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200354 return 0;
355}
356
357static int __init mtd_oobtest_init(void)
358{
359 int err = 0;
360 unsigned int i;
361 uint64_t tmp;
362 struct mtd_oob_ops ops;
363 loff_t addr = 0, addr0;
364
365 printk(KERN_INFO "\n");
366 printk(KERN_INFO "=================================================\n");
Wolfram Sang74060602011-10-30 00:11:53 +0200367
368 if (dev < 0) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530369 pr_info("Please specify a valid mtd-device via module paramter\n");
370 pr_crit(KERN_CRIT "CAREFUL: This test wipes all data on the specified MTD device!\n");
Wolfram Sang74060602011-10-30 00:11:53 +0200371 return -EINVAL;
372 }
373
Vikram Narayanan04810272012-10-10 23:12:02 +0530374 pr_info("MTD device: %d\n", dev);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200375
376 mtd = get_mtd_device(NULL, dev);
377 if (IS_ERR(mtd)) {
378 err = PTR_ERR(mtd);
Vikram Narayanan04810272012-10-10 23:12:02 +0530379 pr_err("error: cannot get MTD device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200380 return err;
381 }
382
383 if (mtd->type != MTD_NANDFLASH) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530384 pr_info("this test requires NAND flash\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200385 goto out;
386 }
387
388 tmp = mtd->size;
389 do_div(tmp, mtd->erasesize);
390 ebcnt = tmp;
391 pgcnt = mtd->erasesize / mtd->writesize;
392
Vikram Narayanan04810272012-10-10 23:12:02 +0530393 pr_info("MTD device size %llu, eraseblock size %u, "
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200394 "page size %u, count of eraseblocks %u, pages per "
395 "eraseblock %u, OOB size %u\n",
396 (unsigned long long)mtd->size, mtd->erasesize,
397 mtd->writesize, ebcnt, pgcnt, mtd->oobsize);
398
399 err = -ENOMEM;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200400 readbuf = kmalloc(mtd->erasesize, GFP_KERNEL);
401 if (!readbuf) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530402 pr_err("error: cannot allocate memory\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200403 goto out;
404 }
405 writebuf = kmalloc(mtd->erasesize, GFP_KERNEL);
406 if (!writebuf) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530407 pr_err("error: cannot allocate memory\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200408 goto out;
409 }
410
411 err = scan_for_bad_eraseblocks();
412 if (err)
413 goto out;
414
415 use_offset = 0;
416 use_len = mtd->ecclayout->oobavail;
417 use_len_max = mtd->ecclayout->oobavail;
418 vary_offset = 0;
419
420 /* First test: write all OOB, read it back and verify */
Vikram Narayanan04810272012-10-10 23:12:02 +0530421 pr_info("test 1 of 5\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200422
423 err = erase_whole_device();
424 if (err)
425 goto out;
426
427 simple_srand(1);
428 err = write_whole_device();
429 if (err)
430 goto out;
431
432 simple_srand(1);
433 err = verify_all_eraseblocks();
434 if (err)
435 goto out;
436
437 /*
438 * Second test: write all OOB, a block at a time, read it back and
439 * verify.
440 */
Vikram Narayanan04810272012-10-10 23:12:02 +0530441 pr_info("test 2 of 5\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200442
443 err = erase_whole_device();
444 if (err)
445 goto out;
446
447 simple_srand(3);
448 err = write_whole_device();
449 if (err)
450 goto out;
451
452 /* Check all eraseblocks */
453 simple_srand(3);
Vikram Narayanan04810272012-10-10 23:12:02 +0530454 pr_info("verifying all eraseblocks\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200455 for (i = 0; i < ebcnt; ++i) {
456 if (bbt[i])
457 continue;
458 err = verify_eraseblock_in_one_go(i);
459 if (err)
460 goto out;
461 if (i % 256 == 0)
Vikram Narayanan04810272012-10-10 23:12:02 +0530462 pr_info("verified up to eraseblock %u\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200463 cond_resched();
464 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530465 pr_info("verified %u eraseblocks\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200466
467 /*
468 * Third test: write OOB at varying offsets and lengths, read it back
469 * and verify.
470 */
Vikram Narayanan04810272012-10-10 23:12:02 +0530471 pr_info("test 3 of 5\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200472
473 err = erase_whole_device();
474 if (err)
475 goto out;
476
477 /* Write all eraseblocks */
478 use_offset = 0;
479 use_len = mtd->ecclayout->oobavail;
480 use_len_max = mtd->ecclayout->oobavail;
481 vary_offset = 1;
482 simple_srand(5);
Akinobu Mitaf54d6332009-10-09 18:43:52 +0900483
484 err = write_whole_device();
485 if (err)
486 goto out;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200487
488 /* Check all eraseblocks */
489 use_offset = 0;
490 use_len = mtd->ecclayout->oobavail;
491 use_len_max = mtd->ecclayout->oobavail;
492 vary_offset = 1;
493 simple_srand(5);
494 err = verify_all_eraseblocks();
495 if (err)
496 goto out;
497
498 use_offset = 0;
499 use_len = mtd->ecclayout->oobavail;
500 use_len_max = mtd->ecclayout->oobavail;
501 vary_offset = 0;
502
503 /* Fourth test: try to write off end of device */
Vikram Narayanan04810272012-10-10 23:12:02 +0530504 pr_info("test 4 of 5\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200505
506 err = erase_whole_device();
507 if (err)
508 goto out;
509
510 addr0 = 0;
Roel Kluinc6f7e7b2009-07-31 16:21:01 +0200511 for (i = 0; i < ebcnt && bbt[i]; ++i)
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200512 addr0 += mtd->erasesize;
513
514 /* Attempt to write off end of OOB */
Brian Norris0612b9d2011-08-30 18:45:40 -0700515 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200516 ops.len = 0;
517 ops.retlen = 0;
518 ops.ooblen = 1;
519 ops.oobretlen = 0;
520 ops.ooboffs = mtd->ecclayout->oobavail;
Hannes Eder23d42492009-03-05 20:15:01 +0100521 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200522 ops.oobbuf = writebuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530523 pr_info("attempting to start write past end of OOB\n");
524 pr_info("an error is expected...\n");
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200525 err = mtd_write_oob(mtd, addr0, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200526 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530527 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200528 err = 0;
529 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530530 pr_err("error: can write past end of OOB\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200531 errcnt += 1;
532 }
533
534 /* Attempt to read off end of OOB */
Brian Norris0612b9d2011-08-30 18:45:40 -0700535 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200536 ops.len = 0;
537 ops.retlen = 0;
538 ops.ooblen = 1;
539 ops.oobretlen = 0;
540 ops.ooboffs = mtd->ecclayout->oobavail;
Hannes Eder23d42492009-03-05 20:15:01 +0100541 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200542 ops.oobbuf = readbuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530543 pr_info("attempting to start read past end of OOB\n");
544 pr_info("an error is expected...\n");
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200545 err = mtd_read_oob(mtd, addr0, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200546 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530547 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200548 err = 0;
549 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530550 pr_err("error: can read past end of OOB\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200551 errcnt += 1;
552 }
553
554 if (bbt[ebcnt - 1])
Vikram Narayanan04810272012-10-10 23:12:02 +0530555 pr_info("skipping end of device tests because last "
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200556 "block is bad\n");
557 else {
558 /* Attempt to write off end of device */
Brian Norris0612b9d2011-08-30 18:45:40 -0700559 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200560 ops.len = 0;
561 ops.retlen = 0;
562 ops.ooblen = mtd->ecclayout->oobavail + 1;
563 ops.oobretlen = 0;
564 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100565 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200566 ops.oobbuf = writebuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530567 pr_info("attempting to write past end of device\n");
568 pr_info("an error is expected...\n");
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200569 err = mtd_write_oob(mtd, mtd->size - mtd->writesize, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200570 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530571 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200572 err = 0;
573 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530574 pr_err("error: wrote past end of device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200575 errcnt += 1;
576 }
577
578 /* Attempt to read off end of device */
Brian Norris0612b9d2011-08-30 18:45:40 -0700579 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200580 ops.len = 0;
581 ops.retlen = 0;
582 ops.ooblen = mtd->ecclayout->oobavail + 1;
583 ops.oobretlen = 0;
584 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100585 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200586 ops.oobbuf = readbuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530587 pr_info("attempting to read past end of device\n");
588 pr_info("an error is expected...\n");
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200589 err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200590 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530591 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200592 err = 0;
593 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530594 pr_err("error: read past end of device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200595 errcnt += 1;
596 }
597
598 err = erase_eraseblock(ebcnt - 1);
599 if (err)
600 goto out;
601
602 /* Attempt to write off end of device */
Brian Norris0612b9d2011-08-30 18:45:40 -0700603 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200604 ops.len = 0;
605 ops.retlen = 0;
606 ops.ooblen = mtd->ecclayout->oobavail;
607 ops.oobretlen = 0;
608 ops.ooboffs = 1;
Hannes Eder23d42492009-03-05 20:15:01 +0100609 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200610 ops.oobbuf = writebuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530611 pr_info("attempting to write past end of device\n");
612 pr_info("an error is expected...\n");
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200613 err = mtd_write_oob(mtd, mtd->size - mtd->writesize, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200614 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530615 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200616 err = 0;
617 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530618 pr_err("error: wrote past end of device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200619 errcnt += 1;
620 }
621
622 /* Attempt to read off end of device */
Brian Norris0612b9d2011-08-30 18:45:40 -0700623 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200624 ops.len = 0;
625 ops.retlen = 0;
626 ops.ooblen = mtd->ecclayout->oobavail;
627 ops.oobretlen = 0;
628 ops.ooboffs = 1;
Hannes Eder23d42492009-03-05 20:15:01 +0100629 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200630 ops.oobbuf = readbuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530631 pr_info("attempting to read past end of device\n");
632 pr_info("an error is expected...\n");
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200633 err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200634 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530635 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200636 err = 0;
637 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530638 pr_err("error: read past end of device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200639 errcnt += 1;
640 }
641 }
642
643 /* Fifth test: write / read across block boundaries */
Vikram Narayanan04810272012-10-10 23:12:02 +0530644 pr_info("test 5 of 5\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200645
646 /* Erase all eraseblocks */
647 err = erase_whole_device();
648 if (err)
649 goto out;
650
651 /* Write all eraseblocks */
652 simple_srand(11);
Vikram Narayanan04810272012-10-10 23:12:02 +0530653 pr_info("writing OOBs of whole device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200654 for (i = 0; i < ebcnt - 1; ++i) {
655 int cnt = 2;
656 int pg;
657 size_t sz = mtd->ecclayout->oobavail;
658 if (bbt[i] || bbt[i + 1])
659 continue;
660 addr = (i + 1) * mtd->erasesize - mtd->writesize;
661 for (pg = 0; pg < cnt; ++pg) {
662 set_random_data(writebuf, sz);
Brian Norris0612b9d2011-08-30 18:45:40 -0700663 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200664 ops.len = 0;
665 ops.retlen = 0;
666 ops.ooblen = sz;
667 ops.oobretlen = 0;
668 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100669 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200670 ops.oobbuf = writebuf;
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200671 err = mtd_write_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200672 if (err)
673 goto out;
674 if (i % 256 == 0)
Vikram Narayanan04810272012-10-10 23:12:02 +0530675 pr_info("written up to eraseblock %u\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200676 cond_resched();
677 addr += mtd->writesize;
678 }
679 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530680 pr_info("written %u eraseblocks\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200681
682 /* Check all eraseblocks */
683 simple_srand(11);
Vikram Narayanan04810272012-10-10 23:12:02 +0530684 pr_info("verifying all eraseblocks\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200685 for (i = 0; i < ebcnt - 1; ++i) {
686 if (bbt[i] || bbt[i + 1])
687 continue;
688 set_random_data(writebuf, mtd->ecclayout->oobavail * 2);
689 addr = (i + 1) * mtd->erasesize - mtd->writesize;
Brian Norris0612b9d2011-08-30 18:45:40 -0700690 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200691 ops.len = 0;
692 ops.retlen = 0;
693 ops.ooblen = mtd->ecclayout->oobavail * 2;
694 ops.oobretlen = 0;
695 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100696 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200697 ops.oobbuf = readbuf;
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200698 err = mtd_read_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200699 if (err)
700 goto out;
701 if (memcmp(readbuf, writebuf, mtd->ecclayout->oobavail * 2)) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530702 pr_err("error: verify failed at %#llx\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200703 (long long)addr);
704 errcnt += 1;
705 if (errcnt > 1000) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530706 pr_err("error: too many errors\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200707 goto out;
708 }
709 }
710 if (i % 256 == 0)
Vikram Narayanan04810272012-10-10 23:12:02 +0530711 pr_info("verified up to eraseblock %u\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200712 cond_resched();
713 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530714 pr_info("verified %u eraseblocks\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200715
Vikram Narayanan04810272012-10-10 23:12:02 +0530716 pr_info("finished with %d errors\n", errcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200717out:
718 kfree(bbt);
719 kfree(writebuf);
720 kfree(readbuf);
721 put_mtd_device(mtd);
722 if (err)
Vikram Narayanan04810272012-10-10 23:12:02 +0530723 pr_info("error %d occurred\n", err);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200724 printk(KERN_INFO "=================================================\n");
725 return err;
726}
727module_init(mtd_oobtest_init);
728
729static void __exit mtd_oobtest_exit(void)
730{
731 return;
732}
733module_exit(mtd_oobtest_exit);
734
735MODULE_DESCRIPTION("Out-of-band test module");
736MODULE_AUTHOR("Adrian Hunter");
737MODULE_LICENSE("GPL");