18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2006-2008 Nokia Corporation
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Test OOB read and write on MTD device.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <asm/div64.h>
138c2ecf20Sopenharmony_ci#include <linux/init.h>
148c2ecf20Sopenharmony_ci#include <linux/module.h>
158c2ecf20Sopenharmony_ci#include <linux/moduleparam.h>
168c2ecf20Sopenharmony_ci#include <linux/err.h>
178c2ecf20Sopenharmony_ci#include <linux/mtd/mtd.h>
188c2ecf20Sopenharmony_ci#include <linux/slab.h>
198c2ecf20Sopenharmony_ci#include <linux/sched.h>
208c2ecf20Sopenharmony_ci#include <linux/random.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#include "mtd_test.h"
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_cistatic int dev = -EINVAL;
258c2ecf20Sopenharmony_cistatic int bitflip_limit;
268c2ecf20Sopenharmony_cimodule_param(dev, int, S_IRUGO);
278c2ecf20Sopenharmony_ciMODULE_PARM_DESC(dev, "MTD device number to use");
288c2ecf20Sopenharmony_cimodule_param(bitflip_limit, int, S_IRUGO);
298c2ecf20Sopenharmony_ciMODULE_PARM_DESC(bitflip_limit, "Max. allowed bitflips per page");
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic struct mtd_info *mtd;
328c2ecf20Sopenharmony_cistatic unsigned char *readbuf;
338c2ecf20Sopenharmony_cistatic unsigned char *writebuf;
348c2ecf20Sopenharmony_cistatic unsigned char *bbt;
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistatic int ebcnt;
378c2ecf20Sopenharmony_cistatic int pgcnt;
388c2ecf20Sopenharmony_cistatic int errcnt;
398c2ecf20Sopenharmony_cistatic int use_offset;
408c2ecf20Sopenharmony_cistatic int use_len;
418c2ecf20Sopenharmony_cistatic int use_len_max;
428c2ecf20Sopenharmony_cistatic int vary_offset;
438c2ecf20Sopenharmony_cistatic struct rnd_state rnd_state;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic void do_vary_offset(void)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	use_len -= 1;
488c2ecf20Sopenharmony_ci	if (use_len < 1) {
498c2ecf20Sopenharmony_ci		use_offset += 1;
508c2ecf20Sopenharmony_ci		if (use_offset >= use_len_max)
518c2ecf20Sopenharmony_ci			use_offset = 0;
528c2ecf20Sopenharmony_ci		use_len = use_len_max - use_offset;
538c2ecf20Sopenharmony_ci	}
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic int write_eraseblock(int ebnum)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	int i;
598c2ecf20Sopenharmony_ci	struct mtd_oob_ops ops;
608c2ecf20Sopenharmony_ci	int err = 0;
618c2ecf20Sopenharmony_ci	loff_t addr = (loff_t)ebnum * mtd->erasesize;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	prandom_bytes_state(&rnd_state, writebuf, use_len_max * pgcnt);
648c2ecf20Sopenharmony_ci	for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
658c2ecf20Sopenharmony_ci		ops.mode      = MTD_OPS_AUTO_OOB;
668c2ecf20Sopenharmony_ci		ops.len       = 0;
678c2ecf20Sopenharmony_ci		ops.retlen    = 0;
688c2ecf20Sopenharmony_ci		ops.ooblen    = use_len;
698c2ecf20Sopenharmony_ci		ops.oobretlen = 0;
708c2ecf20Sopenharmony_ci		ops.ooboffs   = use_offset;
718c2ecf20Sopenharmony_ci		ops.datbuf    = NULL;
728c2ecf20Sopenharmony_ci		ops.oobbuf    = writebuf + (use_len_max * i) + use_offset;
738c2ecf20Sopenharmony_ci		err = mtd_write_oob(mtd, addr, &ops);
748c2ecf20Sopenharmony_ci		if (err || ops.oobretlen != use_len) {
758c2ecf20Sopenharmony_ci			pr_err("error: writeoob failed at %#llx\n",
768c2ecf20Sopenharmony_ci			       (long long)addr);
778c2ecf20Sopenharmony_ci			pr_err("error: use_len %d, use_offset %d\n",
788c2ecf20Sopenharmony_ci			       use_len, use_offset);
798c2ecf20Sopenharmony_ci			errcnt += 1;
808c2ecf20Sopenharmony_ci			return err ? err : -1;
818c2ecf20Sopenharmony_ci		}
828c2ecf20Sopenharmony_ci		if (vary_offset)
838c2ecf20Sopenharmony_ci			do_vary_offset();
848c2ecf20Sopenharmony_ci	}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	return err;
878c2ecf20Sopenharmony_ci}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_cistatic int write_whole_device(void)
908c2ecf20Sopenharmony_ci{
918c2ecf20Sopenharmony_ci	int err;
928c2ecf20Sopenharmony_ci	unsigned int i;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	pr_info("writing OOBs of whole device\n");
958c2ecf20Sopenharmony_ci	for (i = 0; i < ebcnt; ++i) {
968c2ecf20Sopenharmony_ci		if (bbt[i])
978c2ecf20Sopenharmony_ci			continue;
988c2ecf20Sopenharmony_ci		err = write_eraseblock(i);
998c2ecf20Sopenharmony_ci		if (err)
1008c2ecf20Sopenharmony_ci			return err;
1018c2ecf20Sopenharmony_ci		if (i % 256 == 0)
1028c2ecf20Sopenharmony_ci			pr_info("written up to eraseblock %u\n", i);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci		err = mtdtest_relax();
1058c2ecf20Sopenharmony_ci		if (err)
1068c2ecf20Sopenharmony_ci			return err;
1078c2ecf20Sopenharmony_ci	}
1088c2ecf20Sopenharmony_ci	pr_info("written %u eraseblocks\n", i);
1098c2ecf20Sopenharmony_ci	return 0;
1108c2ecf20Sopenharmony_ci}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci/*
1138c2ecf20Sopenharmony_ci * Display the address, offset and data bytes at comparison failure.
1148c2ecf20Sopenharmony_ci * Return number of bitflips encountered.
1158c2ecf20Sopenharmony_ci */
1168c2ecf20Sopenharmony_cistatic size_t memcmpshowoffset(loff_t addr, loff_t offset, const void *cs,
1178c2ecf20Sopenharmony_ci			       const void *ct, size_t count)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	const unsigned char *su1, *su2;
1208c2ecf20Sopenharmony_ci	int res;
1218c2ecf20Sopenharmony_ci	size_t i = 0;
1228c2ecf20Sopenharmony_ci	size_t bitflips = 0;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--, i++) {
1258c2ecf20Sopenharmony_ci		res = *su1 ^ *su2;
1268c2ecf20Sopenharmony_ci		if (res) {
1278c2ecf20Sopenharmony_ci			pr_info("error @addr[0x%lx:0x%lx] 0x%x -> 0x%x diff 0x%x\n",
1288c2ecf20Sopenharmony_ci				(unsigned long)addr, (unsigned long)offset + i,
1298c2ecf20Sopenharmony_ci				*su1, *su2, res);
1308c2ecf20Sopenharmony_ci			bitflips += hweight8(res);
1318c2ecf20Sopenharmony_ci		}
1328c2ecf20Sopenharmony_ci	}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	return bitflips;
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci#define memcmpshow(addr, cs, ct, count) memcmpshowoffset((addr), 0, (cs), (ct),\
1388c2ecf20Sopenharmony_ci							 (count))
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci/*
1418c2ecf20Sopenharmony_ci * Compare with 0xff and show the address, offset and data bytes at
1428c2ecf20Sopenharmony_ci * comparison failure. Return number of bitflips encountered.
1438c2ecf20Sopenharmony_ci */
1448c2ecf20Sopenharmony_cistatic size_t memffshow(loff_t addr, loff_t offset, const void *cs,
1458c2ecf20Sopenharmony_ci			size_t count)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	const unsigned char *su1;
1488c2ecf20Sopenharmony_ci	int res;
1498c2ecf20Sopenharmony_ci	size_t i = 0;
1508c2ecf20Sopenharmony_ci	size_t bitflips = 0;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	for (su1 = cs; 0 < count; ++su1, count--, i++) {
1538c2ecf20Sopenharmony_ci		res = *su1 ^ 0xff;
1548c2ecf20Sopenharmony_ci		if (res) {
1558c2ecf20Sopenharmony_ci			pr_info("error @addr[0x%lx:0x%lx] 0x%x -> 0xff diff 0x%x\n",
1568c2ecf20Sopenharmony_ci				(unsigned long)addr, (unsigned long)offset + i,
1578c2ecf20Sopenharmony_ci				*su1, res);
1588c2ecf20Sopenharmony_ci			bitflips += hweight8(res);
1598c2ecf20Sopenharmony_ci		}
1608c2ecf20Sopenharmony_ci	}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	return bitflips;
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_cistatic int verify_eraseblock(int ebnum)
1668c2ecf20Sopenharmony_ci{
1678c2ecf20Sopenharmony_ci	int i;
1688c2ecf20Sopenharmony_ci	struct mtd_oob_ops ops;
1698c2ecf20Sopenharmony_ci	int err = 0;
1708c2ecf20Sopenharmony_ci	loff_t addr = (loff_t)ebnum * mtd->erasesize;
1718c2ecf20Sopenharmony_ci	size_t bitflips;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	prandom_bytes_state(&rnd_state, writebuf, use_len_max * pgcnt);
1748c2ecf20Sopenharmony_ci	for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
1758c2ecf20Sopenharmony_ci		ops.mode      = MTD_OPS_AUTO_OOB;
1768c2ecf20Sopenharmony_ci		ops.len       = 0;
1778c2ecf20Sopenharmony_ci		ops.retlen    = 0;
1788c2ecf20Sopenharmony_ci		ops.ooblen    = use_len;
1798c2ecf20Sopenharmony_ci		ops.oobretlen = 0;
1808c2ecf20Sopenharmony_ci		ops.ooboffs   = use_offset;
1818c2ecf20Sopenharmony_ci		ops.datbuf    = NULL;
1828c2ecf20Sopenharmony_ci		ops.oobbuf    = readbuf;
1838c2ecf20Sopenharmony_ci		err = mtd_read_oob(mtd, addr, &ops);
1848c2ecf20Sopenharmony_ci		if (mtd_is_bitflip(err))
1858c2ecf20Sopenharmony_ci			err = 0;
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci		if (err || ops.oobretlen != use_len) {
1888c2ecf20Sopenharmony_ci			pr_err("error: readoob failed at %#llx\n",
1898c2ecf20Sopenharmony_ci			       (long long)addr);
1908c2ecf20Sopenharmony_ci			errcnt += 1;
1918c2ecf20Sopenharmony_ci			return err ? err : -1;
1928c2ecf20Sopenharmony_ci		}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci		bitflips = memcmpshow(addr, readbuf,
1958c2ecf20Sopenharmony_ci				      writebuf + (use_len_max * i) + use_offset,
1968c2ecf20Sopenharmony_ci				      use_len);
1978c2ecf20Sopenharmony_ci		if (bitflips > bitflip_limit) {
1988c2ecf20Sopenharmony_ci			pr_err("error: verify failed at %#llx\n",
1998c2ecf20Sopenharmony_ci			       (long long)addr);
2008c2ecf20Sopenharmony_ci			errcnt += 1;
2018c2ecf20Sopenharmony_ci			if (errcnt > 1000) {
2028c2ecf20Sopenharmony_ci				pr_err("error: too many errors\n");
2038c2ecf20Sopenharmony_ci				return -1;
2048c2ecf20Sopenharmony_ci			}
2058c2ecf20Sopenharmony_ci		} else if (bitflips) {
2068c2ecf20Sopenharmony_ci			pr_info("ignoring error as within bitflip_limit\n");
2078c2ecf20Sopenharmony_ci		}
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci		if (use_offset != 0 || use_len < mtd->oobavail) {
2108c2ecf20Sopenharmony_ci			int k;
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci			ops.mode      = MTD_OPS_AUTO_OOB;
2138c2ecf20Sopenharmony_ci			ops.len       = 0;
2148c2ecf20Sopenharmony_ci			ops.retlen    = 0;
2158c2ecf20Sopenharmony_ci			ops.ooblen    = mtd->oobavail;
2168c2ecf20Sopenharmony_ci			ops.oobretlen = 0;
2178c2ecf20Sopenharmony_ci			ops.ooboffs   = 0;
2188c2ecf20Sopenharmony_ci			ops.datbuf    = NULL;
2198c2ecf20Sopenharmony_ci			ops.oobbuf    = readbuf;
2208c2ecf20Sopenharmony_ci			err = mtd_read_oob(mtd, addr, &ops);
2218c2ecf20Sopenharmony_ci			if (mtd_is_bitflip(err))
2228c2ecf20Sopenharmony_ci				err = 0;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci			if (err || ops.oobretlen != mtd->oobavail) {
2258c2ecf20Sopenharmony_ci				pr_err("error: readoob failed at %#llx\n",
2268c2ecf20Sopenharmony_ci						(long long)addr);
2278c2ecf20Sopenharmony_ci				errcnt += 1;
2288c2ecf20Sopenharmony_ci				return err ? err : -1;
2298c2ecf20Sopenharmony_ci			}
2308c2ecf20Sopenharmony_ci			bitflips = memcmpshowoffset(addr, use_offset,
2318c2ecf20Sopenharmony_ci						    readbuf + use_offset,
2328c2ecf20Sopenharmony_ci						    writebuf + (use_len_max * i) + use_offset,
2338c2ecf20Sopenharmony_ci						    use_len);
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci			/* verify pre-offset area for 0xff */
2368c2ecf20Sopenharmony_ci			bitflips += memffshow(addr, 0, readbuf, use_offset);
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci			/* verify post-(use_offset + use_len) area for 0xff */
2398c2ecf20Sopenharmony_ci			k = use_offset + use_len;
2408c2ecf20Sopenharmony_ci			bitflips += memffshow(addr, k, readbuf + k,
2418c2ecf20Sopenharmony_ci					      mtd->oobavail - k);
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci			if (bitflips > bitflip_limit) {
2448c2ecf20Sopenharmony_ci				pr_err("error: verify failed at %#llx\n",
2458c2ecf20Sopenharmony_ci						(long long)addr);
2468c2ecf20Sopenharmony_ci				errcnt += 1;
2478c2ecf20Sopenharmony_ci				if (errcnt > 1000) {
2488c2ecf20Sopenharmony_ci					pr_err("error: too many errors\n");
2498c2ecf20Sopenharmony_ci					return -1;
2508c2ecf20Sopenharmony_ci				}
2518c2ecf20Sopenharmony_ci			} else if (bitflips) {
2528c2ecf20Sopenharmony_ci				pr_info("ignoring errors as within bitflip limit\n");
2538c2ecf20Sopenharmony_ci			}
2548c2ecf20Sopenharmony_ci		}
2558c2ecf20Sopenharmony_ci		if (vary_offset)
2568c2ecf20Sopenharmony_ci			do_vary_offset();
2578c2ecf20Sopenharmony_ci	}
2588c2ecf20Sopenharmony_ci	return err;
2598c2ecf20Sopenharmony_ci}
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_cistatic int verify_eraseblock_in_one_go(int ebnum)
2628c2ecf20Sopenharmony_ci{
2638c2ecf20Sopenharmony_ci	struct mtd_oob_ops ops;
2648c2ecf20Sopenharmony_ci	int err = 0;
2658c2ecf20Sopenharmony_ci	loff_t addr = (loff_t)ebnum * mtd->erasesize;
2668c2ecf20Sopenharmony_ci	size_t len = mtd->oobavail * pgcnt;
2678c2ecf20Sopenharmony_ci	size_t oobavail = mtd->oobavail;
2688c2ecf20Sopenharmony_ci	size_t bitflips;
2698c2ecf20Sopenharmony_ci	int i;
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	prandom_bytes_state(&rnd_state, writebuf, len);
2728c2ecf20Sopenharmony_ci	ops.mode      = MTD_OPS_AUTO_OOB;
2738c2ecf20Sopenharmony_ci	ops.len       = 0;
2748c2ecf20Sopenharmony_ci	ops.retlen    = 0;
2758c2ecf20Sopenharmony_ci	ops.ooblen    = len;
2768c2ecf20Sopenharmony_ci	ops.oobretlen = 0;
2778c2ecf20Sopenharmony_ci	ops.ooboffs   = 0;
2788c2ecf20Sopenharmony_ci	ops.datbuf    = NULL;
2798c2ecf20Sopenharmony_ci	ops.oobbuf    = readbuf;
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	/* read entire block's OOB at one go */
2828c2ecf20Sopenharmony_ci	err = mtd_read_oob(mtd, addr, &ops);
2838c2ecf20Sopenharmony_ci	if (mtd_is_bitflip(err))
2848c2ecf20Sopenharmony_ci		err = 0;
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	if (err || ops.oobretlen != len) {
2878c2ecf20Sopenharmony_ci		pr_err("error: readoob failed at %#llx\n",
2888c2ecf20Sopenharmony_ci		       (long long)addr);
2898c2ecf20Sopenharmony_ci		errcnt += 1;
2908c2ecf20Sopenharmony_ci		return err ? err : -1;
2918c2ecf20Sopenharmony_ci	}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	/* verify one page OOB at a time for bitflip per page limit check */
2948c2ecf20Sopenharmony_ci	for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
2958c2ecf20Sopenharmony_ci		bitflips = memcmpshow(addr, readbuf + (i * oobavail),
2968c2ecf20Sopenharmony_ci				      writebuf + (i * oobavail), oobavail);
2978c2ecf20Sopenharmony_ci		if (bitflips > bitflip_limit) {
2988c2ecf20Sopenharmony_ci			pr_err("error: verify failed at %#llx\n",
2998c2ecf20Sopenharmony_ci			       (long long)addr);
3008c2ecf20Sopenharmony_ci			errcnt += 1;
3018c2ecf20Sopenharmony_ci			if (errcnt > 1000) {
3028c2ecf20Sopenharmony_ci				pr_err("error: too many errors\n");
3038c2ecf20Sopenharmony_ci				return -1;
3048c2ecf20Sopenharmony_ci			}
3058c2ecf20Sopenharmony_ci		} else if (bitflips) {
3068c2ecf20Sopenharmony_ci			pr_info("ignoring error as within bitflip_limit\n");
3078c2ecf20Sopenharmony_ci		}
3088c2ecf20Sopenharmony_ci	}
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	return err;
3118c2ecf20Sopenharmony_ci}
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_cistatic int verify_all_eraseblocks(void)
3148c2ecf20Sopenharmony_ci{
3158c2ecf20Sopenharmony_ci	int err;
3168c2ecf20Sopenharmony_ci	unsigned int i;
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	pr_info("verifying all eraseblocks\n");
3198c2ecf20Sopenharmony_ci	for (i = 0; i < ebcnt; ++i) {
3208c2ecf20Sopenharmony_ci		if (bbt[i])
3218c2ecf20Sopenharmony_ci			continue;
3228c2ecf20Sopenharmony_ci		err = verify_eraseblock(i);
3238c2ecf20Sopenharmony_ci		if (err)
3248c2ecf20Sopenharmony_ci			return err;
3258c2ecf20Sopenharmony_ci		if (i % 256 == 0)
3268c2ecf20Sopenharmony_ci			pr_info("verified up to eraseblock %u\n", i);
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci		err = mtdtest_relax();
3298c2ecf20Sopenharmony_ci		if (err)
3308c2ecf20Sopenharmony_ci			return err;
3318c2ecf20Sopenharmony_ci	}
3328c2ecf20Sopenharmony_ci	pr_info("verified %u eraseblocks\n", i);
3338c2ecf20Sopenharmony_ci	return 0;
3348c2ecf20Sopenharmony_ci}
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_cistatic int __init mtd_oobtest_init(void)
3378c2ecf20Sopenharmony_ci{
3388c2ecf20Sopenharmony_ci	int err = 0;
3398c2ecf20Sopenharmony_ci	unsigned int i;
3408c2ecf20Sopenharmony_ci	uint64_t tmp;
3418c2ecf20Sopenharmony_ci	struct mtd_oob_ops ops;
3428c2ecf20Sopenharmony_ci	loff_t addr = 0, addr0;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	printk(KERN_INFO "\n");
3458c2ecf20Sopenharmony_ci	printk(KERN_INFO "=================================================\n");
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	if (dev < 0) {
3488c2ecf20Sopenharmony_ci		pr_info("Please specify a valid mtd-device via module parameter\n");
3498c2ecf20Sopenharmony_ci		pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
3508c2ecf20Sopenharmony_ci		return -EINVAL;
3518c2ecf20Sopenharmony_ci	}
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	pr_info("MTD device: %d\n", dev);
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	mtd = get_mtd_device(NULL, dev);
3568c2ecf20Sopenharmony_ci	if (IS_ERR(mtd)) {
3578c2ecf20Sopenharmony_ci		err = PTR_ERR(mtd);
3588c2ecf20Sopenharmony_ci		pr_err("error: cannot get MTD device\n");
3598c2ecf20Sopenharmony_ci		return err;
3608c2ecf20Sopenharmony_ci	}
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	if (!mtd_type_is_nand(mtd)) {
3638c2ecf20Sopenharmony_ci		pr_info("this test requires NAND flash\n");
3648c2ecf20Sopenharmony_ci		goto out;
3658c2ecf20Sopenharmony_ci	}
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	tmp = mtd->size;
3688c2ecf20Sopenharmony_ci	do_div(tmp, mtd->erasesize);
3698c2ecf20Sopenharmony_ci	ebcnt = tmp;
3708c2ecf20Sopenharmony_ci	pgcnt = mtd->erasesize / mtd->writesize;
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	pr_info("MTD device size %llu, eraseblock size %u, "
3738c2ecf20Sopenharmony_ci	       "page size %u, count of eraseblocks %u, pages per "
3748c2ecf20Sopenharmony_ci	       "eraseblock %u, OOB size %u\n",
3758c2ecf20Sopenharmony_ci	       (unsigned long long)mtd->size, mtd->erasesize,
3768c2ecf20Sopenharmony_ci	       mtd->writesize, ebcnt, pgcnt, mtd->oobsize);
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	err = -ENOMEM;
3798c2ecf20Sopenharmony_ci	readbuf = kmalloc(mtd->erasesize, GFP_KERNEL);
3808c2ecf20Sopenharmony_ci	if (!readbuf)
3818c2ecf20Sopenharmony_ci		goto out;
3828c2ecf20Sopenharmony_ci	writebuf = kmalloc(mtd->erasesize, GFP_KERNEL);
3838c2ecf20Sopenharmony_ci	if (!writebuf)
3848c2ecf20Sopenharmony_ci		goto out;
3858c2ecf20Sopenharmony_ci	bbt = kzalloc(ebcnt, GFP_KERNEL);
3868c2ecf20Sopenharmony_ci	if (!bbt)
3878c2ecf20Sopenharmony_ci		goto out;
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
3908c2ecf20Sopenharmony_ci	if (err)
3918c2ecf20Sopenharmony_ci		goto out;
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	use_offset = 0;
3948c2ecf20Sopenharmony_ci	use_len = mtd->oobavail;
3958c2ecf20Sopenharmony_ci	use_len_max = mtd->oobavail;
3968c2ecf20Sopenharmony_ci	vary_offset = 0;
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	/* First test: write all OOB, read it back and verify */
3998c2ecf20Sopenharmony_ci	pr_info("test 1 of 5\n");
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
4028c2ecf20Sopenharmony_ci	if (err)
4038c2ecf20Sopenharmony_ci		goto out;
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	prandom_seed_state(&rnd_state, 1);
4068c2ecf20Sopenharmony_ci	err = write_whole_device();
4078c2ecf20Sopenharmony_ci	if (err)
4088c2ecf20Sopenharmony_ci		goto out;
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	prandom_seed_state(&rnd_state, 1);
4118c2ecf20Sopenharmony_ci	err = verify_all_eraseblocks();
4128c2ecf20Sopenharmony_ci	if (err)
4138c2ecf20Sopenharmony_ci		goto out;
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	/*
4168c2ecf20Sopenharmony_ci	 * Second test: write all OOB, a block at a time, read it back and
4178c2ecf20Sopenharmony_ci	 * verify.
4188c2ecf20Sopenharmony_ci	 */
4198c2ecf20Sopenharmony_ci	pr_info("test 2 of 5\n");
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
4228c2ecf20Sopenharmony_ci	if (err)
4238c2ecf20Sopenharmony_ci		goto out;
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	prandom_seed_state(&rnd_state, 3);
4268c2ecf20Sopenharmony_ci	err = write_whole_device();
4278c2ecf20Sopenharmony_ci	if (err)
4288c2ecf20Sopenharmony_ci		goto out;
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	/* Check all eraseblocks */
4318c2ecf20Sopenharmony_ci	prandom_seed_state(&rnd_state, 3);
4328c2ecf20Sopenharmony_ci	pr_info("verifying all eraseblocks\n");
4338c2ecf20Sopenharmony_ci	for (i = 0; i < ebcnt; ++i) {
4348c2ecf20Sopenharmony_ci		if (bbt[i])
4358c2ecf20Sopenharmony_ci			continue;
4368c2ecf20Sopenharmony_ci		err = verify_eraseblock_in_one_go(i);
4378c2ecf20Sopenharmony_ci		if (err)
4388c2ecf20Sopenharmony_ci			goto out;
4398c2ecf20Sopenharmony_ci		if (i % 256 == 0)
4408c2ecf20Sopenharmony_ci			pr_info("verified up to eraseblock %u\n", i);
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci		err = mtdtest_relax();
4438c2ecf20Sopenharmony_ci		if (err)
4448c2ecf20Sopenharmony_ci			goto out;
4458c2ecf20Sopenharmony_ci	}
4468c2ecf20Sopenharmony_ci	pr_info("verified %u eraseblocks\n", i);
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	/*
4498c2ecf20Sopenharmony_ci	 * Third test: write OOB at varying offsets and lengths, read it back
4508c2ecf20Sopenharmony_ci	 * and verify.
4518c2ecf20Sopenharmony_ci	 */
4528c2ecf20Sopenharmony_ci	pr_info("test 3 of 5\n");
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci	err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
4558c2ecf20Sopenharmony_ci	if (err)
4568c2ecf20Sopenharmony_ci		goto out;
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	/* Write all eraseblocks */
4598c2ecf20Sopenharmony_ci	use_offset = 0;
4608c2ecf20Sopenharmony_ci	use_len = mtd->oobavail;
4618c2ecf20Sopenharmony_ci	use_len_max = mtd->oobavail;
4628c2ecf20Sopenharmony_ci	vary_offset = 1;
4638c2ecf20Sopenharmony_ci	prandom_seed_state(&rnd_state, 5);
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	err = write_whole_device();
4668c2ecf20Sopenharmony_ci	if (err)
4678c2ecf20Sopenharmony_ci		goto out;
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	/* Check all eraseblocks */
4708c2ecf20Sopenharmony_ci	use_offset = 0;
4718c2ecf20Sopenharmony_ci	use_len = mtd->oobavail;
4728c2ecf20Sopenharmony_ci	use_len_max = mtd->oobavail;
4738c2ecf20Sopenharmony_ci	vary_offset = 1;
4748c2ecf20Sopenharmony_ci	prandom_seed_state(&rnd_state, 5);
4758c2ecf20Sopenharmony_ci	err = verify_all_eraseblocks();
4768c2ecf20Sopenharmony_ci	if (err)
4778c2ecf20Sopenharmony_ci		goto out;
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci	use_offset = 0;
4808c2ecf20Sopenharmony_ci	use_len = mtd->oobavail;
4818c2ecf20Sopenharmony_ci	use_len_max = mtd->oobavail;
4828c2ecf20Sopenharmony_ci	vary_offset = 0;
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci	/* Fourth test: try to write off end of device */
4858c2ecf20Sopenharmony_ci	pr_info("test 4 of 5\n");
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
4888c2ecf20Sopenharmony_ci	if (err)
4898c2ecf20Sopenharmony_ci		goto out;
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci	addr0 = 0;
4928c2ecf20Sopenharmony_ci	for (i = 0; i < ebcnt && bbt[i]; ++i)
4938c2ecf20Sopenharmony_ci		addr0 += mtd->erasesize;
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	/* Attempt to write off end of OOB */
4968c2ecf20Sopenharmony_ci	ops.mode      = MTD_OPS_AUTO_OOB;
4978c2ecf20Sopenharmony_ci	ops.len       = 0;
4988c2ecf20Sopenharmony_ci	ops.retlen    = 0;
4998c2ecf20Sopenharmony_ci	ops.ooblen    = 1;
5008c2ecf20Sopenharmony_ci	ops.oobretlen = 0;
5018c2ecf20Sopenharmony_ci	ops.ooboffs   = mtd->oobavail;
5028c2ecf20Sopenharmony_ci	ops.datbuf    = NULL;
5038c2ecf20Sopenharmony_ci	ops.oobbuf    = writebuf;
5048c2ecf20Sopenharmony_ci	pr_info("attempting to start write past end of OOB\n");
5058c2ecf20Sopenharmony_ci	pr_info("an error is expected...\n");
5068c2ecf20Sopenharmony_ci	err = mtd_write_oob(mtd, addr0, &ops);
5078c2ecf20Sopenharmony_ci	if (err) {
5088c2ecf20Sopenharmony_ci		pr_info("error occurred as expected\n");
5098c2ecf20Sopenharmony_ci		err = 0;
5108c2ecf20Sopenharmony_ci	} else {
5118c2ecf20Sopenharmony_ci		pr_err("error: can write past end of OOB\n");
5128c2ecf20Sopenharmony_ci		errcnt += 1;
5138c2ecf20Sopenharmony_ci	}
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci	/* Attempt to read off end of OOB */
5168c2ecf20Sopenharmony_ci	ops.mode      = MTD_OPS_AUTO_OOB;
5178c2ecf20Sopenharmony_ci	ops.len       = 0;
5188c2ecf20Sopenharmony_ci	ops.retlen    = 0;
5198c2ecf20Sopenharmony_ci	ops.ooblen    = 1;
5208c2ecf20Sopenharmony_ci	ops.oobretlen = 0;
5218c2ecf20Sopenharmony_ci	ops.ooboffs   = mtd->oobavail;
5228c2ecf20Sopenharmony_ci	ops.datbuf    = NULL;
5238c2ecf20Sopenharmony_ci	ops.oobbuf    = readbuf;
5248c2ecf20Sopenharmony_ci	pr_info("attempting to start read past end of OOB\n");
5258c2ecf20Sopenharmony_ci	pr_info("an error is expected...\n");
5268c2ecf20Sopenharmony_ci	err = mtd_read_oob(mtd, addr0, &ops);
5278c2ecf20Sopenharmony_ci	if (mtd_is_bitflip(err))
5288c2ecf20Sopenharmony_ci		err = 0;
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	if (err) {
5318c2ecf20Sopenharmony_ci		pr_info("error occurred as expected\n");
5328c2ecf20Sopenharmony_ci		err = 0;
5338c2ecf20Sopenharmony_ci	} else {
5348c2ecf20Sopenharmony_ci		pr_err("error: can read past end of OOB\n");
5358c2ecf20Sopenharmony_ci		errcnt += 1;
5368c2ecf20Sopenharmony_ci	}
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	if (bbt[ebcnt - 1])
5398c2ecf20Sopenharmony_ci		pr_info("skipping end of device tests because last "
5408c2ecf20Sopenharmony_ci		       "block is bad\n");
5418c2ecf20Sopenharmony_ci	else {
5428c2ecf20Sopenharmony_ci		/* Attempt to write off end of device */
5438c2ecf20Sopenharmony_ci		ops.mode      = MTD_OPS_AUTO_OOB;
5448c2ecf20Sopenharmony_ci		ops.len       = 0;
5458c2ecf20Sopenharmony_ci		ops.retlen    = 0;
5468c2ecf20Sopenharmony_ci		ops.ooblen    = mtd->oobavail + 1;
5478c2ecf20Sopenharmony_ci		ops.oobretlen = 0;
5488c2ecf20Sopenharmony_ci		ops.ooboffs   = 0;
5498c2ecf20Sopenharmony_ci		ops.datbuf    = NULL;
5508c2ecf20Sopenharmony_ci		ops.oobbuf    = writebuf;
5518c2ecf20Sopenharmony_ci		pr_info("attempting to write past end of device\n");
5528c2ecf20Sopenharmony_ci		pr_info("an error is expected...\n");
5538c2ecf20Sopenharmony_ci		err = mtd_write_oob(mtd, mtd->size - mtd->writesize, &ops);
5548c2ecf20Sopenharmony_ci		if (err) {
5558c2ecf20Sopenharmony_ci			pr_info("error occurred as expected\n");
5568c2ecf20Sopenharmony_ci			err = 0;
5578c2ecf20Sopenharmony_ci		} else {
5588c2ecf20Sopenharmony_ci			pr_err("error: wrote past end of device\n");
5598c2ecf20Sopenharmony_ci			errcnt += 1;
5608c2ecf20Sopenharmony_ci		}
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci		/* Attempt to read off end of device */
5638c2ecf20Sopenharmony_ci		ops.mode      = MTD_OPS_AUTO_OOB;
5648c2ecf20Sopenharmony_ci		ops.len       = 0;
5658c2ecf20Sopenharmony_ci		ops.retlen    = 0;
5668c2ecf20Sopenharmony_ci		ops.ooblen    = mtd->oobavail + 1;
5678c2ecf20Sopenharmony_ci		ops.oobretlen = 0;
5688c2ecf20Sopenharmony_ci		ops.ooboffs   = 0;
5698c2ecf20Sopenharmony_ci		ops.datbuf    = NULL;
5708c2ecf20Sopenharmony_ci		ops.oobbuf    = readbuf;
5718c2ecf20Sopenharmony_ci		pr_info("attempting to read past end of device\n");
5728c2ecf20Sopenharmony_ci		pr_info("an error is expected...\n");
5738c2ecf20Sopenharmony_ci		err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops);
5748c2ecf20Sopenharmony_ci		if (mtd_is_bitflip(err))
5758c2ecf20Sopenharmony_ci			err = 0;
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci		if (err) {
5788c2ecf20Sopenharmony_ci			pr_info("error occurred as expected\n");
5798c2ecf20Sopenharmony_ci			err = 0;
5808c2ecf20Sopenharmony_ci		} else {
5818c2ecf20Sopenharmony_ci			pr_err("error: read past end of device\n");
5828c2ecf20Sopenharmony_ci			errcnt += 1;
5838c2ecf20Sopenharmony_ci		}
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci		err = mtdtest_erase_eraseblock(mtd, ebcnt - 1);
5868c2ecf20Sopenharmony_ci		if (err)
5878c2ecf20Sopenharmony_ci			goto out;
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci		/* Attempt to write off end of device */
5908c2ecf20Sopenharmony_ci		ops.mode      = MTD_OPS_AUTO_OOB;
5918c2ecf20Sopenharmony_ci		ops.len       = 0;
5928c2ecf20Sopenharmony_ci		ops.retlen    = 0;
5938c2ecf20Sopenharmony_ci		ops.ooblen    = mtd->oobavail;
5948c2ecf20Sopenharmony_ci		ops.oobretlen = 0;
5958c2ecf20Sopenharmony_ci		ops.ooboffs   = 1;
5968c2ecf20Sopenharmony_ci		ops.datbuf    = NULL;
5978c2ecf20Sopenharmony_ci		ops.oobbuf    = writebuf;
5988c2ecf20Sopenharmony_ci		pr_info("attempting to write past end of device\n");
5998c2ecf20Sopenharmony_ci		pr_info("an error is expected...\n");
6008c2ecf20Sopenharmony_ci		err = mtd_write_oob(mtd, mtd->size - mtd->writesize, &ops);
6018c2ecf20Sopenharmony_ci		if (err) {
6028c2ecf20Sopenharmony_ci			pr_info("error occurred as expected\n");
6038c2ecf20Sopenharmony_ci			err = 0;
6048c2ecf20Sopenharmony_ci		} else {
6058c2ecf20Sopenharmony_ci			pr_err("error: wrote past end of device\n");
6068c2ecf20Sopenharmony_ci			errcnt += 1;
6078c2ecf20Sopenharmony_ci		}
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci		/* Attempt to read off end of device */
6108c2ecf20Sopenharmony_ci		ops.mode      = MTD_OPS_AUTO_OOB;
6118c2ecf20Sopenharmony_ci		ops.len       = 0;
6128c2ecf20Sopenharmony_ci		ops.retlen    = 0;
6138c2ecf20Sopenharmony_ci		ops.ooblen    = mtd->oobavail;
6148c2ecf20Sopenharmony_ci		ops.oobretlen = 0;
6158c2ecf20Sopenharmony_ci		ops.ooboffs   = 1;
6168c2ecf20Sopenharmony_ci		ops.datbuf    = NULL;
6178c2ecf20Sopenharmony_ci		ops.oobbuf    = readbuf;
6188c2ecf20Sopenharmony_ci		pr_info("attempting to read past end of device\n");
6198c2ecf20Sopenharmony_ci		pr_info("an error is expected...\n");
6208c2ecf20Sopenharmony_ci		err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops);
6218c2ecf20Sopenharmony_ci		if (mtd_is_bitflip(err))
6228c2ecf20Sopenharmony_ci			err = 0;
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci		if (err) {
6258c2ecf20Sopenharmony_ci			pr_info("error occurred as expected\n");
6268c2ecf20Sopenharmony_ci			err = 0;
6278c2ecf20Sopenharmony_ci		} else {
6288c2ecf20Sopenharmony_ci			pr_err("error: read past end of device\n");
6298c2ecf20Sopenharmony_ci			errcnt += 1;
6308c2ecf20Sopenharmony_ci		}
6318c2ecf20Sopenharmony_ci	}
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci	/* Fifth test: write / read across block boundaries */
6348c2ecf20Sopenharmony_ci	pr_info("test 5 of 5\n");
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci	/* Erase all eraseblocks */
6378c2ecf20Sopenharmony_ci	err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
6388c2ecf20Sopenharmony_ci	if (err)
6398c2ecf20Sopenharmony_ci		goto out;
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci	/* Write all eraseblocks */
6428c2ecf20Sopenharmony_ci	prandom_seed_state(&rnd_state, 11);
6438c2ecf20Sopenharmony_ci	pr_info("writing OOBs of whole device\n");
6448c2ecf20Sopenharmony_ci	for (i = 0; i < ebcnt - 1; ++i) {
6458c2ecf20Sopenharmony_ci		int cnt = 2;
6468c2ecf20Sopenharmony_ci		int pg;
6478c2ecf20Sopenharmony_ci		size_t sz = mtd->oobavail;
6488c2ecf20Sopenharmony_ci		if (bbt[i] || bbt[i + 1])
6498c2ecf20Sopenharmony_ci			continue;
6508c2ecf20Sopenharmony_ci		addr = (loff_t)(i + 1) * mtd->erasesize - mtd->writesize;
6518c2ecf20Sopenharmony_ci		prandom_bytes_state(&rnd_state, writebuf, sz * cnt);
6528c2ecf20Sopenharmony_ci		for (pg = 0; pg < cnt; ++pg) {
6538c2ecf20Sopenharmony_ci			ops.mode      = MTD_OPS_AUTO_OOB;
6548c2ecf20Sopenharmony_ci			ops.len       = 0;
6558c2ecf20Sopenharmony_ci			ops.retlen    = 0;
6568c2ecf20Sopenharmony_ci			ops.ooblen    = sz;
6578c2ecf20Sopenharmony_ci			ops.oobretlen = 0;
6588c2ecf20Sopenharmony_ci			ops.ooboffs   = 0;
6598c2ecf20Sopenharmony_ci			ops.datbuf    = NULL;
6608c2ecf20Sopenharmony_ci			ops.oobbuf    = writebuf + pg * sz;
6618c2ecf20Sopenharmony_ci			err = mtd_write_oob(mtd, addr, &ops);
6628c2ecf20Sopenharmony_ci			if (err)
6638c2ecf20Sopenharmony_ci				goto out;
6648c2ecf20Sopenharmony_ci			if (i % 256 == 0)
6658c2ecf20Sopenharmony_ci				pr_info("written up to eraseblock %u\n", i);
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ci			err = mtdtest_relax();
6688c2ecf20Sopenharmony_ci			if (err)
6698c2ecf20Sopenharmony_ci				goto out;
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci			addr += mtd->writesize;
6728c2ecf20Sopenharmony_ci		}
6738c2ecf20Sopenharmony_ci	}
6748c2ecf20Sopenharmony_ci	pr_info("written %u eraseblocks\n", i);
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	/* Check all eraseblocks */
6778c2ecf20Sopenharmony_ci	prandom_seed_state(&rnd_state, 11);
6788c2ecf20Sopenharmony_ci	pr_info("verifying all eraseblocks\n");
6798c2ecf20Sopenharmony_ci	for (i = 0; i < ebcnt - 1; ++i) {
6808c2ecf20Sopenharmony_ci		if (bbt[i] || bbt[i + 1])
6818c2ecf20Sopenharmony_ci			continue;
6828c2ecf20Sopenharmony_ci		prandom_bytes_state(&rnd_state, writebuf, mtd->oobavail * 2);
6838c2ecf20Sopenharmony_ci		addr = (loff_t)(i + 1) * mtd->erasesize - mtd->writesize;
6848c2ecf20Sopenharmony_ci		ops.mode      = MTD_OPS_AUTO_OOB;
6858c2ecf20Sopenharmony_ci		ops.len       = 0;
6868c2ecf20Sopenharmony_ci		ops.retlen    = 0;
6878c2ecf20Sopenharmony_ci		ops.ooblen    = mtd->oobavail * 2;
6888c2ecf20Sopenharmony_ci		ops.oobretlen = 0;
6898c2ecf20Sopenharmony_ci		ops.ooboffs   = 0;
6908c2ecf20Sopenharmony_ci		ops.datbuf    = NULL;
6918c2ecf20Sopenharmony_ci		ops.oobbuf    = readbuf;
6928c2ecf20Sopenharmony_ci		err = mtd_read_oob(mtd, addr, &ops);
6938c2ecf20Sopenharmony_ci		if (mtd_is_bitflip(err))
6948c2ecf20Sopenharmony_ci			err = 0;
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci		if (err)
6978c2ecf20Sopenharmony_ci			goto out;
6988c2ecf20Sopenharmony_ci		if (memcmpshow(addr, readbuf, writebuf,
6998c2ecf20Sopenharmony_ci			       mtd->oobavail * 2)) {
7008c2ecf20Sopenharmony_ci			pr_err("error: verify failed at %#llx\n",
7018c2ecf20Sopenharmony_ci			       (long long)addr);
7028c2ecf20Sopenharmony_ci			errcnt += 1;
7038c2ecf20Sopenharmony_ci			if (errcnt > 1000) {
7048c2ecf20Sopenharmony_ci				pr_err("error: too many errors\n");
7058c2ecf20Sopenharmony_ci				goto out;
7068c2ecf20Sopenharmony_ci			}
7078c2ecf20Sopenharmony_ci		}
7088c2ecf20Sopenharmony_ci		if (i % 256 == 0)
7098c2ecf20Sopenharmony_ci			pr_info("verified up to eraseblock %u\n", i);
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci		err = mtdtest_relax();
7128c2ecf20Sopenharmony_ci		if (err)
7138c2ecf20Sopenharmony_ci			goto out;
7148c2ecf20Sopenharmony_ci	}
7158c2ecf20Sopenharmony_ci	pr_info("verified %u eraseblocks\n", i);
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ci	pr_info("finished with %d errors\n", errcnt);
7188c2ecf20Sopenharmony_ciout:
7198c2ecf20Sopenharmony_ci	kfree(bbt);
7208c2ecf20Sopenharmony_ci	kfree(writebuf);
7218c2ecf20Sopenharmony_ci	kfree(readbuf);
7228c2ecf20Sopenharmony_ci	put_mtd_device(mtd);
7238c2ecf20Sopenharmony_ci	if (err)
7248c2ecf20Sopenharmony_ci		pr_info("error %d occurred\n", err);
7258c2ecf20Sopenharmony_ci	printk(KERN_INFO "=================================================\n");
7268c2ecf20Sopenharmony_ci	return err;
7278c2ecf20Sopenharmony_ci}
7288c2ecf20Sopenharmony_cimodule_init(mtd_oobtest_init);
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_cistatic void __exit mtd_oobtest_exit(void)
7318c2ecf20Sopenharmony_ci{
7328c2ecf20Sopenharmony_ci	return;
7338c2ecf20Sopenharmony_ci}
7348c2ecf20Sopenharmony_cimodule_exit(mtd_oobtest_exit);
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Out-of-band test module");
7378c2ecf20Sopenharmony_ciMODULE_AUTHOR("Adrian Hunter");
7388c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
739