Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) 2006-2008 Nokia Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Test OOB read and write on MTD device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <asm/div64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/mtd/mtd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include "mtd_test.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) static int dev = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) static int bitflip_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) module_param(dev, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) MODULE_PARM_DESC(dev, "MTD device number to use");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) module_param(bitflip_limit, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) MODULE_PARM_DESC(bitflip_limit, "Max. allowed bitflips per page");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) static struct mtd_info *mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) static unsigned char *readbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) static unsigned char *writebuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) static unsigned char *bbt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) static int ebcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) static int pgcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) static int errcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) static int use_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) static int use_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) static int use_len_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) static int vary_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static struct rnd_state rnd_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static void do_vary_offset(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	use_len -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	if (use_len < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		use_offset += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		if (use_offset >= use_len_max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 			use_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		use_len = use_len_max - use_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) static int write_eraseblock(int ebnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	struct mtd_oob_ops ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	loff_t addr = (loff_t)ebnum * mtd->erasesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	prandom_bytes_state(&rnd_state, writebuf, use_len_max * pgcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		ops.mode      = MTD_OPS_AUTO_OOB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		ops.len       = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		ops.retlen    = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		ops.ooblen    = use_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		ops.oobretlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		ops.ooboffs   = use_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		ops.datbuf    = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		ops.oobbuf    = writebuf + (use_len_max * i) + use_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		err = mtd_write_oob(mtd, addr, &ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		if (err || ops.oobretlen != use_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 			pr_err("error: writeoob failed at %#llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 			       (long long)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 			pr_err("error: use_len %d, use_offset %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 			       use_len, use_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 			errcnt += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 			return err ? err : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		if (vary_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 			do_vary_offset();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) static int write_whole_device(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	pr_info("writing OOBs of whole device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	for (i = 0; i < ebcnt; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		if (bbt[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		err = write_eraseblock(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		if (i % 256 == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			pr_info("written up to eraseblock %u\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		err = mtdtest_relax();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	pr_info("written %u eraseblocks\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)  * Display the address, offset and data bytes at comparison failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)  * Return number of bitflips encountered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static size_t memcmpshowoffset(loff_t addr, loff_t offset, const void *cs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 			       const void *ct, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	const unsigned char *su1, *su2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	size_t i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	size_t bitflips = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--, i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		res = *su1 ^ *su2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		if (res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 			pr_info("error @addr[0x%lx:0x%lx] 0x%x -> 0x%x diff 0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 				(unsigned long)addr, (unsigned long)offset + i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 				*su1, *su2, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 			bitflips += hweight8(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	return bitflips;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) #define memcmpshow(addr, cs, ct, count) memcmpshowoffset((addr), 0, (cs), (ct),\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 							 (count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)  * Compare with 0xff and show the address, offset and data bytes at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)  * comparison failure. Return number of bitflips encountered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static size_t memffshow(loff_t addr, loff_t offset, const void *cs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	const unsigned char *su1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	size_t i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	size_t bitflips = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	for (su1 = cs; 0 < count; ++su1, count--, i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		res = *su1 ^ 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		if (res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 			pr_info("error @addr[0x%lx:0x%lx] 0x%x -> 0xff diff 0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 				(unsigned long)addr, (unsigned long)offset + i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 				*su1, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 			bitflips += hweight8(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	return bitflips;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static int verify_eraseblock(int ebnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	struct mtd_oob_ops ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	loff_t addr = (loff_t)ebnum * mtd->erasesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	size_t bitflips;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	prandom_bytes_state(&rnd_state, writebuf, use_len_max * pgcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		ops.mode      = MTD_OPS_AUTO_OOB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		ops.len       = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		ops.retlen    = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		ops.ooblen    = use_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		ops.oobretlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		ops.ooboffs   = use_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		ops.datbuf    = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		ops.oobbuf    = readbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		err = mtd_read_oob(mtd, addr, &ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		if (mtd_is_bitflip(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 			err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		if (err || ops.oobretlen != use_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 			pr_err("error: readoob failed at %#llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 			       (long long)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 			errcnt += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 			return err ? err : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		bitflips = memcmpshow(addr, readbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 				      writebuf + (use_len_max * i) + use_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 				      use_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		if (bitflips > bitflip_limit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 			pr_err("error: verify failed at %#llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 			       (long long)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 			errcnt += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 			if (errcnt > 1000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 				pr_err("error: too many errors\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 				return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		} else if (bitflips) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 			pr_info("ignoring error as within bitflip_limit\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		if (use_offset != 0 || use_len < mtd->oobavail) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			int k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 			ops.mode      = MTD_OPS_AUTO_OOB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 			ops.len       = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			ops.retlen    = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			ops.ooblen    = mtd->oobavail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			ops.oobretlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 			ops.ooboffs   = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 			ops.datbuf    = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			ops.oobbuf    = readbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 			err = mtd_read_oob(mtd, addr, &ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 			if (mtd_is_bitflip(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 				err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 			if (err || ops.oobretlen != mtd->oobavail) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 				pr_err("error: readoob failed at %#llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 						(long long)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 				errcnt += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 				return err ? err : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 			bitflips = memcmpshowoffset(addr, use_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 						    readbuf + use_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 						    writebuf + (use_len_max * i) + use_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 						    use_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 			/* verify pre-offset area for 0xff */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 			bitflips += memffshow(addr, 0, readbuf, use_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			/* verify post-(use_offset + use_len) area for 0xff */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			k = use_offset + use_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 			bitflips += memffshow(addr, k, readbuf + k,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 					      mtd->oobavail - k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 			if (bitflips > bitflip_limit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 				pr_err("error: verify failed at %#llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 						(long long)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 				errcnt += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 				if (errcnt > 1000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 					pr_err("error: too many errors\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 					return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 			} else if (bitflips) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 				pr_info("ignoring errors as within bitflip limit\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		if (vary_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			do_vary_offset();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) static int verify_eraseblock_in_one_go(int ebnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	struct mtd_oob_ops ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	loff_t addr = (loff_t)ebnum * mtd->erasesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	size_t len = mtd->oobavail * pgcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	size_t oobavail = mtd->oobavail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	size_t bitflips;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	prandom_bytes_state(&rnd_state, writebuf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	ops.mode      = MTD_OPS_AUTO_OOB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	ops.len       = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	ops.retlen    = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	ops.ooblen    = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	ops.oobretlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	ops.ooboffs   = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	ops.datbuf    = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	ops.oobbuf    = readbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	/* read entire block's OOB at one go */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	err = mtd_read_oob(mtd, addr, &ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	if (mtd_is_bitflip(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	if (err || ops.oobretlen != len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		pr_err("error: readoob failed at %#llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		       (long long)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		errcnt += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		return err ? err : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	/* verify one page OOB at a time for bitflip per page limit check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		bitflips = memcmpshow(addr, readbuf + (i * oobavail),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 				      writebuf + (i * oobavail), oobavail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		if (bitflips > bitflip_limit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 			pr_err("error: verify failed at %#llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 			       (long long)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 			errcnt += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 			if (errcnt > 1000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 				pr_err("error: too many errors\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 				return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		} else if (bitflips) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 			pr_info("ignoring error as within bitflip_limit\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) static int verify_all_eraseblocks(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	pr_info("verifying all eraseblocks\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	for (i = 0; i < ebcnt; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		if (bbt[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		err = verify_eraseblock(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		if (i % 256 == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 			pr_info("verified up to eraseblock %u\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		err = mtdtest_relax();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	pr_info("verified %u eraseblocks\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) static int __init mtd_oobtest_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	uint64_t tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	struct mtd_oob_ops ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	loff_t addr = 0, addr0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	printk(KERN_INFO "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	printk(KERN_INFO "=================================================\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	if (dev < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		pr_info("Please specify a valid mtd-device via module parameter\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	pr_info("MTD device: %d\n", dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	mtd = get_mtd_device(NULL, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	if (IS_ERR(mtd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		err = PTR_ERR(mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		pr_err("error: cannot get MTD device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	if (!mtd_type_is_nand(mtd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		pr_info("this test requires NAND flash\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	tmp = mtd->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	do_div(tmp, mtd->erasesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	ebcnt = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	pgcnt = mtd->erasesize / mtd->writesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	pr_info("MTD device size %llu, eraseblock size %u, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	       "page size %u, count of eraseblocks %u, pages per "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	       "eraseblock %u, OOB size %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	       (unsigned long long)mtd->size, mtd->erasesize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	       mtd->writesize, ebcnt, pgcnt, mtd->oobsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	readbuf = kmalloc(mtd->erasesize, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	if (!readbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	writebuf = kmalloc(mtd->erasesize, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	if (!writebuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	bbt = kzalloc(ebcnt, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	if (!bbt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	use_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	use_len = mtd->oobavail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	use_len_max = mtd->oobavail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	vary_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	/* First test: write all OOB, read it back and verify */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	pr_info("test 1 of 5\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	prandom_seed_state(&rnd_state, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	err = write_whole_device();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	prandom_seed_state(&rnd_state, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	err = verify_all_eraseblocks();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	 * Second test: write all OOB, a block at a time, read it back and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	 * verify.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	pr_info("test 2 of 5\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	prandom_seed_state(&rnd_state, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	err = write_whole_device();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	/* Check all eraseblocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	prandom_seed_state(&rnd_state, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	pr_info("verifying all eraseblocks\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	for (i = 0; i < ebcnt; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		if (bbt[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		err = verify_eraseblock_in_one_go(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		if (i % 256 == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 			pr_info("verified up to eraseblock %u\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		err = mtdtest_relax();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	pr_info("verified %u eraseblocks\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	 * Third test: write OOB at varying offsets and lengths, read it back
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	 * and verify.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	pr_info("test 3 of 5\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	/* Write all eraseblocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	use_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	use_len = mtd->oobavail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	use_len_max = mtd->oobavail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	vary_offset = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	prandom_seed_state(&rnd_state, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	err = write_whole_device();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	/* Check all eraseblocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	use_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	use_len = mtd->oobavail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	use_len_max = mtd->oobavail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	vary_offset = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	prandom_seed_state(&rnd_state, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	err = verify_all_eraseblocks();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	use_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	use_len = mtd->oobavail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	use_len_max = mtd->oobavail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	vary_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	/* Fourth test: try to write off end of device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	pr_info("test 4 of 5\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	addr0 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	for (i = 0; i < ebcnt && bbt[i]; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		addr0 += mtd->erasesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	/* Attempt to write off end of OOB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	ops.mode      = MTD_OPS_AUTO_OOB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	ops.len       = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	ops.retlen    = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	ops.ooblen    = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	ops.oobretlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	ops.ooboffs   = mtd->oobavail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	ops.datbuf    = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	ops.oobbuf    = writebuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	pr_info("attempting to start write past end of OOB\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	pr_info("an error is expected...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	err = mtd_write_oob(mtd, addr0, &ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 		pr_info("error occurred as expected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		pr_err("error: can write past end of OOB\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 		errcnt += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	/* Attempt to read off end of OOB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	ops.mode      = MTD_OPS_AUTO_OOB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	ops.len       = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	ops.retlen    = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	ops.ooblen    = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	ops.oobretlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	ops.ooboffs   = mtd->oobavail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	ops.datbuf    = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	ops.oobbuf    = readbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	pr_info("attempting to start read past end of OOB\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	pr_info("an error is expected...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	err = mtd_read_oob(mtd, addr0, &ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	if (mtd_is_bitflip(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 		err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 		pr_info("error occurred as expected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 		err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 		pr_err("error: can read past end of OOB\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 		errcnt += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	if (bbt[ebcnt - 1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 		pr_info("skipping end of device tests because last "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 		       "block is bad\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 		/* Attempt to write off end of device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 		ops.mode      = MTD_OPS_AUTO_OOB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 		ops.len       = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 		ops.retlen    = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 		ops.ooblen    = mtd->oobavail + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 		ops.oobretlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 		ops.ooboffs   = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 		ops.datbuf    = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 		ops.oobbuf    = writebuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 		pr_info("attempting to write past end of device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		pr_info("an error is expected...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		err = mtd_write_oob(mtd, mtd->size - mtd->writesize, &ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 			pr_info("error occurred as expected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 			err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 			pr_err("error: wrote past end of device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 			errcnt += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 		/* Attempt to read off end of device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 		ops.mode      = MTD_OPS_AUTO_OOB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 		ops.len       = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 		ops.retlen    = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 		ops.ooblen    = mtd->oobavail + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 		ops.oobretlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 		ops.ooboffs   = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 		ops.datbuf    = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 		ops.oobbuf    = readbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 		pr_info("attempting to read past end of device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 		pr_info("an error is expected...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 		err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 		if (mtd_is_bitflip(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 			err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 			pr_info("error occurred as expected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 			err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 			pr_err("error: read past end of device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 			errcnt += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 		err = mtdtest_erase_eraseblock(mtd, ebcnt - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 		/* Attempt to write off end of device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 		ops.mode      = MTD_OPS_AUTO_OOB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 		ops.len       = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 		ops.retlen    = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 		ops.ooblen    = mtd->oobavail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 		ops.oobretlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 		ops.ooboffs   = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 		ops.datbuf    = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 		ops.oobbuf    = writebuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 		pr_info("attempting to write past end of device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 		pr_info("an error is expected...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 		err = mtd_write_oob(mtd, mtd->size - mtd->writesize, &ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 			pr_info("error occurred as expected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 			err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 			pr_err("error: wrote past end of device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 			errcnt += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 		/* Attempt to read off end of device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 		ops.mode      = MTD_OPS_AUTO_OOB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 		ops.len       = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 		ops.retlen    = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 		ops.ooblen    = mtd->oobavail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 		ops.oobretlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 		ops.ooboffs   = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 		ops.datbuf    = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 		ops.oobbuf    = readbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 		pr_info("attempting to read past end of device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 		pr_info("an error is expected...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 		err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 		if (mtd_is_bitflip(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 			err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 			pr_info("error occurred as expected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 			err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 			pr_err("error: read past end of device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 			errcnt += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	/* Fifth test: write / read across block boundaries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	pr_info("test 5 of 5\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	/* Erase all eraseblocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 	/* Write all eraseblocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 	prandom_seed_state(&rnd_state, 11);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 	pr_info("writing OOBs of whole device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	for (i = 0; i < ebcnt - 1; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 		int cnt = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 		int pg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 		size_t sz = mtd->oobavail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 		if (bbt[i] || bbt[i + 1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 		addr = (loff_t)(i + 1) * mtd->erasesize - mtd->writesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 		prandom_bytes_state(&rnd_state, writebuf, sz * cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 		for (pg = 0; pg < cnt; ++pg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 			ops.mode      = MTD_OPS_AUTO_OOB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 			ops.len       = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 			ops.retlen    = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 			ops.ooblen    = sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 			ops.oobretlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 			ops.ooboffs   = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 			ops.datbuf    = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 			ops.oobbuf    = writebuf + pg * sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 			err = mtd_write_oob(mtd, addr, &ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 			if (i % 256 == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 				pr_info("written up to eraseblock %u\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 			err = mtdtest_relax();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 			addr += mtd->writesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 	pr_info("written %u eraseblocks\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 	/* Check all eraseblocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 	prandom_seed_state(&rnd_state, 11);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	pr_info("verifying all eraseblocks\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 	for (i = 0; i < ebcnt - 1; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 		if (bbt[i] || bbt[i + 1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 		prandom_bytes_state(&rnd_state, writebuf, mtd->oobavail * 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 		addr = (loff_t)(i + 1) * mtd->erasesize - mtd->writesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 		ops.mode      = MTD_OPS_AUTO_OOB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 		ops.len       = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 		ops.retlen    = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 		ops.ooblen    = mtd->oobavail * 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 		ops.oobretlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 		ops.ooboffs   = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 		ops.datbuf    = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 		ops.oobbuf    = readbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 		err = mtd_read_oob(mtd, addr, &ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 		if (mtd_is_bitflip(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 			err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 		if (memcmpshow(addr, readbuf, writebuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 			       mtd->oobavail * 2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 			pr_err("error: verify failed at %#llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 			       (long long)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 			errcnt += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 			if (errcnt > 1000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 				pr_err("error: too many errors\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 		if (i % 256 == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 			pr_info("verified up to eraseblock %u\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 		err = mtdtest_relax();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	pr_info("verified %u eraseblocks\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 	pr_info("finished with %d errors\n", errcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 	kfree(bbt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 	kfree(writebuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	kfree(readbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 	put_mtd_device(mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 		pr_info("error %d occurred\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 	printk(KERN_INFO "=================================================\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) module_init(mtd_oobtest_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) static void __exit mtd_oobtest_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) module_exit(mtd_oobtest_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) MODULE_DESCRIPTION("Out-of-band test module");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) MODULE_AUTHOR("Adrian Hunter");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) MODULE_LICENSE("GPL");