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-2007 Nokia Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Test sub-page read and write on MTD device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/mtd/mtd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include "mtd_test.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) static int dev = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) module_param(dev, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) MODULE_PARM_DESC(dev, "MTD device number to use");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static struct mtd_info *mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) static unsigned char *writebuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static unsigned char *readbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) static unsigned char *bbt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) static int subpgsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) static int bufsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) static int ebcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) static int pgcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) static int errcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) static struct rnd_state rnd_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) static inline void clear_data(unsigned char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	memset(buf, 0, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static int write_eraseblock(int ebnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	size_t written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	loff_t addr = (loff_t)ebnum * mtd->erasesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	prandom_bytes_state(&rnd_state, writebuf, subpgsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	err = mtd_write(mtd, addr, subpgsize, &written, writebuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	if (unlikely(err || written != subpgsize)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		pr_err("error: write failed at %#llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		       (long long)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		if (written != subpgsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 			pr_err("  write size: %#x\n", subpgsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 			pr_err("  written: %#zx\n", written);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		return err ? err : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	addr += subpgsize;
^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, subpgsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	err = mtd_write(mtd, addr, subpgsize, &written, writebuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	if (unlikely(err || written != subpgsize)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		pr_err("error: write failed at %#llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		       (long long)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		if (written != subpgsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 			pr_err("  write size: %#x\n", subpgsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 			pr_err("  written: %#zx\n", written);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		return err ? err : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) static int write_eraseblock2(int ebnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	size_t written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	int err = 0, k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	loff_t addr = (loff_t)ebnum * mtd->erasesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	for (k = 1; k < 33; ++k) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		if (addr + (subpgsize * k) > (loff_t)(ebnum + 1) * mtd->erasesize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		prandom_bytes_state(&rnd_state, writebuf, subpgsize * k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		err = mtd_write(mtd, addr, subpgsize * k, &written, writebuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		if (unlikely(err || written != subpgsize * k)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 			pr_err("error: write failed at %#llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 			       (long long)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 			if (written != subpgsize * k) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 				pr_err("  write size: %#x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 				       subpgsize * k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 				pr_err("  written: %#08zx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 				       written);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 			return err ? err : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		addr += subpgsize * k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static void print_subpage(unsigned char *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	for (i = 0; i < subpgsize; ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		for (j = 0; i < subpgsize && j < 32; ++i, ++j)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 			printk("%02x", *p++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		printk("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static int verify_eraseblock(int ebnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	size_t read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	loff_t addr = (loff_t)ebnum * mtd->erasesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	prandom_bytes_state(&rnd_state, writebuf, subpgsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	clear_data(readbuf, subpgsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	err = mtd_read(mtd, addr, subpgsize, &read, readbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	if (unlikely(err || read != subpgsize)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		if (mtd_is_bitflip(err) && read == subpgsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			pr_info("ECC correction at %#llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			       (long long)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 			err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			pr_err("error: read failed at %#llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 			       (long long)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			return err ? err : -1;
^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) 	if (unlikely(memcmp(readbuf, writebuf, subpgsize))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		pr_err("error: verify failed at %#llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		       (long long)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		pr_info("------------- written----------------\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		print_subpage(writebuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		pr_info("------------- read ------------------\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		print_subpage(readbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		pr_info("-------------------------------------\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		errcnt += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	addr += subpgsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	prandom_bytes_state(&rnd_state, writebuf, subpgsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	clear_data(readbuf, subpgsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	err = mtd_read(mtd, addr, subpgsize, &read, readbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	if (unlikely(err || read != subpgsize)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		if (mtd_is_bitflip(err) && read == subpgsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 			pr_info("ECC correction at %#llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 			       (long long)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 			pr_err("error: read failed at %#llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 			       (long long)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 			return err ? err : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	if (unlikely(memcmp(readbuf, writebuf, subpgsize))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		pr_info("error: verify failed at %#llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		       (long long)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		pr_info("------------- written----------------\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		print_subpage(writebuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		pr_info("------------- read ------------------\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		print_subpage(readbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		pr_info("-------------------------------------\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		errcnt += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) static int verify_eraseblock2(int ebnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	size_t read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	int err = 0, k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	loff_t addr = (loff_t)ebnum * mtd->erasesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	for (k = 1; k < 33; ++k) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		if (addr + (subpgsize * k) > (loff_t)(ebnum + 1) * mtd->erasesize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		prandom_bytes_state(&rnd_state, writebuf, subpgsize * k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		clear_data(readbuf, subpgsize * k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		err = mtd_read(mtd, addr, subpgsize * k, &read, readbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		if (unlikely(err || read != subpgsize * k)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 			if (mtd_is_bitflip(err) && read == subpgsize * k) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 				pr_info("ECC correction at %#llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 				       (long long)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 				err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 				pr_err("error: read failed at "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 				       "%#llx\n", (long long)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 				return err ? err : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		if (unlikely(memcmp(readbuf, writebuf, subpgsize * k))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			pr_err("error: verify failed at %#llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 			       (long long)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 			errcnt += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		addr += subpgsize * k;
^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) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) static int verify_eraseblock_ff(int ebnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	uint32_t j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	size_t read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	loff_t addr = (loff_t)ebnum * mtd->erasesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	memset(writebuf, 0xff, subpgsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	for (j = 0; j < mtd->erasesize / subpgsize; ++j) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		clear_data(readbuf, subpgsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		err = mtd_read(mtd, addr, subpgsize, &read, readbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		if (unlikely(err || read != subpgsize)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 			if (mtd_is_bitflip(err) && read == subpgsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 				pr_info("ECC correction 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) 				err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 				pr_err("error: read failed at "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 				       "%#llx\n", (long long)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 				return err ? err : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		if (unlikely(memcmp(readbuf, writebuf, subpgsize))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 			pr_err("error: verify 0xff failed at "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 			       "%#llx\n", (long long)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			errcnt += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		addr += subpgsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) static int verify_all_eraseblocks_ff(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	pr_info("verifying all eraseblocks for 0xff\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	for (i = 0; i < ebcnt; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		if (bbt[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		err = verify_eraseblock_ff(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		if (i % 256 == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			pr_info("verified up to eraseblock %u\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		err = mtdtest_relax();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	pr_info("verified %u eraseblocks\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) static int __init mtd_subpagetest_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	uint32_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	uint64_t tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	printk(KERN_INFO "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	printk(KERN_INFO "=================================================\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	if (dev < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		pr_info("Please specify a valid mtd-device via module parameter\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	pr_info("MTD device: %d\n", dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	mtd = get_mtd_device(NULL, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	if (IS_ERR(mtd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		err = PTR_ERR(mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		pr_err("error: cannot get MTD device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	if (!mtd_type_is_nand(mtd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		pr_info("this test requires NAND flash\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	subpgsize = mtd->writesize >> mtd->subpage_sft;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	tmp = mtd->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	do_div(tmp, mtd->erasesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	ebcnt = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	pgcnt = mtd->erasesize / mtd->writesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	pr_info("MTD device size %llu, eraseblock size %u, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	       "page size %u, subpage size %u, count of eraseblocks %u, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	       "pages per eraseblock %u, OOB size %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	       (unsigned long long)mtd->size, mtd->erasesize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	       mtd->writesize, subpgsize, ebcnt, pgcnt, mtd->oobsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	bufsize = subpgsize * 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	writebuf = kmalloc(bufsize, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	if (!writebuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	readbuf = kmalloc(bufsize, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	if (!readbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	bbt = kzalloc(ebcnt, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	if (!bbt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	pr_info("writing whole device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	prandom_seed_state(&rnd_state, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	for (i = 0; i < ebcnt; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		if (bbt[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		err = write_eraseblock(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		if (unlikely(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		if (i % 256 == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 			pr_info("written up to eraseblock %u\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		err = mtdtest_relax();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	pr_info("written %u eraseblocks\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	prandom_seed_state(&rnd_state, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	pr_info("verifying all eraseblocks\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	for (i = 0; i < ebcnt; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		if (bbt[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		err = verify_eraseblock(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		if (unlikely(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		if (i % 256 == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 			pr_info("verified up to eraseblock %u\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		err = mtdtest_relax();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	pr_info("verified %u eraseblocks\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	err = verify_all_eraseblocks_ff();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	/* Write all eraseblocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	prandom_seed_state(&rnd_state, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	pr_info("writing whole device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	for (i = 0; i < ebcnt; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		if (bbt[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		err = write_eraseblock2(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		if (unlikely(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		if (i % 256 == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 			pr_info("written up to eraseblock %u\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		err = mtdtest_relax();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	pr_info("written %u eraseblocks\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	/* Check all eraseblocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	prandom_seed_state(&rnd_state, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	pr_info("verifying all eraseblocks\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	for (i = 0; i < ebcnt; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		if (bbt[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		err = verify_eraseblock2(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		if (unlikely(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		if (i % 256 == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 			pr_info("verified up to eraseblock %u\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		err = mtdtest_relax();
^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) 	pr_info("verified %u eraseblocks\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	err = verify_all_eraseblocks_ff();
^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) 	pr_info("finished with %d errors\n", errcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	kfree(bbt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	kfree(readbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	kfree(writebuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	put_mtd_device(mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 		pr_info("error %d occurred\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	printk(KERN_INFO "=================================================\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) module_init(mtd_subpagetest_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) static void __exit mtd_subpagetest_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) module_exit(mtd_subpagetest_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) MODULE_DESCRIPTION("Subpage test module");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) MODULE_AUTHOR("Adrian Hunter");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) MODULE_LICENSE("GPL");